-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuci.jai
368 lines (317 loc) · 8.73 KB
/
uci.jai
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
main :: () {
chess_startpos :: (chessgame: *ChessGame) #expand {
chess_startpos(*chessgame.chess);
chessgame.nnue[chessgame.ply].accumulator.computedAccumulation = 0;
}
chess_fen :: (chessgame: *ChessGame, fen_string: string) -> bool #expand {
chessgame.nnue[chessgame.ply].accumulator.computedAccumulation = 0;
return chess_fen(*chessgame.chess, fen_string);
}
nnue_startup();
init_global_bitboards();
initialize_move_randomness();
init_ttable();
initialize_chess_game_memory(*chess);
chess.main_thread = true;
chess_startpos(*chess);
fifty := 0;
for :getline input: os {
reset_temporary_storage();
if equal(input, "quit") {
free_threads();
return;
}
if equal(input, "uci") {
print(uci_response);
print("option name Clear Hash type button%1", NEWLINE);
print("option name Hash type spin default 16 min 1 max 2000%1", NEWLINE);
print("option name Threads type spin default 1 min 1 max 512%1", NEWLINE);
print("option name MultiPV type spin default 1 min 1 max 100%1", NEWLINE);
print("option name Difficulty type spin default 8 min 1 max 8%1", NEWLINE);
print("uciok%1", NEWLINE);
}
if equal(input, "isready") {
print("readyok%", NEWLINE);
}
if equal(input, "ucinewgame") {
Clear_Hash();
chess_startpos(*chess);
}
if equal(input, "perft_all") {
perft_all();
}
if equal(input, "eval") {
eval := uci_evaluate(*chess);
print_chess(*chess);
push_allocator(temp);
str := to_fen_string(*chess);
print("FEN=[%1]%2", str, NEWLINE);
print("Evaluate = %1 cp%2%2", eval, NEWLINE);
}
if begins_with(input, "position ") {
fifty = parse_position(input, *chess);
}
if begins_with(input, "go") {
go_search(input, *chess, fifty);
}
if begins_with(input, "setoption ") {
set_option(input);
}
}
}
#scope_file
os: OS;
parse_position :: (line: string, chess: *ChessGame) -> fifty: int {
clear(chess);
// TODO: this does not guard against incoherent IO, this is just
// to get it working...
fifty := 0;
input := advance(line, 9);
input = eat_spaces(input);
if begins_with(input, "fen ") {
input = advance(input, 4);
input = eat_spaces(input);
index := find_index_from_left(input, "moves");
fen_string := ifx index != -1 then slice(input, 0, index) else input;
if !chess_fen(chess, fen_string) {
print("invalid fen %1%2", fen_string, NEWLINE);
chess_startpos(chess);
return 0;
}
if index != -1 {
input = advance(input, index);
input = advance(input, 5);
input = eat_spaces(input);
} else {
input = advance(input, input.count);
}
} else if begins_with(input, "startpos") {
input = advance(input, 8);
input = eat_spaces(input);
chess_startpos(chess);
index := find_index_from_left(input, "moves");
if index != -1 {
input = advance(input, index);
input = advance(input, 5);
input = eat_spaces(input);
} else {
input = advance(input, input.count);
}
} else {
// we don't know what the input is, so return
return 0;
}
// we setup the position, now just to parse the moves
while input {
token := parse_token(*input);
x1 := cast(int)(token[0] - #char "a");
x2 := cast(int)(token[1] - #char "1");
y1 := cast(int)(token[2] - #char "a");
y2 := cast(int)(token[3] - #char "1");
from := x2*8 + x1;
to := y2*8 + y1;
promote := ifx token.count == 5 then token[4] else 0;
move_type := get_move16_flag(chess, from, to, promote);
move := to_move16(from, to, move_type);
make_move(chess, move);
if (piece_at(chess,to) != Piece.W_PAWN && piece_at(chess,to) != Piece.B_PAWN) && move_type == Move16.Quiet {
fifty += 1;
} else {
fifty = 0;
}
}
return fifty;
}
go_search :: (line: string, chess: *ChessGame, fifty: int) {
parse_token(*line);
token := parse_token(*line);
if equal(token, "perft") {
depth, TF := parse_int(*line);
if !TF return;
perft_divide(chess, depth);
return;
}
depth := -1;
nodes := -1;
movetime := -1;
movestogo := -1;
time := -1;
incr := 0;
while line {
if token == {
case "depth";
value, TF := parse_int(*line);
if TF == false {
print("info string error unable to parse depth%", NEWLINE);
return;
}
depth = value;
case "nodes";
value, TF := parse_int(*line);
if TF == false {
print("info string error unable to parse nodes%", NEWLINE);
return;
}
nodes = value;
case "movetime";
value, TF := parse_int(*line);
if TF == false {
print("info string error unable to parse movetime%", NEWLINE);
return;
}
movetime = value;
case "wtime";
value, TF := parse_int(*line);
if TF == false {
print("info string error unable to parse wtime%", NEWLINE);
return;
}
if chess.turn == .WHITE {
time = value;
}
case "btime";
value, TF := parse_int(*line);
if TF == false {
print("info string error unable to parse btime%", NEWLINE);
return;
}
if chess.turn == .BLACK {
time = value;
}
case "winc";
value, TF := parse_int(*line);
if TF == false {
print("info string error unable to parse winc%", NEWLINE);
return;
}
if chess.turn == .WHITE {
incr = value;
}
case "binc";
value, TF := parse_int(*line);
if TF == false {
print("info string error unable to parse binc%", NEWLINE);
return;
}
if chess.turn == .BLACK {
incr = value;
}
case "movestogo";
value, TF := parse_int(*line);
if TF == false {
print("info string error unable to parse movestogo%", NEWLINE);
return;
}
movestogo = value;
case;
print("info string error unable to parse [%1]%2", line, NEWLINE);
return;
}
token = parse_token(*line);
token = eat_spaces(token);
}
if time > -1 && movetime == -1 then {
movetime = time_management(time, incr, movestogo, chess.ply);
}
chess.maxnodes = nodes;
chess.movetime = movetime;
chess.maxdepth = depth;
chess.fifty = fifty;
mov := uci_search(chess);
push_allocator(temp);
str := to_string(mov);
print("bestmove %1%2", str, NEWLINE);
}
set_option :: (line: string) {
parse_token(*line);
token, TF := parse_token(*line);
if TF == false || !equal(token, "name") {
print("info string error. unable to parse setoption%", NEWLINE);
return;
}
token = eat_spaces(line);
found, left, right := split_from_left(token, " value ");
if left == {
case "Clear Hash";
if right {
print("info string error. Clear Hash cannot be assigned a value%", NEWLINE);
return;
}
print("info string Transposition Table Cleared%", NEWLINE);
Clear_Hash();
case "Hash";
num, tf := parse_int(*right);
if !tf {
print("info string error. Invalid Hash Value%", NEWLINE);
return;
}
if num >= 1 && num <= 2000 {
num *= 1_000_000;
init_ttable(num);
} else {
print("info string error. Invalid Hash Value%", NEWLINE);
return;
}
case "MultiPV";
num, tf := parse_int(*right);
if !tf {
print("info string error. Invalid MultiPV Value%", NEWLINE);
return;
}
if num >= 1 && num <= 100 {
set_multi_pv(num);
} else {
print("info string error. Invalid MultiPV Value%", NEWLINE);
return;
}
case "Threads";
num, tf := parse_int(*right);
if !tf {
print("info string error. Invalid Thread Value%", NEWLINE);
return;
}
if num >= 1 && num <= 512 {
set_threads(num);
} else {
print("info string error. Invalid Thread Value%", NEWLINE);
return;
}
case "Difficulty";
num, tf := parse_int(*right);
if !tf {
print("info string error. Invalid Difficulty Value%", NEWLINE);
return;
}
if num >= 1 && num <= 8 {
set_difficulty(num);
} else {
print("info string error. Invalid Difficulty Value%", NEWLINE);
return;
}
case;
print("info string error. invalid name: [%1]%2", left, NEWLINE);
}
}
// decides what the movetime is given the time/increment/other parameters in milliseconds
time_management :: (time: int, incr: int, movestogo: int, ply: int) -> movetime: int {
div := 0;
if movestogo != -1 then {
div = movestogo;
} else {
div = max(60 - ply, 20);
}
if incr > time then
incr = 0;
time /= div;
time -= 75;
// time up
if time < 0 {
time = 0;
incr -= 75;
if incr < 0 then {
incr = 1;
}
}
return time + incr;
}
chess: ChessGame #align 64;