diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-25 19:53:22 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-25 19:53:22 -0700 |
commit | 2652dec6b38f04f96a9832622418d01805bb7d4a (patch) | |
tree | dd387f144f17c0a509867fd28cce8fe421973674 | |
parent | 0517dcffe7ac66b604143d9457ba6591d34ae07e (diff) | |
download | dartboat-2652dec6b38f04f96a9832622418d01805bb7d4a.tar.gz dartboat-2652dec6b38f04f96a9832622418d01805bb7d4a.tar.xz |
minor cleanup and organisation of lib code
-rw-r--r-- | board.c | 119 | ||||
-rw-r--r-- | board.h | 21 | ||||
-rw-r--r-- | checkouts.c | 4 | ||||
-rw-r--r-- | checkouts.h | 7 | ||||
-rw-r--r-- | comp.c | 51 | ||||
-rw-r--r-- | comp.h | 1 | ||||
-rw-r--r-- | web/web_svg.c | 3 |
7 files changed, 102 insertions, 104 deletions
@@ -7,73 +7,101 @@ #include <stdlib.h> #include <string.h> -#define NUM_SECTORS (sizeof(SECTORS) / sizeof(*SECTORS)) -#define SECTOR_WIDTH (360.0 / NUM_SECTORS) -int SECTORS[] = { 20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, - 16, 8, 11, 14, 9, 12, 5 }; -int SECTOR_INDS[NUM_SECTORS]; -double SECTOR_ANGLES[NUM_SECTORS]; - +int SECTORS[] = { 20, 1, 18, 4, 13, 6, 10, 15, 2, 17, + 3, 19, 7, 16, 8, 11, 14, 9, 12, 5 }; +static int SECTOR_INDS[NUM_SECTORS]; +static double SECTOR_ANGLES[NUM_SECTORS]; static const char *SECTOR_NAMES[] = { FOREACH_SECTOR(GEN_SECTOR_STR) FOREACH_SECTOR(GEN_SECTOR_STR_D) FOREACH_SECTOR(GEN_SECTOR_STR_T) }; -static const char *RING_NAMES[] = { - FOREACH_RING(GEN_RING_STR) - "OUT" -}; double OUTER_DISTS[] = { FOREACH_RING(GEN_RING_OUTER_DIST) }; -double CENTRE_DISTS[sizeof(OUTER_DISTS) / sizeof(*OUTER_DISTS)]; +static double CENTRE_DISTS[sizeof(OUTER_DISTS) / sizeof(*OUTER_DISTS)]; +static const char *RING_NAMES[] = { + FOREACH_RING(GEN_RING_STR) + "OUT" +}; static void init_sectors() { for (size_t i = 0; i < NUM_SECTORS; ++i) { - SECTOR_INDS[SECTORS[i]-1] = i; + SECTOR_INDS[SECTORS[i] - 1] = i; double angle = 90 - (i * SECTOR_WIDTH); - if (angle < 0) angle += 360; + if (angle < 0) + angle += 360; SECTOR_ANGLES[i] = angle; } } -static void init_centre_dists() +static void init_rings() { - CENTRE_DISTS[0] = 0; + CENTRE_DISTS[R_BULL] = 0; for (int i = R_25; i < R_OUT; ++i) CENTRE_DISTS[i] = (OUTER_DISTS[i] - - ((OUTER_DISTS[i] - OUTER_DISTS[i-1]) / 2)); + ((OUTER_DISTS[i] - OUTER_DISTS[i - 1]) / 2)); } void init_board() { init_sectors(); - init_centre_dists(); + init_rings(); } -int get_sector(double angle) +static int sector_from_angle(double angle) { - double shifted = angle - 90 - (SECTOR_WIDTH/2); - return SECTORS[NUM_SECTORS - 1 - - (int)((shifted - (360 * floor(shifted/360))) / SECTOR_WIDTH)]; + double shifted = angle - 90 - (SECTOR_WIDTH / 2); + shifted -= 360 * floor(shifted / 360); + return SECTORS[NUM_SECTORS - 1 - (int)(shifted / SECTOR_WIDTH)]; } -enum ring get_ring(double radius) +static enum ring ring_from_radius(double radius) { - for (int i = R_BULL; i < R_OUT; ++i) + for (int i = R_BULL; i < R_OUT; ++i) { if (radius < OUTER_DISTS[i]) return i; + } return R_OUT; } -struct segment get_segment(struct pcoords c) +struct segment segment_from_pcoords(struct pcoords c) +{ + return (struct segment){ .sector = sector_from_angle(c.a), + .ring = ring_from_radius(c.r) }; +} + +struct segment segment_from_name(const char *name) +{ + struct segment seg = { .sector = 20 }; + + if (!strcmp(name, "BULL")) { + seg.ring = R_BULL; + } else if (!strcmp(name, "25")) { + seg.ring = R_25; + } else if (name[0] == 'T') { + seg.ring = R_TREBLE; + seg.sector = atoi(name + 1); + } else if (name[0] == 'D') { + seg.ring = R_DOUBLE; + seg.sector = atoi(name + 1); + } else { + seg.ring = R_BIG; + seg.sector = atoi(name); + } + + return seg; +} + +struct pcoords segment_centre(struct segment seg) { - return (struct segment){ .sector = get_sector(c.a), - .ring = get_ring(c.r) }; + return (struct pcoords){ + .a = SECTOR_ANGLES[SECTOR_INDS[seg.sector - 1]], + .r = CENTRE_DISTS[seg.ring] }; } const char *segment_name(struct segment seg) @@ -93,13 +121,13 @@ int segment_points(struct segment seg) { if (seg.ring == R_BULL) return 50; - else if (seg.ring == R_25) + if (seg.ring == R_25) return 25; - else if (seg.ring == R_TREBLE) + if (seg.ring == R_TREBLE) return 3 * seg.sector; - else if (seg.ring == R_DOUBLE) + if (seg.ring == R_DOUBLE) return 2 * seg.sector; - else if (seg.ring == R_SMALL || seg.ring == R_BIG) + if (seg.ring == R_SMALL || seg.ring == R_BIG) return seg.sector; return 0; } @@ -110,32 +138,3 @@ bool segment_is_double(struct segment seg) return true; return false; } - -struct pcoords segment_centre(struct segment seg) -{ - return (struct pcoords){ .a = SECTOR_ANGLES[SECTOR_INDS[seg.sector-1]], - .r = CENTRE_DISTS[seg.ring] }; -} - -struct segment segment_from_name(char *name) -{ - int s = 20; - enum ring r; - - if (!strcmp(name, "BULL")) { - r = R_BULL; - } else if (!strcmp(name, "25")) { - r = R_25; - } else if (name[0] == 'T') { - r = R_TREBLE; - s = atoi(name+1); - } else if (name[0] == 'D') { - r = R_DOUBLE; - s = atoi(name+1); - } else { - r = R_BIG; - s = atoi(name); - } - - return (struct segment){ .sector = s, .ring = r }; -} @@ -13,8 +13,8 @@ #define DOUBLE_INSIDE_WIDTH 8.0 #define TREBLE_INSIDE_WIDTH 8.0 -#define SEGMENT_MAX_LEN 5 - +#define NUM_SECTORS 20 +#define SECTOR_WIDTH (360.0 / NUM_SECTORS) #define FOREACH_SECTOR(M) \ M(1) M(2) M(3) M(4) M(5) M(6) M(7) M(8) M(9) M(10) \ M(11) M(12) M(13) M(14) M(15) M(16) M(17) M(18) M(19) M(20) @@ -36,6 +36,10 @@ #define GEN_RING_ENUM(X, D) R_ ## X, #define GEN_RING_STR(X, D) #X, #define GEN_RING_OUTER_DIST(X, D) D, + +extern int SECTORS[]; +extern double OUTER_DISTS[]; + enum ring { FOREACH_RING(GEN_RING_ENUM) R_OUT @@ -46,11 +50,6 @@ struct segment { int sector; }; -extern int SECTORS[]; -extern double OUTER_DISTS[]; - -void init_board(); - struct ccoords { double x, y; }; @@ -59,11 +58,13 @@ struct pcoords { double a, r; }; -struct segment get_segment(struct pcoords c); +void init_board(); + +struct segment segment_from_pcoords(struct pcoords c); +struct segment segment_from_name(const char *name); +struct pcoords segment_centre(struct segment seg); const char *segment_name(struct segment seg); int segment_points(struct segment seg); bool segment_is_double(struct segment seg); -struct pcoords segment_centre(struct segment seg); -struct segment segment_from_name(char *name); #endif diff --git a/checkouts.c b/checkouts.c index 0099aa5..d7da3ee 100644 --- a/checkouts.c +++ b/checkouts.c @@ -45,7 +45,7 @@ int checkouts_suggested_3_2(char *buf, int rem) return len; } -char *CHECKOUTS[][170] = { +char *CHECKOUTS[][NUM_CHECKOUTS] = { { NULL, // 1 "D1", // 2 @@ -564,7 +564,7 @@ char *CHECKOUTS[][170] = { } }; -char *SETUP_SHOTS[191] = { +char *SETUP_SHOTS[NUM_SETUP_SHOTS] = { NULL, // 1 "D1", // 2 "1", // 3 diff --git a/checkouts.h b/checkouts.h index c4ce8db..52b40d8 100644 --- a/checkouts.h +++ b/checkouts.h @@ -1,8 +1,11 @@ #ifndef CHECKOUTS_H #define CHECKOUTS_H -extern char *CHECKOUTS[3][170]; -extern char *SETUP_SHOTS[191]; +#define NUM_CHECKOUTS 170 +extern char *CHECKOUTS[][NUM_CHECKOUTS]; + +#define NUM_SETUP_SHOTS 191 +extern char *SETUP_SHOTS[NUM_SETUP_SHOTS]; int checkouts_suggested(char *buf, int rem, int darts_in_hand); int checkouts_suggested_3_2(char *buf, int rem); @@ -9,16 +9,15 @@ double horizontal_stdev = 16, vertical_stdev = 16; -double drand() +static double drand() { return (double)rand() / RAND_MAX; } -double gauss(double mean, double stdev) +static double gauss(double mean, double stdev) { static bool have_next; static double next; - double curr; if (have_next) { @@ -35,7 +34,7 @@ double gauss(double mean, double stdev) return mean + (curr * stdev); } -struct ccoords get_offset() +static struct ccoords get_offset() { return (struct ccoords){ .x = gauss(0, horizontal_stdev), .y = gauss(0, vertical_stdev) }; @@ -44,36 +43,33 @@ struct ccoords get_offset() struct ccoords pol_to_cart(struct pcoords c) { double t = c.a * (M_PI / 180); - double x = c.r * cos(t); - double y = c.r * sin(t); - - return (struct ccoords){ .x = x, .y = y }; + return (struct ccoords){ .x = c.r * cos(t), .y = c.r * sin(t) }; } -struct pcoords cart_to_pol(struct ccoords c) +static struct pcoords cart_to_pol(struct ccoords c) { - double a = atan2(c.y, c.x) * (180 / M_PI); - double r = sqrt(pow(c.x, 2) + pow(c.y, 2)); - - return (struct pcoords){ .a = a, .r = r }; + return (struct pcoords){ .a = atan2(c.y, c.x) * (180 / M_PI), + .r = sqrt(pow(c.x, 2) + pow(c.y, 2)) }; } -struct pcoords throw_dart(struct pcoords target) +static struct pcoords throw_dart(struct pcoords target, struct ccoords *cc) { - struct ccoords cc = pol_to_cart(target); + struct ccoords tc = pol_to_cart(target); struct ccoords offset = get_offset(); - return cart_to_pol((struct ccoords){ .x = cc.x + offset.x, - .y = cc.y + offset.y }); + cc->x = tc.x + offset.x; + cc->y = tc.y + offset.y; + + return cart_to_pol(*cc); } struct segment next_dart(int rem, int darts_in_hand) { char *c = NULL; - if (rem <= 170) - c = CHECKOUTS[darts_in_hand-1][rem-1]; - if (!c && rem <= 191) - c = SETUP_SHOTS[rem-1]; + if (rem <= NUM_CHECKOUTS) + c = CHECKOUTS[darts_in_hand - 1][rem - 1]; + if (!c && rem <= NUM_SETUP_SHOTS) + c = SETUP_SHOTS[rem - 1]; if (!c) c = "T20"; @@ -85,17 +81,18 @@ void comp_visit(struct leg *l) if (l->n_visits == l->size_visits) leg_grow_visits(l); struct visit *v = l->visits + l->n_visits++; - v->darts = calloc(3, sizeof(*(v->darts))); - v->ccoords = calloc(3, sizeof(*(v->ccoords))); + v->darts = calloc(3, sizeof(*v->darts)); + v->ccoords = calloc(3, sizeof(*v->ccoords)); for (int i = 0; i < 3; ++i) { struct segment ts = next_dart(l->rem - v->points, 3 - i); struct pcoords tc = segment_centre(ts); - struct pcoords dc = throw_dart(tc); - struct segment ds = get_segment(dc); - // FIXME double conversion - v->ccoords[v->n_darts] = pol_to_cart(dc); + struct ccoords dcart; + struct pcoords dc = throw_dart(tc, &dcart); + struct segment ds = segment_from_pcoords(dc); + + v->ccoords[v->n_darts] = dcart; v->darts[v->n_darts++] = ds; v->points += segment_points(ds); @@ -6,6 +6,7 @@ extern double horizontal_stdev, vertical_stdev; struct ccoords pol_to_cart(struct pcoords c); + void comp_visit(struct leg *l); #endif diff --git a/web/web_svg.c b/web/web_svg.c index 8714e69..1d0b9aa 100644 --- a/web/web_svg.c +++ b/web/web_svg.c @@ -11,9 +11,6 @@ #include <emscripten/emscripten.h> -#define NUM_SECTORS 20 -#define SECTOR_WIDTH 18.0 - #define C_BLACK "#272b2c" #define C_WHITE "#fbe3ba" #define C_RED "#f6302f" |