pt_engine.c

filter: Statementstmt 96.7% 798 / 825 Branchbran 80.4% 624 / 776 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.8% 1422 / 1601 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Terrain.xs ↑ Summary blib/lib/Physics/Terrain.pm »
1 /* pt_engine.c - the field engine, a transliteration of the prototype's
2 * fx.js, terrain.js and sim.js.
3 *
4 * Every expression here is in the same order as the JavaScript it came from
5 * (plan_crater/prototype), because the fixtures recorded from those files are
6 * the specification and t/03-bitwise.t asks for the same integers. Every
7 * product, quotient, shift and modulo on a fixed-point value goes through one
8 * of the fx_ helpers, which floor toward minus infinity as the JavaScript's
9 * do; intermediates are int64_t so no product can overflow, and the state is
10 * checked against a declared box every tick. No floating point, no libm, no
11 * clock, no random beyond the engine's own xorshift32. C89: declarations at
12 * the top of a block, no //, no variable-length arrays.
13 *
14 * The JavaScript's names are kept so the files can be read side by side:
15 * noiseLayer, generate, walkCells, swept, carve, landing, stepBody, moveX,
16 * moveY, land, stepProjectile, bounce, split, explode, hurt, advance.
17 */
18  
19 #include <stdlib.h>
20 #include <string.h>
21 #include "pt_abi.h"
22 #include "pt_sine.h"
23  
24 typedef int64_t i64;
25 typedef uint32_t u32;
26  
27 /* ---- fx.js ---------------------------------------------------------------- */
28  
29 #define ONE 256
30 #define Q16 65536
31 #define BOX ((i64) 1073741824)
32  
33 1133788825 static i64 floordiv(i64 n, i64 d) {
34 1133788825 i64 q = n / d, r = n % d;
35 1133788825 ▶ if (r != 0 && ((r < 0) != (d < 0))) q -= 1;
Branch: 2 branches
BranchTrueFalseTotal
if (r != 0 && ((r < 0) != (d < 0))) q -= 1; 1022534746 111254079 1133788825
if (r != 0 && ((r < 0) != (d < 0))) q -= 1; 45180 1022489566 1022534746
36 1133788825 return q;
37 }
38 3463381 static i64 fx_from(i64 c) { return c * ONE; }
39 19230610 static i64 fx_shr(i64 p) { return floordiv(p, ONE); }
40 15564 static i64 fx_mul(i64 a, i64 b) { return floordiv(a * b, ONE); }
41 5286954115 static i64 fx_imul(i64 a, i64 b) { return a * b; }
42 1111723896 static i64 fx_idiv(i64 a, i64 b) { return floordiv(a, b); }
43 2278373 static i64 fx_mod(i64 a, i64 m) { return a - m * floordiv(a, m); }
44 439741 static i64 fx_abs(i64 a) { return a < 0 ? -a : a; }
45 7348876 static i64 fx_min(i64 a, i64 b) { return a < b ? a : b; }
46 7146172 static i64 fx_max(i64 a, i64 b) { return a > b ? a : b; }
47 117978 ▶static i64 fx_sign(i64 v) { return v < 0 ? -1 : (v > 0 ? 1 : 0); }
Branch: 1 branch
BranchTrueFalseTotal
static i64 fx_sign(i64 v) { return v < 0 ? -1 : (v > 0 ? 1 : 0); } 102386 15592 117978
48 3450 static i64 fx_scale16(i64 v, i64 q) { return floordiv(v * q, Q16); }
49  
50 1547 static i64 fx_isqrt(i64 n) {
51 1547 i64 bit, res = 0;
52 1547 ▶ if (n < 2) return n;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 2) return n; 6 1541 1547
53 1541 bit = 1;
54 12721 ▶ while (bit * 4 <= n) bit = bit * 4;
Branch: 1 branch
BranchTrueFalseTotal
while (bit * 4 <= n) bit = bit * 4; 11180 1541 12721
55 14262 ▶ while (bit != 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (bit != 0) { 12721 1541 14262
56 12721 ▶ if (n >= res + bit) { n = n - res - bit; res = floordiv(res, 2) + bit; }
Branch: 1 branch
BranchTrueFalseTotal
if (n >= res + bit) { n = n - res - bit; res = floordiv(res, 2) + bit; } 6971 5750 12721
57 5750 else res = floordiv(res, 2);
58 12721 bit = floordiv(bit, 4);
59 }
60 1541 return res;
61 }
62  
63 1725 static i64 fx_sin(i64 a) { return pt_sine[fx_mod(a, 4096)]; }
64 3190 static i64 fx_cos(i64 a) { return pt_sine[fx_mod(a + 1024, 4096)]; }
65  
66 7730 static u32 xs32(u32 s) {
67 7730 s ^= s << 13;
68 7730 s ^= s >> 17;
69 7730 s ^= s << 5;
70 7730 return s;
71 }
72 5721738 static u32 mix32(u32 h, u32 v) {
73 5721738 h = h ^ v;
74 5721738 h = h * 0x85ebca6bu;
75 5721738 h = h ^ (h >> 13);
76 5721738 h = h * 0xc2b2ae35u;
77 5721738 h = h ^ (h >> 16);
78 5721738 return h;
79 }
80 955 static u32 seed32(u32 seed) {
81 955 ▶ u32 s = seed ? seed : 0x9e3779b9u;
Branch: 1 branch
BranchTrueFalseTotal
u32 s = seed ? seed : 0x9e3779b9u; 955 0 955
82 955 s = mix32(s, 0x2545f491u);
83 955 ▶ return s == 0 ? 0x9e3779b9u : s;
Branch: 1 branch
BranchTrueFalseTotal
return s == 0 ? 0x9e3779b9u : s; 955 0 955
84 }
85 7315 static i64 rng_below(u32 *s, i64 n) { *s = xs32(*s); return fx_mod((i64) *s, n); }
86 415 static i64 rng_between(u32 *s, i64 lo, i64 hi) { *s = xs32(*s); return lo + fx_mod((i64) *s, hi - lo + 1); }
87  
88 1565616 static u32 hash2(u32 seed, i64 x, i64 y, i64 k) {
89 1565616 u32 h = mix32(seed, (u32) (x + 0x10000));
90 1565616 h = mix32(h, (u32) (y + 0x10000));
91 1565616 h = mix32(h, (u32) (k + 0x7f));
92 1565616 return h & 0xffffu;
93 }
94  
95 struct hasher { u32 a, b; };
96 5182 static void hasher_init(struct hasher *h) { h->a = 0x811c9dc5u; h->b = 0x01000193u; }
97 511490 static void hasher_add(struct hasher *h, i64 v) {
98 511490 u32 lo = (u32) v;
99 511490 u32 hi = (u32) floordiv(v, (i64) 4294967296LL);
100 511490 h->a = mix32(h->a, lo);
101 511490 h->b = mix32((u32) (h->b + 0x9e3779b9u), hi ^ lo);
102 511490 }
103  
104 /* ---- growable arrays ------------------------------------------------------ */
105  
106 struct vec { void *p; int32_t n, cap; size_t sz; };
107  
108 278372 static int vec_push(struct vec *v, const void *item) {
109 278372 ▶ if (v->n == v->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (v->n == v->cap) { 6585 271787 278372
110 6585 ▶ int32_t ncap = v->cap ? v->cap * 2 : 64;
Branch: 1 branch
BranchTrueFalseTotal
int32_t ncap = v->cap ? v->cap * 2 : 64; 1820 4765 6585
111 6585 void *np = realloc(v->p, (size_t) ncap * v->sz);
112 6585 ▶ if (!np) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!np) return 0; 0 6585 6585
113 6585 v->p = np; v->cap = ncap;
114 }
115 278372 memcpy((char *) v->p + (size_t) v->n * v->sz, item, v->sz);
116 278372 v->n++;
117 278372 return 1;
118 }
119 8042 static void vec_init(struct vec *v, size_t sz) { v->p = NULL; v->n = 0; v->cap = 0; v->sz = sz; }
120 6676 static void vec_clear(struct vec *v) { v->n = 0; }
121 8042 static void vec_drop(struct vec *v) { free(v->p); v->p = NULL; v->n = 0; v->cap = 0; }
122 802 static int vec_copy(struct vec *dst, const struct vec *src) {
123 802 vec_clear(dst);
124 802 ▶ if (src->n == 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (src->n == 0) return 1; 802 0 802
125 0 ▶ if (dst->cap < src->n) {
Branch: 1 branch
BranchTrueFalseTotal
if (dst->cap < src->n) { 0 0 0
126 0 void *np = realloc(dst->p, (size_t) src->n * src->sz);
127 0 ▶ if (!np) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!np) return 0; 0 0 0
128 0 dst->p = np; dst->cap = src->n;
129 }
130 0 memcpy(dst->p, src->p, (size_t) src->n * src->sz);
131 0 dst->n = src->n;
132 0 return 1;
133 }
134  
135 /* ---- weapons.js ------------------------------------------------------------ */
136  
137 static const struct pt_weapon WEAPONS[] = {
138 { "bazooka", "Bazooka", PT_KIND_SHELL, 2000, 1, 0, 0, 0, 22, 45, 900, 0, 0, 0, 0 },
139 { "grenade", "Grenade", PT_KIND_BOUNCE, 1700, 0, 180, 128, 200, 18, 40, 800, 0, 0, 0, 0 },
140 { "shotgun", "Shotgun", PT_KIND_HITSCAN, 0, 0, 0, 0, 0, 6, 30, 350, 900, 0, 0, 0 },
141 { "cluster", "Cluster", PT_KIND_CLUSTER, 1900, 1, 55, 0, 0, 10, 12, 400, 0, 5, 200, 240 },
142 { "dynamite", "Dynamite", PT_KIND_DROP, 0, 0, 180, 0, 0, 40, 75, 900, 0, 0, 0, 0 },
143 { NULL, "Bomblet", PT_KIND_SHELL, 0, 1, 0, 0, 0, 9, 18, 450, 0, 0, 0, 0 },
144 { NULL, "Death", PT_KIND_DEATH, 0, 0, 0, 0, 0, 14, 25, 600, 0, 0, 0, 0 }
145 };
146 #define N_WEAPONS 5
147 #define W_CHILD 5
148 #define W_DEATH 6
149  
150 /* ---- the constants of sim.js ---------------------------------------------- */
151  
152 #define C_G 12
153 #define C_TERM 2400
154 #define C_WALK 128
155 #define C_STEP 3
156 #define C_JUMP_VY (-300)
157 #define C_JUMP_VX 130
158 #define C_BODY_L 4
159 #define C_BODY_R 3
160 #define C_BODY_H 12
161 #define C_BODY_MID 6
162 #define C_FALL_HURT 700
163 #define C_FALL_DIV 20
164 #define C_BOUNCE 64
165 #define C_BOUNCE_MIN 400
166 #define C_FRICTION 179
167 #define C_STOP 24
168 #define C_KB_REACH 18
169 #define C_KB_LIFT 3
170 #define C_DIRECT_D 9
171 #define C_HP 100
172 #define C_WIND_MAX 20
173 #define C_WIND_DIV 16
174 #define C_OWNER_GRACE 6
175 #define C_MUZZLE 2560
176 #define C_REST_V 90
177 #define C_ROLL_FRICTION 236
178 #define C_ROLL_STOP 6
179 #define C_ROLL_NUDGE 24
180 #define C_MAX_BOUNCES 60
181 #define C_LADDER 64
182  
183 /* ---- the state -------------------------------------------------------------- */
184  
185 struct body { i64 seat, k, x, y, vx, vy, mode, hp, alive, facing, jump_latch, grace; };
186 struct proj { i64 id, w, x, y, vx, vy, owner, fuse, age, wacc, bounces, resting, alive; };
187  
188 struct turn {
189 i64 active, wind, phase, turn_tick, tick0, shot_tick, last_bits, settled_at;
190 int32_t has_shot; struct pt_shot shot; int32_t error;
191 struct vec inputs, events, craters, trace_bodies, trace_shots, ladder;
192 };
193  
194 struct pt_state {
195 struct pt_gen gen;
196 int32_t W, H;
197 uint8_t *mask, *scorch;
198 uint32_t seed;
199 u32 rng;
200 int32_t teams, per_team;
201 int32_t wind_set, wind_override;
202 int32_t live_cap, settle_cap;
203 int32_t nbodies; struct body bodies[PT_MAX_BODIES];
204 int32_t nproj; struct proj proj[PT_MAX_PROJECTILES];
205 struct vec craters, graves;
206 i64 tick, next_pid;
207 int32_t error, oom;
208 struct turn turn;
209 };
210  
211 struct pt_snapshot {
212 int32_t W, H;
213 uint8_t *mask, *scorch;
214 int32_t nbodies; struct body bodies[PT_MAX_BODIES];
215 struct vec craters, graves;
216 u32 rng; i64 tick, next_pid;
217 };
218  
219 static const struct pt_gen DEFAULT_GEN = { PT_GEN_NOISE, 1280, 640, 64, 16, 32, 44000, 196608, 98304, 31500, 34500, 56, 90, 9, 18, -1 };
220  
221 /* ---- terrain.js ------------------------------------------------------------ */
222  
223 0 static i64 idx(const struct pt_state *f, i64 x, i64 y) { return fx_imul(y, f->W) + x; }
224  
225 1746070 static i64 solid(const struct pt_state *f, i64 x, i64 y) {
226 1746070 ▶ if (x < 0 || y < 0 || x >= f->W || y >= f->H) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (x < 0 || y < 0 || x >= f->W || y >= f->H) return 0; 1740535 5535 1746070
if (x < 0 || y < 0 || x >= f->W || y >= f->H) return 0; 1693587 46948 1740535
if (x < 0 || y < 0 || x >= f->W || y >= f->H) return 0; 1688213 5374 1693587
if (x < 0 || y < 0 || x >= f->W || y >= f->H) return 0; 1 1688212 1688213
227 1688212 return f->mask[fx_imul(y, f->W) + x];
228 }
229 644550 static void set_cell(struct pt_state *f, i64 x, i64 y, uint8_t v) {
230 644550 ▶ if (x < 0 || y < 0 || x >= f->W || y >= f->H) return;
Branch: 4 branches
BranchTrueFalseTotal
if (x < 0 || y < 0 || x >= f->W || y >= f->H) return; 644550 0 644550
if (x < 0 || y < 0 || x >= f->W || y >= f->H) return; 644550 0 644550
if (x < 0 || y < 0 || x >= f->W || y >= f->H) return; 644550 0 644550
if (x < 0 || y < 0 || x >= f->W || y >= f->H) return; 0 644550 644550
231 644550 f->mask[fx_imul(y, f->W) + x] = v;
232 }
233  
234 1946880 static i64 smooth(i64 u, i64 S) {
235 1946880 i64 uu = fx_imul(u, u);
236 1946880 i64 t = fx_imul(uu, fx_imul(3, S) - fx_imul(2, u));
237 1946880 return fx_idiv(t, fx_imul(S, S));
238 }
239  
240 1014 static int noise_layer(const struct pt_state *f, u32 seed, i64 S, i64 key, uint16_t *out) {
241 1014 i64 W = f->W, H = f->H, gw = fx_idiv(W, S) + 2, gh = fx_idiv(H, S) + 2;
242 int32_t *lattice, *gx_of, *su, *sw, *gy_of;
243 1014 i64 x, y, gx, gy, u, w, i, row, v00, v10, v01, v11, top, bot, SS = fx_imul(S, S);
244 1014 lattice = (int32_t *) malloc(sizeof(int32_t) * (size_t) fx_imul(gw, gh));
245 1014 gx_of = (int32_t *) malloc(sizeof(int32_t) * (size_t) W);
246 1014 su = (int32_t *) malloc(sizeof(int32_t) * (size_t) W);
247 1014 sw = (int32_t *) malloc(sizeof(int32_t) * (size_t) H);
248 1014 gy_of = (int32_t *) malloc(sizeof(int32_t) * (size_t) H);
249 1014 ▶ if (!lattice || !gx_of || !su || !sw || !gy_of) { free(lattice); free(gx_of); free(su); free(sw); free(gy_of); return 0; }
Branch: 5 branches
BranchTrueFalseTotal
if (!lattice || !gx_of || !su || !sw || !gy_of) { free(lattice); free(gx_of); free(su); free(sw); free(gy_of); return 0; } 1014 0 1014
if (!lattice || !gx_of || !su || !sw || !gy_of) { free(lattice); free(gx_of); free(su); free(sw); free(gy_of); return 0; } 1014 0 1014
if (!lattice || !gx_of || !su || !sw || !gy_of) { free(lattice); free(gx_of); free(su); free(sw); free(gy_of); return 0; } 1014 0 1014
if (!lattice || !gx_of || !su || !sw || !gy_of) { free(lattice); free(gx_of); free(su); free(sw); free(gy_of); return 0; } 1014 0 1014
if (!lattice || !gx_of || !su || !sw || !gy_of) { free(lattice); free(gx_of); free(su); free(sw); free(gy_of); return 0; } 0 1014 1014
250 26702 ▶ for (gy = 0; gy < gh; gy++) {
Branch: 1 branch
BranchTrueFalseTotal
for (gy = 0; gy < gh; gy++) { 25688 1014 26702
251 1591304 ▶ for (gx = 0; gx < gw; gx++) lattice[fx_imul(gy, gw) + gx] = (int32_t) hash2(seed, gx, gy, key);
Branch: 1 branch
BranchTrueFalseTotal
for (gx = 0; gx < gw; gx++) lattice[fx_imul(gy, gw) + gx] = (int32_t) hash2(seed, gx, gy, key); 1565616 25688 1591304
252 }
253 1298934 ▶ for (x = 0; x < W; x++) { gx_of[x] = (int32_t) fx_idiv(x, S); u = fx_mod(x, S); su[x] = (int32_t) smooth(u, S); }
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < W; x++) { gx_of[x] = (int32_t) fx_idiv(x, S); u = fx_mod(x, S); su[x] = (int32_t) smooth(u, S); } 1297920 1014 1298934
254 649974 ▶ for (y = 0; y < H; y++) { gy_of[y] = (int32_t) fx_idiv(y, S); w = fx_mod(y, S); sw[y] = (int32_t) smooth(w, S); }
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < H; y++) { gy_of[y] = (int32_t) fx_idiv(y, S); w = fx_mod(y, S); sw[y] = (int32_t) smooth(w, S); } 648960 1014 649974
255 649974 ▶ for (y = 0; y < H; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < H; y++) { 648960 1014 649974
256 648960 gy = gy_of[y]; w = sw[y]; row = fx_imul(gy, gw);
257 648960 i = fx_imul(y, W);
258 831317760 ▶ for (x = 0; x < W; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < W; x++) { 830668800 648960 831317760
259 830668800 gx = gx_of[x]; u = su[x];
260 830668800 v00 = lattice[row + gx]; v10 = lattice[row + gx + 1];
261 830668800 v01 = lattice[row + gw + gx]; v11 = lattice[row + gw + gx + 1];
262 830668800 top = fx_imul(v00, S - u) + fx_imul(v10, u);
263 830668800 bot = fx_imul(v01, S - u) + fx_imul(v11, u);
264 830668800 out[i + x] = (uint16_t) fx_idiv(fx_imul(top, S - w) + fx_imul(bot, w), SS);
265 }
266 }
267 1014 free(lattice); free(gx_of); free(su); free(sw); free(gy_of);
268 1014 return 1;
269 }
270  
271 10979 static void box_set(struct pt_state *f, i64 x0, i64 y0, i64 x1, i64 y1, uint8_t v) {
272 i64 x, y, i;
273 10979 x0 = fx_max(x0, 0); y0 = fx_max(y0, 0); x1 = fx_min(x1, f->W - 1); y1 = fx_min(y1, f->H - 1);
274 807492 ▶ for (y = y0; y <= y1; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = y0; y <= y1; y++) { 796513 10979 807492
275 796513 i = fx_imul(y, f->W);
276 62561690 ▶ for (x = x0; x <= x1; x++) f->mask[i + x] = v;
Branch: 1 branch
BranchTrueFalseTotal
for (x = x0; x <= x1; x++) f->mask[i + x] = v; 61765177 796513 62561690
277 }
278 10979 }
279 6672 static void fill_box(struct pt_state *f, i64 x0, i64 y0, i64 x1, i64 y1) { box_set(f, x0, y0, x1, y1, 1); }
280 4307 static void clear_box(struct pt_state *f, i64 x0, i64 y0, i64 x1, i64 y1) { box_set(f, x0, y0, x1, y1, 0); }
281  
282 16 static void slope(struct pt_state *f, i64 x0, i64 y0, i64 x1, i64 y1) {
283 16 i64 x, y, dx = x1 - x0, dy = y1 - y0, top;
284 16 ▶ if (dx == 0) { fill_box(f, x0, fx_min(y0, y1), x0, f->H - 1); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (dx == 0) { fill_box(f, x0, fx_min(y0, y1), x0, f->H - 1); return; } 0 16 16
285 2532 ▶ for (x = fx_min(x0, x1); x <= fx_max(x0, x1); x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (x = fx_min(x0, x1); x <= fx_max(x0, x1); x++) { 2516 16 2532
286 2516 top = y0 + fx_idiv(fx_imul(dy, x - x0), dx);
287 647066 ▶ for (y = fx_max(top, 0); y < f->H; y++) set_cell(f, x, y, 1);
Branch: 1 branch
BranchTrueFalseTotal
for (y = fx_max(top, 0); y < f->H; y++) set_cell(f, x, y, 1); 644550 2516 647066
288 }
289 }
290  
291 3777 static void platform(struct pt_state *f, i64 x, i64 y, i64 half_w, i64 headroom) {
292 3777 clear_box(f, x - half_w, y - headroom, x + half_w, y - 1);
293 3777 fill_box(f, x - half_w, y, x + half_w, y + 4);
294 3777 }
295  
296 1119 static i64 carve(struct pt_state *f, i64 cx, i64 cy, i64 r, int quiet) {
297 1119 i64 dx, dy, rr = fx_imul(r, r), cleared = 0, x, y, i, sr = fx_imul(r + 3, r + 3), d2;
298 49922 ▶ for (dy = -r - 3; dy <= r + 3; dy++) {
Branch: 1 branch
BranchTrueFalseTotal
for (dy = -r - 3; dy <= r + 3; dy++) { 48803 1119 49922
299 48803 y = cy + dy;
300 48803 ▶ if (y < 0 || y >= f->H) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (y < 0 || y >= f->H) continue; 48717 86 48803
if (y < 0 || y >= f->H) continue; 0 48717 48717
301 48717 i = fx_imul(y, f->W);
302 2470994 ▶ for (dx = -r - 3; dx <= r + 3; dx++) {
Branch: 1 branch
BranchTrueFalseTotal
for (dx = -r - 3; dx <= r + 3; dx++) { 2422277 48717 2470994
303 2422277 x = cx + dx;
304 2422277 ▶ if (x < 0 || x >= f->W) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (x < 0 || x >= f->W) continue; 2421591 686 2422277
if (x < 0 || x >= f->W) continue; 0 2421591 2421591
305 2421591 d2 = fx_imul(dx, dx) + fx_imul(dy, dy);
306 2421591 ▶ if (d2 <= rr) {
Branch: 1 branch
BranchTrueFalseTotal
if (d2 <= rr) { 1404743 1016848 2421591
307 1404743 ▶ if (f->mask[i + x]) { cleared++; f->mask[i + x] = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (f->mask[i + x]) { cleared++; f->mask[i + x] = 0; } 423156 981587 1404743
308 1016848 ▶ } else if (d2 <= sr && !quiet && f->mask[i + x]) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (d2 <= sr && !quiet && f->mask[i + x]) { 412260 604588 1016848
} else if (d2 <= sr && !quiet && f->mask[i + x]) { 412144 116 412260
} else if (d2 <= sr && !quiet && f->mask[i + x]) { 108495 303649 412144
309 108495 f->scorch[i + x] = 1;
310 }
311 }
312 }
313 1119 return cleared;
314 }
315  
316 1312 static void sculpt(struct pt_state *f, int32_t n, const struct pt_sculpt *ops) {
317 int32_t k;
318 4697 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 3385 1312 4697
319 3385 const struct pt_sculpt *op = &ops[k];
320 3385 switch (op->op) {
321 2837 case PT_OP_FILL: fill_box(f, op->a, op->b, op->c, op->d); break;
322 530 case PT_OP_CLEAR: clear_box(f, op->a, op->b, op->c, op->d); break;
323 16 case PT_OP_SLOPE: slope(f, op->a, op->b, op->c, op->d); break;
324 1 case PT_OP_DISC: carve(f, op->a, op->b, op->c, 1); break;
325 1 case PT_OP_PLATFORM: platform(f, op->a, op->b, op->c, op->d); break;
326 0 default: break;
327 }
328 }
329 1312 }
330  
331 /* walkCells: the cells of the segment, both ends included; a diagonal step
332 * visits the two cells it cuts between first. visit returns nonzero to stop */
333 struct walk_ctx { const struct pt_state *s; i64 owner; int owner_counts; i64 hx, hy, hpx, hpy; int kind; i64 body; };
334  
335 213877 static int walk_cells(i64 x0, i64 y0, i64 x1, i64 y1, int (*visit)(struct walk_ctx *, i64, i64, i64, i64), struct walk_ctx *ctx) {
336 213877 ▶ i64 dx = fx_abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
i64 dx = fx_abs(x1 - x0), sx = x0 < x1 ? 1 : -1; 93508 120369 213877
337 213877 ▶ i64 dy = -fx_abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
i64 dy = -fx_abs(y1 - y0), sy = y0 < y1 ? 1 : -1; 94662 119215 213877
338 213877 i64 err = dx + dy, e2, x = x0, y = y0, px = x0, py = y0;
339 int r, step_x, step_y;
340 for (;;) {
341 891946 r = visit(ctx, x, y, px, py);
342 891946 ▶ if (r) return r;
Branch: 1 branch
BranchTrueFalseTotal
if (r) return r; 2295 889651 891946
343 889651 ▶ if (x == x1 && y == y1) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (x == x1 && y == y1) return 0; 327987 561664 889651
if (x == x1 && y == y1) return 0; 209810 118177 327987
344 679841 px = x; py = y;
345 679841 e2 = err + err;
346 679841 step_x = e2 >= dy; step_y = e2 <= dx;
347 679841 ▶ if (step_x && step_y) {
Branch: 2 branches
BranchTrueFalseTotal
if (step_x && step_y) { 479493 200348 679841
if (step_x && step_y) { 292769 186724 479493
348 292769 r = visit(ctx, x + sx, y, px, py);
349 292769 ▶ if (r) return r;
Branch: 1 branch
BranchTrueFalseTotal
if (r) return r; 708 292061 292769
350 292061 r = visit(ctx, x, y + sy, px, py);
351 292061 ▶ if (r) return r;
Branch: 1 branch
BranchTrueFalseTotal
if (r) return r; 1064 290997 292061
352 }
353 678069 ▶ if (step_x) { err += dy; x += sx; }
Branch: 1 branch
BranchTrueFalseTotal
if (step_x) { err += dy; x += sx; } 477721 200348 678069
354 678069 ▶ if (step_y) { err += dx; y += sy; }
Branch: 1 branch
BranchTrueFalseTotal
if (step_y) { err += dx; y += sy; } 491345 186724 678069
355 }
356 }
357  
358 747829 static int visit_solid(struct walk_ctx *c, i64 x, i64 y, i64 px, i64 py) {
359 747829 ▶ if (solid(c->s, x, y)) { c->hx = x; c->hy = y; c->hpx = px; c->hpy = py; return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (solid(c->s, x, y)) { c->hx = x; c->hy = y; c->hpx = px; c->hpy = py; return 1; } 2014 745815 747829
360 745815 return 0;
361 }
362  
363 /* swept: the first solid cell on the segment, or none; hpx,hpy the last free cell */
364 107352 static int swept(const struct pt_state *s, i64 x0, i64 y0, i64 x1, i64 y1, struct walk_ctx *c) {
365 107352 memset(c, 0, sizeof *c);
366 107352 c->s = s;
367 107352 ▶ if (walk_cells(x0, y0, x1, y1, visit_solid, c)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (walk_cells(x0, y0, x1, y1, visit_solid, c)) return 1; 2014 105338 107352
368 105338 c->hx = x1; c->hy = y1; c->hpx = x1; c->hpy = y1;
369 105338 return 0;
370 }
371  
372 3554589 static int box_free(const struct pt_state *f, i64 x0, i64 y0, i64 x1, i64 y1) {
373 i64 x, y, i;
374 3554589 ▶ if (x1 < 0 || y1 < 0 || x0 >= f->W || y0 >= f->H) return 1;
Branch: 4 branches
BranchTrueFalseTotal
if (x1 < 0 || y1 < 0 || x0 >= f->W || y0 >= f->H) return 1; 3554515 74 3554589
if (x1 < 0 || y1 < 0 || x0 >= f->W || y0 >= f->H) return 1; 3554515 0 3554515
if (x1 < 0 || y1 < 0 || x0 >= f->W || y0 >= f->H) return 1; 3554491 24 3554515
if (x1 < 0 || y1 < 0 || x0 >= f->W || y0 >= f->H) return 1; 26 3554465 3554491
375 3554465 x0 = fx_max(x0, 0); y0 = fx_max(y0, 0); x1 = fx_min(x1, f->W - 1); y1 = fx_min(y1, f->H - 1);
376 5167370 ▶ for (y = y0; y <= y1; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = y0; y <= y1; y++) { 4941277 226093 5167370
377 4941277 i = fx_imul(y, f->W);
378 18740243 ▶ for (x = x0; x <= x1; x++) if (f->mask[i + x]) return 0;
Branch: 2 branches
BranchTrueFalseTotal
for (x = x0; x <= x1; x++) if (f->mask[i + x]) return 0; 3328372 13798966 17127338
for (x = x0; x <= x1; x++) if (f->mask[i + x]) return 0; 17127338 1612905 18740243
379 }
380 226093 return 1;
381 }
382  
383 4394 static i64 surface_at(const struct pt_state *f, i64 x, i64 y_from) {
384 i64 y;
385 944567 ▶ for (y = fx_max(y_from, 0); y < f->H; y++) if (solid(f, x, y)) return y;
Branch: 2 branches
BranchTrueFalseTotal
for (y = fx_max(y_from, 0); y < f->H; y++) if (solid(f, x, y)) return y; 4394 940173 944567
for (y = fx_max(y_from, 0); y < f->H; y++) if (solid(f, x, y)) return y; 944567 0 944567
386 0 return -1;
387 }
388  
389 /* landing: n spots spread across the field, each a flat stand; spots[k][0..1] = x, ground row */
390 237 static void landing(struct pt_state *f, u32 *rng, i64 n, i64 half_w, i64 headroom, int32_t *spots) {
391 237 i64 k, lane, x = 0, y, tries, best, y_top;
392 237 lane = fx_idiv(f->W - fx_imul(half_w, 4), n);
393 4013 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 3776 237 4013
394 3776 best = -1;
395 7552 ▶ for (tries = 0; tries < 6 && best < 0; tries++) {
Branch: 2 branches
BranchTrueFalseTotal
for (tries = 0; tries < 6 && best < 0; tries++) { 7552 0 7552
for (tries = 0; tries < 6 && best < 0; tries++) { 3776 3776 7552
396 3776 x = fx_imul(half_w, 2) + fx_imul(lane, k) + rng_below(rng, fx_max(lane - fx_imul(half_w, 2), 1));
397 3776 y_top = fx_idiv(fx_imul(f->H, 20), 100);
398 3776 y = surface_at(f, x, y_top);
399 3776 ▶ if (y >= 0 && y < fx_idiv(fx_imul(f->H, 92), 100)) best = y;
Branch: 2 branches
BranchTrueFalseTotal
if (y >= 0 && y < fx_idiv(fx_imul(f->H, 92), 100)) best = y; 3776 0 3776
if (y >= 0 && y < fx_idiv(fx_imul(f->H, 92), 100)) best = y; 3776 0 3776
400 }
401 3776 ▶ if (best < 0) best = fx_idiv(fx_imul(f->H, 60), 100);
Branch: 1 branch
BranchTrueFalseTotal
if (best < 0) best = fx_idiv(fx_imul(f->H, 60), 100); 0 3776 3776
402 3776 platform(f, x, best, half_w, headroom);
403 3776 spots[k * 2] = (int32_t) x; spots[k * 2 + 1] = (int32_t) best;
404 }
405 237 }
406  
407 955 static int generate(struct pt_state *f, uint32_t seed, const struct pt_gen *o) {
408 i64 W, H, x, y, i, n, bias, cave_top, cave_bottom;
409 uint16_t *n1, *n2, *n3;
410 955 W = o->W; H = o->H;
411 955 f->W = (int32_t) W; f->H = (int32_t) H;
412 955 f->mask = (uint8_t *) calloc((size_t) fx_imul(W, H), 1);
413 955 f->scorch = (uint8_t *) calloc((size_t) fx_imul(W, H), 1);
414 955 ▶ if (!f->mask || !f->scorch) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!f->mask || !f->scorch) return 0; 955 0 955
if (!f->mask || !f->scorch) return 0; 0 955 955
415 955 f->seed = seed; f->gen = *o;
416 955 ▶ if (o->profile == PT_GEN_EMPTY) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (o->profile == PT_GEN_EMPTY) return 1; 559 396 955
417 396 ▶ if (o->profile == PT_GEN_FLAT) {
Branch: 1 branch
BranchTrueFalseTotal
if (o->profile == PT_GEN_FLAT) { 58 338 396
418 58 ▶ fill_box(f, 0, o->floor < 0 ? fx_idiv(fx_imul(H, 5), 8) : o->floor, W - 1, H - 1);
Branch: 1 branch
BranchTrueFalseTotal
fill_box(f, 0, o->floor < 0 ? fx_idiv(fx_imul(H, 5), 8) : o->floor, W - 1, H - 1); 0 58 58
419 58 return 1;
420 }
421 338 n1 = (uint16_t *) malloc(sizeof(uint16_t) * (size_t) fx_imul(W, H));
422 338 n2 = (uint16_t *) malloc(sizeof(uint16_t) * (size_t) fx_imul(W, H));
423 338 n3 = (uint16_t *) malloc(sizeof(uint16_t) * (size_t) fx_imul(W, H));
424 338 ▶ if (!n1 || !n2 || !n3 || !noise_layer(f, seed, o->coarse, 1, n1) || !noise_layer(f, seed, o->fine, 2, n2) || !noise_layer(f, seed, o->cave, 3, n3)) {
Branch: 6 branches
BranchTrueFalseTotal
if (!n1 || !n2 || !n3 || !noise_layer(f, seed, o->coarse, 1, n1) || !noise_layer(f, seed, o->fine, 2, n2) || !noise_layer(f, seed, o->cave, 3, n3)) { 338 0 338
if (!n1 || !n2 || !n3 || !noise_layer(f, seed, o->coarse, 1, n1) || !noise_layer(f, seed, o->fine, 2, n2) || !noise_layer(f, seed, o->cave, 3, n3)) { 338 0 338
if (!n1 || !n2 || !n3 || !noise_layer(f, seed, o->coarse, 1, n1) || !noise_layer(f, seed, o->fine, 2, n2) || !noise_layer(f, seed, o->cave, 3, n3)) { 338 0 338
if (!n1 || !n2 || !n3 || !noise_layer(f, seed, o->coarse, 1, n1) || !noise_layer(f, seed, o->fine, 2, n2) || !noise_layer(f, seed, o->cave, 3, n3)) { 338 0 338
if (!n1 || !n2 || !n3 || !noise_layer(f, seed, o->coarse, 1, n1) || !noise_layer(f, seed, o->fine, 2, n2) || !noise_layer(f, seed, o->cave, 3, n3)) { 338 0 338
if (!n1 || !n2 || !n3 || !noise_layer(f, seed, o->coarse, 1, n1) || !noise_layer(f, seed, o->fine, 2, n2) || !noise_layer(f, seed, o->cave, 3, n3)) { 0 338 338
425 0 free(n1); free(n2); free(n3);
426 0 return 0;
427 }
428 338 cave_top = fx_idiv(fx_imul(H, o->cave_top), 100);
429 338 cave_bottom = fx_idiv(fx_imul(H, o->cave_bottom), 100);
430 216658 ▶ for (y = 0; y < H; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < H; y++) { 216320 338 216658
431 216320 bias = fx_idiv(fx_imul(y, o->bias_scale), H) - o->bias_offset;
432 216320 i = fx_imul(y, W);
433 277105920 ▶ for (x = 0; x < W; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < W; x++) { 276889600 216320 277105920
434 276889600 n = fx_idiv(fx_imul(n1[i + x], 3) + n2[i + x], 4);
435 276889600 ▶ if (n + bias >= o->threshold) {
Branch: 1 branch
BranchTrueFalseTotal
if (n + bias >= o->threshold) { 121065945 155823655 276889600
436 121065945 ▶ if (y >= cave_top && y < cave_bottom && n3[i + x] >= o->cave_lo && n3[i + x] <= o->cave_hi) f->mask[i + x] = 0;
Branch: 4 branches
BranchTrueFalseTotal
if (y >= cave_top && y < cave_bottom && n3[i + x] >= o->cave_lo && n3[i + x] <= o->cave_hi) f->mask[i + x] = 0; 115032070 6033875 121065945
if (y >= cave_top && y < cave_bottom && n3[i + x] >= o->cave_lo && n3[i + x] <= o->cave_hi) f->mask[i + x] = 0; 87343110 27688960 115032070
if (y >= cave_top && y < cave_bottom && n3[i + x] >= o->cave_lo && n3[i + x] <= o->cave_hi) f->mask[i + x] = 0; 45211329 42131781 87343110
if (y >= cave_top && y < cave_bottom && n3[i + x] >= o->cave_lo && n3[i + x] <= o->cave_hi) f->mask[i + x] = 0; 6886605 38324724 45211329
437 114179340 else f->mask[i + x] = 1;
438 }
439 }
440 }
441 338 free(n1); free(n2); free(n3);
442 338 return 1;
443 }
444  
445 /* ---- sim.js: bodies and the box ------------------------------------------- */
446  
447 14255018 static i64 cell_x(const struct body *b) { return fx_shr(b->x); }
448 38734 static i64 cell_y(const struct body *b) { return fx_shr(b->y); }
449 308929 static i64 top_row(i64 y) { return fx_shr(y) - C_BODY_H; }
450 301119 static i64 bottom_row(i64 y) { return fx_shr(y - 1); }
451  
452 208895 static int box_free_at(const struct pt_state *s, i64 cx, i64 y) {
453 208895 return box_free(s, cx - C_BODY_L, top_row(y), cx + C_BODY_R, bottom_row(y));
454 }
455 3345694 static int row_solid_at(const struct pt_state *s, i64 cx, i64 row) {
456 3345694 return !box_free(s, cx - C_BODY_L, row, cx + C_BODY_R, row);
457 }
458 3139009 static int grounded(const struct pt_state *s, const struct body *b) { return row_solid_at(s, cell_x(b), fx_shr(b->y)); }
459  
460 4255735 static int box_has(const struct body *b, i64 X, i64 Y) {
461 4255735 i64 cx = cell_x(b);
462 4255735 ▶ return X >= cx - C_BODY_L && X <= cx + C_BODY_R && Y >= top_row(b->y) && Y <= bottom_row(b->y);
Branch: 4 branches
BranchTrueFalseTotal
return X >= cx - C_BODY_L && X <= cx + C_BODY_R && Y >= top_row(b->y) && Y <= bottom_row(b->y); 2392558 1863177 4255735
return X >= cx - C_BODY_L && X <= cx + C_BODY_R && Y >= top_row(b->y) && Y <= bottom_row(b->y); 22924 2369634 2392558
return X >= cx - C_BODY_L && X <= cx + C_BODY_R && Y >= top_row(b->y) && Y <= bottom_row(b->y); 2287 20637 22924
return X >= cx - C_BODY_L && X <= cx + C_BODY_R && Y >= top_row(b->y) && Y <= bottom_row(b->y); 132 2155 2287
463 }
464  
465 4426 static i64 add_body(struct pt_state *s, i64 seat, i64 cx, i64 cy) {
466 struct body *b;
467 int32_t i;
468 4426 ▶ if (s->nbodies >= PT_MAX_BODIES) { s->error = PT_ERR_BODIES; return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (s->nbodies >= PT_MAX_BODIES) { s->error = PT_ERR_BODIES; return -1; } 1 4425 4426
469 4425 b = &s->bodies[s->nbodies];
470 4425 memset(b, 0, sizeof *b);
471 4425 b->seat = seat; b->x = fx_from(cx) + 128; b->y = fx_from(cy); b->mode = PT_STANDING;
472 4425 b->hp = C_HP; b->alive = 1; b->facing = 1;
473 33258 ▶ for (i = 0; i < s->nbodies; i++) if (s->bodies[i].seat == seat) b->k++;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < s->nbodies; i++) if (s->bodies[i].seat == seat) b->k++; 5806 23027 28833
for (i = 0; i < s->nbodies; i++) if (s->bodies[i].seat == seat) b->k++; 28833 4425 33258
474 4425 s->nbodies++;
475 4425 return s->nbodies - 1;
476 }
477  
478 566 static void place_bodies(struct pt_state *s, int32_t n, const struct pt_body_in *list) {
479 int32_t k;
480 i64 cy, i;
481 1183 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 617 566 1183
482 617 cy = surface_at(s, list[k].cx, list[k].cy_from);
483 617 ▶ if (cy < 0) cy = s->H - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (cy < 0) cy = s->H - 1; 0 617 617
484 617 i = add_body(s, list[k].seat, list[k].cx, cy);
485 617 ▶ if (i >= 0 && list[k].hp > 0) s->bodies[i].hp = list[k].hp;
Branch: 2 branches
BranchTrueFalseTotal
if (i >= 0 && list[k].hp > 0) s->bodies[i].hp = list[k].hp; 617 0 617
if (i >= 0 && list[k].hp > 0) s->bodies[i].hp = list[k].hp; 3 614 617
486 }
487 566 }
488  
489 237 static int place_teams(struct pt_state *s) {
490 237 i64 n = fx_imul(s->teams, s->per_team), i, j, t, k, tmp;
491 int32_t *spots, *order;
492 237 ▶ if (n > PT_MAX_BODIES) { s->error = PT_ERR_BODIES; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (n > PT_MAX_BODIES) { s->error = PT_ERR_BODIES; return 0; } 0 237 237
493 237 ▶ spots = (int32_t *) malloc(sizeof(int32_t) * (size_t) (n ? n * 2 : 1));
Branch: 1 branch
BranchTrueFalseTotal
spots = (int32_t *) malloc(sizeof(int32_t) * (size_t) (n ? n * 2 : 1)); 237 0 237
494 237 ▶ order = (int32_t *) malloc(sizeof(int32_t) * (size_t) (n ? n : 1));
Branch: 1 branch
BranchTrueFalseTotal
order = (int32_t *) malloc(sizeof(int32_t) * (size_t) (n ? n : 1)); 237 0 237
495 237 ▶ if (!spots || !order) { free(spots); free(order); return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (!spots || !order) { free(spots); free(order); return 0; } 237 0 237
if (!spots || !order) { free(spots); free(order); return 0; } 0 237 237
496 237 landing(s, &s->rng, n, s->gen.platform, s->gen.headroom, spots);
497 4013 ▶ for (i = 0; i < n; i++) order[i] = (int32_t) i;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) order[i] = (int32_t) i; 3776 237 4013
498 3776 ▶ for (i = n - 1; i > 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = n - 1; i > 0; i--) { 3539 237 3776
499 3539 j = rng_below(&s->rng, i + 1);
500 3539 tmp = order[i]; order[i] = order[j]; order[j] = (int32_t) tmp;
501 }
502 1181 ▶ for (t = 0; t < s->teams; t++) {
Branch: 1 branch
BranchTrueFalseTotal
for (t = 0; t < s->teams; t++) { 944 237 1181
503 4720 ▶ for (k = 0; k < s->per_team; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < s->per_team; k++) { 3776 944 4720
504 3776 i = order[fx_imul(t, s->per_team) + k];
505 3776 add_body(s, t, spots[i * 2], spots[i * 2 + 1]);
506 }
507 }
508 237 free(spots); free(order);
509 237 return 1;
510 }
511  
512 /* ---- the turn ---------------------------------------------------------------- */
513  
514 955 static void turn_init(struct turn *t) {
515 955 memset(t, 0, sizeof *t);
516 955 vec_init(&t->inputs, sizeof(struct pt_input));
517 955 vec_init(&t->events, sizeof(struct pt_event));
518 955 vec_init(&t->craters, sizeof(struct pt_crater));
519 955 vec_init(&t->trace_bodies, sizeof(struct pt_trace));
520 955 vec_init(&t->trace_shots, sizeof(struct pt_trace));
521 955 vec_init(&t->ladder, sizeof(struct pt_ladder));
522 955 t->phase = PT_PHASE_IDLE;
523 955 }
524 955 static void turn_drop(struct turn *t) {
525 955 vec_drop(&t->inputs); vec_drop(&t->events); vec_drop(&t->craters);
526 955 vec_drop(&t->trace_bodies); vec_drop(&t->trace_shots); vec_drop(&t->ladder);
527 955 }
528  
529 5861 static void event(struct pt_state *s, i64 kind, i64 a, i64 b) {
530 struct pt_event e;
531 5861 e.tick = (int32_t) s->turn.turn_tick; e.kind = (int32_t) kind; e.a = (int32_t) a; e.b = (int32_t) b;
532 5861 ▶ if (!vec_push(&s->turn.events, &e)) s->oom = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!vec_push(&s->turn.events, &e)) s->oom = 1; 0 5861 5861
533 5861 }
534 263468 static void trace_row(struct pt_state *s, struct vec *v, i64 who, i64 x, i64 y, i64 weapon) {
535 struct pt_trace r;
536 263468 r.who = (int32_t) who; r.tick = (int32_t) s->turn.turn_tick; r.x = (int32_t) x; r.y = (int32_t) y; r.weapon = (int32_t) weapon;
537 263468 ▶ if (!vec_push(v, &r)) s->oom = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!vec_push(v, &r)) s->oom = 1; 0 263468 263468
538 263468 }
539  
540 5182 static void hash_state(const struct pt_state *s, uint32_t out[2]) {
541 struct hasher h;
542 int32_t i;
543 const struct body *b;
544 const struct proj *p;
545 5182 hasher_init(&h);
546 5182 ▶ hasher_add(&h, s->tick); hasher_add(&h, s->turn.phase == PT_PHASE_IDLE ? 0 : s->turn.wind); hasher_add(&h, s->craters.n);
Branch: 1 branch
BranchTrueFalseTotal
hasher_add(&h, s->tick); hasher_add(&h, s->turn.phase == PT_PHASE_IDLE ? 0 : s->turn.wind); hasher_add(&h, s->craters.n); 5182 0 5182
547 65780 ▶ for (i = 0; i < s->nbodies; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->nbodies; i++) { 60598 5182 65780
548 60598 b = &s->bodies[i];
549 60598 hasher_add(&h, b->x); hasher_add(&h, b->y); hasher_add(&h, b->vx); hasher_add(&h, b->vy);
550 60598 hasher_add(&h, b->mode); hasher_add(&h, b->hp); hasher_add(&h, b->alive); hasher_add(&h, b->facing);
551 }
552 8470 ▶ for (i = 0; i < s->nproj; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->nproj; i++) { 3288 5182 8470
553 3288 p = &s->proj[i];
554 3288 ▶ if (!p->alive) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!p->alive) continue; 1428 1860 3288
555 1860 hasher_add(&h, p->x); hasher_add(&h, p->y); hasher_add(&h, p->vx); hasher_add(&h, p->vy); hasher_add(&h, p->fuse); hasher_add(&h, p->wacc);
556 }
557 5182 out[0] = h.a; out[1] = h.b;
558 5182 }
559  
560 981 static int32_t start_turn(struct pt_state *s, i64 active) {
561 981 struct turn *t = &s->turn;
562 981 ▶ if (active < 0 || active >= s->nbodies) return -PT_ERR_STATE;
Branch: 2 branches
BranchTrueFalseTotal
if (active < 0 || active >= s->nbodies) return -PT_ERR_STATE; 981 0 981
if (active < 0 || active >= s->nbodies) return -PT_ERR_STATE; 2 979 981
563 979 vec_clear(&t->inputs); vec_clear(&t->events); vec_clear(&t->craters);
564 979 vec_clear(&t->trace_bodies); vec_clear(&t->trace_shots); vec_clear(&t->ladder);
565 979 t->active = active;
566 979 ▶ t->wind = s->wind_set ? s->wind_override : rng_between(&s->rng, -C_WIND_MAX, C_WIND_MAX);
Branch: 1 branch
BranchTrueFalseTotal
t->wind = s->wind_set ? s->wind_override : rng_between(&s->rng, -C_WIND_MAX, C_WIND_MAX); 564 415 979
567 979 t->phase = PT_PHASE_LIVE; t->turn_tick = 0; t->tick0 = s->tick; t->has_shot = 0; t->shot_tick = -1;
568 979 t->error = PT_OK; t->settled_at = -1; t->last_bits = 0;
569 979 s->nproj = 0;
570 979 s->bodies[active].grace = 0;
571 979 return (int32_t) t->wind;
572 }
573  
574 132659 static int settled(const struct pt_state *s) {
575 int32_t i;
576 187286 ▶ for (i = 0; i < s->nproj; i++) if (s->proj[i].alive) return 0;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < s->nproj; i++) if (s->proj[i].alive) return 0; 89225 54627 143852
for (i = 0; i < s->nproj; i++) if (s->proj[i].alive) return 0; 143852 43434 187286
577 196794 ▶ for (i = 0; i < s->nbodies; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->nbodies; i++) { 195818 976 196794
578 195818 ▶ if (s->bodies[i].alive && (s->bodies[i].mode == PT_FALLING || s->bodies[i].mode == PT_FLYING)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (s->bodies[i].alive && (s->bodies[i].mode == PT_FALLING || s->bodies[i].mode == PT_FLYING)) return 0; 195321 497 195818
if (s->bodies[i].alive && (s->bodies[i].mode == PT_FALLING || s->bodies[i].mode == PT_FLYING)) return 0; 178601 16720 195321
if (s->bodies[i].alive && (s->bodies[i].mode == PT_FALLING || s->bodies[i].mode == PT_FLYING)) return 0; 25738 152863 178601
579 }
580 976 return 1;
581 }
582  
583 /* ---- firing ------------------------------------------------------------------ */
584  
585 1128 static struct proj *spawn(struct pt_state *s, i64 w, i64 x, i64 y, i64 vx, i64 vy, i64 owner, i64 fuse) {
586 struct proj *p;
587 1128 ▶ if (s->nproj >= PT_MAX_PROJECTILES) { s->turn.error = PT_ERR_PROJECTILES; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (s->nproj >= PT_MAX_PROJECTILES) { s->turn.error = PT_ERR_PROJECTILES; return NULL; } 0 1128 1128
588 1128 p = &s->proj[s->nproj++];
589 1128 memset(p, 0, sizeof *p);
590 1128 p->id = s->next_pid++; p->w = w; p->x = x; p->y = y; p->vx = vx; p->vy = vy; p->owner = owner; p->fuse = fuse; p->alive = 1;
591 1128 ▶ trace_row(s, &s->turn.trace_shots, p->id, x, y, w == W_CHILD ? -2 : w);
Branch: 1 branch
BranchTrueFalseTotal
trace_row(s, &s->turn.trace_shots, p->id, x, y, w == W_CHILD ? -2 : w); 893 235 1128
592 1128 return p;
593 }
594  
595 728947 static int visit_cast(struct walk_ctx *c, i64 x, i64 y, i64 px, i64 py) {
596 int32_t i;
597 const struct body *b;
598 (void) px; (void) py;
599 728947 ▶ if (c->kind == 1 && x == c->hx && y == c->hy) { c->kind = 3; return 1; }
Branch: 3 branches
BranchTrueFalseTotal
if (c->kind == 1 && x == c->hx && y == c->hy) { c->kind = 3; return 1; } 10530 718417 728947
if (c->kind == 1 && x == c->hx && y == c->hy) { c->kind = 3; return 1; } 4000 6530 10530
if (c->kind == 1 && x == c->hx && y == c->hy) { c->kind = 3; return 1; } 1921 2079 4000
600 5078992 ▶ for (i = 0; i < c->s->nbodies; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < c->s->nbodies; i++) { 4352098 726894 5078992
601 4352098 b = &c->s->bodies[i];
602 4352098 ▶ if (!b->alive) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!b->alive) continue; 0 4352098 4352098
603 4352098 ▶ if (i == c->owner && !c->owner_counts) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (i == c->owner && !c->owner_counts) continue; 726979 3625119 4352098
if (i == c->owner && !c->owner_counts) continue; 96363 630616 726979
604 4255735 ▶ if (box_has(b, x, y)) { c->kind = 2; c->body = i; c->hx = x; c->hy = y; return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (box_has(b, x, y)) { c->kind = 2; c->body = i; c->hx = x; c->hy = y; return 1; } 132 4255603 4255735
605 }
606 726894 return 0;
607 }
608  
609 /* cast: kind 0 none, 2 a body at hx,hy, 3 terrain at hx,hy with hpx,hpy the last free cell */
610 106525 static void cast(const struct pt_state *s, i64 x0, i64 y0, i64 x1, i64 y1, i64 owner, int owner_counts, struct walk_ctx *c) {
611 struct walk_ctx sw;
612 106525 int hit = swept(s, x0, y0, x1, y1, &sw);
613 106525 memset(c, 0, sizeof *c);
614 106525 c->s = s; c->owner = owner; c->owner_counts = owner_counts;
615 106525 c->kind = hit ? 1 : 0;
616 106525 c->hx = sw.hx; c->hy = sw.hy; c->hpx = sw.hpx; c->hpy = sw.hpy;
617 106525 ▶ if (walk_cells(x0, y0, x1, y1, visit_cast, c)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (walk_cells(x0, y0, x1, y1, visit_cast, c)) return; 2053 104472 106525
618 104472 c->kind = 0; c->hx = x1; c->hy = y1;
619 }
620  
621 static void explode(struct pt_state *s, i64 x, i64 y, i64 w, i64 owner, i64 hx, i64 hy);
622  
623 71 static void hitscan(struct pt_state *s, i64 w, i64 cx, i64 cy, i64 angle, i64 owner) {
624 71 const struct pt_weapon *wp = &WEAPONS[w];
625 71 i64 ex = cx + fx_scale16(fx_from(wp->range), fx_cos(angle));
626 71 i64 ey = cy + fx_scale16(fx_from(wp->range), -fx_sin(angle));
627 71 i64 hx, hy, pid = 0, i;
628 struct walk_ctx r;
629 const struct pt_trace *rows;
630 71 cast(s, fx_shr(cx), fx_shr(cy), fx_shr(ex), fx_shr(ey), owner, 0, &r);
631 71 ▶ if (r.kind == 2) { hx = fx_from(r.hx) + 128; hy = fx_from(r.hy) + 128; }
Branch: 1 branch
BranchTrueFalseTotal
if (r.kind == 2) { hx = fx_from(r.hx) + 128; hy = fx_from(r.hy) + 128; } 4 67 71
632 67 ▶ else if (r.kind == 3) { hx = fx_from(r.hpx) + 128; hy = fx_from(r.hpy) + 128; }
Branch: 1 branch
BranchTrueFalseTotal
else if (r.kind == 3) { hx = fx_from(r.hpx) + 128; hy = fx_from(r.hpy) + 128; } 43 24 67
633 24 else { hx = ex; hy = ey; }
634 71 event(s, PT_EV_RAY, fx_shr(hx), fx_shr(hy));
635 71 rows = (const struct pt_trace *) s->turn.trace_shots.p;
636 71 ▶ for (i = 0; i < s->turn.trace_shots.n; i++) if (rows[i].who + 1 > pid) pid = rows[i].who + 1;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < s->turn.trace_shots.n; i++) if (rows[i].who + 1 > pid) pid = rows[i].who + 1; 0 0 0
for (i = 0; i < s->turn.trace_shots.n; i++) if (rows[i].who + 1 > pid) pid = rows[i].who + 1; 0 71 71
637 71 trace_row(s, &s->turn.trace_shots, pid, cx, cy, w);
638 71 trace_row(s, &s->turn.trace_shots, pid, hx, hy, w);
639 71 ▶ if (r.kind == 2) event(s, PT_EV_HIT, r.body, -1);
Branch: 1 branch
BranchTrueFalseTotal
if (r.kind == 2) event(s, PT_EV_HIT, r.body, -1); 4 67 71
640 71 ▶ if (r.kind != 0) explode(s, hx, hy, w, owner, ex - cx, ey - cy);
Branch: 1 branch
BranchTrueFalseTotal
if (r.kind != 0) explode(s, hx, hy, w, owner, ex - cx, ey - cy); 47 24 71
641 24 else event(s, PT_EV_LOST, -1, 0);
642 71 }
643  
644 965 static int fire(struct pt_state *s, const struct pt_shot *shot) {
645 965 struct turn *t = &s->turn;
646 965 struct body *b = &s->bodies[t->active];
647 965 i64 w = shot->weapon, angle, power, speed, cx, cy, sx, sy, px, py;
648 const struct pt_weapon *wp;
649 struct walk_ctx sw;
650 965 ▶ if (w < 0 || w >= N_WEAPONS) { t->error = PT_ERR_WEAPON; t->phase = PT_PHASE_DONE; event(s, PT_EV_ERROR, 0, 0); return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (w < 0 || w >= N_WEAPONS) { t->error = PT_ERR_WEAPON; t->phase = PT_PHASE_DONE; event(s, PT_EV_ERROR, 0, 0); return 0; } 965 0 965
if (w < 0 || w >= N_WEAPONS) { t->error = PT_ERR_WEAPON; t->phase = PT_PHASE_DONE; event(s, PT_EV_ERROR, 0, 0); return 0; } 1 964 965
651 964 wp = &WEAPONS[w];
652 964 angle = fx_mod(shot->angle, 4096);
653 964 power = fx_max(1, fx_min(100, shot->power));
654 964 t->has_shot = 1; t->shot.weapon = (int32_t) w; t->shot.angle = (int32_t) angle; t->shot.power = (int32_t) power;
655 964 t->shot_tick = t->turn_tick;
656 964 t->phase = PT_PHASE_SETTLE;
657 964 ▶ if (fx_cos(angle) < 0) b->facing = -1; else if (fx_cos(angle) > 0) b->facing = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (fx_cos(angle) < 0) b->facing = -1; else if (fx_cos(angle) > 0) b->facing = 1; 463 501 964
if (fx_cos(angle) < 0) b->facing = -1; else if (fx_cos(angle) > 0) b->facing = 1; 484 17 501
658 964 event(s, PT_EV_FIRE, w, t->active);
659 964 cx = b->x; cy = b->y - fx_from(C_BODY_MID);
660 964 ▶ if (wp->kind == PT_KIND_DROP) {
Branch: 1 branch
BranchTrueFalseTotal
if (wp->kind == PT_KIND_DROP) { 71 893 964
661 71 spawn(s, w, b->x, b->y - fx_from(2), fx_imul(b->facing, 20), -40, t->active, wp->fuse);
662 71 return 1;
663 }
664 893 ▶ if (wp->kind == PT_KIND_HITSCAN) {
Branch: 1 branch
BranchTrueFalseTotal
if (wp->kind == PT_KIND_HITSCAN) { 71 822 893
665 71 hitscan(s, w, cx, cy, angle, t->active);
666 71 return 1;
667 }
668 822 speed = fx_idiv(fx_imul(wp->speed_max, power), 100);
669 822 sx = fx_scale16(speed, fx_cos(angle));
670 822 sy = fx_scale16(speed, -fx_sin(angle));
671 822 px = cx + fx_scale16(C_MUZZLE, fx_cos(angle));
672 822 py = cy + fx_scale16(C_MUZZLE, -fx_sin(angle));
673 822 ▶ if (swept(s, fx_shr(cx), fx_shr(cy), fx_shr(px), fx_shr(py), &sw)) { px = fx_from(sw.hpx) + 128; py = fx_from(sw.hpy) + 128; }
Branch: 1 branch
BranchTrueFalseTotal
if (swept(s, fx_shr(cx), fx_shr(cy), fx_shr(px), fx_shr(py), &sw)) { px = fx_from(sw.hpx) + 128; py = fx_from(sw.hpy) + 128; } 79 743 822
674 822 spawn(s, w, px, py, sx, sy, t->active, wp->fuse);
675 822 return 1;
676 }
677  
678 /* ---- the tick ------------------------------------------------------------------ */
679  
680 static void hurt(struct pt_state *s, struct body *b, i64 idx_, i64 dmg, int32_t *dead, int32_t *ndead);
681 static void death_blast(struct pt_state *s, i64 idx_);
682  
683 4684 static void land(struct pt_state *s, struct body *b, i64 idx_, i64 vy) {
684 4684 ▶ if (vy > C_FALL_HURT) {
Branch: 1 branch
BranchTrueFalseTotal
if (vy > C_FALL_HURT) { 570 4114 4684
685 570 event(s, PT_EV_LAND, idx_, vy);
686 570 hurt(s, b, idx_, fx_idiv(vy - C_FALL_HURT, C_FALL_DIV), NULL, NULL);
687 570 ▶ if (!b->alive) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!b->alive) return; 0 570 570
688 }
689 4684 ▶ if (b->mode == PT_FLYING && vy > C_BOUNCE_MIN) {
Branch: 2 branches
BranchTrueFalseTotal
if (b->mode == PT_FLYING && vy > C_BOUNCE_MIN) { 3835 849 4684
if (b->mode == PT_FLYING && vy > C_BOUNCE_MIN) { 243 3592 3835
690 243 b->vy = -fx_mul(vy, C_BOUNCE);
691 243 b->vx = fx_mul(b->vx, C_FRICTION);
692 243 return;
693 }
694 4441 b->vy = 0;
695 4441 b->vx = fx_mul(b->vx, C_FRICTION);
696 4441 ▶ if (fx_abs(b->vx) < C_STOP) { b->vx = 0; b->mode = PT_STANDING; }
Branch: 1 branch
BranchTrueFalseTotal
if (fx_abs(b->vx) < C_STOP) { b->vx = 0; b->mode = PT_STANDING; } 1943 2498 4441
697 }
698  
699 117978 static void move_x(struct pt_state *s, struct body *b) {
700 117978 i64 nx = b->x + b->vx, cx = cell_x(b), ncx = fx_shr(nx), dir = fx_sign(ncx - cx), c;
701 117978 ▶ if (b->vx == 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (b->vx == 0) return; 55161 62817 117978
702 62817 ▶ if (ncx == cx) { b->x = nx; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (ncx == cx) { b->x = nx; return; } 29718 33099 62817
703 75132 ▶ for (c = cx + dir; c != ncx + dir; c += dir) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = cx + dir; c != ncx + dir; c += dir) { 42800 32332 75132
704 42800 ▶ if (!box_free_at(s, c, b->y)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!box_free_at(s, c, b->y)) { 767 42033 42800
705 767 ▶ b->x = dir > 0 ? fx_from(c - 1) + 255 : fx_from(c);
Branch: 1 branch
BranchTrueFalseTotal
b->x = dir > 0 ? fx_from(c - 1) + 255 : fx_from(c); 390 377 767
706 767 ▶ b->vx = b->mode == PT_FLYING ? -fx_mul(b->vx, C_BOUNCE) : 0;
Branch: 1 branch
BranchTrueFalseTotal
b->vx = b->mode == PT_FLYING ? -fx_mul(b->vx, C_BOUNCE) : 0; 767 0 767
707 767 return;
708 }
709 }
710 32332 b->x = nx;
711 }
712  
713 117978 static void move_y(struct pt_state *s, struct body *b, i64 idx_) {
714 117978 i64 ny = b->y + b->vy, cx = cell_x(b), r, vy = b->vy;
715 117978 ▶ if (b->vy > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->vy > 0) { 89937 28041 117978
716 226260 ▶ for (r = bottom_row(b->y) + 1; r <= fx_shr(ny); r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = bottom_row(b->y) + 1; r <= fx_shr(ny); r++) { 141003 85257 226260
717 141003 ▶ if (row_solid_at(s, cx, r)) { b->y = fx_from(r); land(s, b, idx_, vy); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (row_solid_at(s, cx, r)) { b->y = fx_from(r); land(s, b, idx_, vy); return; } 4680 136323 141003
718 }
719 85257 b->y = ny;
720 85257 return;
721 }
722 28041 ▶ if (b->vy < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->vy < 0) { 27448 593 28041
723 49662 ▶ for (r = top_row(b->y) - 1; r >= top_row(ny); r--) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = top_row(b->y) - 1; r >= top_row(ny); r--) { 22354 27308 49662
724 22354 ▶ if (row_solid_at(s, cx, r)) { b->y = fx_from(r + 1 + C_BODY_H); b->vy = 0; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (row_solid_at(s, cx, r)) { b->y = fx_from(r + 1 + C_BODY_H); b->vy = 0; return; } 140 22214 22354
725 }
726 27308 b->y = ny;
727 27308 return;
728 }
729 593 ▶ if (grounded(s, b)) land(s, b, idx_, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (grounded(s, b)) land(s, b, idx_, 0); 4 589 593
730 }
731  
732 35 static void die_out(struct pt_state *s, struct body *b, i64 idx_) {
733 35 ▶ if (!b->alive) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!b->alive) return; 0 35 35
734 35 b->alive = 0; b->hp = 0;
735 35 event(s, PT_EV_DIE, idx_, 1);
736 35 event(s, PT_EV_OUT, idx_, 0);
737 }
738  
739 3255527 static void step_body(struct pt_state *s, struct body *b, i64 idx_, i64 bits) {
740 3255527 i64 dx, cx, cy, ncx, k, moved, before = b->mode, was_y = b->y, was_x = b->x;
741 3255527 ▶ if (b->mode == PT_STANDING || b->mode == PT_WALKING) {
Branch: 2 branches
BranchTrueFalseTotal
if (b->mode == PT_STANDING || b->mode == PT_WALKING) { 154407 3101120 3255527
if (b->mode == PT_STANDING || b->mode == PT_WALKING) { 37904 116503 154407
742 3139024 b->mode = PT_STANDING;
743 3139024 ▶ if (bits & (PT_LEFT | PT_RIGHT)) {
Branch: 1 branch
BranchTrueFalseTotal
if (bits & (PT_LEFT | PT_RIGHT)) { 74568 3064456 3139024
744 74568 ▶ dx = (bits & PT_LEFT) ? -C_WALK : C_WALK;
Branch: 1 branch
BranchTrueFalseTotal
dx = (bits & PT_LEFT) ? -C_WALK : C_WALK; 36014 38554 74568
745 74568 ▶ b->facing = dx < 0 ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
b->facing = dx < 0 ? -1 : 1; 36014 38554 74568
746 74568 cx = cell_x(b); ncx = fx_shr(b->x + dx);
747 74568 moved = 0;
748 74568 ▶ if (ncx == cx) { b->x += dx; moved = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (ncx == cx) { b->x += dx; moved = 1; } 19489 55079 74568
749 else {
750 201929 ▶ for (k = 0; k <= C_STEP; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k <= C_STEP; k++) { 166095 35834 201929
751 166095 ▶ if (box_free_at(s, ncx, b->y - fx_from(k))) { b->x += dx; b->y -= fx_from(k); moved = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (box_free_at(s, ncx, b->y - fx_from(k))) { b->x += dx; b->y -= fx_from(k); moved = 1; break; } 19245 146850 166095
752 }
753 }
754 74568 ▶ if (moved) {
Branch: 1 branch
BranchTrueFalseTotal
if (moved) { 38734 35834 74568
755 38734 b->mode = PT_WALKING;
756 38734 cx = cell_x(b); cy = cell_y(b);
757 38734 ▶ if (!row_solid_at(s, cx, cy)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!row_solid_at(s, cx, cy)) { 2985 35749 38734
758 4946 ▶ for (k = 1; k <= C_STEP; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 1; k <= C_STEP; k++) { 4594 352 4946
759 4594 ▶ if (row_solid_at(s, cx, cy + k)) { b->y += fx_from(k); break; }
Branch: 1 branch
BranchTrueFalseTotal
if (row_solid_at(s, cx, cy + k)) { b->y += fx_from(k); break; } 2633 1961 4594
760 }
761 }
762 }
763 }
764 3139024 ▶ if (bits & PT_JUMP) {
Branch: 1 branch
BranchTrueFalseTotal
if (bits & PT_JUMP) { 12053 3126971 3139024
765 12053 ▶ if (!b->jump_latch) {
Branch: 1 branch
BranchTrueFalseTotal
if (!b->jump_latch) { 608 11445 12053
766 608 b->jump_latch = 1;
767 608 b->vy = C_JUMP_VY; b->vx = fx_imul(b->facing, C_JUMP_VX); b->mode = PT_FLYING;
768 608 event(s, PT_EV_JUMP, idx_, 0);
769 }
770 3126971 } else b->jump_latch = 0;
771 3139024 ▶ if (b->mode != PT_FLYING && !grounded(s, b)) { b->mode = PT_FALLING; b->vy = 0; b->vx = 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (b->mode != PT_FLYING && !grounded(s, b)) { b->mode = PT_FALLING; b->vy = 0; b->vx = 0; } 3138416 608 3139024
if (b->mode != PT_FLYING && !grounded(s, b)) { b->mode = PT_FALLING; b->vy = 0; b->vx = 0; } 867 3137549 3138416
772 }
773 3255527 ▶ if (b->mode == PT_FALLING || b->mode == PT_FLYING) {
Branch: 2 branches
BranchTrueFalseTotal
if (b->mode == PT_FALLING || b->mode == PT_FLYING) { 3200596 54931 3255527
if (b->mode == PT_FALLING || b->mode == PT_FLYING) { 63047 3137549 3200596
774 117978 b->vy = fx_min(b->vy + C_G, C_TERM);
775 117978 move_x(s, b);
776 117978 move_y(s, b, idx_);
777 }
778 3255527 ▶ if (b->alive && b->y >= fx_from(s->H)) { die_out(s, b, idx_); return; }
Branch: 2 branches
BranchTrueFalseTotal
if (b->alive && b->y >= fx_from(s->H)) { die_out(s, b, idx_); return; } 3255527 0 3255527
if (b->alive && b->y >= fx_from(s->H)) { die_out(s, b, idx_); return; } 11 3255516 3255527
779 3255516 ▶ if (b->alive && (cell_x(b) + C_BODY_R < 0 || cell_x(b) - C_BODY_L >= s->W)) { die_out(s, b, idx_); return; }
Branch: 3 branches
BranchTrueFalseTotal
if (b->alive && (cell_x(b) + C_BODY_R < 0 || cell_x(b) - C_BODY_L >= s->W)) { die_out(s, b, idx_); return; } 3255516 0 3255516
if (b->alive && (cell_x(b) + C_BODY_R < 0 || cell_x(b) - C_BODY_L >= s->W)) { die_out(s, b, idx_); return; } 3255500 16 3255516
if (b->alive && (cell_x(b) + C_BODY_R < 0 || cell_x(b) - C_BODY_L >= s->W)) { die_out(s, b, idx_); return; } 8 3255492 3255500
780 3255492 ▶ if (b->mode != PT_STANDING || before != PT_STANDING || b->x != was_x || b->y != was_y) {
Branch: 4 branches
BranchTrueFalseTotal
if (b->mode != PT_STANDING || before != PT_STANDING || b->x != was_x || b->y != was_y) { 3101588 153904 3255492
if (b->mode != PT_STANDING || before != PT_STANDING || b->x != was_x || b->y != was_y) { 3099008 2580 3101588
if (b->mode != PT_STANDING || before != PT_STANDING || b->x != was_x || b->y != was_y) { 3099008 0 3099008
if (b->mode != PT_STANDING || before != PT_STANDING || b->x != was_x || b->y != was_y) { 0 3099008 3099008
781 156484 trace_row(s, &s->turn.trace_bodies, idx_, b->x, b->y, -1);
782 }
783 }
784  
785 1021 static void detonate(struct pt_state *s, struct proj *p) {
786 1021 p->alive = 0;
787 1021 explode(s, p->x, p->y, p->w, p->owner, p->vx, p->vy);
788 1021 }
789  
790 1325 static void bounce(struct pt_state *s, struct proj *p, const struct walk_ctx *r) {
791 1325 const struct pt_weapon *w = &WEAPONS[p->w];
792 1325 int across_x = r->hx != r->hpx, across_y = r->hy != r->hpy, flip_x = 0, flip_y = 0;
793 1325 ▶ i64 bnc = w->bounce ? w->bounce : 128, fr = w->friction ? w->friction : 200;
Branch: 2 branches
BranchTrueFalseTotal
i64 bnc = w->bounce ? w->bounce : 128, fr = w->friction ? w->friction : 200; 909 416 1325
i64 bnc = w->bounce ? w->bounce : 128, fr = w->friction ? w->friction : 200; 909 416 1325
794 1325 ▶ if (across_x && !across_y) flip_x = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (across_x && !across_y) flip_x = 1; 400 925 1325
if (across_x && !across_y) flip_x = 1; 382 18 400
795 943 ▶ else if (across_y && !across_x) flip_y = 1;
Branch: 2 branches
BranchTrueFalseTotal
else if (across_y && !across_x) flip_y = 1; 943 0 943
else if (across_y && !across_x) flip_y = 1; 925 18 943
796 else {
797 18 ▶ if (solid(s, r->hx, r->hpy)) flip_x = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (solid(s, r->hx, r->hpy)) flip_x = 1; 0 18 18
798 18 ▶ if (solid(s, r->hpx, r->hy)) flip_y = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (solid(s, r->hpx, r->hy)) flip_y = 1; 0 18 18
799 18 ▶ if (!flip_x && !flip_y) { flip_x = 1; flip_y = 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (!flip_x && !flip_y) { flip_x = 1; flip_y = 1; } 18 0 18
if (!flip_x && !flip_y) { flip_x = 1; flip_y = 1; } 18 0 18
800 }
801 1325 ▶ if (flip_x) { p->vx = -fx_mul(p->vx, bnc); p->vy = fx_mul(p->vy, fr); }
Branch: 1 branch
BranchTrueFalseTotal
if (flip_x) { p->vx = -fx_mul(p->vx, bnc); p->vy = fx_mul(p->vy, fr); } 400 925 1325
802 1325 ▶ if (flip_y) {
Branch: 1 branch
BranchTrueFalseTotal
if (flip_y) { 943 382 1325
803 943 ▶ if (p->vy > 0 && p->vy < C_REST_V) { p->vy = 0; p->resting = 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (p->vy > 0 && p->vy < C_REST_V) { p->vy = 0; p->resting = 1; } 856 87 943
if (p->vy > 0 && p->vy < C_REST_V) { p->vy = 0; p->resting = 1; } 181 675 856
804 762 else { p->vy = -fx_mul(p->vy, bnc); p->vx = fx_mul(p->vx, fr); }
805 }
806 1325 p->bounces++;
807 1325 ▶ if (!p->resting || flip_x) event(s, PT_EV_BOUNCE, fx_shr(p->x), fx_shr(p->y));
Branch: 2 branches
BranchTrueFalseTotal
if (!p->resting || flip_x) event(s, PT_EV_BOUNCE, fx_shr(p->x), fx_shr(p->y)); 199 1126 1325
if (!p->resting || flip_x) event(s, PT_EV_BOUNCE, fx_shr(p->x), fx_shr(p->y)); 18 181 199
808 1325 ▶ if (p->bounces > C_MAX_BOUNCES && !p->resting) detonate(s, p);
Branch: 2 branches
BranchTrueFalseTotal
if (p->bounces > C_MAX_BOUNCES && !p->resting) detonate(s, p); 0 1325 1325
if (p->bounces > C_MAX_BOUNCES && !p->resting) detonate(s, p); 0 0 0
809 1325 }
810  
811 47 static void split(struct pt_state *s, struct proj *p) {
812 47 const struct pt_weapon *w = &WEAPONS[p->w];
813 47 i64 k, n = w->count, mid, px = p->x, py = p->y, pvx = p->vx, pvy = p->vy, owner = p->owner;
814 struct proj *c;
815 47 p->alive = 0;
816 47 event(s, PT_EV_SPLIT, fx_shr(p->x), fx_shr(p->y));
817 47 mid = fx_idiv(n, 2);
818 282 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 235 47 282
819 235 c = spawn(s, W_CHILD, px, py, pvx + fx_imul(k - mid, w->spread), pvy - w->pop, owner, 0);
820 235 ▶ if (!c) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!c) return; 0 235 235
821 235 c->age = C_OWNER_GRACE + 1;
822 }
823 }
824  
825 106454 static void step_projectile(struct pt_state *s, int32_t pi) {
826 106454 struct proj *p = &s->proj[pi];
827 106454 const struct pt_weapon *w = &WEAPONS[p->w];
828 i64 nx, ny, cx, cy;
829 struct walk_ctx r;
830 106454 p->age++;
831 106454 cx = fx_shr(p->x); cy = fx_shr(p->y);
832 106454 ▶ if (p->resting) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->resting) { 7546 98908 106454
833 7546 ▶ if (solid(s, cx, cy + 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (solid(s, cx, cy + 1)) { 7546 0 7546
834 7546 p->vx = fx_mul(p->vx, C_ROLL_FRICTION);
835 7546 ▶ if (fx_abs(p->vx) < C_ROLL_STOP) p->vx = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (fx_abs(p->vx) < C_ROLL_STOP) p->vx = 0; 5145 2401 7546
836 7546 p->vy = 0;
837 7546 ▶ if (p->vx <= 0 && !solid(s, cx - 1, cy + 1) && !solid(s, cx - 1, cy)) { p->vx -= C_ROLL_NUDGE; p->resting = 0; }
Branch: 3 branches
BranchTrueFalseTotal
if (p->vx <= 0 && !solid(s, cx - 1, cy + 1) && !solid(s, cx - 1, cy)) { p->vx -= C_ROLL_NUDGE; p->resting = 0; } 7135 411 7546
if (p->vx <= 0 && !solid(s, cx - 1, cy + 1) && !solid(s, cx - 1, cy)) { p->vx -= C_ROLL_NUDGE; p->resting = 0; } 58 7077 7135
if (p->vx <= 0 && !solid(s, cx - 1, cy + 1) && !solid(s, cx - 1, cy)) { p->vx -= C_ROLL_NUDGE; p->resting = 0; } 58 0 58
838 7488 ▶ else if (p->vx >= 0 && !solid(s, cx + 1, cy + 1) && !solid(s, cx + 1, cy)) { p->vx += C_ROLL_NUDGE; p->resting = 0; }
Branch: 3 branches
BranchTrueFalseTotal
else if (p->vx >= 0 && !solid(s, cx + 1, cy + 1) && !solid(s, cx + 1, cy)) { p->vx += C_ROLL_NUDGE; p->resting = 0; } 5556 1932 7488
else if (p->vx >= 0 && !solid(s, cx + 1, cy + 1) && !solid(s, cx + 1, cy)) { p->vx += C_ROLL_NUDGE; p->resting = 0; } 54 5502 5556
else if (p->vx >= 0 && !solid(s, cx + 1, cy + 1) && !solid(s, cx + 1, cy)) { p->vx += C_ROLL_NUDGE; p->resting = 0; } 54 0 54
839 0 } else p->resting = 0;
840 }
841 106454 ▶ if (!p->resting) p->vy = fx_min(p->vy + C_G, C_TERM);
Branch: 1 branch
BranchTrueFalseTotal
if (!p->resting) p->vy = fx_min(p->vy + C_G, C_TERM); 99020 7434 106454
842 106454 ▶ if (w->wind) {
Branch: 1 branch
BranchTrueFalseTotal
if (w->wind) { 38392 68062 106454
843 38392 p->wacc += s->turn.wind;
844 38392 p->vx += fx_idiv(p->wacc, C_WIND_DIV);
845 38392 p->wacc = fx_mod(p->wacc, C_WIND_DIV);
846 }
847 106454 nx = p->x + p->vx; ny = p->y + p->vy;
848 106454 ▶ cast(s, cx, cy, fx_shr(nx), fx_shr(ny), p->owner, w->kind != PT_KIND_DROP && p->age > C_OWNER_GRACE, &r);
Branch: 2 branches
BranchTrueFalseTotal
cast(s, cx, cy, fx_shr(nx), fx_shr(ny), p->owner, w->kind != PT_KIND_DROP && p->age > C_OWNER_GRACE, &r); 94048 12406 106454
cast(s, cx, cy, fx_shr(nx), fx_shr(ny), p->owner, w->kind != PT_KIND_DROP && p->age > C_OWNER_GRACE, &r); 89522 4526 94048
849 106454 ▶ if (r.kind == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (r.kind == 2) { 128 106326 106454
850 128 p->x = fx_from(r.hx) + 128; p->y = fx_from(r.hy) + 128;
851 128 event(s, PT_EV_HIT, r.body, p->id);
852 128 detonate(s, p);
853 766 return;
854 }
855 106326 ▶ if (r.kind == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (r.kind == 3) { 1878 104448 106326
856 1878 p->x = fx_from(r.hpx) + 128; p->y = fx_from(r.hpy) + 128;
857 1878 ▶ if (w->kind == PT_KIND_BOUNCE || w->kind == PT_KIND_DROP) {
Branch: 2 branches
BranchTrueFalseTotal
if (w->kind == PT_KIND_BOUNCE || w->kind == PT_KIND_DROP) { 969 909 1878
if (w->kind == PT_KIND_BOUNCE || w->kind == PT_KIND_DROP) { 416 553 969
858 1325 bounce(s, p, &r);
859 1325 ▶ if (!p->alive) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!p->alive) return; 0 1325 1325
860 } else {
861 553 detonate(s, p);
862 553 return;
863 }
864 } else {
865 104448 p->x = nx; p->y = ny;
866 }
867 105773 ▶ if (fx_shr(p->x) < -64 || fx_shr(p->x) >= s->W + 64 || fx_shr(p->y) >= s->H) {
Branch: 3 branches
BranchTrueFalseTotal
if (fx_shr(p->x) < -64 || fx_shr(p->x) >= s->W + 64 || fx_shr(p->y) >= s->H) { 105746 27 105773
if (fx_shr(p->x) < -64 || fx_shr(p->x) >= s->W + 64 || fx_shr(p->y) >= s->H) { 105716 30 105746
if (fx_shr(p->x) < -64 || fx_shr(p->x) >= s->W + 64 || fx_shr(p->y) >= s->H) { 2 105714 105716
868 59 p->alive = 0; event(s, PT_EV_LOST, p->id, 0); return;
869 }
870 105714 trace_row(s, &s->turn.trace_shots, p->id, p->x, p->y, -1);
871 105714 ▶ if (w->kind == PT_KIND_CLUSTER && p->age > 8 && p->vy >= 0) { split(s, p); return; }
Branch: 3 branches
BranchTrueFalseTotal
if (w->kind == PT_KIND_CLUSTER && p->age > 8 && p->vy >= 0) { split(s, p); return; } 2221 103493 105714
if (w->kind == PT_KIND_CLUSTER && p->age > 8 && p->vy >= 0) { split(s, p); return; } 1755 466 2221
if (w->kind == PT_KIND_CLUSTER && p->age > 8 && p->vy >= 0) { split(s, p); return; } 26 1729 1755
872 105688 ▶ if (p->fuse > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->fuse > 0) { 70166 35522 105688
873 70166 p->fuse--;
874 70166 ▶ if (p->fuse == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->fuse == 0) { 361 69805 70166
875 361 ▶ if (w->kind == PT_KIND_CLUSTER) split(s, p); else detonate(s, p);
Branch: 1 branch
BranchTrueFalseTotal
if (w->kind == PT_KIND_CLUSTER) split(s, p); else detonate(s, p); 21 340 361
876 }
877 }
878 }
879  
880 1071 static void explode(struct pt_state *s, i64 x, i64 y, i64 w, i64 owner, i64 hx, i64 hy) {
881 1071 const struct pt_weapon *wp = &WEAPONS[w];
882 1071 i64 cx = fx_shr(x), cy = fx_shr(y), R = wp->radius + C_KB_REACH, RR = fx_imul(R, R);
883 1071 i64 i, bx, by, dx, dy, d2, d, dmg, kv, kx, ky, hl = 0;
884 struct body *b;
885 struct pt_crater cr;
886 1071 int32_t dead[PT_MAX_BODIES], ndead = 0;
887 (void) owner;
888 1071 ▶ if (hx || hy) hl = fx_isqrt(fx_imul(hx, hx) + fx_imul(hy, hy));
Branch: 2 branches
BranchTrueFalseTotal
if (hx || hy) hl = fx_isqrt(fx_imul(hx, hx) + fx_imul(hy, hy)); 58 1013 1071
if (hx || hy) hl = fx_isqrt(fx_imul(hx, hx) + fx_imul(hy, hy)); 4 54 58
889 1071 carve(s, cx, cy, wp->radius, 0);
890 1071 cr.x = (int32_t) cx; cr.y = (int32_t) cy; cr.r = wp->radius;
891 1071 ▶ if (!vec_push(&s->craters, &cr) || !vec_push(&s->turn.craters, &cr)) s->oom = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (!vec_push(&s->craters, &cr) || !vec_push(&s->turn.craters, &cr)) s->oom = 1; 1071 0 1071
if (!vec_push(&s->craters, &cr) || !vec_push(&s->turn.craters, &cr)) s->oom = 1; 0 1071 1071
892 1071 event(s, PT_EV_EXPLODE, cx, cy);
893 9924 ▶ for (i = 0; i < s->nbodies; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->nbodies; i++) { 8853 1071 9924
894 8853 b = &s->bodies[i];
895 8853 ▶ if (!b->alive) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!b->alive) continue; 3 8850 8853
896 8850 bx = fx_shr(b->x); by = fx_shr(b->y) - C_BODY_MID;
897 8850 dx = bx - cx; dy = by - cy;
898 8850 d2 = fx_imul(dx, dx) + fx_imul(dy, dy);
899 8850 ▶ if (d2 > RR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (d2 > RR) continue; 8320 530 8850
900 530 d = fx_isqrt(d2);
901 530 dmg = fx_idiv(fx_imul(wp->damage, R - d), R);
902 530 kv = fx_idiv(fx_imul(wp->knock, R - d), R);
903 530 ▶ if (d < C_DIRECT_D && hl > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (d < C_DIRECT_D && hl > 0) { 251 279 530
if (d < C_DIRECT_D && hl > 0) { 220 31 251
904 220 kx = fx_idiv(fx_imul(hx, kv), hl);
905 220 ky = -fx_idiv(kv, 2);
906 310 ▶ } else if (d == 0) { kx = 0; ky = -kv; }
Branch: 1 branch
BranchTrueFalseTotal
} else if (d == 0) { kx = 0; ky = -kv; } 0 310 310
907 else {
908 310 kx = fx_idiv(fx_imul(dx, kv), d);
909 310 ky = fx_idiv(fx_imul(dy, kv), d) - fx_idiv(kv, C_KB_LIFT);
910 }
911 530 b->vx += kx; b->vy += ky;
912 530 ▶ if (b->mode != PT_FLYING) b->mode = PT_FLYING;
Branch: 1 branch
BranchTrueFalseTotal
if (b->mode != PT_FLYING) b->mode = PT_FLYING; 507 23 530
913 530 ▶ if (dmg > 0) hurt(s, b, i, dmg, dead, &ndead);
Branch: 1 branch
BranchTrueFalseTotal
if (dmg > 0) hurt(s, b, i, dmg, dead, &ndead); 524 6 530
914 }
915 1074 ▶ for (i = 0; i < ndead; i++) death_blast(s, dead[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ndead; i++) death_blast(s, dead[i]); 3 1071 1074
916 1071 }
917  
918 1094 static void hurt(struct pt_state *s, struct body *b, i64 idx_, i64 dmg, int32_t *dead, int32_t *ndead) {
919 struct pt_grave g;
920 1094 ▶ if (dmg <= 0 || !b->alive) return;
Branch: 2 branches
BranchTrueFalseTotal
if (dmg <= 0 || !b->alive) return; 1092 2 1094
if (dmg <= 0 || !b->alive) return; 0 1092 1092
921 1092 b->hp = fx_max(0, b->hp - dmg);
922 1092 event(s, PT_EV_HURT, idx_, dmg);
923 1092 ▶ if (b->hp == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->hp == 0) { 3 1089 1092
924 3 b->alive = 0;
925 3 event(s, PT_EV_DIE, idx_, 0);
926 3 g.seat = (int32_t) b->seat; g.x = (int32_t) fx_shr(b->x); g.y = (int32_t) fx_shr(b->y);
927 3 ▶ if (!vec_push(&s->graves, &g)) s->oom = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!vec_push(&s->graves, &g)) s->oom = 1; 0 3 3
928 3 ▶ if (dead) dead[(*ndead)++] = (int32_t) idx_; else death_blast(s, idx_);
Branch: 1 branch
BranchTrueFalseTotal
if (dead) dead[(*ndead)++] = (int32_t) idx_; else death_blast(s, idx_); 3 0 3
929 }
930 }
931  
932 3 static void death_blast(struct pt_state *s, i64 idx_) {
933 3 struct body *b = &s->bodies[idx_];
934 3 event(s, PT_EV_BLAST, idx_, 0);
935 3 explode(s, b->x, b->y - fx_from(C_BODY_MID), W_DEATH, idx_, 0, 0);
936 3 }
937  
938 279482 static void step(struct pt_state *s, i64 bits) {
939 int32_t i;
940 442148 ▶ for (i = 0; i < s->nproj; i++) if (s->proj[i].alive) step_projectile(s, i);
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < s->nproj; i++) if (s->proj[i].alive) step_projectile(s, i); 106454 56212 162666
for (i = 0; i < s->nproj; i++) if (s->proj[i].alive) step_projectile(s, i); 162666 279482 442148
941 3535483 ▶ for (i = 0; i < s->nbodies; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->nbodies; i++) { 3256001 279482 3535483
942 3256001 ▶ if (!s->bodies[i].alive) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!s->bodies[i].alive) continue; 474 3255527 3256001
943 3255527 ▶ step_body(s, &s->bodies[i], i, i == s->turn.active && s->turn.phase == PT_PHASE_LIVE ? bits : 0);
Branch: 2 branches
BranchTrueFalseTotal
step_body(s, &s->bodies[i], i, i == s->turn.active && s->turn.phase == PT_PHASE_LIVE ? bits : 0); 279470 2976057 3255527
step_body(s, &s->bodies[i], i, i == s->turn.active && s->turn.phase == PT_PHASE_LIVE ? bits : 0); 146823 132647 279470
944 }
945 279482 }
946  
947 13445312 ▶static int in_box(i64 v) { return v > -BOX && v < BOX; }
Branch: 2 branches
BranchTrueFalseTotal
static int in_box(i64 v) { return v > -BOX && v < BOX; } 13445312 0 13445312
static int in_box(i64 v) { return v > -BOX && v < BOX; } 13445312 0 13445312
948  
949 279482 static int box_ok(const struct pt_state *s) {
950 int32_t i;
951 3535483 ▶ for (i = 0; i < s->nbodies; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->nbodies; i++) { 3256001 279482 3535483
952 3256001 const struct body *b = &s->bodies[i];
953 3256001 ▶ if (!in_box(b->x) || !in_box(b->y) || !in_box(b->vx) || !in_box(b->vy)) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (!in_box(b->x) || !in_box(b->y) || !in_box(b->vx) || !in_box(b->vy)) return 0; 3256001 0 3256001
if (!in_box(b->x) || !in_box(b->y) || !in_box(b->vx) || !in_box(b->vy)) return 0; 3256001 0 3256001
if (!in_box(b->x) || !in_box(b->y) || !in_box(b->vx) || !in_box(b->vy)) return 0; 3256001 0 3256001
if (!in_box(b->x) || !in_box(b->y) || !in_box(b->vx) || !in_box(b->vy)) return 0; 0 3256001 3256001
954 }
955 442148 ▶ for (i = 0; i < s->nproj; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->nproj; i++) { 162666 279482 442148
956 162666 const struct proj *p = &s->proj[i];
957 162666 ▶ if (!p->alive) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!p->alive) continue; 57339 105327 162666
958 105327 ▶ if (!in_box(p->x) || !in_box(p->y) || !in_box(p->vx) || !in_box(p->vy)) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (!in_box(p->x) || !in_box(p->y) || !in_box(p->vx) || !in_box(p->vy)) return 0; 105327 0 105327
if (!in_box(p->x) || !in_box(p->y) || !in_box(p->vx) || !in_box(p->vy)) return 0; 105327 0 105327
if (!in_box(p->x) || !in_box(p->y) || !in_box(p->vx) || !in_box(p->vy)) return 0; 105327 0 105327
if (!in_box(p->x) || !in_box(p->y) || !in_box(p->vx) || !in_box(p->vy)) return 0; 0 105327 105327
959 }
960 279482 return 1;
961 }
962  
963 279484 static int32_t advance(struct pt_state *s, i64 bits, const struct pt_shot *shot) {
964 279484 struct turn *t = &s->turn;
965 struct body *b;
966 struct pt_input in;
967 struct pt_ladder l;
968 uint32_t h[2];
969 279484 ▶ if (t->phase == PT_PHASE_DONE || t->phase == PT_PHASE_IDLE) return (int32_t) t->phase;
Branch: 2 branches
BranchTrueFalseTotal
if (t->phase == PT_PHASE_DONE || t->phase == PT_PHASE_IDLE) return (int32_t) t->phase; 279484 0 279484
if (t->phase == PT_PHASE_DONE || t->phase == PT_PHASE_IDLE) return (int32_t) t->phase; 1 279483 279484
970 279483 b = &s->bodies[t->active];
971 279483 bits = bits & 7;
972 279483 ▶ if (t->phase == PT_PHASE_LIVE) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->phase == PT_PHASE_LIVE) { 147801 131682 279483
973 147801 ▶ if (!b->alive) { t->phase = PT_PHASE_SETTLE; t->shot_tick = t->turn_tick; }
Branch: 1 branch
BranchTrueFalseTotal
if (!b->alive) { t->phase = PT_PHASE_SETTLE; t->shot_tick = t->turn_tick; } 12 147789 147801
974 else {
975 147789 ▶ if (shot) { if (!fire(s, shot)) return (int32_t) t->phase; }
Branch: 2 branches
BranchTrueFalseTotal
if (shot) { if (!fire(s, shot)) return (int32_t) t->phase; } 965 146824 147789
if (shot) { if (!fire(s, shot)) return (int32_t) t->phase; } 1 964 965
976 146824 ▶ else if (t->turn_tick >= s->live_cap) { t->phase = PT_PHASE_SETTLE; t->shot_tick = t->turn_tick; event(s, PT_EV_EXPIRE, t->active, 0); }
Branch: 1 branch
BranchTrueFalseTotal
else if (t->turn_tick >= s->live_cap) { t->phase = PT_PHASE_SETTLE; t->shot_tick = t->turn_tick; event(s, PT_EV_EXPIRE, t->active, 0); } 1 146823 146824
977 }
978 }
979 279482 ▶ if (t->phase == PT_PHASE_LIVE) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->phase == PT_PHASE_LIVE) { 146823 132659 279482
980 146823 ▶ if (bits != t->last_bits) {
Branch: 1 branch
BranchTrueFalseTotal
if (bits != t->last_bits) { 2116 144707 146823
981 2116 in.tick = (int32_t) t->turn_tick; in.bits = (int32_t) bits;
982 2116 ▶ if (!vec_push(&t->inputs, &in)) s->oom = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!vec_push(&t->inputs, &in)) s->oom = 1; 0 2116 2116
983 2116 t->last_bits = bits;
984 }
985 132659 } else bits = 0;
986 279482 step(s, bits);
987 279482 ▶ if (fx_mod(t->turn_tick, C_LADDER) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fx_mod(t->turn_tick, C_LADDER) == 0) { 4782 274700 279482
988 4782 hash_state(s, h);
989 4782 l.tick = (int32_t) t->turn_tick; l.h1 = h[0]; l.h2 = h[1];
990 4782 ▶ if (!vec_push(&t->ladder, &l)) s->oom = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!vec_push(&t->ladder, &l)) s->oom = 1; 0 4782 4782
991 }
992 279482 t->turn_tick++;
993 279482 s->tick++;
994 279482 ▶ if (!box_ok(s)) { t->phase = PT_PHASE_DONE; t->error = PT_ERR_BOX; event(s, PT_EV_ERROR, 0, 0); return (int32_t) t->phase; }
Branch: 1 branch
BranchTrueFalseTotal
if (!box_ok(s)) { t->phase = PT_PHASE_DONE; t->error = PT_ERR_BOX; event(s, PT_EV_ERROR, 0, 0); return (int32_t) t->phase; } 0 279482 279482
995 279482 ▶ if (t->error == PT_ERR_PROJECTILES) { t->phase = PT_PHASE_DONE; event(s, PT_EV_ERROR, 0, 0); return (int32_t) t->phase; }
Branch: 1 branch
BranchTrueFalseTotal
if (t->error == PT_ERR_PROJECTILES) { t->phase = PT_PHASE_DONE; event(s, PT_EV_ERROR, 0, 0); return (int32_t) t->phase; } 0 279482 279482
996 279482 ▶ if (s->oom) { t->phase = PT_PHASE_DONE; t->error = PT_ERR_MEMORY; return (int32_t) t->phase; }
Branch: 1 branch
BranchTrueFalseTotal
if (s->oom) { t->phase = PT_PHASE_DONE; t->error = PT_ERR_MEMORY; return (int32_t) t->phase; } 0 279482 279482
997 279482 ▶ if (t->phase == PT_PHASE_SETTLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->phase == PT_PHASE_SETTLE) { 132659 146823 279482
998 132659 ▶ if (settled(s)) { t->phase = PT_PHASE_DONE; t->settled_at = t->turn_tick; }
Branch: 1 branch
BranchTrueFalseTotal
if (settled(s)) { t->phase = PT_PHASE_DONE; t->settled_at = t->turn_tick; } 976 131683 132659
999 131683 ▶ else if (t->turn_tick - t->shot_tick > s->settle_cap) { t->phase = PT_PHASE_DONE; t->error = PT_ERR_TICK_CAP; event(s, PT_EV_ERROR, 0, 0); }
Branch: 1 branch
BranchTrueFalseTotal
else if (t->turn_tick - t->shot_tick > s->settle_cap) { t->phase = PT_PHASE_DONE; t->error = PT_ERR_TICK_CAP; event(s, PT_EV_ERROR, 0, 0); } 1 131682 131683
1000 }
1001 279482 return (int32_t) t->phase;
1002 }
1003  
1004 /* ---- the outcome ------------------------------------------------------------- */
1005  
1006 5826 static void *dup_vec(const struct vec *v) {
1007 5826 ▶ void *p = malloc(v->n ? (size_t) v->n * v->sz : 1);
Branch: 1 branch
BranchTrueFalseTotal
void *p = malloc(v->n ? (size_t) v->n * v->sz : 1); 5164 662 5826
1008 5826 ▶ if (p && v->n) memcpy(p, v->p, (size_t) v->n * v->sz);
Branch: 2 branches
BranchTrueFalseTotal
if (p && v->n) memcpy(p, v->p, (size_t) v->n * v->sz); 5826 0 5826
if (p && v->n) memcpy(p, v->p, (size_t) v->n * v->sz); 5164 662 5826
1009 5826 return p;
1010 }
1011  
1012 971 struct pt_outcome *pt_outcome(const struct pt_state *s) {
1013 971 const struct turn *t = &s->turn;
1014 971 struct pt_outcome *o = (struct pt_outcome *) calloc(1, sizeof *o);
1015 int32_t i;
1016 971 ▶ if (!o) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!o) return NULL; 0 971 971
1017 971 o->seed = s->seed; o->active = (int32_t) t->active; o->wind = (int32_t) t->wind;
1018 971 o->ticks = (int32_t) t->turn_tick; o->settled_at = (int32_t) t->settled_at;
1019 971 ▶ o->error = s->oom ? PT_ERR_MEMORY : t->error;
Branch: 1 branch
BranchTrueFalseTotal
o->error = s->oom ? PT_ERR_MEMORY : t->error; 971 0 971
1020 971 o->has_shot = t->has_shot; o->shot_tick = (int32_t) t->shot_tick; o->shot = t->shot;
1021 971 o->ninputs = t->inputs.n; o->inputs = (struct pt_input *) dup_vec(&t->inputs);
1022 971 o->nevents = t->events.n; o->events = (struct pt_event *) dup_vec(&t->events);
1023 971 o->ncraters = t->craters.n; o->craters = (struct pt_crater *) dup_vec(&t->craters);
1024 971 o->ntrace_bodies = t->trace_bodies.n; o->trace_bodies = (struct pt_trace *) dup_vec(&t->trace_bodies);
1025 971 o->ntrace_shots = t->trace_shots.n; o->trace_shots = (struct pt_trace *) dup_vec(&t->trace_shots);
1026 971 o->nladder = t->ladder.n; o->ladder = (struct pt_ladder *) dup_vec(&t->ladder);
1027 971 o->end_tick = (int32_t) s->tick; o->end_craters = s->craters.n;
1028 971 o->nbodies = s->nbodies;
1029 971 ▶ o->bodies = (struct pt_body *) malloc(sizeof(struct pt_body) * (size_t) (s->nbodies ? s->nbodies : 1));
Branch: 1 branch
BranchTrueFalseTotal
o->bodies = (struct pt_body *) malloc(sizeof(struct pt_body) * (size_t) (s->nbodies ? s->nbodies : 1)); 971 0 971
1030 971 ▶ if (!o->inputs || !o->events || !o->craters || !o->trace_bodies || !o->trace_shots || !o->ladder || !o->bodies) { pt_outcome_free(o); return NULL; }
Branch: 7 branches
BranchTrueFalseTotal
if (!o->inputs || !o->events || !o->craters || !o->trace_bodies || !o->trace_shots || !o->ladder || !o->bodies) { pt_outcome_free(o); return NULL; } 971 0 971
if (!o->inputs || !o->events || !o->craters || !o->trace_bodies || !o->trace_shots || !o->ladder || !o->bodies) { pt_outcome_free(o); return NULL; } 971 0 971
if (!o->inputs || !o->events || !o->craters || !o->trace_bodies || !o->trace_shots || !o->ladder || !o->bodies) { pt_outcome_free(o); return NULL; } 971 0 971
if (!o->inputs || !o->events || !o->craters || !o->trace_bodies || !o->trace_shots || !o->ladder || !o->bodies) { pt_outcome_free(o); return NULL; } 971 0 971
if (!o->inputs || !o->events || !o->craters || !o->trace_bodies || !o->trace_shots || !o->ladder || !o->bodies) { pt_outcome_free(o); return NULL; } 971 0 971
if (!o->inputs || !o->events || !o->craters || !o->trace_bodies || !o->trace_shots || !o->ladder || !o->bodies) { pt_outcome_free(o); return NULL; } 971 0 971
if (!o->inputs || !o->events || !o->craters || !o->trace_bodies || !o->trace_shots || !o->ladder || !o->bodies) { pt_outcome_free(o); return NULL; } 0 971 971
1031 8218 ▶ for (i = 0; i < s->nbodies; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->nbodies; i++) { 7247 971 8218
1032 7247 const struct body *b = &s->bodies[i];
1033 7247 struct pt_body *ob = &o->bodies[i];
1034 7247 ob->seat = (int32_t) b->seat; ob->k = (int32_t) b->k; ob->x = (int32_t) b->x; ob->y = (int32_t) b->y;
1035 7247 ob->vx = (int32_t) b->vx; ob->vy = (int32_t) b->vy; ob->mode = (int32_t) b->mode; ob->hp = (int32_t) b->hp;
1036 7247 ob->alive = (int32_t) b->alive; ob->facing = (int32_t) b->facing;
1037 }
1038 971 return o;
1039 }
1040  
1041 971 void pt_outcome_free(struct pt_outcome *o) {
1042 971 ▶ if (!o) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!o) return; 0 971 971
1043 971 free(o->inputs); free(o->events); free(o->craters); free(o->trace_bodies); free(o->trace_shots); free(o->ladder); free(o->bodies);
1044 971 free(o);
1045 }
1046  
1047 771 struct pt_outcome *pt_run_turn(struct pt_state *s, int32_t active, int32_t ninputs, const struct pt_input *inputs, const struct pt_shot *shot, int32_t shot_tick) {
1048 771 int32_t cursor = 0, bits = 0, fired = 0, guard = s->live_cap + s->settle_cap + 8;
1049 const struct pt_shot *now;
1050 771 ▶ if (start_turn(s, active) < 0 && s->turn.phase != PT_PHASE_LIVE) {
Branch: 2 branches
BranchTrueFalseTotal
if (start_turn(s, active) < 0 && s->turn.phase != PT_PHASE_LIVE) { 95 676 771
if (start_turn(s, active) < 0 && s->turn.phase != PT_PHASE_LIVE) { 0 95 95
1051 0 s->turn.error = PT_ERR_STATE;
1052 0 return pt_outcome(s);
1053 }
1054 185594 ▶ while (s->turn.phase != PT_PHASE_DONE) {
Branch: 1 branch
BranchTrueFalseTotal
while (s->turn.phase != PT_PHASE_DONE) { 184823 771 185594
1055 185949 ▶ while (cursor < ninputs && inputs[cursor].tick <= s->turn.turn_tick) { bits = inputs[cursor].bits; cursor++; }
Branch: 2 branches
BranchTrueFalseTotal
while (cursor < ninputs && inputs[cursor].tick <= s->turn.turn_tick) { bits = inputs[cursor].bits; cursor++; } 70736 115213 185949
while (cursor < ninputs && inputs[cursor].tick <= s->turn.turn_tick) { bits = inputs[cursor].bits; cursor++; } 1126 69610 70736
1056 184823 now = NULL;
1057 184823 ▶ if (!fired && shot && shot_tick == s->turn.turn_tick) { fired = 1; now = shot; }
Branch: 3 branches
BranchTrueFalseTotal
if (!fired && shot && shot_tick == s->turn.turn_tick) { fired = 1; now = shot; } 79884 104939 184823
if (!fired && shot && shot_tick == s->turn.turn_tick) { fired = 1; now = shot; } 79853 31 79884
if (!fired && shot && shot_tick == s->turn.turn_tick) { fired = 1; now = shot; } 764 79089 79853
1058 184823 advance(s, bits, now);
1059 184823 ▶ if (--guard < 0) { s->turn.error = PT_ERR_TICK_CAP; s->turn.phase = PT_PHASE_DONE; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (--guard < 0) { s->turn.error = PT_ERR_TICK_CAP; s->turn.phase = PT_PHASE_DONE; break; } 0 184823 184823
1060 }
1061 771 return pt_outcome(s);
1062 }
1063  
1064 /* ---- the state's life ---------------------------------------------------------- */
1065  
1066 955 struct pt_state *pt_state_new(const struct pt_opts *opts) {
1067 955 struct pt_state *s = (struct pt_state *) calloc(1, sizeof *s);
1068 struct pt_gen g;
1069 955 ▶ if (!s) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return NULL; 0 955 955
1070 955 vec_init(&s->craters, sizeof(struct pt_crater));
1071 955 vec_init(&s->graves, sizeof(struct pt_grave));
1072 955 turn_init(&s->turn);
1073 955 s->seed = opts->seed;
1074 955 ▶ s->teams = opts->teams > 0 ? opts->teams : 2;
Branch: 1 branch
BranchTrueFalseTotal
s->teams = opts->teams > 0 ? opts->teams : 2; 955 0 955
1075 955 ▶ s->per_team = opts->per_team > 0 ? opts->per_team : 4;
Branch: 1 branch
BranchTrueFalseTotal
s->per_team = opts->per_team > 0 ? opts->per_team : 4; 955 0 955
1076 955 s->wind_set = opts->wind_set; s->wind_override = opts->wind;
1077 955 ▶ s->live_cap = opts->live_cap > 0 ? opts->live_cap : PT_LIVE_CAP;
Branch: 1 branch
BranchTrueFalseTotal
s->live_cap = opts->live_cap > 0 ? opts->live_cap : PT_LIVE_CAP; 1 954 955
1078 955 ▶ s->settle_cap = opts->settle_cap > 0 ? opts->settle_cap : PT_SETTLE_CAP;
Branch: 1 branch
BranchTrueFalseTotal
s->settle_cap = opts->settle_cap > 0 ? opts->settle_cap : PT_SETTLE_CAP; 1 954 955
1079 955 g = DEFAULT_GEN;
1080 955 ▶ if (opts->use_gen) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts->use_gen) { 617 338 955
1081 617 ▶ if (opts->gen.profile >= 0) g.profile = opts->gen.profile;
Branch: 1 branch
BranchTrueFalseTotal
if (opts->gen.profile >= 0) g.profile = opts->gen.profile; 617 0 617
1082 617 ▶ if (opts->gen.W > 0) g.W = opts->gen.W;
Branch: 1 branch
BranchTrueFalseTotal
if (opts->gen.W > 0) g.W = opts->gen.W; 1 616 617
1083 617 ▶ if (opts->gen.H > 0) g.H = opts->gen.H;
Branch: 1 branch
BranchTrueFalseTotal
if (opts->gen.H > 0) g.H = opts->gen.H; 1 616 617
1084 617 ▶ if (opts->gen.coarse > 0) g.coarse = opts->gen.coarse;
Branch: 1 branch
BranchTrueFalseTotal
if (opts->gen.coarse > 0) g.coarse = opts->gen.coarse; 0 617 617
1085 617 ▶ if (opts->gen.fine > 0) g.fine = opts->gen.fine;
Branch: 1 branch
BranchTrueFalseTotal
if (opts->gen.fine > 0) g.fine = opts->gen.fine; 0 617 617
1086 617 ▶ if (opts->gen.cave > 0) g.cave = opts->gen.cave;
Branch: 1 branch
BranchTrueFalseTotal
if (opts->gen.cave > 0) g.cave = opts->gen.cave; 0 617 617
1087 617 ▶ if (opts->gen.threshold > 0) g.threshold = opts->gen.threshold;
Branch: 1 branch
BranchTrueFalseTotal
if (opts->gen.threshold > 0) g.threshold = opts->gen.threshold; 0 617 617
1088 617 ▶ if (opts->gen.bias_scale > 0) g.bias_scale = opts->gen.bias_scale;
Branch: 1 branch
BranchTrueFalseTotal
if (opts->gen.bias_scale > 0) g.bias_scale = opts->gen.bias_scale; 0 617 617
1089 617 ▶ if (opts->gen.bias_offset > 0) g.bias_offset = opts->gen.bias_offset;
Branch: 1 branch
BranchTrueFalseTotal
if (opts->gen.bias_offset > 0) g.bias_offset = opts->gen.bias_offset; 0 617 617
1090 617 ▶ if (opts->gen.cave_lo > 0) g.cave_lo = opts->gen.cave_lo;
Branch: 1 branch
BranchTrueFalseTotal
if (opts->gen.cave_lo > 0) g.cave_lo = opts->gen.cave_lo; 0 617 617
1091 617 ▶ if (opts->gen.cave_hi > 0) g.cave_hi = opts->gen.cave_hi;
Branch: 1 branch
BranchTrueFalseTotal
if (opts->gen.cave_hi > 0) g.cave_hi = opts->gen.cave_hi; 0 617 617
1092 617 ▶ if (opts->gen.cave_top > 0) g.cave_top = opts->gen.cave_top;
Branch: 1 branch
BranchTrueFalseTotal
if (opts->gen.cave_top > 0) g.cave_top = opts->gen.cave_top; 0 617 617
1093 617 ▶ if (opts->gen.cave_bottom > 0) g.cave_bottom = opts->gen.cave_bottom;
Branch: 1 branch
BranchTrueFalseTotal
if (opts->gen.cave_bottom > 0) g.cave_bottom = opts->gen.cave_bottom; 0 617 617
1094 617 ▶ if (opts->gen.platform > 0) g.platform = opts->gen.platform;
Branch: 1 branch
BranchTrueFalseTotal
if (opts->gen.platform > 0) g.platform = opts->gen.platform; 0 617 617
1095 617 ▶ if (opts->gen.headroom > 0) g.headroom = opts->gen.headroom;
Branch: 1 branch
BranchTrueFalseTotal
if (opts->gen.headroom > 0) g.headroom = opts->gen.headroom; 0 617 617
1096 617 g.floor = opts->gen.floor;
1097 }
1098 955 ▶ if (g.W < 16 || g.H < 16 || g.W > 8192 || g.H > 8192) { pt_state_free(s); return NULL; }
Branch: 4 branches
BranchTrueFalseTotal
if (g.W < 16 || g.H < 16 || g.W > 8192 || g.H > 8192) { pt_state_free(s); return NULL; } 955 0 955
if (g.W < 16 || g.H < 16 || g.W > 8192 || g.H > 8192) { pt_state_free(s); return NULL; } 955 0 955
if (g.W < 16 || g.H < 16 || g.W > 8192 || g.H > 8192) { pt_state_free(s); return NULL; } 955 0 955
if (g.W < 16 || g.H < 16 || g.W > 8192 || g.H > 8192) { pt_state_free(s); return NULL; } 0 955 955
1099 955 ▶ if (!generate(s, opts->seed, &g)) { pt_state_free(s); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!generate(s, opts->seed, &g)) { pt_state_free(s); return NULL; } 0 955 955
1100 955 ▶ if (opts->nsculpt > 0 && opts->sculpt) sculpt(s, opts->nsculpt, opts->sculpt);
Branch: 2 branches
BranchTrueFalseTotal
if (opts->nsculpt > 0 && opts->sculpt) sculpt(s, opts->nsculpt, opts->sculpt); 540 415 955
if (opts->nsculpt > 0 && opts->sculpt) sculpt(s, opts->nsculpt, opts->sculpt); 540 0 540
1101 955 s->rng = seed32(mix32(s->seed, 0x5bd1e995u));
1102 955 ▶ if (opts->place == PT_PLACE_EXPLICIT) place_bodies(s, opts->nbodies, opts->bodies);
Branch: 1 branch
BranchTrueFalseTotal
if (opts->place == PT_PLACE_EXPLICIT) place_bodies(s, opts->nbodies, opts->bodies); 566 389 955
1103 389 ▶ else if (opts->place == PT_PLACE_TEAMS) { if (!place_teams(s) && s->error != PT_ERR_BODIES) { pt_state_free(s); return NULL; } }
Branch: 3 branches
BranchTrueFalseTotal
else if (opts->place == PT_PLACE_TEAMS) { if (!place_teams(s) && s->error != PT_ERR_BODIES) { pt_state_free(s); return NULL; } } 237 152 389
else if (opts->place == PT_PLACE_TEAMS) { if (!place_teams(s) && s->error != PT_ERR_BODIES) { pt_state_free(s); return NULL; } } 0 237 237
else if (opts->place == PT_PLACE_TEAMS) { if (!place_teams(s) && s->error != PT_ERR_BODIES) { pt_state_free(s); return NULL; } } 0 0 0
1104 955 return s;
1105 }
1106  
1107 955 void pt_state_free(struct pt_state *s) {
1108 955 ▶ if (!s) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return; 0 955 955
1109 955 free(s->mask); free(s->scorch);
1110 955 vec_drop(&s->craters); vec_drop(&s->graves);
1111 955 turn_drop(&s->turn);
1112 955 free(s);
1113 }
1114  
1115 /* ---- snapshots ------------------------------------------------------------------ */
1116  
1117 201 static struct pt_snapshot *snapshot_new(const struct pt_state *s) {
1118 201 struct pt_snapshot *n = (struct pt_snapshot *) calloc(1, sizeof *n);
1119 size_t cells;
1120 201 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 201 201
1121 201 cells = (size_t) fx_imul(s->W, s->H);
1122 201 n->W = s->W; n->H = s->H;
1123 201 n->mask = (uint8_t *) malloc(cells); n->scorch = (uint8_t *) malloc(cells);
1124 201 vec_init(&n->craters, sizeof(struct pt_crater)); vec_init(&n->graves, sizeof(struct pt_grave));
1125 201 ▶ if (!n->mask || !n->scorch || !vec_copy(&n->craters, &s->craters) || !vec_copy(&n->graves, &s->graves)) {
Branch: 4 branches
BranchTrueFalseTotal
if (!n->mask || !n->scorch || !vec_copy(&n->craters, &s->craters) || !vec_copy(&n->graves, &s->graves)) { 201 0 201
if (!n->mask || !n->scorch || !vec_copy(&n->craters, &s->craters) || !vec_copy(&n->graves, &s->graves)) { 201 0 201
if (!n->mask || !n->scorch || !vec_copy(&n->craters, &s->craters) || !vec_copy(&n->graves, &s->graves)) { 201 0 201
if (!n->mask || !n->scorch || !vec_copy(&n->craters, &s->craters) || !vec_copy(&n->graves, &s->graves)) { 0 201 201
1126 0 free(n->mask); free(n->scorch); vec_drop(&n->craters); vec_drop(&n->graves); free(n);
1127 0 return NULL;
1128 }
1129 201 memcpy(n->mask, s->mask, cells); memcpy(n->scorch, s->scorch, cells);
1130 201 n->nbodies = s->nbodies; memcpy(n->bodies, s->bodies, sizeof s->bodies);
1131 201 n->rng = s->rng; n->tick = s->tick; n->next_pid = s->next_pid;
1132 201 return n;
1133 }
1134  
1135 201 static int32_t snapshot_restore(struct pt_state *s, const struct pt_snapshot *n) {
1136 size_t cells;
1137 201 ▶ if (n->W != s->W || n->H != s->H) return -PT_ERR_STATE;
Branch: 2 branches
BranchTrueFalseTotal
if (n->W != s->W || n->H != s->H) return -PT_ERR_STATE; 200 1 201
if (n->W != s->W || n->H != s->H) return -PT_ERR_STATE; 0 200 200
1138 200 cells = (size_t) fx_imul(s->W, s->H);
1139 200 memcpy(s->mask, n->mask, cells); memcpy(s->scorch, n->scorch, cells);
1140 200 s->nbodies = n->nbodies; memcpy(s->bodies, n->bodies, sizeof s->bodies);
1141 200 ▶ if (!vec_copy(&s->craters, &n->craters) || !vec_copy(&s->graves, &n->graves)) return -PT_ERR_MEMORY;
Branch: 2 branches
BranchTrueFalseTotal
if (!vec_copy(&s->craters, &n->craters) || !vec_copy(&s->graves, &n->graves)) return -PT_ERR_MEMORY; 200 0 200
if (!vec_copy(&s->craters, &n->craters) || !vec_copy(&s->graves, &n->graves)) return -PT_ERR_MEMORY; 0 200 200
1142 200 s->rng = n->rng; s->tick = n->tick; s->next_pid = n->next_pid;
1143 200 s->nproj = 0;
1144 200 s->turn.phase = PT_PHASE_IDLE;
1145 200 s->error = PT_OK; s->oom = 0;
1146 200 return 0;
1147 }
1148  
1149 201 static void snapshot_free(struct pt_snapshot *n) {
1150 201 ▶ if (!n) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return; 0 201 201
1151 201 free(n->mask); free(n->scorch); vec_drop(&n->craters); vec_drop(&n->graves); free(n);
1152 }
1153  
1154 /* ---- the table -------------------------------------------------------------------- */
1155  
1156 1 static int32_t abi_width(const struct pt_state *s) { return s->W; }
1157 1 static int32_t abi_height(const struct pt_state *s) { return s->H; }
1158 1 static uint32_t abi_seed(const struct pt_state *s) { return s->seed; }
1159 0 static int32_t abi_tick(const struct pt_state *s) { return (int32_t) s->tick; }
1160 0 static int32_t abi_teams(const struct pt_state *s) { return s->teams; }
1161 0 static int32_t abi_per_team(const struct pt_state *s) { return s->per_team; }
1162 33289 static int32_t abi_solid(const struct pt_state *s, int32_t x, int32_t y) { return (int32_t) solid(s, x, y); }
1163 5 static int32_t abi_swept(const struct pt_state *s, int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t out[4]) {
1164 struct walk_ctx c;
1165 5 int hit = swept(s, x0, y0, x1, y1, &c);
1166 5 out[0] = (int32_t) c.hx; out[1] = (int32_t) c.hy; out[2] = (int32_t) c.hpx; out[3] = (int32_t) c.hpy;
1167 5 return hit;
1168 }
1169 48 ▶static int32_t abi_carve(struct pt_state *s, int32_t x, int32_t y, int32_t r) { return r < 0 ? 0 : (int32_t) carve(s, x, y, r, 0); }
Branch: 1 branch
BranchTrueFalseTotal
static int32_t abi_carve(struct pt_state *s, int32_t x, int32_t y, int32_t r) { return r < 0 ? 0 : (int32_t) carve(s, x, y, r, 0); } 47 1 48
1170 772 static void abi_sculpt(struct pt_state *s, int32_t n, const struct pt_sculpt *ops) { sculpt(s, n, ops); }
1171 1 static int32_t abi_surface_at(const struct pt_state *s, int32_t x, int32_t y_from) { return (int32_t) surface_at(s, x, y_from); }
1172 52 static int32_t abi_count(const struct pt_state *s) {
1173 52 i64 n = fx_imul(s->W, s->H), i, c = 0;
1174 42598452 ▶ for (i = 0; i < n; i++) if (s->mask[i]) c++;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n; i++) if (s->mask[i]) c++; 38779698 3818702 42598400
for (i = 0; i < n; i++) if (s->mask[i]) c++; 42598400 52 42598452
1175 52 return (int32_t) c;
1176 }
1177 136 static size_t abi_mask_bytes(const struct pt_state *s) { return (size_t) fx_idiv(fx_imul(s->W, s->H) + 7, 8); }
1178 136 static void abi_mask_pack(const struct pt_state *s, uint8_t *buf) {
1179 136 i64 n = fx_imul(s->W, s->H), i, bit = 0, o = 0;
1180 136 uint8_t byte = 0;
1181 111411336 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 111411200 136 111411336
1182 111411200 ▶ if (s->mask[i]) byte |= (uint8_t) (1 << bit);
Branch: 1 branch
BranchTrueFalseTotal
if (s->mask[i]) byte |= (uint8_t) (1 << bit); 44744181 66667019 111411200
1183 111411200 bit++;
1184 111411200 ▶ if (bit == 8) { buf[o++] = byte; byte = 0; bit = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (bit == 8) { buf[o++] = byte; byte = 0; bit = 0; } 13926400 97484800 111411200
1185 }
1186 136 ▶ if (bit) buf[o] = byte;
Branch: 1 branch
BranchTrueFalseTotal
if (bit) buf[o] = byte; 0 136 136
1187 136 }
1188 33 static int32_t abi_add_body(struct pt_state *s, int32_t seat, int32_t cx, int32_t cy) { return (int32_t) add_body(s, seat, cx, cy); }
1189 95044 static int32_t abi_body_count(const struct pt_state *s) { return s->nbodies; }
1190 1520642 static int32_t abi_body_get(const struct pt_state *s, int32_t i, struct pt_body *out) {
1191 const struct body *b;
1192 1520642 ▶ if (i < 0 || i >= s->nbodies) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (i < 0 || i >= s->nbodies) return -1; 1520642 0 1520642
if (i < 0 || i >= s->nbodies) return -1; 0 1520642 1520642
1193 1520642 b = &s->bodies[i];
1194 1520642 out->seat = (int32_t) b->seat; out->k = (int32_t) b->k; out->x = (int32_t) b->x; out->y = (int32_t) b->y;
1195 1520642 out->vx = (int32_t) b->vx; out->vy = (int32_t) b->vy; out->mode = (int32_t) b->mode; out->hp = (int32_t) b->hp;
1196 1520642 out->alive = (int32_t) b->alive; out->facing = (int32_t) b->facing;
1197 1520642 return 0;
1198 }
1199 210 static int32_t abi_start_turn(struct pt_state *s, int32_t active) { return start_turn(s, active); }
1200 94661 static int32_t abi_advance(struct pt_state *s, int32_t bits, const struct pt_shot *shot) { return advance(s, bits, shot); }
1201 1 static int32_t abi_phase(const struct pt_state *s) { return (int32_t) s->turn.phase; }
1202 600459 static int32_t abi_turn_tick(const struct pt_state *s) { return (int32_t) s->turn.turn_tick; }
1203 3 ▶static int32_t abi_error(const struct pt_state *s) { return s->oom ? PT_ERR_MEMORY : (s->turn.error ? s->turn.error : s->error); }
Branch: 2 branches
BranchTrueFalseTotal
static int32_t abi_error(const struct pt_state *s) { return s->oom ? PT_ERR_MEMORY : (s->turn.error ? s->turn.error : s->error); } 3 0 3
static int32_t abi_error(const struct pt_state *s) { return s->oom ? PT_ERR_MEMORY : (s->turn.error ? s->turn.error : s->error); } 1 2 3
1204 400 static void abi_hash(const struct pt_state *s, uint32_t out[2]) { hash_state(s, out); }
1205 11 static int32_t abi_launch(int32_t weapon, int32_t angle, int32_t power, int32_t out[2]) {
1206 const struct pt_weapon *w;
1207 i64 a, p, speed;
1208 11 ▶ if (weapon < 0 || weapon >= N_WEAPONS) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (weapon < 0 || weapon >= N_WEAPONS) return -1; 11 0 11
if (weapon < 0 || weapon >= N_WEAPONS) return -1; 1 10 11
1209 10 w = &WEAPONS[weapon];
1210 10 a = fx_mod(angle, 4096);
1211 10 p = fx_max(1, fx_min(100, power));
1212 10 speed = fx_idiv(fx_imul(w->speed_max, p), 100);
1213 10 out[0] = (int32_t) fx_scale16(speed, fx_cos(a));
1214 10 out[1] = (int32_t) fx_scale16(speed, -fx_sin(a));
1215 10 return 0;
1216 }
1217 1 static int32_t abi_weapon_count(void) { return N_WEAPONS; }
1218 1026 ▶static const struct pt_weapon *abi_weapon_get(int32_t i) { return (i >= 0 && i < (int32_t) (sizeof WEAPONS / sizeof WEAPONS[0])) ? &WEAPONS[i] : NULL; }
Branch: 2 branches
BranchTrueFalseTotal
static const struct pt_weapon *abi_weapon_get(int32_t i) { return (i >= 0 && i < (int32_t) (sizeof WEAPONS / sizeof WEAPONS[0])) ? &WEAPONS[i] : NULL; } 1026 0 1026
static const struct pt_weapon *abi_weapon_get(int32_t i) { return (i >= 0 && i < (int32_t) (sizeof WEAPONS / sizeof WEAPONS[0])) ? &WEAPONS[i] : NULL; } 1026 0 1026
1219 0 static int32_t abi_craters(const struct pt_state *s, const struct pt_crater **out) { *out = (const struct pt_crater *) s->craters.p; return s->craters.n; }
1220 0 static int32_t abi_graves(const struct pt_state *s, const struct pt_grave **out) { *out = (const struct pt_grave *) s->graves.p; return s->graves.n; }
1221  
1222 static const char *const EVENT_NAMES[PT_EV_COUNT] = {
1223 "jump", "fire", "ray", "hit", "explode", "bounce", "split", "lost", "hurt", "die", "out", "land", "blast", "expire", "error"
1224 };
1225 5836 ▶static const char *abi_event_name(int32_t kind) { return (kind >= 0 && kind < PT_EV_COUNT) ? EVENT_NAMES[kind] : "unknown"; }
Branch: 2 branches
BranchTrueFalseTotal
static const char *abi_event_name(int32_t kind) { return (kind >= 0 && kind < PT_EV_COUNT) ? EVENT_NAMES[kind] : "unknown"; } 5836 0 5836
static const char *abi_event_name(int32_t kind) { return (kind >= 0 && kind < PT_EV_COUNT) ? EVENT_NAMES[kind] : "unknown"; } 5836 0 5836
1226 974 static const char *abi_error_name(int32_t err) {
1227 974 switch (err) {
1228 970 case PT_OK: return NULL;
1229 1 case PT_ERR_TICK_CAP: return "tick cap";
1230 2 case PT_ERR_WEAPON: return "weapon";
1231 1 case PT_ERR_BODIES: return "bodies";
1232 0 case PT_ERR_PROJECTILES: return "projectiles";
1233 0 case PT_ERR_BOX: return "box";
1234 0 case PT_ERR_MEMORY: return "memory";
1235 0 case PT_ERR_STATE: return "state";
1236 0 default: return "unknown";
1237 }
1238 }
1239  
1240 static const struct pt_abi PT_ABI = {
1241 PT_ABI_VERSION,
1242 pt_state_new, pt_state_free,
1243 abi_width, abi_height, abi_seed, abi_tick, abi_teams, abi_per_team,
1244 abi_solid, abi_swept, abi_carve, abi_sculpt, abi_surface_at, abi_count, abi_mask_bytes, abi_mask_pack,
1245 abi_add_body, abi_body_count, abi_body_get,
1246 abi_start_turn, abi_advance, abi_phase, abi_turn_tick, abi_error,
1247 pt_outcome, pt_outcome_free, pt_run_turn,
1248 abi_hash,
1249 snapshot_new, snapshot_restore, snapshot_free,
1250 abi_launch, abi_weapon_count, abi_weapon_get,
1251 abi_craters, abi_graves, abi_event_name, abi_error_name
1252 };
1253  
1254 2358199 const struct pt_abi *pt_abi_table(void) { return &PT_ABI; }
« Terrain.xs ↑ Summary blib/lib/Physics/Terrain.pm »