* Massachusetts.
*
* Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
- * 2007, 2008, 2009 Free Software Foundation, Inc.
+ * 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Free Software Foundation, Inc.
*
* Enhancements Copyright 2005 Alessandro Scotti
*
#ifdef WIN32
#include <windows.h>
-#define DoSleep( n ) if( (n) != 0 ) Sleep( (n) );
+ int flock(int f, int code);
+# define LOCK_EX 2
+# define SLASH '\\'
+
+# ifdef ARC_64BIT
+# define EGBB_NAME "egbbdll64.dll"
+# else
+# define EGBB_NAME "egbbdll.dll"
+# endif
#else
-#define DoSleep( n ) if( (n) >= 0) sleep(n)
+# include <sys/file.h>
+# define SLASH '/'
+
+# include <dlfcn.h>
+# ifdef ARC_64BIT
+# define EGBB_NAME "egbbso64.so"
+# else
+# define EGBB_NAME "egbbso.so"
+# endif
+ // kludge to allow Windows code in back-end by converting it to corresponding Linux code
+# define CDECL
+# define HMODULE void *
+# define LoadLibrary(x) dlopen(x, RTLD_LAZY)
+# define GetProcAddress dlsym
#endif
#if STDC_HEADERS
# include <stdlib.h>
# include <string.h>
+# include <stdarg.h>
#else /* not STDC_HEADERS */
# if HAVE_STRING_H
# include <string.h>
# include "zippy.h"
#endif
#include "backendz.h"
-#include "gettext.h"
-
-#ifdef ENABLE_NLS
-# define _(s) gettext (s)
-# define N_(s) gettext_noop (s)
-#else
-# define _(s) (s)
-# define N_(s) s
-#endif
-
-
-/* A point in time */
-typedef struct {
- long sec; /* Assuming this is >= 32 bits */
- int ms; /* Assuming this is >= 16 bits */
-} TimeMark;
+#include "evalgraph.h"
+#include "engineoutput.h"
+#include "gettext.h"
+
+#ifdef ENABLE_NLS
+# define _(s) gettext (s)
+# define N_(s) gettext_noop (s)
+# define T_(s) gettext(s)
+#else
+# ifdef WIN32
+# define _(s) T_(s)
+# define N_(s) s
+# else
+# define _(s) (s)
+# define N_(s) s
+# define T_(s) s
+# endif
+#endif
+
int establish P((void));
void read_from_player P((InputSourceRef isr, VOIDSTAR closure,
char *buf, int count, int error));
void read_from_ics P((InputSourceRef isr, VOIDSTAR closure,
char *buf, int count, int error));
-void ics_printf P((char *format, ...));
void SendToICS P((char *s));
void SendToICSDelayed P((char *s, long msdelay));
-void SendMoveToICS P((ChessMove moveType, int fromX, int fromY,
- int toX, int toY));
+void SendMoveToICS P((ChessMove moveType, int fromX, int fromY, int toX, int toY, char promoChar));
void HandleMachineMove P((char *message, ChessProgramState *cps));
int AutoPlayOneMove P((void));
int LoadGameOneMove P((ChessMove readAhead));
int LoadGameFromFile P((char *filename, int n, char *title, int useList));
int LoadPositionFromFile P((char *filename, int n, char *title));
int SavePositionToFile P((char *filename));
-void ApplyMove P((int fromX, int fromY, int toX, int toY, int promoChar,
- Board board, char *castle, char *ep));
void MakeMove P((int fromX, int fromY, int toX, int toY, int promoChar));
void ShowMove P((int fromX, int fromY, int toX, int toY));
int FinishMove P((ChessMove moveType, int fromX, int fromY, int toX, int toY,
/*char*/int promoChar));
void BackwardInner P((int target));
void ForwardInner P((int target));
+int Adjudicate P((ChessProgramState *cps));
void GameEnds P((ChessMove result, char *resultDetails, int whosays));
-void EditPositionDone P((void));
+void EditPositionDone P((Boolean fakeRights));
void PrintOpponents P((FILE *fp));
void PrintPosition P((FILE *fp, int move));
void StartChessProgram P((ChessProgramState *cps));
char *TimeControlTagValue P((void));
void Attention P((ChessProgramState *cps));
void FeedMovesToProgram P((ChessProgramState *cps, int upto));
-void ResurrectChessProgram P((void));
+int ResurrectChessProgram P((void));
void DisplayComment P((int moveNumber, char *text));
void DisplayMove P((int moveNumber));
-void DisplayAnalysis P((void));
void ParseGameHistory P((char *game));
void ParseBoard12 P((char *string));
+void KeepAlive P((void));
void StartClocks P((void));
-void SwitchClocks P((void));
+void SwitchClocks P((int nr));
void StopClocks P((void));
void ResetClocks P((void));
char *PGNDate P((void));
void SetGameInfo P((void));
-Boolean ParseFEN P((Board board, int *blackPlaysFirst, char *fen));
int RegisterMove P((void));
void MakeRegisteredMove P((void));
void TruncateGame P((void));
char *SavePart P((char *));
int SaveGameOldStyle P((FILE *));
int SaveGamePGN P((FILE *));
-void GetTimeMark P((TimeMark *));
-long SubtractTimeMarks P((TimeMark *, TimeMark *));
int CheckFlags P((void));
long NextTickLength P((long));
void CheckTimeControl P((void));
int PerpetualChase(int first, int last);
int EngineOutputIsUp();
void InitDrawingSizes(int x, int y);
+void NextMatchGame P((void));
+int NextTourneyGame P((int nr, int *swap));
+int Pairing P((int nr, int nPlayers, int *w, int *b, int *sync));
+FILE *WriteTourneyFile P((char *results, FILE *f));
+void DisplayTwoMachinesTitle P(());
+static void ExcludeClick P((int index));
+void ToggleSecond P((void));
+void PauseEngine P((ChessProgramState *cps));
+static int NonStandardBoardSize P((VariantClass v, int w, int h, int s));
#ifdef WIN32
extern void ConsoleCreate();
#endif
ChessProgramState *WhitePlayer();
-void InsertIntoMemo P((int which, char *text)); // [HGM] kibitz: in engineo.c
int VerifyDisplayMode P(());
char *GetInfoFromComment( int, char * ); // [HGM] PV time: returns stripped comment
char *SendMoveToBookUser P((int nr, ChessProgramState *cps, int initial)); // [HGM] book
void ics_update_width P((int new_width));
extern char installDir[MSG_SIZ];
+VariantClass startVariant; /* [HGM] nicks: initial variant */
+Boolean abortMatch;
extern int tinyLayout, smallLayout;
ChessProgramStats programStats;
+char lastPV[2][2*MSG_SIZ]; /* [HGM] pv: last PV in thinking output of each engine */
+int endPV = -1;
static int exiting = 0; /* [HGM] moved to top */
static int setboardSpoiledMachineBlack = 0 /*, errorExitFlag = 0*/;
int startedFromPositionFile = FALSE; Board filePosition; /* [HGM] loadPos */
+Board partnerBoard; /* [HGM] bughouse: for peeking at partner game */
+int partnerHighlight[2];
+Boolean partnerBoardValid = 0;
+char partnerStatus[MSG_SIZ];
+Boolean partnerUp;
+Boolean originalFlip;
+Boolean twoBoards = 0;
char endingGame = 0; /* [HGM] crash: flag to prevent recursion of GameEnds() */
int whiteNPS, blackNPS; /* [HGM] nps: for easily making clocks aware of NPS */
VariantClass currentlyInitializedVariant; /* [HGM] variantswitch */
int lastIndex = 0; /* [HGM] autoinc: last game/position used in match mode */
+Boolean connectionAlive;/* [HGM] alive: ICS connection status from probing */
int opponentKibitzes;
int lastSavedGame; /* [HGM] save: ID of game */
char chatPartner[MAX_CHAT][MSG_SIZ]; /* [HGM] chat: list of chatting partners */
extern int chatCount;
int chattingPartner;
+char marker[BOARD_RANKS][BOARD_FILES]; /* [HGM] marks for target squares */
+char legal[BOARD_RANKS][BOARD_FILES]; /* [HGM] legal target squares */
+char lastMsg[MSG_SIZ];
+char lastTalker[MSG_SIZ];
+ChessSquare pieceSweep = EmptySquare;
+ChessSquare promoSweep = EmptySquare, defaultPromoChoice;
+int promoDefaultAltered;
+int keepInfo = 0; /* [HGM] to protect PGN tags in auto-step game analysis */
+static int initPing = -1;
+int border; /* [HGM] width of board rim, needed to size seek graph */
+char bestMove[MSG_SIZ];
+int solvingTime, totalTime;
/* States for ics_getting_history */
#define H_FALSE 0
#define TN_SGA 0003
#define TN_PORT 23
-/* [AS] */
-static char * safeStrCpy( char * dst, const char * src, size_t count )
-{
- assert( dst != NULL );
- assert( src != NULL );
- assert( count > 0 );
+char*
+safeStrCpy (char *dst, const char *src, size_t count)
+{ // [HGM] made safe
+ int i;
+ assert( dst != NULL );
+ assert( src != NULL );
+ assert( count > 0 );
+
+ for(i=0; i<count; i++) if((dst[i] = src[i]) == NULLCHAR) break;
+ if( i == count && dst[count-1] != NULLCHAR)
+ {
+ dst[ count-1 ] = '\0'; // make sure incomplete copy still null-terminated
+ if(appData.debugMode)
+ fprintf(debugFP, "safeStrCpy: copying %s into %s didn't work, not enough space %d\n",src,dst, (int)count);
+ }
- strncpy( dst, src, count );
- dst[ count-1 ] = '\0';
- return dst;
+ return dst;
}
/* Some compiler can't cast u64 to double
* We used this for all compiler
*/
double
-u64ToDouble(u64 value)
+u64ToDouble (u64 value)
{
double r;
u64 tmp = value & u64Const(0x7fffffffffffffff);
by this function.
*/
int
-PosFlags(index)
+PosFlags (index)
{
int flags = F_ALL_CASTLE_OK;
if ((index % 2) == 0) flags |= F_WHITE_ON_MOVE;
case VariantKriegspiel:
flags |= F_KRIEGSPIEL_CAPTURE;
break;
- case VariantCapaRandom:
+ case VariantCapaRandom:
case VariantFischeRandom:
flags |= F_FRC_TYPE_CASTLING; /* [HGM] enable this through flag */
case VariantNoCastle:
case VariantShatranj:
case VariantCourier:
+ case VariantMakruk:
+ case VariantASEAN:
+ case VariantGrand:
flags &= ~F_ALL_CASTLE_OK;
break;
+ case VariantChu:
+ case VariantChuChess:
+ case VariantLion:
+ flags |= F_NULL_MOVE;
+ break;
default:
break;
}
+ if(appData.fischerCastling) flags |= F_FRC_TYPE_CASTLING, flags &= ~F_ALL_CASTLE_OK; // [HGM] fischer
return flags;
}
-FILE *gameFileFP, *debugFP;
+FILE *gameFileFP, *debugFP, *serverFP;
+char *currentDebugFile; // [HGM] debug split: to remember name
-/*
+/*
[AS] Note: sometimes, the sscanf() function is used to parse the input
into a fixed-size buffer. Because of this, we must be prepared to
receive strings as long as the size of the input buffer, which is currently
char cmailMove[CMAIL_MAX_GAMES][MOVE_LEN], cmailMsg[MSG_SIZ];
char bookOutput[MSG_SIZ*10], thinkOutput[MSG_SIZ*10], lastHint[MSG_SIZ];
char thinkOutput1[MSG_SIZ*10];
+char promoRestrict[MSG_SIZ];
-ChessProgramState first, second;
+ChessProgramState first, second, pairing;
/* premove variables */
int premoveToX = 0;
/* end premove variables */
char *ics_prefix = "$";
-int ics_type = ICS_GENERIC;
+enum ICS_TYPE ics_type = ICS_GENERIC;
int currentMove = 0, forwardMostMove = 0, backwardMostMove = 0;
int pauseExamForwardMostMove = 0;
char white_holding[64], black_holding[64];
TimeMark lastNodeCountTime;
long lastNodeCount=0;
+int shiftKey, controlKey; // [HGM] set by mouse handler
+
int have_sent_ICS_logon = 0;
int movesPerSession;
-long whiteTimeRemaining, blackTimeRemaining, timeControl, timeIncrement;
+int suddenDeath, whiteStartMove, blackStartMove; /* [HGM] for implementation of 'any per time' sessions, as in first part of byoyomi TC */
+long whiteTimeRemaining, blackTimeRemaining, timeControl, timeIncrement, lastWhite, lastBlack, activePartnerTime;
+Boolean adjustedClock;
long timeControl_2; /* [AS] Allow separate time controls */
-char *fullTimeControlString = NULL; /* [HGM] secondary TC: merge of MPS, TC and inc */
+char *fullTimeControlString = NULL, *nextSession, *whiteTC, *blackTC, activePartner; /* [HGM] secondary TC: merge of MPS, TC and inc */
long timeRemaining[2][MAX_MOVES];
-int matchGame = 0;
-TimeMark programStartTime;
+int matchGame = 0, nextGame = 0, roundNr = 0;
+Boolean waitingForGame = FALSE, startingEngine = FALSE;
+TimeMark programStartTime, pauseStart;
char ics_handle[MSG_SIZ];
int have_set_title = 0;
Board boards[MAX_MOVES];
/* [HGM] Following 7 needed for accurate legality tests: */
-signed char epStatus[MAX_MOVES];
-signed char castlingRights[MAX_MOVES][BOARD_SIZE]; // stores files for pieces with castling rights or -1
-signed char castlingRank[BOARD_SIZE]; // and corresponding ranks
-signed char initialRights[BOARD_SIZE], FENcastlingRights[BOARD_SIZE], fileRights[BOARD_SIZE];
+signed char castlingRank[BOARD_FILES]; // and corresponding ranks
+signed char initialRights[BOARD_FILES];
int nrCastlingRights; // For TwoKings, or to implement castling-unknown status
int initialRulePlies, FENrulePlies;
-char FENepStatus;
FILE *serverMoves = NULL; // next two for broadcasting (/serverMoves option)
-int loadFlag = 0;
-int shuffleOpenings;
+int loadFlag = 0;
+Boolean shuffleOpenings;
int mute; // mute all sounds
-ChessSquare FIDEArray[2][BOARD_SIZE] = {
+// [HGM] vari: next 12 to save and restore variations
+#define MAX_VARIATIONS 10
+int framePtr = MAX_MOVES-1; // points to free stack entry
+int storedGames = 0;
+int savedFirst[MAX_VARIATIONS];
+int savedLast[MAX_VARIATIONS];
+int savedFramePtr[MAX_VARIATIONS];
+char *savedDetails[MAX_VARIATIONS];
+ChessMove savedResult[MAX_VARIATIONS];
+
+void PushTail P((int firstMove, int lastMove));
+Boolean PopTail P((Boolean annotate));
+void PushInner P((int firstMove, int lastMove));
+void PopInner P((Boolean annotate));
+void CleanupTail P((void));
+
+ChessSquare FIDEArray[2][BOARD_FILES] = {
{ WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
{ BlackRook, BlackKnight, BlackBishop, BlackQueen,
BlackKing, BlackBishop, BlackKnight, BlackRook }
};
-ChessSquare twoKingsArray[2][BOARD_SIZE] = {
+ChessSquare twoKingsArray[2][BOARD_FILES] = {
{ WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
WhiteKing, WhiteKing, WhiteKnight, WhiteRook },
{ BlackRook, BlackKnight, BlackBishop, BlackQueen,
BlackKing, BlackKing, BlackKnight, BlackRook }
};
-ChessSquare KnightmateArray[2][BOARD_SIZE] = {
+ChessSquare KnightmateArray[2][BOARD_FILES] = {
{ WhiteRook, WhiteMan, WhiteBishop, WhiteQueen,
WhiteUnicorn, WhiteBishop, WhiteMan, WhiteRook },
{ BlackRook, BlackMan, BlackBishop, BlackQueen,
BlackUnicorn, BlackBishop, BlackMan, BlackRook }
};
-ChessSquare fairyArray[2][BOARD_SIZE] = { /* [HGM] Queen side differs from King side */
- { WhiteCannon, WhiteNightrider, WhiteAlfil, WhiteQueen,
+ChessSquare SpartanArray[2][BOARD_FILES] = {
+ { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
+ WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
+ { BlackAlfil, BlackMarshall, BlackKing, BlackDragon,
+ BlackDragon, BlackKing, BlackAngel, BlackAlfil }
+};
+
+ChessSquare fairyArray[2][BOARD_FILES] = { /* [HGM] Queen side differs from King side */
+ { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
- { BlackCannon, BlackNightrider, BlackAlfil, BlackQueen,
- BlackKing, BlackBishop, BlackKnight, BlackRook }
+ { BlackCardinal, BlackAlfil, BlackMarshall, BlackAngel,
+ BlackKing, BlackMarshall, BlackAlfil, BlackCardinal }
};
-ChessSquare ShatranjArray[2][BOARD_SIZE] = { /* [HGM] (movGen knows about Shatranj Q and P) */
+ChessSquare ShatranjArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
{ WhiteRook, WhiteKnight, WhiteAlfil, WhiteKing,
WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
{ BlackRook, BlackKnight, BlackAlfil, BlackKing,
BlackFerz, BlackAlfil, BlackKnight, BlackRook }
};
+ChessSquare makrukArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
+ { WhiteRook, WhiteKnight, WhiteMan, WhiteKing,
+ WhiteFerz, WhiteMan, WhiteKnight, WhiteRook },
+ { BlackRook, BlackKnight, BlackMan, BlackFerz,
+ BlackKing, BlackMan, BlackKnight, BlackRook }
+};
+
+ChessSquare aseanArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
+ { WhiteRook, WhiteKnight, WhiteMan, WhiteFerz,
+ WhiteKing, WhiteMan, WhiteKnight, WhiteRook },
+ { BlackRook, BlackKnight, BlackMan, BlackFerz,
+ BlackKing, BlackMan, BlackKnight, BlackRook }
+};
+
+ChessSquare lionArray[2][BOARD_FILES] = {
+ { WhiteRook, WhiteLion, WhiteBishop, WhiteQueen,
+ WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
+ { BlackRook, BlackLion, BlackBishop, BlackQueen,
+ BlackKing, BlackBishop, BlackKnight, BlackRook }
+};
+
-#if (BOARD_SIZE>=10)
-ChessSquare ShogiArray[2][BOARD_SIZE] = {
+#if (BOARD_FILES>=10)
+ChessSquare ShogiArray[2][BOARD_FILES] = {
{ WhiteQueen, WhiteKnight, WhiteFerz, WhiteWazir,
WhiteKing, WhiteWazir, WhiteFerz, WhiteKnight, WhiteQueen },
{ BlackQueen, BlackKnight, BlackFerz, BlackWazir,
BlackKing, BlackWazir, BlackFerz, BlackKnight, BlackQueen }
};
-ChessSquare XiangqiArray[2][BOARD_SIZE] = {
+ChessSquare XiangqiArray[2][BOARD_FILES] = {
{ WhiteRook, WhiteKnight, WhiteAlfil, WhiteFerz,
WhiteWazir, WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
{ BlackRook, BlackKnight, BlackAlfil, BlackFerz,
BlackWazir, BlackFerz, BlackAlfil, BlackKnight, BlackRook }
};
-ChessSquare CapablancaArray[2][BOARD_SIZE] = {
- { WhiteRook, WhiteKnight, WhiteAngel, WhiteBishop, WhiteQueen,
+ChessSquare CapablancaArray[2][BOARD_FILES] = {
+ { WhiteRook, WhiteKnight, WhiteAngel, WhiteBishop, WhiteQueen,
WhiteKing, WhiteBishop, WhiteMarshall, WhiteKnight, WhiteRook },
- { BlackRook, BlackKnight, BlackAngel, BlackBishop, BlackQueen,
+ { BlackRook, BlackKnight, BlackAngel, BlackBishop, BlackQueen,
BlackKing, BlackBishop, BlackMarshall, BlackKnight, BlackRook }
};
-ChessSquare GreatArray[2][BOARD_SIZE] = {
- { WhiteDragon, WhiteKnight, WhiteAlfil, WhiteGrasshopper, WhiteKing,
+ChessSquare GreatArray[2][BOARD_FILES] = {
+ { WhiteDragon, WhiteKnight, WhiteAlfil, WhiteGrasshopper, WhiteKing,
WhiteSilver, WhiteCardinal, WhiteAlfil, WhiteKnight, WhiteDragon },
- { BlackDragon, BlackKnight, BlackAlfil, BlackGrasshopper, BlackKing,
+ { BlackDragon, BlackKnight, BlackAlfil, BlackGrasshopper, BlackKing,
BlackSilver, BlackCardinal, BlackAlfil, BlackKnight, BlackDragon },
};
-ChessSquare JanusArray[2][BOARD_SIZE] = {
- { WhiteRook, WhiteAngel, WhiteKnight, WhiteBishop, WhiteKing,
+ChessSquare JanusArray[2][BOARD_FILES] = {
+ { WhiteRook, WhiteAngel, WhiteKnight, WhiteBishop, WhiteKing,
WhiteQueen, WhiteBishop, WhiteKnight, WhiteAngel, WhiteRook },
- { BlackRook, BlackAngel, BlackKnight, BlackBishop, BlackKing,
+ { BlackRook, BlackAngel, BlackKnight, BlackBishop, BlackKing,
BlackQueen, BlackBishop, BlackKnight, BlackAngel, BlackRook }
};
+ChessSquare GrandArray[2][BOARD_FILES] = {
+ { EmptySquare, WhiteKnight, WhiteBishop, WhiteQueen, WhiteKing,
+ WhiteMarshall, WhiteAngel, WhiteBishop, WhiteKnight, EmptySquare },
+ { EmptySquare, BlackKnight, BlackBishop, BlackQueen, BlackKing,
+ BlackMarshall, BlackAngel, BlackBishop, BlackKnight, EmptySquare }
+};
+
+ChessSquare ChuChessArray[2][BOARD_FILES] = {
+ { WhiteMan, WhiteKnight, WhiteBishop, WhiteCardinal, WhiteLion,
+ WhiteQueen, WhiteDragon, WhiteBishop, WhiteKnight, WhiteMan },
+ { BlackMan, BlackKnight, BlackBishop, BlackDragon, BlackQueen,
+ BlackLion, BlackCardinal, BlackBishop, BlackKnight, BlackMan }
+};
+
#ifdef GOTHIC
-ChessSquare GothicArray[2][BOARD_SIZE] = {
- { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen, WhiteMarshall,
+ChessSquare GothicArray[2][BOARD_FILES] = {
+ { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen, WhiteMarshall,
WhiteKing, WhiteAngel, WhiteBishop, WhiteKnight, WhiteRook },
- { BlackRook, BlackKnight, BlackBishop, BlackQueen, BlackMarshall,
+ { BlackRook, BlackKnight, BlackBishop, BlackQueen, BlackMarshall,
BlackKing, BlackAngel, BlackBishop, BlackKnight, BlackRook }
};
#else // !GOTHIC
#endif // !GOTHIC
#ifdef FALCON
-ChessSquare FalconArray[2][BOARD_SIZE] = {
- { WhiteRook, WhiteKnight, WhiteBishop, WhiteLance, WhiteQueen,
- WhiteKing, WhiteLance, WhiteBishop, WhiteKnight, WhiteRook },
- { BlackRook, BlackKnight, BlackBishop, BlackLance, BlackQueen,
- BlackKing, BlackLance, BlackBishop, BlackKnight, BlackRook }
+ChessSquare FalconArray[2][BOARD_FILES] = {
+ { WhiteRook, WhiteKnight, WhiteBishop, WhiteFalcon, WhiteQueen,
+ WhiteKing, WhiteFalcon, WhiteBishop, WhiteKnight, WhiteRook },
+ { BlackRook, BlackKnight, BlackBishop, BlackFalcon, BlackQueen,
+ BlackKing, BlackFalcon, BlackBishop, BlackKnight, BlackRook }
};
#else // !FALCON
#define FalconArray CapablancaArray
#endif // !FALCON
-#else // !(BOARD_SIZE>=10)
+#else // !(BOARD_FILES>=10)
#define XiangqiPosition FIDEArray
#define CapablancaArray FIDEArray
#define GothicArray FIDEArray
#define GreatArray FIDEArray
-#endif // !(BOARD_SIZE>=10)
+#endif // !(BOARD_FILES>=10)
-#if (BOARD_SIZE>=12)
-ChessSquare CourierArray[2][BOARD_SIZE] = {
+#if (BOARD_FILES>=12)
+ChessSquare CourierArray[2][BOARD_FILES] = {
{ WhiteRook, WhiteKnight, WhiteAlfil, WhiteBishop, WhiteMan, WhiteKing,
WhiteFerz, WhiteWazir, WhiteBishop, WhiteAlfil, WhiteKnight, WhiteRook },
{ BlackRook, BlackKnight, BlackAlfil, BlackBishop, BlackMan, BlackKing,
BlackFerz, BlackWazir, BlackBishop, BlackAlfil, BlackKnight, BlackRook }
};
-#else // !(BOARD_SIZE>=12)
+ChessSquare ChuArray[6][BOARD_FILES] = {
+ { WhiteLance, WhiteUnicorn, WhiteMan, WhiteFerz, WhiteWazir, WhiteKing,
+ WhiteAlfil, WhiteWazir, WhiteFerz, WhiteMan, WhiteUnicorn, WhiteLance },
+ { BlackLance, BlackUnicorn, BlackMan, BlackFerz, BlackWazir, BlackAlfil,
+ BlackKing, BlackWazir, BlackFerz, BlackMan, BlackUnicorn, BlackLance },
+ { WhiteCannon, EmptySquare, WhiteBishop, EmptySquare, WhiteNightrider, WhiteMarshall,
+ WhiteAngel, WhiteNightrider, EmptySquare, WhiteBishop, EmptySquare, WhiteCannon },
+ { BlackCannon, EmptySquare, BlackBishop, EmptySquare, BlackNightrider, BlackAngel,
+ BlackMarshall, BlackNightrider, EmptySquare, BlackBishop, EmptySquare, BlackCannon },
+ { WhiteFalcon, WhiteSilver, WhiteRook, WhiteCardinal, WhiteDragon, WhiteLion,
+ WhiteQueen, WhiteDragon, WhiteCardinal, WhiteRook, WhiteSilver, WhiteFalcon },
+ { BlackFalcon, BlackSilver, BlackRook, BlackCardinal, BlackDragon, BlackQueen,
+ BlackLion, BlackDragon, BlackCardinal, BlackRook, BlackSilver, BlackFalcon }
+};
+#else // !(BOARD_FILES>=12)
#define CourierArray CapablancaArray
-#endif // !(BOARD_SIZE>=12)
+#define ChuArray CapablancaArray
+#endif // !(BOARD_FILES>=12)
Board initialPosition;
"++++", etc. Also strips ()'s */
int
-string_to_rating(str)
- char *str;
+string_to_rating (char *str)
{
while(*str && !isdigit(*str)) ++str;
if (!*str)
}
void
-ClearProgramStats()
+ClearProgramStats ()
{
/* Init programStats */
programStats.movelist[0] = 0;
}
void
-InitBackEnd1()
+CommonEngineInit ()
+{ // [HGM] moved some code here from InitBackend1 that has to be done after both engines have contributed their settings
+ if (appData.firstPlaysBlack) {
+ first.twoMachinesColor = "black\n";
+ second.twoMachinesColor = "white\n";
+ } else {
+ first.twoMachinesColor = "white\n";
+ second.twoMachinesColor = "black\n";
+ }
+
+ first.other = &second;
+ second.other = &first;
+
+ { float norm = 1;
+ if(appData.timeOddsMode) {
+ norm = appData.timeOdds[0];
+ if(norm > appData.timeOdds[1]) norm = appData.timeOdds[1];
+ }
+ first.timeOdds = appData.timeOdds[0]/norm;
+ second.timeOdds = appData.timeOdds[1]/norm;
+ }
+
+ if(programVersion) free(programVersion);
+ if (appData.noChessProgram) {
+ programVersion = (char*) malloc(5 + strlen(PACKAGE_STRING));
+ sprintf(programVersion, "%s", PACKAGE_STRING);
+ } else {
+ /* [HGM] tidy: use tidy name, in stead of full pathname (which was probably a bug due to / vs \ ) */
+ programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
+ sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
+ }
+}
+
+void
+UnloadEngine (ChessProgramState *cps)
+{
+ /* Kill off first chess program */
+ if (cps->isr != NULL)
+ RemoveInputSource(cps->isr);
+ cps->isr = NULL;
+
+ if (cps->pr != NoProc) {
+ ExitAnalyzeMode();
+ DoSleep( appData.delayBeforeQuit );
+ SendToProgram("quit\n", cps);
+ DestroyChildProcess(cps->pr, 4 + cps->useSigterm);
+ }
+ cps->pr = NoProc;
+ if(appData.debugMode) fprintf(debugFP, "Unload %s\n", cps->which);
+}
+
+void
+ClearOptions (ChessProgramState *cps)
+{
+ int i;
+ cps->nrOptions = cps->comboCnt = 0;
+ for(i=0; i<MAX_OPTIONS; i++) {
+ cps->option[i].min = cps->option[i].max = cps->option[i].value = 0;
+ cps->option[i].textValue = 0;
+ }
+}
+
+char *engineNames[] = {
+ /* TRANSLATORS: "first" is the first of possible two chess engines. It is inserted into strings
+ such as "%s engine" / "%s chess program" / "%s machine" - all meaning the same thing */
+N_("first"),
+ /* TRANSLATORS: "second" is the second of possible two chess engines. It is inserted into strings
+ such as "%s engine" / "%s chess program" / "%s machine" - all meaning the same thing */
+N_("second")
+};
+
+void
+InitEngine (ChessProgramState *cps, int n)
+{ // [HGM] all engine initialiation put in a function that does one engine
+
+ ClearOptions(cps);
+
+ cps->which = engineNames[n];
+ cps->maybeThinking = FALSE;
+ cps->pr = NoProc;
+ cps->isr = NULL;
+ cps->sendTime = 2;
+ cps->sendDrawOffers = 1;
+
+ cps->program = appData.chessProgram[n];
+ cps->host = appData.host[n];
+ cps->dir = appData.directory[n];
+ cps->initString = appData.engInitString[n];
+ cps->computerString = appData.computerString[n];
+ cps->useSigint = TRUE;
+ cps->useSigterm = TRUE;
+ cps->reuse = appData.reuse[n];
+ cps->nps = appData.NPS[n]; // [HGM] nps: copy nodes per second
+ cps->useSetboard = FALSE;
+ cps->useSAN = FALSE;
+ cps->usePing = FALSE;
+ cps->lastPing = 0;
+ cps->lastPong = 0;
+ cps->usePlayother = FALSE;
+ cps->useColors = TRUE;
+ cps->useUsermove = FALSE;
+ cps->sendICS = FALSE;
+ cps->sendName = appData.icsActive;
+ cps->sdKludge = FALSE;
+ cps->stKludge = FALSE;
+ if(cps->tidy == NULL) cps->tidy = (char*) malloc(MSG_SIZ);
+ TidyProgramName(cps->program, cps->host, cps->tidy);
+ cps->matchWins = 0;
+ ASSIGN(cps->variants, appData.noChessProgram ? "" : appData.variant);
+ cps->analysisSupport = 2; /* detect */
+ cps->analyzing = FALSE;
+ cps->initDone = FALSE;
+ cps->reload = FALSE;
+ cps->pseudo = appData.pseudo[n];
+
+ /* New features added by Tord: */
+ cps->useFEN960 = FALSE;
+ cps->useOOCastle = TRUE;
+ /* End of new features added by Tord. */
+ cps->fenOverride = appData.fenOverride[n];
+
+ /* [HGM] time odds: set factor for each machine */
+ cps->timeOdds = appData.timeOdds[n];
+
+ /* [HGM] secondary TC: how to handle sessions that do not fit in 'level'*/
+ cps->accumulateTC = appData.accumulateTC[n];
+ cps->maxNrOfSessions = 1;
+
+ /* [HGM] debug */
+ cps->debug = FALSE;
+
+ cps->drawDepth = appData.drawDepth[n];
+ cps->supportsNPS = UNKNOWN;
+ cps->memSize = FALSE;
+ cps->maxCores = FALSE;
+ ASSIGN(cps->egtFormats, "");
+
+ /* [HGM] options */
+ cps->optionSettings = appData.engOptions[n];
+
+ cps->scoreIsAbsolute = appData.scoreIsAbsolute[n]; /* [AS] */
+ cps->isUCI = appData.isUCI[n]; /* [AS] */
+ cps->hasOwnBookUCI = appData.hasOwnBookUCI[n]; /* [AS] */
+ cps->highlight = 0;
+
+ if (appData.protocolVersion[n] > PROTOVER
+ || appData.protocolVersion[n] < 1)
+ {
+ char buf[MSG_SIZ];
+ int len;
+
+ len = snprintf(buf, MSG_SIZ, _("protocol version %d not supported"),
+ appData.protocolVersion[n]);
+ if( (len >= MSG_SIZ) && appData.debugMode )
+ fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
+
+ DisplayFatalError(buf, 0, 2);
+ }
+ else
+ {
+ cps->protocolVersion = appData.protocolVersion[n];
+ }
+
+ InitEngineUCI( installDir, cps ); // [HGM] moved here from winboard.c, to make available in xboard
+ ParseFeatures(appData.featureDefaults, cps);
+}
+
+ChessProgramState *savCps;
+
+GameMode oldMode;
+
+void
+LoadEngine ()
+{
+ int i;
+ if(WaitForEngine(savCps, LoadEngine)) return;
+ CommonEngineInit(); // recalculate time odds
+ if(gameInfo.variant != StringToVariant(appData.variant)) {
+ // we changed variant when loading the engine; this forces us to reset
+ Reset(TRUE, savCps != &first);
+ oldMode = BeginningOfGame; // to prevent restoring old mode
+ }
+ InitChessProgram(savCps, FALSE);
+ if(gameMode == EditGame) SendToProgram("force\n", savCps); // in EditGame mode engine must be in force mode
+ DisplayMessage("", "");
+ if (startedFromSetupPosition) SendBoard(savCps, backwardMostMove);
+ for (i = backwardMostMove; i < currentMove; i++) SendMoveToProgram(i, savCps);
+ ThawUI();
+ SetGNUMode();
+ if(oldMode == AnalyzeMode) AnalyzeModeEvent();
+}
+
+void
+ReplaceEngine (ChessProgramState *cps, int n)
+{
+ oldMode = gameMode; // remember mode, so it can be restored after loading sequence is complete
+ keepInfo = 1;
+ if(oldMode != BeginningOfGame) EditGameEvent();
+ keepInfo = 0;
+ UnloadEngine(cps);
+ appData.noChessProgram = FALSE;
+ appData.clockMode = TRUE;
+ InitEngine(cps, n);
+ UpdateLogos(TRUE);
+ if(n) return; // only startup first engine immediately; second can wait
+ savCps = cps; // parameter to LoadEngine passed as globals, to allow scheduled calling :-(
+ LoadEngine();
+}
+
+extern char *engineName, *engineDir, *engineChoice, *engineLine, *nickName, *params;
+extern Boolean isUCI, hasBook, storeVariant, v1, addToList, useNick;
+
+static char resetOptions[] =
+ "-reuse -firstIsUCI false -firstHasOwnBookUCI true -firstTimeOdds 1 "
+ "-firstInitString \"" INIT_STRING "\" -firstComputerString \"" COMPUTER_STRING "\" "
+ "-firstFeatures \"\" -firstLogo \"\" -firstAccumulateTC 1 -fd \".\" "
+ "-firstOptions \"\" -firstNPS -1 -fn \"\" -firstScoreAbs false";
+
+void
+FloatToFront(char **list, char *engineLine)
+{
+ char buf[MSG_SIZ], tidy[MSG_SIZ], *p = buf, *q, *r = buf;
+ int i=0;
+ if(appData.recentEngines <= 0) return;
+ TidyProgramName(engineLine, "localhost", tidy+1);
+ tidy[0] = buf[0] = '\n'; strcat(tidy, "\n");
+ strncpy(buf+1, *list, MSG_SIZ-50);
+ if(p = strstr(buf, tidy)) { // tidy name appears in list
+ q = strchr(++p, '\n'); if(q == NULL) return; // malformed, don't touch
+ while(*p++ = *++q); // squeeze out
+ }
+ strcat(tidy, buf+1); // put list behind tidy name
+ p = tidy + 1; while(q = strchr(p, '\n')) i++, r = p, p = q + 1; // count entries in new list
+ if(i > appData.recentEngines) *r = NULLCHAR; // if maximum rached, strip off last
+ ASSIGN(*list, tidy+1);
+}
+
+char *insert, *wbOptions; // point in ChessProgramNames were we should insert new engine
+
+void
+Load (ChessProgramState *cps, int i)
+{
+ char *p, *q, buf[MSG_SIZ], command[MSG_SIZ], buf2[MSG_SIZ], buf3[MSG_SIZ], jar;
+ if(engineLine && engineLine[0]) { // an engine was selected from the combo box
+ snprintf(buf, MSG_SIZ, "-fcp %s", engineLine);
+ SwapEngines(i); // kludge to parse -f* / -first* like it is -s* / -second*
+ ParseArgsFromString(resetOptions); appData.pvSAN[0] = FALSE;
+ FREE(appData.fenOverride[0]); appData.fenOverride[0] = NULL;
+ appData.firstProtocolVersion = PROTOVER;
+ ParseArgsFromString(buf);
+ SwapEngines(i);
+ ReplaceEngine(cps, i);
+ FloatToFront(&appData.recentEngineList, engineLine);
+ return;
+ }
+ p = engineName;
+ while(q = strchr(p, SLASH)) p = q+1;
+ if(*p== NULLCHAR) { DisplayError(_("You did not specify the engine executable"), 0); return; }
+ if(engineDir[0] != NULLCHAR) {
+ ASSIGN(appData.directory[i], engineDir); p = engineName;
+ } else if(p != engineName) { // derive directory from engine path, when not given
+ p[-1] = 0;
+ ASSIGN(appData.directory[i], engineName);
+ p[-1] = SLASH;
+ if(SLASH == '/' && p - engineName > 1) *(p -= 2) = '.'; // for XBoard use ./exeName as command after split!
+ } else { ASSIGN(appData.directory[i], "."); }
+ jar = (strstr(p, ".jar") == p + strlen(p) - 4);
+ if(params[0]) {
+ if(strchr(p, ' ') && !strchr(p, '"')) snprintf(buf2, MSG_SIZ, "\"%s\"", p), p = buf2; // quote if it contains spaces
+ snprintf(command, MSG_SIZ, "%s %s", p, params);
+ p = command;
+ }
+ if(jar) { snprintf(buf3, MSG_SIZ, "java -jar %s", p); p = buf3; }
+ ASSIGN(appData.chessProgram[i], p);
+ appData.isUCI[i] = isUCI;
+ appData.protocolVersion[i] = v1 ? 1 : PROTOVER;
+ appData.hasOwnBookUCI[i] = hasBook;
+ if(!nickName[0]) useNick = FALSE;
+ if(useNick) ASSIGN(appData.pgnName[i], nickName);
+ if(addToList) {
+ int len;
+ char quote;
+ q = firstChessProgramNames;
+ if(nickName[0]) snprintf(buf, MSG_SIZ, "\"%s\" -fcp ", nickName); else buf[0] = NULLCHAR;
+ quote = strchr(p, '"') ? '\'' : '"'; // use single quotes around engine command if it contains double quotes
+ snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), "%c%s%c -fd \"%s\"%s%s%s%s%s%s%s%s\n",
+ quote, p, quote, appData.directory[i],
+ useNick ? " -fn \"" : "",
+ useNick ? nickName : "",
+ useNick ? "\"" : "",
+ v1 ? " -firstProtocolVersion 1" : "",
+ hasBook ? "" : " -fNoOwnBookUCI",
+ isUCI ? (isUCI == TRUE ? " -fUCI" : gameInfo.variant == VariantShogi ? " -fUSI" : " -fUCCI") : "",
+ storeVariant ? " -variant " : "",
+ storeVariant ? VariantName(gameInfo.variant) : "");
+ if(wbOptions && wbOptions[0]) snprintf(buf+strlen(buf)-1, MSG_SIZ-strlen(buf), " %s\n", wbOptions);
+ firstChessProgramNames = malloc(len = strlen(q) + strlen(buf) + 1);
+ if(insert != q) insert[-1] = NULLCHAR;
+ snprintf(firstChessProgramNames, len, "%s\n%s%s", q, buf, insert);
+ if(q) free(q);
+ FloatToFront(&appData.recentEngineList, buf);
+ }
+ ReplaceEngine(cps, i);
+}
+
+void
+InitTimeControls ()
{
int matched, min, sec;
+ /*
+ * Parse timeControl resource
+ */
+ if (!ParseTimeControl(appData.timeControl, appData.timeIncrement,
+ appData.movesPerSession)) {
+ char buf[MSG_SIZ];
+ snprintf(buf, sizeof(buf), _("bad timeControl option %s"), appData.timeControl);
+ DisplayFatalError(buf, 0, 2);
+ }
+
+ /*
+ * Parse searchTime resource
+ */
+ if (*appData.searchTime != NULLCHAR) {
+ matched = sscanf(appData.searchTime, "%d:%d", &min, &sec);
+ if (matched == 1) {
+ searchTime = min * 60;
+ } else if (matched == 2) {
+ searchTime = min * 60 + sec;
+ } else {
+ char buf[MSG_SIZ];
+ snprintf(buf, sizeof(buf), _("bad searchTime option %s"), appData.searchTime);
+ DisplayFatalError(buf, 0, 2);
+ }
+ }
+}
+
+void
+InitBackEnd1 ()
+{
ShowThinkingEvent(); // [HGM] thinking: make sure post/nopost state is set according to options
+ startVariant = StringToVariant(appData.variant); // [HGM] nicks: remember original variant
GetTimeMark(&programStartTime);
- srand(programStartTime.ms); // [HGM] book: makes sure random is unpredictabe to msec level
+ srandom((programStartTime.ms + 1000*programStartTime.sec)*0x1001001); // [HGM] book: makes sure random is unpredictabe to msec level
+ appData.seedBase = random() + (random()<<15);
+ pauseStart = programStartTime; pauseStart.sec -= 100; // [HGM] matchpause: fake a pause that has long since ended
ClearProgramStats();
programStats.ok_to_send = 1;
if (appData.icsActive) {
appData.matchMode = FALSE;
appData.matchGames = 0;
-#if ZIPPY
+#if ZIPPY
appData.noChessProgram = !appData.zippyPlay;
#else
appData.zippyPlay = FALSE;
{
int i, j;
- for( i=0; i<MAX_MOVES; i++ ) {
+ for( i=0; i<=framePtr; i++ ) {
pvInfoList[i].depth = -1;
- epStatus[i]=EP_NONE;
- for( j=0; j<BOARD_SIZE; j++ ) castlingRights[i][j] = -1;
+ boards[i][EP_STATUS] = EP_NONE;
+ for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
}
}
- /*
- * Parse timeControl resource
- */
- if (!ParseTimeControl(appData.timeControl, appData.timeIncrement,
- appData.movesPerSession)) {
- char buf[MSG_SIZ];
- snprintf(buf, sizeof(buf), _("bad timeControl option %s"), appData.timeControl);
- DisplayFatalError(buf, 0, 2);
- }
-
- /*
- * Parse searchTime resource
- */
- if (*appData.searchTime != NULLCHAR) {
- matched = sscanf(appData.searchTime, "%d:%d", &min, &sec);
- if (matched == 1) {
- searchTime = min * 60;
- } else if (matched == 2) {
- searchTime = min * 60 + sec;
- } else {
- char buf[MSG_SIZ];
- snprintf(buf, sizeof(buf), _("bad searchTime option %s"), appData.searchTime);
- DisplayFatalError(buf, 0, 2);
- }
- }
+ InitTimeControls();
/* [AS] Adjudication threshold */
adjudicateLossThreshold = appData.adjudicateLossThreshold;
-
- first.which = "first";
- second.which = "second";
- first.maybeThinking = second.maybeThinking = FALSE;
- first.pr = second.pr = NoProc;
- first.isr = second.isr = NULL;
- first.sendTime = second.sendTime = 2;
- first.sendDrawOffers = 1;
- if (appData.firstPlaysBlack) {
- first.twoMachinesColor = "black\n";
- second.twoMachinesColor = "white\n";
- } else {
- first.twoMachinesColor = "white\n";
- second.twoMachinesColor = "black\n";
- }
- first.program = appData.firstChessProgram;
- second.program = appData.secondChessProgram;
- first.host = appData.firstHost;
- second.host = appData.secondHost;
- first.dir = appData.firstDirectory;
- second.dir = appData.secondDirectory;
- first.other = &second;
- second.other = &first;
- first.initString = appData.initString;
- second.initString = appData.secondInitString;
- first.computerString = appData.firstComputerString;
- second.computerString = appData.secondComputerString;
- first.useSigint = second.useSigint = TRUE;
- first.useSigterm = second.useSigterm = TRUE;
- first.reuse = appData.reuseFirst;
- second.reuse = appData.reuseSecond;
- first.nps = appData.firstNPS; // [HGM] nps: copy nodes per second
- second.nps = appData.secondNPS;
- first.useSetboard = second.useSetboard = FALSE;
- first.useSAN = second.useSAN = FALSE;
- first.usePing = second.usePing = FALSE;
- first.lastPing = second.lastPing = 0;
- first.lastPong = second.lastPong = 0;
- first.usePlayother = second.usePlayother = FALSE;
- first.useColors = second.useColors = TRUE;
- first.useUsermove = second.useUsermove = FALSE;
- first.sendICS = second.sendICS = FALSE;
- first.sendName = second.sendName = appData.icsActive;
- first.sdKludge = second.sdKludge = FALSE;
- first.stKludge = second.stKludge = FALSE;
- TidyProgramName(first.program, first.host, first.tidy);
- TidyProgramName(second.program, second.host, second.tidy);
- first.matchWins = second.matchWins = 0;
- strcpy(first.variants, appData.variant);
- strcpy(second.variants, appData.variant);
- first.analysisSupport = second.analysisSupport = 2; /* detect */
- first.analyzing = second.analyzing = FALSE;
- first.initDone = second.initDone = FALSE;
-
- /* New features added by Tord: */
- first.useFEN960 = FALSE; second.useFEN960 = FALSE;
- first.useOOCastle = TRUE; second.useOOCastle = TRUE;
- /* End of new features added by Tord. */
- first.fenOverride = appData.fenOverride1;
- second.fenOverride = appData.fenOverride2;
-
- /* [HGM] time odds: set factor for each machine */
- first.timeOdds = appData.firstTimeOdds;
- second.timeOdds = appData.secondTimeOdds;
- { int norm = 1;
- if(appData.timeOddsMode) {
- norm = first.timeOdds;
- if(norm > second.timeOdds) norm = second.timeOdds;
- }
- first.timeOdds /= norm;
- second.timeOdds /= norm;
- }
- /* [HGM] secondary TC: how to handle sessions that do not fit in 'level'*/
- first.accumulateTC = appData.firstAccumulateTC;
- second.accumulateTC = appData.secondAccumulateTC;
- first.maxNrOfSessions = second.maxNrOfSessions = 1;
+ InitEngine(&first, 0);
+ InitEngine(&second, 1);
+ CommonEngineInit();
- /* [HGM] debug */
- first.debug = second.debug = FALSE;
- first.supportsNPS = second.supportsNPS = UNKNOWN;
-
- /* [HGM] options */
- first.optionSettings = appData.firstOptions;
- second.optionSettings = appData.secondOptions;
-
- first.scoreIsAbsolute = appData.firstScoreIsAbsolute; /* [AS] */
- second.scoreIsAbsolute = appData.secondScoreIsAbsolute; /* [AS] */
- first.isUCI = appData.firstIsUCI; /* [AS] */
- second.isUCI = appData.secondIsUCI; /* [AS] */
- first.hasOwnBookUCI = appData.firstHasOwnBookUCI; /* [AS] */
- second.hasOwnBookUCI = appData.secondHasOwnBookUCI; /* [AS] */
-
- if (appData.firstProtocolVersion > PROTOVER ||
- appData.firstProtocolVersion < 1) {
- char buf[MSG_SIZ];
- sprintf(buf, _("protocol version %d not supported"),
- appData.firstProtocolVersion);
- DisplayFatalError(buf, 0, 2);
- } else {
- first.protocolVersion = appData.firstProtocolVersion;
- }
-
- if (appData.secondProtocolVersion > PROTOVER ||
- appData.secondProtocolVersion < 1) {
- char buf[MSG_SIZ];
- sprintf(buf, _("protocol version %d not supported"),
- appData.secondProtocolVersion);
- DisplayFatalError(buf, 0, 2);
- } else {
- second.protocolVersion = appData.secondProtocolVersion;
- }
+ pairing.which = "pairing"; // pairing engine
+ pairing.pr = NoProc;
+ pairing.isr = NULL;
+ pairing.program = appData.pairingEngine;
+ pairing.host = "localhost";
+ pairing.dir = ".";
if (appData.icsActive) {
appData.clockMode = TRUE; /* changes dynamically in ICS mode */
- } else if (*appData.searchTime != NULLCHAR || appData.noChessProgram) {
+ } else if (appData.noChessProgram) { // [HGM] st: searchTime mode now also is clockMode
appData.clockMode = FALSE;
first.sendTime = second.sendTime = 0;
}
-
+
#if ZIPPY
/* Override some settings from environment variables, for backward
compatibility. Unfortunately it's not feasible to have the env
ZippyInit();
}
#endif
-
- if (appData.noChessProgram) {
- programVersion = (char*) malloc(5 + strlen(PACKAGE_STRING));
- sprintf(programVersion, "%s", PACKAGE_STRING);
- } else {
- /* [HGM] tidy: use tidy name, in stead of full pathname (which was probably a bug due to / vs \ ) */
- programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
- sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
- }
if (!appData.icsActive) {
char buf[MSG_SIZ];
+ int len;
+
/* Check for variants that are supported only in ICS mode,
or not at all. Some that are accepted here nevertheless
have bugs; see comments below.
switch (variant) {
case VariantBughouse: /* need four players and two boards */
case VariantKriegspiel: /* need to hide pieces and move details */
- /* case VariantFischeRandom: (Fabien: moved below) */
- sprintf(buf, _("Variant %s supported only in ICS mode"), appData.variant);
+ /* case VariantFischeRandom: (Fabien: moved below) */
+ len = snprintf(buf,MSG_SIZ, _("Variant %s supported only in ICS mode"), appData.variant);
+ if( (len >= MSG_SIZ) && appData.debugMode )
+ fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
+
DisplayFatalError(buf, 0, 2);
return;
case Variant35:
case Variant36:
default:
- sprintf(buf, _("Unknown variant name %s"), appData.variant);
+ len = snprintf(buf, MSG_SIZ, _("Unknown variant name %s"), appData.variant);
+ if( (len >= MSG_SIZ) && appData.debugMode )
+ fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
+
DisplayFatalError(buf, 0, 2);
return;
+ case VariantNormal: /* definitely works! */
+ if(strcmp(appData.variant, "normal") && !appData.noChessProgram) { // [HGM] hope this is an engine-defined variant
+ safeStrCpy(engineVariant, appData.variant, MSG_SIZ);
+ return;
+ }
case VariantXiangqi: /* [HGM] repetition rules not implemented */
case VariantFairy: /* [HGM] TestLegality definitely off! */
case VariantGothic: /* [HGM] should work */
case VariantCapablanca: /* [HGM] should work */
case VariantCourier: /* [HGM] initial forced moves not implemented */
- case VariantShogi: /* [HGM] drops not tested for legality */
+ case VariantShogi: /* [HGM] could still mate with pawn drop */
+ case VariantChu: /* [HGM] experimental */
case VariantKnightmate: /* [HGM] should work */
case VariantCylinder: /* [HGM] untested */
case VariantFalcon: /* [HGM] untested */
case VariantCrazyhouse: /* holdings not shown, ([HGM] fixed that!)
offboard interposition not understood */
- case VariantNormal: /* definitely works! */
case VariantWildCastle: /* pieces not automatically shuffled */
case VariantNoCastle: /* pieces not automatically shuffled */
case VariantFischeRandom: /* [HGM] works and shuffles pieces */
case VariantAtomic: /* should work except for win condition */
case Variant3Check: /* should work except for win condition */
case VariantShatranj: /* should work except for all win conditions */
+ case VariantMakruk: /* should work except for draw countdown */
+ case VariantASEAN : /* should work except for draw countdown */
case VariantBerolina: /* might work if TestLegality is off */
case VariantCapaRandom: /* should work */
case VariantJanus: /* should work */
case VariantSuper: /* experimental */
case VariantGreat: /* experimental, requires legality testing to be off */
+ case VariantSChess: /* S-Chess, should work */
+ case VariantGrand: /* should work */
+ case VariantSpartan: /* should work */
+ case VariantLion: /* should work */
+ case VariantChuChess: /* should work */
break;
}
}
- InitEngineUCI( installDir, &first ); // [HGM] moved here from winboard.c, to make available in xboard
- InitEngineUCI( installDir, &second );
}
-int NextIntegerFromString( char ** str, long * value )
+int
+NextIntegerFromString (char ** str, long * value)
{
int result = -1;
char * s = *str;
return result;
}
-int NextTimeControlFromString( char ** str, long * value )
+int
+NextTimeControlFromString (char ** str, long * value)
{
long temp;
int result = NextIntegerFromString( str, &temp );
return result;
}
-int NextSessionFromString( char ** str, int *moves, long * tc, long *inc)
-{ /* [HGM] routine added to read '+moves/time' for secondary time control */
- int result = -1; long temp, temp2;
+int
+NextSessionFromString (char ** str, int *moves, long * tc, long *inc, int *incType)
+{ /* [HGM] routine added to read '+moves/time' for secondary time control. */
+ int result = -1, type = 0; long temp, temp2;
- if(**str != '+') return -1; // old params remain in force!
+ if(**str != ':') return -1; // old params remain in force!
(*str)++;
- if( NextTimeControlFromString( str, &temp ) ) return -1;
+ if(**str == '*') type = *(*str)++, temp = 0; // sandclock TC
+ if( NextIntegerFromString( str, &temp ) ) return -1;
+ if(type) { *moves = 0; *tc = temp * 500; *inc = temp * 1000; *incType = '*'; return 0; }
if(**str != '/') {
/* time only: incremental or sudden-death time control */
if(**str == '+') { /* increment follows; read it */
(*str)++;
+ if(**str == '!') type = *(*str)++; // Bronstein TC
if(result = NextIntegerFromString( str, &temp2)) return -1;
*inc = temp2 * 1000;
+ if(**str == '.') { // read fraction of increment
+ char *start = ++(*str);
+ if(result = NextIntegerFromString( str, &temp2)) return -1;
+ temp2 *= 1000;
+ while(start++ < *str) temp2 /= 10;
+ *inc += temp2;
+ }
} else *inc = 0;
- *moves = 0; *tc = temp * 1000;
+ *moves = 0; *tc = temp * 1000; *incType = type;
return 0;
- } else if(temp % 60 != 0) return -1; /* moves was given as min:sec */
+ }
(*str)++; /* classical time control */
- result = NextTimeControlFromString( str, &temp2);
+ result = NextIntegerFromString( str, &temp2); // NOTE: already converted to seconds by ParseTimeControl()
+
if(result == 0) {
- *moves = temp/60;
+ *moves = temp;
*tc = temp2 * 1000;
*inc = 0;
+ *incType = type;
}
return result;
}
-int GetTimeQuota(int movenr)
+int
+GetTimeQuota (int movenr, int lastUsed, char *tcString)
{ /* [HGM] get time to add from the multi-session time-control string */
- int moves=1; /* kludge to force reading of first session */
+ int incType, moves=1; /* kludge to force reading of first session */
long time, increment;
- char *s = fullTimeControlString;
+ char *s = tcString;
- if(appData.debugMode) fprintf(debugFP, "TC string = '%s'\n", fullTimeControlString);
+ if(!s || !*s) return 0; // empty TC string means we ran out of the last sudden-death version
do {
- if(moves) NextSessionFromString(&s, &moves, &time, &increment);
- if(appData.debugMode) fprintf(debugFP, "mps=%d tc=%d inc=%d\n", moves, (int) time, (int) increment);
+ if(moves) NextSessionFromString(&s, &moves, &time, &increment, &incType);
+ nextSession = s; suddenDeath = moves == 0 && increment == 0;
if(movenr == -1) return time; /* last move before new session */
+ if(incType == '*') increment = 0; else // for sandclock, time is added while not thinking
+ if(incType == '!' && lastUsed < increment) increment = lastUsed;
if(!moves) return increment; /* current session is incremental */
if(movenr >= 0) movenr -= moves; /* we already finished this session */
} while(movenr >= -1); /* try again for next session */
}
int
-ParseTimeControl(tc, ti, mps)
- char *tc;
- int ti;
- int mps;
+ParseTimeControl (char *tc, float ti, int mps)
{
long tc1;
long tc2;
- char buf[MSG_SIZ];
-
+ char buf[MSG_SIZ], buf2[MSG_SIZ], *mytc = tc;
+ int min, sec=0;
+
if(ti >= 0 && !strchr(tc, '+') && !strchr(tc, '/') ) mps = 0;
+ if(!strchr(tc, '+') && !strchr(tc, '/') && sscanf(tc, "%d:%d", &min, &sec) >= 1)
+ sprintf(mytc=buf2, "%d", 60*min+sec); // convert 'classical' min:sec tc string to seconds
if(ti > 0) {
+
if(mps)
- sprintf(buf, "+%d/%s+%d", mps, tc, ti);
- else sprintf(buf, "+%s+%d", tc, ti);
+ snprintf(buf, MSG_SIZ, ":%d/%s+%g", mps, mytc, ti);
+ else
+ snprintf(buf, MSG_SIZ, ":%s+%g", mytc, ti);
} else {
if(mps)
- sprintf(buf, "+%d/%s", mps, tc);
- else sprintf(buf, "+%s", tc);
+ snprintf(buf, MSG_SIZ, ":%d/%s", mps, mytc);
+ else
+ snprintf(buf, MSG_SIZ, ":%s", mytc);
}
- fullTimeControlString = StrSave(buf);
-
+ fullTimeControlString = StrSave(buf); // this should now be in PGN format
+
if( NextTimeControlFromString( &tc, &tc1 ) != 0 ) {
return FALSE;
}
-
+
if( *tc == '/' ) {
/* Parse second time control */
tc++;
-
+
if( NextTimeControlFromString( &tc, &tc2 ) != 0 ) {
return FALSE;
}
-
+
if( tc2 == 0 ) {
return FALSE;
}
-
+
timeControl_2 = tc2 * 1000;
}
else {
timeControl_2 = 0;
}
-
+
if( tc1 == 0 ) {
return FALSE;
}
-
+
timeControl = tc1 * 1000;
-
+
if (ti >= 0) {
timeIncrement = ti * 1000; /* convert to ms */
movesPerSession = 0;
}
void
-InitBackEnd2()
+InitBackEnd2 ()
{
if (appData.debugMode) {
- fprintf(debugFP, "%s\n", programVersion);
+# ifdef __GIT_VERSION
+ fprintf(debugFP, "Version: %s (%s)\n", programVersion, __GIT_VERSION);
+# else
+ fprintf(debugFP, "Version: %s\n", programVersion);
+# endif
}
+ ASSIGN(currentDebugFile, appData.nameOfDebugFile); // [HGM] debug split: remember initial name in use
+ set_cont_sequence(appData.wrapContSeq);
if (appData.matchGames > 0) {
appData.matchMode = TRUE;
} else if (appData.matchMode) {
}
}
+int
+CalculateIndex (int index, int gameNr)
+{ // [HGM] autoinc: absolute way to determine load index from game number (taking auto-inc and rewind into account)
+ int res;
+ if(index > 0) return index; // fixed nmber
+ if(index == 0) return 1;
+ res = (index == -1 ? gameNr : (gameNr-1)/2 + 1); // autoinc
+ if(appData.rewindIndex > 0) res = (res-1) % appData.rewindIndex + 1; // rewind
+ return res;
+}
+
+int
+LoadGameOrPosition (int gameNr)
+{ // [HGM] taken out of MatchEvent and NextMatchGame (to combine it)
+ if (*appData.loadGameFile != NULLCHAR) {
+ if (!LoadGameFromFile(appData.loadGameFile,
+ CalculateIndex(appData.loadGameIndex, gameNr),
+ appData.loadGameFile, FALSE)) {
+ DisplayFatalError(_("Bad game file"), 0, 1);
+ return 0;
+ }
+ } else if (*appData.loadPositionFile != NULLCHAR) {
+ if (!LoadPositionFromFile(appData.loadPositionFile,
+ CalculateIndex(appData.loadPositionIndex, gameNr),
+ appData.loadPositionFile)) {
+ DisplayFatalError(_("Bad position file"), 0, 1);
+ return 0;
+ }
+ }
+ return 1;
+}
+
+void
+ReserveGame (int gameNr, char resChar)
+{
+ FILE *tf = fopen(appData.tourneyFile, "r+");
+ char *p, *q, c, buf[MSG_SIZ];
+ if(tf == NULL) { nextGame = appData.matchGames + 1; return; } // kludge to terminate match
+ safeStrCpy(buf, lastMsg, MSG_SIZ);
+ DisplayMessage(_("Pick new game"), "");
+ flock(fileno(tf), LOCK_EX); // lock the tourney file while we are messing with it
+ ParseArgsFromFile(tf);
+ p = q = appData.results;
+ if(appData.debugMode) {
+ char *r = appData.participants;
+ fprintf(debugFP, "results = '%s'\n", p);
+ while(*r) fprintf(debugFP, *r >= ' ' ? "%c" : "\\%03o", *r), r++;
+ fprintf(debugFP, "\n");
+ }
+ while(*q && *q != ' ') q++; // get first un-played game (could be beyond end!)
+ nextGame = q - p;
+ q = malloc(strlen(p) + 2); // could be arbitrary long, but allow to extend by one!
+ safeStrCpy(q, p, strlen(p) + 2);
+ if(gameNr >= 0) q[gameNr] = resChar; // replace '*' with result
+ if(appData.debugMode) fprintf(debugFP, "pick next game from '%s': %d\n", q, nextGame);
+ if(nextGame <= appData.matchGames && resChar != ' ' && !abortMatch) { // reserve next game if tourney not yet done
+ if(q[nextGame] == NULLCHAR) q[nextGame+1] = NULLCHAR; // append one char
+ q[nextGame] = '*';
+ }
+ fseek(tf, -(strlen(p)+4), SEEK_END);
+ c = fgetc(tf);
+ if(c != '"') // depending on DOS or Unix line endings we can be one off
+ fseek(tf, -(strlen(p)+2), SEEK_END);
+ else fseek(tf, -(strlen(p)+3), SEEK_END);
+ fprintf(tf, "%s\"\n", q); fclose(tf); // update, and flush by closing
+ DisplayMessage(buf, "");
+ free(p); appData.results = q;
+ if(nextGame <= appData.matchGames && resChar != ' ' && !abortMatch &&
+ (gameNr < 0 || nextGame / appData.defaultMatchGames != gameNr / appData.defaultMatchGames)) {
+ int round = appData.defaultMatchGames * appData.tourneyType;
+ if(gameNr < 0 || appData.tourneyType < 1 || // gauntlet engine can always stay loaded as first engine
+ appData.tourneyType > 1 && nextGame/round != gameNr/round) // in multi-gauntlet change only after round
+ UnloadEngine(&first); // next game belongs to other pairing;
+ UnloadEngine(&second); // already unload the engines, so TwoMachinesEvent will load new ones.
+ }
+ if(appData.debugMode) fprintf(debugFP, "Reserved, next=%d, nr=%d\n", nextGame, gameNr);
+}
+
+void
+MatchEvent (int mode)
+{ // [HGM] moved out of InitBackend3, to make it callable when match starts through menu
+ int dummy;
+ if(matchMode) { // already in match mode: switch it off
+ abortMatch = TRUE;
+ if(!appData.tourneyFile[0]) appData.matchGames = matchGame; // kludge to let match terminate after next game.
+ return;
+ }
+// if(gameMode != BeginningOfGame) {
+// DisplayError(_("You can only start a match from the initial position."), 0);
+// return;
+// }
+ abortMatch = FALSE;
+ if(mode == 2) appData.matchGames = appData.defaultMatchGames;
+ /* Set up machine vs. machine match */
+ nextGame = 0;
+ NextTourneyGame(-1, &dummy); // sets appData.matchGames if this is tourney, to make sure ReserveGame knows it
+ if(appData.tourneyFile[0]) {
+ ReserveGame(-1, 0);
+ if(nextGame > appData.matchGames) {
+ char buf[MSG_SIZ];
+ if(strchr(appData.results, '*') == NULL) {
+ FILE *f;
+ appData.tourneyCycles++;
+ if(f = WriteTourneyFile(appData.results, NULL)) { // make a tourney file with increased number of cycles
+ fclose(f);
+ NextTourneyGame(-1, &dummy);
+ ReserveGame(-1, 0);
+ if(nextGame <= appData.matchGames) {
+ DisplayNote(_("You restarted an already completed tourney.\nOne more cycle will now be added to it.\nGames commence in 10 sec."));
+ matchMode = mode;
+ ScheduleDelayedEvent(NextMatchGame, 10000);
+ return;
+ }
+ }
+ }
+ snprintf(buf, MSG_SIZ, _("All games in tourney '%s' are already played or playing"), appData.tourneyFile);
+ DisplayError(buf, 0);
+ appData.tourneyFile[0] = 0;
+ return;
+ }
+ } else
+ if (appData.noChessProgram) { // [HGM] in tourney engines are loaded automatically
+ DisplayFatalError(_("Can't have a match with no chess programs"),
+ 0, 2);
+ return;
+ }
+ matchMode = mode;
+ matchGame = roundNr = 1;
+ first.matchWins = second.matchWins = 0; // [HGM] match: needed in later matches
+ NextMatchGame();
+}
+
+char *comboLine = NULL; // [HGM] recent: WinBoard's first-engine combobox line
+
void
InitBackEnd3 P((void))
{
GameMode initialMode;
char buf[MSG_SIZ];
- int err;
+ int err, len;
+
+ if(!appData.icsActive && !appData.noChessProgram && !appData.matchMode && // mode involves only first engine
+ !strcmp(appData.variant, "normal") && // no explicit variant request
+ appData.NrRanks == -1 && appData.NrFiles == -1 && appData.holdingsSize == -1 && // no size overrides requested
+ !SupportedVariant(first.variants, VariantNormal, 8, 8, 0, first.protocolVersion, "") && // but 'normal' won't work with engine
+ !SupportedVariant(first.variants, VariantFischeRandom, 8, 8, 0, first.protocolVersion, "") ) { // nor will Chess960
+ char c, *q = first.variants, *p = strchr(q, ',');
+ if(p) *p = NULLCHAR;
+ if(StringToVariant(q) != VariantUnknown) { // the engine can play a recognized variant, however
+ int w, h, s;
+ if(sscanf(q, "%dx%d+%d_%c", &w, &h, &s, &c) == 4) // get size overrides the engine needs with it (if any)
+ appData.NrFiles = w, appData.NrRanks = h, appData.holdingsSize = s, q = strchr(q, '_') + 1;
+ ASSIGN(appData.variant, q); // fake user requested the first variant played by the engine
+ Reset(TRUE, FALSE); // and re-initialize
+ }
+ if(p) *p = ',';
+ }
InitChessProgram(&first, startedFromSetupPosition);
+ if(!appData.noChessProgram) { /* [HGM] tidy: redo program version to use name from myname feature */
+ free(programVersion);
+ programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
+ sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
+ FloatToFront(&appData.recentEngineList, comboLine ? comboLine : appData.firstChessProgram);
+ }
if (appData.icsActive) {
#ifdef WIN32
/* [DM] Make a console window if needed [HGM] merged ifs */
- ConsoleCreate();
+ ConsoleCreate();
#endif
err = establish();
- if (err != 0) {
- if (*appData.icsCommPort != NULLCHAR) {
- sprintf(buf, _("Could not open comm port %s"),
- appData.icsCommPort);
- } else {
- snprintf(buf, sizeof(buf), _("Could not connect to host %s, port %s"),
+ if (err != 0)
+ {
+ if (*appData.icsCommPort != NULLCHAR)
+ len = snprintf(buf, MSG_SIZ, _("Could not open comm port %s"),
+ appData.icsCommPort);
+ else
+ len = snprintf(buf, MSG_SIZ, _("Could not connect to host %s, port %s"),
appData.icsHost, appData.icsPort);
- }
+
+ if( (len >= MSG_SIZ) && appData.debugMode )
+ fprintf(debugFP, "InitBackEnd3: buffer truncated.\n");
+
DisplayFatalError(buf, err, 1);
return;
}
AddInputSource(icsPR, FALSE, read_from_ics, &telnetISR);
fromUserISR =
AddInputSource(NoProc, FALSE, read_from_player, &fromUserISR);
+ if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
+ ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
} else if (appData.noChessProgram) {
SetNCPMode();
} else {
cmailISR =
AddInputSource(cmailPR, FALSE, CmailSigHandlerCallBack, &cmailISR);
}
-
+
ThawUI();
DisplayMessage("", "");
if (StrCaseCmp(appData.initialMode, "") == 0) {
initialMode = BeginningOfGame;
+ if(!appData.icsActive && appData.noChessProgram) { // [HGM] could be fall-back
+ gameMode = MachinePlaysBlack; // "Machine Black" might have been implicitly highlighted
+ ModeHighlight(); // make sure XBoard knows it is highlighted, so it will un-highlight it
+ gameMode = BeginningOfGame; // in case BeginningOfGame now means "Edit Position"
+ ModeHighlight();
+ }
} else if (StrCaseCmp(appData.initialMode, "TwoMachines") == 0) {
initialMode = TwoMachinesPlay;
} else if (StrCaseCmp(appData.initialMode, "AnalyzeFile") == 0) {
- initialMode = AnalyzeFile;
+ initialMode = AnalyzeFile;
} else if (StrCaseCmp(appData.initialMode, "Analysis") == 0) {
initialMode = AnalyzeMode;
} else if (StrCaseCmp(appData.initialMode, "MachineWhite") == 0) {
} else if (StrCaseCmp(appData.initialMode, "Training") == 0) {
initialMode = Training;
} else {
- sprintf(buf, _("Unknown initialMode %s"), appData.initialMode);
+ len = snprintf(buf, MSG_SIZ, _("Unknown initialMode %s"), appData.initialMode);
+ if( (len >= MSG_SIZ) && appData.debugMode )
+ fprintf(debugFP, "InitBackEnd3: buffer truncated.\n");
+
DisplayFatalError(buf, 0, 2);
return;
}
if (appData.matchMode) {
- /* Set up machine vs. machine match */
- if (appData.noChessProgram) {
- DisplayFatalError(_("Can't have a match with no chess programs"),
- 0, 2);
- return;
- }
- matchMode = TRUE;
- matchGame = 1;
- if (*appData.loadGameFile != NULLCHAR) {
- int index = appData.loadGameIndex; // [HGM] autoinc
- if(index<0) lastIndex = index = 1;
- if (!LoadGameFromFile(appData.loadGameFile,
- index,
- appData.loadGameFile, FALSE)) {
- DisplayFatalError(_("Bad game file"), 0, 1);
- return;
- }
- } else if (*appData.loadPositionFile != NULLCHAR) {
- int index = appData.loadPositionIndex; // [HGM] autoinc
- if(index<0) lastIndex = index = 1;
- if (!LoadPositionFromFile(appData.loadPositionFile,
- index,
- appData.loadPositionFile)) {
- DisplayFatalError(_("Bad position file"), 0, 1);
- return;
- }
- }
- TwoMachinesEvent();
+ if(appData.tourneyFile[0]) { // start tourney from command line
+ FILE *f;
+ if(f = fopen(appData.tourneyFile, "r")) {
+ ParseArgsFromFile(f); // make sure tourney parmeters re known
+ fclose(f);
+ appData.clockMode = TRUE;
+ SetGNUMode();
+ } else appData.tourneyFile[0] = NULLCHAR; // for now ignore bad tourney file
+ }
+ MatchEvent(TRUE);
} else if (*appData.cmailGameName != NULLCHAR) {
/* Set up cmail mode */
ReloadCmailMsgEvent(TRUE);
}
/* [HGM] loadPos: make that every new game uses the setup */
/* from file as long as we do not switch variant */
- if(!blackPlaysFirst) { int i;
+ if(!blackPlaysFirst) {
startedFromPositionFile = TRUE;
CopyBoard(filePosition, boards[0]);
- for(i=0; i<BOARD_SIZE; i++) fileRights[i] = castlingRights[0][i];
+ CopyBoard(initialPosition, boards[0]);
}
}
if (initialMode == AnalyzeMode) {
}
}
+void
+HistorySet (char movelist[][2*MOVE_LEN], int first, int last, int current)
+{
+ DisplayBook(current+1);
+
+ MoveHistorySet( movelist, first, last, current, pvInfoList );
+
+ EvalGraphSet( first, last, current, pvInfoList );
+
+ MakeEngineOutputTitle();
+}
+
/*
* Establish will establish a contact to a remote host.port.
* Sets icsPR to a ProcRef for a process (or pseudo-process)
* Returns 0 if okay, error code if not.
*/
int
-establish()
+establish ()
{
char buf[MSG_SIZ];
appData.icsHost, appData.icsPort);
} else {
snprintf(buf, sizeof(buf), "%s %s -l %s %s %s %s",
- appData.remoteShell, appData.gateway,
+ appData.remoteShell, appData.gateway,
appData.remoteUser, appData.telnetProgram,
appData.icsHost, appData.icsPort);
}
}
void
-show_bytes(fp, buf, count)
- FILE *fp;
- char *buf;
- int count;
+EscapeExpand (char *p, char *q)
+{ // [HGM] initstring: routine to shape up string arguments
+ while(*p++ = *q++) if(p[-1] == '\\')
+ switch(*q++) {
+ case 'n': p[-1] = '\n'; break;
+ case 'r': p[-1] = '\r'; break;
+ case 't': p[-1] = '\t'; break;
+ case '\\': p[-1] = '\\'; break;
+ case 0: *p = 0; return;
+ default: p[-1] = q[-1]; break;
+ }
+}
+
+void
+show_bytes (FILE *fp, char *buf, int count)
{
while (count--) {
if (*buf < 040 || *(unsigned char *) buf > 0177) {
/* Returns an errno value */
int
-OutputMaybeTelnet(pr, message, count, outError)
- ProcRef pr;
- char *message;
- int count;
- int *outError;
+OutputMaybeTelnet (ProcRef pr, char *message, int count, int *outError)
{
char buf[8192], *p, *q, *buflim;
int left, newcount, outcount;
}
void
-read_from_player(isr, closure, message, count, error)
- InputSourceRef isr;
- VOIDSTAR closure;
- char *message;
- int count;
- int error;
+read_from_player (InputSourceRef isr, VOIDSTAR closure, char *message, int count, int error)
{
int outError, outCount;
static int gotEof = 0;
+ static FILE *ini;
/* Pass data read from player on to ICS */
if (count > 0) {
if (outCount < count) {
DisplayFatalError(_("Error writing to ICS"), outError, 1);
}
+ if(have_sent_ICS_logon == 2) {
+ if(ini = fopen(appData.icsLogon, "w")) { // save first two lines (presumably username & password) on init script file
+ fprintf(ini, "%s", message);
+ have_sent_ICS_logon = 3;
+ } else
+ have_sent_ICS_logon = 1;
+ } else if(have_sent_ICS_logon == 3) {
+ fprintf(ini, "%s", message);
+ fclose(ini);
+ have_sent_ICS_logon = 1;
+ }
} else if (count < 0) {
RemoveInputSource(isr);
DisplayFatalError(_("Error reading from keyboard"), error, 1);
}
void
-KeepAlive()
+KeepAlive ()
{ // [HGM] alive: periodically send dummy (date) command to ICS to prevent time-out
+ if(!connectionAlive) DisplayFatalError("No response from ICS", 0, 1);
+ connectionAlive = FALSE; // only sticks if no response to 'date' command.
SendToICS("date\n");
if(appData.keepAlive) ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
}
/* added routine for printf style output to ics */
-void ics_printf(char *format, ...)
+void
+ics_printf (char *format, ...)
{
- char buffer[MSG_SIZ], *args;
-
- args = (char *)&format + sizeof(format);
- vsnprintf(buffer, sizeof(buffer), format, args);
- buffer[sizeof(buffer)-1] = '\0';
- SendToICS(buffer);
+ char buffer[MSG_SIZ];
+ va_list args;
+
+ va_start(args, format);
+ vsnprintf(buffer, sizeof(buffer), format, args);
+ buffer[sizeof(buffer)-1] = '\0';
+ SendToICS(buffer);
+ va_end(args);
}
void
-SendToICS(s)
- char *s;
+SendToICS (char *s)
{
int count, outCount, outError;
- if (icsPR == NULL) return;
+ if (icsPR == NoProc) return;
count = strlen(s);
outCount = OutputMaybeTelnet(icsPR, s, count, &outError);
without a delay causes problems when using timestamp on ICC
(at least on my machine). */
void
-SendToICSDelayed(s,msdelay)
- char *s;
- long msdelay;
+SendToICSDelayed (char *s, long msdelay)
{
int count, outCount, outError;
- if (icsPR == NULL) return;
+ if (icsPR == NoProc) return;
count = strlen(s);
if (appData.debugMode) {
/* Remove all highlighting escape sequences in s
- Also deletes any suffix starting with '('
+ Also deletes any suffix starting with '('
*/
char *
-StripHighlightAndTitle(s)
- char *s;
+StripHighlightAndTitle (char *s)
{
static char retbuf[MSG_SIZ];
char *p = retbuf;
/* Remove all highlighting escape sequences in s */
char *
-StripHighlight(s)
- char *s;
+StripHighlight (char *s)
{
static char retbuf[MSG_SIZ];
char *p = retbuf;
return retbuf;
}
+char engineVariant[MSG_SIZ];
char *variantNames[] = VARIANT_NAMES;
char *
-VariantName(v)
- VariantClass v;
+VariantName (VariantClass v)
{
+ if(v == VariantUnknown || *engineVariant) return engineVariant;
return variantNames[v];
}
/* Identify a variant from the strings the chess servers use or the
PGN Variant tag names we use. */
VariantClass
-StringToVariant(e)
- char *e;
+StringToVariant (char *e)
{
char *p;
int wnum = -1;
VariantClass v = VariantNormal;
int i, found = FALSE;
- char buf[MSG_SIZ];
+ char buf[MSG_SIZ], c;
+ int len;
if (!e) return v;
/* [HGM] skip over optional board-size prefixes */
- if( sscanf(e, "%dx%d_", &i, &i) == 2 ||
- sscanf(e, "%dx%d+%d_", &i, &i, &i) == 3 ) {
+ if( sscanf(e, "%dx%d_%c", &i, &i, &c) == 3 ||
+ sscanf(e, "%dx%d+%d_%c", &i, &i, &i, &c) == 4 ) {
while( *e++ != '_');
}
found = TRUE;
} else
for (i=0; i<sizeof(variantNames)/sizeof(char*); i++) {
- if (StrCaseStr(e, variantNames[i])) {
+ if (p = StrCaseStr(e, variantNames[i])) {
+ if(p && i >= VariantShogi && (p != e && !appData.icsActive || isalpha(p[strlen(variantNames[i])]))) continue;
v = (VariantClass) i;
found = TRUE;
break;
if (!found) {
if ((StrCaseStr(e, "fischer") && StrCaseStr(e, "random"))
- || StrCaseStr(e, "wild/fr")
+ || StrCaseStr(e, "wild/fr")
|| StrCaseStr(e, "frc") || StrCaseStr(e, "960")) {
v = VariantFischeRandom;
} else if ((i = 4, p = StrCaseStr(e, "wild")) ||
v = VariantShatranj;
break;
- /* Temporary names for future ICC types. The name *will* change in
+ /* Temporary names for future ICC types. The name *will* change in
the next xboard/WinBoard release after ICC defines it. */
case 29:
v = Variant29;
v = VariantNormal;
break;
default:
- sprintf(buf, _("Unknown wild type %d"), wnum);
+ len = snprintf(buf, MSG_SIZ, _("Unknown wild type %d"), wnum);
+ if( (len >= MSG_SIZ) && appData.debugMode )
+ fprintf(debugFP, "StringToVariant: buffer truncated.\n");
+
DisplayError(buf, 0);
v = VariantUnknown;
break;
}
}
if (appData.debugMode) {
- fprintf(debugFP, _("recognized '%s' (%d) as variant %s\n"),
+ fprintf(debugFP, "recognized '%s' (%d) as variant %s\n",
e, wnum, VariantName(v));
}
return v;
copied into star_match.
*/
int
-looking_at(buf, index, pattern)
- char *buf;
- int *index;
- char *pattern;
+looking_at ( char *buf, int *index, char *pattern)
{
char *bufp = &buf[*index], *patternp = pattern;
int star_count = 0;
char *matchp = star_match[0];
-
+
for (;;) {
if (*patternp == NULLCHAR) {
*index = leftover_start = bufp - buf;
}
void
-SendToPlayer(data, length)
- char *data;
- int length;
+SendToPlayer (char *data, int length)
{
int error, outCount;
outCount = OutputToProcess(NoProc, data, length, &error);
}
void
-PackHolding(packed, holding)
- char packed[];
- char *holding;
+PackHolding (char packed[], char *holding)
{
char *p = holding;
char *q = packed;
/* Telnet protocol requests from the front end */
void
-TelnetRequest(ddww, option)
- unsigned char ddww, option;
+TelnetRequest (unsigned char ddww, unsigned char option)
{
unsigned char msg[3];
int outCount, outError;
break;
default:
ddwwStr = buf1;
- sprintf(buf1, "%d", ddww);
+ snprintf(buf1,sizeof(buf1)/sizeof(buf1[0]), "%d", ddww);
break;
}
switch (option) {
break;
default:
optionStr = buf2;
- sprintf(buf2, "%d", option);
+ snprintf(buf2,sizeof(buf2)/sizeof(buf2[0]), "%d", option);
break;
}
fprintf(debugFP, ">%s %s ", ddwwStr, optionStr);
}
void
-DoEcho()
+DoEcho ()
{
if (!appData.icsActive) return;
TelnetRequest(TN_DO, TN_ECHO);
}
void
-DontEcho()
+DontEcho ()
{
if (!appData.icsActive) return;
TelnetRequest(TN_DONT, TN_ECHO);
}
void
-CopyHoldings(Board board, char *holdings, ChessSquare lowestPiece)
+CopyHoldings (Board board, char *holdings, ChessSquare lowestPiece)
{
/* put the holdings sent to us by the server on the board holdings area */
int i, j, holdingsColumn, holdingsStartRow, direction, countsColumn;
ChessSquare piece;
if(gameInfo.holdingsWidth < 2) return;
+ if(gameInfo.variant != VariantBughouse && board[HOLDINGS_SET])
+ return; // prevent overwriting by pre-board holdings
if( (int)lowestPiece >= BlackPawn ) {
holdingsColumn = 0;
board[holdingsStartRow+j*direction][holdingsColumn] = piece;
board[holdingsStartRow+j*direction][countsColumn]++;
}
-
}
void
-VariantSwitch(Board board, VariantClass newVariant)
+VariantSwitch (Board board, VariantClass newVariant)
{
int newHoldingsWidth, newWidth = 8, newHeight = 8, i, j;
- int oldCurrentMove = currentMove, oldForwardMostMove = forwardMostMove, oldBackwardMostMove = backwardMostMove;
-// Board tempBoard; int saveCastling[BOARD_SIZE], saveEP;
+ static Board oldBoard;
startedFromPositionFile = FALSE;
if(gameInfo.variant == newVariant) return;
*/
- if (appData.debugMode) {
- fprintf(debugFP, "Switch board from %s to %s\n",
- VariantName(gameInfo.variant), VariantName(newVariant));
- setbuf(debugFP, NULL);
- }
- shuffleOpenings = 0; /* [HGM] shuffle */
- gameInfo.holdingsSize = 5; /* [HGM] prepare holdings */
- switch(newVariant) {
- case VariantShogi:
- newWidth = 9; newHeight = 9;
- gameInfo.holdingsSize = 7;
- case VariantBughouse:
- case VariantCrazyhouse:
- newHoldingsWidth = 2; break;
- default:
- newHoldingsWidth = gameInfo.holdingsSize = 0;
- }
-
- if(newWidth != gameInfo.boardWidth ||
- newHeight != gameInfo.boardHeight ||
- newHoldingsWidth != gameInfo.holdingsWidth ) {
-
- /* shift position to new playing area, if needed */
- if(newHoldingsWidth > gameInfo.holdingsWidth) {
- for(i=0; i<BOARD_HEIGHT; i++)
- for(j=BOARD_RGHT-1; j>=BOARD_LEFT; j--)
- board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
- board[i][j];
- for(i=0; i<newHeight; i++) {
- board[i][0] = board[i][newWidth+2*newHoldingsWidth-1] = EmptySquare;
- board[i][1] = board[i][newWidth+2*newHoldingsWidth-2] = (ChessSquare) 0;
- }
- } else if(newHoldingsWidth < gameInfo.holdingsWidth) {
- for(i=0; i<BOARD_HEIGHT; i++)
- for(j=BOARD_LEFT; j<BOARD_RGHT; j++)
- board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
- board[i][j];
- }
-
- gameInfo.boardWidth = newWidth;
- gameInfo.boardHeight = newHeight;
- gameInfo.holdingsWidth = newHoldingsWidth;
- gameInfo.variant = newVariant;
- InitDrawingSizes(-2, 0);
-
- /* [HGM] The following should definitely be solved in a better way */
- InitPosition(FALSE); /* this sets up board[0], but also other stuff */
- } else { gameInfo.variant = newVariant; InitPosition(FALSE); }
-
- forwardMostMove = oldForwardMostMove;
- backwardMostMove = oldBackwardMostMove;
- currentMove = oldCurrentMove; /* InitPos reset these, but we need still to redraw the position */
+ if (appData.debugMode) {
+ fprintf(debugFP, "Switch board from %s to %s\n",
+ VariantName(gameInfo.variant), VariantName(newVariant));
+ setbuf(debugFP, NULL);
+ }
+ shuffleOpenings = 0; /* [HGM] shuffle */
+ gameInfo.holdingsSize = 5; /* [HGM] prepare holdings */
+ switch(newVariant)
+ {
+ case VariantShogi:
+ newWidth = 9; newHeight = 9;
+ gameInfo.holdingsSize = 7;
+ case VariantBughouse:
+ case VariantCrazyhouse:
+ newHoldingsWidth = 2; break;
+ case VariantGreat:
+ newWidth = 10;
+ case VariantSuper:
+ newHoldingsWidth = 2;
+ gameInfo.holdingsSize = 8;
+ break;
+ case VariantGothic:
+ case VariantCapablanca:
+ case VariantCapaRandom:
+ newWidth = 10;
+ default:
+ newHoldingsWidth = gameInfo.holdingsSize = 0;
+ };
+
+ if(newWidth != gameInfo.boardWidth ||
+ newHeight != gameInfo.boardHeight ||
+ newHoldingsWidth != gameInfo.holdingsWidth ) {
+
+ /* shift position to new playing area, if needed */
+ if(newHoldingsWidth > gameInfo.holdingsWidth) {
+ for(i=0; i<BOARD_HEIGHT; i++)
+ for(j=BOARD_RGHT-1; j>=BOARD_LEFT; j--)
+ board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
+ board[i][j];
+ for(i=0; i<newHeight; i++) {
+ board[i][0] = board[i][newWidth+2*newHoldingsWidth-1] = EmptySquare;
+ board[i][1] = board[i][newWidth+2*newHoldingsWidth-2] = (ChessSquare) 0;
+ }
+ } else if(newHoldingsWidth < gameInfo.holdingsWidth) {
+ for(i=0; i<BOARD_HEIGHT; i++)
+ for(j=BOARD_LEFT; j<BOARD_RGHT; j++)
+ board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
+ board[i][j];
+ }
+ board[HOLDINGS_SET] = 0;
+ gameInfo.boardWidth = newWidth;
+ gameInfo.boardHeight = newHeight;
+ gameInfo.holdingsWidth = newHoldingsWidth;
+ gameInfo.variant = newVariant;
+ InitDrawingSizes(-2, 0);
+ } else gameInfo.variant = newVariant;
+ CopyBoard(oldBoard, board); // remember correctly formatted board
+ InitPosition(FALSE); /* this sets up board[0], but also other stuff */
+ DrawPosition(TRUE, currentMove ? boards[currentMove] : oldBoard);
}
static int loggedOn = FALSE;
char gs_kind[MSG_SIZ];
static char player1Name[128] = "";
static char player2Name[128] = "";
+static char cont_seq[] = "\n\\ ";
static int player1Rating = -1;
static int player2Rating = -1;
/*----------------------------*/
ColorClass curColor = ColorNormal;
int suppressKibitz = 0;
+// [HGM] seekgraph
+Boolean soughtPending = FALSE;
+Boolean seekGraphUp;
+#define MAX_SEEK_ADS 200
+#define SQUARE 0x80
+char *seekAdList[MAX_SEEK_ADS];
+int ratingList[MAX_SEEK_ADS], xList[MAX_SEEK_ADS], yList[MAX_SEEK_ADS], seekNrList[MAX_SEEK_ADS], zList[MAX_SEEK_ADS];
+float tcList[MAX_SEEK_ADS];
+char colorList[MAX_SEEK_ADS];
+int nrOfSeekAds = 0;
+int minRating = 1010, maxRating = 2800;
+int hMargin = 10, vMargin = 20, h, w;
+extern int squareSize, lineGap;
+
+void
+PlotSeekAd (int i)
+{
+ int x, y, color = 0, r = ratingList[i]; float tc = tcList[i];
+ xList[i] = yList[i] = -100; // outside graph, so cannot be clicked
+ if(r < minRating+100 && r >=0 ) r = minRating+100;
+ if(r > maxRating) r = maxRating;
+ if(tc < 1.f) tc = 1.f;
+ if(tc > 95.f) tc = 95.f;
+ x = (w-hMargin-squareSize/8-7)* log(tc)/log(95.) + hMargin;
+ y = ((double)r - minRating)/(maxRating - minRating)
+ * (h-vMargin-squareSize/8-1) + vMargin;
+ if(ratingList[i] < 0) y = vMargin + squareSize/4;
+ if(strstr(seekAdList[i], " u ")) color = 1;
+ if(!strstr(seekAdList[i], "lightning") && // for now all wilds same color
+ !strstr(seekAdList[i], "bullet") &&
+ !strstr(seekAdList[i], "blitz") &&
+ !strstr(seekAdList[i], "standard") ) color = 2;
+ if(strstr(seekAdList[i], "(C) ")) color |= SQUARE; // plot computer seeks as squares
+ DrawSeekDot(xList[i]=x+3*(color&~SQUARE), yList[i]=h-1-y, colorList[i]=color);
+}
+
+void
+PlotSingleSeekAd (int i)
+{
+ PlotSeekAd(i);
+}
+
+void
+AddAd (char *handle, char *rating, int base, int inc, char rated, char *type, int nr, Boolean plot)
+{
+ char buf[MSG_SIZ], *ext = "";
+ VariantClass v = StringToVariant(type);
+ if(strstr(type, "wild")) {
+ ext = type + 4; // append wild number
+ if(v == VariantFischeRandom) type = "chess960"; else
+ if(v == VariantLoadable) type = "setup"; else
+ type = VariantName(v);
+ }
+ snprintf(buf, MSG_SIZ, "%s (%s) %d %d %c %s%s", handle, rating, base, inc, rated, type, ext);
+ if(nrOfSeekAds < MAX_SEEK_ADS-1) {
+ if(seekAdList[nrOfSeekAds]) free(seekAdList[nrOfSeekAds]);
+ ratingList[nrOfSeekAds] = -1; // for if seeker has no rating
+ sscanf(rating, "%d", &ratingList[nrOfSeekAds]);
+ tcList[nrOfSeekAds] = base + (2./3.)*inc;
+ seekNrList[nrOfSeekAds] = nr;
+ zList[nrOfSeekAds] = 0;
+ seekAdList[nrOfSeekAds++] = StrSave(buf);
+ if(plot) PlotSingleSeekAd(nrOfSeekAds-1);
+ }
+}
+
+void
+EraseSeekDot (int i)
+{
+ int x = xList[i], y = yList[i], d=squareSize/4, k;
+ DrawSeekBackground(x-squareSize/8, y-squareSize/8, x+squareSize/8+1, y+squareSize/8+1);
+ if(x < hMargin+d) DrawSeekAxis(hMargin, y-squareSize/8, hMargin, y+squareSize/8+1);
+ // now replot every dot that overlapped
+ for(k=0; k<nrOfSeekAds; k++) if(k != i) {
+ int xx = xList[k], yy = yList[k];
+ if(xx <= x+d && xx > x-d && yy <= y+d && yy > y-d)
+ DrawSeekDot(xx, yy, colorList[k]);
+ }
+}
+
void
-read_from_ics(isr, closure, data, count, error)
- InputSourceRef isr;
- VOIDSTAR closure;
- char *data;
- int count;
- int error;
+RemoveSeekAd (int nr)
+{
+ int i;
+ for(i=0; i<nrOfSeekAds; i++) if(seekNrList[i] == nr) {
+ EraseSeekDot(i);
+ if(seekAdList[i]) free(seekAdList[i]);
+ seekAdList[i] = seekAdList[--nrOfSeekAds];
+ seekNrList[i] = seekNrList[nrOfSeekAds];
+ ratingList[i] = ratingList[nrOfSeekAds];
+ colorList[i] = colorList[nrOfSeekAds];
+ tcList[i] = tcList[nrOfSeekAds];
+ xList[i] = xList[nrOfSeekAds];
+ yList[i] = yList[nrOfSeekAds];
+ zList[i] = zList[nrOfSeekAds];
+ seekAdList[nrOfSeekAds] = NULL;
+ break;
+ }
+}
+
+Boolean
+MatchSoughtLine (char *line)
{
-#define BUF_SIZE 8192
+ char handle[MSG_SIZ], rating[MSG_SIZ], type[MSG_SIZ];
+ int nr, base, inc, u=0; char dummy;
+
+ if(sscanf(line, "%d %s %s %d %d rated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
+ sscanf(line, "%d %s %s %s %d %d rated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7 ||
+ (u=1) &&
+ (sscanf(line, "%d %s %s %d %d unrated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
+ sscanf(line, "%d %s %s %s %d %d unrated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7) ) {
+ // match: compact and save the line
+ AddAd(handle, rating, base, inc, u ? 'u' : 'r', type, nr, FALSE);
+ return TRUE;
+ }
+ return FALSE;
+}
+
+int
+DrawSeekGraph ()
+{
+ int i;
+ if(!seekGraphUp) return FALSE;
+ h = BOARD_HEIGHT * (squareSize + lineGap) + lineGap + 2*border;
+ w = BOARD_WIDTH * (squareSize + lineGap) + lineGap + 2*border;
+
+ DrawSeekBackground(0, 0, w, h);
+ DrawSeekAxis(hMargin, h-1-vMargin, w-5, h-1-vMargin);
+ DrawSeekAxis(hMargin, h-1-vMargin, hMargin, 5);
+ for(i=0; i<4000; i+= 100) if(i>=minRating && i<maxRating) {
+ int yy =((double)i - minRating)/(maxRating - minRating)*(h-vMargin-squareSize/8-1) + vMargin;
+ yy = h-1-yy;
+ DrawSeekAxis(hMargin-5, yy, hMargin+5*(i%500==0), yy); // rating ticks
+ if(i%500 == 0) {
+ char buf[MSG_SIZ];
+ snprintf(buf, MSG_SIZ, "%d", i);
+ DrawSeekText(buf, hMargin+squareSize/8+7, yy);
+ }
+ }
+ DrawSeekText("unrated", hMargin+squareSize/8+7, h-1-vMargin-squareSize/4);
+ for(i=1; i<100; i+=(i<10?1:5)) {
+ int xx = (w-hMargin-squareSize/8-7)* log((double)i)/log(95.) + hMargin;
+ DrawSeekAxis(xx, h-1-vMargin, xx, h-6-vMargin-3*(i%10==0)); // TC ticks
+ if(i<=5 || (i>40 ? i%20 : i%10) == 0) {
+ char buf[MSG_SIZ];
+ snprintf(buf, MSG_SIZ, "%d", i);
+ DrawSeekText(buf, xx-2-3*(i>9), h-1-vMargin/2);
+ }
+ }
+ for(i=0; i<nrOfSeekAds; i++) PlotSeekAd(i);
+ return TRUE;
+}
+
+int
+SeekGraphClick (ClickType click, int x, int y, int moving)
+{
+ static int lastDown = 0, displayed = 0, lastSecond;
+ if(y < 0) return FALSE;
+ if(!(appData.seekGraph && appData.icsActive && loggedOn &&
+ (gameMode == BeginningOfGame || gameMode == IcsIdle))) {
+ if(!seekGraphUp) return FALSE;
+ seekGraphUp = FALSE; // seek graph is up when it shouldn't be: take it down
+ DrawPosition(TRUE, NULL);
+ return TRUE;
+ }
+ if(!seekGraphUp) { // initiate cration of seek graph by requesting seek-ad list
+ if(click == Release || moving) return FALSE;
+ nrOfSeekAds = 0;
+ soughtPending = TRUE;
+ SendToICS(ics_prefix);
+ SendToICS("sought\n"); // should this be "sought all"?
+ } else { // issue challenge based on clicked ad
+ int dist = 10000; int i, closest = 0, second = 0;
+ for(i=0; i<nrOfSeekAds; i++) {
+ int d = (x-xList[i])*(x-xList[i]) + (y-yList[i])*(y-yList[i]) + zList[i];
+ if(d < dist) { dist = d; closest = i; }
+ second += (d - zList[i] < 120); // count in-range ads
+ if(click == Press && moving != 1 && zList[i]>0) zList[i] *= 0.8; // age priority
+ }
+ if(dist < 120) {
+ char buf[MSG_SIZ];
+ second = (second > 1);
+ if(displayed != closest || second != lastSecond) {
+ DisplayMessage(second ? "!" : "", seekAdList[closest]);
+ lastSecond = second; displayed = closest;
+ }
+ if(click == Press) {
+ if(moving == 2) zList[closest] = 100; // right-click; push to back on press
+ lastDown = closest;
+ return TRUE;
+ } // on press 'hit', only show info
+ if(moving == 2) return TRUE; // ignore right up-clicks on dot
+ snprintf(buf, MSG_SIZ, "play %d\n", seekNrList[closest]);
+ SendToICS(ics_prefix);
+ SendToICS(buf);
+ return TRUE; // let incoming board of started game pop down the graph
+ } else if(click == Release) { // release 'miss' is ignored
+ zList[lastDown] = 100; // make future selection of the rejected ad more difficult
+ if(moving == 2) { // right up-click
+ nrOfSeekAds = 0; // refresh graph
+ soughtPending = TRUE;
+ SendToICS(ics_prefix);
+ SendToICS("sought\n"); // should this be "sought all"?
+ }
+ return TRUE;
+ } else if(moving) { if(displayed >= 0) DisplayMessage("", ""); displayed = -1; return TRUE; }
+ // press miss or release hit 'pop down' seek graph
+ seekGraphUp = FALSE;
+ DrawPosition(TRUE, NULL);
+ }
+ return TRUE;
+}
+
+void
+read_from_ics (InputSourceRef isr, VOIDSTAR closure, char *data, int count, int error)
+{
+#define BUF_SIZE (16*1024) /* overflowed at 8K with "inchannel 1" on FICS? */
#define STARTED_NONE 0
#define STARTED_MOVES 1
#define STARTED_BOARD 2
#define STARTED_CHATTER 5
#define STARTED_COMMENT 6
#define STARTED_MOVES_NOHIDE 7
-
+
static int started = STARTED_NONE;
static char parse[20000];
static int parse_pos = 0;
static int firstTime = TRUE, intfSet = FALSE;
static ColorClass prevColor = ColorNormal;
static int savingComment = FALSE;
- char str[500];
+ static int cmatch = 0; // continuation sequence match
+ char *bp;
+ char str[MSG_SIZ];
int i, oldi;
int buf_len;
int next_out;
int backup; /* [DM] For zippy color lines */
char *p;
char talker[MSG_SIZ]; // [HGM] chat
- int channel;
+ int channel, collective=0;
+
+ connectionAlive = TRUE; // [HGM] alive: I think, therefore I am...
if (appData.debugMode) {
if (!error) {
if (appData.debugMode) { int f = forwardMostMove;
fprintf(debugFP, "ics input %d, castling = %d %d %d %d %d %d\n", f,
- castlingRights[f][0],castlingRights[f][1],castlingRights[f][2],castlingRights[f][3],castlingRights[f][4],castlingRights[f][5]);
+ boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
+ boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
}
if (count > 0) {
/* If last read ended with a partial line that we couldn't parse,
buf[i] = buf[leftover_start + i];
}
- /* Copy in new characters, removing nulls and \r's */
- buf_len = leftover_len;
- for (i = 0; i < count; i++) {
- if (data[i] != NULLCHAR && data[i] != '\r')
- buf[buf_len++] = data[i];
- if(!appData.noJoin && buf_len >= 5 && buf[buf_len-5]=='\n' && buf[buf_len-4]=='\\' &&
- buf[buf_len-3]==' ' && buf[buf_len-2]==' ' && buf[buf_len-1]==' ') {
- buf_len -= 5; // [HGM] ICS: join continuation line of Lasker 2.2.3 server with previous
- if(buf_len == 0 || buf[buf_len-1] != ' ')
- buf[buf_len++] = ' '; // add space (assumes ICS does not break lines within word)
- }
- }
+ /* copy new characters into the buffer */
+ bp = buf + leftover_len;
+ buf_len=leftover_len;
+ for (i=0; i<count; i++)
+ {
+ // ignore these
+ if (data[i] == '\r')
+ continue;
+
+ // join lines split by ICS?
+ if (!appData.noJoin)
+ {
+ /*
+ Joining just consists of finding matches against the
+ continuation sequence, and discarding that sequence
+ if found instead of copying it. So, until a match
+ fails, there's nothing to do since it might be the
+ complete sequence, and thus, something we don't want
+ copied.
+ */
+ if (data[i] == cont_seq[cmatch])
+ {
+ cmatch++;
+ if (cmatch == strlen(cont_seq))
+ {
+ cmatch = 0; // complete match. just reset the counter
+
+ /*
+ it's possible for the ICS to not include the space
+ at the end of the last word, making our [correct]
+ join operation fuse two separate words. the server
+ does this when the space occurs at the width setting.
+ */
+ if (!buf_len || buf[buf_len-1] != ' ')
+ {
+ *bp++ = ' ';
+ buf_len++;
+ }
+ }
+ continue;
+ }
+ else if (cmatch)
+ {
+ /*
+ match failed, so we have to copy what matched before
+ falling through and copying this character. In reality,
+ this will only ever be just the newline character, but
+ it doesn't hurt to be precise.
+ */
+ strncpy(bp, cont_seq, cmatch);
+ bp += cmatch;
+ buf_len += cmatch;
+ cmatch = 0;
+ }
+ }
+
+ // copy this char
+ *bp++ = data[i];
+ buf_len++;
+ }
buf[buf_len] = NULLCHAR;
- next_out = leftover_len;
+// next_out = leftover_len; // [HGM] should we set this to 0, and not print it in advance?
+ next_out = 0;
leftover_start = 0;
-
+
i = 0;
while (i < buf_len) {
/* Deal with part of the TELNET option negotiation
next_out = i;
continue;
}
-
+
/* OK, this at least will *usually* work */
if (!loggedOn && looking_at(buf, &i, "ics%")) {
loggedOn = TRUE;
}
-
+
if (loggedOn && !intfSet) {
if (ics_type == ICS_ICC) {
- sprintf(str,
+ snprintf(str, MSG_SIZ,
"/set-quietly interface %s\n/set-quietly style 12\n",
programVersion);
- strcat(str, "/set-quietly wrap 0\n");
-
+ if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
+ strcat(str, "/set-2 51 1\n/set seek 1\n");
} else if (ics_type == ICS_CHESSNET) {
- sprintf(str, "/style 12\n");
+ snprintf(str, MSG_SIZ, "/style 12\n");
} else {
- strcpy(str, "alias $ @\n$set interface ");
+ safeStrCpy(str, "alias $ @\n$set interface ", sizeof(str)/sizeof(str[0]));
strcat(str, programVersion);
strcat(str, "\n$iset startpos 1\n$iset ms 1\n");
+ if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
+ strcat(str, "$iset seekremove 1\n$set seek 1\n");
#ifdef WIN32
strcat(str, "$iset nohighlight 1\n");
#endif
- strcat(str, "$iset nowrap 1\n");
strcat(str, "$iset lock 1\n$style 12\n");
}
SendToICS(str);
parse[parse_pos] = NULLCHAR;
if(chattingPartner>=0) {
char mess[MSG_SIZ];
- sprintf(mess, "%s%s", talker, parse);
+ snprintf(mess, MSG_SIZ, "%s%s", talker, parse);
OutputChatMessage(chattingPartner, mess);
+ if(collective == 1) { // broadcasted talk also goes to private chatbox of talker
+ int p;
+ talker[strlen(talker+1)-1] = NULLCHAR; // strip closing delimiter
+ for(p=0; p<MAX_CHAT; p++) if(!StrCaseCmp(talker+1, chatPartner[p])) {
+ snprintf(mess, MSG_SIZ, "%s: %s", chatPartner[chattingPartner], parse);
+ OutputChatMessage(p, mess);
+ break;
+ }
+ }
chattingPartner = -1;
+ if(collective != 3) next_out = i+1; // [HGM] suppress printing in ICS window
+ collective = 0;
} else
if(!suppressKibitz) // [HGM] kibitz
- AppendComment(forwardMostMove, StripHighlight(parse));
+ AppendComment(forwardMostMove, StripHighlight(parse), TRUE);
else { // [HGM kibitz: divert memorized engine kibitz to engine-output window
- int nrDigit = 0, nrAlph = 0, i;
+ int nrDigit = 0, nrAlph = 0, j;
if(parse_pos > MSG_SIZ - 30) // defuse unreasonably long input
{ parse_pos = MSG_SIZ-30; parse[parse_pos - 1] = '\n'; }
parse[parse_pos] = NULLCHAR;
// try to be smart: if it does not look like search info, it should go to
// ICS interaction window after all, not to engine-output window.
- for(i=0; i<parse_pos; i++) { // count letters and digits
- nrDigit += (parse[i] >= '0' && parse[i] <= '9');
- nrAlph += (parse[i] >= 'a' && parse[i] <= 'z');
- nrAlph += (parse[i] >= 'A' && parse[i] <= 'Z');
+ for(j=0; j<parse_pos; j++) { // count letters and digits
+ nrDigit += (parse[j] >= '0' && parse[j] <= '9');
+ nrAlph += (parse[j] >= 'a' && parse[j] <= 'z');
+ nrAlph += (parse[j] >= 'A' && parse[j] <= 'Z');
}
if(nrAlph < 9*nrDigit) { // if more than 10% digit we assume search info
int depth=0; float score;
OutputKibitz(suppressKibitz, parse);
} else {
char tmp[MSG_SIZ];
- sprintf(tmp, _("your opponent kibitzes: %s"), parse);
+ if(gameMode == IcsObserving) // restore original ICS messages
+ /* TRANSLATORS: to 'kibitz' is to send a message to all players and the game observers */
+ snprintf(tmp, MSG_SIZ, "%s kibitzes: %s", star_match[0], parse);
+ else
+ /* TRANSLATORS: to 'kibitz' is to send a message to all players and the game observers */
+ snprintf(tmp, MSG_SIZ, _("your opponent kibitzes: %s"), parse);
SendToPlayer(tmp, strlen(tmp));
}
+ next_out = i+1; // [HGM] suppress printing in ICS window
}
started = STARTED_NONE;
} else {
- /* Don't match patterns against characters in chatter */
+ /* Don't match patterns against characters in comment */
i++;
continue;
}
continue;
}
started = STARTED_NONE;
+ if(suppressKibitz) next_out = i+1;
}
/* Kludge to deal with rcmd protocol */
(looking_at(buf, &i, "\"*\" is *a registered name") ||
looking_at(buf, &i, "Logging you in as \"*\"") ||
looking_at(buf, &i, "will be \"*\""))) {
- strcpy(ics_handle, star_match[0]);
+ safeStrCpy(ics_handle, star_match[0], sizeof(ics_handle)/sizeof(ics_handle[0]));
continue;
}
continue;
}
+ oldi = i;
+ // [HGM] seekgraph: recognize sought lines and end-of-sought message
+ if(appData.seekGraph) {
+ if(soughtPending && MatchSoughtLine(buf+i)) {
+ i = strstr(buf+i, "rated") - buf;
+ if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
+ next_out = leftover_start = i;
+ started = STARTED_CHATTER;
+ suppressKibitz = TRUE;
+ continue;
+ }
+ if((gameMode == IcsIdle || gameMode == BeginningOfGame)
+ && looking_at(buf, &i, "* ads displayed")) {
+ soughtPending = FALSE;
+ seekGraphUp = TRUE;
+ DrawSeekGraph();
+ continue;
+ }
+ if(appData.autoRefresh) {
+ if(looking_at(buf, &i, "* (*) seeking * * * * *\"play *\" to respond)\n")) {
+ int s = (ics_type == ICS_ICC); // ICC format differs
+ if(seekGraphUp)
+ AddAd(star_match[0], star_match[1], atoi(star_match[2+s]), atoi(star_match[3+s]),
+ star_match[4+s][0], star_match[5-3*s], atoi(star_match[7]), TRUE);
+ looking_at(buf, &i, "*% "); // eat prompt
+ if(oldi > 0 && buf[oldi-1] == '\n') oldi--; // suppress preceding LF, if any
+ if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
+ next_out = i; // suppress
+ continue;
+ }
+ if(looking_at(buf, &i, "\nAds removed: *\n") || looking_at(buf, &i, "\031(51 * *\031)")) {
+ char *p = star_match[0];
+ while(*p) {
+ if(seekGraphUp) RemoveSeekAd(atoi(p));
+ while(*p && *p++ != ' '); // next
+ }
+ looking_at(buf, &i, "*% "); // eat prompt
+ if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
+ next_out = i;
+ continue;
+ }
+ }
+ }
+
/* skip formula vars */
if (started == STARTED_NONE &&
buf[i] == 'f' && isdigit(buf[i+1]) && buf[i+2] == ':') {
continue;
}
- oldi = i;
// [HGM] kibitz: try to recognize opponent engine-score kibitzes, to divert them to engine-output window
- if (appData.autoKibitz && started == STARTED_NONE &&
+ if (appData.autoKibitz && started == STARTED_NONE &&
!appData.icsEngineAnalyze && // [HGM] [DM] ICS analyze
(gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack || gameMode == IcsObserving)) {
- if(looking_at(buf, &i, "* kibitzes: ") &&
- (StrStr(star_match[0], gameInfo.white) == star_match[0] ||
+ if((looking_at(buf, &i, "\n* kibitzes: ") || looking_at(buf, &i, "\n* whispers: ") ||
+ looking_at(buf, &i, "* kibitzes: ") || looking_at(buf, &i, "* whispers: ")) &&
+ (StrStr(star_match[0], gameInfo.white) == star_match[0] ||
StrStr(star_match[0], gameInfo.black) == star_match[0] )) { // kibitz of self or opponent
suppressKibitz = TRUE;
+ if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
+ next_out = i;
if((StrStr(star_match[0], gameInfo.white) == star_match[0]
&& (gameMode == IcsPlayingWhite)) ||
(StrStr(star_match[0], gameInfo.black) == star_match[0]
savingComment = TRUE;
suppressKibitz = gameMode != IcsObserving ? 2 :
(StrStr(star_match[0], gameInfo.white) == NULL) + 1;
- }
+ }
continue;
} else
- if(looking_at(buf, &i, "kibitzed to")) { // suppress the acknowledgements of our own autoKibitz
- started = STARTED_CHATTER;
- suppressKibitz = TRUE;
+ if((looking_at(buf, &i, "\nkibitzed to *\n") || looking_at(buf, &i, "kibitzed to *\n") ||
+ looking_at(buf, &i, "\n(kibitzed to *\n") || looking_at(buf, &i, "(kibitzed to *\n"))
+ && atoi(star_match[0])) {
+ // suppress the acknowledgements of our own autoKibitz
+ char *p;
+ if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
+ if(p = strchr(star_match[0], ' ')) p[1] = NULLCHAR; // clip off "players)" on FICS
+ SendToPlayer(star_match[0], strlen(star_match[0]));
+ if(looking_at(buf, &i, "*% ")) // eat prompt
+ suppressKibitz = FALSE;
+ next_out = i;
+ continue;
}
} // [HGM] kibitz: end of patch
-//if(appData.debugMode) fprintf(debugFP, "hunt for tell, buf = %s\n", buf+i);
+ if(looking_at(buf, &i, "* rating adjustment: * --> *\n")) continue;
// [HGM] chat: intercept tells by users for which we have an open chat window
channel = -1;
- if(started == STARTED_NONE && (looking_at(buf, &i, "* tells you:") || looking_at(buf, &i, "* says:") ||
+ if(started == STARTED_NONE && (looking_at(buf, &i, "* tells you:") || looking_at(buf, &i, "* says:") ||
looking_at(buf, &i, "* whispers:") ||
+ looking_at(buf, &i, "* kibitzes:") ||
+ looking_at(buf, &i, "* shouts:") ||
+ looking_at(buf, &i, "* c-shouts:") ||
+ looking_at(buf, &i, "--> * ") ||
looking_at(buf, &i, "*(*):") && (sscanf(star_match[1], "%d", &channel),1) ||
- looking_at(buf, &i, "*(*)(*):") && sscanf(star_match[2], "%d", &channel) == 1 )) {
+ looking_at(buf, &i, "*(*)(*):") && (sscanf(star_match[2], "%d", &channel),1) ||
+ looking_at(buf, &i, "*(*)(*)(*):") && (sscanf(star_match[3], "%d", &channel),1) ||
+ looking_at(buf, &i, "*(*)(*)(*)(*):") && sscanf(star_match[4], "%d", &channel) == 1 )) {
int p;
sscanf(star_match[0], "%[^(]", talker+1); // strip (C) or (U) off ICS handle
- chattingPartner = -1;
+ chattingPartner = -1; collective = 0;
if(channel >= 0) // channel broadcast; look if there is a chatbox for this channel
for(p=0; p<MAX_CHAT; p++) {
- if(channel == atoi(chatPartner[p])) {
- talker[0] = '['; strcat(talker, "]");
+ collective = 1;
+ if(chatPartner[p][0] >= '0' && chatPartner[p][0] <= '9' && channel == atoi(chatPartner[p])) {
+ talker[0] = '['; strcat(talker, "] ");
+ Colorize((channel == 1 ? ColorChannel1 : ColorChannel), FALSE);
chattingPartner = p; break;
}
} else
+ if(buf[i-3] == 'e') // kibitz; look if there is a KIBITZ chatbox
+ for(p=0; p<MAX_CHAT; p++) {
+ collective = 1;
+ if(!strcmp("kibitzes", chatPartner[p])) {
+ talker[0] = '['; strcat(talker, "] ");
+ chattingPartner = p; break;
+ }
+ } else
if(buf[i-3] == 'r') // whisper; look if there is a WHISPER chatbox
for(p=0; p<MAX_CHAT; p++) {
- if(!strcmp("WHISPER", chatPartner[p])) {
- talker[0] = '['; strcat(talker, "]");
+ collective = 1;
+ if(!strcmp("whispers", chatPartner[p])) {
+ talker[0] = '['; strcat(talker, "] ");
+ chattingPartner = p; break;
+ }
+ } else
+ if(buf[i-3] == 't' || buf[oldi+2] == '>') {// shout, c-shout or it; look if there is a 'shouts' chatbox
+ if(buf[i-8] == '-' && buf[i-3] == 't')
+ for(p=0; p<MAX_CHAT; p++) { // c-shout; check if dedicatesd c-shout box exists
+ collective = 1;
+ if(!strcmp("c-shouts", chatPartner[p])) {
+ talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE);
chattingPartner = p; break;
}
+ }
+ if(chattingPartner < 0)
+ for(p=0; p<MAX_CHAT; p++) {
+ collective = 1;
+ if(!strcmp("shouts", chatPartner[p])) {
+ if(buf[oldi+2] == '>') { talker[0] = '<'; strcat(talker, "> "); Colorize(ColorShout, FALSE); }
+ else if(buf[i-8] == '-') { talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE); }
+ else { talker[0] = '['; strcat(talker, "] "); Colorize(ColorShout, FALSE); }
+ chattingPartner = p; break;
+ }
+ }
}
if(chattingPartner<0) // if not, look if there is a chatbox for this indivdual
for(p=0; p<MAX_CHAT; p++) if(!StrCaseCmp(talker+1, chatPartner[p])) {
talker[0] = 0;
+ Colorize(ColorTell, FALSE);
+ if(collective) safeStrCpy(talker, "broadcasts: ", MSG_SIZ);
+ collective |= 2;
chattingPartner = p; break;
}
- if(chattingPartner<0) i = oldi; else {
+ if(chattingPartner<0) i = oldi, safeStrCpy(lastTalker, talker+1, MSG_SIZ); else {
+ Colorize(curColor, TRUE); // undo the bogus colorations we just made to trigger the souds
started = STARTED_COMMENT;
parse_pos = 0; parse[0] = NULLCHAR;
- savingComment = TRUE;
- suppressKibitz = TRUE;
+ savingComment = 3 + chattingPartner; // counts as TRUE
+ if(collective == 3) i = oldi; else {
+ suppressKibitz = TRUE;
+ if(oldi > 0 && buf[oldi-1] == '\n') oldi--;
+ if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
+ continue;
+ }
}
} // [HGM] chat: end of patch
+ backup = i;
if (appData.zippyTalk || appData.zippyPlay) {
/* [DM] Backup address for color zippy lines */
- backup = i;
#if ZIPPY
- #ifdef WIN32
if (loggedOn == TRUE)
if (ZippyControl(buf, &backup) || ZippyConverse(buf, &backup) ||
(appData.zippyPlay && ZippyMatch(buf, &backup)));
- #else
- if (ZippyControl(buf, &i) ||
- ZippyConverse(buf, &i) ||
- (appData.zippyPlay && ZippyMatch(buf, &i))) {
- loggedOn = TRUE;
- if (!appData.colorize) continue;
- }
- #endif
#endif
} // [DM] 'else { ' deleted
if (
parse[parse_pos] = NULLCHAR;
started = STARTED_COMMENT;
savingComment = TRUE;
- } else {
+ } else if(collective != 3) {
started = STARTED_CHATTER;
savingComment = FALSE;
}
continue;
}
+ if(i < backup) { i = backup; continue; } // [HGM] for if ZippyControl matches, but the colorie code doesn't
+
if (looking_at(buf, &i, "\\ ")) {
if (prevColor != ColorNormal) {
if (oldi > next_out) {
memcpy(parse, &buf[oldi], parse_pos);
parse[parse_pos] = NULLCHAR;
started = STARTED_COMMENT;
+ if(savingComment >= 3) // [HGM] chat: continuation of line for chat box
+ chattingPartner = savingComment - 3; // kludge to remember the box
} else {
started = STARTED_CHATTER;
}
SendToICS("refresh\n");
continue;
}
-
- if (!have_sent_ICS_logon && looking_at(buf, &i, "login:")) {
- ICSInitScript();
- have_sent_ICS_logon = 1;
+
+ if (looking_at(buf, &i, "login:")) {
+ if (!have_sent_ICS_logon) {
+ if(ICSInitScript())
+ have_sent_ICS_logon = 1;
+ else // no init script was found
+ have_sent_ICS_logon = (appData.autoCreateLogon ? 2 : 1); // flag that we should capture username + password
+ } else { // we have sent (or created) the InitScript, but apparently the ICS rejected it
+ have_sent_ICS_logon = (appData.autoCreateLogon ? 2 : 1); // request creation of a new script
+ }
continue;
}
-
- if (ics_getting_history != H_GETTING_MOVES /*smpos kludge*/ &&
+
+ if (ics_getting_history != H_GETTING_MOVES /*smpos kludge*/ &&
(looking_at(buf, &i, "\n<12> ") ||
looking_at(buf, &i, "<12> "))) {
loggedOn = TRUE;
gameInfo.whiteRating = string_to_rating(star_match[1]);
gameInfo.blackRating = string_to_rating(star_match[3]);
if (appData.debugMode)
- fprintf(debugFP, _("Ratings from header: W %d, B %d\n"),
+ fprintf(debugFP, "Ratings from header: W %d, B %d\n",
gameInfo.whiteRating, gameInfo.blackRating);
}
continue;
/* Header for a move list -- second line */
/* Initial board will follow if this is a wild game */
if (gameInfo.event != NULL) free(gameInfo.event);
- sprintf(str, "ICS %s %s match", star_match[0], star_match[1]);
+ snprintf(str, MSG_SIZ, "ICS %s %s match", star_match[0], star_match[1]);
gameInfo.event = StrSave(str);
/* [HGM] we switched variant. Translate boards if needed. */
VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event));
break;
}
continue;
- }
-
+ }
+
if (looking_at(buf, &i, "% ") ||
((started == STARTED_MOVES || started == STARTED_MOVES_NOHIDE)
&& looking_at(buf, &i, "}*"))) { char *bookHit = NULL; // [HGM] book
+ if(soughtPending && nrOfSeekAds) { // [HGM] seekgraph: on ICC sought-list has no termination line
+ soughtPending = FALSE;
+ seekGraphUp = TRUE;
+ DrawSeekGraph();
+ }
+ if(suppressKibitz) next_out = i;
savingComment = FALSE;
+ suppressKibitz = 0;
switch (started) {
case STARTED_MOVES:
case STARTED_MOVES_NOHIDE:
if (WhiteOnMove(forwardMostMove)) {
if (first.sendTime) {
if (first.useColors) {
- SendToProgram("black\n", &first);
+ SendToProgram("black\n", &first);
}
SendTimeRemaining(&first, TRUE);
}
firstMove = TRUE;
}
}
- }
+ }
}
#endif
if (gameMode == IcsObserving && ics_gamenum == -1) {
currentMove = forwardMostMove;
ClearHighlights();/*!!could figure this out*/
flipView = appData.flipView;
- DrawPosition(FALSE, boards[currentMove]);
+ DrawPosition(TRUE, boards[currentMove]);
DisplayBothClocks();
- sprintf(str, "%s vs. %s",
- gameInfo.white, gameInfo.black);
+ snprintf(str, MSG_SIZ, "%s %s %s",
+ gameInfo.white, _("vs."), gameInfo.black);
DisplayTitle(str);
gameMode = IcsIdle;
} else {
if(bookHit) { // [HGM] book: simulate book reply
static char bookMove[MSG_SIZ]; // a bit generous?
- programStats.nodes = programStats.depth = programStats.time =
+ programStats.nodes = programStats.depth = programStats.time =
programStats.score = programStats.got_only_move = 0;
sprintf(programStats.movelist, "%s (xbook)", bookHit);
- strcpy(bookMove, "move ");
+ safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
strcat(bookMove, bookHit);
HandleMachineMove(bookMove, &first);
}
continue;
}
-
+
if ((started == STARTED_MOVES || started == STARTED_BOARD ||
started == STARTED_HOLDINGS ||
started == STARTED_MOVES_NOHIDE) && i >= leftover_len) {
/* Accumulate characters in move list or board */
parse[parse_pos++] = buf[i];
}
-
+
/* Start of game messages. Mostly we detect start of game
when the first board image arrives. On some versions
of the ICS, though, we need to do a "refresh" after starting
} else {
player = star_match[2];
}
- sprintf(str, "%sobserve %s\n",
+ snprintf(str, MSG_SIZ, "%sobserve %s\n",
ics_prefix, StripHighlightAndTitle(player));
SendToICS(str);
/* Save ratings from notify string */
- strcpy(player1Name, star_match[0]);
+ safeStrCpy(player1Name, star_match[0], sizeof(player1Name)/sizeof(player1Name[0]));
player1Rating = string_to_rating(star_match[1]);
- strcpy(player2Name, star_match[2]);
+ safeStrCpy(player2Name, star_match[2], sizeof(player2Name)/sizeof(player2Name[0]));
player2Rating = string_to_rating(star_match[3]);
if (appData.debugMode)
- fprintf(debugFP,
+ fprintf(debugFP,
"Ratings from 'Game notification:' %s %d, %s %d\n",
player1Name, player1Rating,
player2Name, player2Rating);
SendToICS("refresh\n");
}
continue;
- }
-
+ }
+
/* Error messages */
// if (ics_user_moved) {
if (1) { // [HGM] old way ignored error after move type in; ics_user_moved is not set then!
looking_at(buf, &i, "It is not your move")) {
/* Illegal move */
if (ics_user_moved && forwardMostMove > backwardMostMove) { // only backup if we already moved
- currentMove = --forwardMostMove;
+ currentMove = forwardMostMove-1;
DisplayMove(currentMove - 1); /* before DMError */
DrawPosition(FALSE, boards[currentMove]);
- SwitchClocks();
+ SwitchClocks(forwardMostMove-1); // [HGM] race
DisplayBothClocks();
}
DisplayMoveError(_("Illegal move (rejected by ICS)")); // [HGM] but always relay error msg
2 empty, white, or black (IGNORED)
3 player 2 name (not necessarily black)
4 player 2 rating
-
+
The names/ratings are sorted out when the game
actually starts (below).
*/
- strcpy(player1Name, StripHighlightAndTitle(star_match[0]));
- player1Rating = string_to_rating(star_match[1]);
- strcpy(player2Name, StripHighlightAndTitle(star_match[3]));
- player2Rating = string_to_rating(star_match[4]);
+ safeStrCpy(player1Name, StripHighlightAndTitle(star_match[0]), sizeof(player1Name)/sizeof(player1Name[0]));
+ player1Rating = string_to_rating(star_match[1]);
+ safeStrCpy(player2Name, StripHighlightAndTitle(star_match[3]), sizeof(player2Name)/sizeof(player2Name[0]));
+ player2Rating = string_to_rating(star_match[4]);
if (appData.debugMode)
- fprintf(debugFP,
+ fprintf(debugFP,
"Ratings from 'Creating:' %s %d, %s %d\n",
player1Name, player1Rating,
player2Name, player2Rating);
continue;
}
-
+
/* Improved generic start/end-of-game messages */
if ((tkind=0, looking_at(buf, &i, "{Game * (* vs. *) *}*")) ||
(tkind=1, looking_at(buf, &i, "{Game * (*(*) vs. *(*)) *}*"))){
/* [4] is " *" or empty (don't care). */
int gamenum = atoi(star_match[0]);
char *whitename, *blackname, *why, *endtoken;
- ChessMove endtype = (ChessMove) 0;
+ ChessMove endtype = EndOfFile;
if (tkind == 0) {
whitename = star_match[1];
if (strncmp(why, "Creating ", 9) == 0 ||
strncmp(why, "Continuing ", 11) == 0) {
gs_gamenum = gamenum;
- strcpy(gs_kind, strchr(why, ' ') + 1);
+ safeStrCpy(gs_kind, strchr(why, ' ') + 1,sizeof(gs_kind)/sizeof(gs_kind[0]));
+ if(ics_gamenum == -1) // [HGM] only if we are not already involved in a game (because gin=1 sends us such messages)
+ VariantSwitch(boards[currentMove], StringToVariant(gs_kind)); // [HGM] variantswitch: even before we get first board
#if ZIPPY
if (appData.zippyPlay) {
ZippyGameStart(whitename, blackname);
}
#endif /*ZIPPY*/
+ partnerBoardValid = FALSE; // [HGM] bughouse
continue;
}
#if ZIPPY
if (appData.zippyPlay && first.initDone) {
ZippyGameEnd(endtype, why);
- if (first.pr == NULL) {
+ if (first.pr == NoProc) {
/* Start the next process early so that we'll
be ready for the next challenge */
StartChessProgram(&first);
Reset(TRUE, TRUE);
}
#endif /*ZIPPY*/
+ if(appData.bgObserve && partnerBoardValid) DrawPosition(TRUE, partnerBoard);
continue;
}
if (currentMove == 0 &&
gameMode == IcsPlayingWhite &&
appData.premoveWhite) {
- sprintf(str, "%s%s\n", ics_prefix,
- appData.premoveWhiteText);
+ snprintf(str, MSG_SIZ, "%s\n", appData.premoveWhiteText);
if (appData.debugMode)
fprintf(debugFP, "Sending premove:\n");
SendToICS(str);
} else if (currentMove == 1 &&
gameMode == IcsPlayingBlack &&
appData.premoveBlack) {
- sprintf(str, "%s%s\n", ics_prefix,
- appData.premoveBlackText);
+ snprintf(str, MSG_SIZ, "%s\n", appData.premoveBlackText);
if (appData.debugMode)
fprintf(debugFP, "Sending premove:\n");
SendToICS(str);
ClearPremoveHighlights();
if (appData.debugMode)
fprintf(debugFP, "Sending premove:\n");
- UserMoveEvent(premoveFromX, premoveFromY,
- premoveToX, premoveToY,
+ UserMoveEvent(premoveFromX, premoveFromY,
+ premoveToX, premoveToY,
premovePromoChar);
}
}
/* Usually suppress following prompt */
if (!(forwardMostMove == 0 && gameMode == IcsExamining)) {
+ while(looking_at(buf, &i, "\n")); // [HGM] skip empty lines
if (looking_at(buf, &i, "*% ")) {
savingComment = FALSE;
+ suppressKibitz = 0;
}
}
next_out = i;
if (appData.debugMode)
fprintf(debugFP, "Parsing holdings: %s, currentMove = %d\n",
parse, currentMove);
- if (sscanf(parse, " game %d", &gamenum) == 1 &&
- gamenum == ics_gamenum) {
+ if (sscanf(parse, " game %d", &gamenum) == 1) {
+ if(gamenum == ics_gamenum) { // [HGM] bughouse: old code if part of foreground game
if (gameInfo.variant == VariantNormal) {
/* [HGM] We seem to switch variant during a game!
* Presumably no holdings were displayed, so we have
* to move the position two files to the right to
* create room for them!
*/
- VariantSwitch(boards[currentMove], VariantCrazyhouse); /* temp guess */
+ VariantClass newVariant;
+ switch(gameInfo.boardWidth) { // base guess on board width
+ case 9: newVariant = VariantShogi; break;
+ case 10: newVariant = VariantGreat; break;
+ default: newVariant = VariantCrazyhouse; break;
+ }
+ VariantSwitch(boards[currentMove], newVariant); /* temp guess */
/* Get a move list just to see the header, which
will tell us whether this is really bug or zh */
if (ics_getting_history == H_FALSE) {
ics_getting_history = H_REQUESTED;
- sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
+ snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
SendToICS(str);
}
}
white_holding[strlen(white_holding)-1] = NULLCHAR;
black_holding[strlen(black_holding)-1] = NULLCHAR;
/* [HGM] copy holdings to board holdings area */
- CopyHoldings(boards[currentMove], white_holding, WhitePawn);
- CopyHoldings(boards[currentMove], black_holding, BlackPawn);
+ CopyHoldings(boards[forwardMostMove], white_holding, WhitePawn);
+ CopyHoldings(boards[forwardMostMove], black_holding, BlackPawn);
+ boards[forwardMostMove][HOLDINGS_SET] = 1; // flag holdings as set
#if ZIPPY
if (appData.zippyPlay && first.initDone) {
ZippyHoldings(white_holding, black_holding,
char wh[16], bh[16];
PackHolding(wh, white_holding);
PackHolding(bh, black_holding);
- sprintf(str, "[%s-%s] %s-%s", wh, bh,
+ snprintf(str, MSG_SIZ, "[%s-%s] %s-%s", wh, bh,
gameInfo.white, gameInfo.black);
} else {
- sprintf(str, "%s [%s] vs. %s [%s]",
- gameInfo.white, white_holding,
+ snprintf(str, MSG_SIZ, "%s [%s] %s %s [%s]",
+ gameInfo.white, white_holding, _("vs."),
gameInfo.black, black_holding);
}
-
+ if(!partnerUp) // [HGM] bughouse: when peeking at partner game we already know what he captured...
DrawPosition(FALSE, boards[currentMove]);
DisplayTitle(str);
+ } else if(appData.bgObserve) { // [HGM] bughouse: holdings of other game => background
+ sscanf(parse, "game %d white [%s black [%s <- %s",
+ &gamenum, white_holding, black_holding,
+ new_piece);
+ white_holding[strlen(white_holding)-1] = NULLCHAR;
+ black_holding[strlen(black_holding)-1] = NULLCHAR;
+ /* [HGM] copy holdings to partner-board holdings area */
+ CopyHoldings(partnerBoard, white_holding, WhitePawn);
+ CopyHoldings(partnerBoard, black_holding, BlackPawn);
+ if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual: always draw
+ if(partnerUp) DrawPosition(FALSE, partnerBoard);
+ if(twoBoards) { partnerUp = 0; flipView = !flipView; }
+ }
}
/* Suppress following prompt */
if (looking_at(buf, &i, "*% ")) {
+ if(strchr(star_match[0], 7)) SendToPlayer("\007", 1); // Bell(); // FICS fuses bell for next board with prompt in zh captures
savingComment = FALSE;
+ suppressKibitz = 0;
}
next_out = i;
}
i++; /* skip unparsed character and loop back */
}
-
- if (started != STARTED_MOVES && started != STARTED_BOARD && !suppressKibitz && // [HGM] kibitz suppress printing in ICS interaction window
- started != STARTED_HOLDINGS && i > next_out) {
- SendToPlayer(&buf[next_out], i - next_out);
+
+ if (started != STARTED_MOVES && started != STARTED_BOARD && !suppressKibitz && // [HGM] kibitz
+// started != STARTED_HOLDINGS && i > next_out) { // [HGM] should we compare to leftover_start in stead of i?
+// SendToPlayer(&buf[next_out], i - next_out);
+ started != STARTED_HOLDINGS && leftover_start > next_out) {
+ SendToPlayer(&buf[next_out], leftover_start - next_out);
next_out = i;
}
- suppressKibitz = FALSE; // [HGM] kibitz: has done its duty in if-statement above
-
+
leftover_len = buf_len - leftover_start;
/* if buffer ends with something we couldn't parse,
reparse it after appending the next read */
-
+
} else if (count == 0) {
RemoveInputSource(isr);
DisplayFatalError(_("Connection closed by ICS"), 0, 0);
/* Board style 12 looks like this:
-
+
<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
-
+
* The "<12> " is stripped before it gets to this routine. The two
* trailing 0's (flip state and clock ticking) are later addition, and
* some chess servers may not have them, or may have only the first.
- * Additional trailing fields may be added in the future.
+ * Additional trailing fields may be added in the future.
*/
#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"
#define RELATION_STARTING_POSITION -4 /* FICS only */
void
-ParseBoard12(string)
- char *string;
-{
+ParseBoard12 (char *string)
+{
+#if ZIPPY
+ int i, takeback;
+ char *bookHit = NULL; // [HGM] book
+#endif
GameMode newGameMode;
- int gamenum, newGame, newMove, relation, basetime, increment, ics_flip = 0, i;
- int j, k, n, moveNum, white_stren, black_stren, white_time, black_time, takeback;
+ int gamenum, newGame, newMove, relation, basetime, increment, ics_flip = 0;
+ int j, k, n, moveNum, white_stren, black_stren, white_time, black_time;
int double_push, castle_ws, castle_wl, castle_bs, castle_bl, irrev_count;
char to_play, board_chars[200];
- char move_str[500], str[500], elapsed_time[500];
+ char move_str[MSG_SIZ], str[MSG_SIZ], elapsed_time[MSG_SIZ];
char black[32], white[32];
Board board;
int prevMove = currentMove;
int fromX, fromY, toX, toY;
char promoChar;
int ranks=1, files=0; /* [HGM] ICS80: allow variable board size */
- char *bookHit = NULL; // [HGM] book
+ Boolean weird = FALSE, reqFlag = FALSE;
fromX = fromY = toX = toY = -1;
-
+
newGame = FALSE;
if (appData.debugMode)
- fprintf(debugFP, _("Parsing board: %s\n"), string);
+ fprintf(debugFP, "Parsing board: %s\n", string);
move_str[0] = NULLCHAR;
elapsed_time[0] = NULLCHAR;
while(i < 199 && (string[i] != ' ' || string[i+2] != ' ')) {
if(string[i] == ' ') { ranks++; files = 0; }
else files++;
+ if(!strchr(" -pnbrqkPNBRQK" , string[i])) weird = TRUE; // test for fairies
i++;
}
for(j = 0; j <i; j++) board_chars[j] = string[j];
&ticking);
if (n < 21) {
- snprintf(str, sizeof(str), _("Failed to parse board string:\n\"%s\""), string);
+ snprintf(str, MSG_SIZ, _("Failed to parse board string:\n\"%s\""), string);
DisplayError(str, 0);
return;
}
/* Convert the move number to internal form */
moveNum = (moveNum - 1) * 2;
if (to_play == 'B') moveNum++;
- if (moveNum >= MAX_MOVES) {
+ if (moveNum > framePtr) { // [HGM] vari: do not run into saved variations
DisplayFatalError(_("Game too long; increase MAX_MOVES and recompile"),
0, 1);
return;
}
-
+
switch (relation) {
case RELATION_OBSERVING_PLAYED:
case RELATION_OBSERVING_STATIC:
newGameMode =
((relation == RELATION_PLAYING_MYMOVE) == (to_play == 'W')) ?
IcsPlayingWhite : IcsPlayingBlack;
+ soughtPending =FALSE; // [HGM] seekgraph: solve race condition
break;
case RELATION_EXAMINING:
newGameMode = IcsExamining;
case RELATION_ISOLATED_BOARD:
default:
/* Just display this board. If user was doing something else,
- we will forget about it until the next board comes. */
+ we will forget about it until the next board comes. */
newGameMode = IcsIdle;
break;
case RELATION_STARTING_POSITION:
newGameMode = gameMode;
break;
}
-
+
+ if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
+ gameMode == IcsObserving && appData.dualBoard) // also allow use of second board for observing two games
+ && newGameMode == IcsObserving && gamenum != ics_gamenum && appData.bgObserve) {
+ // [HGM] bughouse: don't act on alien boards while we play. Just parse the board and save it */
+ int fac = strchr(elapsed_time, '.') ? 1 : 1000;
+ static int lastBgGame = -1;
+ char *toSqr;
+ for (k = 0; k < ranks; k++) {
+ for (j = 0; j < files; j++)
+ board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
+ if(gameInfo.holdingsWidth > 1) {
+ board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
+ board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
+ }
+ }
+ CopyBoard(partnerBoard, board);
+ if(toSqr = strchr(str, '/')) { // extract highlights from long move
+ partnerBoard[EP_STATUS-3] = toSqr[1] - AAA; // kludge: hide highlighting info in board
+ partnerBoard[EP_STATUS-4] = toSqr[2] - ONE;
+ } else partnerBoard[EP_STATUS-4] = partnerBoard[EP_STATUS-3] = -1;
+ if(toSqr = strchr(str, '-')) {
+ partnerBoard[EP_STATUS-1] = toSqr[1] - AAA;
+ partnerBoard[EP_STATUS-2] = toSqr[2] - ONE;
+ } else partnerBoard[EP_STATUS-1] = partnerBoard[EP_STATUS-2] = -1;
+ if(appData.dualBoard && !twoBoards) { twoBoards = 1; InitDrawingSizes(-2,0); }
+ if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual
+ if(partnerUp) DrawPosition(FALSE, partnerBoard);
+ if(twoBoards) {
+ DisplayWhiteClock(white_time*fac, to_play == 'W');
+ DisplayBlackClock(black_time*fac, to_play != 'W');
+ activePartner = to_play;
+ if(gamenum != lastBgGame) {
+ char buf[MSG_SIZ];
+ snprintf(buf, MSG_SIZ, "%s %s %s", white, _("vs."), black);
+ DisplayTitle(buf);
+ }
+ lastBgGame = gamenum;
+ activePartnerTime = to_play == 'W' ? white_time*fac : black_time*fac;
+ partnerUp = 0; flipView = !flipView; } // [HGM] dual
+ snprintf(partnerStatus, MSG_SIZ,"W: %d:%02d B: %d:%02d (%d-%d) %c", white_time*fac/60000, (white_time*fac%60000)/1000,
+ (black_time*fac/60000), (black_time*fac%60000)/1000, white_stren, black_stren, to_play);
+ if(!twoBoards) DisplayMessage(partnerStatus, "");
+ partnerBoardValid = TRUE;
+ return;
+ }
+
+ if(appData.dualBoard && appData.bgObserve) {
+ if((newGameMode == IcsPlayingWhite || newGameMode == IcsPlayingBlack) && moveNum == 1)
+ SendToICS(ics_prefix), SendToICS("pobserve\n");
+ else if(newGameMode == IcsObserving && (gameMode == BeginningOfGame || gameMode == IcsIdle)) {
+ char buf[MSG_SIZ];
+ snprintf(buf, MSG_SIZ, "%spobserve %s\n", ics_prefix, white);
+ SendToICS(buf);
+ }
+ }
+
/* Modify behavior for initial board display on move listing
of wild games.
*/
ics_getting_history = H_FALSE;
return;
}
-
+
+ if (gameInfo.boardHeight != ranks || gameInfo.boardWidth != files ||
+ move_str[1] == '@' && !gameInfo.holdingsWidth ||
+ weird && (int)gameInfo.variant < (int)VariantShogi) {
+ /* [HGM] We seem to have switched variant unexpectedly
+ * Try to guess new variant from board size
+ */
+ VariantClass newVariant = VariantFairy; // if 8x8, but fairies present
+ if(ranks == 8 && files == 10) newVariant = VariantCapablanca; else
+ if(ranks == 10 && files == 9) newVariant = VariantXiangqi; else
+ if(ranks == 8 && files == 12) newVariant = VariantCourier; else
+ if(ranks == 9 && files == 9) newVariant = VariantShogi; else
+ if(ranks == 10 && files == 10) newVariant = VariantGrand; else
+ if(!weird) newVariant = move_str[1] == '@' ? VariantCrazyhouse : VariantNormal;
+ VariantSwitch(boards[currentMove], newVariant); /* temp guess */
+ /* Get a move list just to see the header, which
+ will tell us whether this is really bug or zh */
+ if (ics_getting_history == H_FALSE) {
+ ics_getting_history = H_REQUESTED; reqFlag = TRUE;
+ snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
+ SendToICS(str);
+ }
+ }
+
/* Take action if this is the first board of a new game, or of a
different game than is currently being displayed. */
if (gamenum != ics_gamenum || newGameMode != gameMode ||
relation == RELATION_ISOLATED_BOARD) {
-
+
/* Forget the old game and get the history (if any) of the new one */
if (gameMode != BeginningOfGame) {
- Reset(FALSE, TRUE);
+ Reset(TRUE, TRUE);
}
newGame = TRUE;
if (appData.autoRaiseBoard) BoardToTop();
prevMove = -3;
if (gamenum == -1) {
newGameMode = IcsIdle;
- } else if (moveNum > 0 && newGameMode != IcsIdle &&
- appData.getMoveList) {
+ } else if ((moveNum > 0 || newGameMode == IcsObserving) && newGameMode != IcsIdle &&
+ appData.getMoveList && !reqFlag) {
/* Need to get game history */
ics_getting_history = H_REQUESTED;
- sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
+ snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
SendToICS(str);
}
-
+
/* Initially flip the board to have black on the bottom if playing
black or if the ICS flip flag is set, but let the user change
it with the Flip View button. */
- flipView = appData.autoFlipView ?
+ flipView = appData.autoFlipView ?
(newGameMode == IcsPlayingBlack) || ics_flip :
appData.flipView;
-
+
/* Done with values from previous mode; copy in new ones */
gameMode = newGameMode;
ModeHighlight();
if (gamenum == gs_gamenum) {
int klen = strlen(gs_kind);
if (gs_kind[klen - 1] == '.') gs_kind[klen - 1] = NULLCHAR;
- sprintf(str, "ICS %s", gs_kind);
+ snprintf(str, MSG_SIZ, "ICS %s", gs_kind);
gameInfo.event = StrSave(str);
} else {
gameInfo.event = StrSave("ICS game");
timeIncrement = increment * 1000;
movesPerSession = 0;
gameInfo.timeControl = TimeControlTagValue();
- VariantSwitch(board, StringToVariant(gameInfo.event) );
+ VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event) );
if (appData.debugMode) {
fprintf(debugFP, "ParseBoard says variant = '%s'\n", gameInfo.event);
fprintf(debugFP, "recognized as %s\n", VariantName(gameInfo.variant));
}
gameInfo.outOfBook = NULL;
-
+
/* Do we have the ratings? */
if (strcmp(player1Name, white) == 0 &&
strcmp(player2Name, black) == 0) {
SendToICS("set shout 0\n");
}
}
-
+
/* Deal with midgame name changes */
if (!newGame) {
if (!gameInfo.white || strcmp(gameInfo.white, white) != 0) {
gameInfo.black = StrSave(black);
}
}
-
+
/* Throw away game result if anything actually changes in examine mode */
if (gameMode == IcsExamining && !newGame) {
gameInfo.result = GameUnfinished;
gameInfo.resultDetails = NULL;
}
}
-
+
/* In pausing && IcsExamining mode, we ignore boards coming
in if they are in a different variation than we are. */
if (pauseExamInvalid) return;
return;
}
}
-
+
if (appData.debugMode) {
fprintf(debugFP, "load %dx%d board\n", files, ranks);
}
board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
}
}
+ if(moveNum==0 && gameInfo.variant == VariantSChess) {
+ board[5][BOARD_RGHT+1] = WhiteAngel;
+ board[6][BOARD_RGHT+1] = WhiteMarshall;
+ board[1][0] = BlackMarshall;
+ board[2][0] = BlackAngel;
+ board[1][1] = board[2][1] = board[5][BOARD_RGHT] = board[6][BOARD_RGHT] = 1;
+ }
CopyBoard(boards[moveNum], board);
+ boards[moveNum][HOLDINGS_SET] = 0; // [HGM] indicate holdings not set
if (moveNum == 0) {
startedFromSetupPosition =
!CompareBoards(board, initialPosition);
if(moveNum == 0 || gameInfo.variant != VariantFischeRandom)
{ int i, j; ChessSquare wKing = WhiteKing, bKing = BlackKing;
- for(i=BOARD_LEFT, j= -1; i<BOARD_RGHT; i++)
+ for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
if(board[0][i] == WhiteRook) j = i;
- initialRights[0] = castlingRights[moveNum][0] = (castle_ws == 0 && gameInfo.variant != VariantFischeRandom ? -1 : j);
- for(i=BOARD_RGHT-1, j= -1; i>=BOARD_LEFT; i--)
+ initialRights[0] = boards[moveNum][CASTLING][0] = (castle_ws == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
+ for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
if(board[0][i] == WhiteRook) j = i;
- initialRights[1] = castlingRights[moveNum][1] = (castle_wl == 0 && gameInfo.variant != VariantFischeRandom ? -1 : j);
- for(i=BOARD_LEFT, j= -1; i<BOARD_RGHT; i++)
+ initialRights[1] = boards[moveNum][CASTLING][1] = (castle_wl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
+ for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
- initialRights[3] = castlingRights[moveNum][3] = (castle_bs == 0 && gameInfo.variant != VariantFischeRandom ? -1 : j);
- for(i=BOARD_RGHT-1, j= -1; i>=BOARD_LEFT; i--)
+ initialRights[3] = boards[moveNum][CASTLING][3] = (castle_bs == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
+ for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
- initialRights[4] = castlingRights[moveNum][4] = (castle_bl == 0 && gameInfo.variant != VariantFischeRandom ? -1 : j);
+ initialRights[4] = boards[moveNum][CASTLING][4] = (castle_bl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
+ boards[moveNum][CASTLING][2] = boards[moveNum][CASTLING][5] = NoRights;
if(gameInfo.variant == VariantKnightmate) { wKing = WhiteUnicorn; bKing = BlackUnicorn; }
for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
- if(board[0][k] == wKing) initialRights[2] = castlingRights[moveNum][2] = k;
+ if(board[0][k] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = k;
for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
if(board[BOARD_HEIGHT-1][k] == bKing)
- initialRights[5] = castlingRights[moveNum][5] = k;
+ initialRights[5] = boards[moveNum][CASTLING][5] = k;
+ if(gameInfo.variant == VariantTwoKings) {
+ // In TwoKings looking for a King does not work, so always give castling rights to a King on e1/e8
+ if(board[0][4] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = 4;
+ if(board[BOARD_HEIGHT-1][4] == bKing) initialRights[5] = boards[moveNum][CASTLING][5] = 4;
+ }
} else { int r;
- r = castlingRights[moveNum][0] = initialRights[0];
- if(board[0][r] != WhiteRook) castlingRights[moveNum][0] = -1;
- r = castlingRights[moveNum][1] = initialRights[1];
- if(board[0][r] != WhiteRook) castlingRights[moveNum][1] = -1;
- r = castlingRights[moveNum][3] = initialRights[3];
- if(board[BOARD_HEIGHT-1][r] != BlackRook) castlingRights[moveNum][3] = -1;
- r = castlingRights[moveNum][4] = initialRights[4];
- if(board[BOARD_HEIGHT-1][r] != BlackRook) castlingRights[moveNum][4] = -1;
+ r = boards[moveNum][CASTLING][0] = initialRights[0];
+ if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][0] = NoRights;
+ r = boards[moveNum][CASTLING][1] = initialRights[1];
+ if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][1] = NoRights;
+ r = boards[moveNum][CASTLING][3] = initialRights[3];
+ if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][3] = NoRights;
+ r = boards[moveNum][CASTLING][4] = initialRights[4];
+ if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][4] = NoRights;
/* wildcastle kludge: always assume King has rights */
- r = castlingRights[moveNum][2] = initialRights[2];
- r = castlingRights[moveNum][5] = initialRights[5];
+ r = boards[moveNum][CASTLING][2] = initialRights[2];
+ r = boards[moveNum][CASTLING][5] = initialRights[5];
}
/* [HGM] e.p. rights. Assume that ICS sends file number here? */
- epStatus[moveNum] = double_push == -1 ? EP_NONE : double_push + BOARD_LEFT;
+ boards[moveNum][EP_STATUS] = EP_NONE;
+ if(str[0] == 'P') boards[moveNum][EP_STATUS] = EP_PAWN_MOVE;
+ if(strchr(move_str, 'x')) boards[moveNum][EP_STATUS] = EP_CAPTURE;
+ if(double_push != -1) boards[moveNum][EP_STATUS] = double_push + BOARD_LEFT;
+
-
if (ics_getting_history == H_GOT_REQ_HEADER ||
ics_getting_history == H_GOT_UNREQ_HEADER) {
/* This was an initial position from a move list, not
the current position */
return;
}
-
+
/* Update currentMove and known move number limits */
newMove = newGame || moveNum > forwardMostMove;
- /* [DM] If we found takebacks during icsEngineAnalyze try send to engine */
- if (!newGame && appData.icsEngineAnalyze && moveNum < forwardMostMove) {
- takeback = forwardMostMove - moveNum;
- for (i = 0; i < takeback; i++) {
- if (appData.debugMode) fprintf(debugFP, "take back move\n");
- SendToProgram("undo\n", &first);
- }
- }
-
if (newGame) {
forwardMostMove = backwardMostMove = currentMove = moveNum;
if (gameMode == IcsExamining && moveNum == 0) {
type when starting to examine a game. But if we ask for
the move list, the move list header will tell us */
ics_getting_history = H_REQUESTED;
- sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
+ snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
SendToICS(str);
}
} else if (moveNum == forwardMostMove + 1 || moveNum == forwardMostMove
|| (moveNum < forwardMostMove && moveNum >= backwardMostMove)) {
+#if ZIPPY
+ /* [DM] If we found takebacks during icsEngineAnalyze try send to engine */
+ /* [HGM] applied this also to an engine that is silently watching */
+ if (appData.zippyPlay && moveNum < forwardMostMove && first.initDone &&
+ (gameMode == IcsObserving || gameMode == IcsExamining) &&
+ gameInfo.variant == currentlyInitializedVariant) {
+ takeback = forwardMostMove - moveNum;
+ for (i = 0; i < takeback; i++) {
+ if (appData.debugMode) fprintf(debugFP, "take back move\n");
+ SendToProgram("undo\n", &first);
+ }
+ }
+#endif
+
forwardMostMove = moveNum;
if (!pausing || currentMove > forwardMostMove)
currentMove = forwardMostMove;
} else {
- /* New part of history that is not contiguous with old part */
+ /* New part of history that is not contiguous with old part */
if (pausing && gameMode == IcsExamining) {
pauseExamInvalid = TRUE;
forwardMostMove = pauseExamForwardMostMove;
return;
}
- forwardMostMove = backwardMostMove = currentMove = moveNum;
if (gameMode == IcsExamining && moveNum > 0 && appData.getMoveList) {
+#if ZIPPY
+ if(appData.zippyPlay && forwardMostMove > 0 && first.initDone) {
+ // [HGM] when we will receive the move list we now request, it will be
+ // fed to the engine from the first move on. So if the engine is not
+ // in the initial position now, bring it there.
+ InitChessProgram(&first, 0);
+ }
+#endif
ics_getting_history = H_REQUESTED;
- sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
+ snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
SendToICS(str);
}
+ forwardMostMove = backwardMostMove = currentMove = moveNum;
}
-
+
/* Update the clocks */
if (strchr(elapsed_time, '.')) {
/* Time is in ms */
timeRemaining[0][moveNum] = whiteTimeRemaining = white_time * 1000;
timeRemaining[1][moveNum] = blackTimeRemaining = black_time * 1000;
}
-
+
#if ZIPPY
if (appData.zippyPlay && newGame &&
gameMode != IcsExamining)
ZippyFirstBoard(moveNum, basetime, increment);
#endif
-
+
/* Put the move on the move list, first converting
to canonical algebraic form. */
if (moveNum > 0) {
if (appData.debugMode) {
- if (appData.debugMode) { int f = forwardMostMove;
- fprintf(debugFP, "parseboard %d, castling = %d %d %d %d %d %d\n", f,
- castlingRights[f][0],castlingRights[f][1],castlingRights[f][2],castlingRights[f][3],castlingRights[f][4],castlingRights[f][5]);
- }
+ int f = forwardMostMove;
+ fprintf(debugFP, "parseboard %d, castling = %d %d %d %d %d %d\n", f,
+ boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
+ boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
fprintf(debugFP, "accepted move %s from ICS, parse it.\n", move_str);
fprintf(debugFP, "moveNum = %d\n", moveNum);
fprintf(debugFP, "board = %d-%d x %d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT);
if (moveNum <= backwardMostMove) {
/* We don't know what the board looked like before
this move. Punt. */
- strcpy(parseList[moveNum - 1], move_str);
+ safeStrCpy(parseList[moveNum - 1], move_str, sizeof(parseList[moveNum - 1])/sizeof(parseList[moveNum - 1][0]));
strcat(parseList[moveNum - 1], " ");
strcat(parseList[moveNum - 1], elapsed_time);
moveList[moveNum - 1][0] = NULLCHAR;
startedFromSetupPosition = TRUE;
fromX = fromY = toX = toY = -1;
} else {
- // [HGM] long SAN: if legality-testing is off, disambiguation might not work or give wrong move.
+ // [HGM] long SAN: if legality-testing is off, disambiguation might not work or give wrong move.
// So we parse the long-algebraic move string in stead of the SAN move
int valid; char buf[MSG_SIZ], *prom;
+ if(gameInfo.variant == VariantShogi && !strchr(move_str, '=') && !strchr(move_str, '@'))
+ strcat(move_str, "="); // if ICS does not say 'promote' on non-drop, we defer.
// str looks something like "Q/a1-a2"; kill the slash
- if(str[1] == '/')
- sprintf(buf, "%c%s", str[0], str+2);
- else strcpy(buf, str); // might be castling
- if((prom = strstr(move_str, "=")) && !strstr(buf, "="))
+ if(str[1] == '/')
+ snprintf(buf, MSG_SIZ,"%c%s", str[0], str+2);
+ else safeStrCpy(buf, str, sizeof(buf)/sizeof(buf[0])); // might be castling
+ if((prom = strstr(move_str, "=")) && !strstr(buf, "="))
strcat(buf, prom); // long move lacks promo specification!
if(!appData.testLegality && move_str[1] != '@') { // drops never ambiguous (parser chokes on long form!)
- if(appData.debugMode)
+ if(appData.debugMode)
fprintf(debugFP, "replaced ICS move '%s' by '%s'\n", move_str, buf);
- strcpy(move_str, buf);
+ safeStrCpy(move_str, buf, MSG_SIZ);
}
valid = ParseOneMove(move_str, moveNum - 1, &moveType,
&fromX, &fromY, &toX, &toY, &promoChar)
// end of long SAN patch
if (valid) {
(void) CoordsToAlgebraic(boards[moveNum - 1],
- PosFlags(moveNum - 1), EP_UNKNOWN,
+ PosFlags(moveNum - 1),
fromY, fromX, toY, toX, promoChar,
parseList[moveNum-1]);
- switch (MateTest(boards[moveNum], PosFlags(moveNum), EP_UNKNOWN,
- castlingRights[moveNum]) ) {
+ switch (MateTest(boards[moveNum], PosFlags(moveNum)) ) {
case MT_NONE:
case MT_STALEMATE:
default:
break;
case MT_CHECK:
- if(gameInfo.variant != VariantShogi)
+ if(!IS_SHOGI(gameInfo.variant))
strcat(parseList[moveNum - 1], "+");
break;
case MT_CHECKMATE:
strcat(parseList[moveNum - 1], " ");
strcat(parseList[moveNum - 1], elapsed_time);
/* currentMoveString is set as a side-effect of ParseOneMove */
- strcpy(moveList[moveNum - 1], currentMoveString);
+ if(gameInfo.variant == VariantShogi && currentMoveString[4]) currentMoveString[4] = '^';
+ safeStrCpy(moveList[moveNum - 1], currentMoveString, sizeof(moveList[moveNum - 1])/sizeof(moveList[moveNum - 1][0]));
strcat(moveList[moveNum - 1], "\n");
+
+ if(gameInfo.holdingsWidth && !appData.disguise && gameInfo.variant != VariantSuper && gameInfo.variant != VariantGreat
+ && gameInfo.variant != VariantGrand&& gameInfo.variant != VariantSChess) // inherit info that ICS does not give from previous board
+ for(k=0; k<ranks; k++) for(j=BOARD_LEFT; j<BOARD_RGHT; j++) {
+ ChessSquare old, new = boards[moveNum][k][j];
+ if(fromY == DROP_RANK && k==toY && j==toX) continue; // dropped pieces always stand for themselves
+ old = (k==toY && j==toX) ? boards[moveNum-1][fromY][fromX] : boards[moveNum-1][k][j]; // trace back mover
+ if(old == new) continue;
+ if(old == PROMOTED(new)) boards[moveNum][k][j] = old;// prevent promoted pieces to revert to primordial ones
+ else if(new == WhiteWazir || new == BlackWazir) {
+ if(old < WhiteCannon || old >= BlackPawn && old < BlackCannon)
+ boards[moveNum][k][j] = PROMOTED(old); // choose correct type of Gold in promotion
+ else boards[moveNum][k][j] = old; // preserve type of Gold
+ } else if((old == WhitePawn || old == BlackPawn) && new != EmptySquare) // Pawn promotions (but not e.p.capture!)
+ boards[moveNum][k][j] = PROMOTED(new); // use non-primordial representation of chosen piece
+ }
} else {
/* Move from ICS was illegal!? Punt. */
- if (appData.debugMode) {
- fprintf(debugFP, "Illegal move from ICS '%s'\n", move_str);
- fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
- }
- strcpy(parseList[moveNum - 1], move_str);
+ if (appData.debugMode) {
+ fprintf(debugFP, "Illegal move from ICS '%s'\n", move_str);
+ fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
+ }
+ safeStrCpy(parseList[moveNum - 1], move_str, sizeof(parseList[moveNum - 1])/sizeof(parseList[moveNum - 1][0]));
strcat(parseList[moveNum - 1], " ");
strcat(parseList[moveNum - 1], elapsed_time);
moveList[moveNum - 1][0] = NULLCHAR;
#if ZIPPY
/* Send move to chess program (BEFORE animating it). */
- if (appData.zippyPlay && !newGame && newMove &&
+ if (appData.zippyPlay && !newGame && newMove &&
(!appData.getMoveList || backwardMostMove == 0) && first.initDone) {
if ((gameMode == IcsPlayingWhite && WhiteOnMove(moveNum)) ||
(gameMode == IcsPlayingBlack && !WhiteOnMove(moveNum))) {
if (moveList[moveNum - 1][0] == NULLCHAR) {
- sprintf(str, _("Couldn't parse move \"%s\" from ICS"),
+ snprintf(str, MSG_SIZ, _("Couldn't parse move \"%s\" from ICS"),
move_str);
DisplayError(str, 0);
} else {
}
} else if (gameMode == IcsObserving || gameMode == IcsExamining) {
if (moveList[moveNum - 1][0] == NULLCHAR) {
- sprintf(str, _("Couldn't parse move \"%s\" from ICS"), move_str);
+ snprintf(str, MSG_SIZ, _("Couldn't parse move \"%s\" from ICS"), move_str);
DisplayError(str, 0);
} else {
if(gameInfo.variant == currentlyInitializedVariant) // [HGM] refrain sending moves engine can't understand!
SetHighlights(fromX, fromY, toX, toY);
}
}
-
+
/* Start the clocks */
whiteFlag = blackFlag = FALSE;
appData.clockMode = !(basetime == 0 && increment == 0);
DisplayBothClocks();
else
StartClocks();
-
+
/* Display opponents and material strengths */
if (gameInfo.variant != VariantBughouse &&
gameInfo.variant != VariantCrazyhouse && !appData.noGUI) {
if (tinyLayout || smallLayout) {
if(gameInfo.variant == VariantNormal)
- sprintf(str, "%s(%d) %s(%d) {%d %d}",
+ snprintf(str, MSG_SIZ, "%s(%d) %s(%d) {%d %d}",
gameInfo.white, white_stren, gameInfo.black, black_stren,
basetime, increment);
else
- sprintf(str, "%s(%d) %s(%d) {%d %d w%d}",
+ snprintf(str, MSG_SIZ, "%s(%d) %s(%d) {%d %d w%d}",
gameInfo.white, white_stren, gameInfo.black, black_stren,
basetime, increment, (int) gameInfo.variant);
} else {
if(gameInfo.variant == VariantNormal)
- sprintf(str, "%s (%d) vs. %s (%d) {%d %d}",
- gameInfo.white, white_stren, gameInfo.black, black_stren,
+ snprintf(str, MSG_SIZ, "%s (%d) %s %s (%d) {%d %d}",
+ gameInfo.white, white_stren, _("vs."), gameInfo.black, black_stren,
basetime, increment);
else
- sprintf(str, "%s (%d) vs. %s (%d) {%d %d %s}",
- gameInfo.white, white_stren, gameInfo.black, black_stren,
+ snprintf(str, MSG_SIZ, "%s (%d) %s %s (%d) {%d %d %s}",
+ gameInfo.white, white_stren, _("vs."), gameInfo.black, black_stren,
basetime, increment, VariantName(gameInfo.variant));
}
DisplayTitle(str);
}
}
-
+
/* Display the board */
if (!pausing && !appData.noGUI) {
-
+
if (appData.premove)
- if (!gotPremove ||
+ if (!gotPremove ||
((gameMode == IcsPlayingWhite) && (WhiteOnMove(currentMove))) ||
((gameMode == IcsPlayingBlack) && (!WhiteOnMove(currentMove))))
ClearPremoveHighlights();
- DrawPosition(FALSE, boards[currentMove]);
+ j = seekGraphUp; seekGraphUp = FALSE; // [HGM] seekgraph: when we draw a board, it overwrites the seek graph
+ if(partnerUp) { flipView = originalFlip; partnerUp = FALSE; j = TRUE; } // [HGM] bughouse: restore view
+ DrawPosition(j, boards[currentMove]);
+
DisplayMove(moveNum - 1);
if (appData.ringBellAfterMoves && /*!ics_user_moved*/ // [HGM] use absolute method to recognize own move
!((gameMode == IcsPlayingWhite) && (!WhiteOnMove(moveNum)) ||
if(bookHit) { // [HGM] book: simulate book reply
static char bookMove[MSG_SIZ]; // a bit generous?
- programStats.nodes = programStats.depth = programStats.time =
+ programStats.nodes = programStats.depth = programStats.time =
programStats.score = programStats.got_only_move = 0;
sprintf(programStats.movelist, "%s (xbook)", bookHit);
- strcpy(bookMove, "move ");
+ safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
strcat(bookMove, bookHit);
HandleMachineMove(bookMove, &first);
}
}
void
-GetMoveListEvent()
+GetMoveListEvent ()
{
char buf[MSG_SIZ];
if (appData.icsActive && gameMode != IcsIdle && ics_gamenum > 0) {
ics_getting_history = H_REQUESTED;
- sprintf(buf, "%smoves %d\n", ics_prefix, ics_gamenum);
+ snprintf(buf, MSG_SIZ, "%smoves %d\n", ics_prefix, ics_gamenum);
SendToICS(buf);
}
}
void
-AnalysisPeriodicEvent(force)
- int force;
+SendToBoth (char *msg)
+{ // to make it easy to keep two engines in step in dual analysis
+ SendToProgram(msg, &first);
+ if(second.analyzing) SendToProgram(msg, &second);
+}
+
+void
+AnalysisPeriodicEvent (int force)
{
if (((programStats.ok_to_send == 0 || programStats.line_is_book)
&& !force) || !appData.periodicUpdates)
return;
/* Send . command to Crafty to collect stats */
- SendToProgram(".\n", &first);
+ SendToBoth(".\n");
/* Don't send another until we get a response (this makes
us stop sending to old Crafty's which don't understand
programStats.ok_to_send = 0;
}
-void ics_update_width(new_width)
- int new_width;
+void
+ics_update_width (int new_width)
{
ics_printf("set width %d\n", new_width);
}
void
-SendMoveToProgram(moveNum, cps)
- int moveNum;
- ChessProgramState *cps;
+SendMoveToProgram (int moveNum, ChessProgramState *cps)
{
char buf[MSG_SIZ];
+ if(moveList[moveNum][1] == '@' && moveList[moveNum][0] == '@') {
+ if(gameInfo.variant == VariantLion || gameInfo.variant == VariantChuChess || gameInfo.variant == VariantChu) {
+ sprintf(buf, "%s@@@@\n", cps->useUsermove ? "usermove " : "");
+ SendToProgram(buf, cps);
+ return;
+ }
+ // null move in variant where engine does not understand it (for analysis purposes)
+ SendBoard(cps, moveNum + 1); // send position after move in stead.
+ return;
+ }
if (cps->useUsermove) {
SendToProgram("usermove ", cps);
}
buf[len++] = '\n';
buf[len] = NULLCHAR;
} else {
- sprintf(buf, "%s\n", parseList[moveNum]);
+ snprintf(buf, MSG_SIZ,"%s\n", parseList[moveNum]);
}
SendToProgram(buf, cps);
} else {
AlphaRank(moveList[moveNum], 4); // and back
} else
/* Added by Tord: Send castle moves in "O-O" in FRC games if required by
- * the engine. It would be nice to have a better way to identify castle
+ * the engine. It would be nice to have a better way to identify castle
* moves here. */
- if((gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom)
- && cps->useOOCastle) {
- int fromX = moveList[moveNum][0] - AAA;
+ if(appData.fischerCastling && cps->useOOCastle) {
+ int fromX = moveList[moveNum][0] - AAA;
int fromY = moveList[moveNum][1] - ONE;
- int toX = moveList[moveNum][2] - AAA;
+ int toX = moveList[moveNum][2] - AAA;
int toY = moveList[moveNum][3] - ONE;
- if((boards[moveNum][fromY][fromX] == WhiteKing
+ if((boards[moveNum][fromY][fromX] == WhiteKing
&& boards[moveNum][toY][toX] == WhiteRook)
- || (boards[moveNum][fromY][fromX] == BlackKing
+ || (boards[moveNum][fromY][fromX] == BlackKing
&& boards[moveNum][toY][toX] == BlackRook)) {
if(toX > fromX) SendToProgram("O-O\n", cps);
else SendToProgram("O-O-O\n", cps);
}
else SendToProgram(moveList[moveNum], cps);
+ } else
+ if(moveList[moveNum][4] == ';') { // [HGM] lion: move is double-step over intermediate square
+ char *m = moveList[moveNum];
+ if((boards[moveNum][m[6]-ONE][m[5]-AAA] < BlackPawn) == (boards[moveNum][m[1]-ONE][m[0]-AAA] < BlackPawn)) // move is kludge to indicate castling
+ snprintf(buf, MSG_SIZ, "%c%d%c%d,%c%d%c%d\n", m[0], m[1] - '0', // convert to two moves
+ m[2], m[3] - '0',
+ m[5], m[6] - '0',
+ m[2] + (m[0] > m[5] ? 1 : -1), m[3] - '0');
+ else
+ snprintf(buf, MSG_SIZ, "%c%d%c%d,%c%d%c%d\n", m[0], m[1] - '0', // convert to two moves
+ m[5], m[6] - '0',
+ m[5], m[6] - '0',
+ m[2], m[3] - '0');
+ SendToProgram(buf, cps);
+ } else
+ if(BOARD_HEIGHT > 10) { // [HGM] big: convert ranks to double-digit where needed
+ if(moveList[moveNum][1] == '@' && (BOARD_HEIGHT < 16 || moveList[moveNum][0] <= 'Z')) { // drop move
+ if(moveList[moveNum][0]== '@') snprintf(buf, MSG_SIZ, "@@@@\n"); else
+ snprintf(buf, MSG_SIZ, "%c@%c%d%s", moveList[moveNum][0],
+ moveList[moveNum][2], moveList[moveNum][3] - '0', moveList[moveNum]+4);
+ } else
+ snprintf(buf, MSG_SIZ, "%c%d%c%d%s", moveList[moveNum][0], moveList[moveNum][1] - '0',
+ moveList[moveNum][2], moveList[moveNum][3] - '0', moveList[moveNum]+4);
+ SendToProgram(buf, cps);
}
else SendToProgram(moveList[moveNum], cps);
/* End of additions by Tord */
/* Send 'go' if we are in a mode where machine should play. */
if( (moveNum == 0 && setboardSpoiledMachineBlack && cps == &first) &&
(gameMode == TwoMachinesPlay ||
-#ifdef ZIPPY
+#if ZIPPY
gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite ||
#endif
gameMode == MachinePlaysBlack || gameMode == MachinePlaysWhite) ) {
}
void
-SendMoveToICS(moveType, fromX, fromY, toX, toY)
- ChessMove moveType;
- int fromX, fromY, toX, toY;
+SendMoveToICS (ChessMove moveType, int fromX, int fromY, int toX, int toY, char promoChar)
{
char user_move[MSG_SIZ];
+ char suffix[4];
+
+ if(gameInfo.variant == VariantSChess && promoChar) {
+ snprintf(suffix, 4, "=%c", toX == BOARD_WIDTH<<1 ? ToUpper(promoChar) : ToLower(promoChar));
+ if(moveType == NormalMove) moveType = WhitePromotion; // kludge to do gating
+ } else suffix[0] = NULLCHAR;
switch (moveType) {
default:
- sprintf(user_move, _("say Internal error; bad moveType %d (%d,%d-%d,%d)"),
+ snprintf(user_move, MSG_SIZ, _("say Internal error; bad moveType %d (%d,%d-%d,%d)"),
(int)moveType, fromX, fromY, toX, toY);
DisplayError(user_move + strlen("say "), 0);
break;
case WhiteHSideCastleFR:
case BlackHSideCastleFR:
/* POP Fabien */
- sprintf(user_move, "o-o\n");
+ snprintf(user_move, MSG_SIZ, "o-o%s\n", suffix);
break;
case WhiteQueenSideCastle:
case BlackQueenSideCastle:
case WhiteASideCastleFR:
case BlackASideCastleFR:
/* POP Fabien */
- sprintf(user_move, "o-o-o\n");
+ snprintf(user_move, MSG_SIZ, "o-o-o%s\n",suffix);
break;
- case WhitePromotionQueen:
- case BlackPromotionQueen:
- case WhitePromotionRook:
- case BlackPromotionRook:
- case WhitePromotionBishop:
- case BlackPromotionBishop:
- case WhitePromotionKnight:
- case BlackPromotionKnight:
- case WhitePromotionKing:
- case BlackPromotionKing:
- case WhitePromotionChancellor:
- case BlackPromotionChancellor:
- case WhitePromotionArchbishop:
- case BlackPromotionArchbishop:
- if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier)
- sprintf(user_move, "%c%c%c%c=%c\n",
+ case WhiteNonPromotion:
+ case BlackNonPromotion:
+ sprintf(user_move, "%c%c%c%c==\n", AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
+ break;
+ case WhitePromotion:
+ case BlackPromotion:
+ if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier ||
+ gameInfo.variant == VariantMakruk)
+ snprintf(user_move, MSG_SIZ, "%c%c%c%c=%c\n",
AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
PieceToChar(WhiteFerz));
else if(gameInfo.variant == VariantGreat)
- sprintf(user_move, "%c%c%c%c=%c\n",
+ snprintf(user_move, MSG_SIZ,"%c%c%c%c=%c\n",
AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
PieceToChar(WhiteMan));
else
- sprintf(user_move, "%c%c%c%c=%c\n",
+ snprintf(user_move, MSG_SIZ, "%c%c%c%c=%c\n",
AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
- PieceToChar(PromoPiece(moveType)));
+ promoChar);
break;
case WhiteDrop:
case BlackDrop:
- sprintf(user_move, "%c@%c%c\n",
- ToUpper(PieceToChar((ChessSquare) fromX)),
- AAA + toX, ONE + toY);
+ drop:
+ snprintf(user_move, MSG_SIZ, "%c@%c%c\n",
+ ToUpper(PieceToChar((ChessSquare) fromX)),
+ AAA + toX, ONE + toY);
break;
+ case IllegalMove: /* could be a variant we don't quite understand */
+ if(fromY == DROP_RANK) goto drop; // We need 'IllegalDrop' move type?
case NormalMove:
case WhiteCapturesEnPassant:
case BlackCapturesEnPassant:
- case IllegalMove: /* could be a variant we don't quite understand */
- sprintf(user_move, "%c%c%c%c\n",
+ snprintf(user_move, MSG_SIZ,"%c%c%c%c\n",
AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
break;
}
}
void
-CoordsToComputerAlgebraic(rf, ff, rt, ft, promoChar, move)
- int rf, ff, rt, ft;
- char promoChar;
- char move[7];
+UploadGameEvent ()
+{ // [HGM] upload: send entire stored game to ICS as long-algebraic moves.
+ int i, last = forwardMostMove; // make sure ICS reply cannot pre-empt us by clearing fmm
+ static char *castlingStrings[4] = { "none", "kside", "qside", "both" };
+ if(gameMode == IcsObserving || gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite) {
+ DisplayError(_("You cannot do this while you are playing or observing"), 0);
+ return;
+ }
+ if(gameMode != IcsExamining) { // is this ever not the case?
+ char buf[MSG_SIZ], *p, *fen, command[MSG_SIZ], bsetup = 0;
+
+ if(ics_type == ICS_ICC) { // on ICC match ourselves in applicable variant
+ snprintf(command,MSG_SIZ, "match %s", ics_handle);
+ } else { // on FICS we must first go to general examine mode
+ safeStrCpy(command, "examine\nbsetup", sizeof(command)/sizeof(command[0])); // and specify variant within it with bsetups
+ }
+ if(gameInfo.variant != VariantNormal) {
+ // try figure out wild number, as xboard names are not always valid on ICS
+ for(i=1; i<=36; i++) {
+ snprintf(buf, MSG_SIZ, "wild/%d", i);
+ if(StringToVariant(buf) == gameInfo.variant) break;
+ }
+ if(i<=36 && ics_type == ICS_ICC) snprintf(buf, MSG_SIZ,"%s w%d\n", command, i);
+ else if(i == 22) snprintf(buf,MSG_SIZ, "%s fr\n", command);
+ else snprintf(buf, MSG_SIZ,"%s %s\n", command, VariantName(gameInfo.variant));
+ } else snprintf(buf, MSG_SIZ,"%s\n", ics_type == ICS_ICC ? command : "examine\n"); // match yourself or examine
+ SendToICS(ics_prefix);
+ SendToICS(buf);
+ if(startedFromSetupPosition || backwardMostMove != 0) {
+ fen = PositionToFEN(backwardMostMove, NULL, 1);
+ if(ics_type == ICS_ICC) { // on ICC we can simply send a complete FEN to set everything
+ snprintf(buf, MSG_SIZ,"loadfen %s\n", fen);
+ SendToICS(buf);
+ } else { // FICS: everything has to set by separate bsetup commands
+ p = strchr(fen, ' '); p[0] = NULLCHAR; // cut after board
+ snprintf(buf, MSG_SIZ,"bsetup fen %s\n", fen);
+ SendToICS(buf);
+ if(!WhiteOnMove(backwardMostMove)) {
+ SendToICS("bsetup tomove black\n");
+ }
+ i = (strchr(p+3, 'K') != NULL) + 2*(strchr(p+3, 'Q') != NULL);
+ snprintf(buf, MSG_SIZ,"bsetup wcastle %s\n", castlingStrings[i]);
+ SendToICS(buf);
+ i = (strchr(p+3, 'k') != NULL) + 2*(strchr(p+3, 'q') != NULL);
+ snprintf(buf, MSG_SIZ, "bsetup bcastle %s\n", castlingStrings[i]);
+ SendToICS(buf);
+ i = boards[backwardMostMove][EP_STATUS];
+ if(i >= 0) { // set e.p.
+ snprintf(buf, MSG_SIZ,"bsetup eppos %c\n", i+AAA);
+ SendToICS(buf);
+ }
+ bsetup++;
+ }
+ }
+ if(bsetup || ics_type != ICS_ICC && gameInfo.variant != VariantNormal)
+ SendToICS("bsetup done\n"); // switch to normal examining.
+ }
+ for(i = backwardMostMove; i<last; i++) {
+ char buf[20];
+ snprintf(buf, sizeof(buf)/sizeof(buf[0]),"%s\n", parseList[i]);
+ if((*buf == 'b' || *buf == 'B') && buf[1] == 'x') { // work-around for stupid FICS bug, which thinks bxc3 can be a Bishop move
+ int len = strlen(moveList[i]);
+ snprintf(buf, sizeof(buf)/sizeof(buf[0]),"%s", moveList[i]); // use long algebraic
+ if(!isdigit(buf[len-2])) snprintf(buf+len-2, 20-len, "=%c\n", ToUpper(buf[len-2])); // promotion must have '=' in ICS format
+ }
+ SendToICS(buf);
+ }
+ SendToICS(ics_prefix);
+ SendToICS(ics_type == ICS_ICC ? "tag result Game in progress\n" : "commit\n");
+}
+
+int killX = -1, killY = -1, kill2X = -1, kill2Y = -1; // [HGM] lion: used for passing e.p. capture square to MakeMove
+int legNr = 1;
+
+void
+CoordsToComputerAlgebraic (int rf, int ff, int rt, int ft, char promoChar, char move[9])
{
if (rf == DROP_RANK) {
- sprintf(move, "%c@%c%c\n",
+ if(ff == EmptySquare) sprintf(move, "@@@@\n"); else // [HGM] pass
+ sprintf(move, "%c@%c%c\n",
ToUpper(PieceToChar((ChessSquare) ff)), AAA + ft, ONE + rt);
} else {
if (promoChar == 'x' || promoChar == NULLCHAR) {
- sprintf(move, "%c%c%c%c\n",
+ sprintf(move, "%c%c%c%c\n",
AAA + ff, ONE + rf, AAA + ft, ONE + rt);
+ if(killX >= 0 && killY >= 0) {
+ sprintf(move+4, ";%c%c\n", AAA + killX, ONE + killY);
+ if(kill2X >= 0 && kill2Y >= 0) sprintf(move+7, "%c%c\n", AAA + killX, ONE + killY);
+ }
} else {
sprintf(move, "%c%c%c%c%c\n",
AAA + ff, ONE + rf, AAA + ft, ONE + rt, promoChar);
}
void
-ProcessICSInitScript(f)
- FILE *f;
+ProcessICSInitScript (FILE *f)
{
char buf[MSG_SIZ];
}
+static int lastX, lastY, lastLeftX, lastLeftY, selectFlag;
+int dragging;
+static ClickType lastClickType;
+
+int
+Partner (ChessSquare *p)
+{ // change piece into promotion partner if one shogi-promotes to the other
+ ChessSquare partner = promoPartner[*p];
+ if(PieceToChar(*p) != '+' && PieceToChar(partner) != '+') return 0;
+ *p = partner;
+ return 1;
+}
+
+void
+Sweep (int step)
+{
+ ChessSquare king = WhiteKing, pawn = WhitePawn, last = promoSweep;
+ static int toggleFlag;
+ if(gameInfo.variant == VariantKnightmate) king = WhiteUnicorn;
+ if(gameInfo.variant == VariantSuicide || gameInfo.variant == VariantGiveaway) king = EmptySquare;
+ if(promoSweep >= BlackPawn) king = WHITE_TO_BLACK king, pawn = WHITE_TO_BLACK pawn;
+ if(gameInfo.variant == VariantSpartan && pawn == BlackPawn) pawn = BlackLance, king = EmptySquare;
+ if(fromY != BOARD_HEIGHT-2 && fromY != 1 && gameInfo.variant != VariantChuChess) pawn = EmptySquare;
+ if(!step) toggleFlag = Partner(&last); // piece has shogi-promotion
+ do {
+ if(step && !(toggleFlag && Partner(&promoSweep))) promoSweep -= step;
+ if(promoSweep == EmptySquare) promoSweep = BlackPawn; // wrap
+ else if((int)promoSweep == -1) promoSweep = WhiteKing;
+ else if(promoSweep == BlackPawn && step < 0 && !toggleFlag) promoSweep = WhitePawn;
+ else if(promoSweep == WhiteKing && step > 0 && !toggleFlag) promoSweep = BlackKing;
+ if(!step) step = -1;
+ } while(PieceToChar(promoSweep) == '.' || PieceToChar(promoSweep) == '~' ||
+ !toggleFlag && PieceToChar(promoSweep) == '+' || // skip promoted versions of other
+ promoRestrict[0] ? !strchr(promoRestrict, ToUpper(PieceToChar(promoSweep))) : // if choice set available, use it
+ promoSweep == pawn ||
+ appData.testLegality && (promoSweep == king || gameInfo.variant != VariantChuChess &&
+ (promoSweep == WhiteLion || promoSweep == BlackLion)));
+ if(toX >= 0) {
+ int victim = boards[currentMove][toY][toX];
+ boards[currentMove][toY][toX] = promoSweep;
+ DrawPosition(FALSE, boards[currentMove]);
+ boards[currentMove][toY][toX] = victim;
+ } else
+ ChangeDragPiece(promoSweep);
+}
+
+int
+PromoScroll (int x, int y)
+{
+ int step = 0;
+
+ if(promoSweep == EmptySquare || !appData.sweepSelect) return FALSE;
+ if(abs(x - lastX) < 25 && abs(y - lastY) < 25) return FALSE;
+ if( y > lastY + 2 ) step = -1; else if(y < lastY - 2) step = 1;
+ if(!step) return FALSE;
+ lastX = x; lastY = y;
+ if((promoSweep < BlackPawn) == flipView) step = -step;
+ if(step > 0) selectFlag = 1;
+ if(!selectFlag) Sweep(step);
+ return FALSE;
+}
+
+void
+NextPiece (int step)
+{
+ ChessSquare piece = boards[currentMove][toY][toX];
+ do {
+ pieceSweep -= step;
+ if(pieceSweep == EmptySquare) pieceSweep = WhitePawn; // wrap
+ if((int)pieceSweep == -1) pieceSweep = BlackKing;
+ if(!step) step = -1;
+ } while(PieceToChar(pieceSweep) == '.');
+ boards[currentMove][toY][toX] = pieceSweep;
+ DrawPosition(FALSE, boards[currentMove]);
+ boards[currentMove][toY][toX] = piece;
+}
/* [HGM] Shogi move preprocessor: swap digits for letters, vice versa */
void
-AlphaRank(char *move, int n)
+AlphaRank (char *move, int n)
{
// char *p = move, c; int x, y;
fprintf(debugFP, "alphaRank(%s,%d)\n", move, n);
}
- if(move[1]=='*' &&
+ if(move[1]=='*' &&
move[2]>='0' && move[2]<='9' &&
move[3]>='a' && move[3]<='x' ) {
move[1] = '@';
}
}
+char yy_textstr[8000];
+
/* Parser for moves from gnuchess, ICS, or user typein box */
Boolean
-ParseOneMove(move, moveNum, moveType, fromX, fromY, toX, toY, promoChar)
- char *move;
- int moveNum;
- ChessMove *moveType;
- int *fromX, *fromY, *toX, *toY;
- char *promoChar;
-{
- if (appData.debugMode) {
- fprintf(debugFP, "move to parse: %s\n", move);
- }
- *moveType = yylexstr(moveNum, move);
+ParseOneMove (char *move, int moveNum, ChessMove *moveType, int *fromX, int *fromY, int *toX, int *toY, char *promoChar)
+{
+ *moveType = yylexstr(moveNum, move, yy_textstr, sizeof yy_textstr);
switch (*moveType) {
- case WhitePromotionChancellor:
- case BlackPromotionChancellor:
- case WhitePromotionArchbishop:
- case BlackPromotionArchbishop:
- case WhitePromotionQueen:
- case BlackPromotionQueen:
- case WhitePromotionRook:
- case BlackPromotionRook:
- case WhitePromotionBishop:
- case BlackPromotionBishop:
- case WhitePromotionKnight:
- case BlackPromotionKnight:
- case WhitePromotionKing:
- case BlackPromotionKing:
+ case WhitePromotion:
+ case BlackPromotion:
+ case WhiteNonPromotion:
+ case BlackNonPromotion:
case NormalMove:
+ case FirstLeg:
case WhiteCapturesEnPassant:
case BlackCapturesEnPassant:
case WhiteKingSideCastle:
case BlackASideCastleFR:
/* End of code added by Tord */
case IllegalMove: /* bug or odd chess variant */
+ if(currentMoveString[1] == '@') { // illegal drop
+ *fromX = WhiteOnMove(moveNum) ?
+ (int) CharToPiece(ToUpper(currentMoveString[0])) :
+ (int) CharToPiece(ToLower(currentMoveString[0]));
+ goto drop;
+ }
*fromX = currentMoveString[0] - AAA;
*fromY = currentMoveString[1] - ONE;
*toX = currentMoveString[2] - AAA;
if (appData.testLegality) {
return (*moveType != IllegalMove);
} else {
- return !(fromX == fromY && toX == toY);
+ return !(*fromX == *toX && *fromY == *toY && killX < 0) && boards[moveNum][*fromY][*fromX] != EmptySquare &&
+ // [HGM] lion: if this is a double move we are less critical
+ WhiteOnMove(moveNum) == (boards[moveNum][*fromY][*fromX] < BlackPawn);
}
case WhiteDrop:
*fromX = *moveType == WhiteDrop ?
(int) CharToPiece(ToUpper(currentMoveString[0])) :
(int) CharToPiece(ToLower(currentMoveString[0]));
+ drop:
*fromY = DROP_RANK;
*toX = currentMoveString[2] - AAA;
*toY = currentMoveString[3] - ONE;
case AmbiguousMove:
case ImpossibleMove:
- case (ChessMove) 0: /* end of file */
+ case EndOfFile:
case ElapsedTime:
case Comment:
case PGNTag:
}
}
+Boolean pushed = FALSE;
+char *lastParseAttempt;
+
+void
+ParsePV (char *pv, Boolean storeComments, Boolean atEnd)
+{ // Parse a string of PV moves, and append to current game, behind forwardMostMove
+ int fromX, fromY, toX, toY; char promoChar;
+ ChessMove moveType;
+ Boolean valid;
+ int nr = 0;
+
+ lastParseAttempt = pv; if(!*pv) return; // turns out we crash when we parse an empty PV
+ if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile) && currentMove < forwardMostMove) {
+ PushInner(currentMove, forwardMostMove); // [HGM] engine might not be thinking on forwardMost position!
+ pushed = TRUE;
+ }
+ endPV = forwardMostMove;
+ do {
+ while(*pv == ' ' || *pv == '\n' || *pv == '\t') pv++; // must still read away whitespace
+ if(nr == 0 && !storeComments && *pv == '(') pv++; // first (ponder) move can be in parentheses
+ lastParseAttempt = pv;
+ valid = ParseOneMove(pv, endPV, &moveType, &fromX, &fromY, &toX, &toY, &promoChar);
+ if(!valid && nr == 0 &&
+ ParseOneMove(pv, endPV-1, &moveType, &fromX, &fromY, &toX, &toY, &promoChar)){
+ nr++; moveType = Comment; // First move has been played; kludge to make sure we continue
+ // Hande case where played move is different from leading PV move
+ CopyBoard(boards[endPV+1], boards[endPV-1]); // tentatively unplay last game move
+ CopyBoard(boards[endPV+2], boards[endPV-1]); // and play first move of PV
+ ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV+2]);
+ if(!CompareBoards(boards[endPV], boards[endPV+2])) {
+ endPV += 2; // if position different, keep this
+ moveList[endPV-1][0] = fromX + AAA;
+ moveList[endPV-1][1] = fromY + ONE;
+ moveList[endPV-1][2] = toX + AAA;
+ moveList[endPV-1][3] = toY + ONE;
+ parseList[endPV-1][0] = NULLCHAR;
+ safeStrCpy(moveList[endPV-2], "_0_0", sizeof(moveList[endPV-2])/sizeof(moveList[endPV-2][0])); // suppress premove highlight on takeback move
+ }
+ }
+ pv = strstr(pv, yy_textstr) + strlen(yy_textstr); // skip what we parsed
+ if(nr == 0 && !storeComments && *pv == ')') pv++; // closing parenthesis of ponder move;
+ if(moveType == Comment && storeComments) AppendComment(endPV, yy_textstr, FALSE);
+ if(moveType == Comment || moveType == NAG || moveType == ElapsedTime) {
+ valid++; // allow comments in PV
+ continue;
+ }
+ nr++;
+ if(endPV+1 > framePtr) break; // no space, truncate
+ if(!valid) break;
+ endPV++;
+ CopyBoard(boards[endPV], boards[endPV-1]);
+ ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV]);
+ CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar, moveList[endPV - 1]);
+ strncat(moveList[endPV-1], "\n", MOVE_LEN);
+ CoordsToAlgebraic(boards[endPV - 1],
+ PosFlags(endPV - 1),
+ fromY, fromX, toY, toX, promoChar,
+ parseList[endPV - 1]);
+ } while(valid);
+ if(atEnd == 2) return; // used hidden, for PV conversion
+ currentMove = (atEnd || endPV == forwardMostMove) ? endPV : forwardMostMove + 1;
+ if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
+ SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
+ moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
+ DrawPosition(TRUE, boards[currentMove]);
+}
+
+int
+MultiPV (ChessProgramState *cps, int kind)
+{ // check if engine supports MultiPV, and if so, return the number of the option that sets it
+ int i;
+ for(i=0; i<cps->nrOptions; i++) {
+ char *s = cps->option[i].name;
+ if((kind & 1) && !StrCaseCmp(s, "MultiPV") && cps->option[i].type == Spin) return i;
+ if((kind & 2) && StrCaseStr(s, "multi") && StrCaseStr(s, "PV")
+ && StrCaseStr(s, "margin") && cps->option[i].type == Spin) return -i-2;
+ }
+ return -1;
+}
+
+Boolean extendGame; // signals to UnLoadPV() if walked part of PV has to be appended to game
+static int multi, pv_margin;
+static ChessProgramState *activeCps;
+
+Boolean
+LoadMultiPV (int x, int y, char *buf, int index, int *start, int *end, int pane)
+{
+ int startPV, lineStart, origIndex = index;
+ char *p, buf2[MSG_SIZ];
+ ChessProgramState *cps = (pane ? &second : &first);
+
+ if(index < 0 || index >= strlen(buf)) return FALSE; // sanity
+ lastX = x; lastY = y;
+ while(index > 0 && buf[index-1] != '\n') index--; // beginning of line
+ lineStart = startPV = index;
+ while(buf[index] != '\n') if(buf[index++] == '\t') startPV = index;
+ if(index == startPV && (p = StrCaseStr(buf+index, "PV="))) startPV = p - buf + 3;
+ index = startPV;
+ do{ while(buf[index] && buf[index] != '\n') index++;
+ } while(buf[index] == '\n' && buf[index+1] == '\\' && buf[index+2] == ' ' && index++); // join kibitzed PV continuation line
+ buf[index] = 0;
+ if(lineStart == 0 && gameMode == AnalyzeMode) {
+ int n = 0;
+ if(origIndex > 17 && origIndex < 24) n--; else if(origIndex > index - 6) n++;
+ if(n == 0) { // click not on "fewer" or "more"
+ if((multi = -2 - MultiPV(cps, 2)) >= 0) {
+ pv_margin = cps->option[multi].value;
+ activeCps = cps; // non-null signals margin adjustment
+ }
+ } else if((multi = MultiPV(cps, 1)) >= 0) {
+ n += cps->option[multi].value; if(n < 1) n = 1;
+ snprintf(buf2, MSG_SIZ, "option MultiPV=%d\n", n);
+ if(cps->option[multi].value != n) SendToProgram(buf2, cps);
+ cps->option[multi].value = n;
+ *start = *end = 0;
+ return FALSE;
+ }
+ } else if(strstr(buf+lineStart, "exclude:") == buf+lineStart) { // exclude moves clicked
+ ExcludeClick(origIndex - lineStart);
+ return FALSE;
+ } else if(!strncmp(buf+lineStart, "dep\t", 4)) { // column headers clicked
+ Collapse(origIndex - lineStart);
+ return FALSE;
+ }
+ ParsePV(buf+startPV, FALSE, gameMode != AnalyzeMode);
+ *start = startPV; *end = index-1;
+ extendGame = (gameMode == AnalyzeMode && appData.autoExtend && origIndex - startPV < 5);
+ return TRUE;
+}
+
+char *
+PvToSAN (char *pv)
+{
+ static char buf[10*MSG_SIZ];
+ int i, k=0, savedEnd=endPV, saveFMM = forwardMostMove;
+ *buf = NULLCHAR;
+ if(forwardMostMove < endPV) PushInner(forwardMostMove, endPV); // shelve PV of PV-walk
+ ParsePV(pv, FALSE, 2); // this appends PV to game, suppressing any display of it
+ for(i = forwardMostMove; i<endPV; i++){
+ if(i&1) snprintf(buf+k, 10*MSG_SIZ-k, "%s ", parseList[i]);
+ else snprintf(buf+k, 10*MSG_SIZ-k, "%d. %s ", i/2 + 1, parseList[i]);
+ k += strlen(buf+k);
+ }
+ snprintf(buf+k, 10*MSG_SIZ-k, "%s", lastParseAttempt); // if we ran into stuff that could not be parsed, print it verbatim
+ if(pushed) { PopInner(0); pushed = FALSE; } // restore game continuation shelved by ParsePV
+ if(forwardMostMove < savedEnd) { PopInner(0); forwardMostMove = saveFMM; } // PopInner would set fmm to endPV!
+ endPV = savedEnd;
+ return buf;
+}
+
+Boolean
+LoadPV (int x, int y)
+{ // called on right mouse click to load PV
+ int which = gameMode == TwoMachinesPlay && (WhiteOnMove(forwardMostMove) == (second.twoMachinesColor[0] == 'w'));
+ lastX = x; lastY = y;
+ ParsePV(lastPV[which], FALSE, TRUE); // load the PV of the thinking engine in the boards array.
+ extendGame = FALSE;
+ return TRUE;
+}
+
+void
+UnLoadPV ()
+{
+ int oldFMM = forwardMostMove; // N.B.: this was currentMove before PV was loaded!
+ if(activeCps) {
+ if(pv_margin != activeCps->option[multi].value) {
+ char buf[MSG_SIZ];
+ snprintf(buf, MSG_SIZ, "option %s=%d\n", "Multi-PV Margin", pv_margin);
+ SendToProgram(buf, activeCps);
+ activeCps->option[multi].value = pv_margin;
+ }
+ activeCps = NULL;
+ return;
+ }
+ if(endPV < 0) return;
+ if(appData.autoCopyPV) CopyFENToClipboard();
+ endPV = -1;
+ if(extendGame && currentMove > forwardMostMove) {
+ Boolean saveAnimate = appData.animate;
+ if(pushed) {
+ if(shiftKey && storedGames < MAX_VARIATIONS-2) { // wants to start variation, and there is space
+ if(storedGames == 1) GreyRevert(FALSE); // we already pushed the tail, so just make it official
+ } else storedGames--; // abandon shelved tail of original game
+ }
+ pushed = FALSE;
+ forwardMostMove = currentMove;
+ currentMove = oldFMM;
+ appData.animate = FALSE;
+ ToNrEvent(forwardMostMove);
+ appData.animate = saveAnimate;
+ }
+ currentMove = forwardMostMove;
+ if(pushed) { PopInner(0); pushed = FALSE; } // restore shelved game continuation
+ ClearPremoveHighlights();
+ DrawPosition(TRUE, boards[currentMove]);
+}
+
+void
+MovePV (int x, int y, int h)
+{ // step through PV based on mouse coordinates (called on mouse move)
+ int margin = h>>3, step = 0, threshold = (pieceSweep == EmptySquare ? 10 : 15);
+
+ if(activeCps) { // adjusting engine's multi-pv margin
+ if(x > lastX) pv_margin++; else
+ if(x < lastX) pv_margin -= (pv_margin > 0);
+ if(x != lastX) {
+ char buf[MSG_SIZ];
+ snprintf(buf, MSG_SIZ, "margin = %d", pv_margin);
+ DisplayMessage(buf, "");
+ }
+ lastX = x;
+ return;
+ }
+ // we must somehow check if right button is still down (might be released off board!)
+ if(endPV < 0 && pieceSweep == EmptySquare) return; // needed in XBoard because lastX/Y is shared :-(
+ if(abs(x - lastX) < threshold && abs(y - lastY) < threshold) return;
+ if( y > lastY + 2 ) step = -1; else if(y < lastY - 2) step = 1;
+ if(!step) return;
+ lastX = x; lastY = y;
+
+ if(pieceSweep != EmptySquare) { NextPiece(step); return; }
+ if(endPV < 0) return;
+ if(y < margin) step = 1; else
+ if(y > h - margin) step = -1;
+ if(currentMove + step > endPV || currentMove + step < forwardMostMove) step = 0;
+ currentMove += step;
+ if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
+ SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
+ moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
+ DrawPosition(FALSE, boards[currentMove]);
+}
+
+
// [HGM] shuffle: a general way to suffle opening setups, applicable to arbitrary variants.
// All positions will have equal probability, but the current method will not provide a unique
// numbering scheme for arrays that contain 3 or more pieces of the same kind.
int piecesLeft[(int)BlackPawn];
int seed, nrOfShuffles;
-void GetPositionNumber()
+void
+GetPositionNumber ()
{ // sets global variable seed
int i;
}
}
-int put(Board board, int pieceType, int rank, int n, int shade)
+int
+put (Board board, int pieceType, int rank, int n, int shade)
// put the piece on the (n-1)-th empty squares of the given shade
{
int i;
board[rank][i] = (ChessSquare) pieceType;
squaresLeft[((i-BOARD_LEFT)&1) + 1]--;
squaresLeft[ANY]--;
- piecesLeft[pieceType]--;
+ piecesLeft[pieceType]--;
return i;
}
}
}
-void AddOnePiece(Board board, int pieceType, int rank, int shade)
+void
+AddOnePiece (Board board, int pieceType, int rank, int shade)
// calculate where the next piece goes, (any empty square), and put it there
{
int i;
put(board, pieceType, rank, i, shade);
}
-void AddTwoPieces(Board board, int pieceType, int rank)
+void
+AddTwoPieces (Board board, int pieceType, int rank)
// calculate where the next 2 identical pieces go, (any empty square), and put it there
{
int i, n=squaresLeft[ANY], j=n-1, k;
put(board, pieceType, rank, i, ANY);
}
-void SetUpShuffle(Board board, int number)
+void
+SetUpShuffle (Board board, int number)
{
int i, p, first=1;
// Last King gets castling rights
while(piecesLeft[(int)WhiteUnicorn]) {
i = put(board, WhiteUnicorn, 0, piecesLeft[(int)WhiteRook]/2, ANY);
- initialRights[2] = initialRights[5] = castlingRights[0][2] = castlingRights[0][5] = i;
+ initialRights[2] = initialRights[5] = board[CASTLING][2] = board[CASTLING][5] = i;
}
while(piecesLeft[(int)WhiteKing]) {
i = put(board, WhiteKing, 0, piecesLeft[(int)WhiteRook]/2, ANY);
- initialRights[2] = initialRights[5] = castlingRights[0][2] = castlingRights[0][5] = i;
+ initialRights[2] = initialRights[5] = board[CASTLING][2] = board[CASTLING][5] = i;
}
if(PosFlags(0) & F_FRC_TYPE_CASTLING) { // first and last Rook get FRC castling rights
if(first) {
first=0;
- initialRights[1] = initialRights[4] = castlingRights[0][1] = castlingRights[0][4] = i;
+ initialRights[1] = initialRights[4] = board[CASTLING][1] = board[CASTLING][4] = i;
}
- initialRights[0] = initialRights[3] = castlingRights[0][0] = castlingRights[0][3] = i;
+ initialRights[0] = initialRights[3] = board[CASTLING][0] = board[CASTLING][3] = i;
}
}
for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // copy black from white
if(number >= 0) appData.defaultFrcPosition %= nrOfShuffles; // normalize
}
-int SetCharTable( char *table, const char * map )
+int
+ptclen (const char *s, char *escapes)
+{
+ int n = 0;
+ if(!*escapes) return strlen(s);
+ while(*s) n += (*s != '-' && *s != '^' && *s != '*' && !strchr(escapes, *s)), s++;
+ return n;
+}
+
+static int pieceOrder[] = {
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, // P N B R Q F E A C W M
+ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, // O H I J G D V L S U Lion
+ 45, 23, 24, 25, 26, 27, 28, 29, 46, 31, 32, // Sword Zebra Camel Tower Wolf Dragon Duck Axe Leopard Gnu Cub
+ 44, 51, 56, 57, 58, 59, 60, 61, 62, 63, 34, // Whale Pegasus Wizard Copper Iron Viking Flag Amazon Wheel Shield Claw
+ 33, 55, 53, 42, 37, 48, 39, 40, 41, 22, 30, // +P +N =B =R +L +S +E +Ph +Kn Butterfly Hat
+ 38, 43, 35, 36, 49, 47, 52, 50, 54, 64, 65 // +V +M =H =D Princess HSword +GB HCrown Wheer Shierd King
+};
+
+int
+SetCharTableEsc (unsigned char *table, const char * map, char * escapes)
/* [HGM] moved here from winboard.c because of its general usefulness */
/* Basically a safe strcpy that uses the last character as King */
{
int result = FALSE; int NrPieces;
+ unsigned char partner[EmptySquare];
- if( map != NULL && (NrPieces=strlen(map)) <= (int) EmptySquare
+ if( map != NULL && (NrPieces=ptclen(map, escapes)) <= (int) EmptySquare
&& NrPieces >= 12 && !(NrPieces&1)) {
- int i; /* [HGM] Accept even length from 12 to 34 */
+ int i, ii, j = 0; /* [HGM] Accept even length from 12 to 88 */
for( i=0; i<(int) EmptySquare; i++ ) table[i] = '.';
- for( i=0; i<NrPieces/2-1; i++ ) {
- table[i] = map[i];
- table[i + (int)BlackPawn - (int) WhitePawn] = map[i+NrPieces/2];
+ for( ii=0; ii<NrPieces/2-1; ii++ ) {
+ char *p, c=0;
+ i = pieceOrder[ii];
+ if(*escapes && (map[j] == '*' || map[j] == '-' || map[j] == '^')) c = map[j++];
+ table[i] = map[j++];
+ if(p = strchr(escapes, map[j])) j++, table[i] += 64*(p - escapes + 1);
+ if(c) partner[i] = table[i], table[i] = c;
+ }
+ table[(int) WhiteKing] = map[j++];
+ for( ii=0; ii<NrPieces/2-1; ii++ ) {
+ char *p, c=0;
+ i = WHITE_TO_BLACK pieceOrder[ii];
+ if(*escapes && (map[j] == '*' || map[j] == '-' || map[j] == '^')) c = map[j++];
+ table[i] = map[j++];
+ if(p = strchr(escapes, map[j])) j++, table[i] += 64*(p - escapes + 1);
+ if(c) partner[i] = table[i], table[i] = c;
+ }
+ table[(int) BlackKing] = map[j++];
+
+
+ if(*escapes) { // set up promotion pairing
+ for( i=0; i<(int) EmptySquare; i++ ) promoPartner[i] = (i%BlackPawn < 11 ? i + 11 : i%BlackPawn < 22 ? i - 11 : i); // default
+ // pieceToChar entirely filled, so we can look up specified partners
+ for(i=0; i<EmptySquare; i++) { // adjust promotion pairing
+ int c = table[i];
+ if(c == '^' || c == '-') { // has specified partner
+ int p;
+ for(p=0; p<EmptySquare; p++) if(table[p] == partner[i]) break;
+ if(c == '^') table[i] = '+';
+ if(p < EmptySquare) promoPartner[p] = i, promoPartner[i] = p; // marry them
+ } else if(c == '*') promoPartner[i] = (i < BlackPawn ? WhiteTokin : BlackTokin); // promotes to Tokin
+ }
}
- table[(int) WhiteKing] = map[NrPieces/2-1];
- table[(int) BlackKing] = map[NrPieces-1];
result = TRUE;
}
return result;
}
-void Prelude(Board board)
+int
+SetCharTable (unsigned char *table, const char * map)
+{
+ return SetCharTableEsc(table, map, "");
+}
+
+void
+Prelude (Board board)
{ // [HGM] superchess: random selection of exo-pieces
- int i, j, k; ChessSquare p;
+ int i, j, k; ChessSquare p;
static ChessSquare exoPieces[4] = { WhiteAngel, WhiteMarshall, WhiteSilver, WhiteLance };
GetPositionNumber(); // use FRC position number
if(appData.pieceToCharTable != NULL) { // select pieces to participate from given char table
SetCharTable(pieceToChar, appData.pieceToCharTable);
- for(i=(int)WhiteQueen+1, j=0; i<(int)WhiteKing && j<4; i++)
+ for(i=(int)WhiteQueen+1, j=0; i<(int)WhiteKing && j<4; i++)
if(PieceToChar((ChessSquare)i) != '.') exoPieces[j++] = (ChessSquare) i;
}
- j = seed%4; seed /= 4;
+ j = seed%4; seed /= 4;
p = board[0][BOARD_LEFT+j]; board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
- j = seed%3 + (seed%3 >= j); seed /= 3;
+ j = seed%3 + (seed%3 >= j); seed /= 3;
p = board[0][BOARD_LEFT+j]; board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
- j = seed%3; seed /= 3;
+ j = seed%3; seed /= 3;
p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
- j = seed%2 + (seed%2 >= j); seed /= 2;
+ j = seed%2 + (seed%2 >= j); seed /= 2;
p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
}
void
-InitPosition(redraw)
- int redraw;
+InitPosition (int redraw)
{
- ChessSquare (* pieces)[BOARD_SIZE];
- int i, j, pawnRow, overrule,
+ ChessSquare (* pieces)[BOARD_FILES];
+ int i, j, pawnRow=1, pieceRows=1, overrule,
oldx = gameInfo.boardWidth,
oldy = gameInfo.boardHeight,
- oldh = gameInfo.holdingsWidth,
- oldv = gameInfo.variant;
+ oldh = gameInfo.holdingsWidth;
+ static int oldv;
- if(appData.icsActive) shuffleOpenings = FALSE; // [HGM] shuffle: in ICS mode, only shuffle on ICS request
+ if(appData.icsActive) shuffleOpenings = appData.fischerCastling = FALSE; // [HGM] shuffle: in ICS mode, only shuffle on ICS request
/* [AS] Initialize pv info list [HGM] and game status */
{
- for( i=0; i<MAX_MOVES; i++ ) {
+ for( i=0; i<=framePtr; i++ ) { // [HGM] vari: spare saved variations
pvInfoList[i].depth = 0;
- epStatus[i]=EP_NONE;
- for( j=0; j<BOARD_SIZE; j++ ) castlingRights[i][j] = -1;
+ boards[i][EP_STATUS] = EP_NONE;
+ for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
}
initialRulePlies = 0; /* 50-move counter start */
castlingRank[3] = castlingRank[4] = castlingRank[5] = BOARD_HEIGHT-1;
}
-
+
/* [HGM] logic here is completely changed. In stead of full positions */
/* the initialized data only consist of the two backranks. The switch */
/* selects which one we will use, which is than copied to the Board */
gameInfo.boardHeight = 8;
gameInfo.holdingsSize = 0;
nrCastlingRights = -1; /* [HGM] Kludge to indicate default should be used */
- for(i=0; i<BOARD_SIZE; i++) initialRights[i] = -1; /* but no rights yet */
- SetCharTable(pieceToChar, "PNBRQ...........Kpnbrq...........k");
+ for(i=0; i<BOARD_FILES-6; i++)
+ initialPosition[CASTLING][i] = initialRights[i] = NoRights; /* but no rights yet */
+ initialPosition[EP_STATUS] = EP_NONE;
+ initialPosition[TOUCHED_W] = initialPosition[TOUCHED_B] = 0;
+ SetCharTableEsc(pieceToChar, "PNBRQ...........Kpnbrq...........k", SUFFIXES);
+ if(startVariant == gameInfo.variant) // [HGM] nicks: enable nicknames in original variant
+ SetCharTable(pieceNickName, appData.pieceNickNames);
+ else SetCharTable(pieceNickName, "............");
+ pieces = FIDEArray;
switch (gameInfo.variant) {
case VariantFischeRandom:
shuffleOpenings = TRUE;
+ appData.fischerCastling = TRUE;
default:
- pieces = FIDEArray;
break;
case VariantShatranj:
pieces = ShatranjArray;
nrCastlingRights = 0;
- SetCharTable(pieceToChar, "PN.R.QB...Kpn.r.qb...k");
+ SetCharTable(pieceToChar, "PN.R.QB...Kpn.r.qb...k");
+ break;
+ case VariantMakruk:
+ pieces = makrukArray;
+ nrCastlingRights = 0;
+ SetCharTable(pieceToChar, "PN.R.M....SKpn.r.m....sk");
+ break;
+ case VariantASEAN:
+ pieces = aseanArray;
+ nrCastlingRights = 0;
+ SetCharTable(pieceToChar, "PN.R.Q....BKpn.r.q....bk");
break;
case VariantTwoKings:
pieces = twoKingsArray;
break;
+ case VariantGrand:
+ pieces = GrandArray;
+ nrCastlingRights = 0;
+ SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
+ gameInfo.boardWidth = 10;
+ gameInfo.boardHeight = 10;
+ gameInfo.holdingsSize = 7;
+ break;
case VariantCapaRandom:
shuffleOpenings = TRUE;
+ appData.fischerCastling = TRUE;
case VariantCapablanca:
pieces = CapablancaArray;
gameInfo.boardWidth = 10;
- SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
+ SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
break;
case VariantGothic:
pieces = GothicArray;
gameInfo.boardWidth = 10;
- SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
+ SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
+ break;
+ case VariantSChess:
+ SetCharTable(pieceToChar, "PNBRQ..HEKpnbrq..hek");
+ gameInfo.holdingsSize = 7;
+ for(i=0; i<BOARD_FILES; i++) initialPosition[VIRGIN][i] = VIRGIN_W | VIRGIN_B;
break;
case VariantJanus:
pieces = JanusArray;
gameInfo.boardWidth = 10;
- SetCharTable(pieceToChar, "PNBRQ..JKpnbrq..jk");
+ SetCharTable(pieceToChar, "PNBRQ..JKpnbrq..jk");
nrCastlingRights = 6;
- castlingRights[0][0] = initialRights[0] = BOARD_RGHT-1;
- castlingRights[0][1] = initialRights[1] = BOARD_LEFT;
- castlingRights[0][2] = initialRights[2] =(BOARD_WIDTH-1)>>1;
- castlingRights[0][3] = initialRights[3] = BOARD_RGHT-1;
- castlingRights[0][4] = initialRights[4] = BOARD_LEFT;
- castlingRights[0][5] = initialRights[5] =(BOARD_WIDTH-1)>>1;
+ initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
+ initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
+ initialPosition[CASTLING][2] = initialRights[2] =(BOARD_WIDTH-1)>>1;
+ initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
+ initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
+ initialPosition[CASTLING][5] = initialRights[5] =(BOARD_WIDTH-1)>>1;
break;
case VariantFalcon:
pieces = FalconArray;
gameInfo.boardWidth = 10;
- SetCharTable(pieceToChar, "PNBRQ.............FKpnbrq.............fk");
+ SetCharTable(pieceToChar, "PNBRQ............FKpnbrq............fk");
break;
case VariantXiangqi:
pieces = XiangqiArray;
gameInfo.boardWidth = 9;
gameInfo.boardHeight = 10;
nrCastlingRights = 0;
- SetCharTable(pieceToChar, "PH.R.AE..K.C.ph.r.ae..k.c.");
+ SetCharTable(pieceToChar, "PH.R.AE..K.C.ph.r.ae..k.c.");
break;
case VariantShogi:
pieces = ShogiArray;
gameInfo.boardHeight = 9;
gameInfo.holdingsSize = 7;
nrCastlingRights = 0;
- SetCharTable(pieceToChar, "PNBRLS...G.++++++Kpnbrls...g.++++++k");
+ SetCharTable(pieceToChar, "PNBRLS...G.++++++Kpnbrls...g.++++++k");
+ break;
+ case VariantChu:
+ pieces = ChuArray; pieceRows = 3;
+ gameInfo.boardWidth = 12;
+ gameInfo.boardHeight = 12;
+ nrCastlingRights = 0;
+ SetCharTableEsc(pieceToChar, "P.BRQSEXOGCATHD.VMLIFN/^P.^B^R.^S^E^X^O^G^C^A^T^H^D.^V^M^L^I^FK"
+ "p.brqsexogcathd.vmlifn/^p.^b^r.^s^e^x^o^g^c^a^t^h^d.^v^m^l^i^fk", SUFFIXES);
break;
case VariantCourier:
pieces = CourierArray;
gameInfo.boardWidth = 12;
nrCastlingRights = 0;
- SetCharTable(pieceToChar, "PNBR.FE..WMKpnbr.fe..wmk");
- for(i=0; i<BOARD_SIZE; i++) initialRights[i] = -1;
+ SetCharTable(pieceToChar, "PNBR.FE..WMKpnbr.fe..wmk");
break;
case VariantKnightmate:
pieces = KnightmateArray;
- SetCharTable(pieceToChar, "P.BRQ.....M.........K.p.brq.....m.........k.");
+ SetCharTable(pieceToChar, "P.BRQ.....M.........K.p.brq.....m.........k.");
+ break;
+ case VariantSpartan:
+ pieces = SpartanArray;
+ SetCharTable(pieceToChar, "PNBRQ................K......lwg.....c...h..k");
+ break;
+ case VariantLion:
+ pieces = lionArray;
+ SetCharTable(pieceToChar, "PNBRQ................LKpnbrq................lk");
+ break;
+ case VariantChuChess:
+ pieces = ChuChessArray;
+ gameInfo.boardWidth = 10;
+ gameInfo.boardHeight = 10;
+ SetCharTable(pieceToChar, "PNBRQ.....M.+++......LKpnbrq.....m.+++......lk");
break;
case VariantFairy:
pieces = fairyArray;
- SetCharTable(pieceToChar, "PNBRQFEACWMOHIJGDVSLUKpnbrqfeacwmohijgdvsluk");
+ SetCharTable(pieceToChar, "PNBRQFEACWMOHIJGDVLSUKpnbrqfeacwmohijgdvlsuk");
break;
case VariantGreat:
pieces = GreatArray;
case VariantCrazyhouse:
case VariantBughouse:
pieces = FIDEArray;
- SetCharTable(pieceToChar, "PNBRQ.......~~~~Kpnbrq.......~~~~k");
+ SetCharTable(pieceToChar, "PNBRQ.......~~~~Kpnbrq.......~~~~k");
gameInfo.holdingsSize = 5;
break;
case VariantWildCastle:
case VariantNoCastle:
pieces = FIDEArray;
nrCastlingRights = 0;
- for(i=0; i<BOARD_SIZE; i++) initialRights[i] = -1;
/* !!?unconstrained back-rank shuffle */
shuffleOpenings = 1;
break;
gameInfo.holdingsSize = i;
}
if(gameInfo.holdingsSize) gameInfo.holdingsWidth = 2;
- if(BOARD_HEIGHT > BOARD_SIZE || BOARD_WIDTH > BOARD_SIZE)
- DisplayFatalError(_("Recompile to support this BOARD_SIZE!"), 0, 2);
+ if(BOARD_HEIGHT > BOARD_RANKS || BOARD_WIDTH > BOARD_FILES)
+ DisplayFatalError(_("Recompile to support this BOARD_RANKS or BOARD_FILES!"), 0, 2);
pawnRow = gameInfo.boardHeight - 7; /* seems to work in all common variants */
if(pawnRow < 1) pawnRow = 1;
+ if(gameInfo.variant == VariantMakruk || gameInfo.variant == VariantASEAN ||
+ gameInfo.variant == VariantGrand || gameInfo.variant == VariantChuChess) pawnRow = 2;
+ if(gameInfo.variant == VariantChu) pawnRow = 3;
/* User pieceToChar list overrules defaults */
if(appData.pieceToCharTable != NULL)
- SetCharTable(pieceToChar, appData.pieceToCharTable);
+ SetCharTableEsc(pieceToChar, appData.pieceToCharTable, SUFFIXES);
for( j=0; j<BOARD_WIDTH; j++ ) { ChessSquare s = EmptySquare;
initialPosition[i][j] = s;
if(j < BOARD_LEFT || j >= BOARD_RGHT || overrule) continue;
- initialPosition[0][j] = pieces[0][j-gameInfo.holdingsWidth];
+ initialPosition[gameInfo.variant == VariantGrand || gameInfo.variant == VariantChuChess][j] = pieces[0][j-gameInfo.holdingsWidth];
initialPosition[pawnRow][j] = WhitePawn;
- initialPosition[BOARD_HEIGHT-pawnRow-1][j] = BlackPawn;
+ initialPosition[BOARD_HEIGHT-pawnRow-1][j] = gameInfo.variant == VariantSpartan ? BlackLance : BlackPawn;
if(gameInfo.variant == VariantXiangqi) {
if(j&1) {
- initialPosition[pawnRow][j] =
+ initialPosition[pawnRow][j] =
initialPosition[BOARD_HEIGHT-pawnRow-1][j] = EmptySquare;
if(j==BOARD_LEFT+1 || j>=BOARD_RGHT-2) {
initialPosition[2][j] = WhiteCannon;
}
}
}
- initialPosition[BOARD_HEIGHT-1][j] = pieces[1][j-gameInfo.holdingsWidth];
+ if(gameInfo.variant == VariantChu) {
+ if(j == (BOARD_WIDTH-2)/3 || j == BOARD_WIDTH - (BOARD_WIDTH+1)/3)
+ initialPosition[pawnRow+1][j] = WhiteCobra,
+ initialPosition[BOARD_HEIGHT-pawnRow-2][j] = BlackCobra;
+ for(i=1; i<pieceRows; i++) {
+ initialPosition[i][j] = pieces[2*i][j-gameInfo.holdingsWidth];
+ initialPosition[BOARD_HEIGHT-1-i][j] = pieces[2*i+1][j-gameInfo.holdingsWidth];
+ }
+ }
+ if(gameInfo.variant == VariantGrand || gameInfo.variant == VariantChuChess) {
+ if(j==BOARD_LEFT || j>=BOARD_RGHT-1) {
+ initialPosition[0][j] = WhiteRook;
+ initialPosition[BOARD_HEIGHT-1][j] = BlackRook;
+ }
+ }
+ initialPosition[BOARD_HEIGHT-1-(gameInfo.variant == VariantGrand || gameInfo.variant == VariantChuChess)][j] = pieces[1][j-gameInfo.holdingsWidth];
}
+ if(gameInfo.variant == VariantChuChess) initialPosition[0][BOARD_WIDTH/2] = WhiteKing, initialPosition[BOARD_HEIGHT-1][BOARD_WIDTH/2-1] = BlackKing;
if( (gameInfo.variant == VariantShogi) && !overrule ) {
j=BOARD_LEFT+1;
/* This sets default castling rights from none to normal corners */
/* Variants with other castling rights must set them themselves above */
nrCastlingRights = 6;
-
- castlingRights[0][0] = initialRights[0] = BOARD_RGHT-1;
- castlingRights[0][1] = initialRights[1] = BOARD_LEFT;
- castlingRights[0][2] = initialRights[2] = BOARD_WIDTH>>1;
- castlingRights[0][3] = initialRights[3] = BOARD_RGHT-1;
- castlingRights[0][4] = initialRights[4] = BOARD_LEFT;
- castlingRights[0][5] = initialRights[5] = BOARD_WIDTH>>1;
+
+ initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
+ initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
+ initialPosition[CASTLING][2] = initialRights[2] = BOARD_WIDTH>>1;
+ initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
+ initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
+ initialPosition[CASTLING][5] = initialRights[5] = BOARD_WIDTH>>1;
}
if(gameInfo.variant == VariantSuper) Prelude(initialPosition);
initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][0] = BlackMan;
initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][1] = 9;
}
+ if( gameInfo.variant == VariantSChess ) {
+ initialPosition[1][0] = BlackMarshall;
+ initialPosition[2][0] = BlackAngel;
+ initialPosition[6][BOARD_WIDTH-1] = WhiteMarshall;
+ initialPosition[5][BOARD_WIDTH-1] = WhiteAngel;
+ initialPosition[1][1] = initialPosition[2][1] =
+ initialPosition[6][BOARD_WIDTH-2] = initialPosition[5][BOARD_WIDTH-2] = 1;
+ }
if (appData.debugMode) {
fprintf(debugFP, "shuffleOpenings = %d\n", shuffleOpenings);
}
/* [HGM] loadPos: use PositionFile for every new game */
CopyBoard(initialPosition, filePosition);
for(i=0; i<nrCastlingRights; i++)
- castlingRights[0][i] = initialRights[i] = fileRights[i];
+ initialRights[i] = filePosition[CASTLING][i];
startedFromSetupPosition = TRUE;
}
if(oldx != gameInfo.boardWidth ||
oldy != gameInfo.boardHeight ||
+ oldv != gameInfo.variant ||
oldh != gameInfo.holdingsWidth
-#ifdef GOTHIC
- || oldv == VariantGothic || // For licensing popups
- gameInfo.variant == VariantGothic
-#endif
-#ifdef FALCON
- || oldv == VariantFalcon ||
- gameInfo.variant == VariantFalcon
-#endif
)
InitDrawingSizes(-2 ,0);
+ oldv = gameInfo.variant;
if (redraw)
DrawPosition(TRUE, boards[currentMove]);
}
void
-SendBoard(cps, moveNum)
- ChessProgramState *cps;
- int moveNum;
+SendBoard (ChessProgramState *cps, int moveNum)
{
char message[MSG_SIZ];
-
+
if (cps->useSetboard) {
- char* fen = PositionToFEN(moveNum, cps->fenOverride);
- sprintf(message, "setboard %s\n", fen);
+ char* fen = PositionToFEN(moveNum, cps->fenOverride, 1);
+ snprintf(message, MSG_SIZ,"setboard %s\n", fen);
SendToProgram(message, cps);
free(fen);
} else {
ChessSquare *bp;
- int i, j;
+ int i, j, left=0, right=BOARD_WIDTH;
/* Kludge to set black to move, avoiding the troublesome and now
* deprecated "black" command.
*/
- if (!WhiteOnMove(moveNum)) SendToProgram("a2a3\n", cps);
+ if (!WhiteOnMove(moveNum)) // [HGM] but better a deprecated command than an illegal move...
+ SendToProgram(boards[0][1][BOARD_LEFT] == WhitePawn ? "a2a3\n" : "black\n", cps);
+
+ if(!cps->extendedEdit) left = BOARD_LEFT, right = BOARD_RGHT; // only board proper
SendToProgram("edit\n", cps);
SendToProgram("#\n", cps);
for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
- bp = &boards[moveNum][i][BOARD_LEFT];
- for (j = BOARD_LEFT; j < BOARD_RGHT; j++, bp++) {
+ bp = &boards[moveNum][i][left];
+ for (j = left; j < right; j++, bp++) {
+ if(j == BOARD_LEFT-1 || j == BOARD_RGHT) continue;
if ((int) *bp < (int) BlackPawn) {
- sprintf(message, "%c%c%c\n", PieceToChar(*bp),
- AAA + j, ONE + i);
+ if(j == BOARD_RGHT+1)
+ snprintf(message, MSG_SIZ, "%c@%d\n", PieceToChar(*bp), bp[-1]);
+ else snprintf(message, MSG_SIZ, "%c%c%d\n", PieceToChar(*bp), AAA + j, ONE + i - '0');
if(message[0] == '+' || message[0] == '~') {
- sprintf(message, "%c%c%c+\n",
- PieceToChar((ChessSquare)(DEMOTED *bp)),
- AAA + j, ONE + i);
+ snprintf(message, MSG_SIZ,"%c%c%d+\n",
+ PieceToChar((ChessSquare)(DEMOTED(*bp))),
+ AAA + j, ONE + i - '0');
}
if(cps->alphaRank) { /* [HGM] shogi: translate coords */
message[1] = BOARD_RGHT - 1 - j + '1';
}
}
}
-
+
SendToProgram("c\n", cps);
for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
- bp = &boards[moveNum][i][BOARD_LEFT];
- for (j = BOARD_LEFT; j < BOARD_RGHT; j++, bp++) {
+ bp = &boards[moveNum][i][left];
+ for (j = left; j < right; j++, bp++) {
+ if(j == BOARD_LEFT-1 || j == BOARD_RGHT) continue;
if (((int) *bp != (int) EmptySquare)
&& ((int) *bp >= (int) BlackPawn)) {
- sprintf(message, "%c%c%c\n", ToUpper(PieceToChar(*bp)),
- AAA + j, ONE + i);
+ if(j == BOARD_LEFT-2)
+ snprintf(message, MSG_SIZ, "%c@%d\n", ToUpper(PieceToChar(*bp)), bp[1]);
+ else snprintf(message,MSG_SIZ, "%c%c%d\n", ToUpper(PieceToChar(*bp)),
+ AAA + j, ONE + i - '0');
if(message[0] == '+' || message[0] == '~') {
- sprintf(message, "%c%c%c+\n",
- PieceToChar((ChessSquare)(DEMOTED *bp)),
- AAA + j, ONE + i);
+ snprintf(message, MSG_SIZ,"%c%c%d+\n",
+ PieceToChar((ChessSquare)(DEMOTED(*bp))),
+ AAA + j, ONE + i - '0');
}
if(cps->alphaRank) { /* [HGM] shogi: translate coords */
message[1] = BOARD_RGHT - 1 - j + '1';
}
}
}
-
+
SendToProgram(".\n", cps);
}
setboardSpoiledMachineBlack = 0; /* [HGM] assume WB 4.2.7 already solves this after sending setboard */
}
+char exclusionHeader[MSG_SIZ];
+int exCnt, excludePtr;
+typedef struct { int ff, fr, tf, tr, pc, mark; } Exclusion;
+static Exclusion excluTab[200];
+static char excludeMap[(BOARD_RANKS*BOARD_FILES*BOARD_RANKS*BOARD_FILES+7)/8]; // [HGM] exclude: bitmap for excluced moves
+
+static void
+WriteMap (int s)
+{
+ int j;
+ for(j=0; j<(BOARD_RANKS*BOARD_FILES*BOARD_RANKS*BOARD_FILES+7)/8; j++) excludeMap[j] = s;
+ exclusionHeader[19] = s ? '-' : '+'; // update tail state
+}
+
+static void
+ClearMap ()
+{
+ safeStrCpy(exclusionHeader, "exclude: none best +tail \n", MSG_SIZ);
+ excludePtr = 24; exCnt = 0;
+ WriteMap(0);
+}
+
+static void
+UpdateExcludeHeader (int fromY, int fromX, int toY, int toX, char promoChar, char state)
+{ // search given move in table of header moves, to know where it is listed (and add if not there), and update state
+ char buf[2*MOVE_LEN], *p;
+ Exclusion *e = excluTab;
+ int i;
+ for(i=0; i<exCnt; i++)
+ if(e[i].ff == fromX && e[i].fr == fromY &&
+ e[i].tf == toX && e[i].tr == toY && e[i].pc == promoChar) break;
+ if(i == exCnt) { // was not in exclude list; add it
+ CoordsToAlgebraic(boards[currentMove], PosFlags(currentMove), fromY, fromX, toY, toX, promoChar, buf);
+ if(strlen(exclusionHeader + excludePtr) < strlen(buf)) { // no space to write move
+ if(state != exclusionHeader[19]) exclusionHeader[19] = '*'; // tail is now in mixed state
+ return; // abort
+ }
+ e[i].ff = fromX; e[i].fr = fromY; e[i].tf = toX; e[i].tr = toY; e[i].pc = promoChar;
+ excludePtr++; e[i].mark = excludePtr++;
+ for(p=buf; *p; p++) exclusionHeader[excludePtr++] = *p; // copy move
+ exCnt++;
+ }
+ exclusionHeader[e[i].mark] = state;
+}
+
+static int
+ExcludeOneMove (int fromY, int fromX, int toY, int toX, char promoChar, char state)
+{ // include or exclude the given move, as specified by state ('+' or '-'), or toggle
+ char buf[MSG_SIZ];
+ int j, k;
+ ChessMove moveType;
+ if((signed char)promoChar == -1) { // kludge to indicate best move
+ if(!ParseOneMove(lastPV[0], currentMove, &moveType, &fromX, &fromY, &toX, &toY, &promoChar)) // get current best move from last PV
+ return 1; // if unparsable, abort
+ }
+ // update exclusion map (resolving toggle by consulting existing state)
+ k=(BOARD_FILES*fromY+fromX)*BOARD_RANKS*BOARD_FILES + (BOARD_FILES*toY+toX);
+ j = k%8; k >>= 3;
+ if(state == '*') state = (excludeMap[k] & 1<<j ? '+' : '-'); // toggle
+ if(state == '-' && !promoChar) // only non-promotions get marked as excluded, to allow exclusion of under-promotions
+ excludeMap[k] |= 1<<j;
+ else excludeMap[k] &= ~(1<<j);
+ // update header
+ UpdateExcludeHeader(fromY, fromX, toY, toX, promoChar, state);
+ // inform engine
+ snprintf(buf, MSG_SIZ, "%sclude ", state == '+' ? "in" : "ex");
+ CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar, buf+8);
+ SendToBoth(buf);
+ return (state == '+');
+}
+
+static void
+ExcludeClick (int index)
+{
+ int i, j;
+ Exclusion *e = excluTab;
+ if(index < 25) { // none, best or tail clicked
+ if(index < 13) { // none: include all
+ WriteMap(0); // clear map
+ for(i=0; i<exCnt; i++) exclusionHeader[excluTab[i].mark] = '+'; // and moves
+ SendToBoth("include all\n"); // and inform engine
+ } else if(index > 18) { // tail
+ if(exclusionHeader[19] == '-') { // tail was excluded
+ SendToBoth("include all\n");
+ WriteMap(0); // clear map completely
+ // now re-exclude selected moves
+ for(i=0; i<exCnt; i++) if(exclusionHeader[e[i].mark] == '-')
+ ExcludeOneMove(e[i].fr, e[i].ff, e[i].tr, e[i].tf, e[i].pc, '-');
+ } else { // tail was included or in mixed state
+ SendToBoth("exclude all\n");
+ WriteMap(0xFF); // fill map completely
+ // now re-include selected moves
+ j = 0; // count them
+ for(i=0; i<exCnt; i++) if(exclusionHeader[e[i].mark] == '+')
+ ExcludeOneMove(e[i].fr, e[i].ff, e[i].tr, e[i].tf, e[i].pc, '+'), j++;
+ if(!j) ExcludeOneMove(0, 0, 0, 0, -1, '+'); // if no moves were selected, keep best
+ }
+ } else { // best
+ ExcludeOneMove(0, 0, 0, 0, -1, '-'); // exclude it
+ }
+ } else {
+ for(i=0; i<exCnt; i++) if(i == exCnt-1 || excluTab[i+1].mark > index) {
+ char *p=exclusionHeader + excluTab[i].mark; // do trust header more than map (promotions!)
+ ExcludeOneMove(e[i].fr, e[i].ff, e[i].tr, e[i].tf, e[i].pc, *p == '+' ? '-' : '+');
+ break;
+ }
+ }
+}
+
+ChessSquare
+DefaultPromoChoice (int white)
+{
+ ChessSquare result;
+ if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier ||
+ gameInfo.variant == VariantMakruk)
+ result = WhiteFerz; // no choice
+ else if(gameInfo.variant == VariantASEAN)
+ result = WhiteRook; // no choice
+ else if(gameInfo.variant == VariantSuicide || gameInfo.variant == VariantGiveaway)
+ result= WhiteKing; // in Suicide Q is the last thing we want
+ else if(gameInfo.variant == VariantSpartan)
+ result = white ? WhiteQueen : WhiteAngel;
+ else result = WhiteQueen;
+ if(!white) result = WHITE_TO_BLACK result;
+ return result;
+}
+
+static int autoQueen; // [HGM] oneclick
+
int
-IsPromotion(fromX, fromY, toX, toY)
- int fromX, fromY, toX, toY;
+HasPromotionChoice (int fromX, int fromY, int toX, int toY, char *promoChoice, int sweepSelect)
{
+ /* [HGM] rewritten IsPromotion to only flag promotions that offer a choice */
/* [HGM] add Shogi promotions */
int promotionZoneSize=1, highestPromotingPiece = (int)WhitePawn;
- ChessSquare piece;
+ ChessSquare piece, partner;
+ ChessMove moveType;
+ Boolean premove;
+
+ if(fromX < BOARD_LEFT || fromX >= BOARD_RGHT) return FALSE; // drop
+ if(toX < BOARD_LEFT || toX >= BOARD_RGHT) return FALSE; // move into holdings
+
+ if(gameMode == EditPosition || gameInfo.variant == VariantXiangqi || // no promotions
+ !(fromX >=0 && fromY >= 0 && toX >= 0 && toY >= 0) ) // invalid move
+ return FALSE;
- if(gameMode == EditPosition || gameInfo.variant == VariantXiangqi ||
- !(fromX >=0 && fromY >= 0 && toX >= 0 && toY >= 0) ) return FALSE;
- /* [HGM] Note to self: line above also weeds out drops */
piece = boards[currentMove][fromY][fromX];
- if(gameInfo.variant == VariantShogi) {
+ if(gameInfo.variant == VariantChu) {
+ promotionZoneSize = BOARD_HEIGHT/3;
+ highestPromotingPiece = (PieceToChar(piece) == '+' || PieceToChar(CHUPROMOTED(piece)) != '+') ? WhitePawn : WhiteKing;
+ } else if(gameInfo.variant == VariantShogi) {
+ promotionZoneSize = BOARD_HEIGHT/3 +(BOARD_HEIGHT == 8);
+ highestPromotingPiece = (int)WhiteAlfil;
+ } else if(gameInfo.variant == VariantMakruk || gameInfo.variant == VariantGrand || gameInfo.variant == VariantChuChess) {
promotionZoneSize = 3;
- highestPromotingPiece = (int)WhiteKing;
- /* [HGM] Should be Silver = Ferz, really, but legality testing is off,
- and if in normal chess we then allow promotion to King, why not
- allow promotion of other piece in Shogi? */
}
+
+ // Treat Lance as Pawn when it is not representing Amazon or Lance
+ if(gameInfo.variant != VariantSuper && gameInfo.variant != VariantChu) {
+ if(piece == WhiteLance) piece = WhitePawn; else
+ if(piece == BlackLance) piece = BlackPawn;
+ }
+
+ // next weed out all moves that do not touch the promotion zone at all
if((int)piece >= BlackPawn) {
if(toY >= promotionZoneSize && fromY >= promotionZoneSize)
return FALSE;
+ if(fromY < promotionZoneSize && gameInfo.variant == VariantChuChess) return FALSE;
highestPromotingPiece = WHITE_TO_BLACK highestPromotingPiece;
} else {
if( toY < BOARD_HEIGHT - promotionZoneSize &&
fromY < BOARD_HEIGHT - promotionZoneSize) return FALSE;
+ if(fromY >= BOARD_HEIGHT - promotionZoneSize && gameInfo.variant == VariantChuChess)
+ return FALSE;
+ }
+
+ if( (int)piece > highestPromotingPiece ) return FALSE; // non-promoting piece
+
+ // weed out mandatory Shogi promotions
+ if(gameInfo.variant == VariantShogi) {
+ if(piece >= BlackPawn) {
+ if(toY == 0 && piece == BlackPawn ||
+ toY == 0 && piece == BlackQueen ||
+ toY <= 1 && piece == BlackKnight) {
+ *promoChoice = '+';
+ return FALSE;
+ }
+ } else {
+ if(toY == BOARD_HEIGHT-1 && piece == WhitePawn ||
+ toY == BOARD_HEIGHT-1 && piece == WhiteQueen ||
+ toY >= BOARD_HEIGHT-2 && piece == WhiteKnight) {
+ *promoChoice = '+';
+ return FALSE;
+ }
+ }
}
- return ( (int)piece <= highestPromotingPiece );
+
+ // weed out obviously illegal Pawn moves
+ if(appData.testLegality && (piece == WhitePawn || piece == BlackPawn) ) {
+ if(toX > fromX+1 || toX < fromX-1) return FALSE; // wide
+ if(piece == WhitePawn && toY != fromY+1) return FALSE; // deep
+ if(piece == BlackPawn && toY != fromY-1) return FALSE; // deep
+ if(fromX != toX && gameInfo.variant == VariantShogi) return FALSE;
+ // note we are not allowed to test for valid (non-)capture, due to premove
+ }
+
+ // we either have a choice what to promote to, or (in Shogi) whether to promote
+ if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier ||
+ gameInfo.variant == VariantMakruk) {
+ ChessSquare p=BlackFerz; // no choice
+ while(p < EmptySquare) { //but make sure we use piece that exists
+ *promoChoice = PieceToChar(p++);
+ if(*promoChoice != '.') break;
+ }
+ return FALSE;
+ }
+ // no sense asking what we must promote to if it is going to explode...
+ if(gameInfo.variant == VariantAtomic && boards[currentMove][toY][toX] != EmptySquare) {
+ *promoChoice = PieceToChar(BlackQueen); // Queen as good as any
+ return FALSE;
+ }
+ // give caller the default choice even if we will not make it
+ *promoChoice = ToLower(PieceToChar(defaultPromoChoice));
+ partner = piece; // pieces can promote if the pieceToCharTable says so
+ if(IS_SHOGI(gameInfo.variant)) *promoChoice = (defaultPromoChoice == piece && sweepSelect ? '=' : '+'); // obsolete?
+ else if(Partner(&partner)) *promoChoice = (defaultPromoChoice == piece && sweepSelect ? NULLCHAR : '+');
+ if( sweepSelect && gameInfo.variant != VariantGreat
+ && gameInfo.variant != VariantGrand
+ && gameInfo.variant != VariantSuper) return FALSE;
+ if(autoQueen) return FALSE; // predetermined
+
+ // suppress promotion popup on illegal moves that are not premoves
+ premove = gameMode == IcsPlayingWhite && !WhiteOnMove(currentMove) ||
+ gameMode == IcsPlayingBlack && WhiteOnMove(currentMove);
+ if(appData.testLegality && !premove) {
+ moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
+ fromY, fromX, toY, toX, IS_SHOGI(gameInfo.variant) || gameInfo.variant == VariantChuChess ? '+' : NULLCHAR);
+ if(moveType == IllegalMove) *promoChoice = NULLCHAR; // could be the fact we promoted was illegal
+ if(moveType != WhitePromotion && moveType != BlackPromotion)
+ return FALSE;
+ }
+
+ return TRUE;
}
int
-InPalace(row, column)
- int row, column;
+InPalace (int row, int column)
{ /* [HGM] for Xiangqi */
if( (row < 3 || row > BOARD_HEIGHT-4) &&
column < (BOARD_WIDTH + 4)/2 &&
}
int
-PieceForSquare (x, y)
- int x;
- int y;
+PieceForSquare (int x, int y)
{
if (x < 0 || x >= BOARD_WIDTH || y < 0 || y >= BOARD_HEIGHT)
return -1;
}
int
-OKToStartUserMove(x, y)
- int x, y;
+OKToStartUserMove (int x, int y)
{
ChessSquare from_piece;
int white_piece;
(int)from_piece < (int)BlackPawn; /* [HGM] can be > King! */
switch (gameMode) {
- case PlayFromGameFile:
case AnalyzeFile:
case TwoMachinesPlay:
case EndOfGame:
}
break;
+ case PlayFromGameFile:
+ if(!shiftKey || !appData.variations) return FALSE; // [HGM] allow starting variation in this mode
case EditGame:
if (!white_piece && WhiteOnMove(currentMove)) {
DisplayMoveError(_("It is White's turn"));
return FALSE;
- }
+ }
if (white_piece && !WhiteOnMove(currentMove)) {
DisplayMoveError(_("It is Black's turn"));
return FALSE;
- }
+ }
if (cmailMsgLoaded && (currentMove < cmailOldMove)) {
/* Editing correspondence game history */
/* Could disallow this or prompt for confirmation */
cmailOldMove = -1;
}
- if (currentMove < forwardMostMove) {
- /* Discarding moves */
- /* Could prompt for confirmation here,
- but I don't think that's such a good idea */
- forwardMostMove = currentMove;
- }
break;
case BeginningOfGame:
}
}
break;
-
+
case Training:
if (!white_piece && WhiteOnMove(currentMove)) {
DisplayMoveError(_("It is White's turn"));
return FALSE;
- }
+ }
if (white_piece && !WhiteOnMove(currentMove)) {
DisplayMoveError(_("It is Black's turn"));
return FALSE;
- }
+ }
break;
default:
break;
}
if (currentMove != forwardMostMove && gameMode != AnalyzeMode
+ && gameMode != EditGame // [HGM] vari: treat as AnalyzeMode
+ && gameMode != PlayFromGameFile // [HGM] as EditGame, with protected main line
&& gameMode != AnalyzeFile && gameMode != Training) {
DisplayMoveError(_("Displayed position is not current"));
return FALSE;
return TRUE;
}
+Boolean
+OnlyMove (int *x, int *y, Boolean captures)
+{
+ DisambiguateClosure cl;
+ if (appData.zippyPlay || !appData.testLegality) return FALSE;
+ switch(gameMode) {
+ case MachinePlaysBlack:
+ case IcsPlayingWhite:
+ case BeginningOfGame:
+ if(!WhiteOnMove(currentMove)) return FALSE;
+ break;
+ case MachinePlaysWhite:
+ case IcsPlayingBlack:
+ if(WhiteOnMove(currentMove)) return FALSE;
+ break;
+ case EditGame:
+ break;
+ default:
+ return FALSE;
+ }
+ cl.pieceIn = EmptySquare;
+ cl.rfIn = *y;
+ cl.ffIn = *x;
+ cl.rtIn = -1;
+ cl.ftIn = -1;
+ cl.promoCharIn = NULLCHAR;
+ Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
+ if( cl.kind == NormalMove ||
+ cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
+ cl.kind == WhitePromotion || cl.kind == BlackPromotion ||
+ cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
+ fromX = cl.ff;
+ fromY = cl.rf;
+ *x = cl.ft;
+ *y = cl.rt;
+ return TRUE;
+ }
+ if(cl.kind != ImpossibleMove) return FALSE;
+ cl.pieceIn = EmptySquare;
+ cl.rfIn = -1;
+ cl.ffIn = -1;
+ cl.rtIn = *y;
+ cl.ftIn = *x;
+ cl.promoCharIn = NULLCHAR;
+ Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
+ if( cl.kind == NormalMove ||
+ cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
+ cl.kind == WhitePromotion || cl.kind == BlackPromotion ||
+ cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
+ fromX = cl.ff;
+ fromY = cl.rf;
+ *x = cl.ft;
+ *y = cl.rt;
+ autoQueen = TRUE; // act as if autoQueen on when we click to-square
+ return TRUE;
+ }
+ return FALSE;
+}
+
FILE *lastLoadGameFP = NULL, *lastLoadPositionFP = NULL;
int lastLoadGameNumber = 0, lastLoadPositionNumber = 0;
int lastLoadGameUseList = FALSE;
char lastLoadGameTitle[MSG_SIZ], lastLoadPositionTitle[MSG_SIZ];
-ChessMove lastLoadGameStart = (ChessMove) 0;
+ChessMove lastLoadGameStart = EndOfFile;
+int doubleClick;
+Boolean addToBookFlag;
-ChessMove
-UserMoveTest(fromX, fromY, toX, toY, promoChar, captureOwn)
- int fromX, fromY, toX, toY;
- int promoChar;
- Boolean captureOwn;
+void
+UserMoveEvent(int fromX, int fromY, int toX, int toY, int promoChar)
{
ChessMove moveType;
- ChessSquare pdown, pup;
-
- if (fromX < 0 || fromY < 0) return ImpossibleMove;
-
- /* [HGM] suppress all moves into holdings area and guard band */
- if( toX < BOARD_LEFT || toX >= BOARD_RGHT || toY < 0 )
- return ImpossibleMove;
-
- /* [HGM] <sameColor> moved to here from winboard.c */
- /* note: capture of own piece can be legal as drag-drop premove. For click-click it is selection of new piece. */
- pdown = boards[currentMove][fromY][fromX];
- pup = boards[currentMove][toY][toX];
- if ( gameMode != EditPosition && !captureOwn &&
- (WhitePawn <= pdown && pdown < BlackPawn &&
- WhitePawn <= pup && pup < BlackPawn ||
- BlackPawn <= pdown && pdown < EmptySquare &&
- BlackPawn <= pup && pup < EmptySquare
- ) && !((gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) &&
- (pup == WhiteRook && pdown == WhiteKing && fromY == 0 && toY == 0||
- pup == BlackRook && pdown == BlackKing && fromY == BOARD_HEIGHT-1 && toY == BOARD_HEIGHT-1 ||
- pup == WhiteKing && pdown == WhiteRook && fromY == 0 && toY == 0|| // also allow RxK
- pup == BlackKing && pdown == BlackRook && fromY == BOARD_HEIGHT-1 && toY == BOARD_HEIGHT-1 )
- ) )
- return Comment;
+ ChessSquare pup;
+ int ff=fromX, rf=fromY, ft=toX, rt=toY;
/* Check if the user is playing in turn. This is complicated because we
let the user "pick up" a piece before it is his turn. So the piece he
*/
switch (gameMode) {
- case PlayFromGameFile:
case AnalyzeFile:
case TwoMachinesPlay:
case EndOfGame:
case IcsIdle:
/* We switched into a game mode where moves are not accepted,
perhaps while the mouse button was down. */
- return ImpossibleMove;
+ return;
case MachinePlaysWhite:
/* User is moving for Black */
if (WhiteOnMove(currentMove)) {
DisplayMoveError(_("It is White's turn"));
- return ImpossibleMove;
+ return;
}
break;
/* User is moving for White */
if (!WhiteOnMove(currentMove)) {
DisplayMoveError(_("It is Black's turn"));
- return ImpossibleMove;
+ return;
}
break;
+ case PlayFromGameFile:
+ if(!shiftKey ||!appData.variations) return; // [HGM] only variations
case EditGame:
case IcsExamining:
case BeginningOfGame:
case AnalyzeMode:
case Training:
+ if(fromY == DROP_RANK) break; // [HGM] drop moves (entered through move type-in) are automatically assigned to side-to-move
if ((int) boards[currentMove][fromY][fromX] >= (int) BlackPawn &&
(int) boards[currentMove][fromY][fromX] < (int) EmptySquare) {
/* User is moving for Black */
if (WhiteOnMove(currentMove)) {
DisplayMoveError(_("It is White's turn"));
- return ImpossibleMove;
+ return;
}
} else {
/* User is moving for White */
if (!WhiteOnMove(currentMove)) {
DisplayMoveError(_("It is Black's turn"));
- return ImpossibleMove;
+ return;
}
}
break;
premoveFromY = fromY;
premovePromoChar = promoChar;
gotPremove = 1;
- if (appData.debugMode)
+ if (appData.debugMode)
fprintf(debugFP, "Got premove: fromX %d,"
"fromY %d, toX %d, toY %d\n",
fromX, fromY, toX, toY);
}
- return ImpossibleMove;
+ return;
}
break;
premoveFromY = fromY;
premovePromoChar = promoChar;
gotPremove = 1;
- if (appData.debugMode)
+ if (appData.debugMode)
fprintf(debugFP, "Got premove: fromX %d,"
"fromY %d, toX %d, toY %d\n",
fromX, fromY, toX, toY);
}
- return ImpossibleMove;
+ return;
}
break;
/* EditPosition, empty square, or different color piece;
click-click move is possible */
if (toX == -2 || toY == -2) {
- boards[0][fromY][fromX] = EmptySquare;
- return AmbiguousMove;
+ boards[0][fromY][fromX] = (boards[0][fromY][fromX] == EmptySquare ? DarkSquare : EmptySquare);
+ DrawPosition(FALSE, boards[currentMove]);
+ return;
} else if (toX >= 0 && toY >= 0) {
+ if(!appData.pieceMenu && toX == fromX && toY == fromY && boards[0][rf][ff] != EmptySquare) {
+ ChessSquare q, p = boards[0][rf][ff];
+ if(p >= BlackPawn) p = BLACK_TO_WHITE p;
+ if(CHUPROMOTED(p) < BlackPawn) p = q = CHUPROMOTED(boards[0][rf][ff]);
+ else p = CHUDEMOTED (q = boards[0][rf][ff]);
+ if(PieceToChar(q) == '+') gatingPiece = p;
+ }
boards[0][toY][toX] = boards[0][fromY][fromX];
- boards[0][fromY][fromX] = EmptySquare;
- return AmbiguousMove;
+ if(fromX == BOARD_LEFT-2) { // handle 'moves' out of holdings
+ if(boards[0][fromY][0] != EmptySquare) {
+ if(boards[0][fromY][1]) boards[0][fromY][1]--;
+ if(boards[0][fromY][1] == 0) boards[0][fromY][0] = EmptySquare;
+ }
+ } else
+ if(fromX == BOARD_RGHT+1) {
+ if(boards[0][fromY][BOARD_WIDTH-1] != EmptySquare) {
+ if(boards[0][fromY][BOARD_WIDTH-2]) boards[0][fromY][BOARD_WIDTH-2]--;
+ if(boards[0][fromY][BOARD_WIDTH-2] == 0) boards[0][fromY][BOARD_WIDTH-1] = EmptySquare;
+ }
+ } else
+ boards[0][fromY][fromX] = gatingPiece;
+ DrawPosition(FALSE, boards[currentMove]);
+ return;
}
- return ImpossibleMove;
+ return;
}
- /* [HGM] If move started in holdings, it means a drop */
- if( fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) {
- if( pup != EmptySquare ) return ImpossibleMove;
- if(appData.testLegality) {
- /* it would be more logical if LegalityTest() also figured out
- * which drops are legal. For now we forbid pawns on back rank.
- * Shogi is on its own here...
- */
- if( (pdown == WhitePawn || pdown == BlackPawn) &&
- (toY == 0 || toY == BOARD_HEIGHT -1 ) )
- return(ImpossibleMove); /* no pawn drops on 1st/8th */
- }
- return WhiteDrop; /* Not needed to specify white or black yet */
+ if((toX < 0 || toY < 0) && (fromY != DROP_RANK || fromX != EmptySquare)) return;
+ pup = boards[currentMove][toY][toX];
+
+ /* [HGM] If move started in holdings, it means a drop. Convert to standard form */
+ if( (fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) && fromY != DROP_RANK ) {
+ if( pup != EmptySquare ) return;
+ moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
+ if(appData.debugMode) fprintf(debugFP, "Drop move %d, curr=%d, x=%d,y=%d, p=%d\n",
+ moveType, currentMove, fromX, fromY, boards[currentMove][fromY][fromX]);
+ // holdings might not be sent yet in ICS play; we have to figure out which piece belongs here
+ if(fromX == 0) fromY = BOARD_HEIGHT-1 - fromY; // black holdings upside-down
+ fromX = fromX ? WhitePawn : BlackPawn; // first piece type in selected holdings
+ while(PieceToChar(fromX) == '.' || PieceToChar(fromX) == '+' || PieceToNumber(fromX) != fromY && fromX != (int) EmptySquare) fromX++;
+ fromY = DROP_RANK;
}
- userOfferedDraw = FALSE;
-
/* [HGM] always test for legality, to get promotion info */
moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
- epStatus[currentMove], castlingRights[currentMove],
fromY, fromX, toY, toX, promoChar);
+
+ if(fromY == DROP_RANK && fromX == EmptySquare && (gameMode == AnalyzeMode || gameMode == EditGame || PosFlags(0) & F_NULL_MOVE)) moveType = NormalMove;
+
+ if(moveType == IllegalMove && legal[toY][toX] > 1) moveType = NormalMove; // someone explicitly told us this move is legal
+
/* [HGM] but possibly ignore an IllegalMove result */
if (appData.testLegality) {
if (moveType == IllegalMove || moveType == ImpossibleMove) {
DisplayMoveError(_("Illegal move"));
- return ImpossibleMove;
+ return;
}
}
-if(appData.debugMode) fprintf(debugFP, "moveType 3 = %d, promochar = %x\n", moveType, promoChar);
- return moveType;
- /* [HGM] <popupFix> in stead of calling FinishMove directly, this
- function is made into one that returns an OK move type if FinishMove
- should be called. This to give the calling driver routine the
- opportunity to finish the userMove input with a promotion popup,
- without bothering the user with this for invalid or illegal moves */
-/* FinishMove(moveType, fromX, fromY, toX, toY, promoChar); */
+ if(doubleClick && gameMode == AnalyzeMode) { // [HGM] exclude: move entered with double-click on from square is for exclusion, not playing
+ if(ExcludeOneMove(fromY, fromX, toY, toX, promoChar, '*')) // toggle
+ ClearPremoveHighlights(); // was included
+ else ClearHighlights(), SetPremoveHighlights(ff, rf, ft, rt); // exclusion indicated by premove highlights
+ return;
+ }
+
+ if(addToBookFlag) { // adding moves to book
+ char buf[MSG_SIZ], move[MSG_SIZ];
+ CoordsToAlgebraic(boards[currentMove], PosFlags(currentMove), fromY, fromX, toY, toX, promoChar, move);
+ if(killX >= 0) snprintf(move, MSG_SIZ, "%c%dx%c%d-%c%d", fromX + AAA, fromY + ONE - '0', killX + AAA, killY + ONE - '0', toX + AAA, toY + ONE - '0');
+ snprintf(buf, MSG_SIZ, " 0.0%% 1 %s\n", move);
+ AddBookMove(buf);
+ addToBookFlag = FALSE;
+ ClearHighlights();
+ return;
+ }
+
+ FinishMove(moveType, fromX, fromY, toX, toY, promoChar);
}
/* Common tail of UserMoveEvent and DropMenuEvent */
int
-FinishMove(moveType, fromX, fromY, toX, toY, promoChar)
- ChessMove moveType;
- int fromX, fromY, toX, toY;
- /*char*/int promoChar;
+FinishMove (ChessMove moveType, int fromX, int fromY, int toX, int toY, int promoChar)
{
char *bookHit = 0;
-if(appData.debugMode) fprintf(debugFP, "moveType 5 = %d, promochar = %x\n", moveType, promoChar);
- if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) && promoChar != NULLCHAR) {
- // [HGM] superchess: suppress promotions to non-available piece
+
+ if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) && promoChar != NULLCHAR) {
+ // [HGM] superchess: suppress promotions to non-available piece (but P always allowed)
int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
if(WhiteOnMove(currentMove)) {
if(!boards[currentMove][k][BOARD_WIDTH-2]) return 0;
/* [HGM] <popupFix> kludge to avoid having to know the exact promotion
move type in caller when we know the move is a legal promotion */
if(moveType == NormalMove && promoChar)
- moveType = PromoCharToMoveType(WhiteOnMove(currentMove), promoChar);
-if(appData.debugMode) fprintf(debugFP, "moveType 1 = %d, promochar = %x\n", moveType, promoChar);
- /* [HGM] convert drag-and-drop piece drops to standard form */
- if( fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) {
- moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
- if(appData.debugMode) fprintf(debugFP, "Drop move %d, curr=%d, x=%d,y=%d, p=%d\n",
- moveType, currentMove, fromX, fromY, boards[currentMove][fromY][fromX]);
-// fromX = boards[currentMove][fromY][fromX];
- // holdings might not be sent yet in ICS play; we have to figure out which piece belongs here
- if(fromX == 0) fromY = BOARD_HEIGHT-1 - fromY; // black holdings upside-down
- fromX = fromX ? WhitePawn : BlackPawn; // first piece type in selected holdings
- while(PieceToChar(fromX) == '.' || PieceToNumber(fromX) != fromY && fromX != (int) EmptySquare) fromX++;
- fromY = DROP_RANK;
- }
+ moveType = WhiteOnMove(currentMove) ? WhitePromotion : BlackPromotion;
/* [HGM] <popupFix> The following if has been moved here from
- UserMoveEvent(). Because it seemed to belon here (why not allow
+ UserMoveEvent(). Because it seemed to belong here (why not allow
piece drops in training games?), and because it can only be
performed after it is known to what we promote. */
if (gameMode == Training) {
/* compare the move played on the board to the next move in the
- * game. If they match, display the move and the opponent's response.
+ * game. If they match, display the move and the opponent's response.
* If they don't match, display an error message.
*/
int saveAnimate;
- Board testBoard; char testRights[BOARD_SIZE]; char testStatus;
+ Board testBoard;
CopyBoard(testBoard, boards[currentMove]);
- ApplyMove(fromX, fromY, toX, toY, promoChar, testBoard, testRights, &testStatus);
+ ApplyMove(fromX, fromY, toX, toY, promoChar, testBoard);
if (CompareBoards(testBoard, boards[currentMove+1])) {
ForwardInner(currentMove+1);
/* Ok, now we know that the move is good, so we can kill
the previous line in Analysis Mode */
- if (gameMode == AnalyzeMode && currentMove < forwardMostMove) {
- forwardMostMove = currentMove;
+ if ((gameMode == AnalyzeMode || gameMode == EditGame || gameMode == PlayFromGameFile && appData.variations && shiftKey)
+ && currentMove < forwardMostMove) {
+ if(appData.variations && shiftKey) PushTail(currentMove, forwardMostMove); // [HGM] vari: save tail of game
+ else forwardMostMove = currentMove;
}
+ ClearMap();
+
/* If we need the chess program but it's dead, restart it */
ResurrectChessProgram();
MakeMove(fromX, fromY, toX, toY, promoChar); /*updates forwardMostMove*/
+ if(Adjudicate(NULL)) { // [HGM] adjudicate: take care of automatic game end
+ ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
+ return 1;
+ }
+
if (gameMode == BeginningOfGame) {
if (appData.noChessProgram) {
gameMode = EditGame;
gameMode = MachinePlaysBlack;
StartClocks();
SetGameInfo();
- sprintf(buf, "%s vs. %s", gameInfo.white, gameInfo.black);
+ snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
DisplayTitle(buf);
if (first.sendName) {
- sprintf(buf, "name %s\n", gameInfo.white);
+ snprintf(buf, MSG_SIZ,"name %s\n", gameInfo.white);
SendToProgram(buf, &first);
}
StartClocks();
}
ModeHighlight();
}
-if(appData.debugMode) fprintf(debugFP, "moveType 2 = %d, promochar = %x\n", moveType, promoChar);
+
/* Relay move to ICS or chess engine */
if (appData.icsActive) {
if (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
gameMode == IcsExamining) {
- SendMoveToICS(moveType, fromX, fromY, toX, toY);
+ if(userOfferedDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
+ SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
+ SendToICS("draw ");
+ SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
+ }
+ // also send plain move, in case ICS does not understand atomic claims
+ SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
ics_user_moved = 1;
}
} else {
gameMode == MachinePlaysBlack)) {
SendTimeRemaining(&first, gameMode != MachinePlaysBlack);
}
- if (gameMode != EditGame && gameMode != PlayFromGameFile) {
+ if (gameMode != EditGame && gameMode != PlayFromGameFile && gameMode != AnalyzeMode) {
// [HGM] book: if program might be playing, let it use book
bookHit = SendMoveToBookUser(forwardMostMove-1, &first, FALSE);
first.maybeThinking = TRUE;
- } else SendMoveToProgram(forwardMostMove-1, &first);
+ } else if(fromY == DROP_RANK && fromX == EmptySquare) {
+ if(!first.useSetboard) SendToProgram("undo\n", &first); // kludge to change stm in engines that do not support setboard
+ SendBoard(&first, currentMove+1);
+ if(second.analyzing) {
+ if(!second.useSetboard) SendToProgram("undo\n", &second);
+ SendBoard(&second, currentMove+1);
+ }
+ } else {
+ SendMoveToProgram(forwardMostMove-1, &first);
+ if(second.analyzing) SendMoveToProgram(forwardMostMove-1, &second);
+ }
if (currentMove == cmailOldMove + 1) {
cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
}
switch (gameMode) {
case EditGame:
- switch (MateTest(boards[currentMove], PosFlags(currentMove),
- EP_UNKNOWN, castlingRights[currentMove]) ) {
+ if(appData.testLegality)
+ switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
case MT_NONE:
case MT_CHECK:
break;
break;
}
break;
-
+
case MachinePlaysBlack:
case MachinePlaysWhite:
/* disable certain menu options while machine is thinking */
break;
}
+ userOfferedDraw = FALSE; // [HGM] drawclaim: after move made, and tested for claimable draw
+ promoDefaultAltered = FALSE; // [HGM] fall back on default choice
+
if(bookHit) { // [HGM] book: simulate book reply
static char bookMove[MSG_SIZ]; // a bit generous?
- programStats.nodes = programStats.depth = programStats.time =
+ programStats.nodes = programStats.depth = programStats.time =
programStats.score = programStats.got_only_move = 0;
sprintf(programStats.movelist, "%s (xbook)", bookHit);
- strcpy(bookMove, "move ");
+ safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
strcat(bookMove, bookHit);
HandleMachineMove(bookMove, &first);
}
}
void
-UserMoveEvent(fromX, fromY, toX, toY, promoChar)
- int fromX, fromY, toX, toY;
- int promoChar;
+MarkByFEN(char *fen)
{
- /* [HGM] This routine was added to allow calling of its two logical
- parts from other modules in the old way. Before, UserMoveEvent()
- automatically called FinishMove() if the move was OK, and returned
- otherwise. I separated the two, in order to make it possible to
- slip a promotion popup in between. But that it always needs two
- calls, to the first part, (now called UserMoveTest() ), and to
- FinishMove if the first part succeeded. Calls that do not need
- to do anything in between, can call this routine the old way.
- */
- ChessMove moveType = UserMoveTest(fromX, fromY, toX, toY, promoChar, FALSE);
-if(appData.debugMode) fprintf(debugFP, "moveType 4 = %d, promochar = %x\n", moveType, promoChar);
- if(moveType == AmbiguousMove)
+ int r, f;
+ if(!appData.markers || !appData.highlightDragging) return;
+ for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) legal[r][f] = 0;
+ r=BOARD_HEIGHT-1; f=BOARD_LEFT;
+ while(*fen) {
+ int s = 0;
+ marker[r][f] = 0;
+ if(*fen == 'M') legal[r][f] = 2; else // request promotion choice
+ if(*fen >= 'A' && *fen <= 'Z') legal[r][f] = 3; else
+ if(*fen >= 'a' && *fen <= 'z') *fen += 'A' - 'a';
+ if(*fen == '/' && f > BOARD_LEFT) f = BOARD_LEFT, r--; else
+ if(*fen == 'T') marker[r][f++] = 0; else
+ if(*fen == 'Y') marker[r][f++] = 1; else
+ if(*fen == 'G') marker[r][f++] = 3; else
+ if(*fen == 'B') marker[r][f++] = 4; else
+ if(*fen == 'C') marker[r][f++] = 5; else
+ if(*fen == 'M') marker[r][f++] = 6; else
+ if(*fen == 'W') marker[r][f++] = 7; else
+ if(*fen == 'D') marker[r][f++] = 8; else
+ if(*fen == 'R') marker[r][f++] = 2; else {
+ while(*fen <= '9' && *fen >= '0') s = 10*s + *fen++ - '0';
+ f += s; fen -= s>0;
+ }
+ while(f >= BOARD_RGHT) f -= BOARD_RGHT - BOARD_LEFT, r--;
+ if(r < 0) break;
+ fen++;
+ }
+ DrawPosition(TRUE, NULL);
+}
+
+static char baseMarker[BOARD_RANKS][BOARD_FILES], baseLegal[BOARD_RANKS][BOARD_FILES];
+
+void
+Mark (Board board, int flags, ChessMove kind, int rf, int ff, int rt, int ft, VOIDSTAR closure)
+{
+ typedef char Markers[BOARD_RANKS][BOARD_FILES];
+ Markers *m = (Markers *) closure;
+ if(rf == fromY && ff == fromX && (killX < 0 ? !(rt == rf && ft == ff) && legNr & 1 : rt == killY && ft == killX || legNr & 2))
+ (*m)[rt][ft] = 1 + (board[rt][ft] != EmptySquare
+ || kind == WhiteCapturesEnPassant
+ || kind == BlackCapturesEnPassant) + 3*(kind == FirstLeg && killX < 0), legal[rt][ft] = 3;
+ else if(flags & F_MANDATORY_CAPTURE && board[rt][ft] != EmptySquare) (*m)[rt][ft] = 3, legal[rt][ft] = 3;
+}
+
+static int hoverSavedValid;
+
+void
+MarkTargetSquares (int clear)
+{
+ int x, y, sum=0;
+ if(clear) { // no reason to ever suppress clearing
+ for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) sum += marker[y][x], marker[y][x] = 0;
+ hoverSavedValid = 0;
+ if(!sum) return; // nothing was cleared,no redraw needed
+ } else {
+ int capt = 0;
+ if(!appData.markers || !appData.highlightDragging || appData.icsActive && gameInfo.variant < VariantShogi ||
+ !appData.testLegality && !pieceDefs || gameMode == EditPosition) return;
+ GenLegal(boards[currentMove], PosFlags(currentMove), Mark, (void*) marker, EmptySquare);
+ if(PosFlags(0) & F_MANDATORY_CAPTURE) {
+ for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x]>1) capt++;
+ if(capt)
+ for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x] == 1) marker[y][x] = 0;
+ }
+ }
+ DrawPosition(FALSE, NULL);
+}
+
+int
+Explode (Board board, int fromX, int fromY, int toX, int toY)
+{
+ if(gameInfo.variant == VariantAtomic &&
+ (board[toY][toX] != EmptySquare || // capture?
+ toX != fromX && (board[fromY][fromX] == WhitePawn || // e.p. ?
+ board[fromY][fromX] == BlackPawn )
+ )) {
+ AnimateAtomicCapture(board, fromX, fromY, toX, toY);
+ return TRUE;
+ }
+ return FALSE;
+}
+
+ChessSquare gatingPiece = EmptySquare; // exported to front-end, for dragging
+
+int
+CanPromote (ChessSquare piece, int y)
+{
+ int zone = (gameInfo.variant == VariantChuChess ? 3 : 1);
+ if(gameMode == EditPosition) return FALSE; // no promotions when editing position
+ // some variants have fixed promotion piece, no promotion at all, or another selection mechanism
+ if(IS_SHOGI(gameInfo.variant) || gameInfo.variant == VariantXiangqi ||
+ gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat ||
+ gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier ||
+ gameInfo.variant == VariantMakruk) return FALSE;
+ return (piece == BlackPawn && y <= zone ||
+ piece == WhitePawn && y >= BOARD_HEIGHT-1-zone ||
+ piece == BlackLance && y <= zone ||
+ piece == WhiteLance && y >= BOARD_HEIGHT-1-zone );
+}
+
+void
+HoverEvent (int xPix, int yPix, int x, int y)
+{
+ static int oldX = -1, oldY = -1, oldFromX = -1, oldFromY = -1;
+ int r, f;
+ if(!first.highlight) return;
+ if(fromX != oldFromX || fromY != oldFromY) oldX = oldY = -1; // kludge to fake entry on from-click
+ if(x == oldX && y == oldY) return; // only do something if we enter new square
+ oldFromX = fromX; oldFromY = fromY;
+ if(oldX == -1 && oldY == -1 && x == fromX && y == fromY) { // record markings after from-change
+ for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++)
+ baseMarker[r][f] = marker[r][f], baseLegal[r][f] = legal[r][f];
+ hoverSavedValid = 1;
+ } else if(oldX != x || oldY != y) {
+ // [HGM] lift: entered new to-square; redraw arrow, and inform engine
+ if(hoverSavedValid) // don't restore markers that are supposed to be cleared
+ for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++)
+ marker[r][f] = baseMarker[r][f], legal[r][f] = baseLegal[r][f];
+ if((marker[y][x] == 2 || marker[y][x] == 6) && legal[y][x]) {
+ char buf[MSG_SIZ];
+ snprintf(buf, MSG_SIZ, "hover %c%d\n", x + AAA, y + ONE - '0');
+ SendToProgram(buf, &first);
+ }
+ oldX = x; oldY = y;
+// SetHighlights(fromX, fromY, x, y);
+ }
+}
+
+void ReportClick(char *action, int x, int y)
+{
+ char buf[MSG_SIZ]; // Inform engine of what user does
+ int r, f;
+ if(action[0] == 'l') // mark any target square of a lifted piece as legal to-square, clear markers
+ for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++)
+ legal[r][f] = !pieceDefs || !appData.markers, marker[r][f] = 0;
+ if(!first.highlight || gameMode == EditPosition) return;
+ snprintf(buf, MSG_SIZ, "%s %c%d%s\n", action, x+AAA, y+ONE-'0', controlKey && action[0]=='p' ? "," : "");
+ SendToProgram(buf, &first);
+}
+
+Boolean right; // instructs front-end to use button-1 events as if they were button 3
+
+void
+LeftClick (ClickType clickType, int xPix, int yPix)
+{
+ int x, y;
+ Boolean saveAnimate;
+ static int second = 0, promotionChoice = 0, clearFlag = 0, sweepSelecting = 0;
+ char promoChoice = NULLCHAR;
+ ChessSquare piece;
+ static TimeMark lastClickTime, prevClickTime;
+
+ x = EventToSquare(xPix, BOARD_WIDTH);
+ y = EventToSquare(yPix, BOARD_HEIGHT);
+ if (!flipView && y >= 0) {
+ y = BOARD_HEIGHT - 1 - y;
+ }
+ if (flipView && x >= 0) {
+ x = BOARD_WIDTH - 1 - x;
+ }
+
+ if(appData.monoMouse && gameMode == EditPosition && fromX < 0 && clickType == Press && boards[currentMove][y][x] == EmptySquare) {
+ static int dummy;
+ RightClick(clickType, xPix, yPix, &dummy, &dummy);
+ right = TRUE;
+ return;
+ }
+
+ if(SeekGraphClick(clickType, xPix, yPix, 0)) return;
+
+ prevClickTime = lastClickTime; GetTimeMark(&lastClickTime);
+
+ if (clickType == Press) ErrorPopDown();
+ lastClickType = clickType, lastLeftX = xPix, lastLeftY = yPix; // [HGM] alien: remember state
+
+ if(promoSweep != EmptySquare) { // up-click during sweep-select of promo-piece
+ defaultPromoChoice = promoSweep;
+ promoSweep = EmptySquare; // terminate sweep
+ promoDefaultAltered = TRUE;
+ if(!selectFlag && !sweepSelecting && (x != toX || y != toY)) x = fromX, y = fromY; // and fake up-click on same square if we were still selecting
+ }
+
+ if(promotionChoice) { // we are waiting for a click to indicate promotion piece
+ if(clickType == Release) return; // ignore upclick of click-click destination
+ promotionChoice = FALSE; // only one chance: if click not OK it is interpreted as cancel
+ if(appData.debugMode) fprintf(debugFP, "promotion click, x=%d, y=%d\n", x, y);
+ if(gameInfo.holdingsWidth &&
+ (WhiteOnMove(currentMove)
+ ? x == BOARD_WIDTH-1 && y < gameInfo.holdingsSize && y >= 0
+ : x == 0 && y >= BOARD_HEIGHT - gameInfo.holdingsSize && y < BOARD_HEIGHT) ) {
+ // click in right holdings, for determining promotion piece
+ ChessSquare p = boards[currentMove][y][x];
+ if(appData.debugMode) fprintf(debugFP, "square contains %d\n", (int)p);
+ if(p == WhitePawn || p == BlackPawn) p = EmptySquare; // [HGM] Pawns could be valid as deferral
+ if(p != EmptySquare || gameInfo.variant == VariantGrand && toY != 0 && toY != BOARD_HEIGHT-1) { // [HGM] grand: empty square means defer
+ FinishMove(NormalMove, fromX, fromY, toX, toY, p==EmptySquare ? NULLCHAR : ToLower(PieceToChar(p)));
+ fromX = fromY = -1;
+ return;
+ }
+ }
DrawPosition(FALSE, boards[currentMove]);
- else if(moveType != ImpossibleMove && moveType != Comment)
- FinishMove(moveType, fromX, fromY, toX, toY, promoChar);
+ return;
+ }
+
+ /* [HGM] holdings: next 5 lines: ignore all clicks between board and holdings */
+ if(clickType == Press
+ && ( x == BOARD_LEFT-1 || x == BOARD_RGHT
+ || x == BOARD_LEFT-2 && y < BOARD_HEIGHT-gameInfo.holdingsSize
+ || x == BOARD_RGHT+1 && y >= gameInfo.holdingsSize) )
+ return;
+
+ if(gotPremove && x == premoveFromX && y == premoveFromY && clickType == Release) {
+ // could be static click on premove from-square: abort premove
+ gotPremove = 0;
+ ClearPremoveHighlights();
+ }
+
+ if(clickType == Press && fromX == x && fromY == y && promoDefaultAltered && SubtractTimeMarks(&lastClickTime, &prevClickTime) >= 200)
+ fromX = fromY = -1; // second click on piece after altering default promo piece treated as first click
+
+ if(!promoDefaultAltered) { // determine default promotion piece, based on the side the user is moving for
+ int side = (gameMode == IcsPlayingWhite || gameMode == MachinePlaysBlack ||
+ gameMode != MachinePlaysWhite && gameMode != IcsPlayingBlack && WhiteOnMove(currentMove));
+ defaultPromoChoice = DefaultPromoChoice(side);
+ }
+
+ autoQueen = appData.alwaysPromoteToQueen;
+
+ if (fromX == -1) {
+ int originalY = y;
+ gatingPiece = EmptySquare;
+ if (clickType != Press) {
+ if(dragging) { // [HGM] from-square must have been reset due to game end since last press
+ DragPieceEnd(xPix, yPix); dragging = 0;
+ DrawPosition(FALSE, NULL);
+ }
+ return;
+ }
+ doubleClick = FALSE;
+ if(gameMode == AnalyzeMode && (pausing || controlKey) && first.excludeMoves) { // use pause state to exclude moves
+ doubleClick = TRUE; gatingPiece = boards[currentMove][y][x];
+ }
+ fromX = x; fromY = y; toX = toY = killX = killY = -1;
+ if(!appData.oneClick || !OnlyMove(&x, &y, FALSE) ||
+ // even if only move, we treat as normal when this would trigger a promotion popup, to allow sweep selection
+ appData.sweepSelect && CanPromote(boards[currentMove][fromY][fromX], fromY) && originalY != y) {
+ /* First square */
+ if (OKToStartUserMove(fromX, fromY)) {
+ second = 0;
+ ReportClick("lift", x, y);
+ MarkTargetSquares(0);
+ if(gameMode == EditPosition && controlKey) gatingPiece = boards[currentMove][fromY][fromX];
+ DragPieceBegin(xPix, yPix, FALSE); dragging = 1;
+ if(appData.sweepSelect && CanPromote(piece = boards[currentMove][fromY][fromX], fromY)) {
+ promoSweep = defaultPromoChoice;
+ selectFlag = 0; lastX = xPix; lastY = yPix; *promoRestrict = 0;
+ Sweep(0); // Pawn that is going to promote: preview promotion piece
+ DisplayMessage("", _("Pull pawn backwards to under-promote"));
+ }
+ if (appData.highlightDragging) {
+ SetHighlights(fromX, fromY, -1, -1);
+ } else {
+ ClearHighlights();
+ }
+ } else fromX = fromY = -1;
+ return;
+ }
+ }
+
+ /* fromX != -1 */
+ if (clickType == Press && gameMode != EditPosition) {
+ ChessSquare fromP;
+ ChessSquare toP;
+ int frc;
+
+ // ignore off-board to clicks
+ if(y < 0 || x < 0) return;
+
+ /* Check if clicking again on the same color piece */
+ fromP = boards[currentMove][fromY][fromX];
+ toP = boards[currentMove][y][x];
+ frc = appData.fischerCastling || gameInfo.variant == VariantSChess;
+ if( (killX < 0 || x != fromX || y != fromY) && // [HGM] lion: do not interpret igui as deselect!
+ marker[y][x] == 0 && // if engine told we can move to here, do it even if own piece
+ ((WhitePawn <= fromP && fromP <= WhiteKing &&
+ WhitePawn <= toP && toP <= WhiteKing &&
+ !(fromP == WhiteKing && toP == WhiteRook && frc) &&
+ !(fromP == WhiteRook && toP == WhiteKing && frc)) ||
+ (BlackPawn <= fromP && fromP <= BlackKing &&
+ BlackPawn <= toP && toP <= BlackKing &&
+ !(fromP == BlackRook && toP == BlackKing && frc) && // allow also RxK as FRC castling
+ !(fromP == BlackKing && toP == BlackRook && frc)))) {
+ /* Clicked again on same color piece -- changed his mind */
+ second = (x == fromX && y == fromY);
+ killX = killY = -1;
+ if(second && gameMode == AnalyzeMode && SubtractTimeMarks(&lastClickTime, &prevClickTime) < 200) {
+ second = FALSE; // first double-click rather than scond click
+ doubleClick = first.excludeMoves; // used by UserMoveEvent to recognize exclude moves
+ }
+ promoDefaultAltered = FALSE;
+ MarkTargetSquares(1);
+ if(!(second && appData.oneClick && OnlyMove(&x, &y, TRUE))) {
+ if (appData.highlightDragging) {
+ SetHighlights(x, y, -1, -1);
+ } else {
+ ClearHighlights();
+ }
+ if (OKToStartUserMove(x, y)) {
+ if(gameInfo.variant == VariantSChess && // S-Chess: back-rank piece selected after holdings means gating
+ (fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) &&
+ y == (toP < BlackPawn ? 0 : BOARD_HEIGHT-1))
+ gatingPiece = boards[currentMove][fromY][fromX];
+ else gatingPiece = doubleClick ? fromP : EmptySquare;
+ fromX = x;
+ fromY = y; dragging = 1;
+ if(!second) ReportClick("lift", x, y);
+ MarkTargetSquares(0);
+ DragPieceBegin(xPix, yPix, FALSE);
+ if(appData.sweepSelect && CanPromote(piece = boards[currentMove][y][x], y)) {
+ promoSweep = defaultPromoChoice;
+ selectFlag = 0; lastX = xPix; lastY = yPix; *promoRestrict = 0;
+ Sweep(0); // Pawn that is going to promote: preview promotion piece
+ }
+ }
+ }
+ if(x == fromX && y == fromY) return; // if OnlyMove altered (x,y) we go on
+ second = FALSE;
+ }
+ // ignore clicks on holdings
+ if(x < BOARD_LEFT || x >= BOARD_RGHT) return;
+ }
+
+ if(x == fromX && y == fromY && clickType == Press && gameMode == EditPosition && SubtractTimeMarks(&lastClickTime, &prevClickTime) < 200) {
+ gatingPiece = boards[currentMove][fromY][fromX]; // prepare to copy rather than move
+ DragPieceBegin(xPix, yPix, FALSE); dragging = 1;
+ return;
+ }
+
+ if (clickType == Release && x == fromX && y == fromY && killX < 0 && !sweepSelecting) {
+ DragPieceEnd(xPix, yPix); dragging = 0;
+ if(clearFlag) {
+ // a deferred attempt to click-click move an empty square on top of a piece
+ boards[currentMove][y][x] = EmptySquare;
+ ClearHighlights();
+ DrawPosition(FALSE, boards[currentMove]);
+ fromX = fromY = -1; clearFlag = 0;
+ return;
+ }
+ if (appData.animateDragging) {
+ /* Undo animation damage if any */
+ DrawPosition(FALSE, NULL);
+ }
+ if (second) {
+ /* Second up/down in same square; just abort move */
+ second = 0;
+ fromX = fromY = -1;
+ gatingPiece = EmptySquare;
+ MarkTargetSquares(1);
+ ClearHighlights();
+ gotPremove = 0;
+ ClearPremoveHighlights();
+ } else {
+ /* First upclick in same square; start click-click mode */
+ SetHighlights(x, y, -1, -1);
+ }
+ return;
+ }
+
+ clearFlag = 0;
+
+ if(gameMode != EditPosition && !appData.testLegality && !legal[y][x] &&
+ fromX >= BOARD_LEFT && fromX < BOARD_RGHT && (x != killX || y != killY) && !sweepSelecting) {
+ if(dragging) DragPieceEnd(xPix, yPix), dragging = 0;
+ DisplayMessage(_("only marked squares are legal"),"");
+ DrawPosition(TRUE, NULL);
+ return; // ignore to-click
+ }
+
+ /* we now have a different from- and (possibly off-board) to-square */
+ /* Completed move */
+ if(!sweepSelecting) {
+ toX = x;
+ toY = y;
+ }
+
+ piece = boards[currentMove][fromY][fromX];
+
+ saveAnimate = appData.animate;
+ if (clickType == Press) {
+ if(gameInfo.variant == VariantChuChess && piece != WhitePawn && piece != BlackPawn) defaultPromoChoice = piece;
+ if(gameMode == EditPosition && boards[currentMove][fromY][fromX] == EmptySquare) {
+ // must be Edit Position mode with empty-square selected
+ fromX = x; fromY = y; DragPieceBegin(xPix, yPix, FALSE); dragging = 1; // consider this a new attempt to drag
+ if(x >= BOARD_LEFT && x < BOARD_RGHT) clearFlag = 1; // and defer click-click move of empty-square to up-click
+ return;
+ }
+ if(dragging == 2) { // [HGM] lion: just turn buttonless drag into normal drag, and let release to the job
+ return;
+ }
+ if(x == killX && y == killY) { // second click on this square, which was selected as first-leg target
+ killX = killY = -1; // this informs us no second leg is coming, so treat as to-click without intermediate
+ } else
+ if(marker[y][x] == 5) return; // [HGM] lion: to-click on cyan square; defer action to release
+ if(legal[y][x] == 2 || HasPromotionChoice(fromX, fromY, toX, toY, &promoChoice, FALSE)) {
+ if(appData.sweepSelect) {
+ promoSweep = defaultPromoChoice;
+ if(gameInfo.variant != VariantChuChess && PieceToChar(CHUPROMOTED(piece)) == '+') promoSweep = CHUPROMOTED(piece);
+ selectFlag = 0; lastX = xPix; lastY = yPix;
+ ReportClick("put", x, y); // extra put to prompt engine for 'choice' command
+ Sweep(0); // Pawn that is going to promote: preview promotion piece
+ sweepSelecting = 1;
+ DisplayMessage("", _("Pull pawn backwards to under-promote"));
+ MarkTargetSquares(1);
+ }
+ return; // promo popup appears on up-click
+ }
+ /* Finish clickclick move */
+ if (appData.animate || appData.highlightLastMove) {
+ SetHighlights(fromX, fromY, toX, toY);
+ } else {
+ ClearHighlights();
+ }
+ } else if(sweepSelecting) { // this must be the up-click corresponding to the down-click that started the sweep
+ sweepSelecting = 0; appData.animate = FALSE; // do not animate, a selected piece already on to-square
+ *promoRestrict = 0;
+ if (appData.animate || appData.highlightLastMove) {
+ SetHighlights(fromX, fromY, toX, toY);
+ } else {
+ ClearHighlights();
+ }
+ } else {
+#if 0
+// [HGM] this must be done after the move is made, as with arrow it could lead to a board redraw with piece still on from square
+ /* Finish drag move */
+ if (appData.highlightLastMove) {
+ SetHighlights(fromX, fromY, toX, toY);
+ } else {
+ ClearHighlights();
+ }
+#endif
+ if(gameInfo.variant == VariantChuChess && piece != WhitePawn && piece != BlackPawn) defaultPromoChoice = piece;
+ if(marker[y][x] == 5) { // [HGM] lion: this was the release of a to-click or drag on a cyan square
+ dragging *= 2; // flag button-less dragging if we are dragging
+ MarkTargetSquares(1);
+ if(x == killX && y == killY) killX = kill2X, killY = kill2Y, kill2X = kill2Y = -1; // cancel last kill
+ else {
+ kill2X = killX; kill2Y = killY;
+ killX = x; killY = y; //remeber this square as intermediate
+ ReportClick("put", x, y); // and inform engine
+ ReportClick("lift", x, y);
+ MarkTargetSquares(0);
+ return;
+ }
+ }
+ DragPieceEnd(xPix, yPix); dragging = 0;
+ /* Don't animate move and drag both */
+ appData.animate = FALSE;
+ }
+
+ // moves into holding are invalid for now (except in EditPosition, adapting to-square)
+ if(x >= 0 && x < BOARD_LEFT || x >= BOARD_RGHT) {
+ ChessSquare piece = boards[currentMove][fromY][fromX];
+ if(gameMode == EditPosition && piece != EmptySquare &&
+ fromX >= BOARD_LEFT && fromX < BOARD_RGHT) {
+ int n;
+
+ if(x == BOARD_LEFT-2 && piece >= BlackPawn) {
+ n = PieceToNumber(piece - (int)BlackPawn);
+ if(n >= gameInfo.holdingsSize) { n = 0; piece = BlackPawn; }
+ boards[currentMove][BOARD_HEIGHT-1 - n][0] = piece;
+ boards[currentMove][BOARD_HEIGHT-1 - n][1]++;
+ } else
+ if(x == BOARD_RGHT+1 && piece < BlackPawn) {
+ n = PieceToNumber(piece);
+ if(n >= gameInfo.holdingsSize) { n = 0; piece = WhitePawn; }
+ boards[currentMove][n][BOARD_WIDTH-1] = piece;
+ boards[currentMove][n][BOARD_WIDTH-2]++;
+ }
+ boards[currentMove][fromY][fromX] = EmptySquare;
+ }
+ ClearHighlights();
+ fromX = fromY = -1;
+ MarkTargetSquares(1);
+ DrawPosition(TRUE, boards[currentMove]);
+ return;
+ }
+
+ // off-board moves should not be highlighted
+ if(x < 0 || y < 0) ClearHighlights();
+ else ReportClick("put", x, y);
+
+ if(gatingPiece != EmptySquare && gameInfo.variant == VariantSChess) promoChoice = ToLower(PieceToChar(gatingPiece));
+
+ if(legal[toY][toX] == 2) promoChoice = ToLower(PieceToChar(defaultPromoChoice)); // highlight-induced promotion
+
+ if (legal[toY][toX] == 2 && !appData.sweepSelect || HasPromotionChoice(fromX, fromY, toX, toY, &promoChoice, appData.sweepSelect)) {
+ SetHighlights(fromX, fromY, toX, toY);
+ MarkTargetSquares(1);
+ if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) {
+ // [HGM] super: promotion to captured piece selected from holdings
+ ChessSquare p = boards[currentMove][fromY][fromX], q = boards[currentMove][toY][toX];
+ promotionChoice = TRUE;
+ // kludge follows to temporarily execute move on display, without promoting yet
+ boards[currentMove][fromY][fromX] = EmptySquare; // move Pawn to 8th rank
+ boards[currentMove][toY][toX] = p;
+ DrawPosition(FALSE, boards[currentMove]);
+ boards[currentMove][fromY][fromX] = p; // take back, but display stays
+ boards[currentMove][toY][toX] = q;
+ DisplayMessage("Click in holdings to choose piece", "");
+ return;
+ }
+ PromotionPopUp(promoChoice);
+ } else {
+ int oldMove = currentMove;
+ UserMoveEvent(fromX, fromY, toX, toY, promoChoice);
+ if (!appData.highlightLastMove || gotPremove) ClearHighlights();
+ if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);
+ if(saveAnimate && !appData.animate && currentMove != oldMove && // drag-move was performed
+ Explode(boards[currentMove-1], fromX, fromY, toX, toY))
+ DrawPosition(TRUE, boards[currentMove]);
+ MarkTargetSquares(1);
+ fromX = fromY = -1;
+ }
+ appData.animate = saveAnimate;
+ if (appData.animate || appData.animateDragging) {
+ /* Undo animation damage if needed */
+ DrawPosition(FALSE, NULL);
+ }
}
-void SendProgramStatsToFrontend( ChessProgramState * cps, ChessProgramStats * cpstats )
+int
+RightClick (ClickType action, int x, int y, int *fromX, int *fromY)
+{ // front-end-free part taken out of PieceMenuPopup
+ int whichMenu; int xSqr, ySqr;
+
+ if(seekGraphUp) { // [HGM] seekgraph
+ if(action == Press) SeekGraphClick(Press, x, y, 2); // 2 indicates right-click: no pop-down on miss
+ if(action == Release) SeekGraphClick(Release, x, y, 2); // and no challenge on hit
+ return -2;
+ }
+
+ if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)
+ && !appData.zippyPlay && appData.bgObserve) { // [HGM] bughouse: show background game
+ if(!partnerBoardValid) return -2; // suppress display of uninitialized boards
+ if( appData.dualBoard) return -2; // [HGM] dual: is already displayed
+ if(action == Press) {
+ originalFlip = flipView;
+ flipView = !flipView; // temporarily flip board to see game from partners perspective
+ DrawPosition(TRUE, partnerBoard);
+ DisplayMessage(partnerStatus, "");
+ partnerUp = TRUE;
+ } else if(action == Release) {
+ flipView = originalFlip;
+ DrawPosition(TRUE, boards[currentMove]);
+ partnerUp = FALSE;
+ }
+ return -2;
+ }
+
+ xSqr = EventToSquare(x, BOARD_WIDTH);
+ ySqr = EventToSquare(y, BOARD_HEIGHT);
+ if (action == Release) {
+ if(pieceSweep != EmptySquare) {
+ EditPositionMenuEvent(pieceSweep, toX, toY);
+ pieceSweep = EmptySquare;
+ } else UnLoadPV(); // [HGM] pv
+ }
+ if (action != Press) return -2; // return code to be ignored
+ switch (gameMode) {
+ case IcsExamining:
+ if(xSqr < BOARD_LEFT || xSqr >= BOARD_RGHT) return -1;
+ case EditPosition:
+ if (xSqr == BOARD_LEFT-1 || xSqr == BOARD_RGHT) return -1;
+ if (xSqr < 0 || ySqr < 0) return -1;
+ if(appData.pieceMenu) { whichMenu = 0; break; } // edit-position menu
+ pieceSweep = shiftKey ? BlackPawn : WhitePawn; // [HGM] sweep: prepare selecting piece by mouse sweep
+ toX = xSqr; toY = ySqr; lastX = x, lastY = y;
+ if(flipView) toX = BOARD_WIDTH - 1 - toX; else toY = BOARD_HEIGHT - 1 - toY;
+ NextPiece(0);
+ return 2; // grab
+ case IcsObserving:
+ if(!appData.icsEngineAnalyze) return -1;
+ case IcsPlayingWhite:
+ case IcsPlayingBlack:
+ if(!appData.zippyPlay) goto noZip;
+ case AnalyzeMode:
+ case AnalyzeFile:
+ case MachinePlaysWhite:
+ case MachinePlaysBlack:
+ case TwoMachinesPlay: // [HGM] pv: use for showing PV
+ if (!appData.dropMenu) {
+ LoadPV(x, y);
+ return 2; // flag front-end to grab mouse events
+ }
+ if(gameMode == TwoMachinesPlay || gameMode == AnalyzeMode ||
+ gameMode == AnalyzeFile || gameMode == IcsObserving) return -1;
+ case EditGame:
+ noZip:
+ if (xSqr < 0 || ySqr < 0) return -1;
+ if (!appData.dropMenu || appData.testLegality &&
+ gameInfo.variant != VariantBughouse &&
+ gameInfo.variant != VariantCrazyhouse) return -1;
+ whichMenu = 1; // drop menu
+ break;
+ default:
+ return -1;
+ }
+
+ if (((*fromX = xSqr) < 0) ||
+ ((*fromY = ySqr) < 0)) {
+ *fromX = *fromY = -1;
+ return -1;
+ }
+ if (flipView)
+ *fromX = BOARD_WIDTH - 1 - *fromX;
+ else
+ *fromY = BOARD_HEIGHT - 1 - *fromY;
+
+ return whichMenu;
+}
+
+void
+SendProgramStatsToFrontend (ChessProgramState * cps, ChessProgramStats * cpstats)
{
// char * hint = lastHint;
FrontEndProgramStats stats;
stats.an_move_count = cpstats->nr_moves;
}
+ 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
+
SetProgramStats( &stats );
}
-char *SendMoveToBookUser(int moveNr, ChessProgramState *cps, int initial)
+void
+ClearEngineOutputPane (int which)
+{
+ static FrontEndProgramStats dummyStats;
+ dummyStats.which = which;
+ dummyStats.pv = "#";
+ SetProgramStats( &dummyStats );
+}
+
+#define MAXPLAYERS 500
+
+char *
+TourneyStandings (int display)
+{
+ int i, w, b, color, wScore, bScore, dummy, nr=0, nPlayers=0;
+ int score[MAXPLAYERS], ranking[MAXPLAYERS], points[MAXPLAYERS], games[MAXPLAYERS];
+ char result, *p, *names[MAXPLAYERS];
+
+ if(appData.tourneyType < 0 && !strchr(appData.results, '*'))
+ return strdup(_("Swiss tourney finished")); // standings of Swiss yet TODO
+ names[0] = p = strdup(appData.participants);
+ while(p = strchr(p, '\n')) *p++ = NULLCHAR, names[++nPlayers] = p; // count participants
+
+ for(i=0; i<nPlayers; i++) score[i] = games[i] = 0;
+
+ while(result = appData.results[nr]) {
+ color = Pairing(nr, nPlayers, &w, &b, &dummy);
+ if(!(color ^ matchGame & 1)) { dummy = w; w = b; b = dummy; }
+ wScore = bScore = 0;
+ switch(result) {
+ case '+': wScore = 2; break;
+ case '-': bScore = 2; break;
+ case '=': wScore = bScore = 1; break;
+ case ' ':
+ case '*': return strdup("busy"); // tourney not finished
+ }
+ score[w] += wScore;
+ score[b] += bScore;
+ games[w]++;
+ games[b]++;
+ nr++;
+ }
+ if(appData.tourneyType > 0) nPlayers = appData.tourneyType; // in gauntlet, list only gauntlet engine(s)
+ for(w=0; w<nPlayers; w++) {
+ bScore = -1;
+ for(i=0; i<nPlayers; i++) if(score[i] > bScore) bScore = score[i], b = i;
+ ranking[w] = b; points[w] = bScore; score[b] = -2;
+ }
+ p = malloc(nPlayers*34+1);
+ for(w=0; w<nPlayers && w<display; w++)
+ sprintf(p+34*w, "%2d. %5.1f/%-3d %-19.19s\n", w+1, points[w]/2., games[ranking[w]], names[ranking[w]]);
+ free(names[0]);
+ return p;
+}
+
+void
+Count (Board board, int pCnt[], int *nW, int *nB, int *wStale, int *bStale, int *bishopColor)
+{ // count all piece types
+ int p, f, r;
+ *nB = *nW = *wStale = *bStale = *bishopColor = 0;
+ for(p=WhitePawn; p<=EmptySquare; p++) pCnt[p] = 0;
+ for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
+ p = board[r][f];
+ pCnt[p]++;
+ if(p == WhitePawn && r == BOARD_HEIGHT-1) (*wStale)++; else
+ if(p == BlackPawn && r == 0) (*bStale)++; // count last-Rank Pawns (XQ) separately
+ if(p <= WhiteKing) (*nW)++; else if(p <= BlackKing) (*nB)++;
+ if(p == WhiteBishop || p == WhiteFerz || p == WhiteAlfil ||
+ p == BlackBishop || p == BlackFerz || p == BlackAlfil )
+ *bishopColor |= 1 << ((f^r)&1); // track square color of color-bound pieces
+ }
+}
+
+int
+SufficientDefence (int pCnt[], int side, int nMine, int nHis)
+{
+ int myPawns = pCnt[WhitePawn+side]; // my total Pawn count;
+ int majorDefense = pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackKnight-side];
+
+ nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side]; // discount defenders
+ if(nMine - myPawns > 2) return FALSE; // no trivial draws with more than 1 major
+ if(myPawns == 2 && nMine == 3) // KPP
+ return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 3;
+ if(myPawns == 1 && nMine == 2) // KP
+ return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] + pCnt[BlackPawn-side] >= 1;
+ if(myPawns == 1 && nMine == 3 && pCnt[WhiteKnight+side]) // KHP
+ return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side]*2 >= 5;
+ if(myPawns) return FALSE;
+ if(pCnt[WhiteRook+side])
+ return pCnt[BlackRook-side] ||
+ pCnt[BlackCannon-side] && (pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] >= 2) ||
+ pCnt[BlackKnight-side] && pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] > 2 ||
+ pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 4;
+ if(pCnt[WhiteCannon+side]) {
+ if(pCnt[WhiteFerz+side] + myPawns == 0) return TRUE; // Cannon needs platform
+ return majorDefense || pCnt[BlackAlfil-side] >= 2;
+ }
+ if(pCnt[WhiteKnight+side])
+ return majorDefense || pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] + pCnt[BlackPawn-side] >= 1;
+ return FALSE;
+}
+
+int
+MatingPotential (int pCnt[], int side, int nMine, int nHis, int stale, int bisColor)
+{
+ VariantClass v = gameInfo.variant;
+
+ if(v == VariantShogi || v == VariantCrazyhouse || v == VariantBughouse) return TRUE; // drop games always winnable
+ if(v == VariantShatranj) return TRUE; // always winnable through baring
+ if(v == VariantLosers || v == VariantSuicide || v == VariantGiveaway) return TRUE;
+ if(v == Variant3Check || v == VariantAtomic) return nMine > 1; // can win through checking / exploding King
+
+ if(v == VariantXiangqi) {
+ int majors = 5*pCnt[BlackKnight-side] + 7*pCnt[BlackCannon-side] + 7*pCnt[BlackRook-side];
+
+ nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side] + stale; // discount defensive pieces and back-rank Pawns
+ if(nMine + stale == 1) return (pCnt[BlackFerz-side] > 1 && pCnt[BlackKnight-side] > 0); // bare K can stalemate KHAA (!)
+ if(nMine > 2) return TRUE; // if we don't have P, H or R, we must have CC
+ if(nMine == 2 && pCnt[WhiteCannon+side] == 0) return TRUE; // We have at least one P, H or R
+ // if we get here, we must have KC... or KP..., possibly with additional A, E or last-rank P
+ if(stale) // we have at least one last-rank P plus perhaps C
+ return majors // KPKX
+ || pCnt[BlackFerz-side] && pCnt[BlackFerz-side] + pCnt[WhiteCannon+side] + stale > 2; // KPKAA, KPPKA and KCPKA
+ else // KCA*E*
+ return pCnt[WhiteFerz+side] // KCAK
+ || pCnt[WhiteAlfil+side] && pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackFerz-side] // KCEKA, KCEKX (X!=H)
+ || majors + (12*pCnt[BlackFerz-side] | 6*pCnt[BlackAlfil-side]) > 16; // KCKAA, KCKAX, KCKEEX, KCKEXX (XX!=HH), KCKXXX
+ // TO DO: cases wih an unpromoted f-Pawn acting as platform for an opponent Cannon
+
+ } else if(v == VariantKnightmate) {
+ if(nMine == 1) return FALSE;
+ if(nMine == 2 && nHis == 1 && pCnt[WhiteBishop+side] + pCnt[WhiteFerz+side] + pCnt[WhiteKnight+side]) return FALSE; // KBK is only draw
+ } else if(pCnt[WhiteKing] == 1 && pCnt[BlackKing] == 1) { // other variants with orthodox Kings
+ int nBishops = pCnt[WhiteBishop+side] + pCnt[WhiteFerz+side];
+
+ if(nMine == 1) return FALSE; // bare King
+ 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
+ nMine += (nBishops > 0) - nBishops; // By now all Bishops (and Ferz) on like-colored squares, so count as one
+ if(nMine > 2 && nMine != pCnt[WhiteAlfil+side] + 1) return TRUE; // At least two pieces, not all Alfils
+ // by now we have King + 1 piece (or multiple Bishops on the same color)
+ if(pCnt[WhiteKnight+side])
+ return (pCnt[BlackKnight-side] + pCnt[BlackBishop-side] + pCnt[BlackMan-side] +
+ pCnt[BlackWazir-side] + pCnt[BlackSilver-side] + bisColor // KNKN, KNKB, KNKF, KNKE, KNKW, KNKM, KNKS
+ || nHis > 3); // be sure to cover suffocation mates in corner (e.g. KNKQCA)
+ if(nBishops)
+ return (pCnt[BlackKnight-side]); // KBKN, KFKN
+ if(pCnt[WhiteAlfil+side])
+ return (nHis > 2); // Alfils can in general not reach a corner square, but there might be edge (suffocation) mates
+ if(pCnt[WhiteWazir+side])
+ return (pCnt[BlackKnight-side] + pCnt[BlackWazir-side] + pCnt[BlackAlfil-side]); // KWKN, KWKW, KWKE
+ }
+
+ return TRUE;
+}
+
+int
+CompareWithRights (Board b1, Board b2)
+{
+ int rights = 0;
+ if(!CompareBoards(b1, b2)) return FALSE;
+ if(b1[EP_STATUS] != b2[EP_STATUS]) return FALSE;
+ /* compare castling rights */
+ if( b1[CASTLING][2] != b2[CASTLING][2] && (b2[CASTLING][0] != NoRights || b2[CASTLING][1] != NoRights) )
+ rights++; /* King lost rights, while rook still had them */
+ if( b1[CASTLING][2] != NoRights ) { /* king has rights */
+ if( b1[CASTLING][0] != b2[CASTLING][0] || b1[CASTLING][1] != b2[CASTLING][1] )
+ rights++; /* but at least one rook lost them */
+ }
+ if( b1[CASTLING][5] != b1[CASTLING][5] && (b2[CASTLING][3] != NoRights || b2[CASTLING][4] != NoRights) )
+ rights++;
+ if( b1[CASTLING][5] != NoRights ) {
+ if( b1[CASTLING][3] != b2[CASTLING][3] || b1[CASTLING][4] != b2[CASTLING][4] )
+ rights++;
+ }
+ return rights == 0;
+}
+
+int
+Adjudicate (ChessProgramState *cps)
+{ // [HGM] some adjudications useful with buggy engines
+ // [HGM] adjudicate: made into separate routine, which now can be called after every move
+ // In any case it determnes if the game is a claimable draw (filling in EP_STATUS).
+ // Actually ending the game is now based on the additional internal condition canAdjudicate.
+ // Only when the game is ended, and the opponent is a computer, this opponent gets the move relayed.
+ int k, drop, count = 0; static int bare = 1;
+ ChessProgramState *engineOpponent = (gameMode == TwoMachinesPlay ? cps->other : (cps ? NULL : &first));
+ Boolean canAdjudicate = !appData.icsActive;
+
+ // most tests only when we understand the game, i.e. legality-checking on
+ if( appData.testLegality )
+ { /* [HGM] Some more adjudications for obstinate engines */
+ int nrW, nrB, bishopColor, staleW, staleB, nr[EmptySquare+2], i;
+ static int moveCount = 6;
+ ChessMove result;
+ char *reason = NULL;
+
+ /* Count what is on board. */
+ Count(boards[forwardMostMove], nr, &nrW, &nrB, &staleW, &staleB, &bishopColor);
+
+ /* Some material-based adjudications that have to be made before stalemate test */
+ if(gameInfo.variant == VariantAtomic && nr[WhiteKing] + nr[BlackKing] < 2) {
+ // [HGM] atomic: stm must have lost his King on previous move, as destroying own K is illegal
+ boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // make claimable as if stm is checkmated
+ if(canAdjudicate && appData.checkMates) {
+ if(engineOpponent)
+ SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
+ GameEnds( WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins,
+ "Xboard adjudication: King destroyed", GE_XBOARD );
+ return 1;
+ }
+ }
+
+ /* Bare King in Shatranj (loses) or Losers (wins) */
+ if( nrW == 1 || nrB == 1) {
+ if( gameInfo.variant == VariantLosers) { // [HGM] losers: bare King wins (stm must have it first)
+ boards[forwardMostMove][EP_STATUS] = EP_WINS; // mark as win, so it becomes claimable
+ if(canAdjudicate && appData.checkMates) {
+ if(engineOpponent)
+ SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets to see move
+ GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
+ "Xboard adjudication: Bare king", GE_XBOARD );
+ return 1;
+ }
+ } else
+ if( gameInfo.variant == VariantShatranj && --bare < 0)
+ { /* bare King */
+ boards[forwardMostMove][EP_STATUS] = EP_WINS; // make claimable as win for stm
+ if(canAdjudicate && appData.checkMates) {
+ /* but only adjudicate if adjudication enabled */
+ if(engineOpponent)
+ SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
+ GameEnds( nrW > 1 ? WhiteWins : nrB > 1 ? BlackWins : GameIsDrawn,
+ "Xboard adjudication: Bare king", GE_XBOARD );
+ return 1;
+ }
+ }
+ } else bare = 1;
+
+
+ // don't wait for engine to announce game end if we can judge ourselves
+ switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
+ case MT_CHECK:
+ if(gameInfo.variant == Variant3Check) { // [HGM] 3check: when in check, test if 3rd time
+ int i, checkCnt = 0; // (should really be done by making nr of checks part of game state)
+ for(i=forwardMostMove-2; i>=backwardMostMove; i-=2) {
+ if(MateTest(boards[i], PosFlags(i)) == MT_CHECK)
+ checkCnt++;
+ if(checkCnt >= 2) {
+ reason = "Xboard adjudication: 3rd check";
+ boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE;
+ break;
+ }
+ }
+ }
+ case MT_NONE:
+ default:
+ break;
+ case MT_STEALMATE:
+ case MT_STALEMATE:
+ case MT_STAINMATE:
+ reason = "Xboard adjudication: Stalemate";
+ if((signed char)boards[forwardMostMove][EP_STATUS] != EP_CHECKMATE) { // [HGM] don't touch win through baring or K-capt
+ boards[forwardMostMove][EP_STATUS] = EP_STALEMATE; // default result for stalemate is draw
+ if(gameInfo.variant == VariantLosers || gameInfo.variant == VariantGiveaway) // [HGM] losers:
+ boards[forwardMostMove][EP_STATUS] = EP_WINS; // in these variants stalemated is always a win
+ else if(gameInfo.variant == VariantSuicide) // in suicide it depends
+ boards[forwardMostMove][EP_STATUS] = nrW == nrB ? EP_STALEMATE :
+ ((nrW < nrB) != WhiteOnMove(forwardMostMove) ?
+ EP_CHECKMATE : EP_WINS);
+ else if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantXiangqi || gameInfo.variant == VariantShogi)
+ boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // and in these variants being stalemated loses
+ }
+ break;
+ case MT_CHECKMATE:
+ reason = "Xboard adjudication: Checkmate";
+ boards[forwardMostMove][EP_STATUS] = (gameInfo.variant == VariantLosers ? EP_WINS : EP_CHECKMATE);
+ if(gameInfo.variant == VariantShogi) {
+ if(forwardMostMove > backwardMostMove
+ && moveList[forwardMostMove-1][1] == '@'
+ && CharToPiece(ToUpper(moveList[forwardMostMove-1][0])) == WhitePawn) {
+ reason = "XBoard adjudication: pawn-drop mate";
+ boards[forwardMostMove][EP_STATUS] = EP_WINS;
+ }
+ }
+ break;
+ }
+
+ switch(i = (signed char)boards[forwardMostMove][EP_STATUS]) {
+ case EP_STALEMATE:
+ result = GameIsDrawn; break;
+ case EP_CHECKMATE:
+ result = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins; break;
+ case EP_WINS:
+ result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins; break;
+ default:
+ result = EndOfFile;
+ }
+ if(canAdjudicate && appData.checkMates && result) { // [HGM] mates: adjudicate finished games if requested
+ if(engineOpponent)
+ SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
+ GameEnds( result, reason, GE_XBOARD );
+ return 1;
+ }
+
+ /* Next absolutely insufficient mating material. */
+ if(!MatingPotential(nr, WhitePawn, nrW, nrB, staleW, bishopColor) &&
+ !MatingPotential(nr, BlackPawn, nrB, nrW, staleB, bishopColor))
+ { /* includes KBK, KNK, KK of KBKB with like Bishops */
+
+ /* always flag draws, for judging claims */
+ boards[forwardMostMove][EP_STATUS] = EP_INSUF_DRAW;
+
+ if(canAdjudicate && appData.materialDraws) {
+ /* but only adjudicate them if adjudication enabled */
+ if(engineOpponent) {
+ SendToProgram("force\n", engineOpponent); // suppress reply
+ SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see last move */
+ }
+ GameEnds( GameIsDrawn, "Xboard adjudication: Insufficient mating material", GE_XBOARD );
+ return 1;
+ }
+ }
+
+ /* Then some trivial draws (only adjudicate, cannot be claimed) */
+ if(gameInfo.variant == VariantXiangqi ?
+ SufficientDefence(nr, WhitePawn, nrW, nrB) && SufficientDefence(nr, BlackPawn, nrB, nrW)
+ : nrW + nrB == 4 &&
+ ( nr[WhiteRook] == 1 && nr[BlackRook] == 1 /* KRKR */
+ || nr[WhiteQueen] && nr[BlackQueen]==1 /* KQKQ */
+ || nr[WhiteKnight]==2 || nr[BlackKnight]==2 /* KNNK */
+ || nr[WhiteKnight]+nr[WhiteBishop] == 1 && nr[BlackKnight]+nr[BlackBishop] == 1 /* KBKN, KBKB, KNKN */
+ ) ) {
+ if(--moveCount < 0 && appData.trivialDraws && canAdjudicate)
+ { /* if the first 3 moves do not show a tactical win, declare draw */
+ if(engineOpponent) {
+ SendToProgram("force\n", engineOpponent); // suppress reply
+ SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
+ }
+ GameEnds( GameIsDrawn, "Xboard adjudication: Trivial draw", GE_XBOARD );
+ return 1;
+ }
+ } else moveCount = 6;
+ }
+
+ // Repetition draws and 50-move rule can be applied independently of legality testing
+
+ /* Check for rep-draws */
+ count = 0;
+ drop = gameInfo.holdingsSize && (gameInfo.variant != VariantSuper && gameInfo.variant != VariantSChess
+ && gameInfo.variant != VariantGreat && gameInfo.variant != VariantGrand);
+ for(k = forwardMostMove-2;
+ k>=backwardMostMove && k>=forwardMostMove-100 && (drop ||
+ (signed char)boards[k][EP_STATUS] < EP_UNKNOWN &&
+ (signed char)boards[k+2][EP_STATUS] <= EP_NONE && (signed char)boards[k+1][EP_STATUS] <= EP_NONE);
+ k-=2)
+ { int rights=0;
+ if(CompareBoards(boards[k], boards[forwardMostMove])) {
+ /* compare castling rights */
+ if( boards[forwardMostMove][CASTLING][2] != boards[k][CASTLING][2] &&
+ (boards[k][CASTLING][0] != NoRights || boards[k][CASTLING][1] != NoRights) )
+ rights++; /* King lost rights, while rook still had them */
+ if( boards[forwardMostMove][CASTLING][2] != NoRights ) { /* king has rights */
+ if( boards[forwardMostMove][CASTLING][0] != boards[k][CASTLING][0] ||
+ boards[forwardMostMove][CASTLING][1] != boards[k][CASTLING][1] )
+ rights++; /* but at least one rook lost them */
+ }
+ if( boards[forwardMostMove][CASTLING][5] != boards[k][CASTLING][5] &&
+ (boards[k][CASTLING][3] != NoRights || boards[k][CASTLING][4] != NoRights) )
+ rights++;
+ if( boards[forwardMostMove][CASTLING][5] != NoRights ) {
+ if( boards[forwardMostMove][CASTLING][3] != boards[k][CASTLING][3] ||
+ boards[forwardMostMove][CASTLING][4] != boards[k][CASTLING][4] )
+ rights++;
+ }
+ if( rights == 0 && ++count > appData.drawRepeats-2 && canAdjudicate
+ && appData.drawRepeats > 1) {
+ /* adjudicate after user-specified nr of repeats */
+ int result = GameIsDrawn;
+ char *details = "XBoard adjudication: repetition draw";
+ if((gameInfo.variant == VariantXiangqi || gameInfo.variant == VariantShogi) && appData.testLegality) {
+ // [HGM] xiangqi: check for forbidden perpetuals
+ int m, ourPerpetual = 1, hisPerpetual = 1;
+ for(m=forwardMostMove; m>k; m-=2) {
+ if(MateTest(boards[m], PosFlags(m)) != MT_CHECK)
+ ourPerpetual = 0; // the current mover did not always check
+ if(MateTest(boards[m-1], PosFlags(m-1)) != MT_CHECK)
+ hisPerpetual = 0; // the opponent did not always check
+ }
+ if(appData.debugMode) fprintf(debugFP, "XQ perpetual test, our=%d, his=%d\n",
+ ourPerpetual, hisPerpetual);
+ if(ourPerpetual && !hisPerpetual) { // we are actively checking him: forfeit
+ result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
+ details = "Xboard adjudication: perpetual checking";
+ } else
+ if(hisPerpetual && !ourPerpetual) { // he is checking us, but did not repeat yet
+ break; // (or we would have caught him before). Abort repetition-checking loop.
+ } else
+ if(gameInfo.variant == VariantShogi) { // in Shogi other repetitions are draws
+ if(BOARD_HEIGHT == 5 && BOARD_RGHT - BOARD_LEFT == 5) { // but in mini-Shogi gote wins!
+ result = BlackWins;
+ details = "Xboard adjudication: repetition";
+ }
+ } else // it must be XQ
+ // Now check for perpetual chases
+ if(!ourPerpetual && !hisPerpetual) { // no perpetual check, test for chase
+ hisPerpetual = PerpetualChase(k, forwardMostMove);
+ ourPerpetual = PerpetualChase(k+1, forwardMostMove);
+ if(ourPerpetual && !hisPerpetual) { // we are actively chasing him: forfeit
+ static char resdet[MSG_SIZ];
+ result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
+ details = resdet;
+ snprintf(resdet, MSG_SIZ, "Xboard adjudication: perpetual chasing of %c%c", ourPerpetual>>8, ourPerpetual&255);
+ } else
+ if(hisPerpetual && !ourPerpetual) // he is chasing us, but did not repeat yet
+ break; // Abort repetition-checking loop.
+ }
+ // if neither of us is checking or chasing all the time, or both are, it is draw
+ }
+ if(engineOpponent) {
+ SendToProgram("force\n", engineOpponent); // suppress reply
+ SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
+ }
+ GameEnds( result, details, GE_XBOARD );
+ return 1;
+ }
+ if( rights == 0 && count > 1 ) /* occurred 2 or more times before */
+ boards[forwardMostMove][EP_STATUS] = EP_REP_DRAW;
+ }
+ }
+
+ /* Now we test for 50-move draws. Determine ply count */
+ count = forwardMostMove;
+ /* look for last irreversble move */
+ while( (signed char)boards[count][EP_STATUS] <= EP_NONE && count > backwardMostMove )
+ count--;
+ /* if we hit starting position, add initial plies */
+ if( count == backwardMostMove )
+ count -= initialRulePlies;
+ count = forwardMostMove - count;
+ if(gameInfo.variant == VariantXiangqi && ( count >= 100 || count >= 2*appData.ruleMoves ) ) {
+ // adjust reversible move counter for checks in Xiangqi
+ int i = forwardMostMove - count, inCheck = 0, lastCheck;
+ if(i < backwardMostMove) i = backwardMostMove;
+ while(i <= forwardMostMove) {
+ lastCheck = inCheck; // check evasion does not count
+ inCheck = (MateTest(boards[i], PosFlags(i)) == MT_CHECK);
+ if(inCheck || lastCheck) count--; // check does not count
+ i++;
+ }
+ }
+ if( count >= 100)
+ boards[forwardMostMove][EP_STATUS] = EP_RULE_DRAW;
+ /* this is used to judge if draw claims are legal */
+ if(canAdjudicate && appData.ruleMoves > 0 && count >= 2*appData.ruleMoves) {
+ if(engineOpponent) {
+ SendToProgram("force\n", engineOpponent); // suppress reply
+ SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
+ }
+ GameEnds( GameIsDrawn, "Xboard adjudication: 50-move rule", GE_XBOARD );
+ return 1;
+ }
+
+ /* if draw offer is pending, treat it as a draw claim
+ * when draw condition present, to allow engines a way to
+ * claim draws before making their move to avoid a race
+ * condition occurring after their move
+ */
+ if((gameMode == TwoMachinesPlay ? second.offeredDraw : userOfferedDraw) || first.offeredDraw ) {
+ char *p = NULL;
+ if((signed char)boards[forwardMostMove][EP_STATUS] == EP_RULE_DRAW)
+ p = "Draw claim: 50-move rule";
+ if((signed char)boards[forwardMostMove][EP_STATUS] == EP_REP_DRAW)
+ p = "Draw claim: 3-fold repetition";
+ if((signed char)boards[forwardMostMove][EP_STATUS] == EP_INSUF_DRAW)
+ p = "Draw claim: insufficient mating material";
+ if( p != NULL && canAdjudicate) {
+ if(engineOpponent) {
+ SendToProgram("force\n", engineOpponent); // suppress reply
+ SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
+ }
+ GameEnds( GameIsDrawn, p, GE_XBOARD );
+ return 1;
+ }
+ }
+
+ if( canAdjudicate && appData.adjudicateDrawMoves > 0 && forwardMostMove > (2*appData.adjudicateDrawMoves) ) {
+ if(engineOpponent) {
+ SendToProgram("force\n", engineOpponent); // suppress reply
+ SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
+ }
+ GameEnds( GameIsDrawn, "Xboard adjudication: long game", GE_XBOARD );
+ return 1;
+ }
+ return 0;
+}
+
+typedef int (CDECL *PPROBE_EGBB) (int player, int *piece, int *square);
+typedef int (CDECL *PLOAD_EGBB) (char *path, int cache_size, int load_options);
+static int egbbCode[] = { 6, 5, 4, 3, 2, 1 };
+
+static int
+BitbaseProbe ()
+{
+ int pieces[10], squares[10], cnt=0, r, f, res;
+ static int loaded;
+ static PPROBE_EGBB probeBB;
+ if(!appData.testLegality) return 10;
+ if(BOARD_HEIGHT != 8 || BOARD_RGHT-BOARD_LEFT != 8) return 12;
+ if(gameInfo.holdingsSize && gameInfo.variant != VariantSuper && gameInfo.variant != VariantSChess) return 12;
+ if(loaded == 2 && forwardMostMove < 2) loaded = 0; // retry on new game
+ for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
+ ChessSquare piece = boards[forwardMostMove][r][f];
+ int black = (piece >= BlackPawn);
+ int type = piece - black*BlackPawn;
+ if(piece == EmptySquare) continue;
+ if(type != WhiteKing && type > WhiteQueen) return 12; // unorthodox piece
+ if(type == WhiteKing) type = WhiteQueen + 1;
+ type = egbbCode[type];
+ squares[cnt] = r*(BOARD_RGHT - BOARD_LEFT) + f - BOARD_LEFT;
+ pieces[cnt] = type + black*6;
+ if(++cnt > 5) return 11;
+ }
+ pieces[cnt] = squares[cnt] = 0;
+ // probe EGBB
+ if(loaded == 2) return 13; // loading failed before
+ if(loaded == 0) {
+ char *p, *path = strstr(appData.egtFormats, "scorpio:"), buf[MSG_SIZ];
+ HMODULE lib;
+ PLOAD_EGBB loadBB;
+ loaded = 2; // prepare for failure
+ if(!path) return 13; // no egbb installed
+ strncpy(buf, path + 8, MSG_SIZ);
+ if(p = strchr(buf, ',')) *p = NULLCHAR; else p = buf + strlen(buf);
+ snprintf(p, MSG_SIZ - strlen(buf), "%c%s", SLASH, EGBB_NAME);
+ lib = LoadLibrary(buf);
+ if(!lib) { DisplayError(_("could not load EGBB library"), 0); return 13; }
+ loadBB = (PLOAD_EGBB) GetProcAddress(lib, "load_egbb_xmen");
+ probeBB = (PPROBE_EGBB) GetProcAddress(lib, "probe_egbb_xmen");
+ if(!loadBB || !probeBB) { DisplayError(_("wrong EGBB version"), 0); return 13; }
+ p[1] = NULLCHAR; loadBB(buf, 64*1028, 2); // 2 = SMART_LOAD
+ loaded = 1; // success!
+ }
+ res = probeBB(forwardMostMove & 1, pieces, squares);
+ return res > 0 ? 1 : res < 0 ? -1 : 0;
+}
+
+char *
+SendMoveToBookUser (int moveNr, ChessProgramState *cps, int initial)
{ // [HGM] book: this routine intercepts moves to simulate book replies
char *bookHit = NULL;
+ if(cps->drawDepth && BitbaseProbe() == 0) { // [HG} egbb: reduce depth in drawn position
+ char buf[MSG_SIZ];
+ snprintf(buf, MSG_SIZ, "sd %d\n", cps->drawDepth);
+ SendToProgram(buf, cps);
+ }
//first determine if the incoming move brings opponent into his book
if(appData.usePolyglotBook && (cps == &first ? !appData.firstHasOwnBookUCI : !appData.secondHasOwnBookUCI))
bookHit = ProbeBook(moveNr+1, appData.polyglotBook); // returns move
SendToProgram("force\n", cps);
cps->bookSuspend = TRUE; // flag indicating it has to be restarted
}
+ if(bookHit) setboardSpoiledMachineBlack = FALSE; // suppress 'go' in SendMoveToProgram
if(!initial) SendMoveToProgram(moveNr, cps); // with hit on initial position there is no move
// now arrange restart after book miss
if(bookHit) {
// after a book hit we never send 'go', and the code after the call to this routine
// has '&& !bookHit' added to suppress potential sending there (based on 'firstMove').
- char buf[MSG_SIZ];
- if (cps->useUsermove) sprintf(buf, "usermove "); // sorry, no SAN yet :(
- sprintf(buf, "%s\n", bookHit); // force book move into program supposed to play it
+ char buf[MSG_SIZ], *move = bookHit;
+ if(cps->useSAN) {
+ int fromX, fromY, toX, toY;
+ char promoChar;
+ ChessMove moveType;
+ move = buf + 30;
+ if (ParseOneMove(bookHit, forwardMostMove, &moveType,
+ &fromX, &fromY, &toX, &toY, &promoChar)) {
+ (void) CoordsToAlgebraic(boards[forwardMostMove],
+ PosFlags(forwardMostMove),
+ fromY, fromX, toY, toX, promoChar, move);
+ } else {
+ if(appData.debugMode) fprintf(debugFP, "Book move could not be parsed\n");
+ bookHit = NULL;
+ }
+ }
+ snprintf(buf, MSG_SIZ, "%s%s\n", (cps->useUsermove ? "usermove " : ""), move); // force book move into program supposed to play it
SendToProgram(buf, cps);
if(!initial) firstMove = FALSE; // normally we would clear the firstMove condition after return & sending 'go'
} else if(initial) { // 'go' was needed irrespective of firstMove, and it has to be done in this routine
cps->bookSuspend = FALSE; // after a 'go' we are never suspended
} else { // 'go' might be sent based on 'firstMove' after this routine returns
if(cps->bookSuspend && !firstMove) // 'go' needed, and it will not be done after we return
- SendToProgram("go\n", cps);
+ SendToProgram("go\n", cps);
cps->bookSuspend = FALSE; // anyhow, we will not be suspended after a miss
}
return bookHit; // notify caller of hit, so it can take action to send move to opponent
}
+int
+LoadError (char *errmess, ChessProgramState *cps)
+{ // unloads engine and switches back to -ncp mode if it was first
+ if(cps->initDone) return FALSE;
+ cps->isr = NULL; // this should suppress further error popups from breaking pipes
+ DestroyChildProcess(cps->pr, 9 ); // just to be sure
+ cps->pr = NoProc;
+ if(cps == &first) {
+ appData.noChessProgram = TRUE;
+ gameMode = MachinePlaysBlack; ModeHighlight(); // kludge to unmark Machine Black menu
+ gameMode = BeginningOfGame; ModeHighlight();
+ SetNCPMode();
+ }
+ if(GetDelayedEvent()) CancelDelayedEvent(), ThawUI(); // [HGM] cancel remaining loading effort scheduled after feature timeout
+ DisplayMessage("", ""); // erase waiting message
+ if(errmess) DisplayError(errmess, 0); // announce reason, if given
+ return TRUE;
+}
+
char *savedMessage;
ChessProgramState *savedState;
-void DeferredBookMove(void)
+void
+DeferredBookMove (void)
{
if(savedState->lastPing != savedState->lastPong)
ScheduleDelayedEvent(DeferredBookMove, 10);
HandleMachineMove(savedMessage, savedState);
}
+static int savedWhitePlayer, savedBlackPlayer, pairingReceived;
+static ChessProgramState *stalledEngine;
+static char stashedInputMove[MSG_SIZ], abortEngineThink;
+
void
-HandleMachineMove(message, cps)
- char *message;
- ChessProgramState *cps;
+HandleMachineMove (char *message, ChessProgramState *cps)
{
+ static char firstLeg[20];
char machineMove[MSG_SIZ], buf1[MSG_SIZ*10], buf2[MSG_SIZ];
char realname[MSG_SIZ];
int fromX, fromY, toX, toY;
ChessMove moveType;
- char promoChar;
- char *p;
- int machineWhite;
+ char promoChar, roar;
+ char *p, *pv=buf1;
+ int machineWhite, oldError;
char *bookHit;
+ if(cps == &pairing && sscanf(message, "%d-%d", &savedWhitePlayer, &savedBlackPlayer) == 2) {
+ // [HGM] pairing: Mega-hack! Pairing engine also uses this routine (so it could give other WB commands).
+ if(savedWhitePlayer == 0 || savedBlackPlayer == 0) {
+ DisplayError(_("Invalid pairing from pairing engine"), 0);
+ return;
+ }
+ pairingReceived = 1;
+ NextMatchGame();
+ return; // Skim the pairing messages here.
+ }
+
+ oldError = cps->userError; cps->userError = 0;
+
FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book hit
/*
* Kludge to ignore BEL characters
* Look for machine move.
*/
if ((sscanf(message, "%s %s %s", buf1, buf2, machineMove) == 3 && strcmp(buf2, "...") == 0) ||
- (sscanf(message, "%s %s", buf1, machineMove) == 2 && strcmp(buf1, "move") == 0))
+ (sscanf(message, "%s %s", buf1, machineMove) == 2 && strcmp(buf1, "move") == 0))
{
+ if(pausing && !cps->pause) { // for pausing engine that does not support 'pause', we stash its move for processing when we resume.
+ if(appData.debugMode) fprintf(debugFP, "pause %s engine after move\n", cps->which);
+ safeStrCpy(stashedInputMove, message, MSG_SIZ);
+ stalledEngine = cps;
+ if(appData.ponderNextMove) { // bring opponent out of ponder
+ if(gameMode == TwoMachinesPlay) {
+ if(cps->other->pause)
+ PauseEngine(cps->other);
+ else
+ SendToProgram("easy\n", cps->other);
+ }
+ }
+ StopClocks();
+ return;
+ }
+
+ if(cps->usePing) {
+
/* This method is only useful on engines that support ping */
+ if(abortEngineThink) {
+ if (appData.debugMode) {
+ fprintf(debugFP, "Undoing move from aborted think of %s\n", cps->which);
+ }
+ SendToProgram("undo\n", cps);
+ return;
+ }
+
if (cps->lastPing != cps->lastPong) {
- if (gameMode == BeginningOfGame) {
/* Extra move from before last new; ignore */
if (appData.debugMode) {
fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
}
- } else {
- if (appData.debugMode) {
- fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
- cps->which, gameMode);
- }
-
- SendToProgram("undo\n", cps);
- }
return;
}
+ } else {
+
switch (gameMode) {
case BeginningOfGame:
/* Extra move from before last reset; ignore */
}
return;
}
+ }
- if (appData.debugMode) { int f = forwardMostMove;
- fprintf(debugFP, "machine move %d, castling = %d %d %d %d %d %d\n", f,
- castlingRights[f][0],castlingRights[f][1],castlingRights[f][2],castlingRights[f][3],castlingRights[f][4],castlingRights[f][5]);
- }
if(cps->alphaRank) AlphaRank(machineMove, 4);
+
+ // [HGM] lion: (some very limited) support for Alien protocol
+ killX = killY = kill2X = kill2Y = -1;
+ if(machineMove[strlen(machineMove)-1] == ',') { // move ends in coma: non-final leg of composite move
+ safeStrCpy(firstLeg, machineMove, 20); // just remember it for processing when second leg arrives
+ return;
+ }
+ if(p = strchr(machineMove, ',')) { // we got both legs in one (happens on book move)
+ safeStrCpy(firstLeg, machineMove, 20); // kludge: fake we received the first leg earlier, and clip it off
+ safeStrCpy(machineMove, firstLeg + (p - machineMove) + 1, 20);
+ }
+ if(firstLeg[0]) { // there was a previous leg;
+ // only support case where same piece makes two step
+ char buf[20], *p = machineMove+1, *q = buf+1, f;
+ safeStrCpy(buf, machineMove, 20);
+ while(isdigit(*q)) q++; // find start of to-square
+ safeStrCpy(machineMove, firstLeg, 20);
+ while(isdigit(*p)) p++; // to-square of first leg (which is now copied to machineMove)
+ if(*p == *buf) // if first-leg to not equal to second-leg from first leg says unmodified (assume it ia King move of castling)
+ safeStrCpy(p, q, 20); // glue to-square of second leg to from-square of first, to process over-all move
+ sscanf(buf, "%c%d", &f, &killY); killX = f - AAA; killY -= ONE - '0'; // pass intermediate square to MakeMove in global
+ firstLeg[0] = NULLCHAR;
+ }
+
if (!ParseOneMove(machineMove, forwardMostMove, &moveType,
&fromX, &fromY, &toX, &toY, &promoChar)) {
/* Machine move could not be parsed; ignore it. */
- sprintf(buf1, _("Illegal move \"%s\" from %s machine"),
- machineMove, cps->which);
- DisplayError(buf1, 0);
- sprintf(buf1, "Xboard: Forfeit due to invalid move: %s (%c%c%c%c) res=%d",
- machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, moveType);
+ snprintf(buf1, MSG_SIZ*10, _("Illegal move \"%s\" from %s machine"),
+ machineMove, _(cps->which));
+ DisplayMoveError(buf1);
+ snprintf(buf1, MSG_SIZ*10, "Xboard: Forfeit due to invalid move: %s (%c%c%c%c via %c%c, %c%c) res=%d",
+ machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, killX+AAA, killY+ONE, kill2X+AAA, kill2Y+ONE, moveType);
if (gameMode == TwoMachinesPlay) {
GameEnds(machineWhite ? BlackWins : WhiteWins,
buf1, GE_XBOARD);
/* to make sure an illegal e.p. capture does not slip through, */
/* to cause a forfeit on a justified illegal-move complaint */
/* of the opponent. */
- if( gameMode==TwoMachinesPlay && appData.testLegality
- && fromY != DROP_RANK /* [HGM] temporary; should still add legality test for drops */
- ) {
+ if( gameMode==TwoMachinesPlay && appData.testLegality ) {
ChessMove moveType;
moveType = LegalityTest(boards[forwardMostMove], PosFlags(forwardMostMove),
- epStatus[forwardMostMove], castlingRights[forwardMostMove],
fromY, fromX, toY, toX, promoChar);
- if (appData.debugMode) {
- int i;
- for(i=0; i< nrCastlingRights; i++) fprintf(debugFP, "(%d,%d) ",
- castlingRights[forwardMostMove][i], castlingRank[i]);
- fprintf(debugFP, "castling rights\n");
- }
if(moveType == IllegalMove) {
- sprintf(buf1, "Xboard: Forfeit due to illegal move: %s (%c%c%c%c)%c",
+ snprintf(buf1, MSG_SIZ*10, "Xboard: Forfeit due to illegal move: %s (%c%c%c%c)%c",
machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, 0);
GameEnds(machineWhite ? BlackWins : WhiteWins,
buf1, GE_XBOARD);
return;
- } else if(gameInfo.variant != VariantFischeRandom && gameInfo.variant != VariantCapaRandom)
+ } else if(!appData.fischerCastling)
/* [HGM] Kludge to handle engines that send FRC-style castling
when they shouldn't (like TSCP-Gothic) */
switch(moveType) {
if (cps->sendTime == 2) cps->sendTime = 1;
if (cps->offeredDraw) cps->offeredDraw--;
-#if ZIPPY
- if ((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack) &&
- first.initDone) {
- SendMoveToICS(moveType, fromX, fromY, toX, toY);
- ics_user_moved = 1;
- if(appData.autoKibitz && !appData.icsEngineAnalyze ) { /* [HGM] kibitz: send most-recent PV info to ICS */
- char buf[3*MSG_SIZ];
-
- sprintf(buf, "kibitz !!! %+.2f/%d (%.2f sec, %u nodes, %.0f knps) PV=%s\n",
- programStats.score / 100.,
- programStats.depth,
- programStats.time / 100.,
- (unsigned int)programStats.nodes,
- (unsigned int)programStats.nodes / (10*abs(programStats.time) + 1.),
- programStats.movelist);
- SendToICS(buf);
-if(appData.debugMode) fprintf(debugFP, "nodes = %d, %lld\n", (int) programStats.nodes, programStats.nodes);
- }
- }
-#endif
- /* currentMoveString is set as a side-effect of ParseOneMove */
- strcpy(machineMove, currentMoveString);
- strcat(machineMove, "\n");
- strcpy(moveList[forwardMostMove], machineMove);
-
- /* [AS] Save move info and clear stats for next move */
+ /* [AS] Save move info*/
pvInfoList[ forwardMostMove ].score = programStats.score;
pvInfoList[ forwardMostMove ].depth = programStats.depth;
pvInfoList[ forwardMostMove ].time = programStats.time; // [HGM] PGNtime: take time from engine stats
- ClearProgramStats();
- thinkOutput[0] = NULLCHAR;
- hiddenThinkOutputState = 0;
MakeMove(fromX, fromY, toX, toY, promoChar);/*updates forwardMostMove*/
+ /* Test suites abort the 'game' after one move */
+ if(*appData.finger) {
+ static FILE *f;
+ char *fen = PositionToFEN(backwardMostMove, NULL, 0); // no counts in EPD
+ if(!f) f = fopen(appData.finger, "w");
+ if(f) fprintf(f, "%s bm %s;\n", fen, parseList[backwardMostMove]), fflush(f);
+ else { DisplayFatalError("Bad output file", errno, 0); return; }
+ free(fen);
+ GameEnds(GameUnfinished, NULL, GE_XBOARD);
+ }
+ if(appData.epd) {
+ if(solvingTime >= 0) {
+ snprintf(buf1, MSG_SIZ, "%d. %4.2fs\n", matchGame, solvingTime/100.);
+ totalTime += solvingTime; first.matchWins++;
+ } else {
+ snprintf(buf1, MSG_SIZ, "%d. wrong (%s)\n", matchGame, parseList[backwardMostMove]);
+ second.matchWins++;
+ }
+ OutputKibitz(2, buf1);
+ GameEnds(GameUnfinished, NULL, GE_XBOARD);
+ }
+
/* [AS] Adjudicate game if needed (note: remember that forwardMostMove now points past the last move) */
- if( gameMode == TwoMachinesPlay && adjudicateLossThreshold != 0 && forwardMostMove >= adjudicateLossPlies ) {
+ if( gameMode == TwoMachinesPlay && appData.adjudicateLossThreshold != 0 && forwardMostMove >= adjudicateLossPlies ) {
int count = 0;
while( count < adjudicateLossPlies ) {
score = -score; /* Flip score for winning side */
}
- if( score > adjudicateLossThreshold ) {
+ if( score > appData.adjudicateLossThreshold ) {
break;
}
if( count >= adjudicateLossPlies ) {
ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
- GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
- "Xboard adjudication",
+ GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
+ "Xboard adjudication",
GE_XBOARD );
return;
}
}
- if( gameMode == TwoMachinesPlay ) {
- // [HGM] some adjudications useful with buggy engines
- int k, count = 0, epFile = epStatus[forwardMostMove]; static int bare = 1;
- if(gameInfo.holdingsSize == 0 || gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) {
-
-
- if( appData.testLegality )
- { /* [HGM] Some more adjudications for obstinate engines */
- int NrWN=0, NrBN=0, NrWB=0, NrBB=0, NrWR=0, NrBR=0,
- NrWQ=0, NrBQ=0, NrW=0, NrK=0, bishopsColor = 0,
- NrPieces=0, NrPawns=0, PawnAdvance=0, i, j;
- static int moveCount = 6;
- ChessMove result;
- char *reason = NULL;
-
- /* Count what is on board. */
- for(i=0; i<BOARD_HEIGHT; i++) for(j=BOARD_LEFT; j<BOARD_RGHT; j++)
- { ChessSquare p = boards[forwardMostMove][i][j];
- int m=i;
-
- switch((int) p)
- { /* count B,N,R and other of each side */
- case WhiteKing:
- case BlackKing:
- NrK++; break; // [HGM] atomic: count Kings
- case WhiteKnight:
- NrWN++; break;
- case WhiteBishop:
- case WhiteFerz: // [HGM] shatranj: kludge to mke it work in shatranj
- bishopsColor |= 1 << ((i^j)&1);
- NrWB++; break;
- case BlackKnight:
- NrBN++; break;
- case BlackBishop:
- case BlackFerz: // [HGM] shatranj: kludge to mke it work in shatranj
- bishopsColor |= 1 << ((i^j)&1);
- NrBB++; break;
- case WhiteRook:
- NrWR++; break;
- case BlackRook:
- NrBR++; break;
- case WhiteQueen:
- NrWQ++; break;
- case BlackQueen:
- NrBQ++; break;
- case EmptySquare:
- break;
- case BlackPawn:
- m = 7-i;
- case WhitePawn:
- PawnAdvance += m; NrPawns++;
- }
- NrPieces += (p != EmptySquare);
- NrW += ((int)p < (int)BlackPawn);
- if(gameInfo.variant == VariantXiangqi &&
- (p == WhiteFerz || p == WhiteAlfil || p == BlackFerz || p == BlackAlfil)) {
- NrPieces--; // [HGM] XQ: do not count purely defensive pieces
- NrW -= ((int)p < (int)BlackPawn);
- }
- }
-
- /* Some material-based adjudications that have to be made before stalemate test */
- if(gameInfo.variant == VariantAtomic && NrK < 2) {
- // [HGM] atomic: stm must have lost his King on previous move, as destroying own K is illegal
- epStatus[forwardMostMove] = EP_CHECKMATE; // make claimable as if stm is checkmated
- if(appData.checkMates) {
- SendMoveToProgram(forwardMostMove-1, cps->other); // make sure opponent gets move
- ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
- GameEnds( WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins,
- "Xboard adjudication: King destroyed", GE_XBOARD );
- return;
- }
- }
-
- /* Bare King in Shatranj (loses) or Losers (wins) */
- if( NrW == 1 || NrPieces - NrW == 1) {
- if( gameInfo.variant == VariantLosers) { // [HGM] losers: bare King wins (stm must have it first)
- epStatus[forwardMostMove] = EP_WINS; // mark as win, so it becomes claimable
- if(appData.checkMates) {
- SendMoveToProgram(forwardMostMove-1, cps->other); // make sure opponent gets to see move
- ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
- GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
- "Xboard adjudication: Bare king", GE_XBOARD );
- return;
- }
- } else
- if( gameInfo.variant == VariantShatranj && --bare < 0)
- { /* bare King */
- epStatus[forwardMostMove] = EP_WINS; // make claimable as win for stm
- if(appData.checkMates) {
- /* but only adjudicate if adjudication enabled */
- SendMoveToProgram(forwardMostMove-1, cps->other); // make sure opponent gets move
- ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
- GameEnds( NrW > 1 ? WhiteWins : NrPieces - NrW > 1 ? BlackWins : GameIsDrawn,
- "Xboard adjudication: Bare king", GE_XBOARD );
- return;
- }
- }
- } else bare = 1;
-
-
- // don't wait for engine to announce game end if we can judge ourselves
- switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove), epFile,
- castlingRights[forwardMostMove]) ) {
- case MT_CHECK:
- if(gameInfo.variant == Variant3Check) { // [HGM] 3check: when in check, test if 3rd time
- int i, checkCnt = 0; // (should really be done by making nr of checks part of game state)
- for(i=forwardMostMove-2; i>=backwardMostMove; i-=2) {
- if(MateTest(boards[i], PosFlags(i), epStatus[i], castlingRights[i]) == MT_CHECK)
- checkCnt++;
- if(checkCnt >= 2) {
- reason = "Xboard adjudication: 3rd check";
- epStatus[forwardMostMove] = EP_CHECKMATE;
- break;
- }
- }
- }
- case MT_NONE:
- default:
- break;
- case MT_STALEMATE:
- case MT_STAINMATE:
- reason = "Xboard adjudication: Stalemate";
- if(epStatus[forwardMostMove] != EP_CHECKMATE) { // [HGM] don't touch win through baring or K-capt
- epStatus[forwardMostMove] = EP_STALEMATE; // default result for stalemate is draw
- if(gameInfo.variant == VariantLosers || gameInfo.variant == VariantGiveaway) // [HGM] losers:
- epStatus[forwardMostMove] = EP_WINS; // in these variants stalemated is always a win
- else if(gameInfo.variant == VariantSuicide) // in suicide it depends
- epStatus[forwardMostMove] = NrW == NrPieces-NrW ? EP_STALEMATE :
- ((NrW < NrPieces-NrW) != WhiteOnMove(forwardMostMove) ?
- EP_CHECKMATE : EP_WINS);
- else if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantXiangqi)
- epStatus[forwardMostMove] = EP_CHECKMATE; // and in these variants being stalemated loses
- }
- break;
- case MT_CHECKMATE:
- reason = "Xboard adjudication: Checkmate";
- epStatus[forwardMostMove] = (gameInfo.variant == VariantLosers ? EP_WINS : EP_CHECKMATE);
- break;
- }
-
- switch(i = epStatus[forwardMostMove]) {
- case EP_STALEMATE:
- result = GameIsDrawn; break;
- case EP_CHECKMATE:
- result = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins; break;
- case EP_WINS:
- result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins; break;
- default:
- result = (ChessMove) 0;
- }
- if(appData.checkMates && result) { // [HGM] mates: adjudicate finished games if requested
- SendMoveToProgram(forwardMostMove-1, cps->other); /* make sure opponent gets to see move */
- ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
- GameEnds( result, reason, GE_XBOARD );
- return;
- }
-
- /* Next absolutely insufficient mating material. */
- if( NrPieces == 2 || gameInfo.variant != VariantXiangqi &&
- gameInfo.variant != VariantShatranj && // [HGM] baring will remain possible
- (NrPieces == 3 && NrWN+NrBN+NrWB+NrBB == 1 ||
- NrPieces == NrBB+NrWB+2 && bishopsColor != 3)) // [HGM] all Bishops (Ferz!) same color
- { /* KBK, KNK, KK of KBKB with like Bishops */
-
- /* always flag draws, for judging claims */
- epStatus[forwardMostMove] = EP_INSUF_DRAW;
-
- if(appData.materialDraws) {
- /* but only adjudicate them if adjudication enabled */
- SendToProgram("force\n", cps->other); // suppress reply
- SendMoveToProgram(forwardMostMove-1, cps->other); /* make sure opponent gets to see last move */
- ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
- GameEnds( GameIsDrawn, "Xboard adjudication: Insufficient mating material", GE_XBOARD );
- return;
- }
- }
+ if(Adjudicate(cps)) {
+ ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
+ return; // [HGM] adjudicate: for all automatic game ends
+ }
- /* Then some trivial draws (only adjudicate, cannot be claimed) */
- if(NrPieces == 4 &&
- ( NrWR == 1 && NrBR == 1 /* KRKR */
- || NrWQ==1 && NrBQ==1 /* KQKQ */
- || NrWN==2 || NrBN==2 /* KNNK */
- || NrWN+NrWB == 1 && NrBN+NrBB == 1 /* KBKN, KBKB, KNKN */
- ) ) {
- if(--moveCount < 0 && appData.trivialDraws)
- { /* if the first 3 moves do not show a tactical win, declare draw */
- SendToProgram("force\n", cps->other); // suppress reply
- SendMoveToProgram(forwardMostMove-1, cps->other); /* make sure opponent gets to see move */
- ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
- GameEnds( GameIsDrawn, "Xboard adjudication: Trivial draw", GE_XBOARD );
- return;
- }
- } else moveCount = 6;
- }
- }
-
- if (appData.debugMode) { int i;
- fprintf(debugFP, "repeat test fmm=%d bmm=%d ep=%d, reps=%d\n",
- forwardMostMove, backwardMostMove, epStatus[backwardMostMove],
- appData.drawRepeats);
- for( i=forwardMostMove; i>=backwardMostMove; i-- )
- fprintf(debugFP, "%d ep=%d\n", i, epStatus[i]);
-
+#if ZIPPY
+ if ((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack) &&
+ first.initDone) {
+ if(cps->offeredDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
+ SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
+ SendToICS("draw ");
+ SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
}
+ SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
+ ics_user_moved = 1;
+ if(appData.autoKibitz && !appData.icsEngineAnalyze ) { /* [HGM] kibitz: send most-recent PV info to ICS */
+ char buf[3*MSG_SIZ];
- /* Check for rep-draws */
- count = 0;
- for(k = forwardMostMove-2;
- k>=backwardMostMove && k>=forwardMostMove-100 &&
- epStatus[k] < EP_UNKNOWN &&
- epStatus[k+2] <= EP_NONE && epStatus[k+1] <= EP_NONE;
- k-=2)
- { int rights=0;
- if(CompareBoards(boards[k], boards[forwardMostMove])) {
- /* compare castling rights */
- if( castlingRights[forwardMostMove][2] != castlingRights[k][2] &&
- (castlingRights[k][0] >= 0 || castlingRights[k][1] >= 0) )
- rights++; /* King lost rights, while rook still had them */
- if( castlingRights[forwardMostMove][2] >= 0 ) { /* king has rights */
- if( castlingRights[forwardMostMove][0] != castlingRights[k][0] ||
- castlingRights[forwardMostMove][1] != castlingRights[k][1] )
- rights++; /* but at least one rook lost them */
- }
- if( castlingRights[forwardMostMove][5] != castlingRights[k][5] &&
- (castlingRights[k][3] >= 0 || castlingRights[k][4] >= 0) )
- rights++;
- if( castlingRights[forwardMostMove][5] >= 0 ) {
- if( castlingRights[forwardMostMove][3] != castlingRights[k][3] ||
- castlingRights[forwardMostMove][4] != castlingRights[k][4] )
- rights++;
- }
- if( rights == 0 && ++count > appData.drawRepeats-2
- && appData.drawRepeats > 1) {
- /* adjudicate after user-specified nr of repeats */
- SendToProgram("force\n", cps->other); // suppress reply
- SendMoveToProgram(forwardMostMove-1, cps->other); /* make sure opponent gets to see move */
- ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
- if(gameInfo.variant == VariantXiangqi && appData.testLegality) {
- // [HGM] xiangqi: check for forbidden perpetuals
- int m, ourPerpetual = 1, hisPerpetual = 1;
- for(m=forwardMostMove; m>k; m-=2) {
- if(MateTest(boards[m], PosFlags(m),
- EP_NONE, castlingRights[m]) != MT_CHECK)
- ourPerpetual = 0; // the current mover did not always check
- if(MateTest(boards[m-1], PosFlags(m-1),
- EP_NONE, castlingRights[m-1]) != MT_CHECK)
- hisPerpetual = 0; // the opponent did not always check
- }
- if(appData.debugMode) fprintf(debugFP, "XQ perpetual test, our=%d, his=%d\n",
- ourPerpetual, hisPerpetual);
- if(ourPerpetual && !hisPerpetual) { // we are actively checking him: forfeit
- GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
- "Xboard adjudication: perpetual checking", GE_XBOARD );
- return;
- }
- if(hisPerpetual && !ourPerpetual) // he is checking us, but did not repeat yet
- break; // (or we would have caught him before). Abort repetition-checking loop.
- // Now check for perpetual chases
- if(!ourPerpetual && !hisPerpetual) { // no perpetual check, test for chase
- hisPerpetual = PerpetualChase(k, forwardMostMove);
- ourPerpetual = PerpetualChase(k+1, forwardMostMove);
- if(ourPerpetual && !hisPerpetual) { // we are actively chasing him: forfeit
- GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
- "Xboard adjudication: perpetual chasing", GE_XBOARD );
- return;
- }
- if(hisPerpetual && !ourPerpetual) // he is chasing us, but did not repeat yet
- break; // Abort repetition-checking loop.
- }
- // if neither of us is checking or chasing all the time, or both are, it is draw
- }
- GameEnds( GameIsDrawn, "Xboard adjudication: repetition draw", GE_XBOARD );
- return;
- }
- if( rights == 0 && count > 1 ) /* occurred 2 or more times before */
- epStatus[forwardMostMove] = EP_REP_DRAW;
- }
- }
-
- /* Now we test for 50-move draws. Determine ply count */
- count = forwardMostMove;
- /* look for last irreversble move */
- while( epStatus[count] <= EP_NONE && count > backwardMostMove )
- count--;
- /* if we hit starting position, add initial plies */
- if( count == backwardMostMove )
- count -= initialRulePlies;
- count = forwardMostMove - count;
- if( count >= 100)
- epStatus[forwardMostMove] = EP_RULE_DRAW;
- /* this is used to judge if draw claims are legal */
- if(appData.ruleMoves > 0 && count >= 2*appData.ruleMoves) {
- SendToProgram("force\n", cps->other); // suppress reply
- SendMoveToProgram(forwardMostMove-1, cps->other); /* make sure opponent gets to see move */
- ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
- GameEnds( GameIsDrawn, "Xboard adjudication: 50-move rule", GE_XBOARD );
- return;
- }
-
- /* if draw offer is pending, treat it as a draw claim
- * when draw condition present, to allow engines a way to
- * claim draws before making their move to avoid a race
- * condition occurring after their move
- */
- if( cps->other->offeredDraw || cps->offeredDraw ) {
- char *p = NULL;
- if(epStatus[forwardMostMove] == EP_RULE_DRAW)
- p = "Draw claim: 50-move rule";
- if(epStatus[forwardMostMove] == EP_REP_DRAW)
- p = "Draw claim: 3-fold repetition";
- if(epStatus[forwardMostMove] == EP_INSUF_DRAW)
- p = "Draw claim: insufficient mating material";
- if( p != NULL ) {
- SendToProgram("force\n", cps->other); // suppress reply
- SendMoveToProgram(forwardMostMove-1, cps->other); /* make sure opponent gets to see move */
- GameEnds( GameIsDrawn, p, GE_XBOARD );
- ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
- return;
- }
- }
-
-
- if( appData.adjudicateDrawMoves > 0 && forwardMostMove > (2*appData.adjudicateDrawMoves) ) {
- SendToProgram("force\n", cps->other); // suppress reply
- SendMoveToProgram(forwardMostMove-1, cps->other); /* make sure opponent gets to see move */
- ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
-
- GameEnds( GameIsDrawn, "Xboard adjudication: long game", GE_XBOARD );
+ snprintf(buf, 3*MSG_SIZ, "kibitz !!! %+.2f/%d (%.2f sec, %u nodes, %.0f knps) PV=%s\n",
+ programStats.score / 100.,
+ programStats.depth,
+ programStats.time / 100.,
+ (unsigned int)programStats.nodes,
+ (unsigned int)programStats.nodes / (10*abs(programStats.time) + 1.),
+ programStats.movelist);
+ SendToICS(buf);
+ }
+ }
+#endif
- return;
- }
- }
+ /* [AS] Clear stats for next move */
+ ClearProgramStats();
+ thinkOutput[0] = NULLCHAR;
+ hiddenThinkOutputState = 0;
bookHit = NULL;
if (gameMode == TwoMachinesPlay) {
cps->other->maybeThinking = TRUE;
}
+ roar = (killX >= 0 && IS_LION(boards[forwardMostMove][toY][toX]));
+
ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
-
+
if (!pausing && appData.ringBellAfterMoves) {
- RingBell();
+ if(!roar) RingBell();
}
- /*
+ /*
* Reenable menu items that were disabled while
* machine was thinking
*/
if(bookHit) {
static char bookMove[MSG_SIZ]; // a bit generous?
- strcpy(bookMove, "move ");
+ safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
strcat(bookMove, bookHit);
message = bookMove;
cps = cps->other;
- programStats.nodes = programStats.depth = programStats.time =
+ programStats.nodes = programStats.depth = programStats.time =
programStats.score = programStats.got_only_move = 0;
sprintf(programStats.movelist, "%s (xbook)", bookHit);
return; // [HGM] This return was missing, causing option features to be recognized as non-compliant commands!
}
+ if (!strncmp(message, "setup ", 6) &&
+ (!appData.testLegality || gameInfo.variant == VariantFairy || gameInfo.variant == VariantUnknown ||
+ NonStandardBoardSize(gameInfo.variant, gameInfo.boardWidth, gameInfo.boardHeight, gameInfo.holdingsSize))
+ ) { // [HGM] allow first engine to define opening position
+ int dummy, w, h, hand, s=6; char buf[MSG_SIZ], varName[MSG_SIZ];
+ if(appData.icsActive || forwardMostMove != 0 || cps != &first) return;
+ *buf = NULLCHAR;
+ if(sscanf(message, "setup (%s", buf) == 1) {
+ s = 8 + strlen(buf), buf[s-9] = NULLCHAR, SetCharTableEsc(pieceToChar, buf, SUFFIXES);
+ ASSIGN(appData.pieceToCharTable, buf);
+ }
+ dummy = sscanf(message+s, "%dx%d+%d_%s", &w, &h, &hand, varName);
+ if(dummy >= 3) {
+ while(message[s] && message[s++] != ' ');
+ if(BOARD_HEIGHT != h || BOARD_WIDTH != w + 4*(hand != 0) || gameInfo.holdingsSize != hand ||
+ dummy == 4 && gameInfo.variant != StringToVariant(varName) ) { // engine wants to change board format or variant
+ appData.NrFiles = w; appData.NrRanks = h; appData.holdingsSize = hand;
+ if(dummy == 4) gameInfo.variant = StringToVariant(varName); // parent variant
+ InitPosition(1); // calls InitDrawingSizes to let new parameters take effect
+ if(*buf) SetCharTableEsc(pieceToChar, buf, SUFFIXES); // do again, for it was spoiled by InitPosition
+ startedFromSetupPosition = FALSE;
+ }
+ }
+ if(startedFromSetupPosition) return;
+ ParseFEN(boards[0], &dummy, message+s, FALSE);
+ DrawPosition(TRUE, boards[0]);
+ CopyBoard(initialPosition, boards[0]);
+ startedFromSetupPosition = TRUE;
+ return;
+ }
+ if(sscanf(message, "piece %s %s", buf2, buf1) == 2) {
+ ChessSquare piece = WhitePawn;
+ char *p=message+6, *q, *s = SUFFIXES, ID = *p;
+ if(*p == '+') piece = CHUPROMOTED(WhitePawn), ID = *++p;
+ if(q = strchr(s, p[1])) ID += 64*(q - s + 1), p++;
+ piece += CharToPiece(ID & 255) - WhitePawn;
+ if(cps != &first || appData.testLegality && *engineVariant == NULLCHAR
+ /* always accept definition of */ && piece != WhiteFalcon && piece != BlackFalcon
+ /* wild-card pieces. */ && piece != WhiteCobra && piece != BlackCobra
+ /* For variants we don't have */ && gameInfo.variant != VariantBerolina
+ /* correct rules for, we cannot */ && gameInfo.variant != VariantCylinder
+ /* enforce legality on our own! */ && gameInfo.variant != VariantUnknown
+ && gameInfo.variant != VariantGreat
+ && gameInfo.variant != VariantFairy ) return;
+ if(piece < EmptySquare) {
+ pieceDefs = TRUE;
+ ASSIGN(pieceDesc[piece], buf1);
+ if((ID & 32) == 0 && p[1] == '&') { ASSIGN(pieceDesc[WHITE_TO_BLACK piece], buf1); }
+ }
+ return;
+ }
+ if(sscanf(message, "choice %s", promoRestrict) == 1 && promoSweep != EmptySquare) {
+ promoSweep = PieceToChar(forwardMostMove&1 ? ToLower(*promoRestrict) : ToUpper(*promoRestrict));
+ Sweep(0);
+ return;
+ }
/* [HGM] Allow engine to set up a position. Don't ask me why one would
* want this, I was asked to put it in, and obliged.
*/
if (!strncmp(message, "setboard ", 9)) {
- Board initial_position; int i;
+ Board initial_position;
GameEnds(GameUnfinished, "Engine aborts game", GE_XBOARD);
- if (!ParseFEN(initial_position, &blackPlaysFirst, message + 9)) {
+ if (!ParseFEN(initial_position, &blackPlaysFirst, message + 9, FALSE)) {
DisplayError(_("Bad FEN received from engine"), 0);
return ;
} else {
- Reset(FALSE, FALSE);
+ Reset(TRUE, FALSE);
CopyBoard(boards[0], initial_position);
initialRulePlies = FENrulePlies;
- epStatus[0] = FENepStatus;
- for( i=0; i<nrCastlingRights; i++ )
- castlingRights[0][i] = FENcastlingRights[i];
if(blackPlaysFirst) gameMode = MachinePlaysWhite;
- else gameMode = MachinePlaysBlack;
+ else gameMode = MachinePlaysBlack;
DrawPosition(FALSE, boards[currentMove]);
}
return;
* Look for communication commands
*/
if (!strncmp(message, "telluser ", 9)) {
+ if(message[9] == '\\' && message[10] == '\\')
+ EscapeExpand(message+9, message+11); // [HGM] esc: allow escape sequences in popup box
+ PlayTellSound();
DisplayNote(message + 9);
return;
}
if (!strncmp(message, "tellusererror ", 14)) {
+ cps->userError = 1;
+ if(message[14] == '\\' && message[15] == '\\')
+ EscapeExpand(message+14, message+16); // [HGM] esc: allow escape sequences in popup box
+ PlayTellSound();
DisplayError(message + 14, 0);
return;
}
snprintf(buf1, sizeof(buf1), "%swhisper %s\n", ics_prefix, message + 11);
SendToICS(buf1);
}
- }
+ } else if(appData.autoComment) AppendComment (forwardMostMove, message + 11, 1); // in local mode, add as move comment
return;
}
if (!strncmp(message, "tellall ", 8)) {
return;
}
if (sscanf(message, "askuser %s %[^\n]", buf1, buf2) == 2) {
- strcpy(realname, cps->tidy);
+ safeStrCpy(realname, cps->tidy, sizeof(realname)/sizeof(realname[0]));
strcat(realname, " query");
AskQuestion(realname, buf2, buf1, cps->pr);
return;
}
- /* Commands from the engine directly to ICS. We don't allow these to be
- * sent until we are logged on. Crafty kibitzes have been known to
+ /* Commands from the engine directly to ICS. We don't allow these to be
+ * sent until we are logged on. Crafty kibitzes have been known to
* interfere with the login process.
*/
if (loggedOn) {
}
}
if (sscanf(message, "pong %d", &cps->lastPong) == 1) {
+ if(initPing == cps->lastPong) {
+ if(gameInfo.variant == VariantUnknown) {
+ DisplayError(_("Engine did not send setup for non-standard variant"), 0);
+ *engineVariant = NULLCHAR; appData.variant = VariantNormal; // back to normal as error recovery?
+ GameEnds(GameUnfinished, NULL, GE_XBOARD);
+ }
+ initPing = -1;
+ }
+ if(cps->lastPing == cps->lastPong && abortEngineThink) {
+ abortEngineThink = FALSE;
+ DisplayMessage("", "");
+ ThawUI();
+ }
+ return;
+ }
+ if(!strncmp(message, "highlight ", 10)) {
+ if(appData.testLegality && !*engineVariant && appData.markers) return;
+ MarkByFEN(message+10); // [HGM] alien: allow engine to mark board squares
+ return;
+ }
+ if(!strncmp(message, "click ", 6)) {
+ char f, c=0; int x, y; // [HGM] alien: allow engine to finish user moves (i.e. engine-driven one-click moving)
+ if(appData.testLegality || !appData.oneClick) return;
+ sscanf(message+6, "%c%d%c", &f, &y, &c);
+ x = f - 'a' + BOARD_LEFT, y -= ONE - '0';
+ if(flipView) x = BOARD_WIDTH-1 - x; else y = BOARD_HEIGHT-1 - y;
+ x = x*squareSize + (x+1)*lineGap + squareSize/2;
+ y = y*squareSize + (y+1)*lineGap + squareSize/2;
+ f = first.highlight; first.highlight = 0; // kludge to suppress lift/put in response to own clicks
+ if(lastClickType == Press) // if button still down, fake release on same square, to be ready for next click
+ LeftClick(Release, lastLeftX, lastLeftY);
+ controlKey = (c == ',');
+ LeftClick(Press, x, y);
+ LeftClick(Release, x, y);
+ first.highlight = f;
return;
}
/*
*/
if (strncmp(message + 1, "llegal move", 11) == 0 ||
strncmp(message, "Error", 5) == 0) {
- if (StrStr(message, "name") ||
+ if (StrStr(message, "name") ||
StrStr(message, "rating") || StrStr(message, "?") ||
StrStr(message, "result") || StrStr(message, "board") ||
StrStr(message, "bk") || StrStr(message, "computer") ||
if (StrStr(message, "analyze")) {
cps->analysisSupport = FALSE;
cps->analyzing = FALSE;
- Reset(FALSE, TRUE);
- sprintf(buf2, _("%s does not support analysis"), cps->tidy);
+// Reset(FALSE, TRUE); // [HGM] this caused discrepancy between display and internal state!
+ EditGameEvent(); // [HGM] try to preserve loaded game
+ snprintf(buf2,MSG_SIZ, _("%s does not support analysis"), cps->tidy);
DisplayError(buf2, 0);
return;
}
gameMode = EditGame;
ModeHighlight();
}
- currentMove = --forwardMostMove;
- DisplayMove(currentMove-1); /* before DisplayMoveError */
- SwitchClocks();
- DisplayBothClocks();
- sprintf(buf1, _("Illegal move \"%s\" (rejected by %s chess program)"),
- parseList[currentMove], cps->which);
- DisplayMoveError(buf1);
- DrawPosition(FALSE, boards[currentMove]);
-
/* [HGM] illegal-move claim should forfeit game when Xboard */
/* only passes fully legal moves */
if( appData.testLegality && gameMode == TwoMachinesPlay ) {
GameEnds( cps->twoMachinesColor[0] == 'w' ? BlackWins : WhiteWins,
"False illegal-move claim", GE_XBOARD );
+ return; // do not take back move we tested as valid
}
+ currentMove = forwardMostMove-1;
+ DisplayMove(currentMove-1); /* before DisplayMoveError */
+ SwitchClocks(forwardMostMove-1); // [HGM] race
+ DisplayBothClocks();
+ snprintf(buf1, 10*MSG_SIZ, _("Illegal move \"%s\" (rejected by %s chess program)"),
+ parseList[currentMove], _(cps->which));
+ DisplayMoveError(buf1);
+ DrawPosition(FALSE, boards[currentMove]);
+
+ SetUserThinkingEnables();
return;
}
if (strncmp(message, "time", 4) == 0 && StrStr(message, "Illegal")) {
Don't use it. */
cps->sendTime = 0;
}
-
+ if (cps->pseudo) { // [HGM] pseudo-engine, granted unusual powers
+ if (sscanf(message, "wtime %ld\n", &whiteTimeRemaining) == 1 || // adjust clock times
+ sscanf(message, "btime %ld\n", &blackTimeRemaining) == 1 ) return;
+ }
+
/*
* If chess program startup fails, exit with an error message.
- * Attempts to recover here are futile.
+ * Attempts to recover here are futile. [HGM] Well, we try anyway
*/
if ((StrStr(message, "unknown host") != NULL)
|| (StrStr(message, "No remote directory") != NULL)
cps->maybeThinking = FALSE;
snprintf(buf1, sizeof(buf1), _("Failed to start %s chess program %s on %s: %s\n"),
- cps->which, cps->program, cps->host, message);
+ _(cps->which), cps->program, cps->host, message);
RemoveInputSource(cps->isr);
- DisplayFatalError(buf1, 0, 1);
+ if(appData.icsActive) DisplayFatalError(buf1, 0, 1); else {
+ if(LoadError(oldError ? NULL : buf1, cps)) return; // error has then been handled by LoadError
+ if(!oldError) DisplayError(buf1, 0); // if reason neatly announced, suppress general error popup
+ }
return;
}
-
- /*
+
+ /*
* Look for hint output
*/
if (sscanf(message, "Hint: %s", buf1) == 1) {
if (ParseOneMove(buf1, forwardMostMove, &moveType,
&fromX, &fromY, &toX, &toY, &promoChar)) {
(void) CoordsToAlgebraic(boards[forwardMostMove],
- PosFlags(forwardMostMove), EP_UNKNOWN,
+ PosFlags(forwardMostMove),
fromY, fromX, toY, toX, promoChar, buf1);
snprintf(buf2, sizeof(buf2), _("Hint: %s"), buf1);
DisplayInformation(buf2);
/* Hint move could not be parsed!? */
snprintf(buf2, sizeof(buf2),
_("Illegal hint move \"%s\"\nfrom %s chess program"),
- buf1, cps->which);
+ buf1, _(cps->which));
DisplayError(buf2, 0);
}
} else {
- strcpy(lastHint, buf1);
+ safeStrCpy(lastHint, buf1, sizeof(lastHint)/sizeof(lastHint[0]));
}
return;
}
r = p + 1;
}
}
-
+
GameEnds(GameIsDrawn, r, GE_ENGINE1 + (cps != &first));
return;
DisplayInformation(_("Machine accepts your draw offer"));
GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
} else {
- DisplayInformation(_("Machine offers a draw\nSelect Action / Draw to agree"));
+ DisplayInformation(_("Machine offers a draw.\nSelect Action / Draw to accept."));
}
}
}
-
+
/*
* Look for thinking output
*/
}
if (!ignore) {
+ ChessProgramStats tempStats = programStats; // [HGM] info: filter out info lines
buf1[0] = NULLCHAR;
if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
&plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5) {
+ char score_buf[MSG_SIZ];
+
+ if(nodes>>32 == u64Const(0xFFFFFFFF)) // [HGM] negative node count read
+ nodes += u64Const(0x100000000);
if (plyext != ' ' && plyext != '\t') {
time *= 100;
}
/* [AS] Negate score if machine is playing black and reporting absolute scores */
- if( cps->scoreIsAbsolute &&
- ((gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b')) )
+ if( cps->scoreIsAbsolute &&
+ ( gameMode == MachinePlaysBlack ||
+ gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b' ||
+ gameMode == IcsPlayingBlack || // [HGM] also add other situations where engine should report black POV
+ (gameMode == AnalyzeMode || gameMode == AnalyzeFile || gameMode == IcsObserving && appData.icsEngineAnalyze) &&
+ !WhiteOnMove(currentMove)
+ ) )
{
curscore = -curscore;
}
+ if(appData.pvSAN[cps==&second]) pv = PvToSAN(buf1);
- programStats.depth = plylev;
- programStats.nodes = nodes;
- programStats.time = time;
- programStats.score = curscore;
- programStats.got_only_move = 0;
+ if(*bestMove) { // rememer time best EPD move was first found
+ int ff1, tf1, fr1, tr1, ff2, tf2, fr2, tr2; char pp1, pp2;
+ ChessMove mt;
+ int ok = ParseOneMove(bestMove, forwardMostMove, &mt, &ff1, &fr1, &tf1, &tr1, &pp1);
+ ok &= ParseOneMove(pv, forwardMostMove, &mt, &ff2, &fr2, &tf2, &tr2, &pp2);
+ solvingTime = (ok && ff1==ff2 && fr1==fr2 && tf1==tf2 && tr1==tr2 && pp1==pp2 ? time : -1);
+ }
+
+ if(serverMoves && (time > 100 || time == 0 && plylev > 7)) {
+ char buf[MSG_SIZ];
+ FILE *f;
+ snprintf(buf, MSG_SIZ, "%s", appData.serverMovesName);
+ buf[strlen(buf)-1] = gameMode == MachinePlaysWhite ? 'w' :
+ gameMode == MachinePlaysBlack ? 'b' : cps->twoMachinesColor[0];
+ if(appData.debugMode) fprintf(debugFP, "write PV on file '%s'\n", buf);
+ if(f = fopen(buf, "w")) { // export PV to applicable PV file
+ fprintf(f, "%5.2f/%-2d %s", curscore/100., plylev, pv);
+ fclose(f);
+ }
+ else
+ /* TRANSLATORS: PV = principal variation, the variation the chess engine thinks is the best for everyone */
+ DisplayError(_("failed writing PV"), 0);
+ }
+
+ tempStats.depth = plylev;
+ tempStats.nodes = nodes;
+ tempStats.time = time;
+ tempStats.score = curscore;
+ tempStats.got_only_move = 0;
if(cps->nps >= 0) { /* [HGM] nps: use engine nodes or time to decrement clock */
int ticklen;
if(cps->nps == 0) ticklen = 10*time; // use engine reported time
else ticklen = (1000. * u64ToDouble(nodes)) / cps->nps; // convert node count to time
- if(WhiteOnMove(forwardMostMove))
+ if(WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysWhite ||
+ gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'w'))
whiteTimeRemaining = timeRemaining[0][forwardMostMove] - ticklen;
- else blackTimeRemaining = timeRemaining[1][forwardMostMove] - ticklen;
+ if(!WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysBlack ||
+ gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b'))
+ blackTimeRemaining = timeRemaining[1][forwardMostMove] - ticklen;
}
/* Buffer overflow protection */
- if (buf1[0] != NULLCHAR) {
- if (strlen(buf1) >= sizeof(programStats.movelist)
+ if (pv[0] != NULLCHAR) {
+ if (strlen(pv) >= sizeof(tempStats.movelist)
&& appData.debugMode) {
fprintf(debugFP,
- "PV is too long; using the first %d bytes.\n",
- sizeof(programStats.movelist) - 1);
+ "PV is too long; using the first %u bytes.\n",
+ (unsigned) sizeof(tempStats.movelist) - 1);
}
- safeStrCpy( programStats.movelist, buf1, sizeof(programStats.movelist) );
+ safeStrCpy( tempStats.movelist, pv, sizeof(tempStats.movelist)/sizeof(tempStats.movelist[0]) );
} else {
- sprintf(programStats.movelist, " no PV\n");
+ sprintf(tempStats.movelist, " no PV\n");
}
- if (programStats.seen_stat) {
- programStats.ok_to_send = 1;
+ if (tempStats.seen_stat) {
+ tempStats.ok_to_send = 1;
}
- if (strchr(programStats.movelist, '(') != NULL) {
- programStats.line_is_book = 1;
- programStats.nr_moves = 0;
- programStats.moves_left = 0;
+ if (strchr(tempStats.movelist, '(') != NULL) {
+ tempStats.line_is_book = 1;
+ tempStats.nr_moves = 0;
+ tempStats.moves_left = 0;
} else {
- programStats.line_is_book = 0;
+ tempStats.line_is_book = 0;
}
- SendProgramStatsToFrontend( cps, &programStats );
+ if(tempStats.score != 0 || tempStats.nodes != 0 || tempStats.time != 0)
+ programStats = tempStats; // [HGM] info: only set stats if genuine PV and not an info line
- /*
+ SendProgramStatsToFrontend( cps, &tempStats );
+
+ /*
[AS] Protect the thinkOutput buffer from overflow... this
is only useful if buf1 hasn't overflowed first!
*/
- sprintf(thinkOutput, "[%d]%c%+.2f %s%s",
- plylev,
- (gameMode == TwoMachinesPlay ?
- ToUpper(cps->twoMachinesColor[0]) : ' '),
- ((double) curscore) / 100.0,
- prefixHint ? lastHint : "",
- prefixHint ? " " : "" );
+ if(curscore >= MATE_SCORE)
+ snprintf(score_buf, MSG_SIZ, "#%d", curscore - MATE_SCORE);
+ else if(curscore <= -MATE_SCORE)
+ snprintf(score_buf, MSG_SIZ, "#%d", curscore + MATE_SCORE);
+ else
+ snprintf(score_buf, MSG_SIZ, "%+.2f", ((double) curscore) / 100.0);
+ snprintf(thinkOutput, sizeof(thinkOutput)/sizeof(thinkOutput[0]), "[%d]%c%s %s%s",
+ plylev,
+ (gameMode == TwoMachinesPlay ?
+ ToUpper(cps->twoMachinesColor[0]) : ' '),
+ score_buf,
+ prefixHint ? lastHint : "",
+ prefixHint ? " " : "" );
if( buf1[0] != NULLCHAR ) {
unsigned max_len = sizeof(thinkOutput) - strlen(thinkOutput) - 1;
- if( strlen(buf1) > max_len ) {
+ if( strlen(pv) > max_len ) {
if( appData.debugMode) {
fprintf(debugFP,"PV is too long for thinkOutput, truncating.\n");
}
- buf1[max_len+1] = '\0';
+ pv[max_len+1] = '\0';
}
- strcat( thinkOutput, buf1 );
+ strcat( thinkOutput, pv);
}
if (currentMove == forwardMostMove || gameMode == AnalyzeMode
|| gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
DisplayMove(currentMove - 1);
- DisplayAnalysis();
}
return;
* if there is only 1 legal move
*/
sscanf(p, "(only move) %s", buf1);
- sprintf(thinkOutput, "%s (only move)", buf1);
+ snprintf(thinkOutput, sizeof(thinkOutput)/sizeof(thinkOutput[0]), "%s (only move)", buf1);
sprintf(programStats.movelist, "%s (only move)", buf1);
programStats.depth = 1;
programStats.nr_moves = 1;
programStats.line_is_book = 1;
SendProgramStatsToFrontend( cps, &programStats );
-
- if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
+
+ if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
DisplayMove(currentMove - 1);
- DisplayAnalysis();
}
return;
} else if (sscanf(message,"stat01: %d " u64Display " %d %d %d %s",
programStats.nodes = nodes;
programStats.moves_left = mvleft;
programStats.nr_moves = mvtot;
- strcpy(programStats.move_name, mvname);
+ safeStrCpy(programStats.move_name, mvname, sizeof(programStats.move_name)/sizeof(programStats.move_name[0]));
programStats.ok_to_send = 1;
programStats.movelist[0] = '\0';
SendProgramStatsToFrontend( cps, &programStats );
- DisplayAnalysis();
return;
} else if (strncmp(message,"++",2) == 0) {
if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
DisplayMove(currentMove - 1);
- DisplayAnalysis();
}
return;
}
buf1[0] = NULLCHAR;
if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
- &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5)
+ &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5)
{
ChessProgramStats cpstats;
cpstats.movelist[0] = '\0';
if (buf1[0] != NULLCHAR) {
- safeStrCpy( cpstats.movelist, buf1, sizeof(cpstats.movelist) );
+ safeStrCpy( cpstats.movelist, buf1, sizeof(cpstats.movelist)/sizeof(cpstats.movelist[0]) );
}
cpstats.ok_to_send = 0;
/* Parse a game score from the character string "game", and
record it as the history of the current game. The game
- score is NOT assumed to start from the standard position.
+ score is NOT assumed to start from the standard position.
The display is not updated in any way.
*/
void
-ParseGameHistory(game)
- char *game;
+ParseGameHistory (char *game)
{
ChessMove moveType;
int fromX, fromY, toX, toY, boardIndex;
yynewstr(game);
for (;;) {
yyboardindex = boardIndex;
- moveType = (ChessMove) yylex();
+ moveType = (ChessMove) Myylex();
switch (moveType) {
case IllegalMove: /* maybe suicide chess, etc. */
if (appData.debugMode) {
fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
setbuf(debugFP, NULL);
}
- case WhitePromotionChancellor:
- case BlackPromotionChancellor:
- case WhitePromotionArchbishop:
- case BlackPromotionArchbishop:
- case WhitePromotionQueen:
- case BlackPromotionQueen:
- case WhitePromotionRook:
- case BlackPromotionRook:
- case WhitePromotionBishop:
- case BlackPromotionBishop:
- case WhitePromotionKnight:
- case BlackPromotionKnight:
- case WhitePromotionKing:
- case BlackPromotionKing:
+ case WhitePromotion:
+ case BlackPromotion:
+ case WhiteNonPromotion:
+ case BlackNonPromotion:
case NormalMove:
+ case FirstLeg:
case WhiteCapturesEnPassant:
case BlackCapturesEnPassant:
case WhiteKingSideCastle:
break;
case WhiteDrop:
case BlackDrop:
+ if(currentMoveString[0] == '@') continue; // no null moves in ICS mode!
fromX = moveType == WhiteDrop ?
(int) CharToPiece(ToUpper(currentMoveString[0])) :
(int) CharToPiece(ToLower(currentMoveString[0]));
break;
case AmbiguousMove:
/* bug? */
- sprintf(buf, _("Ambiguous move in ICS output: \"%s\""), yy_text);
+ snprintf(buf, MSG_SIZ, _("Ambiguous move in ICS output: \"%s\""), yy_text);
if (appData.debugMode) {
fprintf(debugFP, "Ambiguous move from ICS: '%s'\n", yy_text);
fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
return;
case ImpossibleMove:
/* bug? */
- sprintf(buf, _("Illegal move in ICS output: \"%s\""), yy_text);
+ snprintf(buf, MSG_SIZ, _("Illegal move in ICS output: \"%s\""), yy_text);
if (appData.debugMode) {
fprintf(debugFP, "Impossible move from ICS: '%s'\n", yy_text);
fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
}
DisplayError(buf, 0);
return;
- case (ChessMove) 0: /* end of file */
+ case EndOfFile:
if (boardIndex < backwardMostMove) {
/* Oops, gap. How did that happen? */
DisplayError(_("Gap in move list"), 0);
if (q != NULL) *q = NULLCHAR;
p++;
}
+ while(q = strchr(p, '\n')) *q = ' '; // [HGM] crush linefeeds in result message
gameInfo.resultDetails = StrSave(p);
continue;
}
return;
}
(void) CoordsToAlgebraic(boards[boardIndex], PosFlags(boardIndex),
- EP_UNKNOWN, fromY, fromX, toY, toX, promoChar,
+ fromY, fromX, toY, toX, promoChar,
parseList[boardIndex]);
CopyBoard(boards[boardIndex + 1], boards[boardIndex]);
- {int i; for(i=0; i<BOARD_SIZE; i++) castlingRights[boardIndex+1][i] = castlingRights[boardIndex][i];}
/* currentMoveString is set as a side-effect of yylex */
- strcpy(moveList[boardIndex], currentMoveString);
+ safeStrCpy(moveList[boardIndex], currentMoveString, sizeof(moveList[boardIndex])/sizeof(moveList[boardIndex][0]));
strcat(moveList[boardIndex], "\n");
boardIndex++;
- ApplyMove(fromX, fromY, toX, toY, promoChar, boards[boardIndex],
- castlingRights[boardIndex], &epStatus[boardIndex]);
- switch (MateTest(boards[boardIndex], PosFlags(boardIndex),
- EP_UNKNOWN, castlingRights[boardIndex]) ) {
+ ApplyMove(fromX, fromY, toX, toY, promoChar, boards[boardIndex]);
+ switch (MateTest(boards[boardIndex], PosFlags(boardIndex)) ) {
case MT_NONE:
case MT_STALEMATE:
default:
break;
case MT_CHECK:
- if(gameInfo.variant != VariantShogi)
+ if(!IS_SHOGI(gameInfo.variant))
strcat(parseList[boardIndex - 1], "+");
break;
case MT_CHECKMATE:
/* Apply a move to the given board */
void
-ApplyMove(fromX, fromY, toX, toY, promoChar, board, castling, ep)
- int fromX, fromY, toX, toY;
- int promoChar;
- Board board;
- char *castling;
- char *ep;
+ApplyMove (int fromX, int fromY, int toX, int toY, int promoChar, Board board)
{
- ChessSquare captured = board[toY][toX], piece, king; int p, oldEP = EP_NONE, berolina = 0;
+ ChessSquare captured = board[toY][toX], piece, pawn, king, killed, killed2; int p, rookX, oldEP, epRank, berolina = 0;
+ int promoRank = gameInfo.variant == VariantMakruk || gameInfo.variant == VariantGrand || gameInfo.variant == VariantChuChess ? 3 : 1;
/* [HGM] compute & store e.p. status and castling rights for new position */
/* we can always do that 'in place', now pointers to these rights are passed to ApplyMove */
- { int i;
if(gameInfo.variant == VariantBerolina) berolina = EP_BEROLIN_A;
- oldEP = *ep;
- *ep = EP_NONE;
+ oldEP = (signed char)board[EP_FILE]; epRank = board[EP_RANK];
+ board[EP_STATUS] = EP_NONE;
+ board[EP_FILE] = board[EP_RANK] = 100;
+
+ if (fromY == DROP_RANK) {
+ /* must be first */
+ if(fromX == EmptySquare) { // [HGM] pass: empty drop encodes null move; nothing to change.
+ board[EP_STATUS] = EP_CAPTURE; // null move considered irreversible
+ return;
+ }
+ piece = board[toY][toX] = (ChessSquare) fromX;
+ } else {
+// ChessSquare victim;
+ int i;
+
+ if( killX >= 0 && killY >= 0 ) { // [HGM] lion: Lion trampled over something
+// victim = board[killY][killX],
+ killed = board[killY][killX],
+ board[killY][killX] = EmptySquare,
+ board[EP_STATUS] = EP_CAPTURE;
+ if( kill2X >= 0 && kill2Y >= 0)
+ killed2 = board[kill2Y][kill2X], board[kill2Y][kill2X] = EmptySquare;
+ }
- if( board[toY][toX] != EmptySquare )
- *ep = EP_CAPTURE;
+ if( board[toY][toX] != EmptySquare ) {
+ board[EP_STATUS] = EP_CAPTURE;
+ if( (fromX != toX || fromY != toY) && // not igui!
+ (captured == WhiteLion && board[fromY][fromX] != BlackLion ||
+ captured == BlackLion && board[fromY][fromX] != WhiteLion ) ) { // [HGM] lion: Chu Lion-capture rules
+ board[EP_STATUS] = EP_IRON_LION; // non-Lion x Lion: no counter-strike allowed
+ }
+ }
- if( board[fromY][fromX] == WhitePawn ) {
+ pawn = board[fromY][fromX];
+ if( pawn == WhiteLance || pawn == BlackLance ) {
+ if( gameInfo.variant != VariantSuper && gameInfo.variant != VariantChu ) {
+ if(gameInfo.variant == VariantSpartan) board[EP_STATUS] = EP_PAWN_MOVE; // in Spartan no e.p. rights must be set
+ else pawn += WhitePawn - WhiteLance; // Lance is Pawn-like in most variants, so let Pawn code treat it by this kludge
+ }
+ }
+ if( pawn == WhitePawn ) {
if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
- *ep = EP_PAWN_MOVE;
- if( toY-fromY==2) {
+ board[EP_STATUS] = EP_PAWN_MOVE;
+ if( toY-fromY>=2) {
+ board[EP_FILE] = (fromX + toX)/2; board[EP_RANK] = toY - 1 | 128*(toY - fromY > 2);
if(toX>BOARD_LEFT && board[toY][toX-1] == BlackPawn &&
gameInfo.variant != VariantBerolina || toX < fromX)
- *ep = toX | berolina;
+ board[EP_STATUS] = toX | berolina;
if(toX<BOARD_RGHT-1 && board[toY][toX+1] == BlackPawn &&
- gameInfo.variant != VariantBerolina || toX > fromX)
- *ep = toX;
+ gameInfo.variant != VariantBerolina || toX > fromX)
+ board[EP_STATUS] = toX;
}
- } else
- if( board[fromY][fromX] == BlackPawn ) {
+ } else
+ if( pawn == BlackPawn ) {
if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
- *ep = EP_PAWN_MOVE;
- if( toY-fromY== -2) {
+ board[EP_STATUS] = EP_PAWN_MOVE;
+ if( toY-fromY<= -2) {
+ board[EP_FILE] = (fromX + toX)/2; board[EP_RANK] = toY + 1 | 128*(fromY - toY > 2);
if(toX>BOARD_LEFT && board[toY][toX-1] == WhitePawn &&
gameInfo.variant != VariantBerolina || toX < fromX)
- *ep = toX | berolina;
+ board[EP_STATUS] = toX | berolina;
if(toX<BOARD_RGHT-1 && board[toY][toX+1] == WhitePawn &&
- gameInfo.variant != VariantBerolina || toX > fromX)
- *ep = toX;
+ gameInfo.variant != VariantBerolina || toX > fromX)
+ board[EP_STATUS] = toX;
}
}
+ if(fromY == 0) board[TOUCHED_W] |= 1<<fromX; else // new way to keep track of virginity
+ if(fromY == BOARD_HEIGHT-1) board[TOUCHED_B] |= 1<<fromX;
+ if(toY == 0) board[TOUCHED_W] |= 1<<toX; else
+ if(toY == BOARD_HEIGHT-1) board[TOUCHED_B] |= 1<<toX;
+
for(i=0; i<nrCastlingRights; i++) {
- if(castling[i] == fromX && castlingRank[i] == fromY ||
- castling[i] == toX && castlingRank[i] == toY
- ) castling[i] = -1; // revoke for moved or captured piece
+ if(board[CASTLING][i] == fromX && castlingRank[i] == fromY ||
+ board[CASTLING][i] == toX && castlingRank[i] == toY
+ ) board[CASTLING][i] = NoRights; // revoke for moved or captured piece
}
- }
+ if(gameInfo.variant == VariantSChess) { // update virginity
+ if(fromY == 0) board[VIRGIN][fromX] &= ~VIRGIN_W; // loss by moving
+ if(fromY == BOARD_HEIGHT-1) board[VIRGIN][fromX] &= ~VIRGIN_B;
+ if(toY == 0) board[VIRGIN][toX] &= ~VIRGIN_W; // loss by capture
+ if(toY == BOARD_HEIGHT-1) board[VIRGIN][toX] &= ~VIRGIN_B;
+ }
- /* [HGM] In Shatranj and Courier all promotions are to Ferz */
- if((gameInfo.variant==VariantShatranj || gameInfo.variant==VariantCourier)
- && promoChar != 0) promoChar = PieceToChar(WhiteFerz);
-
- if (fromX == toX && fromY == toY) return;
+ if (fromX == toX && fromY == toY) return;
- if (fromY == DROP_RANK) {
- /* must be first */
- piece = board[toY][toX] = (ChessSquare) fromX;
- } else {
piece = board[fromY][fromX]; /* [HGM] remember, for Shogi promotion */
king = piece < (int) BlackPawn ? WhiteKing : BlackKing; /* [HGM] Knightmate simplify testing for castling */
if(gameInfo.variant == VariantKnightmate)
king += (int) WhiteUnicorn - (int) WhiteKing;
+ if(pieceDesc[piece] && killX >= 0 && strchr(pieceDesc[piece], 'O') // Betza castling-enabled
+ && (piece < BlackPawn ? killed < BlackPawn : killed >= BlackPawn)) { // and tramples own
+ board[toY][toX] = piece; board[fromY][fromX] = EmptySquare;
+ board[toY][toX + (killX < fromX ? 1 : -1)] = killed;
+ board[EP_STATUS] = EP_NONE; // capture was fake!
+ } else
+ if(nrCastlingRights == 0 && board[toY][toX] < EmptySquare && (piece < BlackPawn) == (board[toY][toX] < BlackPawn)) {
+ board[fromY][fromX] = board[toY][toX]; // capture own will lead to swapping
+ board[toY][toX] = piece;
+ board[EP_STATUS] = EP_NONE; // capture was fake!
+ } else
/* Code added by Tord: */
- /* FRC castling assumed when king captures friendly rook. */
- if (board[fromY][fromX] == WhiteKing &&
- board[toY][toX] == WhiteRook) {
+ /* FRC castling assumed when king captures friendly rook. [HGM] or RxK for S-Chess */
+ if (board[fromY][fromX] == WhiteKing && board[toY][toX] == WhiteRook ||
+ board[fromY][fromX] == WhiteRook && board[toY][toX] == WhiteKing) {
+ board[EP_STATUS] = EP_NONE; // capture was fake!
board[fromY][fromX] = EmptySquare;
board[toY][toX] = EmptySquare;
- if(toX > fromX) {
+ if((toX > fromX) != (piece == WhiteRook)) {
board[0][BOARD_RGHT-2] = WhiteKing; board[0][BOARD_RGHT-3] = WhiteRook;
} else {
board[0][BOARD_LEFT+2] = WhiteKing; board[0][BOARD_LEFT+3] = WhiteRook;
}
- } else if (board[fromY][fromX] == BlackKing &&
- board[toY][toX] == BlackRook) {
+ } else if (board[fromY][fromX] == BlackKing && board[toY][toX] == BlackRook ||
+ board[fromY][fromX] == BlackRook && board[toY][toX] == BlackKing) {
+ board[EP_STATUS] = EP_NONE;
board[fromY][fromX] = EmptySquare;
board[toY][toX] = EmptySquare;
- if(toX > fromX) {
+ if((toX > fromX) != (piece == BlackRook)) {
board[BOARD_HEIGHT-1][BOARD_RGHT-2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_RGHT-3] = BlackRook;
} else {
board[BOARD_HEIGHT-1][BOARD_LEFT+2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_LEFT+3] = BlackRook;
}
/* End of code added by Tord */
+ } else if (pieceDesc[piece] && piece == king && !strchr(pieceDesc[piece], 'O') && strchr(pieceDesc[piece], 'i')) {
+ board[fromY][fromX] = EmptySquare; // never castle if King has virgin moves defined on it other than castling
+ board[toY][toX] = piece;
} else if (board[fromY][fromX] == king
&& fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
&& toY == fromY && toX > fromX+1) {
+ for(rookX=fromX+1; board[toY][rookX] == EmptySquare && rookX < BOARD_RGHT-1; rookX++); // castle with nearest piece
+ board[fromY][toX-1] = board[fromY][rookX];
+ board[fromY][rookX] = EmptySquare;
board[fromY][fromX] = EmptySquare;
board[toY][toX] = king;
- board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
- board[fromY][BOARD_RGHT-1] = EmptySquare;
} else if (board[fromY][fromX] == king
&& fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
&& toY == fromY && toX < fromX-1) {
+ for(rookX=fromX-1; board[toY][rookX] == EmptySquare && rookX > 0; rookX--); // castle with nearest piece
+ board[fromY][toX+1] = board[fromY][rookX];
+ board[fromY][rookX] = EmptySquare;
board[fromY][fromX] = EmptySquare;
board[toY][toX] = king;
- board[toY][toX+1] = board[fromY][BOARD_LEFT];
- board[fromY][BOARD_LEFT] = EmptySquare;
- } else if (board[fromY][fromX] == WhitePawn
- && toY == BOARD_HEIGHT-1
- && gameInfo.variant != VariantXiangqi
+ } else if ((board[fromY][fromX] == WhitePawn && gameInfo.variant != VariantXiangqi ||
+ board[fromY][fromX] == WhiteLance && gameInfo.variant != VariantSuper && gameInfo.variant != VariantChu)
+ && toY >= BOARD_HEIGHT-promoRank && promoChar // defaulting to Q is done elsewhere
) {
/* white pawn promotion */
board[toY][toX] = CharToPiece(ToUpper(promoChar));
- if (board[toY][toX] == EmptySquare) {
- board[toY][toX] = WhiteQueen;
- }
- if(gameInfo.variant==VariantBughouse ||
- gameInfo.variant==VariantCrazyhouse) /* [HGM] use shadow piece */
- board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
+ if(board[toY][toX] < WhiteCannon && PieceToChar(PROMOTED(board[toY][toX])) == '~') /* [HGM] use shadow piece (if available) */
+ board[toY][toX] = (ChessSquare) (PROMOTED(board[toY][toX]));
board[fromY][fromX] = EmptySquare;
- } else if ((fromY == BOARD_HEIGHT-4)
+ } else if ((fromY >= BOARD_HEIGHT>>1)
+ && (oldEP == toX || oldEP == EP_UNKNOWN || appData.testLegality || abs(toX - fromX) > 4)
&& (toX != fromX)
&& gameInfo.variant != VariantXiangqi
&& gameInfo.variant != VariantBerolina
- && (board[fromY][fromX] == WhitePawn)
+ && (pawn == WhitePawn)
&& (board[toY][toX] == EmptySquare)) {
board[fromY][fromX] = EmptySquare;
- board[toY][toX] = WhitePawn;
- captured = board[toY - 1][toX];
- board[toY - 1][toX] = EmptySquare;
+ board[toY][toX] = piece;
+ if(toY == epRank - 128 + 1)
+ captured = board[toY - 2][toX], board[toY - 2][toX] = EmptySquare;
+ else
+ captured = board[toY - 1][toX], board[toY - 1][toX] = EmptySquare;
} else if ((fromY == BOARD_HEIGHT-4)
&& (toX == fromX)
&& gameInfo.variant == VariantBerolina
} else if (board[fromY][fromX] == king
&& fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
&& toY == fromY && toX > fromX+1) {
+ for(rookX=toX+1; board[toY][rookX] == EmptySquare && rookX < BOARD_RGHT - 1; rookX++);
+ board[fromY][toX-1] = board[fromY][rookX];
+ board[fromY][rookX] = EmptySquare;
board[fromY][fromX] = EmptySquare;
board[toY][toX] = king;
- board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
- board[fromY][BOARD_RGHT-1] = EmptySquare;
} else if (board[fromY][fromX] == king
&& fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
&& toY == fromY && toX < fromX-1) {
+ for(rookX=toX-1; board[toY][rookX] == EmptySquare && rookX > 0; rookX--);
+ board[fromY][toX+1] = board[fromY][rookX];
+ board[fromY][rookX] = EmptySquare;
board[fromY][fromX] = EmptySquare;
board[toY][toX] = king;
- board[toY][toX+1] = board[fromY][BOARD_LEFT];
- board[fromY][BOARD_LEFT] = EmptySquare;
} else if (fromY == 7 && fromX == 3
&& board[fromY][fromX] == BlackKing
&& toY == 7 && toX == 5) {
board[toY][toX] = BlackKing;
board[fromY][0] = EmptySquare;
board[toY][2] = BlackRook;
- } else if (board[fromY][fromX] == BlackPawn
- && toY == 0
- && gameInfo.variant != VariantXiangqi
+ } else if ((board[fromY][fromX] == BlackPawn && gameInfo.variant != VariantXiangqi ||
+ board[fromY][fromX] == BlackLance && gameInfo.variant != VariantSuper && gameInfo.variant != VariantChu)
+ && toY < promoRank && promoChar
) {
/* black pawn promotion */
- board[0][toX] = CharToPiece(ToLower(promoChar));
- if (board[0][toX] == EmptySquare) {
- board[0][toX] = BlackQueen;
- }
- if(gameInfo.variant==VariantBughouse ||
- gameInfo.variant==VariantCrazyhouse) /* [HGM] use shadow piece */
- board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
+ board[toY][toX] = CharToPiece(ToLower(promoChar));
+ if(board[toY][toX] < BlackCannon && PieceToChar(PROMOTED(board[toY][toX])) == '~') /* [HGM] use shadow piece (if available) */
+ board[toY][toX] = (ChessSquare) (PROMOTED(board[toY][toX]));
board[fromY][fromX] = EmptySquare;
- } else if ((fromY == 3)
+ } else if ((fromY < BOARD_HEIGHT>>1)
+ && (oldEP == toX || oldEP == EP_UNKNOWN || appData.testLegality || abs(toX - fromX) > 4)
&& (toX != fromX)
&& gameInfo.variant != VariantXiangqi
&& gameInfo.variant != VariantBerolina
- && (board[fromY][fromX] == BlackPawn)
+ && (pawn == BlackPawn)
&& (board[toY][toX] == EmptySquare)) {
board[fromY][fromX] = EmptySquare;
- board[toY][toX] = BlackPawn;
- captured = board[toY + 1][toX];
- board[toY + 1][toX] = EmptySquare;
+ board[toY][toX] = piece;
+ if(toY == epRank - 128 - 1)
+ captured = board[toY + 2][toX], board[toY + 2][toX] = EmptySquare;
+ else
+ captured = board[toY + 1][toX], board[toY + 1][toX] = EmptySquare;
} else if ((fromY == 3)
&& (toX == fromX)
&& gameInfo.variant == VariantBerolina
board[fromY][fromX+1] = EmptySquare;
}
} else {
- board[toY][toX] = board[fromY][fromX];
+ ChessSquare piece = board[fromY][fromX]; // [HGM] lion: allow for igui (where from == to)
board[fromY][fromX] = EmptySquare;
+ board[toY][toX] = piece;
}
-
- /* [HGM] now we promote for Shogi, if needed */
- if(gameInfo.variant == VariantShogi && promoChar == 'q')
- board[toY][toX] = (ChessSquare) (PROMOTED piece);
}
if (gameInfo.holdingsWidth != 0) {
/* [HGM] OK, so I have written it. Holdings are stored in the */
/* penultimate board files, so they are automaticlly stored */
/* in the game history. */
- if (fromY == DROP_RANK) {
+ if (fromY == DROP_RANK || gameInfo.variant == VariantSChess
+ && promoChar && piece != WhitePawn && piece != BlackPawn) {
/* Delete from holdings, by decreasing count */
/* and erasing image if necessary */
- p = (int) fromX;
+ p = fromY == DROP_RANK ? (int) fromX : CharToPiece(piece > BlackPawn ? ToLower(promoChar) : ToUpper(promoChar));
if(p < (int) BlackPawn) { /* white drop */
p -= (int)WhitePawn;
+ p = PieceToNumber((ChessSquare)p);
if(p >= gameInfo.holdingsSize) p = 0;
- if(--board[p][BOARD_WIDTH-2] == 0)
+ if(--board[p][BOARD_WIDTH-2] <= 0)
board[p][BOARD_WIDTH-1] = EmptySquare;
+ if((int)board[p][BOARD_WIDTH-2] < 0)
+ board[p][BOARD_WIDTH-2] = 0;
} else { /* black drop */
p -= (int)BlackPawn;
+ p = PieceToNumber((ChessSquare)p);
if(p >= gameInfo.holdingsSize) p = 0;
- if(--board[BOARD_HEIGHT-1-p][1] == 0)
+ if(--board[BOARD_HEIGHT-1-p][1] <= 0)
board[BOARD_HEIGHT-1-p][0] = EmptySquare;
+ if((int)board[BOARD_HEIGHT-1-p][1] < 0)
+ board[BOARD_HEIGHT-1-p][1] = 0;
}
}
if (captured != EmptySquare && gameInfo.holdingsSize > 0
- && gameInfo.variant != VariantBughouse ) {
+ && gameInfo.variant != VariantBughouse && gameInfo.variant != VariantSChess ) {
/* [HGM] holdings: Add to holdings, if holdings exist */
- if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) {
+ if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) {
// [HGM] superchess: suppress flipping color of captured pieces by reverse pre-flip
captured = (int) captured >= (int) BlackPawn ? BLACK_TO_WHITE captured : WHITE_TO_BLACK captured;
}
p = (int) captured;
if (p >= (int) BlackPawn) {
p -= (int)BlackPawn;
- if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
- /* in Shogi restore piece to its original first */
- captured = (ChessSquare) (DEMOTED captured);
- p = DEMOTED p;
+ if(DEMOTED(p) >= 0 && PieceToChar(p) == '+') {
+ /* Restore shogi-promoted piece to its original first */
+ captured = (ChessSquare) (DEMOTED(captured));
+ p = DEMOTED(p);
}
p = PieceToNumber((ChessSquare)p);
if(p >= gameInfo.holdingsSize) { p = 0; captured = BlackPawn; }
board[p][BOARD_WIDTH-1] = BLACK_TO_WHITE captured;
} else {
p -= (int)WhitePawn;
- if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
- captured = (ChessSquare) (DEMOTED captured);
- p = DEMOTED p;
+ if(DEMOTED(p) >= 0 && PieceToChar(p) == '+') {
+ captured = (ChessSquare) (DEMOTED(captured));
+ p = DEMOTED(p);
}
p = PieceToNumber((ChessSquare)p);
if(p >= gameInfo.holdingsSize) { p = 0; captured = WhitePawn; }
board[BOARD_HEIGHT-1-p][0] = WHITE_TO_BLACK captured;
}
}
-
} else if (gameInfo.variant == VariantAtomic) {
if (captured != EmptySquare) {
int y, x;
board[toY][toX] = EmptySquare;
}
}
- if(gameInfo.variant == VariantShogi && promoChar != NULLCHAR && promoChar != '=') {
- /* [HGM] Shogi promotions */
- board[toY][toX] = (ChessSquare) (PROMOTED piece);
- }
- if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat)
- && promoChar != NULLCHAR && gameInfo.holdingsSize) {
+ if(gameInfo.variant == VariantSChess && promoChar != NULLCHAR && promoChar != '=' && piece != WhitePawn && piece != BlackPawn) {
+ board[fromY][fromX] = CharToPiece(piece < BlackPawn ? ToUpper(promoChar) : ToLower(promoChar)); // S-Chess gating
+ } else
+ if(promoChar == '+') {
+ /* [HGM] Shogi-style promotions, to piece implied by original (Might overwrite ordinary Pawn promotion) */
+ board[toY][toX] = (ChessSquare) (CHUPROMOTED(piece));
+ if(gameInfo.variant == VariantChuChess && (piece == WhiteKnight || piece == BlackKnight))
+ board[toY][toX] = piece + WhiteLion - WhiteKnight; // adjust Knight promotions to Lion
+ } else if(!appData.testLegality && promoChar != NULLCHAR && promoChar != '=') { // without legality testing, unconditionally believe promoChar
+ ChessSquare newPiece = CharToPiece(piece < BlackPawn ? ToUpper(promoChar) : ToLower(promoChar));
+ if((newPiece <= WhiteMan || newPiece >= BlackPawn && newPiece <= BlackMan) // unpromoted piece specified
+ && pieceToChar[PROMOTED(newPiece)] == '~') newPiece = PROMOTED(newPiece);// but promoted version available
+ board[toY][toX] = newPiece;
+ }
+ if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand)
+ && promoChar != NULLCHAR && gameInfo.holdingsSize) {
// [HGM] superchess: take promotion piece out of holdings
int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
if((int)piece < (int)BlackPawn) { // determine stm from piece color
board[BOARD_HEIGHT-1-k][0] = EmptySquare;
}
}
-
}
/* Updates forwardMostMove */
void
-MakeMove(fromX, fromY, toX, toY, promoChar)
- int fromX, fromY, toX, toY;
- int promoChar;
+MakeMove (int fromX, int fromY, int toX, int toY, int promoChar)
{
+ int x = toX, y = toY;
+ char *s = parseList[forwardMostMove];
+ ChessSquare p = boards[forwardMostMove][toY][toX];
// forwardMostMove++; // [HGM] bare: moved downstream
+ if(killX >= 0 && killY >= 0) x = killX, y = killY; // [HGM] lion: make SAN move to intermediate square, if there is one
+ (void) CoordsToAlgebraic(boards[forwardMostMove],
+ PosFlags(forwardMostMove),
+ fromY, fromX, y, x, promoChar,
+ s);
+ if(killX >= 0 && killY >= 0)
+ sprintf(s + strlen(s), "%c%c%d", p == EmptySquare || toX == fromX && toY == fromY ? '-' : 'x', toX + AAA, toY + ONE - '0');
+
if(serverMoves != NULL) { /* [HGM] write moves on file for broadcasting (should be separate routine, really) */
int timeLeft; static int lastLoadFlag=0; int king, piece;
piece = boards[forwardMostMove][fromY][fromX];
if(gameInfo.variant == VariantKnightmate)
king += (int) WhiteUnicorn - (int) WhiteKing;
if(forwardMostMove == 0) {
- if(blackPlaysFirst)
+ if(gameMode == MachinePlaysBlack || gameMode == BeginningOfGame)
+ fprintf(serverMoves, "%s;", UserName());
+ else if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b')
fprintf(serverMoves, "%s;", second.tidy);
fprintf(serverMoves, "%s;", first.tidy);
- if(!blackPlaysFirst)
+ if(gameMode == MachinePlaysWhite)
+ fprintf(serverMoves, "%s;", UserName());
+ else if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w')
fprintf(serverMoves, "%s;", second.tidy);
} else fprintf(serverMoves, loadFlag|lastLoadFlag ? ":" : ";");
lastLoadFlag = loadFlag;
if( (boards[forwardMostMove][fromY][fromX] == WhitePawn ||
boards[forwardMostMove][fromY][fromX] == BlackPawn ) &&
boards[forwardMostMove][toY][toX] == EmptySquare
- && fromX != toX )
+ && fromX != toX && fromY != toY)
fprintf(serverMoves, ":%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+fromY);
// promotion suffix
- if(promoChar != NULLCHAR)
- fprintf(serverMoves, ":%c:%c%c", promoChar, AAA+toX, ONE+toY);
+ if(promoChar != NULLCHAR) {
+ if(fromY == 0 || fromY == BOARD_HEIGHT-1)
+ fprintf(serverMoves, ":%c%c:%c%c", WhiteOnMove(forwardMostMove) ? 'w' : 'b',
+ ToLower(promoChar), AAA+fromX, ONE+fromY); // Seirawan gating
+ else fprintf(serverMoves, ":%c:%c%c", ToLower(promoChar), AAA+toX, ONE+toY);
+ }
if(!loadFlag) {
+ char buf[MOVE_LEN*2], *p; int len;
fprintf(serverMoves, "/%d/%d",
pvInfoList[forwardMostMove].depth, pvInfoList[forwardMostMove].score);
if(forwardMostMove+1 & 1) timeLeft = whiteTimeRemaining/1000;
else timeLeft = blackTimeRemaining/1000;
fprintf(serverMoves, "/%d", timeLeft);
+ strncpy(buf, parseList[forwardMostMove], MOVE_LEN*2);
+ if(p = strchr(buf, '/')) *p = NULLCHAR; else
+ if(p = strchr(buf, '=')) *p = NULLCHAR;
+ len = strlen(buf); if(len > 1 && buf[len-2] != '-') buf[len-2] = NULLCHAR; // strip to-square
+ fprintf(serverMoves, "/%s", buf);
}
fflush(serverMoves);
}
- if (forwardMostMove+1 >= MAX_MOVES) {
- DisplayFatalError(_("Game too long; increase MAX_MOVES and recompile"),
- 0, 1);
+ if (forwardMostMove+1 > framePtr) { // [HGM] vari: do not run into saved variations..
+ GameEnds(GameUnfinished, _("Game too long; increase MAX_MOVES and recompile"), GE_XBOARD);
return;
}
+ UnLoadPV(); // [HGM] pv: if we are looking at a PV, abort this
if (commentList[forwardMostMove+1] != NULL) {
free(commentList[forwardMostMove+1]);
commentList[forwardMostMove+1] = NULL;
}
CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
- {int i; for(i=0; i<BOARD_SIZE; i++) castlingRights[forwardMostMove+1][i] = castlingRights[forwardMostMove][i];}
- ApplyMove(fromX, fromY, toX, toY, promoChar, boards[forwardMostMove+1],
- castlingRights[forwardMostMove+1], &epStatus[forwardMostMove+1]);
- forwardMostMove++; // [HGM] bare: moved to after ApplyMove, to make sure clock interrupt finds complete board
- SwitchClocks(); // uses forwardMostMove, so must be done after incrementing it !
+ ApplyMove(fromX, fromY, toX, toY, promoChar, boards[forwardMostMove+1]);
+ // forwardMostMove++; // [HGM] bare: moved to after ApplyMove, to make sure clock interrupt finds complete board
+ SwitchClocks(forwardMostMove+1); // [HGM] race: incrementing move nr inside
timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
timeRemaining[1][forwardMostMove] = blackTimeRemaining;
+ adjustedClock = FALSE;
gameInfo.result = GameUnfinished;
if (gameInfo.resultDetails != NULL) {
free(gameInfo.resultDetails);
}
CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar,
moveList[forwardMostMove - 1]);
- (void) CoordsToAlgebraic(boards[forwardMostMove - 1],
- PosFlags(forwardMostMove - 1), EP_UNKNOWN,
- fromY, fromX, toY, toX, promoChar,
- parseList[forwardMostMove - 1]);
- switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove),
- epStatus[forwardMostMove], /* [HGM] use true e.p. */
- castlingRights[forwardMostMove]) ) {
+ switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
case MT_NONE:
case MT_STALEMATE:
default:
break;
case MT_CHECK:
- if(gameInfo.variant != VariantShogi)
+ if(!IS_SHOGI(gameInfo.variant))
strcat(parseList[forwardMostMove - 1], "+");
break;
case MT_CHECKMATE:
strcat(parseList[forwardMostMove - 1], "#");
break;
}
- if (appData.debugMode) {
- fprintf(debugFP, "move: %s, parse: %s (%c)\n", moveList[forwardMostMove-1], parseList[forwardMostMove-1], moveList[forwardMostMove-1][4]);
- }
-
}
/* Updates currentMove if not pausing */
void
-ShowMove(fromX, fromY, toX, toY)
+ShowMove (int fromX, int fromY, int toX, int toY)
{
int instant = (gameMode == PlayFromGameFile) ?
(matchMode || (appData.timeDelay == 0 && !pausing)) : pausing;
AnimateMove(boards[forwardMostMove - 1],
fromX, fromY, toX, toY);
}
- if (appData.highlightLastMove) {
- SetHighlights(fromX, fromY, toX, toY);
- }
}
currentMove = forwardMostMove;
}
+ killX = killY = -1; // [HGM] lion: used up
+
if (instant) return;
DisplayMove(currentMove - 1);
+ if (!pausing || gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
+ if (appData.highlightLastMove) { // [HGM] moved to after DrawPosition, as with arrow it could redraw old board
+ SetHighlights(fromX, fromY, toX, toY);
+ }
+ }
DrawPosition(FALSE, boards[currentMove]);
DisplayBothClocks();
HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
}
-void SendEgtPath(ChessProgramState *cps)
+void
+SendEgtPath (ChessProgramState *cps)
{ /* [HGM] EGT: match formats given in feature with those given by user, and send info for each match */
char buf[MSG_SIZ], name[MSG_SIZ], *p;
name[0] = ','; // extract next format name from feature and copy with prefixed ','
while(*p && *p != ',') *q++ = *p++;
*q++ = ':'; *q = 0;
- if( appData.defaultPathEGTB && appData.defaultPathEGTB[0] &&
+ if( appData.defaultPathEGTB && appData.defaultPathEGTB[0] &&
strcmp(name, ",nalimov:") == 0 ) {
// take nalimov path from the menu-changeable option first, if it is defined
- sprintf(buf, "egtpath nalimov %s\n", appData.defaultPathEGTB);
+ snprintf(buf, MSG_SIZ, "egtpath nalimov %s\n", appData.defaultPathEGTB);
SendToProgram(buf,cps); // send egtbpath command for nalimov
} else
if( (s = StrStr(appData.egtFormats, name+1)) == appData.egtFormats ||
while(*r && *r != ',') r++; // path info is everything upto next ';' or end of string
c = *r; *r = 0; // temporarily null-terminate path info
*--q = 0; // strip of trailig ':' from name
- sprintf(buf, "egtpath %s %s\n", name+1, s);
+ snprintf(buf, MSG_SIZ, "egtpath %s %s\n", name+1, s);
*r = c;
SendToProgram(buf,cps); // send egtbpath command for this format
}
}
}
+static int
+NonStandardBoardSize (VariantClass v, int boardWidth, int boardHeight, int holdingsSize)
+{
+ int width = 8, height = 8, holdings = 0; // most common sizes
+ if( v == VariantUnknown || *engineVariant) return 0; // engine-defined name never needs prefix
+ // correct the deviations default for each variant
+ if( v == VariantXiangqi ) width = 9, height = 10;
+ if( v == VariantShogi ) width = 9, height = 9, holdings = 7;
+ if( v == VariantBughouse || v == VariantCrazyhouse) holdings = 5;
+ if( v == VariantCapablanca || v == VariantCapaRandom ||
+ v == VariantGothic || v == VariantFalcon || v == VariantJanus )
+ width = 10;
+ if( v == VariantCourier ) width = 12;
+ if( v == VariantSuper ) holdings = 8;
+ if( v == VariantGreat ) width = 10, holdings = 8;
+ if( v == VariantSChess ) holdings = 7;
+ if( v == VariantGrand ) width = 10, height = 10, holdings = 7;
+ if( v == VariantChuChess) width = 10, height = 10;
+ if( v == VariantChu ) width = 12, height = 12;
+ return boardWidth >= 0 && boardWidth != width || // -1 is default,
+ boardHeight >= 0 && boardHeight != height || // and thus by definition OK
+ holdingsSize >= 0 && holdingsSize != holdings;
+}
+
+char variantError[MSG_SIZ];
+
+char *
+SupportedVariant (char *list, VariantClass v, int boardWidth, int boardHeight, int holdingsSize, int proto, char *engine)
+{ // returns error message (recognizable by upper-case) if engine does not support the variant
+ char *p, *variant = VariantName(v);
+ static char b[MSG_SIZ];
+ if(NonStandardBoardSize(v, boardWidth, boardHeight, holdingsSize)) { /* [HGM] make prefix for non-standard board size. */
+ snprintf(b, MSG_SIZ, "%dx%d+%d_%s", boardWidth, boardHeight,
+ holdingsSize, variant); // cook up sized variant name
+ /* [HGM] varsize: try first if this deviant size variant is specifically known */
+ if(StrStr(list, b) == NULL) {
+ // specific sized variant not known, check if general sizing allowed
+ if(proto != 1 && StrStr(list, "boardsize") == NULL) {
+ snprintf(variantError, MSG_SIZ, "Board size %dx%d+%d not supported by %s",
+ boardWidth, boardHeight, holdingsSize, engine);
+ return NULL;
+ }
+ /* [HGM] here we really should compare with the maximum supported board size */
+ }
+ } else snprintf(b, MSG_SIZ,"%s", variant);
+ if(proto == 1) return b; // for protocol 1 we cannot check and hope for the best
+ p = StrStr(list, b);
+ while(p && (p != list && p[-1] != ',' || p[strlen(b)] && p[strlen(b)] != ',') ) p = StrStr(p+1, b);
+ if(p == NULL) {
+ // occurs not at all in list, or only as sub-string
+ snprintf(variantError, MSG_SIZ, _("Variant %s not supported by %s"), b, engine);
+ if(p = StrStr(list, b)) { // handle requesting parent variant when only size-overridden is supported
+ int l = strlen(variantError);
+ char *q;
+ while(p != list && p[-1] != ',') p--;
+ q = strchr(p, ',');
+ if(q) *q = NULLCHAR;
+ snprintf(variantError + l, MSG_SIZ - l, _(", but %s is"), p);
+ if(q) *q= ',';
+ }
+ return NULL;
+ }
+ return b;
+}
+
void
-InitChessProgram(cps, setup)
- ChessProgramState *cps;
- int setup; /* [HGM] needed to setup FRC opening position */
+InitChessProgram (ChessProgramState *cps, int setup)
+/* setup needed to setup FRC opening position */
{
- char buf[MSG_SIZ], b[MSG_SIZ]; int overruled;
+ char buf[MSG_SIZ], *b;
if (appData.noChessProgram) return;
hintRequested = FALSE;
bookRequested = FALSE;
+ ParseFeatures(appData.features[cps == &second], cps); // [HGM] allow user to overrule features
/* [HGM] some new WB protocol commands to configure engine are sent now, if engine supports them */
/* moved to before sending initstring in 4.3.15, so Polyglot can delay UCI 'isready' to recepton of 'new' */
if(cps->memSize) { /* [HGM] memory */
- sprintf(buf, "memory %d\n", appData.defaultHashSize + appData.defaultCacheSizeEGTB);
+ snprintf(buf, MSG_SIZ, "memory %d\n", appData.defaultHashSize + appData.defaultCacheSizeEGTB);
SendToProgram(buf, cps);
}
SendEgtPath(cps); /* [HGM] EGT */
if(cps->maxCores) { /* [HGM] SMP: (protocol specified must be last settings command before new!) */
- sprintf(buf, "cores %d\n", appData.smpCores);
+ snprintf(buf, MSG_SIZ, "cores %d\n", appData.smpCores);
SendToProgram(buf, cps);
}
+ setboardSpoiledMachineBlack = FALSE;
SendToProgram(cps->initString, cps);
if (gameInfo.variant != VariantNormal &&
gameInfo.variant != VariantLoadable
/* [HGM] also send variant if board size non-standard */
- || gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0
- ) {
- char *v = VariantName(gameInfo.variant);
- if (cps->protocolVersion != 1 && StrStr(cps->variants, v) == NULL) {
- /* [HGM] in protocol 1 we have to assume all variants valid */
- sprintf(buf, _("Variant %s not supported by %s"), v, cps->tidy);
- DisplayFatalError(buf, 0, 1);
+ || gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0) {
+
+ b = SupportedVariant(cps->variants, gameInfo.variant, gameInfo.boardWidth,
+ gameInfo.boardHeight, gameInfo.holdingsSize, cps->protocolVersion, cps->tidy);
+ if (b == NULL) {
+ VariantClass v;
+ char c, *q = cps->variants, *p = strchr(q, ',');
+ if(p) *p = NULLCHAR;
+ v = StringToVariant(q);
+ DisplayError(variantError, 0);
+ if(v != VariantUnknown && cps == &first) {
+ int w, h, s;
+ if(sscanf(q, "%dx%d+%d_%c", &w, &h, &s, &c) == 4) // get size overrides the engine needs with it (if any)
+ appData.NrFiles = w, appData.NrRanks = h, appData.holdingsSize = s, q = strchr(q, '_') + 1;
+ ASSIGN(appData.variant, q);
+ Reset(TRUE, FALSE);
+ }
+ if(p) *p = ',';
return;
}
- /* [HGM] make prefix for non-standard board size. Awkward testing... */
- overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
- if( gameInfo.variant == VariantXiangqi )
- overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 10 || gameInfo.holdingsSize != 0;
- if( gameInfo.variant == VariantShogi )
- overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 9 || gameInfo.holdingsSize != 7;
- if( gameInfo.variant == VariantBughouse || gameInfo.variant == VariantCrazyhouse )
- overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 5;
- if( gameInfo.variant == VariantCapablanca || gameInfo.variant == VariantCapaRandom ||
- gameInfo.variant == VariantGothic || gameInfo.variant == VariantFalcon )
- overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
- if( gameInfo.variant == VariantCourier )
- overruled = gameInfo.boardWidth != 12 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
- if( gameInfo.variant == VariantSuper )
- overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
- if( gameInfo.variant == VariantGreat )
- overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
-
- if(overruled) {
- sprintf(b, "%dx%d+%d_%s", gameInfo.boardWidth, gameInfo.boardHeight,
- gameInfo.holdingsSize, VariantName(gameInfo.variant)); // cook up sized variant name
- /* [HGM] varsize: try first if this defiant size variant is specifically known */
- if(StrStr(cps->variants, b) == NULL) {
- // specific sized variant not known, check if general sizing allowed
- if (cps->protocolVersion != 1) { // for protocol 1 we cannot check and hope for the best
- if(StrStr(cps->variants, "boardsize") == NULL) {
- sprintf(buf, "Board size %dx%d+%d not supported by %s",
- gameInfo.boardWidth, gameInfo.boardHeight, gameInfo.holdingsSize, cps->tidy);
- DisplayFatalError(buf, 0, 1);
- return;
- }
- /* [HGM] here we really should compare with the maximum supported board size */
- }
- }
- } else sprintf(b, "%s", VariantName(gameInfo.variant));
- sprintf(buf, "variant %s\n", b);
+ snprintf(buf, MSG_SIZ, "variant %s\n", b);
SendToProgram(buf, cps);
}
currentlyInitializedVariant = gameInfo.variant;
timeIncrement, appData.searchDepth,
searchTime);
}
- if (appData.showThinking
+ if (appData.showThinking
// [HGM] thinking: four options require thinking output to be sent
|| !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
) {
SendToProgram("easy\n", cps);
}
if (cps->usePing) {
- sprintf(buf, "ping %d\n", ++cps->lastPing);
+ snprintf(buf, MSG_SIZ, "ping %d\n", initPing = ++cps->lastPing);
SendToProgram(buf, cps);
}
cps->initDone = TRUE;
-}
+ ClearEngineOutputPane(cps == &second);
+}
void
-StartChessProgram(cps)
- ChessProgramState *cps;
+ResendOptions (ChessProgramState *cps)
+{ // send the stored value of the options
+ int i;
+ char buf[MSG_SIZ];
+ Option *opt = cps->option;
+ for(i=0; i<cps->nrOptions; i++, opt++) {
+ switch(opt->type) {
+ case Spin:
+ case Slider:
+ case CheckBox:
+ snprintf(buf, MSG_SIZ, "option %s=%d\n", opt->name, opt->value);
+ break;
+ case ComboBox:
+ snprintf(buf, MSG_SIZ, "option %s=%s\n", opt->name, opt->choice[opt->value]);
+ break;
+ default:
+ snprintf(buf, MSG_SIZ, "option %s=%s\n", opt->name, opt->textValue);
+ break;
+ case Button:
+ case SaveButton:
+ continue;
+ }
+ SendToProgram(buf, cps);
+ }
+}
+
+void
+StartChessProgram (ChessProgramState *cps)
{
char buf[MSG_SIZ];
int err;
}
err = StartChildProcess(buf, "", &cps->pr);
}
-
+
if (err != 0) {
- sprintf(buf, _("Startup failure on '%s'"), cps->program);
- DisplayFatalError(buf, err, 1);
- cps->pr = NoProc;
- cps->isr = NULL;
+ snprintf(buf, MSG_SIZ, _("Startup failure on '%s'"), cps->program);
+ DisplayError(buf, err); // [HGM] bit of a rough kludge: ignore failure, (which XBoard would do anyway), and let I/O discover it
+ if(cps != &first) return;
+ appData.noChessProgram = TRUE;
+ ThawUI();
+ SetNCPMode();
+// DisplayFatalError(buf, err, 1);
+// cps->pr = NoProc;
+// cps->isr = NULL;
return;
}
-
+
cps->isr = AddInputSource(cps->pr, TRUE, ReceiveFromProgram, cps);
if (cps->protocolVersion > 1) {
- sprintf(buf, "xboard\nprotover %d\n", cps->protocolVersion);
- cps->nrOptions = 0; // [HGM] options: clear all engine-specific options
- cps->comboCnt = 0; // and values of combo boxes
+ snprintf(buf, MSG_SIZ, "xboard\nprotover %d\n", cps->protocolVersion);
+ if(!cps->reload) { // do not clear options when reloading because of -xreuse
+ cps->nrOptions = 0; // [HGM] options: clear all engine-specific options
+ cps->comboCnt = 0; // and values of combo boxes
+ }
SendToProgram(buf, cps);
+ if(cps->reload) ResendOptions(cps);
} else {
SendToProgram("xboard\n", cps);
}
}
-
void
TwoMachinesEventIfReady P((void))
{
+ static int curMess = 0;
if (first.lastPing != first.lastPong) {
- DisplayMessage("", _("Waiting for first chess program"));
+ if(curMess != 1) DisplayMessage("", _("Waiting for first chess program")); curMess = 1;
ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
return;
}
if (second.lastPing != second.lastPong) {
- DisplayMessage("", _("Waiting for second chess program"));
+ if(curMess != 2) DisplayMessage("", _("Waiting for second chess program")); curMess = 2;
ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
return;
}
- ThawUI();
+ DisplayMessage("", ""); curMess = 0;
TwoMachinesEvent();
}
+char *
+MakeName (char *template)
+{
+ time_t clock;
+ struct tm *tm;
+ static char buf[MSG_SIZ];
+ char *p = buf;
+ int i;
+
+ clock = time((time_t *)NULL);
+ tm = localtime(&clock);
+
+ while(*p++ = *template++) if(p[-1] == '%') {
+ switch(*template++) {
+ case 0: *p = 0; return buf;
+ case 'Y': i = tm->tm_year+1900; break;
+ case 'y': i = tm->tm_year-100; break;
+ case 'M': i = tm->tm_mon+1; break;
+ case 'd': i = tm->tm_mday; break;
+ case 'h': i = tm->tm_hour; break;
+ case 'm': i = tm->tm_min; break;
+ case 's': i = tm->tm_sec; break;
+ default: i = 0;
+ }
+ snprintf(p-1, MSG_SIZ-10 - (p - buf), "%02d", i); p += strlen(p);
+ }
+ return buf;
+}
+
+int
+CountPlayers (char *p)
+{
+ int n = 0;
+ while(p = strchr(p, '\n')) p++, n++; // count participants
+ return n;
+}
+
+FILE *
+WriteTourneyFile (char *results, FILE *f)
+{ // write tournament parameters on tourneyFile; on success return the stream pointer for closing
+ if(f == NULL) f = fopen(appData.tourneyFile, "w");
+ if(f == NULL) DisplayError(_("Could not write on tourney file"), 0); else {
+ // create a file with tournament description
+ fprintf(f, "-participants {%s}\n", appData.participants);
+ fprintf(f, "-seedBase %d\n", appData.seedBase);
+ fprintf(f, "-tourneyType %d\n", appData.tourneyType);
+ fprintf(f, "-tourneyCycles %d\n", appData.tourneyCycles);
+ fprintf(f, "-defaultMatchGames %d\n", appData.defaultMatchGames);
+ fprintf(f, "-syncAfterRound %s\n", appData.roundSync ? "true" : "false");
+ fprintf(f, "-syncAfterCycle %s\n", appData.cycleSync ? "true" : "false");
+ fprintf(f, "-saveGameFile \"%s\"\n", appData.saveGameFile);
+ fprintf(f, "-loadGameFile \"%s\"\n", appData.loadGameFile);
+ fprintf(f, "-loadGameIndex %d\n", appData.loadGameIndex);
+ fprintf(f, "-loadPositionFile \"%s\"\n", appData.loadPositionFile);
+ fprintf(f, "-loadPositionIndex %d\n", appData.loadPositionIndex);
+ fprintf(f, "-rewindIndex %d\n", appData.rewindIndex);
+ fprintf(f, "-usePolyglotBook %s\n", appData.usePolyglotBook ? "true" : "false");
+ fprintf(f, "-polyglotBook \"%s\"\n", appData.polyglotBook);
+ fprintf(f, "-bookDepth %d\n", appData.bookDepth);
+ fprintf(f, "-bookVariation %d\n", appData.bookStrength);
+ fprintf(f, "-discourageOwnBooks %s\n", appData.defNoBook ? "true" : "false");
+ fprintf(f, "-defaultHashSize %d\n", appData.defaultHashSize);
+ fprintf(f, "-defaultCacheSizeEGTB %d\n", appData.defaultCacheSizeEGTB);
+ fprintf(f, "-ponderNextMove %s\n", appData.ponderNextMove ? "true" : "false");
+ fprintf(f, "-smpCores %d\n", appData.smpCores);
+ if(searchTime > 0)
+ fprintf(f, "-searchTime \"%d:%02d\"\n", searchTime/60, searchTime%60);
+ else {
+ fprintf(f, "-mps %d\n", appData.movesPerSession);
+ fprintf(f, "-tc %s\n", appData.timeControl);
+ fprintf(f, "-inc %.2f\n", appData.timeIncrement);
+ }
+ fprintf(f, "-results \"%s\"\n", results);
+ }
+ return f;
+}
+
+char *command[MAXENGINES], *mnemonic[MAXENGINES];
+
void
-NextMatchGame P((void))
+Substitute (char *participants, int expunge)
{
- int index; /* [HGM] autoinc: step lod index during match */
- Reset(FALSE, TRUE);
- if (*appData.loadGameFile != NULLCHAR) {
- index = appData.loadGameIndex;
- if(index < 0) { // [HGM] autoinc
- lastIndex = index = (index == -2 && first.twoMachinesColor[0] == 'b') ? lastIndex : lastIndex+1;
- if(appData.rewindIndex > 0 && index > appData.rewindIndex) lastIndex = index = 1;
- }
- LoadGameFromFile(appData.loadGameFile,
- index,
- appData.loadGameFile, FALSE);
- } else if (*appData.loadPositionFile != NULLCHAR) {
- index = appData.loadPositionIndex;
- if(index < 0) { // [HGM] autoinc
- lastIndex = index = (index == -2 && first.twoMachinesColor[0] == 'b') ? lastIndex : lastIndex+1;
- if(appData.rewindIndex > 0 && index > appData.rewindIndex) lastIndex = index = 1;
- }
- LoadPositionFromFile(appData.loadPositionFile,
- index,
- appData.loadPositionFile);
+ int i, changed, changes=0, nPlayers=0;
+ char *p, *q, *r, buf[MSG_SIZ];
+ if(participants == NULL) return;
+ if(appData.tourneyFile[0] == NULLCHAR) { free(participants); return; }
+ r = p = participants; q = appData.participants;
+ while(*p && *p == *q) {
+ if(*p == '\n') r = p+1, nPlayers++;
+ p++; q++;
+ }
+ if(*p) { // difference
+ while(*p && *p++ != '\n');
+ while(*q && *q++ != '\n');
+ changed = nPlayers;
+ changes = 1 + (strcmp(p, q) != 0);
+ }
+ if(changes == 1) { // a single engine mnemonic was changed
+ q = r; while(*q) nPlayers += (*q++ == '\n');
+ p = buf; while(*r && (*p = *r++) != '\n') p++;
+ *p = NULLCHAR;
+ NamesToList(firstChessProgramNames, command, mnemonic, "all");
+ for(i=1; mnemonic[i]; i++) if(!strcmp(buf, mnemonic[i])) break;
+ if(mnemonic[i]) { // The substitute is valid
+ FILE *f;
+ if(appData.tourneyFile[0] && (f = fopen(appData.tourneyFile, "r+")) ) {
+ flock(fileno(f), LOCK_EX);
+ ParseArgsFromFile(f);
+ fseek(f, 0, SEEK_SET);
+ FREE(appData.participants); appData.participants = participants;
+ if(expunge) { // erase results of replaced engine
+ int len = strlen(appData.results), w, b, dummy;
+ for(i=0; i<len; i++) {
+ Pairing(i, nPlayers, &w, &b, &dummy);
+ if((w == changed || b == changed) && appData.results[i] == '*') {
+ DisplayError(_("You cannot replace an engine while it is engaged!\nTerminate its game first."), 0);
+ fclose(f);
+ return;
+ }
+ }
+ for(i=0; i<len; i++) {
+ Pairing(i, nPlayers, &w, &b, &dummy);
+ if(w == changed || b == changed) appData.results[i] = ' '; // mark as not played
+ }
+ }
+ WriteTourneyFile(appData.results, f);
+ fclose(f); // release lock
+ return;
+ }
+ } else DisplayError(_("No engine with the name you gave is installed"), 0);
}
- TwoMachinesEventIfReady();
+ if(changes == 0) DisplayError(_("First change an engine by editing the participants list\nof the Tournament Options dialog"), 0);
+ if(changes > 1) DisplayError(_("You can only change one engine at the time"), 0);
+ free(participants);
+ return;
+}
+
+int
+CheckPlayers (char *participants)
+{
+ int i;
+ char buf[MSG_SIZ], *p;
+ NamesToList(firstChessProgramNames, command, mnemonic, "all");
+ while(p = strchr(participants, '\n')) {
+ *p = NULLCHAR;
+ for(i=1; mnemonic[i]; i++) if(!strcmp(participants, mnemonic[i])) break;
+ if(!mnemonic[i]) {
+ snprintf(buf, MSG_SIZ, _("No engine %s is installed"), participants);
+ *p = '\n';
+ DisplayError(buf, 0);
+ return 1;
+ }
+ *p = '\n';
+ participants = p + 1;
+ }
+ return 0;
+}
+
+int
+CreateTourney (char *name)
+{
+ FILE *f;
+ if(matchMode && strcmp(name, appData.tourneyFile)) {
+ ASSIGN(name, appData.tourneyFile); //do not allow change of tourneyfile while playing
+ }
+ if(name[0] == NULLCHAR) {
+ if(appData.participants[0])
+ DisplayError(_("You must supply a tournament file,\nfor storing the tourney progress"), 0);
+ return 0;
+ }
+ f = fopen(name, "r");
+ if(f) { // file exists
+ ASSIGN(appData.tourneyFile, name);
+ ParseArgsFromFile(f); // parse it
+ } else {
+ if(!appData.participants[0]) return 0; // ignore tourney file if non-existing & no participants
+ if(CountPlayers(appData.participants) < (appData.tourneyType>0 ? appData.tourneyType+1 : 2)) {
+ DisplayError(_("Not enough participants"), 0);
+ return 0;
+ }
+ if(CheckPlayers(appData.participants)) return 0;
+ ASSIGN(appData.tourneyFile, name);
+ if(appData.tourneyType < 0) appData.defaultMatchGames = 1; // Swiss forces games/pairing = 1
+ if((f = WriteTourneyFile("", NULL)) == NULL) return 0;
+ }
+ fclose(f);
+ appData.noChessProgram = FALSE;
+ appData.clockMode = TRUE;
+ SetGNUMode();
+ return 1;
}
-void UserAdjudicationEvent( int result )
+int
+NamesToList (char *names, char **engineList, char **engineMnemonic, char *group)
+{
+ char buf[MSG_SIZ], *p, *q;
+ int i=1, header, skip, all = !strcmp(group, "all"), depth = 0;
+ insert = names; // afterwards, this global will point just after last retrieved engine line or group end in the 'names'
+ skip = !all && group[0]; // if group requested, we start in skip mode
+ for(;*names && depth >= 0 && i < MAXENGINES-1; names = p) {
+ p = names; q = buf; header = 0;
+ while(*p && *p != '\n') *q++ = *p++;
+ *q = 0;
+ if(*p == '\n') p++;
+ if(buf[0] == '#') {
+ if(strstr(buf, "# end") == buf) { if(!--depth) insert = p; continue; } // leave group, and suppress printing label
+ depth++; // we must be entering a new group
+ if(all) continue; // suppress printing group headers when complete list requested
+ header = 1;
+ if(skip && !strcmp(group, buf)) { depth = 0; skip = FALSE; } // start when we reach requested group
+ }
+ if(depth != header && !all || skip) continue; // skip contents of group (but print first-level header)
+ if(engineList[i]) free(engineList[i]);
+ engineList[i] = strdup(buf);
+ if(buf[0] != '#') insert = p, TidyProgramName(engineList[i], "localhost", buf); // group headers not tidied
+ if(engineMnemonic[i]) free(engineMnemonic[i]);
+ if((q = strstr(engineList[i]+2, "variant")) && q[-2]== ' ' && (q[-1]=='/' || q[-1]=='-') && (q[7]==' ' || q[7]=='=')) {
+ strcat(buf, " (");
+ sscanf(q + 8, "%s", buf + strlen(buf));
+ strcat(buf, ")");
+ }
+ engineMnemonic[i] = strdup(buf);
+ i++;
+ }
+ engineList[i] = engineMnemonic[i] = NULL;
+ return i;
+}
+
+// following implemented as macro to avoid type limitations
+#define SWAP(item, temp) temp = appData.item[0]; appData.item[0] = appData.item[n]; appData.item[n] = temp;
+
+void
+SwapEngines (int n)
+{ // swap settings for first engine and other engine (so far only some selected options)
+ int h;
+ char *p;
+ if(n == 0) return;
+ SWAP(directory, p)
+ SWAP(chessProgram, p)
+ SWAP(isUCI, h)
+ SWAP(hasOwnBookUCI, h)
+ SWAP(protocolVersion, h)
+ SWAP(reuse, h)
+ SWAP(scoreIsAbsolute, h)
+ SWAP(timeOdds, h)
+ SWAP(logo, p)
+ SWAP(pgnName, p)
+ SWAP(pvSAN, h)
+ SWAP(engOptions, p)
+ SWAP(engInitString, p)
+ SWAP(computerString, p)
+ SWAP(features, p)
+ SWAP(fenOverride, p)
+ SWAP(NPS, h)
+ SWAP(accumulateTC, h)
+ SWAP(drawDepth, h)
+ SWAP(host, p)
+ SWAP(pseudo, h)
+}
+
+int
+GetEngineLine (char *s, int n)
+{
+ int i;
+ char buf[MSG_SIZ];
+ extern char *icsNames;
+ if(!s || !*s) return 0;
+ NamesToList(n >= 10 ? icsNames : firstChessProgramNames, command, mnemonic, "all");
+ for(i=1; mnemonic[i]; i++) if(!strcmp(s, mnemonic[i])) break;
+ if(!mnemonic[i]) return 0;
+ if(n == 11) return 1; // just testing if there was a match
+ snprintf(buf, MSG_SIZ, "-%s %s", n == 10 ? "icshost" : "fcp", command[i]);
+ if(n == 1) SwapEngines(n);
+ ParseArgsFromString(buf);
+ if(n == 1) SwapEngines(n);
+ if(n == 0 && *appData.secondChessProgram == NULLCHAR) {
+ SwapEngines(1); // set second same as first if not yet set (to suppress WB startup dialog)
+ ParseArgsFromString(buf);
+ }
+ return 1;
+}
+
+int
+SetPlayer (int player, char *p)
+{ // [HGM] find the engine line of the partcipant given by number, and parse its options.
+ int i;
+ char buf[MSG_SIZ], *engineName;
+ for(i=0; i<player; i++) p = strchr(p, '\n') + 1;
+ engineName = strdup(p); if(p = strchr(engineName, '\n')) *p = NULLCHAR;
+ for(i=1; command[i]; i++) if(!strcmp(mnemonic[i], engineName)) break;
+ if(mnemonic[i]) {
+ snprintf(buf, MSG_SIZ, "-fcp %s", command[i]);
+ ParseArgsFromString(resetOptions); appData.fenOverride[0] = NULL; appData.pvSAN[0] = FALSE;
+ appData.firstHasOwnBookUCI = !appData.defNoBook; appData.protocolVersion[0] = PROTOVER;
+ ParseArgsFromString(buf);
+ } else { // no engine with this nickname is installed!
+ snprintf(buf, MSG_SIZ, _("No engine %s is installed"), engineName);
+ ReserveGame(nextGame, ' '); // unreserve game and drop out of match mode with error
+ matchMode = FALSE; appData.matchGames = matchGame = roundNr = 0;
+ ModeHighlight();
+ DisplayError(buf, 0);
+ return 0;
+ }
+ free(engineName);
+ return i;
+}
+
+char *recentEngines;
+
+void
+RecentEngineEvent (int nr)
+{
+ int n;
+// SwapEngines(1); // bump first to second
+// ReplaceEngine(&second, 1); // and load it there
+ NamesToList(firstChessProgramNames, command, mnemonic, "all"); // get mnemonics of installed engines
+ n = SetPlayer(nr, recentEngines); // select new (using original menu order!)
+ if(mnemonic[n]) { // if somehow the engine with the selected nickname is no longer found in the list, we skip
+ ReplaceEngine(&first, 0);
+ FloatToFront(&appData.recentEngineList, command[n]);
+ }
+}
+
+int
+Pairing (int nr, int nPlayers, int *whitePlayer, int *blackPlayer, int *syncInterval)
+{ // determine players from game number
+ int curCycle, curRound, curPairing, gamesPerCycle, gamesPerRound, roundsPerCycle=1, pairingsPerRound=1;
+
+ if(appData.tourneyType == 0) {
+ roundsPerCycle = (nPlayers - 1) | 1;
+ pairingsPerRound = nPlayers / 2;
+ } else if(appData.tourneyType > 0) {
+ roundsPerCycle = nPlayers - appData.tourneyType;
+ pairingsPerRound = appData.tourneyType;
+ }
+ gamesPerRound = pairingsPerRound * appData.defaultMatchGames;
+ gamesPerCycle = gamesPerRound * roundsPerCycle;
+ appData.matchGames = gamesPerCycle * appData.tourneyCycles - 1; // fake like all games are one big match
+ curCycle = nr / gamesPerCycle; nr %= gamesPerCycle;
+ curRound = nr / gamesPerRound; nr %= gamesPerRound;
+ curPairing = nr / appData.defaultMatchGames; nr %= appData.defaultMatchGames;
+ matchGame = nr + curCycle * appData.defaultMatchGames + 1; // fake game nr that loads correct game or position from file
+ roundNr = (curCycle * roundsPerCycle + curRound) * appData.defaultMatchGames + nr + 1;
+
+ if(appData.cycleSync) *syncInterval = gamesPerCycle;
+ if(appData.roundSync) *syncInterval = gamesPerRound;
+
+ if(appData.debugMode) fprintf(debugFP, "cycle=%d, round=%d, pairing=%d curGame=%d\n", curCycle, curRound, curPairing, matchGame);
+
+ if(appData.tourneyType == 0) {
+ if(curPairing == (nPlayers-1)/2 ) {
+ *whitePlayer = curRound;
+ *blackPlayer = nPlayers - 1; // this is the 'bye' when nPlayer is odd
+ } else {
+ *whitePlayer = curRound - (nPlayers-1)/2 + curPairing;
+ if(*whitePlayer < 0) *whitePlayer += nPlayers-1+(nPlayers&1);
+ *blackPlayer = curRound + (nPlayers-1)/2 - curPairing;
+ if(*blackPlayer >= nPlayers-1+(nPlayers&1)) *blackPlayer -= nPlayers-1+(nPlayers&1);
+ }
+ } else if(appData.tourneyType > 1) {
+ *blackPlayer = curPairing; // in multi-gauntlet, assign gauntlet engines to second, so first an be kept loaded during round
+ *whitePlayer = curRound + appData.tourneyType;
+ } else if(appData.tourneyType > 0) {
+ *whitePlayer = curPairing;
+ *blackPlayer = curRound + appData.tourneyType;
+ }
+
+ // take care of white/black alternation per round.
+ // For cycles and games this is already taken care of by default, derived from matchGame!
+ return curRound & 1;
+}
+
+int
+NextTourneyGame (int nr, int *swapColors)
+{ // !!!major kludge!!! fiddle appData settings to get everything in order for next tourney game
+ char *p, *q;
+ int whitePlayer, blackPlayer, firstBusy=1000000000, syncInterval = 0, nPlayers, OK = 1;
+ FILE *tf;
+ if(appData.tourneyFile[0] == NULLCHAR) return 1; // no tourney, always allow next game
+ tf = fopen(appData.tourneyFile, "r");
+ if(tf == NULL) { DisplayFatalError(_("Bad tournament file"), 0, 1); return 0; }
+ ParseArgsFromFile(tf); fclose(tf);
+ InitTimeControls(); // TC might be altered from tourney file
+
+ nPlayers = CountPlayers(appData.participants); // count participants
+ if(appData.tourneyType < 0) syncInterval = nPlayers/2; else
+ *swapColors = Pairing(nr<0 ? 0 : nr, nPlayers, &whitePlayer, &blackPlayer, &syncInterval);
+
+ if(syncInterval) {
+ p = q = appData.results;
+ while(*q) if(*q++ == '*' || q[-1] == ' ') { firstBusy = q - p - 1; break; }
+ if(firstBusy/syncInterval < (nextGame/syncInterval)) {
+ DisplayMessage(_("Waiting for other game(s)"),"");
+ waitingForGame = TRUE;
+ ScheduleDelayedEvent(NextMatchGame, 1000); // wait for all games of previous round to finish
+ return 0;
+ }
+ waitingForGame = FALSE;
+ }
+
+ if(appData.tourneyType < 0) {
+ if(nr>=0 && !pairingReceived) {
+ char buf[1<<16];
+ if(pairing.pr == NoProc) {
+ if(!appData.pairingEngine[0]) {
+ DisplayFatalError(_("No pairing engine specified"), 0, 1);
+ return 0;
+ }
+ StartChessProgram(&pairing); // starts the pairing engine
+ }
+ snprintf(buf, 1<<16, "results %d %s\n", nPlayers, appData.results);
+ SendToProgram(buf, &pairing);
+ snprintf(buf, 1<<16, "pairing %d\n", nr+1);
+ SendToProgram(buf, &pairing);
+ return 0; // wait for pairing engine to answer (which causes NextTourneyGame to be called again...
+ }
+ pairingReceived = 0; // ... so we continue here
+ *swapColors = 0;
+ appData.matchGames = appData.tourneyCycles * syncInterval - 1;
+ whitePlayer = savedWhitePlayer-1; blackPlayer = savedBlackPlayer-1;
+ matchGame = 1; roundNr = nr / syncInterval + 1;
+ }
+
+ if(first.pr != NoProc && second.pr != NoProc || nr<0) return 1; // engines already loaded
+
+ // redefine engines, engine dir, etc.
+ NamesToList(firstChessProgramNames, command, mnemonic, "all"); // get mnemonics of installed engines
+ if(first.pr == NoProc) {
+ if(!SetPlayer(whitePlayer, appData.participants)) OK = 0; // find white player amongst it, and parse its engine line
+ InitEngine(&first, 0); // initialize ChessProgramStates based on new settings.
+ }
+ if(second.pr == NoProc) {
+ SwapEngines(1);
+ if(!SetPlayer(blackPlayer, appData.participants)) OK = 0; // find black player amongst it, and parse its engine line
+ SwapEngines(1); // and make that valid for second engine by swapping
+ InitEngine(&second, 1);
+ }
+ CommonEngineInit(); // after this TwoMachinesEvent will create correct engine processes
+ UpdateLogos(FALSE); // leave display to ModeHiglight()
+ return OK;
+}
+
+void
+NextMatchGame ()
+{ // performs game initialization that does not invoke engines, and then tries to start the game
+ int res, firstWhite, swapColors = 0;
+ if(!NextTourneyGame(nextGame, &swapColors)) return; // this sets matchGame, -fcp / -scp and other options for next game, if needed
+ if(matchMode && appData.debugMode) { // [HGM] debug split: game is part of a match; we might have to create a debug file just for this game
+ char buf[MSG_SIZ];
+ snprintf(buf, MSG_SIZ, appData.nameOfDebugFile, nextGame+1); // expand name of debug file with %d in it
+ if(strcmp(buf, currentDebugFile)) { // name has changed
+ FILE *f = fopen(buf, "w");
+ if(f) { // if opening the new file failed, just keep using the old one
+ ASSIGN(currentDebugFile, buf);
+ fclose(debugFP);
+ debugFP = f;
+ }
+ if(appData.serverFileName) {
+ if(serverFP) fclose(serverFP);
+ serverFP = fopen(appData.serverFileName, "w");
+ if(serverFP && first.pr != NoProc) fprintf(serverFP, "StartChildProcess (dir=\".\") .\\%s\n", first.tidy);
+ if(serverFP && second.pr != NoProc) fprintf(serverFP, "StartChildProcess (dir=\".\") .\\%s\n", second.tidy);
+ }
+ }
+ }
+ firstWhite = appData.firstPlaysBlack ^ (matchGame & 1 | appData.sameColorGames > 1); // non-incremental default
+ firstWhite ^= swapColors; // reverses if NextTourneyGame says we are in an odd round
+ first.twoMachinesColor = firstWhite ? "white\n" : "black\n"; // perform actual color assignement
+ second.twoMachinesColor = firstWhite ? "black\n" : "white\n";
+ appData.noChessProgram = (first.pr == NoProc); // kludge to prevent Reset from starting up chess program
+ if(appData.loadGameIndex == -2) srandom(appData.seedBase + 68163*(nextGame & ~1)); // deterministic seed to force same opening
+ Reset(FALSE, first.pr != NoProc);
+ res = LoadGameOrPosition(matchGame); // setup game
+ appData.noChessProgram = FALSE; // LoadGameOrPosition might call Reset too!
+ if(!res) return; // abort when bad game/pos file
+ TwoMachinesEvent();
+}
+
+void
+UserAdjudicationEvent (int result)
{
ChessMove gameResult = GameIsDrawn;
// [HGM] save: calculate checksum of game to make games easily identifiable
-int StringCheckSum(char *s)
+int
+StringCheckSum (char *s)
{
int i = 0;
if(s==NULL) return 0;
return i;
}
-int GameCheckSum()
+int
+GameCheckSum ()
{
int i, sum=0;
for(i=backwardMostMove; i<forwardMostMove; i++) {
} // end of save patch
void
-GameEnds(result, resultDetails, whosays)
- ChessMove result;
- char *resultDetails;
- int whosays;
+GameEnds (ChessMove result, char *resultDetails, int whosays)
{
GameMode nextGameMode;
int isIcsGame;
- char buf[MSG_SIZ];
+ char buf[MSG_SIZ], popupRequested = 0, *ranking = NULL;
if(endingGame) return; /* [HGM] crash: forbid recursion */
endingGame = 1;
-
+ if(twoBoards) { // [HGM] dual: switch back to one board
+ twoBoards = partnerUp = 0; InitDrawingSizes(-2, 0);
+ DrawPosition(TRUE, partnerBoard); // observed game becomes foreground
+ }
if (appData.debugMode) {
fprintf(debugFP, "GameEnds(%d, %s, %d)\n",
result, resultDetails ? resultDetails : "(null)", whosays);
}
+ fromX = fromY = killX = killY = -1; // [HGM] abort any move the user is entering. // [HGM] lion
+
+ if(pausing) PauseEvent(); // can happen when we abort a paused game (New Game or Quit)
+
if (appData.icsActive && (whosays == GE_ENGINE || whosays >= GE_ENGINE1)) {
/* If we are playing on ICS, the server decides when the
- game is over, but the engine can offer to draw, claim
- a draw, or resign.
+ game is over, but the engine can offer to draw, claim
+ a draw, or resign.
*/
#if ZIPPY
if (appData.zippyPlay && first.initDone) {
/* If this is an ICS game, only ICS can really say it's done;
if not, anyone can. */
- isIcsGame = (gameMode == IcsPlayingWhite ||
- gameMode == IcsPlayingBlack ||
- gameMode == IcsObserving ||
+ isIcsGame = (gameMode == IcsPlayingWhite ||
+ gameMode == IcsPlayingBlack ||
+ gameMode == IcsObserving ||
gameMode == IcsExamining);
if (!isIcsGame || whosays == GE_ICS) {
/* OK -- not an ICS game, or ICS said it was done */
StopClocks();
- if (!isIcsGame && !appData.noChessProgram)
+ if (!isIcsGame && !appData.noChessProgram)
SetUserThinkingEnables();
-
+
/* [HGM] if a machine claims the game end we verify this claim */
if(gameMode == TwoMachinesPlay && appData.testClaims) {
if(appData.testLegality && whosays >= GE_ENGINE1 ) {
second.twoMachinesColor[0] ;
// [HGM] losers: because the logic is becoming a bit hairy, determine true result first
- if(epStatus[forwardMostMove] == EP_CHECKMATE) {
+ if((signed char)boards[forwardMostMove][EP_STATUS] == EP_CHECKMATE) {
/* [HGM] verify: engine mate claims accepted if they were flagged */
trueResult = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins;
} else
- if(epStatus[forwardMostMove] == EP_WINS) { // added code for games where being mated is a win
+ if((signed char)boards[forwardMostMove][EP_STATUS] == EP_WINS) { // added code for games where being mated is a win
/* [HGM] verify: engine mate claims accepted if they were flagged */
trueResult = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
} else
- if(epStatus[forwardMostMove] == EP_STALEMATE) { // only used to indicate draws now
+ if((signed char)boards[forwardMostMove][EP_STATUS] == EP_STALEMATE) { // only used to indicate draws now
trueResult = GameIsDrawn; // default; in variants where stalemate loses, Status is CHECKMATE
}
// now verify win claims, but not in drop games, as we don't understand those yet
if( (gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper
- || gameInfo.variant == VariantGreat) &&
+ || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) &&
(result == WhiteWins && claimer == 'w' ||
result == BlackWins && claimer == 'b' ) ) { // case to verify: engine claims own win
if (appData.debugMode) {
fprintf(debugFP, "result=%d sp=%d move=%d\n",
- result, epStatus[forwardMostMove], forwardMostMove);
+ result, (signed char)boards[forwardMostMove][EP_STATUS], forwardMostMove);
}
if(result != trueResult) {
- sprintf(buf, "False win claim: '%s'", resultDetails);
+ snprintf(buf, MSG_SIZ, "False win claim: '%s'", resultDetails);
result = claimer == 'w' ? BlackWins : WhiteWins;
resultDetails = buf;
}
} else
- if( result == GameIsDrawn && epStatus[forwardMostMove] > EP_DRAWS
+ if( result == GameIsDrawn && (signed char)boards[forwardMostMove][EP_STATUS] > EP_DRAWS
&& (forwardMostMove <= backwardMostMove ||
- epStatus[forwardMostMove-1] > EP_DRAWS ||
+ (signed char)boards[forwardMostMove-1][EP_STATUS] > EP_DRAWS ||
(claimer=='b')==(forwardMostMove&1))
) {
/* [HGM] verify: draws that were not flagged are false claims */
- sprintf(buf, "False draw claim: '%s'", resultDetails);
+ snprintf(buf, MSG_SIZ, "False draw claim: '%s'", resultDetails);
result = claimer == 'w' ? BlackWins : WhiteWins;
resultDetails = buf;
}
/* (Claiming a loss is accepted no questions asked!) */
+ } else if(matchMode && result == GameIsDrawn && !strcmp(resultDetails, "Engine Abort Request")) {
+ forwardMostMove = backwardMostMove; // [HGM] delete game to surpress saving
+ result = GameUnfinished;
+ if(!*appData.tourneyFile) matchGame--; // replay even in plain match
}
/* [HGM] bare: don't allow bare King to win */
- if((gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat)
- && gameInfo.variant != VariantLosers && gameInfo.variant != VariantGiveaway
+ if((gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper
+ || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand)
+ && gameInfo.variant != VariantLosers && gameInfo.variant != VariantGiveaway
&& gameInfo.variant != VariantSuicide // [HGM] losers: except in losers, of course...
&& result != GameIsDrawn)
- { int i, j, k=0, color = (result==WhiteWins ? (int)WhitePawn : (int)BlackPawn);
+ { int i, j, k=0, oppoKings = 0, color = (result==WhiteWins ? (int)WhitePawn : (int)BlackPawn);
for(j=BOARD_LEFT; j<BOARD_RGHT; j++) for(i=0; i<BOARD_HEIGHT; i++) {
- int p = (int)boards[forwardMostMove][i][j] - color;
+ int p = (signed char)boards[forwardMostMove][i][j] - color;
if(p >= 0 && p <= (int)WhiteKing) k++;
+ oppoKings += (p + color == WhiteKing + BlackPawn - color);
}
if (appData.debugMode) {
fprintf(debugFP, "GE(%d, %s, %d) bare king k=%d color=%d\n",
result, resultDetails ? resultDetails : "(null)", whosays, k, color);
}
- if(k <= 1) {
+ if(k <= 1 && oppoKings > 0) { // the latter needed in Atomic, where bare K wins if opponent King already destroyed
result = GameIsDrawn;
- sprintf(buf, "%s but bare king", resultDetails);
+ snprintf(buf, MSG_SIZ, "%s but bare king", resultDetails);
resultDetails = buf;
}
}
if(result==WhiteWins) c = '+';
if(result==BlackWins) c = '-';
if(resultDetails != NULL)
- fprintf(serverMoves, ";%c;%s\n", c, resultDetails);
+ fprintf(serverMoves, ";%c;%s\n", c, resultDetails), fflush(serverMoves);
}
if (resultDetails != NULL) {
gameInfo.result = result;
/* display last move only if game was not loaded from file */
if ((whosays != GE_FILE) && (currentMove == forwardMostMove))
DisplayMove(currentMove - 1);
-
+
if (forwardMostMove != 0) {
if (gameMode != PlayFromGameFile && gameMode != EditGame
&& lastSavedGame != GameCheckSum() // [HGM] save: suppress duplicates
) {
if (*appData.saveGameFile != NULLCHAR) {
+ if(result == GameUnfinished && matchMode && *appData.tourneyFile)
+ AutoSaveGame(); // [HGM] protect tourney PGN from aborted games, and prompt for name instead
+ else
SaveGameToFile(appData.saveGameFile, TRUE);
} else if (appData.autoSaveGames) {
- AutoSaveGame();
+ if(gameMode != IcsObserving || !appData.onlyOwn) AutoSaveGame();
}
if (*appData.savePositionFile != NULLCHAR) {
SavePositionToFile(appData.savePositionFile);
}
+ AddGameToBook(FALSE); // Only does something during Monte-Carlo book building
}
}
gameMode == IcsPlayingBlack ||
gameMode == BeginningOfGame) {
char buf[MSG_SIZ];
- sprintf(buf, "result %s {%s}\n", PGNResult(result),
+ snprintf(buf, MSG_SIZ, "result %s {%s}\n", PGNResult(result),
resultDetails);
if (first.pr != NoProc) {
SendToProgram(buf, &first);
PlayIcsUnfinishedSound();
}
}
+ if(appData.quitNext) { ExitEvent(0); return; }
} else if (gameMode == EditGame ||
- gameMode == PlayFromGameFile ||
- gameMode == AnalyzeMode ||
+ gameMode == PlayFromGameFile ||
+ gameMode == AnalyzeMode ||
gameMode == AnalyzeFile) {
nextGameMode = gameMode;
} else {
SendToProgram("force\n", &first);
if (first.usePing) {
char buf[MSG_SIZ];
- sprintf(buf, "ping %d\n", ++first.lastPing);
+ snprintf(buf, MSG_SIZ, "ping %d\n", ++first.lastPing);
SendToProgram(buf, &first);
}
}
if (first.isr != NULL)
RemoveInputSource(first.isr);
first.isr = NULL;
-
+
if (first.pr != NoProc) {
ExitAnalyzeMode();
DoSleep( appData.delayBeforeQuit );
SendToProgram("quit\n", &first);
- DoSleep( appData.delayAfterQuit );
- DestroyChildProcess(first.pr, first.useSigterm);
+ DestroyChildProcess(first.pr, 4 + first.useSigterm);
+ first.reload = TRUE;
}
first.pr = NoProc;
}
SendToProgram("force\n", &second);
if (second.usePing) {
char buf[MSG_SIZ];
- sprintf(buf, "ping %d\n", ++second.lastPing);
+ snprintf(buf, MSG_SIZ, "ping %d\n", ++second.lastPing);
SendToProgram(buf, &second);
}
}
if (second.isr != NULL)
RemoveInputSource(second.isr);
second.isr = NULL;
-
+
if (second.pr != NoProc) {
DoSleep( appData.delayBeforeQuit );
SendToProgram("quit\n", &second);
- DoSleep( appData.delayAfterQuit );
- DestroyChildProcess(second.pr, second.useSigterm);
+ DestroyChildProcess(second.pr, 4 + second.useSigterm);
+ second.reload = TRUE;
}
second.pr = NoProc;
}
- if (matchMode && gameMode == TwoMachinesPlay) {
+ if (matchMode && (gameMode == TwoMachinesPlay || (waitingForGame || startingEngine) && exiting)) {
+ char resChar = '=';
switch (result) {
case WhiteWins:
+ resChar = '+';
if (first.twoMachinesColor[0] == 'w') {
first.matchWins++;
} else {
}
break;
case BlackWins:
+ resChar = '-';
if (first.twoMachinesColor[0] == 'b') {
first.matchWins++;
} else {
second.matchWins++;
}
break;
+ case GameUnfinished:
+ resChar = ' ';
default:
break;
}
- if (matchGame < appData.matchGames) {
- char *tmp;
- if(appData.sameColorGames <= 1) { /* [HGM] alternate: suppress color swap */
- tmp = first.twoMachinesColor;
- first.twoMachinesColor = second.twoMachinesColor;
- second.twoMachinesColor = tmp;
- }
+
+ if(exiting) resChar = ' '; // quit while waiting for round sync: unreserve already reserved game
+ if(appData.tourneyFile[0]){ // [HGM] we are in a tourney; update tourney file with game result
+ if(appData.afterGame && appData.afterGame[0]) RunCommand(appData.afterGame);
+ ReserveGame(nextGame, resChar); // sets nextGame
+ if(nextGame > appData.matchGames) appData.tourneyFile[0] = 0, ranking = TourneyStandings(3); // tourney is done
+ else ranking = strdup("busy"); //suppress popup when aborted but not finished
+ } else roundNr = nextGame = matchGame + 1; // normal match, just increment; round equals matchGame
+
+ if (nextGame <= appData.matchGames && !abortMatch) {
gameMode = nextGameMode;
- matchGame++;
- if(appData.matchPause>10000 || appData.matchPause<10)
- appData.matchPause = 10000; /* [HGM] make pause adjustable */
- ScheduleDelayedEvent(NextMatchGame, appData.matchPause);
+ matchGame = nextGame; // this will be overruled in tourney mode!
+ GetTimeMark(&pauseStart); // [HGM] matchpause: stipulate a pause
+ ScheduleDelayedEvent(NextMatchGame, 10); // but start game immediately (as it will wait out the pause itself)
endingGame = 0; /* [HGM] crash */
return;
} else {
- char buf[MSG_SIZ];
gameMode = nextGameMode;
- sprintf(buf, _("Match %s vs. %s: final score %d-%d-%d"),
- first.tidy, second.tidy,
- first.matchWins, second.matchWins,
- appData.matchGames - (first.matchWins + second.matchWins));
- DisplayFatalError(buf, 0, 0);
+ snprintf(buf, MSG_SIZ, _("Match %s vs. %s: final score %d-%d-%d"),
+ first.tidy, second.tidy,
+ first.matchWins, second.matchWins,
+ appData.matchGames - (first.matchWins + second.matchWins));
+ if(!appData.tourneyFile[0]) matchGame++, DisplayTwoMachinesTitle(); // [HGM] update result in window title
+ if(ranking && strcmp(ranking, "busy") && appData.afterTourney && appData.afterTourney[0]) RunCommand(appData.afterTourney);
+ popupRequested++; // [HGM] crash: postpone to after resetting endingGame
+ if (appData.firstPlaysBlack) { // [HGM] match: back to original for next match
+ first.twoMachinesColor = "black\n";
+ second.twoMachinesColor = "white\n";
+ } else {
+ first.twoMachinesColor = "white\n";
+ second.twoMachinesColor = "black\n";
+ }
}
}
if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile) &&
gameMode = nextGameMode;
ModeHighlight();
endingGame = 0; /* [HGM] crash */
+ if(popupRequested) { // [HGM] crash: this calls GameEnds recursively through ExitEvent! Make it a harmless tail recursion.
+ if(matchMode == TRUE) { // match through command line: exit with or without popup
+ if(ranking) {
+ ToNrEvent(forwardMostMove);
+ if(strcmp(ranking, "busy")) DisplayFatalError(ranking, 0, 0);
+ else ExitEvent(0);
+ } else DisplayFatalError(buf, 0, 0);
+ } else { // match through menu; just stop, with or without popup
+ matchMode = FALSE; appData.matchGames = matchGame = roundNr = 0;
+ ModeHighlight();
+ if(ranking){
+ if(strcmp(ranking, "busy")) DisplayNote(ranking);
+ } else DisplayNote(buf);
+ }
+ if(ranking) free(ranking);
+ }
}
/* Assumes program was just initialized (initString sent).
Leaves program in force mode. */
void
-FeedMovesToProgram(cps, upto)
- ChessProgramState *cps;
- int upto;
+FeedMovesToProgram (ChessProgramState *cps, int upto)
{
int i;
-
+
if (appData.debugMode)
fprintf(debugFP, "Feeding %smoves %d through %d to %s chess program\n",
startedFromSetupPosition ? "position and " : "",
backwardMostMove, upto, cps->which);
- if(currentlyInitializedVariant != gameInfo.variant) { char buf[MSG_SIZ];
+ if(currentlyInitializedVariant != gameInfo.variant) {
+ char buf[MSG_SIZ];
// [HGM] variantswitch: make engine aware of new variant
- if(cps->protocolVersion > 1 && StrStr(cps->variants, VariantName(gameInfo.variant)) == NULL)
+ if(!SupportedVariant(cps->variants, gameInfo.variant, gameInfo.boardWidth,
+ gameInfo.boardHeight, gameInfo.holdingsSize, cps->protocolVersion, ""))
return; // [HGM] refrain from feeding moves altogether if variant is unsupported!
- sprintf(buf, "variant %s\n", VariantName(gameInfo.variant));
+ snprintf(buf, MSG_SIZ, "variant %s\n", VariantName(gameInfo.variant));
SendToProgram(buf, cps);
currentlyInitializedVariant = gameInfo.variant;
}
}
-void
-ResurrectChessProgram()
+int
+ResurrectChessProgram ()
{
/* The chess program may have exited.
If so, restart it and feed it all the moves made so far. */
+ static int doInit = 0;
- if (appData.noChessProgram || first.pr != NoProc) return;
-
- StartChessProgram(&first);
+ if (appData.noChessProgram) return 1;
+
+ if(matchMode /*&& appData.tourneyFile[0]*/) { // [HGM] tourney: make sure we get features after engine replacement. (Should we always do this?)
+ if(WaitForEngine(&first, TwoMachinesEventIfReady)) { doInit = 1; return 0; } // request to do init on next visit, because we started engine
+ if(!doInit) return 1; // this replaces testing first.pr != NoProc, which is true when we get here, but first time no reason to abort
+ doInit = 0; // we fell through (first time after starting the engine); make sure it doesn't happen again
+ } else {
+ if (first.pr != NoProc) return 1;
+ StartChessProgram(&first);
+ }
InitChessProgram(&first, FALSE);
FeedMovesToProgram(&first, currentMove);
SendToProgram("analyze\n", &first);
first.analyzing = TRUE;
}
+ return 1;
}
/*
* Button procedures
*/
void
-Reset(redraw, init)
- int redraw, init;
+Reset (int redraw, int init)
{
int i;
fprintf(debugFP, "Reset(%d, %d) from gameMode %d\n",
redraw, init, gameMode);
}
+ pieceDefs = FALSE; // [HGM] gen: reset engine-defined piece moves
+ for(i=0; i<EmptySquare; i++) { FREE(pieceDesc[i]); pieceDesc[i] = NULL; }
+ CleanupTail(); // [HGM] vari: delete any stored variations
+ CommentPopDown(); // [HGM] make sure no comments to the previous game keep hanging on
pausing = pauseExamInvalid = FALSE;
startedFromSetupPosition = blackPlaysFirst = FALSE;
firstMove = TRUE;
lastHint[0] = NULLCHAR;
ClearGameInfo(&gameInfo);
gameInfo.variant = StringToVariant(appData.variant);
+ if(gameInfo.variant == VariantNormal && strcmp(appData.variant, "normal")) gameInfo.variant = VariantUnknown;
ics_user_moved = ics_clock_paused = FALSE;
ics_getting_history = H_FALSE;
ics_gamenum = -1;
white_holding[0] = black_holding[0] = NULLCHAR;
ClearProgramStats();
opponentKibitzes = FALSE; // [HGM] kibitz: do not reserve space in engine-output window in zippy mode
-
+
ResetFrontEnd();
ClearHighlights();
flipView = appData.flipView;
ClearPremoveHighlights();
gotPremove = FALSE;
alarmSounded = FALSE;
+ killX = killY = -1; // [HGM] lion
- GameEnds((ChessMove) 0, NULL, GE_PLAYER);
+ GameEnds(EndOfFile, NULL, GE_PLAYER);
if(appData.serverMovesName != NULL) {
/* [HGM] prepare to make moves file for broadcasting */
clock_t t = clock();
ModeHighlight();
if(appData.icsActive) gameInfo.variant = VariantNormal;
currentMove = forwardMostMove = backwardMostMove = 0;
+ MarkTargetSquares(1);
InitPosition(redraw);
for (i = 0; i < MAX_MOVES; i++) {
if (commentList[i] != NULL) {
ResetClocks();
timeRemaining[0][0] = whiteTimeRemaining;
timeRemaining[1][0] = blackTimeRemaining;
- if (first.pr == NULL) {
+
+ if (first.pr == NoProc) {
StartChessProgram(&first);
}
if (init) {
DisplayMessage("", "");
HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
lastSavedGame = 0; // [HGM] save: make sure next game counts as unsaved
+ ClearMap(); // [HGM] exclude: invalidate map
}
void
-AutoPlayGameLoop()
+AutoPlayGameLoop ()
{
for (;;) {
if (!AutoPlayOneMove())
return;
if (matchMode || appData.timeDelay == 0)
continue;
- if (appData.timeDelay < 0 || gameMode == AnalyzeFile)
+ if (appData.timeDelay < 0)
return;
- StartLoadGameTimer((long)(1000.0 * appData.timeDelay));
+ StartLoadGameTimer((long)(1000.0f * appData.timeDelay));
break;
}
}
+void
+AnalyzeNextGame()
+{
+ ReloadGame(1); // next game
+}
int
-AutoPlayOneMove()
+AutoPlayOneMove ()
{
int fromX, fromY, toX, toY;
fprintf(debugFP, "AutoPlayOneMove(): current %d\n", currentMove);
}
- if (gameMode != PlayFromGameFile)
+ if (gameMode != PlayFromGameFile && gameMode != AnalyzeFile)
return FALSE;
+ if (gameMode == AnalyzeFile && currentMove > backwardMostMove && programStats.depth) {
+ pvInfoList[currentMove].depth = programStats.depth;
+ pvInfoList[currentMove].score = programStats.score;
+ pvInfoList[currentMove].time = 0;
+ if(currentMove < forwardMostMove) AppendComment(currentMove+1, lastPV[0], 2);
+ else { // append analysis of final position as comment
+ char buf[MSG_SIZ];
+ snprintf(buf, MSG_SIZ, "{final score %+4.2f/%d}", programStats.score/100., programStats.depth);
+ AppendComment(currentMove, buf, 3); // the 3 prevents stripping of the score/depth!
+ }
+ programStats.depth = 0;
+ }
+
if (currentMove >= forwardMostMove) {
- gameMode = EditGame;
- ModeHighlight();
+ if(gameMode == AnalyzeFile) {
+ if(appData.loadGameIndex == -1) {
+ GameEnds(gameInfo.result, gameInfo.resultDetails ? gameInfo.resultDetails : "", GE_FILE);
+ ScheduleDelayedEvent(AnalyzeNextGame, 10);
+ } else {
+ ExitAnalyzeMode(); SendToProgram("force\n", &first);
+ }
+ }
+// gameMode = EndOfGame;
+// ModeHighlight();
/* [AS] Clear current move marker at the end of a game */
/* HistorySet(parseList, backwardMostMove, forwardMostMove, -1); */
return FALSE;
}
-
+
toX = moveList[currentMove][2] - AAA;
toY = moveList[currentMove][3] - ONE;
SetHighlights(-1, -1, toX, toY);
}
} else {
+ int viaX = moveList[currentMove][5] - AAA;
+ int viaY = moveList[currentMove][6] - ONE;
fromX = moveList[currentMove][0] - AAA;
fromY = moveList[currentMove][1] - ONE;
HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove); /* [AS] */
+ if(moveList[currentMove][4] == ';') { // multi-leg
+ ChessSquare piece = boards[currentMove][viaY][viaX];
+ AnimateMove(boards[currentMove], fromX, fromY, viaX, viaY);
+ boards[currentMove][viaY][viaX] = boards[currentMove][fromY][fromX];
+ AnimateMove(boards[currentMove], fromX=viaX, fromY=viaY, toX, toY);
+ boards[currentMove][viaY][viaX] = piece;
+ } else
AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
if (appData.highlightLastMove) {
int
-LoadGameOneMove(readAhead)
- ChessMove readAhead;
+LoadGameOneMove (ChessMove readAhead)
{
int fromX = 0, fromY = 0, toX = 0, toY = 0, done;
char promoChar = NULLCHAR;
ChessMove moveType;
char move[MSG_SIZ];
char *p, *q;
-
- if (gameMode != PlayFromGameFile && gameMode != AnalyzeFile &&
+
+ if (gameMode != PlayFromGameFile && gameMode != AnalyzeFile &&
gameMode != AnalyzeMode && gameMode != Training) {
gameFileFP = NULL;
return FALSE;
}
-
+
yyboardindex = forwardMostMove;
- if (readAhead != (ChessMove)0) {
+ if (readAhead != EndOfFile) {
moveType = readAhead;
} else {
if (gameFileFP == NULL)
return FALSE;
- moveType = (ChessMove) yylex();
+ moveType = (ChessMove) Myylex();
}
-
+
done = FALSE;
switch (moveType) {
case Comment:
- if (appData.debugMode)
+ if (appData.debugMode)
fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
p = yy_text;
- if (*p == '{' || *p == '[' || *p == '(') {
- p[strlen(p) - 1] = NULLCHAR;
- p++;
- }
/* append the comment but don't display it */
- while (*p == '\n') p++;
- AppendComment(currentMove, p);
+ AppendComment(currentMove, p, FALSE);
return TRUE;
case WhiteCapturesEnPassant:
case BlackCapturesEnPassant:
- case WhitePromotionChancellor:
- case BlackPromotionChancellor:
- case WhitePromotionArchbishop:
- case BlackPromotionArchbishop:
- case WhitePromotionCentaur:
- case BlackPromotionCentaur:
- case WhitePromotionQueen:
- case BlackPromotionQueen:
- case WhitePromotionRook:
- case BlackPromotionRook:
- case WhitePromotionBishop:
- case BlackPromotionBishop:
- case WhitePromotionKnight:
- case BlackPromotionKnight:
- case WhitePromotionKing:
- case BlackPromotionKing:
+ case WhitePromotion:
+ case BlackPromotion:
+ case WhiteNonPromotion:
+ case BlackNonPromotion:
case NormalMove:
+ case FirstLeg:
case WhiteKingSideCastle:
case WhiteQueenSideCastle:
case BlackKingSideCastle:
case BlackASideCastleFR:
/* POP Fabien */
if (appData.debugMode)
- fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
+ fprintf(debugFP, "Parsed %s into %s virgin=%x,%x\n", yy_text, currentMoveString, boards[forwardMostMove][TOUCHED_W], boards[forwardMostMove][TOUCHED_B]);
fromX = currentMoveString[0] - AAA;
fromY = currentMoveString[1] - ONE;
toX = currentMoveString[2] - AAA;
toY = currentMoveString[3] - ONE;
promoChar = currentMoveString[4];
+ if(promoChar == ';') promoChar = NULLCHAR;
break;
case WhiteDrop:
if (q != NULL) *q = NULLCHAR;
p++;
}
+ while(q = strchr(p, '\n')) *q = ' '; // [HGM] crush linefeeds in result message
GameEnds(moveType, p, GE_FILE);
done = TRUE;
if (cmailMsgLoaded) {
}
break;
- case (ChessMove) 0: /* end of file */
+ case EndOfFile:
if (appData.debugMode)
fprintf(debugFP, "Parser hit end of file\n");
- switch (MateTest(boards[currentMove], PosFlags(currentMove),
- EP_UNKNOWN, castlingRights[currentMove]) ) {
+ switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
case MT_NONE:
case MT_CHECK:
break;
if (appData.debugMode)
fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
yy_text, (int) moveType);
- return LoadGameOneMove((ChessMove)0); /* tail recursion */
+ return LoadGameOneMove(EndOfFile); /* tail recursion */
}
/* else fall thru */
/* Reached start of next game in file */
if (appData.debugMode)
fprintf(debugFP, "Parsed start of next game: %s\n", yy_text);
- switch (MateTest(boards[currentMove], PosFlags(currentMove),
- EP_UNKNOWN, castlingRights[currentMove]) ) {
+ switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
case MT_NONE:
case MT_CHECK:
break;
if (appData.debugMode)
fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
yy_text, (int) moveType);
- return LoadGameOneMove((ChessMove)0); /* tail recursion */
+ return LoadGameOneMove(EndOfFile); /* tail recursion */
case IllegalMove:
if (appData.testLegality) {
if (appData.debugMode)
fprintf(debugFP, "Parsed IllegalMove: %s\n", yy_text);
- sprintf(move, _("Illegal move: %d.%s%s"),
+ snprintf(move, MSG_SIZ, _("Illegal move: %d.%s%s"),
(forwardMostMove / 2) + 1,
WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
DisplayError(move, 0);
if (appData.debugMode)
fprintf(debugFP, "Parsed %s into IllegalMove %s\n",
yy_text, currentMoveString);
- fromX = currentMoveString[0] - AAA;
- fromY = currentMoveString[1] - ONE;
+ if(currentMoveString[1] == '@') {
+ fromX = CharToPiece(WhiteOnMove(currentMove) ? ToUpper(currentMoveString[0]) : ToLower(currentMoveString[0]));
+ fromY = DROP_RANK;
+ } else {
+ fromX = currentMoveString[0] - AAA;
+ fromY = currentMoveString[1] - ONE;
+ }
toX = currentMoveString[2] - AAA;
toY = currentMoveString[3] - ONE;
promoChar = currentMoveString[4];
case AmbiguousMove:
if (appData.debugMode)
fprintf(debugFP, "Parsed AmbiguousMove: %s\n", yy_text);
- sprintf(move, _("Ambiguous move: %d.%s%s"),
+ snprintf(move, MSG_SIZ, _("Ambiguous move: %d.%s%s"),
(forwardMostMove / 2) + 1,
WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
DisplayError(move, 0);
case ImpossibleMove:
if (appData.debugMode)
fprintf(debugFP, "Parsed ImpossibleMove (type = %d): %s\n", moveType, yy_text);
- sprintf(move, _("Illegal move: %d.%s%s"),
+ snprintf(move, MSG_SIZ, _("Illegal move: %d.%s%s"),
(forwardMostMove / 2) + 1,
WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
DisplayError(move, 0);
return FALSE;
} else {
/* currentMoveString is set as a side-effect of yylex */
- strcat(currentMoveString, "\n");
- strcpy(moveList[forwardMostMove], currentMoveString);
-
+
thinkOutput[0] = NULLCHAR;
MakeMove(fromX, fromY, toX, toY, promoChar);
+ killX = killY = -1; // [HGM] lion: used up
currentMove = forwardMostMove;
return TRUE;
}
/* Load the nth game from the given file */
int
-LoadGameFromFile(filename, n, title, useList)
- char *filename;
- int n;
- char *title;
- /*Boolean*/ int useList;
+LoadGameFromFile (char *filename, int n, char *title, int useList)
{
FILE *f;
char buf[MSG_SIZ];
void
-MakeRegisteredMove()
+MakeRegisteredMove ()
{
int fromX, fromY, toX, toY;
char promoChar;
if (appData.debugMode)
fprintf(debugFP, "Restoring %s for game %d\n",
cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
-
+
thinkOutput[0] = NULLCHAR;
- strcpy(moveList[currentMove], cmailMove[lastLoadGameNumber - 1]);
+ safeStrCpy(moveList[currentMove], cmailMove[lastLoadGameNumber - 1], sizeof(moveList[currentMove])/sizeof(moveList[currentMove][0]));
fromX = cmailMove[lastLoadGameNumber - 1][0] - AAA;
fromY = cmailMove[lastLoadGameNumber - 1][1] - ONE;
toX = cmailMove[lastLoadGameNumber - 1][2] - AAA;
promoChar = cmailMove[lastLoadGameNumber - 1][4];
MakeMove(fromX, fromY, toX, toY, promoChar);
ShowMove(fromX, fromY, toX, toY);
-
- switch (MateTest(boards[currentMove], PosFlags(currentMove),
- EP_UNKNOWN, castlingRights[currentMove]) ) {
+
+ switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
case MT_NONE:
case MT_CHECK:
break;
-
+
case MT_CHECKMATE:
case MT_STAINMATE:
if (WhiteOnMove(currentMove)) {
GameEnds(WhiteWins, "White mates", GE_PLAYER);
}
break;
-
+
case MT_STALEMATE:
GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
break;
}
break;
-
+
case CMAIL_RESIGN:
if (WhiteOnMove(currentMove)) {
GameEnds(BlackWins, "White resigns", GE_PLAYER);
GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
}
break;
-
+
case CMAIL_ACCEPT:
GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
break;
-
+
default:
break;
}
/* Wrapper around LoadGame for use when a Cmail message is loaded */
int
-CmailLoadGame(f, gameNumber, title, useList)
- FILE *f;
- int gameNumber;
- char *title;
- int useList;
+CmailLoadGame (FILE *f, int gameNumber, char *title, int useList)
{
int retVal;
/* Support for LoadNextGame, LoadPreviousGame, ReloadSameGame */
int
-ReloadGame(offset)
- int offset;
+ReloadGame (int offset)
{
int gameNumber = lastLoadGameNumber + offset;
if (lastLoadGameFP == NULL) {
}
}
+int keys[EmptySquare+1];
+
+int
+PositionMatches (Board b1, Board b2)
+{
+ int r, f, sum=0;
+ switch(appData.searchMode) {
+ case 1: return CompareWithRights(b1, b2);
+ case 2:
+ for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
+ if(b2[r][f] != EmptySquare && b1[r][f] != b2[r][f]) return FALSE;
+ }
+ return TRUE;
+ case 3:
+ for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
+ if((b2[r][f] == WhitePawn || b2[r][f] == BlackPawn) && b1[r][f] != b2[r][f]) return FALSE;
+ sum += keys[b1[r][f]] - keys[b2[r][f]];
+ }
+ return sum==0;
+ case 4:
+ for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
+ sum += keys[b1[r][f]] - keys[b2[r][f]];
+ }
+ return sum==0;
+ }
+ return TRUE;
+}
+
+#define Q_PROMO 4
+#define Q_EP 3
+#define Q_BCASTL 2
+#define Q_WCASTL 1
+int pieceList[256], quickBoard[256];
+ChessSquare pieceType[256] = { EmptySquare };
+Board soughtBoard, reverseBoard, flipBoard, rotateBoard;
+int counts[EmptySquare], minSought[EmptySquare], minReverse[EmptySquare], maxSought[EmptySquare], maxReverse[EmptySquare];
+int soughtTotal, turn;
+Boolean epOK, flipSearch;
+
+typedef struct {
+ unsigned char piece, to;
+} Move;
+
+#define DSIZE (250000)
+
+Move initialSpace[DSIZE+1000]; // gamble on that game will not be more than 500 moves
+Move *moveDatabase = initialSpace;
+unsigned int movePtr, dataSize = DSIZE;
+
+int
+MakePieceList (Board board, int *counts)
+{
+ int r, f, n=Q_PROMO, total=0;
+ for(r=0;r<EmptySquare;r++) counts[r] = 0; // piece-type counts
+ for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
+ int sq = f + (r<<4);
+ if(board[r][f] == EmptySquare) quickBoard[sq] = 0; else {
+ quickBoard[sq] = ++n;
+ pieceList[n] = sq;
+ pieceType[n] = board[r][f];
+ counts[board[r][f]]++;
+ if(board[r][f] == WhiteKing) pieceList[1] = n; else
+ if(board[r][f] == BlackKing) pieceList[2] = n; // remember which are Kings, for castling
+ total++;
+ }
+ }
+ epOK = gameInfo.variant != VariantXiangqi && gameInfo.variant != VariantBerolina;
+ return total;
+}
+
+void
+PackMove (int fromX, int fromY, int toX, int toY, ChessSquare promoPiece)
+{
+ int sq = fromX + (fromY<<4);
+ int piece = quickBoard[sq], rook;
+ quickBoard[sq] = 0;
+ moveDatabase[movePtr].to = pieceList[piece] = sq = toX + (toY<<4);
+ if(piece == pieceList[1] && fromY == toY) {
+ if((toX > fromX+1 || toX < fromX-1) && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1) {
+ int from = toX>fromX ? BOARD_RGHT-1 : BOARD_LEFT;
+ moveDatabase[movePtr++].piece = Q_WCASTL;
+ quickBoard[sq] = piece;
+ piece = quickBoard[from]; quickBoard[from] = 0;
+ moveDatabase[movePtr].to = pieceList[piece] = sq = toX>fromX ? sq-1 : sq+1;
+ } else if((rook = quickBoard[sq]) && pieceType[rook] == WhiteRook) { // FRC castling
+ quickBoard[sq] = 0; // remove Rook
+ moveDatabase[movePtr].to = sq = (toX>fromX ? BOARD_RGHT-2 : BOARD_LEFT+2); // King to-square
+ moveDatabase[movePtr++].piece = Q_WCASTL;
+ quickBoard[sq] = pieceList[1]; // put King
+ piece = rook;
+ moveDatabase[movePtr].to = pieceList[rook] = sq = toX>fromX ? sq-1 : sq+1;
+ }
+ } else
+ if(piece == pieceList[2] && fromY == toY) {
+ if((toX > fromX+1 || toX < fromX-1) && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1) {
+ int from = (toX>fromX ? BOARD_RGHT-1 : BOARD_LEFT) + (BOARD_HEIGHT-1 <<4);
+ moveDatabase[movePtr++].piece = Q_BCASTL;
+ quickBoard[sq] = piece;
+ piece = quickBoard[from]; quickBoard[from] = 0;
+ moveDatabase[movePtr].to = pieceList[piece] = sq = toX>fromX ? sq-1 : sq+1;
+ } else if((rook = quickBoard[sq]) && pieceType[rook] == BlackRook) { // FRC castling
+ quickBoard[sq] = 0; // remove Rook
+ moveDatabase[movePtr].to = sq = (toX>fromX ? BOARD_RGHT-2 : BOARD_LEFT+2);
+ moveDatabase[movePtr++].piece = Q_BCASTL;
+ quickBoard[sq] = pieceList[2]; // put King
+ piece = rook;
+ moveDatabase[movePtr].to = pieceList[rook] = sq = toX>fromX ? sq-1 : sq+1;
+ }
+ } else
+ if(epOK && (pieceType[piece] == WhitePawn || pieceType[piece] == BlackPawn) && fromX != toX && quickBoard[sq] == 0) {
+ quickBoard[(fromY<<4)+toX] = 0;
+ moveDatabase[movePtr].piece = Q_EP;
+ moveDatabase[movePtr++].to = (fromY<<4)+toX;
+ moveDatabase[movePtr].to = sq;
+ } else
+ if(promoPiece != pieceType[piece]) {
+ moveDatabase[movePtr++].piece = Q_PROMO;
+ moveDatabase[movePtr].to = pieceType[piece] = (int) promoPiece;
+ }
+ moveDatabase[movePtr].piece = piece;
+ quickBoard[sq] = piece;
+ movePtr++;
+}
+
+int
+PackGame (Board board)
+{
+ Move *newSpace = NULL;
+ moveDatabase[movePtr].piece = 0; // terminate previous game
+ if(movePtr > dataSize) {
+ if(appData.debugMode) fprintf(debugFP, "move-cache overflow, enlarge to %d MB\n", dataSize/128);
+ dataSize *= 8; // increase size by factor 8 (512KB -> 4MB -> 32MB -> 256MB -> 2GB)
+ if(dataSize) newSpace = (Move*) calloc(dataSize + 1000, sizeof(Move));
+ if(newSpace) {
+ int i;
+ Move *p = moveDatabase, *q = newSpace;
+ for(i=0; i<movePtr; i++) *q++ = *p++; // copy to newly allocated space
+ if(dataSize > 8*DSIZE) free(moveDatabase); // and free old space (if it was allocated)
+ moveDatabase = newSpace;
+ } else { // calloc failed, we must be out of memory. Too bad...
+ dataSize = 0; // prevent calloc events for all subsequent games
+ return 0; // and signal this one isn't cached
+ }
+ }
+ movePtr++;
+ MakePieceList(board, counts);
+ return movePtr;
+}
+
+int
+QuickCompare (Board board, int *minCounts, int *maxCounts)
+{ // compare according to search mode
+ int r, f;
+ switch(appData.searchMode)
+ {
+ case 1: // exact position match
+ if(!(turn & board[EP_STATUS-1])) return FALSE; // wrong side to move
+ for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
+ if(board[r][f] != pieceType[quickBoard[(r<<4)+f]]) return FALSE;
+ }
+ break;
+ case 2: // can have extra material on empty squares
+ for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
+ if(board[r][f] == EmptySquare) continue;
+ if(board[r][f] != pieceType[quickBoard[(r<<4)+f]]) return FALSE;
+ }
+ break;
+ case 3: // material with exact Pawn structure
+ for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
+ if(board[r][f] != WhitePawn && board[r][f] != BlackPawn) continue;
+ if(board[r][f] != pieceType[quickBoard[(r<<4)+f]]) return FALSE;
+ } // fall through to material comparison
+ case 4: // exact material
+ for(r=0; r<EmptySquare; r++) if(counts[r] != maxCounts[r]) return FALSE;
+ break;
+ case 6: // material range with given imbalance
+ for(r=0; r<BlackPawn; r++) if(counts[r] - minCounts[r] != counts[r+BlackPawn] - minCounts[r+BlackPawn]) return FALSE;
+ // fall through to range comparison
+ case 5: // material range
+ for(r=0; r<EmptySquare; r++) if(counts[r] < minCounts[r] || counts[r] > maxCounts[r]) return FALSE;
+ }
+ return TRUE;
+}
+
+int
+QuickScan (Board board, Move *move)
+{ // reconstruct game,and compare all positions in it
+ int cnt=0, stretch=0, found = -1, total = MakePieceList(board, counts);
+ do {
+ int piece = move->piece;
+ int to = move->to, from = pieceList[piece];
+ if(found < 0) { // if already found just scan to game end for final piece count
+ if(QuickCompare(soughtBoard, minSought, maxSought) ||
+ appData.ignoreColors && QuickCompare(reverseBoard, minReverse, maxReverse) ||
+ flipSearch && (QuickCompare(flipBoard, minSought, maxSought) ||
+ appData.ignoreColors && QuickCompare(rotateBoard, minReverse, maxReverse))
+ ) {
+ static int lastCounts[EmptySquare+1];
+ int i;
+ if(stretch) for(i=0; i<EmptySquare; i++) if(lastCounts[i] != counts[i]) { stretch = 0; break; } // reset if material changes
+ if(stretch++ == 0) for(i=0; i<EmptySquare; i++) lastCounts[i] = counts[i]; // remember actual material
+ } else stretch = 0;
+ if(stretch && (appData.searchMode == 1 || stretch >= appData.stretch)) found = cnt + 1 - stretch;
+ if(found >= 0 && !appData.minPieces) return found;
+ }
+ if(piece <= Q_PROMO) { // special moves encoded by otherwise invalid piece numbers 1-4
+ if(!piece) return (appData.minPieces && (total < appData.minPieces || total > appData.maxPieces) ? -1 : found);
+ if(piece == Q_PROMO) { // promotion, encoded as (Q_PROMO, to) + (piece, promoType)
+ piece = (++move)->piece;
+ from = pieceList[piece];
+ counts[pieceType[piece]]--;
+ pieceType[piece] = (ChessSquare) move->to;
+ counts[move->to]++;
+ } else if(piece == Q_EP) { // e.p. capture, encoded as (Q_EP, ep-sqr) + (piece, to)
+ counts[pieceType[quickBoard[to]]]--;
+ quickBoard[to] = 0; total--;
+ move++;
+ continue;
+ } else if(piece <= Q_BCASTL) { // castling, encoded as (Q_XCASTL, king-to) + (rook, rook-to)
+ piece = pieceList[piece]; // first two elements of pieceList contain King numbers
+ from = pieceList[piece]; // so this must be King
+ quickBoard[from] = 0;
+ pieceList[piece] = to;
+ from = pieceList[(++move)->piece]; // for FRC this has to be done here
+ quickBoard[from] = 0; // rook
+ quickBoard[to] = piece;
+ to = move->to; piece = move->piece;
+ goto aftercastle;
+ }
+ }
+ if(appData.searchMode > 2) counts[pieceType[quickBoard[to]]]--; // account capture
+ if((total -= (quickBoard[to] != 0)) < soughtTotal && found < 0) return -1; // piece count dropped below what we search for
+ quickBoard[from] = 0;
+ aftercastle:
+ quickBoard[to] = piece;
+ pieceList[piece] = to;
+ cnt++; turn ^= 3;
+ move++;
+ } while(1);
+}
+
+void
+InitSearch ()
+{
+ int r, f;
+ flipSearch = FALSE;
+ CopyBoard(soughtBoard, boards[currentMove]);
+ soughtTotal = MakePieceList(soughtBoard, maxSought);
+ soughtBoard[EP_STATUS-1] = (currentMove & 1) + 1;
+ if(currentMove == 0 && gameMode == EditPosition) soughtBoard[EP_STATUS-1] = blackPlaysFirst + 1; // (!)
+ CopyBoard(reverseBoard, boards[currentMove]);
+ for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
+ int piece = boards[currentMove][BOARD_HEIGHT-1-r][f];
+ if(piece < BlackPawn) piece += BlackPawn; else if(piece < EmptySquare) piece -= BlackPawn; // color-flip
+ reverseBoard[r][f] = piece;
+ }
+ reverseBoard[EP_STATUS-1] = soughtBoard[EP_STATUS-1] ^ 3;
+ for(r=0; r<6; r++) reverseBoard[CASTLING][r] = boards[currentMove][CASTLING][(r+3)%6];
+ if(appData.findMirror && appData.searchMode <= 3 && (!nrCastlingRights
+ || (boards[currentMove][CASTLING][2] == NoRights ||
+ boards[currentMove][CASTLING][0] == NoRights && boards[currentMove][CASTLING][1] == NoRights )
+ && (boards[currentMove][CASTLING][5] == NoRights ||
+ boards[currentMove][CASTLING][3] == NoRights && boards[currentMove][CASTLING][4] == NoRights ) )
+ ) {
+ flipSearch = TRUE;
+ CopyBoard(flipBoard, soughtBoard);
+ CopyBoard(rotateBoard, reverseBoard);
+ for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
+ flipBoard[r][f] = soughtBoard[r][BOARD_WIDTH-1-f];
+ rotateBoard[r][f] = reverseBoard[r][BOARD_WIDTH-1-f];
+ }
+ }
+ for(r=0; r<BlackPawn; r++) maxReverse[r] = maxSought[r+BlackPawn], maxReverse[r+BlackPawn] = maxSought[r];
+ if(appData.searchMode >= 5) {
+ for(r=BOARD_HEIGHT/2; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) soughtBoard[r][f] = EmptySquare;
+ MakePieceList(soughtBoard, minSought);
+ for(r=0; r<BlackPawn; r++) minReverse[r] = minSought[r+BlackPawn], minReverse[r+BlackPawn] = minSought[r];
+ }
+ if(gameInfo.variant == VariantCrazyhouse || gameInfo.variant == VariantShogi || gameInfo.variant == VariantBughouse)
+ soughtTotal = 0; // in drop games nr of pieces does not fall monotonously
+}
+
+GameInfo dummyInfo;
+static int creatingBook;
+
+int
+GameContainsPosition (FILE *f, ListGame *lg)
+{
+ int next, btm=0, plyNr=0, scratch=forwardMostMove+2&~1;
+ int fromX, fromY, toX, toY;
+ char promoChar;
+ static int initDone=FALSE;
+
+ // weed out games based on numerical tag comparison
+ if(lg->gameInfo.variant != gameInfo.variant) return -1; // wrong variant
+ if(appData.eloThreshold1 && (lg->gameInfo.whiteRating < appData.eloThreshold1 && lg->gameInfo.blackRating < appData.eloThreshold1)) return -1;
+ if(appData.eloThreshold2 && (lg->gameInfo.whiteRating < appData.eloThreshold2 || lg->gameInfo.blackRating < appData.eloThreshold2)) return -1;
+ if(appData.dateThreshold && (!lg->gameInfo.date || atoi(lg->gameInfo.date) < appData.dateThreshold)) return -1;
+ if(!initDone) {
+ for(next = WhitePawn; next<EmptySquare; next++) keys[next] = random()>>8 ^ random()<<6 ^random()<<20;
+ initDone = TRUE;
+ }
+ if(lg->gameInfo.fen) ParseFEN(boards[scratch], &btm, lg->gameInfo.fen, FALSE);
+ else CopyBoard(boards[scratch], initialPosition); // default start position
+ if(lg->moves) {
+ turn = btm + 1;
+ if((next = QuickScan( boards[scratch], &moveDatabase[lg->moves] )) < 0) return -1; // quick scan rules out it is there
+ if(appData.searchMode >= 4) return next; // for material searches, trust QuickScan.
+ }
+ if(btm) plyNr++;
+ if(PositionMatches(boards[scratch], boards[currentMove])) return plyNr;
+ fseek(f, lg->offset, 0);
+ yynewfile(f);
+ while(1) {
+ yyboardindex = scratch;
+ quickFlag = plyNr+1;
+ next = Myylex();
+ quickFlag = 0;
+ switch(next) {
+ case PGNTag:
+ if(plyNr) return -1; // after we have seen moves, any tags will be start of next game
+ default:
+ continue;
+
+ case XBoardGame:
+ case GNUChessGame:
+ if(plyNr) return -1; // after we have seen moves, this is for new game
+ continue;
+
+ case AmbiguousMove: // we cannot reconstruct the game beyond these two
+ case ImpossibleMove:
+ case WhiteWins: // game ends here with these four
+ case BlackWins:
+ case GameIsDrawn:
+ case GameUnfinished:
+ return -1;
+
+ case IllegalMove:
+ if(appData.testLegality) return -1;
+ case WhiteCapturesEnPassant:
+ case BlackCapturesEnPassant:
+ case WhitePromotion:
+ case BlackPromotion:
+ case WhiteNonPromotion:
+ case BlackNonPromotion:
+ case NormalMove:
+ case FirstLeg:
+ case WhiteKingSideCastle:
+ case WhiteQueenSideCastle:
+ case BlackKingSideCastle:
+ case BlackQueenSideCastle:
+ case WhiteKingSideCastleWild:
+ case WhiteQueenSideCastleWild:
+ case BlackKingSideCastleWild:
+ case BlackQueenSideCastleWild:
+ case WhiteHSideCastleFR:
+ case WhiteASideCastleFR:
+ case BlackHSideCastleFR:
+ case BlackASideCastleFR:
+ fromX = currentMoveString[0] - AAA;
+ fromY = currentMoveString[1] - ONE;
+ toX = currentMoveString[2] - AAA;
+ toY = currentMoveString[3] - ONE;
+ promoChar = currentMoveString[4];
+ break;
+ case WhiteDrop:
+ case BlackDrop:
+ fromX = next == WhiteDrop ?
+ (int) CharToPiece(ToUpper(currentMoveString[0])) :
+ (int) CharToPiece(ToLower(currentMoveString[0]));
+ fromY = DROP_RANK;
+ toX = currentMoveString[2] - AAA;
+ toY = currentMoveString[3] - ONE;
+ promoChar = 0;
+ break;
+ }
+ // Move encountered; peform it. We need to shuttle between two boards, as even/odd index determines side to move
+ plyNr++;
+ ApplyMove(fromX, fromY, toX, toY, promoChar, boards[scratch]);
+ if(PositionMatches(boards[scratch], boards[currentMove])) return plyNr;
+ if(appData.ignoreColors && PositionMatches(boards[scratch], reverseBoard)) return plyNr;
+ if(appData.findMirror) {
+ if(PositionMatches(boards[scratch], flipBoard)) return plyNr;
+ if(appData.ignoreColors && PositionMatches(boards[scratch], rotateBoard)) return plyNr;
+ }
+ }
+}
/* Load the nth game from open file f */
int
-LoadGame(f, gameNumber, title, useList)
- FILE *f;
- int gameNumber;
- char *title;
- int useList;
+LoadGame (FILE *f, int gameNumber, char *title, int useList)
{
ChessMove cm;
char buf[MSG_SIZ];
int gn = gameNumber;
ListGame *lg = NULL;
int numPGNTags = 0;
- int err;
+ int err, pos = -1;
GameMode oldGameMode;
- VariantClass oldVariant = gameInfo.variant; /* [HGM] PGNvariant */
+ VariantClass v, oldVariant = gameInfo.variant; /* [HGM] PGNvariant */
+ char oldName[MSG_SIZ];
- if (appData.debugMode)
+ safeStrCpy(oldName, engineVariant, MSG_SIZ); v = oldVariant;
+
+ if (appData.debugMode)
fprintf(debugFP, "LoadGame(): on entry, gameMode %d\n", gameMode);
if (gameMode == Training )
if (gameMode != BeginningOfGame) {
Reset(FALSE, TRUE);
}
+ killX = killY = -1; // [HGM] lion: in case we did not Reset
gameFileFP = f;
if (lastLoadGameFP != NULL && lastLoadGameFP != f) {
if (useList) {
lg = (ListGame *) ListElem(&gameList, gameNumber-1);
-
+
if (lg) {
fseek(f, lg->offset, 0);
GameListHighlight(gameNumber);
+ pos = lg->position;
gn = 1;
}
else {
+ if(oldGameMode == AnalyzeFile && appData.loadGameIndex == -1)
+ appData.loadGameIndex = 0; // [HGM] suppress error message if we reach file end after auto-stepping analysis
+ else
DisplayError(_("Game number out of range"), 0);
return FALSE;
}
}
lastLoadGameFP = f;
lastLoadGameNumber = gameNumber;
- strcpy(lastLoadGameTitle, title);
+ safeStrCpy(lastLoadGameTitle, title, sizeof(lastLoadGameTitle)/sizeof(lastLoadGameTitle[0]));
lastLoadGameUseList = useList;
yynewfile(f);
if (lg && lg->gameInfo.white && lg->gameInfo.black) {
- snprintf(buf, sizeof(buf), "%s vs. %s", lg->gameInfo.white,
+ snprintf(buf, sizeof(buf), "%s %s %s", lg->gameInfo.white, _("vs."),
lg->gameInfo.black);
DisplayTitle(buf);
} else if (*title != NULLCHAR) {
if (gameNumber > 1) {
- sprintf(buf, "%s %d", title, gameNumber);
+ snprintf(buf, MSG_SIZ, "%s %d", title, gameNumber);
DisplayTitle(buf);
} else {
DisplayTitle(title);
/*
* Skip the first gn-1 games in the file.
- * Also skip over anything that precedes an identifiable
- * start of game marker, to avoid being confused by
- * garbage at the start of the file. Currently
+ * Also skip over anything that precedes an identifiable
+ * start of game marker, to avoid being confused by
+ * garbage at the start of the file. Currently
* recognized start of game markers are the move number "1",
* the pattern "gnuchess .* game", the pattern
- * "^[#;%] [^ ]* game file", and a PGN tag block.
+ * "^[#;%] [^ ]* game file", and a PGN tag block.
* A game that starts with one of the latter two patterns
* will also have a move number 1, possibly
* following a position diagram.
* 5-4-02: Let's try being more lenient and allowing a game to
* start with an unnumbered move. Does that break anything?
*/
- cm = lastLoadGameStart = (ChessMove) 0;
+ cm = lastLoadGameStart = EndOfFile;
while (gn > 0) {
yyboardindex = forwardMostMove;
- cm = (ChessMove) yylex();
+ cm = (ChessMove) Myylex();
switch (cm) {
- case (ChessMove) 0:
+ case EndOfFile:
if (cmailMsgLoaded) {
nCmailGames = CMAIL_MAX_GAMES - gn;
} else {
gn--;
lastLoadGameStart = cm;
break;
-
+
case MoveNumberOne:
switch (lastLoadGameStart) {
case GNUChessGame:
case PGNTag:
break;
case MoveNumberOne:
- case (ChessMove) 0:
+ case EndOfFile:
gn--; /* count this game */
lastLoadGameStart = cm;
break;
case GNUChessGame:
case PGNTag:
case MoveNumberOne:
- case (ChessMove) 0:
+ case EndOfFile:
gn--; /* count this game */
lastLoadGameStart = cm;
break;
if (gn > 0) {
do {
yyboardindex = forwardMostMove;
- cm = (ChessMove) yylex();
+ cm = (ChessMove) Myylex();
} while (cm == PGNTag || cm == Comment);
}
break;
break;
case NormalMove:
+ case FirstLeg:
/* Only a NormalMove can be at the start of a game
* without a position diagram. */
- if (lastLoadGameStart == (ChessMove) 0) {
+ if (lastLoadGameStart == EndOfFile ) {
gn--;
lastLoadGameStart = MoveNumberOne;
}
break;
}
}
-
+
if (appData.debugMode)
fprintf(debugFP, "Parsed game start '%s' (%d)\n", yy_text, (int) cm);
/* Skip any header junk before position diagram and/or move 1 */
for (;;) {
yyboardindex = forwardMostMove;
- cm = (ChessMove) yylex();
+ cm = (ChessMove) Myylex();
- if (cm == (ChessMove) 0 ||
+ if (cm == EndOfFile ||
cm == GNUChessGame || cm == XBoardGame) {
/* Empty game; pretend end-of-file and handle later */
- cm = (ChessMove) 0;
+ cm = EndOfFile;
break;
}
free(gameInfo.event);
}
gameInfo.event = StrSave(yy_text);
- }
+ }
- startedFromSetupPosition = FALSE;
+ startedFromSetupPosition = startedFromPositionFile; // [HGM]
while (cm == PGNTag) {
- if (appData.debugMode)
+ if (appData.debugMode)
fprintf(debugFP, "Parsed PGNTag: %s\n", yy_text);
err = ParsePGNTag(yy_text, &gameInfo);
if (!err) numPGNTags++;
/* [HGM] PGNvariant: automatically switch to variant given in PGN tag */
- if(gameInfo.variant != oldVariant) {
+ if(gameInfo.variant != oldVariant && (gameInfo.variant != VariantNormal || gameInfo.variantName == NULL || *gameInfo.variantName == NULLCHAR)) {
startedFromPositionFile = FALSE; /* [HGM] loadPos: variant switch likely makes position invalid */
+ ResetFrontEnd(); // [HGM] might need other bitmaps. Cannot use Reset() because it clears gameInfo :-(
InitPosition(TRUE);
oldVariant = gameInfo.variant;
- if (appData.debugMode)
+ if (appData.debugMode)
fprintf(debugFP, "New variant %d\n", (int) oldVariant);
}
if (gameInfo.fen != NULL) {
Board initial_position;
startedFromSetupPosition = TRUE;
- if (!ParseFEN(initial_position, &blackPlaysFirst, gameInfo.fen)) {
+ if (!ParseFEN(initial_position, &blackPlaysFirst, gameInfo.fen, TRUE)) {
Reset(TRUE, TRUE);
DisplayError(_("Bad FEN position in file"), 0);
return FALSE;
}
CopyBoard(boards[0], initial_position);
+ if(*engineVariant) // [HGM] for now, assume FEN in engine-defined variant game is default initial position
+ CopyBoard(initialPosition, initial_position);
if (blackPlaysFirst) {
currentMove = forwardMostMove = backwardMostMove = 1;
CopyBoard(boards[1], initial_position);
- strcpy(moveList[0], "");
- strcpy(parseList[0], "");
+ safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
+ safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
timeRemaining[0][1] = whiteTimeRemaining;
timeRemaining[1][1] = blackTimeRemaining;
if (commentList[0] != NULL) {
/* [HGM] copy FEN attributes as well. Bugfix 4.3.14m and 4.3.15e: moved to after 'blackPlaysFirst' */
{ int i;
initialRulePlies = FENrulePlies;
- epStatus[forwardMostMove] = FENepStatus;
for( i=0; i< nrCastlingRights; i++ )
- initialRights[i] = castlingRights[forwardMostMove][i] = FENcastlingRights[i];
+ initialRights[i] = initial_position[CASTLING][i];
}
yyboardindex = forwardMostMove;
free(gameInfo.fen);
}
yyboardindex = forwardMostMove;
- cm = (ChessMove) yylex();
+ cm = (ChessMove) Myylex();
/* Handle comments interspersed among the tags */
while (cm == Comment) {
char *p;
- if (appData.debugMode)
+ if (appData.debugMode)
fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
p = yy_text;
- if (*p == '{' || *p == '[' || *p == '(') {
- p[strlen(p) - 1] = NULLCHAR;
- p++;
- }
- while (*p == '\n') p++;
- AppendComment(currentMove, p);
+ AppendComment(currentMove, p, FALSE);
yyboardindex = forwardMostMove;
- cm = (ChessMove) yylex();
+ cm = (ChessMove) Myylex();
}
}
if (numPGNTags > 0){
char *tags;
if (gameInfo.variant == VariantNormal) {
- gameInfo.variant = StringToVariant(gameInfo.event);
+ VariantClass v = StringToVariant(gameInfo.event);
+ // [HGM] do not recognize variants from event tag that were introduced after supporting variant tag
+ if(v < VariantShogi) gameInfo.variant = v;
}
if (!matchMode) {
if( appData.autoDisplayTags ) {
for (i = BOARD_HEIGHT - 1; i >= 0; i--)
for (j = BOARD_LEFT; j < BOARD_RGHT; p++)
switch (*p) {
+ case '{':
case '[':
case '-':
case ' ':
}
while (*p == ' ' || *p == '\t' ||
*p == '\n' || *p == '\r') p++;
-
+
if (strncmp(p, "black", strlen("black"))==0)
blackPlaysFirst = TRUE;
else
blackPlaysFirst = FALSE;
startedFromSetupPosition = TRUE;
-
+
CopyBoard(boards[0], initial_position);
if (blackPlaysFirst) {
currentMove = forwardMostMove = backwardMostMove = 1;
CopyBoard(boards[1], initial_position);
- strcpy(moveList[0], "");
- strcpy(parseList[0], "");
+ safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
+ safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
timeRemaining[0][1] = whiteTimeRemaining;
timeRemaining[1][1] = blackTimeRemaining;
if (commentList[0] != NULL) {
}
}
yyboardindex = forwardMostMove;
- cm = (ChessMove) yylex();
+ cm = (ChessMove) Myylex();
}
+ if(!creatingBook) {
if (first.pr == NoProc) {
StartChessProgram(&first);
}
InitChessProgram(&first, FALSE);
+ if(gameInfo.variant == VariantUnknown && *oldName) {
+ safeStrCpy(engineVariant, oldName, MSG_SIZ);
+ gameInfo.variant = v;
+ }
SendToProgram("force\n", &first);
if (startedFromSetupPosition) {
SendBoard(&first, forwardMostMove);
fprintf(debugFP, "Load Game\n");
}
DisplayBothClocks();
- }
+ }
+ }
/* [HGM] server: flag to write setup moves in broadcast file as one */
loadFlag = appData.suppressLoadMoves;
while (cm == Comment) {
char *p;
- if (appData.debugMode)
+ if (appData.debugMode)
fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
p = yy_text;
- if (*p == '{' || *p == '[' || *p == '(') {
- p[strlen(p) - 1] = NULLCHAR;
- p++;
- }
- while (*p == '\n') p++;
- AppendComment(currentMove, p);
+ AppendComment(currentMove, p, FALSE);
yyboardindex = forwardMostMove;
- cm = (ChessMove) yylex();
+ cm = (ChessMove) Myylex();
}
- if ((cm == (ChessMove) 0 && lastLoadGameStart != (ChessMove) 0) ||
+ if ((cm == EndOfFile && lastLoadGameStart != EndOfFile ) ||
cm == WhiteWins || cm == BlackWins ||
cm == GameIsDrawn || cm == GameUnfinished) {
DisplayMessage("", _("No moves in game"));
if (!matchMode && (pausing || appData.timeDelay != 0)) {
DisplayComment(currentMove - 1, commentList[currentMove]);
}
- if (!matchMode && appData.timeDelay != 0)
+ if (!matchMode && appData.timeDelay != 0)
DrawPosition(FALSE, boards[currentMove]);
if (gameMode == AnalyzeFile || gameMode == AnalyzeMode) {
}
/* if the first token after the PGN tags is a move
- * and not move number 1, retrieve it from the parser
+ * and not move number 1, retrieve it from the parser
*/
if (cm != MoveNumberOne)
LoadGameOneMove(cm);
/* load the remaining moves from the file */
- while (LoadGameOneMove((ChessMove)0)) {
+ while (LoadGameOneMove(EndOfFile)) {
timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
timeRemaining[1][forwardMostMove] = blackTimeRemaining;
}
HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
- if (oldGameMode == AnalyzeFile ||
- oldGameMode == AnalyzeMode) {
+ if (oldGameMode == AnalyzeFile) {
+ appData.loadGameIndex = -1; // [HGM] order auto-stepping through games
+ AnalyzeFileEvent();
+ } else
+ if (oldGameMode == AnalyzeMode) {
AnalyzeFileEvent();
}
+ if(gameInfo.result == GameUnfinished && gameInfo.resultDetails && appData.clockMode) {
+ long int w, b; // [HGM] adjourn: restore saved clock times
+ char *p = strstr(gameInfo.resultDetails, "(Clocks:");
+ if(p && sscanf(p+8, "%ld,%ld", &w, &b) == 2) {
+ timeRemaining[0][forwardMostMove] = whiteTimeRemaining = 1000*w + 500;
+ timeRemaining[1][forwardMostMove] = blackTimeRemaining = 1000*b + 500;
+ }
+ }
+
+ if(creatingBook) return TRUE;
+ if (!matchMode && pos > 0) {
+ ToNrEvent(pos); // [HGM] no autoplay if selected on position
+ } else
if (matchMode || appData.timeDelay == 0) {
ToEndEvent();
- gameMode = EditGame;
- ModeHighlight();
} else if (appData.timeDelay > 0) {
AutoPlayGameLoop();
}
- if (appData.debugMode)
+ if (appData.debugMode)
fprintf(debugFP, "LoadGame(): on exit, gameMode %d\n", gameMode);
loadFlag = 0; /* [HGM] true game starts */
/* Support for LoadNextPosition, LoadPreviousPosition, ReloadSamePosition */
int
-ReloadPosition(offset)
- int offset;
+ReloadPosition (int offset)
{
int positionNumber = lastLoadPositionNumber + offset;
if (lastLoadPositionFP == NULL) {
/* Load the nth position from the given file */
int
-LoadPositionFromFile(filename, n, title)
- char *filename;
- int n;
- char *title;
+LoadPositionFromFile (char *filename, int n, char *title)
{
FILE *f;
char buf[MSG_SIZ];
/* Load the nth position from the given open file, and close it */
int
-LoadPosition(f, positionNumber, title)
- FILE *f;
- int positionNumber;
- char *title;
+LoadPosition (FILE *f, int positionNumber, char *title)
{
char *p, line[MSG_SIZ];
Board initial_position;
int i, j, fenMode, pn;
-
+
if (gameMode == Training )
SetTrainingModeOff();
if (positionNumber == 0) positionNumber = 1;
lastLoadPositionFP = f;
lastLoadPositionNumber = positionNumber;
- strcpy(lastLoadPositionTitle, title);
- if (first.pr == NoProc) {
+ safeStrCpy(lastLoadPositionTitle, title, sizeof(lastLoadPositionTitle)/sizeof(lastLoadPositionTitle[0]));
+ if (first.pr == NoProc && !appData.noChessProgram) {
StartChessProgram(&first);
InitChessProgram(&first, FALSE);
- }
+ }
pn = positionNumber;
if (positionNumber < 0) {
/* Negative position number means to seek to that byte offset */
DisplayError(_("Position not found in file"), 0);
return FALSE;
}
- // [HGM] FEN can begin with digit, any piece letter valid in this variant, or a + for Shogi promoted pieces
- fenMode = line[0] >= '0' && line[0] <= '9' || line[0] == '+' || CharToPiece(line[0]) != EmptySquare;
+ // [HGM] FEN can begin with digit, any piece letter valid in this variant, or a + for Shogi promoted pieces (or * for blackout)
+ fenMode = line[0] >= '0' && line[0] <= '9' || line[0] == '+' || line[0] == '*' || CharToPiece(line[0]) != EmptySquare;
if (pn >= 2) {
if (fenMode || line[0] == '#') pn--;
}
if (fenMode) {
- if (!ParseFEN(initial_position, &blackPlaysFirst, line)) {
+ char *p;
+ if (!ParseFEN(initial_position, &blackPlaysFirst, line, TRUE)) {
DisplayError(_("Bad FEN position in file"), 0);
return FALSE;
}
+ if((p = strstr(line, ";")) && (p = strstr(p+1, "bm "))) { // EPD with best move
+ sscanf(p+3, "%s", bestMove);
+ } else *bestMove = NULLCHAR;
} else {
(void) fgets(line, MSG_SIZ, f);
(void) fgets(line, MSG_SIZ, f);
-
+
for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
(void) fgets(line, MSG_SIZ, f);
for (p = line, j = BOARD_LEFT; j < BOARD_RGHT; p++) {
initial_position[i][j++] = CharToPiece(*p);
}
}
-
+
blackPlaysFirst = FALSE;
if (!feof(f)) {
(void) fgets(line, MSG_SIZ, f);
}
}
startedFromSetupPosition = TRUE;
-
- SendToProgram("force\n", &first);
+
CopyBoard(boards[0], initial_position);
if (blackPlaysFirst) {
currentMove = forwardMostMove = backwardMostMove = 1;
- strcpy(moveList[0], "");
- strcpy(parseList[0], "");
+ safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
+ safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
CopyBoard(boards[1], initial_position);
DisplayMessage("", _("Black to play"));
} else {
currentMove = forwardMostMove = backwardMostMove = 0;
DisplayMessage("", _("White to play"));
}
- /* [HGM] copy FEN attributes as well */
- { int i;
- initialRulePlies = FENrulePlies;
- epStatus[forwardMostMove] = FENepStatus;
- for( i=0; i< nrCastlingRights; i++ )
- castlingRights[forwardMostMove][i] = FENcastlingRights[i];
- }
- SendBoard(&first, forwardMostMove);
+ initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
+ if(first.pr != NoProc) { // [HGM] in tourney-mode a position can be loaded before the chess engine is installed
+ SendToProgram("force\n", &first);
+ SendBoard(&first, forwardMostMove);
+ }
if (appData.debugMode) {
int i, j;
- for(i=0;i<2;i++){for(j=0;j<6;j++)fprintf(debugFP, " %d", castlingRights[i][j]);fprintf(debugFP,"\n");}
+ for(i=0;i<2;i++){for(j=0;j<6;j++)fprintf(debugFP, " %d", boards[i][CASTLING][j]);fprintf(debugFP,"\n");}
for(j=0;j<6;j++)fprintf(debugFP, " %d", initialRights[j]);fprintf(debugFP,"\n");
fprintf(debugFP, "Load Position\n");
}
if (positionNumber > 1) {
- sprintf(line, "%s %d", title, positionNumber);
+ snprintf(line, MSG_SIZ, "%s %d", title, positionNumber);
DisplayTitle(line);
} else {
DisplayTitle(title);
timeRemaining[0][1] = whiteTimeRemaining;
timeRemaining[1][1] = blackTimeRemaining;
DrawPosition(FALSE, boards[currentMove]);
-
+
return TRUE;
}
void
-CopyPlayerNameIntoFileName(dest, src)
- char **dest, *src;
+CopyPlayerNameIntoFileName (char **dest, char *src)
{
while (*src != NULLCHAR && *src != ',') {
if (*src == ' ') {
}
}
-char *DefaultFileName(ext)
- char *ext;
+char *
+DefaultFileName (char *ext)
{
static char def[MSG_SIZ];
char *p;
*p++ = '-';
CopyPlayerNameIntoFileName(&p, gameInfo.black);
*p++ = '.';
- strcpy(p, ext);
+ safeStrCpy(p, ext, MSG_SIZ-2-strlen(gameInfo.white)-strlen(gameInfo.black));
} else {
def[0] = NULLCHAR;
}
/* Save the current game to the given file */
int
-SaveGameToFile(filename, append)
- char *filename;
- int append;
+SaveGameToFile (char *filename, int append)
{
FILE *f;
char buf[MSG_SIZ];
+ int result, i, t,tot=0;
if (strcmp(filename, "-") == 0) {
return SaveGame(stdout, 0, NULL);
} else {
- f = fopen(filename, append ? "a" : "w");
+ for(i=0; i<10; i++) { // upto 10 tries
+ f = fopen(filename, append ? "a" : "w");
+ if(f && i) fprintf(f, "[Delay \"%d retries, %d msec\"]\n",i,tot);
+ if(f || errno != 13) break;
+ DoSleep(t = 5 + random()%11); // wait 5-15 msec
+ tot += t;
+ }
if (f == NULL) {
snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
DisplayError(buf, errno);
return FALSE;
} else {
- return SaveGame(f, 0, NULL);
+ safeStrCpy(buf, lastMsg, MSG_SIZ);
+ DisplayMessage(_("Waiting for access to save file"), "");
+ flock(fileno(f), LOCK_EX); // [HGM] lock: lock file while we are writing
+ DisplayMessage(_("Saving game"), "");
+ if(lseek(fileno(f), 0, SEEK_END) == -1) DisplayError(_("Bad Seek"), errno); // better safe than sorry...
+ result = SaveGame(f, 0, NULL);
+ DisplayMessage(buf, "");
+ return result;
}
}
}
char *
-SavePart(str)
- char *str;
+SavePart (char *str)
{
static char buf[MSG_SIZ];
char *p;
-
+
p = strchr(str, ' ');
if (p == NULL) return str;
strncpy(buf, str, p - str);
#define PGN_SIDE_WHITE 0
#define PGN_SIDE_BLACK 1
-/* [AS] */
-static int FindFirstMoveOutOfBook( int side )
+static int
+FindFirstMoveOutOfBook (int side)
{
int result = -1;
return result;
}
-/* [AS] */
-void GetOutOfBookInfo( char * buf )
+void
+GetOutOfBookInfo (char * buf)
{
int oob[2];
int i;
}
sprintf( buf+strlen(buf), "%d%s. ", (idx - offset)/2 + 1, idx & 1 ? ".." : "" );
- sprintf( buf+strlen(buf), "%s%.2f",
+ sprintf( buf+strlen(buf), "%s%.2f",
pvInfoList[idx].score >= 0 ? "+" : "",
pvInfoList[idx].score / 100.0 );
}
}
}
-/* Save game in PGN style and close the file */
-int
-SaveGamePGN(f)
- FILE *f;
+/* Save game in PGN style */
+static void
+SaveGamePGN2 (FILE *f)
{
int i, offset, linelen, newblock;
- time_t tm;
// char *movetext;
char numtext[32];
int movelen, numlen, blank;
char move_buffer[100]; /* [AS] Buffer for move+PV info */
offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
-
- tm = time((time_t *) NULL);
-
+
PrintPGNTags(f, &gameInfo);
-
+
+ if(appData.numberTag && matchMode) fprintf(f, "[Number \"%d\"]\n", nextGame+1); // [HGM] number tag
+
if (backwardMostMove > 0 || startedFromSetupPosition) {
- char *fen = PositionToFEN(backwardMostMove, NULL);
+ char *fen = PositionToFEN(backwardMostMove, NULL, 1);
fprintf(f, "[FEN \"%s\"]\n[SetUp \"1\"]\n", fen);
fprintf(f, "\n{--------------\n");
PrintPosition(f, backwardMostMove);
GetOutOfBookInfo( buf );
if( buf[0] != '\0' ) {
- fprintf( f, "[%s \"%s\"]\n", PGN_OUT_OF_BOOK, buf );
+ fprintf( f, "[%s \"%s\"]\n", PGN_OUT_OF_BOOK, buf );
}
}
/* Print comments preceding this move */
if (commentList[i] != NULL) {
if (linelen > 0) fprintf(f, "\n");
- fprintf(f, "{\n%s}\n", commentList[i]);
+ fprintf(f, "%s", commentList[i]);
linelen = 0;
newblock = TRUE;
}
/* Format move number */
- if ((i % 2) == 0) {
- sprintf(numtext, "%d.", (i - offset)/2 + 1);
- } else {
- if (newblock) {
- sprintf(numtext, "%d...", (i - offset)/2 + 1);
- } else {
- numtext[0] = NULLCHAR;
- }
- }
+ if ((i % 2) == 0)
+ snprintf(numtext, sizeof(numtext)/sizeof(numtext[0]),"%d.", (i - offset)/2 + 1);
+ else
+ if (newblock)
+ snprintf(numtext, sizeof(numtext)/sizeof(numtext[0]), "%d...", (i - offset)/2 + 1);
+ else
+ numtext[0] = NULLCHAR;
+
numlen = strlen(numtext);
newblock = FALSE;
fprintf(f, " ");
linelen++;
}
- fprintf(f, numtext);
+ fprintf(f, "%s", numtext);
linelen += numlen;
/* Get move */
- strcpy(move_buffer, SavePart(parseList[i])); // [HGM] pgn: print move via buffer, so it can be edited
+ safeStrCpy(move_buffer, SavePart(parseList[i]), sizeof(move_buffer)/sizeof(move_buffer[0])); // [HGM] pgn: print move via buffer, so it can be edited
movelen = strlen(move_buffer); /* [HGM] pgn: line-break point before move */
/* Print move */
fprintf(f, " ");
linelen++;
}
- fprintf(f, move_buffer);
+ fprintf(f, "%s", move_buffer);
linelen += movelen;
/* [AS] Add PV info if present */
if( i >= 0 && appData.saveExtendedInfoInPGN && pvInfoList[i].depth > 0 ) {
/* [HGM] add time */
- char buf[MSG_SIZ]; int seconds = 0;
+ char buf[MSG_SIZ]; int seconds;
- if(i >= backwardMostMove) {
- if(WhiteOnMove(i))
- seconds = timeRemaining[0][i] - timeRemaining[0][i+1]
- + GetTimeQuota(i/2) / (1000*WhitePlayer()->timeOdds);
- else
- seconds = timeRemaining[1][i] - timeRemaining[1][i+1]
- + GetTimeQuota(i/2) / (1000*WhitePlayer()->other->timeOdds);
- }
- seconds = (seconds+50)/100; // deci-seconds, rounded to nearest
+ seconds = (pvInfoList[i].time+5)/10; // deci-seconds, rounded to nearest
- if( seconds <= 0) buf[0] = 0; else
- if( seconds < 30 ) sprintf(buf, " %3.1f%c", seconds/10., 0); else {
- seconds = (seconds + 4)/10; // round to full seconds
- if( seconds < 60 ) sprintf(buf, " %d%c", seconds, 0); else
- sprintf(buf, " %d:%02d%c", seconds/60, seconds%60, 0);
- }
+ if( seconds <= 0)
+ buf[0] = 0;
+ else
+ if( seconds < 30 )
+ snprintf(buf, MSG_SIZ, " %3.1f%c", seconds/10., 0);
+ else
+ {
+ seconds = (seconds + 4)/10; // round to full seconds
+ if( seconds < 60 )
+ snprintf(buf, MSG_SIZ, " %d%c", seconds, 0);
+ else
+ snprintf(buf, MSG_SIZ, " %d:%02d%c", seconds/60, seconds%60, 0);
+ }
- sprintf( move_buffer, "{%s%.2f/%d%s}",
- pvInfoList[i].score >= 0 ? "+" : "",
- pvInfoList[i].score / 100.0,
- pvInfoList[i].depth,
- buf );
+ snprintf( move_buffer, sizeof(move_buffer)/sizeof(move_buffer[0]),"{%s%.2f/%d%s}",
+ pvInfoList[i].score >= 0 ? "+" : "",
+ pvInfoList[i].score / 100.0,
+ pvInfoList[i].depth,
+ buf );
movelen = strlen(move_buffer); /* [HGM] pgn: line-break point after move */
fprintf(f, " ");
linelen++;
}
- fprintf(f, move_buffer);
+ fprintf(f, "%s", move_buffer);
linelen += movelen;
}
i++;
}
-
+
/* Start a new line */
if (linelen > 0) fprintf(f, "\n");
/* Print comments after last move */
if (commentList[i] != NULL) {
- fprintf(f, "{\n%s}\n", commentList[i]);
+ fprintf(f, "%s\n", commentList[i]);
}
/* Print result */
if (gameInfo.resultDetails != NULL &&
gameInfo.resultDetails[0] != NULLCHAR) {
- fprintf(f, "{%s} %s\n\n", gameInfo.resultDetails,
- PGNResult(gameInfo.result));
+ char buf[MSG_SIZ], *p = gameInfo.resultDetails;
+ if(gameInfo.result == GameUnfinished && appData.clockMode &&
+ (gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack || gameMode == TwoMachinesPlay)) // [HGM] adjourn: save clock settings
+ snprintf(buf, MSG_SIZ, "%s (Clocks: %ld, %ld)", p, whiteTimeRemaining/1000, blackTimeRemaining/1000), p = buf;
+ fprintf(f, "{%s} %s\n\n", p, PGNResult(gameInfo.result));
} else {
fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
}
+}
+/* Save game in PGN style and close the file */
+int
+SaveGamePGN (FILE *f)
+{
+ SaveGamePGN2(f);
fclose(f);
lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
return TRUE;
/* Save game in old style and close the file */
int
-SaveGameOldStyle(f)
- FILE *f;
+SaveGameOldStyle (FILE *f)
{
int i, offset;
time_t tm;
-
+
tm = time((time_t *) NULL);
-
+
fprintf(f, "# %s game file -- %s", programName, ctime(&tm));
PrintOpponents(f);
-
+
if (backwardMostMove > 0 || startedFromSetupPosition) {
fprintf(f, "\n[--------------\n");
PrintPosition(f, backwardMostMove);
i++;
}
}
-
+
if (commentList[i] != NULL) {
fprintf(f, "[%s]\n", commentList[i]);
}
/* Save the current game to open file f and close the file */
int
-SaveGame(f, dummy, dummy2)
- FILE *f;
- int dummy;
- char *dummy2;
+SaveGame (FILE *f, int dummy, char *dummy2)
{
- if (gameMode == EditPosition) EditPositionDone();
+ if (gameMode == EditPosition) EditPositionDone(TRUE);
lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
if (appData.oldSaveStyle)
return SaveGameOldStyle(f);
/* Save the current position to the given file */
int
-SavePositionToFile(filename)
- char *filename;
+SavePositionToFile (char *filename)
{
FILE *f;
char buf[MSG_SIZ];
DisplayError(buf, errno);
return FALSE;
} else {
+ safeStrCpy(buf, lastMsg, MSG_SIZ);
+ DisplayMessage(_("Waiting for access to save file"), "");
+ flock(fileno(f), LOCK_EX); // [HGM] lock
+ DisplayMessage(_("Saving position"), "");
+ lseek(fileno(f), 0, SEEK_END); // better safe than sorry...
SavePosition(f, 0, NULL);
+ DisplayMessage(buf, "");
return TRUE;
}
}
/* Save the current position to the given open file and close the file */
int
-SavePosition(f, dummy, dummy2)
- FILE *f;
- int dummy;
- char *dummy2;
+SavePosition (FILE *f, int dummy, char *dummy2)
{
time_t tm;
char *fen;
-
+
+ if (gameMode == EditPosition) EditPositionDone(TRUE);
if (appData.oldSaveStyle) {
tm = time((time_t *) NULL);
-
+
fprintf(f, "# %s position file -- %s", programName, ctime(&tm));
PrintOpponents(f);
fprintf(f, "[--------------\n");
PrintPosition(f, currentMove);
fprintf(f, "--------------]\n");
} else {
- fen = PositionToFEN(currentMove, NULL);
+ fen = PositionToFEN(currentMove, NULL, 1);
fprintf(f, "%s\n", fen);
free(fen);
}
}
void
-ReloadCmailMsgEvent(unregister)
- int unregister;
+ReloadCmailMsgEvent (int unregister)
{
#if !WIN32
static char *inFilename = NULL;
int i;
struct stat inbuf, outbuf;
int status;
-
+
/* Any registered moves are unregistered if unregister is set, */
/* i.e. invoked by the signal handler */
if (unregister) {
outFilename = (char *) malloc(strlen(appData.cmailGameName) + 5);
sprintf(outFilename, "%s.out", appData.cmailGameName);
}
-
+
status = stat(outFilename, &outbuf);
if (status < 0) {
cmailMailedMove = FALSE;
status = stat(inFilename, &inbuf);
cmailMailedMove = (inbuf.st_mtime < outbuf.st_mtime);
}
-
+
/* LoadGameFromFile(CMAIL_MAX_GAMES) with cmailMsgLoaded == TRUE
counts the games, notes how each one terminated, etc.
-
+
It would be nice to remove this kludge and instead gather all
the information while building the game list. (And to keep it
in the game list nodes instead of having a bunch of fixed-size
*/
cmailMsgLoaded = TRUE;
LoadGameFromFile(inFilename, CMAIL_MAX_GAMES, "", FALSE);
-
+
/* Load first game in the file or popup game menu */
LoadGameFromFile(inFilename, 0, appData.cmailGameName, TRUE);
}
int
-RegisterMove()
+RegisterMove ()
{
FILE *f;
char string[MSG_SIZ];
cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
nCmailMovesRegistered --;
- if (cmailCommentList[lastLoadGameNumber - 1] != NULL)
+ if (cmailCommentList[lastLoadGameNumber - 1] != NULL)
{
free(cmailCommentList[lastLoadGameNumber - 1]);
cmailCommentList[lastLoadGameNumber - 1] = NULL;
cmailCommentList[lastLoadGameNumber - 1]
= StrSave(commentList[currentMove]);
}
- strcpy(cmailMove[lastLoadGameNumber - 1], moveList[currentMove - 1]);
+ safeStrCpy(cmailMove[lastLoadGameNumber - 1], moveList[currentMove - 1], sizeof(cmailMove[lastLoadGameNumber - 1])/sizeof(cmailMove[lastLoadGameNumber - 1][0]));
if (appData.debugMode)
fprintf(debugFP, "Saving %s for game %d\n",
cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
- sprintf(string,
- "%s.game.out.%d", appData.cmailGameName, lastLoadGameNumber);
-
+ snprintf(string, MSG_SIZ, "%s.game.out.%d", appData.cmailGameName, lastLoadGameNumber);
+
f = fopen(string, "w");
if (appData.oldSaveStyle) {
SaveGameOldStyle(f); /* also closes the file */
-
- sprintf(string, "%s.pos.out", appData.cmailGameName);
+
+ snprintf(string, MSG_SIZ, "%s.pos.out", appData.cmailGameName);
f = fopen(string, "w");
SavePosition(f, 0, NULL); /* also closes the file */
} else {
fprintf(f, "{--------------\n");
PrintPosition(f, currentMove);
fprintf(f, "--------------}\n\n");
-
+
SaveGame(f, 0, NULL); /* also closes the file*/
}
-
+
cmailMoveRegistered[lastLoadGameNumber - 1] = TRUE;
nCmailMovesRegistered ++;
} else if (nCmailGames == 1) {
}
void
-MailMoveEvent()
+MailMoveEvent ()
{
#if !WIN32
static char *partCommandString = "cmail -xv%s -remail -game %s 2>&1";
#if CMAIL_PROHIBIT_REMAIL
if (cmailMailedMove) {
- sprintf(msg, _("You have already mailed a move.\nWait until a move arrives from your opponent.\nTo resend the same move, type\n\"cmail -remail -game %s\"\non the command line."), appData.cmailGameName);
+ 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);
DisplayError(msg, 0);
return;
}
#endif
if (! (cmailMailedMove || RegisterMove())) return;
-
+
if ( cmailMailedMove
|| (nCmailMovesRegistered + nCmailResults == nCmailGames)) {
- sprintf(string, partCommandString,
- appData.debugMode ? " -v" : "", appData.cmailGameName);
+ snprintf(string, MSG_SIZ, partCommandString,
+ appData.debugMode ? " -v" : "", appData.cmailGameName);
commandOutput = popen(string, "r");
if (commandOutput == NULL) {
if ( archived
&& ( (arcDir = (char *) getenv("CMAIL_ARCDIR"))
!= NULL)) {
- sprintf(buffer, "%s/%s.%s.archive",
- arcDir,
- appData.cmailGameName,
- gameInfo.date);
+ snprintf(buffer, MSG_SIZ, "%s/%s.%s.archive",
+ arcDir,
+ appData.cmailGameName,
+ gameInfo.date);
LoadGameFromFile(buffer, 1, buffer, FALSE);
cmailMsgLoaded = FALSE;
}
}
char *
-CmailMsg()
+CmailMsg ()
{
#if WIN32
return NULL;
char number[5];
char string[MSG_SIZ]; /* Space for game-list */
int i;
-
+
if (!cmailMsgLoaded) return "";
if (cmailMailedMove) {
- sprintf(cmailMsg, _("Waiting for reply from opponent\n"));
+ snprintf(cmailMsg, MSG_SIZ, _("Waiting for reply from opponent\n"));
} else {
/* Create a list of games left */
- sprintf(string, "[");
+ snprintf(string, MSG_SIZ, "[");
for (i = 0; i < nCmailGames; i ++) {
if (! ( cmailMoveRegistered[i]
|| (cmailResult[i] == CMAIL_OLD_RESULT))) {
if (prependComma) {
- sprintf(number, ",%d", i + 1);
+ snprintf(number, sizeof(number)/sizeof(number[0]), ",%d", i + 1);
} else {
- sprintf(number, "%d", i + 1);
+ snprintf(number, sizeof(number)/sizeof(number[0]), "%d", i + 1);
prependComma = 1;
}
-
+
strcat(string, number);
}
}
if (nCmailMovesRegistered + nCmailResults == 0) {
switch (nCmailGames) {
case 1:
- sprintf(cmailMsg,
- _("Still need to make move for game\n"));
+ snprintf(cmailMsg, MSG_SIZ, _("Still need to make move for game\n"));
break;
-
+
case 2:
- sprintf(cmailMsg,
- _("Still need to make moves for both games\n"));
+ snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for both games\n"));
break;
-
+
default:
- sprintf(cmailMsg,
- _("Still need to make moves for all %d games\n"),
- nCmailGames);
+ snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for all %d games\n"),
+ nCmailGames);
break;
}
} else {
switch (nCmailGames - nCmailMovesRegistered - nCmailResults) {
case 1:
- sprintf(cmailMsg,
- _("Still need to make a move for game %s\n"),
- string);
+ snprintf(cmailMsg, MSG_SIZ, _("Still need to make a move for game %s\n"),
+ string);
break;
-
+
case 0:
if (nCmailResults == nCmailGames) {
- sprintf(cmailMsg, _("No unfinished games\n"));
+ snprintf(cmailMsg, MSG_SIZ, _("No unfinished games\n"));
} else {
- sprintf(cmailMsg, _("Ready to send mail\n"));
+ snprintf(cmailMsg, MSG_SIZ, _("Ready to send mail\n"));
}
break;
-
+
default:
- sprintf(cmailMsg,
- _("Still need to make moves for games %s\n"),
- string);
+ snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for games %s\n"),
+ string);
}
}
}
}
void
-ResetGameEvent()
+ResetGameEvent ()
{
if (gameMode == Training)
SetTrainingModeOff();
}
void
-ExitEvent(status)
- int status;
+ExitEvent (int status)
{
exiting++;
if (exiting > 2) {
return;
}
+ if (appData.icsActive) printf("\n"); // [HGM] end on new line after closing XBoard
if (appData.icsActive && appData.colorize) Colorize(ColorNone, FALSE);
if (telnetISR != NULL) {
/* Kill off chess programs */
if (first.pr != NoProc) {
ExitAnalyzeMode();
-
+
DoSleep( appData.delayBeforeQuit );
SendToProgram("quit\n", &first);
- DoSleep( appData.delayAfterQuit );
- DestroyChildProcess(first.pr, 10 /* [AS] first.useSigterm */ );
+ DestroyChildProcess(first.pr, 4 + first.useSigterm /* [AS] first.useSigterm */ );
}
if (second.pr != NoProc) {
DoSleep( appData.delayBeforeQuit );
SendToProgram("quit\n", &second);
- DoSleep( appData.delayAfterQuit );
- DestroyChildProcess(second.pr, 10 /* [AS] second.useSigterm */ );
+ DestroyChildProcess(second.pr, 4 + second.useSigterm /* [AS] second.useSigterm */ );
}
if (first.isr != NULL) {
RemoveInputSource(first.isr);
RemoveInputSource(second.isr);
}
+ if (pairing.pr != NoProc) SendToProgram("quit\n", &pairing);
+ if (pairing.isr != NULL) RemoveInputSource(pairing.isr);
+
ShutDownFrontEnd();
exit(status);
}
void
-PauseEvent()
+PauseEngine (ChessProgramState *cps)
+{
+ SendToProgram("pause\n", cps);
+ cps->pause = 2;
+}
+
+void
+UnPauseEngine (ChessProgramState *cps)
+{
+ SendToProgram("resume\n", cps);
+ cps->pause = 1;
+}
+
+void
+PauseEvent ()
{
if (appData.debugMode)
fprintf(debugFP, "PauseEvent(): pausing %d\n", pausing);
if (pausing) {
pausing = FALSE;
ModeHighlight();
+ if(stalledEngine) { // [HGM] pause: resume game by releasing withheld move
+ StartClocks();
+ if(gameMode == TwoMachinesPlay) { // we might have to make the opponent resume pondering
+ if(stalledEngine->other->pause == 2) UnPauseEngine(stalledEngine->other);
+ else if(appData.ponderNextMove) SendToProgram("hard\n", stalledEngine->other);
+ }
+ if(appData.ponderNextMove) SendToProgram("hard\n", stalledEngine);
+ HandleMachineMove(stashedInputMove, stalledEngine);
+ stalledEngine = NULL;
+ return;
+ }
if (gameMode == MachinePlaysWhite ||
- gameMode == MachinePlaysBlack) {
+ gameMode == TwoMachinesPlay ||
+ gameMode == MachinePlaysBlack) { // the thinking engine must have used pause mode, or it would have been stalledEngine
+ if(first.pause) UnPauseEngine(&first);
+ else if(appData.ponderNextMove) SendToProgram("hard\n", &first);
+ if(second.pause) UnPauseEngine(&second);
+ else if(gameMode == TwoMachinesPlay && appData.ponderNextMove) SendToProgram("hard\n", &second);
StartClocks();
} else {
DisplayBothClocks();
}
if (gameMode == PlayFromGameFile) {
- if (appData.timeDelay >= 0)
+ if (appData.timeDelay >= 0)
AutoPlayGameLoop();
} else if (gameMode == IcsExamining && pauseExamInvalid) {
Reset(FALSE, TRUE);
SendToICS(ics_prefix);
SendToICS("refresh\n");
- } else if (currentMove < forwardMostMove) {
+ } else if (currentMove < forwardMostMove && gameMode != AnalyzeMode) {
ForwardInner(forwardMostMove);
}
pauseExamInvalid = FALSE;
case TwoMachinesPlay:
if (forwardMostMove == 0)
return; /* don't pause if no one has moved */
- if ((gameMode == MachinePlaysWhite &&
- !WhiteOnMove(forwardMostMove)) ||
- (gameMode == MachinePlaysBlack &&
- WhiteOnMove(forwardMostMove))) {
+ if(gameMode == TwoMachinesPlay) { // [HGM] pause: stop clocks if engine can be paused immediately
+ ChessProgramState *onMove = (WhiteOnMove(forwardMostMove) == (first.twoMachinesColor[0] == 'w') ? &first : &second);
+ if(onMove->pause) { // thinking engine can be paused
+ PauseEngine(onMove); // do it
+ if(onMove->other->pause) // pondering opponent can always be paused immediately
+ PauseEngine(onMove->other);
+ else
+ SendToProgram("easy\n", onMove->other);
+ StopClocks();
+ } else if(appData.ponderNextMove) SendToProgram("easy\n", onMove); // pre-emptively bring out of ponder
+ } else if(gameMode == (WhiteOnMove(forwardMostMove) ? MachinePlaysWhite : MachinePlaysBlack)) { // engine on move
+ if(first.pause) {
+ PauseEngine(&first);
+ StopClocks();
+ } else if(appData.ponderNextMove) SendToProgram("easy\n", &first); // pre-emptively bring out of ponder
+ } else { // human on move, pause pondering by either method
+ if(first.pause)
+ PauseEngine(&first);
+ else if(appData.ponderNextMove)
+ SendToProgram("easy\n", &first);
StopClocks();
}
+ // if no immediate pausing is possible, wait for engine to move, and stop clocks then
+ case AnalyzeMode:
pausing = TRUE;
ModeHighlight();
break;
}
void
-EditCommentEvent()
+EditCommentEvent ()
{
char title[MSG_SIZ];
if (currentMove < 1 || parseList[currentMove - 1][0] == NULLCHAR) {
- strcpy(title, _("Edit comment"));
+ safeStrCpy(title, _("Edit comment"), sizeof(title)/sizeof(title[0]));
} else {
- sprintf(title, _("Edit comment on %d.%s%s"), (currentMove - 1) / 2 + 1,
- WhiteOnMove(currentMove - 1) ? " " : ".. ",
- parseList[currentMove - 1]);
+ snprintf(title, MSG_SIZ, _("Edit comment on %d.%s%s"), (currentMove - 1) / 2 + 1,
+ WhiteOnMove(currentMove - 1) ? " " : ".. ",
+ parseList[currentMove - 1]);
}
EditCommentPopUp(currentMove, title, commentList[currentMove]);
void
-EditTagsEvent()
+EditTagsEvent ()
{
char *tags = PGNTags(&gameInfo);
- EditTagsPopUp(tags);
+ bookUp = FALSE;
+ EditTagsPopUp(tags, NULL);
free(tags);
}
void
-AnalyzeModeEvent()
+ToggleSecond ()
+{
+ if(second.analyzing) {
+ SendToProgram("exit\n", &second);
+ second.analyzing = FALSE;
+ } else {
+ if (second.pr == NoProc) StartChessProgram(&second);
+ InitChessProgram(&second, FALSE);
+ FeedMovesToProgram(&second, currentMove);
+
+ SendToProgram("analyze\n", &second);
+ second.analyzing = TRUE;
+ }
+}
+
+/* Toggle ShowThinking */
+void
+ToggleShowThinking()
+{
+ appData.showThinking = !appData.showThinking;
+ ShowThinkingEvent();
+}
+
+int
+AnalyzeModeEvent ()
{
+ char buf[MSG_SIZ];
+
+ if (!first.analysisSupport) {
+ snprintf(buf, sizeof(buf), _("%s does not support analysis"), first.tidy);
+ DisplayError(buf, 0);
+ return 0;
+ }
+ /* [DM] icsEngineAnalyze [HGM] This is horrible code; reverse the gameMode and isEngineAnalyze tests! */
+ if (appData.icsActive) {
+ if (gameMode != IcsObserving) {
+ snprintf(buf, MSG_SIZ, _("You are not observing a game"));
+ DisplayError(buf, 0);
+ /* secure check */
+ if (appData.icsEngineAnalyze) {
+ if (appData.debugMode)
+ fprintf(debugFP, "Found unexpected active ICS engine analyze \n");
+ ExitAnalyzeMode();
+ ModeHighlight();
+ }
+ return 0;
+ }
+ /* if enable, user wants to disable icsEngineAnalyze */
+ if (appData.icsEngineAnalyze) {
+ ExitAnalyzeMode();
+ ModeHighlight();
+ return 0;
+ }
+ appData.icsEngineAnalyze = TRUE;
+ if (appData.debugMode)
+ fprintf(debugFP, "ICS engine analyze starting... \n");
+ }
+
+ if (gameMode == AnalyzeMode) { ToggleSecond(); return 0; }
if (appData.noChessProgram || gameMode == AnalyzeMode)
- return;
+ return 0;
if (gameMode != AnalyzeFile) {
if (!appData.icsEngineAnalyze) {
EditGameEvent();
- if (gameMode != EditGame) return;
+ if (gameMode != EditGame) return 0;
}
+ if (!appData.showThinking) ToggleShowThinking();
ResurrectChessProgram();
SendToProgram("analyze\n", &first);
first.analyzing = TRUE;
/*first.maybeThinking = TRUE;*/
first.maybeThinking = FALSE; /* avoid killing GNU Chess */
- AnalysisPopUp(_("Analysis"),
- _("Starting analysis mode...\nIf this message stays up, your chess program does not support analysis."));
+ EngineOutputPopUp();
+ }
+ if (!appData.icsEngineAnalyze) {
+ gameMode = AnalyzeMode;
+ ClearEngineOutputPane(0); // [TK] exclude: to print exclusion/multipv header
}
- if (!appData.icsEngineAnalyze) gameMode = AnalyzeMode;
pausing = FALSE;
ModeHighlight();
SetGameInfo();
StartAnalysisClock();
GetTimeMark(&lastNodeCountTime);
lastNodeCount = 0;
+ return 1;
}
void
-AnalyzeFileEvent()
+AnalyzeFileEvent ()
{
if (appData.noChessProgram || gameMode == AnalyzeFile)
return;
+ if (!first.analysisSupport) {
+ char buf[MSG_SIZ];
+ snprintf(buf, sizeof(buf), _("%s does not support analysis"), first.tidy);
+ DisplayError(buf, 0);
+ return;
+ }
+
if (gameMode != AnalyzeMode) {
+ keepInfo = 1; // mere annotating should not alter PGN tags
EditGameEvent();
+ keepInfo = 0;
if (gameMode != EditGame) return;
+ if (!appData.showThinking) ToggleShowThinking();
ResurrectChessProgram();
SendToProgram("analyze\n", &first);
first.analyzing = TRUE;
/*first.maybeThinking = TRUE;*/
first.maybeThinking = FALSE; /* avoid killing GNU Chess */
- AnalysisPopUp(_("Analysis"),
- _("Starting analysis mode...\nIf this message stays up, your chess program does not support analysis."));
+ EngineOutputPopUp();
}
gameMode = AnalyzeFile;
pausing = FALSE;
ModeHighlight();
- SetGameInfo();
StartAnalysisClock();
GetTimeMark(&lastNodeCountTime);
lastNodeCount = 0;
+ if(appData.timeDelay > 0) StartLoadGameTimer((long)(1000.0f * appData.timeDelay));
+ AnalysisPeriodicEvent(1);
}
void
-MachineWhiteEvent()
+MachineWhiteEvent ()
{
char buf[MSG_SIZ];
char *bookHit = NULL;
return;
- if (gameMode == PlayFromGameFile ||
- gameMode == TwoMachinesPlay ||
- gameMode == Training ||
- gameMode == AnalyzeMode ||
+ if (gameMode == PlayFromGameFile ||
+ gameMode == TwoMachinesPlay ||
+ gameMode == Training ||
+ gameMode == AnalyzeMode ||
gameMode == EndOfGame)
EditGameEvent();
- if (gameMode == EditPosition)
- EditPositionDone();
+ if (gameMode == EditPosition)
+ EditPositionDone(TRUE);
if (!WhiteOnMove(currentMove)) {
DisplayError(_("It is not White's turn"), 0);
return;
}
-
+
if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
ExitAnalyzeMode();
- if (gameMode == EditGame || gameMode == AnalyzeMode ||
+ if (gameMode == EditGame || gameMode == AnalyzeMode ||
gameMode == AnalyzeFile)
TruncateGame();
pausing = FALSE;
ModeHighlight();
SetGameInfo();
- sprintf(buf, "%s vs. %s", gameInfo.white, gameInfo.black);
+ snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
DisplayTitle(buf);
if (first.sendName) {
- sprintf(buf, "name %s\n", gameInfo.black);
+ snprintf(buf, MSG_SIZ, "name %s\n", gameInfo.black);
SendToProgram(buf, &first);
}
if (first.sendTime) {
if(bookHit) { // [HGM] book: simulate book reply
static char bookMove[MSG_SIZ]; // a bit generous?
- programStats.nodes = programStats.depth = programStats.time =
+ programStats.nodes = programStats.depth = programStats.time =
programStats.score = programStats.got_only_move = 0;
sprintf(programStats.movelist, "%s (xbook)", bookHit);
- strcpy(bookMove, "move ");
+ safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
strcat(bookMove, bookHit);
HandleMachineMove(bookMove, &first);
}
}
void
-MachineBlackEvent()
+MachineBlackEvent ()
{
- char buf[MSG_SIZ];
- char *bookHit = NULL;
+ char buf[MSG_SIZ];
+ char *bookHit = NULL;
if (appData.noChessProgram || (gameMode == MachinePlaysBlack))
return;
- if (gameMode == PlayFromGameFile ||
- gameMode == TwoMachinesPlay ||
- gameMode == Training ||
- gameMode == AnalyzeMode ||
+ if (gameMode == PlayFromGameFile ||
+ gameMode == TwoMachinesPlay ||
+ gameMode == Training ||
+ gameMode == AnalyzeMode ||
gameMode == EndOfGame)
EditGameEvent();
- if (gameMode == EditPosition)
- EditPositionDone();
+ if (gameMode == EditPosition)
+ EditPositionDone(TRUE);
if (WhiteOnMove(currentMove)) {
DisplayError(_("It is not Black's turn"), 0);
return;
}
-
+
if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
ExitAnalyzeMode();
- if (gameMode == EditGame || gameMode == AnalyzeMode ||
+ if (gameMode == EditGame || gameMode == AnalyzeMode ||
gameMode == AnalyzeFile)
TruncateGame();
pausing = FALSE;
ModeHighlight();
SetGameInfo();
- sprintf(buf, "%s vs. %s", gameInfo.white, gameInfo.black);
+ snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
DisplayTitle(buf);
if (first.sendName) {
- sprintf(buf, "name %s\n", gameInfo.white);
+ snprintf(buf, MSG_SIZ, "name %s\n", gameInfo.white);
SendToProgram(buf, &first);
}
if (first.sendTime) {
if(bookHit) { // [HGM] book: simulate book reply
static char bookMove[MSG_SIZ]; // a bit generous?
- programStats.nodes = programStats.depth = programStats.time =
+ programStats.nodes = programStats.depth = programStats.time =
programStats.score = programStats.got_only_move = 0;
sprintf(programStats.movelist, "%s (xbook)", bookHit);
- strcpy(bookMove, "move ");
+ safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
strcat(bookMove, bookHit);
HandleMachineMove(bookMove, &first);
}
void
-DisplayTwoMachinesTitle()
+DisplayTwoMachinesTitle ()
{
char buf[MSG_SIZ];
if (appData.matchGames > 0) {
+ if(appData.tourneyFile[0]) {
+ snprintf(buf, MSG_SIZ, "%s %s %s (%d/%d%s)",
+ gameInfo.white, _("vs."), gameInfo.black,
+ nextGame+1, appData.matchGames+1,
+ appData.tourneyType>0 ? "gt" : appData.tourneyType<0 ? "sw" : "rr");
+ } else
if (first.twoMachinesColor[0] == 'w') {
- sprintf(buf, "%s vs. %s (%d-%d-%d)",
- gameInfo.white, gameInfo.black,
- first.matchWins, second.matchWins,
- matchGame - 1 - (first.matchWins + second.matchWins));
+ snprintf(buf, MSG_SIZ, "%s %s %s (%d-%d-%d)",
+ gameInfo.white, _("vs."), gameInfo.black,
+ first.matchWins, second.matchWins,
+ matchGame - 1 - (first.matchWins + second.matchWins));
} else {
- sprintf(buf, "%s vs. %s (%d-%d-%d)",
- gameInfo.white, gameInfo.black,
- second.matchWins, first.matchWins,
- matchGame - 1 - (first.matchWins + second.matchWins));
+ snprintf(buf, MSG_SIZ, "%s %s %s (%d-%d-%d)",
+ gameInfo.white, _("vs."), gameInfo.black,
+ second.matchWins, first.matchWins,
+ matchGame - 1 - (first.matchWins + second.matchWins));
}
} else {
- sprintf(buf, "%s vs. %s", gameInfo.white, gameInfo.black);
+ snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
}
DisplayTitle(buf);
}
void
+SettingsMenuIfReady ()
+{
+ if (second.lastPing != second.lastPong) {
+ DisplayMessage("", _("Waiting for second chess program"));
+ ScheduleDelayedEvent(SettingsMenuIfReady, 10); // [HGM] fast: lowered from 1000
+ return;
+ }
+ ThawUI();
+ DisplayMessage("", "");
+ SettingsPopUp(&second);
+}
+
+int
+WaitForEngine (ChessProgramState *cps, DelayedEventCallback retry)
+{
+ char buf[MSG_SIZ];
+ if (cps->pr == NoProc) {
+ StartChessProgram(cps);
+ if (cps->protocolVersion == 1) {
+ retry();
+ ScheduleDelayedEvent(retry, 1); // Do this also through timeout to avoid recursive calling of 'retry'
+ } else {
+ /* kludge: allow timeout for initial "feature" command */
+ if(retry != TwoMachinesEventIfReady) FreezeUI();
+ snprintf(buf, MSG_SIZ, _("Starting %s chess program"), _(cps->which));
+ DisplayMessage("", buf);
+ ScheduleDelayedEvent(retry, FEATURE_TIMEOUT);
+ }
+ return 1;
+ }
+ return 0;
+}
+
+void
TwoMachinesEvent P((void))
{
int i;
char buf[MSG_SIZ];
ChessProgramState *onmove;
char *bookHit = NULL;
-
+ static int stalling = 0;
+ TimeMark now;
+ long wait;
+
if (appData.noChessProgram) return;
switch (gameMode) {
case MachinePlaysWhite:
case MachinePlaysBlack:
if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
- DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
+ DisplayError(_("Wait until your turn,\nor select 'Move Now'."), 0);
return;
}
/* fall through */
if (gameMode != EditGame) return;
break;
case EditPosition:
- EditPositionDone();
+ EditPositionDone(TRUE);
break;
case AnalyzeMode:
case AnalyzeFile:
break;
}
- forwardMostMove = currentMove;
- ResurrectChessProgram(); /* in case first program isn't running */
+// forwardMostMove = currentMove;
+ TruncateGame(); // [HGM] vari: MachineWhite and MachineBlack do this...
+ startingEngine = TRUE;
- if (second.pr == NULL) {
- StartChessProgram(&second);
- if (second.protocolVersion == 1) {
- TwoMachinesEventIfReady();
- } else {
- /* kludge: allow timeout for initial "feature" command */
- FreezeUI();
- DisplayMessage("", _("Starting second chess program"));
- ScheduleDelayedEvent(TwoMachinesEventIfReady, FEATURE_TIMEOUT);
- }
+ if(!ResurrectChessProgram()) return; /* in case first program isn't running (unbalances its ping due to InitChessProgram!) */
+
+ if(!first.initDone && GetDelayedEvent() == TwoMachinesEventIfReady) return; // [HGM] engine #1 still waiting for feature timeout
+ if(first.lastPing != first.lastPong) { // [HGM] wait till we are sure first engine has set up position
+ ScheduleDelayedEvent(TwoMachinesEventIfReady, 10);
+ return;
+ }
+ if(WaitForEngine(&second, TwoMachinesEventIfReady)) return; // (if needed:) started up second engine, so wait for features
+
+ if(!SupportedVariant(second.variants, gameInfo.variant, gameInfo.boardWidth,
+ gameInfo.boardHeight, gameInfo.holdingsSize, second.protocolVersion, second.tidy)) {
+ startingEngine = matchMode = FALSE;
+ DisplayError("second engine does not play this", 0);
+ gameMode = TwoMachinesPlay; ModeHighlight(); // Needed to make sure menu item is unchecked
+ EditGameEvent(); // switch back to EditGame mode
return;
}
+
+ if(!stalling) {
+ InitChessProgram(&second, FALSE); // unbalances ping of second engine
+ SendToProgram("force\n", &second);
+ stalling = 1;
+ ScheduleDelayedEvent(TwoMachinesEventIfReady, 10);
+ return;
+ }
+ GetTimeMark(&now); // [HGM] matchpause: implement match pause after engine load
+ if(appData.matchPause>10000 || appData.matchPause<10)
+ appData.matchPause = 10000; /* [HGM] make pause adjustable */
+ wait = SubtractTimeMarks(&now, &pauseStart);
+ if(wait < appData.matchPause) {
+ ScheduleDelayedEvent(TwoMachinesEventIfReady, appData.matchPause - wait);
+ return;
+ }
+ // we are now committed to starting the game
+ stalling = 0;
DisplayMessage("", "");
- InitChessProgram(&second, FALSE);
- SendToProgram("force\n", &second);
if (startedFromSetupPosition) {
SendBoard(&second, backwardMostMove);
if (appData.debugMode) {
}
gameMode = TwoMachinesPlay;
- pausing = FALSE;
- ModeHighlight();
+ pausing = startingEngine = FALSE;
+ ModeHighlight(); // [HGM] logo: this triggers display update of logos
SetGameInfo();
DisplayTwoMachinesTitle();
firstMove = TRUE;
} else {
onmove = &second;
}
-
+ if(appData.debugMode) fprintf(debugFP, "New game (%d): %s-%s (%c)\n", matchGame, first.tidy, second.tidy, first.twoMachinesColor[0]);
SendToProgram(first.computerString, &first);
if (first.sendName) {
- sprintf(buf, "name %s\n", second.tidy);
+ snprintf(buf, MSG_SIZ, "name %s\n", second.tidy);
SendToProgram(buf, &first);
}
SendToProgram(second.computerString, &second);
if (second.sendName) {
- sprintf(buf, "name %s\n", first.tidy);
+ snprintf(buf, MSG_SIZ, "name %s\n", first.tidy);
SendToProgram(buf, &second);
}
if(bookHit) { // [HGM] book: simulate book reply
static char bookMove[MSG_SIZ]; // a bit generous?
- programStats.nodes = programStats.depth = programStats.time =
+ programStats.nodes = programStats.depth = programStats.time =
programStats.score = programStats.got_only_move = 0;
sprintf(programStats.movelist, "%s (xbook)", bookHit);
- strcpy(bookMove, "move ");
+ safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
strcat(bookMove, bookHit);
- HandleMachineMove(bookMove, &first);
+ savedMessage = bookMove; // args for deferred call
+ savedState = onmove;
+ ScheduleDelayedEvent(DeferredBookMove, 1);
}
}
void
-TrainingEvent()
+TrainingEvent ()
{
if (gameMode == Training) {
SetTrainingModeOff();
}
void
-IcsClientEvent()
+IcsClientEvent ()
{
if (!appData.icsActive) return;
switch (gameMode) {
break;
case EditPosition:
- EditPositionDone();
+ EditPositionDone(TRUE);
break;
case AnalyzeMode:
case AnalyzeFile:
ExitAnalyzeMode();
break;
-
+
default:
EditGameEvent();
break;
return;
}
-
void
-EditGameEvent()
+EditGameEvent ()
{
int i;
case MachinePlaysBlack:
case BeginningOfGame:
SendToProgram("force\n", &first);
+ if(gameMode == (forwardMostMove & 1 ? MachinePlaysBlack : MachinePlaysWhite)) { // engine is thinking
+ if (first.usePing) { // [HGM] always send ping when we might interrupt machine thinking
+ char buf[MSG_SIZ];
+ abortEngineThink = TRUE;
+ snprintf(buf, MSG_SIZ, "ping %d\n", initPing = ++first.lastPing);
+ SendToProgram(buf, &first);
+ DisplayMessage("Aborting engine think", "");
+ FreezeUI();
+ }
+ }
SetUserThinkingEnables();
break;
case PlayFromGameFile:
}
break;
case EditPosition:
- EditPositionDone();
+ EditPositionDone(TRUE);
break;
case AnalyzeMode:
case AnalyzeFile:
SendToProgram("force\n", &first);
break;
case TwoMachinesPlay:
- GameEnds((ChessMove) 0, NULL, GE_PLAYER);
+ GameEnds(EndOfFile, NULL, GE_PLAYER);
ResurrectChessProgram();
SetUserThinkingEnables();
break;
default:
return;
}
-
+
pausing = FALSE;
StopClocks();
first.offeredDraw = second.offeredDraw = 0;
SendToProgram("undo\n", &first);
i--;
}
+ if(!adjustedClock) {
whiteTimeRemaining = timeRemaining[0][currentMove];
blackTimeRemaining = timeRemaining[1][currentMove];
DisplayBothClocks();
+ }
if (whiteFlag || blackFlag) {
whiteFlag = blackFlag = 0;
}
DisplayTitle("");
- }
-
+ }
+
gameMode = EditGame;
ModeHighlight();
SetGameInfo();
void
-EditPositionEvent()
+EditPositionEvent ()
{
if (gameMode == EditPosition) {
EditGameEvent();
return;
}
-
+
EditGameEvent();
if (gameMode != EditGame) return;
-
+
gameMode = EditPosition;
ModeHighlight();
SetGameInfo();
if (currentMove > 0)
CopyBoard(boards[0], boards[currentMove]);
-
+
blackPlaysFirst = !WhiteOnMove(currentMove);
ResetClocks();
currentMove = forwardMostMove = backwardMostMove = 0;
HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
DisplayMove(-1);
+ if(!appData.pieceMenu) DisplayMessage(_("Click clock to clear board"), "");
}
void
-ExitAnalyzeMode()
+ExitAnalyzeMode ()
{
/* [DM] icsEngineAnalyze - possible call from other functions */
if (appData.icsEngineAnalyze) {
DisplayMessage("",_("Close ICS engine analyze..."));
}
if (first.analysisSupport && first.analyzing) {
- SendToProgram("exit\n", &first);
- first.analyzing = FALSE;
+ SendToBoth("exit\n");
+ first.analyzing = second.analyzing = FALSE;
}
- AnalysisPopDown();
thinkOutput[0] = NULLCHAR;
}
void
-EditPositionDone()
+EditPositionDone (Boolean fakeRights)
{
int king = gameInfo.variant == VariantKnightmate ? WhiteUnicorn : WhiteKing;
startedFromSetupPosition = TRUE;
InitChessProgram(&first, FALSE);
- castlingRights[0][2] = castlingRights[0][5] = BOARD_WIDTH>>1;
- if(boards[0][0][BOARD_WIDTH>>1] == king) {
- castlingRights[0][1] = boards[0][0][BOARD_LEFT] == WhiteRook ? 0 : -1;
- castlingRights[0][0] = boards[0][0][BOARD_RGHT-1] == WhiteRook ? BOARD_RGHT-1 : -1;
- } else castlingRights[0][2] = -1;
- if(boards[0][BOARD_HEIGHT-1][BOARD_WIDTH>>1] == WHITE_TO_BLACK king) {
- castlingRights[0][4] = boards[0][BOARD_HEIGHT-1][BOARD_LEFT] == BlackRook ? 0 : -1;
- castlingRights[0][3] = boards[0][BOARD_HEIGHT-1][BOARD_RGHT-1] == BlackRook ? BOARD_RGHT-1 : -1;
- } else castlingRights[0][5] = -1;
+ if(fakeRights) { // [HGM] suppress this if we just pasted a FEN.
+ boards[0][EP_STATUS] = EP_NONE;
+ boards[0][CASTLING][2] = boards[0][CASTLING][5] = BOARD_WIDTH>>1;
+ if(boards[0][0][BOARD_WIDTH>>1] == king) {
+ boards[0][CASTLING][1] = boards[0][0][BOARD_LEFT] == WhiteRook ? BOARD_LEFT : NoRights;
+ boards[0][CASTLING][0] = boards[0][0][BOARD_RGHT-1] == WhiteRook ? BOARD_RGHT-1 : NoRights;
+ } else boards[0][CASTLING][2] = NoRights;
+ if(boards[0][BOARD_HEIGHT-1][BOARD_WIDTH>>1] == WHITE_TO_BLACK king) {
+ boards[0][CASTLING][4] = boards[0][BOARD_HEIGHT-1][BOARD_LEFT] == BlackRook ? BOARD_LEFT : NoRights;
+ boards[0][CASTLING][3] = boards[0][BOARD_HEIGHT-1][BOARD_RGHT-1] == BlackRook ? BOARD_RGHT-1 : NoRights;
+ } else boards[0][CASTLING][5] = NoRights;
+ if(gameInfo.variant == VariantSChess) {
+ int i;
+ for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // pieces in their original position are assumed virgin
+ boards[0][VIRGIN][i] = 0;
+ if(boards[0][0][i] == FIDEArray[0][i-BOARD_LEFT]) boards[0][VIRGIN][i] |= VIRGIN_W;
+ if(boards[0][BOARD_HEIGHT-1][i] == FIDEArray[1][i-BOARD_LEFT]) boards[0][VIRGIN][i] |= VIRGIN_B;
+ }
+ }
+ }
SendToProgram("force\n", &first);
if (blackPlaysFirst) {
- strcpy(moveList[0], "");
- strcpy(parseList[0], "");
+ safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
+ safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
currentMove = forwardMostMove = backwardMostMove = 1;
CopyBoard(boards[1], boards[0]);
- /* [HGM] copy rights as well, as this code is also used after pasting a FEN */
- { int i;
- epStatus[1] = epStatus[0];
- for(i=0; i<nrCastlingRights; i++) castlingRights[1][i] = castlingRights[0][i];
- }
} else {
currentMove = forwardMostMove = backwardMostMove = 0;
}
fprintf(debugFP, "EditPosDone\n");
}
DisplayTitle("");
+ DisplayMessage("", "");
timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
timeRemaining[1][forwardMostMove] = blackTimeRemaining;
gameMode = EditGame;
/* Pause for `ms' milliseconds */
/* !! Ugh, this is a kludge. Fix it sometime. --tpm */
void
-TimeDelay(ms)
- long ms;
+TimeDelay (long ms)
{
TimeMark m1, m2;
/* !! Ugh, this is a kludge. Fix it sometime. --tpm */
void
-SendMultiLineToICS(buf)
- char *buf;
+SendMultiLineToICS (char *buf)
{
char temp[MSG_SIZ+1], *p;
int len;
len = strlen(buf);
if (len > MSG_SIZ)
len = MSG_SIZ;
-
+
strncpy(temp, buf, len);
temp[len] = 0;
}
void
-SetWhiteToPlayEvent()
+SetWhiteToPlayEvent ()
{
if (gameMode == EditPosition) {
blackPlaysFirst = FALSE;
}
void
-SetBlackToPlayEvent()
+SetBlackToPlayEvent ()
{
if (gameMode == EditPosition) {
blackPlaysFirst = TRUE;
}
void
-EditPositionMenuEvent(selection, x, y)
- ChessSquare selection;
- int x, y;
+EditPositionMenuEvent (ChessSquare selection, int x, int y)
{
char buf[MSG_SIZ];
ChessSquare piece = boards[0][y][x];
+ static Board erasedBoard, currentBoard, menuBoard, nullBoard;
+ static int lastVariant;
if (gameMode != EditPosition && gameMode != IcsExamining) return;
switch (selection) {
case ClearBoard:
+ fromX = fromY = killX = killY = -1; // [HGM] abort any move entry in progress
+ MarkTargetSquares(1);
+ CopyBoard(currentBoard, boards[0]);
+ CopyBoard(menuBoard, initialPosition);
if (gameMode == IcsExamining && ics_type == ICS_FICS) {
SendToICS(ics_prefix);
SendToICS("bsetup clear\n");
SendToICS(ics_prefix);
SendToICS("clearboard\n");
} else {
+ int nonEmpty = 0;
for (x = 0; x < BOARD_WIDTH; x++) { ChessSquare p = EmptySquare;
if(x == BOARD_LEFT-1 || x == BOARD_RGHT) p = (ChessSquare) 0; /* [HGM] holdings */
for (y = 0; y < BOARD_HEIGHT; y++) {
if (gameMode == IcsExamining) {
if (boards[currentMove][y][x] != EmptySquare) {
- sprintf(buf, "%sx@%c%c\n", ics_prefix,
+ snprintf(buf, MSG_SIZ, "%sx@%c%c\n", ics_prefix,
AAA + x, ONE + y);
SendToICS(buf);
}
- } else {
+ } else if(boards[0][y][x] != DarkSquare) {
+ if(boards[0][y][x] != p) nonEmpty++;
boards[0][y][x] = p;
}
}
}
+ if(gameMode != IcsExamining) { // [HGM] editpos: cycle trough boards
+ int r;
+ for(r = 0; r < BOARD_HEIGHT; r++) {
+ for(x = BOARD_LEFT; x < BOARD_RGHT; x++) { // create 'menu board' by removing duplicates
+ ChessSquare p = menuBoard[r][x];
+ for(y = x + 1; y < BOARD_RGHT; y++) if(menuBoard[r][y] == p) menuBoard[r][y] = EmptySquare;
+ }
+ }
+ DisplayMessage("Clicking clock again restores position", "");
+ if(gameInfo.variant != lastVariant) lastVariant = gameInfo.variant, CopyBoard(erasedBoard, boards[0]);
+ if(!nonEmpty) { // asked to clear an empty board
+ CopyBoard(boards[0], menuBoard);
+ } else
+ if(CompareBoards(currentBoard, menuBoard)) { // asked to clear an empty board
+ CopyBoard(boards[0], initialPosition);
+ } else
+ if(CompareBoards(currentBoard, initialPosition) && !CompareBoards(currentBoard, erasedBoard)
+ && !CompareBoards(nullBoard, erasedBoard)) {
+ CopyBoard(boards[0], erasedBoard);
+ } else
+ CopyBoard(erasedBoard, currentBoard);
+
+ }
}
if (gameMode == EditPosition) {
DrawPosition(FALSE, boards[0]);
case EmptySquare:
if (gameMode == IcsExamining) {
- sprintf(buf, "%sx@%c%c\n", ics_prefix, AAA + x, ONE + y);
+ if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
+ snprintf(buf, MSG_SIZ, "%sx@%c%c\n", ics_prefix, AAA + x, ONE + y);
SendToICS(buf);
} else {
+ if(x < BOARD_LEFT || x >= BOARD_RGHT) {
+ if(x == BOARD_LEFT-2) {
+ if(y < BOARD_HEIGHT-1-gameInfo.holdingsSize) break;
+ boards[0][y][1] = 0;
+ } else
+ if(x == BOARD_RGHT+1) {
+ if(y >= gameInfo.holdingsSize) break;
+ boards[0][y][BOARD_WIDTH-2] = 0;
+ } else break;
+ }
boards[0][y][x] = EmptySquare;
DrawPosition(FALSE, boards[0]);
}
case PromotePiece:
if(piece >= (int)WhitePawn && piece < (int)WhiteMan ||
piece >= (int)BlackPawn && piece < (int)BlackMan ) {
- selection = (ChessSquare) (PROMOTED piece);
+ selection = (ChessSquare) (PROMOTED(piece));
} else if(piece == EmptySquare) selection = WhiteSilver;
else selection = (ChessSquare)((int)piece - 1);
goto defaultlabel;
case DemotePiece:
if(piece > (int)WhiteMan && piece <= (int)WhiteKing ||
piece > (int)BlackMan && piece <= (int)BlackKing ) {
- selection = (ChessSquare) (DEMOTED piece);
+ selection = (ChessSquare) (DEMOTED(piece));
} else if(piece == EmptySquare) selection = BlackSilver;
- else selection = (ChessSquare)((int)piece + 1);
+ else selection = (ChessSquare)((int)piece + 1);
goto defaultlabel;
case WhiteQueen:
case BlackQueen:
if(gameInfo.variant == VariantShatranj ||
gameInfo.variant == VariantXiangqi ||
- gameInfo.variant == VariantCourier )
+ gameInfo.variant == VariantCourier ||
+ gameInfo.variant == VariantASEAN ||
+ gameInfo.variant == VariantMakruk )
selection = (ChessSquare)((int)selection - (int)WhiteQueen + (int)WhiteFerz);
goto defaultlabel;
default:
defaultlabel:
if (gameMode == IcsExamining) {
- sprintf(buf, "%s%c@%c%c\n", ics_prefix,
- PieceToChar(selection), AAA + x, ONE + y);
+ if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
+ snprintf(buf, MSG_SIZ, "%s%c@%c%c\n", ics_prefix,
+ PieceToChar(selection), AAA + x, ONE + y);
SendToICS(buf);
} else {
+ if(x < BOARD_LEFT || x >= BOARD_RGHT) {
+ int n;
+ if(x == BOARD_LEFT-2 && selection >= BlackPawn) {
+ n = PieceToNumber(selection - BlackPawn);
+ if(n >= gameInfo.holdingsSize) { n = 0; selection = BlackPawn; }
+ boards[0][BOARD_HEIGHT-1-n][0] = selection;
+ boards[0][BOARD_HEIGHT-1-n][1]++;
+ } else
+ if(x == BOARD_RGHT+1 && selection < BlackPawn) {
+ n = PieceToNumber(selection);
+ if(n >= gameInfo.holdingsSize) { n = 0; selection = WhitePawn; }
+ boards[0][n][BOARD_WIDTH-1] = selection;
+ boards[0][n][BOARD_WIDTH-2]++;
+ }
+ } else
boards[0][y][x] = selection;
- DrawPosition(FALSE, boards[0]);
+ DrawPosition(TRUE, boards[0]);
+ ClearHighlights();
+ fromX = fromY = -1;
}
break;
}
void
-DropMenuEvent(selection, x, y)
- ChessSquare selection;
- int x, y;
+DropMenuEvent (ChessSquare selection, int x, int y)
{
ChessMove moveType;
}
void
-AcceptEvent()
+AcceptEvent ()
{
/* Accept a pending offer of any kind from opponent */
-
+
if (appData.icsActive) {
SendToICS(ics_prefix);
SendToICS("accept\n");
}
void
-DeclineEvent()
+DeclineEvent ()
{
/* Decline a pending offer of any kind from opponent */
-
+
if (appData.icsActive) {
SendToICS(ics_prefix);
SendToICS("decline\n");
StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
"Black offers a draw" : "White offers a draw")) {
#ifdef NOTDEF
- AppendComment(cmailOldMove, "Draw declined");
+ AppendComment(cmailOldMove, "Draw declined", TRUE);
DisplayComment(cmailOldMove - 1, "Draw declined");
#endif /*NOTDEF*/
} else {
}
void
-RematchEvent()
+RematchEvent ()
{
/* Issue ICS rematch command */
if (appData.icsActive) {
}
void
-CallFlagEvent()
+CallFlagEvent ()
{
/* Call your opponent's flag (claim a win on time) */
if (appData.icsActive) {
}
void
-DrawEvent()
+ClockClick (int which)
+{ // [HGM] code moved to back-end from winboard.c
+ if(which) { // black clock
+ if (gameMode == EditPosition || gameMode == IcsExamining) {
+ if(!appData.pieceMenu && blackPlaysFirst) EditPositionMenuEvent(ClearBoard, 0, 0);
+ SetBlackToPlayEvent();
+ } else if ((gameMode == AnalyzeMode || gameMode == EditGame ||
+ gameMode == MachinePlaysBlack && PosFlags(0) & F_NULL_MOVE && !blackFlag && !shiftKey) && WhiteOnMove(currentMove)) {
+ UserMoveEvent((int)EmptySquare, DROP_RANK, 0, 0, 0); // [HGM] multi-move: if not out of time, enters null move
+ } else if (shiftKey) {
+ AdjustClock(which, -1);
+ } else if (gameMode == IcsPlayingWhite ||
+ gameMode == MachinePlaysBlack) {
+ CallFlagEvent();
+ }
+ } else { // white clock
+ if (gameMode == EditPosition || gameMode == IcsExamining) {
+ if(!appData.pieceMenu && !blackPlaysFirst) EditPositionMenuEvent(ClearBoard, 0, 0);
+ SetWhiteToPlayEvent();
+ } else if ((gameMode == AnalyzeMode || gameMode == EditGame ||
+ gameMode == MachinePlaysWhite && PosFlags(0) & F_NULL_MOVE && !whiteFlag && !shiftKey) && !WhiteOnMove(currentMove)) {
+ UserMoveEvent((int)EmptySquare, DROP_RANK, 0, 0, 0); // [HGM] multi-move
+ } else if (shiftKey) {
+ AdjustClock(which, -1);
+ } else if (gameMode == IcsPlayingBlack ||
+ gameMode == MachinePlaysWhite) {
+ CallFlagEvent();
+ }
+ }
+}
+
+void
+DrawEvent ()
{
/* Offer draw or accept pending draw offer from opponent */
-
+
if (appData.icsActive) {
/* Note: tournament rules require draw offers to be
made after you make your move but before you punch
your clock. Currently ICS doesn't let you do that;
instead, you immediately punch your clock after making
a move, but you can offer a draw at any time. */
-
+
SendToICS(ics_prefix);
SendToICS("draw\n");
+ userOfferedDraw = TRUE; // [HGM] drawclaim: also set flag in ICS play
} else if (cmailMsgLoaded) {
if (currentMove == cmailOldMove &&
commentList[cmailOldMove] != NULL &&
} else if (currentMove == cmailOldMove + 1) {
char *offer = WhiteOnMove(cmailOldMove) ?
"White offers a draw" : "Black offers a draw";
- AppendComment(currentMove, offer);
+ AppendComment(currentMove, offer, TRUE);
DisplayComment(currentMove - 1, offer);
cmailMoveType[lastLoadGameNumber - 1] = CMAIL_DRAW;
} else {
}
void
-AdjournEvent()
+AdjournEvent ()
{
/* Offer Adjourn or accept pending Adjourn offer from opponent */
-
+
if (appData.icsActive) {
SendToICS(ics_prefix);
SendToICS("adjourn\n");
void
-AbortEvent()
+AbortEvent ()
{
/* Offer Abort or accept pending Abort offer from opponent */
-
+
if (appData.icsActive) {
SendToICS(ics_prefix);
SendToICS("abort\n");
}
void
-ResignEvent()
+ResignEvent ()
{
/* Resign. You can do this even if it's not your turn. */
-
+
if (appData.icsActive) {
SendToICS(ics_prefix);
SendToICS("resign\n");
void
-StopObservingEvent()
+StopObservingEvent ()
{
/* Stop observing current games */
SendToICS(ics_prefix);
}
void
-StopExaminingEvent()
+StopExaminingEvent ()
{
/* Stop observing current game */
SendToICS(ics_prefix);
}
void
-ForwardInner(target)
- int target;
+ForwardInner (int target)
{
- int limit;
+ int limit; int oldSeekGraphUp = seekGraphUp;
if (appData.debugMode)
fprintf(debugFP, "ForwardInner(%d), current %d, forward %d\n",
if (gameMode == EditPosition)
return;
+ seekGraphUp = FALSE;
+ MarkTargetSquares(1);
+ fromX = fromY = killX = killY = -1; // [HGM] abort any move entry in progress
+
if (gameMode == PlayFromGameFile && !pausing)
PauseEvent();
-
+
if (gameMode == IcsExamining && pausing)
limit = pauseExamForwardMostMove;
else
limit = forwardMostMove;
-
+
if (target > limit) target = limit;
if (target > 0 && moveList[target - 1][0]) {
SetHighlights(-1, -1, toX, toY);
}
} else {
+ int viaX = moveList[target - 1][5] - AAA;
+ int viaY = moveList[target - 1][6] - ONE;
fromX = moveList[target - 1][0] - AAA;
fromY = moveList[target - 1][1] - ONE;
if (target == currentMove + 1) {
+ if(moveList[target - 1][4] == ';') { // multi-leg
+ ChessSquare piece = boards[currentMove][viaY][viaX];
+ AnimateMove(boards[currentMove], fromX, fromY, viaX, viaY);
+ boards[currentMove][viaY][viaX] = boards[currentMove][fromY][fromX];
+ AnimateMove(boards[currentMove], viaX, viaY, toX, toY);
+ boards[currentMove][viaY][viaX] = piece;
+ } else
AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
}
if (appData.highlightLastMove) {
}
}
}
- if (gameMode == EditGame || gameMode == AnalyzeMode ||
- gameMode == Training || gameMode == PlayFromGameFile ||
+ if (gameMode == EditGame || gameMode == AnalyzeMode ||
+ gameMode == Training || gameMode == PlayFromGameFile ||
gameMode == AnalyzeFile) {
while (currentMove < target) {
+ if(second.analyzing) SendMoveToProgram(currentMove, &second);
SendMoveToProgram(currentMove++, &first);
}
} else {
currentMove = target;
}
-
+
if (gameMode == EditGame || gameMode == EndOfGame) {
whiteTimeRemaining = timeRemaining[0][currentMove];
blackTimeRemaining = timeRemaining[1][currentMove];
}
DisplayBothClocks();
DisplayMove(currentMove - 1);
- DrawPosition(FALSE, boards[currentMove]);
+ DrawPosition(oldSeekGraphUp, boards[currentMove]);
HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
if ( !matchMode && gameMode != Training) { // [HGM] PV info: routine tests if empty
DisplayComment(currentMove - 1, commentList[currentMove]);
}
+ ClearMap(); // [HGM] exclude: invalidate map
}
void
-ForwardEvent()
+ForwardEvent ()
{
if (gameMode == IcsExamining && !pausing) {
SendToICS(ics_prefix);
}
void
-ToEndEvent()
+ToEndEvent ()
{
if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
/* to optimze, we temporarily turn off analysis mode while we feed
* the remaining moves to the engine. Otherwise we get analysis output
* after each move.
- */
+ */
if (first.analysisSupport) {
SendToProgram("exit\nforce\n", &first);
first.analyzing = FALSE;
}
}
-
+
if (gameMode == IcsExamining && !pausing) {
SendToICS(ics_prefix);
SendToICS("forward 999999\n");
}
void
-BackwardInner(target)
- int target;
+BackwardInner (int target)
{
int full_redraw = TRUE; /* [AS] Was FALSE, had to change it! */
target, currentMove, forwardMostMove);
if (gameMode == EditPosition) return;
+ seekGraphUp = FALSE;
+ MarkTargetSquares(1);
+ fromX = fromY = killX = killY = -1; // [HGM] abort any move entry in progress
if (currentMove <= backwardMostMove) {
ClearHighlights();
DrawPosition(full_redraw, boards[currentMove]);
}
if (gameMode == PlayFromGameFile && !pausing)
PauseEvent();
-
+
if (moveList[target][0]) {
int fromX, fromY, toX, toY;
toX = moveList[target][2] - AAA;
if (gameMode == EditGame || gameMode==AnalyzeMode ||
gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
while (currentMove > target) {
- SendToProgram("undo\n", &first);
+ if(moveList[currentMove-1][1] == '@' && moveList[currentMove-1][0] == '@') {
+ // null move cannot be undone. Reload program with move history before it.
+ int i;
+ for(i=target; i>backwardMostMove; i--) { // seek back to start or previous null move
+ if(moveList[i-1][1] == '@' && moveList[i-1][0] == '@') break;
+ }
+ SendBoard(&first, i);
+ if(second.analyzing) SendBoard(&second, i);
+ for(currentMove=i; currentMove<target; currentMove++) {
+ SendMoveToProgram(currentMove, &first);
+ if(second.analyzing) SendMoveToProgram(currentMove, &second);
+ }
+ break;
+ }
+ SendToBoth("undo\n");
currentMove--;
}
} else {
currentMove = target;
}
-
+
if (gameMode == EditGame || gameMode == EndOfGame) {
whiteTimeRemaining = timeRemaining[0][currentMove];
blackTimeRemaining = timeRemaining[1][currentMove];
HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
// [HGM] PV info: routine tests if comment empty
DisplayComment(currentMove - 1, commentList[currentMove]);
+ ClearMap(); // [HGM] exclude: invalidate map
}
void
-BackwardEvent()
+BackwardEvent ()
{
if (gameMode == IcsExamining && !pausing) {
SendToICS(ics_prefix);
}
void
-ToStartEvent()
+ToStartEvent ()
{
if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
- /* to optimze, we temporarily turn off analysis mode while we undo
+ /* to optimize, we temporarily turn off analysis mode while we undo
* all the moves. Otherwise we get analysis output after each undo.
- */
+ */
if (first.analysisSupport) {
SendToProgram("exit\nforce\n", &first);
first.analyzing = FALSE;
}
void
-ToNrEvent(int to)
+ToNrEvent (int to)
{
if (gameMode == PlayFromGameFile && !pausing) PauseEvent();
if (to >= forwardMostMove) to = forwardMostMove;
}
void
-RevertEvent()
+RevertEvent (Boolean annotate)
{
+ if(PopTail(annotate)) { // [HGM] vari: restore old game tail
+ return;
+ }
if (gameMode != IcsExamining) {
DisplayError(_("You are not examining a game"), 0);
return;
}
void
-RetractMoveEvent()
+RetractMoveEvent ()
{
switch (gameMode) {
case MachinePlaysWhite:
case MachinePlaysBlack:
if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
- DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
+ DisplayError(_("Wait until your turn,\nor select 'Move Now'."), 0);
return;
}
if (forwardMostMove < 2) return;
}
void
-MoveNowEvent()
+MoveNowEvent ()
{
ChessProgramState *cps;
}
void
-TruncateGameEvent()
+TruncateGameEvent ()
{
EditGameEvent();
if (gameMode != EditGame) return;
}
void
-TruncateGame()
+TruncateGame ()
{
+ CleanupTail(); // [HGM] vari: only keep current variation if we explicitly truncate
if (forwardMostMove > currentMove) {
if (gameInfo.resultDetails != NULL) {
free(gameInfo.resultDetails);
}
void
-HintEvent()
+HintEvent ()
{
if (appData.noChessProgram) return;
switch (gameMode) {
case MachinePlaysWhite:
if (WhiteOnMove(forwardMostMove)) {
- DisplayError(_("Wait until your turn"), 0);
+ DisplayError(_("Wait until your turn."), 0);
return;
}
break;
case BeginningOfGame:
case MachinePlaysBlack:
if (!WhiteOnMove(forwardMostMove)) {
- DisplayError(_("Wait until your turn"), 0);
+ DisplayError(_("Wait until your turn."), 0);
return;
}
break;
hintRequested = TRUE;
}
+int
+SaveSelected (FILE *g, int dummy, char *dummy2)
+{
+ ListGame * lg = (ListGame *) gameList.head;
+ int nItem, cnt=0;
+ FILE *f;
+
+ if( !(f = GameFile()) || ((ListGame *) gameList.tailPred)->number <= 0 ) {
+ DisplayError(_("Game list not loaded or empty"), 0);
+ return 0;
+ }
+
+ creatingBook = TRUE; // suppresses stuff during load game
+
+ /* Get list size */
+ for (nItem = 1; nItem <= ((ListGame *) gameList.tailPred)->number; nItem++){
+ if(lg->position >= 0) { // selected?
+ LoadGame(f, nItem, "", TRUE);
+ SaveGamePGN2(g); // leaves g open
+ cnt++; DoEvents();
+ }
+ lg = (ListGame *) lg->node.succ;
+ }
+
+ fclose(g);
+ creatingBook = FALSE;
+
+ return cnt;
+}
+
+void
+CreateBookEvent ()
+{
+ ListGame * lg = (ListGame *) gameList.head;
+ FILE *f, *g;
+ int nItem;
+ static int secondTime = FALSE;
+
+ if( !(f = GameFile()) || ((ListGame *) gameList.tailPred)->number <= 0 ) {
+ DisplayError(_("Game list not loaded or empty"), 0);
+ return;
+ }
+
+ if(!secondTime && (g = fopen(appData.polyglotBook, "r"))) {
+ fclose(g);
+ secondTime++;
+ DisplayNote(_("Book file exists! Try again for overwrite."));
+ return;
+ }
+
+ creatingBook = TRUE;
+ secondTime = FALSE;
+
+ /* Get list size */
+ for (nItem = 1; nItem <= ((ListGame *) gameList.tailPred)->number; nItem++){
+ if(lg->position >= 0) {
+ LoadGame(f, nItem, "", TRUE);
+ AddGameToBook(TRUE);
+ DoEvents();
+ }
+ lg = (ListGame *) lg->node.succ;
+ }
+
+ creatingBook = FALSE;
+ FlushBook();
+}
+
void
-BookEvent()
+BookEvent ()
{
if (appData.noChessProgram) return;
switch (gameMode) {
case MachinePlaysWhite:
if (WhiteOnMove(forwardMostMove)) {
- DisplayError(_("Wait until your turn"), 0);
+ DisplayError(_("Wait until your turn."), 0);
return;
}
break;
case BeginningOfGame:
case MachinePlaysBlack:
if (!WhiteOnMove(forwardMostMove)) {
- DisplayError(_("Wait until your turn"), 0);
+ DisplayError(_("Wait until your turn."), 0);
return;
}
break;
case EditPosition:
- EditPositionDone();
+ EditPositionDone(TRUE);
break;
case TwoMachinesPlay:
return;
}
void
-AboutGameEvent()
+AboutGameEvent ()
{
char *tags = PGNTags(&gameInfo);
TagsPopUp(tags, CmailMsg());
/* end button procedures */
void
-PrintPosition(fp, move)
- FILE *fp;
- int move;
+PrintPosition (FILE *fp, int move)
{
int i, j;
-
+
for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
char c = PieceToChar(boards[move][i][j]);
}
void
-PrintOpponents(fp)
- FILE *fp;
+PrintOpponents (FILE *fp)
{
if (gameInfo.white != NULL) {
fprintf(fp, "\t%s vs. %s\n", gameInfo.white, gameInfo.black);
/* Find last component of program's own name, using some heuristics */
void
-TidyProgramName(prog, host, buf)
- char *prog, *host, buf[MSG_SIZ];
+TidyProgramName (char *prog, char *host, char buf[MSG_SIZ])
{
- char *p, *q;
+ char *p, *q, c;
int local = (strcmp(host, "localhost") == 0);
while (!local && (p = strchr(prog, ';')) != NULL) {
p++;
while (p >= prog && *p != '/' && *p != '\\') p--;
p++;
if(p == prog && *p == '"') p++;
- if (q - p >= 4 && StrCaseCmp(q - 4, ".exe") == 0) q -= 4;
+ c = *q; *q = 0;
+ if (q - p >= 4 && StrCaseCmp(q - 4, ".exe") == 0) *q = c, q -= 4; else *q = c;
memcpy(buf, p, q - p);
buf[q - p] = NULLCHAR;
if (!local) {
}
char *
-TimeControlTagValue()
+TimeControlTagValue ()
{
char buf[MSG_SIZ];
if (!appData.clockMode) {
- strcpy(buf, "-");
+ safeStrCpy(buf, "-", sizeof(buf)/sizeof(buf[0]));
} else if (movesPerSession > 0) {
- sprintf(buf, "%d/%ld", movesPerSession, timeControl/1000);
+ snprintf(buf, MSG_SIZ, "%d/%ld", movesPerSession, timeControl/1000);
} else if (timeIncrement == 0) {
- sprintf(buf, "%ld", timeControl/1000);
+ snprintf(buf, MSG_SIZ, "%ld", timeControl/1000);
} else {
- sprintf(buf, "%ld+%ld", timeControl/1000, timeIncrement/1000);
+ snprintf(buf, MSG_SIZ, "%ld+%ld", timeControl/1000, timeIncrement/1000);
}
return StrSave(buf);
}
void
-SetGameInfo()
+SetGameInfo ()
{
/* This routine is used only for certain modes */
VariantClass v = gameInfo.variant;
+ ChessMove r = GameUnfinished;
+ char *p = NULL;
+
+ if(keepInfo) return;
+
+ if(gameMode == EditGame) { // [HGM] vari: do not erase result on EditGame
+ r = gameInfo.result;
+ p = gameInfo.resultDetails;
+ gameInfo.resultDetails = NULL;
+ }
ClearGameInfo(&gameInfo);
gameInfo.variant = v;
gameInfo.event = StrSave( appData.pgnEventHeader );
gameInfo.site = StrSave(HostName());
gameInfo.date = PGNDate();
- if (matchGame > 0) {
+ if (roundNr > 0) {
char buf[MSG_SIZ];
- sprintf(buf, "%d", matchGame);
+ snprintf(buf, MSG_SIZ, "%d", roundNr);
gameInfo.round = StrSave(buf);
} else {
gameInfo.round = StrSave("-");
}
if (first.twoMachinesColor[0] == 'w') {
- gameInfo.white = StrSave(first.tidy);
- gameInfo.black = StrSave(second.tidy);
+ gameInfo.white = StrSave(appData.pgnName[0][0] ? appData.pgnName[0] : first.tidy);
+ gameInfo.black = StrSave(appData.pgnName[1][0] ? appData.pgnName[1] : second.tidy);
} else {
- gameInfo.white = StrSave(second.tidy);
- gameInfo.black = StrSave(first.tidy);
+ gameInfo.white = StrSave(appData.pgnName[1][0] ? appData.pgnName[1] : second.tidy);
+ gameInfo.black = StrSave(appData.pgnName[0][0] ? appData.pgnName[0] : first.tidy);
}
gameInfo.timeControl = TimeControlTagValue();
break;
gameInfo.round = StrSave("-");
gameInfo.white = StrSave("-");
gameInfo.black = StrSave("-");
+ gameInfo.result = r;
+ gameInfo.resultDetails = p;
break;
case EditPosition:
}
void
-ReplaceComment(index, text)
- int index;
- char *text;
+ReplaceComment (int index, char *text)
{
int len;
+ char *p;
+ float score;
+ if(index && sscanf(text, "%f/%d", &score, &len) == 2 &&
+ pvInfoList[index-1].depth == len &&
+ fabs(pvInfoList[index-1].score - score*100.) < 0.5 &&
+ (p = strchr(text, '\n'))) text = p; // [HGM] strip off first line with PV info, if any
while (*text == '\n') text++;
len = strlen(text);
while (len > 0 && text[len - 1] == '\n') len--;
commentList[index] = NULL;
return;
}
+ if( *text == '{' && strchr(text, '}') || // [HGM] braces: if certainy malformed, put braces
+ *text == '[' && strchr(text, ']') || // otherwise hope the user knows what he is doing
+ *text == '(' && strchr(text, ')')) { // (perhaps check if this parses as comment-only?)
commentList[index] = (char *) malloc(len + 2);
strncpy(commentList[index], text, len);
commentList[index][len] = '\n';
commentList[index][len + 1] = NULLCHAR;
+ } else {
+ // [HGM] braces: if text does not start with known OK delimiter, put braces around it.
+ char *p;
+ commentList[index] = (char *) malloc(len + 7);
+ safeStrCpy(commentList[index], "{\n", 3);
+ safeStrCpy(commentList[index]+2, text, len+1);
+ commentList[index][len+2] = NULLCHAR;
+ while(p = strchr(commentList[index], '}')) *p = ')'; // kill all } to make it one comment
+ strcat(commentList[index], "\n}\n");
+ }
}
void
-CrushCRs(text)
- char *text;
+CrushCRs (char *text)
{
char *p = text;
char *q = text;
}
void
-AppendComment(index, text)
- int index;
- char *text;
+AppendComment (int index, char *text, Boolean addBraces)
+/* addBraces tells if we should add {} */
{
int oldlen, len;
char *old;
+if(appData.debugMode) fprintf(debugFP, "Append: in='%s' %d\n", text, addBraces);
+ if(addBraces == 3) addBraces = 0; else // force appending literally
text = GetInfoFromComment( index, text ); /* [HGM] PV time: strip PV info from comment */
CrushCRs(text);
while (*text == '\n') text++;
len = strlen(text);
while (len > 0 && text[len - 1] == '\n') len--;
+ text[len] = NULLCHAR;
if (len == 0) return;
if (commentList[index] != NULL) {
+ Boolean addClosingBrace = addBraces;
old = commentList[index];
oldlen = strlen(old);
- commentList[index] = (char *) malloc(oldlen + len + 2);
- strcpy(commentList[index], old);
+ while(commentList[index][oldlen-1] == '\n')
+ commentList[index][--oldlen] = NULLCHAR;
+ commentList[index] = (char *) malloc(oldlen + len + 6); // might waste 4
+ safeStrCpy(commentList[index], old, oldlen + len + 6);
free(old);
- strncpy(&commentList[index][oldlen], text, len);
- commentList[index][oldlen + len] = '\n';
- commentList[index][oldlen + len + 1] = NULLCHAR;
+ // [HGM] braces: join "{A\n}\n" + "{\nB}" as "{A\nB\n}"
+ if(commentList[index][oldlen-1] == '}' && (text[0] == '{' || addBraces == TRUE)) {
+ if(addBraces == TRUE) addBraces = FALSE; else { text++; len--; }
+ while (*text == '\n') { text++; len--; }
+ commentList[index][--oldlen] = NULLCHAR;
+ }
+ if(addBraces) strcat(commentList[index], addBraces == 2 ? "\n(" : "\n{\n");
+ else strcat(commentList[index], "\n");
+ strcat(commentList[index], text);
+ if(addClosingBrace) strcat(commentList[index], addClosingBrace == 2 ? ")\n" : "\n}\n");
+ else strcat(commentList[index], "\n");
} else {
- commentList[index] = (char *) malloc(len + 2);
- strncpy(commentList[index], text, len);
- commentList[index][len] = '\n';
- commentList[index][len + 1] = NULLCHAR;
+ commentList[index] = (char *) malloc(len + 6); // perhaps wastes 4...
+ if(addBraces)
+ safeStrCpy(commentList[index], addBraces == 2 ? "(" : "{\n", 3);
+ else commentList[index][0] = NULLCHAR;
+ strcat(commentList[index], text);
+ strcat(commentList[index], addBraces == 2 ? ")\n" : "\n");
+ if(addBraces == TRUE) strcat(commentList[index], "}\n");
}
}
-static char * FindStr( char * text, char * sub_text )
+static char *
+FindStr (char * text, char * sub_text)
{
char * result = strstr( text, sub_text );
/* [AS] Try to extract PV info from PGN comment */
/* [HGM] PV time: and then remove it, to prevent it appearing twice */
-char *GetInfoFromComment( int index, char * text )
+char *
+GetInfoFromComment (int index, char * text)
{
- char * sep = text;
+ char * sep = text, *p;
if( text != NULL && index > 0 ) {
int score = 0;
int time = -1, sec = 0, deci;
char * s_eval = FindStr( text, "[%eval " );
char * s_emt = FindStr( text, "[%emt " );
-
+#if 0
if( s_eval != NULL || s_emt != NULL ) {
+#else
+ if(0) { // [HGM] this code is not finished, and could actually be detrimental
+#endif
/* New style */
char delim;
if( s_emt != NULL ) {
}
+ return text;
}
else {
/* We expect something like: [+|-]nnn.nn/dd */
int score_lo = 0;
+ if(*text != '{') return text; // [HGM] braces: must be normal comment
+
sep = strchr( text, '/' );
if( sep == NULL || sep < (text+4) ) {
return text;
}
+ p = text;
+ if(!strncmp(p+1, "final score ", 12)) p += 12, index++; else
+ if(p[1] == '(') { // comment starts with PV
+ p = strchr(p, ')'); // locate end of PV
+ if(p == NULL || sep < p+5) return text;
+ // at this point we have something like "{(.*) +0.23/6 ..."
+ p = text; while(*++p != ')') p[-1] = *p; p[-1] = ')';
+ *p = '\n'; while(*p == ' ' || *p == '\n') p++; *--p = '{';
+ // we now moved the brace to behind the PV: "(.*) {+0.23/6 ..."
+ }
time = -1; sec = -1; deci = -1;
- if( sscanf( text, "%d.%d/%d %d:%d", &score, &score_lo, &depth, &time, &sec ) != 5 &&
- sscanf( text, "%d.%d/%d %d.%d", &score, &score_lo, &depth, &time, &deci ) != 5 &&
- sscanf( text, "%d.%d/%d %d", &score, &score_lo, &depth, &time ) != 4 &&
- sscanf( text, "%d.%d/%d", &score, &score_lo, &depth ) != 3 ) {
+ if( sscanf( p+1, "%d.%d/%d %d:%d", &score, &score_lo, &depth, &time, &sec ) != 5 &&
+ sscanf( p+1, "%d.%d/%d %d.%d", &score, &score_lo, &depth, &time, &deci ) != 5 &&
+ sscanf( p+1, "%d.%d/%d %d", &score, &score_lo, &depth, &time ) != 4 &&
+ sscanf( p+1, "%d.%d/%d", &score, &score_lo, &depth ) != 3 ) {
return text;
}
if(sec >= 0) time = 600*time + 10*sec; else
if(deci >= 0) time = 10*time + deci; else time *= 10; // deci-sec
- score = score >= 0 ? score*100 + score_lo : score*100 - score_lo;
+ score = score > 0 || !score & p[1] != '-' ? score*100 + score_lo : score*100 - score_lo;
/* [HGM] PV time: now locate end of PV info */
while( *++sep >= '0' && *sep <= '9'); // strip depth
if(time >= 0)
- while( *++sep >= '0' && *sep <= '9'); // strip time
+ while( *++sep >= '0' && *sep <= '9' || *sep == '\n'); // strip time
if(sec >= 0)
while( *++sep >= '0' && *sep <= '9'); // strip seconds
if(deci >= 0)
while( *++sep >= '0' && *sep <= '9'); // strip fractional seconds
- while(*sep == ' ') sep++;
+ while(*sep == ' ' || *sep == '\n' || *sep == '\r') sep++;
}
if( depth <= 0 ) {
pvInfoList[index-1].depth = depth;
pvInfoList[index-1].score = score;
pvInfoList[index-1].time = 10*time; // centi-sec
+ if(*sep == '}') *sep = 0; else *--sep = '{';
+ if(p != text) { while(*p++ = *sep++); sep = text; } // squeeze out space between PV and comment, and return both
}
return sep;
}
void
-SendToProgram(message, cps)
- char *message;
- ChessProgramState *cps;
+SendToProgram (char *message, ChessProgramState *cps)
{
int count, outCount, error;
char buf[MSG_SIZ];
- if (cps->pr == NULL) return;
+ if (cps->pr == NoProc) return;
Attention(cps);
-
+
if (appData.debugMode) {
TimeMark now;
GetTimeMark(&now);
- fprintf(debugFP, "%ld >%-6s: %s",
+ fprintf(debugFP, "%ld >%-6s: %s",
SubtractTimeMarks(&now, &programStartTime),
cps->which, message);
+ if(serverFP)
+ fprintf(serverFP, "%ld >%-6s: %s",
+ SubtractTimeMarks(&now, &programStartTime),
+ cps->which, message), fflush(serverFP);
}
-
+
count = strlen(message);
outCount = OutputToProcess(cps->pr, message, count, &error);
- if (outCount < count && !exiting
+ if (outCount < count && !exiting
&& !endingGame) { /* [HGM] crash: to not hang GameEnds() writing to deceased engines */
- sprintf(buf, _("Error writing to %s chess program"), cps->which);
+ if(!cps->initDone) return; // [HGM] should not generate fatal error during engine load
+ snprintf(buf, MSG_SIZ, _("Error writing to %s chess program"), _(cps->which));
if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
- if(epStatus[forwardMostMove] <= EP_DRAWS) {
+ if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
+ snprintf(buf, MSG_SIZ, _("%s program exits in draw position (%s)"), _(cps->which), cps->program);
+ if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(GameIsDrawn, buf, GE_XBOARD); return; }
gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
- sprintf(buf, "%s program exits in draw position (%s)", cps->which, cps->program);
} else {
- gameInfo.result = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
+ ChessMove res = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
+ if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(res, buf, GE_XBOARD); return; }
+ gameInfo.result = res;
}
- gameInfo.resultDetails = buf;
+ gameInfo.resultDetails = StrSave(buf);
}
- DisplayFatalError(buf, error, 1);
+ if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; return; }
+ if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
}
}
void
-ReceiveFromProgram(isr, closure, message, count, error)
- InputSourceRef isr;
- VOIDSTAR closure;
- char *message;
- int count;
- int error;
+ReceiveFromProgram (InputSourceRef isr, VOIDSTAR closure, char *message, int count, int error)
{
char *end_str;
char buf[MSG_SIZ];
if (isr != cps->isr) return; /* Killed intentionally */
if (count <= 0) {
if (count == 0) {
- sprintf(buf,
- _("Error: %s chess program (%s) exited unexpectedly"),
- cps->which, cps->program);
- if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
- if(epStatus[forwardMostMove] <= EP_DRAWS) {
+ RemoveInputSource(cps->isr);
+ snprintf(buf, MSG_SIZ, _("Error: %s chess program (%s) exited unexpectedly"),
+ _(cps->which), cps->program);
+ if(LoadError(cps->userError ? NULL : buf, cps)) return; // [HGM] should not generate fatal error during engine load
+ if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
+ if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
+ snprintf(buf, MSG_SIZ, _("%s program exits in draw position (%s)"), _(cps->which), cps->program);
+ if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(GameIsDrawn, buf, GE_XBOARD); return; }
gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
- sprintf(buf, _("%s program exits in draw position (%s)"), cps->which, cps->program);
} else {
- gameInfo.result = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
+ ChessMove res = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
+ if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(res, buf, GE_XBOARD); return; }
+ gameInfo.result = res;
}
- gameInfo.resultDetails = buf;
+ gameInfo.resultDetails = StrSave(buf);
}
- RemoveInputSource(cps->isr);
- DisplayFatalError(buf, 0, 1);
+ if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; return; }
+ if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, 0, 1); else errorExitStatus = 1;
} else {
- sprintf(buf,
- _("Error reading from %s chess program (%s)"),
- cps->which, cps->program);
+ snprintf(buf, MSG_SIZ, _("Error reading from %s chess program (%s)"),
+ _(cps->which), cps->program);
RemoveInputSource(cps->isr);
/* [AS] Program is misbehaving badly... kill it */
cps->pr = NoProc;
}
- DisplayFatalError(buf, error, 1);
+ if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
}
return;
}
-
+
if ((end_str = strchr(message, '\r')) != NULL)
*end_str = NULLCHAR;
if ((end_str = strchr(message, '\n')) != NULL)
*end_str = NULLCHAR;
-
+
if (appData.debugMode) {
TimeMark now; int print = 1;
char *quote = ""; char c; int i;
if(appData.engineComments != 1) { /* [HGM] debug: decide if protocol-violating output is written */
char start = message[0];
if(start >='A' && start <= 'Z') start += 'a' - 'A'; // be tolerant to capitalizing
- if(sscanf(message, "%d%c%d%d%d", &i, &c, &i, &i, &i) != 5 &&
+ if(sscanf(message, "%d%c%d%d%d", &i, &c, &i, &i, &i) != 5 &&
sscanf(message, "move %c", &c)!=1 && sscanf(message, "offer%c", &c)!=1 &&
sscanf(message, "resign%c", &c)!=1 && sscanf(message, "feature %c", &c)!=1 &&
sscanf(message, "error %c", &c)!=1 && sscanf(message, "illegal %c", &c)!=1 &&
sscanf(message, "tell%c", &c)!=1 && sscanf(message, "0-1 %c", &c)!=1 &&
sscanf(message, "1-0 %c", &c)!=1 && sscanf(message, "1/2-1/2 %c", &c)!=1 &&
- sscanf(message, "pong %c", &c)!=1 && start != '#')
- { quote = "# "; print = (appData.engineComments == 2); }
+ sscanf(message, "setboard %c", &c)!=1 && sscanf(message, "setup %c", &c)!=1 &&
+ sscanf(message, "hint: %c", &c)!=1 &&
+ sscanf(message, "pong %c", &c)!=1 && start != '#') {
+ quote = appData.engineComments == 2 ? "# " : "### NON-COMPLIANT! ### ";
+ print = (appData.engineComments >= 2);
+ }
message[0] = start; // restore original message
}
if(print) {
GetTimeMark(&now);
- fprintf(debugFP, "%ld <%-6s: %s%s\n",
- SubtractTimeMarks(&now, &programStartTime), cps->which,
+ fprintf(debugFP, "%ld <%-6s: %s%s\n",
+ SubtractTimeMarks(&now, &programStartTime), cps->which,
quote,
message);
+ if(serverFP)
+ fprintf(serverFP, "%ld <%-6s: %s%s\n",
+ SubtractTimeMarks(&now, &programStartTime), cps->which,
+ quote,
+ message), fflush(serverFP);
}
}
/* [DM] if icsEngineAnalyze is active we block all whisper and kibitz output, because nobody want to see this */
if (appData.icsEngineAnalyze) {
if (strstr(message, "whisper") != NULL ||
- strstr(message, "kibitz") != NULL ||
+ strstr(message, "kibitz") != NULL ||
strstr(message, "tellics") != NULL) return;
}
void
-SendTimeControl(cps, mps, tc, inc, sd, st)
- ChessProgramState *cps;
- int mps, inc, sd, st;
- long tc;
+SendTimeControl (ChessProgramState *cps, int mps, long tc, int inc, int sd, int st)
{
char buf[MSG_SIZ];
int seconds;
/* GNU Chess 4 has no st command; uses level in a nonstandard way */
seconds = st % 60;
if (seconds == 0) {
- sprintf(buf, "level 1 %d\n", st/60);
+ snprintf(buf, MSG_SIZ, "level 1 %d\n", st/60);
} else {
- sprintf(buf, "level 1 %d:%02d\n", st/60, seconds);
+ snprintf(buf, MSG_SIZ, "level 1 %d:%02d\n", st/60, seconds);
}
} else {
- sprintf(buf, "st %d\n", st);
+ snprintf(buf, MSG_SIZ, "st %d\n", st);
}
} else {
/* Set conventional or incremental time control, using level command */
/* Note old gnuchess bug -- minutes:seconds used to not work.
Fixed in later versions, but still avoid :seconds
when seconds is 0. */
- sprintf(buf, "level %d %ld %d\n", mps, tc/60000, inc/1000);
+ snprintf(buf, MSG_SIZ, "level %d %ld %g\n", mps, tc/60000, inc/1000.);
} else {
- sprintf(buf, "level %d %ld:%02d %d\n", mps, tc/60000,
- seconds, inc/1000);
+ snprintf(buf, MSG_SIZ, "level %d %ld:%02d %g\n", mps, tc/60000,
+ seconds, inc/1000.);
}
}
SendToProgram(buf, cps);
/* Orthogonally, limit search to given depth */
if (sd > 0) {
if (cps->sdKludge) {
- sprintf(buf, "depth\n%d\n", sd);
+ snprintf(buf, MSG_SIZ, "depth\n%d\n", sd);
} else {
- sprintf(buf, "sd %d\n", sd);
+ snprintf(buf, MSG_SIZ, "sd %d\n", sd);
}
SendToProgram(buf, cps);
}
- if(cps->nps > 0) { /* [HGM] nps */
- if(cps->supportsNPS == FALSE) cps->nps = -1; // don't use if engine explicitly says not supported!
+ if(cps->nps >= 0) { /* [HGM] nps */
+ if(cps->supportsNPS == FALSE)
+ cps->nps = -1; // don't use if engine explicitly says not supported!
else {
- sprintf(buf, "nps %d\n", cps->nps);
- SendToProgram(buf, cps);
+ snprintf(buf, MSG_SIZ, "nps %d\n", cps->nps);
+ SendToProgram(buf, cps);
}
}
}
-ChessProgramState *WhitePlayer()
+ChessProgramState *
+WhitePlayer ()
/* [HGM] return pointer to 'first' or 'second', depending on who plays white */
{
- if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b' ||
+ if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b' ||
gameMode == BeginningOfGame || gameMode == MachinePlaysBlack)
return &second;
return &first;
}
void
-SendTimeRemaining(cps, machineWhite)
- ChessProgramState *cps;
- int /*boolean*/ machineWhite;
+SendTimeRemaining (ChessProgramState *cps, int machineWhite)
{
char message[MSG_SIZ];
long time, otime;
}
/* [HGM] translate opponent's time by time-odds factor */
otime = (otime * cps->other->timeOdds) / cps->timeOdds;
- if (appData.debugMode) {
- fprintf(debugFP, "time odds: %d %d \n", cps->timeOdds, cps->other->timeOdds);
- }
if (time <= 0) time = 1;
if (otime <= 0) otime = 1;
-
- sprintf(message, "time %ld\n", time);
+
+ snprintf(message, MSG_SIZ, "time %ld\n", time);
SendToProgram(message, cps);
- sprintf(message, "otim %ld\n", otime);
+ snprintf(message, MSG_SIZ, "otim %ld\n", otime);
SendToProgram(message, cps);
}
+char *
+EngineDefinedVariant (ChessProgramState *cps, int n)
+{ // return name of n-th unknown variant that engine supports
+ static char buf[MSG_SIZ];
+ char *p, *s = cps->variants;
+ if(!s) return NULL;
+ do { // parse string from variants feature
+ VariantClass v;
+ p = strchr(s, ',');
+ if(p) *p = NULLCHAR;
+ v = StringToVariant(s);
+ if(v == VariantNormal && strcmp(s, "normal") && !strstr(s, "_normal")) v = VariantUnknown; // garbage is recognized as normal
+ if(v == VariantUnknown) { // non-standard variant in list of engine-supported variants
+ if(!strcmp(s, "tenjiku") || !strcmp(s, "dai") || !strcmp(s, "dada") || // ignore Alien-Edition variants
+ !strcmp(s, "maka") || !strcmp(s, "tai") || !strcmp(s, "kyoku") ||
+ !strcmp(s, "checkers") || !strcmp(s, "go") || !strcmp(s, "reversi") ||
+ !strcmp(s, "dark") || !strcmp(s, "alien") || !strcmp(s, "multi") || !strcmp(s, "amazons") ) n++;
+ if(--n < 0) safeStrCpy(buf, s, MSG_SIZ);
+ }
+ if(p) *p++ = ',';
+ if(n < 0) return buf;
+ } while(s = p);
+ return NULL;
+}
+
int
-BoolFeature(p, name, loc, cps)
- char **p;
- char *name;
- int *loc;
- ChessProgramState *cps;
+BoolFeature (char **p, char *name, int *loc, ChessProgramState *cps)
{
char buf[MSG_SIZ];
int len = strlen(name);
int val;
+
if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
(*p) += len + 1;
sscanf(*p, "%d", &val);
*loc = (val != 0);
- while (**p && **p != ' ') (*p)++;
- sprintf(buf, "accepted %s\n", name);
+ while (**p && **p != ' ')
+ (*p)++;
+ snprintf(buf, MSG_SIZ, "accepted %s\n", name);
SendToProgram(buf, cps);
return TRUE;
}
}
int
-IntFeature(p, name, loc, cps)
- char **p;
- char *name;
- int *loc;
- ChessProgramState *cps;
+IntFeature (char **p, char *name, int *loc, ChessProgramState *cps)
{
char buf[MSG_SIZ];
int len = strlen(name);
(*p) += len + 1;
sscanf(*p, "%d", loc);
while (**p && **p != ' ') (*p)++;
- sprintf(buf, "accepted %s\n", name);
+ snprintf(buf, MSG_SIZ, "accepted %s\n", name);
SendToProgram(buf, cps);
return TRUE;
}
}
int
-StringFeature(p, name, loc, cps)
- char **p;
- char *name;
- char loc[];
- ChessProgramState *cps;
+StringFeature (char **p, char *name, char **loc, ChessProgramState *cps)
{
char buf[MSG_SIZ];
int len = strlen(name);
if (strncmp((*p), name, len) == 0
&& (*p)[len] == '=' && (*p)[len+1] == '\"') {
(*p) += len + 2;
- sscanf(*p, "%[^\"]", loc);
+ ASSIGN(*loc, *p); // kludge alert: assign rest of line just to be sure allocation is large enough so that sscanf below always fits
+ sscanf(*p, "%[^\"]", *loc);
while (**p && **p != '\"') (*p)++;
if (**p == '\"') (*p)++;
- sprintf(buf, "accepted %s\n", name);
+ snprintf(buf, MSG_SIZ, "accepted %s\n", name);
SendToProgram(buf, cps);
return TRUE;
}
return FALSE;
}
-int
-ParseOption(Option *opt, ChessProgramState *cps)
+int
+ParseOption (Option *opt, ChessProgramState *cps)
// [HGM] options: process the string that defines an engine option, and determine
// name, type, default value, and allowed value range
{
char *p, *q, buf[MSG_SIZ];
int n, min = (-1)<<31, max = 1<<31, def;
+ opt->target = &opt->value; // OK for spin/slider and checkbox
if(p = strstr(opt->name, " -spin ")) {
if((n = sscanf(p, " -spin %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
if(max < min) max = min; // enforce consistency
} else if((p = strstr(opt->name, " -string "))) {
opt->textValue = p+9;
opt->type = TextBox;
+ opt->target = &opt->textValue;
} else if((p = strstr(opt->name, " -file "))) {
// for now -file is a synonym for -string, to already provide compatibility with future polyglots
- opt->textValue = p+7;
- opt->type = TextBox; // FileName;
+ opt->target = opt->textValue = p+7;
+ opt->type = FileName; // FileName;
+ opt->target = &opt->textValue;
} else if((p = strstr(opt->name, " -path "))) {
// for now -file is a synonym for -string, to already provide compatibility with future polyglots
- opt->textValue = p+7;
- opt->type = TextBox; // PathName;
+ opt->target = opt->textValue = p+7;
+ opt->type = PathName; // PathName;
+ opt->target = &opt->textValue;
} else if(p = strstr(opt->name, " -check ")) {
if(sscanf(p, " -check %d", &def) < 1) return FALSE;
opt->value = (def != 0);
opt->type = CheckBox;
- } else if(p = strstr(opt->name, " -combo ")) {
- opt->textValue = (char*) (&cps->comboList[cps->comboCnt]); // cheat with pointer type
+ } else if(p = strstr(opt->name, " -combo ")) {
+ opt->textValue = (char*) (opt->choice = &cps->comboList[cps->comboCnt]); // cheat with pointer type
cps->comboList[cps->comboCnt++] = q = p+8; // holds possible choices
if(*q == '*') cps->comboList[cps->comboCnt-1]++;
opt->value = n = 0;
if(cps->optionSettings && cps->optionSettings[0])
p = strstr(cps->optionSettings, opt->name); else p = NULL;
if(p && (p == cps->optionSettings || p[-1] == ',')) {
- sprintf(buf, "option %s", p);
+ snprintf(buf, MSG_SIZ, "option %s", p);
if(p = strstr(buf, ",")) *p = 0;
+ if(q = strchr(buf, '=')) switch(opt->type) {
+ case ComboBox:
+ for(n=0; n<opt->max; n++)
+ if(!strcmp(((char**)opt->textValue)[n], q+1)) opt->value = n;
+ break;
+ case TextBox:
+ safeStrCpy(opt->textValue, q+1, MSG_SIZ - (opt->textValue - opt->name));
+ break;
+ case Spin:
+ case CheckBox:
+ opt->value = atoi(q+1);
+ default:
+ break;
+ }
strcat(buf, "\n");
SendToProgram(buf, cps);
}
}
void
-FeatureDone(cps, val)
- ChessProgramState* cps;
- int val;
+FeatureDone (ChessProgramState *cps, int val)
{
DelayedEventCallback cb = GetDelayedEvent();
if ((cb == InitBackEnd3 && cps == &first) ||
- (cb == TwoMachinesEventIfReady && cps == &second)) {
+ (cb == SettingsMenuIfReady && cps == &second) ||
+ (cb == LoadEngine) ||
+ (cb == TwoMachinesEventIfReady)) {
CancelDelayedEvent();
ScheduleDelayedEvent(cb, val ? 1 : 3600000);
- }
+ } else if(!val && !cps->reload) ClearOptions(cps); // let 'spurious' done=0 clear engine's option list
cps->initDone = val;
+ if(val) cps->reload = FALSE, RefreshSettingsDialog(cps, val);
}
/* Parse feature command from engine */
void
-ParseFeatures(args, cps)
- char* args;
- ChessProgramState *cps;
+ParseFeatures (char *args, ChessProgramState *cps)
{
char *p = args;
- char *q;
+ char *q = NULL;
int val;
char buf[MSG_SIZ];
if (*p == NULLCHAR) return;
if (BoolFeature(&p, "setboard", &cps->useSetboard, cps)) continue;
- if (BoolFeature(&p, "time", &cps->sendTime, cps)) continue;
- if (BoolFeature(&p, "draw", &cps->sendDrawOffers, cps)) continue;
- if (BoolFeature(&p, "sigint", &cps->useSigint, cps)) continue;
- if (BoolFeature(&p, "sigterm", &cps->useSigterm, cps)) continue;
+ if (BoolFeature(&p, "xedit", &cps->extendedEdit, cps)) continue;
+ if (BoolFeature(&p, "time", &cps->sendTime, cps)) continue;
+ if (BoolFeature(&p, "draw", &cps->sendDrawOffers, cps)) continue;
+ if (BoolFeature(&p, "sigint", &cps->useSigint, cps)) continue;
+ if (BoolFeature(&p, "sigterm", &cps->useSigterm, cps)) continue;
if (BoolFeature(&p, "reuse", &val, cps)) {
/* Engine can disable reuse, but can't enable it if user said no */
if (!val) cps->reuse = FALSE;
if (BoolFeature(&p, "playother", &cps->usePlayother, cps)) continue;
if (BoolFeature(&p, "colors", &cps->useColors, cps)) continue;
if (BoolFeature(&p, "usermove", &cps->useUsermove, cps)) continue;
+ if (BoolFeature(&p, "exclude", &cps->excludeMoves, cps)) continue;
if (BoolFeature(&p, "ics", &cps->sendICS, cps)) continue;
if (BoolFeature(&p, "name", &cps->sendName, cps)) continue;
- if (BoolFeature(&p, "pause", &val, cps)) continue; /* unused at present */
+ if (BoolFeature(&p, "pause", &cps->pause, cps)) continue; // [HGM] pause
if (IntFeature(&p, "done", &val, cps)) {
FeatureDone(cps, val);
continue;
/* End of additions by Tord */
/* [HGM] added features: */
+ if (BoolFeature(&p, "highlight", &cps->highlight, cps)) continue;
if (BoolFeature(&p, "debug", &cps->debug, cps)) continue;
if (BoolFeature(&p, "nps", &cps->supportsNPS, cps)) continue;
if (IntFeature(&p, "level", &cps->maxNrOfSessions, cps)) continue;
if (BoolFeature(&p, "memory", &cps->memSize, cps)) continue;
if (BoolFeature(&p, "smp", &cps->maxCores, cps)) continue;
if (StringFeature(&p, "egt", &cps->egtFormats, cps)) continue;
- if (StringFeature(&p, "option", &(cps->option[cps->nrOptions].name), cps)) {
+ if (StringFeature(&p, "option", &q, cps)) { // read to freshly allocated temp buffer first
+ if(cps->reload) { FREE(q); q = NULL; continue; } // we are reloading because of xreuse
+ FREE(cps->option[cps->nrOptions].name);
+ cps->option[cps->nrOptions].name = q; q = NULL;
if(!ParseOption(&(cps->option[cps->nrOptions++]), cps)) { // [HGM] options: add option feature
- sprintf(buf, "rejected option %s\n", cps->option[--cps->nrOptions].name);
+ snprintf(buf, MSG_SIZ, "rejected option %s\n", cps->option[--cps->nrOptions].name);
SendToProgram(buf, cps);
continue;
}
if(cps->nrOptions >= MAX_OPTIONS) {
cps->nrOptions--;
- sprintf(buf, "%s engine has too many options\n", cps->which);
+ snprintf(buf, MSG_SIZ, _("%s engine has too many options\n"), _(cps->which));
DisplayError(buf, 0);
}
continue;
}
- if (BoolFeature(&p, "smp", &cps->maxCores, cps)) continue;
/* End of additions by HGM */
/* unknown feature: complain and skip */
q = p;
while (*q && *q != '=') q++;
- sprintf(buf, "rejected %.*s\n", q-p, p);
+ snprintf(buf, MSG_SIZ,"rejected %.*s\n", (int)(q-p), p);
SendToProgram(buf, cps);
p = q;
if (*p == '=') {
}
void
-PeriodicUpdatesEvent(newState)
- int newState;
+PeriodicUpdatesEvent (int newState)
{
if (newState == appData.periodicUpdates)
return;
appData.periodicUpdates=newState;
/* Display type changes, so update it now */
- DisplayAnalysis();
+// DisplayAnalysis();
/* Get the ball rolling again... */
if (newState) {
}
void
-PonderNextMoveEvent(newState)
- int newState;
+PonderNextMoveEvent (int newState)
{
if (newState == appData.ponderNextMove) return;
- if (gameMode == EditPosition) EditPositionDone();
+ if (gameMode == EditPosition) EditPositionDone(TRUE);
if (newState) {
SendToProgram("hard\n", &first);
if (gameMode == TwoMachinesPlay) {
}
void
-NewSettingEvent(option, command, value)
- char *command;
- int option, value;
+NewSettingEvent (int option, int *feature, char *command, int value)
{
char buf[MSG_SIZ];
- if (gameMode == EditPosition) EditPositionDone();
- sprintf(buf, "%s%s %d\n", (option ? "option ": ""), command, value);
- SendToProgram(buf, &first);
+ if (gameMode == EditPosition) EditPositionDone(TRUE);
+ snprintf(buf, MSG_SIZ,"%s%s %d\n", (option ? "option ": ""), command, value);
+ if(feature == NULL || *feature) SendToProgram(buf, &first);
if (gameMode == TwoMachinesPlay) {
- SendToProgram(buf, &second);
+ if(feature == NULL || feature[(int*)&second - (int*)&first]) SendToProgram(buf, &second);
}
}
void
-ShowThinkingEvent()
+ShowThinkingEvent ()
// [HGM] thinking: this routine is now also called from "Options -> Engine..." popup
{
static int oldState = 2; // kludge alert! Neither true nor fals, so first time oldState is always updated
int newState = appData.showThinking
// [HGM] thinking: other features now need thinking output as well
|| !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp();
-
+
if (oldState == newState) return;
oldState = newState;
- if (gameMode == EditPosition) EditPositionDone();
+ if (gameMode == EditPosition) EditPositionDone(TRUE);
if (oldState) {
SendToProgram("post\n", &first);
if (gameMode == TwoMachinesPlay) {
}
void
-AskQuestionEvent(title, question, replyPrefix, which)
- char *title; char *question; char *replyPrefix; char *which;
+AskQuestionEvent (char *title, char *question, char *replyPrefix, char *which)
{
ProcRef pr = (which[0] == '1') ? first.pr : second.pr;
if (pr == NoProc) return;
}
void
-DisplayMove(moveNumber)
- int moveNumber;
+TypeInEvent (char firstChar)
+{
+ if ((gameMode == BeginningOfGame && !appData.icsActive) ||
+ gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack ||
+ gameMode == AnalyzeMode || gameMode == EditGame ||
+ gameMode == EditPosition || gameMode == IcsExamining ||
+ gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
+ isdigit(firstChar) && // [HGM] movenum: allow typing in of move nr in 'passive' modes
+ ( gameMode == AnalyzeFile || gameMode == PlayFromGameFile ||
+ gameMode == IcsObserving || gameMode == TwoMachinesPlay ) ||
+ gameMode == Training) PopUpMoveDialog(firstChar);
+}
+
+void
+TypeInDoneEvent (char *move)
+{
+ Board board;
+ int n, fromX, fromY, toX, toY;
+ char promoChar;
+ ChessMove moveType;
+
+ // [HGM] FENedit
+ if(gameMode == EditPosition && ParseFEN(board, &n, move, TRUE) ) {
+ EditPositionPasteFEN(move);
+ return;
+ }
+ // [HGM] movenum: allow move number to be typed in any mode
+ if(sscanf(move, "%d", &n) == 1 && n != 0 ) {
+ ToNrEvent(2*n-1);
+ return;
+ }
+ // undocumented kludge: allow command-line option to be typed in!
+ // (potentially fatal, and does not implement the effect of the option.)
+ // should only be used for options that are values on which future decisions will be made,
+ // and definitely not on options that would be used during initialization.
+ if(strstr(move, "!!! -") == move) {
+ ParseArgsFromString(move+4);
+ return;
+ }
+
+ if (gameMode != EditGame && currentMove != forwardMostMove &&
+ gameMode != Training) {
+ DisplayMoveError(_("Displayed move is not current"));
+ } else {
+ int ok = ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove,
+ &moveType, &fromX, &fromY, &toX, &toY, &promoChar);
+ if(!ok && move[0] >= 'a') { move[0] += 'A' - 'a'; ok = 2; } // [HGM] try also capitalized
+ if (ok==1 || ok && ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove,
+ &moveType, &fromX, &fromY, &toX, &toY, &promoChar)) {
+ UserMoveEvent(fromX, fromY, toX, toY, promoChar);
+ } else {
+ DisplayMoveError(_("Could not parse move"));
+ }
+ }
+}
+
+void
+DisplayMove (int moveNumber)
{
char message[MSG_SIZ];
char res[MSG_SIZ];
char cpThinkOutput[MSG_SIZ];
if(appData.noGUI) return; // [HGM] fast: suppress display of moves
-
- if (moveNumber == forwardMostMove - 1 ||
+
+ if (moveNumber == forwardMostMove - 1 ||
gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
- safeStrCpy(cpThinkOutput, thinkOutput, sizeof(cpThinkOutput));
+ safeStrCpy(cpThinkOutput, thinkOutput, sizeof(cpThinkOutput)/sizeof(cpThinkOutput[0]));
if (strchr(cpThinkOutput, '\n')) {
*strchr(cpThinkOutput, '\n') = NULLCHAR;
if (moveNumber == forwardMostMove - 1 &&
gameInfo.resultDetails != NULL) {
if (gameInfo.resultDetails[0] == NULLCHAR) {
- sprintf(res, " %s", PGNResult(gameInfo.result));
+ snprintf(res, MSG_SIZ, " %s", PGNResult(gameInfo.result));
} else {
- sprintf(res, " {%s} %s",
- gameInfo.resultDetails, PGNResult(gameInfo.result));
+ snprintf(res, MSG_SIZ, " {%s} %s",
+ T_(gameInfo.resultDetails), PGNResult(gameInfo.result));
}
} else {
res[0] = NULLCHAR;
if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
DisplayMessage(res, cpThinkOutput);
} else {
- sprintf(message, "%d.%s%s%s", moveNumber / 2 + 1,
+ snprintf(message, MSG_SIZ, "%d.%s%s%s", moveNumber / 2 + 1,
WhiteOnMove(moveNumber) ? " " : ".. ",
parseList[moveNumber], res);
DisplayMessage(message, cpThinkOutput);
}
void
-DisplayAnalysisText(text)
- char *text;
+DisplayComment (int moveNumber, char *text)
{
- char buf[MSG_SIZ];
-
- if (gameMode == AnalyzeMode || gameMode == AnalyzeFile
- || appData.icsEngineAnalyze) {
- sprintf(buf, "Analysis (%s)", first.tidy);
- AnalysisPopUp(buf, text);
- }
-}
-
-static int
-only_one_move(str)
- char *str;
-{
- while (*str && isspace(*str)) ++str;
- while (*str && !isspace(*str)) ++str;
- if (!*str) return 1;
- while (*str && isspace(*str)) ++str;
- if (!*str) return 1;
- return 0;
-}
+ char title[MSG_SIZ];
-void
-DisplayAnalysis()
-{
- char buf[MSG_SIZ];
- char lst[MSG_SIZ / 2];
- double nps;
- static char *xtra[] = { "", " (--)", " (++)" };
- int h, m, s, cs;
-
- if (programStats.time == 0) {
- programStats.time = 1;
- }
-
- if (programStats.got_only_move) {
- safeStrCpy(buf, programStats.movelist, sizeof(buf));
+ if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
+ safeStrCpy(title, "Comment", sizeof(title)/sizeof(title[0]));
} else {
- safeStrCpy( lst, programStats.movelist, sizeof(lst));
-
- nps = (u64ToDouble(programStats.nodes) /
- ((double)programStats.time /100.0));
-
- cs = programStats.time % 100;
- s = programStats.time / 100;
- h = (s / (60*60));
- s = s - h*60*60;
- m = (s/60);
- s = s - m*60;
-
- if (programStats.moves_left > 0 && appData.periodicUpdates) {
- if (programStats.move_name[0] != NULLCHAR) {
- sprintf(buf, "depth=%d %d/%d(%s) %+.2f %s%s\nNodes: " u64Display " NPS: %d\nTime: %02d:%02d:%02d.%02d",
- programStats.depth,
- programStats.nr_moves-programStats.moves_left,
- programStats.nr_moves, programStats.move_name,
- ((float)programStats.score)/100.0, lst,
- only_one_move(lst)?
- xtra[programStats.got_fail] : "",
- (u64)programStats.nodes, (int)nps, h, m, s, cs);
- } else {
- sprintf(buf, "depth=%d %d/%d %+.2f %s%s\nNodes: " u64Display " NPS: %d\nTime: %02d:%02d:%02d.%02d",
- programStats.depth,
- programStats.nr_moves-programStats.moves_left,
- programStats.nr_moves, ((float)programStats.score)/100.0,
- lst,
- only_one_move(lst)?
- xtra[programStats.got_fail] : "",
- (u64)programStats.nodes, (int)nps, h, m, s, cs);
- }
- } else {
- sprintf(buf, "depth=%d %+.2f %s%s\nNodes: " u64Display " NPS: %d\nTime: %02d:%02d:%02d.%02d",
- programStats.depth,
- ((float)programStats.score)/100.0,
- lst,
- only_one_move(lst)?
- xtra[programStats.got_fail] : "",
- (u64)programStats.nodes, (int)nps, h, m, s, cs);
- }
+ snprintf(title,MSG_SIZ, "Comment on %d.%s%s", moveNumber / 2 + 1,
+ WhiteOnMove(moveNumber) ? " " : ".. ",
+ parseList[moveNumber]);
}
- DisplayAnalysisText(buf);
-}
-
-void
-DisplayComment(moveNumber, text)
- int moveNumber;
- char *text;
-{
- char title[MSG_SIZ];
- char buf[8000]; // comment can be long!
- int score, depth;
-
- if( appData.autoDisplayComment ) {
- if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
- strcpy(title, "Comment");
- } else {
- sprintf(title, "Comment on %d.%s%s", moveNumber / 2 + 1,
- WhiteOnMove(moveNumber) ? " " : ".. ",
- parseList[moveNumber]);
- }
- // [HGM] PV info: display PV info together with (or as) comment
- if(moveNumber >= 0 && (depth = pvInfoList[moveNumber].depth) > 0) {
- if(text == NULL) text = "";
- score = pvInfoList[moveNumber].score;
- sprintf(buf, "%s%.2f/%d %d\n%s", score>0 ? "+" : "", score/100.,
- depth, (pvInfoList[moveNumber].time+50)/100, text);
- text = buf;
- }
- } else title[0] = 0;
-
- if (text != NULL)
+ if (text != NULL && (appData.autoDisplayComment || commentUp))
CommentPopUp(title, text);
}
* ioctl, which does not work properly on some flavors of Unix.
*/
void
-Attention(cps)
- ChessProgramState *cps;
+Attention (ChessProgramState *cps)
{
#if ATTENTION
if (!cps->useSigint) return;
}
int
-CheckFlags()
+CheckFlags ()
{
if (whiteTimeRemaining <= 0) {
if (!whiteFlag) {
}
void
-CheckTimeControl()
+CheckTimeControl ()
{
- if (!appData.clockMode || appData.icsActive ||
+ if (!appData.clockMode || appData.icsActive || searchTime || // [HGM] st: no inc in st mode
gameMode == PlayFromGameFile || forwardMostMove == 0) return;
/*
* add time to clocks when time control is achieved ([HGM] now also used for increment)
*/
- if ( !WhiteOnMove(forwardMostMove) )
+ if ( !WhiteOnMove(forwardMostMove) ) {
/* White made time control */
- whiteTimeRemaining += GetTimeQuota((forwardMostMove-1)/2)
+ lastWhite -= whiteTimeRemaining; // [HGM] contains start time, socalculate thinking time
+ whiteTimeRemaining += GetTimeQuota((forwardMostMove-whiteStartMove-1)/2, lastWhite, whiteTC)
/* [HGM] time odds: correct new time quota for time odds! */
/ WhitePlayer()->timeOdds;
- else
+ lastBlack = blackTimeRemaining; // [HGM] leave absolute time (after quota), so next switch we can us it to calculate thinking time
+ } else {
+ lastBlack -= blackTimeRemaining;
/* Black made time control */
- blackTimeRemaining += GetTimeQuota((forwardMostMove-1)/2)
+ blackTimeRemaining += GetTimeQuota((forwardMostMove-blackStartMove-1)/2, lastBlack, blackTC)
/ WhitePlayer()->other->timeOdds;
+ lastWhite = whiteTimeRemaining;
+ }
}
void
-DisplayBothClocks()
+DisplayBothClocks ()
{
int wom = gameMode == EditPosition ?
!blackPlaysFirst : WhiteOnMove(currentMove);
/* Get the current time as a TimeMark */
void
-GetTimeMark(tm)
- TimeMark *tm;
+GetTimeMark (TimeMark *tm)
{
#if HAVE_GETTIMEOFDAY
struct timezone timeZone;
gettimeofday(&timeVal, &timeZone);
- tm->sec = (long) timeVal.tv_sec;
+ tm->sec = (long) timeVal.tv_sec;
tm->ms = (int) (timeVal.tv_usec / 1000L);
#else /*!HAVE_GETTIMEOFDAY*/
time marks. We assume the difference will fit in a long!
*/
long
-SubtractTimeMarks(tm2, tm1)
- TimeMark *tm2, *tm1;
+SubtractTimeMarks (TimeMark *tm2, TimeMark *tm1)
{
return 1000L*(tm2->sec - tm1->sec) +
(long) (tm2->ms - tm1->ms);
* We give the human user a slight advantage if he is playing white---the
* clocks don't run until he makes his first move, so it takes zero time.
* Also, we don't account for network lag, so we could get out of sync
- * with GNU Chess's clock -- but then, referees are always right.
+ * with GNU Chess's clock -- but then, referees are always right.
*/
static TimeMark tickStartTM;
static long intendedTickLength;
long
-NextTickLength(timeRemaining)
- long timeRemaining;
+NextTickLength (long timeRemaining)
{
long nominalTickLength, nextTickLength;
/* Adjust clock one minute up or down */
void
-AdjustClock(Boolean which, int dir)
+AdjustClock (Boolean which, int dir)
{
+ if(appData.autoCallFlag) { DisplayError(_("Clock adjustment not allowed in auto-flag mode"), 0); return; }
if(which) blackTimeRemaining += 60000*dir;
else whiteTimeRemaining += 60000*dir;
DisplayBothClocks();
+ adjustedClock = TRUE;
}
/* Stop clocks and reset to a fresh time control */
void
-ResetClocks()
+ResetClocks ()
{
(void) StopClockTimer();
if (appData.icsActive) {
whiteTimeRemaining = blackTimeRemaining = 0;
+ } else if (searchTime) {
+ whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
+ blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
} else { /* [HGM] correct new time quote for time odds */
- whiteTimeRemaining = GetTimeQuota(-1) / WhitePlayer()->timeOdds;
- blackTimeRemaining = GetTimeQuota(-1) / WhitePlayer()->other->timeOdds;
+ whiteTC = blackTC = fullTimeControlString;
+ whiteTimeRemaining = GetTimeQuota(-1, 0, whiteTC) / WhitePlayer()->timeOdds;
+ blackTimeRemaining = GetTimeQuota(-1, 0, blackTC) / WhitePlayer()->other->timeOdds;
}
if (whiteFlag || blackFlag) {
DisplayTitle("");
whiteFlag = blackFlag = FALSE;
}
+ lastWhite = lastBlack = whiteStartMove = blackStartMove = 0;
DisplayBothClocks();
+ adjustedClock = FALSE;
}
#define FUDGE 25 /* 25ms = 1/40 sec; should be plenty even for 50 Hz clocks */
/* Decrement running clock by amount of time that has passed */
void
-DecrementClocks()
+DecrementClocks ()
{
long timeRemaining;
long lastTickLength, fudge;
if (!appData.clockMode) return;
if (gameMode==AnalyzeMode || gameMode == AnalyzeFile) return;
-
+
GetTimeMark(&now);
lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
if (WhiteOnMove(forwardMostMove)) {
if(whiteNPS >= 0) lastTickLength = 0;
timeRemaining = whiteTimeRemaining -= lastTickLength;
+ if(timeRemaining < 0 && !appData.icsActive) {
+ GetTimeQuota((forwardMostMove-whiteStartMove-1)/2, 0, whiteTC); // sets suddenDeath & nextSession;
+ if(suddenDeath) { // [HGM] if we run out of a non-last incremental session, go to the next
+ whiteStartMove = forwardMostMove; whiteTC = nextSession;
+ lastWhite= timeRemaining = whiteTimeRemaining += GetTimeQuota(-1, 0, whiteTC);
+ }
+ }
DisplayWhiteClock(whiteTimeRemaining - fudge,
- WhiteOnMove(currentMove));
+ WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
} else {
if(blackNPS >= 0) lastTickLength = 0;
timeRemaining = blackTimeRemaining -= lastTickLength;
+ if(timeRemaining < 0 && !appData.icsActive) { // [HGM] if we run out of a non-last incremental session, go to the next
+ GetTimeQuota((forwardMostMove-blackStartMove-1)/2, 0, blackTC);
+ if(suddenDeath) {
+ blackStartMove = forwardMostMove;
+ lastBlack = timeRemaining = blackTimeRemaining += GetTimeQuota(-1, 0, blackTC=nextSession);
+ }
+ }
DisplayBlackClock(blackTimeRemaining - fudge,
- !WhiteOnMove(currentMove));
+ !WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
}
-
if (CheckFlags()) return;
-
+
+ if(twoBoards) { // count down secondary board's clocks as well
+ activePartnerTime -= lastTickLength;
+ partnerUp = 1;
+ if(activePartner == 'W')
+ DisplayWhiteClock(activePartnerTime, TRUE); // the counting clock is always the highlighted one!
+ else
+ DisplayBlackClock(activePartnerTime, TRUE);
+ partnerUp = 0;
+ }
+
tickStartTM = now;
intendedTickLength = NextTickLength(timeRemaining - fudge) + fudge;
StartClockTimer(intendedTickLength);
/* if the time remaining has fallen below the alarm threshold, sound the
* alarm. if the alarm has sounded and (due to a takeback or time control
* with increment) the time remaining has increased to a level above the
- * threshold, reset the alarm so it can sound again.
+ * threshold, reset the alarm so it can sound again.
*/
-
+
if (appData.icsActive && appData.icsAlarm) {
/* make sure we are dealing with the user's clock */
if (alarmSounded && (timeRemaining > appData.icsAlarmTime)) {
alarmSounded = FALSE;
- } else if (!alarmSounded && (timeRemaining <= appData.icsAlarmTime)) {
+ } else if (!alarmSounded && (timeRemaining <= appData.icsAlarmTime)) {
PlayAlarmSound();
alarmSounded = TRUE;
}
from the color that is *not* on move now.
*/
void
-SwitchClocks()
+SwitchClocks (int newMoveNr)
{
long lastTickLength;
TimeMark now;
if (StopClockTimer() && appData.clockMode) {
lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
- if (WhiteOnMove(forwardMostMove)) {
+ if (!WhiteOnMove(forwardMostMove)) {
if(blackNPS >= 0) lastTickLength = 0;
blackTimeRemaining -= lastTickLength;
/* [HGM] PGNtime: save time for PGN file if engine did not give it */
-// if(pvInfoList[forwardMostMove-1].time == -1)
- pvInfoList[forwardMostMove-1].time = // use GUI time
+// if(pvInfoList[forwardMostMove].time == -1)
+ pvInfoList[forwardMostMove].time = // use GUI time
(timeRemaining[1][forwardMostMove-1] - blackTimeRemaining)/10;
} else {
if(whiteNPS >= 0) lastTickLength = 0;
whiteTimeRemaining -= lastTickLength;
/* [HGM] PGNtime: save time for PGN file if engine did not give it */
-// if(pvInfoList[forwardMostMove-1].time == -1)
- pvInfoList[forwardMostMove-1].time =
+// if(pvInfoList[forwardMostMove].time == -1)
+ pvInfoList[forwardMostMove].time =
(timeRemaining[0][forwardMostMove-1] - whiteTimeRemaining)/10;
}
flagged = CheckFlags();
}
+ forwardMostMove = newMoveNr; // [HGM] race: change stm when no timer interrupt scheduled
CheckTimeControl();
if (flagged || !appData.clockMode) return;
break;
}
+ if (searchTime) { // [HGM] st: set clock of player that has to move to max time
+ if(WhiteOnMove(forwardMostMove))
+ whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
+ else blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
+ }
+
tickStartTM = now;
intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
whiteTimeRemaining : blackTimeRemaining);
StartClockTimer(intendedTickLength);
}
-
+
/* Stop both clocks */
void
-StopClocks()
-{
+StopClocks ()
+{
long lastTickLength;
TimeMark now;
}
CheckFlags();
}
-
+
/* Start clock of player on move. Time may have been reset, so
if clock is already running, stop and restart it. */
void
-StartClocks()
+StartClocks ()
{
(void) StopClockTimer(); /* in case it was running already */
DisplayBothClocks();
whiteTimeRemaining : blackTimeRemaining);
/* [HGM] nps: figure out nps factors, by determining which engine plays white and/or black once and for all */
- whiteNPS = blackNPS = -1;
+ whiteNPS = blackNPS = -1;
if(gameMode == MachinePlaysWhite || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w'
|| appData.zippyPlay && gameMode == IcsPlayingBlack) // first (perhaps only) engine has white
whiteNPS = first.nps;
}
char *
-TimeString(ms)
- long ms;
+TimeString (long ms)
{
long second, minute, hour, day;
char *sign = "";
static char buf[32];
-
+
if (ms > 0 && ms <= 9900) {
/* convert milliseconds to tenths, rounding up */
double tenths = floor( ((double)(ms + 99L)) / 100.00 );
- sprintf(buf, " %03.1f ", tenths/10.0);
+ snprintf(buf,sizeof(buf)/sizeof(buf[0]), " %03.1f ", tenths/10.0);
return buf;
}
sign = "-";
second = -second;
}
-
+
day = second / (60 * 60 * 24);
second = second % (60 * 60 * 24);
hour = second / (60 * 60);
second = second % (60 * 60);
minute = second / 60;
second = second % 60;
-
+
if (day > 0)
- sprintf(buf, " %s%ld:%02ld:%02ld:%02ld ",
+ snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%ld:%02ld:%02ld:%02ld ",
sign, day, hour, minute, second);
else if (hour > 0)
- sprintf(buf, " %s%ld:%02ld:%02ld ", sign, hour, minute, second);
+ snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%ld:%02ld:%02ld ", sign, hour, minute, second);
else
- sprintf(buf, " %s%2ld:%02ld ", sign, minute, second);
-
+ snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%2ld:%02ld ", sign, minute, second);
+
return buf;
}
* This is necessary because some C libraries aren't ANSI C compliant yet.
*/
char *
-StrStr(string, match)
- char *string, *match;
+StrStr (char *string, char *match)
{
int i, length;
-
+
length = strlen(match);
-
+
for (i = strlen(string) - length; i >= 0; i--, string++)
if (!strncmp(match, string, length))
return string;
-
+
return NULL;
}
char *
-StrCaseStr(string, match)
- char *string, *match;
+StrCaseStr (char *string, char *match)
{
int i, j, length;
-
+
length = strlen(match);
-
+
for (i = strlen(string) - length; i >= 0; i--, string++) {
for (j = 0; j < length; j++) {
if (ToLower(match[j]) != ToLower(string[j]))
#ifndef _amigados
int
-StrCaseCmp(s1, s2)
- char *s1, *s2;
+StrCaseCmp (char *s1, char *s2)
{
char c1, c2;
-
+
for (;;) {
c1 = ToLower(*s1++);
c2 = ToLower(*s2++);
int
-ToLower(c)
- int c;
+ToLower (int c)
{
return isupper(c) ? tolower(c) : c;
}
int
-ToUpper(c)
- int c;
+ToUpper (int c)
{
return islower(c) ? toupper(c) : c;
}
#endif /* !_amigados */
char *
-StrSave(s)
- char *s;
+StrSave (char *s)
{
- char *ret;
+ char *ret;
- if ((ret = (char *) malloc(strlen(s) + 1))) {
- strcpy(ret, s);
+ if ((ret = (char *) malloc(strlen(s) + 1)))
+ {
+ safeStrCpy(ret, s, strlen(s)+1);
}
- return ret;
+ return ret;
}
char *
-StrSavePtr(s, savePtr)
- char *s, **savePtr;
+StrSavePtr (char *s, char **savePtr)
{
if (*savePtr) {
free(*savePtr);
}
if ((*savePtr = (char *) malloc(strlen(s) + 1))) {
- strcpy(*savePtr, s);
+ safeStrCpy(*savePtr, s, strlen(s)+1);
}
return(*savePtr);
}
char *
-PGNDate()
+PGNDate ()
{
time_t clock;
struct tm *tm;
clock = time((time_t *)NULL);
tm = localtime(&clock);
- sprintf(buf, "%04d.%02d.%02d",
+ snprintf(buf, MSG_SIZ, "%04d.%02d.%02d",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
return StrSave(buf);
}
char *
-PositionToFEN(move, overrideCastling)
- int move;
- char *overrideCastling;
+PositionToFEN (int move, char *overrideCastling, int moveCounts)
{
int i, j, fromX, fromY, toX, toY;
- int whiteToPlay;
- char buf[128];
+ int whiteToPlay, haveRights = nrCastlingRights;
+ char buf[MSG_SIZ];
char *p, *q;
int emptycount;
ChessSquare piece;
/* Piece placement data */
for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
+ if(MSG_SIZ - (p - buf) < BOARD_RGHT - BOARD_LEFT + 20) { *p = 0; return StrSave(buf); }
emptycount = 0;
for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
if (boards[move][i][j] == EmptySquare) {
if(PieceToChar(piece) == '+') {
/* [HGM] write promoted pieces as '+<unpromoted>' (Shogi) */
*p++ = '+';
- piece = (ChessSquare)(DEMOTED piece);
- }
- *p++ = PieceToChar(piece);
+ piece = (ChessSquare)(CHUDEMOTED(piece));
+ }
+ *p++ = (piece == DarkSquare ? '*' : PieceToChar(piece));
+ if(*p = PieceSuffix(piece)) p++;
if(p[-1] == '~') {
/* [HGM] flag promoted pieces as '<promoted>~' (Crazyhouse) */
- p[-1] = PieceToChar((ChessSquare)(DEMOTED piece));
+ p[-1] = PieceToChar((ChessSquare)(CHUDEMOTED(piece)));
*p++ = '~';
}
}
*p++ = whiteToPlay ? 'w' : 'b';
*p++ = ' ';
+ if(pieceDesc[WhiteKing] && strchr(pieceDesc[WhiteKing], 'i') && !strchr(pieceDesc[WhiteKing], 'O')) { // redefined without castling
+ haveRights = 0; q = p;
+ for(i=BOARD_RGHT-1; i>=BOARD_LEFT; i--) {
+ piece = boards[move][0][i];
+ if(piece >= WhitePawn && piece <= WhiteKing && pieceDesc[piece] && strchr(pieceDesc[piece], 'i')) { // piece with initial move
+ if(!(boards[move][TOUCHED_W] & 1<<i)) *p++ = 'A' + i; // print file ID if it has not moved
+ }
+ }
+ for(i=BOARD_RGHT-1; i>=BOARD_LEFT; i--) {
+ piece = boards[move][BOARD_HEIGHT-1][i];
+ if(piece >= BlackPawn && piece <= BlackKing && pieceDesc[piece] && strchr(pieceDesc[piece], 'i')) { // piece with initial move
+ if(!(boards[move][TOUCHED_B] & 1<<i)) *p++ = 'a' + i; // print file ID if it has not moved
+ }
+ }
+ if(p == q) *p++ = '-';
+ *p++ = ' ';
+ }
+
if(q = overrideCastling) { // [HGM] FRC: override castling & e.p fields for non-compliant engines
- while(*p++ = *q++); if(q != overrideCastling+1) p[-1] = ' ';
+ while(*p++ = *q++); if(q != overrideCastling+1) p[-1] = ' '; else --p;
} else {
- if(nrCastlingRights) {
+ if(haveRights) {
+ int handW=0, handB=0;
+ if(gameInfo.variant == VariantSChess) { // for S-Chess, all virgin backrank pieces must be listed
+ for(i=0; i<BOARD_HEIGHT; i++) handW += boards[move][i][BOARD_RGHT]; // count white held pieces
+ for(i=0; i<BOARD_HEIGHT; i++) handB += boards[move][i][BOARD_LEFT-1]; // count black held pieces
+ }
q = p;
- if(gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) {
+ if(appData.fischerCastling) {
+ if(handW) { // in shuffle S-Chess simply dump all virgin pieces
+ for(i=BOARD_RGHT-1; i>=BOARD_LEFT; i--)
+ if(boards[move][VIRGIN][i] & VIRGIN_W) *p++ = i + AAA + 'A' - 'a';
+ } else {
/* [HGM] write directly from rights */
- if(castlingRights[move][2] >= 0 &&
- castlingRights[move][0] >= 0 )
- *p++ = castlingRights[move][0] + AAA + 'A' - 'a';
- if(castlingRights[move][2] >= 0 &&
- castlingRights[move][1] >= 0 )
- *p++ = castlingRights[move][1] + AAA + 'A' - 'a';
- if(castlingRights[move][5] >= 0 &&
- castlingRights[move][3] >= 0 )
- *p++ = castlingRights[move][3] + AAA;
- if(castlingRights[move][5] >= 0 &&
- castlingRights[move][4] >= 0 )
- *p++ = castlingRights[move][4] + AAA;
+ if(boards[move][CASTLING][2] != NoRights &&
+ boards[move][CASTLING][0] != NoRights )
+ *p++ = boards[move][CASTLING][0] + AAA + 'A' - 'a';
+ if(boards[move][CASTLING][2] != NoRights &&
+ boards[move][CASTLING][1] != NoRights )
+ *p++ = boards[move][CASTLING][1] + AAA + 'A' - 'a';
+ }
+ if(handB) {
+ for(i=BOARD_RGHT-1; i>=BOARD_LEFT; i--)
+ if(boards[move][VIRGIN][i] & VIRGIN_B) *p++ = i + AAA;
+ } else {
+ if(boards[move][CASTLING][5] != NoRights &&
+ boards[move][CASTLING][3] != NoRights )
+ *p++ = boards[move][CASTLING][3] + AAA;
+ if(boards[move][CASTLING][5] != NoRights &&
+ boards[move][CASTLING][4] != NoRights )
+ *p++ = boards[move][CASTLING][4] + AAA;
+ }
} else {
/* [HGM] write true castling rights */
if( nrCastlingRights == 6 ) {
- if(castlingRights[move][0] == BOARD_RGHT-1 &&
- castlingRights[move][2] >= 0 ) *p++ = 'K';
- if(castlingRights[move][1] == BOARD_LEFT &&
- castlingRights[move][2] >= 0 ) *p++ = 'Q';
- if(castlingRights[move][3] == BOARD_RGHT-1 &&
- castlingRights[move][5] >= 0 ) *p++ = 'k';
- if(castlingRights[move][4] == BOARD_LEFT &&
- castlingRights[move][5] >= 0 ) *p++ = 'q';
+ int q, k=0;
+ if(boards[move][CASTLING][0] != NoRights &&
+ boards[move][CASTLING][2] != NoRights ) k = 1, *p++ = 'K';
+ q = (boards[move][CASTLING][1] != NoRights &&
+ boards[move][CASTLING][2] != NoRights );
+ if(handW) { // for S-Chess with pieces in hand, list virgin pieces between K and Q
+ for(i=BOARD_RGHT-1-k; i>=BOARD_LEFT+q; i--)
+ if((boards[move][0][i] != WhiteKing || k+q == 0) &&
+ boards[move][VIRGIN][i] & VIRGIN_W) *p++ = i + AAA + 'A' - 'a';
+ }
+ if(q) *p++ = 'Q';
+ k = 0;
+ if(boards[move][CASTLING][3] != NoRights &&
+ boards[move][CASTLING][5] != NoRights ) k = 1, *p++ = 'k';
+ q = (boards[move][CASTLING][4] != NoRights &&
+ boards[move][CASTLING][5] != NoRights );
+ if(handB) {
+ for(i=BOARD_RGHT-1-k; i>=BOARD_LEFT+q; i--)
+ if((boards[move][BOARD_HEIGHT-1][i] != BlackKing || k+q == 0) &&
+ boards[move][VIRGIN][i] & VIRGIN_B) *p++ = i + AAA;
+ }
+ if(q) *p++ = 'q';
}
}
if (q == p) *p++ = '-'; /* No castling rights */
}
if(gameInfo.variant != VariantShogi && gameInfo.variant != VariantXiangqi &&
- gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier ) {
+ gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier &&
+ gameInfo.variant != VariantMakruk && gameInfo.variant != VariantASEAN ) {
/* En passant target square */
if (move > backwardMostMove) {
fromX = moveList[move - 1][0] - AAA;
} else {
*p++ = '-';
}
+ } else if(move == backwardMostMove) {
+ // [HGM] perhaps we should always do it like this, and forget the above?
+ if((signed char)boards[move][EP_STATUS] >= 0) {
+ *p++ = boards[move][EP_STATUS] + AAA;
+ *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
+ } else {
+ *p++ = '-';
+ }
} else {
*p++ = '-';
}
}
}
- /* [HGM] find reversible plies */
+ if(moveCounts)
{ int i = 0, j=move;
+ /* [HGM] find reversible plies */
if (appData.debugMode) { int k;
fprintf(debugFP, "write FEN 50-move: %d %d %d\n", initialRulePlies, forwardMostMove, backwardMostMove);
for(k=backwardMostMove; k<=forwardMostMove; k++)
- fprintf(debugFP, "e%d. p=%d\n", k, epStatus[k]);
+ fprintf(debugFP, "e%d. p=%d\n", k, (signed char)boards[k][EP_STATUS]);
}
- while(j > backwardMostMove && epStatus[j] <= EP_NONE) j--,i++;
+ while(j > backwardMostMove && (signed char)boards[j][EP_STATUS] <= EP_NONE) j--,i++;
if( j == backwardMostMove ) i += initialRulePlies;
sprintf(p, "%d ", i);
p += i>=100 ? 4 : i >= 10 ? 3 : 2;
- }
- /* Fullmove number */
- sprintf(p, "%d", (move / 2) + 1);
-
+
+ /* Fullmove number */
+ sprintf(p, "%d", (move / 2) + 1);
+ } else *--p = NULLCHAR;
+
return StrSave(buf);
}
Boolean
-ParseFEN(board, blackPlaysFirst, fen)
- Board board;
- int *blackPlaysFirst;
- char *fen;
+ParseFEN (Board board, int *blackPlaysFirst, char *fen, Boolean autoSize)
{
- int i, j;
- char *p;
- int emptycount;
- ChessSquare piece;
+ int i, j, k, w=0, subst=0, shuffle=0, wKingRank = -1, bKingRank = -1;
+ char *p, c;
+ int emptycount, virgin[BOARD_FILES];
+ ChessSquare piece, king = (gameInfo.variant == VariantKnightmate ? WhiteUnicorn : WhiteKing);
p = fen;
- /* [HGM] by default clear Crazyhouse holdings, if present */
- if(gameInfo.holdingsWidth) {
- for(i=0; i<BOARD_HEIGHT; i++) {
- board[i][0] = EmptySquare; /* black holdings */
- board[i][BOARD_WIDTH-1] = EmptySquare; /* white holdings */
- board[i][1] = (ChessSquare) 0; /* black counts */
- board[i][BOARD_WIDTH-2] = (ChessSquare) 0; /* white counts */
- }
- }
-
/* Piece placement data */
for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
j = 0;
for (;;) {
- if (*p == '/' || *p == ' ' || (*p == '[' && i == 0) ) {
- if (*p == '/') p++;
+ if (*p == '/' || *p == ' ' || *p == '[' ) {
+ if(j > w) w = j;
emptycount = gameInfo.boardWidth - j;
while (emptycount--)
board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
+ if (*p == '/') p++;
+ else if(autoSize && i != BOARD_HEIGHT-1) { // we stumbled unexpectedly into end of board
+ for(k=i; k<BOARD_HEIGHT; k++) { // too few ranks; shift towards bottom
+ for(j=0; j<BOARD_WIDTH; j++) board[k-i][j] = board[k][j];
+ }
+ appData.NrRanks = gameInfo.boardHeight - i; i=0;
+ }
break;
-#if(BOARD_SIZE >= 10)
+#if(BOARD_FILES >= 10)*0
} else if(*p=='x' || *p=='X') { /* [HGM] X means 10 */
p++; emptycount=10;
if (j + emptycount > gameInfo.boardWidth) return FALSE;
while (emptycount--)
board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
#endif
+ } else if (*p == '*') {
+ board[i][(j++)+gameInfo.holdingsWidth] = DarkSquare; p++;
} else if (isdigit(*p)) {
emptycount = *p++ - '0';
while(isdigit(*p)) emptycount = 10*emptycount + *p++ - '0'; /* [HGM] allow > 9 */
if (j + emptycount > gameInfo.boardWidth) return FALSE;
while (emptycount--)
board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
+ } else if (*p == '<') {
+ if(i == BOARD_HEIGHT-1) shuffle = 1;
+ else if (i != 0 || !shuffle) return FALSE;
+ p++;
+ } else if (shuffle && *p == '>') {
+ p++; // for now ignore closing shuffle range, and assume rank-end
+ } else if (*p == '?') {
+ if (j >= gameInfo.boardWidth) return FALSE;
+ if (i != 0 && i != BOARD_HEIGHT-1) return FALSE; // only on back-rank
+ board[i][(j++)+gameInfo.holdingsWidth] = ClearBoard; p++; subst++; // placeHolder
} else if (*p == '+' || isalpha(*p)) {
+ char *q, *s = SUFFIXES;
if (j >= gameInfo.boardWidth) return FALSE;
if(*p=='+') {
- piece = CharToPiece(*++p);
+ char c = *++p;
+ if(q = strchr(s, p[1])) p++;
+ piece = CharToPiece(c + (q ? 64*(q - s + 1) : 0));
if(piece == EmptySquare) return FALSE; /* unknown piece */
- piece = (ChessSquare) (PROMOTED piece ); p++;
+ piece = (ChessSquare) (CHUPROMOTED(piece)); p++;
if(PieceToChar(piece) != '+') return FALSE; /* unpromotable piece */
- } else piece = CharToPiece(*p++);
+ } else {
+ char c = *p++;
+ if(q = strchr(s, *p)) p++;
+ piece = CharToPiece(c + (q ? 64*(q - s + 1) : 0));
+ }
if(piece==EmptySquare) return FALSE; /* unknown piece */
if(*p == '~') { /* [HGM] make it a promoted piece for Crazyhouse */
- piece = (ChessSquare) (PROMOTED piece);
+ piece = (ChessSquare) (PROMOTED(piece));
if(PieceToChar(piece) != '~') return FALSE; /* cannot be a promoted piece */
p++;
}
board[i][(j++)+gameInfo.holdingsWidth] = piece;
+ if(piece == king) wKingRank = i;
+ if(piece == WHITE_TO_BLACK king) bKingRank = i;
} else {
return FALSE;
}
}
while (*p == '/' || *p == ' ') p++;
+ if(autoSize && w != 0) appData.NrFiles = w, InitPosition(TRUE);
+
+ /* [HGM] by default clear Crazyhouse holdings, if present */
+ if(gameInfo.holdingsWidth) {
+ for(i=0; i<BOARD_HEIGHT; i++) {
+ board[i][0] = EmptySquare; /* black holdings */
+ board[i][BOARD_WIDTH-1] = EmptySquare; /* white holdings */
+ board[i][1] = (ChessSquare) 0; /* black counts */
+ board[i][BOARD_WIDTH-2] = (ChessSquare) 0; /* white counts */
+ }
+ }
+
/* [HGM] look for Crazyhouse holdings here */
while(*p==' ') p++;
if( gameInfo.holdingsWidth && p[-1] == '/' || *p == '[') {
+ int swap=0, wcnt=0, bcnt=0;
if(*p == '[') p++;
- if(*p == '-' ) *p++; /* empty holdings */ else {
+ if(*p == '<') swap++, p++;
+ if(*p == '-' ) p++; /* empty holdings */ else {
if( !gameInfo.holdingsWidth ) return FALSE; /* no room to put holdings! */
/* if we would allow FEN reading to set board size, we would */
/* have to add holdings and shift the board read so far here */
while( (piece = CharToPiece(*p) ) != EmptySquare ) {
- *p++;
+ p++;
if((int) piece >= (int) BlackPawn ) {
i = (int)piece - (int)BlackPawn;
i = PieceToNumber((ChessSquare)i);
if( i >= gameInfo.holdingsSize ) return FALSE;
board[BOARD_HEIGHT-1-i][0] = piece; /* black holdings */
board[BOARD_HEIGHT-1-i][1]++; /* black counts */
+ bcnt++;
} else {
i = (int)piece - (int)WhitePawn;
i = PieceToNumber((ChessSquare)i);
if( i >= gameInfo.holdingsSize ) return FALSE;
board[i][BOARD_WIDTH-1] = piece; /* white holdings */
board[i][BOARD_WIDTH-2]++; /* black holdings */
+ wcnt++;
}
}
+ if(subst) { // substitute back-rank question marks by holdings pieces
+ for(j=BOARD_LEFT; j<BOARD_RGHT; j++) {
+ int k, m, n = bcnt + 1;
+ if(board[0][j] == ClearBoard) {
+ if(!wcnt) return FALSE;
+ n = rand() % wcnt;
+ for(k=0, m=n; k<gameInfo.holdingsSize; k++) if((m -= board[k][BOARD_WIDTH-2]) < 0) {
+ board[0][j] = board[k][BOARD_WIDTH-1]; wcnt--;
+ if(--board[k][BOARD_WIDTH-2] == 0) board[k][BOARD_WIDTH-1] = EmptySquare;
+ break;
+ }
+ }
+ if(board[BOARD_HEIGHT-1][j] == ClearBoard) {
+ if(!bcnt) return FALSE;
+ if(n >= bcnt) n = rand() % bcnt; // use same randomization for black and white if possible
+ for(k=0, m=n; k<gameInfo.holdingsSize; k++) if((n -= board[BOARD_HEIGHT-1-k][1]) < 0) {
+ board[BOARD_HEIGHT-1][j] = board[BOARD_HEIGHT-1-k][0]; bcnt--;
+ if(--board[BOARD_HEIGHT-1-k][1] == 0) board[BOARD_HEIGHT-1-k][0] = EmptySquare;
+ break;
+ }
+ }
+ }
+ subst = 0;
+ }
}
- if(*p == ']') *p++;
+ if(*p == ']') p++;
}
+ if(subst) return FALSE; // substitution requested, but no holdings
+
while(*p == ' ') p++;
/* Active color */
- switch (*p++) {
+ c = *p++;
+ if(appData.colorNickNames) {
+ if( c == appData.colorNickNames[0] ) c = 'w'; else
+ if( c == appData.colorNickNames[1] ) c = 'b';
+ }
+ switch (c) {
case 'w':
*blackPlaysFirst = FALSE;
break;
- case 'b':
+ case 'b':
*blackPlaysFirst = TRUE;
break;
default:
/* [HGM] We NO LONGER ignore the rest of the FEN notation */
/* return the extra info in global variiables */
+ while(*p==' ') p++;
+
+ if(!isdigit(*p) && *p != '-') { // we seem to have castling rights. Make sure they are on the rank the King actually is.
+ if(wKingRank >= 0) for(i=0; i<3; i++) castlingRank[i] = wKingRank;
+ if(bKingRank >= 0) for(i=3; i<6; i++) castlingRank[i] = bKingRank;
+ }
+
/* set defaults in case FEN is incomplete */
- FENepStatus = EP_UNKNOWN;
+ board[EP_STATUS] = EP_UNKNOWN;
+ board[TOUCHED_W] = board[TOUCHED_B] = 0;
for(i=0; i<nrCastlingRights; i++ ) {
- FENcastlingRights[i] =
- gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom ? -1 : initialRights[i];
+ board[CASTLING][i] =
+ appData.fischerCastling ? NoRights : initialRights[i];
} /* assume possible unless obviously impossible */
- if(initialRights[0]>=0 && board[castlingRank[0]][initialRights[0]] != WhiteRook) FENcastlingRights[0] = -1;
- if(initialRights[1]>=0 && board[castlingRank[1]][initialRights[1]] != WhiteRook) FENcastlingRights[1] = -1;
- if(initialRights[2]>=0 && board[castlingRank[2]][initialRights[2]] != WhiteKing) FENcastlingRights[2] = -1;
- if(initialRights[3]>=0 && board[castlingRank[3]][initialRights[3]] != BlackRook) FENcastlingRights[3] = -1;
- if(initialRights[4]>=0 && board[castlingRank[4]][initialRights[4]] != BlackRook) FENcastlingRights[4] = -1;
- if(initialRights[5]>=0 && board[castlingRank[5]][initialRights[5]] != BlackKing) FENcastlingRights[5] = -1;
+ if(initialRights[0]!=NoRights && board[castlingRank[0]][initialRights[0]] != WhiteRook) board[CASTLING][0] = NoRights;
+ if(initialRights[1]!=NoRights && board[castlingRank[1]][initialRights[1]] != WhiteRook) board[CASTLING][1] = NoRights;
+ if(initialRights[2]!=NoRights && board[castlingRank[2]][initialRights[2]] != WhiteUnicorn
+ && board[castlingRank[2]][initialRights[2]] != WhiteKing) board[CASTLING][2] = NoRights;
+ if(initialRights[3]!=NoRights && board[castlingRank[3]][initialRights[3]] != BlackRook) board[CASTLING][3] = NoRights;
+ if(initialRights[4]!=NoRights && board[castlingRank[4]][initialRights[4]] != BlackRook) board[CASTLING][4] = NoRights;
+ if(initialRights[5]!=NoRights && board[castlingRank[5]][initialRights[5]] != BlackUnicorn
+ && board[castlingRank[5]][initialRights[5]] != BlackKing) board[CASTLING][5] = NoRights;
FENrulePlies = 0;
- while(*p==' ') p++;
+ if(pieceDesc[WhiteKing] && strchr(pieceDesc[WhiteKing], 'i') && !strchr(pieceDesc[WhiteKing], 'O')) { // redefined without castling
+ char *q = p;
+ int w=0, b=0;
+ while(isalpha(*p)) {
+ if(isupper(*p)) w |= 1 << (*p++ - 'A');
+ if(islower(*p)) b |= 1 << (*p++ - 'a');
+ }
+ if(*p == '-') p++;
+ if(p != q) {
+ board[TOUCHED_W] = ~w;
+ board[TOUCHED_B] = ~b;
+ while(*p == ' ') p++;
+ }
+ } else
+
if(nrCastlingRights) {
- if(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-') {
+ int fischer = 0;
+ if(gameInfo.variant == VariantSChess) for(i=0; i<BOARD_FILES; i++) virgin[i] = 0;
+ if(*p >= 'A' && *p <= 'Z' || *p >= 'a' && *p <= 'z' || *p=='-') {
/* castling indicator present, so default becomes no castlings */
for(i=0; i<nrCastlingRights; i++ ) {
- FENcastlingRights[i] = -1;
+ board[CASTLING][i] = NoRights;
}
}
while(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-' ||
- (gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) &&
+ (appData.fischerCastling || gameInfo.variant == VariantSChess) &&
( *p >= 'a' && *p < 'a' + gameInfo.boardWidth) ||
( *p >= 'A' && *p < 'A' + gameInfo.boardWidth) ) {
- char c = *p++; int whiteKingFile=-1, blackKingFile=-1;
+ int c = *p++, whiteKingFile=NoRights, blackKingFile=NoRights;
for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
- if(board[BOARD_HEIGHT-1][i] == BlackKing) blackKingFile = i;
- if(board[0 ][i] == WhiteKing) whiteKingFile = i;
+ if(board[castlingRank[5]][i] == BlackKing) blackKingFile = i;
+ if(board[castlingRank[2]][i] == WhiteKing) whiteKingFile = i;
}
+ if(gameInfo.variant == VariantTwoKings || gameInfo.variant == VariantKnightmate)
+ whiteKingFile = blackKingFile = BOARD_WIDTH >> 1; // for these variant scanning fails
+ if(whiteKingFile == NoRights || board[castlingRank[2]][whiteKingFile] != WhiteUnicorn
+ && board[castlingRank[2]][whiteKingFile] != WhiteKing) whiteKingFile = NoRights;
+ if(blackKingFile == NoRights || board[castlingRank[5]][blackKingFile] != BlackUnicorn
+ && board[castlingRank[5]][blackKingFile] != BlackKing) blackKingFile = NoRights;
switch(c) {
case'K':
- for(i=BOARD_RGHT-1; board[0][i]!=WhiteRook && i>whiteKingFile; i--);
- FENcastlingRights[0] = i != whiteKingFile ? i : -1;
- FENcastlingRights[2] = whiteKingFile;
+ for(i=BOARD_RGHT-1; board[castlingRank[2]][i]!=WhiteRook && i>whiteKingFile; i--);
+ board[CASTLING][0] = i != whiteKingFile ? i : NoRights;
+ board[CASTLING][2] = whiteKingFile;
+ if(board[CASTLING][0] != NoRights) virgin[board[CASTLING][0]] |= VIRGIN_W;
+ if(board[CASTLING][2] != NoRights) virgin[board[CASTLING][2]] |= VIRGIN_W;
+ if(whiteKingFile != BOARD_WIDTH>>1|| i != BOARD_RGHT-1) fischer = 1;
break;
case'Q':
- for(i=BOARD_LEFT; board[0][i]!=WhiteRook && i<whiteKingFile; i++);
- FENcastlingRights[1] = i != whiteKingFile ? i : -1;
- FENcastlingRights[2] = whiteKingFile;
+ for(i=BOARD_LEFT; i<BOARD_RGHT && board[castlingRank[2]][i]!=WhiteRook && i<whiteKingFile; i++);
+ board[CASTLING][1] = i != whiteKingFile ? i : NoRights;
+ board[CASTLING][2] = whiteKingFile;
+ if(board[CASTLING][1] != NoRights) virgin[board[CASTLING][1]] |= VIRGIN_W;
+ if(board[CASTLING][2] != NoRights) virgin[board[CASTLING][2]] |= VIRGIN_W;
+ if(whiteKingFile != BOARD_WIDTH>>1|| i != BOARD_LEFT) fischer = 1;
break;
case'k':
- for(i=BOARD_RGHT-1; board[BOARD_HEIGHT-1][i]!=BlackRook && i>blackKingFile; i--);
- FENcastlingRights[3] = i != blackKingFile ? i : -1;
- FENcastlingRights[5] = blackKingFile;
+ for(i=BOARD_RGHT-1; board[castlingRank[5]][i]!=BlackRook && i>blackKingFile; i--);
+ board[CASTLING][3] = i != blackKingFile ? i : NoRights;
+ board[CASTLING][5] = blackKingFile;
+ if(board[CASTLING][3] != NoRights) virgin[board[CASTLING][3]] |= VIRGIN_B;
+ if(board[CASTLING][5] != NoRights) virgin[board[CASTLING][5]] |= VIRGIN_B;
+ if(blackKingFile != BOARD_WIDTH>>1|| i != BOARD_RGHT-1) fischer = 1;
break;
case'q':
- for(i=BOARD_LEFT; board[BOARD_HEIGHT-1][i]!=BlackRook && i<blackKingFile; i++);
- FENcastlingRights[4] = i != blackKingFile ? i : -1;
- FENcastlingRights[5] = blackKingFile;
+ for(i=BOARD_LEFT; i<BOARD_RGHT && board[castlingRank[5]][i]!=BlackRook && i<blackKingFile; i++);
+ board[CASTLING][4] = i != blackKingFile ? i : NoRights;
+ board[CASTLING][5] = blackKingFile;
+ if(board[CASTLING][4] != NoRights) virgin[board[CASTLING][4]] |= VIRGIN_B;
+ if(board[CASTLING][5] != NoRights) virgin[board[CASTLING][5]] |= VIRGIN_B;
+ if(blackKingFile != BOARD_WIDTH>>1|| i != BOARD_LEFT) fischer = 1;
case '-':
break;
default: /* FRC castlings */
if(c >= 'a') { /* black rights */
+ if(gameInfo.variant == VariantSChess) { virgin[c-AAA] |= VIRGIN_B; break; } // in S-Chess castlings are always kq, so just virginity
for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
if(board[BOARD_HEIGHT-1][i] == BlackKing) break;
if(i == BOARD_RGHT) break;
- FENcastlingRights[5] = i;
+ board[CASTLING][5] = i;
c -= AAA;
if(board[BOARD_HEIGHT-1][c] < BlackPawn ||
board[BOARD_HEIGHT-1][c] >= BlackKing ) break;
if(c > i)
- FENcastlingRights[3] = c;
+ board[CASTLING][3] = c;
else
- FENcastlingRights[4] = c;
+ board[CASTLING][4] = c;
} else { /* white rights */
+ if(gameInfo.variant == VariantSChess) { virgin[c-AAA-'A'+'a'] |= VIRGIN_W; break; } // in S-Chess castlings are always KQ
for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
if(board[0][i] == WhiteKing) break;
if(i == BOARD_RGHT) break;
- FENcastlingRights[2] = i;
+ board[CASTLING][2] = i;
c -= AAA - 'a' + 'A';
if(board[0][c] >= WhiteKing) break;
if(c > i)
- FENcastlingRights[0] = c;
+ board[CASTLING][0] = c;
else
- FENcastlingRights[1] = c;
+ board[CASTLING][1] = c;
}
}
}
+ for(i=0; i<nrCastlingRights; i++)
+ if(board[CASTLING][i] != NoRights) initialRights[i] = board[CASTLING][i];
+ if(gameInfo.variant == VariantSChess)
+ for(i=0; i<BOARD_FILES; i++) board[VIRGIN][i] = shuffle ? VIRGIN_W | VIRGIN_B : virgin[i]; // when shuffling assume all virgin
+ if(fischer && shuffle) appData.fischerCastling = TRUE;
if (appData.debugMode) {
fprintf(debugFP, "FEN castling rights:");
for(i=0; i<nrCastlingRights; i++)
- fprintf(debugFP, " %d", FENcastlingRights[i]);
+ fprintf(debugFP, " %d", board[CASTLING][i]);
fprintf(debugFP, "\n");
}
while(*p==' ') p++;
}
+ if(shuffle) SetUpShuffle(board, appData.defaultFrcPosition);
+
/* read e.p. field in games that know e.p. capture */
if(gameInfo.variant != VariantShogi && gameInfo.variant != VariantXiangqi &&
- gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier ) {
+ gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier &&
+ gameInfo.variant != VariantMakruk && gameInfo.variant != VariantASEAN ) {
if(*p=='-') {
- p++; FENepStatus = EP_NONE;
+ p++; board[EP_STATUS] = EP_NONE;
} else {
char c = *p++ - AAA;
if(c < BOARD_LEFT || c >= BOARD_RGHT) return TRUE;
- if(*p >= '0' && *p <='9') *p++;
- FENepStatus = c;
+ if(*p >= '0' && *p <='9') p++;
+ board[EP_STATUS] = c;
}
}
return TRUE;
}
-
+
void
-EditPositionPasteFEN(char *fen)
+EditPositionPasteFEN (char *fen)
{
if (fen != NULL) {
Board initial_position;
- if (!ParseFEN(initial_position, &blackPlaysFirst, fen)) {
+ if (!ParseFEN(initial_position, &blackPlaysFirst, fen, TRUE)) {
DisplayError(_("Bad FEN position in clipboard"), 0);
return ;
} else {
EditPositionEvent();
blackPlaysFirst = savedBlackPlaysFirst;
CopyBoard(boards[0], initial_position);
- /* [HGM] copy FEN attributes as well */
- { int i;
- initialRulePlies = FENrulePlies;
- epStatus[0] = FENepStatus;
- for( i=0; i<nrCastlingRights; i++ )
- castlingRights[0][i] = FENcastlingRights[i];
- }
- EditPositionDone();
+ initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
+ EditPositionDone(FALSE); // [HGM] fake: do not fake rights if we had FEN
DisplayBothClocks();
DrawPosition(FALSE, boards[currentMove]);
}
}
}
+
+static char cseq[12] = "\\ ";
+
+Boolean
+set_cont_sequence (char *new_seq)
+{
+ int len;
+ Boolean ret;
+
+ // handle bad attempts to set the sequence
+ if (!new_seq)
+ return 0; // acceptable error - no debug
+
+ len = strlen(new_seq);
+ ret = (len > 0) && (len < sizeof(cseq));
+ if (ret)
+ safeStrCpy(cseq, new_seq, sizeof(cseq)/sizeof(cseq[0]));
+ else if (appData.debugMode)
+ fprintf(debugFP, "Invalid continuation sequence \"%s\" (maximum length is: %u)\n", new_seq, (unsigned) sizeof(cseq)-1);
+ return ret;
+}
+
+/*
+ reformat a source message so words don't cross the width boundary. internal
+ newlines are not removed. returns the wrapped size (no null character unless
+ included in source message). If dest is NULL, only calculate the size required
+ for the dest buffer. lp argument indicats line position upon entry, and it's
+ passed back upon exit.
+*/
+int
+wrap (char *dest, char *src, int count, int width, int *lp)
+{
+ int len, i, ansi, cseq_len, line, old_line, old_i, old_len, clen;
+
+ cseq_len = strlen(cseq);
+ old_line = line = *lp;
+ ansi = len = clen = 0;
+
+ for (i=0; i < count; i++)
+ {
+ if (src[i] == '\033')
+ ansi = 1;
+
+ // if we hit the width, back up
+ if (!ansi && (line >= width) && src[i] != '\n' && src[i] != ' ')
+ {
+ // store i & len in case the word is too long
+ old_i = i, old_len = len;
+
+ // find the end of the last word
+ while (i && src[i] != ' ' && src[i] != '\n')
+ {
+ i--;
+ len--;
+ }
+
+ // word too long? restore i & len before splitting it
+ if ((old_i-i+clen) >= width)
+ {
+ i = old_i;
+ len = old_len;
+ }
+
+ // extra space?
+ if (i && src[i-1] == ' ')
+ len--;
+
+ if (src[i] != ' ' && src[i] != '\n')
+ {
+ i--;
+ if (len)
+ len--;
+ }
+
+ // now append the newline and continuation sequence
+ if (dest)
+ dest[len] = '\n';
+ len++;
+ if (dest)
+ strncpy(dest+len, cseq, cseq_len);
+ len += cseq_len;
+ line = cseq_len;
+ clen = cseq_len;
+ continue;
+ }
+
+ if (dest)
+ dest[len] = src[i];
+ len++;
+ if (!ansi)
+ line++;
+ if (src[i] == '\n')
+ line = 0;
+ if (src[i] == 'm')
+ ansi = 0;
+ }
+ if (dest && appData.debugMode)
+ {
+ fprintf(debugFP, "wrap(count:%d,width:%d,line:%d,len:%d,*lp:%d,src: ",
+ count, width, line, len, *lp);
+ show_bytes(debugFP, src, count);
+ fprintf(debugFP, "\ndest: ");
+ show_bytes(debugFP, dest, len);
+ fprintf(debugFP, "\n");
+ }
+ *lp = dest ? line : old_line;
+
+ return len;
+}
+
+// [HGM] vari: routines for shelving variations
+Boolean modeRestore = FALSE;
+
+void
+PushInner (int firstMove, int lastMove)
+{
+ int i, j, nrMoves = lastMove - firstMove;
+
+ // push current tail of game on stack
+ savedResult[storedGames] = gameInfo.result;
+ savedDetails[storedGames] = gameInfo.resultDetails;
+ gameInfo.resultDetails = NULL;
+ savedFirst[storedGames] = firstMove;
+ savedLast [storedGames] = lastMove;
+ savedFramePtr[storedGames] = framePtr;
+ framePtr -= nrMoves; // reserve space for the boards
+ for(i=nrMoves; i>=1; i--) { // copy boards to stack, working downwards, in case of overlap
+ CopyBoard(boards[framePtr+i], boards[firstMove+i]);
+ for(j=0; j<MOVE_LEN; j++)
+ moveList[framePtr+i][j] = moveList[firstMove+i-1][j];
+ for(j=0; j<2*MOVE_LEN; j++)
+ parseList[framePtr+i][j] = parseList[firstMove+i-1][j];
+ timeRemaining[0][framePtr+i] = timeRemaining[0][firstMove+i];
+ timeRemaining[1][framePtr+i] = timeRemaining[1][firstMove+i];
+ pvInfoList[framePtr+i] = pvInfoList[firstMove+i-1];
+ pvInfoList[firstMove+i-1].depth = 0;
+ commentList[framePtr+i] = commentList[firstMove+i];
+ commentList[firstMove+i] = NULL;
+ }
+
+ storedGames++;
+ forwardMostMove = firstMove; // truncate game so we can start variation
+}
+
+void
+PushTail (int firstMove, int lastMove)
+{
+ if(appData.icsActive) { // only in local mode
+ forwardMostMove = currentMove; // mimic old ICS behavior
+ return;
+ }
+ if(storedGames >= MAX_VARIATIONS-2) return; // leave one for PV-walk
+
+ PushInner(firstMove, lastMove);
+ if(storedGames == 1) GreyRevert(FALSE);
+ if(gameMode == PlayFromGameFile) gameMode = EditGame, modeRestore = TRUE;
+}
+
+void
+PopInner (Boolean annotate)
+{
+ int i, j, nrMoves;
+ char buf[8000], moveBuf[20];
+
+ ToNrEvent(savedFirst[storedGames-1]); // sets currentMove
+ storedGames--; // do this after ToNrEvent, to make sure HistorySet will refresh entire game after PopInner returns
+ nrMoves = savedLast[storedGames] - currentMove;
+ if(annotate) {
+ int cnt = 10;
+ if(!WhiteOnMove(currentMove))
+ snprintf(buf, sizeof(buf)/sizeof(buf[0]),"(%d...", (currentMove+2)>>1);
+ else safeStrCpy(buf, "(", sizeof(buf)/sizeof(buf[0]));
+ for(i=currentMove; i<forwardMostMove; i++) {
+ if(WhiteOnMove(i))
+ snprintf(moveBuf, sizeof(moveBuf)/sizeof(moveBuf[0]), " %d. %s", (i+2)>>1, SavePart(parseList[i]));
+ else snprintf(moveBuf, sizeof(moveBuf)/sizeof(moveBuf[0])," %s", SavePart(parseList[i]));
+ strcat(buf, moveBuf);
+ if(commentList[i]) { strcat(buf, " "); strcat(buf, commentList[i]); }
+ if(!--cnt) { strcat(buf, "\n"); cnt = 10; }
+ }
+ strcat(buf, ")");
+ }
+ for(i=1; i<=nrMoves; i++) { // copy last variation back
+ CopyBoard(boards[currentMove+i], boards[framePtr+i]);
+ for(j=0; j<MOVE_LEN; j++)
+ moveList[currentMove+i-1][j] = moveList[framePtr+i][j];
+ for(j=0; j<2*MOVE_LEN; j++)
+ parseList[currentMove+i-1][j] = parseList[framePtr+i][j];
+ timeRemaining[0][currentMove+i] = timeRemaining[0][framePtr+i];
+ timeRemaining[1][currentMove+i] = timeRemaining[1][framePtr+i];
+ pvInfoList[currentMove+i-1] = pvInfoList[framePtr+i];
+ if(commentList[currentMove+i]) free(commentList[currentMove+i]);
+ commentList[currentMove+i] = commentList[framePtr+i];
+ commentList[framePtr+i] = NULL;
+ }
+ if(annotate) AppendComment(currentMove+1, buf, FALSE);
+ framePtr = savedFramePtr[storedGames];
+ gameInfo.result = savedResult[storedGames];
+ if(gameInfo.resultDetails != NULL) {
+ free(gameInfo.resultDetails);
+ }
+ gameInfo.resultDetails = savedDetails[storedGames];
+ forwardMostMove = currentMove + nrMoves;
+}
+
+Boolean
+PopTail (Boolean annotate)
+{
+ if(appData.icsActive) return FALSE; // only in local mode
+ if(!storedGames) return FALSE; // sanity
+ CommentPopDown(); // make sure no stale variation comments to the destroyed line can remain open
+
+ PopInner(annotate);
+ if(currentMove < forwardMostMove) ForwardEvent(); else
+ HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
+
+ if(storedGames == 0) { GreyRevert(TRUE); if(modeRestore) modeRestore = FALSE, gameMode = PlayFromGameFile; }
+ return TRUE;
+}
+
+void
+CleanupTail ()
+{ // remove all shelved variations
+ int i;
+ for(i=0; i<storedGames; i++) {
+ if(savedDetails[i])
+ free(savedDetails[i]);
+ savedDetails[i] = NULL;
+ }
+ for(i=framePtr; i<MAX_MOVES; i++) {
+ if(commentList[i]) free(commentList[i]);
+ commentList[i] = NULL;
+ }
+ framePtr = MAX_MOVES-1;
+ storedGames = 0;
+}
+
+void
+LoadVariation (int index, char *text)
+{ // [HGM] vari: shelve previous line and load new variation, parsed from text around text[index]
+ char *p = text, *start = NULL, *end = NULL, wait = NULLCHAR;
+ int level = 0, move;
+
+ if(gameMode != EditGame && gameMode != AnalyzeMode && gameMode != PlayFromGameFile) return;
+ // first find outermost bracketing variation
+ while(*p) { // hope I got this right... Non-nesting {} and [] can screen each other and nesting ()
+ if(!wait) { // while inside [] pr {}, ignore everyting except matching closing ]}
+ if(*p == '{') wait = '}'; else
+ if(*p == '[') wait = ']'; else
+ if(*p == '(' && level++ == 0 && p-text < index) start = p+1;
+ if(*p == ')' && level > 0 && --level == 0 && p-text > index && end == NULL) end = p-1;
+ }
+ if(*p == wait) wait = NULLCHAR; // closing ]} found
+ p++;
+ }
+ if(!start || !end) return; // no variation found, or syntax error in PGN: ignore click
+ if(appData.debugMode) fprintf(debugFP, "at move %d load variation '%s'\n", currentMove, start);
+ end[1] = NULLCHAR; // clip off comment beyond variation
+ ToNrEvent(currentMove-1);
+ PushTail(currentMove, forwardMostMove); // shelve main variation. This truncates game
+ // kludge: use ParsePV() to append variation to game
+ move = currentMove;
+ ParsePV(start, TRUE, TRUE);
+ forwardMostMove = endPV; endPV = -1; currentMove = move; // cleanup what ParsePV did
+ ClearPremoveHighlights();
+ CommentPopDown();
+ ToNrEvent(currentMove+1);
+}
+
+void
+LoadTheme ()
+{
+ char *p, *q, buf[MSG_SIZ];
+ if(engineLine && engineLine[0]) { // a theme was selected from the listbox
+ snprintf(buf, MSG_SIZ, "-theme %s", engineLine);
+ ParseArgsFromString(buf);
+ ActivateTheme(TRUE); // also redo colors
+ return;
+ }
+ p = nickName;
+ if(*p && !strchr(p, '"')) // theme name specified and well-formed; add settings to theme list
+ {
+ int len;
+ q = appData.themeNames;
+ snprintf(buf, MSG_SIZ, "\"%s\"", nickName);
+ if(appData.useBitmaps) {
+ snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -ubt true -lbtf \"%s\" -dbtf \"%s\" -lbtm %d -dbtm %d",
+ appData.liteBackTextureFile, appData.darkBackTextureFile,
+ appData.liteBackTextureMode,
+ appData.darkBackTextureMode );
+ } else {
+ snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -ubt false -lsc %s -dsc %s",
+ Col2Text(2), // lightSquareColor
+ Col2Text(3) ); // darkSquareColor
+ }
+ if(appData.useBorder) {
+ snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -ub true -border \"%s\"",
+ appData.border);
+ } else {
+ snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -ub false");
+ }
+ if(appData.useFont) {
+ snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -upf true -pf \"%s\" -fptc \"%s\" -fpfcw %s -fpbcb %s",
+ appData.renderPiecesWithFont,
+ appData.fontToPieceTable,
+ Col2Text(9), // appData.fontBackColorWhite
+ Col2Text(10) ); // appData.fontForeColorBlack
+ } else {
+ snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -upf false -pid \"%s\"",
+ appData.pieceDirectory);
+ if(!appData.pieceDirectory[0])
+ snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -wpc %s -bpc %s",
+ Col2Text(0), // whitePieceColor
+ Col2Text(1) ); // blackPieceColor
+ }
+ snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -hsc %s -phc %s\n",
+ Col2Text(4), // highlightSquareColor
+ Col2Text(5) ); // premoveHighlightColor
+ appData.themeNames = malloc(len = strlen(q) + strlen(buf) + 1);
+ if(insert != q) insert[-1] = NULLCHAR;
+ snprintf(appData.themeNames, len, "%s\n%s%s", q, buf, insert);
+ if(q) free(q);
+ }
+ ActivateTheme(FALSE);
+}