summarylogtreecommitdiffstats
path: root/shogidokoro_usi_20140611.patch
blob: 3924b67041bc05a9af812205f32e9120fd6f2e00 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
diff -Nur bonanza_v6.0/src/client/main.c bonanza_v6.0_patched/src/client/main.c
--- bonanza_v6.0/src/client/main.c	2011-05-09 14:01:40 +0900
+++ bonanza_v6.0_patched/src/client/main.c	2015-01-23 11:27:33 +0900
@@ -37,8 +37,10 @@
 #endif
 
 #if defined(USI)
-  if ( argc == 2 && ! strcmp( argv[1], "usi" ) ) { usi_mode = usi_on; }
+/*  if ( argc == 2 && ! strcmp( argv[1], "usi" ) ) { usi_mode = usi_on; }
   else                                           { usi_mode = usi_off; }
+*/
+  usi_mode = usi_on;
 #endif
 
   if ( ini( ptree ) < 0 )
diff -Nur bonanza_v6.0/src/client/proce.c bonanza_v6.0_patched/src/client/proce.c
--- bonanza_v6.0/src/client/proce.c	2011-05-06 23:01:08 +0900
+++ bonanza_v6.0_patched/src/client/proce.c	2015-01-23 11:39:28 +0900
@@ -6,6 +6,38 @@
 #include <string.h>
 #include "shogi.h"
 
+#if defined(USI)
+/* USI options */
+struct {
+	char time_control[ BUFSIZ ];
+	char time_a[ BUFSIZ ];
+	char time_b[ BUFSIZ ];
+	char nodes[ BUFSIZ ];
+	char depth[ BUFSIZ ];
+	char use_book[ BUFSIZ ];
+	char narrow_book[ BUFSIZ ];
+	char strict_time[ BUFSIZ ];
+	char resign[ BUFSIZ ];
+	char memory[ BUFSIZ ];
+	char ponder[ BUFSIZ ];
+	char threads[ BUFSIZ ];
+} usi_name;
+struct {
+	char time_control[ BUFSIZ ];
+	int time_a;
+	int time_b;
+	int nodes;
+	int depth;
+	char use_book[ BUFSIZ ];
+	char narrow_book[ BUFSIZ ];
+	char strict_time[ BUFSIZ ];
+	int resign;
+	int memory;
+	char ponder[ BUFSIZ ];
+	int threads;
+} usi_value;
+#endif
+
 /* unacceptable when the program is thinking, or quit pondering */
 #define AbortDifficultCommand                                              \
 	  if ( game_status & flag_thinking )                               \
@@ -401,6 +433,172 @@
 
 
 #if defined(USI)
