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.

PolyTrack: the data from Kodub's web build, the car's physics and the 3D picture

Mason3634bbbfdbfb922
diff --git a/games/polygt3d/src/game.h b/games/polygt3d/src/game.h
deleted file mode 100644
index f0b70f42..00000000
--- a/games/polygt3d/src/game.h
+++ /dev/null
@@ -1,146 +0,0 @@
-/* PolyGT 3D's rules: the roads (track.c) and the race (race.c), in whole numbers only, so that a race comes out the
- * same on the calculator and on every computer. The picture (render.c) is drawn from them with floats.
- *
- * Distances are in 1/256 metre (FX), speeds in 1/256 metre a second, angles in 1/65536 of a turn; the race moves on
- * in steps of 20 ms. A car is placed along the road (s, from the start line) and across it (x, + to the right), and
- * points at `ang` from the road's direction; the road turns right where its heading grows. */
-#ifndef PG_GAME_H
-#define PG_GAME_H
-#include <stdbool.h>
-#include <stdint.h>
-
-#define FX 256
-#define PG_TICK 20 /* ms per step */
-#define PG_HZ 50
-#define PG_CARS 8 /* you and up to seven rivals */
-#define PG_CIRCUITS 3
-#define PG_VEHICLES 5
-#define PG_MAXN 288 /* segments in a circuit, at most */
-#define PG_RING 128 /* the endless road: the segments kept around you (a power of two) */
-#define PG_SEG (6 * FX) /* the endless road's segments; circuits come close to it */
-#define PG_ROAD (8 * FX) /* half the road */
-#define PG_KERB (6 * FX / 5)
-#define PG_WALL (25 * FX / 2) /* the barriers, either side */
-#define PG_HL (22 * FX / 10) /* half a car's length... */
-#define PG_HW (115 * FX / 100) /* ...and half its width */
-#define PG_ZONE 70 /* the endless road changes scenery every 70 segments (420 m) */
-#define PG_ORBS 24 /* nitro orbs on a circuit, at most */
-#define PG_MAX_LAPS 9
-#define PG_GHOST 480 /* samples of a lap, every 200 ms: 96 seconds */
-#define ANG_TURN 65536
-
-enum { MODE_RACE, MODE_ENDLESS };
-enum { LEVEL_EASY, LEVEL_NORMAL, LEVEL_HARD };
-enum { VIEW_CHASE, VIEW_FAR, VIEW_BUMPER, VIEWS };
-enum { PH_GRID, PH_RACE, PH_DONE };
-enum { END_NONE, END_FINISH, END_TIME };
-enum { IN_GAS = 1, IN_BRAKE = 2, IN_LEFT = 4, IN_RIGHT = 8, IN_NITRO = 16 };
-enum { BANNER_NONE, BANNER_LAP, BANNER_FINAL, BANNER_ZONE, BANNER_WRONG };
-/* what stands at a segment */
-enum { PROP_NONE, PROP_TREE, PROP_ARCH, PROP_GANTRY, PROP_CHEVRON };
-
-/* ------------------------------------------------------------------ fixed-point helpers (track.c) */
-int32_t pg_sin(int32_t a); /* 32767 = 1 */
-int32_t pg_cos(int32_t a);
-int32_t pg_atan2(int32_t y, int32_t x); /* the angle of (x, y), x along 0 */
-uint32_t pg_isqrt(uint64_t v);
-uint32_t pg_rng(uint32_t *s); /* xorshift32; s must not be 0 */
-
-/* ------------------------------------------------------------------ the roads (track.c) */
-typedef struct {
- uint32_t rng;
- int32_t head; /* heading of the segment being laid */
- int32_t turn, turn_to; /* its bend, and the bend it eases into (1/16 angle unit a segment) */
- int32_t slope, slope_to; /* climb a segment (FX) */
- int16_t turn_hold, slope_hold;
- uint8_t zone, spare;
- int32_t count; /* segments laid */
- int32_t x, y, z; /* the end of the road so far */
- int32_t prev_head; /* the heading of the segment before */
-} pg_gen;
-
-typedef struct {
- uint8_t endless, id; /* id: the circuit, or the scenery the endless road starts in */
- uint16_t n; /* circuit: segments in a lap */
- int32_t seg; /* a segment's length */
- int32_t length; /* circuit: a lap, n * seg */
- int32_t px[PG_MAXN], py[PG_MAXN], pz[PG_MAXN]; /* where each segment starts (its node) */
- uint16_t nh[PG_MAXN]; /* the road's heading at the node */
- int16_t cv[PG_MAXN]; /* how much it turns over the segment */
- uint8_t limit[PG_MAXN]; /* the fastest a car with grip 1 takes the segment's bend (m/s) */
- uint8_t prop[PG_MAXN]; /* PROP_..., + 8 on the left */
- uint8_t zone[PG_MAXN]; /* the scenery (endless: changes along the road) */
- int8_t orb[PG_MAXN]; /* a nitro orb at the segment's middle: its x in 1/8 metre, or -128 */
- int8_t orb_id[PG_MAXN]; /* circuit: the orb's number, for its cooldown */
- uint8_t orbs; /* circuit: orbs on a lap */
- int32_t first, last; /* endless: the segments kept, by number from the start */
- pg_gen gen;
-} pg_track;
-
-extern const char *const pg_circuit_name[PG_CIRCUITS];
-extern const char *const pg_circuit_note[PG_CIRCUITS];
-extern const char *const pg_zone_name[5];
-void pg_build(pg_track *t, int id); /* a circuit */
-void pg_endless(pg_track *t, uint32_t seed); /* the endless road, from its start */
-void pg_extend(pg_track *t, int32_t upto); /* the endless road laid on to segment `upto` (and kept from
- there back), or nothing for a circuit */
-int pg_slot(const pg_track *t, int32_t k); /* the arrays' index of segment k (any lap) */
-int32_t pg_seg_of(const pg_track *t, int32_t s); /* the segment number under s (negative too) */
-int32_t pg_heading_at(const pg_track *t, int32_t s);
-int32_t pg_height_at(const pg_track *t, int32_t s);
-void pg_world(const pg_track *t, int32_t s, int32_t x, int32_t *wx, int32_t *wy, int32_t *wz);
-int32_t pg_wrap(const pg_track *t, int32_t ds); /* a circuit gap brought within half a lap either way */
-int pg_zone_at(const pg_track *t, int32_t s);
-int pg_map(const pg_track *t, int n, int16_t *xs, int16_t *ys, int w, int h); /* the outline, fit in w x h */
-
-/* ------------------------------------------------------------------ cars (race.c) */
-typedef struct {
- const char *name, *kind;
- uint16_t top; /* top speed, 1/8 m/s */
- uint16_t accel; /* 1/8 m/s^2 */
- uint8_t grip, drift; /* 1/128 */
- uint8_t offroad; /* grass drag, 1/128 */
- uint8_t horse;
- uint16_t redline;
- uint8_t body[3], accent[3];
-} pg_vehicle;
-extern const pg_vehicle pg_vehicles[PG_VEHICLES];
-
-typedef struct {
- int32_t s, x, v; /* along (unwound: laps add up), across, speed */
- int32_t lane; /* a rival: the x it heads for */
- int16_t ang; /* where it heads, from the road's direction */
- int16_t slip; /* the body turned from where it heads (a slide) */
- int16_t steer; /* -1024..1024 */
- uint16_t nitro; /* 0..1000 */
- uint32_t finish_ms; /* at the line (0: not yet) */
- uint16_t think, aim; /* a rival: steps to its next look ahead, the speed it aims for (FX / 8) */
- uint8_t gear, boost, drift, model, paint, place, bump;
- uint8_t driven; /* by the game: the rivals, and you after the line */
-} pg_car;
-
-typedef struct {
- uint8_t mode, track, vehicle, laps, rivals, level, manual, phase;
- uint8_t end, lap, finished, banner, banner_arg, zone, wrong, flash; /* flash: an orb just taken */
- uint16_t countdown, done, banner_t, slipstream;
- uint32_t ticks, time_ms, lap_at, best_lap_ms, last_lap_ms, seed;
- int32_t time_left; /* endless: ms left */
- int32_t last_orb; /* endless: the last orb taken (its segment) */
- uint16_t orb_wait[PG_ORBS]; /* circuit: steps before an orb is back */
- pg_car car[PG_CARS]; /* 0 is you */
-} pg_race;
-
-typedef struct {
- uint8_t mode, track, vehicle, laps, rivals, level, manual;
-} pg_setup;
-
-void pg_new(pg_race *r, pg_track *t, const pg_setup *s, uint32_t seed);
-void pg_step(pg_race *r, pg_track *t, unsigned input, int shift);
-bool pg_valid(const pg_race *r, const pg_track *t);
-int pg_rank(const pg_race *r, int car); /* 1.. : by finish, then by distance */
-int pg_kmh(int32_t v);
-int pg_rpm(const pg_race *r);
-int32_t pg_lap_s(const pg_race *r, const pg_track *t); /* where you are in the lap (or on the endless road) */
-int32_t pg_zone_bonus(int zone); /* ms a zone of the endless road gives */
-uint32_t pg_sum(const void *data, uint32_t n);
-#endif
diff --git a/games/polygt3d/src/race.c b/games/polygt3d/src/race.c
deleted file mode 100644
index 1f36faa1..00000000
--- a/games/polygt3d/src/race.c
+++ /dev/null
@@ -1,588 +0,0 @@
-/* The race: your car and the rivals' on the road, a step of 20 ms at a time. Whole numbers only (game.h).
- *
- * Your car goes where it points; steering turns it as far as its tyres hold (24 m/s^2 sideways at grip 1, less on
- * the grass and on the touge), and a bend you don't steer through carries you to its outside. Braking while you
- * steer hard at speed slides the car: a tighter line for less braking. The engine pulls through six gears, nitro
- * adds a push and top speed, and driving close behind a car pulls you along. The rivals keep to their lines, brake
- * for the bends at three levels, pass slower cars and take orbs. Cars never go through each other or the barriers.
- */
-#include <string.h>
-#include "game.h"
-
-const pg_vehicle pg_vehicles[PG_VEHICLES] = {
- /* name, kind, top speed and pull (1/8), grip, slide, grass (1/128), horse, redline, body, accent */
- {"Apex GT", "Rear drive, twin turbo", 81 * 8, 88, 128, 147, 128, 0, 8500, {225, 32, 42}, {245, 190, 40}},
- {"Proto R", "Mid engine, sharp grip", 79 * 8, 82, 146, 115, 128, 0, 9000, {240, 240, 245}, {220, 30, 35}},
- {"Rally IV", "Four-wheel drive", 76 * 8, 96, 138, 134, 64, 0, 7800, {35, 105, 230}, {245, 205, 45}},
- {"Black Stallion", "A horse: quick off the mark", 72 * 8, 104, 153, 138, 80, 1, 9200, {24, 24, 32}, {255, 45, 35}},
- {"Brown Mustang", "A horse: sure-footed", 70 * 8, 100, 157, 134, 72, 1, 8800, {148, 84, 42}, {225, 175, 55}},
-};
-
-/* grip of the road in each scenery (1/128): the three circuits, then the endless road's five */
-static const uint8_t ZONE_GRIP[8] = {128, 133, 118, 128, 125, 133, 119, 123};
-static const uint16_t TORQUE[18] = {206, 206, 211, 223, 235, 245, 255, 262, 268, 272, 274, 274, 272, 268, 262, 254,
- 245, 236};
-static const uint16_t PULL[6] = {346, 289, 211, 165, 133, 133};
-static const uint8_t GEAR_TOP[6] = {22, 35, 48, 60, 71, 84}; /* each gear's top, in 80ths of the car's */
-
-#define MS(x) ((int32_t)((x) * FX)) /* metres (a second, a second squared) */
-#define A_LAT MS(24)
-#define BRAKE MS(28)
-#define DRIFT_BRAKE MS(12)
-#define AI_BRAKE MS(20)
-#define NITRO_A MS(7)
-#define NITRO_TOP MS(13.5)
-#define DRAFT_A MS(2.5)
-#define DRAFT_TOP MS(5)
-#define DRAG0 MS(2.1)
-#define TURN_MAX 14394 /* 1.38 radians a second, at most */
-#define RAD 10430 /* angle units in a radian */
-#define COUNTDOWN (3 * PG_HZ + PG_HZ / 2)
-#define ORB_WAIT 275 /* steps before an orb comes back (5.5 s) */
-#define GRID_GAP MS(8)
-
-static int32_t clampi(int32_t v, int32_t lo, int32_t hi) { return v < lo ? lo : v > hi ? hi : v; }
-static int32_t absi(int32_t v) { return v < 0 ? -v : v; }
-
-uint32_t pg_sum(const void *data, uint32_t n) {
- const uint8_t *p = data;
- uint32_t h = 2166136261u;
- for (uint32_t i = 0; i < n; i++) h = (h ^ p[i]) * 16777619u;
- return h;
-}
-
-int pg_kmh(int32_t v) { return (int)((int64_t)v * 36 / (10 * FX)); }
-
-static int32_t top_of(const pg_car *c) { return (int32_t)pg_vehicles[c->model].top * FX / 8; }
-static int32_t gear_max(const pg_car *c, int g) { return top_of(c) * GEAR_TOP[g - 1] / 80; }
-
-int pg_rpm(const pg_race *r) {
- const pg_car *c = &r->car[0];
- int red = pg_vehicles[c->model].redline;
- int32_t norm = (int32_t)((int64_t)c->v * 256 / gear_max(c, c->gear));
- int rpm = 1250 + (int)((int64_t)norm * (red - 1250) / 256);
- return rpm > red + 200 ? red + 200 : rpm;
-}
-
-/* the engine's push in the car's gear (FX/s^2), skill: 256 = all of it */
-static int32_t engine(const pg_car *c, int skill) {
- int32_t gm = gear_max(c, c->gear);
- if (c->v >= gm + gm / 20) return 0; /* on the limiter */
- int32_t norm = (int32_t)((int64_t)c->v * 16 / gm);
- norm = clampi(norm, 0, 17);
- int64_t a = (int64_t)pg_vehicles[c->model].accel * FX / 8;
- return (int32_t)(a * TORQUE[norm] / 256 * PULL[c->gear - 1] / 256 * skill / 256);
-}
-
-/* air and rolling: none of the engine's push is left at the car's top speed in top gear */
-static int32_t drag(const pg_car *c) {
- int32_t top = top_of(c);
- int64_t a = (int64_t)pg_vehicles[c->model].accel * FX / 8;
- int32_t at_top = (int32_t)(a * 250 / 256 * PULL[5] / 256);
- return DRAG0 + (int32_t)((int64_t)(at_top - DRAG0) * c->v / top * c->v / top);
-}
-
-static int32_t grip_of(const pg_race *r, const pg_track *t, const pg_car *c) {
- int32_t g = (int32_t)pg_vehicles[c->model].grip * ZONE_GRIP[pg_zone_at(t, c->s)] / 128;
- if (absi(c->x) > PG_ROAD + PG_KERB) g = g * 92 / 128;
- (void)r;
- return g; /* 1/128 */
-}
-
-static void shift_auto(pg_car *c) {
- int red = pg_vehicles[c->model].redline;
- int32_t norm = (int32_t)((int64_t)c->v * 256 / gear_max(c, c->gear));
- int rpm = 1250 + (int)((int64_t)norm * (red - 1250) / 256);
- if (rpm > red - 250 && c->gear < 6) c->gear++;
- else if (c->gear > 1 && rpm < 3600 && c->v < gear_max(c, c->gear - 1) * 4 / 5) c->gear--;
-}
-
-/* the leader burns nitro faster, so nobody hoards it out in front */
-static bool leading(const pg_race *r, int i) {
- if (r->mode == MODE_ENDLESS || r->rivals == 0) return false;
- for (int j = 0; j <= r->rivals; j++)
- if (j != i && !r->car[j].finish_ms && r->car[j].s > r->car[i].s) return false;
- return true;
-}
-
-/* a car close in front, in line: you're in its slipstream */
-static bool drafting(const pg_race *r, const pg_track *t, int i) {
- const pg_car *c = &r->car[i];
- if (c->v < MS(30)) return false;
- for (int j = 0; j <= r->rivals; j++) {
- if (j == i) continue;
- int32_t ds = pg_wrap(t, r->car[j].s - c->s);
- if (ds > MS(4.5) && ds < MS(22) && absi(r->car[j].x - c->x) < MS(1.9)) return true;
- }
- return false;
-}
-
-/* where a car goes in a step: along the road (more of it on the inside of a bend), across it, and how the bend
- turns it */
-static void move(pg_track *t, pg_car *c) {
- int32_t k = pg_seg_of(t, c->s);
- int32_t cv = t->cv[pg_slot(t, k)];
- int32_t step = c->v / PG_HZ;
- int32_t kx = (int32_t)((int64_t)cv * c->x * 6283 / (1000LL * t->seg));
- int32_t den = clampi(65536 - kx, 40000, 90000);
- int32_t ds = (int32_t)((int64_t)step * pg_cos(c->ang) / 32767 * 65536 / den);
- int32_t dx = (int32_t)((int64_t)step * pg_sin(c->ang) / 32767);
- c->s += ds;
- c->x += dx;
- c->ang = (int16_t)(c->ang - (int32_t)((int64_t)cv * ds / t->seg));
-}
-
-static void barriers(pg_race *r, pg_track *t, pg_car *c) {
- int32_t lim = PG_WALL - PG_HW;
- if (absi(c->x) > lim) {
- bool right = c->x > 0;
- c->x = right ? lim : -lim;
- int32_t a = c->ang;
- if ((right && a > 0) || (!right && a < 0)) {
- /* into it: the speed across is lost, a little more besides, and the car turns along it (the way it was
- going: forwards, or backwards) */
- c->v = (int32_t)((int64_t)c->v * absi(pg_cos(a)) / 32767 * 15 / 16);
- bool back = a > 16384 || a < -16384;
- int32_t rel = back ? a - (a > 0 ? 32768 : -32768) : a;
- c->ang = (int16_t)(back ? rel / 4 + (a > 0 ? 32768 : -32768) : rel / 4);
- c->bump = 12;
- }
- c->v -= c->v / 160; /* scraping along */
- c->drift = 0;
- }
- /* the endless road behind you isn't kept: it's a wall too */
- if (t->endless) {
- int32_t back = (t->first + 6) * t->seg;
- if (c->s < back) c->s = back, c->v = 0;
- }
- (void)r;
-}
-
-/* ------------------------------------------------------------------ your car */
-static void drive(pg_race *r, pg_track *t, unsigned in, int shift) {
- pg_car *c = &r->car[0];
- const pg_vehicle *veh = &pg_vehicles[c->model];
- bool racing = r->phase == PH_RACE;
- if (!racing) in = r->phase == PH_GRID ? in & (IN_LEFT | IN_RIGHT) : 0;
- /* the wheel turns in quickly and comes back by itself */
- if ((in & IN_LEFT) && !(in & IN_RIGHT)) c->steer = (int16_t)clampi(c->steer - (c->steer > 0 ? 200 : 92), -1024, 1024);
- else if ((in & IN_RIGHT) && !(in & IN_LEFT)) c->steer = (int16_t)clampi(c->steer + (c->steer < 0 ? 200 : 92), -1024, 1024);
- else c->steer = (int16_t)(absi(c->steer) < 24 ? 0 : c->steer * 7 / 8);
- if (r->phase == PH_GRID) return;
- /* gears */
- if (r->manual) {
- if (shift > 0 && c->gear < 6) c->gear++;
- if (shift < 0 && c->gear > 1) c->gear--;
- } else {
- shift_auto(c);
- }
- /* nitro */
- bool lead = leading(r, 0);
- c->boost = (in & IN_NITRO) && c->nitro > 5;
- int burn = lead ? 9 : 5;
- if (c->boost) c->nitro = (uint16_t)(c->nitro > burn ? c->nitro - burn : 0);
- bool draft = drafting(r, t, 0);
- r->slipstream = draft ? (uint16_t)(r->slipstream + 1) : 0;
- /* a slide: braking while steering hard at speed */
- int32_t v = c->v;
- if ((in & IN_BRAKE) && absi(c->steer) >= 512 && v >= MS(22)) c->drift = 1;
- if (!(in & IN_BRAKE) || v < MS(15)) c->drift = 0;
- /* along */
- int32_t a = 0;
- if ((in & IN_GAS) || c->boost) {
- int32_t e = engine(c, 256);
- a += c->drift ? e / 2 : e;
- if (draft) a += DRAFT_A;
- if (c->boost) a += NITRO_A;
- }
- if (in & IN_BRAKE) a -= c->drift ? DRIFT_BRAKE : BRAKE;
- a -= drag(c);
- bool grass = absi(c->x) > PG_ROAD + PG_KERB;
- if (grass) a -= (int32_t)((MS(6) + (int64_t)v * v * 12 / 1000 / FX) * veh->offroad / 128);
- if (c->drift) a -= v / 25; /* the slide scrubs speed off */
- v += a / PG_HZ;
- int32_t top = top_of(c) + (c->boost ? NITRO_TOP : 0) + (draft ? DRAFT_TOP : 0);
- if (v > top) v = v - (v - top) / 8 > top ? v - (v - top) / 8 : top;
- if (v < 0) v = 0;
- c->v = v;
- /* turning: as far as the tyres hold */
- int32_t grip = grip_of(r, t, c);
- int32_t lat = A_LAT * grip / 128;
- int32_t hold = v > MS(1) ? (int32_t)((int64_t)lat * RAD / v) : TURN_MAX; /* units a second */
- int32_t lock = hold + hold / 6 < TURN_MAX ? hold + hold / 6 : TURN_MAX;
- int32_t auth = v >= MS(12) ? 256 : v * 256 / MS(12);
- int32_t want = (int32_t)((int64_t)c->steer * lock / 1024 * auth / 256);
- int32_t can = c->drift ? hold * 13 / 10 : hold;
- int32_t turn = clampi(want, -can, can);
- if (want != turn) c->v -= c->v / 400; /* asking more than the tyres give: they scrub */
- c->ang = (int16_t)(c->ang + turn / PG_HZ);
- /* the body in a slide: turned into the bend, by how much the car slides */
- int32_t slip_to = c->drift ? (int32_t)((int64_t)c->steer * 4700 / 1024 * veh->drift / 128) : 0;
- c->slip = (int16_t)(c->slip + (slip_to - c->slip) / 6);
- move(t, c);
- barriers(r, t, c);
- if (c->bump) c->bump--;
-}
-
-/* ------------------------------------------------------------------ the rivals */
-static const int16_t LEVEL_CORNER[3] = {214, 234, 254}; /* how close to the limit they take bends (1/256) */
-static const int16_t LEVEL_PULL[3] = {226, 242, 256};
-
-static void rival(pg_race *r, pg_track *t, int i) {
- pg_car *c = &r->car[i];
- int lv = r->level <= LEVEL_HARD ? r->level : LEVEL_NORMAL;
- int skill_c = LEVEL_CORNER[lv] - (i == 0 ? 0 : (i - 1) * 3), skill_e = LEVEL_PULL[lv] - (i == 0 ? 0 : (i - 1) * 2);
- bool racing = r->phase == PH_RACE || (r->phase == PH_DONE && r->mode == MODE_RACE);
- int32_t top = top_of(c);
- /* a little help or a little holding back, away from you (not on Hard) */
- if (i && lv != LEVEL_HARD && r->car[0].finish_ms == 0) {
- int32_t gap = r->car[i].s - r->car[0].s;
- if (gap > MS(150)) top = top * 246 / 256, skill_e = skill_e * 240 / 256;
- else if (gap < -MS(120)) top = top * 262 / 256, skill_c += 6;
- }
- if (c->finish_ms) skill_c -= 16, top = top * 7 / 8; /* a lap of honour */
- int32_t k0 = pg_seg_of(t, c->s);
- /* every few steps: the speed for what comes (the slowest bend ahead, braking from here) and the line */
- if (c->think == 0) {
- c->think = 4;
- int32_t grip = grip_of(r, t, c);
- int64_t aim2 = (int64_t)(top + (c->boost ? NITRO_TOP : 0)) * (top + (c->boost ? NITRO_TOP : 0));
- for (int k = 0; k < 24; k++) {
- int sl = pg_slot(t, k0 + k);
- int64_t vl = (int64_t)t->limit[sl] * FX;
- vl = vl * pg_isqrt((uint64_t)grip * 128) / 128 * skill_c / 256; /* (speed grows with the root of grip) */
- int64_t d = (int64_t)(k0 + k) * t->seg - c->s;
- if (d < 0) d = 0;
- int64_t v2 = vl * vl + 2 * (int64_t)AI_BRAKE * d;
- if (v2 < aim2) aim2 = v2;
- }
- c->aim = (uint16_t)(pg_isqrt((uint64_t)aim2) / 8 > 65535 ? 65535 : pg_isqrt((uint64_t)aim2) / 8);
- /* the line: out before a bend, in at its middle, with each rival a little to its own side */
- int32_t near = 0, far = 0;
- for (int k = 0; k < 4; k++) near += t->cv[pg_slot(t, k0 + k)];
- for (int k = 4; k < 13; k++) far += t->cv[pg_slot(t, k0 + k)];
- int32_t line = near * 3 - far; /* angle units */
- int32_t want = (int32_t)((int64_t)line * MS(6) / 3000) + ((i * 37) % 7 - 3) * MS(0.6);
- /* a slower car ahead in the way: go round it, on the side with room; none: follow it */
- int blocker = -1;
- int32_t best = MS(18);
- for (int j = 0; j <= r->rivals; j++) {
- if (j == i) continue;
- int32_t ds = pg_wrap(t, r->car[j].s - c->s);
- if (ds > 0 && ds < best && absi(r->car[j].x - c->x) < MS(2.8) && r->car[j].v < c->v + MS(1)) best = ds, blocker = j;
- }
- if (blocker >= 0) {
- int32_t bx = r->car[blocker].x;
- int32_t lft = bx - MS(3.4), rgt = bx + MS(3.4);
- bool lok = lft > -(PG_ROAD - MS(1.2)), rok = rgt < PG_ROAD - MS(1.2);
- for (int j = 0; j <= r->rivals; j++) {
- if (j == i || j == blocker) continue;
- int32_t ds = pg_wrap(t, r->car[j].s - c->s);
- if (ds < -MS(6) || ds > MS(24)) continue;
- if (absi(r->car[j].x - lft) < MS(2.6)) lok = false;
- if (absi(r->car[j].x - rgt) < MS(2.6)) rok = false;
- }
- if (lok && (!rok || absi(lft - c->x) <= absi(rgt - c->x))) want = lft;
- else if (rok) want = rgt;
- else if (c->aim > (r->car[blocker].v + MS(0.5)) / 8) c->aim = (uint16_t)((r->car[blocker].v + MS(0.5)) / 8);
- }
- /* an orb close to the line, when nitro runs low */
- if (c->nitro < 850 && r->mode == MODE_RACE) {
- for (int k = 1; k <= 5; k++) {
- int sl = pg_slot(t, k0 + k);
- if (t->orb[sl] == -128) continue;
- int id = t->orb_id[sl];
- if (id >= 0 && r->orb_wait[id]) break;
- int32_t ox = t->orb[sl] * FX / 8;
- if (absi(ox - c->x) < MS(5.2) && ((k0 + k) * 7 + i * 13) % 8 < 4 && blocker < 0) want = ox;
- break;
- }
- }
- c->lane = clampi(want, -(PG_ROAD - MS(1.4)), PG_ROAD - MS(1.4));
- }
- c->think--;
- /* nitro on the straights */
- int32_t aim = (int32_t)c->aim * 8;
- if (!racing) aim = 0;
- if (!c->boost) {
- if (racing && c->nitro >= 320 && aim >= top && c->v > MS(38)) c->boost = 1;
- } else if (c->nitro <= 5 || aim < top || !racing) {
- c->boost = 0;
- }
- if (c->boost) {
- int burn = leading(r, i) ? 9 : 5;
- c->nitro = (uint16_t)(c->nitro > burn ? c->nitro - burn : 0);
- }
- /* gas or brakes */
- if (c->gear < 1 || c->gear > 6) c->gear = 1;
- shift_auto(c);
- int32_t a = -drag(c);
- if (c->v > aim + MS(1)) a -= AI_BRAKE;
- else if (c->v < aim) {
- a += engine(c, skill_e);
- if (c->boost) a += NITRO_A;
- if (drafting(r, t, i)) a += DRAFT_A;
- }
- int32_t v = c->v + a / PG_HZ;
- int32_t cap = top + (c->boost ? NITRO_TOP : 0) + MS(3);
- if (absi(c->x) > PG_ROAD + PG_KERB) cap = cap * 3 / 4;
- c->v = clampi(v, 0, cap);
- /* across, towards the line, and pointing where it goes */
- int32_t dx = c->lane - c->x, vx_max = MS(5) / PG_HZ;
- int32_t vx = clampi(dx / 6, -vx_max, vx_max);
- if (c->v < MS(3)) vx = 0;
- int32_t head = c->v > MS(1) ? pg_atan2(vx * PG_HZ, c->v) : 0;
- c->ang = (int16_t)(head > 32767 ? head - 65536 : head);
- c->steer = (int16_t)clampi(c->ang * 8, -1024, 1024);
- /* along the road, as the road goes */
- int32_t keep = c->ang;
- move(t, c);
- c->ang = (int16_t)keep;
- c->slip = 0, c->drift = 0;
- barriers(r, t, c);
- if (c->bump) c->bump--;
-}
-
-/* ------------------------------------------------------------------ cars touching */
-static void touch(pg_race *r, pg_track *t) {
- int n = r->mode == MODE_ENDLESS ? 1 : r->rivals + 1;
- for (int pass = 0; pass < 3; pass++)
- for (int a = 0; a < n; a++)
- for (int b = a + 1; b < n; b++) {
- pg_car *A = &r->car[a], *B = &r->car[b];
- int32_t ds = pg_wrap(t, B->s - A->s), dx = B->x - A->x;
- int32_t ps = 2 * PG_HL - absi(ds), px = 2 * PG_HW - absi(dx);
- if (ps <= 0 || px <= 0) continue;
- if ((int64_t)ps * PG_HW < (int64_t)px * PG_HL) {
- /* nose to tail: the car behind stops at the other's bumper and gives it a push */
- pg_car *back = ds > 0 ? A : B, *front = ds > 0 ? B : A;
- back->s -= ps * 3 / 4 + 1, front->s += ps - ps * 3 / 4;
- if (back->v > front->v) {
- int32_t d = back->v - front->v;
- back->v = front->v - d / 8 > 0 ? front->v - d / 8 : 0, front->v += d / 3;
- }
- back->bump = front->bump = 10;
- } else {
- /* side by side: pushed apart, both straightened */
- pg_car *left = dx > 0 ? A : B, *right = dx > 0 ? B : A;
- int32_t lw = -(PG_WALL - PG_HW), rw = PG_WALL - PG_HW;
- int32_t half = px / 2 + 1;
- int32_t lx = left->x - half, rx = right->x + (px - px / 2 + 1);
- if (lx < lw) rx += lw - lx, lx = lw; /* against a barrier: the other takes all of it */
- if (rx > rw) lx -= rx - rw, rx = rw;
- if (lx < lw) lx = lw;
- left->x = lx, right->x = rx;
- if (left->ang > 0) left->ang = (int16_t)(left->ang / 2);
- if (right->ang < 0) right->ang = (int16_t)(right->ang / 2);
- left->v -= left->v / 64, right->v -= right->v / 64;
- left->bump = right->bump = 10;
- left->drift = right->drift = 0;
- }
- }
-}
-
-/* ------------------------------------------------------------------ orbs */
-static void orbs(pg_race *r, pg_track *t, int i, int32_t s0) {
- pg_car *c = &r->car[i];
- int32_t k0 = pg_seg_of(t, s0), k1 = pg_seg_of(t, c->s);
- for (int32_t k = k0; k <= k1 && k <= k0 + 2; k++) {
- int sl = pg_slot(t, k);
- if (t->orb[sl] == -128) continue;
- int32_t mid = k * t->seg + t->seg / 2;
- if (!(s0 < mid && c->s >= mid)) continue;
- if (absi(t->orb[sl] * FX / 8 - c->x) >= MS(2.6)) continue;
- int gain;
- if (r->mode == MODE_ENDLESS) {
- if (k <= r->last_orb) continue;
- r->last_orb = k, gain = 350;
- } else {
- int id = t->orb_id[sl];
- if (id < 0 || r->orb_wait[id]) continue;
- r->orb_wait[id] = ORB_WAIT;
- gain = leading(r, i) ? 180 : 300;
- }
- c->nitro = (uint16_t)(c->nitro + gain > 1000 ? 1000 : c->nitro + gain);
- if (i == 0) r->flash = 25;
- }
-}
-
-/* ------------------------------------------------------------------ the race */
-static void banner(pg_race *r, int b, int arg) { r->banner = (uint8_t)b, r->banner_arg = (uint8_t)arg, r->banner_t = 2 * PG_HZ; }
-
-void pg_new(pg_race *r, pg_track *t, const pg_setup *s, uint32_t seed) {
- memset(r, 0, sizeof *r);
- r->mode = s->mode <= MODE_ENDLESS ? s->mode : MODE_RACE;
- r->track = s->track < PG_CIRCUITS ? s->track : 0;
- r->vehicle = s->vehicle < PG_VEHICLES ? s->vehicle : 0;
- r->laps = s->laps >= 1 && s->laps <= PG_MAX_LAPS ? s->laps : 3;
- r->rivals = r->mode == MODE_ENDLESS ? 0 : s->rivals <= PG_CARS - 1 ? s->rivals : 5;
- r->level = s->level <= LEVEL_HARD ? s->level : LEVEL_NORMAL;
- r->manual = s->manual ? 1 : 0;
- r->seed = seed ? seed : 1;
- r->phase = PH_GRID, r->countdown = COUNTDOWN, r->lap = 1;
- if (r->mode == MODE_ENDLESS) pg_endless(t, r->seed);
- else pg_build(t, r->track);
- uint32_t rng = r->seed;
- int n = r->rivals + 1;
- for (int i = 0; i < n; i++) {
- pg_car *c = &r->car[i];
- /* the grid: two by two behind the line, you at the back */
- int slot = i == 0 ? n - 1 : i - 1;
- c->s = -(MS(10) + (slot / 2) * GRID_GAP + (slot & 1) * GRID_GAP / 2);
- c->x = r->mode == MODE_ENDLESS ? 0 : (slot & 1) ? MS(3.2) : -MS(3.2);
- c->lane = c->x, c->gear = 1, c->nitro = 300;
- c->model = i == 0 ? r->vehicle : (uint8_t)((i + pg_rng(&rng) % 3) % 3);
- c->paint = (uint8_t)(i == 0 ? 0 : (i + 2) % 8);
- c->driven = i != 0, c->think = (uint16_t)(i % 4);
- }
- if (r->mode == MODE_ENDLESS) {
- r->car[0].s = MS(30), r->car[0].nitro = 1000;
- r->time_left = 30000, r->last_orb = -1;
- }
-}
-
-/* time a zone of the endless road gives: 8 s, a quarter of a second less each zone, 3 s at least */
-int32_t pg_zone_bonus(int zone) {
- int32_t b = 8000 - 250 * (zone - 1);
- return b < 3000 ? 3000 : b;
-}
-
-void pg_step(pg_race *r, pg_track *t, unsigned input, int shift) {
- r->ticks++;
- if (r->mode == MODE_ENDLESS) pg_extend(t, pg_seg_of(t, r->car[0].s) + 48);
- if (r->phase == PH_GRID) {
- drive(r, t, input, 0); /* (the wheel turns; the car stays) */
- if (r->countdown) r->countdown--;
- if (!r->countdown) r->phase = PH_RACE;
- return;
- }
- if ((r->phase == PH_RACE || (r->phase == PH_DONE && r->mode == MODE_RACE)) && r->time_ms < 99u * 60000u) r->time_ms += PG_TICK;
- if (r->flash) r->flash--;
- int n = r->rivals + 1;
- int32_t before[PG_CARS];
- for (int i = 0; i < n; i++) before[i] = r->car[i].s;
- if (r->car[0].driven) rival(r, t, 0);
- else drive(r, t, input, shift);
- for (int i = 1; i < n; i++) rival(r, t, i);
- touch(r, t);
- for (int i = 0; i < n; i++) orbs(r, t, i, before[i]);
- for (int k = 0; k < PG_ORBS; k++)
- if (r->orb_wait[k]) r->orb_wait[k]--;
- if (r->banner_t && !--r->banner_t) r->banner = BANNER_NONE;
- pg_car *me = &r->car[0];
- /* wrong way: pointing back down the road and moving */
- bool back = me->ang > 18000 || me->ang < -18000;
- r->wrong = (uint8_t)(back && me->v > MS(4) && r->phase == PH_RACE ? (r->wrong < 255 ? r->wrong + 1 : 255) : 0);
- if (r->mode == MODE_ENDLESS) {
- int zone = (int)(pg_seg_of(t, me->s) / PG_ZONE);
- if (zone > r->zone && r->phase == PH_RACE) {
- r->zone = (uint8_t)(zone < 255 ? zone : 255);
- r->time_left += pg_zone_bonus(zone);
- banner(r, BANNER_ZONE, zone);
- }
- if (r->phase == PH_RACE) {
- r->time_left -= PG_TICK;
- if (r->time_left <= 0) r->time_left = 0, r->phase = PH_DONE, r->end = END_TIME, me->drift = 0;
- } else {
- r->done = (uint16_t)(r->done < 65535 ? r->done + 1 : 65535);
- me->v -= me->v / 20 + MS(0.2); /* out of time: the car rolls to a stop */
- if (me->v < 0) me->v = 0;
- }
- return;
- }
- /* laps, the finish */
- for (int i = 0; i < n; i++) {
- pg_car *c = &r->car[i];
- int32_t L = t->length;
- if (c->s < -L) c->s += L; /* (a whole lap backwards: the same place) */
- if (c->finish_ms) {
- if (c->s >= (int32_t)(r->laps + 1) * L) c->s -= L;
- continue;
- }
- if (c->s >= (int32_t)r->laps * L) {
- c->finish_ms = r->time_ms ? r->time_ms : 1;
- c->place = (uint8_t)++r->finished;
- if (i == 0) {
- r->phase = PH_DONE, r->end = END_FINISH, c->driven = 1, c->drift = 0, c->boost = 0;
- uint32_t lap = r->time_ms - r->lap_at;
- r->last_lap_ms = lap;
- if (!r->best_lap_ms || lap < r->best_lap_ms) r->best_lap_ms = lap;
- }
- } else if (i == 0) {
- int lap = c->s < 0 ? 1 : (int)(c->s / L) + 1;
- if (lap > r->lap) {
- uint32_t ms = r->time_ms - r->lap_at;
- r->lap_at = r->time_ms, r->last_lap_ms = ms;
- bool best = !r->best_lap_ms || ms < r->best_lap_ms;
- if (best) r->best_lap_ms = ms;
- r->lap = (uint8_t)lap;
- banner(r, lap == r->laps ? BANNER_FINAL : BANNER_LAP, lap);
- }
- }
- }
- /* (99 minutes and still not over the line: out of time) */
- if (r->phase == PH_RACE && r->time_ms >= 99u * 60000u - PG_TICK) r->phase = PH_DONE, r->end = END_TIME, me->driven = 1;
- if (r->phase == PH_DONE) r->done = (uint16_t)(r->done < 65535 ? r->done + 1 : 65535);
- if (r->wrong > PG_HZ && r->banner != BANNER_WRONG) banner(r, BANNER_WRONG, 0);
-}
-
-int pg_rank(const pg_race *r, int car) {
- const pg_car *c = &r->car[car];
- if (c->place) return c->place;
- int rank = 1 + r->finished;
- for (int j = 0; j <= r->rivals; j++)
- if (j != car && !r->car[j].place && (r->car[j].s > c->s || (r->car[j].s == c->s && j < car))) rank++;
- return rank;
-}
-
-int32_t pg_lap_s(const pg_race *r, const pg_track *t) {
- int32_t s = r->car[0].s;
- if (t->endless) return s;
- int32_t L = t->length, m = s % L;
- return m < 0 ? m + L : m;
-}
-
-/* everything in range: what the game makes, and what a damaged save might hold */
-bool pg_valid(const pg_race *r, const pg_track *t) {
- if (r->mode > MODE_ENDLESS || r->track >= PG_CIRCUITS || r->vehicle >= PG_VEHICLES) return false;
- if (r->laps < 1 || r->laps > PG_MAX_LAPS || r->rivals > PG_CARS - 1 || r->level > LEVEL_HARD) return false;
- if (r->manual > 1 || r->phase > PH_DONE || r->end > END_TIME || r->flash > 25) return false;
- if (r->mode == MODE_ENDLESS && r->rivals) return false;
- if (r->lap < 1 || r->lap > r->laps || r->finished > r->rivals + 1 || r->banner > BANNER_WRONG) return false;
- if (r->countdown > COUNTDOWN || (r->phase == PH_GRID) != (r->countdown > 0 && r->time_ms == 0)) return false;
- if (r->phase != PH_DONE && (r->end || r->done)) return false;
- if (r->phase == PH_DONE && !r->end) return false;
- if (r->banner_t > 2 * PG_HZ || r->lap_at > r->time_ms || r->best_lap_ms > r->time_ms || r->last_lap_ms > r->time_ms)
- return false;
- if (r->time_ms > 99 * 60000u || r->seed == 0) return false;
- for (int k = 0; k < PG_ORBS; k++)
- if (r->orb_wait[k] > ORB_WAIT) return false;
- if (r->mode == MODE_ENDLESS) {
- if (r->time_left < 0 || r->time_left > 30 * 60000 || r->last_orb < -1) return false;
- if (r->phase == PH_DONE && r->end != END_TIME) return false;
- } else if (r->end == END_TIME && r->car[0].finish_ms) {
- return false;
- } else if (r->time_left || r->last_orb || r->zone) {
- return false;
- }
- int32_t L = t && !t->endless ? t->length : MS(1800);
- int32_t lo = r->mode == MODE_ENDLESS ? 0 : -L - MS(100);
- int32_t hi = r->mode == MODE_ENDLESS ? MS(2000000) : (int32_t)(r->laps + 2) * L;
- for (int i = 0; i < PG_CARS; i++) {
- const pg_car *c = &r->car[i];
- if (i > r->rivals) {
- const uint8_t *b = (const uint8_t *)c;
- for (unsigned k = 0; k < sizeof *c; k++)
- if (b[k]) return false;
- continue;
- }
- if (c->s < lo || c->s > hi || c->x < -(PG_WALL - PG_HW) || c->x > PG_WALL - PG_HW) return false;
- if (c->lane < -PG_ROAD || c->lane > PG_ROAD || c->v < 0 || c->v > MS(130)) return false;
- if (c->steer < -1024 || c->steer > 1024 || c->nitro > 1000 || c->gear < 1 || c->gear > 6) return false;
- if (c->boost > 1 || c->drift > 1 || c->model >= PG_VEHICLES || c->paint > 7 || c->bump > 12 || c->driven > 1) return false;
- if (c->place > r->rivals + 1 || (c->place != 0) != (c->finish_ms != 0) || c->finish_ms > r->time_ms + 1) return false;
- if (c->think > 4 || (i == 0 && c->model != r->vehicle)) return false;
- if (i > 0 ? !c->driven : c->driven != (c->finish_ms || (r->mode == MODE_RACE && r->end == END_TIME) ? 1 : 0)) return false;
- }
- return true;
-}
diff --git a/games/polygt3d/src/render.c b/games/polygt3d/src/render.c
deleted file mode 100644
index 3e2eb384..00000000
--- a/games/polygt3d/src/render.c
+++ /dev/null
@@ -1,886 +0,0 @@
-/* PolyGT 3D's picture: low-poly, flat-shaded triangles seen from behind your car.
- *
- * A frame is set up once (rd_race, rd_showroom): the road's segments ahead, the roadside, the orbs and the cars are
- * turned into the camera's space, cut at the near plane, projected, and kept as screen triangles with their depth
- * (1/z, which is flat across the screen). rd_draw then fills a strip of rows at a time: the sky, the hills and the
- * ground, then every triangle crossing the strip, nearest wins. Edges are walked in floats with a fill rule, so
- * neighbouring triangles never leave a gap or overlap. */
-#include <string.h>
-#include "../../../launcher/src/gfx.h"
-#include "render.h"
-
-#define CX 160.0f
-#define CY 115.0f
-#define FOCAL 205.0f
-#define NEAR 1.0f
-#define FAR 215.0f
-#define FOG0 55.0f
-#define MAX_TRIS 1400
-#define DRAW_SEGS 34 /* segments ahead */
-#define DETAIL_SEGS 14 /* ...with lines and stripes */
-#define GUARD 1000.0f /* screen triangles are cut to this far outside the screen */
-
-typedef struct { float x, y, z; } v3;
-
-typedef struct {
- int16_t x[3], y[3]; /* 1/16 pixel, top to bottom */
- uint16_t w[3]; /* NEAR/z, 65535 = at the near plane */
- uint16_t c;
- uint8_t stip, pad;
- int16_t y0, y1; /* the rows it covers */
-} tri;
-
-static tri tris[MAX_TRIS];
-static int ntris;
-int rd_dropped, rd_tris;
-
-/* the camera */
-static int32_t cam_x, cam_y, cam_z; /* where it is (FX) */
-static float cy_c, cy_s, cp_c, cp_s, cr_c, cr_s;
-static int32_t cam_yaw;
-static int horizon;
-/* the background */
-static uint8_t sky_top[3], sky_bot[3], fog[3], ground[3], hill[3];
-static uint8_t hills[SCREEN_W];
-
-/* ------------------------------------------------------------------ colours */
-typedef struct {
- uint8_t sky_top[3], sky_bot[3], ground[3], road[3], kerb[3], tree[3], wall[3];
- uint8_t lamps;
-} theme;
-static const theme THEMES[8] = {
- {{35, 25, 85}, {235, 110, 80}, {35, 85, 42}, {62, 64, 72}, {220, 35, 35}, {24, 95, 44}, {214, 214, 222}, 0},
- {{10, 12, 32}, {55, 25, 85}, {22, 24, 34}, {46, 50, 62}, {0, 195, 235}, {90, 220, 255}, {104, 110, 128}, 1},
- {{45, 35, 60}, {165, 130, 145}, {48, 62, 42}, {58, 56, 60}, {230, 165, 20}, {30, 72, 44}, {190, 190, 196}, 0},
- {{25, 60, 140}, {130, 190, 245}, {34, 98, 44}, {62, 65, 72}, {225, 40, 40}, {24, 95, 44}, {214, 214, 222}, 0},
- {{55, 20, 75}, {245, 115, 50}, {180, 98, 46}, {74, 62, 56}, {240, 185, 25}, {205, 120, 52}, {196, 170, 140}, 0},
- {{10, 12, 34}, {70, 25, 105}, {20, 26, 42}, {44, 48, 60}, {0, 215, 255}, {90, 220, 255}, {96, 104, 126}, 1},
- {{38, 82, 150}, {185, 225, 250}, {198, 215, 232}, {68, 76, 92}, {35, 115, 240}, {185, 220, 245}, {150, 160, 176}, 0},
- {{36, 16, 38}, {215, 65, 35}, {44, 36, 38}, {48, 44, 46}, {255, 95, 15}, {235, 85, 28}, {120, 110, 112}, 0},
-};
-const uint8_t rd_paint[8][3] = {{245, 195, 25}, {30, 205, 95}, {175, 55, 235}, {245, 110, 25},
- {50, 195, 240}, {215, 215, 225}, {235, 45, 135}, {40, 85, 220}};
-
-uint16_t rd_rgb(const uint8_t c[3]) { return (uint16_t)((c[0] & 0xF8) << 8 | (c[1] & 0xFC) << 3 | c[2] >> 3); }
-static uint8_t cl8(int v) { return (uint8_t)(v < 0 ? 0 : v > 255 ? 255 : v); }
-static void mix3(uint8_t *o, const uint8_t *a, const uint8_t *b, int t256) {
- for (int i = 0; i < 3; i++) o[i] = (uint8_t)(a[i] + (b[i] - a[i]) * t256 / 256);
-}
-
-/* ------------------------------------------------------------------ small float maths */
-static float fsinu(int32_t a) { return (float)pg_sin(a) * (1.0f / 32767.0f); }
-static float fcosu(int32_t a) { return (float)pg_cos(a) * (1.0f / 32767.0f); }
-static int iceil(float f) {
- int i = (int)f;
- return (float)i < f ? i + 1 : i;
-}
-static float clampf(float v, float lo, float hi) { return v < lo ? lo : v > hi ? hi : v; }
-
-/* a point of the world (FX), in the camera's space */
-static v3 to_cam_f(float dx, float dy, float dz) {
- float x1 = dx * cy_c - dz * cy_s, z1 = dx * cy_s + dz * cy_c;
- float y2 = dy * cp_c - z1 * cp_s, z2 = dy * cp_s + z1 * cp_c;
- v3 o = {x1 * cr_c - y2 * cr_s, x1 * cr_s + y2 * cr_c, z2};
- return o;
-}
-static v3 to_cam(int32_t wx, int32_t wy, int32_t wz) {
- return to_cam_f((float)(wx - cam_x) * (1.0f / FX), (float)(wy - cam_y) * (1.0f / FX), (float)(wz - cam_z) * (1.0f / FX));
-}
-
-/* ------------------------------------------------------------------ triangles */
-typedef struct { float x, y, w; } sv;
-
-static void store(const sv *a, const sv *b, const sv *c, uint16_t col, uint8_t stip) {
- if (ntris >= MAX_TRIS) {
- rd_dropped = 1;
- return;
- }
- const sv *p[3] = {a, b, c};
- /* top to bottom */
- for (int i = 0; i < 2; i++)
- for (int j = 0; j < 2 - i; j++)
- if (p[j]->y > p[j + 1]->y) {
- const sv *t = p[j];
- p[j] = p[j + 1], p[j + 1] = t;
- }
- int y0 = iceil(p[0]->y - 0.5f), y1 = iceil(p[2]->y - 0.5f);
- if (y0 < 0) y0 = 0;
- if (y1 > SCREEN_H) y1 = SCREEN_H;
- if (y1 <= y0) return;
- float xmin = p[0]->x, xmax = p[0]->x;
- for (int i = 1; i < 3; i++) {
- if (p[i]->x < xmin) xmin = p[i]->x;
- if (p[i]->x > xmax) xmax = p[i]->x;
- }
- if (xmax < 0 || xmin > SCREEN_W) return;
- tri *t = &tris[ntris++];
- for (int i = 0; i < 3; i++) {
- t->x[i] = (int16_t)(p[i]->x * 16.0f);
- t->y[i] = (int16_t)(p[i]->y * 16.0f);
- t->w[i] = (uint16_t)(clampf(p[i]->w, 0.0f, 1.0f) * 65535.0f);
- }
- t->c = col, t->stip = stip, t->y0 = (int16_t)y0, t->y1 = (int16_t)y1;
-}
-
-/* a convex polygon on the screen, cut to the guard band when it reaches far outside */
-static void screen_poly(sv *v, int n, uint16_t col, uint8_t stip) {
- bool far_out = false;
- for (int i = 0; i < n; i++)
- if (v[i].x < -GUARD || v[i].x > SCREEN_W + GUARD || v[i].y < -GUARD || v[i].y > SCREEN_H + GUARD) far_out = true;
- if (far_out) {
- static const float LIM[4] = {-GUARD, SCREEN_W + GUARD, -GUARD, SCREEN_H + GUARD};
- sv buf[2][12];
- int m = n;
- memcpy(buf[0], v, sizeof(sv) * (size_t)n);
- int cur = 0;
- for (int e = 0; e < 4 && m; e++) {
- sv *in = buf[cur], *out = buf[cur ^ 1];
- int k = 0;
- for (int i = 0; i < m; i++) {
- sv *A = &in[i], *B = &in[(i + 1) % m];
- float a = e < 2 ? A->x : A->y, b = e < 2 ? B->x : B->y;
- bool ain = (e & 1) ? a <= LIM[e] : a >= LIM[e], bin = (e & 1) ? b <= LIM[e] : b >= LIM[e];
- if (ain) out[k++] = *A;
- if (ain != bin && k < 12) {
- float t = (LIM[e] - a) / (b - a);
- sv q = {A->x + (B->x - A->x) * t, A->y + (B->y - A->y) * t, A->w + (B->w - A->w) * t};
- out[k++] = q;
- }
- }
- m = k, cur ^= 1;
- }
- for (int i = 1; i + 1 < m; i++) store(&buf[cur][0], &buf[cur][i], &buf[cur][i + 1], col, stip);
- return;
- }
- for (int i = 1; i + 1 < n; i++) store(&v[0], &v[i], &v[i + 1], col, stip);
-}
-
-/* a polygon in the camera's space (3 or 4 corners, convex), cut at the near plane, fogged, projected */
-static void poly(const v3 *p, int n, const uint8_t *rgb, uint8_t stip) {
- v3 c[6];
- int m = 0;
- bool any_far = true;
- for (int i = 0; i < n; i++) {
- const v3 *A = &p[i], *B = &p[(i + 1) % n];
- bool ain = A->z >= NEAR, bin = B->z >= NEAR;
- if (A->z < FAR) any_far = false;
- if (ain) c[m++] = *A;
- if (ain != bin) {
- float t = (NEAR - A->z) / (B->z - A->z);
- v3 q = {A->x + (B->x - A->x) * t, A->y + (B->y - A->y) * t, NEAR};
- c[m++] = q;
- }
- }
- if (m < 3 || any_far) return;
- /* fog towards the horizon's colour */
- float zs = 0;
- for (int i = 0; i < n; i++) zs += p[i].z;
- zs /= (float)n;
- uint8_t col[3] = {rgb[0], rgb[1], rgb[2]};
- if (zs > FOG0) {
- int t = (int)(clampf((zs - FOG0) * (1.0f / (FAR - FOG0)), 0.0f, 0.9f) * 256.0f);
- mix3(col, rgb, fog, t);
- }
- sv s[6];
- for (int i = 0; i < m; i++) {
- float inv = 1.0f / c[i].z;
- s[i].x = CX + c[i].x * FOCAL * inv;
- s[i].y = CY - c[i].y * FOCAL * inv;
- s[i].w = NEAR * inv;
- }
- screen_poly(s, m, rd_rgb(col), stip);
-}
-
-static void quad(v3 a, v3 b, v3 c, v3 d, const uint8_t *rgb) {
- v3 p[4] = {a, b, c, d};
- poly(p, 4, rgb, 0);
-}
-static void tri3(v3 a, v3 b, v3 c, const uint8_t *rgb, uint8_t stip) {
- v3 p[3] = {a, b, c};
- poly(p, 3, rgb, stip);
-}
-
-/* a face of a solid object: only when it faces the camera (seen from outside its centre o) */
-static bool facing(const v3 *p, int n, v3 o) {
- v3 u = {p[1].x - p[0].x, p[1].y - p[0].y, p[1].z - p[0].z}, v = {p[n - 1].x - p[0].x, p[n - 1].y - p[0].y, p[n - 1].z - p[0].z};
- v3 nn = {u.y * v.z - u.z * v.y, u.z * v.x - u.x * v.z, u.x * v.y - u.y * v.x};
- float fx = 0, fy = 0, fz = 0;
- for (int i = 0; i < n; i++) fx += p[i].x, fy += p[i].y, fz += p[i].z;
- fx /= (float)n, fy /= (float)n, fz /= (float)n;
- float out = nn.x * (fx - o.x) + nn.y * (fy - o.y) + nn.z * (fz - o.z);
- float view = nn.x * fx + nn.y * fy + nn.z * fz;
- return out * view < 0;
-}
-
-/* ------------------------------------------------------------------ objects: a frame of axes in the camera's space */
-typedef struct {
- v3 o, r, u, f; /* origin, right, up, forward */
-} frame;
-
-static v3 at(const frame *F, float x, float y, float z) {
- v3 p = {F->o.x + F->r.x * x + F->u.x * y + F->f.x * z, F->o.y + F->r.y * x + F->u.y * y + F->f.y * z,
- F->o.z + F->r.z * x + F->u.z * y + F->f.z * z};
- return p;
-}
-
-/* an object standing at a world point, pointing at heading h (angle units); roll and pitch in radians */
-static frame frame_at(int32_t wx, int32_t wy, int32_t wz, int32_t h, float roll, float pitch) {
- frame F;
- F.o = to_cam(wx, wy, wz);
- float s = fsinu(h), c = fcosu(h);
- v3 zero = to_cam_f(0, 0, 0);
- v3 R = to_cam_f(c, 0, -s), U = to_cam_f(0, 1, 0), Fw = to_cam_f(s, 0, c);
- R.x -= zero.x, R.y -= zero.y, R.z -= zero.z;
- U.x -= zero.x, U.y -= zero.y, U.z -= zero.z;
- Fw.x -= zero.x, Fw.y -= zero.y, Fw.z -= zero.z;
- /* small tilts: the body leans in a bend and pitches under braking */
- F.r = (v3){R.x + U.x * roll, R.y + U.y * roll, R.z + U.z * roll};
- F.u = (v3){U.x - R.x * roll + Fw.x * pitch, U.y - R.y * roll + Fw.y * pitch, U.z - R.z * roll + Fw.z * pitch};
- F.f = (v3){Fw.x - U.x * pitch, Fw.y - U.y * pitch, Fw.z - U.z * pitch};
- return F;
-}
-
-static void lquad(const frame *F, const float *a, const float *b, const float *c, const float *d, const uint8_t *rgb,
- bool solid, v3 centre) {
- v3 p[4] = {at(F, a[0], a[1], a[2]), at(F, b[0], b[1], b[2]), at(F, c[0], c[1], c[2]), at(F, d[0], d[1], d[2])};
- if (solid && !facing(p, 4, centre)) return;
- poly(p, 4, rgb, 0);
-}
-
-/* ------------------------------------------------------------------ cars */
-static const float CAR_V[18][3] = {
- {-1.15f, 0.28f, -2.20f}, {1.15f, 0.28f, -2.20f}, {-1.08f, 0.82f, -2.12f}, {1.08f, 0.82f, -2.12f},
- {-1.20f, 0.24f, 0.20f}, {1.20f, 0.24f, 0.20f}, {-1.12f, 0.80f, 0.55f}, {1.12f, 0.80f, 0.55f},
- {-1.10f, 0.24f, 2.25f}, {1.10f, 0.24f, 2.25f}, {-1.02f, 0.64f, 2.15f}, {1.02f, 0.64f, 2.15f},
- {-1.04f, 0.84f, -1.30f}, {1.04f, 0.84f, -1.30f}, {-0.82f, 1.24f, -0.95f}, {0.82f, 1.24f, -0.95f},
- {-0.78f, 1.20f, 0.15f}, {0.78f, 1.20f, 0.15f}};
-/* the body's faces: corners, and which colour (0 body, 1 side, 2 dark, 3 glass, 4 roof) */
-static const uint8_t CAR_F[15][5] = {
- {0, 1, 3, 2, 2}, {2, 3, 13, 12, 0}, {12, 13, 15, 14, 3}, {14, 15, 17, 16, 4}, {16, 17, 7, 6, 3}, {6, 7, 11, 10, 0},
- {10, 11, 9, 8, 2}, {0, 4, 6, 2, 1}, {4, 8, 10, 6, 1}, {12, 6, 16, 14, 3}, {5, 1, 3, 7, 1}, {9, 5, 7, 11, 1},
- {7, 13, 15, 17, 3}, {2, 6, 12, 2, 1}, {3, 13, 7, 3, 1}};
-
-typedef struct {
- int32_t wx, wy, wz, head;
- float roll, pitch, steer;
- int model;
- const uint8_t *paint;
- bool brake, flame, boost, smoke, ghost, near;
-} car_pose;
-
-static void horse(const frame *F, const car_pose *P, uint32_t clock);
-
-static void car(const car_pose *P, uint32_t clock) {
- frame F = frame_at(P->wx, P->wy, P->wz, P->head, P->roll, P->pitch);
- if (F.o.z < -6.0f || F.o.z > FAR) return;
- if (F.o.z > 0 && (F.o.x > F.o.z * 1.2f + 6 || F.o.x < -F.o.z * 1.2f - 6)) return; /* out to the side */
- if (pg_vehicles[P->model].horse) {
- horse(&F, P, clock);
- return;
- }
- static const uint8_t GHOST[3] = {70, 220, 250};
- uint8_t b[3];
- for (int i = 0; i < 3; i++) b[i] = P->ghost ? GHOST[i] : P->paint[i];
- uint8_t col[5][3];
- for (int i = 0; i < 3; i++) {
- col[0][i] = b[i];
- col[1][i] = (uint8_t)(b[i] * 5 / 8);
- col[2][i] = (uint8_t)(b[i] * 3 / 4);
- col[3][i] = P->ghost ? (uint8_t)(b[i] * 3 / 4) : (uint8_t)(i == 0 ? 32 : i == 1 ? 48 : 75);
- col[4][i] = P->model == 1 && !P->ghost ? (uint8_t)(i == 2 ? 40 : 30) : b[i];
- }
- v3 centre = at(&F, 0, 0.6f, 0);
- float roof = P->model == 1 ? 1.18f : P->model == 2 ? 1.32f : 1.24f;
- v3 V[18];
- for (int i = 0; i < 18; i++) V[i] = at(&F, CAR_V[i][0], i >= 14 ? (i >= 16 ? roof - 0.04f : roof) : CAR_V[i][1], CAR_V[i][2]);
- bool far_away = F.o.z > 70.0f;
- uint8_t stip = P->ghost ? 1 : 0;
- if (!P->ghost) { /* the shadow */
- static const uint8_t SH[3] = {14, 14, 20};
- v3 s[4] = {at(&F, -1.25f, 0.04f, -2.3f), at(&F, 1.25f, 0.04f, -2.3f), at(&F, 1.25f, 0.04f, 2.3f), at(&F, -1.25f, 0.04f, 2.3f)};
- poly(s, 4, SH, 0);
- }
- for (int k = 0; k < 15; k++) {
- const uint8_t *f = CAR_F[k];
- if (far_away && (k == 9 || k >= 12)) continue;
- v3 p[4] = {V[f[0]], V[f[1]], V[f[2]], V[f[3]]};
- int n = f[3] == f[0] ? 3 : 4;
- if (!facing(p, n, centre)) continue;
- poly(p, n, col[f[4]], stip);
- }
- if (far_away) return;
- /* the wing, the lights, the plate, the wheels */
- static const uint8_t WING[3] = {35, 35, 42}, TYRE[3] = {24, 24, 28};
- float wl0[3] = {-1.14f, 1.08f, -2.08f}, wr0[3] = {1.14f, 1.08f, -2.08f}, wr1[3] = {1.14f, 1.02f, -1.78f},
- wl1[3] = {-1.14f, 1.02f, -1.78f};
- if (!P->ghost) lquad(&F, wl0, wr0, wr1, wl1, WING, false, centre);
- if (P->ghost) return;
- const pg_vehicle *veh = &pg_vehicles[P->model];
- uint8_t tl[3] = {(uint8_t)(P->brake ? 255 : 185), (uint8_t)(P->brake ? 55 : 15), (uint8_t)(P->brake ? 40 : 20)};
- float a0[3] = {-1.02f, 0.56f, -2.23f}, a1[3] = {-0.42f, 0.56f, -2.23f}, a2[3] = {-0.42f, 0.74f, -2.18f}, a3[3] = {-1.02f, 0.74f, -2.18f};
- lquad(&F, a0, a1, a2, a3, tl, true, centre);
- float b0[3] = {0.42f, 0.56f, -2.23f}, b1[3] = {1.02f, 0.56f, -2.23f}, b2[3] = {1.02f, 0.74f, -2.18f}, b3[3] = {0.42f, 0.74f, -2.18f};
- lquad(&F, b0, b1, b2, b3, tl, true, centre);
- float p0[3] = {-0.24f, 0.36f, -2.23f}, p1[3] = {0.24f, 0.36f, -2.23f}, p2[3] = {0.24f, 0.54f, -2.21f}, p3[3] = {-0.24f, 0.54f, -2.21f};
- lquad(&F, p0, p1, p2, p3, veh->accent, true, centre);
- static const float WX[4] = {-1.18f, 1.18f, -1.16f, 1.16f}, WZ[4] = {-1.38f, -1.38f, 1.38f, 1.38f};
- uint8_t hub[3] = {(uint8_t)(veh->accent[0] / 2), (uint8_t)(veh->accent[1] / 2), (uint8_t)(veh->accent[2] / 2)};
- for (int w = 0; w < 4; w++) {
- float xin = WX[w] > 0 ? WX[w] - 0.22f : WX[w] + 0.22f, xo = WX[w];
- float q0[3] = {xin, 0.05f, WZ[w] - 0.36f}, q1[3] = {xo, 0.05f, WZ[w] - 0.36f}, q2[3] = {xo, 0.58f, WZ[w] - 0.36f},
- q3[3] = {xin, 0.58f, WZ[w] - 0.36f};
- lquad(&F, q0, q1, q2, q3, TYRE, false, centre);
- float s0[3] = {xo, 0.08f, WZ[w] - 0.34f}, s1[3] = {xo, 0.08f, WZ[w] + 0.34f}, s2[3] = {xo, 0.56f, WZ[w] + 0.34f},
- s3[3] = {xo, 0.56f, WZ[w] - 0.34f};
- lquad(&F, s0, s1, s2, s3, hub, true, centre);
- }
- if (P->boost || P->flame) { /* nitro (blue) or a shift (orange), out of the exhausts */
- static const uint8_t FLAME[2][3] = {{255, 195, 40}, {40, 240, 255}};
- float len = P->boost ? ((clock / 40) & 1 ? -3.45f : -3.1f) : -2.9f;
- for (int side = -1; side <= 1; side += 2) {
- tri3(at(&F, side * 0.76f, 0.30f, -2.24f), at(&F, side * 0.36f, 0.30f, -2.24f), at(&F, side * 0.56f, 0.36f, len),
- FLAME[P->boost ? 1 : 0], 0);
- }
- }
- if (P->smoke) {
- static const uint8_t SMOKE[3] = {215, 215, 225};
- float j = (clock / 40) & 1 ? 0.25f : -0.25f;
- for (int side = -1; side <= 1; side += 2)
- tri3(at(&F, side * 1.25f + j, 0.10f, -2.1f), at(&F, side * 0.65f + j, 0.10f, -2.6f), at(&F, side * 0.95f, 0.75f, -2.5f),
- SMOKE, 1);
- }
-}
-
-/* Boris-TH's horses: a galloping body, head turned with the steering */
-static void hq(const frame *F, float ax, float ay, float az, float bx, float by, float bz, float cx, float cy, float cz,
- float dx, float dy, float dz, const uint8_t *rgb, uint8_t stip) {
- v3 p[4] = {at(F, ax, ay, az), at(F, bx, by, bz), at(F, cx, cy, cz), at(F, dx, dy, dz)};
- poly(p, 4, rgb, stip);
-}
-static void ht(const frame *F, float ax, float ay, float az, float bx, float by, float bz, float cx, float cy, float cz,
- const uint8_t *rgb, uint8_t stip) {
- tri3(at(F, ax, ay, az), at(F, bx, by, bz), at(F, cx, cy, cz), rgb, stip);
-}
-
-static float fabsf_(float x) { return x < 0 ? -x : x; }
-static float fsin(float x) { /* x in radians */
- int32_t a = (int32_t)(x * 10430.378f);
- return fsinu(a);
-}
-
-static void horse(const frame *F, const car_pose *P, uint32_t clock) {
- bool brown = P->model == 4, g = P->ghost;
- uint8_t stip = g ? 1 : 0;
- /* top, side, dark, mane: the ghost, the brown mustang, the black stallion */
- static const uint8_t COAT[3][4][3] = {{{70, 220, 250}, {55, 185, 225}, {40, 140, 180}, {90, 240, 255}},
- {{168, 98, 50}, {140, 78, 38}, {96, 52, 24}, {36, 22, 14}},
- {{38, 38, 48}, {24, 24, 32}, {14, 14, 20}, {10, 10, 14}}};
- int coat = g ? 0 : brown ? 1 : 2;
- const uint8_t *top = COAT[coat][0], *side = COAT[coat][1], *dark = COAT[coat][2], *mane = COAT[coat][3];
- float speed = P->near ? 1.0f : 0.8f;
- float phase = (float)clock * 0.0168f * speed;
- float bob = 0.06f * fabsf_(fsin(phase));
- float turn = clampf(P->steer * 0.45f, -0.45f, 0.45f);
- if (!g) {
- static const uint8_t SH[3] = {14, 14, 20};
- hq(F, -0.6f, 0.04f, -1.85f, 0.6f, 0.04f, -1.85f, 0.6f, 0.04f, 1.85f, -0.6f, 0.04f, 1.85f, SH, 0);
- }
- float y0 = 0.86f + bob, y1 = 1.44f + bob;
- /* the body */
- hq(F, -0.42f, y0, -1.38f, 0.42f, y0, -1.38f, 0.38f, y1, -1.28f, -0.38f, y1, -1.28f, dark, stip);
- hq(F, -0.38f, y1, -1.28f, 0.38f, y1, -1.28f, 0.40f, y1 + 0.02f, 0.08f, -0.40f, y1 + 0.02f, 0.08f, top, stip);
- hq(F, -0.40f, y1 + 0.02f, 0.08f, 0.40f, y1 + 0.02f, 0.08f, 0.36f, y1 + 0.08f, 1.18f, -0.36f, y1 + 0.08f, 1.18f, top, stip);
- hq(F, -0.36f, y1 + 0.08f, 1.18f, 0.36f, y1 + 0.08f, 1.18f, 0.42f, y0, 1.25f, -0.42f, y0, 1.25f, side, stip);
- hq(F, -0.42f, y0, -1.38f, -0.46f, y0 - 0.04f, 0.05f, -0.40f, y1 + 0.02f, 0.08f, -0.38f, y1, -1.28f, side, stip);
- hq(F, -0.46f, y0 - 0.04f, 0.05f, -0.42f, y0, 1.25f, -0.36f, y1 + 0.08f, 1.18f, -0.40f, y1 + 0.02f, 0.08f, side, stip);
- hq(F, 0.46f, y0 - 0.04f, 0.05f, 0.42f, y0, -1.38f, 0.38f, y1, -1.28f, 0.40f, y1 + 0.02f, 0.08f, side, stip);
- hq(F, 0.42f, y0, 1.25f, 0.46f, y0 - 0.04f, 0.05f, 0.40f, y1 + 0.02f, 0.08f, 0.36f, y1 + 0.08f, 1.18f, side, stip);
- if (!g) { /* the saddle */
- uint8_t sd[3] = {(uint8_t)(brown ? 75 : 185), (uint8_t)(brown ? 42 : 28), (uint8_t)(brown ? 20 : 35)};
- hq(F, -0.42f, y1 + 0.04f, -0.48f, 0.42f, y1 + 0.04f, -0.48f, 0.42f, y1 + 0.05f, 0.42f, -0.42f, y1 + 0.05f, 0.42f, sd, 0);
- }
- /* the neck and head */
- float nt = turn * 0.32f;
- hq(F, -0.22f, y1 - 0.06f, 0.88f, 0.22f, y1 - 0.06f, 0.88f, nt + 0.18f, y1 + 0.76f, 1.38f, nt - 0.18f, y1 + 0.76f, 1.38f, top, stip);
- hq(F, -0.20f, y1 - 0.18f, 1.34f, -0.22f, y1 - 0.06f, 0.88f, nt - 0.18f, y1 + 0.76f, 1.38f, nt - 0.16f, y1 + 0.52f, 1.72f, side, stip);
- hq(F, 0.22f, y1 - 0.06f, 0.88f, 0.20f, y1 - 0.18f, 1.34f, nt + 0.16f, y1 + 0.52f, 1.72f, nt + 0.18f, y1 + 0.76f, 1.38f, side, stip);
- hq(F, -0.20f, y1 - 0.18f, 1.34f, 0.20f, y1 - 0.18f, 1.34f, nt + 0.16f, y1 + 0.52f, 1.72f, nt - 0.16f, y1 + 0.52f, 1.72f, dark, stip);
- ht(F, 0.0f, y1 + 0.06f, 0.65f, nt * 0.5f, y1 + 0.52f, 0.95f, nt, y1 + 0.86f, 1.35f, mane, stip);
- float sx = turn * 0.52f;
- static const uint8_t WHITE_BLAZE[3] = {235, 230, 220};
- uint8_t blaze[3];
- for (int i = 0; i < 3; i++) blaze[i] = brown && !g ? WHITE_BLAZE[i] : top[i];
- hq(F, nt - 0.18f, y1 + 0.76f, 1.38f, nt + 0.18f, y1 + 0.76f, 1.38f, sx + 0.12f, y1 + 0.45f, 2.18f, sx - 0.12f, y1 + 0.45f, 2.18f, blaze, stip);
- hq(F, nt - 0.16f, y1 + 0.52f, 1.72f, nt - 0.18f, y1 + 0.76f, 1.38f, sx - 0.12f, y1 + 0.45f, 2.18f, sx - 0.11f, y1 + 0.25f, 2.10f, side, stip);
- hq(F, nt + 0.18f, y1 + 0.76f, 1.38f, nt + 0.16f, y1 + 0.52f, 1.72f, sx + 0.11f, y1 + 0.25f, 2.10f, sx + 0.12f, y1 + 0.45f, 2.18f, side, stip);
- hq(F, sx - 0.12f, y1 + 0.45f, 2.18f, sx + 0.12f, y1 + 0.45f, 2.18f, sx + 0.11f, y1 + 0.25f, 2.10f, sx - 0.11f, y1 + 0.25f, 2.10f, dark, stip);
- ht(F, nt - 0.18f, y1 + 0.74f, 1.40f, nt - 0.08f, y1 + 0.74f, 1.46f, nt - 0.14f, y1 + 1.02f, 1.36f, side, stip);
- ht(F, nt + 0.08f, y1 + 0.74f, 1.46f, nt + 0.18f, y1 + 0.74f, 1.40f, nt + 0.14f, y1 + 1.02f, 1.36f, side, stip);
- /* the tail */
- float sway = fsin(phase * 1.3f) * 0.20f - turn * 0.25f, lift = P->boost ? 0.32f : 0.0f;
- hq(F, -0.14f, y1 - 0.06f, -1.34f, 0.14f, y1 - 0.06f, -1.34f, sway + 0.22f, 0.85f + lift, -1.85f, sway - 0.22f, 0.85f + lift, -1.85f, mane, stip);
- ht(F, sway - 0.22f, 0.85f + lift, -1.85f, sway + 0.22f, 0.85f + lift, -1.85f, sway * 1.3f, 0.32f + lift, -2.20f, mane, stip);
- /* four legs at a gallop */
- static const float LX[4] = {-0.32f, 0.32f, -0.30f, 0.30f}, LZ[4] = {-1.10f, -1.10f, 1.02f, 1.02f}, LP[4] = {0.0f, 2.7f, 1.4f, 4.1f};
- for (int l = 0; l < 4; l++) {
- float a = phase + LP[l];
- float swing = fsin(a) * 0.52f, up = clampf(fsin(a + 1.5708f) * 0.28f, 0.0f, 0.30f);
- float tx = LX[l], tz = LZ[l], bz = LZ[l] + swing, by = 0.04f + up;
- hq(F, tx - 0.09f, y0 + 0.08f, tz - 0.10f, tx + 0.09f, y0 + 0.08f, tz + 0.10f, tx + 0.07f, by, bz + 0.08f, tx - 0.07f, by, bz - 0.08f, dark, stip);
- hq(F, tx, y0 + 0.08f, tz - 0.12f, tx, y0 + 0.08f, tz + 0.12f, tx, by, bz + 0.10f, tx, by, bz - 0.10f, side, stip);
- if (!g) {
- uint8_t hf[3] = {(uint8_t)(P->brake ? 255 : P->boost ? 40 : brown ? 225 : 195),
- (uint8_t)(P->brake ? 55 : P->boost ? 235 : brown ? 215 : 165),
- (uint8_t)(P->brake ? 35 : P->boost ? 255 : brown ? 195 : 90)};
- hq(F, tx - 0.10f, by, bz - 0.11f, tx + 0.10f, by, bz - 0.11f, tx + 0.10f, by + 0.16f, bz + 0.11f, tx - 0.10f, by + 0.16f, bz + 0.11f, hf, 0);
- }
- }
-}
-
-/* ------------------------------------------------------------------ the road */
-enum { O_OUTL, O_WALLL, O_VERGEL, O_KERBL, O_LINEL, O_CL, O_CR, O_LINER, O_KERBR, O_VERGER, O_WALLR, O_OUTR, O_TOPL, O_TOPR, O_N };
-static const float OFF[12] = {-38.5f, -12.5f, -9.2f, -8.0f, -7.7f, -0.15f, 0.15f, 7.7f, 8.0f, 9.2f, 12.5f, 38.5f};
-static v3 node_pts[DRAW_SEGS + 4][O_N];
-
-static void node_points(const pg_track *t, int32_t k, v3 *out) {
- int i = pg_slot(t, k);
- float nx = fcosu(t->nh[i]), nz = -fsinu(t->nh[i]);
- float bx = (float)(t->px[i] - cam_x) * (1.0f / FX), by = (float)(t->py[i] - cam_y) * (1.0f / FX),
- bz = (float)(t->pz[i] - cam_z) * (1.0f / FX);
- for (int o = 0; o < 12; o++) out[o] = to_cam_f(bx + nx * OFF[o], by, bz + nz * OFF[o]);
- out[O_TOPL] = to_cam_f(bx + nx * OFF[O_WALLL], by + 0.9f, bz + nz * OFF[O_WALLL]);
- out[O_TOPR] = to_cam_f(bx + nx * OFF[O_WALLR], by + 0.9f, bz + nz * OFF[O_WALLR]);
-}
-
-static void shade3(uint8_t *o, const uint8_t *c, int d) {
- for (int i = 0; i < 3; i++) o[i] = cl8(c[i] + d);
-}
-
-static void road_segment(const pg_track *t, int32_t k, const v3 *A, const v3 *B, int dist) {
- const theme *th = &THEMES[t->zone[pg_slot(t, k)] & 7];
- bool odd = k & 1;
- uint8_t road[3], kerb[3], white[3] = {225, 225, 230}, verge[3], out[3], wall[3];
- shade3(road, th->road, odd ? 0 : 6);
- shade3(verge, th->ground, odd ? 0 : 10);
- shade3(out, th->ground, odd ? -6 : -2);
- for (int i = 0; i < 3; i++) kerb[i] = odd ? white[i] : th->kerb[i];
- shade3(wall, th->wall, odd ? 0 : -24);
- bool detail = dist < DETAIL_SEGS;
- /* the start line: a band of squares */
- if (!t->endless && pg_slot(t, k) == 0) {
- static const uint8_t CHECK[2][3] = {{245, 245, 245}, {25, 25, 30}};
- for (int c = 0; c < 8; c++) {
- float f0 = (float)c / 8, f1 = (float)(c + 1) / 8;
- for (int row = 0; row < 2; row++) {
- float g0 = row * 0.5f, g1 = g0 + 0.5f;
- v3 la = {A[O_KERBL].x + (A[O_KERBR].x - A[O_KERBL].x) * f0, A[O_KERBL].y + (A[O_KERBR].y - A[O_KERBL].y) * f0, A[O_KERBL].z + (A[O_KERBR].z - A[O_KERBL].z) * f0};
- v3 lb = {A[O_KERBL].x + (A[O_KERBR].x - A[O_KERBL].x) * f1, A[O_KERBL].y + (A[O_KERBR].y - A[O_KERBL].y) * f1, A[O_KERBL].z + (A[O_KERBR].z - A[O_KERBL].z) * f1};
- v3 ma = {B[O_KERBL].x + (B[O_KERBR].x - B[O_KERBL].x) * f0, B[O_KERBL].y + (B[O_KERBR].y - B[O_KERBL].y) * f0, B[O_KERBL].z + (B[O_KERBR].z - B[O_KERBL].z) * f0};
- v3 mb = {B[O_KERBL].x + (B[O_KERBR].x - B[O_KERBL].x) * f1, B[O_KERBL].y + (B[O_KERBR].y - B[O_KERBL].y) * f1, B[O_KERBL].z + (B[O_KERBR].z - B[O_KERBL].z) * f1};
- v3 p0 = {la.x + (ma.x - la.x) * g0 * 0.4f, la.y + (ma.y - la.y) * g0 * 0.4f, la.z + (ma.z - la.z) * g0 * 0.4f};
- v3 p1 = {lb.x + (mb.x - lb.x) * g0 * 0.4f, lb.y + (mb.y - lb.y) * g0 * 0.4f, lb.z + (mb.z - lb.z) * g0 * 0.4f};
- v3 p2 = {lb.x + (mb.x - lb.x) * g1 * 0.4f, lb.y + (mb.y - lb.y) * g1 * 0.4f, lb.z + (mb.z - lb.z) * g1 * 0.4f};
- v3 p3 = {la.x + (ma.x - la.x) * g1 * 0.4f, la.y + (ma.y - la.y) * g1 * 0.4f, la.z + (ma.z - la.z) * g1 * 0.4f};
- quad(p0, p1, p2, p3, CHECK[(c + row) & 1]);
- }
- /* the rest of the segment: road */
- }
- v3 m0 = {A[O_KERBL].x + (B[O_KERBL].x - A[O_KERBL].x) * 0.4f, A[O_KERBL].y + (B[O_KERBL].y - A[O_KERBL].y) * 0.4f, A[O_KERBL].z + (B[O_KERBL].z - A[O_KERBL].z) * 0.4f};
- v3 m1 = {A[O_KERBR].x + (B[O_KERBR].x - A[O_KERBR].x) * 0.4f, A[O_KERBR].y + (B[O_KERBR].y - A[O_KERBR].y) * 0.4f, A[O_KERBR].z + (B[O_KERBR].z - A[O_KERBR].z) * 0.4f};
- quad(m0, m1, B[O_KERBR], B[O_KERBL], road);
- } else if (detail) {
- quad(A[O_KERBL], A[O_LINEL], B[O_LINEL], B[O_KERBL], white);
- quad(A[O_LINER], A[O_KERBR], B[O_KERBR], B[O_LINER], white);
- if (!odd) {
- static const uint8_t YEL[3] = {235, 205, 70};
- quad(A[O_LINEL], A[O_CL], B[O_CL], B[O_LINEL], road);
- quad(A[O_CL], A[O_CR], B[O_CR], B[O_CL], YEL);
- quad(A[O_CR], A[O_LINER], B[O_LINER], B[O_CR], road);
- } else {
- quad(A[O_LINEL], A[O_LINER], B[O_LINER], B[O_LINEL], road);
- }
- } else {
- quad(A[O_KERBL], A[O_KERBR], B[O_KERBR], B[O_KERBL], road);
- }
- quad(A[O_VERGEL], A[O_KERBL], B[O_KERBL], B[O_VERGEL], kerb);
- quad(A[O_KERBR], A[O_VERGER], B[O_VERGER], B[O_KERBR], kerb);
- quad(A[O_WALLL], A[O_VERGEL], B[O_VERGEL], B[O_WALLL], verge);
- quad(A[O_VERGER], A[O_WALLR], B[O_WALLR], B[O_VERGER], verge);
- if (dist < 26) {
- quad(A[O_OUTL], A[O_WALLL], B[O_WALLL], B[O_OUTL], out);
- quad(A[O_WALLR], A[O_OUTR], B[O_OUTR], B[O_WALLR], out);
- }
- if (dist < 24) { /* the barriers: their side facing the road */
- quad(A[O_WALLL], B[O_WALLL], B[O_TOPL], A[O_TOPL], wall);
- quad(B[O_WALLR], A[O_WALLR], A[O_TOPR], B[O_TOPR], wall);
- }
-}
-
-/* what stands at a segment */
-static void prop(const pg_track *t, int32_t k, int dist, uint32_t clock) {
- int i = pg_slot(t, k);
- int p = t->prop[i] & 7, left = t->prop[i] & 8;
- const theme *th = &THEMES[t->zone[i] & 7];
- float nx = fcosu(t->nh[i]), nz = -fsinu(t->nh[i]), fx = fsinu(t->nh[i]), fz = fcosu(t->nh[i]);
- float bx = (float)(t->px[i] - cam_x) * (1.0f / FX), by = (float)(t->py[i] - cam_y) * (1.0f / FX),
- bz = (float)(t->pz[i] - cam_z) * (1.0f / FX);
- (void)clock;
- if (p == PROP_ARCH || p == PROP_GANTRY) {
- float aw = 13.2f, ah = p == PROP_ARCH ? 7.0f : 6.2f;
- static const uint8_t PIL[3] = {200, 200, 215}, PIL2[3] = {150, 150, 165};
- for (int s = -1; s <= 1; s += 2) {
- float px = bx + nx * aw * (float)s, pz = bz + nz * aw * (float)s;
- float w = 0.45f;
- v3 a = to_cam_f(px - fx * w, by, pz - fz * w), b = to_cam_f(px + fx * w, by, pz + fz * w);
- v3 c = to_cam_f(px + fx * w, by + ah, pz + fz * w), d = to_cam_f(px - fx * w, by + ah, pz - fz * w);
- quad(a, b, c, d, PIL);
- v3 e = to_cam_f(px - nx * w * (float)s - fx * w, by, pz - nz * w * (float)s - fz * w),
- f = to_cam_f(px - nx * w * (float)s - fx * w, by + ah, pz - nz * w * (float)s - fz * w);
- quad(a, e, f, d, PIL2);
- }
- if (p == PROP_ARCH) {
- bool start = !t->endless && i == 0;
- float y0 = by + ah - 1.4f, y1 = by + ah;
- int parts = start ? 8 : 1;
- static const uint8_t CHECK[2][3] = {{245, 245, 245}, {25, 25, 30}};
- for (int c = 0; c < parts; c++) {
- float s0 = -aw + 2 * aw * (float)c / (float)parts, s1 = -aw + 2 * aw * (float)(c + 1) / (float)parts;
- v3 a = to_cam_f(bx + nx * s0, y0, bz + nz * s0), b = to_cam_f(bx + nx * s1, y0, bz + nz * s1);
- v3 cc = to_cam_f(bx + nx * s1, y1, bz + nz * s1), d = to_cam_f(bx + nx * s0, y1, bz + nz * s0);
- if (start) quad(a, b, cc, d, CHECK[c & 1]);
- else quad(a, b, cc, d, th->kerb);
- }
- } else {
- static const uint8_t BEAM[3] = {70, 74, 90}, LIGHT[3] = {255, 240, 190};
- float y0 = by + ah - 0.5f, y1 = by + ah;
- v3 a = to_cam_f(bx - nx * aw, y0, bz - nz * aw), b = to_cam_f(bx + nx * aw, y0, bz + nz * aw);
- v3 c = to_cam_f(bx + nx * aw, y1, bz + nz * aw), d = to_cam_f(bx - nx * aw, y1, bz - nz * aw);
- quad(a, b, c, d, BEAM);
- if (dist < 20)
- for (int l = -1; l <= 1; l++) {
- float c0 = (float)l * 5.0f - 0.9f, c1 = c0 + 1.8f, ly = y0 - 0.02f;
- v3 p0 = to_cam_f(bx + nx * c0, ly, bz + nz * c0), p1 = to_cam_f(bx + nx * c1, ly, bz + nz * c1);
- v3 p2 = to_cam_f(bx + nx * c1 + fx * 0.6f, ly, bz + nz * c1 + fz * 0.6f), p3 = to_cam_f(bx + nx * c0 + fx * 0.6f, ly, bz + nz * c0 + fz * 0.6f);
- quad(p0, p1, p2, p3, LIGHT);
- }
- }
- return;
- }
- float side = left ? -1.0f : 1.0f;
- if (p == PROP_TREE) {
- if (dist > 26) return;
- float d = side * (12.5f + 3.6f);
- float tx = bx + nx * d, tz = bz + nz * d;
- if (th->lamps) {
- static const uint8_t POLE[3] = {70, 80, 100};
- float w = 0.16f;
- v3 a = to_cam_f(tx - fx * w, by, tz - fz * w), b = to_cam_f(tx + fx * w, by, tz + fz * w);
- v3 c = to_cam_f(tx + fx * w, by + 7.0f, tz + fz * w), dd = to_cam_f(tx - fx * w, by + 7.0f, tz - fz * w);
- quad(a, b, c, dd, POLE);
- v3 e = to_cam_f(tx - nx * w, by, tz - nz * w), f = to_cam_f(tx + nx * w, by, tz + nz * w);
- v3 g = to_cam_f(tx + nx * w, by + 7.0f, tz + nz * w), h = to_cam_f(tx - nx * w, by + 7.0f, tz - nz * w);
- quad(e, f, g, h, POLE);
- /* the lamp, leaning over the road */
- float hx = tx - nx * side * 2.2f, hz = tz - nz * side * 2.2f;
- v3 l0 = to_cam_f(tx, by + 7.0f, tz), l1 = to_cam_f(hx, by + 7.2f, hz);
- v3 l2 = to_cam_f(hx + fx * 0.5f, by + 7.0f, hz + fz * 0.5f);
- tri3(l0, l1, l2, POLE, 0);
- v3 q0 = to_cam_f(hx - fx * 0.5f, by + 6.95f, hz - fz * 0.5f), q1 = to_cam_f(hx + fx * 0.5f, by + 6.95f, hz + fz * 0.5f);
- v3 q2 = to_cam_f(hx + fx * 0.5f + nx * 0.6f, by + 6.95f, hz + fz * 0.5f + nz * 0.6f), q3 = to_cam_f(hx - fx * 0.5f + nx * 0.6f, by + 6.95f, hz - fz * 0.5f + nz * 0.6f);
- quad(q0, q1, q2, q3, th->tree);
- return;
- }
- /* a cone of five sides on a short trunk, lit from the left */
- static const uint8_t TRUNK[3] = {70, 48, 30};
- float r = 2.1f, h = 6.8f;
- v3 apex = to_cam_f(tx, by + h, tz);
- v3 base[5];
- for (int s = 0; s < 5; s++) {
- int32_t a = s * 13107 + (int32_t)(k * 977);
- base[s] = to_cam_f(tx + fsinu(a) * r, by + 0.7f, tz + fcosu(a) * r);
- }
- v3 cen = to_cam_f(tx, by + 2.5f, tz);
- for (int s = 0; s < 5; s++) {
- v3 p[3] = {base[s], base[(s + 1) % 5], apex};
- if (!facing(p, 3, cen)) continue;
- int32_t a = s * 13107 + 6553 + (int32_t)(k * 977) - (int32_t)t->nh[i];
- uint8_t c[3];
- shade3(c, th->tree, (int)(fsinu(a) * 22.0f));
- poly(p, 3, c, 0);
- }
- float w = 0.25f;
- v3 a = to_cam_f(tx - fx * w, by, tz - fz * w), b = to_cam_f(tx + fx * w, by, tz + fz * w);
- v3 c = to_cam_f(tx + fx * w, by + 0.8f, tz + fz * w), dd = to_cam_f(tx - fx * w, by + 0.8f, tz - fz * w);
- quad(a, b, c, dd, TRUNK);
- return;
- }
- if (p == PROP_CHEVRON) {
- if (dist > 22) return;
- float d = side * 13.0f;
- float sx = bx + nx * d, sz = bz + nz * d;
- static const uint8_t YEL[3] = {240, 185, 20}, BLK[3] = {25, 25, 30}, POST[3] = {90, 90, 100};
- v3 a = to_cam_f(sx - nx * 1.3f, by + 0.6f, sz - nz * 1.3f), b = to_cam_f(sx + nx * 1.3f, by + 0.6f, sz + nz * 1.3f);
- v3 c = to_cam_f(sx + nx * 1.3f, by + 2.2f, sz + nz * 1.3f), dd = to_cam_f(sx - nx * 1.3f, by + 2.2f, sz - nz * 1.3f);
- quad(a, b, c, dd, YEL);
- v3 p0 = to_cam_f(sx - nx * 0.15f, by, sz - nz * 0.15f), p1 = to_cam_f(sx + nx * 0.15f, by, sz + nz * 0.15f);
- v3 p2 = to_cam_f(sx + nx * 0.15f, by + 0.6f, sz + nz * 0.15f), p3 = to_cam_f(sx - nx * 0.15f, by + 0.6f, sz - nz * 0.15f);
- quad(p0, p1, p2, p3, POST);
- /* the arrow points into the bend (away from the side it stands on) */
- float dir = -side, e = -0.05f;
- for (int c2 = -1; c2 <= 1; c2 += 2) {
- float o = (float)c2 * 0.55f;
- v3 t0 = to_cam_f(sx + nx * (o - dir * 0.35f) + fx * e, by + 0.8f, sz + nz * (o - dir * 0.35f) + fz * e);
- v3 t1 = to_cam_f(sx + nx * (o + dir * 0.35f) + fx * e, by + 1.4f, sz + nz * (o + dir * 0.35f) + fz * e);
- v3 t2 = to_cam_f(sx + nx * (o - dir * 0.35f) + fx * e, by + 2.0f, sz + nz * (o - dir * 0.35f) + fz * e);
- tri3(t0, t1, t2, BLK, 0);
- }
- }
-}
-
-static void orb(const pg_track *t, int32_t k, uint32_t clock) {
- int i = pg_slot(t, k);
- int32_t s = k * t->seg + t->seg / 2, wx, wy, wz;
- pg_world(t, s, t->orb[i] * FX / 8, &wx, &wy, &wz);
- float ox = (float)(wx - cam_x) * (1.0f / FX), oz = (float)(wz - cam_z) * (1.0f / FX), gy = (float)(wy - cam_y) * (1.0f / FX);
- float oy = gy + 1.25f + 0.25f * fsinu((int32_t)(clock * 6) + k * 3000);
- int32_t ang = (int32_t)(clock * 16) + k * 5000;
- float rx = fcosu(ang) * 1.1f, rz = fsinu(ang) * 1.1f;
- v3 top = to_cam_f(ox, oy + 1.05f, oz), bot = to_cam_f(ox, oy - 0.85f, oz);
- v3 l = to_cam_f(ox - rx, oy + 0.1f, oz - rz), r = to_cam_f(ox + rx, oy + 0.1f, oz + rz);
- v3 f = to_cam_f(ox - rz, oy + 0.1f, oz + rx), b = to_cam_f(ox + rz, oy + 0.1f, oz - rx);
- static const uint8_t C0[3] = {60, 255, 255}, C1[3] = {0, 210, 255}, C2[3] = {20, 150, 245}, C3[3] = {255, 230, 80},
- PAD[3] = {0, 150, 210};
- v3 centre = to_cam_f(ox, oy, oz);
- v3 faces[8][3] = {{top, l, f}, {top, f, r}, {top, r, b}, {top, b, l}, {bot, f, l}, {bot, r, f}, {bot, b, r}, {bot, l, b}};
- const uint8_t *cols[8] = {C0, C1, C0, C1, C2, C3, C2, C3};
- for (int q = 0; q < 8; q++)
- if (facing(faces[q], 3, centre)) poly(faces[q], 3, cols[q], 0);
- float nx = fcosu(t->nh[i]), nz = -fsinu(t->nh[i]), fx = fsinu(t->nh[i]), fz = fcosu(t->nh[i]);
- v3 p0 = to_cam_f(ox - nx * 1.1f, gy + 0.05f, oz - nz * 1.1f), p1 = to_cam_f(ox + fx * 1.4f, gy + 0.05f, oz + fz * 1.4f);
- v3 p2 = to_cam_f(ox + nx * 1.1f, gy + 0.05f, oz + nz * 1.1f), p3 = to_cam_f(ox - fx * 1.4f, gy + 0.05f, oz - fz * 1.4f);
- quad(p0, p1, p2, p3, PAD);
-}
-
-/* ------------------------------------------------------------------ the background */
-static void sky_for(int zone_a, int zone_b, int t256) {
- const theme *a = &THEMES[zone_a & 7], *b = &THEMES[zone_b & 7];
- mix3(sky_top, a->sky_top, b->sky_top, t256);
- mix3(sky_bot, a->sky_bot, b->sky_bot, t256);
- mix3(ground, a->ground, b->ground, t256);
- memcpy(fog, sky_bot, 3);
- for (int i = 0; i < 3; i++) hill[i] = (uint8_t)((sky_top[i] * 3 + sky_bot[i]) / 5);
-}
-
-static void setup_camera(int32_t x, int32_t y, int32_t z, int32_t yaw, float pitch, float roll) {
- cam_x = x, cam_y = y, cam_z = z, cam_yaw = yaw;
- cy_c = fcosu(yaw), cy_s = fsinu(yaw);
- float p2 = pitch * pitch, r2 = roll * roll;
- cp_c = 1 - p2 / 2 + p2 * p2 / 24, cp_s = pitch - pitch * p2 / 6;
- cr_c = 1 - r2 / 2, cr_s = roll - roll * r2 / 6;
- horizon = (int)(CY + FOCAL * cp_s / cp_c);
- /* the hills on the horizon turn with the camera: 1024 pixels a turn; three ranges laid over each other */
- int scroll = (int)(((uint32_t)yaw & 0xFFFF) >> 6);
- for (int x2 = 0; x2 < SCREEN_W; x2++) {
- int m = (x2 + scroll) & 1023;
- int a = (m % 128) - 64, b = ((m + 40) % 52) - 26, c = ((m + 11) % 20) - 10;
- int h = 26 - (a < 0 ? -a : a) * 14 / 64 - (b < 0 ? -b : b) * 7 / 26 - (c < 0 ? -c : c) * 3 / 10;
- hills[x2] = (uint8_t)(h < 1 ? 1 : h);
- }
- ntris = 0, rd_dropped = 0;
-}
-
-/* ------------------------------------------------------------------ frames */
-static int32_t lerp(int32_t a, int32_t b, int alpha) { return a + (int32_t)((int64_t)(b - a) * alpha / 256); }
-
-void rd_race(const pg_track *t, const pg_race *a, const pg_race *b, int alpha, int view, const rd_ghost *g,
- uint32_t clock) {
- const pg_car *ca = &a->car[0], *cb = &b->car[0];
- int32_t s = lerp(ca->s, cb->s, alpha), x = lerp(ca->x, cb->x, alpha);
- int32_t ang = ca->ang + (int32_t)((int64_t)(int16_t)(cb->ang - ca->ang) * alpha / 256);
- int32_t head = pg_heading_at(t, s) + ang;
- int32_t wx, wy, wz;
- pg_world(t, s, x, &wx, &wy, &wz);
- /* behind the car, a little above, looking where it goes */
- float dist = view == VIEW_FAR ? 9.5f : view == VIEW_BUMPER ? -0.6f : 6.8f;
- float high = view == VIEW_FAR ? 3.35f : view == VIEW_BUMPER ? 1.55f : 2.4f;
- bool horse_ = pg_vehicles[cb->model].horse;
- if (horse_ && view == VIEW_CHASE) dist = 6.6f, high = 2.5f;
- if (horse_ && view == VIEW_BUMPER) high = 2.6f, dist = -0.2f;
- int32_t sh = pg_sin(head), ch = pg_cos(head);
- int32_t cx = wx - (int32_t)(dist * (float)sh * (FX / 32767.0f)), cz = wz - (int32_t)(dist * (float)ch * (FX / 32767.0f));
- int32_t ground_y = wy;
- int32_t slope = pg_height_at(t, s + 12 * FX) - pg_height_at(t, s);
- float pitch = -0.11f + clampf((float)slope / (12.0f * FX) * 0.35f, -0.08f, 0.08f);
- float roll = -(float)cb->steer / 1024.0f * clampf((float)cb->v / (75.0f * FX), 0, 1) * 0.15f * 0.35f;
- setup_camera(cx, ground_y + (int32_t)(high * FX), cz, head, pitch, roll);
- /* the sky: the scenery here, blending into the one ahead */
- int32_t k0 = pg_seg_of(t, s);
- int za = t->zone[pg_slot(t, k0)], zb = t->zone[pg_slot(t, k0 + 14)], blend = 0;
- if (za != zb)
- for (int k = 1; k <= 14; k++) blend += t->zone[pg_slot(t, k0 + k)] == zb ? 18 : 0;
- sky_for(za, zb, blend > 256 ? 256 : blend);
- /* the road, from far to near (the depth buffer sorts it out, but the far pieces go first if room runs out) */
- int32_t kc = pg_seg_of(t, s - (int32_t)(dist > 0 ? dist * FX : 0)) - 1;
- int32_t kend = k0 + DRAW_SEGS;
- if (t->endless && kend > t->last) kend = t->last;
- if (t->endless && kc < t->first) kc = t->first;
- int nn = (int)(kend - kc + 1);
- if (nn > DRAW_SEGS + 4) nn = DRAW_SEGS + 4;
- for (int j = 0; j < nn; j++) node_points(t, kc + j, node_pts[j]);
- for (int j = nn - 2; j >= 0; j--) road_segment(t, kc + j, node_pts[j], node_pts[j + 1], (int)(kc + j - k0));
- for (int j = nn - 2; j >= 0; j--) {
- int32_t k = kc + j;
- int i = pg_slot(t, k);
- if (t->prop[i] & 7) prop(t, k, (int)(k - k0), clock);
- if (t->orb[i] != -128 && k - k0 < 28 && k >= k0 - 1) {
- bool here = t->endless ? k > b->last_orb : t->orb_id[i] >= 0 && !b->orb_wait[t->orb_id[i]];
- if (here) orb(t, k, clock);
- }
- }
- /* the rivals */
- for (int i = 1; i <= b->rivals; i++) {
- const pg_car *pa = &a->car[i], *pb = &b->car[i];
- int32_t rs = lerp(pa->s, pb->s, alpha);
- if (!t->endless) rs = s + pg_wrap(t, rs - s);
- if (rs < s - 30 * FX || rs > s + (DRAW_SEGS - 2) * t->seg) continue;
- car_pose P;
- memset(&P, 0, sizeof P);
- pg_world(t, rs, lerp(pa->x, pb->x, alpha), &P.wx, &P.wy, &P.wz);
- P.head = pg_heading_at(t, rs) + pb->ang;
- P.model = pb->model, P.paint = rd_paint[pb->paint & 7];
- P.brake = pb->v < pa->v - FX / 4, P.boost = pb->boost;
- car(&P, clock + (uint32_t)i * 97);
- }
- /* the ghost of your best lap */
- if (g && g->on) {
- int32_t gs = s + pg_wrap(t, g->s - pg_lap_s(b, t));
- if (gs > s - 20 * FX && gs < s + (DRAW_SEGS - 2) * t->seg) {
- car_pose P;
- memset(&P, 0, sizeof P);
- pg_world(t, gs, g->x, &P.wx, &P.wy, &P.wz);
- P.head = pg_heading_at(t, gs) + g->ang, P.model = g->model, P.ghost = true, P.near = true;
- P.paint = pg_vehicles[g->model].body;
- car(&P, clock);
- }
- }
- /* your car */
- if (view != VIEW_BUMPER) {
- car_pose P;
- memset(&P, 0, sizeof P);
- P.wx = wx, P.wy = wy, P.wz = wz;
- int32_t slip = ca->slip + (int32_t)((int64_t)(cb->slip - ca->slip) * alpha / 256);
- P.head = head + slip;
- P.model = cb->model, P.paint = pg_vehicles[cb->model].body;
- float spd = clampf((float)cb->v / (75.0f * FX), 0, 1);
- P.roll = clampf(-(float)cb->steer / 1024.0f * spd * 0.15f - (float)slip * (0.14f / 4700.0f), -0.2f, 0.2f);
- if (cb->bump || (cb->x > PG_ROAD || cb->x < -PG_ROAD)) P.roll += (clock / 40) & 1 ? 0.03f : -0.03f;
- P.pitch = cb->v < ca->v - FX / 6 ? -0.05f : cb->v > ca->v + FX / 8 ? 0.03f : 0.0f;
- P.steer = (float)cb->steer / 1024.0f;
- P.brake = cb->v < ca->v - FX / 6 || cb->drift;
- P.boost = cb->boost, P.flame = b->manual && pg_rpm(b) > pg_vehicles[cb->model].redline - 300;
- P.smoke = (cb->drift || cb->bump) && cb->v > 15 * FX;
- P.near = true;
- car(&P, clock);
- }
-}
-
-void rd_showroom(int vehicle, int32_t angle, int zone, uint32_t clock) {
- /* the car on the right of the screen: the camera to its left */
- setup_camera(-(int32_t)(2.3f * FX), (int32_t)(2.2f * FX), -(int32_t)(7.0f * FX), 0, -0.15f, 0);
- sky_for(zone, zone, 0);
- float ox = 2.3f, oy = -2.2f, oz = 7.0f; /* the turntable's middle, from the camera */
- for (int i = 0; i < 16; i++) {
- int32_t a0 = i * 65536 / 16 + angle / 2, a1 = (i + 1) * 65536 / 16 + angle / 2;
- v3 c = to_cam_f(ox, oy, oz), p = to_cam_f(ox + fcosu(a0) * 3.4f, oy, oz + fsinu(a0) * 3.4f),
- q = to_cam_f(ox + fcosu(a1) * 3.4f, oy, oz + fsinu(a1) * 3.4f);
- uint8_t col[3] = {(uint8_t)((i & 1) ? 40 : 58), (uint8_t)((i & 1) ? 36 : 52), (uint8_t)((i & 1) ? 70 : 92)};
- tri3(c, p, q, col, 0);
- }
- car_pose P;
- memset(&P, 0, sizeof P);
- P.head = angle, P.model = vehicle, P.paint = pg_vehicles[vehicle].body, P.brake = true, P.near = true, P.steer = 0.25f;
- car(&P, clock);
-}
-
-/* ------------------------------------------------------------------ drawing a strip */
-static uint16_t zbuf[SCREEN_W * 24];
-static const uint8_t BAYER[4][4] = {{0, 8, 2, 10}, {12, 4, 14, 6}, {3, 11, 1, 9}, {15, 7, 13, 5}};
-
-static void row_fill(uint16_t *row, int y, const uint8_t *c) {
- uint16_t pat[4];
- for (int i = 0; i < 4; i++) {
- int d = BAYER[y & 3][i];
- int r = c[0] + (d >> 1), g = c[1] + (d >> 2), b = c[2] + (d >> 1);
- pat[i] = (uint16_t)((r > 255 ? 31 : r >> 3) << 11 | (g > 255 ? 63 : g >> 2) << 5 | (b > 255 ? 31 : b >> 3));
- }
- for (int x = 0; x < SCREEN_W; x += 4) row[x] = pat[0], row[x + 1] = pat[1], row[x + 2] = pat[2], row[x + 3] = pat[3];
-}
-
-static void raster(const tri *t, int ys, int ye) {
- float x0 = t->x[0] * (1.0f / 16), y0 = t->y[0] * (1.0f / 16), x1 = t->x[1] * (1.0f / 16), y1 = t->y[1] * (1.0f / 16),
- x2 = t->x[2] * (1.0f / 16), y2 = t->y[2] * (1.0f / 16);
- float w0 = t->w[0] * (1.0f / 65535), w1 = t->w[1] * (1.0f / 65535), w2 = t->w[2] * (1.0f / 65535);
- float dx1 = x1 - x0, dy1 = y1 - y0, dx2 = x2 - x0, dy2 = y2 - y0;
- float det = dx1 * dy2 - dx2 * dy1;
- if (det > -0.01f && det < 0.01f) return;
- float B = ((w1 - w0) * dy2 - (w2 - w0) * dy1) / det, C = (dx1 * (w2 - w0) - dx2 * (w1 - w0)) / det;
- float s02 = (x2 - x0) / (y2 - y0);
- float s01 = y1 > y0 ? (x1 - x0) / (y1 - y0) : 0, s12 = y2 > y1 ? (x2 - x1) / (y2 - y1) : 0;
- int ra = t->y0 > ys ? t->y0 : ys, rb = t->y1 < ye ? t->y1 : ye;
- uint16_t col = t->c;
- for (int r = ra; r < rb; r++) {
- float yc = (float)r + 0.5f;
- float xl = x0 + (yc - y0) * s02;
- float xs = yc < y1 ? x0 + (yc - y0) * s01 : x1 + (yc - y1) * s12;
- float xa = xl < xs ? xl : xs, xb = xl < xs ? xs : xl;
- xa = clampf(xa, -1.0f, SCREEN_W + 1.0f), xb = clampf(xb, -1.0f, SCREEN_W + 1.0f);
- int pa = iceil(xa - 0.5f), pb = iceil(xb - 0.5f);
- if (pa < 0) pa = 0;
- if (pb > SCREEN_W) pb = SCREEN_W;
- if (pb <= pa) continue;
- float wa = w0 + B * ((float)pa + 0.5f - x0) + C * (yc - y0);
- float wb = w0 + B * ((float)pb - 0.5f - x0) + C * (yc - y0);
- int32_t qa = (int32_t)(clampf(wa, 0, 1) * 1073741824.0f), qb = (int32_t)(clampf(wb, 0, 1) * 1073741824.0f);
- int n = pb - pa;
- int32_t dq = n > 1 ? (qb - qa) / (n - 1) : 0;
- uint16_t *z = zbuf + (r - gfx_y0) * SCREEN_W, *p = gfx_buf + (r - gfx_y0) * SCREEN_W;
- if (t->stip) {
- for (int x = pa; x < pb; x++, qa += dq) {
- if ((x + r) & 1) continue;
- uint16_t q = (uint16_t)(qa >> 14 > 65535 ? 65535 : qa >> 14);
- if (q > z[x]) z[x] = q, p[x] = col;
- }
- } else {
- for (int x = pa; x < pb; x++, qa += dq) {
- uint16_t q = (uint16_t)(qa >> 14 > 65535 ? 65535 : qa >> 14);
- if (q > z[x]) z[x] = q, p[x] = col;
- }
- }
- }
-}
-
-void rd_draw(void) {
- int ys = gfx_y0, ye = gfx_y1;
- rd_tris = ntris;
- for (int y = ys; y < ye; y++) {
- uint16_t *row = gfx_buf + (y - ys) * SCREEN_W;
- memset(zbuf + (y - ys) * SCREEN_W, 0, SCREEN_W * 2);
- uint8_t c[3];
- if (y < horizon) {
- int t = horizon > 0 ? y * 256 / horizon : 256;
- mix3(c, sky_top, sky_bot, t > 256 ? 256 : t);
- row_fill(row, y, c);
- uint16_t hc = rd_rgb(hill);
- int up = horizon - y;
- if (up <= 26)
- for (int x = 0; x < SCREEN_W; x++)
- if (hills[x] >= up) row[x] = hc;
- } else {
- int t = SCREEN_H > horizon ? (y - horizon) * 256 / (SCREEN_H - horizon) : 256;
- mix3(c, fog, ground, t > 256 ? 256 : t);
- row_fill(row, y, c);
- }
- }
- for (int i = 0; i < ntris; i++)
- if (tris[i].y1 > ys && tris[i].y0 < ye) raster(&tris[i], ys, ye);
-}
diff --git a/games/polygt3d/src/render.h b/games/polygt3d/src/render.h
deleted file mode 100644
index 6ba52d8a..00000000
--- a/games/polygt3d/src/render.h
+++ /dev/null
@@ -1,25 +0,0 @@
-/* The 3D picture (render.c): the road, the roadside and the cars, flat-shaded triangles drawn with a depth buffer
- * a strip of the screen at a time. */
-#ifndef PG_RENDER_H
-#define PG_RENDER_H
-#include "game.h"
-
-typedef struct {
- bool on;
- int32_t s, x; /* where the ghost is */
- int32_t ang; /* where it points, from the road */
- uint8_t model;
-} rd_ghost;
-
-/* a frame of the race, between the step before (a) and the last one (b), alpha/256 of the way */
-void rd_race(const pg_track *t, const pg_race *a, const pg_race *b, int alpha, int view, const rd_ghost *g,
- uint32_t clock_ms);
-/* a car on a turntable, for the menus; zone: the scenery behind it */
-void rd_showroom(int vehicle, int32_t angle, int zone, uint32_t clock_ms);
-/* draws what was set up into the strip gfx_y0..gfx_y1 of gfx_buf */
-void rd_draw(void);
-/* whether the last frame ran out of room for triangles, and how many it had */
-extern int rd_dropped, rd_tris;
-uint16_t rd_rgb(const uint8_t c[3]);
-extern const uint8_t rd_paint[8][3]; /* the rivals' paints */
-#endif
diff --git a/games/polygt3d/src/track.c b/games/polygt3d/src/track.c
deleted file mode 100644
index 75d703b3..00000000
--- a/games/polygt3d/src/track.c
+++ /dev/null
@@ -1,363 +0,0 @@
-/* The roads: three circuits, each a loop of pieces (so many segments, a bend, a climb) made into segments with
- * eased bends and hills and closed exactly; and the endless road, laid segment by segment ahead of you from a
- * seed, its bends, hills and scenery changing as it goes. Whole numbers only (game.h). */
-#include "game.h"
-
-/* ------------------------------------------------------------------ fixed point */
-static const int16_t SIN[257] = {
- 0, 201, 402, 603, 804, 1005, 1206, 1407, 1608, 1809, 2009, 2210, 2410, 2611, 2811, 3012,
- 3212, 3412, 3612, 3811, 4011, 4210, 4410, 4609, 4808, 5007, 5205, 5404, 5602, 5800, 5998, 6195,
- 6393, 6590, 6786, 6983, 7179, 7375, 7571, 7767, 7962, 8157, 8351, 8545, 8739, 8933, 9126, 9319,
- 9512, 9704, 9896, 10087, 10278, 10469, 10659, 10849, 11039, 11228, 11417, 11605, 11793, 11980, 12167, 12353,
- 12539, 12725, 12910, 13094, 13279, 13462, 13645, 13828, 14010, 14191, 14372, 14553, 14732, 14912, 15090, 15269,
- 15446, 15623, 15800, 15976, 16151, 16325, 16499, 16673, 16846, 17018, 17189, 17360, 17530, 17700, 17869, 18037,
- 18204, 18371, 18537, 18703, 18868, 19032, 19195, 19357, 19519, 19680, 19841, 20000, 20159, 20317, 20475, 20631,
- 20787, 20942, 21096, 21250, 21403, 21554, 21705, 21856, 22005, 22154, 22301, 22448, 22594, 22739, 22884, 23027,
- 23170, 23311, 23452, 23592, 23731, 23870, 24007, 24143, 24279, 24413, 24547, 24680, 24811, 24942, 25072, 25201,
- 25329, 25456, 25582, 25708, 25832, 25955, 26077, 26198, 26319, 26438, 26556, 26674, 26790, 26905, 27019, 27133,
- 27245, 27356, 27466, 27575, 27683, 27790, 27896, 28001, 28105, 28208, 28310, 28411, 28510, 28609, 28706, 28803,
- 28898, 28992, 29085, 29177, 29268, 29358, 29447, 29534, 29621, 29706, 29791, 29874, 29956, 30037, 30117, 30195,
- 30273, 30349, 30424, 30498, 30571, 30643, 30714, 30783, 30852, 30919, 30985, 31050, 31113, 31176, 31237, 31297,
- 31356, 31414, 31470, 31526, 31580, 31633, 31685, 31736, 31785, 31833, 31880, 31926, 31971, 32014, 32057, 32098,
- 32137, 32176, 32213, 32250, 32285, 32318, 32351, 32382, 32412, 32441, 32469, 32495, 32521, 32545, 32567, 32589,
- 32609, 32628, 32646, 32663, 32678, 32692, 32705, 32717, 32728, 32737, 32745, 32752, 32757, 32761, 32765, 32766,
- 32767};
-
-int32_t pg_sin(int32_t a) {
- uint32_t u = (uint32_t)a & 0xFFFF;
- uint32_t q = u >> 14, i = (u >> 6) & 255;
- int32_t f = (int32_t)(u & 63), v;
- if (q & 1) v = SIN[256 - i] + (SIN[255 - i] - SIN[256 - i]) * f / 64;
- else v = SIN[i] + (SIN[i + 1] - SIN[i]) * f / 64;
- return q & 2 ? -v : v;
-}
-int32_t pg_cos(int32_t a) { return pg_sin(a + 16384); }
-
-/* CORDIC: the angle of (x, y) in 1/65536 of a turn, 0 along x, growing towards y */
-static const int32_t ATAN16[16] = {131072, 77376, 40884, 20753, 10417, 5213, 2607, 1304, 652, 326, 163, 81, 41, 20,
- 10, 5};
-int32_t pg_atan2(int32_t y, int32_t x) {
- if (!x && !y) return 0;
- int64_t X = x, Y = y;
- int32_t a = 0;
- if (X < 0) X = -X, Y = -Y, a = 32768 * 16;
- while (X > (1 << 28) || Y > (1 << 28) || Y < -(1 << 28)) X /= 2, Y /= 2;
- X *= 4, Y *= 4;
- for (int i = 0; i < 16; i++) {
- int64_t nx;
- if (Y > 0) nx = X + (Y >> i), Y = Y - (X >> i), X = nx, a += ATAN16[i];
- else nx = X - (Y >> i), Y = Y + (X >> i), X = nx, a -= ATAN16[i];
- }
- return ((a + 8) >> 4) & 0xFFFF;
-}
-
-uint32_t pg_isqrt(uint64_t v) {
- uint64_t r = 0, b = (uint64_t)1 << 62;
- while (b > v) b >>= 2;
- while (b) {
- if (v >= r + b) v -= r + b, r = (r >> 1) + b;
- else r >>= 1;
- b >>= 2;
- }
- return (uint32_t)r;
-}
-
-uint32_t pg_rng(uint32_t *s) {
- uint32_t x = *s;
- x ^= x << 13, x ^= x >> 17, x ^= x << 5;
- return *s = x;
-}
-
-static int32_t wrap16(int32_t a) { return (int16_t)(uint16_t)(a & 0xFFFF); }
-
-/* ------------------------------------------------------------------ circuits */
-typedef struct {
- uint8_t len; /* segments */
- int16_t turn; /* degrees over the piece, + right */
- int8_t climb; /* metres */
-} piece;
-
-#define P(...) {__VA_ARGS__}
-static const piece RIDGE[] = {
- P(24, 0, 0), P(18, 65, 2), P(8, 0, 3), P(9, -38, 2), P(9, 48, 0), P(14, 0, -4), P(22, 105, -3), P(38, 0, 0),
- P(9, -35, 2), P(6, 0, 2), P(11, 95, 0), P(12, 0, -4), P(14, 55, -3), P(10, 0, 0), P(9, -30, 2), P(12, 0, 0),
- P(13, 95, 0), P(10, 0, 0)};
-static const piece WANGAN[] = {
- P(36, 0, 0), P(8, 10, 3), P(16, 0, 4), P(14, 95, 2), P(34, 0, 0), P(12, 85, -2), P(28, 0, -4), P(6, -18, 0),
- P(6, 18, 0), P(3, 0, 0), P(11, 50, 2), P(12, 0, 4), P(9, -25, 2), P(12, 0, 0), P(18, 130, -3), P(7, 0, -3),
- P(6, 15, -2), P(9, 0, 0)};
-static const piece AKAGI[] = {
- P(14, 0, 0), P(10, 80, 4), P(6, 0, 5), P(8, -165, 4), P(10, 0, 7), P(8, 170, 5), P(10, 0, 6), P(9, -55, 3),
- P(9, 65, 2), P(8, 0, 0), P(9, -150, -2), P(10, 0, -6), P(8, 160, -5), P(12, 0, -8), P(12, 110, -6), P(9, -45, -4),
- P(43, 0, -3), P(11, 140, 0), P(7, 0, 0), P(9, -70, 0), P(24, 0, 0), P(13, 120, 0), P(8, 0, 0)};
-#undef P
-
-typedef struct {
- const piece *p;
- uint8_t n;
- uint8_t trees; /* a tree (or lamp) every so many segments */
- uint8_t gantry0, gantry1; /* lit gantries over the road from..to (segments) */
-} circuit;
-#define C(a, ...) {a, sizeof a / sizeof a[0], __VA_ARGS__}
-static const circuit CIRCUITS[PG_CIRCUITS] = {C(RIDGE, 4, 56, 74), C(WANGAN, 3, 24, 120), C(AKAGI, 3, 0, 0)};
-#undef C
-const char *const pg_circuit_name[PG_CIRCUITS] = {"Ridge Valley GT", "Neo-Tokyo Wangan", "Mt. Akagi Touge"};
-const char *const pg_circuit_note[PG_CIRCUITS] = {"Flowing bends", "Long straights", "Hairpins and climbs"};
-const char *const pg_zone_name[5] = {"Alpine Valley", "Sunset Canyon", "Neon Wangan", "Glacier Pass", "Volcanic Ash"};
-
-/* the fastest a car of grip 1 takes a bend: 24 m/s^2 sideways */
-static uint8_t corner_limit(int32_t seg, int32_t turn) {
- if (turn < 0) turn = -turn;
- if (turn < 4) return 255;
- /* R = seg / (turn * 2pi / 65536); v = sqrt(24 R) */
- int64_t r_fx = (int64_t)seg * 65536 * 1000 / ((int64_t)turn * 6283);
- uint32_t v = pg_isqrt((uint64_t)(24 * r_fx / FX));
- return v > 255 ? 255 : (uint8_t)v;
-}
-
-static void finish_nodes(pg_track *t, int n) {
- /* headings from the positions: each segment's, then each node's halfway between its two segments */
- static uint16_t hs[PG_MAXN];
- for (int k = 0; k < n; k++) {
- int j = (k + 1) % n;
- hs[k] = (uint16_t)pg_atan2(t->px[j] - t->px[k], t->pz[j] - t->pz[k]);
- }
- for (int k = 0; k < n; k++) {
- int32_t a = hs[(k + n - 1) % n], b = hs[k];
- t->nh[k] = (uint16_t)(a + wrap16(b - a) / 2);
- }
- for (int k = 0; k < n; k++) t->cv[k] = (int16_t)wrap16(t->nh[(k + 1) % n] - t->nh[k]);
- for (int k = 0; k < n; k++) {
- int32_t c = (t->cv[(k + n - 1) % n] + 2 * t->cv[k] + t->cv[(k + 1) % n]) / 4;
- t->limit[k] = corner_limit(t->seg, c);
- }
-}
-
-void pg_build(pg_track *t, int id) {
- if (id < 0 || id >= PG_CIRCUITS) id = 0;
- const circuit *c = &CIRCUITS[id];
- static int32_t turn[PG_MAXN], climb[PG_MAXN], tmp[PG_MAXN];
- int n = 0;
- for (int i = 0; i < c->n; i++)
- for (int k = 0; k < c->p[i].len && n < PG_MAXN; k++, n++) {
- /* the piece's bend over its segments, in 1/16 angle unit; its climb in FX */
- turn[n] = (int32_t)c->p[i].turn * 65536 * 16 / 360 / c->p[i].len;
- climb[n] = (int32_t)c->p[i].climb * FX / c->p[i].len;
- }
- /* bends and hills ease in and out */
- for (int pass = 0; pass < 4; pass++) {
- for (int k = 0; k < n; k++) tmp[k] = (turn[(k + n - 1) % n] + 2 * turn[k] + turn[(k + 1) % n]) / 4;
- for (int k = 0; k < n; k++) turn[k] = tmp[k];
- for (int k = 0; k < n; k++) tmp[k] = (climb[(k + n - 1) % n] + 2 * climb[k] + climb[(k + 1) % n]) / 4;
- for (int k = 0; k < n; k++) climb[k] = tmp[k];
- }
- /* exactly one turn round, and back to the height it started at */
- int64_t st = 0, sc = 0;
- for (int k = 0; k < n; k++) st += turn[k], sc += climb[k];
- int32_t dt = (int32_t)(65536 * 16 - st), dc = (int32_t)-sc;
- int32_t rt = dt - dt / n * n, rc = dc - dc / n * n; /* (what's left, spread one by one) */
- for (int k = 0; k < n; k++) {
- turn[k] += dt / n + (k < rt ? 1 : 0) - (k < -rt ? 1 : 0);
- climb[k] += dc / n + (k < rc ? 1 : 0) - (k < -rc ? 1 : 0);
- }
- /* laid out, each segment along its middle heading */
- int32_t seg = PG_SEG;
- int64_t x = 0, y = 0, z = 0, h = 0;
- for (int k = 0; k < n; k++) {
- t->px[k] = (int32_t)x, t->py[k] = (int32_t)y, t->pz[k] = (int32_t)z;
- int32_t mid = (int32_t)((h + turn[k] / 2) / 16);
- x += (int64_t)seg * pg_sin(mid) / 32767;
- z += (int64_t)seg * pg_cos(mid) / 32767;
- y += climb[k];
- h += turn[k];
- }
- /* what's left of a closed loop, spread along it */
- for (int k = 0; k < n; k++) {
- t->px[k] -= (int32_t)(x * k / n);
- t->py[k] -= (int32_t)(y * k / n);
- t->pz[k] -= (int32_t)(z * k / n);
- }
- t->endless = 0, t->id = (uint8_t)id, t->n = (uint16_t)n, t->seg = seg, t->length = seg * n;
- t->first = 0, t->last = n - 1;
- finish_nodes(t, n);
- /* the roadside and the orbs */
- int orbs = 0;
- for (int k = 0; k < n; k++) {
- int32_t cv = t->cv[k];
- int side = (k & 1) ? 0 : 8;
- uint8_t p = PROP_NONE;
- if (k == 0 || k == n / 3 || k == 2 * n / 3) p = PROP_ARCH;
- else if (k >= c->gantry0 && k < c->gantry1 && k % 4 == 0) p = PROP_GANTRY;
- else if ((cv > 700 || cv < -700) && k % 2 == 0) p = PROP_CHEVRON, side = cv > 0 ? 8 : 0; /* on the outside */
- else if (k % c->trees == 0) p = PROP_TREE;
- t->prop[k] = (uint8_t)(p | (p ? side : 0));
- t->zone[k] = (uint8_t)id;
- t->orb[k] = -128, t->orb_id[k] = -1;
- if (k >= 8 && k % 15 == 8 && orbs < PG_ORBS) {
- int32_t x8;
- if (cv > 400) x8 = 34; /* towards the inside of the bend */
- else if (cv < -400) x8 = -34;
- else x8 = (int32_t)((uint32_t)(k * 2654435761u) >> 26) - 32; /* -32..31: up to 4 m off the middle */
- t->orb[k] = (int8_t)x8, t->orb_id[k] = (int8_t)orbs++;
- }
- }
- t->orbs = (uint8_t)orbs;
-}
-
-/* ------------------------------------------------------------------ the endless road */
-#define R_MASK (PG_RING - 1)
-
-/* lay one more segment from the end of the road */
-static void lay(pg_track *t) {
- pg_gen *g = &t->gen;
- int32_t k = g->count;
- if (k > 0 && k % PG_ZONE == 0) g->zone = (uint8_t)((g->zone + 1 + pg_rng(&g->rng) % 4) % 5);
- /* bends: straights, sweepers and S-bends, sharper in the mountains */
- if (g->turn_hold <= 0) {
- uint32_t r = pg_rng(&g->rng) & 7;
- if (r < 2) {
- g->turn_to = 0, g->turn_hold = (int16_t)(10 + pg_rng(&g->rng) % 14);
- } else {
- /* (1/16 angle unit a segment: sweepers from a 600 m bend down to 105 m, 70 m in the mountains) */
- int32_t hi = (g->zone == 3 || g->zone == 4) ? 14000 : 9300;
- int32_t mag = 1700 + (int32_t)(pg_rng(&g->rng) % (uint32_t)(hi - 1700));
- g->turn_to = (pg_rng(&g->rng) & 1) ? mag : -mag;
- g->turn_hold = (int16_t)(12 + pg_rng(&g->rng) % 18);
- }
- }
- g->turn_hold--;
- g->turn += (g->turn_to - g->turn) * 46 / 256;
- /* hills: steeper in the mountains, flat on the expressway */
- if (g->slope_hold <= 0) {
- int32_t max = g->zone == 3 ? 150 : g->zone == 4 || g->zone == 1 ? 120 : g->zone == 2 ? 45 : 90; /* FX a seg */
- g->slope_to = (int32_t)(pg_rng(&g->rng) % (uint32_t)(2 * max + 1)) - max;
- g->slope_hold = (int16_t)(10 + pg_rng(&g->rng) % 16);
- }
- g->slope_hold--;
- g->slope += (g->slope_to - g->slope) * 41 / 256;
- /* keep the road within a few hundred metres up or down */
- if (g->y > 60 * FX && g->slope > 0) g->slope -= 8;
- if (g->y < -60 * FX && g->slope < 0) g->slope += 8;
-
- int s = k & R_MASK;
- int32_t head = g->head + g->turn / 32; /* the segment's middle heading */
- t->px[s] = g->x, t->py[s] = g->y, t->pz[s] = g->z;
- t->zone[s] = (uint8_t)(3 + g->zone);
- /* the node's heading: halfway between the segment before and this one */
- int32_t hs = head;
- if (k == 0) g->prev_head = hs;
- t->nh[s] = (uint16_t)(g->prev_head + wrap16(hs - g->prev_head) / 2);
- if (k > 0) {
- int p = (k - 1) & R_MASK;
- t->cv[p] = (int16_t)wrap16(t->nh[s] - t->nh[p]);
- t->limit[p] = corner_limit(t->seg, t->cv[p]);
- }
- g->prev_head = hs;
- g->x += (int32_t)((int64_t)t->seg * pg_sin(hs) / 32767);
- g->z += (int32_t)((int64_t)t->seg * pg_cos(hs) / 32767);
- g->y += g->slope;
- g->head += g->turn / 16;
- /* the roadside: a gate at each new scenery, gantries on the expressway, chevrons on the outside of bends */
- int32_t bend = g->turn / 16;
- uint8_t p = PROP_NONE;
- int side = (k & 1) ? 0 : 8;
- if (k > 0 && k % PG_ZONE == 0) p = PROP_ARCH;
- else if (g->zone == 2 && k % 4 == 0) p = PROP_GANTRY;
- else if ((bend > 450 || bend < -450) && k % 2 == 0) p = PROP_CHEVRON, side = bend > 0 ? 8 : 0;
- else if (k % 3 == 0) p = PROP_TREE;
- t->prop[s] = (uint8_t)(p | (p ? side : 0));
- t->orb[s] = -128, t->orb_id[s] = -1;
- if (k >= 6 && k % 12 == 6) {
- int32_t x8 = bend > 250 ? 32 : bend < -250 ? -32 : (int32_t)(pg_rng(&g->rng) % 65) - 32;
- t->orb[s] = (int8_t)x8;
- }
- g->count = k + 1;
- t->last = k - 1; /* (the last segment whose bend is known) */
- t->first = k + 1 - PG_RING;
- if (t->first < 0) t->first = 0;
-}
-
-void pg_endless(pg_track *t, uint32_t seed) {
- t->endless = 1, t->n = 0, t->seg = PG_SEG, t->length = 0, t->orbs = 0;
- pg_gen *g = &t->gen;
- g->rng = seed ? seed : 0x9E3779B9u;
- for (int i = 0; i < 4; i++) pg_rng(&g->rng);
- g->zone = (uint8_t)(pg_rng(&g->rng) % 5);
- t->id = g->zone;
- g->head = 0, g->turn = 0, g->turn_to = 0, g->turn_hold = 14, g->slope = 0, g->slope_to = 0, g->slope_hold = 10;
- g->count = 0, g->x = g->y = g->z = 0, g->prev_head = 0, g->spare = 0;
- t->first = 0, t->last = -1;
- pg_extend(t, 60);
-}
-
-void pg_extend(pg_track *t, int32_t upto) {
- if (!t->endless) return;
- while (t->last < upto) lay(t);
-}
-
-int pg_slot(const pg_track *t, int32_t k) {
- if (t->endless) return (int)(k & R_MASK);
- int32_t n = t->n, m = k % n;
- return (int)(m < 0 ? m + n : m);
-}
-
-int32_t pg_seg_of(const pg_track *t, int32_t s) {
- int32_t q = s / t->seg;
- if (s < 0 && q * t->seg != s) q--;
- return q;
-}
-
-int32_t pg_heading_at(const pg_track *t, int32_t s) {
- int32_t k = pg_seg_of(t, s), f = s - k * t->seg;
- int i = pg_slot(t, k);
- return (int32_t)t->nh[i] + (int32_t)((int64_t)t->cv[i] * f / t->seg);
-}
-
-int32_t pg_height_at(const pg_track *t, int32_t s) {
- int32_t k = pg_seg_of(t, s), f = s - k * t->seg;
- int i = pg_slot(t, k), j = pg_slot(t, k + 1);
- return t->py[i] + (int32_t)((int64_t)(t->py[j] - t->py[i]) * f / t->seg);
-}
-
-void pg_world(const pg_track *t, int32_t s, int32_t x, int32_t *wx, int32_t *wy, int32_t *wz) {
- int32_t k = pg_seg_of(t, s), f = s - k * t->seg;
- int i = pg_slot(t, k), j = pg_slot(t, k + 1);
- int32_t h = (int32_t)t->nh[i] + (int32_t)((int64_t)t->cv[i] * f / t->seg);
- *wx = t->px[i] + (int32_t)((int64_t)(t->px[j] - t->px[i]) * f / t->seg) + (int32_t)((int64_t)x * pg_cos(h) / 32767);
- *wz = t->pz[i] + (int32_t)((int64_t)(t->pz[j] - t->pz[i]) * f / t->seg) - (int32_t)((int64_t)x * pg_sin(h) / 32767);
- *wy = t->py[i] + (int32_t)((int64_t)(t->py[j] - t->py[i]) * f / t->seg);
-}
-
-int32_t pg_wrap(const pg_track *t, int32_t ds) {
- if (t->endless) return ds;
- int32_t L = t->length;
- ds %= L;
- if (ds >= L / 2) ds -= L;
- if (ds < -L / 2) ds += L;
- return ds;
-}
-
-int pg_zone_at(const pg_track *t, int32_t s) { return t->zone[pg_slot(t, pg_seg_of(t, s))]; }
-
-/* the outline of a circuit, fit in w x h (y down, north up) */
-int pg_map(const pg_track *t, int n, int16_t *xs, int16_t *ys, int w, int h) {
- int32_t x0 = t->px[0], x1 = x0, z0 = t->pz[0], z1 = z0;
- int nn = t->endless ? PG_RING : t->n;
- int32_t base = t->endless ? t->first : 0;
- for (int k = 0; k < nn; k++) {
- int i = pg_slot(t, base + k);
- if (t->px[i] < x0) x0 = t->px[i];
- if (t->px[i] > x1) x1 = t->px[i];
- if (t->pz[i] < z0) z0 = t->pz[i];
- if (t->pz[i] > z1) z1 = t->pz[i];
- }
- int32_t sx = x1 - x0 + 1, sz = z1 - z0 + 1;
- int32_t scale_n = w * sz > h * sx ? h : w, scale_d = w * sz > h * sx ? sz : sx; /* the tighter fit */
- int32_t ox = (w - (int32_t)((int64_t)sx * scale_n / scale_d)) / 2, oy = (h - (int32_t)((int64_t)sz * scale_n / scale_d)) / 2;
- for (int i = 0; i < n; i++) {
- int k = pg_slot(t, base + (int32_t)((int64_t)i * nn / n));
- xs[i] = (int16_t)(ox + (int64_t)(t->px[k] - x0) * scale_n / scale_d);
- ys[i] = (int16_t)(h - 1 - oy - (int64_t)(t->pz[k] - z0) * scale_n / scale_d);
- }
- return n;
-}
diff --git a/games/polygt3d/.gitignore b/games/polytrack/.gitignore
similarity index 100%
rename from games/polygt3d/.gitignore
rename to games/polytrack/.gitignore
diff --git a/games/polygt3d/Makefile b/games/polytrack/Makefile
similarity index 100%
rename from games/polygt3d/Makefile
rename to games/polytrack/Makefile
diff --git a/games/polygt3d/README.md b/games/polytrack/README.md
similarity index 100%
rename from games/polygt3d/README.md
rename to games/polytrack/README.md
diff --git a/games/polygt3d/docs/akagi.png b/games/polytrack/docs/akagi.png
similarity index 100%
rename from games/polygt3d/docs/akagi.png
rename to games/polytrack/docs/akagi.png
diff --git a/games/polygt3d/docs/endless.png b/games/polytrack/docs/endless.png
similarity index 100%
rename from games/polygt3d/docs/endless.png
rename to games/polytrack/docs/endless.png
diff --git a/games/polygt3d/docs/horse.png b/games/polytrack/docs/horse.png
similarity index 100%
rename from games/polygt3d/docs/horse.png
rename to games/polytrack/docs/horse.png
diff --git a/games/polygt3d/docs/setup.png b/games/polytrack/docs/setup.png
similarity index 100%
rename from games/polygt3d/docs/setup.png
rename to games/polytrack/docs/setup.png
diff --git a/games/polygt3d/docs/shot.png b/games/polytrack/docs/shot.png
similarity index 100%
rename from games/polygt3d/docs/shot.png
rename to games/polytrack/docs/shot.png
diff --git a/games/polygt3d/docs/wangan.png b/games/polytrack/docs/wangan.png
similarity index 100%
rename from games/polygt3d/docs/wangan.png
rename to games/polytrack/docs/wangan.png
diff --git a/games/polytrack/src/car.c b/games/polytrack/src/car.c
new file mode 100644
index 00000000..e6f14820
--- /dev/null
+++ b/games/polytrack/src/car.c
@@ -0,0 +1,747 @@
+/* The car (car.h): what polytrack_physics.wasm's updateCarModel does each 1 ms frame, then Bullet's step: gravity,
+ * damping, the chassis' contacts (a sequential impulse solver with split impulses), the integration, and the
+ * btRaycastVehicle. The numbers are PolyTrack's (see the spec); Bullet's vehicle code is followed line by line. */
+#include <string.h>
+#include "car.h"
+
+#define DT 0.001f
+#define GRAVITY -9.82f
+#define MASS 400.0f
+#define INV_MASS (1.0f / 400.0f)
+#define DAMP_LIN 0.999894619f /* (1 - 0.1)^0.001 */
+#define DAMP_ANG_LOW 0.999894619f /* angular 0.1 */
+#define DAMP_ANG_HIGH 0.999084115f /* angular 0.6 (in the air, Down held) */
+#define REST 0.12f
+#define RADIUS 0.331f
+#define TRAVEL 10.0f /* maxSuspensionTravelCm 1000 */
+#define DAMP_COMP 200.0f
+#define DAMP_RELAX 1.35f
+#define FRICTION_SLIP 3.0f
+#define ROLL_INFLUENCE 0.5f
+#define MAX_SUSP_FORCE 1e6f
+#define ENGINE 4000.0f
+#define MARGIN 0.03f /* the hull's 0.02 and the meshes' 0.01 */
+#define BREAKING 0.02f /* Bullet's contact breaking threshold */
+#define MAX_CONTACTS 12
+
+const v3 car_wheel_pos[4] = {{0.627909f, 0.27f, 1.3478f}, {-0.627909f, 0.27f, 1.3478f},
+ {0.720832f, 0.27f, -1.52686f}, {-0.720832f, 0.27f, -1.52686f}};
+static const float RESET_LEN[4] = {0.0780950100f, 0.0780950100f, 0.0781289680f, 0.0781289680f};
+
+static v3 inv_inertia_local;
+static v3 hull[PT_HULL_N]; /* body space: the model's hull moved up by the mass offset */
+static v3 hull_lo, hull_hi;
+static float hull_plane[16][4]; /* the hull's face planes, outwards */
+static int nhull_plane;
+static wtri cache[PT_CACHE];
+
+/* the checkpoints of the track */
+#define MAX_DET 64
+static detector_t det[MAX_DET];
+static int ndet, ncp_det;
+static uint16_t orders[MAX_DET];
+static int norders;
+
+int car_checkpoints(void) { return norders; }
+
+static void hull_setup(void) {
+ for (int i = 0; i < PT_HULL_N; i++) hull[i] = V3(pt_hull[i][0], pt_hull[i][1] + CAR_MASS_OFFSET, pt_hull[i][2]);
+ hull_lo = hull_hi = hull[0];
+ for (int i = 1; i < PT_HULL_N; i++) {
+ hull_lo = V3(pt_minf(hull_lo.x, hull[i].x), pt_minf(hull_lo.y, hull[i].y), pt_minf(hull_lo.z, hull[i].z));
+ hull_hi = V3(pt_maxf(hull_hi.x, hull[i].x), pt_maxf(hull_hi.y, hull[i].y), pt_maxf(hull_hi.z, hull[i].z));
+ }
+ /* inertia: Bullet's box from the cached AABB (support points +- 0.04), its margin 0.02 added twice */
+ float lx = 2.0f * ((hull_hi.x - hull_lo.x) * 0.5f + 0.04f + 0.02f + 0.02f);
+ float ly = 2.0f * ((hull_hi.y - hull_lo.y) * 0.5f + 0.04f + 0.02f + 0.02f);
+ float lz = 2.0f * ((hull_hi.z - hull_lo.z) * 0.5f + 0.04f + 0.02f + 0.02f);
+ float m = MASS * 0.08333333f;
+ inv_inertia_local = V3(1.0f / (m * (ly * ly + lz * lz)), 1.0f / (m * (lx * lx + lz * lz)), 1.0f / (m * (lx * lx + ly * ly)));
+ /* face planes: every plane through three hull points with all the others behind it */
+ nhull_plane = 0;
+ for (int a = 0; a < PT_HULL_N; a++)
+ for (int b = a + 1; b < PT_HULL_N; b++)
+ for (int c = b + 1; c < PT_HULL_N; c++) {
+ v3 n = vcross(vsub(hull[b], hull[a]), vsub(hull[c], hull[a]));
+ float l = vlen(n);
+ if (l < 1e-6f) continue;
+ n = vmul(n, 1.0f / l);
+ float d = vdot(n, hull[a]);
+ int pos = 0, neg = 0;
+ for (int k = 0; k < PT_HULL_N; k++) {
+ float s = vdot(n, hull[k]) - d;
+ if (s > 1e-5f) pos++;
+ if (s < -1e-5f) neg++;
+ }
+ if (pos && neg) continue;
+ if (pos) n = vneg(n), d = -d;
+ bool dup = false;
+ for (int k = 0; k < nhull_plane; k++)
+ if (pt_absf(hull_plane[k][0] - n.x) + pt_absf(hull_plane[k][1] - n.y) + pt_absf(hull_plane[k][2] - n.z) < 1e-4f) dup = true;
+ if (dup || nhull_plane >= 16) continue;
+ hull_plane[nhull_plane][0] = n.x, hull_plane[nhull_plane][1] = n.y, hull_plane[nhull_plane][2] = n.z;
+ hull_plane[nhull_plane][3] = d;
+ nhull_plane++;
+ }
+}
+
+static void update_inertia(car_t *c) {
+ qmat(c->rot, &c->basis);
+ const m3 *b = &c->basis;
+ m3 *I = &c->inv_inertia;
+ for (int i = 0; i < 3; i++) {
+ v3 r = b->r[i];
+ v3 s = V3(r.x * inv_inertia_local.x, r.y * inv_inertia_local.y, r.z * inv_inertia_local.z);
+ I->r[i] = V3(vdot(s, b->r[0]), vdot(s, b->r[1]), vdot(s, b->r[2]));
+ }
+}
+
+static void wheels_reset(car_t *c) {
+ for (int i = 0; i < 4; i++) {
+ wheel_t *w = &c->w[i];
+ w->clipped = 1.0f, w->relvel = 0.0f;
+ w->susp = REST;
+ w->normal = vneg(w->dir);
+ }
+ for (int i = 0; i < 4; i++) c->w[i].susp = RESET_LEN[i];
+ c->w[0].steering = c->w[1].steering = 0.0f;
+}
+
+static void update_wheel_ws(car_t *c, wheel_t *w, int i) {
+ w->contact = 0;
+ w->hard = vadd(c->pos, mrow(&c->basis, car_wheel_pos[i]));
+ w->dir = mrow(&c->basis, V3(0.0f, -1.0f, 0.0f));
+ w->axle = mrow(&c->basis, V3(-1.0f, 0.0f, 0.0f));
+}
+
+void car_reset(car_t *c) {
+ static int setup;
+ if (!setup) hull_setup(), setup = 1;
+ memset(c, 0, sizeof *c);
+ world_start(&c->pos, &c->rot);
+ update_inertia(c);
+ for (int i = 0; i < 4; i++) {
+ wheel_t *w = &c->w[i];
+ update_wheel_ws(c, w, i);
+ w->stiffness = 50.0f;
+ }
+ wheels_reset(c);
+ c->ang_damp = DAMP_ANG_LOW;
+ c->ground_on = c->pos.y < 4.0f;
+ /* the track's detectors, and its checkpoint orders */
+ const pt_track *t = W.t;
+ ndet = t->ncp + t->nfin;
+ if (ndet > MAX_DET) ndet = MAX_DET;
+ ncp_det = t->ncp;
+ norders = 0;
+ for (int k = 0; k < ndet; k++) {
+ world_detector(k, &det[k]);
+ if (k < ncp_det && (!norders || orders[norders - 1] != det[k].order)) orders[norders++] = det[k].order;
+ }
+}
+
+bool car_finished(const car_t *c) { return c->finish != 0; }
+
+/* ------------------------------------------------------------------ the triangles round the car */
+static void cache_refresh(car_t *c) {
+ /* as the game re-picks the parts round the car when it moved more than 1.5 m: here the triangles within 5 m */
+ if (c->cache_ok && pt_absf(c->pos.x - c->cache_at.x) + pt_absf(c->pos.y - c->cache_at.y) + pt_absf(c->pos.z - c->cache_at.z) <= 1.5f)
+ return;
+ c->cache_at = c->pos;
+ v3 h = V3(5.0f, 5.0f, 5.0f);
+ c->ncache = world_gather(vsub(c->pos, h), vadd(c->pos, h), cache, PT_CACHE);
+ c->cache_ok = 1;
+}
+
+/* Bullet's btTriangleRaycastCallback, closest hit: fraction, normal facing the ray's start */
+static bool ray_tri(const wtri *t, v3 from, v3 to, float *frac, v3 *normal) {
+ v3 v10 = vsub(t->b, t->a), v20 = vsub(t->c, t->a);
+ v3 tn = vcross(v10, v20);
+ float dist = vdot(t->a, tn);
+ float da = vdot(tn, from) - dist, db = vdot(tn, to) - dist;
+ if (da * db >= 0.0f) return false;
+ float dd = da / (da - db);
+ if (dd >= *frac) return false;
+ float tol = vlen2(tn) * -0.0001f;
+ v3 p = vadd(vmul(from, 1.0f - dd), vmul(to, dd));
+ v3 v0p = vsub(t->a, p), v1p = vsub(t->b, p), v2p = vsub(t->c, p);
+ if (vdot(vcross(v0p, v1p), tn) < tol) return false;
+ if (vdot(vcross(v1p, v2p), tn) < tol) return false;
+ if (vdot(vcross(v2p, v0p), tn) < tol) return false;
+ v3 n = vnorm(tn);
+ *normal = da <= 0.0f ? vneg(n) : n;
+ *frac = dd;
+ return true;
+}
+
+static bool cast_ray(car_t *c, v3 from, v3 to, float *frac, v3 *normal) {
+ bool hit = false;
+ float lx = pt_minf(from.x, to.x), hx = pt_maxf(from.x, to.x);
+ float ly = pt_minf(from.y, to.y), hy = pt_maxf(from.y, to.y);
+ float lz = pt_minf(from.z, to.z), hz = pt_maxf(from.z, to.z);
+ *frac = 1.0f;
+ for (int i = 0; i < c->ncache; i++) {
+ const wtri *t = &cache[i];
+ if (pt_maxf(t->a.x, pt_maxf(t->b.x, t->c.x)) < lx || pt_minf(t->a.x, pt_minf(t->b.x, t->c.x)) > hx) continue;
+ if (pt_maxf(t->a.y, pt_maxf(t->b.y, t->c.y)) < ly || pt_minf(t->a.y, pt_minf(t->b.y, t->c.y)) > hy) continue;
+ if (pt_maxf(t->a.z, pt_maxf(t->b.z, t->c.z)) < lz || pt_minf(t->a.z, pt_minf(t->b.z, t->c.z)) > hz) continue;
+ if (ray_tri(t, from, to, frac, normal)) hit = true;
+ }
+ /* the ground plane, y = 0 (in the world only while the car is low) */
+ if (c->ground_on && (from.y > 0.0f) != (to.y > 0.0f)) {
+ float dd = from.y / (from.y - to.y);
+ if (dd < *frac) {
+ *frac = dd;
+ *normal = from.y > 0.0f ? V3(0.0f, 1.0f, 0.0f) : V3(0.0f, -1.0f, 0.0f);
+ hit = true;
+ }
+ }
+ return hit;
+}
+
+/* ------------------------------------------------------------------ the chassis' contacts */
+typedef struct {
+ v3 n; /* from the world to the car */
+ v3 r; /* contact point - centre of mass */
+ float dist; /* < 0: inside */
+ float jinv, tjinv;
+ v3 t; /* friction direction */
+ float rhs, rhs_pen, trhs;
+ float imp, timp, pimp;
+} contact_t;
+
+static int find_contacts(car_t *c, contact_t *out) {
+ int n = 0;
+ v3 wh[PT_HULL_N];
+ v3 lo = V3(1e30f, 1e30f, 1e30f), hi = V3(-1e30f, -1e30f, -1e30f);
+ for (int i = 0; i < PT_HULL_N; i++) {
+ wh[i] = vadd(c->pos, mrow(&c->basis, hull[i]));
+ lo = V3(pt_minf(lo.x, wh[i].x), pt_minf(lo.y, wh[i].y), pt_minf(lo.z, wh[i].z));
+ hi = V3(pt_maxf(hi.x, wh[i].x), pt_maxf(hi.y, wh[i].y), pt_maxf(hi.z, wh[i].z));
+ }
+ float g = MARGIN + BREAKING;
+ lo = vsub(lo, V3(g, g, g)), hi = vadd(hi, V3(g, g, g));
+ contact_t cand[48];
+ int nc = 0;
+ for (int i = 0; i < c->ncache && nc < 48; i++) {
+ const wtri *t = &cache[i];
+ if (pt_maxf(t->a.x, pt_maxf(t->b.x, t->c.x)) < lo.x || pt_minf(t->a.x, pt_minf(t->b.x, t->c.x)) > hi.x) continue;
+ if (pt_maxf(t->a.y, pt_maxf(t->b.y, t->c.y)) < lo.y || pt_minf(t->a.y, pt_minf(t->b.y, t->c.y)) > hi.y) continue;
+ if (pt_maxf(t->a.z, pt_maxf(t->b.z, t->c.z)) < lo.z || pt_minf(t->a.z, pt_minf(t->b.z, t->c.z)) > hi.z) continue;
+ /* the hull's corners against the triangle's face (either side, the side the car's centre is on) */
+ float side = vdot(t->n, c->pos) - t->d;
+ v3 nn = side >= 0.0f ? t->n : vneg(t->n);
+ float dd = side >= 0.0f ? t->d : -t->d;
+ v3 e0 = vsub(t->b, t->a), e1 = vsub(t->c, t->b), e2 = vsub(t->a, t->c);
+ for (int k = 0; k < PT_HULL_N && nc < 48; k++) {
+ float s = vdot(nn, wh[k]) - dd;
+ if (s >= g || s < -0.5f) continue;
+ if (vdot(vcross(e0, vsub(wh[k], t->a)), t->n) < 0.0f) continue;
+ if (vdot(vcross(e1, vsub(wh[k], t->b)), t->n) < 0.0f) continue;
+ if (vdot(vcross(e2, vsub(wh[k], t->c)), t->n) < 0.0f) continue;
+ contact_t *ct = &cand[nc++];
+ ct->n = nn;
+ ct->r = vsub(wh[k], c->pos);
+ ct->dist = s - MARGIN;
+ }
+ /* the triangle's corners inside the hull */
+ const v3 *tv[3] = {&t->a, &t->b, &t->c};
+ for (int k = 0; k < 3 && nc < 48; k++) {
+ v3 p = mcol(&c->basis, vsub(*tv[k], c->pos));
+ float best = -1e30f;
+ int bi = -1;
+ for (int j = 0; j < nhull_plane; j++) {
+ float s = hull_plane[j][0] * p.x + hull_plane[j][1] * p.y + hull_plane[j][2] * p.z - hull_plane[j][3];
+ if (s > g) {
+ bi = -2;
+ break;
+ }
+ if (s > best) best = s, bi = j;
+ }
+ if (bi < 0) continue;
+ contact_t *ct = &cand[nc++];
+ ct->n = vneg(mrow(&c->basis, V3(hull_plane[bi][0], hull_plane[bi][1], hull_plane[bi][2])));
+ ct->r = vsub(*tv[k], c->pos);
+ ct->dist = best - MARGIN;
+ }
+ }
+ /* the ground plane */
+ if (c->ground_on)
+ for (int k = 0; k < PT_HULL_N && nc < 48; k++)
+ if (wh[k].y < g && wh[k].y > -0.5f) {
+ contact_t *ct = &cand[nc++];
+ ct->n = V3(0.0f, 1.0f, 0.0f);
+ ct->r = vsub(wh[k], c->pos);
+ ct->dist = wh[k].y - MARGIN;
+ }
+ /* the deepest ones */
+ for (int i = 0; i < nc && n < MAX_CONTACTS; i++) {
+ int b = i;
+ for (int j = i + 1; j < nc; j++)
+ if (cand[j].dist < cand[b].dist) b = j;
+ contact_t tmp = cand[i];
+ cand[i] = cand[b], cand[b] = tmp;
+ out[n++] = cand[i];
+ }
+ return n;
+}
+
+static float jac_inv(const car_t *c, v3 r, v3 n) {
+ v3 rn = vcross(r, n);
+ return 1.0f / (INV_MASS + vdot(rn, mrow(&c->inv_inertia, rn)));
+}
+
+/* the solver: contacts with friction 1.0, ten iterations, split impulses (Bullet's defaults) */
+static void solve(car_t *c, contact_t *ct, int n, v3 *push, v3 *turn) {
+ v3 v = vadd(c->vel, V3(0.0f, GRAVITY * DT, 0.0f)); /* with the gravity impulse */
+ v3 w = c->ang;
+ v3 dv = V3(0.0f, 0.0f, 0.0f), dw = dv, pv = dv, pw = dv;
+ for (int i = 0; i < n; i++) {
+ contact_t *k = &ct[i];
+ k->jinv = jac_inv(c, k->r, k->n);
+ v3 vel = vadd(v, vcross(w, k->r));
+ float rel = vdot(k->n, vel);
+ float pen = k->dist, perr = 0.0f, verr = -rel;
+ float erp = pen < -0.04f ? 0.8f : 0.2f;
+ if (pen > 0.0f) verr -= pen / DT;
+ else perr = -pen * erp / DT;
+ if (pen > -0.04f) k->rhs = (perr + verr) * k->jinv, k->rhs_pen = 0.0f;
+ else k->rhs = verr * k->jinv, k->rhs_pen = perr * k->jinv;
+ /* one friction direction: along the sliding */
+ v3 lat = vsub(vel, vmul(k->n, rel));
+ float l2 = vlen2(lat);
+ if (l2 > 1.1920929e-07f) {
+ k->t = vmul(lat, 1.0f / pt_sqrtf(l2));
+ } else {
+ v3 a = pt_absf(k->n.x) > 0.7071f ? V3(-k->n.y, k->n.x, 0.0f) : V3(0.0f, -k->n.z, k->n.y);
+ k->t = vnorm(a);
+ }
+ k->tjinv = jac_inv(c, k->r, k->t);
+ k->trhs = -vdot(k->t, vel) * k->tjinv;
+ k->imp = k->timp = k->pimp = 0.0f;
+ }
+ for (int it = 0; it < 10; it++) {
+ for (int i = 0; i < n; i++) {
+ contact_t *k = &ct[i];
+ float dvn = vdot(k->n, dv) + vdot(vcross(k->r, k->n), dw);
+ float d = k->rhs - dvn * k->jinv;
+ float sum = k->imp + d;
+ if (sum < 0.0f) d = -k->imp, sum = 0.0f;
+ k->imp = sum;
+ dv = vadd(dv, vmul(k->n, d * INV_MASS));
+ dw = vadd(dw, mrow(&c->inv_inertia, vmul(vcross(k->r, k->n), d)));
+ }
+ for (int i = 0; i < n; i++) {
+ contact_t *k = &ct[i];
+ float lim = 1.0f * k->imp;
+ float dvt = vdot(k->t, dv) + vdot(vcross(k->r, k->t), dw);
+ float d = k->trhs - dvt * k->tjinv;
+ float sum = k->timp + d;
+ if (sum < -lim) sum = -lim;
+ if (sum > lim) sum = lim;
+ d = sum - k->timp;
+ k->timp = sum;
+ dv = vadd(dv, vmul(k->t, d * INV_MASS));
+ dw = vadd(dw, mrow(&c->inv_inertia, vmul(vcross(k->r, k->t), d)));
+ }
+ }
+ for (int it = 0; it < 10; it++)
+ for (int i = 0; i < n; i++) {
+ contact_t *k = &ct[i];
+ if (k->rhs_pen == 0.0f) continue;
+ float dvn = vdot(k->n, pv) + vdot(vcross(k->r, k->n), pw);
+ float d = k->rhs_pen - dvn * k->jinv;
+ float sum = k->pimp + d;
+ if (sum < 0.0f) d = -k->pimp, sum = 0.0f;
+ k->pimp = sum;
+ pv = vadd(pv, vmul(k->n, d * INV_MASS));
+ pw = vadd(pw, mrow(&c->inv_inertia, vmul(vcross(k->r, k->n), d)));
+ }
+ c->vel = vadd(v, dv);
+ c->ang = vadd(w, dw);
+ *push = pv, *turn = pw;
+ for (int i = 0; i < n; i++)
+ if (ct[i].imp > c->impulse) c->impulse = ct[i].imp;
+}
+
+/* btTransformUtil::integrateTransform */
+static void integrate(car_t *c, v3 lin, v3 ang) {
+ c->pos = vadd(c->pos, vmul(lin, DT));
+ float a2 = vlen2(ang), a = 0.0f;
+ if (a2 > 1.1920929e-07f) a = pt_sqrtf(a2);
+ if (a * DT > 0.785398163f) a = 0.785398163f / DT;
+ v3 axis;
+ if (a < 0.001f) axis = vmul(ang, 0.5f * DT - (DT * DT * DT) * 0.020833333333f * a * a);
+ else axis = vmul(ang, pt_sinf(0.5f * a * DT) / a);
+ qt d = {axis.x, axis.y, axis.z, pt_cosf(a * DT * 0.5f)};
+ qt q = qmul(d, c->rot);
+ float l = pt_sqrtf(q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w);
+ if (l > 1.1920929e-07f) q.x /= l, q.y /= l, q.z /= l, q.w /= l;
+ c->rot = q;
+ update_inertia(c);
+}
+
+/* ------------------------------------------------------------------ btRaycastVehicle */
+static void apply_impulse(car_t *c, v3 imp, v3 rel) {
+ c->vel = vadd(c->vel, vmul(imp, INV_MASS));
+ c->ang = vadd(c->ang, mrow(&c->inv_inertia, vcross(rel, imp)));
+}
+static v3 vel_at(const car_t *c, v3 rel) { return vadd(c->vel, vcross(c->ang, rel)); }
+
+static void ray_cast(car_t *c, wheel_t *w, int i) {
+ update_wheel_ws(c, w, i);
+ float raylen = REST + RADIUS;
+ v3 to = vadd(w->hard, vmul(w->dir, raylen));
+ w->point = to;
+ float frac;
+ v3 normal;
+ w->ground = 0;
+ if (cast_ray(c, w->hard, to, &frac, &normal)) {
+ w->normal = normal;
+ w->contact = 1;
+ w->ground = 1;
+ float hit = frac * raylen;
+ w->susp = hit - RADIUS;
+ float lo = REST - TRAVEL, hi = REST + TRAVEL;
+ if (w->susp < lo) w->susp = lo;
+ if (w->susp > hi) w->susp = hi;
+ w->point = vadd(vmul(w->hard, 1.0f - frac), vmul(to, frac));
+ float den = vdot(w->normal, w->dir);
+ v3 vel = vel_at(c, vsub(w->point, c->pos));
+ float proj = vdot(w->normal, vel);
+ if (den >= -0.1f) {
+ w->relvel = 0.0f;
+ w->clipped = 1.0f / 0.1f;
+ } else {
+ float inv = -1.0f / den;
+ w->relvel = proj * inv;
+ w->clipped = inv;
+ }
+ } else {
+ w->susp = REST;
+ w->relvel = 0.0f;
+ w->normal = vneg(w->dir);
+ w->clipped = 1.0f;
+ }
+}
+
+/* the wheel's axle turned by its steering (updateWheelTransform: steeringMat * right) */
+static v3 wheel_axle(const wheel_t *w) {
+ v3 up = vneg(w->dir), right = w->axle;
+ float s = pt_sinf(w->steering), co = pt_cosf(w->steering);
+ /* Rodrigues: right cos + (up x right) sin + up (up . right)(1 - cos) */
+ return vadd(vadd(vmul(right, co), vmul(vcross(up, right), s)), vmul(up, vdot(up, right) * (1.0f - co)));
+}
+
+static void vehicle_update(car_t *c) {
+ v3 axle_ws[4];
+ for (int i = 0; i < 4; i++) {
+ update_wheel_ws(c, &c->w[i], i);
+ axle_ws[i] = wheel_axle(&c->w[i]);
+ }
+ v3 fwd = mcolumn(&c->basis, 2);
+ c->speed = 3.6f * vlen(c->vel);
+ if (vdot(fwd, c->vel) < 0.0f) c->speed = -c->speed;
+ for (int i = 0; i < 4; i++) ray_cast(c, &c->w[i], i);
+ /* the springs */
+ for (int i = 0; i < 4; i++) {
+ wheel_t *w = &c->w[i];
+ if (w->contact) {
+ float force = w->stiffness * (REST - w->susp) * w->clipped;
+ float rv = w->relvel;
+ force -= (rv < 0.0f ? DAMP_COMP : DAMP_RELAX) * rv;
+ w->force = force * MASS;
+ if (w->force < 0.0f) w->force = 0.0f;
+ } else {
+ w->force = 0.0f;
+ }
+ }
+ for (int i = 0; i < 4; i++) {
+ wheel_t *w = &c->w[i];
+ float f = w->force > MAX_SUSP_FORCE ? MAX_SUSP_FORCE : w->force;
+ apply_impulse(c, vmul(w->normal, f * DT), vsub(w->point, c->pos));
+ }
+ /* updateFriction */
+ int on = 0;
+ float side[4] = {0}, fwdimp[4] = {0};
+ v3 axle[4], fw[4];
+ for (int i = 0; i < 4; i++) on += c->w[i].ground;
+ for (int i = 0; i < 4; i++) {
+ wheel_t *w = &c->w[i];
+ if (!w->ground) continue;
+ v3 a = axle_ws[i];
+ a = vsub(a, vmul(w->normal, vdot(a, w->normal)));
+ a = vnorm(a);
+ axle[i] = a;
+ fw[i] = vnorm(vcross(w->normal, a));
+ /* resolveSingleBilateral against the fixed world */
+ v3 rel = vsub(w->point, c->pos);
+ float nl = vlen2(a);
+ if (nl > 1.1f) {
+ side[i] = 0.0f;
+ } else {
+ float relv = vdot(a, vel_at(c, rel));
+ side[i] = -0.2f * relv * jac_inv(c, rel, a);
+ }
+ }
+ bool sliding = false;
+ for (int i = 0; i < 4; i++) {
+ wheel_t *w = &c->w[i];
+ float roll = 0.0f;
+ if (w->ground) {
+ if (w->engine != 0.0f) {
+ roll = w->engine * DT;
+ } else {
+ float maximp = w->brake != 0.0f ? w->brake : 0.0f;
+ v3 rel = vsub(w->point, c->pos);
+ float jinv = jac_inv(c, rel, fw[i]);
+ float vrel = vdot(fw[i], vel_at(c, rel));
+ float j = -vrel * jinv / (float)on;
+ if (j > maximp) j = maximp;
+ if (j < -maximp) j = -maximp;
+ roll = j;
+ }
+ }
+ fwdimp[i] = 0.0f;
+ w->skid = 1.0f;
+ if (w->ground) {
+ float maximp = w->force * DT * FRICTION_SLIP;
+ float maxsq = maximp * maximp;
+ fwdimp[i] = roll;
+ float x = roll * 0.5f, y = side[i];
+ float sq = x * x + y * y;
+ if (sq > maxsq) {
+ sliding = true;
+ w->skid *= maximp / pt_sqrtf(sq);
+ }
+ }
+ }
+ if (sliding)
+ for (int i = 0; i < 4; i++)
+ if (side[i] != 0.0f && c->w[i].skid < 1.0f) fwdimp[i] *= c->w[i].skid, side[i] *= c->w[i].skid;
+ v3 up = mcolumn(&c->basis, 1);
+ for (int i = 0; i < 4; i++) {
+ wheel_t *w = &c->w[i];
+ v3 rel = vsub(w->point, c->pos);
+ if (fwdimp[i] != 0.0f) apply_impulse(c, vmul(fw[i], fwdimp[i]), rel);
+ if (side[i] != 0.0f) {
+ v3 imp = vmul(axle[i], side[i]);
+ /* (the roll influence along the car's up, ROLLING_INFLUENCE_FIX) */
+ v3 r2 = vsub(rel, vmul(up, vdot(up, rel) * (1.0f - ROLL_INFLUENCE)));
+ apply_impulse(c, imp, r2);
+ }
+ }
+ /* the wheels' turning (for the picture) */
+ for (int i = 0; i < 4; i++) {
+ wheel_t *w = &c->w[i];
+ if (w->contact) {
+ v3 f = mcolumn(&c->basis, 2);
+ f = vsub(f, vmul(w->normal, vdot(f, w->normal)));
+ v3 vel = vel_at(c, vsub(w->hard, c->pos));
+ w->drot = vdot(f, vel) * DT / RADIUS;
+ }
+ w->rotation += w->drot;
+ if (w->rotation > 6.2831853f) w->rotation -= 6.2831853f;
+ if (w->rotation < -6.2831853f) w->rotation += 6.2831853f;
+ w->drot *= 0.99f;
+ }
+}
+
+/* ------------------------------------------------------------------ checkpoints: the car's detector box against a
+ detector's (OBB against AABB, 15 axes) */
+static bool box_hit(const car_t *c, const detector_t *d) {
+ v3 cen = vadd(c->pos, mrow(&c->basis, V3(0.0f, 0.48f, -0.15f)));
+ float he[3] = {0.445f, 0.11f, 0.9f};
+ v3 ax[3] = {mcolumn(&c->basis, 0), mcolumn(&c->basis, 1), mcolumn(&c->basis, 2)};
+ v3 bc = vmul(vadd(d->lo, d->hi), 0.5f), bh = vmul(vsub(d->hi, d->lo), 0.5f);
+ float bhe[3] = {bh.x, bh.y, bh.z};
+ v3 t = vsub(cen, bc);
+ float R[3][3], AR[3][3];
+ for (int i = 0; i < 3; i++) {
+ float a[3] = {ax[i].x, ax[i].y, ax[i].z};
+ for (int j = 0; j < 3; j++) R[i][j] = a[j], AR[i][j] = pt_absf(a[j]) + 1.19e-07f;
+ }
+ float tt[3] = {t.x, t.y, t.z};
+ /* the box's axes (world) */
+ for (int j = 0; j < 3; j++) {
+ float ra = he[0] * AR[0][j] + he[1] * AR[1][j] + he[2] * AR[2][j];
+ if (pt_absf(tt[j]) > ra + bhe[j]) return false;
+ }
+ /* the car's axes */
+ for (int i = 0; i < 3; i++) {
+ float rb = bhe[0] * AR[i][0] + bhe[1] * AR[i][1] + bhe[2] * AR[i][2];
+ float p = tt[0] * R[i][0] + tt[1] * R[i][1] + tt[2] * R[i][2];
+ if (pt_absf(p) > he[i] + rb) return false;
+ }
+ /* cross products: car axis i x world axis j */
+ for (int i = 0; i < 3; i++)
+ for (int j = 0; j < 3; j++) {
+ int i1 = (i + 1) % 3, i2 = (i + 2) % 3, j1 = (j + 1) % 3, j2 = (j + 2) % 3;
+ float ra = he[i1] * AR[i2][j] + he[i2] * AR[i1][j];
+ float rb = bhe[j1] * AR[i][j2] + bhe[j2] * AR[i][j1];
+ float p = tt[j2] * R[i][j1] - tt[j1] * R[i][j2];
+ if (pt_absf(p) > ra + rb) return false;
+ }
+ return true;
+}
+
+static void checkpoints(car_t *c) {
+ if (c->next_cp >= norders) {
+ for (int k = ncp_det; k < ndet; k++)
+ if (box_hit(c, &det[k])) {
+ c->finish = c->frames;
+ return;
+ }
+ return;
+ }
+ uint16_t want = orders[c->next_cp];
+ for (int k = 0; k < ncp_det; k++) {
+ const detector_t *d = &det[k];
+ if (d->order != want || !box_hit(c, d)) continue;
+ if (c->next_cp < 64) c->cp_frames[c->next_cp] = c->frames;
+ c->next_cp++;
+ if (d->axis != 0) return; /* (no respawn on a checkpoint on a wall) */
+ float a = (float)d->rot * PT_HALF_PI;
+ float vz = c->vel.z, vx = c->vel.x;
+ bool back = (d->rot == 0 && vz < 0.0f) || (d->rot == 1 && vx < 0.0f) || (d->rot == 2 && vz > 0.0f) ||
+ (d->rot == 3 && vx > 0.0f);
+ float b = back ? a + PT_PI : a;
+ qt q = {0.0f, pt_sinf(b * 0.5f), 0.0f, pt_cosf(b * 0.5f)};
+ v3 off = qrot(q, V3(0.0f, 0.35f, -1.35f));
+ if (d->wide) {
+ float s = pt_sinf(a), co = pt_cosf(a);
+ /* the part's turn of (10, 0, 0) about y */
+ off = vadd(off, V3(10.0f * co, 0.0f, -10.0f * s));
+ }
+ c->respawn.pos = vadd(d->part, off);
+ c->respawn.rot = q;
+ c->respawn.next = c->next_cp;
+ c->has_respawn = 1;
+ return;
+ }
+}
+
+/* ------------------------------------------------------------------ a frame */
+void car_step(car_t *c, unsigned in) {
+ if (c->frames > PT_MAX_FRAMES) return;
+ cache_refresh(c);
+ c->impulse = 0.0f;
+ /* the downforce: -up |v . forward| 0.05 */
+ {
+ v3 up = mcolumn(&c->basis, 1), fwd = mcolumn(&c->basis, 2);
+ float s = pt_absf(vdot(c->vel, fwd));
+ c->vel = vadd(c->vel, vmul(up, -s * 0.05f * INV_MASS));
+ }
+ if (c->finish) in = 0;
+ c->frames++;
+ bool up = in & IN_UP, down = in & IN_DOWN, left = in & IN_LEFT, right = in & IN_RIGHT;
+ bool w0 = c->w[0].contact, w1 = c->w[1].contact, w2 = c->w[2].contact, w3 = c->w[3].contact;
+ float lock_now = 0.0f;
+ bool grounded;
+ bool engine_off = false;
+ if (w0 || w1 || w2 || w3) {
+ c->ang_damp = DAMP_ANG_LOW;
+ c->air = 0.0f;
+ c->lock -= 0.001f;
+ grounded = true;
+ if (c->lock < 0.0f) c->lock = 0.0f;
+ lock_now = c->lock;
+ } else {
+ c->ang_damp = down ? DAMP_ANG_HIGH : DAMP_ANG_LOW;
+ grounded = false;
+ c->air += 0.001f;
+ if (c->air > 0.2f) {
+ c->air = 0.2f, c->lock = 0.2f;
+ engine_off = true;
+ }
+ lock_now = c->lock;
+ }
+ if (!(w0 || w1 || w2) && w3) c->ang_damp = DAMP_ANG_LOW;
+ float engine = engine_off ? 0.0f : (up && lock_now == 0.0f ? ENGINE : 0.0f);
+ c->w[2].engine = c->w[3].engine = engine;
+ c->brake_light = 0;
+ if (down) {
+ if (!(grounded && !(c->speed > 1.0f))) {
+ for (int i = 0; i < 4; i++) c->w[i].brake = 10.0f;
+ c->brake_light = 1;
+ } else {
+ for (int i = 0; i < 4; i++) c->w[i].brake = 0.0f;
+ float e = (c->speed / 100.0f + 1.0f) * -2000.0f;
+ e = e > -2000.0f ? e : -2000.0f;
+ e = e < 0.0f ? e : 0.0f;
+ c->w[2].engine = c->w[3].engine = e;
+ }
+ } else {
+ for (int i = 0; i < 4; i++) c->w[i].brake = 0.0f;
+ }
+ /* steering: where the car slides, while a front wheel touches, and the keys, less at speed */
+ {
+ v3 lv = mcol(&c->basis, c->vel);
+ float lx = lv.x, lz = lv.z, l2 = lx * lx + lz * lz;
+ if (l2 != 0.0f) {
+ float l = pt_sqrtf(l2);
+ lz = lz / l, lx = lx / l;
+ }
+ float k = pt_clampf(c->speed / 30.0f, 0.0f, 1.0f);
+ float slip = PT_HALF_PI - (pt_atan2f(-lz, -lx) + PT_PI);
+ float assist = (w0 ? k : (w1 ? k : 0.0f)) * slip;
+ assist = pt_clampf(assist, -0.410258055f, 0.410258055f);
+ float amount = 155.0f / pt_powf(pt_maxf(pt_absf(c->speed), 46.0f), 1.55f);
+ float s = c->steer_in;
+ if (left && !right) s = pt_minf(1.0f, s + 0.010000001f);
+ else if (right && !left) s = pt_maxf(-1.0f, s - 0.010000001f);
+ else if (s > 0.0f) s = pt_maxf(0.0f, s - 0.010000001f);
+ else if (s < 0.0f) s = pt_minf(0.0f, s + 0.010000001f);
+ c->steer_in = s;
+ float key = amount * s, st;
+ if (assist < 0.0f && key < 0.0f) st = assist > key ? key : assist;
+ else if (assist > 0.0f && key > 0.0f) st = assist < key ? key : assist;
+ else st = assist + key;
+ c->w[0].steering = c->w[1].steering = st;
+ }
+ for (int i = 0; i < 4; i++) c->w[i].stiffness = c->w[i].susp < -0.2f ? 200.0f : 50.0f;
+ /* Reset: back to the last checkpoint */
+ bool reset = in & IN_RESET;
+ if (reset && !c->reset_held && c->has_respawn) {
+ c->pos = c->respawn.pos;
+ c->rot = c->respawn.rot;
+ update_inertia(c);
+ c->vel = c->ang = V3(0.0f, 0.0f, 0.0f);
+ for (int i = 0; i < 4; i++) update_wheel_ws(c, &c->w[i], i);
+ wheels_reset(c);
+ c->next_cp = c->respawn.next;
+ c->cache_ok = 0;
+ cache_refresh(c);
+ }
+ c->reset_held = reset;
+ /* Bullet's step */
+ if (c->pos.y < 4.0f) c->ground_on = 1;
+ else if (c->pos.y > 5.0f) c->ground_on = 0;
+ c->vel = vmul(c->vel, DAMP_LIN);
+ c->ang = vmul(c->ang, c->ang_damp);
+ contact_t ct[MAX_CONTACTS];
+ int n = find_contacts(c, ct);
+ v3 push = V3(0.0f, 0.0f, 0.0f), turn = push;
+ if (n) {
+ solve(c, ct, n, &push, &turn);
+ if (vlen2(push) != 0.0f || vlen2(turn) != 0.0f) integrate(c, push, vmul(turn, 0.1f));
+ } else {
+ c->vel = vadd(c->vel, V3(0.0f, GRAVITY * DT, 0.0f));
+ }
+ integrate(c, c->vel, c->ang);
+ vehicle_update(c);
+ if (!c->finish) checkpoints(c);
+}
+
+uint32_t car_hash(const car_t *c) {
+ uint32_t h = 2166136261u;
+ h = pt_hash(h, &c->pos, sizeof c->pos);
+ h = pt_hash(h, &c->rot, sizeof c->rot);
+ h = pt_hash(h, &c->vel, sizeof c->vel);
+ h = pt_hash(h, &c->ang, sizeof c->ang);
+ h = pt_hash(h, &c->frames, sizeof c->frames);
+ h = pt_hash(h, &c->next_cp, sizeof c->next_cp);
+ h = pt_hash(h, &c->finish, sizeof c->finish);
+ for (int i = 0; i < 4; i++) h = pt_hash(h, &c->w[i].susp, sizeof(float));
+ return h;
+}
diff --git a/games/polytrack/src/car.h b/games/polytrack/src/car.h
new file mode 100644
index 00000000..6e4f85b6
--- /dev/null
+++ b/games/polytrack/src/car.h
@@ -0,0 +1,63 @@
+/* The car (car.c): PolyTrack's physics, a port of what its polytrack_physics.wasm does each 1 ms frame. A Bullet
+ * rigid body of 400 kg with a convex hull, a btRaycastVehicle with four wheels, the downforce and the steering PolyTrack
+ * adds, the checkpoints and the finish. Floats, rounded the same on every machine (see pt.h). */
+#ifndef PT_CAR_H
+#define PT_CAR_H
+#include "pt.h"
+#include "world.h"
+
+#define PT_HZ 1000
+#define PT_MAX_FRAMES 5999998u
+
+enum { IN_UP = 1, IN_RIGHT = 2, IN_DOWN = 4, IN_LEFT = 8, IN_RESET = 16 };
+
+typedef struct {
+ /* raycast info */
+ v3 normal, point, hard, dir, axle;
+ float susp; /* suspension length */
+ uint8_t contact, ground;
+ /* the rest of btWheelInfo that changes */
+ float steering, rotation, drot;
+ float engine, brake, stiffness;
+ float clipped, relvel, force, skid;
+} wheel_t;
+
+typedef struct {
+ v3 pos;
+ qt rot;
+ uint16_t next; /* the next checkpoint (index into the sorted checkpoints) */
+} respawn_t;
+
+typedef struct {
+ /* the rigid body */
+ v3 pos, vel, ang;
+ qt rot;
+ m3 basis, inv_inertia; /* world */
+ float ang_damp;
+ wheel_t w[4];
+ float speed; /* km/h, minus going backwards (btRaycastVehicle) */
+ float steer_in; /* -1 .. 1, eased */
+ float air, lock; /* seconds in the air, engine lock after a long jump */
+ uint32_t frames; /* 1 ms frames since the start */
+ uint32_t finish; /* frames at the finish, 0 while racing */
+ uint16_t next_cp;
+ uint8_t has_respawn, reset_held, brake_light, ground_on;
+ respawn_t respawn;
+ uint32_t cp_frames[64]; /* when each checkpoint was taken (for the splits) */
+ /* the physics triangles round the car */
+ v3 cache_at;
+ int ncache;
+ uint8_t cache_ok;
+ float impulse; /* the biggest contact impulse this frame (a bump) */
+} car_t;
+
+extern const v3 car_wheel_pos[4];
+#define CAR_MASS_OFFSET 0.6f
+#define CAR_RADIUS 0.331f
+
+void car_reset(car_t *c); /* at the start, as before the first frame */
+void car_step(car_t *c, unsigned input);
+bool car_finished(const car_t *c);
+uint32_t car_hash(const car_t *c); /* the state's bits, for the tests */
+int car_checkpoints(void); /* checkpoints on the track (orders) */
+#endif
diff --git a/games/polytrack/src/data.S b/games/polytrack/src/data.S
new file mode 100644
index 00000000..ccf0a685
--- /dev/null
+++ b/games/polytrack/src/data.S
@@ -0,0 +1,19 @@
+/* data.bin (tools/pack.py) in the app's read-only data */
+#ifdef __APPLE__
+ .const_data
+ .p2align 2
+ .globl _pt_blob
+_pt_blob:
+ .incbin DATA_BIN
+ .p2align 2
+#else
+ .section .rodata.pt_blob,"a"
+ .balign 4
+ .global pt_blob
+pt_blob:
+ .incbin DATA_BIN
+ .balign 4
+#endif
+#ifdef __ELF__
+ .section .note.GNU-stack,"",%progbits
+#endif
diff --git a/games/polytrack/src/data.bin b/games/polytrack/src/data.bin
new file mode 100644
index 00000000..9aa9572a
Binary files /dev/null and b/games/polytrack/src/data.bin differ
diff --git a/games/polytrack/src/data.c b/games/polytrack/src/data.c
new file mode 100644
index 00000000..15d1cb9e
--- /dev/null
+++ b/games/polytrack/src/data.c
@@ -0,0 +1,620 @@
+/* Made by tools/pack.py from Kodub's web build of PolyTrack 0.6.3. Do not edit. */
+#include "data.h"
+
+const pt_chunk pt_chunks[216] = {
+ {{{0, 68, 0, 123, 0}, {68, 22, 0, 40, 123}}, {-1681,0,-2560,1681,241,2560}},
+ {{{90, 164, 0, 324, 163}, {254, 57, 0, 110, 487}}, {-1681,0,-2560,1681,1521,2560}},
+ {{{311, 164, 0, 324, 597}, {475, 55, 0, 106, 921}}, {-1681,0,-2560,1681,1521,2560}},
+ {{{530, 68, 0, 113, 1027}, {598, 23, 0, 41, 1140}}, {-1681,0,-2560,1681,2801,2560}},
+ {{{621, 78, 0, 149, 1181}, {699, 34, 0, 64, 1330}}, {-1681,0,-2560,1681,241,2560}},
+ {{{733, 169, 0, 323, 1394}, {902, 108, 0, 206, 1717}}, {-1681,0,-2560,1681,1258,2560}},
+ {{{1010, 68, 0, 129, 1923}, {1078, 49, 0, 93, 2052}}, {-2560,0,-2560,2560,241,2560}},
+ {{{1127, 68, 0, 126, 2145}, {1195, 34, 0, 64, 2271}}, {-1681,0,-2560,2560,241,2560}},
+ {{{1229, 68, 0, 126, 2335}, {1297, 34, 0, 64, 2461}}, {-2560,0,-2560,1681,241,2560}},
+ {{{1331, 38, 0, 70, 2525}, {1369, 13, 0, 22, 2595}}, {-2560,0,-2560,1681,241,2560}},
+ {{{1382, 38, 0, 71, 2617}, {1420, 24, 0, 44, 2688}}, {-2560,0,-2560,2560,241,2560}},
+ {{{1444, 58, 0, 112, 2732}, {1502, 23, 0, 42, 2844}}, {-1681,0,-1681,2560,241,2560}},
+ {{{1525, 120, 0, 232, 2886}, {1645, 58, 0, 108, 3118}}, {-1681,0,-2560,2560,1521,2560}},
+ {{{1703, 120, 0, 232, 3226}, {1823, 58, 0, 108, 3458}}, {-2560,0,-2560,1681,1521,2560}},
+ {{{1881, 120, 0, 236, 3566}, {2001, 61, 0, 118, 3802}}, {-1681,0,-2560,2560,1521,2560}},
+ {{{2062, 120, 0, 236, 3920}, {2182, 61, 0, 118, 4156}}, {-2560,0,-2560,1681,1521,2560}},
+ {{{2243, 38, 0, 53, 4274}, {2281, 38, 0, 53, 4327}}, {-1681,0,-2560,2560,2801,2560}},
+ {{{2319, 38, 0, 53, 4380}, {2357, 38, 0, 53, 4433}}, {-2560,0,-2560,1681,2801,2560}},
+ {{{2395, 16, 0, 28, 4486}, {2411, 16, 0, 28, 4514}}, {-384,0,-384,384,1280,384}},
+ {{{2427, 8, 0, 12, 4542}, {2435, 8, 0, 12, 4554}}, {-256,0,-256,256,1280,256}},
+ {{{2443, 16, 0, 28, 4566}, {2459, 16, 0, 28, 4594}}, {-384,0,-384,384,1280,384}},
+ {{{2475, 24, 0, 44, 4622}, {2499, 24, 0, 44, 4666}}, {-384,0,-384,384,1280,384}},
+ {{{2523, 24, 0, 38, 4710}, {2547, 24, 0, 38, 4748}}, {-2560,0,-2560,2560,1280,2560}},
+ {{{2571, 32, 0, 54, 4786}, {2603, 32, 0, 54, 4840}}, {-2560,0,-2560,2560,1280,2560}},
+ {{{2635, 8, 0, 12, 4894}, {2643, 8, 0, 12, 4906}}, {-2560,0,-2560,2560,54,2560}},
+ {{{2651, 24, 0, 44, 4918}, {2675, 24, 0, 44, 4962}}, {-2560,0,2304,2560,1280,2560}},
+ {{{2699, 40, 0, 76, 5006}, {2739, 40, 0, 76, 5082}}, {-2560,0,-2560,2560,1280,2560}},
+ {{{2779, 16, 0, 28, 5158}, {2795, 16, 0, 28, 5186}}, {2304,0,2304,2560,1280,2560}},
+ {{{2811, 8, 0, 12, 5214}, {2819, 8, 0, 12, 5226}}, {-2560,0,-2560,2560,1280,2560}},
+ {{{2827, 170, 0, 335, 5238}, {2997, 58, 0, 111, 5573}}, {-2560,0,-2560,2560,5120,2560}},
+ {{{3055, 12, 0, 20, 5684}, {3067, 12, 0, 20, 5704}}, {-2560,0,-2560,2560,1280,-2506}},
+ {{{3079, 170, 0, 335, 5724}, {3249, 58, 0, 111, 6059}}, {-2560,0,-2560,2560,5120,2560}},
+ {{{3307, 42, 0, 80, 6170}, {3349, 20, 0, 36, 6250}}, {-2560,0,-2560,2560,1334,2560}},
+ {{{3369, 42, 0, 80, 6286}, {3411, 19, 0, 34, 6366}}, {-2560,0,-2560,2560,1334,2560}},
+ {{{3430, 8, 0, 12, 6400}, {3438, 8, 0, 12, 6412}}, {-2560,0,-2560,2560,2614,2560}},
+ {{{3446, 127, 0, 200, 6424}, {3573, 55, 0, 74, 6624}}, {-968,0,-6276,4681,241,-608}},
+ {{{3628, 199, 0, 366, 6698}, {3827, 77, 0, 133, 7064}}, {-1681,0,-2893,3579,241,2560}},
+ {{{3904, 199, 0, 362, 7197}, {4103, 77, 0, 132, 7559}}, {2227,0,-6801,7680,241,-1541}},
+ {{{4180, 39, 0, 51, 7691}, {4219, 23, 0, 25, 7742}}, {1606,0,-8634,4785,241,-5146}},
+ {{{4242, 173, 0, 285, 7767}, {4415, 88, 0, 131, 8052}}, {-1252,0,-7623,4785,241,-814}},
+ {{{4503, 162, 0, 297, 8183}, {4665, 84, 0, 145, 8480}}, {-1681,0,-2452,2586,241,2560}},
+ {{{4749, 169, 0, 282, 8625}, {4918, 88, 0, 134, 8907}}, {2361,0,-11452,9572,241,-5553}},
+ {{{5006, 98, 0, 149, 9041}, {5104, 50, 0, 71, 9190}}, {1975,0,-7999,7498,241,-3435}},
+ {{{5154, 163, 0, 300, 9261}, {5317, 81, 0, 138, 9561}}, {7258,0,-11921,12800,241,-7471}},
+ {{{5398, 117, 0, 215, 9699}, {5515, 40, 0, 64, 9914}}, {-1681,414,-7680,1681,2801,-1280}},
+ {{{5555, 118, 0, 217, 9978}, {5673, 41, 0, 66, 10195}}, {-1681,0,-3840,1681,1217,2560}},
+ {{{5714, 117, 0, 215, 10261}, {5831, 44, 0, 75, 10476}}, {-1681,0,-2560,1681,2547,4608}},
+ {{{5875, 118, 0, 217, 10551}, {5993, 43, 0, 73, 10768}}, {-1681,1777,1280,1681,2801,7680}},
+ {{{6036, 210, 0, 391, 10841}, {6246, 106, 0, 191, 11232}}, {-6801,0,-12800,-2055,241,-6899}},
+ {{{6352, 142, 0, 219, 11423}, {6494, 60, 0, 80, 11642}}, {-6002,0,-8714,200,241,-1901}},
+ {{{6554, 79, 0, 117, 11722}, {6633, 38, 0, 51, 11839}}, {-2982,0,-2711,-1538,241,2560}},
+ {{{6671, 81, 0, 119, 11890}, {6752, 42, 0, 52, 12009}}, {-3442,0,-7593,882,241,-1859}},
+ {{{6794, 151, 0, 260, 12061}, {6945, 78, 0, 124, 12321}}, {-2982,0,-3189,1681,241,2560}},
+ {{{7023, 81, 0, 118, 12445}, {7104, 42, 0, 57, 12563}}, {2055,0,-12800,3556,241,-7390}},
+ {{{7146, 136, 0, 220, 12620}, {7282, 65, 0, 86, 12840}}, {-882,0,-8339,5058,241,-1859}},
+ {{{7347, 201, 0, 377, 12926}, {7548, 99, 0, 175, 13303}}, {-1681,0,-3189,2982,241,2560}},
+ {{{7647, 163, 0, 273, 13478}, {7810, 82, 0, 134, 13751}}, {2343,0,-12800,6801,241,-6899}},
+ {{{7892, 81, 0, 118, 13885}, {7973, 38, 0, 46, 14003}}, {1678,0,-8714,6002,241,-2647}},
+ {{{8011, 128, 0, 248, 14049}, {8139, 56, 0, 108, 14297}}, {-2560,0,-2560,2560,241,2560}},
+ {{{8195, 16, 0, 28, 14405}, {8211, 16, 0, 28, 14433}}, {-256,0,-2560,256,1280,256}},
+ {{{8227, 22, 0, 40, 14461}, {8249, 22, 0, 40, 14501}}, {-256,0,-2560,2560,1280,256}},
+ {{{8271, 32, 0, 60, 14541}, {8303, 32, 0, 60, 14601}}, {-2560,0,-2560,2560,1280,2560}},
+ {{{8335, 68, 0, 132, 14661}, {8403, 50, 0, 96, 14793}}, {-2560,0,-2560,2560,1280,2560}},
+ {{{8453, 240, 0, 326, 14889}, {8693, 87, 0, 170, 15215}}, {-2560,0,-2560,2560,5120,2560}},
+ {{{8780, 240, 0, 325, 15385}, {9020, 0, 0, 0, 15710}}, {-2560,0,-2560,2560,5114,2560}},
+ {{{9020, 46, 0, 39, 15710}, {9066, 0, 0, 0, 15749}}, {-1234,0,-2560,2560,5120,2560}},
+ {{{9066, 96, 0, 177, 15749}, {9162, 43, 0, 76, 15926}}, {-1681,0,-2560,1681,1258,2560}},
+ {{{9205, 6, 0, 8, 16002}, {9211, 6, 0, 8, 16010}}, {-2560,0,-2560,2560,1280,2560}},
+ {{{9217, 6, 0, 8, 16018}, {9223, 6, 0, 8, 16026}}, {-2560,0,-2560,0,1280,2560}},
+ {{{9229, 6, 0, 8, 16034}, {9235, 6, 0, 8, 16042}}, {-2560,0,-2560,2560,54,2560}},
+ {{{9241, 6, 0, 8, 16050}, {9247, 6, 0, 8, 16058}}, {-2560,0,-2560,0,54,2560}},
+ {{{9253, 66, 0, 128, 16066}, {9319, 66, 0, 128, 16194}}, {-2560,0,-2560,2560,1280,-2506}},
+ {{{9385, 66, 0, 128, 16322}, {9451, 66, 0, 128, 16450}}, {-2560,0,-2560,2560,1280,-2506}},
+ {{{9517, 48, 0, 91, 16578}, {9565, 48, 0, 91, 16669}}, {-2560,0,-2560,2560,1280,-2506}},
+ {{{9613, 48, 0, 91, 16760}, {9661, 48, 0, 91, 16851}}, {-2560,0,-2560,2560,1280,-2506}},
+ {{{9709, 54, 0, 98, 16942}, {9763, 54, 0, 98, 17040}}, {-2560,0,-2560,2560,1280,-2506}},
+ {{{9817, 48, 0, 92, 17138}, {9865, 48, 0, 92, 17230}}, {-2560,0,-2560,2560,1280,-2506}},
+ {{{9913, 60, 0, 87, 17322}, {9973, 29, 0, 37, 17409}}, {-1681,0,-2560,6801,1258,2560}},
+ {{{10002, 60, 0, 90, 17446}, {10062, 30, 0, 39, 17536}}, {-1681,0,-2560,6801,1258,2560}},
+ {{{10092, 12, 0, 20, 17575}, {10104, 12, 0, 20, 17595}}, {-2560,1226,-2560,2560,1280,2560}},
+ {{{10116, 12, 0, 20, 17615}, {10128, 12, 0, 20, 17635}}, {-2560,0,-2560,2560,54,2560}},
+ {{{10140, 6, 0, 8, 17655}, {10146, 6, 0, 8, 17663}}, {-2560,0,-2560,2560,2560,2560}},
+ {{{10152, 7, 0, 10, 17671}, {10159, 7, 0, 10, 17681}}, {-2560,0,-2560,2560,2560,2560}},
+ {{{10166, 5, 0, 6, 17691}, {10171, 5, 0, 6, 17697}}, {-2560,0,-2560,2560,2560,2560}},
+ {{{10176, 6, 0, 8, 17703}, {10182, 6, 0, 8, 17711}}, {-2560,0,-2560,2560,2560,2560}},
+ {{{10188, 7, 0, 10, 17719}, {10195, 7, 0, 10, 17729}}, {-2560,0,-2560,2560,2560,2560}},
+ {{{10202, 5, 0, 6, 17739}, {10207, 5, 0, 6, 17745}}, {-2560,0,-2560,2560,2560,2560}},
+ {{{10212, 36, 0, 60, 17751}, {10248, 26, 0, 42, 17811}}, {-2560,0,-2560,2560,1258,2560}},
+ {{{10274, 167, 0, 322, 17853}, {10441, 153, 0, 296, 18175}}, {-2560,0,-2560,2560,1258,2560}},
+ {{{10594, 27, 0, 30, 18471}, {10621, 18, 0, 19, 18501}}, {-2560,0,-2560,7680,1258,2560}},
+ {{{10639, 27, 0, 30, 18520}, {10666, 20, 0, 23, 18550}}, {-2560,0,-2560,7680,1258,2560}},
+ {{{10686, 166, 0, 287, 18573}, {10852, 156, 0, 274, 18860}}, {-2560,0,-2560,7680,1258,2560}},
+ {{{11008, 166, 0, 287, 19134}, {11174, 156, 0, 274, 19421}}, {-2560,0,-2560,7680,1258,2560}},
+ {{{11330, 13, 0, 9, 19695}, {11343, 10, 0, 4, 19704}}, {2674,0,-13123,3930,241,-11048}},
+ {{{11353, 107, 0, 164, 19708}, {11460, 54, 0, 77, 19872}}, {-70,0,-12562,5281,241,-5614}},
+ {{{11514, 172, 0, 304, 19949}, {11686, 82, 0, 129, 20253}}, {-1393,0,-7549,3874,241,-491}},
+ {{{11768, 138, 0, 254, 20382}, {11906, 71, 0, 122, 20636}}, {-1681,0,-3001,2286,241,2560}},
+ {{{11977, 14, 0, 12, 20758}, {11991, 10, 0, 5, 20770}}, {7778,0,-17970,9323,241,-16887}},
+ {{{12001, 156, 0, 255, 20775}, {12157, 78, 0, 114, 21030}}, {2970,0,-17215,9635,241,-10442}},
+ {{{12235, 124, 0, 189, 21144}, {12359, 56, 0, 78, 21333}}, {1367,0,-12913,7451,241,-5614}},
+ {{{12415, 15, 0, 13, 21411}, {12430, 9, 0, 4, 21424}}, {2923,0,-7153,3988,241,-5263}},
+ {{{12439, 105, 0, 162, 21428}, {12544, 49, 0, 67, 21590}}, {7778,0,-20576,14412,241,-15454}},
+ {{{12593, 126, 0, 194, 21657}, {12719, 58, 0, 80, 21851}}, {7150,0,-18574,14347,241,-12707}},
+ {{{12777, 173, 0, 308, 21931}, {12950, 83, 0, 132, 22239}}, {12593,0,-21831,19096,241,-16385}},
+ {{{13033, 14, 0, 11, 22371}, {13047, 10, 0, 5, 22382}}, {13178,0,-17346,15299,241,-16385}},
+ {{{13057, 141, 0, 261, 22387}, {13198, 68, 0, 114, 22648}}, {17730,0,-22161,23040,241,-18159}},
+ {{{13266, 23, 0, 42, 22762}, {13289, 12, 0, 20, 22804}}, {-2560,0,-2560,2560,1280,2304}},
+ {{{13301, 24, 0, 44, 22824}, {13325, 14, 0, 24, 22868}}, {-2560,0,-2560,2560,1280,2560}},
+ {{{13339, 36, 0, 68, 22892}, {13375, 20, 0, 36, 22960}}, {-2560,259,-2560,2560,5120,2301}},
+ {{{13395, 36, 0, 68, 22996}, {13431, 20, 0, 36, 23064}}, {-2560,0,-2560,2560,4861,2301}},
+ {{{13451, 20, 0, 36, 23100}, {13471, 20, 0, 36, 23136}}, {-2560,0,-2560,2560,54,2560}},
+ {{{13491, 16, 0, 18, 23172}, {13507, 16, 0, 18, 23190}}, {-2560,0,-2560,7680,54,2560}},
+ {{{13523, 16, 0, 18, 23208}, {13539, 16, 0, 18, 23226}}, {-2560,0,-2560,7680,54,2560}},
+ {{{13555, 31, 0, 38, 23244}, {13586, 21, 0, 20, 23282}}, {1067,0,-8631,3778,241,-5703}},
+ {{{13607, 80, 0, 132, 23302}, {13687, 51, 0, 76, 23434}}, {-1153,0,-7925,3062,241,-1208}},
+ {{{13738, 55, 0, 93, 23510}, {13793, 40, 0, 66, 23603}}, {-1681,0,-2560,2560,241,2560}},
+ {{{13833, 79, 0, 130, 23669}, {13912, 50, 0, 77, 23799}}, {2315,0,-11393,9032,241,-7267}},
+ {{{13962, 8, 0, 8, 23876}, {13970, 7, 0, 6, 23884}}, {2315,0,-7925,7680,54,-2560}},
+ {{{13977, 55, 0, 93, 23890}, {14032, 38, 0, 60, 23983}}, {7680,0,-11921,12800,241,-7680}},
+ {{{14070, 49, 0, 75, 24043}, {14119, 32, 0, 45, 24118}}, {343,0,-10909,4003,241,-6052}},
+ {{{14151, 62, 0, 90, 24163}, {14213, 33, 0, 44, 24253}}, {-1302,0,-7680,2560,241,2560}},
+ {{{14246, 58, 0, 93, 24297}, {14304, 32, 0, 48, 24390}}, {-1681,0,-7680,2560,241,2560}},
+ {{{14336, 48, 0, 73, 24438}, {14384, 33, 0, 46, 24511}}, {4383,0,-15017,9351,241,-11427}},
+ {{{14417, 37, 0, 54, 24557}, {14454, 28, 0, 36, 24611}}, {2560,0,-12800,7680,241,-7680}},
+ {{{14482, 62, 0, 89, 24647}, {14544, 34, 0, 45, 24736}}, {7680,0,-16662,17920,241,-12800}},
+ {{{14578, 60, 0, 94, 24781}, {14638, 33, 0, 50, 24875}}, {7680,0,-17041,17920,241,-12800}},
+ {{{14671, 20, 0, 18, 24925}, {14691, 13, 0, 11, 24943}}, {-2560,0,-7680,3265,54,2560}},
+ {{{14704, 85, 0, 148, 24954}, {14789, 42, 0, 68, 25102}}, {-2560,0,-7680,2830,241,2560}},
+ {{{14831, 20, 0, 18, 25170}, {14851, 13, 0, 11, 25188}}, {2560,0,-12800,12800,54,-6583}},
+ {{{14864, 105, 0, 182, 25199}, {14969, 61, 0, 96, 25381}}, {2319,0,-7830,8497,241,-1743}},
+ {{{15030, 13, 0, 9, 25477}, {15043, 8, 0, 3, 25486}}, {2319,0,-3840,3552,241,-1694}},
+ {{{15051, 85, 0, 148, 25489}, {15136, 43, 0, 70, 25637}}, {2560,0,-12800,12800,241,-7410}},
+ {{{15179, 14, 0, 9, 25707}, {15193, 7, 0, 3, 25716}}, {6969,0,-7830,8546,241,-7169}},
+ {{{15200, 240, 0, 475, 25719}, {15440, 66, 0, 127, 26194}}, {-1681,0,-2560,1681,5120,2560}},
+ {{{15506, 12, 0, 20, 26321}, {15518, 12, 0, 20, 26341}}, {-256,0,-2560,256,896,256}},
+ {{{15530, 12, 0, 20, 26361}, {15542, 12, 0, 20, 26381}}, {-256,384,-2560,256,1280,256}},
+ {{{15554, 17, 0, 30, 26401}, {15571, 17, 0, 30, 26431}}, {-256,0,-2560,2560,896,256}},
+ {{{15588, 17, 0, 30, 26461}, {15605, 17, 0, 30, 26491}}, {-256,384,-2560,2560,1280,256}},
+ {{{15622, 22, 0, 40, 26521}, {15644, 22, 0, 40, 26561}}, {-2560,0,-2560,2560,896,256}},
+ {{{15666, 16, 0, 28, 26601}, {15682, 16, 0, 28, 26629}}, {-2560,384,-2560,2560,896,256}},
+ {{{15698, 24, 0, 44, 26657}, {15722, 24, 0, 44, 26701}}, {-2560,384,-2560,2560,896,2560}},
+ {{{15746, 24, 0, 44, 26745}, {15770, 24, 0, 44, 26789}}, {-2560,0,-256,2560,1280,256}},
+ {{{15794, 16, 0, 28, 26833}, {15810, 16, 0, 28, 26861}}, {-2560,0,-256,2560,896,256}},
+ {{{15826, 8, 0, 12, 26889}, {15834, 8, 0, 12, 26901}}, {-2560,384,-256,2560,896,256}},
+ {{{15842, 16, 0, 28, 26913}, {15858, 16, 0, 28, 26941}}, {-2560,384,-256,2560,1280,256}},
+ {{{15874, 84, 0, 149, 26969}, {15958, 38, 0, 66, 27118}}, {-1681,0,-2560,1681,1280,2560}},
+ {{{15996, 8, 0, 12, 27184}, {16004, 8, 0, 12, 27196}}, {-251,0,-2560,251,1280,2560}},
+ {{{16012, 120, 0, 232, 27208}, {16132, 60, 0, 112, 27440}}, {-2560,0,-2560,2560,5120,2560}},
+ {{{16192, 67, 0, 130, 27552}, {16259, 27, 0, 50, 27682}}, {-2560,0,-2560,2560,5120,2560}},
+ {{{16286, 36, 0, 68, 27732}, {16322, 17, 0, 30, 27800}}, {-2560,0,-2560,2560,5120,2560}},
+ {{{16339, 25, 0, 44, 27830}, {16364, 14, 0, 20, 27874}}, {-2560,409,-7680,2560,2614,-1280}},
+ {{{16378, 27, 0, 48, 27894}, {16405, 14, 0, 20, 27942}}, {-2560,0,-3584,2560,921,2560}},
+ {{{16419, 26, 0, 46, 27962}, {16445, 14, 0, 20, 28008}}, {-2560,0,-2560,2560,2204,3584}},
+ {{{16459, 28, 0, 50, 28028}, {16487, 14, 0, 20, 28078}}, {-2560,1614,1280,2560,2614,7680}},
+ {{{16501, 95, 0, 177, 28098}, {16596, 47, 0, 77, 28275}}, {-2560,409,-7680,1681,2801,-1536}},
+ {{{16643, 96, 0, 179, 28352}, {16739, 52, 0, 87, 28531}}, {-2560,0,-3584,1681,1139,2560}},
+ {{{16791, 95, 0, 177, 28618}, {16886, 47, 0, 77, 28795}}, {-1681,409,-7680,2560,2801,-1536}},
+ {{{16933, 96, 0, 179, 28872}, {17029, 52, 0, 87, 29051}}, {-1681,0,-3584,2560,1139,2560}},
+ {{{17081, 95, 0, 176, 29138}, {17176, 46, 0, 75, 29314}}, {-2560,0,-2560,1681,2392,3584}},
+ {{{17222, 97, 0, 180, 29389}, {17319, 48, 0, 79, 29569}}, {-2560,1638,1536,1681,2801,7680}},
+ {{{17367, 95, 0, 176, 29648}, {17462, 46, 0, 75, 29824}}, {-1681,0,-2560,2560,2392,3584}},
+ {{{17508, 97, 0, 180, 29899}, {17605, 48, 0, 79, 30079}}, {-1681,1638,1536,2560,2801,7680}},
+ {{{17653, 19, 0, 27, 30158}, {17672, 12, 0, 15, 30185}}, {-2560,0,-7680,2560,2560,2304}},
+ {{{17684, 14, 0, 17, 30200}, {17698, 8, 0, 7, 30217}}, {-2560,0,-7680,2560,921,2304}},
+ {{{17706, 14, 0, 15, 30224}, {17720, 9, 0, 8, 30239}}, {-2560,0,-2560,2560,2150,7680}},
+ {{{17729, 21, 0, 29, 30247}, {17750, 13, 0, 16, 30276}}, {-2560,0,-2560,2560,2560,7680}},
+ {{{17763, 36, 0, 68, 30292}, {17799, 16, 0, 28, 30360}}, {-2560,0,-2560,2560,1280,2560}},
+ {{{17815, 185, 0, 348, 30388}, {18000, 60, 0, 110, 30736}}, {-1681,0,-2560,1681,5120,2560}},
+ {{{18060, 68, 0, 123, 30846}, {18128, 22, 0, 40, 30969}}, {-1504,528,-2560,1564,2192,2560}},
+ {{{18150, 15, 0, 11, 31009}, {18165, 9, 0, 4, 31020}}, {3028,1916,-12938,4155,2192,-11452}},
+ {{{18174, 129, 0, 203, 31024}, {18303, 58, 0, 80, 31227}}, {90,528,-12252,5004,2192,-5486}},
+ {{{18361, 191, 0, 338, 31307}, {18552, 86, 0, 141, 31645}}, {-1236,528,-7929,3937,2192,-906}},
+ {{{18638, 144, 0, 267, 31786}, {18782, 71, 0, 127, 32053}}, {-1504,528,-2334,2049,2192,2560}},
+ {{{18853, 16, 0, 16, 32180}, {18869, 11, 0, 6, 32196}}, {7483,1916,-17899,9690,2192,-16325}},
+ {{{18880, 183, 0, 311, 32202}, {19063, 88, 0, 132, 32513}}, {3028,528,-17233,9930,2192,-10550}},
+ {{{19151, 113, 0, 174, 32645}, {19264, 57, 0, 80, 32819}}, {2057,528,-12916,7898,2032,-6303}},
+ {{{19321, 131, 0, 208, 32899}, {19452, 58, 0, 82, 33107}}, {8228,528,-20294,14469,2192,-15724}},
+ {{{19510, 116, 0, 179, 33189}, {19626, 54, 0, 76, 33368}}, {7678,528,-18423,14575,2032,-12916}},
+ {{{19680, 190, 0, 340, 33444}, {19870, 93, 0, 152, 33784}}, {12879,528,-21698,19994,2192,-16449}},
+ {{{19963, 144, 0, 269, 33936}, {20107, 70, 0, 122, 34205}}, {18123,528,-21984,23040,2192,-18431}},
+ {{{20177, 168, 0, 313, 34327}, {20345, 85, 0, 150, 34640}}, {-6801,0,-17920,-2533,241,-11975}},
+ {{{20430, 123, 0, 205, 34790}, {20553, 57, 0, 78, 34995}}, {-6362,0,-13854,-367,241,-4279}},
+ {{{20610, 74, 0, 103, 35073}, {20684, 34, 0, 38, 35176}}, {-5056,0,-11538,291,241,-1780}},
+ {{{20718, 56, 0, 77, 35214}, {20774, 28, 0, 33, 35291}}, {-2887,0,-3832,-1610,241,2560}},
+ {{{20802, 21, 0, 23, 35324}, {20823, 14, 0, 9, 35347}}, {-2655,0,-12412,-155,241,-6073}},
+ {{{20837, 83, 0, 122, 35356}, {20920, 41, 0, 52, 35478}}, {-3389,0,-9932,1242,241,-1553}},
+ {{{20961, 140, 0, 241, 35530}, {21101, 71, 0, 114, 35771}}, {-2518,0,-3614,1681,241,2560}},
+ {{{21172, 57, 0, 77, 35885}, {21229, 29, 0, 35, 35962}}, {2533,0,-17920,3540,241,-12345}},
+ {{{21258, 75, 0, 104, 35997}, {21333, 30, 0, 35, 36101}}, {155,0,-13588,5411,241,-6073}},
+ {{{21363, 120, 0, 202, 36136}, {21483, 58, 0, 80, 36338}}, {-1242,0,-11538,4568,241,-1553}},
+ {{{21541, 170, 0, 318, 36418}, {21711, 84, 0, 147, 36736}}, {-1681,0,-3832,2887,241,2560}},
+ {{{21795, 138, 0, 236, 36883}, {21933, 70, 0, 115, 37119}}, {2735,0,-17920,6801,241,-11975}},
+ {{{22003, 85, 0, 124, 37234}, {22088, 40, 0, 52, 37358}}, {1731,0,-13854,6362,241,-4279}},
+ {{{22128, 22, 0, 23, 37410}, {22150, 14, 0, 10, 37433}}, {2846,0,-9452,5056,241,-3832}},
+ {{{22164, 110, 0, 194, 37443}, {22274, 45, 0, 72, 37637}}, {-1622,528,-12800,1568,2192,-6912}},
+ {{{22319, 138, 0, 228, 37709}, {22457, 52, 0, 72, 37937}}, {-1679,673,-9472,1678,2128,-1536}},
+ {{{22509, 113, 0, 194, 38009}, {22622, 48, 0, 72, 38203}}, {-1682,1083,-3840,1681,1658,2560}},
+ {{{22670, 110, 0, 194, 38275}, {22780, 45, 0, 72, 38469}}, {-1568,528,-12800,1622,2192,-6912}},
+ {{{22825, 138, 0, 228, 38541}, {22963, 52, 0, 72, 38769}}, {-1678,673,-9472,1679,2128,-1536}},
+ {{{23015, 113, 0, 194, 38841}, {23128, 48, 0, 72, 39035}}, {-1681,1083,-3840,1682,1658,2560}},
+ {{{23176, 16, 0, 28, 39107}, {23192, 16, 0, 28, 39135}}, {-384,0,-384,384,1472,384}},
+ {{{23208, 24, 0, 46, 39163}, {23232, 24, 0, 46, 39209}}, {-384,0,-384,384,1472,384}},
+ {{{23256, 6, 0, 8, 39255}, {23262, 6, 0, 8, 39263}}, {-2560,0,-2560,2560,2614,2560}},
+ {{{23268, 6, 0, 8, 39271}, {23274, 6, 0, 8, 39279}}, {-2560,0,-2560,2560,2614,2560}},
+ {{{23280, 6, 0, 8, 39287}, {23286, 6, 0, 8, 39295}}, {-2560,0,-2560,2560,2614,2560}},
+ {{{23292, 4, 0, 4, 39303}, {23296, 4, 0, 4, 39307}}, {-2560,0,-2560,2560,2560,2560}},
+ {{{23300, 4, 0, 4, 39311}, {23304, 4, 0, 4, 39315}}, {-2560,0,-2560,2560,2560,2560}},
+ {{{23308, 5, 0, 6, 39319}, {23313, 5, 0, 6, 39325}}, {-2560,0,-2560,2560,2560,2560}},
+ {{{23318, 5, 0, 6, 39331}, {23323, 5, 0, 6, 39337}}, {-2560,0,-2560,2560,2560,2560}},
+ {{{23328, 128, 0, 252, 39343}, {23456, 60, 0, 116, 39595}}, {2304,0,-2560,2560,2560,2560}},
+ {{{23516, 128, 0, 252, 39711}, {23644, 60, 0, 116, 39963}}, {-2560,0,-2560,-2304,2560,2560}},
+ {{{23704, 128, 0, 252, 40079}, {23832, 60, 0, 116, 40331}}, {2304,0,-2560,2560,2560,2560}},
+ {{{23892, 128, 0, 252, 40447}, {24020, 60, 0, 116, 40699}}, {-2560,0,-2560,-2304,2560,2560}},
+ {{{24080, 68, 0, 126, 40815}, {24148, 32, 0, 54, 40941}}, {2304,410,-7680,2560,3840,-1536}},
+ {{{24180, 68, 0, 126, 40995}, {24248, 34, 0, 58, 41121}}, {2304,0,-3584,2560,2202,2560}},
+ {{{24282, 36, 0, 68, 41179}, {24318, 17, 0, 30, 41247}}, {-2560,0,-2560,2560,1280,2560}},
+};
+const pt_part pt_parts[PT_NPARTS] = {
+ {0, 68, 0, 1, 0, 44, 0, 3, 4, {-1681, 0, -2560}, {1681, 241, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* Straight */
+ {68, 276, 1, 1, 44, 460, 12, 3, 4, {-1681, 0, -2560}, {1681, 1521, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* SlopeUp */
+ {344, 276, 2, 1, 504, 460, 24, 3, 4, {-1681, 0, -2560}, {1681, 1521, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* SlopeDown */
+ {620, 68, 3, 1, 964, 48, 36, 3, 4, {-1681, 0, -2560}, {1681, 2801, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* Slope */
+ {688, 78, 4, 1, 1012, 44, 48, 3, 4, {-1681, 0, -2560}, {1681, 241, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.35f, 1.35f}}, /* Start */
+ {766, 169, 5, 1, 1056, 76, 60, 3, 4, {-1681, 0, -2560}, {1681, 1258, 2560}, 0, 2, 0, {0.0f, 2.2f, 0.0f}, {10.5f, 3.8f, 2.0f}, {0.0f, 0.0f, 0.0f}}, /* Finish */
+ {935, 68, 6, 1, 1132, 100, 72, 4, 4, {-2560, 0, -2560}, {2560, 241, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* ToWideMiddle */
+ {1003, 68, 7, 1, 1232, 72, 88, 4, 4, {-1681, 0, -2560}, {2560, 241, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* ToWideLeft */
+ {1071, 68, 8, 1, 1304, 72, 104, 4, 4, {-2560, 0, -2560}, {1681, 241, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* ToWideRight */
+ {1139, 38, 9, 1, 1376, 28, 120, 4, 4, {-2560, 0, -2560}, {1681, 241, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* StraightWide */
+ {1177, 38, 10, 1, 1404, 56, 136, 4, 4, {-2560, 0, -2560}, {2560, 241, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* InnerCornerWide */
+ {1215, 58, 11, 1, 1460, 48, 152, 4, 4, {-1681, 0, -1681}, {2560, 241, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* OuterCornerWide */
+ {1273, 180, 12, 1, 1508, 310, 168, 4, 4, {-1681, 0, -2560}, {2560, 1521, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* SlopeUpLeftWide */
+ {1453, 180, 13, 1, 1818, 310, 184, 4, 4, {-2560, 0, -2560}, {1681, 1521, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* SlopeUpRightWide */
+ {1633, 180, 14, 1, 2128, 312, 200, 4, 4, {-1681, 0, -2560}, {2560, 1521, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* SlopeDownLeftWide */
+ {1813, 180, 15, 1, 2440, 312, 216, 4, 4, {-2560, 0, -2560}, {1681, 1521, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* SlopeDownRightWide */
+ {1993, 38, 16, 1, 2752, 29, 232, 4, 4, {-1681, 0, -2560}, {2560, 2801, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* SlopeLeftWide */
+ {2031, 38, 17, 1, 2781, 29, 248, 4, 4, {-2560, 0, -2560}, {1681, 2801, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* SlopeRightWide */
+ {2069, 16, 18, 1, 2810, 28, 264, 1, 1, {-384, 0, -384}, {384, 1280, 384}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PillarTop */
+ {2085, 8, 19, 1, 2838, 12, 265, 1, 1, {-256, 0, -256}, {256, 1280, 256}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PillarMiddle */
+ {2093, 16, 20, 1, 2850, 28, 266, 1, 1, {-384, 0, -384}, {384, 1280, 384}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PillarBottom */
+ {2109, 24, 21, 1, 2878, 44, 267, 1, 1, {-384, 0, -384}, {384, 1280, 384}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PillarShort */
+ {2133, 24, 22, 1, 2922, 38, 268, 4, 4, {-2560, 0, -2560}, {2560, 1280, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PlanePillarBottom */
+ {2157, 32, 23, 1, 2960, 54, 284, 4, 4, {-2560, 0, -2560}, {2560, 1280, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PlanePillarShort */
+ {2189, 8, 24, 1, 3014, 12, 300, 4, 4, {-2560, 0, -2560}, {2560, 54, 2560}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* Plane */
+ {2197, 24, 25, 1, 3026, 44, 316, 4, 1, {-2560, 0, 2304}, {2560, 1280, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PlaneWall */
+ {2221, 40, 26, 1, 3070, 76, 320, 4, 4, {-2560, 0, -2560}, {2560, 1280, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PlaneWallCorner */
+ {2261, 16, 27, 1, 3146, 28, 336, 1, 1, {2304, 0, 2304}, {2560, 1280, 2560}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PlaneWallInnerCorner */
+ {2277, 8, 28, 1, 3174, 12, 337, 4, 4, {-2560, 0, -2560}, {2560, 1280, 2560}, 1, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* Block */
+ {2285, 170, 29, 1, 3186, 260, 353, 5, 5, {-2560, 0, -2560}, {2560, 5120, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* WallTrackTop */
+ {2455, 12, 30, 1, 3446, 20, 378, 4, 1, {-2560, 0, -2560}, {2560, 1280, -2506}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* WallTrackMiddle */
+ {2467, 170, 31, 1, 3466, 260, 382, 5, 5, {-2560, 0, -2560}, {2560, 5120, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* WallTrackBottom */
+ {2637, 84, 32, 1, 3726, 164, 407, 4, 4, {-2560, 0, -2560}, {2560, 1334, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PlaneSlopeUp */
+ {2721, 84, 33, 1, 3890, 164, 423, 4, 4, {-2560, 0, -2560}, {2560, 1334, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PlaneSlopeDown */
+ {2805, 8, 34, 1, 4054, 12, 439, 4, 4, {-2560, 0, -2560}, {2560, 2614, 2560}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PlaneSlope */
+ {2813, 663, 35, 3, 4066, 1126, 455, 8, 8, {-1681, 0, -6801}, {7680, 241, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* TurnShort */
+ {3476, 1053, 38, 6, 5192, 1768, 519, 12, 12, {-1681, 0, -11921}, {12800, 241, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* TurnLong */
+ {4529, 382, 44, 2, 6960, 594, 663, 3, 8, {-1681, 0, -7680}, {1681, 2801, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* SlopeUpLong */
+ {4911, 384, 46, 2, 7554, 584, 687, 3, 8, {-1681, 0, -2560}, {1681, 2801, 7680}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* SlopeDownLong */
+ {5295, 797, 48, 5, 8138, 1278, 711, 7, 13, {-6801, 0, -12800}, {1681, 241, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* TurnSLeft */
+ {6092, 797, 53, 5, 9416, 1278, 802, 7, 13, {-1681, 0, -12800}, {6801, 241, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* TurnSRight */
+ {6889, 128, 58, 1, 10694, 188, 893, 4, 4, {-2560, 0, -2560}, {2560, 241, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* IntersectionCross */
+ {7017, 16, 59, 1, 10882, 28, 909, 1, 3, {-256, 0, -2560}, {256, 1280, 256}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PillarBranch1 */
+ {7033, 22, 60, 1, 10910, 40, 912, 3, 3, {-256, 0, -2560}, {2560, 1280, 256}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PillarBranch2 */
+ {7055, 32, 61, 1, 10950, 60, 921, 4, 4, {-2560, 0, -2560}, {2560, 1280, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PillarBranch4 */
+ {7087, 68, 62, 1, 11010, 132, 937, 4, 4, {-2560, 0, -2560}, {2560, 1280, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* WallTrackMiddleCorner */
+ {7155, 1018, 63, 3, 11142, 2022, 953, 5, 5, {-2560, 0, -2560}, {2560, 5120, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* WallTrackTopCorner */
+ {8173, 96, 66, 1, 13164, 76, 978, 3, 4, {-1681, 0, -2560}, {1681, 1258, 2560}, 0, 1, 0, {0.0f, 2.2f, 0.0f}, {10.5f, 3.8f, 1.0f}, {0.0f, 0.0f, 0.0f}}, /* Checkpoint */
+ {8269, 6, 67, 1, 13240, 8, 990, 4, 4, {-2560, 0, -2560}, {2560, 1280, 2560}, 1, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* HalfBlock */
+ {8275, 6, 68, 1, 13248, 8, 1006, 2, 4, {-2560, 0, -2560}, {0, 1280, 2560}, 1, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* QuarterBlock */
+ {8281, 6, 69, 1, 13256, 8, 1014, 4, 4, {-2560, 0, -2560}, {2560, 54, 2560}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* HalfPlane */
+ {8287, 6, 70, 1, 13264, 8, 1030, 2, 4, {-2560, 0, -2560}, {0, 54, 2560}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* QuarterPlane */
+ {8293, 66, 71, 1, 13272, 12, 1038, 5, 1, {-2560, 0, -2560}, {2560, 1280, -2506}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* SignArrowLeft */
+ {8359, 66, 72, 1, 13284, 12, 1043, 5, 1, {-2560, 0, -2560}, {2560, 1280, -2506}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* SignArrowRight */
+ {8425, 48, 73, 1, 13296, 12, 1048, 5, 1, {-2560, 0, -2560}, {2560, 1280, -2506}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* SignArrowUp */
+ {8473, 48, 74, 1, 13308, 12, 1053, 5, 1, {-2560, 0, -2560}, {2560, 1280, -2506}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* SignArrowDown */
+ {8521, 54, 75, 1, 13320, 12, 1058, 5, 1, {-2560, 0, -2560}, {2560, 1280, -2506}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* SignWarning */
+ {8575, 48, 76, 1, 13332, 12, 1063, 5, 1, {-2560, 0, -2560}, {2560, 1280, -2506}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* SignWrongWay */
+ {8623, 96, 77, 2, 13344, 76, 1068, 7, 4, {-1681, 0, -2560}, {6801, 1258, 2560}, 0, 1, 0, {10.0f, 2.2f, 0.0f}, {30.6f, 3.8f, 1.0f}, {0.0f, 0.0f, 0.0f}}, /* CheckpointWide */
+ {8719, 12, 79, 1, 13420, 20, 1096, 4, 4, {-2560, 1226, -2560}, {2560, 1280, 2560}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* WallTrackCeiling */
+ {8731, 12, 80, 1, 13440, 20, 1112, 4, 4, {-2560, 0, -2560}, {2560, 54, 2560}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* WallTrackFloor */
+ {8743, 6, 81, 1, 13460, 8, 1128, 4, 4, {-2560, 0, -2560}, {2560, 2560, 2560}, 1, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* BlockSlopedDown */
+ {8749, 7, 82, 1, 13468, 10, 1144, 4, 4, {-2560, 0, -2560}, {2560, 2560, 2560}, 1, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* BlockSlopedDownInnerCorner */
+ {8756, 5, 83, 1, 13478, 6, 1160, 4, 4, {-2560, 0, -2560}, {2560, 2560, 2560}, 1, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* BlockSlopedDownOuterCorner */
+ {8761, 6, 84, 1, 13484, 8, 1176, 4, 4, {-2560, 0, -2560}, {2560, 2560, 2560}, 1, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* BlockSlopedUp */
+ {8767, 7, 85, 1, 13492, 10, 1192, 4, 4, {-2560, 0, -2560}, {2560, 2560, 2560}, 1, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* BlockSlopedUpInnerCorner */
+ {8774, 5, 86, 1, 13502, 6, 1208, 4, 4, {-2560, 0, -2560}, {2560, 2560, 2560}, 1, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* BlockSlopedUpOuterCorner */
+ {8779, 36, 87, 1, 13508, 44, 1224, 4, 4, {-2560, 0, -2560}, {2560, 1258, 2560}, 0, 1, 0, {0.0f, 2.2f, 0.0f}, {18.25f, 3.8f, 1.0f}, {0.0f, 0.0f, 0.0f}}, /* PlaneCheckpoint */
+ {8815, 167, 88, 1, 13552, 44, 1240, 4, 4, {-2560, 0, -2560}, {2560, 1258, 2560}, 0, 2, 0, {0.0f, 2.2f, 0.0f}, {18.25f, 3.8f, 2.0f}, {0.0f, 0.0f, 0.0f}}, /* PlaneFinish */
+ {8982, 36, 89, 2, 13596, 44, 1256, 8, 4, {-2560, 0, -2560}, {7680, 1258, 2560}, 0, 1, 0, {10.0f, 2.2f, 0.0f}, {38.25f, 3.8f, 1.0f}, {0.0f, 0.0f, 0.0f}}, /* PlaneCheckpointWide */
+ {9018, 293, 91, 2, 13640, 44, 1288, 8, 4, {-2560, 0, -2560}, {7680, 1258, 2560}, 0, 2, 0, {10.0f, 2.2f, 0.0f}, {38.25f, 3.8f, 2.0f}, {0.0f, 0.0f, 0.0f}}, /* PlaneFinishWide */
+ {9311, 1469, 93, 13, 13684, 2112, 1320, 20, 20, {-1681, 0, -22161}, {23040, 241, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* TurnLong3 */
+ {10780, 43, 106, 1, 15796, 82, 1720, 4, 4, {-2560, 0, -2560}, {2560, 1280, 2560}, 0, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* BlockSlopeUp */
+ {10823, 44, 107, 1, 15878, 84, 1736, 4, 4, {-2560, 0, -2560}, {2560, 1280, 2560}, 0, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* BlockSlopeDown */
+ {10867, 66, 108, 1, 15962, 128, 1752, 4, 4, {-2560, 0, -2560}, {2560, 5120, 2560}, 0, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* BlockSlopeVerticalTop */
+ {10933, 66, 109, 1, 16090, 128, 1768, 4, 4, {-2560, 0, -2560}, {2560, 5120, 2560}, 0, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* BlockSlopeVerticalBottom */
+ {10999, 20, 110, 1, 16218, 12, 1784, 4, 4, {-2560, 0, -2560}, {2560, 54, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.35f, 1.35f}}, /* PlaneStart */
+ {11019, 20, 111, 2, 16230, 12, 1800, 8, 4, {-2560, 0, -2560}, {7680, 54, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {-10.0f, 0.35f, 1.35f}}, /* PlaneStartWide */
+ {11039, 313, 113, 6, 16242, 510, 1832, 12, 12, {-1681, 0, -11921}, {12800, 241, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* TurnShortLeftWide */
+ {11352, 362, 119, 7, 16752, 552, 1976, 16, 16, {-1681, 0, -17041}, {17920, 241, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* TurnLongLeftWide */
+ {11714, 330, 126, 7, 17304, 472, 2232, 12, 13, {-2560, 0, -12800}, {12800, 241, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* TurnLongRightWide */
+ {12044, 432, 133, 1, 17776, 724, 2388, 3, 5, {-1681, 0, -2560}, {1681, 5120, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* SlopeUpVertical */
+ {12476, 12, 134, 1, 18500, 20, 2403, 1, 3, {-256, 0, -2560}, {256, 896, 256}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PillarBranch1Top */
+ {12488, 12, 135, 1, 18520, 20, 2406, 1, 3, {-256, 384, -2560}, {256, 1280, 256}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PillarBranch1Bottom */
+ {12500, 17, 136, 1, 18540, 30, 2409, 3, 3, {-256, 0, -2560}, {2560, 896, 256}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PillarBranch2Top */
+ {12517, 17, 137, 1, 18570, 30, 2418, 3, 3, {-256, 384, -2560}, {2560, 1280, 256}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PillarBranch2Bottom */
+ {12534, 22, 138, 1, 18600, 40, 2427, 4, 3, {-2560, 0, -2560}, {2560, 896, 256}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PillarBranch3Top */
+ {12556, 16, 139, 1, 18640, 28, 2439, 4, 3, {-2560, 384, -2560}, {2560, 896, 256}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PillarBranch3Middle */
+ {12572, 24, 140, 1, 18668, 44, 2451, 4, 4, {-2560, 384, -2560}, {2560, 896, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PillarBranch4Middle */
+ {12596, 24, 141, 1, 18712, 44, 2467, 4, 1, {-2560, 0, -256}, {2560, 1280, 256}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PillarBranch5 */
+ {12620, 16, 142, 1, 18756, 28, 2471, 4, 1, {-2560, 0, -256}, {2560, 896, 256}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PillarBranch5Top */
+ {12636, 8, 143, 1, 18784, 12, 2475, 4, 1, {-2560, 384, -256}, {2560, 896, 256}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PillarBranch5Middle */
+ {12644, 16, 144, 1, 18796, 28, 2479, 4, 1, {-2560, 384, -256}, {2560, 1280, 256}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PillarBranch5Bottom */
+ {12660, 84, 145, 1, 18824, 70, 2483, 3, 4, {-1681, 0, -2560}, {1681, 1280, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* StraightPillarBottom */
+ {12744, 8, 146, 1, 18894, 12, 2495, 1, 4, {-251, 0, -2560}, {251, 1280, 2560}, 1, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* BlockBridge */
+ {12752, 120, 147, 1, 18906, 158, 2499, 4, 5, {-2560, 0, -2560}, {2560, 5120, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* WallTrackSlopeToVertical */
+ {12872, 80, 148, 1, 19064, 156, 2519, 4, 5, {-2560, 0, -2560}, {2560, 5120, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PlaneSlopeToVertical */
+ {12952, 42, 149, 1, 19220, 80, 2539, 4, 5, {-2560, 0, -2560}, {2560, 5120, 2560}, 0, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* BlockSlopeToVertical */
+ {12994, 91, 150, 2, 19300, 178, 2559, 5, 8, {-2560, 0, -7680}, {2560, 2614, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PlaneSlopeUpLong */
+ {13085, 92, 152, 2, 19478, 180, 2599, 5, 8, {-2560, 0, -2560}, {2560, 2614, 7680}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PlaneSlopeDownLong */
+ {13177, 260, 154, 2, 19658, 388, 2639, 4, 8, {-2560, 0, -7680}, {1681, 2801, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* SlopeUpLongLeftWide */
+ {13437, 260, 156, 2, 20046, 388, 2671, 4, 8, {-1681, 0, -7680}, {2560, 2801, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* SlopeUpLongRightWide */
+ {13697, 259, 158, 2, 20434, 392, 2703, 4, 8, {-2560, 0, -2560}, {1681, 2801, 7680}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* SlopeDownLongLeftWide */
+ {13956, 259, 160, 2, 20826, 392, 2735, 4, 8, {-1681, 0, -2560}, {2560, 2801, 7680}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* SlopeDownLongRightWide */
+ {14215, 44, 162, 2, 21218, 84, 2767, 5, 8, {-2560, 0, -7680}, {2560, 2560, 2560}, 0, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* BlockSlopeUpLong */
+ {14259, 45, 164, 2, 21302, 86, 2807, 5, 8, {-2560, 0, -2560}, {2560, 2560, 7680}, 0, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* BlockSlopeDownLong */
+ {14304, 36, 166, 1, 21388, 68, 2847, 4, 4, {-2560, 0, -2560}, {2560, 1280, 2560}, 0, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* BlockInnerCorner */
+ {14340, 282, 167, 1, 21456, 458, 2863, 3, 5, {-1681, 0, -2560}, {1681, 5120, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* SlopeToVertical */
+ {14622, 68, 168, 1, 21914, 46, 2878, 3, 4, {-1504, 528, -2560}, {1564, 2192, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* StraightTilted */
+ {14690, 1440, 169, 11, 21960, 2032, 2890, 20, 20, {-1504, 528, -21984}, {23040, 2192, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* TurnLong3Tilted */
+ {16130, 753, 180, 7, 23992, 1072, 3290, 7, 17, {-6801, 0, -17920}, {1681, 241, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* TurnSLongLeft */
+ {16883, 753, 187, 7, 25064, 1072, 3409, 7, 17, {-1681, 0, -17920}, {6801, 241, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* TurnSLongRight */
+ {17636, 925, 194, 3, 26136, 1648, 3528, 3, 13, {-1682, 528, -12800}, {1681, 2192, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* ToTiltedLeft */
+ {18561, 925, 197, 3, 27784, 1648, 3567, 3, 13, {-1681, 528, -12800}, {1682, 2192, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* ToTiltedRight */
+ {19486, 16, 200, 1, 29432, 28, 3606, 1, 1, {-384, 0, -384}, {384, 1472, 384}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PillarTopSlope */
+ {19502, 24, 201, 1, 29460, 46, 3607, 1, 1, {-384, 0, -384}, {384, 1472, 384}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PillarShortSlope */
+ {19526, 6, 202, 1, 29506, 8, 3608, 4, 4, {-2560, 0, -2560}, {2560, 2614, 2560}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* HalfPlaneSlopeBottomLeft */
+ {19532, 6, 203, 1, 29514, 8, 3624, 4, 4, {-2560, 0, -2560}, {2560, 2614, 2560}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* HalfPlaneSlopeBottomRight */
+ {19538, 6, 204, 1, 29522, 8, 3640, 4, 4, {-2560, 0, -2560}, {2560, 2614, 2560}, 1, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* HalfPlaneSlopeTopRight */
+ {19544, 4, 205, 1, 29530, 4, 3656, 4, 4, {-2560, 0, -2560}, {2560, 2560, 2560}, 1, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* HalfBlockSlopeBottomLeft */
+ {19548, 4, 206, 1, 29534, 4, 3672, 4, 4, {-2560, 0, -2560}, {2560, 2560, 2560}, 1, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* HalfBlockSlopeBottomRight */
+ {19552, 5, 207, 1, 29538, 6, 3688, 4, 4, {-2560, 0, -2560}, {2560, 2560, 2560}, 1, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* HalfBlockSlopeTopLeft */
+ {19557, 5, 208, 1, 29544, 6, 3704, 4, 4, {-2560, 0, -2560}, {2560, 2560, 2560}, 1, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* HalfBlockSlopeTopRight */
+ {19562, 168, 209, 1, 29550, 332, 3720, 1, 4, {2304, 0, -2560}, {2560, 2560, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PlaneWallSlopeUpLeft */
+ {19730, 168, 210, 1, 29882, 332, 3724, 1, 4, {-2560, 0, -2560}, {-2304, 2560, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PlaneWallSlopeUpRight */
+ {19898, 168, 211, 1, 30214, 332, 3728, 1, 4, {2304, 0, -2560}, {2560, 2560, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PlaneWallSlopeDownLeft */
+ {20066, 168, 212, 1, 30546, 332, 3732, 1, 4, {-2560, 0, -2560}, {-2304, 2560, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PlaneWallSlopeDownRight */
+ {20234, 172, 213, 2, 30878, 332, 3736, 1, 9, {2304, 0, -7680}, {2560, 3840, 2560}, 0, 0, 0, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* PlaneWallSlopeUpLongLeft */
+ {20406, 36, 215, 1, 31210, 68, 3745, 4, 4, {-2560, 0, -2560}, {2560, 1280, 2560}, 0, 0, 1, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}, /* BlockOuterCorner */
+};
+const uint8_t pt_part_id[129] = {
+ 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+ 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
+ 33, 34, 35, 36, 37, 38, 39, 41, 42, 44, 45, 46, 48, 50, 51, 52,
+ 53, 54, 55, 56, 58, 59, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
+ 71, 72, 73, 75, 76, 77, 78, 83, 85, 86, 87, 88, 92, 93, 94, 96,
+ 97, 98, 101, 102, 104, 106, 107, 108, 111, 113, 114, 115, 116, 119, 130, 142,
+ 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 155, 156, 159, 163, 164, 165,
+ 166, 167, 168, 169, 170, 171, 173, 174, 175, 176, 177, 180, 181, 182, 183, 184,
+ 188,
+};
+const int8_t pt_orient[216] = {
+ 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, -1, 0, 0,
+ -1, 0, 0, 0, 1, 0, 0, 0, -1, 0, 0, -1, 0, 1, 0, 1, 0, 0,
+ -1, 0, 0, 0, -1, 0, 0, 0, 1, 0, 0, 1, 0, -1, 0, 1, 0, 0,
+ 1, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, -1, 0, -1, 0, -1, 0, 0,
+ 0, 1, 0, -1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, -1, -1, 0, 0,
+ 0, 1, 0, 1, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0, 1, 1, 0, 0,
+ 0, -1, 0, 1, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, -1, 1, 0, 0,
+ 0, -1, 0, -1, 0, 0, 0, 0, -1, 0, -1, 0, 0, 0, 1, -1, 0, 0,
+ 1, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0,
+ -1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, -1, -1, 0, 0, 0, 1, 0,
+ 1, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, -1, 1, 0, 0, 0, -1, 0,
+ -1, 0, 0, 0, 0, -1, 0, -1, 0, 0, 0, 1, -1, 0, 0, 0, -1, 0,
+};
+const pt_track pt_tracks[PT_NTRACKS] = {
+ {"Summer 1", 0, 28, 0, 679, 0, 0, -9, -4, 18, 12, 30, 0, 3, 1, 3, 416, -64, 64, -28, 56, 0, 33, 22},
+ {"Summer 2", 0, 28, 679, 1080, 57, 217, -1, -8, 9, 9, 29, 4, 2, 1, 2, 1072, -8, 60, -64, 4, 363, 18, 18},
+ {"Summer 3", 0, 28, 1759, 1408, 127, 299, -30, -10, 32, 11, 10, 7, 5, 1, 3, 1395, -236, 8, -76, 0, 525, 62, 20},
+ {"Summer 4", 0, 28, 3167, 3360, 205, 652, -9, -18, 27, 19, 39, 13, 4, 1, 4, 3349, -72, 136, -140, 0, 1145, 53, 36},
+ {"Summer 5", 0, 28, 6527, 2872, 286, 1166, -18, -5, 30, 20, 30, 18, 8, 1, 8, 56, -144, 88, -40, 116, 2099, 59, 40},
+ {"Summer 6", 0, 28, 9399, 1116, 377, 1767, -18, -12, 24, 16, 40, 27, 10, 1, 9, 467, -140, 40, -92, 28, 3279, 46, 31},
+ {"Summer 7", 0, 28, 10515, 2054, 463, 2152, -1, -9, 5, 11, 30, 38, 5, 1, 4, 1644, -8, 24, -68, 12, 3992, 9, 21},
+ {"Winter 1", 1, 28, 12569, 670, 577, 2208, -1, -13, 12, 14, 40, 44, 2, 1, 2, 666, -8, 80, -104, 4, 4087, 23, 28},
+ {"Winter 2", 1, 28, 13239, 3316, 635, 2377, -13, -34, 19, 42, 30, 47, 2, 1, 2, 3240, -104, 44, -272, 56, 4409, 38, 83},
+ {"Winter 3", 1, 28, 16555, 2495, 719, 3176, -9, -17, 14, 22, 30, 50, 5, 2, 5, 2488, -68, 32, -136, 32, 5986, 26, 43},
+ {"Winter 4", 1, 28, 19050, 1338, 799, 3485, -5, -9, 15, 16, 49, 57, 4, 1, 4, 890, -36, 72, -72, 52, 6545, 28, 32},
+ {"Winter 5", 1, 76, 20388, 2622, 875, 3726, -12, -9, 19, 8, 48, 62, 2, 1, 2, 0, -96, 48, -68, -12, 6993, 37, 15},
+ {"Desert 1", 2, 28, 23010, 1988, 979, 3879, -12, -4, 15, 11, 29, 65, 3, 1, 3, 943, -92, 16, -28, 48, 7271, 28, 20},
+ {"Desert 2", 2, 28, 24998, 2155, 1057, 4045, -2, -11, 18, 17, 49, 69, 4, 1, 4, 943, -12, 120, -84, 40, 7551, 34, 32},
+ {"Desert 3", 2, 127, 27153, 2824, 1150, 4352, -6, -25, 7, 32, 20, 74, 2, 1, 1, 2236, -48, 0, -196, 52, 8095, 13, 63},
+ {"Desert 4", 2, 28, 29977, 2147, 1249, 4577, -27, -6, 36, 23, 39, 77, 3, 1, 3, 1151, -212, 68, -44, 128, 8505, 71, 44},
+ {"Desert 5", 2, 23, 32124, 2954, 1333, 5406, -18, -7, 28, 13, 40, 81, 5, 1, 4, 1596, -140, 76, -56, 40, 10067, 55, 25},
+};
+const uint16_t pt_envmat[189] = {
+ 1211, 1211, 1211, 3277, 132, 52, 4096, 4096, 4096, 307, 307, 307, 4096, 4096, 4096,
+ 60, 60, 60, 3277, 132, 52, 3277, 1546, 66, 3277, 2446, 0, 420, 420, 420,
+ 540, 569, 1122, 852, 852, 852, 827, 827, 827, 29, 29, 29, 403, 403, 403,
+ 300, 300, 300, 902, 722, 397, 2427, 71, 0, 26, 26, 26, 3277, 1861, 131,
+ 3686, 2114, 2074, 329, 756, 1824, 56, 191, 602, 4096, 4096, 4096, 1025, 1025, 1025,
+ 4096, 4096, 4096, 60, 60, 60, 3277, 132, 52, 3277, 1546, 66, 3277, 2446, 0,
+ 56, 191, 602, 992, 992, 992, 992, 992, 992, 99, 304, 869, 9, 29, 87,
+ 329, 756, 1824, 992, 992, 992, 4096, 4096, 4096, 945, 40, 1, 329, 756, 1824,
+ 45, 95, 1025, 2076, 2144, 2349, 1305, 689, 210, 429, 72, 72, 337, 10, 10,
+ 62, 21, 1, 4096, 4096, 4096, 60, 60, 60, 3277, 132, 52, 3277, 1546, 66,
+ 3277, 2446, 0, 511, 121, 121, 1647, 1041, 354, 1940, 1125, 429, 1305, 676, 191,
+ 48, 23, 6, 79, 14, 14, 33, 7, 7, 729, 381, 112, 2813, 2, 2,
+ 479, 50, 50, 1305, 689, 210, 383, 41, 41,
+};
+const uint16_t pt_custom[27] = {
+ 27, 27, 27, 329, 45, 45, 869, 304, 99, 1195, 976, 107, 95, 458, 121,
+ 69, 511, 511, 59, 72, 288, 409, 83, 409, 121, 69, 37,
+};
+const uint16_t pt_carmat[15] = {
+ 0, 0, 0, 315, 315, 315, 410, 0, 0, 123, 123, 123, 1638, 1638, 1638,
+};
+const int16_t pt_body_v[294] = {
+ -24, 22, -445, -24, 22, -430, -24, -3, -430, -39, -3, -430,
+ -39, 22, -430, -39, -3, -445, -39, 22, -445, -24, -3, -445,
+ -77, 25, 151, -77, -88, 151, -68, 17, 220, -26, -81, 424,
+ -43, -25, 371, -43, -88, 384, -68, -88, 220, -180, 4, 117,
+ -159, 4, -230, -180, -88, 117, -159, -88, -230, -77, -88, -498,
+ -77, -88, -308, -77, -6, -468, -77, 25, -308, -175, 0, 80,
+ -81, 20, 111, -175, 0, 119, -81, 20, 149, -81, -84, 111,
+ -81, -84, 149, -175, -84, 80, -175, -84, 119, 82, -82, 411,
+ 82, -70, 367, 82, -76, 413, 82, -64, 369, -82, -70, 367,
+ -82, -82, 411, -82, -76, 413, -82, -64, 369, 24, -3, -430,
+ 39, -3, -430, 24, 22, -430, 39, 22, -430, 39, -3, -445,
+ 39, 22, -445, 24, -3, -445, 24, 22, -445, 128, 12, -416,
+ 128, 24, -459, 128, 18, -414, 128, 31, -457, -128, 24, -459,
+ -128, 12, -416, -128, 18, -414, -128, 31, -457, 26, -81, 424,
+ 43, -88, 384, 43, -25, 371, 68, -88, 220, 68, 17, 220,
+ 77, -88, 151, 77, 25, 151, -42, 91, -160, 42, 91, -160,
+ 42, 94, -147, 73, 42, -133, 73, 39, -147, -73, 42, -133,
+ -73, 39, -147, -42, 94, -147, 43, 97, -148, 77, 38, -133,
+ -77, 38, -133, -43, 97, -148, 180, 4, 117, 180, -88, 117,
+ 159, 4, -230, 159, -88, -230, 77, 25, -308, 77, -88, -498,
+ 77, -6, -468, 77, -88, -308, 175, 0, 80, 175, -84, 80,
+ 175, -84, 119, 81, -84, 149, 81, -84, 111, 81, 20, 149,
+ 81, 20, 111, 175, 0, 119, 59, 43, -298, -59, 43, -298,
+ 64, -23, -474, -64, -23, -474, -64, -34, -478, -32, -38, -480,
+ 32, -38, -480, 64, -34, -478,
+};
+const uint8_t pt_body_t[668] = {
+ 0, 1, 2, 0, 2, 1, 3, 0, 1, 4, 3, 0, 3, 4, 5, 0,
+ 4, 6, 5, 0, 5, 6, 7, 0, 6, 0, 7, 0, 0, 2, 7, 0,
+ 8, 9, 10, 0, 11, 12, 13, 0, 13, 12, 14, 0, 12, 10, 14, 0,
+ 10, 9, 14, 0, 15, 16, 17, 0, 17, 16, 18, 0, 19, 20, 21, 0,
+ 21, 20, 22, 0, 20, 18, 22, 0, 18, 16, 22, 0, 23, 24, 25, 0,
+ 25, 24, 26, 0, 24, 27, 26, 0, 26, 27, 28, 0, 27, 29, 28, 0,
+ 28, 29, 30, 0, 29, 23, 30, 0, 23, 25, 30, 0, 30, 25, 17, 0,
+ 17, 25, 15, 0, 25, 26, 15, 0, 15, 26, 8, 0, 26, 28, 8, 0,
+ 8, 28, 9, 0, 28, 30, 9, 0, 30, 17, 9, 0, 31, 32, 33, 0,
+ 33, 32, 34, 0, 34, 32, 35, 0, 32, 31, 35, 0, 35, 31, 36, 0,
+ 36, 31, 37, 0, 31, 33, 37, 0, 33, 34, 37, 0, 37, 34, 38, 0,
+ 34, 35, 38, 0, 35, 36, 38, 0, 39, 40, 41, 0, 41, 40, 42, 0,
+ 40, 43, 42, 0, 42, 43, 44, 0, 43, 45, 44, 0, 44, 45, 46, 0,
+ 45, 39, 46, 0, 39, 41, 46, 0, 47, 48, 49, 0, 49, 48, 50, 0,
+ 50, 48, 51, 0, 48, 47, 51, 0, 51, 47, 52, 0, 52, 47, 53, 0,
+ 47, 49, 53, 0, 49, 50, 53, 0, 53, 50, 54, 0, 50, 51, 54, 0,
+ 51, 52, 54, 0, 55, 56, 57, 0, 56, 58, 57, 0, 57, 58, 59, 0,
+ 58, 60, 59, 0, 60, 61, 59, 0, 62, 63, 64, 0, 64, 63, 65, 0,
+ 63, 66, 65, 0, 65, 66, 67, 0, 66, 68, 67, 0, 68, 62, 67, 0,
+ 67, 62, 69, 0, 62, 64, 69, 0, 69, 64, 70, 0, 70, 64, 71, 0,
+ 64, 65, 71, 0, 71, 65, 72, 0, 65, 67, 72, 0, 67, 69, 72, 0,
+ 72, 69, 73, 0, 69, 70, 73, 0, 74, 75, 76, 0, 75, 77, 76, 0,
+ 76, 77, 78, 0, 79, 80, 81, 0, 80, 78, 81, 0, 78, 77, 81, 0,
+ 82, 83, 84, 0, 84, 83, 85, 0, 83, 86, 85, 0, 85, 86, 87, 0,
+ 86, 88, 87, 0, 87, 88, 89, 0, 88, 82, 89, 0, 82, 84, 89, 0,
+ 89, 84, 75, 0, 75, 84, 60, 0, 84, 85, 60, 0, 60, 85, 61, 0,
+ 85, 87, 61, 0, 61, 87, 74, 0, 87, 89, 74, 0, 89, 75, 74, 0,
+ 71, 90, 70, 0, 70, 90, 73, 1, 90, 91, 73, 1, 73, 91, 72, 0,
+ 71, 72, 8, 1, 8, 72, 15, 0, 15, 72, 16, 0, 16, 72, 22, 0,
+ 72, 91, 22, 0, 92, 93, 21, 0, 93, 94, 21, 0, 21, 94, 19, 0,
+ 94, 95, 19, 0, 19, 95, 79, 0, 95, 96, 79, 0, 96, 97, 79, 0,
+ 79, 97, 80, 0, 97, 92, 80, 0, 92, 21, 80, 0, 21, 22, 80, 1,
+ 80, 22, 78, 1, 22, 91, 78, 0, 91, 90, 78, 0, 90, 71, 78, 0,
+ 78, 71, 76, 0, 76, 71, 74, 0, 74, 71, 61, 0, 71, 8, 61, 1,
+ 13, 56, 11, 0, 56, 55, 11, 0, 55, 57, 11, 1, 11, 57, 12, 1,
+ 57, 59, 12, 1, 12, 59, 10, 1, 59, 61, 10, 1, 61, 8, 10, 1,
+ 53, 54, 52, 0, 37, 38, 36, 0, 29, 27, 23, 2, 27, 24, 23, 2,
+ 62, 68, 63, 2, 68, 66, 63, 2, 88, 86, 82, 2, 86, 83, 82, 2,
+ 77, 75, 81, 3, 75, 60, 81, 3, 79, 81, 19, 3, 81, 60, 19, 3,
+ 18, 20, 17, 3, 17, 20, 9, 3, 19, 60, 9, 3, 13, 14, 56, 3,
+ 56, 14, 58, 3, 14, 9, 58, 3, 9, 60, 58, 3, 93, 92, 95, 4,
+ 92, 97, 96, 4, 92, 96, 95, 4, 93, 95, 94, 4,
+};
+const int16_t pt_wheel_v[126] = {
+ 44, 81, 26, -44, 81, 26, 44, 50, 68, -44, 50, 68,
+ 44, 0, 85, -44, 0, 85, 44, -50, 68, -44, -50, 68,
+ 44, -81, 26, -44, -81, 26, 44, -81, -26, -44, -81, -26,
+ 44, -50, -68, -44, -50, -68, 44, 0, -85, -44, 0, -85,
+ 44, 50, -68, -44, 50, -68, 44, 81, -26, -44, 81, -26,
+ 44, 68, 22, 44, 42, 58, 44, 0, 72, 44, -42, 58,
+ 44, -68, 22, 44, -68, -22, 44, -42, -58, 44, 0, -72,
+ 44, 42, -58, 44, 68, -22, 44, 0, 0, -44, 68, 22,
+ -44, 42, 58, -44, 0, 72, -44, -42, 58, -44, -68, 22,
+ -44, -68, -22, -44, -42, -58, -44, 0, -72, -44, 42, -58,
+ -44, 68, -22, -44, 0, 0,
+};
+const uint8_t pt_wheel_t[320] = {
+ 0, 1, 2, 5, 2, 1, 3, 5, 2, 3, 4, 5, 4, 3, 5, 5,
+ 4, 5, 6, 5, 6, 5, 7, 5, 6, 7, 8, 5, 8, 7, 9, 5,
+ 8, 9, 10, 5, 10, 9, 11, 5, 10, 11, 12, 5, 12, 11, 13, 5,
+ 12, 13, 14, 5, 14, 13, 15, 5, 14, 15, 16, 5, 16, 15, 17, 5,
+ 16, 17, 18, 5, 18, 17, 19, 5, 18, 19, 0, 5, 0, 19, 1, 5,
+ 0, 2, 20, 5, 20, 2, 21, 5, 2, 4, 21, 5, 21, 4, 22, 5,
+ 4, 6, 22, 5, 22, 6, 23, 5, 6, 8, 23, 5, 23, 8, 24, 5,
+ 8, 10, 24, 5, 24, 10, 25, 5, 10, 12, 25, 5, 25, 12, 26, 5,
+ 12, 14, 26, 5, 26, 14, 27, 5, 14, 16, 27, 5, 27, 16, 28, 5,
+ 16, 18, 28, 5, 28, 18, 29, 5, 18, 0, 29, 5, 29, 0, 20, 5,
+ 30, 20, 21, 6, 30, 21, 22, 3, 30, 22, 23, 6, 30, 23, 24, 3,
+ 30, 24, 25, 6, 30, 25, 26, 3, 30, 26, 27, 6, 30, 27, 28, 3,
+ 30, 28, 29, 6, 30, 29, 20, 3, 1, 31, 3, 5, 31, 32, 3, 5,
+ 3, 32, 5, 5, 32, 33, 5, 5, 5, 33, 7, 5, 33, 34, 7, 5,
+ 7, 34, 9, 5, 34, 35, 9, 5, 9, 35, 11, 5, 35, 36, 11, 5,
+ 11, 36, 13, 5, 36, 37, 13, 5, 13, 37, 15, 5, 37, 38, 15, 5,
+ 15, 38, 17, 5, 38, 39, 17, 5, 17, 39, 19, 5, 39, 40, 19, 5,
+ 19, 40, 1, 5, 40, 31, 1, 5, 41, 32, 31, 6, 41, 33, 32, 3,
+ 41, 34, 33, 6, 41, 35, 34, 3, 41, 36, 35, 6, 41, 37, 36, 3,
+ 41, 38, 37, 6, 41, 39, 38, 3, 41, 40, 39, 6, 41, 31, 40, 3,
+};
+const int16_t pt_extra_v[252] = {
+ 140, -79, 337, 0, -74, 337, 0, -74, 353, 140, -79, 353,
+ 0, -90, 337, 140, -94, 337, 140, -94, 353, 0, -90, 353,
+ 140, -53, 337, 0, -48, 337, 0, -48, 353, 140, -53, 353,
+ 0, -64, 337, 140, -68, 337, 140, -68, 353, 0, -64, 353,
+ 150, -79, -399, 0, -74, -399, 0, -74, -383, 150, -79, -383,
+ 0, -90, -399, 150, -94, -399, 150, -94, -383, 0, -90, -383,
+ 150, -53, -399, 0, -48, -399, 0, -48, -383, 150, -53, -383,
+ 0, -64, -399, 150, -68, -399, 150, -68, -383, 0, -64, -383,
+ -140, -79, 337, -140, -79, 353, -140, -94, 353, -140, -94, 337,
+ -140, -53, 337, -140, -53, 353, -140, -68, 353, -140, -68, 337,
+ -150, -79, -399, -150, -79, -383, -150, -94, -383, -150, -94, -399,
+ -150, -53, -399, -150, -53, -383, -150, -68, -383, -150, -68, -399,
+ -14, -90, -505, -14, -103, -505, -27, -90, -505, -27, -103, -505,
+ 27, -103, -505, 14, -103, -505, 27, -90, -505, 14, -90, -505,
+ -27, -103, -514, -29, -105, -514, -14, -103, -514, -14, -90, -514,
+ -27, -90, -514, -29, -88, -514, -12, -88, -471, -29, -88, -471,
+ -29, -105, -471, -29, -87, -432, -12, -87, -432, -12, -105, -471,
+ -12, -105, -514, -12, -88, -514, 14, -90, -514, 12, -105, -514,
+ 14, -103, -514, 27, -103, -514, 27, -90, -514, 12, -88, -514,
+ 12, -105, -471, 29, -105, -471, 29, -105, -514, 29, -88, -514,
+ 12, -88, -471, 12, -87, -432, 29, -87, -432, 29, -88, -471,
+};
+const uint8_t pt_extra_t[528] = {
+ 0, 1, 2, 3, 0, 2, 3, 3, 4, 5, 6, 3, 4, 6, 7, 3,
+ 6, 3, 2, 3, 6, 2, 7, 3, 4, 1, 0, 3, 4, 0, 5, 3,
+ 7, 2, 1, 3, 7, 1, 4, 3, 8, 9, 10, 3, 8, 10, 11, 3,
+ 12, 13, 14, 3, 12, 14, 15, 3, 14, 11, 10, 3, 14, 10, 15, 3,
+ 12, 9, 8, 3, 12, 8, 13, 3, 15, 10, 9, 3, 15, 9, 12, 3,
+ 16, 17, 18, 3, 16, 18, 19, 3, 20, 21, 22, 3, 20, 22, 23, 3,
+ 22, 19, 18, 3, 22, 18, 23, 3, 20, 17, 16, 3, 20, 16, 21, 3,
+ 23, 18, 17, 3, 23, 17, 20, 3, 24, 25, 26, 3, 24, 26, 27, 3,
+ 28, 29, 30, 3, 28, 30, 31, 3, 30, 27, 26, 3, 30, 26, 31, 3,
+ 28, 25, 24, 3, 28, 24, 29, 3, 31, 26, 25, 3, 31, 25, 28, 3,
+ 32, 33, 2, 3, 32, 2, 1, 3, 4, 7, 34, 3, 4, 34, 35, 3,
+ 34, 7, 2, 3, 34, 2, 33, 3, 4, 35, 32, 3, 4, 32, 1, 3,
+ 36, 37, 10, 3, 36, 10, 9, 3, 12, 15, 38, 3, 12, 38, 39, 3,
+ 38, 15, 10, 3, 38, 10, 37, 3, 12, 39, 36, 3, 12, 36, 9, 3,
+ 40, 41, 18, 3, 40, 18, 17, 3, 20, 23, 42, 3, 20, 42, 43, 3,
+ 42, 23, 18, 3, 42, 18, 41, 3, 20, 43, 40, 3, 20, 40, 17, 3,
+ 44, 45, 26, 3, 44, 26, 25, 3, 28, 31, 46, 3, 28, 46, 47, 3,
+ 46, 31, 26, 3, 46, 26, 45, 3, 28, 47, 44, 3, 28, 44, 25, 3,
+ 48, 49, 50, 2, 49, 51, 50, 2, 52, 53, 54, 2, 53, 55, 54, 2,
+ 51, 49, 56, 3, 57, 56, 58, 3, 56, 49, 58, 3, 49, 48, 58, 3,
+ 58, 48, 59, 3, 48, 50, 59, 3, 59, 50, 60, 3, 50, 51, 60, 3,
+ 51, 56, 60, 3, 60, 56, 61, 3, 56, 57, 61, 3, 62, 61, 63, 3,
+ 61, 57, 63, 3, 57, 64, 63, 3, 63, 64, 65, 3, 62, 66, 67, 3,
+ 66, 65, 67, 3, 65, 64, 67, 3, 67, 64, 68, 3, 64, 57, 68, 3,
+ 57, 58, 68, 3, 58, 59, 68, 3, 68, 59, 69, 3, 59, 60, 69, 3,
+ 60, 61, 69, 3, 61, 62, 69, 3, 62, 67, 69, 3, 54, 55, 70, 3,
+ 71, 70, 72, 3, 70, 55, 72, 3, 55, 53, 72, 3, 72, 53, 73, 3,
+ 53, 52, 73, 3, 73, 52, 74, 3, 52, 54, 74, 3, 54, 70, 74, 3,
+ 74, 70, 75, 3, 70, 71, 75, 3, 75, 71, 76, 3, 76, 71, 77, 3,
+ 77, 71, 78, 3, 71, 72, 78, 3, 72, 73, 78, 3, 78, 73, 79, 3,
+ 73, 74, 79, 3, 74, 75, 79, 3, 79, 75, 80, 3, 75, 76, 80, 3,
+ 80, 76, 81, 3, 81, 76, 82, 3, 76, 77, 82, 3, 82, 77, 83, 3,
+ 77, 78, 83, 3, 78, 79, 83, 3, 79, 80, 83, 3, 68, 69, 67, 3,
+};
+const float pt_hull[12][3] = {
+ {-0.753237069f, -0.0327468291f, -1.85726798f},
+ {-0.168063566f, 0.378367513f, -0.5776124f},
+ {0.753236949f, -0.0327468291f, -1.85726798f},
+ {0.168063387f, 0.378367513f, -0.5776124f},
+ {0.753237247f, -0.108256564f, 1.57974494f},
+ {-0.753237069f, -0.108256564f, 1.57974291f},
+ {-0.753237069f, -0.345998049f, 1.57974291f},
+ {-0.753237069f, -0.345998049f, -1.85726798f},
+ {-5.96046448e-08f, -0.345998049f, -1.9763006f},
+ {0.753236949f, -0.345998049f, -1.85726798f},
+ {0.753236949f, -0.345998049f, 1.57974291f},
+ {-1.1920929e-07f, -0.345998675f, 1.66989052f},
+};
+const float pt_mountain[128] = {
+ 0.120477648f, 0.196457623f, 0.552562908f, 0.412726264f, 0.7795036f, 0.13367266f,
+ 0.799960156f, 0.951971425f, 0.173504838f, 0.751336708f, 0.653138672f, 0.902642787f,
+ 0.854327274f, 0.1117685f, 0.670569828f, 0.266287321f, 0.31140323f, 0.451703008f,
+ 0.126155151f, 0.0610638095f, 0.291990923f, 0.461398387f, 0.661575983f, 0.437318288f,
+ 0.74328905f, 0.393167103f, 0.494441228f, 0.599429669f, 0.0600501191f, 0.416588543f,
+ 0.439743648f, 0.16283145f, 0.0578797273f, 0.225388541f, 0.607577524f, 0.890835437f,
+ 0.470729831f, 0.766200345f, 0.206510369f, 0.0372406214f, 0.171102773f, 0.762642608f,
+ 0.83721128f, 0.87616908f, 0.138870249f, 0.828751337f, 0.979444629f, 0.807658524f,
+ 0.846562912f, 0.518728563f, 0.339629536f, 0.979841967f, 0.677707196f, 0.538889988f,
+ 0.786338917f, 0.427459142f, 0.256313669f, 0.569528906f, 0.0268413828f, 0.182679382f,
+ 0.985364298f, 0.244284859f, 0.532202875f, 0.965506584f, 0.0438101832f, 0.54121619f,
+ 0.0589798161f, 0.284916854f, 0.534982301f, 0.965567614f, 0.228318128f, 0.769870166f,
+ 0.410399507f, 0.257827631f, 0.849022263f, 0.392808795f, 0.319994679f, 0.286082087f,
+ 0.968492858f, 0.997383148f, 0.293091209f, 0.484712813f, 0.721840091f, 0.404070096f,
+ 0.705929806f, 0.453621466f, 0.464097466f, 0.160767695f, 0.598945353f, 0.5857593f,
+ 0.941703557f, 0.201179307f, 0.577787318f, 0.19918544f, 0.874378144f, 0.624666387f,
+ 0.387205736f, 0.996793153f, 0.498178946f, 0.245852678f, 0.863916828f, 0.286562403f,
+ 0.61636055f, 0.586474807f, 0.878104915f, 0.749754761f, 0.786409806f, 0.0334170452f,
+ 0.487558811f, 0.673739534f, 0.218511212f, 0.292373965f, 0.607379761f, 0.418232289f,
+ 0.853102942f, 0.326091633f, 0.63062622f, 0.526857669f, 0.351657091f, 0.865936638f,
+ 0.844744846f, 0.379454898f, 0.98327759f, 0.844225676f, 0.300655059f, 0.971866062f,
+ 0.510324504f, 0.794319831f,
+};
diff --git a/games/polytrack/src/data.h b/games/polytrack/src/data.h
new file mode 100644
index 00000000..586303c8
--- /dev/null
+++ b/games/polytrack/src/data.h
@@ -0,0 +1,102 @@
+/* PolyTrack's data, made by tools/pack.py from Kodub's web build of PolyTrack 0.6.3: the official tracks,
+ * the track parts and the car (their models simplified), the materials. Do not edit. */
+#ifndef PT_DATA_H
+#define PT_DATA_H
+#include <stdint.h>
+
+#define PT_FIX 256 /* vertex units a metre */
+typedef struct { uint32_t vfirst; uint8_t nv, pad; uint16_t ntri; uint32_t tfirst; } pt_lod;
+typedef struct { pt_lod lod[2]; int16_t box[6]; } pt_chunk;
+typedef struct {
+ uint16_t vbase, nv; /* its vertices in pt_pool */
+ uint16_t chunk; uint8_t nchunk;
+ uint16_t col, ncol; /* physics triangles in pt_ctri */
+ uint16_t grid; uint8_t gx, gz; /* their 5 m grid, from lo */
+ int16_t lo[3], hi[3];
+ uint8_t simple; /* one mesh for all: faces can be hidden per instance (pt_masks) */
+ uint8_t det; /* 1 checkpoint, 2 finish */
+ uint8_t custom; /* takes the custom block colours */
+ float dc[3], ds[3]; /* its detector box */
+ float start[3]; /* a start: where the car goes */
+} pt_part;
+typedef struct { int16_t x, z; uint8_t part, orient, color, y, mask, pad; } pt_inst;
+extern const uint8_t pt_blob[]; /* data.bin (data.S) */
+typedef struct { uint16_t inst, order; } pt_spec;
+typedef struct {
+ const char *name;
+ uint8_t env, sun;
+ uint16_t inst, ninst, mask, cell;
+ int16_t gx0, gz0; uint8_t gw, gh, reach;
+ uint16_t spec; uint8_t ncp, nfin, norders;
+ uint16_t start;
+ int16_t bx0, bx1, bz0, bz1;
+ uint16_t thumb; uint8_t tw, th;
+} pt_track;
+
+#define PT_NPARTS 129
+#define PT_NTRACKS 17
+#define PT_NMAT 21
+#define PT_M_ROAD 0
+#define PT_M_ROAD_EDGE_RED 1
+#define PT_M_ROAD_EDGE_WHITE 2
+#define PT_M_ROAD_BARRIER 3
+#define PT_M_FINISH_WHITE 4
+#define PT_M_FINISH_BLACK 5
+#define PT_M_FINISH 6
+#define PT_M_START_LINE 7
+#define PT_M_CHECKPOINT 8
+#define PT_M_PLANE_WALL 9
+#define PT_M_PLANE_WALL_DETAIL 10
+#define PT_M_BLOCK_SURFACE 11
+#define PT_M_PILLAR 12
+#define PT_M_PILLAR_EDGE 13
+#define PT_M_WALL_TRACK 14
+#define PT_M_WALL_TRACK_BOTTOM 15
+#define PT_M_WALL_TRACK_SIDES 16
+#define PT_M_SIGN_RED 17
+#define PT_M_SIGN_BLACK 18
+#define PT_M_SIGN_YELLOW 19
+#define PT_M_ROAD_EDGE 20
+#define PT_CM_MAIN 0
+#define PT_CM_STRIPE 1
+#define PT_CM_AIRINTAKE 2
+#define PT_CM_METAL 3
+#define PT_CM_BRAKELIGHT 4
+#define PT_CM_TIRE 5
+#define PT_CM_RIM 6
+#define pt_pool ((const int16_t *)(pt_blob + 0)) /* 61326 */
+#define pt_cvidx ((const uint16_t *)(pt_blob + 122652)) /* 24335 */
+#define pt_rtri ((const uint8_t *)(pt_blob + 171324)) /* 288939 */
+extern const pt_chunk pt_chunks[216];
+#define pt_ctri ((const uint16_t *)(pt_blob + 460264)) /* 93834 */
+#define pt_cgrid ((const uint32_t *)(pt_blob + 647932)) /* 3762 */
+#define pt_cgidx ((const uint16_t *)(pt_blob + 662980)) /* 75289 */
+extern const pt_part pt_parts[PT_NPARTS];
+extern const uint8_t pt_part_id[129];
+extern const int8_t pt_orient[216];
+#define pt_insts ((const pt_inst *)(pt_blob + 813560)) /* 35078 */
+#define pt_masks ((const uint32_t *)(pt_blob + 1164340)) /* 1425 */
+#define pt_tcells ((const uint16_t *)(pt_blob + 1170040)) /* 5771 */
+#define pt_specs ((const pt_spec *)(pt_blob + 1181584)) /* 87 */
+#define pt_thumbs ((const uint8_t *)(pt_blob + 1181932)) /* 10755 */
+extern const pt_track pt_tracks[PT_NTRACKS];
+extern const uint16_t pt_envmat[189];
+extern const uint16_t pt_custom[27];
+extern const uint16_t pt_carmat[15];
+extern const int16_t pt_body_v[294];
+extern const uint8_t pt_body_t[668];
+#define PT_BODY_NV 98
+#define PT_BODY_NT 167
+extern const int16_t pt_wheel_v[126];
+extern const uint8_t pt_wheel_t[320];
+#define PT_WHEEL_NV 42
+#define PT_WHEEL_NT 80
+extern const int16_t pt_extra_v[252];
+extern const uint8_t pt_extra_t[528];
+#define PT_EXTRA_NV 84
+#define PT_EXTRA_NT 132
+extern const float pt_hull[12][3];
+#define PT_HULL_N 12
+extern const float pt_mountain[128];
+#define PT_MOUNTAIN_N 128
+#endif
diff --git a/games/polygt3d/src/draw.c b/games/polytrack/src/draw.c
similarity index 100%
rename from games/polygt3d/src/draw.c
rename to games/polytrack/src/draw.c
diff --git a/games/polygt3d/src/draw.h b/games/polytrack/src/draw.h
similarity index 100%
rename from games/polygt3d/src/draw.h
rename to games/polytrack/src/draw.h
diff --git a/games/polygt3d/src/icon.png b/games/polytrack/src/icon.png
similarity index 100%
rename from games/polygt3d/src/icon.png
rename to games/polytrack/src/icon.png
diff --git a/games/polygt3d/src/main.c b/games/polytrack/src/main.c
similarity index 100%
rename from games/polygt3d/src/main.c
rename to games/polytrack/src/main.c
diff --git a/games/polygt3d/src/plat.h b/games/polytrack/src/plat.h
similarity index 100%
rename from games/polygt3d/src/plat.h
rename to games/polytrack/src/plat.h
diff --git a/games/polygt3d/src/plat_eadk.c b/games/polytrack/src/plat_eadk.c
similarity index 100%
rename from games/polygt3d/src/plat_eadk.c
rename to games/polytrack/src/plat_eadk.c
diff --git a/games/polytrack/src/pt.h b/games/polytrack/src/pt.h
new file mode 100644
index 00000000..ea817bce
--- /dev/null
+++ b/games/polytrack/src/pt.h
@@ -0,0 +1,78 @@
+/* PolyTrack's shared pieces: vectors, quaternions and the few functions of maths the game needs, written out so that
+ * they give the same bits everywhere (only + - * / and sqrt, which IEEE 754 rounds the same on every machine; the
+ * files that use them are compiled with -ffp-contract=off, and every constant is a float). */
+#ifndef PT_H
+#define PT_H
+#include <stdbool.h>
+#include <stdint.h>
+
+typedef struct { float x, y, z; } v3;
+typedef struct { float x, y, z, w; } qt;
+typedef struct { v3 r[3]; } m3; /* rows */
+
+#define PT_PI 3.14159265f
+#define PT_HALF_PI 1.57079633f
+
+static inline v3 V3(float x, float y, float z) { v3 v = {x, y, z}; return v; }
+static inline v3 vadd(v3 a, v3 b) { return V3(a.x + b.x, a.y + b.y, a.z + b.z); }
+static inline v3 vsub(v3 a, v3 b) { return V3(a.x - b.x, a.y - b.y, a.z - b.z); }
+static inline v3 vmul(v3 a, float s) { return V3(a.x * s, a.y * s, a.z * s); }
+static inline v3 vneg(v3 a) { return V3(-a.x, -a.y, -a.z); }
+static inline float vdot(v3 a, v3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
+static inline v3 vcross(v3 a, v3 b) { return V3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); }
+static inline float vlen2(v3 a) { return vdot(a, a); }
+static inline float pt_sqrtf(float x) { return __builtin_sqrtf(x); }
+static inline float vlen(v3 a) { return pt_sqrtf(vdot(a, a)); }
+static inline v3 vnorm(v3 a) {
+ float l = vlen(a);
+ return l > 0.0f ? vmul(a, 1.0f / l) : a;
+}
+static inline float pt_absf(float x) { return x < 0.0f ? -x : x; }
+static inline float pt_minf(float a, float b) { return a < b ? a : b; }
+static inline float pt_maxf(float a, float b) { return a > b ? a : b; }
+static inline float pt_clampf(float v, float lo, float hi) { return v < lo ? lo : v > hi ? hi : v; }
+
+static inline v3 mrow(const m3 *m, v3 v) { return V3(vdot(m->r[0], v), vdot(m->r[1], v), vdot(m->r[2], v)); }
+/* m^T v */
+static inline v3 mcol(const m3 *m, v3 v) {
+ return V3(m->r[0].x * v.x + m->r[1].x * v.y + m->r[2].x * v.z, m->r[0].y * v.x + m->r[1].y * v.y + m->r[2].y * v.z,
+ m->r[0].z * v.x + m->r[1].z * v.y + m->r[2].z * v.z);
+}
+static inline v3 mcolumn(const m3 *m, int i) {
+ return i == 0 ? V3(m->r[0].x, m->r[1].x, m->r[2].x) : i == 1 ? V3(m->r[0].y, m->r[1].y, m->r[2].y)
+ : V3(m->r[0].z, m->r[1].z, m->r[2].z);
+}
+
+/* Bullet's btMatrix3x3::setRotation */
+static inline void qmat(qt q, m3 *m) {
+ float d = q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w;
+ float s = 2.0f / d;
+ float xs = q.x * s, ys = q.y * s, zs = q.z * s;
+ float wx = q.w * xs, wy = q.w * ys, wz = q.w * zs;
+ float xx = q.x * xs, xy = q.x * ys, xz = q.x * zs;
+ float yy = q.y * ys, yz = q.y * zs, zz = q.z * zs;
+ m->r[0] = V3(1.0f - (yy + zz), xy - wz, xz + wy);
+ m->r[1] = V3(xy + wz, 1.0f - (xx + zz), yz - wx);
+ m->r[2] = V3(xz - wy, yz + wx, 1.0f - (xx + yy));
+}
+static inline qt qmul(qt a, qt b) {
+ qt r = {a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y, a.w * b.y + a.y * b.w + a.z * b.x - a.x * b.z,
+ a.w * b.z + a.z * b.w + a.x * b.y - a.y * b.x, a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z};
+ return r;
+}
+static inline qt qnorm(qt q) {
+ float l = pt_sqrtf(q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w);
+ qt r = {q.x / l, q.y / l, q.z / l, q.w / l};
+ return r;
+}
+v3 qrot(qt q, v3 v);
+qt mat_quat(const m3 *m); /* Bullet's btMatrix3x3::getRotation */
+
+float pt_sinf(float x);
+float pt_cosf(float x);
+float pt_atan2f(float y, float x);
+float pt_powf(float x, float y); /* x > 0 */
+float pt_expf(float x);
+
+uint32_t pt_hash(uint32_t h, const void *data, uint32_t n); /* FNV-1a */
+#endif
diff --git a/games/polytrack/src/ptmath.c b/games/polytrack/src/ptmath.c
new file mode 100644
index 00000000..2fe5ef7f
--- /dev/null
+++ b/games/polytrack/src/ptmath.c
@@ -0,0 +1,145 @@
+/* The maths of pt.h: polynomial sine, cosine, arctangent and powers (the coefficients are Cephes' single precision
+ * ones), using only operations IEEE 754 rounds the same everywhere, so a race comes out the same on the calculator
+ * and on every computer. */
+#include <string.h>
+#include "pt.h"
+
+v3 qrot(qt q, v3 v) {
+ m3 m;
+ qmat(q, &m);
+ return mrow(&m, v);
+}
+
+qt mat_quat(const m3 *m) {
+ float tr = m->r[0].x + m->r[1].y + m->r[2].z;
+ float t[4];
+ if (tr > 0.0f) {
+ float s = pt_sqrtf(tr + 1.0f);
+ t[3] = s * 0.5f;
+ s = 0.5f / s;
+ t[0] = (m->r[2].y - m->r[1].z) * s;
+ t[1] = (m->r[0].z - m->r[2].x) * s;
+ t[2] = (m->r[1].x - m->r[0].y) * s;
+ } else {
+ float d[3] = {m->r[0].x, m->r[1].y, m->r[2].z};
+ float e[3][3] = {{m->r[0].x, m->r[0].y, m->r[0].z}, {m->r[1].x, m->r[1].y, m->r[1].z},
+ {m->r[2].x, m->r[2].y, m->r[2].z}};
+ int i = d[0] < d[1] ? (d[1] < d[2] ? 2 : 1) : (d[0] < d[2] ? 2 : 0);
+ int j = (i + 1) % 3, k = (i + 2) % 3;
+ float s = pt_sqrtf(e[i][i] - e[j][j] - e[k][k] + 1.0f);
+ t[i] = s * 0.5f;
+ s = 0.5f / s;
+ t[3] = (e[k][j] - e[j][k]) * s;
+ t[j] = (e[j][i] + e[i][j]) * s;
+ t[k] = (e[k][i] + e[i][k]) * s;
+ }
+ qt q = {t[0], t[1], t[2], t[3]};
+ return q;
+}
+
+/* x reduced by multiples of pi/2: x = k pi/2 + r, |r| <= pi/4 (Cody-Waite, three parts of pi/2) */
+static float reduce(float x, int *quadrant) {
+ float k = x * 0.636619772f;
+ int n = (int)(k < 0.0f ? k - 0.5f : k + 0.5f);
+ float fn = (float)n;
+ float r = ((x - fn * 1.5703125f) - fn * 4.83751297e-4f) - fn * 7.54978995e-8f;
+ *quadrant = n & 3;
+ return r;
+}
+static float sin_poly(float r) {
+ float z = r * r;
+ return ((-1.9515295891e-4f * z + 8.3321608736e-3f) * z - 1.6666654611e-1f) * z * r + r;
+}
+static float cos_poly(float r) {
+ float z = r * r;
+ return ((2.443315711809948e-5f * z - 1.388731625493765e-3f) * z + 4.166664568298827e-2f) * z * z - 0.5f * z + 1.0f;
+}
+float pt_sinf(float x) {
+ int q;
+ float r = reduce(x, &q);
+ switch (q) {
+ case 0: return sin_poly(r);
+ case 1: return cos_poly(r);
+ case 2: return -sin_poly(r);
+ default: return -cos_poly(r);
+ }
+}
+float pt_cosf(float x) {
+ int q;
+ float r = reduce(x, &q);
+ switch (q) {
+ case 0: return cos_poly(r);
+ case 1: return -sin_poly(r);
+ case 2: return -cos_poly(r);
+ default: return sin_poly(r);
+ }
+}
+
+static float atan_poly(float t) {
+ /* t >= 0 */
+ float y = 0.0f;
+ if (t > 2.414213562f) {
+ y = PT_HALF_PI;
+ t = -1.0f / t;
+ } else if (t > 0.414213562f) {
+ y = 0.785398163f;
+ t = (t - 1.0f) / (t + 1.0f);
+ }
+ float z = t * t;
+ return y + (((8.05374449538e-2f * z - 1.38776856032e-1f) * z + 1.99777106478e-1f) * z - 3.33329491539e-1f) * z * t + t;
+}
+float pt_atan2f(float y, float x) {
+ if (x == 0.0f) {
+ if (y > 0.0f) return PT_HALF_PI;
+ if (y < 0.0f) return -PT_HALF_PI;
+ return 0.0f;
+ }
+ float a = atan_poly(pt_absf(y / x));
+ if (x < 0.0f) a = PT_PI - a;
+ return y < 0.0f ? -a : a;
+}
+
+static uint32_t fbits(float f) {
+ uint32_t u;
+ memcpy(&u, &f, 4);
+ return u;
+}
+static float bitsf(uint32_t u) {
+ float f;
+ memcpy(&f, &u, 4);
+ return f;
+}
+/* log2 for x > 0 (normal numbers) */
+static float log2_(float x) {
+ uint32_t u = fbits(x);
+ int e = (int)((u >> 23) & 255) - 127;
+ float m = bitsf((u & 0x007FFFFFu) | 0x3F800000u); /* 1 <= m < 2 */
+ if (m > 1.41421356f) m *= 0.5f, e++;
+ float t = m - 1.0f; /* -0.29 .. 0.41 */
+ float z = t * t;
+ float y = (((((((7.0376836292e-2f * t - 1.1514610310e-1f) * t + 1.1676998740e-1f) * t - 1.2420140846e-1f) * t +
+ 1.4249322787e-1f) * t - 1.6668057665e-1f) * t + 2.0000714765e-1f) * t - 2.4999993993e-1f) * t +
+ 3.3333331174e-1f;
+ float ln = t - 0.5f * z + y * z * t;
+ return (float)e + ln * 1.44269504f;
+}
+/* 2^x */
+static float exp2_(float x) {
+ if (x > 126.0f) x = 126.0f;
+ if (x < -126.0f) return 0.0f;
+ float fl = (float)(int)x;
+ if (fl > x) fl -= 1.0f;
+ float f = (x - fl) * 0.693147181f; /* 0 <= f < ln 2 */
+ float p = ((((1.9875691500e-4f * f + 1.3981999507e-3f) * f + 8.3334519073e-3f) * f + 4.1665795894e-2f) * f +
+ 1.6666665459e-1f) * f + 5.0000001201e-1f;
+ float ef = 1.0f + f + p * f * f;
+ return ef * bitsf((uint32_t)((int)fl + 127) << 23);
+}
+float pt_powf(float x, float y) { return exp2_(y * log2_(x)); }
+float pt_expf(float x) { return exp2_(x * 1.44269504f); }
+
+uint32_t pt_hash(uint32_t h, const void *data, uint32_t n) {
+ const uint8_t *p = data;
+ for (uint32_t i = 0; i < n; i++) h = (h ^ p[i]) * 16777619u;
+ return h;
+}
diff --git a/games/polytrack/src/render.c b/games/polytrack/src/render.c
new file mode 100644
index 00000000..ef6f3575
--- /dev/null
+++ b/games/polytrack/src/render.c
@@ -0,0 +1,651 @@
+/* PolyTrack's picture (render.h). A frame is set up once: the visible pieces of the track (each part is cut into 20 m
+ * chunks, near ones drawn in full, far ones simplified), the mountains, the car and its shadow are turned into the
+ * camera's space, cut at the near plane, lit (Three.js' hemisphere light and sun, as the game) and kept as screen
+ * triangles with their depth. rd_draw then fills a strip of rows at a time: the sky and the ground, the mountains
+ * behind everything, then every triangle crossing the strip with a depth buffer, then the shadow. */
+#include <string.h>
+#include "../../../launcher/src/gfx.h"
+#include "data.h"
+#include "render.h"
+
+#define CX 160.0f
+#define CY 120.0f
+#define NEAR 0.5f
+#define MAX_TRIS 1700
+#define MAX_VIS 400
+#define GUARD 1500.0f
+#define STRIP_ROWS 24
+
+enum { RK_SOLID, RK_GHOST, RK_SHADOW, RK_BACK };
+
+typedef struct {
+ int16_t x[3], y[3]; /* 1/16 pixel, top to bottom */
+ uint16_t w[3]; /* NEAR/z, 65535 = at the near plane */
+ uint16_t c;
+ uint8_t kind, pad;
+} tri;
+
+static tri tris[MAX_TRIS];
+static int ntris;
+int rd_tris, rd_dropped;
+int rd_detail = 2;
+static const float DRAW[4] = {100.0f, 140.0f, 180.0f, 230.0f};
+static const float NEAR_DETAIL[4] = {22.0f, 30.0f, 40.0f, 52.0f};
+
+/* the camera */
+static camera_t C;
+static float focal, far_d, fog0;
+static float px_n[2], py_n[2]; /* the side planes */
+static int env_now;
+/* the light */
+static v3 sun;
+static float hemi_sky[3], hemi_ground[3];
+static float matlin[3][PT_NMAT][3], custom_lin[9][3], ground_lin[3][3];
+static uint8_t srgb[1025];
+static uint16_t back_lut[256];
+static float fog_rgb[3];
+
+const uint32_t rd_paints[8] = {0x6B0B0B, 0x0B2D6B, 0x0F5223, 0x4A1166, 0x6B3A0B, 0x0B5A5A, 0x1A1A1A, 0x8C8C8C};
+
+/* ------------------------------------------------------------------ colours */
+static float lin_of(int c8) {
+ float c = (float)c8 / 255.0f;
+ return c <= 0.04045f ? c / 12.92f : pt_powf((c + 0.055f) / 1.055f, 2.4f);
+}
+static void hex_lin(uint32_t h, float *o) {
+ o[0] = lin_of((int)(h >> 16 & 255)), o[1] = lin_of((int)(h >> 8 & 255)), o[2] = lin_of((int)(h & 255));
+}
+static void color_setup(void) {
+ static int done;
+ if (done) return;
+ done = 1;
+ for (int i = 0; i <= 1024; i++) {
+ float l = (float)i / 1024.0f;
+ float s = l <= 0.0031308f ? l * 12.92f : 1.055f * pt_powf(l, 1.0f / 2.4f) - 0.055f;
+ int v = (int)(s * 255.0f + 0.5f);
+ srgb[i] = (uint8_t)(v > 255 ? 255 : v < 0 ? 0 : v);
+ }
+ for (int e = 0; e < 3; e++)
+ for (int m = 0; m < PT_NMAT; m++)
+ for (int k = 0; k < 3; k++) matlin[e][m][k] = (float)pt_envmat[(e * PT_NMAT + m) * 3 + k] / 4096.0f;
+ for (int i = 0; i < 9; i++)
+ for (int k = 0; k < 3; k++) custom_lin[i][k] = (float)pt_custom[i * 3 + k] / 4096.0f;
+ hex_lin(0x355638, ground_lin[0]);
+ hex_lin(0xA8A8A8, ground_lin[1]);
+ hex_lin(0xAA7942, ground_lin[2]);
+ hex_lin(0x3B618D, hemi_sky);
+ hex_lin(0xB2C0C3, hemi_ground);
+}
+static int enc(float l) {
+ int i = (int)(l * 1024.0f);
+ return srgb[i < 0 ? 0 : i > 1024 ? 1024 : i];
+}
+static uint16_t rgb565(int r, int g, int b) { return (uint16_t)((r & 0xF8) << 8 | (g & 0xFC) << 3 | b >> 3); }
+
+/* Three.js' light on a flat face: base/pi (4.7 hemisphere(n) + 4.7 max(0, n.sun)), then the fade to the haze far
+ away (t 0..256) */
+static uint16_t lit(const float *base, v3 n, int t, float dim) {
+ float h = 0.5f * n.y + 0.5f, d = vdot(n, sun);
+ if (d < 0.0f) d = 0.0f;
+ d *= dim;
+ int c[3];
+ for (int k = 0; k < 3; k++) {
+ float e = (hemi_ground[k] + (hemi_sky[k] - hemi_ground[k]) * h + d) * (4.7f / PT_PI);
+ c[k] = enc(base[k] * e);
+ if (t) c[k] += (enc(fog_rgb[k]) - c[k]) * t / 256;
+ }
+ return rgb565(c[0], c[1], c[2]);
+}
+
+/* ------------------------------------------------------------------ cameras */
+static void look_at(camera_t *cam, v3 eye, v3 target) {
+ /* Three.js' lookAt with the world's up */
+ v3 z = vsub(eye, target);
+ if (vlen2(z) == 0.0f) z = V3(0.0f, 0.0f, 1.0f);
+ z = vnorm(z);
+ v3 up = V3(0.0f, 1.0f, 0.0f);
+ v3 x = vcross(up, z);
+ if (vlen2(x) == 0.0f) {
+ z.z += pt_absf(up.z) == 1.0f ? 0.0001f : 0.0f;
+ z.x += pt_absf(up.z) == 1.0f ? 0.0f : 0.0001f;
+ z = vnorm(z);
+ x = vcross(up, z);
+ }
+ x = vnorm(x);
+ cam->pos = eye;
+ cam->right = x;
+ cam->up = vcross(z, x);
+ cam->fwd = vneg(z);
+}
+
+void chase_reset(chase_t *ch, v3 pos, qt rot) {
+ ch->anchor = vadd(qrot(rot, V3(1e-5f, 0.0f, -1.0f)), pos);
+ ch->up = qrot(rot, V3(0.0f, 1.0f, 0.0f));
+}
+
+void chase_update(chase_t *ch, camera_t *cam, float dt, v3 pos, qt rot, float kmh) {
+ v3 r = qrot(rot, V3(0.0f, 1.0f, 0.0f));
+ float a = pt_minf(1.0f, 5.0f * dt);
+ ch->up = vadd(vmul(r, a), vmul(ch->up, 1.0f - a));
+ v3 s = vnorm(vsub(pos, ch->anchor));
+ v3 eye = vadd(vsub(pos, vmul(s, 5.5f)), vmul(ch->up, 2.0f));
+ if (eye.y < 0.25f) eye.y = 0.25f;
+ look_at(cam, eye, vadd(pos, vmul(ch->up, 1.8f)));
+ ch->anchor = vsub(pos, vmul(s, 5.5f));
+ cam->fov = 70.0f + 30.0f * (1.0f - pt_expf(-pt_absf(kmh) / 200.0f));
+}
+
+void cockpit_camera(camera_t *cam, v3 pos, qt rot, float kmh) {
+ m3 m;
+ qmat(rot, &m);
+ cam->pos = vadd(pos, mrow(&m, V3(0.0f, 1.1f, 0.4f)));
+ /* the car's turn, then half a turn round y: the camera looks along the car's forward */
+ cam->right = vneg(mcolumn(&m, 0));
+ cam->up = mcolumn(&m, 1);
+ cam->fwd = mcolumn(&m, 2);
+ cam->fov = 70.0f + 10.0f * (1.0f - pt_expf(-pt_absf(kmh) / 200.0f));
+}
+
+/* ------------------------------------------------------------------ triangles */
+typedef struct { float x, y, w; } sv;
+static int iceil(float f) {
+ int i = (int)f;
+ return (float)i < f ? i + 1 : i;
+}
+
+static void store(const sv *a, const sv *b, const sv *c, uint16_t col, int kind) {
+ if (ntris >= MAX_TRIS) {
+ rd_dropped = 1;
+ return;
+ }
+ const sv *p[3] = {a, b, c};
+ for (int i = 0; i < 2; i++)
+ for (int j = 0; j < 2 - i; j++)
+ if (p[j]->y > p[j + 1]->y) {
+ const sv *t = p[j];
+ p[j] = p[j + 1], p[j + 1] = t;
+ }
+ if (iceil(p[2]->y - 0.5f) <= iceil(p[0]->y - 0.5f)) return;
+ if (p[2]->y < 0.0f || p[0]->y > 240.0f) return;
+ float xmin = pt_minf(p[0]->x, pt_minf(p[1]->x, p[2]->x)), xmax = pt_maxf(p[0]->x, pt_maxf(p[1]->x, p[2]->x));
+ if (xmax < 0.0f || xmin > 320.0f) return;
+ tri *t = &tris[ntris++];
+ for (int i = 0; i < 3; i++) {
+ t->x[i] = (int16_t)(p[i]->x * 16.0f);
+ t->y[i] = (int16_t)(p[i]->y * 16.0f);
+ float w = pt_clampf(p[i]->w, 0.0f, 1.0f);
+ t->w[i] = (uint16_t)(w * 65535.0f);
+ }
+ t->c = col, t->kind = (uint8_t)kind;
+}
+
+static void screen_poly(sv *v, int n, uint16_t col, int kind) {
+ bool out = false;
+ for (int i = 0; i < n; i++)
+ if (v[i].x < -GUARD || v[i].x > 320.0f + GUARD || v[i].y < -GUARD || v[i].y > 240.0f + GUARD) out = true;
+ if (out) {
+ static const float LIM[4] = {-GUARD, 320.0f + GUARD, -GUARD, 240.0f + GUARD};
+ sv buf[2][10];
+ int m = n, cur = 0;
+ memcpy(buf[0], v, sizeof(sv) * (size_t)n);
+ for (int e = 0; e < 4 && m; e++) {
+ sv *in = buf[cur], *o = buf[cur ^ 1];
+ int k = 0;
+ for (int i = 0; i < m; i++) {
+ sv *A = &in[i], *B = &in[(i + 1) % m];
+ float a = e < 2 ? A->x : A->y, b = e < 2 ? B->x : B->y;
+ bool ain = (e & 1) ? a <= LIM[e] : a >= LIM[e], bin = (e & 1) ? b <= LIM[e] : b >= LIM[e];
+ if (ain && k < 10) o[k++] = *A;
+ if (ain != bin && k < 10) {
+ float t = (LIM[e] - a) / (b - a);
+ sv q = {A->x + (B->x - A->x) * t, A->y + (B->y - A->y) * t, A->w + (B->w - A->w) * t};
+ o[k++] = q;
+ }
+ }
+ m = k, cur ^= 1;
+ }
+ for (int i = 1; i + 1 < m; i++) store(&buf[cur][0], &buf[cur][i], &buf[cur][i + 1], col, kind);
+ return;
+ }
+ for (int i = 1; i + 1 < n; i++) store(&v[0], &v[i], &v[i + 1], col, kind);
+}
+
+/* a triangle in the camera's space, cut at the near plane, projected */
+static void cam_tri(v3 a, v3 b, v3 c, uint16_t col, int kind) {
+ v3 p[3] = {a, b, c}, q[4];
+ int m = 0;
+ for (int i = 0; i < 3; i++) {
+ const v3 *A = &p[i], *B = &p[(i + 1) % 3];
+ bool ain = A->z >= NEAR, bin = B->z >= NEAR;
+ if (ain) q[m++] = *A;
+ if (ain != bin) {
+ float t = (NEAR - A->z) / (B->z - A->z);
+ q[m++] = V3(A->x + (B->x - A->x) * t, A->y + (B->y - A->y) * t, NEAR);
+ }
+ }
+ if (m < 3) return;
+ sv s[4];
+ for (int i = 0; i < m; i++) {
+ float inv = 1.0f / q[i].z;
+ s[i].x = CX + q[i].x * focal * inv;
+ s[i].y = CY - q[i].y * focal * inv;
+ s[i].w = NEAR * inv;
+ }
+ screen_poly(s, m, col, kind);
+}
+
+static v3 to_cam(v3 w) {
+ v3 d = vsub(w, C.pos);
+ return V3(vdot(d, C.right), vdot(d, C.up), vdot(d, C.fwd));
+}
+
+static bool sphere_visible(v3 c, float r) {
+ if (c.z + r < NEAR || c.z - r > far_d) return false;
+ if (c.x * px_n[0] - c.z * px_n[1] > r) return false;
+ if (-c.x * px_n[0] - c.z * px_n[1] > r) return false;
+ if (c.y * py_n[0] - c.z * py_n[1] > r) return false;
+ if (-c.y * py_n[0] - c.z * py_n[1] > r) return false;
+ return true;
+}
+
+/* ------------------------------------------------------------------ the frame */
+void rd_begin(const camera_t *cam, int env, int sun_byte, bool sky_only) {
+ color_setup();
+ C = *cam;
+ env_now = env;
+ ntris = 0, rd_dropped = 0;
+ float half = C.fov * 0.5f * (PT_PI / 180.0f);
+ focal = CY * pt_cosf(half) / pt_sinf(half);
+ far_d = DRAW[rd_detail & 3];
+ fog0 = far_d * 0.55f;
+ float l = pt_sqrtf(focal * focal + CX * CX);
+ px_n[0] = focal / l, px_n[1] = CX / l;
+ l = pt_sqrtf(focal * focal + CY * CY);
+ py_n[0] = focal / l, py_n[1] = CY / l;
+ float a = 2.0f * (float)sun_byte * (PT_PI / 180.0f);
+ sun = vnorm(V3(pt_cosf(a), 0.78f, pt_sinf(a)));
+ /* the background: the sky dome (horizon white, zenith (5, 140, 255)) above, the lit ground below, fading into
+ the haze (#9bd1ff) with distance */
+ float g[3];
+ for (int k = 0; k < 3; k++) {
+ float e = (hemi_sky[k] + sun.y) * (4.7f / PT_PI);
+ g[k] = ground_lin[env][k] * e;
+ }
+ hex_lin(0x9BD1FF, fog_rgb);
+ /* (the far track fades to the ground's colour seen far off) */
+ for (int k = 0; k < 3; k++) fog_rgb[k] = g[k] + (fog_rgb[k] - g[k]) * 0.18f;
+ for (int i = 0; i < 256; i++) {
+ float dy = ((float)i - 127.5f) / 127.5f;
+ int r, gg, b;
+ if (dy >= 0.0f) {
+ float h = pt_powf(pt_clampf(dy, 0.01f, 1.0f), 0.2f);
+ r = (int)((5.0f * h + 255.0f * (1.0f - h)) + 0.5f);
+ gg = (int)((140.0f * h + 255.0f * (1.0f - h)) + 0.5f);
+ b = 255;
+ } else {
+ float dist = pt_maxf(C.pos.y, 0.25f) / -dy;
+ float t = pt_clampf(dist / 10000.0f, 0.0f, 1.0f);
+ float hz[3];
+ hex_lin(0x9BD1FF, hz);
+ r = enc(g[0] + (hz[0] - g[0]) * t), gg = enc(g[1] + (hz[1] - g[1]) * t), b = enc(g[2] + (hz[2] - g[2]) * t);
+ }
+ back_lut[i] = rgb565(r, gg, b);
+ }
+ (void)sky_only;
+}
+
+/* ------------------------------------------------------------------ the track */
+typedef struct {
+ uint16_t inst;
+ uint8_t chunk, lod;
+ float dist;
+} vis_t;
+static vis_t vis[MAX_VIS];
+static int nvis;
+
+static void collect(int i, void *ctx) {
+ (void)ctx;
+ const pt_inst *in = world_inst(i);
+ const pt_part *p = &pt_parts[in->part];
+ const int8_t *o = world_orient(in);
+ v3 o0 = world_origin(in);
+ for (int k = 0; k < p->nchunk; k++) {
+ const pt_chunk *ch = &pt_chunks[p->chunk + k];
+ v3 lo = V3((float)ch->box[0] / PT_FIX, (float)ch->box[1] / PT_FIX, (float)ch->box[2] / PT_FIX);
+ v3 hi = V3((float)ch->box[3] / PT_FIX, (float)ch->box[4] / PT_FIX, (float)ch->box[5] / PT_FIX);
+ v3 lc = vmul(vadd(lo, hi), 0.5f);
+ float r = vlen(vsub(hi, lc));
+ v3 wc = vadd(V3((float)o[0] * lc.x + (float)o[1] * lc.y + (float)o[2] * lc.z,
+ (float)o[3] * lc.x + (float)o[4] * lc.y + (float)o[5] * lc.z,
+ (float)o[6] * lc.x + (float)o[7] * lc.y + (float)o[8] * lc.z), o0);
+ v3 cc = to_cam(wc);
+ if (!sphere_visible(cc, r)) continue;
+ if (nvis >= MAX_VIS) return;
+ float d = vlen(vsub(wc, C.pos)) - r;
+ vis_t *v = &vis[nvis++];
+ v->inst = (uint16_t)i, v->chunk = (uint8_t)k, v->dist = d;
+ v->lod = d < NEAR_DETAIL[rd_detail & 3] ? 0 : 1;
+ }
+}
+
+static v3 cv[256];
+static void draw_chunk(const vis_t *v) {
+ const pt_inst *in = world_inst(v->inst);
+ const pt_part *p = &pt_parts[in->part];
+ const pt_chunk *ch = &pt_chunks[p->chunk + v->chunk];
+ const pt_lod *lod = &ch->lod[v->lod];
+ if (!lod->ntri) lod = &ch->lod[v->lod ^ 1];
+ if (!lod->ntri) return;
+ const int8_t *o = world_orient(in);
+ /* part space -> camera space */
+ float M[3][3];
+ v3 ax[3] = {C.right, C.up, C.fwd};
+ for (int i = 0; i < 3; i++) {
+ float a[3] = {ax[i].x, ax[i].y, ax[i].z};
+ for (int j = 0; j < 3; j++) M[i][j] = (a[0] * (float)o[j] + a[1] * (float)o[3 + j] + a[2] * (float)o[6 + j]);
+ }
+ v3 t = to_cam(world_origin(in));
+ const float s = 1.0f / PT_FIX;
+ const int16_t *pool = &pt_pool[p->vbase * 3];
+ for (int k = 0; k < lod->nv; k++) {
+ const int16_t *q = &pool[pt_cvidx[lod->vfirst + k] * 3];
+ float x = (float)q[0] * s, y = (float)q[1] * s, z = (float)q[2] * s;
+ cv[k] = V3(M[0][0] * x + M[0][1] * y + M[0][2] * z + t.x, M[1][0] * x + M[1][1] * y + M[1][2] * z + t.y,
+ M[2][0] * x + M[2][1] * y + M[2][2] * z + t.z);
+ }
+ /* the colours: the track's world, or the one the part asks for */
+ int env = env_now;
+ int col = in->color;
+ if (col >= 1 && col <= 3) env = col - 1;
+ const float *custom = (p->custom && col >= 32 && col <= 40) ? custom_lin[col - 32] : NULL;
+ uint32_t mask = p->simple ? pt_masks[W.t->mask + in->mask] : 0xFFFFFFFFu;
+ int fade = v->dist > fog0 ? (int)((v->dist - fog0) * 256.0f / (far_d - fog0)) : 0;
+ if (fade > 240) fade = 240;
+ const uint8_t *tr = &pt_rtri[lod->tfirst * 7];
+ for (int k = 0; k < lod->ntri; k++, tr += 7) {
+ if (k < 32 && !(mask & (1u << k))) continue;
+ v3 a = cv[tr[0]], b = cv[tr[1]], c = cv[tr[2]];
+ float nx = (float)(int8_t)tr[4], ny = (float)(int8_t)tr[5], nz = (float)(int8_t)tr[6];
+ /* back faces */
+ float cnx = M[0][0] * nx + M[0][1] * ny + M[0][2] * nz, cny = M[1][0] * nx + M[1][1] * ny + M[1][2] * nz,
+ cnz = M[2][0] * nx + M[2][1] * ny + M[2][2] * nz;
+ if (cnx * a.x + cny * a.y + cnz * a.z >= 0.0f) continue;
+ v3 wn = vmul(V3((float)o[0] * nx + (float)o[1] * ny + (float)o[2] * nz, (float)o[3] * nx + (float)o[4] * ny + (float)o[5] * nz,
+ (float)o[6] * nx + (float)o[7] * ny + (float)o[8] * nz), 1.0f / 127.0f);
+ int m = tr[3];
+ const float *base = (custom && m == PT_M_BLOCK_SURFACE) ? custom : matlin[env][m];
+ cam_tri(a, b, c, lit(base, wn, fade, 1.0f), RK_SOLID);
+ }
+}
+
+static void mountains(void) {
+ /* the ring, far to near: the outer rings first, then in */
+ int s = W.mseg;
+ if (!s) return;
+ float rad[8];
+ for (int k = 0; k < 8; k++) rad[k] = W.mr + 100.0f * (float)k;
+ for (int k = 6; k >= 0; k--)
+ for (int e = 0; e < s; e++) {
+ float a0 = (float)e / (float)s * 2.0f * PT_PI, a1 = (float)(e + 1) / (float)s * 2.0f * PT_PI;
+ float c0 = pt_cosf(a0), s0 = pt_sinf(a0), c1 = pt_cosf(a1), s1 = pt_sinf(a1);
+ int e1 = (e + 1) % s;
+ float h00 = (float)W.mh[e][k] * (100.0f / 255.0f), h10 = (float)W.mh[e1][k] * (100.0f / 255.0f);
+ float h01 = (float)W.mh[e][k + 1] * (100.0f / 255.0f), h11 = (float)W.mh[e1][k + 1] * (100.0f / 255.0f);
+ if (h00 + h10 + h01 + h11 == 0.0f) continue;
+ v3 A = V3(W.mx + c0 * rad[k], h00, W.mz + s0 * rad[k]), B = V3(W.mx + c1 * rad[k], h10, W.mz + s1 * rad[k]);
+ v3 Cc = V3(W.mx + c1 * rad[k + 1], h11, W.mz + s1 * rad[k + 1]), D = V3(W.mx + c0 * rad[k + 1], h01, W.mz + s0 * rad[k + 1]);
+ v3 ca = to_cam(A), cb = to_cam(B), cc = to_cam(Cc), cd = to_cam(D);
+ if (ca.z < NEAR && cb.z < NEAR && cc.z < NEAR && cd.z < NEAR) continue;
+ v3 n1 = vnorm(vcross(vsub(B, A), vsub(Cc, A))), n2 = vnorm(vcross(vsub(Cc, A), vsub(D, A)));
+ float fade = 0.0f;
+ int t = (int)(fade);
+ cam_tri(ca, cb, cc, lit(ground_lin[env_now], n1, t, 1.0f), RK_BACK);
+ cam_tri(ca, cc, cd, lit(ground_lin[env_now], n2, t, 1.0f), RK_BACK);
+ }
+}
+
+void rd_track(void) {
+ mountains();
+ nvis = 0;
+ v3 r = V3(far_d, far_d, far_d);
+ world_query(vsub(C.pos, r), vadd(C.pos, r), collect, NULL);
+ /* near first, so that when there is no more room it is the far ones that go */
+ for (int i = 1; i < nvis; i++) {
+ vis_t t = vis[i];
+ int j = i - 1;
+ while (j >= 0 && vis[j].dist > t.dist) vis[j + 1] = vis[j], j--;
+ vis[j + 1] = t;
+ }
+ for (int i = 0; i < nvis; i++) draw_chunk(&vis[i]);
+}
+
+/* ------------------------------------------------------------------ the car */
+car_look car_look_of(const car_t *c) {
+ car_look l;
+ memset(&l, 0, sizeof l);
+ l.pos = c->pos, l.rot = c->rot;
+ for (int i = 0; i < 4; i++) l.susp[i] = c->w[i].susp, l.spin[i] = c->w[i].rotation;
+ l.steer = c->w[0].steering;
+ l.brake = c->brake_light;
+ return l;
+}
+
+car_look car_look_mix(const car_look *a, const car_look *b, float t) {
+ car_look l = *b;
+ l.pos = vadd(a->pos, vmul(vsub(b->pos, a->pos), t));
+ qt q0 = a->rot, q1 = b->rot;
+ if (q0.x * q1.x + q0.y * q1.y + q0.z * q1.z + q0.w * q1.w < 0.0f) q1.x = -q1.x, q1.y = -q1.y, q1.z = -q1.z, q1.w = -q1.w;
+ qt q = {q0.x + (q1.x - q0.x) * t, q0.y + (q1.y - q0.y) * t, q0.z + (q1.z - q0.z) * t, q0.w + (q1.w - q0.w) * t};
+ l.rot = qnorm(q);
+ for (int i = 0; i < 4; i++) l.susp[i] = a->susp[i] + (b->susp[i] - a->susp[i]) * t;
+ return l;
+}
+
+static void car_mesh(const int16_t *vtx, int nv, const uint8_t *tr, int nt, const float M[3][3], v3 t, const m3 *wrot,
+ const float mat[7][3], const car_look *c) {
+ const float s = 1.0f / PT_FIX;
+ for (int k = 0; k < nv; k++) {
+ float x = (float)vtx[k * 3] * s, y = (float)vtx[k * 3 + 1] * s, z = (float)vtx[k * 3 + 2] * s;
+ cv[k] = V3(M[0][0] * x + M[0][1] * y + M[0][2] * z + t.x, M[1][0] * x + M[1][1] * y + M[1][2] * z + t.y,
+ M[2][0] * x + M[2][1] * y + M[2][2] * z + t.z);
+ }
+ for (int k = 0; k < nt; k++, tr += 4) {
+ v3 a = cv[tr[0]], b = cv[tr[1]], cc = cv[tr[2]];
+ v3 n = vcross(vsub(b, a), vsub(cc, a));
+ if (vdot(n, a) >= 0.0f) continue;
+ /* the world's normal for the light */
+ v3 cn = vnorm(n);
+ v3 wn = V3(cn.x * C.right.x + cn.y * C.up.x + cn.z * C.fwd.x, cn.x * C.right.y + cn.y * C.up.y + cn.z * C.fwd.y,
+ cn.x * C.right.z + cn.y * C.up.z + cn.z * C.fwd.z);
+ int m = tr[3];
+ uint16_t col;
+ if (m == PT_CM_BRAKELIGHT && c->brake) col = rgb565(255, 102, 77);
+ else col = lit(mat[m], wn, 0, 1.0f);
+ cam_tri(a, b, cc, col, c->ghost ? RK_GHOST : RK_SOLID);
+ }
+ (void)wrot;
+}
+
+static void mat_mul(float M[3][3], const m3 *rot) {
+ v3 ax[3] = {C.right, C.up, C.fwd};
+ for (int i = 0; i < 3; i++)
+ for (int j = 0; j < 3; j++) {
+ v3 col = mcolumn(rot, j);
+ M[i][j] = vdot(ax[i], col);
+ }
+}
+
+void rd_car(const car_look *c) {
+ float mat[7][3];
+ hex_lin(rd_paints[c->paint & 7], mat[PT_CM_MAIN]);
+ hex_lin(0xFFFFFF, mat[PT_CM_STRIPE]);
+ for (int i = 2; i < 7; i++)
+ for (int k = 0; k < 3; k++) mat[i][k] = (float)pt_carmat[(i - 2) * 3 + k] / 4096.0f;
+ hex_lin(0x131313, mat[PT_CM_METAL]);
+ hex_lin(0x666666, mat[PT_CM_RIM]);
+ if (c->ghost) {
+ for (int i = 0; i < 7; i++)
+ for (int k = 0; k < 3; k++) mat[i][k] = mat[i][k] * 0.4f + 0.35f;
+ }
+ m3 R;
+ qmat(c->rot, &R);
+ float M[3][3];
+ mat_mul(M, &R);
+ /* the body and the suspension: the model drawn at the body's transform, moved up by the mass offset */
+ v3 t = to_cam(vadd(c->pos, mrow(&R, V3(0.0f, CAR_MASS_OFFSET, 0.0f))));
+ if (t.z < -6.0f || t.z > far_d) return;
+ car_mesh(pt_body_v, PT_BODY_NV, pt_body_t, PT_BODY_NT, M, t, &R, mat, c);
+ if (!c->ghost) car_mesh(pt_extra_v, PT_EXTRA_NV, pt_extra_t, PT_EXTRA_NT, M, t, &R, mat, c);
+ /* the wheels: at their suspension's end, turned by the steering (front) and rolling */
+ for (int i = 0; i < 4; i++) {
+ v3 wp = vadd(c->pos, mrow(&R, vadd(car_wheel_pos[i], V3(0.0f, -c->susp[i], 0.0f))));
+ float st = i < 2 ? c->steer : 0.0f;
+ qt qs = {0.0f, pt_sinf(st * 0.5f), 0.0f, pt_cosf(st * 0.5f)};
+ qt qr = {pt_sinf(c->spin[i] * 0.5f), 0.0f, 0.0f, pt_cosf(c->spin[i] * 0.5f)};
+ m3 WR;
+ qmat(qmul(qmul(c->rot, qs), qr), &WR);
+ float WM[3][3];
+ mat_mul(WM, &WR);
+ car_mesh(pt_wheel_v, PT_WHEEL_NV, pt_wheel_t, PT_WHEEL_NT, WM, to_cam(wp), &WR, mat, c);
+ }
+}
+
+/* the car's shadow: its outline cast along the sun onto the ground under its wheels */
+void rd_shadow(const car_look *c, const car_t *phys) {
+ v3 n = V3(0.0f, 0.0f, 0.0f), p = n;
+ int k = 0;
+ for (int i = 0; i < 4; i++)
+ if (phys->w[i].contact) n = vadd(n, phys->w[i].normal), p = vadd(p, phys->w[i].point), k++;
+ if (!k) {
+ if (!phys->ground_on) return;
+ n = V3(0.0f, 1.0f, 0.0f);
+ p = V3(c->pos.x, 0.0f, c->pos.z);
+ } else {
+ n = vnorm(n), p = vmul(p, 1.0f / (float)k);
+ }
+ m3 R;
+ qmat(c->rot, &R);
+ static const float FOOT[6][2] = {{0.75f, 1.6f}, {0.55f, 1.9f}, {-0.55f, 1.9f}, {-0.75f, 1.6f}, {-0.75f, -1.9f}, {0.75f, -1.9f}};
+ v3 q[6];
+ float dn = vdot(sun, n);
+ if (dn < 0.2f) return;
+ for (int i = 0; i < 6; i++) {
+ v3 w = vadd(c->pos, mrow(&R, V3(FOOT[i][0], CAR_MASS_OFFSET - 0.2f, FOOT[i][1])));
+ /* along the sun onto the plane through p */
+ float d = vdot(vsub(w, p), n);
+ if (d < -0.5f) return;
+ v3 s = vsub(w, vmul(sun, d / dn));
+ q[i] = to_cam(vadd(s, vmul(n, 0.02f)));
+ }
+ for (int i = 1; i + 1 < 6; i++) cam_tri(q[0], q[i], q[i + 1], 0, RK_SHADOW);
+}
+
+void rd_showroom(int paint, float angle, int env) {
+ (void)paint, (void)angle, (void)env;
+}
+
+/* ------------------------------------------------------------------ drawing a strip */
+static uint16_t zbuf[SCREEN_W * STRIP_ROWS];
+
+static void background(int ys, int ye) {
+ /* the view ray's height, per 16 pixels: dir = right (x - CX)/f + up (CY - y)/f + forward */
+ float rx = C.right.y / focal, uy = C.up.y / focal, fy = C.fwd.y;
+ float r2[2] = {C.right.x, C.right.z};
+ (void)r2;
+ for (int y = ys; y < ye; y++) {
+ uint16_t *row = gfx_buf + (y - ys) * SCREEN_W;
+ float yy = CY - ((float)y + 0.5f);
+ float base = uy * yy + fy;
+ float prev_idx = 0.0f;
+ for (int x = 0; x <= SCREEN_W; x += 16) {
+ float xx = (float)x - CX;
+ float dy = rx * xx + base;
+ float len2 = (xx * xx + yy * yy) / (focal * focal) + 1.0f;
+ float n = dy / pt_sqrtf(len2);
+ float idx = (n * 127.5f + 127.5f);
+ if (x > 0) {
+ float d = (idx - prev_idx) / 16.0f, f = prev_idx;
+ for (int k = x - 16; k < x; k++, f += d) {
+ int i = (int)f;
+ row[k] = back_lut[i < 0 ? 0 : i > 255 ? 255 : i];
+ }
+ }
+ prev_idx = idx;
+ }
+ }
+}
+
+static void raster(const tri *t, int ys, int ye) {
+ float x0 = t->x[0] * (1.0f / 16), y0 = t->y[0] * (1.0f / 16), x1 = t->x[1] * (1.0f / 16), y1 = t->y[1] * (1.0f / 16),
+ x2 = t->x[2] * (1.0f / 16), y2 = t->y[2] * (1.0f / 16);
+ int ta = iceil(y0 - 0.5f), tb = iceil(y2 - 0.5f);
+ int ra = ta > ys ? ta : ys, rb = tb < ye ? tb : ye;
+ if (ra >= rb) return;
+ float w0 = t->w[0] * (1.0f / 65535), w1 = t->w[1] * (1.0f / 65535), w2 = t->w[2] * (1.0f / 65535);
+ float dx1 = x1 - x0, dy1 = y1 - y0, dx2 = x2 - x0, dy2 = y2 - y0;
+ float det = dx1 * dy2 - dx2 * dy1;
+ if (det > -0.01f && det < 0.01f) return;
+ float B = ((w1 - w0) * dy2 - (w2 - w0) * dy1) / det, Cc = (dx1 * (w2 - w0) - dx2 * (w1 - w0)) / det;
+ float s02 = (x2 - x0) / (y2 - y0);
+ float s01 = y1 > y0 ? (x1 - x0) / (y1 - y0) : 0, s12 = y2 > y1 ? (x2 - x1) / (y2 - y1) : 0;
+ uint16_t col = t->c;
+ int kind = t->kind;
+ for (int r = ra; r < rb; r++) {
+ float yc = (float)r + 0.5f;
+ float xl = x0 + (yc - y0) * s02;
+ float xs = yc < y1 ? x0 + (yc - y0) * s01 : x1 + (yc - y1) * s12;
+ float xa = xl < xs ? xl : xs, xb = xl < xs ? xs : xl;
+ xa = pt_clampf(xa, -1.0f, 321.0f), xb = pt_clampf(xb, -1.0f, 321.0f);
+ int pa = iceil(xa - 0.5f), pb = iceil(xb - 0.5f);
+ if (pa < 0) pa = 0;
+ if (pb > SCREEN_W) pb = SCREEN_W;
+ if (pb <= pa) continue;
+ uint16_t *p = gfx_buf + (r - ys) * SCREEN_W;
+ if (kind == RK_BACK) {
+ for (int x = pa; x < pb; x++) p[x] = col;
+ continue;
+ }
+ float wa = w0 + B * ((float)pa + 0.5f - x0) + Cc * (yc - y0);
+ float wb = w0 + B * ((float)pb - 0.5f - x0) + Cc * (yc - y0);
+ int32_t qa = (int32_t)(pt_clampf(wa, 0, 1) * 1073741824.0f), qb = (int32_t)(pt_clampf(wb, 0, 1) * 1073741824.0f);
+ int n = pb - pa;
+ int32_t dq = n > 1 ? (qb - qa) / (n - 1) : 0;
+ uint16_t *z = zbuf + (r - ys) * SCREEN_W;
+ if (kind == RK_SOLID) {
+ for (int x = pa; x < pb; x++, qa += dq) {
+ uint16_t q = (uint16_t)(qa >> 14 > 65535 ? 65535 : qa >> 14);
+ if (q > z[x]) z[x] = q, p[x] = col;
+ }
+ } else if (kind == RK_GHOST) {
+ for (int x = pa; x < pb; x++, qa += dq) {
+ if ((x + r) & 1) continue;
+ uint16_t q = (uint16_t)(qa >> 14 > 65535 ? 65535 : qa >> 14);
+ if (q > z[x]) p[x] = col;
+ }
+ } else {
+ for (int x = pa; x < pb; x++, qa += dq) {
+ int q = qa >> 14;
+ if (q + 24 >= z[x]) {
+ uint16_t c = p[x];
+ /* 60 % of the sun taken away: about five eighths of the colour */
+ p[x] = (uint16_t)(((c >> 1) & 0x7BEF) + ((c >> 3) & 0x18E3));
+ }
+ }
+ }
+ }
+}
+
+void rd_draw(void) {
+ int ys = gfx_y0, ye = gfx_y1;
+ rd_tris = ntris;
+ background(ys, ye);
+ memset(zbuf, 0, sizeof(uint16_t) * (size_t)SCREEN_W * (size_t)(ye - ys));
+ for (int pass = 0; pass < 3; pass++)
+ for (int i = 0; i < ntris; i++) {
+ const tri *t = &tris[i];
+ int k = t->kind == RK_BACK ? 0 : t->kind == RK_SHADOW ? 2 : 1;
+ if (k != pass) continue;
+ if (t->y[2] < ys * 16 - 8 || t->y[0] > ye * 16 + 8) continue;
+ raster(t, ys, ye);
+ }
+}
diff --git a/games/polytrack/src/render.h b/games/polytrack/src/render.h
new file mode 100644
index 00000000..14260a21
--- /dev/null
+++ b/games/polytrack/src/render.h
@@ -0,0 +1,43 @@
+/* The 3D picture (render.c): PolyTrack's sky, ground and mountains, the track's parts lit by its sun, the car, its
+ * shadow and the ghost, as flat triangles drawn with a depth buffer a strip of the screen at a time. */
+#ifndef PT_RENDER_H
+#define PT_RENDER_H
+#include "car.h"
+#include "pt.h"
+
+typedef struct {
+ v3 pos;
+ v3 right, up, fwd; /* the camera's axes in the world */
+ float fov; /* vertical, degrees */
+} camera_t;
+
+/* the chase camera (main.bundle.js): a point trailing 5.5 m behind the car on a rope, 2 m up the car's (eased) up */
+typedef struct {
+ v3 anchor, up;
+} chase_t;
+void chase_reset(chase_t *ch, v3 pos, qt rot);
+void chase_update(chase_t *ch, camera_t *cam, float dt, v3 pos, qt rot, float kmh);
+void cockpit_camera(camera_t *cam, v3 pos, qt rot, float kmh);
+
+/* what a car looks like this frame */
+typedef struct {
+ v3 pos;
+ qt rot;
+ float susp[4], steer, spin[4];
+ uint8_t brake, ghost, paint;
+} car_look;
+car_look car_look_of(const car_t *c);
+car_look car_look_mix(const car_look *a, const car_look *b, float t);
+
+extern const uint32_t rd_paints[8]; /* the car colours */
+extern int rd_detail; /* 0..3: how far it draws; lowered when a frame is slow */
+extern int rd_tris, rd_dropped;
+
+void rd_begin(const camera_t *cam, int env, int sun, bool sky_only);
+void rd_track(void);
+void rd_car(const car_look *c);
+void rd_shadow(const car_look *c, const car_t *phys);
+void rd_draw(void); /* the strip gfx_y0..gfx_y1 of gfx_buf */
+/* a car alone on a turntable (the menus) */
+void rd_showroom(int paint, float angle, int env);
+#endif
diff --git a/games/polygt3d/src/save.c b/games/polytrack/src/save.c
similarity index 100%
rename from games/polygt3d/src/save.c
rename to games/polytrack/src/save.c
diff --git a/games/polygt3d/src/save.h b/games/polytrack/src/save.h
similarity index 100%
rename from games/polygt3d/src/save.h
rename to games/polytrack/src/save.h
diff --git a/games/polytrack/src/world.c b/games/polytrack/src/world.c
new file mode 100644
index 00000000..0795339b
--- /dev/null
+++ b/games/polytrack/src/world.c
@@ -0,0 +1,232 @@
+/* The track (world.h): the official track's parts from data.c, placed and turned on the 5 m grid. Every position is
+ * a whole number of 1/256 m plus a multiple of 5 m, so the physics triangles come out exact. */
+#include <string.h>
+#include "world.h"
+
+world_t W;
+
+static int floordiv(int a, int b) { return a >= 0 ? a / b : -((-a + b - 1) / b); }
+static int ffloor(float f) {
+ int i = (int)f;
+ return (float)i > f ? i - 1 : i;
+}
+
+void world_load(int track) {
+ memset(&W, 0, sizeof W);
+ W.id = track;
+ W.t = &pt_tracks[track];
+ const pt_track *t = W.t;
+ /* the mountains (createMountainVertices): radius from the track's extent in tiles */
+ float dx = (float)(t->bx1 - t->bx0) * PT_PART_SIZE / 2.0f * 1.41421356f;
+ float dz = (float)(t->bz1 - t->bz0) * PT_PART_SIZE / 2.0f * 1.41421356f;
+ float r = pt_maxf(200.0f, 160.0f + pt_maxf(dx, dz));
+ W.mr = r;
+ W.mx = ((float)t->bx0 + (float)(t->bx1 - t->bx0) / 2.0f) * PT_PART_SIZE;
+ W.mz = ((float)t->bz0 + (float)(t->bz1 - t->bz0) / 2.0f) * PT_PART_SIZE;
+ int seg = (int)(r / 10.0f);
+ if (seg > 160) seg = 160;
+ W.mseg = seg;
+ int k = 0;
+#define NEXT() (k = (k + 1) % PT_MOUNTAIN_N, pt_mountain[k])
+ for (int e = 0; e < seg; e++)
+ for (int n = 0; n < 8; n++) {
+ float h;
+ if (n == 0 || n == 7) h = 0.0f;
+ else if (n == 1 && NEXT() < 0.5f) h = 0.0f;
+ else h = NEXT();
+ W.mh[e][n] = (uint8_t)(h * 255.0f + 0.5f);
+ }
+#undef NEXT
+}
+
+const pt_inst *world_inst(int i) { return &pt_insts[W.t->inst + i]; }
+const int8_t *world_orient(const pt_inst *in) { return &pt_orient[(in->orient >> 2) * 36 + (in->orient & 3) * 9]; }
+v3 world_origin(const pt_inst *in) {
+ return V3((float)in->x * PT_PART_SIZE, (float)in->y * PT_PART_SIZE, (float)in->z * PT_PART_SIZE);
+}
+static v3 orient_apply(const int8_t *o, v3 p) {
+ return V3((float)o[0] * p.x + (float)o[1] * p.y + (float)o[2] * p.z, (float)o[3] * p.x + (float)o[4] * p.y + (float)o[5] * p.z,
+ (float)o[6] * p.x + (float)o[7] * p.y + (float)o[8] * p.z);
+}
+static v3 orient_back(const int8_t *o, v3 p) {
+ return V3((float)o[0] * p.x + (float)o[3] * p.y + (float)o[6] * p.z, (float)o[1] * p.x + (float)o[4] * p.y + (float)o[7] * p.z,
+ (float)o[2] * p.x + (float)o[5] * p.y + (float)o[8] * p.z);
+}
+v3 world_apply(const pt_inst *in, v3 local) { return vadd(orient_apply(world_orient(in), local), world_origin(in)); }
+
+static void box_apply(const int8_t *o, v3 o0, v3 lo, v3 hi, v3 *wlo, v3 *whi) {
+ v3 c = vmul(vadd(lo, hi), 0.5f), h = vmul(vsub(hi, lo), 0.5f);
+ v3 wc = vadd(orient_apply(o, c), o0);
+ v3 wh = V3(pt_absf((float)o[0]) * h.x + pt_absf((float)o[1]) * h.y + pt_absf((float)o[2]) * h.z,
+ pt_absf((float)o[3]) * h.x + pt_absf((float)o[4]) * h.y + pt_absf((float)o[5]) * h.z,
+ pt_absf((float)o[6]) * h.x + pt_absf((float)o[7]) * h.y + pt_absf((float)o[8]) * h.z);
+ *wlo = vsub(wc, wh), *whi = vadd(wc, wh);
+}
+
+void world_box(const pt_inst *in, v3 *lo, v3 *hi) {
+ const pt_part *p = &pt_parts[in->part];
+ v3 l = V3((float)p->lo[0] / PT_FIX, (float)p->lo[1] / PT_FIX, (float)p->lo[2] / PT_FIX);
+ v3 h = V3((float)p->hi[0] / PT_FIX, (float)p->hi[1] / PT_FIX, (float)p->hi[2] / PT_FIX);
+ box_apply(world_orient(in), world_origin(in), l, h, lo, hi);
+}
+
+void world_query(v3 lo, v3 hi, void (*f)(int i, void *ctx), void *ctx) {
+ const pt_track *t = W.t;
+ float reach = (float)t->reach;
+ int cx0 = ffloor((lo.x - reach) / 40.0f) - t->gx0, cx1 = ffloor((hi.x + reach) / 40.0f) - t->gx0;
+ int cz0 = ffloor((lo.z - reach) / 40.0f) - t->gz0, cz1 = ffloor((hi.z + reach) / 40.0f) - t->gz0;
+ if (cx0 < 0) cx0 = 0;
+ if (cz0 < 0) cz0 = 0;
+ if (cx1 >= t->gw) cx1 = t->gw - 1;
+ if (cz1 >= t->gh) cz1 = t->gh - 1;
+ for (int cz = cz0; cz <= cz1; cz++)
+ for (int cx = cx0; cx <= cx1; cx++) {
+ int c = cz * t->gw + cx;
+ for (int i = pt_tcells[t->cell + c]; i < pt_tcells[t->cell + c + 1]; i++) {
+ v3 a, b;
+ world_box(world_inst(i), &a, &b);
+ if (a.x > hi.x || b.x < lo.x || a.y > hi.y || b.y < lo.y || a.z > hi.z || b.z < lo.z) continue;
+ f(i, ctx);
+ }
+ }
+}
+
+/* ------------------------------------------------------------------ gathering physics triangles */
+typedef struct {
+ v3 lo, hi;
+ wtri *out;
+ int n, max;
+} gather_t;
+static uint8_t seen[(4096 + 7) / 8];
+
+static void add_tri(gather_t *g, v3 a, v3 b, v3 c) {
+ float x0 = pt_minf(a.x, pt_minf(b.x, c.x)), x1 = pt_maxf(a.x, pt_maxf(b.x, c.x));
+ float y0 = pt_minf(a.y, pt_minf(b.y, c.y)), y1 = pt_maxf(a.y, pt_maxf(b.y, c.y));
+ float z0 = pt_minf(a.z, pt_minf(b.z, c.z)), z1 = pt_maxf(a.z, pt_maxf(b.z, c.z));
+ if (x0 > g->hi.x || x1 < g->lo.x || y0 > g->hi.y || y1 < g->lo.y || z0 > g->hi.z || z1 < g->lo.z) return;
+ v3 n = vcross(vsub(b, a), vsub(c, a));
+ float l = vlen(n);
+ if (!(l > 1e-9f)) return;
+ if (g->n >= g->max) return;
+ wtri *w = &g->out[g->n++];
+ w->a = a, w->b = b, w->c = c;
+ w->n = vmul(n, 1.0f / l);
+ w->d = vdot(w->n, a);
+}
+
+static void gather_inst(int i, void *ctx) {
+ gather_t *g = ctx;
+ const pt_inst *in = world_inst(i);
+ const pt_part *p = &pt_parts[in->part];
+ const int8_t *o = world_orient(in);
+ v3 o0 = world_origin(in);
+ /* the query box in the part's space */
+ v3 a = orient_back(o, vsub(g->lo, o0)), b = orient_back(o, vsub(g->hi, o0));
+ v3 l = V3(pt_minf(a.x, b.x), pt_minf(a.y, b.y), pt_minf(a.z, b.z));
+ float px = (float)p->lo[0] / PT_FIX, pz = (float)p->lo[2] / PT_FIX;
+ int gx0 = ffloor((l.x - px) / 5.0f), gx1 = ffloor((pt_maxf(a.x, b.x) - px) / 5.0f);
+ int gz0 = ffloor((l.z - pz) / 5.0f), gz1 = ffloor((pt_maxf(a.z, b.z) - pz) / 5.0f);
+ if (gx0 < 0) gx0 = 0;
+ if (gz0 < 0) gz0 = 0;
+ if (gx1 >= p->gx) gx1 = p->gx - 1;
+ if (gz1 >= p->gz) gz1 = p->gz - 1;
+ if (gx0 > gx1 || gz0 > gz1) return;
+ uint32_t mask = p->simple ? pt_masks[W.t->mask + in->mask] : 0xFFFFFFFFu;
+ memset(seen, 0, (size_t)(p->ncol + 7) / 8);
+ const int16_t *pool = &pt_pool[p->vbase * 3];
+ for (int gz = gz0; gz <= gz1; gz++)
+ for (int gx = gx0; gx <= gx1; gx++) {
+ int c = p->grid + gz * p->gx + gx;
+ for (uint32_t j = pt_cgrid[c]; j < pt_cgrid[c + 1]; j++) {
+ int ti = pt_cgidx[j];
+ if (seen[ti >> 3] & (1 << (ti & 7))) continue;
+ seen[ti >> 3] |= (uint8_t)(1 << (ti & 7));
+ if (ti < 32 && !(mask & (1u << ti))) continue;
+ const uint16_t *tr = &pt_ctri[(p->col + ti) * 3];
+ v3 v[3];
+ for (int k = 0; k < 3; k++) {
+ const int16_t *q = &pool[tr[k] * 3];
+ v3 lp = V3((float)q[0] / PT_FIX, (float)q[1] / PT_FIX, (float)q[2] / PT_FIX);
+ v[k] = vadd(orient_apply(o, lp), o0);
+ }
+ add_tri(g, v[0], v[1], v[2]);
+ }
+ }
+}
+
+static v3 mountain_point(int e, int k) {
+ int s = W.mseg;
+ float ang = (float)(e % s) / (float)s * 2.0f * PT_PI;
+ float rad = W.mr + 100.0f * (float)k;
+ float h = (float)W.mh[e % s][k] * (100.0f / 255.0f);
+ return V3(W.mx + pt_cosf(ang) * rad, h, W.mz + pt_sinf(ang) * rad);
+}
+
+static void gather_mountains(gather_t *g) {
+ float cx = (g->lo.x + g->hi.x) * 0.5f - W.mx, cz = (g->lo.z + g->hi.z) * 0.5f - W.mz;
+ float half = pt_maxf(g->hi.x - g->lo.x, g->hi.z - g->lo.z) * 0.5f * 1.5f;
+ float d = pt_sqrtf(cx * cx + cz * cz);
+ if (d + half < W.mr || d - half > W.mr + 700.0f) return;
+ float ang = pt_atan2f(cz, cx);
+ if (ang < 0.0f) ang += 2.0f * PT_PI;
+ int s = W.mseg;
+ int e0 = (int)(ang / (2.0f * PT_PI) * (float)s);
+ int k0 = ffloor((d - half - W.mr) / 100.0f), k1 = ffloor((d + half - W.mr) / 100.0f);
+ if (k0 < 0) k0 = 0;
+ if (k1 > 6) k1 = 6;
+ for (int e = e0 - 1; e <= e0 + 1; e++) {
+ int ee = (e + s) % s;
+ for (int k = k0; k <= k1; k++) {
+ v3 a = mountain_point(ee, k), b = mountain_point(ee + 1, k), c = mountain_point(ee + 1, k + 1),
+ dd = mountain_point(ee, k + 1);
+ add_tri(g, a, b, c);
+ add_tri(g, a, c, dd);
+ }
+ }
+}
+
+int world_gather(v3 lo, v3 hi, wtri *out, int max) {
+ gather_t g = {lo, hi, out, 0, max};
+ world_query(lo, hi, gather_inst, &g);
+ gather_mountains(&g);
+ return g.n;
+}
+
+float mountain_height(float x, float z) {
+ (void)x, (void)z;
+ return 0.0f;
+}
+
+/* ------------------------------------------------------------------ start and detectors */
+qt world_quat(int rot, int axis) {
+ const int8_t *o = &pt_orient[axis * 36 + rot * 9];
+ m3 m;
+ for (int r = 0; r < 3; r++) m.r[r] = V3((float)o[r * 3], (float)o[r * 3 + 1], (float)o[r * 3 + 2]);
+ return mat_quat(&m);
+}
+
+void world_start(v3 *pos, qt *rot) {
+ const pt_inst *in = world_inst(W.t->start);
+ const pt_part *p = &pt_parts[in->part];
+ const int8_t *o = world_orient(in);
+ /* the part's turn, then half a turn round y */
+ m3 m;
+ for (int r = 0; r < 3; r++) m.r[r] = V3(-(float)o[r * 3], (float)o[r * 3 + 1], -(float)o[r * 3 + 2]);
+ *rot = mat_quat(&m);
+ v3 off = mrow(&m, V3(p->start[0], p->start[1], p->start[2]));
+ *pos = vadd(world_origin(in), off);
+}
+
+void world_detector(int k, detector_t *d) {
+ const pt_spec *s = &pt_specs[W.t->spec + k];
+ const pt_inst *in = world_inst(s->inst);
+ const pt_part *p = &pt_parts[in->part];
+ const int8_t *o = world_orient(in);
+ v3 c = V3(p->dc[0], p->dc[1], p->dc[2]), h = V3(p->ds[0] * 0.5f, p->ds[1] * 0.5f, p->ds[2] * 0.5f);
+ box_apply(o, world_origin(in), vsub(c, h), vadd(c, h), &d->lo, &d->hi);
+ d->order = s->order;
+ d->rot = in->orient & 3, d->axis = (uint8_t)(in->orient >> 2);
+ d->wide = pt_part_id[in->part] == 65 || pt_part_id[in->part] == 77;
+ d->part = world_origin(in);
+ (void)floordiv;
+}
diff --git a/games/polytrack/src/world.h b/games/polytrack/src/world.h
new file mode 100644
index 00000000..335b9bc9
--- /dev/null
+++ b/games/polytrack/src/world.h
@@ -0,0 +1,56 @@
+/* The track as the physics and the picture see it (world.c): parts placed on the 5 m grid, their triangles near a
+ * point, the start, the checkpoints and the finish, the ring of mountains. */
+#ifndef PT_WORLD_H
+#define PT_WORLD_H
+#include "data.h"
+#include "pt.h"
+
+#define PT_PART_SIZE 5.0f
+#define PT_CACHE 256 /* physics triangles kept around the car */
+
+enum { ENV_SUMMER, ENV_WINTER, ENV_DESERT };
+
+/* a triangle of the world, with its plane */
+typedef struct {
+ v3 a, b, c;
+ v3 n; /* unit normal (front face: a, b, c counter-clockwise) */
+ float d; /* n . a */
+} wtri;
+
+typedef struct {
+ const pt_track *t;
+ int id;
+ /* the mountains: a ring round the track (main.bundle.js, createMountainVertices) */
+ float mx, mz, mr; /* centre and inner radius */
+ int mseg; /* segments round */
+ uint8_t mh[160][8]; /* heights, x 100/255 m */
+ int nmh;
+} world_t;
+
+extern world_t W;
+
+void world_load(int track);
+const pt_inst *world_inst(int i); /* i < W.t->ninst */
+/* an instance's orientation (3x3, whole numbers) and its position (metres) */
+const int8_t *world_orient(const pt_inst *in);
+v3 world_origin(const pt_inst *in);
+v3 world_apply(const pt_inst *in, v3 local); /* part space -> world */
+void world_box(const pt_inst *in, v3 *lo, v3 *hi); /* the instance's world box */
+/* calls f for every instance whose box meets [lo, hi] */
+void world_query(v3 lo, v3 hi, void (*f)(int i, void *ctx), void *ctx);
+/* physics triangles meeting the box (and the mountains near it); returns how many (at most max) */
+int world_gather(v3 lo, v3 hi, wtri *out, int max);
+
+/* the start: where the car is put and how it points */
+void world_start(v3 *pos, qt *rot);
+/* checkpoint detectors: n = pt_track.ncp, then nfin finishes */
+typedef struct {
+ v3 lo, hi; /* world box */
+ uint16_t order; /* 0xFFFF: a finish */
+ uint8_t rot, axis, wide;
+ v3 part; /* the part's position (metres) */
+} detector_t;
+void world_detector(int k, detector_t *d);
+qt world_quat(int rot, int axis);
+float mountain_height(float x, float z); /* (for the picture) */
+#endif
diff --git a/games/polygt3d/tests/play.c b/games/polytrack/tests/play.c
similarity index 100%
rename from games/polygt3d/tests/play.c
rename to games/polytrack/tests/play.c
diff --git a/games/polytrack/tools/pack.py b/games/polytrack/tools/pack.py
new file mode 100644
index 00000000..0045b8f7
--- /dev/null
+++ b/games/polytrack/tools/pack.py
@@ -0,0 +1,1137 @@
+#!/usr/bin/env python3
+"""PolyTrack's data for the calculator, read from Kodub's web build of PolyTrack 0.6.3 (kodub.itch.io/polytrack):
+the 17 official tracks, the track parts' models (render and physics), the car, the materials and the track maps.
+
+ python3 tools/pack.py WEB WEB: the web build's folder (index.html, main.bundle.js, models/, tracks/)
+
+Writes src/data.c and src/data.h. Needs numpy, DracoPy (the models are Draco compressed) and Pillow. The web build
+is only read as data: its JavaScript is parsed with regular expressions, never run.
+"""
+import collections
+import heapq
+import json
+import math
+import os
+import re
+import struct
+import sys
+import zlib
+
+import numpy as np
+import DracoPy
+
+HERE = os.path.dirname(os.path.abspath(__file__))
+SRC = os.path.join(HERE, '..', 'src')
+FIX = 256.0 # vertex units a metre
+TRACKS = ['summer%d' % i for i in range(1, 8)] + ['winter%d' % i for i in range(1, 6)] + \
+ ['desert%d' % i for i in range(1, 6)]
+SCENES = {'Road': 'road', 'RoadWide': 'road_wide', 'Planes': 'planes', 'Blocks': 'blocks', 'Pillar': 'pillar',
+ 'Signs': 'signs', 'WallTrack': 'wall_track'}
+MATERIALS = ['Road', 'RoadEdgeRed', 'RoadEdgeWhite', 'RoadBarrier', 'FinishWhite', 'FinishBlack', 'Finish', 'StartLine',
+ 'Checkpoint', 'PlaneWall', 'PlaneWallDetail', 'BlockSurface', 'Pillar', 'PillarEdge', 'WallTrack',
+ 'WallTrackBottom', 'WallTrackSides', 'SignRed', 'SignBlack', 'SignYellow', 'RoadEdge']
+CAR_MATERIALS = ['Main', 'Stripe', 'AirIntake', 'Metal', 'BrakeLight', 'Tire', 'Rim']
+EDGE = {'RoadEdgeRed': 'RoadEdge', 'RoadEdgeWhite': 'RoadEdge'}
+CHUNK = 20.0 # parts are cut into 20 m squares for culling and detail
+CELL = 5.0 # the physics grid inside a part
+TCELL = 8 # the track grid: 8 tiles (40 m)
+
+
+# ------------------------------------------------------------------ glb (Draco)
+def load_glb(path):
+ b = open(path, 'rb').read()
+ _, _, length = struct.unpack_from('<III', b, 0)
+ off, js, binc = 12, None, None
+ while off < length:
+ cl, ct = struct.unpack_from('<II', b, off)
+ off += 8
+ if ct == 0x4E4F534A:
+ js = json.loads(b[off:off + cl])
+ elif ct == 0x004E4942:
+ binc = b[off:off + cl]
+ off += cl
+ return js, binc
+
+
+def quat_mat(q):
+ x, y, z, w = q
+ return np.array([[1 - 2 * (y * y + z * z), 2 * (x * y - z * w), 2 * (x * z + y * w)],
+ [2 * (x * y + z * w), 1 - 2 * (x * x + z * z), 2 * (y * z - x * w)],
+ [2 * (x * z - y * w), 2 * (y * z + x * w), 1 - 2 * (x * x + y * y)]])
+
+
+def glb_meshes(path):
+ """name -> list of primitives (material, local points, points with the node's transform, faces, uv)"""
+ js, binc = load_glb(path)
+ mats = [m.get('name') for m in js.get('materials', [])]
+ out = {}
+ for n in js['nodes']:
+ if 'mesh' not in n:
+ continue
+ T = np.array(n.get('translation', [0, 0, 0]), float)
+ R = quat_mat(n.get('rotation', [0, 0, 0, 1]))
+ S = np.array(n.get('scale', [1, 1, 1]), float)
+ prims = []
+ for pr in js['meshes'][n['mesh']]['primitives']:
+ ext = pr['extensions']['KHR_draco_mesh_compression']
+ bv = js['bufferViews'][ext['bufferView']]
+ o = bv.get('byteOffset', 0)
+ m = DracoPy.decode(binc[o:o + bv['byteLength']])
+ pts = np.array(m.points, float).reshape(-1, 3)
+ faces = np.array(m.faces, int).reshape(-1, 3)
+ uv = np.array(m.tex_coord, float).reshape(-1, 2) if m.tex_coord is not None and len(m.tex_coord) else None
+ world = (R @ (pts * S).T).T + T
+ prims.append(dict(material=mats[pr['material']] if pr.get('material') is not None else None,
+ local=pts, verts=world, faces=faces, uv=uv))
+ out[n['name']] = prims
+ return out
+
+
+class Models:
+ def __init__(self, web):
+ self.web, self.cache = web, {}
+
+ def scene(self, name):
+ if name not in self.cache:
+ self.cache[name] = glb_meshes(os.path.join(self.web, 'models', SCENES.get(name, name) + '.glb'))
+ return self.cache[name]
+
+
+# ------------------------------------------------------------------ the bundle
+def module(src, mid):
+ i = src.find(',' + mid + ':(e,t,n)=>')
+ if i < 0:
+ raise SystemExit('module %s not found in main.bundle.js' % mid)
+ k = src.find('{', i)
+ d = 0
+ while True:
+ c = src[k]
+ if c == '{':
+ d += 1
+ elif c == '}':
+ d -= 1
+ if d == 0:
+ break
+ elif c in '"\'`':
+ q = c
+ k += 1
+ while src[k] != q:
+ k += 2 if src[k] == '\\' else 1
+ k += 1
+ return src[i:k + 1]
+
+
+NUM = r'-?(?:\d+\.?\d*|\.\d+)(?:e-?\d+)?'
+
+
+def nums(s):
+ return [float(x) for x in re.findall(NUM, s)]
+
+
+def split_top(s):
+ out, d, cur, q = [], 0, '', None
+ for ch in s:
+ if q:
+ cur += ch
+ if ch == q:
+ q = None
+ continue
+ if ch in '"\'':
+ q = ch
+ if ch in '([{':
+ d += 1
+ if ch in ')]}':
+ d -= 1
+ if ch == ',' and d == 0:
+ out.append(cur)
+ cur = ''
+ else:
+ cur += ch
+ if cur:
+ out.append(cur)
+ return out
+
+
+def inner(s):
+ return nums(re.search(r'\(([^()]*)\)', s).group(1))
+
+
+def parse_opts(s):
+ o = {}
+ for k in ('flipX', 'flipY', 'flipZ'):
+ if k + ':!0' in s:
+ o[k] = True
+ m = re.search(r'offset:new i\.Pq0\(([^)]*)\)', s)
+ if m:
+ o['offset'] = nums(m.group(1))
+ m = re.search(r'scale:new i\.Pq0\(([^)]*)\)', s)
+ if m:
+ o['scale'] = nums(m.group(1))
+ m = re.search(r'setFromEuler\(new i\.O9p\(([^)]*)\)\)', s)
+ if m:
+ o['euler'] = nums(m.group(1))
+ return o
+
+
+def part_table(src):
+ names = {int(v): k for k, v in re.findall(r'\[e\.(\w+)=(\d+)\]', module(src, '494'))}
+ byname = {v: k for k, v in names.items()}
+ m = module(src, '2600')
+ body = m[m.find('const u=[') + 9:]
+ parts = {}
+ for item in re.finditer(r'new d\("([0-9a-f]{64})",', body):
+ k, d, q = item.end(), 1, None
+ while d:
+ ch = body[k]
+ if q:
+ if ch == q:
+ q = None
+ elif ch in '"\'':
+ q = ch
+ elif ch == '(':
+ d += 1
+ elif ch == ')':
+ d -= 1
+ k += 1
+ args = split_top(body[item.end():k - 1])
+ name = args[1].split('.')[-1]
+ models = [(mm.group(1), mm.group(2), parse_opts(mm.group(3) or ''))
+ for mm in re.finditer(r'\["(\w+)","(\w+)"(,\{.*?\})?\]', args[2])]
+ det = start = None
+ if len(args) > 5 and args[5] != 'null':
+ dm = re.search(r'type:o\.A\.(\w+),center:\[([^\]]*)\],size:\[([^\]]*)\]', args[5])
+ det = dict(type=dm.group(1), center=nums(dm.group(2)), size=nums(dm.group(3)))
+ if len(args) > 6:
+ start = inner(args[6])
+ parts[byname[name]] = dict(id=byname[name], name=name, cat=args[0].split('.')[-1], models=models,
+ palette=args[3].strip(), tiles=json.loads(args[4]), detector=det, start=start)
+ colors = {}
+ for env, block in (('Winter', 'Winter'), ('Desert', 'Desert')):
+ mm = re.search(r'id:a\.A\.' + block + r',colors:\{([^}]*)\}', m)
+ colors[env] = dict(re.findall(r'(\w+):"#([0-9a-f]{6})"', mm.group(1)))
+ custom = re.findall(r'id:a\.A\.Custom(\d),colors:\{BlockSurface:"#([0-9a-f]{6})"\}', m)
+ colors['Custom'] = [c for _, c in sorted(custom)]
+ return names, parts, colors
+
+
+def orient_table(src):
+ i = src.find(',5494:(e,t,n)=>')
+ seg = src[i:i + 3000]
+ seg = seg[seg.find('const a=[') + 9:]
+ rows = []
+ for row in re.finditer(r'\[((?:new i\.PTz\([^)]*\),?)+)\]', seg):
+ rows.append([nums(q) for q in re.findall(r'new i\.PTz\(([^)]*)\)', row.group(1))])
+ if len(rows) == 6:
+ break
+ mats = []
+ for axis in range(6):
+ for rot in range(4):
+ M = np.round(quat_mat(rows[axis][rot])).astype(int)
+ mats.append(M)
+ return mats, rows
+
+
+def mountain_table(src):
+ i = src.find('a={value:[.12047764760664692')
+ seg = src[i + 10:src.find(']}', i)]
+ return nums(seg)
+
+
+# ------------------------------------------------------------------ part meshes
+def euler_quat(e):
+ x, y, z = e
+ c1, c2, c3 = math.cos(x / 2), math.cos(y / 2), math.cos(z / 2)
+ s1, s2, s3 = math.sin(x / 2), math.sin(y / 2), math.sin(z / 2)
+ return (s1 * c2 * c3 + c1 * s2 * s3, c1 * s2 * c3 - s1 * c2 * s3, c1 * c2 * s3 + s1 * s2 * c3,
+ c1 * c2 * c3 - s1 * s2 * s3)
+
+
+def build_part(models, part):
+ """The part's triangles, as the game merges its models: [(material, a, b, c)] in metres, part space."""
+ tris = []
+ for scene, node, o in part['models']:
+ prims = models.scene(scene)[node]
+ p = max(pr['verts'][:, 1].max() for pr in prims) if o.get('flipY') else 0.0
+ for pr in prims:
+ v = pr['verts'].copy()
+ f = pr['faces'].copy()
+ if 'scale' in o:
+ v = v * np.array(o['scale'])
+ if 'euler' in o:
+ v = (quat_mat(euler_quat(o['euler'])) @ v.T).T
+ flip = [o.get('flipX'), o.get('flipY'), o.get('flipZ')]
+ v = v * np.array([-1.0 if k else 1.0 for k in flip])
+ if any(flip):
+ f = f[:, [0, 2, 1]]
+ off = np.array(o.get('offset', [0, 0, 0]), float)
+ if o.get('flipY'):
+ off = off + np.array([0, p, 0])
+ v = v + off
+ for a, b, c in f:
+ tris.append((pr['material'], v[a], v[b], v[c]))
+ return tris
+
+
+# ------------------------------------------------------------------ simplification (quadric edge collapse)
+QW = 1e-4
+
+
+def weld(tris, matmap=None):
+ idx, verts, faces = {}, [], []
+ for mat, *vs in tris:
+ if matmap:
+ mat = matmap.get(mat, mat)
+ ids = []
+ for v in vs:
+ k = (round(v[0] / QW), round(v[1] / QW), round(v[2] / QW))
+ if k not in idx:
+ idx[k] = len(verts)
+ verts.append(np.array(v, float))
+ ids.append(idx[k])
+ if len(set(ids)) == 3:
+ faces.append([mat, ids[0], ids[1], ids[2]])
+ return np.array(verts), faces
+
+
+def plane(V, f):
+ a, b, c = V[f[1]], V[f[2]], V[f[3]]
+ n = np.cross(b - a, c - a)
+ l = np.linalg.norm(n)
+ if l < 1e-12:
+ return None, 0.0
+ n = n / l
+ return np.append(n, -np.dot(n, a)), l / 2
+
+
+def simplify(tris, tol, matmap=None, keep_materials=True, boundary_weight=100.0):
+ """Garland-Heckbert edge collapse onto existing vertices (so planar surfaces stay exact), keeping open and
+ material boundaries. tol: the error allowed (metres)."""
+ V, F = weld(tris, matmap)
+ if not keep_materials:
+ for f in F:
+ f[0] = 'x'
+ nv = len(V)
+ Qv = [np.zeros((4, 4)) for _ in range(nv)]
+ vf = [set() for _ in range(nv)]
+ alive = [True] * len(F)
+ for i, f in enumerate(F):
+ p, _ = plane(V, f)
+ for k in (1, 2, 3):
+ vf[f[k]].add(i)
+ if p is not None:
+ K = np.outer(p, p)
+ for k in (1, 2, 3):
+ Qv[f[k]] += K
+ emap = collections.defaultdict(list)
+ for i, f in enumerate(F):
+ for a, b in ((f[1], f[2]), (f[2], f[3]), (f[3], f[1])):
+ emap[(min(a, b), max(a, b))].append(i)
+ for (a, b), fs in emap.items():
+ if len(fs) == 1 or len({F[i][0] for i in fs}) > 1:
+ for i in fs:
+ p, _ = plane(V, F[i])
+ if p is None:
+ continue
+ n2 = np.cross(V[b] - V[a], p[:3])
+ l = np.linalg.norm(n2)
+ if l < 1e-12:
+ continue
+ n2 /= l
+ p2 = np.append(n2, -np.dot(n2, V[a]))
+ K = np.outer(p2, p2) * boundary_weight
+ Qv[a] += K
+ Qv[b] += K
+ ver = [0] * nv
+ gone = [False] * nv
+
+ def cost(u, v):
+ Qs = Qv[u] + Qv[v]
+ best = None
+ for t, w in ((v, u), (u, v)):
+ x = np.append(V[t], 1.0)
+ c = float(x @ Qs @ x)
+ if best is None or c < best[0] - 1e-15:
+ best = (c, w, t)
+ return best
+
+ heap = []
+ for (a, b) in sorted(emap):
+ heap.append((cost(a, b)[0], a, b, 0, 0))
+ heapq.heapify(heap)
+ lim = tol * tol
+
+ def neighbors(x):
+ s = set()
+ for i in vf[x]:
+ if alive[i]:
+ s.update(F[i][1:])
+ s.discard(x)
+ return s
+
+ while heap:
+ c, a, b, va, vb = heapq.heappop(heap)
+ if c > lim:
+ break
+ if gone[a] or gone[b] or ver[a] != va or ver[b] != vb:
+ continue
+ c2, w, t = cost(a, b)
+ if c2 > lim:
+ continue
+ shared = [i for i in vf[w] if alive[i] and t in F[i][1:]]
+ if not shared or len(neighbors(w) & neighbors(t)) != len(shared):
+ continue
+ ok = True
+ for i in vf[w]:
+ if not alive[i] or t in F[i][1:]:
+ continue
+ f = F[i]
+ old, _ = plane(V, f)
+ if old is None:
+ continue # a sliver already: it may stay one, or go
+ new, a1 = plane(V, [f[0]] + [t if k == w else k for k in f[1:]])
+ if new is None or a1 < 1e-7 or np.dot(new[:3], old[:3]) < 0.5:
+ ok = False
+ break
+ if not ok:
+ continue
+ for i in shared:
+ alive[i] = False
+ for i in list(vf[w]):
+ if alive[i]:
+ f = F[i]
+ for k in (1, 2, 3):
+ if f[k] == w:
+ f[k] = t
+ vf[t].add(i)
+ vf[w] = set()
+ gone[w] = True
+ Qv[t] = Qv[t] + Qv[w]
+ ver[t] += 1
+ for x in sorted(neighbors(t)):
+ a2, b2 = min(t, x), max(t, x)
+ heapq.heappush(heap, (cost(a2, b2)[0], a2, b2, ver[a2], ver[b2]))
+ return [(f[0], V[f[1]], V[f[2]], V[f[3]]) for i, f in enumerate(F) if alive[i] and plane(V, f)[1] > 1e-7]
+
+
+def simplify_budget(tris, tol, budget, area, matmap=None, top=1.0):
+ """simplify, raising the error allowed until there are at most `budget` triangles per 400 m2 of the part"""
+ while True:
+ out = simplify(tris, tol, matmap=matmap)
+ if len(out) <= budget * max(1.0, area / 400.0) or tol >= top:
+ return out
+ tol *= 1.5
+
+
+# ------------------------------------------------------------------ tracks
+B62 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
+B62R = {c: i for i, c in enumerate(B62)}
+
+
+def b62(e):
+ t, n, L = 0, bytearray(), len(e)
+ for s, ch in enumerate(e):
+ l = B62R[ch]
+ nb = 5 if (l & 30) == 30 else 6
+ a = t // 8
+ while a >= len(n):
+ n.append(0)
+ sh = t - 8 * a
+ n[a] |= (l << sh) & 255
+ if sh > 8 - nb and s != L - 1:
+ if a + 1 >= len(n):
+ n.append(0)
+ n[a + 1] |= l >> (8 - sh)
+ t += nb
+ return bytes(n)
+
+
+def read_track(text, cp_ids, start_ids):
+ assert text.startswith('PolyTrack2')
+ b = zlib.decompress(b62(zlib.decompress(b62(text[10:])).decode('ascii')))
+ h = 0
+ n = b[h]; h += 1
+ name = b[h:h + n].decode(); h += n
+ n = b[h]; h += 1
+ author = b[h:h + n].decode(); h += n
+ if b[h]:
+ h += 4
+ h += 1
+ env, sun = b[h], b[h + 1]; h += 2
+ mx, my, mz = struct.unpack_from('<iii', b, h); h += 12
+ w = b[h]; h += 1
+ wx, wy, wz = w & 3, (w >> 2) & 3, (w >> 4) & 3
+ parts = []
+ while h < len(b):
+ pid = b[h]; h += 1
+ cnt = struct.unpack_from('<i', b, h)[0]; h += 4
+ for _ in range(cnt):
+ x = int.from_bytes(b[h:h + wx], 'little') + mx; h += wx
+ y = int.from_bytes(b[h:h + wy], 'little') + my; h += wy
+ z = int.from_bytes(b[h:h + wz], 'little') + mz; h += wz
+ r = b[h]; col = b[h + 1]; h += 2
+ p = dict(id=pid, x=x, y=y, z=z, rot=r & 3, axis=(r >> 2) & 7, color=col)
+ if pid in cp_ids:
+ p['cp'] = b[h] | b[h + 1] << 8; h += 2
+ if pid in start_ids:
+ p['start'] = struct.unpack_from('<I', b, h)[0]; h += 4
+ parts.append(p)
+ return dict(name=name, author=author, env=env, sun=sun, parts=parts)
+
+
+def tile_rotate(x, y, z, rot, axis):
+ """module 5494's sR: a part's tile turned like the part"""
+ if axis in (1, 3, 5):
+ if rot == 1: x, z = -z - 1, x
+ elif rot == 2: x, z = -x - 1, -z - 1
+ elif rot == 3: x, z = z, -x - 1
+ else:
+ if rot == 1: x, z = z, -x - 1
+ elif rot == 2: x, z = -x - 1, -z - 1
+ elif rot == 3: x, z = -z - 1, x
+ if axis == 1: x, y = -x - 1, -y - 1
+ elif axis == 2: x, y = y, -x - 1
+ elif axis == 3: x, y = -y - 1, x
+ elif axis == 4: y, z = -z - 1, y
+ elif axis == 5: y, z = z, -y - 1
+ return x, y, z
+
+
+def part_tiles(part):
+ out = []
+ for (a, b) in part['tiles']:
+ for x in range(a[0], b[0] + 1):
+ for y in range(a[1], b[1] + 1):
+ for z in range(a[2], b[2] + 1):
+ out.append((x, y, z))
+ return out
+
+
+# ------------------------------------------------------------------ output helpers
+class Out:
+ FMT = {'int8_t': 'b', 'uint8_t': 'B', 'int16_t': 'h', 'uint16_t': 'H', 'int32_t': 'i', 'uint32_t': 'I'}
+
+ def __init__(self):
+ self.c, self.h = [], []
+ self.bin = bytearray()
+
+ def blob(self, ctype, name, values):
+ """a big table in data.bin, reached through a macro"""
+ values = [int(v, 16) if isinstance(v, str) else int(v) for v in values] or [0]
+ while len(self.bin) % 4:
+ self.bin.append(0)
+ off = len(self.bin)
+ self.bin += struct.pack('<%d%s' % (len(values), self.FMT[ctype]), *values)
+ self.h.append('#define %s ((const %s *)(pt_blob + %d)) /* %d */' % (name, ctype, off, len(values)))
+
+ def blob_raw(self, ctype, name, data, count):
+ while len(self.bin) % 4:
+ self.bin.append(0)
+ off = len(self.bin)
+ self.bin += data
+ self.h.append('#define %s ((const %s *)(pt_blob + %d)) /* %d */' % (name, ctype, off, count))
+
+ def arr(self, ctype, name, values, per_line=16, static=False):
+ values = list(values)
+ self.c.append('%sconst %s %s[%d] = {' % ('static ' if static else '', ctype, name, max(1, len(values))))
+ if not values:
+ values = [0]
+ for i in range(0, len(values), per_line):
+ self.c.append(' ' + ', '.join(str(v) for v in values[i:i + per_line]) + ',')
+ self.c.append('};')
+ if not static:
+ self.h.append('extern const %s %s[%d];' % (ctype, name, max(1, len(values))))
+
+
+def fl(v):
+ s = '%.9g' % v
+ if '.' not in s and 'e' not in s and 'n' not in s:
+ s += '.0'
+ return s + 'f'
+
+
+def q16(v):
+ r = int(round(v * FIX))
+ if not -32768 <= r <= 32767:
+ raise SystemExit('vertex out of range: %r' % v)
+ return r
+
+
+def srgb_to_linear(c):
+ c /= 255.0
+ return c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4
+
+
+def hex_linear(h):
+ return [srgb_to_linear(int(h[i:i + 2], 16)) for i in (0, 2, 4)]
+
+
+# ------------------------------------------------------------------ the stripe on the car (images/pattern_stripe.svg)
+STRIPE = [[(259.51, 81.711), (400.05, 81.711), (400.05, 227.23), (259.51, 227.23)],
+ [(276.18, 236.23), (383.37, 236.23), (368.7, 373.52), (290.86, 373.52)],
+ [(259.51, 387.11), (400.05, 387.11), (400.05, 644.93), (391.43, 708.02), (368.75, 845.56),
+ (353.28, 893.41), (306.28, 893.41), (290.81, 845.56), (268.13, 708.02), (259.51, 644.93)]]
+
+
+def in_poly(pt, poly):
+ x, y = pt
+ c = False
+ for i in range(len(poly)):
+ x1, y1 = poly[i - 1]
+ x2, y2 = poly[i]
+ if (y1 > y) != (y2 > y) and x < x1 + (y - y1) * (x2 - x1) / (y2 - y1):
+ c = not c
+ return c
+
+
+def main():
+ web = sys.argv[1] if len(sys.argv) > 1 else os.environ.get('POLYTRACK_WEB', '')
+ if not web or not os.path.exists(os.path.join(web, 'main.bundle.js')):
+ raise SystemExit(__doc__)
+ src = open(os.path.join(web, 'main.bundle.js'), encoding='utf-8', errors='replace').read()
+ names, table, envcolors = part_table(src)
+ orients, _ = orient_table(src)
+ mtable = mountain_table(src)
+ models = Models(web)
+ cp_ids = {k for k, v in table.items() if v['detector'] and v['detector']['type'] == 'Checkpoint'}
+ start_ids = {k for k, v in table.items() if v['start']}
+
+ tracks = []
+ for t in TRACKS:
+ tracks.append(read_track(open(os.path.join(web, 'tracks', 'official', t + '.track')).read().strip(),
+ cp_ids, start_ids))
+ used = sorted({p['id'] for t in tracks for p in t['parts']})
+ pidx = {pid: i for i, pid in enumerate(used)}
+ out = Out()
+
+ # ---------------------------------------------------------------- parts
+ pool, chunks, cvidx, rtris, ctris, cgrid, cgidx, partrec = [], [], [], [], [], [], [], []
+ simple = {}
+ part_mesh = {}
+ print('parts:', len(used), file=sys.stderr)
+ for pid in used:
+ part = table[pid]
+ raw = build_part(models, part)
+ raw = [t for t in raw if t[0] in MATERIALS or t[0] is None]
+ mats = {t[0] for t in raw}
+ col = simplify(raw, 0.015, keep_materials=False)
+ if len(raw) <= 64:
+ exact = simplify(raw, 0.001)
+ lod0 = lod1 = exact
+ if len(exact) <= 32:
+ col = [('x', a, b, c) for _, a, b, c in exact]
+ simple[pid] = True
+ else:
+ av = np.array([v for t in raw for v in t[1:]])
+ area = float((av[:, 0].max() - av[:, 0].min()) * (av[:, 2].max() - av[:, 2].min()))
+ lod0 = simplify_budget(raw, 0.06, 360, area, top=0.4)
+ lod1 = simplify_budget(lod0, 0.35, 120, area, matmap=EDGE, top=2.0)
+ part_mesh[pid] = (lod0, lod1, col)
+ # vertex pool for the part
+ vbase = len(pool)
+ vid = {}
+
+ def vref(v):
+ k = (q16(v[0]), q16(v[1]), q16(v[2]))
+ if k not in vid:
+ vid[k] = len(pool) - vbase
+ pool.append(k)
+ return vid[k]
+
+ allv = np.array([v for t in raw for v in t[1:]])
+ lo, hi = allv.min(0), allv.max(0)
+ # chunks: by triangle centre, on a 20 m grid anchored at the part's minimum corner
+ ncx = max(1, int(math.ceil((hi[0] - lo[0]) / CHUNK - 0.01)))
+ ncz = max(1, int(math.ceil((hi[2] - lo[2]) / CHUNK - 0.01)))
+
+ def chunk_of(t):
+ c = (t[1] + t[2] + t[3]) / 3
+ return (min(ncx - 1, max(0, int((c[0] - lo[0]) // CHUNK))), min(ncz - 1, max(0, int((c[2] - lo[2]) // CHUNK))))
+
+ groups = collections.defaultdict(lambda: ([], []))
+ for t in lod0:
+ groups[chunk_of(t)][0].append(t)
+ for t in lod1:
+ groups[chunk_of(t)][1].append(t)
+ # a chunk holds at most 240 vertices in each detail: cut bigger ones by triangle order
+ pieces = []
+ for key in sorted(groups):
+ l0, l1 = groups[key]
+ s0, s1 = cut(l0), cut(l1)
+ for i in range(max(len(s0), len(s1))):
+ pieces.append((s0[i] if i < len(s0) else [], s1[i] if i < len(s1) else []))
+ first_chunk = len(chunks)
+ for piece in pieces:
+ rec = {}
+ for li, lst in enumerate(piece):
+ local = {}
+ vlist = []
+ tl = []
+ for mat, a, b, c in lst:
+ ids = []
+ for v in (a, b, c):
+ g = vref(v)
+ if g not in local:
+ local[g] = len(vlist)
+ vlist.append(g)
+ ids.append(local[g])
+ qa = np.array(pool[vbase + vlist[ids[0]]], float)
+ qb = np.array(pool[vbase + vlist[ids[1]]], float)
+ qc = np.array(pool[vbase + vlist[ids[2]]], float)
+ n = np.cross(qb - qa, qc - qa)
+ ln = np.linalg.norm(n)
+ if ln < 1e-9:
+ continue
+ n = n / ln
+ tl.append((ids[0], ids[1], ids[2], MATERIALS.index(mat if mat else 'Road'),
+ int(round(n[0] * 127)), int(round(n[1] * 127)), int(round(n[2] * 127))))
+ if len(vlist) > 255:
+ raise SystemExit('chunk with too many vertices in %s' % part['name'])
+ rec[li] = (len(cvidx), len(vlist), len(rtris), len(tl))
+ cvidx.extend(vlist)
+ rtris.extend(tl)
+ pts = [pool[vbase + cvidx[rec[0][0] + i]] for i in range(rec[0][1])] or [(0, 0, 0)]
+ pts += [pool[vbase + cvidx[rec[1][0] + i]] for i in range(rec[1][1])]
+ pa = np.array(pts)
+ box = list(pa.min(0)) + list(pa.max(0))
+ chunks.append((rec[0], rec[1], box))
+ # physics triangles and their 5 m grid
+ cfirst = len(ctris)
+ ctl = []
+ for _, a, b, c in col:
+ ia, ib, ic = vref(a), vref(b), vref(c)
+ if len({ia, ib, ic}) < 3:
+ continue
+ ctl.append((ia, ib, ic))
+ ctris.extend(ctl)
+ gx = max(1, int(math.ceil((hi[0] - lo[0]) / CELL - 1e-6)))
+ gz = max(1, int(math.ceil((hi[2] - lo[2]) / CELL - 1e-6)))
+ cells = [[] for _ in range(gx * gz)]
+ for ti, (ia, ib, ic) in enumerate(ctl):
+ P = np.array([pool[vbase + ia], pool[vbase + ib], pool[vbase + ic]], float) / FIX
+ x0 = int(max(0, min(gx - 1, (P[:, 0].min() - lo[0] - 0.05) // CELL)))
+ x1 = int(max(0, min(gx - 1, (P[:, 0].max() - lo[0] + 0.05) // CELL)))
+ z0 = int(max(0, min(gz - 1, (P[:, 2].min() - lo[2] - 0.05) // CELL)))
+ z1 = int(max(0, min(gz - 1, (P[:, 2].max() - lo[2] + 0.05) // CELL)))
+ for cx in range(x0, x1 + 1):
+ for cz in range(z0, z1 + 1):
+ cells[cz * gx + cx].append(ti)
+ gfirst = len(cgrid)
+ for cl in cells:
+ cgrid.append(len(cgidx))
+ cgidx.extend(cl)
+ lo16 = [q16(v) for v in lo]
+ hi16 = [q16(v) for v in hi]
+ det = part['detector']
+ partrec.append(dict(vbase=vbase, nv=len(pool) - vbase, chunk=first_chunk, nchunk=len(chunks) - first_chunk,
+ col=cfirst, ncol=len(ctl), grid=gfirst, gx=gx, gz=gz, lo=lo16, hi=hi16,
+ simple=1 if pid in simple else 0,
+ det=0 if not det else (1 if det['type'] == 'Checkpoint' else 2),
+ dc=det['center'] if det else [0, 0, 0], ds=det['size'] if det else [0, 0, 0],
+ start=part['start'] or [0, 0, 0], custom=1 if part['palette'] == 'h' else 0,
+ name=part['name'], id=pid))
+ print(' %-26s raw %5d lod0 %5d lod1 %5d col %5d chunks %d' % (part['name'], len(raw), len(lod0), len(lod1),
+ len(ctl), len(chunks) - first_chunk),
+ file=sys.stderr)
+ cgrid.append(len(cgidx))
+
+ # ---------------------------------------------------------------- tracks
+ tinst, tmasks, tcells, tspec, theads, tthumb = [], [], [], [], [], []
+ M = [np.array(m, float) for m in orients]
+ for ti, t in enumerate(tracks):
+ insts = []
+ # world triangles of simple parts, to find faces hidden against a neighbour (or the ground)
+ planes = collections.defaultdict(list)
+ for k, p in enumerate(t['parts']):
+ pid = p['id']
+ if pid not in simple:
+ continue
+ Mo = M[p['axis'] * 4 + p['rot']]
+ base = np.array([p['x'], p['y'], p['z']], float) * 5
+ for i, (_, a, b, c) in enumerate(part_mesh[pid][0]):
+ W = [Mo @ v + base for v in (a, b, c)]
+ n = np.cross(W[1] - W[0], W[2] - W[0])
+ n = n / np.linalg.norm(n)
+ key = (round(n[0], 2), round(n[1], 2), round(n[2], 2), round(float(np.dot(n, W[0])), 2))
+ Wa = np.array(W)
+ c0 = np.floor((Wa.min(0) - 0.01) / 10).astype(int)
+ c1 = np.floor((Wa.max(0) + 0.01) / 10).astype(int)
+ for gx_ in range(c0[0], c1[0] + 1):
+ for gy_ in range(c0[1], c1[1] + 1):
+ for gz_ in range(c0[2], c1[2] + 1):
+ planes[key + (gx_, gy_, gz_)].append((k, W))
+ masks = {}
+ for k, p in enumerate(t['parts']):
+ pid = p['id']
+ if pid not in simple:
+ continue
+ Mo = M[p['axis'] * 4 + p['rot']]
+ base = np.array([p['x'], p['y'], p['z']], float) * 5
+ mask = 0
+ for i, (_, a, b, c) in enumerate(part_mesh[pid][0]):
+ W = [Mo @ v + base for v in (a, b, c)]
+ n = np.cross(W[1] - W[0], W[2] - W[0])
+ n = n / np.linalg.norm(n)
+ if n[1] < -0.99 and max(abs(w[1]) for w in W) < 1e-3:
+ continue # on the ground, facing it
+ key = (round(-n[0], 2), round(-n[1], 2), round(-n[2], 2), round(float(np.dot(-n, W[0])), 2))
+ cen = (W[0] + W[1] + W[2]) / 3
+ samples = [cen] + [w + (cen - w) * 0.02 for w in W]
+ cov = True
+ for s in samples:
+ hit = False
+ cs = tuple(int(x) for x in np.floor(s / 10))
+ for k2, W2 in planes.get(key + cs, ()):
+ if k2 == k:
+ continue
+ if point_in_tri(s, W2):
+ hit = True
+ break
+ if not hit:
+ cov = False
+ break
+ if not cov:
+ mask |= 1 << i
+ masks[k] = mask
+ # instances
+ mlist = [0xFFFFFFFF]
+ mix = {0xFFFFFFFF: 0}
+ recs = []
+ for k, p in enumerate(t['parts']):
+ pid = p['id']
+ mi = 0
+ if pid in simple:
+ m = masks[k]
+ if m == 0:
+ continue # nothing of it can be seen or touched
+ if m != (1 << len(part_mesh[pid][0])) - 1:
+ if m not in mix:
+ mix[m] = len(mlist)
+ mlist.append(m)
+ mi = mix[m]
+ pr = partrec[pidx[pid]]
+ Mo = M[p['axis'] * 4 + p['rot']]
+ lo = np.array(pr['lo'], float) / FIX
+ hi = np.array(pr['hi'], float) / FIX
+ corners = np.array([[x, y, z] for x in (lo[0], hi[0]) for y in (lo[1], hi[1]) for z in (lo[2], hi[2])])
+ W = (Mo @ corners.T).T + np.array([p['x'], p['y'], p['z']], float) * 5
+ wl, wh = W.min(0), W.max(0)
+ color = p['color']
+ recs.append(dict(p=p, part=pidx[pid], mi=mi, lo=wl, hi=wh, color=color, k=k))
+ if len(mlist) > 256:
+ raise SystemExit('too many masks in %s' % t['name'])
+ # grid of 40 m cells by centre
+ cx = [int(math.floor(((r['lo'][0] + r['hi'][0]) / 2) / (TCELL * 5))) for r in recs]
+ cz = [int(math.floor(((r['lo'][2] + r['hi'][2]) / 2) / (TCELL * 5))) for r in recs]
+ x0, x1, z0, z1 = min(cx), max(cx), min(cz), max(cz)
+ gw, gh = x1 - x0 + 1, z1 - z0 + 1
+ order = sorted(range(len(recs)), key=lambda i: ((cz[i] - z0) * gw + (cx[i] - x0), recs[i]['k']))
+ cellstart = []
+ j = 0
+ for c in range(gw * gh):
+ cellstart.append(j)
+ while j < len(order) and (cz[order[j]] - z0) * gw + (cx[order[j]] - x0) == c:
+ j += 1
+ cellstart.append(len(order))
+ reach = 0.0
+ for r in recs:
+ reach = max(reach, float(max(r['hi'][0] - r['lo'][0], r['hi'][2] - r['lo'][2])) / 2)
+ newidx = {}
+ ibase = len(tinst)
+ for n_, i in enumerate(order):
+ r = recs[i]
+ p = r['p']
+ newidx[r['k']] = n_
+ tinst.append((r['part'], p['rot'] | p['axis'] << 2, r['color'], p['y'], p['x'], p['z'], r['mi']))
+ # start, checkpoints, finish
+ starts = [r for r in recs if 'start' in r['p']]
+ st = max(starts, key=lambda r: r['p']['start'])
+ cps = sorted([r for r in recs if 'cp' in r['p']], key=lambda r: (r['p']['cp'], newidx[r['k']]))
+ fins = [r for r in recs if table[r['p']['id']]['detector'] and table[r['p']['id']]['detector']['type'] == 'Finish']
+ sbase = len(tspec)
+ for r in cps:
+ tspec.append((newidx[r['k']], r['p']['cp']))
+ for r in fins:
+ tspec.append((newidx[r['k']], 0xFFFF))
+ norders = len({r['p']['cp'] for r in cps})
+ # bounds (tiles) for the mountains and the map
+ xs = [p['x'] for p in t['parts']]
+ zs = [p['z'] for p in t['parts']]
+ # map (module 9117's createThumbnail): a pixel a part (4 tiles)
+ tiles = []
+ for p in t['parts']:
+ part = table[p['id']]
+ kind = 1
+ if part['start']:
+ kind = 3
+ elif part['detector'] and part['detector']['type'] == 'Checkpoint':
+ kind = 2
+ elif part['detector'] and part['detector']['type'] == 'Finish':
+ kind = 4
+ for (tx, ty, tz) in part_tiles(part):
+ rx, ry, rz = tile_rotate(tx, ty, tz, p['rot'], p['axis'])
+ tiles.append(((p['x'] + rx - 2) // 4, (p['z'] + rz - 2) // 4, kind))
+ mx0 = min(a for a, _, _ in tiles); mx1 = max(a for a, _, _ in tiles)
+ mz0 = min(b for _, b, _ in tiles); mz1 = max(b for _, b, _ in tiles)
+ tw, th = mx1 - mx0 + 1, mz1 - mz0 + 1
+ img = [0] * (tw * th)
+ for a, b, kind in tiles:
+ img[(b - mz0) * tw + (a - mx0)] = max(1, img[(b - mz0) * tw + (a - mx0)]) if kind == 1 else kind
+ # (as the game: checkpoints, then starts, then finishes painted over the plain parts)
+ for prio in (2, 3, 4):
+ for a, b, kind in tiles:
+ if kind == prio:
+ img[(b - mz0) * tw + (a - mx0)] = kind
+ tbase = len(tthumb)
+ for i in range(0, len(img), 2):
+ tthumb.append(img[i] | ((img[i + 1] if i + 1 < len(img) else 0) << 4))
+ mbase = len(tmasks)
+ tmasks.extend(mlist)
+ cbase = len(tcells)
+ tcells.extend(cellstart)
+ stp = st['p']
+ theads.append(dict(name=t['name'], env=t['env'], sun=t['sun'], inst=ibase, ninst=len(order), mask=mbase,
+ cell=cbase, gx0=x0, gz0=z0, gw=gw, gh=gh, reach=int(math.ceil(reach)),
+ spec=sbase, ncp=len(cps), nfin=len(fins), norders=norders,
+ start=newidx[st['k']], bx0=min(xs), bx1=max(xs), bz0=min(zs), bz1=max(zs),
+ thumb=tbase, tw=tw, th=th))
+ print('track %-9s parts %4d kept %4d masks %3d cells %dx%d reach %.0f orders %d map %dx%d' % (
+ t['name'], len(t['parts']), len(order), len(mlist), gw, gh, reach, norders, tw, th), file=sys.stderr)
+
+ # ---------------------------------------------------------------- the car
+ car = models.scene('car')
+ body = []
+ for pr in car['Body']:
+ for f in pr['faces']:
+ a, b, c = (pr['verts'][i] for i in f)
+ mat = pr['material']
+ if mat == 'Main' and pr['uv'] is not None:
+ uv = (pr['uv'][f[0]] + pr['uv'][f[1]] + pr['uv'][f[2]]) / 3 * 1024
+ if any(in_poly(uv, s) for s in STRIPE):
+ mat = 'Stripe'
+ body.append((mat, a, b, c))
+ body = simplify(body, 0.004)
+ wheel = make_wheel(car['Wheel0'])
+ extra = []
+ for node in ('Suspension', 'Exhaust0'):
+ for pr in car[node]:
+ for f in pr['faces']:
+ extra.append((pr['material'], *(pr['verts'][i] for i in f)))
+ extra = simplify(extra, 0.01)
+ hull = []
+ for pr in car['Collision']:
+ hull = [tuple(float(x) for x in v) for v in pr['verts']]
+ print('car: body %d wheel %d extra %d hull %d' % (len(body), len(wheel), len(extra), len(hull)), file=sys.stderr)
+
+ def mesh_arrays(name, tris):
+ vid, verts, tl = {}, [], []
+ for mat, a, b, c in tris:
+ ids = []
+ for v in (a, b, c):
+ k = (q16(v[0]), q16(v[1]), q16(v[2]))
+ if k not in vid:
+ vid[k] = len(verts)
+ verts.append(k)
+ ids.append(vid[k])
+ if len(set(ids)) < 3:
+ continue
+ tl.append((ids[0], ids[1], ids[2], CAR_MATERIALS.index(mat)))
+ out.arr('int16_t', 'pt_%s_v' % name, [x for v in verts for x in v], 12)
+ out.arr('uint8_t', 'pt_%s_t' % name, [x for t in tl for x in t], 16)
+ out.h.append('#define PT_%s_NV %d' % (name.upper(), len(verts)))
+ out.h.append('#define PT_%s_NT %d' % (name.upper(), len(tl)))
+ if len(verts) > 255:
+ raise SystemExit('car mesh %s has too many vertices' % name)
+
+ # ---------------------------------------------------------------- materials (linear RGB, x 4096)
+ base = {}
+ for name in ('road', 'road_wide', 'planes', 'blocks', 'pillar', 'signs', 'wall_track'):
+ js, _ = load_glb(os.path.join(web, 'models', name + '.glb'))
+ for m in js['materials']:
+ c = m.get('pbrMetallicRoughness', {}).get('baseColorFactor', [1, 1, 1, 1])
+ base.setdefault(m['name'], c[:3])
+ base['RoadEdge'] = [(base['RoadEdgeRed'][i] + base['RoadEdgeWhite'][i]) / 2 for i in range(3)]
+ envmat = []
+ for env in ('Summer', 'Winter', 'Desert'):
+ over = envcolors.get(env, {})
+ for mname in MATERIALS:
+ if mname in over:
+ c = hex_linear(over[mname])
+ elif mname == 'RoadEdge' and ('RoadEdgeRed' in over or 'RoadEdgeWhite' in over):
+ r = hex_linear(over.get('RoadEdgeRed', '%02x%02x%02x' % tuple(0 for _ in range(3))))
+ w = hex_linear(over.get('RoadEdgeWhite', 'ffffff'))
+ c = [(r[i] + w[i]) / 2 for i in range(3)]
+ else:
+ c = base[mname]
+ envmat.extend(int(round(min(1.0, x) * 4096)) for x in c)
+ custom = []
+ for h in envcolors['Custom']:
+ custom.extend(int(round(x * 4096)) for x in hex_linear(h))
+ carbase = {'AirIntake': [0, 0, 0], 'Metal': [0.077, 0.077, 0.077], 'BrakeLight': [0.1, 0, 0],
+ 'Tire': [0.03, 0.03, 0.03], 'Rim': [0.4, 0.4, 0.4]}
+
+ # ---------------------------------------------------------------- write
+ out.h.append('#define PT_NPARTS %d' % len(used))
+ out.h.append('#define PT_NTRACKS %d' % len(tracks))
+ out.h.append('#define PT_NMAT %d' % len(MATERIALS))
+ for i, m in enumerate(MATERIALS):
+ out.h.append('#define PT_M_%s %d' % (re.sub(r'(?<!^)(?=[A-Z])', '_', m).upper(), i))
+ for i, m in enumerate(CAR_MATERIALS):
+ out.h.append('#define PT_CM_%s %d' % (m.upper(), i))
+ out.blob('int16_t', 'pt_pool', [x for v in pool for x in v])
+ out.blob('uint16_t', 'pt_cvidx', cvidx)
+ out.blob('uint8_t', 'pt_rtri', [x & 0xFF for t in rtris for x in t])
+ out.c.append('const pt_chunk pt_chunks[%d] = {' % len(chunks))
+ for r0, r1, box in chunks:
+ out.c.append(' {{{%d, %d, 0, %d, %d}, {%d, %d, 0, %d, %d}}, {%s}},' % (
+ r0[0], r0[1], r0[3], r0[2], r1[0], r1[1], r1[3], r1[2], ','.join(str(int(b)) for b in box)))
+ out.c.append('};')
+ out.h.append('extern const pt_chunk pt_chunks[%d];' % len(chunks))
+ out.blob('uint16_t', 'pt_ctri', [x for t in ctris for x in t])
+ out.blob('uint32_t', 'pt_cgrid', cgrid)
+ out.blob('uint16_t', 'pt_cgidx', cgidx)
+ out.c.append('const pt_part pt_parts[PT_NPARTS] = {')
+ for p in partrec:
+ out.c.append(' {%d, %d, %d, %d, %d, %d, %d, %d, %d, {%s}, {%s}, %d, %d, %d, {%s}, {%s}, {%s}}, /* %s */' % (
+ p['vbase'], p['nv'], p['chunk'], p['nchunk'], p['col'], p['ncol'], p['grid'], p['gx'], p['gz'],
+ ', '.join(map(str, p['lo'])), ', '.join(map(str, p['hi'])), p['simple'], p['det'], p['custom'],
+ ', '.join(fl(v) for v in p['dc']), ', '.join(fl(v) for v in p['ds']),
+ ', '.join(fl(v) for v in p['start']), p['name']))
+ out.c.append('};')
+ out.h.append('extern const pt_part pt_parts[PT_NPARTS];')
+ out.arr('uint8_t', 'pt_part_id', [p['id'] for p in partrec], 16)
+ out.arr('int8_t', 'pt_orient', [int(x) for m in orients for x in m.flatten()], 18)
+ out.blob_raw('pt_inst', 'pt_insts', b''.join(struct.pack('<hhBBBBBB', r[4], r[5], r[0], r[1], r[2], r[3], r[6], 0)
+ for r in tinst), len(tinst))
+ out.blob('uint32_t', 'pt_masks', tmasks)
+ out.blob('uint16_t', 'pt_tcells', tcells)
+ out.blob_raw('pt_spec', 'pt_specs', b''.join(struct.pack('<HH', *r) for r in tspec), len(tspec))
+ out.blob('uint8_t', 'pt_thumbs', tthumb)
+ out.c.append('const pt_track pt_tracks[PT_NTRACKS] = {')
+ for h in theads:
+ out.c.append(' {"%s", %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d},' % (
+ h['name'], h['env'], h['sun'], h['inst'], h['ninst'], h['mask'], h['cell'], h['gx0'], h['gz0'], h['gw'],
+ h['gh'], h['reach'], h['spec'], h['ncp'], h['nfin'], h['norders'], h['start'], h['bx0'], h['bx1'],
+ h['bz0'], h['bz1'], h['thumb'], h['tw'], h['th']))
+ out.c.append('};')
+ out.h.append('extern const pt_track pt_tracks[PT_NTRACKS];')
+ out.arr('uint16_t', 'pt_envmat', envmat, 15)
+ out.arr('uint16_t', 'pt_custom', custom, 15)
+ out.arr('uint16_t', 'pt_carmat', [int(round(x * 4096)) for m in CAR_MATERIALS[2:] for x in carbase[m]], 15)
+ mesh_arrays('body', body)
+ mesh_arrays('wheel', wheel)
+ mesh_arrays('extra', extra)
+ out.c.append('const float pt_hull[%d][3] = {' % len(hull))
+ for v in hull:
+ out.c.append(' {%s, %s, %s},' % tuple(fl(x) for x in v))
+ out.c.append('};')
+ out.h.append('extern const float pt_hull[%d][3];' % len(hull))
+ out.h.append('#define PT_HULL_N %d' % len(hull))
+ out.c.append('const float pt_mountain[%d] = {' % len(mtable))
+ for i in range(0, len(mtable), 6):
+ out.c.append(' ' + ', '.join(fl(v) for v in mtable[i:i + 6]) + ',')
+ out.c.append('};')
+ out.h.append('extern const float pt_mountain[%d];' % len(mtable))
+ out.h.append('#define PT_MOUNTAIN_N %d' % len(mtable))
+
+ hdr = ['/* PolyTrack\'s data, made by tools/pack.py from Kodub\'s web build of PolyTrack 0.6.3: the official tracks,',
+ ' * the track parts and the car (their models simplified), the materials. Do not edit. */',
+ '#ifndef PT_DATA_H', '#define PT_DATA_H', '#include <stdint.h>', '',
+ '#define PT_FIX %d /* vertex units a metre */' % int(FIX),
+ 'typedef struct { uint32_t vfirst; uint8_t nv, pad; uint16_t ntri; uint32_t tfirst; } pt_lod;',
+ 'typedef struct { pt_lod lod[2]; int16_t box[6]; } pt_chunk;',
+ 'typedef struct {',
+ ' uint16_t vbase, nv; /* its vertices in pt_pool */',
+ ' uint16_t chunk; uint8_t nchunk;',
+ ' uint16_t col, ncol; /* physics triangles in pt_ctri */',
+ ' uint16_t grid; uint8_t gx, gz; /* their 5 m grid, from lo */',
+ ' int16_t lo[3], hi[3];',
+ ' uint8_t simple; /* one mesh for all: faces can be hidden per instance (pt_masks) */',
+ ' uint8_t det; /* 1 checkpoint, 2 finish */',
+ ' uint8_t custom; /* takes the custom block colours */',
+ ' float dc[3], ds[3]; /* its detector box */',
+ ' float start[3]; /* a start: where the car goes */',
+ '} pt_part;',
+ 'typedef struct { int16_t x, z; uint8_t part, orient, color, y, mask, pad; } pt_inst;',
+ 'extern const uint8_t pt_blob[]; /* data.bin (data.S) */',
+ 'typedef struct { uint16_t inst, order; } pt_spec;',
+ 'typedef struct {',
+ ' const char *name;',
+ ' uint8_t env, sun;',
+ ' uint16_t inst, ninst, mask, cell;',
+ ' int16_t gx0, gz0; uint8_t gw, gh, reach;',
+ ' uint16_t spec; uint8_t ncp, nfin, norders;',
+ ' uint16_t start;',
+ ' int16_t bx0, bx1, bz0, bz1;',
+ ' uint16_t thumb; uint8_t tw, th;',
+ '} pt_track;', '']
+ open(os.path.join(SRC, 'data.h'), 'w').write('\n'.join(hdr + out.h + ['#endif', '']))
+ open(os.path.join(SRC, 'data.bin'), 'wb').write(bytes(out.bin))
+ open(os.path.join(SRC, 'data.c'), 'w').write(
+ '/* Made by tools/pack.py from Kodub\'s web build of PolyTrack 0.6.3. Do not edit. */\n#include "data.h"\n\n' +
+ '\n'.join(out.c) + '\n')
+ size = len(pool) * 6 + len(cvidx) * 2 + len(rtris) * 7 + len(chunks) * 28 + len(ctris) * 6 + len(cgrid) * 2 + \
+ len(cgidx) * 2 + len(tinst) * 10 + len(tmasks) * 4 + len(tcells) * 2 + len(tthumb)
+ print('pool %d verts, render %d tris, physics %d tris, chunks %d, instances %d: data.bin %d bytes' % (
+ len(pool), len(rtris), len(ctris), len(chunks), len(tinst), len(out.bin)), file=sys.stderr)
+
+
+def make_wheel(prims):
+ """The wheel (car.glb's Wheel0) at the calculator's size: its tyre as a ten-sided drum of the same radius and
+ width, its rim as a ten-sided disc in the rim colour with the hub's spokes alternating in metal."""
+ tv = np.concatenate([pr['local'] for pr in prims if pr['material'] == 'Tire'])
+ rv = np.concatenate([pr['local'] for pr in prims if pr['material'] == 'Rim'])
+ R = float(np.max(np.hypot(tv[:, 1], tv[:, 2])))
+ W = float(np.max(np.abs(tv[:, 0])))
+ r = float(np.max(np.hypot(rv[:, 1], rv[:, 2])))
+ n = 10
+ ring = [(math.cos(2 * math.pi * (k + 0.5) / n), math.sin(2 * math.pi * (k + 0.5) / n)) for k in range(n)]
+ tris = []
+ def P(x, rad, k):
+ return np.array([x, rad * ring[k % n][0], rad * ring[k % n][1]])
+ for k in range(n):
+ a, b = P(W, R, k), P(W, R, k + 1)
+ c, d = P(-W, R, k), P(-W, R, k + 1)
+ tris.append(('Tire', a, c, b))
+ tris.append(('Tire', b, c, d))
+ for side in (1, -1):
+ x = W * side
+ for k in range(n):
+ a, b = P(x, R, k), P(x, R, k + 1)
+ c, d = P(x, r, k), P(x, r, k + 1)
+ if side > 0:
+ tris.append(('Tire', a, b, c)); tris.append(('Tire', c, b, d))
+ else:
+ tris.append(('Tire', a, c, b)); tris.append(('Tire', c, d, b))
+ cen = np.array([x, 0.0, 0.0])
+ for k in range(n):
+ c, d = P(x, r, k), P(x, r, k + 1)
+ mat = 'Rim' if k % 2 == 0 else 'Metal'
+ tris.append((mat, cen, c, d) if side > 0 else (mat, cen, d, c))
+ return tris
+
+
+def cut(tris, limit=240):
+ out, cur, vs = [], [], set()
+ for t in tris:
+ keys = {tuple(np.round(v * FIX).astype(int)) for v in t[1:]}
+ if len(vs | keys) > limit:
+ out.append(cur)
+ cur, vs = [], set()
+ cur.append(t)
+ vs |= keys
+ if cur:
+ out.append(cur)
+ return out
+
+
+def point_in_tri(p, W):
+ a, b, c = W
+ n = np.cross(b - a, c - a)
+ for u, v in ((a, b), (b, c), (c, a)):
+ if np.dot(np.cross(v - u, p - u), n) < -1e-6 * np.dot(n, n):
+ return False
+ return True
+
+
+if __name__ == '__main__':
+ main()