#include "checkouts.h" #include "comp.h" #include #include #include #include #include int delay_ms = 500; enum match_mode { M_PVC = 1, M_P, M_PVP }; // TODO refactor *everything* struct match_state { enum match_mode mode; struct leg *l1, *l2, *active_l; int active_p; int undone_count; }; struct match_state *state; 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() { set_active_player(3 - state->active_p); } void update_player_sugg(int player, 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)}, player, str); } bool is_match_over() { return state->l1->rem <= 0 || state->l2->rem <= 0; } void prompt_match_over() { EM_ASM(setPlayerActive()); // sets all inactive EM_ASM({setPromptHandler($0)}, "match_over"); EM_ASM({promptMsgR($0)}, "Press OK to end match."); } 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->l1->start); sprintf(p2_rem, "%d", state->l2->start); EM_ASM({drawVisit($0, $1, $2, $3, $4, $5)}, "0", "", p1_rem, "", state->mode == M_P ? "" : p2_rem, ""); int n_visits = state->l1->n_visits > state->l2->n_visits ? state->l1->n_visits : state->l2->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->l1->visits + i; sprintf(p1_pts, "%d", v->points); sprintf(p1_rem, "%d", v->rem); if (i < state->l2->n_visits) { v = state->l2->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 player, int rem) { char str[5]; if (rem < 0 || rem == 1) strcpy(str, "BUST"); else sprintf(str, "%d", rem); EM_ASM({updatePlayerRem($0, $1)}, player, str); if (player == 1 || state->mode != M_PVC) update_player_sugg(player, rem); } EMSCRIPTEN_KEEPALIVE void update_user_rem_from_pts(int pts) { update_player_rem(state->active_p, state->active_l->rem - pts); } EMSCRIPTEN_KEEPALIVE void draw_boat_throwing(int pts, char *str) { char pts_str[10]; sprintf(pts_str, "%d", pts); int rem = state->l2->n_visits > 1 ? state->l2->visits[state->l2->n_visits-2].rem : state->l2->start; update_player_rem(2, rem - pts); EM_ASM({setPromptInput($0)}, pts_str); EM_ASM({promptMsgR($0)}, str); } void post_visit_checks(); EMSCRIPTEN_KEEPALIVE void end_boat_throwing(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(); post_visit_checks(); } EMSCRIPTEN_KEEPALIVE void boat_visit() { struct leg *l = state->l2; if (state->undone_count) { --state->undone_count; 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_throwing(l->rem, avg); return; } int pts = 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]); 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); } EM_ASM({scheduleCCall($0, $1, $2, $3, $4, $5)}, "end_boat_throwing", delay_ms * (v->n_darts + 1), "number", l->rem, "number", avg); } void post_visit_checks() { if (is_match_over()) { if (state->mode == M_PVC && state->l2->rem <= 0) { EM_ASM({promptMsgL($0)}, "Bot wins. :("); prompt_match_over(); } else { EM_ASM({setPromptHandler($0)}, "num_darts"); EM_ASM({promptMsgL($0)}, "Darts needed?"); } } else { if (state->mode != M_P) { toggle_active_player(); if (state->mode == M_PVC && state->active_p == 2) boat_visit(); } } } EMSCRIPTEN_KEEPALIVE void user_visit(int points) { if (points < 0 || state->l1->rem <= 0 || state->l2->rem <= 0 || points > state->active_l->rem || state->active_l->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_l->rem - points == 0 && (points == 168 || points == 165 || points == 162 || points == 159))) { EM_ASM(oi()); return; } struct leg *l = state->active_l; struct visit *v = l->visits + l->n_visits++; v->points = points; l->rem -= points; v->rem = l->rem; update_player_rem(state->active_p, l->rem); if (v->rem > 0) EM_ASM({updatePlayerAvg($0, $1)}, state->active_p, (double)(l->start-l->rem)/l->n_visits); draw_visits(); post_visit_checks(); } EMSCRIPTEN_KEEPALIVE void user_visit_to_rem(int rem) { user_visit(state->l1->rem - rem); } EMSCRIPTEN_KEEPALIVE void user_undo() { if (!state->l1->n_visits) { EM_ASM(oi()); 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_p); else toggle_active_player(); } 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); EM_ASM({updatePlayerAvg($0, $1)}, 2, state->l2->n_visits ? (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); draw_visits(); } EMSCRIPTEN_KEEPALIVE void user_num_darts(int n) { if (n < 1 || n > 3) { EM_ASM(oi()); return; } struct leg *l = state->active_l; EM_ASM({updatePlayerAvg($0, $1)}, state->active_p, ((double)l->start / (((l->n_visits - 1) * 3) + n)) * 3); EM_ASM({promptMsgL($0)}, "You win! :)"); prompt_match_over(); } void free_state() { state->l2->n_visits += state->undone_count; // avoid memory leak leg_free(state->l1); leg_free(state->l2); free(state); state = NULL; } EMSCRIPTEN_KEEPALIVE void start_match(int mode) { if (mode != M_PVP && mode != M_PVC && mode != M_P) { EM_ASM(oi()); return; } if (state) free_state(); state = calloc(1, sizeof(*state)); 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"); set_active_player(1); EM_ASM({showPlayerInfo($0)}, 1); EM_ASM({updatePlayerName($0, $1)}, 1, state->l1->name); update_player_rem(1, state->l1->rem); EM_ASM({updatePlayerAvg($0, $1)}, 1, 0); if (mode != M_P) { EM_ASM({showPlayerInfo($0)}, 2); EM_ASM({updatePlayerName($0, $1)}, 2, state->l2->name); update_player_rem(2, state->l2->rem); EM_ASM({updatePlayerAvg($0, $1)}, 2, 0); } draw_visits(); } 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); } EMSCRIPTEN_KEEPALIVE void prompt_match_mode() { for (int pn = 1; pn < 3; ++pn) { EM_ASM({hidePlayerInfo($0)}, pn); clear_player_info(pn); } EM_ASM(clearVisits()); EM_ASM({setPromptHandler($0)}, "init"); EM_ASM({promptMsgR($0)}, ""); EM_ASM({promptMsgL($0)}, "Select match mode:"); } EMSCRIPTEN_KEEPALIVE void set_delay(int delay) { delay_ms = delay; } EMSCRIPTEN_KEEPALIVE void set_stdev(float hstdev, float vstdev) { horizontal_stdev = hstdev; vertical_stdev = vstdev; } void init_boat() { srand(time(NULL)); init_board(); EM_ASM(readOpts()); EM_ASM({updateDelay($0)}, delay_ms); EM_ASM({updateStdev($0)}, horizontal_stdev); } int main() { init_boat(); prompt_match_mode(); }