-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdefs.h
503 lines (458 loc) · 13.7 KB
/
defs.h
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
//===========================================================//
//
// Maverick Chess Engine
// Copyright 2013 Steve Maughan
//
//===========================================================//
//===========================================================//
// Define Version
//===========================================================//
#define ENGINE_NAME "Maverick"
#define VERSION_NUMBER "0.51"
#ifdef _DEBUG
#define ENGINE_VERSION "Debug"
#else
#ifdef MINGW64
#ifdef POPCOUNT
#define ENGINE_VERSION VERSION_NUMBER " x64"
#else
#define ENGINE_VERSION VERSION_NUMBER " x64 (No Popcount)"
#endif
#else
#define ENGINE_VERSION VERSION_NUMBER " x32"
#endif
#endif
#define ENGINE_AUTHOR "Steve Maughan"
#include <cassert>
//===========================================================//
// Primitive Logic
//===========================================================///
typedef int BOOL;
#define TRUE 1
#define FALSE 0
//===========================================================//
// Basic Chess Constants
//===========================================================//
#define CHESS_INFINITY 99999999
#define CHECKMATE 10000000
#define MAXPLY 127
#define MAX_MOVES 1024
#define MAX_CHECKMATE (CHECKMATE - 2 * MAXPLY)
#define WHITE 0
#define BLACK 1
#define BLANK 0
#define KNIGHT 1
#define BISHOP 2
#define ROOK 3
#define QUEEN 4
#define PAWN 5
#define KING 6
#define WHITEKNIGHT 1
#define WHITEBISHOP 2
#define WHITEROOK 3
#define WHITEQUEEN 4
#define WHITEPAWN 5
#define WHITEKING 6
#define BLACKKNIGHT 9
#define BLACKBISHOP 10
#define BLACKROOK 11
#define BLACKQUEEN 12
#define BLACKPAWN 13
#define BLACKKING 14
#define WHITE_CASTLE_OO 1
#define WHITE_CASTLE_OOO 2
#define BLACK_CASTLE_OO 4
#define BLACK_CASTLE_OOO 8
#define MIDDLEGAME 0
#define ENDGAME 1
//===========================================================//
// Ranks
//===========================================================//
#define FIRST_RANK 0
#define SECOND_RANK 1
#define THIRD_RANK 2
#define FOURTH_RANK 3
#define FIFTH_RANK 4
#define SIXTH_RANK 5
#define SEVENTH_RANK 6
#define EIGHTH_RANK 7
//===========================================================//
// Types of Moves
//===========================================================//
typedef enum movetypes {
MOVE_CASTLE,
MOVE_PAWN_PUSH1,
MOVE_PAWN_PUSH2,
MOVE_PxPAWN,
MOVE_PxPIECE,
MOVE_PxP_EP,
MOVE_PROMOTION,
MOVE_CAPTUREPROMOTE,
MOVE_PIECE_MOVE,
MOVE_PIECExPIECE,
MOVE_PIECExPAWN,
MOVE_KING_MOVE,
MOVE_KINGxPIECE,
MOVE_KINGxPAWN
} t_chess_move_type;
#define GLOBAL_MOVE_COUNT 43764
//===========================================================//
// Bitboards
//===========================================================//
typedef unsigned long long t_bitboard;
typedef long long unsigned int t_hash;
typedef long long unsigned int t_nodes;
typedef unsigned char t_chess_piece;
typedef unsigned char t_chess_square;
typedef unsigned char t_chess_color;
typedef unsigned char uchar;
typedef long long unsigned int t_magic;
typedef unsigned int t_piece_mask;
typedef int t_chess_value;
typedef long long int t_history_value;
typedef signed long t_chess_time;
//===========================================================//
// UCI Engine States
//===========================================================//
typedef enum enginestates {
UCI_ENGINE_WAITING,
UCI_ENGINE_THINKING,
UCI_ENGINE_START_THINKING
} t_uci_engine_state;
//===========================================================//
// UCI Constants
//===========================================================//
#define UCI_BUFFER_SIZE 4096
//===========================================================//
// General Macros
//===========================================================//
#define OPPONENT(x) ((x) ^ (t_chess_color)1)
#define COLUMN(s) ((s) & (uchar)7)
#define RANK(s) ((s) >> 3)
#define SQUARE64(s) ((t_bitboard)1 << (s))
#define COLOR(x) ((x) >> 3)
#define PIECETYPE(x) ((x) & (uchar)7)
#define PIECEINDEX(color, piece) (((int)(color) << 3) | piece)
#define x88_SQUARE(x) ((16 * RANK(x)) + COLUMN(x))
#define x88_ONBOARD(x) (((x) & 136) == 0)
#define x88_TO_64(x) ((8 * ((x) >> 4)) + COLUMN(x))
#define FLIP64(x) ((7 - RANK(x)) * 8 + COLUMN(x))
#define FLIPPIECECOLOR(x) PIECEINDEX(OPPONENT(COLOR(x)), PIECETYPE(x))
#define PIECEMASK(x) ((t_piece_mask)1 << (x))
//===========================================================//
// Global Move List Record Structure
//===========================================================//
struct t_move_record
{
t_chess_move_type move_type;
t_chess_piece piece;
t_chess_piece captured;
t_chess_square from_square;
t_chess_square to_square;
t_bitboard from_to_bitboard;
t_bitboard capture_mask;
t_chess_piece promote_to;
t_hash hash_delta;
t_hash pawn_hash_delta;
uchar castling_delta;
int index;
t_chess_value history;
int mvvlva;
};
struct t_move_list
{
int count; // Number of moves (this doesn't change)
int imove; // This starts at "count" and is decremented as the moves are played
struct t_move_record *current_move; // The move last played
struct t_move_record *hash_move; // The hash move
t_bitboard pinned_pieces; // A bitboard which stores the position of pinned pieces
struct t_move_record *move[256]; // The moves!
signed long long value[256]; // Notional values for all of the moves
};
struct t_undo
{
struct t_move_record *move;
uchar castling;
t_bitboard ep_square;
t_chess_square attacker;
uchar fifty_move_count;
uchar in_check;
t_hash hash;
t_hash pawn_hash;
t_hash material_hash;
};
struct t_castle_record
{
t_bitboard possible; // the squares which must be empty for castling to be possible
t_bitboard not_attacked; // the squares which must not be attacked
t_chess_square king_from; // position of the king before castling (relevant for Chess960)
t_chess_square rook_from; // position of the rook before castling (relevant for Chess960)
t_chess_square rook_to; // position of the rook after castling
t_bitboard rook_from_to; // from and to bitboard of rook
t_chess_piece rook_piece; // white or black rook
};
//===========================================================//
// Evaluation Structure
//===========================================================//
struct t_chess_eval
{
struct t_pawn_hash_record *pawn_evaluation;
int game_phase;
t_chess_value middlegame;
t_chess_value endgame;
t_chess_value static_score;
t_bitboard attacklist[15];
t_bitboard *attacks[2];
};
//===========================================================//
// Hash Records
//===========================================================//
#define HASH_ATTEMPTS 4
typedef enum hash_bound {
HASH_LOWER,
HASH_EXACT,
HASH_UPPER
} t_hash_bound;
struct t_hash_record
{
t_hash key;
t_hash_bound bound;
int depth;
int age;
t_chess_value score;
struct t_move_record *move;
};
struct t_pawn_hash_record
{
t_hash key;
t_chess_value middlegame;
t_chess_value endgame;
t_bitboard forward_squares[2];
t_bitboard backward_squares[2];
t_bitboard attacks[2];
t_bitboard passed[2];
t_bitboard candidate_passed[2];
t_bitboard double_pawns[2];
t_bitboard backward[2];
t_bitboard isolated[2];
t_bitboard blocked[2];
t_bitboard weak[2];
t_bitboard open_file;
t_bitboard semi_open_file[2];
int pawn_count[2];
};
struct t_material_hash_record
{
t_hash key;
void (*eval_endgame)(struct t_board *board, struct t_chess_eval *eval);
};
//===========================================================//
// PV Data Structures
//===========================================================//
struct t_perft_pv_data
{
uchar index;
char fen[100];
struct t_move_record *move;
struct t_move_list move_list[1];
};
struct t_pv_data
{
struct t_chess_eval eval[1];
struct t_move_record *current_move;
struct t_move_record *killer1;
struct t_move_record *killer2;
struct t_move_record *check_killer1;
struct t_move_record *check_killer2;
int legal_moves_played;
int best_line_length;
struct t_move_record *best_line[MAXPLY + 1];
};
//===========================================================//
// Chess Board Structure
//===========================================================//
struct t_board
{
t_bitboard piecelist[15];
t_bitboard *pieces[2];
t_bitboard all_pieces;
t_bitboard occupied[2];
t_chess_color to_move;
t_hash hash;
t_hash pawn_hash;
t_hash material_hash;
BOOL chess960;
uchar castling;
t_bitboard ep_square;
uchar king_square[2];
uchar in_check;
t_chess_square check_attacker;
t_chess_square square[64];
uchar fifty_move_count;
struct t_pv_data pv_data[MAXPLY + 2];
};
//===========================================================//
// Search Constants
//===========================================================//
#define NULL_REDUCTION 3
//===========================================================//
// UCI Interface
//===========================================================//
struct t_level
{
int ponder;
int infinite;
t_chess_time time[2];
t_chess_time tinc[2];
t_nodes nodes;
int depth;
int movestogo;
t_chess_time movetime;
int mate;
};
struct t_uci_options
{
int hash_table_size;
int pawn_hash_table_size;
BOOL current_line;
BOOL show_search_statistics;
BOOL eval_test;
BOOL smart_book;
};
struct t_uci_thinking
{
t_hash predicted_hash;
unsigned long predicted_time;
BOOL predicted_hash_hit;
BOOL fail_high_last_ply;
};
struct t_uci_opening_book
{
BOOL use_own_book;
char filename[FILENAME_MAX];
FILE *f;
int book_size;
};
struct t_uci
{
t_uci_engine_state engine_state;
BOOL stop;
BOOL quit;
struct t_uci_opening_book opening_book;
struct t_level level;
struct t_uci_options options;
struct t_uci_thinking thinking;
BOOL debug;
BOOL engine_initialized;
};
struct t_book_move {
t_hash key;
int move;
int weight;
int n;
int learn;
};
//===========================================================//
// "Magics"
//===========================================================//
struct t_magic_structure
{
t_bitboard mask;
t_magic magic;
};
//===========================================================//
// Move Ordering
//===========================================================//
#define MAX_CHESS_INT 2147483647
#define MOVE_ORDER_HASH (MAX_CHESS_INT >> 1)
#define MOVE_ORDER_CAPTURE (MAX_CHESS_INT >> 2)
#define MOVE_ORDER_KILLER1 (MAX_CHESS_INT >> 3)
#define MOVE_ORDER_KILLER2 (MAX_CHESS_INT >> 4)
#define MOVE_ORDER_KILLER3 (MAX_CHESS_INT >> 6)
#define MOVE_ORDER_KILLER4 (MAX_CHESS_INT >> 7)
#define MOVE_ORDER_ETC (MAX_CHESS_INT >> 5)
//===========================================================//
// Multi-PV
//===========================================================//
struct t_pv_record{
t_chess_value score;
int pv_length;
struct t_move_record *move[MAXPLY];
};
struct t_multi_pv {
int count;
struct t_pv_record pv[128];
};
//===========================================================//
// Squares
//===========================================================//
#define A1 0
#define B1 1
#define C1 2
#define D1 3
#define E1 4
#define F1 5
#define G1 6
#define H1 7
#define A2 8
#define B2 9
#define C2 10
#define D2 11
#define E2 12
#define F2 13
#define G2 14
#define H2 15
#define A3 16
#define B3 17
#define C3 18
#define D3 19
#define E3 20
#define F3 21
#define G3 22
#define H3 23
#define A4 24
#define B4 25
#define C4 26
#define D4 27
#define E4 28
#define F4 29
#define G4 30
#define H4 31
#define A5 32
#define B5 33
#define C5 34
#define D5 35
#define E5 36
#define F5 37
#define G5 38
#define H5 39
#define A6 40
#define B6 41
#define C6 42
#define D6 43
#define E6 44
#define F6 45
#define G6 46
#define H6 47
#define A7 48
#define B7 49
#define C7 50
#define D7 51
#define E7 52
#define F7 53
#define G7 54
#define H7 55
#define A8 56
#define B8 57
#define C8 58
#define D8 59
#define E8 60
#define F8 61
#define G8 62
#define H8 63
#define B8H1 0xfefefefefefefefe
#define A8G1 0x7f7f7f7f7f7f7f7f
#define BITBOARDS_CENTER 0x00003C3C3C3C0000
#define BITBOARDS_RING 0x007E424242427E00
#define BITBOARDS_EDGE 0xff818181818181FF
#define INDEX_CHECK(index, array) assert((index) >= 0 && (index) < sizeof array / sizeof array[0]);