summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--web/dartboat_wasm.c84
1 files changed, 29 insertions, 55 deletions
diff --git a/web/dartboat_wasm.c b/web/dartboat_wasm.c
index afff868..936af7d 100644
--- a/web/dartboat_wasm.c
+++ b/web/dartboat_wasm.c
@@ -32,6 +32,15 @@ void set_active_player(int player)
state->active_p = player;
state->active_l = player == 1 ? state->l1 : state->l2;
EM_ASM({setPlayerActive($0)}, player);
+
+ if (state->mode == M_PVC && player == 2) {
+ EM_ASM({promptMsgL($0)}, "Bot is throwing…");
+ EM_ASM({setPromptHandler($0)}, "");
+ } else {
+ EM_ASM({promptMsgL($0)}, "Enter points:");
+ EM_ASM({setPromptHandler($0)}, "visit");
+ }
+ EM_ASM({promptMsgR($0)}, "");
}
void toggle_active_player()
@@ -39,18 +48,6 @@ void toggle_active_player()
set_active_player(3 - state->active_p);
}
-void set_user_active()
-{
- set_active_player(1);
- EM_ASM({setPromptHandler($0)}, "visit");
-}
-
-void set_boat_active()
-{
- set_active_player(2);
- EM_ASM({setPromptHandler()}, "");
-}
-
void update_player_sugg(int player, int rem)
{
char str[15] = {0}, *p = str;
@@ -166,11 +163,6 @@ EMSCRIPTEN_KEEPALIVE void end_boat_throwing(int rem, double avg)
EMSCRIPTEN_KEEPALIVE void boat_visit()
{
- if (state->l1->rem <= 0 || state->l2->rem <= 0)
- return;
-
- EM_ASM({promptMsgL($0)}, "Bot is throwing…");
-
struct leg *l = state->l2;
if (state->undone_count) {
--state->undone_count;
@@ -180,12 +172,9 @@ EMSCRIPTEN_KEEPALIVE void boat_visit()
}
struct visit *v = l->visits + l->n_visits - 1;
-
- double avg;
- if (v->rem > 0)
- avg = (double)(l->start - l->rem) / l->n_visits;
- else
- avg = ((double)l->start / (((l->n_visits - 1) * 3) + v->n_darts)) * 3;
+ 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_throwing(l->rem, avg);
@@ -193,14 +182,14 @@ EMSCRIPTEN_KEEPALIVE void boat_visit()
}
int pts = 0;
- char str[20] = {0};
+ char str[15] = {0};
for (int i = 0; i < v->n_darts; ++i) {
pts += segment_points(v->darts[i]);
char *seg_name = segment_name(v->darts[i]);
- if (i) strcat(str, "-");
- strcat(str, seg_name);
+ strcat(i ? strcat(str, "-") : str, seg_name);
free(seg_name);
+
EM_ASM({scheduleCCall($0, $1, $2, $3, $4, $5)},
"draw_boat_throwing", delay_ms * (i+1),
"number", pts, "string", str);
@@ -222,19 +211,11 @@ void post_visit_checks()
EM_ASM({promptMsgL($0)}, "Darts needed?");
}
} else {
- if (state->mode == M_PVC) {
- if (state->active_p == 1) {
- set_boat_active();
- boat_visit();
- } else {
- set_user_active();
- EM_ASM({promptMsgL($0)}, "Enter points:");
- }
- } else if (state->mode == M_PVP) {
+ if (state->mode != M_P) {
toggle_active_player();
- EM_ASM({promptMsgL($0)}, "Enter points:");
- } else {
- EM_ASM({promptMsgL($0)}, "Enter points:");
+
+ if (state->mode == M_PVC && state->active_p == 2)
+ boat_visit();
}
}
}
@@ -280,38 +261,35 @@ EMSCRIPTEN_KEEPALIVE void user_undo()
return;
}
- if (state->mode == M_PVP) {
+ if (state->mode == M_PVC) {
+ set_active_player(1);
+ } else if (state->mode == M_PVP) {
if (is_match_over())
- EM_ASM({setPlayerActive($0)}, state->active_p);
+ set_active_player(state->active_p);
else
toggle_active_player();
}
- if (state->mode == M_PVC)
- set_user_active();
-
struct leg *l = state->active_l;
struct visit *v = l->visits + --l->n_visits;
l->rem += v->points;
+ memcpy(v, 0, sizeof(*v));
+
if (state->mode == M_PVC && state->l2->n_visits > l->n_visits) {
state->l2->rem += state->l2->visits[--state->l2->n_visits].points;
++state->undone_count;
}
EM_ASM({updatePlayerAvg($0, $1)}, 1,
- l->n_visits ? (double)(l->start-l->rem)/l->n_visits : 0);
+ l->n_visits ? (double)(l->start - l->rem) / l->n_visits : 0);
EM_ASM({updatePlayerAvg($0, $1)}, 2,
state->l2->n_visits ?
- (double)(state->l2->start-state->l2->rem)/state->l2->n_visits : 0);
+ (double)(state->l2->start - state->l2->rem) / state->l2->n_visits :
+ 0);
update_player_rem(state->active_p, l->rem);
if (state->mode == M_PVC)
update_player_rem(2, state->l2->rem);
- memcpy(v, 0, sizeof(*v));
-
- EM_ASM({promptMsgR($0)}, "");
- EM_ASM({setPromptHandler($0)}, "visit");
- EM_ASM({promptMsgL($0)}, "Enter points:");
draw_visits();
}
@@ -351,9 +329,7 @@ EMSCRIPTEN_KEEPALIVE void start_match(int mode)
state->mode = mode;
state->l1 = leg_init(501, mode == M_PVP ? "Player 1" : "User");
state->l2 = leg_init(501, mode == M_PVC ? "Bot" : "Player 2");
- state->active_p = 1;
- state->active_l = state->l1;
- set_user_active();
+ set_active_player(1);
EM_ASM({showPlayerInfo($0)}, 1);
EM_ASM({updatePlayerName($0, $1)}, 1, state->l1->name);
@@ -367,8 +343,6 @@ EMSCRIPTEN_KEEPALIVE void start_match(int mode)
EM_ASM({updatePlayerAvg($0, $1)}, 2, 0);
}
- EM_ASM({promptMsgL($0)}, "Enter points:");
- EM_ASM({promptMsgR($0)}, "");
draw_visits();
}