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.

Celeste: the TI-84 Plus CE version's engine

Mason3630c9e205864fb
diff --git a/games/celeste/ce/data/CEL10.bin b/games/celeste/ce/data/CEL10.bin
index 7ae63591..d4a6661f 100644
Binary files a/games/celeste/ce/data/CEL10.bin and b/games/celeste/ce/data/CEL10.bin differ
diff --git a/games/celeste/ce/data/CEL11.bin b/games/celeste/ce/data/CEL11.bin
index 3cf02106..8bfb868d 100644
Binary files a/games/celeste/ce/data/CEL11.bin and b/games/celeste/ce/data/CEL11.bin differ
diff --git a/games/celeste/ce/data/CEL12.bin b/games/celeste/ce/data/CEL12.bin
index 35d721bd..5a300ca2 100644
Binary files a/games/celeste/ce/data/CEL12.bin and b/games/celeste/ce/data/CEL12.bin differ
diff --git a/games/celeste/ce/host/plat_host.c b/games/celeste/ce/host/plat_host.c
new file mode 100644
index 00000000..d0ca70b0
--- /dev/null
+++ b/games/celeste/ce/host/plat_host.c
@@ -0,0 +1,215 @@
+/* The CE version on a computer, for tests and pictures: the same 8-bit screen and palette, the appvars read from
+ * data/, the keys from a script, the clock a frame at a time (30 a second, two updates each).
+ *
+ * CEL_DATA=data/ CEL_KEYS="R*30 RJ*5 _*60" CEL_FRAMES=600 CEL_SHOTS=60,120 CEL_OUT=dir build/host/celeste
+ * Keys: L R U D left right up down, J jump (2nd), X dash (clear), G grab (math), P pause (del), O ok, B back, Q quit;
+ * "_" none; "*n": for n updates. CEL_START=chapter,room: straight into a room. */
+#include "../src/ce.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <zlib.h>
+
+uint8_t *g_fb;
+static uint8_t fbs[2][320 * 240];
+static int cur;
+static uint16_t pal[256];
+static unsigned clock_ticks;
+static long updates, frames, max_updates;
+static const char *script;
+static int script_left;
+static unsigned script_keys;
+static const char *shots, *out_dir;
+uint8_t *g_shown; /* the buffer on screen */
+
+static unsigned parse_keys(const char **s) {
+ unsigned k = 0;
+ for (; **s && **s != '*' && **s != ' '; (*s)++) switch (**s) {
+ case 'L': k |= K_LEFT; break;
+ case 'R': k |= K_RIGHT; break;
+ case 'U': k |= K_UP; break;
+ case 'D': k |= K_DOWN; break;
+ case 'J': k |= K_JUMP | K_OK; break;
+ case 'X': k |= K_DASH | K_BACK | K_TALK; break;
+ case 'G': k |= K_GRAB; break;
+ case 'P': k |= K_PAUSE; break;
+ case 'O': k |= K_OK; break;
+ case 'B': k |= K_BACK; break;
+ case 'Q': k |= K_QUIT; break;
+ }
+ return k;
+}
+static void next_step(void) {
+ while (script && *script == ' ') script++;
+ if (!script || !*script) {
+ script_keys = 0, script_left = 1 << 30;
+ return;
+ }
+ script_keys = parse_keys(&script);
+ script_left = 1;
+ if (*script == '*') script++, script_left = (int)strtol(script, (char **)&script, 10);
+}
+
+void plat_init(void) {
+ g_fb = fbs[1];
+ g_shown = fbs[0];
+ script = getenv("CEL_KEYS");
+ script_left = 0;
+ max_updates = getenv("CEL_FRAMES") ? atol(getenv("CEL_FRAMES")) : 600;
+ shots = getenv("CEL_SHOTS");
+ out_dir = getenv("CEL_OUT") ? getenv("CEL_OUT") : ".";
+}
+void plat_exit(void) {}
+
+static void png(const char *path, const uint8_t *fb) {
+ static uint8_t raw[240 * (1 + 320 * 3)];
+ uint8_t *p = raw;
+ for (int y = 0; y < 240; y++) {
+ *p++ = 0;
+ for (int x = 0; x < 320; x++) {
+ uint16_t c = pal[fb[y * 320 + x]];
+ int r = c >> 10 & 31, g = c >> 5 & 31, b = c & 31;
+ *p++ = (uint8_t)(r << 3 | r >> 2), *p++ = (uint8_t)(g << 3 | g >> 2), *p++ = (uint8_t)(b << 3 | b >> 2);
+ }
+ }
+ uLongf zn = compressBound(sizeof raw);
+ uint8_t *z = malloc(zn);
+ compress2(z, &zn, raw, sizeof raw, 6);
+ FILE *f = fopen(path, "wb");
+ if (!f) return;
+ static const uint8_t sig[8] = {137, 'P', 'N', 'G', 13, 10, 26, 10};
+ fwrite(sig, 1, 8, f);
+ uint8_t ihdr[13] = {0, 0, 1, 64, 0, 0, 0, 240, 8, 2, 0, 0, 0};
+ struct { const char *t; const uint8_t *d; uLong n; } ch[3] = {{"IHDR", ihdr, 13}, {"IDAT", z, zn}, {"IEND", NULL, 0}};
+ for (int i = 0; i < 3; i++) {
+ uint8_t len[4] = {(uint8_t)(ch[i].n >> 24), (uint8_t)(ch[i].n >> 16), (uint8_t)(ch[i].n >> 8), (uint8_t)ch[i].n};
+ fwrite(len, 1, 4, f);
+ fwrite(ch[i].t, 1, 4, f);
+ if (ch[i].n) fwrite(ch[i].d, 1, ch[i].n, f);
+ uLong crc = crc32(0, (const Bytef *)ch[i].t, 4);
+ if (ch[i].n) crc = crc32(crc, ch[i].d, (uInt)ch[i].n);
+ uint8_t c[4] = {(uint8_t)(crc >> 24), (uint8_t)(crc >> 16), (uint8_t)(crc >> 8), (uint8_t)crc};
+ fwrite(c, 1, 4, f);
+ }
+ fclose(f);
+ free(z);
+}
+
+void plat_frame(void) {
+ g_shown = g_fb;
+ cur ^= 1;
+ g_fb = fbs[cur ^ 1];
+ frames++;
+ clock_ticks += 1093; /* 30 a second */
+ /* the pictures asked for: the frame drawn after update n */
+ if (shots) {
+ const char *s = shots;
+ while (*s) {
+ long n = strtol(s, (char **)&s, 10);
+ if (n && updates >= n && updates - n < 2) {
+ char path[512];
+ snprintf(path, sizeof path, "%s/shot_%05ld.png", out_dir, n);
+ png(path, g_shown);
+ }
+ if (*s == ',') s++;
+ else break;
+ }
+ }
+}
+void plat_begin_draw(void) {}
+void plat_palette(const uint16_t *p, int first, int n) { memcpy(pal + first, p, (size_t)n * 2); }
+
+unsigned plat_keys(void) {
+ updates++;
+ if (getenv("CEL_TRACE") && g_player.e)
+ printf("%ld st %d co %d x %d y %d sx %d sy %d ground %d dashes %d dead %d room %s\n", updates, g_player.state, g_player.co_step,
+ g_player.e->x, g_player.e->y, g_player.sx, g_player.sy, g_player.on_ground, g_player.dashes, g_player.dead,
+ g_level.room ? level_room_name() : "-");
+ if (updates > max_updates) return K_QUIT;
+ if (script_left <= 0) next_step();
+ script_left--;
+ return script_keys;
+}
+unsigned plat_ticks(void) { return clock_ticks; }
+void plat_wait(unsigned t) { clock_ticks += t ? t : 1; }
+
+const uint8_t *plat_appvar(const char *name) {
+ static struct { char name[9]; uint8_t *data; } vars[32];
+ for (int i = 0; i < 32 && vars[i].data; i++)
+ if (!strcmp(vars[i].name, name)) return vars[i].data;
+ char path[512];
+ snprintf(path, sizeof path, "%s/%s.bin", getenv("CEL_DATA") ? getenv("CEL_DATA") : "data", name);
+ FILE *f = fopen(path, "rb");
+ if (!f) return NULL;
+ fseek(f, 0, SEEK_END);
+ long n = ftell(f);
+ fseek(f, 0, SEEK_SET);
+ uint8_t *d = malloc((size_t)n);
+ if (fread(d, 1, (size_t)n, f) != (size_t)n) n = 0;
+ fclose(f);
+ for (int i = 0; i < 32; i++)
+ if (!vars[i].data) {
+ strncpy(vars[i].name, name, 8);
+ vars[i].data = d;
+ break;
+ }
+ return d;
+}
+bool plat_save(const char *name, const void *data, unsigned n) {
+ char path[512];
+ snprintf(path, sizeof path, "%s/%s.sav", out_dir, name);
+ FILE *f = fopen(path, "wb");
+ if (!f) return false;
+ fwrite(data, 1, n, f);
+ fclose(f);
+ return true;
+}
+bool plat_load(const char *name, void *data, unsigned n) {
+ char path[512];
+ snprintf(path, sizeof path, "%s/%s.sav", out_dir, name);
+ FILE *f = fopen(path, "rb");
+ if (!f) return false;
+ bool ok = fread(data, 1, n, f) == n;
+ fclose(f);
+ return ok;
+}
+
+/* ZX0 (Einar Saukas), as the CE's zx0_Decompress reads it */
+static const uint8_t *zin;
+static uint8_t zbits, zmask, zlast;
+static int zback;
+static uint8_t zbyte(void) { return zlast = *zin++; }
+static int bit(void) {
+ if (zback) {
+ zback = 0;
+ return zlast & 1;
+ }
+ zmask >>= 1;
+ if (!zmask) zmask = 128, zbits = zbyte();
+ return zbits & zmask ? 1 : 0;
+}
+static int gamma(int inverted) {
+ int v = 1;
+ while (!bit()) v = v << 1 | (bit() ^ inverted);
+ return v;
+}
+void plat_unpack(void *dst, const void *src) {
+ uint8_t *o = dst;
+ int last = 1, len;
+ zin = src, zmask = 0, zback = 0;
+literals:
+ len = gamma(0);
+ while (len--) *o++ = zbyte();
+ if (bit()) goto new_offset;
+ len = gamma(0);
+ for (int i = 0; i < len; i++, o++) *o = o[-last];
+ if (!bit()) goto literals;
+new_offset:
+ last = gamma(1);
+ if (last == 256) return;
+ last = last * 128 - (zbyte() >> 1);
+ zback = 1;
+ len = gamma(0) + 1;
+ for (int i = 0; i < len; i++, o++) *o = o[-last];
+ if (bit()) goto new_offset;
+ goto literals;
+}
diff --git a/games/celeste/ce/makefile b/games/celeste/ce/makefile
new file mode 100644
index 00000000..ce7e8d64
--- /dev/null
+++ b/games/celeste/ce/makefile
@@ -0,0 +1,37 @@
+# Celeste for the TI-84 Plus CE: a CE C toolchain project (CEdev v15).
+# make bin/CELESTE.8xp and its appvars bin/CEL*.8xv
+# make host build/host/celeste: the same game on a computer, for the tests and pictures
+# make test the tests (tests/run.py) on the computer build
+# make data data/ and src/gen.h again from the game (CELESTE=<its Content folder>)
+all: target appvars
+
+NAME = CELESTE
+DESCRIPTION = "Celeste"
+COMPRESSED = YES
+COMPRESSED_MODE = zx0
+ARCHIVED = YES
+CFLAGS = -Wall -Wextra -Wno-unused-parameter -Oz
+HAS_PRINTF = NO
+
+include $(shell cedev-config --makefile)
+
+APPVARS = $(patsubst data/%.bin,bin/%.8xv,$(wildcard data/*.bin))
+appvars: $(APPVARS)
+bin/%.8xv: data/%.bin tools/appvar.py
+ @mkdir -p bin
+ python3 tools/appvar.py bin $<
+
+HOST_SRC = $(filter-out src/plat_ce.c,$(wildcard src/*.c)) host/plat_host.c
+HOST_FLAGS = -std=gnu11 -O1 -g -Wall -Wextra -Wno-unused-parameter -Isrc
+host: build/host/celeste
+build/host/celeste: $(HOST_SRC) $(wildcard src/*.h)
+ @mkdir -p $(@D)
+ cc $(HOST_FLAGS) -fsanitize=address,undefined $(HOST_SRC) -lz -o $@
+
+test: build/host/celeste
+ python3 tests/run.py build/host/celeste
+
+data:
+ python3 tools/pack_ce.py
+
+.PHONY: all appvars host test data
diff --git a/games/celeste/ce/src/ce.h b/games/celeste/ce/src/ce.h
index 9c285215..80073dfc 100644
--- a/games/celeste/ce/src/ce.h
+++ b/games/celeste/ce/src/ce.h
@@ -36,6 +36,7 @@ extern uint8_t *g_fb; /* the buffer drawn this frame: 320 x 240
void plat_init(void);
void plat_exit(void);
void plat_frame(void); /* shows g_fb, and g_fb becomes the other buffer */
+void plat_begin_draw(void); /* waits until the buffer to draw is no longer shown */
void plat_palette(const uint16_t *pal, int first, int n); /* 1555 colours */
unsigned plat_keys(void);
unsigned plat_ticks(void); /* a 32768 Hz clock (wraps at 16 bits) */
@@ -80,6 +81,8 @@ const uint8_t *player_meta(int sprite_id); /* s8 hair x, s8 hair y, u8 bangs |
const uint16_t *hair_mask(int which); /* 0 hair, 1-3 bangs: 10 rows (mask), then 10 (dark) */
extern const uint8_t *g_bg; /* the chapter's background (320 x 180), NULL if none */
extern int g_bg_camx, g_bg_camy;
+extern int g_chapter_berries; /* the chapter's strawberries */
+extern const uint8_t *g_remap, *g_remap_red, *g_remap_half; /* sprites drawn through g_remap if set */
/* ---------------------------------------------------------------- drawing (draw.c) */
enum { DF_FLIPX = 1, DF_FLIPY = 2, DF_SOLID = 4 }; /* DF_SOLID: every pixel in one colour (draw_color) */
@@ -100,6 +103,18 @@ void draw_layer_fg(void); /* fg tiles */
void draw_decals(int fg);
void draw_clear_screen(uint8_t c);
+/* ---------------------------------------------------------------- floats (sf.c): IEEE single precision in whole numbers */
+typedef uint32_t f32;
+f32 f_add(f32 a, f32 b), f_sub(f32 a, f32 b), f_mul(f32 a, f32 b), f_div(f32 a, f32 b);
+bool f_lt(f32 a, f32 b), f_le(f32 a, f32 b);
+f32 f_int(int v);
+int f_trunc(f32 a), f_floor(f32 a), f_round(f32 a), f_sign(f32 a);
+f32 f_approach(f32 v, f32 t, f32 d), f_min(f32 a, f32 b), f_max(f32 a, f32 b), f_abs(f32 a), f_lerp(f32 a, f32 b, f32 t);
+#define f_neg(a) ((f32)(a) ^ 0x80000000ul)
+#define f_pos(a) (!((a) & 0x80000000ul) && ((a) & 0x7FFFFFFFul)) /* > 0 */
+#define f_negative(a) (((a) & 0x80000000ul) && ((a) & 0x7FFFFFFFul)) /* < 0 */
+#define f_zero(a) (!((a) & 0x7FFFFFFFul))
+
/* ---------------------------------------------------------------- math */
static inline int imin(int a, int b) { return a < b ? a : b; }
static inline int imax(int a, int b) { return a > b ? a : b; }
@@ -108,17 +123,19 @@ static inline int isign(int a) { return (a > 0) - (a < 0); }
static inline int iclamp(int v, int lo, int hi) { return v < lo ? lo : v > hi ? hi : v; }
static inline int approach(int v, int t, int d) { return v <= t ? imin(v + d, t) : imax(v - d, t); }
int fdiv(int a, int b); /* floor(a / b), b > 0 */
-int round_mu(int c, int dir); /* c / MU rounded as Celeste's floats round it (see level.c) */
+int round_mu(int c, int dir); /* c / MU rounded as Celeste's floats round it (util.c) */
+int bias_add(int b, int amount); /* the bias round_mu needs, kept within 24 bits */
int isqrt(int32_t v);
-int sin256(int a); /* a: 1024 a turn, result * 256 */
-static inline int cos256(int a) { return sin256(a + 256); }
+int sin15(int a); /* a: 1024 a turn, result * 32768 */
+static inline int sin256(int a) { return sin15(a) >> 7; }
+static inline int cos256(int a) { return sin15(a + 256) >> 7; }
uint32_t rnd(void);
static inline int rndn(int n) { return (int)((rnd() >> 8) % (uint32_t)n); }
/* ---------------------------------------------------------------- input */
typedef struct {
- bool check, pressed; /* held; went down this frame (or buffered) */
- int8_t buffer; /* frames the press is kept (Celeste's 0.08 s) */
+ bool check, edge, consumed; /* held; went down this frame; ConsumePress */
+ int8_t buffer; /* time units the press is kept (Celeste's 0.08 s) */
} Button;
typedef struct {
Button jump, dash, grab, pause, ok, back, talk;
@@ -127,8 +144,9 @@ typedef struct {
} Input;
extern Input g_in;
void input_update(unsigned keys);
-static inline bool btn_pressed(const Button *b) { return b->buffer > 0; }
-static inline void btn_consume(Button *b) { b->buffer = 0; }
+static inline bool btn_pressed(const Button *b) { return !b->consumed && (b->buffer > 0 || b->edge); }
+static inline void btn_consume(Button *b) { b->buffer = 0, b->consumed = true; } /* ConsumePress */
+static inline void btn_consume_buffer(Button *b) { b->buffer = 0; }
/* ---------------------------------------------------------------- entities (ents.c) */
enum { /* Ent.flags */
@@ -142,13 +160,14 @@ struct Ent {
uint8_t sub; /* type's own little state */
int16_t depth;
int x, y;
- int remx, remy; /* MU */
+ f32 remx, remy; /* movementCounter (px) */
int16_t hx, hy, hw, hh; /* collider: left, top (from x, y), width, height */
- int liftx, lifty; /* speed units: a platform's LiftSpeed this frame */
+ f32 liftx, lifty; /* a platform's LiftSpeed this frame (px/s) */
const uint8_t *rec; /* its map record */
int t0, t1, t2, t3; /* the type's own numbers */
int16_t a0, a1, a2, a3;
void *ptr;
+ f32 ptr2;
};
#define MAX_ENTS 96
extern Ent g_ents[MAX_ENTS];
@@ -176,6 +195,7 @@ typedef struct {
uint8_t *grid; /* fg[tw * th], bg[tw * th], solid bits (rb a row) */
int rb;
const uint8_t *tiles[256];
+ const uint8_t *decals, *ents, *trigs;
uint8_t ntiles;
uint8_t flags;
int8_t camx, camy;
@@ -223,48 +243,53 @@ enum { INTRO_TRANSITION, INTRO_RESPAWN, INTRO_WALKINRIGHT, INTRO_WALKINLEFT, INT
#define HAIR_NODES 5
typedef struct {
Ent *e;
- int sx, sy; /* speed */
+ f32 sx, sy; /* Speed (px/s) */
int facing;
int state, prev_state;
- bool on_ground, was_on_ground, on_safe_ground, ducking, dead;
+ bool on_ground, was_on_ground, on_safe_ground, dead;
int dashes, max_dashes, last_dashes;
- int stamina; /* 1/36000 */
- int move_x;
- int jump_grace, var_jump, var_jump_speed, auto_jump_timer;
+ f32 stamina;
+ int move_x, aim_x, aim_y;
+ f32 jump_grace, var_jump, var_jump_speed, auto_jump_timer;
bool auto_jump;
- int force_move_x, force_move_x_timer;
- int dash_cooldown, dash_refill_cooldown, dash_attack;
- int dash_dx, dash_dy; /* 1/256 */
- int last_aim_x, last_aim_y; /* aim: -1, 0, 1 */
- int wall_slide_timer, wall_slide_dir;
- int climb_no_move;
- int wall_speed_retention, wall_speed_retained;
- int wall_boost_dir, wall_boost_timer;
- int max_fall;
- int hop_wait_x, hop_wait_x_speed;
+ int force_move_x;
+ f32 force_move_x_timer;
+ f32 dash_cooldown, dash_refill_cooldown, dash_attack;
+ f32 dash_dx, dash_dy;
+ f32 wall_slide_timer;
+ int wall_slide_dir;
+ f32 climb_no_move;
+ f32 wall_speed_retention, wall_speed_retained;
+ int wall_boost_dir;
+ f32 wall_boost_timer;
+ f32 max_fall;
+ int hop_wait_x;
+ f32 hop_wait_x_speed;
+ Ent *climb_hop_solid;
+ int climb_hop_x, climb_hop_y;
int last_climb_move;
bool dash_started_on_ground, fast_jump, was_dash_b, called_dash_events;
- int before_dash_sx, before_dash_sy;
- int co_step, co_wait; /* the state's coroutine */
+ f32 before_dash_sx, before_dash_sy;
+ int co_step;
+ f32 co_wait; /* the state's coroutine */
bool co_active;
- int dash_trail_timer;
- int lift_timer, last_liftx, last_lifty;
+ f32 lift_timer, last_liftx, last_lifty;
+ f32 intro_fy;
/* the sprite */
int anim, frame, anim_timer;
- int scale_x, scale_y; /* 1/256 */
+ int scale_x, scale_y; /* 1/1024 */
int idle_timer;
bool flash;
int hair_flash;
uint32_t hair_rgb;
int hairx[HAIR_NODES], hairy[HAIR_NODES]; /* 1/256 px */
- int hair_count, hair_wave;
+ int hair_count;
/* intro, respawn, death */
int intro_t, respawn_fromx, respawn_fromy;
int body_step, body_t, body_dx, body_dy, death_t;
int bodyx, bodyy;
- int death_color;
- int strawb_index, strawb_reset;
- int walk_target;
+ int strawb_index;
+ f32 strawb_reset;
} Player;
extern Player g_player;
void player_spawn(int x, int y, int intro);
diff --git a/games/celeste/ce/src/data.c b/games/celeste/ce/src/data.c
index 82672a19..3ddc6d74 100644
--- a/games/celeste/ce/src/data.c
+++ b/games/celeste/ce/src/data.c
@@ -10,6 +10,8 @@ int g_nrooms;
static const uint8_t *rooms;
const uint8_t *g_bg;
int g_bg_camx, g_bg_camy;
+int g_chapter_berries;
+const uint8_t *g_remap, *g_remap_red, *g_remap_half;
static uint16_t pal[256];
static uint8_t tint[3];
@@ -45,7 +47,9 @@ bool chapter_open(int n) {
memcpy(pal + COMMON, h + 6, (256 - COMMON) * 2);
const uint8_t *p = h + 6 + (256 - COMMON) * 2;
memcpy(tint, p, 3);
- p += 3;
+ g_chapter_berries = p[3];
+ g_remap_red = p + 4, g_remap_half = p + 4 + 256;
+ p += 4 + 512;
ntiles = (int)R16(p);
tile_chunks = p + 3;
p += 3 + 3 * p[2];
diff --git a/games/celeste/ce/src/draw.c b/games/celeste/ce/src/draw.c
index b5d78abd..ead0e42c 100644
--- a/games/celeste/ce/src/draw.c
+++ b/games/celeste/ce/src/draw.c
@@ -34,6 +34,8 @@ static void blit_sprite(const uint8_t *s, int x, int y, uint8_t flags) {
if (dx + b > VIEW_W) b = VIEW_W - dx;
if (a < b) {
if (solid) memset(row + dx + a, col, b - a);
+ else if (g_remap)
+ for (int i = a; i < b; i++) row[dx + i] = g_remap[px[i]];
else memcpy(row + dx + a, px + a, b - a);
}
} else {
@@ -41,7 +43,7 @@ static void blit_sprite(const uint8_t *s, int x, int y, uint8_t flags) {
int base = x + fw - 1 - x0 - at;
for (int i = 0; i < len; i++) {
int dx = base - i;
- if ((unsigned)dx < VIEW_W) row[dx] = solid ? col : px[i];
+ if ((unsigned)dx < VIEW_W) row[dx] = solid ? col : g_remap ? g_remap[px[i]] : px[i];
}
}
at += len;
@@ -59,7 +61,7 @@ void draw_sprite_scaled(const uint8_t *s, int x, int y, int sx, int sy, int ox,
}
/* decode into a buffer, then sample it */
static uint8_t buf[64 * 64];
- int fw = s[0], fh = s[1], x0 = s[2], y0 = s[3], w = s[4], h = s[5];
+ int fw = s[0], fh = s[1], x0 = s[2], y0 = s[3], h = s[5];
if (fw > 64 || fh > 64) return;
memset(buf, 0, (size_t)fw * fh);
const uint8_t *p = s + 6;
@@ -90,7 +92,7 @@ void draw_sprite_scaled(const uint8_t *s, int x, int y, int sx, int sy, int ox,
int u = ox + fdiv(rel * 256 + 128, sx);
if (u < 0 || u >= fw) continue;
uint8_t c = buf[v * fw + u];
- if (c) row[vx] = (flags & DF_SOLID) ? g_draw_color : c;
+ if (c) row[vx] = (flags & DF_SOLID) ? g_draw_color : g_remap ? g_remap[c] : c;
}
}
}
@@ -251,7 +253,7 @@ void draw_decals(int fg) {
for (int k = 0; k < 2; k++) {
Room *rm = &g_level.rooms[k];
if (rm->index < 0) continue;
- const uint8_t *d = HPTR(rm->rec + rm->rec[0] + 1 + 3 + 3 + 2 + 2 + 3 + 4 + 1 + 2 * (rm->ntiles + 1) + 2 + 3);
+ const uint8_t *d = rm->decals;
int n = d[fg], skip = fg ? d[0] : 0;
const uint8_t *p = d + 2 + 8 * skip;
for (int i = 0; i < n; i++, p += 8) {
diff --git a/games/celeste/ce/src/ents.c b/games/celeste/ce/src/ents.c
new file mode 100644
index 00000000..e21cdf09
--- /dev/null
+++ b/games/celeste/ce/src/ents.c
@@ -0,0 +1,877 @@
+/* Entities: what the rooms hold (their records: tools/pack_ce.py Chapter.entity), after src/entities.c and berry.c,
+ * which port the game's own classes. Platforms move whole pixels and carry or push Madeline as Monocle's Solids do. */
+#include "ce.h"
+#include "fconst.h"
+
+Ent g_ents[MAX_ENTS];
+int g_nents;
+static int trig_ox, trig_oy;
+
+/* a record: u8 type, u8 size, s16 x, y, u16 w, h, u16 id, u8 nodes, (s16 x, y) each, u8 flags, u8 n, u16 vals */
+#define REC_X(r) S16((r) + 2)
+#define REC_Y(r) S16((r) + 4)
+#define REC_W(r) (int)R16((r) + 6)
+#define REC_H(r) (int)R16((r) + 8)
+#define REC_ID(r) (int)R16((r) + 10)
+#define REC_NODES(r) ((r)[12])
+static const uint8_t *rec_node(const uint8_t *r, int i) { return r + 13 + 4 * i; }
+static int rec_flags(const uint8_t *r) { return r[13 + 4 * r[12]]; }
+static int rec_nvals(const uint8_t *r) { return r[14 + 4 * r[12]]; }
+static int rec_val(const uint8_t *r, int i) { return (int)R16(r + 15 + 4 * r[12] + 2 * i); }
+
+Ent *ent_new(int type, int x, int y) {
+ Ent *e = NULL;
+ for (int i = 0; i < g_nents; i++)
+ if (g_ents[i].type == 255) { e = &g_ents[i]; break; }
+ if (!e) {
+ if (g_nents >= MAX_ENTS) return NULL;
+ e = &g_ents[g_nents++];
+ }
+ memset(e, 0, sizeof *e);
+ e->type = (uint8_t)type;
+ e->flags = EF_ACTIVE | EF_VISIBLE | EF_COLLIDABLE;
+ e->room = (uint8_t)g_level.slot;
+ e->x = x, e->y = y;
+ return e;
+}
+void ent_free(Ent *e) {
+ e->type = 255;
+ e->flags = 0;
+ while (g_nents && g_ents[g_nents - 1].type == 255) g_nents--;
+}
+static void box(Ent *e, int w, int h, int x, int y) { e->hw = (int16_t)w, e->hh = (int16_t)h, e->hx = (int16_t)x, e->hy = (int16_t)y; }
+
+bool rect_hits(int l, int t, int r, int b, const Ent *o) {
+ return r > e_left(o) && b > e_top(o) && l < e_right(o) && t < e_bottom(o);
+}
+bool ent_collide_at(const Ent *e, int x, int y, const Ent *o) {
+ int l = x + e->hx, t = y + e->hy;
+ return rect_hits(l, t, l + e->hw, t + e->hh, o);
+}
+
+/* ---------------------------------------------------------------- Madeline and the platforms */
+extern bool player_riding(const Ent *s), player_riding_jumpthru(const Ent *j);
+extern void player_push_h(int move, Ent *pusher), player_push_v(int move, Ent *pusher);
+extern void player_carry_h(int move), player_carry_v(int move);
+extern void player_set_lift(f32 lx, f32 ly);
+extern bool player_climbing(void);
+static Ent *pl(void) { return g_player.e && !g_player.dead ? g_player.e : NULL; }
+static bool player_on_top(const Ent *s) { Ent *p = pl(); return p && ent_collide_at(s, s->x, s->y - 1, p); }
+static bool player_climbing_on(const Ent *s) {
+ Ent *p = pl();
+ if (!p || !player_climbing()) return false;
+ return g_player.facing < 0 ? ent_collide_at(s, s->x + 1, s->y, p) : ent_collide_at(s, s->x - 1, s->y, p);
+}
+
+static void movers_move(Ent *p, int dx, int dy) { /* StaticMovers ride along */
+ for (int i = 0; i < g_nents; i++)
+ if (g_ents[i].type != 255 && g_ents[i].ptr == p && g_ents[i].type < 64) g_ents[i].x += dx, g_ents[i].y += dy;
+}
+static void solid_move_h_exact(Ent *s, int move) {
+ Ent *p = pl();
+ bool rider = p && (s->flags & EF_COLLIDABLE) && player_riding(s);
+ s->x += move;
+ movers_move(s, move, 0);
+ if (!(s->flags & EF_COLLIDABLE) || !p) return;
+ if (ent_collide_at(s, s->x, s->y, p)) {
+ int mv = move > 0 ? e_right(s) - e_left(p) : e_left(s) - e_right(p);
+ s->flags &= ~EF_COLLIDABLE;
+ player_push_h(mv, s);
+ player_set_lift(s->liftx, s->lifty);
+ s->flags |= EF_COLLIDABLE;
+ } else if (rider) {
+ s->flags &= ~EF_COLLIDABLE;
+ player_carry_h(move);
+ player_set_lift(s->liftx, s->lifty);
+ s->flags |= EF_COLLIDABLE;
+ }
+}
+static void solid_move_v_exact(Ent *s, int move) {
+ Ent *p = pl();
+ bool rider = p && (s->flags & EF_COLLIDABLE) && player_riding(s);
+ s->y += move;
+ movers_move(s, 0, move);
+ if (!(s->flags & EF_COLLIDABLE) || !p) return;
+ if (ent_collide_at(s, s->x, s->y, p)) {
+ int mv = move > 0 ? e_bottom(s) - e_top(p) : e_top(s) - e_bottom(p);
+ s->flags &= ~EF_COLLIDABLE;
+ player_push_v(mv, s);
+ player_set_lift(s->liftx, s->lifty);
+ s->flags |= EF_COLLIDABLE;
+ } else if (rider) {
+ s->flags &= ~EF_COLLIDABLE;
+ player_carry_v(move);
+ player_set_lift(s->liftx, s->lifty);
+ s->flags |= EF_COLLIDABLE;
+ }
+}
+/* Platform.MoveH/MoveV (amount: px, a float), the lift speed amount / DT unless given */
+static void plat_move_h(Ent *s, f32 amount) {
+ s->liftx = f_div(amount, F_DT);
+ s->remx = f_add(s->remx, amount);
+ int n = f_round(s->remx);
+ if (n) s->remx = f_sub(s->remx, f_int(n)), solid_move_h_exact(s, n);
+}
+static void plat_move_v(Ent *s, f32 amount) {
+ s->lifty = f_div(amount, F_DT);
+ s->remy = f_add(s->remy, amount);
+ int n = f_round(s->remy);
+ if (n) s->remy = f_sub(s->remy, f_int(n)), solid_move_v_exact(s, n);
+}
+/* MoveVCollideSolids: down until something solid (or a jump-through) is in the way */
+static bool plat_fall(Ent *s, f32 amount) {
+ s->lifty = f_div(amount, F_DT);
+ s->remy = f_add(s->remy, amount);
+ int n = f_round(s->remy);
+ if (!n) return false;
+ s->remy = f_sub(s->remy, f_int(n));
+ int sg = isign(n), moved = 0;
+ bool hit = false;
+ while (n) {
+ if (solid_at(e_left(s), e_top(s) + moved + sg, e_right(s), e_bottom(s) + moved + sg, s) ||
+ (sg > 0 && jumpthru_first_outside(s, s->x, s->y + moved + sg))) {
+ hit = true;
+ break;
+ }
+ moved += sg, n -= sg;
+ }
+ if (moved) solid_move_v_exact(s, moved);
+ return hit;
+}
+static void shake_update(Ent *s) { /* Platform.Shake: a new offset every 0.04 s while shaking */
+ if (s->sub & 0x80) {
+ if (g_level.frame % 2 == 0) s->a2 = (int16_t)(rndn(3) - 1), s->a3 = (int16_t)(rndn(3) - 1);
+ if (s->a1 > 0 && (s->a1 -= DTU) <= 0) s->sub &= 0x7F, s->a2 = s->a3 = 0;
+ }
+}
+static void shake_start(Ent *s, int time) { s->sub |= 0x80, s->a1 = (int16_t)time; }
+static void shake_stop(Ent *s) { s->sub &= 0x7F, s->a2 = s->a3 = 0; }
+
+/* ---------------------------------------------------------------- the leader (strawberries follow Madeline) */
+#define MAX_FOLLOW 8
+#define PAST 41
+static int pastx[PAST], pasty[PAST], npast;
+static Ent *fol[MAX_FOLLOW];
+static int nfol;
+static int fol_delay[MAX_FOLLOW];
+static int fol_index(const Ent *e) {
+ for (int i = 0; i < nfol; i++)
+ if (fol[i] == e) return i;
+ return -1;
+}
+static void leader_update(void) {
+ Ent *p = g_player.e;
+ if (!p) return;
+ int x = p->x, y = p->y - 8;
+ if (g_level.frame % 2 == 0 && (npast == 0 || iabs(x - pastx[0]) + iabs(y - pasty[0]) >= 3)) { /* every 0.02 s */
+ memmove(pastx + 1, pastx, sizeof pastx - sizeof pastx[0]);
+ memmove(pasty + 1, pasty, sizeof pasty - sizeof pasty[0]);
+ pastx[0] = x, pasty[0] = y;
+ if (npast < PAST) npast++;
+ }
+ for (int i = 0, k = 5; i < nfol && k < npast; i++, k += 5) {
+ if (fol_delay[i] > 0) { fol_delay[i] -= DTU; continue; }
+ Ent *f = fol[i]; /* 1 - 0.01 ^ dt of the way, in 1/256 px */
+ f->t0 += (int)(((int32_t)(pastx[k] * 256 - f->t0) * 1210) >> 14);
+ f->t1 += (int)(((int32_t)(pasty[k] * 256 - f->t1) * 1210) >> 14);
+ f->x = f->t0 >> 8, f->y = f->t1 >> 8;
+ }
+}
+static void leader_lose(Ent *e) {
+ int i = fol_index(e);
+ if (i < 0) return;
+ memmove(fol + i, fol + i + 1, sizeof(Ent *) * (size_t)(nfol - i - 1));
+ memmove(fol_delay + i, fol_delay + i + 1, sizeof(int) * (size_t)(nfol - i - 1));
+ nfol--;
+}
+
+/* ---------------------------------------------------------------- strawberries */
+enum { BERRY_GOLD = 1, BERRY_GHOST = 2, BERRY_WINGED = 4, BERRY_FLYING = 8, BERRY_COLLECTED = 16, BERRY_HOME = 32 };
+typedef struct { uint16_t room_id; uint8_t index; } BerryKey;
+extern bool save_has_berry(int chapter, int index);
+extern void save_add_berry(int chapter, int index, bool golden);
+extern int g_berries_now; /* this go's strawberries (the HUD) */
+static void points_new(int x, int y, int index, bool ghost);
+
+static int berry_anim(Ent *e, int which) { /* 0 idle, 1 collect, 2 flap */
+ int base = (e->sub & BERRY_GOLD) ? A_goldberry_idle : (e->sub & BERRY_GHOST) ? A_ghostberry_idle : A_strawberry_idle;
+ return base + (which == 0 ? 0 : which == 1 ? (A_strawberry_collect - A_strawberry_idle) : (A_strawberry_flap - A_strawberry_idle));
+}
+static void anim_step(Ent *e) { /* a0 anim, a1 frame, t3 timer (time units); a1 = -1: done */
+ const uint8_t *an = anim(e->a0);
+ int delay = (int)R16(an + 2);
+ if (!delay || e->a1 < 0) return;
+ e->t3 += DTU;
+ while (e->t3 >= delay && e->a1 >= 0) {
+ e->t3 -= delay;
+ if (e->a1 + 1 < an[0]) e->a1++;
+ else if (an[4] != 255) e->a0 = an[4], e->a1 = 0;
+ else if (an[1] & 1) e->a1 = 0;
+ else e->a1 = -1;
+ }
+}
+static int anim_sprite(const Ent *e) {
+ const uint8_t *an = anim(e->a0);
+ int f = e->a1 < 0 ? an[0] - 1 : imin(e->a1, an[0] - 1);
+ return (int)R16(an + 6 + 2 * f);
+}
+static void anim_play(Ent *e, int a) {
+ if (e->a0 == a && e->a1 >= 0) return;
+ e->a0 = (int16_t)a, e->a1 = 0, e->t3 = 0;
+}
+
+static void berry_collect(Ent *e) {
+ if (e->sub & BERRY_COLLECTED) return;
+ e->sub |= BERRY_COLLECTED;
+ int index = 0;
+ if (fol_index(e) >= 0) {
+ index = g_player.strawb_index++;
+ g_player.strawb_reset = TIM(2.5f);
+ leader_lose(e);
+ }
+ save_add_berry(g_level.chapter, rec_val(e->rec, 2), e->sub & BERRY_GOLD);
+ g_berries_now++;
+ e->t2 = index;
+ e->depth = -32000;
+ anim_play(e, berry_anim(e, 1));
+}
+static void berry_update(Ent *e) {
+ if (!(e->sub & BERRY_COLLECTED)) {
+ e->a2 = (int16_t)(e->a2 + 174); /* the wobble: 4 radians a second (16384ths of a turn) */
+ int f = fol_index(e);
+ if (f >= 0 && fol_delay[f] <= 0 && f == 0) {
+ Player *p = &g_player;
+ bool ok = !p->dead && ((e->sub & BERRY_GOLD) ? g_level.completed : p->on_safe_ground);
+ if (ok) {
+ if ((e->a3 += DTU) > TIM(.15f)) berry_collect(e);
+ } else if (e->a3 > 0)
+ e->a3 = 0;
+ } else if (e->sub & BERRY_WINGED) {
+ e->t1 += e->t2; /* flap speed, speed units */
+ int n = round_mu(e->t1, e->t2);
+ e->y += n, e->t1 -= n * MU;
+ if (e->sub & BERRY_FLYING) {
+ if (e->y < g_level.rooms[e->room].y - 16) { ent_free(e); return; }
+ e->t2 = imax(e->t2 - 1200 / 6, SPD(-200)) ;
+ } else {
+ e->t2 = approach(e->t2, SPD(20), 170);
+ int sy = e->rec ? g_level.rooms[e->room].y + REC_Y(e->rec) : e->y;
+ e->y = iclamp(e->y, sy - 5, sy + 5);
+ }
+ }
+ }
+ anim_step(e);
+ if ((e->sub & BERRY_COLLECTED) && e->a1 < 0) {
+ points_new(e->x, e->y, e->t2, e->sub & BERRY_GHOST);
+ ent_free(e);
+ }
+}
+static void berry_touch(Ent *e) {
+ if (fol_index(e) >= 0 || (e->sub & BERRY_COLLECTED) || nfol >= MAX_FOLLOW) return;
+ if (e->sub & BERRY_WINGED) e->sub &= ~(BERRY_WINGED | BERRY_FLYING), anim_play(e, berry_anim(e, 0));
+ fol[nfol] = e, fol_delay[nfol] = TIM(.3f), nfol++;
+ e->t0 = e->x * 256, e->t1 = e->y * 256;
+ e->depth = -32000;
+}
+static void berry_render(Ent *e) {
+ int y = e->y + ((e->sub & (BERRY_WINGED | BERRY_COLLECTED)) || fol_index(e) >= 0 ? 0 : sin15(((uint16_t)e->a2 >> 4) & 1023) * 2 / 32768);
+ const uint8_t *s = sprite(anim_sprite(e));
+ draw_sprite(s, e->x - s[0] / 2, y - s[1] / 2, 0);
+}
+/* the dash makes winged ones fly off */
+void ents_on_dash(void) {
+ for (int i = 0; i < g_nents; i++) {
+ Ent *e = &g_ents[i];
+ if ((e->type == ET_strawberry || e->type == ET_goldenBerry) && (e->sub & BERRY_WINGED) && !(e->sub & BERRY_FLYING) &&
+ fol_index(e) < 0)
+ e->sub |= BERRY_FLYING, e->t2 = SPD(-200), e->depth = -32000;
+ }
+}
+/* the end of the chapter: what follows is collected */
+void ents_collect_berries(void) {
+ while (nfol) berry_collect(fol[0]);
+}
+/* a death: the followers go home */
+void ents_lose_followers(void) { nfol = 0, npast = 0; }
+
+/* StrawberryPoints: "1000" and up, rising */
+static void points_new(int x, int y, int index, bool ghost) {
+ Ent *e = ent_new(ET_strawberry, x, y);
+ if (!e) return;
+ e->type = 200;
+ e->room = 2;
+ e->flags = EF_ACTIVE | EF_VISIBLE;
+ e->depth = -32700;
+ e->a0 = (int16_t)(A_strawberry_fade0 + imin(index, 5));
+ (void)ghost;
+}
+static void points_update(Ent *e) {
+ e->t1 -= SPD(8);
+ int n = round_mu(e->t1, -1);
+ e->y += n, e->t1 -= n * MU;
+ e->x = iclamp(e->x, g_camx + 8, g_camx + 312), e->y = iclamp(e->y, g_camy + 8, g_camy + 172);
+ anim_step(e);
+ if (e->a1 < 0) ent_free(e);
+}
+
+/* ---------------------------------------------------------------- spikes */
+static void spikes_touch(Ent *e) {
+ Player *p = &g_player;
+ switch (e->type) {
+ case ET_spikesUp: if (!f_negative(p->sy) && e_bottom(p->e) <= e_bottom(e)) player_die(0, -1); break;
+ case ET_spikesDown: if (!f_pos(p->sy)) player_die(0, 1); break;
+ case ET_spikesLeft: if (!f_negative(p->sx)) player_die(-1, 0); break;
+ case ET_spikesRight: if (!f_pos(p->sx)) player_die(1, 0); break;
+ }
+}
+static void spikes_render(Ent *e) {
+ const uint8_t *r = e->rec;
+ int n = rec_nvals(r), x = e->x + e->a2, y = e->y + e->a3;
+ if (e->ptr) x += ((Ent *)e->ptr)->a2, y += ((Ent *)e->ptr)->a3;
+ for (int j = 0; j < n; j++) {
+ const uint8_t *s = csprite(rec_val(r, j));
+ int fw = s[0], fh = s[1];
+ switch (e->type) {
+ case ET_spikesUp: draw_sprite(s, x + j * 8 + 4 - fw / 2, y + 1 - fh, 0); break;
+ case ET_spikesDown: draw_sprite(s, x + j * 8 + 4 - fw / 2, y - 1, 0); break;
+ case ET_spikesRight: draw_sprite(s, x - 1, y + j * 8 + 4 - fh / 2, 0); break;
+ case ET_spikesLeft: draw_sprite(s, x + 1 - fw, y + j * 8 + 4 - fh / 2, 0); break;
+ }
+ }
+}
+
+/* ---------------------------------------------------------------- tiles in a row or a box */
+static void tiles_render(Ent *e, int first, int cols, int n, int ox, int oy) {
+ for (int i = 0; i < n; i++) draw_tile(rec_val(e->rec, first + i), e->x + ox + (i % cols) * 8, e->y + oy + (i / cols) * 8);
+}
+
+/* ---------------------------------------------------------------- spring */
+static const uint8_t SPRING_SEQ[] = {0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5};
+static void spring_touch(Ent *e) {
+ if (e->type == ET_spring) {
+ if (f_negative(g_player.sy)) return;
+ player_super_bounce(e_top(e));
+ } else if (e->type == ET_wallSpringLeft)
+ player_side_bounce(1, e_right(e), e->y);
+ else
+ player_side_bounce(-1, e_left(e), e->y);
+ e->a0 = 1, e->t0 = 0; /* the animation */
+ e->t1 = 256; /* the wiggle */
+}
+static void spring_update(Ent *e) {
+ if (e->a0) {
+ e->t0 += DTU;
+ while (e->t0 >= TIM(.07f) && e->a0) {
+ e->t0 -= TIM(.07f);
+ if (++e->a0 > (int)sizeof SPRING_SEQ) e->a0 = 0;
+ }
+ }
+ if (e->t1 > 0) e->t1 = imax(e->t1 - 256 * DTU / TIM(1), 0);
+}
+static void spring_render(Ent *e) {
+ int f = e->a0 ? SPRING_SEQ[e->a0 - 1] : 0;
+ const uint8_t *s = sprite(S_objects_spring_00 + f);
+ int x = e->x, y = e->y;
+ if (e->ptr) x += ((Ent *)e->ptr)->a2, y += ((Ent *)e->ptr)->a3;
+ if (e->type == ET_spring) draw_sprite(s, x - 8, y - 16, 0);
+ else { /* on a wall: turned a quarter (the sprite is 16 x 16, its bottom against the wall) */
+ static uint8_t buf[6 + 16 * (1 + 16 * 3)];
+ (void)buf;
+ draw_sprite(s, e->type == ET_wallSpringLeft ? x - 8 : x - 8, y - 8, e->type == ET_wallSpringRight ? DF_FLIPX : 0);
+ }
+}
+
+/* ---------------------------------------------------------------- refill */
+static void refill_touch(Ent *e) {
+ if (!player_use_refill(rec_flags(e->rec) & 1)) return;
+ e->flags &= ~EF_COLLIDABLE;
+ e->t0 = TIM(2.5f); /* respawn */
+ e->sub = 1;
+ level_freeze(TIM(.05f));
+}
+static void refill_update(Ent *e) {
+ if (e->sub == 1) e->sub = 2;
+ else if (e->sub == 2) {
+ level_shake(TIM(.3f) / DTU);
+ e->sub = 3;
+ e->depth = 8999;
+ if (rec_flags(e->rec) & 2) { ent_free(e); return; }
+ }
+ if (e->t0 > 0) {
+ e->t0 -= DTU;
+ if (e->t0 <= 0 && !(e->flags & EF_COLLIDABLE)) e->flags |= EF_COLLIDABLE, e->sub = 0, e->depth = -100;
+ }
+ e->t1 += DTU; /* frames: 0.1 s each */
+ e->t2 += DTU; /* the sine: 0.6 a second */
+}
+static void refill_render(Ent *e) {
+ if (e->sub >= 3 && !(e->flags & EF_COLLIDABLE)) {
+ draw_sprite(sprite(S_objects_refill_outline), e->x - 8, e->y - 8, 0);
+ return;
+ }
+ int y = e->y + sin15(e->t2 * 1024 / 1000) * 2 / 32768; /* SineWave(0.6) */
+ int f = e->t1 / TIM(.1f) % 5;
+ draw_sprite(sprite(S_objects_refill_idle00 + f), e->x - 8, y - 8, 0);
+ int fl = e->t1 % TIM(2); /* a flash every 2 s */
+ if (fl < 6 * TIM(.05f)) draw_sprite(sprite(S_objects_refill_flash00 + fl / TIM(.05f)), e->x - 8, y - 8, 0);
+}
+
+/* ---------------------------------------------------------------- crumble block */
+/* sub: 0 waiting, 1 shaking (t0 particles left), 2 falling soon, 3 gone; t1 timer; a0 the player was on top */
+static void crumble_update(Ent *e) {
+ switch (e->sub & 0x7F) {
+ case 0: {
+ bool top = player_on_top(e);
+ if (!top && !player_climbing_on(e)) break;
+ e->a0 = top;
+ e->t0 = top ? 1 : 3;
+ e->t1 = TIM(.2f);
+ e->t2 = top ? TIM(.6f) : TIM(1); /* the shake */
+ e->sub = 1;
+ break;
+ }
+ case 1:
+ if ((e->t1 -= DTU) > 0) break;
+ if (--e->t0) { e->t1 = TIM(.2f); break; }
+ e->t1 = TIM(.4f), e->sub = 2;
+ if (e->a0 && !player_on_top(e)) goto fall;
+ break;
+ case 2:
+ e->t1 -= DTU;
+ if (e->t1 <= 0 || (e->a0 && !player_on_top(e))) goto fall;
+ break;
+ case 3: /* back after 2 s, when nothing is in its way */
+ if ((e->t1 -= DTU) > 0) break;
+ if (pl() && ent_collide_at(e, e->x, e->y, pl())) break;
+ e->flags |= EF_COLLIDABLE;
+ e->sub = 0, e->t3 = 0;
+ break;
+ }
+ if (e->t2 > 0) e->t2 -= DTU;
+ if (e->sub == 3 || e->t3 < 0) e->t3 += DTU;
+ return;
+fall:
+ e->flags &= ~EF_COLLIDABLE;
+ e->sub = 3, e->t1 = TIM(2), e->t3 = 0;
+}
+static void crumble_render(Ent *e) {
+ int n = rec_nvals(e->rec) / 2;
+ if (e->sub == 3) { /* the pieces fall (0.4 s, each group of four 0.05 s after the last), then the outline */
+ for (int i = 0; i < n; i++) {
+ int t = e->t3 - (i % 4) * TIM(.05f);
+ if (t < 0) t = 0;
+ if (t >= TIM(.4f)) continue;
+ int k = t * 256 / TIM(.4f), fall = k * k / 256 * k / 256 * (((e->x + i * 8) * 7 % 3 + 1) * 12) / 256;
+ draw_tile(rec_val(e->rec, i), e->x + i * 8, e->y + fall);
+ }
+ if (e->t3 > TIM(.25f)) tiles_render(e, n, n, n, 0, 0);
+ return;
+ }
+ for (int i = 0; i < n; i++) {
+ int sx = 0, sy = 0;
+ if (e->t2 > 0) sx = rndn(3) - 1, sy = rndn(3) - 1;
+ draw_tile(rec_val(e->rec, i), e->x + i * 8 + sx, e->y + sy);
+ }
+}
+
+/* ---------------------------------------------------------------- falling block */
+static bool fall_check(Ent *e) { return (rec_flags(e->rec) & 1) ? (pl() && player_riding(e)) : player_on_top(e); }
+static void falling_update(Ent *e) {
+ shake_update(e);
+ switch (e->sub & 0x7F) {
+ case 0:
+ if (e->a0 || fall_check(e)) e->sub = (uint8_t)((e->sub & 0x80) | 1);
+ break;
+ case 1:
+ shake_start(e, 0);
+ e->t0 = TIM(.2f);
+ e->sub = 0x82;
+ break;
+ case 2:
+ if ((e->t0 -= DTU) > 0) break;
+ e->t0 = TIM(.4f);
+ e->sub = 0x83;
+ /* fall through */
+ case 3: {
+ bool wait = e->a0 || fall_check(e);
+ if (!wait && (rec_flags(e->rec) & 1) && pl()) wait = ent_collide_at(e, e->x - 1, e->y, pl()) || ent_collide_at(e, e->x + 1, e->y, pl());
+ if (e->t0 > 0 && wait) {
+ e->t0 -= DTU;
+ break;
+ }
+ shake_stop(e);
+ e->ptr2 = 0;
+ e->sub = 4;
+ break;
+ }
+ case 4: {
+ Room *rm = &g_level.rooms[e->room];
+ e->ptr2 = f_approach(e->ptr2, F_MaxFall, f_mul(F_DuckFriction, F_DT)); /* Approach(speed, 160, 500 * DT) */
+ if (plat_fall(e, f_mul(e->ptr2, F_DT))) {
+ level_dir_shake(0, 1, TIM(.3f) / DTU);
+ shake_start(e, 0);
+ e->t0 = TIM(.2f);
+ e->sub = 0x85;
+ break;
+ }
+ if (e_top(e) > rm->y + rm->h + 16 || (e_top(e) > rm->y + rm->h - 1 && solid_at(e_left(e), e_top(e) + 1, e_right(e), e_bottom(e) + 1, e))) {
+ for (int i = 0; i < g_nents; i++)
+ if (g_ents[i].ptr == e) ent_free(&g_ents[i]);
+ ent_free(e);
+ }
+ break;
+ }
+ case 5:
+ if ((e->t0 -= DTU) > 0) break;
+ shake_stop(e);
+ if (tiles_solid(e_left(e), e_bottom(e), e_right(e), e_bottom(e) + 1)) {
+ e->sub = 7;
+ e->flags |= EF_SAFE;
+ break;
+ }
+ e->sub = 6, e->t0 = 0;
+ /* fall through */
+ case 6:
+ if ((e->t0 -= DTU) > 0) break;
+ if (solid_at(e_left(e), e_top(e) + 1, e_right(e), e_bottom(e) + 1, e) || jumpthru_first_outside(e, e->x, e->y + 1)) {
+ e->t0 = TIM(.1f);
+ break;
+ }
+ e->sub = 1;
+ break;
+ }
+}
+static void block_render(Ent *e) { tiles_render(e, 0, REC_W(e->rec) / 8, rec_nvals(e->rec), e->a2, e->a3); }
+
+/* ---------------------------------------------------------------- dash block: breaks when dashed into */
+void ents_dash_into(Ent *hit, int dx, int dy, bool *rebound) {
+ *rebound = false;
+ if (!hit || hit->type != ET_dashBlock || !(rec_flags(hit->rec) & 2)) return;
+ (void)dx, (void)dy;
+ level_shake(TIM(.2f) / DTU);
+ ent_free(hit);
+ *rebound = true;
+}
+
+/* ---------------------------------------------------------------- fake walls: fade when entered */
+static void fakewall_update(Ent *e) {
+ Ent *p = pl();
+ bool in = p && ent_collide_at(e, e->x, e->y, p);
+ if (e->type == ET_coverupWall) {
+ e->t0 = approach(e->t0, in ? 0 : 256, 256 * 4 * DTU / TIM(1));
+ return;
+ }
+ if (e->sub) {
+ e->t0 = approach(e->t0, 0, 256 * 2 * DTU / TIM(1));
+ if (e->t0 <= 0) ent_free(e);
+ } else if (in)
+ e->sub = 1;
+}
+static void fakewall_render(Ent *e) {
+ /* no blending at 8 bits: shown while more than half there, then gone (fades in a frame or two) */
+ if (e->t0 > 128) block_render(e);
+}
+
+/* ---------------------------------------------------------------- jump-through */
+static void jumpthru_render(Ent *e) { tiles_render(e, 0, 64, rec_nvals(e->rec), 0, 0); }
+
+/* ---------------------------------------------------------------- zip mover */
+/* sub: step (bit 7: shaking); t0 the frame of its move (its Ease.SineIn: ZIP_OUT and ZIP_BACK, worked out as the game
+ * works them, in tools/fconst.py), t1 a wait; a0 its light */
+static const f32 zip_out[ZIP_OUT_N] = ZIP_OUT, zip_back[ZIP_BACK_N] = ZIP_BACK;
+static void zip_moveto(Ent *e, bool back, f32 pct) { /* Platform.MoveTo(Vector2.Lerp(from, to, pct)) */
+ Room *rm = &g_level.rooms[e->room];
+ f32 sx = f_int(rm->x + REC_X(e->rec)), sy = f_int(rm->y + REC_Y(e->rec));
+ const uint8_t *n = rec_node(e->rec, 0);
+ f32 tx = f_int(rm->x + S16(n)), ty = f_int(rm->y + S16(n + 2));
+ f32 x = back ? f_lerp(tx, sx, pct) : f_lerp(sx, tx, pct), y = back ? f_lerp(ty, sy, pct) : f_lerp(sy, ty, pct);
+ plat_move_h(e, f_sub(x, f_add(f_int(e->x), e->remx)));
+ plat_move_v(e, f_sub(y, f_add(f_int(e->y), e->remy)));
+}
+static void zip_update(Ent *e) {
+ shake_update(e);
+ e->liftx = e->lifty = 0;
+ int st = e->sub & 0x7F;
+ switch (st) {
+ case 0:
+ if (!(pl() && player_riding(e))) break;
+ shake_start(e, TIM(.1f));
+ e->t1 = TIM(.1f), st = 1;
+ break;
+ case 1:
+ if ((e->t1 -= DTU) > 0) break;
+ e->a0 = 3, e->t0 = 0, st = 2;
+ break;
+ case 2: /* there in half a second, Ease.SineIn */
+ zip_moveto(e, false, zip_out[e->t0]);
+ if (++e->t0 >= ZIP_OUT_N) {
+ shake_start(e, TIM(.2f));
+ level_shake(TIM(.3f) / DTU);
+ e->t1 = TIM(.5f), st = 3;
+ }
+ break;
+ case 3:
+ if ((e->t1 -= DTU) > 0) break;
+ e->a0 = 2, e->t0 = 0, st = 4;
+ break;
+ case 4: /* back in two seconds */
+ zip_moveto(e, true, zip_back[e->t0]);
+ if (++e->t0 >= ZIP_BACK_N) {
+ shake_start(e, TIM(.2f));
+ e->a0 = 1, e->t1 = TIM(.5f), st = 5;
+ }
+ break;
+ case 5:
+ if ((e->t1 -= DTU) > 0) break;
+ st = 0;
+ break;
+ }
+ e->sub = (uint8_t)((e->sub & 0x80) | st);
+}
+static void zip_render(Ent *e) {
+ int x = e->x + e->a2, y = e->y + e->a3, w = e->hw, h = e->hh;
+ draw_rect(x + 1, y + 1, w - 2, h - 2, BLACK);
+ tiles_render(e, 0, w / 8, rec_nvals(e->rec), e->a2, e->a3);
+}
+
+/* ---------------------------------------------------------------- triggers */
+static void trig_enter(Ent *e) {
+ switch (e->type - 64) {
+ case TT_cameraOffsetTrigger:
+ g_level.cam_offx = (int16_t)rec_val(e->rec, 0), g_level.cam_offy = (int16_t)rec_val(e->rec, 1);
+ break;
+ case TT_changeRespawnTrigger: {
+ const uint8_t *n = REC_NODES(e->rec) ? rec_node(e->rec, 0) : NULL;
+ int tx = n ? g_level.rooms[e->room].x + S16(n) : e->x + e->hw / 2, ty = n ? g_level.rooms[e->room].y + S16(n + 2) : e->y + e->hh / 2;
+ if (!solid_at(tx, ty - 4, tx + 1, ty - 3, NULL)) g_level.respawn_x = e->t0, g_level.respawn_y = e->t1;
+ (void)tx;
+ break;
+ }
+ case TT_eventTrigger:
+ if (rec_val(e->rec, 0) == EV_end_city) {
+ extern void game_chapter_end(void);
+ game_chapter_end();
+ }
+ break;
+ }
+}
+static void trig_stay(Ent *e) {
+ if (e->type - 64 != TT_cameraTargetTrigger) return;
+ Ent *p = g_player.e;
+ int px = (e_left(p) + e_right(p)) / 2, py = (e_top(p) + e_bottom(p)) / 2;
+ int l = e_left(e), r = e_right(e), t = e_top(e), b = e_bottom(e), cx = (l + r) / 2, cy = (t + b) / 2;
+#define CMAP(v, a, bb) iclamp(((v) - (a)) * 256 / ((bb) - (a)), 0, 256)
+ int k;
+ switch (rec_val(e->rec, 1)) {
+ case 1: k = CMAP(px, l, r); break;
+ case 2: k = CMAP(px, r, l); break;
+ case 3: k = CMAP(py, t, b); break;
+ case 4: k = CMAP(py, b, t); break;
+ case 5: k = imin(CMAP(px, l, cx), CMAP(px, r, cx)); break;
+ case 6: k = imin(CMAP(py, t, cy), CMAP(py, b, cy)); break;
+ default: k = 256;
+ }
+#undef CMAP
+ const uint8_t *n = rec_node(e->rec, 0);
+ extern int g_anchor_x, g_anchor_y, g_anchor_lerp;
+ extern uint8_t g_anchor_ignore;
+ g_anchor_x = g_level.rooms[e->room].x + S16(n) - 160, g_anchor_y = g_level.rooms[e->room].y + S16(n + 2) - 90;
+ g_anchor_lerp = iclamp(rec_val(e->rec, 0) * k / 256, 0, 256);
+ g_anchor_ignore = (uint8_t)(rec_flags(e->rec) & 3); /* 1 x only (ignore y), 2 y only (ignore x) */
+}
+static void trig_leave(Ent *e) {
+ if (e->type - 64 != TT_cameraTargetTrigger) return;
+ for (int i = 0; i < g_nents; i++)
+ if (&g_ents[i] != e && g_ents[i].type == 64 + TT_cameraTargetTrigger && (g_ents[i].sub & 1)) return;
+ extern int g_anchor_lerp;
+ g_anchor_lerp = 0;
+}
+
+/* ---------------------------------------------------------------- spawning a room */
+static Ent *spawn(const uint8_t *r, int slot, bool trigger) {
+ Room *rm = &g_level.rooms[slot];
+ int type = r[0], x = rm->x + REC_X(r), y = rm->y + REC_Y(r), w = REC_W(r), h = REC_H(r);
+ if (trigger) {
+ Ent *e = ent_new(64 + type, x, y);
+ if (!e) return NULL;
+ e->rec = r, e->room = (uint8_t)slot;
+ e->flags = EF_ACTIVE | EF_TRIGGER | EF_COLLIDABLE;
+ box(e, w, h, 0, 0);
+ if (type == TT_changeRespawnTrigger) { /* the spawn point nearest its node (or middle) */
+ const uint8_t *n = REC_NODES(r) ? rec_node(r, 0) : NULL;
+ int tx = n ? rm->x + S16(n) : x + w / 2, ty = n ? rm->y + S16(n + 2) : y + h / 2;
+ const uint8_t *p = rm->ents;
+ int best = -1;
+ int32_t bd = 0;
+ p++;
+ for (int i = 0; i < rm->ents[0]; i++, p += p[1]) {
+ if (p[0] != ET_player) continue;
+ int sx = rm->x + S16(p + 2), sy = rm->y + S16(p + 4);
+ int32_t d = (int32_t)(sx - tx) * (sx - tx) + (int32_t)(sy - ty) * (sy - ty);
+ if (best < 0 || d < bd) best = i, bd = d, e->t0 = sx, e->t1 = sy;
+ }
+ }
+ return e;
+ }
+ Ent *e = ent_new(type, x, y);
+ if (!e) return NULL;
+ e->rec = r, e->room = (uint8_t)slot;
+ int fl = rec_flags(r);
+ switch (type) {
+ case ET_player: ent_free(e); return NULL;
+ case ET_spikesUp: box(e, w, 3, 0, -3), e->depth = -1, e->flags |= EF_PCOLLIDE; break;
+ case ET_spikesDown: box(e, w, 3, 0, 0), e->depth = -1, e->flags |= EF_PCOLLIDE; break;
+ case ET_spikesLeft: box(e, 3, h, -3, 0), e->depth = -1, e->flags |= EF_PCOLLIDE; break;
+ case ET_spikesRight: box(e, 3, h, 0, 0), e->depth = -1, e->flags |= EF_PCOLLIDE; break;
+ case ET_jumpThru: box(e, w, 5, 0, 0), e->depth = -60, e->flags |= EF_JUMPTHRU | EF_SAFE; break;
+ case ET_strawberry:
+ case ET_goldenBerry:
+ box(e, 14, 14, -7, -7), e->depth = -100, e->flags |= EF_PCOLLIDE;
+ if (type == ET_goldenBerry) e->sub |= BERRY_GOLD;
+ else if (save_has_berry(g_level.chapter, rec_val(r, 2))) e->sub |= BERRY_GHOST;
+ if (fl & 1) e->sub |= BERRY_WINGED;
+ e->a0 = (int16_t)berry_anim(e, (fl & 1) ? 2 : 0);
+ break;
+ case ET_refill: box(e, 16, 16, -8, -8), e->depth = -100, e->flags |= EF_PCOLLIDE; break;
+ case ET_spring: box(e, 16, 6, -8, -6), e->depth = -8501, e->flags |= EF_PCOLLIDE; break;
+ case ET_wallSpringLeft: box(e, 6, 16, 0, -8), e->depth = -8501, e->flags |= EF_PCOLLIDE; break;
+ case ET_wallSpringRight: box(e, 6, 16, -6, -8), e->depth = -8501, e->flags |= EF_PCOLLIDE; break;
+ case ET_crumbleBlock: box(e, w, 8, 0, 0), e->flags |= EF_SOLID; break;
+ case ET_fallingBlock: box(e, w, h, 0, 0), e->flags |= EF_SOLID, e->depth = (fl & 2) ? 5000 : 0; break;
+ case ET_dashBlock: box(e, w, h, 0, 0), e->flags |= EF_SOLID, e->depth = -12999; break;
+ case ET_fakeWall: box(e, w, h, 0, 0), e->flags &= ~EF_COLLIDABLE, e->depth = -13000, e->t0 = 256; break;
+ case ET_coverupWall: box(e, w, h, 0, 0), e->flags &= ~EF_COLLIDABLE, e->depth = -13000, e->t0 = 256; break;
+ case ET_zipMover: box(e, w, h, 0, 0), e->flags |= EF_SOLID, e->depth = -9999, e->a0 = 1; break;
+ default:
+ e->flags = EF_ACTIVE | EF_VISIBLE; /* scenery not drawn yet */
+ e->type = 100;
+ break;
+ }
+ return e;
+}
+/* StaticMover: spikes and springs stuck to a solid move with it */
+static void attach_movers(int slot) {
+ for (int i = 0; i < g_nents; i++) {
+ Ent *m = &g_ents[i];
+ if (m->room != slot || !((m->type >= ET_spikesUp && m->type <= ET_spikesRight) || m->type == ET_spring ||
+ m->type == ET_wallSpringLeft || m->type == ET_wallSpringRight))
+ continue;
+ int dx = 0, dy = 0;
+ switch (m->type) {
+ case ET_spikesUp: case ET_spring: dy = 1; break;
+ case ET_spikesDown: dy = -1; break;
+ case ET_spikesLeft: case ET_wallSpringRight: dx = 1; break;
+ case ET_spikesRight: case ET_wallSpringLeft: dx = -1; break;
+ }
+ for (int j = 0; j < g_nents; j++) {
+ Ent *s = &g_ents[j];
+ if (s->room != slot || !(s->flags & EF_SOLID) || s->type == 255) continue;
+ if (ent_collide_at(m, m->x + dx, m->y + dy, s) && !ent_collide_at(m, m->x, m->y, s)) {
+ m->ptr = s;
+ if (m->type >= ET_spring) m->depth = (int16_t)(s->depth + 1);
+ break;
+ }
+ }
+ }
+}
+void ents_spawn_room(int slot) {
+ Room *rm = &g_level.rooms[slot];
+ const uint8_t *p = rm->ents;
+ int n = *p++;
+ for (int i = 0; i < n; i++, p += p[1]) spawn(p, slot, false);
+ p = rm->trigs;
+ n = *p++;
+ for (int i = 0; i < n; i++, p += p[1]) spawn(p, slot, true);
+ attach_movers(slot);
+ (void)trig_ox, (void)trig_oy;
+}
+void ents_remove_room(int slot) {
+ for (int i = 0; i < g_nents; i++) {
+ Ent *e = &g_ents[i];
+ if (e->type != 255 && e->room == slot && fol_index(e) < 0) ent_free(e);
+ }
+}
+void ents_clear(void) {
+ g_nents = 0;
+ nfol = npast = 0;
+}
+
+/* ---------------------------------------------------------------- the frame */
+void ents_update(void) {
+ for (int i = 0; i < g_nents; i++) {
+ Ent *e = &g_ents[i];
+ if (e->type == 255 || !(e->flags & EF_ACTIVE)) continue;
+ switch (e->type) {
+ case ET_player: player_update(); break;
+ case ET_strawberry: case ET_goldenBerry: berry_update(e); break;
+ case ET_spring: case ET_wallSpringLeft: case ET_wallSpringRight: spring_update(e); break;
+ case ET_refill: refill_update(e); break;
+ case ET_crumbleBlock: crumble_update(e); break;
+ case ET_fallingBlock: falling_update(e); break;
+ case ET_fakeWall: case ET_coverupWall: fakewall_update(e); break;
+ case ET_zipMover: zip_update(e); break;
+ case 200: points_update(e); break;
+ }
+ }
+ leader_update();
+ extern void player_after_update(void);
+ player_after_update();
+}
+/* PlayerCollider and triggers: what Madeline (her hurt box) touches */
+void ents_player_touch(Ent *p) {
+ for (int i = 0; i < g_nents && !g_player.dead; i++) {
+ Ent *e = &g_ents[i];
+ if (e->type == 255 || !(e->flags & EF_COLLIDABLE)) continue;
+ bool hit = ent_collide_at(p, p->x, p->y, e);
+ if (e->flags & EF_TRIGGER) {
+ if (hit) {
+ if (!(e->sub & 1)) e->sub |= 1, trig_enter(e);
+ trig_stay(e);
+ } else if (e->sub & 1)
+ e->sub &= ~1, trig_leave(e);
+ continue;
+ }
+ if (!hit || !(e->flags & EF_PCOLLIDE)) continue;
+ switch (e->type) {
+ case ET_spikesUp: case ET_spikesDown: case ET_spikesLeft: case ET_spikesRight: spikes_touch(e); break;
+ case ET_strawberry: case ET_goldenBerry: berry_touch(e); break;
+ case ET_spring: case ET_wallSpringLeft: case ET_wallSpringRight: spring_touch(e); break;
+ case ET_refill: refill_touch(e); break;
+ }
+ }
+}
+void ents_render(int dmax, int dmin) {
+ /* back to front: higher depths first, then the order they came in */
+ int last = 0x7FFFFF;
+ for (;;) {
+ int d = -0x7FFFFF;
+ for (int i = 0; i < g_nents; i++) {
+ Ent *e = &g_ents[i];
+ if (e->type != 255 && (e->flags & EF_VISIBLE) && e->depth < last && e->depth > d && e->depth <= dmax && e->depth > dmin)
+ d = e->depth;
+ }
+ if (d == -0x7FFFFF) return;
+ for (int i = 0; i < g_nents; i++) {
+ Ent *e = &g_ents[i];
+ if (e->type == 255 || !(e->flags & EF_VISIBLE) || e->depth != d) continue;
+ switch (e->type) {
+ case ET_player: player_render(); break;
+ case ET_spikesUp: case ET_spikesDown: case ET_spikesLeft: case ET_spikesRight: spikes_render(e); break;
+ case ET_jumpThru: jumpthru_render(e); break;
+ case ET_strawberry: case ET_goldenBerry: berry_render(e); break;
+ case ET_spring: case ET_wallSpringLeft: case ET_wallSpringRight: spring_render(e); break;
+ case ET_refill: refill_render(e); break;
+ case ET_crumbleBlock: crumble_render(e); break;
+ case ET_fallingBlock: case ET_dashBlock: block_render(e); break;
+ case ET_fakeWall: case ET_coverupWall: fakewall_render(e); break;
+ case ET_zipMover: zip_render(e); break;
+ case 200: {
+ const uint8_t *s = sprite(anim_sprite(e));
+ draw_sprite(s, e->x - s[0] / 2, e->y - s[1] / 2, 0);
+ break;
+ }
+ }
+ }
+ last = d;
+ }
+}
diff --git a/games/celeste/ce/src/fconst.h b/games/celeste/ce/src/fconst.h
new file mode 100644
index 00000000..dec81cf0
--- /dev/null
+++ b/games/celeste/ce/src/fconst.h
@@ -0,0 +1,107 @@
+/* Generated by tools/fconst.py: Celeste's movement constants as single-precision bits. */
+#ifndef FCONST_H
+#define FCONST_H
+#define F_0 0x00000000ul /* 0.0 */
+#define F_1 0x3F800000ul /* 1.0 */
+#define F_HALF 0x3F000000ul /* 0.5 */
+#define F_2 0x40000000ul /* 2.0 */
+#define F_3 0x40400000ul /* 3.0 */
+#define F_4 0x40800000ul /* 4.0 */
+#define F_5 0x40A00000ul /* 5.0 */
+#define F_6 0x40C00000ul /* 6.0 */
+#define F_8 0x41000000ul /* 8.0 */
+#define F_DT 0x3C88889Aul /* 0.016666699200868607 */
+#define F_MaxFall 0x43200000ul /* 160.0 */
+#define F_Gravity 0x44610000ul /* 900.0 */
+#define F_HalfGravThreshold 0x42200000ul /* 40.0 */
+#define F_FastMaxFall 0x43700000ul /* 240.0 */
+#define F_FastMaxAccel 0x43960000ul /* 300.0 */
+#define F_MaxRun 0x42B40000ul /* 90.0 */
+#define F_RunAccel 0x447A0000ul /* 1000.0 */
+#define F_RunReduce 0x43C80000ul /* 400.0 */
+#define F_AirMult 0x3F266666ul /* 0.6499999761581421 */
+#define F_DuckFriction 0x43FA0000ul /* 500.0 */
+#define F_DuckCorrectSlide 0x42480000ul /* 50.0 */
+#define F_DodgeSlideSpeedMult 0x3F99999Aul /* 1.2000000476837158 */
+#define F_DuckSuperJumpXMult 0x3FA00000ul /* 1.25 */
+#define F_DuckSuperJumpYMult 0x3F000000ul /* 0.5 */
+#define F_JumpGraceTime 0x3DCCCCCDul /* 0.10000000149011612 */
+#define F_JumpSpeed 0xC2D20000ul /* -105.0 */
+#define F_JumpHBoost 0x42200000ul /* 40.0 */
+#define F_VarJumpTime 0x3E4CCCCDul /* 0.20000000298023224 */
+#define F_CeilingVarJumpGrace 0x3D4CCCCDul /* 0.05000000074505806 */
+#define F_WallSpeedRetentionTime 0x3D75C28Ful /* 0.05999999865889549 */
+#define F_WallJumpForceTime 0x3E23D70Aul /* 0.1599999964237213 */
+#define F_WallSlideStartMax 0x41A00000ul /* 20.0 */
+#define F_WallSlideTime 0x3F99999Aul /* 1.2000000476837158 */
+#define F_BounceVarJumpTime 0x3E4CCCCDul /* 0.20000000298023224 */
+#define F_BounceSpeed 0xC30C0000ul /* -140.0 */
+#define F_BounceAutoJumpTime 0x3DCCCCCDul /* 0.10000000149011612 */
+#define F_SuperBounceVarJumpTime 0x3E4CCCCDul /* 0.20000000298023224 */
+#define F_SuperBounceSpeed 0xC3390000ul /* -185.0 */
+#define F_SuperJumpH 0x43820000ul /* 260.0 */
+#define F_SuperWallJumpSpeed 0xC3200000ul /* -160.0 */
+#define F_SuperWallJumpVarTime 0x3E800000ul /* 0.25 */
+#define F_DashSpeed 0x43700000ul /* 240.0 */
+#define F_EndDashSpeed 0x43200000ul /* 160.0 */
+#define F_EndDashUpMult 0x3F400000ul /* 0.75 */
+#define F_DashTime 0x3E19999Aul /* 0.15000000596046448 */
+#define F_DashCooldown 0x3E4CCCCDul /* 0.20000000298023224 */
+#define F_DashRefillCooldown 0x3DCCCCCDul /* 0.10000000149011612 */
+#define F_DashAttackTime 0x3E99999Aul /* 0.30000001192092896 */
+#define F_ClimbMaxStamina 0x42DC0000ul /* 110.0 */
+#define F_ClimbUpCost 0x4235D174ul /* 45.45454406738281 */
+#define F_ClimbStillCost 0x41200000ul /* 10.0 */
+#define F_ClimbJumpCost 0x41DC0000ul /* 27.5 */
+#define F_ClimbNoMoveTime 0x3DCCCCCDul /* 0.10000000149011612 */
+#define F_ClimbTiredThreshold 0x41A00000ul /* 20.0 */
+#define F_ClimbUpSpeed 0xC2340000ul /* -45.0 */
+#define F_ClimbDownSpeed 0x42A00000ul /* 80.0 */
+#define F_ClimbSlipSpeed 0x41F00000ul /* 30.0 */
+#define F_ClimbAccel 0x44610000ul /* 900.0 */
+#define F_ClimbGrabYMult 0x3E4CCCCDul /* 0.20000000298023224 */
+#define F_ClimbHopY 0xC2F00000ul /* -120.0 */
+#define F_ClimbHopX 0x42C80000ul /* 100.0 */
+#define F_ClimbHopForceTime 0x3E4CCCCDul /* 0.20000000298023224 */
+#define F_ClimbJumpBoostTime 0x3E4CCCCDul /* 0.20000000298023224 */
+#define F_LiftYCap 0xC3020000ul /* -130.0 */
+#define F_LiftXCap 0x437A0000ul /* 250.0 */
+#define F_JumpThruAssistSpeed 0xC2200000ul /* -40.0 */
+#define F_Diag 0x3F3504F3ul /* 0.7071067690849304 */
+#define F_ReboundSpeedX 0x42F00000ul /* 120.0 */
+#define F_ReboundSpeedY 0xC2F00000ul /* -120.0 */
+#define F_ReboundVarJumpTime 0x3E19999Aul /* 0.15000000596046448 */
+#define F_SideBounceSpeed 0x43700000ul /* 240.0 */
+#define F_SideBounceForce 0x3E99999Aul /* 0.30000001192092896 */
+#define F_TransitionSpeed 0x3F800010ul /* 1.0000019073486328 */
+#define F_IntroRise 0xC0000010ul /* -2.0000038146972656 */
+#define F_IntroJumpSpeed 0xC2C80000ul /* -100.0 */
+#define F_IntroGravity 0x41555571ul /* 13.333359718322754 */
+#define F_WalkIn 0x3F88889Aul /* 1.0666687488555908 */
+#define F_LiftTime 0x3E23D70Aul /* 0.1599999964237213 */
+#define F_StrawbReset 0x40200000ul /* 2.5 */
+#define F_Respawn 0x3F19999Aul /* 0.6000000238418579 */
+#define F_IdleVariant 0x40400000ul /* 3.0 */
+#define F_SixtyHalf 0x3DF5C28Ful /* 0.11999999731779099 */
+#define F_HairFlash 0x3DF5C28Ful /* 0.11999999731779099 */
+#define F_Wall 0x3F19999Aul /* 0.6000000238418579 */
+#define F_Freeze 0x3D4CCCCDul /* 0.05000000074505806 */
+#define F_Shake 0x3E99999Aul /* 0.30000001192092896 */
+#define F_DashShake 0x3E4CCCCDul /* 0.20000000298023224 */
+#define F_Hop 0x3F000000ul /* 0.5 */
+#define F_Collect 0x3E19999Aul /* 0.15000000596046448 */
+#define F_BerryFollow 0x3E99999Aul /* 0.30000001192092896 */
+#define F_WallJumpHSpeed 0x43020000ul /* 130.0 */
+#define F_SuperWallJumpH 0x432A0000ul /* 170.0 */
+#define F_RunAccelAir 0x44228000ul /* 650.0 */
+#define F_RunReduceAir 0x43820000ul /* 260.0 */
+#define F_WallSlideHalf 0x3F19999Aul /* 0.6000000238418579 */
+#define F_FastHalf 0x43480000ul /* 200.0 */
+#define F_DashDiag 0x4329B4A4ul /* 169.70562744140625 */
+#define F_EndDashDiag 0x42E24630ul /* 113.1370849609375 */
+#define F_RunAccel40 0x41C80000ul /* 25.0 */
+#define ZIP_OUT_N 30
+#define ZIP_OUT {0x3AB3A15Dul, 0x3BB381DAul, 0x3C49B70Dul, 0x3CB303FBul, 0x3D0B9180ul, 0x3D487929ul, 0x3D88071Eul, 0x3DB10F3Ful, 0x3DDF382Eul, 0x3E0930C3ul, 0x3E2533B1ul, 0x3E43913Aul, 0x3E64340Ful, 0x3E8382A3ul, 0x3E95F63Cul, 0x3EA967DEul, 0x3EBDC9E4ul, 0x3ED30E01ul, 0x3EE9254Bul, 0x3F000020ul, 0x3F0BC769ul, 0x3F17E041ul, 0x3F244226ul, 0x3F30E46Eul, 0x3F3DBE37ul, 0x3F4AC67Cul, 0x3F57F41Cul, 0x3F653DD6ul, 0x3F729A54ul, 0x3F800000ul}
+#define ZIP_BACK_N 120
+#define ZIP_BACK {0x38B3AB36ul, 0x39B3A93Eul, 0x3A4A1AB6ul, 0x3AB3A15Dul, 0x3B0C5173ul, 0x3B4A06C2ul, 0x3B8976F4ul, 0x3BB381D8ul, 0x3BE32237ul, 0x3C0C2AFCul, 0x3C298D62ul, 0x3C49B708ul, 0x3C6CA681ul, 0x3C892D22ul, 0x3C9D6856ul, 0x3CB303F9ul, 0x3CC9FF19ul, 0x3CE258B3ul, 0x3CFC0FB7ul, 0x3D0B9182ul, 0x3D19C8B2ul, 0x3D28ACD1ul, 0x3D383D33ul, 0x3D48792Dul, 0x3D596008ul, 0x3D6AF102ul, 0x3D7D2B5Cul, 0x3D880723ul, 0x3D91CC75ul, 0x3D9BE538ul, 0x3DA650FAul, 0x3DB10F47ul, 0x3DBC1FA4ul, 0x3DC78197ul, 0x3DD334A0ul, 0x3DDF3839ul, 0x3DEB8BDFul, 0x3DF82F03ul, 0x3E02908Eul, 0x3E0930CBul, 0x3E0FF7EFul, 0x3E16E5ADul, 0x3E1DF9B5ul, 0x3E2533BBul, 0x3E2C936Eul, 0x3E34187Aul, 0x3E3BC28Aul, 0x3E439148ul, 0x3E4B845Bul, 0x3E539B6Dul, 0x3E5BD623ul, 0x3E64341Eul, 0x3E6CB502ul, 0x3E75586Cul, 0x3E7E1E00ul, 0x3E8382ADul, 0x3E88070Aul, 0x3E8C9BE5ul, 0x3E91410Bul, 0x3E95F646ul, 0x3E9ABB62ul, 0x3E9F902Aul, 0x3EA47469ul, 0x3EA967E5ul, 0x3EAE6A69ul, 0x3EB37BBBul, 0x3EB89BA3ul, 0x3EBDC9E7ul, 0x3EC3064Eul, 0x3EC8509Cul, 0x3ECDA895ul, 0x3ED30DFFul, 0x3ED8809Dul, 0x3EDE0030ul, 0x3EE38C7Cul, 0x3EE92542ul, 0x3EEECA44ul, 0x3EF47B43ul, 0x3EFA37FBul, 0x3F000019ul, 0x3F02E9D0ul, 0x3F05D905ul, 0x3F08CD94ul, 0x3F0BC760ul, 0x3F0EC643ul, 0x3F11CA1Ful, 0x3F14D2CFul, 0x3F17E034ul, 0x3F1AF228ul, 0x3F1E088Dul, 0x3F21233Dul, 0x3F244217ul, 0x3F2764F6ul, 0x3F2A8BBAul, 0x3F2DB63Bul, 0x3F30E45Bul, 0x3F3415F0ul, 0x3F374ADCul, 0x3F3A82F7ul, 0x3F3DBE20ul, 0x3F40FC31ul, 0x3F443D03ul, 0x3F478077ul, 0x3F4AC663ul, 0x3F4E0EA6ul, 0x3F515919ul, 0x3F54A599ul, 0x3F57F3FFul, 0x3F5B4427ul, 0x3F5E95EBul, 0x3F61E927ul, 0x3F653DB4ul, 0x3F68936Ful, 0x3F6BEA2Ful, 0x3F6F41D2ul, 0x3F729A30ul, 0x3F75F326ul, 0x3F794C8Bul, 0x3F7CA63Dul, 0x3F800000ul}
+#endif
diff --git a/games/celeste/ce/src/game.c b/games/celeste/ce/src/game.c
new file mode 100644
index 00000000..a1b997f3
--- /dev/null
+++ b/games/celeste/ce/src/game.c
@@ -0,0 +1,236 @@
+/* The game: the frame loop's steps, the menus, the HUD, the save. */
+#include "ce.h"
+
+bool g_running = true;
+int g_berries_now;
+int g_anchor_x, g_anchor_y, g_anchor_lerp;
+uint8_t g_anchor_ignore;
+
+enum { SC_TITLE, SC_CHAPTERS, SC_PLAY, SC_PAUSE, SC_COMPLETE };
+static int screen = SC_TITLE, sel, chapter = 1;
+static int screen_t;
+static bool full_redraw = true;
+
+/* ---------------------------------------------------------------- the save (appvar CELSAVE) */
+#define NCHAPTERS 10
+typedef struct {
+ char magic[4];
+ uint8_t version, size_lo, size_hi, pad;
+ struct {
+ uint8_t berries[8]; /* a bit per strawberry (its index in the chapter) */
+ uint32_t deaths, best_time;
+ uint8_t flags; /* 1 completed, 2 golden, 4 cassette, 8 heart */
+ uint8_t checkpoints;
+ } ch[NCHAPTERS];
+ uint32_t total_deaths;
+} Save;
+static Save save;
+static void save_write(void) { plat_save("CELSAVE", &save, sizeof save); }
+static void save_read(void) {
+ if (!plat_load("CELSAVE", &save, sizeof save) || memcmp(save.magic, "CELS", 4) || save.version != 1) {
+ memset(&save, 0, sizeof save);
+ memcpy(save.magic, "CELS", 4);
+ save.version = 1;
+ }
+}
+bool save_has_berry(int ch, int index) { return index >= 0 && index < 64 && (save.ch[ch].berries[index >> 3] >> (index & 7) & 1); }
+void save_add_berry(int ch, int index, bool golden) {
+ if (golden) save.ch[ch].flags |= 2;
+ else if (index >= 0 && index < 64) save.ch[ch].berries[index >> 3] |= (uint8_t)(1 << (index & 7));
+}
+static int berries_of(int ch) {
+ int n = 0;
+ for (int i = 0; i < 64; i++) n += save_has_berry(ch, i);
+ return n;
+}
+void save_add_death(void) {
+ save.ch[g_level.chapter].deaths++;
+ save.total_deaths++;
+}
+
+/* ---------------------------------------------------------------- text helpers */
+static void text_center(const char *s, int y, uint8_t c) { draw_text(s, 160 - text_width(s) / 2, y, c); }
+static void num(char *out, uint32_t v) {
+ char t[12];
+ int n = 0;
+ do t[n++] = (char)('0' + v % 10), v /= 10;
+ while (v);
+ while (n) *out++ = t[--n];
+ *out = 0;
+}
+
+/* ---------------------------------------------------------------- playing */
+static void play_chapter(int ch) {
+ if (!chapter_open(ch)) return;
+ chapter = ch;
+ palette_apply(256);
+ g_berries_now = 0;
+ level_load(ch, -1, INTRO_JUMP);
+ screen = SC_PLAY;
+ full_redraw = true;
+}
+
+void game_chapter_end(void) {
+ if (g_level.completed) return;
+ extern void ents_collect_berries(void);
+ ents_collect_berries();
+ g_level.completed = true;
+ save.ch[chapter].flags |= 1;
+ if (!save.ch[chapter].best_time || g_level.time < save.ch[chapter].best_time) save.ch[chapter].best_time = g_level.time;
+ save_write();
+ screen_t = 0;
+}
+
+static void hud_draw(void) {
+ /* the strawberry count, top left, while some are being collected (Celeste's TotalStrawberriesDisplay) */
+ static int shown, timer;
+ if (g_berries_now != shown) shown = g_berries_now, timer = 180;
+ if (timer > 0) {
+ timer--;
+ char buf[16];
+ buf[0] = 'x';
+ num(buf + 1, (uint32_t)(berries_of(chapter)));
+ draw_sprite(sprite(S_collectables_strawberry_normal00), g_camx + 2, g_camy + 2, 0);
+ draw_text(buf, 22, VIEW_Y + 8, WHITE);
+ }
+}
+
+/* ---------------------------------------------------------------- the frame */
+void game_frame(void) {
+ input_update(plat_keys());
+ if (g_in.keys & K_QUIT) {
+ save_write();
+ g_running = false;
+ return;
+ }
+ screen_t++;
+ switch (screen) {
+ case SC_TITLE:
+ if (btn_pressed(&g_in.ok) || btn_pressed(&g_in.jump)) screen = SC_CHAPTERS, sel = 0, full_redraw = true;
+ break;
+ case SC_CHAPTERS:
+ if (btn_pressed(&g_in.back)) screen = SC_TITLE, full_redraw = true;
+ else if (btn_pressed(&g_in.ok) || btn_pressed(&g_in.jump)) play_chapter(1);
+ break;
+ case SC_PLAY:
+ if (g_level.completed) {
+ if (++screen_t > 90 && (btn_pressed(&g_in.ok) || btn_pressed(&g_in.jump))) screen = SC_CHAPTERS, full_redraw = true;
+ break;
+ }
+ if (btn_pressed(&g_in.pause) && !g_player.dead && !g_level.transitioning) {
+ btn_consume(&g_in.pause);
+ screen = SC_PAUSE, sel = 0;
+ break;
+ }
+ level_update();
+ break;
+ case SC_PAUSE:
+ if (btn_pressed(&g_in.back) || btn_pressed(&g_in.pause)) screen = SC_PLAY;
+ else if (g_in.move_y && !(g_in.prev & (K_UP | K_DOWN))) sel = (sel + g_in.move_y + 3) % 3;
+ else if (btn_pressed(&g_in.ok) || btn_pressed(&g_in.jump)) {
+ if (sel == 0) screen = SC_PLAY;
+ else if (sel == 1) {
+ screen = SC_PLAY;
+ if (!g_player.dead) player_die(0, 0);
+ } else {
+ save_write();
+ screen = SC_CHAPTERS, full_redraw = true;
+ }
+ }
+ break;
+ }
+}
+
+static void wipe_draw(void) { /* AngledWipe: black bands sliding across, 24 frames either way */
+ int w = g_level.wipe;
+ if (!w) return;
+ int t = w > 0 ? w : 24 + w; /* 0..24: how closed */
+ if (w < 0) t = -w; /* opening: from closed to open */
+ for (int y = 0; y < VIEW_H; y += 4) {
+ int x = (t * 380 / 24) - 30 - (y * 30 / VIEW_H) + ((y / 4) & 1) * 6;
+ if (w > 0) draw_rect_screen(320 - x, y, x + 1, 4, 0);
+ else draw_rect_screen(0, y, x, 4, 0);
+ }
+}
+
+void game_draw(void) {
+ if (full_redraw) draw_clear_screen(0), full_redraw = false;
+ switch (screen) {
+ case SC_TITLE:
+ draw_clear_screen(0);
+ text_center("CELESTE", 90, WHITE);
+ text_center("2nd: start", 150, WHITE);
+ break;
+ case SC_CHAPTERS: {
+ draw_clear_screen(0);
+ text_center("Chapter 1", 80, WHITE);
+ text_center("Forsaken City", 95, WHITE);
+ char buf[24];
+ num(buf, (uint32_t)berries_of(1));
+ strcat(buf, " / 20 strawberries");
+ text_center(buf, 120, WHITE);
+ text_center("2nd: climb clear: back", 200, WHITE);
+ break;
+ }
+ case SC_PLAY:
+ case SC_PAUSE:
+ level_render();
+ hud_draw();
+ wipe_draw();
+ if (screen == SC_PAUSE) {
+ static const char *items[] = {"Resume", "Retry", "Save & Quit"};
+ draw_rect_screen(100, 50, 120, 80, 0);
+ for (int i = 0; i < 3; i++) draw_text(items[i], 120, VIEW_Y + 62 + i * 20, i == sel ? WHITE : 60);
+ }
+ if (g_level.completed) {
+ draw_rect_screen(60, 50, 200, 80, 0);
+ text_center("Chapter complete", VIEW_Y + 62, WHITE);
+ char buf[32];
+ num(buf, (uint32_t)berries_of(chapter));
+ strcat(buf, " strawberries");
+ text_center(buf, VIEW_Y + 82, WHITE);
+ }
+ break;
+ }
+}
+
+/* straight into a room, Madeline at (x, y) in it (tests) */
+void game_start_room(int ch, const char *room, int x, int y) {
+ if (!chapter_open(ch)) return;
+ chapter = ch;
+ palette_apply(256);
+ level_load(ch, level_room_find(room), INTRO_NONE);
+ g_level.wipe = 0;
+ if (x || y) {
+ g_player.e->x = g_level.room->x + x, g_player.e->y = g_level.room->y + y;
+ g_level.camx = player_cam_targetx() * 256, g_level.camy = player_cam_targety() * 256;
+ }
+ screen = SC_PLAY;
+ full_redraw = true;
+}
+
+void game_init(void) {
+ save_read();
+ palette_apply(256);
+#ifndef __TICE__
+ {
+ extern char *getenv(const char *);
+ extern int atoi(const char *);
+ const char *s = getenv("CEL_START"); /* chapter,room[,x,y] */
+ if (s) {
+ char room[32] = "";
+ int ch = atoi(s), x = 0, y = 0;
+ const char *p = s;
+ while (*p && *p != ',') p++;
+ if (*p) p++;
+ int k = 0;
+ while (*p && *p != ',' && k < 31) room[k++] = *p++;
+ room[k] = 0;
+ if (*p) x = atoi(++p);
+ while (*p && *p != ',') p++;
+ if (*p) y = atoi(++p);
+ game_start_room(ch, room, x, y);
+ }
+ }
+#endif
+}
diff --git a/games/celeste/ce/src/gen.h b/games/celeste/ce/src/gen.h
index fbea74dd..64608c28 100644
--- a/games/celeste/ce/src/gen.h
+++ b/games/celeste/ce/src/gen.h
@@ -506,6 +506,10 @@
#define BANK_ORIGINS {{1, 16, 32}, {2, 128, 128}, {2, 128, 128}, {2, 128, 128}, {2, 128, 128}}
#define NBANKS 5
#define COMMON 96
+#define HAIR 1
+#define HAIR_DARK 2
+#define WHITE 3
+#define BLACK 4
#define ET_player 0
#define ET_spikesUp 1
#define ET_spikesDown 2
diff --git a/games/celeste/ce/src/level.c b/games/celeste/ce/src/level.c
index 238a3a22..6ebf889b 100644
--- a/games/celeste/ce/src/level.c
+++ b/games/celeste/ce/src/level.c
@@ -120,7 +120,7 @@ void level_dir_shake(int dx, int dy, int frames) {
g_level.shake_timer = imax(g_level.shake_timer, frames);
g_level.shake_dirx = dx, g_level.shake_diry = dy;
}
-void level_freeze(int frames) { g_level.freeze = imax(g_level.freeze, frames); }
+void level_freeze(int t) { g_level.freeze = imax(g_level.freeze, t); }
/* ---------------------------------------------------------------- loading */
static int spawn_near(int x, int y) { /* the room's spawn nearest (x, y), as an index into its records */
@@ -148,7 +148,7 @@ static void load_into_current(int index, int intro) {
ents_spawn_room(0);
player_spawn(g_level.respawn_x, g_level.respawn_y, intro);
g_level.cam_offx = rm->camx * 48, g_level.cam_offy = rm->camy * 32;
- g_level.camx = player_cam_targetx() << 8, g_level.camy = player_cam_targety() << 8;
+ g_level.camx = player_cam_targetx() * 256, g_level.camy = player_cam_targety() * 256;
g_level.transitioning = false;
g_level.wipe = -24;
}
@@ -193,7 +193,7 @@ static void transition_to(int index, int dx, int dy) {
int wx = pe->x, wy = pe->y;
pe->x = tx, pe->y = ty;
g_level.cam_offx = rm->camx * 48, g_level.cam_offy = rm->camy * 32;
- g_level.tr_tox = player_cam_targetx() << 8, g_level.tr_toy = player_cam_targety() << 8;
+ g_level.tr_tox = player_cam_targetx() * 256, g_level.tr_toy = player_cam_targety() * 256;
pe->x = wx, pe->y = wy;
}
@@ -272,8 +272,8 @@ static void enforce_bounds(void) {
/* ---------------------------------------------------------------- the frame */
void level_update(void) {
- if (g_level.freeze > 0) {
- g_level.freeze--;
+ if (g_level.freeze > 0) { /* Engine.FreezeTimer */
+ g_level.freeze -= DTU;
return;
}
g_level.frame++;
@@ -298,7 +298,8 @@ void level_update(void) {
transition_update();
} else {
ents_update();
- if (!g_player.dead && g_player.state != ST_INTRORESPAWN && !g_level.transitioning) enforce_bounds();
+ extern bool player_in_control(void);
+ if (!g_player.dead && player_in_control() && !g_level.transitioning) enforce_bounds();
}
if (pending_room >= 0) {
int r = pending_room;
diff --git a/games/celeste/ce/src/main.c b/games/celeste/ce/src/main.c
new file mode 100644
index 00000000..c74e5542
--- /dev/null
+++ b/games/celeste/ce/src/main.c
@@ -0,0 +1,48 @@
+/* Celeste for the TI-84 Plus CE.
+ *
+ * Inspired by Celeste, not affiliated with Maddy Makes Games.
+ * Made by Mason Chen as part of NumPlay. */
+#include "ce.h"
+
+void game_init(void);
+uint32_t g_perf_updates, g_perf_frames; /* (read by the tests and the emulator) */
+
+int main(void) {
+ plat_init();
+ if (!data_open()) {
+ draw_clear_screen(0);
+ draw_text("Celeste needs its appvars:", 20, 100, 255);
+ draw_text("send CELG*.8xv and CEL*.8xv too.", 20, 112, 255);
+ plat_frame();
+ while (!(plat_keys() & (K_QUIT | K_BACK | K_OK))) plat_wait(300);
+ plat_exit();
+ return 0;
+ }
+ game_init();
+ /* 60 updates a second (546.13 ticks of the 32768 Hz clock each), drawn when caught up, 3 at most a frame */
+ unsigned last = plat_ticks();
+ int acc = 0;
+ while (g_running) {
+ unsigned now = plat_ticks();
+ acc += (int)((now - last) & 0xFFFF) * 60;
+ last = now;
+ if (acc > 32768 * 6) acc = 32768 * 6; /* far behind: no rush */
+ int n = 0;
+ while (acc >= 32768 && n < 3 && g_running) {
+ game_frame();
+ g_perf_updates++;
+ acc -= 32768;
+ n++;
+ }
+ if (!g_running) break;
+ if (n) {
+ plat_begin_draw();
+ game_draw();
+ plat_frame();
+ g_perf_frames++;
+ } else
+ plat_wait((unsigned)(32768 - acc) / 60);
+ }
+ plat_exit();
+ return 0;
+}
diff --git a/games/celeste/ce/src/plat_ce.c b/games/celeste/ce/src/plat_ce.c
new file mode 100644
index 00000000..1adb66a9
--- /dev/null
+++ b/games/celeste/ce/src/plat_ce.c
@@ -0,0 +1,82 @@
+/* The TI-84 Plus CE: the LCD at 8 bits a pixel, two buffers (graphx), the keypad, the 32768 Hz timer, appvars.
+ * Keys as NumPlay has them on the CE (platform/ti84ce/README.md): 2nd OK (jump), clear Back (dash), math toolbox
+ * (grab), del backspace (pause), enter EXE, mode Home (save and quit). */
+#include "ce.h"
+#include <compression.h>
+#include <fileioc.h>
+#include <graphx.h>
+#include <keypadc.h>
+#include <sys/timers.h>
+#include <ti/getcsc.h>
+
+uint8_t *g_fb;
+
+void plat_init(void) {
+ gfx_Begin();
+ gfx_SetDrawBuffer();
+ memset(gfx_vram, 0, 320 * 240 * 2);
+ g_fb = (uint8_t *)gfx_vbuffer;
+ timer_Disable(2);
+ timer_Set(2, 0);
+ timer_Enable(2, TIMER_32K, TIMER_NOINT, TIMER_UP);
+ kb_DisableOnLatch();
+}
+void plat_exit(void) {
+ uint8_t h = ti_Open("CELSAVE", "r+"); /* the save goes to the archive (it stays in RAM while playing) */
+ if (h) ti_SetArchiveStatus(true, h), ti_Close(h);
+ gfx_End();
+ kb_ClearOnLatch();
+}
+void plat_frame(void) {
+ gfx_SwapDraw();
+ g_fb = (uint8_t *)gfx_vbuffer;
+}
+void plat_begin_draw(void) { gfx_Wait(); }
+void plat_palette(const uint16_t *pal, int first, int n) { memcpy(gfx_palette + first, pal, (size_t)n * 2); }
+
+unsigned plat_keys(void) {
+ kb_Scan();
+ unsigned k = 0;
+ uint8_t g1 = kb_Data[1], g2 = kb_Data[2], g6 = kb_Data[6], g7 = kb_Data[7];
+ if (g7 & kb_Left) k |= K_LEFT;
+ if (g7 & kb_Right) k |= K_RIGHT;
+ if (g7 & kb_Up) k |= K_UP;
+ if (g7 & kb_Down) k |= K_DOWN;
+ if (g1 & kb_2nd) k |= K_JUMP | K_OK;
+ if (g6 & kb_Enter) k |= K_OK;
+ if (g6 & kb_Clear) k |= K_DASH | K_BACK | K_TALK;
+ if (g2 & kb_Math) k |= K_GRAB;
+ if (g1 & kb_Del) k |= K_PAUSE;
+ if (g1 & kb_Mode) k |= K_QUIT;
+ if (kb_On) k |= K_QUIT, kb_ClearOnLatch();
+ return k;
+}
+
+unsigned plat_ticks(void) { return (unsigned)timer_Get(2); }
+void plat_wait(unsigned t) {
+ unsigned start = plat_ticks();
+ while (((plat_ticks() - start) & 0xFFFFFF) < t) {}
+}
+
+const uint8_t *plat_appvar(const char *name) {
+ uint8_t h = ti_Open(name, "r");
+ if (!h) return NULL;
+ const uint8_t *p = ti_GetDataPtr(h);
+ ti_Close(h);
+ return p;
+}
+bool plat_save(const char *name, const void *data, unsigned n) {
+ uint8_t h = ti_Open(name, "w");
+ if (!h) return false;
+ bool ok = ti_Write(data, n, 1, h) == 1;
+ ti_Close(h);
+ return ok;
+}
+bool plat_load(const char *name, void *data, unsigned n) {
+ uint8_t h = ti_Open(name, "r");
+ if (!h) return false;
+ bool ok = ti_Read(data, n, 1, h) == 1;
+ ti_Close(h);
+ return ok;
+}
+void plat_unpack(void *dst, const void *src) { zx0_Decompress(dst, src); }
diff --git a/games/celeste/ce/src/player.c b/games/celeste/ce/src/player.c
index ed868940..0a1a96ef 100644
--- a/games/celeste/ce/src/player.c
+++ b/games/celeste/ce/src/player.c
@@ -1,74 +1,12 @@
-/* Madeline: Player.cs (NoelFB/Celeste) as src/player.c ports it for the NumWorks, in whole numbers (ce.h): the same
- * constants, states and order of operations. */
+/* Madeline: Player.cs (NoelFB/Celeste) as src/player.c ports it for the NumWorks: the same constants, states and
+ * order of operations, and the same single-precision arithmetic (sf.c), so she moves pixel for pixel as there. */
#include "ce.h"
+#include "fconst.h"
Player g_player;
#define P (&g_player)
static Ent *E;
-
-/* Player.cs's constants in the CE's units */
-#define MaxFall SPD(160)
-#define Gravity 900 /* per frame */
-#define HalfGravThreshold SPD(40)
-#define FastMaxFall SPD(240)
-#define FastMaxAccel 300
-#define MaxRun SPD(90)
-#define RunAccel 1000
-#define RunReduce 400
-#define DuckFriction 500
-#define DuckCorrectCheck 4
-#define DuckCorrectSlide SPD(50)
-#define JumpGraceTime TIM(.1f)
-#define JumpSpeed SPD(-105)
-#define JumpHBoost SPD(40)
-#define VarJumpTime TIM(.2f)
-#define CeilingVarJumpGrace TIM(.05f)
-#define UpwardCornerCorrection 4
-#define WallSpeedRetentionTime TIM(.06f)
-#define WallJumpCheckDist 3
-#define WallJumpForceTime TIM(.16f)
-#define WallJumpHSpeed (MaxRun + JumpHBoost)
-#define WallSlideStartMax SPD(20)
-#define WallSlideTime TIM(1.2f)
-#define BounceVarJumpTime TIM(.2f)
-#define BounceSpeed SPD(-140)
-#define BounceAutoJumpTime TIM(.1f)
-#define SuperBounceVarJumpTime TIM(.2f)
-#define SuperBounceSpeed SPD(-185)
-#define SuperJumpH SPD(260)
-#define SuperWallJumpSpeed SPD(-160)
-#define SuperWallJumpVarTime TIM(.25f)
-#define SuperWallJumpH (MaxRun + JumpHBoost * 2)
-#define DashSpeed SPD(240)
-#define EndDashSpeed SPD(160)
-#define DashTime TIM(.15f)
-#define DashCooldown TIM(.2f)
-#define DashRefillCooldown TIM(.1f)
-#define DashHJumpThruNudge 6
-#define DashCornerCorrection 4
-#define DashVFloorSnapDist 3
-#define DashAttackTime TIM(.3f)
-#define STAM 3600 /* stamina: 3600ths */
-#define ClimbMaxStamina (110 * STAM)
-#define ClimbUpCost 2727 /* 100 / 2.2 a second, a frame */
-#define ClimbStillCost 600 /* 10 a second */
-#define ClimbJumpCost (110 * STAM / 4)
-#define ClimbTiredThreshold (20 * STAM)
-#define ClimbCheckDist 2
-#define ClimbUpCheckDist 2
-#define ClimbNoMoveTime TIM(.1f)
-#define ClimbUpSpeed SPD(-45)
-#define ClimbDownSpeed SPD(80)
-#define ClimbSlipSpeed SPD(30)
-#define ClimbAccel 900
-#define ClimbHopY SPD(-120)
-#define ClimbHopX SPD(100)
-#define ClimbHopForceTime TIM(.2f)
-#define ClimbJumpBoostTime TIM(.2f)
-#define LiftYCap SPD(-130)
-#define LiftXCap SPD(250)
-#define JumpThruAssistSpeed SPD(-40)
-#define D 181 /* 0.70710677 * 256 */
+#define DT F_DT
#define NormalHairColor 0xAC3232
#define UsedHairColor 0x44B7FF
@@ -84,13 +22,13 @@ static bool box_is(const int8_t *b) { return E->hw == b[0] && E->hh == b[1] && E
#define Ducking box_is(HB_DUCK)
static void set_ducking(bool d) { set_box(d ? HB_DUCK : HB_NORMAL); }
+extern Ent *solid_hit(int l, int t, int r, int b, const Ent *self);
static bool solid_off(int dx, int dy) { return solid_at(e_left(E) + dx, e_top(E) + dy, e_right(E) + dx, e_bottom(E) + dy, E); }
-static Ent *solid_hit_off(int dx, int dy) {
- extern Ent *solid_hit(int l, int t, int r, int b, const Ent *self);
- return solid_hit(e_left(E) + dx, e_top(E) + dy, e_right(E) + dx, e_bottom(E) + dy, E);
-}
+static Ent *solid_hit_off(int dx, int dy) { return solid_hit(e_left(E) + dx, e_top(E) + dy, e_right(E) + dx, e_bottom(E) + dy, E); }
static bool jumpthru_off(int dx, int dy) { return jumpthru_first_outside(E, E->x + dx, E->y + dy) != NULL; }
static bool point_solid(int x, int y) { return solid_at(x, y, x + 1, y + 1, E); }
+static f32 ftimes(f32 a, int k) { return f_mul(a, f_int(k)); } /* a * k (k a small whole number) */
+static f32 dec(f32 t) { return f_sub(t, DT); }
/* ---------------------------------------------------------------- moving (Actor) */
typedef void (*Collide)(int dx, int dy, Ent *hit);
@@ -123,18 +61,18 @@ static bool move_v_exact(int move, Collide cb) {
}
return false;
}
-static bool move_h(int amount, Collide cb) {
- E->remx += amount;
- int n = round_mu(E->remx, amount);
+static bool move_h(f32 amount, Collide cb) {
+ E->remx = f_add(E->remx, amount);
+ int n = f_round(E->remx);
if (!n) return false;
- E->remx -= n * MU;
+ E->remx = f_sub(E->remx, f_int(n));
return move_h_exact(n, cb);
}
-static bool move_v(int amount, Collide cb) {
- E->remy += amount;
- int n = round_mu(E->remy, amount);
+static bool move_v(f32 amount, Collide cb) {
+ E->remy = f_add(E->remy, amount);
+ int n = f_round(E->remy);
if (!n) return false;
- E->remy -= n * MU;
+ E->remy = f_sub(E->remy, f_int(n));
return move_v_exact(n, cb);
}
static bool on_ground_at(int x, int y) {
@@ -145,29 +83,71 @@ static bool on_ground_at(int x, int y) {
return r;
}
-/* lift speed: what the platform under her gives (kept 0.16 s after she leaves it) */
-static int liftx(void) { return E->liftx || E->lifty ? E->liftx : P->last_liftx; }
-static int lifty(void) { return E->liftx || E->lifty ? E->lifty : P->last_lifty; }
-static void lift_boost(int *x, int *y) {
- int lx = liftx(), ly = lifty();
- if (iabs(lx) > LiftXCap) lx = LiftXCap * isign(lx);
- if (ly > 0) ly = 0;
- else if (ly < LiftYCap) ly = LiftYCap;
+/* the lift speed: what the platform under her gives (kept 0.16 s after she leaves it) */
+static f32 lift_x(void) { return (E->liftx || E->lifty) ? E->liftx : P->last_liftx; }
+static f32 lift_y(void) { return (E->liftx || E->lifty) ? E->lifty : P->last_lifty; }
+static void lift_boost(f32 *x, f32 *y) {
+ f32 lx = lift_x(), ly = lift_y();
+ if (f_lt(F_LiftXCap, f_abs(lx))) lx = (lx & 0x80000000ul) | F_LiftXCap;
+ if (f_pos(ly)) ly = 0;
+ else if (f_lt(ly, F_LiftYCap)) ly = F_LiftYCap;
*x = lx, *y = ly;
}
+/* ---------------------------------------------------------------- what the platforms do to her */
+bool player_climbing(void) { return P->state == ST_CLIMB; }
+bool player_riding(const Ent *s) { /* IsRiding(Solid) */
+ Ent *e = P->e;
+ if (P->state == ST_CLIMB) return ent_collide_at(e, e->x + P->facing, e->y, s);
+ return ent_collide_at(e, e->x, e->y + 1, s);
+}
+bool player_riding_jumpthru(const Ent *j) {
+ Ent *e = P->e;
+ return P->state != ST_CLIMB && !f_negative(P->sy) && ent_collide_at(e, e->x, e->y + 1, j) && !ent_collide_at(e, e->x, e->y, j);
+}
+void player_set_lift(f32 lx, f32 ly) {
+ P->e->liftx = lx, P->e->lifty = ly;
+ if (lx || ly) P->last_liftx = lx, P->last_lifty = ly, P->lift_timer = F_LiftTime;
+}
+static bool can_unduck(void);
+/* squished between solids: ducking first, then a wiggle of up to 3 pixels, else dead */
+static void on_squish(int dx, int dy, Ent *hit) {
+ (void)dx, (void)dy, (void)hit;
+ bool ducked = false;
+ if (!Ducking) {
+ ducked = true;
+ set_ducking(true);
+ if (!solid_off(0, 0)) return;
+ }
+ for (int i = 0; i <= 3; i++)
+ for (int j = 0; j <= 3; j++) {
+ if (!i && !j) continue;
+ for (int sx = 1; sx >= -1; sx -= 2)
+ for (int sy = 1; sy >= -1; sy -= 2)
+ if (!solid_off(i * sx, j * sy)) {
+ E->x += i * sx, E->y += j * sy;
+ if (ducked && can_unduck()) set_ducking(false);
+ return;
+ }
+ }
+ player_die(0, 0);
+}
+void player_push_h(int move, Ent *pusher) { E = P->e; (void)pusher; move_h_exact(move, on_squish); }
+void player_push_v(int move, Ent *pusher) { E = P->e; (void)pusher; move_v_exact(move, on_squish); }
+void player_carry_h(int move) { E = P->e; move_h_exact(move, NULL); }
+void player_carry_v(int move) { E = P->e; move_v_exact(move, NULL); }
+
/* ---------------------------------------------------------------- the sprite */
static void play(int a) {
if (P->anim == a) return;
P->anim = a, P->frame = 0, P->anim_timer = 0;
}
-static void play_r(int a) { P->anim = -1, play(a); }
static int cur_sprite(void) {
const uint8_t *an = anim(P->anim);
int f = imin(P->frame, an[0] - 1);
return (int)R16(an + 6 + 2 * f);
}
-static void sprite_update(void) {
+static void sprite_update(void) { /* (frame timing in 600ths of a second: only the look depends on it) */
const uint8_t *an = anim(P->anim);
int delay = (int)R16(an + 2), n = an[0];
if (!delay) return;
@@ -180,8 +160,8 @@ static void sprite_update(void) {
}
if (an[4] != 255) { /* goto */
int g = an[4];
- if (g == A_player_idle && P->anim == A_player_idle && P->idle_timer > TIM(3) && rndn(5) == 0) {
- int r = rndn(9); /* the idle's variants (Player.cs: idleA, B, C) */
+ if (g == A_player_idle && P->anim == A_player_idle && P->idle_timer > 3 * 60 && rndn(5) == 0) {
+ int r = rndn(9); /* the idle's variants */
g = r < 5 ? A_player_idleA : r < 8 ? A_player_idleB : A_player_idleC;
}
P->anim = -1, play(g);
@@ -193,8 +173,12 @@ static void sprite_update(void) {
static bool in_control(void) {
return P->state != ST_INTROJUMP && P->state != ST_INTROWALK && P->state != ST_INTRORESPAWN && P->state != ST_DUMMY;
}
-bool player_dash_attacking(void) { return P->dash_attack > 0; }
-static bool is_tired(void) { return (P->wall_boost_timer > 0 ? P->stamina + ClimbJumpCost : P->stamina) < ClimbTiredThreshold; }
+bool player_in_control(void) { return in_control(); }
+bool player_dash_attacking(void) { return f_pos(P->dash_attack); }
+static bool is_tired(void) {
+ f32 s = f_pos(P->wall_boost_timer) ? f_add(P->stamina, F_ClimbJumpCost) : P->stamina;
+ return f_lt(s, F_ClimbTiredThreshold);
+}
bool player_refill_dash(void) {
if (P->dashes < P->max_dashes) {
P->dashes = P->max_dashes;
@@ -204,9 +188,9 @@ bool player_refill_dash(void) {
}
bool player_use_refill(bool two) {
int max = two ? 2 : P->max_dashes;
- if (P->dashes < max || P->stamina < ClimbTiredThreshold) {
+ if (P->dashes < max || f_lt(P->stamina, F_ClimbTiredThreshold)) {
P->dashes = max;
- P->stamina = ClimbMaxStamina;
+ P->stamina = F_ClimbMaxStamina;
return true;
}
return false;
@@ -239,28 +223,28 @@ static bool duck_free_at(int x, int y) {
static bool climb_bounds_check(int dir) {
Room *rm = g_level.room;
- return e_left(E) + dir * ClimbCheckDist >= rm->x && e_right(E) + dir * ClimbCheckDist < rm->x + rm->w;
+ return e_left(E) + dir * 2 >= rm->x && e_right(E) + dir * 2 < rm->x + rm->w;
}
-static bool climb_check(int dir, int yadd) { return climb_bounds_check(dir) && solid_off(dir * ClimbCheckDist, yadd); }
-static bool wall_jump_check(int dir) { return climb_bounds_check(dir) && solid_off(dir * WallJumpCheckDist, 0); }
+static bool climb_check(int dir, int yadd) { return climb_bounds_check(dir) && solid_off(dir * 2, yadd); }
+static bool wall_jump_check(int dir) { return climb_bounds_check(dir) && solid_off(dir * 3, 0); }
-static void aim(int *x, int *y) { /* 1/256 */
+static void aim(void) {
int ax = g_in.aim_x, ay = g_in.aim_y;
- if (!ax && !ay) *x = P->facing * 256, *y = 0;
- else if (ax && ay) *x = ax * D, *y = ay * D;
- else *x = ax * 256, *y = ay * 256;
+ P->aim_x = ax, P->aim_y = ay;
+ if (!ax && !ay) P->aim_x = P->facing;
}
static int normal_update(void), climb_update(void), dash_update(void);
static void normal_begin(void), normal_end(void), climb_begin(void), climb_end(void), dash_begin(void), dash_end(void);
-static void respawn_begin(void), respawn_end(void);
-static int co_dash(void), co_intro_jump(void), co_intro_walk(void);
+static void respawn_begin(void);
+static int32_t co_dash(void), co_intro_jump(void), co_intro_walk(void);
typedef int (*UpdFn)(void);
typedef void (*VFn)(void);
static const UpdFn UPD[ST_COUNT] = {normal_update, climb_update, dash_update};
static const VFn BEGIN[ST_COUNT] = {normal_begin, climb_begin, dash_begin, respawn_begin};
-static const VFn END[ST_COUNT] = {normal_end, climb_end, dash_end, respawn_end};
-static const UpdFn CO[ST_COUNT] = {NULL, NULL, co_dash, NULL, co_intro_jump, co_intro_walk};
+static const VFn END[ST_COUNT] = {normal_end, climb_end, dash_end};
+typedef int32_t (*CoFn)(void);
+static const CoFn CO[ST_COUNT] = {NULL, NULL, co_dash, NULL, co_intro_jump, co_intro_walk};
static void set_state(int st) {
if (P->state == st) return;
@@ -271,247 +255,272 @@ static void set_state(int st) {
P->co_active = CO[st] != NULL;
P->co_step = 0, P->co_wait = 0;
}
+/* the state's coroutine: a step returns how long to wait (a float's bits), or -1 when it ends */
static void co_update(void) {
if (!P->co_active) return;
- if (P->co_wait > 0) {
- P->co_wait -= DTU;
+ if (f_pos(P->co_wait)) {
+ P->co_wait = dec(P->co_wait);
return;
}
int st = P->state;
- int r = CO[st]();
+ int32_t r = CO[st]();
if (P->state != st || !P->co_active) return;
if (r < 0) P->co_active = false;
- else P->co_wait = r;
+ else P->co_wait = (f32)r;
}
/* ---------------------------------------------------------------- jumps */
-static void jump_common(int var_time) {
- btn_consume(&g_in.jump), g_in.jump.pressed = true; /* (ConsumeBuffer: still Pressed this frame) */
+static void jump_reset(f32 var_time) {
+ btn_consume_buffer(&g_in.jump);
P->jump_grace = 0;
P->var_jump = var_time;
P->auto_jump = false;
P->dash_attack = 0;
- P->wall_slide_timer = WallSlideTime;
+ P->wall_slide_timer = F_WallSlideTime;
P->wall_boost_timer = 0;
}
static void jump(void) {
- jump_common(VarJumpTime);
- int lx, ly;
+ jump_reset(F_VarJumpTime);
+ f32 lx, ly;
+ P->sx = f_add(P->sx, ftimes(F_JumpHBoost, P->move_x));
+ P->sy = F_JumpSpeed;
lift_boost(&lx, &ly);
- P->sx += JumpHBoost * P->move_x + lx;
- P->sy = JumpSpeed + ly;
+ P->sx = f_add(P->sx, lx), P->sy = f_add(P->sy, ly);
P->var_jump_speed = P->sy;
P->scale_x = 614, P->scale_y = 1434;
}
static void super_jump(void) {
- jump_common(VarJumpTime);
- int lx, ly;
+ jump_reset(F_VarJumpTime);
+ f32 lx, ly;
+ P->sx = ftimes(F_SuperJumpH, P->facing);
+ P->sy = F_JumpSpeed;
lift_boost(&lx, &ly);
- P->sx = SuperJumpH * P->facing + lx;
- P->sy = JumpSpeed + ly;
+ P->sx = f_add(P->sx, lx), P->sy = f_add(P->sy, ly);
if (Ducking) {
set_ducking(false);
- P->sx = P->sx * 5 / 4; /* DuckSuperJumpXMult */
- P->sy /= 2; /* DuckSuperJumpYMult */
+ P->sx = f_mul(P->sx, F_DuckSuperJumpXMult);
+ P->sy = f_mul(P->sy, F_DuckSuperJumpYMult);
}
P->var_jump_speed = P->sy;
P->scale_x = 614, P->scale_y = 1434;
}
static void wall_jump(int dir) {
set_ducking(false);
- jump_common(VarJumpTime);
- if (P->move_x != 0) P->force_move_x = dir, P->force_move_x_timer = WallJumpForceTime;
- int lx, ly;
+ jump_reset(F_VarJumpTime);
+ if (P->move_x != 0) P->force_move_x = dir, P->force_move_x_timer = F_WallJumpForceTime;
+ f32 lx, ly;
+ if (!lift_x() && !lift_y()) { /* the wall's own lift speed */
+ Ent *w = solid_first(e_left(E) + 3, e_top(E), e_right(E) + 3, e_bottom(E), E);
+ if (w) player_set_lift(w->liftx, w->lifty);
+ }
+ P->sx = ftimes(F_WallJumpHSpeed, dir);
+ P->sy = F_JumpSpeed;
lift_boost(&lx, &ly);
- P->sx = WallJumpHSpeed * dir + lx;
- P->sy = JumpSpeed + ly;
+ P->sx = f_add(P->sx, lx), P->sy = f_add(P->sy, ly);
P->var_jump_speed = P->sy;
P->scale_x = 614, P->scale_y = 1434;
}
static void super_wall_jump(int dir) {
set_ducking(false);
- jump_common(SuperWallJumpVarTime);
- int lx, ly;
+ jump_reset(F_SuperWallJumpVarTime);
+ f32 lx, ly;
+ P->sx = ftimes(F_SuperWallJumpH, dir);
+ P->sy = F_SuperWallJumpSpeed;
lift_boost(&lx, &ly);
- P->sx = SuperWallJumpH * dir + lx;
- P->sy = SuperWallJumpSpeed + ly;
+ P->sx = f_add(P->sx, lx), P->sy = f_add(P->sy, ly);
P->var_jump_speed = P->sy;
P->scale_x = 614, P->scale_y = 1434;
}
static void climb_jump(void) {
- if (!P->on_ground) P->stamina -= ClimbJumpCost;
+ if (!P->on_ground) P->stamina = f_sub(P->stamina, F_ClimbJumpCost);
jump();
- if (P->move_x == 0) P->wall_boost_dir = -P->facing, P->wall_boost_timer = ClimbJumpBoostTime;
+ if (P->move_x == 0) P->wall_boost_dir = -P->facing, P->wall_boost_timer = F_ClimbJumpBoostTime;
}
-void player_bounce(int from_y) {
- E = P->e;
- int8_t was[4] = {(int8_t)E->hw, (int8_t)E->hh, (int8_t)E->hx, (int8_t)E->hy};
- set_box(HB_NORMAL);
- move_v_exact(from_y - e_bottom(E), NULL);
+static void save_box(int8_t *w) { w[0] = (int8_t)E->hw, w[1] = (int8_t)E->hh, w[2] = (int8_t)E->hx, w[3] = (int8_t)E->hy; }
+static void bounce_common(f32 var_time) {
player_refill_dash();
- P->stamina = ClimbMaxStamina;
+ P->stamina = F_ClimbMaxStamina;
set_state(ST_NORMAL);
P->jump_grace = 0;
- P->var_jump = BounceVarJumpTime;
+ P->var_jump = var_time;
P->auto_jump = true;
- P->auto_jump_timer = BounceAutoJumpTime;
P->dash_attack = 0;
- P->wall_slide_timer = WallSlideTime;
+ P->wall_slide_timer = F_WallSlideTime;
P->wall_boost_timer = 0;
- P->var_jump_speed = P->sy = BounceSpeed;
+}
+void player_bounce(int from_y) {
+ E = P->e;
+ int8_t was[4];
+ save_box(was);
+ set_box(HB_NORMAL);
+ move_v_exact(from_y - e_bottom(E), NULL);
+ bounce_common(F_BounceVarJumpTime);
+ P->auto_jump_timer = F_BounceAutoJumpTime;
+ P->var_jump_speed = P->sy = F_BounceSpeed;
P->scale_x = 614, P->scale_y = 1434;
set_box(was);
}
void player_super_bounce(int from_y) {
E = P->e;
- int8_t was[4] = {(int8_t)E->hw, (int8_t)E->hh, (int8_t)E->hx, (int8_t)E->hy};
+ int8_t was[4];
+ save_box(was);
set_box(HB_NORMAL);
- move_v((from_y - e_bottom(E)) * MU, NULL);
- player_refill_dash();
- P->stamina = ClimbMaxStamina;
- set_state(ST_NORMAL);
- P->jump_grace = 0;
- P->var_jump = SuperBounceVarJumpTime;
- P->auto_jump = true;
+ move_v(f_int(from_y - e_bottom(E)), NULL);
+ bounce_common(F_SuperBounceVarJumpTime);
P->auto_jump_timer = 0;
- P->dash_attack = 0;
- P->wall_slide_timer = WallSlideTime;
- P->wall_boost_timer = 0;
P->sx = 0;
- P->var_jump_speed = P->sy = SuperBounceSpeed;
- level_dir_shake(0, -1, TIM(.1f) / DTU);
+ P->var_jump_speed = P->sy = F_SuperBounceSpeed;
+ level_dir_shake(0, -1, 6);
P->scale_x = 512, P->scale_y = 1536;
set_box(was);
}
void player_side_bounce(int dir, int from_x, int from_y) {
E = P->e;
- int8_t was[4] = {(int8_t)E->hw, (int8_t)E->hh, (int8_t)E->hx, (int8_t)E->hy};
+ int8_t was[4];
+ save_box(was);
set_box(HB_NORMAL);
- move_v(iclamp(from_y - e_bottom(E), -4, 4) * MU, NULL);
- if (dir > 0) move_h((from_x - e_left(E)) * MU, NULL);
- else if (dir < 0) move_h((from_x - e_right(E)) * MU, NULL);
- player_refill_dash();
- P->stamina = ClimbMaxStamina;
- set_state(ST_NORMAL);
- P->jump_grace = 0;
- P->var_jump = BounceVarJumpTime;
- P->auto_jump = true;
+ move_v(f_int(iclamp(from_y - e_bottom(E), -4, 4)), NULL);
+ if (dir > 0) move_h(f_int(from_x - e_left(E)), NULL);
+ else if (dir < 0) move_h(f_int(from_x - e_right(E)), NULL);
+ bounce_common(F_BounceVarJumpTime);
P->auto_jump_timer = 0;
- P->dash_attack = 0;
- P->wall_slide_timer = WallSlideTime;
P->force_move_x = dir;
- P->force_move_x_timer = TIM(.3f);
- P->wall_boost_timer = 0;
- P->sx = SPD(240) * dir;
- P->var_jump_speed = P->sy = BounceSpeed;
- level_dir_shake(dir, 0, TIM(.1f) / DTU);
+ P->force_move_x_timer = F_SideBounceForce;
+ P->sx = ftimes(F_SideBounceSpeed, dir);
+ P->var_jump_speed = P->sy = F_BounceSpeed;
+ level_dir_shake(dir, 0, 6);
P->scale_x = 1536, P->scale_y = 512;
set_box(was);
}
+static void rebound(int dir) { /* Player.Rebound: off a dash block */
+ P->sx = ftimes(F_ReboundSpeedX, dir);
+ P->sy = F_ReboundSpeedY;
+ P->var_jump_speed = P->sy;
+ P->var_jump = F_ReboundVarJumpTime;
+ P->auto_jump = true;
+ P->auto_jump_timer = 0;
+ P->dash_attack = 0;
+ P->wall_slide_timer = F_WallSlideTime;
+ P->wall_boost_timer = 0;
+ P->force_move_x_timer = 0;
+ set_state(ST_NORMAL);
+}
/* ---------------------------------------------------------------- collisions */
+extern void ents_dash_into(Ent *hit, int dx, int dy, bool *rebound);
static void on_collide_h(int dx, int dy, Ent *hit) {
- (void)dy, (void)hit;
+ (void)dy;
+ if (player_dash_attacking() && dx == f_sign(P->dash_dx)) {
+ bool rb;
+ ents_dash_into(hit, dx, 0, &rb);
+ if (rb) { rebound(-f_sign(P->sx)); return; }
+ }
if (P->state == ST_DASH) {
- if (P->on_ground && duck_free_at(E->x + isign(P->sx), E->y)) {
+ if (P->on_ground && duck_free_at(E->x + f_sign(P->sx), E->y)) {
set_ducking(true);
return;
- } else if (P->sy == 0 && P->sx != 0) {
- for (int i = 1; i <= DashCornerCorrection; i++)
+ } else if (f_zero(P->sy) && !f_zero(P->sx)) {
+ for (int i = 1; i <= 4; i++) /* DashCornerCorrection */
for (int j = 1; j >= -1; j -= 2)
- if (!solid_off(isign(P->sx), i * j)) {
+ if (!solid_off(f_sign(P->sx), i * j)) {
move_v_exact(i * j, NULL);
- move_h_exact(isign(P->sx), NULL);
+ move_h_exact(f_sign(P->sx), NULL);
return;
}
}
}
- if (P->wall_speed_retention <= 0) {
+ if (!f_pos(P->wall_speed_retention)) {
P->wall_speed_retained = P->sx;
- P->wall_speed_retention = WallSpeedRetentionTime;
+ P->wall_speed_retention = F_WallSpeedRetentionTime;
}
P->sx = 0;
P->dash_attack = 0;
- (void)dx;
}
static void on_collide_v(int dx, int dy, Ent *hit) {
- (void)dx, (void)dy, (void)hit;
- if (P->sy > 0) {
+ (void)dx;
+ if (player_dash_attacking() && dy == f_sign(P->dash_dy)) {
+ bool rb;
+ ents_dash_into(hit, 0, dy, &rb);
+ if (rb) { rebound(0); return; }
+ }
+ if (f_pos(P->sy)) {
if (P->state == ST_DASH && !P->dash_started_on_ground) {
- if (P->sx <= 0)
- for (int i = -1; i >= -DashCornerCorrection; i--)
+ if (!f_pos(P->sx))
+ for (int i = -1; i >= -4; i--)
if (!on_ground_at(E->x + i, E->y)) {
move_h_exact(i, NULL);
move_v_exact(1, NULL);
return;
}
- if (P->sx >= 0)
- for (int i = 1; i <= DashCornerCorrection; i++)
+ if (!f_negative(P->sx))
+ for (int i = 1; i <= 4; i++)
if (!on_ground_at(E->x + i, E->y)) {
move_h_exact(i, NULL);
move_v_exact(1, NULL);
return;
}
}
- if (P->dash_dx != 0 && P->dash_dy > 0 && P->sy > 0) { /* a diagonal dash into the floor: a slide */
- P->dash_dx = isign(P->dash_dx) * 256;
+ if (!f_zero(P->dash_dx) && f_pos(P->dash_dy) && f_pos(P->sy)) { /* a diagonal dash into the floor: a slide */
+ P->dash_dx = f_sign(P->dash_dx) > 0 ? F_1 : f_neg(F_1);
P->dash_dy = 0;
P->sy = 0;
- P->sx = P->sx * 6 / 5; /* DodgeSlideSpeedMult */
+ P->sx = f_mul(P->sx, F_DodgeSlideSpeedMult);
set_ducking(true);
}
- if (P->state != ST_CLIMB) { /* a squash as she lands */
- int sq = imin(P->sy * 256 / FastMaxFall, 256);
+ if (P->state != ST_CLIMB) { /* the landing's squash (Lerp(1, 1.6), Lerp(1, .4)) */
+ int sq = imin(f_trunc(f_mul(P->sy, f_int(256))) / 240, 256);
P->scale_x = 1024 + 614 * sq / 256;
P->scale_y = 1024 - 614 * sq / 256;
}
- } else if (P->sy < 0) {
- if (P->sx <= 0)
- for (int i = 1; i <= UpwardCornerCorrection; i++)
- if (!solid_off(-i, -1)) {
- E->x -= i, E->y -= 1;
- return;
- }
- if (P->sx >= 0)
- for (int i = 1; i <= UpwardCornerCorrection; i++)
- if (!solid_off(i, -1)) {
- E->x += i, E->y -= 1;
- return;
- }
- if (P->var_jump < VarJumpTime - CeilingVarJumpGrace) P->var_jump = 0;
+ } else {
+ if (f_negative(P->sy)) {
+ if (!f_pos(P->sx))
+ for (int i = 1; i <= 4; i++) /* UpwardCornerCorrection */
+ if (!solid_off(-i, -1)) {
+ E->x -= i, E->y -= 1;
+ return;
+ }
+ if (!f_negative(P->sx))
+ for (int i = 1; i <= 4; i++)
+ if (!solid_off(i, -1)) {
+ E->x += i, E->y -= 1;
+ return;
+ }
+ if (f_lt(P->var_jump, f_sub(F_VarJumpTime, F_CeilingVarJumpGrace))) P->var_jump = 0;
+ }
}
P->dash_attack = 0;
P->sy = 0;
}
/* ---------------------------------------------------------------- normal */
-static void normal_begin(void) { P->max_fall = MaxFall; }
+static void normal_begin(void) { P->max_fall = F_MaxFall; }
static void normal_end(void) {
P->wall_boost_timer = 0;
P->wall_speed_retention = 0;
P->hop_wait_x = 0;
}
-static bool can_dash(void) { return btn_pressed(&g_in.dash) && P->dash_cooldown <= 0 && P->dashes > 0; }
+static bool can_dash(void) { return btn_pressed(&g_in.dash) && !f_pos(P->dash_cooldown) && P->dashes > 0; }
static int start_dash(void) {
P->was_dash_b = P->dashes == 2;
P->dashes = imax(P->dashes - 1, 0);
- btn_consume(&g_in.dash), g_in.dash.pressed = true;
+ btn_consume_buffer(&g_in.dash);
return ST_DASH;
}
static int normal_update(void) {
- int lx, ly;
+ f32 lx, ly;
lift_boost(&lx, &ly);
- if (ly < 0 && P->was_on_ground && !P->on_ground && P->sy >= 0) P->sy = ly;
+ if (f_negative(ly) && P->was_on_ground && !P->on_ground && !f_negative(P->sy)) P->sy = ly;
if (g_in.grab.check && !is_tired() && !Ducking) {
- if (P->sy >= 0 && isign(P->sx) != -P->facing) {
+ if (!f_negative(P->sy) && f_sign(P->sx) != -P->facing) {
if (climb_check(P->facing, 0)) {
set_ducking(false);
return ST_CLIMB;
}
if (g_in.move_y < 1)
- for (int i = 1; i <= ClimbUpCheckDist; i++)
+ for (int i = 1; i <= 2; i++) /* ClimbUpCheckDist */
if (!solid_off(0, -i) && climb_check(P->facing, -i)) {
move_v_exact(-i, NULL);
set_ducking(false);
@@ -520,7 +529,8 @@ static int normal_update(void) {
}
}
if (can_dash()) {
- P->sx += lx, P->sy += ly;
+ lift_boost(&lx, &ly);
+ P->sx = f_add(P->sx, lx), P->sy = f_add(P->sy, ly);
return start_dash();
}
if (Ducking) {
@@ -528,67 +538,68 @@ static int normal_update(void) {
if (can_unduck()) {
set_ducking(false);
P->scale_x = 819, P->scale_y = 1229;
- } else if (P->sx == 0) {
- for (int i = DuckCorrectCheck; i > 0; i--) {
+ } else if (f_zero(P->sx)) {
+ for (int i = 4; i > 0; i--) { /* DuckCorrectCheck */
if (can_unduck_at(E->x + i, E->y)) {
- move_h(DuckCorrectSlide, on_collide_h);
+ move_h(f_mul(F_DuckCorrectSlide, DT), NULL);
break;
} else if (can_unduck_at(E->x - i, E->y)) {
- move_h(-DuckCorrectSlide, on_collide_h);
+ move_h(f_mul(f_neg(F_DuckCorrectSlide), DT), NULL);
break;
}
}
}
}
- } else if (P->on_ground && g_in.move_y == 1 && P->sy >= 0) {
+ } else if (P->on_ground && g_in.move_y == 1 && !f_negative(P->sy)) {
set_ducking(true);
P->scale_x = 1434, P->scale_y = 614;
}
- if (Ducking && P->on_ground) P->sx = approach(P->sx, 0, DuckFriction);
+ if (Ducking && P->on_ground) P->sx = f_approach(P->sx, 0, f_mul(F_DuckFriction, DT));
else {
bool g = P->on_ground;
- if (iabs(P->sx) > MaxRun && isign(P->sx) == P->move_x)
- P->sx = approach(P->sx, MaxRun * P->move_x, g ? RunReduce : RunReduce * 65 / 100);
+ f32 target = ftimes(F_MaxRun, P->move_x);
+ if (f_lt(F_MaxRun, f_abs(P->sx)) && f_sign(P->sx) == P->move_x)
+ P->sx = f_approach(P->sx, target, f_mul(g ? F_RunReduce : F_RunReduceAir, DT));
else
- P->sx = approach(P->sx, MaxRun * P->move_x, g ? RunAccel : RunAccel * 65 / 100);
+ P->sx = f_approach(P->sx, target, f_mul(g ? F_RunAccel : F_RunAccelAir, DT));
}
- if (g_in.move_y == 1 && P->sy >= MaxFall) {
- P->max_fall = approach(P->max_fall, FastMaxFall, FastMaxAccel);
- int half = MaxFall + (FastMaxFall - MaxFall) / 2;
- if (P->sy >= half) {
- int l = imin(256, (P->sy - half) * 256 / (FastMaxFall - half));
+ if (g_in.move_y == 1 && !f_lt(P->sy, F_MaxFall)) {
+ P->max_fall = f_approach(P->max_fall, F_FastMaxFall, f_mul(F_FastMaxAccel, DT));
+ if (!f_lt(P->sy, F_FastHalf)) { /* the stretch: Lerp(1, .5), Lerp(1, 1.5) */
+ int l = imin(256, f_trunc(f_mul(f_sub(P->sy, F_FastHalf), f_int(256))) / 40);
P->scale_x = 1024 - 512 * l / 256, P->scale_y = 1024 + 512 * l / 256;
}
} else
- P->max_fall = approach(P->max_fall, MaxFall, FastMaxAccel);
+ P->max_fall = f_approach(P->max_fall, F_MaxFall, f_mul(F_FastMaxAccel, DT));
if (!P->on_ground) {
- int max = P->max_fall;
+ f32 max = P->max_fall;
if ((P->move_x == P->facing || (P->move_x == 0 && g_in.grab.check)) && g_in.move_y != 1) {
- if (P->sy >= 0 && P->wall_slide_timer > 0 && climb_bounds_check(P->facing) && solid_off(P->facing, 0) &&
+ if (!f_negative(P->sy) && f_pos(P->wall_slide_timer) && climb_bounds_check(P->facing) && solid_off(P->facing, 0) &&
can_unduck()) {
set_ducking(false);
P->wall_slide_dir = P->facing;
}
- if (P->wall_slide_dir != 0) /* Lerp(MaxFall, WallSlideStartMax, timer / WallSlideTime) */
- max = MaxFall + (int)((int32_t)(WallSlideStartMax - MaxFall) * P->wall_slide_timer / WallSlideTime);
+ if (P->wall_slide_dir != 0)
+ max = f_lerp(F_MaxFall, F_WallSlideStartMax, f_div(P->wall_slide_timer, F_WallSlideTime));
}
- bool half = iabs(P->sy) < HalfGravThreshold && (g_in.jump.check || P->auto_jump);
- P->sy = approach(P->sy, max, half ? Gravity / 2 : Gravity);
+ bool half = f_lt(f_abs(P->sy), F_HalfGravThreshold) && (g_in.jump.check || P->auto_jump);
+ P->sy = f_approach(P->sy, max, f_mul(half ? f_mul(F_Gravity, F_HALF) : F_Gravity, DT));
}
- if (P->var_jump > 0) {
- if (P->auto_jump || g_in.jump.check) P->sy = imin(P->sy, P->var_jump_speed);
+ if (f_pos(P->var_jump)) {
+ if (P->auto_jump || g_in.jump.check) P->sy = f_min(P->sy, P->var_jump_speed);
else P->var_jump = 0;
}
if (btn_pressed(&g_in.jump)) {
- if (P->jump_grace > 0) jump();
+ if (f_pos(P->jump_grace)) jump();
else if (can_unduck()) {
+ bool up_dash = player_dash_attacking() && f_zero(P->dash_dx) && P->dash_dy == f_neg(F_1);
if (wall_jump_check(1)) {
- if (P->facing == 1 && g_in.grab.check && P->stamina > 0) climb_jump();
- else if (player_dash_attacking() && P->dash_dx == 0 && P->dash_dy == -256) super_wall_jump(-1);
+ if (P->facing == 1 && g_in.grab.check && f_pos(P->stamina)) climb_jump();
+ else if (up_dash) super_wall_jump(-1);
else wall_jump(-1);
} else if (wall_jump_check(-1)) {
- if (P->facing == -1 && g_in.grab.check && P->stamina > 0) climb_jump();
- else if (player_dash_attacking() && P->dash_dx == 0 && P->dash_dy == -256) super_wall_jump(1);
+ if (P->facing == -1 && g_in.grab.check && f_pos(P->stamina)) climb_jump();
+ else if (up_dash) super_wall_jump(1);
else wall_jump(1);
}
}
@@ -600,23 +611,30 @@ static int normal_update(void) {
static void climb_begin(void) {
P->auto_jump = false;
P->sx = 0;
- P->sy /= 5; /* ClimbGrabYMult */
- P->wall_slide_timer = WallSlideTime;
- P->climb_no_move = ClimbNoMoveTime;
+ P->sy = f_mul(P->sy, F_ClimbGrabYMult);
+ P->wall_slide_timer = F_WallSlideTime;
+ P->climb_no_move = F_ClimbNoMoveTime;
P->wall_boost_timer = 0;
P->last_climb_move = 0;
- for (int i = 0; i < ClimbCheckDist; i++)
+ for (int i = 0; i < 2; i++)
if (!solid_off(P->facing, 0)) E->x += P->facing;
else break;
}
static void climb_end(void) { P->wall_speed_retention = 0; }
static void climb_hop(void) {
- P->hop_wait_x = 0;
- if (solid_off(P->facing, 0)) P->hop_wait_x = P->facing, P->hop_wait_x_speed = P->facing * ClimbHopX;
- else P->sx = P->facing * ClimbHopX;
- P->sy = imin(P->sy, ClimbHopY);
+ Ent *s = solid_first(e_left(E) + P->facing, e_top(E), e_right(E) + P->facing, e_bottom(E), E);
+ P->climb_hop_solid = s;
+ if (s || tiles_solid(e_left(E) + P->facing, e_top(E), e_right(E) + P->facing, e_bottom(E))) {
+ if (s) P->climb_hop_x = s->x, P->climb_hop_y = s->y;
+ P->hop_wait_x = P->facing;
+ P->hop_wait_x_speed = ftimes(F_ClimbHopX, P->facing);
+ } else {
+ P->hop_wait_x = 0;
+ P->sx = ftimes(F_ClimbHopX, P->facing);
+ }
+ P->sy = f_min(P->sy, F_ClimbHopY);
P->force_move_x = 0;
- P->force_move_x_timer = ClimbHopForceTime;
+ P->force_move_x_timer = F_ClimbHopForceTime;
P->fast_jump = false;
}
static bool slip_check(int add_y) {
@@ -626,42 +644,42 @@ static bool slip_check(int add_y) {
static bool ledge_blocked(int x, int y) { /* spikes in the way of a hop (LedgeBlocker) */
for (int i = 0; i < g_nents; i++) {
Ent *o = &g_ents[i];
- if (o->type <= ET_spikesRight && o->type >= ET_spikesUp && o->type != ET_spikesDown && (o->flags & EF_COLLIDABLE) &&
+ if ((o->type == ET_spikesUp || o->type == ET_spikesLeft || o->type == ET_spikesRight) && (o->flags & EF_COLLIDABLE) &&
ent_collide_at(E, x, y, o))
return true;
}
return false;
}
static int climb_update(void) {
- P->climb_no_move -= DTU;
- if (P->on_ground) P->stamina = ClimbMaxStamina;
+ P->climb_no_move = dec(P->climb_no_move);
+ if (P->on_ground) P->stamina = F_ClimbMaxStamina;
if (btn_pressed(&g_in.jump) && (!Ducking || can_unduck())) {
if (P->move_x == -P->facing) wall_jump(-P->facing);
else climb_jump();
return ST_NORMAL;
}
- int lx, ly;
+ f32 lx, ly;
if (can_dash()) {
lift_boost(&lx, &ly);
- P->sx += lx, P->sy += ly;
+ P->sx = f_add(P->sx, lx), P->sy = f_add(P->sy, ly);
return start_dash();
}
if (!g_in.grab.check) {
lift_boost(&lx, &ly);
- P->sx += lx, P->sy += ly;
+ P->sx = f_add(P->sx, lx), P->sy = f_add(P->sy, ly);
return ST_NORMAL;
}
if (!solid_off(P->facing, 0)) {
- if (P->sy < 0) climb_hop();
+ if (f_negative(P->sy)) climb_hop();
return ST_NORMAL;
}
- int target = 0;
+ f32 target = 0;
bool try_slip = false;
- if (P->climb_no_move <= 0) {
+ if (!f_pos(P->climb_no_move)) {
if (g_in.move_y == -1) {
- target = ClimbUpSpeed;
+ target = F_ClimbUpSpeed;
if (solid_off(0, -1) || ((ledge_blocked(E->x + P->facing * 8, E->y) || solid_off(0, -6)) && slip_check(-1))) {
- if (P->sy < 0) P->sy = 0;
+ if (f_negative(P->sy)) P->sy = 0;
target = 0;
try_slip = true;
} else if (slip_check(0)) {
@@ -669,26 +687,26 @@ static int climb_update(void) {
return ST_NORMAL;
}
} else if (g_in.move_y == 1) {
- target = ClimbDownSpeed;
+ target = F_ClimbDownSpeed;
if (P->on_ground) {
- if (P->sy > 0) P->sy = 0;
+ if (f_pos(P->sy)) P->sy = 0;
target = 0;
}
} else
try_slip = true;
} else
try_slip = true;
- P->last_climb_move = isign(target);
- if (try_slip && slip_check(0)) target = ClimbSlipSpeed;
- P->sy = approach(P->sy, target, ClimbAccel);
- if (g_in.move_y != 1 && P->sy > 0 && !solid_off(P->facing, 1)) P->sy = 0;
- if (P->climb_no_move <= 0) {
- if (P->last_climb_move == -1) P->stamina -= ClimbUpCost;
- else if (P->last_climb_move == 0) P->stamina -= ClimbStillCost;
+ P->last_climb_move = f_sign(target);
+ if (try_slip && slip_check(0)) target = F_ClimbSlipSpeed;
+ P->sy = f_approach(P->sy, target, f_mul(F_ClimbAccel, DT));
+ if (g_in.move_y != 1 && f_pos(P->sy) && !solid_off(P->facing, 1)) P->sy = 0;
+ if (!f_pos(P->climb_no_move)) {
+ if (P->last_climb_move == -1) P->stamina = f_sub(P->stamina, f_mul(F_ClimbUpCost, DT));
+ else if (P->last_climb_move == 0) P->stamina = f_sub(P->stamina, f_mul(F_ClimbStillCost, DT));
}
- if (P->stamina <= 0) {
+ if (!f_pos(P->stamina)) {
lift_boost(&lx, &ly);
- P->sx += lx, P->sy += ly;
+ P->sx = f_add(P->sx, lx), P->sy = f_add(P->sy, ly);
return ST_NORMAL;
}
return ST_CLIMB;
@@ -699,11 +717,10 @@ static void dash_begin(void) {
P->called_dash_events = false;
P->dash_started_on_ground = P->on_ground;
level_freeze(TIM(.05f));
- P->dash_cooldown = DashCooldown;
- P->dash_refill_cooldown = DashRefillCooldown;
- P->wall_slide_timer = WallSlideTime;
- P->dash_trail_timer = 0;
- P->dash_attack = DashAttackTime;
+ P->dash_cooldown = F_DashCooldown;
+ P->dash_refill_cooldown = F_DashRefillCooldown;
+ P->wall_slide_timer = F_WallSlideTime;
+ P->dash_attack = F_DashAttackTime;
P->before_dash_sx = P->sx, P->before_dash_sy = P->sy;
P->sx = P->sy = 0;
P->dash_dx = P->dash_dy = 0;
@@ -711,19 +728,19 @@ static void dash_begin(void) {
}
static void dash_end(void) { P->called_dash_events = true; }
static int dash_update(void) {
- if (P->dash_dy == 0) {
+ if (f_zero(P->dash_dy)) {
for (int i = 0; i < g_nents; i++) { /* up onto a jump-through a dash runs into */
Ent *jt = &g_ents[i];
if ((jt->flags & (EF_JUMPTHRU | EF_COLLIDABLE)) == (EF_JUMPTHRU | EF_COLLIDABLE) && ent_collide_at(E, E->x, E->y, jt) &&
- e_bottom(E) - e_top(jt) <= DashHJumpThruNudge)
+ e_bottom(E) - e_top(jt) <= 6)
move_v_exact(e_top(jt) - e_bottom(E), NULL);
}
- if (can_unduck() && btn_pressed(&g_in.jump) && P->jump_grace > 0) {
+ if (can_unduck() && btn_pressed(&g_in.jump) && f_pos(P->jump_grace)) {
super_jump();
return ST_NORMAL;
}
}
- if (P->dash_dx == 0 && P->dash_dy == -256) {
+ if (f_zero(P->dash_dx) && P->dash_dy == f_neg(F_1)) {
if (btn_pressed(&g_in.jump) && can_unduck()) {
if (wall_jump_check(1)) { super_wall_jump(-1); return ST_NORMAL; }
if (wall_jump_check(-1)) { super_wall_jump(1); return ST_NORMAL; }
@@ -734,40 +751,44 @@ static int dash_update(void) {
}
return ST_DASH;
}
-static int co_dash(void) {
+static f32 unit(int s) { return s > 0 ? F_1 : s < 0 ? f_neg(F_1) : 0; }
+static int32_t co_dash(void) {
switch (P->co_step) {
case 0:
P->co_step = 1;
return 0;
case 1: {
- int dx = P->last_aim_x, dy = P->last_aim_y;
- int nx = dx * DashSpeed / 256, ny = dy * DashSpeed / 256;
- if (dx && dy) nx = isign(dx) * 10182, ny = isign(dy) * 10182; /* 240 * 0.70710677 */
- if (isign(P->before_dash_sx) == isign(nx) && iabs(P->before_dash_sx) > iabs(nx)) nx = P->before_dash_sx;
+ int ax = P->aim_x, ay = P->aim_y;
+ f32 nx, ny, dx, dy;
+ if (ax && ay) {
+ dx = ax > 0 ? F_Diag : f_neg(F_Diag), dy = ay > 0 ? F_Diag : f_neg(F_Diag);
+ nx = ax > 0 ? F_DashDiag : f_neg(F_DashDiag), ny = ay > 0 ? F_DashDiag : f_neg(F_DashDiag);
+ } else {
+ dx = unit(ax), dy = unit(ay);
+ nx = ftimes(F_DashSpeed, ax), ny = ftimes(F_DashSpeed, ay);
+ }
+ if (f_sign(P->before_dash_sx) == f_sign(nx) && f_lt(f_abs(nx), f_abs(P->before_dash_sx))) nx = P->before_dash_sx;
P->sx = nx, P->sy = ny;
P->dash_dx = dx, P->dash_dy = dy;
- level_dir_shake(isign(dx), isign(dy), TIM(.2f) / DTU);
- if (dx != 0) P->facing = isign(dx);
+ level_dir_shake(ax, ay, 12);
+ if (ax) P->facing = ax;
P->called_dash_events = true;
- if (P->on_ground && P->dash_dx != 0 && P->dash_dy > 0 && P->sy > 0) {
- P->dash_dx = isign(P->dash_dx) * 256;
+ { extern void ents_on_dash(void); ents_on_dash(); }
+ if (P->on_ground && !f_zero(P->dash_dx) && f_pos(P->dash_dy) && f_pos(P->sy)) {
+ P->dash_dx = unit(f_sign(P->dash_dx));
P->dash_dy = 0;
P->sy = 0;
- P->sx = P->sx * 6 / 5;
+ P->sx = f_mul(P->sx, F_DodgeSlideSpeedMult);
set_ducking(true);
}
P->co_step = 2;
- return DashTime;
+ return (int32_t)F_DashTime;
}
case 2:
P->auto_jump = true;
P->auto_jump_timer = 0;
- if (P->dash_dy <= 0) {
- int dx = P->dash_dx, dy = P->dash_dy;
- P->sx = dx * EndDashSpeed / 256, P->sy = dy * EndDashSpeed / 256;
- if (dx && dy) P->sx = isign(dx) * 6788, P->sy = isign(dy) * 6788; /* 160 * 0.70710677 */
- }
- if (P->sy < 0) P->sy = P->sy * 3 / 4; /* EndDashUpMult */
+ if (!f_pos(P->dash_dy)) P->sx = f_mul(P->dash_dx, F_EndDashSpeed), P->sy = f_mul(P->dash_dy, F_EndDashSpeed);
+ if (f_negative(P->sy)) P->sy = f_mul(P->sy, F_EndDashUpMult);
set_state(ST_NORMAL);
return -1;
}
@@ -776,76 +797,75 @@ static int co_dash(void) {
/* ---------------------------------------------------------------- intros */
static int intro_x, intro_y;
-static int co_intro_jump(void) {
+static int32_t co_intro_jump(void) {
switch (P->co_step) {
case 0:
intro_x = E->x, intro_y = E->y;
- E->depth = -1000000 / 64;
+ E->depth = -32000;
P->facing = 1;
E->y = g_level.room->y + g_level.room->h + 16;
P->co_step = 1;
- return TIM(.5f);
+ return (int32_t)F_Hop;
case 1:
play(A_player_jumpSlow);
P->co_step = 2;
/* fall through */
case 2:
- if (E->y > intro_y - 8) {
- E->remy -= SPD(120);
- int n = round_mu(E->remy, -1);
- E->y += n, E->remy -= n * MU;
+ if (E->y > intro_y - 8) { /* Y += -120 * DT (a float position: its rest is kept) */
+ P->intro_fy = f_add(P->intro_fy, F_IntroRise);
+ int n = f_trunc(P->intro_fy);
+ E->y += n, P->intro_fy = f_sub(P->intro_fy, f_int(n));
return 0;
}
- E->remy = 0;
- P->sy = SPD(-100);
+ P->sy = F_IntroJumpSpeed;
P->co_step = 3;
/* fall through */
case 3:
- if (P->sy < 0) {
- P->sy += 800;
+ if (f_negative(P->sy)) {
+ P->sy = f_add(P->sy, F_IntroGravity);
return 0;
}
P->sy = 0;
P->co_step = 4;
- return TIM(.1f);
+ return (int32_t)F_JumpGraceTime;
case 4:
play(A_player_fallSlow);
P->co_step = 5;
/* fall through */
case 5:
if (!P->on_ground) {
- P->sy += 800;
+ P->sy = f_add(P->sy, F_IntroGravity);
return 0;
}
E->x = intro_x, E->y = intro_y;
E->depth = 0;
- level_dir_shake(0, 1, TIM(.3f) / DTU);
+ level_dir_shake(0, 1, 18);
set_state(ST_NORMAL);
return -1;
}
return -1;
}
-static int co_intro_walk(void) {
+static int32_t co_intro_walk(void) {
switch (P->co_step) {
case 0:
intro_x = E->x, intro_y = E->y;
- if (P->facing > 0) E->x = g_level.room->x - 16;
- else E->x = g_level.room->x + g_level.room->w + 16;
+ E->x = P->facing > 0 ? g_level.room->x - 16 : g_level.room->x + g_level.room->w + 16;
P->co_step = 1;
- return TIM(.3f);
+ return (int32_t)F_SideBounceForce;
case 1:
play(A_player_runSlow);
P->co_step = 2;
/* fall through */
case 2:
- if (iabs(E->x - intro_x) > 2 && !solid_off(P->facing, 0)) {
- move_h(isign(intro_x - E->x) * SPD(64), NULL);
+ if (iabs(E->x - intro_x) > 2 && !solid_off(P->facing, 0)) { /* MoveTowardsX(start, 64 * DT) */
+ f32 at = f_add(f_int(E->x), E->remx), to = f_int(intro_x);
+ move_h(f_sub(f_approach(at, to, F_WalkIn), at), NULL);
return 0;
}
E->x = intro_x, E->y = intro_y;
play(A_player_idle);
P->co_step = 3;
- return TIM(.2f);
+ return (int32_t)F_ClimbHopForceTime;
default:
set_state(ST_NORMAL);
return -1;
@@ -857,24 +877,26 @@ static void respawn_begin(void) {
P->respawn_fromx = fx - E->x, P->respawn_fromy = fy - E->y;
P->intro_t = 0;
}
-static void respawn_end(void) {}
/* ---------------------------------------------------------------- death */
void player_die(int dx, int dy) {
if (P->dead) return;
+ extern void save_add_death(void), ents_lose_followers(void);
+ save_add_death();
+ ents_lose_followers();
P->dead = true;
P->sx = P->sy = 0;
E = P->e;
E->flags &= ~EF_COLLIDABLE;
E->depth = -32000;
- level_shake(TIM(.3f) / DTU);
+ level_shake(18);
P->body_step = 0;
P->body_dx = dx, P->body_dy = dy;
P->body_t = 0;
P->death_t = -1;
P->bodyx = E->x, P->bodyy = E->y;
P->co_active = false;
- btn_consume(&g_in.dash), btn_consume(&g_in.jump);
+ btn_consume_buffer(&g_in.dash), btn_consume_buffer(&g_in.jump);
}
/* PlayerDeadBody.DeathRoutine: a bounce back (0.5 s, stopped at 3/4), then the burst of hair, then the reload */
static void body_update(void) {
@@ -897,7 +919,7 @@ static void body_update(void) {
}
if (P->body_step == 3) {
E->y -= 5;
- level_shake(TIM(.3f) / DTU);
+ level_shake(18);
P->death_t = 0;
P->body_step = 4;
P->body_t = 0;
@@ -912,37 +934,34 @@ static void body_update(void) {
if (P->body_step == 5 && g_level.wipe >= 24) g_level.dead_reload = true;
}
-/* ---------------------------------------------------------------- the update */
+/* ---------------------------------------------------------------- the hair */
static void update_hair_color(void) {
uint32_t c;
- if (P->dashes == 0 && P->dashes < P->max_dashes) c = UsedHairColor;
- else if (P->last_dashes != P->dashes) c = FlashHairColor, P->hair_flash = TIM(.12f);
- else if (P->hair_flash > 0) c = FlashHairColor, P->hair_flash -= DTU;
- else c = P->dashes == 2 ? TwoDashesHairColor : NormalHairColor;
- if (P->dashes == 0 && P->dashes < P->max_dashes && P->hair_rgb != UsedHairColor) { /* Lerp to it, 6 a second */
+ if (P->dashes == 0 && P->dashes < P->max_dashes) { /* Lerp to the used colour, 6 a second */
uint32_t a = P->hair_rgb, b = UsedHairColor;
int r = (int)(a >> 16 & 255), g = (int)(a >> 8 & 255), bl = (int)(a & 255);
r += ((int)(b >> 16 & 255) - r) * 26 / 256, g += ((int)(b >> 8 & 255) - g) * 26 / 256, bl += ((int)(b & 255) - bl) * 26 / 256;
c = (uint32_t)r << 16 | (uint32_t)g << 8 | (uint32_t)bl;
- if (iabs(r - 0x44) < 4 && iabs(g - 0xB7) < 4 && iabs(bl - 0xFF) < 4) c = UsedHairColor;
- }
+ if (iabs(r - 0x44) < 6 && iabs(g - 0xB7) < 6 && iabs(bl - 0xFF) < 6) c = UsedHairColor;
+ } else if (P->last_dashes != P->dashes) c = FlashHairColor, P->hair_flash = TIM(.12f);
+ else if (P->hair_flash > 0) c = FlashHairColor, P->hair_flash -= DTU;
+ else c = P->dashes == 2 ? TwoDashesHairColor : NormalHairColor;
P->hair_rgb = c;
P->last_dashes = P->dashes;
}
-
-static void hair_update(void) {
+static void hair_update(void) { /* PlayerHair.AfterUpdate (1/256 px): the nodes follow, 3 px apart at most */
const uint8_t *m = player_meta(cur_sprite());
int fac = P->facing;
- /* PlayerHair.AfterUpdate: the first node at the head, the others following, 3 px apart at most */
- P->hairx[0] = (E->x + (int8_t)m[0] * fac) << 8;
- P->hairy[0] = (E->y - 9 * P->scale_y / 1024 + (int8_t)m[1]) << 8;
- int tx = P->hairx[0] - fac * 128, ty = P->hairy[0] + 512; /* the step: (-facing * 0.5, 2) */
+ P->hairx[0] = (E->x + (int8_t)m[0] * fac) * 256;
+ P->hairy[0] = (E->y - 9 * P->scale_y / 1024 + (int8_t)m[1]) * 256;
+ int tx = P->hairx[0] - fac * 128, ty = P->hairy[0] + 512;
for (int i = 1; i < P->hair_count; i++) {
int dx = tx - P->hairx[i], dy = ty - P->hairy[i];
+ if (iabs(dx) > 32 * 256 || iabs(dy) > 32 * 256) P->hairx[i] = tx, P->hairy[i] = ty, dx = dy = 0;
int l = isqrt((int32_t)dx * dx + (int32_t)dy * dy);
- int mv = (256 - i * 128 / P->hair_count) * 64 / 60; /* (1 - i / n / 2) * 64 px a second */
+ int mv = (256 - i * 128 / P->hair_count) * 64 / 60;
if (l <= mv) P->hairx[i] = tx, P->hairy[i] = ty;
- else if (l > 0) P->hairx[i] += (int)((int32_t)dx * mv / l), P->hairy[i] += (int)((int32_t)dy * mv / l);
+ else P->hairx[i] += (int)((int32_t)dx * mv / l), P->hairy[i] += (int)((int32_t)dy * mv / l);
dx = P->hairx[i] - P->hairx[i - 1], dy = P->hairy[i] - P->hairy[i - 1];
l = isqrt((int32_t)dx * dx + (int32_t)dy * dy);
if (l > 768) P->hairx[i] = P->hairx[i - 1] + (int)((int32_t)dx * 768 / l), P->hairy[i] = P->hairy[i - 1] + (int)((int32_t)dy * 768 / l);
@@ -950,12 +969,13 @@ static void hair_update(void) {
}
}
+/* ---------------------------------------------------------------- the sprite's animation */
static void update_sprite(void) {
P->scale_x = approach(P->scale_x, 1024, 30);
P->scale_y = approach(P->scale_y, 1024, 30);
if (!in_control()) return;
if (player_dash_attacking()) {
- if (P->on_ground && P->dash_dy == 0 && !Ducking && P->sx != 0 && P->move_x == -isign(P->sx)) play(A_player_skid);
+ if (P->on_ground && f_zero(P->dash_dy) && !Ducking && !f_zero(P->sx) && P->move_x == -f_sign(P->sx)) play(A_player_skid);
else play(A_player_dash);
} else if (P->state == ST_CLIMB) {
if (P->last_climb_move < 0) play(A_player_climbup);
@@ -970,7 +990,7 @@ static void update_sprite(void) {
} else if (P->on_ground) {
P->fast_jump = false;
if (P->move_x != 0 && solid_off(P->move_x, 0)) play(A_player_push);
- else if (iabs(P->sx) <= RunAccel / 40 * 60 / 60 && P->move_x == 0) {
+ else if (!f_lt(F_RunAccel40, f_abs(P->sx)) && P->move_x == 0) {
int f = P->facing;
if (!solid_at(E->x + f, E->y + 2, E->x + f + 1, E->y + 3, E) && !solid_at(E->x + f * 4, E->y + 2, E->x + f * 4 + 1, E->y + 3, E) &&
!jumpthru_first_outside(E, E->x + f * 4, E->y + 2))
@@ -982,24 +1002,24 @@ static void update_sprite(void) {
if (P->anim != A_player_lookUp) play(A_player_lookUp);
} else if (P->anim != A_player_idle && P->anim != A_player_idleA && P->anim != A_player_idleB && P->anim != A_player_idleC)
play(A_player_idle);
- } else if (isign(P->sx) == -P->move_x && P->move_x != 0) {
- if (iabs(P->sx) > MaxRun) play(A_player_skid);
+ } else if (f_sign(P->sx) == -P->move_x && P->move_x != 0) {
+ if (f_lt(F_MaxRun, f_abs(P->sx))) play(A_player_skid);
else if (P->anim != A_player_skid) play(A_player_flip);
} else if (P->anim != A_player_flip && P->anim != A_player_runSlow && P->anim != A_player_runFast &&
P->anim != A_player_runStumble) {
- if (iabs(P->sx) < MaxRun / 2) play(A_player_runSlow);
+ if (f_lt(f_abs(P->sx), f_mul(F_MaxRun, F_HALF))) play(A_player_runSlow);
else play(A_player_runFast);
}
} else if (P->wall_slide_dir != 0) {
play(A_player_wallslide);
- } else if (P->sy < 0) {
- if (P->fast_jump || iabs(P->sx) > MaxRun) {
+ } else if (f_negative(P->sy)) {
+ if (P->fast_jump || f_lt(F_MaxRun, f_abs(P->sx))) {
P->fast_jump = true;
play(A_player_jumpFast);
} else
play(A_player_jumpSlow);
} else {
- if (P->fast_jump || P->sy >= MaxFall) {
+ if (P->fast_jump || !f_lt(P->sy, F_MaxFall)) {
P->fast_jump = true;
play(A_player_fallFast);
} else
@@ -1007,6 +1027,7 @@ static void update_sprite(void) {
}
}
+/* ---------------------------------------------------------------- the update */
void player_update(void) {
E = P->e;
if (P->dead) {
@@ -1014,10 +1035,10 @@ void player_update(void) {
if (P->body_step < 4) sprite_update();
return;
}
- P->idle_timer += DTU;
+ P->idle_timer++;
if (P->sx || P->sy) P->idle_timer = 0;
- if (P->strawb_reset > 0 && (P->strawb_reset -= DTU) <= 0) P->strawb_index = 0;
- if (P->sy >= 0) {
+ if (f_pos(P->strawb_reset) && !f_pos(P->strawb_reset = dec(P->strawb_reset))) P->strawb_index = 0;
+ if (!f_negative(P->sy)) {
Ent *first = solid_hit_off(0, 1);
if (!first) first = jumpthru_first_outside(E, E->x, E->y + 1);
P->on_ground = first != NULL;
@@ -1026,27 +1047,27 @@ void player_update(void) {
P->on_ground = P->on_safe_ground = false;
if (g_level.frame % 6 == 0) P->flash = !P->flash; /* (OnInterval .05) */
if (P->wall_slide_dir != 0) {
- P->wall_slide_timer = imax(P->wall_slide_timer - DTU, 0);
+ P->wall_slide_timer = f_max(dec(P->wall_slide_timer), 0);
P->wall_slide_dir = 0;
}
- if (P->wall_boost_timer > 0) {
- P->wall_boost_timer -= DTU;
+ if (f_pos(P->wall_boost_timer)) {
+ P->wall_boost_timer = dec(P->wall_boost_timer);
if (P->move_x == P->wall_boost_dir) {
- P->sx = WallJumpHSpeed * P->move_x;
- P->stamina += ClimbJumpCost;
+ P->sx = ftimes(F_WallJumpHSpeed, P->move_x);
+ P->stamina = f_add(P->stamina, F_ClimbJumpCost);
P->wall_boost_timer = 0;
}
}
if (P->on_ground && P->state != ST_CLIMB) {
P->auto_jump = false;
- P->stamina = ClimbMaxStamina;
- P->wall_slide_timer = WallSlideTime;
+ P->stamina = F_ClimbMaxStamina;
+ P->wall_slide_timer = F_WallSlideTime;
}
- if (P->dash_attack > 0) P->dash_attack -= DTU;
- if (P->on_ground) P->jump_grace = JumpGraceTime;
- else if (P->jump_grace > 0) P->jump_grace -= DTU;
- if (P->dash_cooldown > 0) P->dash_cooldown -= DTU;
- if (P->dash_refill_cooldown > 0) P->dash_refill_cooldown -= DTU;
+ if (f_pos(P->dash_attack)) P->dash_attack = dec(P->dash_attack);
+ if (P->on_ground) P->jump_grace = F_JumpGraceTime;
+ else if (f_pos(P->jump_grace)) P->jump_grace = dec(P->jump_grace);
+ if (f_pos(P->dash_cooldown)) P->dash_cooldown = dec(P->dash_cooldown);
+ if (f_pos(P->dash_refill_cooldown)) P->dash_refill_cooldown = dec(P->dash_refill_cooldown);
else if (P->on_ground && (solid_off(0, 1) || jumpthru_off(0, 1))) {
bool on_spikes = false; /* (CollideCheck<Spikes>) */
for (int i = 0; i < g_nents && !on_spikes; i++) {
@@ -1055,42 +1076,50 @@ void player_update(void) {
}
if (!on_spikes) player_refill_dash();
}
- if (P->var_jump > 0) P->var_jump -= DTU;
- if (P->auto_jump_timer > 0) {
+ if (f_pos(P->var_jump)) P->var_jump = dec(P->var_jump);
+ if (f_pos(P->auto_jump_timer)) {
if (P->auto_jump) {
- P->auto_jump_timer -= DTU;
- if (P->auto_jump_timer <= 0) P->auto_jump = false;
+ P->auto_jump_timer = dec(P->auto_jump_timer);
+ if (!f_pos(P->auto_jump_timer)) P->auto_jump = false;
} else
P->auto_jump_timer = 0;
}
- if (P->force_move_x_timer > 0) {
- P->force_move_x_timer -= DTU;
+ if (f_pos(P->force_move_x_timer)) {
+ P->force_move_x_timer = dec(P->force_move_x_timer);
P->move_x = P->force_move_x;
- } else
+ } else {
P->move_x = g_in.move_x;
+ P->climb_hop_solid = NULL;
+ }
+ if (P->climb_hop_solid && !(P->climb_hop_solid->flags & EF_COLLIDABLE)) P->climb_hop_solid = NULL;
+ else if (P->climb_hop_solid && (P->climb_hop_solid->x != P->climb_hop_x || P->climb_hop_solid->y != P->climb_hop_y)) {
+ int mx = P->climb_hop_solid->x - P->climb_hop_x, my = P->climb_hop_solid->y - P->climb_hop_y;
+ P->climb_hop_x = P->climb_hop_solid->x, P->climb_hop_y = P->climb_hop_solid->y;
+ move_h_exact(mx, NULL);
+ move_v_exact(my, NULL);
+ }
if (P->move_x != 0 && in_control() && P->state != ST_CLIMB) {
if (P->move_x != P->facing && Ducking) P->scale_x = 819, P->scale_y = 1229;
P->facing = P->move_x;
}
- aim(&P->last_aim_x, &P->last_aim_y);
- if (P->wall_speed_retention > 0) {
- if (isign(P->sx) == -isign(P->wall_speed_retained)) P->wall_speed_retention = 0;
- else if (!solid_off(isign(P->wall_speed_retained), 0)) {
+ aim();
+ if (f_pos(P->wall_speed_retention)) {
+ if (f_sign(P->sx) == -f_sign(P->wall_speed_retained)) P->wall_speed_retention = 0;
+ else if (!solid_off(f_sign(P->wall_speed_retained), 0)) {
P->sx = P->wall_speed_retained;
P->wall_speed_retention = 0;
} else
- P->wall_speed_retention -= DTU;
+ P->wall_speed_retention = dec(P->wall_speed_retention);
}
if (P->hop_wait_x != 0) {
- if (isign(P->sx) == -P->hop_wait_x || P->sy > 0) P->hop_wait_x = 0;
+ if (f_sign(P->sx) == -P->hop_wait_x || f_pos(P->sy)) P->hop_wait_x = 0;
else if (!solid_off(P->hop_wait_x, 0)) {
P->sx = P->hop_wait_x_speed;
P->hop_wait_x = 0;
}
}
P->hair_count = P->dashes > 1 ? 5 : 4;
- /* components: the hair's wave, the sprite, the state machine, then the actor's lift */
- P->hair_wave += 4;
+ /* components: the sprite, the state machine, then the actor's lift */
sprite_update();
if (P->state == ST_INTRORESPAWN) { /* the respawn's tween: 0.6 s */
P->intro_t += DTU;
@@ -1099,89 +1128,93 @@ void player_update(void) {
P->scale_x = 1536, P->scale_y = 512;
}
}
- if (P->state < 3 && UPD[P->state]) set_state(UPD[P->state]());
+ if (P->state < 3) set_state(UPD[P->state]());
co_update();
- /* the lift speed: kept 0.16 s */
- if (E->liftx || E->lifty) P->last_liftx = E->liftx, P->last_lifty = E->lifty, P->lift_timer = TIM(.16f);
- E->liftx = E->lifty = 0;
- if (P->lift_timer > 0 && (P->lift_timer -= DTU) <= 0) P->last_liftx = P->last_lifty = 0;
- if (!P->on_ground && P->sy <= 0 && (P->state != ST_CLIMB || P->last_climb_move == -1)) { /* up through jump-throughs */
+ E->liftx = E->lifty = 0; /* (LiftSpeed: the moves of this frame set it again) */
+ if (f_pos(P->lift_timer) && !f_pos(P->lift_timer = dec(P->lift_timer))) P->last_liftx = P->last_lifty = 0;
+ if (!P->on_ground && !f_pos(P->sy) && (P->state != ST_CLIMB || P->last_climb_move == -1)) { /* up through jump-throughs */
bool in = false;
for (int i = 0; i < g_nents && !in; i++) {
Ent *o = &g_ents[i];
in = (o->flags & (EF_JUMPTHRU | EF_COLLIDABLE)) == (EF_JUMPTHRU | EF_COLLIDABLE) && ent_collide_at(E, E->x, E->y, o);
}
- if (in && !ledge_blocked(E->x, E->y - 2)) move_v(JumpThruAssistSpeed, NULL);
+ if (in && !ledge_blocked(E->x, E->y - 2)) move_v(f_mul(F_JumpThruAssistSpeed, DT), NULL);
}
- if (!P->on_ground && player_dash_attacking() && P->dash_dy == 0)
- if (solid_off(0, DashVFloorSnapDist) || jumpthru_off(0, DashVFloorSnapDist)) move_v_exact(DashVFloorSnapDist, NULL);
- if (P->sy > 0 && can_unduck() && !P->on_ground) set_ducking(false);
+ if (!P->on_ground && player_dash_attacking() && f_zero(P->dash_dy))
+ if (solid_off(0, 3) || jumpthru_off(0, 3)) move_v_exact(3, NULL); /* DashVFloorSnapDist */
+ if (f_pos(P->sy) && can_unduck() && !P->on_ground) set_ducking(false);
if (P->state != ST_INTROJUMP || P->co_step >= 3) {
- move_h(P->sx, on_collide_h);
- move_v(P->sy, on_collide_v);
+ move_h(f_mul(P->sx, DT), on_collide_h);
+ move_v(f_mul(P->sy, DT), on_collide_v);
}
if (P->dead) return;
update_sprite();
- if (in_control() && P->state != ST_INTROJUMP) { /* the camera: 1 - 0.01 ^ dt of the way */
- int tx = player_cam_targetx() << 8, ty = player_cam_targety() << 8;
+ if (in_control()) { /* the camera: 1 - 0.01 ^ dt of the way */
+ int tx = player_cam_targetx() * 256, ty = player_cam_targety() * 256;
g_level.camx += (int)(((int32_t)(tx - g_level.camx) * 1210) >> 14);
g_level.camy += (int)(((int32_t)(ty - g_level.camy) * 1210) >> 14);
}
/* what she touches (PlayerCollider), with the hurt box */
- if (!P->dead) {
- int8_t was[4] = {(int8_t)E->hw, (int8_t)E->hh, (int8_t)E->hx, (int8_t)E->hy};
- set_box(Ducking ? HB_DUCK_HURT : HB_NORMAL_HURT);
- extern void ents_player_touch(Ent *p);
- ents_player_touch(E);
- if (P->dead) return;
- if (box_is(HB_DUCK_HURT) || box_is(HB_NORMAL_HURT)) set_box(was);
- }
+ int8_t was[4];
+ save_box(was);
+ set_box(Ducking ? HB_DUCK_HURT : HB_NORMAL_HURT);
+ extern void ents_player_touch(Ent *p);
+ ents_player_touch(E);
+ if (P->dead) return;
+ if (box_is(HB_DUCK_HURT) || box_is(HB_NORMAL_HURT)) set_box(was);
update_hair_color();
P->was_on_ground = P->on_ground;
}
/* ---------------------------------------------------------------- camera and transitions */
+extern int g_anchor_x, g_anchor_y, g_anchor_lerp;
+extern uint8_t g_anchor_ignore;
+static int cam_target(int px, int off, int anchor, bool ignore, int lo, int hi) {
+ int t = px + off;
+ if (g_anchor_lerp > 0 && !ignore) t += (int)((int32_t)(anchor - t) * g_anchor_lerp / 256);
+ return iclamp(t, lo, hi);
+}
int player_cam_targetx(void) {
Room *rm = g_level.room;
- return iclamp(P->e->x - 160 + g_level.cam_offx, rm->x, rm->x + rm->w - 320);
+ return cam_target(P->e->x - 160, g_level.cam_offx, g_anchor_x, g_anchor_ignore & 2, rm->x, rm->x + rm->w - 320);
}
int player_cam_targety(void) {
Room *rm = g_level.room;
- return iclamp(P->e->y - 90 + g_level.cam_offy, rm->y, rm->y + rm->h - 180);
+ return cam_target(P->e->y - 90, g_level.cam_offy, g_anchor_y, g_anchor_ignore & 1, rm->y, rm->y + rm->h - 180);
}
bool player_transition_to(int x, int y) {
E = P->e;
- /* MoveTowardsX/Y: 60 px a second */
- int cx = E->x * MU + E->remx, cy = E->y * MU + E->remy;
- move_h(approach(cx, x * MU, SPD(60)) - cx, NULL);
- move_v(approach(cy, y * MU, SPD(60)) - cy, NULL);
+ f32 cx = f_add(f_int(E->x), E->remx), cy = f_add(f_int(E->y), E->remy); /* MoveTowardsX/Y: 60 px a second */
+ move_h(f_sub(f_approach(cx, f_int(x), F_TransitionSpeed), cx), NULL);
+ cy = f_add(f_int(E->y), E->remy);
+ move_v(f_sub(f_approach(cy, f_int(y), F_TransitionSpeed), cy), NULL);
hair_update();
if (E->x == x && E->y == y) {
E->remx = E->remy = 0;
- P->sx = P->sx / 60 * 60, P->sy = P->sy / 60 * 60; /* Speed rounded to whole pixels a second */
+ P->sx = f_int(f_round(P->sx)), P->sy = f_int(f_round(P->sy));
return true;
}
return false;
}
void player_on_transition(void) {
- P->wall_slide_timer = WallSlideTime;
+ P->wall_slide_timer = F_WallSlideTime;
P->jump_grace = 0;
P->force_move_x_timer = 0;
player_refill_dash();
- P->stamina = ClimbMaxStamina;
+ P->stamina = F_ClimbMaxStamina;
}
void player_before_up_transition(void) {
P->sx = 0;
- P->var_jump_speed = P->sy = JumpSpeed;
+ P->var_jump_speed = P->sy = F_JumpSpeed;
set_state(ST_NORMAL);
P->auto_jump = true;
P->auto_jump_timer = 0;
- P->var_jump = VarJumpTime;
- P->dash_cooldown = TIM(.2f);
+ P->var_jump = F_VarJumpTime;
+ P->dash_cooldown = F_DashCooldown;
}
void player_before_down_transition(void) {
set_state(ST_NORMAL);
- P->sy = imax(0, P->sy);
+ P->sy = f_max(0, P->sy);
P->auto_jump = false;
P->var_jump = 0;
}
@@ -1198,11 +1231,11 @@ static void hair_draw(bool outline) {
int cy = (i == 0 ? P->hairy[0] >> 8 : (P->hairy[i] + 128) >> 8) - g_camy;
bool flip = i == 0 && P->facing < 0;
int sx = (i == 0 ? 256 : scale) * P->scale_x / 1024, sy = i == 0 ? 256 : scale;
- /* the 10 x 10 picture, its centre (5, 5) at (cx, cy) */
+ if (sx < 16) sx = 16;
int x0 = cx - (5 * sx + 128) / 256, x1 = cx + (5 * sx + 128) / 256, y0 = cy - (5 * sy + 128) / 256, y1 = cy + (5 * sy + 128) / 256;
for (int y = y0; y < y1; y++)
for (int x = x0; x < x1; x++) {
- int u = 5 + ((x - cx) * 256 + 128) / sx, v = 5 + ((y - cy) * 256 + 128) / sy;
+ int u = 5 + fdiv((x - cx) * 256 + 128, sx), v = 5 + fdiv((y - cy) * 256 + 128, sy);
if ((unsigned)u >= 10 || (unsigned)v >= 10) continue;
if (flip) u = 9 - u;
if (!(mask[v] >> u & 1)) continue;
@@ -1213,7 +1246,7 @@ static void hair_draw(bool outline) {
if ((unsigned)px < VIEW_W && (unsigned)py < VIEW_H) g_fb[(VIEW_Y + py) * 320 + px] = BLACK;
}
} else if ((unsigned)x < VIEW_W && (unsigned)y < VIEW_H)
- g_fb[(VIEW_Y + y) * 320 + x] = mask[10 + v] >> u & 1 ? 2 : 1;
+ g_fb[(VIEW_Y + y) * 320 + x] = mask[10 + v] >> u & 1 ? HAIR_DARK : HAIR;
}
}
}
@@ -1222,27 +1255,20 @@ static void death_effect(int x, int y, int t) { /* DeathEffect: 8 hair circles
int e = 256 - (int)((int32_t)(256 - t) * (256 - t) / 256 * (256 - t) / 256);
int s = t < 128 ? 128 + t : (256 - (int)((int32_t)(t * 2 - 256) * (t * 2 - 256) / 256 * (t * 2 - 256) / 256));
int r = 4 * s / 256;
- uint8_t col = (t * 10 / 256) % 2 == 0 ? 1 : WHITE;
- for (int i = 0; i < 8; i++) {
- int a = i * 128 + t / 4;
- int px = x + sin256(a + 256) * 24 * e / 65536, py = y + sin256(a) * 24 * e / 65536;
- draw_circle_fill(px, py, r + 1, BLACK);
- }
- for (int i = 0; i < 8; i++) {
- int a = i * 128 + t / 4;
- int px = x + sin256(a + 256) * 24 * e / 65536, py = y + sin256(a) * 24 * e / 65536;
- draw_circle_fill(px, py, r, col);
- }
+ uint8_t col = (t * 10 / 256) % 2 == 0 ? HAIR : WHITE;
+ for (int pass = 0; pass < 2; pass++)
+ for (int i = 0; i < 8; i++) {
+ int a = i * 128 + t / 4;
+ int px = x + (int)((int32_t)sin15(a + 256) * 24 * e / 8388608), py = y + (int)((int32_t)sin15(a) * 24 * e / 8388608);
+ draw_circle_fill(px, py, pass ? r : r + 1, pass ? col : BLACK);
+ }
}
void player_render(void) {
E = P->e;
if (P->dead) {
- if (P->body_step < 4) {
- const uint8_t *s = sprite(cur_sprite());
- draw_sprite_scaled(s, E->x, E->y, 256, 256, 16, 32, P->facing < 0 ? DF_FLIPX : 0);
- } else if (P->death_t >= 0 && P->death_t <= TIM(.834f))
- death_effect(E->x, E->y - 6, P->death_t * 256 / TIM(.834f));
+ if (P->body_step < 4) draw_sprite_scaled(sprite(cur_sprite()), E->x, E->y, 256, 256, 16, 32, P->facing < 0 ? DF_FLIPX : 0);
+ else if (P->death_t >= 0 && P->death_t <= TIM(.834f)) death_effect(E->x, E->y - 6, P->death_t * 256 / TIM(.834f));
return;
}
palette_hair(P->hair_rgb);
@@ -1253,10 +1279,11 @@ void player_render(void) {
}
hair_draw(true);
hair_draw(false);
- const uint8_t *s = sprite(cur_sprite());
- bool red = is_tired() && P->flash;
- if (red) g_draw_color = 1;
- draw_sprite_scaled(s, E->x, E->y, P->scale_x / 4, P->scale_y / 4, 16, 32, (P->facing < 0 ? DF_FLIPX : 0));
+ extern const uint8_t *g_remap;
+ extern const uint8_t *g_remap_red;
+ if (is_tired() && P->flash) g_remap = g_remap_red; /* (Color.Red: tired) */
+ draw_sprite_scaled(sprite(cur_sprite()), E->x, E->y, P->scale_x / 4, P->scale_y / 4, 16, 32, P->facing < 0 ? DF_FLIPX : 0);
+ g_remap = NULL;
}
void player_spawn(int x, int y, int intro) {
@@ -1270,12 +1297,13 @@ void player_spawn(int x, int y, int intro) {
P->facing = 1;
P->max_dashes = 1;
P->dashes = P->last_dashes = 1;
- P->stamina = ClimbMaxStamina;
- P->wall_slide_timer = WallSlideTime;
- P->max_fall = MaxFall;
+ P->stamina = F_ClimbMaxStamina;
+ P->wall_slide_timer = F_WallSlideTime;
+ P->max_fall = F_MaxFall;
P->scale_x = P->scale_y = 1024;
P->hair_rgb = NormalHairColor;
P->hair_count = 4;
+ P->aim_x = 1;
P->anim = -1;
play(A_player_idle);
P->state = -1;
@@ -1287,7 +1315,7 @@ void player_spawn(int x, int y, int intro) {
case INTRO_WALKINLEFT: P->facing = -1, set_state(ST_INTROWALK); break;
default: set_state(ST_NORMAL); break;
}
- for (int i = 0; i < HAIR_NODES; i++) P->hairx[i] = (x - P->facing * 2) << 8, P->hairy[i] = (y - 10) << 8;
+ for (int i = 0; i < HAIR_NODES; i++) P->hairx[i] = (x - P->facing * 2) * 256, P->hairy[i] = (y - 10) * 256;
hair_update();
}
diff --git a/games/celeste/ce/src/sf.c b/games/celeste/ce/src/sf.c
new file mode 100644
index 00000000..b2988cdb
--- /dev/null
+++ b/games/celeste/ce/src/sf.c
@@ -0,0 +1,151 @@
+/* Single-precision floats done with whole numbers, IEEE 754 (round to nearest, ties to even), the same on the CE and
+ * on a computer: Celeste's movement is float arithmetic, and its pixel-exact paths come from how each operation rounds.
+ * Finite numbers only; results too small for a normal float are 0 (the game's never are). tests/sf.c checks it against
+ * the computer's own floats. */
+#include "ce.h"
+
+#define SIGN 0x80000000ul
+static inline int fexp(f32 a) { return (int)(a >> 23 & 255); }
+static inline uint32_t fman(f32 a) { return a & 0x7FFFFFul; }
+
+/* m: the significand with its top bit at bit 26 (3 bits below the float's last: guard, round, sticky), e the exponent */
+static f32 pack(uint32_t sign, int e, uint32_t m) {
+ if (!m) return sign;
+ while (m >= 1ul << 27) m = m >> 1 | (m & 1), e++; /* (sticky) */
+ while (m < 1ul << 26) m <<= 1, e--;
+ if (e <= 0) return sign; /* too small */
+ uint32_t low = m & 7;
+ m >>= 3;
+ if (low > 4 || (low == 4 && (m & 1))) {
+ m++;
+ if (m >= 1ul << 24) m >>= 1, e++;
+ }
+ if (e >= 255) return sign | 0x7F800000ul;
+ return sign | (uint32_t)e << 23 | (m & 0x7FFFFFul);
+}
+
+f32 f_add(f32 a, f32 b) {
+ if (!(a & ~SIGN)) return (b & ~SIGN) ? b : (a & b); /* 0 + b; -0 + -0 = -0 */
+ if (!(b & ~SIGN)) return a;
+ int ea = fexp(a), eb = fexp(b);
+ if (!ea) return b; /* (no denormals) */
+ if (!eb) return a;
+ uint32_t ma = (fman(a) | 0x800000ul) << 3, mb = (fman(b) | 0x800000ul) << 3;
+ if (ea < eb || (ea == eb && ma < mb)) {
+ f32 t = a;
+ a = b, b = t;
+ uint32_t u = ma;
+ ma = mb, mb = u;
+ int v = ea;
+ ea = eb, eb = v;
+ }
+ int d = ea - eb;
+ if (d > 27) mb = 1; /* only the sticky bit */
+ else if (d) {
+ uint32_t lost = mb & ((1ul << d) - 1);
+ mb = mb >> d | (lost != 0);
+ }
+ if ((a ^ b) & SIGN) {
+ uint32_t m = ma - mb;
+ if (!m) return 0;
+ return pack(a & SIGN, ea, m);
+ }
+ return pack(a & SIGN, ea, ma + mb);
+}
+f32 f_sub(f32 a, f32 b) { return f_add(a, b ^ SIGN); }
+
+f32 f_mul(f32 a, f32 b) {
+ uint32_t sign = (a ^ b) & SIGN;
+ int ea = fexp(a), eb = fexp(b);
+ if (!ea || !eb) return sign;
+ uint32_t ma = fman(a) | 0x800000ul, mb = fman(b) | 0x800000ul;
+ /* the 48-bit product from 12-bit halves */
+ uint32_t ah = ma >> 12, al = ma & 0xFFF, bh = mb >> 12, bl = mb & 0xFFF;
+ uint32_t hh = ah * bh, hl = ah * bl + al * bh, ll = al * bl;
+ /* product = hh << 24 + hl << 12 + ll */
+ uint32_t lo = ll + ((hl & 0xFFF) << 12);
+ uint32_t hi = hh + (hl >> 12) + (lo >> 24);
+ lo &= 0xFFFFFF;
+ /* the product's top bit is bit 47 or 46 of hi:lo (hi bits 23 or 22); keep 27 bits with sticky */
+ int e = ea + eb - 127;
+ uint32_t m;
+ if (hi & 0x800000ul) { /* 48 bits: shift down 21 */
+ m = hi << 3 | lo >> 21;
+ m |= (lo & 0x1FFFFF) != 0;
+ e++;
+ } else {
+ m = hi << 4 | lo >> 20;
+ m |= (lo & 0xFFFFF) != 0;
+ }
+ return pack(sign, e, m);
+}
+
+f32 f_div(f32 a, f32 b) {
+ uint32_t sign = (a ^ b) & SIGN;
+ int ea = fexp(a), eb = fexp(b);
+ if (!ea) return sign;
+ if (!eb) return sign | 0x7F800000ul;
+ uint32_t ma = fman(a) | 0x800000ul, mb = fman(b) | 0x800000ul;
+ int e = ea - eb + 127;
+ if (ma < mb) ma <<= 1, e--;
+ /* 27 quotient bits, a bit at a time, the rest as sticky */
+ uint32_t q = 0, r = ma;
+ for (int i = 0; i < 27; i++) {
+ q <<= 1;
+ if (r >= mb) r -= mb, q |= 1;
+ r <<= 1;
+ }
+ q |= r != 0;
+ return pack(sign, e, q);
+}
+
+/* comparisons: the bits in order (zeros equal) */
+static inline int32_t key(f32 a) { return (a & SIGN) ? -(int32_t)(a & ~SIGN) : (int32_t)a; }
+bool f_lt(f32 a, f32 b) { return key(a) < key(b); }
+bool f_le(f32 a, f32 b) { return key(a) <= key(b); }
+
+f32 f_int(int v) { /* exact below 2^24 */
+ if (!v) return 0;
+ uint32_t sign = v < 0 ? SIGN : 0;
+ uint32_t m = (uint32_t)(v < 0 ? -v : v);
+ return pack(sign, 150 + 3 - 3, m << 3);
+}
+int f_trunc(f32 a) { /* toward 0 */
+ int e = fexp(a) - 127;
+ if (e < 0) return 0;
+ uint32_t m = fman(a) | 0x800000ul;
+ int v = e >= 23 ? (int)(m << (e - 23)) : (int)(m >> (23 - e));
+ return (a & SIGN) ? -v : v;
+}
+int f_floor(f32 a) {
+ int t = f_trunc(a);
+ if ((a & SIGN) && f_lt(a, f_int(t))) t--;
+ return t;
+}
+int f_round(f32 a) { /* Math.Round: to even */
+ int e = fexp(a) - 127;
+ if (e < -1) return 0;
+ uint32_t m = fman(a) | 0x800000ul; /* value = m * 2^(e - 23) */
+ int v;
+ if (e >= 23) v = (int)(m << (e - 23));
+ else {
+ int sh = 23 - e;
+ uint32_t q = m >> sh, rem = m & ((1ul << sh) - 1), half = 1ul << (sh - 1);
+ if (rem > half || (rem == half && (q & 1))) q++;
+ v = (int)q;
+ }
+ return (a & SIGN) ? -v : v;
+}
+f32 f_approach(f32 v, f32 t, f32 d) { /* Calc.Approach */
+ if (f_le(v, t)) {
+ f32 s = f_add(v, d);
+ return f_lt(s, t) ? s : t;
+ }
+ f32 s = f_sub(v, d);
+ return f_lt(t, s) ? s : t;
+}
+f32 f_min(f32 a, f32 b) { return f_lt(b, a) ? b : a; }
+f32 f_max(f32 a, f32 b) { return f_lt(a, b) ? b : a; }
+f32 f_abs(f32 a) { return a & ~SIGN; }
+int f_sign(f32 a) { return (a & ~SIGN) ? ((a & SIGN) ? -1 : 1) : 0; }
+f32 f_lerp(f32 a, f32 b, f32 t) { return f_add(a, f_mul(f_sub(b, a), t)); }
diff --git a/games/celeste/ce/src/util.c b/games/celeste/ce/src/util.c
new file mode 100644
index 00000000..12bb0e22
--- /dev/null
+++ b/games/celeste/ce/src/util.c
@@ -0,0 +1,101 @@
+/* Whole-number math, randomness and the buttons. */
+#include "ce.h"
+
+int fdiv(int a, int b) {
+ int q = a / b;
+ return (a % b != 0 && a < 0) ? q - 1 : q;
+}
+
+/* Math.Round(movementCounter): the counter c (MU) in whole pixels. Celeste's frame is 0.0166667 s, a hair longer than
+ * 1/60, so what moves goes that much further: the float counter is ahead of the exact one by a little more than a
+ * millionth of all it moved since it was last zeroed. A counter exactly between two pixels (which whole numbers hit and
+ * floats almost never do) rounds the way that sum goes (dir: it; 0: to even, as .NET does). */
+int round_mu(int c, int dir) {
+ int n = fdiv(c + MU / 2, MU); /* half up */
+ if ((c + MU / 2) - n * MU == 0) { /* exactly halfway between n - 1 and n */
+ if (dir < 0) n--;
+ else if (dir == 0 && (n & 1)) n--;
+ }
+ return n;
+}
+
+int bias_add(int b, int amount) {
+ b += amount;
+ return b > 4000000 ? 4000000 : b < -4000000 ? -4000000 : b;
+}
+
+int isqrt(int32_t v) {
+ if (v <= 0) return 0;
+ uint32_t r = 0, b = 1ul << 30, x = (uint32_t)v;
+ while (b > x) b >>= 2;
+ while (b) {
+ if (x >= r + b) x -= r + b, r = (r >> 1) + b;
+ else r >>= 1;
+ b >>= 2;
+ }
+ return (int)r;
+}
+
+/* sin over a quarter turn (256 steps), 32767 at the top */
+static const uint16_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};
+int sin15(int a) {
+ a &= 1023;
+ if (a < 256) return SIN[a];
+ if (a < 512) return SIN[512 - a];
+ if (a < 768) return -(int)SIN[a - 512];
+ return -(int)SIN[1024 - a];
+}
+
+static uint32_t seed = 0x2F6E2B1u;
+uint32_t rnd(void) { /* xorshift */
+ seed ^= seed << 13, seed ^= seed >> 17, seed ^= seed << 5;
+ return seed;
+}
+
+/* ---------------------------------------------------------------- the buttons: Celeste's 0.08 s press buffer */
+Input g_in;
+static int last_x, last_y;
+static void button(Button *b, bool down, bool was) {
+ b->consumed = false;
+ if (b->buffer > 0) b->buffer = (int8_t)(b->buffer - DTU);
+ b->edge = down && !was;
+ b->check = down;
+ if (b->edge) b->buffer = (int8_t)TIM(.08f);
+ if (!down) b->buffer = 0;
+}
+void input_update(unsigned keys) {
+ unsigned prev = g_in.keys;
+ g_in.prev = prev;
+ g_in.keys = keys;
+ button(&g_in.jump, keys & K_JUMP, prev & K_JUMP);
+ button(&g_in.dash, keys & K_DASH, prev & K_DASH);
+ button(&g_in.grab, keys & K_GRAB, prev & K_GRAB);
+ button(&g_in.pause, keys & K_PAUSE, prev & K_PAUSE);
+ button(&g_in.ok, keys & K_OK, prev & K_OK);
+ button(&g_in.back, keys & K_BACK, prev & K_BACK);
+ button(&g_in.talk, keys & K_TALK, prev & K_TALK);
+ bool l = keys & K_LEFT, r = keys & K_RIGHT, u = keys & K_UP, d = keys & K_DOWN;
+ if (l && !(prev & K_LEFT)) last_x = -1;
+ if (r && !(prev & K_RIGHT)) last_x = 1;
+ if (u && !(prev & K_UP)) last_y = -1;
+ if (d && !(prev & K_DOWN)) last_y = 1;
+ g_in.move_x = l && r ? last_x : l ? -1 : r ? 1 : 0;
+ g_in.move_y = u && d ? last_y : u ? -1 : d ? 1 : 0;
+ g_in.aim_x = g_in.move_x, g_in.aim_y = g_in.move_y;
+}
diff --git a/games/celeste/ce/tests/physics.py b/games/celeste/ce/tests/physics.py
new file mode 100644
index 00000000..b16c843b
--- /dev/null
+++ b/games/celeste/ce/tests/physics.py
@@ -0,0 +1,100 @@
+#!/usr/bin/env python3
+"""Madeline on the CE against the NumWorks port (whose floats are Celeste's): the same keys in the same rooms, and
+every frame's position compared.
+
+ python3 tests/physics.py CE_HOST NUMWORKS_PLAY [--runs N] [--frames N] [--seed S] [-v]
+
+CE_HOST is ce/build/host/celeste, NUMWORKS_PLAY games/celeste/build/play (make host in each). Random key runs,
+held for a few frames each, in rooms of Forsaken City; a run stops being compared when Madeline dies or leaves the
+room (the CE version's rooms then differ). Prints how many frames matched."""
+import os
+import random
+import subprocess
+import sys
+
+HERE = os.path.dirname(os.path.abspath(__file__))
+CE_DIR = os.path.join(HERE, "..")
+NW_DATA = os.path.join(HERE, "..", "..", "src", "data.bin")
+# (room, x, y): Madeline's start in rooms with walls, ledges and room to move (no moving blocks)
+ROOMS = ["1", "2", "3", "4", "3b", "5", "6", "6a", "6b", "6c", "7", "8", "8b", "9", "9b", "10a", "11", "12", "12a"]
+KEYS = ["", "r", "l", "rj", "lj", "j", "x", "rx", "lx", "ux", "urx", "ulx", "dx", "drx", "rg", "lg", "rgu", "lgu", "g",
+ "gu", "gd", "d", "rd", "rjx"]
+
+
+def ce_keys(seq):
+ out = []
+ for k, n in seq:
+ c = "".join({"r": "R", "l": "L", "u": "U", "d": "D", "j": "J", "x": "X", "g": "G"}[ch] for ch in k) or "_"
+ out.append("%s*%d" % (c, n))
+ return " ".join(out)
+
+
+def nw_keys(seq):
+ out, f = [], 0
+ for k, n in seq:
+ if k:
+ out.append("%d-%d:%s" % (f, f + n - 1, k))
+ f += n
+ return ",".join(out)
+
+
+def run_ce(host, room, seq, frames):
+ env = dict(os.environ, CEL_START="1,%s" % room, CEL_TRACE="1", CEL_KEYS=ce_keys(seq),
+ CEL_FRAMES=str(frames + 2), CEL_DATA=os.path.join(CE_DIR, "data"), CEL_OUT="/tmp")
+ out = subprocess.run([host], env=env, capture_output=True, text=True).stdout.split("\n")
+ pos = []
+ for line in out:
+ p = line.split()
+ if len(p) > 10 and p[1] == "st":
+ pos.append((int(p[6]), int(p[8]), int(p[2]), p[-1], int(p[-3])))
+ return pos[1:] # (line n: before update n = after n - 1)
+
+
+def run_nw(play, room, seq, frames):
+ env = dict(os.environ, TRACE="1")
+ out = subprocess.run([play, NW_DATA, "--chapter", "1", "--room", room, "--frames",
+ str(frames), "--keys", nw_keys(seq), "--nowipe"], env=env, capture_output=True, text=True).stdout
+ pos = []
+ for line in out.split("\n"):
+ if line.startswith("f") and " pos " in line:
+ p = line.split()
+ a, b = p[2].split(",")
+ pos.append((int(a), int(b), int(p[6])))
+ return pos
+
+
+def main():
+ ce, nw = sys.argv[1], sys.argv[2]
+ runs = int(sys.argv[sys.argv.index("--runs") + 1]) if "--runs" in sys.argv else 40
+ frames = int(sys.argv[sys.argv.index("--frames") + 1]) if "--frames" in sys.argv else 240
+ seed = int(sys.argv[sys.argv.index("--seed") + 1]) if "--seed" in sys.argv else 1
+ verbose = "-v" in sys.argv
+ rnd = random.Random(seed)
+ total = same = bad = 0
+ for r in range(runs):
+ room = ROOMS[r % len(ROOMS)]
+ seq, n = [], 0
+ while n < frames:
+ k, d = rnd.choice(KEYS), rnd.choice([1, 2, 3, 5, 8, 12, 20, 30])
+ seq.append((k, d))
+ n += d
+ a = run_ce(ce, room, seq, frames)
+ b = run_nw(nw, room, seq, frames)
+ m = 0
+ for i in range(min(len(a), len(b))):
+ if a[i][3] != room or a[i][4] or a[i][2] not in (0, 1, 2):
+ break
+ if a[i][:2] != b[i][:2]:
+ bad += 1
+ if verbose:
+ print(f"room {room} run {r}: frame {i} CE {a[i][:3]} NumWorks {b[i]} (keys {nw_keys(seq)})")
+ break
+ m += 1
+ total += min(len(a), len(b))
+ same += m
+ print(f"{runs} runs: {same} frames the same, {bad} runs differ")
+ return 1 if bad else 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/games/celeste/ce/tests/sf_test.c b/games/celeste/ce/tests/sf_test.c
new file mode 100644
index 00000000..75c7df1e
--- /dev/null
+++ b/games/celeste/ce/tests/sf_test.c
@@ -0,0 +1,54 @@
+/* src/sf.c against the computer's floats: random operands of the sizes Celeste's movement uses, and some edges.
+ * cc -O2 -Isrc tests/sf_test.c src/sf.c -o build/sf_test && build/sf_test */
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "ce.h"
+
+static uint32_t bits(float f) { uint32_t u; memcpy(&u, &f, 4); return u; }
+static float flt(uint32_t u) { float f; memcpy(&f, &u, 4); return f; }
+static uint32_t s = 12345;
+static uint32_t r32(void) { s ^= s << 13, s ^= s >> 17, s ^= s << 5; return s; }
+static float rf(void) {
+ switch (r32() % 6) {
+ case 0: return (float)((int)(r32() % 2001) - 1000); /* whole */
+ case 1: return ((int)(r32() % 200001) - 100000) / 64.f; /* fractions */
+ case 2: return flt((r32() & 0x807FFFFF) | ((110 + r32() % 40) << 23)); /* any, 2^-17 .. 2^22 */
+ case 3: return ((int)(r32() % 2001) - 1000) * 0.0166667f; /* speed * DT */
+ case 4: return flt((r32() & 0x807FFFFF) | ((120 + r32() % 15) << 23));
+ default: return 0.5f + (int)(r32() % 9) - 4; /* halves */
+ }
+}
+
+int main(void) {
+ long bad = 0, n = 4000000;
+ for (long i = 0; i < n; i++) {
+ float a = rf(), b = rf();
+ uint32_t A = bits(a), B = bits(b);
+ volatile float sum = a + b, dif = a - b, pro = a * b, quo = b != 0 ? a / b : 0;
+ if (f_add(A, B) != bits(sum) && !(sum == 0 && f_add(A, B) << 1 == 0)) {
+ if (bad++ < 10) printf("add %a + %a = %a, got %a\n", a, b, sum, flt(f_add(A, B)));
+ }
+ if (f_sub(A, B) != bits(dif) && !(dif == 0 && f_sub(A, B) << 1 == 0)) {
+ if (bad++ < 10) printf("sub %a - %a = %a, got %a\n", a, b, dif, flt(f_sub(A, B)));
+ }
+ if (fabsf(pro) > 1e-30f && f_mul(A, B) != bits(pro)) {
+ if (bad++ < 10) printf("mul %a * %a = %a, got %a\n", a, b, pro, flt(f_mul(A, B)));
+ }
+ if (b != 0 && fabsf(quo) > 1e-30f && f_div(A, B) != bits(quo)) {
+ if (bad++ < 10) printf("div %a / %a = %a, got %a\n", a, b, quo, flt(f_div(A, B)));
+ }
+ if (f_lt(A, B) != (a < b) || f_le(A, B) != (a <= b)) {
+ if (bad++ < 10) printf("cmp %a %a\n", a, b);
+ }
+ if (fabsf(a) < 1e6f) {
+ if (f_round(A) != (int)rintf(a) && bad++ < 10) printf("round %a: %d, got %d\n", a, (int)rintf(a), f_round(A));
+ if (f_floor(A) != (int)floorf(a) && bad++ < 10) printf("floor %a: %d, got %d\n", a, (int)floorf(a), f_floor(A));
+ if (f_trunc(A) != (int)a && bad++ < 10) printf("trunc %a\n", a);
+ }
+ int k = (int)(r32() % 2000001) - 1000000;
+ if (f_int(k) != bits((float)k) && bad++ < 10) printf("int %d\n", k);
+ }
+ printf("%ld operations each, %ld wrong\n", n, bad);
+ return bad != 0;
+}
diff --git a/games/celeste/ce/tools/fconst.py b/games/celeste/ce/tools/fconst.py
new file mode 100644
index 00000000..22ced3c9
--- /dev/null
+++ b/games/celeste/ce/tools/fconst.py
@@ -0,0 +1,70 @@
+#!/usr/bin/env python3
+"""src/fconst.h: the float constants of Celeste's movement as their IEEE single bits (src/sf.c computes with them),
+each worked out the way the C code of the NumWorks port (Player.cs's constants) works it out, in single precision."""
+import os
+
+import numpy as np
+
+f = np.float32
+C = {
+ "0": f(0), "1": f(1), "HALF": f(.5), "2": f(2), "3": f(3), "4": f(4), "5": f(5), "6": f(6), "8": f(8),
+ "DT": f(0.0166667),
+ "MaxFall": f(160), "Gravity": f(900), "HalfGravThreshold": f(40), "FastMaxFall": f(240), "FastMaxAccel": f(300),
+ "MaxRun": f(90), "RunAccel": f(1000), "RunReduce": f(400), "AirMult": f(.65), "DuckFriction": f(500),
+ "DuckCorrectSlide": f(50), "DodgeSlideSpeedMult": f(1.2), "DuckSuperJumpXMult": f(1.25),
+ "DuckSuperJumpYMult": f(.5), "JumpGraceTime": f(.1), "JumpSpeed": f(-105), "JumpHBoost": f(40),
+ "VarJumpTime": f(.2), "CeilingVarJumpGrace": f(.05), "WallSpeedRetentionTime": f(.06),
+ "WallJumpForceTime": f(.16), "WallSlideStartMax": f(20), "WallSlideTime": f(1.2),
+ "BounceVarJumpTime": f(.2), "BounceSpeed": f(-140), "BounceAutoJumpTime": f(.1),
+ "SuperBounceVarJumpTime": f(.2), "SuperBounceSpeed": f(-185), "SuperJumpH": f(260),
+ "SuperWallJumpSpeed": f(-160), "SuperWallJumpVarTime": f(.25), "DashSpeed": f(240), "EndDashSpeed": f(160),
+ "EndDashUpMult": f(.75), "DashTime": f(.15), "DashCooldown": f(.2), "DashRefillCooldown": f(.1),
+ "DashAttackTime": f(.3), "ClimbMaxStamina": f(110), "ClimbUpCost": f(100) / f(2.2), "ClimbStillCost": f(100) / f(10),
+ "ClimbJumpCost": f(110) / f(4), "ClimbNoMoveTime": f(.1), "ClimbTiredThreshold": f(20), "ClimbUpSpeed": f(-45),
+ "ClimbDownSpeed": f(80), "ClimbSlipSpeed": f(30), "ClimbAccel": f(900), "ClimbGrabYMult": f(.2),
+ "ClimbHopY": f(-120), "ClimbHopX": f(100), "ClimbHopForceTime": f(.2), "ClimbJumpBoostTime": f(.2),
+ "LiftYCap": f(-130), "LiftXCap": f(250), "JumpThruAssistSpeed": f(-40), "Diag": f(0.70710677),
+ "ReboundSpeedX": f(120), "ReboundSpeedY": f(-120), "ReboundVarJumpTime": f(.15), "SideBounceSpeed": f(240),
+ "SideBounceForce": f(.3), "TransitionSpeed": f(60) * f(0.0166667), "IntroRise": f(-120) * f(0.0166667),
+ "IntroJumpSpeed": f(-100), "IntroGravity": f(0.0166667) * f(800), "WalkIn": f(64) * f(0.0166667),
+ "LiftTime": f(.16), "StrawbReset": f(2.5), "Respawn": f(.6), "IdleVariant": f(3), "SixtyHalf": f(.12),
+ "HairFlash": f(.12), "Wall": f(1.2) * f(.5), "Freeze": f(.05), "Shake": f(.3), "DashShake": f(.2), "Hop": f(.5),
+ "Collect": f(.15), "BerryFollow": f(.3),
+}
+C["WallJumpHSpeed"] = C["MaxRun"] + C["JumpHBoost"]
+C["SuperWallJumpH"] = C["MaxRun"] + C["JumpHBoost"] * f(2)
+C["RunAccelAir"] = C["RunAccel"] * C["AirMult"]
+C["RunReduceAir"] = C["RunReduce"] * C["AirMult"]
+C["WallSlideHalf"] = C["WallSlideTime"] * f(.5)
+C["FastHalf"] = C["MaxFall"] + (C["FastMaxFall"] - C["MaxFall"]) * f(.5)
+C["DashDiag"] = C["Diag"] * C["DashSpeed"]
+C["EndDashDiag"] = C["Diag"] * C["EndDashSpeed"]
+C["RunAccel40"] = C["RunAccel"] / f(40)
+
+out = ["/* Generated by tools/fconst.py: Celeste's movement constants as single-precision bits. */", "#ifndef FCONST_H",
+ "#define FCONST_H"]
+for k, v in C.items():
+ out.append("#define F_%s 0x%08Xul /* %r */" % (k, np.float32(v).view(np.uint32), float(np.float32(v))))
+
+
+def sine_in(t): # Ease.SineIn: (float)(-Math.Cos(MathHelper.PiOver2 * t) + 1.0), the product a float's
+ import math
+ x = np.float32(np.float32(1.5707964) * np.float32(t))
+ return np.float32(-math.cos(float(x)) + 1.0)
+
+
+def zip_table(step): # ZipMover.Sequence: at = Approach(at, 1, step * DT) until 1, Ease.SineIn(at) each frame
+ at, out = f(0), []
+ d = f(step) * f(0.0166667)
+ while at < f(1):
+ at = min(f(at + d), f(1))
+ out.append(sine_in(at))
+ return out
+
+
+for name, step in (("ZIP_OUT", 2), ("ZIP_BACK", 0.5)):
+ t = zip_table(step)
+ out.append("#define %s_N %d" % (name, len(t)))
+ out.append("#define %s {%s}" % (name, ", ".join("0x%08Xul" % np.float32(v).view(np.uint32) for v in t)))
+out.append("#endif")
+open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "src", "fconst.h"), "w").write("\n".join(out) + "\n")
diff --git a/games/celeste/ce/tools/pack_ce.py b/games/celeste/ce/tools/pack_ce.py
index 0b1a3f40..e1f5f15e 100644
--- a/games/celeste/ce/tools/pack_ce.py
+++ b/games/celeste/ce/tools/pack_ce.py
@@ -489,6 +489,7 @@ class Common:
h.raw(pal.rgb1555(0, COMMON))
h.ref(table).ref(anims).ref(meta).ref(hair)
H.append(f"#define COMMON {COMMON}")
+ H.append(f"#define HAIR {HAIR}\n#define HAIR_DARK {HAIR_DARK}\n#define WHITE {WHITE}\n#define BLACK {BLACK}")
return self.blob.write()
@@ -594,6 +595,7 @@ class Chapter:
self.head = self.blob.item("chapter header")
self.imgs = {}
self.rnd = random.Random(number)
+ self.nberries = 0
# -------------------------------------------------------------- pictures
def image(self, path):
@@ -766,7 +768,15 @@ class Chapter:
h = self.head
h.raw(b"CELC").u8(FORMAT).u8(self.number)
h.raw(pal.rgb1555(COMMON, 256))
- h.u8(self.tint[0]).u8(self.tint[1]).u8(self.tint[2])
+ h.u8(self.tint[0]).u8(self.tint[1]).u8(self.tint[2]).u8(self.nberries)
+ # remaps: each colour tinted red (Color.Red: Madeline tired), and halved (Color.Gray)
+ for f in (lambda c: c * np.array([1, 0, 0]), lambda c: c * 0.5):
+ for i in range(256):
+ if i == 0 or i >= pal.n:
+ h.u8(i)
+ continue
+ c = np.clip(f(rgb_of(pal.colors[i])), 0, 255).astype(int)
+ h.u8(pal.index(int((c[0] >> 3) << 10 | (c[1] >> 3) << 5 | (c[2] >> 3))))
h.u16(len(self.tiles.list)).u8(len(chunks))
for c in chunks:
h.ref(c)
@@ -973,7 +983,10 @@ class Chapter:
elif n in ("strawberry", "goldenBerry"):
flags |= 1 if a("winged", False) else 0
flags |= 2 if a("moon", False) else 0
- vals += [int(a("checkpointID", -1)) & 0xFFFF, int(a("order", -1)) & 0xFFFF]
+ if n == "strawberry":
+ self.nberries += 1
+ vals += [int(a("checkpointID", -1)) & 0xFFFF, int(a("order", -1)) & 0xFFFF,
+ self.nberries - 1 if n == "strawberry" else 0xFFFF]
elif n == "refill":
flags |= 1 if a("twoDash", False) else 0
flags |= 2 if a("oneUse", False) else 0