2 * backend.c -- Common back end for X and Windows NT versions of
4 * Copyright 1991 by Digital Equipment Corporation, Maynard,
7 * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
8 * 2007, 2008, 2009, 2010, 2011 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) );
60 int flock(int f, int code);
66 #define DoSleep( n ) if( (n) >= 0) sleep(n)
77 #include <sys/types.h>
86 #else /* not STDC_HEADERS */
89 # else /* not HAVE_STRING_H */
91 # endif /* not HAVE_STRING_H */
92 #endif /* not STDC_HEADERS */
95 # include <sys/fcntl.h>
96 #else /* not HAVE_SYS_FCNTL_H */
99 # endif /* HAVE_FCNTL_H */
100 #endif /* not HAVE_SYS_FCNTL_H */
102 #if TIME_WITH_SYS_TIME
103 # include <sys/time.h>
107 # include <sys/time.h>
113 #if defined(_amigados) && !defined(__GNUC__)
118 extern int gettimeofday(struct timeval *, struct timezone *);
126 #include "frontend.h"
133 #include "backendz.h"
137 # define _(s) gettext (s)
138 # define N_(s) gettext_noop (s)
139 # define T_(s) gettext(s)
152 /* A point in time */
154 long sec; /* Assuming this is >= 32 bits */
155 int ms; /* Assuming this is >= 16 bits */
158 int establish P((void));
159 void read_from_player P((InputSourceRef isr, VOIDSTAR closure,
160 char *buf, int count, int error));
161 void read_from_ics P((InputSourceRef isr, VOIDSTAR closure,
162 char *buf, int count, int error));
163 void ics_printf P((char *format, ...));
164 void SendToICS P((char *s));
165 void SendToICSDelayed P((char *s, long msdelay));
166 void SendMoveToICS P((ChessMove moveType, int fromX, int fromY, int toX, int toY, char promoChar));
167 void HandleMachineMove P((char *message, ChessProgramState *cps));
168 int AutoPlayOneMove P((void));
169 int LoadGameOneMove P((ChessMove readAhead));
170 int LoadGameFromFile P((char *filename, int n, char *title, int useList));
171 int LoadPositionFromFile P((char *filename, int n, char *title));
172 int SavePositionToFile P((char *filename));
173 void ApplyMove P((int fromX, int fromY, int toX, int toY, int promoChar,
175 void MakeMove P((int fromX, int fromY, int toX, int toY, int promoChar));
176 void ShowMove P((int fromX, int fromY, int toX, int toY));
177 int FinishMove P((ChessMove moveType, int fromX, int fromY, int toX, int toY,
178 /*char*/int promoChar));
179 void BackwardInner P((int target));
180 void ForwardInner P((int target));
181 int Adjudicate P((ChessProgramState *cps));
182 void GameEnds P((ChessMove result, char *resultDetails, int whosays));
183 void EditPositionDone P((Boolean fakeRights));
184 void PrintOpponents P((FILE *fp));
185 void PrintPosition P((FILE *fp, int move));
186 void StartChessProgram P((ChessProgramState *cps));
187 void SendToProgram P((char *message, ChessProgramState *cps));
188 void SendMoveToProgram P((int moveNum, ChessProgramState *cps));
189 void ReceiveFromProgram P((InputSourceRef isr, VOIDSTAR closure,
190 char *buf, int count, int error));
191 void SendTimeControl P((ChessProgramState *cps,
192 int mps, long tc, int inc, int sd, int st));
193 char *TimeControlTagValue P((void));
194 void Attention P((ChessProgramState *cps));
195 void FeedMovesToProgram P((ChessProgramState *cps, int upto));
196 int ResurrectChessProgram P((void));
197 void DisplayComment P((int moveNumber, char *text));
198 void DisplayMove P((int moveNumber));
200 void ParseGameHistory P((char *game));
201 void ParseBoard12 P((char *string));
202 void KeepAlive P((void));
203 void StartClocks P((void));
204 void SwitchClocks P((int nr));
205 void StopClocks P((void));
206 void ResetClocks P((void));
207 char *PGNDate P((void));
208 void SetGameInfo P((void));
209 Boolean ParseFEN P((Board board, int *blackPlaysFirst, char *fen));
210 int RegisterMove P((void));
211 void MakeRegisteredMove P((void));
212 void TruncateGame P((void));
213 int looking_at P((char *, int *, char *));
214 void CopyPlayerNameIntoFileName P((char **, char *));
215 char *SavePart P((char *));
216 int SaveGameOldStyle P((FILE *));
217 int SaveGamePGN P((FILE *));
218 void GetTimeMark P((TimeMark *));
219 long SubtractTimeMarks P((TimeMark *, TimeMark *));
220 int CheckFlags P((void));
221 long NextTickLength P((long));
222 void CheckTimeControl P((void));
223 void show_bytes P((FILE *, char *, int));
224 int string_to_rating P((char *str));
225 void ParseFeatures P((char* args, ChessProgramState *cps));
226 void InitBackEnd3 P((void));
227 void FeatureDone P((ChessProgramState* cps, int val));
228 void InitChessProgram P((ChessProgramState *cps, int setup));
229 void OutputKibitz(int window, char *text);
230 int PerpetualChase(int first, int last);
231 int EngineOutputIsUp();
232 void InitDrawingSizes(int x, int y);
233 void NextMatchGame P((void));
234 int NextTourneyGame P((int nr, int *swap));
235 int Pairing P((int nr, int nPlayers, int *w, int *b, int *sync));
236 FILE *WriteTourneyFile P((char *results));
239 extern void ConsoleCreate();
242 ChessProgramState *WhitePlayer();
243 void InsertIntoMemo P((int which, char *text)); // [HGM] kibitz: in engineo.c
244 int VerifyDisplayMode P(());
246 char *GetInfoFromComment( int, char * ); // [HGM] PV time: returns stripped comment
247 void InitEngineUCI( const char * iniDir, ChessProgramState * cps ); // [HGM] moved here from winboard.c
248 char *ProbeBook P((int moveNr, char *book)); // [HGM] book: returns a book move
249 char *SendMoveToBookUser P((int nr, ChessProgramState *cps, int initial)); // [HGM] book
250 void ics_update_width P((int new_width));
251 extern char installDir[MSG_SIZ];
252 VariantClass startVariant; /* [HGM] nicks: initial variant */
255 extern int tinyLayout, smallLayout;
256 ChessProgramStats programStats;
257 char lastPV[2][2*MSG_SIZ]; /* [HGM] pv: last PV in thinking output of each engine */
259 static int exiting = 0; /* [HGM] moved to top */
260 static int setboardSpoiledMachineBlack = 0 /*, errorExitFlag = 0*/;
261 int startedFromPositionFile = FALSE; Board filePosition; /* [HGM] loadPos */
262 Board partnerBoard; /* [HGM] bughouse: for peeking at partner game */
263 int partnerHighlight[2];
264 Boolean partnerBoardValid = 0;
265 char partnerStatus[MSG_SIZ];
267 Boolean originalFlip;
268 Boolean twoBoards = 0;
269 char endingGame = 0; /* [HGM] crash: flag to prevent recursion of GameEnds() */
270 int whiteNPS, blackNPS; /* [HGM] nps: for easily making clocks aware of NPS */
271 VariantClass currentlyInitializedVariant; /* [HGM] variantswitch */
272 int lastIndex = 0; /* [HGM] autoinc: last game/position used in match mode */
273 Boolean connectionAlive;/* [HGM] alive: ICS connection status from probing */
274 int opponentKibitzes;
275 int lastSavedGame; /* [HGM] save: ID of game */
276 char chatPartner[MAX_CHAT][MSG_SIZ]; /* [HGM] chat: list of chatting partners */
277 extern int chatCount;
279 char marker[BOARD_RANKS][BOARD_FILES]; /* [HGM] marks for target squares */
280 char lastMsg[MSG_SIZ];
281 ChessSquare pieceSweep = EmptySquare;
282 ChessSquare promoSweep = EmptySquare, defaultPromoChoice;
283 int promoDefaultAltered;
285 /* States for ics_getting_history */
287 #define H_REQUESTED 1
288 #define H_GOT_REQ_HEADER 2
289 #define H_GOT_UNREQ_HEADER 3
290 #define H_GETTING_MOVES 4
291 #define H_GOT_UNWANTED_HEADER 5
293 /* whosays values for GameEnds */
302 /* Maximum number of games in a cmail message */
303 #define CMAIL_MAX_GAMES 20
305 /* Different types of move when calling RegisterMove */
307 #define CMAIL_RESIGN 1
309 #define CMAIL_ACCEPT 3
311 /* Different types of result to remember for each game */
312 #define CMAIL_NOT_RESULT 0
313 #define CMAIL_OLD_RESULT 1
314 #define CMAIL_NEW_RESULT 2
316 /* Telnet protocol constants */
327 safeStrCpy( char *dst, const char *src, size_t count )
330 assert( dst != NULL );
331 assert( src != NULL );
334 for(i=0; i<count; i++) if((dst[i] = src[i]) == NULLCHAR) break;
335 if( i == count && dst[count-1] != NULLCHAR)
337 dst[ count-1 ] = '\0'; // make sure incomplete copy still null-terminated
338 if(appData.debugMode)
339 fprintf(debugFP, "safeStrCpy: copying %s into %s didn't work, not enough space %d\n",src,dst, (int)count);
345 /* Some compiler can't cast u64 to double
346 * This function do the job for us:
348 * We use the highest bit for cast, this only
349 * works if the highest bit is not
350 * in use (This should not happen)
352 * We used this for all compiler
355 u64ToDouble(u64 value)
358 u64 tmp = value & u64Const(0x7fffffffffffffff);
359 r = (double)(s64)tmp;
360 if (value & u64Const(0x8000000000000000))
361 r += 9.2233720368547758080e18; /* 2^63 */
365 /* Fake up flags for now, as we aren't keeping track of castling
366 availability yet. [HGM] Change of logic: the flag now only
367 indicates the type of castlings allowed by the rule of the game.
368 The actual rights themselves are maintained in the array
369 castlingRights, as part of the game history, and are not probed
375 int flags = F_ALL_CASTLE_OK;
376 if ((index % 2) == 0) flags |= F_WHITE_ON_MOVE;
377 switch (gameInfo.variant) {
379 flags &= ~F_ALL_CASTLE_OK;
380 case VariantGiveaway: // [HGM] moved this case label one down: seems Giveaway does have castling on ICC!
381 flags |= F_IGNORE_CHECK;
383 flags |= F_MANDATORY_CAPTURE; //[HGM] losers: sets flag so TestLegality rejects non-capts if capts exist
386 flags |= F_IGNORE_CHECK | F_ATOMIC_CAPTURE;
388 case VariantKriegspiel:
389 flags |= F_KRIEGSPIEL_CAPTURE;
391 case VariantCapaRandom:
392 case VariantFischeRandom:
393 flags |= F_FRC_TYPE_CASTLING; /* [HGM] enable this through flag */
394 case VariantNoCastle:
395 case VariantShatranj:
398 flags &= ~F_ALL_CASTLE_OK;
406 FILE *gameFileFP, *debugFP;
409 [AS] Note: sometimes, the sscanf() function is used to parse the input
410 into a fixed-size buffer. Because of this, we must be prepared to
411 receive strings as long as the size of the input buffer, which is currently
412 set to 4K for Windows and 8K for the rest.
413 So, we must either allocate sufficiently large buffers here, or
414 reduce the size of the input buffer in the input reading part.
417 char cmailMove[CMAIL_MAX_GAMES][MOVE_LEN], cmailMsg[MSG_SIZ];
418 char bookOutput[MSG_SIZ*10], thinkOutput[MSG_SIZ*10], lastHint[MSG_SIZ];
419 char thinkOutput1[MSG_SIZ*10];
421 ChessProgramState first, second, pairing;
423 /* premove variables */
426 int premoveFromX = 0;
427 int premoveFromY = 0;
428 int premovePromoChar = 0;
430 Boolean alarmSounded;
431 /* end premove variables */
433 char *ics_prefix = "$";
434 int ics_type = ICS_GENERIC;
436 int currentMove = 0, forwardMostMove = 0, backwardMostMove = 0;
437 int pauseExamForwardMostMove = 0;
438 int nCmailGames = 0, nCmailResults = 0, nCmailMovesRegistered = 0;
439 int cmailMoveRegistered[CMAIL_MAX_GAMES], cmailResult[CMAIL_MAX_GAMES];
440 int cmailMsgLoaded = FALSE, cmailMailedMove = FALSE;
441 int cmailOldMove = -1, firstMove = TRUE, flipView = FALSE;
442 int blackPlaysFirst = FALSE, startedFromSetupPosition = FALSE;
443 int searchTime = 0, pausing = FALSE, pauseExamInvalid = FALSE;
444 int whiteFlag = FALSE, blackFlag = FALSE;
445 int userOfferedDraw = FALSE;
446 int ics_user_moved = 0, ics_gamenum = -1, ics_getting_history = H_FALSE;
447 int matchMode = FALSE, hintRequested = FALSE, bookRequested = FALSE;
448 int cmailMoveType[CMAIL_MAX_GAMES];
449 long ics_clock_paused = 0;
450 ProcRef icsPR = NoProc, cmailPR = NoProc;
451 InputSourceRef telnetISR = NULL, fromUserISR = NULL, cmailISR = NULL;
452 GameMode gameMode = BeginningOfGame;
453 char moveList[MAX_MOVES][MOVE_LEN], parseList[MAX_MOVES][MOVE_LEN * 2];
454 char *commentList[MAX_MOVES], *cmailCommentList[CMAIL_MAX_GAMES];
455 ChessProgramStats_Move pvInfoList[MAX_MOVES]; /* [AS] Info about engine thinking */
456 int hiddenThinkOutputState = 0; /* [AS] */
457 int adjudicateLossThreshold = 0; /* [AS] Automatic adjudication */
458 int adjudicateLossPlies = 6;
459 char white_holding[64], black_holding[64];
460 TimeMark lastNodeCountTime;
461 long lastNodeCount=0;
462 int shiftKey; // [HGM] set by mouse handler
464 int have_sent_ICS_logon = 0;
466 int suddenDeath, whiteStartMove, blackStartMove; /* [HGM] for implementation of 'any per time' sessions, as in first part of byoyomi TC */
467 long whiteTimeRemaining, blackTimeRemaining, timeControl, timeIncrement, lastWhite, lastBlack;
468 long timeControl_2; /* [AS] Allow separate time controls */
469 char *fullTimeControlString = NULL, *nextSession, *whiteTC, *blackTC; /* [HGM] secondary TC: merge of MPS, TC and inc */
470 long timeRemaining[2][MAX_MOVES];
471 int matchGame = 0, nextGame = 0, roundNr = 0;
472 Boolean waitingForGame = FALSE;
473 TimeMark programStartTime, pauseStart;
474 char ics_handle[MSG_SIZ];
475 int have_set_title = 0;
477 /* animateTraining preserves the state of appData.animate
478 * when Training mode is activated. This allows the
479 * response to be animated when appData.animate == TRUE and
480 * appData.animateDragging == TRUE.
482 Boolean animateTraining;
488 Board boards[MAX_MOVES];
489 /* [HGM] Following 7 needed for accurate legality tests: */
490 signed char castlingRank[BOARD_FILES]; // and corresponding ranks
491 signed char initialRights[BOARD_FILES];
492 int nrCastlingRights; // For TwoKings, or to implement castling-unknown status
493 int initialRulePlies, FENrulePlies;
494 FILE *serverMoves = NULL; // next two for broadcasting (/serverMoves option)
497 int mute; // mute all sounds
499 // [HGM] vari: next 12 to save and restore variations
500 #define MAX_VARIATIONS 10
501 int framePtr = MAX_MOVES-1; // points to free stack entry
503 int savedFirst[MAX_VARIATIONS];
504 int savedLast[MAX_VARIATIONS];
505 int savedFramePtr[MAX_VARIATIONS];
506 char *savedDetails[MAX_VARIATIONS];
507 ChessMove savedResult[MAX_VARIATIONS];
509 void PushTail P((int firstMove, int lastMove));
510 Boolean PopTail P((Boolean annotate));
511 void PushInner P((int firstMove, int lastMove));
512 void PopInner P((Boolean annotate));
513 void CleanupTail P((void));
515 ChessSquare FIDEArray[2][BOARD_FILES] = {
516 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
517 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
518 { BlackRook, BlackKnight, BlackBishop, BlackQueen,
519 BlackKing, BlackBishop, BlackKnight, BlackRook }
522 ChessSquare twoKingsArray[2][BOARD_FILES] = {
523 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
524 WhiteKing, WhiteKing, WhiteKnight, WhiteRook },
525 { BlackRook, BlackKnight, BlackBishop, BlackQueen,
526 BlackKing, BlackKing, BlackKnight, BlackRook }
529 ChessSquare KnightmateArray[2][BOARD_FILES] = {
530 { WhiteRook, WhiteMan, WhiteBishop, WhiteQueen,
531 WhiteUnicorn, WhiteBishop, WhiteMan, WhiteRook },
532 { BlackRook, BlackMan, BlackBishop, BlackQueen,
533 BlackUnicorn, BlackBishop, BlackMan, BlackRook }
536 ChessSquare SpartanArray[2][BOARD_FILES] = {
537 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
538 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
539 { BlackAlfil, BlackMarshall, BlackKing, BlackDragon,
540 BlackDragon, BlackKing, BlackAngel, BlackAlfil }
543 ChessSquare fairyArray[2][BOARD_FILES] = { /* [HGM] Queen side differs from King side */
544 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
545 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
546 { BlackCardinal, BlackAlfil, BlackMarshall, BlackAngel,
547 BlackKing, BlackMarshall, BlackAlfil, BlackCardinal }
550 ChessSquare ShatranjArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
551 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteKing,
552 WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
553 { BlackRook, BlackKnight, BlackAlfil, BlackKing,
554 BlackFerz, BlackAlfil, BlackKnight, BlackRook }
557 ChessSquare makrukArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
558 { WhiteRook, WhiteKnight, WhiteMan, WhiteKing,
559 WhiteFerz, WhiteMan, WhiteKnight, WhiteRook },
560 { BlackRook, BlackKnight, BlackMan, BlackFerz,
561 BlackKing, BlackMan, BlackKnight, BlackRook }
565 #if (BOARD_FILES>=10)
566 ChessSquare ShogiArray[2][BOARD_FILES] = {
567 { WhiteQueen, WhiteKnight, WhiteFerz, WhiteWazir,
568 WhiteKing, WhiteWazir, WhiteFerz, WhiteKnight, WhiteQueen },
569 { BlackQueen, BlackKnight, BlackFerz, BlackWazir,
570 BlackKing, BlackWazir, BlackFerz, BlackKnight, BlackQueen }
573 ChessSquare XiangqiArray[2][BOARD_FILES] = {
574 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteFerz,
575 WhiteWazir, WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
576 { BlackRook, BlackKnight, BlackAlfil, BlackFerz,
577 BlackWazir, BlackFerz, BlackAlfil, BlackKnight, BlackRook }
580 ChessSquare CapablancaArray[2][BOARD_FILES] = {
581 { WhiteRook, WhiteKnight, WhiteAngel, WhiteBishop, WhiteQueen,
582 WhiteKing, WhiteBishop, WhiteMarshall, WhiteKnight, WhiteRook },
583 { BlackRook, BlackKnight, BlackAngel, BlackBishop, BlackQueen,
584 BlackKing, BlackBishop, BlackMarshall, BlackKnight, BlackRook }
587 ChessSquare GreatArray[2][BOARD_FILES] = {
588 { WhiteDragon, WhiteKnight, WhiteAlfil, WhiteGrasshopper, WhiteKing,
589 WhiteSilver, WhiteCardinal, WhiteAlfil, WhiteKnight, WhiteDragon },
590 { BlackDragon, BlackKnight, BlackAlfil, BlackGrasshopper, BlackKing,
591 BlackSilver, BlackCardinal, BlackAlfil, BlackKnight, BlackDragon },
594 ChessSquare JanusArray[2][BOARD_FILES] = {
595 { WhiteRook, WhiteAngel, WhiteKnight, WhiteBishop, WhiteKing,
596 WhiteQueen, WhiteBishop, WhiteKnight, WhiteAngel, WhiteRook },
597 { BlackRook, BlackAngel, BlackKnight, BlackBishop, BlackKing,
598 BlackQueen, BlackBishop, BlackKnight, BlackAngel, BlackRook }
602 ChessSquare GothicArray[2][BOARD_FILES] = {
603 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen, WhiteMarshall,
604 WhiteKing, WhiteAngel, WhiteBishop, WhiteKnight, WhiteRook },
605 { BlackRook, BlackKnight, BlackBishop, BlackQueen, BlackMarshall,
606 BlackKing, BlackAngel, BlackBishop, BlackKnight, BlackRook }
609 #define GothicArray CapablancaArray
613 ChessSquare FalconArray[2][BOARD_FILES] = {
614 { WhiteRook, WhiteKnight, WhiteBishop, WhiteLance, WhiteQueen,
615 WhiteKing, WhiteLance, WhiteBishop, WhiteKnight, WhiteRook },
616 { BlackRook, BlackKnight, BlackBishop, BlackLance, BlackQueen,
617 BlackKing, BlackLance, BlackBishop, BlackKnight, BlackRook }
620 #define FalconArray CapablancaArray
623 #else // !(BOARD_FILES>=10)
624 #define XiangqiPosition FIDEArray
625 #define CapablancaArray FIDEArray
626 #define GothicArray FIDEArray
627 #define GreatArray FIDEArray
628 #endif // !(BOARD_FILES>=10)
630 #if (BOARD_FILES>=12)
631 ChessSquare CourierArray[2][BOARD_FILES] = {
632 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteBishop, WhiteMan, WhiteKing,
633 WhiteFerz, WhiteWazir, WhiteBishop, WhiteAlfil, WhiteKnight, WhiteRook },
634 { BlackRook, BlackKnight, BlackAlfil, BlackBishop, BlackMan, BlackKing,
635 BlackFerz, BlackWazir, BlackBishop, BlackAlfil, BlackKnight, BlackRook }
637 #else // !(BOARD_FILES>=12)
638 #define CourierArray CapablancaArray
639 #endif // !(BOARD_FILES>=12)
642 Board initialPosition;
645 /* Convert str to a rating. Checks for special cases of "----",
647 "++++", etc. Also strips ()'s */
649 string_to_rating(str)
652 while(*str && !isdigit(*str)) ++str;
654 return 0; /* One of the special "no rating" cases */
662 /* Init programStats */
663 programStats.movelist[0] = 0;
664 programStats.depth = 0;
665 programStats.nr_moves = 0;
666 programStats.moves_left = 0;
667 programStats.nodes = 0;
668 programStats.time = -1; // [HGM] PGNtime: make invalid to recognize engine output
669 programStats.score = 0;
670 programStats.got_only_move = 0;
671 programStats.got_fail = 0;
672 programStats.line_is_book = 0;
677 { // [HGM] moved some code here from InitBackend1 that has to be done after both engines have contributed their settings
678 if (appData.firstPlaysBlack) {
679 first.twoMachinesColor = "black\n";
680 second.twoMachinesColor = "white\n";
682 first.twoMachinesColor = "white\n";
683 second.twoMachinesColor = "black\n";
686 first.other = &second;
687 second.other = &first;
690 if(appData.timeOddsMode) {
691 norm = appData.timeOdds[0];
692 if(norm > appData.timeOdds[1]) norm = appData.timeOdds[1];
694 first.timeOdds = appData.timeOdds[0]/norm;
695 second.timeOdds = appData.timeOdds[1]/norm;
698 if(programVersion) free(programVersion);
699 if (appData.noChessProgram) {
700 programVersion = (char*) malloc(5 + strlen(PACKAGE_STRING));
701 sprintf(programVersion, "%s", PACKAGE_STRING);
703 /* [HGM] tidy: use tidy name, in stead of full pathname (which was probably a bug due to / vs \ ) */
704 programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
705 sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
710 UnloadEngine(ChessProgramState *cps)
712 /* Kill off first chess program */
713 if (cps->isr != NULL)
714 RemoveInputSource(cps->isr);
717 if (cps->pr != NoProc) {
719 DoSleep( appData.delayBeforeQuit );
720 SendToProgram("quit\n", cps);
721 DoSleep( appData.delayAfterQuit );
722 DestroyChildProcess(cps->pr, cps->useSigterm);
725 if(appData.debugMode) fprintf(debugFP, "Unload %s\n", cps->which);
729 ClearOptions(ChessProgramState *cps)
732 cps->nrOptions = cps->comboCnt = 0;
733 for(i=0; i<MAX_OPTIONS; i++) {
734 cps->option[i].min = cps->option[i].max = cps->option[i].value = 0;
735 cps->option[i].textValue = 0;
739 char *engineNames[] = {
745 InitEngine(ChessProgramState *cps, int n)
746 { // [HGM] all engine initialiation put in a function that does one engine
750 cps->which = engineNames[n];
751 cps->maybeThinking = FALSE;
755 cps->sendDrawOffers = 1;
757 cps->program = appData.chessProgram[n];
758 cps->host = appData.host[n];
759 cps->dir = appData.directory[n];
760 cps->initString = appData.engInitString[n];
761 cps->computerString = appData.computerString[n];
762 cps->useSigint = TRUE;
763 cps->useSigterm = TRUE;
764 cps->reuse = appData.reuse[n];
765 cps->nps = appData.NPS[n]; // [HGM] nps: copy nodes per second
766 cps->useSetboard = FALSE;
768 cps->usePing = FALSE;
771 cps->usePlayother = FALSE;
772 cps->useColors = TRUE;
773 cps->useUsermove = FALSE;
774 cps->sendICS = FALSE;
775 cps->sendName = appData.icsActive;
776 cps->sdKludge = FALSE;
777 cps->stKludge = FALSE;
778 TidyProgramName(cps->program, cps->host, cps->tidy);
780 safeStrCpy(cps->variants, appData.variant, MSG_SIZ);
781 cps->analysisSupport = 2; /* detect */
782 cps->analyzing = FALSE;
783 cps->initDone = FALSE;
785 /* New features added by Tord: */
786 cps->useFEN960 = FALSE;
787 cps->useOOCastle = TRUE;
788 /* End of new features added by Tord. */
789 cps->fenOverride = appData.fenOverride[n];
791 /* [HGM] time odds: set factor for each machine */
792 cps->timeOdds = appData.timeOdds[n];
794 /* [HGM] secondary TC: how to handle sessions that do not fit in 'level'*/
795 cps->accumulateTC = appData.accumulateTC[n];
796 cps->maxNrOfSessions = 1;
801 cps->supportsNPS = UNKNOWN;
802 cps->memSize = FALSE;
803 cps->maxCores = FALSE;
804 cps->egtFormats[0] = NULLCHAR;
807 cps->optionSettings = appData.engOptions[n];
809 cps->scoreIsAbsolute = appData.scoreIsAbsolute[n]; /* [AS] */
810 cps->isUCI = appData.isUCI[n]; /* [AS] */
811 cps->hasOwnBookUCI = appData.hasOwnBookUCI[n]; /* [AS] */
813 if (appData.protocolVersion[n] > PROTOVER
814 || appData.protocolVersion[n] < 1)
819 len = snprintf(buf, MSG_SIZ, _("protocol version %d not supported"),
820 appData.protocolVersion[n]);
821 if( (len > MSG_SIZ) && appData.debugMode )
822 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
824 DisplayFatalError(buf, 0, 2);
828 cps->protocolVersion = appData.protocolVersion[n];
831 InitEngineUCI( installDir, cps ); // [HGM] moved here from winboard.c, to make available in xboard
834 ChessProgramState *savCps;
840 if(WaitForEngine(savCps, LoadEngine)) return;
841 CommonEngineInit(); // recalculate time odds
842 if(gameInfo.variant != StringToVariant(appData.variant)) {
843 // we changed variant when loading the engine; this forces us to reset
844 Reset(TRUE, savCps != &first);
845 EditGameEvent(); // for consistency with other path, as Reset changes mode
847 InitChessProgram(savCps, FALSE);
848 SendToProgram("force\n", savCps);
849 DisplayMessage("", "");
850 if (startedFromSetupPosition) SendBoard(savCps, backwardMostMove);
851 for (i = backwardMostMove; i < forwardMostMove; i++) SendMoveToProgram(i, savCps);
857 ReplaceEngine(ChessProgramState *cps, int n)
861 appData.noChessProgram = FALSE;
862 appData.clockMode = TRUE;
864 if(n) return; // only startup first engine immediately; second can wait
865 savCps = cps; // parameter to LoadEngine passed as globals, to allow scheduled calling :-(
869 extern char *engineName, *engineDir, *engineChoice, *engineLine, *nickName, *params;
870 extern Boolean isUCI, hasBook, storeVariant, v1, addToList, useNick;
872 static char resetOptions[] =
873 "-reuse -firstIsUCI false -firstHasOwnBookUCI true -firstTimeOdds 1 "
874 "-firstOptions \"\" -firstNPS -1 -fn \"\"";
877 Load(ChessProgramState *cps, int i)
879 char *p, *q, buf[MSG_SIZ], command[MSG_SIZ], buf2[MSG_SIZ];
880 if(engineLine[0]) { // an engine was selected from the combo box
881 snprintf(buf, MSG_SIZ, "-fcp %s", engineLine);
882 SwapEngines(i); // kludge to parse -f* / -first* like it is -s* / -second*
883 ParseArgsFromString(resetOptions); appData.fenOverride[0] = NULL;
884 ParseArgsFromString(buf);
886 ReplaceEngine(cps, i);
890 while(q = strchr(p, SLASH)) p = q+1;
891 if(*p== NULLCHAR) { DisplayError(_("You did not specify the engine executable"), 0); return; }
892 if(engineDir[0] != NULLCHAR)
893 appData.directory[i] = engineDir;
894 else if(p != engineName) { // derive directory from engine path, when not given
896 appData.directory[i] = strdup(engineName);
898 } else appData.directory[i] = ".";
899 if(strchr(p, ' ') && !strchr(p, '"')) snprintf(buf2, MSG_SIZ, "\"%s\"", p), p = buf2; // quote if it contains spaces
901 snprintf(command, MSG_SIZ, "%s %s", p, params);
904 appData.chessProgram[i] = strdup(p);
905 appData.isUCI[i] = isUCI;
906 appData.protocolVersion[i] = v1 ? 1 : PROTOVER;
907 appData.hasOwnBookUCI[i] = hasBook;
908 if(!nickName[0]) useNick = FALSE;
909 if(useNick) ASSIGN(appData.pgnName[i], nickName);
913 q = firstChessProgramNames;
914 if(nickName[0]) snprintf(buf, MSG_SIZ, "\"%s\" -fcp ", nickName); else buf[0] = NULLCHAR;
915 quote = strchr(p, '"') ? '\'' : '"'; // use single quotes around engine command if it contains double quotes
916 snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), "%c%s%c -fd \"%s\"%s%s%s%s%s%s%s%s\n",
917 quote, p, quote, appData.directory[i],
918 useNick ? " -fn \"" : "",
919 useNick ? nickName : "",
921 v1 ? " -firstProtocolVersion 1" : "",
922 hasBook ? "" : " -fNoOwnBookUCI",
923 isUCI ? (isUCI == TRUE ? " -fUCI" : gameInfo.variant == VariantShogi ? " -fUSI" : " -fUCCI") : "",
924 storeVariant ? " -variant " : "",
925 storeVariant ? VariantName(gameInfo.variant) : "");
926 firstChessProgramNames = malloc(len = strlen(q) + strlen(buf) + 1);
927 snprintf(firstChessProgramNames, len, "%s%s", q, buf);
930 ReplaceEngine(cps, i);
936 int matched, min, sec;
938 * Parse timeControl resource
940 if (!ParseTimeControl(appData.timeControl, appData.timeIncrement,
941 appData.movesPerSession)) {
943 snprintf(buf, sizeof(buf), _("bad timeControl option %s"), appData.timeControl);
944 DisplayFatalError(buf, 0, 2);
948 * Parse searchTime resource
950 if (*appData.searchTime != NULLCHAR) {
951 matched = sscanf(appData.searchTime, "%d:%d", &min, &sec);
953 searchTime = min * 60;
954 } else if (matched == 2) {
955 searchTime = min * 60 + sec;
958 snprintf(buf, sizeof(buf), _("bad searchTime option %s"), appData.searchTime);
959 DisplayFatalError(buf, 0, 2);
968 ShowThinkingEvent(); // [HGM] thinking: make sure post/nopost state is set according to options
969 startVariant = StringToVariant(appData.variant); // [HGM] nicks: remember original variant
971 GetTimeMark(&programStartTime);
972 srandom((programStartTime.ms + 1000*programStartTime.sec)*0x1001001); // [HGM] book: makes sure random is unpredictabe to msec level
973 pauseStart = programStartTime; pauseStart.sec -= 100; // [HGM] matchpause: fake a pause that has long since ended
976 programStats.ok_to_send = 1;
977 programStats.seen_stat = 0;
980 * Initialize game list
986 * Internet chess server status
988 if (appData.icsActive) {
989 appData.matchMode = FALSE;
990 appData.matchGames = 0;
992 appData.noChessProgram = !appData.zippyPlay;
994 appData.zippyPlay = FALSE;
995 appData.zippyTalk = FALSE;
996 appData.noChessProgram = TRUE;
998 if (*appData.icsHelper != NULLCHAR) {
999 appData.useTelnet = TRUE;
1000 appData.telnetProgram = appData.icsHelper;
1003 appData.zippyTalk = appData.zippyPlay = FALSE;
1006 /* [AS] Initialize pv info list [HGM] and game state */
1010 for( i=0; i<=framePtr; i++ ) {
1011 pvInfoList[i].depth = -1;
1012 boards[i][EP_STATUS] = EP_NONE;
1013 for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
1019 /* [AS] Adjudication threshold */
1020 adjudicateLossThreshold = appData.adjudicateLossThreshold;
1022 InitEngine(&first, 0);
1023 InitEngine(&second, 1);
1026 pairing.which = "pairing"; // pairing engine
1027 pairing.pr = NoProc;
1029 pairing.program = appData.pairingEngine;
1030 pairing.host = "localhost";
1033 if (appData.icsActive) {
1034 appData.clockMode = TRUE; /* changes dynamically in ICS mode */
1035 } else if (appData.noChessProgram) { // [HGM] st: searchTime mode now also is clockMode
1036 appData.clockMode = FALSE;
1037 first.sendTime = second.sendTime = 0;
1041 /* Override some settings from environment variables, for backward
1042 compatibility. Unfortunately it's not feasible to have the env
1043 vars just set defaults, at least in xboard. Ugh.
1045 if (appData.icsActive && (appData.zippyPlay || appData.zippyTalk)) {
1050 if (!appData.icsActive) {
1054 /* Check for variants that are supported only in ICS mode,
1055 or not at all. Some that are accepted here nevertheless
1056 have bugs; see comments below.
1058 VariantClass variant = StringToVariant(appData.variant);
1060 case VariantBughouse: /* need four players and two boards */
1061 case VariantKriegspiel: /* need to hide pieces and move details */
1062 /* case VariantFischeRandom: (Fabien: moved below) */
1063 len = snprintf(buf,MSG_SIZ, _("Variant %s supported only in ICS mode"), appData.variant);
1064 if( (len > MSG_SIZ) && appData.debugMode )
1065 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
1067 DisplayFatalError(buf, 0, 2);
1070 case VariantUnknown:
1071 case VariantLoadable:
1081 len = snprintf(buf, MSG_SIZ, _("Unknown variant name %s"), appData.variant);
1082 if( (len > MSG_SIZ) && appData.debugMode )
1083 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
1085 DisplayFatalError(buf, 0, 2);
1088 case VariantXiangqi: /* [HGM] repetition rules not implemented */
1089 case VariantFairy: /* [HGM] TestLegality definitely off! */
1090 case VariantGothic: /* [HGM] should work */
1091 case VariantCapablanca: /* [HGM] should work */
1092 case VariantCourier: /* [HGM] initial forced moves not implemented */
1093 case VariantShogi: /* [HGM] could still mate with pawn drop */
1094 case VariantKnightmate: /* [HGM] should work */
1095 case VariantCylinder: /* [HGM] untested */
1096 case VariantFalcon: /* [HGM] untested */
1097 case VariantCrazyhouse: /* holdings not shown, ([HGM] fixed that!)
1098 offboard interposition not understood */
1099 case VariantNormal: /* definitely works! */
1100 case VariantWildCastle: /* pieces not automatically shuffled */
1101 case VariantNoCastle: /* pieces not automatically shuffled */
1102 case VariantFischeRandom: /* [HGM] works and shuffles pieces */
1103 case VariantLosers: /* should work except for win condition,
1104 and doesn't know captures are mandatory */
1105 case VariantSuicide: /* should work except for win condition,
1106 and doesn't know captures are mandatory */
1107 case VariantGiveaway: /* should work except for win condition,
1108 and doesn't know captures are mandatory */
1109 case VariantTwoKings: /* should work */
1110 case VariantAtomic: /* should work except for win condition */
1111 case Variant3Check: /* should work except for win condition */
1112 case VariantShatranj: /* should work except for all win conditions */
1113 case VariantMakruk: /* should work except for daw countdown */
1114 case VariantBerolina: /* might work if TestLegality is off */
1115 case VariantCapaRandom: /* should work */
1116 case VariantJanus: /* should work */
1117 case VariantSuper: /* experimental */
1118 case VariantGreat: /* experimental, requires legality testing to be off */
1119 case VariantSChess: /* S-Chess, should work */
1120 case VariantSpartan: /* should work */
1127 int NextIntegerFromString( char ** str, long * value )
1132 while( *s == ' ' || *s == '\t' ) {
1138 if( *s >= '0' && *s <= '9' ) {
1139 while( *s >= '0' && *s <= '9' ) {
1140 *value = *value * 10 + (*s - '0');
1152 int NextTimeControlFromString( char ** str, long * value )
1155 int result = NextIntegerFromString( str, &temp );
1158 *value = temp * 60; /* Minutes */
1159 if( **str == ':' ) {
1161 result = NextIntegerFromString( str, &temp );
1162 *value += temp; /* Seconds */
1169 int NextSessionFromString( char ** str, int *moves, long * tc, long *inc, int *incType)
1170 { /* [HGM] routine added to read '+moves/time' for secondary time control. */
1171 int result = -1, type = 0; long temp, temp2;
1173 if(**str != ':') return -1; // old params remain in force!
1175 if(**str == '*') type = *(*str)++, temp = 0; // sandclock TC
1176 if( NextIntegerFromString( str, &temp ) ) return -1;
1177 if(type) { *moves = 0; *tc = temp * 500; *inc = temp * 1000; *incType = '*'; return 0; }
1180 /* time only: incremental or sudden-death time control */
1181 if(**str == '+') { /* increment follows; read it */
1183 if(**str == '!') type = *(*str)++; // Bronstein TC
1184 if(result = NextIntegerFromString( str, &temp2)) return -1;
1185 *inc = temp2 * 1000;
1186 if(**str == '.') { // read fraction of increment
1187 char *start = ++(*str);
1188 if(result = NextIntegerFromString( str, &temp2)) return -1;
1190 while(start++ < *str) temp2 /= 10;
1194 *moves = 0; *tc = temp * 1000; *incType = type;
1198 (*str)++; /* classical time control */
1199 result = NextIntegerFromString( str, &temp2); // NOTE: already converted to seconds by ParseTimeControl()
1210 int GetTimeQuota(int movenr, int lastUsed, char *tcString)
1211 { /* [HGM] get time to add from the multi-session time-control string */
1212 int incType, moves=1; /* kludge to force reading of first session */
1213 long time, increment;
1216 if(!*s) return 0; // empty TC string means we ran out of the last sudden-death version
1217 if(appData.debugMode) fprintf(debugFP, "TC string = '%s'\n", tcString);
1219 if(moves) NextSessionFromString(&s, &moves, &time, &increment, &incType);
1220 nextSession = s; suddenDeath = moves == 0 && increment == 0;
1221 if(appData.debugMode) fprintf(debugFP, "mps=%d tc=%d inc=%d\n", moves, (int) time, (int) increment);
1222 if(movenr == -1) return time; /* last move before new session */
1223 if(incType == '*') increment = 0; else // for sandclock, time is added while not thinking
1224 if(incType == '!' && lastUsed < increment) increment = lastUsed;
1225 if(!moves) return increment; /* current session is incremental */
1226 if(movenr >= 0) movenr -= moves; /* we already finished this session */
1227 } while(movenr >= -1); /* try again for next session */
1229 return 0; // no new time quota on this move
1233 ParseTimeControl(tc, ti, mps)
1240 char buf[MSG_SIZ], buf2[MSG_SIZ], *mytc = tc;
1243 if(ti >= 0 && !strchr(tc, '+') && !strchr(tc, '/') ) mps = 0;
1244 if(!strchr(tc, '+') && !strchr(tc, '/') && sscanf(tc, "%d:%d", &min, &sec) >= 1)
1245 sprintf(mytc=buf2, "%d", 60*min+sec); // convert 'classical' min:sec tc string to seconds
1249 snprintf(buf, MSG_SIZ, ":%d/%s+%g", mps, mytc, ti);
1251 snprintf(buf, MSG_SIZ, ":%s+%g", mytc, ti);
1254 snprintf(buf, MSG_SIZ, ":%d/%s", mps, mytc);
1256 snprintf(buf, MSG_SIZ, ":%s", mytc);
1258 fullTimeControlString = StrSave(buf); // this should now be in PGN format
1260 if( NextTimeControlFromString( &tc, &tc1 ) != 0 ) {
1265 /* Parse second time control */
1268 if( NextTimeControlFromString( &tc, &tc2 ) != 0 ) {
1276 timeControl_2 = tc2 * 1000;
1286 timeControl = tc1 * 1000;
1289 timeIncrement = ti * 1000; /* convert to ms */
1290 movesPerSession = 0;
1293 movesPerSession = mps;
1301 if (appData.debugMode) {
1302 fprintf(debugFP, "%s\n", programVersion);
1305 set_cont_sequence(appData.wrapContSeq);
1306 if (appData.matchGames > 0) {
1307 appData.matchMode = TRUE;
1308 } else if (appData.matchMode) {
1309 appData.matchGames = 1;
1311 if(appData.matchMode && appData.sameColorGames > 0) /* [HGM] alternate: overrule matchGames */
1312 appData.matchGames = appData.sameColorGames;
1313 if(appData.rewindIndex > 1) { /* [HGM] autoinc: rewind implies auto-increment and overrules given index */
1314 if(appData.loadPositionIndex >= 0) appData.loadPositionIndex = -1;
1315 if(appData.loadGameIndex >= 0) appData.loadGameIndex = -1;
1318 if (appData.noChessProgram || first.protocolVersion == 1) {
1321 /* kludge: allow timeout for initial "feature" commands */
1323 DisplayMessage("", _("Starting chess program"));
1324 ScheduleDelayedEvent(InitBackEnd3, FEATURE_TIMEOUT);
1329 CalculateIndex(int index, int gameNr)
1330 { // [HGM] autoinc: absolute way to determine load index from game number (taking auto-inc and rewind into account)
1332 if(index > 0) return index; // fixed nmber
1333 if(index == 0) return 1;
1334 res = (index == -1 ? gameNr : (gameNr-1)/2 + 1); // autoinc
1335 if(appData.rewindIndex > 0) res = (res-1) % appData.rewindIndex + 1; // rewind
1340 LoadGameOrPosition(int gameNr)
1341 { // [HGM] taken out of MatchEvent and NextMatchGame (to combine it)
1342 if (*appData.loadGameFile != NULLCHAR) {
1343 if (!LoadGameFromFile(appData.loadGameFile,
1344 CalculateIndex(appData.loadGameIndex, gameNr),
1345 appData.loadGameFile, FALSE)) {
1346 DisplayFatalError(_("Bad game file"), 0, 1);
1349 } else if (*appData.loadPositionFile != NULLCHAR) {
1350 if (!LoadPositionFromFile(appData.loadPositionFile,
1351 CalculateIndex(appData.loadPositionIndex, gameNr),
1352 appData.loadPositionFile)) {
1353 DisplayFatalError(_("Bad position file"), 0, 1);
1361 ReserveGame(int gameNr, char resChar)
1363 FILE *tf = fopen(appData.tourneyFile, "r+");
1364 char *p, *q, c, buf[MSG_SIZ];
1365 if(tf == NULL) { nextGame = appData.matchGames + 1; return; } // kludge to terminate match
1366 safeStrCpy(buf, lastMsg, MSG_SIZ);
1367 DisplayMessage(_("Pick new game"), "");
1368 flock(fileno(tf), LOCK_EX); // lock the tourney file while we are messing with it
1369 ParseArgsFromFile(tf);
1370 p = q = appData.results;
1371 if(appData.debugMode) {
1372 char *r = appData.participants;
1373 fprintf(debugFP, "results = '%s'\n", p);
1374 while(*r) fprintf(debugFP, *r >= ' ' ? "%c" : "\\%03o", *r), r++;
1375 fprintf(debugFP, "\n");
1377 while(*q && *q != ' ') q++; // get first un-played game (could be beyond end!)
1379 q = malloc(strlen(p) + 2); // could be arbitrary long, but allow to extend by one!
1380 safeStrCpy(q, p, strlen(p) + 2);
1381 if(gameNr >= 0) q[gameNr] = resChar; // replace '*' with result
1382 if(appData.debugMode) fprintf(debugFP, "pick next game from '%s': %d\n", q, nextGame);
1383 if(nextGame <= appData.matchGames && resChar != ' ') { // already reserve next game, if tourney not yet done
1384 if(q[nextGame] == NULLCHAR) q[nextGame+1] = NULLCHAR; // append one char
1387 fseek(tf, -(strlen(p)+4), SEEK_END);
1389 if(c != '"') // depending on DOS or Unix line endings we can be one off
1390 fseek(tf, -(strlen(p)+2), SEEK_END);
1391 else fseek(tf, -(strlen(p)+3), SEEK_END);
1392 fprintf(tf, "%s\"\n", q); fclose(tf); // update, and flush by closing
1393 DisplayMessage(buf, "");
1394 free(p); appData.results = q;
1395 if(nextGame <= appData.matchGames && resChar != ' ' &&
1396 (gameNr < 0 || nextGame / appData.defaultMatchGames != gameNr / appData.defaultMatchGames)) {
1397 UnloadEngine(&first); // next game belongs to other pairing;
1398 UnloadEngine(&second); // already unload the engines, so TwoMachinesEvent will load new ones.
1403 MatchEvent(int mode)
1404 { // [HGM] moved out of InitBackend3, to make it callable when match starts through menu
1406 if(matchMode) { // already in match mode: switch it off
1408 appData.matchGames = appData.tourneyFile[0] ? nextGame: matchGame; // kludge to let match terminate after next game.
1409 ModeHighlight(); // kludgey way to remove checkmark...
1412 // if(gameMode != BeginningOfGame) {
1413 // DisplayError(_("You can only start a match from the initial position."), 0);
1417 if(mode == 2) appData.matchGames = appData.defaultMatchGames;
1418 /* Set up machine vs. machine match */
1420 NextTourneyGame(-1, &dummy); // sets appData.matchGames if this is tourney, to make sure ReserveGame knows it
1421 if(appData.tourneyFile[0]) {
1423 if(nextGame > appData.matchGames) {
1425 if(strchr(appData.results, '*') == NULL) {
1427 appData.tourneyCycles++;
1428 if(f = WriteTourneyFile(appData.results)) { // make a tourney file with increased number of cycles
1430 NextTourneyGame(-1, &dummy);
1432 if(nextGame <= appData.matchGames) {
1433 DisplayNote(_("You restarted an already completed tourney\nOne more cycle will now be added to it\nGames commence in 10 sec"));
1435 ScheduleDelayedEvent(NextMatchGame, 10000);
1440 snprintf(buf, MSG_SIZ, _("All games in tourney '%s' are already played or playing"), appData.tourneyFile);
1441 DisplayError(buf, 0);
1442 appData.tourneyFile[0] = 0;
1446 if (appData.noChessProgram) { // [HGM] in tourney engines are loaded automatically
1447 DisplayFatalError(_("Can't have a match with no chess programs"),
1452 matchGame = roundNr = 1;
1453 first.matchWins = second.matchWins = 0; // [HGM] match: needed in later matches
\r
1458 InitBackEnd3 P((void))
1460 GameMode initialMode;
1464 InitChessProgram(&first, startedFromSetupPosition);
1466 if(!appData.noChessProgram) { /* [HGM] tidy: redo program version to use name from myname feature */
1467 free(programVersion);
1468 programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
1469 sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
1472 if (appData.icsActive) {
1474 /* [DM] Make a console window if needed [HGM] merged ifs */
1480 if (*appData.icsCommPort != NULLCHAR)
1481 len = snprintf(buf, MSG_SIZ, _("Could not open comm port %s"),
1482 appData.icsCommPort);
1484 len = snprintf(buf, MSG_SIZ, _("Could not connect to host %s, port %s"),
1485 appData.icsHost, appData.icsPort);
1487 if( (len > MSG_SIZ) && appData.debugMode )
1488 fprintf(debugFP, "InitBackEnd3: buffer truncated.\n");
1490 DisplayFatalError(buf, err, 1);
1495 AddInputSource(icsPR, FALSE, read_from_ics, &telnetISR);
1497 AddInputSource(NoProc, FALSE, read_from_player, &fromUserISR);
1498 if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
1499 ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1500 } else if (appData.noChessProgram) {
1506 if (*appData.cmailGameName != NULLCHAR) {
1508 OpenLoopback(&cmailPR);
1510 AddInputSource(cmailPR, FALSE, CmailSigHandlerCallBack, &cmailISR);
1514 DisplayMessage("", "");
1515 if (StrCaseCmp(appData.initialMode, "") == 0) {
1516 initialMode = BeginningOfGame;
1517 if(!appData.icsActive && appData.noChessProgram) { // [HGM] could be fall-back
1518 gameMode = MachinePlaysBlack; // "Machine Black" might have been implicitly highlighted
1519 ModeHighlight(); // make sure XBoard knows it is highlighted, so it will un-highlight it
1520 gameMode = BeginningOfGame; // in case BeginningOfGame now means "Edit Position"
1523 } else if (StrCaseCmp(appData.initialMode, "TwoMachines") == 0) {
1524 initialMode = TwoMachinesPlay;
1525 } else if (StrCaseCmp(appData.initialMode, "AnalyzeFile") == 0) {
1526 initialMode = AnalyzeFile;
1527 } else if (StrCaseCmp(appData.initialMode, "Analysis") == 0) {
1528 initialMode = AnalyzeMode;
1529 } else if (StrCaseCmp(appData.initialMode, "MachineWhite") == 0) {
1530 initialMode = MachinePlaysWhite;
1531 } else if (StrCaseCmp(appData.initialMode, "MachineBlack") == 0) {
1532 initialMode = MachinePlaysBlack;
1533 } else if (StrCaseCmp(appData.initialMode, "EditGame") == 0) {
1534 initialMode = EditGame;
1535 } else if (StrCaseCmp(appData.initialMode, "EditPosition") == 0) {
1536 initialMode = EditPosition;
1537 } else if (StrCaseCmp(appData.initialMode, "Training") == 0) {
1538 initialMode = Training;
1540 len = snprintf(buf, MSG_SIZ, _("Unknown initialMode %s"), appData.initialMode);
1541 if( (len > MSG_SIZ) && appData.debugMode )
1542 fprintf(debugFP, "InitBackEnd3: buffer truncated.\n");
1544 DisplayFatalError(buf, 0, 2);
1548 if (appData.matchMode) {
1549 if(appData.tourneyFile[0]) { // start tourney from command line
1551 if(f = fopen(appData.tourneyFile, "r")) {
1552 ParseArgsFromFile(f); // make sure tourney parmeters re known
1554 } else appData.tourneyFile[0] = NULLCHAR; // for now ignore bad tourney file
1557 } else if (*appData.cmailGameName != NULLCHAR) {
1558 /* Set up cmail mode */
1559 ReloadCmailMsgEvent(TRUE);
1561 /* Set up other modes */
1562 if (initialMode == AnalyzeFile) {
1563 if (*appData.loadGameFile == NULLCHAR) {
1564 DisplayFatalError(_("AnalyzeFile mode requires a game file"), 0, 1);
1568 if (*appData.loadGameFile != NULLCHAR) {
1569 (void) LoadGameFromFile(appData.loadGameFile,
1570 appData.loadGameIndex,
1571 appData.loadGameFile, TRUE);
1572 } else if (*appData.loadPositionFile != NULLCHAR) {
1573 (void) LoadPositionFromFile(appData.loadPositionFile,
1574 appData.loadPositionIndex,
1575 appData.loadPositionFile);
1576 /* [HGM] try to make self-starting even after FEN load */
1577 /* to allow automatic setup of fairy variants with wtm */
1578 if(initialMode == BeginningOfGame && !blackPlaysFirst) {
1579 gameMode = BeginningOfGame;
1580 setboardSpoiledMachineBlack = 1;
1582 /* [HGM] loadPos: make that every new game uses the setup */
1583 /* from file as long as we do not switch variant */
1584 if(!blackPlaysFirst) {
1585 startedFromPositionFile = TRUE;
1586 CopyBoard(filePosition, boards[0]);
1589 if (initialMode == AnalyzeMode) {
1590 if (appData.noChessProgram) {
1591 DisplayFatalError(_("Analysis mode requires a chess engine"), 0, 2);
1594 if (appData.icsActive) {
1595 DisplayFatalError(_("Analysis mode does not work with ICS mode"),0,2);
1599 } else if (initialMode == AnalyzeFile) {
1600 appData.showThinking = TRUE; // [HGM] thinking: moved out of ShowThinkingEvent
1601 ShowThinkingEvent();
1603 AnalysisPeriodicEvent(1);
1604 } else if (initialMode == MachinePlaysWhite) {
1605 if (appData.noChessProgram) {
1606 DisplayFatalError(_("MachineWhite mode requires a chess engine"),
1610 if (appData.icsActive) {
1611 DisplayFatalError(_("MachineWhite mode does not work with ICS mode"),
1615 MachineWhiteEvent();
1616 } else if (initialMode == MachinePlaysBlack) {
1617 if (appData.noChessProgram) {
1618 DisplayFatalError(_("MachineBlack mode requires a chess engine"),
1622 if (appData.icsActive) {
1623 DisplayFatalError(_("MachineBlack mode does not work with ICS mode"),
1627 MachineBlackEvent();
1628 } else if (initialMode == TwoMachinesPlay) {
1629 if (appData.noChessProgram) {
1630 DisplayFatalError(_("TwoMachines mode requires a chess engine"),
1634 if (appData.icsActive) {
1635 DisplayFatalError(_("TwoMachines mode does not work with ICS mode"),
1640 } else if (initialMode == EditGame) {
1642 } else if (initialMode == EditPosition) {
1643 EditPositionEvent();
1644 } else if (initialMode == Training) {
1645 if (*appData.loadGameFile == NULLCHAR) {
1646 DisplayFatalError(_("Training mode requires a game file"), 0, 2);
1655 * Establish will establish a contact to a remote host.port.
1656 * Sets icsPR to a ProcRef for a process (or pseudo-process)
1657 * used to talk to the host.
1658 * Returns 0 if okay, error code if not.
1665 if (*appData.icsCommPort != NULLCHAR) {
1666 /* Talk to the host through a serial comm port */
1667 return OpenCommPort(appData.icsCommPort, &icsPR);
1669 } else if (*appData.gateway != NULLCHAR) {
1670 if (*appData.remoteShell == NULLCHAR) {
1671 /* Use the rcmd protocol to run telnet program on a gateway host */
1672 snprintf(buf, sizeof(buf), "%s %s %s",
1673 appData.telnetProgram, appData.icsHost, appData.icsPort);
1674 return OpenRcmd(appData.gateway, appData.remoteUser, buf, &icsPR);
1677 /* Use the rsh program to run telnet program on a gateway host */
1678 if (*appData.remoteUser == NULLCHAR) {
1679 snprintf(buf, sizeof(buf), "%s %s %s %s %s", appData.remoteShell,
1680 appData.gateway, appData.telnetProgram,
1681 appData.icsHost, appData.icsPort);
1683 snprintf(buf, sizeof(buf), "%s %s -l %s %s %s %s",
1684 appData.remoteShell, appData.gateway,
1685 appData.remoteUser, appData.telnetProgram,
1686 appData.icsHost, appData.icsPort);
1688 return StartChildProcess(buf, "", &icsPR);
1691 } else if (appData.useTelnet) {
1692 return OpenTelnet(appData.icsHost, appData.icsPort, &icsPR);
1695 /* TCP socket interface differs somewhat between
1696 Unix and NT; handle details in the front end.
1698 return OpenTCP(appData.icsHost, appData.icsPort, &icsPR);
1702 void EscapeExpand(char *p, char *q)
1703 { // [HGM] initstring: routine to shape up string arguments
1704 while(*p++ = *q++) if(p[-1] == '\\')
1706 case 'n': p[-1] = '\n'; break;
1707 case 'r': p[-1] = '\r'; break;
1708 case 't': p[-1] = '\t'; break;
1709 case '\\': p[-1] = '\\'; break;
1710 case 0: *p = 0; return;
1711 default: p[-1] = q[-1]; break;
1716 show_bytes(fp, buf, count)
1722 if (*buf < 040 || *(unsigned char *) buf > 0177) {
1723 fprintf(fp, "\\%03o", *buf & 0xff);
1732 /* Returns an errno value */
1734 OutputMaybeTelnet(pr, message, count, outError)
1740 char buf[8192], *p, *q, *buflim;
1741 int left, newcount, outcount;
1743 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet ||
1744 *appData.gateway != NULLCHAR) {
1745 if (appData.debugMode) {
1746 fprintf(debugFP, ">ICS: ");
1747 show_bytes(debugFP, message, count);
1748 fprintf(debugFP, "\n");
1750 return OutputToProcess(pr, message, count, outError);
1753 buflim = &buf[sizeof(buf)-1]; /* allow 1 byte for expanding last char */
1760 if (appData.debugMode) {
1761 fprintf(debugFP, ">ICS: ");
1762 show_bytes(debugFP, buf, newcount);
1763 fprintf(debugFP, "\n");
1765 outcount = OutputToProcess(pr, buf, newcount, outError);
1766 if (outcount < newcount) return -1; /* to be sure */
1773 } else if (((unsigned char) *p) == TN_IAC) {
1774 *q++ = (char) TN_IAC;
1781 if (appData.debugMode) {
1782 fprintf(debugFP, ">ICS: ");
1783 show_bytes(debugFP, buf, newcount);
1784 fprintf(debugFP, "\n");
1786 outcount = OutputToProcess(pr, buf, newcount, outError);
1787 if (outcount < newcount) return -1; /* to be sure */
1792 read_from_player(isr, closure, message, count, error)
1799 int outError, outCount;
1800 static int gotEof = 0;
1802 /* Pass data read from player on to ICS */
1805 outCount = OutputMaybeTelnet(icsPR, message, count, &outError);
1806 if (outCount < count) {
1807 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1809 } else if (count < 0) {
1810 RemoveInputSource(isr);
1811 DisplayFatalError(_("Error reading from keyboard"), error, 1);
1812 } else if (gotEof++ > 0) {
1813 RemoveInputSource(isr);
1814 DisplayFatalError(_("Got end of file from keyboard"), 0, 0);
1820 { // [HGM] alive: periodically send dummy (date) command to ICS to prevent time-out
1821 if(!connectionAlive) DisplayFatalError("No response from ICS", 0, 1);
1822 connectionAlive = FALSE; // only sticks if no response to 'date' command.
1823 SendToICS("date\n");
1824 if(appData.keepAlive) ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1827 /* added routine for printf style output to ics */
1828 void ics_printf(char *format, ...)
1830 char buffer[MSG_SIZ];
1833 va_start(args, format);
1834 vsnprintf(buffer, sizeof(buffer), format, args);
1835 buffer[sizeof(buffer)-1] = '\0';
1844 int count, outCount, outError;
1846 if (icsPR == NULL) return;
1849 outCount = OutputMaybeTelnet(icsPR, s, count, &outError);
1850 if (outCount < count) {
1851 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1855 /* This is used for sending logon scripts to the ICS. Sending
1856 without a delay causes problems when using timestamp on ICC
1857 (at least on my machine). */
1859 SendToICSDelayed(s,msdelay)
1863 int count, outCount, outError;
1865 if (icsPR == NULL) return;
1868 if (appData.debugMode) {
1869 fprintf(debugFP, ">ICS: ");
1870 show_bytes(debugFP, s, count);
1871 fprintf(debugFP, "\n");
1873 outCount = OutputToProcessDelayed(icsPR, s, count, &outError,
1875 if (outCount < count) {
1876 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1881 /* Remove all highlighting escape sequences in s
1882 Also deletes any suffix starting with '('
1885 StripHighlightAndTitle(s)
1888 static char retbuf[MSG_SIZ];
1891 while (*s != NULLCHAR) {
1892 while (*s == '\033') {
1893 while (*s != NULLCHAR && !isalpha(*s)) s++;
1894 if (*s != NULLCHAR) s++;
1896 while (*s != NULLCHAR && *s != '\033') {
1897 if (*s == '(' || *s == '[') {
1908 /* Remove all highlighting escape sequences in s */
1913 static char retbuf[MSG_SIZ];
1916 while (*s != NULLCHAR) {
1917 while (*s == '\033') {
1918 while (*s != NULLCHAR && !isalpha(*s)) s++;
1919 if (*s != NULLCHAR) s++;
1921 while (*s != NULLCHAR && *s != '\033') {
1929 char *variantNames[] = VARIANT_NAMES;
1934 return variantNames[v];
1938 /* Identify a variant from the strings the chess servers use or the
1939 PGN Variant tag names we use. */
1946 VariantClass v = VariantNormal;
1947 int i, found = FALSE;
1953 /* [HGM] skip over optional board-size prefixes */
1954 if( sscanf(e, "%dx%d_", &i, &i) == 2 ||
1955 sscanf(e, "%dx%d+%d_", &i, &i, &i) == 3 ) {
1956 while( *e++ != '_');
1959 if(StrCaseStr(e, "misc/")) { // [HGM] on FICS, misc/shogi is not shogi
1963 for (i=0; i<sizeof(variantNames)/sizeof(char*); i++) {
1964 if (StrCaseStr(e, variantNames[i])) {
1965 v = (VariantClass) i;
1972 if ((StrCaseStr(e, "fischer") && StrCaseStr(e, "random"))
1973 || StrCaseStr(e, "wild/fr")
1974 || StrCaseStr(e, "frc") || StrCaseStr(e, "960")) {
1975 v = VariantFischeRandom;
1976 } else if ((i = 4, p = StrCaseStr(e, "wild")) ||
1977 (i = 1, p = StrCaseStr(e, "w"))) {
1979 while (*p && (isspace(*p) || *p == '(' || *p == '/')) p++;
1986 case 0: /* FICS only, actually */
1988 /* Castling legal even if K starts on d-file */
1989 v = VariantWildCastle;
1994 /* Castling illegal even if K & R happen to start in
1995 normal positions. */
1996 v = VariantNoCastle;
2009 /* Castling legal iff K & R start in normal positions */
2015 /* Special wilds for position setup; unclear what to do here */
2016 v = VariantLoadable;
2019 /* Bizarre ICC game */
2020 v = VariantTwoKings;
2023 v = VariantKriegspiel;
2029 v = VariantFischeRandom;
2032 v = VariantCrazyhouse;
2035 v = VariantBughouse;
2041 /* Not quite the same as FICS suicide! */
2042 v = VariantGiveaway;
2048 v = VariantShatranj;
2051 /* Temporary names for future ICC types. The name *will* change in
2052 the next xboard/WinBoard release after ICC defines it. */
2090 v = VariantCapablanca;
2093 v = VariantKnightmate;
2099 v = VariantCylinder;
2105 v = VariantCapaRandom;
2108 v = VariantBerolina;
2120 /* Found "wild" or "w" in the string but no number;
2121 must assume it's normal chess. */
2125 len = snprintf(buf, MSG_SIZ, _("Unknown wild type %d"), wnum);
2126 if( (len > MSG_SIZ) && appData.debugMode )
2127 fprintf(debugFP, "StringToVariant: buffer truncated.\n");
2129 DisplayError(buf, 0);
2135 if (appData.debugMode) {
2136 fprintf(debugFP, _("recognized '%s' (%d) as variant %s\n"),
2137 e, wnum, VariantName(v));
2142 static int leftover_start = 0, leftover_len = 0;
2143 char star_match[STAR_MATCH_N][MSG_SIZ];
2145 /* Test whether pattern is present at &buf[*index]; if so, return TRUE,
2146 advance *index beyond it, and set leftover_start to the new value of
2147 *index; else return FALSE. If pattern contains the character '*', it
2148 matches any sequence of characters not containing '\r', '\n', or the
2149 character following the '*' (if any), and the matched sequence(s) are
2150 copied into star_match.
2153 looking_at(buf, index, pattern)
2158 char *bufp = &buf[*index], *patternp = pattern;
2160 char *matchp = star_match[0];
2163 if (*patternp == NULLCHAR) {
2164 *index = leftover_start = bufp - buf;
2168 if (*bufp == NULLCHAR) return FALSE;
2169 if (*patternp == '*') {
2170 if (*bufp == *(patternp + 1)) {
2172 matchp = star_match[++star_count];
2176 } else if (*bufp == '\n' || *bufp == '\r') {
2178 if (*patternp == NULLCHAR)
2183 *matchp++ = *bufp++;
2187 if (*patternp != *bufp) return FALSE;
2194 SendToPlayer(data, length)
2198 int error, outCount;
2199 outCount = OutputToProcess(NoProc, data, length, &error);
2200 if (outCount < length) {
2201 DisplayFatalError(_("Error writing to display"), error, 1);
2206 PackHolding(packed, holding)
2218 switch (runlength) {
2229 sprintf(q, "%d", runlength);
2241 /* Telnet protocol requests from the front end */
2243 TelnetRequest(ddww, option)
2244 unsigned char ddww, option;
2246 unsigned char msg[3];
2247 int outCount, outError;
2249 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet) return;
2251 if (appData.debugMode) {
2252 char buf1[8], buf2[8], *ddwwStr, *optionStr;
2268 snprintf(buf1,sizeof(buf1)/sizeof(buf1[0]), "%d", ddww);
2277 snprintf(buf2,sizeof(buf2)/sizeof(buf2[0]), "%d", option);
2280 fprintf(debugFP, ">%s %s ", ddwwStr, optionStr);
2285 outCount = OutputToProcess(icsPR, (char *)msg, 3, &outError);
2287 DisplayFatalError(_("Error writing to ICS"), outError, 1);
2294 if (!appData.icsActive) return;
2295 TelnetRequest(TN_DO, TN_ECHO);
2301 if (!appData.icsActive) return;
2302 TelnetRequest(TN_DONT, TN_ECHO);
2306 CopyHoldings(Board board, char *holdings, ChessSquare lowestPiece)
2308 /* put the holdings sent to us by the server on the board holdings area */
2309 int i, j, holdingsColumn, holdingsStartRow, direction, countsColumn;
2313 if(gameInfo.holdingsWidth < 2) return;
2314 if(gameInfo.variant != VariantBughouse && board[HOLDINGS_SET])
2315 return; // prevent overwriting by pre-board holdings
2317 if( (int)lowestPiece >= BlackPawn ) {
2320 holdingsStartRow = BOARD_HEIGHT-1;
2323 holdingsColumn = BOARD_WIDTH-1;
2324 countsColumn = BOARD_WIDTH-2;
2325 holdingsStartRow = 0;
2329 for(i=0; i<BOARD_HEIGHT; i++) { /* clear holdings */
2330 board[i][holdingsColumn] = EmptySquare;
2331 board[i][countsColumn] = (ChessSquare) 0;
2333 while( (p=*holdings++) != NULLCHAR ) {
2334 piece = CharToPiece( ToUpper(p) );
2335 if(piece == EmptySquare) continue;
2336 /*j = (int) piece - (int) WhitePawn;*/
2337 j = PieceToNumber(piece);
2338 if(j >= gameInfo.holdingsSize) continue; /* ignore pieces that do not fit */
2339 if(j < 0) continue; /* should not happen */
2340 piece = (ChessSquare) ( (int)piece + (int)lowestPiece );
2341 board[holdingsStartRow+j*direction][holdingsColumn] = piece;
2342 board[holdingsStartRow+j*direction][countsColumn]++;
2348 VariantSwitch(Board board, VariantClass newVariant)
2350 int newHoldingsWidth, newWidth = 8, newHeight = 8, i, j;
2351 static Board oldBoard;
2353 startedFromPositionFile = FALSE;
2354 if(gameInfo.variant == newVariant) return;
2356 /* [HGM] This routine is called each time an assignment is made to
2357 * gameInfo.variant during a game, to make sure the board sizes
2358 * are set to match the new variant. If that means adding or deleting
2359 * holdings, we shift the playing board accordingly
2360 * This kludge is needed because in ICS observe mode, we get boards
2361 * of an ongoing game without knowing the variant, and learn about the
2362 * latter only later. This can be because of the move list we requested,
2363 * in which case the game history is refilled from the beginning anyway,
2364 * but also when receiving holdings of a crazyhouse game. In the latter
2365 * case we want to add those holdings to the already received position.
2369 if (appData.debugMode) {
2370 fprintf(debugFP, "Switch board from %s to %s\n",
2371 VariantName(gameInfo.variant), VariantName(newVariant));
2372 setbuf(debugFP, NULL);
2374 shuffleOpenings = 0; /* [HGM] shuffle */
2375 gameInfo.holdingsSize = 5; /* [HGM] prepare holdings */
2379 newWidth = 9; newHeight = 9;
2380 gameInfo.holdingsSize = 7;
2381 case VariantBughouse:
2382 case VariantCrazyhouse:
2383 newHoldingsWidth = 2; break;
2387 newHoldingsWidth = 2;
2388 gameInfo.holdingsSize = 8;
2391 case VariantCapablanca:
2392 case VariantCapaRandom:
2395 newHoldingsWidth = gameInfo.holdingsSize = 0;
2398 if(newWidth != gameInfo.boardWidth ||
2399 newHeight != gameInfo.boardHeight ||
2400 newHoldingsWidth != gameInfo.holdingsWidth ) {
2402 /* shift position to new playing area, if needed */
2403 if(newHoldingsWidth > gameInfo.holdingsWidth) {
2404 for(i=0; i<BOARD_HEIGHT; i++)
2405 for(j=BOARD_RGHT-1; j>=BOARD_LEFT; j--)
2406 board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2408 for(i=0; i<newHeight; i++) {
2409 board[i][0] = board[i][newWidth+2*newHoldingsWidth-1] = EmptySquare;
2410 board[i][1] = board[i][newWidth+2*newHoldingsWidth-2] = (ChessSquare) 0;
2412 } else if(newHoldingsWidth < gameInfo.holdingsWidth) {
2413 for(i=0; i<BOARD_HEIGHT; i++)
2414 for(j=BOARD_LEFT; j<BOARD_RGHT; j++)
2415 board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2418 gameInfo.boardWidth = newWidth;
2419 gameInfo.boardHeight = newHeight;
2420 gameInfo.holdingsWidth = newHoldingsWidth;
2421 gameInfo.variant = newVariant;
2422 InitDrawingSizes(-2, 0);
2423 } else gameInfo.variant = newVariant;
2424 CopyBoard(oldBoard, board); // remember correctly formatted board
2425 InitPosition(FALSE); /* this sets up board[0], but also other stuff */
2426 DrawPosition(TRUE, currentMove ? boards[currentMove] : oldBoard);
2429 static int loggedOn = FALSE;
2431 /*-- Game start info cache: --*/
2433 char gs_kind[MSG_SIZ];
2434 static char player1Name[128] = "";
2435 static char player2Name[128] = "";
2436 static char cont_seq[] = "\n\\ ";
2437 static int player1Rating = -1;
2438 static int player2Rating = -1;
2439 /*----------------------------*/
2441 ColorClass curColor = ColorNormal;
2442 int suppressKibitz = 0;
2445 Boolean soughtPending = FALSE;
2446 Boolean seekGraphUp;
2447 #define MAX_SEEK_ADS 200
2449 char *seekAdList[MAX_SEEK_ADS];
2450 int ratingList[MAX_SEEK_ADS], xList[MAX_SEEK_ADS], yList[MAX_SEEK_ADS], seekNrList[MAX_SEEK_ADS], zList[MAX_SEEK_ADS];
2451 float tcList[MAX_SEEK_ADS];
2452 char colorList[MAX_SEEK_ADS];
2453 int nrOfSeekAds = 0;
2454 int minRating = 1010, maxRating = 2800;
2455 int hMargin = 10, vMargin = 20, h, w;
2456 extern int squareSize, lineGap;
2461 int x, y, color = 0, r = ratingList[i]; float tc = tcList[i];
2462 xList[i] = yList[i] = -100; // outside graph, so cannot be clicked
2463 if(r < minRating+100 && r >=0 ) r = minRating+100;
2464 if(r > maxRating) r = maxRating;
2465 if(tc < 1.) tc = 1.;
2466 if(tc > 95.) tc = 95.;
2467 x = (w-hMargin-squareSize/8-7)* log(tc)/log(95.) + hMargin;
2468 y = ((double)r - minRating)/(maxRating - minRating)
2469 * (h-vMargin-squareSize/8-1) + vMargin;
2470 if(ratingList[i] < 0) y = vMargin + squareSize/4;
2471 if(strstr(seekAdList[i], " u ")) color = 1;
2472 if(!strstr(seekAdList[i], "lightning") && // for now all wilds same color
2473 !strstr(seekAdList[i], "bullet") &&
2474 !strstr(seekAdList[i], "blitz") &&
2475 !strstr(seekAdList[i], "standard") ) color = 2;
2476 if(strstr(seekAdList[i], "(C) ")) color |= SQUARE; // plot computer seeks as squares
2477 DrawSeekDot(xList[i]=x+3*(color&~SQUARE), yList[i]=h-1-y, colorList[i]=color);
2481 AddAd(char *handle, char *rating, int base, int inc, char rated, char *type, int nr, Boolean plot)
2483 char buf[MSG_SIZ], *ext = "";
2484 VariantClass v = StringToVariant(type);
2485 if(strstr(type, "wild")) {
2486 ext = type + 4; // append wild number
2487 if(v == VariantFischeRandom) type = "chess960"; else
2488 if(v == VariantLoadable) type = "setup"; else
2489 type = VariantName(v);
2491 snprintf(buf, MSG_SIZ, "%s (%s) %d %d %c %s%s", handle, rating, base, inc, rated, type, ext);
2492 if(nrOfSeekAds < MAX_SEEK_ADS-1) {
2493 if(seekAdList[nrOfSeekAds]) free(seekAdList[nrOfSeekAds]);
2494 ratingList[nrOfSeekAds] = -1; // for if seeker has no rating
2495 sscanf(rating, "%d", &ratingList[nrOfSeekAds]);
2496 tcList[nrOfSeekAds] = base + (2./3.)*inc;
2497 seekNrList[nrOfSeekAds] = nr;
2498 zList[nrOfSeekAds] = 0;
2499 seekAdList[nrOfSeekAds++] = StrSave(buf);
2500 if(plot) PlotSeekAd(nrOfSeekAds-1);
2507 int x = xList[i], y = yList[i], d=squareSize/4, k;
2508 DrawSeekBackground(x-squareSize/8, y-squareSize/8, x+squareSize/8+1, y+squareSize/8+1);
2509 if(x < hMargin+d) DrawSeekAxis(hMargin, y-squareSize/8, hMargin, y+squareSize/8+1);
2510 // now replot every dot that overlapped
2511 for(k=0; k<nrOfSeekAds; k++) if(k != i) {
2512 int xx = xList[k], yy = yList[k];
2513 if(xx <= x+d && xx > x-d && yy <= y+d && yy > y-d)
2514 DrawSeekDot(xx, yy, colorList[k]);
2519 RemoveSeekAd(int nr)
2522 for(i=0; i<nrOfSeekAds; i++) if(seekNrList[i] == nr) {
2524 if(seekAdList[i]) free(seekAdList[i]);
2525 seekAdList[i] = seekAdList[--nrOfSeekAds];
2526 seekNrList[i] = seekNrList[nrOfSeekAds];
2527 ratingList[i] = ratingList[nrOfSeekAds];
2528 colorList[i] = colorList[nrOfSeekAds];
2529 tcList[i] = tcList[nrOfSeekAds];
2530 xList[i] = xList[nrOfSeekAds];
2531 yList[i] = yList[nrOfSeekAds];
2532 zList[i] = zList[nrOfSeekAds];
2533 seekAdList[nrOfSeekAds] = NULL;
2539 MatchSoughtLine(char *line)
2541 char handle[MSG_SIZ], rating[MSG_SIZ], type[MSG_SIZ];
2542 int nr, base, inc, u=0; char dummy;
2544 if(sscanf(line, "%d %s %s %d %d rated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2545 sscanf(line, "%d %s %s %s %d %d rated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7 ||
2547 (sscanf(line, "%d %s %s %d %d unrated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2548 sscanf(line, "%d %s %s %s %d %d unrated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7) ) {
2549 // match: compact and save the line
2550 AddAd(handle, rating, base, inc, u ? 'u' : 'r', type, nr, FALSE);
2560 if(!seekGraphUp) return FALSE;
2561 h = BOARD_HEIGHT * (squareSize + lineGap) + lineGap;
2562 w = BOARD_WIDTH * (squareSize + lineGap) + lineGap;
2564 DrawSeekBackground(0, 0, w, h);
2565 DrawSeekAxis(hMargin, h-1-vMargin, w-5, h-1-vMargin);
2566 DrawSeekAxis(hMargin, h-1-vMargin, hMargin, 5);
2567 for(i=0; i<4000; i+= 100) if(i>=minRating && i<maxRating) {
2568 int yy =((double)i - minRating)/(maxRating - minRating)*(h-vMargin-squareSize/8-1) + vMargin;
2570 DrawSeekAxis(hMargin+5*(i%500==0), yy, hMargin-5, yy); // rating ticks
2573 snprintf(buf, MSG_SIZ, "%d", i);
2574 DrawSeekText(buf, hMargin+squareSize/8+7, yy);
2577 DrawSeekText("unrated", hMargin+squareSize/8+7, h-1-vMargin-squareSize/4);
2578 for(i=1; i<100; i+=(i<10?1:5)) {
2579 int xx = (w-hMargin-squareSize/8-7)* log((double)i)/log(95.) + hMargin;
2580 DrawSeekAxis(xx, h-1-vMargin, xx, h-6-vMargin-3*(i%10==0)); // TC ticks
2581 if(i<=5 || (i>40 ? i%20 : i%10) == 0) {
2583 snprintf(buf, MSG_SIZ, "%d", i);
2584 DrawSeekText(buf, xx-2-3*(i>9), h-1-vMargin/2);
2587 for(i=0; i<nrOfSeekAds; i++) PlotSeekAd(i);
2591 int SeekGraphClick(ClickType click, int x, int y, int moving)
2593 static int lastDown = 0, displayed = 0, lastSecond;
2594 if(!seekGraphUp) { // initiate cration of seek graph by requesting seek-ad list
2595 if(click == Release || moving) return FALSE;
2597 soughtPending = TRUE;
2598 SendToICS(ics_prefix);
2599 SendToICS("sought\n"); // should this be "sought all"?
2600 } else { // issue challenge based on clicked ad
2601 int dist = 10000; int i, closest = 0, second = 0;
2602 for(i=0; i<nrOfSeekAds; i++) {
2603 int d = (x-xList[i])*(x-xList[i]) + (y-yList[i])*(y-yList[i]) + zList[i];
2604 if(d < dist) { dist = d; closest = i; }
2605 second += (d - zList[i] < 120); // count in-range ads
2606 if(click == Press && moving != 1 && zList[i]>0) zList[i] *= 0.8; // age priority
2610 second = (second > 1);
2611 if(displayed != closest || second != lastSecond) {
2612 DisplayMessage(second ? "!" : "", seekAdList[closest]);
2613 lastSecond = second; displayed = closest;
2615 if(click == Press) {
2616 if(moving == 2) zList[closest] = 100; // right-click; push to back on press
2619 } // on press 'hit', only show info
2620 if(moving == 2) return TRUE; // ignore right up-clicks on dot
2621 snprintf(buf, MSG_SIZ, "play %d\n", seekNrList[closest]);
2622 SendToICS(ics_prefix);
2624 return TRUE; // let incoming board of started game pop down the graph
2625 } else if(click == Release) { // release 'miss' is ignored
2626 zList[lastDown] = 100; // make future selection of the rejected ad more difficult
2627 if(moving == 2) { // right up-click
2628 nrOfSeekAds = 0; // refresh graph
2629 soughtPending = TRUE;
2630 SendToICS(ics_prefix);
2631 SendToICS("sought\n"); // should this be "sought all"?
2634 } else if(moving) { if(displayed >= 0) DisplayMessage("", ""); displayed = -1; return TRUE; }
2635 // press miss or release hit 'pop down' seek graph
2636 seekGraphUp = FALSE;
2637 DrawPosition(TRUE, NULL);
2643 read_from_ics(isr, closure, data, count, error)
2650 #define BUF_SIZE (16*1024) /* overflowed at 8K with "inchannel 1" on FICS? */
2651 #define STARTED_NONE 0
2652 #define STARTED_MOVES 1
2653 #define STARTED_BOARD 2
2654 #define STARTED_OBSERVE 3
2655 #define STARTED_HOLDINGS 4
2656 #define STARTED_CHATTER 5
2657 #define STARTED_COMMENT 6
2658 #define STARTED_MOVES_NOHIDE 7
2660 static int started = STARTED_NONE;
2661 static char parse[20000];
2662 static int parse_pos = 0;
2663 static char buf[BUF_SIZE + 1];
2664 static int firstTime = TRUE, intfSet = FALSE;
2665 static ColorClass prevColor = ColorNormal;
2666 static int savingComment = FALSE;
2667 static int cmatch = 0; // continuation sequence match
2674 int backup; /* [DM] For zippy color lines */
2676 char talker[MSG_SIZ]; // [HGM] chat
2679 connectionAlive = TRUE; // [HGM] alive: I think, therefore I am...
2681 if (appData.debugMode) {
2683 fprintf(debugFP, "<ICS: ");
2684 show_bytes(debugFP, data, count);
2685 fprintf(debugFP, "\n");
2689 if (appData.debugMode) { int f = forwardMostMove;
2690 fprintf(debugFP, "ics input %d, castling = %d %d %d %d %d %d\n", f,
2691 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
2692 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
2695 /* If last read ended with a partial line that we couldn't parse,
2696 prepend it to the new read and try again. */
2697 if (leftover_len > 0) {
2698 for (i=0; i<leftover_len; i++)
2699 buf[i] = buf[leftover_start + i];
2702 /* copy new characters into the buffer */
2703 bp = buf + leftover_len;
2704 buf_len=leftover_len;
2705 for (i=0; i<count; i++)
2708 if (data[i] == '\r')
2711 // join lines split by ICS?
2712 if (!appData.noJoin)
2715 Joining just consists of finding matches against the
2716 continuation sequence, and discarding that sequence
2717 if found instead of copying it. So, until a match
2718 fails, there's nothing to do since it might be the
2719 complete sequence, and thus, something we don't want
2722 if (data[i] == cont_seq[cmatch])
2725 if (cmatch == strlen(cont_seq))
2727 cmatch = 0; // complete match. just reset the counter
2730 it's possible for the ICS to not include the space
2731 at the end of the last word, making our [correct]
2732 join operation fuse two separate words. the server
2733 does this when the space occurs at the width setting.
2735 if (!buf_len || buf[buf_len-1] != ' ')
2746 match failed, so we have to copy what matched before
2747 falling through and copying this character. In reality,
2748 this will only ever be just the newline character, but
2749 it doesn't hurt to be precise.
2751 strncpy(bp, cont_seq, cmatch);
2763 buf[buf_len] = NULLCHAR;
2764 // next_out = leftover_len; // [HGM] should we set this to 0, and not print it in advance?
2769 while (i < buf_len) {
2770 /* Deal with part of the TELNET option negotiation
2771 protocol. We refuse to do anything beyond the
2772 defaults, except that we allow the WILL ECHO option,
2773 which ICS uses to turn off password echoing when we are
2774 directly connected to it. We reject this option
2775 if localLineEditing mode is on (always on in xboard)
2776 and we are talking to port 23, which might be a real
2777 telnet server that will try to keep WILL ECHO on permanently.
2779 if (buf_len - i >= 3 && (unsigned char) buf[i] == TN_IAC) {
2780 static int remoteEchoOption = FALSE; /* telnet ECHO option */
2781 unsigned char option;
2783 switch ((unsigned char) buf[++i]) {
2785 if (appData.debugMode)
2786 fprintf(debugFP, "\n<WILL ");
2787 switch (option = (unsigned char) buf[++i]) {
2789 if (appData.debugMode)
2790 fprintf(debugFP, "ECHO ");
2791 /* Reply only if this is a change, according
2792 to the protocol rules. */
2793 if (remoteEchoOption) break;
2794 if (appData.localLineEditing &&
2795 atoi(appData.icsPort) == TN_PORT) {
2796 TelnetRequest(TN_DONT, TN_ECHO);
2799 TelnetRequest(TN_DO, TN_ECHO);
2800 remoteEchoOption = TRUE;
2804 if (appData.debugMode)
2805 fprintf(debugFP, "%d ", option);
2806 /* Whatever this is, we don't want it. */
2807 TelnetRequest(TN_DONT, option);
2812 if (appData.debugMode)
2813 fprintf(debugFP, "\n<WONT ");
2814 switch (option = (unsigned char) buf[++i]) {
2816 if (appData.debugMode)
2817 fprintf(debugFP, "ECHO ");
2818 /* Reply only if this is a change, according
2819 to the protocol rules. */
2820 if (!remoteEchoOption) break;
2822 TelnetRequest(TN_DONT, TN_ECHO);
2823 remoteEchoOption = FALSE;
2826 if (appData.debugMode)
2827 fprintf(debugFP, "%d ", (unsigned char) option);
2828 /* Whatever this is, it must already be turned
2829 off, because we never agree to turn on
2830 anything non-default, so according to the
2831 protocol rules, we don't reply. */
2836 if (appData.debugMode)
2837 fprintf(debugFP, "\n<DO ");
2838 switch (option = (unsigned char) buf[++i]) {
2840 /* Whatever this is, we refuse to do it. */
2841 if (appData.debugMode)
2842 fprintf(debugFP, "%d ", option);
2843 TelnetRequest(TN_WONT, option);
2848 if (appData.debugMode)
2849 fprintf(debugFP, "\n<DONT ");
2850 switch (option = (unsigned char) buf[++i]) {
2852 if (appData.debugMode)
2853 fprintf(debugFP, "%d ", option);
2854 /* Whatever this is, we are already not doing
2855 it, because we never agree to do anything
2856 non-default, so according to the protocol
2857 rules, we don't reply. */
2862 if (appData.debugMode)
2863 fprintf(debugFP, "\n<IAC ");
2864 /* Doubled IAC; pass it through */
2868 if (appData.debugMode)
2869 fprintf(debugFP, "\n<%d ", (unsigned char) buf[i]);
2870 /* Drop all other telnet commands on the floor */
2873 if (oldi > next_out)
2874 SendToPlayer(&buf[next_out], oldi - next_out);
2880 /* OK, this at least will *usually* work */
2881 if (!loggedOn && looking_at(buf, &i, "ics%")) {
2885 if (loggedOn && !intfSet) {
2886 if (ics_type == ICS_ICC) {
2887 snprintf(str, MSG_SIZ,
2888 "/set-quietly interface %s\n/set-quietly style 12\n",
2890 if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2891 strcat(str, "/set-2 51 1\n/set seek 1\n");
2892 } else if (ics_type == ICS_CHESSNET) {
2893 snprintf(str, MSG_SIZ, "/style 12\n");
2895 safeStrCpy(str, "alias $ @\n$set interface ", sizeof(str)/sizeof(str[0]));
2896 strcat(str, programVersion);
2897 strcat(str, "\n$iset startpos 1\n$iset ms 1\n");
2898 if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2899 strcat(str, "$iset seekremove 1\n$set seek 1\n");
2901 strcat(str, "$iset nohighlight 1\n");
2903 strcat(str, "$iset lock 1\n$style 12\n");
2906 NotifyFrontendLogin();
2910 if (started == STARTED_COMMENT) {
2911 /* Accumulate characters in comment */
2912 parse[parse_pos++] = buf[i];
2913 if (buf[i] == '\n') {
2914 parse[parse_pos] = NULLCHAR;
2915 if(chattingPartner>=0) {
2917 snprintf(mess, MSG_SIZ, "%s%s", talker, parse);
2918 OutputChatMessage(chattingPartner, mess);
2919 chattingPartner = -1;
2920 next_out = i+1; // [HGM] suppress printing in ICS window
2922 if(!suppressKibitz) // [HGM] kibitz
2923 AppendComment(forwardMostMove, StripHighlight(parse), TRUE);
2924 else { // [HGM kibitz: divert memorized engine kibitz to engine-output window
2925 int nrDigit = 0, nrAlph = 0, j;
2926 if(parse_pos > MSG_SIZ - 30) // defuse unreasonably long input
2927 { parse_pos = MSG_SIZ-30; parse[parse_pos - 1] = '\n'; }
2928 parse[parse_pos] = NULLCHAR;
2929 // try to be smart: if it does not look like search info, it should go to
2930 // ICS interaction window after all, not to engine-output window.
2931 for(j=0; j<parse_pos; j++) { // count letters and digits
2932 nrDigit += (parse[j] >= '0' && parse[j] <= '9');
2933 nrAlph += (parse[j] >= 'a' && parse[j] <= 'z');
2934 nrAlph += (parse[j] >= 'A' && parse[j] <= 'Z');
2936 if(nrAlph < 9*nrDigit) { // if more than 10% digit we assume search info
2937 int depth=0; float score;
2938 if(sscanf(parse, "!!! %f/%d", &score, &depth) == 2 && depth>0) {
2939 // [HGM] kibitz: save kibitzed opponent info for PGN and eval graph
2940 pvInfoList[forwardMostMove-1].depth = depth;
2941 pvInfoList[forwardMostMove-1].score = 100*score;
2943 OutputKibitz(suppressKibitz, parse);
2946 snprintf(tmp, MSG_SIZ, _("your opponent kibitzes: %s"), parse);
2947 SendToPlayer(tmp, strlen(tmp));
2949 next_out = i+1; // [HGM] suppress printing in ICS window
2951 started = STARTED_NONE;
2953 /* Don't match patterns against characters in comment */
2958 if (started == STARTED_CHATTER) {
2959 if (buf[i] != '\n') {
2960 /* Don't match patterns against characters in chatter */
2964 started = STARTED_NONE;
2965 if(suppressKibitz) next_out = i+1;
2968 /* Kludge to deal with rcmd protocol */
2969 if (firstTime && looking_at(buf, &i, "\001*")) {
2970 DisplayFatalError(&buf[1], 0, 1);
2976 if (!loggedOn && looking_at(buf, &i, "chessclub.com")) {
2979 if (appData.debugMode)
2980 fprintf(debugFP, "ics_type %d\n", ics_type);
2983 if (!loggedOn && looking_at(buf, &i, "freechess.org")) {
2984 ics_type = ICS_FICS;
2986 if (appData.debugMode)
2987 fprintf(debugFP, "ics_type %d\n", ics_type);
2990 if (!loggedOn && looking_at(buf, &i, "chess.net")) {
2991 ics_type = ICS_CHESSNET;
2993 if (appData.debugMode)
2994 fprintf(debugFP, "ics_type %d\n", ics_type);
2999 (looking_at(buf, &i, "\"*\" is *a registered name") ||
3000 looking_at(buf, &i, "Logging you in as \"*\"") ||
3001 looking_at(buf, &i, "will be \"*\""))) {
3002 safeStrCpy(ics_handle, star_match[0], sizeof(ics_handle)/sizeof(ics_handle[0]));
3006 if (loggedOn && !have_set_title && ics_handle[0] != NULLCHAR) {
3008 snprintf(buf, sizeof(buf), "%s@%s", ics_handle, appData.icsHost);
3009 DisplayIcsInteractionTitle(buf);
3010 have_set_title = TRUE;
3013 /* skip finger notes */
3014 if (started == STARTED_NONE &&
3015 ((buf[i] == ' ' && isdigit(buf[i+1])) ||
3016 (buf[i] == '1' && buf[i+1] == '0')) &&
3017 buf[i+2] == ':' && buf[i+3] == ' ') {
3018 started = STARTED_CHATTER;
3024 // [HGM] seekgraph: recognize sought lines and end-of-sought message
3025 if(appData.seekGraph) {
3026 if(soughtPending && MatchSoughtLine(buf+i)) {
3027 i = strstr(buf+i, "rated") - buf;
3028 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3029 next_out = leftover_start = i;
3030 started = STARTED_CHATTER;
3031 suppressKibitz = TRUE;
3034 if((gameMode == IcsIdle || gameMode == BeginningOfGame)
3035 && looking_at(buf, &i, "* ads displayed")) {
3036 soughtPending = FALSE;
3041 if(appData.autoRefresh) {
3042 if(looking_at(buf, &i, "* (*) seeking * * * * *\"play *\" to respond)\n")) {
3043 int s = (ics_type == ICS_ICC); // ICC format differs
3045 AddAd(star_match[0], star_match[1], atoi(star_match[2+s]), atoi(star_match[3+s]),
3046 star_match[4+s][0], star_match[5-3*s], atoi(star_match[7]), TRUE);
3047 looking_at(buf, &i, "*% "); // eat prompt
3048 if(oldi > 0 && buf[oldi-1] == '\n') oldi--; // suppress preceding LF, if any
3049 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3050 next_out = i; // suppress
3053 if(looking_at(buf, &i, "\nAds removed: *\n") || looking_at(buf, &i, "\031(51 * *\031)")) {
3054 char *p = star_match[0];
3056 if(seekGraphUp) RemoveSeekAd(atoi(p));
3057 while(*p && *p++ != ' '); // next
3059 looking_at(buf, &i, "*% "); // eat prompt
3060 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3067 /* skip formula vars */
3068 if (started == STARTED_NONE &&
3069 buf[i] == 'f' && isdigit(buf[i+1]) && buf[i+2] == ':') {
3070 started = STARTED_CHATTER;
3075 // [HGM] kibitz: try to recognize opponent engine-score kibitzes, to divert them to engine-output window
3076 if (appData.autoKibitz && started == STARTED_NONE &&
3077 !appData.icsEngineAnalyze && // [HGM] [DM] ICS analyze
3078 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack || gameMode == IcsObserving)) {
3079 if((looking_at(buf, &i, "* kibitzes: ") || looking_at(buf, &i, "* whispers: ")) &&
3080 (StrStr(star_match[0], gameInfo.white) == star_match[0] ||
3081 StrStr(star_match[0], gameInfo.black) == star_match[0] )) { // kibitz of self or opponent
3082 suppressKibitz = TRUE;
3083 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3085 if((StrStr(star_match[0], gameInfo.white) == star_match[0]
3086 && (gameMode == IcsPlayingWhite)) ||
3087 (StrStr(star_match[0], gameInfo.black) == star_match[0]
3088 && (gameMode == IcsPlayingBlack)) ) // opponent kibitz
3089 started = STARTED_CHATTER; // own kibitz we simply discard
3091 started = STARTED_COMMENT; // make sure it will be collected in parse[]
3092 parse_pos = 0; parse[0] = NULLCHAR;
3093 savingComment = TRUE;
3094 suppressKibitz = gameMode != IcsObserving ? 2 :
3095 (StrStr(star_match[0], gameInfo.white) == NULL) + 1;
3099 if((looking_at(buf, &i, "\nkibitzed to *\n") || looking_at(buf, &i, "kibitzed to *\n") ||
3100 looking_at(buf, &i, "\n(kibitzed to *\n") || looking_at(buf, &i, "(kibitzed to *\n"))
3101 && atoi(star_match[0])) {
3102 // suppress the acknowledgements of our own autoKibitz
3104 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3105 if(p = strchr(star_match[0], ' ')) p[1] = NULLCHAR; // clip off "players)" on FICS
3106 SendToPlayer(star_match[0], strlen(star_match[0]));
3107 if(looking_at(buf, &i, "*% ")) // eat prompt
3108 suppressKibitz = FALSE;
3112 } // [HGM] kibitz: end of patch
3114 // [HGM] chat: intercept tells by users for which we have an open chat window
3116 if(started == STARTED_NONE && (looking_at(buf, &i, "* tells you:") || looking_at(buf, &i, "* says:") ||
3117 looking_at(buf, &i, "* whispers:") ||
3118 looking_at(buf, &i, "* kibitzes:") ||
3119 looking_at(buf, &i, "* shouts:") ||
3120 looking_at(buf, &i, "* c-shouts:") ||
3121 looking_at(buf, &i, "--> * ") ||
3122 looking_at(buf, &i, "*(*):") && (sscanf(star_match[1], "%d", &channel),1) ||
3123 looking_at(buf, &i, "*(*)(*):") && (sscanf(star_match[2], "%d", &channel),1) ||
3124 looking_at(buf, &i, "*(*)(*)(*):") && (sscanf(star_match[3], "%d", &channel),1) ||
3125 looking_at(buf, &i, "*(*)(*)(*)(*):") && sscanf(star_match[4], "%d", &channel) == 1 )) {
3127 sscanf(star_match[0], "%[^(]", talker+1); // strip (C) or (U) off ICS handle
3128 chattingPartner = -1;
3130 if(channel >= 0) // channel broadcast; look if there is a chatbox for this channel
3131 for(p=0; p<MAX_CHAT; p++) {
3132 if(chatPartner[p][0] >= '0' && chatPartner[p][0] <= '9' && channel == atoi(chatPartner[p])) {
3133 talker[0] = '['; strcat(talker, "] ");
3134 Colorize(channel == 1 ? ColorChannel1 : ColorChannel, FALSE);
3135 chattingPartner = p; break;
3138 if(buf[i-3] == 'e') // kibitz; look if there is a KIBITZ chatbox
3139 for(p=0; p<MAX_CHAT; p++) {
3140 if(!strcmp("kibitzes", chatPartner[p])) {
3141 talker[0] = '['; strcat(talker, "] ");
3142 chattingPartner = p; break;
3145 if(buf[i-3] == 'r') // whisper; look if there is a WHISPER chatbox
3146 for(p=0; p<MAX_CHAT; p++) {
3147 if(!strcmp("whispers", chatPartner[p])) {
3148 talker[0] = '['; strcat(talker, "] ");
3149 chattingPartner = p; break;
3152 if(buf[i-3] == 't' || buf[oldi+2] == '>') {// shout, c-shout or it; look if there is a 'shouts' chatbox
3153 if(buf[i-8] == '-' && buf[i-3] == 't')
3154 for(p=0; p<MAX_CHAT; p++) { // c-shout; check if dedicatesd c-shout box exists
3155 if(!strcmp("c-shouts", chatPartner[p])) {
3156 talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE);
3157 chattingPartner = p; break;
3160 if(chattingPartner < 0)
3161 for(p=0; p<MAX_CHAT; p++) {
3162 if(!strcmp("shouts", chatPartner[p])) {
3163 if(buf[oldi+2] == '>') { talker[0] = '<'; strcat(talker, "> "); Colorize(ColorShout, FALSE); }
3164 else if(buf[i-8] == '-') { talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE); }
3165 else { talker[0] = '['; strcat(talker, "] "); Colorize(ColorShout, FALSE); }
3166 chattingPartner = p; break;
3170 if(chattingPartner<0) // if not, look if there is a chatbox for this indivdual
3171 for(p=0; p<MAX_CHAT; p++) if(!StrCaseCmp(talker+1, chatPartner[p])) {
3172 talker[0] = 0; Colorize(ColorTell, FALSE);
3173 chattingPartner = p; break;
3175 if(chattingPartner<0) i = oldi; else {
3176 Colorize(curColor, TRUE); // undo the bogus colorations we just made to trigger the souds
3177 if(oldi > 0 && buf[oldi-1] == '\n') oldi--;
3178 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3179 started = STARTED_COMMENT;
3180 parse_pos = 0; parse[0] = NULLCHAR;
3181 savingComment = 3 + chattingPartner; // counts as TRUE
3182 suppressKibitz = TRUE;
3185 } // [HGM] chat: end of patch
3188 if (appData.zippyTalk || appData.zippyPlay) {
3189 /* [DM] Backup address for color zippy lines */
3191 if (loggedOn == TRUE)
3192 if (ZippyControl(buf, &backup) || ZippyConverse(buf, &backup) ||
3193 (appData.zippyPlay && ZippyMatch(buf, &backup)));
3195 } // [DM] 'else { ' deleted
3197 /* Regular tells and says */
3198 (tkind = 1, looking_at(buf, &i, "* tells you: ")) ||
3199 looking_at(buf, &i, "* (your partner) tells you: ") ||
3200 looking_at(buf, &i, "* says: ") ||
3201 /* Don't color "message" or "messages" output */
3202 (tkind = 5, looking_at(buf, &i, "*. * (*:*): ")) ||
3203 looking_at(buf, &i, "*. * at *:*: ") ||
3204 looking_at(buf, &i, "--* (*:*): ") ||
3205 /* Message notifications (same color as tells) */
3206 looking_at(buf, &i, "* has left a message ") ||
3207 looking_at(buf, &i, "* just sent you a message:\n") ||
3208 /* Whispers and kibitzes */
3209 (tkind = 2, looking_at(buf, &i, "* whispers: ")) ||
3210 looking_at(buf, &i, "* kibitzes: ") ||
3212 (tkind = 3, looking_at(buf, &i, "*(*: "))) {
3214 if (tkind == 1 && strchr(star_match[0], ':')) {
3215 /* Avoid "tells you:" spoofs in channels */
3218 if (star_match[0][0] == NULLCHAR ||
3219 strchr(star_match[0], ' ') ||
3220 (tkind == 3 && strchr(star_match[1], ' '))) {
3221 /* Reject bogus matches */
3224 if (appData.colorize) {
3225 if (oldi > next_out) {
3226 SendToPlayer(&buf[next_out], oldi - next_out);
3231 Colorize(ColorTell, FALSE);
3232 curColor = ColorTell;
3235 Colorize(ColorKibitz, FALSE);
3236 curColor = ColorKibitz;
3239 p = strrchr(star_match[1], '(');
3246 Colorize(ColorChannel1, FALSE);
3247 curColor = ColorChannel1;
3249 Colorize(ColorChannel, FALSE);
3250 curColor = ColorChannel;
3254 curColor = ColorNormal;
3258 if (started == STARTED_NONE && appData.autoComment &&
3259 (gameMode == IcsObserving ||
3260 gameMode == IcsPlayingWhite ||
3261 gameMode == IcsPlayingBlack)) {
3262 parse_pos = i - oldi;
3263 memcpy(parse, &buf[oldi], parse_pos);
3264 parse[parse_pos] = NULLCHAR;
3265 started = STARTED_COMMENT;
3266 savingComment = TRUE;
3268 started = STARTED_CHATTER;
3269 savingComment = FALSE;
3276 if (looking_at(buf, &i, "* s-shouts: ") ||
3277 looking_at(buf, &i, "* c-shouts: ")) {
3278 if (appData.colorize) {
3279 if (oldi > next_out) {
3280 SendToPlayer(&buf[next_out], oldi - next_out);
3283 Colorize(ColorSShout, FALSE);
3284 curColor = ColorSShout;
3287 started = STARTED_CHATTER;
3291 if (looking_at(buf, &i, "--->")) {
3296 if (looking_at(buf, &i, "* shouts: ") ||
3297 looking_at(buf, &i, "--> ")) {
3298 if (appData.colorize) {
3299 if (oldi > next_out) {
3300 SendToPlayer(&buf[next_out], oldi - next_out);
3303 Colorize(ColorShout, FALSE);
3304 curColor = ColorShout;
3307 started = STARTED_CHATTER;
3311 if (looking_at( buf, &i, "Challenge:")) {
3312 if (appData.colorize) {
3313 if (oldi > next_out) {
3314 SendToPlayer(&buf[next_out], oldi - next_out);
3317 Colorize(ColorChallenge, FALSE);
3318 curColor = ColorChallenge;
3324 if (looking_at(buf, &i, "* offers you") ||
3325 looking_at(buf, &i, "* offers to be") ||
3326 looking_at(buf, &i, "* would like to") ||
3327 looking_at(buf, &i, "* requests to") ||
3328 looking_at(buf, &i, "Your opponent offers") ||
3329 looking_at(buf, &i, "Your opponent requests")) {
3331 if (appData.colorize) {
3332 if (oldi > next_out) {
3333 SendToPlayer(&buf[next_out], oldi - next_out);
3336 Colorize(ColorRequest, FALSE);
3337 curColor = ColorRequest;
3342 if (looking_at(buf, &i, "* (*) seeking")) {
3343 if (appData.colorize) {
3344 if (oldi > next_out) {
3345 SendToPlayer(&buf[next_out], oldi - next_out);
3348 Colorize(ColorSeek, FALSE);
3349 curColor = ColorSeek;
3354 if(i < backup) { i = backup; continue; } // [HGM] for if ZippyControl matches, but the colorie code doesn't
3356 if (looking_at(buf, &i, "\\ ")) {
3357 if (prevColor != ColorNormal) {
3358 if (oldi > next_out) {
3359 SendToPlayer(&buf[next_out], oldi - next_out);
3362 Colorize(prevColor, TRUE);
3363 curColor = prevColor;
3365 if (savingComment) {
3366 parse_pos = i - oldi;
3367 memcpy(parse, &buf[oldi], parse_pos);
3368 parse[parse_pos] = NULLCHAR;
3369 started = STARTED_COMMENT;
3370 if(savingComment >= 3) // [HGM] chat: continuation of line for chat box
3371 chattingPartner = savingComment - 3; // kludge to remember the box
3373 started = STARTED_CHATTER;
3378 if (looking_at(buf, &i, "Black Strength :") ||
3379 looking_at(buf, &i, "<<< style 10 board >>>") ||
3380 looking_at(buf, &i, "<10>") ||
3381 looking_at(buf, &i, "#@#")) {
3382 /* Wrong board style */
3384 SendToICS(ics_prefix);
3385 SendToICS("set style 12\n");
3386 SendToICS(ics_prefix);
3387 SendToICS("refresh\n");
3391 if (!have_sent_ICS_logon && looking_at(buf, &i, "login:")) {
3393 have_sent_ICS_logon = 1;
3397 if (ics_getting_history != H_GETTING_MOVES /*smpos kludge*/ &&
3398 (looking_at(buf, &i, "\n<12> ") ||
3399 looking_at(buf, &i, "<12> "))) {
3401 if (oldi > next_out) {
3402 SendToPlayer(&buf[next_out], oldi - next_out);
3405 started = STARTED_BOARD;
3410 if ((started == STARTED_NONE && looking_at(buf, &i, "\n<b1> ")) ||
3411 looking_at(buf, &i, "<b1> ")) {
3412 if (oldi > next_out) {
3413 SendToPlayer(&buf[next_out], oldi - next_out);
3416 started = STARTED_HOLDINGS;
3421 if (looking_at(buf, &i, "* *vs. * *--- *")) {
3423 /* Header for a move list -- first line */
3425 switch (ics_getting_history) {
3429 case BeginningOfGame:
3430 /* User typed "moves" or "oldmoves" while we
3431 were idle. Pretend we asked for these
3432 moves and soak them up so user can step
3433 through them and/or save them.
3436 gameMode = IcsObserving;
3439 ics_getting_history = H_GOT_UNREQ_HEADER;
3441 case EditGame: /*?*/
3442 case EditPosition: /*?*/
3443 /* Should above feature work in these modes too? */
3444 /* For now it doesn't */
3445 ics_getting_history = H_GOT_UNWANTED_HEADER;
3448 ics_getting_history = H_GOT_UNWANTED_HEADER;
3453 /* Is this the right one? */
3454 if (gameInfo.white && gameInfo.black &&
3455 strcmp(gameInfo.white, star_match[0]) == 0 &&
3456 strcmp(gameInfo.black, star_match[2]) == 0) {
3458 ics_getting_history = H_GOT_REQ_HEADER;
3461 case H_GOT_REQ_HEADER:
3462 case H_GOT_UNREQ_HEADER:
3463 case H_GOT_UNWANTED_HEADER:
3464 case H_GETTING_MOVES:
3465 /* Should not happen */
3466 DisplayError(_("Error gathering move list: two headers"), 0);
3467 ics_getting_history = H_FALSE;
3471 /* Save player ratings into gameInfo if needed */
3472 if ((ics_getting_history == H_GOT_REQ_HEADER ||
3473 ics_getting_history == H_GOT_UNREQ_HEADER) &&
3474 (gameInfo.whiteRating == -1 ||
3475 gameInfo.blackRating == -1)) {
3477 gameInfo.whiteRating = string_to_rating(star_match[1]);
3478 gameInfo.blackRating = string_to_rating(star_match[3]);
3479 if (appData.debugMode)
3480 fprintf(debugFP, _("Ratings from header: W %d, B %d\n"),
3481 gameInfo.whiteRating, gameInfo.blackRating);
3486 if (looking_at(buf, &i,
3487 "* * match, initial time: * minute*, increment: * second")) {
3488 /* Header for a move list -- second line */
3489 /* Initial board will follow if this is a wild game */
3490 if (gameInfo.event != NULL) free(gameInfo.event);
3491 snprintf(str, MSG_SIZ, "ICS %s %s match", star_match[0], star_match[1]);
3492 gameInfo.event = StrSave(str);
3493 /* [HGM] we switched variant. Translate boards if needed. */
3494 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event));
3498 if (looking_at(buf, &i, "Move ")) {
3499 /* Beginning of a move list */
3500 switch (ics_getting_history) {
3502 /* Normally should not happen */
3503 /* Maybe user hit reset while we were parsing */
3506 /* Happens if we are ignoring a move list that is not
3507 * the one we just requested. Common if the user
3508 * tries to observe two games without turning off
3511 case H_GETTING_MOVES:
3512 /* Should not happen */
3513 DisplayError(_("Error gathering move list: nested"), 0);
3514 ics_getting_history = H_FALSE;
3516 case H_GOT_REQ_HEADER:
3517 ics_getting_history = H_GETTING_MOVES;
3518 started = STARTED_MOVES;
3520 if (oldi > next_out) {
3521 SendToPlayer(&buf[next_out], oldi - next_out);
3524 case H_GOT_UNREQ_HEADER:
3525 ics_getting_history = H_GETTING_MOVES;
3526 started = STARTED_MOVES_NOHIDE;
3529 case H_GOT_UNWANTED_HEADER:
3530 ics_getting_history = H_FALSE;
3536 if (looking_at(buf, &i, "% ") ||
3537 ((started == STARTED_MOVES || started == STARTED_MOVES_NOHIDE)
3538 && looking_at(buf, &i, "}*"))) { char *bookHit = NULL; // [HGM] book
3539 if(ics_type == ICS_ICC && soughtPending) { // [HGM] seekgraph: on ICC sought-list has no termination line
3540 soughtPending = FALSE;
3544 if(suppressKibitz) next_out = i;
3545 savingComment = FALSE;
3549 case STARTED_MOVES_NOHIDE:
3550 memcpy(&parse[parse_pos], &buf[oldi], i - oldi);
3551 parse[parse_pos + i - oldi] = NULLCHAR;
3552 ParseGameHistory(parse);
3554 if (appData.zippyPlay && first.initDone) {
3555 FeedMovesToProgram(&first, forwardMostMove);
3556 if (gameMode == IcsPlayingWhite) {
3557 if (WhiteOnMove(forwardMostMove)) {
3558 if (first.sendTime) {
3559 if (first.useColors) {
3560 SendToProgram("black\n", &first);
3562 SendTimeRemaining(&first, TRUE);
3564 if (first.useColors) {
3565 SendToProgram("white\n", &first); // [HGM] book: made sending of "go\n" book dependent
3567 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: probe book for initial pos
3568 first.maybeThinking = TRUE;
3570 if (first.usePlayother) {
3571 if (first.sendTime) {
3572 SendTimeRemaining(&first, TRUE);
3574 SendToProgram("playother\n", &first);
3580 } else if (gameMode == IcsPlayingBlack) {
3581 if (!WhiteOnMove(forwardMostMove)) {
3582 if (first.sendTime) {
3583 if (first.useColors) {
3584 SendToProgram("white\n", &first);
3586 SendTimeRemaining(&first, FALSE);
3588 if (first.useColors) {
3589 SendToProgram("black\n", &first);
3591 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE);
3592 first.maybeThinking = TRUE;
3594 if (first.usePlayother) {
3595 if (first.sendTime) {
3596 SendTimeRemaining(&first, FALSE);
3598 SendToProgram("playother\n", &first);
3607 if (gameMode == IcsObserving && ics_gamenum == -1) {
3608 /* Moves came from oldmoves or moves command
3609 while we weren't doing anything else.
3611 currentMove = forwardMostMove;
3612 ClearHighlights();/*!!could figure this out*/
3613 flipView = appData.flipView;
3614 DrawPosition(TRUE, boards[currentMove]);
3615 DisplayBothClocks();
3616 snprintf(str, MSG_SIZ, "%s vs. %s",
3617 gameInfo.white, gameInfo.black);
3621 /* Moves were history of an active game */
3622 if (gameInfo.resultDetails != NULL) {
3623 free(gameInfo.resultDetails);
3624 gameInfo.resultDetails = NULL;
3627 HistorySet(parseList, backwardMostMove,
3628 forwardMostMove, currentMove-1);
3629 DisplayMove(currentMove - 1);
3630 if (started == STARTED_MOVES) next_out = i;
3631 started = STARTED_NONE;
3632 ics_getting_history = H_FALSE;
3635 case STARTED_OBSERVE:
3636 started = STARTED_NONE;
3637 SendToICS(ics_prefix);
3638 SendToICS("refresh\n");
3644 if(bookHit) { // [HGM] book: simulate book reply
3645 static char bookMove[MSG_SIZ]; // a bit generous?
3647 programStats.nodes = programStats.depth = programStats.time =
3648 programStats.score = programStats.got_only_move = 0;
3649 sprintf(programStats.movelist, "%s (xbook)", bookHit);
3651 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
3652 strcat(bookMove, bookHit);
3653 HandleMachineMove(bookMove, &first);
3658 if ((started == STARTED_MOVES || started == STARTED_BOARD ||
3659 started == STARTED_HOLDINGS ||
3660 started == STARTED_MOVES_NOHIDE) && i >= leftover_len) {
3661 /* Accumulate characters in move list or board */
3662 parse[parse_pos++] = buf[i];
3665 /* Start of game messages. Mostly we detect start of game
3666 when the first board image arrives. On some versions
3667 of the ICS, though, we need to do a "refresh" after starting
3668 to observe in order to get the current board right away. */
3669 if (looking_at(buf, &i, "Adding game * to observation list")) {
3670 started = STARTED_OBSERVE;
3674 /* Handle auto-observe */
3675 if (appData.autoObserve &&
3676 (gameMode == IcsIdle || gameMode == BeginningOfGame) &&
3677 looking_at(buf, &i, "Game notification: * (*) vs. * (*)")) {
3679 /* Choose the player that was highlighted, if any. */
3680 if (star_match[0][0] == '\033' ||
3681 star_match[1][0] != '\033') {
3682 player = star_match[0];
3684 player = star_match[2];
3686 snprintf(str, MSG_SIZ, "%sobserve %s\n",
3687 ics_prefix, StripHighlightAndTitle(player));
3690 /* Save ratings from notify string */
3691 safeStrCpy(player1Name, star_match[0], sizeof(player1Name)/sizeof(player1Name[0]));
3692 player1Rating = string_to_rating(star_match[1]);
3693 safeStrCpy(player2Name, star_match[2], sizeof(player2Name)/sizeof(player2Name[0]));
3694 player2Rating = string_to_rating(star_match[3]);
3696 if (appData.debugMode)
3698 "Ratings from 'Game notification:' %s %d, %s %d\n",
3699 player1Name, player1Rating,
3700 player2Name, player2Rating);
3705 /* Deal with automatic examine mode after a game,
3706 and with IcsObserving -> IcsExamining transition */
3707 if (looking_at(buf, &i, "Entering examine mode for game *") ||
3708 looking_at(buf, &i, "has made you an examiner of game *")) {
3710 int gamenum = atoi(star_match[0]);
3711 if ((gameMode == IcsIdle || gameMode == IcsObserving) &&
3712 gamenum == ics_gamenum) {
3713 /* We were already playing or observing this game;
3714 no need to refetch history */
3715 gameMode = IcsExamining;
3717 pauseExamForwardMostMove = forwardMostMove;
3718 } else if (currentMove < forwardMostMove) {
3719 ForwardInner(forwardMostMove);
3722 /* I don't think this case really can happen */
3723 SendToICS(ics_prefix);
3724 SendToICS("refresh\n");
3729 /* Error messages */
3730 // if (ics_user_moved) {
3731 if (1) { // [HGM] old way ignored error after move type in; ics_user_moved is not set then!
3732 if (looking_at(buf, &i, "Illegal move") ||
3733 looking_at(buf, &i, "Not a legal move") ||
3734 looking_at(buf, &i, "Your king is in check") ||
3735 looking_at(buf, &i, "It isn't your turn") ||
3736 looking_at(buf, &i, "It is not your move")) {
3738 if (ics_user_moved && forwardMostMove > backwardMostMove) { // only backup if we already moved
3739 currentMove = forwardMostMove-1;
3740 DisplayMove(currentMove - 1); /* before DMError */
3741 DrawPosition(FALSE, boards[currentMove]);
3742 SwitchClocks(forwardMostMove-1); // [HGM] race
3743 DisplayBothClocks();
3745 DisplayMoveError(_("Illegal move (rejected by ICS)")); // [HGM] but always relay error msg
3751 if (looking_at(buf, &i, "still have time") ||
3752 looking_at(buf, &i, "not out of time") ||
3753 looking_at(buf, &i, "either player is out of time") ||
3754 looking_at(buf, &i, "has timeseal; checking")) {
3755 /* We must have called his flag a little too soon */
3756 whiteFlag = blackFlag = FALSE;
3760 if (looking_at(buf, &i, "added * seconds to") ||
3761 looking_at(buf, &i, "seconds were added to")) {
3762 /* Update the clocks */
3763 SendToICS(ics_prefix);
3764 SendToICS("refresh\n");
3768 if (!ics_clock_paused && looking_at(buf, &i, "clock paused")) {
3769 ics_clock_paused = TRUE;
3774 if (ics_clock_paused && looking_at(buf, &i, "clock resumed")) {
3775 ics_clock_paused = FALSE;
3780 /* Grab player ratings from the Creating: message.
3781 Note we have to check for the special case when
3782 the ICS inserts things like [white] or [black]. */
3783 if (looking_at(buf, &i, "Creating: * (*)* * (*)") ||
3784 looking_at(buf, &i, "Creating: * (*) [*] * (*)")) {
3786 0 player 1 name (not necessarily white)
3788 2 empty, white, or black (IGNORED)
3789 3 player 2 name (not necessarily black)
3792 The names/ratings are sorted out when the game
3793 actually starts (below).
3795 safeStrCpy(player1Name, StripHighlightAndTitle(star_match[0]), sizeof(player1Name)/sizeof(player1Name[0]));
3796 player1Rating = string_to_rating(star_match[1]);
3797 safeStrCpy(player2Name, StripHighlightAndTitle(star_match[3]), sizeof(player2Name)/sizeof(player2Name[0]));
3798 player2Rating = string_to_rating(star_match[4]);
3800 if (appData.debugMode)
3802 "Ratings from 'Creating:' %s %d, %s %d\n",
3803 player1Name, player1Rating,
3804 player2Name, player2Rating);
3809 /* Improved generic start/end-of-game messages */
3810 if ((tkind=0, looking_at(buf, &i, "{Game * (* vs. *) *}*")) ||
3811 (tkind=1, looking_at(buf, &i, "{Game * (*(*) vs. *(*)) *}*"))){
3812 /* If tkind == 0: */
3813 /* star_match[0] is the game number */
3814 /* [1] is the white player's name */
3815 /* [2] is the black player's name */
3816 /* For end-of-game: */
3817 /* [3] is the reason for the game end */
3818 /* [4] is a PGN end game-token, preceded by " " */
3819 /* For start-of-game: */
3820 /* [3] begins with "Creating" or "Continuing" */
3821 /* [4] is " *" or empty (don't care). */
3822 int gamenum = atoi(star_match[0]);
3823 char *whitename, *blackname, *why, *endtoken;
3824 ChessMove endtype = EndOfFile;
3827 whitename = star_match[1];
3828 blackname = star_match[2];
3829 why = star_match[3];
3830 endtoken = star_match[4];
3832 whitename = star_match[1];
3833 blackname = star_match[3];
3834 why = star_match[5];
3835 endtoken = star_match[6];
3838 /* Game start messages */
3839 if (strncmp(why, "Creating ", 9) == 0 ||
3840 strncmp(why, "Continuing ", 11) == 0) {
3841 gs_gamenum = gamenum;
3842 safeStrCpy(gs_kind, strchr(why, ' ') + 1,sizeof(gs_kind)/sizeof(gs_kind[0]));
3843 VariantSwitch(boards[currentMove], StringToVariant(gs_kind)); // [HGM] variantswitch: even before we get first board
3845 if (appData.zippyPlay) {
3846 ZippyGameStart(whitename, blackname);
3849 partnerBoardValid = FALSE; // [HGM] bughouse
3853 /* Game end messages */
3854 if (gameMode == IcsIdle || gameMode == BeginningOfGame ||
3855 ics_gamenum != gamenum) {
3858 while (endtoken[0] == ' ') endtoken++;
3859 switch (endtoken[0]) {
3862 endtype = GameUnfinished;
3865 endtype = BlackWins;
3868 if (endtoken[1] == '/')
3869 endtype = GameIsDrawn;
3871 endtype = WhiteWins;
3874 GameEnds(endtype, why, GE_ICS);
3876 if (appData.zippyPlay && first.initDone) {
3877 ZippyGameEnd(endtype, why);
3878 if (first.pr == NULL) {
3879 /* Start the next process early so that we'll
3880 be ready for the next challenge */
3881 StartChessProgram(&first);
3883 /* Send "new" early, in case this command takes
3884 a long time to finish, so that we'll be ready
3885 for the next challenge. */
3886 gameInfo.variant = VariantNormal; // [HGM] variantswitch: suppress sending of 'variant'
3890 if(appData.bgObserve && partnerBoardValid) DrawPosition(TRUE, partnerBoard);
3894 if (looking_at(buf, &i, "Removing game * from observation") ||
3895 looking_at(buf, &i, "no longer observing game *") ||
3896 looking_at(buf, &i, "Game * (*) has no examiners")) {
3897 if (gameMode == IcsObserving &&
3898 atoi(star_match[0]) == ics_gamenum)
3900 /* icsEngineAnalyze */
3901 if (appData.icsEngineAnalyze) {
3908 ics_user_moved = FALSE;
3913 if (looking_at(buf, &i, "no longer examining game *")) {
3914 if (gameMode == IcsExamining &&
3915 atoi(star_match[0]) == ics_gamenum)
3919 ics_user_moved = FALSE;
3924 /* Advance leftover_start past any newlines we find,
3925 so only partial lines can get reparsed */
3926 if (looking_at(buf, &i, "\n")) {
3927 prevColor = curColor;
3928 if (curColor != ColorNormal) {
3929 if (oldi > next_out) {
3930 SendToPlayer(&buf[next_out], oldi - next_out);
3933 Colorize(ColorNormal, FALSE);
3934 curColor = ColorNormal;
3936 if (started == STARTED_BOARD) {
3937 started = STARTED_NONE;
3938 parse[parse_pos] = NULLCHAR;
3939 ParseBoard12(parse);
3942 /* Send premove here */
3943 if (appData.premove) {
3945 if (currentMove == 0 &&
3946 gameMode == IcsPlayingWhite &&
3947 appData.premoveWhite) {
3948 snprintf(str, MSG_SIZ, "%s\n", appData.premoveWhiteText);
3949 if (appData.debugMode)
3950 fprintf(debugFP, "Sending premove:\n");
3952 } else if (currentMove == 1 &&
3953 gameMode == IcsPlayingBlack &&
3954 appData.premoveBlack) {
3955 snprintf(str, MSG_SIZ, "%s\n", appData.premoveBlackText);
3956 if (appData.debugMode)
3957 fprintf(debugFP, "Sending premove:\n");
3959 } else if (gotPremove) {
3961 ClearPremoveHighlights();
3962 if (appData.debugMode)
3963 fprintf(debugFP, "Sending premove:\n");
3964 UserMoveEvent(premoveFromX, premoveFromY,
3965 premoveToX, premoveToY,
3970 /* Usually suppress following prompt */
3971 if (!(forwardMostMove == 0 && gameMode == IcsExamining)) {
3972 while(looking_at(buf, &i, "\n")); // [HGM] skip empty lines
3973 if (looking_at(buf, &i, "*% ")) {
3974 savingComment = FALSE;
3979 } else if (started == STARTED_HOLDINGS) {
3981 char new_piece[MSG_SIZ];
3982 started = STARTED_NONE;
3983 parse[parse_pos] = NULLCHAR;
3984 if (appData.debugMode)
3985 fprintf(debugFP, "Parsing holdings: %s, currentMove = %d\n",
3986 parse, currentMove);
3987 if (sscanf(parse, " game %d", &gamenum) == 1) {
3988 if(gamenum == ics_gamenum) { // [HGM] bughouse: old code if part of foreground game
3989 if (gameInfo.variant == VariantNormal) {
3990 /* [HGM] We seem to switch variant during a game!
3991 * Presumably no holdings were displayed, so we have
3992 * to move the position two files to the right to
3993 * create room for them!
3995 VariantClass newVariant;
3996 switch(gameInfo.boardWidth) { // base guess on board width
3997 case 9: newVariant = VariantShogi; break;
3998 case 10: newVariant = VariantGreat; break;
3999 default: newVariant = VariantCrazyhouse; break;
4001 VariantSwitch(boards[currentMove], newVariant); /* temp guess */
4002 /* Get a move list just to see the header, which
4003 will tell us whether this is really bug or zh */
4004 if (ics_getting_history == H_FALSE) {
4005 ics_getting_history = H_REQUESTED;
4006 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4010 new_piece[0] = NULLCHAR;
4011 sscanf(parse, "game %d white [%s black [%s <- %s",
4012 &gamenum, white_holding, black_holding,
4014 white_holding[strlen(white_holding)-1] = NULLCHAR;
4015 black_holding[strlen(black_holding)-1] = NULLCHAR;
4016 /* [HGM] copy holdings to board holdings area */
4017 CopyHoldings(boards[forwardMostMove], white_holding, WhitePawn);
4018 CopyHoldings(boards[forwardMostMove], black_holding, BlackPawn);
4019 boards[forwardMostMove][HOLDINGS_SET] = 1; // flag holdings as set
4021 if (appData.zippyPlay && first.initDone) {
4022 ZippyHoldings(white_holding, black_holding,
4026 if (tinyLayout || smallLayout) {
4027 char wh[16], bh[16];
4028 PackHolding(wh, white_holding);
4029 PackHolding(bh, black_holding);
4030 snprintf(str, MSG_SIZ,"[%s-%s] %s-%s", wh, bh,
4031 gameInfo.white, gameInfo.black);
4033 snprintf(str, MSG_SIZ, "%s [%s] vs. %s [%s]",
4034 gameInfo.white, white_holding,
4035 gameInfo.black, black_holding);
4037 if(!partnerUp) // [HGM] bughouse: when peeking at partner game we already know what he captured...
4038 DrawPosition(FALSE, boards[currentMove]);
4040 } else if(appData.bgObserve) { // [HGM] bughouse: holdings of other game => background
4041 sscanf(parse, "game %d white [%s black [%s <- %s",
4042 &gamenum, white_holding, black_holding,
4044 white_holding[strlen(white_holding)-1] = NULLCHAR;
4045 black_holding[strlen(black_holding)-1] = NULLCHAR;
4046 /* [HGM] copy holdings to partner-board holdings area */
4047 CopyHoldings(partnerBoard, white_holding, WhitePawn);
4048 CopyHoldings(partnerBoard, black_holding, BlackPawn);
4049 if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual: always draw
4050 if(partnerUp) DrawPosition(FALSE, partnerBoard);
4051 if(twoBoards) { partnerUp = 0; flipView = !flipView; }
4054 /* Suppress following prompt */
4055 if (looking_at(buf, &i, "*% ")) {
4056 if(strchr(star_match[0], 7)) SendToPlayer("\007", 1); // Bell(); // FICS fuses bell for next board with prompt in zh captures
4057 savingComment = FALSE;
4065 i++; /* skip unparsed character and loop back */
4068 if (started != STARTED_MOVES && started != STARTED_BOARD && !suppressKibitz && // [HGM] kibitz
4069 // started != STARTED_HOLDINGS && i > next_out) { // [HGM] should we compare to leftover_start in stead of i?
4070 // SendToPlayer(&buf[next_out], i - next_out);
4071 started != STARTED_HOLDINGS && leftover_start > next_out) {
4072 SendToPlayer(&buf[next_out], leftover_start - next_out);
4076 leftover_len = buf_len - leftover_start;
4077 /* if buffer ends with something we couldn't parse,
4078 reparse it after appending the next read */
4080 } else if (count == 0) {
4081 RemoveInputSource(isr);
4082 DisplayFatalError(_("Connection closed by ICS"), 0, 0);
4084 DisplayFatalError(_("Error reading from ICS"), error, 1);
4089 /* Board style 12 looks like this:
4091 <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
4093 * The "<12> " is stripped before it gets to this routine. The two
4094 * trailing 0's (flip state and clock ticking) are later addition, and
4095 * some chess servers may not have them, or may have only the first.
4096 * Additional trailing fields may be added in the future.
4099 #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"
4101 #define RELATION_OBSERVING_PLAYED 0
4102 #define RELATION_OBSERVING_STATIC -2 /* examined, oldmoves, or smoves */
4103 #define RELATION_PLAYING_MYMOVE 1
4104 #define RELATION_PLAYING_NOTMYMOVE -1
4105 #define RELATION_EXAMINING 2
4106 #define RELATION_ISOLATED_BOARD -3
4107 #define RELATION_STARTING_POSITION -4 /* FICS only */
4110 ParseBoard12(string)
4113 GameMode newGameMode;
4114 int gamenum, newGame, newMove, relation, basetime, increment, ics_flip = 0, i;
4115 int j, k, n, moveNum, white_stren, black_stren, white_time, black_time, takeback;
4116 int double_push, castle_ws, castle_wl, castle_bs, castle_bl, irrev_count;
4117 char to_play, board_chars[200];
4118 char move_str[MSG_SIZ], str[MSG_SIZ], elapsed_time[MSG_SIZ];
4119 char black[32], white[32];
4121 int prevMove = currentMove;
4124 int fromX, fromY, toX, toY;
4126 int ranks=1, files=0; /* [HGM] ICS80: allow variable board size */
4127 char *bookHit = NULL; // [HGM] book
4128 Boolean weird = FALSE, reqFlag = FALSE;
4130 fromX = fromY = toX = toY = -1;
4134 if (appData.debugMode)
4135 fprintf(debugFP, _("Parsing board: %s\n"), string);
4137 move_str[0] = NULLCHAR;
4138 elapsed_time[0] = NULLCHAR;
4139 { /* [HGM] figure out how many ranks and files the board has, for ICS extension used by Capablanca server */
4141 while(i < 199 && (string[i] != ' ' || string[i+2] != ' ')) {
4142 if(string[i] == ' ') { ranks++; files = 0; }
4144 if(!strchr(" -pnbrqkPNBRQK" , string[i])) weird = TRUE; // test for fairies
4147 for(j = 0; j <i; j++) board_chars[j] = string[j];
4148 board_chars[i] = '\0';
4151 n = sscanf(string, PATTERN, &to_play, &double_push,
4152 &castle_ws, &castle_wl, &castle_bs, &castle_bl, &irrev_count,
4153 &gamenum, white, black, &relation, &basetime, &increment,
4154 &white_stren, &black_stren, &white_time, &black_time,
4155 &moveNum, str, elapsed_time, move_str, &ics_flip,
4159 snprintf(str, MSG_SIZ, _("Failed to parse board string:\n\"%s\""), string);
4160 DisplayError(str, 0);
4164 /* Convert the move number to internal form */
4165 moveNum = (moveNum - 1) * 2;
4166 if (to_play == 'B') moveNum++;
4167 if (moveNum > framePtr) { // [HGM] vari: do not run into saved variations
4168 DisplayFatalError(_("Game too long; increase MAX_MOVES and recompile"),
4174 case RELATION_OBSERVING_PLAYED:
4175 case RELATION_OBSERVING_STATIC:
4176 if (gamenum == -1) {
4177 /* Old ICC buglet */
4178 relation = RELATION_OBSERVING_STATIC;
4180 newGameMode = IcsObserving;
4182 case RELATION_PLAYING_MYMOVE:
4183 case RELATION_PLAYING_NOTMYMOVE:
4185 ((relation == RELATION_PLAYING_MYMOVE) == (to_play == 'W')) ?
4186 IcsPlayingWhite : IcsPlayingBlack;
4188 case RELATION_EXAMINING:
4189 newGameMode = IcsExamining;
4191 case RELATION_ISOLATED_BOARD:
4193 /* Just display this board. If user was doing something else,
4194 we will forget about it until the next board comes. */
4195 newGameMode = IcsIdle;
4197 case RELATION_STARTING_POSITION:
4198 newGameMode = gameMode;
4202 if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)
4203 && newGameMode == IcsObserving && gamenum != ics_gamenum && appData.bgObserve) {
4204 // [HGM] bughouse: don't act on alien boards while we play. Just parse the board and save it */
4206 for (k = 0; k < ranks; k++) {
4207 for (j = 0; j < files; j++)
4208 board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
4209 if(gameInfo.holdingsWidth > 1) {
4210 board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
4211 board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
4214 CopyBoard(partnerBoard, board);
4215 if(toSqr = strchr(str, '/')) { // extract highlights from long move
4216 partnerBoard[EP_STATUS-3] = toSqr[1] - AAA; // kludge: hide highlighting info in board
4217 partnerBoard[EP_STATUS-4] = toSqr[2] - ONE;
4218 } else partnerBoard[EP_STATUS-4] = partnerBoard[EP_STATUS-3] = -1;
4219 if(toSqr = strchr(str, '-')) {
4220 partnerBoard[EP_STATUS-1] = toSqr[1] - AAA;
4221 partnerBoard[EP_STATUS-2] = toSqr[2] - ONE;
4222 } else partnerBoard[EP_STATUS-1] = partnerBoard[EP_STATUS-2] = -1;
4223 if(appData.dualBoard && !twoBoards) { twoBoards = 1; InitDrawingSizes(-2,0); }
4224 if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual
4225 if(partnerUp) DrawPosition(FALSE, partnerBoard);
4226 if(twoBoards) { partnerUp = 0; flipView = !flipView; } // [HGM] dual
4227 snprintf(partnerStatus, MSG_SIZ,"W: %d:%02d B: %d:%02d (%d-%d) %c", white_time/60000, (white_time%60000)/1000,
4228 (black_time/60000), (black_time%60000)/1000, white_stren, black_stren, to_play);
4229 DisplayMessage(partnerStatus, "");
4230 partnerBoardValid = TRUE;
4234 /* Modify behavior for initial board display on move listing
4237 switch (ics_getting_history) {
4241 case H_GOT_REQ_HEADER:
4242 case H_GOT_UNREQ_HEADER:
4243 /* This is the initial position of the current game */
4244 gamenum = ics_gamenum;
4245 moveNum = 0; /* old ICS bug workaround */
4246 if (to_play == 'B') {
4247 startedFromSetupPosition = TRUE;
4248 blackPlaysFirst = TRUE;
4250 if (forwardMostMove == 0) forwardMostMove = 1;
4251 if (backwardMostMove == 0) backwardMostMove = 1;
4252 if (currentMove == 0) currentMove = 1;
4254 newGameMode = gameMode;
4255 relation = RELATION_STARTING_POSITION; /* ICC needs this */
4257 case H_GOT_UNWANTED_HEADER:
4258 /* This is an initial board that we don't want */
4260 case H_GETTING_MOVES:
4261 /* Should not happen */
4262 DisplayError(_("Error gathering move list: extra board"), 0);
4263 ics_getting_history = H_FALSE;
4267 if (gameInfo.boardHeight != ranks || gameInfo.boardWidth != files ||
4268 weird && (int)gameInfo.variant < (int)VariantShogi) {
4269 /* [HGM] We seem to have switched variant unexpectedly
4270 * Try to guess new variant from board size
4272 VariantClass newVariant = VariantFairy; // if 8x8, but fairies present
4273 if(ranks == 8 && files == 10) newVariant = VariantCapablanca; else
4274 if(ranks == 10 && files == 9) newVariant = VariantXiangqi; else
4275 if(ranks == 8 && files == 12) newVariant = VariantCourier; else
4276 if(ranks == 9 && files == 9) newVariant = VariantShogi; else
4277 if(!weird) newVariant = VariantNormal;
4278 VariantSwitch(boards[currentMove], newVariant); /* temp guess */
4279 /* Get a move list just to see the header, which
4280 will tell us whether this is really bug or zh */
4281 if (ics_getting_history == H_FALSE) {
4282 ics_getting_history = H_REQUESTED; reqFlag = TRUE;
4283 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4288 /* Take action if this is the first board of a new game, or of a
4289 different game than is currently being displayed. */
4290 if (gamenum != ics_gamenum || newGameMode != gameMode ||
4291 relation == RELATION_ISOLATED_BOARD) {
4293 /* Forget the old game and get the history (if any) of the new one */
4294 if (gameMode != BeginningOfGame) {
4298 if (appData.autoRaiseBoard) BoardToTop();
4300 if (gamenum == -1) {
4301 newGameMode = IcsIdle;
4302 } else if ((moveNum > 0 || newGameMode == IcsObserving) && newGameMode != IcsIdle &&
4303 appData.getMoveList && !reqFlag) {
4304 /* Need to get game history */
4305 ics_getting_history = H_REQUESTED;
4306 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4310 /* Initially flip the board to have black on the bottom if playing
4311 black or if the ICS flip flag is set, but let the user change
4312 it with the Flip View button. */
4313 flipView = appData.autoFlipView ?
4314 (newGameMode == IcsPlayingBlack) || ics_flip :
4317 /* Done with values from previous mode; copy in new ones */
4318 gameMode = newGameMode;
4320 ics_gamenum = gamenum;
4321 if (gamenum == gs_gamenum) {
4322 int klen = strlen(gs_kind);
4323 if (gs_kind[klen - 1] == '.') gs_kind[klen - 1] = NULLCHAR;
4324 snprintf(str, MSG_SIZ, "ICS %s", gs_kind);
4325 gameInfo.event = StrSave(str);
4327 gameInfo.event = StrSave("ICS game");
4329 gameInfo.site = StrSave(appData.icsHost);
4330 gameInfo.date = PGNDate();
4331 gameInfo.round = StrSave("-");
4332 gameInfo.white = StrSave(white);
4333 gameInfo.black = StrSave(black);
4334 timeControl = basetime * 60 * 1000;
4336 timeIncrement = increment * 1000;
4337 movesPerSession = 0;
4338 gameInfo.timeControl = TimeControlTagValue();
4339 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event) );
4340 if (appData.debugMode) {
4341 fprintf(debugFP, "ParseBoard says variant = '%s'\n", gameInfo.event);
4342 fprintf(debugFP, "recognized as %s\n", VariantName(gameInfo.variant));
4343 setbuf(debugFP, NULL);
4346 gameInfo.outOfBook = NULL;
4348 /* Do we have the ratings? */
4349 if (strcmp(player1Name, white) == 0 &&
4350 strcmp(player2Name, black) == 0) {
4351 if (appData.debugMode)
4352 fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
4353 player1Rating, player2Rating);
4354 gameInfo.whiteRating = player1Rating;
4355 gameInfo.blackRating = player2Rating;
4356 } else if (strcmp(player2Name, white) == 0 &&
4357 strcmp(player1Name, black) == 0) {
4358 if (appData.debugMode)
4359 fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
4360 player2Rating, player1Rating);
4361 gameInfo.whiteRating = player2Rating;
4362 gameInfo.blackRating = player1Rating;
4364 player1Name[0] = player2Name[0] = NULLCHAR;
4366 /* Silence shouts if requested */
4367 if (appData.quietPlay &&
4368 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)) {
4369 SendToICS(ics_prefix);
4370 SendToICS("set shout 0\n");
4374 /* Deal with midgame name changes */
4376 if (!gameInfo.white || strcmp(gameInfo.white, white) != 0) {
4377 if (gameInfo.white) free(gameInfo.white);
4378 gameInfo.white = StrSave(white);
4380 if (!gameInfo.black || strcmp(gameInfo.black, black) != 0) {
4381 if (gameInfo.black) free(gameInfo.black);
4382 gameInfo.black = StrSave(black);
4386 /* Throw away game result if anything actually changes in examine mode */
4387 if (gameMode == IcsExamining && !newGame) {
4388 gameInfo.result = GameUnfinished;
4389 if (gameInfo.resultDetails != NULL) {
4390 free(gameInfo.resultDetails);
4391 gameInfo.resultDetails = NULL;
4395 /* In pausing && IcsExamining mode, we ignore boards coming
4396 in if they are in a different variation than we are. */
4397 if (pauseExamInvalid) return;
4398 if (pausing && gameMode == IcsExamining) {
4399 if (moveNum <= pauseExamForwardMostMove) {
4400 pauseExamInvalid = TRUE;
4401 forwardMostMove = pauseExamForwardMostMove;
4406 if (appData.debugMode) {
4407 fprintf(debugFP, "load %dx%d board\n", files, ranks);
4409 /* Parse the board */
4410 for (k = 0; k < ranks; k++) {
4411 for (j = 0; j < files; j++)
4412 board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
4413 if(gameInfo.holdingsWidth > 1) {
4414 board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
4415 board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
4418 CopyBoard(boards[moveNum], board);
4419 boards[moveNum][HOLDINGS_SET] = 0; // [HGM] indicate holdings not set
4421 startedFromSetupPosition =
4422 !CompareBoards(board, initialPosition);
4423 if(startedFromSetupPosition)
4424 initialRulePlies = irrev_count; /* [HGM] 50-move counter offset */
4427 /* [HGM] Set castling rights. Take the outermost Rooks,
4428 to make it also work for FRC opening positions. Note that board12
4429 is really defective for later FRC positions, as it has no way to
4430 indicate which Rook can castle if they are on the same side of King.
4431 For the initial position we grant rights to the outermost Rooks,
4432 and remember thos rights, and we then copy them on positions
4433 later in an FRC game. This means WB might not recognize castlings with
4434 Rooks that have moved back to their original position as illegal,
4435 but in ICS mode that is not its job anyway.
4437 if(moveNum == 0 || gameInfo.variant != VariantFischeRandom)
4438 { int i, j; ChessSquare wKing = WhiteKing, bKing = BlackKing;
4440 for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4441 if(board[0][i] == WhiteRook) j = i;
4442 initialRights[0] = boards[moveNum][CASTLING][0] = (castle_ws == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4443 for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4444 if(board[0][i] == WhiteRook) j = i;
4445 initialRights[1] = boards[moveNum][CASTLING][1] = (castle_wl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4446 for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4447 if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4448 initialRights[3] = boards[moveNum][CASTLING][3] = (castle_bs == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4449 for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4450 if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4451 initialRights[4] = boards[moveNum][CASTLING][4] = (castle_bl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4453 if(gameInfo.variant == VariantKnightmate) { wKing = WhiteUnicorn; bKing = BlackUnicorn; }
4454 for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4455 if(board[0][k] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = k;
4456 for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4457 if(board[BOARD_HEIGHT-1][k] == bKing)
4458 initialRights[5] = boards[moveNum][CASTLING][5] = k;
4459 if(gameInfo.variant == VariantTwoKings) {
4460 // In TwoKings looking for a King does not work, so always give castling rights to a King on e1/e8
4461 if(board[0][4] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = 4;
4462 if(board[BOARD_HEIGHT-1][4] == bKing) initialRights[5] = boards[moveNum][CASTLING][5] = 4;
4465 r = boards[moveNum][CASTLING][0] = initialRights[0];
4466 if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][0] = NoRights;
4467 r = boards[moveNum][CASTLING][1] = initialRights[1];
4468 if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][1] = NoRights;
4469 r = boards[moveNum][CASTLING][3] = initialRights[3];
4470 if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][3] = NoRights;
4471 r = boards[moveNum][CASTLING][4] = initialRights[4];
4472 if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][4] = NoRights;
4473 /* wildcastle kludge: always assume King has rights */
4474 r = boards[moveNum][CASTLING][2] = initialRights[2];
4475 r = boards[moveNum][CASTLING][5] = initialRights[5];
4477 /* [HGM] e.p. rights. Assume that ICS sends file number here? */
4478 boards[moveNum][EP_STATUS] = double_push == -1 ? EP_NONE : double_push + BOARD_LEFT;
4481 if (ics_getting_history == H_GOT_REQ_HEADER ||
4482 ics_getting_history == H_GOT_UNREQ_HEADER) {
4483 /* This was an initial position from a move list, not
4484 the current position */
4488 /* Update currentMove and known move number limits */
4489 newMove = newGame || moveNum > forwardMostMove;
4492 forwardMostMove = backwardMostMove = currentMove = moveNum;
4493 if (gameMode == IcsExamining && moveNum == 0) {
4494 /* Workaround for ICS limitation: we are not told the wild
4495 type when starting to examine a game. But if we ask for
4496 the move list, the move list header will tell us */
4497 ics_getting_history = H_REQUESTED;
4498 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4501 } else if (moveNum == forwardMostMove + 1 || moveNum == forwardMostMove
4502 || (moveNum < forwardMostMove && moveNum >= backwardMostMove)) {
4504 /* [DM] If we found takebacks during icsEngineAnalyze try send to engine */
4505 /* [HGM] applied this also to an engine that is silently watching */
4506 if (appData.zippyPlay && moveNum < forwardMostMove && first.initDone &&
4507 (gameMode == IcsObserving || gameMode == IcsExamining) &&
4508 gameInfo.variant == currentlyInitializedVariant) {
4509 takeback = forwardMostMove - moveNum;
4510 for (i = 0; i < takeback; i++) {
4511 if (appData.debugMode) fprintf(debugFP, "take back move\n");
4512 SendToProgram("undo\n", &first);
4517 forwardMostMove = moveNum;
4518 if (!pausing || currentMove > forwardMostMove)
4519 currentMove = forwardMostMove;
4521 /* New part of history that is not contiguous with old part */
4522 if (pausing && gameMode == IcsExamining) {
4523 pauseExamInvalid = TRUE;
4524 forwardMostMove = pauseExamForwardMostMove;
4527 if (gameMode == IcsExamining && moveNum > 0 && appData.getMoveList) {
4529 if(appData.zippyPlay && forwardMostMove > 0 && first.initDone) {
4530 // [HGM] when we will receive the move list we now request, it will be
4531 // fed to the engine from the first move on. So if the engine is not
4532 // in the initial position now, bring it there.
4533 InitChessProgram(&first, 0);
4536 ics_getting_history = H_REQUESTED;
4537 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4540 forwardMostMove = backwardMostMove = currentMove = moveNum;
4543 /* Update the clocks */
4544 if (strchr(elapsed_time, '.')) {
4546 timeRemaining[0][moveNum] = whiteTimeRemaining = white_time;
4547 timeRemaining[1][moveNum] = blackTimeRemaining = black_time;
4549 /* Time is in seconds */
4550 timeRemaining[0][moveNum] = whiteTimeRemaining = white_time * 1000;
4551 timeRemaining[1][moveNum] = blackTimeRemaining = black_time * 1000;
4556 if (appData.zippyPlay && newGame &&
4557 gameMode != IcsObserving && gameMode != IcsIdle &&
4558 gameMode != IcsExamining)
4559 ZippyFirstBoard(moveNum, basetime, increment);
4562 /* Put the move on the move list, first converting
4563 to canonical algebraic form. */
4565 if (appData.debugMode) {
4566 if (appData.debugMode) { int f = forwardMostMove;
4567 fprintf(debugFP, "parseboard %d, castling = %d %d %d %d %d %d\n", f,
4568 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
4569 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
4571 fprintf(debugFP, "accepted move %s from ICS, parse it.\n", move_str);
4572 fprintf(debugFP, "moveNum = %d\n", moveNum);
4573 fprintf(debugFP, "board = %d-%d x %d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT);
4574 setbuf(debugFP, NULL);
4576 if (moveNum <= backwardMostMove) {
4577 /* We don't know what the board looked like before
4579 safeStrCpy(parseList[moveNum - 1], move_str, sizeof(parseList[moveNum - 1])/sizeof(parseList[moveNum - 1][0]));
4580 strcat(parseList[moveNum - 1], " ");
4581 strcat(parseList[moveNum - 1], elapsed_time);
4582 moveList[moveNum - 1][0] = NULLCHAR;
4583 } else if (strcmp(move_str, "none") == 0) {
4584 // [HGM] long SAN: swapped order; test for 'none' before parsing move
4585 /* Again, we don't know what the board looked like;
4586 this is really the start of the game. */
4587 parseList[moveNum - 1][0] = NULLCHAR;
4588 moveList[moveNum - 1][0] = NULLCHAR;
4589 backwardMostMove = moveNum;
4590 startedFromSetupPosition = TRUE;
4591 fromX = fromY = toX = toY = -1;
4593 // [HGM] long SAN: if legality-testing is off, disambiguation might not work or give wrong move.
4594 // So we parse the long-algebraic move string in stead of the SAN move
4595 int valid; char buf[MSG_SIZ], *prom;
4597 if(gameInfo.variant == VariantShogi && !strchr(move_str, '=') && !strchr(move_str, '@'))
4598 strcat(move_str, "="); // if ICS does not say 'promote' on non-drop, we defer.
4599 // str looks something like "Q/a1-a2"; kill the slash
4601 snprintf(buf, MSG_SIZ,"%c%s", str[0], str+2);
4602 else safeStrCpy(buf, str, sizeof(buf)/sizeof(buf[0])); // might be castling
4603 if((prom = strstr(move_str, "=")) && !strstr(buf, "="))
4604 strcat(buf, prom); // long move lacks promo specification!
4605 if(!appData.testLegality && move_str[1] != '@') { // drops never ambiguous (parser chokes on long form!)
4606 if(appData.debugMode)
4607 fprintf(debugFP, "replaced ICS move '%s' by '%s'\n", move_str, buf);
4608 safeStrCpy(move_str, buf, MSG_SIZ);
4610 valid = ParseOneMove(move_str, moveNum - 1, &moveType,
4611 &fromX, &fromY, &toX, &toY, &promoChar)
4612 || ParseOneMove(buf, moveNum - 1, &moveType,
4613 &fromX, &fromY, &toX, &toY, &promoChar);
4614 // end of long SAN patch
4616 (void) CoordsToAlgebraic(boards[moveNum - 1],
4617 PosFlags(moveNum - 1),
4618 fromY, fromX, toY, toX, promoChar,
4619 parseList[moveNum-1]);
4620 switch (MateTest(boards[moveNum], PosFlags(moveNum)) ) {
4626 if(gameInfo.variant != VariantShogi)
4627 strcat(parseList[moveNum - 1], "+");
4630 case MT_STAINMATE: // [HGM] xq: for notation stalemate that wins counts as checkmate
4631 strcat(parseList[moveNum - 1], "#");
4634 strcat(parseList[moveNum - 1], " ");
4635 strcat(parseList[moveNum - 1], elapsed_time);
4636 /* currentMoveString is set as a side-effect of ParseOneMove */
4637 if(gameInfo.variant == VariantShogi && currentMoveString[4]) currentMoveString[4] = '^';
4638 safeStrCpy(moveList[moveNum - 1], currentMoveString, sizeof(moveList[moveNum - 1])/sizeof(moveList[moveNum - 1][0]));
4639 strcat(moveList[moveNum - 1], "\n");
4641 if(gameInfo.holdingsWidth && !appData.disguise && gameInfo.variant != VariantSuper
4642 && gameInfo.variant != VariantGreat) // inherit info that ICS does not give from previous board
4643 for(k=0; k<ranks; k++) for(j=BOARD_LEFT; j<BOARD_RGHT; j++) {
4644 ChessSquare old, new = boards[moveNum][k][j];
4645 if(fromY == DROP_RANK && k==toY && j==toX) continue; // dropped pieces always stand for themselves
4646 old = (k==toY && j==toX) ? boards[moveNum-1][fromY][fromX] : boards[moveNum-1][k][j]; // trace back mover
4647 if(old == new) continue;
4648 if(old == PROMOTED new) boards[moveNum][k][j] = old; // prevent promoted pieces to revert to primordial ones
4649 else if(new == WhiteWazir || new == BlackWazir) {
4650 if(old < WhiteCannon || old >= BlackPawn && old < BlackCannon)
4651 boards[moveNum][k][j] = PROMOTED old; // choose correct type of Gold in promotion
4652 else boards[moveNum][k][j] = old; // preserve type of Gold
4653 } else if((old == WhitePawn || old == BlackPawn) && new != EmptySquare) // Pawn promotions (but not e.p.capture!)
4654 boards[moveNum][k][j] = PROMOTED new; // use non-primordial representation of chosen piece
4657 /* Move from ICS was illegal!? Punt. */
4658 if (appData.debugMode) {
4659 fprintf(debugFP, "Illegal move from ICS '%s'\n", move_str);
4660 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
4662 safeStrCpy(parseList[moveNum - 1], move_str, sizeof(parseList[moveNum - 1])/sizeof(parseList[moveNum - 1][0]));
4663 strcat(parseList[moveNum - 1], " ");
4664 strcat(parseList[moveNum - 1], elapsed_time);
4665 moveList[moveNum - 1][0] = NULLCHAR;
4666 fromX = fromY = toX = toY = -1;
4669 if (appData.debugMode) {
4670 fprintf(debugFP, "Move parsed to '%s'\n", parseList[moveNum - 1]);
4671 setbuf(debugFP, NULL);
4675 /* Send move to chess program (BEFORE animating it). */
4676 if (appData.zippyPlay && !newGame && newMove &&
4677 (!appData.getMoveList || backwardMostMove == 0) && first.initDone) {
4679 if ((gameMode == IcsPlayingWhite && WhiteOnMove(moveNum)) ||
4680 (gameMode == IcsPlayingBlack && !WhiteOnMove(moveNum))) {
4681 if (moveList[moveNum - 1][0] == NULLCHAR) {
4682 snprintf(str, MSG_SIZ, _("Couldn't parse move \"%s\" from ICS"),
4684 DisplayError(str, 0);
4686 if (first.sendTime) {
4687 SendTimeRemaining(&first, gameMode == IcsPlayingWhite);
4689 bookHit = SendMoveToBookUser(moveNum - 1, &first, FALSE); // [HGM] book
4690 if (firstMove && !bookHit) {
4692 if (first.useColors) {
4693 SendToProgram(gameMode == IcsPlayingWhite ?
4695 "black\ngo\n", &first);
4697 SendToProgram("go\n", &first);
4699 first.maybeThinking = TRUE;
4702 } else if (gameMode == IcsObserving || gameMode == IcsExamining) {
4703 if (moveList[moveNum - 1][0] == NULLCHAR) {
4704 snprintf(str, MSG_SIZ, _("Couldn't parse move \"%s\" from ICS"), move_str);
4705 DisplayError(str, 0);
4707 if(gameInfo.variant == currentlyInitializedVariant) // [HGM] refrain sending moves engine can't understand!
4708 SendMoveToProgram(moveNum - 1, &first);
4715 if (moveNum > 0 && !gotPremove && !appData.noGUI) {
4716 /* If move comes from a remote source, animate it. If it
4717 isn't remote, it will have already been animated. */
4718 if (!pausing && !ics_user_moved && prevMove == moveNum - 1) {
4719 AnimateMove(boards[moveNum - 1], fromX, fromY, toX, toY);
4721 if (!pausing && appData.highlightLastMove) {
4722 SetHighlights(fromX, fromY, toX, toY);
4726 /* Start the clocks */
4727 whiteFlag = blackFlag = FALSE;
4728 appData.clockMode = !(basetime == 0 && increment == 0);
4730 ics_clock_paused = TRUE;
4732 } else if (ticking == 1) {
4733 ics_clock_paused = FALSE;
4735 if (gameMode == IcsIdle ||
4736 relation == RELATION_OBSERVING_STATIC ||
4737 relation == RELATION_EXAMINING ||
4739 DisplayBothClocks();
4743 /* Display opponents and material strengths */
4744 if (gameInfo.variant != VariantBughouse &&
4745 gameInfo.variant != VariantCrazyhouse && !appData.noGUI) {
4746 if (tinyLayout || smallLayout) {
4747 if(gameInfo.variant == VariantNormal)
4748 snprintf(str, MSG_SIZ, "%s(%d) %s(%d) {%d %d}",
4749 gameInfo.white, white_stren, gameInfo.black, black_stren,
4750 basetime, increment);
4752 snprintf(str, MSG_SIZ, "%s(%d) %s(%d) {%d %d w%d}",
4753 gameInfo.white, white_stren, gameInfo.black, black_stren,
4754 basetime, increment, (int) gameInfo.variant);
4756 if(gameInfo.variant == VariantNormal)
4757 snprintf(str, MSG_SIZ, "%s (%d) vs. %s (%d) {%d %d}",
4758 gameInfo.white, white_stren, gameInfo.black, black_stren,
4759 basetime, increment);
4761 snprintf(str, MSG_SIZ, "%s (%d) vs. %s (%d) {%d %d %s}",
4762 gameInfo.white, white_stren, gameInfo.black, black_stren,
4763 basetime, increment, VariantName(gameInfo.variant));
4766 if (appData.debugMode) {
4767 fprintf(debugFP, "Display title '%s, gameInfo.variant = %d'\n", str, gameInfo.variant);
4772 /* Display the board */
4773 if (!pausing && !appData.noGUI) {
4775 if (appData.premove)
4777 ((gameMode == IcsPlayingWhite) && (WhiteOnMove(currentMove))) ||
4778 ((gameMode == IcsPlayingBlack) && (!WhiteOnMove(currentMove))))
4779 ClearPremoveHighlights();
4781 j = seekGraphUp; seekGraphUp = FALSE; // [HGM] seekgraph: when we draw a board, it overwrites the seek graph
4782 if(partnerUp) { flipView = originalFlip; partnerUp = FALSE; j = TRUE; } // [HGM] bughouse: restore view
4783 DrawPosition(j, boards[currentMove]);
4785 DisplayMove(moveNum - 1);
4786 if (appData.ringBellAfterMoves && /*!ics_user_moved*/ // [HGM] use absolute method to recognize own move
4787 !((gameMode == IcsPlayingWhite) && (!WhiteOnMove(moveNum)) ||
4788 (gameMode == IcsPlayingBlack) && (WhiteOnMove(moveNum)) ) ) {
4789 if(newMove) RingBell(); else PlayIcsUnfinishedSound();
4793 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
4795 if(bookHit) { // [HGM] book: simulate book reply
4796 static char bookMove[MSG_SIZ]; // a bit generous?
4798 programStats.nodes = programStats.depth = programStats.time =
4799 programStats.score = programStats.got_only_move = 0;
4800 sprintf(programStats.movelist, "%s (xbook)", bookHit);
4802 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
4803 strcat(bookMove, bookHit);
4804 HandleMachineMove(bookMove, &first);
4813 if (appData.icsActive && gameMode != IcsIdle && ics_gamenum > 0) {
4814 ics_getting_history = H_REQUESTED;
4815 snprintf(buf, MSG_SIZ, "%smoves %d\n", ics_prefix, ics_gamenum);
4821 AnalysisPeriodicEvent(force)
4824 if (((programStats.ok_to_send == 0 || programStats.line_is_book)
4825 && !force) || !appData.periodicUpdates)
4828 /* Send . command to Crafty to collect stats */
4829 SendToProgram(".\n", &first);
4831 /* Don't send another until we get a response (this makes
4832 us stop sending to old Crafty's which don't understand
4833 the "." command (sending illegal cmds resets node count & time,
4834 which looks bad)) */
4835 programStats.ok_to_send = 0;
4838 void ics_update_width(new_width)
4841 ics_printf("set width %d\n", new_width);
4845 SendMoveToProgram(moveNum, cps)
4847 ChessProgramState *cps;
4851 if (cps->useUsermove) {
4852 SendToProgram("usermove ", cps);
4856 if ((space = strchr(parseList[moveNum], ' ')) != NULL) {
4857 int len = space - parseList[moveNum];
4858 memcpy(buf, parseList[moveNum], len);
4860 buf[len] = NULLCHAR;
4862 snprintf(buf, MSG_SIZ,"%s\n", parseList[moveNum]);
4864 SendToProgram(buf, cps);
4866 if(cps->alphaRank) { /* [HGM] shogi: temporarily convert to shogi coordinates before sending */
4867 AlphaRank(moveList[moveNum], 4);
4868 SendToProgram(moveList[moveNum], cps);
4869 AlphaRank(moveList[moveNum], 4); // and back
4871 /* Added by Tord: Send castle moves in "O-O" in FRC games if required by
4872 * the engine. It would be nice to have a better way to identify castle
4874 if((gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom)
4875 && cps->useOOCastle) {
4876 int fromX = moveList[moveNum][0] - AAA;
4877 int fromY = moveList[moveNum][1] - ONE;
4878 int toX = moveList[moveNum][2] - AAA;
4879 int toY = moveList[moveNum][3] - ONE;
4880 if((boards[moveNum][fromY][fromX] == WhiteKing
4881 && boards[moveNum][toY][toX] == WhiteRook)
4882 || (boards[moveNum][fromY][fromX] == BlackKing
4883 && boards[moveNum][toY][toX] == BlackRook)) {
4884 if(toX > fromX) SendToProgram("O-O\n", cps);
4885 else SendToProgram("O-O-O\n", cps);
4887 else SendToProgram(moveList[moveNum], cps);
4889 else SendToProgram(moveList[moveNum], cps);
4890 /* End of additions by Tord */
4893 /* [HGM] setting up the opening has brought engine in force mode! */
4894 /* Send 'go' if we are in a mode where machine should play. */
4895 if( (moveNum == 0 && setboardSpoiledMachineBlack && cps == &first) &&
4896 (gameMode == TwoMachinesPlay ||
4898 gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite ||
4900 gameMode == MachinePlaysBlack || gameMode == MachinePlaysWhite) ) {
4901 SendToProgram("go\n", cps);
4902 if (appData.debugMode) {
4903 fprintf(debugFP, "(extra)\n");
4906 setboardSpoiledMachineBlack = 0;
4910 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar)
4912 int fromX, fromY, toX, toY;
4915 char user_move[MSG_SIZ];
4919 snprintf(user_move, MSG_SIZ, _("say Internal error; bad moveType %d (%d,%d-%d,%d)"),
4920 (int)moveType, fromX, fromY, toX, toY);
4921 DisplayError(user_move + strlen("say "), 0);
4923 case WhiteKingSideCastle:
4924 case BlackKingSideCastle:
4925 case WhiteQueenSideCastleWild:
4926 case BlackQueenSideCastleWild:
4928 case WhiteHSideCastleFR:
4929 case BlackHSideCastleFR:
4931 snprintf(user_move, MSG_SIZ, "o-o\n");
4933 case WhiteQueenSideCastle:
4934 case BlackQueenSideCastle:
4935 case WhiteKingSideCastleWild:
4936 case BlackKingSideCastleWild:
4938 case WhiteASideCastleFR:
4939 case BlackASideCastleFR:
4941 snprintf(user_move, MSG_SIZ, "o-o-o\n");
4943 case WhiteNonPromotion:
4944 case BlackNonPromotion:
4945 sprintf(user_move, "%c%c%c%c==\n", AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
4947 case WhitePromotion:
4948 case BlackPromotion:
4949 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
4950 snprintf(user_move, MSG_SIZ, "%c%c%c%c=%c\n",
4951 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
4952 PieceToChar(WhiteFerz));
4953 else if(gameInfo.variant == VariantGreat)
4954 snprintf(user_move, MSG_SIZ,"%c%c%c%c=%c\n",
4955 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
4956 PieceToChar(WhiteMan));
4958 snprintf(user_move, MSG_SIZ, "%c%c%c%c=%c\n",
4959 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
4965 snprintf(user_move, MSG_SIZ, "%c@%c%c\n",
4966 ToUpper(PieceToChar((ChessSquare) fromX)),
4967 AAA + toX, ONE + toY);
4969 case IllegalMove: /* could be a variant we don't quite understand */
4970 if(fromY == DROP_RANK) goto drop; // We need 'IllegalDrop' move type?
4972 case WhiteCapturesEnPassant:
4973 case BlackCapturesEnPassant:
4974 snprintf(user_move, MSG_SIZ,"%c%c%c%c\n",
4975 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
4978 SendToICS(user_move);
4979 if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
4980 ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
4985 { // [HGM] upload: send entire stored game to ICS as long-algebraic moves.
4986 int i, last = forwardMostMove; // make sure ICS reply cannot pre-empt us by clearing fmm
4987 static char *castlingStrings[4] = { "none", "kside", "qside", "both" };
4988 if(gameMode == IcsObserving || gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite) {
4989 DisplayError("You cannot do this while you are playing or observing", 0);
4992 if(gameMode != IcsExamining) { // is this ever not the case?
4993 char buf[MSG_SIZ], *p, *fen, command[MSG_SIZ], bsetup = 0;
4995 if(ics_type == ICS_ICC) { // on ICC match ourselves in applicable variant
4996 snprintf(command,MSG_SIZ, "match %s", ics_handle);
4997 } else { // on FICS we must first go to general examine mode
4998 safeStrCpy(command, "examine\nbsetup", sizeof(command)/sizeof(command[0])); // and specify variant within it with bsetups
5000 if(gameInfo.variant != VariantNormal) {
5001 // try figure out wild number, as xboard names are not always valid on ICS
5002 for(i=1; i<=36; i++) {
5003 snprintf(buf, MSG_SIZ, "wild/%d", i);
5004 if(StringToVariant(buf) == gameInfo.variant) break;
5006 if(i<=36 && ics_type == ICS_ICC) snprintf(buf, MSG_SIZ,"%s w%d\n", command, i);
5007 else if(i == 22) snprintf(buf,MSG_SIZ, "%s fr\n", command);
5008 else snprintf(buf, MSG_SIZ,"%s %s\n", command, VariantName(gameInfo.variant));
5009 } else snprintf(buf, MSG_SIZ,"%s\n", ics_type == ICS_ICC ? command : "examine\n"); // match yourself or examine
5010 SendToICS(ics_prefix);
5012 if(startedFromSetupPosition || backwardMostMove != 0) {
5013 fen = PositionToFEN(backwardMostMove, NULL);
5014 if(ics_type == ICS_ICC) { // on ICC we can simply send a complete FEN to set everything
5015 snprintf(buf, MSG_SIZ,"loadfen %s\n", fen);
5017 } else { // FICS: everything has to set by separate bsetup commands
5018 p = strchr(fen, ' '); p[0] = NULLCHAR; // cut after board
5019 snprintf(buf, MSG_SIZ,"bsetup fen %s\n", fen);
5021 if(!WhiteOnMove(backwardMostMove)) {
5022 SendToICS("bsetup tomove black\n");
5024 i = (strchr(p+3, 'K') != NULL) + 2*(strchr(p+3, 'Q') != NULL);
5025 snprintf(buf, MSG_SIZ,"bsetup wcastle %s\n", castlingStrings[i]);
5027 i = (strchr(p+3, 'k') != NULL) + 2*(strchr(p+3, 'q') != NULL);
5028 snprintf(buf, MSG_SIZ, "bsetup bcastle %s\n", castlingStrings[i]);
5030 i = boards[backwardMostMove][EP_STATUS];
5031 if(i >= 0) { // set e.p.
5032 snprintf(buf, MSG_SIZ,"bsetup eppos %c\n", i+AAA);
5038 if(bsetup || ics_type != ICS_ICC && gameInfo.variant != VariantNormal)
5039 SendToICS("bsetup done\n"); // switch to normal examining.
5041 for(i = backwardMostMove; i<last; i++) {
5043 snprintf(buf, sizeof(buf)/sizeof(buf[0]),"%s\n", parseList[i]);
5046 SendToICS(ics_prefix);
5047 SendToICS(ics_type == ICS_ICC ? "tag result Game in progress\n" : "commit\n");
5051 CoordsToComputerAlgebraic(rf, ff, rt, ft, promoChar, move)
5056 if (rf == DROP_RANK) {
5057 sprintf(move, "%c@%c%c\n",
5058 ToUpper(PieceToChar((ChessSquare) ff)), AAA + ft, ONE + rt);
5060 if (promoChar == 'x' || promoChar == NULLCHAR) {
5061 sprintf(move, "%c%c%c%c\n",
5062 AAA + ff, ONE + rf, AAA + ft, ONE + rt);
5064 sprintf(move, "%c%c%c%c%c\n",
5065 AAA + ff, ONE + rf, AAA + ft, ONE + rt, promoChar);
5071 ProcessICSInitScript(f)
5076 while (fgets(buf, MSG_SIZ, f)) {
5077 SendToICSDelayed(buf,(long)appData.msLoginDelay);
5084 static int lastX, lastY, selectFlag, dragging;
5089 ChessSquare king = WhiteKing, pawn = WhitePawn, last = promoSweep;
5090 if(gameInfo.variant == VariantKnightmate) king = WhiteUnicorn;
5091 if(gameInfo.variant == VariantSuicide || gameInfo.variant == VariantGiveaway) king = EmptySquare;
5092 if(promoSweep >= BlackPawn) king = WHITE_TO_BLACK king, pawn = WHITE_TO_BLACK pawn;
5093 if(gameInfo.variant == VariantSpartan && pawn == BlackPawn) pawn = BlackLance, king = EmptySquare;
5094 if(fromY != BOARD_HEIGHT-2 && fromY != 1) pawn = EmptySquare;
5097 if(promoSweep == EmptySquare) promoSweep = BlackPawn; // wrap
5098 else if((int)promoSweep == -1) promoSweep = WhiteKing;
5099 else if(promoSweep == BlackPawn && step < 0) promoSweep = WhitePawn;
5100 else if(promoSweep == WhiteKing && step > 0) promoSweep = BlackKing;
5102 } while(PieceToChar(promoSweep) == '.' || PieceToChar(promoSweep) == '~' || promoSweep == pawn ||
5103 appData.testLegality && (promoSweep == king ||
5104 gameInfo.variant == VariantShogi && promoSweep != PROMOTED last && last != PROMOTED promoSweep && last != promoSweep));
5105 ChangeDragPiece(promoSweep);
5108 int PromoScroll(int x, int y)
5112 if(promoSweep == EmptySquare || !appData.sweepSelect) return FALSE;
5113 if(abs(x - lastX) < 15 && abs(y - lastY) < 15) return FALSE;
5114 if( y > lastY + 2 ) step = -1; else if(y < lastY - 2) step = 1;
5115 if(!step) return FALSE;
5116 lastX = x; lastY = y;
5117 if((promoSweep < BlackPawn) == flipView) step = -step;
5118 if(step > 0) selectFlag = 1;
5119 if(!selectFlag) Sweep(step);
5126 ChessSquare piece = boards[currentMove][toY][toX];
5129 if(pieceSweep == EmptySquare) pieceSweep = WhitePawn; // wrap
5130 if((int)pieceSweep == -1) pieceSweep = BlackKing;
5131 if(!step) step = -1;
5132 } while(PieceToChar(pieceSweep) == '.');
5133 boards[currentMove][toY][toX] = pieceSweep;
5134 DrawPosition(FALSE, boards[currentMove]);
5135 boards[currentMove][toY][toX] = piece;
5137 /* [HGM] Shogi move preprocessor: swap digits for letters, vice versa */
5139 AlphaRank(char *move, int n)
5141 // char *p = move, c; int x, y;
5143 if (appData.debugMode) {
5144 fprintf(debugFP, "alphaRank(%s,%d)\n", move, n);
5148 move[2]>='0' && move[2]<='9' &&
5149 move[3]>='a' && move[3]<='x' ) {
5151 move[2] = BOARD_RGHT -1 - (move[2]-'1') + AAA;
5152 move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
5154 if(move[0]>='0' && move[0]<='9' &&
5155 move[1]>='a' && move[1]<='x' &&
5156 move[2]>='0' && move[2]<='9' &&
5157 move[3]>='a' && move[3]<='x' ) {
5158 /* input move, Shogi -> normal */
5159 move[0] = BOARD_RGHT -1 - (move[0]-'1') + AAA;
5160 move[1] = BOARD_HEIGHT-1 - (move[1]-'a') + ONE;
5161 move[2] = BOARD_RGHT -1 - (move[2]-'1') + AAA;
5162 move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
5165 move[3]>='0' && move[3]<='9' &&
5166 move[2]>='a' && move[2]<='x' ) {
5168 move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
5169 move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
5172 move[0]>='a' && move[0]<='x' &&
5173 move[3]>='0' && move[3]<='9' &&
5174 move[2]>='a' && move[2]<='x' ) {
5175 /* output move, normal -> Shogi */
5176 move[0] = BOARD_RGHT - 1 - (move[0]-AAA) + '1';
5177 move[1] = BOARD_HEIGHT-1 - (move[1]-ONE) + 'a';
5178 move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
5179 move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
5180 if(move[4] == PieceToChar(BlackQueen)) move[4] = '+';
5182 if (appData.debugMode) {
5183 fprintf(debugFP, " out = '%s'\n", move);
5187 char yy_textstr[8000];
5189 /* Parser for moves from gnuchess, ICS, or user typein box */
5191 ParseOneMove(move, moveNum, moveType, fromX, fromY, toX, toY, promoChar)
5194 ChessMove *moveType;
5195 int *fromX, *fromY, *toX, *toY;
5198 *moveType = yylexstr(moveNum, move, yy_textstr, sizeof yy_textstr);
5200 switch (*moveType) {
5201 case WhitePromotion:
5202 case BlackPromotion:
5203 case WhiteNonPromotion:
5204 case BlackNonPromotion:
5206 case WhiteCapturesEnPassant:
5207 case BlackCapturesEnPassant:
5208 case WhiteKingSideCastle:
5209 case WhiteQueenSideCastle:
5210 case BlackKingSideCastle:
5211 case BlackQueenSideCastle:
5212 case WhiteKingSideCastleWild:
5213 case WhiteQueenSideCastleWild:
5214 case BlackKingSideCastleWild:
5215 case BlackQueenSideCastleWild:
5216 /* Code added by Tord: */
5217 case WhiteHSideCastleFR:
5218 case WhiteASideCastleFR:
5219 case BlackHSideCastleFR:
5220 case BlackASideCastleFR:
5221 /* End of code added by Tord */
5222 case IllegalMove: /* bug or odd chess variant */
5223 *fromX = currentMoveString[0] - AAA;
5224 *fromY = currentMoveString[1] - ONE;
5225 *toX = currentMoveString[2] - AAA;
5226 *toY = currentMoveString[3] - ONE;
5227 *promoChar = currentMoveString[4];
5228 if (*fromX < BOARD_LEFT || *fromX >= BOARD_RGHT || *fromY < 0 || *fromY >= BOARD_HEIGHT ||
5229 *toX < BOARD_LEFT || *toX >= BOARD_RGHT || *toY < 0 || *toY >= BOARD_HEIGHT) {
5230 if (appData.debugMode) {
5231 fprintf(debugFP, "Off-board move (%d,%d)-(%d,%d)%c, type = %d\n", *fromX, *fromY, *toX, *toY, *promoChar, *moveType);
5233 *fromX = *fromY = *toX = *toY = 0;
5236 if (appData.testLegality) {
5237 return (*moveType != IllegalMove);
5239 return !(*fromX == *toX && *fromY == *toY) && boards[moveNum][*fromY][*fromX] != EmptySquare &&
5240 WhiteOnMove(moveNum) == (boards[moveNum][*fromY][*fromX] < BlackPawn);
5245 *fromX = *moveType == WhiteDrop ?
5246 (int) CharToPiece(ToUpper(currentMoveString[0])) :
5247 (int) CharToPiece(ToLower(currentMoveString[0]));
5249 *toX = currentMoveString[2] - AAA;
5250 *toY = currentMoveString[3] - ONE;
5251 *promoChar = NULLCHAR;
5255 case ImpossibleMove:
5265 if (appData.debugMode) {
5266 fprintf(debugFP, "Impossible move %s, type = %d\n", currentMoveString, *moveType);
5269 *fromX = *fromY = *toX = *toY = 0;
5270 *promoChar = NULLCHAR;
5275 Boolean pushed = FALSE;
5278 ParsePV(char *pv, Boolean storeComments, Boolean atEnd)
5279 { // Parse a string of PV moves, and append to current game, behind forwardMostMove
5280 int fromX, fromY, toX, toY; char promoChar;
5285 if (gameMode == AnalyzeMode && currentMove < forwardMostMove) {
5286 PushInner(currentMove, forwardMostMove); // [HGM] engine might not be thinking on forwardMost position!
5289 endPV = forwardMostMove;
5291 while(*pv == ' ' || *pv == '\n' || *pv == '\t') pv++; // must still read away whitespace
5292 if(nr == 0 && !storeComments && *pv == '(') pv++; // first (ponder) move can be in parentheses
5293 valid = ParseOneMove(pv, endPV, &moveType, &fromX, &fromY, &toX, &toY, &promoChar);
5294 if(appData.debugMode){
5295 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);
5297 if(!valid && nr == 0 &&
5298 ParseOneMove(pv, endPV-1, &moveType, &fromX, &fromY, &toX, &toY, &promoChar)){
5299 nr++; moveType = Comment; // First move has been played; kludge to make sure we continue
5300 // Hande case where played move is different from leading PV move
5301 CopyBoard(boards[endPV+1], boards[endPV-1]); // tentatively unplay last game move
5302 CopyBoard(boards[endPV+2], boards[endPV-1]); // and play first move of PV
5303 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV+2]);
5304 if(!CompareBoards(boards[endPV], boards[endPV+2])) {
5305 endPV += 2; // if position different, keep this
5306 moveList[endPV-1][0] = fromX + AAA;
5307 moveList[endPV-1][1] = fromY + ONE;
5308 moveList[endPV-1][2] = toX + AAA;
5309 moveList[endPV-1][3] = toY + ONE;
5310 parseList[endPV-1][0] = NULLCHAR;
5311 safeStrCpy(moveList[endPV-2], "_0_0", sizeof(moveList[endPV-2])/sizeof(moveList[endPV-2][0])); // suppress premove highlight on takeback move
5314 pv = strstr(pv, yy_textstr) + strlen(yy_textstr); // skip what we parsed
5315 if(nr == 0 && !storeComments && *pv == ')') pv++; // closing parenthesis of ponder move;
5316 if(moveType == Comment && storeComments) AppendComment(endPV, yy_textstr, FALSE);
5317 if(moveType == Comment || moveType == NAG || moveType == ElapsedTime) {
5318 valid++; // allow comments in PV
5322 if(endPV+1 > framePtr) break; // no space, truncate
5325 CopyBoard(boards[endPV], boards[endPV-1]);
5326 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV]);
5327 CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar, moveList[endPV - 1]);
5328 strncat(moveList[endPV-1], "\n", MOVE_LEN);
5329 CoordsToAlgebraic(boards[endPV - 1],
5330 PosFlags(endPV - 1),
5331 fromY, fromX, toY, toX, promoChar,
5332 parseList[endPV - 1]);
5334 currentMove = (atEnd || endPV == forwardMostMove) ? endPV : forwardMostMove + 1;
5335 if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
5336 SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
5337 moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
5338 DrawPosition(TRUE, boards[currentMove]);
5342 MultiPV(ChessProgramState *cps)
5343 { // check if engine supports MultiPV, and if so, return the number of the option that sets it
5345 for(i=0; i<cps->nrOptions; i++)
5346 if(!strcmp(cps->option[i].name, "MultiPV") && cps->option[i].type == Spin)
5352 LoadMultiPV(int x, int y, char *buf, int index, int *start, int *end)
5354 int startPV, multi, lineStart, origIndex = index;
5355 char *p, buf2[MSG_SIZ];
5357 if(index < 0 || index >= strlen(buf)) return FALSE; // sanity
5358 lastX = x; lastY = y;
5359 while(index > 0 && buf[index-1] != '\n') index--; // beginning of line
5360 lineStart = startPV = index;
5361 while(buf[index] != '\n') if(buf[index++] == '\t') startPV = index;
5362 if(index == startPV && (p = StrCaseStr(buf+index, "PV="))) startPV = p - buf + 3;
5364 do{ while(buf[index] && buf[index] != '\n') index++;
5365 } while(buf[index] == '\n' && buf[index+1] == '\\' && buf[index+2] == ' ' && index++); // join kibitzed PV continuation line
5367 if(lineStart == 0 && gameMode == AnalyzeMode && (multi = MultiPV(&first)) >= 0) {
5368 int n = first.option[multi].value;
5369 if(origIndex > 17 && origIndex < 24) { if(n>1) n--; } else if(origIndex > index - 6) n++;
5370 snprintf(buf2, MSG_SIZ, "option MultiPV=%d\n", n);
5371 if(first.option[multi].value != n) SendToProgram(buf2, &first);
5372 first.option[multi].value = n;
5376 ParsePV(buf+startPV, FALSE, gameMode != AnalyzeMode);
5377 *start = startPV; *end = index-1;
5382 LoadPV(int x, int y)
5383 { // called on right mouse click to load PV
5384 int which = gameMode == TwoMachinesPlay && (WhiteOnMove(forwardMostMove) == (second.twoMachinesColor[0] == 'w'));
5385 lastX = x; lastY = y;
5386 ParsePV(lastPV[which], FALSE, TRUE); // load the PV of the thinking engine in the boards array.
5393 int oldFMM = forwardMostMove; // N.B.: this was currentMove before PV was loaded!
5394 if(endPV < 0) return;
5396 if(gameMode == AnalyzeMode && currentMove > forwardMostMove) {
5397 Boolean saveAnimate = appData.animate;
5399 if(shiftKey && storedGames < MAX_VARIATIONS-2) { // wants to start variation, and there is space
5400 if(storedGames == 1) GreyRevert(FALSE); // we already pushed the tail, so just make it official
5401 } else storedGames--; // abandon shelved tail of original game
5404 forwardMostMove = currentMove;
5405 currentMove = oldFMM;
5406 appData.animate = FALSE;
5407 ToNrEvent(forwardMostMove);
5408 appData.animate = saveAnimate;
5410 currentMove = forwardMostMove;
5411 if(pushed) { PopInner(0); pushed = FALSE; } // restore shelved game continuation
5412 ClearPremoveHighlights();
5413 DrawPosition(TRUE, boards[currentMove]);
5417 MovePV(int x, int y, int h)
5418 { // step through PV based on mouse coordinates (called on mouse move)
5419 int margin = h>>3, step = 0;
5421 // we must somehow check if right button is still down (might be released off board!)
5422 if(endPV < 0 && pieceSweep == EmptySquare) return; // needed in XBoard because lastX/Y is shared :-(
5423 if(abs(x - lastX) < 7 && abs(y - lastY) < 7) return;
5424 if( y > lastY + 2 ) step = -1; else if(y < lastY - 2) step = 1;
5426 lastX = x; lastY = y;
5428 if(pieceSweep != EmptySquare) { NextPiece(step); return; }
5429 if(endPV < 0) return;
5430 if(y < margin) step = 1; else
5431 if(y > h - margin) step = -1;
5432 if(currentMove + step > endPV || currentMove + step < forwardMostMove) step = 0;
5433 currentMove += step;
5434 if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
5435 SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
5436 moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
5437 DrawPosition(FALSE, boards[currentMove]);
5441 // [HGM] shuffle: a general way to suffle opening setups, applicable to arbitrary variants.
5442 // All positions will have equal probability, but the current method will not provide a unique
5443 // numbering scheme for arrays that contain 3 or more pieces of the same kind.
5449 int piecesLeft[(int)BlackPawn];
5450 int seed, nrOfShuffles;
5452 void GetPositionNumber()
5453 { // sets global variable seed
5456 seed = appData.defaultFrcPosition;
5457 if(seed < 0) { // randomize based on time for negative FRC position numbers
5458 for(i=0; i<50; i++) seed += random();
5459 seed = random() ^ random() >> 8 ^ random() << 8;
5460 if(seed<0) seed = -seed;
5464 int put(Board board, int pieceType, int rank, int n, int shade)
5465 // put the piece on the (n-1)-th empty squares of the given shade
5469 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
5470 if( (((i-BOARD_LEFT)&1)+1) & shade && board[rank][i] == EmptySquare && n-- == 0) {
5471 board[rank][i] = (ChessSquare) pieceType;
5472 squaresLeft[((i-BOARD_LEFT)&1) + 1]--;
5474 piecesLeft[pieceType]--;
5482 void AddOnePiece(Board board, int pieceType, int rank, int shade)
5483 // calculate where the next piece goes, (any empty square), and put it there
5487 i = seed % squaresLeft[shade];
5488 nrOfShuffles *= squaresLeft[shade];
5489 seed /= squaresLeft[shade];
5490 put(board, pieceType, rank, i, shade);
5493 void AddTwoPieces(Board board, int pieceType, int rank)
5494 // calculate where the next 2 identical pieces go, (any empty square), and put it there
5496 int i, n=squaresLeft[ANY], j=n-1, k;
5498 k = n*(n-1)/2; // nr of possibilities, not counting permutations
5499 i = seed % k; // pick one
5502 while(i >= j) i -= j--;
5503 j = n - 1 - j; i += j;
5504 put(board, pieceType, rank, j, ANY);
5505 put(board, pieceType, rank, i, ANY);
5508 void SetUpShuffle(Board board, int number)
5512 GetPositionNumber(); nrOfShuffles = 1;
5514 squaresLeft[DARK] = (BOARD_RGHT - BOARD_LEFT + 1)/2;
5515 squaresLeft[ANY] = BOARD_RGHT - BOARD_LEFT;
5516 squaresLeft[LITE] = squaresLeft[ANY] - squaresLeft[DARK];
5518 for(p = 0; p<=(int)WhiteKing; p++) piecesLeft[p] = 0;
5520 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // count pieces and clear board
5521 p = (int) board[0][i];
5522 if(p < (int) BlackPawn) piecesLeft[p] ++;
5523 board[0][i] = EmptySquare;
5526 if(PosFlags(0) & F_ALL_CASTLE_OK) {
5527 // shuffles restricted to allow normal castling put KRR first
5528 if(piecesLeft[(int)WhiteKing]) // King goes rightish of middle
5529 put(board, WhiteKing, 0, (gameInfo.boardWidth+1)/2, ANY);
5530 else if(piecesLeft[(int)WhiteUnicorn]) // in Knightmate Unicorn castles
5531 put(board, WhiteUnicorn, 0, (gameInfo.boardWidth+1)/2, ANY);
5532 if(piecesLeft[(int)WhiteRook]) // First supply a Rook for K-side castling
5533 put(board, WhiteRook, 0, gameInfo.boardWidth-2, ANY);
5534 if(piecesLeft[(int)WhiteRook]) // Then supply a Rook for Q-side castling
5535 put(board, WhiteRook, 0, 0, ANY);
5536 // in variants with super-numerary Kings and Rooks, we leave these for the shuffle
5539 if(((BOARD_RGHT-BOARD_LEFT) & 1) == 0)
5540 // only for even boards make effort to put pairs of colorbound pieces on opposite colors
5541 for(p = (int) WhiteKing; p > (int) WhitePawn; p--) {
5542 if(p != (int) WhiteBishop && p != (int) WhiteFerz && p != (int) WhiteAlfil) continue;
5543 while(piecesLeft[p] >= 2) {
5544 AddOnePiece(board, p, 0, LITE);
5545 AddOnePiece(board, p, 0, DARK);
5547 // Odd color-bound pieces are shuffled with the rest (to not run out of paired squares)
5550 for(p = (int) WhiteKing - 2; p > (int) WhitePawn; p--) {
5551 // Remaining pieces (non-colorbound, or odd color bound) can be put anywhere
5552 // but we leave King and Rooks for last, to possibly obey FRC restriction
5553 if(p == (int)WhiteRook) continue;
5554 while(piecesLeft[p] >= 2) AddTwoPieces(board, p, 0); // add in pairs, for not counting permutations
5555 if(piecesLeft[p]) AddOnePiece(board, p, 0, ANY); // add the odd piece
5558 // now everything is placed, except perhaps King (Unicorn) and Rooks
5560 if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
5561 // Last King gets castling rights
5562 while(piecesLeft[(int)WhiteUnicorn]) {
5563 i = put(board, WhiteUnicorn, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5564 initialRights[2] = initialRights[5] = board[CASTLING][2] = board[CASTLING][5] = i;
5567 while(piecesLeft[(int)WhiteKing]) {
5568 i = put(board, WhiteKing, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5569 initialRights[2] = initialRights[5] = board[CASTLING][2] = board[CASTLING][5] = i;
5574 while(piecesLeft[(int)WhiteKing]) AddOnePiece(board, WhiteKing, 0, ANY);
5575 while(piecesLeft[(int)WhiteUnicorn]) AddOnePiece(board, WhiteUnicorn, 0, ANY);
5578 // Only Rooks can be left; simply place them all
5579 while(piecesLeft[(int)WhiteRook]) {
5580 i = put(board, WhiteRook, 0, 0, ANY);
5581 if(PosFlags(0) & F_FRC_TYPE_CASTLING) { // first and last Rook get FRC castling rights
5584 initialRights[1] = initialRights[4] = board[CASTLING][1] = board[CASTLING][4] = i;
5586 initialRights[0] = initialRights[3] = board[CASTLING][0] = board[CASTLING][3] = i;
5589 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // copy black from white
5590 board[BOARD_HEIGHT-1][i] = (int) board[0][i] < BlackPawn ? WHITE_TO_BLACK board[0][i] : EmptySquare;
5593 if(number >= 0) appData.defaultFrcPosition %= nrOfShuffles; // normalize
5596 int SetCharTable( char *table, const char * map )
5597 /* [HGM] moved here from winboard.c because of its general usefulness */
5598 /* Basically a safe strcpy that uses the last character as King */
5600 int result = FALSE; int NrPieces;
5602 if( map != NULL && (NrPieces=strlen(map)) <= (int) EmptySquare
5603 && NrPieces >= 12 && !(NrPieces&1)) {
5604 int i; /* [HGM] Accept even length from 12 to 34 */
5606 for( i=0; i<(int) EmptySquare; i++ ) table[i] = '.';
5607 for( i=0; i<NrPieces/2-1; i++ ) {
5609 table[i + (int)BlackPawn - (int) WhitePawn] = map[i+NrPieces/2];
5611 table[(int) WhiteKing] = map[NrPieces/2-1];
5612 table[(int) BlackKing] = map[NrPieces-1];
5620 void Prelude(Board board)
5621 { // [HGM] superchess: random selection of exo-pieces
5622 int i, j, k; ChessSquare p;
5623 static ChessSquare exoPieces[4] = { WhiteAngel, WhiteMarshall, WhiteSilver, WhiteLance };
5625 GetPositionNumber(); // use FRC position number
5627 if(appData.pieceToCharTable != NULL) { // select pieces to participate from given char table
5628 SetCharTable(pieceToChar, appData.pieceToCharTable);
5629 for(i=(int)WhiteQueen+1, j=0; i<(int)WhiteKing && j<4; i++)
5630 if(PieceToChar((ChessSquare)i) != '.') exoPieces[j++] = (ChessSquare) i;
5633 j = seed%4; seed /= 4;
5634 p = board[0][BOARD_LEFT+j]; board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
5635 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5636 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5637 j = seed%3 + (seed%3 >= j); seed /= 3;
5638 p = board[0][BOARD_LEFT+j]; board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
5639 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5640 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5641 j = seed%3; seed /= 3;
5642 p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
5643 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5644 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5645 j = seed%2 + (seed%2 >= j); seed /= 2;
5646 p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
5647 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5648 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5649 j = seed%4; seed /= 4; put(board, exoPieces[3], 0, j, ANY);
5650 j = seed%3; seed /= 3; put(board, exoPieces[2], 0, j, ANY);
5651 j = seed%2; seed /= 2; put(board, exoPieces[1], 0, j, ANY);
5652 put(board, exoPieces[0], 0, 0, ANY);
5653 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) board[BOARD_HEIGHT-1][i] = WHITE_TO_BLACK board[0][i];
5657 InitPosition(redraw)
5660 ChessSquare (* pieces)[BOARD_FILES];
5661 int i, j, pawnRow, overrule,
5662 oldx = gameInfo.boardWidth,
5663 oldy = gameInfo.boardHeight,
5664 oldh = gameInfo.holdingsWidth;
5667 if(appData.icsActive) shuffleOpenings = FALSE; // [HGM] shuffle: in ICS mode, only shuffle on ICS request
5669 /* [AS] Initialize pv info list [HGM] and game status */
5671 for( i=0; i<=framePtr; i++ ) { // [HGM] vari: spare saved variations
5672 pvInfoList[i].depth = 0;
5673 boards[i][EP_STATUS] = EP_NONE;
5674 for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
5677 initialRulePlies = 0; /* 50-move counter start */
5679 castlingRank[0] = castlingRank[1] = castlingRank[2] = 0;
5680 castlingRank[3] = castlingRank[4] = castlingRank[5] = BOARD_HEIGHT-1;
5684 /* [HGM] logic here is completely changed. In stead of full positions */
5685 /* the initialized data only consist of the two backranks. The switch */
5686 /* selects which one we will use, which is than copied to the Board */
5687 /* initialPosition, which for the rest is initialized by Pawns and */
5688 /* empty squares. This initial position is then copied to boards[0], */
5689 /* possibly after shuffling, so that it remains available. */
5691 gameInfo.holdingsWidth = 0; /* default board sizes */
5692 gameInfo.boardWidth = 8;
5693 gameInfo.boardHeight = 8;
5694 gameInfo.holdingsSize = 0;
5695 nrCastlingRights = -1; /* [HGM] Kludge to indicate default should be used */
5696 for(i=0; i<BOARD_FILES-2; i++)
5697 initialPosition[CASTLING][i] = initialRights[i] = NoRights; /* but no rights yet */
5698 initialPosition[EP_STATUS] = EP_NONE;
5699 SetCharTable(pieceToChar, "PNBRQ...........Kpnbrq...........k");
5700 if(startVariant == gameInfo.variant) // [HGM] nicks: enable nicknames in original variant
5701 SetCharTable(pieceNickName, appData.pieceNickNames);
5702 else SetCharTable(pieceNickName, "............");
5705 switch (gameInfo.variant) {
5706 case VariantFischeRandom:
5707 shuffleOpenings = TRUE;
5710 case VariantShatranj:
5711 pieces = ShatranjArray;
5712 nrCastlingRights = 0;
5713 SetCharTable(pieceToChar, "PN.R.QB...Kpn.r.qb...k");
5716 pieces = makrukArray;
5717 nrCastlingRights = 0;
5718 startedFromSetupPosition = TRUE;
5719 SetCharTable(pieceToChar, "PN.R.M....SKpn.r.m....sk");
5721 case VariantTwoKings:
5722 pieces = twoKingsArray;
5724 case VariantCapaRandom:
5725 shuffleOpenings = TRUE;
5726 case VariantCapablanca:
5727 pieces = CapablancaArray;
5728 gameInfo.boardWidth = 10;
5729 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5732 pieces = GothicArray;
5733 gameInfo.boardWidth = 10;
5734 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5737 SetCharTable(pieceToChar, "PNBRQ..HEKpnbrq..hek");
5738 gameInfo.holdingsSize = 7;
5741 pieces = JanusArray;
5742 gameInfo.boardWidth = 10;
5743 SetCharTable(pieceToChar, "PNBRQ..JKpnbrq..jk");
5744 nrCastlingRights = 6;
5745 initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
5746 initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
5747 initialPosition[CASTLING][2] = initialRights[2] =(BOARD_WIDTH-1)>>1;
5748 initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
5749 initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
5750 initialPosition[CASTLING][5] = initialRights[5] =(BOARD_WIDTH-1)>>1;
5753 pieces = FalconArray;
5754 gameInfo.boardWidth = 10;
5755 SetCharTable(pieceToChar, "PNBRQ.............FKpnbrq.............fk");
5757 case VariantXiangqi:
5758 pieces = XiangqiArray;
5759 gameInfo.boardWidth = 9;
5760 gameInfo.boardHeight = 10;
5761 nrCastlingRights = 0;
5762 SetCharTable(pieceToChar, "PH.R.AE..K.C.ph.r.ae..k.c.");
5765 pieces = ShogiArray;
5766 gameInfo.boardWidth = 9;
5767 gameInfo.boardHeight = 9;
5768 gameInfo.holdingsSize = 7;
5769 nrCastlingRights = 0;
5770 SetCharTable(pieceToChar, "PNBRLS...G.++++++Kpnbrls...g.++++++k");
5772 case VariantCourier:
5773 pieces = CourierArray;
5774 gameInfo.boardWidth = 12;
5775 nrCastlingRights = 0;
5776 SetCharTable(pieceToChar, "PNBR.FE..WMKpnbr.fe..wmk");
5778 case VariantKnightmate:
5779 pieces = KnightmateArray;
5780 SetCharTable(pieceToChar, "P.BRQ.....M.........K.p.brq.....m.........k.");
5782 case VariantSpartan:
5783 pieces = SpartanArray;
5784 SetCharTable(pieceToChar, "PNBRQ................K......lwg.....c...h..k");
5787 pieces = fairyArray;
5788 SetCharTable(pieceToChar, "PNBRQFEACWMOHIJGDVLSUKpnbrqfeacwmohijgdvlsuk");
5791 pieces = GreatArray;
5792 gameInfo.boardWidth = 10;
5793 SetCharTable(pieceToChar, "PN....E...S..HWGMKpn....e...s..hwgmk");
5794 gameInfo.holdingsSize = 8;
5798 SetCharTable(pieceToChar, "PNBRQ..SE.......V.AKpnbrq..se.......v.ak");
5799 gameInfo.holdingsSize = 8;
5800 startedFromSetupPosition = TRUE;
5802 case VariantCrazyhouse:
5803 case VariantBughouse:
5805 SetCharTable(pieceToChar, "PNBRQ.......~~~~Kpnbrq.......~~~~k");
5806 gameInfo.holdingsSize = 5;
5808 case VariantWildCastle:
5810 /* !!?shuffle with kings guaranteed to be on d or e file */
5811 shuffleOpenings = 1;
5813 case VariantNoCastle:
5815 nrCastlingRights = 0;
5816 /* !!?unconstrained back-rank shuffle */
5817 shuffleOpenings = 1;
5822 if(appData.NrFiles >= 0) {
5823 if(gameInfo.boardWidth != appData.NrFiles) overrule++;
5824 gameInfo.boardWidth = appData.NrFiles;
5826 if(appData.NrRanks >= 0) {
5827 gameInfo.boardHeight = appData.NrRanks;
5829 if(appData.holdingsSize >= 0) {
5830 i = appData.holdingsSize;
5831 if(i > gameInfo.boardHeight) i = gameInfo.boardHeight;
5832 gameInfo.holdingsSize = i;
5834 if(gameInfo.holdingsSize) gameInfo.holdingsWidth = 2;
5835 if(BOARD_HEIGHT > BOARD_RANKS || BOARD_WIDTH > BOARD_FILES)
5836 DisplayFatalError(_("Recompile to support this BOARD_RANKS or BOARD_FILES!"), 0, 2);
5838 pawnRow = gameInfo.boardHeight - 7; /* seems to work in all common variants */
5839 if(pawnRow < 1) pawnRow = 1;
5840 if(gameInfo.variant == VariantMakruk) pawnRow = 2;
5842 /* User pieceToChar list overrules defaults */
5843 if(appData.pieceToCharTable != NULL)
5844 SetCharTable(pieceToChar, appData.pieceToCharTable);
5846 for( j=0; j<BOARD_WIDTH; j++ ) { ChessSquare s = EmptySquare;
5848 if(j==BOARD_LEFT-1 || j==BOARD_RGHT)
5849 s = (ChessSquare) 0; /* account holding counts in guard band */
5850 for( i=0; i<BOARD_HEIGHT; i++ )
5851 initialPosition[i][j] = s;
5853 if(j < BOARD_LEFT || j >= BOARD_RGHT || overrule) continue;
5854 initialPosition[0][j] = pieces[0][j-gameInfo.holdingsWidth];
5855 initialPosition[pawnRow][j] = WhitePawn;
5856 initialPosition[BOARD_HEIGHT-pawnRow-1][j] = gameInfo.variant == VariantSpartan ? BlackLance : BlackPawn;
5857 if(gameInfo.variant == VariantXiangqi) {
5859 initialPosition[pawnRow][j] =
5860 initialPosition[BOARD_HEIGHT-pawnRow-1][j] = EmptySquare;
5861 if(j==BOARD_LEFT+1 || j>=BOARD_RGHT-2) {
5862 initialPosition[2][j] = WhiteCannon;
5863 initialPosition[BOARD_HEIGHT-3][j] = BlackCannon;
5867 initialPosition[BOARD_HEIGHT-1][j] = pieces[1][j-gameInfo.holdingsWidth];
5869 if( (gameInfo.variant == VariantShogi) && !overrule ) {
5872 initialPosition[1][j] = WhiteBishop;
5873 initialPosition[BOARD_HEIGHT-2][j] = BlackRook;
5875 initialPosition[1][j] = WhiteRook;
5876 initialPosition[BOARD_HEIGHT-2][j] = BlackBishop;
5879 if( nrCastlingRights == -1) {
5880 /* [HGM] Build normal castling rights (must be done after board sizing!) */
5881 /* This sets default castling rights from none to normal corners */
5882 /* Variants with other castling rights must set them themselves above */
5883 nrCastlingRights = 6;
5885 initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
5886 initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
5887 initialPosition[CASTLING][2] = initialRights[2] = BOARD_WIDTH>>1;
5888 initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
5889 initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
5890 initialPosition[CASTLING][5] = initialRights[5] = BOARD_WIDTH>>1;
5893 if(gameInfo.variant == VariantSuper) Prelude(initialPosition);
5894 if(gameInfo.variant == VariantGreat) { // promotion commoners
5895 initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-1] = WhiteMan;
5896 initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-2] = 9;
5897 initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][0] = BlackMan;
5898 initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][1] = 9;
5900 if( gameInfo.variant == VariantSChess ) {
5901 initialPosition[1][0] = BlackMarshall;
5902 initialPosition[2][0] = BlackAngel;
5903 initialPosition[6][BOARD_WIDTH-1] = WhiteMarshall;
5904 initialPosition[5][BOARD_WIDTH-1] = WhiteAngel;
5905 initialPosition[1][1] = initialPosition[2][1] =
5906 initialPosition[6][BOARD_WIDTH-2] = initialPosition[5][BOARD_WIDTH-2] = 1;
5908 if (appData.debugMode) {
5909 fprintf(debugFP, "shuffleOpenings = %d\n", shuffleOpenings);
5911 if(shuffleOpenings) {
5912 SetUpShuffle(initialPosition, appData.defaultFrcPosition);
5913 startedFromSetupPosition = TRUE;
5915 if(startedFromPositionFile) {
5916 /* [HGM] loadPos: use PositionFile for every new game */
5917 CopyBoard(initialPosition, filePosition);
5918 for(i=0; i<nrCastlingRights; i++)
5919 initialRights[i] = filePosition[CASTLING][i];
5920 startedFromSetupPosition = TRUE;
5923 CopyBoard(boards[0], initialPosition);
5925 if(oldx != gameInfo.boardWidth ||
5926 oldy != gameInfo.boardHeight ||
5927 oldv != gameInfo.variant ||
5928 oldh != gameInfo.holdingsWidth
5930 InitDrawingSizes(-2 ,0);
5932 oldv = gameInfo.variant;
5934 DrawPosition(TRUE, boards[currentMove]);
5938 SendBoard(cps, moveNum)
5939 ChessProgramState *cps;
5942 char message[MSG_SIZ];
5944 if (cps->useSetboard) {
5945 char* fen = PositionToFEN(moveNum, cps->fenOverride);
5946 snprintf(message, MSG_SIZ,"setboard %s\n", fen);
5947 SendToProgram(message, cps);
5953 /* Kludge to set black to move, avoiding the troublesome and now
5954 * deprecated "black" command.
5956 if (!WhiteOnMove(moveNum)) // [HGM] but better a deprecated command than an illegal move...
5957 SendToProgram(boards[0][1][BOARD_LEFT] == WhitePawn ? "a2a3\n" : "black\n", cps);
5959 SendToProgram("edit\n", cps);
5960 SendToProgram("#\n", cps);
5961 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
5962 bp = &boards[moveNum][i][BOARD_LEFT];
5963 for (j = BOARD_LEFT; j < BOARD_RGHT; j++, bp++) {
5964 if ((int) *bp < (int) BlackPawn) {
5965 snprintf(message, MSG_SIZ, "%c%c%c\n", PieceToChar(*bp),
5967 if(message[0] == '+' || message[0] == '~') {
5968 snprintf(message, MSG_SIZ,"%c%c%c+\n",
5969 PieceToChar((ChessSquare)(DEMOTED *bp)),
5972 if(cps->alphaRank) { /* [HGM] shogi: translate coords */
5973 message[1] = BOARD_RGHT - 1 - j + '1';
5974 message[2] = BOARD_HEIGHT - 1 - i + 'a';
5976 SendToProgram(message, cps);
5981 SendToProgram("c\n", cps);
5982 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
5983 bp = &boards[moveNum][i][BOARD_LEFT];
5984 for (j = BOARD_LEFT; j < BOARD_RGHT; j++, bp++) {
5985 if (((int) *bp != (int) EmptySquare)
5986 && ((int) *bp >= (int) BlackPawn)) {
5987 snprintf(message,MSG_SIZ, "%c%c%c\n", ToUpper(PieceToChar(*bp)),
5989 if(message[0] == '+' || message[0] == '~') {
5990 snprintf(message, MSG_SIZ,"%c%c%c+\n",
5991 PieceToChar((ChessSquare)(DEMOTED *bp)),
5994 if(cps->alphaRank) { /* [HGM] shogi: translate coords */
5995 message[1] = BOARD_RGHT - 1 - j + '1';
5996 message[2] = BOARD_HEIGHT - 1 - i + 'a';
5998 SendToProgram(message, cps);
6003 SendToProgram(".\n", cps);
6005 setboardSpoiledMachineBlack = 0; /* [HGM] assume WB 4.2.7 already solves this after sending setboard */
6009 DefaultPromoChoice(int white)
6012 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
6013 result = WhiteFerz; // no choice
6014 else if(gameInfo.variant == VariantSuicide || gameInfo.variant == VariantGiveaway)
6015 result= WhiteKing; // in Suicide Q is the last thing we want
6016 else if(gameInfo.variant == VariantSpartan)
6017 result = white ? WhiteQueen : WhiteAngel;
6018 else result = WhiteQueen;
6019 if(!white) result = WHITE_TO_BLACK result;
6023 static int autoQueen; // [HGM] oneclick
6026 HasPromotionChoice(int fromX, int fromY, int toX, int toY, char *promoChoice)
6028 /* [HGM] rewritten IsPromotion to only flag promotions that offer a choice */
6029 /* [HGM] add Shogi promotions */
6030 int promotionZoneSize=1, highestPromotingPiece = (int)WhitePawn;
6035 if(fromX < BOARD_LEFT || fromX >= BOARD_RGHT) return FALSE; // drop
6036 if(toX < BOARD_LEFT || toX >= BOARD_RGHT) return FALSE; // move into holdings
6038 if(gameMode == EditPosition || gameInfo.variant == VariantXiangqi || // no promotions
6039 !(fromX >=0 && fromY >= 0 && toX >= 0 && toY >= 0) ) // invalid move
6042 piece = boards[currentMove][fromY][fromX];
6043 if(gameInfo.variant == VariantShogi) {
6044 promotionZoneSize = BOARD_HEIGHT/3;
6045 highestPromotingPiece = (int)WhiteFerz;
6046 } else if(gameInfo.variant == VariantMakruk) {
6047 promotionZoneSize = 3;
6050 // Treat Lance as Pawn when it is not representing Amazon
6051 if(gameInfo.variant != VariantSuper) {
6052 if(piece == WhiteLance) piece = WhitePawn; else
6053 if(piece == BlackLance) piece = BlackPawn;
6056 // next weed out all moves that do not touch the promotion zone at all
6057 if((int)piece >= BlackPawn) {
6058 if(toY >= promotionZoneSize && fromY >= promotionZoneSize)
6060 highestPromotingPiece = WHITE_TO_BLACK highestPromotingPiece;
6062 if( toY < BOARD_HEIGHT - promotionZoneSize &&
6063 fromY < BOARD_HEIGHT - promotionZoneSize) return FALSE;
6066 if( (int)piece > highestPromotingPiece ) return FALSE; // non-promoting piece
6068 // weed out mandatory Shogi promotions
6069 if(gameInfo.variant == VariantShogi) {
6070 if(piece >= BlackPawn) {
6071 if(toY == 0 && piece == BlackPawn ||
6072 toY == 0 && piece == BlackQueen ||
6073 toY <= 1 && piece == BlackKnight) {
6078 if(toY == BOARD_HEIGHT-1 && piece == WhitePawn ||
6079 toY == BOARD_HEIGHT-1 && piece == WhiteQueen ||
6080 toY >= BOARD_HEIGHT-2 && piece == WhiteKnight) {
6087 // weed out obviously illegal Pawn moves
6088 if(appData.testLegality && (piece == WhitePawn || piece == BlackPawn) ) {
6089 if(toX > fromX+1 || toX < fromX-1) return FALSE; // wide
6090 if(piece == WhitePawn && toY != fromY+1) return FALSE; // deep
6091 if(piece == BlackPawn && toY != fromY-1) return FALSE; // deep
6092 if(fromX != toX && gameInfo.variant == VariantShogi) return FALSE;
6093 // note we are not allowed to test for valid (non-)capture, due to premove
6096 // we either have a choice what to promote to, or (in Shogi) whether to promote
6097 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk) {
6098 *promoChoice = PieceToChar(BlackFerz); // no choice
6101 // no sense asking what we must promote to if it is going to explode...
6102 if(gameInfo.variant == VariantAtomic && boards[currentMove][toY][toX] != EmptySquare) {
6103 *promoChoice = PieceToChar(BlackQueen); // Queen as good as any
6106 // give caller the default choice even if we will not make it
6107 *promoChoice = ToLower(PieceToChar(defaultPromoChoice));
6108 if(gameInfo.variant == VariantShogi) *promoChoice = '+';
6109 if(appData.sweepSelect && gameInfo.variant != VariantGreat
6110 && gameInfo.variant != VariantShogi
6111 && gameInfo.variant != VariantSuper) return FALSE;
6112 if(autoQueen) return FALSE; // predetermined
6114 // suppress promotion popup on illegal moves that are not premoves
6115 premove = gameMode == IcsPlayingWhite && !WhiteOnMove(currentMove) ||
6116 gameMode == IcsPlayingBlack && WhiteOnMove(currentMove);
6117 if(appData.testLegality && !premove) {
6118 moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
6119 fromY, fromX, toY, toX, gameInfo.variant == VariantShogi ? '+' : NULLCHAR);
6120 if(moveType != WhitePromotion && moveType != BlackPromotion)
6128 InPalace(row, column)
6130 { /* [HGM] for Xiangqi */
6131 if( (row < 3 || row > BOARD_HEIGHT-4) &&
6132 column < (BOARD_WIDTH + 4)/2 &&
6133 column > (BOARD_WIDTH - 5)/2 ) return TRUE;
6138 PieceForSquare (x, y)
6142 if (x < 0 || x >= BOARD_WIDTH || y < 0 || y >= BOARD_HEIGHT)
6145 return boards[currentMove][y][x];
6149 OKToStartUserMove(x, y)
6152 ChessSquare from_piece;
6155 if (matchMode) return FALSE;
6156 if (gameMode == EditPosition) return TRUE;
6158 if (x >= 0 && y >= 0)
6159 from_piece = boards[currentMove][y][x];
6161 from_piece = EmptySquare;
6163 if (from_piece == EmptySquare) return FALSE;
6165 white_piece = (int)from_piece >= (int)WhitePawn &&
6166 (int)from_piece < (int)BlackPawn; /* [HGM] can be > King! */
6169 case PlayFromGameFile:
6171 case TwoMachinesPlay:
6179 case MachinePlaysWhite:
6180 case IcsPlayingBlack:
6181 if (appData.zippyPlay) return FALSE;
6183 DisplayMoveError(_("You are playing Black"));
6188 case MachinePlaysBlack:
6189 case IcsPlayingWhite:
6190 if (appData.zippyPlay) return FALSE;
6192 DisplayMoveError(_("You are playing White"));
6198 if (!white_piece && WhiteOnMove(currentMove)) {
6199 DisplayMoveError(_("It is White's turn"));
6202 if (white_piece && !WhiteOnMove(currentMove)) {
6203 DisplayMoveError(_("It is Black's turn"));
6206 if (cmailMsgLoaded && (currentMove < cmailOldMove)) {
6207 /* Editing correspondence game history */
6208 /* Could disallow this or prompt for confirmation */
6213 case BeginningOfGame:
6214 if (appData.icsActive) return FALSE;
6215 if (!appData.noChessProgram) {
6217 DisplayMoveError(_("You are playing White"));
6224 if (!white_piece && WhiteOnMove(currentMove)) {
6225 DisplayMoveError(_("It is White's turn"));
6228 if (white_piece && !WhiteOnMove(currentMove)) {
6229 DisplayMoveError(_("It is Black's turn"));
6238 if (currentMove != forwardMostMove && gameMode != AnalyzeMode
6239 && gameMode != EditGame // [HGM] vari: treat as AnalyzeMode
6240 && gameMode != AnalyzeFile && gameMode != Training) {
6241 DisplayMoveError(_("Displayed position is not current"));
6248 OnlyMove(int *x, int *y, Boolean captures) {
6249 DisambiguateClosure cl;
6250 if (appData.zippyPlay) return FALSE;
6252 case MachinePlaysBlack:
6253 case IcsPlayingWhite:
6254 case BeginningOfGame:
6255 if(!WhiteOnMove(currentMove)) return FALSE;
6257 case MachinePlaysWhite:
6258 case IcsPlayingBlack:
6259 if(WhiteOnMove(currentMove)) return FALSE;
6266 cl.pieceIn = EmptySquare;
6271 cl.promoCharIn = NULLCHAR;
6272 Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
6273 if( cl.kind == NormalMove ||
6274 cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
6275 cl.kind == WhitePromotion || cl.kind == BlackPromotion ||
6276 cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
6283 if(cl.kind != ImpossibleMove) return FALSE;
6284 cl.pieceIn = EmptySquare;
6289 cl.promoCharIn = NULLCHAR;
6290 Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
6291 if( cl.kind == NormalMove ||
6292 cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
6293 cl.kind == WhitePromotion || cl.kind == BlackPromotion ||
6294 cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
6299 autoQueen = TRUE; // act as if autoQueen on when we click to-square
6305 FILE *lastLoadGameFP = NULL, *lastLoadPositionFP = NULL;
6306 int lastLoadGameNumber = 0, lastLoadPositionNumber = 0;
6307 int lastLoadGameUseList = FALSE;
6308 char lastLoadGameTitle[MSG_SIZ], lastLoadPositionTitle[MSG_SIZ];
6309 ChessMove lastLoadGameStart = EndOfFile;
6312 UserMoveEvent(fromX, fromY, toX, toY, promoChar)
6313 int fromX, fromY, toX, toY;
6317 ChessSquare pdown, pup;
6319 /* Check if the user is playing in turn. This is complicated because we
6320 let the user "pick up" a piece before it is his turn. So the piece he
6321 tried to pick up may have been captured by the time he puts it down!
6322 Therefore we use the color the user is supposed to be playing in this
6323 test, not the color of the piece that is currently on the starting
6324 square---except in EditGame mode, where the user is playing both
6325 sides; fortunately there the capture race can't happen. (It can
6326 now happen in IcsExamining mode, but that's just too bad. The user
6327 will get a somewhat confusing message in that case.)
6331 case PlayFromGameFile:
6333 case TwoMachinesPlay:
6337 /* We switched into a game mode where moves are not accepted,
6338 perhaps while the mouse button was down. */
6341 case MachinePlaysWhite:
6342 /* User is moving for Black */
6343 if (WhiteOnMove(currentMove)) {
6344 DisplayMoveError(_("It is White's turn"));
6349 case MachinePlaysBlack:
6350 /* User is moving for White */
6351 if (!WhiteOnMove(currentMove)) {
6352 DisplayMoveError(_("It is Black's turn"));
6359 case BeginningOfGame:
6362 if(fromY == DROP_RANK) break; // [HGM] drop moves (entered through move type-in) are automatically assigned to side-to-move
6363 if ((int) boards[currentMove][fromY][fromX] >= (int) BlackPawn &&
6364 (int) boards[currentMove][fromY][fromX] < (int) EmptySquare) {
6365 /* User is moving for Black */
6366 if (WhiteOnMove(currentMove)) {
6367 DisplayMoveError(_("It is White's turn"));
6371 /* User is moving for White */
6372 if (!WhiteOnMove(currentMove)) {
6373 DisplayMoveError(_("It is Black's turn"));
6379 case IcsPlayingBlack:
6380 /* User is moving for Black */
6381 if (WhiteOnMove(currentMove)) {
6382 if (!appData.premove) {
6383 DisplayMoveError(_("It is White's turn"));
6384 } else if (toX >= 0 && toY >= 0) {
6387 premoveFromX = fromX;
6388 premoveFromY = fromY;
6389 premovePromoChar = promoChar;
6391 if (appData.debugMode)
6392 fprintf(debugFP, "Got premove: fromX %d,"
6393 "fromY %d, toX %d, toY %d\n",
6394 fromX, fromY, toX, toY);
6400 case IcsPlayingWhite:
6401 /* User is moving for White */
6402 if (!WhiteOnMove(currentMove)) {
6403 if (!appData.premove) {
6404 DisplayMoveError(_("It is Black's turn"));
6405 } else if (toX >= 0 && toY >= 0) {
6408 premoveFromX = fromX;
6409 premoveFromY = fromY;
6410 premovePromoChar = promoChar;
6412 if (appData.debugMode)
6413 fprintf(debugFP, "Got premove: fromX %d,"
6414 "fromY %d, toX %d, toY %d\n",
6415 fromX, fromY, toX, toY);
6425 /* EditPosition, empty square, or different color piece;
6426 click-click move is possible */
6427 if (toX == -2 || toY == -2) {
6428 boards[0][fromY][fromX] = EmptySquare;
6429 DrawPosition(FALSE, boards[currentMove]);
6431 } else if (toX >= 0 && toY >= 0) {
6432 boards[0][toY][toX] = boards[0][fromY][fromX];
6433 if(fromX == BOARD_LEFT-2) { // handle 'moves' out of holdings
6434 if(boards[0][fromY][0] != EmptySquare) {
6435 if(boards[0][fromY][1]) boards[0][fromY][1]--;
6436 if(boards[0][fromY][1] == 0) boards[0][fromY][0] = EmptySquare;
6439 if(fromX == BOARD_RGHT+1) {
6440 if(boards[0][fromY][BOARD_WIDTH-1] != EmptySquare) {
6441 if(boards[0][fromY][BOARD_WIDTH-2]) boards[0][fromY][BOARD_WIDTH-2]--;
6442 if(boards[0][fromY][BOARD_WIDTH-2] == 0) boards[0][fromY][BOARD_WIDTH-1] = EmptySquare;
6445 boards[0][fromY][fromX] = EmptySquare;
6446 DrawPosition(FALSE, boards[currentMove]);
6452 if(toX < 0 || toY < 0) return;
6453 pdown = boards[currentMove][fromY][fromX];
6454 pup = boards[currentMove][toY][toX];
6456 /* [HGM] If move started in holdings, it means a drop. Convert to standard form */
6457 if( (fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) && fromY != DROP_RANK ) {
6458 if( pup != EmptySquare ) return;
6459 moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
6460 if(appData.debugMode) fprintf(debugFP, "Drop move %d, curr=%d, x=%d,y=%d, p=%d\n",
6461 moveType, currentMove, fromX, fromY, boards[currentMove][fromY][fromX]);
6462 // holdings might not be sent yet in ICS play; we have to figure out which piece belongs here
6463 if(fromX == 0) fromY = BOARD_HEIGHT-1 - fromY; // black holdings upside-down
6464 fromX = fromX ? WhitePawn : BlackPawn; // first piece type in selected holdings
6465 while(PieceToChar(fromX) == '.' || PieceToNumber(fromX) != fromY && fromX != (int) EmptySquare) fromX++;
6469 /* [HGM] always test for legality, to get promotion info */
6470 moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
6471 fromY, fromX, toY, toX, promoChar);
6472 /* [HGM] but possibly ignore an IllegalMove result */
6473 if (appData.testLegality) {
6474 if (moveType == IllegalMove || moveType == ImpossibleMove) {
6475 DisplayMoveError(_("Illegal move"));
6480 FinishMove(moveType, fromX, fromY, toX, toY, promoChar);
6483 /* Common tail of UserMoveEvent and DropMenuEvent */
6485 FinishMove(moveType, fromX, fromY, toX, toY, promoChar)
6487 int fromX, fromY, toX, toY;
6488 /*char*/int promoChar;
6492 if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) && promoChar != NULLCHAR) {
6493 // [HGM] superchess: suppress promotions to non-available piece
6494 int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
6495 if(WhiteOnMove(currentMove)) {
6496 if(!boards[currentMove][k][BOARD_WIDTH-2]) return 0;
6498 if(!boards[currentMove][BOARD_HEIGHT-1-k][1]) return 0;
6502 /* [HGM] <popupFix> kludge to avoid having to know the exact promotion
6503 move type in caller when we know the move is a legal promotion */
6504 if(moveType == NormalMove && promoChar)
6505 moveType = WhiteOnMove(currentMove) ? WhitePromotion : BlackPromotion;
6507 /* [HGM] <popupFix> The following if has been moved here from
6508 UserMoveEvent(). Because it seemed to belong here (why not allow
6509 piece drops in training games?), and because it can only be
6510 performed after it is known to what we promote. */
6511 if (gameMode == Training) {
6512 /* compare the move played on the board to the next move in the
6513 * game. If they match, display the move and the opponent's response.
6514 * If they don't match, display an error message.
6518 CopyBoard(testBoard, boards[currentMove]);
6519 ApplyMove(fromX, fromY, toX, toY, promoChar, testBoard);
6521 if (CompareBoards(testBoard, boards[currentMove+1])) {
6522 ForwardInner(currentMove+1);
6524 /* Autoplay the opponent's response.
6525 * if appData.animate was TRUE when Training mode was entered,
6526 * the response will be animated.
6528 saveAnimate = appData.animate;
6529 appData.animate = animateTraining;
6530 ForwardInner(currentMove+1);
6531 appData.animate = saveAnimate;
6533 /* check for the end of the game */
6534 if (currentMove >= forwardMostMove) {
6535 gameMode = PlayFromGameFile;
6537 SetTrainingModeOff();
6538 DisplayInformation(_("End of game"));
6541 DisplayError(_("Incorrect move"), 0);
6546 /* Ok, now we know that the move is good, so we can kill
6547 the previous line in Analysis Mode */
6548 if ((gameMode == AnalyzeMode || gameMode == EditGame)
6549 && currentMove < forwardMostMove) {
6550 if(appData.variations && shiftKey) PushTail(currentMove, forwardMostMove); // [HGM] vari: save tail of game
6551 else forwardMostMove = currentMove;
6554 /* If we need the chess program but it's dead, restart it */
6555 ResurrectChessProgram();
6557 /* A user move restarts a paused game*/
6561 thinkOutput[0] = NULLCHAR;
6563 MakeMove(fromX, fromY, toX, toY, promoChar); /*updates forwardMostMove*/
6565 if(Adjudicate(NULL)) { // [HGM] adjudicate: take care of automatic game end
6566 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6570 if (gameMode == BeginningOfGame) {
6571 if (appData.noChessProgram) {
6572 gameMode = EditGame;
6576 gameMode = MachinePlaysBlack;
6579 snprintf(buf, MSG_SIZ, "%s vs. %s", gameInfo.white, gameInfo.black);
6581 if (first.sendName) {
6582 snprintf(buf, MSG_SIZ,"name %s\n", gameInfo.white);
6583 SendToProgram(buf, &first);
6590 /* Relay move to ICS or chess engine */
6591 if (appData.icsActive) {
6592 if (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
6593 gameMode == IcsExamining) {
6594 if(userOfferedDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
6595 SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
6597 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
6599 // also send plain move, in case ICS does not understand atomic claims
6600 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
6604 if (first.sendTime && (gameMode == BeginningOfGame ||
6605 gameMode == MachinePlaysWhite ||
6606 gameMode == MachinePlaysBlack)) {
6607 SendTimeRemaining(&first, gameMode != MachinePlaysBlack);
6609 if (gameMode != EditGame && gameMode != PlayFromGameFile) {
6610 // [HGM] book: if program might be playing, let it use book
6611 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, FALSE);
6612 first.maybeThinking = TRUE;
6613 } else SendMoveToProgram(forwardMostMove-1, &first);
6614 if (currentMove == cmailOldMove + 1) {
6615 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
6619 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6623 if(appData.testLegality)
6624 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
6630 if (WhiteOnMove(currentMove)) {
6631 GameEnds(BlackWins, "Black mates", GE_PLAYER);
6633 GameEnds(WhiteWins, "White mates", GE_PLAYER);
6637 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
6642 case MachinePlaysBlack:
6643 case MachinePlaysWhite:
6644 /* disable certain menu options while machine is thinking */
6645 SetMachineThinkingEnables();
6652 userOfferedDraw = FALSE; // [HGM] drawclaim: after move made, and tested for claimable draw
6653 promoDefaultAltered = FALSE; // [HGM] fall back on default choice
6655 if(bookHit) { // [HGM] book: simulate book reply
6656 static char bookMove[MSG_SIZ]; // a bit generous?
6658 programStats.nodes = programStats.depth = programStats.time =
6659 programStats.score = programStats.got_only_move = 0;
6660 sprintf(programStats.movelist, "%s (xbook)", bookHit);
6662 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
6663 strcat(bookMove, bookHit);
6664 HandleMachineMove(bookMove, &first);
6670 Mark(board, flags, kind, rf, ff, rt, ft, closure)
6677 typedef char Markers[BOARD_RANKS][BOARD_FILES];
6678 Markers *m = (Markers *) closure;
6679 if(rf == fromY && ff == fromX)
6680 (*m)[rt][ft] = 1 + (board[rt][ft] != EmptySquare
6681 || kind == WhiteCapturesEnPassant
6682 || kind == BlackCapturesEnPassant);
6683 else if(flags & F_MANDATORY_CAPTURE && board[rt][ft] != EmptySquare) (*m)[rt][ft] = 3;
6687 MarkTargetSquares(int clear)
6690 if(!appData.markers || !appData.highlightDragging ||
6691 !appData.testLegality || gameMode == EditPosition) return;
6693 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) marker[y][x] = 0;
6696 GenLegal(boards[currentMove], PosFlags(currentMove), Mark, (void*) marker);
6697 if(PosFlags(0) & F_MANDATORY_CAPTURE) {
6698 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x]>1) capt++;
6700 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x] == 1) marker[y][x] = 0;
6703 DrawPosition(TRUE, NULL);
6707 Explode(Board board, int fromX, int fromY, int toX, int toY)
6709 if(gameInfo.variant == VariantAtomic &&
6710 (board[toY][toX] != EmptySquare || // capture?
6711 toX != fromX && (board[fromY][fromX] == WhitePawn || // e.p. ?
6712 board[fromY][fromX] == BlackPawn )
6714 AnimateAtomicCapture(board, fromX, fromY, toX, toY);
6720 ChessSquare gatingPiece = EmptySquare; // exported to front-end, for dragging
6722 int CanPromote(ChessSquare piece, int y)
6724 if(gameMode == EditPosition) return FALSE; // no promotions when editing position
6725 // some variants have fixed promotion piece, no promotion at all, or another selection mechanism
6726 if(gameInfo.variant == VariantShogi || gameInfo.variant == VariantXiangqi ||
6727 gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat ||
6728 gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier ||
6729 gameInfo.variant == VariantMakruk) return FALSE;
6730 return (piece == BlackPawn && y == 1 ||
6731 piece == WhitePawn && y == BOARD_HEIGHT-2 ||
6732 piece == BlackLance && y == 1 ||
6733 piece == WhiteLance && y == BOARD_HEIGHT-2 );
6736 void LeftClick(ClickType clickType, int xPix, int yPix)
6739 Boolean saveAnimate;
6740 static int second = 0, promotionChoice = 0, clearFlag = 0;
6741 char promoChoice = NULLCHAR;
6744 if(appData.seekGraph && appData.icsActive && loggedOn &&
6745 (gameMode == BeginningOfGame || gameMode == IcsIdle)) {
6746 SeekGraphClick(clickType, xPix, yPix, 0);
6750 if (clickType == Press) ErrorPopDown();
6751 MarkTargetSquares(1);
6753 x = EventToSquare(xPix, BOARD_WIDTH);
6754 y = EventToSquare(yPix, BOARD_HEIGHT);
6755 if (!flipView && y >= 0) {
6756 y = BOARD_HEIGHT - 1 - y;
6758 if (flipView && x >= 0) {
6759 x = BOARD_WIDTH - 1 - x;
6762 if(promoSweep != EmptySquare) { // up-click during sweep-select of promo-piece
6763 defaultPromoChoice = promoSweep;
6764 promoSweep = EmptySquare; // terminate sweep
6765 promoDefaultAltered = TRUE;
6766 if(!selectFlag) x = fromX, y = fromY; // and fake up-click on same square if we were still selecting
6769 if(promotionChoice) { // we are waiting for a click to indicate promotion piece
6770 if(clickType == Release) return; // ignore upclick of click-click destination
6771 promotionChoice = FALSE; // only one chance: if click not OK it is interpreted as cancel
6772 if(appData.debugMode) fprintf(debugFP, "promotion click, x=%d, y=%d\n", x, y);
6773 if(gameInfo.holdingsWidth &&
6774 (WhiteOnMove(currentMove)
6775 ? x == BOARD_WIDTH-1 && y < gameInfo.holdingsSize && y > 0
6776 : x == 0 && y >= BOARD_HEIGHT - gameInfo.holdingsSize && y < BOARD_HEIGHT-1) ) {
6777 // click in right holdings, for determining promotion piece
6778 ChessSquare p = boards[currentMove][y][x];
6779 if(appData.debugMode) fprintf(debugFP, "square contains %d\n", (int)p);
6780 if(p != EmptySquare) {
6781 FinishMove(NormalMove, fromX, fromY, toX, toY, ToLower(PieceToChar(p)));
6786 DrawPosition(FALSE, boards[currentMove]);
6790 /* [HGM] holdings: next 5 lines: ignore all clicks between board and holdings */
6791 if(clickType == Press
6792 && ( x == BOARD_LEFT-1 || x == BOARD_RGHT
6793 || x == BOARD_LEFT-2 && y < BOARD_HEIGHT-gameInfo.holdingsSize
6794 || x == BOARD_RGHT+1 && y >= gameInfo.holdingsSize) )
6797 if(clickType == Press && fromX == x && fromY == y && promoDefaultAltered)
6798 fromX = fromY = -1; // second click on piece after altering default promo piece treated as first click
6800 if(!promoDefaultAltered) { // determine default promotion piece, based on the side the user is moving for
6801 int side = (gameMode == IcsPlayingWhite || gameMode == MachinePlaysBlack ||
6802 gameMode != MachinePlaysWhite && gameMode != IcsPlayingBlack && WhiteOnMove(currentMove));
6803 defaultPromoChoice = DefaultPromoChoice(side);
6806 autoQueen = appData.alwaysPromoteToQueen;
6810 gatingPiece = EmptySquare;
6811 if (clickType != Press) {
6812 if(dragging) { // [HGM] from-square must have been reset due to game end since last press
6813 DragPieceEnd(xPix, yPix); dragging = 0;
6814 DrawPosition(FALSE, NULL);
6818 fromX = x; fromY = y;
6819 if(!appData.oneClick || !OnlyMove(&x, &y, FALSE) ||
6820 // even if only move, we treat as normal when this would trigger a promotion popup, to allow sweep selection
6821 appData.sweepSelect && CanPromote(boards[currentMove][fromY][fromX], fromY) && originalY != y) {
6823 if (OKToStartUserMove(fromX, fromY)) {
6825 MarkTargetSquares(0);
6826 DragPieceBegin(xPix, yPix); dragging = 1;
6827 if(appData.sweepSelect && CanPromote(piece = boards[currentMove][fromY][fromX], fromY)) {
6828 promoSweep = defaultPromoChoice;
6829 selectFlag = 0; lastX = xPix; lastY = yPix;
6830 Sweep(0); // Pawn that is going to promote: preview promotion piece
6831 DisplayMessage("", _("Pull pawn backwards to under-promote"));
6833 if (appData.highlightDragging) {
6834 SetHighlights(fromX, fromY, -1, -1);
6836 } else fromX = fromY = -1;
6842 if (clickType == Press && gameMode != EditPosition) {
6847 // ignore off-board to clicks
6848 if(y < 0 || x < 0) return;
6850 /* Check if clicking again on the same color piece */
6851 fromP = boards[currentMove][fromY][fromX];
6852 toP = boards[currentMove][y][x];
6853 frc = gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom || gameInfo.variant == VariantSChess;
6854 if ((WhitePawn <= fromP && fromP <= WhiteKing &&
6855 WhitePawn <= toP && toP <= WhiteKing &&
6856 !(fromP == WhiteKing && toP == WhiteRook && frc) &&
6857 !(fromP == WhiteRook && toP == WhiteKing && frc)) ||
6858 (BlackPawn <= fromP && fromP <= BlackKing &&
6859 BlackPawn <= toP && toP <= BlackKing &&
6860 !(fromP == BlackRook && toP == BlackKing && frc) && // allow also RxK as FRC castling
6861 !(fromP == BlackKing && toP == BlackRook && frc))) {
6862 /* Clicked again on same color piece -- changed his mind */
6863 second = (x == fromX && y == fromY);
6864 promoDefaultAltered = FALSE;
6865 if(!second || appData.oneClick && !OnlyMove(&x, &y, TRUE)) {
6866 if (appData.highlightDragging) {
6867 SetHighlights(x, y, -1, -1);
6871 if (OKToStartUserMove(x, y)) {
6872 if(gameInfo.variant == VariantSChess && // S-Chess: back-rank piece selected after holdings means gating
6873 (fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) &&
6874 y == (toP < BlackPawn ? 0 : BOARD_HEIGHT-1))
6875 gatingPiece = boards[currentMove][fromY][fromX];
6876 else gatingPiece = EmptySquare;
6878 fromY = y; dragging = 1;
6879 MarkTargetSquares(0);
6880 DragPieceBegin(xPix, yPix);
6881 if(appData.sweepSelect && CanPromote(piece = boards[currentMove][y][x], y)) {
6882 promoSweep = defaultPromoChoice;
6883 selectFlag = 0; lastX = xPix; lastY = yPix;
6884 Sweep(0); // Pawn that is going to promote: preview promotion piece
6888 if(x == fromX && y == fromY) return; // if OnlyMove altered (x,y) we go on
6891 // ignore clicks on holdings
6892 if(x < BOARD_LEFT || x >= BOARD_RGHT) return;
6895 if (clickType == Release && x == fromX && y == fromY) {
6896 DragPieceEnd(xPix, yPix); dragging = 0;
6898 // a deferred attempt to click-click move an empty square on top of a piece
6899 boards[currentMove][y][x] = EmptySquare;
6901 DrawPosition(FALSE, boards[currentMove]);
6902 fromX = fromY = -1; clearFlag = 0;
6905 if (appData.animateDragging) {
6906 /* Undo animation damage if any */
6907 DrawPosition(FALSE, NULL);
6910 /* Second up/down in same square; just abort move */
6913 gatingPiece = EmptySquare;
6916 ClearPremoveHighlights();
6918 /* First upclick in same square; start click-click mode */
6919 SetHighlights(x, y, -1, -1);
6926 /* we now have a different from- and (possibly off-board) to-square */
6927 /* Completed move */
6930 saveAnimate = appData.animate;
6931 if (clickType == Press) {
6932 if(gameMode == EditPosition && boards[currentMove][fromY][fromX] == EmptySquare) {
6933 // must be Edit Position mode with empty-square selected
6934 fromX = x; fromY = y; DragPieceBegin(xPix, yPix); dragging = 1; // consider this a new attempt to drag
6935 if(x >= BOARD_LEFT && x < BOARD_RGHT) clearFlag = 1; // and defer click-click move of empty-square to up-click
6938 /* Finish clickclick move */
6939 if (appData.animate || appData.highlightLastMove) {
6940 SetHighlights(fromX, fromY, toX, toY);
6945 /* Finish drag move */
6946 if (appData.highlightLastMove) {
6947 SetHighlights(fromX, fromY, toX, toY);
6951 DragPieceEnd(xPix, yPix); dragging = 0;
6952 /* Don't animate move and drag both */
6953 appData.animate = FALSE;
6956 // moves into holding are invalid for now (except in EditPosition, adapting to-square)
6957 if(x >= 0 && x < BOARD_LEFT || x >= BOARD_RGHT) {
6958 ChessSquare piece = boards[currentMove][fromY][fromX];
6959 if(gameMode == EditPosition && piece != EmptySquare &&
6960 fromX >= BOARD_LEFT && fromX < BOARD_RGHT) {
6963 if(x == BOARD_LEFT-2 && piece >= BlackPawn) {
6964 n = PieceToNumber(piece - (int)BlackPawn);
6965 if(n >= gameInfo.holdingsSize) { n = 0; piece = BlackPawn; }
6966 boards[currentMove][BOARD_HEIGHT-1 - n][0] = piece;
6967 boards[currentMove][BOARD_HEIGHT-1 - n][1]++;
6969 if(x == BOARD_RGHT+1 && piece < BlackPawn) {
6970 n = PieceToNumber(piece);
6971 if(n >= gameInfo.holdingsSize) { n = 0; piece = WhitePawn; }
6972 boards[currentMove][n][BOARD_WIDTH-1] = piece;
6973 boards[currentMove][n][BOARD_WIDTH-2]++;
6975 boards[currentMove][fromY][fromX] = EmptySquare;
6979 DrawPosition(TRUE, boards[currentMove]);
6983 // off-board moves should not be highlighted
6984 if(x < 0 || y < 0) ClearHighlights();
6986 if(gatingPiece != EmptySquare) promoChoice = ToLower(PieceToChar(gatingPiece));
6988 if (HasPromotionChoice(fromX, fromY, toX, toY, &promoChoice)) {
6989 SetHighlights(fromX, fromY, toX, toY);
6990 if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) {
6991 // [HGM] super: promotion to captured piece selected from holdings
6992 ChessSquare p = boards[currentMove][fromY][fromX], q = boards[currentMove][toY][toX];
6993 promotionChoice = TRUE;
6994 // kludge follows to temporarily execute move on display, without promoting yet
6995 boards[currentMove][fromY][fromX] = EmptySquare; // move Pawn to 8th rank
6996 boards[currentMove][toY][toX] = p;
6997 DrawPosition(FALSE, boards[currentMove]);
6998 boards[currentMove][fromY][fromX] = p; // take back, but display stays
6999 boards[currentMove][toY][toX] = q;
7000 DisplayMessage("Click in holdings to choose piece", "");
7005 int oldMove = currentMove;
7006 UserMoveEvent(fromX, fromY, toX, toY, promoChoice);
7007 if (!appData.highlightLastMove || gotPremove) ClearHighlights();
7008 if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);
7009 if(saveAnimate && !appData.animate && currentMove != oldMove && // drag-move was performed
7010 Explode(boards[currentMove-1], fromX, fromY, toX, toY))
7011 DrawPosition(TRUE, boards[currentMove]);
7014 appData.animate = saveAnimate;
7015 if (appData.animate || appData.animateDragging) {
7016 /* Undo animation damage if needed */
7017 DrawPosition(FALSE, NULL);
7021 int RightClick(ClickType action, int x, int y, int *fromX, int *fromY)
7022 { // front-end-free part taken out of PieceMenuPopup
7023 int whichMenu; int xSqr, ySqr;
7025 if(seekGraphUp) { // [HGM] seekgraph
7026 if(action == Press) SeekGraphClick(Press, x, y, 2); // 2 indicates right-click: no pop-down on miss
7027 if(action == Release) SeekGraphClick(Release, x, y, 2); // and no challenge on hit
7031 if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)
7032 && !appData.zippyPlay && appData.bgObserve) { // [HGM] bughouse: show background game
7033 if(!partnerBoardValid) return -2; // suppress display of uninitialized boards
7034 if( appData.dualBoard) return -2; // [HGM] dual: is already displayed
7035 if(action == Press) {
7036 originalFlip = flipView;
7037 flipView = !flipView; // temporarily flip board to see game from partners perspective
7038 DrawPosition(TRUE, partnerBoard);
7039 DisplayMessage(partnerStatus, "");
7041 } else if(action == Release) {
7042 flipView = originalFlip;
7043 DrawPosition(TRUE, boards[currentMove]);
7049 xSqr = EventToSquare(x, BOARD_WIDTH);
7050 ySqr = EventToSquare(y, BOARD_HEIGHT);
7051 if (action == Release) {
7052 if(pieceSweep != EmptySquare) {
7053 EditPositionMenuEvent(pieceSweep, toX, toY);
7054 pieceSweep = EmptySquare;
7055 } else UnLoadPV(); // [HGM] pv
7057 if (action != Press) return -2; // return code to be ignored
7060 if(xSqr < BOARD_LEFT || xSqr >= BOARD_RGHT) return -1;
\r
7062 if (xSqr == BOARD_LEFT-1 || xSqr == BOARD_RGHT) return -1;
\r
7063 if (xSqr < 0 || ySqr < 0) return -1;
7064 if(appData.pieceMenu) { whichMenu = 0; break; } // edit-position menu
7065 pieceSweep = shiftKey ? BlackPawn : WhitePawn; // [HGM] sweep: prepare selecting piece by mouse sweep
7066 toX = xSqr; toY = ySqr; lastX = x, lastY = y;
7067 if(flipView) toX = BOARD_WIDTH - 1 - toX; else toY = BOARD_HEIGHT - 1 - toY;
7071 if(!appData.icsEngineAnalyze) return -1;
7072 case IcsPlayingWhite:
7073 case IcsPlayingBlack:
7074 if(!appData.zippyPlay) goto noZip;
7077 case MachinePlaysWhite:
7078 case MachinePlaysBlack:
7079 case TwoMachinesPlay: // [HGM] pv: use for showing PV
7080 if (!appData.dropMenu) {
7082 return 2; // flag front-end to grab mouse events
7084 if(gameMode == TwoMachinesPlay || gameMode == AnalyzeMode ||
7085 gameMode == AnalyzeFile || gameMode == IcsObserving) return -1;
7088 if (xSqr < 0 || ySqr < 0) return -1;
7089 if (!appData.dropMenu || appData.testLegality &&
7090 gameInfo.variant != VariantBughouse &&
7091 gameInfo.variant != VariantCrazyhouse) return -1;
7092 whichMenu = 1; // drop menu
7098 if (((*fromX = xSqr) < 0) ||
7099 ((*fromY = ySqr) < 0)) {
7100 *fromX = *fromY = -1;
7104 *fromX = BOARD_WIDTH - 1 - *fromX;
7106 *fromY = BOARD_HEIGHT - 1 - *fromY;
7111 void SendProgramStatsToFrontend( ChessProgramState * cps, ChessProgramStats * cpstats )
7113 // char * hint = lastHint;
7114 FrontEndProgramStats stats;
7116 stats.which = cps == &first ? 0 : 1;
7117 stats.depth = cpstats->depth;
7118 stats.nodes = cpstats->nodes;
7119 stats.score = cpstats->score;
7120 stats.time = cpstats->time;
7121 stats.pv = cpstats->movelist;
7122 stats.hint = lastHint;
7123 stats.an_move_index = 0;
7124 stats.an_move_count = 0;
7126 if( gameMode == AnalyzeMode || gameMode == AnalyzeFile ) {
7127 stats.hint = cpstats->move_name;
7128 stats.an_move_index = cpstats->nr_moves - cpstats->moves_left;
7129 stats.an_move_count = cpstats->nr_moves;
7132 if(stats.pv && stats.pv[0]) safeStrCpy(lastPV[stats.which], stats.pv, sizeof(lastPV[stats.which])/sizeof(lastPV[stats.which][0])); // [HGM] pv: remember last PV of each
7134 SetProgramStats( &stats );
7137 #define MAXPLAYERS 500
7140 TourneyStandings(int display)
7142 int i, w, b, color, wScore, bScore, dummy, nr=0, nPlayers=0;
7143 int score[MAXPLAYERS], ranking[MAXPLAYERS], points[MAXPLAYERS], games[MAXPLAYERS];
7144 char result, *p, *names[MAXPLAYERS];
7146 if(appData.tourneyType < 0) return strdup("Swiss tourney finished"); // standings of Swiss yet TODO
7148 names[0] = p = strdup(appData.participants);
7149 while(p = strchr(p, '\n')) *p++ = NULLCHAR, names[++nPlayers] = p; // count participants
7151 for(i=0; i<nPlayers; i++) score[i] = games[i] = 0;
7153 while(result = appData.results[nr]) {
7154 color = Pairing(nr, nPlayers, &w, &b, &dummy);
7155 if(!(color ^ matchGame & 1)) { dummy = w; w = b; b = dummy; }
7156 wScore = bScore = 0;
7158 case '+': wScore = 2; break;
7159 case '-': bScore = 2; break;
7160 case '=': wScore = bScore = 1; break;
7162 case '*': return strdup("busy"); // tourney not finished
7170 if(appData.tourneyType > 0) nPlayers = appData.tourneyType; // in gauntlet, list only gauntlet engine(s)
7171 for(w=0; w<nPlayers; w++) {
7173 for(i=0; i<nPlayers; i++) if(score[i] > bScore) bScore = score[i], b = i;
7174 ranking[w] = b; points[w] = bScore; score[b] = -2;
7176 p = malloc(nPlayers*34+1);
7177 for(w=0; w<nPlayers && w<display; w++)
7178 sprintf(p+34*w, "%2d. %5.1f/%-3d %-19.19s\n", w+1, points[w]/2., games[ranking[w]], names[ranking[w]]);
7184 Count(Board board, int pCnt[], int *nW, int *nB, int *wStale, int *bStale, int *bishopColor)
7185 { // count all piece types
7187 *nB = *nW = *wStale = *bStale = *bishopColor = 0;
7188 for(p=WhitePawn; p<=EmptySquare; p++) pCnt[p] = 0;
7189 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
7192 if(p == WhitePawn && r == BOARD_HEIGHT-1) (*wStale)++; else
7193 if(p == BlackPawn && r == 0) (*bStale)++; // count last-Rank Pawns (XQ) separately
7194 if(p <= WhiteKing) (*nW)++; else if(p <= BlackKing) (*nB)++;
7195 if(p == WhiteBishop || p == WhiteFerz || p == WhiteAlfil ||
7196 p == BlackBishop || p == BlackFerz || p == BlackAlfil )
7197 *bishopColor |= 1 << ((f^r)&1); // track square color of color-bound pieces
7202 SufficientDefence(int pCnt[], int side, int nMine, int nHis)
7204 int myPawns = pCnt[WhitePawn+side]; // my total Pawn count;
7205 int majorDefense = pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackKnight-side];
7207 nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side]; // discount defenders
7208 if(nMine - myPawns > 2) return FALSE; // no trivial draws with more than 1 major
7209 if(myPawns == 2 && nMine == 3) // KPP
7210 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 3;
7211 if(myPawns == 1 && nMine == 2) // KP
7212 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] + pCnt[BlackPawn-side] >= 1;
7213 if(myPawns == 1 && nMine == 3 && pCnt[WhiteKnight+side]) // KHP
7214 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side]*2 >= 5;
7215 if(myPawns) return FALSE;
7216 if(pCnt[WhiteRook+side])
7217 return pCnt[BlackRook-side] ||
7218 pCnt[BlackCannon-side] && (pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] >= 2) ||
7219 pCnt[BlackKnight-side] && pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] > 2 ||
7220 pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 4;
7221 if(pCnt[WhiteCannon+side]) {
7222 if(pCnt[WhiteFerz+side] + myPawns == 0) return TRUE; // Cannon needs platform
7223 return majorDefense || pCnt[BlackAlfil-side] >= 2;
7225 if(pCnt[WhiteKnight+side])
7226 return majorDefense || pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] + pCnt[BlackPawn-side] >= 1;
7231 MatingPotential(int pCnt[], int side, int nMine, int nHis, int stale, int bisColor)
7233 VariantClass v = gameInfo.variant;
7235 if(v == VariantShogi || v == VariantCrazyhouse || v == VariantBughouse) return TRUE; // drop games always winnable
7236 if(v == VariantShatranj) return TRUE; // always winnable through baring
7237 if(v == VariantLosers || v == VariantSuicide || v == VariantGiveaway) return TRUE;
7238 if(v == Variant3Check || v == VariantAtomic) return nMine > 1; // can win through checking / exploding King
7240 if(v == VariantXiangqi) {
7241 int majors = 5*pCnt[BlackKnight-side] + 7*pCnt[BlackCannon-side] + 7*pCnt[BlackRook-side];
7243 nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side] + stale; // discount defensive pieces and back-rank Pawns
7244 if(nMine + stale == 1) return (pCnt[BlackFerz-side] > 1 && pCnt[BlackKnight-side] > 0); // bare K can stalemate KHAA (!)
7245 if(nMine > 2) return TRUE; // if we don't have P, H or R, we must have CC
7246 if(nMine == 2 && pCnt[WhiteCannon+side] == 0) return TRUE; // We have at least one P, H or R
7247 // if we get here, we must have KC... or KP..., possibly with additional A, E or last-rank P
7248 if(stale) // we have at least one last-rank P plus perhaps C
7249 return majors // KPKX
7250 || pCnt[BlackFerz-side] && pCnt[BlackFerz-side] + pCnt[WhiteCannon+side] + stale > 2; // KPKAA, KPPKA and KCPKA
7252 return pCnt[WhiteFerz+side] // KCAK
7253 || pCnt[WhiteAlfil+side] && pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackFerz-side] // KCEKA, KCEKX (X!=H)
7254 || majors + (12*pCnt[BlackFerz-side] | 6*pCnt[BlackAlfil-side]) > 16; // KCKAA, KCKAX, KCKEEX, KCKEXX (XX!=HH), KCKXXX
7255 // TO DO: cases wih an unpromoted f-Pawn acting as platform for an opponent Cannon
7257 } else if(pCnt[WhiteKing] == 1 && pCnt[BlackKing] == 1) { // other variants with orthodox Kings
7258 int nBishops = pCnt[WhiteBishop+side] + pCnt[WhiteFerz+side];
7260 if(nMine == 1) return FALSE; // bare King
7261 if(nBishops && bisColor == 3) return TRUE; // There must be a second B/A/F, which can either block (his) or attack (mine) the escape square
7262 nMine += (nBishops > 0) - nBishops; // By now all Bishops (and Ferz) on like-colored squares, so count as one
7263 if(nMine > 2 && nMine != pCnt[WhiteAlfil+side] + 1) return TRUE; // At least two pieces, not all Alfils
7264 // by now we have King + 1 piece (or multiple Bishops on the same color)
7265 if(pCnt[WhiteKnight+side])
7266 return (pCnt[BlackKnight-side] + pCnt[BlackBishop-side] + pCnt[BlackMan-side] +
7267 pCnt[BlackWazir-side] + pCnt[BlackSilver-side] + bisColor // KNKN, KNKB, KNKF, KNKE, KNKW, KNKM, KNKS
7268 || nHis > 3); // be sure to cover suffocation mates in corner (e.g. KNKQCA)
7270 return (pCnt[BlackKnight-side]); // KBKN, KFKN
7271 if(pCnt[WhiteAlfil+side])
7272 return (nHis > 2); // Alfils can in general not reach a corner square, but there might be edge (suffocation) mates
7273 if(pCnt[WhiteWazir+side])
7274 return (pCnt[BlackKnight-side] + pCnt[BlackWazir-side] + pCnt[BlackAlfil-side]); // KWKN, KWKW, KWKE
7281 Adjudicate(ChessProgramState *cps)
7282 { // [HGM] some adjudications useful with buggy engines
7283 // [HGM] adjudicate: made into separate routine, which now can be called after every move
7284 // In any case it determnes if the game is a claimable draw (filling in EP_STATUS).
7285 // Actually ending the game is now based on the additional internal condition canAdjudicate.
7286 // Only when the game is ended, and the opponent is a computer, this opponent gets the move relayed.
7287 int k, count = 0; static int bare = 1;
7288 ChessProgramState *engineOpponent = (gameMode == TwoMachinesPlay ? cps->other : (cps ? NULL : &first));
7289 Boolean canAdjudicate = !appData.icsActive;
7291 // most tests only when we understand the game, i.e. legality-checking on
7292 if( appData.testLegality )
7293 { /* [HGM] Some more adjudications for obstinate engines */
7294 int nrW, nrB, bishopColor, staleW, staleB, nr[EmptySquare+1], i;
7295 static int moveCount = 6;
7297 char *reason = NULL;
7299 /* Count what is on board. */
7300 Count(boards[forwardMostMove], nr, &nrW, &nrB, &staleW, &staleB, &bishopColor);
7302 /* Some material-based adjudications that have to be made before stalemate test */
7303 if(gameInfo.variant == VariantAtomic && nr[WhiteKing] + nr[BlackKing] < 2) {
7304 // [HGM] atomic: stm must have lost his King on previous move, as destroying own K is illegal
7305 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // make claimable as if stm is checkmated
7306 if(canAdjudicate && appData.checkMates) {
7308 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
7309 GameEnds( WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins,
7310 "Xboard adjudication: King destroyed", GE_XBOARD );
7315 /* Bare King in Shatranj (loses) or Losers (wins) */
7316 if( nrW == 1 || nrB == 1) {
7317 if( gameInfo.variant == VariantLosers) { // [HGM] losers: bare King wins (stm must have it first)
7318 boards[forwardMostMove][EP_STATUS] = EP_WINS; // mark as win, so it becomes claimable
7319 if(canAdjudicate && appData.checkMates) {
7321 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets to see move
7322 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
7323 "Xboard adjudication: Bare king", GE_XBOARD );
7327 if( gameInfo.variant == VariantShatranj && --bare < 0)
7329 boards[forwardMostMove][EP_STATUS] = EP_WINS; // make claimable as win for stm
7330 if(canAdjudicate && appData.checkMates) {
7331 /* but only adjudicate if adjudication enabled */
7333 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
7334 GameEnds( nrW > 1 ? WhiteWins : nrB > 1 ? BlackWins : GameIsDrawn,
7335 "Xboard adjudication: Bare king", GE_XBOARD );
7342 // don't wait for engine to announce game end if we can judge ourselves
7343 switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
7345 if(gameInfo.variant == Variant3Check) { // [HGM] 3check: when in check, test if 3rd time
7346 int i, checkCnt = 0; // (should really be done by making nr of checks part of game state)
7347 for(i=forwardMostMove-2; i>=backwardMostMove; i-=2) {
7348 if(MateTest(boards[i], PosFlags(i)) == MT_CHECK)
7351 reason = "Xboard adjudication: 3rd check";
7352 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE;
7362 reason = "Xboard adjudication: Stalemate";
7363 if((signed char)boards[forwardMostMove][EP_STATUS] != EP_CHECKMATE) { // [HGM] don't touch win through baring or K-capt
7364 boards[forwardMostMove][EP_STATUS] = EP_STALEMATE; // default result for stalemate is draw
7365 if(gameInfo.variant == VariantLosers || gameInfo.variant == VariantGiveaway) // [HGM] losers:
7366 boards[forwardMostMove][EP_STATUS] = EP_WINS; // in these variants stalemated is always a win
7367 else if(gameInfo.variant == VariantSuicide) // in suicide it depends
7368 boards[forwardMostMove][EP_STATUS] = nrW == nrB ? EP_STALEMATE :
7369 ((nrW < nrB) != WhiteOnMove(forwardMostMove) ?
7370 EP_CHECKMATE : EP_WINS);
7371 else if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantXiangqi)
7372 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // and in these variants being stalemated loses
7376 reason = "Xboard adjudication: Checkmate";
7377 boards[forwardMostMove][EP_STATUS] = (gameInfo.variant == VariantLosers ? EP_WINS : EP_CHECKMATE);
7381 switch(i = (signed char)boards[forwardMostMove][EP_STATUS]) {
7383 result = GameIsDrawn; break;
7385 result = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins; break;
7387 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins; break;
7391 if(canAdjudicate && appData.checkMates && result) { // [HGM] mates: adjudicate finished games if requested
7393 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7394 GameEnds( result, reason, GE_XBOARD );
7398 /* Next absolutely insufficient mating material. */
7399 if(!MatingPotential(nr, WhitePawn, nrW, nrB, staleW, bishopColor) &&
7400 !MatingPotential(nr, BlackPawn, nrB, nrW, staleB, bishopColor))
7401 { /* includes KBK, KNK, KK of KBKB with like Bishops */
7403 /* always flag draws, for judging claims */
7404 boards[forwardMostMove][EP_STATUS] = EP_INSUF_DRAW;
7406 if(canAdjudicate && appData.materialDraws) {
7407 /* but only adjudicate them if adjudication enabled */
7408 if(engineOpponent) {
7409 SendToProgram("force\n", engineOpponent); // suppress reply
7410 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see last move */
7412 GameEnds( GameIsDrawn, "Xboard adjudication: Insufficient mating material", GE_XBOARD );
7417 /* Then some trivial draws (only adjudicate, cannot be claimed) */
7418 if(gameInfo.variant == VariantXiangqi ?
7419 SufficientDefence(nr, WhitePawn, nrW, nrB) && SufficientDefence(nr, BlackPawn, nrB, nrW)
7421 ( nr[WhiteRook] == 1 && nr[BlackRook] == 1 /* KRKR */
7422 || nr[WhiteQueen] && nr[BlackQueen]==1 /* KQKQ */
7423 || nr[WhiteKnight]==2 || nr[BlackKnight]==2 /* KNNK */
7424 || nr[WhiteKnight]+nr[WhiteBishop] == 1 && nr[BlackKnight]+nr[BlackBishop] == 1 /* KBKN, KBKB, KNKN */
7426 if(--moveCount < 0 && appData.trivialDraws && canAdjudicate)
7427 { /* if the first 3 moves do not show a tactical win, declare draw */
7428 if(engineOpponent) {
7429 SendToProgram("force\n", engineOpponent); // suppress reply
7430 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7432 GameEnds( GameIsDrawn, "Xboard adjudication: Trivial draw", GE_XBOARD );
7435 } else moveCount = 6;
7437 if (appData.debugMode) { int i;
7438 fprintf(debugFP, "repeat test fmm=%d bmm=%d ep=%d, reps=%d\n",
7439 forwardMostMove, backwardMostMove, boards[backwardMostMove][EP_STATUS],
7440 appData.drawRepeats);
7441 for( i=forwardMostMove; i>=backwardMostMove; i-- )
7442 fprintf(debugFP, "%d ep=%d\n", i, (signed char)boards[i][EP_STATUS]);
7446 // Repetition draws and 50-move rule can be applied independently of legality testing
7448 /* Check for rep-draws */
7450 for(k = forwardMostMove-2;
7451 k>=backwardMostMove && k>=forwardMostMove-100 &&
7452 (signed char)boards[k][EP_STATUS] < EP_UNKNOWN &&
7453 (signed char)boards[k+2][EP_STATUS] <= EP_NONE && (signed char)boards[k+1][EP_STATUS] <= EP_NONE;
7456 if(CompareBoards(boards[k], boards[forwardMostMove])) {
7457 /* compare castling rights */
7458 if( boards[forwardMostMove][CASTLING][2] != boards[k][CASTLING][2] &&
7459 (boards[k][CASTLING][0] != NoRights || boards[k][CASTLING][1] != NoRights) )
7460 rights++; /* King lost rights, while rook still had them */
7461 if( boards[forwardMostMove][CASTLING][2] != NoRights ) { /* king has rights */
7462 if( boards[forwardMostMove][CASTLING][0] != boards[k][CASTLING][0] ||
7463 boards[forwardMostMove][CASTLING][1] != boards[k][CASTLING][1] )
7464 rights++; /* but at least one rook lost them */
7466 if( boards[forwardMostMove][CASTLING][5] != boards[k][CASTLING][5] &&
7467 (boards[k][CASTLING][3] != NoRights || boards[k][CASTLING][4] != NoRights) )
7469 if( boards[forwardMostMove][CASTLING][5] != NoRights ) {
7470 if( boards[forwardMostMove][CASTLING][3] != boards[k][CASTLING][3] ||
7471 boards[forwardMostMove][CASTLING][4] != boards[k][CASTLING][4] )
7474 if( rights == 0 && ++count > appData.drawRepeats-2 && canAdjudicate
7475 && appData.drawRepeats > 1) {
7476 /* adjudicate after user-specified nr of repeats */
7477 int result = GameIsDrawn;
7478 char *details = "XBoard adjudication: repetition draw";
7479 if(gameInfo.variant == VariantXiangqi && appData.testLegality) {
7480 // [HGM] xiangqi: check for forbidden perpetuals
7481 int m, ourPerpetual = 1, hisPerpetual = 1;
7482 for(m=forwardMostMove; m>k; m-=2) {
7483 if(MateTest(boards[m], PosFlags(m)) != MT_CHECK)
7484 ourPerpetual = 0; // the current mover did not always check
7485 if(MateTest(boards[m-1], PosFlags(m-1)) != MT_CHECK)
7486 hisPerpetual = 0; // the opponent did not always check
7488 if(appData.debugMode) fprintf(debugFP, "XQ perpetual test, our=%d, his=%d\n",
7489 ourPerpetual, hisPerpetual);
7490 if(ourPerpetual && !hisPerpetual) { // we are actively checking him: forfeit
7491 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
7492 details = "Xboard adjudication: perpetual checking";
7494 if(hisPerpetual && !ourPerpetual) { // he is checking us, but did not repeat yet
7495 break; // (or we would have caught him before). Abort repetition-checking loop.
7497 // Now check for perpetual chases
7498 if(!ourPerpetual && !hisPerpetual) { // no perpetual check, test for chase
7499 hisPerpetual = PerpetualChase(k, forwardMostMove);
7500 ourPerpetual = PerpetualChase(k+1, forwardMostMove);
7501 if(ourPerpetual && !hisPerpetual) { // we are actively chasing him: forfeit
7502 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
7503 details = "Xboard adjudication: perpetual chasing";
7505 if(hisPerpetual && !ourPerpetual) // he is chasing us, but did not repeat yet
7506 break; // Abort repetition-checking loop.
7508 // if neither of us is checking or chasing all the time, or both are, it is draw
7510 if(engineOpponent) {
7511 SendToProgram("force\n", engineOpponent); // suppress reply
7512 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7514 GameEnds( result, details, GE_XBOARD );
7517 if( rights == 0 && count > 1 ) /* occurred 2 or more times before */
7518 boards[forwardMostMove][EP_STATUS] = EP_REP_DRAW;
7522 /* Now we test for 50-move draws. Determine ply count */
7523 count = forwardMostMove;
7524 /* look for last irreversble move */
7525 while( (signed char)boards[count][EP_STATUS] <= EP_NONE && count > backwardMostMove )
7527 /* if we hit starting position, add initial plies */
7528 if( count == backwardMostMove )
7529 count -= initialRulePlies;
7530 count = forwardMostMove - count;
7531 if(gameInfo.variant == VariantXiangqi && ( count >= 100 || count >= 2*appData.ruleMoves ) ) {
7532 // adjust reversible move counter for checks in Xiangqi
7533 int i = forwardMostMove - count, inCheck = 0, lastCheck;
7534 if(i < backwardMostMove) i = backwardMostMove;
7535 while(i <= forwardMostMove) {
7536 lastCheck = inCheck; // check evasion does not count
7537 inCheck = (MateTest(boards[i], PosFlags(i)) == MT_CHECK);
7538 if(inCheck || lastCheck) count--; // check does not count
7543 boards[forwardMostMove][EP_STATUS] = EP_RULE_DRAW;
7544 /* this is used to judge if draw claims are legal */
7545 if(canAdjudicate && appData.ruleMoves > 0 && count >= 2*appData.ruleMoves) {
7546 if(engineOpponent) {
7547 SendToProgram("force\n", engineOpponent); // suppress reply
7548 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7550 GameEnds( GameIsDrawn, "Xboard adjudication: 50-move rule", GE_XBOARD );
7554 /* if draw offer is pending, treat it as a draw claim
7555 * when draw condition present, to allow engines a way to
7556 * claim draws before making their move to avoid a race
7557 * condition occurring after their move
7559 if((gameMode == TwoMachinesPlay ? second.offeredDraw : userOfferedDraw) || first.offeredDraw ) {
7561 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_RULE_DRAW)
7562 p = "Draw claim: 50-move rule";
7563 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_REP_DRAW)
7564 p = "Draw claim: 3-fold repetition";
7565 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_INSUF_DRAW)
7566 p = "Draw claim: insufficient mating material";
7567 if( p != NULL && canAdjudicate) {
7568 if(engineOpponent) {
7569 SendToProgram("force\n", engineOpponent); // suppress reply
7570 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7572 GameEnds( GameIsDrawn, p, GE_XBOARD );
7577 if( canAdjudicate && appData.adjudicateDrawMoves > 0 && forwardMostMove > (2*appData.adjudicateDrawMoves) ) {
7578 if(engineOpponent) {
7579 SendToProgram("force\n", engineOpponent); // suppress reply
7580 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7582 GameEnds( GameIsDrawn, "Xboard adjudication: long game", GE_XBOARD );
7588 char *SendMoveToBookUser(int moveNr, ChessProgramState *cps, int initial)
7589 { // [HGM] book: this routine intercepts moves to simulate book replies
7590 char *bookHit = NULL;
7592 //first determine if the incoming move brings opponent into his book
7593 if(appData.usePolyglotBook && (cps == &first ? !appData.firstHasOwnBookUCI : !appData.secondHasOwnBookUCI))
7594 bookHit = ProbeBook(moveNr+1, appData.polyglotBook); // returns move
7595 if(appData.debugMode) fprintf(debugFP, "book hit = %s\n", bookHit ? bookHit : "(NULL)");
7596 if(bookHit != NULL && !cps->bookSuspend) {
7597 // make sure opponent is not going to reply after receiving move to book position
7598 SendToProgram("force\n", cps);
7599 cps->bookSuspend = TRUE; // flag indicating it has to be restarted
7601 if(!initial) SendMoveToProgram(moveNr, cps); // with hit on initial position there is no move
7602 // now arrange restart after book miss
7604 // after a book hit we never send 'go', and the code after the call to this routine
7605 // has '&& !bookHit' added to suppress potential sending there (based on 'firstMove').
7606 char buf[MSG_SIZ], *move = bookHit;
7608 int fromX, fromY, toX, toY;
7612 if (ParseOneMove(bookHit, forwardMostMove, &moveType,
7613 &fromX, &fromY, &toX, &toY, &promoChar)) {
7614 (void) CoordsToAlgebraic(boards[forwardMostMove],
7615 PosFlags(forwardMostMove),
7616 fromY, fromX, toY, toX, promoChar, move);
7618 if(appData.debugMode) fprintf(debugFP, "Book move could not be parsed\n");
7622 snprintf(buf, MSG_SIZ, "%s%s\n", (cps->useUsermove ? "usermove " : ""), move); // force book move into program supposed to play it
7623 SendToProgram(buf, cps);
7624 if(!initial) firstMove = FALSE; // normally we would clear the firstMove condition after return & sending 'go'
7625 } else if(initial) { // 'go' was needed irrespective of firstMove, and it has to be done in this routine
7626 SendToProgram("go\n", cps);
7627 cps->bookSuspend = FALSE; // after a 'go' we are never suspended
7628 } else { // 'go' might be sent based on 'firstMove' after this routine returns
7629 if(cps->bookSuspend && !firstMove) // 'go' needed, and it will not be done after we return
7630 SendToProgram("go\n", cps);
7631 cps->bookSuspend = FALSE; // anyhow, we will not be suspended after a miss
7633 return bookHit; // notify caller of hit, so it can take action to send move to opponent
7637 ChessProgramState *savedState;
7638 void DeferredBookMove(void)
7640 if(savedState->lastPing != savedState->lastPong)
7641 ScheduleDelayedEvent(DeferredBookMove, 10);
7643 HandleMachineMove(savedMessage, savedState);
7646 static int savedWhitePlayer, savedBlackPlayer, pairingReceived;
7649 HandleMachineMove(message, cps)
7651 ChessProgramState *cps;
7653 char machineMove[MSG_SIZ], buf1[MSG_SIZ*10], buf2[MSG_SIZ];
7654 char realname[MSG_SIZ];
7655 int fromX, fromY, toX, toY;
7662 if(cps == &pairing && sscanf(message, "%d-%d", &savedWhitePlayer, &savedBlackPlayer) == 2) {
7663 // [HGM] pairing: Mega-hack! Pairing engine also uses this routine (so it could give other WB commands).
7664 if(savedWhitePlayer == 0 || savedBlackPlayer == 0) return;
7665 pairingReceived = 1;
7667 return; // Skim the pairing messages here.
7672 FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book hit
7674 * Kludge to ignore BEL characters
7676 while (*message == '\007') message++;
7679 * [HGM] engine debug message: ignore lines starting with '#' character
7681 if(cps->debug && *message == '#') return;
7684 * Look for book output
7686 if (cps == &first && bookRequested) {
7687 if (message[0] == '\t' || message[0] == ' ') {
7688 /* Part of the book output is here; append it */
7689 strcat(bookOutput, message);
7690 strcat(bookOutput, " \n");
7692 } else if (bookOutput[0] != NULLCHAR) {
7693 /* All of book output has arrived; display it */
7694 char *p = bookOutput;
7695 while (*p != NULLCHAR) {
7696 if (*p == '\t') *p = ' ';
7699 DisplayInformation(bookOutput);
7700 bookRequested = FALSE;
7701 /* Fall through to parse the current output */
7706 * Look for machine move.
7708 if ((sscanf(message, "%s %s %s", buf1, buf2, machineMove) == 3 && strcmp(buf2, "...") == 0) ||
7709 (sscanf(message, "%s %s", buf1, machineMove) == 2 && strcmp(buf1, "move") == 0))
7711 /* This method is only useful on engines that support ping */
7712 if (cps->lastPing != cps->lastPong) {
7713 if (gameMode == BeginningOfGame) {
7714 /* Extra move from before last new; ignore */
7715 if (appData.debugMode) {
7716 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
7719 if (appData.debugMode) {
7720 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
7721 cps->which, gameMode);
7724 SendToProgram("undo\n", cps);
7730 case BeginningOfGame:
7731 /* Extra move from before last reset; ignore */
7732 if (appData.debugMode) {
7733 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
7740 /* Extra move after we tried to stop. The mode test is
7741 not a reliable way of detecting this problem, but it's
7742 the best we can do on engines that don't support ping.
7744 if (appData.debugMode) {
7745 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
7746 cps->which, gameMode);
7748 SendToProgram("undo\n", cps);
7751 case MachinePlaysWhite:
7752 case IcsPlayingWhite:
7753 machineWhite = TRUE;
7756 case MachinePlaysBlack:
7757 case IcsPlayingBlack:
7758 machineWhite = FALSE;
7761 case TwoMachinesPlay:
7762 machineWhite = (cps->twoMachinesColor[0] == 'w');
7765 if (WhiteOnMove(forwardMostMove) != machineWhite) {
7766 if (appData.debugMode) {
7768 "Ignoring move out of turn by %s, gameMode %d"
7769 ", forwardMost %d\n",
7770 cps->which, gameMode, forwardMostMove);
7775 if (appData.debugMode) { int f = forwardMostMove;
7776 fprintf(debugFP, "machine move %d, castling = %d %d %d %d %d %d\n", f,
7777 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
7778 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
7780 if(cps->alphaRank) AlphaRank(machineMove, 4);
7781 if (!ParseOneMove(machineMove, forwardMostMove, &moveType,
7782 &fromX, &fromY, &toX, &toY, &promoChar)) {
7783 /* Machine move could not be parsed; ignore it. */
7784 snprintf(buf1, MSG_SIZ*10, _("Illegal move \"%s\" from %s machine"),
7785 machineMove, _(cps->which));
7786 DisplayError(buf1, 0);
7787 snprintf(buf1, MSG_SIZ*10, "Xboard: Forfeit due to invalid move: %s (%c%c%c%c) res=%d",
7788 machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, moveType);
7789 if (gameMode == TwoMachinesPlay) {
7790 GameEnds(machineWhite ? BlackWins : WhiteWins,
7796 /* [HGM] Apparently legal, but so far only tested with EP_UNKOWN */
7797 /* So we have to redo legality test with true e.p. status here, */
7798 /* to make sure an illegal e.p. capture does not slip through, */
7799 /* to cause a forfeit on a justified illegal-move complaint */
7800 /* of the opponent. */
7801 if( gameMode==TwoMachinesPlay && appData.testLegality ) {
7803 moveType = LegalityTest(boards[forwardMostMove], PosFlags(forwardMostMove),
7804 fromY, fromX, toY, toX, promoChar);
7805 if (appData.debugMode) {
7807 for(i=0; i< nrCastlingRights; i++) fprintf(debugFP, "(%d,%d) ",
7808 boards[forwardMostMove][CASTLING][i], castlingRank[i]);
7809 fprintf(debugFP, "castling rights\n");
7811 if(moveType == IllegalMove) {
7812 snprintf(buf1, MSG_SIZ*10, "Xboard: Forfeit due to illegal move: %s (%c%c%c%c)%c",
7813 machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, 0);
7814 GameEnds(machineWhite ? BlackWins : WhiteWins,
7817 } else if(gameInfo.variant != VariantFischeRandom && gameInfo.variant != VariantCapaRandom)
7818 /* [HGM] Kludge to handle engines that send FRC-style castling
7819 when they shouldn't (like TSCP-Gothic) */
7821 case WhiteASideCastleFR:
7822 case BlackASideCastleFR:
7824 currentMoveString[2]++;
7826 case WhiteHSideCastleFR:
7827 case BlackHSideCastleFR:
7829 currentMoveString[2]--;
7831 default: ; // nothing to do, but suppresses warning of pedantic compilers
7834 hintRequested = FALSE;
7835 lastHint[0] = NULLCHAR;
7836 bookRequested = FALSE;
7837 /* Program may be pondering now */
7838 cps->maybeThinking = TRUE;
7839 if (cps->sendTime == 2) cps->sendTime = 1;
7840 if (cps->offeredDraw) cps->offeredDraw--;
7842 /* [AS] Save move info*/
7843 pvInfoList[ forwardMostMove ].score = programStats.score;
7844 pvInfoList[ forwardMostMove ].depth = programStats.depth;
7845 pvInfoList[ forwardMostMove ].time = programStats.time; // [HGM] PGNtime: take time from engine stats
7847 MakeMove(fromX, fromY, toX, toY, promoChar);/*updates forwardMostMove*/
7849 /* [AS] Adjudicate game if needed (note: remember that forwardMostMove now points past the last move) */
7850 if( gameMode == TwoMachinesPlay && adjudicateLossThreshold != 0 && forwardMostMove >= adjudicateLossPlies ) {
7853 while( count < adjudicateLossPlies ) {
7854 int score = pvInfoList[ forwardMostMove - count - 1 ].score;
7857 score = -score; /* Flip score for winning side */
7860 if( score > adjudicateLossThreshold ) {
7867 if( count >= adjudicateLossPlies ) {
7868 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
7870 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
7871 "Xboard adjudication",
7878 if(Adjudicate(cps)) {
7879 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
7880 return; // [HGM] adjudicate: for all automatic game ends
7884 if ((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack) &&
7886 if(cps->offeredDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
7887 SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
7889 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
7891 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
7893 if(appData.autoKibitz && !appData.icsEngineAnalyze ) { /* [HGM] kibitz: send most-recent PV info to ICS */
7894 char buf[3*MSG_SIZ];
7896 snprintf(buf, 3*MSG_SIZ, "kibitz !!! %+.2f/%d (%.2f sec, %u nodes, %.0f knps) PV=%s\n",
7897 programStats.score / 100.,
7899 programStats.time / 100.,
7900 (unsigned int)programStats.nodes,
7901 (unsigned int)programStats.nodes / (10*abs(programStats.time) + 1.),
7902 programStats.movelist);
7904 if(appData.debugMode) fprintf(debugFP, "nodes = %d, %lld\n", (int) programStats.nodes, programStats.nodes);
7909 /* [AS] Clear stats for next move */
7910 ClearProgramStats();
7911 thinkOutput[0] = NULLCHAR;
7912 hiddenThinkOutputState = 0;
7915 if (gameMode == TwoMachinesPlay) {
7916 /* [HGM] relaying draw offers moved to after reception of move */
7917 /* and interpreting offer as claim if it brings draw condition */
7918 if (cps->offeredDraw == 1 && cps->other->sendDrawOffers) {
7919 SendToProgram("draw\n", cps->other);
7921 if (cps->other->sendTime) {
7922 SendTimeRemaining(cps->other,
7923 cps->other->twoMachinesColor[0] == 'w');
7925 bookHit = SendMoveToBookUser(forwardMostMove-1, cps->other, FALSE);
7926 if (firstMove && !bookHit) {
7928 if (cps->other->useColors) {
7929 SendToProgram(cps->other->twoMachinesColor, cps->other);
7931 SendToProgram("go\n", cps->other);
7933 cps->other->maybeThinking = TRUE;
7936 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
7938 if (!pausing && appData.ringBellAfterMoves) {
7943 * Reenable menu items that were disabled while
7944 * machine was thinking
7946 if (gameMode != TwoMachinesPlay)
7947 SetUserThinkingEnables();
7949 // [HGM] book: after book hit opponent has received move and is now in force mode
7950 // force the book reply into it, and then fake that it outputted this move by jumping
7951 // back to the beginning of HandleMachineMove, with cps toggled and message set to this move
7953 static char bookMove[MSG_SIZ]; // a bit generous?
7955 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
7956 strcat(bookMove, bookHit);
7959 programStats.nodes = programStats.depth = programStats.time =
7960 programStats.score = programStats.got_only_move = 0;
7961 sprintf(programStats.movelist, "%s (xbook)", bookHit);
7963 if(cps->lastPing != cps->lastPong) {
7964 savedMessage = message; // args for deferred call
7966 ScheduleDelayedEvent(DeferredBookMove, 10);
7975 /* Set special modes for chess engines. Later something general
7976 * could be added here; for now there is just one kludge feature,
7977 * needed because Crafty 15.10 and earlier don't ignore SIGINT
7978 * when "xboard" is given as an interactive command.
7980 if (strncmp(message, "kibitz Hello from Crafty", 24) == 0) {
7981 cps->useSigint = FALSE;
7982 cps->useSigterm = FALSE;
7984 if (strncmp(message, "feature ", 8) == 0) { // [HGM] moved forward to pre-empt non-compliant commands
7985 ParseFeatures(message+8, cps);
7986 return; // [HGM] This return was missing, causing option features to be recognized as non-compliant commands!
7989 if (!appData.testLegality && !strncmp(message, "setup ", 6)) { // [HGM] allow first engine to define opening position
7990 int dummy, s=6; char buf[MSG_SIZ];
7991 if(appData.icsActive || forwardMostMove != 0 || cps != &first || startedFromSetupPosition) return;
7992 if(sscanf(message, "setup (%s", buf) == 1) s = 8 + strlen(buf), buf[s-9] = NULLCHAR, SetCharTable(pieceToChar, buf);
7993 ParseFEN(boards[0], &dummy, message+s);
7994 DrawPosition(TRUE, boards[0]);
7995 startedFromSetupPosition = TRUE;
7998 /* [HGM] Allow engine to set up a position. Don't ask me why one would
7999 * want this, I was asked to put it in, and obliged.
8001 if (!strncmp(message, "setboard ", 9)) {
8002 Board initial_position;
8004 GameEnds(GameUnfinished, "Engine aborts game", GE_XBOARD);
8006 if (!ParseFEN(initial_position, &blackPlaysFirst, message + 9)) {
8007 DisplayError(_("Bad FEN received from engine"), 0);
8011 CopyBoard(boards[0], initial_position);
8012 initialRulePlies = FENrulePlies;
8013 if(blackPlaysFirst) gameMode = MachinePlaysWhite;
8014 else gameMode = MachinePlaysBlack;
8015 DrawPosition(FALSE, boards[currentMove]);
8021 * Look for communication commands
8023 if (!strncmp(message, "telluser ", 9)) {
8024 if(message[9] == '\\' && message[10] == '\\')
8025 EscapeExpand(message+9, message+11); // [HGM] esc: allow escape sequences in popup box
8026 DisplayNote(message + 9);
8029 if (!strncmp(message, "tellusererror ", 14)) {
8031 if(message[14] == '\\' && message[15] == '\\')
8032 EscapeExpand(message+14, message+16); // [HGM] esc: allow escape sequences in popup box
8033 DisplayError(message + 14, 0);
8036 if (!strncmp(message, "tellopponent ", 13)) {
8037 if (appData.icsActive) {
8039 snprintf(buf1, sizeof(buf1), "%ssay %s\n", ics_prefix, message + 13);
8043 DisplayNote(message + 13);
8047 if (!strncmp(message, "tellothers ", 11)) {
8048 if (appData.icsActive) {
8050 snprintf(buf1, sizeof(buf1), "%swhisper %s\n", ics_prefix, message + 11);
8056 if (!strncmp(message, "tellall ", 8)) {
8057 if (appData.icsActive) {
8059 snprintf(buf1, sizeof(buf1), "%skibitz %s\n", ics_prefix, message + 8);
8063 DisplayNote(message + 8);
8067 if (strncmp(message, "warning", 7) == 0) {
8068 /* Undocumented feature, use tellusererror in new code */
8069 DisplayError(message, 0);
8072 if (sscanf(message, "askuser %s %[^\n]", buf1, buf2) == 2) {
8073 safeStrCpy(realname, cps->tidy, sizeof(realname)/sizeof(realname[0]));
8074 strcat(realname, " query");
8075 AskQuestion(realname, buf2, buf1, cps->pr);
8078 /* Commands from the engine directly to ICS. We don't allow these to be
8079 * sent until we are logged on. Crafty kibitzes have been known to
8080 * interfere with the login process.
8083 if (!strncmp(message, "tellics ", 8)) {
8084 SendToICS(message + 8);
8088 if (!strncmp(message, "tellicsnoalias ", 15)) {
8089 SendToICS(ics_prefix);
8090 SendToICS(message + 15);
8094 /* The following are for backward compatibility only */
8095 if (!strncmp(message,"whisper",7) || !strncmp(message,"kibitz",6) ||
8096 !strncmp(message,"draw",4) || !strncmp(message,"tell",3)) {
8097 SendToICS(ics_prefix);
8103 if (sscanf(message, "pong %d", &cps->lastPong) == 1) {
8107 * If the move is illegal, cancel it and redraw the board.
8108 * Also deal with other error cases. Matching is rather loose
8109 * here to accommodate engines written before the spec.
8111 if (strncmp(message + 1, "llegal move", 11) == 0 ||
8112 strncmp(message, "Error", 5) == 0) {
8113 if (StrStr(message, "name") ||
8114 StrStr(message, "rating") || StrStr(message, "?") ||
8115 StrStr(message, "result") || StrStr(message, "board") ||
8116 StrStr(message, "bk") || StrStr(message, "computer") ||
8117 StrStr(message, "variant") || StrStr(message, "hint") ||
8118 StrStr(message, "random") || StrStr(message, "depth") ||
8119 StrStr(message, "accepted")) {
8122 if (StrStr(message, "protover")) {
8123 /* Program is responding to input, so it's apparently done
8124 initializing, and this error message indicates it is
8125 protocol version 1. So we don't need to wait any longer
8126 for it to initialize and send feature commands. */
8127 FeatureDone(cps, 1);
8128 cps->protocolVersion = 1;
8131 cps->maybeThinking = FALSE;
8133 if (StrStr(message, "draw")) {
8134 /* Program doesn't have "draw" command */
8135 cps->sendDrawOffers = 0;
8138 if (cps->sendTime != 1 &&
8139 (StrStr(message, "time") || StrStr(message, "otim"))) {
8140 /* Program apparently doesn't have "time" or "otim" command */
8144 if (StrStr(message, "analyze")) {
8145 cps->analysisSupport = FALSE;
8146 cps->analyzing = FALSE;
8148 snprintf(buf2,MSG_SIZ, _("%s does not support analysis"), cps->tidy);
8149 DisplayError(buf2, 0);
8152 if (StrStr(message, "(no matching move)st")) {
8153 /* Special kludge for GNU Chess 4 only */
8154 cps->stKludge = TRUE;
8155 SendTimeControl(cps, movesPerSession, timeControl,
8156 timeIncrement, appData.searchDepth,
8160 if (StrStr(message, "(no matching move)sd")) {
8161 /* Special kludge for GNU Chess 4 only */
8162 cps->sdKludge = TRUE;
8163 SendTimeControl(cps, movesPerSession, timeControl,
8164 timeIncrement, appData.searchDepth,
8168 if (!StrStr(message, "llegal")) {
8171 if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
8172 gameMode == IcsIdle) return;
8173 if (forwardMostMove <= backwardMostMove) return;
8174 if (pausing) PauseEvent();
8175 if(appData.forceIllegal) {
8176 // [HGM] illegal: machine refused move; force position after move into it
8177 SendToProgram("force\n", cps);
8178 if(!cps->useSetboard) { // hideous kludge on kludge, because SendBoard sucks.
8179 // we have a real problem now, as SendBoard will use the a2a3 kludge
8180 // when black is to move, while there might be nothing on a2 or black
8181 // might already have the move. So send the board as if white has the move.
8182 // But first we must change the stm of the engine, as it refused the last move
8183 SendBoard(cps, 0); // always kludgeless, as white is to move on boards[0]
8184 if(WhiteOnMove(forwardMostMove)) {
8185 SendToProgram("a7a6\n", cps); // for the engine black still had the move
8186 SendBoard(cps, forwardMostMove); // kludgeless board
8188 SendToProgram("a2a3\n", cps); // for the engine white still had the move
8189 CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
8190 SendBoard(cps, forwardMostMove+1); // kludgeless board
8192 } else SendBoard(cps, forwardMostMove); // FEN case, also sets stm properly
8193 if(gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack ||
8194 gameMode == TwoMachinesPlay)
8195 SendToProgram("go\n", cps);
8198 if (gameMode == PlayFromGameFile) {
8199 /* Stop reading this game file */
8200 gameMode = EditGame;
8203 /* [HGM] illegal-move claim should forfeit game when Xboard */
8204 /* only passes fully legal moves */
8205 if( appData.testLegality && gameMode == TwoMachinesPlay ) {
8206 GameEnds( cps->twoMachinesColor[0] == 'w' ? BlackWins : WhiteWins,
8207 "False illegal-move claim", GE_XBOARD );
8208 return; // do not take back move we tested as valid
8210 currentMove = forwardMostMove-1;
8211 DisplayMove(currentMove-1); /* before DisplayMoveError */
8212 SwitchClocks(forwardMostMove-1); // [HGM] race
8213 DisplayBothClocks();
8214 snprintf(buf1, 10*MSG_SIZ, _("Illegal move \"%s\" (rejected by %s chess program)"),
8215 parseList[currentMove], _(cps->which));
8216 DisplayMoveError(buf1);
8217 DrawPosition(FALSE, boards[currentMove]);
8220 if (strncmp(message, "time", 4) == 0 && StrStr(message, "Illegal")) {
8221 /* Program has a broken "time" command that
8222 outputs a string not ending in newline.
8228 * If chess program startup fails, exit with an error message.
8229 * Attempts to recover here are futile.
8231 if ((StrStr(message, "unknown host") != NULL)
8232 || (StrStr(message, "No remote directory") != NULL)
8233 || (StrStr(message, "not found") != NULL)
8234 || (StrStr(message, "No such file") != NULL)
8235 || (StrStr(message, "can't alloc") != NULL)
8236 || (StrStr(message, "Permission denied") != NULL)) {
8238 cps->maybeThinking = FALSE;
8239 snprintf(buf1, sizeof(buf1), _("Failed to start %s chess program %s on %s: %s\n"),
8240 _(cps->which), cps->program, cps->host, message);
8241 RemoveInputSource(cps->isr);
8242 if(appData.icsActive) DisplayFatalError(buf1, 0, 1); else {
8243 if(cps == &first) appData.noChessProgram = TRUE;
8244 DisplayError(buf1, 0);
8250 * Look for hint output
8252 if (sscanf(message, "Hint: %s", buf1) == 1) {
8253 if (cps == &first && hintRequested) {
8254 hintRequested = FALSE;
8255 if (ParseOneMove(buf1, forwardMostMove, &moveType,
8256 &fromX, &fromY, &toX, &toY, &promoChar)) {
8257 (void) CoordsToAlgebraic(boards[forwardMostMove],
8258 PosFlags(forwardMostMove),
8259 fromY, fromX, toY, toX, promoChar, buf1);
8260 snprintf(buf2, sizeof(buf2), _("Hint: %s"), buf1);
8261 DisplayInformation(buf2);
8263 /* Hint move could not be parsed!? */
8264 snprintf(buf2, sizeof(buf2),
8265 _("Illegal hint move \"%s\"\nfrom %s chess program"),
8266 buf1, _(cps->which));
8267 DisplayError(buf2, 0);
8270 safeStrCpy(lastHint, buf1, sizeof(lastHint)/sizeof(lastHint[0]));
8276 * Ignore other messages if game is not in progress
8278 if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
8279 gameMode == IcsIdle || cps->lastPing != cps->lastPong) return;
8282 * look for win, lose, draw, or draw offer
8284 if (strncmp(message, "1-0", 3) == 0) {
8285 char *p, *q, *r = "";
8286 p = strchr(message, '{');
8294 GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first)); /* [HGM] pass claimer indication for claim test */
8296 } else if (strncmp(message, "0-1", 3) == 0) {
8297 char *p, *q, *r = "";
8298 p = strchr(message, '{');
8306 /* Kludge for Arasan 4.1 bug */
8307 if (strcmp(r, "Black resigns") == 0) {
8308 GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first));
8311 GameEnds(BlackWins, r, GE_ENGINE1 + (cps != &first));
8313 } else if (strncmp(message, "1/2", 3) == 0) {
8314 char *p, *q, *r = "";
8315 p = strchr(message, '{');
8324 GameEnds(GameIsDrawn, r, GE_ENGINE1 + (cps != &first));
8327 } else if (strncmp(message, "White resign", 12) == 0) {
8328 GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
8330 } else if (strncmp(message, "Black resign", 12) == 0) {
8331 GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
8333 } else if (strncmp(message, "White matches", 13) == 0 ||
8334 strncmp(message, "Black matches", 13) == 0 ) {
8335 /* [HGM] ignore GNUShogi noises */
8337 } else if (strncmp(message, "White", 5) == 0 &&
8338 message[5] != '(' &&
8339 StrStr(message, "Black") == NULL) {
8340 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8342 } else if (strncmp(message, "Black", 5) == 0 &&
8343 message[5] != '(') {
8344 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8346 } else if (strcmp(message, "resign") == 0 ||
8347 strcmp(message, "computer resigns") == 0) {
8349 case MachinePlaysBlack:
8350 case IcsPlayingBlack:
8351 GameEnds(WhiteWins, "Black resigns", GE_ENGINE);
8353 case MachinePlaysWhite:
8354 case IcsPlayingWhite:
8355 GameEnds(BlackWins, "White resigns", GE_ENGINE);
8357 case TwoMachinesPlay:
8358 if (cps->twoMachinesColor[0] == 'w')
8359 GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
8361 GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
8368 } else if (strncmp(message, "opponent mates", 14) == 0) {
8370 case MachinePlaysBlack:
8371 case IcsPlayingBlack:
8372 GameEnds(WhiteWins, "White mates", GE_ENGINE);
8374 case MachinePlaysWhite:
8375 case IcsPlayingWhite:
8376 GameEnds(BlackWins, "Black mates", GE_ENGINE);
8378 case TwoMachinesPlay:
8379 if (cps->twoMachinesColor[0] == 'w')
8380 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8382 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8389 } else if (strncmp(message, "computer mates", 14) == 0) {
8391 case MachinePlaysBlack:
8392 case IcsPlayingBlack:
8393 GameEnds(BlackWins, "Black mates", GE_ENGINE1);
8395 case MachinePlaysWhite:
8396 case IcsPlayingWhite:
8397 GameEnds(WhiteWins, "White mates", GE_ENGINE);
8399 case TwoMachinesPlay:
8400 if (cps->twoMachinesColor[0] == 'w')
8401 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8403 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8410 } else if (strncmp(message, "checkmate", 9) == 0) {
8411 if (WhiteOnMove(forwardMostMove)) {
8412 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8414 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8417 } else if (strstr(message, "Draw") != NULL ||
8418 strstr(message, "game is a draw") != NULL) {
8419 GameEnds(GameIsDrawn, "Draw", GE_ENGINE1 + (cps != &first));
8421 } else if (strstr(message, "offer") != NULL &&
8422 strstr(message, "draw") != NULL) {
8424 if (appData.zippyPlay && first.initDone) {
8425 /* Relay offer to ICS */
8426 SendToICS(ics_prefix);
8427 SendToICS("draw\n");
8430 cps->offeredDraw = 2; /* valid until this engine moves twice */
8431 if (gameMode == TwoMachinesPlay) {
8432 if (cps->other->offeredDraw) {
8433 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
8434 /* [HGM] in two-machine mode we delay relaying draw offer */
8435 /* until after we also have move, to see if it is really claim */
8437 } else if (gameMode == MachinePlaysWhite ||
8438 gameMode == MachinePlaysBlack) {
8439 if (userOfferedDraw) {
8440 DisplayInformation(_("Machine accepts your draw offer"));
8441 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
8443 DisplayInformation(_("Machine offers a draw\nSelect Action / Draw to agree"));
8450 * Look for thinking output
8452 if ( appData.showThinking // [HGM] thinking: test all options that cause this output
8453 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
8455 int plylev, mvleft, mvtot, curscore, time;
8456 char mvname[MOVE_LEN];
8460 int prefixHint = FALSE;
8461 mvname[0] = NULLCHAR;
8464 case MachinePlaysBlack:
8465 case IcsPlayingBlack:
8466 if (WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
8468 case MachinePlaysWhite:
8469 case IcsPlayingWhite:
8470 if (!WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
8475 case IcsObserving: /* [DM] icsEngineAnalyze */
8476 if (!appData.icsEngineAnalyze) ignore = TRUE;
8478 case TwoMachinesPlay:
8479 if ((cps->twoMachinesColor[0] == 'w') != WhiteOnMove(forwardMostMove)) {
8489 ChessProgramStats tempStats = programStats; // [HGM] info: filter out info lines
8491 if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
8492 &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5) {
8494 if (plyext != ' ' && plyext != '\t') {
8498 /* [AS] Negate score if machine is playing black and reporting absolute scores */
8499 if( cps->scoreIsAbsolute &&
8500 ( gameMode == MachinePlaysBlack ||
8501 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b' ||
8502 gameMode == IcsPlayingBlack || // [HGM] also add other situations where engine should report black POV
8503 (gameMode == AnalyzeMode || gameMode == AnalyzeFile || gameMode == IcsObserving && appData.icsEngineAnalyze) &&
8504 !WhiteOnMove(currentMove)
8507 curscore = -curscore;
8511 tempStats.depth = plylev;
8512 tempStats.nodes = nodes;
8513 tempStats.time = time;
8514 tempStats.score = curscore;
8515 tempStats.got_only_move = 0;
8517 if(cps->nps >= 0) { /* [HGM] nps: use engine nodes or time to decrement clock */
8520 if(cps->nps == 0) ticklen = 10*time; // use engine reported time
8521 else ticklen = (1000. * u64ToDouble(nodes)) / cps->nps; // convert node count to time
8522 if(WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysWhite ||
8523 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'w'))
8524 whiteTimeRemaining = timeRemaining[0][forwardMostMove] - ticklen;
8525 if(!WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysBlack ||
8526 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b'))
8527 blackTimeRemaining = timeRemaining[1][forwardMostMove] - ticklen;
8530 /* Buffer overflow protection */
8531 if (buf1[0] != NULLCHAR) {
8532 if (strlen(buf1) >= sizeof(tempStats.movelist)
8533 && appData.debugMode) {
8535 "PV is too long; using the first %u bytes.\n",
8536 (unsigned) sizeof(tempStats.movelist) - 1);
8539 safeStrCpy( tempStats.movelist, buf1, sizeof(tempStats.movelist)/sizeof(tempStats.movelist[0]) );
8541 sprintf(tempStats.movelist, " no PV\n");
8544 if (tempStats.seen_stat) {
8545 tempStats.ok_to_send = 1;
8548 if (strchr(tempStats.movelist, '(') != NULL) {
8549 tempStats.line_is_book = 1;
8550 tempStats.nr_moves = 0;
8551 tempStats.moves_left = 0;
8553 tempStats.line_is_book = 0;
8556 if(tempStats.score != 0 || tempStats.nodes != 0 || tempStats.time != 0)
8557 programStats = tempStats; // [HGM] info: only set stats if genuine PV and not an info line
8559 SendProgramStatsToFrontend( cps, &tempStats );
8562 [AS] Protect the thinkOutput buffer from overflow... this
8563 is only useful if buf1 hasn't overflowed first!
8565 snprintf(thinkOutput, sizeof(thinkOutput)/sizeof(thinkOutput[0]), "[%d]%c%+.2f %s%s",
8567 (gameMode == TwoMachinesPlay ?
8568 ToUpper(cps->twoMachinesColor[0]) : ' '),
8569 ((double) curscore) / 100.0,
8570 prefixHint ? lastHint : "",
8571 prefixHint ? " " : "" );
8573 if( buf1[0] != NULLCHAR ) {
8574 unsigned max_len = sizeof(thinkOutput) - strlen(thinkOutput) - 1;
8576 if( strlen(buf1) > max_len ) {
8577 if( appData.debugMode) {
8578 fprintf(debugFP,"PV is too long for thinkOutput, truncating.\n");
8580 buf1[max_len+1] = '\0';
8583 strcat( thinkOutput, buf1 );
8586 if (currentMove == forwardMostMove || gameMode == AnalyzeMode
8587 || gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
8588 DisplayMove(currentMove - 1);
8592 } else if ((p=StrStr(message, "(only move)")) != NULL) {
8593 /* crafty (9.25+) says "(only move) <move>"
8594 * if there is only 1 legal move
8596 sscanf(p, "(only move) %s", buf1);
8597 snprintf(thinkOutput, sizeof(thinkOutput)/sizeof(thinkOutput[0]), "%s (only move)", buf1);
8598 sprintf(programStats.movelist, "%s (only move)", buf1);
8599 programStats.depth = 1;
8600 programStats.nr_moves = 1;
8601 programStats.moves_left = 1;
8602 programStats.nodes = 1;
8603 programStats.time = 1;
8604 programStats.got_only_move = 1;
8606 /* Not really, but we also use this member to
8607 mean "line isn't going to change" (Crafty
8608 isn't searching, so stats won't change) */
8609 programStats.line_is_book = 1;
8611 SendProgramStatsToFrontend( cps, &programStats );
8613 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
8614 gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
8615 DisplayMove(currentMove - 1);
8618 } else if (sscanf(message,"stat01: %d " u64Display " %d %d %d %s",
8619 &time, &nodes, &plylev, &mvleft,
8620 &mvtot, mvname) >= 5) {
8621 /* The stat01: line is from Crafty (9.29+) in response
8622 to the "." command */
8623 programStats.seen_stat = 1;
8624 cps->maybeThinking = TRUE;
8626 if (programStats.got_only_move || !appData.periodicUpdates)
8629 programStats.depth = plylev;
8630 programStats.time = time;
8631 programStats.nodes = nodes;
8632 programStats.moves_left = mvleft;
8633 programStats.nr_moves = mvtot;
8634 safeStrCpy(programStats.move_name, mvname, sizeof(programStats.move_name)/sizeof(programStats.move_name[0]));
8635 programStats.ok_to_send = 1;
8636 programStats.movelist[0] = '\0';
8638 SendProgramStatsToFrontend( cps, &programStats );
8642 } else if (strncmp(message,"++",2) == 0) {
8643 /* Crafty 9.29+ outputs this */
8644 programStats.got_fail = 2;
8647 } else if (strncmp(message,"--",2) == 0) {
8648 /* Crafty 9.29+ outputs this */
8649 programStats.got_fail = 1;
8652 } else if (thinkOutput[0] != NULLCHAR &&
8653 strncmp(message, " ", 4) == 0) {
8654 unsigned message_len;
8657 while (*p && *p == ' ') p++;
8659 message_len = strlen( p );
8661 /* [AS] Avoid buffer overflow */
8662 if( sizeof(thinkOutput) - strlen(thinkOutput) - 1 > message_len ) {
8663 strcat(thinkOutput, " ");
8664 strcat(thinkOutput, p);
8667 if( sizeof(programStats.movelist) - strlen(programStats.movelist) - 1 > message_len ) {
8668 strcat(programStats.movelist, " ");
8669 strcat(programStats.movelist, p);
8672 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
8673 gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
8674 DisplayMove(currentMove - 1);
8682 if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
8683 &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5)
8685 ChessProgramStats cpstats;
8687 if (plyext != ' ' && plyext != '\t') {
8691 /* [AS] Negate score if machine is playing black and reporting absolute scores */
8692 if( cps->scoreIsAbsolute && ((gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b')) ) {
8693 curscore = -curscore;
8696 cpstats.depth = plylev;
8697 cpstats.nodes = nodes;
8698 cpstats.time = time;
8699 cpstats.score = curscore;
8700 cpstats.got_only_move = 0;
8701 cpstats.movelist[0] = '\0';
8703 if (buf1[0] != NULLCHAR) {
8704 safeStrCpy( cpstats.movelist, buf1, sizeof(cpstats.movelist)/sizeof(cpstats.movelist[0]) );
8707 cpstats.ok_to_send = 0;
8708 cpstats.line_is_book = 0;
8709 cpstats.nr_moves = 0;
8710 cpstats.moves_left = 0;
8712 SendProgramStatsToFrontend( cps, &cpstats );
8719 /* Parse a game score from the character string "game", and
8720 record it as the history of the current game. The game
8721 score is NOT assumed to start from the standard position.
8722 The display is not updated in any way.
8725 ParseGameHistory(game)
8729 int fromX, fromY, toX, toY, boardIndex;
8734 if (appData.debugMode)
8735 fprintf(debugFP, "Parsing game history: %s\n", game);
8737 if (gameInfo.event == NULL) gameInfo.event = StrSave("ICS game");
8738 gameInfo.site = StrSave(appData.icsHost);
8739 gameInfo.date = PGNDate();
8740 gameInfo.round = StrSave("-");
8742 /* Parse out names of players */
8743 while (*game == ' ') game++;
8745 while (*game != ' ') *p++ = *game++;
8747 gameInfo.white = StrSave(buf);
8748 while (*game == ' ') game++;
8750 while (*game != ' ' && *game != '\n') *p++ = *game++;
8752 gameInfo.black = StrSave(buf);
8755 boardIndex = blackPlaysFirst ? 1 : 0;
8758 yyboardindex = boardIndex;
8759 moveType = (ChessMove) Myylex();
8761 case IllegalMove: /* maybe suicide chess, etc. */
8762 if (appData.debugMode) {
8763 fprintf(debugFP, "Illegal move from ICS: '%s'\n", yy_text);
8764 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
8765 setbuf(debugFP, NULL);
8767 case WhitePromotion:
8768 case BlackPromotion:
8769 case WhiteNonPromotion:
8770 case BlackNonPromotion:
8772 case WhiteCapturesEnPassant:
8773 case BlackCapturesEnPassant:
8774 case WhiteKingSideCastle:
8775 case WhiteQueenSideCastle:
8776 case BlackKingSideCastle:
8777 case BlackQueenSideCastle:
8778 case WhiteKingSideCastleWild:
8779 case WhiteQueenSideCastleWild:
8780 case BlackKingSideCastleWild:
8781 case BlackQueenSideCastleWild:
8783 case WhiteHSideCastleFR:
8784 case WhiteASideCastleFR:
8785 case BlackHSideCastleFR:
8786 case BlackASideCastleFR:
8788 fromX = currentMoveString[0] - AAA;
8789 fromY = currentMoveString[1] - ONE;
8790 toX = currentMoveString[2] - AAA;
8791 toY = currentMoveString[3] - ONE;
8792 promoChar = currentMoveString[4];
8796 fromX = moveType == WhiteDrop ?
8797 (int) CharToPiece(ToUpper(currentMoveString[0])) :
8798 (int) CharToPiece(ToLower(currentMoveString[0]));
8800 toX = currentMoveString[2] - AAA;
8801 toY = currentMoveString[3] - ONE;
8802 promoChar = NULLCHAR;
8806 snprintf(buf, MSG_SIZ, _("Ambiguous move in ICS output: \"%s\""), yy_text);
8807 if (appData.debugMode) {
8808 fprintf(debugFP, "Ambiguous move from ICS: '%s'\n", yy_text);
8809 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
8810 setbuf(debugFP, NULL);
8812 DisplayError(buf, 0);
8814 case ImpossibleMove:
8816 snprintf(buf, MSG_SIZ, _("Illegal move in ICS output: \"%s\""), yy_text);
8817 if (appData.debugMode) {
8818 fprintf(debugFP, "Impossible move from ICS: '%s'\n", yy_text);
8819 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
8820 setbuf(debugFP, NULL);
8822 DisplayError(buf, 0);
8825 if (boardIndex < backwardMostMove) {
8826 /* Oops, gap. How did that happen? */
8827 DisplayError(_("Gap in move list"), 0);
8830 backwardMostMove = blackPlaysFirst ? 1 : 0;
8831 if (boardIndex > forwardMostMove) {
8832 forwardMostMove = boardIndex;
8836 if (boardIndex > (blackPlaysFirst ? 1 : 0)) {
8837 strcat(parseList[boardIndex-1], " ");
8838 strcat(parseList[boardIndex-1], yy_text);
8850 case GameUnfinished:
8851 if (gameMode == IcsExamining) {
8852 if (boardIndex < backwardMostMove) {
8853 /* Oops, gap. How did that happen? */
8856 backwardMostMove = blackPlaysFirst ? 1 : 0;
8859 gameInfo.result = moveType;
8860 p = strchr(yy_text, '{');
8861 if (p == NULL) p = strchr(yy_text, '(');
8864 if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
8866 q = strchr(p, *p == '{' ? '}' : ')');
8867 if (q != NULL) *q = NULLCHAR;
8870 while(q = strchr(p, '\n')) *q = ' '; // [HGM] crush linefeeds in result message
8871 gameInfo.resultDetails = StrSave(p);
8874 if (boardIndex >= forwardMostMove &&
8875 !(gameMode == IcsObserving && ics_gamenum == -1)) {
8876 backwardMostMove = blackPlaysFirst ? 1 : 0;
8879 (void) CoordsToAlgebraic(boards[boardIndex], PosFlags(boardIndex),
8880 fromY, fromX, toY, toX, promoChar,
8881 parseList[boardIndex]);
8882 CopyBoard(boards[boardIndex + 1], boards[boardIndex]);
8883 /* currentMoveString is set as a side-effect of yylex */
8884 safeStrCpy(moveList[boardIndex], currentMoveString, sizeof(moveList[boardIndex])/sizeof(moveList[boardIndex][0]));
8885 strcat(moveList[boardIndex], "\n");
8887 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[boardIndex]);
8888 switch (MateTest(boards[boardIndex], PosFlags(boardIndex)) ) {
8894 if(gameInfo.variant != VariantShogi)
8895 strcat(parseList[boardIndex - 1], "+");
8899 strcat(parseList[boardIndex - 1], "#");
8906 /* Apply a move to the given board */
8908 ApplyMove(fromX, fromY, toX, toY, promoChar, board)
8909 int fromX, fromY, toX, toY;
8913 ChessSquare captured = board[toY][toX], piece, king; int p, oldEP = EP_NONE, berolina = 0;
8914 int promoRank = gameInfo.variant == VariantMakruk ? 3 : 1;
8916 /* [HGM] compute & store e.p. status and castling rights for new position */
8917 /* we can always do that 'in place', now pointers to these rights are passed to ApplyMove */
8919 if(gameInfo.variant == VariantBerolina) berolina = EP_BEROLIN_A;
8920 oldEP = (signed char)board[EP_STATUS];
8921 board[EP_STATUS] = EP_NONE;
8923 if( board[toY][toX] != EmptySquare )
8924 board[EP_STATUS] = EP_CAPTURE;
8926 if (fromY == DROP_RANK) {
8928 piece = board[toY][toX] = (ChessSquare) fromX;
8932 if( board[fromY][fromX] == WhiteLance || board[fromY][fromX] == BlackLance ) {
8933 if( gameInfo.variant != VariantSuper && gameInfo.variant != VariantShogi )
8934 board[EP_STATUS] = EP_PAWN_MOVE; // Lance is Pawn-like in most variants
8936 if( board[fromY][fromX] == WhitePawn ) {
8937 if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
8938 board[EP_STATUS] = EP_PAWN_MOVE;
8940 if(toX>BOARD_LEFT && board[toY][toX-1] == BlackPawn &&
8941 gameInfo.variant != VariantBerolina || toX < fromX)
8942 board[EP_STATUS] = toX | berolina;
8943 if(toX<BOARD_RGHT-1 && board[toY][toX+1] == BlackPawn &&
8944 gameInfo.variant != VariantBerolina || toX > fromX)
8945 board[EP_STATUS] = toX;
8948 if( board[fromY][fromX] == BlackPawn ) {
8949 if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
8950 board[EP_STATUS] = EP_PAWN_MOVE;
8951 if( toY-fromY== -2) {
8952 if(toX>BOARD_LEFT && board[toY][toX-1] == WhitePawn &&
8953 gameInfo.variant != VariantBerolina || toX < fromX)
8954 board[EP_STATUS] = toX | berolina;
8955 if(toX<BOARD_RGHT-1 && board[toY][toX+1] == WhitePawn &&
8956 gameInfo.variant != VariantBerolina || toX > fromX)
8957 board[EP_STATUS] = toX;
8961 for(i=0; i<nrCastlingRights; i++) {
8962 if(board[CASTLING][i] == fromX && castlingRank[i] == fromY ||
8963 board[CASTLING][i] == toX && castlingRank[i] == toY
8964 ) board[CASTLING][i] = NoRights; // revoke for moved or captured piece
8967 if (fromX == toX && fromY == toY) return;
8969 piece = board[fromY][fromX]; /* [HGM] remember, for Shogi promotion */
8970 king = piece < (int) BlackPawn ? WhiteKing : BlackKing; /* [HGM] Knightmate simplify testing for castling */
8971 if(gameInfo.variant == VariantKnightmate)
8972 king += (int) WhiteUnicorn - (int) WhiteKing;
8974 /* Code added by Tord: */
8975 /* FRC castling assumed when king captures friendly rook. [HGM] or RxK for S-Chess */
8976 if (board[fromY][fromX] == WhiteKing && board[toY][toX] == WhiteRook ||
8977 board[fromY][fromX] == WhiteRook && board[toY][toX] == WhiteKing) {
8978 board[fromY][fromX] = EmptySquare;
8979 board[toY][toX] = EmptySquare;
8980 if((toX > fromX) != (piece == WhiteRook)) {
8981 board[0][BOARD_RGHT-2] = WhiteKing; board[0][BOARD_RGHT-3] = WhiteRook;
8983 board[0][BOARD_LEFT+2] = WhiteKing; board[0][BOARD_LEFT+3] = WhiteRook;
8985 } else if (board[fromY][fromX] == BlackKing && board[toY][toX] == BlackRook ||
8986 board[fromY][fromX] == BlackRook && board[toY][toX] == BlackKing) {
8987 board[fromY][fromX] = EmptySquare;
8988 board[toY][toX] = EmptySquare;
8989 if((toX > fromX) != (piece == BlackRook)) {
8990 board[BOARD_HEIGHT-1][BOARD_RGHT-2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_RGHT-3] = BlackRook;
8992 board[BOARD_HEIGHT-1][BOARD_LEFT+2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_LEFT+3] = BlackRook;
8994 /* End of code added by Tord */
8996 } else if (board[fromY][fromX] == king
8997 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
8998 && toY == fromY && toX > fromX+1) {
8999 board[fromY][fromX] = EmptySquare;
9000 board[toY][toX] = king;
9001 board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
9002 board[fromY][BOARD_RGHT-1] = EmptySquare;
9003 } else if (board[fromY][fromX] == king
9004 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9005 && toY == fromY && toX < fromX-1) {
9006 board[fromY][fromX] = EmptySquare;
9007 board[toY][toX] = king;
9008 board[toY][toX+1] = board[fromY][BOARD_LEFT];
9009 board[fromY][BOARD_LEFT] = EmptySquare;
9010 } else if ((board[fromY][fromX] == WhitePawn && gameInfo.variant != VariantXiangqi ||
9011 board[fromY][fromX] == WhiteLance && gameInfo.variant != VariantSuper && gameInfo.variant != VariantShogi)
9012 && toY >= BOARD_HEIGHT-promoRank
9014 /* white pawn promotion */
9015 board[toY][toX] = CharToPiece(ToUpper(promoChar));
9016 if (board[toY][toX] == EmptySquare) {
9017 board[toY][toX] = WhiteQueen;
9019 if(gameInfo.variant==VariantBughouse ||
9020 gameInfo.variant==VariantCrazyhouse) /* [HGM] use shadow piece */
9021 board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
9022 board[fromY][fromX] = EmptySquare;
9023 } else if ((fromY == BOARD_HEIGHT-4)
9025 && gameInfo.variant != VariantXiangqi
9026 && gameInfo.variant != VariantBerolina
9027 && (board[fromY][fromX] == WhitePawn)
9028 && (board[toY][toX] == EmptySquare)) {
9029 board[fromY][fromX] = EmptySquare;
9030 board[toY][toX] = WhitePawn;
9031 captured = board[toY - 1][toX];
9032 board[toY - 1][toX] = EmptySquare;
9033 } else if ((fromY == BOARD_HEIGHT-4)
9035 && gameInfo.variant == VariantBerolina
9036 && (board[fromY][fromX] == WhitePawn)
9037 && (board[toY][toX] == EmptySquare)) {
9038 board[fromY][fromX] = EmptySquare;
9039 board[toY][toX] = WhitePawn;
9040 if(oldEP & EP_BEROLIN_A) {
9041 captured = board[fromY][fromX-1];
9042 board[fromY][fromX-1] = EmptySquare;
9043 }else{ captured = board[fromY][fromX+1];
9044 board[fromY][fromX+1] = EmptySquare;
9046 } else if (board[fromY][fromX] == king
9047 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9048 && toY == fromY && toX > fromX+1) {
9049 board[fromY][fromX] = EmptySquare;
9050 board[toY][toX] = king;
9051 board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
9052 board[fromY][BOARD_RGHT-1] = EmptySquare;
9053 } else if (board[fromY][fromX] == king
9054 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9055 && toY == fromY && toX < fromX-1) {
9056 board[fromY][fromX] = EmptySquare;
9057 board[toY][toX] = king;
9058 board[toY][toX+1] = board[fromY][BOARD_LEFT];
9059 board[fromY][BOARD_LEFT] = EmptySquare;
9060 } else if (fromY == 7 && fromX == 3
9061 && board[fromY][fromX] == BlackKing
9062 && toY == 7 && toX == 5) {
9063 board[fromY][fromX] = EmptySquare;
9064 board[toY][toX] = BlackKing;
9065 board[fromY][7] = EmptySquare;
9066 board[toY][4] = BlackRook;
9067 } else if (fromY == 7 && fromX == 3
9068 && board[fromY][fromX] == BlackKing
9069 && toY == 7 && toX == 1) {
9070 board[fromY][fromX] = EmptySquare;
9071 board[toY][toX] = BlackKing;
9072 board[fromY][0] = EmptySquare;
9073 board[toY][2] = BlackRook;
9074 } else if ((board[fromY][fromX] == BlackPawn && gameInfo.variant != VariantXiangqi ||
9075 board[fromY][fromX] == BlackLance && gameInfo.variant != VariantSuper && gameInfo.variant != VariantShogi)
9078 /* black pawn promotion */
9079 board[toY][toX] = CharToPiece(ToLower(promoChar));
9080 if (board[toY][toX] == EmptySquare) {
9081 board[toY][toX] = BlackQueen;
9083 if(gameInfo.variant==VariantBughouse ||
9084 gameInfo.variant==VariantCrazyhouse) /* [HGM] use shadow piece */
9085 board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
9086 board[fromY][fromX] = EmptySquare;
9087 } else if ((fromY == 3)
9089 && gameInfo.variant != VariantXiangqi
9090 && gameInfo.variant != VariantBerolina
9091 && (board[fromY][fromX] == BlackPawn)
9092 && (board[toY][toX] == EmptySquare)) {
9093 board[fromY][fromX] = EmptySquare;
9094 board[toY][toX] = BlackPawn;
9095 captured = board[toY + 1][toX];
9096 board[toY + 1][toX] = EmptySquare;
9097 } else if ((fromY == 3)
9099 && gameInfo.variant == VariantBerolina
9100 && (board[fromY][fromX] == BlackPawn)
9101 && (board[toY][toX] == EmptySquare)) {
9102 board[fromY][fromX] = EmptySquare;
9103 board[toY][toX] = BlackPawn;
9104 if(oldEP & EP_BEROLIN_A) {
9105 captured = board[fromY][fromX-1];
9106 board[fromY][fromX-1] = EmptySquare;
9107 }else{ captured = board[fromY][fromX+1];
9108 board[fromY][fromX+1] = EmptySquare;
9111 board[toY][toX] = board[fromY][fromX];
9112 board[fromY][fromX] = EmptySquare;
9116 if (gameInfo.holdingsWidth != 0) {
9118 /* !!A lot more code needs to be written to support holdings */
9119 /* [HGM] OK, so I have written it. Holdings are stored in the */
9120 /* penultimate board files, so they are automaticlly stored */
9121 /* in the game history. */
9122 if (fromY == DROP_RANK || gameInfo.variant == VariantSChess
9123 && promoChar && piece != WhitePawn && piece != BlackPawn) {
9124 /* Delete from holdings, by decreasing count */
9125 /* and erasing image if necessary */
9126 p = fromY == DROP_RANK ? (int) fromX : CharToPiece(piece > BlackPawn ? ToLower(promoChar) : ToUpper(promoChar));
9127 if(p < (int) BlackPawn) { /* white drop */
9128 p -= (int)WhitePawn;
9129 p = PieceToNumber((ChessSquare)p);
9130 if(p >= gameInfo.holdingsSize) p = 0;
9131 if(--board[p][BOARD_WIDTH-2] <= 0)
9132 board[p][BOARD_WIDTH-1] = EmptySquare;
9133 if((int)board[p][BOARD_WIDTH-2] < 0)
9134 board[p][BOARD_WIDTH-2] = 0;
9135 } else { /* black drop */
9136 p -= (int)BlackPawn;
9137 p = PieceToNumber((ChessSquare)p);
9138 if(p >= gameInfo.holdingsSize) p = 0;
9139 if(--board[BOARD_HEIGHT-1-p][1] <= 0)
9140 board[BOARD_HEIGHT-1-p][0] = EmptySquare;
9141 if((int)board[BOARD_HEIGHT-1-p][1] < 0)
9142 board[BOARD_HEIGHT-1-p][1] = 0;
9145 if (captured != EmptySquare && gameInfo.holdingsSize > 0
9146 && gameInfo.variant != VariantBughouse && gameInfo.variant != VariantSChess ) {
9147 /* [HGM] holdings: Add to holdings, if holdings exist */
9148 if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) {
9149 // [HGM] superchess: suppress flipping color of captured pieces by reverse pre-flip
9150 captured = (int) captured >= (int) BlackPawn ? BLACK_TO_WHITE captured : WHITE_TO_BLACK captured;
9153 if (p >= (int) BlackPawn) {
9154 p -= (int)BlackPawn;
9155 if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
9156 /* in Shogi restore piece to its original first */
9157 captured = (ChessSquare) (DEMOTED captured);
9160 p = PieceToNumber((ChessSquare)p);
9161 if(p >= gameInfo.holdingsSize) { p = 0; captured = BlackPawn; }
9162 board[p][BOARD_WIDTH-2]++;
9163 board[p][BOARD_WIDTH-1] = BLACK_TO_WHITE captured;
9165 p -= (int)WhitePawn;
9166 if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
9167 captured = (ChessSquare) (DEMOTED captured);
9170 p = PieceToNumber((ChessSquare)p);
9171 if(p >= gameInfo.holdingsSize) { p = 0; captured = WhitePawn; }
9172 board[BOARD_HEIGHT-1-p][1]++;
9173 board[BOARD_HEIGHT-1-p][0] = WHITE_TO_BLACK captured;
9176 } else if (gameInfo.variant == VariantAtomic) {
9177 if (captured != EmptySquare) {
9179 for (y = toY-1; y <= toY+1; y++) {
9180 for (x = toX-1; x <= toX+1; x++) {
9181 if (y >= 0 && y < BOARD_HEIGHT && x >= BOARD_LEFT && x < BOARD_RGHT &&
9182 board[y][x] != WhitePawn && board[y][x] != BlackPawn) {
9183 board[y][x] = EmptySquare;
9187 board[toY][toX] = EmptySquare;
9190 if(gameInfo.variant == VariantSChess && promoChar != NULLCHAR && promoChar != '=' && piece != WhitePawn && piece != BlackPawn) {
9191 board[fromY][fromX] = CharToPiece(piece < BlackPawn ? ToUpper(promoChar) : ToLower(promoChar)); // S-Chess gating
9193 if(promoChar == '+') {
9194 /* [HGM] Shogi-style promotions, to piece implied by original (Might overwrite orinary Pawn promotion) */
9195 board[toY][toX] = (ChessSquare) (PROMOTED piece);
9196 } else if(!appData.testLegality && promoChar != NULLCHAR && promoChar != '=') { // without legality testing, unconditionally believe promoChar
9197 board[toY][toX] = CharToPiece(piece < BlackPawn ? ToUpper(promoChar) : ToLower(promoChar));
9199 if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat)
9200 && promoChar != NULLCHAR && gameInfo.holdingsSize) {
9201 // [HGM] superchess: take promotion piece out of holdings
9202 int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
9203 if((int)piece < (int)BlackPawn) { // determine stm from piece color
9204 if(!--board[k][BOARD_WIDTH-2])
9205 board[k][BOARD_WIDTH-1] = EmptySquare;
9207 if(!--board[BOARD_HEIGHT-1-k][1])
9208 board[BOARD_HEIGHT-1-k][0] = EmptySquare;
9214 /* Updates forwardMostMove */
9216 MakeMove(fromX, fromY, toX, toY, promoChar)
9217 int fromX, fromY, toX, toY;
9220 // forwardMostMove++; // [HGM] bare: moved downstream
9222 if(serverMoves != NULL) { /* [HGM] write moves on file for broadcasting (should be separate routine, really) */
9223 int timeLeft; static int lastLoadFlag=0; int king, piece;
9224 piece = boards[forwardMostMove][fromY][fromX];
9225 king = piece < (int) BlackPawn ? WhiteKing : BlackKing;
9226 if(gameInfo.variant == VariantKnightmate)
9227 king += (int) WhiteUnicorn - (int) WhiteKing;
9228 if(forwardMostMove == 0) {
9230 fprintf(serverMoves, "%s;", second.tidy);
9231 fprintf(serverMoves, "%s;", first.tidy);
9232 if(!blackPlaysFirst)
9233 fprintf(serverMoves, "%s;", second.tidy);
9234 } else fprintf(serverMoves, loadFlag|lastLoadFlag ? ":" : ";");
9235 lastLoadFlag = loadFlag;
9237 fprintf(serverMoves, "%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+toY);
9238 // print castling suffix
9239 if( toY == fromY && piece == king ) {
9241 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_RGHT-1, ONE+fromY, AAA+toX-1,ONE+toY);
9243 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_LEFT, ONE+fromY, AAA+toX+1,ONE+toY);
9246 if( (boards[forwardMostMove][fromY][fromX] == WhitePawn ||
9247 boards[forwardMostMove][fromY][fromX] == BlackPawn ) &&
9248 boards[forwardMostMove][toY][toX] == EmptySquare
9249 && fromX != toX && fromY != toY)
9250 fprintf(serverMoves, ":%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+fromY);
9252 if(promoChar != NULLCHAR)
9253 fprintf(serverMoves, ":%c:%c%c", promoChar, AAA+toX, ONE+toY);
9255 fprintf(serverMoves, "/%d/%d",
9256 pvInfoList[forwardMostMove].depth, pvInfoList[forwardMostMove].score);
9257 if(forwardMostMove+1 & 1) timeLeft = whiteTimeRemaining/1000;
9258 else timeLeft = blackTimeRemaining/1000;
9259 fprintf(serverMoves, "/%d", timeLeft);
9261 fflush(serverMoves);
9264 if (forwardMostMove+1 > framePtr) { // [HGM] vari: do not run into saved variations
9265 DisplayFatalError(_("Game too long; increase MAX_MOVES and recompile"),
9269 UnLoadPV(); // [HGM] pv: if we are looking at a PV, abort this
9270 if (commentList[forwardMostMove+1] != NULL) {
9271 free(commentList[forwardMostMove+1]);
9272 commentList[forwardMostMove+1] = NULL;
9274 CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
9275 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[forwardMostMove+1]);
9276 // forwardMostMove++; // [HGM] bare: moved to after ApplyMove, to make sure clock interrupt finds complete board
9277 SwitchClocks(forwardMostMove+1); // [HGM] race: incrementing move nr inside
9278 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
9279 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
9280 gameInfo.result = GameUnfinished;
9281 if (gameInfo.resultDetails != NULL) {
9282 free(gameInfo.resultDetails);
9283 gameInfo.resultDetails = NULL;
9285 CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar,
9286 moveList[forwardMostMove - 1]);
9287 (void) CoordsToAlgebraic(boards[forwardMostMove - 1],
9288 PosFlags(forwardMostMove - 1),
9289 fromY, fromX, toY, toX, promoChar,
9290 parseList[forwardMostMove - 1]);
9291 switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
9297 if(gameInfo.variant != VariantShogi)
9298 strcat(parseList[forwardMostMove - 1], "+");
9302 strcat(parseList[forwardMostMove - 1], "#");
9305 if (appData.debugMode) {
9306 fprintf(debugFP, "move: %s, parse: %s (%c)\n", moveList[forwardMostMove-1], parseList[forwardMostMove-1], moveList[forwardMostMove-1][4]);
9311 /* Updates currentMove if not pausing */
9313 ShowMove(fromX, fromY, toX, toY)
9315 int instant = (gameMode == PlayFromGameFile) ?
9316 (matchMode || (appData.timeDelay == 0 && !pausing)) : pausing;
9317 if(appData.noGUI) return;
9318 if (!pausing || gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
9320 if (forwardMostMove == currentMove + 1) {
9321 AnimateMove(boards[forwardMostMove - 1],
9322 fromX, fromY, toX, toY);
9324 if (appData.highlightLastMove) {
9325 SetHighlights(fromX, fromY, toX, toY);
9328 currentMove = forwardMostMove;
9331 if (instant) return;
9333 DisplayMove(currentMove - 1);
9334 DrawPosition(FALSE, boards[currentMove]);
9335 DisplayBothClocks();
9336 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
9337 DisplayBook(currentMove);
9340 void SendEgtPath(ChessProgramState *cps)
9341 { /* [HGM] EGT: match formats given in feature with those given by user, and send info for each match */
9342 char buf[MSG_SIZ], name[MSG_SIZ], *p;
9344 if((p = cps->egtFormats) == NULL || appData.egtFormats == NULL) return;
9347 char c, *q = name+1, *r, *s;
9349 name[0] = ','; // extract next format name from feature and copy with prefixed ','
9350 while(*p && *p != ',') *q++ = *p++;
9352 if( appData.defaultPathEGTB && appData.defaultPathEGTB[0] &&
9353 strcmp(name, ",nalimov:") == 0 ) {
9354 // take nalimov path from the menu-changeable option first, if it is defined
9355 snprintf(buf, MSG_SIZ, "egtpath nalimov %s\n", appData.defaultPathEGTB);
9356 SendToProgram(buf,cps); // send egtbpath command for nalimov
9358 if( (s = StrStr(appData.egtFormats, name+1)) == appData.egtFormats ||
9359 (s = StrStr(appData.egtFormats, name)) != NULL) {
9360 // format name occurs amongst user-supplied formats, at beginning or immediately after comma
9361 s = r = StrStr(s, ":") + 1; // beginning of path info
9362 while(*r && *r != ',') r++; // path info is everything upto next ';' or end of string
9363 c = *r; *r = 0; // temporarily null-terminate path info
9364 *--q = 0; // strip of trailig ':' from name
9365 snprintf(buf, MSG_SIZ, "egtpath %s %s\n", name+1, s);
9367 SendToProgram(buf,cps); // send egtbpath command for this format
9369 if(*p == ',') p++; // read away comma to position for next format name
9374 InitChessProgram(cps, setup)
9375 ChessProgramState *cps;
9376 int setup; /* [HGM] needed to setup FRC opening position */
9378 char buf[MSG_SIZ], b[MSG_SIZ]; int overruled;
9379 if (appData.noChessProgram) return;
9380 hintRequested = FALSE;
9381 bookRequested = FALSE;
9383 /* [HGM] some new WB protocol commands to configure engine are sent now, if engine supports them */
9384 /* moved to before sending initstring in 4.3.15, so Polyglot can delay UCI 'isready' to recepton of 'new' */
9385 if(cps->memSize) { /* [HGM] memory */
9386 snprintf(buf, MSG_SIZ, "memory %d\n", appData.defaultHashSize + appData.defaultCacheSizeEGTB);
9387 SendToProgram(buf, cps);
9389 SendEgtPath(cps); /* [HGM] EGT */
9390 if(cps->maxCores) { /* [HGM] SMP: (protocol specified must be last settings command before new!) */
9391 snprintf(buf, MSG_SIZ, "cores %d\n", appData.smpCores);
9392 SendToProgram(buf, cps);
9395 SendToProgram(cps->initString, cps);
9396 if (gameInfo.variant != VariantNormal &&
9397 gameInfo.variant != VariantLoadable
9398 /* [HGM] also send variant if board size non-standard */
9399 || gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0
9401 char *v = VariantName(gameInfo.variant);
9402 if (cps->protocolVersion != 1 && StrStr(cps->variants, v) == NULL) {
9403 /* [HGM] in protocol 1 we have to assume all variants valid */
9404 snprintf(buf, MSG_SIZ, _("Variant %s not supported by %s"), v, cps->tidy);
9405 DisplayFatalError(buf, 0, 1);
9409 /* [HGM] make prefix for non-standard board size. Awkward testing... */
9410 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
9411 if( gameInfo.variant == VariantXiangqi )
9412 overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 10 || gameInfo.holdingsSize != 0;
9413 if( gameInfo.variant == VariantShogi )
9414 overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 9 || gameInfo.holdingsSize != 7;
9415 if( gameInfo.variant == VariantBughouse || gameInfo.variant == VariantCrazyhouse )
9416 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 5;
9417 if( gameInfo.variant == VariantCapablanca || gameInfo.variant == VariantCapaRandom ||
9418 gameInfo.variant == VariantGothic || gameInfo.variant == VariantFalcon || gameInfo.variant == VariantJanus )
9419 overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
9420 if( gameInfo.variant == VariantCourier )
9421 overruled = gameInfo.boardWidth != 12 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
9422 if( gameInfo.variant == VariantSuper )
9423 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
9424 if( gameInfo.variant == VariantGreat )
9425 overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
9426 if( gameInfo.variant == VariantSChess )
9427 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 7;
9430 snprintf(b, MSG_SIZ, "%dx%d+%d_%s", gameInfo.boardWidth, gameInfo.boardHeight,
9431 gameInfo.holdingsSize, VariantName(gameInfo.variant)); // cook up sized variant name
9432 /* [HGM] varsize: try first if this defiant size variant is specifically known */
9433 if(StrStr(cps->variants, b) == NULL) {
9434 // specific sized variant not known, check if general sizing allowed
9435 if (cps->protocolVersion != 1) { // for protocol 1 we cannot check and hope for the best
9436 if(StrStr(cps->variants, "boardsize") == NULL) {
9437 snprintf(buf, MSG_SIZ, "Board size %dx%d+%d not supported by %s",
9438 gameInfo.boardWidth, gameInfo.boardHeight, gameInfo.holdingsSize, cps->tidy);
9439 DisplayFatalError(buf, 0, 1);
9442 /* [HGM] here we really should compare with the maximum supported board size */
9445 } else snprintf(b, MSG_SIZ,"%s", VariantName(gameInfo.variant));
9446 snprintf(buf, MSG_SIZ, "variant %s\n", b);
9447 SendToProgram(buf, cps);
9449 currentlyInitializedVariant = gameInfo.variant;
9451 /* [HGM] send opening position in FRC to first engine */
9453 SendToProgram("force\n", cps);
9455 /* engine is now in force mode! Set flag to wake it up after first move. */
9456 setboardSpoiledMachineBlack = 1;
9460 snprintf(buf, sizeof(buf), "ics %s\n", appData.icsActive ? appData.icsHost : "-");
9461 SendToProgram(buf, cps);
9463 cps->maybeThinking = FALSE;
9464 cps->offeredDraw = 0;
9465 if (!appData.icsActive) {
9466 SendTimeControl(cps, movesPerSession, timeControl,
9467 timeIncrement, appData.searchDepth,
9470 if (appData.showThinking
9471 // [HGM] thinking: four options require thinking output to be sent
9472 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
9474 SendToProgram("post\n", cps);
9476 SendToProgram("hard\n", cps);
9477 if (!appData.ponderNextMove) {
9478 /* Warning: "easy" is a toggle in GNU Chess, so don't send
9479 it without being sure what state we are in first. "hard"
9480 is not a toggle, so that one is OK.
9482 SendToProgram("easy\n", cps);
9485 snprintf(buf, MSG_SIZ, "ping %d\n", ++cps->lastPing);
9486 SendToProgram(buf, cps);
9488 cps->initDone = TRUE;
9493 StartChessProgram(cps)
9494 ChessProgramState *cps;
9499 if (appData.noChessProgram) return;
9500 cps->initDone = FALSE;
9502 if (strcmp(cps->host, "localhost") == 0) {
9503 err = StartChildProcess(cps->program, cps->dir, &cps->pr);
9504 } else if (*appData.remoteShell == NULLCHAR) {
9505 err = OpenRcmd(cps->host, appData.remoteUser, cps->program, &cps->pr);
9507 if (*appData.remoteUser == NULLCHAR) {
9508 snprintf(buf, sizeof(buf), "%s %s %s", appData.remoteShell, cps->host,
9511 snprintf(buf, sizeof(buf), "%s %s -l %s %s", appData.remoteShell,
9512 cps->host, appData.remoteUser, cps->program);
9514 err = StartChildProcess(buf, "", &cps->pr);
9518 snprintf(buf, MSG_SIZ, _("Startup failure on '%s'"), cps->program);
9519 DisplayError(buf, err); // [HGM] bit of a rough kludge: ignore failure, (which XBoard would do anyway), and let I/O discover it
9520 if(cps != &first) return;
9521 appData.noChessProgram = TRUE;
9524 // DisplayFatalError(buf, err, 1);
9525 // cps->pr = NoProc;
9530 cps->isr = AddInputSource(cps->pr, TRUE, ReceiveFromProgram, cps);
9531 if (cps->protocolVersion > 1) {
9532 snprintf(buf, MSG_SIZ, "xboard\nprotover %d\n", cps->protocolVersion);
9533 cps->nrOptions = 0; // [HGM] options: clear all engine-specific options
9534 cps->comboCnt = 0; // and values of combo boxes
9535 SendToProgram(buf, cps);
9537 SendToProgram("xboard\n", cps);
9542 TwoMachinesEventIfReady P((void))
9544 static int curMess = 0;
9545 if (first.lastPing != first.lastPong) {
9546 if(curMess != 1) DisplayMessage("", _("Waiting for first chess program")); curMess = 1;
9547 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
9550 if (second.lastPing != second.lastPong) {
9551 if(curMess != 2) DisplayMessage("", _("Waiting for second chess program")); curMess = 2;
9552 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
9555 DisplayMessage("", ""); curMess = 0;
9561 MakeName(char *template)
9565 static char buf[MSG_SIZ];
9569 clock = time((time_t *)NULL);
9570 tm = localtime(&clock);
9572 while(*p++ = *template++) if(p[-1] == '%') {
9573 switch(*template++) {
9574 case 0: *p = 0; return buf;
9575 case 'Y': i = tm->tm_year+1900; break;
9576 case 'y': i = tm->tm_year-100; break;
9577 case 'M': i = tm->tm_mon+1; break;
9578 case 'd': i = tm->tm_mday; break;
9579 case 'h': i = tm->tm_hour; break;
9580 case 'm': i = tm->tm_min; break;
9581 case 's': i = tm->tm_sec; break;
9584 snprintf(p-1, MSG_SIZ-10 - (p - buf), "%02d", i); p += strlen(p);
9590 CountPlayers(char *p)
9593 while(p = strchr(p, '\n')) p++, n++; // count participants
9598 WriteTourneyFile(char *results)
9599 { // write tournament parameters on tourneyFile; on success return the stream pointer for closing
9600 FILE *f = fopen(appData.tourneyFile, "w");
9601 if(f == NULL) DisplayError(_("Could not write on tourney file"), 0); else {
9602 // create a file with tournament description
9603 fprintf(f, "-participants {%s}\n", appData.participants);
9604 fprintf(f, "-tourneyType %d\n", appData.tourneyType);
9605 fprintf(f, "-tourneyCycles %d\n", appData.tourneyCycles);
9606 fprintf(f, "-defaultMatchGames %d\n", appData.defaultMatchGames);
9607 fprintf(f, "-syncAfterRound %s\n", appData.roundSync ? "true" : "false");
9608 fprintf(f, "-syncAfterCycle %s\n", appData.cycleSync ? "true" : "false");
9609 fprintf(f, "-saveGameFile \"%s\"\n", appData.saveGameFile);
9610 fprintf(f, "-loadGameFile \"%s\"\n", appData.loadGameFile);
9611 fprintf(f, "-loadGameIndex %d\n", appData.loadGameIndex);
9612 fprintf(f, "-loadPositionFile \"%s\"\n", appData.loadPositionFile);
9613 fprintf(f, "-loadPositionIndex %d\n", appData.loadPositionIndex);
9614 fprintf(f, "-rewindIndex %d\n", appData.rewindIndex);
9616 fprintf(f, "-searchTime \"%s\"\n", appData.searchTime);
9618 fprintf(f, "-mps %d\n", appData.movesPerSession);
9619 fprintf(f, "-tc %s\n", appData.timeControl);
9620 fprintf(f, "-inc %.2f\n", appData.timeIncrement);
9622 fprintf(f, "-results \"%s\"\n", results);
9628 CreateTourney(char *name)
9631 if(name[0] == NULLCHAR) {
9632 if(appData.participants[0])
9633 DisplayError(_("You must supply a tournament file,\nfor storing the tourney progress"), 0);
9636 f = fopen(name, "r");
9637 if(f) { // file exists
9638 ASSIGN(appData.tourneyFile, name);
9639 ParseArgsFromFile(f); // parse it
9641 if(!appData.participants[0]) return 0; // ignore tourney file if non-existing & no participants
9642 if(CountPlayers(appData.participants) < (appData.tourneyType>0 ? appData.tourneyType+1 : 2)) {
9643 DisplayError(_("Not enough participants"), 0);
9646 ASSIGN(appData.tourneyFile, name);
9647 if((f = WriteTourneyFile("")) == NULL) return 0;
9650 appData.noChessProgram = FALSE;
9651 appData.clockMode = TRUE;
9656 #define MAXENGINES 1000
9657 char *command[MAXENGINES], *mnemonic[MAXENGINES];
9659 void NamesToList(char *names, char **engineList, char **engineMnemonic)
9661 char buf[MSG_SIZ], *p, *q;
9665 while(*p && *p != '\n') *q++ = *p++;
9667 if(engineList[i]) free(engineList[i]);
9668 engineList[i] = strdup(buf);
9670 TidyProgramName(engineList[i], "localhost", buf);
9671 if(engineMnemonic[i]) free(engineMnemonic[i]);
9672 if((q = strstr(engineList[i]+2, "variant")) && q[-2]== ' ' && (q[-1]=='/' || q[-1]=='-') && (q[7]==' ' || q[7]=='=')) {
9674 sscanf(q + 8, "%s", buf + strlen(buf));
9677 engineMnemonic[i] = strdup(buf);
9679 if(i > MAXENGINES - 2) break;
9681 engineList[i] = NULL;
9684 // following implemented as macro to avoid type limitations
9685 #define SWAP(item, temp) temp = appData.item[0]; appData.item[0] = appData.item[n]; appData.item[n] = temp;
9687 void SwapEngines(int n)
9688 { // swap settings for first engine and other engine (so far only some selected options)
9693 SWAP(chessProgram, p)
9695 SWAP(hasOwnBookUCI, h)
9696 SWAP(protocolVersion, h)
9698 SWAP(scoreIsAbsolute, h)
9705 SetPlayer(int player)
9706 { // [HGM] find the engine line of the partcipant given by number, and parse its options.
9708 char buf[MSG_SIZ], *engineName, *p = appData.participants;
9709 for(i=0; i<player; i++) p = strchr(p, '\n') + 1;
9710 engineName = strdup(p); if(p = strchr(engineName, '\n')) *p = NULLCHAR;
9711 for(i=1; command[i]; i++) if(!strcmp(mnemonic[i], engineName)) break;
9713 snprintf(buf, MSG_SIZ, "-fcp %s", command[i]);
9714 ParseArgsFromString(resetOptions); appData.fenOverride[0] = NULL;
9715 ParseArgsFromString(buf);
9721 Pairing(int nr, int nPlayers, int *whitePlayer, int *blackPlayer, int *syncInterval)
9722 { // determine players from game number
9723 int curCycle, curRound, curPairing, gamesPerCycle, gamesPerRound, roundsPerCycle=1, pairingsPerRound=1;
9725 if(appData.tourneyType == 0) {
9726 roundsPerCycle = (nPlayers - 1) | 1;
9727 pairingsPerRound = nPlayers / 2;
9728 } else if(appData.tourneyType > 0) {
9729 roundsPerCycle = nPlayers - appData.tourneyType;
9730 pairingsPerRound = appData.tourneyType;
9732 gamesPerRound = pairingsPerRound * appData.defaultMatchGames;
9733 gamesPerCycle = gamesPerRound * roundsPerCycle;
9734 appData.matchGames = gamesPerCycle * appData.tourneyCycles - 1; // fake like all games are one big match
9735 curCycle = nr / gamesPerCycle; nr %= gamesPerCycle;
9736 curRound = nr / gamesPerRound; nr %= gamesPerRound;
9737 curPairing = nr / appData.defaultMatchGames; nr %= appData.defaultMatchGames;
9738 matchGame = nr + curCycle * appData.defaultMatchGames + 1; // fake game nr that loads correct game or position from file
9739 roundNr = (curCycle * roundsPerCycle + curRound) * appData.defaultMatchGames + nr + 1;
9741 if(appData.cycleSync) *syncInterval = gamesPerCycle;
9742 if(appData.roundSync) *syncInterval = gamesPerRound;
9744 if(appData.debugMode) fprintf(debugFP, "cycle=%d, round=%d, pairing=%d curGame=%d\n", curCycle, curRound, curPairing, matchGame);
9746 if(appData.tourneyType == 0) {
9747 if(curPairing == (nPlayers-1)/2 ) {
9748 *whitePlayer = curRound;
9749 *blackPlayer = nPlayers - 1; // this is the 'bye' when nPlayer is odd
9751 *whitePlayer = curRound - pairingsPerRound + curPairing;
9752 if(*whitePlayer < 0) *whitePlayer += nPlayers-1+(nPlayers&1);
9753 *blackPlayer = curRound + pairingsPerRound - curPairing;
9754 if(*blackPlayer >= nPlayers-1+(nPlayers&1)) *blackPlayer -= nPlayers-1+(nPlayers&1);
9756 } else if(appData.tourneyType > 0) {
9757 *whitePlayer = curPairing;
9758 *blackPlayer = curRound + appData.tourneyType;
9761 // take care of white/black alternation per round.
9762 // For cycles and games this is already taken care of by default, derived from matchGame!
9763 return curRound & 1;
9767 NextTourneyGame(int nr, int *swapColors)
9768 { // !!!major kludge!!! fiddle appData settings to get everything in order for next tourney game
9770 int whitePlayer, blackPlayer, firstBusy=1000000000, syncInterval = 0, nPlayers;
9772 if(appData.tourneyFile[0] == NULLCHAR) return 1; // no tourney, always allow next game
9773 tf = fopen(appData.tourneyFile, "r");
9774 if(tf == NULL) { DisplayFatalError(_("Bad tournament file"), 0, 1); return 0; }
9775 ParseArgsFromFile(tf); fclose(tf);
9776 InitTimeControls(); // TC might be altered from tourney file
9778 nPlayers = CountPlayers(appData.participants); // count participants
9779 if(appData.tourneyType < 0 && appData.pairingEngine[0]) {
9780 if(nr>=0 && !pairingReceived) {
9782 if(pairing.pr == NoProc) StartChessProgram(&pairing);
9783 snprintf(buf, 1<<16, "results %d %s\n", nPlayers, appData.results);
9784 SendToProgram(buf, &pairing);
9785 snprintf(buf, 1<<16, "pairing %d\n", nr+1);
9786 SendToProgram(buf, &pairing);
9787 return 0; // wait for pairing engine to answer (which causes NextTourneyGame to be called again...
9789 pairingReceived = 0; // ... so we continue here
9790 syncInterval = nPlayers/2; *swapColors = 0;
9791 appData.matchGames = appData.tourneyCycles * syncInterval - 1;
9792 whitePlayer = savedWhitePlayer-1; blackPlayer = savedBlackPlayer-1;
9793 matchGame = 1; roundNr = nr / syncInterval + 1;
9795 *swapColors = Pairing(nr<0 ? 0 : nr, nPlayers, &whitePlayer, &blackPlayer, &syncInterval);
9798 p = q = appData.results;
9799 while(*q) if(*q++ == '*' || q[-1] == ' ') { firstBusy = q - p - 1; break; }
9800 if(firstBusy/syncInterval < (nextGame/syncInterval)) {
9801 DisplayMessage(_("Waiting for other game(s)"),"");
9802 waitingForGame = TRUE;
9803 ScheduleDelayedEvent(NextMatchGame, 1000); // wait for all games of previous round to finish
9806 waitingForGame = FALSE;
9809 if(first.pr != NoProc) return 1; // engines already loaded
9811 // redefine engines, engine dir, etc.
9812 NamesToList(firstChessProgramNames, command, mnemonic); // get mnemonics of installed engines
9813 SetPlayer(whitePlayer); // find white player amongst it, and parse its engine line
9815 SetPlayer(blackPlayer); // find black player amongst it, and parse its engine line
9816 SwapEngines(1); // and make that valid for second engine by swapping
9817 InitEngine(&first, 0); // initialize ChessProgramStates based on new settings.
9818 InitEngine(&second, 1);
9819 CommonEngineInit(); // after this TwoMachinesEvent will create correct engine processes
9825 { // performs game initialization that does not invoke engines, and then tries to start the game
9826 int firstWhite, swapColors = 0;
9827 if(!NextTourneyGame(nextGame, &swapColors)) return; // this sets matchGame, -fcp / -scp and other options for next game, if needed
9828 firstWhite = appData.firstPlaysBlack ^ (matchGame & 1 | appData.sameColorGames > 1); // non-incremental default
9829 firstWhite ^= swapColors; // reverses if NextTourneyGame says we are in an odd round
9830 first.twoMachinesColor = firstWhite ? "white\n" : "black\n"; // perform actual color assignement
9831 second.twoMachinesColor = firstWhite ? "black\n" : "white\n";
9832 appData.noChessProgram = (first.pr == NoProc); // kludge to prevent Reset from starting up chess program
9833 Reset(FALSE, first.pr != NoProc);
9834 appData.noChessProgram = FALSE;
9835 if(!LoadGameOrPosition(matchGame)) return; // setup game; abort when bad game/pos file
9839 void UserAdjudicationEvent( int result )
9841 ChessMove gameResult = GameIsDrawn;
9844 gameResult = WhiteWins;
9846 else if( result < 0 ) {
9847 gameResult = BlackWins;
9850 if( gameMode == TwoMachinesPlay ) {
9851 GameEnds( gameResult, "User adjudication", GE_XBOARD );
9856 // [HGM] save: calculate checksum of game to make games easily identifiable
9857 int StringCheckSum(char *s)
9860 if(s==NULL) return 0;
9861 while(*s) i = i*259 + *s++;
9868 for(i=backwardMostMove; i<forwardMostMove; i++) {
9869 sum += pvInfoList[i].depth;
9870 sum += StringCheckSum(parseList[i]);
9871 sum += StringCheckSum(commentList[i]);
9874 if(i>1 && sum==0) sum++; // make sure never zero for non-empty game
9875 return sum + StringCheckSum(commentList[i]);
9876 } // end of save patch
9879 GameEnds(result, resultDetails, whosays)
9881 char *resultDetails;
9884 GameMode nextGameMode;
9886 char buf[MSG_SIZ], popupRequested = 0, *ranking = NULL;
9888 if(endingGame) return; /* [HGM] crash: forbid recursion */
9890 if(twoBoards) { // [HGM] dual: switch back to one board
9891 twoBoards = partnerUp = 0; InitDrawingSizes(-2, 0);
9892 DrawPosition(TRUE, partnerBoard); // observed game becomes foreground
9894 if (appData.debugMode) {
9895 fprintf(debugFP, "GameEnds(%d, %s, %d)\n",
9896 result, resultDetails ? resultDetails : "(null)", whosays);
9899 fromX = fromY = -1; // [HGM] abort any move the user is entering.
9901 if (appData.icsActive && (whosays == GE_ENGINE || whosays >= GE_ENGINE1)) {
9902 /* If we are playing on ICS, the server decides when the
9903 game is over, but the engine can offer to draw, claim
9907 if (appData.zippyPlay && first.initDone) {
9908 if (result == GameIsDrawn) {
9909 /* In case draw still needs to be claimed */
9910 SendToICS(ics_prefix);
9911 SendToICS("draw\n");
9912 } else if (StrCaseStr(resultDetails, "resign")) {
9913 SendToICS(ics_prefix);
9914 SendToICS("resign\n");
9918 endingGame = 0; /* [HGM] crash */
9922 /* If we're loading the game from a file, stop */
9923 if (whosays == GE_FILE) {
9924 (void) StopLoadGameTimer();
9928 /* Cancel draw offers */
9929 first.offeredDraw = second.offeredDraw = 0;
9931 /* If this is an ICS game, only ICS can really say it's done;
9932 if not, anyone can. */
9933 isIcsGame = (gameMode == IcsPlayingWhite ||
9934 gameMode == IcsPlayingBlack ||
9935 gameMode == IcsObserving ||
9936 gameMode == IcsExamining);
9938 if (!isIcsGame || whosays == GE_ICS) {
9939 /* OK -- not an ICS game, or ICS said it was done */
9941 if (!isIcsGame && !appData.noChessProgram)
9942 SetUserThinkingEnables();
9944 /* [HGM] if a machine claims the game end we verify this claim */
9945 if(gameMode == TwoMachinesPlay && appData.testClaims) {
9946 if(appData.testLegality && whosays >= GE_ENGINE1 ) {
9948 ChessMove trueResult = (ChessMove) -1;
9950 claimer = whosays == GE_ENGINE1 ? /* color of claimer */
9951 first.twoMachinesColor[0] :
9952 second.twoMachinesColor[0] ;
9954 // [HGM] losers: because the logic is becoming a bit hairy, determine true result first
9955 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_CHECKMATE) {
9956 /* [HGM] verify: engine mate claims accepted if they were flagged */
9957 trueResult = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins;
9959 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_WINS) { // added code for games where being mated is a win
9960 /* [HGM] verify: engine mate claims accepted if they were flagged */
9961 trueResult = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
9963 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_STALEMATE) { // only used to indicate draws now
9964 trueResult = GameIsDrawn; // default; in variants where stalemate loses, Status is CHECKMATE
9967 // now verify win claims, but not in drop games, as we don't understand those yet
9968 if( (gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper
9969 || gameInfo.variant == VariantGreat) &&
9970 (result == WhiteWins && claimer == 'w' ||
9971 result == BlackWins && claimer == 'b' ) ) { // case to verify: engine claims own win
9972 if (appData.debugMode) {
9973 fprintf(debugFP, "result=%d sp=%d move=%d\n",
9974 result, (signed char)boards[forwardMostMove][EP_STATUS], forwardMostMove);
9976 if(result != trueResult) {
9977 snprintf(buf, MSG_SIZ, "False win claim: '%s'", resultDetails);
9978 result = claimer == 'w' ? BlackWins : WhiteWins;
9979 resultDetails = buf;
9982 if( result == GameIsDrawn && (signed char)boards[forwardMostMove][EP_STATUS] > EP_DRAWS
9983 && (forwardMostMove <= backwardMostMove ||
9984 (signed char)boards[forwardMostMove-1][EP_STATUS] > EP_DRAWS ||
9985 (claimer=='b')==(forwardMostMove&1))
9987 /* [HGM] verify: draws that were not flagged are false claims */
9988 snprintf(buf, MSG_SIZ, "False draw claim: '%s'", resultDetails);
9989 result = claimer == 'w' ? BlackWins : WhiteWins;
9990 resultDetails = buf;
9992 /* (Claiming a loss is accepted no questions asked!) */
9994 /* [HGM] bare: don't allow bare King to win */
9995 if((gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat)
9996 && gameInfo.variant != VariantLosers && gameInfo.variant != VariantGiveaway
9997 && gameInfo.variant != VariantSuicide // [HGM] losers: except in losers, of course...
9998 && result != GameIsDrawn)
9999 { int i, j, k=0, color = (result==WhiteWins ? (int)WhitePawn : (int)BlackPawn);
10000 for(j=BOARD_LEFT; j<BOARD_RGHT; j++) for(i=0; i<BOARD_HEIGHT; i++) {
10001 int p = (signed char)boards[forwardMostMove][i][j] - color;
10002 if(p >= 0 && p <= (int)WhiteKing) k++;
10004 if (appData.debugMode) {
10005 fprintf(debugFP, "GE(%d, %s, %d) bare king k=%d color=%d\n",
10006 result, resultDetails ? resultDetails : "(null)", whosays, k, color);
10009 result = GameIsDrawn;
10010 snprintf(buf, MSG_SIZ, "%s but bare king", resultDetails);
10011 resultDetails = buf;
10017 if(serverMoves != NULL && !loadFlag) { char c = '=';
10018 if(result==WhiteWins) c = '+';
10019 if(result==BlackWins) c = '-';
10020 if(resultDetails != NULL)
10021 fprintf(serverMoves, ";%c;%s\n", c, resultDetails);
10023 if (resultDetails != NULL) {
10024 gameInfo.result = result;
10025 gameInfo.resultDetails = StrSave(resultDetails);
10027 /* display last move only if game was not loaded from file */
10028 if ((whosays != GE_FILE) && (currentMove == forwardMostMove))
10029 DisplayMove(currentMove - 1);
10031 if (forwardMostMove != 0) {
10032 if (gameMode != PlayFromGameFile && gameMode != EditGame
10033 && lastSavedGame != GameCheckSum() // [HGM] save: suppress duplicates
10035 if (*appData.saveGameFile != NULLCHAR) {
10036 SaveGameToFile(appData.saveGameFile, TRUE);
10037 } else if (appData.autoSaveGames) {
10040 if (*appData.savePositionFile != NULLCHAR) {
10041 SavePositionToFile(appData.savePositionFile);
10046 /* Tell program how game ended in case it is learning */
10047 /* [HGM] Moved this to after saving the PGN, just in case */
10048 /* engine died and we got here through time loss. In that */
10049 /* case we will get a fatal error writing the pipe, which */
10050 /* would otherwise lose us the PGN. */
10051 /* [HGM] crash: not needed anymore, but doesn't hurt; */
10052 /* output during GameEnds should never be fatal anymore */
10053 if (gameMode == MachinePlaysWhite ||
10054 gameMode == MachinePlaysBlack ||
10055 gameMode == TwoMachinesPlay ||
10056 gameMode == IcsPlayingWhite ||
10057 gameMode == IcsPlayingBlack ||
10058 gameMode == BeginningOfGame) {
10060 snprintf(buf, MSG_SIZ, "result %s {%s}\n", PGNResult(result),
10062 if (first.pr != NoProc) {
10063 SendToProgram(buf, &first);
10065 if (second.pr != NoProc &&
10066 gameMode == TwoMachinesPlay) {
10067 SendToProgram(buf, &second);
10072 if (appData.icsActive) {
10073 if (appData.quietPlay &&
10074 (gameMode == IcsPlayingWhite ||
10075 gameMode == IcsPlayingBlack)) {
10076 SendToICS(ics_prefix);
10077 SendToICS("set shout 1\n");
10079 nextGameMode = IcsIdle;
10080 ics_user_moved = FALSE;
10081 /* clean up premove. It's ugly when the game has ended and the
10082 * premove highlights are still on the board.
10085 gotPremove = FALSE;
10086 ClearPremoveHighlights();
10087 DrawPosition(FALSE, boards[currentMove]);
10089 if (whosays == GE_ICS) {
10092 if (gameMode == IcsPlayingWhite)
10094 else if(gameMode == IcsPlayingBlack)
10095 PlayIcsLossSound();
10098 if (gameMode == IcsPlayingBlack)
10100 else if(gameMode == IcsPlayingWhite)
10101 PlayIcsLossSound();
10104 PlayIcsDrawSound();
10107 PlayIcsUnfinishedSound();
10110 } else if (gameMode == EditGame ||
10111 gameMode == PlayFromGameFile ||
10112 gameMode == AnalyzeMode ||
10113 gameMode == AnalyzeFile) {
10114 nextGameMode = gameMode;
10116 nextGameMode = EndOfGame;
10121 nextGameMode = gameMode;
10124 if (appData.noChessProgram) {
10125 gameMode = nextGameMode;
10127 endingGame = 0; /* [HGM] crash */
10132 /* Put first chess program into idle state */
10133 if (first.pr != NoProc &&
10134 (gameMode == MachinePlaysWhite ||
10135 gameMode == MachinePlaysBlack ||
10136 gameMode == TwoMachinesPlay ||
10137 gameMode == IcsPlayingWhite ||
10138 gameMode == IcsPlayingBlack ||
10139 gameMode == BeginningOfGame)) {
10140 SendToProgram("force\n", &first);
10141 if (first.usePing) {
10143 snprintf(buf, MSG_SIZ, "ping %d\n", ++first.lastPing);
10144 SendToProgram(buf, &first);
10147 } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
10148 /* Kill off first chess program */
10149 if (first.isr != NULL)
10150 RemoveInputSource(first.isr);
10153 if (first.pr != NoProc) {
10155 DoSleep( appData.delayBeforeQuit );
10156 SendToProgram("quit\n", &first);
10157 DoSleep( appData.delayAfterQuit );
10158 DestroyChildProcess(first.pr, first.useSigterm);
10162 if (second.reuse) {
10163 /* Put second chess program into idle state */
10164 if (second.pr != NoProc &&
10165 gameMode == TwoMachinesPlay) {
10166 SendToProgram("force\n", &second);
10167 if (second.usePing) {
10169 snprintf(buf, MSG_SIZ, "ping %d\n", ++second.lastPing);
10170 SendToProgram(buf, &second);
10173 } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
10174 /* Kill off second chess program */
10175 if (second.isr != NULL)
10176 RemoveInputSource(second.isr);
10179 if (second.pr != NoProc) {
10180 DoSleep( appData.delayBeforeQuit );
10181 SendToProgram("quit\n", &second);
10182 DoSleep( appData.delayAfterQuit );
10183 DestroyChildProcess(second.pr, second.useSigterm);
10185 second.pr = NoProc;
10188 if (matchMode && (gameMode == TwoMachinesPlay || waitingForGame && exiting)) {
10189 char resChar = '=';
10193 if (first.twoMachinesColor[0] == 'w') {
10196 second.matchWins++;
10201 if (first.twoMachinesColor[0] == 'b') {
10204 second.matchWins++;
10207 case GameUnfinished:
10213 if(waitingForGame) resChar = ' '; // quit while waiting for round sync: unreserve already reserved game
10214 if(appData.tourneyFile[0] && !abortMatch){ // [HGM] we are in a tourney; update tourney file with game result
10215 ReserveGame(nextGame, resChar); // sets nextGame
10216 if(nextGame > appData.matchGames) appData.tourneyFile[0] = 0, ranking = TourneyStandings(3); // tourney is done
10217 } else roundNr = nextGame = matchGame + 1; // normal match, just increment; round equals matchGame
10219 if (nextGame <= appData.matchGames && !abortMatch) {
10220 gameMode = nextGameMode;
10221 matchGame = nextGame; // this will be overruled in tourney mode!
10222 GetTimeMark(&pauseStart); // [HGM] matchpause: stipulate a pause
10223 ScheduleDelayedEvent(NextMatchGame, 10); // but start game immediately (as it will wait out the pause itself)
10224 endingGame = 0; /* [HGM] crash */
10227 gameMode = nextGameMode;
10228 snprintf(buf, MSG_SIZ, _("Match %s vs. %s: final score %d-%d-%d"),
10229 first.tidy, second.tidy,
10230 first.matchWins, second.matchWins,
10231 appData.matchGames - (first.matchWins + second.matchWins));
10232 popupRequested++; // [HGM] crash: postpone to after resetting endingGame
10233 if (appData.firstPlaysBlack) { // [HGM] match: back to original for next match
10234 first.twoMachinesColor = "black\n";
10235 second.twoMachinesColor = "white\n";
10237 first.twoMachinesColor = "white\n";
10238 second.twoMachinesColor = "black\n";
10242 if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile) &&
10243 !(nextGameMode == AnalyzeMode || nextGameMode == AnalyzeFile))
10245 gameMode = nextGameMode;
10247 endingGame = 0; /* [HGM] crash */
10248 if(popupRequested) { // [HGM] crash: this calls GameEnds recursively through ExitEvent! Make it a harmless tail recursion.
10249 if(matchMode == TRUE) { // match through command line: exit with or without popup
10251 if(strcmp(ranking, "busy")) DisplayFatalError(ranking, 0, 0);
10253 } else DisplayFatalError(buf, 0, 0);
10254 } else { // match through menu; just stop, with or without popup
10255 matchMode = FALSE; appData.matchGames = matchGame = roundNr = 0;
10257 if(strcmp(ranking, "busy")) DisplayNote(ranking);
10258 } else DisplayNote(buf);
10260 if(ranking) free(ranking);
10264 /* Assumes program was just initialized (initString sent).
10265 Leaves program in force mode. */
10267 FeedMovesToProgram(cps, upto)
10268 ChessProgramState *cps;
10273 if (appData.debugMode)
10274 fprintf(debugFP, "Feeding %smoves %d through %d to %s chess program\n",
10275 startedFromSetupPosition ? "position and " : "",
10276 backwardMostMove, upto, cps->which);
10277 if(currentlyInitializedVariant != gameInfo.variant) {
10279 // [HGM] variantswitch: make engine aware of new variant
10280 if(cps->protocolVersion > 1 && StrStr(cps->variants, VariantName(gameInfo.variant)) == NULL)
10281 return; // [HGM] refrain from feeding moves altogether if variant is unsupported!
10282 snprintf(buf, MSG_SIZ, "variant %s\n", VariantName(gameInfo.variant));
10283 SendToProgram(buf, cps);
10284 currentlyInitializedVariant = gameInfo.variant;
10286 SendToProgram("force\n", cps);
10287 if (startedFromSetupPosition) {
10288 SendBoard(cps, backwardMostMove);
10289 if (appData.debugMode) {
10290 fprintf(debugFP, "feedMoves\n");
10293 for (i = backwardMostMove; i < upto; i++) {
10294 SendMoveToProgram(i, cps);
10300 ResurrectChessProgram()
10302 /* The chess program may have exited.
10303 If so, restart it and feed it all the moves made so far. */
10304 static int doInit = 0;
10306 if (appData.noChessProgram) return 1;
10308 if(matchMode && appData.tourneyFile[0]) { // [HGM] tourney: make sure we get features after engine replacement. (Should we always do this?)
10309 if(WaitForEngine(&first, TwoMachinesEventIfReady)) { doInit = 1; return 0; } // request to do init on next visit
10310 if(!doInit) return 1; // this replaces testing first.pr != NoProc, which is true when we get here, but first time no reason to abort
10311 doInit = 0; // we fell through (first time after starting the engine); make sure it doesn't happen again
10313 if (first.pr != NoProc) return 1;
10314 StartChessProgram(&first);
10316 InitChessProgram(&first, FALSE);
10317 FeedMovesToProgram(&first, currentMove);
10319 if (!first.sendTime) {
10320 /* can't tell gnuchess what its clock should read,
10321 so we bow to its notion. */
10323 timeRemaining[0][currentMove] = whiteTimeRemaining;
10324 timeRemaining[1][currentMove] = blackTimeRemaining;
10327 if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile ||
10328 appData.icsEngineAnalyze) && first.analysisSupport) {
10329 SendToProgram("analyze\n", &first);
10330 first.analyzing = TRUE;
10336 * Button procedures
10339 Reset(redraw, init)
10344 if (appData.debugMode) {
10345 fprintf(debugFP, "Reset(%d, %d) from gameMode %d\n",
10346 redraw, init, gameMode);
10348 CleanupTail(); // [HGM] vari: delete any stored variations
10349 pausing = pauseExamInvalid = FALSE;
10350 startedFromSetupPosition = blackPlaysFirst = FALSE;
10352 whiteFlag = blackFlag = FALSE;
10353 userOfferedDraw = FALSE;
10354 hintRequested = bookRequested = FALSE;
10355 first.maybeThinking = FALSE;
10356 second.maybeThinking = FALSE;
10357 first.bookSuspend = FALSE; // [HGM] book
10358 second.bookSuspend = FALSE;
10359 thinkOutput[0] = NULLCHAR;
10360 lastHint[0] = NULLCHAR;
10361 ClearGameInfo(&gameInfo);
10362 gameInfo.variant = StringToVariant(appData.variant);
10363 ics_user_moved = ics_clock_paused = FALSE;
10364 ics_getting_history = H_FALSE;
10366 white_holding[0] = black_holding[0] = NULLCHAR;
10367 ClearProgramStats();
10368 opponentKibitzes = FALSE; // [HGM] kibitz: do not reserve space in engine-output window in zippy mode
10372 flipView = appData.flipView;
10373 ClearPremoveHighlights();
10374 gotPremove = FALSE;
10375 alarmSounded = FALSE;
10377 GameEnds(EndOfFile, NULL, GE_PLAYER);
10378 if(appData.serverMovesName != NULL) {
10379 /* [HGM] prepare to make moves file for broadcasting */
10380 clock_t t = clock();
10381 if(serverMoves != NULL) fclose(serverMoves);
10382 serverMoves = fopen(appData.serverMovesName, "r");
10383 if(serverMoves != NULL) {
10384 fclose(serverMoves);
10385 /* delay 15 sec before overwriting, so all clients can see end */
10386 while(clock()-t < appData.serverPause*CLOCKS_PER_SEC);
10388 serverMoves = fopen(appData.serverMovesName, "w");
10392 gameMode = BeginningOfGame;
10394 if(appData.icsActive) gameInfo.variant = VariantNormal;
10395 currentMove = forwardMostMove = backwardMostMove = 0;
10396 InitPosition(redraw);
10397 for (i = 0; i < MAX_MOVES; i++) {
10398 if (commentList[i] != NULL) {
10399 free(commentList[i]);
10400 commentList[i] = NULL;
10404 timeRemaining[0][0] = whiteTimeRemaining;
10405 timeRemaining[1][0] = blackTimeRemaining;
10407 if (first.pr == NULL) {
10408 StartChessProgram(&first);
10411 InitChessProgram(&first, startedFromSetupPosition);
10414 DisplayMessage("", "");
10415 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
10416 lastSavedGame = 0; // [HGM] save: make sure next game counts as unsaved
10423 if (!AutoPlayOneMove())
10425 if (matchMode || appData.timeDelay == 0)
10427 if (appData.timeDelay < 0)
10429 StartLoadGameTimer((long)(1000.0 * appData.timeDelay));
10438 int fromX, fromY, toX, toY;
10440 if (appData.debugMode) {
10441 fprintf(debugFP, "AutoPlayOneMove(): current %d\n", currentMove);
10444 if (gameMode != PlayFromGameFile && gameMode != AnalyzeFile)
10447 if (gameMode == AnalyzeFile && currentMove > backwardMostMove) {
10448 pvInfoList[currentMove].depth = programStats.depth;
10449 pvInfoList[currentMove].score = programStats.score;
10450 pvInfoList[currentMove].time = 0;
10451 if(currentMove < forwardMostMove) AppendComment(currentMove+1, lastPV[0], 2);
10454 if (currentMove >= forwardMostMove) {
10455 if(gameMode == AnalyzeFile) { ExitAnalyzeMode(); SendToProgram("force\n", &first); }
10456 gameMode = EditGame;
10459 /* [AS] Clear current move marker at the end of a game */
10460 /* HistorySet(parseList, backwardMostMove, forwardMostMove, -1); */
10465 toX = moveList[currentMove][2] - AAA;
10466 toY = moveList[currentMove][3] - ONE;
10468 if (moveList[currentMove][1] == '@') {
10469 if (appData.highlightLastMove) {
10470 SetHighlights(-1, -1, toX, toY);
10473 fromX = moveList[currentMove][0] - AAA;
10474 fromY = moveList[currentMove][1] - ONE;
10476 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove); /* [AS] */
10478 AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
10480 if (appData.highlightLastMove) {
10481 SetHighlights(fromX, fromY, toX, toY);
10484 DisplayMove(currentMove);
10485 SendMoveToProgram(currentMove++, &first);
10486 DisplayBothClocks();
10487 DrawPosition(FALSE, boards[currentMove]);
10488 // [HGM] PV info: always display, routine tests if empty
10489 DisplayComment(currentMove - 1, commentList[currentMove]);
10495 LoadGameOneMove(readAhead)
10496 ChessMove readAhead;
10498 int fromX = 0, fromY = 0, toX = 0, toY = 0, done;
10499 char promoChar = NULLCHAR;
10500 ChessMove moveType;
10501 char move[MSG_SIZ];
10504 if (gameMode != PlayFromGameFile && gameMode != AnalyzeFile &&
10505 gameMode != AnalyzeMode && gameMode != Training) {
10510 yyboardindex = forwardMostMove;
10511 if (readAhead != EndOfFile) {
10512 moveType = readAhead;
10514 if (gameFileFP == NULL)
10516 moveType = (ChessMove) Myylex();
10520 switch (moveType) {
10522 if (appData.debugMode)
10523 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
10526 /* append the comment but don't display it */
10527 AppendComment(currentMove, p, FALSE);
10530 case WhiteCapturesEnPassant:
10531 case BlackCapturesEnPassant:
10532 case WhitePromotion:
10533 case BlackPromotion:
10534 case WhiteNonPromotion:
10535 case BlackNonPromotion:
10537 case WhiteKingSideCastle:
10538 case WhiteQueenSideCastle:
10539 case BlackKingSideCastle:
10540 case BlackQueenSideCastle:
10541 case WhiteKingSideCastleWild:
10542 case WhiteQueenSideCastleWild:
10543 case BlackKingSideCastleWild:
10544 case BlackQueenSideCastleWild:
10546 case WhiteHSideCastleFR:
10547 case WhiteASideCastleFR:
10548 case BlackHSideCastleFR:
10549 case BlackASideCastleFR:
10551 if (appData.debugMode)
10552 fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
10553 fromX = currentMoveString[0] - AAA;
10554 fromY = currentMoveString[1] - ONE;
10555 toX = currentMoveString[2] - AAA;
10556 toY = currentMoveString[3] - ONE;
10557 promoChar = currentMoveString[4];
10562 if (appData.debugMode)
10563 fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
10564 fromX = moveType == WhiteDrop ?
10565 (int) CharToPiece(ToUpper(currentMoveString[0])) :
10566 (int) CharToPiece(ToLower(currentMoveString[0]));
10568 toX = currentMoveString[2] - AAA;
10569 toY = currentMoveString[3] - ONE;
10575 case GameUnfinished:
10576 if (appData.debugMode)
10577 fprintf(debugFP, "Parsed game end: %s\n", yy_text);
10578 p = strchr(yy_text, '{');
10579 if (p == NULL) p = strchr(yy_text, '(');
10582 if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
10584 q = strchr(p, *p == '{' ? '}' : ')');
10585 if (q != NULL) *q = NULLCHAR;
10588 while(q = strchr(p, '\n')) *q = ' '; // [HGM] crush linefeeds in result message
10589 GameEnds(moveType, p, GE_FILE);
10591 if (cmailMsgLoaded) {
10593 flipView = WhiteOnMove(currentMove);
10594 if (moveType == GameUnfinished) flipView = !flipView;
10595 if (appData.debugMode)
10596 fprintf(debugFP, "Setting flipView to %d\n", flipView) ;
10601 if (appData.debugMode)
10602 fprintf(debugFP, "Parser hit end of file\n");
10603 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
10609 if (WhiteOnMove(currentMove)) {
10610 GameEnds(BlackWins, "Black mates", GE_FILE);
10612 GameEnds(WhiteWins, "White mates", GE_FILE);
10616 GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
10622 case MoveNumberOne:
10623 if (lastLoadGameStart == GNUChessGame) {
10624 /* GNUChessGames have numbers, but they aren't move numbers */
10625 if (appData.debugMode)
10626 fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
10627 yy_text, (int) moveType);
10628 return LoadGameOneMove(EndOfFile); /* tail recursion */
10630 /* else fall thru */
10635 /* Reached start of next game in file */
10636 if (appData.debugMode)
10637 fprintf(debugFP, "Parsed start of next game: %s\n", yy_text);
10638 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
10644 if (WhiteOnMove(currentMove)) {
10645 GameEnds(BlackWins, "Black mates", GE_FILE);
10647 GameEnds(WhiteWins, "White mates", GE_FILE);
10651 GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
10657 case PositionDiagram: /* should not happen; ignore */
10658 case ElapsedTime: /* ignore */
10659 case NAG: /* ignore */
10660 if (appData.debugMode)
10661 fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
10662 yy_text, (int) moveType);
10663 return LoadGameOneMove(EndOfFile); /* tail recursion */
10666 if (appData.testLegality) {
10667 if (appData.debugMode)
10668 fprintf(debugFP, "Parsed IllegalMove: %s\n", yy_text);
10669 snprintf(move, MSG_SIZ, _("Illegal move: %d.%s%s"),
10670 (forwardMostMove / 2) + 1,
10671 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
10672 DisplayError(move, 0);
10675 if (appData.debugMode)
10676 fprintf(debugFP, "Parsed %s into IllegalMove %s\n",
10677 yy_text, currentMoveString);
10678 fromX = currentMoveString[0] - AAA;
10679 fromY = currentMoveString[1] - ONE;
10680 toX = currentMoveString[2] - AAA;
10681 toY = currentMoveString[3] - ONE;
10682 promoChar = currentMoveString[4];
10686 case AmbiguousMove:
10687 if (appData.debugMode)
10688 fprintf(debugFP, "Parsed AmbiguousMove: %s\n", yy_text);
10689 snprintf(move, MSG_SIZ, _("Ambiguous move: %d.%s%s"),
10690 (forwardMostMove / 2) + 1,
10691 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
10692 DisplayError(move, 0);
10697 case ImpossibleMove:
10698 if (appData.debugMode)
10699 fprintf(debugFP, "Parsed ImpossibleMove (type = %d): %s\n", moveType, yy_text);
10700 snprintf(move, MSG_SIZ, _("Illegal move: %d.%s%s"),
10701 (forwardMostMove / 2) + 1,
10702 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
10703 DisplayError(move, 0);
10709 if (appData.matchMode || (appData.timeDelay == 0 && !pausing)) {
10710 DrawPosition(FALSE, boards[currentMove]);
10711 DisplayBothClocks();
10712 if (!appData.matchMode) // [HGM] PV info: routine tests if empty
10713 DisplayComment(currentMove - 1, commentList[currentMove]);
10715 (void) StopLoadGameTimer();
10717 cmailOldMove = forwardMostMove;
10720 /* currentMoveString is set as a side-effect of yylex */
10722 thinkOutput[0] = NULLCHAR;
10723 MakeMove(fromX, fromY, toX, toY, promoChar);
10724 currentMove = forwardMostMove;
10729 /* Load the nth game from the given file */
10731 LoadGameFromFile(filename, n, title, useList)
10735 /*Boolean*/ int useList;
10740 if (strcmp(filename, "-") == 0) {
10744 f = fopen(filename, "rb");
10746 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
10747 DisplayError(buf, errno);
10751 if (fseek(f, 0, 0) == -1) {
10752 /* f is not seekable; probably a pipe */
10755 if (useList && n == 0) {
10756 int error = GameListBuild(f);
10758 DisplayError(_("Cannot build game list"), error);
10759 } else if (!ListEmpty(&gameList) &&
10760 ((ListGame *) gameList.tailPred)->number > 1) {
10761 GameListPopUp(f, title);
10768 return LoadGame(f, n, title, FALSE);
10773 MakeRegisteredMove()
10775 int fromX, fromY, toX, toY;
10777 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
10778 switch (cmailMoveType[lastLoadGameNumber - 1]) {
10781 if (appData.debugMode)
10782 fprintf(debugFP, "Restoring %s for game %d\n",
10783 cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
10785 thinkOutput[0] = NULLCHAR;
10786 safeStrCpy(moveList[currentMove], cmailMove[lastLoadGameNumber - 1], sizeof(moveList[currentMove])/sizeof(moveList[currentMove][0]));
10787 fromX = cmailMove[lastLoadGameNumber - 1][0] - AAA;
10788 fromY = cmailMove[lastLoadGameNumber - 1][1] - ONE;
10789 toX = cmailMove[lastLoadGameNumber - 1][2] - AAA;
10790 toY = cmailMove[lastLoadGameNumber - 1][3] - ONE;
10791 promoChar = cmailMove[lastLoadGameNumber - 1][4];
10792 MakeMove(fromX, fromY, toX, toY, promoChar);
10793 ShowMove(fromX, fromY, toX, toY);
10795 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
10802 if (WhiteOnMove(currentMove)) {
10803 GameEnds(BlackWins, "Black mates", GE_PLAYER);
10805 GameEnds(WhiteWins, "White mates", GE_PLAYER);
10810 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
10817 if (WhiteOnMove(currentMove)) {
10818 GameEnds(BlackWins, "White resigns", GE_PLAYER);
10820 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
10825 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
10836 /* Wrapper around LoadGame for use when a Cmail message is loaded */
10838 CmailLoadGame(f, gameNumber, title, useList)
10846 if (gameNumber > nCmailGames) {
10847 DisplayError(_("No more games in this message"), 0);
10850 if (f == lastLoadGameFP) {
10851 int offset = gameNumber - lastLoadGameNumber;
10853 cmailMsg[0] = NULLCHAR;
10854 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
10855 cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
10856 nCmailMovesRegistered--;
10858 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
10859 if (cmailResult[lastLoadGameNumber - 1] == CMAIL_NEW_RESULT) {
10860 cmailResult[lastLoadGameNumber - 1] = CMAIL_NOT_RESULT;
10863 if (! RegisterMove()) return FALSE;
10867 retVal = LoadGame(f, gameNumber, title, useList);
10869 /* Make move registered during previous look at this game, if any */
10870 MakeRegisteredMove();
10872 if (cmailCommentList[lastLoadGameNumber - 1] != NULL) {
10873 commentList[currentMove]
10874 = StrSave(cmailCommentList[lastLoadGameNumber - 1]);
10875 DisplayComment(currentMove - 1, commentList[currentMove]);
10881 /* Support for LoadNextGame, LoadPreviousGame, ReloadSameGame */
10886 int gameNumber = lastLoadGameNumber + offset;
10887 if (lastLoadGameFP == NULL) {
10888 DisplayError(_("No game has been loaded yet"), 0);
10891 if (gameNumber <= 0) {
10892 DisplayError(_("Can't back up any further"), 0);
10895 if (cmailMsgLoaded) {
10896 return CmailLoadGame(lastLoadGameFP, gameNumber,
10897 lastLoadGameTitle, lastLoadGameUseList);
10899 return LoadGame(lastLoadGameFP, gameNumber,
10900 lastLoadGameTitle, lastLoadGameUseList);
10906 /* Load the nth game from open file f */
10908 LoadGame(f, gameNumber, title, useList)
10916 int gn = gameNumber;
10917 ListGame *lg = NULL;
10918 int numPGNTags = 0;
10920 GameMode oldGameMode;
10921 VariantClass oldVariant = gameInfo.variant; /* [HGM] PGNvariant */
10923 if (appData.debugMode)
10924 fprintf(debugFP, "LoadGame(): on entry, gameMode %d\n", gameMode);
10926 if (gameMode == Training )
10927 SetTrainingModeOff();
10929 oldGameMode = gameMode;
10930 if (gameMode != BeginningOfGame) {
10931 Reset(FALSE, TRUE);
10935 if (lastLoadGameFP != NULL && lastLoadGameFP != f) {
10936 fclose(lastLoadGameFP);
10940 lg = (ListGame *) ListElem(&gameList, gameNumber-1);
10943 fseek(f, lg->offset, 0);
10944 GameListHighlight(gameNumber);
10948 DisplayError(_("Game number out of range"), 0);
10953 if (fseek(f, 0, 0) == -1) {
10954 if (f == lastLoadGameFP ?
10955 gameNumber == lastLoadGameNumber + 1 :
10959 DisplayError(_("Can't seek on game file"), 0);
10964 lastLoadGameFP = f;
10965 lastLoadGameNumber = gameNumber;
10966 safeStrCpy(lastLoadGameTitle, title, sizeof(lastLoadGameTitle)/sizeof(lastLoadGameTitle[0]));
10967 lastLoadGameUseList = useList;
10971 if (lg && lg->gameInfo.white && lg->gameInfo.black) {
10972 snprintf(buf, sizeof(buf), "%s vs. %s", lg->gameInfo.white,
10973 lg->gameInfo.black);
10975 } else if (*title != NULLCHAR) {
10976 if (gameNumber > 1) {
10977 snprintf(buf, MSG_SIZ, "%s %d", title, gameNumber);
10980 DisplayTitle(title);
10984 if (gameMode != AnalyzeFile && gameMode != AnalyzeMode) {
10985 gameMode = PlayFromGameFile;
10989 currentMove = forwardMostMove = backwardMostMove = 0;
10990 CopyBoard(boards[0], initialPosition);
10994 * Skip the first gn-1 games in the file.
10995 * Also skip over anything that precedes an identifiable
10996 * start of game marker, to avoid being confused by
10997 * garbage at the start of the file. Currently
10998 * recognized start of game markers are the move number "1",
10999 * the pattern "gnuchess .* game", the pattern
11000 * "^[#;%] [^ ]* game file", and a PGN tag block.
11001 * A game that starts with one of the latter two patterns
11002 * will also have a move number 1, possibly
11003 * following a position diagram.
11004 * 5-4-02: Let's try being more lenient and allowing a game to
11005 * start with an unnumbered move. Does that break anything?
11007 cm = lastLoadGameStart = EndOfFile;
11009 yyboardindex = forwardMostMove;
11010 cm = (ChessMove) Myylex();
11013 if (cmailMsgLoaded) {
11014 nCmailGames = CMAIL_MAX_GAMES - gn;
11017 DisplayError(_("Game not found in file"), 0);
11024 lastLoadGameStart = cm;
11027 case MoveNumberOne:
11028 switch (lastLoadGameStart) {
11033 case MoveNumberOne:
11035 gn--; /* count this game */
11036 lastLoadGameStart = cm;
11045 switch (lastLoadGameStart) {
11048 case MoveNumberOne:
11050 gn--; /* count this game */
11051 lastLoadGameStart = cm;
11054 lastLoadGameStart = cm; /* game counted already */
11062 yyboardindex = forwardMostMove;
11063 cm = (ChessMove) Myylex();
11064 } while (cm == PGNTag || cm == Comment);
11071 if (cmailMsgLoaded && (CMAIL_MAX_GAMES == lastLoadGameNumber)) {
11072 if ( cmailResult[CMAIL_MAX_GAMES - gn - 1]
11073 != CMAIL_OLD_RESULT) {
11075 cmailResult[ CMAIL_MAX_GAMES
11076 - gn - 1] = CMAIL_OLD_RESULT;
11082 /* Only a NormalMove can be at the start of a game
11083 * without a position diagram. */
11084 if (lastLoadGameStart == EndOfFile ) {
11086 lastLoadGameStart = MoveNumberOne;
11095 if (appData.debugMode)
11096 fprintf(debugFP, "Parsed game start '%s' (%d)\n", yy_text, (int) cm);
11098 if (cm == XBoardGame) {
11099 /* Skip any header junk before position diagram and/or move 1 */
11101 yyboardindex = forwardMostMove;
11102 cm = (ChessMove) Myylex();
11104 if (cm == EndOfFile ||
11105 cm == GNUChessGame || cm == XBoardGame) {
11106 /* Empty game; pretend end-of-file and handle later */
11111 if (cm == MoveNumberOne || cm == PositionDiagram ||
11112 cm == PGNTag || cm == Comment)
11115 } else if (cm == GNUChessGame) {
11116 if (gameInfo.event != NULL) {
11117 free(gameInfo.event);
11119 gameInfo.event = StrSave(yy_text);
11122 startedFromSetupPosition = FALSE;
11123 while (cm == PGNTag) {
11124 if (appData.debugMode)
11125 fprintf(debugFP, "Parsed PGNTag: %s\n", yy_text);
11126 err = ParsePGNTag(yy_text, &gameInfo);
11127 if (!err) numPGNTags++;
11129 /* [HGM] PGNvariant: automatically switch to variant given in PGN tag */
11130 if(gameInfo.variant != oldVariant) {
11131 startedFromPositionFile = FALSE; /* [HGM] loadPos: variant switch likely makes position invalid */
11132 ResetFrontEnd(); // [HGM] might need other bitmaps. Cannot use Reset() because it clears gameInfo :-(
11133 InitPosition(TRUE);
11134 oldVariant = gameInfo.variant;
11135 if (appData.debugMode)
11136 fprintf(debugFP, "New variant %d\n", (int) oldVariant);
11140 if (gameInfo.fen != NULL) {
11141 Board initial_position;
11142 startedFromSetupPosition = TRUE;
11143 if (!ParseFEN(initial_position, &blackPlaysFirst, gameInfo.fen)) {
11145 DisplayError(_("Bad FEN position in file"), 0);
11148 CopyBoard(boards[0], initial_position);
11149 if (blackPlaysFirst) {
11150 currentMove = forwardMostMove = backwardMostMove = 1;
11151 CopyBoard(boards[1], initial_position);
11152 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
11153 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
11154 timeRemaining[0][1] = whiteTimeRemaining;
11155 timeRemaining[1][1] = blackTimeRemaining;
11156 if (commentList[0] != NULL) {
11157 commentList[1] = commentList[0];
11158 commentList[0] = NULL;
11161 currentMove = forwardMostMove = backwardMostMove = 0;
11163 /* [HGM] copy FEN attributes as well. Bugfix 4.3.14m and 4.3.15e: moved to after 'blackPlaysFirst' */
11165 initialRulePlies = FENrulePlies;
11166 for( i=0; i< nrCastlingRights; i++ )
11167 initialRights[i] = initial_position[CASTLING][i];
11169 yyboardindex = forwardMostMove;
11170 free(gameInfo.fen);
11171 gameInfo.fen = NULL;
11174 yyboardindex = forwardMostMove;
11175 cm = (ChessMove) Myylex();
11177 /* Handle comments interspersed among the tags */
11178 while (cm == Comment) {
11180 if (appData.debugMode)
11181 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
11183 AppendComment(currentMove, p, FALSE);
11184 yyboardindex = forwardMostMove;
11185 cm = (ChessMove) Myylex();
11189 /* don't rely on existence of Event tag since if game was
11190 * pasted from clipboard the Event tag may not exist
11192 if (numPGNTags > 0){
11194 if (gameInfo.variant == VariantNormal) {
11195 VariantClass v = StringToVariant(gameInfo.event);
11196 // [HGM] do not recognize variants from event tag that were introduced after supporting variant tag
11197 if(v < VariantShogi) gameInfo.variant = v;
11200 if( appData.autoDisplayTags ) {
11201 tags = PGNTags(&gameInfo);
11202 TagsPopUp(tags, CmailMsg());
11207 /* Make something up, but don't display it now */
11212 if (cm == PositionDiagram) {
11215 Board initial_position;
11217 if (appData.debugMode)
11218 fprintf(debugFP, "Parsed PositionDiagram: %s\n", yy_text);
11220 if (!startedFromSetupPosition) {
11222 for (i = BOARD_HEIGHT - 1; i >= 0; i--)
11223 for (j = BOARD_LEFT; j < BOARD_RGHT; p++)
11234 initial_position[i][j++] = CharToPiece(*p);
11237 while (*p == ' ' || *p == '\t' ||
11238 *p == '\n' || *p == '\r') p++;
11240 if (strncmp(p, "black", strlen("black"))==0)
11241 blackPlaysFirst = TRUE;
11243 blackPlaysFirst = FALSE;
11244 startedFromSetupPosition = TRUE;
11246 CopyBoard(boards[0], initial_position);
11247 if (blackPlaysFirst) {
11248 currentMove = forwardMostMove = backwardMostMove = 1;
11249 CopyBoard(boards[1], initial_position);
11250 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
11251 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
11252 timeRemaining[0][1] = whiteTimeRemaining;
11253 timeRemaining[1][1] = blackTimeRemaining;
11254 if (commentList[0] != NULL) {
11255 commentList[1] = commentList[0];
11256 commentList[0] = NULL;
11259 currentMove = forwardMostMove = backwardMostMove = 0;
11262 yyboardindex = forwardMostMove;
11263 cm = (ChessMove) Myylex();
11266 if (first.pr == NoProc) {
11267 StartChessProgram(&first);
11269 InitChessProgram(&first, FALSE);
11270 SendToProgram("force\n", &first);
11271 if (startedFromSetupPosition) {
11272 SendBoard(&first, forwardMostMove);
11273 if (appData.debugMode) {
11274 fprintf(debugFP, "Load Game\n");
11276 DisplayBothClocks();
11279 /* [HGM] server: flag to write setup moves in broadcast file as one */
11280 loadFlag = appData.suppressLoadMoves;
11282 while (cm == Comment) {
11284 if (appData.debugMode)
11285 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
11287 AppendComment(currentMove, p, FALSE);
11288 yyboardindex = forwardMostMove;
11289 cm = (ChessMove) Myylex();
11292 if ((cm == EndOfFile && lastLoadGameStart != EndOfFile ) ||
11293 cm == WhiteWins || cm == BlackWins ||
11294 cm == GameIsDrawn || cm == GameUnfinished) {
11295 DisplayMessage("", _("No moves in game"));
11296 if (cmailMsgLoaded) {
11297 if (appData.debugMode)
11298 fprintf(debugFP, "Setting flipView to %d.\n", FALSE);
11302 DrawPosition(FALSE, boards[currentMove]);
11303 DisplayBothClocks();
11304 gameMode = EditGame;
11311 // [HGM] PV info: routine tests if comment empty
11312 if (!matchMode && (pausing || appData.timeDelay != 0)) {
11313 DisplayComment(currentMove - 1, commentList[currentMove]);
11315 if (!matchMode && appData.timeDelay != 0)
11316 DrawPosition(FALSE, boards[currentMove]);
11318 if (gameMode == AnalyzeFile || gameMode == AnalyzeMode) {
11319 programStats.ok_to_send = 1;
11322 /* if the first token after the PGN tags is a move
11323 * and not move number 1, retrieve it from the parser
11325 if (cm != MoveNumberOne)
11326 LoadGameOneMove(cm);
11328 /* load the remaining moves from the file */
11329 while (LoadGameOneMove(EndOfFile)) {
11330 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
11331 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
11334 /* rewind to the start of the game */
11335 currentMove = backwardMostMove;
11337 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
11339 if (oldGameMode == AnalyzeFile ||
11340 oldGameMode == AnalyzeMode) {
11341 AnalyzeFileEvent();
11344 if (matchMode || appData.timeDelay == 0) {
11346 gameMode = EditGame;
11348 } else if (appData.timeDelay > 0) {
11349 AutoPlayGameLoop();
11352 if (appData.debugMode)
11353 fprintf(debugFP, "LoadGame(): on exit, gameMode %d\n", gameMode);
11355 loadFlag = 0; /* [HGM] true game starts */
11359 /* Support for LoadNextPosition, LoadPreviousPosition, ReloadSamePosition */
11361 ReloadPosition(offset)
11364 int positionNumber = lastLoadPositionNumber + offset;
11365 if (lastLoadPositionFP == NULL) {
11366 DisplayError(_("No position has been loaded yet"), 0);
11369 if (positionNumber <= 0) {
11370 DisplayError(_("Can't back up any further"), 0);
11373 return LoadPosition(lastLoadPositionFP, positionNumber,
11374 lastLoadPositionTitle);
11377 /* Load the nth position from the given file */
11379 LoadPositionFromFile(filename, n, title)
11387 if (strcmp(filename, "-") == 0) {
11388 return LoadPosition(stdin, n, "stdin");
11390 f = fopen(filename, "rb");
11392 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
11393 DisplayError(buf, errno);
11396 return LoadPosition(f, n, title);
11401 /* Load the nth position from the given open file, and close it */
11403 LoadPosition(f, positionNumber, title)
11405 int positionNumber;
11408 char *p, line[MSG_SIZ];
11409 Board initial_position;
11410 int i, j, fenMode, pn;
11412 if (gameMode == Training )
11413 SetTrainingModeOff();
11415 if (gameMode != BeginningOfGame) {
11416 Reset(FALSE, TRUE);
11418 if (lastLoadPositionFP != NULL && lastLoadPositionFP != f) {
11419 fclose(lastLoadPositionFP);
11421 if (positionNumber == 0) positionNumber = 1;
11422 lastLoadPositionFP = f;
11423 lastLoadPositionNumber = positionNumber;
11424 safeStrCpy(lastLoadPositionTitle, title, sizeof(lastLoadPositionTitle)/sizeof(lastLoadPositionTitle[0]));
11425 if (first.pr == NoProc) {
11426 StartChessProgram(&first);
11427 InitChessProgram(&first, FALSE);
11429 pn = positionNumber;
11430 if (positionNumber < 0) {
11431 /* Negative position number means to seek to that byte offset */
11432 if (fseek(f, -positionNumber, 0) == -1) {
11433 DisplayError(_("Can't seek on position file"), 0);
11438 if (fseek(f, 0, 0) == -1) {
11439 if (f == lastLoadPositionFP ?
11440 positionNumber == lastLoadPositionNumber + 1 :
11441 positionNumber == 1) {
11444 DisplayError(_("Can't seek on position file"), 0);
11449 /* See if this file is FEN or old-style xboard */
11450 if (fgets(line, MSG_SIZ, f) == NULL) {
11451 DisplayError(_("Position not found in file"), 0);
11454 // [HGM] FEN can begin with digit, any piece letter valid in this variant, or a + for Shogi promoted pieces
11455 fenMode = line[0] >= '0' && line[0] <= '9' || line[0] == '+' || CharToPiece(line[0]) != EmptySquare;
11458 if (fenMode || line[0] == '#') pn--;
11460 /* skip positions before number pn */
11461 if (fgets(line, MSG_SIZ, f) == NULL) {
11463 DisplayError(_("Position not found in file"), 0);
11466 if (fenMode || line[0] == '#') pn--;
11471 if (!ParseFEN(initial_position, &blackPlaysFirst, line)) {
11472 DisplayError(_("Bad FEN position in file"), 0);
11476 (void) fgets(line, MSG_SIZ, f);
11477 (void) fgets(line, MSG_SIZ, f);
11479 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
11480 (void) fgets(line, MSG_SIZ, f);
11481 for (p = line, j = BOARD_LEFT; j < BOARD_RGHT; p++) {
11484 initial_position[i][j++] = CharToPiece(*p);
11488 blackPlaysFirst = FALSE;
11490 (void) fgets(line, MSG_SIZ, f);
11491 if (strncmp(line, "black", strlen("black"))==0)
11492 blackPlaysFirst = TRUE;
11495 startedFromSetupPosition = TRUE;
11497 SendToProgram("force\n", &first);
11498 CopyBoard(boards[0], initial_position);
11499 if (blackPlaysFirst) {
11500 currentMove = forwardMostMove = backwardMostMove = 1;
11501 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
11502 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
11503 CopyBoard(boards[1], initial_position);
11504 DisplayMessage("", _("Black to play"));
11506 currentMove = forwardMostMove = backwardMostMove = 0;
11507 DisplayMessage("", _("White to play"));
11509 initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
11510 SendBoard(&first, forwardMostMove);
11511 if (appData.debugMode) {
11513 for(i=0;i<2;i++){for(j=0;j<6;j++)fprintf(debugFP, " %d", boards[i][CASTLING][j]);fprintf(debugFP,"\n");}
11514 for(j=0;j<6;j++)fprintf(debugFP, " %d", initialRights[j]);fprintf(debugFP,"\n");
11515 fprintf(debugFP, "Load Position\n");
11518 if (positionNumber > 1) {
11519 snprintf(line, MSG_SIZ, "%s %d", title, positionNumber);
11520 DisplayTitle(line);
11522 DisplayTitle(title);
11524 gameMode = EditGame;
11527 timeRemaining[0][1] = whiteTimeRemaining;
11528 timeRemaining[1][1] = blackTimeRemaining;
11529 DrawPosition(FALSE, boards[currentMove]);
11536 CopyPlayerNameIntoFileName(dest, src)
11539 while (*src != NULLCHAR && *src != ',') {
11544 *(*dest)++ = *src++;
11549 char *DefaultFileName(ext)
11552 static char def[MSG_SIZ];
11555 if (gameInfo.white != NULL && gameInfo.white[0] != '-') {
11557 CopyPlayerNameIntoFileName(&p, gameInfo.white);
11559 CopyPlayerNameIntoFileName(&p, gameInfo.black);
11561 safeStrCpy(p, ext, MSG_SIZ-2-strlen(gameInfo.white)-strlen(gameInfo.black));
11568 /* Save the current game to the given file */
11570 SaveGameToFile(filename, append)
11578 if (strcmp(filename, "-") == 0) {
11579 return SaveGame(stdout, 0, NULL);
11581 f = fopen(filename, append ? "a" : "w");
11583 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
11584 DisplayError(buf, errno);
11587 safeStrCpy(buf, lastMsg, MSG_SIZ);
11588 DisplayMessage(_("Waiting for access to save file"), "");
11589 flock(fileno(f), LOCK_EX); // [HGM] lock: lock file while we are writing
11590 DisplayMessage(_("Saving game"), "");
11591 if(lseek(fileno(f), 0, SEEK_END) == -1) DisplayError("Bad Seek", errno); // better safe than sorry...
11592 result = SaveGame(f, 0, NULL);
11593 DisplayMessage(buf, "");
11603 static char buf[MSG_SIZ];
11606 p = strchr(str, ' ');
11607 if (p == NULL) return str;
11608 strncpy(buf, str, p - str);
11609 buf[p - str] = NULLCHAR;
11613 #define PGN_MAX_LINE 75
11615 #define PGN_SIDE_WHITE 0
11616 #define PGN_SIDE_BLACK 1
11619 static int FindFirstMoveOutOfBook( int side )
11623 if( backwardMostMove == 0 && ! startedFromSetupPosition) {
11624 int index = backwardMostMove;
11625 int has_book_hit = 0;
11627 if( (index % 2) != side ) {
11631 while( index < forwardMostMove ) {
11632 /* Check to see if engine is in book */
11633 int depth = pvInfoList[index].depth;
11634 int score = pvInfoList[index].score;
11640 else if( score == 0 && depth == 63 ) {
11641 in_book = 1; /* Zappa */
11643 else if( score == 2 && depth == 99 ) {
11644 in_book = 1; /* Abrok */
11647 has_book_hit += in_book;
11663 void GetOutOfBookInfo( char * buf )
11667 int offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
11669 oob[0] = FindFirstMoveOutOfBook( PGN_SIDE_WHITE );
11670 oob[1] = FindFirstMoveOutOfBook( PGN_SIDE_BLACK );
11674 if( oob[0] >= 0 || oob[1] >= 0 ) {
11675 for( i=0; i<2; i++ ) {
11679 if( i > 0 && oob[0] >= 0 ) {
11680 strcat( buf, " " );
11683 sprintf( buf+strlen(buf), "%d%s. ", (idx - offset)/2 + 1, idx & 1 ? ".." : "" );
11684 sprintf( buf+strlen(buf), "%s%.2f",
11685 pvInfoList[idx].score >= 0 ? "+" : "",
11686 pvInfoList[idx].score / 100.0 );
11692 /* Save game in PGN style and close the file */
11697 int i, offset, linelen, newblock;
11701 int movelen, numlen, blank;
11702 char move_buffer[100]; /* [AS] Buffer for move+PV info */
11704 offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
11706 tm = time((time_t *) NULL);
11708 PrintPGNTags(f, &gameInfo);
11710 if (backwardMostMove > 0 || startedFromSetupPosition) {
11711 char *fen = PositionToFEN(backwardMostMove, NULL);
11712 fprintf(f, "[FEN \"%s\"]\n[SetUp \"1\"]\n", fen);
11713 fprintf(f, "\n{--------------\n");
11714 PrintPosition(f, backwardMostMove);
11715 fprintf(f, "--------------}\n");
11719 /* [AS] Out of book annotation */
11720 if( appData.saveOutOfBookInfo ) {
11723 GetOutOfBookInfo( buf );
11725 if( buf[0] != '\0' ) {
11726 fprintf( f, "[%s \"%s\"]\n", PGN_OUT_OF_BOOK, buf );
11733 i = backwardMostMove;
11737 while (i < forwardMostMove) {
11738 /* Print comments preceding this move */
11739 if (commentList[i] != NULL) {
11740 if (linelen > 0) fprintf(f, "\n");
11741 fprintf(f, "%s", commentList[i]);
11746 /* Format move number */
11748 snprintf(numtext, sizeof(numtext)/sizeof(numtext[0]),"%d.", (i - offset)/2 + 1);
11751 snprintf(numtext, sizeof(numtext)/sizeof(numtext[0]), "%d...", (i - offset)/2 + 1);
11753 numtext[0] = NULLCHAR;
11755 numlen = strlen(numtext);
11758 /* Print move number */
11759 blank = linelen > 0 && numlen > 0;
11760 if (linelen + (blank ? 1 : 0) + numlen > PGN_MAX_LINE) {
11769 fprintf(f, "%s", numtext);
11773 safeStrCpy(move_buffer, SavePart(parseList[i]), sizeof(move_buffer)/sizeof(move_buffer[0])); // [HGM] pgn: print move via buffer, so it can be edited
11774 movelen = strlen(move_buffer); /* [HGM] pgn: line-break point before move */
11777 blank = linelen > 0 && movelen > 0;
11778 if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
11787 fprintf(f, "%s", move_buffer);
11788 linelen += movelen;
11790 /* [AS] Add PV info if present */
11791 if( i >= 0 && appData.saveExtendedInfoInPGN && pvInfoList[i].depth > 0 ) {
11792 /* [HGM] add time */
11793 char buf[MSG_SIZ]; int seconds;
11795 seconds = (pvInfoList[i].time+5)/10; // deci-seconds, rounded to nearest
11801 snprintf(buf, MSG_SIZ, " %3.1f%c", seconds/10., 0);
11804 seconds = (seconds + 4)/10; // round to full seconds
11806 snprintf(buf, MSG_SIZ, " %d%c", seconds, 0);
11808 snprintf(buf, MSG_SIZ, " %d:%02d%c", seconds/60, seconds%60, 0);
11811 snprintf( move_buffer, sizeof(move_buffer)/sizeof(move_buffer[0]),"{%s%.2f/%d%s}",
11812 pvInfoList[i].score >= 0 ? "+" : "",
11813 pvInfoList[i].score / 100.0,
11814 pvInfoList[i].depth,
11817 movelen = strlen(move_buffer); /* [HGM] pgn: line-break point after move */
11819 /* Print score/depth */
11820 blank = linelen > 0 && movelen > 0;
11821 if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
11830 fprintf(f, "%s", move_buffer);
11831 linelen += movelen;
11837 /* Start a new line */
11838 if (linelen > 0) fprintf(f, "\n");
11840 /* Print comments after last move */
11841 if (commentList[i] != NULL) {
11842 fprintf(f, "%s\n", commentList[i]);
11846 if (gameInfo.resultDetails != NULL &&
11847 gameInfo.resultDetails[0] != NULLCHAR) {
11848 fprintf(f, "{%s} %s\n\n", gameInfo.resultDetails,
11849 PGNResult(gameInfo.result));
11851 fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
11855 lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
11859 /* Save game in old style and close the file */
11861 SaveGameOldStyle(f)
11867 tm = time((time_t *) NULL);
11869 fprintf(f, "# %s game file -- %s", programName, ctime(&tm));
11872 if (backwardMostMove > 0 || startedFromSetupPosition) {
11873 fprintf(f, "\n[--------------\n");
11874 PrintPosition(f, backwardMostMove);
11875 fprintf(f, "--------------]\n");
11880 i = backwardMostMove;
11881 offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
11883 while (i < forwardMostMove) {
11884 if (commentList[i] != NULL) {
11885 fprintf(f, "[%s]\n", commentList[i]);
11888 if ((i % 2) == 1) {
11889 fprintf(f, "%d. ... %s\n", (i - offset)/2 + 1, parseList[i]);
11892 fprintf(f, "%d. %s ", (i - offset)/2 + 1, parseList[i]);
11894 if (commentList[i] != NULL) {
11898 if (i >= forwardMostMove) {
11902 fprintf(f, "%s\n", parseList[i]);
11907 if (commentList[i] != NULL) {
11908 fprintf(f, "[%s]\n", commentList[i]);
11911 /* This isn't really the old style, but it's close enough */
11912 if (gameInfo.resultDetails != NULL &&
11913 gameInfo.resultDetails[0] != NULLCHAR) {
11914 fprintf(f, "%s (%s)\n\n", PGNResult(gameInfo.result),
11915 gameInfo.resultDetails);
11917 fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
11924 /* Save the current game to open file f and close the file */
11926 SaveGame(f, dummy, dummy2)
11931 if (gameMode == EditPosition) EditPositionDone(TRUE);
11932 lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
11933 if (appData.oldSaveStyle)
11934 return SaveGameOldStyle(f);
11936 return SaveGamePGN(f);
11939 /* Save the current position to the given file */
11941 SavePositionToFile(filename)
11947 if (strcmp(filename, "-") == 0) {
11948 return SavePosition(stdout, 0, NULL);
11950 f = fopen(filename, "a");
11952 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
11953 DisplayError(buf, errno);
11956 safeStrCpy(buf, lastMsg, MSG_SIZ);
11957 DisplayMessage(_("Waiting for access to save file"), "");
11958 flock(fileno(f), LOCK_EX); // [HGM] lock
11959 DisplayMessage(_("Saving position"), "");
11960 lseek(fileno(f), 0, SEEK_END); // better safe than sorry...
11961 SavePosition(f, 0, NULL);
11962 DisplayMessage(buf, "");
11968 /* Save the current position to the given open file and close the file */
11970 SavePosition(f, dummy, dummy2)
11978 if (gameMode == EditPosition) EditPositionDone(TRUE);
11979 if (appData.oldSaveStyle) {
11980 tm = time((time_t *) NULL);
11982 fprintf(f, "# %s position file -- %s", programName, ctime(&tm));
11984 fprintf(f, "[--------------\n");
11985 PrintPosition(f, currentMove);
11986 fprintf(f, "--------------]\n");
11988 fen = PositionToFEN(currentMove, NULL);
11989 fprintf(f, "%s\n", fen);
11997 ReloadCmailMsgEvent(unregister)
12001 static char *inFilename = NULL;
12002 static char *outFilename;
12004 struct stat inbuf, outbuf;
12007 /* Any registered moves are unregistered if unregister is set, */
12008 /* i.e. invoked by the signal handler */
12010 for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
12011 cmailMoveRegistered[i] = FALSE;
12012 if (cmailCommentList[i] != NULL) {
12013 free(cmailCommentList[i]);
12014 cmailCommentList[i] = NULL;
12017 nCmailMovesRegistered = 0;
12020 for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
12021 cmailResult[i] = CMAIL_NOT_RESULT;
12025 if (inFilename == NULL) {
12026 /* Because the filenames are static they only get malloced once */
12027 /* and they never get freed */
12028 inFilename = (char *) malloc(strlen(appData.cmailGameName) + 9);
12029 sprintf(inFilename, "%s.game.in", appData.cmailGameName);
12031 outFilename = (char *) malloc(strlen(appData.cmailGameName) + 5);
12032 sprintf(outFilename, "%s.out", appData.cmailGameName);
12035 status = stat(outFilename, &outbuf);
12037 cmailMailedMove = FALSE;
12039 status = stat(inFilename, &inbuf);
12040 cmailMailedMove = (inbuf.st_mtime < outbuf.st_mtime);
12043 /* LoadGameFromFile(CMAIL_MAX_GAMES) with cmailMsgLoaded == TRUE
12044 counts the games, notes how each one terminated, etc.
12046 It would be nice to remove this kludge and instead gather all
12047 the information while building the game list. (And to keep it
12048 in the game list nodes instead of having a bunch of fixed-size
12049 parallel arrays.) Note this will require getting each game's
12050 termination from the PGN tags, as the game list builder does
12051 not process the game moves. --mann
12053 cmailMsgLoaded = TRUE;
12054 LoadGameFromFile(inFilename, CMAIL_MAX_GAMES, "", FALSE);
12056 /* Load first game in the file or popup game menu */
12057 LoadGameFromFile(inFilename, 0, appData.cmailGameName, TRUE);
12059 #endif /* !WIN32 */
12067 char string[MSG_SIZ];
12069 if ( cmailMailedMove
12070 || (cmailResult[lastLoadGameNumber - 1] == CMAIL_OLD_RESULT)) {
12071 return TRUE; /* Allow free viewing */
12074 /* Unregister move to ensure that we don't leave RegisterMove */
12075 /* with the move registered when the conditions for registering no */
12077 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
12078 cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
12079 nCmailMovesRegistered --;
12081 if (cmailCommentList[lastLoadGameNumber - 1] != NULL)
12083 free(cmailCommentList[lastLoadGameNumber - 1]);
12084 cmailCommentList[lastLoadGameNumber - 1] = NULL;
12088 if (cmailOldMove == -1) {
12089 DisplayError(_("You have edited the game history.\nUse Reload Same Game and make your move again."), 0);
12093 if (currentMove > cmailOldMove + 1) {
12094 DisplayError(_("You have entered too many moves.\nBack up to the correct position and try again."), 0);
12098 if (currentMove < cmailOldMove) {
12099 DisplayError(_("Displayed position is not current.\nStep forward to the correct position and try again."), 0);
12103 if (forwardMostMove > currentMove) {
12104 /* Silently truncate extra moves */
12108 if ( (currentMove == cmailOldMove + 1)
12109 || ( (currentMove == cmailOldMove)
12110 && ( (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_ACCEPT)
12111 || (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_RESIGN)))) {
12112 if (gameInfo.result != GameUnfinished) {
12113 cmailResult[lastLoadGameNumber - 1] = CMAIL_NEW_RESULT;
12116 if (commentList[currentMove] != NULL) {
12117 cmailCommentList[lastLoadGameNumber - 1]
12118 = StrSave(commentList[currentMove]);
12120 safeStrCpy(cmailMove[lastLoadGameNumber - 1], moveList[currentMove - 1], sizeof(cmailMove[lastLoadGameNumber - 1])/sizeof(cmailMove[lastLoadGameNumber - 1][0]));
12122 if (appData.debugMode)
12123 fprintf(debugFP, "Saving %s for game %d\n",
12124 cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
12126 snprintf(string, MSG_SIZ, "%s.game.out.%d", appData.cmailGameName, lastLoadGameNumber);
12128 f = fopen(string, "w");
12129 if (appData.oldSaveStyle) {
12130 SaveGameOldStyle(f); /* also closes the file */
12132 snprintf(string, MSG_SIZ, "%s.pos.out", appData.cmailGameName);
12133 f = fopen(string, "w");
12134 SavePosition(f, 0, NULL); /* also closes the file */
12136 fprintf(f, "{--------------\n");
12137 PrintPosition(f, currentMove);
12138 fprintf(f, "--------------}\n\n");
12140 SaveGame(f, 0, NULL); /* also closes the file*/
12143 cmailMoveRegistered[lastLoadGameNumber - 1] = TRUE;
12144 nCmailMovesRegistered ++;
12145 } else if (nCmailGames == 1) {
12146 DisplayError(_("You have not made a move yet"), 0);
12157 static char *partCommandString = "cmail -xv%s -remail -game %s 2>&1";
12158 FILE *commandOutput;
12159 char buffer[MSG_SIZ], msg[MSG_SIZ], string[MSG_SIZ];
12160 int nBytes = 0; /* Suppress warnings on uninitialized variables */
12166 if (! cmailMsgLoaded) {
12167 DisplayError(_("The cmail message is not loaded.\nUse Reload CMail Message and make your move again."), 0);
12171 if (nCmailGames == nCmailResults) {
12172 DisplayError(_("No unfinished games"), 0);
12176 #if CMAIL_PROHIBIT_REMAIL
12177 if (cmailMailedMove) {
12178 snprintf(msg, MSG_SIZ, _("You have already mailed a move.\nWait until a move arrives from your opponent.\nTo resend the same move, type\n\"cmail -remail -game %s\"\non the command line."), appData.cmailGameName);
12179 DisplayError(msg, 0);
12184 if (! (cmailMailedMove || RegisterMove())) return;
12186 if ( cmailMailedMove
12187 || (nCmailMovesRegistered + nCmailResults == nCmailGames)) {
12188 snprintf(string, MSG_SIZ, partCommandString,
12189 appData.debugMode ? " -v" : "", appData.cmailGameName);
12190 commandOutput = popen(string, "r");
12192 if (commandOutput == NULL) {
12193 DisplayError(_("Failed to invoke cmail"), 0);
12195 for (nBuffers = 0; (! feof(commandOutput)); nBuffers ++) {
12196 nBytes = fread(buffer, 1, MSG_SIZ - 1, commandOutput);
12198 if (nBuffers > 1) {
12199 (void) memcpy(msg, buffer + nBytes, MSG_SIZ - nBytes - 1);
12200 (void) memcpy(msg + MSG_SIZ - nBytes - 1, buffer, nBytes);
12201 nBytes = MSG_SIZ - 1;
12203 (void) memcpy(msg, buffer, nBytes);
12205 *(msg + nBytes) = '\0'; /* \0 for end-of-string*/
12207 if(StrStr(msg, "Mailed cmail message to ") != NULL) {
12208 cmailMailedMove = TRUE; /* Prevent >1 moves */
12211 for (i = 0; i < nCmailGames; i ++) {
12212 if (cmailResult[i] == CMAIL_NOT_RESULT) {
12217 && ( (arcDir = (char *) getenv("CMAIL_ARCDIR"))
12219 snprintf(buffer, MSG_SIZ, "%s/%s.%s.archive",
12221 appData.cmailGameName,
12223 LoadGameFromFile(buffer, 1, buffer, FALSE);
12224 cmailMsgLoaded = FALSE;
12228 DisplayInformation(msg);
12229 pclose(commandOutput);
12232 if ((*cmailMsg) != '\0') {
12233 DisplayInformation(cmailMsg);
12238 #endif /* !WIN32 */
12247 int prependComma = 0;
12249 char string[MSG_SIZ]; /* Space for game-list */
12252 if (!cmailMsgLoaded) return "";
12254 if (cmailMailedMove) {
12255 snprintf(cmailMsg, MSG_SIZ, _("Waiting for reply from opponent\n"));
12257 /* Create a list of games left */
12258 snprintf(string, MSG_SIZ, "[");
12259 for (i = 0; i < nCmailGames; i ++) {
12260 if (! ( cmailMoveRegistered[i]
12261 || (cmailResult[i] == CMAIL_OLD_RESULT))) {
12262 if (prependComma) {
12263 snprintf(number, sizeof(number)/sizeof(number[0]), ",%d", i + 1);
12265 snprintf(number, sizeof(number)/sizeof(number[0]), "%d", i + 1);
12269 strcat(string, number);
12272 strcat(string, "]");
12274 if (nCmailMovesRegistered + nCmailResults == 0) {
12275 switch (nCmailGames) {
12277 snprintf(cmailMsg, MSG_SIZ, _("Still need to make move for game\n"));
12281 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for both games\n"));
12285 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for all %d games\n"),
12290 switch (nCmailGames - nCmailMovesRegistered - nCmailResults) {
12292 snprintf(cmailMsg, MSG_SIZ, _("Still need to make a move for game %s\n"),
12297 if (nCmailResults == nCmailGames) {
12298 snprintf(cmailMsg, MSG_SIZ, _("No unfinished games\n"));
12300 snprintf(cmailMsg, MSG_SIZ, _("Ready to send mail\n"));
12305 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for games %s\n"),
12317 if (gameMode == Training)
12318 SetTrainingModeOff();
12321 cmailMsgLoaded = FALSE;
12322 if (appData.icsActive) {
12323 SendToICS(ics_prefix);
12324 SendToICS("refresh\n");
12334 /* Give up on clean exit */
12338 /* Keep trying for clean exit */
12342 if (appData.icsActive && appData.colorize) Colorize(ColorNone, FALSE);
12344 if (telnetISR != NULL) {
12345 RemoveInputSource(telnetISR);
12347 if (icsPR != NoProc) {
12348 DestroyChildProcess(icsPR, TRUE);
12351 /* [HGM] crash: leave writing PGN and position entirely to GameEnds() */
12352 GameEnds(gameInfo.result, gameInfo.resultDetails==NULL ? "xboard exit" : gameInfo.resultDetails, GE_PLAYER);
12354 /* [HGM] crash: the above GameEnds() is a dud if another one was running */
12355 /* make sure this other one finishes before killing it! */
12356 if(endingGame) { int count = 0;
12357 if(appData.debugMode) fprintf(debugFP, "ExitEvent() during GameEnds(), wait\n");
12358 while(endingGame && count++ < 10) DoSleep(1);
12359 if(appData.debugMode && endingGame) fprintf(debugFP, "GameEnds() seems stuck, proceed exiting\n");
12362 /* Kill off chess programs */
12363 if (first.pr != NoProc) {
12366 DoSleep( appData.delayBeforeQuit );
12367 SendToProgram("quit\n", &first);
12368 DoSleep( appData.delayAfterQuit );
12369 DestroyChildProcess(first.pr, 10 /* [AS] first.useSigterm */ );
12371 if (second.pr != NoProc) {
12372 DoSleep( appData.delayBeforeQuit );
12373 SendToProgram("quit\n", &second);
12374 DoSleep( appData.delayAfterQuit );
12375 DestroyChildProcess(second.pr, 10 /* [AS] second.useSigterm */ );
12377 if (first.isr != NULL) {
12378 RemoveInputSource(first.isr);
12380 if (second.isr != NULL) {
12381 RemoveInputSource(second.isr);
12384 if (pairing.pr != NoProc) SendToProgram("quit\n", &pairing);
12385 if (pairing.isr != NULL) RemoveInputSource(pairing.isr);
12387 ShutDownFrontEnd();
12394 if (appData.debugMode)
12395 fprintf(debugFP, "PauseEvent(): pausing %d\n", pausing);
12399 if (gameMode == MachinePlaysWhite ||
12400 gameMode == MachinePlaysBlack) {
12403 DisplayBothClocks();
12405 if (gameMode == PlayFromGameFile) {
12406 if (appData.timeDelay >= 0)
12407 AutoPlayGameLoop();
12408 } else if (gameMode == IcsExamining && pauseExamInvalid) {
12409 Reset(FALSE, TRUE);
12410 SendToICS(ics_prefix);
12411 SendToICS("refresh\n");
12412 } else if (currentMove < forwardMostMove) {
12413 ForwardInner(forwardMostMove);
12415 pauseExamInvalid = FALSE;
12417 switch (gameMode) {
12421 pauseExamForwardMostMove = forwardMostMove;
12422 pauseExamInvalid = FALSE;
12425 case IcsPlayingWhite:
12426 case IcsPlayingBlack:
12430 case PlayFromGameFile:
12431 (void) StopLoadGameTimer();
12435 case BeginningOfGame:
12436 if (appData.icsActive) return;
12437 /* else fall through */
12438 case MachinePlaysWhite:
12439 case MachinePlaysBlack:
12440 case TwoMachinesPlay:
12441 if (forwardMostMove == 0)
12442 return; /* don't pause if no one has moved */
12443 if ((gameMode == MachinePlaysWhite &&
12444 !WhiteOnMove(forwardMostMove)) ||
12445 (gameMode == MachinePlaysBlack &&
12446 WhiteOnMove(forwardMostMove))) {
12459 char title[MSG_SIZ];
12461 if (currentMove < 1 || parseList[currentMove - 1][0] == NULLCHAR) {
12462 safeStrCpy(title, _("Edit comment"), sizeof(title)/sizeof(title[0]));
12464 snprintf(title, MSG_SIZ, _("Edit comment on %d.%s%s"), (currentMove - 1) / 2 + 1,
12465 WhiteOnMove(currentMove - 1) ? " " : ".. ",
12466 parseList[currentMove - 1]);
12469 EditCommentPopUp(currentMove, title, commentList[currentMove]);
12476 char *tags = PGNTags(&gameInfo);
12478 EditTagsPopUp(tags, NULL);
12485 if (appData.noChessProgram || gameMode == AnalyzeMode)
12488 if (gameMode != AnalyzeFile) {
12489 if (!appData.icsEngineAnalyze) {
12491 if (gameMode != EditGame) return;
12493 ResurrectChessProgram();
12494 SendToProgram("analyze\n", &first);
12495 first.analyzing = TRUE;
12496 /*first.maybeThinking = TRUE;*/
12497 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
12498 EngineOutputPopUp();
12500 if (!appData.icsEngineAnalyze) gameMode = AnalyzeMode;
12505 StartAnalysisClock();
12506 GetTimeMark(&lastNodeCountTime);
12513 if (appData.noChessProgram || gameMode == AnalyzeFile)
12516 if (gameMode != AnalyzeMode) {
12518 if (gameMode != EditGame) return;
12519 ResurrectChessProgram();
12520 SendToProgram("analyze\n", &first);
12521 first.analyzing = TRUE;
12522 /*first.maybeThinking = TRUE;*/
12523 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
12524 EngineOutputPopUp();
12526 gameMode = AnalyzeFile;
12531 StartAnalysisClock();
12532 GetTimeMark(&lastNodeCountTime);
12537 MachineWhiteEvent()
12540 char *bookHit = NULL;
12542 if (appData.noChessProgram || (gameMode == MachinePlaysWhite))
12546 if (gameMode == PlayFromGameFile ||
12547 gameMode == TwoMachinesPlay ||
12548 gameMode == Training ||
12549 gameMode == AnalyzeMode ||
12550 gameMode == EndOfGame)
12553 if (gameMode == EditPosition)
12554 EditPositionDone(TRUE);
12556 if (!WhiteOnMove(currentMove)) {
12557 DisplayError(_("It is not White's turn"), 0);
12561 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
12564 if (gameMode == EditGame || gameMode == AnalyzeMode ||
12565 gameMode == AnalyzeFile)
12568 ResurrectChessProgram(); /* in case it isn't running */
12569 if(gameMode == BeginningOfGame) { /* [HGM] time odds: to get right odds in human mode */
12570 gameMode = MachinePlaysWhite;
12573 gameMode = MachinePlaysWhite;
12577 snprintf(buf, MSG_SIZ, "%s vs. %s", gameInfo.white, gameInfo.black);
12579 if (first.sendName) {
12580 snprintf(buf, MSG_SIZ, "name %s\n", gameInfo.black);
12581 SendToProgram(buf, &first);
12583 if (first.sendTime) {
12584 if (first.useColors) {
12585 SendToProgram("black\n", &first); /*gnu kludge*/
12587 SendTimeRemaining(&first, TRUE);
12589 if (first.useColors) {
12590 SendToProgram("white\n", &first); // [HGM] book: send 'go' separately
12592 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
12593 SetMachineThinkingEnables();
12594 first.maybeThinking = TRUE;
12598 if (appData.autoFlipView && !flipView) {
12599 flipView = !flipView;
12600 DrawPosition(FALSE, NULL);
12601 DisplayBothClocks(); // [HGM] logo: clocks might have to be exchanged;
12604 if(bookHit) { // [HGM] book: simulate book reply
12605 static char bookMove[MSG_SIZ]; // a bit generous?
12607 programStats.nodes = programStats.depth = programStats.time =
12608 programStats.score = programStats.got_only_move = 0;
12609 sprintf(programStats.movelist, "%s (xbook)", bookHit);
12611 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
12612 strcat(bookMove, bookHit);
12613 HandleMachineMove(bookMove, &first);
12618 MachineBlackEvent()
12621 char *bookHit = NULL;
12623 if (appData.noChessProgram || (gameMode == MachinePlaysBlack))
12627 if (gameMode == PlayFromGameFile ||
12628 gameMode == TwoMachinesPlay ||
12629 gameMode == Training ||
12630 gameMode == AnalyzeMode ||
12631 gameMode == EndOfGame)
12634 if (gameMode == EditPosition)
12635 EditPositionDone(TRUE);
12637 if (WhiteOnMove(currentMove)) {
12638 DisplayError(_("It is not Black's turn"), 0);
12642 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
12645 if (gameMode == EditGame || gameMode == AnalyzeMode ||
12646 gameMode == AnalyzeFile)
12649 ResurrectChessProgram(); /* in case it isn't running */
12650 gameMode = MachinePlaysBlack;
12654 snprintf(buf, MSG_SIZ, "%s vs. %s", gameInfo.white, gameInfo.black);
12656 if (first.sendName) {
12657 snprintf(buf, MSG_SIZ, "name %s\n", gameInfo.white);
12658 SendToProgram(buf, &first);
12660 if (first.sendTime) {
12661 if (first.useColors) {
12662 SendToProgram("white\n", &first); /*gnu kludge*/
12664 SendTimeRemaining(&first, FALSE);
12666 if (first.useColors) {
12667 SendToProgram("black\n", &first); // [HGM] book: 'go' sent separately
12669 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
12670 SetMachineThinkingEnables();
12671 first.maybeThinking = TRUE;
12674 if (appData.autoFlipView && flipView) {
12675 flipView = !flipView;
12676 DrawPosition(FALSE, NULL);
12677 DisplayBothClocks(); // [HGM] logo: clocks might have to be exchanged;
12679 if(bookHit) { // [HGM] book: simulate book reply
12680 static char bookMove[MSG_SIZ]; // a bit generous?
12682 programStats.nodes = programStats.depth = programStats.time =
12683 programStats.score = programStats.got_only_move = 0;
12684 sprintf(programStats.movelist, "%s (xbook)", bookHit);
12686 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
12687 strcat(bookMove, bookHit);
12688 HandleMachineMove(bookMove, &first);
12694 DisplayTwoMachinesTitle()
12697 if (appData.matchGames > 0) {
12698 if(appData.tourneyFile[0]) {
12699 snprintf(buf, MSG_SIZ, "%s vs. %s (%d/%d%s)",
12700 gameInfo.white, gameInfo.black,
12701 nextGame+1, appData.matchGames+1,
12702 appData.tourneyType>0 ? "gt" : appData.tourneyType<0 ? "sw" : "rr");
12704 if (first.twoMachinesColor[0] == 'w') {
12705 snprintf(buf, MSG_SIZ, "%s vs. %s (%d-%d-%d)",
12706 gameInfo.white, gameInfo.black,
12707 first.matchWins, second.matchWins,
12708 matchGame - 1 - (first.matchWins + second.matchWins));
12710 snprintf(buf, MSG_SIZ, "%s vs. %s (%d-%d-%d)",
12711 gameInfo.white, gameInfo.black,
12712 second.matchWins, first.matchWins,
12713 matchGame - 1 - (first.matchWins + second.matchWins));
12716 snprintf(buf, MSG_SIZ, "%s vs. %s", gameInfo.white, gameInfo.black);
12722 SettingsMenuIfReady()
12724 if (second.lastPing != second.lastPong) {
12725 DisplayMessage("", _("Waiting for second chess program"));
12726 ScheduleDelayedEvent(SettingsMenuIfReady, 10); // [HGM] fast: lowered from 1000
12730 DisplayMessage("", "");
12731 SettingsPopUp(&second);
12735 WaitForEngine(ChessProgramState *cps, DelayedEventCallback retry)
12738 if (cps->pr == NULL) {
12739 StartChessProgram(cps);
12740 if (cps->protocolVersion == 1) {
12743 /* kludge: allow timeout for initial "feature" command */
12745 snprintf(buf, MSG_SIZ, _("Starting %s chess program"), cps->which);
12746 DisplayMessage("", buf);
12747 ScheduleDelayedEvent(retry, FEATURE_TIMEOUT);
12755 TwoMachinesEvent P((void))
12759 ChessProgramState *onmove;
12760 char *bookHit = NULL;
12761 static int stalling = 0;
12765 if (appData.noChessProgram) return;
12767 switch (gameMode) {
12768 case TwoMachinesPlay:
12770 case MachinePlaysWhite:
12771 case MachinePlaysBlack:
12772 if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
12773 DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
12777 case BeginningOfGame:
12778 case PlayFromGameFile:
12781 if (gameMode != EditGame) return;
12784 EditPositionDone(TRUE);
12795 // forwardMostMove = currentMove;
12796 TruncateGame(); // [HGM] vari: MachineWhite and MachineBlack do this...
12798 if(!ResurrectChessProgram()) return; /* in case first program isn't running (unbalances its ping due to InitChessProgram!) */
12800 if(WaitForEngine(&second, TwoMachinesEventIfReady)) return; // (if needed:) started up second engine, so wait for features
12801 if(first.lastPing != first.lastPong) { // [HGM] wait till we are sure first engine has set up position
12802 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10);
12806 InitChessProgram(&second, FALSE); // unbalances ping of second engine
12807 SendToProgram("force\n", &second);
12809 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10);
12812 GetTimeMark(&now); // [HGM] matchpause: implement match pause after engine load
12813 if(appData.matchPause>10000 || appData.matchPause<10)
12814 appData.matchPause = 10000; /* [HGM] make pause adjustable */
12815 wait = SubtractTimeMarks(&now, &pauseStart);
12816 if(wait < appData.matchPause) {
12817 ScheduleDelayedEvent(TwoMachinesEventIfReady, appData.matchPause - wait);
12821 DisplayMessage("", "");
12822 if (startedFromSetupPosition) {
12823 SendBoard(&second, backwardMostMove);
12824 if (appData.debugMode) {
12825 fprintf(debugFP, "Two Machines\n");
12828 for (i = backwardMostMove; i < forwardMostMove; i++) {
12829 SendMoveToProgram(i, &second);
12832 gameMode = TwoMachinesPlay;
12836 DisplayTwoMachinesTitle();
12838 if ((first.twoMachinesColor[0] == 'w') == WhiteOnMove(forwardMostMove)) {
12843 if(appData.debugMode) fprintf(debugFP, "New game (%d): %s-%s (%c)\n", matchGame, first.tidy, second.tidy, first.twoMachinesColor[0]);
12844 SendToProgram(first.computerString, &first);
12845 if (first.sendName) {
12846 snprintf(buf, MSG_SIZ, "name %s\n", second.tidy);
12847 SendToProgram(buf, &first);
12849 SendToProgram(second.computerString, &second);
12850 if (second.sendName) {
12851 snprintf(buf, MSG_SIZ, "name %s\n", first.tidy);
12852 SendToProgram(buf, &second);
12856 if (!first.sendTime || !second.sendTime) {
12857 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
12858 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
12860 if (onmove->sendTime) {
12861 if (onmove->useColors) {
12862 SendToProgram(onmove->other->twoMachinesColor, onmove); /*gnu kludge*/
12864 SendTimeRemaining(onmove, WhiteOnMove(forwardMostMove));
12866 if (onmove->useColors) {
12867 SendToProgram(onmove->twoMachinesColor, onmove);
12869 bookHit = SendMoveToBookUser(forwardMostMove-1, onmove, TRUE); // [HGM] book: send go or retrieve book move
12870 // SendToProgram("go\n", onmove);
12871 onmove->maybeThinking = TRUE;
12872 SetMachineThinkingEnables();
12876 if(bookHit) { // [HGM] book: simulate book reply
12877 static char bookMove[MSG_SIZ]; // a bit generous?
12879 programStats.nodes = programStats.depth = programStats.time =
12880 programStats.score = programStats.got_only_move = 0;
12881 sprintf(programStats.movelist, "%s (xbook)", bookHit);
12883 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
12884 strcat(bookMove, bookHit);
12885 savedMessage = bookMove; // args for deferred call
12886 savedState = onmove;
12887 ScheduleDelayedEvent(DeferredBookMove, 1);
12894 if (gameMode == Training) {
12895 SetTrainingModeOff();
12896 gameMode = PlayFromGameFile;
12897 DisplayMessage("", _("Training mode off"));
12899 gameMode = Training;
12900 animateTraining = appData.animate;
12902 /* make sure we are not already at the end of the game */
12903 if (currentMove < forwardMostMove) {
12904 SetTrainingModeOn();
12905 DisplayMessage("", _("Training mode on"));
12907 gameMode = PlayFromGameFile;
12908 DisplayError(_("Already at end of game"), 0);
12917 if (!appData.icsActive) return;
12918 switch (gameMode) {
12919 case IcsPlayingWhite:
12920 case IcsPlayingBlack:
12923 case BeginningOfGame:
12931 EditPositionDone(TRUE);
12944 gameMode = IcsIdle;
12955 switch (gameMode) {
12957 SetTrainingModeOff();
12959 case MachinePlaysWhite:
12960 case MachinePlaysBlack:
12961 case BeginningOfGame:
12962 SendToProgram("force\n", &first);
12963 SetUserThinkingEnables();
12965 case PlayFromGameFile:
12966 (void) StopLoadGameTimer();
12967 if (gameFileFP != NULL) {
12972 EditPositionDone(TRUE);
12977 SendToProgram("force\n", &first);
12979 case TwoMachinesPlay:
12980 GameEnds(EndOfFile, NULL, GE_PLAYER);
12981 ResurrectChessProgram();
12982 SetUserThinkingEnables();
12985 ResurrectChessProgram();
12987 case IcsPlayingBlack:
12988 case IcsPlayingWhite:
12989 DisplayError(_("Warning: You are still playing a game"), 0);
12992 DisplayError(_("Warning: You are still observing a game"), 0);
12995 DisplayError(_("Warning: You are still examining a game"), 0);
13006 first.offeredDraw = second.offeredDraw = 0;
13008 if (gameMode == PlayFromGameFile) {
13009 whiteTimeRemaining = timeRemaining[0][currentMove];
13010 blackTimeRemaining = timeRemaining[1][currentMove];
13014 if (gameMode == MachinePlaysWhite ||
13015 gameMode == MachinePlaysBlack ||
13016 gameMode == TwoMachinesPlay ||
13017 gameMode == EndOfGame) {
13018 i = forwardMostMove;
13019 while (i > currentMove) {
13020 SendToProgram("undo\n", &first);
13023 whiteTimeRemaining = timeRemaining[0][currentMove];
13024 blackTimeRemaining = timeRemaining[1][currentMove];
13025 DisplayBothClocks();
13026 if (whiteFlag || blackFlag) {
13027 whiteFlag = blackFlag = 0;
13032 gameMode = EditGame;
13039 EditPositionEvent()
13041 if (gameMode == EditPosition) {
13047 if (gameMode != EditGame) return;
13049 gameMode = EditPosition;
13052 if (currentMove > 0)
13053 CopyBoard(boards[0], boards[currentMove]);
13055 blackPlaysFirst = !WhiteOnMove(currentMove);
13057 currentMove = forwardMostMove = backwardMostMove = 0;
13058 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
13065 /* [DM] icsEngineAnalyze - possible call from other functions */
13066 if (appData.icsEngineAnalyze) {
13067 appData.icsEngineAnalyze = FALSE;
13069 DisplayMessage("",_("Close ICS engine analyze..."));
13071 if (first.analysisSupport && first.analyzing) {
13072 SendToProgram("exit\n", &first);
13073 first.analyzing = FALSE;
13075 thinkOutput[0] = NULLCHAR;
13079 EditPositionDone(Boolean fakeRights)
13081 int king = gameInfo.variant == VariantKnightmate ? WhiteUnicorn : WhiteKing;
13083 startedFromSetupPosition = TRUE;
13084 InitChessProgram(&first, FALSE);
13085 if(fakeRights) { // [HGM] suppress this if we just pasted a FEN.
13086 boards[0][EP_STATUS] = EP_NONE;
13087 boards[0][CASTLING][2] = boards[0][CASTLING][5] = BOARD_WIDTH>>1;
13088 if(boards[0][0][BOARD_WIDTH>>1] == king) {
13089 boards[0][CASTLING][1] = boards[0][0][BOARD_LEFT] == WhiteRook ? 0 : NoRights;
13090 boards[0][CASTLING][0] = boards[0][0][BOARD_RGHT-1] == WhiteRook ? BOARD_RGHT-1 : NoRights;
13091 } else boards[0][CASTLING][2] = NoRights;
13092 if(boards[0][BOARD_HEIGHT-1][BOARD_WIDTH>>1] == WHITE_TO_BLACK king) {
13093 boards[0][CASTLING][4] = boards[0][BOARD_HEIGHT-1][BOARD_LEFT] == BlackRook ? 0 : NoRights;
13094 boards[0][CASTLING][3] = boards[0][BOARD_HEIGHT-1][BOARD_RGHT-1] == BlackRook ? BOARD_RGHT-1 : NoRights;
13095 } else boards[0][CASTLING][5] = NoRights;
13097 SendToProgram("force\n", &first);
13098 if (blackPlaysFirst) {
13099 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
13100 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
13101 currentMove = forwardMostMove = backwardMostMove = 1;
13102 CopyBoard(boards[1], boards[0]);
13104 currentMove = forwardMostMove = backwardMostMove = 0;
13106 SendBoard(&first, forwardMostMove);
13107 if (appData.debugMode) {
13108 fprintf(debugFP, "EditPosDone\n");
13111 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
13112 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
13113 gameMode = EditGame;
13115 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
13116 ClearHighlights(); /* [AS] */
13119 /* Pause for `ms' milliseconds */
13120 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
13130 } while (SubtractTimeMarks(&m2, &m1) < ms);
13133 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
13135 SendMultiLineToICS(buf)
13138 char temp[MSG_SIZ+1], *p;
13145 strncpy(temp, buf, len);
13150 if (*p == '\n' || *p == '\r')
13155 strcat(temp, "\n");
13157 SendToPlayer(temp, strlen(temp));
13161 SetWhiteToPlayEvent()
13163 if (gameMode == EditPosition) {
13164 blackPlaysFirst = FALSE;
13165 DisplayBothClocks(); /* works because currentMove is 0 */
13166 } else if (gameMode == IcsExamining) {
13167 SendToICS(ics_prefix);
13168 SendToICS("tomove white\n");
13173 SetBlackToPlayEvent()
13175 if (gameMode == EditPosition) {
13176 blackPlaysFirst = TRUE;
13177 currentMove = 1; /* kludge */
13178 DisplayBothClocks();
13180 } else if (gameMode == IcsExamining) {
13181 SendToICS(ics_prefix);
13182 SendToICS("tomove black\n");
13187 EditPositionMenuEvent(selection, x, y)
13188 ChessSquare selection;
13192 ChessSquare piece = boards[0][y][x];
13194 if (gameMode != EditPosition && gameMode != IcsExamining) return;
13196 switch (selection) {
13198 if (gameMode == IcsExamining && ics_type == ICS_FICS) {
13199 SendToICS(ics_prefix);
13200 SendToICS("bsetup clear\n");
13201 } else if (gameMode == IcsExamining && ics_type == ICS_ICC) {
13202 SendToICS(ics_prefix);
13203 SendToICS("clearboard\n");
13205 for (x = 0; x < BOARD_WIDTH; x++) { ChessSquare p = EmptySquare;
13206 if(x == BOARD_LEFT-1 || x == BOARD_RGHT) p = (ChessSquare) 0; /* [HGM] holdings */
13207 for (y = 0; y < BOARD_HEIGHT; y++) {
13208 if (gameMode == IcsExamining) {
13209 if (boards[currentMove][y][x] != EmptySquare) {
13210 snprintf(buf, MSG_SIZ, "%sx@%c%c\n", ics_prefix,
13215 boards[0][y][x] = p;
13220 if (gameMode == EditPosition) {
13221 DrawPosition(FALSE, boards[0]);
13226 SetWhiteToPlayEvent();
13230 SetBlackToPlayEvent();
13234 if (gameMode == IcsExamining) {
13235 if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
13236 snprintf(buf, MSG_SIZ, "%sx@%c%c\n", ics_prefix, AAA + x, ONE + y);
13239 if(x < BOARD_LEFT || x >= BOARD_RGHT) {
13240 if(x == BOARD_LEFT-2) {
13241 if(y < BOARD_HEIGHT-1-gameInfo.holdingsSize) break;
13242 boards[0][y][1] = 0;
13244 if(x == BOARD_RGHT+1) {
13245 if(y >= gameInfo.holdingsSize) break;
13246 boards[0][y][BOARD_WIDTH-2] = 0;
13249 boards[0][y][x] = EmptySquare;
13250 DrawPosition(FALSE, boards[0]);
13255 if(piece >= (int)WhitePawn && piece < (int)WhiteMan ||
13256 piece >= (int)BlackPawn && piece < (int)BlackMan ) {
13257 selection = (ChessSquare) (PROMOTED piece);
13258 } else if(piece == EmptySquare) selection = WhiteSilver;
13259 else selection = (ChessSquare)((int)piece - 1);
13263 if(piece > (int)WhiteMan && piece <= (int)WhiteKing ||
13264 piece > (int)BlackMan && piece <= (int)BlackKing ) {
13265 selection = (ChessSquare) (DEMOTED piece);
13266 } else if(piece == EmptySquare) selection = BlackSilver;
13267 else selection = (ChessSquare)((int)piece + 1);
13272 if(gameInfo.variant == VariantShatranj ||
13273 gameInfo.variant == VariantXiangqi ||
13274 gameInfo.variant == VariantCourier ||
13275 gameInfo.variant == VariantMakruk )
13276 selection = (ChessSquare)((int)selection - (int)WhiteQueen + (int)WhiteFerz);
13281 if(gameInfo.variant == VariantXiangqi)
13282 selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteWazir);
13283 if(gameInfo.variant == VariantKnightmate)
13284 selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteUnicorn);
13287 if (gameMode == IcsExamining) {
13288 if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
13289 snprintf(buf, MSG_SIZ, "%s%c@%c%c\n", ics_prefix,
13290 PieceToChar(selection), AAA + x, ONE + y);
13293 if(x < BOARD_LEFT || x >= BOARD_RGHT) {
13295 if(x == BOARD_LEFT-2 && selection >= BlackPawn) {
13296 n = PieceToNumber(selection - BlackPawn);
13297 if(n >= gameInfo.holdingsSize) { n = 0; selection = BlackPawn; }
13298 boards[0][BOARD_HEIGHT-1-n][0] = selection;
13299 boards[0][BOARD_HEIGHT-1-n][1]++;
13301 if(x == BOARD_RGHT+1 && selection < BlackPawn) {
13302 n = PieceToNumber(selection);
13303 if(n >= gameInfo.holdingsSize) { n = 0; selection = WhitePawn; }
13304 boards[0][n][BOARD_WIDTH-1] = selection;
13305 boards[0][n][BOARD_WIDTH-2]++;
13308 boards[0][y][x] = selection;
13309 DrawPosition(TRUE, boards[0]);
13317 DropMenuEvent(selection, x, y)
13318 ChessSquare selection;
13321 ChessMove moveType;
13323 switch (gameMode) {
13324 case IcsPlayingWhite:
13325 case MachinePlaysBlack:
13326 if (!WhiteOnMove(currentMove)) {
13327 DisplayMoveError(_("It is Black's turn"));
13330 moveType = WhiteDrop;
13332 case IcsPlayingBlack:
13333 case MachinePlaysWhite:
13334 if (WhiteOnMove(currentMove)) {
13335 DisplayMoveError(_("It is White's turn"));
13338 moveType = BlackDrop;
13341 moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
13347 if (moveType == BlackDrop && selection < BlackPawn) {
13348 selection = (ChessSquare) ((int) selection
13349 + (int) BlackPawn - (int) WhitePawn);
13351 if (boards[currentMove][y][x] != EmptySquare) {
13352 DisplayMoveError(_("That square is occupied"));
13356 FinishMove(moveType, (int) selection, DROP_RANK, x, y, NULLCHAR);
13362 /* Accept a pending offer of any kind from opponent */
13364 if (appData.icsActive) {
13365 SendToICS(ics_prefix);
13366 SendToICS("accept\n");
13367 } else if (cmailMsgLoaded) {
13368 if (currentMove == cmailOldMove &&
13369 commentList[cmailOldMove] != NULL &&
13370 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
13371 "Black offers a draw" : "White offers a draw")) {
13373 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
13374 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
13376 DisplayError(_("There is no pending offer on this move"), 0);
13377 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
13380 /* Not used for offers from chess program */
13387 /* Decline a pending offer of any kind from opponent */
13389 if (appData.icsActive) {
13390 SendToICS(ics_prefix);
13391 SendToICS("decline\n");
13392 } else if (cmailMsgLoaded) {
13393 if (currentMove == cmailOldMove &&
13394 commentList[cmailOldMove] != NULL &&
13395 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
13396 "Black offers a draw" : "White offers a draw")) {
13398 AppendComment(cmailOldMove, "Draw declined", TRUE);
13399 DisplayComment(cmailOldMove - 1, "Draw declined");
13402 DisplayError(_("There is no pending offer on this move"), 0);
13405 /* Not used for offers from chess program */
13412 /* Issue ICS rematch command */
13413 if (appData.icsActive) {
13414 SendToICS(ics_prefix);
13415 SendToICS("rematch\n");
13422 /* Call your opponent's flag (claim a win on time) */
13423 if (appData.icsActive) {
13424 SendToICS(ics_prefix);
13425 SendToICS("flag\n");
13427 switch (gameMode) {
13430 case MachinePlaysWhite:
13433 GameEnds(GameIsDrawn, "Both players ran out of time",
13436 GameEnds(BlackWins, "Black wins on time", GE_PLAYER);
13438 DisplayError(_("Your opponent is not out of time"), 0);
13441 case MachinePlaysBlack:
13444 GameEnds(GameIsDrawn, "Both players ran out of time",
13447 GameEnds(WhiteWins, "White wins on time", GE_PLAYER);
13449 DisplayError(_("Your opponent is not out of time"), 0);
13457 ClockClick(int which)
13458 { // [HGM] code moved to back-end from winboard.c
13459 if(which) { // black clock
13460 if (gameMode == EditPosition || gameMode == IcsExamining) {
13461 if(!appData.pieceMenu && blackPlaysFirst) EditPositionMenuEvent(ClearBoard, 0, 0);
13462 SetBlackToPlayEvent();
13463 } else if (gameMode == EditGame || shiftKey) {
13464 AdjustClock(which, -1);
13465 } else if (gameMode == IcsPlayingWhite ||
13466 gameMode == MachinePlaysBlack) {
13469 } else { // white clock
13470 if (gameMode == EditPosition || gameMode == IcsExamining) {
13471 if(!appData.pieceMenu && !blackPlaysFirst) EditPositionMenuEvent(ClearBoard, 0, 0);
13472 SetWhiteToPlayEvent();
13473 } else if (gameMode == EditGame || shiftKey) {
13474 AdjustClock(which, -1);
13475 } else if (gameMode == IcsPlayingBlack ||
13476 gameMode == MachinePlaysWhite) {
13485 /* Offer draw or accept pending draw offer from opponent */
13487 if (appData.icsActive) {
13488 /* Note: tournament rules require draw offers to be
13489 made after you make your move but before you punch
13490 your clock. Currently ICS doesn't let you do that;
13491 instead, you immediately punch your clock after making
13492 a move, but you can offer a draw at any time. */
13494 SendToICS(ics_prefix);
13495 SendToICS("draw\n");
13496 userOfferedDraw = TRUE; // [HGM] drawclaim: also set flag in ICS play
13497 } else if (cmailMsgLoaded) {
13498 if (currentMove == cmailOldMove &&
13499 commentList[cmailOldMove] != NULL &&
13500 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
13501 "Black offers a draw" : "White offers a draw")) {
13502 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
13503 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
13504 } else if (currentMove == cmailOldMove + 1) {
13505 char *offer = WhiteOnMove(cmailOldMove) ?
13506 "White offers a draw" : "Black offers a draw";
13507 AppendComment(currentMove, offer, TRUE);
13508 DisplayComment(currentMove - 1, offer);
13509 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_DRAW;
13511 DisplayError(_("You must make your move before offering a draw"), 0);
13512 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
13514 } else if (first.offeredDraw) {
13515 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
13517 if (first.sendDrawOffers) {
13518 SendToProgram("draw\n", &first);
13519 userOfferedDraw = TRUE;
13527 /* Offer Adjourn or accept pending Adjourn offer from opponent */
13529 if (appData.icsActive) {
13530 SendToICS(ics_prefix);
13531 SendToICS("adjourn\n");
13533 /* Currently GNU Chess doesn't offer or accept Adjourns */
13541 /* Offer Abort or accept pending Abort offer from opponent */
13543 if (appData.icsActive) {
13544 SendToICS(ics_prefix);
13545 SendToICS("abort\n");
13547 GameEnds(GameUnfinished, "Game aborted", GE_PLAYER);
13554 /* Resign. You can do this even if it's not your turn. */
13556 if (appData.icsActive) {
13557 SendToICS(ics_prefix);
13558 SendToICS("resign\n");
13560 switch (gameMode) {
13561 case MachinePlaysWhite:
13562 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
13564 case MachinePlaysBlack:
13565 GameEnds(BlackWins, "White resigns", GE_PLAYER);
13568 if (cmailMsgLoaded) {
13570 if (WhiteOnMove(cmailOldMove)) {
13571 GameEnds(BlackWins, "White resigns", GE_PLAYER);
13573 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
13575 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_RESIGN;
13586 StopObservingEvent()
13588 /* Stop observing current games */
13589 SendToICS(ics_prefix);
13590 SendToICS("unobserve\n");
13594 StopExaminingEvent()
13596 /* Stop observing current game */
13597 SendToICS(ics_prefix);
13598 SendToICS("unexamine\n");
13602 ForwardInner(target)
13607 if (appData.debugMode)
13608 fprintf(debugFP, "ForwardInner(%d), current %d, forward %d\n",
13609 target, currentMove, forwardMostMove);
13611 if (gameMode == EditPosition)
13614 if (gameMode == PlayFromGameFile && !pausing)
13617 if (gameMode == IcsExamining && pausing)
13618 limit = pauseExamForwardMostMove;
13620 limit = forwardMostMove;
13622 if (target > limit) target = limit;
13624 if (target > 0 && moveList[target - 1][0]) {
13625 int fromX, fromY, toX, toY;
13626 toX = moveList[target - 1][2] - AAA;
13627 toY = moveList[target - 1][3] - ONE;
13628 if (moveList[target - 1][1] == '@') {
13629 if (appData.highlightLastMove) {
13630 SetHighlights(-1, -1, toX, toY);
13633 fromX = moveList[target - 1][0] - AAA;
13634 fromY = moveList[target - 1][1] - ONE;
13635 if (target == currentMove + 1) {
13636 AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
13638 if (appData.highlightLastMove) {
13639 SetHighlights(fromX, fromY, toX, toY);
13643 if (gameMode == EditGame || gameMode == AnalyzeMode ||
13644 gameMode == Training || gameMode == PlayFromGameFile ||
13645 gameMode == AnalyzeFile) {
13646 while (currentMove < target) {
13647 SendMoveToProgram(currentMove++, &first);
13650 currentMove = target;
13653 if (gameMode == EditGame || gameMode == EndOfGame) {
13654 whiteTimeRemaining = timeRemaining[0][currentMove];
13655 blackTimeRemaining = timeRemaining[1][currentMove];
13657 DisplayBothClocks();
13658 DisplayMove(currentMove - 1);
13659 DrawPosition(FALSE, boards[currentMove]);
13660 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
13661 if ( !matchMode && gameMode != Training) { // [HGM] PV info: routine tests if empty
13662 DisplayComment(currentMove - 1, commentList[currentMove]);
13664 DisplayBook(currentMove);
13671 if (gameMode == IcsExamining && !pausing) {
13672 SendToICS(ics_prefix);
13673 SendToICS("forward\n");
13675 ForwardInner(currentMove + 1);
13682 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
13683 /* to optimze, we temporarily turn off analysis mode while we feed
13684 * the remaining moves to the engine. Otherwise we get analysis output
13687 if (first.analysisSupport) {
13688 SendToProgram("exit\nforce\n", &first);
13689 first.analyzing = FALSE;
13693 if (gameMode == IcsExamining && !pausing) {
13694 SendToICS(ics_prefix);
13695 SendToICS("forward 999999\n");
13697 ForwardInner(forwardMostMove);
13700 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
13701 /* we have fed all the moves, so reactivate analysis mode */
13702 SendToProgram("analyze\n", &first);
13703 first.analyzing = TRUE;
13704 /*first.maybeThinking = TRUE;*/
13705 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
13710 BackwardInner(target)
13713 int full_redraw = TRUE; /* [AS] Was FALSE, had to change it! */
13715 if (appData.debugMode)
13716 fprintf(debugFP, "BackwardInner(%d), current %d, forward %d\n",
13717 target, currentMove, forwardMostMove);
13719 if (gameMode == EditPosition) return;
13720 if (currentMove <= backwardMostMove) {
13722 DrawPosition(full_redraw, boards[currentMove]);
13725 if (gameMode == PlayFromGameFile && !pausing)
13728 if (moveList[target][0]) {
13729 int fromX, fromY, toX, toY;
13730 toX = moveList[target][2] - AAA;
13731 toY = moveList[target][3] - ONE;
13732 if (moveList[target][1] == '@') {
13733 if (appData.highlightLastMove) {
13734 SetHighlights(-1, -1, toX, toY);
13737 fromX = moveList[target][0] - AAA;
13738 fromY = moveList[target][1] - ONE;
13739 if (target == currentMove - 1) {
13740 AnimateMove(boards[currentMove], toX, toY, fromX, fromY);
13742 if (appData.highlightLastMove) {
13743 SetHighlights(fromX, fromY, toX, toY);
13747 if (gameMode == EditGame || gameMode==AnalyzeMode ||
13748 gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
13749 while (currentMove > target) {
13750 SendToProgram("undo\n", &first);
13754 currentMove = target;
13757 if (gameMode == EditGame || gameMode == EndOfGame) {
13758 whiteTimeRemaining = timeRemaining[0][currentMove];
13759 blackTimeRemaining = timeRemaining[1][currentMove];
13761 DisplayBothClocks();
13762 DisplayMove(currentMove - 1);
13763 DrawPosition(full_redraw, boards[currentMove]);
13764 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
13765 // [HGM] PV info: routine tests if comment empty
13766 DisplayComment(currentMove - 1, commentList[currentMove]);
13767 DisplayBook(currentMove);
13773 if (gameMode == IcsExamining && !pausing) {
13774 SendToICS(ics_prefix);
13775 SendToICS("backward\n");
13777 BackwardInner(currentMove - 1);
13784 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
13785 /* to optimize, we temporarily turn off analysis mode while we undo
13786 * all the moves. Otherwise we get analysis output after each undo.
13788 if (first.analysisSupport) {
13789 SendToProgram("exit\nforce\n", &first);
13790 first.analyzing = FALSE;
13794 if (gameMode == IcsExamining && !pausing) {
13795 SendToICS(ics_prefix);
13796 SendToICS("backward 999999\n");
13798 BackwardInner(backwardMostMove);
13801 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
13802 /* we have fed all the moves, so reactivate analysis mode */
13803 SendToProgram("analyze\n", &first);
13804 first.analyzing = TRUE;
13805 /*first.maybeThinking = TRUE;*/
13806 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
13813 if (gameMode == PlayFromGameFile && !pausing) PauseEvent();
13814 if (to >= forwardMostMove) to = forwardMostMove;
13815 if (to <= backwardMostMove) to = backwardMostMove;
13816 if (to < currentMove) {
13824 RevertEvent(Boolean annotate)
13826 if(PopTail(annotate)) { // [HGM] vari: restore old game tail
13829 if (gameMode != IcsExamining) {
13830 DisplayError(_("You are not examining a game"), 0);
13834 DisplayError(_("You can't revert while pausing"), 0);
13837 SendToICS(ics_prefix);
13838 SendToICS("revert\n");
13844 switch (gameMode) {
13845 case MachinePlaysWhite:
13846 case MachinePlaysBlack:
13847 if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
13848 DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
13851 if (forwardMostMove < 2) return;
13852 currentMove = forwardMostMove = forwardMostMove - 2;
13853 whiteTimeRemaining = timeRemaining[0][currentMove];
13854 blackTimeRemaining = timeRemaining[1][currentMove];
13855 DisplayBothClocks();
13856 DisplayMove(currentMove - 1);
13857 ClearHighlights();/*!! could figure this out*/
13858 DrawPosition(TRUE, boards[currentMove]); /* [AS] Changed to full redraw! */
13859 SendToProgram("remove\n", &first);
13860 /*first.maybeThinking = TRUE;*/ /* GNU Chess does not ponder here */
13863 case BeginningOfGame:
13867 case IcsPlayingWhite:
13868 case IcsPlayingBlack:
13869 if (WhiteOnMove(forwardMostMove) == (gameMode == IcsPlayingWhite)) {
13870 SendToICS(ics_prefix);
13871 SendToICS("takeback 2\n");
13873 SendToICS(ics_prefix);
13874 SendToICS("takeback 1\n");
13883 ChessProgramState *cps;
13885 switch (gameMode) {
13886 case MachinePlaysWhite:
13887 if (!WhiteOnMove(forwardMostMove)) {
13888 DisplayError(_("It is your turn"), 0);
13893 case MachinePlaysBlack:
13894 if (WhiteOnMove(forwardMostMove)) {
13895 DisplayError(_("It is your turn"), 0);
13900 case TwoMachinesPlay:
13901 if (WhiteOnMove(forwardMostMove) ==
13902 (first.twoMachinesColor[0] == 'w')) {
13908 case BeginningOfGame:
13912 SendToProgram("?\n", cps);
13916 TruncateGameEvent()
13919 if (gameMode != EditGame) return;
13926 CleanupTail(); // [HGM] vari: only keep current variation if we explicitly truncate
13927 if (forwardMostMove > currentMove) {
13928 if (gameInfo.resultDetails != NULL) {
13929 free(gameInfo.resultDetails);
13930 gameInfo.resultDetails = NULL;
13931 gameInfo.result = GameUnfinished;
13933 forwardMostMove = currentMove;
13934 HistorySet(parseList, backwardMostMove, forwardMostMove,
13942 if (appData.noChessProgram) return;
13943 switch (gameMode) {
13944 case MachinePlaysWhite:
13945 if (WhiteOnMove(forwardMostMove)) {
13946 DisplayError(_("Wait until your turn"), 0);
13950 case BeginningOfGame:
13951 case MachinePlaysBlack:
13952 if (!WhiteOnMove(forwardMostMove)) {
13953 DisplayError(_("Wait until your turn"), 0);
13958 DisplayError(_("No hint available"), 0);
13961 SendToProgram("hint\n", &first);
13962 hintRequested = TRUE;
13968 if (appData.noChessProgram) return;
13969 switch (gameMode) {
13970 case MachinePlaysWhite:
13971 if (WhiteOnMove(forwardMostMove)) {
13972 DisplayError(_("Wait until your turn"), 0);
13976 case BeginningOfGame:
13977 case MachinePlaysBlack:
13978 if (!WhiteOnMove(forwardMostMove)) {
13979 DisplayError(_("Wait until your turn"), 0);
13984 EditPositionDone(TRUE);
13986 case TwoMachinesPlay:
13991 SendToProgram("bk\n", &first);
13992 bookOutput[0] = NULLCHAR;
13993 bookRequested = TRUE;
13999 char *tags = PGNTags(&gameInfo);
14000 TagsPopUp(tags, CmailMsg());
14004 /* end button procedures */
14007 PrintPosition(fp, move)
14013 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
14014 for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
14015 char c = PieceToChar(boards[move][i][j]);
14016 fputc(c == 'x' ? '.' : c, fp);
14017 fputc(j == BOARD_RGHT - 1 ? '\n' : ' ', fp);
14020 if ((gameMode == EditPosition) ? !blackPlaysFirst : (move % 2 == 0))
14021 fprintf(fp, "white to play\n");
14023 fprintf(fp, "black to play\n");
14030 if (gameInfo.white != NULL) {
14031 fprintf(fp, "\t%s vs. %s\n", gameInfo.white, gameInfo.black);
14037 /* Find last component of program's own name, using some heuristics */
14039 TidyProgramName(prog, host, buf)
14040 char *prog, *host, buf[MSG_SIZ];
14043 int local = (strcmp(host, "localhost") == 0);
14044 while (!local && (p = strchr(prog, ';')) != NULL) {
14046 while (*p == ' ') p++;
14049 if (*prog == '"' || *prog == '\'') {
14050 q = strchr(prog + 1, *prog);
14052 q = strchr(prog, ' ');
14054 if (q == NULL) q = prog + strlen(prog);
14056 while (p >= prog && *p != '/' && *p != '\\') p--;
14058 if(p == prog && *p == '"') p++;
14059 if (q - p >= 4 && StrCaseCmp(q - 4, ".exe") == 0) q -= 4;
14060 memcpy(buf, p, q - p);
14061 buf[q - p] = NULLCHAR;
14069 TimeControlTagValue()
14072 if (!appData.clockMode) {
14073 safeStrCpy(buf, "-", sizeof(buf)/sizeof(buf[0]));
14074 } else if (movesPerSession > 0) {
14075 snprintf(buf, MSG_SIZ, "%d/%ld", movesPerSession, timeControl/1000);
14076 } else if (timeIncrement == 0) {
14077 snprintf(buf, MSG_SIZ, "%ld", timeControl/1000);
14079 snprintf(buf, MSG_SIZ, "%ld+%ld", timeControl/1000, timeIncrement/1000);
14081 return StrSave(buf);
14087 /* This routine is used only for certain modes */
14088 VariantClass v = gameInfo.variant;
14089 ChessMove r = GameUnfinished;
14092 if(gameMode == EditGame) { // [HGM] vari: do not erase result on EditGame
14093 r = gameInfo.result;
14094 p = gameInfo.resultDetails;
14095 gameInfo.resultDetails = NULL;
14097 ClearGameInfo(&gameInfo);
14098 gameInfo.variant = v;
14100 switch (gameMode) {
14101 case MachinePlaysWhite:
14102 gameInfo.event = StrSave( appData.pgnEventHeader );
14103 gameInfo.site = StrSave(HostName());
14104 gameInfo.date = PGNDate();
14105 gameInfo.round = StrSave("-");
14106 gameInfo.white = StrSave(first.tidy);
14107 gameInfo.black = StrSave(UserName());
14108 gameInfo.timeControl = TimeControlTagValue();
14111 case MachinePlaysBlack:
14112 gameInfo.event = StrSave( appData.pgnEventHeader );
14113 gameInfo.site = StrSave(HostName());
14114 gameInfo.date = PGNDate();
14115 gameInfo.round = StrSave("-");
14116 gameInfo.white = StrSave(UserName());
14117 gameInfo.black = StrSave(first.tidy);
14118 gameInfo.timeControl = TimeControlTagValue();
14121 case TwoMachinesPlay:
14122 gameInfo.event = StrSave( appData.pgnEventHeader );
14123 gameInfo.site = StrSave(HostName());
14124 gameInfo.date = PGNDate();
14127 snprintf(buf, MSG_SIZ, "%d", roundNr);
14128 gameInfo.round = StrSave(buf);
14130 gameInfo.round = StrSave("-");
14132 if (first.twoMachinesColor[0] == 'w') {
14133 gameInfo.white = StrSave(appData.pgnName[0][0] ? appData.pgnName[0] : first.tidy);
14134 gameInfo.black = StrSave(appData.pgnName[1][0] ? appData.pgnName[1] : second.tidy);
14136 gameInfo.white = StrSave(appData.pgnName[1][0] ? appData.pgnName[1] : second.tidy);
14137 gameInfo.black = StrSave(appData.pgnName[0][0] ? appData.pgnName[0] : first.tidy);
14139 gameInfo.timeControl = TimeControlTagValue();
14143 gameInfo.event = StrSave("Edited game");
14144 gameInfo.site = StrSave(HostName());
14145 gameInfo.date = PGNDate();
14146 gameInfo.round = StrSave("-");
14147 gameInfo.white = StrSave("-");
14148 gameInfo.black = StrSave("-");
14149 gameInfo.result = r;
14150 gameInfo.resultDetails = p;
14154 gameInfo.event = StrSave("Edited position");
14155 gameInfo.site = StrSave(HostName());
14156 gameInfo.date = PGNDate();
14157 gameInfo.round = StrSave("-");
14158 gameInfo.white = StrSave("-");
14159 gameInfo.black = StrSave("-");
14162 case IcsPlayingWhite:
14163 case IcsPlayingBlack:
14168 case PlayFromGameFile:
14169 gameInfo.event = StrSave("Game from non-PGN file");
14170 gameInfo.site = StrSave(HostName());
14171 gameInfo.date = PGNDate();
14172 gameInfo.round = StrSave("-");
14173 gameInfo.white = StrSave("?");
14174 gameInfo.black = StrSave("?");
14183 ReplaceComment(index, text)
14191 if(index && sscanf(text, "%f/%d", &score, &len) == 2 &&
14192 pvInfoList[index-1].depth == len &&
14193 fabs(pvInfoList[index-1].score - score*100.) < 0.5 &&
14194 (p = strchr(text, '\n'))) text = p; // [HGM] strip off first line with PV info, if any
14195 while (*text == '\n') text++;
14196 len = strlen(text);
14197 while (len > 0 && text[len - 1] == '\n') len--;
14199 if (commentList[index] != NULL)
14200 free(commentList[index]);
14203 commentList[index] = NULL;
14206 if( *text == '{' && strchr(text, '}') || // [HGM] braces: if certainy malformed, put braces
14207 *text == '[' && strchr(text, ']') || // otherwise hope the user knows what he is doing
14208 *text == '(' && strchr(text, ')')) { // (perhaps check if this parses as comment-only?)
14209 commentList[index] = (char *) malloc(len + 2);
14210 strncpy(commentList[index], text, len);
14211 commentList[index][len] = '\n';
14212 commentList[index][len + 1] = NULLCHAR;
14214 // [HGM] braces: if text does not start with known OK delimiter, put braces around it.
14216 commentList[index] = (char *) malloc(len + 7);
14217 safeStrCpy(commentList[index], "{\n", 3);
14218 safeStrCpy(commentList[index]+2, text, len+1);
14219 commentList[index][len+2] = NULLCHAR;
14220 while(p = strchr(commentList[index], '}')) *p = ')'; // kill all } to make it one comment
14221 strcat(commentList[index], "\n}\n");
14235 if (ch == '\r') continue;
14237 } while (ch != '\0');
14241 AppendComment(index, text, addBraces)
14244 Boolean addBraces; // [HGM] braces: tells if we should add {}
14249 if(appData.debugMode) fprintf(debugFP, "Append: in='%s' %d\n", text, addBraces); fflush(debugFP);
14250 text = GetInfoFromComment( index, text ); /* [HGM] PV time: strip PV info from comment */
14253 while (*text == '\n') text++;
14254 len = strlen(text);
14255 while (len > 0 && text[len - 1] == '\n') len--;
14257 if (len == 0) return;
14259 if (commentList[index] != NULL) {
14260 old = commentList[index];
14261 oldlen = strlen(old);
14262 while(commentList[index][oldlen-1] == '\n')
14263 commentList[index][--oldlen] = NULLCHAR;
14264 commentList[index] = (char *) malloc(oldlen + len + 6); // might waste 4
14265 safeStrCpy(commentList[index], old, oldlen + len + 6);
14267 // [HGM] braces: join "{A\n}\n" + "{\nB}" as "{A\nB\n}"
14268 if(commentList[index][oldlen-1] == '}' && (text[0] == '{' || addBraces == TRUE)) {
14269 if(addBraces == TRUE) addBraces = FALSE; else { text++; len--; }
14270 while (*text == '\n') { text++; len--; }
14271 commentList[index][--oldlen] = NULLCHAR;
14273 if(addBraces) strcat(commentList[index], addBraces == 2 ? "\n(" : "\n{\n");
14274 else strcat(commentList[index], "\n");
14275 strcat(commentList[index], text);
14276 if(addBraces) strcat(commentList[index], addBraces == 2 ? ")\n" : "\n}\n");
14277 else strcat(commentList[index], "\n");
14279 commentList[index] = (char *) malloc(len + 6); // perhaps wastes 4...
14281 safeStrCpy(commentList[index], addBraces == 2 ? "(" : "{\n", 3);
14282 else commentList[index][0] = NULLCHAR;
14283 strcat(commentList[index], text);
14284 strcat(commentList[index], addBraces == 2 ? ")\n" : "\n");
14285 if(addBraces == TRUE) strcat(commentList[index], "}\n");
14289 static char * FindStr( char * text, char * sub_text )
14291 char * result = strstr( text, sub_text );
14293 if( result != NULL ) {
14294 result += strlen( sub_text );
14300 /* [AS] Try to extract PV info from PGN comment */
14301 /* [HGM] PV time: and then remove it, to prevent it appearing twice */
14302 char *GetInfoFromComment( int index, char * text )
14304 char * sep = text, *p;
14306 if( text != NULL && index > 0 ) {
14309 int time = -1, sec = 0, deci;
14310 char * s_eval = FindStr( text, "[%eval " );
14311 char * s_emt = FindStr( text, "[%emt " );
14313 if( s_eval != NULL || s_emt != NULL ) {
14317 if( s_eval != NULL ) {
14318 if( sscanf( s_eval, "%d,%d%c", &score, &depth, &delim ) != 3 ) {
14322 if( delim != ']' ) {
14327 if( s_emt != NULL ) {
14332 /* We expect something like: [+|-]nnn.nn/dd */
14335 if(*text != '{') return text; // [HGM] braces: must be normal comment
14337 sep = strchr( text, '/' );
14338 if( sep == NULL || sep < (text+4) ) {
14343 if(p[1] == '(') { // comment starts with PV
14344 p = strchr(p, ')'); // locate end of PV
14345 if(p == NULL || sep < p+5) return text;
14346 // at this point we have something like "{(.*) +0.23/6 ..."
14347 p = text; while(*++p != ')') p[-1] = *p; p[-1] = ')';
14348 *p = '\n'; while(*p == ' ' || *p == '\n') p++; *--p = '{';
14349 // we now moved the brace to behind the PV: "(.*) {+0.23/6 ..."
14351 time = -1; sec = -1; deci = -1;
14352 if( sscanf( p+1, "%d.%d/%d %d:%d", &score, &score_lo, &depth, &time, &sec ) != 5 &&
14353 sscanf( p+1, "%d.%d/%d %d.%d", &score, &score_lo, &depth, &time, &deci ) != 5 &&
14354 sscanf( p+1, "%d.%d/%d %d", &score, &score_lo, &depth, &time ) != 4 &&
14355 sscanf( p+1, "%d.%d/%d", &score, &score_lo, &depth ) != 3 ) {
14359 if( score_lo < 0 || score_lo >= 100 ) {
14363 if(sec >= 0) time = 600*time + 10*sec; else
14364 if(deci >= 0) time = 10*time + deci; else time *= 10; // deci-sec
14366 score = score >= 0 ? score*100 + score_lo : score*100 - score_lo;
14368 /* [HGM] PV time: now locate end of PV info */
14369 while( *++sep >= '0' && *sep <= '9'); // strip depth
14371 while( *++sep >= '0' && *sep <= '9' || *sep == '\n'); // strip time
14373 while( *++sep >= '0' && *sep <= '9'); // strip seconds
14375 while( *++sep >= '0' && *sep <= '9'); // strip fractional seconds
14376 while(*sep == ' ') sep++;
14387 pvInfoList[index-1].depth = depth;
14388 pvInfoList[index-1].score = score;
14389 pvInfoList[index-1].time = 10*time; // centi-sec
14390 if(*sep == '}') *sep = 0; else *--sep = '{';
14391 if(p != text) { while(*p++ = *sep++); sep = text; } // squeeze out space between PV and comment, and return both
14397 SendToProgram(message, cps)
14399 ChessProgramState *cps;
14401 int count, outCount, error;
14404 if (cps->pr == NULL) return;
14407 if (appData.debugMode) {
14410 fprintf(debugFP, "%ld >%-6s: %s",
14411 SubtractTimeMarks(&now, &programStartTime),
14412 cps->which, message);
14415 count = strlen(message);
14416 outCount = OutputToProcess(cps->pr, message, count, &error);
14417 if (outCount < count && !exiting
14418 && !endingGame) { /* [HGM] crash: to not hang GameEnds() writing to deceased engines */
14419 if(!cps->initDone) return; // [HGM] should not generate fatal error during engine load
14420 snprintf(buf, MSG_SIZ, _("Error writing to %s chess program"), _(cps->which));
14421 if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
14422 if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
14423 snprintf(buf, MSG_SIZ, _("%s program exits in draw position (%s)"), _(cps->which), cps->program);
14424 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(GameIsDrawn, buf, GE_XBOARD); return; }
14425 gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
14427 ChessMove res = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
14428 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(res, buf, GE_XBOARD); return; }
14429 gameInfo.result = res;
14431 gameInfo.resultDetails = StrSave(buf);
14433 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; return; }
14434 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
14439 ReceiveFromProgram(isr, closure, message, count, error)
14440 InputSourceRef isr;
14448 ChessProgramState *cps = (ChessProgramState *)closure;
14450 if (isr != cps->isr) return; /* Killed intentionally */
14453 RemoveInputSource(cps->isr);
14454 if(!cps->initDone) return; // [HGM] should not generate fatal error during engine load
14455 snprintf(buf, MSG_SIZ, _("Error: %s chess program (%s) exited unexpectedly"),
14456 _(cps->which), cps->program);
14457 if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
14458 if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
14459 snprintf(buf, MSG_SIZ, _("%s program exits in draw position (%s)"), _(cps->which), cps->program);
14460 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(GameIsDrawn, buf, GE_XBOARD); return; }
14461 gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
14463 ChessMove res = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
14464 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(res, buf, GE_XBOARD); return; }
14465 gameInfo.result = res;
14467 gameInfo.resultDetails = StrSave(buf);
14469 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; return; }
14470 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, 0, 1); else errorExitStatus = 1;
14472 snprintf(buf, MSG_SIZ, _("Error reading from %s chess program (%s)"),
14473 _(cps->which), cps->program);
14474 RemoveInputSource(cps->isr);
14476 /* [AS] Program is misbehaving badly... kill it */
14477 if( count == -2 ) {
14478 DestroyChildProcess( cps->pr, 9 );
14482 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
14487 if ((end_str = strchr(message, '\r')) != NULL)
14488 *end_str = NULLCHAR;
14489 if ((end_str = strchr(message, '\n')) != NULL)
14490 *end_str = NULLCHAR;
14492 if (appData.debugMode) {
14493 TimeMark now; int print = 1;
14494 char *quote = ""; char c; int i;
14496 if(appData.engineComments != 1) { /* [HGM] debug: decide if protocol-violating output is written */
14497 char start = message[0];
14498 if(start >='A' && start <= 'Z') start += 'a' - 'A'; // be tolerant to capitalizing
14499 if(sscanf(message, "%d%c%d%d%d", &i, &c, &i, &i, &i) != 5 &&
14500 sscanf(message, "move %c", &c)!=1 && sscanf(message, "offer%c", &c)!=1 &&
14501 sscanf(message, "resign%c", &c)!=1 && sscanf(message, "feature %c", &c)!=1 &&
14502 sscanf(message, "error %c", &c)!=1 && sscanf(message, "illegal %c", &c)!=1 &&
14503 sscanf(message, "tell%c", &c)!=1 && sscanf(message, "0-1 %c", &c)!=1 &&
14504 sscanf(message, "1-0 %c", &c)!=1 && sscanf(message, "1/2-1/2 %c", &c)!=1 &&
14505 sscanf(message, "setboard %c", &c)!=1 && sscanf(message, "setup %c", &c)!=1 &&
14506 sscanf(message, "hint: %c", &c)!=1 &&
14507 sscanf(message, "pong %c", &c)!=1 && start != '#') {
14508 quote = appData.engineComments == 2 ? "# " : "### NON-COMPLIANT! ### ";
14509 print = (appData.engineComments >= 2);
14511 message[0] = start; // restore original message
14515 fprintf(debugFP, "%ld <%-6s: %s%s\n",
14516 SubtractTimeMarks(&now, &programStartTime), cps->which,
14522 /* [DM] if icsEngineAnalyze is active we block all whisper and kibitz output, because nobody want to see this */
14523 if (appData.icsEngineAnalyze) {
14524 if (strstr(message, "whisper") != NULL ||
14525 strstr(message, "kibitz") != NULL ||
14526 strstr(message, "tellics") != NULL) return;
14529 HandleMachineMove(message, cps);
14534 SendTimeControl(cps, mps, tc, inc, sd, st)
14535 ChessProgramState *cps;
14536 int mps, inc, sd, st;
14542 if( timeControl_2 > 0 ) {
14543 if( (gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b') ) {
14544 tc = timeControl_2;
14547 tc /= cps->timeOdds; /* [HGM] time odds: apply before telling engine */
14548 inc /= cps->timeOdds;
14549 st /= cps->timeOdds;
14551 seconds = (tc / 1000) % 60; /* [HGM] displaced to after applying odds */
14554 /* Set exact time per move, normally using st command */
14555 if (cps->stKludge) {
14556 /* GNU Chess 4 has no st command; uses level in a nonstandard way */
14558 if (seconds == 0) {
14559 snprintf(buf, MSG_SIZ, "level 1 %d\n", st/60);
14561 snprintf(buf, MSG_SIZ, "level 1 %d:%02d\n", st/60, seconds);
14564 snprintf(buf, MSG_SIZ, "st %d\n", st);
14567 /* Set conventional or incremental time control, using level command */
14568 if (seconds == 0) {
14569 /* Note old gnuchess bug -- minutes:seconds used to not work.
14570 Fixed in later versions, but still avoid :seconds
14571 when seconds is 0. */
14572 snprintf(buf, MSG_SIZ, "level %d %ld %g\n", mps, tc/60000, inc/1000.);
14574 snprintf(buf, MSG_SIZ, "level %d %ld:%02d %g\n", mps, tc/60000,
14575 seconds, inc/1000.);
14578 SendToProgram(buf, cps);
14580 /* Orthoganally (except for GNU Chess 4), limit time to st seconds */
14581 /* Orthogonally, limit search to given depth */
14583 if (cps->sdKludge) {
14584 snprintf(buf, MSG_SIZ, "depth\n%d\n", sd);
14586 snprintf(buf, MSG_SIZ, "sd %d\n", sd);
14588 SendToProgram(buf, cps);
14591 if(cps->nps >= 0) { /* [HGM] nps */
14592 if(cps->supportsNPS == FALSE)
14593 cps->nps = -1; // don't use if engine explicitly says not supported!
14595 snprintf(buf, MSG_SIZ, "nps %d\n", cps->nps);
14596 SendToProgram(buf, cps);
14601 ChessProgramState *WhitePlayer()
14602 /* [HGM] return pointer to 'first' or 'second', depending on who plays white */
14604 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b' ||
14605 gameMode == BeginningOfGame || gameMode == MachinePlaysBlack)
14611 SendTimeRemaining(cps, machineWhite)
14612 ChessProgramState *cps;
14613 int /*boolean*/ machineWhite;
14615 char message[MSG_SIZ];
14618 /* Note: this routine must be called when the clocks are stopped
14619 or when they have *just* been set or switched; otherwise
14620 it will be off by the time since the current tick started.
14622 if (machineWhite) {
14623 time = whiteTimeRemaining / 10;
14624 otime = blackTimeRemaining / 10;
14626 time = blackTimeRemaining / 10;
14627 otime = whiteTimeRemaining / 10;
14629 /* [HGM] translate opponent's time by time-odds factor */
14630 otime = (otime * cps->other->timeOdds) / cps->timeOdds;
14631 if (appData.debugMode) {
14632 fprintf(debugFP, "time odds: %f %f \n", cps->timeOdds, cps->other->timeOdds);
14635 if (time <= 0) time = 1;
14636 if (otime <= 0) otime = 1;
14638 snprintf(message, MSG_SIZ, "time %ld\n", time);
14639 SendToProgram(message, cps);
14641 snprintf(message, MSG_SIZ, "otim %ld\n", otime);
14642 SendToProgram(message, cps);
14646 BoolFeature(p, name, loc, cps)
14650 ChessProgramState *cps;
14653 int len = strlen(name);
14656 if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
14658 sscanf(*p, "%d", &val);
14660 while (**p && **p != ' ')
14662 snprintf(buf, MSG_SIZ, "accepted %s\n", name);
14663 SendToProgram(buf, cps);
14670 IntFeature(p, name, loc, cps)
14674 ChessProgramState *cps;
14677 int len = strlen(name);
14678 if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
14680 sscanf(*p, "%d", loc);
14681 while (**p && **p != ' ') (*p)++;
14682 snprintf(buf, MSG_SIZ, "accepted %s\n", name);
14683 SendToProgram(buf, cps);
14690 StringFeature(p, name, loc, cps)
14694 ChessProgramState *cps;
14697 int len = strlen(name);
14698 if (strncmp((*p), name, len) == 0
14699 && (*p)[len] == '=' && (*p)[len+1] == '\"') {
14701 sscanf(*p, "%[^\"]", loc);
14702 while (**p && **p != '\"') (*p)++;
14703 if (**p == '\"') (*p)++;
14704 snprintf(buf, MSG_SIZ, "accepted %s\n", name);
14705 SendToProgram(buf, cps);
14712 ParseOption(Option *opt, ChessProgramState *cps)
14713 // [HGM] options: process the string that defines an engine option, and determine
14714 // name, type, default value, and allowed value range
14716 char *p, *q, buf[MSG_SIZ];
14717 int n, min = (-1)<<31, max = 1<<31, def;
14719 if(p = strstr(opt->name, " -spin ")) {
14720 if((n = sscanf(p, " -spin %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
14721 if(max < min) max = min; // enforce consistency
14722 if(def < min) def = min;
14723 if(def > max) def = max;
14728 } else if((p = strstr(opt->name, " -slider "))) {
14729 // for now -slider is a synonym for -spin, to already provide compatibility with future polyglots
14730 if((n = sscanf(p, " -slider %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
14731 if(max < min) max = min; // enforce consistency
14732 if(def < min) def = min;
14733 if(def > max) def = max;
14737 opt->type = Spin; // Slider;
14738 } else if((p = strstr(opt->name, " -string "))) {
14739 opt->textValue = p+9;
14740 opt->type = TextBox;
14741 } else if((p = strstr(opt->name, " -file "))) {
14742 // for now -file is a synonym for -string, to already provide compatibility with future polyglots
14743 opt->textValue = p+7;
14744 opt->type = FileName; // FileName;
14745 } else if((p = strstr(opt->name, " -path "))) {
14746 // for now -file is a synonym for -string, to already provide compatibility with future polyglots
14747 opt->textValue = p+7;
14748 opt->type = PathName; // PathName;
14749 } else if(p = strstr(opt->name, " -check ")) {
14750 if(sscanf(p, " -check %d", &def) < 1) return FALSE;
14751 opt->value = (def != 0);
14752 opt->type = CheckBox;
14753 } else if(p = strstr(opt->name, " -combo ")) {
14754 opt->textValue = (char*) (&cps->comboList[cps->comboCnt]); // cheat with pointer type
14755 cps->comboList[cps->comboCnt++] = q = p+8; // holds possible choices
14756 if(*q == '*') cps->comboList[cps->comboCnt-1]++;
14757 opt->value = n = 0;
14758 while(q = StrStr(q, " /// ")) {
14759 n++; *q = 0; // count choices, and null-terminate each of them
14761 if(*q == '*') { // remember default, which is marked with * prefix
14765 cps->comboList[cps->comboCnt++] = q;
14767 cps->comboList[cps->comboCnt++] = NULL;
14769 opt->type = ComboBox;
14770 } else if(p = strstr(opt->name, " -button")) {
14771 opt->type = Button;
14772 } else if(p = strstr(opt->name, " -save")) {
14773 opt->type = SaveButton;
14774 } else return FALSE;
14775 *p = 0; // terminate option name
14776 // now look if the command-line options define a setting for this engine option.
14777 if(cps->optionSettings && cps->optionSettings[0])
14778 p = strstr(cps->optionSettings, opt->name); else p = NULL;
14779 if(p && (p == cps->optionSettings || p[-1] == ',')) {
14780 snprintf(buf, MSG_SIZ, "option %s", p);
14781 if(p = strstr(buf, ",")) *p = 0;
14782 if(q = strchr(buf, '=')) switch(opt->type) {
14784 for(n=0; n<opt->max; n++)
14785 if(!strcmp(((char**)opt->textValue)[n], q+1)) opt->value = n;
14788 safeStrCpy(opt->textValue, q+1, MSG_SIZ - (opt->textValue - opt->name));
14792 opt->value = atoi(q+1);
14797 SendToProgram(buf, cps);
14803 FeatureDone(cps, val)
14804 ChessProgramState* cps;
14807 DelayedEventCallback cb = GetDelayedEvent();
14808 if ((cb == InitBackEnd3 && cps == &first) ||
14809 (cb == SettingsMenuIfReady && cps == &second) ||
14810 (cb == LoadEngine) ||
14811 (cb == TwoMachinesEventIfReady)) {
14812 CancelDelayedEvent();
14813 ScheduleDelayedEvent(cb, val ? 1 : 3600000);
14815 cps->initDone = val;
14818 /* Parse feature command from engine */
14820 ParseFeatures(args, cps)
14822 ChessProgramState *cps;
14830 while (*p == ' ') p++;
14831 if (*p == NULLCHAR) return;
14833 if (BoolFeature(&p, "setboard", &cps->useSetboard, cps)) continue;
14834 if (BoolFeature(&p, "time", &cps->sendTime, cps)) continue;
14835 if (BoolFeature(&p, "draw", &cps->sendDrawOffers, cps)) continue;
14836 if (BoolFeature(&p, "sigint", &cps->useSigint, cps)) continue;
14837 if (BoolFeature(&p, "sigterm", &cps->useSigterm, cps)) continue;
14838 if (BoolFeature(&p, "reuse", &val, cps)) {
14839 /* Engine can disable reuse, but can't enable it if user said no */
14840 if (!val) cps->reuse = FALSE;
14843 if (BoolFeature(&p, "analyze", &cps->analysisSupport, cps)) continue;
14844 if (StringFeature(&p, "myname", &cps->tidy, cps)) {
14845 if (gameMode == TwoMachinesPlay) {
14846 DisplayTwoMachinesTitle();
14852 if (StringFeature(&p, "variants", &cps->variants, cps)) continue;
14853 if (BoolFeature(&p, "san", &cps->useSAN, cps)) continue;
14854 if (BoolFeature(&p, "ping", &cps->usePing, cps)) continue;
14855 if (BoolFeature(&p, "playother", &cps->usePlayother, cps)) continue;
14856 if (BoolFeature(&p, "colors", &cps->useColors, cps)) continue;
14857 if (BoolFeature(&p, "usermove", &cps->useUsermove, cps)) continue;
14858 if (BoolFeature(&p, "ics", &cps->sendICS, cps)) continue;
14859 if (BoolFeature(&p, "name", &cps->sendName, cps)) continue;
14860 if (BoolFeature(&p, "pause", &val, cps)) continue; /* unused at present */
14861 if (IntFeature(&p, "done", &val, cps)) {
14862 FeatureDone(cps, val);
14865 /* Added by Tord: */
14866 if (BoolFeature(&p, "fen960", &cps->useFEN960, cps)) continue;
14867 if (BoolFeature(&p, "oocastle", &cps->useOOCastle, cps)) continue;
14868 /* End of additions by Tord */
14870 /* [HGM] added features: */
14871 if (BoolFeature(&p, "debug", &cps->debug, cps)) continue;
14872 if (BoolFeature(&p, "nps", &cps->supportsNPS, cps)) continue;
14873 if (IntFeature(&p, "level", &cps->maxNrOfSessions, cps)) continue;
14874 if (BoolFeature(&p, "memory", &cps->memSize, cps)) continue;
14875 if (BoolFeature(&p, "smp", &cps->maxCores, cps)) continue;
14876 if (StringFeature(&p, "egt", &cps->egtFormats, cps)) continue;
14877 if (StringFeature(&p, "option", &(cps->option[cps->nrOptions].name), cps)) {
14878 if(!ParseOption(&(cps->option[cps->nrOptions++]), cps)) { // [HGM] options: add option feature
14879 snprintf(buf, MSG_SIZ, "rejected option %s\n", cps->option[--cps->nrOptions].name);
14880 SendToProgram(buf, cps);
14883 if(cps->nrOptions >= MAX_OPTIONS) {
14885 snprintf(buf, MSG_SIZ, _("%s engine has too many options\n"), _(cps->which));
14886 DisplayError(buf, 0);
14890 /* End of additions by HGM */
14892 /* unknown feature: complain and skip */
14894 while (*q && *q != '=') q++;
14895 snprintf(buf, MSG_SIZ,"rejected %.*s\n", (int)(q-p), p);
14896 SendToProgram(buf, cps);
14902 while (*p && *p != '\"') p++;
14903 if (*p == '\"') p++;
14905 while (*p && *p != ' ') p++;
14913 PeriodicUpdatesEvent(newState)
14916 if (newState == appData.periodicUpdates)
14919 appData.periodicUpdates=newState;
14921 /* Display type changes, so update it now */
14922 // DisplayAnalysis();
14924 /* Get the ball rolling again... */
14926 AnalysisPeriodicEvent(1);
14927 StartAnalysisClock();
14932 PonderNextMoveEvent(newState)
14935 if (newState == appData.ponderNextMove) return;
14936 if (gameMode == EditPosition) EditPositionDone(TRUE);
14938 SendToProgram("hard\n", &first);
14939 if (gameMode == TwoMachinesPlay) {
14940 SendToProgram("hard\n", &second);
14943 SendToProgram("easy\n", &first);
14944 thinkOutput[0] = NULLCHAR;
14945 if (gameMode == TwoMachinesPlay) {
14946 SendToProgram("easy\n", &second);
14949 appData.ponderNextMove = newState;
14953 NewSettingEvent(option, feature, command, value)
14955 int option, value, *feature;
14959 if (gameMode == EditPosition) EditPositionDone(TRUE);
14960 snprintf(buf, MSG_SIZ,"%s%s %d\n", (option ? "option ": ""), command, value);
14961 if(feature == NULL || *feature) SendToProgram(buf, &first);
14962 if (gameMode == TwoMachinesPlay) {
14963 if(feature == NULL || feature[(int*)&second - (int*)&first]) SendToProgram(buf, &second);
14968 ShowThinkingEvent()
14969 // [HGM] thinking: this routine is now also called from "Options -> Engine..." popup
14971 static int oldState = 2; // kludge alert! Neither true nor fals, so first time oldState is always updated
14972 int newState = appData.showThinking
14973 // [HGM] thinking: other features now need thinking output as well
14974 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp();
14976 if (oldState == newState) return;
14977 oldState = newState;
14978 if (gameMode == EditPosition) EditPositionDone(TRUE);
14980 SendToProgram("post\n", &first);
14981 if (gameMode == TwoMachinesPlay) {
14982 SendToProgram("post\n", &second);
14985 SendToProgram("nopost\n", &first);
14986 thinkOutput[0] = NULLCHAR;
14987 if (gameMode == TwoMachinesPlay) {
14988 SendToProgram("nopost\n", &second);
14991 // appData.showThinking = newState; // [HGM] thinking: responsible option should already have be changed when calling this routine!
14995 AskQuestionEvent(title, question, replyPrefix, which)
14996 char *title; char *question; char *replyPrefix; char *which;
14998 ProcRef pr = (which[0] == '1') ? first.pr : second.pr;
14999 if (pr == NoProc) return;
15000 AskQuestion(title, question, replyPrefix, pr);
15004 TypeInEvent(char firstChar)
15006 if ((gameMode == BeginningOfGame && !appData.icsActive) ||
\r
15007 gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack ||
\r
15008 gameMode == AnalyzeMode || gameMode == EditGame ||
\r
15009 gameMode == EditPosition || gameMode == IcsExamining ||
\r
15010 gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
\r
15011 isdigit(firstChar) && // [HGM] movenum: allow typing in of move nr in 'passive' modes
\r
15012 ( gameMode == AnalyzeFile || gameMode == PlayFromGameFile ||
\r
15013 gameMode == IcsObserving || gameMode == TwoMachinesPlay ) ||
\r
15014 gameMode == Training) PopUpMoveDialog(firstChar);
15018 TypeInDoneEvent(char *move)
15021 int n, fromX, fromY, toX, toY;
15023 ChessMove moveType;
\r
15026 if(gameMode == EditPosition && ParseFEN(board, &n, move) ) {
\r
15027 EditPositionPasteFEN(move);
\r
15030 // [HGM] movenum: allow move number to be typed in any mode
\r
15031 if(sscanf(move, "%d", &n) == 1 && n != 0 ) {
\r
15032 ToNrEvent(2*n-1);
\r
15036 if (gameMode != EditGame && currentMove != forwardMostMove &&
\r
15037 gameMode != Training) {
\r
15038 DisplayMoveError(_("Displayed move is not current"));
\r
15040 int ok = ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove,
\r
15041 &moveType, &fromX, &fromY, &toX, &toY, &promoChar);
\r
15042 if(!ok && move[0] >= 'a') { move[0] += 'A' - 'a'; ok = 2; } // [HGM] try also capitalized
\r
15043 if (ok==1 || ok && ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove,
\r
15044 &moveType, &fromX, &fromY, &toX, &toY, &promoChar)) {
\r
15045 UserMoveEvent(fromX, fromY, toX, toY, promoChar);
\r
15047 DisplayMoveError(_("Could not parse move"));
\r
15053 DisplayMove(moveNumber)
15056 char message[MSG_SIZ];
15058 char cpThinkOutput[MSG_SIZ];
15060 if(appData.noGUI) return; // [HGM] fast: suppress display of moves
15062 if (moveNumber == forwardMostMove - 1 ||
15063 gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
15065 safeStrCpy(cpThinkOutput, thinkOutput, sizeof(cpThinkOutput)/sizeof(cpThinkOutput[0]));
15067 if (strchr(cpThinkOutput, '\n')) {
15068 *strchr(cpThinkOutput, '\n') = NULLCHAR;
15071 *cpThinkOutput = NULLCHAR;
15074 /* [AS] Hide thinking from human user */
15075 if( appData.hideThinkingFromHuman && gameMode != TwoMachinesPlay ) {
15076 *cpThinkOutput = NULLCHAR;
15077 if( thinkOutput[0] != NULLCHAR ) {
15080 for( i=0; i<=hiddenThinkOutputState; i++ ) {
15081 cpThinkOutput[i] = '.';
15083 cpThinkOutput[i] = NULLCHAR;
15084 hiddenThinkOutputState = (hiddenThinkOutputState + 1) % 3;
15088 if (moveNumber == forwardMostMove - 1 &&
15089 gameInfo.resultDetails != NULL) {
15090 if (gameInfo.resultDetails[0] == NULLCHAR) {
15091 snprintf(res, MSG_SIZ, " %s", PGNResult(gameInfo.result));
15093 snprintf(res, MSG_SIZ, " {%s} %s",
15094 T_(gameInfo.resultDetails), PGNResult(gameInfo.result));
15100 if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
15101 DisplayMessage(res, cpThinkOutput);
15103 snprintf(message, MSG_SIZ, "%d.%s%s%s", moveNumber / 2 + 1,
15104 WhiteOnMove(moveNumber) ? " " : ".. ",
15105 parseList[moveNumber], res);
15106 DisplayMessage(message, cpThinkOutput);
15111 DisplayComment(moveNumber, text)
15115 char title[MSG_SIZ];
15116 char buf[8000]; // comment can be long!
15119 if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
15120 safeStrCpy(title, "Comment", sizeof(title)/sizeof(title[0]));
15122 snprintf(title,MSG_SIZ, "Comment on %d.%s%s", moveNumber / 2 + 1,
15123 WhiteOnMove(moveNumber) ? " " : ".. ",
15124 parseList[moveNumber]);
15126 // [HGM] PV info: display PV info together with (or as) comment
15127 if(moveNumber >= 0 && (depth = pvInfoList[moveNumber].depth) > 0) {
15128 if(text == NULL) text = "";
15129 score = pvInfoList[moveNumber].score;
15130 snprintf(buf,sizeof(buf)/sizeof(buf[0]), "%s%.2f/%d %d\n%s", score>0 ? "+" : "", score/100.,
15131 depth, (pvInfoList[moveNumber].time+50)/100, text);
15134 if (text != NULL && (appData.autoDisplayComment || commentUp))
15135 CommentPopUp(title, text);
15138 /* This routine sends a ^C interrupt to gnuchess, to awaken it if it
15139 * might be busy thinking or pondering. It can be omitted if your
15140 * gnuchess is configured to stop thinking immediately on any user
15141 * input. However, that gnuchess feature depends on the FIONREAD
15142 * ioctl, which does not work properly on some flavors of Unix.
15146 ChessProgramState *cps;
15149 if (!cps->useSigint) return;
15150 if (appData.noChessProgram || (cps->pr == NoProc)) return;
15151 switch (gameMode) {
15152 case MachinePlaysWhite:
15153 case MachinePlaysBlack:
15154 case TwoMachinesPlay:
15155 case IcsPlayingWhite:
15156 case IcsPlayingBlack:
15159 /* Skip if we know it isn't thinking */
15160 if (!cps->maybeThinking) return;
15161 if (appData.debugMode)
15162 fprintf(debugFP, "Interrupting %s\n", cps->which);
15163 InterruptChildProcess(cps->pr);
15164 cps->maybeThinking = FALSE;
15169 #endif /*ATTENTION*/
15175 if (whiteTimeRemaining <= 0) {
15178 if (appData.icsActive) {
15179 if (appData.autoCallFlag &&
15180 gameMode == IcsPlayingBlack && !blackFlag) {
15181 SendToICS(ics_prefix);
15182 SendToICS("flag\n");
15186 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
15188 if(gameMode != TwoMachinesPlay) DisplayTitle(_("White's flag fell"));
15189 if (appData.autoCallFlag) {
15190 GameEnds(BlackWins, "Black wins on time", GE_XBOARD);
15197 if (blackTimeRemaining <= 0) {
15200 if (appData.icsActive) {
15201 if (appData.autoCallFlag &&
15202 gameMode == IcsPlayingWhite && !whiteFlag) {
15203 SendToICS(ics_prefix);
15204 SendToICS("flag\n");
15208 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
15210 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Black's flag fell"));
15211 if (appData.autoCallFlag) {
15212 GameEnds(WhiteWins, "White wins on time", GE_XBOARD);
15225 if (!appData.clockMode || appData.icsActive || searchTime || // [HGM] st: no inc in st mode
15226 gameMode == PlayFromGameFile || forwardMostMove == 0) return;
15229 * add time to clocks when time control is achieved ([HGM] now also used for increment)
15231 if ( !WhiteOnMove(forwardMostMove) ) {
15232 /* White made time control */
15233 lastWhite -= whiteTimeRemaining; // [HGM] contains start time, socalculate thinking time
15234 whiteTimeRemaining += GetTimeQuota((forwardMostMove-whiteStartMove-1)/2, lastWhite, whiteTC)
15235 /* [HGM] time odds: correct new time quota for time odds! */
15236 / WhitePlayer()->timeOdds;
15237 lastBlack = blackTimeRemaining; // [HGM] leave absolute time (after quota), so next switch we can us it to calculate thinking time
15239 lastBlack -= blackTimeRemaining;
15240 /* Black made time control */
15241 blackTimeRemaining += GetTimeQuota((forwardMostMove-blackStartMove-1)/2, lastBlack, blackTC)
15242 / WhitePlayer()->other->timeOdds;
15243 lastWhite = whiteTimeRemaining;
15248 DisplayBothClocks()
15250 int wom = gameMode == EditPosition ?
15251 !blackPlaysFirst : WhiteOnMove(currentMove);
15252 DisplayWhiteClock(whiteTimeRemaining, wom);
15253 DisplayBlackClock(blackTimeRemaining, !wom);
15257 /* Timekeeping seems to be a portability nightmare. I think everyone
15258 has ftime(), but I'm really not sure, so I'm including some ifdefs
15259 to use other calls if you don't. Clocks will be less accurate if
15260 you have neither ftime nor gettimeofday.
15263 /* VS 2008 requires the #include outside of the function */
15264 #if !HAVE_GETTIMEOFDAY && HAVE_FTIME
15265 #include <sys/timeb.h>
15268 /* Get the current time as a TimeMark */
15273 #if HAVE_GETTIMEOFDAY
15275 struct timeval timeVal;
15276 struct timezone timeZone;
15278 gettimeofday(&timeVal, &timeZone);
15279 tm->sec = (long) timeVal.tv_sec;
15280 tm->ms = (int) (timeVal.tv_usec / 1000L);
15282 #else /*!HAVE_GETTIMEOFDAY*/
15285 // include <sys/timeb.h> / moved to just above start of function
15286 struct timeb timeB;
15289 tm->sec = (long) timeB.time;
15290 tm->ms = (int) timeB.millitm;
15292 #else /*!HAVE_FTIME && !HAVE_GETTIMEOFDAY*/
15293 tm->sec = (long) time(NULL);
15299 /* Return the difference in milliseconds between two
15300 time marks. We assume the difference will fit in a long!
15303 SubtractTimeMarks(tm2, tm1)
15304 TimeMark *tm2, *tm1;
15306 return 1000L*(tm2->sec - tm1->sec) +
15307 (long) (tm2->ms - tm1->ms);
15312 * Code to manage the game clocks.
15314 * In tournament play, black starts the clock and then white makes a move.
15315 * We give the human user a slight advantage if he is playing white---the
15316 * clocks don't run until he makes his first move, so it takes zero time.
15317 * Also, we don't account for network lag, so we could get out of sync
15318 * with GNU Chess's clock -- but then, referees are always right.
15321 static TimeMark tickStartTM;
15322 static long intendedTickLength;
15325 NextTickLength(timeRemaining)
15326 long timeRemaining;
15328 long nominalTickLength, nextTickLength;
15330 if (timeRemaining > 0L && timeRemaining <= 10000L)
15331 nominalTickLength = 100L;
15333 nominalTickLength = 1000L;
15334 nextTickLength = timeRemaining % nominalTickLength;
15335 if (nextTickLength <= 0) nextTickLength += nominalTickLength;
15337 return nextTickLength;
15340 /* Adjust clock one minute up or down */
15342 AdjustClock(Boolean which, int dir)
15344 if(which) blackTimeRemaining += 60000*dir;
15345 else whiteTimeRemaining += 60000*dir;
15346 DisplayBothClocks();
15349 /* Stop clocks and reset to a fresh time control */
15353 (void) StopClockTimer();
15354 if (appData.icsActive) {
15355 whiteTimeRemaining = blackTimeRemaining = 0;
15356 } else if (searchTime) {
15357 whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
15358 blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
15359 } else { /* [HGM] correct new time quote for time odds */
15360 whiteTC = blackTC = fullTimeControlString;
15361 whiteTimeRemaining = GetTimeQuota(-1, 0, whiteTC) / WhitePlayer()->timeOdds;
15362 blackTimeRemaining = GetTimeQuota(-1, 0, blackTC) / WhitePlayer()->other->timeOdds;
15364 if (whiteFlag || blackFlag) {
15366 whiteFlag = blackFlag = FALSE;
15368 lastWhite = lastBlack = whiteStartMove = blackStartMove = 0;
15369 DisplayBothClocks();
15372 #define FUDGE 25 /* 25ms = 1/40 sec; should be plenty even for 50 Hz clocks */
15374 /* Decrement running clock by amount of time that has passed */
15378 long timeRemaining;
15379 long lastTickLength, fudge;
15382 if (!appData.clockMode) return;
15383 if (gameMode==AnalyzeMode || gameMode == AnalyzeFile) return;
15387 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
15389 /* Fudge if we woke up a little too soon */
15390 fudge = intendedTickLength - lastTickLength;
15391 if (fudge < 0 || fudge > FUDGE) fudge = 0;
15393 if (WhiteOnMove(forwardMostMove)) {
15394 if(whiteNPS >= 0) lastTickLength = 0;
15395 timeRemaining = whiteTimeRemaining -= lastTickLength;
15396 if(timeRemaining < 0 && !appData.icsActive) {
15397 GetTimeQuota((forwardMostMove-whiteStartMove-1)/2, 0, whiteTC); // sets suddenDeath & nextSession;
15398 if(suddenDeath) { // [HGM] if we run out of a non-last incremental session, go to the next
15399 whiteStartMove = forwardMostMove; whiteTC = nextSession;
15400 lastWhite= timeRemaining = whiteTimeRemaining += GetTimeQuota(-1, 0, whiteTC);
15403 DisplayWhiteClock(whiteTimeRemaining - fudge,
15404 WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
15406 if(blackNPS >= 0) lastTickLength = 0;
15407 timeRemaining = blackTimeRemaining -= lastTickLength;
15408 if(timeRemaining < 0 && !appData.icsActive) { // [HGM] if we run out of a non-last incremental session, go to the next
15409 GetTimeQuota((forwardMostMove-blackStartMove-1)/2, 0, blackTC);
15411 blackStartMove = forwardMostMove;
15412 lastBlack = timeRemaining = blackTimeRemaining += GetTimeQuota(-1, 0, blackTC=nextSession);
15415 DisplayBlackClock(blackTimeRemaining - fudge,
15416 !WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
15418 if (CheckFlags()) return;
15421 intendedTickLength = NextTickLength(timeRemaining - fudge) + fudge;
15422 StartClockTimer(intendedTickLength);
15424 /* if the time remaining has fallen below the alarm threshold, sound the
15425 * alarm. if the alarm has sounded and (due to a takeback or time control
15426 * with increment) the time remaining has increased to a level above the
15427 * threshold, reset the alarm so it can sound again.
15430 if (appData.icsActive && appData.icsAlarm) {
15432 /* make sure we are dealing with the user's clock */
15433 if (!( ((gameMode == IcsPlayingWhite) && WhiteOnMove(currentMove)) ||
15434 ((gameMode == IcsPlayingBlack) && !WhiteOnMove(currentMove))
15437 if (alarmSounded && (timeRemaining > appData.icsAlarmTime)) {
15438 alarmSounded = FALSE;
15439 } else if (!alarmSounded && (timeRemaining <= appData.icsAlarmTime)) {
15441 alarmSounded = TRUE;
15447 /* A player has just moved, so stop the previously running
15448 clock and (if in clock mode) start the other one.
15449 We redisplay both clocks in case we're in ICS mode, because
15450 ICS gives us an update to both clocks after every move.
15451 Note that this routine is called *after* forwardMostMove
15452 is updated, so the last fractional tick must be subtracted
15453 from the color that is *not* on move now.
15456 SwitchClocks(int newMoveNr)
15458 long lastTickLength;
15460 int flagged = FALSE;
15464 if (StopClockTimer() && appData.clockMode) {
15465 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
15466 if (!WhiteOnMove(forwardMostMove)) {
15467 if(blackNPS >= 0) lastTickLength = 0;
15468 blackTimeRemaining -= lastTickLength;
15469 /* [HGM] PGNtime: save time for PGN file if engine did not give it */
15470 // if(pvInfoList[forwardMostMove].time == -1)
15471 pvInfoList[forwardMostMove].time = // use GUI time
15472 (timeRemaining[1][forwardMostMove-1] - blackTimeRemaining)/10;
15474 if(whiteNPS >= 0) lastTickLength = 0;
15475 whiteTimeRemaining -= lastTickLength;
15476 /* [HGM] PGNtime: save time for PGN file if engine did not give it */
15477 // if(pvInfoList[forwardMostMove].time == -1)
15478 pvInfoList[forwardMostMove].time =
15479 (timeRemaining[0][forwardMostMove-1] - whiteTimeRemaining)/10;
15481 flagged = CheckFlags();
15483 forwardMostMove = newMoveNr; // [HGM] race: change stm when no timer interrupt scheduled
15484 CheckTimeControl();
15486 if (flagged || !appData.clockMode) return;
15488 switch (gameMode) {
15489 case MachinePlaysBlack:
15490 case MachinePlaysWhite:
15491 case BeginningOfGame:
15492 if (pausing) return;
15496 case PlayFromGameFile:
15504 if (searchTime) { // [HGM] st: set clock of player that has to move to max time
15505 if(WhiteOnMove(forwardMostMove))
15506 whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
15507 else blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
15511 intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
15512 whiteTimeRemaining : blackTimeRemaining);
15513 StartClockTimer(intendedTickLength);
15517 /* Stop both clocks */
15521 long lastTickLength;
15524 if (!StopClockTimer()) return;
15525 if (!appData.clockMode) return;
15529 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
15530 if (WhiteOnMove(forwardMostMove)) {
15531 if(whiteNPS >= 0) lastTickLength = 0;
15532 whiteTimeRemaining -= lastTickLength;
15533 DisplayWhiteClock(whiteTimeRemaining, WhiteOnMove(currentMove));
15535 if(blackNPS >= 0) lastTickLength = 0;
15536 blackTimeRemaining -= lastTickLength;
15537 DisplayBlackClock(blackTimeRemaining, !WhiteOnMove(currentMove));
15542 /* Start clock of player on move. Time may have been reset, so
15543 if clock is already running, stop and restart it. */
15547 (void) StopClockTimer(); /* in case it was running already */
15548 DisplayBothClocks();
15549 if (CheckFlags()) return;
15551 if (!appData.clockMode) return;
15552 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) return;
15554 GetTimeMark(&tickStartTM);
15555 intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
15556 whiteTimeRemaining : blackTimeRemaining);
15558 /* [HGM] nps: figure out nps factors, by determining which engine plays white and/or black once and for all */
15559 whiteNPS = blackNPS = -1;
15560 if(gameMode == MachinePlaysWhite || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w'
15561 || appData.zippyPlay && gameMode == IcsPlayingBlack) // first (perhaps only) engine has white
15562 whiteNPS = first.nps;
15563 if(gameMode == MachinePlaysBlack || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b'
15564 || appData.zippyPlay && gameMode == IcsPlayingWhite) // first (perhaps only) engine has black
15565 blackNPS = first.nps;
15566 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b') // second only used in Two-Machines mode
15567 whiteNPS = second.nps;
15568 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w')
15569 blackNPS = second.nps;
15570 if(appData.debugMode) fprintf(debugFP, "nps: w=%d, b=%d\n", whiteNPS, blackNPS);
15572 StartClockTimer(intendedTickLength);
15579 long second, minute, hour, day;
15581 static char buf[32];
15583 if (ms > 0 && ms <= 9900) {
15584 /* convert milliseconds to tenths, rounding up */
15585 double tenths = floor( ((double)(ms + 99L)) / 100.00 );
15587 snprintf(buf,sizeof(buf)/sizeof(buf[0]), " %03.1f ", tenths/10.0);
15591 /* convert milliseconds to seconds, rounding up */
15592 /* use floating point to avoid strangeness of integer division
15593 with negative dividends on many machines */
15594 second = (long) floor(((double) (ms + 999L)) / 1000.0);
15601 day = second / (60 * 60 * 24);
15602 second = second % (60 * 60 * 24);
15603 hour = second / (60 * 60);
15604 second = second % (60 * 60);
15605 minute = second / 60;
15606 second = second % 60;
15609 snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%ld:%02ld:%02ld:%02ld ",
15610 sign, day, hour, minute, second);
15612 snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%ld:%02ld:%02ld ", sign, hour, minute, second);
15614 snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%2ld:%02ld ", sign, minute, second);
15621 * This is necessary because some C libraries aren't ANSI C compliant yet.
15624 StrStr(string, match)
15625 char *string, *match;
15629 length = strlen(match);
15631 for (i = strlen(string) - length; i >= 0; i--, string++)
15632 if (!strncmp(match, string, length))
15639 StrCaseStr(string, match)
15640 char *string, *match;
15644 length = strlen(match);
15646 for (i = strlen(string) - length; i >= 0; i--, string++) {
15647 for (j = 0; j < length; j++) {
15648 if (ToLower(match[j]) != ToLower(string[j]))
15651 if (j == length) return string;
15665 c1 = ToLower(*s1++);
15666 c2 = ToLower(*s2++);
15667 if (c1 > c2) return 1;
15668 if (c1 < c2) return -1;
15669 if (c1 == NULLCHAR) return 0;
15678 return isupper(c) ? tolower(c) : c;
15686 return islower(c) ? toupper(c) : c;
15688 #endif /* !_amigados */
15696 if ((ret = (char *) malloc(strlen(s) + 1)))
15698 safeStrCpy(ret, s, strlen(s)+1);
15704 StrSavePtr(s, savePtr)
15705 char *s, **savePtr;
15710 if ((*savePtr = (char *) malloc(strlen(s) + 1))) {
15711 safeStrCpy(*savePtr, s, strlen(s)+1);
15723 clock = time((time_t *)NULL);
15724 tm = localtime(&clock);
15725 snprintf(buf, MSG_SIZ, "%04d.%02d.%02d",
15726 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
15727 return StrSave(buf);
15732 PositionToFEN(move, overrideCastling)
15734 char *overrideCastling;
15736 int i, j, fromX, fromY, toX, toY;
15743 whiteToPlay = (gameMode == EditPosition) ?
15744 !blackPlaysFirst : (move % 2 == 0);
15747 /* Piece placement data */
15748 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
15750 for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
15751 if (boards[move][i][j] == EmptySquare) {
15753 } else { ChessSquare piece = boards[move][i][j];
15754 if (emptycount > 0) {
15755 if(emptycount<10) /* [HGM] can be >= 10 */
15756 *p++ = '0' + emptycount;
15757 else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
15760 if(PieceToChar(piece) == '+') {
15761 /* [HGM] write promoted pieces as '+<unpromoted>' (Shogi) */
15763 piece = (ChessSquare)(DEMOTED piece);
15765 *p++ = PieceToChar(piece);
15767 /* [HGM] flag promoted pieces as '<promoted>~' (Crazyhouse) */
15768 p[-1] = PieceToChar((ChessSquare)(DEMOTED piece));
15773 if (emptycount > 0) {
15774 if(emptycount<10) /* [HGM] can be >= 10 */
15775 *p++ = '0' + emptycount;
15776 else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
15783 /* [HGM] print Crazyhouse or Shogi holdings */
15784 if( gameInfo.holdingsWidth ) {
15785 *(p-1) = '['; /* if we wanted to support BFEN, this could be '/' */
15787 for(i=0; i<gameInfo.holdingsSize; i++) { /* white holdings */
15788 piece = boards[move][i][BOARD_WIDTH-1];
15789 if( piece != EmptySquare )
15790 for(j=0; j<(int) boards[move][i][BOARD_WIDTH-2]; j++)
15791 *p++ = PieceToChar(piece);
15793 for(i=0; i<gameInfo.holdingsSize; i++) { /* black holdings */
15794 piece = boards[move][BOARD_HEIGHT-i-1][0];
15795 if( piece != EmptySquare )
15796 for(j=0; j<(int) boards[move][BOARD_HEIGHT-i-1][1]; j++)
15797 *p++ = PieceToChar(piece);
15800 if( q == p ) *p++ = '-';
15806 *p++ = whiteToPlay ? 'w' : 'b';
15809 if(q = overrideCastling) { // [HGM] FRC: override castling & e.p fields for non-compliant engines
15810 while(*p++ = *q++); if(q != overrideCastling+1) p[-1] = ' '; else --p;
15812 if(nrCastlingRights) {
15814 if(gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) {
15815 /* [HGM] write directly from rights */
15816 if(boards[move][CASTLING][2] != NoRights &&
15817 boards[move][CASTLING][0] != NoRights )
15818 *p++ = boards[move][CASTLING][0] + AAA + 'A' - 'a';
15819 if(boards[move][CASTLING][2] != NoRights &&
15820 boards[move][CASTLING][1] != NoRights )
15821 *p++ = boards[move][CASTLING][1] + AAA + 'A' - 'a';
15822 if(boards[move][CASTLING][5] != NoRights &&
15823 boards[move][CASTLING][3] != NoRights )
15824 *p++ = boards[move][CASTLING][3] + AAA;
15825 if(boards[move][CASTLING][5] != NoRights &&
15826 boards[move][CASTLING][4] != NoRights )
15827 *p++ = boards[move][CASTLING][4] + AAA;
15830 /* [HGM] write true castling rights */
15831 if( nrCastlingRights == 6 ) {
15832 if(boards[move][CASTLING][0] == BOARD_RGHT-1 &&
15833 boards[move][CASTLING][2] != NoRights ) *p++ = 'K';
15834 if(boards[move][CASTLING][1] == BOARD_LEFT &&
15835 boards[move][CASTLING][2] != NoRights ) *p++ = 'Q';
15836 if(boards[move][CASTLING][3] == BOARD_RGHT-1 &&
15837 boards[move][CASTLING][5] != NoRights ) *p++ = 'k';
15838 if(boards[move][CASTLING][4] == BOARD_LEFT &&
15839 boards[move][CASTLING][5] != NoRights ) *p++ = 'q';
15842 if (q == p) *p++ = '-'; /* No castling rights */
15846 if(gameInfo.variant != VariantShogi && gameInfo.variant != VariantXiangqi &&
15847 gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier && gameInfo.variant != VariantMakruk ) {
15848 /* En passant target square */
15849 if (move > backwardMostMove) {
15850 fromX = moveList[move - 1][0] - AAA;
15851 fromY = moveList[move - 1][1] - ONE;
15852 toX = moveList[move - 1][2] - AAA;
15853 toY = moveList[move - 1][3] - ONE;
15854 if (fromY == (whiteToPlay ? BOARD_HEIGHT-2 : 1) &&
15855 toY == (whiteToPlay ? BOARD_HEIGHT-4 : 3) &&
15856 boards[move][toY][toX] == (whiteToPlay ? BlackPawn : WhitePawn) &&
15858 /* 2-square pawn move just happened */
15860 *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
15864 } else if(move == backwardMostMove) {
15865 // [HGM] perhaps we should always do it like this, and forget the above?
15866 if((signed char)boards[move][EP_STATUS] >= 0) {
15867 *p++ = boards[move][EP_STATUS] + AAA;
15868 *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
15879 /* [HGM] find reversible plies */
15880 { int i = 0, j=move;
15882 if (appData.debugMode) { int k;
15883 fprintf(debugFP, "write FEN 50-move: %d %d %d\n", initialRulePlies, forwardMostMove, backwardMostMove);
15884 for(k=backwardMostMove; k<=forwardMostMove; k++)
15885 fprintf(debugFP, "e%d. p=%d\n", k, (signed char)boards[k][EP_STATUS]);
15889 while(j > backwardMostMove && (signed char)boards[j][EP_STATUS] <= EP_NONE) j--,i++;
15890 if( j == backwardMostMove ) i += initialRulePlies;
15891 sprintf(p, "%d ", i);
15892 p += i>=100 ? 4 : i >= 10 ? 3 : 2;
15894 /* Fullmove number */
15895 sprintf(p, "%d", (move / 2) + 1);
15897 return StrSave(buf);
15901 ParseFEN(board, blackPlaysFirst, fen)
15903 int *blackPlaysFirst;
15913 /* [HGM] by default clear Crazyhouse holdings, if present */
15914 if(gameInfo.holdingsWidth) {
15915 for(i=0; i<BOARD_HEIGHT; i++) {
15916 board[i][0] = EmptySquare; /* black holdings */
15917 board[i][BOARD_WIDTH-1] = EmptySquare; /* white holdings */
15918 board[i][1] = (ChessSquare) 0; /* black counts */
15919 board[i][BOARD_WIDTH-2] = (ChessSquare) 0; /* white counts */
15923 /* Piece placement data */
15924 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
15927 if (*p == '/' || *p == ' ' || (*p == '[' && i == 0) ) {
15928 if (*p == '/') p++;
15929 emptycount = gameInfo.boardWidth - j;
15930 while (emptycount--)
15931 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
15933 #if(BOARD_FILES >= 10)
15934 } else if(*p=='x' || *p=='X') { /* [HGM] X means 10 */
15935 p++; emptycount=10;
15936 if (j + emptycount > gameInfo.boardWidth) return FALSE;
15937 while (emptycount--)
15938 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
15940 } else if (isdigit(*p)) {
15941 emptycount = *p++ - '0';
15942 while(isdigit(*p)) emptycount = 10*emptycount + *p++ - '0'; /* [HGM] allow > 9 */
15943 if (j + emptycount > gameInfo.boardWidth) return FALSE;
15944 while (emptycount--)
15945 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
15946 } else if (*p == '+' || isalpha(*p)) {
15947 if (j >= gameInfo.boardWidth) return FALSE;
15949 piece = CharToPiece(*++p);
15950 if(piece == EmptySquare) return FALSE; /* unknown piece */
15951 piece = (ChessSquare) (PROMOTED piece ); p++;
15952 if(PieceToChar(piece) != '+') return FALSE; /* unpromotable piece */
15953 } else piece = CharToPiece(*p++);
15955 if(piece==EmptySquare) return FALSE; /* unknown piece */
15956 if(*p == '~') { /* [HGM] make it a promoted piece for Crazyhouse */
15957 piece = (ChessSquare) (PROMOTED piece);
15958 if(PieceToChar(piece) != '~') return FALSE; /* cannot be a promoted piece */
15961 board[i][(j++)+gameInfo.holdingsWidth] = piece;
15967 while (*p == '/' || *p == ' ') p++;
15969 /* [HGM] look for Crazyhouse holdings here */
15970 while(*p==' ') p++;
15971 if( gameInfo.holdingsWidth && p[-1] == '/' || *p == '[') {
15973 if(*p == '-' ) p++; /* empty holdings */ else {
15974 if( !gameInfo.holdingsWidth ) return FALSE; /* no room to put holdings! */
15975 /* if we would allow FEN reading to set board size, we would */
15976 /* have to add holdings and shift the board read so far here */
15977 while( (piece = CharToPiece(*p) ) != EmptySquare ) {
15979 if((int) piece >= (int) BlackPawn ) {
15980 i = (int)piece - (int)BlackPawn;
15981 i = PieceToNumber((ChessSquare)i);
15982 if( i >= gameInfo.holdingsSize ) return FALSE;
15983 board[BOARD_HEIGHT-1-i][0] = piece; /* black holdings */
15984 board[BOARD_HEIGHT-1-i][1]++; /* black counts */
15986 i = (int)piece - (int)WhitePawn;
15987 i = PieceToNumber((ChessSquare)i);
15988 if( i >= gameInfo.holdingsSize ) return FALSE;
15989 board[i][BOARD_WIDTH-1] = piece; /* white holdings */
15990 board[i][BOARD_WIDTH-2]++; /* black holdings */
15997 while(*p == ' ') p++;
16001 if(appData.colorNickNames) {
16002 if( c == appData.colorNickNames[0] ) c = 'w'; else
16003 if( c == appData.colorNickNames[1] ) c = 'b';
16007 *blackPlaysFirst = FALSE;
16010 *blackPlaysFirst = TRUE;
16016 /* [HGM] We NO LONGER ignore the rest of the FEN notation */
16017 /* return the extra info in global variiables */
16019 /* set defaults in case FEN is incomplete */
16020 board[EP_STATUS] = EP_UNKNOWN;
16021 for(i=0; i<nrCastlingRights; i++ ) {
16022 board[CASTLING][i] =
16023 gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom ? NoRights : initialRights[i];
16024 } /* assume possible unless obviously impossible */
16025 if(initialRights[0]!=NoRights && board[castlingRank[0]][initialRights[0]] != WhiteRook) board[CASTLING][0] = NoRights;
16026 if(initialRights[1]!=NoRights && board[castlingRank[1]][initialRights[1]] != WhiteRook) board[CASTLING][1] = NoRights;
16027 if(initialRights[2]!=NoRights && board[castlingRank[2]][initialRights[2]] != WhiteUnicorn
16028 && board[castlingRank[2]][initialRights[2]] != WhiteKing) board[CASTLING][2] = NoRights;
16029 if(initialRights[3]!=NoRights && board[castlingRank[3]][initialRights[3]] != BlackRook) board[CASTLING][3] = NoRights;
16030 if(initialRights[4]!=NoRights && board[castlingRank[4]][initialRights[4]] != BlackRook) board[CASTLING][4] = NoRights;
16031 if(initialRights[5]!=NoRights && board[castlingRank[5]][initialRights[5]] != BlackUnicorn
16032 && board[castlingRank[5]][initialRights[5]] != BlackKing) board[CASTLING][5] = NoRights;
16035 while(*p==' ') p++;
16036 if(nrCastlingRights) {
16037 if(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-') {
16038 /* castling indicator present, so default becomes no castlings */
16039 for(i=0; i<nrCastlingRights; i++ ) {
16040 board[CASTLING][i] = NoRights;
16043 while(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-' ||
16044 (gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) &&
16045 ( *p >= 'a' && *p < 'a' + gameInfo.boardWidth) ||
16046 ( *p >= 'A' && *p < 'A' + gameInfo.boardWidth) ) {
16047 char c = *p++; int whiteKingFile=NoRights, blackKingFile=NoRights;
16049 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
16050 if(board[BOARD_HEIGHT-1][i] == BlackKing) blackKingFile = i;
16051 if(board[0 ][i] == WhiteKing) whiteKingFile = i;
16053 if(gameInfo.variant == VariantTwoKings || gameInfo.variant == VariantKnightmate)
16054 whiteKingFile = blackKingFile = BOARD_WIDTH >> 1; // for these variant scanning fails
16055 if(whiteKingFile == NoRights || board[0][whiteKingFile] != WhiteUnicorn
16056 && board[0][whiteKingFile] != WhiteKing) whiteKingFile = NoRights;
16057 if(blackKingFile == NoRights || board[BOARD_HEIGHT-1][blackKingFile] != BlackUnicorn
16058 && board[BOARD_HEIGHT-1][blackKingFile] != BlackKing) blackKingFile = NoRights;
16061 for(i=BOARD_RGHT-1; board[0][i]!=WhiteRook && i>whiteKingFile; i--);
16062 board[CASTLING][0] = i != whiteKingFile ? i : NoRights;
16063 board[CASTLING][2] = whiteKingFile;
16066 for(i=BOARD_LEFT; i<BOARD_RGHT && board[0][i]!=WhiteRook && i<whiteKingFile; i++);
16067 board[CASTLING][1] = i != whiteKingFile ? i : NoRights;
16068 board[CASTLING][2] = whiteKingFile;
16071 for(i=BOARD_RGHT-1; board[BOARD_HEIGHT-1][i]!=BlackRook && i>blackKingFile; i--);
16072 board[CASTLING][3] = i != blackKingFile ? i : NoRights;
16073 board[CASTLING][5] = blackKingFile;
16076 for(i=BOARD_LEFT; i<BOARD_RGHT && board[BOARD_HEIGHT-1][i]!=BlackRook && i<blackKingFile; i++);
16077 board[CASTLING][4] = i != blackKingFile ? i : NoRights;
16078 board[CASTLING][5] = blackKingFile;
16081 default: /* FRC castlings */
16082 if(c >= 'a') { /* black rights */
16083 for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
16084 if(board[BOARD_HEIGHT-1][i] == BlackKing) break;
16085 if(i == BOARD_RGHT) break;
16086 board[CASTLING][5] = i;
16088 if(board[BOARD_HEIGHT-1][c] < BlackPawn ||
16089 board[BOARD_HEIGHT-1][c] >= BlackKing ) break;
16091 board[CASTLING][3] = c;
16093 board[CASTLING][4] = c;
16094 } else { /* white rights */
16095 for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
16096 if(board[0][i] == WhiteKing) break;
16097 if(i == BOARD_RGHT) break;
16098 board[CASTLING][2] = i;
16099 c -= AAA - 'a' + 'A';
16100 if(board[0][c] >= WhiteKing) break;
16102 board[CASTLING][0] = c;
16104 board[CASTLING][1] = c;
16108 for(i=0; i<nrCastlingRights; i++)
16109 if(board[CASTLING][i] != NoRights) initialRights[i] = board[CASTLING][i];
16110 if (appData.debugMode) {
16111 fprintf(debugFP, "FEN castling rights:");
16112 for(i=0; i<nrCastlingRights; i++)
16113 fprintf(debugFP, " %d", board[CASTLING][i]);
16114 fprintf(debugFP, "\n");
16117 while(*p==' ') p++;
16120 /* read e.p. field in games that know e.p. capture */
16121 if(gameInfo.variant != VariantShogi && gameInfo.variant != VariantXiangqi &&
16122 gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier && gameInfo.variant != VariantMakruk ) {
16124 p++; board[EP_STATUS] = EP_NONE;
16126 char c = *p++ - AAA;
16128 if(c < BOARD_LEFT || c >= BOARD_RGHT) return TRUE;
16129 if(*p >= '0' && *p <='9') p++;
16130 board[EP_STATUS] = c;
16135 if(sscanf(p, "%d", &i) == 1) {
16136 FENrulePlies = i; /* 50-move ply counter */
16137 /* (The move number is still ignored) */
16144 EditPositionPasteFEN(char *fen)
16147 Board initial_position;
16149 if (!ParseFEN(initial_position, &blackPlaysFirst, fen)) {
16150 DisplayError(_("Bad FEN position in clipboard"), 0);
16153 int savedBlackPlaysFirst = blackPlaysFirst;
16154 EditPositionEvent();
16155 blackPlaysFirst = savedBlackPlaysFirst;
16156 CopyBoard(boards[0], initial_position);
16157 initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
16158 EditPositionDone(FALSE); // [HGM] fake: do not fake rights if we had FEN
16159 DisplayBothClocks();
16160 DrawPosition(FALSE, boards[currentMove]);
16165 static char cseq[12] = "\\ ";
16167 Boolean set_cont_sequence(char *new_seq)
16172 // handle bad attempts to set the sequence
16174 return 0; // acceptable error - no debug
16176 len = strlen(new_seq);
16177 ret = (len > 0) && (len < sizeof(cseq));
16179 safeStrCpy(cseq, new_seq, sizeof(cseq)/sizeof(cseq[0]));
16180 else if (appData.debugMode)
16181 fprintf(debugFP, "Invalid continuation sequence \"%s\" (maximum length is: %u)\n", new_seq, (unsigned) sizeof(cseq)-1);
16186 reformat a source message so words don't cross the width boundary. internal
16187 newlines are not removed. returns the wrapped size (no null character unless
16188 included in source message). If dest is NULL, only calculate the size required
16189 for the dest buffer. lp argument indicats line position upon entry, and it's
16190 passed back upon exit.
16192 int wrap(char *dest, char *src, int count, int width, int *lp)
16194 int len, i, ansi, cseq_len, line, old_line, old_i, old_len, clen;
16196 cseq_len = strlen(cseq);
16197 old_line = line = *lp;
16198 ansi = len = clen = 0;
16200 for (i=0; i < count; i++)
16202 if (src[i] == '\033')
16205 // if we hit the width, back up
16206 if (!ansi && (line >= width) && src[i] != '\n' && src[i] != ' ')
16208 // store i & len in case the word is too long
16209 old_i = i, old_len = len;
16211 // find the end of the last word
16212 while (i && src[i] != ' ' && src[i] != '\n')
16218 // word too long? restore i & len before splitting it
16219 if ((old_i-i+clen) >= width)
16226 if (i && src[i-1] == ' ')
16229 if (src[i] != ' ' && src[i] != '\n')
16236 // now append the newline and continuation sequence
16241 strncpy(dest+len, cseq, cseq_len);
16249 dest[len] = src[i];
16253 if (src[i] == '\n')
16258 if (dest && appData.debugMode)
16260 fprintf(debugFP, "wrap(count:%d,width:%d,line:%d,len:%d,*lp:%d,src: ",
16261 count, width, line, len, *lp);
16262 show_bytes(debugFP, src, count);
16263 fprintf(debugFP, "\ndest: ");
16264 show_bytes(debugFP, dest, len);
16265 fprintf(debugFP, "\n");
16267 *lp = dest ? line : old_line;
16272 // [HGM] vari: routines for shelving variations
16275 PushInner(int firstMove, int lastMove)
16277 int i, j, nrMoves = lastMove - firstMove;
16279 // push current tail of game on stack
16280 savedResult[storedGames] = gameInfo.result;
16281 savedDetails[storedGames] = gameInfo.resultDetails;
16282 gameInfo.resultDetails = NULL;
16283 savedFirst[storedGames] = firstMove;
16284 savedLast [storedGames] = lastMove;
16285 savedFramePtr[storedGames] = framePtr;
16286 framePtr -= nrMoves; // reserve space for the boards
16287 for(i=nrMoves; i>=1; i--) { // copy boards to stack, working downwards, in case of overlap
16288 CopyBoard(boards[framePtr+i], boards[firstMove+i]);
16289 for(j=0; j<MOVE_LEN; j++)
16290 moveList[framePtr+i][j] = moveList[firstMove+i-1][j];
16291 for(j=0; j<2*MOVE_LEN; j++)
16292 parseList[framePtr+i][j] = parseList[firstMove+i-1][j];
16293 timeRemaining[0][framePtr+i] = timeRemaining[0][firstMove+i];
16294 timeRemaining[1][framePtr+i] = timeRemaining[1][firstMove+i];
16295 pvInfoList[framePtr+i] = pvInfoList[firstMove+i-1];
16296 pvInfoList[firstMove+i-1].depth = 0;
16297 commentList[framePtr+i] = commentList[firstMove+i];
16298 commentList[firstMove+i] = NULL;
16302 forwardMostMove = firstMove; // truncate game so we can start variation
16306 PushTail(int firstMove, int lastMove)
16308 if(appData.icsActive) { // only in local mode
16309 forwardMostMove = currentMove; // mimic old ICS behavior
16312 if(storedGames >= MAX_VARIATIONS-2) return; // leave one for PV-walk
16314 PushInner(firstMove, lastMove);
16315 if(storedGames == 1) GreyRevert(FALSE);
16319 PopInner(Boolean annotate)
16322 char buf[8000], moveBuf[20];
16325 ToNrEvent(savedFirst[storedGames]); // sets currentMove
16326 nrMoves = savedLast[storedGames] - currentMove;
16329 if(!WhiteOnMove(currentMove))
16330 snprintf(buf, sizeof(buf)/sizeof(buf[0]),"(%d...", (currentMove+2)>>1);
16331 else safeStrCpy(buf, "(", sizeof(buf)/sizeof(buf[0]));
16332 for(i=currentMove; i<forwardMostMove; i++) {
16334 snprintf(moveBuf, sizeof(moveBuf)/sizeof(moveBuf[0]), " %d. %s", (i+2)>>1, SavePart(parseList[i]));
16335 else snprintf(moveBuf, sizeof(moveBuf)/sizeof(moveBuf[0])," %s", SavePart(parseList[i]));
16336 strcat(buf, moveBuf);
16337 if(commentList[i]) { strcat(buf, " "); strcat(buf, commentList[i]); }
16338 if(!--cnt) { strcat(buf, "\n"); cnt = 10; }
16342 for(i=1; i<=nrMoves; i++) { // copy last variation back
16343 CopyBoard(boards[currentMove+i], boards[framePtr+i]);
16344 for(j=0; j<MOVE_LEN; j++)
16345 moveList[currentMove+i-1][j] = moveList[framePtr+i][j];
16346 for(j=0; j<2*MOVE_LEN; j++)
16347 parseList[currentMove+i-1][j] = parseList[framePtr+i][j];
16348 timeRemaining[0][currentMove+i] = timeRemaining[0][framePtr+i];
16349 timeRemaining[1][currentMove+i] = timeRemaining[1][framePtr+i];
16350 pvInfoList[currentMove+i-1] = pvInfoList[framePtr+i];
16351 if(commentList[currentMove+i]) free(commentList[currentMove+i]);
16352 commentList[currentMove+i] = commentList[framePtr+i];
16353 commentList[framePtr+i] = NULL;
16355 if(annotate) AppendComment(currentMove+1, buf, FALSE);
16356 framePtr = savedFramePtr[storedGames];
16357 gameInfo.result = savedResult[storedGames];
16358 if(gameInfo.resultDetails != NULL) {
16359 free(gameInfo.resultDetails);
16361 gameInfo.resultDetails = savedDetails[storedGames];
16362 forwardMostMove = currentMove + nrMoves;
16366 PopTail(Boolean annotate)
16368 if(appData.icsActive) return FALSE; // only in local mode
16369 if(!storedGames) return FALSE; // sanity
16370 CommentPopDown(); // make sure no stale variation comments to the destroyed line can remain open
16372 PopInner(annotate);
16374 if(storedGames == 0) GreyRevert(TRUE);
16380 { // remove all shelved variations
16382 for(i=0; i<storedGames; i++) {
16383 if(savedDetails[i])
16384 free(savedDetails[i]);
16385 savedDetails[i] = NULL;
16387 for(i=framePtr; i<MAX_MOVES; i++) {
16388 if(commentList[i]) free(commentList[i]);
16389 commentList[i] = NULL;
16391 framePtr = MAX_MOVES-1;
16396 LoadVariation(int index, char *text)
16397 { // [HGM] vari: shelve previous line and load new variation, parsed from text around text[index]
16398 char *p = text, *start = NULL, *end = NULL, wait = NULLCHAR;
16399 int level = 0, move;
16401 if(gameMode != EditGame && gameMode != AnalyzeMode) return;
16402 // first find outermost bracketing variation
16403 while(*p) { // hope I got this right... Non-nesting {} and [] can screen each other and nesting ()
16404 if(!wait) { // while inside [] pr {}, ignore everyting except matching closing ]}
16405 if(*p == '{') wait = '}'; else
16406 if(*p == '[') wait = ']'; else
16407 if(*p == '(' && level++ == 0 && p-text < index) start = p+1;
16408 if(*p == ')' && level > 0 && --level == 0 && p-text > index && end == NULL) end = p-1;
16410 if(*p == wait) wait = NULLCHAR; // closing ]} found
16413 if(!start || !end) return; // no variation found, or syntax error in PGN: ignore click
16414 if(appData.debugMode) fprintf(debugFP, "at move %d load variation '%s'\n", currentMove, start);
16415 end[1] = NULLCHAR; // clip off comment beyond variation
16416 ToNrEvent(currentMove-1);
16417 PushTail(currentMove, forwardMostMove); // shelve main variation. This truncates game
16418 // kludge: use ParsePV() to append variation to game
16419 move = currentMove;
16420 ParsePV(start, TRUE, TRUE);
16421 forwardMostMove = endPV; endPV = -1; currentMove = move; // cleanup what ParsePV did
16422 ClearPremoveHighlights();
16424 ToNrEvent(currentMove+1);