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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
|
#include "checkouts.h"
#include "comp.h"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <emscripten/emscripten.h>
int delay_ms = 500;
enum prompt_mode {
PM_NONE,
PM_VISIT,
PM_NUM_DARTS,
PM_END_MATCH,
PM_SELECT_MODE
};
enum prompt_mode pm;
void set_prompt_mode(enum prompt_mode mode)
{
pm = mode;
if (pm != PM_NONE)
EM_ASM(setPromptActive());
else
EM_ASM(setPromptInactive());
EM_ASM({setKeypad($0)}, pm == PM_SELECT_MODE ? "select_mode" : "default");
}
enum match_mode {
M_FIRST = 1,
M_PVC = M_FIRST,
M_P,
M_PVP,
M_LAST = M_PVP
};
struct match_state {
enum match_mode mode;
struct leg *legs[2];
struct leg *active_leg;
int active_player;
int boat_undone;
int num_darts;
};
struct match_state *state;
void set_active_player(int pn)
{
state->active_player = pn;
state->active_leg = state->legs[pn-1];
EM_ASM({setPlayerActive($0)}, pn);
if (state->mode == M_PVC && pn == 2) {
EM_ASM({promptMsgL($0)}, "Bot is throwing…");
EM_ASM({setKeyLabel($0, $1)}, "submit", "OK");
EM_ASM({setKeyLabel($0, $1)}, "rem", "REMAINING");
set_prompt_mode(PM_NONE);
} else {
EM_ASM({promptMsgL($0)}, "Enter points:");
EM_ASM({setKeyLabel($0, $1)}, "submit", "OK");
EM_ASM({setKeyLabel($0, $1)}, "rem", "REMAINING");
set_prompt_mode(PM_VISIT);
}
EM_ASM({promptMsgR($0)}, "");
}
void toggle_active_player()
{
set_active_player(3 - state->active_player);
}
void update_player_sugg(int pn, int rem)
{
char str[15] = {0}, *p = str;
if (rem > 1 && rem <= 170) {
char *target;
int i = 3, trem = rem;
while (i && trem && (target = CHECKOUTS[--i][trem-1])) {
p += sprintf(p, i == 2 ? "%s" : "-%s", target);
trem -= segment_points(segment_from_name(target));
}
}
EM_ASM({updatePlayerSugg($0, $1)}, pn, str);
}
bool is_match_over()
{
return state->legs[0]->rem <= 0 || state->legs[1]->rem <= 0;
}
void draw_visits()
{
EM_ASM({clearVisits()});
char visit_no[10], p1_pts[10], p1_rem[10],
p2_pts[10], p2_rem[10], p2_darts[100];
sprintf(p1_rem, "%d", state->legs[0]->start);
sprintf(p2_rem, "%d", state->legs[1]->start);
EM_ASM({drawVisit($0, $1, $2, $3, $4, $5)},
"0", "", p1_rem, "", state->mode == M_P ? "" : p2_rem, "");
int n_visits = state->legs[0]->n_visits > state->legs[1]->n_visits ?
state->legs[0]->n_visits : state->legs[1]->n_visits;
for (int i = 0; i < n_visits; ++i) {
visit_no[0] = p1_pts[0] = p1_rem[0] =
p2_pts[0] = p2_rem[0] = p2_darts[0] = 0;
sprintf(visit_no, "%d", i + 1);
struct visit *v = state->legs[0]->visits + i;
sprintf(p1_pts, "%d", v->points);
sprintf(p1_rem, "%d", v->rem);
if (i < state->legs[1]->n_visits) {
v = state->legs[1]->visits + i;
sprintf(p2_pts, "%d", v->points);
sprintf(p2_rem, "%d", v->rem);
for (int j = 0; j < v->n_darts; ++j) {
char *n = segment_name(v->darts[j]);
sprintf(p2_darts + (j ? (j*5 - 1) : 0),
j == 0 ? "%4s" : " %4s", n);
free(n);
}
}
EM_ASM({drawVisit($0, $1, $2, $3, $4, $5)},
visit_no, p1_pts, p1_rem, p2_pts, p2_rem, p2_darts);
}
}
void update_player_rem(int pn, int rem)
{
char str[5];
if (rem < 0 || rem == 1)
strcpy(str, "BUST");
else
sprintf(str, "%d", rem);
EM_ASM({updatePlayerRem($0, $1)}, pn, str);
if (pn == 1 || state->mode != M_PVC)
update_player_sugg(pn, rem);
}
EMSCRIPTEN_KEEPALIVE void update_user_rem_from_pts(int pts)
{
update_player_rem(state->active_player, state->active_leg->rem - pts);
}
EMSCRIPTEN_KEEPALIVE void draw_boat_throwing(int pts, char *str)
{
char pts_str[10];
sprintf(pts_str, "%d", pts);
int rem = state->legs[1]->n_visits > 1 ?
state->legs[1]->visits[state->legs[1]->n_visits-2].rem :
state->legs[1]->start;
update_player_rem(2, rem - pts);
EM_ASM({setPromptInput($0)}, pts_str);
EM_ASM({promptMsgR($0)}, str);
free(str);
}
void handle_next();
EMSCRIPTEN_KEEPALIVE void end_boat_visit(int rem, double avg)
{
update_player_rem(2, rem);
EM_ASM({updatePlayerAvg($0, $1)}, 2, avg);
EM_ASM({setPromptInput($0)}, "");
EM_ASM({promptMsgR($0)}, "");
draw_visits();
handle_next();
}
EMSCRIPTEN_KEEPALIVE void boat_visit()
{
struct leg *l = state->legs[1];
if (state->boat_undone) {
--state->boat_undone;
l->rem = l->visits[l->n_visits++].rem;
} else {
comp_visit(l);
}
struct visit *v = l->visits + l->n_visits - 1;
double avg = v->rem > 0 ?
(double)(l->start - l->rem) / l->n_visits :
(double)l->start / (((l->n_visits - 1) * 3) + v->n_darts) * 3;
if (!delay_ms) {
end_boat_visit(l->rem, avg);
return;
}
int pts = 0;
char str[15] = {0};
size_t len_str = 0;
for (int i = 0; i < v->n_darts; ++i) {
pts += segment_points(v->darts[i]);
char *seg_name = segment_name(v->darts[i]);
len_str += sprintf(str + len_str, i ? "-%s" : "%s", seg_name);
free(seg_name);
char *tmp = malloc(len_str + 1); // free in draw_boat_throwing
memcpy(tmp, str, len_str + 1);
EM_ASM({scheduleCCall($0, $1, $2, $3)}, "draw_boat_throwing",
delay_ms * (i+1), pts, tmp);
}
EM_ASM({scheduleCCall($0, $1, $2, $3)}, "end_boat_visit",
delay_ms * (v->n_darts + 1), l->rem, avg);
}
void clear_player_info(int pn)
{
EM_ASM({updatePlayerName($0, $1)}, pn, "");
EM_ASM({updatePlayerRem($0, $1)}, pn, "");
EM_ASM({updatePlayerSugg($0, $1)}, pn, "");
EM_ASM({updatePlayerAvg($0, $1)}, pn, 0);
}
void prompt_num_darts()
{
set_prompt_mode(PM_NUM_DARTS);
EM_ASM({promptMsgL($0)}, "Darts needed?");
EM_ASM({promptMsgR($0)}, "");
EM_ASM({setKeyLabel($0, $1)}, "submit", "OK");
EM_ASM({setKeyLabel($0, $1)}, "rem", "REMAINING");
}
void prompt_end_match()
{
EM_ASM(setPlayerActive()); // sets all inactive
set_prompt_mode(PM_END_MATCH);
EM_ASM({promptMsgL($0)},
state->mode == M_PVC && state->legs[1]->rem <= 0 ? "Bot wins. :(" :
"You win! :)");
EM_ASM({promptMsgR($0)}, "");
EM_ASM({setKeyLabel($0, $1)}, "submit", "END MATCH");
EM_ASM({setKeyLabel($0, $1)}, "rem", "REMATCH");
}
void prompt_select_mode()
{
for (int pn = 1; pn < 3; ++pn) {
EM_ASM({hidePlayerInfo($0)}, pn);
clear_player_info(pn);
}
EM_ASM(clearVisits());
set_prompt_mode(PM_SELECT_MODE);
EM_ASM({promptMsgL($0)}, "Select match mode:");
EM_ASM({promptMsgR($0)}, "");
}
void handle_next()
{
if (!state) {
prompt_select_mode();
} else if (is_match_over()) {
if (state->num_darts ||
(state->mode == M_PVC && state->legs[1]->rem <= 0))
prompt_end_match();
else
prompt_num_darts();
} else {
if (state->mode == M_P) {
set_active_player(1);
} else {
if (state->active_player)
toggle_active_player();
else
set_active_player(1);
if (state->mode == M_PVC && state->active_player == 2)
boat_visit();
}
}
}
void update_player_avg(int pn, int n_darts)
{
struct leg *l = state->legs[pn-1];
EM_ASM({updatePlayerAvg($0, $1)}, pn,
l->n_visits ?
(l->rem > 0 ? ((double)(l->start - l->rem) / l->n_visits) :
((double)l->start / (((l->n_visits - 1) * 3) + n_darts) * 3)) :
0);
}
EMSCRIPTEN_KEEPALIVE void user_visit(int points)
{
if (points < 0 || points > state->active_leg->rem ||
state->active_leg->rem - points == 1 ||
points > 180 || points == 179 || points == 178 || points == 176 ||
points == 175 || points == 173 || points == 172 || points == 169 ||
points == 166 || points == 163 ||
(state->active_leg->rem - points == 0 &&
(points > 170 || points == 168 || points == 165 || points == 162 ||
points == 159))) {
EM_ASM(oi());
return;
}
struct leg *l = state->active_leg;
struct visit *v = l->visits + l->n_visits++;
v->points = points;
l->rem -= points;
v->rem = l->rem;
update_player_rem(state->active_player, l->rem);
if (v->rem > 0)
update_player_avg(state->active_player, 0);
draw_visits();
handle_next();
}
EMSCRIPTEN_KEEPALIVE void user_visit_to_rem(int rem)
{
user_visit(state->legs[0]->rem - rem);
}
EMSCRIPTEN_KEEPALIVE void user_undo()
{
if (!state->legs[0]->n_visits) {
EM_ASM(oi());
return;
}
if (state->num_darts) {
state->num_darts = 0;
EM_ASM({updatePlayerAvg($0, $1)}, state->active_player,
((double)(state->active_leg->start -
state->active_leg->visits[
state->active_leg->n_visits-2].rem) /
(state->active_leg->n_visits-1)));
handle_next();
return;
}
if (state->mode == M_PVC) {
set_active_player(1);
} else if (state->mode == M_PVP) {
if (is_match_over())
set_active_player(state->active_player);
else
toggle_active_player();
}
struct leg *l = state->active_leg;
struct visit *v = l->visits + --l->n_visits;
l->rem += v->points;
memcpy(v, 0, sizeof(*v));
if (state->mode == M_PVC && state->legs[1]->n_visits > l->n_visits) {
state->legs[1]->rem +=
state->legs[1]->visits[--state->legs[1]->n_visits].points;
++state->boat_undone;
}
update_player_avg(state->active_player, 0);
update_player_rem(state->active_player, l->rem);
if (state->mode == M_PVC) {
update_player_avg(2, 0);
update_player_rem(2, state->legs[1]->rem);
}
draw_visits();
if (state->mode != M_P) // FIXME avoid double toggle?
toggle_active_player();
handle_next();
}
EMSCRIPTEN_KEEPALIVE void user_num_darts(int n)
{
if (n < 1 || n > 3) {
EM_ASM(oi());
return;
}
state->num_darts = n;
update_player_avg(state->active_player, n);
handle_next();
}
void free_state()
{
state->legs[1]->n_visits += state->boat_undone; // avoid memory leak
leg_free(state->legs[0]);
leg_free(state->legs[1]);
free(state);
state = NULL;
}
EMSCRIPTEN_KEEPALIVE void start_match(int mode)
{
if (mode < M_FIRST || mode > M_LAST) {
EM_ASM(oi());
return;
}
if (state) free_state(); // rematch gets us here
state = calloc(1, sizeof(*state));
state->mode = mode;
state->legs[0] = leg_init(501, mode == M_PVP ? "Player 1" : "User");
state->legs[1] = leg_init(501, mode == M_PVC ? "Bot" : "Player 2");
EM_ASM({showPlayerInfo($0)}, 1);
EM_ASM({updatePlayerName($0, $1)}, 1, state->legs[0]->name);
update_player_rem(1, state->legs[0]->rem);
EM_ASM({updatePlayerAvg($0, $1)}, 1, 0);
if (mode != M_P) {
EM_ASM({showPlayerInfo($0)}, 2);
EM_ASM({updatePlayerName($0, $1)}, 2, state->legs[1]->name);
update_player_rem(2, state->legs[1]->rem);
EM_ASM({updatePlayerAvg($0, $1)}, 2, 0);
}
draw_visits();
handle_next();
}
EMSCRIPTEN_KEEPALIVE void end_match()
{
if (state) free_state();
handle_next();
}
EMSCRIPTEN_KEEPALIVE void set_delay(int delay)
{
delay_ms = delay;
}
EMSCRIPTEN_KEEPALIVE void set_stdev(float stdev)
{
if (!isnan(stdev))
horizontal_stdev = vertical_stdev = stdev;
}
void init_boat()
{
srand(time(NULL));
init_board();
EM_ASM(readOpts());
EM_ASM({updateDelay($0)}, delay_ms);
EM_ASM({updateStdev($0)}, horizontal_stdev);
}
bool is_key_active(char *k)
{
return (EM_ASM_INT({return isKeyActive($0)}, k));
}
void toggle_key(char *k)
{
EM_ASM({toggleKey($0)}, k);
}
void deactivate_key(char *k)
{
EM_ASM({deactivateKey($0)}, k);
}
char *prompt_get()
{
return (char *)EM_ASM_INT({return promptGet()});
}
void prompt_handle_pre(char *command)
{
if (pm == PM_NONE)
return;
EM_ASM(clearOi());
if ((pm == PM_VISIT || pm == PM_NUM_DARTS || pm == PM_END_MATCH) &&
strcmp(command, "undo"))
deactivate_key("undo");
if (pm == PM_END_MATCH && strcmp(command, "rem"))
deactivate_key("rem");
if (pm == PM_END_MATCH && strcmp(command, "submit"))
deactivate_key("submit");
}
void prompt_handle_on_change()
{
if (pm != PM_VISIT)
return;
char *str = prompt_get();
update_user_rem_from_pts(atoi(str));
free(str);
}
void prompt_handle_append(char *data)
{
if (pm != PM_SELECT_MODE && pm != PM_VISIT && pm != PM_NUM_DARTS)
return;
char *str = prompt_get();
size_t len_str = strlen(str);
size_t len_data = strlen(data);
if (len_str < 3) {
str = realloc(str, len_str + len_data + 1);
memcpy(str + len_str, data, len_data + 1);
EM_ASM({setPromptInput($0)}, str);
prompt_handle_on_change();
}
free(str);
}
void prompt_handle_backspace()
{
if (pm == PM_NONE)
return;
char *str = prompt_get();
size_t len_str = strlen(str);
if (len_str > 0) {
str[len_str-1] = 0;
EM_ASM({setPromptInput($0)}, str);
prompt_handle_on_change();
}
free(str);
}
void prompt_handle_clear()
{
if (pm == PM_NONE)
return;
EM_ASM({setPromptInput($0)}, "");
prompt_handle_on_change();
}
void prompt_handle_submit()
{
if (pm == PM_END_MATCH) {
if (is_key_active("submit"))
end_match();
toggle_key("submit");
return;
}
if (pm != PM_VISIT && pm != PM_NUM_DARTS && pm != PM_SELECT_MODE)
return;
char *str = prompt_get();
prompt_handle_clear();
if (*str) {
if (pm == PM_VISIT)
user_visit(atoi(str));
else if (pm == PM_NUM_DARTS)
user_num_darts(atoi(str));
else if (pm == PM_SELECT_MODE)
start_match(atoi(str));
}
free(str);
}
void prompt_handle_rem()
{
if (pm == PM_END_MATCH) {
if (is_key_active("rem"))
start_match(state->mode);
toggle_key("rem");
return;
}
if (pm != PM_VISIT)
return;
char *str = prompt_get();
prompt_handle_clear();
if (*str)
user_visit_to_rem(atoi(str));
free(str);
}
void prompt_handle_undo()
{
if (pm != PM_VISIT && pm != PM_NUM_DARTS && pm != PM_END_MATCH)
return;
prompt_handle_clear();
if (is_key_active("undo"))
user_undo();
toggle_key("undo");
}
EMSCRIPTEN_KEEPALIVE void prompt_handle(char *command, char *data)
{
prompt_handle_pre(command);
if (!strcmp(command, "append"))
prompt_handle_append(data);
else if (!strcmp(command, "backspace"))
prompt_handle_backspace();
else if (!strcmp(command, "clear"))
prompt_handle_clear();
else if (!strcmp(command, "submit"))
prompt_handle_submit();
else if (!strcmp(command, "rem"))
prompt_handle_rem();
else if (!strcmp(command, "undo"))
prompt_handle_undo();
}
EMSCRIPTEN_KEEPALIVE void init()
{
init_boat();
set_prompt_mode(PM_VISIT);
handle_next();
}
int main()
{
EM_ASM(boatReady());
}
|