Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

Hill Climb Racing: the Volcano as smooth as the other stages

Mason3636356e2438a10
diff --git a/games/hillclimb/src/box2d/Box2D/Common/b2Math.cpp b/games/hillclimb/src/box2d/Box2D/Common/b2Math.cpp
index 4974fe18..19811d5b 100644
--- a/games/hillclimb/src/box2d/Box2D/Common/b2Math.cpp
+++ b/games/hillclimb/src/box2d/Box2D/Common/b2Math.cpp
@@ -20,6 +20,24 @@
const b2Vec2 b2Vec2_zero(0.0f, 0.0f);
+/// (NumPlay: see b2SinCos)
+void b2SinCosFar(float32 angle, float32* s, float32* c)
+{
+ static struct { float32 a, s, c; } memo[64]; // (a is 0 in the empty ones: never a far angle)
+ union { float32 f; uint32 u; } bits;
+ bits.f = angle;
+ int32 i = (int32)((bits.u * 2654435761u) >> 26);
+ if (memo[i].a != angle)
+ {
+ float32 sa = sinf(angle), ca = cosf(angle); // (as b2Rot did: some compilers make it one sincosf)
+ memo[i].a = angle;
+ memo[i].s = sa;
+ memo[i].c = ca;
+ }
+ *s = memo[i].s;
+ *c = memo[i].c;
+}
+
/// Solve A * x = b, where b is a column vector. This is more efficient
/// than computing the inverse in one-shot cases.
b2Vec3 b2Mat33::Solve33(const b2Vec3& b) const
diff --git a/games/hillclimb/src/box2d/Box2D/Common/b2Math.h b/games/hillclimb/src/box2d/Box2D/Common/b2Math.h
index 9808258b..47d47765 100644
--- a/games/hillclimb/src/box2d/Box2D/Common/b2Math.h
+++ b/games/hillclimb/src/box2d/Box2D/Common/b2Math.h
@@ -306,6 +306,21 @@ struct b2Mat33
b2Vec3 ex, ey, ez;
};
+/// (changed for NumPlay: sinf and cosf take a slow path past 201 radians, which a wheel's angle passes after a few
+/// hundred meters, and each step asks for the same angles several times; those far ones are remembered, the same
+/// angle giving the very same values)
+void b2SinCosFar(float32 angle, float32* s, float32* c);
+inline void b2SinCos(float32 angle, float32* s, float32* c)
+{
+ if (angle > 201.0f || angle < -201.0f)
+ {
+ b2SinCosFar(angle, s, c);
+ return;
+ }
+ *s = sinf(angle);
+ *c = cosf(angle);
+}
+
/// Rotation
struct b2Rot
{
@@ -314,17 +329,13 @@ struct b2Rot
/// Initialize from an angle in radians
explicit b2Rot(float32 angle)
{
- /// TODO_ERIN optimize
- s = sinf(angle);
- c = cosf(angle);
+ b2SinCos(angle, &s, &c);
}
/// Set using an angle in radians.
void Set(float32 angle)
{
- /// TODO_ERIN optimize
- s = sinf(angle);
- c = cosf(angle);
+ b2SinCos(angle, &s, &c);
}
/// Set to the identity rotation
diff --git a/games/hillclimb/src/gfx.c b/games/hillclimb/src/gfx.c
index bcebabfe..247586dd 100644
--- a/games/hillclimb/src/gfx.c
+++ b/games/hillclimb/src/gfx.c
@@ -114,6 +114,8 @@ static void blit(const Img *im, int x, int y, int alpha) {
void gfx_image(int id, float x, float y, float scale, float angle, int alpha) {
const Img *im = img(id);
if (scale <= 0.01f) return;
+ /* (a wheel's angle grows with the distance, and sinf and cosf get slow past 200 or so) */
+ if (angle > 6.2831853f || angle < -6.2831853f) angle = fmodf(angle, 6.2831853f);
if (angle == 0 && scale > 0.999f && scale < 1.001f) {
blit(im, (int)floorf(x - im->px4 / 4.0f + 0.5f), (int)floorf(y - im->py4 / 4.0f + 0.5f), alpha);
return;
diff --git a/games/hillclimb/src/run.c b/games/hillclimb/src/run.c
index 5ae33b84..87db086b 100644
--- a/games/hillclimb/src/run.c
+++ b/games/hillclimb/src/run.c
@@ -153,7 +153,7 @@ static void pickups(void) {
}
}
}
- for (int k = 0; k < 512; k++) {
+ for (int k = stage_fuel_first(xmin); k < 512; k++) {
float fx, fy;
if (!stage_fuel(k, &fx, &fy) || fx > xmax) break;
if (fx < xmin || run.fuel_got[k >> 3] >> (k & 7) & 1) continue;
@@ -442,7 +442,7 @@ static void draw_things(void) {
}
}
/* fuel cans */
- for (int k = 0; k < 512; k++) {
+ for (int k = stage_fuel_first(x0); k < 512; k++) {
float fx, fy;
if (!stage_fuel(k, &fx, &fy) || fx > x1) break;
if (fx < x0 || run.fuel_got[k >> 3] >> (k & 7) & 1) continue;
diff --git a/games/hillclimb/src/stage.c b/games/hillclimb/src/stage.c
index 63a1ae18..837a9ff9 100644
--- a/games/hillclimb/src/stage.c
+++ b/games/hillclimb/src/stage.c
@@ -221,7 +221,15 @@ static float gp_x0[MAXGAP], gp_len[MAXGAP], gp_y[MAXGAP], gp_shift[MAXGAP];
static int gp_n; /* worked out so far */
/* the stage's own height with the bridges or pits so far taken out */
-static float base_height(float x) { return heights[stage_id](x); }
+#ifdef HOST
+long stage_evals; /* (tests: how often it was worked out) */
+#endif
+static float base_height(float x) {
+#ifdef HOST
+ stage_evals++;
+#endif
+ return heights[stage_id](x);
+}
static float gap_start(void) { return stage->pits ? 110 : 120; }
@@ -427,10 +435,17 @@ static float coin_floor(float x) {
return h;
}
+/* the coins' heights cost the stage's formula three times a coin, and the run asks for the chunks near the car every
+ * step and every frame: they are kept for the last few chunks */
+#define CCACHE 8
+static int ckey[CCACHE];
+static float cval[CCACHE][14];
+
int stage_coin_group(int k, float *xs, float *ys, int *vals) {
if (k < 1 || k >= COIN_GROUPS) return 0;
float X = (float)k * COIN_SPACING;
- int left = (int)(X * stage->coin_mult), n = 0, cols = 0, row = 1;
+ int left = (int)(X * stage->coin_mult), n = 0, cols = 0, row = 1, slot = k & (CCACHE - 1);
+ bool have = ckey[slot] == k;
while (left > 0) {
int v = left > 500 ? 500 : left > 100 ? 100 : left > 25 ? 25 : left > 5 ? 5 : 0;
if (!v) break;
@@ -439,9 +454,11 @@ int stage_coin_group(int k, float *xs, float *ys, int *vals) {
else if (row == 1) cols = 1, col = 0, row = 2;
else break;
float x = X + 4.5f + (float)col * 1.5f;
- xs[n] = x, ys[n] = coin_floor(x) + ((float)row + stage->coin_off) * 1.5f, vals[n] = v, n++;
+ if (!have) cval[slot][n] = coin_floor(x) + ((float)row + stage->coin_off) * 1.5f;
+ xs[n] = x, ys[n] = cval[slot][n], vals[n] = v, n++;
left -= v;
}
+ ckey[slot] = k;
return n;
}
@@ -459,10 +476,14 @@ static int fuel_density(int n) {
#define MAXCAN 512
static int can_chunk[MAXCAN], can_n, can_scan, can_count, can_dens;
-
-bool stage_fuel(int k, float *x, float *y) {
- if (k < 0 || k >= MAXCAN) return false;
- while (can_n <= k && can_scan < COIN_GROUPS) {
+/* (the last cans' heights, as the coins') */
+#define FCACHE 4
+static int fkey[FCACHE];
+static float fval[FCACHE];
+
+/* the cans up to the k-th worked out, or all there are */
+static void cans_upto(int k) {
+ while (can_n <= k && can_n < MAXCAN && can_scan < COIN_GROUPS) {
can_scan++; /* the chunk at 30 can_scan m */
can_count = (can_count + 1) % can_dens;
if (can_count == 0) {
@@ -470,12 +491,35 @@ bool stage_fuel(int k, float *x, float *y) {
can_dens = fuel_density(can_n);
}
}
+}
+
+bool stage_fuel(int k, float *x, float *y) {
+ if (k < 0 || k >= MAXCAN) return false;
+ cans_upto(k);
if (k >= can_n) return false;
float cx = (float)can_chunk[k] * COIN_SPACING + 1.5f;
- *x = cx, *y = coin_floor(cx) + (1 + stage->coin_off) * 1.5f;
+ int slot = k & (FCACHE - 1);
+ if (fkey[slot] != k) fkey[slot] = k, fval[slot] = coin_floor(cx) + (1 + stage->coin_off) * 1.5f;
+ *x = cx, *y = fval[slot];
return true;
}
+int stage_fuel_first(float x) {
+ /* (a can is 1.5 m into its chunk) */
+ int lo = 0, hi;
+ for (;;) {
+ cans_upto(can_n);
+ hi = can_n;
+ if (!hi || (float)can_chunk[hi - 1] * COIN_SPACING + 1.5f >= x || hi >= MAXCAN || can_scan >= COIN_GROUPS) break;
+ }
+ while (lo < hi) {
+ int m = (lo + hi) / 2;
+ if ((float)can_chunk[m] * COIN_SPACING + 1.5f < x) lo = m + 1;
+ else hi = m;
+ }
+ return lo;
+}
+
/* ------------------------------------------------------------------ levels */
uint32_t stage_level_goal(int n) { return stage->lvl_base + stage->lvl_step * (uint32_t)n; }
uint32_t stage_level_bonus(int n) { return stage->lvl_mult * stage_level_goal(n) + 2500; }
@@ -489,4 +533,6 @@ void stage_set(int s) {
tr_n = 0, tr_count = 0, tr_group = 0, tr_next = 30;
can_n = 0, can_scan = 0, can_count = 0, can_dens = fuel_density(0);
for (int i = 0; i < VCACHE; i++) vkey[i] = rkey[i] = 0x7FFFFFFF;
+ for (int i = 0; i < CCACHE; i++) ckey[i] = -1;
+ for (int i = 0; i < FCACHE; i++) fkey[i] = -1;
}
diff --git a/games/hillclimb/src/stage.h b/games/hillclimb/src/stage.h
index f1c0b7ff..2b406c8d 100644
--- a/games/hillclimb/src/stage.h
+++ b/games/hillclimb/src/stage.h
@@ -78,6 +78,7 @@ void stage_tree_fall(int k);
int stage_coin_group(int k, float *x, float *y, int *value);
/* fuel cans: the k-th one's place, k from 0; false past the last */
bool stage_fuel(int k, float *x, float *y);
+int stage_fuel_first(float x); /* the first can at or after x */
/* levels (StageInfo::getLevelGoal, getLevelBonus): where level n (0, 1...) ends, and its bonus */
uint32_t stage_level_goal(int n);
uint32_t stage_level_bonus(int n);
diff --git a/games/hillclimb/tests/check.py b/games/hillclimb/tests/check.py
index 0fb9952d..60178db8 100644
--- a/games/hillclimb/tests/check.py
+++ b/games/hillclimb/tests/check.py
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
"""Checks of Hill Climb Racing on a computer (make test): the host build, with AddressSanitizer and UBSan, through
-the menus, buying, every stage's ground, every stage with every vehicle, each vehicle's numbers, the ends of a run,
-the saves (and the old versions' carried over) and random keys.
+the menus, buying, every stage's ground, every stage with every vehicle, each vehicle's numbers, how often the ground
+is worked out, the ends of a run, the saves (and the old versions' carried over) and random keys.
python3 tests/check.py build/play [--quick]"""
import os
@@ -212,6 +212,15 @@ def main():
if s:
check(s["dist"] > 400, f"90 s on {STAGES[st]} with the {VEHICLES[v]}: only {s['dist']} m")
+ # the ground's formula (on the Volcano, sines of hundreds of radians: slow on the calculator) is worked out for new
+ # ground only, not for every coin and fuel can at every step and drawing as in 1.16.0 (140 times a frame); the
+ # player driving here asks for it twice a frame
+ d = fresh()
+ p = play(["--run", "12,8", "--levels", "12,12,12,10", "--auto", "--draw", "--frames", "3600"], d)
+ m = re.search(r"EVALS (\d+)", p.stdout)
+ check(m and int(m.group(1)) < 3600 * 4, f"the Volcano's ground worked out {m.group(1) if m else '?'} times in 3600 frames")
+ shutil.rmtree(d, ignore_errors=True)
+
# the ends of a run: Bill's neck (the bike flipped on the Moon, gas held) and an empty tank (reversing)
d = fresh()
s = run(["--run", "5,1", "--keys", "0-1500:right", "--frames", "1500"], d, "driver down")
diff --git a/games/hillclimb/tests/play.c b/games/hillclimb/tests/play.c
index 8704dc4e..9aaf2d60 100644
--- a/games/hillclimb/tests/play.c
+++ b/games/hillclimb/tests/play.c
@@ -2,15 +2,16 @@
*
* play [--frames N] [--keys "10-40:right,50:ok,..."] [--shots 30,60|every:N] [--out DIR] [--saves DIR]
* [--run STAGE,VEHICLE] [--levels E,S,T,A] [--coins N] [--auto] [--trace N] [--fuzz SEED]
- * [--numbers VEHICLE] [--ground STAGE]
+ * [--numbers VEHICLE] [--ground STAGE] [--draw]
*
* Keys: left right up down ok back home. "a-b:key" holds the key from frame a to b, "a:key" for one frame.
* --run starts a run on that stage with that vehicle, past the menus; --auto then drives it like a player would
* (gas, and the pedals to land on the wheels). Screenshots are DIR/shot_<frame>.ppm.
* --fuzz presses random keys (its own generator, the same on every computer) through menus and runs; --numbers
* prints the numbers a vehicle is built with (at --levels); --ground checks a stage's ground, bridges, pits and
- * coins over 6 km.
- * At the end it prints the state: END mode M dist D coins C total T over O end E level L flips F. */
+ * coins over 6 km; --draw draws every frame (without saving it), as the calculator does.
+ * At the end it prints how often the stage's ground formula was worked out (EVALS N), then the state: END mode M
+ * dist D coins C total T over O end E level L flips F. */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
@@ -155,7 +156,7 @@ static int ground_check(int s) {
int main(int argc, char **argv) {
int frames = 600, run_stage = -1, run_vehicle = 0, trace = 0, numbers = -1;
bool fuzz = false;
- bool autop = false;
+ bool autop = false, draw_all = false;
const char *out = "build/shots";
int levels[4] = {1, 1, 1, 1};
long coins = -1;
@@ -179,6 +180,7 @@ int main(int argc, char **argv) {
else if (!strcmp(argv[i], "--coins")) coins = atol(argv[++i]);
else if (!strcmp(argv[i], "--auto")) autop = true;
else if (!strcmp(argv[i], "--nohud")) g_nohud = true;
+ else if (!strcmp(argv[i], "--draw")) draw_all = true;
else if (!strcmp(argv[i], "--flips")) autop = flipper = true;
else if (!strcmp(argv[i], "--trace")) trace = atoi(argv[++i]);
else if (!strcmp(argv[i], "--fuzz")) fuzz = true, fuzz_state = (uint32_t)atol(argv[++i]);
@@ -248,6 +250,7 @@ int main(int argc, char **argv) {
host_time += 17;
bool shot = every && f >= every_from && f <= every_to ? (f - every_from) % every == 0 : false;
for (int i = 0; i < nshots; i++) shot |= shots[i] == f;
+ if (draw_all && !shot) game_draw();
if (shot) {
game_draw();
char p[512];
@@ -259,8 +262,9 @@ int main(int argc, char **argv) {
run.car.ch.y, run.car.ch.a, car_speed(&run.car), run.fuel, run.car.air, run.car.dead,
run.dist, (unsigned)run_total());
}
- extern long b2_heap_peak;
+ extern long b2_heap_peak, stage_evals;
fprintf(stderr, "heap peak %ld\n", b2_heap_peak);
+ printf("EVALS %ld\n", stage_evals);
printf("END mode %d dist %d coins %u total %u over %d end %d level %d flips %u saved %u running %d\n", g_mode,
(int)run.dist, (unsigned)run.coins, (unsigned)run_total(), run.over, run.end, run.level, (unsigned)run.flips,
(unsigned)sv.coins, g_running);