5941400180e289e9e3b99bc0be7b55f6a0d5719c
[bonanza.git] / data.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "shogi.h"
4
5 FILE *pf_book;
6 uint64_t ehash_tbl[ EHASH_MASK + 1 ];
7 trans_table_t *ptrans_table_orig;
8 SHARE trans_table_t *ptrans_table;
9 record_t record_problems;
10 record_t record_game;
11 rand_work_t rand_work;
12 root_move_t root_move_list[ MAX_LEGAL_MOVES ];
13 pv_t last_pv;
14 pv_t alast_pv_save[NUM_UNMAKE];
15 slide_tbl_t aslide[ nsquare ];
16 bitboard_t abb_b_knight_attacks[ nsquare ];
17 bitboard_t abb_b_silver_attacks[ nsquare ];
18 bitboard_t abb_b_gold_attacks[ nsquare ];
19 bitboard_t abb_w_knight_attacks[ nsquare ];
20 bitboard_t abb_w_silver_attacks[ nsquare ];
21 bitboard_t abb_w_gold_attacks[ nsquare ];
22 bitboard_t abb_king_attacks[ nsquare ];
23 bitboard_t abb_bishop_attacks_rl45[ nsquare ][ 128 ];
24 bitboard_t abb_bishop_attacks_rr45[ nsquare ][ 128 ];
25 bitboard_t abb_rank_attacks[ nsquare ][ 128 ];
26 bitboard_t abb_file_attacks[ nsquare ][ 128 ];
27 bitboard_t abb_obstacle[ nsquare ][ nsquare ];
28 bitboard_t abb_mask[ nsquare ];
29 bitboard_t abb_mask_rl90[ nsquare ];
30 bitboard_t abb_mask_rl45[ nsquare ];
31 bitboard_t abb_mask_rr45[ nsquare ];
32 bitboard_t abb_plus_rays[ nsquare ];
33 bitboard_t abb_minus_rays[ nsquare ];
34 uint64_t b_pawn_rand[ nsquare ];
35 uint64_t b_lance_rand[ nsquare ];
36 uint64_t b_knight_rand[ nsquare ];
37 uint64_t b_silver_rand[ nsquare ];
38 uint64_t b_gold_rand[ nsquare ];
39 uint64_t b_bishop_rand[ nsquare ];
40 uint64_t b_rook_rand[ nsquare ];
41 uint64_t b_king_rand[ nsquare ];
42 uint64_t b_pro_pawn_rand[ nsquare ];
43 uint64_t b_pro_lance_rand[ nsquare ];
44 uint64_t b_pro_knight_rand[ nsquare ];
45 uint64_t b_pro_silver_rand[ nsquare ];
46 uint64_t b_horse_rand[ nsquare ];
47 uint64_t b_dragon_rand[ nsquare ];
48 uint64_t b_hand_pawn_rand[ npawn_max ];
49 uint64_t b_hand_lance_rand[ nlance_max ];
50 uint64_t b_hand_knight_rand[ nknight_max ];
51 uint64_t b_hand_silver_rand[ nsilver_max ];
52 uint64_t b_hand_gold_rand[ ngold_max ];
53 uint64_t b_hand_bishop_rand[ nbishop_max ];
54 uint64_t b_hand_rook_rand[ nrook_max ];
55 uint64_t w_pawn_rand[ nsquare ];
56 uint64_t w_lance_rand[ nsquare ];
57 uint64_t w_knight_rand[ nsquare ];
58 uint64_t w_silver_rand[ nsquare ];
59 uint64_t w_gold_rand[ nsquare ];
60 uint64_t w_bishop_rand[ nsquare ];
61 uint64_t w_rook_rand[ nsquare ];
62 uint64_t w_king_rand[ nsquare ];
63 uint64_t w_pro_pawn_rand[ nsquare ];
64 uint64_t w_pro_lance_rand[ nsquare ];
65 uint64_t w_pro_knight_rand[ nsquare ];
66 uint64_t w_pro_silver_rand[ nsquare ];
67 uint64_t w_horse_rand[ nsquare ];
68 uint64_t w_dragon_rand[ nsquare ];
69 uint64_t w_hand_pawn_rand[ npawn_max ];
70 uint64_t w_hand_lance_rand[ nlance_max ];
71 uint64_t w_hand_knight_rand[ nknight_max ];
72 uint64_t w_hand_silver_rand[ nsilver_max ];
73 uint64_t w_hand_gold_rand[ ngold_max ];
74 uint64_t w_hand_bishop_rand[ nbishop_max ];
75 uint64_t w_hand_rook_rand[ nrook_max ];
76 uint64_t node_limit;
77 SHARE unsigned int game_status;
78 unsigned int amove_save[NUM_UNMAKE];
79 unsigned int move_evasion_pchk;
80 unsigned int node_per_second;
81 unsigned int node_next_signal;
82 unsigned int node_last_check;
83 unsigned int hash_mask;
84 unsigned int sec_elapsed;
85 unsigned int sec_b_total;
86 unsigned int sec_w_total;
87 unsigned int sec_limit;
88 unsigned int sec_limit_up;
89 unsigned int sec_limit_depth;
90 unsigned int time_last_result;
91 unsigned int time_last_check;
92 unsigned int time_start;
93 unsigned int time_turn_start;
94 unsigned int time_limit;
95 unsigned int time_max_limit;
96 unsigned int time_last_search;
97 unsigned int time_response;
98 unsigned int ponder_move;
99 int easy_abs;
100 int easy_min;
101 int easy_max;
102 int easy_value;
103 SHARE int fmg_misc;
104 SHARE int fmg_cap;
105 SHARE int fmg_drop;
106 SHARE int fmg_mt;
107 SHARE int fmg_misc_king;
108 SHARE int fmg_cap_king;
109 unsigned int ponder_move_list[ MAX_LEGAL_MOVES ];
110 int ponder_nmove;
111 SHARE int root_abort;
112 int root_nmove;
113 int root_alpha;
114 int root_beta;
115 int root_value;
116 int root_turn;
117 int root_index;
118 int root_nfail_high;
119 int root_nfail_low;
120 int trans_table_age;
121 int log2_ntrans_table;
122 int last_root_value;
123 int iteration_depth;
124 int depth_limit;
125 int record_num;
126 int npawn_box;
127 int nlance_box;
128 int nknight_box;
129 int nsilver_box;
130 int ngold_box;
131 int nbishop_box;
132 int nrook_box;
133 int resign_threshold;
134 int amaterial_save[NUM_UNMAKE];
135 int alast_root_value_save[NUM_UNMAKE];
136 int p_value_ex[31];
137 int p_value_pm[15];
138 int p_value[31];
139 short pc_on_sq[nsquare][pos_n];
140 short kkp[nsquare][nsquare][kkp_end];
141 unsigned char ansuc_check_save[NUM_UNMAKE];
142 unsigned char book_section[ MAX_SIZE_SECTION+1 ];
143 unsigned char adirec[ nsquare ][ nsquare ];
144 unsigned char is_same[ 16 ][ 16 ];
145 char str_cmdline[ SIZE_CMDLINE ];
146 char str_message[ SIZE_MESSAGE ];
147 char str_buffer_cmdline[ SIZE_CMDBUFFER ];
148 const char *str_error;
149
150 #if defined(MPV)
151 int root_mpv;
152 int mpv_num;
153 int mpv_width;
154 pv_t mpv_pv[ MPV_MAX_PV*2 + 1 ];
155 #endif
156
157 #if defined(DFPN)
158 unsigned int dfpn_hash_log2;
159 sckt_t dfpn_sckt;
160 #endif
161
162 #  if ! defined(_WIN32) && ( defined(DFPN_CLIENT) || defined(TLP) )
163 pthread_attr_t pthread_attr;
164 #  endif
165
166 #if defined(TLP) || defined(DFPN_CLIENT)
167 lock_t io_lock;
168 #endif
169
170 #if defined(DFPN_CLIENT)
171 volatile sckt_t dfpn_client_sckt;
172 volatile int dfpn_client_flag_read;
173 volatile unsigned int dfpn_client_move_unlocked;
174 volatile int dfpn_client_rresult_unlocked;
175 volatile int dfpn_client_rresult;
176 volatile int dfpn_client_num_cresult;
177 volatile char dfpn_client_signature[ DFPN_CLIENT_SIZE_SIGNATURE ];
178 volatile dfpn_client_cresult_t dfpn_client_cresult[ MAX_LEGAL_MOVES ];
179 volatile char dfpn_client_str_move[7];
180 unsigned int dfpn_client_best_move;
181 lock_t dfpn_client_lock;
182 char dfpn_client_str_addr[256];
183 int dfpn_client_port;
184 int dfpn_client_cresult_index;
185 #endif
186
187 #if defined(TLP)
188 lock_t tlp_lock;
189 tree_t tlp_atree_work[ TLP_NUM_WORK ];
190 tree_t * volatile tlp_ptrees[ TLP_MAX_THREADS ];
191 volatile int tlp_abort;
192 volatile int tlp_idle;
193 volatile int tlp_num;
194 int tlp_max;
195 int tlp_nsplit;
196 int tlp_nabort;
197 int tlp_nslot;
198 #else
199 tree_t tree;
200 #endif
201
202 #if ! defined(_WIN32)
203 clock_t clk_tck;
204 #endif
205
206 #if ! defined(NO_LOGGING)
207 FILE *pf_log;
208 const char *str_dir_logs = "log";
209 #endif
210
211 #if defined(CSA_LAN)||defined(MNJ_LAN)|| defined(DFPN_CLIENT)|| defined(DFPN)
212 unsigned int time_last_send;
213 #endif
214
215 #if defined(CSA_LAN)
216 int client_turn;
217 int client_ngame;
218 int client_max_game;
219 long client_port;
220 char client_str_addr[256];
221 char client_str_id[256];
222 char client_str_pwd[256];
223 sckt_t sckt_csa;
224 #endif
225
226 #if defined(MNJ_LAN) || defined(USI)
227 unsigned int moves_ignore[MAX_LEGAL_MOVES];
228 #endif
229
230 #if defined(MNJ_LAN)
231 sckt_t sckt_mnj;
232 int mnj_posi_id;
233 int mnj_depth_stable;
234 unsigned int mnj_moves_ignore[MAX_LEGAL_MOVES];
235 #endif
236
237 #if defined(USI)
238 enum usi_mode usi_mode;
239 unsigned int usi_time_out_last;
240 unsigned int usi_byoyomi;
241 #endif
242
243 check_table_t b_chk_tbl[nsquare];
244 check_table_t w_chk_tbl[nsquare];
245
246 #if defined(_MSC_VER)
247 #elif defined(__GNUC__) && ( defined(__i386__) || defined(__x86_64__) )
248 #else
249 unsigned char aifirst_one[512];
250 unsigned char ailast_one[512];
251 #endif
252
253 #if defined(NDEBUG)
254 #  if ! defined(CSASHOGI)
255 const char *str_myname = ( "Bonanza " BNZ_VER );
256 #  else
257 const char *str_myname = ( "Bonanza " BNZ_VER );
258 #  endif
259 #else
260 const char *str_myname = ( "Bonanza " BNZ_VER " Debug Build ("
261                            __TIME__ " " __DATE__ ")" );
262 #endif
263
264 #if defined(DBG_EASY)
265 unsigned int easy_move;
266 #endif
267
268 #if defined(INANIWA_SHIFT)
269 int inaniwa_flag;
270 #endif
271
272 const char *str_resign        = "%TORYO";
273 const char *str_repetition    = "%SENNICHITE";
274 const char *str_jishogi       = "%JISHOGI";
275 const char *str_record_error  = "%ERROR";
276 const char *str_delimiters    = " \t,";
277 const char *str_fmt_line      = "Line %u: %s";
278 const char *str_on            = "on";
279 const char *str_off           = "off";
280 const char *str_book          = "book.bin";
281 const char *str_fv            = "fv.bin";
282 const char *str_book_error    = "invalid opening book";
283 const char *str_io_error      = "I/O error";
284 const char *str_perpet_check  = "perpetual check";
285 const char *str_bad_cmdline   = "invalid command line";
286 const char *str_busy_think    = "I'm busy in thinking now";
287 const char *str_bad_record    = "invalid record of game";
288 const char *str_bad_board     = "invalid board representation";
289 const char *str_illegal_move  = "illegal move";
290 const char *str_double_pawn   = "double pawn";
291 const char *str_mate_drppawn  = "mated by a droped pawn";
292 const char *str_unexpect_eof  = "unexpected end of file";
293 const char *str_king_hang     = "The king is hang.";
294 const char *str_game_ended    = "move after a game was concluded";
295 const char *str_fopen_error   = "Can't open a file";
296 const char *str_ovrflw_line   = "Too many characters in a line.";
297 const char *str_no_legal_move = "No legal moves to search";
298 const char *str_warning       = "WARNING: ";
299 #if defined(CSA_LAN)
300 const char *str_server_err   = "received invalid message from the server";
301 #endif
302
303 const char *astr_table_piece[16]  = { "* ", "FU", "KY", "KE", "GI", "KI",
304                                       "KA", "HI", "OU", "TO", "NY", "NK",
305                                       "NG", "##", "UM", "RY" };
306
307 const char ach_turn[2] = { '+', '-' };
308
309 const int ashell_h[ SHELL_H_LEN ] = { 1, 3, 7, 15, 31, 63, 127 };
310
311 const int aikpp[31] = { e_dragon, e_horse,  0,        e_gold,
312                         e_gold,   e_gold,   e_gold,   0,
313                         e_rook,   e_bishop, e_gold,   e_silver,
314                         e_knight, e_lance,  e_pawn,   0,
315                         f_pawn,   f_lance,  f_knight,
316                         f_silver, f_gold,   f_bishop, f_rook,
317                         0,        f_gold,   f_gold,   f_gold,
318                         f_gold,   0,        f_horse,  f_dragon };
319
320 const int aikkp[16] = { 0,          kkp_pawn, kkp_lance,  kkp_knight,
321                         kkp_silver, kkp_gold, kkp_bishop, kkp_rook,
322                         0,          kkp_gold, kkp_gold,   kkp_gold,
323                         kkp_gold,   kkp_gold, kkp_horse,  kkp_dragon };
324
325 const int aikkp_hand[8] = { 0,               kkp_hand_pawn,   kkp_hand_lance,
326                             kkp_hand_knight, kkp_hand_silver, kkp_hand_gold,
327                             kkp_hand_bishop, kkp_hand_rook };
328
329 const unsigned char aifile[ nsquare ]= {
330   file1, file2, file3, file4, file5, file6, file7, file8, file9,
331   file1, file2, file3, file4, file5, file6, file7, file8, file9,
332   file1, file2, file3, file4, file5, file6, file7, file8, file9,
333   file1, file2, file3, file4, file5, file6, file7, file8, file9,
334   file1, file2, file3, file4, file5, file6, file7, file8, file9,
335   file1, file2, file3, file4, file5, file6, file7, file8, file9,
336   file1, file2, file3, file4, file5, file6, file7, file8, file9,
337   file1, file2, file3, file4, file5, file6, file7, file8, file9,
338   file1, file2, file3, file4, file5, file6, file7, file8, file9 };
339
340 const unsigned char airank[ nsquare ]= {
341   rank1, rank1, rank1, rank1, rank1, rank1, rank1, rank1, rank1,
342   rank2, rank2, rank2, rank2, rank2, rank2, rank2, rank2, rank2,
343   rank3, rank3, rank3, rank3, rank3, rank3, rank3, rank3, rank3,
344   rank4, rank4, rank4, rank4, rank4, rank4, rank4, rank4, rank4,
345   rank5, rank5, rank5, rank5, rank5, rank5, rank5, rank5, rank5,
346   rank6, rank6, rank6, rank6, rank6, rank6, rank6, rank6, rank6,
347   rank7, rank7, rank7, rank7, rank7, rank7, rank7, rank7, rank7,
348   rank8, rank8, rank8, rank8, rank8, rank8, rank8, rank8, rank8,
349   rank9, rank9, rank9, rank9, rank9, rank9, rank9, rank9, rank9 };
350
351 const min_posi_t min_posi_no_handicap = {
352   0,0,0,
353   { -lance, -knight, -silver, -gold, -king, -gold, -silver, -knight, -lance,
354     empty, -rook, empty, empty, empty, empty, empty, -bishop, empty,
355     -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn,
356     empty, empty, empty, empty, empty, empty, empty, empty, empty,
357     empty, empty, empty, empty, empty, empty, empty, empty, empty,
358     empty, empty, empty, empty, empty, empty, empty, empty, empty,
359     pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
360     empty, bishop, empty, empty, empty, empty, empty, rook, empty,
361     lance, knight, silver, gold, king, gold, silver, knight, lance } };