2 Copyright (c) 1993 Richard V. Nash.
3 Copyright (c) 2000 Dan Papasian
4 Copyright (C) Andrew Tridgell 2002
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
39 static ECO_entry *ECO_book[1096];
40 static NIC_entry *NIC_book[1096];
41 static LONG_entry *LONG_book[4096];
43 static int ECO_entries, NIC_entries, LONG_entries;
45 /* This coverts a FEN position to a FICS board
46 it assumes FENpos contains a valid position
47 the color tag has been separated and put in color*/
49 void FEN_to_board(char* FENpos, struct game_state_t* gs)
53 printf("FEN, var='%s'\n", gs->variant);
\r
54 for (r=gs->ranks-1; r >=0; r--) {
\r
56 while (f<gs->files) {
\r
59 gs->board[f++][r] = CharToPiece(next, gs->variant);
\r
60 else if (next != '/') {
\r
61 int t = (next - '0');
\r
62 if(*FENpos >= '0' && *FENpos <= '9') // [HGM] can be double-digit
\r
63 t = 10*t + *(FENpos++) - '0';
\r
65 gs->board[f++][r] = NOPIECE;
\r
66 while (--t && f < gs->files);
\r
70 if (*(FENpos + 1) == 'w') /* the char after the space */
\r
76 /* converts a board to a FEN pos */
78 static void board_to_FEN(char* FENpos, struct game_state_t* gs)
83 for (r=gs->ranks-1; r>=0; r--) {
\r
85 for (f=0; f<gs->files; f++) {
\r
86 if ((piece = PieceToChar(gs->board[f][r])) != ' ') {
\r
88 if(count > 9) { count -= 10; *(FENpos++) = '1'; }
\r
89 *(FENpos++) = count + '0';
\r
92 *(FENpos++) = piece;
\r
94 if (f == gs->files-1) {
\r
95 if(count > 8) { count -= 10; *(FENpos++) = '1'; }
\r
96 *(FENpos++) = count + '0' + 1;
\r
106 if (gs->onMove == WHITE)
\r
110 *(++FENpos) = '\0';
\r
113 char *boardToFEN(int g)
115 static char FENstring[74];
117 board_to_FEN (FENstring,&game_globals.garray[g].game_state);
121 static void ECO_init()
126 char FENpos[73], ECO[4], onMove[2];
129 fp= fopen_s(BOOK_DIR "/eco999.idx", "r");
131 d_printf( "Could not open ECO file\n");
136 fgets(ptmp, 1024, fp);
137 if (feof(fp)) continue;
138 sscanf(ptmp, "%[\x21-z] %s", FENpos, onMove);
139 sprintf(FENpos, "%s %s", FENpos, onMove);
141 fgets(ptmp, 1024, fp);
142 if (feof(fp)) continue;
143 sscanf(ptmp, "%[0-z]", ECO);
144 ECO_book[i]= (ECO_entry *) malloc(sizeof(ECO_entry));
145 if (ECO_book[i]==NULL) {
146 d_printf( "Cound not alloc mem for ECO entry %d.\n", i);
149 strcpy(ECO_book[i]->ECO, ECO);
150 strcpy(ECO_book[i]->FENpos, FENpos);
158 if (ECO_book[i] == NULL)
159 d_printf( "ERROR! ECO book position number %d is NULL.", i);
162 static void NIC_init()
167 char FENpos[73], NIC[6], onMove[2];
170 fp= fopen_p("%s/nic999.idx", "r", BOOK_DIR);
172 d_printf( "Could not open NIC file\n");
177 fgets(ptmp, 1024, fp);
178 if (feof(fp)) continue;
179 sscanf(ptmp, "%[\x21-z] %s", FENpos, onMove);
180 sprintf(FENpos, "%s %s", FENpos, onMove);
182 fgets(ptmp, 1024, fp);
183 if (feof(fp)) continue;
184 sscanf(ptmp, "%[.-z]", NIC);
185 NIC_book[i]= (NIC_entry *) malloc(sizeof(NIC_entry));
186 if (NIC_book[i]==NULL) {
187 d_printf( "Cound not alloc mem for NIC entry %d.\n", i);
190 strcpy(NIC_book[i]->NIC, NIC);
191 strcpy(NIC_book[i]->FENpos, FENpos);
199 static void LONG_init()
204 char FENpos[74], LONG[256], onMove[2];
207 fp= fopen_p("%s/long999.idx", "r", BOOK_DIR);
209 d_printf( "Could not open LONG file\n");
214 fgets(ptmp, 1024, fp);
215 if (feof(fp)) continue;
216 sscanf(ptmp, "%[\x21-z] %s", FENpos, onMove);
217 sprintf(FENpos, "%s %s", FENpos, onMove);
219 fgets(ptmp, 1024, fp);
220 if (feof(fp)) continue;
221 sscanf(ptmp, "%[^*\n]", LONG);
222 LONG_book[i]= (LONG_entry *) malloc(sizeof(LONG_entry));
223 if (LONG_book[i]==NULL) {
224 d_printf( "Cound not alloc mem for LONG entry %d.\n", i);
227 strcpy(LONG_book[i]->LONG, LONG);
228 strcpy(LONG_book[i]->FENpos, FENpos);
236 /* free up any memory used by books */
237 void book_close(void)
241 for (i=0;ECO_book[i];i++) {
245 for (i=0;NIC_book[i];i++) {
249 for (i=0;LONG_book[i];i++) {
260 d_printf("CHESSD: Loaded books\n");
267 int i, flag, l = 0, r = ECO_entries - 1, x;
270 if (game_globals.garray[g].type == TYPE_WILD) {
273 } else if (game_globals.garray[g].moveList == NULL) {
280 for (flag=0,i=game_globals.garray[g].numHalfMoves; (i>0 && !flag); i--) {
283 while ((r >= l) && !flag) {
285 if ((strcmp(game_globals.garray[g].moveList[i].FENpos, ECO_book[x]->FENpos)) < 0)
289 if (!strcmp(game_globals.garray[g].moveList[i].FENpos, ECO_book[x]->FENpos)) {
290 strcpy(ECO, ECO_book[x]->ECO);
299 int com_eco(int p, param_list param)
301 struct player *pp = &player_globals.parray[p];
302 int i, flag = 0, x, l, r;
306 if (param[0].type == TYPE_NULL) { /* own game */
308 pprintf(p, "You are not playing or examining a game.\n");
312 if (((game_globals.garray[g1].status != GAME_EXAMINE) &&
313 (game_globals.garray[g1].status != GAME_SETUP)) && !pIsPlaying(p))
316 g1 = GameNumFromParam (p, &p1, ¶m[0]);
317 if (g1 < 0) return COM_OK;
318 if ((g1 >= game_globals.g_num) || ((game_globals.garray[g1].status != GAME_ACTIVE) &&
319 (game_globals.garray[g1].status != GAME_EXAMINE) &&
320 (game_globals.garray[g1].status != GAME_SETUP))) {
321 pprintf(p, "There is no such game.\n");
326 if (game_globals.garray[g1].status == GAME_SETUP) {
327 pprintf (p,"The postion is still being set up.\n");
331 if (game_globals.garray[g1].private &&
332 (p != game_globals.garray[g1].white) && (p != game_globals.garray[g1].black) &&
333 !check_admin(p, 1)) {
334 pprintf(p, "Sorry - that game is private.\n");
337 if (game_globals.garray[g1].type == TYPE_WILD) {
338 pprintf(p, "That game is a wild game.\n");
343 pprintf(p, "Info about game %d: \"%s vs. %s\"\n\n", g1+1,
344 game_globals.garray[g1].white_name,
345 game_globals.garray[g1].black_name);
347 if (game_globals.garray[g1].moveList==NULL) {
351 for (flag=0,i=game_globals.garray[g1].numHalfMoves; (i>0 && !flag); i--) {
354 while ((r >= l) && !flag) {
356 if ((strcmp(game_globals.garray[g1].moveList[i].FENpos, ECO_book[x]->FENpos)) < 0)
360 if (!strcmp(game_globals.garray[g1].moveList[i].FENpos, ECO_book[x]->FENpos)) {
361 pprintf(p, " ECO[%3d]: %s\n", i, ECO_book[x]->ECO);
367 for (flag=0, i=game_globals.garray[g1].numHalfMoves; ((i>0) && (!flag)); i--) {
370 while ((r >=l) && !flag) {
372 if ((strcmp(game_globals.garray[g1].moveList[i].FENpos, NIC_book[x]->FENpos)) < 0)
376 if (!strcmp(game_globals.garray[g1].moveList[i].FENpos, NIC_book[x]->FENpos)) {
377 pprintf(p, " NIC[%3d]: %s\n", i, NIC_book[x]->NIC);
383 for (flag=0, i=game_globals.garray[g1].numHalfMoves; ((i>0) && (!flag)); i--) {
385 r = LONG_entries - 1;
386 while ((r >=l) && !flag) {
388 if ((strcmp(game_globals.garray[g1].moveList[i].FENpos, LONG_book[x]->FENpos)) < 0)
392 if (!strcmp(game_globals.garray[g1].moveList[i].FENpos, LONG_book[x]->FENpos)) {
393 pprintf(p, " LONG[%3d]: %s\n", i, LONG_book[x]->LONG);