Import upstream Source
[gamazons.git] / src / amazons.h
blob2eb3b703291deca821f7c2a7bf9b8b3ec8de2a04
1 #define ushort unsigned short
2 #define uchar unsigned char
3 #define ull unsigned long long
4 #define ulong unsigned long
6 #define TT 0x3d0925
7 #define MAX_TT_SEARCH 30
9 //#define TRUE 1
10 //#define FALSE 0
12 #ifndef FALSE
13 #define FALSE (0)
14 #endif
16 #ifndef TRUE
17 #define TRUE (!FALSE)
18 #endif
20 #define WHITE_PLAYER 1 //player 1
21 #define BLACK_PLAYER 2 //player 2
23 #define NOTHINK 0
24 #define THINK 1
26 #define GAMAZONS
27 #undef DEBUG
29 //#define DEBUG_HEVAL
30 //#define DEBUG_WEB
32 struct move_s
34 uchar queen;
35 uchar tocol, torow;
36 uchar wallcol, wallrow;
37 int val;
38 int depth;
40 typedef struct move_s move;
43 //Basic game state at one point in time
44 struct state_s
46 ull white_bd[2];
47 ull black_bd[2];
48 ull blocks_bd[2];
50 uchar white_q_x[4];
51 uchar white_q_y[4];
53 uchar black_q_x[4];
54 uchar black_q_y[4];
56 uchar turn;
57 short value;
58 uchar depth;
59 uchar winner;
62 //state stored in the transposition table
63 struct state_t
65 ull white_bd[2];
66 ull black_bd[2];
67 ull blocks_bd[2];
69 uchar turn;
70 uchar depth;
71 short value;
72 short alpha;
73 short beta;
76 typedef struct state_s state;
77 typedef struct state_t state_t;
78 state_t **tt;
80 //Collection of all game states
81 struct game_states
83 state *s[100];
84 int current_state;
85 int max_state;
89 //Global game options
90 struct engine
92 int timeout;
93 int maxdepth;
94 int maxwidth;
97 //paths to game pieces & squares
98 struct images
100 char white_piece[255];
101 char black_piece[255];
102 char white_sq[255];
103 char grey_sq[255];
104 char arrow_sq[255];
105 int grid;
108 struct options
110 int white_player;
111 int black_player;
112 int print_statistics;
113 int replay_delay;
114 int movement_speed;
115 char hist_dir[512];
117 struct engine engine;
118 struct images images;
122 // Different player choices
123 enum {
125 HUMAN,
128 /* intersting statistic globals */
129 int tt_overwrite;
130 int tt_stores;
132 int tt_updates;
133 int tt_lookups;
134 int tt_lookup_finds;
135 int heval_calls;
139 /* Prototypes */
140 int sean_heval(state *s);
141 move savemove(state *s, move m);
142 int mmselect(int oldval, int newval);
143 int test(state *s, uchar col, uchar row);
144 int xor(ull bd[2], uchar col, uchar row);
145 int pboard(state);
146 move search(state *s, int depth, int alpha, int beta, int, int);
147 move isearch(state *s, int);
148 move getmove(state *s, int player);
149 int pvec(ull v);
150 int moves(ushort q[][13], int w);
151 int minmoves(ushort q[][13], int w);
152 int maxmoves(ushort q[][13], int w);
153 int countmoves(ushort q[][13], int w);
154 int pbits(ushort q[][13], int s);
155 int children(state *s, move movelist[]);
156 int countchildren(state *s);
157 int undomove(state *s, move m);
158 char get_token();
159 void clear_buf();
160 int move_lookup(move *m, move movelist[], int move_count);
161 void dup_state(state *s_old, state *s_new);
162 #ifdef GAMAZONS
163 void init_engine();
164 void load_values_from_file();
165 void store_values_in_file();
166 int load_images_from_theme(char *theme);
167 #endif
170 int makemove(state *s, move m);
171 void print_usage_menu();
172 void parse_args(int argc, char *argv[]);
173 void print_stats();
175 /* eval.c */
176 int get_forward_diag(ull board_l, ull board_u, int diag);
177 int get_back_diag(ull board_l, ull board_u, int diag);
178 void put_forward_diag(ull *board_l, ull *board_u, ushort stream, int diag);
179 void put_back_diag(ull *board_l, ull *board_u, ushort stream, int diag);
180 int calc_stream_moves(ushort stream, ushort pos, ushort len);
181 int calc_moves(ull board_l, ull board_u, int pos);
182 int count_contig_bits(ushort stream, int len);
183 int heval(state *s);
184 ushort gen_web_stream(ushort stream, int pos, int len);
185 ushort gen_web_stream_plus(ushort stream, int pos, int len);
186 void gen_web_board(ull *web_l, ull *web_u, ull board_l, ull board_u, int pos);
187 int gen_web_board_count(ull *web_l, ull *web_u, ull board_l, ull board_u, int pos);
188 void gen_dirs_board(ull *board_l, ull *board_u, int pos);
189 int count_bits(ull board_l, ull board_u);
193 int pbvec(ull l, ull u);
195 state_t *tt_lookup(state *s);
196 void tt_store(state *s, short alpha, short beta);
197 void tt_update(state *s, short alpha, short beta);
199 int tt_compare(state *s, state_t *t);
204 //convert x,y coords to pos 0-99
205 #define XY_TO_POS(x,y) y*10 + x
207 /* Bit board stuff */
208 #define GET_ROW(board, row) (board >> (row%5) * 10) & 0x3ff
209 #define GET_COL(board_l, board_u, col) (GET_HALF_COL(board_l, col) | (GET_HALF_COL(board_u, col) << 5))
211 #define GET_HALF_COL(board, col) (((board >> col) &0x1) | ((board >> (col + 9)) &0x2) | ((board >> (col + 18)) &0x4) | ((board >> (col + 27)) &0x8) | ((board >> (col + 36)) &0x10))
213 /* These defines require a board number between 0-99 */
214 //Tells you where in a stream of 10 bits the position (x,y) would be (0-9)
215 #define GET_COL_POS(y) (int) y / 10
216 #define GET_ROW_POS(x) x % 10
217 //Tells you where in a stream of up to 10 bits the position (f,b) would be (0-9)
218 #define GET_FDIAG_POS(f) (f%10 > f/10) ? f/10 : f%10
219 #define GET_BDIAG_POS(b) (b/10 < (10 - b%10)) ? b/10 : 9 - b%10
221 //Gets diag numbers, for passing into get_forw_diag() & get_back_diag()
222 #define GET_FDIAG(f) (f%10 > f/10) ? f - ((f/10) * 11) : f - ((f%10) * 11)
223 #define GET_BDIAG(b) (b/10 < (10 - b%10)) ? b - ((b/10) * 9) : b - ((9 - (b%10)) * 9)
225 //Calculates length of a diagonal, pass in value from GET_FDIAG/GET_BDIAG
226 //Note: Don't call GET_[F,B]DIAG within GET_[F,B]DIAG_LEN, you get really weird results
227 //eg GET_FDIAG_LEN(GET_FDIAG(pos)). Instead, store it into a variable first:
228 //eg diag = GET_FDIAG(pos); len = GET_FDIAG_LEN(diag);
229 #define GET_FDIAG_LEN(fdiag) (fdiag < 10) ? (10 - fdiag) : (10 - fdiag/10)
230 #define GET_BDIAG_LEN(bdiag) (bdiag < 10) ? bdiag + 1 : (10 - bdiag/10)
232 #define PUT_ROW(board, row, stream) board |= ((ull) stream << ((row % 5) * 10))
233 #define PUT_COL(board_l, board_u, col, stream) PUT_HALF_COL(board_l, col, stream); PUT_HALF_COL(board_u, col, stream >> 5)
234 #define PUT_HALF_COL(board, col, stream) board |= ((((ull)stream & 0x1) << col) | \
235 (((ull)stream & 0x2) << (col + 9)) | \
236 (((ull)stream & 0x4) << (col + 18)) | \
237 (((ull)stream & 0x8) << (col + 27)) | \
238 (((ull)stream & 0x10) << (col + 36)))