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
|
#include "web_control.h"
#include "web_dom.h"
#include "web_match.h"
#include "web_opts.h"
#include "web_prompt.h"
#include "web_scoreboard.h"
#include "web_svg.h"
#include "comp.h"
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <emscripten/emscripten.h>
static int curr_match_id = 0;
void set_active_player(int pn)
{
state->active_player = pn;
scoreboard_set_player_active(pn);
if (state->mode == M_PVC && pn == 2)
prompt_bot_visit();
else
prompt_visit();
}
void toggle_active_player()
{
set_active_player(3 - state->active_player);
}
void update_user_rem_from_pts(int pts)
{
update_player_rem(state->active_player, state_active_leg()->rem - pts);
scoreboard_flush_player_info(state->active_player);
}
EMSCRIPTEN_KEEPALIVE
void end_boat_visit(int rem, double avg, int match_id)
{
if (!state || match_id != curr_match_id)
return;
svg_clear_points();
update_player_rem(2, rem);
scoreboard_set_player_avg(2, avg);
prompt_set_input(NULL);
prompt_set_msgr(NULL);
draw_visits();
handle_next();
}
EMSCRIPTEN_KEEPALIVE
void draw_boat_throwing(int pts, char *str, double x, double y, int match_id)
{
if (!state || match_id != curr_match_id)
return;
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;
svg_draw_point(x, y);
update_player_rem(2, rem - pts);
prompt_set_input(pts_str);
prompt_set_msgr(str);
free(str);
prompt_flush();
scoreboard_flush_player_info(2);
}
static void schedule_boat_visit_draws(struct leg *l, struct visit *v,
double avg)
{
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);
struct ccoords c = v->ccoords[i];
int delay = delay_ms * (i + 1);
if (delay_ms >= SVG_THROW_ANIM_MS)
delay -= SVG_THROW_ANIM_MS;
char *tmp = malloc(len_str + 1); // free in draw_boat_throwing
memcpy(tmp, str, len_str + 1);
EM_ASM({scheduleCCall($0, $1, $2, $3, $4, $5, $6)},
"draw_boat_throwing", delay,
pts, tmp, c.x, c.y, curr_match_id);
}
EM_ASM({scheduleCCall($0, $1, $2, $3, $4)}, "end_boat_visit",
delay_ms * (v->n_darts + 1), l->rem, avg, curr_match_id);
}
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)
schedule_boat_visit_draws(l, v, avg);
else
end_boat_visit(l->rem, avg, curr_match_id);
}
void handle_next()
{
if (!state) {
prompt_select_mode();
} else if (match_is_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(match_opts->throws_first);
if (state->mode == M_PVC && state->active_player == 2)
boat_visit();
}
}
prompt_flush();
scoreboard_flush();
}
void user_visit(int points)
{
if (!is_points_valid(points, state_active_leg()->rem)) {
oi();
return;
}
struct leg *l = state_active_leg();
if (l->n_visits == l->size_visits)
leg_grow_visits(l);
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();
}
void user_visit_to_rem(int rem)
{
user_visit(state->legs[0]->rem - rem);
}
static int num_players()
{
if (state->mode == M_P)
return 1;
return 2;
}
static int prev_throw_player()
{
if (match_is_over())
return state->active_player;
if (state->active_player == 1)
return num_players();
else
return state->active_player - 1;
}
static bool player_is_comp(int pn)
{
return state->mode == M_PVC && pn == 2;
}
static void undo_active()
{
struct leg *l = state_active_leg();
if (player_is_comp(state->active_player)) {
l->rem += l->visits[--l->n_visits].points;
++state->boat_undone;
} else {
struct visit *v = l->visits + --l->n_visits;
l->rem += v->points;
memset(v, 0, sizeof(*v));
}
update_player_avg(state->active_player, 0);
update_player_rem(state->active_player, l->rem);
state->active_player = prev_throw_player();
}
static bool first_user_has_thrown() {
for (int i = 0, np = num_players(); i < np; ++i) {
int pn = match_opts->throws_first + i;
if (pn > np)
pn -= np;
if (!player_is_comp(pn))
return !!state->legs[pn - 1]->n_visits;
}
return false;
}
void user_undo()
{
if (!first_user_has_thrown()) {
oi();
return;
}
if (state->num_darts) {
state->num_darts = 0;
struct leg *l = state_active_leg();
scoreboard_set_player_avg(state->active_player,
((double)(l->start - l->visits[l->n_visits-2].rem) /
(l->n_visits - 1)));
scoreboard_set_player_active(state->active_player);
handle_next();
return;
}
state->active_player = prev_throw_player();
while (player_is_comp(state->active_player))
undo_active();
undo_active();
draw_visits();
handle_next();
}
void user_num_darts(int n)
{
if (n < 1 || n > 3) {
oi();
return;
}
state->num_darts = n;
update_player_avg(state->active_player, n);
handle_next();
}
void match_mode_selected(int mode)
{
if (mode < M_FIRST || mode > M_LAST) {
oi();
return;
}
if (match_opts) match_opts_free();
match_opts = calloc(1, sizeof(*match_opts));
match_opts->mode = mode;
match_opts->start_pts = 501;
match_opts->throws_first = 1;
// names need to be freed if we stop using string literals
if (mode == M_PVC) {
match_opts->p1_name = "User";
match_opts->p2_name = "Computer";
} else if (mode == M_P) {
match_opts->p1_name = "Player 1";
match_opts->p2_name = NULL;
} else if (mode == M_PVP) {
match_opts->p1_name = "Player 1";
match_opts->p2_name = "Player 2";
}
prompt_match_opts();
prompt_flush();
}
void start_match()
{
if (state) free_state(); // rematch gets us here
++curr_match_id;
state = calloc(1, sizeof(*state));
state->mode = match_opts->mode;
state->legs[0] = leg_init(match_opts->start_pts, match_opts->p1_name);
state->legs[1] = leg_init(match_opts->start_pts,
match_opts->p2_name ? match_opts->p2_name : "oi");
show_player_info(1);
update_player_name(1, state->legs[0]->name);
update_player_rem(1, state->legs[0]->rem);
update_player_avg(1, 0);
if (state->mode != M_P) {
show_player_info(2);
update_player_name(2, state->legs[1]->name);
update_player_rem(2, state->legs[1]->rem);
update_player_avg(2, 0);
}
dom_enable_exit_dialogue(true);
dom_add_class("#key-exit", "visible");
draw_visits();
handle_next();
}
void end_match()
{
// clean up in case match was ended early
svg_clear_points();
prompt_set_input(NULL);
scoreboard_set_player_active(-1);
dom_remove_class("#key-exit", "visible");
dom_enable_exit_dialogue(false);
if (state) free_state();
handle_next();
}
|