per test
[rattatechess.git] / engine.h
blob06fdd6eef53e591752898fa60cfeb2200a4d4236
1 /***************************************************************************
2 engine.h - description
3 -------------------
4 begin : mer ott 23 2002
5 copyright : (C) 2002-2005 by Maurizio Monge
6 email : monge@linuz.sns.it
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #ifndef __ENGINE_H__
19 #define __ENGINE_H__
21 #include <string.h>
23 #define PLAYING 1
24 #define _10 2
25 #define _01 3
26 #define _12 4
28 //#define PLAYING 1
29 #define ANALYZING 2
30 #define FORCING 3
32 #define TIME_LIMIT 1
33 #define DEPTH_LIMIT 2
34 #define NODES_LIMIT 3
37 #include <map>
38 #include <string>
39 #include <setjmp.h>
40 #include "board.h"
42 class SearchGui;
43 class SearchRoot;
45 class Engine
47 public:
48 class Cmd;
49 class Variable;
51 typedef std::map<std::string, Cmd> CmdSet;
52 typedef std::map<std::string, Variable> VariableSet;
54 Board board;
56 SearchGui* search_gui;
58 SearchRoot* current_root;
60 Move* mv_done;
61 int mv_done_num;
63 SaveBuf *save_buf;
64 int save_buf_num;
66 /* the max depth of the engine */
67 int st_depth;
68 int st_nodes;
70 /* the color of the engine */
71 uint8_t st_computer_color;
73 /* the time the engine will use to think */
74 int time_best_csec;
76 /* if there is a fixed time for a move, or else 0 */
77 int time_fixed;
79 /* time move per section, time per section and inc... */
80 int time_mps;
81 int time_base;
82 int time_inc;
84 /* clok and other clock in sec/100 */
85 int time_clock_csec;
86 int time_oth_clock_csec;
88 /* flags for options of the engine */
89 int analysis_limit;
91 bool ponder;
92 bool post;
93 //uint8_t eng_color;
94 uint64_t processed_nodes;
95 int max_think_time;
97 /* flag for output behaviour */
98 bool io_xboard;
99 bool io_san;
100 bool io_colors;
102 /* engine status */
103 int status;
104 int eng_status;
105 const char *status_string;
106 bool thinking;
107 int start_think_time;
108 uint8_t analysis_color;
110 FILE *log;
111 CmdSet commands;
112 VariableSet variables;
113 jmp_buf back;
115 HashEntry *hash_table;
116 void *book_mmap;
117 uint32_t book_size;
119 typedef std::map<HashKey, std::map<Move, int> > Lines;
120 Lines lines;
122 class OpeningInfo {
123 public:
124 const char *eco;
125 const char *name;
126 void set(const char* e, const char* n) {
127 if(eco) free((void*)eco); if(name) free((void*)name);
128 eco = e ? strdup(e) : NULL; name = n ? strdup(n) : NULL; }
129 OpeningInfo(const OpeningInfo& o)
130 : eco(o.eco ? strdup(o.eco) : NULL), name(o.name ? strdup(o.name) : NULL) {}
131 OpeningInfo() : eco(NULL), name(NULL) {}
132 ~OpeningInfo(){ if(eco) free((void*)eco); if(name) free((void*)name); }
134 typedef std::map<HashKey, OpeningInfo> Openings;
135 Openings openings;
137 char opponent[256];
138 int w_rating;
139 int b_rating;
141 static Engine* instance;
143 /* variables */
144 int v_book_creation_max_depth;
145 int v_book_creation_max_alternatives;
146 int v_book_creation_min_games;
147 int v_book_creation_min_probability;
148 int v_book_creation_weigh_win;
149 int v_book_creation_weigh_draw;
150 int v_book_creation_weigh_lose;
152 int v_search_null_reduction;
153 int v_search_threat_extension;
154 int v_search_threat_threshold;
156 int v_eval_draw;
158 void cmd__check(int argc, char *argv[]);
159 void cmd__attacks(int argc, char *argv[]);
160 void cmd__see(int argc, char *argv[]);
161 void cmd__shat(int argc, char *argv[]);
162 void cmd__find_moves(int argc, char *argv[]);
163 void cmd__gen_hash(int argc, char *argv[]);
164 void cmd__gon(int argc, char *argv[]);
165 void cmd__goff(int argc, char *argv[]);
166 void cmd_quit(int argc, char *argv[]);
167 void cmd_ping(int argc, char *argv[]);
168 void cmd_bench(int argc, char *argv[]);
169 void cmd_perft(int argc, char *argv[]);
170 void cmd_perftall(int argc, char *argv[]);
171 void cmd_sd(int argc, char *argv[]);
172 void cmd_st(int argc, char *argv[]);
173 void cmd_sn(int argc, char *argv[]);
174 void cmd_otim(int argc, char *argv[]);
175 void cmd_time(int argc, char *argv[]);
176 void cmd_level(int argc, char *argv[]);
177 void cmd_help(int argc, char *argv[]);
178 void cmd_force(int argc, char *argv[]);
179 void cmd_undo(int argc, char *argv[]);
180 void cmd_remove(int argc, char *argv[]);
181 void cmd_new(int argc, char *argv[]);
182 void cmd_white(int argc, char *argv[]);
183 void cmd_black(int argc, char *argv[]);
184 void cmd_go(int argc, char *argv[]);
185 void cmd_setboard(int argc, char *argv[]);
186 void cmd_edit(int argc, char *argv[]);
187 void cmd_load(int argc, char *argv[]);
188 void cmd_save(int argc, char *argv[]);
189 void cmd_load_pgn(int argc, char *argv[]);
190 void cmd_epdtest(int argc, char *argv[]);
191 void cmd_challenge(int argc, char *argv[]);
192 void cmd_create_book(int argc, char *argv[]);
193 void cmd_open_book(int argc, char *argv[]);
194 void cmd_close_book(int argc, char *argv[]);
195 void cmd_open_lines(int argc, char *argv[]);
196 void cmd_close_lines(int argc, char *argv[]);
197 void cmd_open_eco(int argc, char *argv[]);
198 void cmd_close_eco(int argc, char *argv[]);
199 void cmd_test(int argc, char *argv[]);
200 void cmd_protover(int argc, char *argv[]);
201 void cmd_accepted(int argc, char *argv[]);
202 void cmd_rejected(int argc, char *argv[]);
203 void cmd_xboard(int argc, char *argv[]);
204 void cmd_analyze(int argc, char *argv[]);
205 void cmd_exit(int argc, char *argv[]);
206 void cmd_easy(int argc, char *argv[]);
207 void cmd_hard(int argc, char *argv[]);
208 void cmd_post(int argc, char *argv[]);
209 void cmd_nopost(int argc, char *argv[]);
210 void cmd_result(int argc, char *argv[]);
211 void cmd_DOT(int argc, char *argv[]);
212 void cmd_QUESTION(int argc, char *argv[]);
213 void cmd_name(int argc, char *argv[]);
214 void cmd_rating(int argc, char *argv[]);
215 void cmd_var(int argc, char *argv[]);
216 void cmd_varhelp(int argc, char *argv[]);
217 void cmd_set(int argc, char *argv[]);
219 void create_hash();
220 void reset_hash();
221 void make_old_hash();
223 HashEntry* probe_hash(const HashKey& hk);
224 void prefetch_hash(const HashKey& hk);
225 void write_hash(const HashKey& hk, int16_t lo, int16_t up, int depth, int best_cont, bool no_good_moves);
227 bool check_time();
228 bool check_draw();
229 bool check_repetition(int nfold);
231 void print_stat();
232 void move_now();
233 static void sort_moves(Move* m,int num);
234 static void incremental_sort_moves(Move* m,int num);
236 void calc_best_time();
239 friend class Board;
241 public:
242 Engine();
243 static Engine *eng();
245 void run_command(int argc, char** argv);
246 void load_ini();
248 uint8_t color_to_move();
249 uint8_t computer_color();
250 int get_status();
251 int move_number();
253 void register_commands();
254 void register_variables();
256 void process_input();
257 void output(const char* fmt, ...) PRINTF(2, 3);
259 Move find_best_move();
261 void move(Move mv);
262 void retract_move();
263 void undo();
264 void start();
265 void start(unsigned char color);
266 void force();
267 void analyze();
268 void do_ponder();
269 void new_game();
270 uint64_t perft(int lev);
271 void set_depth(int depth);
272 void set_max_nodes(int nodes);
273 void set_time_control(int mps, int basesec, int inc);
274 void set_time_fixed(int time);
275 void set_my_clock(int time);
276 void set_other_clock(int time);
278 /* io functions */
279 void save_pgn(FILE *f, const char *result1, const char *result);
280 void print_moves(Move* m, int num, bool nums=true, bool walk_ht=false);
281 void print_board(); //prints colored board to stdout
282 void read_board(char* brd, char* color, char* castle,
283 char* passing, int moves_to_draw, int num_moves);
284 void read_board(char* str);//reads from f a Forsythe-Edwards notation board
285 void read_board(FILE* f); //reads from f a Forsythe-Edwards notation board
286 void write_board(FILE* f); //writes to f a Forsythe-Edwards notation board
287 int load_pgn(FILE *f);
288 int run_epd_prog(const char* prog, int time, const char* file, bool quiet = true);
289 void challenge(const char* prog, int time, const char* file);
291 void create_book(int argc, char *argv[]);
292 bool open_book(const char *f);
293 void close_book();
294 Move probe_book(Board *b);
296 bool open_lines(const char *f);
297 void close_lines();
298 Move probe_lines(Board *b);
300 bool open_openings(const char *f);
301 void close_openings();
302 OpeningInfo* probe_openings(Board *b);
304 bool parse_command(char *str);
306 void autotune();
309 #endif //__ENGINE_H__