+/* initialize usi options */
+static void CONV
+init_usi_option( void )
+{
+  strcpy( usi_name.time_control, "TimeControl" );
+  strcpy( usi_name.time_a,       "TimeA[min]" );
+  strcpy( usi_name.time_b,       "TimeB[sec]" );
+  strcpy( usi_name.nodes,        "Nodes" );
+  strcpy( usi_name.depth,        "Depth" );
+  strcpy( usi_name.use_book,     "UseBook" );
+  strcpy( usi_name.narrow_book,  "NarrowBook" );
+  strcpy( usi_name.strict_time,  "StrictTime" );
+  strcpy( usi_name.resign,       "Resign" );
+  strcpy( usi_name.memory,       "Memory[MB]" );
+  strcpy( usi_name.ponder,       "Ponder" );
+  strcpy( usi_name.threads,      "Threads" );
+
+  strcpy( usi_value.time_control, "Time" );
+  usi_value.time_a  = 0;
+  usi_value.time_b  = 3;
+  usi_value.nodes   = 100000;
+  usi_value.depth   = 5;
+  strcpy( usi_value.use_book,    "true" );
+  strcpy( usi_value.narrow_book, "false" );
+  strcpy( usi_value.strict_time, "true" );
+  usi_value.resign  = 3000;
+  usi_value.memory  = 12;
+  strcpy( usi_value.ponder,      "false" );
+  usi_value.threads = 1;
+}
+
+
+/* set usi_options */
+static void CONV
+set_usi_option( tree_t * restrict ptree, const char *name, const char *value )
+{
+  char str[ SIZE_CMDLINE ];
+  char *last, *token, *ptr;
+
+  if ( ! strcmp( name, usi_name.time_control ) )
+    {
+      strcpy( usi_value.time_control, value );
+    }
+  else if ( ! strcmp( name, usi_name.time_a ) && ! strcmp( usi_value.time_control, "Time") )
+    {
+      usi_value.time_a = (int)strtol( value, &ptr, 0 );
+    }
+  else if ( ! strcmp( name, usi_name.time_b ) && ! strcmp( usi_value.time_control, "Time") )
+    {
+      usi_value.time_b = (int)strtol( value, &ptr, 0 );
+      sprintf( str, "limit time %d %d", usi_value.time_a, usi_value.time_b );
+      token = strtok_r( str, str_delimiters, &last );
+      cmd_limit( &last );
+    }
+  else if ( ! strcmp( name, usi_name.nodes ) && ! strcmp( usi_value.time_control, "Nodes") )
+    {
+      usi_value.nodes = (int)strtol( value, &ptr, 0 );
+      sprintf( str, "limit nodes %d", usi_value.nodes );
+      token = strtok_r( str, str_delimiters, &last );
+      cmd_limit( &last );
+    }
+  else if ( ! strcmp( name, usi_name.depth ) && ! strcmp( usi_value.time_control, "Depth") )
+    {
+      usi_value.depth = (int)strtol( value, &ptr, 0 );
+      sprintf( str, "limit depth %d", usi_value.depth );
+      token = strtok_r( str, str_delimiters, &last );
+      cmd_limit( &last );
+    }
+  else if ( ! strcmp( name, usi_name.use_book ) )
+    {
+      strcpy( usi_value.use_book, value );
+      sprintf( str, "book %s", ( ! strcmp( usi_value.use_book, "true" ) ? str_on : str_off ) );
+      token = strtok_r( str, str_delimiters, &last );
+      CmdBook( ptree, &last );
+    }
+  else if ( ! strcmp( name, usi_name.narrow_book ) )
+    {
+      strcpy( usi_value.narrow_book, value );
+      sprintf( str, "book %s", ( ! strcmp( usi_value.narrow_book, "true" ) ? "narrow" : "wide" ) );
+      token = strtok_r( str, str_delimiters, &last );
+      CmdBook( ptree, &last );
+    }
+  else if ( ! strcmp( name, usi_name.strict_time ) )
+    {
+      strcpy( usi_value.strict_time, value );
+      sprintf( str, "limit time %s", ( !strcmp( usi_value.strict_time, "true" ) ? "strict" : "extendable" ) );
+      token = strtok_r( str, str_delimiters, &last );
+      cmd_limit( &last );
+    }
+  else if ( ! strcmp( name, usi_name.resign ) )
+    {
+      usi_value.resign = (int)strtol( value, &ptr, 0 );
+      sprintf( str, "resign %d", usi_value.resign );
+      token = strtok_r( str, str_delimiters, &last );
+      cmd_resign( ptree, &last );
+    }
+  else if ( ! strcmp( name, usi_name.memory ) )
+    {
+      int hash;
+      usi_value.memory = (int)strtol( value, &ptr, 0 );
+      hash = (int)(log(usi_value.memory * 1e6 / 48) / log(2)) + 1;
+      sprintf( str, "hash %d", hash );
+      token = strtok_r( str, str_delimiters, &last );
+      cmd_hash ( &last );
+    }
+  else if ( ! strcmp( name, usi_name.ponder ) )
+    {
+/* (not implemented)
+      strcpy( usi_value.ponder, value );
+      sprintf( str, "ponder %s", ( ! strcmp( usi_value.ponder, "true" ) ? str_on : str_off ) );
+      token = strtok_r( str, str_delimiters, &last );
+      cmd_ponder( &last );
+*/
+    }
+  else if ( ! strcmp( name, usi_name.threads ) )
+    {
+      usi_value.threads = (int)strtol( value, &ptr, 0 );
+#if defined(TLP)
+      sprintf( str, "tlp num %d", usi_value.threads );
+      token = strtok_r( str, str_delimiters, &last );
+      cmd_thread( &last );
+#endif
+    }
+}
+
+
+/* "setoption name XXX value YYY" */
+static int CONV
+usi_option( tree_t * restrict ptree, char **lasts )
+{
+  const char *str1, *str2, *str3, *str4;
+
+  str1 = strtok_r( NULL, str_delimiters, lasts );
+  if ( str1 == NULL || strcmp( str1, "name" ) )
+    {
+      str_error = str_bad_cmdline;
+      return -1;
+    }
+
+  str2 = strtok_r( NULL, str_delimiters, lasts );
+  if ( str2 == NULL )
+    {
+      str_error = str_bad_cmdline;
+      return -1;
+    }
+
+  str3 = strtok_r( NULL, str_delimiters, lasts );
+  if ( str3 == NULL || strcmp( str3, "value" ) )
+    {
+      str_error = str_bad_cmdline;
+      return -1;
+    }
+
+  str4 = strtok_r( NULL, str_delimiters, lasts );
+  if ( str4 == NULL )
+    {
+      str_error = str_bad_cmdline;
+      return -1;
+    }
+
+  set_usi_option( ptree, str2, str4 );
+
+  return 1;
+}
+
+
 static int CONV proce_usi( tree_t * restrict ptree )
 {
   const char *token;
@@ -414,16 +612,44 @@
     {
       USIOut( "id name %s\n", str_myname );
       USIOut( "id author Kunihito Hoki\n" );
+
+      /* options */
+      init_usi_option();
+      USIOut( "option name %s type combo default %s var Time var Nodes var Depth\n",                                 usi_name.time_control, usi_value.time_control );
+      USIOut( "option name %s type spin default %d min 0 max 600\n",                                                 usi_name.time_a,       usi_value.time_a );
+      USIOut( "option name %s type spin default %d min 0 max 36000\n",                                               usi_name.time_b,       usi_value.time_b );
+      USIOut( "option name %s type spin default %d min 1000 max 100000000\n",                                        usi_name.nodes,        usi_value.nodes );
+      USIOut( "option name %s type spin default %d min 1 max 30\n",                                                  usi_name.depth,        usi_value.depth );
+      USIOut( "option name %s type check default %s\n",                                                              usi_name.use_book,     usi_value.use_book );
+      USIOut( "option name %s type check default %s\n",                                                              usi_name.narrow_book,  usi_value.narrow_book );
+      USIOut( "option name %s type check default %s\n",                                                              usi_name.strict_time,  usi_value.strict_time );
+      USIOut( "option name %s type combo default %d var 1000 var 2000 var 3000 var 5000 var 32596\n",                usi_name.resign,       usi_value.resign );
+      USIOut( "option name %s type combo default %d var 12 var 24 var 48 var 96 var 192 var 384 var 768 var 1536\n", usi_name.memory,       usi_value.memory );
+      USIOut( "option name %s type check default %s\n",                                                              usi_name.ponder,       usi_value.ponder );
+      USIOut( "option name %s type spin default %d min 1 max 256\n",                                                 usi_name.threads,      usi_value.threads );
+
       USIOut( "usiok\n" );
+
       return 1;
     }
 
+  /* set options */
+  if ( ! strcmp( token, "setoption" ) )
+    {
+      return usi_option( ptree, &lasts );
+    }
+
   if ( ! strcmp( token, "isready" ) )
     {
       USIOut( "readyok\n", str_myname );
       return 1;
     }
 
+  if ( ! strcmp( token, "usinewgame" ) )
+    {
+      return 1;
+    }
+
   if ( ! strcmp( token, "echo" ) )
     {
       USIOut( "%s\n", lasts );
@@ -448,10 +674,15 @@
       return iret;
     }
 
+  if ( ! strcmp( token, "gameover" ) )
+    {
+      return 1;
+    }
+
   if ( ! strcmp( token, "stop" ) )     { return cmd_move_now(); }
   if ( ! strcmp( token, "position" ) ) { return usi_posi( ptree, &lasts ); }
   if ( ! strcmp( token, "quit" ) )     { return cmd_quit(); }
-  
+
   str_error = str_bad_cmdline;
   return -1;
 }
