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
Bad Time Simulator: the fight, its attacks and tests
Mason3633a1ef99f6abf
diff --git a/games/badtime/.gitignore b/games/badtime/.gitignorenew file mode 100644index 00000000..342d9dda--- /dev/null+++ b/games/badtime/.gitignore@@ -0,0 +1,2 @@+output/+build/diff --git a/games/badtime/Makefile b/games/badtime/Makefilenew file mode 100644index 00000000..aa44b7d2--- /dev/null+++ b/games/badtime/Makefile@@ -0,0 +1,93 @@+# Bad Time Simulator on the NumWorks calculator.+# make output/badtime.nwa+# make check link it like the calculator does (output/badtime.bin), within the RAM and the app space+# make host build/play: the game on a computer, for tests and pictures+# make test every attack against jcw87's scripts (tests/expect.h), the heart's and the damage's numbers, a+# bot through the whole fight to the win, the same fight to the same hash on every computer,+# saves (kept, damaged, storage full) and random keys (with the sanitizers)+# make armperf the busiest attacks drawn at an N0110's speed (in tools/emu.py): the frame rate+# make data src/attacks.c, src/attacks.h and tests/expect.h from jcw87's attack scripts (REF=its folder),+# src/art.c and src/art.h from tools/art.py+Q ?= @+# the ARM toolchain on the PATH, or in TOOLCHAIN=<its bin folder>+TOOLCHAIN ?=+BIN = $(if $(TOOLCHAIN),$(TOOLCHAIN)/)+CC = $(BIN)arm-none-eabi-gcc+STRIP ?= $(BIN)arm-none-eabi-strip --strip-unneeded+NWLINK ?= npx --yes -- [email protected]+BUILD_DIR = output+L = ../../launcher/src+REF ?= $(HOME)/Documents/Assets/gamesrc/badtime/ref+PYTHON ?= python3++# the fight and the drawing run every frame at -O2; the screens are small at -Os+HOT = src/sim.c src/draw.c src/fight.c+COLD = src/main.c src/save.c src/text.c src/art.c src/attacks.c $(L)/gfx.c $(L)/font_data.c $(L)/xfont.c+COLD_OBJ = $(patsubst %.c,$(BUILD_DIR)/cold/%.o,$(notdir $(COLD)))+HEADERS = $(wildcard src/*.h) $(L)/gfx.h $(L)/font.h $(L)/np.h ../common/epsilon_app.h ../common/epsilon_files.h++CFLAGS = -std=gnu11 $(shell $(NWLINK) eadk-cflags-device) -DPLATFORM_DEVICE=1 -DEPSILON_XL=1 -I$(L) -Isrc+CFLAGS += -Wall -Wextra -Wno-unused-parameter -fno-math-errno -ffp-contract=off+CFLAGS += -fno-tree-loop-distribute-patterns -flto -fno-fat-lto-objects -fwhole-program -fvisibility=internal+CFLAGS += -ffunction-sections -fdata-sections+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++# apps get 133652 bytes of RAM on custom builds of Epsilon 25.2 (148928 or more on official ones), and the+# calculator's app space is 2031616 bytes+RAM = 133652+APP_SPACE = 2031616++.PHONY: build check clean host test data armperf+build: $(BUILD_DIR)/badtime.nwa++check: $(BUILD_DIR)/badtime.nwa+ $(Q) $(NWLINK) nwa-bin --ram-length $(RAM) $< $(BUILD_DIR)/badtime.bin+ $(Q) n=$$(wc -c < $(BUILD_DIR)/badtime.bin); [ $$n -le $(APP_SPACE) ] || \+ { echo "badtime.nwa is bigger than the calculator's app space ($(APP_SPACE) bytes)"; exit 1; }; \+ echo "BIN $(BUILD_DIR)/badtime.bin: $$n bytes, $$(( $(APP_SPACE) - n )) to spare, RAM within $(RAM) bytes"++$(BUILD_DIR)/cold/%.o: src/%.c $(HEADERS) | $(BUILD_DIR)+ @mkdir -p $(@D)+ $(Q) $(CC) $(CFLAGS) -Os -c $< -o $@+$(BUILD_DIR)/cold/%.o: $(L)/%.c $(HEADERS) | $(BUILD_DIR)+ @mkdir -p $(@D)+ $(Q) $(CC) $(CFLAGS) -Os -c $< -o $@++$(BUILD_DIR)/badtime.nwa: $(HOT) src/plat_eadk.c $(COLD_OBJ) $(HEADERS) $(BUILD_DIR)/icon.o+ @echo "LD $@"+ $(Q) $(CC) $(CFLAGS) -O2 $(LDFLAGS) $(HOT) src/plat_eadk.c $(COLD_OBJ) $(BUILD_DIR)/icon.o -o $@+ $(Q) $(STRIP) $@++# (the game with tools/armperf.py's hooks)+$(BUILD_DIR)/armperf.nwa: $(HOT) src/plat_eadk.c $(COLD) $(HEADERS) $(BUILD_DIR)/icon.o+ $(Q) $(CC) $(CFLAGS) -O2 -DPERF $(LDFLAGS) $(HOT) src/plat_eadk.c $(COLD) $(BUILD_DIR)/icon.o -o $@+armperf: $(BUILD_DIR)/armperf.nwa+ $(PYTHON) tools/armperf.py $<++$(BUILD_DIR)/icon.o: src/icon.png | $(BUILD_DIR)+ @echo "ICON $<"+ $(Q) $(NWLINK) png-icon-o $< $@++$(BUILD_DIR):+ $(Q) mkdir -p $@++data:+ $(PYTHON) -I tools/attacks.py $(REF)+ $(PYTHON) tools/art.py++# on a computer: tests/play.c checks the fight and the whole app, and saves pictures+HOST_SRC = tests/play.c src/sim.c src/fight.c src/draw.c src/save.c src/text.c src/art.c src/attacks.c \+ $(L)/gfx.c $(L)/font_data.c $(L)/xfont.c+HOST_FLAGS = -std=gnu11 -O2 -g -Wall -Wextra -Wno-unused-parameter -ffp-contract=off -Isrc -I$(L) -DHOST+host: build/play+build/play: $(HOST_SRC) src/main.c $(wildcard src/*.h) tests/expect.h+ $(Q) mkdir -p build+ $(Q) cc $(HOST_FLAGS) -fsanitize=address,undefined -fno-sanitize-recover=undefined $(HOST_SRC) -lm -o $@+ @echo "HOST $@"+test: build/play+ build/play test build++clean:+ $(Q) rm -rf $(BUILD_DIR) builddiff --git a/games/badtime/src/art.c b/games/badtime/src/art.cnew file mode 100644index 00000000..e0764b0c--- /dev/null+++ b/games/badtime/src/art.c@@ -0,0 +1,116 @@+/* Generated by tools/art.py: don't edit. The game's own pictures. */+#include "art.h"++static const uint32_t PAL_JUDGE[16] = {0, 0x1C1418, 0x7D2433, 0x521826, 0xA83D4F, 0xE0B84C, 0xA27D2A, 0x2D2D38, 0x55556A, 0x0B0B10, 0xF4F2EE, 0xBDB8AE, 0x4A3324, 0x3EC8FF, 0xA8ECFF, 0x9FD8FF};+static const uint32_t PAL_GAVEL[16] = {0, 0x1A120C, 0x5C3A20, 0x8A5A34, 0xB07A48, 0xE0B84C, 0xA27D2A, 0x2A1E16, 0xFFFFFF, 0xBFF2FF, 0x3EC8FF, 0xFFF3B0};+static const uint32_t PAL_HEART[16] = {0, 0xFFFFFF};++static const uint8_t PX_HEAD[306] = {+ 0, 0, 0, 0, 0, 17, 17, 17, 17, 17, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 135, 136,+ 136, 136, 136, 136, 120, 1, 0, 0, 0, 0, 0, 0, 0, 0, 16, 135, 119, 119, 119, 119, 119, 119, 1, 0,+ 0, 0, 0, 0, 0, 0, 0, 16, 119, 119, 119, 119, 119, 119, 119, 1, 0, 0, 0, 0, 0, 0, 0, 0,+ 16, 119, 119, 119, 119, 119, 119, 119, 1, 0, 0, 0, 0, 0, 0, 0, 0, 16, 85, 85, 85, 85, 85, 85,+ 85, 1, 0, 0, 0, 0, 0, 0, 0, 0, 16, 102, 102, 102, 102, 102, 102, 102, 1, 0, 0, 0, 0, 0,+ 16, 17, 17, 17, 119, 119, 119, 119, 119, 119, 119, 17, 17, 17, 1, 0, 16, 129, 136, 136, 136, 136, 136, 136,+ 136, 136, 136, 136, 136, 136, 136, 24, 1, 113, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119,+ 119, 23, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 1, 0, 0, 0, 0, 145,+ 153, 153, 153, 153, 153, 153, 153, 25, 0, 0, 0, 0, 0, 0, 0, 0, 145, 153, 153, 153, 153, 153, 153, 153,+ 25, 0, 0, 0, 0, 0, 0, 0, 0, 145, 153, 153, 153, 153, 153, 153, 153, 25, 0, 0, 0, 0, 0, 0,+ 0, 0, 145, 153, 153, 153, 153, 153, 153, 153, 25, 0, 0, 0, 0, 0, 0, 0, 0, 16, 153, 153, 153, 153,+ 153, 153, 153, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 145, 153, 153, 153, 153, 153, 25, 0, 0, 0, 0,+ 0, 0, 0, 0, 0, 0, 16, 145, 153, 153, 153, 25, 1, 0, 0, 0, 0, 0,+};+const art_t ART_HEAD = {34, 18, 17, 18, PAL_JUDGE, PX_HEAD};+static const uint8_t PX_TORSO[224] = {+ 0, 0, 0, 16, 17, 17, 17, 17, 17, 17, 1, 0, 0, 0, 0, 0, 17, 65, 68, 68, 170, 170, 68, 68,+ 20, 17, 0, 0, 0, 17, 68, 36, 34, 34, 170, 170, 34, 34, 66, 68, 17, 0, 16, 68, 34, 34, 34, 34,+ 161, 26, 34, 34, 34, 34, 68, 1, 65, 34, 34, 34, 34, 34, 162, 42, 34, 34, 34, 34, 34, 20, 33, 34,+ 34, 34, 34, 34, 82, 37, 34, 34, 34, 34, 34, 18, 33, 34, 34, 34, 34, 101, 82, 37, 86, 34, 34, 34,+ 34, 18, 33, 34, 34, 34, 34, 82, 86, 101, 37, 34, 34, 34, 34, 18, 33, 34, 34, 34, 34, 34, 85, 85,+ 34, 34, 34, 34, 34, 18, 33, 34, 34, 34, 34, 34, 101, 86, 34, 34, 34, 34, 34, 18, 33, 34, 34, 34,+ 34, 34, 82, 37, 34, 34, 34, 34, 34, 18, 33, 34, 34, 34, 34, 34, 82, 37, 34, 34, 34, 34, 34, 18,+ 49, 34, 34, 34, 34, 34, 82, 37, 34, 34, 34, 34, 34, 19, 49, 34, 34, 34, 34, 34, 82, 37, 34, 34,+ 34, 34, 34, 19, 49, 35, 34, 34, 34, 34, 82, 37, 34, 34, 34, 34, 50, 19, 49, 51, 34, 34, 34, 34,+ 82, 37, 34, 34, 34, 34, 51, 19,+};+const art_t ART_TORSO = {28, 16, 14, 16, PAL_JUDGE, PX_TORSO};+static const uint8_t PX_LEGS[192] = {+ 0, 49, 35, 34, 34, 34, 34, 82, 37, 34, 34, 34, 34, 50, 19, 0, 0, 49, 35, 34, 34, 34, 34, 82,+ 37, 34, 34, 34, 34, 50, 19, 0, 16, 51, 35, 34, 34, 34, 34, 82, 37, 34, 34, 34, 34, 50, 51, 1,+ 16, 51, 35, 34, 34, 34, 34, 82, 37, 34, 34, 34, 34, 50, 51, 1, 16, 51, 51, 34, 34, 34, 34, 82,+ 37, 34, 34, 34, 34, 51, 51, 1, 49, 51, 51, 34, 34, 34, 34, 82, 37, 34, 34, 34, 34, 51, 51, 19,+ 49, 51, 51, 35, 34, 34, 34, 82, 37, 34, 34, 34, 50, 51, 51, 19, 97, 85, 85, 85, 85, 85, 85, 85,+ 85, 85, 85, 85, 85, 85, 85, 22, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 1,+ 0, 0, 0, 16, 204, 204, 28, 0, 0, 193, 204, 204, 1, 0, 0, 0, 0, 0, 0, 193, 204, 204, 28, 0,+ 0, 193, 204, 204, 28, 0, 0, 0, 0, 0, 0, 17, 17, 17, 17, 0, 0, 17, 17, 17, 17, 0, 0, 0,+};+const art_t ART_LEGS = {32, 12, 16, 12, PAL_JUDGE, PX_LEGS};+static const uint8_t PX_ARM_SIDE[48] = {+ 0, 17, 1, 16, 36, 18, 65, 34, 18, 33, 34, 19, 33, 34, 19, 33, 34, 19, 33, 50, 19, 33, 50, 19,+ 33, 50, 19, 49, 51, 19, 97, 85, 21, 17, 170, 17, 161, 170, 27, 161, 186, 27, 16, 187, 1, 0, 17, 0,+};+const art_t ART_ARM_SIDE = {6, 16, 3, 0, PAL_JUDGE, PX_ARM_SIDE};+static const uint8_t PX_ARM_UP[48] = {+ 0, 17, 0, 16, 170, 1, 161, 170, 27, 161, 186, 27, 17, 186, 17, 97, 85, 21, 49, 34, 19, 33, 34, 19,+ 33, 34, 19, 33, 34, 19, 33, 34, 20, 33, 34, 20, 33, 66, 20, 33, 68, 20, 16, 68, 1, 0, 17, 0,+};+const art_t ART_ARM_UP = {6, 16, 3, 15, PAL_JUDGE, PX_ARM_UP};+static const uint8_t PX_ARM_OUT[57] = {+ 0, 17, 17, 17, 17, 17, 17, 0, 0, 0, 65, 68, 68, 68, 68, 68, 20, 17, 1, 65, 34, 34, 34, 34,+ 34, 18, 166, 170, 17, 34, 34, 34, 34, 34, 34, 81, 170, 27, 49, 51, 51, 51, 51, 51, 19, 165, 187, 1,+ 17, 17, 17, 17, 17, 17, 17, 17, 1,+};+const art_t ART_ARM_OUT = {19, 6, 0, 1, PAL_JUDGE, PX_ARM_OUT};+static const uint8_t PX_ARM_DOWN[64] = {+ 0, 17, 1, 0, 16, 36, 18, 0, 65, 34, 18, 0, 33, 34, 19, 0, 33, 34, 19, 0, 16, 34, 51, 1,+ 16, 34, 51, 1, 16, 34, 51, 1, 0, 33, 51, 19, 0, 33, 51, 19, 0, 97, 85, 21, 0, 17, 170, 17,+ 0, 161, 170, 27, 0, 161, 186, 27, 0, 16, 187, 1, 0, 0, 17, 0,+};+const art_t ART_ARM_DOWN = {8, 16, 3, 0, PAL_JUDGE, PX_ARM_DOWN};+static const uint8_t PX_ARM_SHRUG[32] = {+ 0, 0, 16, 1, 0, 0, 161, 26, 16, 17, 161, 27, 65, 34, 161, 27, 33, 34, 97, 21, 33, 34, 19, 17,+ 33, 50, 1, 0, 16, 19, 0, 0,+};+const art_t ART_ARM_SHRUG = {8, 8, 1, 4, PAL_JUDGE, PX_ARM_SHRUG};+static const uint8_t PX_HEART[28] = {+ 16, 1, 16, 1, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 16, 17, 17, 1, 0, 17, 17, 0,+ 0, 16, 1, 0,+};+const art_t ART_HEART = {8, 7, 4, 4, PAL_HEART, PX_HEART};+static const uint8_t PX_HEART_BROKEN[28] = {+ 16, 1, 16, 1, 17, 1, 17, 17, 17, 17, 16, 17, 17, 1, 17, 17, 16, 17, 16, 1, 0, 1, 17, 0,+ 0, 16, 1, 0,+};+const art_t ART_HEART_BROKEN = {8, 7, 4, 4, PAL_HEART, PX_HEART_BROKEN};+static const uint8_t PX_GAVEL_SHUT[308] = {+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 17, 0, 17, 17, 17, 17, 17, 1,+ 16, 17, 17, 0, 0, 81, 102, 17, 65, 68, 68, 68, 68, 17, 113, 119, 119, 1, 0, 81, 102, 49, 67, 68,+ 68, 68, 68, 19, 113, 119, 119, 1, 16, 85, 102, 49, 51, 51, 51, 51, 51, 19, 113, 119, 119, 23, 16, 85,+ 102, 49, 51, 51, 51, 51, 51, 19, 113, 119, 119, 23, 16, 85, 102, 49, 51, 51, 51, 51, 51, 19, 113, 119,+ 119, 23, 16, 85, 102, 49, 51, 51, 51, 51, 51, 19, 113, 119, 119, 23, 16, 85, 102, 49, 51, 51, 51, 51,+ 51, 19, 113, 119, 119, 23, 16, 85, 102, 49, 51, 51, 51, 51, 51, 19, 113, 119, 119, 23, 0, 81, 102, 33,+ 34, 34, 34, 34, 34, 18, 113, 119, 119, 1, 0, 81, 102, 33, 34, 34, 34, 34, 34, 18, 113, 119, 119, 1,+ 0, 16, 17, 16, 17, 17, 51, 17, 17, 1, 16, 17, 17, 0, 0, 0, 0, 0, 0, 16, 51, 1, 0, 0,+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 51, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16,+ 35, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 35, 1, 0, 0, 0, 0, 0, 0, 0, 0,+ 0, 0, 0, 16, 35, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 35, 1, 0, 0, 0, 0,+ 0, 0, 0, 0, 0, 0, 0, 16, 86, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 86, 1,+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0,+};+const art_t ART_GAVEL_SHUT = {28, 22, 14, 7, PAL_GAVEL, PX_GAVEL_SHUT};+static const uint8_t PX_GAVEL_OPEN[308] = {+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 17, 0, 17, 17, 17, 17, 17, 1,+ 16, 17, 17, 0, 0, 81, 102, 17, 65, 68, 68, 68, 68, 17, 145, 153, 152, 1, 0, 81, 102, 49, 67, 68,+ 68, 68, 68, 19, 145, 136, 136, 25, 16, 85, 102, 49, 51, 51, 51, 51, 51, 19, 145, 136, 136, 152, 16, 85,+ 102, 49, 51, 51, 51, 51, 51, 19, 145, 136, 136, 152, 16, 85, 102, 49, 51, 51, 51, 51, 51, 19, 145, 136,+ 136, 152, 16, 85, 102, 49, 51, 51, 51, 51, 51, 19, 145, 136, 136, 152, 16, 85, 102, 49, 51, 51, 51, 51,+ 51, 19, 145, 136, 136, 152, 16, 85, 102, 49, 51, 51, 51, 51, 51, 19, 145, 136, 136, 152, 0, 81, 102, 33,+ 34, 34, 34, 34, 34, 18, 145, 136, 136, 25, 0, 81, 102, 33, 34, 34, 34, 34, 34, 18, 145, 153, 152, 1,+ 0, 16, 17, 16, 17, 17, 51, 17, 17, 1, 16, 17, 17, 0, 0, 0, 0, 0, 0, 16, 51, 1, 0, 0,+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 51, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16,+ 35, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 35, 1, 0, 0, 0, 0, 0, 0, 0, 0,+ 0, 0, 0, 16, 35, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 35, 1, 0, 0, 0, 0,+ 0, 0, 0, 0, 0, 0, 0, 16, 86, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 86, 1,+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0,+};+const art_t ART_GAVEL_OPEN = {28, 22, 14, 7, PAL_GAVEL, PX_GAVEL_OPEN};diff --git a/games/badtime/src/art.h b/games/badtime/src/art.hnew file mode 100644index 00000000..18cb6afe--- /dev/null+++ b/games/badtime/src/art.h@@ -0,0 +1,24 @@+/* Generated by tools/art.py: don't edit. */+#ifndef BT_ART_H+#define BT_ART_H+#include <stdint.h>++typedef struct {+ uint8_t w, h, ax, ay; /* size, anchor */+ const uint32_t *pal; /* 0xRRGGBB; index 0 is see-through */+ const uint8_t *px; /* 4 bits a pixel, two a byte, the first in the low half */+} art_t;++extern const art_t ART_HEAD;+extern const art_t ART_TORSO;+extern const art_t ART_LEGS;+extern const art_t ART_ARM_SIDE;+extern const art_t ART_ARM_UP;+extern const art_t ART_ARM_OUT;+extern const art_t ART_ARM_DOWN;+extern const art_t ART_ARM_SHRUG;+extern const art_t ART_HEART;+extern const art_t ART_HEART_BROKEN;+extern const art_t ART_GAVEL_SHUT;+extern const art_t ART_GAVEL_OPEN;+#endifdiff --git a/games/badtime/src/attacks.c b/games/badtime/src/attacks.cnew file mode 100644index 00000000..e8673084--- /dev/null+++ b/games/badtime/src/attacks.c@@ -0,0 +1,505 @@+/* Generated by tools/attacks.py from jcw87's attack scripts: don't edit. */+#include "attacks.h"++const double atk_pool[263] = {+ 0.0, 3.0, 1.0, 0.06666, 239.0, 226.0,+ 404.0, 391.0, 320.0, 304.0, 5.0, 0.26666,+ 0.5, 4.0, 54.0, 0.16666, 0.7, 0.4,+ 20.0, -24.0, 360.0, 25.0, 1.1, 189.0,+ 246.0, 0.333, 259.0, 166.0, 90.0, 640.0,+ 480.0, 449.0, 366.0, 180.0, 379.0, 446.0,+ 270.0, 0.9, 176.0, 45.0, 135.0, 436.0,+ 225.0, 315.0, 2.0, 240.0, 139.0, 306.0,+ 0.666, 499.0, 133.0, 251.0, 508.0, 376.0,+ 0.2, 128.0, 257.0, 95.0, 8.0, 120.0,+ 503.0, 6.4, 286.0, 100.0, 300.0, 0.23333,+ 0.93333, 0.33333, 1.66666, 61.0, 150.0, 9.0,+ 30.0, 11.0, 6.0, 40.0, 19.0, 60.0,+ 28.0, 111.0, 7.0, 386.0, 32.0, 34.0,+ -1.0, 10.0, 41.0, 12.0, 15.0, 17.0,+ 346.0, 356.0, 1.2, -61.0, 1.7, 210.0,+ 119.0, 105.0, 2.3, 51.0, 58.0, 296.0,+ 316.0, 70.0, 31.0, 0.6, 326.0, 336.0,+ 0.3, 55.0, 35.0, 1.5, 276.0, 110.0,+ 2.5, 513.0, 121.0, 220.0, -71.0, 161.0,+ 280.0, 16.0, 22.0, 517.0, 125.0, 349.0,+ 113.0, 231.0, 548.0, 151.0, 175.0, 327.0,+ 528.0, 283.0, 267.0, 85.0, 363.0, 331.0,+ 13.0, 443.0, 248.0, 7.3, 552.0, 140.0,+ -20.0, 160.0, 285.0, 73.0, 0.56666, 0.1,+ 563.0, 268.0, 65.0, 325.0, 76.0, 107.0,+ 7.2, 103.0, 21.0, 80.0, 89.0, 526.0,+ 341.0, 64.0, 512.0, 576.0, 56.0, 24.0,+ 584.0, 616.0, 171.0, 476.0, 86.0, 14.0,+ 282.0, 47.0, 1.9, 200.0, 371.0, 440.0,+ 98.0, 506.0, -64.0, 149.0, 704.0, 33.0,+ 608.0, 186.0, 400.0, 50.0, 23.0, 590.0,+ 27.0, 0.46666, 0.03333, 0.53333, 62.0, 330.0,+ 309.0, 314.0, 354.0, 364.0, 29.0, 521.0,+ 319.0, 639.0, 241.0, 406.0, 44.0, 191.0,+ 321.0, 0.66666, 451.0, 179.0, 382.0, 71.0,+ 489.0, 345.0, 297.0, 82.0, 168.0, 26.0,+ 0.43333, 130.0, -10.0, 183.0, 650.0, 5.76666,+ 0.13333, 84.0, 93.0, 123.0, 152.0, 102.0,+ 134.0, 148.0, 450.0, 900.0, -300.0, 264.0,+ 369.0, 634.0, 49.0, 390.0, 365.0, 540.0,+ 410.0, 48.0, 262.0, 156.0, 0.015, 190.0,+ 750.0, 18.0, 170.0, 172.0, 187.0, 194.0,+ 203.0, 206.0, 36.0, 38.0, 2.4,+};+const uint16_t atk_code[4608] = {+ 302, 0, 0, 305, 0, 1, 310, 0, 0, 283, 0, 2, 0, 0,+ 283, 3, 0, 0, 0, 1068, 0, 4, 5, 6, 7, 550, 0, 8,+ 9, 293, 0, 0, 303, 0, 2, 305, 0, 10, 0, 0, 296, 11,+ 2, 303, 12, 0, 305, 0, 13, 1057, 0, 2, 14, 15, 2, 293,+ 16, 0, 303, 0, 1, 0, 0, 0, 17, 1056, 17, 18, 19, 20,+ 21, 302, 22, 0, 2082, 0, 2, 0, 0, 23, 24, 0, 25, 11,+ 2082, 0, 2, 0, 0, 26, 27, 28, 25, 11, 2082, 0, 2, 29,+ 30, 31, 32, 33, 25, 11, 2082, 0, 2, 29, 30, 34, 35, 36,+ 25, 11, 2082, 37, 2, 0, 0, 23, 38, 39, 25, 11, 2082, 0,+ 2, 29, 0, 31, 38, 40, 25, 11, 2082, 0, 2, 29, 30, 31,+ 41, 42, 25, 11, 2082, 0, 2, 0, 30, 23, 41, 43, 25, 11,+ 2082, 37, 2, 0, 0, 23, 24, 0, 25, 11, 2082, 0, 2, 0,+ 0, 26, 27, 28, 25, 11, 2082, 0, 2, 29, 30, 31, 32, 33,+ 25, 11, 2082, 0, 2, 29, 30, 34, 35, 36, 25, 11, 2082, 16,+ 44, 0, 45, 46, 47, 0, 48, 12, 2082, 0, 44, 29, 45, 49,+ 47, 33, 48, 12, 305, 1, 0, 310, 0, 2, 26, 0, 1323, 0,+ 50, 51, 52, 7, 2, 550, 0, 8, 53, 293, 0, 2, 24, 0,+ 1823, 54, 55, 56, 57, 0, 33, 58, 59, 1823, 0, 55, 32, 18,+ 0, 33, 58, 59, 1823, 0, 60, 56, 57, 44, 33, 58, 59, 1823,+ 0, 60, 32, 18, 44, 33, 58, 59, 26, 61, 1323, 0, 50, 51,+ 52, 7, 2, 550, 0, 8, 53, 293, 0, 2, 24, 0, 1565, 54,+ 60, 62, 63, 44, 64, 2, 1565, 65, 60, 32, 18, 44, 64, 0,+ 1565, 12, 60, 62, 63, 44, 64, 2, 1565, 65, 60, 32, 18, 44,+ 64, 0, 1565, 12, 60, 62, 63, 44, 64, 2, 1565, 65, 60, 32,+ 18, 44, 64, 0, 1565, 66, 55, 32, 18, 0, 64, 0, 1565, 17,+ 55, 62, 63, 0, 64, 2, 1565, 67, 55, 32, 18, 0, 64, 0,+ 1565, 17, 55, 62, 63, 0, 64, 2, 1565, 67, 55, 32, 18, 0,+ 64, 0, 1565, 17, 55, 62, 63, 0, 64, 2, 26, 68, 1323, 0,+ 50, 51, 52, 7, 2, 550, 0, 8, 53, 293, 0, 2, 24, 0,+ 513, 0, 2, 0, 0, 0, 789, 0, 69, 32769, 70, 523, 0, 44,+ 13, 772, 0, 1, 32770, 1, 770, 0, 1, 32771, 2, 271, 0, 32771,+ 513, 0, 13, 18, 770, 0, 2, 32769, 71, 271, 0, 71, 513, 0,+ 13, 72, 770, 0, 2, 32769, 73, 271, 0, 74, 513, 0, 13, 75,+ 770, 0, 2, 32769, 76, 271, 0, 1, 513, 0, 13, 77, 770, 0,+ 2, 32769, 21, 523, 0, 10, 1, 771, 0, 10, 32773, 2, 772, 0,+ 10, 32773, 44, 787, 0, 78, 32772, 75, 513, 0, 10, 0, 0, 0,+ 771, 0, 74, 79, 32772, 771, 0, 80, 81, 32772, 770, 0, 58, 32769,+ 82, 787, 0, 83, 32772, 77, 513, 0, 10, 84, 0, 0, 770, 0,+ 71, 58, 32773, 772, 0, 85, 32776, 32777, 771, 0, 85, 8, 32778, 772,+ 0, 71, 32777, 72, 786, 0, 86, 32772, 77, 772, 0, 10, 32773, 84,+ 0, 0, 770, 0, 73, 58, 32773, 772, 0, 87, 32776, 32779, 770, 0,+ 87, 8, 32780, 772, 0, 73, 32779, 72, 1309, 0, 32778, 56, 32774, 0,+ 32777, 1309, 0, 32778, 32775, 32772, 0, 32777, 1309, 0, 32780, 56, 32774, 44,+ 32779, 1309, 0, 32780, 32775, 32772, 44, 32779, 772, 0, 1, 32770, 44, 770,+ 0, 1, 32771, 2, 271, 0, 32771, 770, 0, 2, 32769, 88, 271, 0,+ 74, 770, 0, 2, 32769, 89, 271, 0, 13, 770, 0, 2, 32769, 76,+ 271, 0, 44, 770, 0, 2, 32769, 21, 270, 0, 74, 0, 0, 26,+ 80, 1323, 0, 50, 51, 52, 7, 2, 550, 0, 8, 53, 293, 0,+ 2, 24, 0, 1315, 0, 88, 90, 69, 0, 59, 1823, 17, 50, 91,+ 75, 0, 59, 86, 88, 1315, 92, 93, 90, 69, 0, 70, 1315, 94,+ 93, 90, 69, 0, 33, 1309, 2, 50, 56, 39, 0, 95, 1309, 0,+ 96, 56, 39, 0, 95, 1309, 0, 97, 56, 39, 0, 95, 1309, 98,+ 50, 56, 57, 0, 36, 26, 94, 1323, 0, 50, 51, 52, 7, 2,+ 550, 0, 8, 53, 293, 0, 2, 24, 0, 1315, 0, 29, 90, 99,+ 44, 70, 1823, 17, 52, 91, 75, 44, 59, 100, 88, 1315, 17, 29,+ 101, 99, 44, 70, 1315, 12, 29, 90, 99, 44, 70, 1309, 17, 52,+ 102, 103, 44, 70, 1315, 17, 29, 101, 104, 44, 77, 1315, 105, 29,+ 106, 99, 44, 70, 1315, 16, 29, 107, 99, 44, 70, 1309, 108, 52,+ 56, 39, 44, 70, 1315, 17, 29, 102, 99, 44, 70, 1309, 108, 52,+ 56, 109, 44, 70, 1309, 16, 52, 56, 110, 44, 70, 1309, 111, 50,+ 56, 57, 0, 28, 1309, 16, 52, 112, 113, 44, 45, 26, 114, 1323,+ 0, 50, 51, 52, 7, 2, 550, 0, 8, 53, 293, 0, 2, 24,+ 0, 1828, 0, 115, 90, 116, 44, 59, 10, 117, 1828, 0, 118, 47,+ 119, 0, 59, 13, 120, 513, 0, 2, 121, 528, 0, 122, 32769, 771,+ 0, 2, 32769, 2, 523, 0, 44, 1, 770, 0, 44, 32770, 2, 271,+ 0, 32770, 271, 0, 1, 271, 0, 13, 271, 0, 10, 1309, 0, 123,+ 56, 39, 44, 59, 270, 12, 58, 1309, 0, 124, 47, 75, 0, 59,+ 270, 12, 58, 1309, 0, 123, 125, 110, 44, 59, 270, 12, 58, 26,+ 0, 1323, 0, 126, 127, 128, 7, 2, 550, 0, 8, 53, 293, 0,+ 2, 24, 0, 1571, 0, 129, 107, 86, 0, 28, 2, 550, 0, 130,+ 131, 1823, 0, 132, 32, 75, 0, 77, 77, 88, 1823, 0, 133, 134,+ 75, 1, 28, 73, 135, 1823, 0, 136, 137, 75, 2, 59, 138, 57,+ 1823, 0, 139, 140, 75, 1, 28, 73, 135, 26, 141, 1323, 0, 50,+ 51, 52, 7, 2, 550, 0, 8, 53, 293, 0, 2, 24, 0, 1828,+ 0, 142, 90, 99, 44, 59, 58, 143, 1828, 0, 144, 47, 99, 0,+ 59, 58, 145, 513, 0, 2, 10, 771, 0, 2, 32769, 2, 523, 0,+ 44, 1, 772, 0, 44, 32770, 75, 770, 0, 44, 32770, 146, 2082, 0,+ 0, 0, 0, 147, 32770, 0, 148, 149, 523, 37, 44, 1, 772, 0,+ 44, 32770, 75, 770, 0, 44, 32770, 146, 2082, 0, 0, 29, 0, 150,+ 32770, 33, 148, 149, 529, 37, 58, 32769, 26, 0, 1323, 0, 126, 127,+ 128, 7, 2, 550, 0, 8, 53, 293, 0, 2, 24, 0, 1571, 0,+ 129, 107, 104, 0, 28, 2, 550, 0, 130, 131, 1823, 0, 132, 32,+ 75, 0, 77, 77, 88, 1823, 0, 133, 151, 75, 1, 28, 87, 152,+ 1823, 0, 136, 153, 75, 2, 59, 73, 28, 1823, 0, 139, 151, 75,+ 1, 28, 87, 152, 26, 141, 1323, 0, 50, 51, 52, 7, 2, 550,+ 0, 8, 53, 293, 0, 2, 24, 0, 1823, 17, 55, 56, 57, 0,+ 95, 58, 50, 1823, 0, 55, 32, 18, 0, 95, 58, 50, 1823, 0,+ 60, 56, 57, 44, 95, 58, 50, 1823, 0, 60, 32, 18, 44, 95,+ 58, 50, 26, 74, 1323, 0, 50, 51, 52, 7, 2, 550, 0, 8,+ 53, 293, 0, 2, 24, 0, 1823, 12, 55, 32, 18, 0, 59, 58,+ 154, 1823, 0, 115, 56, 155, 44, 59, 58, 154, 26, 156, 1323, 0,+ 50, 51, 52, 7, 2, 550, 0, 8, 53, 293, 0, 2, 24, 0,+ 1828, 0, 142, 90, 99, 44, 59, 58, 143, 1828, 0, 144, 47, 99,+ 0, 59, 58, 145, 513, 0, 2, 74, 771, 0, 2, 32769, 2, 523,+ 0, 44, 1, 772, 0, 44, 32770, 75, 770, 0, 44, 32770, 146, 2082,+ 0, 0, 0, 0, 147, 32770, 0, 148, 149, 523, 16, 44, 1, 772,+ 0, 44, 32770, 75, 770, 0, 44, 32770, 146, 2082, 0, 0, 29, 0,+ 150, 32770, 33, 148, 149, 529, 16, 58, 32769, 26, 0, 1323, 0, 50,+ 51, 52, 7, 2, 550, 0, 8, 53, 293, 0, 2, 24, 0, 26,+ 108, 1323, 0, 50, 51, 52, 7, 2, 550, 0, 8, 9, 293, 0,+ 2, 24, 0, 513, 0, 2, 10, 0, 0, 283, 0, 2, 0, 0,+ 283, 17, 0, 0, 0, 528, 0, 157, 32769, 771, 0, 2, 32769, 2,+ 523, 0, 44, 10, 770, 0, 44, 32770, 2, 271, 0, 32770, 270, 0,+ 158, 270, 0, 72, 270, 0, 86, 270, 0, 159, 270, 0, 160, 0,+ 0, 1068, 0, 116, 112, 161, 7, 293, 0, 2, 550, 0, 8, 53,+ 1823, 0, 55, 162, 39, 0, 45, 13, 121, 1309, 0, 163, 62, 63,+ 0, 45, 1823, 0, 164, 162, 39, 44, 45, 13, 121, 1309, 0, 165,+ 62, 63, 44, 45, 270, 37, 74, 0, 0, 1068, 0, 116, 112, 161,+ 7, 293, 0, 2, 550, 0, 8, 53, 1565, 0, 55, 62, 63, 0,+ 45, 2, 1565, 0, 166, 32, 18, 0, 45, 0, 1565, 0, 167, 62,+ 63, 0, 45, 0, 1565, 0, 164, 62, 63, 44, 45, 2, 1565, 0,+ 168, 32, 18, 44, 45, 0, 1565, 0, 169, 62, 63, 44, 45, 0,+ 270, 22, 74, 0, 0, 1068, 0, 170, 112, 171, 7, 293, 0, 2,+ 550, 0, 8, 53, 513, 0, 1, 0, 513, 0, 13, 0, 0, 0,+ 523, 0, 10, 1, 770, 0, 74, 32773, 44, 772, 0, 74, 32774, 85,+ 513, 0, 80, 0, 528, 0, 166, 32772, 523, 0, 80, 1, 771, 0,+ 80, 32775, 2, 772, 0, 80, 32775, 44, 0, 0, 771, 0, 58, 172,+ 32774, 771, 0, 71, 81, 32774, 772, 0, 85, 32772, 122, 770, 0, 85,+ 32778, 21, 770, 0, 85, 32778, 32771, 770, 0, 73, 74, 32775, 772, 0,+ 87, 32778, 32779, 771, 0, 87, 8, 32780, 772, 0, 73, 32779, 72, 772,+ 0, 80, 32775, 84, 770, 0, 138, 74, 32775, 772, 0, 173, 32778, 32781,+ 770, 0, 173, 8, 32782, 772, 0, 138, 32781, 72, 1309, 0, 32780, 174,+ 32776, 0, 32779, 1309, 0, 32780, 32777, 32774, 0, 32779, 1309, 0, 32782, 174,+ 32776, 44, 32781, 1309, 0, 32782, 32777, 32774, 44, 32781, 772, 0, 88, 32773,+ 10, 770, 0, 1, 32771, 32783, 770, 0, 13, 32772, 2, 788, 0, 175,+ 32772, 13, 270, 176, 74, 0, 0, 1068, 0, 170, 112, 171, 7, 293,+ 0, 2, 550, 0, 8, 53, 1823, 0, 177, 174, 103, 0, 70, 1,+ 124, 1823, 0, 177, 178, 88, 0, 70, 1, 124, 1823, 0, 179, 174,+ 103, 44, 70, 1, 124, 1823, 0, 179, 178, 88, 44, 70, 1, 124,+ 270, 94, 74, 0, 0, 1068, 0, 116, 112, 161, 7, 293, 0, 2,+ 523, 0, 121, 44, 528, 0, 180, 32784, 550, 0, 181, 53, 1823, 0,+ 177, 137, 109, 0, 20, 73, 167, 1823, 0, 182, 178, 88, 0, 20,+ 85, 167, 270, 111, 74, 0, 0, 550, 0, 183, 53, 1823, 0, 179,+ 137, 109, 44, 20, 73, 167, 1823, 0, 184, 178, 88, 44, 20, 85,+ 167, 270, 111, 74, 0, 0, 1068, 0, 185, 51, 186, 7, 26, 0,+ 1323, 0, 116, 187, 161, 7, 2, 550, 0, 8, 9, 293, 0, 0,+ 24, 0, 513, 12, 2, 88, 771, 0, 2, 32769, 2, 523, 0, 44,+ 20, 521, 0, 1, 32770, 520, 0, 13, 32770, 772, 0, 10, 32771, 177,+ 772, 0, 74, 32772, 177, 772, 0, 1, 32771, 188, 772, 0, 13, 32772,+ 64, 557, 0, 80, 58, 770, 0, 10, 32773, 32775, 770, 0, 74, 32774,+ 32776, 770, 0, 1, 32771, 32775, 770, 0, 13, 32772, 32776, 789, 0, 158,+ 32773, 189, 513, 0, 10, 189, 791, 0, 190, 32773, 191, 513, 0, 10,+ 191, 789, 0, 21, 32774, 75, 513, 0, 74, 75, 791, 0, 192, 32774,+ 179, 513, 0, 74, 179, 1290, 0, 44, 32773, 32774, 32775, 32776, 2082, 0,+ 0, 32771, 32772, 32773, 32774, 32770, 193, 194, 529, 195, 74, 32769, 26, 0,+ 550, 0, 8, 9, 513, 0, 2, 10, 0, 0, 283, 0, 2, 0,+ 0, 283, 17, 0, 0, 0, 528, 0, 154, 32769, 771, 0, 2, 32769,+ 2, 523, 0, 44, 13, 770, 0, 44, 32770, 2, 271, 0, 32770, 270,+ 0, 89, 270, 0, 185, 270, 0, 189, 270, 0, 196, 0, 0, 1068,+ 0, 116, 112, 161, 7, 293, 0, 2, 550, 0, 197, 9, 1315, 0,+ 198, 199, 86, 0, 0, 1315, 0, 198, 200, 86, 0, 0, 1823, 0,+ 116, 201, 72, 44, 0, 21, 121, 523, 0, 1, 44, 528, 0, 202,+ 32771, 1309, 0, 203, 120, 110, 44, 45, 1309, 0, 2, 204, 152, 0,+ 45, 270, 111, 1, 0, 0, 1309, 0, 96, 120, 110, 0, 45, 1309,+ 0, 205, 204, 152, 44, 45, 270, 111, 1, 0, 0, 1068, 0, 206,+ 5, 207, 7, 293, 0, 0, 550, 0, 8, 9, 523, 0, 13, 44,+ 528, 0, 208, 32772, 2082, 0, 2, 209, 47, 209, 47, 0, 105, 11,+ 2082, 0, 2, 210, 27, 210, 27, 28, 105, 11, 2082, 0, 2, 31,+ 47, 31, 47, 33, 105, 11, 2082, 0, 2, 210, 35, 210, 35, 36,+ 105, 11, 270, 92, 1, 0, 0, 2082, 0, 2, 209, 38, 209, 38,+ 39, 211, 11, 2082, 0, 2, 212, 38, 212, 38, 40, 211, 11, 2082,+ 0, 2, 212, 41, 212, 41, 42, 211, 11, 2082, 0, 2, 209, 41,+ 209, 41, 43, 211, 11, 270, 92, 1, 0, 0, 1068, 0, 213, 5,+ 6, 7, 293, 0, 0, 523, 0, 1, 44, 528, 0, 100, 32771, 550,+ 0, 214, 9, 1056, 0, 121, 144, 64, 109, 270, 94, 1, 0, 0,+ 550, 0, 134, 9, 1056, 0, 121, 18, 64, 109, 270, 94, 1, 0,+ 0, 1068, 0, 116, 112, 161, 7, 293, 0, 2, 523, 0, 1, 44,+ 528, 0, 215, 32771, 550, 0, 216, 53, 1823, 0, 217, 201, 18, 0,+ 59, 74, 154, 1823, 0, 218, 120, 219, 44, 59, 74, 154, 270, 176,+ 1, 0, 0, 550, 0, 220, 53, 1823, 0, 218, 201, 18, 44, 59,+ 74, 154, 1823, 0, 217, 120, 219, 0, 59, 74, 154, 270, 176, 1,+ 0, 0, 1068, 0, 185, 51, 186, 7, 26, 0, 1323, 0, 206, 5,+ 207, 7, 2, 550, 0, 8, 9, 293, 0, 0, 24, 0, 513, 0,+ 2, 71, 528, 0, 221, 32769, 771, 0, 2, 32769, 2, 523, 0, 44,+ 13, 770, 0, 1, 32770, 2, 271, 0, 32771, 271, 0, 13, 271, 0,+ 10, 271, 0, 74, 271, 0, 80, 303, 0, 1, 271, 0, 80, 303,+ 0, 2, 271, 0, 10, 303, 0, 44, 271, 0, 1, 303, 0, 0,+ 271, 0, 2, 296, 11, 32770, 1057, 54, 32770, 21, 17, 67, 270, 105,+ 74, 26, 0, 1323, 0, 206, 5, 207, 7, 2, 550, 0, 8, 9,+ 293, 0, 0, 24, 0, 513, 0, 2, 71, 528, 0, 221, 32769, 771,+ 0, 2, 32769, 2, 523, 0, 44, 13, 770, 0, 1, 32770, 2, 271,+ 0, 32771, 271, 0, 13, 271, 0, 10, 271, 0, 74, 271, 0, 80,+ 303, 0, 1, 271, 0, 80, 303, 0, 2, 271, 0, 10, 303, 0,+ 44, 271, 0, 1, 303, 0, 0, 271, 0, 2, 296, 11, 32770, 1057,+ 54, 32770, 21, 108, 54, 270, 222, 74, 26, 0, 1323, 0, 116, 187,+ 161, 7, 2, 550, 0, 8, 9, 293, 0, 0, 24, 0, 513, 17,+ 2, 87, 771, 0, 2, 32769, 2, 523, 0, 44, 20, 521, 0, 1,+ 32770, 520, 0, 13, 32770, 772, 0, 10, 32771, 177, 772, 0, 74, 32772,+ 177, 772, 0, 1, 32771, 188, 772, 0, 13, 32772, 64, 557, 0, 80,+ 58, 770, 0, 10, 32773, 32775, 770, 0, 74, 32774, 32776, 770, 0, 1,+ 32771, 32775, 770, 0, 13, 32772, 32776, 789, 0, 158, 32773, 189, 513, 0,+ 10, 189, 791, 0, 190, 32773, 191, 513, 0, 10, 191, 789, 0, 21,+ 32774, 75, 513, 0, 74, 75, 791, 0, 192, 32774, 179, 513, 0, 74,+ 179, 1290, 0, 44, 32773, 32774, 32775, 32776, 2082, 0, 2, 32771, 32772, 32773,+ 32774, 32770, 211, 194, 529, 211, 74, 32769, 26, 0, 1323, 0, 206, 5,+ 207, 7, 2, 550, 0, 8, 9, 293, 0, 0, 24, 0, 1822, 54,+ 223, 224, 177, 2, 64, 80, 225, 1822, 0, 197, 226, 177, 1, 64,+ 80, 225, 26, 227, 1323, 0, 4, 5, 6, 7, 2, 550, 0, 8,+ 9, 293, 0, 0, 24, 0, 513, 0, 2, 10, 0, 0, 283, 0,+ 2, 0, 0, 283, 228, 0, 0, 0, 528, 0, 27, 32769, 771, 0,+ 2, 32769, 2, 523, 0, 44, 71, 770, 0, 44, 32770, 2, 271, 0,+ 32770, 270, 0, 21, 270, 0, 83, 270, 0, 39, 270, 0, 229, 270,+ 0, 230, 270, 0, 155, 270, 0, 231, 270, 0, 143, 270, 0, 232,+ 0, 0, 1068, 0, 116, 112, 161, 7, 293, 0, 2, 550, 0, 8,+ 53, 1823, 0, 55, 162, 39, 0, 45, 13, 121, 1309, 0, 163, 62,+ 63, 0, 45, 1823, 0, 164, 162, 39, 44, 45, 13, 121, 1309, 0,+ 165, 62, 63, 44, 45, 270, 37, 74, 0, 0, 1068, 0, 116, 112,+ 161, 7, 293, 0, 2, 550, 0, 8, 53, 1565, 0, 55, 62, 63,+ 0, 45, 2, 1565, 0, 166, 32, 18, 0, 45, 0, 1565, 0, 167,+ 62, 63, 0, 45, 0, 1565, 0, 164, 62, 63, 44, 45, 2, 1565,+ 0, 168, 32, 18, 44, 45, 0, 1565, 0, 169, 62, 63, 44, 45,+ 0, 270, 22, 74, 0, 0, 1068, 0, 170, 112, 171, 7, 293, 0,+ 2, 550, 0, 8, 53, 513, 0, 1, 0, 513, 0, 13, 0, 0,+ 0, 523, 0, 10, 1, 770, 0, 74, 32773, 44, 772, 0, 74, 32774,+ 85, 513, 0, 80, 0, 528, 0, 77, 32772, 523, 0, 80, 1, 771,+ 0, 80, 32775, 2, 772, 0, 80, 32775, 44, 0, 0, 771, 0, 58,+ 172, 32774, 771, 0, 71, 81, 32774, 772, 0, 85, 32772, 122, 770, 0,+ 85, 32778, 21, 770, 0, 85, 32778, 32771, 770, 0, 73, 74, 32775, 772,+ 0, 87, 32778, 32779, 771, 0, 87, 8, 32780, 772, 0, 73, 32779, 72,+ 772, 0, 80, 32775, 84, 770, 0, 138, 74, 32775, 772, 0, 173, 32778,+ 32781, 770, 0, 173, 8, 32782, 772, 0, 138, 32781, 72, 1309, 0, 32780,+ 174, 32776, 0, 32779, 1309, 0, 32780, 32777, 32774, 0, 32779, 1309, 0, 32782,+ 174, 32776, 44, 32781, 1309, 0, 32782, 32777, 32774, 44, 32781, 772, 0, 88,+ 32773, 10, 770, 0, 1, 32771, 32783, 770, 0, 13, 32772, 2, 788, 0,+ 99, 32772, 13, 270, 176, 74, 0, 0, 1068, 0, 170, 112, 171, 7,+ 293, 0, 2, 550, 0, 8, 53, 1823, 0, 177, 174, 103, 0, 70,+ 1, 124, 1823, 0, 177, 178, 88, 0, 70, 1, 124, 1823, 0, 179,+ 174, 103, 44, 70, 1, 124, 1823, 0, 179, 178, 88, 44, 70, 1,+ 124, 270, 94, 74, 0, 0, 1068, 0, 116, 112, 161, 7, 293, 0,+ 2, 523, 0, 121, 44, 528, 0, 233, 32784, 550, 0, 181, 53, 1823,+ 0, 177, 137, 109, 0, 20, 73, 167, 1823, 0, 182, 178, 88, 0,+ 20, 85, 167, 270, 111, 74, 0, 0, 550, 0, 183, 53, 1823, 0,+ 179, 137, 109, 44, 20, 73, 167, 1823, 0, 184, 178, 88, 44, 20,+ 85, 167, 270, 111, 74, 0, 0, 1068, 0, 116, 112, 161, 7, 293,+ 0, 2, 550, 0, 197, 9, 1315, 0, 198, 199, 86, 0, 0, 1315,+ 0, 198, 200, 86, 0, 0, 1823, 0, 116, 201, 72, 44, 0, 21,+ 121, 523, 0, 121, 44, 528, 0, 96, 32784, 1309, 0, 203, 120, 110,+ 44, 45, 1309, 0, 2, 204, 152, 0, 45, 270, 111, 74, 0, 0,+ 1309, 0, 96, 120, 110, 0, 45, 1309, 0, 205, 204, 152, 44, 45,+ 270, 111, 74, 0, 0, 1068, 0, 206, 5, 207, 7, 293, 0, 0,+ 550, 0, 8, 9, 523, 0, 89, 44, 528, 0, 234, 32785, 2082, 0,+ 2, 209, 47, 209, 47, 0, 105, 11, 2082, 0, 2, 210, 27, 210,+ 27, 28, 105, 11, 2082, 0, 2, 31, 47, 31, 47, 33, 105, 11,+ 2082, 0, 2, 210, 35, 210, 35, 36, 105, 11, 270, 92, 74, 0,+ 0, 2082, 0, 2, 209, 38, 209, 38, 39, 211, 11, 2082, 0, 2,+ 212, 38, 212, 38, 40, 211, 11, 2082, 0, 2, 212, 41, 212, 41,+ 42, 211, 11, 2082, 0, 2, 209, 41, 209, 41, 43, 211, 11, 270,+ 92, 74, 0, 0, 1068, 0, 213, 5, 6, 7, 293, 0, 0, 523,+ 0, 121, 44, 528, 0, 235, 32784, 550, 0, 214, 9, 1056, 0, 121,+ 144, 64, 109, 270, 94, 74, 0, 0, 550, 0, 134, 9, 1056, 0,+ 121, 18, 64, 109, 270, 94, 74, 0, 0, 1068, 0, 116, 112, 161,+ 7, 293, 0, 2, 523, 0, 121, 44, 528, 0, 119, 32784, 550, 0,+ 216, 53, 1823, 0, 217, 201, 18, 0, 59, 74, 154, 1823, 0, 218,+ 120, 219, 44, 59, 74, 154, 270, 176, 74, 0, 0, 550, 0, 220,+ 53, 1823, 0, 218, 201, 18, 44, 59, 74, 154, 1823, 0, 217, 120,+ 219, 0, 59, 74, 154, 270, 176, 74, 0, 0, 1068, 0, 185, 51,+ 186, 7, 26, 0, 1323, 0, 206, 5, 207, 7, 2, 550, 0, 8,+ 9, 293, 0, 0, 24, 0, 513, 0, 2, 71, 528, 0, 221, 32769,+ 771, 0, 2, 32769, 2, 523, 0, 44, 13, 770, 0, 1, 32770, 2,+ 271, 0, 32771, 271, 0, 13, 271, 0, 10, 271, 0, 74, 271, 0,+ 80, 303, 0, 1, 271, 0, 80, 303, 0, 2, 271, 0, 10, 303,+ 0, 44, 271, 0, 1, 303, 0, 0, 271, 0, 2, 296, 11, 32770,+ 1057, 54, 32770, 202, 17, 0, 270, 65, 74, 26, 0, 1323, 0, 206,+ 5, 207, 7, 2, 550, 0, 8, 9, 293, 0, 0, 24, 0, 306,+ 0, 0, 513, 0, 2, 0, 523, 108, 44, 13, 770, 0, 1, 32770,+ 2, 271, 0, 32771, 271, 0, 13, 271, 0, 10, 271, 0, 74, 271,+ 0, 80, 303, 0, 1, 271, 0, 80, 303, 0, 2, 271, 0, 10,+ 303, 0, 44, 271, 0, 1, 303, 0, 0, 271, 0, 2, 296, 11,+ 32770, 1057, 54, 32770, 202, 17, 0, 770, 0, 2, 32769, 2, 788, 0,+ 80, 32769, 13, 1822, 0, 223, 224, 177, 2, 64, 1, 225, 1822, 0,+ 197, 226, 177, 1, 64, 1, 225, 293, 12, 0, 303, 44, 44, 296,+ 54, 44, 303, 108, 1, 295, 54, 236, 296, 0, 0, 1068, 0, 206,+ 5, 31, 7, 298, 0, 237, 1323, 0, 206, 5, 226, 7, 0, 295,+ 67, 238, 302, 0, 2, 52, 0, 1323, 0, 224, 5, 226, 7, 0,+ 298, 108, 72, 1323, 0, 224, 239, 226, 240, 0, 557, 37, 13, 10,+ 550, 0, 75, 32773, 295, 0, 0, 296, 0, 0, 773, 0, 74, 33,+ 32768, 513, 0, 2, 0, 773, 0, 80, 32769, 44, 772, 0, 80, 32775,+ 32774, 520, 0, 58, 32775, 772, 0, 58, 32776, 21, 519, 0, 58, 32776,+ 772, 0, 71, 32769, 77, 770, 0, 71, 32777, 241, 513, 0, 85, 36,+ 770, 0, 73, 72, 32776, 1309, 0, 32777, 32778, 32779, 44, 237, 770, 0,+ 85, 32778, 32779, 770, 0, 85, 32778, 83, 771, 0, 73, 201, 32778, 1309,+ 0, 32777, 32778, 32779, 44, 237, 770, 0, 2, 32769, 2, 788, 0, 242,+ 32769, 208, 770, 0, 71, 32777, 20, 1823, 0, 32777, 36, 189, 44, 237,+ 1, 88, 770, 0, 71, 32777, 197, 1823, 0, 32777, 199, 189, 44, 237,+ 1, 88, 770, 0, 71, 32777, 64, 1823, 0, 32777, 36, 189, 44, 237,+ 1, 88, 770, 0, 71, 32777, 64, 1823, 0, 32777, 199, 189, 44, 237,+ 1, 88, 770, 0, 71, 32777, 36, 1823, 0, 32777, 36, 189, 44, 237,+ 1, 88, 770, 0, 71, 32777, 36, 1823, 0, 32777, 199, 189, 44, 237,+ 1, 88, 770, 0, 71, 32777, 45, 1823, 0, 32777, 36, 189, 44, 237,+ 1, 88, 770, 0, 71, 32777, 197, 1823, 0, 32777, 199, 189, 44, 237,+ 1, 88, 770, 0, 71, 32777, 36, 1823, 0, 32777, 36, 189, 44, 237,+ 1, 88, 770, 0, 71, 32777, 243, 513, 0, 2, 0, 772, 0, 87,+ 32769, 72, 770, 0, 87, 32780, 32777, 770, 0, 73, 85, 32769, 1309, 0,+ 32780, 36, 32779, 44, 237, 771, 0, 85, 244, 32779, 1309, 0, 32780, 32778,+ 32779, 44, 237, 770, 0, 2, 32769, 2, 788, 0, 135, 32769, 167, 513,+ 0, 2, 0, 295, 58, 197, 296, 0, 0, 298, 0, 245, 1323, 0,+ 224, 239, 246, 240, 0, 53, 37, 305, 0, 0, 304, 0, 0, 303,+ 0, 44, 1057, 54, 0, 189, 17, 2, 283, 37, 2, 0, 0, 283,+ 17, 0, 0, 0, 1068, 0, 4, 5, 6, 7, 550, 0, 8, 53,+ 293, 0, 2, 302, 0, 44, 1057, 194, 2, 247, 105, 2, 1057, 0,+ 1, 247, 105, 2, 283, 37, 2, 0, 0, 283, 149, 0, 0, 0,+ 550, 0, 248, 45, 296, 0, 1, 1057, 194, 44, 247, 105, 2, 1057,+ 0, 1, 247, 105, 2, 283, 37, 2, 0, 0, 283, 149, 0, 0,+ 0, 550, 0, 7, 53, 296, 0, 0, 1057, 194, 0, 247, 105, 2,+ 1057, 0, 2, 247, 105, 2, 283, 37, 2, 0, 0, 283, 149, 0,+ 0, 0, 550, 0, 248, 45, 296, 0, 44, 307, 0, 8, 1057, 194,+ 44, 247, 105, 2, 293, 16, 0, 513, 0, 138, 0, 513, 0, 173,+ 2, 772, 0, 80, 32781, 224, 521, 0, 71, 32775, 520, 0, 85, 32775,+ 772, 0, 88, 32777, 70, 772, 0, 121, 32778, 70, 772, 0, 71, 32783,+ 1, 772, 0, 85, 32784, 1, 770, 0, 71, 32777, 8, 770, 0, 85,+ 32778, 47, 770, 0, 88, 32783, 8, 770, 0, 121, 32784, 47, 770, 0,+ 80, 32775, 33, 2082, 0, 0, 32777, 32778, 32783, 32784, 32775, 12, 0, 770,+ 0, 138, 32781, 32782, 789, 0, 249, 32782, 94, 770, 0, 173, 32782, 250,+ 788, 3, 143, 32781, 251, 305, 2, 10, 295, 0, 252, 303, 0, 1,+ 297, 0, 2, 513, 0, 2, 0, 513, 0, 44, 0, 513, 0, 89,+ 44, 513, 0, 253, 228, 513, 0, 76, 228, 787, 0, 220, 32770, 32785,+ 771, 0, 44, 32770, 44, 789, 0, 254, 32770, 0, 770, 0, 44, 32770,+ 13, 788, 0, 255, 32770, 13, 771, 0, 44, 32770, 13, 772, 0, 1,+ 32770, 44, 770, 0, 1, 32771, 2, 271, 0, 32771, 303, 0, 1, 271,+ 0, 74, 303, 0, 2, 271, 0, 13, 303, 0, 44, 271, 0, 44,+ 303, 0, 0, 296, 32786, 32770, 513, 32787, 89, 32770, 774, 0, 18, 32769,+ 44, 528, 0, 256, 32788, 523, 0, 44, 13, 787, 0, 209, 32769, 158,+ 295, 0, 30, 513, 0, 253, 54, 513, 0, 76, 54, 787, 0, 257,+ 32769, 21, 305, 0, 0, 306, 0, 2, 787, 0, 177, 32769, 185, 305,+ 0, 74, 306, 0, 44, 295, 0, 197, 513, 0, 253, 12, 513, 0,+ 76, 22, 787, 0, 258, 32769, 185, 786, 0, 187, 32770, 2, 786, 0,+ 187, 32770, 32785, 787, 0, 259, 32769, 110, 295, 0, 45, 513, 0, 44,+ 1, 787, 0, 95, 32769, 260, 305, 0, 80, 306, 0, 1, 295, 0,+ 77, 770, 0, 2, 32769, 2, 788, 0, 27, 32769, 261, 302, 111, 1,+ 26, 262,+};+const uint16_t atk_line[971] = {+ 0, 3, 6, 9, 12, 14, 17, 19, 25, 29, 32, 35, 38, 40,+ 43, 46, 49, 55, 58, 61, 63, 65, 71, 74, 84, 94, 104, 114,+ 124, 134, 144, 154, 164, 174, 184, 194, 204, 214, 217, 220, 222, 229,+ 233, 236, 238, 247, 256, 265, 274, 276, 283, 287, 290, 292, 300, 308,+ 316, 324, 332, 340, 348, 356, 364, 372, 380, 388, 390, 397, 401, 404,+ 406, 410, 412, 417, 421, 426, 431, 434, 438, 443, 446, 450, 455, 458,+ 462, 467, 470, 474, 479, 483, 488, 493, 498, 502, 504, 509, 514, 519,+ 524, 528, 530, 535, 540, 545, 550, 555, 560, 562, 567, 572, 577, 582,+ 589, 596, 603, 610, 615, 620, 623, 628, 631, 636, 639, 644, 647, 652,+ 655, 657, 659, 666, 670, 673, 675, 682, 691, 698, 705, 712, 719, 726,+ 733, 735, 742, 746, 749, 751, 758, 767, 774, 781, 788, 795, 802, 809,+ 816, 823, 830, 837, 844, 851, 853, 860, 864, 867, 869, 878, 887, 891,+ 895, 900, 904, 909, 912, 915, 918, 921, 928, 931, 938, 941, 948, 951,+ 953, 960, 964, 967, 969, 977, 981, 990, 999, 1008, 1017, 1019, 1026, 1030,+ 1033, 1035, 1044, 1053, 1057, 1062, 1066, 1071, 1076, 1086, 1090, 1095, 1100, 1110,+ 1114, 1116, 1123, 1127, 1130, 1132, 1140, 1144, 1153, 1162, 1171, 1180, 1182, 1189,+ 1193, 1196, 1198, 1207, 1216, 1225, 1234, 1236, 1243, 1247, 1250, 1252, 1261, 1270,+ 1272, 1279, 1283, 1286, 1288, 1297, 1306, 1310, 1315, 1319, 1324, 1329, 1339, 1343,+ 1348, 1353, 1363, 1367, 1369, 1376, 1380, 1383, 1385, 1387, 1394, 1398, 1401, 1403,+ 1407, 1409, 1412, 1414, 1417, 1419, 1423, 1428, 1432, 1437, 1440, 1443, 1446, 1449,+ 1452, 1455, 1457, 1463, 1466, 1470, 1479, 1486, 1495, 1502, 1505, 1507, 1513, 1516,+ 1520, 1528, 1536, 1544, 1552, 1560, 1568, 1571, 1573, 1579, 1582, 1586, 1590, 1594,+ 1596, 1600, 1605, 1610, 1614, 1618, 1622, 1627, 1632, 1634, 1639, 1644, 1649, 1654,+ 1659, 1664, 1669, 1674, 1679, 1684, 1689, 1694, 1699, 1704, 1711, 1718, 1725, 1732,+ 1737, 1742, 1747, 1752, 1755, 1757, 1763, 1766, 1770, 1779, 1788, 1797, 1806, 1809,+ 1811, 1817, 1820, 1824, 1828, 1832, 1841, 1850, 1853, 1855, 1859, 1868, 1877, 1880,+ 1882, 1888, 1890, 1897, 1901, 1904, 1906, 1910, 1915, 1919, 1923, 1927, 1932, 1937,+ 1942, 1947, 1951, 1956, 1961, 1966, 1971, 1976, 1980, 1985, 1989, 1994, 1998, 2003,+ 2007, 2014, 2024, 2028, 2030, 2034, 2038, 2040, 2043, 2045, 2048, 2050, 2054, 2059,+ 2063, 2068, 2071, 2074, 2077, 2080, 2083, 2085, 2091, 2094, 2098, 2105, 2112, 2121,+ 2125, 2129, 2136, 2143, 2146, 2148, 2155, 2162, 2165, 2167, 2173, 2176, 2180, 2184,+ 2188, 2198, 2208, 2218, 2228, 2231, 2233, 2243, 2253, 2263, 2273, 2276, 2278, 2284,+ 2287, 2291, 2295, 2299, 2305, 2308, 2310, 2314, 2320, 2323, 2325, 2331, 2334, 2338,+ 2342, 2346, 2355, 2364, 2367, 2369, 2373, 2382, 2391, 2394, 2396, 2402, 2404, 2411,+ 2415, 2418, 2420, 2424, 2428, 2433, 2437, 2442, 2445, 2448, 2451, 2454, 2457, 2460,+ 2463, 2466, 2469, 2472, 2475, 2478, 2481, 2484, 2490, 2493, 2495, 2502, 2506, 2509,+ 2511, 2515, 2519, 2524, 2528, 2533, 2536, 2539, 2542, 2545, 2548, 2551, 2554, 2557,+ 2560, 2563, 2566, 2569, 2572, 2575, 2581, 2584, 2586, 2593, 2597, 2600, 2602, 2606,+ 2611, 2615, 2619, 2623, 2628, 2633, 2638, 2643, 2647, 2652, 2657, 2662, 2667, 2672,+ 2676, 2681, 2685, 2690, 2694, 2699, 2703, 2710, 2720, 2724, 2726, 2733, 2737, 2740,+ 2742, 2751, 2760, 2762, 2769, 2773, 2776, 2778, 2782, 2784, 2787, 2789, 2792, 2794,+ 2798, 2803, 2807, 2812, 2815, 2818, 2821, 2824, 2827, 2830, 2833, 2836, 2839, 2842,+ 2844, 2850, 2853, 2857, 2866, 2873, 2882, 2889, 2892, 2894, 2900, 2903, 2907, 2915,+ 2923, 2931, 2939, 2947, 2955, 2958, 2960, 2966, 2969, 2973, 2977, 2981, 2983, 2987,+ 2992, 2997, 3001, 3005, 3009, 3014, 3019, 3021, 3026, 3031, 3036, 3041, 3046, 3051,+ 3056, 3061, 3066, 3071, 3076, 3081, 3086, 3091, 3098, 3105, 3112, 3119, 3124, 3129,+ 3134, 3139, 3142, 3144, 3150, 3153, 3157, 3166, 3175, 3184, 3193, 3196, 3198, 3204,+ 3207, 3211, 3215, 3219, 3228, 3237, 3240, 3242, 3246, 3255, 3264, 3267, 3269, 3275,+ 3278, 3282, 3289, 3296, 3305, 3309, 3313, 3320, 3327, 3330, 3332, 3339, 3346, 3349,+ 3351, 3357, 3360, 3364, 3368, 3372, 3382, 3392, 3402, 3412, 3415, 3417, 3427, 3437,+ 3447, 3457, 3460, 3462, 3468, 3471, 3475, 3479, 3483, 3489, 3492, 3494, 3498, 3504,+ 3507, 3509, 3515, 3518, 3522, 3526, 3530, 3539, 3548, 3551, 3553, 3557, 3566, 3575,+ 3578, 3580, 3586, 3588, 3595, 3599, 3602, 3604, 3608, 3612, 3617, 3621, 3626, 3629,+ 3632, 3635, 3638, 3641, 3644, 3647, 3650, 3653, 3656, 3659, 3662, 3665, 3668, 3674,+ 3677, 3679, 3686, 3690, 3693, 3695, 3698, 3702, 3706, 3711, 3714, 3717, 3720, 3723,+ 3726, 3729, 3732, 3735, 3738, 3741, 3744, 3747, 3750, 3753, 3759, 3764, 3769, 3778,+ 3787, 3790, 3793, 3796, 3799, 3802, 3805, 3811, 3814, 3821, 3824, 3827, 3829, 3836,+ 3839, 3846, 3850, 3854, 3857, 3860, 3865, 3869, 3874, 3879, 3883, 3888, 3892, 3897,+ 3902, 3906, 3911, 3918, 3923, 3928, 3933, 3940, 3945, 3950, 3955, 3964, 3969, 3978,+ 3983, 3992, 3997, 4006, 4011, 4020, 4025, 4034, 4039, 4048, 4053, 4062, 4067, 4076,+ 4081, 4085, 4090, 4095, 4100, 4107, 4112, 4119, 4124, 4129, 4133, 4136, 4139, 4142,+ 4149, 4151, 4154, 4157, 4160, 4166, 4169, 4171, 4174, 4176, 4182, 4186, 4189, 4192,+ 4198, 4204, 4207, 4209, 4212, 4214, 4218, 4221, 4227, 4233, 4236, 4238, 4241, 4243,+ 4247, 4250, 4256, 4262, 4265, 4267, 4270, 4272, 4276, 4279, 4282, 4288, 4291, 4295,+ 4299, 4304, 4308, 4312, 4317, 4322, 4327, 4332, 4337, 4342, 4347, 4352, 4357, 4367,+ 4372, 4377, 4382, 4387, 4390, 4393, 4396, 4399, 4403, 4407, 4411, 4415, 4419, 4424,+ 4429, 4434, 4439, 4444, 4449, 4454, 4459, 4462, 4465, 4468, 4471, 4474, 4477, 4480,+ 4483, 4486, 4490, 4495, 4499, 4503, 4508, 4511, 4515, 4519, 4524, 4527, 4530, 4535,+ 4538, 4541, 4544, 4548, 4552, 4557, 4562, 4567, 4572, 4575, 4579, 4584, 4587, 4590,+ 4593, 4598, 4603, 4606, 4608,+};+const atk_info_t atk_info[N_ATTACKS] = {+ {0, 40, 1}, /* sans_intro */+ {40, 9, 1}, /* sans_bonegap1 */+ {49, 17, 1}, /* sans_bluebone */+ {66, 62, 13}, /* sans_bonegap2 */+ {128, 13, 1}, /* sans_platforms1 */+ {141, 19, 1}, /* sans_platforms2 */+ {160, 22, 3}, /* sans_platforms3 */+ {182, 11, 1}, /* sans_platforms4 */+ {193, 18, 3}, /* sans_platformblaster */+ {211, 11, 1}, /* sans_platforms4hard */+ {222, 9, 1}, /* sans_bonegap1fast */+ {231, 7, 1}, /* sans_boneslideh */+ {238, 18, 3}, /* sans_platformblasterfast */+ {256, 5, 1}, /* sans_spare */+ {261, 105, 17}, /* sans_multi1 */+ {366, 30, 9}, /* sans_randomblaster1 */+ {396, 78, 5}, /* sans_multi2 */+ {474, 26, 4}, /* sans_bonestab1 */+ {500, 26, 4}, /* sans_bonestab2 */+ {526, 30, 9}, /* sans_randomblaster2 */+ {556, 7, 1}, /* sans_boneslidev */+ {563, 168, 18}, /* sans_multi3 */+ {731, 26, 4}, /* sans_bonestab3 */+ {757, 213, 21}, /* sans_final */+};+const char *const atk_name[N_ATTACKS] = {+ "Intro",+ "Bone gap",+ "Blue bones",+ "Bone gap 2",+ "Platforms",+ "Platforms 2",+ "Platforms 3",+ "Platforms 4",+ "Platform blasters",+ "Platforms 4 hard",+ "Bone gap fast",+ "Bone slide",+ "Platform blasters fast",+ "The break",+ "Multi 1",+ "Random blasters",+ "Multi 2",+ "Bone stab",+ "Bone stab 2",+ "Random blasters 2",+ "Bone slide 2",+ "Multi 3",+ "Bone stab 3",+ "The final attack",+};diff --git a/games/badtime/src/attacks.h b/games/badtime/src/attacks.hnew file mode 100644index 00000000..0ca4bc3c--- /dev/null+++ b/games/badtime/src/attacks.h@@ -0,0 +1,102 @@+/* Generated by tools/attacks.py from jcw87's attack scripts: don't edit. */+#ifndef BT_ATTACKS_H+#define BT_ATTACKS_H+#include <stdint.h>++enum {+ OP_NOP,+ OP_SET,+ OP_ADD,+ OP_SUB,+ OP_MUL,+ OP_DIV,+ OP_MOD,+ OP_FLOOR,+ OP_SIN,+ OP_COS,+ OP_ANGLE,+ OP_RND,+ OP_DEG,+ OP_RAD,+ OP_JMPABS,+ OP_JMPREL,+ OP_JMPZ,+ OP_JMPNZ,+ OP_JMPE,+ OP_JMPNE,+ OP_JMPL,+ OP_JMPNL,+ OP_JMPG,+ OP_JMPNG,+ OP_TLPAUSE,+ OP_TLRESUME,+ OP_ENDATTACK,+ OP_BLACKSCREEN,+ OP_BONEH,+ OP_BONEV,+ OP_BONEHREPEAT,+ OP_BONEVREPEAT,+ OP_SINEBONES,+ OP_BONESTAB,+ OP_GASTERBLASTER,+ OP_PLATFORM,+ OP_PLATFORMREPEAT,+ OP_HEARTMODE,+ OP_HEARTTELEPORT,+ OP_HEARTMAXFALLSPEED,+ OP_SANSSLAM,+ OP_SANSSLAMDAMAGE,+ OP_COMBATZONESPEED,+ OP_COMBATZONERESIZE,+ OP_COMBATZONERESIZEINSTANT,+ OP_GETHEARTPOS,+ OP_SANSANIMATION,+ OP_SANSBODY,+ OP_SANSTORSO,+ OP_SANSHEAD,+ OP_SANSSWEAT,+ OP_SANSX,+ OP_SANSREPEAT,+ OP_SANSENDREPEAT,+ OP_SANSTEXT,+ OP_COUNT+};+#define N_ATTACKS 24+enum {+ A_INTRO,+ A_BONEGAP1,+ A_BLUEBONE,+ A_BONEGAP2,+ A_PLATFORMS1,+ A_PLATFORMS2,+ A_PLATFORMS3,+ A_PLATFORMS4,+ A_PLATFORMBLASTER,+ A_PLATFORMS4HARD,+ A_BONEGAP1FAST,+ A_BONESLIDEH,+ A_PLATFORMBLASTERFAST,+ A_SPARE,+ A_MULTI1,+ A_RANDOMBLASTER1,+ A_MULTI2,+ A_BONESTAB1,+ A_BONESTAB2,+ A_RANDOMBLASTER2,+ A_BONESLIDEV,+ A_MULTI3,+ A_BONESTAB3,+ A_FINAL,+};+typedef struct {+ uint16_t first, lines; /* its lines in atk_line */+ uint8_t vars; /* its variables ($pi first) */+} atk_info_t;+/* a line: op | args << 8, the delay, then the arguments; a word with its top bit set is a variable,+ * otherwise a number in atk_pool */+extern const double atk_pool[];+extern const uint16_t atk_code[];+extern const uint16_t atk_line[];+extern const atk_info_t atk_info[N_ATTACKS];+extern const char *const atk_name[N_ATTACKS];+#endifdiff --git a/games/badtime/src/bt.h b/games/badtime/src/bt.hnew file mode 100644index 00000000..4eaa99c9--- /dev/null+++ b/games/badtime/src/bt.h@@ -0,0 +1,165 @@+/* Bad Time Simulator: the fight's state and rules.+ *+ * Everything runs in the original's 640 x 480 pixels, at 30 ticks a second (Undertale's own rate): the scripts'+ * numbers are used as they are, a speed v moves v / 30 px a tick, and the screen shows it at half size. The fight+ * is deterministic: the same keys and seed give the same fight on every computer (no libm, no qsort, no rand;+ * the scripts' sums in doubles, the rest in floats, -ffp-contract=off). */+#ifndef BT_H+#define BT_H+#include <stdbool.h>+#include <stdint.h>+#include "attacks.h"++/* keys (the game's own names: plat_eadk.c reads the calculator's) */+enum { BK_LEFT = 1, BK_RIGHT = 2, BK_UP = 4, BK_DOWN = 8, BK_OK = 16, BK_BACK = 32, BK_HOME = 64, BK_SLOW = 128 };++#define MAX_HP 92+#define LAYOUT_W 640.0f+#define LAYOUT_H 480.0f+#define MAX_BONES 288+#define MAX_STABS 8+#define MAX_BLASTERS 56+#define MAX_PLATS 24+#define MAX_MBONES 12+#define MAX_VARS 40+#define MAX_SHARDS 6++enum { DIR_RIGHT, DIR_DOWN, DIR_LEFT, DIR_UP };+enum { COL_WHITE, COL_BLUE, COL_ORANGE };+enum { HEART_RED, HEART_BLUE };+/* the opponent's poses (the scripts' names) */+enum { ANIM_NONE, ANIM_IDLE, ANIM_HEADBOB, ANIM_TIRED };+enum { BODY_UP, BODY_DOWN, BODY_LEFT, BODY_RIGHT, BODY_NONE };+enum { TORSO_DEFAULT, TORSO_SHRUG };+enum { HEAD_DEFAULT, HEAD_LOOKLEFT, HEAD_WINK, HEAD_CLOSED, HEAD_NOEYES, HEAD_BLUEEYE, HEAD_TIRED1, HEAD_TIRED2 };++typedef struct {+ float x, y, w, h; /* top-left and size */+ float vx, vy; /* px a tick */+ uint8_t dir, color, karma, vertical;+} bone_t;++typedef struct {+ float x, y, w, h; /* the bones (or, while warning, the warning box) */+ float dest_x, dest_y, speed;+ uint8_t dir, state, karma, pad;+ int16_t warn, stay; /* ticks */+ float distance;+} stab_t;+enum { STAB_WARN, STAB_IN, STAB_STAY, STAB_OUT };++typedef struct {+ float x, y, end_x, end_y, ang, end_ang;+ float sx, sy; /* its scale across and along (size 0: 2 x 1) */+ float scale; /* the beam's scale: 1, 2 or 3 */+ float leave; /* backing off, px/s */+ float base, sine; /* the beam's width (35 * scale when full) and its wobble */+ int16_t timer; /* ticks: waiting, then opening */+ int16_t blast; /* the beam's hold, ticks */+ int16_t beam_t; /* ticks since it fired */+ uint8_t state, size, karma, hurts;+ uint8_t opacity; /* the beam's, 0-100 */+ uint8_t pad[3];+} blaster_t;+enum { BL_ENTER, BL_WAIT, BL_FIRE, BL_LEAVE };++typedef struct {+ float x, y, w, vx;+ uint8_t dir, reverse, pad[2];+} plat_t;++typedef struct {+ float x, y, t;+ uint8_t kind, state, button, pad;+} mbone_t;+enum { MB_LEFT, MB_BOTTOM };++typedef struct {+ float x, y, vx, vy;+} shard_t;++/* the script interpreter */+typedef struct {+ double vars[MAX_VARS];+ int16_t attack; /* the attack being played, -1 for none */+ uint16_t line; /* 1-based, as the scripts count */+ int32_t t; /* ticks since the current line was due */+ uint8_t running, finish, talk, ended;+ int16_t talk_line; /* what the opponent says (text.c), -1 for nothing */+} script_t;++typedef struct {+ /* the zone: its edges now and where they're going */+ float zl, zt, zr, zb;+ float tl, tt, tr, tb;+ float zone_speed;+ uint8_t zone_hidden, black;+ /* the heart */+ float hx, hy, hvx, hvy;+ float max_fall;+ uint8_t heart_mode, heart_dir, slammed, slam_damage;+ uint8_t heart_on, moving, jump_held, heart_free; /* shown (and hit), moving, ..., moved by the arrows */+ /* the player */+ int16_t hp, kr;+ int16_t kr_t;+ uint16_t hits; /* ticks hurt (for the tests) */+ /* the opponent */+ float ox, ox_speed;+ uint8_t anim, body, torso, head, sweat, pad2[3];+ int16_t anim_t; /* ticks */+ uint8_t dodge, pad3;+ int16_t dodge_t;+ /* the screen's shake */+ int16_t shake, shake_t;+ /* things */+ uint16_t nbones, nstabs, nblasters, nplats, nmbones;+ bone_t bones[MAX_BONES];+ stab_t stabs[MAX_STABS];+ blaster_t blasters[MAX_BLASTERS];+ plat_t plats[MAX_PLATS];+ mbone_t mbones[MAX_MBONES];+ uint8_t menu_left, menu_bottom;+ float bottom_t;+ uint8_t bottom_alt, pad4[3];+ script_t sc;+ uint32_t rng; /* the fight's random numbers (xorshift32) */+ uint32_t fx_rng; /* the look's (shake, shards): never the fight's */+ uint32_t tick;+ /* while checking the scripts: GetHeartPos says (320, 304) and every spawn goes to a log */+ uint8_t checking, pad5[3];+} world_t;++/* floor in floats (|x| < 2^31): the drawing's and the moves' (the scripts' sums are doubles) */+static inline int bt_ifloor(float x) {+ int i = (int)x;+ return (float)i > x ? i - 1 : i;+}++/* sim.c */+double bt_sin_deg(double d);+double bt_cos_deg(double d);+double bt_atan2_deg(double y, double x);+double bt_floor(double x);+uint32_t bt_rand(uint32_t *s);+uint32_t bt_below(uint32_t *s, uint32_t n);+float bt_sinf_deg(float d);++void sim_reset(world_t *w, uint32_t seed);+void sim_start_attack(world_t *w, int attack);+void sim_stop_attack(world_t *w);+void sim_tick(world_t *w, uint32_t keys, uint32_t pressed);+void sim_zone_to(world_t *w, float l, float t, float r, float b, bool finish);+bool sim_zone_there(const world_t *w);+void sim_clear_attacks(world_t *w);+void sim_menu_bones(world_t *w, bool left, bool bottom);+void sim_heart_mode(world_t *w, int mode);+void sim_resume(world_t *w);+void sim_shake(world_t *w, int n);+/* the heart's centre point is inside a rectangle */+bool sim_heart_in(const world_t *w, float x, float y, float rw, float rh);+/* the beam's corners, for drawing and hurting: its band from `from` to `to` px in front of the blaster, `half`+ * px either side */+void blaster_beam(const blaster_t *b, float from, float to, float half, float qx[4], float qy[4]);+/* the log while checking (tests): a line per thing made */+extern void (*sim_log)(const char *line);+#endifdiff --git a/games/badtime/src/draw.c b/games/badtime/src/draw.cnew file mode 100644index 00000000..0fee30ce--- /dev/null+++ b/games/badtime/src/draw.c@@ -0,0 +1,635 @@+/* Bad Time Simulator: the screens, drawn strip by strip (the launcher's gfx.c: the calculator has no frame+ * buffer). The fight is in the original's 640 x 480 pixels; the screen shows it at half size. */+#include <string.h>+#include "art.h"+#include "draw.h"+#include "gfx.h"++void plat_push(int x, int y, int w, int h, const uint16_t *px); /* (plat.h: its key names are the launcher's too) */+void np_push(int x, int y, int w, int h, const color_t *px) { plat_push(x, y, w, h, px); }++static color_t strip[SCREEN_W * STRIP_H];++#define WHITE 0xFFFF+#define BLACK 0x0000+#define RED HEX(0xFF0000)+#define SOUL_BLUE HEX(0x003CFF)+#define BONE_BLUE HEX(0x14A9FF)+#define BONE_ORANGE HEX(0xFFA040)+#define ORANGE HEX(0xFF7F27)+#define YELLOW HEX(0xFFFF00)+#define MAGENTA HEX(0xFF00FF)+#define HP_RED HEX(0xBF0000)+#define GREY HEX(0x8A8A8A)+#define DIM HEX(0x6E737A)+#define GREEN HEX(0x3CDC3C)+#define PLAT_TOP HEX(0x3CFF3C)+#define PLAT_BODY HEX(0x1E8C1E)++/* the shake: everything of the fight moves by it */+static int ox_, oy_;++static inline int S(float v) { return bt_ifloor(v * 0.5f); }+static inline color_t *px(int x, int y) { return strip + (y - gfx_y0) * SCREEN_W + x; }++/* ---------------------------------------------------------------- pictures */+static color_t pal_cache[3][16];+static const uint32_t *pal_src[3];+static const color_t *pal_of(const art_t *a) {+ for (int i = 0; i < 3; i++) {+ if (pal_src[i] == a->pal) return pal_cache[i];+ if (!pal_src[i]) {+ pal_src[i] = a->pal;+ for (int k = 0; k < 16; k++) pal_cache[i][k] = gfx_rgb(a->pal[k]);+ return pal_cache[i];+ }+ }+ return pal_cache[0];+}+static inline int art_px(const art_t *a, int x, int y) {+ int i = y * a->w + x;+ return (a->px[i >> 1] >> ((i & 1) * 4)) & 15;+}++/* a picture, its anchor at (x, y); mirrored; tint: every pixel that colour (0: its own) */+static void blit(const art_t *a, int x, int y, bool flip, color_t tint, bool tinted) {+ int x0 = x - (flip ? a->w - 1 - a->ax : a->ax), y0 = y - a->ay;+ if (y0 >= gfx_y1 || y0 + a->h <= gfx_y0 || x0 >= SCREEN_W || x0 + a->w <= 0) return;+ const color_t *pal = pal_of(a);+ int ya = y0 < gfx_y0 ? gfx_y0 : y0, yb = y0 + a->h > gfx_y1 ? gfx_y1 : y0 + a->h;+ for (int yy = ya; yy < yb; yy++) {+ int sy = yy - y0;+ for (int sx = 0; sx < a->w; sx++) {+ int xx = x0 + sx;+ if (xx < 0 || xx >= SCREEN_W) continue;+ int c = art_px(a, flip ? a->w - 1 - sx : sx, sy);+ if (c) *px(xx, yy) = tinted ? tint : pal[c];+ }+ }+}++/* a picture turned by ang degrees around its anchor at (cx, cy) on the screen, sa screen pixels a picture+ * pixel along its length (x) and sc across (y) */+static void blit_turned(const art_t *a, float cx, float cy, float ang, float sa, float sc, int alpha32) {+ float c = bt_sinf_deg(ang + 90.0f), s = bt_sinf_deg(ang);+ /* the picture's corners on the screen: its box */+ float lx[2] = {-(float)a->ax * sa, (float)(a->w - a->ax) * sa}, ly[2] = {-(float)a->ay * sc, (float)(a->h - a->ay) * sc};+ float minx = 1e9f, maxx = -1e9f, miny = 1e9f, maxy = -1e9f;+ for (int i = 0; i < 2; i++)+ for (int j = 0; j < 2; j++) {+ float X = cx + lx[i] * c - ly[j] * s, Y = cy + lx[i] * s + ly[j] * c;+ minx = X < minx ? X : minx, maxx = X > maxx ? X : maxx;+ miny = Y < miny ? Y : miny, maxy = Y > maxy ? Y : maxy;+ }+ int y0 = bt_ifloor(miny), y1 = bt_ifloor(maxy) + 1, x0 = bt_ifloor(minx), x1 = bt_ifloor(maxx) + 1;+ if (y0 < gfx_y0) y0 = gfx_y0;+ if (y1 > gfx_y1) y1 = gfx_y1;+ if (x0 < 0) x0 = 0;+ if (x1 > SCREEN_W) x1 = SCREEN_W;+ if (y0 >= y1 || x0 >= x1) return;+ const color_t *pal = pal_of(a);+ float ia = 1.0f / sa, ic = 1.0f / sc;+ for (int y = y0; y < y1; y++) {+ float dy = (float)y + 0.5f - cy, dx = (float)x0 + 0.5f - cx;+ float u = (dx * c + dy * s) * ia + (float)a->ax, v = (-dx * s + dy * c) * ic + (float)a->ay;+ float du = c * ia, dv = -s * ic;+ color_t *p = px(x0, y);+ for (int x = x0; x < x1; x++, u += du, v += dv, p++) {+ if (u < 0 || v < 0 || u >= (float)a->w || v >= (float)a->h) continue;+ int k = art_px(a, (int)u, (int)v);+ if (k) *p = alpha32 >= 32 ? pal[k] : gfx_mix(*p, pal[k], alpha32);+ }+ }+}++/* a convex quad (screen pixels, corners in order), blended white */+static void quad(const float qx[4], const float qy[4], color_t col, int alpha32, int cx0, int cx1, int cy0, int cy1) {+ float miny = qy[0], maxy = qy[0];+ for (int i = 1; i < 4; i++) miny = qy[i] < miny ? qy[i] : miny, maxy = qy[i] > maxy ? qy[i] : maxy;+ int y0 = bt_ifloor(miny), y1 = bt_ifloor(maxy) + 1;+ if (y0 < gfx_y0) y0 = gfx_y0;+ if (y1 > gfx_y1) y1 = gfx_y1;+ if (y0 < cy0) y0 = cy0;+ if (y1 > cy1) y1 = cy1;+ for (int y = y0; y < y1; y++) {+ float yc = (float)y + 0.5f, lo = 1e9f, hi = -1e9f;+ for (int i = 0; i < 4; i++) {+ int j = (i + 1) & 3;+ float ya = qy[i], yb = qy[j];+ if ((yc < ya) == (yc < yb)) continue;+ float x = qx[i] + (qx[j] - qx[i]) * (yc - ya) / (yb - ya);+ lo = x < lo ? x : lo, hi = x > hi ? x : hi;+ }+ if (lo > hi) continue;+ int a = bt_ifloor(lo + 0.5f), b = bt_ifloor(hi + 0.5f);+ if (a < cx0) a = cx0;+ if (b > cx1) b = cx1;+ color_t *p = px(a, y);+ for (int x = a; x < b; x++, p++) *p = alpha32 >= 32 ? col : gfx_mix(*p, col, alpha32);+ }+}++/* ---------------------------------------------------------------- the zone and what's in it */+typedef struct {+ int x0, y0, x1, y1; /* the zone's inside, on the screen */+} clip_t;++static void fill_clip(int x, int y, int w, int h, color_t c, const clip_t *k) {+ int x0 = x < k->x0 ? k->x0 : x, y0 = y < k->y0 ? k->y0 : y;+ int x1 = x + w > k->x1 ? k->x1 : x + w, y1 = y + h > k->y1 ? k->y1 : y + h;+ if (x1 > x0 && y1 > y0) gfx_fill(x0, y0, x1 - x0, y1 - y0, c);+}++/* a bone from (x0, y0) to (x1, y1) on the screen: knobs at both ends */+static void bone(int x0, int y0, int x1, int y1, bool vertical, color_t c, const clip_t *k) {+ int w = x1 - x0, h = y1 - y0;+ if (w <= 0 || h <= 0) return;+ if (vertical) {+ if (h < 6) return fill_clip(x0, y0, w, h, c, k);+ fill_clip(x0 + 1, y0, w - 2, h, c, k);+ fill_clip(x0, y0, 2, 2, c, k), fill_clip(x0 + w - 2, y0, 2, 2, c, k), fill_clip(x0, y0 + 1, w, 1, c, k);+ fill_clip(x0, y1 - 2, 2, 2, c, k), fill_clip(x0 + w - 2, y1 - 2, 2, 2, c, k), fill_clip(x0, y1 - 2, w, 1, c, k);+ } else {+ if (w < 6) return fill_clip(x0, y0, w, h, c, k);+ fill_clip(x0, y0 + 1, w, h - 2, c, k);+ fill_clip(x0, y0, 2, 2, c, k), fill_clip(x0, y0 + h - 2, 2, 2, c, k), fill_clip(x0 + 1, y0, 1, h, c, k);+ fill_clip(x1 - 2, y0, 2, 2, c, k), fill_clip(x1 - 2, y0 + h - 2, 2, 2, c, k), fill_clip(x1 - 2, y0, 1, h, c, k);+ }+}++static color_t bone_color(int c) { return c == COL_BLUE ? BONE_BLUE : c == COL_ORANGE ? BONE_ORANGE : WHITE; }++static void draw_stab(const stab_t *s, const clip_t *k) {+ int x0 = S(s->x) + ox_, y0 = S(s->y) + oy_, x1 = S(s->x + s->w) + ox_, y1 = S(s->y + s->h) + oy_;+ if (s->state == STAB_WARN) {+ /* the warning: a red frame */+ gfx_fill(x0, y0, x1 - x0, 1, RED), gfx_fill(x0, y1 - 1, x1 - x0, 1, RED);+ gfx_fill(x0, y0, 1, y1 - y0, RED), gfx_fill(x1 - 1, y0, 1, y1 - y0, RED);+ return;+ }+ if (s->dir == DIR_DOWN || s->dir == DIR_UP)+ for (int x = x0; x < x1; x += 6) bone(x, y0, x + 5 < x1 ? x + 5 : x1, y1, true, WHITE, k);+ else+ for (int y = y0; y < y1; y += 6) bone(x0, y, x1, y + 5 < y1 ? y + 5 : y1, false, WHITE, k);+}++static void draw_heart(const world_t *w) {+ int x = S(w->hx) + ox_, y = S(w->hy) + oy_;+ color_t c = w->heart_mode == HEART_BLUE ? SOUL_BLUE : RED;+ int dir = w->heart_mode == HEART_BLUE ? w->heart_dir : DIR_DOWN;+ /* turned with its gravity: its point towards the floor it falls to */+ const art_t *a = &ART_HEART;+ for (int yy = 0; yy < 8; yy++) {+ int sy = y - 4 + yy;+ if (sy < gfx_y0 || sy >= gfx_y1) continue;+ for (int xx = 0; xx < 8; xx++) {+ int sx = x - 4 + xx, u = xx, v = yy;+ if (dir == DIR_UP) u = 7 - xx, v = 7 - yy;+ else if (dir == DIR_LEFT) u = yy, v = 7 - xx;+ else if (dir == DIR_RIGHT) u = 7 - yy, v = xx;+ if (u >= a->w || v >= a->h || sx < 0 || sx >= SCREEN_W) continue;+ if (art_px(a, u, v)) *px(sx, sy) = c;+ }+ }+}++/* ---------------------------------------------------------------- the Judge */+typedef struct {+ int8_t x0, y0, x1, y1; /* two eyes: x, y of each (-1: none) */+ uint8_t kind; /* 0 dots, 1 left dot right line, 2 lines, 3 the blue eye, 4 low lines, 5 half dots */+} eyes_t;+static const eyes_t EYES[8] = {+ {12, 13, 20, 13, 0}, /* default */+ {11, 13, 19, 13, 0}, /* looking left */+ {12, 13, 20, 14, 1}, /* wink */+ {11, 14, 20, 14, 2}, /* eyes closed */+ {-1, -1, -1, -1, 0}, /* no eyes */+ {11, 12, -1, -1, 3}, /* the blue eye */+ {12, 14, 20, 14, 5}, /* tired */+ {11, 15, 20, 15, 4}, /* very tired */+};++static void eye_rect(int hx0, int hy0, int x, int y, int w, int h, color_t c) {+ gfx_fill(hx0 + x, hy0 + y, w, h, c);+}++static void draw_judge(const world_t *w, int feet_x, int feet_y, uint32_t tick) {+ float t = (float)w->anim_t / 30.0f;+ float tx = 0, ty = 0, hx = 0, hy = 0;+ if (w->body == BODY_NONE) {+ if (w->anim == ANIM_IDLE) {+ float p = t - 1.2f * (float)bt_ifloor(t / 1.2f);+ tx = bt_sinf_deg(360.0f * p / 1.2f), ty = bt_sinf_deg(720.0f * p / 1.2f);+ hy = -bt_sinf_deg(720.0f * p / 1.2f) * 0.4f;+ } else if (w->anim == ANIM_HEADBOB) {+ float p = t - 1.1f * (float)bt_ifloor(t / 1.1f);+ hx = bt_sinf_deg(360.0f * p / 1.1f), hy = bt_sinf_deg(720.0f * p / 1.1f);+ } else if (w->anim == ANIM_TIRED) {+ float p = t - 3.8f * (float)bt_ifloor(t / 3.8f);+ ty = bt_sinf_deg(360.0f * p / 3.8f) * 2, hy = ty;+ }+ }+ /* (offsets in the original's pixels: rounded to the screen's) */+ int itx = bt_ifloor(tx * 0.5f + 0.5f), ity = bt_ifloor(ty * 0.5f + 0.5f);+ int ihx = itx + bt_ifloor(hx * 0.5f + 0.5f), ihy = ity + bt_ifloor(hy * 0.5f + 0.5f);+ blit(&ART_LEGS, feet_x, feet_y, false, 0, false);+ int tbx = feet_x + itx, tby = feet_y - ART_LEGS.h + ity; /* the torso's bottom-middle */+ int tlx = tbx - ART_TORSO.ax, tty = tby - ART_TORSO.h; /* its top-left */+ int pose = w->body;+ bool shrug = pose == BODY_NONE && w->torso == TORSO_SHRUG;+ /* the arm behind first, then the body, then the arm in front */+ blit(&ART_TORSO, tbx, tby, false, 0, false);+ int lsx = tlx + 2, rsx = tlx + ART_TORSO.w - 3, sy = tty + 2;+ if (shrug) {+ blit(&ART_ARM_SHRUG, lsx - 4, sy + 4, true, 0, false);+ blit(&ART_ARM_SHRUG, rsx + 4, sy + 4, false, 0, false);+ } else {+ if (pose == BODY_LEFT) blit(&ART_ARM_OUT, lsx + 1, sy + 1, true, 0, false);+ else blit(&ART_ARM_SIDE, lsx, sy, false, 0, false);+ if (pose == BODY_UP) blit(&ART_ARM_UP, rsx, sy + 1, true, 0, false);+ else if (pose == BODY_DOWN) blit(&ART_ARM_DOWN, rsx, sy, true, 0, false);+ else if (pose == BODY_RIGHT) blit(&ART_ARM_OUT, rsx - 1, sy + 1, false, 0, false);+ else blit(&ART_ARM_SIDE, rsx, sy, true, 0, false);+ }+ /* the head */+ int hbx = tbx + (ihx - itx), hby = tty + 2 + (ihy - ity);+ blit(&ART_HEAD, hbx, hby, false, 0, false);+ int hx0 = hbx - ART_HEAD.ax, hy0 = hby - ART_HEAD.ay;+ const eyes_t *e = &EYES[w->head & 7];+ color_t ew = HEX(0xF4F2EE);+ switch (e->kind) {+ case 0:+ eye_rect(hx0, hy0, e->x0, e->y0, 2, 2, ew), eye_rect(hx0, hy0, e->x1, e->y1, 2, 2, ew);+ break;+ case 1: eye_rect(hx0, hy0, e->x0, e->y0, 2, 2, ew), eye_rect(hx0, hy0, e->x1, e->y1, 3, 1, ew); break;+ case 2:+ case 4: eye_rect(hx0, hy0, e->x0, e->y0, 3, 1, ew), eye_rect(hx0, hy0, e->x1, e->y1, 3, 1, ew); break;+ case 5: eye_rect(hx0, hy0, e->x0, e->y0, 2, 1, ew), eye_rect(hx0, hy0, e->x1, e->y1, 2, 1, ew); break;+ case 3: {+ bool glow = (tick / 2) & 1;+ eye_rect(hx0, hy0, e->x0, e->y0, 3, 3, HEX(0x3EC8FF));+ eye_rect(hx0, hy0, e->x0 + 1, e->y0 + 1, 1, 1, HEX(0xE8FBFF));+ if (glow) eye_rect(hx0, hy0, e->x0 - 1, e->y0 + 1, 5, 1, HEX(0x1F7FB0)), eye_rect(hx0, hy0, e->x0 + 1, e->y0 - 1, 1, 5, HEX(0x1F7FB0)),+ eye_rect(hx0, hy0, e->x0, e->y0, 3, 3, HEX(0x3EC8FF)), eye_rect(hx0, hy0, e->x0 + 1, e->y0 + 1, 1, 1, HEX(0xE8FBFF));+ break;+ }+ }+ /* sweat: drops off the brim */+ static const int8_t DROP[3][2] = {{28, 11}, {5, 11}, {30, 14}};+ for (int i = 0; i < w->sweat && i < 3; i++) {+ gfx_fill(hx0 + DROP[i][0], hy0 + DROP[i][1], 1, 2, HEX(0x9FD8FF));+ gfx_fill(hx0 + DROP[i][0], hy0 + DROP[i][1] + 2, 1, 1, HEX(0x5FA8DF));+ }+}++/* ---------------------------------------------------------------- words */+static const np_font_t *const BODY_FONT = &np_font_body;+static const np_font_t *const SMALL = &np_font_small;++/* the first n letters of s, line by line, from (x, baseline) */+static void lines(const np_font_t *f, int x, int base, int step, const char *s, int n, color_t c) {+ char buf[80];+ int k = 0, line = 0;+ for (int i = 0; i <= n; i++) {+ char ch = i < n ? s[i] : 0;+ if (ch == '\n' || ch == 0 || k >= 78) {+ buf[k] = 0;+ if (k) gfx_text(f, x, base + line * step, buf, c, 256);+ k = 0, line++;+ if (!ch) break;+ if (ch == '\n') continue;+ }+ buf[k++] = ch;+ }+}++/* s wrapped to width (into out, with line breaks); returns the number of lines */+static int wrap(const np_font_t *f, const char *s, int width, char *out, int cap) {+ int n = 0, linew = 0, nl = 1, word = 0;+ for (const char *p = s; *p && n < cap - 1; p++) {+ if (*p == '\n') {+ out[n++] = '\n', linew = 0, nl++, word = n;+ continue;+ }+ char one[2] = {*p, 0};+ int cw = gfx_text_width(f, one);+ if (*p == ' ') word = n + 1;+ if (linew + cw > width && word > 0 && out[word - 1] == ' ') {+ out[word - 1] = '\n', nl++;+ linew = 0;+ for (int i = word; i < n; i++) {+ char o[2] = {out[i], 0};+ linew += gfx_text_width(f, o);+ }+ }+ out[n++] = *p, linew += cw;+ }+ out[n] = 0;+ return nl;+}++static void key_line(const char *s) {+ if (s && *s) gfx_text_center(SMALL, SCREEN_W / 2, 10, s, DIM, 256);+}++/* ---------------------------------------------------------------- the fight */+static const char *const BUTTONS[4] = {"FIGHT", "ACT", "ITEM", "MERCY"};++static void draw_ui(const fight_t *f) {+ const world_t *w = &f->w;+ gfx_text(BODY_FONT, 16, 210, "YOU LV 19", WHITE, 256);+ gfx_text(SMALL, 110, 209, "HP", WHITE, 256);+ int bw = 55, hpw = w->hp * bw / MAX_HP, krx = (w->hp - w->kr) * bw / MAX_HP;+ if (hpw < 0) hpw = 0;+ if (krx < 0) krx = 0;+ gfx_fill(128, 200, bw, 11, HP_RED);+ gfx_fill(128, 200, hpw, 11, YELLOW);+ if (w->kr > 0) gfx_fill(128 + krx, 200, hpw - krx > 1 ? hpw - krx : 1, 11, MAGENTA);+ gfx_text(SMALL, 188, 209, "KR", WHITE, 256);+ char num[16];+ int hp = w->hp < 0 ? 0 : w->hp, k = 0;+ num[k++] = (char)('0' + hp / 10 % 10), num[k++] = (char)('0' + hp % 10);+ memcpy(num + k, " / 92", 6);+ gfx_text(BODY_FONT, 208, 210, num, w->kr > 0 ? MAGENTA : WHITE, 256);+ for (int i = 0; i < 4; i++) {+ static const int BX[4] = {16, 92, 172, 248};+ bool on = (f->state == FS_MENU || f->state == FS_SUB) && f->sel == i;+ color_t c = on ? YELLOW : ORANGE;+ int x = BX[i], y = 216;+ gfx_fill(x, y, 55, 1, c), gfx_fill(x, y + 20, 55, 1, c), gfx_fill(x, y, 1, 21, c), gfx_fill(x + 54, y, 1, 21, c);+ gfx_fill(x + 1, y + 1, 53, 1, c), gfx_fill(x + 1, y + 19, 53, 1, c), gfx_fill(x + 1, y + 1, 1, 19, c), gfx_fill(x + 53, y + 1, 1, 19, c);+ gfx_text_center(SMALL, x + 35, y + 14, BUTTONS[i], c, 256);+ }+}++static void draw_box_words(const fight_t *f) {+ const world_t *w = &f->w;+ int x = S(w->zl + 5) + 6, top = S(w->zt + 5);+ if (f->state == FS_SUB) {+ static char buf[48];+ if (f->sub == SUB_ITEM) {+ int page = f->sub_sel / 4;+ for (int i = page * 4; i < f->nitems && i < page * 4 + 4; i++) {+ float hx, hy;+ fight_item_pos(i, &hx, &hy);+ memcpy(buf, "* ", 2);+ strcpy(buf + 2, ITEMS[f->items[i]].name);+ gfx_text(BODY_FONT, S(hx) + 8, S(hy) + 5, buf, WHITE, 256);+ }+ char pg[8] = "PAGE 1";+ pg[5] = (char)('1' + page);+ gfx_text(BODY_FONT, 192, 182, pg, WHITE, 256);+ return;+ }+ const char *item = f->sub == SUB_ENEMY ? "* Judge" : f->sub == SUB_ACT ? "* Check" : "* Spare";+ float hx, hy;+ fight_item_pos(0, &hx, &hy);+ gfx_text(BODY_FONT, S(hx) + 8, S(hy) + 5, item, f->sub == SUB_MERCY ? YELLOW : WHITE, 256);+ return;+ }+ if (f->text_kind == TXT_BOX) lines(BODY_FONT, x, top + 15, 16, f->text, f->text_shown, WHITE);+}++static void draw_bubble(const fight_t *f) {+ const world_t *w = &f->w;+ float feet_y = (w->ox_speed != 0 ? w->zt : w->tt) - 16;+ int bx = S(w->ox + 64), by = S(feet_y - 128);+ static char wrapped[TEXT_MAX + 16];+ int n = wrap(SMALL, f->text, 112, wrapped, sizeof wrapped);+ int bw = 124, bh = n * 11 + 10;+ if (bx + bw > SCREEN_W - 2) bx = SCREEN_W - 2 - bw;+ if (by < 16) by = 16;+ gfx_rrect(bx, by, bw, bh, 4, WHITE, 256);+ /* the tail, towards his head */+ for (int i = 0; i < 6; i++) gfx_fill(bx - 6 + i, by + 12 + i / 2, 6 - i, 1, WHITE);+ /* only the letters typed so far (the line breaks are where they'll end up) */+ int shown = f->text_shown, k = 0, out = 0;+ for (; wrapped[k] && out < shown; k++)+ if (wrapped[k] != '\n') out++;+ lines(SMALL, bx + 6, by + 13, 11, wrapped, k, BLACK);+}++static void draw_target(const fight_t *f, const clip_t *k) {+ const world_t *w = &f->w;+ int x0 = k->x0, x1 = k->x1, y0 = k->y0, y1 = k->y1, cx = (x0 + x1) / 2, cy = (y0 + y1) / 2;+ int fade = f->state == FS_TARGET ? 32 : f->bar_fade * 32 / 40;+ if (fade <= 0) return;+ /* the target: a lens of lines, its middle the best */+ int hw = (x1 - x0) / 2 - 4, hh = (y1 - y0) / 2 - 4;+ for (int y = y0; y < y1; y++) {+ if (y < gfx_y0 || y >= gfx_y1) continue;+ int dy = y - cy;+ /* x where the lens is this high: |dy| <= hh * (1 - (dx / hw)^2) */+ float r = 1.0f - (float)(dy < 0 ? -dy : dy) / (float)hh;+ if (r < 0) continue;+ float sq = r, g = 0.5f;+ for (int i = 0; i < 8; i++) g = 0.5f * (g + sq / (g > 1e-3f ? g : 1e-3f));+ int span = (int)((float)hw * g);+ color_t edge = gfx_mix(BLACK, HEX(0x1FA84A), fade);+ gfx_fill(cx - span, y, 1, 1, edge), gfx_fill(cx + span, y, 1, 1, edge);+ for (int i = 1; i < 8; i++) {+ int dx = span * i / 8;+ color_t c = gfx_mix(BLACK, i < 2 ? HEX(0xE8E8E8) : HEX(0x2E6E44), fade);+ if (!(y & 1)) gfx_fill(cx - dx, y, 1, 1, c), gfx_fill(cx + dx, y, 1, 1, c);+ }+ }+ gfx_fill(cx, y0 + 2, 1, y1 - y0 - 4, gfx_mix(BLACK, WHITE, fade));+ /* the bar */+ int bx = S(f->bar_x) + ox_;+ bool flash = f->bar_hit && ((w->tick / 2) & 1);+ color_t in = flash ? BLACK : WHITE, out = flash ? WHITE : BLACK;+ if (f->state == FS_TARGET || f->bar_hit) {+ gfx_fill(bx - 3, y0, 7, y1 - y0, gfx_mix(BLACK, out, fade));+ gfx_fill(bx - 2, y0 + 1, 5, y1 - y0 - 2, gfx_mix(BLACK, in, fade));+ }+}++static void draw_slash(const fight_t *f, int cx, int cy) {+ if (f->slash_t <= 0) return;+ int t = 18 - f->slash_t, len = t < 8 ? t * 4 : 32;+ for (int i = 0; i < len; i++) {+ int x = cx - 16 + i, y = cy - 16 + i;+ gfx_fill(x, y, 3, 2, i > len - 4 ? HEX(0xFF8080) : RED);+ }+}++typedef struct {+ const fight_t *f;+ const char *keys, *title;+} fight_ctx_t;++static void fight_scene(void *ctx) {+ const fight_ctx_t *c = ctx;+ const fight_t *f = c->f;+ const world_t *w = &f->w;+ memset(strip, 0, sizeof strip);+ if (!w->black) {+ /* the Judge */+ float feet = (w->ox_speed != 0 ? w->zt : w->tt) - 16;+ int fx = S(w->ox) + ox_, fy = S(feet) + oy_;+ draw_judge(w, fx, fy, w->tick);+ draw_slash(f, fx, fy - 30);+ if (f->miss_t > 0) gfx_text_center(&np_font_title, 150, 46, "MISS", HEX(0xC0C0C0), 256);+ /* the zone */+ clip_t k = {S(w->zl + 5) + ox_, S(w->zt + 5) + oy_, S(w->zr - 5) + ox_, S(w->zb - 5) + oy_};+ if (k.x0 < 0) k.x0 = 0;+ if (k.x1 > SCREEN_W) k.x1 = SCREEN_W;+ if (!w->zone_hidden) {+ int zl = S(w->zl) + ox_, zt = S(w->zt) + oy_, zr = S(w->zr) + ox_, zb = S(w->zb) + oy_;+ gfx_fill(zl, zt, zr - zl, k.y0 - zt, WHITE);+ gfx_fill(zl, k.y1, zr - zl, zb - k.y1, WHITE);+ gfx_fill(zl, zt, k.x0 - zl, zb - zt, WHITE);+ gfx_fill(k.x1, zt, zr - k.x1, zb - zt, WHITE);+ }+ if (k.y1 > gfx_y0 && k.y0 < gfx_y1) {+ for (int i = 0; i < w->nplats; i++) {+ const plat_t *p = &w->plats[i];+ int x0 = S(p->x) + ox_, y0 = S(p->y) + oy_, x1 = S(p->x + p->w) + ox_;+ fill_clip(x0, y0, x1 - x0, 1, PLAT_TOP, &k);+ fill_clip(x0, y0 + 1, x1 - x0, 2, PLAT_BODY, &k);+ fill_clip(x0, y0 + 1, 1, 2, PLAT_TOP, &k), fill_clip(x1 - 1, y0 + 1, 1, 2, PLAT_TOP, &k);+ }+ for (int i = 0; i < w->nbones; i++) {+ const bone_t *b = &w->bones[i];+ int y0 = S(b->y) + oy_, y1 = S(b->y + b->h) + oy_;+ if (y1 <= gfx_y0 || y0 >= gfx_y1) continue;+ int x0 = S(b->x) + ox_, x1 = S(b->x + b->w) + ox_;+ if (x1 <= k.x0 || x0 >= k.x1) continue;+ bone(x0, y0, x1, y1, b->vertical, bone_color(b->color), &k);+ }+ for (int i = 0; i < w->nstabs; i++) draw_stab(&w->stabs[i], &k);+ if (f->state == FS_TARGET || f->bar_fade > 0) draw_target(f, &k);+ }+ /* the gavels' beams, then the gavels */+ for (int i = 0; i < w->nblasters; i++) {+ const blaster_t *b = &w->blasters[i];+ if (b->state != BL_LEAVE || b->base <= 0) continue;+ float k3 = b->scale, qx[4], qy[4];+ int a = b->opacity * 32 / 100;+ static const float FROM[3] = {35, 30, 25}, DIV[3] = {1, 1.25f, 2};+ for (int j = 2; j >= 0; j--) {+ float half = (b->base / DIV[j] + b->sine) / 2;+ if (half <= 0) continue;+ blaster_beam(b, FROM[j] * k3, j ? 35 * k3 : 35 * k3 + 1000, half, qx, qy);+ for (int m = 0; m < 4; m++) qx[m] = qx[m] * 0.5f + (float)ox_, qy[m] = qy[m] * 0.5f + (float)oy_;+ quad(qx, qy, WHITE, a, 0, SCREEN_W, 0, SCREEN_H);+ }+ }+ for (int i = 0; i < w->nblasters; i++) {+ const blaster_t *b = &w->blasters[i];+ const art_t *a = b->state >= BL_FIRE ? &ART_GAVEL_OPEN : &ART_GAVEL_SHUT;+ blit_turned(a, b->x * 0.5f + (float)ox_, b->y * 0.5f + (float)oy_, b->ang, b->sy, b->sx, 32);+ }+ /* the menu's bones */+ for (int i = 0; i < w->nmbones; i++) {+ const mbone_t *m = &w->mbones[i];+ int x0 = S(m->x) + ox_, y0 = S(m->y) + oy_;+ clip_t all = {0, 0, SCREEN_W, SCREEN_H};+ bone(x0, y0, x0 + 7, y0 + 22, true, WHITE, &all);+ }+ draw_ui(f);+ draw_box_words(f);+ if (f->text_kind == TXT_BUBBLE) draw_bubble(f);+ }+ if (w->heart_on) {+ if (f->state == FS_DEATH && f->death_t >= 60) {+ for (int i = 0; i < MAX_SHARDS; i++) {+ const shard_t *s = &f->shards[i];+ int x = S(s->x), y = S(s->y);+ bool t = (f->death_t / 3 + i) & 1;+ gfx_fill(x, y, 2, t ? 2 : 1, RED), gfx_fill(x + (t ? 0 : 1), y + 1, 1, 1, RED);+ }+ } else if (f->state == FS_DEATH && f->death_t >= 20) {+ blit(&ART_HEART_BROKEN, S(w->hx), S(w->hy), false, RED, true);+ } else draw_heart(w);+ }+ if (f->banner_t > 0 && f->banner)+ gfx_text(&np_font_title, 8, 46, f->banner == 1 ? "SUCCESS" : "FAILURE", f->banner == 1 ? GREEN : RED, 256);+ if (c->title) gfx_text(SMALL, 6, 24, c->title, GREY, 256);+ key_line(c->keys);+}++void draw_fight(const fight_t *f, const char *title) {+ const world_t *w = &f->w;+ ox_ = oy_ = 0;+ if (w->shake > 0) {+ uint32_t r = w->tick * 2654435761u;+ int n = (w->shake + 1) / 2;+ ox_ = (r >> 7) & 1 ? n : -n, oy_ = (r >> 9) & 1 ? n : -n;+ }+ gfx_buf = strip;+ fight_ctx_t c = {f, fight_keys(f), title};+ gfx_render(fight_scene, &c, 0, SCREEN_H);+}++/* ---------------------------------------------------------------- the other screens */+typedef struct {+ const screen_t *s;+} screen_ctx_t;++static void heart_cursor(int x, int y) {+ for (int yy = 0; yy < ART_HEART.h; yy++)+ for (int xx = 0; xx < ART_HEART.w; xx++)+ if (art_px(&ART_HEART, xx, yy)) gfx_fill(x + xx, y + yy, 1, 1, RED);+}++static void screen_scene(void *ctx) {+ const screen_t *s = ((const screen_ctx_t *)ctx)->s;+ memset(strip, 0, sizeof strip);+ if (s->judge) {+ world_t w;+ memset(&w, 0, sizeof w);+ w.anim = ANIM_IDLE, w.body = BODY_NONE, w.head = s->judge_head, w.anim_t = (int16_t)(s->tick % 3600);+ draw_judge(&w, 160, s->judge_y, s->tick);+ }+ if (s->title) gfx_text_center(&np_font_title, SCREEN_W / 2, s->title_y, s->title, WHITE, 256);+ for (int i = 0; i < s->n; i++) {+ int y = s->list_y + (i - s->first) * s->step;+ if (i < s->first || y > SCREEN_H - 14) continue;+ bool on = i == s->sel;+ gfx_text(BODY_FONT, s->list_x + 14, y, s->items[i], on ? YELLOW : WHITE, 256);+ if (on) heart_cursor(s->list_x, y - 8);+ }+ if (s->more_up) gfx_text_center(SMALL, SCREEN_W / 2, s->list_y - s->step + 2, "^", GREY, 256);+ if (s->more_down) gfx_text_center(SMALL, SCREEN_W / 2, s->list_y + s->rows * s->step - 4, "v", GREY, 256);+ if (s->body) lines(BODY_FONT, s->body_x, s->body_y, 16, s->body, (int)strlen(s->body), WHITE);+ if (s->foot) gfx_text_center(SMALL, SCREEN_W / 2, 232, s->foot, GREY, 256);+ key_line(s->keys);+}++void draw_screen(const screen_t *s) {+ gfx_buf = strip;+ screen_ctx_t c = {s};+ gfx_render(screen_scene, &c, 0, SCREEN_H);+}++/* the pause card over the fight */+typedef struct {+ const fight_t *f;+ const screen_t *s;+} pause_ctx_t;+static void pause_scene(void *ctx) {+ const pause_ctx_t *p = ctx;+ fight_ctx_t c = {p->f, 0, 0};+ fight_scene(&c);+ for (int y = gfx_y0; y < gfx_y1; y++)+ for (int x = 0; x < SCREEN_W; x++) *px(x, y) = gfx_mix(*px(x, y), BLACK, 22);+ const screen_t *s = p->s;+ int bx = 70, by = 60, bw = 180, bh = 112;+ gfx_rrect(bx, by, bw, bh, 6, HEX(0x101014), 256);+ gfx_rrect_outline(bx, by, bw, bh, 6, 2, WHITE, 256);+ gfx_text_center(&np_font_title, SCREEN_W / 2, by + 28, s->title, WHITE, 256);+ for (int i = 0; i < s->n; i++) {+ int y = by + 52 + i * 18;+ bool on = i == s->sel;+ gfx_text(BODY_FONT, bx + 44, y, s->items[i], on ? YELLOW : WHITE, 256);+ if (on) heart_cursor(bx + 30, y - 8);+ }+ key_line(s->keys);+}+void draw_pause(const fight_t *f, const screen_t *s) {+ ox_ = oy_ = 0;+ gfx_buf = strip;+ pause_ctx_t c = {f, s};+ gfx_render(pause_scene, &c, 0, SCREEN_H);+}diff --git a/games/badtime/src/draw.h b/games/badtime/src/draw.hnew file mode 100644index 00000000..0dfe339d--- /dev/null+++ b/games/badtime/src/draw.h@@ -0,0 +1,25 @@+/* Bad Time Simulator: the screens (draw.c). */+#ifndef BT_DRAW_H+#define BT_DRAW_H+#include "fight.h"++/* a menu screen: a title, a list with the heart beside the chosen line, some words, a key line */+typedef struct {+ const char *title;+ int title_y;+ const char *const *items;+ int n, sel, first, rows, list_x, list_y, step;+ bool more_up, more_down;+ const char *body;+ int body_x, body_y;+ const char *foot, *keys;+ bool judge; /* the Judge in the middle, his feet at judge_y */+ int judge_y;+ uint8_t judge_head;+ uint32_t tick;+} screen_t;++void draw_fight(const fight_t *f, const char *title);+void draw_screen(const screen_t *s);+void draw_pause(const fight_t *f, const screen_t *s); /* (title, items, sel, keys) over the fight */+#endifdiff --git a/games/badtime/src/fight.c b/games/badtime/src/fight.cnew file mode 100644index 00000000..e11a4353--- /dev/null+++ b/games/badtime/src/fight.c@@ -0,0 +1,448 @@+/* Bad Time Simulator: the fight's turns (fight.h), as jcw87's Battle sheet runs them: the attacks in their+ * order, the menu between them, FIGHT and the dodge, the items, the break, the final attack and the end; and the+ * Practice, Endless and Single attack modes. */+#include <string.h>+#include "fight.h"++#define MENU_L 33.0f+#define MENU_T 251.0f+#define MENU_R 608.0f+#define MENU_B 391.0f++static const uint8_t FIRST_HALF[14] = {A_INTRO, A_BONEGAP1, A_BLUEBONE, A_BONEGAP2, A_PLATFORMS1, A_PLATFORMS2,+ A_PLATFORMS3, A_PLATFORMS4, A_PLATFORMBLASTER, A_PLATFORMS4HARD,+ A_BONEGAP1FAST, A_BONESLIDEH, A_BONEGAP2, A_PLATFORMBLASTERFAST};+static const uint8_t FIRST_RANDOM[4] = {A_BONEGAP1FAST, A_BONEGAP2, A_BONESLIDEH, A_PLATFORMBLASTERFAST};+static const uint8_t SECOND_HALF[9] = {A_MULTI1, A_RANDOMBLASTER1, A_MULTI2, A_BONESTAB1, A_BONESTAB2,+ A_RANDOMBLASTER2, A_BONESLIDEV, A_MULTI3, A_BONESTAB3};+static const uint8_t SECOND_RANDOM[3] = {A_BONESTAB3, A_MULTI3, A_RANDOMBLASTER2};+/* the buttons, in the original's pixels */+static const float BUTTON_X[4] = {32, 184, 344, 496};++int fight_progress_of(int hit, int next) {+ if (hit < 13) return (next < 13 ? next : 13) + 1;+ if (hit == 13) return 14;+ if (hit <= 22) return 15 + (next < 8 ? next : 8);+ return 24;+}++static void say(fight_t *f, const char *s, int kind, int after) {+ size_t n = s ? strlen(s) : 0;+ if (n >= TEXT_MAX) n = TEXT_MAX - 1;+ memcpy(f->text, s ? s : "", n);+ f->text[n] = 0;+ f->text_len = (uint16_t)n, f->text_shown = 0;+ f->text_kind = (uint8_t)kind, f->text_after = (uint8_t)after;+}+static void quiet(fight_t *f) { f->text_kind = TXT_NONE, f->text_len = f->text_shown = 0, f->text[0] = 0; }++/* the heart as the menu's cursor */+static void heart_at(fight_t *f, float x, float y) {+ f->w.hx = x, f->w.hy = y, f->w.heart_on = 1, f->w.heart_free = 0;+ f->w.heart_mode = HEART_RED, f->w.heart_dir = DIR_DOWN;+}+static void heart_on_button(fight_t *f) { heart_at(f, BUTTON_X[f->sel] + 18, 453); }+void fight_item_pos(int i, float *x, float *y) {+ *x = 64 + (float)(i % 2) * 256 + 8;+ *y = 272 + (float)((i % 4) / 2) * 32 + 12;+}++static int pick_attack(fight_t *f) {+ if (f->mode == MODE_SINGLE) return f->single;+ if (f->mode == MODE_PRACTICE) f->next_attack = f->practice;+ int h = f->hit_attempts, n = f->next_attack, a;+ if (h < 13) {+ if (n < 14) a = FIRST_HALF[n], f->next_attack++;+ else a = FIRST_RANDOM[bt_below(&f->w.rng, 4)];+ } else if (h == 13) {+ a = A_SPARE;+ f->w.sweat = 2;+ f->next_attack = 0;+ } else if (h <= 22) {+ if (n < 9) a = SECOND_HALF[n], f->next_attack++;+ else a = SECOND_RANDOM[bt_below(&f->w.rng, 3)];+ } else a = A_FINAL;+ return a;+}++/* what he says before an attack (Normal) */+static const char *turn_line(const fight_t *f, int attack) {+ if (f->mode != MODE_NORMAL) return 0;+ int h = f->hit_attempts, n = f->next_attack - 1; /* (already counted) */+ if (attack == A_FINAL) return TALK_FINAL;+ if (h < 13) return n >= 1 && n < 14 ? TALK_FIRST[n] : 0;+ if (h == 13) return TALK_BREAK;+ return n >= 0 && n < 10 ? TALK_SECOND[n] : 0;+}++static void start_turn(fight_t *f) {+ world_t *w = &f->w;+ if (f->mode == MODE_NORMAL) {+ int p = fight_progress_of(f->hit_attempts, f->next_attack);+ if (p > f->progress) f->progress = (uint8_t)p;+ }+ int a = pick_attack(f);+ f->last_attack = (uint8_t)a;+ sim_start_attack(w, a);+ if (f->mode == MODE_PRACTICE) w->hp = MAX_HP, w->kr = 0, f->failed = 0;+ if (f->mode == MODE_SINGLE) w->anim = ANIM_IDLE, w->head = HEAD_DEFAULT;+ quiet(f);+ const char *line = turn_line(f, a);+ if (line) {+ w->sc.running = 0, w->sc.talk = 1, w->sc.talk_line = -1;+ say(f, line, TXT_BUBBLE, 0);+ }+ f->state = FS_ATTACK;+}++static const char *info_line(const fight_t *f) {+ const char *s = f->info ? f->info : INFO_DEFAULT;+ int h = f->hit_attempts;+ if (f->w.kr > 0) s = INFO_KARMA;+ if (h < 13 && f->next_attack == 1) s = INFO_FIRST;+ if (h == 13) s = INFO_BREAK;+ if (h == 15) s = INFO_15;+ if (h == 19) s = INFO_19;+ if (h == 20) s = INFO_20;+ if (h == 21) s = INFO_21;+ if (h == 22) s = INFO_22;+ return s;+}++static void open_menu(fight_t *f) {+ f->state = FS_MENU;+ heart_on_button(f);+ f->info = info_line(f);+ say(f, f->info, TXT_BOX, 0);+}++static void end_attack(fight_t *f) {+ world_t *w = &f->w;+ bool failed = f->failed;+ sim_stop_attack(w);+ f->attacks_done++;+ if (f->mode == MODE_PRACTICE) {+ f->banner = failed ? 2 : 1, f->banner_t = 30;+ if (!failed) {+ f->practice++;+ if (f->hit_attempts < 13 && f->practice == 14) f->hit_attempts = 14, f->practice = 0;+ if (f->hit_attempts > 13 && f->practice > 9) f->practice = 9;+ }+ if (f->hit_attempts > 13 && f->practice == 9) f->hit_attempts = 23;+ }+ w->anim = ANIM_IDLE, w->head = HEAD_DEFAULT, w->torso = TORSO_DEFAULT, w->body = BODY_NONE;+ if (f->mode == MODE_NORMAL && f->hit_attempts > 22) {+ /* the last words, then the win */+ f->state = FS_WIN, f->win_line = 0;+ sim_zone_to(w, w->zl, w->zt, w->zr, w->zb, false);+ w->anim = ANIM_TIRED, w->head = HEAD_TIRED2;+ w->heart_on = 0;+ say(f, TALK_WIN[0], TXT_BUBBLE, 0);+ return;+ }+ sim_zone_to(w, MENU_L, MENU_T, MENU_R, MENU_B, false);+ int h = f->hit_attempts;+ if (f->mode == MODE_NORMAL) sim_menu_bones(w, h > 13 && h != 16 && h != 17 && h <= 22, h > 15 && h <= 22);+ f->state = FS_RETURN;+}++static void die(fight_t *f) {+ world_t *w = &f->w;+ sim_stop_attack(w);+ sim_menu_bones(w, false, false);+ w->nmbones = 0;+ w->black = 1, w->heart_on = 1, w->heart_mode = HEART_RED, w->heart_dir = DIR_DOWN;+ quiet(f);+ f->state = FS_DEATH, f->death_t = 0;+}++void fight_start(fight_t *f, int mode, int arg, uint32_t seed) {+ memset(f, 0, sizeof *f);+ f->mode = (uint8_t)mode, f->seed = seed;+ sim_reset(&f->w, seed);+ static const uint8_t START_ITEMS[8] = {0, 1, 2, 3, 3, 3, 3, 3};+ memcpy(f->items, START_ITEMS, 8);+ f->nitems = 8;+ world_t *w = &f->w;+ if (mode == MODE_SINGLE) f->single = (uint8_t)(arg >= 0 && arg < N_ATTACKS ? arg : 0);+ if (mode == MODE_ENDLESS) {+ f->phase = (uint8_t)(arg == 1);+ if (f->phase) f->hit_attempts = 15, w->head = HEAD_DEFAULT, w->anim = ANIM_IDLE;+ }+ if (mode == MODE_PRACTICE) f->practice = 0;+ /* the fight opens on its first attack, the box as the menu's */+ sim_zone_to(w, MENU_L, MENU_T, MENU_R, MENU_B, false);+ w->zl = MENU_L, w->zt = MENU_T, w->zr = MENU_R, w->zb = MENU_B;+ start_turn(f);+}++bool fight_over(const fight_t *f) { return f->state == FS_LOST || f->state == FS_WON; }+bool fight_can_pause(const fight_t *f) { return f->state != FS_SUB; }++/* the words come a letter a tick; OK shows them all, then goes on */+static bool text_tick(fight_t *f, uint32_t pressed) {+ if (f->text_kind == TXT_NONE) return false;+ if (f->text_shown < f->text_len) {+ f->text_shown++;+ if (pressed & BK_OK) f->text_shown = f->text_len;+ return false;+ }+ return (pressed & BK_OK) != 0;+}++static int item_count_on_page(const fight_t *f, int page) {+ int n = f->nitems - page * 4;+ return n < 0 ? 0 : n > 4 ? 4 : n;+}++static void sub_open(fight_t *f, int sub) {+ f->state = FS_SUB, f->sub = (uint8_t)sub, f->sub_sel = 0;+ quiet(f);+ float x, y;+ fight_item_pos(0, &x, &y);+ heart_at(f, x, y);+}++static void sub_cursor(fight_t *f) {+ float x, y;+ fight_item_pos(f->sub_sel, &x, &y);+ heart_at(f, x, y);+}++static void use_item(fight_t *f) {+ int slot = f->sub_sel;+ if (slot >= f->nitems) return;+ const item_t *it = &ITEMS[f->items[slot]];+ world_t *w = &f->w;+ w->hp = (int16_t)(w->hp + it->heal);+ bool maxed = w->hp >= MAX_HP;+ if (w->hp > MAX_HP) w->hp = MAX_HP;+ if (w->kr >= w->hp) w->kr = (int16_t)(w->hp - 1);+ memmove(f->items + slot, f->items + slot + 1, (size_t)(f->nitems - slot - 1));+ f->nitems--;+ char buf[TEXT_MAX], *p = buf;+ const char *a = "* You ate the ", *b = ".\n* ";+ while (*a) *p++ = *a++;+ for (const char *s = it->full; *s; s++) *p++ = *s;+ while (*b) *p++ = *b++;+ if (maxed) {+ for (const char *s = "Your HP was maxed out."; *s; s++) *p++ = *s;+ } else {+ for (const char *s = "You got "; *s; s++) *p++ = *s;+ int v = it->heal;+ if (v >= 10) *p++ = (char)('0' + v / 10);+ *p++ = (char)('0' + v % 10);+ for (const char *s = " HP back."; *s; s++) *p++ = *s;+ }+ *p = 0;+ f->state = FS_INFO, f->w.heart_on = 0;+ say(f, buf, TXT_BOX, 0);+}++static void sub_ok(fight_t *f) {+ switch (f->sub) {+ case SUB_ENEMY:+ if (f->sel == 0) {+ /* FIGHT: the bar comes in from a side */+ f->state = FS_TARGET, f->w.heart_on = 0;+ f->bar_dir = bt_below(&f->w.rng, 2) == 0 ? 1 : -1;+ f->bar_x = f->bar_dir > 0 ? f->w.zl : f->w.zr;+ f->bar_hit = 0, f->bar_fade = 0;+ } else {+ f->sub = SUB_ACT, f->sub_sel = 0;+ sub_cursor(f);+ }+ break;+ case SUB_ACT:+ f->state = FS_INFO, f->w.heart_on = 0;+ say(f, CHECK_TEXT, TXT_BOX, f->hit_attempts > 0 ? 1 : 0);+ break;+ case SUB_ITEM: use_item(f); break;+ case SUB_MERCY:+ f->w.heart_on = 0;+ start_turn(f);+ break;+ }+}++static void sub_keys(fight_t *f, uint32_t pressed) {+ if (pressed & BK_BACK) {+ if (f->sub == SUB_ACT) {+ f->sub = SUB_ENEMY, f->sub_sel = 0;+ sub_cursor(f);+ } else open_menu(f);+ return;+ }+ if (pressed & BK_OK) {+ sub_ok(f);+ return;+ }+ if (f->sub != SUB_ITEM) return;+ /* the items: two columns, four a page; Left and Right go on to the next page */+ int s = f->sub_sel, page = s / 4, i = s % 4, on = item_count_on_page(f, page);+ if ((pressed & BK_RIGHT) && (i % 2 == 0) && i + 1 < on) s++;+ else if ((pressed & BK_RIGHT) && (i % 2 == 1 || i + 1 >= on) && item_count_on_page(f, page + 1))+ s = (page + 1) * 4 + (i / 2) * 2, s = s < f->nitems ? s : f->nitems - 1;+ else if ((pressed & BK_LEFT) && (i % 2 == 1)) s--;+ else if ((pressed & BK_LEFT) && (i % 2 == 0) && page > 0) s = (page - 1) * 4 + (i / 2) * 2 + 1;+ else if ((pressed & BK_DOWN) && i + 2 < on) s += 2;+ else if ((pressed & BK_UP) && i >= 2) s -= 2;+ if (s != f->sub_sel) {+ f->sub_sel = (uint8_t)s;+ sub_cursor(f);+ }+}++static void dodge_tick(fight_t *f) {+ world_t *w = &f->w;+ float t = (float)f->timer / 30.0f;+ f->timer++;+ if (w->dodge == 1) {+ w->ox = 320.0f - bt_sinf_deg(t * 225.0f) * 100.0f;+ if (f->timer >= 12) w->ox = 220, w->dodge = 2, f->timer = 0;+ } else if (w->dodge == 2) {+ if (f->timer == 18) f->miss_t = 45;+ if (f->timer >= 33) w->dodge = 3, f->timer = 0;+ } else if (w->dodge == 3) {+ w->ox = 320.0f - bt_sinf_deg(t * 225.0f + 90.0f) * 100.0f;+ if (f->timer >= 12) {+ w->ox = 320, w->dodge = 0, f->timer = 0;+ f->hit_attempts++;+ f->strikes++;+ start_turn(f);+ }+ }+}++void fight_tick(fight_t *f, uint32_t keys, uint32_t pressed) {+ world_t *w = &f->w;+ if (f->banner_t > 0) f->banner_t--;+ if (f->miss_t > 0) f->miss_t--;+ if (f->slash_t > 0) f->slash_t--;+ if (f->bar_fade > 0) f->bar_fade--;+ switch (f->state) {+ case FS_ATTACK: {+ /* the scripts' own words, then ours */+ if (w->sc.talk && f->text_kind != TXT_BUBBLE && w->sc.talk_line >= 0)+ say(f, TALK_SCRIPT[w->sc.talk_line & 1], TXT_BUBBLE, 0);+ if (f->text_kind == TXT_BUBBLE && text_tick(f, pressed)) {+ quiet(f);+ sim_resume(w);+ pressed &= ~(uint32_t)BK_OK;+ }+ sim_tick(w, keys, pressed);+ if (f->mode == MODE_PRACTICE && !w->slam_damage && w->sc.running && !w->sc.ended) {+ int target = f->hit_attempts > 22 ? 2 : 60;+ if (w->hp - w->kr < target) {+ if (f->hit_attempts > 22) f->hit_attempts = 22;+ f->failed = 1, w->sc.ended = 1;+ }+ }+ if (w->hp <= 0) {+ die(f);+ break;+ }+ if (w->sc.ended) end_attack(f);+ break;+ }+ case FS_RETURN:+ sim_tick(w, 0, 0);+ if (w->hp <= 0) w->hp = 1;+ if (sim_zone_there(w)) {+ if (f->mode == MODE_NORMAL) open_menu(f);+ else start_turn(f);+ }+ break;+ case FS_MENU:+ sim_tick(w, 0, 0);+ if (w->hp <= 0) w->hp = 1;+ text_tick(f, 0);+ if (pressed & BK_LEFT) f->sel = (uint8_t)((f->sel + 3) & 3);+ if (pressed & BK_RIGHT) f->sel = (uint8_t)((f->sel + 1) & 3);+ heart_on_button(f);+ if (pressed & BK_OK) {+ if (f->sel == 0 || f->sel == 1) sub_open(f, SUB_ENEMY);+ else if (f->sel == 2 && f->nitems) sub_open(f, SUB_ITEM);+ else if (f->sel == 3) sub_open(f, SUB_MERCY);+ }+ break;+ case FS_SUB:+ sim_tick(w, 0, 0);+ if (w->hp <= 0) w->hp = 1;+ sub_keys(f, pressed);+ break;+ case FS_TARGET:+ sim_tick(w, 0, 0);+ if (!f->bar_hit) {+ f->bar_x += (float)f->bar_dir * 12.0f;+ bool past = f->bar_dir > 0 ? f->bar_x >= w->zr : f->bar_x <= w->zl;+ if (past) {+ f->miss_t = 30, f->bar_fade = 12;+ start_turn(f);+ } else if (pressed & BK_OK) {+ f->bar_hit = 1, f->slash_t = 18, f->bar_fade = 40;+ f->state = FS_DODGE, w->dodge = 1, f->timer = 0;+ }+ }+ break;+ case FS_DODGE:+ sim_tick(w, 0, 0);+ dodge_tick(f);+ break;+ case FS_INFO:+ sim_tick(w, 0, 0);+ if (text_tick(f, pressed)) {+ if (f->text_after == 1) say(f, CHECK_TEXT2, TXT_BOX, 0);+ else start_turn(f);+ }+ break;+ case FS_DEATH:+ f->death_t++;+ if (f->death_t == 60) {+ for (int i = 0; i < MAX_SHARDS; i++) {+ float a = (float)bt_below(&w->fx_rng, 360);+ shard_t *s = &f->shards[i];+ s->x = w->hx, s->y = w->hy;+ s->vx = bt_sinf_deg(a + 90.0f) * 6.0f, s->vy = bt_sinf_deg(a) * 6.0f;+ }+ }+ if (f->death_t > 60)+ for (int i = 0; i < MAX_SHARDS; i++) {+ shard_t *s = &f->shards[i];+ s->x += s->vx, s->y += s->vy, s->vy += 300.0f / 900.0f;+ }+ if (f->death_t >= 120) f->state = FS_LOST;+ break;+ case FS_WIN:+ sim_tick(w, 0, 0);+ if (text_tick(f, pressed)) {+ if (++f->win_line < 2) say(f, TALK_WIN[f->win_line], TXT_BUBBLE, 0);+ else quiet(f), f->state = FS_WON, f->progress = 24;+ }+ break;+ default: break;+ }+}++const char *fight_keys(const fight_t *f) {+ const world_t *w = &f->w;+ switch (f->state) {+ case FS_ATTACK:+ if (f->text_kind == TXT_BUBBLE) return "OK next";+ if (w->heart_mode == HEART_RED) return "Arrows move shift slow Back pause";+ {+ static const char *const BLUE[4] = {+ "Left jump Up Down move Back pause", "Up jump, hold for higher Left Right move Back pause",+ "Right jump Up Down move Back pause", "Down jump Left Right move Back pause"};+ return BLUE[w->heart_dir & 3];+ }+ case FS_MENU: return "Left Right choose OK select Back pause";+ case FS_SUB: return "Arrows choose OK select Back back";+ case FS_TARGET: return "OK strike";+ case FS_INFO:+ case FS_WIN: return "OK next";+ case FS_LOST:+ case FS_WON: return "OK menu";+ default: return "";+ }+}diff --git a/games/badtime/src/fight.h b/games/badtime/src/fight.hnew file mode 100644index 00000000..647e99cc--- /dev/null+++ b/games/badtime/src/fight.h@@ -0,0 +1,75 @@+/* Bad Time Simulator: the fight around the rules (bt.h): the turns, the menu, the dodge, the words, the end. */+#ifndef BT_FIGHT_H+#define BT_FIGHT_H+#include "bt.h"++enum { MODE_NORMAL, MODE_PRACTICE, MODE_ENDLESS, MODE_SINGLE };+enum {+ FS_ATTACK, /* an attack */+ FS_RETURN, /* the zone going back to the menu's */+ FS_MENU, /* FIGHT ACT ITEM MERCY */+ FS_SUB, /* a list in the box */+ FS_TARGET, /* FIGHT's bar */+ FS_DODGE, /* he dodges */+ FS_INFO, /* words in the box, then the attack */+ FS_DEATH, /* the heart breaks */+ FS_LOST, /* the end of a lost fight */+ FS_WIN, /* the last words */+ FS_WON, /* the end of a won fight */+};+enum { SUB_ENEMY, SUB_ACT, SUB_ITEM, SUB_MERCY };+enum { TXT_NONE, TXT_BOX, TXT_BUBBLE };++typedef struct {+ const char *name, *full;+ uint8_t heal;+} item_t;++#define TEXT_MAX 200+typedef struct {+ world_t w;+ uint8_t mode, state, phase, sel; /* phase: Endless' 0 or 1 */+ uint8_t sub, sub_sel, act_open, single; /* the list shown, its cursor, ACT's second list, Single attack's */+ uint8_t next_attack, hit_attempts, practice, failed;+ uint8_t items[8], nitems;+ uint8_t progress; /* the furthest attack reached (1..24) in Normal */+ uint8_t win_line, strikes, checked, last_attack;+ int16_t timer;+ /* FIGHT */+ float bar_x;+ int8_t bar_dir;+ uint8_t bar_hit;+ int16_t bar_fade;+ int16_t miss_t, slash_t;+ /* the words */+ char text[TEXT_MAX];+ uint16_t text_len, text_shown;+ uint8_t text_kind, text_after;+ const char *info; /* the box's line between attacks */+ /* SUCCESS / FAILURE (Practice) */+ int16_t banner, banner_t;+ /* the heart's pieces */+ shard_t shards[MAX_SHARDS];+ int16_t death_t;+ uint32_t seed, attacks_done;+ uint8_t heart_dirty;+} fight_t;++void fight_start(fight_t *f, int mode, int arg, uint32_t seed);+/* a tick: keys held and keys just pressed (BK_*) */+void fight_tick(fight_t *f, uint32_t keys, uint32_t pressed);+/* the key line's words for now */+const char *fight_keys(const fight_t *f);+bool fight_over(const fight_t *f); /* lost or won: the end screen */+bool fight_can_pause(const fight_t *f); /* Back pauses (not in a list, where it goes back) */+int fight_progress_of(int hit_attempts, int next_attack);+void fight_item_pos(int i, float *x, float *y); /* a list's line i: where the heart goes (the original's pixels) */++/* text.c */+extern const char *const TALK_SCRIPT[2], *const TALK_FIRST[14], *const TALK_BREAK, *const TALK_SECOND[10],+ *const TALK_FINAL, *const TALK_WIN[2];+extern const char *const INFO_DEFAULT, *const INFO_FIRST, *const INFO_KARMA, *const INFO_BREAK, *const INFO_15,+ *const INFO_19, *const INFO_20, *const INFO_21, *const INFO_22, *const CHECK_TEXT, *const CHECK_TEXT2;+extern const item_t ITEMS[4];+extern const char *const HELP[3];+#endifdiff --git a/games/badtime/src/icon.png b/games/badtime/src/icon.pngnew file mode 100644index 00000000..03ef363cBinary files /dev/null and b/games/badtime/src/icon.png differdiff --git a/games/badtime/src/main.c b/games/badtime/src/main.cnew file mode 100644index 00000000..c29197cd--- /dev/null+++ b/games/badtime/src/main.c@@ -0,0 +1,326 @@+/* Bad Time Simulator on the NumWorks calculator: the Judge's fight, a recreation of the fight in jcw87's Bad Time+ * Simulator (itself Undertale's last fight of its hardest route): 24 attacks of bones, gavels and platforms, the+ * blue heart, KR, the menu between attacks, the break and the final attack; Practice, Endless and Single attack.+ * The rules are sim.c and fight.c, the screens draw.c, the words text.c.+ *+ * Keys: the arrows move the heart (shift: slowly), OK chooses, Back goes back or pauses; Home saves and quits. */+#include <string.h>+#include "draw.h"+#include "fight.h"+#include "plat.h"+#include "save.h"++enum { V_TITLE, V_ENDLESS, V_SINGLE, V_HELP, V_FIGHT, V_PAUSE, V_CONFIRM, V_END };+enum { T_NORMAL, T_PRACTICE, T_ENDLESS, T_SINGLE, T_HELP, T_COUNT };++static bt_progress sv;+static fight_t fight;+static uint8_t view, under, sel, page, first_run, single_first, start_mode;+static int start_arg;+static uint32_t now, frame;+static bool fight_saved;++/* frames drawn, the slowest (ms) and those over 33 ms: read by tools/emu.py */+#ifndef HOST+__attribute__((used, externally_visible))+#endif+uint32_t perf_frames, perf_max, perf_slow;+#ifdef PERF+/* tools/armperf.py: an attack played with the heart unhurt, every frame's work timed */+volatile uint32_t perf_go, perf_attack, perf_done, perf_out[8];+#endif++static const char *const TITLE_ITEMS[T_COUNT] = {"Normal", "Practice", "Endless", "Single attack", "How to play"};+static const char *const ENDLESS_ITEMS[2] = {"Phase 1", "Phase 2"};+static const char *const PAUSE_ITEMS[3] = {"Resume", "How to play", "Quit game"};+static const char *const CONFIRM_ITEMS[2] = {"Keep playing", "Quit this fight"};++static char *cat(char *p, const char *s) {+ while (*s) *p++ = *s++;+ *p = 0;+ return p;+}+static char *num(char *p, unsigned v) {+ char t[10];+ int n = 0;+ do t[n++] = (char)('0' + v % 10); while ((v /= 10) && n < 10);+ while (n) *p++ = t[--n];+ *p = 0;+ return p;+}++static void save(void) { save_write(&sv); }++/* what a fight left in the record */+static void record(void) {+ bool changed = false;+ if (fight.mode == MODE_NORMAL && fight.progress > sv.best) sv.best = fight.progress, changed = true;+ if (fight.mode == MODE_NORMAL && fight.state == FS_WON && !fight_saved) sv.wins++, changed = true;+ if (fight.mode == MODE_ENDLESS && fight.attacks_done > sv.endless[fight.phase & 1])+ sv.endless[fight.phase & 1] = (uint16_t)(fight.attacks_done > 65535 ? 65535 : fight.attacks_done), changed = true;+ if (fight.state == FS_WON || fight.state == FS_LOST) fight_saved = true;+ if (changed) save();+}++static void start(int mode, int arg) {+ start_mode = (uint8_t)mode, start_arg = arg;+ if (!sv.guide) {+ /* how to play first, the first time */+ sv.guide = 1, first_run = 1;+ save();+ view = V_HELP, under = V_TITLE, page = 0;+ return;+ }+ fight_start(&fight, mode, arg, plat_random() ^ now ^ 0x5EEDu);+ fight_saved = false;+ view = V_FIGHT;+}++static void title_screen(screen_t *s) {+ static char foot[64];+ char *p = foot;+ if (sv.best) p = cat(p, "Best: attack "), p = num(p, sv.best), p = cat(p, " of 24");+ else p = cat(p, "24 attacks. Survive them all.");+ if (sv.wins) p = cat(p, " Wins: "), p = num(p, sv.wins);+ s->title = "Bad Time Simulator", s->title_y = 40;+ s->judge = true, s->judge_y = 112, s->judge_head = HEAD_DEFAULT;+ s->items = TITLE_ITEMS, s->n = T_COUNT, s->sel = sel, s->list_x = 116, s->list_y = 136, s->step = 17, s->rows = T_COUNT;+ s->foot = foot, s->keys = "Up Down choose OK start Back quit";+}++static void help_screen(screen_t *s) {+ static char t[24];+ char *p = cat(t, "How to play ");+ p = num(p, page + 1u), p = cat(p, "/3");+ s->title = t, s->title_y = 44;+ s->body = HELP[page], s->body_x = 14, s->body_y = 84;+ s->keys = first_run ? "Left Right page OK start" : "Left Right page Back done";+}++static void end_screen(screen_t *s) {+ static char body[160];+ char *p = body;+ bool won = fight.state == FS_WON;+ s->title = won ? "You won" : "You lost", s->title_y = 44;+ s->judge = true, s->judge_y = 128, s->judge_head = won ? HEAD_TIRED2 : HEAD_WINK;+ if (fight.mode == MODE_NORMAL) {+ if (won) p = cat(p, "All 24 attacks survived.");+ else p = cat(p, "Attack "), p = num(p, fight.progress), p = cat(p, " of 24.");+ } else {+ p = num(p, fight.attacks_done), p = cat(p, fight.attacks_done == 1 ? " attack survived." : " attacks survived.");+ }+ s->body = body, s->body_x = 160 - 8 * (int)strlen(body) / 3, s->body_y = 156;+ static char foot[48];+ p = foot;+ if (fight.mode == MODE_NORMAL) {+ p = cat(p, "Best: attack "), p = num(p, sv.best), p = cat(p, " of 24");+ if (sv.wins) p = cat(p, " Wins: "), p = num(p, sv.wins);+ } else if (fight.mode == MODE_ENDLESS) p = cat(p, "Best: "), p = num(p, sv.endless[fight.phase & 1]);+ else foot[0] = 0;+ s->foot = foot, s->keys = "OK menu";+}++static void single_screen(screen_t *s) {+ int rows = 9;+ if (sel < single_first) single_first = sel;+ if (sel >= single_first + rows) single_first = (uint8_t)(sel - rows + 1);+ s->title = "Single attack", s->title_y = 40;+ s->items = atk_name, s->n = N_ATTACKS, s->sel = sel, s->first = single_first, s->rows = rows;+ s->list_x = 70, s->list_y = 74, s->step = 17;+ s->more_up = single_first > 0, s->more_down = single_first + rows < N_ATTACKS;+ s->keys = "Up Down choose OK play Back back";+}++static void draw(void) {+ screen_t s;+ memset(&s, 0, sizeof s);+ s.tick = frame;+ switch (view) {+ case V_FIGHT: {+ static char t[48];+ const char *title = 0;+ if (fight.mode != MODE_NORMAL && fight.state == FS_ATTACK) {+ char *p = cat(t, fight.mode == MODE_PRACTICE ? "Practice: " : fight.mode == MODE_ENDLESS ? "Endless: " : "");+ cat(p, atk_name[fight.last_attack]);+ title = t;+ }+ draw_fight(&fight, title);+ return;+ }+ case V_PAUSE:+ case V_CONFIRM:+ s.title = view == V_PAUSE ? "Paused" : "Quit?";+ s.items = view == V_PAUSE ? PAUSE_ITEMS : CONFIRM_ITEMS, s.n = view == V_PAUSE ? 3 : 2, s.sel = sel;+ s.keys = "Up Down choose OK select Back resume";+ draw_pause(&fight, &s);+ return;+ case V_TITLE: title_screen(&s); break;+ case V_ENDLESS:+ s.title = "Endless", s.title_y = 40, s.judge = true, s.judge_y = 120, s.judge_head = HEAD_DEFAULT;+ s.items = ENDLESS_ITEMS, s.n = 2, s.sel = sel, s.list_x = 126, s.list_y = 150, s.step = 18, s.rows = 2;+ s.foot = "Attacks back to back, no menu, until you fall.";+ s.keys = "Up Down choose OK play Back back";+ break;+ case V_SINGLE: single_screen(&s); break;+ case V_HELP: help_screen(&s); break;+ case V_END: end_screen(&s); break;+ }+ draw_screen(&s);+}++static void menu_move(uint32_t hit, int n) {+ if (hit & BK_UP) sel = (uint8_t)((sel + n - 1) % n);+ if (hit & BK_DOWN) sel = (uint8_t)((sel + 1) % n);+}++/* a key on a screen other than the fight; false: quit the app */+static bool screen_key(uint32_t hit) {+ switch (view) {+ case V_TITLE:+ menu_move(hit, T_COUNT);+ if (hit & BK_BACK) return false;+ if (hit & BK_OK) {+ if (sel == T_NORMAL) start(MODE_NORMAL, 0);+ else if (sel == T_PRACTICE) start(MODE_PRACTICE, 0);+ else if (sel == T_ENDLESS) view = V_ENDLESS, sel = 0;+ else if (sel == T_SINGLE) view = V_SINGLE, sel = 0, single_first = 0;+ else view = V_HELP, under = V_TITLE, page = 0, first_run = 0;+ }+ break;+ case V_ENDLESS:+ menu_move(hit, 2);+ if (hit & BK_BACK) view = V_TITLE, sel = T_ENDLESS;+ else if (hit & BK_OK) start(MODE_ENDLESS, sel);+ break;+ case V_SINGLE:+ menu_move(hit, N_ATTACKS);+ if (hit & BK_BACK) view = V_TITLE, sel = T_SINGLE;+ else if (hit & BK_OK) start(MODE_SINGLE, sel);+ break;+ case V_HELP:+ if ((hit & BK_RIGHT) && page < 2) page++;+ else if ((hit & BK_LEFT) && page > 0) page--;+ else if ((hit & BK_OK) && first_run && page < 2) page++;+ else if (hit & (BK_BACK | BK_OK)) {+ if (first_run) {+ first_run = 0;+ start(start_mode, start_arg);+ } else if (under == V_PAUSE) view = V_PAUSE, sel = 1;+ else view = V_TITLE, sel = T_HELP;+ }+ break;+ case V_PAUSE:+ menu_move(hit, 3);+ if (hit & BK_BACK) view = V_FIGHT;+ else if (hit & BK_OK) {+ if (sel == 0) view = V_FIGHT;+ else if (sel == 1) view = V_HELP, under = V_PAUSE, page = 0, first_run = 0;+ else view = V_CONFIRM, sel = 0;+ }+ break;+ case V_CONFIRM:+ menu_move(hit, 2);+ if (hit & BK_BACK) view = V_PAUSE, sel = 2;+ else if (hit & BK_OK) {+ if (sel == 0) view = V_FIGHT;+ else record(), view = V_TITLE, sel = 0;+ }+ break;+ case V_END:+ if (hit & (BK_OK | BK_BACK)) view = V_TITLE, sel = 0;+ break;+ }+ return true;+}++int main(void) {+ plat_begin();+ save_load(&sv);+ view = V_TITLE, sel = 0;+ uint32_t stale = plat_keys(), prev = stale, pending = 0;+ now = plat_millis();+ uint32_t t0 = now, ticks = 0;+ bool dirty = true;+ for (;;) {+ now = plat_millis();+ uint32_t keys = plat_keys();+ stale &= keys; /* keys still down from the screen before don't count */+ uint32_t live = keys & ~stale, hit = live & ~prev;+ prev = live;+ if (keys & BK_HOME) break; /* Home: saved below, then out */+#ifdef PERF+ if (perf_go == 1) {+ perf_go = 2;+ fight_start(&fight, MODE_SINGLE, (int)perf_attack, 1234);+ view = V_FIGHT, t0 = now, ticks = 0;+ }+#endif+ if (view == V_FIGHT) {+ pending |= hit;+ if (hit & BK_BACK && fight_can_pause(&fight) && !fight_over(&fight)) {+ view = V_PAUSE, sel = 0, pending = 0;+ stale = keys;+ dirty = true;+ continue;+ }+ if (fight_over(&fight) && (hit & (BK_OK | BK_BACK))) {+ record();+ view = V_END;+ stale = keys;+ dirty = true;+ continue;+ }+ /* the fight at 30 ticks a second, catching up when a frame took long */+ uint32_t due = (now - t0) * 3 / 100;+ if (due > ticks + 3) ticks = due - 3;+ bool ran = false;+ uint32_t w0 = plat_millis();+ while (ticks < due) {+ fight_tick(&fight, live, pending);+ pending = 0, ticks++, ran = true;+#ifdef PERF+ if (perf_go == 2) fight.w.hp = MAX_HP, fight.w.kr = 0;+#endif+ }+ if (fight.progress > sv.best && fight.mode == MODE_NORMAL) record();+ if (fight_over(&fight) && !fight_saved) record();+ if (ran) {+ frame++;+ draw();+ uint32_t spent = plat_millis() - w0;+ perf_frames++;+ if (spent > perf_max) perf_max = spent;+ if (spent > 33) perf_slow++;+#ifdef PERF+ if (perf_go == 2) {+ perf_out[0]++, perf_out[1] += spent;+ if (spent > perf_out[2]) perf_out[2] = spent;+ if (spent > 33) perf_out[3]++;+ perf_out[4] = fight.w.nbones > perf_out[4] ? fight.w.nbones : perf_out[4];+ perf_out[5] = fight.w.nblasters > perf_out[5] ? fight.w.nblasters : perf_out[5];+ if (fight.attacks_done >= 1) perf_go = 3, perf_done = 1;+ }+#endif+ } else plat_sleep(4);+ continue;+ }+ /* the other screens */+ if (hit) {+ uint8_t before = view;+ if (!screen_key(hit)) break;+ if (view == V_FIGHT && before != V_FIGHT) t0 = now, ticks = 0, stale = keys, prev = 0, pending = 0;+ dirty = true;+ }+ if (view == V_TITLE || view == V_ENDLESS || view == V_END) {+ /* (the Judge breathes) */+ if ((now - t0) / 33 != frame) frame = (now - t0) / 33, dirty = true;+ }+ if (dirty) {+ draw();+ dirty = false;+ } else plat_sleep(10);+ }+ if (view == V_FIGHT || view == V_PAUSE || view == V_CONFIRM) record();+ save();+ return plat_end();+}diff --git a/games/badtime/src/plat.h b/games/badtime/src/plat.hnew file mode 100644index 00000000..65e90f97--- /dev/null+++ b/games/badtime/src/plat.h@@ -0,0 +1,16 @@+/* What the game needs from the calculator (plat_eadk.c), or from a computer for the tests. Keys are bt.h's BK_*. */+#ifndef BT_PLAT_H+#define BT_PLAT_H+#include <stdbool.h>+#include <stdint.h>++uint32_t plat_keys(void);+uint32_t plat_millis(void);+void plat_sleep(uint32_t ms);+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);+uint32_t plat_random(void);+void plat_begin(void);+int plat_end(void);+#endifdiff --git a/games/badtime/src/plat_eadk.c b/games/badtime/src/plat_eadk.cnew file mode 100644index 00000000..be021e3b--- /dev/null+++ b/games/badtime/src/plat_eadk.c@@ -0,0 +1,35 @@+/* The calculator: keys, time, the screen and files (EADK, and NumPlay's careful file helpers). */+#include <eadk.h>+#include "../../common/epsilon_app.h"+#include "../../common/epsilon_files.h"+#include "bt.h"+#include "plat.h"++const char eadk_app_name[] __attribute__((section(".rodata.eadk_app_name"))) = "Bad Time";+const uint32_t eadk_api_level __attribute__((section(".rodata.eadk_api_level"))) = 0;++#define KEY(k) ((uint64_t)1 << (k))++uint32_t plat_keys(void) {+ uint64_t k = eadk_keyboard_scan();+ uint32_t r = 0;+ if (k & (KEY(eadk_key_ok) | KEY(eadk_key_exe) | KEY(eadk_key_five))) r |= BK_OK;+ if (k & KEY(eadk_key_shift)) r |= BK_SLOW;+ if (k & (KEY(eadk_key_up) | KEY(eadk_key_eight))) r |= BK_UP;+ if (k & (KEY(eadk_key_down) | KEY(eadk_key_two))) r |= BK_DOWN;+ if (k & (KEY(eadk_key_left) | KEY(eadk_key_four))) r |= BK_LEFT;+ if (k & (KEY(eadk_key_right) | KEY(eadk_key_six))) r |= BK_RIGHT;+ if (k & (KEY(eadk_key_back) | KEY(eadk_key_backspace))) r |= BK_BACK;+ if (k & (KEY(eadk_key_home) | KEY(eadk_key_on_off))) r |= BK_HOME;+ return r;+}+uint32_t plat_millis(void) { return (uint32_t)eadk_timing_millis(); }+void plat_sleep(uint32_t ms) { eadk_timing_msleep(ms); }+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}, (const eadk_color_t *)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); }+uint32_t plat_random(void) { return eadk_random(); }+void plat_begin(void) { np_app_begin(); }+int plat_end(void) { return np_app_end(); }diff --git a/games/badtime/src/save.c b/games/badtime/src/save.cnew file mode 100644index 00000000..4a727ac6--- /dev/null+++ b/games/badtime/src/save.c@@ -0,0 +1,114 @@+/* The save (save.h): a bytes file with a magic, a version, its size and a checksum, its numbers checked; anything+ * else (damaged, cut short, from elsewhere) is not read: the copy in the Python script is tried, then a fresh+ * start. */+#include <stddef.h>+#include <string.h>+#include "plat.h"+#include "save.h"++void bt_fresh(bt_progress *p) { memset(p, 0, sizeof *p); }++uint32_t bt_sum(const void *data, uint32_t n) {+ const uint8_t *p = data;+ uint32_t h = 2166136261u;+ for (uint32_t i = 0; i < n; i++) h = (h ^ p[i]) * 16777619u;+ return h;+}++bool bt_file_ok(const bt_file *f) {+ if (f->magic != BT_MAGIC || f->version != BT_VERSION || f->size != sizeof *f) return false;+ if (f->sum != bt_sum(f, offsetof(bt_file, sum))) return false;+ const bt_progress *p = &f->p;+ return p->best <= 24 && p->guide <= 1;+}++static const char head[] = "# Bad Time Simulator keeps a copy of your record here,\n"+ "# so that installing it again doesn't erase it.\n";+static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";+static uint8_t copy[1 + sizeof head + 2 + sizeof BT_SAVE_NAME + 1 + (sizeof(bt_file) + 2) / 3 * 4 + 2];++/* the copy: the file in base 64, in a comment line of a script */+static bool copy_write(const bt_file *f) {+ const uint8_t *bytes = (const uint8_t *)f;+ uint32_t len = sizeof *f, nl = sizeof BT_SAVE_NAME - 1;+ uint8_t *p = copy;+ *p++ = 0; /* (a script's first byte: not imported) */+ memcpy(p, head, sizeof head - 1), p += sizeof head - 1;+ *p++ = '#', *p++ = '>';+ memcpy(p, BT_SAVE_NAME, nl), p += nl;+ *p++ = ':';+ for (uint32_t i = 0; i < len; i += 3, p += 4) {+ uint32_t left = len - i;+ uint32_t v = (uint32_t)bytes[i] << 16 | (left > 1 ? (uint32_t)bytes[i + 1] << 8 : 0) | (left > 2 ? bytes[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';+ return plat_save(BT_COPY_NAME, copy, (uint32_t)(p - copy));+}++static int b64v(uint8_t c) {+ for (int i = 0; i < 64; i++)+ if ((uint8_t)b64[i] == c) return i;+ return -1;+}++static bool copy_read(bt_file *f) {+ uint32_t n, nl = sizeof BT_SAVE_NAME - 1;+ const uint8_t *c = plat_load(BT_COPY_NAME, &n);+ if (!c) return false;+ for (uint32_t i = 0; i + nl + 3 <= n; i++) {+ if (c[i] != '#' || c[i + 1] != '>' || memcmp(c + i + 2, BT_SAVE_NAME, nl) || c[i + 2 + nl] != ':') continue;+ const uint8_t *l = c + i + 3 + nl;+ uint32_t m = 0;+ while (l + m < c + n && l[m] != '\n' && l[m]) m++;+ if (m != (sizeof *f + 2) / 3 * 4) return false;+ uint8_t *bytes = (uint8_t *)f;+ uint32_t o = 0;+ for (uint32_t k = 0; k < m; k += 4) {+ int v0 = b64v(l[k]), v1 = b64v(l[k + 1]), v2 = l[k + 2] == '=' ? 0 : b64v(l[k + 2]),+ v3 = l[k + 3] == '=' ? 0 : b64v(l[k + 3]);+ if (v0 < 0 || v1 < 0 || v2 < 0 || v3 < 0) return false;+ uint32_t w = (uint32_t)(v0 << 18 | v1 << 12 | v2 << 6 | v3);+ if (o < sizeof *f) bytes[o++] = (uint8_t)(w >> 16);+ if (o < sizeof *f) bytes[o++] = (uint8_t)(w >> 8);+ if (o < sizeof *f) bytes[o++] = (uint8_t)w;+ }+ return true;+ }+ return false;+}++static bt_file file; /* (static: the stack stays small) */++bool save_load(bt_progress *p) {+ uint32_t n;+ const uint8_t *d = plat_load(BT_SAVE_NAME, &n);+ bool ok = false;+ if (d && n == sizeof file) {+ memcpy(&file, d, sizeof file);+ ok = bt_file_ok(&file);+ }+ if (!ok && copy_read(&file) && bt_file_ok(&file)) {+ ok = true;+ plat_save(BT_SAVE_NAME, &file, sizeof file); /* back where it belongs */+ }+ if (!ok) {+ bt_fresh(p);+ return false;+ }+ *p = file.p;+ return true;+}++bool save_write(const bt_progress *p) {+ memset(&file, 0, sizeof file);+ file.magic = BT_MAGIC, file.version = BT_VERSION, file.size = sizeof file;+ file.p = *p;+ file.sum = bt_sum(&file, offsetof(bt_file, sum));+ bool ok = plat_save(BT_SAVE_NAME, &file, sizeof file);+ copy_write(&file);+ return ok;+}diff --git a/games/badtime/src/save.h b/games/badtime/src/save.hnew file mode 100644index 00000000..ad1f8327--- /dev/null+++ b/games/badtime/src/save.h@@ -0,0 +1,35 @@+/* What's kept (the furthest attack reached, the wins, how to play read, Endless' best runs), in badtime.sav, and a+ * copy of it in a Python script, badtime_saves.py (installing apps from the NumWorks website keeps only scripts,+ * so the save comes back from it). */+#ifndef BT_SAVE_H+#define BT_SAVE_H+#include <stdbool.h>+#include <stdint.h>++#define BT_SAVE_NAME "badtime.sav"+#define BT_COPY_NAME "badtime_saves.py"+#define BT_MAGIC 0x31535442u /* "BTS1" */+#define BT_VERSION 1++typedef struct {+ uint8_t best; /* the furthest attack reached in a whole fight, 0 (none) to 24 */+ uint8_t guide; /* how to play shown before the first fight */+ uint16_t wins; /* fights won */+ uint16_t endless[2]; /* the most attacks dodged in a row in Endless, phase 1 and 2 */+ uint8_t spare[4];+} bt_progress;++typedef struct {+ uint32_t magic;+ uint16_t version, size;+ bt_progress p;+ uint32_t sum; /* FNV-1a of everything before it */+} bt_file;+_Static_assert(sizeof(bt_file) == 24, "badtime.sav: 24 bytes");++void bt_fresh(bt_progress *p);+uint32_t bt_sum(const void *data, uint32_t n);+bool bt_file_ok(const bt_file *f);+bool save_load(bt_progress *p); /* the save, its copy, or a fresh start; false: a fresh start */+bool save_write(const bt_progress *p);+#endifdiff --git a/games/badtime/src/sim.c b/games/badtime/src/sim.cnew file mode 100644index 00000000..15032ee5--- /dev/null+++ b/games/badtime/src/sim.c@@ -0,0 +1,876 @@+/* Bad Time Simulator: the fight's rules (bt.h), tick by tick: the attack scripts, the zone, the heart, the bones,+ * blasters and platforms, the damage and KR. The numbers are jcw87's (Bad Time Simulator's event sheets), in+ * the original's 640 x 480 pixels and seconds, at 30 ticks a second. */+#include <stddef.h>+#include <stdio.h>+#include <string.h>+#include "bt.h"++void (*sim_log)(const char *line);++/* ---------------------------------------------------------------- sums that come out the same everywhere */+#define PI 3.14159265358979323846+#define D2R (PI / 180.0)++double bt_floor(double x) {+ if (!(x > -4.0e15 && x < 4.0e15)) return x != x ? 0.0 : x; /* (NaN: 0; huge: already whole) */+ long long i = (long long)x;+ double f = (double)i;+ return f > x ? f - 1.0 : f;+}++/* sin of d degrees: the angle brought to [0, 90], then a polynomial (the same steps as tools/attacks.py) */+double bt_sin_deg(double d) {+ d = d - 360.0 * bt_floor(d / 360.0);+ if (d == 0.0 || d == 180.0) return 0.0;+ if (d == 90.0) return 1.0;+ if (d == 270.0) return -1.0;+ double s = 1.0;+ if (d >= 180.0) d = d - 180.0, s = -1.0;+ if (d > 90.0) d = 180.0 - d;+ double x = d * D2R, x2 = x * x;+ double p = -2.5052108385441720e-08;+ p = p * x2 + 2.7557319223985893e-06;+ p = p * x2 - 1.9841269841269841e-04;+ p = p * x2 + 8.3333333333333333e-03;+ p = p * x2 - 1.6666666666666666e-01;+ p = p * x2 + 1.0;+ return s * (x * p);+}+double bt_cos_deg(double d) { return bt_sin_deg(d + 90.0); }++static double atan_unit(double t) {+ int big = t > 0.4142135623730950;+ if (big) t = (t - 1.0) / (t + 1.0);+ double t2 = t * t;+ double p = 0.0769230769230769;+ p = p * t2 - 0.0833333333333333;+ p = p * t2 + 0.0909090909090909;+ p = p * t2 - 0.1;+ p = p * t2 + 0.1111111111111111;+ p = p * t2 - 0.1428571428571428;+ p = p * t2 + 0.2;+ p = p * t2 - 0.3333333333333333;+ double r = t + t * t2 * p;+ if (big) r = r + 0.7853981633974483;+ return r;+}+double bt_atan2_deg(double y, double x) {+ if (x == 0.0 && y == 0.0) return 0.0;+ double ax = x < 0 ? -x : x, ay = y < 0 ? -y : y, a;+ if (ay <= ax) a = atan_unit(ay / ax);+ else a = 1.5707963267948966 - atan_unit(ax / ay);+ if (x < 0.0) a = 3.141592653589793 - a;+ if (y < 0.0) a = -a;+ return a / D2R;+}++/* the same in floats, for the look and the moves (a turn of 360) */+float bt_sinf_deg(float d) {+ d = d - 360.0f * (float)bt_ifloor(d / 360.0f);+ float s = 1.0f;+ if (d >= 180.0f) d -= 180.0f, s = -1.0f;+ if (d > 90.0f) d = 180.0f - d;+ float x = d * (float)D2R, x2 = x * x;+ float p = 2.7557319e-06f;+ p = p * x2 - 1.9841270e-04f;+ p = p * x2 + 8.3333333e-03f;+ p = p * x2 - 1.6666667e-01f;+ p = p * x2 + 1.0f;+ return s * x * p;+}+static float cosf_deg(float d) { return bt_sinf_deg(d + 90.0f); }+static float absf(float x) { return x < 0 ? -x : x; }+static float minf(float a, float b) { return a < b ? a : b; }++uint32_t bt_rand(uint32_t *s) {+ uint32_t x = *s ? *s : 1;+ x ^= x << 13;+ x ^= x >> 17;+ x ^= x << 5;+ return *s = x;+}+uint32_t bt_below(uint32_t *s, uint32_t n) { return n ? (uint32_t)(((uint64_t)bt_rand(s) * n) >> 32) : 0; }++/* ---------------------------------------------------------------- the log, while checking */+static const signed char DX[4] = {1, 0, -1, 0}, DY[4] = {0, 1, 0, -1};++static void logf_(const world_t *w, const char *op, int n, const int *v) {+ if (!w->checking || !sim_log) return;+ char line[160];+ int p = snprintf(line, sizeof line, "%u %s", (unsigned)w->tick, op);+ for (int i = 0; i < n && p < (int)sizeof line - 12; i++) p += snprintf(line + p, sizeof line - (size_t)p, " %d", v[i]);+ sim_log(line);+}+static int ims(double v) { return (int)bt_floor(v * 1000.0 + 0.5); }+static int ifl(double v) {+ v = bt_floor(v);+ return v > 2.0e9 ? 2000000000 : v < -2.0e9 ? -2000000000 : (int)v;+}++/* ---------------------------------------------------------------- the zone */+#define MENU_L 33.0f+#define MENU_T 251.0f+#define MENU_R 608.0f+#define MENU_B 391.0f++void sim_zone_to(world_t *w, float l, float t, float r, float b, bool finish) {+ w->tl = l, w->tt = t, w->tr = r, w->tb = b;+ w->sc.finish = finish;+ w->zone_hidden = 0;+}+bool sim_zone_there(const world_t *w) { return w->zl == w->tl && w->zt == w->tt && w->zr == w->tr && w->zb == w->tb; }++static float step_to(float v, float to, float step) {+ float d = to - v;+ if (d > 0) return v + minf(step, d);+ if (d < 0) return v - minf(step, -d);+ return v;+}++/* the heart pushed back into the zone */+static void heart_clamp(world_t *w) {+ if (!w->heart_on) return;+ if (w->hx + 8 <= w->zl || w->hx - 8 >= w->zr || w->hy + 8 <= w->zt || w->hy - 8 >= w->zb) return;+ if (w->zl + 5 > w->hx - 8) w->hx = w->zl + 13;+ if (w->zt + 5 > w->hy - 8) w->hy = w->zt + 13;+ if (w->zr - 5 < w->hx + 8) w->hx = w->zr - 13;+ if (w->zb - 5 < w->hy + 8) w->hy = w->zb - 13;+}++static void zone_tick(world_t *w) {+ float s = w->zone_speed / 30.0f;+ w->zl = step_to(w->zl, w->tl, s);+ w->zt = step_to(w->zt, w->tt, s);+ w->zr = step_to(w->zr, w->tr, s);+ w->zb = step_to(w->zb, w->tb, s);+ if (w->sc.finish && sim_zone_there(w)) {+ w->sc.finish = 0;+ w->sc.running = 1;+ }+ if (w->sc.talk && w->checking) { /* (checking: said and read at once) */+ w->sc.talk = 0;+ w->sc.running = 1;+ }+ heart_clamp(w);+}++/* ---------------------------------------------------------------- things */+void sim_clear_attacks(world_t *w) { w->nbones = w->nstabs = w->nblasters = w->nplats = 0; }++void sim_shake(world_t *w, int n) {+ w->shake = (int16_t)n;+ w->shake_t = 0;+}++static void add_bone(world_t *w, bool vertical, int x, int y, int len, int dir, int speed, int color) {+ int v[6] = {x, y, len, dir, speed, color};+ logf_(w, vertical ? "BoneV" : "BoneH", 6, v);+ if (w->nbones >= MAX_BONES) return;+ bone_t *b = &w->bones[w->nbones++];+ memset(b, 0, sizeof *b);+ b->x = (float)x, b->y = (float)y;+ b->w = vertical ? 10.0f : (float)len;+ b->h = vertical ? (float)len : 10.0f;+ b->dir = (uint8_t)(dir & 3);+ b->vx = (float)(DX[dir & 3] * speed) / 30.0f;+ b->vy = (float)(DY[dir & 3] * speed) / 30.0f;+ b->color = (uint8_t)(color >= 0 && color <= 2 ? color : 0);+ b->karma = 6;+ b->vertical = vertical;+}++static void add_plat(world_t *w, int x, int y, int width, int dir, int speed, int reverse) {+ int v[6] = {x, y, width, dir, speed, reverse};+ logf_(w, "Platform", 6, v);+ if (w->nplats >= MAX_PLATS) return;+ plat_t *p = &w->plats[w->nplats++];+ memset(p, 0, sizeof *p);+ p->x = (float)x, p->y = (float)y, p->w = (float)width;+ p->dir = (uint8_t)(dir & 3);+ p->vx = (float)(DX[dir & 3] * speed) / 30.0f;+ p->reverse = reverse > 0;+}++static void add_stab(world_t *w, int dir, int distance, double warn, double stay) {+ int v[4] = {dir, distance, ims(warn), ims(stay)};+ logf_(w, "BoneStab", 4, v);+ if (dir < 0 || dir > 3 || w->nstabs >= MAX_STABS) return;+ stab_t *s = &w->stabs[w->nstabs++];+ memset(s, 0, sizeof *s);+ float d = (float)distance, zw = w->zr - w->zl, zh = w->zb - w->zt;+ s->dir = (uint8_t)dir, s->distance = d, s->karma = 6, s->state = STAB_WARN;+ s->warn = (int16_t)ifl(warn * 30.0 + 0.5);+ s->stay = (int16_t)ifl(stay * 30.0 + 0.5);+ if (dir == DIR_RIGHT) s->w = d - 3, s->h = zh - 16, s->x = w->zr - s->w - 8, s->y = w->zt + 8;+ else if (dir == DIR_DOWN) s->w = zw - 16, s->h = d - 3, s->x = w->zl + 8, s->y = w->zb - s->h - 8;+ else if (dir == DIR_LEFT) s->w = d - 3, s->h = zh - 16, s->x = w->zl + 8, s->y = w->zt + 8;+ else s->w = zw - 16, s->h = d - 3, s->x = w->zl + 8, s->y = w->zt + 8;+}++/* the warning's over: the wall of bones, just out of sight, slides in */+static void stab_go(world_t *w, stab_t *s) {+ float d = s->distance, zw = w->zr - w->zl, zh = w->zb - w->zt;+ if (s->dir == DIR_DOWN || s->dir == DIR_UP) {+ s->x = w->zl, s->w = zw, s->h = d + 8, s->dest_x = s->x;+ if (s->dir == DIR_DOWN) s->y = w->zb - 5, s->dest_y = w->zb - 5 - d;+ else s->y = w->zt + 5 - s->h, s->dest_y = s->y + d;+ } else {+ s->y = w->zt, s->h = zh, s->w = d + 8, s->dest_y = s->y;+ if (s->dir == DIR_RIGHT) s->x = w->zr - 5, s->dest_x = w->zr - 5 - d;+ else s->x = w->zl + 5 - s->w, s->dest_x = s->x + d;+ }+ s->speed = d * 10.0f / 30.0f;+ s->state = STAB_IN;+}++static void add_blaster(world_t *w, int size, int x, int y, int ex, int ey, int ea, double spin, double blast) {+ int v[8] = {size, x, y, ex, ey, ea, ims(spin), ims(blast)};+ logf_(w, "GasterBlaster", 8, v);+ if (w->nblasters >= MAX_BLASTERS) return;+ blaster_t *b = &w->blasters[w->nblasters++];+ memset(b, 0, sizeof *b);+ b->x = (float)x, b->y = (float)y, b->end_x = (float)ex, b->end_y = (float)ey;+ b->ang = 90.0f, b->end_ang = (float)ea;+ b->size = (uint8_t)(size < 0 ? 0 : size > 2 ? 2 : size);+ b->sx = b->size == 0 ? 1.0f : (float)(b->size + 1); /* across */+ b->sy = b->size == 0 ? 2.0f : (float)(b->size + 1); /* along */+ b->scale = b->sx;+ b->timer = (int16_t)ifl(spin * 30.0 + 0.5);+ b->blast = (int16_t)ifl(blast * 30.0 + 0.5);+ b->karma = 10, b->opacity = 100;+ if (b->x == b->end_x && b->y == b->end_y) b->ang = b->end_ang;+}++void blaster_beam(const blaster_t *b, float from, float to, float half, float qx[4], float qy[4]) {+ float c = cosf_deg(b->ang), s = bt_sinf_deg(b->ang);+ float ax = b->x + c * from, ay = b->y + s * from, bx = b->x + c * to, by = b->y + s * to;+ qx[0] = ax - s * half, qy[0] = ay + c * half;+ qx[1] = bx - s * half, qy[1] = by + c * half;+ qx[2] = bx + s * half, qy[2] = by - c * half;+ qx[3] = ax + s * half, qy[3] = ay - c * half;+}++void sim_menu_bones(world_t *w, bool left, bool bottom) {+ /* the left one slides back out first; the bottom ones stop coming */+ for (int i = 0; i < w->nmbones; i++)+ if (w->mbones[i].kind == MB_LEFT && !left) w->mbones[i].state = 1;+ if (left) {+ bool have = false;+ for (int i = 0; i < w->nmbones; i++) have |= w->mbones[i].kind == MB_LEFT && !w->mbones[i].state;+ if (!have && w->nmbones < MAX_MBONES) {+ mbone_t *m = &w->mbones[w->nmbones++];+ memset(m, 0, sizeof *m);+ m->kind = MB_LEFT, m->x = -10, m->y = 270;+ }+ }+ w->menu_bottom = bottom;+ if (bottom) w->bottom_t = 0, w->bottom_alt = 0;+}++/* the buttons' edges (FIGHT, ACT, ITEM, MERCY) */+static const float BUTTON_X[4] = {32, 184, 344, 496};+#define BUTTON_W 110.0f++static void mbones_tick(world_t *w) {+ if (w->menu_bottom) {+ w->bottom_t += 1.0f / 30.0f;+ if (w->bottom_t >= 0.6f) {+ w->bottom_t -= 0.6f;+ for (int k = 0; k < 2 && w->nmbones < MAX_MBONES; k++) {+ mbone_t *m = &w->mbones[w->nmbones++];+ memset(m, 0, sizeof *m);+ m->kind = MB_BOTTOM, m->button = (uint8_t)(k * 2 + w->bottom_alt);+ m->x = BUTTON_X[m->button] + BUTTON_W, m->y = LAYOUT_H;+ }+ w->bottom_alt ^= 1;+ }+ }+ int n = 0;+ for (int i = 0; i < w->nmbones; i++) {+ mbone_t m = w->mbones[i];+ bool keep = true;+ if (m.kind == MB_LEFT) {+ m.t += 1.0f / 30.0f;+ m.x = -30.0f + absf(bt_sinf_deg(600.0f * m.t / (float)PI)) * 105.0f;+ if (m.x > 64.0f) m.t -= 0.72f / 30.0f;+ if (m.state && m.x <= -8.0f) keep = false;+ } else {+ if (m.state == 0) {+ m.y -= 10.0f;+ if (m.y <= 440.0f) m.y = 440.0f, m.state = 1;+ } else if (m.state == 1) {+ m.x -= 5.0f;+ if (m.x <= BUTTON_X[m.button] - 14.0f) m.x = BUTTON_X[m.button] - 14.0f, m.state = 2;+ } else {+ m.y += 10.0f;+ if (m.y > LAYOUT_H) keep = false;+ }+ }+ if (keep) w->mbones[n++] = m;+ }+ w->nmbones = (uint16_t)n;+}++static void bones_tick(world_t *w) {+ int n = 0;+ for (int i = 0; i < w->nbones; i++) {+ bone_t b = w->bones[i];+ b.x += b.vx, b.y += b.vy;+ bool gone = (b.dir == DIR_RIGHT && b.x > LAYOUT_W) || (b.dir == DIR_DOWN && b.y > LAYOUT_H) ||+ (b.dir == DIR_LEFT && b.x < -b.w) || (b.dir == DIR_UP && b.y < -b.h);+ if (!gone) w->bones[n++] = b;+ }+ w->nbones = (uint16_t)n;+}++static bool outside_layout(float x, float y, float rw, float rh) {+ return x + rw < 0 || y + rh < 0 || x > LAYOUT_W || y > LAYOUT_H;+}++static void stabs_tick(world_t *w) {+ int n = 0;+ for (int i = 0; i < w->nstabs; i++) {+ stab_t s = w->stabs[i];+ bool keep = true;+ if (s.state == STAB_WARN) {+ if (s.warn <= 0) stab_go(w, &s);+ else s.warn--;+ } else if (s.state == STAB_IN || s.state == STAB_STAY) {+ if (s.state == STAB_IN) {+ float px = s.x - DX[s.dir] * s.speed, py = s.y - DY[s.dir] * s.speed;+ /* past where it stops: there */+ bool past = (DX[s.dir] < 0 && px >= s.dest_x) || (DX[s.dir] > 0 && px <= s.dest_x) ||+ (DY[s.dir] < 0 && py >= s.dest_y) || (DY[s.dir] > 0 && py <= s.dest_y);+ if (past) px = s.dest_x, py = s.dest_y, s.state = STAB_STAY;+ s.x = px, s.y = py;+ }+ if (s.state == STAB_STAY) {+ if (s.stay > 0) s.stay--;+ if (s.stay <= 0) s.state = STAB_OUT;+ }+ } else {+ s.x += DX[s.dir] * s.speed, s.y += DY[s.dir] * s.speed;+ if (outside_layout(s.x, s.y, s.w, s.h)) keep = false;+ }+ if (keep) w->stabs[n++] = s;+ }+ w->nstabs = (uint16_t)n;+}++static void blasters_tick(world_t *w) {+ int n = 0;+ for (int i = 0; i < w->nblasters; i++) {+ blaster_t b = w->blasters[i];+ bool keep = true;+ if (b.timer > 0 && (b.state == BL_WAIT || b.state == BL_FIRE)) b.timer--;+ if (b.state == BL_ENTER) {+ if (absf(b.x - b.end_x) >= 3) b.x += (b.end_x - b.x) / 3.0f;+ if (absf(b.x - b.end_x) < 3) b.x = b.end_x;+ if (absf(b.y - b.end_y) >= 3) b.y += (b.end_y - b.y) / 3.0f;+ if (absf(b.y - b.end_y) < 3) b.y = b.end_y;+ if (absf(b.ang - b.end_ang) >= 3) b.ang += (b.end_ang - b.ang) / 3.0f;+ if (absf(b.ang - b.end_ang) < 3) b.ang = b.end_ang;+ if (b.x == b.end_x && b.y == b.end_y && b.ang == b.end_ang) b.state = BL_WAIT;+ }+ if (b.state == BL_WAIT && b.timer == 0) b.state = BL_FIRE, b.timer = 3;+ else if (b.state == BL_FIRE && b.timer == 0) {+ b.state = BL_LEAVE;+ b.hurts = 1;+ if (b.size >= 1) sim_shake(w, 5);+ }+ if (b.state == BL_LEAVE) {+ b.leave += 30.0f;+ float r = 28.5f * (b.sy > b.sx ? b.sy : b.sx);+ if (outside_layout(b.x - r, b.y - r, r * 2, r * 2)) b.leave = 0;+ b.x -= cosf_deg(b.ang) * b.leave / 30.0f;+ b.y -= bt_sinf_deg(b.ang) * b.leave / 30.0f;+ /* the beam */+ b.beam_t++;+ float full = 35.0f * b.scale;+ if (b.beam_t < 4) b.base += (float)(int)(full / 4.0f);+ else if (b.beam_t == 4) b.base = full;+ if (b.beam_t > 5 + b.blast) {+ b.base *= 0.8f;+ int op = 100 - ((b.beam_t - b.blast) - 5) * 10;+ b.opacity = (uint8_t)(op < 0 ? 0 : op);+ if (b.base <= 2.0f) keep = false;+ }+ if (b.opacity <= 80) b.hurts = 0;+ b.sine = bt_sinf_deg((float)b.beam_t * (2.0f / 3.0f) * (float)(180.0 / PI)) * b.base / 4.0f;+ }+ if (keep) w->blasters[n++] = b;+ }+ w->nblasters = (uint16_t)n;+}++static void plats_tick(world_t *w) {+ int n = 0;+ for (int i = 0; i < w->nplats; i++) {+ plat_t p = w->plats[i];+ p.x += p.vx;+ if (p.reverse) {+ if (p.dir == DIR_RIGHT && p.x + p.w >= w->zr) p.dir = DIR_LEFT, p.vx = -p.vx;+ else if (p.dir == DIR_LEFT && p.x <= w->zl) p.dir = DIR_RIGHT, p.vx = -p.vx;+ }+ bool gone = (p.vx > 0 && p.x > LAYOUT_W + 200) || (p.vx < 0 && p.x + p.w < -200);+ if (!gone) w->plats[n++] = p;+ }+ w->nplats = (uint16_t)n;+}++/* ---------------------------------------------------------------- the heart */+void sim_heart_mode(world_t *w, int mode) {+ w->heart_mode = (uint8_t)(mode == HEART_BLUE);+ w->heart_dir = DIR_DOWN;+}++/* a 16 x 16 heart at (x, y) against the zone's border */+static bool in_wall(const world_t *w, float x, float y) {+ return x - 8 < w->zl + 5 || x + 8 > w->zr - 5 || y - 8 < w->zt + 5 || y + 8 > w->zb - 5;+}+/* ... and against a platform it's falling onto (gravity up or down only: they're jump-through) */+static const plat_t *on_plat(const world_t *w, float x, float y, float vy) {+ if (w->heart_mode != HEART_BLUE || (w->heart_dir != DIR_DOWN && w->heart_dir != DIR_UP)) return NULL;+ for (int i = 0; i < w->nplats; i++) {+ const plat_t *p = &w->plats[i];+ if (x + 8 <= p->x || x - 8 >= p->x + p->w || y + 8 <= p->y || y - 8 >= p->y + 7) continue;+ if (w->heart_dir == DIR_DOWN && p->y > y && vy >= 0 && y + 8 <= p->y + 2) return p;+ if (w->heart_dir == DIR_UP && p->y < y && vy <= 0 && y - 8 >= p->y + 7 - 2) return p;+ }+ return NULL;+}+static bool solid(const world_t *w, float x, float y, float vy) { return in_wall(w, x, y) || on_plat(w, x, y, vy); }++/* the heart lands: a slam's end */+static void landed(world_t *w, float speed) {+ if (!w->slammed) return;+ w->slammed = 0;+ if (absf(speed) >= 330.0f) {+ sim_shake(w, (int)(absf(speed) / 90.0f));+ if (w->slam_damage && w->hp > 1) w->hp--;+ }+}++/* moves along one axis, stopping flush against the zone's border or on a platform it lands on */+static void move_axis(world_t *w, bool vertical) {+ float v = vertical ? w->hvy : w->hvx, d = v / 30.0f;+ if (d == 0) return;+ float p0 = vertical ? w->hy : w->hx, p1 = p0 + d;+ float lo = vertical ? w->zt + 13 : w->zl + 13, hi = vertical ? w->zb - 13 : w->zr - 13;+ bool hit = false;+ if (d > 0 && p0 <= hi && p1 > hi) p1 = hi, hit = true;+ if (d < 0 && p0 >= lo && p1 < lo) p1 = lo, hit = true;+ /* platforms: landed on from the side gravity pulls to (they're jump-through) */+ if (vertical && w->heart_mode == HEART_BLUE && (w->heart_dir == DIR_DOWN || w->heart_dir == DIR_UP)) {+ bool down = w->heart_dir == DIR_DOWN;+ for (int i = 0; i < w->nplats; i++) {+ const plat_t *p = &w->plats[i];+ if (w->hx + 8 <= p->x || w->hx - 8 >= p->x + p->w) continue;+ if (down && d > 0 && p0 + 8 <= p->y + 2 && p1 + 8 > p->y && p->y - 8.05f < p1) p1 = p->y - 8.05f, hit = true;+ if (!down && d < 0 && p0 - 8 >= p->y + 5 && p1 - 8 < p->y + 7 && p->y + 15.05f > p1) p1 = p->y + 15.05f, hit = true;+ }+ }+ if (vertical) w->hy = p1;+ else w->hx = p1;+ if (hit) {+ landed(w, v);+ if (vertical) w->hvy = 0;+ else w->hvx = 0;+ }+}++static void heart_tick(world_t *w, uint32_t keys, uint32_t pressed) {+ if (!w->heart_on || !w->heart_free) return;+ float speed = keys & BK_SLOW ? 75.0f : 150.0f;+ bool l = keys & BK_LEFT, r = keys & BK_RIGHT, u = keys & BK_UP, dn = keys & BK_DOWN;+ if (w->heart_mode == HEART_RED) {+ w->hvx = l == r ? 0 : l ? -speed : speed;+ w->hvy = u == dn ? 0 : u ? -speed : speed;+ } else {+ int g = w->heart_dir;+ float gx = (float)DX[g], gy = (float)DY[g];+ /* the jump key is the one against gravity */+ static const uint32_t JUMP[4] = {BK_LEFT, BK_UP, BK_RIGHT, BK_DOWN};+ uint32_t jk = JUMP[g];+ if ((pressed & jk) && solid(w, w->hx + gx, w->hy + gy, w->hvy)) w->hvx -= gx * 180.0f, w->hvy -= gy * 180.0f;+ if (!(keys & jk) && w->jump_held) {+ float along = w->hvx * gx + w->hvy * gy;+ if (along < -30.0f) {+ if (gx != 0) w->hvx = -30.0f * gx;+ else w->hvy = -30.0f * gy;+ }+ }+ w->jump_held = (keys & jk) != 0;+ float down = w->hvx * gx + w->hvy * gy, grav = 0;+ if (down < 240 && down > 15) grav = 540;+ else if (down <= 15 && down > -30) grav = 180;+ else if (down <= -30 && down > -120) grav = 450;+ else if (down <= -120) grav = 180;+ if (!solid(w, w->hx + gx * 0.2f, w->hy + gy * 0.2f, w->hvy)) {+ w->hvx += gx * grav / 30.0f, w->hvy += gy * grav / 30.0f;+ float m = w->max_fall;+ if (g == DIR_RIGHT && w->hvx > m) w->hvx = m;+ if (g == DIR_DOWN && w->hvy > m) w->hvy = m;+ if (g == DIR_LEFT && w->hvx < -m) w->hvx = -m;+ if (g == DIR_UP && w->hvy < -m) w->hvy = -m;+ }+ if (g == DIR_RIGHT || g == DIR_LEFT) {+ w->hvy = 0;+ if (u != dn) w->hvy += u ? -speed : speed;+ } else {+ w->hvx = 0;+ const plat_t *p = on_plat(w, w->hx + gx * 0.5f, w->hy + gy * 0.5f, w->hvy);+ if (p) {+ w->hvx = p->vx * 30.0f, w->hvy = 0;+ w->hy = g == DIR_DOWN ? p->y - 8.05f : p->y + 7 + 8.05f;+ }+ if (l != r) w->hvx += l ? -speed : speed;+ }+ }+ move_axis(w, false);+ move_axis(w, true);+ w->moving = w->hvx != 0 || w->hvy != 0;+}++bool sim_heart_in(const world_t *w, float x, float y, float rw, float rh) {+ return w->hx >= x && w->hx <= x + rw && w->hy >= y && w->hy <= y + rh;+}+/* the heart's centre inside the convex quad (corners in order) */+static bool heart_in_quad(const world_t *w, const float qx[4], const float qy[4]) {+ int pos = 0, neg = 0;+ for (int i = 0; i < 4; i++) {+ int j = (i + 1) & 3;+ float c = (qx[j] - qx[i]) * (w->hy - qy[i]) - (qy[j] - qy[i]) * (w->hx - qx[i]);+ if (c > 0) pos++;+ else if (c < 0) neg++;+ }+ return !(pos && neg);+}++/* ---------------------------------------------------------------- damage and KR */+static bool hurts(const world_t *w, int color) {+ return color == COL_WHITE || (color == COL_BLUE && w->moving) || (color == COL_ORANGE && !w->moving);+}++static void damage_tick(world_t *w) {+ if (w->hp <= 0) return;+ int karma = -1;+ bool floor1 = false;+ for (int i = 0; i < w->nbones && w->heart_on; i++) {+ bone_t *b = &w->bones[i];+ if (sim_heart_in(w, b->x, b->y, b->w, b->h) && hurts(w, b->color)) {+ if (karma < 0) karma = b->karma;+ if (b->karma >= 3) b->karma = 2;+ }+ }+ for (int i = 0; i < w->nstabs && w->heart_on; i++) {+ stab_t *s = &w->stabs[i];+ if (s->state != STAB_WARN && sim_heart_in(w, s->x, s->y, s->w, s->h)) {+ if (karma < 0) karma = s->karma;+ if (s->karma >= 3) s->karma = 2;+ }+ }+ for (int i = 0; i < w->nblasters && w->heart_on; i++) {+ blaster_t *b = &w->blasters[i];+ if (!b->hurts) continue;+ float qx[4], qy[4], k = b->scale;+ blaster_beam(b, 35.0f * k, 35.0f * k + 1000.0f, b->base * 3.0f / 8.0f, qx, qy);+ if (heart_in_quad(w, qx, qy)) {+ if (karma < 0) karma = b->karma;+ if (b->karma >= 3) b->karma = 2;+ }+ }+ for (int i = 0; i < w->nmbones && w->heart_on; i++) {+ const mbone_t *m = &w->mbones[i];+ /* 14 x 44, its sides and ends rounded off */+ if (sim_heart_in(w, m->x + 2, m->y + 2, 10, 40)) {+ if (karma < 0) karma = 0;+ floor1 = true;+ }+ }+ if (karma >= 0) {+ w->hp--;+ w->kr = (int16_t)(w->kr + karma);+ w->hits++;+ if (floor1 && w->hp <= 0) w->hp = 1;+ }+ if (w->kr > 40) w->kr = 40;+ if (w->kr >= w->hp) w->kr = (int16_t)(w->hp - 1);+ if (w->kr < 0) w->kr = 0;+ if (w->kr > 0 && w->hp > 1) {+ w->kr_t++;+ int k = w->kr, t = w->kr_t;+ if ((k >= 40 && t >= 1) || (k >= 30 && t >= 2) || (k >= 20 && t >= 5) || (k >= 10 && t >= 15) || t >= 30)+ w->kr--, w->hp--, w->kr_t = 0;+ }+ if (w->hp > MAX_HP) w->hp = MAX_HP;+}++/* ---------------------------------------------------------------- the script */+static double arg(const world_t *w, uint16_t a) { return a & 0x8000 ? w->sc.vars[a & 0x7FFF] : atk_pool[a]; }++static int delay_ticks(double s) {+ double v = bt_floor(s * 30.0 + 0.5);+ return v > 0 ? (v > 100000 ? 100000 : (int)v) : 0;+}++void sim_resume(world_t *w) {+ w->sc.talk = 0;+ w->sc.running = 1;+}++static void set_var(world_t *w, double idx, double v) {+ int i = (int)idx;+ if (i >= 0 && i < MAX_VARS) w->sc.vars[i] = v;+}++/* one line of the script; returns the line to go on from (before the step to the next) */+static int exec(world_t *w, int line, int op, int n, const uint16_t *a) {+ double v[10] = {0};+ for (int i = 0; i < n && i < 10; i++) v[i] = arg(w, a[i]);+ int iv[10];+ for (int i = 0; i < 10; i++) iv[i] = ifl(v[i]);+ switch (op) {+ case OP_SET: set_var(w, v[0], v[1]); break;+ case OP_ADD: set_var(w, v[0], v[1] + v[2]); break;+ case OP_SUB: set_var(w, v[0], v[1] - v[2]); break;+ case OP_MUL: set_var(w, v[0], v[1] * v[2]); break;+ case OP_DIV: set_var(w, v[0], v[2] != 0.0 ? v[1] / v[2] : 0.0); break;+ case OP_MOD: {+ double q = v[2] != 0.0 ? v[1] / v[2] : 0.0;+ double t = q < 0 ? -bt_floor(-q) : bt_floor(q); /* (fmod: towards zero) */+ set_var(w, v[0], v[2] != 0.0 ? v[1] - t * v[2] : 0.0);+ break;+ }+ case OP_FLOOR: set_var(w, v[0], bt_floor(v[1])); break;+ case OP_SIN: set_var(w, v[0], bt_sin_deg(v[1])); break;+ case OP_COS: set_var(w, v[0], bt_cos_deg(v[1])); break;+ case OP_DEG: set_var(w, v[0], v[1] / D2R); break;+ case OP_RAD: set_var(w, v[0], v[1] * D2R); break;+ case OP_ANGLE: set_var(w, v[0], bt_atan2_deg(v[4] - v[2], v[3] - v[1])); break;+ case OP_RND: set_var(w, v[0], (double)bt_below(&w->rng, iv[1] > 0 ? (uint32_t)iv[1] : 0)); break;+ case OP_JMPABS: return iv[0] - 1;+ case OP_JMPREL: return line + iv[0] - 1;+ case OP_JMPZ: return v[1] == 0.0 ? iv[0] - 1 : line;+ case OP_JMPNZ: return v[1] != 0.0 ? iv[0] - 1 : line;+ case OP_JMPE: return v[1] == v[2] ? iv[0] - 1 : line;+ case OP_JMPNE: return v[1] != v[2] ? iv[0] - 1 : line;+ case OP_JMPL: return v[1] < v[2] ? iv[0] - 1 : line;+ case OP_JMPNL: return v[1] >= v[2] ? iv[0] - 1 : line;+ case OP_JMPG: return v[1] > v[2] ? iv[0] - 1 : line;+ case OP_JMPNG: return v[1] <= v[2] ? iv[0] - 1 : line;+ case OP_TLPAUSE: w->sc.running = 0; break;+ case OP_ENDATTACK:+ logf_(w, "EndAttack", 0, iv);+ w->sc.ended = 1, w->sc.running = 0;+ break;+ case OP_BLACKSCREEN:+ logf_(w, "BlackScreen", 1, iv);+ w->black = iv[0] == 1;+ if (w->black) sim_clear_attacks(w);+ break;+ case OP_BONEH:+ case OP_BONEV: add_bone(w, op == OP_BONEV, iv[0], iv[1], iv[2], iv[3], iv[4], n > 5 ? iv[5] : 0); break;+ case OP_BONEHREPEAT:+ case OP_BONEVREPEAT:+ case OP_PLATFORMREPEAT: {+ int d = iv[3] & 3, cnt = iv[5], sp = iv[6];+ for (int i = 0; i < cnt && i < 200; i++) {+ double x = v[0] - (double)DX[d] * sp * i, y = v[1] - (double)DY[d] * sp * i;+ if (op == OP_PLATFORMREPEAT) add_plat(w, ifl(x), ifl(y), iv[2], iv[3], iv[4], 0);+ else add_bone(w, op == OP_BONEVREPEAT, ifl(x), ifl(y), iv[2], iv[3], iv[4], 0);+ }+ break;+ }+ case OP_SINEBONES: {+ int cnt = iv[0], sp = iv[1], spd = iv[2], h = iv[3];+ for (int i = 0; i < cnt && i < 100; i++) {+ double x;+ int d;+ if (sp > 0) x = (double)w->zr + sp * i, d = DIR_LEFT;+ else x = (double)w->zl + sp * i, d = DIR_RIGHT;+ int sine = ifl(bt_sin_deg((double)i / 3.0 / D2R) * 28.0);+ double y = (double)w->zt + 6;+ add_bone(w, true, ifl(x), ifl(y), h + sine, d, spd, 0);+ y = (double)w->zt + 6 + h + sine + 39;+ add_bone(w, true, ifl(x), ifl(y), ifl((double)w->zb - 5 - y), d, spd, 0);+ }+ break;+ }+ case OP_BONESTAB: add_stab(w, iv[0], iv[1], v[2], v[3]); break;+ case OP_GASTERBLASTER: add_blaster(w, iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], v[6], v[7]); break;+ case OP_PLATFORM: add_plat(w, iv[0], iv[1], iv[2], iv[3], iv[4], n > 5 ? iv[5] : 0); break;+ case OP_HEARTMODE:+ logf_(w, "HeartMode", 1, iv);+ sim_heart_mode(w, iv[0]);+ break;+ case OP_HEARTTELEPORT:+ logf_(w, "HeartTeleport", 2, iv);+ w->hx = (float)iv[0], w->hy = (float)iv[1];+ w->heart_on = 1;+ break;+ case OP_HEARTMAXFALLSPEED:+ logf_(w, "HeartMaxFallSpeed", 1, iv);+ w->max_fall = (float)iv[0];+ break;+ case OP_SANSSLAM:+ logf_(w, "SansSlam", 1, iv);+ if (v[0] >= 0 && v[0] <= 3) {+ int d = iv[0] & 3;+ w->heart_mode = HEART_BLUE, w->heart_dir = (uint8_t)d, w->slammed = 1;+ w->hvx = DX[d] * w->max_fall, w->hvy = DY[d] * w->max_fall;+ }+ break;+ case OP_SANSSLAMDAMAGE:+ logf_(w, "SansSlamDamage", 1, iv);+ w->slam_damage = iv[0] != 0;+ break;+ case OP_COMBATZONESPEED:+ logf_(w, "CombatZoneSpeed", 1, iv);+ w->zone_speed = (float)iv[0];+ break;+ case OP_COMBATZONERESIZE: {+ int z[5] = {ims(v[0]), ims(v[1]), ims(v[2]), ims(v[3]), v[4] != 0.0};+ logf_(w, "CombatZoneResize", 5, z);+ sim_zone_to(w, (float)v[0], (float)v[1], (float)v[2], (float)v[3], v[4] != 0.0);+ break;+ }+ case OP_COMBATZONERESIZEINSTANT: {+ int z[4] = {ims(v[0]), ims(v[1]), ims(v[2]), ims(v[3])};+ logf_(w, "CombatZoneResizeInstant", 4, z);+ w->zl = w->tl = (float)v[0], w->zt = w->tt = (float)v[1], w->zr = w->tr = (float)v[2], w->zb = w->tb = (float)v[3];+ w->zone_hidden = 0;+ heart_clamp(w);+ break;+ }+ case OP_GETHEARTPOS:+ logf_(w, "GetHeartPos", 0, iv);+ set_var(w, v[0], w->checking ? 320.0 : (double)w->hx);+ set_var(w, v[1], w->checking ? 304.0 : (double)w->hy);+ break;+ case OP_SANSANIMATION:+ logf_(w, "SansAnimation", 1, iv);+ w->anim = (uint8_t)iv[0], w->body = BODY_NONE, w->anim_t = 0;+ break;+ case OP_SANSBODY:+ logf_(w, "SansBody", 1, iv);+ w->body = (uint8_t)(iv[0] & 3), w->anim = ANIM_NONE, w->anim_t = 0;+ break;+ case OP_SANSTORSO:+ logf_(w, "SansTorso", 1, iv);+ w->torso = (uint8_t)(iv[0] & 1), w->body = BODY_NONE;+ break;+ case OP_SANSHEAD:+ logf_(w, "SansHead", 1, iv);+ w->head = (uint8_t)(iv[0] & 7);+ break;+ case OP_SANSSWEAT:+ logf_(w, "SansSweat", 1, iv);+ w->sweat = (uint8_t)(iv[0] < 0 ? 0 : iv[0] > 3 ? 3 : iv[0]);+ break;+ case OP_SANSX:+ logf_(w, "SansX", 1, iv);+ w->ox = (float)iv[0];+ break;+ case OP_SANSREPEAT:+ logf_(w, "SansRepeat", 0, iv);+ w->ox_speed = -900.0f;+ break;+ case OP_SANSENDREPEAT:+ logf_(w, "SansEndRepeat", 0, iv);+ w->ox_speed = 0;+ break;+ case OP_SANSTEXT:+ logf_(w, "SansText", 1, iv);+ w->sc.running = 0, w->sc.talk = 1, w->sc.talk_line = (int16_t)iv[0];+ break;+ default: break;+ }+ return line;+}++static void script_tick(world_t *w) {+ script_t *s = &w->sc;+ if (s->attack < 0 || !s->running) return;+ const atk_info_t *in = &atk_info[s->attack];+ int guard = 0;+ while (s->running && !s->ended && s->line >= 1 && s->line <= in->lines) {+ const uint16_t *c = &atk_code[atk_line[in->first + s->line - 1]];+ int op = c[0] & 0xFF, n = c[0] >> 8;+ int d = delay_ticks(arg(w, c[1]));+ if (s->t < d) break;+ int to = exec(w, s->line, op, n, c + 2);+ s->t -= d;+ s->line = (uint16_t)(to + 1 < 0 ? 0 : to + 1);+ if (++guard >= 4000) { /* a loop that never waits: the attack ends */+ s->ended = 1, s->running = 0;+ break;+ }+ }+ if (s->running) s->t++;+}++void sim_start_attack(world_t *w, int attack) {+ script_t *s = &w->sc;+ memset(s, 0, sizeof *s);+ s->attack = (int16_t)attack;+ s->talk_line = -1;+ s->vars[0] = PI;+ s->line = 1, s->running = 1;+ /* (ResetVars) */+ w->zone_speed = 480, w->max_fall = 750, w->slam_damage = 0;+ w->ox_speed = 0, w->ox = 320;+ w->hvx = w->hvy = 0, w->slammed = 0;+ w->heart_dir = DIR_DOWN;+ w->heart_free = 1;+ w->menu_left = 0;+ sim_menu_bones(w, false, false);+}++void sim_stop_attack(world_t *w) {+ w->heart_free = 0, w->hvx = w->hvy = 0, w->moving = 0;+ w->sc.attack = -1, w->sc.running = 0, w->sc.finish = 0, w->sc.talk = 0;+ sim_clear_attacks(w);+ w->black = 0;+ w->zone_speed = 480, w->max_fall = 750, w->slam_damage = 0;+ w->ox_speed = 0, w->ox = 320;+}++void sim_reset(world_t *w, uint32_t seed) {+ memset(w, 0, sizeof *w);+ w->zl = w->tl = 32, w->zt = w->tt = 240, w->zr = w->tr = 608, w->zb = w->tb = 384;+ w->zone_speed = 480;+ w->hx = 320, w->hy = 320;+ w->max_fall = 750;+ w->hp = MAX_HP;+ w->ox = 320;+ w->anim = ANIM_NONE, w->body = BODY_NONE, w->head = HEAD_CLOSED;+ w->sc.attack = -1, w->sc.talk_line = -1;+ w->rng = seed ? seed : 1;+ w->fx_rng = seed ^ 0xA5A5A5A5u;+ if (!w->fx_rng) w->fx_rng = 7;+}++void sim_tick(world_t *w, uint32_t keys, uint32_t pressed) {+ heart_tick(w, keys, pressed);+ script_tick(w);+ zone_tick(w);+ bones_tick(w);+ stabs_tick(w);+ blasters_tick(w);+ plats_tick(w);+ mbones_tick(w);+ damage_tick(w);+ /* the opponent */+ w->anim_t++;+ if (w->ox_speed != 0) {+ w->ox += w->ox_speed / 30.0f;+ w->ox_speed -= 1.5f;+ if (w->ox < -100) w->ox = 740;+ }+ if (w->shake > 0) {+ if (++w->shake_t >= 1) w->shake_t = 0, w->shake--;+ }+ w->tick++;+}diff --git a/games/badtime/src/text.c b/games/badtime/src/text.cnew file mode 100644index 00000000..3f8c9c62--- /dev/null+++ b/games/badtime/src/text.c@@ -0,0 +1,80 @@+/* Bad Time Simulator: the words (the game's own: the Judge's lines, the box's text, the items, the help). */+#include "fight.h"++/* what the Judge says where the scripts speak (tools/attacks.py: "ready?", "here we go.") */+const char *const TALK_SCRIPT[2] = {+ "Court is in session.",+ "That was the opening statement.",+};++/* before each attack of the first half (by the attack's number in the fight) */+const char *const TALK_FIRST[14] = {+ 0,+ "Not bad. Most don't get past that.",+ "Watch the blue ones. Stand still and they pass.",+ "You've been busy down here, haven't you?",+ "Every name on that list. I read them all.",+ "Keep your feet under you.",+ "I don't like doing this. You made it a job.",+ "Still standing? Huh.",+ "You know why I wear this hat? Neither do I.",+ "Do you even remember why you started?",+ "It's not too late to stop. Just very, very late.",+ "You're good at this. That's the problem.",+ "My arm's getting tired. My patience isn't.",+ "One more round. Then we'll see.",+};+const char *const TALK_BREAK = "I'm calling a recess. Put it down and we both walk out of here.";+/* the second half */+const char *const TALK_SECOND[10] = {+ "Recess is over. No more warnings.",+ "Each time you come back, you look a little less sorry.",+ "I've seen how this ends. I've seen it a lot.",+ "Hold still. It's easier for both of us.",+ "...",+ "Getting heavy, isn't it? The weight.",+ "I'm not tired. I'm saving my strength.",+ "Alright. Here's my last ruling.",+ "Still here.",+ 0,+};+const char *const TALK_FINAL = "Case closed.";+const char *const TALK_WIN[2] = {+ "...can't keep my eyes open.",+ "Fine. You win. Go on.",+};++/* the box, between attacks */+const char *const INFO_DEFAULT = "* The JUDGE taps his gavel.";+const char *const INFO_FIRST = "* You feel this won't go well.";+const char *const INFO_KARMA = "* Something heavy clings to you.";+const char *const INFO_BREAK = "* The JUDGE calls a recess.";+const char *const INFO_15 = "* The real trial begins.";+const char *const INFO_19 = "* Reading this won't help you.";+const char *const INFO_20 = "* The JUDGE looks worn out.";+const char *const INFO_21 = "* The JUDGE is preparing\n something.";+const char *const INFO_22 = "* The JUDGE is getting ready\n for his final ruling.";+const char *const CHECK_TEXT = "* JUDGE ATK 1 DEF 1\n* Never been hit.\n* Every hit takes 1 HP.";+const char *const CHECK_TEXT2 = "* He can't dodge forever.\n* Keep swinging.";++const item_t ITEMS[4] = {+ {"Pie", "Pie", 99},+ {"Noodles", "Noodles", 90},+ {"Steak", "Steak", 60},+ {"Hero", "Hero sandwich", 40},+};++const char *const HELP[3] = {+ "Dodge everything. The arrows move your heart.\n"+ "Red heart: it goes where you push it.\n"+ "Blue heart: it falls. Up jumps, higher while held.\n"+ "Hold shift to move slowly.",+ "White bones always hurt. Blue ones hurt only\n"+ "while you move, orange ones only when you don't.\n"+ "A hit takes 1 HP and adds KR, which drains your\n"+ "HP after it, faster the more there is.",+ "Between attacks: FIGHT (he dodges), ACT, ITEM\n"+ "(eat to heal) or MERCY. Survive all 24 attacks.\n"+ "Practice: retry each attack until you pass it.\n"+ "Back pauses. Home saves and quits.",+};diff --git a/games/badtime/tests/expect.h b/games/badtime/tests/expect.hnew file mode 100644index 00000000..d93e9a4c--- /dev/null+++ b/games/badtime/tests/expect.h@@ -0,0 +1,28 @@+/* Generated by tools/attacks.py: each attack as a second reading of jcw87's scripts plays it (seed+ * 0x5EED0000 + n): the tick it ends, how many things it makes, the FNV-1a of their log. */+static const struct { uint32_t end, events, hash; } EXPECT[] = {+ {270, 74, 0xD997CAE1u}, /* sans_intro */+ {205, 36, 0xCFC61AD3u}, /* sans_bonegap1 */+ {198, 16, 0x42C46368u}, /* sans_bluebone */+ {217, 24, 0xB0E9A224u}, /* sans_bonegap2 */+ {256, 52, 0x725D969Du}, /* sans_platforms1 */+ {301, 75, 0x25994C66u}, /* sans_platforms2 */+ {247, 29, 0xE014628Bu}, /* sans_platforms3 */+ {224, 101, 0xF91C8662u}, /* sans_platforms4 */+ {277, 30, 0x21162145u}, /* sans_platformblaster */+ {224, 101, 0x08FDE27Eu}, /* sans_platforms4hard */+ {199, 36, 0xBD3BAC39u}, /* sans_bonegap1fast */+ {238, 20, 0x5836458Cu}, /* sans_boneslideh */+ {259, 32, 0xEFFD37EFu}, /* sans_platformblasterfast */+ {16, 4, 0x4C28C2B5u}, /* sans_spare */+ {298, 101, 0x79E491E3u}, /* sans_multi1 */+ {261, 34, 0xC984B841u}, /* sans_randomblaster1 */+ {312, 139, 0xB67B8599u}, /* sans_multi2 */+ {301, 31, 0xBE3CFB87u}, /* sans_bonestab1 */+ {256, 31, 0xCE658489u}, /* sans_bonestab2 */+ {258, 28, 0xB612BB3Du}, /* sans_randomblaster2 */+ {192, 18, 0x63185310u}, /* sans_boneslidev */+ {229, 105, 0x892B6D77u}, /* sans_multi3 */+ {202, 31, 0x7C6E3F7Au}, /* sans_bonestab3 */+ {1577, 457, 0x8A717A6Bu}, /* sans_final */+};diff --git a/games/badtime/tests/play.c b/games/badtime/tests/play.cnew file mode 100644index 00000000..11ee5646--- /dev/null+++ b/games/badtime/tests/play.c@@ -0,0 +1,701 @@+/* Bad Time Simulator on a computer, for tests and pictures (make host, make test).+ *+ * play test DIR every attack against a second reading of jcw87's scripts (expect.h), the heart's, the+ * damage's and the things' numbers against the spec, a bot through the whole fight to the+ * win (the same fight to the same hash on every computer), Practice, Endless and Single+ * attack, saves (kept, damaged, from the copy, storage full) and 300 sessions of the whole+ * app with random keys+ * play shots DIR pictures of the screens (DIR/<name>.ppm)+ * play film DIR N SEED N ticks of the bot's fight from attack SEED's turn (DIR/<frame>_<ms>.raw, for+ * tools/record.py --frames)+ */+#define main bt_app_main+#include "../src/main.c"+#undef main+#include <stddef.h>+#include <stdio.h>+#include <stdlib.h>+#include "expect.h"++/* ---------------------------------------------------------------- the calculator, on a computer */+static uint16_t fb[240][320];+static void (*on_frame)(void);+void plat_push(int x, int y, int w, int h, const uint16_t *px) {+ for (int j = 0; j < h; j++)+ for (int i = 0; i < w; i++) fb[y + j][x + i] = px[j * w + i];+ if (on_frame && y + h == 240) on_frame();+}+void *np_alloc(uint32_t n) { (void)n; return 0; }+static double clock_ms;+static uint32_t (*script)(uint32_t frame);+static uint32_t frame_no, quit_at, session_seed = 1;+uint32_t plat_keys(void) {+ clock_ms += 10;+ frame_no++;+ if (frame_no >= quit_at) return BK_HOME;+ return script ? script(frame_no) : 0;+}+uint32_t plat_millis(void) { return (uint32_t)clock_ms; }+void plat_sleep(uint32_t ms) { clock_ms += ms; }+uint32_t plat_random(void) { return 0x9E3779B9u ^ frame_no ^ session_seed; }+void plat_begin(void) {}+int plat_end(void) { return 0; }++static struct { char name[32]; uint8_t data[1024]; uint32_t len; bool on; } files[4];+static bool storage_full;+bool plat_save(const char *name, const void *data, uint32_t len) {+ if (storage_full) return false;+ int k = -1;+ for (int i = 0; i < 4; i++)+ if (files[i].on && !strcmp(files[i].name, name)) k = i;+ for (int i = 0; i < 4 && k < 0; i++)+ if (!files[i].on) k = i;+ if (k < 0 || len > sizeof files[k].data) return false;+ strcpy(files[k].name, name), memcpy(files[k].data, data, len), files[k].len = len, files[k].on = true;+ return true;+}+const uint8_t *plat_load(const char *name, uint32_t *len) {+ for (int i = 0; i < 4; i++)+ if (files[i].on && !strcmp(files[i].name, name)) return *len = files[i].len, files[i].data;+ return 0;+}+static void forget(const char *name) {+ for (int i = 0; i < 4; i++)+ if (files[i].on && !strcmp(files[i].name, name)) files[i].on = false;+}+static uint8_t *file_data(const char *name, uint32_t *len) {+ for (int i = 0; i < 4; i++)+ if (files[i].on && !strcmp(files[i].name, name)) return *len = files[i].len, files[i].data;+ return 0;+}++static void shot(const char *dir, const char *name) {+ char path[512];+ snprintf(path, sizeof path, "%s/%s.ppm", dir, name);+ FILE *f = fopen(path, "wb");+ if (!f) exit(2);+ fprintf(f, "P6 320 240 255\n");+ for (int y = 0; y < 240; y++)+ for (int x = 0; x < 320; x++) {+ uint16_t c = fb[y][x];+ fputc((c >> 11) * 255 / 31, f), fputc((c >> 5 & 63) * 255 / 63, f), fputc((c & 31) * 255 / 31, f);+ }+ fclose(f);+}++static int fails;+#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL %s:%d: ", __FILE__, __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)++static uint32_t fnv(uint32_t h, const void *p, size_t n) {+ const uint8_t *b = p;+ for (size_t i = 0; i < n; i++) h = (h ^ b[i]) * 16777619u;+ return h;+}++/* ---------------------------------------------------------------- the attacks, against the scripts */+static uint32_t log_hash;+static int log_count;+static void log_line(const char *l) {+ if (log_count) log_hash = (log_hash ^ '\n') * 16777619u;+ log_hash = fnv(log_hash, l, strlen(l));+ log_count++;+}++static world_t W;+static void test_attacks(void) {+ for (int a = 0; a < N_ATTACKS; a++) {+ sim_reset(&W, 0x5EED0000u + (uint32_t)a);+ W.zl = W.tl = 33, W.zt = W.tt = 251, W.zr = W.tr = 608, W.zb = W.tb = 391;+ W.checking = 1, sim_log = log_line, log_hash = 2166136261u, log_count = 0;+ sim_start_attack(&W, a);+ W.tick = 0;+ while (!W.sc.ended && W.tick < 30 * 120) sim_tick(&W, 0, 0);+ uint32_t end = W.tick - 1;+ CHECK(end == EXPECT[a].end && (uint32_t)log_count == EXPECT[a].events && log_hash == EXPECT[a].hash,+ "%s: ends at %u (%u), %d things (%u), hash %08X (%08X)", atk_name[a], end, EXPECT[a].end, log_count,+ EXPECT[a].events, log_hash, EXPECT[a].hash);+ }+ sim_log = 0;+ printf("attacks: the %d against the scripts\n", N_ATTACKS);+}++/* ---------------------------------------------------------------- the numbers (spec/fight.md) */+static void fresh(world_t *w, int mode) {+ sim_reset(w, 1);+ w->zl = w->tl = 133, w->zt = w->tt = 251, w->zr = w->tr = 508, w->zb = w->tb = 391;+ w->sc.attack = -1;+ w->heart_on = w->heart_free = 1;+ sim_heart_mode(w, mode);+ w->hx = 320, w->hy = mode == HEART_BLUE ? 378 : 320;+}++static void test_numbers(void) {+ world_t *w = &W;+ /* the red heart: 150 px/s, 75 with shift; opposite keys cancel */+ fresh(w, HEART_RED);+ sim_tick(w, BK_RIGHT, BK_RIGHT);+ CHECK(w->hx == 325, "red heart: 5 px a tick (%g)", (double)w->hx);+ sim_tick(w, BK_RIGHT | BK_SLOW, 0);+ CHECK(w->hx == 327.5f, "slow: 2.5 px a tick (%g)", (double)w->hx);+ sim_tick(w, BK_LEFT | BK_RIGHT, 0);+ CHECK(w->hx == 327.5f && !w->moving, "left and right: still");+ for (int i = 0; i < 100; i++) sim_tick(w, BK_UP, 0);+ CHECK(w->hy == 251 + 13, "the wall: centre 13 px in (%g)", (double)w->hy);+ /* the blue heart: a jump of 180 px/s, then the gravity table; let go: 30 px/s up at most */+ fresh(w, HEART_BLUE);+ sim_tick(w, 0, 0);+ CHECK(w->hy == 378 && w->hvy == 0, "blue heart: standing (%g, %g)", (double)w->hy, (double)w->hvy);+ sim_tick(w, BK_UP, BK_UP);+ CHECK(w->hvy == -180 && w->hy == 372, "jump: -180 px/s (%g, %g)", (double)w->hvy, (double)w->hy);+ sim_tick(w, BK_UP, 0);+ CHECK(w->hvy == -174, "rising: 180 px/s^2 (%g)", (double)w->hvy);+ sim_tick(w, 0, 0);+ CHECK(w->hvy == -30 + 15, "let go: -30, then 450 px/s^2 (%g)", (double)w->hvy);+ /* a fall from rest: 180 then 540 px/s^2, no more past 240 px/s */+ fresh(w, HEART_BLUE);+ w->hy = 300;+ float v = 0, expect_v[40];+ for (int i = 0; i < 40; i++) {+ float g = v < 240 && v > 15 ? 540 : v <= 15 && v > -30 ? 180 : v <= -30 && v > -120 ? 450 : v <= -120 ? 180 : 0;+ v += g / 30.0f;+ if (v > 750) v = 750;+ expect_v[i] = v;+ }+ bool fall_ok = true;+ for (int i = 0; i < 12; i++) {+ sim_tick(w, 0, 0);+ if (w->hvy != 0 && w->hvy != expect_v[i]) fall_ok = false;+ }+ CHECK(fall_ok, "the blue heart's fall follows the gravity table");+ /* a slam: down at MaxFallSpeed; landing at 330 px/s or more shakes, and hurts with slam damage on */+ fresh(w, HEART_RED);+ w->hy = 280, w->max_fall = 750, w->slam_damage = 1;+ w->heart_mode = HEART_BLUE, w->heart_dir = DIR_DOWN, w->slammed = 1, w->hvy = 750;+ int hp = w->hp;+ for (int i = 0; i < 10; i++) sim_tick(w, 0, 0);+ CHECK(w->hy == 378 && w->hp == hp - 1 && !w->slammed, "slam: down, 1 HP (%g, %d)", (double)w->hy, w->hp);+ /* damage: 1 HP a tick inside a white bone, KR 6 the first tick, then 2 */+ fresh(w, HEART_RED);+ w->nbones = 1;+ memset(&w->bones[0], 0, sizeof w->bones[0]);+ w->bones[0] = (bone_t){310, 250, 20, 100, 0, 0, DIR_RIGHT, COL_WHITE, 6, 1};+ sim_tick(w, 0, 0);+ CHECK(w->hp == 91 && w->kr == 6, "a bone: -1 HP, KR 6 (%d, %d)", w->hp, w->kr);+ sim_tick(w, 0, 0);+ CHECK(w->hp == 90 && w->kr == 8, "again: -1 HP, KR +2 (%d, %d)", w->hp, w->kr);+ /* blue bones hurt only a moving heart, orange ones only a still one */+ fresh(w, HEART_RED);+ w->nbones = 1;+ w->bones[0] = (bone_t){300, 250, 60, 100, 0, 0, DIR_RIGHT, COL_BLUE, 6, 1};+ sim_tick(w, 0, 0);+ CHECK(w->hp == 92, "a blue bone, standing still: no harm");+ sim_tick(w, BK_RIGHT, BK_RIGHT);+ CHECK(w->hp == 91, "a blue bone, moving: harm");+ w->bones[0].color = COL_ORANGE;+ sim_tick(w, BK_LEFT, BK_LEFT);+ CHECK(w->hp == 91, "an orange bone, moving: no harm");+ sim_tick(w, 0, 0);+ CHECK(w->hp == 90, "an orange bone, standing still: harm");+ /* KR: capped at 40 and at HP - 1, drained 1 HP at a time: every 1, 2, 5, 15 or 30 ticks by KR */+ fresh(w, HEART_RED);+ w->heart_on = 0, w->kr = 40;+ int expect_ticks = 0, k = 40, t = 0;+ while (k > 0) {+ t++, expect_ticks++;+ int need = k >= 40 ? 1 : k >= 30 ? 2 : k >= 20 ? 5 : k >= 10 ? 15 : 30;+ if (t >= need) k--, t = 0;+ }+ int ticks = 0;+ while (w->kr > 0 && ticks < 2000) sim_tick(w, 0, 0), ticks++;+ CHECK(ticks == expect_ticks && w->hp == 52, "KR 40 drains in %d ticks (%d), 40 HP (%d)", expect_ticks, ticks, w->hp);+ fresh(w, HEART_RED);+ w->heart_on = 0, w->hp = 10, w->kr = 30;+ sim_tick(w, 0, 0);+ CHECK(w->kr <= 9, "KR below HP (%d)", w->kr);+ /* bones: speed / 30 px a tick, gone past the layout's edge */+ fresh(w, HEART_RED);+ w->heart_on = 0;+ w->nbones = 1;+ w->bones[0] = (bone_t){600, 250, 10, 50, 6, 0, DIR_RIGHT, COL_WHITE, 6, 1};+ sim_tick(w, 0, 0);+ CHECK(w->bones[0].x == 606, "a bone at 180 px/s: 6 px a tick");+ for (int i = 0; i < 5; i++) sim_tick(w, 0, 0);+ CHECK(w->nbones == 1, "still in the layout");+ sim_tick(w, 0, 0);+ CHECK(w->nbones == 0, "gone past x 640");+ /* the zone: 480 px/s an edge (16 a tick) */+ fresh(w, HEART_RED);+ w->heart_on = 0;+ sim_zone_to(w, 33, 251, 608, 391, true);+ sim_tick(w, 0, 0);+ CHECK(w->zl == 117 && w->zr == 524, "the zone: 16 px a tick (%g, %g)", (double)w->zl, (double)w->zr);+ /* a blaster: comes in (1/3 of the way a tick), waits `spin`, opens 0.1 s, fires; the beam hurts until blast ++ * 7/30 s, KR 10 */+ fresh(w, HEART_RED);+ w->hx = 320, w->hy = 320;+ w->nblasters = 1;+ memset(&w->blasters[0], 0, sizeof w->blasters[0]);+ blaster_t *b = &w->blasters[0];+ b->x = b->end_x = 100, b->y = b->end_y = 320, b->ang = b->end_ang = 0;+ b->size = 1, b->sx = b->sy = b->scale = 2, b->timer = 10, b->blast = 8, b->karma = 10, b->opacity = 100;+ int first = -1, last = -1;+ for (int i = 0; i < 60; i++) {+ int before = w->hits;+ sim_tick(w, 0, 0);+ if (w->hits != before) {+ if (first < 0) {+ first = i;+ CHECK(w->kr == 10, "a beam: KR 10 (%d)", w->kr);+ }+ last = i;+ }+ }+ CHECK(first == 10 + 3 && last == first + 8 + 6 - 1, "a beam: from tick %d (13) to %d (26)", first, last);+ printf("numbers: the heart, damage, KR, bones, the zone and blasters as the spec says\n");+}++/* ---------------------------------------------------------------- a bot that dodges by looking ahead */+static world_t probe;+static uint32_t bot_prev;++/* key schedules: a lateral direction held for `lat` ticks, a jump held for `jump` ticks */+typedef struct {+ uint32_t dir;+ uint8_t lat, jump, from; /* the jump key held from tick `from` for `jump` ticks */+} plan_t;++static uint32_t plan_keys(const world_t *w, const plan_t *p, int t) {+ uint32_t k = t < p->lat ? p->dir : 0;+ if (w->heart_mode == HEART_BLUE && p->jump && t >= p->from && t < p->from + p->jump) {+ static const uint32_t JUMP[4] = {BK_LEFT, BK_UP, BK_RIGHT, BK_DOWN};+ k |= JUMP[w->heart_dir & 3];+ }+ return k;+}++#define HORIZON 24+static long score_plan(const world_t *w, const plan_t *p, uint32_t prev) {+ memcpy(&probe, w, sizeof probe);+ long score = 0;+ for (int t = 0; t < HORIZON; t++) {+ uint32_t k = plan_keys(w, p, t);+ uint16_t hits = probe.hits;+ sim_tick(&probe, k, k & ~prev);+ prev = k;+ if (probe.hits != hits) score += 1000L * (HORIZON - t);+ if (probe.sc.ended) break;+ }+ /* a little: the middle of the box, keeping still */+ float cx = (probe.zl + probe.zr) / 2, cy = (probe.zt + probe.zb) / 2;+ float d = (probe.hx - cx) * (probe.hx - cx) + (probe.hy - cy) * (probe.hy - cy);+ score += (long)(d / 400.0f);+ score += p->lat ? 2 : 0;+ return score;+}++static uint32_t bot_dodge(const world_t *w) {+ static plan_t plans[64];+ int n = 0;+ if (w->heart_mode == HEART_RED) {+ static const uint32_t DIRS[9] = {0, BK_LEFT, BK_RIGHT, BK_UP, BK_DOWN, BK_LEFT | BK_UP, BK_RIGHT | BK_UP,+ BK_LEFT | BK_DOWN, BK_RIGHT | BK_DOWN};+ for (int i = 0; i < 9; i++)+ for (int l = 0; l < 3; l++) plans[n++] = (plan_t){DIRS[i], (uint8_t)(i ? (l == 0 ? HORIZON : l == 1 ? 8 : 3) : 0), 0, 0};+ } else {+ bool across_x = w->heart_dir == DIR_DOWN || w->heart_dir == DIR_UP;+ uint32_t lat[3] = {0, across_x ? BK_LEFT : BK_UP, across_x ? BK_RIGHT : BK_DOWN};+ /* (a jump needs the key pressed again: from the next tick when it's held now) */+ static const uint8_t JUMP[7][2] = {{0, 0}, {0, 4}, {0, HORIZON}, {1, 4}, {1, HORIZON}, {4, 4}, {4, HORIZON}};+ for (int i = 0; i < 3; i++)+ for (int j = 0; j < 7; j++)+ for (int l = 0; l < (i ? 2 : 1); l++)+ plans[n++] = (plan_t){lat[i], (uint8_t)(i ? (l == 0 ? HORIZON : 6) : 0), JUMP[j][1], JUMP[j][0]};+ }+ long best = 0;+ int bi = 0;+ for (int i = 0; i < n; i++) {+ long s = score_plan(w, &plans[i], bot_prev);+ if (i == 0 || s < best) best = s, bi = i;+ }+ return plan_keys(w, &plans[bi], 0);+}++/* the keys for the next tick of a Normal fight: dodge, then FIGHT (or eat when low) */+static uint32_t bot_keys(const fight_t *f) {+ const world_t *w = &f->w;+ bool odd = f->w.tick & 1;+ switch (f->state) {+ case FS_ATTACK:+ if (f->text_kind == TXT_BUBBLE) return odd ? BK_OK : 0;+ if (w->heart_free && w->heart_on && w->sc.running) return bot_dodge(w);+ return 0;+ case FS_MENU: {+ int want = w->hp < 45 && f->nitems ? 2 : 0;+ if (f->sel != want) return odd ? BK_RIGHT : 0;+ return odd ? BK_OK : 0;+ }+ case FS_SUB: return odd ? BK_OK : 0;+ case FS_TARGET: {+ float mid = (w->zl + w->zr) / 2, d = f->bar_x - mid;+ return d > -20 && d < 20 ? BK_OK : 0;+ }+ case FS_INFO:+ case FS_WIN: return odd ? BK_OK : 0;+ default: return 0;+ }+}++static fight_t F;+/* a whole fight with the bot: its hash (every tick's state); won: 1, lost: 0 */+static int bot_fight(int mode, int arg, uint32_t seed, uint32_t *hash, uint32_t max_ticks, int *worst_hp) {+ fight_start(&F, mode, arg, seed);+ bot_prev = 0;+ uint32_t h = 2166136261u;+ int low = MAX_HP;+ static int verbose = -1;+ if (verbose < 0) verbose = getenv("BOT_VERBOSE") != 0;+ int last_state = -1, hp_in = 0;+ uint16_t hits_in = 0;+ for (uint32_t t = 0; t < max_ticks && !fight_over(&F); t++) {+ uint32_t k = bot_keys(&F);+ fight_tick(&F, k, k & ~bot_prev);+ bot_prev = k;+ if (verbose && F.state != last_state) {+ if (F.state == FS_ATTACK) hp_in = F.w.hp, hits_in = F.w.hits;+ if (last_state == FS_ATTACK)+ printf(" %-24s HP %2d -> %2d, %3d ticks hurt (tick %u)\n", atk_name[F.last_attack], hp_in, F.w.hp,+ F.w.hits - hits_in, (unsigned)F.w.tick);+ last_state = F.state;+ }+ int16_t v[4] = {F.w.hp, F.w.kr, (int16_t)F.state, (int16_t)F.last_attack};+ h = fnv(h, v, sizeof v);+ h = fnv(h, &F.w.hx, sizeof F.w.hx);+ h = fnv(h, &F.w.hy, sizeof F.w.hy);+ if (F.state == FS_ATTACK && F.w.hp < low) low = F.w.hp;+ }+ if (hash) *hash = h;+ if (worst_hp) *worst_hp = low;+ return F.state == FS_WON;+}++#define RUN_SEED 0xB0B0B0u+#define RUN_HASH 0xC53F43E5u+static void test_bot(void) {+ uint32_t h;+ int low;+ int won = bot_fight(MODE_NORMAL, 0, RUN_SEED, &h, 30 * 60 * 20, &low);+ CHECK(won, "the bot doesn't get through the fight (attack %d of 24, hit attempts %d, HP %d)", F.progress,+ F.hit_attempts, F.w.hp);+ CHECK(F.progress == 24, "the bot's furthest attack: %d", F.progress);+ printf("bot: %s the whole fight in %u attacks, HP never below %d, hash %08X\n", won ? "won" : "LOST",+ (unsigned)F.attacks_done, low, h);+ if (RUN_HASH) CHECK(h == RUN_HASH, "the fight's hash: %08X, not %08X (not the same on every computer?)", h, RUN_HASH);+ uint32_t h2;+ bot_fight(MODE_NORMAL, 0, RUN_SEED, &h2, 30 * 60 * 20, 0);+ CHECK(h == h2, "the same fight twice: %08X then %08X", h, h2);+}++static void test_modes(void) {+ /* Practice: every attack in order, then the final over and over */+ fight_start(&F, MODE_PRACTICE, 0, 99);+ bot_prev = 0;+ int seen[N_ATTACKS] = {0};+ for (uint32_t t = 0; t < 30 * 60 * 12 && !fight_over(&F); t++) {+ uint32_t k = bot_keys(&F);+ fight_tick(&F, k, k & ~bot_prev);+ bot_prev = k;+ if (F.state == FS_ATTACK) seen[F.last_attack] = 1;+ CHECK(F.w.hp > 0, "Practice: the heart can't break");+ if (F.w.hp <= 0) break;+ }+ int all = 1;+ for (int i = 0; i < N_ATTACKS; i++) all &= i == A_SPARE || seen[i];+ CHECK(all, "Practice: every attack comes");+ CHECK(F.last_attack == A_FINAL, "Practice: ends on the final attack (%s)", atk_name[F.last_attack]);+ /* Endless: phase 2 never reaches the final attack */+ fight_start(&F, MODE_ENDLESS, 1, 7);+ bot_prev = 0;+ for (uint32_t t = 0; t < 30 * 60 * 4 && !fight_over(&F); t++) {+ uint32_t k = bot_keys(&F);+ fight_tick(&F, k, k & ~bot_prev);+ bot_prev = k;+ CHECK(F.last_attack != A_FINAL && F.last_attack >= A_MULTI1, "Endless phase 2: the second half's attacks");+ if (F.last_attack == A_FINAL) break;+ }+ CHECK(F.attacks_done >= 10, "Endless: attacks back to back (%u)", (unsigned)F.attacks_done);+ /* Single attack: the same one again and again */+ fight_start(&F, MODE_SINGLE, A_BONESTAB2, 5);+ bot_prev = 0;+ for (uint32_t t = 0; t < 30 * 40 && !fight_over(&F); t++) {+ uint32_t k = bot_keys(&F);+ fight_tick(&F, k, k & ~bot_prev);+ bot_prev = k;+ }+ CHECK(F.last_attack == A_BONESTAB2 && F.attacks_done >= 2, "Single attack: again");+ /* the turns' order, as jcw87's StartAttack */+ fight_t g;+ memset(&g, 0, sizeof g);+ CHECK(fight_progress_of(0, 0) == 1 && fight_progress_of(5, 13) == 14 && fight_progress_of(13, 3) == 14 &&+ fight_progress_of(14, 0) == 15 && fight_progress_of(22, 9) == 23 && fight_progress_of(23, 0) == 24,+ "progress");+ printf("modes: Practice, Endless and Single attack\n");+}++/* ---------------------------------------------------------------- saves */+static void test_saves(void) {+ memset(files, 0, sizeof files);+ bt_progress p, q;+ CHECK(!save_load(&p) && p.best == 0 && p.wins == 0, "no save: a fresh start");+ p.best = 17, p.wins = 3, p.guide = 1, p.endless[1] = 12;+ CHECK(save_write(&p), "a save");+ memset(&q, 0, sizeof q);+ CHECK(save_load(&q) && !memcmp(&p, &q, sizeof p), "a save read back");+ uint32_t n;+ uint8_t *d = file_data(BT_SAVE_NAME, &n);+ CHECK(d && n == sizeof(bt_file), "badtime.sav: %u bytes", n);+ /* damaged ways: each is refused, then the copy brings it back */+ for (int k = 0; k < 6; k++) {+ save_write(&p);+ d = file_data(BT_SAVE_NAME, &n);+ if (k == 0) d[0] ^= 0xFF; /* not ours */+ if (k == 1) d[4] = 9; /* another version */+ if (k == 2) d[9] ^= 1; /* the checksum's wrong */+ if (k == 3) files[0].len = 10; /* cut short */+ if (k == 4) { /* sums right, but best 99 */+ bt_file f;+ memcpy(&f, d, sizeof f);+ f.p.best = 99;+ f.sum = bt_sum(&f, offsetof(bt_file, sum));+ memcpy(d, &f, sizeof f);+ }+ if (k == 5) forget(BT_SAVE_NAME); /* gone (a reinstall) */+ memset(&q, 0, sizeof q);+ CHECK(save_load(&q) && !memcmp(&p, &q, sizeof p), "damaged save %d: back from the copy", k);+ d = file_data(BT_SAVE_NAME, &n);+ CHECK(d && n == sizeof(bt_file) && bt_file_ok((const bt_file *)d), "damaged save %d: written back", k);+ }+ /* both broken: a fresh start, no crash */+ save_write(&p);+ d = file_data(BT_SAVE_NAME, &n), d[2] ^= 0x55;+ uint8_t *c = file_data(BT_COPY_NAME, &n);+ CHECK(c && n > 20, "badtime_saves.py");+ if (c) for (uint32_t i = 0; i < n; i++) if (c[i] == ':') c[i + 3] = '!';+ memset(&q, 0xAA, sizeof q);+ CHECK(!save_load(&q) && q.best == 0 && q.wins == 0, "both damaged: a fresh start");+ /* storage full: the game goes on */+ storage_full = true;+ CHECK(!save_write(&p), "storage full: not saved");+ storage_full = false;+ printf("saves: kept, damaged (6 ways) and back from the copy, storage full\n");+}++/* ---------------------------------------------------------------- the whole app, random keys */+static uint32_t rnd_state;+static uint32_t random_keys(uint32_t frame) {+ (void)frame;+ rnd_state ^= rnd_state << 13, rnd_state ^= rnd_state >> 17, rnd_state ^= rnd_state << 5;+ static uint32_t held;+ if ((rnd_state & 7) == 0) {+ static const uint32_t K[8] = {BK_LEFT, BK_RIGHT, BK_UP, BK_DOWN, BK_OK, BK_OK, BK_BACK, BK_SLOW};+ held ^= K[(rnd_state >> 3) & 7];+ if ((rnd_state >> 9 & 15) == 0) held = 0;+ }+ return held;+}++static void test_random(void) {+ int sessions = 300;+ for (int s = 0; s < sessions; s++) {+ memset(files, 0, sizeof files);+ if (s & 1) { /* half of them past how to play */+ bt_progress p;+ bt_fresh(&p);+ p.guide = 1;+ save_write(&p);+ }+ rnd_state = 0x1234567u + (uint32_t)s * 7919u, session_seed = (uint32_t)s;+ script = random_keys, frame_no = 0, quit_at = 900 + (uint32_t)(s % 7) * 150, clock_ms = 0;+ bt_app_main();+ uint32_t n;+ const uint8_t *d = file_data(BT_SAVE_NAME, &n);+ CHECK(d && n == sizeof(bt_file) && bt_file_ok((const bt_file *)d), "session %d: the save's fine", s);+ }+ script = 0;+ printf("random keys: %d sessions, saves fine\n", sessions);+}++/* ---------------------------------------------------------------- pictures */+static const char *shots_dir;+static void run_bot_until(int mode, int arg, uint32_t seed, int (*stop)(const fight_t *), uint32_t max) {+ fight_start(&F, mode, arg, seed);+ bot_prev = 0;+ for (uint32_t t = 0; t < max && !fight_over(&F); t++) {+ if (stop(&F)) return;+ uint32_t k = bot_keys(&F);+ fight_tick(&F, k, k & ~bot_prev);+ bot_prev = k;+ }+}+static int stop_attack;+static uint32_t stop_tick;+static int at_attack_tick(const fight_t *f) {+ return f->state == FS_ATTACK && f->last_attack == stop_attack && f->w.sc.running && f->w.sc.attack >= 0 &&+ f->w.tick >= stop_tick;+}+static void snap_fight(const char *name, const char *title) {+ draw_fight(&F, title);+ shot(shots_dir, name);+}+static void snap_single(int attack, uint32_t ticks, const char *name) {+ fight_start(&F, MODE_SINGLE, attack, 42);+ bot_prev = 0;+ for (uint32_t t = 0; t < ticks; t++) {+ uint32_t k = bot_keys(&F);+ fight_tick(&F, k, k & ~bot_prev);+ bot_prev = k;+ F.w.hp = F.w.hp < 40 ? 40 : F.w.hp;+ }+ snap_fight(name, 0);+}++static void make_shots(const char *dir) {+ shots_dir = dir;+ /* the title */+ memset(files, 0, sizeof files);+ sv.best = 17, sv.wins = 1, view = V_TITLE, sel = 0, frame = 12;+ draw();+ shot(dir, "title");+ /* the fight: the intro's blue eye, the menu, the attacks */+ snap_single(A_INTRO, 30, "intro");+ snap_single(A_INTRO, 140, "gavels");+ snap_single(A_PLATFORMS2, 150, "platforms");+ snap_single(A_BONEGAP1, 70, "bonegap");+ snap_single(A_BLUEBONE, 40, "bluebone");+ snap_single(A_BONESTAB1, 40, "bonestab");+ snap_single(A_MULTI2, 120, "multi");+ snap_single(A_RANDOMBLASTER1, 70, "random");+ snap_single(A_FINAL, 300, "final");+ snap_single(A_FINAL, 880, "spiral");+ snap_single(A_FINAL, 1300, "final2");+ /* the menu, after the first attack */+ stop_attack = A_BONEGAP1, stop_tick = 0;+ run_bot_until(MODE_NORMAL, 0, 3, at_attack_tick, 30 * 60);+ /* (back to the menu before it) */+ fight_start(&F, MODE_NORMAL, 0, 3);+ bot_prev = 0;+ for (uint32_t t = 0; t < 30 * 60 && !(F.state == FS_MENU && F.text_shown == F.text_len); t++) {+ uint32_t k = bot_keys(&F);+ if (F.state == FS_MENU) k = 0;+ fight_tick(&F, k, k & ~bot_prev);+ bot_prev = k;+ }+ snap_fight("menu", 0);+ F.sel = 2;+ fight_tick(&F, BK_OK, BK_OK);+ for (int i = 0; i < 3; i++) fight_tick(&F, 0, 0);+ fight_tick(&F, BK_RIGHT, BK_RIGHT);+ snap_fight("items", 0);+ fight_tick(&F, BK_BACK, BK_BACK);+ F.sel = 0;+ fight_tick(&F, 0, 0);+ fight_tick(&F, BK_OK, BK_OK), fight_tick(&F, 0, 0), fight_tick(&F, BK_OK, BK_OK);+ for (int i = 0; i < 40 && F.state == FS_TARGET; i++) fight_tick(&F, 0, 0);+ snap_fight("target", 0);+ fight_tick(&F, BK_OK, BK_OK);+ for (int i = 0; i < 26; i++) fight_tick(&F, 0, 0);+ snap_fight("dodge", 0);+ /* the speech bubble */+ for (int i = 0; i < 400 && !(F.state == FS_ATTACK && F.text_kind == TXT_BUBBLE && F.text_shown == F.text_len); i++)+ fight_tick(&F, 0, 0);+ snap_fight("talk", 0);+ /* the heart breaks */+ snap_single(A_BONEGAP2, 60, "x");+ F.w.hp = 1;+ for (int i = 0; i < 200 && F.state != FS_DEATH; i++) fight_tick(&F, BK_UP, 0);+ for (int i = 0; i < 30; i++) fight_tick(&F, 0, 0);+ snap_fight("death", 0);+ /* pause, how to play, the end */+ fight_start(&F, MODE_NORMAL, 0, 3);+ for (int i = 0; i < 60; i++) fight_tick(&F, 0, 0);+ view = V_PAUSE, sel = 2;+ draw();+ shot(dir, "pause");+ view = V_HELP, page = 0, first_run = 0;+ draw();+ shot(dir, "help");+ F.state = FS_WON, F.mode = MODE_NORMAL, sv.wins = 2, sv.best = 24;+ memcpy(&fight, &F, sizeof F);+ view = V_END;+ draw();+ shot(dir, "won");+ printf("shots in %s\n", dir);+}++/* ---------------------------------------------------------------- frames for a GIF */+static void film(const char *dir, uint32_t n, int attack) {+ fight_start(&F, attack >= 0 ? MODE_SINGLE : MODE_NORMAL, attack, 77);+ bot_prev = 0;+ char path[512];+ for (uint32_t t = 0; t < n; t++) {+ uint32_t k = bot_keys(&F);+ fight_tick(&F, k, k & ~bot_prev);+ bot_prev = k;+ if (F.w.hp < 30) F.w.hp = 30;+ draw_fight(&F, 0);+ snprintf(path, sizeof path, "%s/%06u_%06u.raw", dir, t, t * 100 / 3);+ FILE *f = fopen(path, "wb");+ if (!f) exit(2);+ fwrite(fb, 2, 320 * 240, f);+ fclose(f);+ }+}++int main(int argc, char **argv) {+ if (argc > 2 && !strcmp(argv[1], "quick")) {+ test_attacks();+ test_numbers();+ test_saves();+ printf(fails ? "%d FAILED\n" : "all good\n", fails);+ return fails != 0;+ }+ if (argc > 3 && !strcmp(argv[1], "trace")) {+ fight_start(&F, MODE_SINGLE, atoi(argv[3]), 42);+ bot_prev = 0;+ for (uint32_t t = 0; t < (uint32_t)atoi(argv[2]); t++) {+ uint32_t k = bot_keys(&F);+ uint16_t h = F.w.hits;+ fight_tick(&F, k, k & ~bot_prev);+ bot_prev = k;+ printf("%4u st%d keys %02X heart %d dir %d (%7.2f %7.2f) v (%7.2f %7.2f) hp %d kr %d stabs %d%s\n", (unsigned)F.w.tick, F.state, k,+ F.w.heart_mode, F.w.heart_dir, (double)F.w.hx, (double)F.w.hy, (double)F.w.hvx, (double)F.w.hvy, F.w.hp,+ F.w.kr, F.w.nstabs, F.w.hits != h ? " HIT" : "");+ if (F.w.hp < 30) F.w.hp = 92;+ }+ return 0;+ }+ if (argc > 2 && !strcmp(argv[1], "bot")) {+ test_bot();+ printf(fails ? "%d FAILED\n" : "all good\n", fails);+ return fails != 0;+ }+ if (argc > 2 && !strcmp(argv[1], "test")) {+ test_attacks();+ test_numbers();+ test_saves();+ test_modes();+ test_bot();+ test_random();+ printf(fails ? "%d FAILED\n" : "all good\n", fails);+ return fails != 0;+ }+ if (argc > 2 && !strcmp(argv[1], "shots")) {+ make_shots(argv[2]);+ return 0;+ }+ if (argc > 4 && !strcmp(argv[1], "film")) {+ film(argv[2], (uint32_t)atoi(argv[3]), atoi(argv[4]));+ return 0;+ }+ printf("usage: play test DIR | shots DIR | film DIR TICKS ATTACK\n");+ return 2;+}diff --git a/games/badtime/tools/armperf.py b/games/badtime/tools/armperf.pynew file mode 100644index 00000000..06458d1c--- /dev/null+++ b/games/badtime/tools/armperf.py@@ -0,0 +1,53 @@+#!/usr/bin/env python3+"""The frame rate of the attacks at an N0110's speed (tools/emu.py): armperf.py output/armperf.nwa [ATTACK...]+(numbers as in src/attacks.h; the busiest by default, several at a time). Each attack is played once, start to end,+the heart unhurt, every frame drawn as in the game and timed. Steady 30 fps: no frame over 33 ms."""+import os+import struct+import sys+from concurrent.futures import ProcessPoolExecutor++HERE = os.path.dirname(os.path.abspath(__file__))+sys.path.insert(0, os.path.join(HERE, "..", "..", "..", "tools"))+import emu # noqa: E402++NAMES = ["Intro", "Bone gap", "Blue bones", "Bone gap 2", "Platforms", "Platforms 2", "Platforms 3", "Platforms 4",+ "Platform blasters", "Platforms 4 hard", "Bone gap fast", "Bone slide", "Platform blasters fast",+ "The break", "Multi 1", "Random blasters", "Multi 2", "Bone stab", "Bone stab 2", "Random blasters 2",+ "Bone slide 2", "Multi 3", "Bone stab 3", "The final attack"]+BUSIEST = [0, 5, 12, 16, 19, 21, 22, 23]+++def one(args):+ nwa, a = args+ emu.configure("n0110")+ c = emu.Calculator(nwa)+ c.run(300) # (the title, waiting)+ c.uc.mem_write(c.symbols["perf_attack"], struct.pack("<I", a))+ c.uc.mem_write(c.symbols["perf_go"], struct.pack("<I", 1))+ t = 300+ while t < 120_000:+ t += 5000+ c.run(t)+ if struct.unpack("<I", bytes(c.uc.mem_read(c.symbols["perf_done"], 4)))[0]:+ break+ out = struct.unpack("<8I", bytes(c.uc.mem_read(c.symbols["perf_out"], 32)))+ done, = struct.unpack("<I", bytes(c.uc.mem_read(c.symbols["perf_done"], 4)))+ return a, out, done+++def main():+ nwa = sys.argv[1]+ attacks = [int(x) for x in sys.argv[2:]] or BUSIEST+ bad = 0+ with ProcessPoolExecutor(max_workers=min(8, len(attacks))) as ex:+ for a, o, done in ex.map(one, [(nwa, a) for a in attacks]):+ frames, work, worst, over = o[0], o[1], o[2], o[3]+ print(f"{NAMES[a]:22}: {frames:5} frames, {work / max(frames, 1):5.1f} ms a frame, slowest {worst:3} ms, "+ f"{over:3} over 33 ms, up to {o[4]} bones and {o[5]} gavels" + ("" if done else ", NOT done"))+ bad += over > 0 or not done+ sys.exit(1 if bad else 0)+++if __name__ == "__main__":+ main()diff --git a/games/badtime/tools/art.py b/games/badtime/tools/art.pynew file mode 100644index 00000000..1f8fe3aa--- /dev/null+++ b/games/badtime/tools/art.py@@ -0,0 +1,300 @@+#!/usr/bin/env python3+"""Bad Time Simulator's pictures, drawn here pixel by pixel (the game's own: the Judge, the gavels, the heart):+writes src/art.c and src/art.h, and with --preview DIR a picture of each at 4x (Pillow).++Each picture is rows of letters, a letter a pixel ('.' is see-through), in one of two palettes."""+import os+import sys++HERE = os.path.dirname(os.path.abspath(__file__))+SRC = os.path.join(HERE, "..", "src")++JUDGE = { # the Judge: robe, gold, hat, the shadow under it, white gloves and bands+ "k": 0x1C1418, "R": 0x7D2433, "r": 0x521826, "L": 0xA83D4F, "g": 0xE0B84C, "G": 0xA27D2A, "H": 0x2D2D38,+ "h": 0x55556A, "f": 0x0B0B10, "w": 0xF4F2EE, "W": 0xBDB8AE, "B": 0x4A3324, "b": 0x3EC8FF, "c": 0xA8ECFF,+ "s": 0x9FD8FF,+}+GAVEL = { # the gavels: wood, gold bands, the striking face that lights up+ "k": 0x1A120C, "D": 0x5C3A20, "M": 0x8A5A34, "l": 0xB07A48, "g": 0xE0B84C, "G": 0xA27D2A, "f": 0x2A1E16,+ "w": 0xFFFFFF, "c": 0xBFF2FF, "b": 0x3EC8FF, "y": 0xFFF3B0,+}++HEAD = """+..........kkkkkkkkkkkkkk..........+.........kHhhhhhhhhhhhhHk.........+.........kHhHHHHHHHHHHHHk.........+.........kHHHHHHHHHHHHHHk.........+.........kHHHHHHHHHHHHHHk.........+.........kggggggggggggggk.........+.........kGGGGGGGGGGGGGGk.........+...kkkkkkkHHHHHHHHHHHHHHkkkkkkk...+.kkhhhhhhhhhhhhhhhhhhhhhhhhhhhhkk.+kHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHk+.kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk.+........kffffffffffffffffk........+........kffffffffffffffffk........+........kffffffffffffffffk........+........kffffffffffffffffk........+.........kffffffffffffffk.........+..........kffffffffffffk..........+...........kkffffffffkk...........+"""++TORSO = """+.......kkkkkkkkkkkkkk.......+....kkkLLLLLwwwwLLLLLkkk....+..kkLLLRRRRRwwwwRRRRRLLLkk..+.kLLRRRRRRRRkwwkRRRRRRRRLLk.+kLRRRRRRRRRRRwwRRRRRRRRRRRLk+kRRRRRRRRRRRRggRRRRRRRRRRRRk+kRRRRRRRRRgGRggRGgRRRRRRRRRk+kRRRRRRRRRRgGggGgRRRRRRRRRRk+kRRRRRRRRRRRggggRRRRRRRRRRRk+kRRRRRRRRRRRgGGgRRRRRRRRRRRk+kRRRRRRRRRRRRggRRRRRRRRRRRRk+kRRRRRRRRRRRRggRRRRRRRRRRRRk+krRRRRRRRRRRRggRRRRRRRRRRRrk+krRRRRRRRRRRRggRRRRRRRRRRRrk+krrRRRRRRRRRRggRRRRRRRRRRrrk+krrrRRRRRRRRRggRRRRRRRRRrrrk+"""++LEGS = """+..krrRRRRRRRRRRggRRRRRRRRRRrrk..+..krrRRRRRRRRRRggRRRRRRRRRRrrk..+.krrrRRRRRRRRRRggRRRRRRRRRRrrrk.+.krrrRRRRRRRRRRggRRRRRRRRRRrrrk.+.krrrrRRRRRRRRRggRRRRRRRRRrrrrk.+krrrrrRRRRRRRRRggRRRRRRRRRrrrrrk+krrrrrrRRRRRRRRggRRRRRRRRrrrrrrk+kGggggggggggggggggggggggggggggGk+.kkkkkkkkkkkkkkkkkkkkkkkkkkkkkk.+.......kBBBBBk....kBBBBBk.......+......kBBBBBBk....kBBBBBBk......+......kkkkkkkk....kkkkkkkk......+"""++# arms, the left ones (the right ones are mirrored); their shoulder joint is the top-middle+ARM_SIDE = """+..kkk.+.kLRRk+kLRRRk+kRRRrk+kRRRrk+kRRRrk+kRRrrk+kRRrrk+kRRrrk+krrrrk+kGgggk+kkwwkk+kwwwWk+kwwWWk+.kWWk.+..kk..+"""+ARM_UP = """+..kk..+.kwwk.+kwwwWk+kwwWWk+kkwWkk+kGgggk+krRRrk+kRRRrk+kRRRrk+kRRRrk+kRRRLk+kRRRLk+kRRLLk+kRLLLk+.kLLk.+..kk..+"""+ARM_OUT = """+..kkkkkkkkkkkk.....+.kLLLLLLLLLLLLkkkk.+kLRRRRRRRRRRRkGwwwk+kRRRRRRRRRRRRkgwwWk+krrrrrrrrrrrrkgwWWk+.kkkkkkkkkkkkkkkkk.+"""+ARM_DOWN = """+..kkk...+.kLRRk..+kLRRRk..+kRRRrk..+kRRRrk..+.kRRrrk.+.kRRrrk.+.kRRrrk.+..kRrrrk+..kRrrrk+..kGgggk+..kkwwkk+..kwwwWk+..kwwWWk+...kWWk.+....kk..+"""+ARM_SHRUG = """+.....kk.+....kwwk+.kkkkwWk+kLRRkwWk+kRRRkGgk+kRRRrkkk+kRRrk...+.krk....+"""++HEART = """+.rr..rr.+rrrrrrrr+rrrrrrrr+rrrrrrrr+.rrrrrr.+..rrrr..+...rr...+"""+HEART_BROKEN = """+.rr..rr.+rrr.rrrr+rrrr.rrr+rrr.rrrr+.rrr.rr.+..r.rr..+...rr...+"""++GAVEL_SHUT = """+............................+...kkk..kkkkkkkkkkk..kkkkk..+..kgGGkkklllllllllkkkfffffk.+..kgGGkMMlllllllllMkkfffffk.+.kggGGkMMMMMMMMMMMMkkffffffk+.kggGGkMMMMMMMMMMMMkkffffffk+.kggGGkMMMMMMMMMMMMkkffffffk+.kggGGkMMMMMMMMMMMMkkffffffk+.kggGGkMMMMMMMMMMMMkkffffffk+.kggGGkMMMMMMMMMMMMkkffffffk+..kgGGkDDDDDDDDDDDDkkfffffk.+..kgGGkDDDDDDDDDDDDkkfffffk.+...kkk.kkkkkMMkkkkk..kkkkk..+...........kMMk.............+...........kMMk.............+...........kMDk.............+...........kMDk.............+...........kMDk.............+...........kMDk.............+...........kGgk.............+...........kGgk.............+............kk..............+"""+GAVEL_OPEN = """+............................+...kkk..kkkkkkkkkkk..kkkkk..+..kgGGkkklllllllllkkkcccwck.+..kgGGkMMlllllllllMkkcwwwwck+.kggGGkMMMMMMMMMMMMkkcwwwwwc+.kggGGkMMMMMMMMMMMMkkcwwwwwc+.kggGGkMMMMMMMMMMMMkkcwwwwwc+.kggGGkMMMMMMMMMMMMkkcwwwwwc+.kggGGkMMMMMMMMMMMMkkcwwwwwc+.kggGGkMMMMMMMMMMMMkkcwwwwwc+..kgGGkDDDDDDDDDDDDkkcwwwwck+..kgGGkDDDDDDDDDDDDkkcccwck.+...kkk.kkkkkMMkkkkk..kkkkk..+...........kMMk.............+...........kMMk.............+...........kMDk.............+...........kMDk.............+...........kMDk.............+...........kMDk.............+...........kGgk.............+...........kGgk.............+............kk..............+"""++PICTURES = [ # name, rows, palette, anchor (x, y) from the top-left+ ("head", HEAD, "judge", (17, 18)), # bottom-middle: the chin+ ("torso", TORSO, "judge", (14, 16)), # bottom-middle+ ("legs", LEGS, "judge", (16, 12)), # bottom-middle: the feet+ ("arm_side", ARM_SIDE, "judge", (3, 0)),+ ("arm_up", ARM_UP, "judge", (3, 15)),+ ("arm_out", ARM_OUT, "judge", (0, 1)),+ ("arm_down", ARM_DOWN, "judge", (3, 0)),+ ("arm_shrug", ARM_SHRUG, "judge", (1, 4)),+ ("heart", HEART, "heart", (4, 4)),+ ("heart_broken", HEART_BROKEN, "heart", (4, 4)),+ ("gavel_shut", GAVEL_SHUT, "gavel", (14, 7)), # the head's middle: where it turns, and the beam's axis+ ("gavel_open", GAVEL_OPEN, "gavel", (14, 7)),+]+PALETTES = {"judge": JUDGE, "gavel": GAVEL, "heart": {"r": 0xFFFFFF}} # (the heart is tinted when drawn)+++def rows(text):+ r = [l for l in text.strip("\n").split("\n")]+ if len({len(l) for l in r}) != 1:+ sys.exit("art.py: uneven rows:\n" + "\n".join(f"{len(l):3} {l}" for l in r))+ return r+++def main():+ preview = sys.argv[sys.argv.index("--preview") + 1] if "--preview" in sys.argv else None+ pals = {}+ for pn, pal in PALETTES.items():+ keys = list(pal)+ pals[pn] = (keys, [pal[k] for k in keys])+ out_c = ["/* Generated by tools/art.py: don't edit. The game's own pictures. */", '#include "art.h"', ""]+ out_h = ["/* Generated by tools/art.py: don't edit. */", "#ifndef BT_ART_H", "#define BT_ART_H",+ "#include <stdint.h>", "",+ "typedef struct {", " uint8_t w, h, ax, ay; /* size, anchor */",+ " const uint32_t *pal; /* 0xRRGGBB; index 0 is see-through */",+ " const uint8_t *px; /* 4 bits a pixel, two a byte, the first in the low half */",+ "} art_t;", ""]+ for pn, (keys, cols) in pals.items():+ out_c.append(f"static const uint32_t PAL_{pn.upper()}[16] = {{0, " + ", ".join(f"0x{c:06X}" for c in cols) + "};")+ out_c.append("")+ imgs = {}+ for name, text, pn, (ax, ay) in PICTURES:+ r = rows(text)+ keys, cols = pals[pn]+ w, h = len(r[0]), len(r)+ idx = []+ for line in r:+ for ch in line:+ idx.append(0 if ch == "." else keys.index(ch) + 1)+ if len(idx) % 2:+ idx.append(0)+ data = [idx[i] | idx[i + 1] << 4 for i in range(0, len(idx), 2)]+ out_c.append(f"static const uint8_t PX_{name.upper()}[{len(data)}] = {{")+ for i in range(0, len(data), 24):+ out_c.append(" " + ", ".join(str(b) for b in data[i:i + 24]) + ",")+ out_c.append("};")+ out_c.append(f"const art_t ART_{name.upper()} = {{{w}, {h}, {ax}, {ay}, PAL_{pn.upper()}, PX_{name.upper()}}};")+ out_h.append(f"extern const art_t ART_{name.upper()};")+ imgs[name] = (r, pn)+ out_h += ["#endif", ""]+ with open(os.path.join(SRC, "art.c"), "w") as f:+ f.write("\n".join(out_c) + "\n")+ with open(os.path.join(SRC, "art.h"), "w") as f:+ f.write("\n".join(out_h))+ if preview:+ from PIL import Image+ os.makedirs(preview, exist_ok=True)+ for name, (r, pn) in imgs.items():+ pal = PALETTES[pn]+ im = Image.new("RGB", (len(r[0]) * 6, len(r) * 6), (40, 40, 40))+ for y, line in enumerate(r):+ for x, ch in enumerate(line):+ if ch != ".":+ c = pal[ch]+ for dy in range(6):+ for dx in range(6):+ im.putpixel((x * 6 + dx, y * 6 + dy), (c >> 16, c >> 8 & 255, c & 255))+ im.save(os.path.join(preview, name + ".png"))+++if __name__ == "__main__":+ main()diff --git a/games/badtime/tools/attacks.py b/games/badtime/tools/attacks.pynew file mode 100644index 00000000..2cc8df11--- /dev/null+++ b/games/badtime/tools/attacks.py@@ -0,0 +1,529 @@+#!/usr/bin/env python3+"""Bad Time Simulator's attacks: jcw87's attack scripts (Files/*.csv of github.com/jcw87/c2-sans-fight) turned+into the game's tables, and played by a second, separate reading of them to check the game's own.++ attacks.py REF [SPEC] REF: the project's folder; writes src/attacks.c, src/attacks.h, tests/expect.h+ (and SPEC/attacks.md, every attack tick by tick, when SPEC is given)++A script line is: a delay in seconds (a number or $variable), a function, its arguments. The game keeps every line+(labels and sounds become NOPs, so line numbers stay right for the jumps), the arguments as numbers from a pool or+variables, the delays turned into ticks of 1/30 s when the game runs them. tests/expect.h holds, for each attack,+what this reading of the scripts does with a fixed random seed: the tick it ends, the number of things it makes and+a hash of them (the zone, the heart, the bones, blasters, platforms, the opponent's poses), which the game's own+interpreter must match exactly (make test). Read the reference with python3 -I; never run it."""+import math+import os+import sys++HERE = os.path.dirname(os.path.abspath(__file__))+GAME = os.path.join(HERE, "..")++# the scripts, in the order the fight first plays them (the game's attack numbers)+ATTACKS = ["sans_intro", "sans_bonegap1", "sans_bluebone", "sans_bonegap2", "sans_platforms1", "sans_platforms2",+ "sans_platforms3", "sans_platforms4", "sans_platformblaster", "sans_platforms4hard", "sans_bonegap1fast",+ "sans_boneslideh", "sans_platformblasterfast", "sans_spare", "sans_multi1", "sans_randomblaster1",+ "sans_multi2", "sans_bonestab1", "sans_bonestab2", "sans_randomblaster2", "sans_boneslidev", "sans_multi3",+ "sans_bonestab3", "sans_final"]++OPS = ["NOP", "SET", "ADD", "SUB", "MUL", "DIV", "MOD", "FLOOR", "SIN", "COS", "ANGLE", "RND", "DEG", "RAD",+ "JMPABS", "JMPREL", "JMPZ", "JMPNZ", "JMPE", "JMPNE", "JMPL", "JMPNL", "JMPG", "JMPNG", "TLPause", "TLResume",+ "EndAttack", "BlackScreen", "BoneH", "BoneV", "BoneHRepeat", "BoneVRepeat", "SineBones", "BoneStab",+ "GasterBlaster", "Platform", "PlatformRepeat", "HeartMode", "HeartTeleport", "HeartMaxFallSpeed", "SansSlam",+ "SansSlamDamage", "CombatZoneSpeed", "CombatZoneResize", "CombatZoneResizeInstant", "GetHeartPos",+ "SansAnimation", "SansBody", "SansTorso", "SansHead", "SansSweat", "SansX", "SansRepeat", "SansEndRepeat",+ "SansText"]+NOPS = {"Sound", "Music"}+# the opponent's poses, as numbers+ANIM = {"": 0, "Idle": 1, "HeadBob": 2, "Tired": 3}+BODY = {"HandUp": 0, "HandDown": 1, "HandLeft": 2, "HandRight": 3}+TORSO = {"Default": 0, "Shrug": 1}+HEAD = {"Default": 0, "LookLeft": 1, "Wink": 2, "ClosedEyes": 3, "NoEyes": 4, "BlueEye": 5, "Tired1": 6, "Tired2": 7}+# the two lines the scripts say: the game says its own words there (text.c)+TALK = {"ready?": 0, "here we go.": 1}+# first arguments that name a variable to write+WRITES = {"SET", "ADD", "SUB", "MUL", "DIV", "MOD", "FLOOR", "SIN", "COS", "ANGLE", "RND", "DEG", "RAD"}+JUMPS = {"JMPABS", "JMPZ", "JMPNZ", "JMPE", "JMPNE", "JMPL", "JMPNL", "JMPG", "JMPNG"}+++def die(msg):+ sys.exit("attacks.py: " + msg)+++def parse_float(s):+ """JavaScript's parseFloat, for the scripts' numbers (0 when there's none)"""+ s = s.strip()+ n = 0+ while n < len(s) and (s[n].isdigit() or s[n] in "+-.eE"):+ n += 1+ while n:+ try:+ return float(s[:n])+ except ValueError:+ n -= 1+ return 0.0+++def load(ref, name):+ rows = []+ with open(os.path.join(ref, "Files", name + ".csv"), encoding="utf-8") as f:+ for raw in f.read().splitlines():+ if not raw.strip():+ continue+ rows.append(raw.split(","))+ return rows+++class Pool:+ def __init__(self):+ self.vals, self.at = [], {}++ def add(self, v):+ v = float(v)+ key = repr(v)+ if key not in self.at:+ self.at[key] = len(self.vals)+ self.vals.append(v)+ return self.at[key]+++def compile_attack(rows, pool, name):+ """each line: (op, [delay, args...]) with args ('n', value) or ('v', variable)"""+ labels, vars_ = {}, {"pi": 0}+ for i, r in enumerate(rows, 1):+ f = r[1].strip() if len(r) > 1 else ""+ if f.startswith(":"):+ labels[f[1:]] = i++ def var(nm):+ if nm not in vars_:+ vars_[nm] = len(vars_)+ return vars_[nm]++ def arg(tok, kind="num"):+ tok = tok.strip()+ if tok.startswith("$"):+ return ("v", var(tok[1:]))+ if kind == "target":+ if tok.isdigit():+ return ("n", float(tok))+ if tok not in labels:+ die(f"{name}: no label {tok}")+ return ("n", float(labels[tok]))+ return ("n", parse_float(tok))++ out = []+ for i, r in enumerate(rows, 1):+ f = r[1].strip() if len(r) > 1 else ""+ a = [c for c in r[2:]]+ while a and a[-1].strip() == "":+ a.pop()+ delay = arg(r[0])+ if f.startswith(":") or f in NOPS:+ out.append(("NOP", [delay]))+ continue+ if f not in OPS:+ die(f"{name} line {i}: unknown function {f}")+ if f in WRITES:+ args = [("n", float(var(a[0].strip())))] + [arg(t) for t in a[1:]]+ elif f in JUMPS:+ args = [arg(a[0], "target")] + [arg(t) for t in a[1:]]+ elif f == "GetHeartPos":+ args = [("n", float(var(a[0].strip()))), ("n", float(var(a[1].strip())))]+ elif f == "SansAnimation":+ args = [("n", float(ANIM[a[0].strip() if a else ""]))]+ elif f == "SansBody":+ args = [("n", float(BODY[a[0].strip()]))]+ elif f == "SansTorso":+ args = [("n", float(TORSO[a[0].strip()]))]+ elif f == "SansHead":+ args = [("n", float(HEAD[a[0].strip()]))]+ elif f == "SansText":+ args = [("n", float(TALK[a[0].strip()]))]+ elif f == "CombatZoneResize":+ fin = a[4].strip() if len(a) > 4 else ""+ if fin not in ("", "TLResume"):+ die(f"{name} line {i}: finishing action {fin}")+ args = [arg(t) for t in a[:4]] + [("n", 1.0 if fin else 0.0)]+ else:+ args = [arg(t) for t in a]+ out.append((f, [delay] + args))+ for f, args in out:+ for k, v in args:+ if k == "n":+ pool.add(v)+ return out, vars_+++# ---------------------------------------------------------------- the same sums as the game (sim.c), in doubles+PI = 3.14159265358979323846+D2R = PI / 180.0+++def fl(x):+ return float(math.floor(x))+++def sin_deg(d):+ """sin of d degrees: as the game's bt_sin_deg, step by step (same doubles, same rounding)"""+ d = d - 360.0 * fl(d / 360.0)+ if d == 0.0 or d == 180.0:+ return 0.0+ if d == 90.0:+ return 1.0+ if d == 270.0:+ return -1.0+ s = 1.0+ if d >= 180.0:+ d = d - 180.0+ s = -1.0+ if d > 90.0:+ d = 180.0 - d+ x = d * D2R+ x2 = x * x+ p = -2.5052108385441720e-08+ p = p * x2 + 2.7557319223985893e-06+ p = p * x2 - 1.9841269841269841e-04+ p = p * x2 + 8.3333333333333333e-03+ p = p * x2 - 1.6666666666666666e-01+ p = p * x2 + 1.0+ return s * (x * p)+++def cos_deg(d):+ return sin_deg(d + 90.0)+++def atan_unit(t):+ """atan of t in [0, 1], radians"""+ big = t > 0.4142135623730950+ if big:+ t = (t - 1.0) / (t + 1.0)+ t2 = t * t+ p = 0.0769230769230769+ p = p * t2 - 0.0833333333333333+ p = p * t2 + 0.0909090909090909+ p = p * t2 - 0.1+ p = p * t2 + 0.1111111111111111+ p = p * t2 - 0.1428571428571428+ p = p * t2 + 0.2+ p = p * t2 - 0.3333333333333333+ r = t + t * t2 * p+ if big:+ r = r + 0.7853981633974483+ return r+++def atan2_deg(y, x):+ if x == 0.0 and y == 0.0:+ return 0.0+ ax, ay = abs(x), abs(y)+ if ay <= ax:+ a = atan_unit(ay / ax)+ else:+ a = 1.5707963267948966 - atan_unit(ax / ay)+ if x < 0.0:+ a = 3.141592653589793 - a+ if y < 0.0:+ a = -a+ return a / D2R+++class Rng:+ """xorshift32, as the game's"""+ def __init__(self, seed):+ self.x = seed & 0xFFFFFFFF or 1++ def next(self):+ x = self.x+ x ^= (x << 13) & 0xFFFFFFFF+ x ^= x >> 17+ x ^= (x << 5) & 0xFFFFFFFF+ self.x = x+ return x++ def below(self, n):+ return (self.next() * n) >> 32 if n > 0 else 0+++def ticks(sec):+ v = math.floor(sec * 30.0 + 0.5)+ return int(v) if v > 0 else 0+++def ms(v):+ return int(math.floor(v * 1000.0 + 0.5))+++MENU = (33.0, 251.0, 608.0, 391.0)+DX = (1, 0, -1, 0)+DY = (0, 1, 0, -1)+HEART_TEST = (320.0, 304.0) # GetHeartPos while checking (the game's test hook says the same)+++def run(code, nvars, seed, limit=30 * 120):+ """plays one attack: the log of what it makes, and the tick it ends"""+ rng = Rng(seed)+ vars_ = [0.0] * nvars+ vars_[0] = PI+ zone = list(MENU)+ target = list(MENU)+ speed = 480+ finish = talk = False+ running, line, t = True, 1, 0+ log = []+ tick = 0+ ended = None++ def val(a):+ k, v = a+ return vars_[v] if k == "v" else v++ def emit(*items):+ log.append(" ".join(str(x) for x in (tick,) + items))++ while ended is None and tick < limit:+ # the script+ if running:+ n = 0+ while running and ended is None and 1 <= line <= len(code):+ op, args = code[line - 1]+ d = ticks(val(args[0]))+ if t < d:+ break+ a = [val(x) for x in args[1:]]+ jump = None+ if op == "NOP" or op == "TLResume":+ pass+ elif op == "SET":+ vars_[int(a[0])] = a[1]+ elif op == "ADD":+ vars_[int(a[0])] = a[1] + a[2]+ elif op == "SUB":+ vars_[int(a[0])] = a[1] - a[2]+ elif op == "MUL":+ vars_[int(a[0])] = a[1] * a[2]+ elif op == "DIV":+ vars_[int(a[0])] = a[1] / a[2] if a[2] != 0.0 else 0.0+ elif op == "MOD":+ vars_[int(a[0])] = math.fmod(a[1], a[2]) if a[2] != 0.0 else 0.0+ elif op == "FLOOR":+ vars_[int(a[0])] = fl(a[1])+ elif op == "SIN":+ vars_[int(a[0])] = sin_deg(a[1])+ elif op == "COS":+ vars_[int(a[0])] = cos_deg(a[1])+ elif op == "DEG":+ vars_[int(a[0])] = a[1] / D2R+ elif op == "RAD":+ vars_[int(a[0])] = a[1] * D2R+ elif op == "ANGLE":+ vars_[int(a[0])] = atan2_deg(a[4] - a[2], a[3] - a[1])+ elif op == "RND":+ vars_[int(a[0])] = float(rng.below(int(fl(a[1]))))+ elif op == "JMPABS":+ jump = int(a[0])+ elif op == "JMPREL":+ jump = line + int(fl(a[0]))+ elif op in JUMPS:+ x = a[1]+ y = a[2] if len(a) > 2 else 0.0+ ok = {"JMPZ": x == 0.0, "JMPNZ": x != 0.0, "JMPE": x == y, "JMPNE": x != y, "JMPL": x < y,+ "JMPNL": x >= y, "JMPG": x > y, "JMPNG": x <= y}[op]+ if ok:+ jump = int(a[0])+ elif op == "TLPause":+ running = False+ elif op == "EndAttack":+ ended = tick+ emit("EndAttack")+ elif op == "SansText":+ running = False+ emit("SansText", int(a[0]))+ talk = True # (checking: said and read at once)+ elif op == "GetHeartPos":+ vars_[int(a[0])], vars_[int(a[1])] = HEART_TEST+ emit("GetHeartPos")+ elif op in ("BoneHRepeat", "BoneVRepeat", "PlatformRepeat"):+ sx, sy, w, dr, sp, cnt, spc = a[0], a[1], a[2], a[3], a[4], int(fl(a[5])), int(fl(a[6]))+ di = int(fl(dr)) & 3+ for i in range(cnt):+ x = sx - DX[di] * spc * i+ y = sy - DY[di] * spc * i+ nm = {"BoneHRepeat": "BoneH", "BoneVRepeat": "BoneV", "PlatformRepeat": "Platform"}[op]+ tail = (0,)+ emit(nm, int(fl(x)), int(fl(y)), int(fl(w)), int(fl(dr)), int(fl(sp)), *tail)+ elif op == "SineBones":+ cnt, spc, sp, h = (int(fl(v)) for v in a[:4])+ for i in range(cnt):+ if spc > 0:+ x, di = zone[2] + spc * i, 2+ else:+ x, di = zone[0] + spc * i, 0+ sine = int(fl(sin_deg(i / 3.0 / D2R) * 28.0))+ y = zone[1] + 6+ emit("BoneV", int(fl(x)), int(fl(y)), h + sine, di, sp, 0)+ y = zone[1] + 6 + h + sine + 39+ emit("BoneV", int(fl(x)), int(fl(y)), int(fl(zone[3] - 5 - y)), di, sp, 0)+ elif op in ("BoneH", "BoneV"):+ v = [int(fl(x)) for x in a[:6]] + [0] * (6 - len(a[:6]))+ emit(op, *v)+ elif op == "Platform":+ v = [int(fl(x)) for x in a[:5]]+ emit(op, *v, 1 if len(a) > 5 and int(fl(a[5])) > 0 else 0)+ elif op == "BoneStab":+ emit(op, int(fl(a[0])), int(fl(a[1])), ms(a[2]), ms(a[3]))+ elif op == "GasterBlaster":+ emit(op, *[int(fl(x)) for x in a[:6]], ms(a[6]), ms(a[7]))+ elif op == "CombatZoneSpeed":+ speed = int(fl(a[0]))+ emit(op, speed)+ elif op == "CombatZoneResize":+ target = [a[0], a[1], a[2], a[3]]+ finish = a[4] != 0.0+ emit(op, *[ms(x) for x in target], int(finish))+ elif op == "CombatZoneResizeInstant":+ zone = [a[0], a[1], a[2], a[3]]+ target = list(zone)+ emit(op, *[ms(x) for x in zone])+ elif op in ("HeartMode", "HeartTeleport", "HeartMaxFallSpeed", "SansSlamDamage", "SansX",+ "SansSweat", "BlackScreen", "SansAnimation", "SansBody", "SansTorso", "SansHead"):+ emit(op, *[int(fl(x)) for x in a])+ elif op == "SansSlam":+ emit(op, int(fl(a[0])))+ elif op in ("SansRepeat", "SansEndRepeat"):+ emit(op)+ else:+ die("can't play " + op)+ if jump is not None:+ line = jump - 1+ t -= d+ line += 1+ n += 1+ if n >= 4000:+ die("a loop that never waits")+ if running:+ t += 1+ # the zone+ step = speed / 30.0+ for i in range(4):+ dd = target[i] - zone[i]+ if dd > 0:+ zone[i] += min(step, dd)+ elif dd < 0:+ zone[i] -= min(step, -dd)+ if finish and zone == target:+ finish = False+ running = True+ if talk:+ talk = False+ running = True+ tick += 1+ return log, ended+++def fnv(text):+ h = 2166136261+ for b in text.encode():+ h = ((h ^ b) * 16777619) & 0xFFFFFFFF+ return h+++def main():+ if len(sys.argv) < 2:+ die("attacks.py REF [SPEC]")+ ref = sys.argv[1]+ spec = sys.argv[2] if len(sys.argv) > 2 else None+ pool = Pool()+ pool.add(0.0)+ progs = []+ for name in ATTACKS:+ code, vars_ = compile_attack(load(ref, name), pool, name)+ progs.append((name, code, vars_))+ if len(pool.vals) >= 0x8000:+ die("too many numbers")++ # the tables+ words, lines, info = [], [], []+ for name, code, vars_ in progs:+ first = len(lines)+ for op, args in code:+ lines.append(len(words))+ words.append(OPS.index(op) | (len(args) - 1) << 8)+ for k, v in args:+ words.append(0x8000 | int(v) if k == "v" else pool.add(v))+ info.append((first, len(code), len(vars_)))+ lines.append(len(words))+ if len(words) >= 0x10000:+ die("too much code")+ src = os.path.join(GAME, "src")+ with open(os.path.join(src, "attacks.h"), "w") as f:+ f.write("/* Generated by tools/attacks.py from jcw87's attack scripts: don't edit. */\n")+ f.write("#ifndef BT_ATTACKS_H\n#define BT_ATTACKS_H\n#include <stdint.h>\n\n")+ f.write("enum {\n" + "".join(f" OP_{o.upper()},\n" for o in OPS) + " OP_COUNT\n};\n")+ f.write(f"#define N_ATTACKS {len(ATTACKS)}\n")+ f.write("enum {\n" + "".join(f" A_{n[5:].upper()},\n" for n in ATTACKS) + "};\n")+ f.write("typedef struct {\n uint16_t first, lines; /* its lines in atk_line */\n"+ " uint8_t vars; /* its variables ($pi first) */\n} atk_info_t;\n")+ f.write("/* a line: op | args << 8, the delay, then the arguments; a word with its top bit set is a variable,\n"+ " * otherwise a number in atk_pool */\n")+ f.write("extern const double atk_pool[];\nextern const uint16_t atk_code[];\nextern const uint16_t atk_line[];\n")+ f.write("extern const atk_info_t atk_info[N_ATTACKS];\nextern const char *const atk_name[N_ATTACKS];\n")+ f.write("#endif\n")+ with open(os.path.join(src, "attacks.c"), "w") as f:+ f.write("/* Generated by tools/attacks.py from jcw87's attack scripts: don't edit. */\n#include \"attacks.h\"\n\n")+ f.write(f"const double atk_pool[{len(pool.vals)}] = {{\n")+ for i in range(0, len(pool.vals), 6):+ f.write(" " + ", ".join(repr(v) for v in pool.vals[i:i + 6]) + ",\n")+ f.write("};\n")+ f.write(f"const uint16_t atk_code[{len(words)}] = {{\n")+ for i in range(0, len(words), 14):+ f.write(" " + ", ".join(str(w) for w in words[i:i + 14]) + ",\n")+ f.write("};\n")+ f.write(f"const uint16_t atk_line[{len(lines)}] = {{\n")+ for i in range(0, len(lines), 14):+ f.write(" " + ", ".join(str(w) for w in lines[i:i + 14]) + ",\n")+ f.write("};\n")+ f.write("const atk_info_t atk_info[N_ATTACKS] = {\n")+ for a, (n, (first, cnt, nv)) in zip(ATTACKS, zip(ATTACKS, info)):+ f.write(f" {{{first}, {cnt}, {nv}}}, /* {a} */\n")+ f.write("};\nconst char *const atk_name[N_ATTACKS] = {\n")+ names = ["Intro", "Bone gap", "Blue bones", "Bone gap 2", "Platforms", "Platforms 2", "Platforms 3",+ "Platforms 4", "Platform blasters", "Platforms 4 hard", "Bone gap fast", "Bone slide",+ "Platform blasters fast", "The break", "Multi 1", "Random blasters", "Multi 2", "Bone stab",+ "Bone stab 2", "Random blasters 2", "Bone slide 2", "Multi 3", "Bone stab 3", "The final attack"]+ f.write("".join(f" \"{n}\",\n" for n in names) + "};\n")++ # what the game's interpreter must do: per attack, its end, its count and hash, with seed 0x5EED0000 + n+ rows = []+ md = ["# The attacks, tick by tick\n",+ "Generated by games/badtime/tools/attacks.py from jcw87's scripts. One line per thing an attack makes or",+ "changes, at its tick (30 a second), in the original's 640 x 480 pixels: bones (x, y, length, direction 0",+ "right 1 down 2 left 3 up, speed px/s, colour 0 white 1 blue 2 orange), platforms (x, y, width, direction,",+ "speed, turns), bone stabs (side, depth, warning ms, stay ms), blasters (size, from x y, to x y, angle, wait",+ "ms, blast ms), the zone (left top right bottom, x1000), the heart and the opponent. Random choices with",+ "seed 0x5EED0000 + n; GetHeartPos says (320, 304).\n"]+ for n, (name, code, vars_) in enumerate(progs):+ log, end = run(code, len(vars_), 0x5EED0000 + n)+ if end is None:+ die(name + " never ends")+ text = "\n".join(log)+ rows.append((end, len(log), fnv(text)))+ md.append(f"## {n}. {name}: {end} ticks ({end / 30:.2f} s), {len(log)} events\n")+ md.append("```")+ md.extend(log)+ md.append("```\n")+ with open(os.path.join(GAME, "tests", "expect.h"), "w") as f:+ f.write("/* Generated by tools/attacks.py: each attack as a second reading of jcw87's scripts plays it (seed\n"+ " * 0x5EED0000 + n): the tick it ends, how many things it makes, the FNV-1a of their log. */\n")+ f.write("static const struct { uint32_t end, events, hash; } EXPECT[] = {\n")+ for (end, cnt, h), name in zip(rows, ATTACKS):+ f.write(f" {{{end}, {cnt}, 0x{h:08X}u}}, /* {name} */\n")+ f.write("};\n")+ if spec:+ with open(os.path.join(spec, "attacks.md"), "w") as f:+ f.write("\n".join(md))+ print(f"{len(ATTACKS)} attacks, {len(lines) - 1} lines, {len(words) * 2} bytes of code, {len(pool.vals)} numbers")+++if __name__ == "__main__":+ main()