forked from oliviadecodes/Pachi_Source
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine.h
128 lines (106 loc) · 4.26 KB
/
engine.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
#ifndef PACHI_ENGINE_H
#define PACHI_ENGINE_H
#include "board.h"
#include "move.h"
#include "gtp.h"
struct move_queue;
typedef struct engine *(*engine_init_t)(char *arg, struct board *b);
typedef enum parse_code (*engine_notify_t)(struct engine *e, struct board *b, int id, char *cmd, char *args, char **reply);
typedef void (*engine_board_print_t)(struct engine *e, struct board *b, FILE *f);
typedef char *(*engine_notify_play_t)(struct engine *e, struct board *b, struct move *m, char *enginearg);
typedef char *(*engine_undo_t)(struct engine *e, struct board *b);
typedef char *(*engine_result_t)(struct engine *e, struct board *b);
typedef char *(*engine_chat_t)(struct engine *e, struct board *b, bool in_game, char *from, char *cmd);
typedef coord_t (*engine_genmove_t)(struct engine *e, struct board *b, struct time_info *ti, enum stone color, bool pass_all_alive);
typedef void (*engine_best_moves_t)(struct engine *e, struct board *b, struct time_info *ti, enum stone color,
coord_t *best_c, float *best_r, int nbest);
typedef char *(*engine_genmoves_t)(struct engine *e, struct board *b, struct time_info *ti, enum stone color,
char *args, bool pass_all_alive, void **stats_buf, int *stats_size);
typedef void (*engine_evaluate_t)(struct engine *e, struct board *b, struct time_info *ti, floating_t *vals, enum stone color);
typedef void (*engine_dead_group_list_t)(struct engine *e, struct board *b, struct move_queue *mq);
typedef void (*engine_stop_t)(struct engine *e);
typedef void (*engine_done_t)(struct engine *e);
typedef struct board_ownermap* (*engine_ownermap_t)(struct engine *e, struct board *b);
typedef void (*engine_livegfx_hook_t)(struct engine *e);
/* This is engine data structure. A new engine instance is spawned
* for each new game during the program lifetime. */
struct engine {
char *name;
char *comment;
/* If set, do not reset the engine state on clear_board. */
bool keep_on_clear;
engine_notify_t notify;
engine_board_print_t board_print;
engine_notify_play_t notify_play;
engine_chat_t chat;
engine_undo_t undo;
engine_result_t result;
/* Generate a move. If pass_all_alive is true, <pass> shall be generated only
* if all stones on the board can be considered alive, without regard to "dead"
* considered stones. */
engine_genmove_t genmove;
/* Used by distributed engine */
engine_genmoves_t genmoves;
/* List best moves for current position. */
engine_best_moves_t best_moves;
/* Evaluate feasibility of player @color playing at all free moves. Will
* simulate each move from b->f[i] for time @ti, then set
* 1-max(opponent_win_likelihood) in vals[i]. */
engine_evaluate_t evaluate;
/* One dead group per queued move (coord_t is (ab)used as group_t). */
engine_dead_group_list_t dead_group_list;
/* Pause any background thinking being done, but do not tear down
* any data structures yet. */
engine_stop_t stop;
/* e->data and e will be free()d by caller afterwards. */
engine_done_t done;
/* Return current ownermap, if engine supports it. */
engine_ownermap_t ownermap;
/* GoGui hook */
engine_livegfx_hook_t livegfx_hook;
void *data;
};
static inline void
engine_board_print(struct engine *e, struct board *b, FILE *f)
{
(e->board_print ? e->board_print(e, b, f) : board_print(b, f));
}
static inline void
engine_done(struct engine *e)
{
if (e->done) e->done(e);
if (e->data) free(e->data);
free(e);
}
/* For engines best_move(): Add move @c with prob @r to best moves @best_c, @best_r */
static inline void
best_moves_add(coord_t c, float r, coord_t *best_c, float *best_r, int nbest)
{
for (int i = 0; i < nbest; i++)
if (r > best_r[i]) {
for (int j = nbest - 1; j > i; j--) { // shift
best_r[j] = best_r[j - 1];
best_c[j] = best_c[j - 1];
}
best_r[i] = r;
best_c[i] = c;
break;
}
}
static inline void
best_moves_add_full(coord_t c, float r, void *d, coord_t *best_c, float *best_r, void **best_d, int nbest)
{
for (int i = 0; i < nbest; i++)
if (r > best_r[i]) {
for (int j = nbest - 1; j > i; j--) { // shift
best_r[j] = best_r[j - 1];
best_c[j] = best_c[j - 1];
best_d[j] = best_d[j - 1];
}
best_r[i] = r;
best_c[i] = c;
best_d[i] = d;
break;
}
}
#endif