-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathepdglue.c
2357 lines (2265 loc) · 72 KB
/
epdglue.c
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*>>> epdglue.c: glue to connect Crafty to the EPD Kit routines */
#if defined(EPD)
/* Revised: 1996.04.21 */
/*
Copyright (C) 1996 by Steven J. Edwards ([email protected])
All rights reserved. This code may be freely redistibuted and used by
both research and commerical applications. No warranty exists.
*/
/*
The contents of this source file form the programmatic glue between
the host program Crafty and the EPD Kit. Therefore, this file will
have to be changed if used with a different host program. Also, the
contents of the prototype include file (epdglue.h) may also require
modification for a different host.
The contents of the other source files in the EPD Kit (epddefs.h,
epd.h, and epd.c) should not have to be changed for different hosts.
*/
/*
This file was originally prepared on an Apple Macintosh using the
Metrowerks CodeWarrior 6 ANSI C compiler. Tabs are set at every
four columns. Further testing and development was performed on a
generic PC running Linux 1.2.9 and using the gcc 2.6.3 compiler.
*/
/* system includes */
# if defined(UNIX)
# include <unistd.h>
# endif
# include <ctype.h>
# include <time.h>
# if !defined(UNIX)
# include <process.h>
# endif
/* Crafty includes */
# include "chess.h"
# include "data.h"
/* EPD Kit definitions (host program independent) */
# include "epddefs.h"
/* EPD Kit routine prototypes (host program independent) */
# include "epd.h"
/* prototypes for this file (host program dependent) */
# include "epdglue.h"
/* EPD glue command type */
typedef siT egcommT, *egcommptrT;
# define egcommL 26
# define egcomm_nil (-1)
# define egcomm_epdapgn 0 /* append a PGN game to a file */
# define egcomm_epdbfix 1 /* fix file for Bookup import */
# define egcomm_epdcert 2 /* display certain evaluation (if possible) */
# define egcomm_epdcics 3 /* slave to an Internet Chess Server */
# define egcomm_epdcomm 4 /* slave to the Duplex referee program */
# define egcomm_epddpgn 5 /* display the current game in PGN */
# define egcomm_epddsml 6 /* display SAN move list */
# define egcomm_epddstr 7 /* display PGN Seven Tag Roster */
# define egcomm_epddtpv 8 /* display PGN tag pair value */
# define egcomm_epdenum 9 /* enumerate EPD file */
# define egcomm_epdhelp 10 /* display EPD help */
# define egcomm_epdlink 11 /* slave to the Argus referee program */
# define egcomm_epdlpgn 12 /* load a PGN game from a file */
# define egcomm_epdlrec 13 /* load an EPD record form a file */
# define egcomm_epdmore 14 /* more help */
# define egcomm_epdnoop 15 /* no operation */
# define egcomm_epdpfdn 16 /* process file: data normalization */
# define egcomm_epdpfdr 17 /* process file: data repair */
# define egcomm_epdpfga 18 /* process file: general analysis */
# define egcomm_epdpflc 19 /* process file: locate cooks */
# define egcomm_epdpfop 20 /* process file: operation purge */
# define egcomm_epdscor 21 /* score EPD benchmark result file */
# define egcomm_epdshow 22 /* show EPD four fields for current position */
# define egcomm_epdspgn 23 /* save a PGN game to a file */
# define egcomm_epdstpv 24 /* set PGN tag pair value */
# define egcomm_epdtest 25 /* developer testing */
/* output text buffer */
# define tbufL 256
static char tbufv[tbufL];
/* EPD glue command strings */
static charptrT egcommstrv[egcommL];
/* EPD glue command string descriptions */
static charptrT eghelpstrv[egcommL];
/* EPD glue command parameter counts (includes command token) */
/* the current (default) EPD game structure */
static gamptrT default_gamptr;
/*--> EGPrint: print a string to the output */
static
void EGPrint(charptrT s) {
/* this is an internal EPD glue routine */
/*
This routine is provided as an alternative to direct writing to the
standard output. All EPD glue printing output goes through here. The
idea is that the host program may have some special requirements for
printing output (like a window display), so a convenient single point
is provided to handle this.
Note that there is no corresponding routine for reading from the
standard input because the EPD glue does no interactive reading, except
for a single getchar() call in the epdhelp display pager.
*/
/* for Crafty, the standard output is used */
printf("%s", s);
return;
}
/*--> EGPrintTB: print the contents of the text buffer */
static
void EGPrintTB(void) {
/* this is an internal EPD glue routine */
EGPrint(tbufv);
return;
}
/*--> EGPL: print a string followed by a newline */
static
void EGPL(charptrT s) {
/* this is an internal EPD glue routine */
EGPrint(s);
EGPrint("\n");
return;
}
/*--> EGLocateCommand: locate an EPD glue command from a token */
static egcommT EGLocateCommand(charptrT s) {
egcommT egcomm, index;
/* this is an internal EPD glue routine */
/* set the default return value: no match */
egcomm = egcomm_nil;
/* scan the EPD glue command string vector */
index = 0;
while ((index < egcommL) && (egcomm == egcomm_nil))
if (strcmp(s, egcommstrv[index]) == 0)
egcomm = index;
else
index++;
return egcomm;
}
/*--> EGMapFromHostColor: map a color from the host to the EPD style */
static cT EGMapFromHostColor(siT color) {
cT c;
/* this is an internal glue routine */
/* map from Crafty's color representation */
if (color == 1)
c = c_w;
else
c = c_b;
return c;
}
/*--> EGMapToHostColor: map a color to the host from the EPD style */
static siT EGMapToHostColor(cT c) {
siT color;
/* this is an internal glue routine */
/* map to Crafty's color representation */
if (c == c_w)
color = 1;
else
color = 0;
return color;
}
/*--> EGMapFromHostCP: map a color piece from the host to the EPD style */
static cpT EGMapFromHostCP(siT hostcp) {
cpT cp = 0;
/* this is an internal glue routine */
/* map from Crafty's color-piece representation */
switch (hostcp) {
case -queen:
cp = cp_bq;
break;
case -rook:
cp = cp_br;
break;
case -bishop:
cp = cp_bb;
break;
case -king:
cp = cp_bk;
break;
case -knight:
cp = cp_bn;
break;
case -pawn:
cp = cp_bp;
break;
case 0:
cp = cp_v0;
break;
case pawn:
cp = cp_wp;
break;
case knight:
cp = cp_wn;
break;
case king:
cp = cp_wk;
break;
case bishop:
cp = cp_wb;
break;
case rook:
cp = cp_wr;
break;
case queen:
cp = cp_wq;
break;
};
return cp;
}
/*--> EGMapToHostCP: map a color piece to the host from the EPD style */
static siT EGMapToHostCP(cpT cp) {
siT hostcp = 0;
/* this is an internal glue routine */
/* map to Crafty's color-piece representation */
switch (cp) {
case cp_wp:
hostcp = pawn;
break;
case cp_wn:
hostcp = knight;
break;
case cp_wb:
hostcp = bishop;
break;
case cp_wr:
hostcp = rook;
break;
case cp_wq:
hostcp = queen;
break;
case cp_wk:
hostcp = king;
break;
case cp_bp:
hostcp = -pawn;
break;
case cp_bn:
hostcp = -knight;
break;
case cp_bb:
hostcp = -bishop;
break;
case cp_br:
hostcp = -rook;
break;
case cp_bq:
hostcp = -queen;
break;
case cp_bk:
hostcp = -king;
break;
case cp_v0:
hostcp = 0;
break;
};
return hostcp;
}
/*--> EGMapFromHostSq: map square index from host style */
static sqT EGMapFromHostSq(siT index) {
sqT sq;
/* this is an internal glue routine */
/* Crafty's square index is the same as the EPD Kit square index */
sq = index;
return sq;
}
/*--> EGMapToHostSq: map square index to host style */
static siT EGMapToHostSq(sqT sq) {
siT index;
/* this is an internal glue routine */
/* Crafty's square index is the same as the EPD Kit square index */
index = sq;
return index;
}
/*--> EGMapFromHostScore: map score from host style */
static cpevT EGMapFromHostScore(liT score) {
cpevT cpev;
liT distance;
/* this is an internal EPD glue routine */
/* check for a forced mate */
if (score >= (MATE - MAXPLY)) {
/* convert forced mate score */
distance = (MATE - score) / 2;
cpev = synth_mate(distance);
} else if (score <= (MAXPLY - MATE)) {
/* convert forced loss score */
distance = (MATE + score) / 2;
cpev = synth_loss(distance);
} else {
/* convert regular score */
cpev = score;
};
return cpev;
}
/*--> EGMapFromHostMove: map move from host style to EPD style */
static mT EGMapFromHostMove(liT move) {
mT m;
siT flag;
/* this is an internal EPD glue routine */
/* the EPD current position must be properly set */
m.m_flag = 0;
m.m_frsq = EGMapFromHostSq((siT) From(move));
m.m_tosq = EGMapFromHostSq((siT) To(move));
m.m_frcp = EPDFetchCP(m.m_frsq);
m.m_tocp = EPDFetchCP(m.m_tosq);
/* determine special case move indication */
flag = 0;
if (!flag)
if ((m.m_frcp == cp_wk) && (m.m_frsq == sq_e1) && (m.m_tosq == sq_g1)) {
m.m_scmv = scmv_cks;
flag = 1;
};
if (!flag)
if ((m.m_frcp == cp_bk) && (m.m_frsq == sq_e8) && (m.m_tosq == sq_g8)) {
m.m_scmv = scmv_cks;
flag = 1;
};
if (!flag)
if ((m.m_frcp == cp_wk) && (m.m_frsq == sq_e1) && (m.m_tosq == sq_c1)) {
m.m_scmv = scmv_cqs;
flag = 1;
};
if (!flag)
if ((m.m_frcp == cp_bk) && (m.m_frsq == sq_e8) && (m.m_tosq == sq_c8)) {
m.m_scmv = scmv_cqs;
flag = 1;
};
if (!flag)
if ((m.m_frcp == cp_wp) && (m.m_tosq == EPDFetchEPSQ())) {
m.m_scmv = scmv_epc;
flag = 1;
};
if (!flag)
if ((m.m_frcp == cp_bp) && (m.m_tosq == EPDFetchEPSQ())) {
m.m_scmv = scmv_epc;
flag = 1;
};
if (!flag)
if (Promote(move) != 0) {
switch (Promote(move)) {
case knight:
m.m_scmv = scmv_ppn;
break;
case bishop:
m.m_scmv = scmv_ppb;
break;
case rook:
m.m_scmv = scmv_ppr;
break;
case queen:
m.m_scmv = scmv_ppq;
break;
};
flag = 1;
};
if (!flag)
m.m_scmv = scmv_reg;
return m;
}
/*--> EGGetIndexedHostPosition: copy from indexed host position */
static
void EGGetIndexedHostPosition(TREE * RESTRICT tree, siT posdex, int active) {
sqT sq;
rbT rb;
cT actc;
castT cast;
sqT epsq;
siT hmvc;
siT fmvn;
/* this is an internal EPD glue routine */
/*
This routine is called from within the EPD glue to copy the host program's
current position at the given dpeth into the EPD Kit. Information about
the previous EPD Kit current position is lost.
*/
/* read from the host piece placement */
for (sq = sq_a1; sq <= sq_h8; sq++)
rb.rbv[sq] = EGMapFromHostCP(tree->position.board[EGMapToHostSq(sq)]);
/* read from the host piece active color */
actc = EGMapFromHostColor((siT) active);
/* read from the host piece castling availability */
cast = 0;
switch (tree->status[posdex].castle[1]) {
case 0:
break;
case 1:
cast |= cf_wk;
break;
case 2:
cast |= cf_wq;
break;
case 3:
cast |= cf_wk | cf_wq;
break;
};
switch (tree->status[posdex].castle[0]) {
case 0:
break;
case 1:
cast |= cf_bk;
break;
case 2:
cast |= cf_bq;
break;
case 3:
cast |= cf_bk | cf_bq;
break;
};
/* read from the host piece en passant target square */
epsq = sq_nil;
if (tree->status[posdex].enpassant_target != 0) {
sq = sq_a1;
while ((epsq == sq_nil) && (sq <= sq_h8))
if (tree->status[posdex].enpassant_target == EGMapToHostSq(sq))
epsq = sq;
else
sq++;
};
/* read from the host halfmove clock */
hmvc = tree->status[posdex].reversible;
/* read from the host fullmove number */
fmvn = move_number;
/* set the EPD current position */
EPDSetCurrentPosition(&rb, actc, cast, epsq, hmvc, fmvn);
return;
}
/*--> EGGetHostPosition: copy from host position to EPD Kit position */
static
void EGGetHostPosition(void) {
/* this is an internal EPD glue routine */
/*
This routine is called from within the EPD glue to copy the host program's
current position into the EPD Kit. Information about the previous EPD Kit
current position is lost.
*/
/* transfer from ply zero host position */
EGGetIndexedHostPosition(block[0], 0, game_wtm);
return;
}
/*--> EGPutHostPosition: copy from EPD Kit position to host position */
static
void EGPutHostPosition(void) {
sqT sq;
rbT rb;
cT actc;
castT cast;
sqT epsq;
siT hmvc;
siT fmvn;
siT index;
TREE *tree = block[0];
/* this is an internal EPD glue routine */
/*
This routine is called from within the EPD glue to copy the EPD Kit's current
position into the host program. If the previous host program current position
is different from the new position, then information about the previous host
program current position is lost. This means that the host program preserves
history information if and only if such preservation is appropriate.
Actually, the host position data is completely overwritten, so the above
comment is temporarily false, but will be true as developement proceeds.
*/
/* fetch the EPD current position data items */
rb = *EPDFetchBoard();
actc = EPDFetchACTC();
cast = EPDFetchCAST();
epsq = EPDFetchEPSQ();
hmvc = EPDFetchHMVC();
fmvn = EPDFetchFMVN();
/* copy the board into the host board */
for (sq = sq_a1; sq <= sq_h8; sq++)
tree->position.board[EGMapToHostSq(sq)] = EGMapToHostCP(rb.rbv[sq]);
/* copy the active color */
game_wtm = EGMapToHostColor(actc);
/* copy the castling availibility */
tree->status[0].castle[1] = 0;
if (cast & cf_wk)
tree->status[0].castle[1] += 1;
if (cast & cf_wq)
tree->status[0].castle[1] += 2;
tree->status[0].castle[0] = 0;
if (cast & cf_bk)
tree->status[0].castle[0] += 1;
if (cast & cf_bq)
tree->status[0].castle[0] += 2;
/* copy the en passant target square */
if (epsq == sq_nil)
tree->status[0].enpassant_target = 0;
else
tree->status[0].enpassant_target = EGMapToHostSq(epsq);
/* copy the halfmove clock */
tree->status[0].reversible = hmvc;
/* copy the fullmove number */
move_number = fmvn;
/* set secondary host data items */
SetChessBitBoards(tree);
rep_index = 0;
tree->rep_list[0] = HashKey;
moves_out_of_book = 0;
last_mate_score = 0;
/* clear the host killer information */
for (index = 0; index < MAXPLY; index++) {
tree->killers[index].move1 = 0;
tree->killers[index].move2 = 0;
}
/* clear miscellaneous host items */
ponder_move = 0;
last_pv.pathd = 0;
last_pv.pathl = 0;
over = 0;
return;
}
/*--> EGEncodeHostHistory: generate a string from host move history */
static charptrT EGEncodeHostHistory(void) {
charptrT sptr;
gamptrT gamptr;
pgnstrT pgnstr;
mptrT mptr;
siT flag;
siT ch = 0;
charptrT s;
mT m;
/* this works only for games starting form the initial position */
/* set default return value: failure */
sptr = NULL;
/* set okay */
flag = 1;
/* reset the EPD current position */
EPDReset();
/* make sure the host history file exists */
if (history_file == NULL)
flag = 0;
else {
rewind(history_file);
ch = fgetc(history_file);
};
/* open a game structure */
gamptr = EPDGameOpen();
/* copy tag data from default game structure */
if (default_gamptr != NULL)
for (pgnstr = 0; pgnstr < pgnstrL; pgnstr++)
EPDPGNPutSTR(gamptr, pgnstr, EPDPGNGetSTR(default_gamptr, pgnstr));
/* copy GTIM from default game structure */
if (default_gamptr != NULL)
EPDPutGTIM(gamptr, EPDGetGTIM(default_gamptr));
/* read the host history file */
while (flag && (ch != EOF)) {
/* skip whitespace */
while ((ch != EOF) && isspace(ch))
ch = fgetc(history_file);
/* if not EOF, then construct a move */
if (ch != EOF) {
/* attach the first character */
s = EPDStringGrab("");
s = EPDStringAppendChar(s, (char) ch);
ch = fgetc(history_file);
/* attach the remaining characters */
while ((ch != EOF) && !isspace(ch)) {
s = EPDStringAppendChar(s, (char) ch);
ch = fgetc(history_file);
};
/* match the move */
EPDGenMoves();
mptr = EPDSANDecodeAux(s, 0);
if (mptr == NULL)
flag = 0;
else {
/* execute the move */
m = *mptr;
EPDGameAppendMove(gamptr, &m);
EPDExecuteUpdate(&m);
EPDCollapse();
};
/* release move buffer storage */
EPDMemoryFree(s);
};
};
/* construct the string representation of the game */
if (flag)
sptr = EPDPGNHistory(gamptr);
/* clean up if there was a problem */
if (!flag)
if (sptr != NULL) {
EPDMemoryFree(sptr);
sptr = NULL;
};
/* close the game structure */
EPDGameClose(gamptr);
return sptr;
}
/*--> EGIterate: wrapper function for host Iterate() call */
static
int EGIterate(siT wtm_flag, siT think_flag) {
int value;
epdptrT epdptr;
TREE *tree = block[0];
EPDGenMoves();
/* save current kit position */
epdptr = EPDGetCurrentPosition();
/* more than one move, execute regular search */
value = Iterate(wtm_flag, think_flag, 0);
/* restore kit position */
EPDRealize(epdptr);
EPDReleaseEPD(epdptr);
EPDGenMoves();
last_pv = tree->pv[0];
last_value = value;
return value;
}
/*--> EGCommHandler: callback routine for handling Duplex events */
static epdptrT EGCommHandler(epdptrT epdptr0, siptrT flagptr) {
epdptrT epdptr1;
eopptrT eopptr;
charptrT s;
fptrT fptr;
mptrT mptr;
mT m;
sanT san;
int move;
char tv[tL];
TREE *tree = block[0];
/* set flag: no errors (so far) */
*flagptr = 1;
/* clear result */
epdptr1 = NULL;
/* process according to input referee command */
switch (EPDExtractRefcomIndex(epdptr0)) {
case refcom_conclude:
/* end of game */
s = EPDPGNHistory(default_gamptr);
if (s == NULL)
*flagptr = 0;
else {
/* append game to PGN output file */
sprintf(tv, "c%05hd.pgn", ((siT) getpid()));
fptr = fopen(tv, "a");
if (fptr == NULL)
*flagptr = 0;
else {
fprintf(fptr, "%s", s);
fclose(fptr);
};
EPDMemoryFree(s);
};
/* clean up and remove the temporary history file */
if (history_file != NULL) {
fclose(history_file);
history_file = NULL;
};
sprintf(tv, "h%05hd.pml", ((siT) getpid()));
remove(tv);
/* close the game structure */
if (default_gamptr != NULL) {
EPDGameClose(default_gamptr);
default_gamptr = NULL;
};
break;
case refcom_disconnect:
/* channel shutdown */
/* clean up and remove the temporary history file */
if (history_file != NULL) {
fclose(history_file);
history_file = NULL;
};
sprintf(tv, "h%05hd.pml", ((siT) getpid()));
remove(tv);
/* ensure game structure is closed */
if (default_gamptr != NULL) {
EPDGameClose(default_gamptr);
default_gamptr = NULL;
};
break;
case refcom_execute:
/* execute the supplied move */
eopptr = EPDLocateEOPCode(epdptr0, epdso_sm);
move = InputMove(tree, 0, game_wtm, 0, 0, eopptr->eop_headeov->eov_str);
if (history_file) {
fseek(history_file, ((((move_number - 1) * 2) + 1 - game_wtm) * 10),
SEEK_SET);
fprintf(history_file, "%9s\n", eopptr->eop_headeov->eov_str);
}
MakeMoveRoot(tree, game_wtm, move);
game_wtm = Flip(game_wtm);
if (game_wtm)
move_number++;
/* execute the move in the EPD Kit */
EPDGenMoves();
mptr = EPDSANDecodeAux(eopptr->eop_headeov->eov_str, 0);
m = *mptr;
EPDGameAppendMove(default_gamptr, &m);
EPDExecuteUpdate(&m);
EPDCollapse();
break;
case refcom_fault:
/* a problem occurred */
EGPL("EPDCommHandler: refcom fault");
*flagptr = 0;
break;
case refcom_inform:
/* process incidental information */
EPDCopyOutPTP(default_gamptr, epdptr0);
/* update the current game termination marker */
s = EPDPGNGetSTR(default_gamptr, pgnstr_result);
if ((s == NULL) || (strcmp(s, "*") == 0))
EPDPutGTIM(default_gamptr, gtim_u);
else if (strcmp(s, "1-0") == 0)
EPDPutGTIM(default_gamptr, gtim_w);
else if (strcmp(s, "0-1") == 0)
EPDPutGTIM(default_gamptr, gtim_b);
else if (strcmp(s, "1/2-1/2") == 0)
EPDPutGTIM(default_gamptr, gtim_d);
break;
case refcom_respond:
/* execute the supplied move (if any) */
eopptr = EPDLocateEOPCode(epdptr0, epdso_sm);
if (eopptr != NULL) {
move =
InputMove(tree, 0, game_wtm, 0, 0, eopptr->eop_headeov->eov_str);
if (history_file) {
fseek(history_file, ((((move_number - 1) * 2) + 1 - game_wtm) * 10),
SEEK_SET);
fprintf(history_file, "%9s\n", eopptr->eop_headeov->eov_str);
}
MakeMoveRoot(tree, game_wtm, move);
game_wtm = Flip(game_wtm);
if (game_wtm)
move_number++;
/* execute the move in the EPD Kit */
EPDGenMoves();
mptr = EPDSANDecodeAux(eopptr->eop_headeov->eov_str, 0);
m = *mptr;
EPDGameAppendMove(default_gamptr, &m);
EPDExecuteUpdate(&m);
EPDCollapse();
};
/* calculate move + respond */
EPDGenMoves();
if (EPDFetchMoveCount() > 0) {
/* at least one move exists, set up for search */
ponder_move = 0;
thinking = 1;
last_pv.pathd = 0;
last_pv.pathl = 0;
tree->status[1] = tree->status[0];
/* search */
EGIterate((siT) game_wtm, (siT) think);
/* process search result */
strcpy(tv, OutputMove(tree, 0, game_wtm, last_pv.path[1]));
move = last_pv.path[1];
/* locate SAN move */
mptr = EPDSANDecodeAux(tv, 0);
m = *mptr;
EPDSANEncode(&m, san);
/* output to temporary history file */
if (history_file) {
fseek(history_file, ((((move_number - 1) * 2) + 1 - game_wtm) * 10),
SEEK_SET);
fprintf(history_file, "%9s\n", san);
}
/* update host position */
MakeMoveRoot(tree, game_wtm, move);
game_wtm = Flip(game_wtm);
if (game_wtm)
move_number++;
/* create reply EPD structure */
epdptr1 = EPDGetCurrentPosition();
/* add in referee request */
EPDAddOpSym(epdptr1, epdso_refreq, EPDFetchRefreqStr(refreq_reply));
/* add in supplied move */
EPDAddOpSym(epdptr1, epdso_sm, san);
/* execute the move in the EPD Kit */
mptr = EPDSANDecodeAux(san, 0);
m = *mptr;
EPDGameAppendMove(default_gamptr, &m);
EPDExecuteUpdate(&m);
EPDCollapse();
} else {
/* no moves exist, so no move response possible */
epdptr1 = EPDGetCurrentPosition();
eopptr = EPDCreateEOPCode(epdso_refreq);
EPDAppendEOV(eopptr,
EPDCreateEOVSym(EPDFetchRefreqStr(refreq_reply)));
EPDAppendEOP(epdptr1, eopptr);
};
break;
case refcom_reset:
/* reset EPD Kit */
EPDReset();
/* reset host for a new game */
ponder = 0;
ponder_move = 0;
last_pv.pathd = 0;
last_pv.pathl = 0;
InitializeChessBoard(tree);
InitializeHashTables(0);
game_wtm = 1;
move_number = 1;
/* open the temporary history file */
if (history_file != NULL) {
fclose(history_file);
history_file = NULL;
};
sprintf(tv, "h%05hd.pml", ((siT) getpid()));
remove(tv);
history_file = fopen(tv, "w+");
/* open the current game structure */
if (default_gamptr != NULL)
EPDGameClose(default_gamptr);
default_gamptr = EPDGameOpen();
break;
case refcom_nil:
*flagptr = 0;
break;
};
/* clean up if there was a problem */
if (!(*flagptr))
if (epdptr1 != NULL) {
EPDReleaseEPD(epdptr1);
epdptr1 = NULL;
};
return epdptr1;
}
/*--> EGProcessAPGN: process the EG command epdapgn */
static siT EGProcessAPGN(void) {
siT flag;
charptrT s;
fptrT fptr;
/* this is an internal EPD glue routine */
/* set the default return value: success */
flag = 1;
/* parameter count check */
if (EPDTokenCount() != 2) {
EGPL("This command takes one parameter.");
flag = 0;
};
/* process the epdapgn command */
if (flag) {
s = EGEncodeHostHistory();
if (s == NULL)
flag = 0;
else {
fptr = fopen(EPDTokenFetch(1), "a");
if (fptr == NULL)
flag = 0;
else {
fprintf(fptr, "%s", s);
fclose(fptr);
};
EPDMemoryFree(s);
};
};
return flag;
}
/*--> EGProcessBFIX: process the EG command epdbfix */
static siT EGProcessBFIX(void) {
siT flag;
fptrT fptr0, fptr1;
eopptrT eopptr;
epdptrT epdptr, nptr;
charptrT s;
char ev[epdL];
/* this is an internal EPD glue routine */
/* set the default return value: success */
flag = 1;
/* clear the file pointers */
fptr0 = fptr1 = NULL;
/* parameter count check */
if (EPDTokenCount() != 3) {
EGPL("This command takes two parameters");
flag = 0;
};
/* set up the input file */
if (flag) {
fptr0 = fopen(EPDTokenFetch(1), "r");
if (fptr0 == NULL) {
sprintf(tbufv, "Can't open %s for reading\n", EPDTokenFetch(1));
EGPrintTB();
flag = 0;
};
};
/* set up the output file */
if (flag) {
fptr1 = fopen(EPDTokenFetch(2), "w");
if (fptr1 == NULL) {
sprintf(tbufv, "Can't open %s for writing\n", EPDTokenFetch(2));
EGPrintTB();
flag = 0;
};
};
/* scan the file */
if (flag)
while (flag && (fgets(ev, (epdL - 1), fptr0) != NULL)) {
/* decode the record into an EPD structure */
epdptr = EPDDecode(ev);
/* check record decode validity */
if (epdptr == NULL)
flag = 0;
else {
/* clone the input EPD structure base */
nptr = EPDCloneEPDBase(epdptr);
/* copy the ce operation */
eopptr = EPDLocateEOPCode(epdptr, epdso_ce);
if (eopptr != NULL)
EPDAppendEOP(nptr, EPDCloneEOP(eopptr));
/* copy the pv operation */
eopptr = EPDLocateEOPCode(epdptr, epdso_pv);
if (eopptr != NULL)
EPDAppendEOP(nptr, EPDCloneEOP(eopptr));
/* output the new EPD strucutre */
s = EPDEncode(nptr);
fprintf(fptr1, "%s\n", s);
EPDMemoryFree(s);
/* deallocate both EPD structures */
EPDReleaseEPD(epdptr);
EPDReleaseEPD(nptr);
};
};
/* ensure file close */
if (fptr0 != NULL)
fclose(fptr0);
if (fptr1 != NULL)
fclose(fptr1);
return flag;
}
/*--> EGProcessCICS: process the EG command epdcics */
static siT EGProcessCICS(void) {
siT flag;
/* this is an internal EPD glue routine */
/* set the default return value: success */
flag = 1;
/* parameter count check */
if (EPDTokenCount() != 3) {
EGPL("This command takes two parameters");
flag = 0;
};
/* process the epdcics command */
if (flag) {
EGPL("This command is not yet implemented.");
flag = 0;
};
return flag;
}
/*--> EGProcessCOMM: process the EG command epdcomm */
static siT EGProcessCOMM(void) {
siT flag;
fptrT save_history_file;
gamptrT save_default_gamptr;
/* this is an internal EPD glue routine */
/* set the default return value: success */
flag = 1;
/* parameter count check */
if (EPDTokenCount() != 2) {
EGPL("This command takes one parameter.");
flag = 0;
};
/* process the epdcomm command */
if (flag) {
/* save the history file pointer */
save_history_file = history_file;
history_file = NULL;
/* save the game structure */
save_default_gamptr = default_gamptr;
default_gamptr = NULL;
/* process via callback */
EGPL("Duplex slave mode begin");
flag = EPDComm(EGCommHandler, EPDTokenFetch(1));
EGPL("Duplex slave mode end");
/* restore the game structure */