1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
|
/* eco.c
*
*/
#include "stdinclude.h"
#include "common.h"
#include <err.h>
#include "board.h"
#include "command.h"
#include "config.h"
#include "eco.h"
#include "gamedb.h"
#include "gameproc.h"
#include "obsproc.h"
#include "playerdb.h"
#include "utils.h"
#if __linux__
#include <bsd/string.h>
#endif
#define FENPOS_SIZE 73
#define ONMOVE_SIZE 2
#define ECO_MAXFILENAME 1024
#define ECO_MAXTMP 1024
#define SCAN_FP_AND_ONMOVE "%72[\x21-z] %1s"
PRIVATE char *book_dir = DEFAULT_BOOK;
PRIVATE ECO_entry *ECO_book[1096];
PRIVATE NIC_entry *NIC_book[1096];
PRIVATE LONG_entry *LONG_book[4096];
PRIVATE int ECO_entries, NIC_entries, LONG_entries;
PRIVATE inline int
fencmp(const unsigned char *pos1, const char *pos2)
{
return strcmp((const char *)pos1, pos2);
}
PUBLIC char *
boardToFEN(int g)
{
int FENcount = 0;
int space = 0;
static char FENstring[80];
for (int i = 7; i >= 0; i--) {
for (int j = 0; j < 8; j++) {
switch (garray[g].game_state.board[j][i]) {
case W_PAWN:
SPACE_CHK();
FENstring[FENcount++] = 'P';
break;
case W_ROOK:
SPACE_CHK();
FENstring[FENcount++] = 'R';
break;
case W_KNIGHT:
SPACE_CHK();
FENstring[FENcount++] = 'N';
break;
case W_BISHOP:
SPACE_CHK();
FENstring[FENcount++] = 'B';
break;
case W_QUEEN:
SPACE_CHK();
FENstring[FENcount++] = 'Q';
break;
case W_KING:
SPACE_CHK();
FENstring[FENcount++] = 'K';
break;
case B_PAWN:
SPACE_CHK();
FENstring[FENcount++] = 'p';
break;
case B_ROOK:
SPACE_CHK();
FENstring[FENcount++] = 'r';
break;
case B_KNIGHT:
SPACE_CHK();
FENstring[FENcount++] = 'n';
break;
case B_BISHOP:
SPACE_CHK();
FENstring[FENcount++] = 'b';
break;
case B_QUEEN:
SPACE_CHK();
FENstring[FENcount++] = 'q';
break;
case B_KING:
SPACE_CHK();
FENstring[FENcount++] = 'k';
break;
default:
space++;
break;
} /* switch */
} /* for */
if (space > 0) {
FENstring[FENcount++] = (space + '0');
space = 0;
}
FENstring[FENcount++] = '/';
} /* for */
FENstring[--FENcount] = ' ';
FENstring[++FENcount] = ((garray[g].game_state.onMove == WHITE)
? 'w'
: 'b');
FENstring[++FENcount] = '\0';
return FENstring;
}
PUBLIC void
ECO_init(void)
{
FILE *fp;
char ECO[4] = {0,0,0,0};
char FENpos[FENPOS_SIZE] = { '\0' };
char filename[ECO_MAXFILENAME] = { '\0' };
char onMove[ONMOVE_SIZE] = {0,0};
char tmp[ECO_MAXTMP] = { '\0' };
char *ptmp = tmp;
int i = 0;
snprintf(filename, sizeof filename, "%s/eco999.idx", book_dir);
if ((fp = fopen(filename, "r")) == NULL)
err(1, "Could not open ECO file (%s)", filename);
while (!feof(fp)) {
(void) strlcpy(ptmp, "", sizeof tmp);
if (fgets(ptmp, sizeof tmp, fp) == NULL ||
feof(fp))
break;
/* XXX */
sscanf(ptmp, SCAN_FP_AND_ONMOVE, FENpos, onMove);
(void) strlcat(FENpos, " ", sizeof FENpos);
(void) strlcat(FENpos, onMove, sizeof FENpos);
(void) strlcpy(ptmp, "", sizeof tmp);
if (fgets(ptmp, sizeof tmp, fp) == NULL ||
feof(fp))
break;
sscanf(ptmp, "%[0-z]", ECO);
if ((ECO_book[i] = malloc(sizeof(ECO_entry))) == NULL)
err(1, "Cound not alloc mem for ECO entry %d", i);
(void) strlcpy(ECO_book[i]->ECO, ECO,
sizeof(ECO_book[i]->ECO));
(void) strlcpy(ECO_book[i]->FENpos, FENpos,
sizeof(ECO_book[i]->FENpos));
++i;
}
fclose(fp);
ECO_book[i] = NULL;
fprintf(stderr, "%d entries in ECO book\n", i);
ECO_entries = i;
while (--i >= 0) {
if (ECO_book[i] == NULL) {
fprintf(stderr, "ERROR! ECO book position number %d "
"is NULL.", i);
}
}
}
PUBLIC void
NIC_init(void)
{
FILE *fp;
char FENpos[FENPOS_SIZE] = { '\0' };
char NIC[6] = {0,0,0,0,0,0};
char filename[ECO_MAXFILENAME] = { '\0' };
char onMove[ONMOVE_SIZE] = {0,0};
char tmp[ECO_MAXTMP] = { '\0' };
char *ptmp = tmp;
int i = 0;
snprintf(filename, sizeof filename, "%s/nic999.idx", book_dir);
if ((fp = fopen(filename, "r")) == NULL)
err(1, "Could not open NIC file (%s)", filename);
while (!feof(fp)) {
(void) strlcpy(ptmp, "", sizeof tmp);
if (fgets(ptmp, sizeof tmp, fp) == NULL ||
feof(fp))
break;
sscanf(ptmp, SCAN_FP_AND_ONMOVE, FENpos, onMove);
(void) strlcat(FENpos, " ", sizeof FENpos);
(void) strlcat(FENpos, onMove, sizeof FENpos);
(void) strlcpy(ptmp, "", sizeof tmp);
if (fgets(ptmp, sizeof tmp, fp) == NULL ||
feof(fp))
break;
sscanf(ptmp, "%[.-z]", NIC);
if ((NIC_book[i] = malloc(sizeof(NIC_entry))) == NULL)
err(1, "Cound not alloc mem for NIC entry %d", i);
(void) strlcpy(NIC_book[i]->NIC, NIC,
sizeof(NIC_book[i]->NIC));
(void) strlcpy(NIC_book[i]->FENpos, FENpos,
sizeof(NIC_book[i]->FENpos));
++i;
}
fclose(fp);
NIC_book[i] = NULL;
fprintf(stderr, "%d entries in NIC book\n", i);
NIC_entries = i;
}
PUBLIC void
LONG_init(void)
{
FILE *fp;
char FENpos[FENPOS_SIZE] = { '\0' };
char LONG[256] = { '\0' };
char filename[ECO_MAXFILENAME] = { '\0' };
char onMove[ONMOVE_SIZE] = {0,0};
char tmp[ECO_MAXTMP] = { '\0' };
char *ptmp = tmp;
int i = 0;
snprintf(filename, sizeof filename, "%s/long999.idx", book_dir);
if ((fp = fopen(filename, "r")) == NULL)
err(1, "Could not open LONG file (%s)", filename);
while (!feof(fp)) {
(void) strlcpy(ptmp, "", sizeof tmp);
if (fgets(ptmp, sizeof tmp, fp) == NULL ||
feof(fp))
break;
/* XXX */
sscanf(ptmp, SCAN_FP_AND_ONMOVE, FENpos, onMove);
(void) strlcat(FENpos, " ", sizeof FENpos);
(void) strlcat(FENpos, onMove, sizeof FENpos);
(void) strlcpy(ptmp, "", sizeof tmp);
if (fgets(ptmp, sizeof tmp, fp) == NULL ||
feof(fp))
break;
sscanf(ptmp, "%[^*\n]", LONG);
if ((LONG_book[i] = malloc(sizeof(LONG_entry))) == NULL)
err(1, "Cound not alloc mem for LONG entry %d", i);
(void) strlcpy(LONG_book[i]->LONG, LONG,
sizeof(LONG_book[i]->LONG));
(void) strlcpy(LONG_book[i]->FENpos, FENpos,
sizeof(LONG_book[i]->FENpos));
++i;
}
fclose(fp);
LONG_book[i] = NULL;
fprintf(stderr, "%d entries in LONG book\n", i);
LONG_entries = i;
}
PUBLIC void
BookInit(void)
{
ECO_init();
NIC_init();
LONG_init();
}
PUBLIC char *
getECO(int g)
{
static char ECO[4];
#ifndef IGNORE_ECO
int i, flag, l, r, x;
if (parray[garray[g].white].private ||
parray[garray[g].black].private) {
(void) strlcpy(ECO, "---", sizeof ECO);
return ECO;
} else {
if (garray[g].type == TYPE_WILD) {
(void) strlcpy(ECO, "---", sizeof ECO);
return ECO;
} else if (garray[g].moveList == NULL) {
(void) strlcpy(ECO, "***", sizeof ECO);
return ECO;
} else {
(void) strlcpy(ECO, "A00", sizeof ECO);
}
}
flag = 0;
i = garray[g].numHalfMoves;
while (i > 0 && !flag) {
l = 0;
r = (ECO_entries - 1);
while ((r >= l) && !flag) {
x = ((l + r) / 2);
if (fencmp(garray[g].moveList[i].FENpos,
ECO_book[x]->FENpos) < 0)
r = (x - 1);
else
l = (x + 1);
if (!fencmp(garray[g].moveList[i].FENpos,
ECO_book[x]->FENpos)) {
(void)strlcpy(ECO, ECO_book[x]->ECO,
sizeof ECO);
flag = 1;
}
}
i--;
} /* while */
#else
(void) strlcpy(ECO, "---", sizeof ECO);
#endif
return ECO;
}
PUBLIC int
com_eco(int p, param_list param)
{
#ifndef IGNORE_ECO
int g1, p1;
int i, flag = 0, x, l, r;
if (param[0].type == TYPE_NULL) { // own game
if (parray[p].game < 0) {
pprintf(p, "You are not playing or examining a game."
"\n");
return COM_OK;
}
g1 = parray[p].game;
if (garray[g1].status != GAME_EXAMINE && !pIsPlaying(p))
return COM_OK;
} else {
if ((g1 = GameNumFromParam(p, &p1, ¶m[0])) < 0)
return COM_OK;
if (g1 >= g_num ||
(garray[g1].status != GAME_ACTIVE &&
garray[g1].status != GAME_EXAMINE)) {
pprintf(p, "There is no such game.\n");
return COM_OK;
}
}
if ((parray[garray[g1].white].private ||
parray[garray[g1].black].private) &&
parray[p].adminLevel == 0) {
pprintf(p, "Sorry - that game is private.\n");
return COM_OK;
} else {
if (garray[g1].type == TYPE_WILD) {
pprintf(p, "That game is a wild game.\n");
return COM_OK;
}
}
pprintf(p, "Info about game %d: \"%s vs. %s\"\n\n", (g1 + 1),
garray[g1].white_name,
garray[g1].black_name);
if (garray[g1].moveList == NULL)
return COM_OK;
/*
* ECO
*/
flag = 0;
i = garray[g1].numHalfMoves;
while (i > 0 && !flag) {
l = 0;
r = (ECO_entries - 1);
while (r >= l && !flag) {
x = ((l + r) / 2);
if (fencmp(garray[g1].moveList[i].FENpos,
ECO_book[x]->FENpos) < 0)
r = (x - 1);
else
l = (x + 1);
if (!fencmp(garray[g1].moveList[i].FENpos,
ECO_book[x]->FENpos)) {
pprintf(p, " ECO[%3d]: %s\n", i,
ECO_book[x]->ECO);
flag = 1;
}
}
i--;
} /* while */
/*
* NIC
*/
flag = 0;
i = garray[g1].numHalfMoves;
while (i > 0 && !flag) {
l = 0;
r = (NIC_entries - 1);
while (r >= l && !flag) {
x = ((l + r) / 2);
if (fencmp(garray[g1].moveList[i].FENpos,
NIC_book[x]->FENpos) < 0)
r = (x - 1);
else
l = (x + 1);
if (!fencmp(garray[g1].moveList[i].FENpos,
NIC_book[x]->FENpos)) {
pprintf(p, " NIC[%3d]: %s\n", i,
NIC_book[x]->NIC);
flag = 1;
}
}
i--;
} /* while */
/*
* LONG
*/
flag = 0;
i = garray[g1].numHalfMoves;
while (i > 0 && !flag) {
l = 0;
r = (LONG_entries - 1);
while (r >= l && !flag) {
x = ((l + r) / 2);
if (fencmp(garray[g1].moveList[i].FENpos,
LONG_book[x]->FENpos) < 0)
r = (x - 1);
else
l = (x + 1);
if (!fencmp(garray[g1].moveList[i].FENpos,
LONG_book[x]->FENpos)) {
pprintf(p, " LONG[%3d]: %s\n", i,
LONG_book[x]->LONG);
flag = 1;
}
}
i--;
} /* while */
#else
pprintf(p, "ECO not available... out of service!.\n");
#endif
return COM_OK;
}
|