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.
C calculatorcalculator-gamescasiochesscrossy-roaddrive-madfx-cg50gamesgeometry-dashgraphing-calculatorndlessnumworksnumworks-appsnumworks-calculatornumworks-gamenumworks-gamestetristi-84-plus-ceti-nspire
4 Players: party minigames for 2, 3 or 4 players on one calculator, and teams
Mason36346fbd0798fb0
diff --git a/games/fourplayer/.gitignore b/games/fourplayer/.gitignorenew file mode 100644index 00000000..643bd909--- /dev/null+++ b/games/fourplayer/.gitignore@@ -0,0 +1,3 @@+output/+build/+__pycache__/diff --git a/games/fourplayer/Makefile b/games/fourplayer/Makefilenew file mode 100644index 00000000..88ee629c--- /dev/null+++ b/games/fourplayer/Makefile@@ -0,0 +1,84 @@+# 2 3 4 Players on the NumWorks calculator (C), drawn with NumPlay's launcher (launcher/src/gfx.c and its fonts).+# make output/fourplayer.nwa+# make check link it like the calculator does (output/fourplayer.bin): RAM and the app space+# make host build/play: the game on a computer, for tests and screenshots+# make test the game played on a computer (tests/check.py, with the sanitizers)+Q ?= @+# the ARM toolchain on the PATH, or in TOOLCHAIN=<its bin folder>+TOOLCHAIN ?=+BIN = $(if $(TOOLCHAIN),$(TOOLCHAIN)/)+CC = $(BIN)arm-none-eabi-gcc+NWLINK ?= npx --yes -- [email protected]+STRIP ?= $(BIN)arm-none-eabi-strip --strip-unneeded+SIZE = $(BIN)arm-none-eabi-size+BUILD_DIR = output++LAUNCHER = ../../launcher/src+SRC = $(filter-out src/plat_host.c,$(wildcard src/*.c)) $(LAUNCHER)/gfx.c $(LAUNCHER)/font_data.c $(LAUNCHER)/xfont.c+HEADERS = $(wildcard src/*.h) ../common/epsilon_app.h ../common/epsilon_files.h ../common/np_text.h \+ $(LAUNCHER)/gfx.h $(LAUNCHER)/np.h $(LAUNCHER)/font.h++CFLAGS = -std=gnu11 $(shell $(NWLINK) eadk-cflags-device)+CFLAGS += -Os -Wall -Wextra -Wno-unused-parameter -fno-math-errno -fsingle-precision-constant+CFLAGS += -fno-tree-loop-distribute-patterns -flto -fno-fat-lto-objects -fwhole-program -fvisibility=internal+CFLAGS += -ffunction-sections -fdata-sections $(EXTRA)+LDFLAGS = -Wl,--relocatable -nostartfiles --specs=nano.specs+LDFLAGS += -Wl,-e,main -Wl,-u,eadk_app_name -Wl,-u,eadk_app_icon -Wl,-u,eadk_api_level+LDFLAGS += -Wl,--gc-sections -flinker-output=nolto-rel++.PHONY: build check clean host test+build: $(BUILD_DIR)/fourplayer.nwa++# apps get 133652 bytes of RAM on custom builds of Epsilon 25.2 (148928 or more on official ones); installed on its+# own, the app must fit the calculator's space for apps (0x90200000 to 0x903F0000 in its flash)+RAM_LENGTH = 133652+APP_SPACE = 2031616+check: $(BUILD_DIR)/fourplayer.nwa+ $(Q) $(NWLINK) nwa-bin --ram-length $(RAM_LENGTH) $< $(BUILD_DIR)/fourplayer.bin+ @n=$$(wc -c < $(BUILD_DIR)/fourplayer.bin); \+ echo "BIN $(BUILD_DIR)/fourplayer.bin: $$n bytes, $$(( $(APP_SPACE) - n )) to spare of the app space"; \+ [ $$n -le $(APP_SPACE) ] || { echo "fourplayer.nwa is bigger than the calculator's app space ($(APP_SPACE) bytes)"; exit 1; }++# compiled, then linked (the link options would upset the compiler)+OBJ = $(patsubst %.c,$(BUILD_DIR)/obj/%.o,$(notdir $(SRC)))+vpath %.c src $(LAUNCHER)+$(BUILD_DIR)/obj/%.o: %.c $(HEADERS)+ @mkdir -p $(@D)+ $(Q) $(CC) $(CFLAGS) -c $< -o $@++$(BUILD_DIR)/fourplayer.nwa: $(OBJ) $(BUILD_DIR)/icon.o+ @echo "LD $@"+ $(Q) $(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) $(BUILD_DIR)/icon.o -o $@+ $(Q) $(STRIP) $@+ $(Q) $(SIZE) $@++$(BUILD_DIR)/icon.o: src/icon.png | $(BUILD_DIR)+ @echo "ICON $<"+ $(Q) $(NWLINK) png-icon-o $< $@++$(BUILD_DIR):+ $(Q) mkdir -p $@++# on a computer: tests/play.c plays it with scripted keys and computer players, and saves screenshots+HOST_FLAGS = -std=gnu11 -O1 -g -Wall -Wextra -Wno-unused-parameter -Isrc -DHOST+HOST_SAN = -fsanitize=address,undefined -fno-sanitize-recover=undefined+HOST_SRC = $(filter-out src/plat_eadk.c src/main.c,$(wildcard src/*.c)) $(LAUNCHER)/gfx.c $(LAUNCHER)/font_data.c \+ $(LAUNCHER)/xfont.c+host: build/play+build/play: $(HOST_SRC) tests/play.c $(HEADERS)+ $(Q) mkdir -p $(dir $@)+ $(Q) cc $(HOST_FLAGS) $(HOST_SAN) $(HOST_SRC) tests/play.c -lm -o $@+ @echo "HOST $@"++test: build/play+ python3 tests/check.py build/play++clean:+ $(Q) rm -rf $(BUILD_DIR) build++# the other targets (platform/port.mk): make port TARGET=sdl, ti84ce, nspire, web...+PORT_ID = fourplayer+PORT_NAME = 2 3 4 Players+PORT_SRC = $(filter-out src/plat_host.c,$(wildcard src/*.c)) $(LAUNCHER)/gfx.c $(LAUNCHER)/xfont.c+PORT_FLAGS = -Isrc+include ../../platform/port.mkdiff --git a/games/fourplayer/src/app.c b/games/fourplayer/src/app.cnew file mode 100644index 00000000..1ccbf453--- /dev/null+++ b/games/fourplayer/src/app.c@@ -0,0 +1,697 @@+/* 2 3 4 Players: the screens (players, games, rules, the rounds, the scores, pause), the matches and the save.+ *+ * Every player has one key, spread around the keypad: shift and backspace at the top corners, 0 and EXE at the+ * bottom ones, 7 and ) on the sides for 3 v 3. Every minigame is played with that key alone. */+#include <string.h>+#include "app.h"+#include "fp.h"+#include "plat.h"++const game_t *const GAMES[NG] = {&g_sumo, &g_race, &g_tanks, &g_snakes, &g_soccer, &g_fish, &g_skate,+ &g_rope, &g_paint, &g_balloon, &g_birds, &g_gravity, &g_frog, &g_clock};+#define MIX NG /* (the menu's first tile: another game every round) */++/* ---- the players */+int np = 2;+bool teams;+uint8_t hold[NMAX], tap[NMAX], rel[NMAX], gone[NMAX];+int32_t rt;+color_t col[NMAX], lite[NMAX], dark[NMAX];++static const uint8_t PKEY[NMAX] = {FK_SHIFT, FK_BACKSPACE, FK_ZERO, FK_EXE, FK_SEVEN, FK_RPAREN};+static const char *const KEYNAME[NMAX] = {"shift", "", "0", "EXE", "7", ")"};+static const uint32_t FFA_RGB[4] = {0xF2453D, 0x2F7FF0, 0x2DB653, 0xF5B400};+static const uint32_t TEAM_RGB[NMAX] = {0xF2453D, 0x2F7FF0, 0xFF8A1F, 0x1FC2D6, 0xF25FA8, 0x8E5CF0};+static const char *const FFA_NAME[4] = {"Red", "Blue", "Green", "Yellow"};+static const char *const TEAM_NAME[2] = {"Red team", "Blue team"};+#define NMODES 5+static const uint8_t MODE_N[NMODES] = {2, 3, 4, 4, 6};+static const char *const MODE_NAME[NMODES] = {"2 players", "3 players", "4 players", "2 v 2", "3 v 3"};++const char *keyname(int p) { return KEYNAME[p]; }+int side(int p) { return teams ? p & 1 : p; }+float seat(int p) {+ static const int16_t DEG[4][NMAX] = {{180, 0}, {210, 330, 90}, {225, 315, 135, 45}, {240, 300, 120, 60, 180, 0}};+ return DEG[np == 2 ? 0 : np == 3 ? 1 : np == 4 ? 2 : 3][p] * (PI / 180);+}++/* ---- the save: the mode, the game, the target, and the matches each colour won, each game and Mix */+#define SAVE_NAME "fourplayer.sav"+#define COPY_NAME "fourplayer_saves.py"+#define VERSION 1+typedef struct {+ char magic[4]; /* "FOUR" */+ uint8_t version, mode, game, target; /* game: the tile last chosen (NG: Mix); target: first to */+ uint16_t wins[NG + 1][6]; /* red, blue, green, yellow (2 to 4 players), the red and blue teams */+} save_t;+static save_t sv;++static const char copy_head[] = "# 2 3 4 Players' saves: keep this file\n#>" SAVE_NAME ":";+static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";+#define COPY_SIZE (1 + sizeof copy_head - 1 + (sizeof(save_t) + 2) / 3 * 4 + 2)++/* the save, and its copy in a Python script: installing apps from the NumWorks website keeps only scripts */+static void save(void) {+ static uint8_t copy[COPY_SIZE];+ memcpy(sv.magic, "FOUR", 4), sv.version = VERSION;+ plat_save(SAVE_NAME, &sv, sizeof sv);+ const uint8_t *d = (const uint8_t *)&sv;+ uint8_t *p = copy;+ *p++ = 0; /* (a script's first byte: not imported) */+ memcpy(p, copy_head, sizeof copy_head - 1), p += sizeof copy_head - 1;+ for (uint32_t i = 0; i < sizeof sv; i += 3, p += 4) {+ uint32_t left = (uint32_t)sizeof sv - i;+ uint32_t v = (uint32_t)d[i] << 16 | (uint32_t)(left > 1 ? d[i + 1] : 0) << 8 | (left > 2 ? d[i + 2] : 0);+ p[0] = (uint8_t)b64[v >> 18 & 63], p[1] = (uint8_t)b64[v >> 12 & 63];+ p[2] = left > 1 ? (uint8_t)b64[v >> 6 & 63] : '=';+ p[3] = left > 2 ? (uint8_t)b64[v & 63] : '=';+ }+ *p++ = '\n';+ *p++ = 0;+ plat_save(COPY_NAME, copy, (uint32_t)(p - copy));+}+static int b64_value(uint8_t c) {+ for (int i = 0; i < 64; i++)+ if ((uint8_t)b64[i] == c) return i;+ return -1;+}+static bool usable(const uint8_t *d, uint32_t n) { return d && n == sizeof sv && !memcmp(d, "FOUR", 4) && d[4] == VERSION; }+static void load(void) {+ uint32_t n;+ const uint8_t *d = plat_load(SAVE_NAME, &n);+ static save_t from_copy;+ if (!usable(d, n)) { /* the copy, then */+ d = NULL;+ const uint8_t *c = plat_load(COPY_NAME, &n);+ uint32_t head = 1 + sizeof copy_head - 1, len = (sizeof(save_t) + 2) / 3 * 4;+ if (c && n >= head + len && !memcmp(c + 1, copy_head, sizeof copy_head - 1)) {+ uint8_t *o = (uint8_t *)&from_copy;+ uint32_t k = 0;+ bool ok = true;+ for (uint32_t i = 0; i < len && ok; i += 4) {+ int v[4];+ for (int j = 0; j < 4; j++) {+ uint8_t q = c[head + i + j];+ v[j] = q == '=' ? 0 : b64_value(q);+ ok &= v[j] >= 0;+ }+ uint32_t w = (uint32_t)(v[0] & 63) << 18 | (uint32_t)(v[1] & 63) << 12 | (uint32_t)(v[2] & 63) << 6 | (uint32_t)(v[3] & 63);+ for (int j = 0; j < 3 && k < sizeof(save_t); j++) o[k++] = (uint8_t)(w >> (16 - 8 * j));+ }+ if (ok && usable((const uint8_t *)&from_copy, sizeof from_copy)) d = (const uint8_t *)&from_copy;+ }+ }+ if (d) memcpy(&sv, d, sizeof sv);+ else memset(&sv, 0, sizeof sv), sv.mode = 2, sv.target = 3, sv.game = 0;+ /* whatever the file held: settings this game could have written */+ if (sv.mode >= NMODES) sv.mode = 2;+ if (sv.game > NG) sv.game = 0;+ if (sv.target < 1 || sv.target > 9) sv.target = 3;+}++/* ---- the app */+#ifndef HOST /* (app.h names them for the tests) */+enum { S_MODE, S_GAMES, S_RULES, S_PLAY, S_SCORE, S_OVER, S_PAUSE, S_ASK };+#endif+#define COUNTDOWN 2000 /* ms of 3, 2, 1 before a round */+#define ENDING 1300 /* ms the game plays on once a round is decided */+#define BANNER 1100 /* ms its winner shows */+#define SCORE_MS 3000 /* the scores between rounds */+static struct {+ int screen, sel, under; /* under: the screen under Paused or Quit game? */+ int cur, last_cur; /* the game playing, the one before (Mix) */+ int nsides, score[4], round, winner, matches; /* winner: the round's side, -1 a draw */+ bool decided, started, dirty;+ int32_t end_t, freeze; /* freeze: ms of countdown left after Resume */+ uint32_t now, shown, acc;+ uint8_t ready, prev_h[NMAX];+ uint64_t held, stale;+ float tag_x[NMAX], tag_y[NMAX];+} u;++static int nsides(void) { return teams ? 2 : np; }+static int slot(int s) { return teams ? 4 + s : s; } /* a side's place in the wins */+static const char *side_name(int s) { return teams ? TEAM_NAME[s] : FFA_NAME[s]; }+static const game_t *G(void) { return GAMES[u.cur]; }++static void set_mode(int m) {+ np = MODE_N[m], teams = m >= 3;+ for (int p = 0; p < NMAX; p++) {+ uint32_t c = teams ? TEAM_RGB[p] : FFA_RGB[p & 3];+ col[p] = gfx_rgb(c), lite[p] = gfx_rgb(gfx_lerp888(c, 0xFFFFFF, 120)), dark[p] = gfx_rgb(gfx_lerp888(c, 0, 80));+ }+ u.nsides = nsides();+}+static void open(int s) {+ u.screen = s, u.shown = u.now, u.dirty = true;+ if (s == S_PAUSE || s == S_ASK || s == S_OVER) u.sel = 0;+}++/* ---- the round */+void tag_at(int p, float x, float y) { u.tag_x[p] = x, u.tag_y[p] = y; }+void out(int p) { gone[p] = 1; }+int alive(void) {+ int n = 0;+ for (int p = 0; p < np; p++) n += !gone[p];+ return n;+}+static void decide(int s) {+ if (u.decided) return;+ u.decided = true, u.winner = s, u.end_t = rt;+ if (s >= 0) u.score[s]++;+}+void win(int p) { decide(side(p)); }+void tie(void) { decide(-1); }+bool over(void) { return u.decided; }++static void begin_round(void) {+ memset(gone, 0, sizeof gone);+ memset(hold, 0, sizeof hold), memset(tap, 0, sizeof tap), memset(rel, 0, sizeof rel);+ for (int p = 0; p < NMAX; p++) u.tag_x[p] = -1000;+ u.decided = false, u.winner = -1, u.freeze = 0, u.acc = 0;+ rt = -COUNTDOWN;+ G()->start();+ open(S_PLAY);+#ifdef HOST+ extern int fp_round_serial;+ fp_round_serial++;+#endif+}+static int mix_pick(void) {+ int g;+ do g = rn(NG); while (g == u.cur && NG > 1);+ return g;+}+static void new_match(void) {+ memset(u.score, 0, sizeof u.score);+ u.round = 0, u.started = false, u.ready = 0;+ u.cur = sv.game == MIX ? mix_pick() : sv.game;+ open(S_RULES);+}+/* the round's winner seen: the scores, or the match is over */+static void after_banner(void) {+ int s = u.winner;+ if (s >= 0 && u.score[s] >= sv.target) {+ if (sv.wins[sv.game][slot(s)] < 0xFFFF) sv.wins[sv.game][slot(s)]++;+ u.matches++;+ save();+ open(S_OVER);+ } else {+ open(S_SCORE);+ }+}+static void next_round(void) {+ u.round++;+ if (sv.game == MIX && u.winner >= 0) { /* (a draw is played again) */+ u.cur = mix_pick(), u.ready = 0;+ open(S_RULES);+ } else {+ begin_round();+ }+}++/* a step of the game, with each player's key held, just pressed, just let go */+static void game_step(const uint8_t *h, const uint8_t *t, const uint8_t *r) {+ for (int p = 0; p < np; p++) {+ bool on = !u.decided && !gone[p];+ hold[p] = on && h[p], tap[p] = on && t[p], rel[p] = on && r[p];+ }+ G()->step();+ if (G()->last && !u.decided) { /* the last side standing */+ int left = -1;+ bool one = true;+ for (int p = 0; p < np; p++)+ if (!gone[p]) {+ if (left < 0) left = side(p);+ else one &= side(p) == left;+ }+ if (left < 0) tie();+ else if (one) decide(left);+ }+}++/* a frame of play: the game moved on in steps of TICK ms, the keys read once a frame */+static void play(uint32_t ms) {+ uint8_t now_h[NMAX], t_[NMAX], r_[NMAX];+ for (int p = 0; p < np; p++) {+ now_h[p] = (u.held & KEY(PKEY[p])) != 0;+ t_[p] = now_h[p] && !u.prev_h[p], r_[p] = !now_h[p] && u.prev_h[p];+ u.prev_h[p] = now_h[p];+ }+ u.acc += ms > 100 ? 100 : ms;+ bool first = true;+ while (u.acc >= TICK) {+ u.acc -= TICK;+ if (u.freeze > 0) {+ u.freeze -= TICK;+ continue;+ }+ if (u.decided && rt - u.end_t >= ENDING + BANNER) {+ after_banner();+ return;+ }+ rt += TICK;+ if (rt <= 0 || (u.decided && rt - u.end_t >= ENDING)) continue; /* the countdown; the winner showing */+ static const uint8_t none[NMAX];+ game_step(now_h, first ? t_ : none, first ? r_ : none);+ first = false;+ }+}++/* ---- drawing */+#define BG_TOP 0x2B2452+#define BG_BOT 0x100D22+#define CARD HEX(0x2A2350)+#define EDGE HEX(0x5A4F96)+#define DIM HEX(0x3A3363)+#define SOFT HEX(0xCFC9F2)++static void backdrop(void) { gfx_vgrad(0, 0, SCREEN_W, SCREEN_H, BG_TOP, BG_BOT); }+static void keys_line(int y, const char *s) { say(&np_font_small, 160, y, s, SOFT); }+static void title(int cx, int base) { /* 2 3 4 in three players' colours */+ static const char *const W[4] = {"2", "3", "4", "Players"};+ int w = 0, sp = 7;+ for (int i = 0; i < 4; i++) w += gfx_text_width(&np_font_title, W[i]) + (i < 3 ? sp : 0);+ int x = cx - w / 2;+ for (int i = 0; i < 4; i++) {+ gfx_text(&np_font_title, x + 1, base + 2, W[i], 0, 140);+ x = gfx_text(&np_font_title, x, base, W[i], i < 3 ? gfx_rgb(FFA_RGB[i]) : WHITE, 256) + sp;+ }+}+static void button(int x, int y, int w, const char *s, bool on) {+ box(x, y, w, 26, 13, on ? GOLD : HEX(0x3D3570));+ gfx_text_center(&np_font_body, x + w / 2, y + 18, s, on ? INK : WHITE, 256);+}+static void card(int y, int h) {+ gfx_shadow(40, y, 240, h, 16, 10, 200);+ box(40, y, 240, h, 16, CARD);+ gfx_rrect_outline(40, y, 240, h, 16, 2, EDGE, 256);+}+static void tri(int x, int y, int d, color_t c) { /* a little arrow: right, down, left, up */+ for (int i = 0; i < 4; i++) {+ int a = d < 2 ? 3 - i : i;+ if (d & 1) gfx_fill(x - a, y - 2 + i, 2 * a + 1, 1, c);+ else gfx_fill(x - 2 + i, y - a, 1, 2 * a + 1, c);+ }+}+static void tick_sign(int x, int y, color_t c) {+ line(x - 4, y, x - 1, y + 3, 2.2f, c);+ line(x - 1, y + 3, x + 5, y - 4, 2.2f, c);+}++/* the calculator's keypad, 112 x 136, with each player's key in their colour (lit: those to show) */+static void key_at(int p, int x0, int y0, int *x, int *y, int *w, int *h) {+ static const uint8_t ROW[NMAX] = {0, 0, 6, 6, 3, 3}, COLM[NMAX] = {0, 5, 0, 4, 0, 4};+ int r = ROW[p], c = COLM[p];+ if (r < 3) *x = x0 + 5 + c * 172 / 10, *y = y0 + 34 + r * 11, *w = 15, *h = 8;+ else *x = x0 + 5 + c * 206 / 10, *y = y0 + 69 + (r - 3) * 16, *w = 18, *h = 12;+}+static void keypad(int x0, int y0, int n, uint8_t ready) {+ gfx_shadow(x0, y0, 112, 136, 12, 8, 160);+ box(x0, y0, 112, 136, 12, HEX(0x0D0B1C));+ disc(x0 + 21, y0 + 17, 12, DIM); /* the arrows */+ disc(x0 + 21, y0 + 17, 5, HEX(0x0D0B1C));+ disc(x0 + 56, y0 + 11, 5, DIM); /* Home */+ disc(x0 + 86, y0 + 12, 6, DIM); /* OK, Back */+ disc(x0 + 99, y0 + 24, 6, DIM);+ for (int r = 0; r < 7; r++)+ for (int c = 0; c < (r < 3 ? 6 : 5); c++) {+ int x = r < 3 ? x0 + 5 + c * 172 / 10 : x0 + 5 + c * 206 / 10, y = r < 3 ? y0 + 34 + r * 11 : y0 + 69 + (r - 3) * 16;+ box(x, y, r < 3 ? 15 : 18, r < 3 ? 8 : 12, 3, DIM);+ }+ for (int p = 0; p < n; p++) {+ int x, y, w, h;+ key_at(p, x0, y0, &x, &y, &w, &h);+ if (ready >> p & 1) gfx_rrect(x - 2, y - 2, w + 4, h + 4, 5, WHITE, 256);+ box(x, y, w, h, 3, col[p]);+ gfx_fill_alpha(x + 2, y + 1, w - 4, 2, WHITE, 90);+ }+}+/* a side's chip in the score bar */+static void chip(int x, int w, int s) {+ char t[6];+ box(x, 3, w, 17, 8, col[s]);+ if (teams) gfx_text(&np_font_small, x + 8, 15, TEAM_NAME[s], WHITE, 256);+ else keytag(x + 8 + keytag_w(s) / 2, 16, s, WHITE);+ num(t, (uint32_t)u.score[s]);+ gfx_text_right(&np_font_body, x + w - 7, 16, t, WHITE, 256);+}+static void hud(void) {+ if (gfx_y0 >= TOP) return;+ gfx_fill(0, 0, SCREEN_W, TOP, INK);+ int ns = u.nsides, w = ns <= 2 ? 104 : ns == 3 ? 66 : 56, l = 0, r = 0;+ for (int s = 0; s < ns; s++) /* keys on the left of the keypad on the left, the others on the right */+ if (!(s & 1)) chip(3 + (w + 3) * l++, w, s);+ else chip(SCREEN_W - 3 - w - (w + 3) * r++, w, s);+ gfx_text_center(&np_font_small, 160, 15, "Back pause", SOFT, 256);+}+/* the players' keys over them during the countdown */+static void key_tags(void) {+ for (int p = 0; p < np; p++) {+ if (u.tag_x[p] < -500) continue;+ int w = keytag_w(p) + 10, x = clampi((int)u.tag_x[p] - w / 2, 2, SCREEN_W - 2 - w), y = clampi((int)u.tag_y[p] - 22, TOP + 2, 220);+ box(x, y, w, 17, 6, col[p]);+ gfx_fill(x + w / 2 - 3, y + 17, 6, 2, col[p]);+ keytag(x + w / 2, y + 13, p, WHITE);+ }+}+static void countdown(int32_t left) { /* 3, 2, 1 */+ char s[2] = {(char)('1' + left / 667), 0};+ int k = (int)(left % 667);+ disc_a(160, 128, 34, INK, 200);+ say(&np_font_title, 160, 136, s, k > 560 ? GOLD : WHITE);+}++static void pips(int x, int y, int s, bool pop) { /* a side's rounds: won ones filled */+ int n = sv.target, gap = n > 7 ? 13 : 17;+ for (int i = 0; i < n; i++) {+ int on = i < u.score[s], big = pop && i == u.score[s] - 1;+ disc(x + i * gap, y, big ? 7 : 6, on ? col[s] : HEX(0x1A1636));+ if (on) disc_a(x + i * gap - 2, y - 2, 2, WHITE, 140);+ }+}+static void scores(int y0, bool pop) {+ for (int s = 0; s < u.nsides; s++) {+ int y = y0 + s * (u.nsides > 3 ? 22 : 26);+ blob(66, y, 8, s, 0);+ gfx_text(&np_font_body, 82, y + 5, side_name(s), s == u.winner ? GOLD : WHITE, 256);+ pips(176, y, s, pop && s == u.winner);+ }+}++/* the menu's tiles: Mix, then the games */+#define TW 58+#define TH 52+static void tile_xy(int i, int *x, int *y) { *x = 7 + i % 5 * (TW + 4), *y = 36 + i / 5 * (TH + 4); }+static void mix_icon(int x, int y) {+ static const int8_t D[4][2] = {{14, 10}, {42, 10}, {14, 26}, {42, 26}};+ for (int i = 0; i < 4; i++) {+ box(x + D[i][0] - 9, y + D[i][1] - 7, 18, 14, 4, gfx_rgb(FFA_RGB[i]));+ disc(x + D[i][0] - 3, y + D[i][1] - 1, 2, WHITE), disc(x + D[i][0] + 3, y + D[i][1] - 1, 2, WHITE);+ }+ disc(x + 28, y + 18, 8, WHITE);+ gfx_text_center(&np_font_body, x + 28, y + 23, "?", INK, 256);+}+static void tiles(void) {+ for (int i = 0; i <= NG; i++) {+ int x, y, g = i ? i - 1 : MIX, on = g == sv.game;+ tile_xy(i, &x, &y);+ if (y > gfx_y1 || y + TH < gfx_y0) continue;+ if (on) gfx_rrect(x - 3, y - 3, TW + 6, TH + 6, 11, GOLD, 256);+ box(x, y, TW, TH, 9, on ? HEX(0x4A4086) : HEX(0x2C2654));+ gfx_fill_alpha(x + 1, y + 1, TW - 2, 36, 0, 60);+ if (g == MIX) mix_icon(x + 1, y + 2);+ else GAMES[g]->icon(x + 1, y + 2);+ gfx_text_center(&np_font_small, x + TW / 2, y + 48, g == MIX ? "Mix" : GAMES[g]->shortname, WHITE, 256);+ }+}+static void wins_line(int cx, int base) {+ char t[8];+ int n = teams ? 2 : 4, w = 0, at[4];+ for (int i = 0; i < n; i++) {+ num(t, sv.wins[sv.game][teams ? 4 + i : i]);+ at[i] = w, w += 16 + gfx_text_width(&np_font_body, t) + 12;+ }+ int x = cx - w / 2;+ for (int i = 0; i < n; i++) {+ num(t, sv.wins[sv.game][teams ? 4 + i : i]);+ disc(x + at[i] + 6, base - 4, 6, gfx_rgb(teams ? TEAM_RGB[i] : FFA_RGB[i]));+ gfx_text(&np_font_body, x + at[i] + 16, base, t, WHITE, 256);+ }+}++static void scene(void *ctx) {+ char s[24];+ int sc = u.screen, base = sc >= S_PAUSE ? u.under : sc;+ if (base == S_MODE) {+ backdrop();+ title(160, 30);+ for (int m = 0; m < NMODES; m++) {+ int y = 46 + m * 35, on = m == sv.mode, n = MODE_N[m];+ box(14, y, 162, 29, 14, on ? GOLD : HEX(0x3D3570));+ gfx_text(&np_font_body, 26, y + 20, MODE_NAME[m], on ? INK : WHITE, 256);+ for (int i = 0; i < n; i++) { /* its players' colours, the teams apart */+ int x = 168 - (n - i) * 11 - (m >= 3 && i < n / 2 ? 6 : 0), p = m >= 3 ? (i < n / 2 ? 2 * i : 2 * (i - n / 2) + 1) : i;+ disc(x, y + 14, 5, gfx_rgb(m >= 3 ? TEAM_RGB[p] : FFA_RGB[p]));+ }+ }+ keypad(194, 44, np, 0);+ keys_line(233, "OK choose Back quit");+ } else if (base == S_GAMES) {+ backdrop();+ gfx_text(&np_font_body, 12, 24, "Pick a game", WHITE, 256);+ gfx_text_right(&np_font_small, 308, 23, "OK play Back players", SOFT, 256);+ tiles();+ const char *name = sv.game == MIX ? "Mix: another game every round" : GAMES[sv.game]->name;+ say(&np_font_body, 160, 218, name, GOLD);+ wins_line(160, 235);+ } else if (base == S_RULES) {+ backdrop();+ say(&np_font_title, 160, 28, G()->name, WHITE);+ if (sv.game == MIX) gfx_text_right(&np_font_small, 312, 16, u.started ? "Mix: next game" : "Mix", GOLD, 256);+ int y = gfx_paragraph(&np_font_small, 160, 46, 300, 12, G()->rules, SOFT, 256);+ int top = y < 84 ? 84 : y - 4;+ keypad(10, top, np, u.ready);+ int rows = np, gap = rows > 4 ? 19 : 24;+ for (int i = 0; i < rows; i++) { /* the players, teams together */+ int p = teams ? (i < np / 2 ? 2 * i : 2 * (i - np / 2) + 1) : i, ry = top + 10 + i * gap + (teams && i >= np / 2 ? 4 : 0);+ bool r = u.ready >> p & 1;+ blob(140, ry, 7, p, 0);+ keytag(170, ry + 4, p, lite[p]);+ if (r) {+ box(196, ry - 9, 100, 18, 9, col[p]);+ tick_sign(210, ry, WHITE);+ gfx_text(&np_font_small, 222, ry + 4, "Ready", WHITE, 256);+ } else {+ gfx_rrect_outline(196, ry - 9, 100, 18, 9, 1, DIM, 256);+ gfx_text(&np_font_small, 206, ry + 4, "Press your key", SOFT, 256);+ }+ }+ if (!u.started) { /* the target, until the match starts */+ char *e = s;+ for (const char *q = "First to "; *q; q++) *e++ = *q;+ num(e, sv.target);+ int fy = top + 10 + rows * gap + 6;+ if (fy > 214) fy = 214;+ tri(144, fy, 2, SOFT), tri(296, fy, 0, SOFT);+ gfx_text_center(&np_font_body, 220, fy + 5, s, GOLD, 256);+ }+ keys_line(233, u.started ? "OK start Back menu" : "OK start Back games");+ } else if (base == S_PLAY || base == S_SCORE || base == S_OVER) {+ G()->draw();+ if (rt <= 0 && base == S_PLAY) key_tags();+ hud();+ if (base == S_PLAY) {+ if (rt <= 0) countdown(-rt);+ else if (u.freeze > 0) countdown(u.freeze * (COUNTDOWN - 1) / 1200);+ else if (rt < 600 && !u.decided) say(&np_font_title, 160, 136, "GO!", GOLD);+ if (u.decided && rt - u.end_t >= ENDING) { /* the round's winner */+ int32_t k = rt - u.end_t - ENDING;+ int y = 104 - (k < 160 ? (160 - k) / 8 : 0);+ gfx_rrect(60, y, 200, 58, 16, INK, 225);+ if (u.winner >= 0) {+ say(&np_font_title, 160, y + 28, side_name(u.winner), lite[u.winner]);+ gfx_text_center(&np_font_body, 160, y + 46, "takes the round", WHITE, 256);+ } else {+ say(&np_font_title, 160, y + 28, "Draw", WHITE);+ gfx_text_center(&np_font_body, 160, y + 46, "Play it again", SOFT, 256);+ }+ }+ }+ if (base == S_SCORE || base == S_OVER) gfx_fill_alpha(0, TOP, SCREEN_W, SCREEN_H - TOP, 0, 110);+ if (base == S_SCORE) {+ int h = 64 + u.nsides * (u.nsides > 3 ? 22 : 26);+ card(120 - h / 2, h);+ char *e = s;+ for (const char *q = u.winner >= 0 ? "Round " : "Draw, round "; *q; q++) *e++ = *q;+ num(e, (uint32_t)u.round + 1);+ say(&np_font_body, 160, 120 - h / 2 + 24, s, WHITE);+ scores(120 - h / 2 + 46, u.now - u.shown < 400);+ keys_line(120 + h / 2 - 8, "OK next");+ } else if (base == S_OVER) {+ int w = u.winner, h = 100 + u.nsides * (u.nsides > 3 ? 22 : 26);+ card(118 - h / 2, h);+ say(&np_font_title, 160, 118 - h / 2 + 32, side_name(w), lite[w]);+ gfx_text_center(&np_font_body, 160, 118 - h / 2 + 50, teams ? "wins the match!" : "wins the match!", WHITE, 256);+ scores(118 - h / 2 + 68, false);+ static const char *const ITEM[2] = {"Rematch", "Other games"};+ for (int i = 0; i < 2; i++) button(48 + i * 116, 118 + h / 2 - 34, 108, ITEM[i], u.sel == i && sc == S_OVER);+ }+ }+ if (sc == S_PAUSE) {+ static const char *const ITEM[4] = {"Resume", "Restart", "Other games", "Quit game"};+ gfx_fill_alpha(0, 0, SCREEN_W, SCREEN_H, 0, 120);+ card(36, 176);+ say(&np_font_title, 160, 66, "Paused", WHITE);+ for (int i = 0; i < 4; i++) button(84, 80 + 32 * i, 152, ITEM[i], u.sel == i);+ keys_line(232, "OK choose Back resume");+ } else if (sc == S_ASK) {+ static const char *const ITEM[2] = {"No", "Yes"};+ gfx_fill_alpha(0, 0, SCREEN_W, SCREEN_H, 0, 120);+ card(60, 120);+ say(&np_font_title, 160, 92, "Quit game?", WHITE);+ for (int i = 0; i < 2; i++) button(84, 104 + 32 * i, 152, ITEM[i], u.sel == i);+ keys_line(232, "OK choose Back no");+ }+ (void)ctx;+}++/* ---- the keys */+static color_t strip[SCREEN_W * STRIP_H];+void np_push(int x, int y, int w, int h, const color_t *px) { plat_push(x, y, w, h, px); }+#ifndef NP_PORT /* (the other targets bring their own: platform/common/np_alloc.c) */+void *np_alloc(uint32_t size) { /* (gfx_init's: never called, the strip is ours) */+ (void)size;+ return strip;+}+#endif++void app_init(uint32_t now) {+ extern uint32_t fp_seed;+ memset(&u, 0, sizeof u);+ gfx_buf = strip;+ fp_seed = plat_random() ^ now * 2654435761u;+ if (!fp_seed) fp_seed = 1;+ load();+ set_mode(sv.mode);+ u.now = now, u.cur = sv.game < NG ? sv.game : 0;+ u.stale = plat_keys(); /* keys still down from the launcher wait to be let go */+ open(S_MODE);+}+bool app_dirty(void) { return u.dirty; }++bool app_step(uint64_t keys, uint32_t now) {+ uint32_t ms = now - u.now;+ u.now = now;+ u.stale &= keys, keys &= ~u.stale;+ uint64_t hit = keys & ~u.held;+ u.held = keys;+ if (hit) {+ extern uint32_t fp_seed;+ fp_seed ^= now * 2654435761u ^ (uint32_t)hit; /* the moment a key is pressed adds to the chance */+ if (!fp_seed) fp_seed = 1;+ }+ if (keys & (KEY(FK_HOME) | KEY(FK_ONOFF))) {+ save();+ return false;+ }+ bool back = hit & KEY(FK_BACK), ok = (hit & KEY(FK_OK)) || ((hit & KEY(FK_EXE)) && np < 4);+ int dx = !!(hit & KEY(FK_RIGHT)) - !!(hit & KEY(FK_LEFT)), dy = !!(hit & KEY(FK_DOWN)) - !!(hit & KEY(FK_UP));+ int was = u.screen, was_sel = u.sel, was_game = sv.game, was_mode = sv.mode;+ switch (u.screen) {+ case S_MODE:+ if (dy) sv.mode = (uint8_t)((sv.mode + dy + NMODES) % NMODES), set_mode(sv.mode);+ if (ok) save(), open(S_GAMES);+ else if (back) u.under = S_MODE, open(S_ASK);+ break;+ case S_GAMES: {+ int i = sv.game == MIX ? 0 : sv.game + 1;+ if (dx) i = (i + dx + NG + 1) % (NG + 1);+ if (dy) i = (i + dy * 5 + NG + 1) % (NG + 1);+ sv.game = (uint8_t)(i ? i - 1 : MIX);+ if (ok) save(), new_match();+ else if (back) open(S_MODE);+ break;+ }+ case S_RULES:+ for (int p = 0; p < np; p++)+ if (hit & KEY(PKEY[p])) u.ready |= (uint8_t)(1 << p), u.dirty = true;+ if (dx && !u.started) sv.target = (uint8_t)clampi(sv.target + dx, 1, 9), u.dirty = true;+ if (back) {+ if (u.started) u.under = S_RULES, open(S_PAUSE);+ else open(S_GAMES);+ } else if (ok || u.ready == (1 << np) - 1) {+ if (!u.started) save();+ u.started = true;+ for (int p = 0; p < np; p++) u.prev_h[p] = 1; /* (the key just pressed isn't the round's first tap) */+ begin_round();+ }+ break;+ case S_PLAY:+ if (back) {+ u.under = S_PLAY, open(S_PAUSE);+ break;+ }+ play(ms);+ break;+ case S_SCORE:+ if (back) u.under = S_SCORE, open(S_PAUSE);+ else if ((ok && now - u.shown > 500) || now - u.shown > SCORE_MS) next_round();+ break;+ case S_OVER:+ if (dx || dy) u.sel ^= 1;+ if (now - u.shown < 900) break; /* (a key still being mashed doesn't choose) */+ if (back) open(S_GAMES);+ else if (ok) {+ if (u.sel) open(S_GAMES);+ else new_match();+ }+ break;+ case S_PAUSE:+ u.sel = (u.sel + dy + 4) & 3;+ if (back || (ok && u.sel == 0)) { /* Resume */+ open(u.under);+ if (u.under == S_PLAY && rt > 0 && !u.decided) u.freeze = 1200;+ for (int p = 0; p < np; p++) u.prev_h[p] = (u.held & KEY(PKEY[p])) != 0;+ } else if (ok && u.sel == 1) {+ new_match();+ if (sv.game != MIX) u.started = true, begin_round();+ } else if (ok && u.sel == 2) {+ open(S_GAMES);+ } else if (ok) {+ open(S_ASK);+ }+ break;+ case S_ASK:+ u.sel = (u.sel + dy + 2) & 1;+ if (back || (ok && !u.sel)) open(u.under == S_MODE ? S_MODE : S_PAUSE), u.sel = u.under == S_MODE ? 0 : 3;+ else if (ok) {+ save();+ return false;+ }+ break;+ }+ if (u.screen != was || u.sel != was_sel || sv.game != was_game || sv.mode != was_mode) u.dirty = true;+ if (u.screen == S_PLAY || (u.screen == S_SCORE && now - u.shown < 500)) u.dirty = true;+ return true;+}++void app_draw(void) {+ gfx_render(scene, 0, 0, SCREEN_H);+ u.dirty = false;+}++#ifdef HOST+void app_state(app_state_t *s) {+ s->screen = u.screen, s->mode = sv.mode, s->game = sv.game, s->cur = u.cur, s->target = sv.target;+ s->round = u.round, s->nsides = u.nsides, s->winner = u.winner, s->decided = u.decided, s->rt = rt;+ s->matches = u.matches;+ memcpy(s->score, u.score, sizeof s->score);+ memcpy(s->wins, sv.wins, sizeof sv.wins);+}+uint64_t app_player_key(int p) { return KEY(PKEY[p]); }+/* the rules' tests (tests/rules.c): a round of a game straight away, then steps with the keys held (a bit each) */+void app_test_round(int mode, int game) {+ set_mode(mode);+ u.cur = game, memset(u.score, 0, sizeof u.score), memset(u.prev_h, 0, sizeof u.prev_h);+ begin_round();+ rt = 0;+}+void app_test_step(unsigned held) {+ uint8_t h[NMAX], t[NMAX], r[NMAX];+ for (int p = 0; p < NMAX; p++) {+ h[p] = held >> p & 1, t[p] = h[p] && !u.prev_h[p], r[p] = !h[p] && u.prev_h[p];+ u.prev_h[p] = h[p];+ }+ rt += TICK;+ game_step(h, t, r);+}+int app_test_winner(void) { return u.decided ? u.winner : -2; } /* a side, -1 a draw, -2 not yet */+uint64_t app_bot_keys(void) {+ uint64_t k = 0;+ if (u.screen == S_PLAY && rt > 0 && !u.decided && u.freeze <= 0)+ for (int p = 0; p < np; p++)+ if (!gone[p] && G()->bot(p)) k |= KEY(PKEY[p]);+ return k;+}+#endifdiff --git a/games/fourplayer/src/app.h b/games/fourplayer/src/app.hnew file mode 100644index 00000000..3ac0a247--- /dev/null+++ b/games/fourplayer/src/app.h@@ -0,0 +1,29 @@+/* 2 3 4 Players: the screens and their keys (app.c). */+#ifndef FP_APP_H+#define FP_APP_H+#include <stdbool.h>+#include <stdint.h>++void app_init(uint32_t now);+/* the keys held now (plat_keys); false: the game ends (saved) */+bool app_step(uint64_t keys, uint32_t now);+bool app_dirty(void);+void app_draw(void);++#ifdef HOST /* (tests/play.c) */+enum { S_MODE, S_GAMES, S_RULES, S_PLAY, S_SCORE, S_OVER, S_PAUSE, S_ASK };+typedef struct {+ int screen, mode, game, cur, target, round, nsides, winner, matches;+ int score[4];+ bool decided;+ int32_t rt;+ uint16_t wins[16][6];+} app_state_t;+void app_state(app_state_t *s);+uint64_t app_bot_keys(void); /* the keys the computer players hold now (the game's bot) */+uint64_t app_player_key(int p);+void app_test_round(int mode, int game);+void app_test_step(unsigned held);+int app_test_winner(void);+#endif+#endifdiff --git a/games/fourplayer/src/fp.h b/games/fourplayer/src/fp.hnew file mode 100644index 00000000..e64a9c07--- /dev/null+++ b/games/fourplayer/src/fp.h@@ -0,0 +1,105 @@+/* 2 3 4 Players: what the minigames share (kit.c, app.c), and the minigames (g_*.c).+ *+ * A minigame plays rounds: start() sets one up, step() moves it on by TICK milliseconds with each player's+ * key, draw() paints it (the launcher's strips: called once a strip, every shape clips itself). It ends a round+ * with win() or tie(), or, in a last-one-standing game, with out() until one side is left. */+#ifndef FP_H+#define FP_H+#include <stdbool.h>+#include <stdint.h>+#include "../../../launcher/src/gfx.h"++#define NMAX 6+#define TICK 10 /* ms, a step of the games */+#define DT 0.01f /* the same in seconds */+#define TOP 22 /* the score bar: the games below it */+#define PI 3.14159265f+#define WHITE 0xFFFF+#define INK HEX(0x1B1733) /* the menus' night violet */+#define GOLD HEX(0xFFC93C)++/* ---- the round, for the minigame playing it */+extern int np; /* players: 2 to 6 */+extern bool teams; /* teams: players 0, 2, 4 are the red team, 1, 3, 5 the blue */+extern uint8_t hold[NMAX], tap[NMAX], rel[NMAX]; /* each one's key this step: held, just pressed, just let go */+extern uint8_t gone[NMAX]; /* out of the round */+extern int32_t rt; /* the round's time in ms, from GO */+extern color_t col[NMAX], lite[NMAX], dark[NMAX]; /* each player's colours */+int side(int p); /* p's side: their team, or themselves */+void out(int p); /* p is out (in a last-one-standing game, the last side left takes the round) */+void win(int p); /* p's side takes the round */+void tie(void); /* nobody does: the round is played again */+bool over(void); /* the round is decided: the game plays on a moment, keys off */+int alive(void); /* players still in */+/* where p's key is on the keypad, seen from its middle (radians, 0 right, y down): where p starts */+float seat(int p);+/* p's key, as printed on it ("shift", "0"...; backspace is drawn: keyname(p) gives "" for it) */+const char *keyname(int p);++/* ---- chance and numbers */+uint32_t rnd(void);+int rn(int n); /* 0 to n - 1 */+float rf(void); /* 0 to 1 */+float rr(float a, float b);+float fsin(float a);+float fcos(float a);+float fsqrt(float v);+float fabs_(float v);+float clampf(float v, float a, float b);+int clampi(int v, int a, int b);+float wrap_pi(float a); /* to -pi..pi */+float fatan2(float y, float x);+typedef struct {+ float x, y, vx, vy;+} body_t;+/* two discs closer than r: pushed apart, their speeds changed as masses ma and mb would (e: the bounce) */+bool collide(body_t *a, body_t *b, float r, float ma, float mb, float e);+#ifdef HOST+int bot_tap(int p, bool want); /* a computer player's tap: pressed for one frame, then let go */+bool bot_fresh(int p); /* a new round for this computer player (once) */+#endif++/* ---- drawing (on the strip being drawn) */+void disc(float x, float y, float r, color_t c);+void disc_a(float x, float y, float r, color_t c, int a256);+void box(int x, int y, int w, int h, int r, color_t c);+/* a rectangle turned to angle a around (x, y): from l0 to l1 along a, w to each side */+void quad(float x, float y, float a, float l0, float l1, float w, color_t c);+void poly(const float *xy, int n, color_t c); /* a convex polygon */+void line(float x0, float y0, float x1, float y1, float w, color_t c);+void say(const np_font_t *f, int cx, int base, const char *s, color_t c); /* centred, with a shadow */+/* a player: a round body looking towards look (radians), r its radius */+void blob(float x, float y, float r, int p, float look);+void eyes(float x, float y, float r, float look);+/* p's key label at (cx, base), centred, in c (the backspace key's sign is drawn) */+void keytag(int cx, int base, int p, color_t c);+int keytag_w(int p);+/* where p is drawn (the top of them): their key's label shows there during the countdown */+void tag_at(int p, float x, float y);+char *num(char *o, uint32_t v);++/* ---- the minigames */+typedef struct {+ const char *name, *shortname, *rules;+ uint8_t last; /* the last side standing takes the round */+ void (*start)(void);+ void (*step)(void);+ void (*draw)(void);+ void (*icon)(int x, int y); /* its picture in the menu, 56 x 34 */+#ifdef HOST+ int (*bot)(int p); /* tests: whether a computer player holds its key now */+#endif+} game_t;++#ifdef HOST+#define BOT(f) , f+#else+#define BOT(f)+#endif++enum { G_SUMO, G_RACE, G_TANKS, G_SNAKES, G_SOCCER, G_FISH, G_SKATE, G_ROPE, G_PAINT, G_BALLOON, G_BIRDS, G_GRAVITY,+ G_FROG, G_CLOCK, NG };+extern const game_t g_sumo, g_race, g_tanks, g_snakes, g_soccer, g_fish, g_skate, g_rope, g_paint, g_balloon, g_birds,+ g_gravity, g_frog, g_clock;+extern const game_t *const GAMES[NG];+#endifdiff --git a/games/fourplayer/src/g_balloon.c b/games/fourplayer/src/g_balloon.cnew file mode 100644index 00000000..7ad873a9--- /dev/null+++ b/games/fourplayer/src/g_balloon.c@@ -0,0 +1,115 @@+/* Balloons: every tap pumps your balloon bigger, but each one bursts at its own size, which nobody knows. After+ * 10 seconds the biggest balloon still whole takes the round (in teams, the most air between you). */+#include "fp.h"++#define TIME 10000+static int pumps[NMAX], burst_at[NMAX];+static int32_t pump_t[NMAX], burst_t[NMAX], last_t[NMAX];++static const uint8_t ORDER[4][NMAX] = {{0, 1}, {0, 2, 1}, {0, 2, 3, 1}, {0, 4, 2, 3, 5, 1}}; /* left to right */+static float column(int p) {+ const uint8_t *o = ORDER[np == 2 ? 0 : np == 3 ? 1 : np == 4 ? 2 : 3];+ int i = 0;+ while (o[i] != p) i++;+ return (i + 0.5f) * SCREEN_W / np;+}+static float radius(int p) {+ float big = SCREEN_W / np / 2.0f - 5;+ if (big > 34) big = 34;+ return 7 + pumps[p] * (big - 7) / 40;+}+static void start(void) {+ for (int p = 0; p < np; p++) pumps[p] = 0, burst_at[p] = 18 + rn(19), pump_t[p] = burst_t[p] = last_t[p] = -10000;+}+static void decide(void) {+ int ns = teams ? 2 : np, air[NMAX] = {0}, whole = 0, best = -1, n = 0, who = -1;+ for (int p = 0; p < np; p++)+ if (burst_t[p] < 0) whole |= 1 << side(p), air[side(p)] = teams ? air[side(p)] + pumps[p] : pumps[p];+ if (!whole) {+ tie();+ return;+ }+ if (rt < TIME && (whole & (whole - 1))) return; /* (two sides or more still have a whole balloon) */+ for (int s = 0; s < ns; s++) {+ if (!(whole >> s & 1)) continue;+ if (air[s] > best || (!teams && air[s] == best && last_t[s] < last_t[who])) best = air[s], n = 1, who = s;+ else if (air[s] == best && (teams || last_t[s] == last_t[who])) n++;+ }+ if (n == 1) win(who);+ else tie();+}+static void step(void) {+ for (int p = 0; p < np; p++) {+ if (!tap[p] || burst_t[p] >= 0 || rt >= TIME) continue;+ pumps[p]++, pump_t[p] = last_t[p] = rt;+ if (pumps[p] >= burst_at[p]) burst_t[p] = rt;+ }+ if (!over()) decide();+}++static void balloon(int p, float x, float y, float r) {+ disc(x, y + r * 0.08f, r, dark[p]);+ disc(x, y - r * 0.05f, r * 0.96f, col[p]);+ disc_a(x - r * 0.38f, y - r * 0.42f, r * 0.26f, WHITE, 150);+ float k[6] = {x, y + r - 1, x - 3, y + r + 4, x + 3, y + r + 4};+ poly(k, 3, dark[p]);+}+static void draw(void) {+ gfx_vgrad(0, TOP, SCREEN_W, SCREEN_H - TOP, 0x9FD8F7, 0xE9F7FF);+ for (int i = 0; i < 3; i++) { /* clouds */+ float cx = 50 + i * 115 + fsin(rt * 0.0002f + i) * 8, cy = 50 + (i & 1) * 18;+ disc_a(cx, cy, 13, WHITE, 200), disc_a(cx + 14, cy + 3, 10, WHITE, 200), disc_a(cx - 14, cy + 4, 9, WHITE, 200);+ }+ gfx_fill(0, 222, SCREEN_W, SCREEN_H - 222, HEX(0x7BBF5A));+ char t[6];+ for (int p = 0; p < np; p++) {+ float x = column(p), r = radius(p), shake = pumps[p] > 14 ? fsin(rt * 0.06f + p * 1.7f) * (pumps[p] - 14) * 0.07f : 0;+ float by = 168 - r;+ int32_t k = rt - pump_t[p];+ int down = k < 140 ? (int)(8 - k / 18) : 0;+ line(x, 214, x, 182, 2, HEX(0x5A5466)); /* the tube */+ if (burst_t[p] < 0) balloon(p, x + shake, by, r);+ else {+ int32_t b = rt - burst_t[p];+ for (int i = 0; i < 7 && b < 600; i++) {+ float a = i * 0.9f, d = r * 0.6f + b * 0.08f, px = x + fcos(a) * d, py = by + fsin(a) * d + b * b * 0.00006f;+ float s[6] = {px, py, px + 4, py + 2, px + 1, py + 5};+ poly(s, 3, col[p]);+ }+ if (b < 700) say(&np_font_title, (int)x, (int)by + 7, "POP", WHITE);+ quad(x, 180, PI / 2 + 0.4f, -2, 8, 2.5f, dark[p]); /* what's left */+ }+ gfx_fill((int)x - 2, 196 + down - 18, 4, 18, HEX(0x8A8494)); /* the pump: its handle, its body */+ box((int)x - 9, 192 + down - 18, 18, 4, 2, HEX(0x3D3846));+ box((int)x - 8, 196, 16, 26, 4, col[p]);+ gfx_fill((int)x - 8, 200, 16, 3, dark[p]);+ num(t, (uint32_t)pumps[p]);+ gfx_text_center(&np_font_body, (int)x, 236, t, INK, 256);+ tag_at(p, x, by - r);+ }+ num(t, (uint32_t)(rt < TIME ? (TIME - rt + 999) / 1000 : 0));+ if (rt > 0) say(&np_font_title, 160, TOP + 24, t, WHITE);+}++static void icon(int x, int y) {+ box(x + 2, y + 1, 52, 32, 7, HEX(0x9FD8F7));+ disc(x + 18, y + 13, 9, col[0]), disc(x + 40, y + 16, 6, col[1]);+ disc_a(x + 15, y + 10, 3, WHITE, 150);+ gfx_fill(x + 17, y + 22, 2, 9, HEX(0x5A5466)), gfx_fill(x + 39, y + 22, 2, 9, HEX(0x5A5466));+}++#ifdef HOST+static int bot(int p) {+ static int32_t next[NMAX];+ static int goal[NMAX];+ if (bot_fresh(p)) next[p] = 0, goal[p] = 16 + rn(14);+ if (pumps[p] >= goal[p] || rt < next[p]) return bot_tap(p, false);+ next[p] = rt + 120 + rn(80);+ return bot_tap(p, true);+}+#endif++const game_t g_balloon = {"Balloons", "Balloons",+ "Tap your key to pump. Each balloon bursts at its own size, and nobody knows which. After 10 "+ "seconds the biggest balloon still whole takes the round.",+ 0, start, step, draw, icon BOT(bot)};diff --git a/games/fourplayer/src/g_birds.c b/games/fourplayer/src/g_birds.cnew file mode 100644index 00000000..9f22a0c5--- /dev/null+++ b/games/fourplayer/src/g_birds.c@@ -0,0 +1,109 @@+/* Birds: every tap is a flap. Fly through the gaps between the columns: touch one, or the ground, and you're+ * out. The columns come faster and the gaps get smaller. The last bird flying takes the round. */+#include "fp.h"++#define GROUND 226+#define BR 7.0f+#define NCOL 4+#define CW 26 /* a column's width */+static float y[NMAX], vy[NMAX], scroll, speed;+static int32_t dead_t[NMAX], flap_t[NMAX];+static struct {+ float x, gap, mid;+} c[NCOL];++static float bx(int p) { return 60 + p * 13; }+static void column(int i, float x) {+ float gap = 78 - (rt > 0 ? rt : 0) * 0.0006f;+ if (gap < 58) gap = 58;+ c[i].x = x, c[i].gap = gap, c[i].mid = rr(TOP + 22 + gap / 2, GROUND - 16 - gap / 2);+}+static void start(void) {+ for (int p = 0; p < np; p++) y[p] = 110 + (p & 1) * 16 - p * 3, vy[p] = -120, dead_t[p] = -1, flap_t[p] = -10000;+ for (int i = 0; i < NCOL; i++) column(i, 360 + i * 150);+ scroll = 0, speed = 80;+}+static void step(void) {+ speed = 80 + (rt < 40000 ? rt : 40000) * 0.001f;+ for (int i = 0; i < NCOL; i++) {+ c[i].x -= speed * DT;+ if (c[i].x < -CW) column(i, c[(i + NCOL - 1) % NCOL].x + 150);+ }+ scroll += speed * DT;+ for (int p = 0; p < np; p++) {+ if (dead_t[p] >= 0 && y[p] >= GROUND - BR) continue;+ if (tap[p] && dead_t[p] < 0) vy[p] = -235, flap_t[p] = rt;+ vy[p] += 760 * DT;+ if (vy[p] > 340) vy[p] = 340;+ y[p] += vy[p] * DT;+ if (y[p] < TOP + BR) y[p] = TOP + BR, vy[p] = 0;+ if (y[p] > GROUND - BR) y[p] = GROUND - BR;+ if (dead_t[p] >= 0) continue;+ bool hit = y[p] >= GROUND - BR;+ for (int i = 0; i < NCOL && !hit; i++) {+ float dx = bx(p) - clampf(bx(p), c[i].x, c[i].x + CW), top = c[i].mid - c[i].gap / 2, bot = c[i].mid + c[i].gap / 2;+ if (fabs_(dx) < BR - 1 && (y[p] - BR + 1.5f < top || y[p] + BR - 1.5f > bot)) hit = true;+ else if (fabs_(dx) < BR - 1) continue;+ else { /* the corners */+ float dt_ = y[p] - top, db = y[p] - bot;+ if ((dt_ < 0 && dx * dx + dt_ * dt_ < (BR - 1.5f) * (BR - 1.5f)) || (db > 0 && dx * dx + db * db < (BR - 1.5f) * (BR - 1.5f))) hit = true;+ }+ }+ if (hit) dead_t[p] = rt, out(p), vy[p] = vy[p] < 0 ? 0 : vy[p];+ }+}++static void bird(int p, float x, float yy) {+ float tilt = clampf(vy[p] * 0.004f, -0.5f, 1.2f);+ int32_t k = rt - flap_t[p];+ float beak[6] = {x + fcos(tilt) * 6, yy + fsin(tilt) * 6 - 1, x + fcos(tilt) * 12, yy + fsin(tilt) * 12 + 1, x + fcos(tilt + 0.5f) * 6, yy + fsin(tilt + 0.5f) * 6 + 2};+ disc_a(x + 1, yy + 2, BR, 0, 60);+ disc(x, yy, BR, col[p]);+ poly(beak, 3, HEX(0xFF9F1C));+ eyes(x + 1, yy - 1, 6, tilt);+ quad(x - 3, yy + 2, k < 120 ? -2.2f : 2.6f, 0, 6, 2.2f, dark[p]); /* the wing */+}+static void draw(void) {+ gfx_vgrad(0, TOP, SCREEN_W, GROUND - TOP, 0x5BC0EB, 0xD7F2FC);+ for (int i = 0; i < 5; i++) { /* far hills */+ float x = i * 96 - ((int)(scroll * 0.25f) % 96) + 30;+ disc(x, GROUND + 20, 56, HEX(0xA8D9A0));+ }+ for (int i = 0; i < NCOL; i++) {+ int x = (int)c[i].x, top = (int)(c[i].mid - c[i].gap / 2), bot = (int)(c[i].mid + c[i].gap / 2);+ color_t body = HEX(0x8E9BB8), edge = HEX(0x5F6A88), lit = HEX(0xB4BED6);+ gfx_fill(x, TOP, CW, top - TOP, body), gfx_fill(x, bot, CW, GROUND - bot, body);+ gfx_fill(x + 4, TOP, 4, top - TOP, lit), gfx_fill(x + 4, bot, 4, GROUND - bot, lit);+ gfx_fill(x + CW - 5, TOP, 5, top - TOP, edge), gfx_fill(x + CW - 5, bot, 5, GROUND - bot, edge);+ box(x - 3, top - 10, CW + 6, 10, 2, edge), box(x - 3, bot, CW + 6, 10, 2, edge);+ }+ gfx_fill(0, GROUND, SCREEN_W, SCREEN_H - GROUND, HEX(0xD8C27A));+ for (int k = 0; k < 22; k++) gfx_fill(k * 16 - ((int)scroll % 16), GROUND, 8, 3, HEX(0x8CC152));+ gfx_fill(0, GROUND + 3, SCREEN_W, 2, HEX(0x6EA03E));+ for (int p = np - 1; p >= 0; p--) {+ bird(p, bx(p), y[p]);+ if (dead_t[p] < 0) tag_at(p, bx(p), y[p] - BR);+ }+}++static void icon(int x0, int y0) {+ box(x0 + 2, y0 + 1, 52, 32, 7, HEX(0x5BC0EB));+ gfx_fill(x0 + 36, y0 + 1, 10, 9, HEX(0x8E9BB8)), gfx_fill(x0 + 36, y0 + 24, 10, 9, HEX(0x8E9BB8));+ disc(x0 + 16, y0 + 13, 5, col[0]), disc(x0 + 24, y0 + 21, 5, col[1]);+ disc(x0 + 21, y0 + 13, 1.5f, HEX(0xFF9F1C)), disc(x0 + 29, y0 + 21, 1.5f, HEX(0xFF9F1C));+}++#ifdef HOST+static int bot(int p) {+ float target = 131, best = 1e9f; /* the middle of the nearest gap ahead */+ for (int i = 0; i < NCOL; i++)+ if (c[i].x + CW > bx(p) - BR && c[i].x < best) best = c[i].x, target = c[i].mid + 6;+ float noise = (p * 7 % 5) * 1.5f;+ return bot_tap(p, y[p] > target + noise && vy[p] > -40);+}+#endif++const game_t g_birds = {"Birds", "Birds",+ "Every tap of your key is a flap. Fly through the gaps: touch a column or the ground and "+ "you're out. The last bird flying takes the round.",+ 1, start, step, draw, icon BOT(bot)};diff --git a/games/fourplayer/src/g_clock.c b/games/fourplayer/src/g_clock.cnew file mode 100644index 00000000..8718ab31--- /dev/null+++ b/games/fourplayer/src/g_clock.c@@ -0,0 +1,108 @@+/* Stop the Clock: a stopwatch runs, then hides after two seconds. Tap your key when you think it shows the+ * time asked: the closest takes the round. */+#include "fp.h"++#define SHOW 2000 /* ms the stopwatch shows */+#define CX 160.0f+#define CY 124.0f+static int32_t target, stopped[NMAX], reveal_t;++static void start(void) {+ target = 5000 + rn(6) * 1000, reveal_t = -1;+ for (int p = 0; p < NMAX; p++) stopped[p] = -1;+}+static int32_t off(int p) { return stopped[p] > target ? stopped[p] - target : target - stopped[p]; }+static void step(void) {+ if (reveal_t >= 0) return;+ int waiting = 0;+ for (int p = 0; p < np; p++) {+ if (tap[p] && stopped[p] < 0) stopped[p] = rt;+ waiting += stopped[p] < 0;+ }+ if (waiting && rt < target + 4000) return;+ reveal_t = rt;+ int best = -1, n = 0;+ for (int p = 0; p < np; p++) {+ if (stopped[p] < 0) continue;+ if (best < 0 || off(p) < off(best)) best = p, n = 1;+ else if (off(p) == off(best) && side(p) != side(best)) n++;+ }+ if (best >= 0 && n == 1) win(best);+ else tie();+}++static void time_text(char *s, int32_t ms) { /* 6.87 */+ char *e = num(s, (uint32_t)(ms / 1000));+ int c = (int)(ms % 1000 / 10);+ e[0] = '.', e[1] = (char)('0' + c / 10), e[2] = (char)('0' + c % 10), e[3] = 0;+}+static void draw(void) {+ char s[24];+ gfx_vgrad(0, TOP, SCREEN_W, SCREEN_H - TOP, 0x3A2F6E, 0x1A1538);+ int32_t now = rt < 0 ? 0 : rt;+ bool shown = now < SHOW || reveal_t >= 0;+ char *e = s;+ for (const char *q = "Stop at "; *q; q++) *e++ = *q;+ time_text(e, target);+ say(&np_font_body, 160, TOP + 20, s, GOLD);+ disc(CX, CY - 44, 6, HEX(0xC9C3E8)); /* the stopwatch */+ disc(CX + 2, CY + 3, 44, HEX(0x0F0C22));+ disc(CX, CY, 44, HEX(0xC9C3E8));+ disc(CX, CY, 39, WHITE);+ for (int k = 0; k < 12; k++) {+ float a = k * PI / 6;+ line(CX + fcos(a) * 33, CY + fsin(a) * 33, CX + fcos(a) * 37, CY + fsin(a) * 37, k % 3 ? 1.2f : 2.2f, HEX(0x5A5378));+ }+ if (shown) {+ int32_t t = reveal_t >= 0 ? target : now;+ float a = t * (2 * PI / 10000) - PI / 2;+ line(CX, CY, CX + fcos(a) * 30, CY + fsin(a) * 30, 2.2f, HEX(0xE8433E));+ time_text(s, reveal_t >= 0 ? target : now);+ } else {+ disc(CX, CY, 33, HEX(0xE6E2F5));+ s[0] = '?', s[1] = '.', s[2] = '?', s[3] = '?', s[4] = 0;+ }+ disc(CX, CY, 3, HEX(0x2A2440));+ gfx_text_center(&np_font_title, (int)CX, (int)CY + 26, s, HEX(0x2A2440), 256);+ for (int p = 0; p < np; p++) {+ float a = seat(p), x = CX + fcos(a) * 112, y = CY + 4 + fsin(a) * 66;+ if (y > 214) y = 214;+ blob(x, y, 11, p, fatan2(CY - y, CX - x));+ tag_at(p, x, y - 11);+ int by = (int)y + 24;+ if (reveal_t >= 0 && stopped[p] >= 0) {+ time_text(s, stopped[p]);+ say(&np_font_body, (int)x, by, s, lite[p]);+ char d[12];+ d[0] = stopped[p] >= target ? '+' : '-';+ time_text(d + 1, off(p));+ gfx_text_center(&np_font_small, (int)x, by + 12, d, WHITE, 256);+ } else if (reveal_t >= 0) {+ say(&np_font_small, (int)x, by, "No time", WHITE);+ } else if (stopped[p] >= 0) {+ box((int)x - 20, by - 11, 40, 14, 7, col[p]);+ gfx_text_center(&np_font_small, (int)x, by, "Stop", WHITE, 256);+ }+ }+}++static void icon(int x, int y) {+ box(x + 2, y + 1, 52, 32, 7, HEX(0x3A2F6E));+ disc(x + 28, y + 18, 13, HEX(0xC9C3E8));+ disc(x + 28, y + 18, 11, WHITE);+ disc(x + 28, y + 4, 3, HEX(0xC9C3E8));+ line(x + 28, y + 18, x + 34, y + 12, 2, HEX(0xE8433E));+ disc(x + 10, y + 26, 4, col[0]), disc(x + 46, y + 26, 4, col[1]);+}++#ifdef HOST+static int bot(int p) {+ int32_t when = target + (int32_t)((rn(1000) + rn(1000) - 1000) * 0.5f) + p * 7;+ return bot_tap(p, stopped[p] < 0 && rt >= when - 200 && rt <= when + 200 && rn(4) == 0);+}+#endif++const game_t g_clock = {"Stop the Clock", "Clock",+ "The stopwatch hides after two seconds. Tap your key when you think it shows the time asked. "+ "The closest takes the round.",+ 0, start, step, draw, icon BOT(bot)};diff --git a/games/fourplayer/src/g_fish.c b/games/fourplayer/src/g_fish.cnew file mode 100644index 00000000..99410ae1--- /dev/null+++ b/games/fourplayer/src/g_fish.c@@ -0,0 +1,162 @@+/* Grab the Fish: cats round a pond. Things jump out of it: tap to swipe, and the first paw on a goldfish keeps+ * it. Swipe at a boot, a can or a puffer fish, or at nothing, and you're dizzy a moment. Three goldfish take the+ * round (in teams, three between you). */+#include "fp.h"++#define PX 160.0f /* the pond */+#define PY 134.0f+#define UP 1100 /* ms a thing is out of the water */+#define GOAL 3+enum { FISH, BOOT, CAN, PUFFER };+static int item, stage, grabber; /* stage: 0 under water, 1 out; grabber: who caught the last one (-1: it fell back) */+static int32_t item_t, done_t, react[NMAX];+static int32_t paw_t[NMAX], dizzy_t[NMAX], dizzy_ms[NMAX];+static uint8_t caught[NMAX], tally[NMAX];+static float cx[NMAX], cy[NMAX];++static void next_item(int32_t after) {+ item = rn(100) < 55 ? FISH : 1 + rn(3), stage = 0, item_t = rt + after;+ for (int p = 0; p < np; p++) react[p] = 230 + rn(260);+}+static void start(void) {+ for (int p = 0; p < np; p++) {+ float s = seat(p);+ cx[p] = PX + fcos(s) * 124, cy[p] = PY + fsin(s) * 80;+ if (cy[p] > 222) cy[p] = 222;+ paw_t[p] = dizzy_t[p] = -10000, dizzy_ms[p] = 0, caught[p] = tally[p] = 0;+ }+ done_t = -10000, grabber = -1;+ next_item(1200);+}+static bool dizzy(int p) { return rt - dizzy_t[p] < dizzy_ms[p]; }++static void step(void) {+ if (stage == 0 && rt >= item_t) stage = 1;+ if (stage == 1 && rt - item_t >= UP) grabber = -1, done_t = rt, next_item(900 + rn(1700));+ int first = -1, n = 0;+ for (int p = 0; p < np; p++) {+ if (!tap[p] || dizzy(p)) continue;+ paw_t[p] = rt;+ bool up = stage == 1 && rt - item_t > 80;+ if (up && item == FISH) {+ if (rn(++n) == 0) first = p; /* (two paws at once: either) */+ } else {+ dizzy_t[p] = rt, dizzy_ms[p] = up ? 1800 : 900;+ }+ }+ if (first >= 0) {+ caught[first]++, tally[side(first)]++;+ grabber = first, done_t = rt;+ if (tally[side(first)] >= GOAL) win(first);+ else next_item(1000 + rn(1600));+ }+}++static void fish(float x, float y, float s, float a, color_t c) { /* a goldfish, s its size */+ float ca = fcos(a), sa = fsin(a);+ float tail[6] = {x - ca * s * 0.8f, y - sa * s * 0.8f, x - ca * s * 1.6f - sa * s * 0.6f, y - sa * s * 1.6f + ca * s * 0.6f,+ x - ca * s * 1.6f + sa * s * 0.6f, y - sa * s * 1.6f - ca * s * 0.6f};+ poly(tail, 3, HEX(0xF07A1A));+ disc(x, y, s, c);+ disc(x + ca * s * 0.45f, y + sa * s * 0.45f, s * 0.75f, c);+ disc(x + ca * s * 0.6f - sa * s * 0.25f, y + sa * s * 0.6f - s * 0.25f, s * 0.22f > 1 ? s * 0.22f : 1, HEX(0x1A1A1A));+}+static void thing(float x, float y, float a) {+ switch (item) {+ case FISH: fish(x, y, 7, a - PI / 2 + 0.3f, HEX(0xFFB51E)); break;+ case BOOT:+ quad(x, y, a, -9, 2, 3.5f, HEX(0x7A4E2A));+ quad(x + fcos(a) * 3 + fsin(a) * 4, y + fsin(a) * 3 - fcos(a) * 4, a + PI / 2, -3, 6, 4, HEX(0x7A4E2A));+ quad(x, y, a, -9, -7, 3.6f, HEX(0x4E3018));+ break;+ case CAN:+ quad(x, y, a, -7, 7, 4.5f, HEX(0xA8B0BC));+ quad(x, y, a, -2, 2, 4.6f, HEX(0xD8463E));+ break;+ default:+ for (int i = 0; i < 8; i++) {+ float b = a + i * PI / 4, t[6] = {x + fcos(b) * 10, y + fsin(b) * 10, x + fcos(b + 0.4f) * 6, y + fsin(b + 0.4f) * 6,+ x + fcos(b - 0.4f) * 6, y + fsin(b - 0.4f) * 6};+ poly(t, 3, HEX(0x6E8F2A));+ }+ disc(x, y, 7, HEX(0x9CC23F));+ eyes(x, y, 7, PI / 2);+ }+}+static void cat(int p) {+ float x = cx[p], y = cy[p], look = fatan2(PY - y, PX - x);+ for (int k = -1; k <= 1; k += 2) { /* the ears */+ float e[6] = {x + k * 12, y - 6, x + k * 4, y - 12, x + k * 13, y - 19};+ poly(e, 3, dark[p]);+ }+ blob(x, y, 13, p, look);+ disc(x, y + 4, 2, HEX(0xF4A0B0));+ for (int k = -1; k <= 1; k += 2) line(x + k * 5, y + 4, x + k * 15, y + 2 + k * 0, 1, lite[p]);+ if (dizzy(p))+ for (int i = 0; i < 3; i++) {+ float b = rt * 0.008f + i * 2.1f;+ disc(x + fcos(b) * 12, y - 16 + fsin(b) * 4, 2, GOLD);+ }+ int32_t k = rt - paw_t[p];+ if (k < 260) { /* the swipe */+ float f = k < 110 ? k / 110.0f : (260 - k) / 150.0f, px = x + (PX - x) * 0.8f * f, py = y + (PY - 10 - y) * 0.8f * f;+ line(x, y, px, py, 7, col[p]);+ disc(px, py, 5.5f, lite[p]);+ }+ for (int i = 0; i < GOAL; i++) { /* its goldfish */+ float fx = x - 16 + i * 14, fy = y + 22;+ if (fy > 234) fy = y - 30;+ if (i < caught[p]) fish(fx, fy, 4, 0, HEX(0xFFB51E));+ else disc_a(fx, fy, 3, 0, 60);+ }+ tag_at(p, x, y - 18);+}+static void draw(void) {+ for (int y = gfx_y0 < TOP ? TOP : gfx_y0; y < gfx_y1; y++) gfx_fill(0, y, SCREEN_W, 1, (y / 5) & 1 ? HEX(0x5DAA4A) : HEX(0x58A346));+ gfx_rrect(PX - 78, PY - 52, 156, 104, 52, HEX(0xE6D29A), 256);+ gfx_rrect(PX - 72, PY - 46, 144, 92, 46, HEX(0x2D7FC4), 256);+ gfx_rrect(PX - 64, PY - 38, 128, 76, 38, HEX(0x3590D6), 256);+ if (stage == 0 && item_t - rt < 700) { /* something under the water */+ float r = 8 + (rt % 600) / 40.0f;+ disc_a(PX, PY, r, HEX(0x7CC0EE), 140 - (int)r * 4);+ }+ if (stage == 1) {+ float f = (rt - item_t) / (float)UP, h = 52 * 4 * f * (1 - f);+ disc_a(PX, PY + 2, 10 * (1 - f) + 4, HEX(0x1F5F9A), 140);+ thing(PX, PY - h, (f - 0.5f) * 1.6f);+ }+ if (rt - done_t < 500) {+ float f = (rt - done_t) / 500.0f;+ if (grabber >= 0) fish(PX + (cx[grabber] - PX) * f, PY - 30 + (cy[grabber] + 22 - PY + 30) * f, 7 - 3 * f, 0, HEX(0xFFB51E));+ else disc_a(PX, PY, 6 + f * 14, WHITE, (int)(200 * (1 - f)));+ }+ for (int p = 0; p < np; p++) cat(p);+ if (teams)+ for (int s = 0; s < 2; s++)+ for (int i = 0; i < GOAL; i++) {+ float x = s ? 214 + i * 14 : 78 + i * 14;+ if (i < tally[s]) fish(x, TOP + 12, 4, 0, HEX(0xFFB51E));+ else disc_a(x, TOP + 12, 3, 0, 60);+ if (!i) disc(x - 14, TOP + 12, 4, col[s]);+ }+}++static void icon(int x, int y) {+ box(x + 2, y + 1, 52, 32, 7, HEX(0x5DAA4A));+ gfx_rrect(x + 11, y + 16, 34, 15, 7, HEX(0x3590D6), 256);+ fish(x + 30, y + 10, 5, -0.3f, HEX(0xFFB51E));+ disc(x + 8, y + 25, 5, col[0]), disc(x + 48, y + 25, 5, col[1]);+}++#ifdef HOST+static int bot(int p) {+ bool up = stage == 1 && rt - item_t > react[p];+ bool want = up && (item == FISH || (react[p] % 17 == 0 && rt - item_t > 300));+ return bot_tap(p, want && !dizzy(p));+}+#endif++const game_t g_fish = {"Grab the Fish", "Fish",+ "Things jump out of the pond. Tap your key to swipe: the first paw on a goldfish keeps it. Swipe "+ "at anything else and you're dizzy. Three goldfish take the round.",+ 0, start, step, draw, icon BOT(bot)};diff --git a/games/fourplayer/src/g_frog.c b/games/fourplayer/src/g_frog.cnew file mode 100644index 00000000..b5af27db--- /dev/null+++ b/games/fourplayer/src/g_frog.c@@ -0,0 +1,137 @@+/* Frog Jump: lily pads across a river, the same for everyone, each in their own lane. Hold your key to gather+ * a jump (a ring shows where you'd land), let go to jump. Miss the pads and you swim back. The first frog+ * across takes the round. */+#include "fp.h"++#define NPAD 10+#define MAP 18 /* the map of the race, at the top */+#define FULL 700 /* ms to a full jump */+#define FLY 360+#define BANK 36.0f /* the start's bank */+static float pad[NPAD], finish;+static struct {+ float x, from, to, cam;+ int st; /* on a pad or a bank, in the air, swimming back */+ int32_t charge_t, fly_t, splash_t; /* charge_t: -1 not gathering */+} f[NMAX];+enum { SIT, AIR, SWIM };++static int lane_h(void) { return (SCREEN_H - TOP - MAP) / np; }+static int lane_y(int p) {+ static const uint8_t ORDER[4][NMAX] = {{0, 1}, {0, 1, 2}, {0, 1, 2, 3}, {0, 4, 2, 1, 5, 3}}; /* the teams together */+ const uint8_t *o = ORDER[np == 2 ? 0 : np == 3 ? 1 : np == 4 ? 2 : 3];+ int i = 0;+ while (o[i] != p) i++;+ return TOP + MAP + i * lane_h();+}+static float jump_len(int32_t held) { return 16 + 114 * clampf(held / (float)FULL, 0, 1); }++static void start(void) {+ float x = BANK + rr(20, 40);+ for (int k = 0; k < NPAD; k++) pad[k] = x, x += rr(36, 78);+ finish = x - 10;+ for (int p = 0; p < np; p++) f[p].x = 22, f[p].cam = 0, f[p].st = SIT, f[p].charge_t = -1, f[p].fly_t = f[p].splash_t = -10000;+}+static bool flying(int p) { return f[p].st == AIR; }+static void step(void) {+ int first = -1;+ for (int p = 0; p < np; p++) {+ if (f[p].st == AIR && rt - f[p].fly_t >= FLY) { /* landing */+ float x = f[p].to;+ bool ok = x >= finish - 6;+ for (int k = 0; k < NPAD; k++) ok |= fabs_(x - pad[k]) <= 11;+ f[p].x = x, f[p].st = ok ? SIT : SWIM, f[p].splash_t = ok ? f[p].splash_t : rt;+ if (x >= finish - 6 && (first < 0 || x > f[first].x)) first = p;+ }+ if (f[p].st == SWIM && rt - f[p].splash_t >= 700) f[p].x = f[p].from, f[p].st = SIT; /* swum back */+ if (f[p].st == SIT) {+ if (tap[p]) f[p].charge_t = rt;+ if (rel[p] && f[p].charge_t >= 0)+ f[p].from = f[p].x, f[p].to = f[p].x + jump_len(rt - f[p].charge_t), f[p].fly_t = rt, f[p].st = AIR;+ }+ if (!hold[p] || f[p].st != SIT) f[p].charge_t = -1;+ float want = clampf(f[p].x - 64, 0, finish + 40 - SCREEN_W);+ f[p].cam += (want - f[p].cam) * 0.12f;+ }+ if (first >= 0) win(first);+}++static float frog_x(int p) {+ if (!flying(p)) return f[p].x;+ return f[p].from + (f[p].to - f[p].from) * (rt - f[p].fly_t) / FLY;+}+static void draw(void) {+ int h = lane_h();+ gfx_fill(0, TOP, SCREEN_W, MAP, HEX(0x1E3A5A)); /* the race's map */+ gfx_fill(16, TOP + 9, 288, 2, HEX(0x5E86AE));+ gfx_fill(300, TOP + 3, 3, 14, WHITE);+ for (int p = 0; p < np; p++) disc(16 + clampf(frog_x(p) / finish, 0, 1) * 284, TOP + 10, 4, col[p]);+ for (int p = 0; p < np; p++) {+ int y0 = lane_y(p);+ if (y0 + h <= gfx_y0 || y0 >= gfx_y1) continue;+ float cam = f[p].cam, mid = y0 + h / 2.0f, pr = h * 0.3f > 11 ? 11 : h * 0.3f;+ gfx_fill(0, y0, SCREEN_W, h, HEX(0x2F86C8));+ for (int k = 0; k < 9; k++) gfx_fill((int)(k * 40 - ((int)(cam + rt * 0.01f) % 40)), y0 + h / 3 + (k & 1) * h / 3, 14, 1, HEX(0x5AA6DE));+ gfx_fill(0, y0, SCREEN_W, 1, HEX(0x1E3A5A));+ if (BANK - cam > 0) gfx_fill(0, y0 + 1, (int)(BANK - cam), h - 1, HEX(0x5DAA4A));+ int fx = (int)(finish - cam);+ if (fx < SCREEN_W) {+ gfx_fill(fx, y0 + 1, SCREEN_W - fx, h - 1, HEX(0x5DAA4A));+ for (int k = 0; k < (h - 1) / 4; k++) gfx_fill(fx + 2 + (k & 1) * 4, y0 + 1 + k * 4, 4, 4, WHITE);+ }+ for (int k = 0; k < NPAD; k++) {+ float x = pad[k] - cam;+ if (x < -20 || x > SCREEN_W + 20) continue;+ disc(x, mid + 2, pr, HEX(0x2E7A3C));+ disc(x, mid, pr, HEX(0x58B04A));+ float n[6] = {x, mid, x + pr, mid - pr * 0.3f, x + pr, mid + pr * 0.3f};+ poly(n, 3, HEX(0x2F86C8));+ }+ float x = frog_x(p) - cam, y = mid - 3;+ if (flying(p)) {+ float t = (rt - f[p].fly_t) / (float)FLY;+ y -= 4 * h * 0.28f * t * (1 - t);+ }+ if (f[p].charge_t >= 0) { /* where it would land */+ float lx = f[p].x + jump_len(rt - f[p].charge_t) - cam;+ disc(lx, mid, 5, WHITE), disc(lx, mid, 3, HEX(0x2F86C8));+ line(x, mid, lx, mid, 1, lite[p]);+ }+ int32_t s = rt - f[p].splash_t;+ if (f[p].st == SWIM) {+ disc_a(x, mid, 4 + s / 40.0f, WHITE, 220 - s / 4);+ disc_a(x, mid, 4, col[p], 200 - s / 4);+ } else {+ float r = h > 40 ? 9 : 7;+ disc(x - r * 0.45f, y - r * 0.8f, r * 0.42f, col[p]), disc(x + r * 0.45f, y - r * 0.8f, r * 0.42f, col[p]);+ blob(x, y, r, p, 0);+ tag_at(p, x, y - r);+ }+ }+}++static void icon(int x, int y) {+ box(x + 2, y + 1, 52, 32, 7, HEX(0x2F86C8));+ disc(x + 14, y + 22, 6, HEX(0x58B04A)), disc(x + 34, y + 20, 6, HEX(0x58B04A));+ disc(x + 24, y + 12, 5, col[0]);+ disc(x + 21, y + 8, 2, col[0]), disc(x + 27, y + 8, 2, col[0]);+ disc(x + 47, y + 17, 3, WHITE);+}++#ifdef HOST+static int bot(int p) {+ if (f[p].st != SIT) return 0;+ if (f[p].charge_t < 0) return 1;+ float target = finish + 4;+ for (int k = NPAD - 1; k >= 0; k--)+ if (pad[k] > f[p].x + 20 && pad[k] - f[p].x <= 128) target = pad[k];+ if (finish - f[p].x <= 126) target = finish + 2;+ float err = (p * 13 % 7 - 3) * 1.8f;+ return f[p].x + jump_len(rt - f[p].charge_t) < target + err;+}+#endif++const game_t g_frog = {"Frog Jump", "Frogs",+ "Hold your key to gather a jump: the ring shows where you'll land. Let go to jump. Miss the "+ "lily pads and you swim back. The first frog across takes the round.",+ 0, start, step, draw, icon BOT(bot)};diff --git a/games/fourplayer/src/g_gravity.c b/games/fourplayer/src/g_gravity.cnew file mode 100644index 00000000..4b5c2072--- /dev/null+++ b/games/fourplayer/src/g_gravity.c@@ -0,0 +1,134 @@+/* Gravity Run: everyone runs on their own, on the floor or on the ceiling. A tap flips your gravity (from the+ * floor or the ceiling, not in the air). Fall through a hole and you're out: the last runner left takes the+ * round. It gets faster and faster. */+#include "fp.h"++#define CEIL (TOP + 22) /* the ceiling's underside */+#define FLOOR 216 /* the floor's top */+#define R 7.0f+#define NGAP 16+static struct {+ float x0, x1;+ uint8_t up; /* in the ceiling */+} gap[NGAP];+static int ngap;+static float world, speed, next_x; /* world: the screen's left edge, in the run's pixels */+static struct {+ float y, vy;+ int g; /* 1 down, -1 up */+ bool on;+ int32_t dead_t;+} r[NMAX];++static float rx(int p) { return 64 + p * 17; }+static void add_gap(void) {+ float later = (rt < 60000 ? (rt > 0 ? rt : 0) : 60000) / 60000.0f; /* (holes get wider, and come sooner) */+ float w = rr(30, 56) + later * 40;+ int k = ngap < NGAP ? ngap++ : 0;+ if (ngap == NGAP) /* (the oldest one goes: it's long gone) */+ for (int i = 1; i < NGAP; i++)+ if (gap[i].x1 < gap[k].x1) k = i;+ gap[k].x0 = next_x, gap[k].x1 = next_x + w, gap[k].up = (uint8_t)rn(2);+ next_x += w + speed * (0.9f - later * 0.2f) + rr(0, 120 - later * 60);+}+static bool solid(float x, int up) {+ for (int i = 0; i < ngap; i++)+ if (gap[i].up == up && x > gap[i].x0 && x < gap[i].x1) return false;+ return true;+}+static void start(void) {+ world = 0, speed = 105, ngap = 0, next_x = 420;+ for (int p = 0; p < np; p++) r[p].g = 1, r[p].y = FLOOR - R, r[p].vy = 0, r[p].on = true, r[p].dead_t = -1;+ if (np > 2) /* (half of them start on the ceiling) */+ for (int p = 1; p < np; p += 2) r[p].g = -1, r[p].y = CEIL + R;+ while (next_x < world + 700) add_gap();+}+static void step(void) {+ speed = 105 + (rt < 45000 ? rt : 45000) * 0.0015f;+ world += speed * DT;+ while (next_x < world + 700) add_gap();+ for (int p = 0; p < np; p++) {+ if (gone[p] && r[p].dead_t >= 0 && rt - r[p].dead_t > 1500) continue;+ if (tap[p] && r[p].on) r[p].g = -r[p].g, r[p].on = false, r[p].vy = r[p].g * 60.0f;+ float x = world + rx(p);+ bool under = r[p].g > 0 ? solid(x - R * 0.6f, 0) || solid(x + R * 0.6f, 0) : solid(x - R * 0.6f, 1) || solid(x + R * 0.6f, 1);+ if (r[p].on && !under) r[p].on = false; /* the ground ends */+ if (!r[p].on) {+ r[p].vy = clampf(r[p].vy + 900 * r[p].g * DT, -420, 420);+ float ny = r[p].y + r[p].vy * DT;+ if (r[p].g > 0 && ny + R >= FLOOR && r[p].y + R <= FLOOR + 6 && under) ny = FLOOR - R, r[p].on = true, r[p].vy = 0;+ if (r[p].g < 0 && ny - R <= CEIL && r[p].y - R >= CEIL - 6 && under) ny = CEIL + R, r[p].on = true, r[p].vy = 0;+ r[p].y = ny;+ }+ if (!gone[p] && (r[p].y > SCREEN_H + R || r[p].y < TOP - R)) out(p), r[p].dead_t = rt;+ }+}++static void band(int y0, int y1, int up) {+ int h = y1 - y0;+ if (y1 <= gfx_y0 || y0 >= gfx_y1) return;+ for (int x = 0; x < SCREEN_W;) { /* the solid stretches, then the holes */+ float wx = world + x;+ bool s = solid(wx + 0.5f, up);+ int e = x + 1;+ while (e < SCREEN_W && solid(world + e + 0.5f, up) == s) e++;+ if (s) {+ gfx_fill(x, y0, e - x, h, HEX(0x4F5BC4));+ gfx_fill(x, up ? y1 - 3 : y0, e - x, 3, HEX(0x8D9BFF));+ for (int k = (int)((x + world) / 16) * 16 + 16; k < world + e; k += 16) gfx_fill((int)(k - world), y0 + (up ? 0 : 3), 1, h - 3, HEX(0x3E489E));+ }+ x = e;+ }+}+static void draw(void) {+ gfx_vgrad(0, TOP, SCREEN_W, SCREEN_H - TOP, 0x1A1E3A, 0x2E2A5C);+ for (int i = 0; i < 40; i++) { /* stars, slower than the run */+ int sx = (int)((i * 97 + 13) % 340 - (int)(world * 0.2f) % 340), sy = TOP + 26 + (i * 53) % 168;+ if (sx < 0) sx += 340;+ gfx_fill(sx, sy, 1 + (i % 3 == 0), 1 + (i % 3 == 0), i & 1 ? HEX(0x8A86C8) : HEX(0xD7D4FF));+ }+ band(TOP, CEIL, 1);+ band(FLOOR, SCREEN_H, 0);+ for (int p = 0; p < np; p++) {+ if (r[p].dead_t >= 0 && rt - r[p].dead_t > 1500) continue;+ float x = rx(p), y = r[p].y, f = r[p].g;+ if (r[p].on) { /* running feet */+ int k = (int)(rt / 90 + p) & 1;+ disc(x - 3 + k * 2, y + f * (R - 1), 2.5f, dark[p]);+ disc(x + 3 - k * 2, y + f * (R - 1), 2.5f, dark[p]);+ }+ blob(x, y, R, p, f > 0 ? 0 : 0.6f);+ float a[6] = {x - 3, y - f * (R + 3), x + 3, y - f * (R + 3), x, y - f * (R + 7)}; /* which way is down */+ if (!r[p].on) poly(a, 3, lite[p]);+ tag_at(p, x, f > 0 ? y - R : y + R + 26);+ }+}++static void icon(int x, int y) {+ box(x + 2, y + 1, 52, 32, 7, HEX(0x1A1E3A));+ gfx_fill(x + 2, y + 1, 30, 6, HEX(0x4F5BC4)), gfx_fill(x + 40, y + 1, 14, 6, HEX(0x4F5BC4));+ gfx_fill(x + 2, y + 27, 18, 6, HEX(0x4F5BC4)), gfx_fill(x + 30, y + 27, 24, 6, HEX(0x4F5BC4));+ disc(x + 16, y + 22, 4, col[0]), disc(x + 26, y + 12, 4, col[1]);+}++#ifdef HOST+static int bot(int p) {+ static float skip[NMAX]; /* a hole it didn't see coming, until it's past */+ if (bot_fresh(p)) skip[p] = 0;+ float x = world + rx(p), d = speed * (0.12f + (p * 3 % 5) * 0.02f);+ if (!r[p].on || x < skip[p]) return bot_tap(p, false);+ int here = r[p].g < 0;+ bool hole = !solid(x + d, here), other = solid(x + speed * 0.7f, !here) && solid(x + speed * 0.5f, !here);+ if (hole && rn(100) < 3 + rt / 8000) {+ for (int i = 0; i < ngap; i++)+ if (gap[i].up == here && x + d > gap[i].x0 && x + d < gap[i].x1) skip[p] = gap[i].x1;+ return bot_tap(p, false);+ }+ return bot_tap(p, hole && (other || rn(3) == 0));+}+#endif++const game_t g_gravity = {"Gravity Run", "Gravity",+ "You run on your own. Tap your key to flip your gravity, from the floor to the ceiling and "+ "back. Fall through a hole and you're out: the last runner left takes the round.",+ 1, start, step, draw, icon BOT(bot)};diff --git a/games/fourplayer/src/g_paint.c b/games/fourplayer/src/g_paint.cnew file mode 100644index 00000000..ba672074--- /dev/null+++ b/games/fourplayer/src/g_paint.c@@ -0,0 +1,124 @@+/* Paint: rollers that never stop, always curving. A tap makes yours curve the other way. Paint the floor your+ * colour, over the others' too: after 30 seconds, the most floor takes the round. Teams paint together. */+#include "fp.h"++#define CS 4 /* a cell of floor */+#define GW 80+#define GH 54+#define Y0 (TOP + 1)+#define SPEED 72.0f+#define CURVE 2.1f+#define BRUSH 7.0f+#define TIME 30000+static uint8_t floor_[GH][GW]; /* 0 bare, else the side + 1 */+static int count[NMAX];+static color_t paint[NMAX];+static struct {+ float x, y, a;+ int sgn;+} r[NMAX];++static void start(void) {+ for (int y = 0; y < GH; y++)+ for (int x = 0; x < GW; x++) floor_[y][x] = 0;+ for (int s = 0; s < NMAX; s++) count[s] = 0, paint[s] = gfx_mix(col[s], WHITE, 9);+ for (int p = 0; p < np; p++) {+ float s = seat(p);+ r[p].x = 160 + fcos(s) * 110, r[p].y = 131 + fsin(s) * 70, r[p].a = s + PI + 0.6f, r[p].sgn = p & 1 ? 1 : -1;+ }+}+static void roll(int p) { /* paints under p's roller */+ int s = side(p) + 1, cx = (int)(r[p].x / CS), cy = (int)((r[p].y - Y0) / CS);+ for (int y = cy - 2; y <= cy + 2; y++)+ for (int x = cx - 2; x <= cx + 2; x++) {+ if (x < 0 || y < 0 || x >= GW || y >= GH || floor_[y][x] == s) continue;+ float dx = x * CS + 2 - r[p].x, dy = Y0 + y * CS + 2 - r[p].y;+ if (dx * dx + dy * dy > BRUSH * BRUSH) continue;+ if (floor_[y][x]) count[floor_[y][x] - 1]--;+ floor_[y][x] = (uint8_t)s, count[s - 1]++;+ }+}+static void step(void) {+ for (int p = 0; p < np; p++) {+ if (tap[p]) r[p].sgn = -r[p].sgn;+ r[p].a = wrap_pi(r[p].a + r[p].sgn * CURVE * DT);+ r[p].x += fcos(r[p].a) * SPEED * DT, r[p].y += fsin(r[p].a) * SPEED * DT;+ if (r[p].x < 6 || r[p].x > SCREEN_W - 6) r[p].a = wrap_pi(PI - r[p].a), r[p].x = clampf(r[p].x, 6, SCREEN_W - 6);+ if (r[p].y < TOP + 6 || r[p].y > SCREEN_H - 6) r[p].a = -r[p].a, r[p].y = clampf(r[p].y, TOP + 6, SCREEN_H - 6);+ }+ for (int p = 0; p < np; p++)+ for (int q = p + 1; q < np; q++) {+ body_t a = {r[p].x, r[p].y, fcos(r[p].a) * SPEED, fsin(r[p].a) * SPEED};+ body_t b = {r[q].x, r[q].y, fcos(r[q].a) * SPEED, fsin(r[q].a) * SPEED};+ if (!collide(&a, &b, 14, 1, 1, 1)) continue;+ r[p].x = a.x, r[p].y = a.y, r[p].a = fatan2(a.vy, a.vx);+ r[q].x = b.x, r[q].y = b.y, r[q].a = fatan2(b.vy, b.vx);+ }+ if (!over())+ for (int p = 0; p < np; p++) roll(p);+ if (rt >= TIME && !over()) {+ int best = -1, n = 0, who = 0; /* (side s is player s's: in teams, players 0 and 1 lead them) */+ for (int s = 0; s < (teams ? 2 : np); s++)+ if (count[s] > best) best = count[s], n = 1, who = s;+ else if (count[s] == best) n++;+ if (n == 1) win(who);+ else tie();+ }+}++static void draw(void) {+ int r0 = clampi((gfx_y0 - Y0) / CS, 0, GH), r1 = clampi((gfx_y1 - Y0 + CS - 1) / CS, 0, GH);+ color_t bare = HEX(0xF1ECE1);+ gfx_fill(0, TOP, SCREEN_W, SCREEN_H - TOP, HEX(0xD9D1C0));+ for (int row = r0; row < r1; row++) /* the floor, straight into the strip */+ for (int yy = 0; yy < CS; yy++) {+ int y = Y0 + row * CS + yy;+ if (y < gfx_y0 || y >= gfx_y1) continue;+ color_t *px = gfx_buf + (y - gfx_y0) * SCREEN_W;+ for (int c = 0; c < GW; c++) {+ int v = floor_[row][c];+ color_t k = v ? paint[v - 1] : bare;+ px[c * 4] = px[c * 4 + 1] = px[c * 4 + 2] = px[c * 4 + 3] = k;+ }+ }+ for (int p = 0; p < np; p++) {+ float c = fcos(r[p].a), s = fsin(r[p].a);+ quad(r[p].x + c * 8, r[p].y + s * 8, r[p].a + PI / 2, -7, 7, 2.5f, dark[p]); /* the roller */+ line(r[p].x, r[p].y, r[p].x + c * 8, r[p].y + s * 8, 2, HEX(0x5A5466));+ blob(r[p].x, r[p].y, 7, p, r[p].a);+ tag_at(p, r[p].x, r[p].y - 7);+ }+ int total = GW * GH, x = 0; /* the shares, along the bottom */+ for (int s = 0; s < (teams ? 2 : np); s++) {+ int w = count[s] * SCREEN_W / total;+ gfx_fill(x, SCREEN_H - 4, w, 4, col[s]);+ x += w;+ }+ char t[4];+ num(t, (uint32_t)(rt < TIME ? (TIME - rt + 999) / 1000 : 0));+ if (rt > 0) say(&np_font_title, 160, TOP + 24, t, INK);+}++static void icon(int x, int y) {+ box(x + 2, y + 1, 52, 32, 7, HEX(0xF1ECE1));+ gfx_rrect(x + 4, y + 4, 26, 18, 8, gfx_mix(col[0], WHITE, 9), 256);+ gfx_rrect(x + 26, y + 14, 26, 17, 8, gfx_mix(col[1], WHITE, 9), 256);+ disc(x + 26, y + 12, 5, col[0]), disc(x + 30, y + 24, 5, col[1]);+}++#ifdef HOST+static int bot(int p) {+ static int32_t next[NMAX];+ if (bot_fresh(p)) next[p] = 0;+ float ax = r[p].x + fcos(r[p].a) * 24, ay = r[p].y + fsin(r[p].a) * 24;+ bool wall = ax < 0 || ax > SCREEN_W || ay < TOP || ay > SCREEN_H;+ if (rt < next[p] && !wall) return bot_tap(p, false);+ next[p] = rt + 400 + rn(1200);+ return bot_tap(p, true);+}+#endif++const game_t g_paint = {"Paint", "Paint",+ "Your roller never stops and always curves. Tap your key to curve the other way. After 30 "+ "seconds, the most floor in your colour takes the round.",+ 0, start, step, draw, icon BOT(bot)};diff --git a/games/fourplayer/src/g_race.c b/games/fourplayer/src/g_race.cnew file mode 100644index 00000000..a5c8b945--- /dev/null+++ b/games/fourplayer/src/g_race.c@@ -0,0 +1,170 @@+/* Racing: little cars on a loop, driven anticlockwise. They go on their own; holding the key turns left, and+ * every bend turns left. Grass slows, the island's barrier pushes you along it, oil takes your steering away+ * a moment. Three laps: the first one home takes the round. */+#include "fp.h"++#define OX 6 /* the track: between the outer rounded rectangle and the island's */+#define OY 26+#define OW 308+#define OH 210+#define OR 74+#define IX 54+#define IY 74+#define IW 212+#define IH 114+#define IR 28+#define MX 160.0f /* its middle: laps are counted round it */+#define MY 131.0f+#define LAPS 3+#define CR 5.5f /* a car */+#define TURN 3.0f /* radians a second */+#define FAST 125.0f+#define SLOW 50.0f+static struct {+ float x, y, h, va, s, prog, last_a; /* h: where it points, va: where it goes, prog: radians driven round */+ int32_t oil_t, bump_t;+} c[NMAX];+static float oil[2][2];++/* how far (x, y) is outside a rounded rectangle (negative inside) */+static float rbox(float x, float y, float bx, float by, float bw, float bh, float r) {+ float qx = fabs_(x - (bx + bw / 2)) - (bw / 2 - r), qy = fabs_(y - (by + bh / 2)) - (bh / 2 - r);+ float mx = qx > 0 ? qx : 0, my = qy > 0 ? qy : 0, in = qx > qy ? qx : qy;+ return fsqrt(mx * mx + my * my) + (in < 0 ? in : 0) - r;+}+static float island(float x, float y) { return rbox(x, y, IX, IY, IW, IH, IR); }++static void start(void) {+ static const float SPOT[3][2] = {{160, 46}, {28, 131}, {292, 131}}; /* oil: on the top straight or a side */+ int skip = rn(3);+ for (int i = 0, k = 0; i < 3; i++)+ if (i != skip) oil[k][0] = SPOT[i][0] + rr(-30, 30) * (i == 0), oil[k][1] = SPOT[i][1] + rr(-22, 22) * (i > 0), k++;+ for (int p = 0; p < np; p++) {+ int row = p / 3, lane = p % 3, lanes = np < 3 ? np : 3;+ c[p].x = 148 - row * 22 - lane * 3, c[p].y = 212 + (lane - (lanes - 1) / 2.0f) * 14;+ c[p].h = c[p].va = 0, c[p].s = 0, c[p].oil_t = -10000, c[p].bump_t = -10000;+ c[p].last_a = fatan2(c[p].y - MY, c[p].x - MX), c[p].prog = PI / 2 - c[p].last_a;+ }+}++/* against a wall whose way out is (nx, ny): along it, a little slower */+static void slide(int p, float nx, float ny, float push) {+ c[p].x += nx * push, c[p].y += ny * push;+ float vx = fcos(c[p].va), vy = fsin(c[p].va), vn = vx * nx + vy * ny;+ if (vn >= 0) return;+ vx -= vn * nx, vy -= vn * ny;+ if (vx * vx + vy * vy < 0.01f) vx = -ny, vy = nx;+ c[p].va = c[p].h = fatan2(vy, vx);+ if (rt - c[p].bump_t > 200) c[p].s *= 0.7f;+ c[p].bump_t = rt;+}++static void step(void) {+ for (int p = 0; p < np; p++) {+ bool oily = rt - c[p].oil_t < 550, grass = rbox(c[p].x, c[p].y, OX, OY, OW, OH, OR) > 0;+ if (hold[p] && !oily) c[p].h = wrap_pi(c[p].h - TURN * DT);+ float top = grass ? SLOW : hold[p] ? FAST * 0.94f : FAST;+ if (over() && rt > 0) top = c[p].s * 0.98f; /* (the round decided: they roll to a stop) */+ c[p].s += c[p].s < top ? 150 * DT : -260 * DT;+ if (c[p].s < 0) c[p].s = 0;+ if (!oily) /* the tyres grip: where it goes follows where it points, a little late */+ c[p].va = wrap_pi(c[p].va + wrap_pi(c[p].h - c[p].va) * (grass ? 0.05f : 0.14f));+ c[p].x += fcos(c[p].va) * c[p].s * DT, c[p].y += fsin(c[p].va) * c[p].s * DT;+ for (int k = 0; k < 2; k++) {+ float dx = c[p].x - oil[k][0], dy = c[p].y - oil[k][1];+ if (dx * dx + dy * dy < 81 && !oily) c[p].oil_t = rt;+ }+ }+ for (int p = 0; p < np; p++)+ for (int q = p + 1; q < np; q++) {+ body_t a = {c[p].x, c[p].y, fcos(c[p].va) * c[p].s, fsin(c[p].va) * c[p].s};+ body_t b = {c[q].x, c[q].y, fcos(c[q].va) * c[q].s, fsin(c[q].va) * c[q].s};+ if (!collide(&a, &b, 2 * CR, 1, 1, 0.4f)) continue;+ c[p].x = a.x, c[p].y = a.y, c[p].s = fsqrt(a.vx * a.vx + a.vy * a.vy), c[p].va = fatan2(a.vy, a.vx);+ c[q].x = b.x, c[q].y = b.y, c[q].s = fsqrt(b.vx * b.vx + b.vy * b.vy), c[q].va = fatan2(b.vy, b.vx);+ }+ for (int p = 0; p < np; p++) {+ float d = island(c[p].x, c[p].y);+ if (d < CR) { /* the island's barrier */+ float e = 0.5f, gx = island(c[p].x + e, c[p].y) - island(c[p].x - e, c[p].y);+ float gy = island(c[p].x, c[p].y + e) - island(c[p].x, c[p].y - e), l = fsqrt(gx * gx + gy * gy);+ if (l > 0.0001f) slide(p, gx / l, gy / l, CR - d);+ }+ if (c[p].x < CR) slide(p, 1, 0, CR - c[p].x);+ if (c[p].x > SCREEN_W - CR) slide(p, -1, 0, c[p].x - (SCREEN_W - CR));+ if (c[p].y < TOP + CR) slide(p, 0, 1, TOP + CR - c[p].y);+ if (c[p].y > SCREEN_H - CR) slide(p, 0, -1, c[p].y - (SCREEN_H - CR));+ float a = fatan2(c[p].y - MY, c[p].x - MX);+ c[p].prog -= wrap_pi(a - c[p].last_a), c[p].last_a = a;+ if (c[p].prog >= LAPS * 2 * PI) win(p);+ }+}++static int lap(int p) { return clampi((int)(c[p].prog / (2 * PI)) + 1, 1, LAPS); }++static void car(int p, float x, float y, float h) {+ quad(x + 1.5f, y + 2.5f, h, -7, 7, 5, HEX(0x2C3A2F)); /* shadow */+ for (int i = 0; i < 4; i++) {+ float l = i < 2 ? 3.5f : -4.5f, e = i & 1 ? 4.2f : -4.2f;+ quad(x + fcos(h) * l - fsin(h) * e, y + fsin(h) * l + fcos(h) * e, h, -2, 2, 1.4f, HEX(0x15131C));+ }+ quad(x, y, h, -7, 7, 3.6f, col[p]);+ quad(x, y, h, -1, 3.5f, 2.6f, HEX(0x1E2A3A)); /* the windscreen */+ quad(x, y, h, -6.5f, -4.5f, 3.2f, dark[p]);+ quad(x, y, h, 5.5f, 7, 3.2f, lite[p]);+}++static void draw(void) {+ for (int y = gfx_y0 < TOP ? TOP : gfx_y0; y < gfx_y1; y++) gfx_fill(0, y, SCREEN_W, 1, (y >> 3) & 1 ? HEX(0x3F9A48) : HEX(0x46A250));+ gfx_rrect(OX - 3, OY - 3, OW + 6, OH + 6, OR + 3, HEX(0xF2F2F2), 256);+ gfx_rrect(OX, OY, OW, OH, OR, HEX(0x52555F), 256);+ for (int i = 0; i < 12; i++) { /* the start line, chequered */+ int y = 188 + i * 4;+ gfx_fill(156, y, 4, 4, i & 1 ? HEX(0x1A1A1A) : WHITE);+ gfx_fill(160, y, 4, 4, i & 1 ? WHITE : HEX(0x1A1A1A));+ }+ gfx_rrect(IX - 3, IY - 3, IW + 6, IH + 6, IR + 3, HEX(0xE84A3F), 256);+ gfx_rrect(IX, IY, IW, IH, IR, HEX(0x2F7F3A), 256);+ for (int k = 0; k < 2; k++) {+ float ox = oil[k][0], oy = oil[k][1];+ disc_a(ox - 4, oy + 1, 6, HEX(0x1C1626), 235), disc_a(ox + 3, oy - 2, 7, HEX(0x1C1626), 235);+ disc_a(ox + 1, oy + 4, 5, HEX(0x1C1626), 235);+ disc_a(ox + 3, oy - 4, 2, HEX(0x8C7BD8), 170), disc_a(ox - 4, oy + 2, 1.5f, HEX(0x5FC0C8), 150);+ }+ /* the laps, on the island */+ gfx_text_center(&np_font_small, 160, 96, "LAP", HEX(0xBFE5C4), 256);+ for (int p = 0; p < np; p++) {+ char s[6], *e = num(s, (uint32_t)lap(p));+ e[0] = '/', e[1] = (char)('0' + LAPS), e[2] = 0;+ int x = 160 + (p % 3 - (np < 3 ? np - 1 : 2) / 2.0f) * 56, y = 120 + p / 3 * 22;+ disc(x - 14, y - 4, 5, col[p]);+ gfx_text(&np_font_body, x - 6, y, s, WHITE, 256);+ }+ for (int p = 0; p < np; p++) {+ float h = c[p].h;+ if (rt - c[p].oil_t < 550) h += (rt - c[p].oil_t) * 0.012f; /* a spin on the oil */+ car(p, c[p].x, c[p].y, h);+ tag_at(p, c[p].x, c[p].y - 6);+ }+}++static void icon(int x, int y) {+ box(x + 2, y + 1, 52, 32, 7, HEX(0x3F9A48));+ gfx_rrect(x + 5, y + 4, 46, 26, 12, HEX(0x52555F), 256);+ gfx_rrect(x + 15, y + 12, 26, 10, 5, HEX(0x2F7F3A), 256);+ box(x + 20, y + 24, 9, 5, 2, col[0]);+ box(x + 32, y + 24, 9, 5, 2, col[1]);+}++#ifdef HOST+static int bot(int p) {+ float a = fatan2(c[p].y - MY, c[p].x - MX) - 0.55f, tx = MX + fcos(a) * 134, ty = MY + fsin(a) * 86;+ float want = fatan2(ty - c[p].y, tx - c[p].x);+ return wrap_pi(want - c[p].h) < -0.06f;+}+#endif++const game_t g_race = {"Racing", "Racing",+ "Your car drives itself. Hold your key to turn left: every bend turns left. Grass slows you, oil "+ "spins you. Three laps: the first one home takes the round.",+ 0, start, step, draw, icon BOT(bot)};diff --git a/games/fourplayer/src/g_rope.c b/games/fourplayer/src/g_rope.cnew file mode 100644index 00000000..b1cbf195--- /dev/null+++ b/games/fourplayer/src/g_rope.c@@ -0,0 +1,113 @@+/* Jump Rope: a bar sweeps round the platform. Tap to jump over it as it comes; caught on the ground, you're+ * thrown off. It speeds up, turns back now and then, and after a while a second bar joins it. The last one on+ * the platform takes the round. */+#include "fp.h"++#define CX 160.0f+#define CY 131.0f+#define PLAT 98.0f /* the platform */+#define STAND 70.0f /* where the players stand */+#define AIR 520 /* ms in the air */+static float beam, w;+static int dir;+static int32_t flip_at, jump_t[NMAX], fall_t[NMAX];+static bool two;++static void start(void) {+ beam = seat(0) + PI + 0.3f, w = 0, dir = rn(2) ? 1 : -1, two = false;+ flip_at = 6000 + rn(5000);+ for (int p = 0; p < np; p++) jump_t[p] = fall_t[p] = -10000;+}+static float height(int p) {+ int32_t k = rt - jump_t[p];+ if (k >= AIR) return 0;+ float f = k / (float)AIR;+ return 4 * 22 * f * (1 - f);+}+static float speed(void) { return 1.7f + (rt < 40000 ? rt : 40000) * 0.000085f; }++static void step(void) {+ if (rt >= flip_at) dir = -dir, flip_at = rt + 5000 + rn(6000);+ if (rt > 22000) two = true;+ float want = dir * speed();+ w += clampf(want - w, -6 * DT, 6 * DT);+ float old = beam;+ beam = wrap_pi(beam + w * DT);+ for (int p = 0; p < np; p++) {+ if (gone[p]) continue;+ if (tap[p] && rt - jump_t[p] >= AIR + 120) jump_t[p] = rt;+ if (height(p) > 6) continue;+ float a = seat(p);+ for (int k = 0; k < 1 + two; k++) {+ float d0 = wrap_pi(a - old - k * PI), d1 = wrap_pi(a - beam - k * PI);+ if ((d0 < 0) != (d1 < 0) && fabs_(d0) < 1 && fabs_(d1) < 1) out(p), fall_t[p] = rt;+ }+ }+}++static void bar(float a) {+ float c = fcos(a), s = fsin(a);+ quad(CX + 3, CY + 5, a, 0, PLAT + 4, 4, HEX(0x8E6A38)); /* its shadow */+ quad(CX, CY, a, 0, PLAT + 4, 4.5f, HEX(0xE8433E));+ quad(CX, CY, a, 0, PLAT + 4, 1.6f, HEX(0xFF9A8A));+ disc(CX + c * (PLAT + 4), CY + s * (PLAT + 4), 6, HEX(0x3A2A4A));+}+static void draw(void) {+ for (int y = gfx_y0 < TOP ? TOP : gfx_y0; y < gfx_y1; y++) gfx_fill(0, y, SCREEN_W, 1, (y / 6) & 1 ? HEX(0x4FA65A) : HEX(0x4A9E55));+ disc(CX + 2, CY + 5, PLAT + 4, HEX(0x3F7F44));+ disc(CX, CY, PLAT + 4, HEX(0xB98446));+ disc(CX, CY, PLAT, HEX(0xDDAA66));+ disc(CX, CY, STAND + 12, HEX(0xD09A55));+ disc(CX, CY, STAND - 12, HEX(0xDDAA66));+ for (int p = 0; p < np; p++) /* the shadows, under everything */+ if (!gone[p]) {+ float a = seat(p), h = height(p);+ disc_a(CX + fcos(a) * STAND + 2, CY + fsin(a) * STAND + 4, 9 - h / 6, 0, 80);+ }+ for (int p = 0; p < np; p++) /* the ones on the ground go under the bar */+ if (!gone[p] && height(p) <= 6) {+ float a = seat(p);+ blob(CX + fcos(a) * STAND, CY + fsin(a) * STAND, 10, p, a + PI);+ }+ bar(beam);+ if (two) bar(beam + PI);+ disc(CX, CY, 9, HEX(0x3A2A4A));+ disc(CX - 2, CY - 2, 3, HEX(0x6A5A7A));+ for (int p = 0; p < np; p++) {+ float a = seat(p), x = CX + fcos(a) * STAND, y = CY + fsin(a) * STAND;+ if (!gone[p] && height(p) > 6) blob(x, y - height(p), 10, p, a + PI);+ if (!gone[p]) tag_at(p, x, y - 10);+ int32_t k = rt - fall_t[p];+ if (gone[p] && k < 800) { /* thrown off */+ float d = STAND + k * 0.16f, b = a + w * 0.1f * k / 800;+ blob(CX + fcos(b) * d, CY + fsin(b) * d - k * 0.03f, 10 * (1 - k / 1100.0f), p, a + k * 0.02f);+ }+ }+}++static void icon(int x, int y) {+ box(x + 2, y + 1, 52, 32, 7, HEX(0x4FA65A));+ disc(x + 28, y + 17, 15, HEX(0xDDAA66));+ quad(x + 28, y + 17, -0.5f, 0, 16, 2.5f, HEX(0xE8433E));+ disc(x + 28, y + 17, 3, HEX(0x3A2A4A));+ disc(x + 16, y + 25, 4, col[0]), disc(x + 40, y + 10, 4, col[1]);+}++#ifdef HOST+static int bot(int p) {+ if (height(p) > 0 || fabs_(w) < 0.3f) return bot_tap(p, false);+ float a = seat(p), soon = 99;+ for (int k = 0; k < 1 + two; k++) {+ float d = wrap_pi(a - beam - k * PI), dist = w > 0 ? (d >= 0 ? d : d + 2 * PI) : (d <= 0 ? -d : 2 * PI - d);+ float t = dist / fabs_(w);+ soon = t < soon ? t : soon;+ }+ float when = 0.12f + (p * 37 % 10) * 0.012f + (rn(60) == 0 ? 0.3f : 0);+ return bot_tap(p, soon < when);+}+#endif++const game_t g_rope = {"Jump Rope", "Rope",+ "A bar sweeps round the platform. Tap your key to jump over it. Caught on the ground, you're "+ "thrown off. The last one standing takes the round.",+ 1, start, step, draw, icon BOT(bot)};diff --git a/games/fourplayer/src/g_skate.c b/games/fourplayer/src/g_skate.cnew file mode 100644index 00000000..3f3fae9e--- /dev/null+++ b/games/fourplayer/src/g_skate.c@@ -0,0 +1,92 @@+/* Skate Race: every tap is a kick. Tap as fast as you can and roll down your lane: the first over the line takes+ * the round. */+#include "fp.h"++#define LEN 1100.0f /* px to the line */+#define LANE 26+static float x[NMAX], v[NMAX], cam;+static int32_t kick_t[NMAX];++static int lanes_top(void) { return SCREEN_H - 6 - np * LANE; }+static void start(void) {+ for (int p = 0; p < np; p++) x[p] = 30, v[p] = 0, kick_t[p] = -10000;+ cam = 0;+}+static void step(void) {+ int first = -1;+ for (int p = 0; p < np; p++) {+ if (tap[p]) v[p] = v[p] + 24 > 300 ? 300 : v[p] + 24, kick_t[p] = rt;+ v[p] *= over() ? 0.97f : 0.986f;+ x[p] += v[p] * DT;+ if (x[p] >= LEN && (first < 0 || x[p] > x[first])) first = p;+ }+ if (first >= 0) win(first);+ float lead = 0;+ for (int p = 0; p < np; p++) lead = x[p] > lead ? x[p] : lead;+ float want = clampf(lead - 190, 0, LEN - 240);+ cam += (want - cam) * 0.15f;+}++static void draw(void) {+ int top = lanes_top();+ gfx_vgrad(0, TOP, SCREEN_W, top - TOP, 0x7EC8F2, 0xFCD9B4);+ for (int i = 0; i < 14; i++) { /* the town, far off: it moves slower */+ int w = 26 + (i * 37) % 22, h = 30 + (i * 53) % 48, bx = (int)(i * 52 - cam * 0.3f) % 728;+ if (bx < -60) bx += 728;+ gfx_fill(bx, top - 12 - h, w, h + 12, i & 1 ? HEX(0x8B7FB5) : HEX(0x9A8FC2));+ for (int k = 0; k < h / 12; k++) gfx_fill(bx + 5, top - 4 - h + k * 12, w - 10, 3, HEX(0xFFE7A8));+ }+ gfx_fill(0, top - 12, SCREEN_W, 12, HEX(0xBDB6A8)); /* the pavement */+ for (int m = 100; m <= (int)LEN; m += 100) {+ int sx = (int)(m - cam + 20);+ if (sx < -20 || sx > 340) continue;+ char s[6];+ num(s, (uint32_t)m);+ gfx_text_center(&np_font_small, sx, top - 2, m == (int)LEN ? "FINISH" : s, HEX(0x4A4458), 256);+ }+ for (int p = 0; p < np; p++) {+ int y = top + p * LANE;+ gfx_fill(0, y, SCREEN_W, LANE, p & 1 ? HEX(0x5C5966) : HEX(0x64616F));+ for (int k = 0; k < 12; k++) gfx_fill((int)(k * 32 - ((int)cam % 32)), y + LANE - 1, 16, 1, HEX(0x8C8898));+ }+ gfx_fill(0, SCREEN_H - 6, SCREEN_W, 6, HEX(0xBDB6A8));+ int fx = (int)(LEN - cam + 20); /* the line */+ for (int r = 0; r < np * LANE / 4; r++) gfx_fill(fx, top + r * 4, 4, 4, r & 1 ? HEX(0x1A1A1A) : WHITE), gfx_fill(fx + 4, top + r * 4, 4, 4, r & 1 ? WHITE : HEX(0x1A1A1A));+ for (int p = 0; p < np; p++) {+ float sx = x[p] - cam + 20, y = top + p * LANE + 12;+ bool behind = sx < 10;+ if (behind) sx = 10;+ if (rt - kick_t[p] < 160) line(sx - 3, y + 2, sx - 13, y + 8, 3, dark[p]); /* the kicking leg */+ box((int)sx - 10, (int)y + 6, 20, 3, 1, HEX(0x2A2433));+ disc(sx - 6, y + 10, 2, HEX(0xEDE7D6)), disc(sx + 6, y + 10, 2, HEX(0xEDE7D6));+ blob(sx, y - 1, 7, p, 0);+ if (behind) disc_a(sx, y - 1, 8, INK, 120);+ tag_at(p, sx, y - 8);+ }+}++static void icon(int x0, int y0) {+ box(x0 + 2, y0 + 1, 52, 32, 7, HEX(0x7EC8F2));+ gfx_fill(x0 + 2, y0 + 15, 52, 18, HEX(0x5C5966));+ for (int i = 0; i < 2; i++) {+ int y = y0 + 20 + i * 8;+ box(x0 + 14 + i * 10, y + 3, 12, 2, 1, HEX(0x2A2433));+ disc(x0 + 20 + i * 10, y, 4, col[i]);+ }+ gfx_fill(x0 + 44, y0 + 15, 3, 18, WHITE);+}++#ifdef HOST+static int bot(int p) {+ static int32_t next[NMAX], every[NMAX];+ if (bot_fresh(p)) next[p] = 0, every[p] = 100 + rn(40);+ if (rt < next[p]) return bot_tap(p, false);+ next[p] = rt + every[p] + rn(50);+ return bot_tap(p, true);+}+#endif++const game_t g_skate = {"Skate Race", "Skate",+ "Every tap of your key is a kick. Tap as fast as you can: the first over the line takes the "+ "round.",+ 0, start, step, draw, icon BOT(bot)};diff --git a/games/fourplayer/src/g_snakes.c b/games/fourplayer/src/g_snakes.cnew file mode 100644index 00000000..3b995647--- /dev/null+++ b/games/fourplayer/src/g_snakes.c@@ -0,0 +1,180 @@+/* Snakes: they never stop. A tap turns yours right (two taps can wait their turn). Stars make you longer. Hit a+ * wall or a snake, your own too, and you're out: the last snake left takes the round. They speed up as the+ * round goes on. */+#include "fp.h"++#define CS 8 /* a cell */+#define GW 40+#define GH 27+#define X0 0+#define Y0 (TOP + 1)+#define MAXLEN 256+#define NSTAR 3+static const int8_t DX[4] = {1, 0, -1, 0}, DY[4] = {0, 1, 0, -1}; /* right, down, left, up */+static uint8_t grid[GH][GW]; /* 0 free, else the snake's player + 1 */+static struct {+ uint16_t cell[MAXLEN]; /* a ring: the head at hd, the tail len - 1 before it */+ int hd, len, grow, dir, turns;+ int32_t dead_t;+} s[NMAX];+static int16_t star[NSTAR];+static int32_t next_t;++static int at(int x, int y) { return y * GW + x; }+static void place_star(int k) {+ for (int tries = 0; tries < 200; tries++) {+ int x = 2 + rn(GW - 4), y = 2 + rn(GH - 4), ok = !grid[y][x];+ for (int j = 0; j < NSTAR; j++) ok &= j == k || star[j] != at(x, y);+ for (int p = 0; p < np && ok; p++) {+ int h = s[p].cell[s[p].hd], d = (h % GW - x) * (h % GW - x) + (h / GW - y) * (h / GW - y);+ ok = d > 9;+ }+ if (ok) {+ star[k] = (int16_t)at(x, y);+ return;+ }+ }+ star[k] = -1;+}++static void start(void) {+ static const int8_t SPAWN[4][NMAX][3] = {+ {{9, 18, 3}, {30, 8, 1}},+ {{6, 18, 3}, {33, 8, 1}, {24, 22, 2}},+ {{9, 6, 0}, {33, 7, 1}, {7, 20, 3}, {31, 21, 2}},+ {{9, 5, 0}, {30, 5, 1}, {9, 22, 3}, {30, 22, 2}, {15, 13, 1}, {24, 14, 3}}};+ const int8_t(*sp)[3] = SPAWN[np == 2 ? 0 : np == 3 ? 1 : np == 4 ? 2 : 3];+ for (int y = 0; y < GH; y++)+ for (int x = 0; x < GW; x++) grid[y][x] = 0;+ for (int p = 0; p < np; p++) {+ s[p].dir = sp[p][2], s[p].len = 4, s[p].hd = 3, s[p].grow = 0, s[p].turns = 0, s[p].dead_t = -1;+ for (int i = 0; i < 4; i++) {+ int x = sp[p][0] - DX[s[p].dir] * (3 - i), y = sp[p][1] - DY[s[p].dir] * (3 - i);+ s[p].cell[i] = (uint16_t)at(x, y), grid[y][x] = (uint8_t)(p + 1);+ }+ }+ for (int k = 0; k < NSTAR; k++) star[k] = -1;+ for (int k = 0; k < NSTAR; k++) place_star(k);+ next_t = 0;+}++static int tail_of(int p) { return (s[p].hd - s[p].len + 1 + MAXLEN) % MAXLEN; }+static void clear_snake(int p) {+ for (int i = 0; i < s[p].len; i++) {+ int c = s[p].cell[(tail_of(p) + i) % MAXLEN];+ if (grid[c / GW][c % GW] == p + 1) grid[c / GW][c % GW] = 0;+ }+ s[p].len = 0;+}++static void move(void) {+ int nx[NMAX], ny[NMAX];+ bool dies[NMAX] = {0};+ for (int p = 0; p < np; p++) {+ if (gone[p]) continue;+ if (s[p].turns) s[p].dir = (s[p].dir + 1) & 3, s[p].turns--;+ int h = s[p].cell[s[p].hd];+ nx[p] = h % GW + DX[s[p].dir], ny[p] = h / GW + DY[s[p].dir];+ bool grows = s[p].grow > 0 && s[p].len < MAXLEN - 1;+ if (s[p].grow) s[p].grow--;+ if (grows) s[p].len++;+ else { /* the tail moves on first */+ int t = s[p].cell[tail_of(p)];+ if (grid[t / GW][t % GW] == p + 1) grid[t / GW][t % GW] = 0;+ }+ }+ for (int p = 0; p < np; p++) {+ if (gone[p]) continue;+ dies[p] = nx[p] < 0 || ny[p] < 0 || nx[p] >= GW || ny[p] >= GH || grid[ny[p]][nx[p]];+ for (int q = 0; q < np; q++)+ if (q != p && !gone[q] && nx[q] == nx[p] && ny[q] == ny[p]) dies[p] = true; /* head to head */+ }+ for (int p = 0; p < np; p++) {+ if (gone[p]) continue;+ if (dies[p]) {+ out(p), s[p].dead_t = rt;+ s[p].len--; /* (its tail had moved on, or it hadn't grown a head) */+ continue;+ }+ s[p].hd = (s[p].hd + 1) % MAXLEN, s[p].cell[s[p].hd] = (uint16_t)at(nx[p], ny[p]), grid[ny[p]][nx[p]] = (uint8_t)(p + 1);+ for (int k = 0; k < NSTAR; k++)+ if (star[k] == at(nx[p], ny[p])) s[p].grow += 3, place_star(k);+ }+}++static void step(void) {+ for (int p = 0; p < np; p++) {+ if (tap[p] && s[p].turns < 2) s[p].turns++;+ if (s[p].dead_t >= 0 && s[p].len && rt - s[p].dead_t > 900) clear_snake(p);+ }+ if (rt >= next_t) {+ int period = 130 - (rt < 50000 ? rt / 1000 : 50);+ next_t = rt + period;+ if (!over()) move();+ }+}++static void draw(void) {+ int r0 = clampi((gfx_y0 - Y0) / CS, 0, GH), r1 = clampi((gfx_y1 - Y0) / CS + 1, 0, GH);+ gfx_fill(0, TOP, SCREEN_W, SCREEN_H - TOP, HEX(0x15382A));+ for (int r = r0; r < r1; r++)+ for (int c = (r & 1); c < GW; c += 2) gfx_fill(X0 + c * CS, Y0 + r * CS, CS, CS, HEX(0x184031));+ for (int k = 0; k < NSTAR; k++) {+ if (star[k] < 0) continue;+ float x = X0 + star[k] % GW * CS + 4, y = Y0 + star[k] / GW * CS + 4, a = rt * 0.002f;+ for (int i = 0; i < 5; i++) {+ float b = a + i * 2 * PI / 5, p[6] = {x + fcos(b) * 7.5f, y + fsin(b) * 7.5f, x + fcos(b + 2.2f) * 3, y + fsin(b + 2.2f) * 3,+ x + fcos(b - 2.2f) * 3, y + fsin(b - 2.2f) * 3};+ poly(p, 3, GOLD);+ }+ disc(x, y, 3, HEX(0xFFE89A));+ }+ for (int r = r0; r < r1; r++)+ for (int c = 0; c < GW; c++) {+ int v = grid[r][c];+ if (!v) continue;+ int p = v - 1, x = X0 + c * CS, y = Y0 + r * CS;+ if (s[p].dead_t >= 0 && ((rt - s[p].dead_t) / 120 & 1)) continue; /* blinks out */+ bool rgt = c + 1 < GW && grid[r][c + 1] == v, dn = r + 1 < GH && grid[r + 1][c] == v;+ gfx_fill(x + 1, y + 1, 6 + 2 * rgt, 6 + 2 * dn, col[p]);+ gfx_fill(x + 2, y + 2, 3, 3, lite[p]);+ }+ for (int p = 0; p < np; p++) {+ if (!s[p].len) continue;+ int h = s[p].cell[s[p].hd];+ float x = X0 + h % GW * CS + 4, y = Y0 + h / GW * CS + 4, a = s[p].dir * PI / 2;+ disc(x, y, 6, col[p]);+ eyes(x, y, 6, a);+ tag_at(p, x, y - 5);+ }+}++static void icon(int x, int y) {+ box(x + 2, y + 1, 52, 32, 7, HEX(0x15382A));+ gfx_fill(x + 8, y + 8, 22, 5, col[0]), gfx_fill(x + 25, y + 8, 5, 14, col[0]);+ gfx_fill(x + 26, y + 25, 22, 5, col[1]), gfx_fill(x + 43, y + 14, 5, 16, col[1]);+ disc(x + 45, y + 13, 4, col[1]), disc(x + 28, y + 23, 4, col[0]);+ disc(x + 12, y + 25, 3, GOLD);+}++#ifdef HOST+static bool free_cell(int x, int y) { return x >= 0 && y >= 0 && x < GW && y < GH && !grid[y][x]; }+static int bot(int p) {+ if (s[p].turns) return bot_tap(p, false);+ int h = s[p].cell[s[p].hd], x = h % GW, y = h / GW, d = s[p].dir, r = (d + 1) & 3;+ bool ahead = free_cell(x + DX[d], y + DY[d]) && free_cell(x + 2 * DX[d], y + 2 * DY[d]);+ bool right = free_cell(x + DX[r], y + DY[r]) && free_cell(x + 2 * DX[r], y + 2 * DY[r]);+ bool want = (!ahead && right) || (right && rn(14) == 0);+ for (int k = 0; k < NSTAR && !want; k++) /* a star on its right */+ if (star[k] >= 0 && right) {+ int sx = star[k] % GW - x, sy = star[k] / GW - y;+ want = (DX[r] && sx * DX[r] > 0 && sy == 0) || (DY[r] && sy * DY[r] > 0 && sx == 0);+ }+ return bot_tap(p, want);+}+#endif++const game_t g_snakes = {"Snakes", "Snakes",+ "Your snake never stops. Tap your key to turn right. Stars make you longer. Hit a wall or a "+ "snake, yours too, and you're out: the last snake left takes the round.",+ 1, start, step, draw, icon BOT(bot)};diff --git a/games/fourplayer/src/g_soccer.c b/games/fourplayer/src/g_soccer.cnew file mode 100644index 00000000..2fc18613--- /dev/null+++ b/games/fourplayer/src/g_soccer.c@@ -0,0 +1,199 @@+/* Soccer: each player's arrow spins, a tap dashes where it points and turns the spin the other way. Two sides+ * play on a pitch with a goal at each end: the first goal takes the round. Three or four players get a goal+ * each, in the corners: let one in and you're out, the last one left takes the round. */+#include "fp.h"++#define PR 9.0f /* a player */+#define BR 6.0f /* the ball */+#define SPIN 3.6f+#define DASH 200.0f+#define FAST 300.0f+#define COOL 280+#define CX 160.0f+#define CY 131.0f+typedef struct {+ float x0, y0, x1, y1;+ int8_t goal; /* the side whose goal it is, or -1: a wall */+} seg_t;+static seg_t seg[12];+static int nseg;+static bool corners; /* the corner goals of 3 and 4 players */+static struct {+ body_t b;+ float a, spin;+ int32_t dash_t;+} pl[NMAX];+static body_t ball;+static int32_t goal_t, kick_t; /* the last goal, the next kick-off */+static int last_goal; /* whose */++static void add(float x0, float y0, float x1, float y1, int goal) {+ seg[nseg++] = (seg_t){x0, y0, x1, y1, (int8_t)goal};+}+static void kick_off(void) { ball = (body_t){CX, CY, 0, 0}; }++static void start(void) {+ nseg = 0, corners = !teams && np > 2;+ if (corners) { /* an octagon: its four corners are goals (bottom right a wall for 3 players) */+ float L = 8, R = 312, T = 26, B = 236, c = 46;+ add(L + c, T, R - c, T, -1), add(R - c, T, R, T + c, 1), add(R, T + c, R, B - c, -1), add(R, B - c, R - c, B, np > 3 ? 3 : -1);+ add(R - c, B, L + c, B, -1), add(L + c, B, L, B - c, 2), add(L, B - c, L, T + c, -1), add(L, T + c, L + c, T, 0);+ } else { /* a pitch: the left goal the left side's (player 1, or the red team) */+ float L = 14, R = 306, T = 26, B = 236;+ add(L, T, R, T, -1), add(R, T, R, CY - 30, -1), add(R, CY - 30, R, CY + 30, 1), add(R, CY + 30, R, B, -1);+ add(R, B, L, B, -1), add(L, B, L, CY + 30, -1), add(L, CY + 30, L, CY - 30, 0), add(L, CY - 30, L, T, -1);+ }+ static const float TEAM[3][3][2] = {{{60, 131}}, {{85, 95}, {85, 167}}, {{95, 70}, {55, 131}, {95, 192}}};+ for (int p = 0; p < np; p++) {+ float x, y;+ if (corners) x = CX + fcos(seat(p)) * 100, y = CY + fsin(seat(p)) * 64;+ else {+ const float *t = TEAM[np / 2 - 1][p >> 1];+ x = p & 1 ? 320 - t[0] : t[0], y = t[1];+ }+ pl[p].b = (body_t){x, y, 0, 0}, pl[p].a = fatan2(CY - y, CX - x), pl[p].spin = 1, pl[p].dash_t = -10000;+ }+ kick_off();+ goal_t = -10000, kick_t = -1, last_goal = -1;+}++/* a disc against a segment (a wall, or a goal for the players): pushed out, bounced (e) */+static void against(body_t *b, const seg_t *s, float r, float e) {+ float dx = s->x1 - s->x0, dy = s->y1 - s->y0, l2 = dx * dx + dy * dy;+ float t = clampf(((b->x - s->x0) * dx + (b->y - s->y0) * dy) / l2, 0, 1);+ float cx = s->x0 + dx * t, cy = s->y0 + dy * t, ox = b->x - cx, oy = b->y - cy, d2 = ox * ox + oy * oy;+ if (d2 >= r * r) return;+ float d = fsqrt(d2), nx, ny;+ if (d > 0.001f) nx = ox / d, ny = oy / d;+ else nx = dy / fsqrt(l2), ny = -dx / fsqrt(l2);+ b->x = cx + nx * r, b->y = cy + ny * r;+ float vn = b->vx * nx + b->vy * ny;+ if (vn < 0) b->vx -= (1 + e) * vn * nx, b->vy -= (1 + e) * vn * ny;+}+/* how far past a goal's line the ball is (the walls go clockwise round the field: outside is on their left) */+static float past(const seg_t *s, float x, float y) {+ float dx = s->x1 - s->x0, dy = s->y1 - s->y0, l = fsqrt(dx * dx + dy * dy);+ float t = ((x - s->x0) * dx + (y - s->y0) * dy) / (l * l);+ if (t < 0 || t > 1) return -1;+ return ((x - s->x0) * dy - (y - s->y0) * dx) / l;+}+static bool open_goal(const seg_t *s) { return s->goal >= 0 && (teams || !gone[s->goal]); }++static void step(void) {+ for (int p = 0; p < np; p++) {+ if (gone[p]) continue;+ body_t *b = &pl[p].b;+ pl[p].a = wrap_pi(pl[p].a + pl[p].spin * SPIN * DT);+ if (tap[p] && rt - pl[p].dash_t >= COOL) {+ b->vx += fcos(pl[p].a) * DASH, b->vy += fsin(pl[p].a) * DASH;+ float v = fsqrt(b->vx * b->vx + b->vy * b->vy);+ if (v > FAST) b->vx *= FAST / v, b->vy *= FAST / v;+ pl[p].spin = -pl[p].spin, pl[p].dash_t = rt;+ }+ b->vx *= 0.975f, b->vy *= 0.975f;+ b->x += b->vx * DT, b->y += b->vy * DT;+ }+ bool in_play = kick_t < 0;+ if (!in_play && rt >= kick_t) kick_off(), kick_t = -1, in_play = true;+ ball.vx *= in_play ? 0.992f : 0.8f, ball.vy *= in_play ? 0.992f : 0.8f; /* (in the net: it stops) */+ float v = fsqrt(ball.vx * ball.vx + ball.vy * ball.vy);+ if (v > 420) ball.vx *= 420 / v, ball.vy *= 420 / v;+ ball.x += ball.vx * DT, ball.y += ball.vy * DT;+ for (int p = 0; p < np; p++) {+ if (gone[p]) continue;+ for (int q = p + 1; q < np; q++)+ if (!gone[q]) collide(&pl[p].b, &pl[q].b, 2 * PR, 1, 1, 0.7f);+ if (in_play) collide(&pl[p].b, &ball, PR + BR, 3, 1, 0.9f);+ }+ for (int i = 0; i < nseg; i++) {+ for (int p = 0; p < np; p++)+ if (!gone[p]) against(&pl[p].b, &seg[i], PR, 0.3f);+ if (!open_goal(&seg[i])) against(&ball, &seg[i], BR, 0.8f);+ else { /* the posts */+ body_t *b = &ball;+ for (int k = 0; k < 2; k++) {+ seg_t post = {k ? seg[i].x1 : seg[i].x0, k ? seg[i].y1 : seg[i].y0, k ? seg[i].x1 : seg[i].x0, (k ? seg[i].y1 : seg[i].y0) + 0.01f, -1};+ against(b, &post, BR + 1, 0.8f);+ }+ if (in_play && past(&seg[i], ball.x, ball.y) > BR) { /* a goal */+ int s = seg[i].goal;+ goal_t = rt, last_goal = s, kick_t = rt + 1100;+ if (teams) win(1 - s);+ else out(s);+ in_play = false;+ }+ }+ }+ if (rt > 90000) tie();+}++static void lines(void) {+ color_t w = HEX(0xEAF5EA);+ for (int i = 0; i < nseg; i++)+ if (!open_goal(&seg[i])) line(seg[i].x0, seg[i].y0, seg[i].x1, seg[i].y1, 3, w);+ disc(CX, CY, 30, w), disc(CX, CY, 28, HEX(0x40A050)), disc(CX, CY, 3, w);+ if (!corners) gfx_fill(159, 26, 2, 210, w);+}+static void net(const seg_t *s) { /* the goal behind its line, in its side's colour */+ float dx = s->x1 - s->x0, dy = s->y1 - s->y0, l = fsqrt(dx * dx + dy * dy), nx = dy / l * 13, ny = -dx / l * 13;+ float p[8] = {s->x0, s->y0, s->x1, s->y1, s->x1 + nx * 0.8f, s->y1 + ny * 0.8f, s->x0 + nx * 0.8f, s->y0 + ny * 0.8f};+ poly(p, 4, dark[s->goal]);+ for (int k = 1; k < 6; k++) {+ float t = k / 6.0f;+ line(s->x0 + dx * t, s->y0 + dy * t, s->x0 + dx * t + nx * 0.8f, s->y0 + dy * t + ny * 0.8f, 1, lite[s->goal]);+ }+ for (int k = 1; k < 3; k++) line(s->x0 + nx * 0.27f * k, s->y0 + ny * 0.27f * k, s->x1 + nx * 0.27f * k, s->y1 + ny * 0.27f * k, 1, lite[s->goal]);+ disc(s->x0, s->y0, 3, col[s->goal]), disc(s->x1, s->y1, 3, col[s->goal]);+}+static void draw(void) {+ gfx_fill(0, TOP, SCREEN_W, SCREEN_H - TOP, HEX(0x255E33));+ if (corners) {+ float P[16] = {54, 26, 266, 26, 312, 72, 312, 190, 266, 236, 54, 236, 8, 190, 8, 72};+ poly(P, 8, HEX(0x40A050));+ } else {+ gfx_fill(14, 26, 292, 210, HEX(0x40A050));+ }+ for (int x = 14; x < 306; x += 40) gfx_fill_alpha(x, 26, 20, 210, HEX(0x4DB25D), 120);+ if (corners) { /* (the stripes trimmed to the octagon) */+ for (int k = 0; k < 4; k++) {+ float cx = k & 1 ? 312 : 8, cy = k & 2 ? 236 : 26, ex = k & 1 ? -46 : 46, ey = k & 2 ? -46 : 46;+ float tr[6] = {cx, cy, cx + ex, cy, cx, cy + ey};+ poly(tr, 3, HEX(0x255E33));+ }+ }+ lines();+ for (int i = 0; i < nseg; i++)+ if (open_goal(&seg[i])) net(&seg[i]);+ disc_a(ball.x + 1.5f, ball.y + 2.5f, BR, 0, 80);+ disc(ball.x, ball.y, BR, WHITE);+ disc(ball.x - 1, ball.y - 1, 2, HEX(0x2A2A2A)), disc(ball.x + 2.5f, ball.y + 2, 1.5f, HEX(0x2A2A2A));+ for (int p = 0; p < np; p++) {+ if (gone[p]) continue;+ float x = pl[p].b.x, y = pl[p].b.y, a = pl[p].a, c = fcos(a), s = fsin(a), d = PR + 3;+ float t[6] = {x + c * (d + 7), y + s * (d + 7), x + c * d - s * 4.5f, y + s * d + c * 4.5f, x + c * d + s * 4.5f, y + s * d - c * 4.5f};+ poly(t, 3, WHITE);+ blob(x, y, PR, p, a);+ tag_at(p, x, y - PR);+ }+ if (rt - goal_t < 1100 && last_goal >= 0) say(&np_font_title, 160, 80, "GOAL!", WHITE);+}++static void icon(int x, int y) {+ box(x + 2, y + 1, 52, 32, 7, HEX(0x40A050));+ gfx_fill(x + 27, y + 2, 2, 30, HEX(0xEAF5EA));+ gfx_fill(x + 2, y + 11, 6, 12, dark[0]), gfx_fill(x + 48, y + 11, 6, 12, dark[1]);+ disc(x + 17, y + 17, 5, col[0]), disc(x + 39, y + 17, 5, col[1]);+ disc(x + 28, y + 9, 3, WHITE);+}++#ifdef HOST+static int bot(int p) {+ float want = fatan2(ball.y - pl[p].b.y, ball.x - pl[p].b.x);+ return bot_tap(p, fabs_(wrap_pi(pl[p].a - want)) < 0.25f && rt - pl[p].dash_t >= COOL && rn(2) == 0);+}+#endif++const game_t g_soccer = {"Soccer", "Soccer",+ "Your arrow spins. Tap your key to dash where it points and hit the ball. Two sides: the first "+ "goal takes the round. With three or four, let one in and you're out.",+ 1, start, step, draw, icon BOT(bot)};diff --git a/games/fourplayer/src/g_sumo.c b/games/fourplayer/src/g_sumo.cnew file mode 100644index 00000000..c6bb5ef4--- /dev/null+++ b/games/fourplayer/src/g_sumo.c@@ -0,0 +1,126 @@+/* Sumo: wrestlers on a ring of ice in the sea. Each one's arrow spins; a tap dashes where it points and turns+ * the spin the other way. Off the ice is out, and the last one on it takes the round. After 20 seconds the+ * ice melts, a little more every second. */+#include "fp.h"++#define CX 160.0f+#define CY 131.0f+#define RING 100.0f /* the ice */+#define R 11.0f /* a wrestler */+#define SPIN 3.4f /* the arrow, radians a second */+#define DASH 215.0f /* px/s a dash adds */+#define FAST 330.0f+#define COOL 300 /* ms between dashes */+static struct {+ body_t b;+ float a, spin;+ int32_t dash_t, fall_t;+} w[NMAX];+static float ring;++static void start(void) {+ ring = RING;+ for (int p = 0; p < np; p++) {+ float s = seat(p);+ w[p].b = (body_t){CX + fcos(s) * 62, CY + fsin(s) * 62, 0, 0};+ w[p].a = s + PI, w[p].spin = 1, w[p].dash_t = -10000, w[p].fall_t = -1;+ }+}++static void step(void) {+ if (rt > 20000 && ring > 0) ring -= 3.0f * DT; /* the ice melts */+ for (int p = 0; p < np; p++) {+ body_t *b = &w[p].b;+ if (w[p].fall_t >= 0) { /* sinking */+ b->x += b->vx * DT * 0.4f, b->y += b->vy * DT * 0.4f;+ continue;+ }+ w[p].a = wrap_pi(w[p].a + w[p].spin * SPIN * DT);+ if (tap[p] && rt - w[p].dash_t >= COOL) {+ b->vx += fcos(w[p].a) * DASH, b->vy += fsin(w[p].a) * DASH;+ float v = fsqrt(b->vx * b->vx + b->vy * b->vy);+ if (v > FAST) b->vx *= FAST / v, b->vy *= FAST / v;+ w[p].spin = -w[p].spin, w[p].dash_t = rt;+ }+ b->vx *= 0.988f, b->vy *= 0.988f; /* ice: barely slows */+ b->x += b->vx * DT, b->y += b->vy * DT;+ }+ for (int p = 0; p < np; p++)+ for (int q = p + 1; q < np; q++)+ if (w[p].fall_t < 0 && w[q].fall_t < 0) collide(&w[p].b, &w[q].b, 2 * R, 1, 1, 0.95f);+ for (int p = 0; p < np; p++) {+ float dx = w[p].b.x - CX, dy = w[p].b.y - CY;+ if (w[p].fall_t < 0 && dx * dx + dy * dy > ring * ring) w[p].fall_t = rt, out(p);+ }+}++static void sea(void) {+ for (int y = gfx_y0 < TOP ? TOP : gfx_y0; y < gfx_y1; y++) {+ int k = ((y + rt / 90) / 7) & 3;+ gfx_fill(0, y, SCREEN_W, 1, k == 0 ? HEX(0x1F5FA0) : HEX(0x1B5291));+ }+}+static void wrestler(int p, float x, float y, float r) {+ blob(x, y, r, p, w[p].a);+ disc(x, y - r * 0.92f, r * 0.3f, HEX(0x221B33)); /* the topknot */+}+static void draw(void) {+ sea();+ if (ring > 1) {+ disc(CX, CY + 3, ring + 4, HEX(0x174780)); /* the ice's side, under water */+ disc(CX, CY, ring + 3, HEX(0x9FD3EE));+ disc(CX, CY, ring, HEX(0xE4F5FF));+ if (ring > 30) {+ disc(CX, CY, ring - 14, HEX(0xC4E4F6));+ disc(CX, CY, ring - 16, HEX(0xE4F5FF));+ }+ gfx_fill(CX - 18, CY - 8, 12, 3, HEX(0xC4E4F6));+ gfx_fill(CX + 6, CY + 5, 12, 3, HEX(0xC4E4F6));+ }+ for (int p = 0; p < np; p++) {+ float x = w[p].b.x, y = w[p].b.y;+ if (w[p].fall_t >= 0) {+ int32_t k = rt - w[p].fall_t;+ if (k < 700) {+ disc_a(x, y, R + k / 25, WHITE, 180 - k / 4);+ wrestler(p, x, y, R * (1 - k / 800.0f));+ }+ continue;+ }+ if (rt - w[p].dash_t < 160) disc_a(x - w[p].b.vx * 0.05f, y - w[p].b.vy * 0.05f, R * 0.8f, WHITE, 110);+ float a = w[p].a, c = fcos(a), s = fsin(a), d = R + 4;+ float t[6] = {x + c * (d + 8), y + s * (d + 8), x + c * d - s * 5, y + s * d + c * 5, x + c * d + s * 5, y + s * d - c * 5};+ poly(t, 3, dark[p]);+ wrestler(p, x, y, R);+ tag_at(p, x, y - R);+ }+}++static void icon(int x, int y) {+ box(x + 2, y + 1, 52, 32, 7, HEX(0x1F5FA0));+ disc(x + 28, y + 17, 15, HEX(0x9FD3EE));+ disc(x + 28, y + 17, 13, HEX(0xE4F5FF));+ disc(x + 21, y + 17, 6, col[0]);+ disc(x + 35, y + 17, 6, col[1]);+ disc(x + 21, y + 11, 2, HEX(0x221B33));+ disc(x + 35, y + 11, 2, HEX(0x221B33));+}++#ifdef HOST+static int bot(int p) {+ float x = w[p].b.x, y = w[p].b.y, want = 99, best = 1e9f;+ if ((x - CX) * (x - CX) + (y - CY) * (y - CY) > (ring - 30) * (ring - 30)) want = fatan2(CY - y, CX - x);+ else+ for (int q = 0; q < np; q++) {+ float d = (w[q].b.x - x) * (w[q].b.x - x) + (w[q].b.y - y) * (w[q].b.y - y);+ if (q != p && !gone[q] && side(q) != side(p) && d < best) best = d, want = fatan2(w[q].b.y - y, w[q].b.x - x);+ }+ bool aim = want < 50 && fabs_(wrap_pi(w[p].a - want)) < 0.3f && rt - w[p].dash_t >= COOL && rn(3) == 0;+ return bot_tap(p, aim);+}+#endif++const game_t g_sumo = {"Sumo", "Sumo",+ "Your arrow spins. Tap your key to dash where it points: the spin turns the other way. Push the "+ "others into the sea. The last one on the ice takes the round.",+ 1, start, step, draw, icon BOT(bot)};diff --git a/games/fourplayer/src/g_tanks.c b/games/fourplayer/src/g_tanks.cnew file mode 100644index 00000000..d43230ca--- /dev/null+++ b/games/fourplayer/src/g_tanks.c@@ -0,0 +1,200 @@+/* Tanks: each tank turns on the spot. Holding the key drives it straight; letting go fires. Shells bounce off+ * the walls three times, and one hit is enough. The last tank left takes the round. In teams, shells pass+ * through your own team. */+#include "fp.h"++#define TR 8.0f /* a tank */+#define TURN 2.5f /* radians a second, clockwise */+#define DRIVE 62.0f /* px/s */+#define SHOT 150.0f+#define NSH 18+#define WALL 5+typedef struct {+ int16_t x, y, w, h;+} rect_t;+static const rect_t LAYOUT[3][7] = {+ {{152, 86, 16, 90}, {115, 123, 90, 16}, {70, 72, 26, 14}, {224, 72, 26, 14}, {70, 190, 26, 14}, {224, 190, 26, 14}},+ {{100, 62, 14, 48}, {206, 62, 14, 48}, {100, 154, 14, 48}, {206, 154, 14, 48}, {145, 116, 30, 30}},+ {{80, 70, 18, 18}, {151, 70, 18, 18}, {222, 70, 18, 18}, {80, 174, 18, 18}, {151, 174, 18, 18}, {222, 174, 18, 18},+ {120, 122, 80, 18}},+};+static const rect_t *map;+static int nmap;+static struct {+ body_t b;+ float a;+ int32_t shot_t, dead_t;+} t[NMAX];+static struct {+ float x, y, vx, vy;+ int8_t own, hits;+ int32_t born;+} sh[NSH];++static void start(void) {+ int k = rn(3);+ map = LAYOUT[k], nmap = 0;+ while (nmap < 7 && map[nmap].w) nmap++;+ for (int p = 0; p < np; p++) {+ float s = seat(p);+ t[p].b = (body_t){160 + fcos(s) * 122, 131 + fsin(s) * 82, 0, 0};+ if (np == 3 && p == 2) t[p].b.y = 214;+ t[p].a = fatan2(131 - t[p].b.y, 160 - t[p].b.x), t[p].shot_t = -10000, t[p].dead_t = -1;+ }+ for (int i = 0; i < NSH; i++) sh[i].own = -1;+}++static bool solid(float x, float y) { /* a wall at (x, y) */+ if (x < WALL || x >= SCREEN_W - WALL || y < TOP + WALL || y >= SCREEN_H - WALL) return true;+ for (int i = 0; i < nmap; i++)+ if (x >= map[i].x && x < map[i].x + map[i].w && y >= map[i].y && y < map[i].y + map[i].h) return true;+ return false;+}+/* a tank out of the walls */+static void push_out(body_t *b) {+ b->x = clampf(b->x, WALL + TR, SCREEN_W - WALL - TR), b->y = clampf(b->y, TOP + WALL + TR, SCREEN_H - WALL - TR);+ for (int i = 0; i < nmap; i++) {+ float cx = clampf(b->x, map[i].x, map[i].x + map[i].w), cy = clampf(b->y, map[i].y, map[i].y + map[i].h);+ float dx = b->x - cx, dy = b->y - cy, d2 = dx * dx + dy * dy;+ if (d2 >= TR * TR) continue;+ if (d2 < 0.0001f) { /* (its middle inside: out the nearest side) */+ float l = b->x - map[i].x, r = map[i].x + map[i].w - b->x, u = b->y - map[i].y, d = map[i].y + map[i].h - b->y;+ float m = l < r ? l : r, n = u < d ? u : d;+ if (m < n) b->x = l < r ? map[i].x - TR : map[i].x + map[i].w + TR;+ else b->y = u < d ? map[i].y - TR : map[i].y + map[i].h + TR;+ continue;+ }+ float d = fsqrt(d2);+ b->x = cx + dx / d * TR, b->y = cy + dy / d * TR;+ }+}++static void fire(int p) {+ int live = 0, free_ = -1;+ for (int i = 0; i < NSH; i++)+ if (sh[i].own == p) live++;+ else if (sh[i].own < 0 && free_ < 0) free_ = i;+ bool late = rt > 30000; /* (after 30 seconds: shells come faster) */+ if (live >= (late ? 4 : 3) || free_ < 0 || rt - t[p].shot_t < (late ? 220 : 400)) return;+ float c = fcos(t[p].a), s = fsin(t[p].a), x = t[p].b.x + c * 12, y = t[p].b.y + s * 12;+ if (solid(x, y)) return; /* (the barrel against a wall) */+ sh[free_].x = x, sh[free_].y = y, sh[free_].vx = c * SHOT, sh[free_].vy = s * SHOT;+ sh[free_].own = (int8_t)p, sh[free_].hits = 0, sh[free_].born = rt;+ t[p].shot_t = rt;+}++static void step(void) {+ for (int p = 0; p < np; p++) {+ if (gone[p]) continue;+ if (hold[p]) t[p].b.x += fcos(t[p].a) * DRIVE * DT, t[p].b.y += fsin(t[p].a) * DRIVE * DT;+ else t[p].a = wrap_pi(t[p].a + TURN * DT);+ if (rel[p]) fire(p);+ }+ for (int p = 0; p < np; p++)+ for (int q = p + 1; q < np; q++)+ if (!gone[p] && !gone[q]) {+ t[p].b.vx = t[p].b.vy = t[q].b.vx = t[q].b.vy = 0;+ collide(&t[p].b, &t[q].b, 2 * TR, 1, 1, 0);+ }+ for (int p = 0; p < np; p++)+ if (!gone[p]) push_out(&t[p].b);+ for (int i = 0; i < NSH; i++) {+ if (sh[i].own < 0) continue;+ float ox = sh[i].x, oy = sh[i].y;+ sh[i].x += sh[i].vx * DT, sh[i].y += sh[i].vy * DT;+ if (solid(sh[i].x, sh[i].y)) { /* a bounce: off the side it came through */+ bool fx = !solid(ox, sh[i].y), fy = !solid(sh[i].x, oy);+ if (fx || !fy) sh[i].vx = -sh[i].vx;+ if (fy || !fx) sh[i].vy = -sh[i].vy;+ sh[i].x = ox, sh[i].y = oy;+ if (++sh[i].hits > 3) sh[i].own = -1;+ }+ if (rt - sh[i].born > 6000) sh[i].own = -1;+ for (int p = 0; p < np && sh[i].own >= 0; p++) {+ if (gone[p] || (teams && side(p) == side(sh[i].own)) || (p == sh[i].own && rt - sh[i].born < 200)) continue;+ float dx = sh[i].x - t[p].b.x, dy = sh[i].y - t[p].b.y;+ if (dx * dx + dy * dy < (TR + 1.5f) * (TR + 1.5f)) out(p), t[p].dead_t = rt, sh[i].own = -1;+ }+ }+ if (rt > 75000) tie();+}++static void tank(int p) {+ float x = t[p].b.x, y = t[p].b.y, a = t[p].a, c = fcos(a), s = fsin(a);+ quad(x + 1.5f, y + 2.5f, a, -9, 9, 8, HEX(0xB7AE95)); /* shadow */+ for (int k = -1; k <= 1; k += 2) quad(x - s * k * 6, y + c * k * 6, a, -9, 9, 2.6f, HEX(0x26232E));+ quad(x, y, a, -7.5f, 7.5f, 5, col[p]);+ quad(x, y, a, 2, 13, 1.7f, dark[p]);+ disc(x, y, 4, dark[p]);+ disc(x - 1, y - 1, 2, lite[p]);+}+static void draw(void) {+ for (int y = gfx_y0 < TOP ? TOP : gfx_y0; y < gfx_y1; y++) gfx_fill(0, y, SCREEN_W, 1, (y - TOP) % 20 ? HEX(0xDCD5C1) : HEX(0xCFC7B1));+ for (int x = 20; x < SCREEN_W; x += 20) gfx_fill(x, TOP, 1, SCREEN_H - TOP, HEX(0xCFC7B1));+ color_t wc = HEX(0x3B3F4E), hc = HEX(0x5A5F72);+ gfx_fill(0, TOP, SCREEN_W, WALL, wc), gfx_fill(0, SCREEN_H - WALL, SCREEN_W, WALL, wc);+ gfx_fill(0, TOP, WALL, SCREEN_H - TOP, wc), gfx_fill(SCREEN_W - WALL, TOP, WALL, SCREEN_H - TOP, wc);+ for (int i = 0; i < nmap; i++) {+ gfx_fill(map[i].x + 2, map[i].y + 3, map[i].w, map[i].h, HEX(0xB7AE95));+ gfx_fill(map[i].x, map[i].y, map[i].w, map[i].h, wc);+ gfx_fill(map[i].x, map[i].y, map[i].w, 2, hc);+ }+ for (int p = 0; p < np; p++) {+ if (t[p].dead_t < 0) continue;+ int32_t k = rt - t[p].dead_t;+ disc_a(t[p].b.x, t[p].b.y, 11, HEX(0x4A4438), 160);+ if (k < 600) disc_a(t[p].b.x, t[p].b.y, 6 + k / 30.0f, HEX(0xFF8A2A), 256 - k * 256 / 600),+ disc_a(t[p].b.x, t[p].b.y, 3 + k / 50.0f, HEX(0xFFE066), 256 - k * 256 / 600);+ }+ for (int p = 0; p < np; p++)+ if (t[p].dead_t < 0) tank(p), tag_at(p, t[p].b.x, t[p].b.y - 9);+ for (int i = 0; i < NSH; i++)+ if (sh[i].own >= 0) disc(sh[i].x, sh[i].y, 2.5f, HEX(0x1E1F24)), disc_a(sh[i].x - 1, sh[i].y - 1, 1, WHITE, 160);+}++static void icon(int x, int y) {+ box(x + 2, y + 1, 52, 32, 7, HEX(0xDCD5C1));+ gfx_fill(x + 25, y + 6, 6, 22, HEX(0x3B3F4E));+ for (int i = 0; i < 2; i++) {+ int cx = x + 13 + i * 30, cy = y + 17;+ gfx_fill(cx - 7, cy - 6, 14, 3, HEX(0x26232E)), gfx_fill(cx - 7, cy + 3, 14, 3, HEX(0x26232E));+ box(cx - 6, cy - 4, 12, 8, 2, col[i]);+ gfx_fill(i ? cx - 11 : cx, cy - 1, 11, 3, dark[i]);+ }+}++#ifdef HOST+static bool clear_shot(float x, float y, float tx, float ty) {+ for (int i = 1; i < 24; i++)+ if (solid(x + (tx - x) * i / 24, y + (ty - y) * i / 24)) return false;+ return true;+}+static int bot(int p) {+ static int8_t driving[NMAX];+ static int32_t until[NMAX], seen[NMAX];+ if (bot_fresh(p)) driving[p] = 0, seen[p] = rt;+ float x = t[p].b.x, y = t[p].b.y, best = 1e9f, want = 0, bx = 0, by = 0;+ for (int q = 0; q < np; q++) {+ float d = (t[q].b.x - x) * (t[q].b.x - x) + (t[q].b.y - y) * (t[q].b.y - y);+ if (q != p && !gone[q] && side(q) != side(p) && d < best) best = d, bx = t[q].b.x, by = t[q].b.y;+ }+ if (best > 1e8f) return 0;+ want = fatan2(by - y, bx - x);+ float off = fabs_(wrap_pi(t[p].a - want)), ax = x + fcos(t[p].a) * 18, ay = y + fsin(t[p].a) * 18;+ bool clear = clear_shot(x, y, bx, by);+ if (clear) seen[p] = rt;+ if (driving[p]) { /* on the move: until it's time, or a wall ahead */+ if (rt > until[p] || solid(ax, ay)) driving[p] = 0;+ return driving[p];+ }+ if (off < 0.14f && clear && rt - t[p].shot_t > 400) return bot_tap(p, true);+ bool lost = rt - seen[p] > 1500 && !solid(ax, ay) && rn(30) == 0; /* no shot for a while: somewhere else */+ if ((off < 0.2f && best > 140 * 140 && rn(4) == 0) || lost) driving[p] = 1, until[p] = rt + 500 + rn(800);+ return driving[p];+}+#endif++const game_t g_tanks = {"Tanks", "Tanks",+ "Your tank turns on the spot. Hold your key to drive, let go to fire. Shells bounce off the "+ "walls. One hit and you're out: the last tank left takes the round.",+ 1, start, step, draw, icon BOT(bot)};diff --git a/games/fourplayer/src/icon.png b/games/fourplayer/src/icon.pngnew file mode 100644index 00000000..86d11626Binary files /dev/null and b/games/fourplayer/src/icon.png differdiff --git a/games/fourplayer/src/kit.c b/games/fourplayer/src/kit.cnew file mode 100644index 00000000..5e2c6ad1--- /dev/null+++ b/games/fourplayer/src/kit.c@@ -0,0 +1,175 @@+/* 2 3 4 Players: chance, numbers and the shapes every minigame draws with. */+#include "fp.h"++/* ---- chance (app.c seeds it, and every key pressed stirs it) */+uint32_t fp_seed = 1;+uint32_t rnd(void) {+ fp_seed ^= fp_seed << 13;+ fp_seed ^= fp_seed >> 17;+ return fp_seed ^= fp_seed << 5;+}+int rn(int n) { return n > 0 ? (int)(rnd() % (uint32_t)n) : 0; }+float rf(void) { return (float)(rnd() >> 8) * (1.0f / 16777216.0f); }+float rr(float a, float b) { return a + (b - a) * rf(); }++/* ---- numbers */+float fabs_(float v) { return v < 0 ? -v : v; }+float fsqrt(float v) { return v > 0 ? __builtin_sqrtf(v) : 0; }+float clampf(float v, float a, float b) { return v < a ? a : v > b ? b : v; }+int clampi(int v, int a, int b) { return v < a ? a : v > b ? b : v; }+float wrap_pi(float a) {+ while (a > PI) a -= 2 * PI;+ while (a < -PI) a += 2 * PI;+ return a;+}+/* a parabola, refined: within a thousandth */+float fsin(float a) {+ a = wrap_pi(a);+ float y = 1.2732395f * a - 0.4052847f * a * fabs_(a);+ return 0.225f * (y * fabs_(y) - y) + y;+}+float fcos(float a) { return fsin(a + PI / 2); }+float fatan2(float y, float x) {+ float ax = fabs_(x), ay = fabs_(y);+ if (ax < 1e-9f && ay < 1e-9f) return 0;+ float a = ax > ay ? ay / ax : ax / ay, s = a * a;+ float r = ((-0.0464964749f * s + 0.15931422f) * s - 0.327622764f) * s * a + a;+ if (ay > ax) r = PI / 2 - r;+ if (x < 0) r = PI - r;+ return y < 0 ? -r : r;+}+bool collide(body_t *a, body_t *b, float r, float ma, float mb, float e) {+ float dx = b->x - a->x, dy = b->y - a->y, d2 = dx * dx + dy * dy;+ if (d2 >= r * r) return false;+ float d = fsqrt(d2), nx = 1, ny = 0;+ if (d > 0.001f) nx = dx / d, ny = dy / d;+ float over = r - d, ia = mb / (ma + mb), ib = ma / (ma + mb);+ a->x -= nx * over * ia, a->y -= ny * over * ia, b->x += nx * over * ib, b->y += ny * over * ib;+ float vn = (b->vx - a->vx) * nx + (b->vy - a->vy) * ny;+ if (vn < 0) {+ float j = -(1 + e) * vn / (1 / ma + 1 / mb);+ a->vx -= j * nx / ma, a->vy -= j * ny / ma, b->vx += j * nx / mb, b->vy += j * ny / mb;+ }+ return true;+}+#ifdef HOST+int fp_round_serial; /* (app.c counts the rounds) */+bool bot_fresh(int p) {+ static int seen[NMAX];+ if (seen[p] == fp_round_serial) return false;+ seen[p] = fp_round_serial;+ return true;+}+int bot_tap(int p, bool want) {+ static uint8_t was[NMAX];+ int r = want && !was[p];+ was[p] = (uint8_t)r;+ return r;+}+#endif++char *num(char *o, uint32_t v) { /* v in o, then the end of it */+ char t[10];+ int n = 0;+ do t[n++] = (char)('0' + v % 10); while (v /= 10);+ while (n) *o++ = t[--n];+ *o = 0;+ return o;+}++/* ---- shapes */+static int iround(float v) { return (int)(v < 0 ? v - 0.5f : v + 0.5f); }+void disc(float x, float y, float r, color_t c) {+ int R = iround(r);+ if (R > 0) gfx_circle(iround(x), iround(y), R, c, 256);+}+void disc_a(float x, float y, float r, color_t c, int a) {+ int R = iround(r);+ if (R > 0 && a > 0) gfx_circle(iround(x), iround(y), R, c, a > 256 ? 256 : a);+}+void box(int x, int y, int w, int h, int r, color_t c) { gfx_rrect(x, y, w, h, r, c, 256); }++/* a convex polygon, its left and right edges softened */+void poly(const float *xy, int n, color_t c) {+ float top = xy[1], bot = xy[1];+ for (int i = 1; i < n; i++) top = xy[2 * i + 1] < top ? xy[2 * i + 1] : top, bot = xy[2 * i + 1] > bot ? xy[2 * i + 1] : bot;+ int r0 = (int)top, r1 = (int)bot + 1;+ if (r0 < gfx_y0) r0 = gfx_y0;+ if (r1 > gfx_y1) r1 = gfx_y1;+ for (int row = r0; row < r1; row++) {+ float yc = row + 0.5f, lo = 1e9f, hi = -1e9f;+ for (int i = 0; i < n; i++) {+ int j = i + 1 < n ? i + 1 : 0;+ float ya = xy[2 * i + 1], yb = xy[2 * j + 1];+ if ((ya <= yc) != (yb <= yc)) {+ float x = xy[2 * i] + (yc - ya) * (xy[2 * j] - xy[2 * i]) / (yb - ya);+ lo = x < lo ? x : lo, hi = x > hi ? x : hi;+ }+ }+ if (hi <= lo) continue;+ if (lo < 0) lo = 0;+ if (hi > SCREEN_W) hi = SCREEN_W;+ int a = (int)lo, b = (int)hi;+ if (a == b) {+ gfx_fill_alpha(a, row, 1, 1, c, (int)((hi - lo) * 256));+ continue;+ }+ gfx_fill_alpha(a, row, 1, 1, c, (int)((a + 1 - lo) * 256));+ if (b > a + 1) gfx_fill(a + 1, row, b - a - 1, 1, c);+ if (b < SCREEN_W) gfx_fill_alpha(b, row, 1, 1, c, (int)((hi - b) * 256));+ }+}++void quad(float x, float y, float a, float l0, float l1, float w, color_t c) {+ float cs = fcos(a), sn = fsin(a), p[8];+ for (int i = 0; i < 4; i++) {+ float l = i == 1 || i == 2 ? l1 : l0, e = i < 2 ? -w : w;+ p[2 * i] = x + cs * l - sn * e, p[2 * i + 1] = y + sn * l + cs * e;+ }+ if (y + (l0 > l1 ? l0 : l1) + w + 2 < gfx_y0 - 64 || y - 200 > gfx_y1) return;+ poly(p, 4, c);+}++void line(float x0, float y0, float x1, float y1, float w, color_t c) {+ float dx = x1 - x0, dy = y1 - y0, l = fsqrt(dx * dx + dy * dy);+ if (l < 0.01f) return;+ float nx = -dy / l * w / 2, ny = dx / l * w / 2;+ float p[8] = {x0 + nx, y0 + ny, x1 + nx, y1 + ny, x1 - nx, y1 - ny, x0 - nx, y0 - ny};+ poly(p, 4, c);+}++void say(const np_font_t *f, int cx, int base, const char *s, color_t c) {+ gfx_text_center(f, cx + 1, base + 2, s, 0, 150);+ gfx_text_center(f, cx, base, s, c, 256);+}++void eyes(float x, float y, float r, float look) {+ float cs = fcos(look), sn = fsin(look);+ float ex = x + cs * r * 0.32f, ey = y + sn * r * 0.32f - r * 0.12f, s = r * 0.33f, e = r * 0.27f;+ if (e < 1.6f) e = 1.6f;+ for (int i = -1; i <= 1; i += 2) {+ disc(ex + i * s, ey, e, WHITE);+ disc(ex + i * s + cs * e * 0.45f, ey + sn * e * 0.45f, e * 0.55f, HEX(0x15122A));+ }+}++void blob(float x, float y, float r, int p, float look) {+ disc_a(x + 1.5f, y + 2.5f, r, 0, 70); /* its shadow */+ disc(x, y, r, dark[p]);+ disc(x - r * 0.06f, y - r * 0.1f, r * 0.9f, col[p]);+ disc_a(x - r * 0.35f, y - r * 0.4f, r * 0.32f, lite[p], 170);+ eyes(x, y, r, look);+}++/* the backspace key's sign: an arrow head with a cross in it */+static void erase_sign(int x, int mid, color_t c) {+ float p[10] = {x, mid, x + 5, mid - 5, x + 15, mid - 5, x + 15, mid + 5, x + 5, mid + 5};+ poly(p, 5, c);+ line(x + 7, mid - 2.5f, x + 12, mid + 2.5f, 1.4f, INK);+ line(x + 7, mid + 2.5f, x + 12, mid - 2.5f, 1.4f, INK);+}+int keytag_w(int p) { return *keyname(p) ? gfx_text_width(&np_font_body, keyname(p)) : 15; }+void keytag(int cx, int base, int p, color_t c) {+ if (*keyname(p)) gfx_text_center(&np_font_body, cx, base, keyname(p), c, 256);+ else erase_sign(cx - 8, base - 4, c);+}diff --git a/games/fourplayer/src/main.c b/games/fourplayer/src/main.cnew file mode 100644index 00000000..ba8d5f32--- /dev/null+++ b/games/fourplayer/src/main.c@@ -0,0 +1,35 @@+/* 2 3 4 Players for the NumWorks calculator (and NumPlay's other targets): one-key minigames for 2, 3 or 4+ * players on one calculator, and teams, 2 v 2 and 3 v 3.+ *+ * Inspired by the party app 2 3 4 Player Games. Made by Mason Chen as part of NumPlay. */+#include "app.h"+#include "plat.h"++#ifndef HOST+uint32_t perf_frames __attribute__((used)); /* read by tools/emu.py */+uint32_t perf_max __attribute__((used));+uint32_t perf_slow __attribute__((used));++int main(void) {+ plat_begin();+ app_init(plat_millis());+ uint32_t last = plat_millis();+ for (;;) {+ if (!app_step(plat_keys(), plat_millis())) break;+ if (app_dirty()) {+ plat_vblank();+ uint32_t t0 = plat_millis();+ app_draw();+ uint32_t t = plat_millis(), dt = t - t0;+ if (dt > perf_max) perf_max = dt;+ if (t - last > 33) perf_slow++;+ last = t;+ perf_frames++;+ } else {+ plat_sleep(10);+ last = plat_millis();+ }+ }+ return plat_end();+}+#endifdiff --git a/games/fourplayer/src/plat.h b/games/fourplayer/src/plat.hnew file mode 100644index 00000000..388184fd--- /dev/null+++ b/games/fourplayer/src/plat.h@@ -0,0 +1,28 @@+/* 2 3 4 Players: the platform (the calculator, another target, or a computer for tests).+ *+ * plat_eadk.c: EADK's display, keyboard, time and files (the NumWorks, and every target of platform/).+ * plat_host.c: a frame buffer that tests save as images, scripted keys and simulated time (tests/play.c). */+#ifndef FP_PLAT_H+#define FP_PLAT_H+#include <stdbool.h>+#include <stddef.h>+#include <stdint.h>++/* the keys, numbered as EADK numbers them (eadk_key_*) */+enum {+ FK_LEFT = 0, FK_UP = 1, FK_DOWN = 2, FK_RIGHT = 3, FK_OK = 4, FK_BACK = 5, FK_HOME = 6, FK_ONOFF = 8, FK_SHIFT = 12,+ FK_BACKSPACE = 17, FK_SEVEN = 30, FK_RPAREN = 34, FK_ZERO = 48, FK_EXE = 52+};+#define KEY(k) ((uint64_t)1 << (k))++uint64_t plat_keys(void); /* the keys held now, a bit each */+uint32_t plat_millis(void);+void plat_sleep(uint32_t ms);+void plat_vblank(void);+uint32_t plat_random(void);+void plat_push(int x, int y, int w, int h, const uint16_t *px);+bool plat_save(const char *name, const void *data, uint32_t len);+const uint8_t *plat_load(const char *name, uint32_t *len); /* NULL if absent */+void plat_begin(void);+int plat_end(void);+#endifdiff --git a/games/fourplayer/src/plat_eadk.c b/games/fourplayer/src/plat_eadk.cnew file mode 100644index 00000000..dbb976b2--- /dev/null+++ b/games/fourplayer/src/plat_eadk.c@@ -0,0 +1,23 @@+/* EADK: the NumWorks's display, keyboard, time and files, which every target of platform/ also gives. */+#include <eadk.h>+#include "../../common/epsilon_app.h"+#include "../../common/epsilon_files.h"+#include "plat.h"++#ifndef NP_PORT /* (another target: platform/port.mk names the app) */+const char eadk_app_name[] __attribute__((section(".rodata.eadk_app_name"))) = "2 3 4 Players";+const uint32_t eadk_api_level __attribute__((section(".rodata.eadk_api_level"))) = 0;+#endif++uint64_t plat_keys(void) { return (uint64_t)eadk_keyboard_scan(); }+uint32_t plat_millis(void) { return (uint32_t)eadk_timing_millis(); }+void plat_sleep(uint32_t ms) { eadk_timing_msleep(ms); }+void plat_vblank(void) { eadk_display_wait_for_vblank(); }+uint32_t plat_random(void) { return eadk_random(); }+void plat_push(int x, int y, int w, int h, const uint16_t *px) {+ eadk_display_push_rect((eadk_rect_t){(uint16_t)x, (uint16_t)y, (uint16_t)w, (uint16_t)h}, px);+}+bool plat_save(const char *name, const void *data, uint32_t len) { return ef_write(name, data, len); }+const uint8_t *plat_load(const char *name, uint32_t *len) { return ef_read(name, len); }+void plat_begin(void) { np_app_begin(); }+int plat_end(void) { return np_app_end(); }diff --git a/games/fourplayer/src/plat_host.c b/games/fourplayer/src/plat_host.cnew file mode 100644index 00000000..f6d6db2b--- /dev/null+++ b/games/fourplayer/src/plat_host.c@@ -0,0 +1,73 @@+/* A computer: the screen goes to a frame buffer that tests save as images, keys come from a script, time is+ * simulated, files are in a folder. */+#include <stdio.h>+#include <stdlib.h>+#include "plat.h"++#define W 320+#define H 240+uint16_t host_fb[W * H];+uint32_t host_time; /* ms */+uint64_t host_keys;+uint32_t host_seed = 1;++uint64_t plat_keys(void) { return host_keys; }+uint32_t plat_millis(void) { return host_time; }+void plat_sleep(uint32_t ms) { host_time += ms; }+void plat_vblank(void) {}+uint32_t plat_random(void) {+ host_seed ^= host_seed << 13, host_seed ^= host_seed >> 17, host_seed ^= host_seed << 5;+ return host_seed;+}+void plat_push(int x, int y, int w, int h, const uint16_t *px) {+ for (int r = 0; r < h; r++)+ for (int c = 0; c < w; c++)+ if ((unsigned)(x + c) < W && (unsigned)(y + r) < H) host_fb[(y + r) * W + x + c] = px[r * w + c];+}++static char save_dir[256] = "build/host-saves";+void host_save_dir(const char *d) { snprintf(save_dir, sizeof save_dir, "%s", d); }+bool plat_save(const char *name, const void *data, uint32_t len) {+ char p[512];+ snprintf(p, sizeof p, "%s/%s", save_dir, name);+ FILE *f = fopen(p, "wb");+ if (!f) return false;+ bool ok = fwrite(data, 1, len, f) == len;+ fclose(f);+ return ok;+}+/* (each file read gets its own buffer: callers may hold two at once) */+static uint8_t loaded[4][1 << 14];+static int next_load;+const uint8_t *plat_load(const char *name, uint32_t *len) {+ char p[512];+ snprintf(p, sizeof p, "%s/%s", save_dir, name);+ FILE *f = fopen(p, "rb");+ if (!f) return NULL;+ uint8_t *b = loaded[next_load++ & 3];+ *len = (uint32_t)fread(b, 1, sizeof loaded[0], f);+ fclose(f);+ return b;+}+void plat_begin(void) {}+int plat_end(void) { return 0; }++/* writes the screen as a PPM image */+void host_shot(const char *path) {+ FILE *f = fopen(path, "wb");+ if (!f) return;+ fprintf(f, "P6\n%d %d\n255\n", W, H);+ for (int i = 0; i < W * H; i++) {+ uint16_t c = host_fb[i];+ uint8_t rgb[3] = {(uint8_t)((c >> 11) * 255 / 31), (uint8_t)(((c >> 5) & 63) * 255 / 63), (uint8_t)((c & 31) * 255 / 31)};+ fwrite(rgb, 1, 3, f);+ }+ fclose(f);+}+/* the screen as raw RGB565 (tools/record.py --frames) */+void host_raw(const char *path) {+ FILE *f = fopen(path, "wb");+ if (!f) return;+ fwrite(host_fb, 2, W * H, f);+ fclose(f);+}diff --git a/games/fourplayer/tests/play.c b/games/fourplayer/tests/play.cnew file mode 100644index 00000000..85af28ac--- /dev/null+++ b/games/fourplayer/tests/play.c@@ -0,0 +1,157 @@+/* Plays 2 3 4 Players on a computer, with scripted keys and computer players, and saves screenshots.+ *+ * play [--frames N] [--keys "10:ok,20-30:p1,..."] [--shots "40:name.ppm,..."] [--out DIR] [--saves DIR]+ * [--trace] [--fuzz SEED] [--seed N] [--bots FRAME] [--raw DIR] [--every K]+ *+ * A frame is 20 ms. Keys: left right up down ok exe back home onoff, and p1 to p6 the players' (shift,+ * backspace, 0, EXE, 7 and )). "a-b:key" holds the key from frame a to b, "a:key" for one frame.+ * --bots F: from frame F on, the computer plays for every player whenever a round is on (each game's bot).+ * --fuzz presses random keys (Home now and then ends a session, the next one starts from the save).+ * --raw DIR saves every frame drawn as raw RGB565 (tools/record.py --frames); --every K draws every Kth frame+ * (the screenshots and --raw draw theirs anyway). --trace prints each screen as it comes (SCREEN ...).+ * At the end: STATE ... */+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include "../src/app.h"+#include "../src/fp.h"+#include "../src/plat.h"++extern uint32_t host_time, host_seed;+extern uint64_t host_keys;+void host_shot(const char *path);+void host_raw(const char *path);+void host_save_dir(const char *d);++typedef struct {+ int a, b;+ uint64_t k;+} Hold;+static Hold holds[8192];+static int nholds;+typedef struct {+ int frame;+ char name[128];+} Shot;+static Shot shots[256];+static int nshots;+static char out_dir[512] = ".", raw_dir[512];++static uint64_t key_of(const char *s) {+ static const char *const names[] = {"left", "right", "up", "down", "ok", "exe", "back", "home", "onoff"};+ static const int bits[] = {FK_LEFT, FK_RIGHT, FK_UP, FK_DOWN, FK_OK, FK_EXE, FK_BACK, FK_HOME, FK_ONOFF};+ for (int i = 0; i < 9; i++)+ if (!strcmp(s, names[i])) return KEY(bits[i]);+ if (s[0] == 'p' && s[1] >= '1' && s[1] <= '6' && !s[2]) return app_player_key(s[1] - '1');+ fprintf(stderr, "unknown key %s\n", s);+ exit(2);+}+static void parse_keys(const char *spec) {+ char buf[65536];+ snprintf(buf, sizeof buf, "%s", spec);+ for (char *t = strtok(buf, ","); t; t = strtok(NULL, ",")) {+ int a, b;+ char name[32];+ if (sscanf(t, "%d-%d:%31s", &a, &b, name) == 3) holds[nholds++] = (Hold){a, b, key_of(name)};+ else if (sscanf(t, "%d:%31s", &a, name) == 2) holds[nholds++] = (Hold){a, a, key_of(name)};+ }+}+static void parse_shots(const char *spec) {+ char buf[65536];+ snprintf(buf, sizeof buf, "%s", spec);+ for (char *t = strtok(buf, ","); t; t = strtok(NULL, ",")) {+ Shot s;+ if (sscanf(t, "%d:%127s", &s.frame, s.name) == 2) shots[nshots++] = s;+ }+}++static const char *const SCREEN[] = {"mode", "games", "rules", "play", "score", "over", "pause", "ask"};+static void print_state(const char *what) {+ app_state_t s;+ app_state(&s);+ printf("%s %s mode %d game %d cur %d target %d round %d sides %d score %d,%d,%d,%d winner %d matches %d", what,+ SCREEN[s.screen], s.mode, s.game, s.cur, s.target, s.round, s.nsides, s.score[0], s.score[1], s.score[2],+ s.score[3], s.winner, s.matches);+ if (!strcmp(what, "STATE")) {+ printf(" wins");+ for (int g = 0; g < 15; g++) printf(" %d,%d,%d,%d,%d,%d", s.wins[g][0], s.wins[g][1], s.wins[g][2], s.wins[g][3], s.wins[g][4], s.wins[g][5]);+ }+ printf("\n");+}++int main(int argc, char **argv) {+ int frames = 600, fuzz = -1, bots = -1, every = 1;+ bool trace = false;+ for (int i = 1; i < argc; i++) {+ if (!strcmp(argv[i], "--frames") && i + 1 < argc) frames = atoi(argv[++i]);+ else if (!strcmp(argv[i], "--keys") && i + 1 < argc) parse_keys(argv[++i]);+ else if (!strcmp(argv[i], "--shots") && i + 1 < argc) parse_shots(argv[++i]);+ else if (!strcmp(argv[i], "--out") && i + 1 < argc) snprintf(out_dir, sizeof out_dir, "%s", argv[++i]);+ else if (!strcmp(argv[i], "--raw") && i + 1 < argc) snprintf(raw_dir, sizeof raw_dir, "%s", argv[++i]);+ else if (!strcmp(argv[i], "--saves") && i + 1 < argc) host_save_dir(argv[++i]);+ else if (!strcmp(argv[i], "--trace")) trace = true;+ else if (!strcmp(argv[i], "--fuzz") && i + 1 < argc) fuzz = atoi(argv[++i]);+ else if (!strcmp(argv[i], "--seed") && i + 1 < argc) host_seed = (uint32_t)atoi(argv[++i]) | 1;+ else if (!strcmp(argv[i], "--bots") && i + 1 < argc) bots = atoi(argv[++i]);+ else if (!strcmp(argv[i], "--every") && i + 1 < argc) every = atoi(argv[++i]);+ else {+ fprintf(stderr, "play: what is %s?\n", argv[i]);+ return 2;+ }+ }+ uint32_t fz = (uint32_t)fuzz * 2654435761u + 12345;+ host_time = 1000;+ app_init(host_time);+ int last_screen = -1, sessions = 1, raw_n = 0;+ uint64_t fuzz_keys = 0;+ for (int f = 0; f < frames; f++) {+ uint64_t k = 0;+ for (int i = 0; i < nholds; i++)+ if (f >= holds[i].a && f <= holds[i].b) k |= holds[i].k;+ if (fuzz >= 0) { /* random keys: a few held at once, changing often */+ fz ^= fz << 13, fz ^= fz >> 17, fz ^= fz << 5;+ if (fz % 5 == 0) {+ static const int pool[] = {FK_LEFT, FK_RIGHT, FK_UP, FK_DOWN, FK_OK, FK_OK, FK_EXE, FK_BACK, FK_SHIFT, FK_BACKSPACE,+ FK_ZERO, FK_EXE, FK_SEVEN, FK_RPAREN, FK_SHIFT, FK_ZERO, FK_HOME};+ int key = pool[(fz >> 8) % (sizeof pool / sizeof *pool)];+ if (key == FK_HOME && (fz >> 20) % 6) key = FK_OK; /* (Home now and then) */+ fuzz_keys ^= KEY(key);+ }+ if (fz % 23 == 0) fuzz_keys = 0;+ k |= fuzz_keys;+ }+ if (bots >= 0 && f >= bots) k |= app_bot_keys();+ host_keys = k;+ host_time += 20;+ if (!app_step(k, host_time)) {+ if (fuzz < 0) {+ printf("QUIT frame %d\n", f);+ break;+ }+ host_keys = fuzz_keys = 0; /* a new session, from the save */+ app_init(host_time), sessions++;+ }+ app_state_t s;+ app_state(&s);+ if (trace && s.screen != last_screen) print_state("SCREEN");+ last_screen = s.screen;+ bool shot = false;+ for (int i = 0; i < nshots; i++) shot |= shots[i].frame == f;+ if (app_dirty() && (f % every == 0 || shot || raw_dir[0])) app_draw();+ if (raw_dir[0]) {+ char p[1024];+ snprintf(p, sizeof p, "%s/%06d_%06d.raw", raw_dir, raw_n++, f * 20);+ host_raw(p);+ }+ for (int i = 0; i < nshots; i++)+ if (shots[i].frame == f) {+ char p[1024];+ app_draw();+ snprintf(p, sizeof p, "%s/%s", out_dir, shots[i].name);+ host_shot(p);+ }+ }+ if (fuzz >= 0) printf("SESSIONS %d\n", sessions);+ print_state("STATE");+ return 0;+}diff --git a/games/fourplayer/tools/icon.py b/games/fourplayer/tools/icon.pynew file mode 100644index 00000000..f15ecc4b--- /dev/null+++ b/games/fourplayer/tools/icon.py@@ -0,0 +1,44 @@+#!/usr/bin/env python3+"""Draws src/icon.png (55 x 56): the four players, red, blue, green and yellow, looking at each other on the game's+night violet. Then round it as every NumPlay icon: python3 ../../tools/round_icons.py src/icon.png++Usage (in games/fourplayer): python3 tools/icon.py"""+import math+import os++from PIL import Image, ImageDraw++HERE = os.path.dirname(os.path.abspath(__file__))+K = 8 # drawn 8 times bigger, then scaled down+W, H = 55 * K, 56 * K+++def lerp(a, b, t):+ return tuple(round(x + (y - x) * t) for x, y in zip(a, b))+++im = Image.new("RGB", (W, H))+d = ImageDraw.Draw(im)+top, bottom = (0x2B, 0x24, 0x52), (0x10, 0x0D, 0x22)+for y in range(H):+ d.line([(0, y), (W, y)], fill=lerp(top, bottom, y / (H - 1)))++PLAYERS = [((0xF2, 0x45, 0x3D), 15, 15), ((0x2F, 0x7F, 0xF0), 40, 15), ((0x2D, 0xB6, 0x53), 15, 41),+ ((0xF5, 0xB4, 0x00), 40, 41)]+R = 11.5+for c, x, y in PLAYERS:+ x, y = x * K, (y + 0.5) * K+ look = math.atan2(28 * K - y, 27.5 * K - x)+ d.ellipse([x - R * K + 1.5 * K, y - R * K + 2.5 * K, x + R * K + 1.5 * K, y + R * K + 2.5 * K], fill=(8, 6, 18))+ d.ellipse([x - R * K, y - R * K, x + R * K, y + R * K], fill=lerp(c, (0, 0, 0), 0.3))+ d.ellipse([x - R * 0.9 * K - 0.6 * K, y - R * 0.9 * K - 1.1 * K, x + R * 0.9 * K - 0.6 * K, y + R * 0.9 * K - 1.1 * K], fill=c)+ ex, ey = x + math.cos(look) * 3.6 * K, y + math.sin(look) * 3.6 * K - 1.4 * K+ for s in (-1, 1):+ cx = ex + s * 3.8 * K+ d.ellipse([cx - 3.1 * K, ey - 3.1 * K, cx + 3.1 * K, ey + 3.1 * K], fill=(255, 255, 255))+ px, py = cx + math.cos(look) * 1.4 * K, ey + math.sin(look) * 1.4 * K+ d.ellipse([px - 1.7 * K, py - 1.7 * K, px + 1.7 * K, py + 1.7 * K], fill=(0x15, 0x12, 0x2A))++im = im.resize((55, 56), Image.LANCZOS)+im.save(os.path.join(HERE, "..", "src", "icon.png"))+print("src/icon.png")