Change default time settings from -t =80000 to -t 15
[pachi/nmclean.git] / pachi.c
blob643a203abe22956882d600d1a166cf7896dc7e7f
1 #define DEBUG
2 #include <assert.h>
3 #include <getopt.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <time.h>
8 #include <unistd.h>
10 #include "board.h"
11 #include "debug.h"
12 #include "engine.h"
13 #include "replay/replay.h"
14 #include "montecarlo/montecarlo.h"
15 #include "random/random.h"
16 #include "patternscan/patternscan.h"
17 #include "patternplay/patternplay.h"
18 #include "joseki/joseki.h"
19 #include "t-unit/test.h"
20 #include "uct/uct.h"
21 #include "distributed/distributed.h"
22 #include "gtp.h"
23 #include "chat.h"
24 #include "timeinfo.h"
25 #include "random.h"
26 #include "version.h"
27 #include "network.h"
29 int debug_level = 3;
30 bool debug_boardprint = true;
31 long verbose_logs = 0;
32 int seed;
35 enum engine_id {
36 E_RANDOM,
37 E_REPLAY,
38 E_PATTERNSCAN,
39 E_PATTERNPLAY,
40 E_MONTECARLO,
41 E_UCT,
42 E_DISTRIBUTED,
43 E_JOSEKI,
44 E_MAX,
47 static struct engine *(*engine_init[E_MAX])(char *arg, struct board *b) = {
48 engine_random_init,
49 engine_replay_init,
50 engine_patternscan_init,
51 engine_patternplay_init,
52 engine_montecarlo_init,
53 engine_uct_init,
54 engine_distributed_init,
55 engine_joseki_init,
58 static struct engine *init_engine(enum engine_id engine, char *e_arg, struct board *b)
60 char *arg = e_arg? strdup(e_arg) : e_arg;
61 assert(engine < E_MAX);
62 struct engine *e = engine_init[engine](arg, b);
63 if (arg) free(arg);
64 return e;
67 static void done_engine(struct engine *e)
69 if (e->done) e->done(e);
70 if (e->data) free(e->data);
71 free(e);
74 static void usage(char *name)
76 fprintf(stderr, "Pachi version %s\n", PACHI_VERSION);
77 fprintf(stderr, "Usage: %s [-e random|replay|montecarlo|uct|distributed]\n"
78 " [-d DEBUG_LEVEL] [-D] [-r RULESET] [-s RANDOM_SEED] [-t TIME_SETTINGS] [-u TEST_FILENAME]\n"
79 " [-g [HOST:]GTP_PORT] [-l [HOST:]LOG_PORT] [-f FBOOKFILE] [ENGINE_ARGS]\n", name);
82 int main(int argc, char *argv[])
84 enum engine_id engine = E_UCT;
85 struct time_info ti_default = { .period = TT_NULL };
86 char *testfile = NULL;
87 char *gtp_port = NULL;
88 char *log_port = NULL;
89 int gtp_sock = -1;
90 char *chatfile = NULL;
91 char *fbookfile = NULL;
92 char *ruleset = NULL;
94 seed = time(NULL) ^ getpid();
96 int opt;
97 while ((opt = getopt(argc, argv, "c:e:d:Df:g:l:r:s:t:u:")) != -1) {
98 switch (opt) {
99 case 'c':
100 chatfile = strdup(optarg);
101 break;
102 case 'e':
103 if (!strcasecmp(optarg, "random")) {
104 engine = E_RANDOM;
105 } else if (!strcasecmp(optarg, "replay")) {
106 engine = E_REPLAY;
107 } else if (!strcasecmp(optarg, "montecarlo")) {
108 engine = E_MONTECARLO;
109 } else if (!strcasecmp(optarg, "uct")) {
110 engine = E_UCT;
111 } else if (!strcasecmp(optarg, "distributed")) {
112 engine = E_DISTRIBUTED;
113 } else if (!strcasecmp(optarg, "patternscan")) {
114 engine = E_PATTERNSCAN;
115 } else if (!strcasecmp(optarg, "patternplay")) {
116 engine = E_PATTERNPLAY;
117 } else if (!strcasecmp(optarg, "joseki")) {
118 engine = E_JOSEKI;
119 } else {
120 fprintf(stderr, "%s: Invalid -e argument %s\n", argv[0], optarg);
121 exit(1);
123 break;
124 case 'd':
125 debug_level = atoi(optarg);
126 break;
127 case 'D':
128 debug_boardprint = false;
129 break;
130 case 'f':
131 fbookfile = strdup(optarg);
132 break;
133 case 'g':
134 gtp_port = strdup(optarg);
135 break;
136 case 'l':
137 log_port = strdup(optarg);
138 break;
139 case 'r':
140 ruleset = strdup(optarg);
141 break;
142 case 's':
143 seed = atoi(optarg);
144 break;
145 case 't':
146 /* Time settings to follow; if specified,
147 * GTP time information is ignored. Useful
148 * e.g. when you want to force your bot to
149 * play weaker while giving the opponent
150 * reasonable time to play, or force play
151 * by number of simulations in timed games. */
152 /* Please see timeinfo.h:time_parse()
153 * description for syntax details. */
154 if (!time_parse(&ti_default, optarg)) {
155 fprintf(stderr, "%s: Invalid -t argument %s\n", argv[0], optarg);
156 exit(1);
158 ti_default.ignore_gtp = true;
159 assert(ti_default.period != TT_NULL);
160 break;
161 case 'u':
162 testfile = strdup(optarg);
163 break;
164 default: /* '?' */
165 usage(argv[0]);
166 exit(1);
170 if (log_port)
171 open_log_port(log_port);
173 fast_srandom(seed);
174 if (DEBUGL(0))
175 fprintf(stderr, "Random seed: %d\n", seed);
177 struct board *b = board_init(fbookfile);
178 if (ruleset) {
179 if (!board_set_rules(b, ruleset)) {
180 fprintf(stderr, "Unknown ruleset: %s\n", ruleset);
181 exit(1);
185 struct time_info ti[S_MAX];
186 ti[S_BLACK] = ti_default;
187 ti[S_WHITE] = ti_default;
189 chat_init(chatfile);
191 char *e_arg = NULL;
192 if (optind < argc)
193 e_arg = argv[optind];
194 struct engine *e = init_engine(engine, e_arg, b);
196 if (testfile) {
197 unittest(testfile);
198 return 0;
201 if (gtp_port) {
202 open_gtp_connection(&gtp_sock, gtp_port);
205 for (;;) {
206 char buf[4096];
207 while (fgets(buf, 4096, stdin)) {
208 if (DEBUGL(1))
209 fprintf(stderr, "IN: %s", buf);
211 enum parse_code c = gtp_parse(b, e, ti, buf);
212 if (c == P_ENGINE_RESET) {
213 ti[S_BLACK] = ti_default;
214 ti[S_WHITE] = ti_default;
215 if (!e->keep_on_clear) {
216 b->es = NULL;
217 done_engine(e);
218 e = init_engine(engine, e_arg, b);
220 } else if (c == P_UNKNOWN_COMMAND && gtp_port) {
221 /* The gtp command is a weak identity check,
222 * close the connection with a wrong peer. */
223 break;
226 if (!gtp_port) break;
227 open_gtp_connection(&gtp_sock, gtp_port);
229 done_engine(e);
230 chat_done();
231 return 0;