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.

FNAF: the TI-84 Plus CE version

Mason363826bc8c9e9ea
diff --git a/games/fnaf/Makefile b/games/fnaf/Makefile
index c06d8da5..47834cda 100644
--- a/games/fnaf/Makefile
+++ b/games/fnaf/Makefile
@@ -87,4 +87,6 @@ PORT_NAME = Five Nights at Freddys
PORT_SRC = src/calls.c src/game.c src/main.c src/night.c src/save.c src/view.c src/pic.c src/gfx.c src/plat_eadk.c
PORT_ASM = src/data.S
PORT_FLAGS = -DEPSILON_XL=1 -DDATA_BIN='"src/data.bin"'
+# the TI-84 Plus CE has a version of its own (ce/: 8-bit pictures in appvars)
+PORT_CE = ce
include ../../platform/port.mk
diff --git a/games/fnaf/ce/.gitignore b/games/fnaf/ce/.gitignore
new file mode 100644
index 00000000..6bc89085
--- /dev/null
+++ b/games/fnaf/ce/.gitignore
@@ -0,0 +1,3 @@
+bin/
+obj/
+build/
diff --git a/games/fnaf/ce/data/FNAFD0.bin b/games/fnaf/ce/data/FNAFD0.bin
new file mode 100644
index 00000000..1f70ab50
Binary files /dev/null and b/games/fnaf/ce/data/FNAFD0.bin differ
diff --git a/games/fnaf/ce/data/FNAFD1.bin b/games/fnaf/ce/data/FNAFD1.bin
new file mode 100644
index 00000000..2af96377
Binary files /dev/null and b/games/fnaf/ce/data/FNAFD1.bin differ
diff --git a/games/fnaf/ce/data/FNAFD10.bin b/games/fnaf/ce/data/FNAFD10.bin
new file mode 100644
index 00000000..78215ed3
Binary files /dev/null and b/games/fnaf/ce/data/FNAFD10.bin differ
diff --git a/games/fnaf/ce/data/FNAFD2.bin b/games/fnaf/ce/data/FNAFD2.bin
new file mode 100644
index 00000000..8c86d09d
Binary files /dev/null and b/games/fnaf/ce/data/FNAFD2.bin differ
diff --git a/games/fnaf/ce/data/FNAFD3.bin b/games/fnaf/ce/data/FNAFD3.bin
new file mode 100644
index 00000000..d5ce3795
Binary files /dev/null and b/games/fnaf/ce/data/FNAFD3.bin differ
diff --git a/games/fnaf/ce/data/FNAFD4.bin b/games/fnaf/ce/data/FNAFD4.bin
new file mode 100644
index 00000000..2692ed44
Binary files /dev/null and b/games/fnaf/ce/data/FNAFD4.bin differ
diff --git a/games/fnaf/ce/data/FNAFD5.bin b/games/fnaf/ce/data/FNAFD5.bin
new file mode 100644
index 00000000..2039a344
Binary files /dev/null and b/games/fnaf/ce/data/FNAFD5.bin differ
diff --git a/games/fnaf/ce/data/FNAFD6.bin b/games/fnaf/ce/data/FNAFD6.bin
new file mode 100644
index 00000000..caad8b0e
Binary files /dev/null and b/games/fnaf/ce/data/FNAFD6.bin differ
diff --git a/games/fnaf/ce/data/FNAFD7.bin b/games/fnaf/ce/data/FNAFD7.bin
new file mode 100644
index 00000000..c4f82d0c
Binary files /dev/null and b/games/fnaf/ce/data/FNAFD7.bin differ
diff --git a/games/fnaf/ce/data/FNAFD8.bin b/games/fnaf/ce/data/FNAFD8.bin
new file mode 100644
index 00000000..ffd283f9
Binary files /dev/null and b/games/fnaf/ce/data/FNAFD8.bin differ
diff --git a/games/fnaf/ce/data/FNAFD9.bin b/games/fnaf/ce/data/FNAFD9.bin
new file mode 100644
index 00000000..4b710740
Binary files /dev/null and b/games/fnaf/ce/data/FNAFD9.bin differ
diff --git a/games/fnaf/ce/engine/ce.h b/games/fnaf/ce/engine/ce.h
new file mode 100644
index 00000000..e71ec75d
--- /dev/null
+++ b/games/fnaf/ce/engine/ce.h
@@ -0,0 +1,45 @@
+/* The Five Nights at Freddy's games on the TI-84 Plus CE: the calculator (ce_calc.c: graphx, keypadc, fileioc)
+ * or a computer for tests (ce_host.c), the same to the games.
+ *
+ * The screen is 320 x 240 palette indices, two buffers: a frame is drawn into scr, then ce_show shows it and scr
+ * is the other buffer. The palette changes with the frame that shows it (ce_pal, ce_pal_dirty). */
+#ifndef CE_H
+#define CE_H
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+
+#define SCR_W 320
+#define SCR_H 240
+
+extern uint8_t *scr; /* the frame being drawn */
+extern uint16_t ce_pal[256]; /* the palette (1555 words: the CE's), shown with the next frame */
+extern bool ce_pal_dirty; /* ce_pal changed */
+
+bool ce_init(void); /* false: the data is missing (ce_missing names it) */
+void ce_quit(void);
+/* shows the frame drawn; keep: the next frame starts as a copy of it */
+void ce_show(bool keep);
+
+/* the keys held: the CE's (CK_*) */
+enum {
+ CK_LEFT = 1 << 0, CK_RIGHT = 1 << 1, CK_UP = 1 << 2, CK_DOWN = 1 << 3,
+ CK_2ND = 1 << 4, CK_ENTER = 1 << 5, CK_CLEAR = 1 << 6, CK_MODE = 1 << 7, CK_DEL = 1 << 8,
+ CK_0 = 1 << 9, /* the digits: CK_0 << n */
+ CK_ON = 1 << 19, CK_ALPHA = 1 << 20
+};
+#define CK_DIGIT(n) ((uint32_t)CK_0 << (n))
+uint32_t ce_keys(void);
+uint32_t ce_ticks(void); /* 32768 a second */
+
+/* the data: the appvars <prefix>0, <prefix>1... (archived, read where they are) */
+bool ce_data(const char *prefix, int count, const uint8_t **vars);
+extern char ce_missing[9]; /* the first one missing */
+/* the RAM nobody uses (after the program), for the games' big buffers */
+uint8_t *ce_ram(size_t *len);
+/* the save: an appvar of a fixed size, in RAM while the game runs (written in place), archived when it ends.
+ * ce_save_open makes it (zeros when new) before ce_ram is taken; ce_save_data gives its bytes, ce_save_done
+ * writes them back (on the calculator they are written where they are) */
+uint8_t *ce_save_open(const char *name, size_t size);
+void ce_save_done(void);
+#endif
diff --git a/games/fnaf/ce/engine/ce_calc.c b/games/fnaf/ce/engine/ce_calc.c
new file mode 100644
index 00000000..edfa5917
--- /dev/null
+++ b/games/fnaf/ce/engine/ce_calc.c
@@ -0,0 +1,135 @@
+/* The TI-84 Plus CE (ce.h): graphx's 8-bit mode and two buffers, keypadc, fileioc's appvars. */
+#include "ce.h"
+#include <fileioc.h>
+#include <graphx.h>
+#include <keypadc.h>
+#include <string.h>
+#include <sys/lcd.h>
+#include <ti/screen.h>
+#include <ti/vars.h>
+#include <time.h>
+
+#define LCD_INT_LNBU 4 /* the LCD took the next frame's address (its vertical sync is over) */
+
+uint8_t *scr;
+uint16_t ce_pal[256];
+bool ce_pal_dirty;
+char ce_missing[9];
+static uint8_t save_handle;
+
+bool ce_init(void) {
+ kb_DisableOnLatch();
+ gfx_Begin();
+ gfx_SetDrawBuffer();
+ memset(gfx_palette, 0, 512);
+ memset(gfx_vram, 0, 2 * SCR_W * SCR_H);
+ scr = (uint8_t *)gfx_vbuffer;
+ return true;
+}
+
+void ce_quit(void) {
+ if (save_handle) {
+ ti_SetArchiveStatus(true, save_handle);
+ ti_Close(save_handle);
+ save_handle = 0;
+ }
+ gfx_End();
+ /* a key still down would act on the home screen: wait until it is let go (2 seconds at most) */
+ for (clock_t t = clock(); ce_keys() && clock() - t < 2 * CLOCKS_PER_SEC;) {}
+ os_ClrHomeFull();
+}
+
+void ce_show(bool keep) {
+ gfx_SwapDraw();
+ if (ce_pal_dirty) { /* the palette as the new frame starts: once the LCD has taken its address (gfx_SwapDraw
+ * cleared the flag) */
+ while (!(lcd_IntStatus & LCD_INT_LNBU)) {}
+ memcpy(gfx_palette, ce_pal, sizeof ce_pal);
+ ce_pal_dirty = false;
+ }
+ gfx_Wait();
+ scr = (uint8_t *)gfx_vbuffer;
+ if (keep) memcpy(scr, scr == gfx_vram ? gfx_vram + SCR_W * SCR_H : gfx_vram, SCR_W * SCR_H);
+}
+
+uint32_t ce_keys(void) {
+ static const struct {
+ uint8_t group, bit;
+ uint32_t key;
+ } map[] = {
+ {7, kb_Left, CK_LEFT}, {7, kb_Right, CK_RIGHT}, {7, kb_Up, CK_UP}, {7, kb_Down, CK_DOWN},
+ {1, kb_2nd, CK_2ND}, {6, kb_Enter, CK_ENTER}, {6, kb_Clear, CK_CLEAR}, {1, kb_Mode, CK_MODE},
+ {1, kb_Del, CK_DEL}, {2, kb_Alpha, CK_ALPHA},
+ {3, kb_0, CK_DIGIT(0)}, {3, kb_1, CK_DIGIT(1)}, {4, kb_2, CK_DIGIT(2)}, {5, kb_3, CK_DIGIT(3)},
+ {3, kb_4, CK_DIGIT(4)}, {4, kb_5, CK_DIGIT(5)}, {5, kb_6, CK_DIGIT(6)}, {3, kb_7, CK_DIGIT(7)},
+ {4, kb_8, CK_DIGIT(8)}, {5, kb_9, CK_DIGIT(9)},
+ };
+ kb_Scan();
+ uint32_t k = 0;
+ for (unsigned i = 0; i < sizeof map / sizeof map[0]; i++)
+ if (kb_Data[map[i].group] & map[i].bit) k |= map[i].key;
+ if (kb_On) k |= CK_ON;
+ return k;
+}
+
+uint32_t ce_ticks(void) { return (uint32_t)clock(); }
+
+bool ce_data(const char *prefix, int count, const uint8_t **vars) {
+ for (int i = 0; i < count; i++) {
+ char name[9];
+ size_t n = strlen(prefix);
+ memcpy(name, prefix, n);
+ if (i >= 10) name[n++] = (char)('0' + i / 10);
+ name[n++] = (char)('0' + i % 10);
+ name[n] = 0;
+ uint8_t h = ti_Open(name, "r");
+ if (!h) {
+ memcpy(ce_missing, name, sizeof name);
+ return false;
+ }
+ vars[i] = ti_GetDataPtr(h);
+ ti_Close(h);
+ }
+ return true;
+}
+
+uint8_t *ce_ram(size_t *len) {
+ void *p;
+ *len = os_MemChk(&p);
+ return p;
+}
+
+uint8_t *ce_save_open(const char *name, size_t size) {
+ uint8_t h = ti_Open(name, "r+");
+ if (h && ti_GetSize(h) != size) {
+ ti_Close(h);
+ ti_Delete(name);
+ h = 0;
+ }
+ if (h) {
+ ti_SetArchiveStatus(false, h); /* in RAM while the game runs: written in place */
+ } else {
+ h = ti_Open(name, "w+");
+ if (!h) return NULL;
+ static const uint8_t zero[64];
+ for (size_t n = 0; n < size; n += sizeof zero) ti_Write(zero, 1, size - n < sizeof zero ? size - n : sizeof zero, h);
+ }
+ save_handle = h;
+ return ti_GetDataPtr(h);
+}
+
+void ce_save_done(void) {}
+
+/* a message on the home screen (the game can't start), until a key */
+void ce_message(const char *a, const char *b) {
+ gfx_End();
+ os_ClrHomeFull();
+ os_SetCursorPos(0, 0);
+ os_PutStrFull(a);
+ os_SetCursorPos(1, 0);
+ os_PutStrFull(b);
+ while (ce_keys()) {}
+ while (!ce_keys()) {}
+ while (ce_keys()) {}
+ os_ClrHomeFull();
+}
diff --git a/games/fnaf/ce/engine/ce_host.c b/games/fnaf/ce/engine/ce_host.c
new file mode 100644
index 00000000..8e55daf5
--- /dev/null
+++ b/games/fnaf/ce/engine/ce_host.c
@@ -0,0 +1,166 @@
+/* A computer standing in for the calculator (ce.h), for tests: the two screen buffers and the palette in memory,
+ * keys and time from the test (host_keys, host_ticks), the appvars from files, the save in a file. host_shot
+ * writes what the screen shows. With HOST_SDL, a window too (host_sdl.c). */
+#include "ce.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+uint8_t *scr;
+uint16_t ce_pal[256];
+bool ce_pal_dirty;
+char ce_missing[9];
+
+static uint8_t buffers[2][SCR_W * SCR_H];
+static int shown; /* the buffer the screen shows */
+static uint16_t shown_pal[256];
+uint32_t host_keys, host_ticks;
+const char *host_data_dir = "data"; /* where FNAFD0.bin... are */
+const char *host_save_dir = "build/host-saves";
+size_t host_ram_size = 96 * 1024; /* the free RAM a calculator would have */
+long host_frames; /* frames shown */
+
+bool ce_init(void) {
+ memset(buffers, 0, sizeof buffers);
+ shown = 0;
+ scr = buffers[1];
+ return true;
+}
+
+void ce_quit(void) { ce_save_done(); }
+
+void ce_show(bool keep) {
+ if (ce_pal_dirty) memcpy(shown_pal, ce_pal, sizeof ce_pal), ce_pal_dirty = false;
+ shown ^= 1;
+ scr = buffers[shown ^ 1];
+ if (keep) memcpy(scr, buffers[shown], sizeof buffers[0]);
+ host_frames++;
+}
+
+uint32_t ce_keys(void) { return host_keys; }
+uint32_t ce_ticks(void) { return host_ticks; }
+
+bool ce_data(const char *prefix, int count, const uint8_t **vars) {
+ for (int i = 0; i < count; i++) {
+ char p[512];
+ snprintf(p, sizeof p, "%s/%s%d.bin", host_data_dir, prefix, i);
+ FILE *f = fopen(p, "rb");
+ if (!f) {
+ snprintf(ce_missing, sizeof ce_missing, "%s%d", prefix, i);
+ return false;
+ }
+ fseek(f, 0, SEEK_END);
+ long n = ftell(f);
+ fseek(f, 0, SEEK_SET);
+ uint8_t *b = malloc((size_t)n + 64); /* (kept until the end, as the archive keeps them) */
+ if (fread(b, 1, (size_t)n, f) != (size_t)n) n = 0;
+ fclose(f);
+ vars[i] = b;
+ }
+ return true;
+}
+
+static uint8_t *ram;
+uint8_t *ce_ram(size_t *len) {
+ if (!ram) ram = malloc(host_ram_size);
+ *len = host_ram_size;
+ return ram;
+}
+
+static uint8_t *save;
+static size_t save_size;
+static char save_name[64];
+uint8_t *ce_save_open(const char *name, size_t size) {
+ free(save);
+ save = calloc(1, size);
+ save_size = size;
+ snprintf(save_name, sizeof save_name, "%s", name);
+ char p[512];
+ snprintf(p, sizeof p, "%s/%s.8xv.bin", host_save_dir, name);
+ FILE *f = fopen(p, "rb");
+ if (f) {
+ fseek(f, 0, SEEK_END);
+ long n = ftell(f);
+ fseek(f, 0, SEEK_SET);
+ if ((size_t)n == size && fread(save, 1, size, f) != size) memset(save, 0, size);
+ fclose(f);
+ }
+ return save;
+}
+
+void ce_save_done(void) {
+ if (!save) return;
+ char p[512];
+ snprintf(p, sizeof p, "%s/%s.8xv.bin", host_save_dir, save_name);
+ FILE *f = fopen(p, "wb");
+ if (!f) return;
+ fwrite(save, 1, save_size, f);
+ fclose(f);
+}
+
+/* the screen as shown, in RGB */
+void host_rgb(uint8_t *out) {
+ const uint8_t *b = buffers[shown];
+ for (int i = 0; i < SCR_W * SCR_H; i++) {
+ uint16_t c = shown_pal[b[i]];
+ int r = c >> 10 & 31, g = (c >> 5 & 31) << 1 | c >> 15, bl = c & 31;
+ out[3 * i] = (uint8_t)(r * 255 / 31), out[3 * i + 1] = (uint8_t)(g * 255 / 63), out[3 * i + 2] = (uint8_t)(bl * 255 / 31);
+ }
+}
+
+void host_shot(const char *path) {
+ static uint8_t rgb[SCR_W * SCR_H * 3];
+ host_rgb(rgb);
+ FILE *f = fopen(path, "wb");
+ if (!f) return;
+ fprintf(f, "P6\n%d %d\n255\n", SCR_W, SCR_H);
+ fwrite(rgb, 1, sizeof rgb, f);
+ fclose(f);
+}
+
+/* zx0 (the toolchain's zx0_Decompress on the calculator): Einar Saukas's format, version 2. Elias gamma
+ * numbers with their bits interlaced; a new offset's high part has its bits inverted, its low 7 bits are a byte's
+ * top 7, and that byte's lowest bit is the first bit of the length that follows. */
+typedef struct {
+ const uint8_t *s;
+ int mask, value, back, last;
+} Zx0;
+
+static int zbyte(Zx0 *z) { return z->last = *z->s++; }
+
+static int zbit(Zx0 *z) {
+ if (z->back) {
+ z->back = 0;
+ return z->last & 1;
+ }
+ z->mask >>= 1;
+ if (!z->mask) z->mask = 128, z->value = zbyte(z);
+ return (z->value & z->mask) != 0;
+}
+
+static int zgamma(Zx0 *z, int inverted) {
+ int v = 1;
+ while (!zbit(z)) v = v << 1 | (zbit(z) ^ inverted);
+ return v;
+}
+
+void zx0_Decompress(void *dst, const void *src) {
+ Zx0 z = {src, 0, 0, 0, 0};
+ uint8_t *o = dst;
+ int offset = 1, n;
+ for (;;) {
+ for (n = zgamma(&z, 0); n--;) *o++ = (uint8_t)zbyte(&z); /* literals */
+ if (!zbit(&z)) {
+ for (n = zgamma(&z, 0); n--; o++) *o = o[-offset]; /* the last offset again */
+ if (!zbit(&z)) continue;
+ }
+ for (;;) { /* a new offset */
+ n = zgamma(&z, 1);
+ if (n == 256) return;
+ offset = n * 128 - (zbyte(&z) >> 1);
+ z.back = 1;
+ for (n = zgamma(&z, 0) + 1; n--; o++) *o = o[-offset];
+ if (!zbit(&z)) break;
+ }
+ }
+}
diff --git a/games/fnaf/ce/engine/gfx.c b/games/fnaf/ce/engine/gfx.c
new file mode 100644
index 00000000..580aed30
--- /dev/null
+++ b/games/fnaf/ce/engine/gfx.c
@@ -0,0 +1,268 @@
+/* Drawing on the CE's screen (gfx.h). */
+#include "gfx.h"
+#include <string.h>
+
+void zx0_Decompress(void *dst, const void *src);
+
+static Assets A;
+
+void gfx_init(const Assets *a, int tables) {
+ A = *a;
+ pic_init(asset(tables));
+}
+
+const uint8_t *asset(int id) {
+ uint32_t at = A.at[id];
+ return A.vars[at >> 16] + (at & 0xFFFF);
+}
+
+/* ------------------------------------------------------------------ maps: two places, the older one reused */
+static uint8_t maps[2][(MAP_BYTES + 3) & ~3] __attribute__((aligned(4)));
+static int map_group[2] = {-1, -1}, map_age[2];
+static int age;
+static PicMap picmap[2];
+static uint8_t phase_bytes[256];
+
+static int slot_of(int group) {
+ for (int s = 0; s < 2; s++)
+ if (map_group[s] == group) {
+ map_age[s] = ++age;
+ return s;
+ }
+ int s = map_age[0] <= map_age[1] ? 0 : 1;
+ zx0_Decompress(maps[s], asset(A.map_first + group));
+ map_group[s] = group;
+ map_age[s] = ++age;
+ const uint8_t *m = maps[s] + 512;
+ /* a group with phases has its colours four times over: the static's phase goes in the low two bits */
+ bool phases = maps[s][MAP_BYTES - 1] == 4;
+ if (!phase_bytes[0] && !phase_bytes[1]) { /* (once: random bytes) */
+ uint32_t r = 0x9E3779B9u;
+ for (int i = 0; i < 256; i++) r ^= r << 13, r ^= r >> 17, r ^= r << 5, phase_bytes[i] = (uint8_t)(r >> 11);
+ }
+ picmap[s] = (PicMap){m, m + (YQ_HI - YQ_LO), m + (YQ_HI - YQ_LO) + (CQ_HI - CQ_LO),
+ m + (YQ_HI - YQ_LO) + 2 * (CQ_HI - CQ_LO), phases ? phase_bytes : NULL};
+ return s;
+}
+
+const uint8_t *map_of(int group) { return maps[slot_of(group)]; }
+const uint16_t *map_palette(int group) { return (const uint16_t *)(const void *)maps[slot_of(group)]; }
+const uint8_t *map_remap(int group, int which) { return maps[slot_of(group)] + MAP_BYTES - 513 + 256 * which; }
+
+/* ------------------------------------------------------------------ pictures */
+int pic_group_of(int pic) { return A.pic_group[pic]; }
+int pic_w(int pic) { return pic_width(asset(A.pic_first + pic)); }
+int pic_h(int pic) { return pic_height(asset(A.pic_first + pic)); }
+
+void pic_into(int pic, uint8_t *dst, int stride) {
+ int s = slot_of(A.pic_group[pic]);
+ pic_decode(asset(A.pic_first + pic), dst, stride, &picmap[s]);
+}
+
+void pic_full(int pic, uint8_t *dst, int stride) {
+ int base = pic_base(asset(A.pic_first + pic));
+ if (base >= 0) pic_full(base, dst, stride);
+ pic_into(pic, dst, stride);
+}
+
+/* ------------------------------------------------------------------ drawing */
+static bool clip(int *x, int *y, int *w, int *h, int *sx, int *sy) {
+ if (*x < 0) *sx -= *x, *w += *x, *x = 0;
+ if (*y < 0) *sy -= *y, *h += *y, *y = 0;
+ if (*x + *w > SCR_W) *w = SCR_W - *x;
+ if (*y + *h > SCR_H) *h = SCR_H - *y;
+ return *w > 0 && *h > 0;
+}
+
+void d_fill(int x, int y, int w, int h, uint8_t c) {
+ int sx = 0, sy = 0;
+ if (!clip(&x, &y, &w, &h, &sx, &sy)) return;
+ for (uint8_t *p = scr + y * SCR_W + x; h--; p += SCR_W) memset(p, c, (size_t)w);
+}
+
+void d_frame(int x, int y, int w, int h, uint8_t c) {
+ d_fill(x, y, w, 1, c);
+ d_fill(x, y + h - 1, w, 1, c);
+ d_fill(x, y, 1, h, c);
+ d_fill(x + w - 1, y, 1, h, c);
+}
+
+void d_remap(int x, int y, int w, int h, const uint8_t *lut) {
+ int sx = 0, sy = 0;
+ if (!clip(&x, &y, &w, &h, &sx, &sy)) return;
+ for (uint8_t *row = scr + y * SCR_W + x; h--; row += SCR_W)
+ for (uint8_t *p = row, *e = row + w; p < e; p++) *p = lut[*p];
+}
+
+void d_blit(const uint8_t *src, int stride, int sx, int sy, int w, int h, int x, int y, bool dbl) {
+ if (!dbl) {
+ if (!clip(&x, &y, &w, &h, &sx, &sy)) return;
+ const uint8_t *s = src + sy * stride + sx;
+ for (uint8_t *p = scr + y * SCR_W + x; h--; p += SCR_W, s += stride) memcpy(p, s, (size_t)w);
+ return;
+ }
+ /* rows doubled: source row r at screen rows y + 2r and y + 2r + 1 */
+ if (x < 0) sx -= x, w += x, x = 0;
+ if (x + w > SCR_W) w = SCR_W - x;
+ if (w <= 0) return;
+ for (int r = 0; r < h; r++) {
+ int yy = y + 2 * r;
+ if (yy + 1 < 0 || yy >= SCR_H) continue;
+ const uint8_t *s = src + (sy + r) * stride + sx;
+ if (yy >= 0) memcpy(scr + yy * SCR_W + x, s, (size_t)w);
+ if (yy + 1 < SCR_H) memcpy(scr + (yy + 1) * SCR_W + x, s, (size_t)w);
+ }
+}
+
+void d_blit_key(const uint8_t *src, int stride, int w, int h, int x, int y, uint8_t key) {
+ int sx = 0, sy = 0;
+ if (!clip(&x, &y, &w, &h, &sx, &sy)) return;
+ const uint8_t *s = src + sy * stride + sx;
+ for (uint8_t *p = scr + y * SCR_W + x; h--; p += SCR_W, s += stride)
+ for (int i = 0; i < w; i++)
+ if (s[i] != key) p[i] = s[i];
+}
+
+void d_blit2(const uint8_t *src, int stride, int dx, uint8_t fill) {
+ int x0 = dx > 0 ? dx : 0, x1 = dx < 0 ? SCR_W + dx : SCR_W;
+ for (int r = 0; r < SCR_H / 2; r++) {
+ uint8_t *d = scr + 2 * r * SCR_W;
+ const uint8_t *s = src + r * stride;
+ if (dx > 0) memset(d, fill, (size_t)dx);
+ if (dx < 0) memset(d + x1, fill, (size_t)-dx);
+ uint8_t *o = d + x0;
+ const uint8_t *i = s + ((x0 - dx) >> 1);
+ int n = x1 - x0;
+ if ((x0 - dx) & 1) *o++ = *i++, n--;
+ for (; n >= 2; n -= 2, i++) o[0] = o[1] = *i, o += 2;
+ if (n) *o = *i;
+ memcpy(d + SCR_W, d, SCR_W);
+ }
+}
+
+void d_noise(int x, int y, int w, int h, uint32_t seed, const uint8_t *greys) {
+ int sx = 0, sy = 0;
+ if (!clip(&x, &y, &w, &h, &sx, &sy)) return;
+ uint32_t r = seed | 1;
+ for (uint8_t *row = scr + y * SCR_W + x; h--; row += SCR_W) {
+ uint8_t *p = row, *e = row + w;
+ while (p < e) {
+ r ^= r << 13, r ^= r >> 17, r ^= r << 5;
+ uint32_t b = r;
+ for (int k = 0; k < 8 && p < e; k++, b >>= 3) *p++ = greys[b & 7];
+ }
+ }
+}
+
+/* ------------------------------------------------------------------ text */
+static const uint8_t *font_at(int font) { return asset(A.font_first + font); }
+int d_line_h(int font) { return font_at(font)[0]; }
+
+static void glyph(const uint8_t *f, const uint8_t *gl, int x, int y, Ink ink) {
+ int w = gl[1], h = gl[2];
+ if (!w) return;
+ const uint8_t *rows = f + (gl[6] | gl[7] << 8);
+ int stride = (w + 3) >> 2;
+ x += (int8_t)gl[3], y += (int8_t)gl[4];
+ for (int r = 0; r < h; r++, rows += stride) {
+ int yy = y + r;
+ if (yy < 0 || yy >= SCR_H) continue;
+ uint8_t *d = scr + yy * SCR_W;
+ for (int c = 0; c < w; c++) {
+ int a = rows[c >> 2] >> (6 - 2 * (c & 3)) & 3, xx = x + c;
+ if (a && xx >= 0 && xx < SCR_W) d[xx] = ink[a];
+ }
+ }
+}
+
+int d_text_n(int font, int x, int y, const char *s, int n, Ink ink) {
+ const uint8_t *f = font_at(font);
+ int x0 = x;
+ for (; *s && n-- > 0; s++) {
+ int ch = (uint8_t)*s;
+ if (ch < 32 || ch > 126) ch = '?';
+ const uint8_t *gl = f + 2 + 8 * (ch - 32);
+ glyph(f, gl, x, y, ink);
+ x += gl[0];
+ }
+ return x - x0;
+}
+
+int d_text(int font, int x, int y, const char *s, Ink ink) { return d_text_n(font, x, y, s, 1 << 20, ink); }
+
+int d_text_wn(int font, const char *s, int n) {
+ const uint8_t *f = font_at(font);
+ int w = 0;
+ for (; *s && n-- > 0; s++) {
+ int ch = (uint8_t)*s;
+ if (ch < 32 || ch > 126) ch = '?';
+ w += f[2 + 8 * (ch - 32)];
+ }
+ return w;
+}
+int d_text_w(int font, const char *s) { return d_text_wn(font, s, 1 << 20); }
+void d_text_c(int font, int cx, int y, const char *s, Ink ink) { d_text(font, cx - d_text_w(font, s) / 2, y, s, ink); }
+void d_text_r(int font, int rx, int y, const char *s, Ink ink) { d_text(font, rx - d_text_w(font, s), y, s, ink); }
+
+void d_img(const uint8_t *img, int x, int y, Ink ink) {
+ int w = img[0] | img[1] << 8, h = img[2] | img[3] << 8, stride = (w + 3) >> 2;
+ const uint8_t *rows = img + 4;
+ for (int r = 0; r < h; r++, rows += stride) {
+ int yy = y + r;
+ if (yy < 0 || yy >= SCR_H) continue;
+ uint8_t *d = scr + yy * SCR_W;
+ for (int c = 0; c < w; c++) {
+ int a = rows[c >> 2] >> (6 - 2 * (c & 3)) & 3, xx = x + c;
+ if (a && xx >= 0 && xx < SCR_W) d[xx] = ink[a];
+ }
+ }
+}
+
+/* ------------------------------------------------------------------ the palette */
+static uint16_t mix(uint16_t c, int r2, int g2, int b2, int a) { /* c toward (r2, g2, b2) by a / 32 */
+ int r = c >> 10 & 31, g = (c >> 5 & 31) << 1 | c >> 15, b = c & 31;
+ r += (r2 - r) * a / 32, g += (g2 - g) * a / 32, b += (b2 - b) * a / 32;
+ return (uint16_t)((g & 1) << 15 | r << 10 | (g >> 1) << 5 | b);
+}
+
+static uint16_t scale(uint16_t c, int k) { /* each channel times k / 32 */
+ int r = (c >> 10 & 31) * k / 32, g = ((c >> 5 & 31) << 1 | c >> 15) * k / 32, b = (c & 31) * k / 32;
+ r = r > 31 ? 31 : r, g = g > 63 ? 63 : g, b = b > 31 ? 31 : b;
+ return (uint16_t)((g & 1) << 15 | r << 10 | (g >> 1) << 5 | b);
+}
+
+static struct {
+ int group, dim, white;
+} last = {-2, 0, 0};
+
+void pal_set(int group, int dim, const uint8_t *phase, int white) {
+ if (!phase && group == last.group && dim == last.dim && white == last.white) return;
+ last.group = group, last.dim = dim, last.white = white;
+ if (phase) last.group = -2;
+ if (group < 0) {
+ memset(ce_pal, 0, 2 * PICS_COLORS);
+ if (map_group[0] < 0 && map_group[1] < 0) slot_of(0);
+ memcpy(ce_pal + PICS_COLORS, maps[map_group[0] >= 0 ? 0 : 1] + 2 * PICS_COLORS, 2 * (256 - PICS_COLORS));
+ ce_pal_dirty = true;
+ return;
+ }
+ const uint16_t *p = map_palette(group);
+ for (int i = 0; i < PICS_COLORS; i++) {
+ uint16_t c = p[i];
+ if (phase && phase[i & 3]) { /* the static: each phase toward its grey */
+ int k = phase[4 + (i & 3)];
+ c = mix(c, k, 2 * k, k, phase[i & 3]);
+ }
+ if (white) c = mix(c, 31, 63, 31, white);
+ if (dim != 32) c = scale(c, dim);
+ ce_pal[i] = c;
+ }
+ memcpy(ce_pal + PICS_COLORS, p + PICS_COLORS, 2 * (256 - PICS_COLORS));
+ ce_pal_dirty = true;
+}
+
+void pal_ui(int dim) {
+ for (int i = PICS_COLORS; i < 256; i++) ce_pal[i] = scale(ce_pal[i], dim);
+ last.group = -2;
+ ce_pal_dirty = true;
+}
diff --git a/games/fnaf/ce/engine/gfx.h b/games/fnaf/ce/engine/gfx.h
new file mode 100644
index 00000000..cee70749
--- /dev/null
+++ b/games/fnaf/ce/engine/gfx.h
@@ -0,0 +1,68 @@
+/* Drawing on the CE's 8-bit screen (ce.h's scr): the data's assets, picture groups (their palettes and maps),
+ * pictures, blits, text and the palette's effects. */
+#ifndef CE_GFX_H
+#define CE_GFX_H
+#include <stdbool.h>
+#include <stdint.h>
+#include "ce.h"
+#include "pic.h"
+
+#define PICS_COLORS 224 /* a palette: the group's colours, then 32 fixed ones (the games' UI_*) */
+#define MAP_BYTES (512 + (YQ_HI - YQ_LO) + 2 * (CQ_HI - CQ_LO) + 2048 + 512 + 1) /* (the last: 4 with phases) */
+
+/* the data: the appvars, where each asset is (appvar << 16 | offset), each picture's group, where the kinds
+ * of assets start */
+typedef struct {
+ const uint8_t *const *vars;
+ const uint32_t *at;
+ const uint8_t *pic_group;
+ int pic_first, map_first, font_first;
+} Assets;
+void gfx_init(const Assets *a, int tables);
+const uint8_t *asset(int id);
+
+/* a group's map (decoded once into one of two places): its palette (256 words), and the rest */
+const uint8_t *map_of(int group);
+const uint16_t *map_palette(int group);
+const uint8_t *map_remap(int group, int which); /* 0: each colour darker, 1: lighter */
+
+/* pictures: decoded over what dst holds (pic_into), or with their bases first (pic_full) */
+int pic_group_of(int pic);
+void pic_into(int pic, uint8_t *dst, int stride);
+void pic_full(int pic, uint8_t *dst, int stride);
+int pic_w(int pic);
+int pic_h(int pic);
+
+/* drawing on scr, clipped to the screen */
+void d_fill(int x, int y, int w, int h, uint8_t c);
+void d_frame(int x, int y, int w, int h, uint8_t c);
+void d_remap(int x, int y, int w, int h, const uint8_t *lut);
+/* (sx, sy, w, h) of an image (stride bytes a row) at (x, y): rows doubled (dbl: the image's rows are half) */
+void d_blit(const uint8_t *src, int stride, int sx, int sy, int w, int h, int x, int y, bool dbl);
+/* the same, the colour key skipped */
+void d_blit_key(const uint8_t *src, int stride, int w, int h, int x, int y, uint8_t key);
+/* an image doubled both ways over the whole screen (160 x 120), dx pixels to the right (fill on the side) */
+void d_blit2(const uint8_t *src, int stride, int dx, uint8_t fill);
+/* static: random greys of the fixed colours (UI) over the rectangle */
+void d_noise(int x, int y, int w, int h, uint32_t seed, const uint8_t *greys);
+
+/* text: the fonts' glyphs (2-bit alpha) in an ink: the palette indices of alpha 1, 2, 3 (ink[1..3]) */
+typedef const uint8_t *Ink;
+int d_text(int font, int x, int y, const char *s, Ink ink);
+int d_text_n(int font, int x, int y, const char *s, int n, Ink ink);
+int d_text_w(int font, const char *s);
+int d_text_wn(int font, const char *s, int n);
+int d_line_h(int font);
+void d_text_c(int font, int cx, int y, const char *s, Ink ink);
+void d_text_r(int font, int rx, int y, const char *s, Ink ink);
+/* a small picture of 2-bit alpha (w, h, rows) */
+void d_img(const uint8_t *img, int x, int y, Ink ink);
+
+/* the palette shown (ce_pal) from a group's (-1: none, its colours black): dim (0 black .. 32 as it is .. 64
+ * twice as bright), the 4 phases' static (phase[0..3]: 0 .. 32 of the way to the grey phase[4..7], 0 .. 31; NULL:
+ * none), white (0 .. 32 of the way
+ * to white); the fixed colours as they are */
+void pal_set(int group, int dim, const uint8_t *phase, int white);
+/* the fixed colours dimmed too (0 .. 32), until the next pal_set */
+void pal_ui(int dim);
+#endif
diff --git a/games/fnaf/ce/host.mk b/games/fnaf/ce/host.mk
new file mode 100644
index 00000000..f2ffc2fd
--- /dev/null
+++ b/games/fnaf/ce/host.mk
@@ -0,0 +1,26 @@
+# The CE version of Five Nights at Freddy's on a computer, for tests (the calculator's build: makefile).
+# make -f host.mk build/play (tests/play.c), with the sanitizers
+# make -f host.mk test tests/check.py
+Q ?= @
+FLAGS = -std=gnu11 -O1 -g -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -DHOST \
+ -fsanitize=address,undefined -fno-sanitize-recover=undefined
+SRC = src/main.c src/scenes.c src/view.c src/save.c src/rules.c engine/pic.c engine/gfx.c engine/ce_host.c
+HEADERS = $(wildcard src/*.h engine/*.h ../src/*.h) ../src/game.c ../src/night.c ../src/calls.c
+
+.PHONY: all test clean
+all: build/play build/pics
+
+build/play: $(SRC) tests/play.c $(HEADERS)
+ $(Q)mkdir -p build
+ $(Q)cc $(FLAGS) $(SRC) tests/play.c -lm -o $@
+ @echo "HOST $@"
+
+build/pics: engine/pic.c engine/gfx.c engine/ce_host.c tests/pics.c $(HEADERS)
+ $(Q)mkdir -p build
+ $(Q)cc $(FLAGS) engine/pic.c engine/gfx.c engine/ce_host.c tests/pics.c -o $@
+
+test: build/play build/pics
+ python3 tests/check.py build/play build/pics
+
+clean:
+ $(Q)rm -rf build
diff --git a/games/fnaf/ce/icon.png b/games/fnaf/ce/icon.png
new file mode 100644
index 00000000..beb26590
Binary files /dev/null and b/games/fnaf/ce/icon.png differ
diff --git a/games/fnaf/ce/makefile b/games/fnaf/ce/makefile
new file mode 100644
index 00000000..4d83ef70
--- /dev/null
+++ b/games/fnaf/ce/makefile
@@ -0,0 +1,21 @@
+# Five Nights at Freddy's for the TI-84 Plus CE (the CE C toolchain, v15: its bin folder on the PATH).
+# make bin/FNAF.8xp and the data, bin/FNAFD0.8xv, FNAFD1.8xv... (from data/, made by tools/pack.py)
+# make -f host.mk test the same on a computer, tested
+NAME = FNAF
+ICON = icon.png
+DESCRIPTION = "Five Nights at Freddy's"
+COMPRESSED = YES
+ARCHIVED = YES
+CFLAGS = -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Oz
+SRCDIR = src
+EXTRA_C_SOURCES = engine/pic.c engine/gfx.c engine/ce_calc.c
+
+all: target appvars
+include $(shell cedev-config --makefile)
+
+APPVARS = $(patsubst data/%.bin,$(BINDIR)/%.8xv,$(wildcard data/FNAFD*.bin))
+.PHONY: all appvars
+appvars: $(APPVARS)
+$(BINDIR)/%.8xv: data/%.bin
+ $(Q)$(call MKDIR,$(@D))
+ $(Q)$(CONVBIN) -r -j bin -k 8xv -n $* -i $< -o $@
diff --git a/games/fnaf/ce/src/assets.h b/games/fnaf/ce/src/assets.h
new file mode 100644
index 00000000..cdf4721f
--- /dev/null
+++ b/games/fnaf/ce/src/assets.h
@@ -0,0 +1,324 @@
+/* Made by tools/pack.py: what is in the appvars FNAFD0, FNAFD1... */
+#ifndef FNAF_ASSETS_H
+#define FNAF_ASSETS_H
+#include <stdint.h>
+
+#define DATA_VAR "FNAFD"
+#define DATA_VARS 11
+#define ASSET_COUNT 223
+
+#define ASSET_TABLES 0
+#define MAP_FIRST 1
+#define PIC_FIRST 25
+#define FONT_FIRST 214
+#define ASSET_IMG_TITLE 220
+#define ASSET_IMG_STAR 221
+#define ASSET_CAMMAP 222
+
+#define PIC_OFFICE 0
+#define PIC_LIGHT_L 1
+#define PIC_LIGHT_L_WHO 2
+#define PIC_LIGHT_R 3
+#define PIC_LIGHT_R_WHO 4
+#define PIC_DOOR_L 5
+#define PIC_DOOR_R 6
+#define PIC_PANEL_L0 7
+#define PIC_PANEL_L1 8
+#define PIC_PANEL_L2 9
+#define PIC_PANEL_L3 10
+#define PIC_PANEL_R0 11
+#define PIC_PANEL_R1 12
+#define PIC_PANEL_R2 13
+#define PIC_PANEL_R3 14
+#define PIC_FAN0 15
+#define PIC_FAN1 16
+#define PIC_FAN2 17
+#define PIC_OFFICE_GOLD 18
+#define PIC_DARK 19
+#define PIC_DARK_FREDDY 20
+#define PIC_CAM1A_EMPTY 21
+#define PIC_CAM1A_F 22
+#define PIC_CAM1A_BF 23
+#define PIC_CAM1A_CF 24
+#define PIC_CAM1A_BCF 25
+#define PIC_CAM1B_EMPTY 26
+#define PIC_CAM1B_B 27
+#define PIC_CAM1B_C 28
+#define PIC_CAM1B_F 29
+#define PIC_CAM1B_BC 30
+#define PIC_CAM1B_BF 31
+#define PIC_CAM1B_CF 32
+#define PIC_CAM1B_BCF 33
+#define PIC_CAM2A_EMPTY 34
+#define PIC_CAM2A_B 35
+#define PIC_CAM2A_DARK 36
+#define PIC_FOXYRUN0 37
+#define PIC_FOXYRUN1 38
+#define PIC_FOXYRUN2 39
+#define PIC_FOXYRUN3 40
+#define PIC_FOXYRUN4 41
+#define PIC_FOXYRUN5 42
+#define PIC_FOXYRUN6 43
+#define PIC_FOXYRUN7 44
+#define PIC_FOXYRUN8 45
+#define PIC_FOXYRUN9 46
+#define PIC_FOXYRUN10 47
+#define PIC_FOXYRUN11 48
+#define PIC_FOXYRUN12 49
+#define PIC_FOXYRUN13 50
+#define PIC_FOXYRUN14 51
+#define PIC_FOXYRUN15 52
+#define PIC_FOXYRUN16 53
+#define PIC_FOXYRUN17 54
+#define PIC_CAM2B_EMPTY 55
+#define PIC_CAM2B_B 56
+#define PIC_CAM2B_GOLD 57
+#define PIC_CAM3_EMPTY 58
+#define PIC_CAM3_B 59
+#define PIC_CAM4A_EMPTY 60
+#define PIC_CAM4A_C 61
+#define PIC_CAM4A_F 62
+#define PIC_CAM4A_CF 63
+#define PIC_CAM4B_EMPTY 64
+#define PIC_CAM4B_C 65
+#define PIC_CAM4B_F 66
+#define PIC_CAM5_EMPTY 67
+#define PIC_CAM5_B 68
+#define PIC_CAM7_EMPTY 69
+#define PIC_CAM7_C 70
+#define PIC_CAM7_F 71
+#define PIC_CAM7_CF 72
+#define PIC_CAM1C_0 73
+#define PIC_CAM1C_1 74
+#define PIC_CAM1C_2 75
+#define PIC_CAM1C_3 76
+#define PIC_BONNIE0 77
+#define PIC_BONNIE1 78
+#define PIC_BONNIE2 79
+#define PIC_BONNIE3 80
+#define PIC_BONNIE4 81
+#define PIC_BONNIE5 82
+#define PIC_BONNIE6 83
+#define PIC_BONNIE7 84
+#define PIC_BONNIE8 85
+#define PIC_BONNIE9 86
+#define PIC_CHICA0 87
+#define PIC_CHICA1 88
+#define PIC_CHICA2 89
+#define PIC_CHICA3 90
+#define PIC_CHICA4 91
+#define PIC_CHICA5 92
+#define PIC_CHICA6 93
+#define PIC_CHICA7 94
+#define PIC_CHICA8 95
+#define PIC_CHICA9 96
+#define PIC_CHICA10 97
+#define PIC_CHICA11 98
+#define PIC_CHICA12 99
+#define PIC_CHICA13 100
+#define PIC_CHICA14 101
+#define PIC_FREDDY0 102
+#define PIC_FREDDY1 103
+#define PIC_FREDDY2 104
+#define PIC_FREDDY3 105
+#define PIC_FREDDY4 106
+#define PIC_FREDDY5 107
+#define PIC_FREDDY6 108
+#define PIC_FREDDY7 109
+#define PIC_FREDDY8 110
+#define PIC_FREDDY9 111
+#define PIC_FREDDY10 112
+#define PIC_FREDDY11 113
+#define PIC_FREDDY12 114
+#define PIC_FREDDY13 115
+#define PIC_FREDDY14 116
+#define PIC_FREDDY15 117
+#define PIC_FREDDY16 118
+#define PIC_FREDDY17 119
+#define PIC_FREDDY18 120
+#define PIC_FREDDY19 121
+#define PIC_FOXY0 122
+#define PIC_FOXY1 123
+#define PIC_FOXY2 124
+#define PIC_FOXY3 125
+#define PIC_FOXY4 126
+#define PIC_FOXY5 127
+#define PIC_FOXY6 128
+#define PIC_FOXY7 129
+#define PIC_FOXY8 130
+#define PIC_FOXY9 131
+#define PIC_FOXY10 132
+#define PIC_FOXY11 133
+#define PIC_FOXY12 134
+#define PIC_FOXY13 135
+#define PIC_FOXY14 136
+#define PIC_FOXY15 137
+#define PIC_FOXY16 138
+#define PIC_FOXY17 139
+#define PIC_FOXY18 140
+#define PIC_POWEROUT0 141
+#define PIC_POWEROUT1 142
+#define PIC_POWEROUT2 143
+#define PIC_POWEROUT3 144
+#define PIC_POWEROUT4 145
+#define PIC_POWEROUT5 146
+#define PIC_POWEROUT6 147
+#define PIC_POWEROUT7 148
+#define PIC_POWEROUT8 149
+#define PIC_POWEROUT9 150
+#define PIC_POWEROUT10 151
+#define PIC_POWEROUT11 152
+#define PIC_POWEROUT12 153
+#define PIC_POWEROUT13 154
+#define PIC_POWEROUT14 155
+#define PIC_POWEROUT15 156
+#define PIC_POWEROUT16 157
+#define PIC_POWEROUT17 158
+#define PIC_POWEROUT18 159
+#define PIC_POWEROUT19 160
+#define PIC_POWEROUT20 161
+#define PIC_FLIP0 162
+#define PIC_FLIP1 163
+#define PIC_FLIP2 164
+#define PIC_FLIP3 165
+#define PIC_FLIP4 166
+#define PIC_FLIP5 167
+#define PIC_FLIP6 168
+#define PIC_FLIP7 169
+#define PIC_FLIP8 170
+#define PIC_FLIP9 171
+#define PIC_FLIP10 172
+#define PIC_HALLUC_GOLDEN 173
+#define PIC_HALLUC_ENDO 174
+#define PIC_TITLE0 175
+#define PIC_TITLE1 176
+#define PIC_TITLE2 177
+#define PIC_TITLE3 178
+#define PIC_NEWSPAPER 179
+#define PIC_PAYCHECK 180
+#define PIC_OVERTIME 181
+#define PIC_PINKSLIP 182
+#define PIC_GAMEOVER 183
+#define PIC_GOLDEN 184
+#define PIC_FACE_FREDDY 185
+#define PIC_FACE_BONNIE 186
+#define PIC_FACE_CHICA 187
+#define PIC_FACE_FOXY 188
+#define PIC_COUNT 189
+#define FONT_HUD 0
+#define FONT_BIG 1
+#define FONT_TEXT 2
+#define FONT_CLOCK 3
+#define FONT_PIXEL 4
+#define FONT_HUGE 5
+#define GRP_OFFICE 0
+#define GRP_CAM1A 1
+#define GRP_CAM1B 2
+#define GRP_CAM2A 3
+#define GRP_CAM2B 4
+#define GRP_CAM3 5
+#define GRP_CAM4A 6
+#define GRP_CAM4B 7
+#define GRP_CAM5 8
+#define GRP_CAM7 9
+#define GRP_CAM1C 10
+#define GRP_BONNIE 11
+#define GRP_CHICA 12
+#define GRP_FREDDY 13
+#define GRP_FOXY 14
+#define GRP_POWEROUT 15
+#define GRP_FLIP 16
+#define GRP_HALLUC 17
+#define GRP_TITLE 18
+#define GRP_NEWS 19
+#define GRP_PAPER 20
+#define GRP_GAMEOVER 21
+#define GRP_GOLDEN 22
+#define GRP_FACES 23
+#define GRP_COUNT 24
+#define UI_BLACK 224
+#define UI_WHITE 225
+#define UI_WHITE2 226
+#define UI_WHITE1 227
+#define UI_GREY 228
+#define UI_GREY2 229
+#define UI_GREY1 230
+#define UI_SILVER 231
+#define UI_DIM 232
+#define UI_CAPTION 233
+#define UI_CAPTION2 234
+#define UI_CAPTION1 235
+#define UI_CALL 236
+#define UI_CALL2 237
+#define UI_CALL1 238
+#define UI_MINT 239
+#define UI_MINT2 240
+#define UI_MINT1 241
+#define UI_REC 242
+#define UI_BAR_G 243
+#define UI_BAR_Y 244
+#define UI_BAR_R 245
+#define UI_BAR_GL 246
+#define UI_BAR_YL 247
+#define UI_BAR_RL 248
+#define UI_MAP_ON 249
+#define UI_MAP_LINE 250
+#define UI_FRAME 251
+#define UI_BOX 252
+#define UI_EDGE 253
+#define UI_MAP_OFF 254
+#define UI_SPARE 255
+#define DOOR_L_X 27
+#define DOOR_L_W 70
+#define DOOR_L_H 239
+#define DOOR_R_X 434
+#define DOOR_R_W 60
+#define DOOR_R_H 239
+#define PANEL_L_X 0
+#define PANEL_R_X 496
+#define PANEL_Y 96
+#define FAN_X 256
+#define FAN_Y 96
+#define GOLD_X 176
+#define GOLD_Y 128
+#define GOLD_W 176
+#define GOLD_H 112
+#define MAP_SIZE 133
+#define OFFICE_W 533
+#define LIGHT_L_X 32
+#define LIGHT_L_Y 16
+#define LIGHT_L_W 56
+#define LIGHT_L_H 104
+#define LIGHT_R_X 368
+#define LIGHT_R_Y 24
+#define LIGHT_R_W 128
+#define LIGHT_R_H 96
+#define DARK_FREDDY_X 32
+#define DARK_FREDDY_Y 32
+#define DARK_FREDDY_W 56
+#define DARK_FREDDY_H 32
+#define FOXYRUN_N 18
+#define BONNIE_N 10
+#define CHICA_N 15
+#define FREDDY_N 20
+#define FOXY_N 19
+#define POWEROUT_N 21
+#define FLIP_N 11
+#define TITLE1_X 112
+#define TITLE1_Y 0
+#define TITLE1_W 208
+#define TITLE1_H 120
+#define TITLE2_X 112
+#define TITLE2_Y 0
+#define TITLE2_W 208
+#define TITLE2_H 120
+#define TITLE3_X 112
+#define TITLE3_Y 0
+#define TITLE3_W 192
+#define TITLE3_H 120
+/* each picture's group, and where it goes (pixels) */
+static const uint8_t pic_group[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 18, 18, 18, 18, 19, 20, 20, 20, 21, 22, 23, 23, 23, 23};
+/* each asset: its appvar << 16 | its offset */
+static const uint32_t asset_at[] = {0x0, 0x1E5, 0xAEF, 0xF17, 0x12CE, 0x1634, 0x1A35, 0x1E0C, 0x21E9, 0x2613, 0x2945, 0x2CD7, 0x30A8, 0x38FE, 0x41C7, 0x4942, 0x5174, 0x5A37, 0x606B, 0x6946, 0x6D53, 0x75C6, 0x7E70, 0x8187, 0x86EB, 0x9048, 0xB99D, 0xBEE2, 0xC2D6, 0xCB69, 0xCF16, 0xD6B9, 0xDE20, 0xDFB4, 0xE169, 0xE326, 0xE502, 0xE6C0, 0xE89D, 0xEA7E, 0xEC75, 0xEE5F, 0xF04C, 0x10000, 0x10DDD, 0x125F8, 0x12732, 0x13801, 0x14BCD, 0x1669E, 0x1782E, 0x1A12F, 0x1BD26, 0x1C27F, 0x1C776, 0x1C7B9, 0x1D2D9, 0x1E258, 0x20000, 0x210A8, 0x23241, 0x23F5E, 0x250F8, 0x25C69, 0x25F27, 0x2614D, 0x263F0, 0x266BB, 0x26B0D, 0x26F66, 0x2743D, 0x2797D, 0x28064, 0x287DF, 0x290C6, 0x29BBF, 0x2A6F7, 0x2B17E, 0x2BD75, 0x2C8B7, 0x2D3DB, 0x30000, 0x31496, 0x31825, 0x331D7, 0x339FC, 0x355D4, 0x376B3, 0x38ED4, 0x3AF7A, 0x3CE4C, 0x3E708, 0x40000, 0x41C7A, 0x43196, 0x4551B, 0x46509, 0x477D8, 0x49111, 0x4B060, 0x4C119, 0x4D5F3, 0x4E9B2, 0x4F354, 0x50000, 0x509CB, 0x51397, 0x51CEA, 0x524EA, 0x52D13, 0x534C3, 0x53D20, 0x5464D, 0x55397, 0x5610E, 0x56E66, 0x57B26, 0x587E4, 0x593F0, 0x5A069, 0x5AC1B, 0x5B8AE, 0x5C609, 0x5D479, 0x5E1ED, 0x5EF48, 0x60000, 0x60D5E, 0x61796, 0x619F4, 0x61FB4, 0x626FD, 0x62DA2, 0x63416, 0x63A68, 0x63F99, 0x64494, 0x649CF, 0x6504C, 0x65700, 0x65B01, 0x66124, 0x666F5, 0x66C21, 0x670A5, 0x67553, 0x67A66, 0x68032, 0x68B46, 0x68F0E, 0x69322, 0x6979E, 0x69C98, 0x6A216, 0x6A7D0, 0x6AD56, 0x6B2BA, 0x6B71A, 0x6BBD1, 0x6C038, 0x6C4CD, 0x6C961, 0x6CDEF, 0x6CDFF, 0x6CE08, 0x6CE11, 0x6CE1A, 0x6D142, 0x6D1F8, 0x6D437, 0x6D888, 0x6DDFF, 0x6E3CE, 0x6EC39, 0x70000, 0x709AC, 0x71309, 0x71C7A, 0x72618, 0x72F04, 0x738E9, 0x742F8, 0x74DE8, 0x7587D, 0x7622B, 0x76B9D, 0x773F5, 0x77DF6, 0x78168, 0x784C7, 0x78830, 0x78C00, 0x79003, 0x79406, 0x797CC, 0x79BEE, 0x7A037, 0x7A3C3, 0x7A6E3, 0x7BD60, 0x7C5EA, 0x7D347, 0x7E1C5, 0x80000, 0x80F13, 0x866D2, 0x8B237, 0x90000, 0x949C6, 0x96635, 0x9C498, 0x9CE82, 0x9D77A, 0x9E280, 0x9EAD2, 0xA0000, 0xA0FCC, 0xA1852, 0xA1DDB, 0xA22D1, 0xA3E8F, 0xA48ED, 0xA496F};
+
+#endif
diff --git a/games/fnaf/ce/src/fnaf.h b/games/fnaf/ce/src/fnaf.h
new file mode 100644
index 00000000..6bec7588
--- /dev/null
+++ b/games/fnaf/ce/src/fnaf.h
@@ -0,0 +1,43 @@
+/* Five Nights at Freddy's on the TI-84 Plus CE: what its files share. The game itself (the nights, the
+ * animatronics, the scenes' steps) is the NumWorks version's (../src: game.c, night.c, calls.c); this version
+ * draws it on the CE's 8-bit screen with the pictures of tools/pack.py. */
+#ifndef FNAF_CE_H
+#define FNAF_CE_H
+#include "../../src/game.h"
+#include "../../src/plat.h"
+#include "../engine/gfx.h"
+#include "assets.h"
+
+/* the RAM: scene (the office, a camera, the title, an animation's frames) and what is left after it */
+#define SCENE_W 536
+#define SCENE_H 120
+#define RAM_NEEDED (SCENE_W * SCENE_H + 26 * 1024)
+extern uint8_t *scene, *extra;
+extern int scene_pic; /* the picture scene holds, -1 none */
+void scene_want(int pic); /* scene holds pic: its bases (or the frames before it) decoded first as needed */
+extern int extra_pic; /* what extra holds (a picture: the monitor's frame, a twitch...; or EXTRA_*) */
+#define EXTRA_FACES (-2) /* the custom night's four faces */
+void extra_want(int pic, int stride);
+
+/* a picture shown as it is on both screens (the newspaper, the paychecks...): true when they hold it already;
+ * else it is drawn now (and kept for the next frame) */
+bool still(int pic);
+void still_forget(void);
+extern bool keep_frame; /* the next ce_show keeps the frame */
+
+/* the inks of the fixed colours */
+extern const uint8_t INK_WHITE[4], INK_GREY[4], INK_SILVER[4], INK_DIM[4], INK_CAPTION[4], INK_CALL[4], INK_MINT[4],
+ INK_BLACK[4];
+/* an ink fading out: a from 0 (gone) to 32 */
+Ink ink_fade(Ink ink, int a);
+
+/* view.c */
+void view_paused(void);
+const uint8_t *static_phases(int a); /* the static's four phases this frame, from its strength (of 32) */
+void view_reset(void); /* a new night: the office's pictures again */
+int office_pan(void);
+/* save.c */
+void save_open(void);
+/* keys: the CE's as the game's (plat.h) */
+uint32_t fnaf_keys(void);
+#endif
diff --git a/games/fnaf/ce/src/main.c b/games/fnaf/ce/src/main.c
new file mode 100644
index 00000000..0b6c76c3
--- /dev/null
+++ b/games/fnaf/ce/src/main.c
@@ -0,0 +1,137 @@
+/* Five Nights at Freddy's for the TI-84 Plus CE.
+ *
+ * Inspired by Five Nights at Freddy's by Scott Cawthon, not affiliated with him.
+ * Made by Mason Chen as part of NumPlay. */
+#include <string.h>
+#include "fnaf.h"
+
+static const uint8_t *vars[DATA_VARS];
+uint8_t *scene, *extra;
+int scene_pic = -1, extra_pic = -1;
+bool keep_frame;
+static int still_pic = -1;
+
+const uint8_t INK_WHITE[4] = {0, UI_WHITE1, UI_WHITE2, UI_WHITE};
+const uint8_t INK_GREY[4] = {0, UI_GREY1, UI_GREY2, UI_GREY};
+const uint8_t INK_SILVER[4] = {0, UI_GREY1, UI_GREY2, UI_SILVER};
+const uint8_t INK_DIM[4] = {0, UI_GREY1, UI_GREY1, UI_DIM};
+const uint8_t INK_CAPTION[4] = {0, UI_CAPTION1, UI_CAPTION2, UI_CAPTION};
+const uint8_t INK_CALL[4] = {0, UI_CALL1, UI_CALL2, UI_CALL};
+const uint8_t INK_MINT[4] = {0, UI_MINT1, UI_MINT2, UI_MINT};
+const uint8_t INK_BLACK[4] = {0, UI_BLACK, UI_BLACK, UI_BLACK};
+
+Ink ink_fade(Ink ink, int a) {
+ static uint8_t out[4];
+ if (a >= 32) return ink;
+ if (a < 6) return INK_BLACK;
+ /* each level one step down, or two: what is left of the ink over black */
+ int k = a >= 20 ? 1 : 2;
+ out[0] = 0;
+ for (int i = 1; i <= 3; i++) out[i] = i - k >= 1 ? ink[i - k] : UI_BLACK;
+ return out;
+}
+
+/* ------------------------------------------------------------------ pictures in RAM */
+void scene_want(int pic) {
+ if (scene_pic == pic) return;
+ /* the frames (or base) it is coded over: from the one scene holds, if it is among them */
+ int chain[24], n = 0;
+ for (int p = pic; p >= 0 && n < 24; p = pic_base(asset(PIC_FIRST + p))) {
+ chain[n++] = p;
+ if (p == scene_pic) break;
+ }
+ int from = n - 1;
+ if (chain[from] == scene_pic) from--;
+ scene_pic = -1;
+ for (int k = from; k >= 0; k--) pic_into(chain[k], scene, SCENE_W);
+ scene_pic = pic;
+}
+
+void extra_want(int pic, int stride) {
+ if (extra_pic == pic) return;
+ pic_full(pic, extra, stride);
+ extra_pic = pic;
+}
+
+bool still(int pic) {
+ if (still_pic == pic) return true;
+ pic_full(pic, scr, SCR_W);
+ still_pic = pic;
+ keep_frame = true;
+ return false;
+}
+
+void still_forget(void) { still_pic = -1; }
+
+/* ------------------------------------------------------------------ the keys, the time */
+uint32_t fnaf_keys(void) {
+ uint32_t c = ce_keys(), k = 0;
+ if (c & CK_LEFT) k |= K_LEFT;
+ if (c & CK_RIGHT) k |= K_RIGHT;
+ if (c & CK_UP) k |= K_UP;
+ if (c & CK_DOWN) k |= K_DOWN;
+ if (c & (CK_2ND | CK_ENTER)) k |= K_OK;
+ if (c & CK_CLEAR) k |= K_BACK;
+ if (c & (CK_MODE | CK_ON)) k |= K_HOME;
+ if (c & CK_DEL) k |= K_DEL;
+ for (int n = 0; n < 10; n++)
+ if (c & CK_DIGIT(n)) k |= K_DIGIT(n);
+ if (c) k |= K_ANY;
+ return k;
+}
+
+uint32_t plat_millis(void) {
+ uint32_t t = ce_ticks();
+ return (t >> 15) * 1000 + ((t & 32767) * 1000 >> 15);
+}
+
+/* ------------------------------------------------------------------ starting */
+static const Assets assets = {vars, asset_at, pic_group, PIC_FIRST, MAP_FIRST, FONT_FIRST};
+
+/* 0: ready; 1: data missing (ce_missing); 2: not enough RAM */
+int fnaf_start(void) {
+ if (!ce_data(DATA_VAR, DATA_VARS, vars)) return 1;
+ save_open(); /* (before the RAM is taken: the save is made there) */
+ size_t n;
+ scene = ce_ram(&n);
+ if (n < RAM_NEEDED) return 2;
+ extra = scene + SCENE_W * SCENE_H;
+ gfx_init(&assets, ASSET_TABLES);
+ game_init();
+ return 0;
+}
+
+#ifndef HOST
+void ce_message(const char *a, const char *b);
+
+int main(void) {
+ ce_init();
+ int r = fnaf_start();
+ if (r) {
+ ce_message(r == 1 ? "Missing appvar:" : "Not enough RAM.", r == 1 ? ce_missing : "Archive some files.");
+ return 0;
+ }
+ uint32_t last = ce_ticks(), acc = 0;
+ while (g.running) {
+ /* the game's steps, 60 a second (the 32768 Hz clock), then a frame */
+ uint32_t now = ce_ticks();
+ acc += (now - last) * 60;
+ last = now;
+ int steps = 0;
+ while (acc >= 32768 && steps < 8 && g.running) {
+ game_step(fnaf_keys());
+ acc -= 32768;
+ steps++;
+ }
+ if (acc > 32768 * 8) acc = 0; /* far behind: no rush */
+ if (!g.running) break;
+ if (steps) {
+ game_draw();
+ ce_show(keep_frame);
+ keep_frame = false;
+ }
+ }
+ ce_quit();
+ return 0;
+}
+#endif
diff --git a/games/fnaf/ce/src/rules.c b/games/fnaf/ce/src/rules.c
new file mode 100644
index 00000000..1287a870
--- /dev/null
+++ b/games/fnaf/ce/src/rules.c
@@ -0,0 +1,3 @@
+/* The game's rules: the NumWorks version's, as they are (the nights, the animatronics, the power, the calls). */
+#include "../../src/night.c"
+#include "../../src/calls.c"
diff --git a/games/fnaf/ce/src/save.c b/games/fnaf/ce/src/save.c
new file mode 100644
index 00000000..ec172248
--- /dev/null
+++ b/games/fnaf/ce/src/save.c
@@ -0,0 +1,78 @@
+/* Saves: the appvar NPfnaf (NumPlay's name for the game's saves), of a fixed size: the progress (the night
+ * reached, the stars, the custom night's levels), then the night as Home left it (read once on the next start).
+ * It stays in RAM while the game runs (written where it is) and is archived when the game ends. The same
+ * functions as the NumWorks version's save.c, for game.c. */
+#include <string.h>
+#include "fnaf.h"
+
+#define MAGIC 0x46414E46u /* "FNAF" */
+#define VERSION 1
+
+typedef struct {
+ uint32_t magic;
+ uint8_t version, reached, stars, keys_seen;
+ int8_t custom[4];
+} Progress;
+
+typedef struct {
+ uint32_t magic;
+ uint32_t size;
+ Night n;
+} Resume;
+
+typedef struct {
+ Progress p;
+ Resume r;
+} Save;
+
+static Save *save;
+static Save fallback; /* (no room for the appvar: the game still runs, without saving) */
+
+void save_open(void) {
+ save = (Save *)(void *)ce_save_open("NPfnaf", sizeof(Save));
+ if (!save) save = &fallback;
+}
+
+void save_restore_copy(void) {}
+
+void save_progress(void) {
+ Progress p;
+ memset(&p, 0, sizeof p);
+ p.magic = MAGIC, p.version = VERSION;
+ p.reached = g.reached, p.stars = g.stars, p.keys_seen = g.keys_seen;
+ memcpy(p.custom, g.custom, sizeof p.custom);
+ memcpy(&save->p, &p, sizeof p);
+ ce_save_done();
+}
+
+void save_load(void) {
+ Progress p;
+ memcpy(&p, &save->p, sizeof p);
+ g.reached = 1;
+ if (p.magic != MAGIC || p.version != VERSION) return;
+ g.reached = p.reached >= 1 && p.reached <= 5 ? p.reached : 1;
+ g.stars = p.stars & 7;
+ g.keys_seen = p.keys_seen;
+ for (int k = 0; k < 4; k++) g.custom[k] = p.custom[k] >= 0 && p.custom[k] <= 20 ? p.custom[k] : 0;
+}
+
+void save_resume(void) {
+ Resume r;
+ memset(&r, 0, sizeof r);
+ r.magic = MAGIC, r.size = sizeof(Night), r.n = g.n;
+ memcpy(&save->r, &r, sizeof r);
+ ce_save_done();
+}
+
+bool save_resume_load(void) {
+ Resume r;
+ memcpy(&r, &save->r, sizeof r);
+ const Night *n = &r.n;
+ bool ok = r.magic == MAGIC && r.size == sizeof(Night) && n->night >= 1 && n->night <= 7 && n->hour < 6 &&
+ n->cam < CAM_COUNT && n->light <= 2 && n->foxy <= 3 && n->gold <= 4 && n->dark <= 3 &&
+ n->pos[0] <= IN_OFFICE && n->pos[1] <= IN_OFFICE && n->pos[2] <= IN_OFFICE;
+ if (ok) g.n = r.n;
+ memset(&save->r, 0, sizeof save->r);
+ ce_save_done();
+ return ok;
+}
diff --git a/games/fnaf/ce/src/scenes.c b/games/fnaf/ce/src/scenes.c
new file mode 100644
index 00000000..7b83c191
--- /dev/null
+++ b/games/fnaf/ce/src/scenes.c
@@ -0,0 +1,213 @@
+/* The scenes around the nights on the CE: game.c's steps (the NumWorks version's, as they are), and drawings of
+ * the CE's own for the title, the newspaper, how to play, the night's card, the pause, 6 AM, the paychecks, the
+ * game over, Golden Freddy and the custom night. */
+#define FNAF_CE 1
+#include "fnaf.h"
+#include "../../src/game.c"
+
+
+static int fade(float t, float in, float hold, float out) { /* 0 black to 32 */
+ float a = t < in ? t / in : t < hold ? 1 : t < hold + out ? 1 - (t - hold) / out : 0;
+ return (int)(a * 32);
+}
+
+/* ------------------------------------------------------------------ the title */
+
+static void title_draw(void) {
+ static const int faces[4][5] = {{0}, {PIC_TITLE1, TITLE1_X, TITLE1_Y, TITLE1_W, TITLE1_H},
+ {PIC_TITLE2, TITLE2_X, TITLE2_Y, TITLE2_W, TITLE2_H},
+ {PIC_TITLE3, TITLE3_X, TITLE3_Y, TITLE3_W, TITLE3_H}};
+ scene_want(PIC_TITLE0);
+ d_blit(scene, SCENE_W, 0, 0, SCR_W, SCENE_H, 0, 0, true);
+ if (face) {
+ const int *fc = faces[face];
+ extra_want(fc[0], fc[3]);
+ d_blit(extra, fc[3], 0, 0, fc[3], fc[4], fc[1], 2 * fc[2], true);
+ }
+ int grp = pic_group_of(PIC_TITLE0);
+ pal_set(grp, face_light, static_phases(7), 0);
+ /* the slow light band going down, and lines that flash */
+ const uint8_t *light = map_remap(grp, 1);
+ int sy = (int)(g.t / 20 * (SCR_H + 10)) % (SCR_H + 10) - 10;
+ for (int r = 1; r < 10; r += 4) d_remap(0, sy + r, SCR_W, 1, light);
+ if ((int)(g.t * 10) % 7 == 0) d_remap(0, (int)(g.t * 997) % SCR_H, SCR_W, 1, light);
+ d_img(asset(ASSET_IMG_TITLE), 20, 18, INK_WHITE);
+ for (int i = 0; i < 3; i++)
+ if (g.stars >> i & 1) d_img(asset(ASSET_IMG_STAR), 20 + 27 * i, 132, INK_WHITE);
+ static const char *const items[] = {"New Game", "Continue", "6th Night", "Custom Night"};
+ int y0 = g.stars ? 160 : 150;
+ for (int i = 0; i < g.menu_n; i++) {
+ int y = y0 + 18 * i;
+ d_text(FONT_BIG, 44, y, items[i], INK_WHITE);
+ if (g.menu == i) d_text(FONT_BIG, 20, y, ">>", INK_WHITE);
+ }
+ if (g.menu == M_CONTINUE) {
+ char s[] = "Night 0";
+ s[6] = (char)('0' + g.reached);
+ d_text(FONT_HUD, 44, y0 + 18 * g.menu_n + 4, s, INK_WHITE);
+ }
+}
+
+/* ------------------------------------------------------------------ the newspaper, how to play, the card */
+static void news_draw(void) {
+ still(PIC_NEWSPAPER);
+ pal_set(pic_group_of(PIC_NEWSPAPER), fade(g.t, 2, 7, 2), NULL, 0);
+}
+
+static void keys_draw(void) {
+ static const char *const rows[][2] = {
+ {"In the office", NULL},
+ {"4 6, < >", "look left and right"},
+ {"7 1", "left door, left light"},
+ {"9 3", "right door, right light"},
+ {"2nd, 8", "the monitor up and down"},
+ {"0", "mute the call"},
+ {"On the monitor", NULL},
+ {"1 to 7", "a camera (again: 1B, 1C...)"},
+ {"arrows", "around the map"},
+ {"clear", "pause"},
+ };
+ d_fill(0, 0, SCR_W, SCR_H, UI_BLACK);
+ d_text_c(FONT_BIG, 160, 8, "How to survive", INK_WHITE);
+ int y = 34;
+ for (unsigned i = 0; i < sizeof rows / sizeof rows[0]; i++) {
+ if (!rows[i][1]) {
+ d_text(FONT_HUD, 16, y + 2, rows[i][0], INK_MINT);
+ y += 17;
+ continue;
+ }
+ d_text(FONT_HUD, 26, y, rows[i][0], INK_WHITE);
+ d_text(FONT_TEXT, 108, y, rows[i][1], INK_GREY);
+ y += 15;
+ }
+ d_text_c(FONT_TEXT, 160, 196, "Watch the power: every door, light and", INK_GREY);
+ d_text_c(FONT_TEXT, 160, 209, "camera drains it. Survive until 6 AM.", INK_GREY);
+ if ((int)(g.t * 2) & 1) d_text_c(FONT_HUD, 160, 226, "2nd", INK_WHITE);
+ pal_set(-1, 32, NULL, 0);
+}
+
+static const char *const nth[] = {"", "1st Night", "2nd Night", "3rd Night", "4th Night", "5th Night", "6th Night",
+ "7th Night"};
+
+static void intro_draw(void) {
+ d_fill(0, 0, SCR_W, SCR_H, UI_BLACK);
+ if (g.t < 0.15f) d_noise(0, 0, SCR_W, SCR_H, rnd(), (const uint8_t[8]){UI_BLACK, UI_BOX, UI_EDGE, UI_BLACK,
+ UI_GREY1, UI_BOX, UI_BLACK, UI_EDGE});
+ d_text_c(FONT_HUGE, 160, 92, "12:00 AM", INK_WHITE);
+ d_text_c(FONT_BIG, 160, 124, nth[g.n.night], INK_WHITE);
+ pal_set(-1, 32, NULL, 0);
+ if (g.t > 1.2f) pal_ui((int)((2.2f - g.t) * 32));
+}
+
+/* ------------------------------------------------------------------ the night, paused */
+static void pause_draw(void) {
+ view_paused();
+ if (g.confirm) {
+ d_text_c(FONT_BIG, 160, 84, "Quit this night?", INK_WHITE);
+ d_text_c(FONT_TEXT, 160, 106, "It starts over next time.", INK_SILVER);
+ d_text(FONT_BIG, 104, 132, "No", INK_WHITE);
+ d_text(FONT_BIG, 190, 132, "Yes", INK_WHITE);
+ d_text(FONT_BIG, g.pause_item ? 168 : 82, 132, ">>", INK_WHITE);
+ return;
+ }
+ d_text_c(FONT_HUGE, 160, 62, "Paused", INK_WHITE);
+ static const char *const items[] = {"Resume", "How to play", "Quit night"};
+ for (int i = 0; i < 3; i++) {
+ d_text(FONT_BIG, 112, 104 + 20 * i, items[i], INK_WHITE);
+ if (g.pause_item == i) d_text(FONT_BIG, 88, 104 + 20 * i, ">>", INK_WHITE);
+ }
+}
+
+/* ------------------------------------------------------------------ 6 AM, the paychecks */
+static void six_draw(void) {
+ d_fill(0, 0, SCR_W, SCR_H, UI_BLACK);
+ /* 5 rolls up into 6 */
+ float r = g.t < 1 ? 0 : g.t > 6 ? 1 : (g.t - 1) / 5;
+ int cy = 104, dh = 30, off = (int)(r * dh);
+ d_text(FONT_HUGE, 118, cy - off, "5", INK_WHITE);
+ d_text(FONT_HUGE, 118, cy + dh - off, "6", INK_WHITE);
+ d_fill(110, cy - dh, 30, dh, UI_BLACK); /* (only the window between shows) */
+ d_fill(110, cy + dh, 30, dh, UI_BLACK);
+ d_text(FONT_HUGE, 148, cy, "AM", INK_WHITE);
+ d_text_c(FONT_TEXT, 160, 170, g.t > 6.2f ? "*children cheering*" : "*chimes*", INK_SILVER);
+ pal_set(-1, 32, NULL, 0);
+ pal_ui(fade(g.t, 1, 10.5f, 1));
+}
+
+static void paper_draw(void) {
+ static const int pics[3] = {PIC_PAYCHECK, PIC_OVERTIME, PIC_PINKSLIP};
+ still(pics[g.paper]);
+ pal_set(pic_group_of(pics[g.paper]), fade(g.t, 2, 17, 2), NULL, 0);
+}
+
+/* ------------------------------------------------------------------ the end of a night */
+static const uint8_t GREYS[8] = {UI_BLACK, UI_WHITE1, UI_GREY1, UI_SPARE, UI_DIM, UI_GREY2, UI_EDGE, UI_BOX};
+
+static void over_draw(void) {
+ if (g.t < 2.5f) {
+ still_forget();
+ d_noise(0, 0, SCR_W, SCR_H, rnd(), GREYS);
+ pal_set(-1, 32, NULL, 0);
+ return;
+ }
+ if (!still(PIC_GAMEOVER)) d_text_r(FONT_HUGE, 306, 200, "Game Over", INK_WHITE);
+ pal_set(pic_group_of(PIC_GAMEOVER), 32, static_phases(g.t < 3 ? 26 : 6), 0);
+}
+
+static void golden_draw(void) {
+ still(PIC_GOLDEN);
+ pal_set(pic_group_of(PIC_GOLDEN), 32, static_phases(6 + (int)(rnd() % 8)), 0);
+}
+
+/* ------------------------------------------------------------------ the custom night */
+static const int face_pics[4] = {PIC_FACE_FREDDY, PIC_FACE_BONNIE, PIC_FACE_CHICA, PIC_FACE_FOXY};
+static const char *const names[4] = {"Freddy", "Bonnie", "Chica", "Foxy"};
+
+static void custom_draw(void) {
+ /* the faces, in extra once */
+ if (extra_pic != EXTRA_FACES) {
+ for (int i = 0; i < 4; i++) pic_full(face_pics[i], extra + 64 * 64 * i, 64);
+ extra_pic = EXTRA_FACES;
+ }
+ d_fill(0, 0, SCR_W, SCR_H, UI_BLACK);
+ d_text_c(FONT_BIG, 160, 14, "Customize Night", INK_WHITE);
+ for (int i = 0; i < 4; i++) {
+ int x = 12 + 76 * i, y = 52;
+ d_blit(extra + 64 * 64 * i, 64, 0, 0, 64, 64, x, y, false);
+ d_frame(x - 2, y - 2, 68, 68, g.custom_sel == i ? UI_WHITE : UI_EDGE);
+ d_text_c(FONT_HUD, x + 32, y + 72, names[i], INK_WHITE);
+ d_text_c(FONT_TEXT, x + 32, y + 88, "A.I. Level", INK_SILVER);
+ char s[3] = {(char)('0' + g.custom[i] / 10), (char)('0' + g.custom[i] % 10), 0};
+ d_text_c(FONT_HUGE, x + 32, y + 104, g.custom[i] >= 10 ? s : s + 1, INK_WHITE);
+ if (g.custom_sel == i) {
+ d_text(FONT_BIG, x + 2, y + 106, "<", INK_WHITE);
+ d_text(FONT_BIG, x + 52, y + 106, ">", INK_WHITE);
+ }
+ }
+ d_text(FONT_TEXT, 12, 214, "< > pick up down: level", INK_DIM);
+ d_text_r(FONT_BIG, 308, 208, "Ready", (int)(g.t * 2) & 1 ? INK_WHITE : INK_GREY);
+ pal_set(pic_group_of(PIC_FACE_FREDDY), 32, NULL, 0);
+}
+
+/* ------------------------------------------------------------------ a frame */
+void game_draw(void) {
+ switch (g.scene) {
+ case SC_NEWS: news_draw(); return;
+ case SC_PAPER: paper_draw(); return;
+ case SC_GOLDEN: golden_draw(); return;
+ case SC_OVER: over_draw(); return;
+ }
+ still_forget();
+ switch (g.scene) {
+ case SC_TITLE: title_draw(); break;
+ case SC_KEYS: keys_draw(); break;
+ case SC_INTRO: intro_draw(); break;
+ case SC_NIGHT:
+ if (g.paused) pause_draw();
+ else view_night();
+ break;
+ case SC_SCARE: view_scare(); break;
+ case SC_SIX: six_draw(); break;
+ case SC_CUSTOM: custom_draw(); break;
+ }
+}
diff --git a/games/fnaf/ce/src/view.c b/games/fnaf/ce/src/view.c
new file mode 100644
index 00000000..80c7b968
--- /dev/null
+++ b/games/fnaf/ce/src/view.c
@@ -0,0 +1,386 @@
+/* Drawing a night on the CE: the office (turning, its lights, doors, buttons and fan), the monitor and its
+ * cameras (the feed's slow turn, its static, the map), and around them the clock, the power, the call's subtitles
+ * and the captions of what can be heard. As the NumWorks version's view.c, with the CE's pictures: the office and
+ * the cameras at half height in RAM (scene), drawn with each row twice. */
+#include <stddef.h>
+#include <string.h>
+#include "fnaf.h"
+
+#define N g.n
+
+/* the office's small pictures, decoded once */
+#define DOOR_LW 72
+#define DOOR_RW 64
+#define PATCH_MAX (LIGHT_R_W * LIGHT_R_H > LIGHT_L_W * LIGHT_L_H ? LIGHT_R_W * LIGHT_R_H : LIGHT_L_W * LIGHT_L_H)
+static uint8_t door_l[DOOR_LW * 120], door_r[DOOR_RW * 120];
+static uint8_t panel_l[4][32 * 40], panel_r[4][48 * 40], fans[3][48 * 24];
+static uint8_t patch[PATCH_MAX > DARK_FREDDY_W * DARK_FREDDY_H ? PATCH_MAX : DARK_FREDDY_W * DARK_FREDDY_H];
+static bool office_ready;
+static int patch_pic = -1;
+
+void view_reset(void) { patch_pic = -1; }
+
+static void want_patch(int pic, int w) {
+ if (patch_pic == pic) return;
+ int base = pic_base(asset(PIC_FIRST + pic));
+ if (base >= 0 && patch_pic != base) pic_into(base, patch, w);
+ pic_into(pic, patch, w);
+ patch_pic = pic;
+}
+
+
+static void office_pictures(void) {
+ if (office_ready) return;
+ pic_full(PIC_DOOR_L, door_l, DOOR_LW);
+ pic_full(PIC_DOOR_R, door_r, DOOR_RW);
+ for (int k = 0; k < 4; k++) {
+ pic_full(PIC_PANEL_L0 + k, panel_l[k], 32);
+ pic_full(PIC_PANEL_R0 + k, panel_r[k], 48);
+ }
+ for (int k = 0; k < 3; k++) pic_full(PIC_FAN0 + k, fans[k], 48);
+ office_ready = true;
+}
+
+/* chosen once a frame */
+static struct {
+ bool light_off; /* the door light flickers */
+ bool hall_lit; /* 2A's light flickers on */
+ int fan;
+ int halluc; /* 0, or what flashes: 1 Golden Freddy, 2 the endoskeleton, 3 IT'S ME */
+ int freddy_face; /* in the dark */
+ int noise, shake;
+ uint32_t seed;
+} f;
+
+static void prepare(void) {
+ f.light_off = rnd() % 10 <= 1;
+ f.hall_lit = rnd() % 10 < 3;
+ f.fan = (int)(N.t * 30) % 3;
+ f.halluc = N.halluc > 0 && rnd() % 10 == 0 ? 1 + (int)(rnd() % 3) : 0;
+ f.freddy_face = rnd() % 4 == 0;
+ f.noise = 4 + (int)(rnd() % 4) + (rnd() % 16 == 0 ? 6 : 0);
+ f.shake = (int)(rnd() % 7) - 3;
+ f.seed = rnd();
+}
+
+/* the static's four phases, from its strength (of 32): each a share of it, toward a grey of its own (the
+ * NumWorks version's static: each pixel toward a random grey, 0 to 143) */
+const uint8_t *static_phases(int a) {
+ static uint8_t ph[8];
+ for (int k = 0; k < 4; k++) {
+ uint32_t r = rnd();
+ ph[k] = (uint8_t)(a * (int)(r % 5) / 4);
+ ph[4 + k] = (uint8_t)(r >> 8 & 15);
+ }
+ return ph;
+}
+
+static const uint8_t GREYS[8] = {UI_BLACK, UI_WHITE1, UI_GREY1, UI_SPARE, UI_DIM, UI_GREY2, UI_EDGE, UI_BOX};
+static const uint8_t DARK_GREYS[8] = {UI_BLACK, UI_BLACK, UI_BOX, UI_BLACK, UI_EDGE, UI_BLACK, UI_BOX, UI_GREY1};
+
+/* ------------------------------------------------------------------ text helpers */
+static void small_digits(int font, int x, int y, int v, Ink ink) {
+ char s[4];
+ int n = 0;
+ if (v >= 100) s[n++] = (char)('0' + v / 100);
+ if (v >= 10) s[n++] = (char)('0' + v / 10 % 10);
+ s[n++] = (char)('0' + v % 10);
+ s[n] = 0;
+ d_text(font, x, y, s, ink);
+}
+
+/* a 3 x 5 font for the map's buttons: 1 to 7, A, B, C */
+static const uint16_t tiny[10] = {0x2C97, 0x73E7, 0x73CF, 0x5BC9, 0x79CF, 0x79EF, 0x7249, 0x2BED, 0x6BAE, 0x3923};
+static void tiny_text(int x, int y, const char *s, uint8_t c) {
+ for (; *s; s++, x += 4) {
+ int k = *s >= '1' && *s <= '7' ? *s - '1' : *s - 'A' + 7;
+ uint16_t b = tiny[k];
+ for (int r = 0; r < 5; r++)
+ for (int col = 0; col < 3; col++)
+ if (b >> (14 - r * 3 - col) & 1) scr[(y + r) * SCR_W + x + col] = c;
+ }
+}
+
+/* wraps s to lines of at most w pixels: the start and length of line k, false past the last */
+static bool wrap(int font, const char *s, int w, int k, const char **start, int *len) {
+ for (;;) {
+ while (*s == ' ') s++;
+ if (!*s) return false;
+ int n = 0, last_space = -1, width = 0;
+ while (s[n]) {
+ int cw = d_text_wn(font, s + n, 1);
+ if (width + cw > w) break;
+ if (s[n] == ' ') last_space = n;
+ width += cw;
+ n++;
+ }
+ if (s[n] && last_space > 0) n = last_space;
+ if (!k--) {
+ *start = s, *len = n;
+ return true;
+ }
+ s += n;
+ }
+}
+
+static void text_shadow(int font, int x, int y, const char *s, int n, Ink ink) {
+ d_text_n(font, x + 1, y + 1, s, n, INK_BLACK);
+ d_text_n(font, x, y, s, n, ink);
+}
+
+/* ------------------------------------------------------------------ around the view */
+static void clock_and_night(void) {
+ char h[3] = {'1', '2', 0}; /* 12 AM, then 1 to 5 */
+ if (N.hour) h[0] = (char)('0' + N.hour), h[1] = 0;
+ int x = 310 - d_text_w(FONT_BIG, " AM") - d_text_w(FONT_CLOCK, h);
+ x += d_text(FONT_CLOCK, x, 6, h, INK_WHITE);
+ d_text(FONT_BIG, x, 8, " AM", INK_WHITE);
+ int nx = 310 - d_text_w(FONT_HUD, "Night ") - d_text_w(FONT_PIXEL, "0");
+ nx += d_text(FONT_HUD, nx, 31, "Night ", INK_WHITE);
+ small_digits(FONT_PIXEL, nx, 30, N.night, INK_WHITE);
+}
+
+static void power_hud(void) {
+ int x = 8 + d_text(FONT_HUD, 8, 206, "Power left: ", INK_WHITE);
+ int p = N.power / 10;
+ small_digits(FONT_PIXEL, x, 205, p, INK_WHITE);
+ d_text(FONT_HUD, x + d_text_w(FONT_PIXEL, p >= 10 ? "00" : "0") + 1, 206, "%", INK_WHITE);
+ x = 8 + d_text(FONT_HUD, 8, 222, "Usage: ", INK_WHITE);
+ static const uint8_t bars[5] = {UI_BAR_G, UI_BAR_G, UI_BAR_Y, UI_BAR_R, UI_BAR_R};
+ static const uint8_t lit[5] = {UI_BAR_GL, UI_BAR_GL, UI_BAR_YL, UI_BAR_RL, UI_BAR_RL};
+ for (int i = 0; i < N.usage && i < 5; i++) {
+ d_fill(x + 9 * i, 222, 7, 12, bars[i]);
+ d_fill(x + 9 * i + 1, 223, 5, 2, lit[i]);
+ }
+}
+
+static void call_hud(int grp) {
+ if (N.night > 5 || N.call_muted || N.t >= call_end(N.night)) return;
+ if (N.t > 3) {
+ d_remap(6, 6, 74, 15, map_remap(grp, 0));
+ d_frame(6, 6, 74, 15, UI_SILVER);
+ d_text(FONT_HUD, 12, 8, "MUTE CALL", INK_WHITE);
+ d_text(FONT_TEXT, 84, 8, "0", INK_DIM);
+ }
+ const char *s = call_line(N.night, N.t);
+ if (!s) return;
+ const char *line;
+ int len, n = 0;
+ while (n < 3 && wrap(FONT_TEXT, s, 300, n, &line, &len)) n++;
+ int y = 52;
+ d_remap(4, y - 2, 312, 14 * n + 4, map_remap(grp, 0));
+ for (int k = 0; k < n; k++) {
+ wrap(FONT_TEXT, s, 300, k, &line, &len);
+ int w = d_text_wn(FONT_TEXT, line, len);
+ text_shadow(FONT_TEXT, 160 - w / 2, y + 14 * k, line, len, INK_CALL);
+ }
+}
+
+void view_captions(void) {
+ int y = 186;
+ for (int i = 3; i >= 0; i--) {
+ if (!g.cap[i] || g.cap_t[i] <= 0) continue;
+ int a = g.cap_t[i] > 0.5f ? 32 : (int)(g.cap_t[i] * 64);
+ d_text(FONT_TEXT, 9, y + 1, g.cap[i], INK_BLACK);
+ d_text(FONT_TEXT, 8, y, g.cap[i], ink_fade(INK_CAPTION, a));
+ y -= 13;
+ }
+}
+
+/* the bar at the bottom to bring the monitor up or down */
+static void flip_bar(int grp) {
+ d_remap(118, 229, 84, 9, map_remap(grp, 1));
+ d_frame(118, 229, 84, 9, UI_SPARE);
+ for (int k = 0; k < 2; k++)
+ for (int i = 0; i < 4; i++) {
+ scr[(235 - i) * SCR_W + 152 + 9 * k + i] = UI_WHITE;
+ scr[(235 - i) * SCR_W + 152 + 9 * k + 7 - i] = UI_WHITE;
+ }
+}
+
+/* ------------------------------------------------------------------ the office */
+int office_pan(void) { return (int)N.pan; }
+
+static int office(void) {
+ int px = (int)N.pan;
+ if (N.dark) {
+ if (N.dark == 3 && (N.dark_t > 0.35f || (int)(N.dark_t * 30) & 1)) {
+ d_fill(0, 0, SCR_W, SCR_H, UI_BLACK);
+ return pic_group_of(PIC_DARK);
+ }
+ scene_want(PIC_DARK);
+ d_blit(scene, SCENE_W, px, 0, SCR_W, SCENE_H, 0, 0, true);
+ if (N.dark == 2 && f.freddy_face) {
+ want_patch(PIC_DARK_FREDDY, DARK_FREDDY_W);
+ d_blit(patch, DARK_FREDDY_W, 0, 0, DARK_FREDDY_W, DARK_FREDDY_H, DARK_FREDDY_X - px, 2 * DARK_FREDDY_Y, true);
+ }
+ return pic_group_of(PIC_DARK);
+ }
+ office_pictures();
+ scene_want(PIC_OFFICE);
+ d_blit(scene, SCENE_W, px, 0, SCR_W, SCENE_H, 0, 0, true);
+ if (N.light == 1 + LEFT && !f.light_off) {
+ want_patch(N.pos[BONNIE] == AT_DOOR ? PIC_LIGHT_L_WHO : PIC_LIGHT_L, LIGHT_L_W);
+ d_blit(patch, LIGHT_L_W, 0, 0, LIGHT_L_W, LIGHT_L_H, LIGHT_L_X - px, 2 * LIGHT_L_Y, true);
+ }
+ if (N.light == 1 + RIGHT && !f.light_off) {
+ want_patch(N.pos[CHICA] == AT_DOOR ? PIC_LIGHT_R_WHO : PIC_LIGHT_R, LIGHT_R_W);
+ d_blit(patch, LIGHT_R_W, 0, 0, LIGHT_R_W, LIGHT_R_H, LIGHT_R_X - px, 2 * LIGHT_R_Y, true);
+ }
+ d_blit(fans[f.fan], 48, 0, 0, 48, 24, FAN_X - px, FAN_Y, true);
+ d_blit(panel_l[N.door[LEFT] | (N.light == 1 + LEFT) << 1], 32, 0, 0, 26, 40, PANEL_L_X - px, PANEL_Y, true);
+ d_blit(panel_r[N.door[RIGHT] | (N.light == 1 + RIGHT) << 1], 48, 0, 0, 48, 40, PANEL_R_X - px, PANEL_Y, true);
+ for (int s = 0; s < 2; s++) {
+ int vis = (int)(N.door_anim[s] * 120 + 0.5f);
+ if (vis > 0)
+ d_blit(s ? door_r : door_l, s ? DOOR_RW : DOOR_LW, 0, 120 - vis, s ? DOOR_R_W : DOOR_L_W, vis,
+ (s ? DOOR_R_X : DOOR_L_X) - px, 0, true);
+ }
+ if (N.gold == 3) {
+ extra_want(PIC_OFFICE_GOLD, GOLD_W);
+ d_blit(extra, GOLD_W, 0, 0, GOLD_W, GOLD_H / 2, GOLD_X - px, GOLD_Y, true);
+ }
+ return pic_group_of(PIC_OFFICE);
+}
+
+/* ------------------------------------------------------------------ the cameras */
+static const char *const cam_names[CAM_COUNT] = {"Show Stage", "Dining Area", "Pirate Cove", "West Hall",
+ "W. Hall Corner", "Supply Closet", "East Hall", "E. Hall Corner",
+ "Backstage", "Kitchen", "Restrooms"};
+static const char *const cam_labels[CAM_COUNT] = {"1A", "1B", "1C", "2A", "2B", "3", "4A", "4B", "5", "6", "7"};
+
+static bool here(int who, int cam) { return N.pos[who] == cam; }
+
+static int cam_picture(void) {
+ bool b = here(BONNIE, N.cam), c = here(CHICA, N.cam), fr = here(FREDDY, N.cam);
+ switch (N.cam) {
+ case CAM_1A:
+ if (!fr) return PIC_CAM1A_EMPTY;
+ return b && c ? PIC_CAM1A_BCF : b ? PIC_CAM1A_BF : c ? PIC_CAM1A_CF : PIC_CAM1A_F;
+ case CAM_1B: return PIC_CAM1B_EMPTY + (b && c && fr ? 7 : b && c ? 4 : b && fr ? 5 : c && fr ? 6 : b ? 1 : c ? 2 : fr ? 3 : 0);
+ case CAM_1C: return PIC_CAM1C_0 + (N.foxy < 3 ? N.foxy : 3);
+ case CAM_2A: return !f.hall_lit ? PIC_CAM2A_DARK : b ? PIC_CAM2A_B : PIC_CAM2A_EMPTY;
+ case CAM_2B: return (N.gold == 1 || N.gold == 2) && !b ? PIC_CAM2B_GOLD : b ? PIC_CAM2B_B : PIC_CAM2B_EMPTY;
+ case CAM_3: return b ? PIC_CAM3_B : PIC_CAM3_EMPTY;
+ case CAM_4A: return c && fr ? PIC_CAM4A_CF : c ? PIC_CAM4A_C : fr ? PIC_CAM4A_F : PIC_CAM4A_EMPTY;
+ case CAM_4B: return c ? PIC_CAM4B_C : fr ? PIC_CAM4B_F : PIC_CAM4B_EMPTY;
+ case CAM_5: return b ? PIC_CAM5_B : PIC_CAM5_EMPTY;
+ case CAM_7: return c && fr ? PIC_CAM7_CF : c ? PIC_CAM7_C : fr ? PIC_CAM7_F : PIC_CAM7_EMPTY;
+ }
+ return -1;
+}
+
+static void camera_map(void) {
+ const uint8_t *m = asset(ASSET_CAMMAP);
+ int size = m[0], runs = m[1] | m[2] << 8;
+ int mx = SCR_W - size - 4, my = SCR_H - size - 12;
+ const uint8_t *r = m + 3;
+ for (int i = 0; i < runs; i++, r += 3) memset(scr + (my + r[0]) * SCR_W + mx + r[1], UI_WHITE, r[2]);
+ bool blink = !((int)(g.t * 3) & 1);
+ for (int k = 0; k < CAM_COUNT; k++) {
+ int cx = mx + r[2 * k], cy = my + r[2 * k + 1];
+ bool sel = k == N.cam;
+ d_fill(cx - 8, cy - 5, 17, 11, sel && blink ? UI_MAP_ON : UI_MAP_OFF);
+ d_frame(cx - 8, cy - 5, 17, 11, UI_MAP_LINE);
+ tiny_text(cx - (cam_labels[k][1] ? 3 : 1), cy - 2, cam_labels[k], UI_WHITE);
+ }
+ d_text(FONT_HUD, mx - 2, my - 18, cam_names[N.cam], INK_WHITE);
+}
+
+static int cameras(bool paused) {
+ float c = N.cam_t; /* 5 seconds there, 2 still, 5 back, 2 still */
+ float a = c < 5 ? c / 5 : c < 7 ? 1 : c < 12 ? 1 - (c - 7) / 5 : 0;
+ int px = (int)(a * CAM_PAN), grp = -1, noise = f.noise;
+ if (N.glitch > 0) {
+ d_noise(0, 0, SCR_W, SCR_H, f.seed, GREYS);
+ } else if (N.cam == CAM_6) {
+ d_noise(0, 0, SCR_W, SCR_H, f.seed, DARK_GREYS);
+ d_text_c(FONT_BIG, 100, 64, "CAMERA DISABLED", INK_WHITE);
+ d_text_c(FONT_BIG, 100, 86, "-AUDIO ONLY-", INK_WHITE);
+ } else if (N.cam == CAM_2A && N.foxy_run >= 0 && N.foxy_run < 35) {
+ int pic = PIC_FOXYRUN0 + (int)(N.foxy_run / 35 * FOXYRUN_N);
+ scene_want(pic);
+ d_blit(scene, SCENE_W, CAM_PAN, 0, SCR_W, SCENE_H, 0, 0, true);
+ grp = pic_group_of(pic);
+ } else {
+ int pic = cam_picture();
+ scene_want(pic);
+ d_blit(scene, SCENE_W, px, 0, SCR_W, SCENE_H, 0, 0, true);
+ grp = pic_group_of(pic);
+ }
+ if (grp < 0) grp = pic_group_of(PIC_CAM1A_EMPTY); /* (no picture shows: its colours unused) */
+ pal_set(grp, paused ? 9 : 32, paused ? NULL : static_phases(noise),
+ g.blip > 0 && !paused ? (int)(g.blip * 120) : 0);
+ d_frame(5, 5, 310, 230, UI_FRAME);
+ if ((int)(g.t * 1.5f) & 1) { /* the recording dot */
+ static const uint8_t dot[7] = {2, 1, 0, 0, 0, 1, 2};
+ for (int r = 0; r < 7; r++) d_fill(14 + dot[r], 26 + r, 7 - 2 * dot[r], 1, UI_REC);
+ }
+ if (!paused) camera_map();
+ return grp;
+}
+
+/* ------------------------------------------------------------------ the night */
+static int hallucination(int grp) {
+ if (!f.halluc) return grp;
+ if (f.halluc == 3) {
+ d_fill(0, 0, SCR_W, SCR_H, UI_BLACK);
+ d_text_c(FONT_HUGE, 160, 104, "IT'S ME", INK_WHITE);
+ return grp;
+ }
+ int pic = f.halluc == 1 ? PIC_HALLUC_GOLDEN : PIC_HALLUC_ENDO;
+ extra_want(pic, 160);
+ d_blit2(extra, 160, 0, UI_BLACK);
+ return pic_group_of(pic);
+}
+
+/* the night: paused, the picture only, darker */
+static void night_draw(bool paused) {
+ bool cams = N.flip >= 1 && !N.dark;
+ int grp;
+ if (cams) {
+ grp = cameras(paused);
+ } else {
+ grp = office();
+ if (N.flip > 0) { /* the monitor coming up, or down */
+ int k = (int)(N.flip * FLIP_N);
+ k = k < FLIP_N ? k : FLIP_N - 1;
+ extra_want(PIC_FLIP0 + k, 160);
+ d_blit2(extra, 160, 0, UI_BLACK);
+ grp = pic_group_of(PIC_FLIP0 + k);
+ }
+ pal_set(grp, paused ? 9 : 32, NULL, 0);
+ }
+ if (paused) return;
+ int shown = hallucination(grp);
+ if (shown != grp) pal_set(shown, 32, NULL, 0);
+ if (!N.dark) {
+ clock_and_night();
+ power_hud();
+ call_hud(shown);
+ if (!cams || N.flip >= 1) flip_bar(shown);
+ }
+ view_captions();
+}
+
+void view_night(void) {
+ prepare();
+ night_draw(false);
+}
+
+void view_paused(void) { night_draw(true); }
+
+/* ------------------------------------------------------------------ jumpscares */
+void view_scare(void) {
+ static const int first[5] = {PIC_BONNIE0, PIC_CHICA0, PIC_FREDDY0, PIC_FOXY0, PIC_POWEROUT0};
+ static const int count[5] = {BONNIE_N, CHICA_N, FREDDY_N, FOXY_N, POWEROUT_N};
+ static const float fps[5] = {30, 30, 25, 25, 30};
+ static const bool loops[5] = {true, true, false, false, false};
+ prepare();
+ int w = g.scare, k = (int)(g.scare_t * fps[w]);
+ k = loops[w] ? k % count[w] : k < count[w] ? k : count[w] - 1;
+ int dx = g.scare_t > 0.1f ? f.shake : 0;
+ scene_want(first[w] + k);
+ d_blit2(scene, SCENE_W, dx, UI_BLACK);
+ pal_set(pic_group_of(first[w]), 32, NULL, 0);
+}
diff --git a/games/fnaf/ce/tests/pics.c b/games/fnaf/ce/tests/pics.c
new file mode 100644
index 00000000..3688c95a
--- /dev/null
+++ b/games/fnaf/ce/tests/pics.c
@@ -0,0 +1,43 @@
+/* Decodes every picture of the CE version (with its bases) and writes it as the screen would show it:
+ * pics OUT_DIR [--data DIR] (OUT_DIR/pic_<id>.ppm, rows doubled for the half-height ones) */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "../src/fnaf.h"
+
+extern const char *host_data_dir;
+static const uint8_t *vars[DATA_VARS];
+
+int main(int argc, char **argv) {
+ if (argc < 2) return 2;
+ if (argc > 3 && !strcmp(argv[2], "--data")) host_data_dir = argv[3];
+ if (!ce_data(DATA_VAR, DATA_VARS, vars)) {
+ fprintf(stderr, "missing %s\n", ce_missing);
+ return 1;
+ }
+ static const Assets a = {vars, asset_at, pic_group, PIC_FIRST, MAP_FIRST, FONT_FIRST};
+ gfx_init(&a, ASSET_TABLES);
+ static uint8_t buf[640 * 480];
+ for (int p = 0; p < PIC_COUNT; p++) {
+ int w = pic_w(p), h = pic_h(p);
+ memset(buf, 0, sizeof buf);
+ pic_full(p, buf, w);
+ const uint16_t *pal = map_palette(pic_group_of(p));
+ int dbl = h <= 120 && w > 160 ? 2 : 1; /* (the half-height ones as shown) */
+ char path[512];
+ snprintf(path, sizeof path, "%s/pic_%03d.ppm", argv[1], p);
+ FILE *f = fopen(path, "wb");
+ if (!f) return 1;
+ fprintf(f, "P6\n%d %d\n255\n", w, h * dbl);
+ for (int y = 0; y < h * dbl; y++)
+ for (int x = 0; x < w; x++) {
+ uint16_t c = pal[buf[(y / dbl) * w + x]];
+ int r = c >> 10 & 31, gg = (c >> 5 & 31) << 1 | c >> 15, b = c & 31;
+ uint8_t rgb[3] = {(uint8_t)(r * 255 / 31), (uint8_t)(gg * 255 / 63), (uint8_t)(b * 255 / 31)};
+ fwrite(rgb, 1, 3, f);
+ }
+ fclose(f);
+ }
+ printf("%d pictures\n", PIC_COUNT);
+ return 0;
+}
diff --git a/games/fnaf/ce/tests/play.c b/games/fnaf/ce/tests/play.c
new file mode 100644
index 00000000..907cd875
--- /dev/null
+++ b/games/fnaf/ce/tests/play.c
@@ -0,0 +1,179 @@
+/* Plays the CE version of Five Nights at Freddy's on a computer, with scripted keys (or a player of its own), and
+ * saves frames as the calculator's screen shows them.
+ *
+ * play [--frames N] [--keys "10-40:left,50:2nd,..."] [--shots 30,60|all|every:N] [--out DIR] [--saves DIR]
+ * [--data DIR] [--night N] [--ai F,B,C,X] [--power P] [--hour H] [--pos F,B,C] [--foxy N] [--gold]
+ * [--stars S] [--seed S] [--bot] [--fps N] [--ram BYTES]
+ *
+ * Keys (the CE's): left right up down 2nd enter clear mode del 0 to 9. "a-b:key" holds the key from step a to b,
+ * "a:key" for one step. --night starts that night (its card first); --ai, --power, --hour, --pos (Freddy, Bonnie,
+ * Chica: where they are), --foxy (his step out of the cove) and --gold then set the night; --stars the stars won.
+ * --bot plays the night itself. Steps are 60 a second; a frame is drawn every 60/fps steps (default 30 fps).
+ * Screenshots are DIR/shot_<step>.ppm. At the end it prints the state: "END scene ... night ... hour ...". */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include "../src/fnaf.h"
+
+extern uint32_t host_keys, host_ticks;
+extern const char *host_data_dir, *host_save_dir;
+extern size_t host_ram_size;
+void host_shot(const char *path);
+int fnaf_start(void);
+
+typedef struct { int a, b; uint32_t k; } Hold;
+static Hold holds[4096];
+static int nholds;
+
+static uint32_t key_of(const char *s) {
+ static const struct { const char *n; uint32_t k; } names[] = {
+ {"left", CK_LEFT}, {"right", CK_RIGHT}, {"up", CK_UP}, {"down", CK_DOWN}, {"2nd", CK_2ND}, {"ok", CK_2ND},
+ {"enter", CK_ENTER}, {"clear", CK_CLEAR}, {"back", CK_CLEAR}, {"mode", CK_MODE}, {"home", CK_MODE},
+ {"del", CK_DEL}};
+ for (unsigned i = 0; i < sizeof names / sizeof names[0]; i++)
+ if (!strcmp(s, names[i].n)) return names[i].k;
+ if (s[0] >= '0' && s[0] <= '9' && !s[1]) return CK_DIGIT(s[0] - '0');
+ fprintf(stderr, "unknown key %s\n", s);
+ exit(2);
+}
+
+static void parse_keys(char *s) {
+ for (char *t = strtok(s, ","); t; t = strtok(NULL, ",")) {
+ char *colon = strchr(t, ':');
+ if (!colon) continue;
+ *colon = 0;
+ int a = atoi(t), b = a;
+ char *dash = strchr(t, '-');
+ if (dash) b = atoi(dash + 1);
+ holds[nholds++] = (Hold){a, b, key_of(colon + 1)};
+ }
+}
+
+/* ------------------------------------------------------------------ a player (as the NumWorks version's tests) */
+static uint32_t bot(int f) {
+ Night *n = &g.n;
+ if (g.scene != SC_NIGHT || g.paused) return 0;
+ static int next_look, look_until;
+ bool up = n->flip >= 1;
+ bool bonnie = n->pos[BONNIE] == AT_DOOR, chica = n->pos[CHICA] == AT_DOOR;
+ bool want_l = bonnie || n->foxy == 3;
+ bool want_r = chica || (n->pos[FREDDY] == CAM_4B && (n->monitor || f >= next_look - 40));
+ if (n->monitor) {
+ if (up && n->cam != CAM_1C && !(f & 1)) return CK_DIGIT(1);
+ if (up && f >= look_until && !(f & 1)) return CK_2ND;
+ return 0;
+ }
+ if (n->flip > 0 || (f & 1)) return 0;
+ if (n->door[LEFT] != want_l) return CK_DIGIT(7);
+ if (n->door[RIGHT] != want_r) return CK_DIGIT(9);
+ if (f >= next_look && !n->dark) {
+ next_look = f + (n->foxy >= 1 ? 200 : 360) + (int)(rnd() % 200);
+ look_until = f + 50;
+ return CK_2ND;
+ }
+ return 0;
+}
+
+int main(int argc, char **argv) {
+ int frames = 600, night = 0, power = -1, hour = -1, ai[4] = {-1, -1, -1, -1}, gold = 0, stars = -1, fps = 30;
+ int pos[3] = {-1, -1, -1}, foxy = -1;
+ bool use_bot = false, all = false;
+ int every = 0;
+ const char *out = "build/shots";
+ static int shots[4096];
+ int nshots = 0;
+ for (int i = 1; i < argc; i++) {
+ const char *a = argv[i];
+ if (!strcmp(a, "--frames")) frames = atoi(argv[++i]);
+ else if (!strcmp(a, "--keys")) parse_keys(argv[++i]);
+ else if (!strcmp(a, "--shots")) {
+ char *s = argv[++i];
+ if (!strcmp(s, "all")) all = true;
+ else if (!strncmp(s, "every:", 6)) every = atoi(s + 6);
+ else
+ for (char *t = strtok(s, ","); t && nshots < 4096; t = strtok(NULL, ",")) shots[nshots++] = atoi(t);
+ } else if (!strcmp(a, "--out")) out = argv[++i];
+ else if (!strcmp(a, "--saves")) host_save_dir = argv[++i];
+ else if (!strcmp(a, "--data")) host_data_dir = argv[++i];
+ else if (!strcmp(a, "--night")) night = atoi(argv[++i]);
+ else if (!strcmp(a, "--power")) power = atoi(argv[++i]);
+ else if (!strcmp(a, "--hour")) hour = atoi(argv[++i]);
+ else if (!strcmp(a, "--ai")) sscanf(argv[++i], "%d,%d,%d,%d", &ai[0], &ai[1], &ai[2], &ai[3]);
+ else if (!strcmp(a, "--seed")) {
+ unsigned s = (unsigned)atoi(argv[++i]);
+ for (unsigned k = 0; k < s % 1000; k++) rnd();
+ } else if (!strcmp(a, "--bot")) use_bot = true;
+ else if (!strcmp(a, "--gold")) gold = 1;
+ else if (!strcmp(a, "--foxy")) foxy = atoi(argv[++i]);
+ else if (!strcmp(a, "--pos")) sscanf(argv[++i], "%d,%d,%d", &pos[0], &pos[1], &pos[2]);
+ else if (!strcmp(a, "--stars")) stars = atoi(argv[++i]);
+ else if (!strcmp(a, "--fps")) fps = atoi(argv[++i]);
+ else if (!strcmp(a, "--ram")) host_ram_size = (size_t)atol(argv[++i]);
+ else {
+ fprintf(stderr, "unknown argument %s\n", a);
+ return 2;
+ }
+ }
+ mkdir(out, 0755);
+ mkdir(host_save_dir, 0755);
+ ce_init();
+ host_ticks = 32768;
+ int r = fnaf_start();
+ if (r) {
+ printf("START FAILED %d %s\n", r, ce_missing);
+ return 1;
+ }
+ if (stars >= 0) g.stars = (uint8_t)stars;
+ if (night) {
+ if (night == 7)
+ for (int k = 0; k < 4; k++) g.custom[k] = (int8_t)(ai[k] >= 0 ? ai[k] : 0);
+ start_night(night);
+ }
+ bool set = false;
+ int f, step = 60 / (fps > 0 ? fps : 30);
+ for (f = 0; f < frames && g.running; f++) {
+ if (!set && g.scene == SC_NIGHT) {
+ set = true;
+ for (int k = 0; k < 4; k++)
+ if (ai[k] >= 0) g.n.ai[k] = (int8_t)ai[k];
+ if (power >= 0) g.n.power = (int16_t)power;
+ if (hour >= 0) g.n.hour = (uint8_t)hour, g.n.t = hour * 89.0f + 1;
+ if (gold) g.n.gold = 1;
+ for (int k = 0; k < 3; k++)
+ if (pos[k] >= 0) g.n.pos[k] = (uint8_t)pos[k];
+ if (foxy >= 0) g.n.foxy = (uint8_t)foxy;
+ }
+ uint32_t k = 0;
+ for (int i = 0; i < nholds; i++)
+ if (f >= holds[i].a && f <= holds[i].b) k |= holds[i].k;
+ if (use_bot && !k) k = bot(f);
+ host_keys = k;
+ int was = g.scene;
+ game_step(fnaf_keys());
+ if (g.scene != was && (g.scene == SC_SIX || g.scene == SC_SCARE || g.scene == SC_GOLDEN))
+ printf("EVENT %s night %d hour %d power %d t %.1f who %d foxy_pounds %d\n",
+ g.scene == SC_SIX ? "six" : g.scene == SC_SCARE ? "scare" : "golden", g.n.night, g.n.hour, g.n.power,
+ g.n.t, g.scare, g.n.foxy_pounds);
+ host_ticks += 32768 / 60 + (f % 15 == 0 ? 2 : 0);
+ bool shot = all || (every && f % every == 0);
+ for (int i = 0; i < nshots; i++) shot |= shots[i] == f;
+ if (g.running && (shot || f % step == 0)) {
+ game_draw();
+ ce_show(keep_frame);
+ keep_frame = false;
+ if (shot) {
+ char p[512];
+ snprintf(p, sizeof p, "%s/shot_%05d.ppm", out, f);
+ host_shot(p);
+ }
+ }
+ }
+ ce_quit();
+ printf("END scene %d night %d hour %d power %d t %.1f frames %d running %d paused %d reached %d stars %d "
+ "pos %d,%d,%d foxy %d gold %d dark %d muted %d ai %d,%d,%d,%d doors %d,%d cam %d keys_seen %d pounds %d\n",
+ g.scene, g.n.night, g.n.hour, g.n.power, g.n.t, f, g.running, g.paused, g.reached, g.stars, g.n.pos[0],
+ g.n.pos[1], g.n.pos[2], g.n.foxy, g.n.gold, g.n.dark, g.n.call_muted, g.n.ai[0], g.n.ai[1], g.n.ai[2],
+ g.n.ai[3], g.n.door[0], g.n.door[1], g.n.cam, g.keys_seen, g.n.foxy_pounds);
+ return 0;
+}
diff --git a/games/fnaf/ce/tools/pack.py b/games/fnaf/ce/tools/pack.py
index 50ee5eee..6d703284 100644
--- a/games/fnaf/ce/tools/pack.py
+++ b/games/fnaf/ce/tools/pack.py
@@ -17,7 +17,7 @@ import sys
import tempfile
import numpy as np
-from PIL import Image
+from PIL import Image, ImageFilter
HERE = os.path.dirname(os.path.abspath(__file__))
CE = os.path.dirname(HERE)
@@ -72,8 +72,13 @@ def resize(a, w, h):
np.float64)
-def halfv(a):
- return resize(a, a.shape[1], (a.shape[0] + 1) // 2)
+def halfv(a, sharpen=False):
+ """at half height (the calculator shows each row twice); the office a little sharper for it"""
+ im = Image.fromarray(np.clip(a, 0, 255).astype(np.uint8))
+ im = im.resize((im.width, (im.height + 1) // 2), Image.LANCZOS)
+ if sharpen:
+ im = im.filter(ImageFilter.UnsharpMask(radius=1.0, percent=50, threshold=1))
+ return np.asarray(im).astype(np.float64)
class Pic:
@@ -118,14 +123,14 @@ def mb_box(a, b, thresh=10):
def office_pictures(N):
g = group("office")
- office = halfv(N["OFFICE"])
+ office = halfv(sharpen=True, a=N["OFFICE"])
base = pic("OFFICE", office, g, "office")
define("OFFICE_W", office.shape[1])
# the lights: each side's lit doorway, a patch over the office (with Bonnie or Chica: over it again)
for side, lit, who in (("L", "OFFICE_LL", "OFFICE_LLB"), ("R", "OFFICE_RL", "OFFICE_RLC")):
- a, b = halfv(N[lit]), halfv(N[who])
+ a, b = halfv(sharpen=True, a=N[lit]), halfv(sharpen=True, a=N[who])
x0, y0, x1, y1 = [min(v1, v2) if k < 2 else max(v1, v2)
- for k, (v1, v2) in enumerate(zip(mb_box(a, office), mb_box(b, office)))]
+ for k, (v1, v2) in enumerate(zip(mb_box(a, office, 24), mb_box(b, office, 24)))]
p = pic(f"LIGHT_{side}", pad8(a)[y0:y1, x0:x1], g, "office", x=x0, y=y0)
pic(f"LIGHT_{side}_WHO", pad8(b)[y0:y1, x0:x1], g, "office", base=p, skip=TIGHT, x=x0, y=y0)
define(f"LIGHT_{side}_X", x0)
@@ -134,14 +139,14 @@ def office_pictures(N):
define(f"LIGHT_{side}_H", y1 - y0)
# the doors, buttons, fan and Golden Freddy, as the NumWorks version places them (rows halved)
for n in ("DOOR_L", "DOOR_R"):
- pic(n, halfv(N[n]), g, "office")
+ pic(n, halfv(sharpen=True, a=N[n]), g, "office")
for n in [f"PANEL_{s}{k}" for s in "LR" for k in range(4)] + ["FAN0", "FAN1", "FAN2", "OFFICE_GOLD"]:
- pic(n, halfv(N[n]), g, "office")
+ pic(n, halfv(sharpen=True, a=N[n]), g, "office")
# out of power: the dark office, and Freddy's face in the doorway (a patch)
- dark = halfv(N["DARK"])
+ dark = halfv(sharpen=True, a=N["DARK"])
pic("DARK", dark, g, "office")
- df = halfv(N["DARK_FREDDY"])
- x0, y0, x1, y1 = mb_box(df, dark, 6)
+ df = halfv(sharpen=True, a=N["DARK_FREDDY"])
+ x0, y0, x1, y1 = mb_box(df, dark, 20)
pic("DARK_FREDDY", pad8(df)[y0:y1, x0:x1], g, "office", x=x0, y=y0)
for k, v in zip("XYWH", (x0, y0, x1 - x0, y1 - y0)):
define(f"DARK_FREDDY_{k}", v)
@@ -169,17 +174,19 @@ def camera_pictures(N):
pic("CAM2B_GOLD", halfv(N["CAM2B_GOLD"]), g, "cam", base=first, skip=TIGHT)
-def anim(name, frames, grp, quality="scare"):
+def anim(name, frames, grp, quality="scare", chain=True):
+ """160 x 120 frames, each coded over the one before (chain), or on its own (played both ways)"""
prev = None
for i, a in enumerate(frames):
- prev = pic(f"{name}{i}", resize(a, 160, 120), grp, quality, base=prev, skip=ANIM if prev else None)
+ p = pic(f"{name}{i}", resize(a, 160, 120), grp, quality, base=prev, skip=ANIM if prev else None)
+ prev = p if chain else None
define(f"{name}_N", len(frames))
def scare_pictures(N):
for name, n in (("BONNIE", 10), ("CHICA", 15), ("FREDDY", 20), ("FOXY", 19), ("POWEROUT", 21)):
anim(name, [N[f"{name}{i}"] for i in range(n)], group(name.lower()))
- anim("FLIP", [N[f"FLIP{i}"] for i in range(11)], group("flip"))
+ anim("FLIP", [N[f"FLIP{i}"] for i in range(11)], group("flip"), chain=False)
# the hallucinations' pictures (a frame now and then): small
g = group("halluc")
pic("HALLUC_GOLDEN", resize(N["GOLDEN"], 160, 120), g, "scare")
@@ -312,7 +319,7 @@ def main():
blob("TABLES", C.tables_blob(Q, cc))
gnames = list(GROUPS)
for n in gnames:
- blob("MAP_" + n.upper(), zx0(GROUPS[n].blob(UI_RGB) + remaps(GROUPS[n])))
+ blob("MAP_" + n.upper(), zx0(GROUPS[n].blob(UI_RGB) + remaps(GROUPS[n]) + bytes([GROUPS[n].phases])))
pic_ids = {p.name: i for i, p in enumerate(PICS)}
for p, st in zip(PICS, streams):
blob("PIC_" + p.name, C.write(p.coded, st, codes, p.q, pic_ids[p.base.name] if p.base else None))
diff --git a/games/fnaf/src/game.c b/games/fnaf/src/game.c
index 1502f999..3169583f 100644
--- a/games/fnaf/src/game.c
+++ b/games/fnaf/src/game.c
@@ -1,9 +1,14 @@
/* The scenes around the nights: the title, the newspaper, how to play, the night's card, 6 AM, the
- * paychecks and the pink slip, the game over, the custom night and Golden Freddy; keys and saves. */
+ * paychecks and the pink slip, the game over, the custom night and Golden Freddy; keys and saves. The TI-84 Plus
+ * CE version (ce/) has drawings of its own: FNAF_CE leaves these out. */
#include <string.h>
#include "game.h"
+#ifndef FNAF_CE
#include "gfx.h"
#include "res.h"
+#else
+#include "plat.h"
+#endif
Game g;
static uint32_t seed = 0x2C1B3C6Du;
@@ -82,6 +87,7 @@ static void title_step(void) {
}
}
+#ifndef FNAF_CE
static void title_draw(void) {
gfx_fill(0, gfx_y0, SCREEN_W, BAND_H, BLACK);
pic_draw(PIC_TITLE0 + face, 0, 0);
@@ -107,17 +113,22 @@ static void title_draw(void) {
gfx_text(FONT_HUD, 44, y0 + 18 * g.menu_n + 4, s, WHITE);
}
}
+#endif
/* ------------------------------------------------------------------ the newspaper, how to play, the card */
+#ifndef FNAF_CE
static int fade_in_out(float t, float in, float hold, float out) { /* 0 black to 32 */
float a = t < in ? t / in : t < hold ? 1 : t < hold + out ? 1 - (t - hold) / out : 0;
return (int)(a * 32);
}
+#endif
+#ifndef FNAF_CE
static void news_draw(void) {
pic_draw(PIC_NEWSPAPER, 0, 0);
gfx_dim(0, gfx_y0, SCREEN_W, BAND_H, fade_in_out(g.t, 2, 7, 2));
}
+#endif
static void news_step(void) {
if (pressed(K_OK) && g.t > 2 && g.t < 7) g.t = 7;
@@ -130,6 +141,7 @@ static void news_step(void) {
}
}
+#ifndef FNAF_CE
static void keys_draw(void) {
static const char *const rows[][2] = {
{"In the office", NULL},
@@ -160,6 +172,7 @@ static void keys_draw(void) {
gfx_text_c(FONT_TEXT, 160, 209, "camera drains it. Survive until 6 AM.", RGB(0xC8C8C8));
if ((int)(g.t * 2) & 1) gfx_text_c(FONT_HUD, 160, 226, "OK", WHITE);
}
+#endif
static void keys_step(void) {
if (!pressed(K_OK | K_BACK) || g.t < 0.4f) return;
@@ -174,9 +187,12 @@ static void keys_step(void) {
start_night(g.n.night ? g.n.night : 1);
}
+#ifndef FNAF_CE
static const char *const nth[] = {"", "1st Night", "2nd Night", "3rd Night", "4th Night", "5th Night", "6th Night",
"7th Night"};
+#endif
+#ifndef FNAF_CE
static void intro_draw(void) {
gfx_fill(0, gfx_y0, SCREEN_W, BAND_H, BLACK);
gfx_text_c(FONT_HUGE, 160, 92, "12:00 AM", WHITE);
@@ -184,6 +200,7 @@ static void intro_draw(void) {
if (g.t < 0.15f) gfx_static(0, gfx_y0, SCREEN_W, BAND_H, 20, rnd());
if (g.t > 1.2f) gfx_dim(0, gfx_y0, SCREEN_W, BAND_H, (int)((2.2f - g.t) * 32));
}
+#endif
static void intro_step(void) {
if (g.t < 2.2f) return;
@@ -221,6 +238,7 @@ static void night_scene_step(void) {
night_step();
}
+#ifndef FNAF_CE
static void pause_draw(void) {
view_night();
gfx_dim(0, gfx_y0, SCREEN_W, BAND_H, 9);
@@ -239,6 +257,7 @@ static void pause_draw(void) {
if (g.pause_item == i) gfx_text(FONT_BIG, 88, 104 + 20 * i, ">>", WHITE);
}
}
+#endif
/* ------------------------------------------------------------------ 6 AM, the paychecks */
static void six_begin(void) {
@@ -251,6 +270,7 @@ static void six_begin(void) {
caption("*chimes*");
}
+#ifndef FNAF_CE
static void six_draw(void) {
gfx_fill(0, gfx_y0, SCREEN_W, BAND_H, BLACK);
/* 5 rolls up into 6 */
@@ -266,6 +286,7 @@ static void six_draw(void) {
gfx_text_c(FONT_TEXT, 160, 170, g.t > 6.2f ? "*children cheering*" : "*chimes*", RGB(0xA0A0A0));
gfx_dim(0, gfx_y0, SCREEN_W, BAND_H, fade_in_out(g.t, 1, 10.5f, 1));
}
+#endif
static void six_step(void) {
if (g.t < DT * 1.5f) six_begin();
@@ -279,11 +300,13 @@ static void six_step(void) {
}
}
+#ifndef FNAF_CE
static void paper_draw(void) {
static const int pics[3] = {PIC_PAYCHECK, PIC_OVERTIME, PIC_PINKSLIP};
pic_draw(pics[g.paper], 0, 0);
gfx_dim(0, gfx_y0, SCREEN_W, BAND_H, fade_in_out(g.t, 2, 17, 2));
}
+#endif
static void paper_step(void) {
if (pressed(K_OK) && g.t > 2 && g.t < 17) g.t = 17;
@@ -291,6 +314,7 @@ static void paper_step(void) {
}
/* ------------------------------------------------------------------ the end of a night */
+#ifndef FNAF_CE
static void over_draw(void) {
if (g.t < 2.5f) {
gfx_fill(0, gfx_y0, SCREEN_W, BAND_H, BLACK);
@@ -301,24 +325,30 @@ static void over_draw(void) {
gfx_static(0, gfx_y0, SCREEN_W, BAND_H, g.t < 3 ? 26 : 6, rnd());
gfx_text_r(FONT_HUGE, 306, 200, "Game Over", WHITE);
}
+#endif
static void over_step(void) {
if ((pressed(K_OK) && g.t > 3) || g.t > 13) go(SC_TITLE);
}
+#ifndef FNAF_CE
static void golden_draw(void) {
pic_draw(PIC_GOLDEN, ((int)(g.t * 60) & 2) - 1, 0);
gfx_static(0, gfx_y0, SCREEN_W, BAND_H, 6 + (int)(rnd() % 8), rnd());
}
+#endif
static void golden_step(void) {
if (g.t > 1.6f) g.running = false; /* and the game is gone, as it crashes on a computer */
}
/* ------------------------------------------------------------------ the custom night */
+#ifndef FNAF_CE
static const int faces[4] = {PIC_FACE_FREDDY, PIC_FACE_BONNIE, PIC_FACE_CHICA, PIC_FACE_FOXY};
static const char *const names[4] = {"Freddy", "Bonnie", "Chica", "Foxy"};
+#endif
+#ifndef FNAF_CE
static void custom_draw(void) {
gfx_fill(0, gfx_y0, SCREEN_W, BAND_H, BLACK);
gfx_text_c(FONT_BIG, 160, 14, "Customize Night", WHITE);
@@ -338,6 +368,7 @@ static void custom_draw(void) {
gfx_text(FONT_TEXT, 12, 214, "< > pick up down: level", RGB(0x909090));
gfx_text_r(FONT_BIG, 308, 208, "Ready", (int)(g.t * 2) & 1 ? WHITE : RGB(0xC0C0C0));
}
+#endif
static void custom_step(void) {
if (pressed(K_LEFT | K_DIGIT(4))) g.custom_sel = (uint8_t)((g.custom_sel + 3) % 4);
@@ -394,6 +425,7 @@ void game_step(uint32_t keys) {
}
}
+#ifndef FNAF_CE
void game_draw(void) {
static void (*const draws[])(void) = {
[SC_TITLE] = title_draw, [SC_NEWS] = news_draw, [SC_KEYS] = keys_draw, [SC_INTRO] = intro_draw,
@@ -401,3 +433,4 @@ void game_draw(void) {
[SC_OVER] = over_draw, [SC_CUSTOM] = custom_draw, [SC_GOLDEN] = golden_draw};
gfx_frame(g.scene == SC_NIGHT && g.paused ? pause_draw : draws[g.scene]);
}
+#endif