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)
55 for (r=7; r >=0; r--) {
60 gs->board[f++][r] = CharToPiece(next);
61 else if (next != '/') {
64 gs->board[f++][r] = NOPIECE;
69 if (*(FENpos + 1) == 'w') /* the char after the space */
75 /* converts a board to a FEN pos */
77 static void board_to_FEN(char* FENpos, struct game_state_t* gs)
82 for (r=7; r>=0; r--) {
85 if ((piece = PieceToChar(gs->board[f][r])) != ' ') {
87 *(FENpos++) = count + '0';
93 *(FENpos++) = count + '0' + 1;
103 if (gs->onMove == WHITE)
110 char *boardToFEN(int g)
112 static char FENstring[74];
114 board_to_FEN (FENstring,&game_globals.garray[g].game_state);
118 static void ECO_init()
123 char FENpos[73], ECO[4], onMove[2];
126 fp= fopen_s(BOOK_DIR "/eco999.idx", "r");
128 d_printf( "Could not open ECO file\n");
133 fgets(ptmp, 1024, fp);
134 if (feof(fp)) continue;
135 sscanf(ptmp, "%[\x21-z] %s", FENpos, onMove);
136 sprintf(FENpos, "%s %s", FENpos, onMove);
138 fgets(ptmp, 1024, fp);
139 if (feof(fp)) continue;
140 sscanf(ptmp, "%[0-z]", ECO);
141 ECO_book[i]= (ECO_entry *) malloc(sizeof(ECO_entry));
142 if (ECO_book[i]==NULL) {
143 d_printf( "Cound not alloc mem for ECO entry %d.\n", i);
146 strcpy(ECO_book[i]->ECO, ECO);
147 strcpy(ECO_book[i]->FENpos, FENpos);
155 if (ECO_book[i] == NULL)
156 d_printf( "ERROR! ECO book position number %d is NULL.", i);
159 static void NIC_init()
164 char FENpos[73], NIC[6], onMove[2];
167 fp= fopen_p("%s/nic999.idx", "r", BOOK_DIR);
169 d_printf( "Could not open NIC file\n");
174 fgets(ptmp, 1024, fp);
175 if (feof(fp)) continue;
176 sscanf(ptmp, "%[\x21-z] %s", FENpos, onMove);
177 sprintf(FENpos, "%s %s", FENpos, onMove);
179 fgets(ptmp, 1024, fp);
180 if (feof(fp)) continue;
181 sscanf(ptmp, "%[.-z]", NIC);
182 NIC_book[i]= (NIC_entry *) malloc(sizeof(NIC_entry));
183 if (NIC_book[i]==NULL) {
184 d_printf( "Cound not alloc mem for NIC entry %d.\n", i);
187 strcpy(NIC_book[i]->NIC, NIC);
188 strcpy(NIC_book[i]->FENpos, FENpos);
196 static void LONG_init()
201 char FENpos[74], LONG[256], onMove[2];
204 fp= fopen_p("%s/long999.idx", "r", BOOK_DIR);
206 d_printf( "Could not open LONG file\n");
211 fgets(ptmp, 1024, fp);
212 if (feof(fp)) continue;
213 sscanf(ptmp, "%[\x21-z] %s", FENpos, onMove);
214 sprintf(FENpos, "%s %s", FENpos, onMove);
216 fgets(ptmp, 1024, fp);
217 if (feof(fp)) continue;
218 sscanf(ptmp, "%[^*\n]", LONG);
219 LONG_book[i]= (LONG_entry *) malloc(sizeof(LONG_entry));
220 if (LONG_book[i]==NULL) {
221 d_printf( "Cound not alloc mem for LONG entry %d.\n", i);
224 strcpy(LONG_book[i]->LONG, LONG);
225 strcpy(LONG_book[i]->FENpos, FENpos);
233 /* free up any memory used by books */
234 void book_close(void)
238 for (i=0;ECO_book[i];i++) {
242 for (i=0;NIC_book[i];i++) {
246 for (i=0;LONG_book[i];i++) {
257 d_printf("CHESSD: Loaded books\n");
264 int i, flag, l = 0, r = ECO_entries - 1, x;
267 if (game_globals.garray[g].type == TYPE_WILD) {
270 } else if (game_globals.garray[g].moveList == NULL) {
277 for (flag=0,i=game_globals.garray[g].numHalfMoves; (i>0 && !flag); i--) {
280 while ((r >= l) && !flag) {
282 if ((strcmp(game_globals.garray[g].moveList[i].FENpos, ECO_book[x]->FENpos)) < 0)
286 if (!strcmp(game_globals.garray[g].moveList[i].FENpos, ECO_book[x]->FENpos)) {
287 strcpy(ECO, ECO_book[x]->ECO);
296 int com_eco(int p, param_list param)
298 struct player *pp = &player_globals.parray[p];
299 int i, flag = 0, x, l, r;
303 if (param[0].type == TYPE_NULL) { /* own game */
305 pprintf(p, "You are not playing or examining a game.\n");
309 if (((game_globals.garray[g1].status != GAME_EXAMINE) &&
310 (game_globals.garray[g1].status != GAME_SETUP)) && !pIsPlaying(p))
313 g1 = GameNumFromParam (p, &p1, ¶m[0]);
314 if (g1 < 0) return COM_OK;
315 if ((g1 >= game_globals.g_num) || ((game_globals.garray[g1].status != GAME_ACTIVE) &&
316 (game_globals.garray[g1].status != GAME_EXAMINE) &&
317 (game_globals.garray[g1].status != GAME_SETUP))) {
318 pprintf(p, "There is no such game.\n");
323 if (game_globals.garray[g1].status == GAME_SETUP) {
324 pprintf (p,"The postion is still being set up.\n");
328 if (game_globals.garray[g1].private &&
329 (p != game_globals.garray[g1].white) && (p != game_globals.garray[g1].black) &&
330 !check_admin(p, 1)) {
331 pprintf(p, "Sorry - that game is private.\n");
334 if (game_globals.garray[g1].type == TYPE_WILD) {
335 pprintf(p, "That game is a wild game.\n");
340 pprintf(p, "Info about game %d: \"%s vs. %s\"\n\n", g1+1,
341 game_globals.garray[g1].white_name,
342 game_globals.garray[g1].black_name);
344 if (game_globals.garray[g1].moveList==NULL) {
348 for (flag=0,i=game_globals.garray[g1].numHalfMoves; (i>0 && !flag); i--) {
351 while ((r >= l) && !flag) {
353 if ((strcmp(game_globals.garray[g1].moveList[i].FENpos, ECO_book[x]->FENpos)) < 0)
357 if (!strcmp(game_globals.garray[g1].moveList[i].FENpos, ECO_book[x]->FENpos)) {
358 pprintf(p, " ECO[%3d]: %s\n", i, ECO_book[x]->ECO);
364 for (flag=0, i=game_globals.garray[g1].numHalfMoves; ((i>0) && (!flag)); i--) {
367 while ((r >=l) && !flag) {
369 if ((strcmp(game_globals.garray[g1].moveList[i].FENpos, NIC_book[x]->FENpos)) < 0)
373 if (!strcmp(game_globals.garray[g1].moveList[i].FENpos, NIC_book[x]->FENpos)) {
374 pprintf(p, " NIC[%3d]: %s\n", i, NIC_book[x]->NIC);
380 for (flag=0, i=game_globals.garray[g1].numHalfMoves; ((i>0) && (!flag)); i--) {
382 r = LONG_entries - 1;
383 while ((r >=l) && !flag) {
385 if ((strcmp(game_globals.garray[g1].moveList[i].FENpos, LONG_book[x]->FENpos)) < 0)
389 if (!strcmp(game_globals.garray[g1].moveList[i].FENpos, LONG_book[x]->FENpos)) {
390 pprintf(p, " LONG[%3d]: %s\n", i, LONG_book[x]->LONG);