Initial support for a qt gui to show search.
[rattatechess.git] / engine.h
blob37fe19ec58acdb3952f8530c860d026ae02aaf1f
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__
22 #define PLAYING 1
23 #define _10 2
24 #define _01 3
25 #define _12 4
27 //#define PLAYING 1
28 #define ANALYZING 2
29 #define FORCING 3
31 #define TIME_LIMIT 1
32 #define DEPTH_LIMIT 2
33 #define NODES_LIMIT 3
36 #include <map>
37 #include <string>
38 #include <list>
39 #include <setjmp.h>
40 #include "board.h"
42 class Engine;
43 class SearchGui;
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 Move* mv_done;
59 int mv_done_num;
61 /* the max depth of the engine */
62 int st_depth;
63 int st_nodes;
65 /* the color of the engine */
66 uint8_t st_computer_color;
68 /* the time the engine will use to think */
69 int time_best_csec;
71 /* if there is a fixed time for a move, or else 0 */
72 int time_fixed;
74 /* time move per section, time per section and inc... */
75 int time_mps;
76 int time_base;
77 int time_inc;
79 /* clok and other clock in sec/100 */
80 int time_clock_csec;
81 int time_oth_clock_csec;
83 /* flags for options of the engine */
84 int analysis_limit;
86 bool ponder;
87 bool post;
88 //uint8_t eng_color;
89 uint64_t processed_nodes;
90 int max_think_time;
92 /* flag for output behaviour */
93 bool io_xboard;
94 bool io_san;
95 bool io_colors;
97 /* engine status */
98 int status;
99 int eng_status;
100 const char *status_string;
101 bool thinking;
102 int start_think_time;
103 uint8_t analysis_color;
105 FILE *log;
106 CmdSet commands;
107 VariableSet variables;
108 jmp_buf back;
110 HashEntry *hash_table;
111 void *book_mmap;
112 uint32_t book_size;
114 char opponent[256];
115 int w_rating;
116 int b_rating;
118 static Engine* instance;
120 /* variables */
121 int v_book_creation_max_depth;
122 int v_book_creation_max_alternatives;
123 int v_book_creation_min_games;
124 int v_book_creation_min_probability;
125 int v_book_creation_weigh_win;
126 int v_book_creation_weigh_draw;
127 int v_book_creation_weigh_lose;
129 int v_search_null_reduction;
130 int v_search_threat_extension;
131 int v_search_threat_threshold;
133 int v_eval_draw;
135 void cmd__check(int argc, char *argv[]);
136 void cmd__attacks(int argc, char *argv[]);
137 void cmd__see(int argc, char *argv[]);
138 void cmd__shat(int argc, char *argv[]);
139 void cmd__find_moves(int argc, char *argv[]);
140 void cmd__gen_hash(int argc, char *argv[]);
141 void cmd__rewrite_hash(int argc, char *argv[]);
142 void cmd__gon(int argc, char *argv[]);
143 void cmd__goff(int argc, char *argv[]);
144 void cmd_quit(int argc, char *argv[]);
145 void cmd_ping(int argc, char *argv[]);
146 void cmd_bench(int argc, char *argv[]);
147 void cmd_perft(int argc, char *argv[]);
148 void cmd_perftall(int argc, char *argv[]);
149 void cmd_sd(int argc, char *argv[]);
150 void cmd_st(int argc, char *argv[]);
151 void cmd_sn(int argc, char *argv[]);
152 void cmd_otim(int argc, char *argv[]);
153 void cmd_time(int argc, char *argv[]);
154 void cmd_level(int argc, char *argv[]);
155 void cmd_help(int argc, char *argv[]);
156 void cmd_force(int argc, char *argv[]);
157 void cmd_undo(int argc, char *argv[]);
158 void cmd_remove(int argc, char *argv[]);
159 void cmd_new(int argc, char *argv[]);
160 void cmd_white(int argc, char *argv[]);
161 void cmd_black(int argc, char *argv[]);
162 void cmd_go(int argc, char *argv[]);
163 void cmd_setboard(int argc, char *argv[]);
164 void cmd_edit(int argc, char *argv[]);
165 void cmd_load(int argc, char *argv[]);
166 void cmd_save(int argc, char *argv[]);
167 void cmd_load_pgn(int argc, char *argv[]);
168 void cmd_epdtest(int argc, char *argv[]);
169 void cmd_challenge(int argc, char *argv[]);
170 void cmd_create_book(int argc, char *argv[]);
171 void cmd_open_book(int argc, char *argv[]);
172 void cmd_close_book(int argc, char *argv[]);
173 void cmd_test(int argc, char *argv[]);
174 void cmd_protover(int argc, char *argv[]);
175 void cmd_accepted(int argc, char *argv[]);
176 void cmd_rejected(int argc, char *argv[]);
177 void cmd_xboard(int argc, char *argv[]);
178 void cmd_analyze(int argc, char *argv[]);
179 void cmd_exit(int argc, char *argv[]);
180 void cmd_easy(int argc, char *argv[]);
181 void cmd_hard(int argc, char *argv[]);
182 void cmd_post(int argc, char *argv[]);
183 void cmd_nopost(int argc, char *argv[]);
184 void cmd_result(int argc, char *argv[]);
185 void cmd_DOT(int argc, char *argv[]);
186 void cmd_QUESTION(int argc, char *argv[]);
187 void cmd_name(int argc, char *argv[]);
188 void cmd_rating(int argc, char *argv[]);
189 void cmd_var(int argc, char *argv[]);
190 void cmd_varhelp(int argc, char *argv[]);
192 void create_hash();
193 void reset_hash();
194 void make_old_hash();
196 HashEntry* probe_hash(const HashKey& hk);
197 void prefetch_hash(const HashKey& hk);
198 void write_hash(int16_t lo, int16_t up, int depth, int best_cont);
200 bool check_time();
201 bool check_draw();
202 bool insufficient_material();
204 void rewind_thinking();
205 void restore_thinking();
206 void print_stat();
207 void move_now();
208 bool null_move_ok();
209 int16_t search(int ply, int depth, bool pv,
210 int16_t alpha, int16_t beta);
211 void moves_heuristic(Move *mv, int num_mv, int ply,
212 int orig_depth, Move best_mv_hash, bool quiesce, Move* prev);
213 void moves_quiescence_heuristic(Move *mv, int num_mv, const Move& hash,
214 int static_eval, int alpha, int beta, int depth, Move* prev);
215 static void sort_moves(Move* m,int num);
216 static void incremental_sort_moves(Move* m,int num);
218 void calc_best_time();
221 friend class Board;
223 public:
224 Engine();
225 static Engine *eng();
227 uint8_t color_to_move();
228 uint8_t computer_color();
229 int get_status();
230 int move_number();
232 void register_commands();
233 void register_variables();
235 void process_input();
236 void output(const char* fmt, ...) PRINTF(2, 3);
238 Move find_best_move();
240 void move(Move mv);
241 void retract_move();
242 void undo();
243 void start();
244 void start(unsigned char color);
245 void force();
246 void analyze();
247 void do_ponder();
248 void new_game();
249 uint64_t perft(int lev);
250 void set_depth(int depth);
251 void set_max_nodes(int nodes);
252 void set_time_control(int mps, int basesec, int inc);
253 void set_time_fixed(int time);
254 void set_my_clock(int time);
255 void set_other_clock(int time);
257 int parse_move(Move* mv,char* str);
258 char* move_to_coord(char* str,Move* mv);
259 char* move_to_alg(char* str,Move* mv);
261 /* io functions */
262 void save_pgn(FILE *f, const char *result1, const char *result);
263 void print_moves(Move* m,int num,bool nums=1);
264 void print_board(); //prints colored board to stdout
265 void read_board(char* brd, char* color, char* castle,
266 char* passing, int moves_to_draw, int num_moves);
267 void read_board(char* str);//reads from f a Forsythe-Edwards notation board
268 void read_board(FILE* f); //reads from f a Forsythe-Edwards notation board
269 void write_board(FILE* f); //writes to f a Forsythe-Edwards notation board
270 int load_pgn(FILE *f);
271 int run_epd_prog(const char* prog, int time, const char* file, bool quiet = true);
272 void challenge(const char* prog, int time, const char* file);
274 void create_book(int argc, char *argv[]);
275 bool open_book(const char *f);
276 void close_book();
277 bool probe_book(const HashKey& h, Move* m);
279 typedef std::map< HashKey, std::list< Move > > Lines;
280 Lines lines;
281 bool open_lines(const char *f);
282 void close_lines();
283 bool probe_lines(const HashKey& h, Move* m);
285 bool parse_command(char *str);
287 void autotune();
290 #endif //__ENGINE_H__