-
Notifications
You must be signed in to change notification settings - Fork 4
/
thordb.c
2989 lines (2446 loc) · 72.2 KB
/
thordb.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
/*
File: thordb.c
Created: March 30, 1999
Modified: October 6, 2002
Author: Gunnar Andersson (gunnar@radagast.se)
Contents: An interface to the Thor database designed to
perform fast lookup operations.
*/
#include "porting.h"
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bitboard.h"
#include "constant.h"
#include "error.h"
#include "macros.h"
#include "moves.h"
#include "myrandom.h"
#include "patterns.h"
#include "safemem.h"
#include "texts.h"
#include "thordb.h"
#include "thorop.c"
/* This constant denotes "No stored patterns are available". */
#define NOT_AVAILABLE -1
/* The index in the player database which corresponds to an unknown player */
#define UNKNOWN_PLAYER 0
/* The possible statuses for an opening when matched with a position */
#define OPENING_INCONCLUSIVE 0
#define OPENING_MATCH 1
#define OPENING_MISMATCH 2
/* The default and maximum number of criteria to use when sorting
the set of matching games. */
#define DEFAULT_SORT_CRITERIA 5
#define MAX_SORT_CRITERIA 10
/* Type definitions */
typedef char int_8;
typedef short int_16;
typedef int int_32;
typedef struct {
int creation_century;
int creation_year;
int creation_month;
int creation_day;
int game_count;
int item_count;
int origin_year;
int reserved;
} PrologType;
typedef struct {
int lex_order;
int selected;
const char *name;
} TournamentType;
typedef struct {
PrologType prolog;
char *name_buffer;
int count;
TournamentType *tournament_list;
} TournamentDatabaseType;
typedef struct {
int lex_order;
int is_program;
int selected;
const char *name;
} PlayerType;
typedef struct {
PrologType prolog;
char *name_buffer;
int count;
PlayerType *player_list;
} PlayerDatabaseType;
typedef char MoveListType[64];
typedef struct ThorOpeningNode_ {
unsigned int hash1, hash2;
int current_match;
int frequency;
int matching_symmetry;
char child_move;
char sibling_move;
struct ThorOpeningNode_ *child_node;
struct ThorOpeningNode_ *sibling_node;
struct ThorOpeningNode_ *parent_node;
} ThorOpeningNode;
typedef struct {
short tournament_no;
short black_no;
short white_no;
short actual_black_score;
short perfect_black_score;
char moves[60];
short move_count;
char black_disc_count[61];
ThorOpeningNode *opening;
struct DatabaseType_ *database;
unsigned int shape_hi, shape_lo;
short shape_state_hi, shape_state_lo;
unsigned int corner_descriptor;
int sort_order;
short matching_symmetry;
short passes_filter;
} GameType;
typedef struct DatabaseType_ {
PrologType prolog;
GameType *games;
int count;
struct DatabaseType_ *next;
} DatabaseType;
typedef struct {
double average_black_score;
double next_move_score[100];
int match_count;
int black_wins;
int draws;
int white_wins;
int median_black_score;
int allocation;
int next_move_frequency[100];
GameType **match_list;
} SearchResultType;
typedef struct {
int game_categories;
int first_year;
int last_year;
PlayerFilterType player_filter;
} FilterType;
/* Local variables */
static int thor_game_count, thor_database_count;
static int thor_side_to_move;
static int thor_sort_criteria_count;
static int thor_games_sorted;
static int thor_games_filtered;
static int thor_row_pattern[8], thor_col_pattern[8];
static int thor_board[100];
static int b1_b1_map[100], g1_b1_map[100], g8_b1_map[100], b8_b1_map[100];
static int a2_b1_map[100], a7_b1_map[100], h7_b1_map[100], h2_b1_map[100];
static unsigned int primary_hash[8][6561], secondary_hash[8][6561];
static int *symmetry_map[8], *inv_symmetry_map[8];
static unsigned int move_mask_hi[100], move_mask_lo[100];
static unsigned int unmove_mask_hi[100], unmove_mask_lo[100];
static DatabaseType *database_head;
static PlayerDatabaseType players;
static SearchResultType thor_search;
static TournamentDatabaseType tournaments;
static ThorOpeningNode *root_node;
static int default_sort_order[DEFAULT_SORT_CRITERIA] = {
SORT_BY_BLACK_NAME,
SORT_BY_WHITE_NAME,
SORT_BY_YEAR_REVERSED,
SORT_BY_BLACK_SCORE,
SORT_BY_TOURNAMENT
};
static int thor_sort_order[MAX_SORT_CRITERIA];
static FilterType filter;
/*
CLEAR_THOR_BOARD
*/
static void
clear_thor_board( void ) {
int pos;
for ( pos = 11; pos <= 88; pos++ )
thor_board[pos] = EMPTY;
thor_board[45] = thor_board[54] = BLACKSQ;
thor_board[44] = thor_board[55] = WHITESQ;
}
/*
PREPARE_THOR_BOARD
Mark the positions outside the board as OUTSIDE.
*/
static void
prepare_thor_board( void ) {
int i, j;
int pos;
for ( i = 0; i < 10; i++ )
for ( j = 0, pos = 10 * i; j < 10; j++, pos++ )
if ( (i == 0) || (i == 9) || (j == 0) || (j == 9) )
thor_board[pos] = OUTSIDE;
}
/*
DIRECTIONAL_FLIP_COUNT
Count the number of discs flipped in the direction given by INC
when SQ is played by COLOR and flip those discs.
*/
INLINE static int
directional_flip_count( int sq, int inc, int color, int oppcol ) {
int count = 1;
int pt = sq + inc;
if ( thor_board[pt] == oppcol) {
pt += inc;
if ( thor_board[pt] == oppcol ) {
count++;
pt += inc;
if ( thor_board[pt] == oppcol ) {
count++;
pt += inc;
if ( thor_board[pt] == oppcol ) {
count++;
pt += inc;
if ( thor_board[pt] == oppcol ) {
count++;
pt += inc;
if ( thor_board[pt] == oppcol ) {
count++;
pt += inc;
}
}
}
}
}
if ( thor_board[pt] == color ) {
int g = count;
do {
pt -= inc;
thor_board[pt] = color;
} while( --g );
return count;
}
}
return 0;
}
/*
DIRECTIONAL_FLIP_ANY
Returns 1 if SQ is feasible for COLOR in the direction given by INC
and flip the discs which are flipped if SQ is played.
*/
INLINE static int
directional_flip_any( int sq, int inc, int color, int oppcol ) {
int pt = sq + inc;
if ( thor_board[pt] == oppcol) {
pt += inc;
if ( thor_board[pt] == oppcol ) {
pt += inc;
if ( thor_board[pt] == oppcol ) {
pt += inc;
if ( thor_board[pt] == oppcol ) {
pt += inc;
if ( thor_board[pt] == oppcol ) {
pt += inc;
if ( thor_board[pt] == oppcol ) {
pt += inc;
}
}
}
}
}
if ( thor_board[pt] == color ) {
pt -= inc;
do {
thor_board[pt] = color;
pt -= inc;
} while ( pt != sq );
return TRUE;
}
}
return FALSE;
}
/*
COUNT_FLIPS
Returns the number of discs flipped if SQNUM is played by COLOR
and flips those discs (if there are any).
*/
INLINE static int
count_flips( int sqnum, int color, int oppcol ) {
int count;
int mask;
count = 0;
mask = dir_mask[sqnum];
if ( mask & 128 )
count += directional_flip_count( sqnum, -11, color, oppcol );
if ( mask & 64 )
count += directional_flip_count( sqnum, 11, color, oppcol );
if ( mask & 32 )
count += directional_flip_count( sqnum, -10, color, oppcol );
if ( mask & 16 )
count += directional_flip_count( sqnum, 10, color, oppcol );
if ( mask & 8 )
count += directional_flip_count( sqnum, -9, color, oppcol );
if ( mask & 4 )
count += directional_flip_count( sqnum, 9, color, oppcol );
if ( mask & 2 )
count += directional_flip_count( sqnum, -1, color, oppcol );
if ( mask & 1 )
count += directional_flip_count( sqnum, 1, color, oppcol );
return count;
}
/*
ANY_FLIPS
Returns 1 if SQNUM flips any discs for COLOR, otherwise 0, and
flips those discs.
*/
INLINE static int
any_flips( int sqnum, int color, int oppcol ) {
int count;
int mask;
count = FALSE;
mask = dir_mask[sqnum];
if ( mask & 128 )
count |= directional_flip_any( sqnum, -11, color, oppcol );
if ( mask & 64 )
count |= directional_flip_any( sqnum, 11, color, oppcol );
if ( mask & 32 )
count |= directional_flip_any( sqnum, -10, color, oppcol );
if ( mask & 16 )
count |= directional_flip_any( sqnum, 10, color, oppcol );
if ( mask & 8 )
count |= directional_flip_any( sqnum, -9, color, oppcol );
if ( mask & 4 )
count |= directional_flip_any( sqnum, 9, color, oppcol );
if ( mask & 2 )
count |= directional_flip_any( sqnum, -1, color, oppcol );
if ( mask & 1 )
count |= directional_flip_any( sqnum, 1, color, oppcol );
return count;
}
/*
COMPUTE_THOR_PATTERNS
Computes the row and column patterns.
*/
INLINE static void
compute_thor_patterns( int *in_board ) {
int i, j;
int pos;
for ( i = 0; i < 8; i++ ) {
thor_row_pattern[i] = 0;
thor_col_pattern[i] = 0;
}
for ( i = 0; i < 8; i++ )
for ( j = 0, pos = 10 * i + 11; j < 8; j++, pos++ ) {
thor_row_pattern[i] += pow3[j] * in_board[pos];
thor_col_pattern[j] += pow3[i] * in_board[pos];
}
}
/*
GET_CORNER_MASK
Returns an 32-bit mask for the corner configuration. The rotation
which minimizes the numerical value is chosen.
The mask is to be interpreted as follows: There are two bits
for each corner; 00 means empty, 01 means black and 10 means white.
The bit blocks are given in the order h8h1a8a1 (MSB to LSB).
Furthermore, this 8-bit value is put in the leftmost byte if
all four corners have been played, in the rightmost byte if only
one corner has been played (obvious generalization for one or two
corners).
*/
static unsigned int
get_corner_mask( int disc_a1, int disc_a8, int disc_h1, int disc_h8 ) {
int i;
int count;
int mask_a1, mask_a8, mask_h1, mask_h8;
unsigned out_mask;
unsigned int config[8];
mask_a1 = 0;
if ( disc_a1 == BLACKSQ )
mask_a1 = 1;
else if ( disc_a1 == WHITESQ )
mask_a1 = 2;
mask_a8 = 0;
if ( disc_a8 == BLACKSQ )
mask_a8 = 1;
else if ( disc_a8 == WHITESQ )
mask_a8 = 2;
mask_h1 = 0;
if ( disc_h1 == BLACKSQ )
mask_h1 = 1;
else if ( disc_h1 == WHITESQ )
mask_h1 = 2;
mask_h8 = 0;
if ( disc_h8 == BLACKSQ )
mask_h8 = 1;
else if ( disc_h8 == WHITESQ )
mask_h8 = 2;
count = 0;
if ( disc_a1 != EMPTY )
count++;
if ( disc_a8 != EMPTY )
count++;
if ( disc_h1 != EMPTY )
count++;
if ( disc_h8 != EMPTY )
count++;
if ( count == 0 )
return 0;
config[0] = mask_a1 + 4 * mask_a8 + 16 * mask_h1 + 64 * mask_h8;
config[1] = mask_a1 + 4 * mask_h1 + 16 * mask_a8 + 64 * mask_h8;
config[2] = mask_a8 + 4 * mask_a1 + 16 * mask_h8 + 64 * mask_h1;
config[3] = mask_a8 + 4 * mask_h8 + 16 * mask_a1 + 64 * mask_h1;
config[4] = mask_h1 + 4 * mask_h8 + 16 * mask_a1 + 64 * mask_a8;
config[5] = mask_h1 + 4 * mask_a1 + 16 * mask_h8 + 64 * mask_a8;
config[6] = mask_h8 + 4 * mask_h1 + 16 * mask_a8 + 64 * mask_a1;
config[7] = mask_h8 + 4 * mask_a8 + 16 * mask_h1 + 64 * mask_a1;
out_mask = config[0];
for ( i = 1; i < 8; i++ )
out_mask = MIN( out_mask, config[i] );
return out_mask << (8 * (count - 1));
}
/*
PLAY_THROUGH_GAME
Play the MAX_MOVES first moves of GAME and update THOR_BOARD
and THOR_SIDE_TO_MOVE to represent the position after those moves.
*/
static int
play_through_game( GameType *game, int max_moves ) {
int i;
int move;
int flipped;
clear_thor_board();
thor_side_to_move = BLACKSQ;
for ( i = 0; i < max_moves; i++ ) {
move = abs( game->moves[i] );
flipped = any_flips( move, thor_side_to_move, OPP( thor_side_to_move ) );
if ( flipped ) {
thor_board[move] = thor_side_to_move;
thor_side_to_move = OPP( thor_side_to_move );
}
else {
thor_side_to_move = OPP( thor_side_to_move );
flipped = any_flips( move, thor_side_to_move, OPP( thor_side_to_move ) );
if ( flipped ) {
thor_board[move] = thor_side_to_move;
thor_side_to_move = OPP( thor_side_to_move );
}
else
return FALSE;
}
}
return TRUE;
}
/*
PREPARE_GAME
Performs off-line analysis of GAME to speed up subsequent requests.
The main result is that the number of black discs on the board after
each of the moves is stored.
*/
static void
prepare_game( GameType *game ) {
int i;
int move;
int done;
int flipped;
int opening_match;
int moves_played;
int disc_count[3];
unsigned int corner_descriptor;
ThorOpeningNode *opening, *child;
/* Play through the game and count the number of black discs
at each stage. */
clear_thor_board();
disc_count[BLACKSQ] = disc_count[WHITESQ] = 2;
thor_side_to_move = BLACKSQ;
corner_descriptor = 0;
moves_played = 0;
done = FALSE;
do {
/* Store the number of black discs. */
game->black_disc_count[moves_played] = disc_count[BLACKSQ];
/* Make the move, update the board and disc count,
and change the sign for white moves */
move = game->moves[moves_played];
flipped = count_flips( move, thor_side_to_move, OPP( thor_side_to_move ) );
if ( flipped ) {
thor_board[move] = thor_side_to_move;
disc_count[thor_side_to_move] += flipped + 1;
disc_count[OPP( thor_side_to_move )] -= flipped;
if ( thor_side_to_move == WHITESQ )
game->moves[moves_played] = -game->moves[moves_played];
thor_side_to_move = OPP( thor_side_to_move );
moves_played++;
}
else {
thor_side_to_move = OPP( thor_side_to_move );
flipped = count_flips( move, thor_side_to_move,
OPP( thor_side_to_move ) );
if ( flipped ) {
thor_board[move] = thor_side_to_move;
disc_count[thor_side_to_move] += flipped + 1;
disc_count[OPP( thor_side_to_move )] -= flipped;
if ( thor_side_to_move == WHITESQ )
game->moves[moves_played] = -game->moves[moves_played];
thor_side_to_move = OPP( thor_side_to_move );
moves_played++;
}
else
done = TRUE;
}
/* Update the corner descriptor if necessary */
if ( (move == 11) || (move == 18) || (move == 81) || (move == 88) )
corner_descriptor |=
get_corner_mask( thor_board[11], thor_board[81],
thor_board[18], thor_board[88] );
} while ( !done && (moves_played < 60) );
game->black_disc_count[moves_played] = disc_count[BLACKSQ];
game->move_count = moves_played;
for ( i = moves_played + 1; i <= 60; i++ )
game->black_disc_count[i] = -1;
/* Find the longest opening which coincides with the game */
opening = root_node;
i = 0;
opening_match = TRUE;
while ( opening_match ) {
move = opening->child_move;
child = opening->child_node;
while ( (child != NULL) && (move != abs( game->moves[i] )) ) {
move = child->sibling_move;
child = child->sibling_node;
}
if ( child == NULL )
opening_match = FALSE;
else {
opening = child;
i++;
}
}
game->opening = opening;
/* Initialize the shape state */
game->shape_lo = 3 << 27;
game->shape_hi = 3 << 3;
game->shape_state_hi = 0;
game->shape_state_lo = 0;
/* Store the corner descriptor */
game->corner_descriptor = corner_descriptor;
}
/*
GET_INT_8
Reads an 8-bit signed integer from STREAM. Returns TRUE upon
success, FALSE otherwise.
*/
INLINE static int
get_int_8( FILE *stream, int_8 *value ) {
int actually_read;
actually_read = fread( value, sizeof( int_8 ), 1, stream );
return (actually_read == 1);
}
/*
GET_INT_16
Reads a 16-bit signed integer from STREAM. Returns TRUE upon
success, FALSE otherwise.
*/
INLINE static int
get_int_16( FILE *stream, int_16 *value ) {
int actually_read;
actually_read = fread( value, sizeof( int_16 ), 1, stream );
return (actually_read == 1);
}
/*
GET_INT_32
Reads a 32-bit signed integer from STREAM. Returns TRUE upon
success, FALSE otherwise.
*/
INLINE static int
get_int_32( FILE *stream, int_32 *value ) {
int actually_read;
actually_read = fread( value, sizeof( int_32 ), 1, stream );
return (actually_read == 1);
}
/*
TOURNAMENT_NAME
Returns the name of the INDEXth tournament if available.
*/
INLINE static const char *
tournament_name( int index ) {
if ( (index < 0) || (index >= tournaments.count) )
return "<" NA_ERROR ">";
else
return tournaments.name_buffer + TOURNAMENT_NAME_LENGTH * index;
}
/*
TOURNAMENT_LEX_ORDER
Returns the index into the lexicographical order of the
INDEXth tournament if available, otherwise the last
index + 1.
*/
INLINE static int
tournament_lex_order( int index ) {
if ( (index < 0) || (index >= tournaments.count) )
return tournaments.count;
else
return tournaments.tournament_list[index].lex_order;
}
/*
GET_PLAYER_NAME
Returns the name of the INDEXth player if available.
*/
const char *
get_player_name( int index ) {
if ( (index < 0) || (index >= players.count) )
return "< " NA_ERROR " >";
else
return players.name_buffer + PLAYER_NAME_LENGTH * index;
}
/*
GET_PLAYER_COUNT
Returns the number of players in the database.
*/
int get_player_count( void ) {
return players.count;
}
/*
PLAYER_LEX_ORDER
Returns the index into the lexicographical order of the
INDEXth player if available, otherwise the last index + 1.
*/
INLINE static int
player_lex_order( int index ) {
if ( (index < 0) || (index >= players.count) )
return players.count;
else
return players.player_list[index].lex_order;
}
/*
READ_PROLOG
Reads the prolog from STREAM into PROLOG. As the prolog is common
for all the three database types (game, player, tournament) also
values which aren't used are read.
Returns TRUE upon success, otherwise FALSE.
*/
static int
read_prolog( FILE *stream, PrologType *prolog ) {
int success;
int_8 byte_val;
int_16 word_val;
int_32 longint_val;
success = get_int_8( stream, &byte_val );
prolog->creation_century = byte_val;
success = success && get_int_8( stream, &byte_val );
prolog->creation_year = byte_val;
success = success && get_int_8( stream, &byte_val );
prolog->creation_month = byte_val;
success = success && get_int_8( stream, &byte_val );
prolog->creation_day = byte_val;
success = success && get_int_32( stream, &longint_val );
prolog->game_count = longint_val;
success = success && get_int_16( stream, &word_val );
prolog->item_count = word_val;
success = success && get_int_16( stream, &word_val );
prolog->origin_year = word_val;
success = success && get_int_32( stream, &longint_val );
prolog->reserved = longint_val;
return success;
}
/*
THOR_COMPARE_TOURNAMENTS
Lexicographically compares the names of two tournaments
represented by pointers.
*/
#ifdef _WIN32_WCE
static int CE_CDECL
#else
static int
#endif
thor_compare_tournaments( const void *t1, const void *t2 ) {
TournamentType *tournament1 = (TournamentType *) *((TournamentType **) t1);
TournamentType *tournament2 = (TournamentType *) *((TournamentType **) t2);
return strcmp( tournament1->name, tournament2->name );
}
/*
SORT_TOURNAMENT_DATABASE
Computes the lexicographic order of all tournaments in the database.
*/
static void
sort_tournament_database( void ) {
TournamentType **tournament_buffer;
int i;
tournament_buffer =
(TournamentType **)
safe_malloc( tournaments.count * sizeof( TournamentType * ) );
for ( i = 0; i < tournaments.count; i++ )
tournament_buffer[i] = &tournaments.tournament_list[i];
qsort( tournament_buffer, tournaments.count, sizeof( TournamentType * ),
thor_compare_tournaments );
for ( i = 0; i < tournaments.count; i++ )
tournament_buffer[i]->lex_order = i;
free( tournament_buffer );
}
/*
READ_TOURNAMENT_DATABASE
Reads the tournament database from FILE_NAME.
Returns TRUE if all went well, otherwise FALSE.
*/
int
read_tournament_database( const char *file_name ) {
FILE *stream;
int i;
int success;
int actually_read;
int buffer_size;
stream = fopen( file_name, "rb" );
if ( stream == NULL )
return FALSE;
if ( !read_prolog( stream, &tournaments.prolog ) ) {
fclose( stream );
return FALSE;
}
tournaments.count = tournaments.prolog.item_count;
buffer_size = TOURNAMENT_NAME_LENGTH * tournaments.prolog.item_count;
tournaments.name_buffer =
(char *) safe_realloc( tournaments.name_buffer, buffer_size );
actually_read = fread( tournaments.name_buffer, 1, buffer_size, stream );
success = (actually_read == buffer_size);
fclose( stream );
if ( success ) {
tournaments.tournament_list = (TournamentType *)
safe_realloc( tournaments.tournament_list,
tournaments.count * sizeof( TournamentType ) );
for ( i = 0; i < tournaments.count; i++ ) {
tournaments.tournament_list[i].name = tournament_name( i );
tournaments.tournament_list[i].selected = TRUE;
}
sort_tournament_database();
thor_games_sorted = FALSE;
thor_games_filtered = FALSE;
}
return success;
}
/*
GET_TOURNAMENT_NAME
Returns the name of the INDEXth tournament if available.
*/
const char *
get_tournament_name( int index ) {
if ( (index < 0) || (index >= tournaments.count) )
return "< " NA_ERROR " >";
else
return tournaments.name_buffer + TOURNAMENT_NAME_LENGTH * index;
}
/*
GET_TOURNAMENT_COUNT
Returns the number of players in the database.
*/
int get_tournament_count( void ) {
return tournaments.count;
}
/*
THOR_COMPARE_PLAYERS
Lexicographically compares the names of two players
represented by pointers.
*/
#ifdef _WIN32_WCE
static int CE_CDECL
#else
static int
#endif
thor_compare_players( const void *p1, const void *p2 ) {
char ch;
char buffer1[PLAYER_NAME_LENGTH];
char buffer2[PLAYER_NAME_LENGTH];
int i;
PlayerType *player1 = (PlayerType *) *((PlayerType **) p1);
PlayerType *player2 = (PlayerType *) *((PlayerType **) p2);
i = 0;
do {
ch = player1->name[i];
buffer1[i] = tolower( ch );
i++;
} while ( ch != 0 );
if ( buffer1[0] == '?' ) /* Put unknown players LAST */
buffer1[0] = '~';
i = 0;
do {
ch = player2->name[i];
buffer2[i] = tolower( ch );
i++;
} while ( ch != 0 );
if ( buffer2[0] == '?' ) /* Put unknown players LAST */
buffer2[0] = '~';
return strcmp( buffer1, buffer2 );
}
/*
SORT_PLAYER_DATABASE
Computes the lexicographic order of all players in the database.
*/
static void
sort_player_database( void ) {
PlayerType **player_buffer;
int i;
player_buffer =
(PlayerType **) safe_malloc( players.count * sizeof( PlayerType * ) );
for ( i = 0; i < players.count; i++ )
player_buffer[i] = &players.player_list[i];
qsort( player_buffer, players.count, sizeof( PlayerType * ),
thor_compare_players );
for ( i = 0; i < players.count; i++ )
player_buffer[i]->lex_order = i;
free( player_buffer );
}
/*
READ_PLAYER_DATABASE
Reads the player database from FILE_NAME.
Returns TRUE if all went well, otherwise FALSE.
*/
int
read_player_database( const char *file_name ) {
FILE *stream;
int i;
int success;
int actually_read;
int buffer_size;
stream = fopen( file_name, "rb" );
if ( stream == NULL )
return FALSE;
if ( !read_prolog( stream, &players.prolog ) ) {
fclose( stream );
return FALSE;
}
players.count = players.prolog.item_count;
buffer_size = PLAYER_NAME_LENGTH * players.count;
players.name_buffer =
(char *) safe_realloc( players.name_buffer, buffer_size );