@@ -471,7 +702,7 @@
     {
       token = strtok_r( NULL, str_delimiters, lasts );
       if ( token == NULL ) { break; }
-      
+
       if ( usi2csa( ptree, token, str_buf ) < 0 )            { return -1; }
       if ( interpret_CSA_move( ptree, &move, str_buf ) < 0 ) { return -1; }
 
@@ -499,7 +730,7 @@
       str_error = str_game_ended;
       return -1;
     }
-  
+
   token = strtok_r( NULL, str_delimiters, lasts );
 
   if ( ! strcmp( token, "book" ) )
@@ -518,6 +749,9 @@
       node_limit      = UINT64_MAX;
       sec_limit_depth = UINT_MAX;
     }
+  else if ( ! strcmp( token, "btime" ) )
+    {
+    }
   else if ( ! strcmp( token, "byoyomi" ) )
     {
       token = strtok_r( NULL, str_delimiters, lasts );
@@ -533,18 +767,22 @@
 	  str_error = str_bad_cmdline;
 	  return -1;
 	}
-      
+
       usi_byoyomi     = (unsigned int)l;
       depth_limit     = PLY_MAX;
       node_limit      = UINT64_MAX;
       sec_limit_depth = UINT_MAX;
     }
+  else if ( ! strcmp( token, "mate" ) )
+    {
+      /* not implemented */
+    }
   else {
     str_error = str_bad_cmdline;
     return -1;
   }
 
-      
+
   if ( get_elapsed( &time_turn_start ) < 0 ) { return -1; }
 
   iret = com_turn_start( ptree, 0 );
@@ -552,7 +790,7 @@
     if ( str_error == str_no_legal_move ) { USIOut( "bestmove resign\n" ); }
     else                                  { return -1; }
   }
-  
+
   return 1;
 }
 
diff -Nur bonanza_v6.0/src/client/searchr.c bonanza_v6.0_patched/src/client/searchr.c
--- bonanza_v6.0/src/client/searchr.c	2011-05-06 23:01:08 +0900
+++ bonanza_v6.0_patched/src/client/searchr.c	2015-01-23 11:38:44 +0900
@@ -372,7 +372,7 @@
 	  Out( "%2d.%c%-7s", ply, ach_turn[tt], str );
 
 #if defined(USI)
-	  if ( usi_mode != usi_off && ply <= 4 )
+	  if ( usi_mode != usi_off )
 	    {
 	      char str_usi[6];
 	      csa2usi( ptree, str_CSA_move(ptree->pv[0].a[ply]), str_usi );
diff -Nur bonanza_v6.0/src/client/utility.c bonanza_v6.0_patched/src/client/utility.c
--- bonanza_v6.0/src/client/utility.c	2011-05-06 23:01:08 +0900
+++ bonanza_v6.0_patched/src/client/utility.c	2015-01-23 11:38:14 +0900
@@ -519,6 +519,12 @@
 	  if ( iret < 0 ) { return iret; }
 	}
 #endif
+#if defined(USI)
+      if ( usi_mode != usi_off )
+        {
+          USIOut( "bestmove resign\n" );
+        }
+#endif
       OutCsaShogi( "resign\n" );
     }
   else {