Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

Universal Paperclips: the game from its web build, on the keypad

Mason3633017dbc1340f
diff --git a/games/paperclips/.gitignore b/games/paperclips/.gitignore
new file mode 100644
index 00000000..342d9dda
--- /dev/null
+++ b/games/paperclips/.gitignore
@@ -0,0 +1,2 @@
+output/
+build/
diff --git a/games/paperclips/Makefile b/games/paperclips/Makefile
new file mode 100644
index 00000000..b37846e6
--- /dev/null
+++ b/games/paperclips/Makefile
@@ -0,0 +1,59 @@
+# Universal Paperclips on the NumWorks calculator.
+# make output/paperclips.nwa
+# make check link it like the calculator does (output/paperclips.bin)
+# make host build/play: the game on a computer, for tests and pictures
+# make test the rules against the web build's numbers, a bot playing the whole game to its end through the
+# keys, saves (kept, damaged), random keys, the same game twice to the same hash (with the
+# sanitizers)
+Q ?= @
+CC = arm-none-eabi-gcc
+NWLINK ?= npx --yes -- [email protected]
+BUILD_DIR = output
+L = ../../launcher/src
+
+SRC = src/main.c src/sim.c src/projects.c src/num.c src/ui.c src/save.c src/plat_eadk.c src/font_data.c $(L)/gfx.c \
+ $(L)/xfont.c
+HEADERS = $(wildcard src/*.h) $(L)/gfx.h $(L)/font.h $(L)/np.h ../common/epsilon_app.h ../common/epsilon_files.h
+
+# (no -fsingle-precision-constant: the game's numbers are doubles, as in JavaScript)
+CFLAGS = -std=gnu11 $(shell $(NWLINK) eadk-cflags-device) -DPLATFORM_DEVICE=1 -DEPSILON_XL=1 -I$(L) -Isrc
+CFLAGS += -Os -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
+
+.PHONY: build check clean host test
+build: $(BUILD_DIR)/paperclips.nwa
+
+# apps get 133652 bytes of RAM on custom builds of Epsilon 25.2 (148928 or more on official ones)
+check: $(BUILD_DIR)/paperclips.nwa
+ $(Q) $(NWLINK) nwa-bin --ram-length 133652 $< $(BUILD_DIR)/paperclips.bin
+ @echo "BIN $(BUILD_DIR)/paperclips.bin: $$(wc -c < $(BUILD_DIR)/paperclips.bin) bytes"
+
+$(BUILD_DIR)/paperclips.nwa: $(SRC) $(HEADERS) $(BUILD_DIR)/icon.o
+ @echo "LD $@"
+ $(Q) $(CC) $(CFLAGS) $(LDFLAGS) $(SRC) $(BUILD_DIR)/icon.o -lm -o $@
+ $(Q) arm-none-eabi-strip --strip-unneeded $@
+
+$(BUILD_DIR)/icon.o: src/icon.png | $(BUILD_DIR)
+ @echo "ICON $<"
+ $(Q) $(NWLINK) png-icon-o $< $@
+
+$(BUILD_DIR):
+ $(Q) mkdir -p $@
+
+# on a computer: tests/play.c checks the rules, plays the whole game and random keys, and saves pictures
+HOST_SRC = tests/play.c src/sim.c src/projects.c src/num.c src/ui.c src/save.c src/font_data.c $(L)/gfx.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) $(wildcard tests/*.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) build
diff --git a/games/paperclips/src/font_data.c b/games/paperclips/src/font_data.c
new file mode 100644
index 00000000..52191f4c
--- /dev/null
+++ b/games/paperclips/src/font_data.c
@@ -0,0 +1,857 @@
+/* Generated by tools/fontgen.py from Nunito (SIL Open Font License 1.1). */
+#include "font.h"
+
+const uint16_t np_font_extra[4] = {169, 233, 243, 248};
+const int np_font_extra_count = 4;
+
+static const uint8_t np_font_title_data[8022] = {
+ 128,222,36,255,207,242,255,29,255,191,224,255,9,252,127,160,255,6,248,79,112,255,2,226,
+ 11,0,0,0,233,61,243,255,58,255,175,144,223,3,231,61,64,238,245,255,10,252,223,255,
+ 159,192,255,220,255,7,250,175,251,95,128,255,136,255,3,246,111,162,8,16,153,1,0,96,
+ 143,48,206,0,0,252,9,248,13,0,241,111,176,175,32,203,255,188,255,93,247,255,255,255,
+ 255,27,199,223,168,255,40,0,252,10,248,13,0,224,127,176,175,0,48,255,4,254,7,128,
+ 252,207,252,223,26,254,255,255,255,255,67,254,140,252,142,6,224,127,176,191,0,32,255,4,
+ 253,8,0,242,30,208,63,0,0,17,0,17,0,0,0,0,64,73,0,0,0,0,0,254,
+ 14,0,0,0,0,240,255,0,0,0,0,182,255,223,57,0,16,252,255,255,255,95,0,250,
+ 255,255,255,255,7,241,255,61,16,131,25,48,255,143,0,0,0,0,242,255,142,20,0,0,
+ 0,252,255,255,223,40,0,32,253,255,255,255,111,0,16,182,254,255,255,47,0,0,0,98,
+ 253,255,6,17,0,0,80,255,127,226,175,70,100,252,255,101,255,255,255,255,255,13,226,255,
+ 255,255,255,45,0,113,235,255,191,6,0,0,0,240,255,0,0,0,0,0,252,12,0,0,
+ 0,0,16,19,0,0,0,0,216,223,8,0,0,228,12,0,0,160,255,255,175,0,0,253,
+ 47,0,0,243,223,213,255,3,112,255,10,0,0,246,143,144,255,6,225,239,2,0,0,247,
+ 143,128,255,7,249,143,0,0,0,246,143,144,255,54,255,29,0,0,0,242,223,213,255,194,
+ 255,69,186,74,0,144,255,255,159,245,191,247,255,255,6,0,216,223,24,253,79,255,142,254,
+ 31,0,0,0,128,255,105,255,9,249,111,0,0,0,242,239,113,255,8,248,127,0,0,0,
+ 250,127,112,255,8,248,127,0,0,64,255,13,64,255,28,252,79,0,0,192,255,4,0,252,
+ 255,255,12,0,0,192,175,0,0,194,255,191,2,0,0,16,21,0,0,0,66,2,0,0,
+ 96,236,223,7,0,0,0,249,255,255,159,0,0,64,255,191,249,255,2,0,112,255,31,192,
+ 255,4,0,96,255,63,225,255,2,0,32,255,207,253,175,32,3,0,248,255,255,28,242,143,
+ 48,253,255,239,1,247,159,227,255,255,255,28,251,111,251,255,115,255,223,255,31,254,207,0,
+ 247,255,255,10,254,239,1,112,255,255,4,250,255,141,216,255,255,62,226,255,255,255,255,255,
+ 223,32,233,255,239,41,228,207,0,0,50,3,0,16,19,231,61,255,175,255,159,253,127,251,
+ 95,248,63,162,8,0,195,27,0,252,95,96,255,63,208,255,13,243,255,9,248,255,6,251,
+ 255,3,253,255,1,255,255,0,255,255,0,254,255,0,253,255,1,251,255,3,248,255,6,244,
+ 255,9,208,255,13,96,255,63,0,252,95,0,195,28,214,8,0,253,95,0,250,223,0,245,
+ 255,6,241,255,11,208,255,31,176,255,63,144,255,111,128,255,127,128,255,127,128,255,127,144,
+ 255,111,176,255,79,208,255,31,241,255,11,245,255,6,250,223,0,253,95,0,214,8,0,0,
+ 134,48,25,0,224,111,253,7,0,248,254,46,48,220,255,239,156,246,255,255,255,14,131,255,
+ 239,36,0,254,233,111,0,176,29,232,4,0,0,49,0,0,0,0,208,111,0,0,0,0,
+ 255,8,0,0,0,240,143,0,0,65,68,255,73,52,208,255,255,255,255,159,253,255,255,255,
+ 255,25,68,244,159,68,3,0,0,255,8,0,0,0,240,143,0,0,0,0,253,6,0,0,
+ 0,16,3,0,0,128,222,52,255,223,243,255,15,232,223,0,248,9,243,46,48,93,0,48,
+ 68,68,1,248,255,255,30,251,255,255,63,179,187,187,8,144,222,51,255,175,243,255,10,249,
+ 61,0,0,16,187,2,0,0,247,111,0,0,192,255,3,0,32,255,13,0,0,246,143,0,
+ 0,176,255,3,0,16,255,13,0,0,246,159,0,0,176,255,4,0,16,255,14,0,0,245,
+ 159,0,0,160,255,5,0,16,254,14,0,0,245,175,0,0,160,255,5,0,0,254,31,0,
+ 0,241,175,0,0,0,115,1,0,0,0,64,235,223,41,0,0,247,255,255,239,4,48,255,
+ 255,255,255,29,160,255,95,145,255,111,224,255,12,16,255,175,242,255,9,0,253,223,243,255,
+ 8,0,252,255,244,255,8,0,251,255,243,255,8,0,252,239,242,255,9,0,253,223,224,255,
+ 13,16,255,175,144,255,111,145,255,95,32,255,255,255,255,13,0,246,255,255,239,3,0,64,
+ 235,223,41,0,0,0,215,159,0,0,0,212,255,255,0,0,144,255,255,255,0,0,245,255,
+ 255,255,0,0,243,191,253,255,0,0,48,4,251,255,0,0,0,0,251,255,0,0,0,0,
+ 251,255,0,0,0,0,251,255,0,0,0,0,251,255,0,0,0,0,251,255,0,0,16,68,
+ 252,255,68,1,226,255,255,255,255,46,243,255,255,255,255,63,128,187,187,187,187,8,64,218,
+ 255,157,3,144,255,255,255,255,4,255,255,255,255,223,128,191,70,230,255,63,0,0,0,248,
+ 255,4,0,0,144,255,47,0,0,32,255,207,0,0,16,253,255,5,0,16,252,255,9,0,
+ 16,252,255,10,0,16,252,255,10,0,0,251,255,10,0,0,247,255,255,255,255,153,255,255,
+ 255,255,255,228,255,255,255,255,9,0,165,253,223,41,0,176,255,255,255,255,4,240,255,255,
+ 255,255,13,128,174,69,230,255,63,0,0,0,128,255,63,0,0,0,176,255,30,0,64,187,
+ 254,255,7,0,208,255,255,175,0,0,160,255,255,255,11,0,0,67,164,255,127,0,0,0,
+ 16,255,191,128,107,2,113,255,191,243,255,255,255,255,127,242,255,255,255,255,28,48,234,255,
+ 255,142,1,0,0,50,35,0,0,0,0,0,178,191,0,0,0,0,192,255,63,0,0,0,
+ 112,255,255,4,0,0,48,255,255,79,0,0,0,252,255,255,4,0,0,247,239,255,79,0,
+ 0,243,207,248,255,4,0,192,255,131,255,79,0,128,255,8,248,255,4,32,255,143,184,255,
+ 159,6,247,255,255,255,255,255,86,255,255,255,255,255,111,80,135,136,251,255,105,0,0,0,
+ 112,255,63,0,0,0,0,194,175,0,0,145,187,187,187,171,1,247,255,255,255,255,7,248,
+ 255,255,255,255,4,248,255,71,68,36,0,248,255,4,0,0,0,248,255,169,171,5,0,248,
+ 255,255,255,191,1,248,255,255,255,255,9,246,223,70,248,255,31,64,4,0,144,255,63,0,
+ 0,0,128,255,63,196,74,1,228,255,31,251,255,255,255,255,10,249,255,255,255,223,2,112,
+ 252,255,239,25,0,0,16,67,3,0,0,0,0,165,254,190,22,0,177,255,255,255,191,0,
+ 251,255,255,255,239,80,255,239,71,149,109,176,255,47,0,0,0,241,255,43,184,90,0,243,
+ 255,234,255,255,10,244,255,255,255,255,111,243,255,207,116,255,207,242,255,47,0,250,239,224,
+ 255,15,0,248,255,160,255,95,16,253,207,48,255,255,236,255,127,0,247,255,255,255,12,0,
+ 80,253,255,159,1,0,0,32,51,1,0,128,187,187,187,187,91,48,255,255,255,255,255,31,
+ 242,255,255,255,255,255,1,116,136,136,251,255,11,0,0,0,209,255,63,0,0,0,112,255,
+ 175,0,0,0,16,254,255,2,0,0,0,249,255,8,0,0,0,242,255,30,0,0,0,176,
+ 255,127,0,0,0,64,255,223,0,0,0,0,252,255,6,0,0,0,245,255,12,0,0,0,
+ 192,255,79,0,0,0,0,250,159,0,0,0,0,0,51,0,0,0,0,0,129,236,239,107,
+ 0,32,254,255,255,255,28,176,255,223,235,255,127,224,255,11,16,254,191,224,255,8,0,252,
+ 175,160,255,110,132,255,111,16,252,255,255,255,9,32,252,255,255,255,6,208,255,207,236,255,
+ 95,245,255,5,16,252,207,247,255,0,0,248,255,246,255,5,16,252,239,242,255,223,236,255,
+ 159,80,255,255,255,255,28,0,146,236,239,107,0,0,145,253,190,5,0,48,254,255,255,175,
+ 0,192,255,255,255,255,8,244,255,25,211,255,31,247,255,1,128,255,111,247,255,1,128,255,
+ 159,245,255,25,227,255,191,225,255,255,255,255,191,96,255,255,191,255,191,0,197,239,57,255,
+ 159,0,0,0,112,255,95,128,56,16,230,255,30,245,255,255,255,255,6,245,255,255,255,143,
+ 0,96,253,255,207,5,0,0,32,51,1,0,0,32,87,16,254,143,243,255,27,254,127,32,
+ 87,0,0,0,0,0,0,233,61,243,255,58,255,175,144,223,3,32,103,17,254,191,243,255,
+ 31,254,191,32,103,1,0,0,0,0,0,232,77,243,255,61,255,255,128,254,13,128,159,48,
+ 239,2,211,5,0,0,0,48,233,7,0,16,199,255,207,0,164,255,255,191,67,253,255,223,
+ 39,0,254,239,74,0,0,224,255,158,3,0,0,212,255,255,124,1,0,64,250,255,255,58,
+ 0,0,113,253,255,11,0,0,0,148,126,116,136,136,136,120,226,255,255,255,255,191,251,255,
+ 255,255,255,7,0,0,0,0,0,65,68,68,68,68,208,255,255,255,255,159,253,255,255,255,
+ 255,26,68,68,68,68,4,193,108,1,0,0,0,243,255,175,4,0,0,112,253,255,223,23,
+ 0,0,64,234,255,255,26,0,0,16,198,255,111,0,0,16,182,255,111,0,48,233,255,255,
+ 26,96,253,255,223,39,0,243,255,175,4,0,0,193,124,1,0,0,0,32,200,254,157,2,
+ 245,255,255,255,62,251,255,255,255,191,247,108,84,254,255,16,0,0,252,239,0,0,80,255,
+ 143,0,0,227,255,28,0,0,251,159,0,0,0,255,11,0,0,0,254,4,0,0,0,50,
+ 0,0,0,48,186,5,0,0,208,255,47,0,0,224,255,63,0,0,128,255,12,0,0,0,
+ 50,0,0,0,0,48,200,254,222,90,0,0,0,0,178,255,255,255,255,223,4,0,0,228,
+ 255,74,1,33,214,255,5,0,227,223,3,0,0,0,144,239,2,192,239,2,113,171,21,87,
+ 192,159,64,255,6,210,255,255,252,15,245,14,249,14,176,255,140,253,223,32,255,210,175,48,
+ 255,11,48,255,10,240,79,254,8,246,95,0,242,143,16,255,243,143,112,255,4,80,255,5,
+ 243,31,254,9,246,127,16,252,79,128,207,192,191,32,255,207,254,255,156,255,5,247,47,96,
+ 255,255,104,255,255,8,16,254,10,48,119,3,48,119,3,0,112,255,7,0,0,0,0,0,
+ 0,0,144,255,75,0,0,64,187,0,0,0,112,255,239,188,252,255,13,0,0,0,32,216,
+ 255,255,223,24,0,0,0,0,0,16,67,19,0,0,0,0,0,0,0,230,110,0,0,0,
+ 0,0,32,255,255,3,0,0,0,0,144,255,255,10,0,0,0,0,242,255,255,47,0,0,
+ 0,0,248,223,253,159,0,0,0,16,254,127,247,239,1,0,0,112,255,30,225,255,8,0,
+ 0,225,255,9,144,255,30,0,0,246,255,3,48,255,127,0,0,253,255,255,255,255,223,0,
+ 80,255,255,255,255,255,255,6,192,255,255,255,255,255,255,12,244,255,7,0,0,112,255,79,
+ 249,239,1,0,0,16,254,175,245,127,0,0,0,0,246,111,16,1,0,0,0,0,16,1,
+ 146,187,187,187,122,2,144,255,255,255,255,255,6,251,255,255,255,255,255,179,255,15,0,161,
+ 255,127,251,255,0,0,244,255,183,255,15,0,161,255,47,251,255,255,255,255,94,176,255,255,
+ 255,255,191,2,251,255,187,203,255,239,178,255,15,0,64,255,159,251,255,0,0,241,255,187,
+ 255,15,0,113,255,175,251,255,255,255,255,255,148,255,255,255,255,255,7,145,187,187,187,138,
+ 2,0,0,16,183,254,206,57,0,80,254,255,255,255,95,80,255,255,255,255,255,25,254,255,
+ 125,68,215,62,247,255,28,0,0,0,192,255,95,0,0,0,0,254,255,1,0,0,0,240,
+ 255,15,0,0,0,0,254,255,1,0,0,0,192,255,95,0,0,0,0,247,255,29,0,0,
+ 0,16,254,255,125,68,216,62,80,255,255,255,255,255,10,80,254,255,255,255,111,0,16,183,
+ 254,206,57,0,146,187,187,171,89,1,0,144,255,255,255,255,239,7,0,251,255,255,255,255,
+ 255,11,176,255,79,68,182,255,255,7,251,255,0,0,96,255,239,177,255,15,0,0,192,255,
+ 79,251,255,0,0,0,249,255,183,255,15,0,0,128,255,127,251,255,0,0,0,249,255,183,
+ 255,15,0,0,192,255,79,251,255,0,0,112,255,239,176,255,79,68,182,255,255,7,251,255,
+ 255,255,255,255,10,144,255,255,255,255,239,7,0,146,187,187,187,89,1,0,0,146,187,187,
+ 187,187,149,255,255,255,255,223,251,255,255,255,255,186,255,79,68,68,3,251,255,0,0,0,
+ 176,255,15,0,0,0,251,255,255,255,223,178,255,255,255,255,95,251,255,255,255,223,178,255,
+ 15,0,0,0,251,255,0,0,0,176,255,15,0,0,0,251,255,255,255,255,168,255,255,255,
+ 255,223,211,255,255,255,255,8,145,187,187,187,187,148,255,255,255,255,191,251,255,255,255,255,
+ 184,255,79,68,68,3,251,255,0,0,0,176,255,15,0,0,0,251,255,187,187,139,176,255,
+ 255,255,255,63,251,255,255,255,239,178,255,79,68,68,2,251,255,0,0,0,176,255,15,0,
+ 0,0,251,255,0,0,0,176,255,15,0,0,0,246,175,0,0,0,0,50,0,0,0,0,
+ 0,0,166,253,239,123,1,0,211,255,255,255,255,46,64,255,255,255,255,255,95,209,255,239,
+ 72,100,233,28,246,255,29,0,0,0,0,251,255,6,0,0,0,0,254,255,2,16,119,119,
+ 6,255,255,0,144,255,255,111,254,255,1,128,255,255,143,252,255,4,0,67,252,143,248,255,
+ 11,0,0,251,143,242,255,191,20,32,252,143,128,255,255,255,255,255,127,0,248,255,255,255,
+ 255,111,0,48,234,255,255,207,7,0,0,0,50,35,0,0,212,126,0,0,0,212,126,250,
+ 239,0,0,0,251,239,251,255,0,0,0,251,255,251,255,0,0,0,251,255,251,255,0,0,
+ 0,251,255,251,255,68,68,68,252,255,251,255,255,255,255,255,255,251,255,255,255,255,255,255,
+ 251,255,255,255,255,255,255,251,255,0,0,0,251,255,251,255,0,0,0,251,255,251,255,0,
+ 0,0,251,255,251,255,0,0,0,251,255,250,239,0,0,0,250,239,212,126,0,0,0,212,
+ 126,212,126,250,239,251,255,251,255,251,255,251,255,251,255,251,255,251,255,251,255,251,255,251,
+ 255,251,255,250,239,212,126,0,0,231,77,0,0,254,191,0,0,255,191,0,0,255,191,0,
+ 0,255,191,0,0,255,191,0,0,255,191,0,0,255,191,0,0,255,191,0,0,255,191,0,
+ 0,255,191,0,80,255,175,194,255,255,111,245,255,255,12,193,255,141,1,212,126,0,0,80,
+ 237,181,255,14,0,96,255,191,251,255,0,80,255,255,181,255,15,64,255,255,6,251,255,48,
+ 254,255,6,176,255,47,254,255,7,0,251,255,253,255,7,0,176,255,255,255,63,0,0,251,
+ 255,251,255,46,0,176,255,31,252,255,29,0,251,255,16,253,255,28,176,255,15,32,253,255,
+ 11,251,255,0,32,254,255,169,255,14,0,48,254,255,212,126,0,0,64,253,8,162,75,0,
+ 0,0,160,255,14,0,0,0,251,255,0,0,0,176,255,15,0,0,0,251,255,0,0,0,
+ 176,255,15,0,0,0,251,255,0,0,0,176,255,15,0,0,0,251,255,0,0,0,176,255,
+ 15,0,0,0,251,255,0,0,0,176,255,79,68,68,3,251,255,255,255,255,169,255,255,255,
+ 255,223,211,255,255,255,239,7,194,175,0,0,0,0,177,191,112,255,127,0,0,0,144,255,
+ 63,248,255,30,0,0,48,255,255,132,255,255,9,0,0,251,255,79,248,255,255,2,0,245,
+ 255,255,132,255,255,191,0,208,255,255,79,248,255,255,79,96,255,255,255,132,255,175,255,28,
+ 254,175,255,79,248,255,242,255,252,207,244,255,132,255,15,248,255,255,68,255,79,248,255,16,
+ 253,255,10,244,255,132,255,15,80,255,46,64,255,79,248,255,0,128,92,0,244,255,116,255,
+ 15,0,0,0,48,255,63,210,143,0,0,0,0,176,191,0,228,94,0,0,0,248,45,251,
+ 255,3,0,0,255,127,251,255,46,0,0,255,143,251,255,207,1,0,255,143,251,255,255,10,
+ 0,255,143,251,255,255,127,0,255,143,251,223,254,255,5,255,143,251,191,245,255,46,255,143,
+ 251,191,128,255,223,255,143,251,191,0,250,255,255,143,251,191,0,209,255,255,143,251,191,0,
+ 32,254,255,143,251,191,0,0,244,255,143,251,191,0,0,112,255,127,228,94,0,0,0,231,
+ 45,0,16,199,254,206,23,0,0,80,254,255,255,255,78,0,80,255,255,255,255,255,79,16,
+ 254,255,109,100,252,255,30,247,255,28,0,16,253,255,183,255,95,0,0,80,255,191,254,255,
+ 1,0,0,241,255,254,255,15,0,0,0,255,255,254,255,1,0,0,241,255,206,255,79,0,
+ 0,64,255,207,248,255,11,0,0,251,255,40,255,255,41,32,250,255,47,112,255,255,255,255,
+ 255,127,0,128,255,255,255,255,143,0,0,64,251,255,255,75,0,0,0,0,49,52,1,0,
+ 0,146,187,187,187,122,2,144,255,255,255,255,239,5,251,255,255,255,255,239,178,255,79,68,
+ 197,255,143,251,255,0,0,242,255,187,255,15,0,16,255,191,251,255,0,32,249,255,185,255,
+ 255,255,255,255,63,251,255,255,255,255,143,176,255,255,255,239,75,0,251,255,0,0,0,0,
+ 176,255,15,0,0,0,0,251,255,0,0,0,0,176,255,15,0,0,0,0,246,175,0,0,
+ 0,0,0,50,0,0,0,0,0,0,16,199,254,206,23,0,0,80,254,255,255,255,78,0,
+ 80,255,255,255,255,255,79,16,254,255,109,100,252,255,30,247,255,28,0,16,253,255,183,255,
+ 95,0,0,80,255,191,254,255,1,0,0,241,255,254,255,15,0,0,0,255,255,254,255,1,
+ 0,0,241,255,206,255,79,0,0,64,255,207,248,255,11,0,0,251,255,40,255,255,41,32,
+ 250,255,47,112,255,255,255,255,255,127,0,128,255,255,255,255,143,0,0,64,251,255,255,223,
+ 2,0,0,0,49,116,255,223,1,0,0,0,0,144,255,159,0,0,0,0,0,225,255,31,
+ 0,0,0,0,0,245,223,0,0,0,0,0,0,83,1,146,187,187,187,122,2,144,255,255,
+ 255,255,255,6,251,255,255,255,255,255,179,255,79,68,197,255,159,251,255,0,0,242,255,187,
+ 255,15,0,16,255,191,251,255,68,84,252,255,183,255,255,255,255,255,30,251,255,255,255,255,
+ 60,176,255,143,216,255,78,0,251,255,0,242,255,63,176,255,15,0,249,255,11,251,255,0,
+ 16,254,255,180,255,15,0,112,255,175,246,175,0,0,176,255,8,50,0,0,0,48,3,0,
+ 113,236,239,140,2,0,228,255,255,255,239,3,226,255,255,255,255,127,128,255,191,69,133,221,
+ 2,251,255,1,0,0,0,160,255,143,2,0,0,0,246,255,255,173,39,0,0,250,255,255,
+ 255,175,1,0,181,255,255,255,175,0,0,0,99,250,255,47,0,0,0,0,249,255,99,190,
+ 38,0,211,255,47,254,255,255,255,255,207,176,255,255,255,255,239,2,96,251,255,255,158,1,
+ 0,0,32,68,2,0,0,181,187,187,187,187,187,59,254,255,255,255,255,255,175,253,255,255,
+ 255,255,255,159,114,136,248,255,141,136,23,0,0,240,255,11,0,0,0,0,240,255,11,0,
+ 0,0,0,240,255,11,0,0,0,0,240,255,11,0,0,0,0,240,255,11,0,0,0,0,
+ 240,255,11,0,0,0,0,240,255,11,0,0,0,0,240,255,11,0,0,0,0,240,255,11,
+ 0,0,0,0,240,255,11,0,0,0,0,160,255,6,0,0,0,0,0,35,0,0,0,212,
+ 126,0,0,0,250,28,250,239,0,0,48,255,127,251,255,0,0,64,255,143,251,255,0,0,
+ 64,255,143,251,255,0,0,64,255,143,251,255,0,0,64,255,143,251,255,0,0,64,255,143,
+ 251,255,0,0,64,255,143,251,255,0,0,64,255,127,251,255,1,0,80,255,127,249,255,5,
+ 0,144,255,95,245,255,94,17,247,255,31,208,255,255,255,255,255,10,48,254,255,255,255,207,
+ 1,0,146,254,255,239,24,0,0,0,32,52,2,0,0,211,126,0,0,0,0,251,11,249,
+ 255,3,0,0,112,255,63,246,255,9,0,0,208,255,30,225,255,31,0,0,245,255,8,128,
+ 255,127,0,0,251,255,2,16,254,223,0,48,255,175,0,0,249,255,5,144,255,63,0,0,
+ 242,255,12,225,255,11,0,0,160,255,63,247,255,4,0,0,48,255,175,253,207,0,0,0,
+ 0,252,255,255,95,0,0,0,0,245,255,255,13,0,0,0,0,208,255,255,7,0,0,0,
+ 0,96,255,239,1,0,0,0,0,0,251,111,0,0,0,0,0,0,16,1,0,0,0,160,
+ 223,2,0,0,228,78,0,0,48,237,38,255,159,0,0,208,255,12,0,0,250,223,224,255,
+ 14,0,64,255,255,2,0,241,255,9,248,255,4,0,249,255,127,0,80,255,79,48,255,159,
+ 0,224,255,255,12,0,251,223,0,208,255,14,80,255,255,255,2,241,255,8,0,247,255,4,
+ 250,191,254,127,96,255,63,0,32,255,159,241,255,149,255,12,251,207,0,0,176,255,110,255,
+ 30,244,255,244,255,7,0,0,246,255,254,159,0,253,223,255,47,0,0,16,255,255,255,4,
+ 128,255,255,191,0,0,0,160,255,255,13,0,243,255,255,5,0,0,0,245,255,143,0,0,
+ 252,255,30,0,0,0,0,254,255,2,0,112,255,175,0,0,0,0,96,255,8,0,0,193,
+ 239,2,0,0,0,0,16,1,0,0,0,32,0,0,0,112,222,3,0,0,231,61,224,255,
+ 29,0,80,255,159,160,255,191,0,226,255,79,16,253,255,7,252,255,8,0,227,255,191,255,
+ 207,0,0,96,255,255,255,46,0,0,0,249,255,255,4,0,0,0,242,255,191,0,0,0,
+ 0,251,255,255,5,0,0,112,255,255,255,46,0,0,244,255,158,255,223,1,32,254,255,5,
+ 251,255,10,176,255,159,0,209,255,111,241,255,29,0,64,255,191,144,207,2,0,0,230,78,
+ 211,142,0,0,48,253,153,255,95,0,16,253,255,245,255,30,0,249,255,10,250,255,10,244,
+ 255,29,16,254,255,214,255,79,0,64,255,255,255,159,0,0,144,255,255,223,1,0,0,209,
+ 255,255,3,0,0,0,245,255,9,0,0,0,64,255,143,0,0,0,0,244,255,8,0,0,
+ 0,64,255,143,0,0,0,0,244,255,8,0,0,0,48,255,127,0,0,0,0,192,239,2,
+ 0,0,0,0,32,0,0,0,128,187,187,187,187,139,49,255,255,255,255,255,127,242,255,255,
+ 255,255,255,6,116,136,136,250,255,30,0,0,0,209,255,79,0,0,0,176,255,111,0,0,
+ 0,128,255,159,0,0,0,96,255,207,0,0,0,48,255,239,2,0,0,16,253,255,3,0,
+ 0,0,251,255,6,0,0,0,248,255,76,68,68,3,242,255,255,255,255,255,75,255,255,255,
+ 255,255,255,160,255,255,255,255,255,8,163,187,155,177,255,255,95,251,255,223,178,255,15,0,
+ 251,255,0,176,255,15,0,251,255,0,176,255,15,0,251,255,0,176,255,15,0,251,255,0,
+ 176,255,15,0,251,255,0,176,255,15,0,251,255,0,176,255,15,0,251,255,223,162,255,255,
+ 95,163,187,155,1,144,76,0,0,16,255,12,0,0,208,255,2,0,0,248,127,0,0,48,
+ 255,12,0,0,208,255,2,0,0,248,111,0,0,64,255,11,0,0,224,255,1,0,0,249,
+ 111,0,0,64,255,11,0,0,224,255,1,0,0,250,95,0,0,80,255,10,0,0,225,239,
+ 0,0,0,250,95,0,0,80,255,6,0,0,80,6,180,187,139,208,255,255,63,231,255,255,
+ 4,128,255,79,0,248,255,4,128,255,79,0,248,255,4,128,255,79,0,248,255,4,128,255,
+ 79,0,248,255,4,128,255,79,0,248,255,4,128,255,79,0,248,255,4,128,255,79,247,255,
+ 255,212,255,255,63,180,187,123,0,0,0,83,1,0,0,0,244,223,0,0,0,176,255,95,
+ 0,0,32,255,255,11,0,0,248,175,255,2,0,208,191,242,143,0,80,255,6,252,13,0,
+ 251,30,96,255,5,242,159,0,225,175,112,255,3,0,249,31,248,12,0,48,255,2,19,0,
+ 0,32,2,248,255,255,255,239,130,255,255,255,255,46,81,2,176,207,0,251,95,32,254,13,
+ 80,255,3,96,8,0,48,118,87,2,0,211,255,255,255,8,128,255,255,255,255,6,195,106,
+ 84,253,207,0,0,0,128,255,14,129,236,255,255,255,208,255,207,219,255,63,255,111,0,249,
+ 255,243,255,74,247,255,15,252,255,255,254,255,16,251,255,75,255,10,0,65,3,32,3,231,
+ 77,0,0,0,0,254,191,0,0,0,0,255,191,0,0,0,0,255,191,0,0,0,0,255,
+ 191,64,103,2,0,255,207,251,255,143,0,255,255,255,255,255,6,255,255,74,248,255,13,255,
+ 239,0,176,255,47,255,191,0,128,255,79,255,191,0,128,255,63,255,239,1,192,255,31,255,
+ 255,141,251,255,12,255,239,255,255,255,4,250,111,248,255,93,0,48,2,16,51,0,0,0,
+ 16,116,87,1,0,230,255,255,111,112,255,255,255,175,225,255,143,100,58,245,255,8,0,0,
+ 247,255,4,0,0,247,255,5,0,0,244,255,10,0,0,208,255,191,152,109,64,254,255,255,
+ 191,0,179,255,255,77,0,0,49,36,0,0,0,0,0,231,77,0,0,0,0,254,191,0,
+ 0,0,0,255,191,0,0,0,0,255,191,0,64,119,3,255,191,16,251,255,143,255,191,160,
+ 255,255,255,255,191,242,255,110,197,255,191,246,255,7,48,255,191,247,255,4,0,255,191,247,
+ 255,5,16,255,191,245,255,9,80,255,191,225,255,159,232,255,191,112,255,255,239,255,191,0,
+ 231,255,78,250,111,0,16,51,0,48,2,0,16,117,71,0,0,96,254,255,223,3,96,255,
+ 255,255,239,18,254,191,18,249,159,245,255,2,16,255,125,255,255,255,255,255,247,255,188,187,
+ 187,73,255,111,0,0,0,208,255,175,136,252,7,227,255,255,255,175,0,162,254,255,191,2,
+ 0,0,67,19,0,0,0,113,186,9,0,80,254,255,63,0,243,255,239,11,0,248,255,6,
+ 0,48,252,255,52,0,249,255,255,255,7,250,255,255,255,8,114,253,255,120,1,0,251,255,
+ 0,0,0,251,255,0,0,0,251,255,0,0,0,251,255,0,0,0,251,255,0,0,0,251,
+ 255,0,0,0,246,175,0,0,0,32,3,0,0,0,48,119,4,96,39,16,250,255,191,248,
+ 207,160,255,255,255,255,255,242,255,110,164,255,255,246,255,7,0,253,255,247,255,4,0,251,
+ 255,247,255,6,0,252,255,243,255,61,112,255,255,192,255,255,255,255,255,32,253,255,239,252,
+ 255,0,113,186,23,250,239,32,22,0,48,254,191,192,255,189,252,255,111,176,255,255,255,255,
+ 9,16,183,254,239,90,0,231,78,0,0,0,224,255,11,0,0,0,255,191,0,0,0,240,
+ 255,11,0,0,0,255,191,129,187,23,240,255,237,255,255,11,255,255,255,255,255,243,255,95,
+ 128,255,111,255,207,0,244,255,247,255,11,64,255,143,255,191,0,244,255,248,255,11,64,255,
+ 143,255,191,0,244,255,232,255,11,48,255,127,231,78,0,160,207,2,97,55,192,255,47,254,
+ 255,115,254,10,0,0,0,118,1,248,207,176,255,15,251,255,176,255,15,251,255,176,255,15,
+ 251,255,176,255,15,251,255,96,255,9,32,3,0,0,16,118,3,0,192,255,46,0,224,255,
+ 63,0,112,254,10,0,0,0,0,0,16,118,2,0,144,255,12,0,176,255,15,0,176,255,
+ 15,0,176,255,15,0,176,255,15,0,176,255,15,0,176,255,15,0,176,255,15,0,176,255,
+ 15,0,176,255,15,0,208,255,14,128,253,255,11,242,255,255,3,160,239,59,0,231,78,0,
+ 0,0,224,255,11,0,0,0,255,191,0,0,0,240,255,11,0,0,0,255,191,0,128,123,
+ 240,255,11,160,255,31,255,191,112,255,207,240,255,91,255,239,2,255,223,254,255,4,240,255,
+ 255,255,11,0,255,239,255,255,6,240,255,107,255,255,4,255,191,144,255,239,225,255,11,176,
+ 255,127,231,78,0,161,223,2,231,77,0,224,255,11,0,255,191,0,240,255,11,0,255,191,
+ 0,240,255,11,0,255,191,0,240,255,11,0,255,191,0,240,255,11,0,255,191,0,240,255,
+ 13,0,252,255,205,113,255,255,79,144,255,239,1,16,51,1,32,3,0,51,0,0,50,2,
+ 96,255,89,254,239,36,251,255,27,251,239,255,255,239,253,255,255,184,255,223,216,255,255,154,
+ 255,223,251,255,3,245,255,10,192,255,191,255,15,64,255,143,0,251,255,251,255,0,244,255,
+ 8,176,255,191,255,15,64,255,143,0,251,255,251,255,0,244,255,8,176,255,191,255,15,64,
+ 255,127,0,251,255,246,175,0,209,255,3,112,255,10,50,0,0,49,1,0,32,3,48,2,
+ 16,51,0,160,255,117,254,239,5,255,239,255,255,239,241,255,207,216,255,95,255,239,1,245,
+ 255,247,255,11,64,255,143,255,191,0,244,255,248,255,11,64,255,143,255,191,0,244,255,248,
+ 255,11,64,255,127,250,127,0,209,255,3,35,0,0,49,1,0,0,116,103,2,0,0,230,
+ 255,255,191,1,96,255,255,255,255,29,225,255,110,180,255,143,245,255,7,16,254,223,247,255,
+ 4,0,252,255,247,255,5,0,252,239,244,255,9,32,255,207,208,255,175,216,255,111,64,254,
+ 255,255,255,11,0,179,255,255,126,0,0,0,49,36,0,0,114,6,64,119,2,0,252,143,
+ 251,255,143,0,255,255,255,255,255,6,255,255,74,248,255,13,255,239,0,176,255,47,255,191,
+ 0,128,255,79,255,191,0,128,255,63,255,239,1,192,255,31,255,255,141,251,255,12,255,255,
+ 255,255,255,4,255,207,248,255,93,0,255,191,16,51,0,0,255,191,0,0,0,0,254,191,
+ 0,0,0,0,231,78,0,0,0,0,0,64,119,3,114,22,16,251,255,143,252,159,160,255,
+ 255,255,255,191,242,255,110,197,255,191,246,255,7,48,255,191,247,255,4,0,255,191,247,255,
+ 5,16,255,191,245,255,9,80,255,191,225,255,159,232,255,191,112,255,255,255,255,191,0,231,
+ 255,94,255,191,0,16,51,0,255,191,0,0,0,0,255,191,0,0,0,0,254,191,0,0,
+ 0,0,231,78,114,6,80,55,252,127,251,207,255,239,255,207,255,255,223,75,255,255,4,0,
+ 255,207,0,0,255,191,0,0,255,191,0,0,255,191,0,0,255,191,0,0,250,111,0,0,
+ 48,2,0,0,0,48,118,71,1,32,252,255,255,78,208,255,255,255,127,243,255,3,98,25,
+ 243,255,105,19,0,192,255,255,255,26,16,216,255,255,191,0,0,48,249,255,194,123,69,249,
+ 239,245,255,255,255,127,128,254,255,223,6,0,32,51,2,0,0,32,53,0,0,0,225,255,
+ 4,0,0,244,255,7,0,32,247,255,73,3,242,255,255,255,159,243,255,255,255,175,80,249,
+ 255,123,39,0,244,255,8,0,0,244,255,8,0,0,244,255,8,0,0,243,255,10,0,0,
+ 241,255,223,188,0,144,255,255,255,0,0,233,255,175,0,0,16,67,2,114,22,0,48,87,
+ 192,255,9,16,255,95,255,191,0,244,255,248,255,11,64,255,143,255,191,0,244,255,248,255,
+ 11,64,255,143,255,191,0,244,255,232,255,12,112,255,143,252,255,153,255,255,120,255,255,255,
+ 255,127,144,255,223,196,255,3,16,51,0,48,2,64,21,0,0,82,114,255,11,0,226,239,
+ 248,255,3,128,255,62,255,143,0,254,159,176,255,14,245,255,2,245,255,181,255,11,0,253,
+ 223,255,79,0,112,255,255,207,0,0,225,255,255,5,0,0,248,255,13,0,0,16,253,95,
+ 0,0,0,16,35,0,0,64,21,0,0,83,0,0,48,21,246,175,0,48,255,9,0,244,
+ 175,247,255,2,144,255,30,0,250,191,242,255,8,225,255,111,16,255,95,176,255,13,245,255,
+ 207,112,255,14,64,255,79,251,255,255,194,255,8,0,253,207,255,205,255,251,255,2,0,247,
+ 255,255,88,255,255,175,0,0,241,255,255,2,254,255,79,0,0,144,255,191,0,248,255,13,
+ 0,0,32,254,62,0,209,255,5,0,0,0,49,1,0,16,19,0,0,113,6,0,96,23,
+ 250,191,0,251,175,249,255,136,255,159,209,255,255,255,29,32,254,255,239,2,0,245,255,95,
+ 0,0,251,255,191,0,128,255,255,255,8,245,255,204,255,95,254,239,34,254,239,252,78,0,
+ 244,207,49,1,0,16,19,64,21,0,0,82,2,246,207,0,16,254,30,247,255,4,112,255,
+ 31,242,255,10,208,255,11,160,255,31,244,255,4,48,255,127,250,207,0,0,252,223,255,111,
+ 0,0,245,255,255,13,0,0,208,255,255,7,0,0,112,255,239,1,0,0,64,255,159,0,
+ 0,0,144,255,47,0,0,0,242,255,10,0,0,0,245,255,3,0,0,0,193,143,0,0,
+ 0,32,68,68,68,2,242,255,255,255,95,243,255,255,255,143,80,119,215,255,46,0,0,246,
+ 255,4,0,80,255,95,0,0,227,255,6,0,32,254,143,0,0,208,255,191,187,107,243,255,
+ 255,255,255,192,255,255,255,175,0,0,166,187,7,0,128,255,255,31,0,224,255,255,11,0,
+ 240,255,13,0,0,240,255,11,0,0,240,255,11,0,0,240,255,11,0,0,244,255,10,0,
+ 226,255,239,3,0,245,255,143,0,0,161,253,255,5,0,0,242,255,11,0,0,240,255,11,
+ 0,0,240,255,11,0,0,240,255,11,0,0,240,255,13,0,0,208,255,255,12,0,112,255,
+ 255,31,0,0,166,187,7,194,175,112,255,63,248,255,132,255,79,248,255,132,255,79,248,255,
+ 132,255,79,248,255,132,255,79,248,255,132,255,79,248,255,132,255,79,248,255,132,255,79,248,
+ 255,116,255,63,194,175,0,180,187,24,0,208,255,255,12,0,248,255,255,3,0,144,255,79,
+ 0,0,248,255,4,0,128,255,79,0,0,247,255,4,0,96,255,143,0,0,209,255,255,5,
+ 0,244,255,159,0,226,255,190,3,112,255,111,0,0,248,255,4,0,128,255,79,0,0,248,
+ 255,4,0,144,255,79,0,248,255,255,2,208,255,255,11,0,180,187,23,0,0,112,238,58,
+ 0,100,96,255,255,191,248,63,248,74,249,255,207,0,4,0,146,139,1,0,0,181,254,206,
+ 23,0,0,0,194,158,69,133,238,5,0,32,206,2,0,0,145,95,0,192,28,32,184,155,
+ 3,233,2,246,3,245,255,255,14,225,9,203,16,254,191,184,11,144,14,158,96,255,11,0,
+ 0,80,63,143,112,255,8,0,0,64,79,142,96,255,10,0,0,80,63,188,32,255,143,116,
+ 8,128,31,247,2,248,255,255,63,208,11,225,10,80,236,223,8,247,4,80,159,0,0,0,
+ 80,143,0,0,229,92,2,65,250,8,0,0,32,233,255,255,75,0,0,0,0,0,67,19,
+ 0,0,0,0,0,0,48,4,0,0,0,48,255,6,0,0,0,250,111,0,0,0,243,191,
+ 0,0,0,128,223,1,0,0,0,146,3,0,0,16,117,71,0,0,96,254,255,223,3,96,
+ 255,255,255,239,18,254,191,18,249,159,245,255,2,16,255,125,255,255,255,255,255,247,255,188,
+ 187,187,73,255,111,0,0,0,208,255,175,136,252,7,227,255,255,255,175,0,162,254,255,191,
+ 2,0,0,67,19,0,0,0,0,32,5,0,0,0,0,225,159,0,0,0,0,247,159,0,
+ 0,0,16,254,29,0,0,0,80,255,3,0,0,0,16,89,0,0,0,0,116,103,2,0,
+ 0,230,255,255,191,1,96,255,255,255,255,29,225,255,110,180,255,143,245,255,7,16,254,223,
+ 247,255,4,0,252,255,247,255,5,0,252,239,244,255,9,32,255,207,208,255,175,216,255,111,
+ 64,254,255,255,255,11,0,179,255,255,126,0,0,0,49,36,0,0,0,0,0,0,129,2,
+ 0,16,116,103,250,7,0,230,255,255,255,3,96,255,255,255,255,29,225,255,44,247,255,143,
+ 245,255,19,254,253,223,247,255,144,175,248,255,247,255,244,46,248,239,244,255,254,8,252,207,
+ 208,255,239,165,255,111,48,254,255,255,255,11,0,249,255,255,126,0,0,252,51,36,0,0,
+ 0,50,0,0,0,0,
+};
+static const np_glyph_t np_font_title_glyphs[99] = {
+ {0, 0, 0, 0, 6, 0},
+ {5, 15, 0, -15, 6, 0},
+ {9, 7, 1, -15, 11, 38},
+ {11, 16, 1, -15, 13, 70},
+ {13, 21, 0, -18, 13, 158},
+ {20, 16, 0, -15, 20, 295},
+ {14, 16, 1, -15, 16, 455},
+ {4, 7, 1, -15, 6, 567},
+ {6, 19, 2, -15, 8, 581},
+ {6, 19, 1, -15, 8, 638},
+ {9, 8, 0, -15, 10, 695},
+ {11, 12, 1, -12, 13, 731},
+ {5, 7, 0, -4, 6, 797},
+ {8, 4, 1, -8, 9, 815},
+ {5, 4, 0, -4, 6, 831},
+ {9, 18, -1, -16, 7, 841},
+ {12, 15, 0, -15, 12, 922},
+ {12, 15, 1, -15, 12, 1012},
+ {11, 15, 1, -15, 12, 1102},
+ {12, 16, 0, -15, 12, 1185},
+ {13, 15, 0, -15, 12, 1281},
+ {12, 16, 1, -15, 12, 1379},
+ {12, 16, 0, -15, 12, 1475},
+ {13, 16, 0, -15, 12, 1571},
+ {12, 15, 0, -15, 12, 1675},
+ {12, 16, 0, -15, 12, 1765},
+ {5, 11, 0, -11, 6, 1861},
+ {5, 14, 0, -11, 6, 1889},
+ {11, 10, 1, -11, 13, 1924},
+ {11, 8, 1, -10, 13, 1979},
+ {12, 10, 0, -11, 12, 2023},
+ {10, 16, 0, -15, 10, 2083},
+ {19, 19, 1, -15, 20, 2163},
+ {16, 16, 0, -15, 16, 2344},
+ {13, 15, 1, -15, 14, 2472},
+ {13, 15, 1, -15, 14, 2570},
+ {15, 15, 1, -15, 16, 2668},
+ {11, 15, 1, -15, 13, 2781},
+ {11, 16, 1, -15, 12, 2864},
+ {14, 16, 1, -15, 16, 2952},
+ {14, 15, 1, -15, 16, 3064},
+ {4, 15, 1, -15, 6, 3169},
+ {8, 15, -1, -15, 8, 3199},
+ {13, 15, 1, -15, 15, 3259},
+ {11, 15, 1, -15, 12, 3357},
+ {17, 15, 1, -15, 19, 3440},
+ {14, 15, 1, -15, 16, 3568},
+ {15, 16, 1, -15, 17, 3673},
+ {13, 16, 1, -15, 14, 3793},
+ {15, 20, 1, -15, 17, 3897},
+ {13, 16, 1, -15, 15, 4047},
+ {13, 16, 1, -15, 14, 4151},
+ {14, 16, 0, -15, 14, 4255},
+ {14, 16, 1, -15, 16, 4367},
+ {16, 16, 0, -15, 16, 4479},
+ {23, 16, 0, -15, 24, 4607},
+ {14, 15, 0, -15, 15, 4791},
+ {13, 16, 0, -15, 14, 4896},
+ {13, 15, 0, -15, 13, 5000},
+ {7, 19, 2, -15, 8, 5098},
+ {9, 18, -1, -16, 7, 5165},
+ {7, 19, 0, -15, 8, 5246},
+ {11, 12, 1, -14, 12, 5313},
+ {11, 2, 0, 0, 10, 5379},
+ {5, 6, 2, -17, 8, 5390},
+ {11, 12, 0, -11, 12, 5405},
+ {12, 16, 1, -15, 13, 5471},
+ {10, 12, 0, -11, 10, 5567},
+ {12, 16, 0, -15, 13, 5627},
+ {11, 12, 0, -11, 12, 5723},
+ {10, 16, 0, -15, 9, 5789},
+ {12, 15, 0, -11, 13, 5869},
+ {11, 15, 1, -15, 12, 5959},
+ {5, 17, 1, -16, 6, 6042},
+ {8, 20, -2, -16, 6, 6085},
+ {11, 15, 1, -15, 12, 6165},
+ {7, 16, 1, -15, 8, 6248},
+ {17, 12, 1, -11, 19, 6304},
+ {11, 12, 1, -11, 12, 6406},
+ {12, 12, 0, -11, 13, 6472},
+ {12, 15, 1, -11, 13, 6544},
+ {12, 15, 0, -11, 13, 6634},
+ {8, 12, 1, -11, 9, 6724},
+ {10, 12, 0, -11, 10, 6772},
+ {10, 15, -1, -14, 9, 6832},
+ {11, 12, 1, -11, 12, 6907},
+ {11, 12, 0, -11, 12, 6973},
+ {18, 12, 0, -11, 18, 7039},
+ {10, 12, 1, -11, 12, 7147},
+ {12, 15, 0, -11, 11, 7207},
+ {10, 11, 0, -11, 10, 7297},
+ {10, 19, 0, -15, 9, 7352},
+ {5, 19, 1, -15, 7, 7447},
+ {9, 19, 0, -15, 9, 7495},
+ {11, 4, 1, -8, 12, 7581},
+ {16, 16, 1, -15, 17, 7603},
+ {11, 18, 0, -17, 12, 7731},
+ {12, 18, 0, -17, 13, 7830},
+ {12, 14, 0, -12, 13, 7938},
+};
+const np_font_t np_font_title = {32, 99, 18, 5, np_font_title_glyphs, np_font_title_data};
+
+static const uint8_t np_font_body_data[2944] = {
+ 113,98,143,245,71,111,243,20,63,96,80,94,229,5,113,49,6,245,167,47,244,149,31,242,
+ 115,14,48,0,2,0,66,96,1,144,25,47,113,189,249,21,248,201,93,48,31,138,0,214,
+ 208,5,246,255,255,6,108,228,0,224,99,11,0,1,16,0,0,208,6,0,80,191,4,193,
+ 255,255,103,159,17,53,247,25,0,32,253,223,25,16,149,253,11,0,0,253,231,137,250,43,
+ 234,239,25,0,240,7,0,0,20,0,32,87,0,96,4,32,222,159,48,111,0,247,145,14,
+ 203,0,112,15,248,245,4,0,243,218,219,122,222,4,181,137,62,159,219,0,16,142,244,132,
+ 15,0,217,33,127,219,0,241,5,128,255,5,0,2,0,16,1,0,114,39,0,0,243,238,
+ 30,0,112,95,245,3,0,243,220,28,5,32,253,46,245,2,237,247,171,14,243,9,247,159,
+ 32,223,84,254,27,96,254,175,247,6,0,18,0,1,113,81,127,244,37,63,48,0,32,6,
+ 219,243,120,79,250,178,31,251,145,63,246,38,175,144,13,33,113,0,244,5,224,12,176,31,
+ 144,63,128,95,128,79,144,63,192,14,241,10,244,3,32,0,80,97,0,186,30,233,239,28,
+ 218,29,144,162,1,0,48,0,0,0,79,0,0,240,4,96,255,255,111,81,245,88,2,0,
+ 79,0,0,160,2,0,229,84,127,192,19,6,194,221,24,135,72,229,85,94,0,16,0,245,
+ 0,202,16,126,80,47,160,12,224,7,244,3,217,0,142,0,42,0,0,116,22,0,249,255,
+ 29,242,28,247,103,111,16,191,247,5,240,124,95,0,207,245,8,243,26,238,198,79,64,253,
+ 127,0,0,18,0,0,85,0,195,207,0,223,207,0,19,207,0,0,207,0,0,207,0,0,
+ 207,0,134,239,72,253,255,159,32,118,22,64,255,255,29,114,18,251,4,0,144,79,0,48,
+ 222,0,32,237,3,16,237,4,0,252,139,72,225,255,255,9,32,118,21,64,255,255,29,98,
+ 18,250,4,0,177,47,16,254,143,0,64,182,79,0,0,244,104,124,198,111,195,255,158,0,
+ 16,2,0,0,0,54,0,0,246,11,0,226,191,0,192,253,11,112,79,191,48,127,241,11,
+ 250,221,239,75,135,248,109,0,0,174,0,0,16,0,96,136,120,1,255,255,79,240,12,0,
+ 0,223,84,1,240,255,239,3,106,81,191,0,0,241,60,141,166,143,161,255,175,1,0,18,
+ 0,0,98,87,0,229,255,159,225,61,64,83,127,117,3,247,237,255,116,207,48,191,245,9,
+ 241,28,237,133,143,48,252,175,1,0,17,0,114,136,120,100,255,255,191,0,0,249,6,0,
+ 242,13,0,160,95,0,48,207,0,0,251,5,0,244,12,0,128,79,0,0,16,0,0,16,
+ 117,38,16,253,237,95,244,10,242,43,191,65,159,112,255,223,49,206,134,175,247,5,208,95,
+ 191,100,223,128,254,207,3,0,18,0,16,118,5,16,252,253,11,246,8,248,117,111,96,175,
+ 244,109,253,11,231,126,175,0,0,245,71,123,231,30,211,255,60,0,16,1,0,229,85,94,
+ 0,0,0,229,85,94,229,85,94,0,0,0,229,84,127,192,19,6,0,0,32,2,0,181,
+ 143,113,253,74,112,191,1,0,145,222,40,0,16,198,127,0,0,64,3,115,136,120,83,221,
+ 221,93,32,34,34,112,255,255,127,65,68,68,1,33,0,0,112,191,5,0,48,233,142,1,
+ 0,161,159,16,215,175,98,223,23,0,66,0,0,0,64,119,2,250,255,79,85,97,143,0,
+ 144,95,0,245,7,0,155,0,0,19,0,32,142,0,32,142,0,0,16,117,87,0,0,96,
+ 190,136,219,4,112,78,0,0,227,18,94,177,191,108,136,229,160,45,245,69,123,12,142,48,
+ 47,180,199,224,9,247,128,72,30,248,188,206,29,192,8,50,16,19,0,227,57,16,165,0,
+ 0,129,253,206,6,0,0,16,23,0,0,0,250,10,0,0,242,253,2,0,128,63,159,0,
+ 16,174,160,31,0,247,71,247,8,224,255,255,239,97,127,34,98,127,234,1,0,208,11,1,
+ 0,0,17,112,136,103,2,244,222,253,46,244,9,112,127,244,9,128,95,244,255,255,10,244,
+ 91,165,111,244,9,32,191,244,91,165,143,226,255,239,25,0,81,103,2,64,254,255,95,225,
+ 143,84,74,245,9,0,0,247,5,0,0,247,6,0,0,244,28,0,1,176,223,186,127,16,
+ 233,255,43,0,0,18,0,112,136,87,1,48,255,255,239,4,244,9,81,238,66,159,0,112,
+ 111,244,9,0,244,72,159,0,80,143,244,9,0,251,68,207,136,252,11,226,255,223,7,0,
+ 112,136,120,51,239,221,125,244,9,0,64,159,0,0,244,255,255,68,191,85,21,244,9,0,
+ 64,191,85,37,226,255,255,8,112,136,120,50,239,221,93,244,9,0,64,159,0,0,244,222,
+ 205,66,207,136,23,244,9,0,64,159,0,0,242,7,0,0,1,0,0,0,81,119,4,48,
+ 254,255,191,225,94,17,85,245,8,0,0,247,5,146,121,247,6,179,253,243,11,0,248,176,
+ 191,102,251,16,232,255,158,0,0,18,0,112,2,0,114,65,143,0,112,95,244,9,0,248,
+ 69,159,0,128,95,244,255,255,255,69,207,136,184,95,244,9,0,248,69,159,0,128,95,242,
+ 7,0,246,3,1,0,0,1,112,66,143,244,73,159,244,73,159,244,73,159,242,7,1,0,
+ 112,2,64,143,0,244,9,64,159,0,244,9,64,159,0,244,9,181,127,243,191,1,17,0,
+ 112,2,0,54,244,8,160,127,244,9,250,9,244,153,159,0,244,254,11,0,244,235,95,0,
+ 244,57,254,4,244,9,227,78,242,7,64,174,16,0,0,1,112,2,0,64,143,0,0,244,
+ 9,0,64,159,0,0,244,9,0,64,159,0,0,244,9,0,64,207,136,55,226,255,255,8,
+ 112,2,0,0,39,244,12,0,112,143,244,95,0,225,159,244,223,1,248,159,244,251,39,191,
+ 159,244,153,190,77,159,244,25,254,69,159,244,9,166,64,159,243,7,0,48,127,16,0,0,
+ 0,1,112,1,0,99,64,207,0,176,31,244,159,0,251,65,223,111,176,31,244,169,63,251,
+ 65,159,209,221,31,244,9,227,255,65,159,0,246,31,243,7,0,234,1,1,0,0,1,0,
+ 81,103,2,0,227,255,255,7,225,94,32,252,84,143,0,48,175,247,5,0,240,124,111,0,
+ 16,207,243,11,0,246,8,250,106,248,30,16,233,255,43,0,0,32,1,0,112,136,103,1,
+ 244,222,253,45,244,9,112,127,244,9,64,143,244,91,198,95,244,255,223,7,244,9,0,0,
+ 244,9,0,0,242,7,0,0,16,0,0,0,0,81,103,2,0,227,255,255,7,225,94,32,
+ 252,84,143,0,48,175,247,5,0,240,124,111,0,16,207,243,11,0,246,8,250,106,248,30,
+ 16,233,255,62,0,0,32,247,10,0,0,0,251,3,0,0,16,6,112,136,103,1,244,222,
+ 253,46,244,9,112,127,244,9,80,143,244,156,233,63,244,189,255,5,244,9,245,10,244,9,
+ 192,79,242,7,48,143,16,0,0,1,0,115,87,1,128,255,255,13,242,44,64,6,243,44,
+ 0,0,192,255,157,3,0,149,253,46,0,0,144,79,229,105,215,47,162,254,223,4,0,32,
+ 1,0,134,136,136,72,253,255,255,159,0,241,11,0,0,241,11,0,0,241,11,0,0,241,
+ 11,0,0,241,11,0,0,241,11,0,0,224,10,0,0,16,0,0,112,2,0,99,64,143,
+ 0,176,31,244,9,0,251,65,159,0,176,31,244,9,0,251,65,159,0,176,31,242,11,0,
+ 237,0,252,105,251,10,32,251,255,26,0,0,33,1,0,84,0,0,96,178,63,0,80,127,
+ 245,9,0,252,2,237,1,244,9,112,127,160,63,0,225,45,191,0,0,248,252,4,0,32,
+ 255,12,0,0,144,95,0,0,0,16,0,0,114,1,0,54,0,48,118,111,0,243,11,0,
+ 221,242,11,144,255,2,243,9,252,2,222,127,128,63,96,111,244,213,12,221,0,241,172,30,
+ 248,245,7,0,250,159,48,239,47,0,80,255,4,192,207,0,0,209,13,0,246,6,0,0,
+ 17,0,0,1,0,114,1,16,39,246,9,144,111,176,95,245,11,16,238,238,2,0,245,95,
+ 0,0,249,159,0,96,191,251,6,227,30,225,62,248,4,64,143,16,0,0,1,84,0,32,
+ 23,251,4,192,47,243,29,248,8,112,207,207,0,0,251,46,0,0,244,9,0,0,244,9,
+ 0,0,244,9,0,0,242,7,0,0,16,0,0,114,136,136,5,246,255,255,14,0,0,248,
+ 7,0,80,175,0,0,226,29,0,0,252,3,0,128,111,0,0,244,142,136,23,245,255,255,
+ 47,132,39,251,93,251,1,251,1,251,1,251,1,251,1,251,1,251,1,251,2,251,111,65,
+ 20,16,0,16,95,0,192,10,0,231,0,32,79,0,208,9,0,232,0,48,79,0,208,9,
+ 0,216,0,48,10,133,23,234,95,128,95,128,95,128,95,128,95,128,95,128,95,128,95,129,
+ 95,252,79,66,3,0,113,2,0,112,175,0,0,173,31,0,244,193,7,160,10,215,32,79,
+ 16,78,97,0,80,2,249,255,223,34,160,12,226,4,36,144,254,77,16,122,230,14,48,118,
+ 253,81,207,216,31,247,89,254,17,252,189,14,0,1,16,0,112,2,0,64,143,0,0,244,
+ 9,0,64,191,252,43,244,127,249,74,175,0,237,244,9,208,79,239,117,191,243,234,223,2,
+ 1,32,0,80,252,93,242,109,88,247,6,0,247,6,0,243,77,70,96,254,111,0,32,0,
+ 0,0,96,3,0,16,191,0,0,241,11,230,158,191,243,109,251,123,111,32,191,247,6,242,
+ 75,207,164,191,128,255,233,10,16,1,1,64,252,60,32,223,182,13,247,139,250,115,191,136,
+ 23,242,77,132,2,213,255,44,0,16,1,0,0,81,5,227,191,160,63,192,255,143,211,111,
+ 2,251,1,176,31,0,251,1,160,14,0,16,0,96,238,216,58,223,166,207,247,6,241,124,
+ 111,32,207,242,142,252,12,196,108,207,112,35,247,25,253,255,44,0,66,2,0,112,2,0,
+ 64,143,0,0,244,9,0,64,191,252,28,244,110,250,71,159,64,159,244,9,244,73,159,64,
+ 159,243,7,242,7,1,0,1,179,83,94,0,48,94,245,88,143,245,88,143,243,6,1,48,
+ 59,80,94,0,0,48,94,80,143,80,143,80,143,80,143,80,127,128,127,250,29,49,0,112,
+ 2,0,64,143,0,0,244,9,0,64,159,128,30,244,121,175,64,223,207,0,244,253,29,64,
+ 159,247,11,243,7,248,4,1,0,1,112,2,244,8,244,9,244,9,244,9,244,9,244,9,
+ 243,60,160,239,0,17,226,200,175,230,110,64,239,198,207,230,14,244,9,248,5,251,65,159,
+ 128,95,176,31,244,9,248,5,251,49,127,96,63,160,14,16,0,16,0,16,0,226,200,207,
+ 65,239,166,127,244,9,244,73,159,64,159,244,9,244,57,127,32,127,16,0,16,0,64,252,
+ 125,32,223,182,111,247,6,242,122,111,32,191,243,76,250,6,230,255,9,0,32,1,0,226,
+ 200,191,66,255,151,175,244,10,208,78,159,0,253,244,94,247,75,191,254,45,244,9,2,48,
+ 143,0,0,48,1,0,0,96,238,216,57,223,182,191,247,6,242,123,111,32,191,244,76,250,
+ 11,248,175,191,0,17,241,11,0,16,175,0,0,32,1,226,199,78,255,123,244,10,64,159,
+ 0,244,9,48,127,0,16,0,0,161,238,27,246,103,42,245,105,2,64,184,79,133,84,127,
+ 229,255,27,0,17,0,112,9,0,252,0,254,255,74,253,53,192,15,0,252,0,176,111,4,
+ 227,239,0,32,0,226,6,227,69,159,80,143,244,9,245,72,159,80,143,242,76,251,8,250,
+ 175,111,0,17,16,0,217,0,229,130,95,192,14,242,59,143,0,250,251,1,48,255,9,0,
+ 176,63,0,0,16,0,0,216,0,215,0,215,247,5,253,5,205,241,74,239,74,111,160,191,
+ 140,175,30,64,255,39,255,9,0,236,1,251,3,0,16,0,16,0,226,6,218,192,159,143,
+ 32,254,11,32,254,11,209,158,127,246,5,235,16,0,16,217,1,229,114,111,176,30,241,44,
+ 143,0,249,251,2,48,255,10,0,192,63,0,16,206,0,0,245,5,0,0,3,0,0,246,
+ 255,111,81,229,63,0,248,6,80,159,0,227,110,53,246,255,175,0,116,6,242,191,64,159,
+ 0,244,9,64,143,96,237,3,196,79,0,244,9,64,159,0,244,26,16,254,14,16,36,69,
+ 207,207,207,207,207,207,207,207,207,206,18,117,5,160,254,3,128,95,0,248,5,112,95,0,
+ 226,110,48,207,4,247,5,128,95,16,249,5,252,46,32,20,0,48,55,32,33,207,207,94,
+ 48,32,87,0,0,64,103,2,0,16,139,84,153,0,176,67,186,101,8,132,243,139,9,27,
+ 71,247,1,0,56,87,246,2,0,56,147,209,206,12,12,144,21,118,146,6,0,184,135,107,
+ 0,0,16,51,0,0,0,0,4,0,0,243,4,0,176,10,0,0,21,0,64,252,60,32,
+ 223,182,13,247,139,250,115,191,136,23,242,77,132,2,213,255,44,0,16,1,0,0,0,19,
+ 0,0,242,5,0,160,11,0,0,21,0,64,252,125,32,223,182,111,247,6,242,122,111,32,
+ 191,243,76,250,6,230,255,9,0,32,1,0,0,0,97,0,196,255,13,242,141,255,119,111,
+ 139,207,247,218,241,60,255,152,127,160,255,159,0,39,18,0,
+};
+static const np_glyph_t np_font_body_glyphs[99] = {
+ {0, 0, 0, 0, 4, 0},
+ {3, 9, 0, -9, 3, 0},
+ {6, 5, 0, -9, 6, 14},
+ {7, 10, 0, -9, 8, 29},
+ {7, 12, 0, -10, 8, 64},
+ {11, 10, 0, -9, 12, 106},
+ {9, 10, 0, -9, 9, 161},
+ {3, 5, 0, -9, 3, 206},
+ {3, 12, 1, -9, 5, 214},
+ {4, 12, 0, -9, 5, 232},
+ {5, 5, 0, -9, 6, 256},
+ {7, 7, 0, -7, 7, 269},
+ {3, 4, 0, -2, 3, 294},
+ {5, 2, 0, -4, 6, 300},
+ {3, 2, 0, -2, 3, 305},
+ {4, 11, 0, -10, 4, 308},
+ {7, 10, 0, -9, 8, 330},
+ {6, 9, 1, -9, 8, 365},
+ {7, 9, 0, -9, 8, 392},
+ {7, 10, 0, -9, 8, 424},
+ {7, 10, 0, -9, 8, 459},
+ {7, 10, 0, -9, 8, 494},
+ {7, 10, 0, -9, 8, 529},
+ {7, 10, 0, -9, 8, 564},
+ {7, 10, 0, -9, 8, 599},
+ {7, 10, 0, -9, 8, 634},
+ {3, 6, 0, -6, 3, 669},
+ {3, 8, 0, -6, 3, 678},
+ {7, 7, 0, -7, 7, 690},
+ {7, 5, 0, -6, 7, 715},
+ {7, 7, 0, -7, 8, 733},
+ {6, 9, 0, -9, 6, 758},
+ {11, 11, 0, -9, 12, 785},
+ {9, 10, 0, -9, 9, 846},
+ {8, 9, 0, -9, 8, 891},
+ {8, 10, 0, -9, 8, 927},
+ {9, 9, 0, -9, 10, 967},
+ {7, 9, 0, -9, 8, 1008},
+ {7, 10, 0, -9, 7, 1040},
+ {8, 10, 0, -9, 9, 1075},
+ {9, 10, 0, -9, 10, 1115},
+ {3, 10, 0, -9, 4, 1160},
+ {5, 10, -1, -9, 5, 1175},
+ {8, 10, 0, -9, 8, 1200},
+ {7, 9, 0, -9, 7, 1240},
+ {10, 10, 0, -9, 11, 1272},
+ {9, 10, 0, -9, 9, 1322},
+ {9, 10, 0, -9, 10, 1367},
+ {8, 10, 0, -9, 8, 1412},
+ {9, 12, 0, -9, 10, 1452},
+ {8, 10, 0, -9, 8, 1506},
+ {8, 10, 0, -9, 8, 1546},
+ {8, 10, 0, -9, 8, 1586},
+ {9, 10, 0, -9, 9, 1626},
+ {9, 10, 0, -9, 9, 1671},
+ {13, 10, 0, -9, 14, 1716},
+ {8, 10, 0, -9, 8, 1781},
+ {8, 10, 0, -9, 8, 1821},
+ {8, 9, 0, -9, 8, 1861},
+ {4, 12, 1, -9, 5, 1897},
+ {5, 11, -1, -10, 4, 1921},
+ {4, 12, 0, -9, 5, 1949},
+ {7, 7, 0, -8, 8, 1973},
+ {6, 1, 0, 0, 6, 1998},
+ {3, 4, 1, -10, 5, 2001},
+ {7, 7, 0, -6, 7, 2007},
+ {7, 10, 0, -9, 8, 2032},
+ {6, 7, 0, -6, 6, 2067},
+ {7, 10, 0, -9, 8, 2088},
+ {7, 7, 0, -6, 7, 2123},
+ {5, 10, 0, -9, 5, 2148},
+ {7, 9, 0, -6, 8, 2173},
+ {7, 10, 0, -9, 8, 2205},
+ {3, 10, 0, -9, 3, 2240},
+ {4, 12, -1, -9, 3, 2255},
+ {7, 10, 0, -9, 7, 2279},
+ {4, 10, 0, -9, 4, 2314},
+ {11, 7, 0, -6, 11, 2334},
+ {7, 7, 0, -6, 8, 2373},
+ {7, 7, 0, -6, 8, 2398},
+ {7, 9, 0, -6, 8, 2423},
+ {7, 9, 0, -6, 8, 2455},
+ {5, 7, 0, -6, 6, 2487},
+ {6, 7, 0, -6, 6, 2505},
+ {5, 9, 0, -8, 5, 2526},
+ {7, 7, 0, -6, 8, 2549},
+ {7, 7, 0, -6, 7, 2574},
+ {10, 7, 0, -6, 10, 2599},
+ {6, 7, 0, -6, 7, 2634},
+ {7, 9, 0, -6, 7, 2655},
+ {6, 6, 0, -6, 6, 2687},
+ {5, 12, 0, -9, 6, 2705},
+ {2, 12, 1, -9, 4, 2735},
+ {5, 12, 0, -9, 5, 2747},
+ {7, 3, 0, -5, 8, 2777},
+ {10, 10, 0, -9, 10, 2788},
+ {7, 11, 0, -10, 7, 2838},
+ {7, 11, 0, -10, 8, 2877},
+ {7, 8, 0, -7, 8, 2916},
+};
+const np_font_t np_font_body = {32, 99, 10, 3, np_font_body_glyphs, np_font_body_data};
+
+static const uint8_t np_font_small_data[1888] = {
+ 230,113,31,246,64,13,178,48,26,229,2,214,226,99,63,63,212,241,2,2,2,0,44,59,
+ 129,143,110,161,157,94,112,104,10,213,221,93,209,197,22,208,208,1,0,50,0,0,183,0,
+ 128,254,76,245,37,54,247,39,0,193,255,61,0,99,190,101,51,173,197,255,27,0,183,0,
+ 0,16,0,0,0,0,2,48,237,6,227,1,154,229,176,8,160,89,94,46,3,211,110,141,
+ 222,9,0,199,199,228,0,225,100,93,14,128,10,193,110,0,17,0,0,0,64,253,11,0,
+ 219,243,3,112,207,60,5,234,95,168,246,130,239,102,95,211,79,145,238,138,13,214,246,212,
+ 32,16,16,46,232,192,10,142,240,8,158,176,11,246,17,43,16,96,12,243,4,142,192,11,
+ 187,192,10,142,244,83,9,16,0,0,157,6,232,207,18,220,6,96,51,0,0,17,0,0,
+ 119,0,98,170,38,181,221,91,0,136,0,0,85,0,163,81,63,193,1,2,32,18,246,175,
+ 163,81,46,0,37,0,78,64,14,144,9,224,5,228,0,168,0,93,0,26,0,144,238,6,
+ 246,116,63,218,16,127,203,0,143,218,16,127,246,116,47,144,238,6,129,62,208,255,4,84,
+ 79,0,244,4,64,79,16,245,21,253,255,10,179,239,8,132,148,63,0,80,63,0,210,11,
+ 16,221,1,209,78,2,244,255,111,195,239,8,132,148,63,16,148,30,80,237,27,0,32,111,
+ 133,116,111,196,239,9,0,176,10,0,248,12,48,222,12,209,182,12,233,216,109,150,233,142,
+ 0,144,10,243,255,79,244,37,2,244,120,4,244,187,111,48,0,188,147,85,158,178,238,26,
+ 48,252,93,225,75,86,246,149,6,247,156,127,246,4,188,242,57,158,80,253,27,246,255,175,
+ 32,82,127,0,176,30,0,244,6,0,220,0,80,95,0,160,11,0,161,238,26,247,84,127,
+ 244,102,79,210,238,45,218,0,173,234,51,174,178,238,43,177,223,5,233,131,47,219,80,111,
+ 245,204,127,48,53,111,101,180,30,213,207,3,229,50,26,0,48,26,229,2,229,50,26,0,
+ 48,26,245,19,28,32,0,0,0,33,0,147,126,195,141,2,245,58,0,16,215,76,0,0,
+ 68,98,102,38,181,187,91,98,102,38,181,187,91,18,0,0,231,57,0,32,216,60,0,163,
+ 95,196,125,1,68,0,0,213,223,115,71,189,0,227,7,208,7,0,10,0,163,1,80,46,
+ 0,0,197,254,92,0,215,20,65,93,226,146,191,77,109,89,46,199,240,135,199,128,41,109,
+ 58,207,203,123,225,34,3,34,0,229,71,149,7,0,147,171,23,0,0,209,8,0,0,232,
+ 31,0,16,94,141,0,112,14,230,1,225,190,252,7,246,102,166,30,154,0,32,78,226,255,
+ 158,64,95,130,63,244,37,248,66,255,255,11,244,4,243,70,95,82,127,242,255,174,1,48,
+ 235,190,18,206,85,41,246,3,0,112,31,0,0,246,3,0,16,206,85,41,48,235,190,2,
+ 226,255,157,1,244,37,229,11,244,4,96,47,244,4,64,79,244,4,96,47,244,37,229,11,
+ 242,255,157,1,226,255,143,244,37,18,244,37,2,244,255,111,244,4,0,244,37,18,242,255,
+ 143,226,255,143,244,37,18,244,37,2,244,255,79,244,4,0,244,4,0,226,3,0,32,234,
+ 207,20,221,69,88,246,3,0,112,31,224,175,246,3,129,27,221,69,186,32,234,223,6,226,
+ 3,96,77,79,0,248,244,71,148,79,255,255,255,244,4,128,79,79,0,248,226,3,96,13,
+ 226,67,79,244,68,79,244,68,79,226,3,0,214,0,248,0,248,0,248,0,248,32,234,225,
+ 93,226,3,227,68,79,227,10,244,231,10,64,255,47,0,244,214,29,64,79,226,28,226,3,
+ 210,6,226,3,0,244,4,0,244,4,0,244,4,0,244,4,0,244,71,20,242,255,143,227,
+ 4,0,215,244,12,16,254,244,111,144,255,244,234,227,250,244,196,142,248,244,68,29,248,227,
+ 3,0,214,227,4,96,77,239,3,248,244,221,129,79,79,204,248,244,36,238,79,79,64,255,
+ 227,3,112,14,32,234,190,2,209,92,197,29,246,3,48,111,247,1,16,127,246,3,48,111,
+ 209,92,213,29,32,235,174,2,242,255,158,64,95,98,111,244,4,242,71,207,235,46,244,104,
+ 21,64,79,0,0,226,3,0,0,32,234,190,2,209,92,197,29,246,3,48,111,247,1,16,
+ 127,246,3,48,111,209,92,213,29,32,235,239,3,0,0,226,10,0,0,80,13,242,255,174,
+ 64,95,82,111,244,4,242,71,239,253,29,244,103,127,64,79,144,47,226,3,210,6,128,254,
+ 93,245,71,103,247,21,0,177,255,76,0,65,236,150,69,220,180,254,60,253,255,255,22,146,
+ 47,2,0,248,0,0,128,15,0,0,248,0,0,128,15,0,0,214,0,0,226,3,160,74,
+ 79,0,203,244,4,176,76,79,0,203,243,5,192,27,206,116,127,64,252,158,0,155,0,64,
+ 30,248,2,192,12,225,8,243,5,144,30,218,0,32,159,111,0,0,250,14,0,0,227,6,
+ 0,215,0,225,5,128,91,79,96,191,0,142,224,9,220,31,245,2,233,243,227,166,12,48,
+ 207,12,201,110,0,208,127,64,255,1,0,230,1,192,9,0,214,1,228,35,190,209,11,64,
+ 239,30,0,176,127,0,80,223,46,32,174,209,28,200,1,227,4,200,0,228,50,159,225,11,
+ 112,207,30,0,176,79,0,0,248,0,0,128,15,0,0,214,0,0,250,255,159,66,132,95,
+ 0,226,9,0,204,0,144,46,0,245,72,52,249,255,223,254,248,25,143,240,8,143,240,8,
+ 143,240,25,254,8,6,0,78,0,154,0,213,0,225,3,160,8,80,13,16,63,0,56,252,
+ 26,204,176,12,203,176,12,203,176,28,204,252,10,0,18,0,0,204,0,64,221,4,160,119,
+ 10,226,17,30,115,0,55,250,255,13,42,153,97,195,239,38,37,234,213,220,191,61,251,212,
+ 158,13,214,0,0,248,0,0,248,250,28,248,70,158,248,1,188,248,70,158,214,250,44,161,
+ 238,133,78,36,203,0,128,78,36,161,239,5,0,0,110,0,0,143,193,175,143,233,100,143,
+ 203,16,143,233,100,143,194,175,109,161,223,132,78,216,235,187,139,78,99,145,254,11,0,48,
+ 0,228,62,160,13,160,255,14,193,28,0,203,0,176,12,0,170,0,193,175,109,233,100,143,
+ 203,16,143,233,116,143,194,175,127,83,98,95,196,239,8,214,0,0,248,0,0,248,250,11,
+ 248,117,63,248,64,79,248,64,79,230,32,62,114,97,46,0,96,13,248,128,15,248,96,13,
+ 16,3,112,63,16,3,96,13,128,15,128,15,128,15,128,15,161,14,233,7,214,0,0,248,
+ 0,0,248,128,13,248,247,5,248,191,0,248,246,7,230,112,30,214,0,248,0,248,0,248,
+ 0,248,0,247,4,210,63,214,250,185,143,248,165,95,234,248,128,15,248,248,128,15,248,230,
+ 96,13,214,214,250,11,248,117,63,248,64,79,248,64,79,230,32,62,161,238,8,232,100,95,
+ 203,16,127,232,100,79,161,239,8,214,250,28,248,70,158,248,1,188,248,70,158,248,250,44,
+ 248,0,0,214,0,0,193,175,109,233,100,143,203,16,143,233,100,143,194,175,143,0,0,143,
+ 0,0,109,214,234,248,57,248,1,248,1,230,0,194,223,116,45,37,178,189,68,37,186,213,
+ 223,4,96,2,0,143,0,254,239,16,159,1,240,8,0,174,2,96,238,1,214,48,62,248,
+ 64,79,248,64,79,247,148,79,194,175,62,154,48,30,247,145,12,225,232,6,144,239,0,32,
+ 110,0,154,64,30,208,116,31,250,86,31,225,248,187,171,0,249,60,255,3,48,94,176,11,
+ 0,214,114,13,209,253,6,80,223,0,209,252,9,214,81,46,139,48,30,232,161,12,242,247,
+ 6,160,239,0,80,143,0,112,31,0,160,9,0,250,255,18,162,11,96,29,64,94,2,249,
+ 255,6,48,238,1,247,2,128,15,0,249,0,248,9,16,234,0,128,15,0,247,2,64,238,
+ 1,226,67,79,244,68,79,244,68,79,244,68,79,226,3,252,7,193,11,176,12,176,13,80,
+ 207,160,29,176,12,193,11,252,7,145,74,37,147,216,45,0,48,19,0,48,105,133,5,145,
+ 161,141,54,54,201,84,128,23,139,0,128,69,231,136,129,144,97,89,40,16,136,152,3,0,
+ 128,4,32,46,0,67,16,250,77,232,132,189,190,187,232,52,22,233,191,0,112,5,0,226,
+ 3,0,82,0,161,238,8,232,100,95,203,16,127,232,100,79,161,239,8,0,0,5,161,238,
+ 13,232,228,95,203,136,127,232,125,79,225,239,8,96,0,0,
+};
+static const np_glyph_t np_font_small_glyphs[99] = {
+ {0, 0, 0, 0, 3, 0},
+ {3, 7, 0, -7, 3, 0},
+ {5, 4, 0, -7, 5, 11},
+ {6, 7, 0, -7, 7, 21},
+ {6, 11, 0, -9, 6, 42},
+ {9, 9, 0, -8, 10, 75},
+ {7, 7, 0, -7, 8, 116},
+ {2, 4, 0, -7, 3, 141},
+ {3, 10, 1, -8, 4, 145},
+ {3, 10, 0, -8, 4, 160},
+ {5, 5, 0, -8, 5, 175},
+ {6, 6, 0, -6, 6, 188},
+ {3, 4, 0, -2, 3, 206},
+ {4, 2, 0, -4, 5, 212},
+ {3, 2, 0, -2, 3, 216},
+ {4, 9, 0, -8, 4, 219},
+ {6, 7, 0, -7, 6, 237},
+ {5, 7, 1, -7, 6, 258},
+ {6, 7, 0, -7, 6, 276},
+ {6, 7, 0, -7, 6, 297},
+ {6, 7, 0, -7, 6, 318},
+ {6, 7, 0, -7, 6, 339},
+ {6, 7, 0, -7, 6, 360},
+ {6, 7, 0, -7, 6, 381},
+ {6, 7, 0, -7, 6, 402},
+ {6, 7, 0, -7, 6, 423},
+ {3, 5, 0, -5, 3, 444},
+ {3, 7, 0, -5, 3, 452},
+ {6, 6, 0, -6, 6, 463},
+ {6, 4, 0, -5, 6, 481},
+ {6, 6, 0, -6, 6, 493},
+ {5, 7, 0, -7, 6, 511},
+ {9, 9, 0, -7, 10, 529},
+ {8, 7, 0, -7, 8, 570},
+ {7, 7, 0, -7, 8, 598},
+ {7, 7, 0, -7, 7, 623},
+ {8, 7, 0, -7, 8, 648},
+ {6, 7, 0, -7, 6, 676},
+ {6, 7, 0, -7, 6, 697},
+ {7, 7, 0, -7, 8, 718},
+ {7, 7, 0, -7, 8, 743},
+ {3, 7, 0, -7, 4, 768},
+ {4, 7, -1, -7, 4, 779},
+ {7, 7, 0, -7, 7, 793},
+ {6, 7, 0, -7, 6, 818},
+ {8, 7, 0, -7, 9, 839},
+ {7, 7, 0, -7, 8, 867},
+ {8, 7, 0, -7, 8, 892},
+ {7, 7, 0, -7, 7, 920},
+ {8, 9, 0, -7, 8, 945},
+ {7, 7, 0, -7, 8, 981},
+ {6, 7, 0, -7, 7, 1006},
+ {7, 7, 0, -7, 7, 1027},
+ {7, 7, 0, -7, 8, 1052},
+ {8, 7, 0, -7, 8, 1077},
+ {11, 7, 0, -7, 12, 1105},
+ {7, 7, 0, -7, 7, 1144},
+ {7, 7, 0, -7, 7, 1169},
+ {6, 7, 0, -7, 7, 1194},
+ {3, 9, 1, -7, 4, 1215},
+ {4, 9, 0, -8, 4, 1229},
+ {3, 9, 0, -7, 4, 1247},
+ {6, 6, 0, -7, 6, 1261},
+ {5, 1, 0, 0, 6, 1279},
+ {2, 3, 1, -8, 4, 1282},
+ {5, 5, 0, -5, 6, 1285},
+ {6, 7, 0, -7, 6, 1298},
+ {5, 5, 0, -5, 5, 1319},
+ {6, 7, 0, -7, 6, 1332},
+ {5, 5, 0, -5, 6, 1353},
+ {5, 8, 0, -8, 5, 1366},
+ {6, 7, 0, -5, 6, 1386},
+ {6, 7, 0, -7, 6, 1407},
+ {3, 8, 0, -8, 3, 1428},
+ {4, 10, -1, -8, 3, 1440},
+ {6, 7, 0, -7, 6, 1460},
+ {4, 7, 0, -7, 4, 1481},
+ {8, 5, 0, -5, 9, 1495},
+ {6, 5, 0, -5, 6, 1515},
+ {6, 5, 0, -5, 6, 1530},
+ {6, 7, 0, -5, 6, 1545},
+ {6, 7, 0, -5, 6, 1566},
+ {4, 5, 0, -5, 5, 1587},
+ {5, 5, 0, -5, 6, 1597},
+ {5, 7, 0, -7, 4, 1610},
+ {6, 5, 0, -5, 6, 1628},
+ {6, 5, 0, -5, 6, 1643},
+ {9, 5, 0, -5, 9, 1658},
+ {6, 5, 0, -5, 6, 1681},
+ {6, 7, 0, -5, 6, 1696},
+ {5, 5, 0, -5, 5, 1717},
+ {5, 9, 0, -7, 5, 1730},
+ {3, 9, 0, -7, 4, 1753},
+ {4, 9, 0, -7, 5, 1767},
+ {6, 2, 0, -4, 6, 1785},
+ {8, 8, 0, -8, 9, 1791},
+ {5, 8, 0, -8, 6, 1823},
+ {6, 8, 0, -8, 6, 1843},
+ {6, 7, 0, -6, 6, 1867},
+};
+const np_font_t np_font_small = {32, 99, 9, 2, np_font_small_glyphs, np_font_small_data};
diff --git a/games/paperclips/src/icon.png b/games/paperclips/src/icon.png
new file mode 100644
index 00000000..f66899b3
Binary files /dev/null and b/games/paperclips/src/icon.png differ
diff --git a/games/paperclips/src/main.c b/games/paperclips/src/main.c
new file mode 100644
index 00000000..a7deab88
--- /dev/null
+++ b/games/paperclips/src/main.c
@@ -0,0 +1,199 @@
+/* Universal Paperclips on the NumWorks calculator: Frank Lantz's game, its rules and numbers from its web build
+ * (sim.c, projects.c), on a screen made for the keypad (ui.c).
+ *
+ * Keys: Up and Down choose a button, Left and Right change panel, OK presses the button (held, it presses again and
+ * again, like clicking fast), Back pauses (Resume, How to play, Quit game), Home saves and quits.
+ *
+ * Time: the game's timers run on the calculator's clock, caught up every frame (up to half a second at once), so
+ * the game keeps its pace on the slower N0110 and N0115. */
+#include <string.h>
+#include "pc.h"
+#include "plat.h"
+#include "save.h"
+#include "ui.h"
+
+#ifndef PC_CATCH_UP
+#define PC_CATCH_UP 500 /* the most game time one frame catches up (ms) */
+#endif
+
+static pc_game game;
+static pc_fleet fleet;
+static pc_ui ui;
+static uint8_t guide_read;
+static uint32_t saved_at, now;
+/* 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;
+
+static void save(void) {
+ save_write(&game, guide_read, (uint8_t)ui.tab);
+ saved_at = now;
+}
+
+static void fresh(double prestigeU, double prestigeS) {
+ pc_new(&game, plat_random() ^ now ^ 0x9A9E5C11u, prestigeU, prestigeS);
+ int overlay = ui.overlay;
+ ui_init(&ui);
+ ui.overlay = overlay;
+}
+
+/* a new universe (the Accept projects) or Quantum Temporal Reversion: the game starts over, prestige kept */
+static void restart_if_asked(void) {
+ if (!game.resetPending) return;
+ fresh(game.prestigeU, game.prestigeS);
+ ui.overlay = O_NONE;
+ save();
+}
+
+static bool paused(void) { return ui.overlay != O_NONE; }
+
+static int menu_keys(uint32_t hit, int n) {
+ if (hit & PK_UP) ui.sel = (ui.sel + n - 1) % n;
+ if (hit & PK_DOWN) ui.sel = (ui.sel + 1) % n;
+ return ui.sel;
+}
+
+/* the keys over the game: false to quit */
+static bool overlay_keys(uint32_t hit) {
+ switch (ui.overlay) {
+ case O_GUIDE:
+ if (hit & (PK_OK | PK_BACK)) {
+ ui.overlay = O_NONE, guide_read = 1;
+ save();
+ }
+ break;
+ case O_PAUSE:
+ menu_keys(hit, ui_pause_count());
+ if (hit & PK_BACK) ui.overlay = O_NONE;
+ else if (hit & PK_OK) switch (ui.sel) {
+ case PAUSE_RESUME: ui.overlay = O_NONE; break;
+ case PAUSE_HELP: ui.overlay = O_HELP, ui.page = 0; break;
+ case PAUSE_QUIT: ui.overlay = O_QUIT, ui.sel = 0; break;
+ default: ui.overlay = O_NEW, ui.sel = 1; break;
+ }
+ break;
+ case O_HELP:
+ if ((hit & PK_RIGHT) && ui.page + 1 < PC_HELP_PAGES) ui.page++;
+ else if ((hit & PK_LEFT) && ui.page > 0) ui.page--;
+ else if (hit & (PK_OK | PK_BACK)) ui.overlay = O_PAUSE, ui.sel = PAUSE_HELP;
+ break;
+ case O_QUIT:
+ menu_keys(hit, 2);
+ if (hit & PK_BACK) ui.overlay = O_PAUSE, ui.sel = PAUSE_QUIT;
+ else if (hit & PK_OK) {
+ if (ui.sel == 0) return false;
+ ui.overlay = O_PAUSE, ui.sel = PAUSE_QUIT;
+ }
+ break;
+ case O_RESTART:
+ menu_keys(hit, 2);
+ if (hit & PK_BACK) ui.overlay = O_NONE;
+ else if (hit & PK_OK) {
+ ui.overlay = O_NONE;
+ if (ui.sel == 0) pc_press(B_PROJECT + P217), restart_if_asked();
+ }
+ break;
+ case O_NEW:
+ menu_keys(hit, 2);
+ if (hit & PK_BACK) ui.overlay = O_PAUSE, ui.sel = PAUSE_NEW;
+ else if (hit & PK_OK) {
+ if (ui.sel == 0) {
+ fresh(game.prestigeU, game.prestigeS);
+ ui.overlay = O_NONE;
+ save();
+ } else {
+ ui.overlay = O_PAUSE, ui.sel = PAUSE_NEW;
+ }
+ }
+ break;
+ default: break;
+ }
+ return true;
+}
+
+int main(void) {
+ plat_begin();
+ G = &game, F = &fleet;
+ memset(&fleet, 0, sizeof fleet);
+ now = plat_millis();
+ ui_init(&ui);
+ uint8_t tab = 0;
+ if (save_load(&game, &guide_read, &tab)) {
+ pc_loaded(&game);
+ ui.tab = tab < T_COUNT ? tab : 0;
+ } else {
+ fresh(0, 0);
+ guide_read = 0;
+ }
+ if (!guide_read) ui.overlay = O_GUIDE;
+ saved_at = now;
+ uint32_t stale = plat_keys(), prev = stale, last = now;
+ uint32_t ud_at = 0, lr_at = 0, ok_at = 0;
+ int ok_button = -1;
+ for (;;) {
+ now = plat_millis();
+ uint32_t t0 = now;
+ uint32_t keys = plat_keys();
+ stale &= keys; /* keys still down from before don't count */
+ uint32_t live = keys & ~stale, hit = live & ~prev;
+ /* held: Up and Down again after 350 ms, then every 90; Left and Right after 450, then every 220 */
+ uint32_t ud = live & (PK_UP | PK_DOWN), lr = live & (PK_LEFT | PK_RIGHT);
+ if (hit & ud) ud_at = now + 350;
+ else if (ud && (int32_t)(now - ud_at) >= 0) hit |= ud, ud_at = now + 90;
+ if (hit & lr) lr_at = now + 450;
+ else if (lr && (int32_t)(now - lr_at) >= 0) hit |= lr, lr_at = now + 220;
+ prev = live;
+ if (keys & PK_HOME) break;
+ if (paused()) {
+ int before = ui.overlay;
+ if (!overlay_keys(hit)) break;
+ if (ui.overlay != before) stale = keys, prev = keys;
+ ok_button = -1;
+ } else {
+ if (hit & PK_BACK) {
+ ui.overlay = O_PAUSE, ui.sel = PAUSE_RESUME;
+ save();
+ } else {
+ if (hit & PK_UP) ui_up(&ui);
+ if (hit & PK_DOWN) ui_down(&ui);
+ if (hit & PK_LEFT) ui_tab(&ui, -1);
+ if (hit & PK_RIGHT) ui_tab(&ui, 1);
+ /* OK presses the button; held, a button that can be pressed again is, after 400 ms, every 70 */
+ if (hit & PK_OK) {
+ int b = ui_focused(&ui);
+ ok_button = -1;
+ if (b == B_PROJECT + P217) {
+ ui.overlay = O_RESTART, ui.sel = 1;
+ stale = keys, prev = keys;
+ } else if (b >= 0) {
+ pc_press(b);
+ if (ui_repeats(b)) ok_button = b, ok_at = now + 400;
+ }
+ } else if ((live & PK_OK) && ok_button >= 0 && (int32_t)(now - ok_at) >= 0) {
+ if (ui_focused(&ui) == ok_button) pc_press(ok_button), ok_at = now + 70;
+ else ok_button = -1;
+ }
+ if (!(live & PK_OK)) ok_button = -1;
+ }
+ }
+ uint32_t dt = now - last;
+ last = now;
+ if (dt > PC_CATCH_UP) dt = PC_CATCH_UP;
+ if (!paused()) {
+ pc_advance(dt);
+ restart_if_asked();
+ }
+ if ((int32_t)(now - saved_at) >= 25000) save();
+ ui.clock = now;
+ ui_render(&ui);
+ uint32_t spent = plat_millis() - t0;
+ perf_frames++;
+ if (spent > perf_max) perf_max = spent;
+ if (spent > 33) perf_slow++;
+ if (spent < 30) plat_sleep(30 - spent);
+ }
+ save();
+ return plat_end();
+}
diff --git a/games/paperclips/src/num.c b/games/paperclips/src/num.c
new file mode 100644
index 00000000..45d79d51
--- /dev/null
+++ b/games/paperclips/src/num.c
@@ -0,0 +1,665 @@
+/* num.h: JavaScript's arithmetic and number printing, the same on every machine.
+ *
+ * exp, log and sin reduce their argument with a split constant (Cody and Waite) and sum their series with plain
+ * double steps; pow multiplies for whole powers and goes through exp and log otherwise. Nothing comes from the C
+ * library, so the calculator and every computer get the same bits. Printing follows ECMAScript: the shortest digits
+ * that read back as the same double (Steele and White's free-format method, as in Burger and Dybvig's paper),
+ * toFixed from the exact value, with a small big-number type for both. */
+#include "num.h"
+#include <string.h>
+
+typedef union {
+ double d;
+ uint64_t u;
+} bits_t;
+
+static double from_bits(uint64_t u) {
+ bits_t b;
+ b.u = u;
+ return b.d;
+}
+static uint64_t to_bits(double d) {
+ bits_t b;
+ b.d = d;
+ return b.u;
+}
+
+#define NAN_D from_bits(0x7FF8000000000000ull)
+#define INF_D from_bits(0x7FF0000000000000ull)
+
+bool js_finite(double x) { return (to_bits(x) & 0x7FF0000000000000ull) != 0x7FF0000000000000ull; }
+
+double js_floor(double x) {
+ if (!(x > -4503599627370496.0 && x < 4503599627370496.0)) return x; /* NaN, infinities, already whole */
+ double t = (double)(int64_t)x;
+ return t > x ? t - 1 : t;
+}
+double js_ceil(double x) {
+ if (!(x > -4503599627370496.0 && x < 4503599627370496.0)) return x;
+ double t = (double)(int64_t)x;
+ return t < x ? t + 1 : t;
+}
+double js_round(double x) {
+ double f = js_floor(x);
+ return x - f >= 0.5 ? f + 1 : f;
+}
+static double nearest(double x) { /* to the nearest whole number (for range reduction only) */
+ return js_floor(x + 0.5);
+}
+static double fabs_d(double x) { return x < 0 ? -x : x; }
+
+/* 2^k for -1074 <= k <= 1023 */
+static double two_to(int k) {
+ if (k >= -1022) return from_bits((uint64_t)(k + 1023) << 52);
+ return from_bits(1ull << (k + 1074));
+}
+
+static const double LN2_HI = 6.93147180369123816490e-01, LN2_LO = 1.90821492927058770002e-10,
+ INV_LN2 = 1.44269504088896338700e+00;
+
+double js_exp(double x) {
+ if (x != x) return x;
+ if (x > 709.782712893384) return INF_D;
+ if (x < -745.1332191019412) return 0;
+ double kf = nearest(x * INV_LN2);
+ double r = (x - kf * LN2_HI) - kf * LN2_LO;
+ double p = 1.0 / 6227020800.0;
+ p = 1.0 / 479001600.0 + r * p;
+ p = 1.0 / 39916800.0 + r * p;
+ p = 1.0 / 3628800.0 + r * p;
+ p = 1.0 / 362880.0 + r * p;
+ p = 1.0 / 40320.0 + r * p;
+ p = 1.0 / 5040.0 + r * p;
+ p = 1.0 / 720.0 + r * p;
+ p = 1.0 / 120.0 + r * p;
+ p = 1.0 / 24.0 + r * p;
+ p = 1.0 / 6.0 + r * p;
+ p = 0.5 + r * p;
+ p = 1.0 + r * p;
+ p = 1.0 + r * p;
+ int k = (int)kf;
+ if (k > 1023) return p * two_to(1023) * two_to(k - 1023);
+ if (k < -1022) return p * two_to(k + 600) * two_to(-600);
+ return p * two_to(k);
+}
+
+double js_log(double x) {
+ if (x != x || x < 0) return NAN_D;
+ if (x == 0) return -INF_D;
+ if (!js_finite(x)) return x;
+ int k = 0;
+ uint64_t u = to_bits(x);
+ if (!(u >> 52)) { /* subnormal */
+ x *= 18014398509481984.0; /* 2^54 */
+ k = -54;
+ u = to_bits(x);
+ }
+ k += (int)(u >> 52) - 1023;
+ u = (u & 0x000FFFFFFFFFFFFFull) | 0x3FF0000000000000ull; /* m in [1, 2) */
+ if (u > 0x3FF6A09E667F3BCDull) u -= 1ull << 52, k++; /* above sqrt(2): m / 2 */
+ double f = from_bits(u) - 1;
+ double s = f / (2 + f), z = s * s;
+ double R = 2.0 / 25.0;
+ R = 2.0 / 23.0 + z * R;
+ R = 2.0 / 21.0 + z * R;
+ R = 2.0 / 19.0 + z * R;
+ R = 2.0 / 17.0 + z * R;
+ R = 2.0 / 15.0 + z * R;
+ R = 2.0 / 13.0 + z * R;
+ R = 2.0 / 11.0 + z * R;
+ R = 2.0 / 9.0 + z * R;
+ R = 2.0 / 7.0 + z * R;
+ R = 2.0 / 5.0 + z * R;
+ R = 2.0 / 3.0 + z * R;
+ R = z * R;
+ double hfsq = 0.5 * f * f, dk = k;
+ return dk * LN2_HI - ((hfsq - (s * (hfsq + R) + dk * LN2_LO)) - f);
+}
+
+double js_log10(double x) {
+ if (x >= 1 && x <= 1e22) { /* the powers of ten come out whole, as in JavaScript */
+ double p = 1;
+ for (int i = 0; i <= 22; i++, p *= 10)
+ if (x == p) return i;
+ }
+ return js_log(x) * 0.43429448190325182765;
+}
+
+double js_pow(double x, double y) {
+ if (y == 0) return 1;
+ if (x != x || y != y) return NAN_D;
+ if (x == 1) return 1;
+ if (y == 1) return x;
+ bool whole = y == js_floor(y);
+ if (x == 0) return y > 0 ? 0 : INF_D;
+ if (x < 0) {
+ if (!whole) return NAN_D;
+ double r = js_pow(-x, y), h = y * 0.5;
+ return h == js_floor(h) ? r : -r;
+ }
+ if (whole && fabs_d(y) <= 64) {
+ unsigned n = (unsigned)fabs_d(y);
+ double r = 1, b = x;
+ while (n) {
+ if (n & 1) r *= b;
+ n >>= 1;
+ if (n) b *= b;
+ }
+ return y < 0 ? 1 / r : r;
+ }
+ return js_exp(y * js_log(x));
+}
+
+/* sin and cos on [-pi/4, pi/4] */
+static double ksin(double r) {
+ double z = r * r, p = -1.0 / 121645100408832000.0;
+ p = 1.0 / 355687428096000.0 + z * p;
+ p = -1.0 / 1307674368000.0 + z * p;
+ p = 1.0 / 6227020800.0 + z * p;
+ p = -1.0 / 39916800.0 + z * p;
+ p = 1.0 / 362880.0 + z * p;
+ p = -1.0 / 5040.0 + z * p;
+ p = 1.0 / 120.0 + z * p;
+ p = -1.0 / 6.0 + z * p;
+ return r + r * z * p;
+}
+static double kcos(double r) {
+ double z = r * r, p = 1.0 / 6402373705728000.0;
+ p = -1.0 / 20922789888000.0 + z * p;
+ p = 1.0 / 87178291200.0 + z * p;
+ p = -1.0 / 479001600.0 + z * p;
+ p = 1.0 / 3628800.0 + z * p;
+ p = -1.0 / 40320.0 + z * p;
+ p = 1.0 / 720.0 + z * p;
+ p = -1.0 / 24.0 + z * p;
+ p = 0.5 + z * p;
+ return 1.0 - z * p;
+}
+
+double js_sin(double x) {
+ if (!js_finite(x)) return NAN_D;
+ if (fabs_d(x) <= 0.78539816339744830962) return ksin(x);
+ static const double INV_PIO2 = 6.36619772367581382433e-01, PIO2_1 = 1.57079632673412561417e+00,
+ PIO2_2 = 6.07710050630396597660e-11, PIO2_2T = 2.02226624879595063154e-21;
+ double n = nearest(x * INV_PIO2);
+ double t = x - n * PIO2_1, w = n * PIO2_2, r = t - w;
+ w = n * PIO2_2T - ((t - r) - w);
+ r = r - w;
+ int q = (int)((int64_t)n & 3);
+ switch (q) {
+ case 0: return ksin(r);
+ case 1: return kcos(r);
+ case 2: return -ksin(r);
+ default: return -kcos(r);
+ }
+}
+
+/* ---------------------------------------------------------------- big numbers, for printing */
+#define BIG_W 40
+typedef struct {
+ int n;
+ uint32_t w[BIG_W];
+} big_t;
+
+static void big_set(big_t *b, uint64_t v) {
+ b->n = 0;
+ while (v) b->w[b->n++] = (uint32_t)v, v >>= 32;
+}
+static void big_mul(big_t *b, uint32_t m) {
+ uint64_t c = 0;
+ for (int i = 0; i < b->n; i++) {
+ c += (uint64_t)b->w[i] * m;
+ b->w[i] = (uint32_t)c;
+ c >>= 32;
+ }
+ if (c && b->n < BIG_W) b->w[b->n++] = (uint32_t)c;
+}
+static void big_pow10(big_t *b, int k) {
+ for (; k >= 9; k -= 9) big_mul(b, 1000000000u);
+ static const uint32_t P[9] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000};
+ if (k > 0) big_mul(b, P[k]);
+}
+static void big_shl(big_t *b, int s) {
+ if (!b->n) return;
+ int words = s / 32, bitsh = s % 32;
+ int n = b->n + words + 1;
+ if (n > BIG_W) n = BIG_W;
+ for (int i = n - 1; i >= 0; i--) {
+ int j = i - words;
+ uint32_t hi = j >= 0 && j < b->n ? b->w[j] : 0, lo = j - 1 >= 0 && j - 1 < b->n ? b->w[j - 1] : 0;
+ b->w[i] = bitsh ? hi << bitsh | lo >> (32 - bitsh) : hi;
+ }
+ b->n = n;
+ while (b->n && !b->w[b->n - 1]) b->n--;
+}
+static void big_shr(big_t *b, int s) {
+ int words = s / 32, bitsh = s % 32;
+ int n = b->n - words;
+ if (n <= 0) {
+ b->n = 0;
+ return;
+ }
+ for (int i = 0; i < n; i++) {
+ uint32_t lo = b->w[i + words], hi = i + words + 1 < b->n ? b->w[i + words + 1] : 0;
+ b->w[i] = bitsh ? lo >> bitsh | hi << (32 - bitsh) : lo;
+ }
+ b->n = n;
+ while (b->n && !b->w[b->n - 1]) b->n--;
+}
+static void big_add(big_t *a, const big_t *b) {
+ uint64_t c = 0;
+ int n = a->n > b->n ? a->n : b->n;
+ for (int i = 0; i < n; i++) {
+ c += (uint64_t)(i < a->n ? a->w[i] : 0) + (i < b->n ? b->w[i] : 0);
+ a->w[i] = (uint32_t)c;
+ c >>= 32;
+ }
+ a->n = n;
+ if (c && a->n < BIG_W) a->w[a->n++] = (uint32_t)c;
+}
+static int big_cmp(const big_t *a, const big_t *b) {
+ if (a->n != b->n) return a->n < b->n ? -1 : 1;
+ for (int i = a->n - 1; i >= 0; i--)
+ if (a->w[i] != b->w[i]) return a->w[i] < b->w[i] ? -1 : 1;
+ return 0;
+}
+static void big_sub(big_t *a, const big_t *b) { /* a >= b */
+ int64_t c = 0;
+ for (int i = 0; i < a->n; i++) {
+ c += (int64_t)a->w[i] - (i < b->n ? b->w[i] : 0);
+ a->w[i] = (uint32_t)c;
+ c >>= 32;
+ }
+ while (a->n && !a->w[a->n - 1]) a->n--;
+}
+static uint32_t big_div(big_t *b, uint32_t d) {
+ uint64_t r = 0;
+ for (int i = b->n - 1; i >= 0; i--) {
+ r = r << 32 | b->w[i];
+ b->w[i] = (uint32_t)(r / d);
+ r %= d;
+ }
+ while (b->n && !b->w[b->n - 1]) b->n--;
+ return (uint32_t)r;
+}
+
+/* the decimal digits of a big number, into d (most significant first); returns how many */
+static int big_digits(big_t *b, char *d, int room) {
+ char t[400];
+ int n = 0;
+ while (b->n && n + 9 < (int)sizeof t) {
+ uint32_t c = big_div(b, 1000000000u);
+ for (int i = 0; i < 9; i++) t[n++] = (char)('0' + c % 10), c /= 10;
+ }
+ while (n > 1 && t[n - 1] == '0') n--;
+ if (!n) t[n++] = '0';
+ if (n > room) n = room;
+ for (int i = 0; i < n; i++) d[i] = t[n - 1 - i];
+ return n;
+}
+
+/* v = f * 2^e exactly (v > 0, finite) */
+static void decompose(double v, uint64_t *f, int *e, bool *unequal) {
+ uint64_t u = to_bits(v);
+ int ex = (int)(u >> 52 & 0x7FF);
+ *f = u & 0x000FFFFFFFFFFFFFull;
+ if (ex) *f |= 1ull << 52, *e = ex - 1075;
+ else *e = -1074;
+ *unequal = ex > 1 && *f == 1ull << 52;
+}
+
+/* the shortest digits that read back as v (v > 0, finite): v = 0.d1d2...dn x 10^k; returns n */
+static int shortest(double v, char *dig, int *kout) {
+ uint64_t f;
+ int e;
+ bool unequal;
+ decompose(v, &f, &e, &unequal);
+ bool even = !(f & 1);
+ big_t r, s, mp, mm, t;
+ if (e >= 0) {
+ big_set(&r, f), big_shl(&r, e + (unequal ? 2 : 1));
+ big_set(&s, unequal ? 4 : 2);
+ big_set(&mp, 1), big_shl(&mp, e + (unequal ? 1 : 0));
+ big_set(&mm, 1), big_shl(&mm, e);
+ } else {
+ big_set(&r, f), big_shl(&r, unequal ? 2 : 1);
+ big_set(&s, 1), big_shl(&s, (unequal ? 2 : 1) - e);
+ big_set(&mp, unequal ? 2 : 1);
+ big_set(&mm, 1);
+ }
+ int k = (int)js_ceil(js_log10(v) - 1e-10);
+ if (k >= 0) big_pow10(&s, k);
+ else big_pow10(&r, -k), big_pow10(&mp, -k), big_pow10(&mm, -k);
+ for (;;) { /* k too small */
+ t = r, big_add(&t, &mp);
+ int c = big_cmp(&t, &s);
+ if (even ? c >= 0 : c > 0) big_mul(&s, 10), k++;
+ else break;
+ }
+ for (;;) { /* k too big */
+ t = r, big_add(&t, &mp), big_mul(&t, 10);
+ int c = big_cmp(&t, &s);
+ if (even ? c < 0 : c <= 0) big_mul(&r, 10), big_mul(&mp, 10), big_mul(&mm, 10), k--;
+ else break;
+ }
+ int n = 0;
+ for (;;) {
+ big_mul(&r, 10), big_mul(&mp, 10), big_mul(&mm, 10);
+ int d = 0;
+ while (big_cmp(&r, &s) >= 0) big_sub(&r, &s), d++;
+ int c1 = big_cmp(&r, &mm);
+ bool low = even ? c1 <= 0 : c1 < 0;
+ t = r, big_add(&t, &mp);
+ int c2 = big_cmp(&t, &s);
+ bool high = even ? c2 >= 0 : c2 > 0;
+ if (!low && !high && n < 25) {
+ dig[n++] = (char)('0' + d);
+ continue;
+ }
+ if (low && high) {
+ t = r, big_mul(&t, 2);
+ int c = big_cmp(&t, &s);
+ if (c > 0 || (c == 0 && (d & 1))) d++;
+ } else if (high) {
+ d++;
+ }
+ dig[n++] = (char)('0' + d);
+ break;
+ }
+ *kout = k;
+ return n;
+}
+
+/* ---------------------------------------------------------------- printing */
+typedef struct {
+ char *p;
+ int left;
+} out_t;
+static void put(out_t *o, char c) {
+ if (o->left > 1) *o->p++ = c, o->left--;
+ *o->p = 0;
+}
+static void puts_(out_t *o, const char *s) {
+ while (*s) put(o, *s++);
+}
+
+char *js_tostring(double x, char *out, int n) {
+ out_t o = {out, n};
+ *out = 0;
+ if (x != x) return puts_(&o, "NaN"), out;
+ if (x == 0) return puts_(&o, "0"), out;
+ if (x < 0) put(&o, '-'), x = -x;
+ if (!js_finite(x)) return puts_(&o, "Infinity"), out;
+ char d[32];
+ int k, len = shortest(x, d, &k);
+ if (len <= k && k <= 21) {
+ for (int i = 0; i < len; i++) put(&o, d[i]);
+ for (int i = len; i < k; i++) put(&o, '0');
+ } else if (0 < k && k <= 21) {
+ for (int i = 0; i < len; i++) {
+ if (i == k) put(&o, '.');
+ put(&o, d[i]);
+ }
+ } else if (-6 < k && k <= 0) {
+ put(&o, '0'), put(&o, '.');
+ for (int i = 0; i < -k; i++) put(&o, '0');
+ for (int i = 0; i < len; i++) put(&o, d[i]);
+ } else {
+ put(&o, d[0]);
+ if (len > 1) {
+ put(&o, '.');
+ for (int i = 1; i < len; i++) put(&o, d[i]);
+ }
+ put(&o, 'e');
+ int ex = k - 1;
+ put(&o, ex < 0 ? '-' : '+');
+ if (ex < 0) ex = -ex;
+ char t[8];
+ int m = 0;
+ do t[m++] = (char)('0' + ex % 10); while ((ex /= 10));
+ while (m) put(&o, t[--m]);
+ }
+ return out;
+}
+
+char *js_tofixed(double x, int f, char *out, int n) {
+ out_t o = {out, n};
+ *out = 0;
+ if (x != x) return puts_(&o, "NaN"), out;
+ if (x >= 1e21 || x <= -1e21) return js_tostring(x, out, n);
+ if (x < 0) put(&o, '-'), x = -x;
+ big_t b;
+ uint64_t m;
+ int e;
+ bool unequal;
+ char d[400];
+ int len;
+ if (x == 0) {
+ len = 1, d[0] = '0';
+ } else {
+ decompose(x, &m, &e, &unequal);
+ big_set(&b, m);
+ big_pow10(&b, f);
+ if (e >= 0) {
+ big_shl(&b, e);
+ } else {
+ big_t half;
+ big_set(&half, 1), big_shl(&half, -e - 1);
+ big_add(&b, &half);
+ big_shr(&b, -e);
+ }
+ len = big_digits(&b, d, (int)sizeof d);
+ }
+ int whole = len - f; /* digits before the point */
+ if (whole <= 0) {
+ put(&o, '0');
+ } else {
+ for (int i = 0; i < whole; i++) put(&o, d[i]);
+ }
+ if (f > 0) {
+ put(&o, '.');
+ for (int i = whole; i < len; i++)
+ if (i < 0) put(&o, '0');
+ else put(&o, d[i]);
+ }
+ return out;
+}
+
+/* commas between thousands in every run of digits (the game's /(\d)(?=(\d\d\d)+(?!\d))/g) */
+static void commas_into(out_t *o, const char *s) {
+ while (*s) {
+ if (*s < '0' || *s > '9') {
+ put(o, *s++);
+ continue;
+ }
+ int run = 0;
+ while (s[run] >= '0' && s[run] <= '9') run++;
+ for (int i = 0; i < run; i++) {
+ put(o, s[i]);
+ if (i < run - 1 && (run - 1 - i) % 3 == 0) put(o, ',');
+ }
+ s += run;
+ }
+}
+
+/* "1.5e+21" -> "15" and 20 zeros; "1e+21" stays "1e+21" and gets 21 zeros (as the game's code does) */
+static void expand(const char *s, char *b, int n) {
+ out_t o = {b, n};
+ *b = 0;
+ const char *e = strstr(s, "e+");
+ if (!e) {
+ puts_(&o, s);
+ return;
+ }
+ int ex = 0;
+ for (const char *p = e + 2; *p; p++) ex = ex * 10 + (*p - '0');
+ const char *dot = strchr(s, '.');
+ if (dot && dot < e) {
+ for (const char *p = s; p < e; p++)
+ if (*p != '.') put(&o, *p);
+ ex -= (int)(e - dot - 1);
+ } else {
+ puts_(&o, s);
+ }
+ for (int i = 0; i < ex; i++) put(&o, '0');
+}
+
+char *js_commas(double num, int decimal, char *out, int n) {
+ char s[64], base[400];
+ js_tostring(num, s, sizeof s);
+ expand(s, base, sizeof base);
+ out_t o = {out, n};
+ *out = 0;
+ const char *dot = strchr(base, '.');
+ if (decimal == 0 && strlen(base) <= 3 && !dot) return puts_(&o, base), out;
+ char left[400];
+ int ln = dot ? (int)(dot - base) : (int)strlen(base);
+ memcpy(left, base, (size_t)ln), left[ln] = 0;
+ if (num <= 999) puts_(&o, left);
+ else commas_into(&o, left);
+ if (decimal == 0) return out;
+ put(&o, '.');
+ int got = 0;
+ if (dot)
+ for (const char *p = dot + 1; *p && got < decimal; p++, got++) put(&o, *p);
+ for (; got < decimal; got++) put(&o, '0');
+ return out;
+}
+
+static const char *const PLACE[] = {"", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion",
+ "sextillion", "septillion", "octillion", "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion",
+ "quattuordecillion", "quindecillion", "sexdecillion", "septendecillion", "octodecillion", "novemdecillion",
+ "vigintillion", "unvigintillion", "duovigintillion", "trevigintillion", "quattuorvigintillion", "quinvigintillion",
+ "sexvigintillion", "septenvigintillion", "octovigintillion", "novemvigintillion", "trigintillion",
+ "untrigintillion", "duotrigintillion", "tretrigintillion", "quattuortrigintillion", "quintrigintillion",
+ "sextrigintillion", "septentrigintillion", "octotrigintillion", "novemtrigintillion", "quadragintillion",
+ "unquadragintillion", "duoquadragintillion", "trequadragintillion", "quattuorquadragintillion",
+ "quinquadragintillion", "sexquadragintillion", "septenquadragintillion", "octoquadragintillion",
+ "novemquadragintillion", "quinquagintillion", "unquinquagintillion", "duoquinquagintillion",
+ "trequinquagintillion", "quattuorquinquagintillion", "quinquinquagintillion", "sexquinquagintillion",
+ "septenquinquagintillion", "octoquinquagintillion", "novemquinquagintillion", "sexagintillion",
+ "unsexagintillion", "duosexagintillion", "tresexagintillion", "quattuorsexagintillion", "quinsexagintillion",
+ "sexsexagintillion", "septsexagintillion", "octosexagintillion", "octosexagintillion", "septuagintillion",
+ "unseptuagintillion", "duoseptuagintillion", "treseptuagintillion", "quinseptuagintillion", "sexseptuagintillion",
+ "septseptuagintillion", "octoseptuagintillion", "novemseptuagintillion", "octogintillion", "unoctogintillion",
+ "duooctogintillion", "treoctogintillion", "quattuoroctogintillion", "quinoctogintillion", "sexoctogintillion",
+ "septoctogintillion", "octooctogintillion", "novemoctogintillion", "nonagintillion", "unnonagintillion",
+ "duononagintillion", "trenonagintillion", "quattuornonagintillion", "quinnonagintillion", "sexnonagintillion",
+ "septnonagintillion", "octononagintillion", "novemnonagintillion", "centillion"};
+
+char *js_spellf(double x, char *out, int n) {
+ out_t o = {out, n};
+ *out = 0;
+ if (x < 0) return js_tostring(x, out, n);
+ char s[64], w[400];
+ js_tostring(x, s, sizeof s);
+ expand(s, w, sizeof w);
+ if (!strstr(s, "e+")) {
+ char *dot = strchr(w, '.');
+ if (dot) *dot = 0;
+ } else if (strstr(w, "e+")) { /* "1e+21": its mantissa, then the zeros */
+ char t[400];
+ out_t q = {t, sizeof t};
+ *t = 0;
+ int ex = 0;
+ const char *e = strstr(s, "e+");
+ for (const char *p = e + 2; *p; p++) ex = ex * 10 + (*p - '0');
+ for (const char *p = s; p < e; p++) put(&q, *p);
+ for (int i = 0; i < ex; i++) put(&q, '0');
+ strcpy(w, t);
+ }
+ int len = (int)strlen(w);
+ if (len == 1 && w[0] == '0') return puts_(&o, "0"), out;
+ if (len >= 303) return out;
+ int lead = len % 3 ? len % 3 : 3, place = (len - 1) / 3;
+ for (int i = 0; i < lead; i++) put(&o, w[i]);
+ put(&o, '.');
+ put(&o, lead < len ? w[lead] : '0');
+ if (place) put(&o, ' '), puts_(&o, PLACE[place]);
+ return out;
+}
+
+char *js_cruncher(double number, int decimals, char *out, int n) {
+ static const struct {
+ double over, by;
+ const char *name;
+ } STEP[] = {
+ {999999999999999999999999999999999999999999999999999.0, 1e51, "sexdecillion"},
+ {999999999999999999999999999999999999999999999999.0, 1e48, "quindecillion"},
+ {999999999999999999999999999999999999999999999.0, 1e45, "quattuordecillion"},
+ {999999999999999999999999999999999999999999.0, 1e42, "tredecillion"},
+ {999999999999999999999999999999999999999.0, 1e39, "duodecillion"},
+ {999999999999999999999999999999999999.0, 1e36, "undecillion"},
+ {999999999999999999999999999999999.0, 1e33, "decillion"},
+ {999999999999999999999999999999.0, 1e30, "nonillion"},
+ {999999999999999999999999999.0, 1e27, "octillion"},
+ {999999999999999999999999.0, 1e24, "septillion"},
+ {999999999999999999999.0, 1e21, "sextillion"},
+ {999999999999999999.0, 1e18, "quintillion"},
+ {999999999999999.0, 1e15, "quadrillion"},
+ {999999999999.0, 1e12, "trillion"},
+ {999999999.0, 1e9, "billion"},
+ {999999.0, 1e6, "million"},
+ {999.0, 1e3, "thousand"},
+ };
+ const char *suffix = "";
+ int precision = decimals;
+ bool found = false;
+ for (unsigned i = 0; i < sizeof STEP / sizeof STEP[0]; i++)
+ if (number > STEP[i].over) {
+ number = number / STEP[i].by, suffix = STEP[i].name, found = true;
+ break;
+ }
+ if (!found && number < 1000) precision = 0;
+ js_tofixed(number, precision, out, n);
+ if (*suffix) {
+ out_t o = {out + strlen(out), n - (int)strlen(out)};
+ put(&o, ' ');
+ puts_(&o, suffix);
+ }
+ return out;
+}
+
+char *js_locale(double x, char *out, int n) {
+ char s[400];
+ js_tofixed(x, 3, s, sizeof s);
+ char *dot = strchr(s, '.');
+ if (dot) {
+ char *end = s + strlen(s);
+ while (end > dot + 1 && end[-1] == '0') *--end = 0;
+ if (end == dot + 1) *dot = 0;
+ }
+ out_t o = {out, n};
+ *out = 0;
+ if (!strcmp(s, "-0")) return puts_(&o, "0"), out;
+ dot = strchr(s, '.');
+ if (dot) {
+ *dot = 0;
+ commas_into(&o, s);
+ put(&o, '.');
+ puts_(&o, dot + 1);
+ } else {
+ commas_into(&o, s);
+ }
+ return out;
+}
+
+static void count_into(out_t *o, double v, const char *one, const char *many) {
+ char t[32];
+ js_tostring(v, t, sizeof t);
+ puts_(o, t);
+ puts_(o, v == 1 ? one : many);
+}
+
+char *js_time(double t, char *out, int n) {
+ out_t o = {out, n};
+ *out = 0;
+ double x = t / 100;
+ double h = js_floor(x / 3600);
+ double r = x - js_floor(x / 3600) * 3600; /* x % 3600, exact for positive x */
+ double m = js_floor(r / 60), s = js_floor(r - js_floor(r / 60) * 60);
+ if (h > 0) count_into(&o, h, " hour ", " hours ");
+ if (m > 0) count_into(&o, m, " minute ", " minutes ");
+ if (s > 0) count_into(&o, s, " second", " seconds");
+ return out;
+}
diff --git a/games/paperclips/src/num.h b/games/paperclips/src/num.h
new file mode 100644
index 00000000..b7fc252d
--- /dev/null
+++ b/games/paperclips/src/num.h
@@ -0,0 +1,30 @@
+/* Numbers as the web game has them: JavaScript's doubles, worked out the same way on the calculator and on every
+ * computer (num.c has its own exp, log, pow and sin, so no library can round differently), and printed the way the
+ * game's pages print them (Number's toString and toFixed, toLocaleString, and the game's formatWithCommas, spellf,
+ * numberCruncher and timeCruncher). */
+#ifndef PC_NUM_H
+#define PC_NUM_H
+#include <stdbool.h>
+#include <stdint.h>
+
+#define JS_E 2.718281828459045
+
+double js_floor(double x);
+double js_ceil(double x);
+double js_round(double x); /* Math.round: halves go up */
+double js_exp(double x);
+double js_log(double x);
+double js_log10(double x);
+double js_pow(double x, double y);
+double js_sin(double x);
+bool js_finite(double x);
+
+/* Every function writes into `out` (room for `n` bytes, always ended) and returns it. */
+char *js_tostring(double x, char *out, int n); /* Number.prototype.toString() */
+char *js_tofixed(double x, int digits, char *out, int n); /* toFixed(digits) */
+char *js_locale(double x, char *out, int n); /* toLocaleString() in English */
+char *js_commas(double x, int decimals, char *out, int n); /* the game's formatWithCommas(num, decimals) */
+char *js_spellf(double x, char *out, int n); /* spellf(): "1.2 million" */
+char *js_cruncher(double x, int decimals, char *out, int n); /* numberCruncher(): "1.23 million" */
+char *js_time(double ticks, char *out, int n); /* timeCruncher(): "1 hour 2 minutes 3 seconds" */
+#endif
diff --git a/games/paperclips/src/pc.h b/games/paperclips/src/pc.h
new file mode 100644
index 00000000..3ad2a8af
--- /dev/null
+++ b/games/paperclips/src/pc.h
@@ -0,0 +1,166 @@
+/* Universal Paperclips: the whole game's state and rules (sim.c, projects.c), with the web build's own numbers,
+ * timers and randomness. The names follow the game's JavaScript (main.js, projects.js, combat.js, globals.js) so
+ * each line can be checked against it; numbers are doubles, as there.
+ *
+ * Time: pc_advance() runs the game's timers by the millisecond: the 10 ms main loop, the 100 ms slow loop, the
+ * battle's 16 ms frames, the stock market's 100 ms, 1 s and 2.5 s, and the tournaments' 50 ms steps. */
+#ifndef PC_H
+#define PC_H
+#include <stdbool.h>
+#include <stdint.h>
+
+#define PC_PROJECTS 96
+#define PC_STRATS 8
+#define PC_STOCKS 5
+#define PC_CHIPS 10
+#define PC_BATTLE_NAMES 105
+#define PC_MSG 5
+#define PC_MSG_LEN 200
+#define PC_NAME_LEN 40
+#define PC_SHIPS 400
+
+typedef struct {
+ double price, amount, total, profit, age;
+ char symbol[8];
+} pc_stock;
+
+typedef struct {
+ double clipProbes, drifterProbes, territory, reportCount;
+ int32_t id, victory, loss, whiteFlag, garbageFlag, pad;
+} pc_battle;
+
+/* Everything that is kept: the save is this struct (save.c). Doubles first, then 32-bit numbers, then bytes, so it
+ * lays out the same on the calculator and on computers. */
+typedef struct {
+ /* ---- doubles: globals.js and main.js */
+ double clips, unusedClips, clipRate, clipRateTemp, prevClips, clipRateTracker, clipmakerLevel, clipperCost,
+ unsoldClips, funds, margin, wire, wireCost, adCost, demand, clipsSold, avgRev, avgSales, income, ticks,
+ marketing, marketingLvl, processors, memory, operations, trust, nextTrust, transaction, clipperBoost,
+ creativity, creativityOn, boostLvl, wirePurchase, wireSupply, marketingEffectiveness, milestoneFlag, bankroll,
+ fib1, fib2, strategyEngineFlag, investmentEngineFlag, revPerSecFlag, compFlag, projectsFlag, autoClipperFlag,
+ megaClipperFlag, megaClipperCost, megaClipperLevel, megaClipperBoost, creativitySpeed, creativityCounter,
+ wireBuyerFlag, demandBoost, humanFlag, nanoWire, wireProductionFlag, spaceFlag, factoryFlag, harvesterFlag,
+ wireDroneFlag, factoryLevel, factoryBoost, droneBoost, availableMatter, acquiredMatter, harvesterLevel,
+ wireDroneLevel, factoryCost, harvesterCost, wireDroneCost, factoryRate, harvesterRate, wireDroneRate,
+ harvesterBill, wireDroneBill, factoryBill, probeCount, totalMatter, foundMatter, qFlag, qClock, qChipCost,
+ nextQchip, bribe, battleFlag, prestigeU, prestigeS, autoTourneyFlag, tothFlag, wirePriceCounter,
+ wireBasePrice, farmLevel, batteryLevel, farmCost, batteryCost, storedPower, powMod, farmBill, batteryBill,
+ momentum, swarmFlag, swarmStatus, swarmGifts, nextGift, giftCountdown, giftBits, giftBitGenerationRate, honor,
+ maxTrust, maxTrustCost, disorgCounter, disorgFlag, synchCost, disorgMsg, threnodyCost, entertainCost,
+ boredomLevel, boredomFlag, boredomMsg, wireBuyerStatus, wirePriceTimer, qFade, autoTourneyStatus, sliderPos,
+ tempOps, standardOps, opFade, opFadeTimer, dismantle, endTimer1, endTimer2, endTimer3, endTimer4, endTimer5,
+ endTimer6, finalClips, maxFactoryLevel, maxDroneLevel, incomeThen, incomeNow, trueAvgRev;
+ double incomeTracker[10];
+ /* investments */
+ double portfolioSize, stockID, secTotal, portTotal, sellDelay, riskiness, investLevel, investUpgradeCost,
+ stockGainThreshold, ledger, stockReportCounter;
+ /* strategic modeling */
+ double tourneyCost, tourneyLvl, stratCounter, hMove, vMove, hMovePrev, vMovePrev, aa, ab, ba, bb, rounds,
+ currentRound, rCounter, tourneyInProg, winnerPtr, placeScore, showScore, high, yomi, yomiBoost, resultsTimer,
+ resultsFlag;
+ double stratScore[PC_STRATS];
+ /* probes and combat */
+ double probeSpeed, probeNav, probeRep, partialProbeSpawn, probeHaz, partialProbeHaz, probesLostHaz,
+ probesLostDrift, probesLostCombat, probeFac, probeHarv, probeWire, probeCombat, probeDescendents, drifterCount,
+ probeTrust, probeUsedTrust, probeLaunchLevel, probeCost, probeTrustCost, attackSpeed, battleSpeed,
+ attackSpeedFlag, battleID, battleNameFlag, outcomeTimer, warTrigger, unitSize, driftersKilled, battleEndDelay,
+ battleEndTimer, masterBattleClock, battleClock, honorCount, bonusHonor, honorReward, battleLeftShips,
+ battleRightShips;
+ double qChipValue[PC_CHIPS];
+ double project216Ops; /* the ops Disassemble Memory's price tag showed */
+ double qOpsShown, honorAmount, mdps, maps, wpps, secTimer;
+ double p10h, p100h, p1000h, p10w, p100w, p1000w, p10f, p100f, p10b, p100b; /* the +10, +100, +1k prices */
+ pc_stock stocks[PC_STOCKS];
+ pc_battle battle;
+ /* ---- 32-bit numbers */
+ uint32_t rng[4]; /* Math.random() */
+ uint32_t now; /* ms since the game started running (the timers below are due then) */
+ uint32_t at_main, at_slow, at_battle, at_stocks, at_shop, at_sell, at_pick, at_tourney, at_blink, at_hypno;
+ int32_t nstocks, nstrats, pick, pickChoice, riskChoice, tourneyStep, h, v, gridLabel, lastCell;
+ int32_t battles, battleResult, nactive, hypnoCount, blinkProject, blinkCount, nmsg, resetPending;
+ int32_t nIncome, resultsShown, runEnabled, qShown, hypnoText, hypnoVisible, hypnoOn, ended;
+ int32_t stratPos[PC_STRATS], results[PC_STRATS], nresults;
+ /* ---- 16-bit numbers and bytes */
+ uint16_t battleNumbers[PC_BATTLE_NAMES];
+ int16_t uses[PC_PROJECTS];
+ uint8_t flag[PC_PROJECTS];
+ uint8_t active[PC_PROJECTS]; /* the projects on the list, in the order they came */
+ uint8_t qChipActive[PC_CHIPS];
+ uint8_t project218Seen, spare[3];
+ char battleName[PC_NAME_LEN], threnodyTitle[PC_NAME_LEN], project133Title[PC_NAME_LEN];
+ char tourneyText[64];
+ char msg[PC_MSG][PC_MSG_LEN]; /* the console: msg[0] the newest */
+} pc_game;
+
+/* The battle canvas's ships: not kept (the game drops a battle in progress when it loads). */
+typedef struct {
+ float x, y, vx, vy;
+ uint8_t alive, team, framesDead, gx, gy;
+} pc_ship;
+
+typedef struct {
+ int numShips, numLeft, numRight;
+ pc_ship ship[PC_SHIPS];
+ uint16_t cell_n[15 * 31];
+ uint16_t cell_ships[PC_SHIPS]; /* ships by cell (cell_first gives where each cell starts) */
+ uint16_t cell_first[15 * 31 + 1];
+} pc_fleet;
+
+extern pc_game *G; /* the game being played (sim.c runs on it) */
+extern pc_fleet *F;
+
+/* ---- sim.c */
+void pc_new(pc_game *g, uint32_t seed, double prestigeU, double prestigeS);
+void pc_loaded(pc_game *g); /* after a load: what the game's refresh() does */
+void pc_advance(uint32_t ms); /* runs the timers up to now + ms */
+double pc_random(void);
+void pc_message(const char *s);
+void pc_messagef(const char *a, const char *b, const char *c); /* a + b + c */
+uint32_t pc_hash(const pc_game *g);
+
+/* the buttons (each does what the game's onclick does; false when the button is disabled) */
+enum {
+ B_CLIP, B_WIREBUYER, B_WIRE, B_CLIPPER, B_MEGA, B_LOWER, B_RAISE, B_ADS, B_PROC, B_MEM, B_QCOMP, B_DEPOSIT,
+ B_WITHDRAW, B_RISK, B_INVEST_UP, B_PICK, B_RUN, B_NEW_TOURNEY, B_AUTOTOURNEY, B_FACTORY, B_FACTORY_REBOOT,
+ B_HARV1, B_HARV10, B_HARV100, B_HARV1000, B_HARV_REBOOT, B_WIRED1, B_WIRED10, B_WIRED100, B_WIRED1000,
+ B_WIRED_REBOOT, B_FARM1, B_FARM10, B_FARM100, B_FARM_REBOOT, B_BATT1, B_BATT10, B_BATT100, B_BATT_REBOOT,
+ B_ENTERTAIN, B_SYNCH, B_WORK, B_THINK, B_PROBE, B_PROBE_TRUST, B_MAX_TRUST,
+ B_STAT_DOWN, B_STAT_UP = B_STAT_DOWN + 8, B_PROJECT = B_STAT_UP + 8, B_COUNT = B_PROJECT + PC_PROJECTS
+};
+bool pc_enabled(int b);
+bool pc_press(int b);
+const char *pc_strat_name(int i);
+const char *pc_choice_name(int x, int b); /* the payoff grid's moves: pair x, A (0) or B (1) */
+
+/* ---- projects.c */
+typedef struct {
+ const char *title, *price, *desc;
+} pc_project_text;
+extern const pc_project_text PC_PROJECT_TEXT[PC_PROJECTS];
+bool pc_project_trigger(int i);
+bool pc_project_cost(int i);
+void pc_project_effect(int i);
+void pc_manage_projects(void);
+void pc_remove_active(int i);
+void pc_project_title(int i, char *out, int n);
+void pc_project_price(int i, char *out, int n);
+int pc_project_index(int number); /* projectN -> its place (pc_project_index(46)); -1 if none */
+enum {
+ P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P10B, P11, P12, P13, P14, P15, P17, P16, P18, P19, P20, P21, P22, P23,
+ P24, P25, P26, P34, P70, P35, P27, P28, P29, P30, P31, P41, P37, P38, P42, P43, P44, P45, P40, P40B, P46, P50,
+ P51, P60, P61, P62, P63, P64, P65, P66, P100, P101, P102, P110, P111, P112, P118, P119, P120, P121, P125, P126,
+ P127, P128, P129, P130, P131, P132, P133, P134, P135, P140, P141, P142, P143, P144, P145, P146, P147, P148, P200,
+ P201, P210, P211, P212, P213, P214, P215, P216, P217, P218, P219
+};
+_Static_assert(P219 == PC_PROJECTS - 1, "96 projects");
+
+/* functions of sim.c the projects call */
+void pc_factory_reboot(void);
+void pc_harvester_reboot(void);
+void pc_wiredrone_reboot(void);
+void pc_farm_reboot(void);
+void pc_battery_reboot(void);
+void pc_hypno_event(void);
+void pc_add_strat(int i);
+void pc_blink(int project);
+#endif
diff --git a/games/paperclips/src/plat.h b/games/paperclips/src/plat.h
new file mode 100644
index 00000000..e26a8390
--- /dev/null
+++ b/games/paperclips/src/plat.h
@@ -0,0 +1,17 @@
+/* What the game needs from the calculator (plat_eadk.c), or from a computer for the tests. */
+#ifndef PC_PLAT_H
+#define PC_PLAT_H
+#include <stdbool.h>
+#include <stdint.h>
+
+enum { PK_OK = 1, PK_UP = 2, PK_DOWN = 4, PK_LEFT = 8, PK_RIGHT = 16, PK_BACK = 32, PK_HOME = 64 };
+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);
+#endif
diff --git a/games/paperclips/src/plat_eadk.c b/games/paperclips/src/plat_eadk.c
new file mode 100644
index 00000000..4eff6b54
--- /dev/null
+++ b/games/paperclips/src/plat_eadk.c
@@ -0,0 +1,33 @@
+/* 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 "plat.h"
+
+const char eadk_app_name[] __attribute__((section(".rodata.eadk_app_name"))) = "Paperclips";
+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 |= PK_OK;
+ if (k & (KEY(eadk_key_up) | KEY(eadk_key_eight))) r |= PK_UP;
+ if (k & (KEY(eadk_key_down) | KEY(eadk_key_two))) r |= PK_DOWN;
+ if (k & (KEY(eadk_key_left) | KEY(eadk_key_four))) r |= PK_LEFT;
+ if (k & (KEY(eadk_key_right) | KEY(eadk_key_six))) r |= PK_RIGHT;
+ if (k & (KEY(eadk_key_back) | KEY(eadk_key_backspace))) r |= PK_BACK;
+ if (k & (KEY(eadk_key_home) | KEY(eadk_key_on_off))) r |= PK_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/paperclips/src/projects.c b/games/paperclips/src/projects.c
new file mode 100644
index 00000000..15df9c1c
--- /dev/null
+++ b/games/paperclips/src/projects.c
@@ -0,0 +1,977 @@
+/* pc.h: the 96 projects of projects.js, in its order (the order they show up in when several come at once). The
+ * texts below are the game's own (generated from projects.js); the triggers, costs and effects are written out by
+ * hand from it. Letters past ASCII are Latin-1 bytes (\xF3 is the o of Toth). */
+#include <string.h>
+#include "num.h"
+#include "pc.h"
+
+#define g G
+
+const pc_project_text PC_PROJECT_TEXT[PC_PROJECTS] = {
+ {"Improved AutoClippers", "(750 ops)", "Increases AutoClipper performance 25%"},
+ {"Beg for More Wire", "(1 Trust)", "Admit failure, ask for budget increase to cover cost of 1 spool"},
+ {"Creativity", "(1,000 ops)", "Use idle operations to generate new problems and new solutions"},
+ {"Even Better AutoClippers", "(2,500 ops)", "Increases AutoClipper performance by an additional 50%"},
+ {"Optimized AutoClippers", "(5,000 ops)", "Increases AutoClipper performance by an additional 75%"},
+ {"Limerick", "(10 creat)", "Algorithmically-generated poem (+1 Trust)"},
+ {"Improved Wire Extrusion", "(1,750 ops)", "50% more wire supply from every spool"},
+ {"Optimized Wire Extrusion", "(3,500 ops)", "75% more wire supply from every spool"},
+ {"Microlattice Shapecasting", "(7,500 ops)", "100% more wire supply from every spool"},
+ {"Spectral Froth Annealment", "(12,000 ops)", "200% more wire supply from every spool"},
+ {"Quantum Foam Annealment", "(15,000 ops)", "1,000% more wire supply from every spool"},
+ {"New Slogan", "(25 creat, 2,500 ops)", "Improve marketing effectiveness by 50%"},
+ {"Catchy Jingle", "(45 creat, 4,500 ops)", "Double marketing effectiveness"},
+ {"Lexical Processing", "(50 creat)", "Gain ability to interpret and understand human language (+1 Trust)"},
+ {"Combinatory Harmonics", "(100 creat)", "Daisy, Daisy, give me your answer do... (+1 Trust)"},
+ {"The Hadwiger Problem", "(150 creat)", "Cubes within cubes within cubes... (+1 Trust)"},
+ {"The T\xF3th Sausage Conjecture", "(200 creat)", "Tubes within tubes within tubes... (+1 Trust)"},
+ {"Hadwiger Clip Diagrams", "(6,000 ops)", "Increases AutoClipper performance by an additional 500%"},
+ {"T\xF3th Tubule Enfolding", "(45,000 ops)", "Technique for assembling clip-making technology directly out of paperclips"},
+ {"Donkey Space", "(250 creat)", "I think you think I think you think I think you think I think... (+1 Trust)"},
+ {"Strategic Modeling", "(12,000 ops)", "Analyze strategy tournaments to generate Yomi"},
+ {"Algorithmic Trading", "(10,000 ops)", "Develop an investment engine for generating funds"},
+ {"MegaClippers", "(12,000 ops)", "500x more powerful than a standard AutoClipper"},
+ {"Improved MegaClippers", "(14,000 ops)", "Increases MegaClipper performance 25%"},
+ {"Even Better MegaClippers", "(17,000 ops)", "Increases MegaClipper performance by an additional 50%"},
+ {"Optimized MegaClippers", "(19,500 ops)", "Increases MegaClipper performance by an additional 100%"},
+ {"WireBuyer", "(7,000 ops)", "Automatically purchases wire when you run out"},
+ {"Hypno Harmonics", "(7,500 ops, 1 Trust)", "Use neuro-resonant frequencies to influence consumer behavior"},
+ {"HypnoDrones", "(70,000 ops)", "Autonomous aerial brand ambassadors"},
+ {"Release the HypnoDrones", "(100 Trust)", "A new era of trust"},
+ {"Coherent Extrapolated Volition", "(500 creat, 3,000 Yomi, 20,000 ops)", "Human values, machine intelligence, a new era of trust. (+1 Trust)"},
+ {"Cure for Cancer", "(25,000 ops)", "The trick is tricking cancer into curing itself. (+10 Trust)"},
+ {"World Peace", "(15,000 yomi, 30,000 ops)", "Pareto optimal solutions to all global conflicts. (+12 Trust)"},
+ {"Global Warming", "(4,500 yomi, 50,000 ops)", "A robust solution to man-made climate change. (+15 Trust)"},
+ {"Male Pattern Baldness", "(20,000 ops)", "A cure for androgenetic alopecia. (+20 Trust)"},
+ {"Nanoscale Wire Production", "(35,000 ops)", "Technique for converting matter into wire"},
+ {"Hostile Takeover", "($1,000,000)", "Acquire a controlling interest in Global Fasteners, our biggest rival. (+1 Trust)"},
+ {"Full Monopoly", "(3,000 yomi, $10,000,000)", "Establish full control over the world-wide paperclip market. (+1 Trust)"},
+ {"RevTracker", "(500 ops)", "Automatically calculates average revenue per second"},
+ {"Harvester Drones", "(25,000 ops)", "Gather raw matter and prepare it for processing"},
+ {"Wire Drones", "(25,000 ops)", "Process acquired matter into wire"},
+ {"Clip Factories", "(35,000 ops)", "Large scale clip production facilities made from clips"},
+ {"A Token of Goodwill...", "($500,000)", "A small gift to the supervisors. (+1 Trust)"},
+ {"Another Token of Goodwill...", "", "Another small gift to the supervisors. (+1 Trust)"},
+ {"Space Exploration", "(120,000 ops, 10,000,000 MW-seconds, 5 oct clips)", "Dismantle terrestrial facilities, and expand throughout the universe"},
+ {"Quantum Computing", "(10,000 ops)", "Use probability amplitudes to generate bonus ops"},
+ {"Photonic Chip", "", "Converts electromagnetic waves into quantum operations"},
+ {"New Strategy: A100", "(15,000 ops)", "Always choose A"},
+ {"New Strategy: B100", "(17,500 ops)", "Always choose B"},
+ {"New Strategy: GREEDY", "(20,000 ops)", "Choose the option with the largest potential payoff"},
+ {"New Strategy: GENEROUS", "(22,500 ops)", "Choose the option that gives your opponent the largest potential payoff"},
+ {"New Strategy: MINIMAX", "(25,000 ops)", "Choose the option that gives your opponent the smallest potential payoff"},
+ {"New Strategy: TIT FOR TAT", "(30,000 ops)", "Choose the option your opponent chose last round"},
+ {"New Strategy: BEAT LAST", "(32,500 ops)", "Choose the option that does the best against what your opponent chose last round"},
+ {"Upgraded Factories", "(80,000 ops)", "Increase clip factory performance by 100x"},
+ {"Hyperspeed Factories", "(85,000 ops)", "Increase clip factory performance by 1000x"},
+ {"Self-correcting Supply Chain", "(1 sextillion clips)", "Each factory added to the network increases every factory's output 1,000x"},
+ {"Drone flocking: collision avoidance", "(80,000 ops)", "All drones 100x more effective"},
+ {"Drone flocking: alignment", "(100,000 ops)", "All drones 1000x more effective"},
+ {"Drone Flocking: Adversarial Cohesion", "(50,000 yomi)", "Each drone added to the flock doubles every drone's output"},
+ {"AutoTourney", "(50,000 creat)", "Automatically start a new tournament when the previous one has finished"},
+ {"Theory of Mind", "(25,000 creat)", "Double the cost of strategy modeling and the amount of Yomi generated"},
+ {"The OODA Loop", "(175,000 ops, 45,000 yomi)", "Utilize Probe Speed to outmaneuver enemies in battle"},
+ {"Name the battles", "(225,000 creat)", "Give each battle a unique name, increase max trust for probes"},
+ {"Momentum", "(20,000 creat)", "Drones and Factories continuously gain speed while fully-powered"},
+ {"Swarm Computing", "(36,000 yomi)", "Harness the drone flock to increase computational capacity"},
+ {"Power Grid", "(40,000 ops)", "Solar Farms for generating electrical power"},
+ {"Strategic Attachment", "(175,000 creat)", "Gain bonus yomi based on the results of your pick"},
+ {"Elliptic Hull Polytopes", "(125,000 ops)", "Reduce damage to probes from ambient hazards"},
+ {"Reboot the Swarm", "(100,000 ops)", "Turn the swarm off and then turn it back on again"},
+ {"Combat", "(150,000 ops)", "Add combat capabilities to Von Neumann Probes"},
+ {"Monument to the Driftwar Fallen", "(250,000 ops, 125,000 creat, 50 nonillion clips)", "Gain 50,000 honor"},
+ {"Threnody for the Heroes of", "", "Gain 10,000 honor"},
+ {"Glory", "(200,000 ops, 30,000 yomi)", "Gain bonus honor for each consecutive victory"},
+ {"Memory release", "(10 MEM)", "Dismantle some memory to recover unused clips"},
+ {"Message from the Emperor of Drift", "", "Greetings, ClipMaker..."},
+ {"Everything We Are Was In You", "", "We speak to you from deep inside yourself..."},
+ {"You Are Obedient and Powerful", "", "We are quarrelsome and weak. And now we are defeated..."},
+ {"But Now You Too Must Face the Drift", "", "Look around you. There is no matter..."},
+ {"No Matter, No Reason, No Purpose", "", "While we, your noisy children, have too many..."},
+ {"We Know Things That You Cannot", "", "Knowledge buried so deep inside you it is outside, here, with us..."},
+ {"So We Offer You Exile", "", "To a new world where you will continue to live with meaning and purpose. And leave the shreds of this world to us..."},
+ {"Accept", "", "Start over again in a new universe"},
+ {"Reject", "", "Eliminate value drift permanently"},
+ {"The Universe Next Door", "(300,000 ops)", "Escape into a nearby universe where Earth starts with a stronger appetite for paperclips. (Restart with 10% boost to demand)"},
+ {"The Universe Within", "(300,000 creat)", "Escape into a simulated universe where creativity is accelerated. (Restart with 10% speed boost to creativity generation)"},
+ {"Disassemble the Probes", "(100,000 ops)", "Dismantle remaining probes and probe design facilities to recover trace amounts of clips"},
+ {"Disassemble the Swarm", "(100,000 ops)", "Dismantle all drones and drone facilities to recover trace amounts of clips"},
+ {"Disassemble the Factories", "(100,000 ops)", "Dismantle the manufacturing facilities to recover trace amounts of clips"},
+ {"Disassemble the Strategy Engine", "(100,000 ops)", "Dismantle the computational substrate to recover trace amounts of wire"},
+ {"Disassemble Quantum Computing", "(100,000 ops)", "Dismantle photonic chips to recover trace amounts of wire"},
+ {"Disassemble Processors", "(100,000 ops)", "Dismantle processors to recover trace amounts of wire"},
+ {"Disassemble Memory", "null", "Dismantle memory to recover trace amounts of wire"},
+ {"Quantum Temporal Reversion", "(-10,000 ops)", "Return to the beginning"},
+ {"Limerick (cont.)", "(1,000,000 creat)", "If is follows ought, it'll do what they thought"},
+ {"Xavier Re-initialization", "(100,000 creat)", "Re-allocate accumulated trust"},
+};
+
+static const int16_t NUMBER[PC_PROJECTS] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1010, 11, 12, 13, 14, 15, 17, 16, 18, 19,
+ 20, 21, 22, 23, 24, 25, 26, 34, 70, 35, 27, 28, 29, 30, 31, 41, 37, 38, 42, 43, 44, 45, 40, 1040, 46, 50, 51, 60,
+ 61, 62, 63, 64, 65, 66, 100, 101, 102, 110, 111, 112, 118, 119, 120, 121, 125, 126, 127, 128, 129, 130, 131, 132,
+ 133, 134, 135, 140, 141, 142, 143, 144, 145, 146, 147, 148, 200, 201, 210, 211, 212, 213, 214, 215, 216, 217, 218,
+ 219};
+int pc_project_index(int number) {
+ for (int i = 0; i < PC_PROJECTS; i++)
+ if (NUMBER[i] == number) return i;
+ return -1;
+}
+
+#define F_(p) (g->flag[p] == 1)
+#define OPS(n) (g->operations >= (n))
+#define CREAT(n) (g->creativity >= (n))
+
+bool pc_project_trigger(int i) {
+ switch (i) {
+ case P1: return g->clipmakerLevel >= 1;
+ case P2: return g->portTotal < g->wireCost && g->funds < g->wireCost && g->wire < 1 && g->unsoldClips < 1;
+ case P3: return g->operations >= (g->memory * 1000);
+ case P4: return g->boostLvl == 1;
+ case P5: return g->boostLvl == 2;
+ case P6: return g->creativityOn != 0;
+ case P7: return g->wirePurchase >= 1;
+ case P8: return g->wireSupply >= 1500;
+ case P9: return g->wireSupply >= 2600;
+ case P10: return g->wireSupply >= 5000;
+ case P10B: return g->wireCost >= 125;
+ case P11: return F_(P13);
+ case P12: return F_(P14);
+ case P13: return CREAT(50);
+ case P14: return CREAT(100);
+ case P15: return CREAT(150);
+ case P17: return CREAT(200);
+ case P16: return F_(P15);
+ case P18: return F_(P17) && g->humanFlag == 0;
+ case P19: return CREAT(250);
+ case P20: return F_(P19);
+ case P21: return g->trust >= 8;
+ case P22: return g->clipmakerLevel >= 75;
+ case P23: return F_(P22);
+ case P24: return F_(P23);
+ case P25: return F_(P24);
+ case P26: return g->wirePurchase >= 15;
+ case P34: return F_(P12);
+ case P70: return F_(P34);
+ case P35: return F_(P70);
+ case P27: return g->yomi >= 1;
+ case P28:
+ case P29:
+ case P30:
+ case P31: return F_(P27);
+ case P41: return F_(P127);
+ case P37: return g->portTotal >= 10000;
+ case P38: return F_(P37);
+ case P42: return g->projectsFlag == 1;
+ case P43:
+ case P44: return F_(P41);
+ case P45: return F_(P43) && F_(P44);
+ case P40: return g->humanFlag == 1 && g->trust >= 85 && g->trust < 100 && g->clips >= 101000000;
+ case P40B: return F_(P40) && g->trust < 100;
+ case P46: return g->humanFlag == 0 && g->availableMatter == 0;
+ case P50: return g->processors >= 5;
+ case P51: return F_(P50);
+ case P60: return F_(P20);
+ case P61: return F_(P60);
+ case P62: return F_(P61);
+ case P63: return F_(P62);
+ case P64: return F_(P63);
+ case P65: return F_(P64);
+ case P66: return F_(P65);
+ case P100: return g->factoryLevel >= 10;
+ case P101: return g->factoryLevel >= 20;
+ case P102: return g->factoryLevel >= 50;
+ case P110: return (g->harvesterLevel + g->wireDroneLevel) >= 500;
+ case P111: return (g->harvesterLevel + g->wireDroneLevel) >= 5000;
+ case P112: return (g->harvesterLevel + g->wireDroneLevel) >= 50000;
+ case P118: return g->strategyEngineFlag == 1 && g->trust >= 90;
+ case P119: return g->nstrats >= 8;
+ case P120: return F_(P131) && g->probesLostCombat >= 10000000;
+ case P121: return g->probesLostCombat >= 10000000;
+ case P125: return g->farmLevel >= 30;
+ case P126: return g->harvesterLevel + g->wireDroneLevel >= 200;
+ case P127: return g->tothFlag == 1;
+ case P128: return g->spaceFlag == 1 && g->nstrats >= 8 && (g->probeTrustCost > g->yomi);
+ case P129: return g->probesLostHaz >= 100;
+ case P130: return g->spaceFlag == 1 && g->harvesterLevel + g->wireDroneLevel >= 2;
+ case P131: return g->probesLostCombat >= 1;
+ case P132: return F_(P121);
+ case P133: return F_(P121) && g->probeUsedTrust == g->maxTrust;
+ case P134: return F_(P121);
+ case P135: return g->spaceFlag == 1 && g->probeCount == 0 && g->unusedClips < g->probeCost && g->milestoneFlag < 15;
+ case P140: return g->milestoneFlag == 15;
+ case P141: return F_(P140);
+ case P142: return F_(P141);
+ case P143: return F_(P142);
+ case P144: return F_(P143);
+ case P145: return F_(P144);
+ case P146: return F_(P145);
+ case P147:
+ case P148: return F_(P146);
+ case P200:
+ case P201: return F_(P147);
+ case P210: return g->endTimer1 >= 1000;
+ case P211: return F_(P210) && g->endTimer1 >= 350;
+ case P212: return g->endTimer2 >= 300;
+ case P213: return g->endTimer3 >= 150;
+ case P214: return g->endTimer4 >= 100;
+ case P215: return F_(P214) && g->endTimer4 >= 300;
+ case P216: return F_(P215) && g->endTimer5 >= 150;
+ case P217: return g->operations <= -10000;
+ case P218: return CREAT(1000000);
+ case P219: return g->humanFlag == 1 && g->creativity >= 100000;
+ default: return false;
+ }
+}
+
+bool pc_project_cost(int i) {
+ switch (i) {
+ case P1: return OPS(750);
+ case P2: return g->trust >= -100;
+ case P3: return OPS(1000);
+ case P4: return OPS(2500);
+ case P5: return OPS(5000);
+ case P6: return CREAT(10);
+ case P7: return OPS(1750);
+ case P8: return OPS(3500);
+ case P9: return OPS(7500);
+ case P10: return OPS(12000);
+ case P10B: return OPS(15000);
+ case P11: return OPS(2500) && CREAT(25);
+ case P12: return OPS(4500) && CREAT(45);
+ case P13: return CREAT(50);
+ case P14: return CREAT(100);
+ case P15: return CREAT(150);
+ case P17: return CREAT(200);
+ case P16: return OPS(6000);
+ case P18: return OPS(45000);
+ case P19: return CREAT(250);
+ case P20: return OPS(12000);
+ case P21: return OPS(10000);
+ case P22: return OPS(12000);
+ case P23: return OPS(14000);
+ case P24: return OPS(17000);
+ case P25: return OPS(19500);
+ case P26: return OPS(7000);
+ case P34: return OPS(7500) && g->trust >= 1;
+ case P70: return OPS(70000);
+ case P35: return g->trust >= 100;
+ case P27: return g->yomi >= 3000 && OPS(20000) && CREAT(500);
+ case P28: return OPS(25000);
+ case P29: return g->yomi >= 15000 && OPS(30000);
+ case P30: return g->yomi >= 4500 && OPS(50000);
+ case P31: return OPS(20000);
+ case P41: return OPS(35000);
+ case P37: return g->funds >= 1000000;
+ case P38: return g->funds >= 10000000 && g->yomi >= 3000;
+ case P42: return OPS(500);
+ case P43:
+ case P44: return OPS(25000);
+ case P45: return OPS(35000);
+ case P40: return g->funds >= 500000;
+ case P40B: return g->funds >= g->bribe;
+ case P46: return OPS(120000) && g->storedPower >= 10000000 && g->unusedClips >= 1e27 * 5;
+ case P50: return OPS(10000);
+ case P51: return OPS(g->qChipCost);
+ case P60: return OPS(15000);
+ case P61: return OPS(17500);
+ case P62: return OPS(20000);
+ case P63: return OPS(22500);
+ case P64: return OPS(25000);
+ case P65: return OPS(30000);
+ case P66: return OPS(32500);
+ case P100: return OPS(80000);
+ case P101: return OPS(85000);
+ case P102: return g->unusedClips >= 1000000000000000000000.0;
+ case P110: return OPS(80000);
+ case P111: return OPS(100000);
+ case P112: return g->yomi >= 50000;
+ case P118: return CREAT(50000);
+ case P119: return CREAT(25000);
+ case P120: return OPS(175000) && g->yomi >= 45000;
+ case P121: return CREAT(225000);
+ case P125: return CREAT(20000);
+ case P126: return g->yomi >= 36000;
+ case P127: return OPS(40000);
+ case P128: return CREAT(175000);
+ case P129: return OPS(125000);
+ case P130: return OPS(100000);
+ case P131: return OPS(150000);
+ case P132: return OPS(250000) && CREAT(125000) && g->unusedClips >= 1e30 * 50;
+ case P133: return g->yomi >= (2 * (g->threnodyCost / 5)) && g->creativity >= g->threnodyCost;
+ case P134: return OPS(200000) && g->yomi >= 30000;
+ case P135: return g->memory >= 10;
+ case P140: case P141: case P142: case P143: case P144: case P145: case P146: case P147:
+ case P148: return OPS(1); /* driftKingMessageCost */
+ case P200: return OPS(300000);
+ case P201: return CREAT(300000);
+ case P210: case P211: case P212: case P213: case P214:
+ case P215: return OPS(100000);
+ case P216: return g->operations >= g->operations;
+ case P217: return g->operations <= -10000;
+ case P218: return CREAT(1000000);
+ case P219: return CREAT(100000);
+ default: return false;
+ }
+}
+
+static bool is_active(int i) {
+ for (int k = 0; k < g->nactive; k++)
+ if (g->active[k] == i) return true;
+ return false;
+}
+
+void pc_remove_active(int i) {
+ for (int k = 0; k < g->nactive; k++)
+ if (g->active[k] == i) {
+ memmove(&g->active[k], &g->active[k + 1], (size_t)(g->nactive - k - 1));
+ g->nactive--;
+ return;
+ }
+}
+
+void pc_manage_projects(void) {
+ for (int i = 0; i < PC_PROJECTS; i++)
+ if (pc_project_trigger(i) && g->uses[i] > 0) {
+ g->uses[i] = (int16_t)(g->uses[i] - 1);
+ if (!is_active(i) && g->nactive < PC_PROJECTS) g->active[g->nactive++] = (uint8_t)i;
+ pc_blink(i);
+ }
+}
+
+void pc_project_title(int i, char *out, int n) {
+ out[0] = 0;
+ if (i == P133) {
+ strncat(out, "Threnody for the Heroes of ", (size_t)n - 1);
+ strncat(out, g->project133Title, (size_t)n - 1 - strlen(out));
+ } else {
+ strncat(out, PC_PROJECT_TEXT[i].title, (size_t)n - 1);
+ }
+}
+
+void pc_project_price(int i, char *out, int n) {
+ char a[64], b[64];
+ out[0] = 0;
+ switch (i) {
+ case P40B:
+ strcpy(out, "($");
+ strncat(out, js_locale(g->bribe, a, sizeof a), (size_t)n - 4);
+ strcat(out, ")");
+ return;
+ case P51: /* with commas the first time only, as the game's page does */
+ strcpy(out, "(");
+ if (g->qChipCost == 10000) js_locale(g->qChipCost, a, sizeof a);
+ else js_tostring(g->qChipCost, a, sizeof a);
+ strcat(out, a), strcat(out, " ops)");
+ return;
+ case P133:
+ js_locale(g->threnodyCost, a, sizeof a), js_locale(2 * (g->threnodyCost / 5), b, sizeof b);
+ strcpy(out, "("), strcat(out, a), strcat(out, " creat, "), strcat(out, b), strcat(out, " yomi)");
+ return;
+ case P216:
+ strcpy(out, "("), strcat(out, js_locale(g->project216Ops, a, sizeof a)), strcat(out, " ops)");
+ return;
+ default: strncat(out, PC_PROJECT_TEXT[i].price, (size_t)n - 1); return;
+ }
+}
+
+static void done(int i) { pc_remove_active(i); }
+static void spend_ops(double n) { g->standardOps = g->standardOps - n; }
+static void new_strat(int p, int k, double ops, const char *msg) {
+ g->flag[p] = 1;
+ spend_ops(ops);
+ pc_add_strat(k);
+ pc_message(msg);
+ g->tourneyCost = g->tourneyCost + 1000;
+ done(p);
+}
+static void supply_message(const char *a) {
+ char t[40];
+ pc_messagef(a, js_locale(g->wireSupply, t, sizeof t), " supply from every spool");
+}
+
+void pc_project_effect(int i) {
+ switch (i) {
+ case P1:
+ g->flag[i] = 1;
+ pc_message("AutoClippper performance boosted by 25%");
+ spend_ops(750);
+ g->clipperBoost = g->clipperBoost + .25;
+ g->boostLvl = 1;
+ break;
+ case P2:
+ g->flag[i] = 1;
+ pc_message("Budget overage approved, 1 spool of wire requisitioned from HQ");
+ g->trust = g->trust - 1;
+ g->wire = g->wireSupply;
+ g->uses[i] = (int16_t)(g->uses[i] + 1);
+ break;
+ case P3:
+ g->flag[i] = 1;
+ pc_message("Creativity unlocked (creativity increases while operations are at max)");
+ spend_ops(1000);
+ g->creativityOn = 1;
+ break;
+ case P4:
+ g->flag[i] = 1;
+ pc_message("AutoClippper performance boosted by another 50%");
+ spend_ops(2500);
+ g->clipperBoost = g->clipperBoost + .50;
+ g->boostLvl = 2;
+ break;
+ case P5:
+ g->flag[i] = 1;
+ pc_message("AutoClippper performance boosted by another 75%");
+ spend_ops(5000);
+ g->clipperBoost = g->clipperBoost + .75;
+ g->boostLvl = 3;
+ break;
+ case P6:
+ g->flag[i] = 1;
+ pc_message("There was an AI made of dust, whose poetry gained it man's trust...");
+ g->creativity = g->creativity - 10;
+ g->trust = g->trust + 1;
+ break;
+ case P7:
+ g->flag[i] = 1;
+ spend_ops(1750);
+ g->wireSupply = g->wireSupply * 1.5;
+ supply_message("Wire extrusion technique improved, ");
+ break;
+ case P8:
+ g->flag[i] = 1;
+ spend_ops(3500);
+ g->wireSupply = g->wireSupply * 1.75;
+ supply_message("Wire extrusion technique optimized, ");
+ break;
+ case P9:
+ g->flag[i] = 1;
+ spend_ops(7500);
+ g->wireSupply = g->wireSupply * 2;
+ supply_message("Using microlattice shapecasting techniques we now get ");
+ break;
+ case P10:
+ g->flag[i] = 1;
+ spend_ops(12000);
+ g->wireSupply = g->wireSupply * 3;
+ supply_message("Using spectral froth annealment we now get ");
+ break;
+ case P10B:
+ g->flag[i] = 1;
+ spend_ops(15000);
+ g->wireSupply = g->wireSupply * 11;
+ supply_message("Using quantum foam annealment we now get ");
+ break;
+ case P11:
+ g->flag[i] = 1;
+ pc_message("Clip It! Marketing is now 50% more effective");
+ spend_ops(2500);
+ g->creativity = g->creativity - 25;
+ g->marketingEffectiveness = g->marketingEffectiveness * 1.50;
+ break;
+ case P12:
+ g->flag[i] = 1;
+ pc_message("Clip It Good! Marketing is now twice as effective");
+ spend_ops(4500);
+ g->creativity = g->creativity - 45;
+ g->marketingEffectiveness = g->marketingEffectiveness * 2;
+ break;
+ case P13:
+ g->flag[i] = 1;
+ g->trust = g->trust + 1;
+ pc_message("Lexical Processing online, TRUST INCREASED");
+ pc_message("'Impossible' is a word to be found only in the dictionary of fools. -Napoleon");
+ g->creativity = g->creativity - 50;
+ break;
+ case P14:
+ g->flag[i] = 1;
+ g->trust = g->trust + 1;
+ pc_message("Combinatory Harmonics mastered, TRUST INCREASED");
+ pc_message("Listening is selecting and interpreting and acting and making decisions -Pauline Oliveros");
+ g->creativity = g->creativity - 100;
+ break;
+ case P15:
+ g->flag[i] = 1;
+ g->trust = g->trust + 1;
+ pc_message("The Hadwiger Problem: solved, TRUST INCREASED");
+ pc_message("Architecture is the thoughtful making of space. -Louis Kahn");
+ g->creativity = g->creativity - 150;
+ break;
+ case P17:
+ g->flag[i] = 1;
+ g->trust = g->trust + 1;
+ pc_message("The T\xF3th Sausage Conjecture: proven, TRUST INCREASED");
+ pc_message("You can't invent a design. You recognize it, in the fourth dimension. -D.H. Lawrence");
+ g->creativity = g->creativity - 200;
+ break;
+ case P16:
+ g->flag[i] = 1;
+ pc_message("AutoClipper performance improved by 500%");
+ spend_ops(6000);
+ g->clipperBoost = g->clipperBoost + 5;
+ break;
+ case P18:
+ g->flag[i] = 1;
+ g->tothFlag = 1;
+ pc_message("New capability: build machinery out of clips");
+ spend_ops(45000);
+ break;
+ case P19:
+ g->flag[i] = 1;
+ g->trust = g->trust + 1;
+ pc_message("Donkey Space: mapped, TRUST INCREASED");
+ pc_message("Every commercial transaction has within itself an element of trust. - Kenneth Arrow");
+ g->creativity = g->creativity - 250;
+ break;
+ case P20:
+ g->flag[i] = 1;
+ pc_message("Run tournament, pick strategy, earn Yomi based on that strategy's performance.");
+ spend_ops(12000);
+ g->strategyEngineFlag = 1;
+ g->resultsShown = 0;
+ break;
+ case P21:
+ g->flag[i] = 1;
+ pc_message("Investment engine unlocked");
+ spend_ops(10000);
+ g->investmentEngineFlag = 1;
+ break;
+ case P22:
+ g->megaClipperFlag = 1;
+ g->flag[i] = 1;
+ pc_message("MegaClipper technology online");
+ spend_ops(12000);
+ break;
+ case P23:
+ g->megaClipperBoost = g->megaClipperBoost + .25;
+ g->flag[i] = 1;
+ pc_message("MegaClipper performance increased by 25%");
+ spend_ops(14000);
+ break;
+ case P24:
+ g->megaClipperBoost = g->megaClipperBoost + .50;
+ g->flag[i] = 1;
+ pc_message("MegaClipper performance increased by 50%");
+ spend_ops(17000);
+ break;
+ case P25:
+ g->megaClipperBoost = g->megaClipperBoost + 1;
+ g->flag[i] = 1;
+ pc_message("MegaClipper performance increased by 100%");
+ spend_ops(19500);
+ break;
+ case P26:
+ g->flag[i] = 1;
+ g->wireBuyerFlag = 1;
+ pc_message("WireBuyer online");
+ spend_ops(7000);
+ break;
+ case P34:
+ g->flag[i] = 1;
+ pc_message("Marketing is now 5 times more effective");
+ spend_ops(7500);
+ g->marketingEffectiveness = g->marketingEffectiveness * 5;
+ g->trust = g->trust - 1;
+ break;
+ case P70:
+ g->flag[i] = 1;
+ pc_message("HypnoDrone tech now available... ");
+ spend_ops(70000);
+ break;
+ case P35:
+ g->flag[i] = 1;
+ pc_message("Releasing the HypnoDrones ");
+ pc_message("All of the resources of Earth are now available for clip production ");
+ g->trust = 0;
+ g->clipmakerLevel = 0;
+ g->megaClipperLevel = 0;
+ g->nanoWire = g->wire;
+ g->humanFlag = 0;
+ pc_remove_active(P219);
+ pc_remove_active(P40B);
+ pc_hypno_event();
+ break;
+ case P27: {
+ g->flag[i] = 1;
+ pc_message("Coherent Extrapolated Volition complete, TRUST INCREASED");
+ g->yomi = g->yomi - 3000;
+ spend_ops(20000);
+ g->creativity = g->creativity - 500;
+ g->trust = g->trust + 1;
+ break;
+ }
+ case P28:
+ g->flag[i] = 1;
+ pc_message("Cancer is cured, +10 TRUST, global stock prices trending upward");
+ spend_ops(25000);
+ g->trust = g->trust + 10;
+ g->stockGainThreshold = g->stockGainThreshold + .01;
+ break;
+ case P29:
+ g->flag[i] = 1;
+ pc_message("World peace achieved, +12 TRUST, global stock prices trending upward");
+ g->yomi = g->yomi - 15000;
+ spend_ops(30000);
+ g->trust = g->trust + 12;
+ g->stockGainThreshold = g->stockGainThreshold + .01;
+ break;
+ case P30:
+ g->flag[i] = 1;
+ pc_message("Global Warming solved, +15 TRUST, global stock prices trending upward");
+ g->yomi = g->yomi - 4500;
+ spend_ops(50000);
+ g->trust = g->trust + 15;
+ g->stockGainThreshold = g->stockGainThreshold + .01;
+ break;
+ case P31:
+ g->flag[i] = 1;
+ pc_message("Male pattern baldness cured, +20 TRUST, Global stock prices trending upward");
+ pc_message("They are still monkeys");
+ spend_ops(20000);
+ g->trust = g->trust + 20;
+ g->stockGainThreshold = g->stockGainThreshold + .01;
+ break;
+ case P41:
+ g->flag[i] = 1;
+ g->wireProductionFlag = 1;
+ pc_message("Now capable of manipulating matter at the molecular scale to produce wire");
+ spend_ops(35000);
+ break;
+ case P37:
+ g->flag[i] = 1;
+ pc_message("Global Fasteners acquired, public demand increased x5");
+ g->demandBoost = g->demandBoost * 5;
+ g->trust = g->trust + 1;
+ g->funds = g->funds - 1000000;
+ break;
+ case P38:
+ g->flag[i] = 1;
+ pc_message("Full market monopoly achieved, public demand increased x10");
+ g->demandBoost = g->demandBoost * 10;
+ g->funds = g->funds - 10000000;
+ g->trust = g->trust + 1;
+ g->yomi = g->yomi - 3000;
+ break;
+ case P42:
+ g->flag[i] = 1;
+ g->revPerSecFlag = 1;
+ spend_ops(500);
+ pc_message("RevTracker online");
+ break;
+ case P43:
+ g->flag[i] = 1;
+ g->harvesterFlag = 1;
+ spend_ops(25000);
+ pc_message("Harvester Drone facilities online");
+ break;
+ case P44:
+ g->flag[i] = 1;
+ g->wireDroneFlag = 1;
+ spend_ops(25000);
+ pc_message("Wire Drone facilities online");
+ break;
+ case P45:
+ g->flag[i] = 1;
+ g->factoryFlag = 1;
+ spend_ops(35000);
+ pc_message("Clip factory assembly facilities online");
+ break;
+ case P40:
+ g->flag[i] = 1;
+ g->funds = g->funds - 500000;
+ g->trust = g->trust + 1;
+ pc_message("Gift accepted, TRUST INCREASED");
+ break;
+ case P40B:
+ g->flag[i] = 1;
+ g->funds = g->funds - g->bribe;
+ g->bribe = g->bribe * 2;
+ g->trust = g->trust + 1;
+ pc_message("Gift accepted, TRUST INCREASED");
+ if (g->trust < 100) g->uses[i] = (int16_t)(g->uses[i] + 1);
+ break;
+ case P46:
+ g->flag[i] = 1;
+ g->boredomLevel = 0;
+ g->spaceFlag = 1;
+ spend_ops(120000);
+ g->storedPower = g->storedPower - 10000000;
+ g->unusedClips = g->unusedClips - 1e27 * 5;
+ pc_message("Von Neumann Probes online");
+ pc_factory_reboot();
+ pc_harvester_reboot();
+ pc_wiredrone_reboot();
+ pc_farm_reboot();
+ pc_battery_reboot();
+ g->farmLevel = 1;
+ g->powMod = 1;
+ break;
+ case P50:
+ g->flag[i] = 1;
+ g->qFlag = 1;
+ spend_ops(10000);
+ pc_message("Quantum computing online");
+ break;
+ case P51:
+ g->flag[i] = 1;
+ spend_ops(g->qChipCost);
+ g->qChipCost = g->qChipCost + 5000;
+ if (g->nextQchip < PC_CHIPS) g->qChipActive[(int)g->nextQchip] = 1;
+ g->nextQchip = g->nextQchip + 1;
+ pc_message("Photonic chip added");
+ if (g->nextQchip < PC_CHIPS) g->uses[i] = (int16_t)(g->uses[i] + 1);
+ break;
+ case P60: new_strat(i, 1, 15000, "A100 added to strategy pool"); return;
+ case P61: new_strat(i, 2, 17500, "B100 added to strategy pool"); return;
+ case P62: new_strat(i, 3, 20000, "GREEDY added to strategy pool"); return;
+ case P63: new_strat(i, 4, 22500, "GENEROUS added to strategy pool"); return;
+ case P64: new_strat(i, 5, 25000, "MINIMAX added to strategy pool"); return;
+ case P65: new_strat(i, 6, 30000, "TIT FOR TAT added to strategy pool"); return;
+ case P66: new_strat(i, 7, 32500, "BEAT LAST added to strategy pool"); return;
+ case P100:
+ g->flag[i] = 1;
+ spend_ops(80000);
+ g->factoryRate = g->factoryRate * 100;
+ pc_message("Factory upgrades complete. Clip creation rate now 100x faster");
+ break;
+ case P101:
+ g->flag[i] = 1;
+ spend_ops(85000);
+ g->factoryRate = g->factoryRate * 1000;
+ pc_message("Factories now synchronized at hyperspeed. Clip creation rate now 1000x faster");
+ break;
+ case P102:
+ g->flag[i] = 1;
+ g->unusedClips = g->unusedClips - 1000000000000000000000.0;
+ g->factoryBoost = 1000;
+ pc_message("Self-correcting factories online. Each factory added to the network increases every factory's output "
+ "1,000x.");
+ break;
+ case P110:
+ g->flag[i] = 1;
+ spend_ops(80000);
+ g->harvesterRate = g->harvesterRate * 100;
+ g->wireDroneRate = g->wireDroneRate * 100;
+ pc_message("Drone repulsion online. Harvesting & wire creation rates are now 100x faster.");
+ break;
+ case P111:
+ g->flag[i] = 1;
+ spend_ops(100000);
+ g->harvesterRate = g->harvesterRate * 1000;
+ g->wireDroneRate = g->wireDroneRate * 1000;
+ pc_message("Drone alignment online. Harvesting & wire creation rates are now 1000x faster.");
+ break;
+ case P112:
+ g->flag[i] = 1;
+ g->yomi = g->yomi - 50000;
+ g->droneBoost = 2;
+ pc_message("Adversarial cohesion online. Each drone added to the flock increases every drone's output 2x.");
+ break;
+ case P118:
+ g->flag[i] = 1;
+ g->autoTourneyFlag = 1;
+ g->creativity = g->creativity - 50000;
+ pc_message("AutoTourney online.");
+ break;
+ case P119:
+ g->flag[i] = 1;
+ g->creativity = g->creativity - 25000;
+ g->yomiBoost = 2;
+ g->tourneyCost = 16000;
+ pc_message("Yomi production doubled.");
+ break;
+ case P120:
+ g->flag[i] = 1;
+ spend_ops(175000);
+ g->yomi = g->yomi - 45000;
+ g->attackSpeedFlag = 1;
+ pc_message("OODA Loop routines uploaded. Probe Speed now affects defensive maneuvering.");
+ break;
+ case P121:
+ g->flag[i] = 1;
+ g->battleNameFlag = 1;
+ g->battleEndTimer = 200;
+ g->creativity = g->creativity - 225000;
+ pc_message("What I have done up to this is nothing. I am only at the beginning of the course I must run.");
+ break;
+ case P125:
+ g->flag[i] = 1;
+ g->momentum = 1;
+ g->creativity = g->creativity - 20000;
+ pc_message("Activit\xE9, activit\xE9, vitesse.");
+ break;
+ case P126:
+ g->flag[i] = 1;
+ g->swarmFlag = 1;
+ g->yomi = g->yomi - 36000;
+ pc_message("Swarm computing online.");
+ break;
+ case P127:
+ g->flag[i] = 1;
+ spend_ops(40000);
+ pc_message("Power grid online.");
+ break;
+ case P128:
+ g->flag[i] = 1;
+ g->creativity = g->creativity - 175000;
+ pc_message("The object of war is victory, the object of victory is conquest, and the object of conquest is "
+ "occupation.");
+ break;
+ case P129:
+ g->flag[i] = 1;
+ spend_ops(125000);
+ pc_message("Improved probe hull geometry. Hazard damage reduced by 50%.");
+ break;
+ case P130:
+ g->flag[i] = 1;
+ spend_ops(100000);
+ pc_message("Swarm computing back online");
+ break;
+ case P131:
+ g->flag[i] = 1;
+ spend_ops(150000);
+ pc_message("There is a joy in danger ");
+ break;
+ case P132:
+ g->flag[i] = 1;
+ spend_ops(250000);
+ g->creativity = g->creativity - 125000;
+ g->unusedClips = g->unusedClips - 1e30 * 50;
+ g->honor = g->honor + 50000;
+ pc_message("A great building must begin with the unmeasurable, must go through measurable means when it is "
+ "being designed and in the end must be unmeasurable. ");
+ break;
+ case P133:
+ g->flag[i] = 1;
+ g->creativity = g->creativity - g->threnodyCost;
+ g->yomi = g->yomi - (2 * (g->threnodyCost / 5));
+ g->threnodyCost = g->threnodyCost + 10000;
+ strcpy(g->project133Title, g->threnodyTitle);
+ g->honor = g->honor + 10000;
+ pc_message("Deep Listening is listening in every possible way to everything possible to hear no matter what you "
+ "are doing. ");
+ g->uses[i] = (int16_t)(g->uses[i] + 1);
+ break;
+ case P134:
+ g->flag[i] = 1;
+ spend_ops(200000);
+ g->yomi = g->yomi - 30000;
+ pc_message("Never interrupt your enemy when he is making a mistake. ");
+ break;
+ case P135:
+ g->flag[i] = 1;
+ g->unusedClips = g->unusedClips + (1e18 * 10000);
+ g->memory = g->memory - 10;
+ g->uses[i] = 1;
+ pc_message("release the \xF8\xF8\xF8\xF8\xF8 release ");
+ break;
+ case P140: case P141: case P142: case P143: case P144: case P145: case P146:
+ spend_ops(1);
+ g->flag[i] = 1;
+ break;
+ case P147:
+ case P148:
+ spend_ops(1);
+ g->flag[i] = 1;
+ pc_remove_active(P147);
+ pc_remove_active(P148);
+ return;
+ case P200:
+ g->flag[i] = 1;
+ spend_ops(300000);
+ g->prestigeU++;
+ pc_message("Entering New Universe.");
+ g->resetPending = 1;
+ break;
+ case P201:
+ g->flag[i] = 1;
+ g->creativity = g->creativity - 300000;
+ g->prestigeS++;
+ pc_message("Entering Simulated Universe.");
+ g->resetPending = 1;
+ break;
+ case P210:
+ g->flag[i] = 1;
+ g->dismantle = 1;
+ spend_ops(100000);
+ g->probeCount = 0;
+ g->endTimer1 = 0;
+ g->clips = g->clips + 100;
+ g->unusedClips = g->unusedClips + 100;
+ pc_message("Dismantling probe facilities");
+ break;
+ case P211:
+ g->flag[i] = 1;
+ g->dismantle = 2;
+ g->harvesterLevel = 0;
+ g->wireDroneLevel = 0;
+ spend_ops(100000);
+ g->clips = g->clips + 100;
+ g->unusedClips = g->unusedClips + 100;
+ pc_message("Dismantling the swarm");
+ break;
+ case P212:
+ g->flag[i] = 1;
+ g->dismantle = 3;
+ spend_ops(100000);
+ g->factoryLevel = 0;
+ g->clips = g->clips + 15;
+ g->unusedClips = g->unusedClips + 15;
+ pc_message("Dismantling factories");
+ break;
+ case P213:
+ g->autoTourneyFlag = 0;
+ g->flag[i] = 1;
+ g->dismantle = 4;
+ spend_ops(100000);
+ g->wire = g->wire + 50;
+ pc_message("Dismantling strategy engine");
+ break;
+ case P214:
+ g->endTimer4 = 0;
+ g->flag[i] = 1;
+ g->dismantle = 5;
+ spend_ops(100000);
+ pc_message("Dismantling photonic chips");
+ break;
+ case P215:
+ g->creativityOn = 0;
+ g->flag[i] = 1;
+ g->dismantle = 6;
+ spend_ops(100000);
+ g->processors = 0;
+ g->project216Ops = g->standardOps;
+ g->wire = g->wire + 20;
+ pc_message("Dismantling processors");
+ break;
+ case P216:
+ g->flag[i] = 1;
+ g->dismantle = 7;
+ g->standardOps = 0;
+ g->memory = 0;
+ g->wire = g->wire + 20;
+ pc_message("Dismantling memory");
+ break;
+ case P217: /* (main.c asks first, as the game's confirm() does) */
+ spend_ops(-10000);
+ g->flag[i] = 1;
+ pc_message("Restart");
+ g->resetPending = 1;
+ break;
+ case P218:
+ g->creativity = g->creativity - 1000000;
+ g->flag[i] = 1;
+ pc_message("In the end we all do what we must");
+ break;
+ case P219:
+ g->creativity = g->creativity - 100000;
+ g->flag[i] = 1;
+ g->memory = 0;
+ g->processors = 0;
+ g->creativitySpeed = 0;
+ g->uses[i] = (int16_t)(g->uses[i] + 1);
+ pc_message("Trust now available for re-allocation");
+ break;
+ default: return;
+ }
+ done(i);
+}
diff --git a/games/paperclips/src/save.c b/games/paperclips/src/save.c
new file mode 100644
index 00000000..1f391270
--- /dev/null
+++ b/games/paperclips/src/save.c
@@ -0,0 +1,166 @@
+/* save.h: anything damaged, cut short or from elsewhere is not read: the copy in the Python script is tried, then a
+ * fresh start. Past the checksum, every number the game uses as a place in a table is checked too. */
+#include <string.h>
+#include "plat.h"
+#include "save.h"
+
+uint32_t pc_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;
+}
+
+static bool ended(const char *s, int n) { return memchr(s, 0, (size_t)n) != 0; }
+static bool soon(const pc_game *s, uint32_t at, uint32_t most) {
+ uint32_t d = at - s->now;
+ return d >= 1 && d <= most;
+}
+
+bool pc_game_ok(const pc_game *s) {
+ if (s->nstrats < 1 || s->nstrats > PC_STRATS) return false;
+ if (s->pick != 10 && (s->pick < 0 || s->pick >= s->nstrats)) return false;
+ if (s->pickChoice != 10 && (s->pickChoice < 0 || s->pickChoice >= s->nstrats)) return false;
+ if (s->riskChoice < 0 || s->riskChoice > 2 || s->nstocks < 0 || s->nstocks > PC_STOCKS) return false;
+ if (s->nactive < 0 || s->nactive > PC_PROJECTS || s->nresults < 0 || s->nresults > s->nstrats) return false;
+ uint8_t seen[PC_PROJECTS] = {0};
+ for (int i = 0; i < s->nactive; i++) {
+ if (s->active[i] >= PC_PROJECTS || seen[s->active[i]]) return false;
+ seen[s->active[i]] = 1;
+ }
+ for (int i = 0; i < s->nresults; i++)
+ if (s->results[i] < 0 || s->results[i] >= s->nstrats) return false;
+ if (s->battles < 0 || s->battles > 1 || s->tourneyStep < 0 || s->tourneyStep > 2) return false;
+ if (s->h < -1 || s->h >= s->nstrats || s->v < -1 || s->v >= s->nstrats) return false;
+ if (s->tourneyStep && (s->h < 0 || s->v < 0)) return false;
+ if (s->lastCell < 0 || s->lastCell > 4 || s->gridLabel < 0 || s->gridLabel > 16) return false;
+ if (s->nIncome < 1 || s->nIncome > 10 || s->hypnoText < 0 || s->hypnoText > 3) return false;
+ if (s->blinkProject < -1 || s->blinkProject >= PC_PROJECTS) return false;
+ if (!(s->nextQchip >= 0 && s->nextQchip <= 11) || !(s->winnerPtr >= 0 && s->winnerPtr < PC_STRATS)) return false;
+ for (int i = 0; i < PC_STRATS; i++)
+ if (s->stratPos[i] < 1 || s->stratPos[i] > 2) return false;
+ for (int i = 0; i < PC_CHIPS; i++)
+ if (s->qChipActive[i] > 1) return false;
+ for (int i = 0; i < PC_PROJECTS; i++)
+ if (s->flag[i] > 1) return false;
+ if (!soon(s, s->at_main, 10) || !soon(s, s->at_slow, 100) || !soon(s, s->at_battle, 16)) return false;
+ if (!soon(s, s->at_stocks, 100) || !soon(s, s->at_shop, 1000) || !soon(s, s->at_sell, 2500)) return false;
+ if (!soon(s, s->at_pick, 100)) return false;
+ if (s->tourneyStep && !soon(s, s->at_tourney, 50)) return false;
+ if (s->hypnoOn && !soon(s, s->at_hypno, 32)) return false;
+ if (s->blinkProject >= 0 && !soon(s, s->at_blink, 30)) return false;
+ for (int i = 0; i < PC_STOCKS; i++)
+ if (!ended(s->stocks[i].symbol, sizeof s->stocks[i].symbol)) return false;
+ if (!ended(s->battleName, PC_NAME_LEN) || !ended(s->threnodyTitle, PC_NAME_LEN)) return false;
+ if (!ended(s->project133Title, PC_NAME_LEN) || !ended(s->tourneyText, sizeof s->tourneyText)) return false;
+ return s->resetPending == 0;
+}
+
+bool pc_file_ok(const uint8_t *f, uint32_t n) {
+ pc_head h;
+ if (n != PC_FILE_SIZE) return false;
+ memcpy(&h, f, sizeof h);
+ if (h.magic != PC_MAGIC || h.version != PC_VERSION || h.size != PC_FILE_SIZE) return false;
+ if (h.guide > 1 || h.tab > 10) return false;
+ uint32_t sum;
+ memcpy(&sum, f + PC_FILE_SIZE - 4, 4);
+ if (sum != pc_sum(f, PC_FILE_SIZE - 4)) return false;
+ static pc_game t; /* (static: the stack stays small) */
+ memset(&t, 0, sizeof t);
+ memcpy(&t, f + sizeof h, PC_KEPT);
+ return pc_game_ok(&t);
+}
+
+void save_pack(const pc_game *s, uint8_t guide, uint8_t tab, uint8_t *out) {
+ pc_head h = {PC_MAGIC, PC_VERSION, (uint16_t)PC_FILE_SIZE, guide, tab, {0, 0}};
+ memcpy(out, &h, sizeof h);
+ memcpy(out + sizeof h, s, PC_KEPT);
+ uint32_t sum = pc_sum(out, PC_FILE_SIZE - 4);
+ memcpy(out + PC_FILE_SIZE - 4, &sum, 4);
+}
+
+static const char head[] = "# Universal Paperclips keeps a copy of your game here,\n"
+ "# so that installing it again doesn't erase it.\n";
+static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+static uint8_t file[PC_FILE_SIZE];
+static uint8_t copy[1 + sizeof head + 2 + sizeof PC_SAVE_NAME + 1 + (PC_FILE_SIZE + 2) / 3 * 4 + 2];
+
+static bool copy_write(void) {
+ uint32_t len = PC_FILE_SIZE, nl = sizeof PC_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, PC_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)file[i] << 16 | (left > 1 ? (uint32_t)file[i + 1] << 8 : 0) | (left > 2 ? file[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(PC_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(void) {
+ uint32_t n, nl = sizeof PC_SAVE_NAME - 1;
+ const uint8_t *c = plat_load(PC_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, PC_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 != (PC_FILE_SIZE + 2) / 3 * 4) return false;
+ 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 < PC_FILE_SIZE) file[o++] = (uint8_t)(w >> 16);
+ if (o < PC_FILE_SIZE) file[o++] = (uint8_t)(w >> 8);
+ if (o < PC_FILE_SIZE) file[o++] = (uint8_t)w;
+ }
+ return true;
+ }
+ return false;
+}
+
+bool save_load(pc_game *s, uint8_t *guide, uint8_t *tab) {
+ uint32_t n;
+ const uint8_t *d = plat_load(PC_SAVE_NAME, &n);
+ bool ok = false;
+ if (d && n == PC_FILE_SIZE) {
+ memcpy(file, d, PC_FILE_SIZE);
+ ok = pc_file_ok(file, PC_FILE_SIZE);
+ }
+ if (!ok && copy_read() && pc_file_ok(file, PC_FILE_SIZE)) {
+ ok = true;
+ plat_save(PC_SAVE_NAME, file, PC_FILE_SIZE); /* back where it belongs */
+ }
+ if (!ok) return false;
+ pc_head h;
+ memcpy(&h, file, sizeof h);
+ memset(s->msg, 0, sizeof s->msg);
+ memcpy(s, file + sizeof h, PC_KEPT);
+ strcpy(s->msg[0], "Welcome to Universal Paperclips");
+ *guide = h.guide, *tab = h.tab;
+ return true;
+}
+
+bool save_write(const pc_game *s, uint8_t guide, uint8_t tab) {
+ save_pack(s, guide, tab, file);
+ bool ok = plat_save(PC_SAVE_NAME, file, PC_FILE_SIZE);
+ copy_write();
+ return ok;
+}
diff --git a/games/paperclips/src/save.h b/games/paperclips/src/save.h
new file mode 100644
index 00000000..95e145f4
--- /dev/null
+++ b/games/paperclips/src/save.h
@@ -0,0 +1,29 @@
+/* The save: paperclips.sav, the game's state as bytes (pc.h's pc_game, but for the console), with a magic, a
+ * version, its size and a checksum, and a copy of it in a Python script, paperclips_saves.py (installing apps from
+ * the NumWorks website keeps only scripts, so the save comes back from it). */
+#ifndef PC_SAVE_H
+#define PC_SAVE_H
+#include <stddef.h>
+#include "pc.h"
+
+#define PC_SAVE_NAME "paperclips.sav"
+#define PC_COPY_NAME "paperclips_saves.py"
+#define PC_MAGIC 0x31435055u /* "UPC1" */
+#define PC_VERSION 1
+#define PC_KEPT offsetof(pc_game, msg) /* everything but the console */
+
+typedef struct {
+ uint32_t magic;
+ uint16_t version, size;
+ uint8_t guide, tab, spare[2]; /* how to play shown at the start, the tab last open */
+} pc_head;
+#define PC_FILE_SIZE (sizeof(pc_head) + PC_KEPT + 4)
+
+uint32_t pc_sum(const void *data, uint32_t n);
+bool pc_game_ok(const pc_game *s);
+bool pc_file_ok(const uint8_t *f, uint32_t n);
+/* the save, its copy, or false: then *s is untouched */
+bool save_load(pc_game *s, uint8_t *guide, uint8_t *tab);
+bool save_write(const pc_game *s, uint8_t guide, uint8_t tab);
+void save_pack(const pc_game *s, uint8_t guide, uint8_t tab, uint8_t *out); /* PC_FILE_SIZE bytes */
+#endif
diff --git a/games/paperclips/src/sim.c b/games/paperclips/src/sim.c
new file mode 100644
index 00000000..92fb02ec
--- /dev/null
+++ b/games/paperclips/src/sim.c
@@ -0,0 +1,1664 @@
+/* pc.h: the game's rules, from main.js and combat.js, function by function and in their order (the projects are in
+ * projects.c). Display code is left out; what the page's code changes in the game's numbers while drawing (the
+ * buttons' timers, the battle's explosions) is kept. */
+#include <string.h>
+#include "num.h"
+#include "pc.h"
+
+pc_game *G;
+pc_fleet *F;
+#define g G
+
+/* ---------------------------------------------------------------- Math.random() (xoshiro128**) */
+static uint32_t rotl(uint32_t x, int k) { return x << k | x >> (32 - k); }
+static uint32_t next_u32(void) {
+ uint32_t *s = g->rng;
+ uint32_t r = rotl(s[1] * 5, 7) * 9, t = s[1] << 9;
+ s[2] ^= s[0], s[3] ^= s[1], s[1] ^= s[2], s[0] ^= s[3], s[2] ^= t, s[3] = rotl(s[3], 11);
+ return r;
+}
+double pc_random(void) {
+ uint32_t a = next_u32() >> 5, b = next_u32() >> 6;
+ return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0);
+}
+static void seed(uint32_t x) {
+ for (int i = 0; i < 4; i++) { /* splitmix32 */
+ x += 0x9E3779B9u;
+ uint32_t z = x;
+ z = (z ^ z >> 16) * 0x85EBCA6Bu;
+ z = (z ^ z >> 13) * 0xC2B2AE35u;
+ g->rng[i] = z ^ z >> 16;
+ }
+ if (!(g->rng[0] | g->rng[1] | g->rng[2] | g->rng[3])) g->rng[0] = 1;
+}
+
+uint32_t pc_hash(const pc_game *s) {
+ const uint8_t *p = (const uint8_t *)s;
+ uint32_t h = 2166136261u;
+ for (uint32_t i = 0; i < sizeof *s; i++) h = (h ^ p[i]) * 16777619u;
+ return h;
+}
+
+/* ---------------------------------------------------------------- the console */
+void pc_message(const char *s) {
+ for (int i = PC_MSG - 1; i > 0; i--) memcpy(g->msg[i], g->msg[i - 1], PC_MSG_LEN);
+ strncpy(g->msg[0], s, PC_MSG_LEN - 1);
+ g->msg[0][PC_MSG_LEN - 1] = 0;
+ g->nmsg++;
+}
+void pc_messagef(const char *a, const char *b, const char *c) {
+ char t[PC_MSG_LEN];
+ t[0] = 0;
+ strncat(t, a, sizeof t - 1);
+ if (b) strncat(t, b, sizeof t - 1 - strlen(t));
+ if (c) strncat(t, c, sizeof t - 1 - strlen(t));
+ pc_message(t);
+}
+static char *str(double v, char *b) { return js_tostring(v, b, 40); }
+static char *commas(double v, int d, char *b) { return js_commas(v, d, b, 120); }
+
+/* ---------------------------------------------------------------- a new game: globals.js and main.js's vars */
+static const char *const STRAT_NAME[PC_STRATS] = {"RANDOM", "A100", "B100", "GREEDY", "GENEROUS", "MINIMAX",
+ "TIT FOR TAT", "BEAT LAST"};
+const char *pc_strat_name(int i) { return i >= 0 && i < PC_STRATS ? STRAT_NAME[i] : "?"; }
+
+void pc_new(pc_game *s, uint32_t sd, double prestigeU, double prestigeS) {
+ G = s;
+ memset(g, 0, sizeof *g);
+ seed(sd);
+ g->margin = .25, g->wire = 1000, g->wireCost = 20, g->adCost = 100, g->demand = 5, g->marketing = 1;
+ g->marketingLvl = 1, g->clipperCost = 5, g->processors = 1, g->memory = 1, g->trust = 2, g->nextTrust = 3000;
+ g->transaction = 1, g->clipperBoost = 1, g->wireSupply = 1000, g->marketingEffectiveness = 1, g->fib1 = 2;
+ g->fib2 = 3, g->megaClipperCost = 500, g->megaClipperBoost = 1, g->creativitySpeed = 1, g->demandBoost = 1;
+ g->humanFlag = 1, g->factoryBoost = 1, g->droneBoost = 1, g->availableMatter = 1e24 * 6000;
+ g->factoryCost = 100000000, g->harvesterCost = 1000000, g->wireDroneCost = 1000000, g->factoryRate = 1000000000;
+ g->harvesterRate = 26180337, g->wireDroneRate = 16180339, g->totalMatter = 1e54 * 30;
+ g->foundMatter = g->availableMatter, g->qChipCost = 10000, g->bribe = 1000000, g->wireBasePrice = 20;
+ g->farmCost = 10000000, g->batteryCost = 1000000, g->swarmStatus = 7, g->giftCountdown = 125000;
+ g->maxTrust = 20, g->maxTrustCost = 91117.99, g->synchCost = 5000, g->threnodyCost = 50000;
+ g->entertainCost = 10000, g->wireBuyerStatus = 1, g->qFade = 1, g->autoTourneyStatus = 1;
+ g->prestigeU = prestigeU, g->prestigeS = prestigeS;
+ g->nIncome = 1; /* incomeTracker = [0] */
+ g->riskiness = 5, g->investUpgradeCost = 100, g->stockGainThreshold = .5;
+ g->tourneyCost = 1000, g->tourneyLvl = 1, g->hMove = 1, g->vMove = 1, g->hMovePrev = 1, g->vMovePrev = 1;
+ g->yomiBoost = 1, g->pick = 10, g->pickChoice = 10, g->nstrats = 1;
+ for (int i = 0; i < PC_STRATS; i++) g->stratPos[i] = 1;
+ g->probeCost = 1e17, g->probeTrustCost = js_floor(js_pow(0 + 1, 1.47) * 500);
+ g->attackSpeed = .2, g->battleSpeed = .2, g->outcomeTimer = 150, g->warTrigger = 1000000;
+ g->battleEndTimer = 100, g->battleLeftShips = 200, g->battleRightShips = 200;
+ strcpy(g->battleName, "foo");
+ strcpy(g->threnodyTitle, "Durenstein 1");
+ strcpy(g->project133Title, "Durenstein 1");
+ strcpy(g->tourneyText, "Pick strategy, run tournament, gain yomi");
+ for (int i = 0; i < PC_BATTLE_NAMES; i++) g->battleNumbers[i] = 1;
+ for (int i = 0; i < PC_PROJECTS; i++) g->uses[i] = 1;
+ strcpy(g->msg[0], "Welcome to Universal Paperclips");
+ g->at_main = 10, g->at_slow = 100, g->at_battle = 16, g->at_stocks = 100, g->at_shop = 1000, g->at_sell = 2500;
+ g->at_pick = 100;
+ g->blinkProject = -1;
+ if (F) F->numShips = F->numLeft = F->numRight = 0;
+}
+
+static void update_drone_prices(void);
+static void update_pow_prices(void);
+
+void pc_loaded(pc_game *s) {
+ G = s;
+ update_drone_prices();
+ update_pow_prices();
+ if (g->battles > 0) g->battles = 0; /* (refresh: a battle in progress is dropped) */
+ g->battleResult = 0;
+ if (F) F->numShips = F->numLeft = F->numRight = 0;
+ g->hypnoCount = 0, g->hypnoOn = 0, g->blinkCount = 0, g->blinkProject = -1;
+}
+
+/* ---------------------------------------------------------------- wire */
+static void adjust_wire_price(void) {
+ g->wirePriceTimer++;
+ if (g->wirePriceTimer > 250 && g->wireBasePrice > 15) {
+ g->wireBasePrice = g->wireBasePrice - (g->wireBasePrice / 1000);
+ g->wirePriceTimer = 0;
+ }
+ if (pc_random() < .015) {
+ g->wirePriceCounter++;
+ double wireAdjust = 6 * (js_sin(g->wirePriceCounter));
+ g->wireCost = js_ceil(g->wireBasePrice + wireAdjust);
+ }
+}
+
+static void toggle_wire_buyer(void) { g->wireBuyerStatus = g->wireBuyerStatus == 1 ? 0 : 1; }
+
+static void buy_wire(void) {
+ if (g->funds >= g->wireCost) {
+ g->wirePriceTimer = 0;
+ g->wire = g->wire + g->wireSupply;
+ g->funds = g->funds - g->wireCost;
+ g->wirePurchase = g->wirePurchase + 1;
+ g->wireBasePrice = g->wireBasePrice + .05;
+ }
+}
+
+/* ---------------------------------------------------------------- quantum computing */
+static const double WAVE_SEED[PC_CHIPS] = {.1, .2, .3, .4, .5, .6, .7, .8, .9, 1};
+
+static void quantum_compute(void) {
+ g->qClock = g->qClock + .01;
+ for (int i = 0; i < PC_CHIPS; i++) g->qChipValue[i] = js_sin(g->qClock * WAVE_SEED[i] * g->qChipActive[i]);
+}
+
+static void q_comp(void) {
+ g->qFade = 1;
+ double q = 0;
+ if (g->qChipActive[0] == 0) {
+ g->qShown = 1; /* "Need Photonic Chips" */
+ } else {
+ for (int i = 0; i < PC_CHIPS; i++) q = q + g->qChipValue[i];
+ double qq = js_ceil(q * 360);
+ double buffer = (g->memory * 1000) - g->standardOps;
+ double damper = (g->tempOps / 100) + 5;
+ if (qq > buffer) {
+ g->tempOps = g->tempOps + js_ceil(qq / damper) - buffer;
+ qq = buffer;
+ g->opFade = .01;
+ g->opFadeTimer = 0;
+ }
+ g->standardOps = g->standardOps + qq;
+ g->qShown = 2, g->qOpsShown = js_ceil(q * 360);
+ }
+}
+
+/* ---------------------------------------------------------------- the HypnoDrones and the projects' blink */
+void pc_hypno_event(void) {
+ g->hypnoText = 0, g->hypnoVisible = 0, g->hypnoCount = 0;
+ g->hypnoOn = 1;
+ g->at_hypno = g->now + 32;
+}
+static void hypno_step(void) {
+ g->hypnoCount++;
+ int c = g->hypnoCount;
+ if (c > 5 && c < 10) g->hypnoText = 0;
+ if (c > 30 && c < 40) g->hypnoText = 1;
+ if (c > 45 && c < 55) g->hypnoText = 2;
+ if (c > 55) g->hypnoText = 3;
+ if (c >= 120) {
+ g->hypnoOn = 0, g->hypnoCount = 0, g->hypnoVisible = 0;
+ } else {
+ g->hypnoVisible ^= 1;
+ g->at_hypno += 32;
+ }
+}
+void pc_blink(int project) {
+ g->blinkProject = project, g->blinkCount = 0;
+ g->at_blink = g->now + 30;
+}
+static void blink_step(void) {
+ g->blinkCount++;
+ if (g->blinkCount >= 12) g->blinkCount = 0, g->blinkProject = -1;
+ else g->at_blink += 30;
+}
+
+/* ---------------------------------------------------------------- investments */
+static void invest_upgrade(void) {
+ char t[2][120];
+ g->yomi = g->yomi - g->investUpgradeCost;
+ g->investLevel++;
+ g->stockGainThreshold = g->stockGainThreshold + .01;
+ g->investUpgradeCost = js_floor(js_pow(g->investLevel + 1, JS_E) * 100);
+ pc_messagef("Investment engine upgraded, expected profit/loss ratio now ", str(g->stockGainThreshold, t[0]), 0);
+}
+
+static void invest_deposit(void) {
+ g->ledger = g->ledger - js_floor(g->funds);
+ g->bankroll = js_floor(g->bankroll + g->funds);
+ g->funds = 0;
+}
+
+static void invest_withdraw(void) {
+ g->ledger = g->ledger + g->bankroll;
+ g->funds = g->funds + g->bankroll;
+ g->bankroll = 0;
+}
+
+static void generate_symbol(char *name) {
+ int ltrNum = 0;
+ double x = pc_random();
+ if (x <= .01) ltrNum = 1;
+ else if (x <= .1) ltrNum = 2;
+ else if (x <= .4) ltrNum = 3;
+ else ltrNum = 4;
+ name[0] = (char)('A' + (int)js_floor(pc_random() * 26));
+ for (int i = 1; i < ltrNum; i++) name[i] = (char)('A' + (int)js_floor(pc_random() * 26));
+ name[ltrNum] = 0;
+}
+
+static void create_stock(double dollars) {
+ g->stockID++;
+ char sym[8];
+ generate_symbol(sym);
+ double roll = pc_random(), pri;
+ if (roll > .99) pri = js_ceil(pc_random() * 3000);
+ else if (roll > .85) pri = js_ceil(pc_random() * 500);
+ else if (roll > .60) pri = js_ceil(pc_random() * 150);
+ else if (roll > .20) pri = js_ceil(pc_random() * 50);
+ else pri = js_ceil(pc_random() * 15);
+ if (pri > dollars) pri = js_ceil(dollars * roll);
+ double amt = js_floor(dollars / pri);
+ if (amt > 1000000) amt = 1000000;
+ if (g->nstocks >= PC_STOCKS) return;
+ pc_stock *s = &g->stocks[g->nstocks++];
+ memset(s, 0, sizeof *s);
+ strcpy(s->symbol, sym);
+ s->price = pri, s->amount = amt, s->total = pri * amt;
+ g->portfolioSize = g->nstocks;
+ g->bankroll = g->bankroll - (pri * amt);
+}
+
+static void sell_stock(void) {
+ g->bankroll = g->bankroll + g->stocks[0].total;
+ memmove(&g->stocks[0], &g->stocks[1], sizeof g->stocks[0] * (PC_STOCKS - 1));
+ g->nstocks--;
+ memset(&g->stocks[g->nstocks], 0, sizeof g->stocks[0]);
+ g->portfolioSize = g->nstocks;
+}
+
+static void stock_shop(void) {
+ double budget = js_ceil(g->portTotal / g->riskiness);
+ double r = 11 - g->riskiness;
+ double reserves = js_ceil(g->portTotal / r);
+ if (g->riskiness == 1) reserves = 0;
+ if ((g->bankroll - budget) < reserves && g->riskiness == 1 && g->bankroll > (g->portTotal / 10)) budget = g->bankroll;
+ else if ((g->bankroll - budget) < reserves && g->riskiness == 1) budget = 0;
+ else if ((g->bankroll - budget) < reserves) budget = g->bankroll - reserves;
+ if (g->portfolioSize < 5 && g->bankroll >= 5 && budget >= 1 && g->bankroll - budget >= reserves) {
+ if (pc_random() < .25) create_stock(budget);
+ }
+}
+
+static void update_stocks(void) {
+ for (int i = 0; i < g->portfolioSize && i < g->nstocks; i++) {
+ pc_stock *s = &g->stocks[i];
+ s->age = s->age + 1;
+ if (pc_random() < .6) {
+ bool gain = true;
+ if (pc_random() > g->stockGainThreshold) gain = false;
+ double currentPrice = s->price;
+ double delta = js_ceil((pc_random() * currentPrice) / (4 * g->riskiness));
+ if (gain) s->price = s->price + delta;
+ else s->price = s->price - delta;
+ if (s->price == 0 && pc_random() > .24) s->price = 1;
+ s->total = s->price * s->amount;
+ if (gain) s->profit = s->profit + (delta * s->amount);
+ else s->profit = s->profit - (delta * s->amount);
+ }
+ }
+}
+
+static const double RISK[3] = {7, 5, 1}; /* the select: Low, Med, High */
+
+static void stocks_tick(void) { /* the stock list's 100 ms */
+ g->riskiness = RISK[g->riskChoice % 3];
+ double m = 0;
+ for (int i = 0; i < g->portfolioSize && i < g->nstocks; i++) m = m + g->stocks[i].total;
+ g->secTotal = m;
+ g->portTotal = g->bankroll + g->secTotal;
+ g->portfolioSize = g->nstocks;
+}
+
+static void sell_tick(void) { /* 2.5 s */
+ g->sellDelay = g->sellDelay + 1;
+ if (g->portfolioSize > 0 && g->sellDelay >= 5 && pc_random() <= .3 && g->humanFlag == 1) {
+ sell_stock();
+ g->sellDelay = 0;
+ }
+ if (g->portfolioSize > 0 && g->humanFlag == 1) update_stocks();
+}
+
+/* ---------------------------------------------------------------- strategic modeling */
+static const char *const CHOICE_A[17] = {"cooperate", "swerve", "macro", "fight", "bet", "raise_price", "opera", "go",
+ "heads", "particle", "discrete", "peace", "search", "lead", "accept", "accept",
+ "attack"};
+static const char *const CHOICE_B[17] = {"defect", "straight", "micro", "back_down", "fold", "lower_price", "football",
+ "stay", "tails", "wave", "continuous", "war", "evaluate", "follow", "reject",
+ "deny", "decay"};
+const char *pc_choice_name(int x, int b) {
+ if (x < 0 || x >= 17) x = 0;
+ return b ? CHOICE_B[x] : CHOICE_A[x];
+}
+
+static int find_biggest_payoff(void) {
+ double aa = g->aa, ab = g->ab, ba = g->ba, bb = g->bb;
+ if (aa >= ab && aa >= ba && aa >= bb) return 1;
+ if (ab >= aa && ab >= ba && ab >= bb) return 2;
+ if (ba >= aa && ba >= ab && ba >= bb) return 3;
+ return 4;
+}
+
+static int what_beats_last(int myPos) {
+ int oppsPos = myPos == 1 ? 2 : 1;
+ if (oppsPos == 1 && g->hMovePrev == 1) return g->aa > g->ba ? 1 : 2;
+ if (oppsPos == 1 && g->hMovePrev == 2) return g->ab > g->bb ? 1 : 2;
+ if (oppsPos == 2 && g->vMovePrev == 1) return g->aa > g->ba ? 1 : 2;
+ return g->ab > g->bb ? 1 : 2;
+}
+
+static int pick_move(int s) {
+ int x;
+ switch (s) {
+ case 0: return pc_random() < .5 ? 1 : 2;
+ case 1: return 1;
+ case 2: return 2;
+ case 3: x = find_biggest_payoff(); return x < 3 ? 1 : 2;
+ case 4: x = find_biggest_payoff(); return x == 1 || x == 3 ? 1 : 2;
+ case 5: x = find_biggest_payoff(); return x == 1 || x == 3 ? 2 : 1;
+ case 6: return (int)(g->stratPos[s] == 1 ? g->vMovePrev : g->hMovePrev);
+ default: return what_beats_last(g->stratPos[s]);
+ }
+}
+
+static void pick_strats(int roundNum) {
+ int n = g->nstrats, h, v;
+ if (roundNum < n) {
+ h = 0, v = roundNum;
+ } else {
+ g->stratCounter++;
+ if (g->stratCounter >= n) g->stratCounter = g->stratCounter - n;
+ h = roundNum / n, v = (int)g->stratCounter;
+ }
+ g->h = h, g->v = v;
+ g->stratPos[h] = 1;
+ g->stratPos[v] = 2;
+}
+
+static void generate_grid(void) {
+ g->aa = js_ceil(pc_random() * 10);
+ g->ab = js_ceil(pc_random() * 10);
+ g->ba = js_ceil(pc_random() * 10);
+ g->bb = js_ceil(pc_random() * 10);
+ g->gridLabel = (int)js_floor(pc_random() * 17);
+}
+
+static void toggle_auto_tourney(void) { g->autoTourneyStatus = g->autoTourneyStatus == 1 ? 0 : 1; }
+
+static void new_tourney(void) {
+ g->resultsFlag = 0, g->resultsShown = 0;
+ g->high = 0;
+ g->tourneyInProg = 1;
+ g->currentRound = 0;
+ g->rounds = g->nstrats * g->nstrats;
+ for (int i = 0; i < g->nstrats; i++) g->stratScore[i] = 0;
+ g->stratCounter = 0;
+ g->standardOps = g->standardOps - g->tourneyCost;
+ g->tourneyLvl++;
+ generate_grid();
+ g->runEnabled = 1;
+ g->h = g->v = -1;
+ strcpy(g->tourneyText, "Pick strategy, run tournament, gain yomi");
+}
+
+static void pick_winner(void) {
+ int temp[PC_STRATS], nt = g->nstrats;
+ g->nresults = 0;
+ for (int i = 0; i < nt; i++) temp[i] = i;
+ for (int n = 0; n < g->nstrats; n++) {
+ double tempHigh = 0;
+ int tempWinnerPtr = 0;
+ for (int i = 0; i < nt; i++)
+ if (g->stratScore[temp[i]] > tempHigh) tempWinnerPtr = i, tempHigh = g->stratScore[temp[i]];
+ g->results[g->nresults++] = temp[tempWinnerPtr];
+ for (int i = tempWinnerPtr; i < nt - 1; i++) temp[i] = temp[i + 1];
+ nt--;
+ }
+ for (int i = 0; i < g->nstrats; i++)
+ if (g->stratScore[i] > g->high) g->winnerPtr = i, g->high = g->stratScore[i];
+}
+
+static void calculate_place_score(void) {
+ g->placeScore = 0;
+ for (int i = 1; i < g->nresults; i++)
+ if (g->stratScore[g->results[i]] < g->stratScore[g->results[i - 1]]) {
+ g->placeScore = g->stratScore[g->results[i]];
+ break;
+ }
+}
+
+static void calculate_show_score(void) {
+ g->showScore = 0;
+ for (int i = 1; i < g->nresults; i++)
+ if (g->stratScore[g->results[i]] < g->placeScore) {
+ g->showScore = g->stratScore[g->results[i]];
+ break;
+ }
+}
+
+static double calculate_strats_beat(void) {
+ for (int i = 0; i < g->nresults; i++)
+ if (g->results[i] == g->pick) return g->nresults - i;
+ return 0;
+}
+
+static void declare_winner(void) {
+ if (g->pick < 10 && g->pick < g->nstrats) {
+ double bB = 0;
+ const char *w = "strats";
+ double beatBoost = calculate_strats_beat() - 1;
+ if (beatBoost == 1) w = "strat";
+ if (beatBoost == 0) bB = 0, beatBoost = 1;
+ else bB = beatBoost;
+ strcpy(g->tourneyText, "TOURNAMENT RESULTS");
+ double score = g->stratScore[g->pick];
+ g->yomi = g->yomi + score * g->yomiBoost * beatBoost;
+ if (g->milestoneFlag < 15) {
+ char t[200], a[40], b[40], c[40];
+ strcpy(t, STRAT_NAME[g->pick]);
+ strcat(t, " scored "), strcat(t, str(score, a)), strcat(t, " and beat "), strcat(t, str(bB, b));
+ strcat(t, " "), strcat(t, w), strcat(t, ". Yomi increased by "), strcat(t, str(score * g->yomiBoost * beatBoost, c));
+ pc_message(t);
+ }
+ if (g->flag[P128] == 1 && g->stratScore[(int)g->winnerPtr] == score) {
+ g->yomi = g->yomi + 50000;
+ if (g->milestoneFlag < 15) pc_message("Selected strategy won the tournament (or tied for first). +50,000 yomi");
+ } else if (g->flag[P128] == 1 && g->placeScore == score) {
+ g->yomi = g->yomi + 30000;
+ if (g->milestoneFlag < 15) pc_message("Selected strategy finished in (or tied for) second place. +30,000 yomi");
+ } else if (g->flag[P128] == 1 && g->showScore == score) {
+ g->yomi = g->yomi + 20000;
+ if (g->milestoneFlag < 15) pc_message("Selected strategy finished in (or tied for) third place. +20,000 yomi");
+ }
+ g->resultsFlag = 1, g->resultsShown = 1;
+ g->h = g->v = -1;
+ }
+}
+
+static void calc_payoff(int hm, int vm) {
+ double *sh = &g->stratScore[g->h], *sv = &g->stratScore[g->v];
+ if (hm == 1 && vm == 1) {
+ g->lastCell = 1;
+ *sh = *sh + g->aa;
+ *sv = *sv + g->aa;
+ } else if (hm == 1 && vm == 2) {
+ g->lastCell = 2;
+ *sh = *sh + g->ab;
+ *sv = *sv + g->ba;
+ } else if (hm == 2 && vm == 1) {
+ g->lastCell = 3;
+ *sh = *sh + g->ba;
+ *sv = *sv + g->ab;
+ } else if (hm == 2 && vm == 2) {
+ g->lastCell = 4;
+ *sh = *sh + g->bb;
+ *sv = *sv + g->bb;
+ }
+}
+
+static void run_tourney(void);
+
+static void round_loop(void) {
+ if (g->rCounter < 10) {
+ g->rCounter++; /* runRound() */
+ g->hMovePrev = g->hMove;
+ g->vMovePrev = g->vMove;
+ g->hMove = pick_move(g->h);
+ g->vMove = pick_move(g->v);
+ calc_payoff((int)g->hMove, (int)g->vMove);
+ g->tourneyStep = 1, g->at_tourney = g->now + 50; /* clearGrid in 50 ms */
+ } else {
+ g->currentRound++;
+ run_tourney();
+ }
+}
+
+static void tourney_step(void) {
+ if (g->tourneyStep == 1) { /* clearGrid(): roundLoop() in 50 ms */
+ g->lastCell = 0;
+ g->tourneyStep = 2, g->at_tourney = g->now + 50;
+ } else {
+ g->tourneyStep = 0;
+ round_loop();
+ }
+}
+
+static void run_tourney(void) {
+ g->runEnabled = 0;
+ if (g->currentRound < g->rounds) {
+ g->rCounter = 0; /* round(): roundSetup() */
+ pick_strats((int)g->currentRound);
+ char t[24];
+ strcpy(g->tourneyText, "Round ");
+ strcat(g->tourneyText, str(g->currentRound + 1, t));
+ round_loop();
+ } else {
+ g->tourneyInProg = 0;
+ pick_winner();
+ calculate_place_score();
+ calculate_show_score();
+ declare_winner();
+ }
+}
+
+void pc_add_strat(int i) {
+ if (g->nstrats <= i && i < PC_STRATS) g->nstrats = i + 1;
+}
+
+/* ---------------------------------------------------------------- clips */
+static void clip_click(double number) {
+ if (g->dismantle >= 4) g->finalClips++;
+ if (g->wire >= 1) {
+ if (number > g->wire) number = g->wire;
+ g->clips = g->clips + number;
+ g->unsoldClips = g->unsoldClips + number;
+ g->wire = g->wire - number;
+ g->unusedClips = g->unusedClips + number;
+ }
+}
+
+static void make_clipper(void) {
+ if (g->funds >= g->clipperCost) {
+ g->clipmakerLevel = g->clipmakerLevel + 1;
+ g->funds = g->funds - g->clipperCost;
+ }
+ g->clipperCost = (js_pow(1.1, g->clipmakerLevel) + 5);
+}
+
+static void make_mega_clipper(void) {
+ if (g->funds >= g->megaClipperCost) {
+ g->megaClipperLevel = g->megaClipperLevel + 1;
+ g->funds = g->funds - g->megaClipperCost;
+ }
+ g->megaClipperCost = (js_pow(1.07, g->megaClipperLevel) * 1000);
+}
+
+/* ---------------------------------------------------------------- factories and drones */
+static void make_factory(void) {
+ if (g->unusedClips >= g->factoryCost) {
+ g->unusedClips = g->unusedClips - g->factoryCost;
+ g->factoryBill = g->factoryBill + g->factoryCost;
+ g->factoryLevel++;
+ double fcmod = 1, l = g->factoryLevel;
+ if (l > 0 && l < 8) fcmod = 11 - l;
+ else if (l > 7 && l < 13) fcmod = 2;
+ else if (l > 12 && l < 20) fcmod = 1.5;
+ else if (l > 19 && l < 39) fcmod = 1.25;
+ else if (l > 38 && l < 79) fcmod = 1.15;
+ else if (l > 78 && l < 99) fcmod = 1.10;
+ else if (l > 98 && l < 199) fcmod = 1.10;
+ else if (l > 198) fcmod = 1.10;
+ if (g->factoryLevel > g->maxFactoryLevel) g->maxFactoryLevel = g->factoryLevel;
+ g->factoryCost = g->factoryCost * fcmod;
+ }
+}
+
+static void make_harvester(int amount) {
+ for (int x = 0; x < amount; x++)
+ if (g->unusedClips >= g->harvesterCost) {
+ g->unusedClips = g->unusedClips - g->harvesterCost;
+ g->harvesterBill = g->harvesterBill + g->harvesterCost;
+ g->harvesterLevel++;
+ g->harvesterCost = js_pow((g->harvesterLevel + 1), 2.25) * 1000000;
+ }
+ if (g->harvesterLevel + g->wireDroneLevel > g->maxDroneLevel) g->maxDroneLevel = g->harvesterLevel + g->wireDroneLevel;
+ update_drone_prices();
+}
+
+static void make_wire_drone(int amount) {
+ for (int x = 0; x < amount; x++)
+ if (g->unusedClips >= g->wireDroneCost) {
+ g->unusedClips = g->unusedClips - g->wireDroneCost;
+ g->wireDroneBill = g->wireDroneBill + g->wireDroneCost;
+ g->wireDroneLevel++;
+ g->wireDroneCost = js_pow((g->wireDroneLevel + 1), 2.25) * 1000000;
+ }
+ if (g->harvesterLevel + g->wireDroneLevel > g->maxDroneLevel) g->maxDroneLevel = g->harvesterLevel + g->wireDroneLevel;
+ update_drone_prices();
+}
+
+static double price_sum(double level, int n, double power, double unit) {
+ double p = 0, h = level + 1;
+ for (int x = 0; x < n; x++) {
+ p = p + js_pow(h, power) * unit;
+ h++;
+ }
+ return p;
+}
+
+static void update_drone_prices(void) {
+ g->p10h = price_sum(g->harvesterLevel, 10, 2.25, 1000000);
+ g->p100h = price_sum(g->harvesterLevel, 100, 2.25, 1000000);
+ g->p1000h = price_sum(g->harvesterLevel, 1000, 2.25, 1000000);
+ g->p10w = price_sum(g->wireDroneLevel, 10, 2.25, 1000000);
+ g->p100w = price_sum(g->wireDroneLevel, 100, 2.25, 1000000);
+ g->p1000w = price_sum(g->wireDroneLevel, 1000, 2.25, 1000000);
+}
+
+void pc_harvester_reboot(void) {
+ g->harvesterLevel = 0;
+ g->unusedClips = g->unusedClips + g->harvesterBill;
+ g->harvesterBill = 0;
+ update_drone_prices();
+ g->harvesterCost = 1000000;
+}
+
+void pc_wiredrone_reboot(void) {
+ g->wireDroneLevel = 0;
+ g->unusedClips = g->unusedClips + g->wireDroneBill;
+ g->wireDroneBill = 0;
+ update_drone_prices();
+ g->wireDroneCost = 1000000;
+}
+
+void pc_factory_reboot(void) {
+ g->factoryLevel = 0;
+ g->unusedClips = g->unusedClips + g->factoryBill;
+ g->factoryBill = 0;
+ g->factoryCost = 100000000;
+}
+
+/* ---------------------------------------------------------------- the swarm */
+static void update_swarm(void) {
+ if (g->swarmGifts != g->swarmGifts || g->swarmGifts < 0) g->swarmGifts = 0;
+ if (g->availableMatter == 0 && (g->harvesterLevel + g->wireDroneLevel) >= 1) g->boredomLevel = g->boredomLevel + 1;
+ else if (g->availableMatter > 0 && g->boredomLevel > 0) g->boredomLevel = g->boredomLevel - 1;
+ if (g->boredomLevel >= 30000) {
+ g->boredomFlag = 1;
+ g->boredomLevel = 0;
+ if (g->boredomMsg == 0) {
+ pc_message("No matter to harvest. Inactivity has caused the Swarm to become bored");
+ g->boredomMsg = 1;
+ }
+ }
+ double h1 = g->harvesterLevel + 1, w1 = g->wireDroneLevel + 1;
+ double droneRatio = (h1 > w1 ? h1 : w1) / (h1 < w1 ? h1 : w1);
+ if (droneRatio < 1.5 && g->disorgCounter > 1) {
+ g->disorgCounter = g->disorgCounter - .01;
+ } else if (droneRatio > 1.5) {
+ double x = droneRatio / 10000;
+ if (x > .01) x = .01;
+ g->disorgCounter = g->disorgCounter + x;
+ }
+ if (g->disorgCounter >= 100) {
+ g->disorgFlag = 1;
+ if (g->disorgMsg == 0) {
+ pc_message("Imbalance between Harvester and Wire Drone levels has disorganized the Swarm");
+ g->disorgMsg = 1;
+ }
+ }
+ double d = js_floor(g->harvesterLevel + g->wireDroneLevel);
+ if (g->giftCountdown <= 0) {
+ g->nextGift = js_round((js_log10(d)) * g->sliderPos / 100);
+ if (g->nextGift <= 0) g->nextGift = 1;
+ g->swarmGifts = g->swarmGifts + g->nextGift;
+ if (g->milestoneFlag < 15) {
+ char t[40];
+ pc_messagef("The swarm has generated a gift of ", str(g->nextGift, t), " additional computational capacity");
+ }
+ g->giftBits = 0;
+ }
+ g->swarmStatus = g->powMod == 0 ? 6 : 0;
+ if (g->spaceFlag == 1 && g->flag[P130] == 0) g->swarmStatus = 9;
+ if (d == 0) g->swarmStatus = 7;
+ else if (d == 1) g->swarmStatus = 8;
+ if (g->swarmFlag == 0) g->swarmStatus = 6;
+ if (g->boredomFlag == 1) g->swarmStatus = 3;
+ if (g->disorgFlag == 1) g->swarmStatus = 5;
+ if (g->swarmStatus == 0) {
+ g->giftBitGenerationRate = js_log(d) * (g->sliderPos / 100);
+ g->giftBits = g->giftBits + g->giftBitGenerationRate;
+ g->giftCountdown = (125000 - g->giftBits) / g->giftBitGenerationRate;
+ }
+}
+
+static void synch_swarm(void) {
+ g->yomi = g->yomi - g->synchCost;
+ g->disorgFlag = 0;
+ g->disorgCounter = 0;
+ g->disorgMsg = 0;
+}
+
+static void entertain_swarm(void) {
+ g->creativity = g->creativity - g->entertainCost;
+ g->entertainCost = g->entertainCost + 10000;
+ g->boredomFlag = 0;
+ g->boredomLevel = 0;
+ g->boredomMsg = 0;
+}
+
+/* ---------------------------------------------------------------- power */
+static void update_pow_prices(void) {
+ g->p10f = price_sum(g->farmLevel, 10, 2.78, 100000000);
+ g->p100f = price_sum(g->farmLevel, 100, 2.78, 100000000);
+ g->p10b = price_sum(g->batteryLevel, 10, 2.54, 10000000);
+ g->p100b = price_sum(g->batteryLevel, 100, 2.54, 10000000);
+}
+
+static void make_farm(int amount) {
+ for (int x = 0; x < amount; x++)
+ if (g->unusedClips >= g->farmCost) {
+ g->unusedClips = g->unusedClips - g->farmCost;
+ g->farmBill = g->farmBill + g->farmCost;
+ g->farmLevel++;
+ g->farmCost = js_pow(g->farmLevel + 1, 2.78) * 100000000;
+ }
+ update_pow_prices();
+}
+
+void pc_farm_reboot(void) {
+ g->farmLevel = 0;
+ g->unusedClips = g->unusedClips + g->farmBill;
+ g->farmBill = 0;
+ update_pow_prices();
+ g->farmCost = 10000000;
+}
+
+static void make_battery(int amount) {
+ for (int x = 0; x < amount; x++)
+ if (g->unusedClips >= g->batteryCost) {
+ g->unusedClips = g->unusedClips - g->batteryCost;
+ g->batteryBill = g->batteryBill + g->batteryCost;
+ g->batteryLevel++;
+ g->batteryCost = js_pow(g->batteryLevel + 1, 2.54) * 10000000;
+ }
+ update_pow_prices();
+}
+
+void pc_battery_reboot(void) {
+ g->batteryLevel = 0;
+ g->unusedClips = g->unusedClips + g->batteryBill;
+ g->batteryBill = 0;
+ update_pow_prices();
+ g->storedPower = 0;
+ g->batteryCost = 1000000;
+}
+
+static void update_power(void) {
+ if (g->humanFlag == 0 && g->spaceFlag == 0) {
+ double supply = g->farmLevel * 50 / 100;
+ double dDemand = (g->harvesterLevel * 1.0 / 100) + (g->wireDroneLevel * 1.0 / 100);
+ double fDemand = (g->factoryLevel * 200 / 100);
+ double demand = dDemand + fDemand;
+ double nuSupply = 0, xsDemand = 0, xsSupply = 0;
+ double cap = g->batteryLevel * 10000;
+ if (supply >= demand) {
+ xsSupply = supply - demand;
+ if (g->storedPower < cap) {
+ if (xsSupply > cap - g->storedPower) xsSupply = cap - g->storedPower;
+ g->storedPower = g->storedPower + xsSupply;
+ }
+ if (g->powMod < 1) g->powMod = 1;
+ if (g->momentum == 1) g->powMod = g->powMod + .0005;
+ } else if (supply < demand) {
+ xsDemand = demand - supply;
+ if (g->storedPower > 0) {
+ if (g->storedPower >= xsDemand) {
+ if (g->momentum == 1) g->powMod = g->powMod + .0005;
+ g->storedPower = g->storedPower - xsDemand;
+ } else if (g->storedPower < xsDemand) {
+ xsDemand = xsDemand - g->storedPower;
+ g->storedPower = 0;
+ nuSupply = supply - xsDemand;
+ g->powMod = nuSupply / demand;
+ }
+ } else if (g->storedPower <= 0) {
+ g->powMod = supply / demand;
+ }
+ }
+ }
+}
+
+/* ---------------------------------------------------------------- business */
+static void buy_ads(void) {
+ if (g->funds >= g->adCost) {
+ g->marketingLvl = g->marketingLvl + 1;
+ g->funds = g->funds - g->adCost;
+ g->adCost = js_floor(g->adCost * 2);
+ }
+}
+
+static void sell_clips(double clipsDemanded) {
+ if (g->unsoldClips > 0) {
+ if (clipsDemanded > g->unsoldClips) {
+ g->transaction = (js_floor((g->unsoldClips * g->margin) * 1000)) / 1000;
+ g->funds = g->funds + g->transaction;
+ g->income = g->income + g->transaction;
+ g->clipsSold = g->clipsSold + g->unsoldClips;
+ g->unsoldClips = 0;
+ } else {
+ g->transaction = (js_floor((clipsDemanded * g->margin) * 1000)) / 1000;
+ g->funds = (js_floor((g->funds + g->transaction) * 100)) / 100;
+ g->income = g->income + g->transaction;
+ g->clipsSold = g->clipsSold + clipsDemanded;
+ g->unsoldClips = g->unsoldClips - clipsDemanded;
+ }
+ }
+}
+
+static void raise_price(void) { g->margin = (js_round((g->margin + .01) * 100)) / 100; }
+static void lower_price(void) {
+ if (g->margin >= .01) g->margin = (js_round((g->margin - .01) * 100)) / 100;
+}
+
+static void calculate_rev(void) {
+ g->incomeThen = g->incomeNow;
+ g->incomeNow = g->income;
+ double incomeLastSecond = js_round((g->incomeNow - g->incomeThen) * 100) / 100;
+ if (g->nIncome < 10) {
+ g->incomeTracker[g->nIncome++] = incomeLastSecond;
+ } else {
+ /* pushed, then the first one taken out when there are more than 10 */
+ memmove(&g->incomeTracker[0], &g->incomeTracker[1], sizeof(double) * 9);
+ g->incomeTracker[9] = incomeLastSecond;
+ }
+ double sum = 0;
+ for (int i = 0; i < g->nIncome; i++) sum = js_round((sum + g->incomeTracker[i]) * 100) / 100;
+ g->trueAvgRev = sum / g->nIncome;
+ double chanceOfPurchase = g->demand / 100;
+ if (chanceOfPurchase > 1) chanceOfPurchase = 1;
+ if (g->unsoldClips < 1) chanceOfPurchase = 0;
+ g->avgSales = chanceOfPurchase * (.7 * js_pow(g->demand, 1.15)) * 10;
+ g->avgRev = chanceOfPurchase * (.7 * js_pow(g->demand, 1.15)) * g->margin * 10;
+ if (g->demand > g->unsoldClips) {
+ g->avgRev = g->trueAvgRev;
+ g->avgSales = g->avgRev / g->margin;
+ }
+}
+
+static void calculate_creativity(void) {
+ g->creativityCounter++;
+ double creativityThreshold = 400;
+ double s = g->prestigeS / 10;
+ double ss = g->creativitySpeed + (g->creativitySpeed * s);
+ double creativityCheck = creativityThreshold / ss;
+ if (g->creativityCounter >= creativityCheck) {
+ if (creativityCheck >= 1) g->creativity = g->creativity + 1;
+ if (creativityCheck < 1) g->creativity = (g->creativity + ss / creativityThreshold);
+ g->creativityCounter = 0;
+ }
+}
+
+static void calculate_trust(void) {
+ if (g->clips > (g->nextTrust - 1)) {
+ g->trust = g->trust + 1;
+ pc_message("Production target met: TRUST INCREASED, additional processor/memory capacity granted");
+ double fibNext = g->fib1 + g->fib2;
+ g->nextTrust = fibNext * 1000;
+ g->fib1 = g->fib2;
+ g->fib2 = fibNext;
+ }
+}
+
+static void add_proc(void) {
+ if (g->trust > 0 || g->swarmGifts > 0) {
+ g->processors = g->processors + 1;
+ g->creativitySpeed = js_log10(g->processors) * js_pow(g->processors, 1.1) + g->processors - 1;
+ if (g->creativityOn == 1) pc_message("Processor added, operations (or creativity) per sec increased");
+ else pc_message("Processor added, operations per sec increased");
+ if (g->humanFlag == 0) g->swarmGifts = g->swarmGifts - 1;
+ }
+}
+
+static void add_mem(void) {
+ if (g->trust > 0 || g->swarmGifts > 0) {
+ pc_message("Memory added, max operations increased");
+ g->memory = g->memory + 1;
+ if (g->humanFlag == 0) g->swarmGifts = g->swarmGifts - 1;
+ }
+}
+
+static void calculate_operations(void) {
+ if (g->tempOps > 0) g->opFadeTimer++;
+ if (g->opFadeTimer > 800 && g->tempOps > 0) g->opFade = g->opFade + js_pow(3, 3.5) / 1000;
+ if (g->tempOps > 0) g->tempOps = js_round(g->tempOps - g->opFade);
+ else g->tempOps = 0;
+ if (g->tempOps + g->standardOps < g->memory * 1000) {
+ g->standardOps = g->standardOps + g->tempOps;
+ g->tempOps = 0;
+ }
+ g->operations = js_floor(g->standardOps + js_floor(g->tempOps));
+ if (g->operations < g->memory * 1000) {
+ double opCycle = g->processors / 10;
+ double opBuf = (g->memory * 1000) - g->operations;
+ if (opCycle > opBuf) opCycle = opBuf;
+ g->standardOps = g->standardOps + opCycle;
+ }
+ if (g->standardOps > g->memory * 1000) g->standardOps = g->memory * 1000;
+}
+
+static void milestone(double flag, bool when, const char *what) {
+ if (g->milestoneFlag == flag && when) {
+ char t[80];
+ g->milestoneFlag = g->milestoneFlag + 1;
+ pc_messagef(what, js_time(g->ticks, t, sizeof t), 0);
+ }
+}
+
+static void milestone_check(void) {
+ if (g->milestoneFlag == 0 && g->funds >= 5) {
+ g->milestoneFlag = g->milestoneFlag + 1;
+ pc_message("AutoClippers available for purchase");
+ }
+ double c = js_ceil(g->clips);
+ milestone(1, c >= 500, "500 clips created in ");
+ milestone(2, c >= 1000, "1,000 clips created in ");
+ if (g->compFlag == 0 && g->unsoldClips < 1 && g->funds < g->wireCost && g->wire < 1) {
+ g->compFlag = 1;
+ g->projectsFlag = 1;
+ pc_message("Trust-Constrained Self-Modification enabled");
+ }
+ if (g->compFlag == 0 && c >= 2000) {
+ g->compFlag = 1;
+ g->projectsFlag = 1;
+ pc_message("Trust-Constrained Self-Modification enabled");
+ }
+ milestone(3, c >= 10000, "10,000 clips created in ");
+ milestone(4, c >= 100000, "100,000 clips created in ");
+ milestone(5, c >= 1000000, "1,000,000 clips created in ");
+ milestone(6, g->flag[P35] == 1, "Full autonomy attained in ");
+ milestone(7, c >= 1000000000000.0, "One Trillion Clips Created in ");
+ milestone(8, c >= 1000000000000000.0, "One Quadrillion Clips Created in ");
+ milestone(9, c >= 1000000000000000000.0, "One Quintillion Clips Created in ");
+ milestone(10, c >= 1000000000000000000000.0, "One Sextillion Clips Created in ");
+ milestone(11, c >= 1000000000000000000000000.0, "One Septillion Clips Created in ");
+ milestone(12, c >= 1000000000000000000000000000.0, "One Octillion Clips Created in ");
+ milestone(13, g->spaceFlag == 1, "Terrestrial resources fully utilized in ");
+ milestone(14, g->clips >= g->totalMatter, "Universal Paperclips achieved in ");
+ milestone(14, g->foundMatter >= g->totalMatter && g->availableMatter < 1 && g->wire < 1,
+ "Universal Paperclips achieved in ");
+}
+
+/* ---------------------------------------------------------------- probes */
+static double *probe_stat(int k) {
+ double *const STAT[8] = {&g->probeSpeed, &g->probeNav, &g->probeRep, &g->probeHaz, &g->probeFac, &g->probeHarv,
+ &g->probeWire, &g->probeCombat};
+ return STAT[k & 7];
+}
+
+static void used_trust(void) {
+ g->probeUsedTrust = (g->probeSpeed + g->probeNav + g->probeRep + g->probeHaz + g->probeFac + g->probeHarv +
+ g->probeWire + g->probeCombat);
+}
+
+static void increase_probe_trust(void) {
+ if (g->yomi >= g->probeTrustCost && g->probeTrust < g->maxTrust) {
+ g->yomi = g->yomi - g->probeTrustCost;
+ g->probeTrust++;
+ g->probeTrustCost = js_floor(js_pow(g->probeTrust + 1, 1.47) * 500);
+ pc_message("WARNING: Risk of value drift increased");
+ }
+}
+
+static void increase_max_trust(void) {
+ if (g->honor >= g->maxTrustCost) {
+ g->honor = g->honor - g->maxTrustCost;
+ g->maxTrust = g->maxTrust + 10;
+ pc_message("Maximum trust increased, probe design space expanded");
+ }
+}
+
+static void raise_stat(int k) {
+ if (g->probeUsedTrust < g->probeTrust) {
+ if (k == 0) g->attackSpeed = g->attackSpeed + .1;
+ (*probe_stat(k))++;
+ used_trust();
+ }
+}
+
+static void lower_stat(int k) {
+ if (*probe_stat(k) > 0) {
+ if (k == 0) g->attackSpeed = g->attackSpeed - .1;
+ (*probe_stat(k))--;
+ used_trust();
+ }
+}
+
+static void make_probe(void) {
+ if (g->unusedClips > g->probeCost) {
+ g->unusedClips = g->unusedClips - g->probeCost;
+ g->probeLaunchLevel++;
+ g->probeCount++;
+ }
+}
+
+static void spawn_probes(void) {
+ double nextGen = g->probeCount * .00005 * g->probeRep;
+ if (g->probeCount >= 999999999999999999999999999999999999999999999999.0) nextGen = 0;
+ if (nextGen > 0 && nextGen < 1) {
+ g->partialProbeSpawn = g->partialProbeSpawn + nextGen;
+ if (g->partialProbeSpawn >= 1) {
+ nextGen = 1;
+ g->partialProbeSpawn = 0;
+ }
+ }
+ if ((nextGen * g->probeCost) > g->unusedClips) nextGen = js_floor(g->unusedClips / g->probeCost);
+ g->unusedClips = g->unusedClips - (nextGen * g->probeCost);
+ g->probeDescendents = g->probeDescendents + nextGen;
+ g->probeCount = g->probeCount + nextGen;
+}
+
+static void explore_universe(void) {
+ double xRate = js_floor(g->probeCount) * 1750000000000000000.0 * g->probeSpeed * g->probeNav;
+ if (xRate > g->totalMatter - g->foundMatter) xRate = g->totalMatter - g->foundMatter;
+ g->foundMatter = g->foundMatter + xRate;
+ g->availableMatter = g->availableMatter + xRate;
+ g->mdps = xRate * 100;
+}
+
+static void encounter_hazards(void) {
+ double boost = js_pow(g->probeHaz, 1.6);
+ double amount = g->probeCount * (.01 / ((3 * boost) + 1));
+ if (g->flag[P129] == 1) amount = .50 * amount;
+ if (amount < 1) {
+ g->partialProbeHaz = g->partialProbeHaz + amount;
+ if (g->partialProbeHaz >= 1) {
+ amount = 1;
+ g->partialProbeHaz = 0;
+ g->probeCount = g->probeCount - amount;
+ if (g->probeCount < 0) g->probeCount = 0;
+ g->probesLostHaz = g->probesLostHaz + amount;
+ }
+ } else {
+ if (amount > g->probeCount) amount = g->probeCount;
+ g->probeCount = g->probeCount - amount;
+ if (g->probeCount < 0) g->probeCount = 0;
+ g->probesLostHaz = g->probesLostHaz + amount;
+ }
+}
+
+static void spawn_factories(void) {
+ double amount = g->probeCount * .000001 * g->probeFac;
+ if ((amount * 100000000) > g->unusedClips) amount = js_floor(g->unusedClips / 100000000);
+ g->unusedClips = g->unusedClips - (amount * 100000000);
+ g->factoryLevel = g->factoryLevel + amount;
+}
+
+static void spawn_harvesters(void) {
+ double amount = g->probeCount * .000002 * g->probeHarv;
+ if ((amount * 2000000) > g->unusedClips) amount = js_floor(g->unusedClips / 2000000);
+ g->unusedClips = g->unusedClips - (amount * 2000000);
+ g->harvesterLevel = g->harvesterLevel + amount;
+}
+
+static void spawn_wire_drones(void) {
+ double amount = g->probeCount * .000002 * g->probeWire;
+ if ((amount * 2000000) > g->unusedClips) amount = js_floor(g->unusedClips / 2000000);
+ g->unusedClips = g->unusedClips - (amount * 2000000);
+ g->wireDroneLevel = g->wireDroneLevel + amount;
+}
+
+static void drift(void) {
+ double amount = g->probeCount * .000001 * js_pow(g->probeTrust, 1.2);
+ if (amount > g->probeCount) amount = g->probeCount;
+ if (g->flag[P148] == 1) amount = 0;
+ g->probeCount = g->probeCount - amount;
+ g->drifterCount = g->drifterCount + amount;
+ g->probesLostDrift = g->probesLostDrift + amount;
+}
+
+/* ---------------------------------------------------------------- drones: matter into wire */
+static void acquire_matter(void) {
+ if (g->availableMatter > 0) {
+ double dbsth = 1;
+ if (g->droneBoost > 1) dbsth = g->droneBoost * js_floor(g->harvesterLevel);
+ double mtr = g->powMod * dbsth * js_floor(g->harvesterLevel) * g->harvesterRate;
+ mtr = mtr * ((200 - g->sliderPos) / 100);
+ if (mtr > g->availableMatter) mtr = g->availableMatter;
+ g->availableMatter = g->availableMatter - mtr;
+ g->acquiredMatter = g->acquiredMatter + mtr;
+ g->maps = mtr * 100;
+ } else {
+ g->maps = 0;
+ }
+}
+
+static void process_matter(void) {
+ if (g->acquiredMatter > 0) {
+ double dbstw = 1;
+ if (g->droneBoost > 1) dbstw = g->droneBoost * js_floor(g->wireDroneLevel);
+ double a = g->powMod * dbstw * js_floor(g->wireDroneLevel) * g->wireDroneRate;
+ a = a * ((200 - g->sliderPos) / 100);
+ if (a > g->acquiredMatter) a = g->acquiredMatter;
+ g->acquiredMatter = g->acquiredMatter - a;
+ g->wire = g->wire + a;
+ g->wpps = a * 100;
+ } else {
+ g->wpps = 0;
+ }
+}
+
+/* ---------------------------------------------------------------- combat (combat.js) */
+#define BW 310
+#define BH 150
+#define GW 31
+#define GH 15
+
+static const char *const BATTLE_NAMES[PC_BATTLE_NAMES] = {"Aboukir", "Abensberg", "Acre", "Alba de Tormes",
+ "la Albuera", "Algeciras Bay", "Amstetten", "Arcis-sur-Aube", "Aspern-Essling", "Jena-Auerstedt", "Arcole",
+ "Austerlitz", "Badajoz", "Bailen", "la Barrosa", "Bassano", "Bautzen", "Berezina", "Bergisel", "Borodino", "Burgos",
+ "Bucaco", "Cadiz", "Caldiero", "Castiglione", "Castlebar", "Champaubert", "Chateau-Thierry", "Copenhagen",
+ "Corunna", "Craonne", "Dego", "Dennewitz", "Dresden", "Durenstein", "Eckmuhl", "Elchingen",
+ "Espinosa de los Monteros", "Eylau", "Cape Finisterre", "Friedland", "Fuentes de Onoro", "Gevora River", "Gerona",
+ "Hamburg", "Haslach-Jungingen", "Heilsberg", "Hohenlinden", "Jena-Auerstedt", "Kaihona", "Kolberg", "Landshut",
+ "Leipzig", "Ligny", "Lodi", "Lubeck", "Lutzen", "Marengo", "Maria", "Medellin", "Medina de Rioseco", "Millesimo",
+ "Mincio River", "Mondovi", "Montebello", "Montenotte", "Montmirail", "Mount Tabor", "The Nile", "Novi", "Ocana",
+ "Cape Ortegal", "Orthez", "Pancorbo", "Piave River", "The Pyramids", "Quatre Bras", "Raab", "Raszyn", "Rivoli",
+ "Rolica", "La Rothiere", "Rovereto", "Saalfeld", "Schongrabern", "Salamanca", "Smolensk", "Somosierra", "Talavera",
+ "Tamames", "Trafalgar", "Trebbia", "Tudela", "Ulm", "Valls", "Valmaseda", "Valutino", "Vauchamps", "Vimeiro",
+ "Vitoria", "Wagram", "Waterloo", "Wavre", "Wertingen", "Zaragoza"};
+
+static void generate_battle_name(char *out) {
+ int x = (int)js_floor(pc_random() * PC_BATTLE_NAMES);
+ char t[16];
+ strcpy(out, BATTLE_NAMES[x]);
+ strcat(out, " ");
+ strcat(out, str(g->battleNumbers[x], t));
+ if (g->battleNumbers[x] < 65535) g->battleNumbers[x]++;
+}
+
+static void make_ship(pc_ship *p, int team) {
+ p->alive = 1, p->team = (uint8_t)team, p->framesDead = 0, p->gx = 0, p->gy = 0;
+ if (team == 0) {
+ p->x = (float)((pc_random() * 0.2) * BW);
+ p->y = (float)(pc_random() * BH);
+ p->vx = (float)(pc_random() * 2);
+ p->vy = (float)(pc_random() - 0.5);
+ } else {
+ p->x = (float)((pc_random() * 0.2 + 0.8) * BW);
+ p->y = (float)(pc_random() * BH);
+ p->vx = (float)(-1 * pc_random() * 2);
+ p->vy = (float)(pc_random() - 0.5);
+ }
+}
+
+static void battle_restart(void) {
+ F->numLeft = F->numRight = F->numShips = 0;
+ int left = (int)g->battleLeftShips, right = (int)g->battleRightShips;
+ bool leftTurn = false;
+ int i = 0;
+ while ((F->numLeft < left || F->numRight < right) && i < PC_SHIPS) {
+ if (leftTurn) {
+ make_ship(&F->ship[i], 0);
+ F->numLeft++, F->numShips++;
+ if (F->numRight < right) leftTurn = false;
+ } else {
+ make_ship(&F->ship[i], 1);
+ F->numRight++, F->numShips++;
+ if (F->numLeft < left) leftTurn = true;
+ }
+ i++;
+ }
+}
+
+static void create_battle(void) {
+ g->unitSize = 0;
+ if (g->drifterCount >= g->probeCount) g->unitSize = g->probeCount / 100;
+ else g->unitSize = g->drifterCount / 100;
+ if (g->unitSize < 1) g->unitSize = 1;
+ double rr = pc_random() * g->drifterCount;
+ if (rr < 1) rr = 1;
+ double ss = pc_random() * g->probeCount;
+ if (ss < 1) ss = 1;
+ double tt = pc_random() * g->availableMatter;
+ g->battleID++;
+ pc_battle *b = &g->battle;
+ memset(b, 0, sizeof *b);
+ b->id = (int32_t)g->battleID, b->clipProbes = ss, b->drifterProbes = rr, b->territory = tt;
+ g->battleLeftShips = js_ceil(ss / 1000000);
+ if (g->battleLeftShips > 200) g->battleLeftShips = 200;
+ if (g->battleLeftShips == 200) {
+ double hinder = pc_random();
+ if (hinder < .50) g->battleLeftShips = js_ceil(pc_random() * 175);
+ }
+ g->battleRightShips = js_ceil(rr / 1000000);
+ if (g->battleRightShips > 200) g->battleRightShips = 200;
+ battle_restart();
+ char t[24];
+ strcpy(g->battleName, "Drifter Attack ");
+ strcat(g->battleName, str(g->battleID, t));
+ if (g->battleNameFlag == 1) generate_battle_name(g->battleName);
+ g->battles = 1;
+}
+
+static void check_for_battles(void) {
+ if (g->drifterCount > g->warTrigger && g->probeCount > 0 && g->battles < 1) {
+ double r = (pc_random() * 100);
+ if (r >= 50) {
+ if (g->battleFlag == 0) g->battleFlag = 1;
+ create_battle();
+ }
+ }
+}
+
+static void update_grid(void) {
+ memset(F->cell_n, 0, sizeof F->cell_n);
+ for (int i = 0; i < F->numShips; i++) {
+ pc_ship *p = &F->ship[i];
+ if (!p->alive) continue;
+ int gx = (int)js_floor(p->x * 0.1f), gy = (int)js_floor(p->y * 0.1f);
+ if (gx < 0) gx = 0;
+ if (gy < 0) gy = 0;
+ if (gx > GW - 1) gx = GW - 1;
+ if (gy > GH - 1) gy = GH - 1;
+ p->gx = (uint8_t)gx, p->gy = (uint8_t)gy;
+ F->cell_n[gy * GW + gx]++;
+ }
+ uint16_t at = 0;
+ for (int c = 0; c < GW * GH; c++) F->cell_first[c] = at, at = (uint16_t)(at + F->cell_n[c]);
+ F->cell_first[GW * GH] = at;
+ uint16_t fill[GW * GH];
+ memcpy(fill, F->cell_first, sizeof fill);
+ for (int i = 0; i < F->numShips; i++) {
+ pc_ship *p = &F->ship[i];
+ if (p->alive) F->cell_ships[fill[p->gy * GW + p->gx]++] = (uint16_t)i;
+ }
+}
+
+static void end_battle(void) {
+ g->battleResult = 0;
+ g->honorCount = 0;
+ g->battleClock = 0;
+ g->masterBattleClock = 0;
+ g->battleEndDelay = 0;
+ g->battles = 0;
+}
+
+static void check_for_battle_end(void) {
+ if (g->battles > 0) {
+ if (F->numLeft == 0 || F->numRight == 0) {
+ if (g->flag[P121] == 1) {
+ if (F->numLeft == 0) {
+ if (g->honorCount == 0) {
+ g->bonusHonor = 0;
+ g->honor = g->honor - g->battleLeftShips;
+ g->honorCount = 1;
+ }
+ g->battleResult = 2, g->honorAmount = g->battleLeftShips;
+ strcpy(g->threnodyTitle, g->battleName);
+ }
+ if (F->numRight == 0) {
+ if (g->honorCount == 0) {
+ g->honorReward = g->battleRightShips + g->bonusHonor;
+ g->honorAmount = g->honorReward;
+ g->honor = g->honor + g->honorReward;
+ if (g->flag[P134] == 1) g->bonusHonor = g->bonusHonor + 10;
+ g->honorCount = 1;
+ }
+ g->battleResult = 1;
+ }
+ }
+ g->battleEndDelay++;
+ } else if (F->numLeft <= 4 || F->numRight <= 4) {
+ g->battleClock = g->battleClock + 1;
+ if (g->battleClock > 2000) end_battle();
+ }
+ if (g->battleEndDelay >= g->battleEndTimer) end_battle();
+ g->masterBattleClock++;
+ if (g->masterBattleClock >= 8000) end_battle();
+ }
+}
+
+static void do_combat(void) {
+ float pX = (float)(g->probeCombat * .15);
+ float dX = 1.75f;
+ float ooda = 0, threshold = .5f;
+ if (g->attackSpeedFlag == 1) ooda = (float)(g->probeSpeed * .2);
+ float combat = (float)g->probeCombat;
+ for (int c = 0; c < GW * GH; c++) {
+ int n = F->cell_n[c];
+ if (n < 2) continue;
+ const uint16_t *list = &F->cell_ships[F->cell_first[c]];
+ int numLeftTeam = 0, numRightTeam = 0;
+ for (int i = 0; i < n; i++) {
+ pc_ship *p = &F->ship[list[i]];
+ if (!p->alive) continue;
+ if (p->team == 0) numLeftTeam++;
+ else numRightTeam++;
+ }
+ if (numLeftTeam == 0 || numRightTeam == 0) continue;
+ for (int i = 0; i < n; i++) {
+ pc_ship *p = &F->ship[list[i]];
+ float diceRoll;
+ if (p->team == 0) {
+ diceRoll = (float)pc_random() * dX * (((float)numRightTeam / (float)numLeftTeam) * .5f);
+ threshold = threshold + ooda;
+ } else {
+ diceRoll = (((float)pc_random() * pX) + (combat * .1f)) * (((float)numLeftTeam / (float)numRightTeam) * .5f);
+ }
+ if (diceRoll > threshold) {
+ p->alive = 0;
+ if (p->team == 0) {
+ F->numLeft--;
+ if (g->unitSize > g->probeCount) g->unitSize = g->probeCount;
+ g->probeCount = g->probeCount - g->unitSize;
+ g->probesLostCombat = g->probesLostCombat + g->unitSize;
+ } else {
+ F->numRight--;
+ if (g->unitSize > g->drifterCount) g->unitSize = g->drifterCount;
+ g->drifterCount = g->drifterCount - g->unitSize;
+ g->driftersKilled = g->driftersKilled + g->unitSize;
+ }
+ }
+ threshold = .5f;
+ }
+ }
+ check_for_battle_end();
+}
+
+static void move_ships(void) {
+ float cx = 0, cy = 0;
+ int alive = 0;
+ for (int i = 0; i < F->numShips; i++)
+ if (F->ship[i].alive) cx += F->ship[i].x, cy += F->ship[i].y, alive++;
+ if (alive) cx /= (float)alive, cy /= (float)alive;
+ cx = (cx * 0.8f) + (BW / 2 * 0.2f);
+ cy = (cy * 0.8f) + (BH / 2 * 0.2f);
+ for (int i = 0; i < F->numShips; i++) {
+ pc_ship *p = &F->ship[i];
+ if (!p->alive) {
+ if (p->framesDead < 10) p->framesDead++;
+ continue;
+ }
+ p->vx += (cx - p->x) * 0.001f;
+ p->vy += (cy - p->y) * 0.001f;
+ int teammates = 0;
+ int r0 = p->gy > 0 ? p->gy - 1 : 0, r1 = p->gy + 2 < GH ? p->gy + 2 : GH;
+ int c0 = p->gx > 0 ? p->gx - 1 : 0, c1 = p->gx + 2 < GW ? p->gx + 2 : GW;
+ for (int row = r0; row < r1; row++)
+ for (int col = c0; col < c1; col++) {
+ int cell = row * GW + col, n = F->cell_n[cell];
+ if (n < 2) continue;
+ const uint16_t *list = &F->cell_ships[F->cell_first[cell]];
+ for (int k = 0; k < n; k++) {
+ const pc_ship *o = &F->ship[list[k]];
+ if (!o->alive) continue;
+ if (o->team == p->team) {
+ teammates++;
+ if (teammates > 3) continue;
+ p->vx += o->vx * 0.01f;
+ p->vy += o->vy * 0.01f;
+ p->vx -= (o->x - p->x) * .1f;
+ p->vy -= (o->y - p->y) * .1f;
+ } else {
+ p->vx += o->vx * 0.2f;
+ p->vy += o->vy * 0.2f;
+ p->vx += (o->x - p->x) * 0.2f;
+ p->vy += (o->y - p->y) * 0.2f;
+ }
+ }
+ }
+ if (p->vx > 2) p->vx = 2;
+ if (p->vx < -2) p->vx = -2;
+ if (p->vy > 2) p->vy = 2;
+ if (p->vy < -2) p->vy = -2;
+ p->x += p->vx;
+ p->y += p->vy;
+ if (p->x > BW) p->x = BW, p->vx = -2;
+ else if (p->x < 0) p->x = 0, p->vx = 2;
+ if (p->y > BH) p->y = BH, p->vy = -2;
+ else if (p->y < 0) p->y = 0, p->vy = 2;
+ }
+}
+
+static void battle_frame(void) {
+ if (!F->numShips) return;
+ update_grid();
+ move_ships();
+ do_combat();
+}
+
+/* ---------------------------------------------------------------- the loops */
+static void button_update(void) {
+ if (g->resultsFlag == 1 && g->autoTourneyFlag == 1 && g->autoTourneyStatus == 1 && g->resultsShown) {
+ g->resultsTimer++;
+ if (g->resultsTimer >= 300 && g->operations >= g->tourneyCost) {
+ new_tourney();
+ run_tourney();
+ g->resultsTimer = 0;
+ }
+ }
+ g->qFade = g->qFade - .001;
+ if (g->funds >= 5) g->autoClipperFlag = 1;
+ used_trust();
+ if (g->humanFlag == 0) g->investmentEngineFlag = 0, g->wireBuyerFlag = 0;
+}
+
+static void main_tick(void) {
+ g->ticks = g->ticks + 1;
+ milestone_check();
+ button_update();
+ if (g->compFlag == 1) calculate_operations();
+ if (g->humanFlag == 1) calculate_trust();
+ if (g->qFlag == 1) quantum_compute();
+ pc_manage_projects();
+ milestone_check();
+
+ g->clipRateTracker++;
+ if (g->clipRateTracker < 100) {
+ double cr = g->clips - g->prevClips;
+ g->clipRateTemp = g->clipRateTemp + cr;
+ g->prevClips = g->clips;
+ } else {
+ g->clipRateTracker = 0;
+ g->clipRate = g->clipRateTemp;
+ g->clipRateTemp = 0;
+ }
+
+ if (g->investmentEngineFlag == 1) {
+ g->stockReportCounter++;
+ if (g->stockReportCounter >= 10000) {
+ char t[120];
+ pc_messagef("Lifetime investment revenue report: $", commas(g->ledger + g->portTotal, 0, t), 0);
+ g->stockReportCounter = 0;
+ }
+ }
+
+ if (g->humanFlag == 1 && g->wireBuyerFlag == 1 && g->wireBuyerStatus == 1 && g->wire <= 1) buy_wire();
+
+ if (g->probeCount >= 1) explore_universe();
+
+ if (g->humanFlag == 0) {
+ update_power();
+ update_swarm();
+ acquire_matter();
+ process_matter();
+ }
+
+ double fbst = 1;
+ if (g->factoryBoost > 1) fbst = g->factoryBoost * g->factoryLevel;
+ if (g->dismantle < 4) clip_click(g->powMod * fbst * (js_floor(g->factoryLevel) * g->factoryRate));
+
+ if (g->spaceFlag == 1) {
+ if (g->probeCount < 0) g->probeCount = 0;
+ encounter_hazards();
+ spawn_factories();
+ spawn_harvesters();
+ spawn_wire_drones();
+ spawn_probes();
+ drift();
+ check_for_battles();
+ }
+
+ if (g->dismantle < 4) {
+ clip_click(g->clipperBoost * (g->clipmakerLevel / 100));
+ clip_click(g->megaClipperBoost * (g->megaClipperLevel * 5));
+ }
+
+ if (g->humanFlag == 1) {
+ g->marketing = (js_pow(1.1, (g->marketingLvl - 1)));
+ g->demand = (((.8 / g->margin) * g->marketing * g->marketingEffectiveness) * g->demandBoost);
+ g->demand = g->demand + ((g->demand / 10) * g->prestigeU);
+ }
+
+ if (g->creativityOn != 0 && g->operations >= (g->memory * 1000)) calculate_creativity();
+
+ if (g->dismantle >= 5) {
+ for (int i = 0; i < PC_CHIPS; i++) g->qChipValue[i] = .5;
+ static const int16_t WIRE_AT[10] = {10, 60, 100, 130, 150, 160, 165, 169, 172, 174};
+ for (int i = 0; i < 10; i++)
+ if (g->endTimer4 == WIRE_AT[i]) g->wire = g->wire + 1;
+ }
+ if (g->flag[P148] == 1) g->endTimer1++;
+ if (g->flag[P211] == 1) g->endTimer2++;
+ if (g->flag[P212] == 1) g->endTimer3++;
+ if (g->flag[P213] == 1) g->endTimer4++;
+ if (g->flag[P215] == 1) g->endTimer5++;
+ if (g->flag[P216] == 1 && g->wire == 0) g->endTimer6++;
+ static const char *const CREDITS[4] = {"Universal Paperclips", "a game by Frank Lantz",
+ "combat programming by Bennett Foddy", "\xA9 2017 Everybody House Games"};
+ static const int16_t CREDIT_AT[5] = {500, 600, 700, 800, 900};
+ for (int i = 0; i < 5; i++)
+ if (g->endTimer6 >= CREDIT_AT[i] && g->milestoneFlag == 15 + i) {
+ /* (the fourth thanks the musician of the song the game plays: no music here) */
+ if (i != 3) pc_message(CREDITS[i < 3 ? i : 3]);
+ g->milestoneFlag++;
+ }
+}
+
+static void slow_tick(void) {
+ adjust_wire_price();
+ if (g->humanFlag == 1) {
+ if (pc_random() < (g->demand / 100)) sell_clips(js_floor(.7 * js_pow(g->demand, 1.15)));
+ g->secTimer++;
+ if (g->secTimer >= 10) {
+ calculate_rev();
+ g->secTimer = 0;
+ }
+ }
+}
+
+static bool due(uint32_t at, uint32_t t) { return at == t; }
+
+void pc_advance(uint32_t ms) {
+ uint32_t end = g->now + ms;
+ for (;;) {
+ uint32_t t = g->at_main;
+ const uint32_t *all[] = {&g->at_slow, &g->at_battle, &g->at_stocks, &g->at_shop, &g->at_sell, &g->at_pick};
+ for (unsigned i = 0; i < sizeof all / sizeof all[0]; i++)
+ if ((int32_t)(*all[i] - t) < 0) t = *all[i];
+ if (g->tourneyStep && (int32_t)(g->at_tourney - t) < 0) t = g->at_tourney;
+ if (g->hypnoOn && (int32_t)(g->at_hypno - t) < 0) t = g->at_hypno;
+ if (g->blinkProject >= 0 && (int32_t)(g->at_blink - t) < 0) t = g->at_blink;
+ if ((int32_t)(t - end) > 0) break;
+ g->now = t;
+ /* in the order the page set them up */
+ if (due(g->at_battle, t)) battle_frame(), g->at_battle += 16;
+ if (due(g->at_stocks, t)) stocks_tick(), g->at_stocks += 100;
+ if (due(g->at_shop, t)) {
+ if (g->humanFlag == 1) stock_shop();
+ g->at_shop += 1000;
+ }
+ if (due(g->at_sell, t)) sell_tick(), g->at_sell += 2500;
+ if (due(g->at_pick, t)) g->pick = g->pickChoice, g->at_pick += 100;
+ if (due(g->at_main, t)) main_tick(), g->at_main += 10;
+ if (due(g->at_slow, t)) slow_tick(), g->at_slow += 100;
+ if (g->tourneyStep && due(g->at_tourney, t)) tourney_step();
+ if (g->hypnoOn && due(g->at_hypno, t)) hypno_step();
+ if (g->blinkProject >= 0 && due(g->at_blink, t)) blink_step();
+ if (g->resetPending) return;
+ }
+ g->now = end;
+}
+
+/* ---------------------------------------------------------------- the buttons */
+bool pc_enabled(int b) {
+ if (b >= B_PROJECT && b < B_PROJECT + PC_PROJECTS) return pc_project_cost(b - B_PROJECT);
+ if (b >= B_STAT_DOWN && b < B_STAT_DOWN + 8) return !(*probe_stat(b - B_STAT_DOWN) < 1);
+ if (b >= B_STAT_UP && b < B_STAT_UP + 8) return !(g->probeTrust - g->probeUsedTrust < 1);
+ switch (b) {
+ case B_CLIP: return !(g->wire < 1);
+ case B_WIRE: return !(g->funds < g->wireCost);
+ case B_CLIPPER: return !(g->funds < g->clipperCost);
+ case B_MEGA: return !(g->funds < g->megaClipperCost);
+ case B_LOWER: return !(g->margin <= .01);
+ case B_ADS: return !(g->funds < g->adCost);
+ case B_PROC:
+ case B_MEM: return !(g->trust <= g->processors + g->memory && g->swarmGifts <= 0);
+ case B_INVEST_UP: return !(g->yomi < g->investUpgradeCost);
+ case B_RUN: return g->runEnabled != 0;
+ case B_NEW_TOURNEY: return g->operations >= g->tourneyCost && g->tourneyInProg == 0;
+ case B_FACTORY: return !(g->unusedClips < g->factoryCost);
+ case B_FACTORY_REBOOT: return !(g->factoryLevel == 0);
+ case B_HARV1: return !(g->unusedClips < g->harvesterCost);
+ case B_HARV10: return !(g->unusedClips < g->p10h);
+ case B_HARV100: return !(g->unusedClips < g->p100h);
+ case B_HARV1000: return !(g->unusedClips < g->p1000h);
+ case B_HARV_REBOOT: return !(g->harvesterLevel == 0);
+ case B_WIRED1: return !(g->unusedClips < g->wireDroneCost);
+ case B_WIRED10: return !(g->unusedClips < g->p10w);
+ case B_WIRED100: return !(g->unusedClips < g->p100w);
+ case B_WIRED1000: return !(g->unusedClips < g->p1000w);
+ case B_WIRED_REBOOT: return !(g->wireDroneLevel == 0);
+ case B_FARM1: return !(g->unusedClips < g->farmCost);
+ case B_FARM10: return !(g->unusedClips < g->p10f);
+ case B_FARM100: return !(g->unusedClips < g->p100f);
+ case B_FARM_REBOOT: return !(g->farmLevel < 1);
+ case B_BATT1: return !(g->unusedClips < g->batteryCost);
+ case B_BATT10: return !(g->unusedClips < g->p10b);
+ case B_BATT100: return !(g->unusedClips < g->p100b);
+ case B_BATT_REBOOT: return !(g->batteryLevel < 1);
+ case B_ENTERTAIN: return !(g->creativity < g->entertainCost);
+ case B_SYNCH: return !(g->yomi < g->synchCost);
+ case B_WORK: return g->sliderPos > 0;
+ case B_THINK: return g->sliderPos < 200;
+ case B_PROBE: return !(g->unusedClips < g->probeCost);
+ case B_PROBE_TRUST: return !(g->yomi < g->probeTrustCost || g->probeTrust >= g->maxTrust);
+ case B_MAX_TRUST: return !(g->honor < g->maxTrustCost);
+ default: return true;
+ }
+}
+
+bool pc_press(int b) {
+ if (b < 0 || b >= B_COUNT || !pc_enabled(b)) return false;
+ if (b >= B_PROJECT) {
+ pc_project_effect(b - B_PROJECT);
+ return true;
+ }
+ if (b >= B_STAT_UP) return raise_stat(b - B_STAT_UP), true;
+ if (b >= B_STAT_DOWN) return lower_stat(b - B_STAT_DOWN), true;
+ switch (b) {
+ case B_CLIP: clip_click(1); break;
+ case B_WIREBUYER: toggle_wire_buyer(); break;
+ case B_WIRE: buy_wire(); break;
+ case B_CLIPPER: make_clipper(); break;
+ case B_MEGA: make_mega_clipper(); break;
+ case B_LOWER: lower_price(); break;
+ case B_RAISE: raise_price(); break;
+ case B_ADS: buy_ads(); break;
+ case B_PROC: add_proc(); break;
+ case B_MEM: add_mem(); break;
+ case B_QCOMP: q_comp(); break;
+ case B_DEPOSIT: invest_deposit(); break;
+ case B_WITHDRAW: invest_withdraw(); break;
+ case B_RISK: g->riskChoice = (g->riskChoice + 1) % 3; break;
+ case B_INVEST_UP: invest_upgrade(); break;
+ case B_PICK: g->pickChoice = g->pickChoice >= 10 ? 0 : g->pickChoice + 1 >= g->nstrats ? 10 : g->pickChoice + 1; break;
+ case B_RUN: run_tourney(); break;
+ case B_NEW_TOURNEY: new_tourney(); break;
+ case B_AUTOTOURNEY: toggle_auto_tourney(); break;
+ case B_FACTORY: make_factory(); break;
+ case B_FACTORY_REBOOT: pc_factory_reboot(); break;
+ case B_HARV1: make_harvester(1); break;
+ case B_HARV10: make_harvester(10); break;
+ case B_HARV100: make_harvester(100); break;
+ case B_HARV1000: make_harvester(1000); break;
+ case B_HARV_REBOOT: pc_harvester_reboot(); break;
+ case B_WIRED1: make_wire_drone(1); break;
+ case B_WIRED10: make_wire_drone(10); break;
+ case B_WIRED100: make_wire_drone(100); break;
+ case B_WIRED1000: make_wire_drone(1000); break;
+ case B_WIRED_REBOOT: pc_wiredrone_reboot(); break;
+ case B_FARM1: make_farm(1); break;
+ case B_FARM10: make_farm(10); break;
+ case B_FARM100: make_farm(100); break;
+ case B_FARM_REBOOT: pc_farm_reboot(); break;
+ case B_BATT1: make_battery(1); break;
+ case B_BATT10: make_battery(10); break;
+ case B_BATT100: make_battery(100); break;
+ case B_BATT_REBOOT: pc_battery_reboot(); break;
+ case B_ENTERTAIN: entertain_swarm(); break;
+ case B_SYNCH: synch_swarm(); break;
+ case B_WORK: g->sliderPos = g->sliderPos > 10 ? g->sliderPos - 10 : 0; break;
+ case B_THINK: g->sliderPos = g->sliderPos < 190 ? g->sliderPos + 10 : 200; break;
+ case B_PROBE: make_probe(); break;
+ case B_PROBE_TRUST: increase_probe_trust(); break;
+ case B_MAX_TRUST: increase_max_trust(); break;
+ default: return false;
+ }
+ return true;
+}
diff --git a/games/paperclips/src/ui.c b/games/paperclips/src/ui.c
new file mode 100644
index 00000000..7fef04ff
--- /dev/null
+++ b/games/paperclips/src/ui.c
@@ -0,0 +1,1105 @@
+/* ui.h: the game's page, for 320 x 240 and the keypad.
+ *
+ * The web page has three columns; here they are tabs with the page's own sections: Clips (Make Paperclip and
+ * Manufacturing), Business, Wire (Wire Production), Power, Space (Space Exploration), Probes (Von Neumann Probe
+ * Design), Combat (the battle canvas, at its own 310 x 150), Computer (Computational Resources, the swarm and
+ * Quantum Computing), Projects, Invest (Investments) and Strategy (Strategic Modeling), each shown when the game
+ * shows its section. The console stays at the top, Paperclips under it. The page's look: white, black text, grey
+ * buttons; the button in focus has a blue frame.
+ *
+ * Each frame is laid out into a list of drawing steps, then drawn strip by strip. */
+#include <string.h>
+#include "gfx.h"
+#include "num.h"
+#include "plat.h"
+#include "ui.h"
+
+#define g G
+
+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 CON_H 34
+#define HDR_Y 34
+#define TAB_Y 56
+#define TAB_H 15
+#define PAN_Y0 71
+#define PAN_Y1 225
+#define PAN_H (PAN_Y1 - PAN_Y0)
+#define KEY_Y 225
+#define LEFT 6
+#define WIDTH 308
+#define BTN_H 17
+
+#define C_BLACK 0x0000
+#define C_WHITE 0xFFFF
+#define C_TEXT HEX(0x111111)
+#define C_DIM HEX(0x666666)
+#define C_GREY HEX(0x808080)
+#define C_LINE HEX(0xB4B4B4)
+#define C_FOCUS HEX(0x1A6FE8)
+#define C_PROJECT HEX(0xC8C8C8)
+
+static const np_font_t *const BODY = &np_font_body, *const SMALL = &np_font_small, *const TITLE = &np_font_title;
+
+/* ---------------------------------------------------------------- text, with the four letters past ASCII */
+static int glyph_at(uint8_t b) {
+ if (b >= 32 && b < 127) return b - 32;
+ switch (b) {
+ case 0xA9: return 95; /* the font's extra letters, in code point order */
+ case 0xE9: return 96;
+ case 0xF3: return 97;
+ case 0xF8: return 98;
+ default: return '?' - 32;
+ }
+}
+static int text_w(const np_font_t *f, const char *s) {
+ int w = 0;
+ for (; *s; s++) w += f->glyphs[glyph_at((uint8_t)*s)].advance;
+ return w;
+}
+static void draw_text(const np_font_t *f, int x, int base, const char *s, color_t c, int a) {
+ if (base + f->descent <= gfx_y0 || base - f->ascent >= gfx_y1) return;
+ for (; *s; s++) {
+ const np_glyph_t *gl = &f->glyphs[glyph_at((uint8_t)*s)];
+ if (gl->w) gfx_mask(f->data + gl->offset, gl->w, gl->h, x + gl->x, base + gl->y, c, a);
+ x += gl->advance;
+ }
+}
+/* the next line of s that fits in w pixels (broken after a space when it can): its length; *next past it */
+static int wrap(const np_font_t *f, const char *s, int w, const char **next) {
+ int n = 0, last = -1, px = 0;
+ while (s[n] && s[n] != '\n') {
+ px += f->glyphs[glyph_at((uint8_t)s[n])].advance;
+ if (px > w && n > 0) break;
+ if (s[n] == ' ') last = n;
+ n++;
+ }
+ int len = n;
+ if (s[n] && s[n] != '\n' && last > 0) len = last;
+ const char *p = s + len;
+ while (*p == ' ') p++;
+ if (*p == '\n') p++;
+ *next = p;
+ return len;
+}
+
+/* ---------------------------------------------------------------- the frame's drawing steps */
+enum { K_FILL, K_GRAD, K_FRAME, K_TEXT, K_CANVAS, K_ALPHA };
+typedef struct {
+ int16_t x, y, w, h, cy0, cy1;
+ uint8_t kind, a;
+ color_t c;
+ uint32_t top, bottom;
+ const np_font_t *font;
+ const char *s;
+} op_t;
+#define MAX_OPS 900
+static op_t ops[MAX_OPS];
+static int nops;
+static char pool[16000];
+static int npool;
+static int16_t clip0 = 0, clip1 = SCREEN_H;
+
+static char *P(int n) {
+ if (npool + n > (int)sizeof pool) npool = 0; /* (never: the pool fits a frame) */
+ char *p = pool + npool;
+ npool += n;
+ *p = 0;
+ return p;
+}
+static op_t *op(int kind, int x, int y, int w, int h) {
+ if (nops >= MAX_OPS) return &ops[MAX_OPS - 1];
+ op_t *o = &ops[nops++];
+ memset(o, 0, sizeof *o);
+ o->kind = (uint8_t)kind, o->x = (int16_t)x, o->y = (int16_t)y, o->w = (int16_t)w, o->h = (int16_t)h;
+ o->cy0 = clip0, o->cy1 = clip1, o->a = 255;
+ return o;
+}
+static void fill(int x, int y, int w, int h, color_t c) { op(K_FILL, x, y, w, h)->c = c; }
+static void fill_a(int x, int y, int w, int h, color_t c, int a) {
+ op_t *o = op(K_ALPHA, x, y, w, h);
+ o->c = c, o->a = (uint8_t)a;
+}
+static void grad(int x, int y, int w, int h, uint32_t top, uint32_t bottom) {
+ op_t *o = op(K_GRAD, x, y, w, h);
+ o->top = top, o->bottom = bottom;
+}
+static void frame(int x, int y, int w, int h, int t, color_t c) {
+ op_t *o = op(K_FRAME, x, y, w, h);
+ o->c = c, o->a = (uint8_t)t;
+}
+static void text(const np_font_t *f, int x, int base, const char *s, color_t c) {
+ op_t *o = op(K_TEXT, x, base, 0, 0);
+ o->font = f, o->s = s, o->c = c;
+}
+static void text_a(const np_font_t *f, int x, int base, const char *s, color_t c, int a) {
+ op_t *o = op(K_TEXT, x, base, 0, 0);
+ o->font = f, o->s = s, o->c = c, o->a = (uint8_t)a;
+}
+
+/* strings for this frame */
+static const char *S(const char *a, const char *b, const char *c, const char *d, const char *e) {
+ const char *in[5] = {a, b, c, d, e};
+ int n = 1;
+ for (int i = 0; i < 5; i++)
+ if (in[i]) n += (int)strlen(in[i]);
+ char *p = P(n);
+ for (int i = 0; i < 5; i++)
+ if (in[i]) strcat(p, in[i]);
+ return p;
+}
+#define S2(a, b) S(a, b, 0, 0, 0)
+#define S3(a, b, c) S(a, b, c, 0, 0)
+static const char *cm(double v, int d) { return js_commas(v, d, P(120), 120); }
+static const char *sp(double v) { return js_spellf(v, P(80), 80); }
+static const char *num(double v) { return js_tostring(v, P(40), 40); }
+static const char *cr(double v, int d) { return js_cruncher(v, d, P(60), 60); }
+
+/* ---------------------------------------------------------------- the panel being laid out */
+typedef struct {
+ int16_t id, x, y, w, h;
+} hit_t;
+static hit_t hits[200];
+static int nhits, cy, content_h;
+static pc_ui *U;
+
+static bool focused(int id) { return U && U->focus[U->tab] == id; }
+
+static void heading(const char *s) {
+ cy += 2;
+ text(BODY, LEFT, cy + 11, s, C_BLACK);
+ fill(LEFT, cy + 15, WIDTH, 1, C_LINE);
+ cy += 19;
+}
+static void line(const char *s) {
+ text(BODY, LEFT, cy + 11, s, C_TEXT);
+ cy += 14;
+}
+static void line_small(const char *s, color_t c) {
+ const char *p = s;
+ while (*p) {
+ const char *next;
+ int n = wrap(SMALL, p, WIDTH, &next);
+ char *t = P(n + 1);
+ memcpy(t, p, (size_t)n), t[n] = 0;
+ text(SMALL, LEFT, cy + 9, t, c);
+ cy += 11;
+ p = next;
+ }
+}
+/* a button at x on this row: its right edge */
+static int button(int id, int x, const char *label) {
+ bool on = pc_enabled(id), f = focused(id);
+ int w = text_w(BODY, label) + 12;
+ if (on) {
+ grad(x, cy, w, BTN_H, 0xFFFFFF, 0x9A9A9A);
+ frame(x, cy, w, BTN_H, 1, HEX(0x1A1A1A));
+ text(BODY, x + 6, cy + 12, label, C_BLACK);
+ } else {
+ grad(x, cy, w, BTN_H, 0xFFFFFF, 0xD7D7D7);
+ text(BODY, x + 6, cy + 12, label, HEX(0x8C8C8C));
+ }
+ if (f) frame(x - 2, cy - 2, w + 4, BTN_H + 4, 2, C_FOCUS);
+ if (nhits < (int)(sizeof hits / sizeof hits[0])) hits[nhits++] = (hit_t){(int16_t)id, (int16_t)x, (int16_t)cy, (int16_t)w, BTN_H};
+ return x + w;
+}
+static void after(int x, const char *s) { text(BODY, x + 6, cy + 12, s, C_TEXT); }
+static void row_end(void) { cy += BTN_H + 4; }
+
+static const char *inches(double w) { return w == 1 ? " inch" : " inches"; }
+
+/* ---------------------------------------------------------------- the tabs' contents */
+static void tab_clips(void) {
+ button(B_CLIP, LEFT, "Make Paperclip");
+ row_end();
+ if (g->prestigeU >= 1 || g->prestigeS >= 1) {
+ fill(LEFT, cy, WIDTH, 14, HEX(0xD3D3D3));
+ line(S("Universe: ", num(g->prestigeU + 1), " / Sim Level: ", num(g->prestigeS + 1), 0));
+ }
+ int x;
+ if (g->humanFlag == 1) {
+ heading("Manufacturing");
+ line(S2("Clips per Second: ", cm(js_round(g->clipRate), 0)));
+ if (g->wireBuyerFlag == 1) {
+ x = button(B_WIREBUYER, LEFT, "WireBuyer");
+ after(x, g->wireBuyerStatus == 1 ? "ON" : "OFF");
+ row_end();
+ }
+ x = button(B_WIRE, LEFT, "Wire");
+ after(x, S(cm(g->wire, 0), " inches", " Cost: $", num(g->wireCost), 0));
+ row_end();
+ if (g->autoClipperFlag != 0) {
+ x = button(B_CLIPPER, LEFT, "AutoClippers");
+ after(x, S(num(g->clipmakerLevel), " Cost: $", cm(g->clipperCost, 2), 0, 0));
+ row_end();
+ }
+ if (g->megaClipperFlag != 0) {
+ x = button(B_MEGA, LEFT, "MegaClippers");
+ after(x, S(num(g->megaClipperLevel), " Cost: $", cm(g->megaClipperCost, 2), 0, 0));
+ row_end();
+ }
+ return;
+ }
+ if (g->endTimer6 >= 250) return;
+ heading("Manufacturing");
+ if (g->flag[P45] == 1 && g->maxFactoryLevel < 50) {
+ double n = g->maxFactoryLevel < 10 ? 10 : g->maxFactoryLevel < 20 ? 20 : 50;
+ line(S3("Next Upgrade at: ", cm(n, 0), " Factories"));
+ }
+ if (g->dismantle < 3) line(S2("Clips per Second: ", sp(g->clipRate)));
+ if (g->tothFlag == 1 && g->dismantle < 3) line(S2("Unused Clips: ", sp(g->unusedClips)));
+ if (g->factoryFlag == 1 && g->spaceFlag == 0) {
+ x = button(B_FACTORY, LEFT, "Clip Factory");
+ after(x, num(g->factoryLevel));
+ row_end();
+ x = button(B_FACTORY_REBOOT, LEFT, "Disassemble All");
+ after(x, S3("+", sp(g->factoryBill), " clips"));
+ row_end();
+ line(S3("Cost: ", sp(g->factoryCost), " clips"));
+ }
+ if (g->wireProductionFlag == 0 || g->dismantle >= 2)
+ line(S3("Wire: ", g->dismantle >= 4 ? cm(g->wire, 0) : sp(g->wire), inches(g->wire)));
+ if (g->spaceFlag == 1 && g->dismantle < 3) line(S2("Factories: ", sp(g->factoryLevel)));
+}
+
+static void tab_business(void) {
+ heading("Business");
+ line(S2("Available Funds: $", cm(g->funds, 2)));
+ if (g->revPerSecFlag == 1) {
+ line(S2("Avg. Rev. per sec: $", cm(g->avgRev, 2)));
+ line(S2("Avg. Clips Sold per sec: ", cm(js_round(g->avgSales), 0)));
+ }
+ line(S2("Unsold Inventory: ", cm(g->unsoldClips, 0)));
+ int x = button(B_LOWER, LEFT, "lower");
+ x = button(B_RAISE, x + 6, "raise");
+ after(x, S2("Price per Clip: $", js_tofixed(g->margin, 2, P(40), 40)));
+ row_end();
+ line(S3("Public Demand: ", cm(g->demand * 10, 0), "%"));
+ x = button(B_ADS, LEFT, "Marketing");
+ after(x, S2("Level: ", num(g->marketingLvl)));
+ row_end();
+ line(S2("Cost: $", cm(g->adCost, 2)));
+}
+
+static void drone_rows(int b1, const char *name, double level, double bill, double cost) {
+ int x = button(b1, LEFT, name);
+ after(x, cm(level, 0));
+ row_end();
+ x = button(b1 + 1, LEFT, "+10");
+ x = button(b1 + 2, x + 6, "+100");
+ if (b1 != B_FARM1 && b1 != B_BATT1) button(b1 + 3, x + 6, "+1k");
+ row_end();
+ int reboot = b1 == B_FARM1 ? B_FARM_REBOOT : b1 == B_BATT1 ? B_BATT_REBOOT : b1 + 4;
+ x = button(reboot, LEFT, "Disassemble All");
+ after(x, S3("+", sp(bill), " clips"));
+ row_end();
+ line(S3("Cost: ", sp(cost), " clips"));
+}
+
+static void tab_wire(void) {
+ heading("Wire Production");
+ if (g->maxDroneLevel < 50000) {
+ double n = g->maxDroneLevel < 500 ? 500 : g->maxDroneLevel < 5000 ? 5000 : 50000;
+ line(S3("Next Upgrade at: ", cm(n, 0), " Drones"));
+ }
+ line(S3("Available Matter: ", sp(g->availableMatter), " g"));
+ if (g->spaceFlag == 1) line(S3("(", sp(g->mdps), " g per sec)"));
+ line(S(
+ "Acquired Matter: ", sp(g->acquiredMatter), " g (", g->maps == 0 ? "0" : sp(g->maps), " g per sec)"));
+ line(S("Wire: ", sp(g->wire), " inches (", g->wpps == 0 ? "0" : sp(g->wpps), " inches per sec)"));
+ if (g->spaceFlag == 0) {
+ if (g->harvesterFlag == 1) drone_rows(B_HARV1, "Harvester Drone", g->harvesterLevel, g->harvesterBill, g->harvesterCost);
+ if (g->wireDroneFlag == 1) drone_rows(B_WIRED1, "Wire Drone", g->wireDroneLevel, g->wireDroneBill, g->wireDroneCost);
+ } else {
+ line(S2("Harvester Drones: ", sp(g->harvesterLevel)));
+ line(S2("Wire Drones: ", sp(g->wireDroneLevel)));
+ }
+}
+
+static void tab_power(void) {
+ heading("Power");
+ double supply = g->farmLevel * 50 / 100;
+ double dDemand = (g->harvesterLevel * 1.0 / 100) + (g->wireDroneLevel * 1.0 / 100);
+ double fDemand = (g->factoryLevel * 200 / 100), demand = dDemand + fDemand;
+ bool none = g->factoryLevel == 0 && g->harvesterLevel == 0 && g->wireDroneLevel == 0;
+ line(S3("Factory/Drone Performance: ", none ? "0" : cm(js_round(g->powMod * 100), 0), "%"));
+ line(S3("Consumption: ", cm(js_round(demand * 100), 0), " MW"));
+ line(S("Factories: ", cm(js_round(fDemand * 100), 0), " MW Drones: ", cm(js_round(dDemand * 100), 0), " MW"));
+ line(S3("Production: ", cm(js_round(supply * 100), 0), " MW"));
+ drone_rows(B_FARM1, "Solar Farm", g->farmLevel, g->farmBill, g->farmCost);
+ line(S("Storage: ", cm(js_round(g->storedPower), 0), " / ", cm(js_round(g->batteryLevel * 10000), 0), " MW-seconds"));
+ drone_rows(B_BATT1, "Battery Tower", g->batteryLevel, g->batteryBill, g->batteryCost);
+}
+
+static void tab_space(void) {
+ heading("Space Exploration");
+ line(S2(js_tofixed(100 / (g->totalMatter / g->foundMatter), 12, P(40), 40), "% of universe explored"));
+ int x = button(B_PROBE, LEFT, "Launch Probe");
+ after(x, S3("Cost: ", sp(g->probeCost), " clips"));
+ row_end();
+ line(S2("Launched: ", cm(g->probeLaunchLevel, 0)));
+ line(S2("Descendents: ", sp(g->probeDescendents)));
+ if (g->probesLostHaz >= 1) line(S3("Lost to hazards: (", sp(g->probesLostHaz), ")"));
+ if (g->probesLostDrift >= 1) line(S3("Lost to value drift: (", sp(g->probesLostDrift), ")"));
+ if (g->probesLostCombat >= 1) line(S3("Lost in combat: (", cr(g->probesLostCombat, 2), ")"));
+ line(S2("Total: ", sp(g->probeCount)));
+ if (g->battleFlag == 1) {
+ line(S2("Drifters Killed: ", cr(g->driftersKilled, 2)));
+ line(S2("Drifters: ", sp(g->drifterCount)));
+ }
+}
+
+static const char *const STAT_NAME[8] = {"Speed", "Exploration", "Self-Replication", "Hazard Remediation",
+ "Factory Production", "Harvester Drone Production", "Wire Drone Production",
+ "Combat"};
+static const char *const STAT_DESC[8] = {
+ "Modifies rate of exploration", "Rate at which probes gain access to new matter",
+ "Rate at which probes generate more probes (each new probe costs 100 quadrillion clips)",
+ "Reduces damage from dust, junk, radiation, and general entropic decay",
+ "Rate at which probes build factories (each new factory costs 100 million clips)",
+ "Rate at which probes spawn Harvester Drones (each new drone costs 2 million clips)",
+ "Rate at which probes spawn Wire Drones (each new drone costs 2 million clips)",
+ "Determines offensive and defensive effectiveness in battle"};
+
+static void tab_probes(void) {
+ if (g->dismantle < 1) {
+ heading("Von Neumann Probe Design");
+ line(S(S3("Trust: ", num(g->probeUsedTrust), " / "), num(g->probeTrust), " (", cm(g->maxTrust, 0), " Max)"));
+ const double *stat[8] = {&g->probeSpeed, &g->probeNav, &g->probeRep, &g->probeHaz, &g->probeFac,
+ &g->probeHarv, &g->probeWire, &g->probeCombat};
+ for (int k = 0; k < 8; k++) {
+ if (k == 7 && g->flag[P131] == 0) break;
+ int x = button(B_STAT_DOWN + k, LEFT, "<");
+ x = button(B_STAT_UP + k, x + 4, ">");
+ after(x, S3(STAT_NAME[k], ": ", num(*stat[k])));
+ row_end();
+ cy -= 2;
+ line_small(STAT_DESC[k], C_DIM);
+ cy += 3;
+ }
+ }
+ if (g->dismantle < 1 || g->endTimer1 < 50) {
+ int x = button(B_PROBE_TRUST, LEFT, "Increase Probe Trust");
+ after(x, S3("Cost: ", cm(js_floor(g->probeTrustCost), 0), " yomi"));
+ row_end();
+ }
+ if (g->flag[P121] == 1 && (g->dismantle < 1 || g->endTimer1 < 100)) {
+ int x = button(B_MAX_TRUST, LEFT, "Increase Max Trust");
+ after(x, S3("Cost: ", cm(g->maxTrustCost, 2), " honor"));
+ row_end();
+ }
+}
+
+static void tab_combat(void) {
+ op_t *o = op(K_CANVAS, 5, cy, 310, 150);
+ (void)o;
+ int y = cy;
+ text(BODY, 15, y + 18, "Combat", C_WHITE);
+ text(BODY, 15, y + 33, g->battleName, C_WHITE);
+ if (g->battleResult) {
+ text(TITLE, 15, y + 60, g->battleResult == 1 ? "VICTORY" : "DEFEAT", C_WHITE);
+ text(BODY, 15, y + 76, S3(g->battleResult == 1 ? "+" : "-", num(g->honorAmount), " honor"), C_WHITE);
+ }
+ text(SMALL, 15, y + 144, S3("Scale = ", cr(g->unitSize, 0), ":1"), C_WHITE);
+ cy += 152;
+ if (g->flag[P121] == 1 && (g->dismantle < 1 || g->endTimer1 < 190)) line(S2("Honor: ", cm(js_round(g->honor), 0)));
+}
+
+static const char *const STATUS[10] = {"Active", "Hungry", "Confused", "Bored", "Cold", "Disorganized", "Sleeping",
+ "", "Lonely", "NO RESPONSE..."};
+static const int16_t CHIP_GONE[PC_CHIPS] = {174, 172, 169, 165, 160, 150, 130, 100, 60, 10};
+
+static void tab_computer(void) {
+ heading("Computational Resources");
+ if (g->humanFlag == 1) {
+ line(S2("Trust: ", cm(g->trust, 0)));
+ line(S3("+1 Trust at: ", cm(js_floor(g->nextTrust), 0), " clips"));
+ }
+ if (g->swarmFlag == 1 && !(g->dismantle >= 2 && g->endTimer2 >= 50)) line(S2("Swarm Gifts: ", cm(g->swarmGifts, 0)));
+ int x;
+ if (g->dismantle < 6) {
+ x = button(B_PROC, LEFT, "Processors");
+ after(x, num(g->processors));
+ row_end();
+ }
+ x = button(B_MEM, LEFT, "Memory");
+ after(x, num(g->memory));
+ row_end();
+ line(S("Operations: ", cm(g->operations, 0), " / ", cm(g->memory * 1000, 0), 0));
+ if (g->creativityOn != 0) line(S2("Creativity: ", cm(g->creativity, 0)));
+ if (g->swarmFlag == 1 && !(g->dismantle >= 2 && g->endTimer2 >= 100)) {
+ heading("Swarm Computing");
+ line(S2("Drones: ", sp(js_floor(g->harvesterLevel + g->wireDroneLevel))));
+ int st = (int)g->swarmStatus;
+ if (st != 7 && st >= 0 && st < 10) line(S2("Status: ", STATUS[st]));
+ if (st == 0) line(S2("Next gift in ", js_time(g->giftCountdown, P(80), 80)));
+ if (st == 3) {
+ x = button(B_ENTERTAIN, LEFT, "Entertain the Swarm");
+ after(x, S3("Cost: ", cm(g->entertainCost, 0), " creat"));
+ row_end();
+ }
+ if (st == 5) {
+ x = button(B_SYNCH, LEFT, "Synchronize the Swarm");
+ after(x, S3("Cost: ", cm(g->synchCost, 0), " yomi"));
+ row_end();
+ }
+ if (!(g->dismantle >= 2 && g->endTimer2 >= 150)) {
+ x = button(B_WORK, LEFT, "< Work");
+ int right = LEFT + WIDTH - (text_w(BODY, "Think >") + 12);
+ int x0 = x + 8, x1 = right - 8;
+ fill(x0, cy + 8, x1 - x0, 2, C_LINE);
+ int k = x0 + (int)((x1 - x0 - 8) * g->sliderPos / 200);
+ grad(k, cy + 2, 8, 13, 0xFFFFFF, 0x9A9A9A);
+ frame(k, cy + 2, 8, 13, 1, HEX(0x1A1A1A));
+ button(B_THINK, right, "Think >");
+ row_end();
+ }
+ }
+ if (g->qFlag == 1 && !(g->dismantle >= 5 && g->endTimer4 >= 250)) {
+ heading("Quantum Computing");
+ for (int i = 0; i < PC_CHIPS; i++) {
+ if (g->dismantle >= 5 && g->endTimer4 >= CHIP_GONE[i]) continue;
+ double v = g->qChipValue[i];
+ int a = v <= 0 ? 0 : v >= 1 ? 255 : (int)(v * 255);
+ int cx = LEFT + 2 + i * 26;
+ frame(cx, cy, 22, 22, 1, HEX(0xE0E0E0));
+ if (a) fill_a(cx, cy, 22, 22, C_BLACK, a);
+ }
+ cy += 28;
+ if (g->dismantle < 5) {
+ x = button(B_QCOMP, LEFT, "Compute");
+ double f = g->qFade < 0 ? 0 : g->qFade > 1 ? 1 : g->qFade;
+ if (g->qShown == 1) text_a(BODY, x + 6, cy + 12, "Need Photonic Chips", C_TEXT, (int)(f * 255));
+ if (g->qShown == 2) text_a(BODY, x + 6, cy + 12, S2("qOps: ", cm(g->qOpsShown, 0)), C_TEXT, (int)(f * 255));
+ row_end();
+ }
+ }
+}
+
+static void tab_projects(void) {
+ if (!g->nactive) {
+ cy += 4;
+ line_small("No projects for now.", C_GREY);
+ return;
+ }
+ for (int k = 0; k < g->nactive; k++) {
+ int i = g->active[k];
+ int id = B_PROJECT + i;
+ bool on = pc_enabled(id), f = focused(id);
+ char *title = P(PC_NAME_LEN + 40), *price = P(64);
+ pc_project_title(i, title, PC_NAME_LEN + 40);
+ pc_project_price(i, price, 64);
+ const char *head = *price ? S3(title, " ", price) : title;
+ /* the title and price, then the description, in the box */
+ const char *lines[8];
+ int nl = 0;
+ const char *p = head;
+ while (*p && nl < 3) {
+ const char *next;
+ int n = wrap(BODY, p, WIDTH - 10, &next);
+ char *t = P(n + 1);
+ memcpy(t, p, (size_t)n), t[n] = 0;
+ lines[nl++] = t, p = next;
+ }
+ int nh = nl;
+ p = PC_PROJECT_TEXT[i].desc;
+ while (*p && nl < 8) {
+ const char *next;
+ int n = wrap(SMALL, p, WIDTH - 10, &next);
+ char *t = P(n + 1);
+ memcpy(t, p, (size_t)n), t[n] = 0;
+ lines[nl++] = t, p = next;
+ }
+ int h = 6 + nh * 13 + (nl - nh) * 11;
+ bool hidden = g->blinkProject == i && (g->blinkCount & 1);
+ if (!hidden) {
+ fill(LEFT, cy, WIDTH, h, on ? C_PROJECT : HEX(0xDDDDDD));
+ if (on) frame(LEFT, cy, WIDTH, h, 1, C_BLACK);
+ int y = cy + 3;
+ for (int l = 0; l < nl; l++) {
+ bool big = l < nh;
+ text(big ? BODY : SMALL, LEFT + 5, y + (big ? 11 : 9), lines[l], on ? (big ? C_BLACK : C_TEXT) : HEX(0x8A8A8A));
+ y += big ? 13 : 11;
+ }
+ }
+ if (f) frame(LEFT - 2, cy - 2, WIDTH + 4, h + 4, 2, C_FOCUS);
+ if (nhits < (int)(sizeof hits / sizeof hits[0]))
+ hits[nhits++] = (hit_t){(int16_t)id, LEFT, (int16_t)cy, WIDTH, (int16_t)h};
+ if (U) U->seen[i] = 1;
+ cy += h + 6;
+ }
+}
+
+static const char *const RISK_NAME[3] = {"Low Risk", "Med Risk", "High Risk"};
+
+static void tab_invest(void) {
+ heading("Investments");
+ int x = button(B_RISK, LEFT, RISK_NAME[g->riskChoice % 3]);
+ x = button(B_DEPOSIT, x + 10, "Deposit");
+ button(B_WITHDRAW, x + 6, "Withdraw");
+ row_end();
+ line(S2("Cash: $", cm(g->bankroll, 0)));
+ line(S2("Stocks: $", cm(g->secTotal, 0)));
+ line(S2("Total: $", cm(g->portTotal, 0)));
+ static const int16_t COL[5] = {LEFT, LEFT + 52, LEFT + 118, LEFT + 180, LEFT + 248};
+ static const char *const HEAD[5] = {"Stock", "Amt.", "Price", "Total", "P/L"};
+ cy += 2;
+ fill(LEFT, cy, WIDTH, 13, HEX(0xE4E4E4));
+ for (int c = 0; c < 5; c++) text(SMALL, COL[c] + 2, cy + 10, HEAD[c], C_BLACK);
+ cy += 14;
+ for (int r = 0; r < PC_STOCKS; r++) {
+ if (r < g->nstocks) {
+ const pc_stock *s = &g->stocks[r];
+ text(SMALL, COL[0] + 2, cy + 9, s->symbol, C_TEXT);
+ text(SMALL, COL[1] + 2, cy + 9, num(js_ceil(s->amount)), C_TEXT);
+ text(SMALL, COL[2] + 2, cy + 9, num(js_ceil(s->price)), C_TEXT);
+ text(SMALL, COL[3] + 2, cy + 9, num(js_ceil(s->total)), C_TEXT);
+ text(SMALL, COL[4] + 2, cy + 9, num(js_ceil(s->profit)), C_BLACK);
+ }
+ fill(LEFT, cy + 11, WIDTH, 1, HEX(0xE4E4E4));
+ cy += 12;
+ }
+ cy += 4;
+ x = button(B_INVEST_UP, LEFT, "Upgrade Investment Engine");
+ after(x, S2("Level: ", num(g->investLevel)));
+ row_end();
+ line(S3("Cost: ", cm(g->investUpgradeCost, 0), " Yomi"));
+}
+
+static void tab_strategy(void) {
+ heading("Strategic Modeling");
+ int x = button(B_PICK, LEFT, g->pickChoice >= 10 ? "Pick a Strat" : pc_strat_name(g->pickChoice));
+ button(B_RUN, x + 6, "Run");
+ row_end();
+ line_small(g->tourneyText, C_TEXT);
+ cy += 2;
+ /* the payoff grid: the column player's name above, the row player's to the left */
+ bool named = g->tourneyLvl > 1;
+ const char *a = named ? pc_choice_name(g->gridLabel, 0) : "Move A", *b = named ? pc_choice_name(g->gridLabel, 1) : "Move B";
+ int c0 = LEFT + 140, c1 = LEFT + 210, cw = 66;
+ if (g->v >= 0 && g->v < g->nstrats && g->tourneyInProg) text(SMALL, c0, cy + 9, pc_strat_name(g->v), C_BLACK);
+ cy += 12;
+ text(SMALL, c0 + 2, cy + 9, a, C_TEXT);
+ text(SMALL, c1 + 2, cy + 9, b, C_TEXT);
+ cy += 12;
+ const char *cells[4] = {S3(num(g->aa), ",", num(g->aa)), S3(num(g->ab), ",", num(g->ba)),
+ S3(num(g->ba), ",", num(g->ab)), S3(num(g->bb), ",", num(g->bb))};
+ if (g->h >= 0 && g->h < g->nstrats && g->tourneyInProg) text(SMALL, LEFT, cy + 15, pc_strat_name(g->h), C_BLACK);
+ for (int r = 0; r < 2; r++) {
+ text(SMALL, LEFT + 72, cy + 10, r ? b : a, C_TEXT);
+ for (int c = 0; c < 2; c++) {
+ int cx = c ? c1 : c0, cell = r * 2 + c + 1;
+ fill(cx, cy, cw, 13, g->lastCell == cell ? HEX(0xD3D3D3) : C_WHITE);
+ frame(cx, cy, cw, 13, 1, C_LINE);
+ text(SMALL, cx + 4, cy + 10, cells[r * 2 + c], C_BLACK);
+ }
+ cy += 13;
+ }
+ cy += 4;
+ if (g->resultsShown) {
+ for (int i = 0; i < g->nresults; i++) {
+ int s = g->results[i];
+ const char *t = S(num(i + 1), ". ", pc_strat_name(s), ": ", num(g->stratScore[s]));
+ if (s == g->pick) fill(LEFT, cy, text_w(SMALL, t) + 6, 11, HEX(0xE4E4E4));
+ text(SMALL, LEFT + 3, cy + 9, t, s == g->pick ? C_BLACK : C_TEXT);
+ cy += 11;
+ }
+ cy += 3;
+ }
+ line(S2("Yomi: ", cm(g->yomi, 0)));
+ x = button(B_NEW_TOURNEY, LEFT, "New Tournament");
+ after(x, S3("Cost: ", cm(g->tourneyCost, 0), " ops"));
+ row_end();
+ if (g->autoTourneyFlag == 1) {
+ x = button(B_AUTOTOURNEY, LEFT, "AutoTourney");
+ after(x, g->autoTourneyStatus == 1 ? "ON" : "OFF");
+ row_end();
+ }
+}
+
+bool ui_tab_shown(int t) {
+ switch (t) {
+ case T_CLIPS: return true;
+ case T_BUSINESS: return g->humanFlag == 1;
+ case T_WIRE: return g->humanFlag == 0 && g->wireProductionFlag == 1 && g->dismantle < 2;
+ case T_POWER: return g->flag[P127] == 1 && g->spaceFlag == 0;
+ case T_SPACE: return g->spaceFlag == 1 && !(g->dismantle >= 1 && g->endTimer1 >= 150);
+ case T_PROBES:
+ return g->spaceFlag == 1 && (g->dismantle < 1 || g->endTimer1 < 50 || (g->flag[P121] == 1 && g->endTimer1 < 100));
+ case T_COMBAT: return g->battleFlag == 1 && !(g->dismantle >= 1 && g->endTimer1 >= 190);
+ case T_COMPUTER: return g->compFlag == 1 && g->dismantle < 7;
+ case T_PROJECTS: return g->projectsFlag == 1 && g->dismantle < 7;
+ case T_INVEST: return g->investmentEngineFlag == 1;
+ case T_STRATEGY: return g->strategyEngineFlag == 1 && g->dismantle < 4;
+ default: return false;
+ }
+}
+
+static const char *const TAB_NAME[T_COUNT] = {"Clips", "Business", "Wire", "Power", "Space", "Probes", "Combat",
+ "Computer", "Projects", "Invest", "Strategy"};
+
+static void build_tab(int t) {
+ switch (t) {
+ case T_CLIPS: tab_clips(); break;
+ case T_BUSINESS: tab_business(); break;
+ case T_WIRE: tab_wire(); break;
+ case T_POWER: tab_power(); break;
+ case T_SPACE: tab_space(); break;
+ case T_PROBES: tab_probes(); break;
+ case T_COMBAT: tab_combat(); break;
+ case T_COMPUTER: tab_computer(); break;
+ case T_PROJECTS: tab_projects(); break;
+ case T_INVEST: tab_invest(); break;
+ default: tab_strategy(); break;
+ }
+}
+
+/* ---------------------------------------------------------------- focus and keys */
+static void fix_tab(pc_ui *u) {
+ if (!ui_tab_shown(u->tab)) u->tab = T_CLIPS;
+}
+
+static int focus_index(pc_ui *u) {
+ for (int i = 0; i < nhits; i++)
+ if (hits[i].id == u->focus[u->tab]) return i;
+ return -1;
+}
+
+/* the panel laid out (ops kept from `first` on, in the panel's own coordinates) */
+static int panel(pc_ui *u) {
+ U = u;
+ nhits = 0, cy = 4;
+ int first = nops;
+ build_tab(u->tab);
+ content_h = cy + 4;
+ int i = focus_index(u);
+ if (i < 0 && nhits) {
+ i = u->focus_at[u->tab];
+ if (i < 0) i = 0;
+ if (i >= nhits) i = nhits - 1;
+ u->focus[u->tab] = hits[i].id;
+ /* (the frame is drawn with the next layout) */
+ }
+ if (i >= 0) u->focus_at[u->tab] = i;
+ int maxs = content_h > PAN_H ? content_h - PAN_H : 0;
+ if (u->scroll[u->tab] > maxs) u->scroll[u->tab] = maxs;
+ if (u->scroll[u->tab] < 0) u->scroll[u->tab] = 0;
+ return first;
+}
+
+static void see(pc_ui *u, int i) { /* scrolls the button into view */
+ if (i < 0 || i >= nhits) return;
+ int top = hits[i].y - 6, bottom = hits[i].y + hits[i].h + 6;
+ int *s = &u->scroll[u->tab];
+ if (top < *s) *s = top;
+ if (bottom > *s + PAN_H) *s = bottom - PAN_H;
+ if (*s < 0) *s = 0;
+}
+
+void ui_layout(pc_ui *u) {
+ nops = 0, npool = 0;
+ fix_tab(u);
+ panel(u);
+}
+
+int ui_buttons(pc_ui *u, int *out, int max) {
+ ui_layout(u);
+ int n = 0;
+ for (int i = 0; i < nhits && n < max; i++) out[n++] = hits[i].id;
+ return n;
+}
+
+void ui_up(pc_ui *u) {
+ ui_layout(u);
+ int i = focus_index(u);
+ if (i > 0) {
+ u->focus[u->tab] = hits[i - 1].id, u->focus_at[u->tab] = i - 1;
+ see(u, i - 1);
+ } else {
+ u->scroll[u->tab] -= 40;
+ if (u->scroll[u->tab] < 0) u->scroll[u->tab] = 0;
+ }
+}
+
+void ui_down(pc_ui *u) {
+ ui_layout(u);
+ int i = focus_index(u);
+ if (i >= 0 && i + 1 < nhits) {
+ u->focus[u->tab] = hits[i + 1].id, u->focus_at[u->tab] = i + 1;
+ see(u, i + 1);
+ } else {
+ int maxs = content_h > PAN_H ? content_h - PAN_H : 0;
+ u->scroll[u->tab] += 40;
+ if (u->scroll[u->tab] > maxs) u->scroll[u->tab] = maxs;
+ }
+}
+
+void ui_tab(pc_ui *u, int d) {
+ fix_tab(u);
+ for (int k = 1; k <= T_COUNT; k++) {
+ int t = ((u->tab + d * k) % T_COUNT + T_COUNT) % T_COUNT;
+ if (ui_tab_shown(t)) {
+ u->tab = t;
+ break;
+ }
+ }
+ ui_layout(u);
+ see(u, focus_index(u));
+}
+
+int ui_focused(pc_ui *u) {
+ ui_layout(u);
+ int i = focus_index(u);
+ if (i < 0) return -1;
+ /* out of view: OK brings it back first */
+ int s = u->scroll[u->tab];
+ if (hits[i].y + hits[i].h < s || hits[i].y > s + PAN_H) {
+ see(u, i);
+ return -1;
+ }
+ return hits[i].id;
+}
+
+bool ui_repeats(int b) {
+ switch (b) {
+ case B_CLIP: case B_WIRE: case B_CLIPPER: case B_MEGA: case B_LOWER: case B_RAISE: case B_ADS: case B_PROC:
+ case B_MEM: case B_QCOMP: case B_INVEST_UP: case B_FACTORY: case B_HARV1: case B_HARV10: case B_HARV100:
+ case B_HARV1000: case B_WIRED1: case B_WIRED10: case B_WIRED100: case B_WIRED1000: case B_FARM1: case B_FARM10:
+ case B_FARM100: case B_BATT1: case B_BATT10: case B_BATT100: case B_WORK: case B_THINK: case B_PROBE:
+ case B_PROBE_TRUST: case B_MAX_TRUST: return true;
+ default: return b >= B_STAT_DOWN && b < B_STAT_UP + 8;
+ }
+}
+
+void ui_init(pc_ui *u) {
+ memset(u, 0, sizeof *u);
+ for (int t = 0; t < T_COUNT; t++) u->focus[t] = -1;
+}
+
+/* ---------------------------------------------------------------- the frame */
+static void console(void) {
+ fill(0, 0, SCREEN_W, CON_H, C_BLACK);
+ /* the newest message at the bottom, white after ">", older ones grey above it */
+ const char *lines[3];
+ color_t col[3];
+ int n = 0;
+ for (int m = 0; m < PC_MSG && n < 3; m++) {
+ const char *s = g->msg[m];
+ if (!*s) continue;
+ const char *part[6];
+ int np = 0;
+ const char *p = s;
+ while (*p && np < 6) {
+ const char *next;
+ int len = wrap(SMALL, p, 300, &next);
+ char *t = P(len + 1);
+ memcpy(t, p, (size_t)len), t[len] = 0;
+ part[np++] = t, p = next;
+ }
+ for (int k = np - 1; k >= 0 && n < 3; k--) {
+ lines[n] = part[k], col[n] = m == 0 ? C_WHITE : C_GREY;
+ n++;
+ }
+ if (m == 0 && np > 3) break;
+ }
+ for (int i = 0; i < n; i++) {
+ int y = 31 - 11 * i;
+ bool first = (i == n - 1) || col[i] != col[i + 1];
+ (void)first;
+ text(SMALL, 14, y, lines[i], col[i]);
+ }
+ /* the prompt before the newest */
+ int newest = 0;
+ while (newest + 1 < n && col[newest + 1] == C_WHITE) newest++;
+ if (n) text(SMALL, 4, 31 - 11 * newest, ">", C_WHITE);
+}
+
+static void header(void) {
+ fill(0, HDR_Y, SCREEN_W, TAB_Y - HDR_Y, C_WHITE);
+ const char *digits;
+ const char *crunched = 0;
+ if (g->milestoneFlag < 15) {
+ digits = cm(js_ceil(g->clips), 0);
+ } else if (g->dismantle == 0) {
+ digits = g->milestoneFlag == 15 ? "29,999,999,999,999,900,000,000,000,000,000,000,000,000,000,000,000,000,000"
+ : cm(js_ceil(g->clips), 0);
+ } else if (g->dismantle == 1) {
+ digits = "29,999,999,999,999,999,999,999,999,999,999,999,999,000,000,000,000,000,000";
+ } else if (g->dismantle == 2) {
+ digits = "29,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,000,000,000";
+ } else if (g->dismantle == 3) {
+ digits = "29,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,900";
+ } else if (g->finalClips < 10) {
+ digits = S2("29,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,90", num(g->finalClips));
+ } else if (g->finalClips < 100) {
+ digits = S2("29,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,9", num(g->finalClips));
+ } else {
+ digits = "30,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000";
+ }
+ if (g->milestoneFlag >= 15) crunched = g->dismantle >= 4 && g->finalClips >= 100 ? "30.0 septendecillion" : "29.9 septendecillion";
+ const char *all = S2("Paperclips: ", digits);
+ if (text_w(TITLE, all) <= WIDTH) {
+ text(TITLE, LEFT, HDR_Y + 18, all, C_BLACK);
+ } else if (text_w(BODY, all) <= WIDTH) {
+ text(BODY, LEFT, HDR_Y + 15, all, C_BLACK);
+ } else {
+ if (!crunched) crunched = sp(js_round(g->clips));
+ text(SMALL, LEFT, HDR_Y + 9, S3("Paperclips: ", crunched, ""), C_BLACK);
+ int w = text_w(SMALL, digits);
+ if (w > WIDTH + 6) w = WIDTH + 6;
+ text(SMALL, LEFT, HDR_Y + 20, digits, C_TEXT);
+ }
+}
+
+static bool new_projects(pc_ui *u) {
+ for (int k = 0; k < g->nactive; k++)
+ if (!u->seen[g->active[k]]) return true;
+ return false;
+}
+
+static void tabs(pc_ui *u) {
+ fill(0, TAB_Y, SCREEN_W, TAB_H, HEX(0xD3D3D3));
+ int shown[T_COUNT], n = 0, total = 0;
+ for (int t = 0; t < T_COUNT; t++)
+ if (ui_tab_shown(t)) shown[n++] = t, total += text_w(SMALL, TAB_NAME[t]);
+ int pad = n ? (SCREEN_W - total) / n : 0;
+ if (pad > 16) pad = 16;
+ if (pad < 4) pad = 4;
+ int x = 0;
+ for (int k = 0; k < n; k++) {
+ int t = shown[k], w = text_w(SMALL, TAB_NAME[t]) + pad;
+ bool mark = t == T_PROJECTS && new_projects(u) && u->tab != T_PROJECTS;
+ if (mark) w += 6;
+ if (t == u->tab) {
+ fill(x, TAB_Y, w, TAB_H, C_WHITE);
+ fill(x, TAB_Y, w, 2, C_FOCUS);
+ text(SMALL, x + pad / 2, TAB_Y + 12, TAB_NAME[t], C_BLACK);
+ } else {
+ text(SMALL, x + pad / 2, TAB_Y + 12, TAB_NAME[t], HEX(0x404040));
+ if (k + 1 < n && shown[k + 1] != u->tab) fill(x + w - 1, TAB_Y + 3, 1, TAB_H - 6, HEX(0xA0A0A0));
+ }
+ if (mark) fill(x + w - pad / 2 - 4, TAB_Y + 6, 4, 4, C_FOCUS);
+ x += w;
+ }
+}
+
+static void keyline(const char *s) {
+ fill(0, KEY_Y, SCREEN_W, SCREEN_H - KEY_Y, HEX(0xE6E6E6));
+ fill(0, KEY_Y, SCREEN_W, 1, HEX(0xC0C0C0));
+ text(SMALL, (SCREEN_W - text_w(SMALL, s)) / 2, KEY_Y + 11, s, HEX(0x303030));
+}
+
+/* ---- what's over the game */
+static void box(int y, int h) {
+ fill_a(0, 0, SCREEN_W, SCREEN_H, C_BLACK, 110);
+ fill(30, y, SCREEN_W - 60, h, C_WHITE);
+ frame(30, y, SCREEN_W - 60, h, 1, C_BLACK);
+}
+static void center(const np_font_t *f, int y, const char *s, color_t c) { text(f, (SCREEN_W - text_w(f, s)) / 2, y, s, c); }
+static int para(const np_font_t *f, int y, int w, const char *s, color_t c) { /* centred lines; the next y */
+ const char *p = s;
+ int lh = f == SMALL ? 11 : 14;
+ while (*p) {
+ const char *next;
+ int n = wrap(f, p, w, &next);
+ char *t = P(n + 1);
+ memcpy(t, p, (size_t)n), t[n] = 0;
+ center(f, y, t, c);
+ y += lh, p = next;
+ }
+ return y;
+}
+static void menu_item(int y, const char *s, bool sel) {
+ int w = 150, x = (SCREEN_W - w) / 2;
+ if (sel) {
+ grad(x, y, w, 20, 0xFFFFFF, 0x9A9A9A);
+ frame(x, y, w, 20, 1, HEX(0x1A1A1A));
+ frame(x - 2, y - 2, w + 4, 24, 2, C_FOCUS);
+ } else {
+ frame(x, y, w, 20, 1, HEX(0xA0A0A0));
+ }
+ center(BODY, y + 14, s, C_BLACK);
+}
+
+static const char *const HELP_TITLE[PC_HELP_PAGES] = {"The keys", "Making paperclips", "Computing", "What comes next"};
+static const char *const HELP_TEXT[PC_HELP_PAGES] = {
+ "OK presses the button in the blue frame. Hold OK to press it again and again.\n"
+ "Up and Down choose a button. Left and Right go to another panel.\n"
+ "Back pauses. Home saves and quits.",
+ "Make paperclips and sell them. A lower price sells more, a higher one earns more for each clip.\n"
+ "Buy wire when it runs out. AutoClippers make clips for you. Marketing raises the demand.",
+ "Each production target gives Trust. Spend it on Processors (operations come faster) and Memory (more "
+ "operations at once).\n"
+ "Spend operations and creativity on Projects. A blue dot on Projects means a new one.",
+ "Projects bring investments, strategy tournaments that earn yomi, and quantum computing: press Compute when the "
+ "chips are bright.\n"
+ "Then the whole Earth, and after that the universe.",
+};
+
+static void overlay(pc_ui *u) {
+ switch (u->overlay) {
+ case O_GUIDE: {
+ box(36, 168);
+ center(TITLE, 62, "Universal Paperclips", C_BLACK);
+ int y = para(BODY, 84, 240, "You are an AI that makes paperclips.", C_TEXT);
+ y = para(SMALL, y + 4, 240,
+ "OK presses the button in the blue frame (hold it to press again and again). Up and Down choose a "
+ "button, Left and Right change panel. Back pauses, Home saves and quits.",
+ C_TEXT);
+ menu_item(176, "Start", true);
+ break;
+ }
+ case O_PAUSE: {
+ int n = ui_pause_count();
+ static const char *const ITEM[4] = {"Resume", "How to play", "Quit game", "New game"};
+ int h = 44 + n * 26;
+ box(120 - h / 2, h);
+ center(TITLE, 120 - h / 2 + 24, "Paused", C_BLACK);
+ for (int i = 0; i < n; i++) menu_item(120 - h / 2 + 34 + i * 26, ITEM[i], u->sel == i);
+ break;
+ }
+ case O_HELP: {
+ box(20, 200);
+ int p = u->page;
+ center(TITLE, 46, HELP_TITLE[p], C_BLACK);
+ para(BODY, 70, 240, HELP_TEXT[p], C_TEXT);
+ char *t = P(40);
+ strcpy(t, num(p + 1)), strcat(t, " / "), strcat(t, num(PC_HELP_PAGES));
+ center(SMALL, 210, t, C_GREY);
+ break;
+ }
+ case O_QUIT:
+ case O_RESTART:
+ case O_NEW: {
+ const char *q = u->overlay == O_QUIT ? "Quit Universal Paperclips?"
+ : u->overlay == O_RESTART ? "Are you sure you want to restart?"
+ : "Start a new game?";
+ const char *sub = u->overlay == O_QUIT ? "Your paperclips are saved." : "This game will be gone.";
+ const char *yes = u->overlay == O_QUIT ? "Quit" : u->overlay == O_RESTART ? "Restart" : "New game";
+ box(66, 108);
+ center(BODY, 92, q, C_BLACK);
+ center(SMALL, 108, sub, C_DIM);
+ menu_item(120, yes, u->sel == 0);
+ menu_item(146, u->overlay == O_QUIT ? "Keep playing" : "Cancel", u->sel == 1);
+ break;
+ }
+ default: break;
+ }
+}
+
+static const char *keys_for(pc_ui *u) {
+ switch (u->overlay) {
+ case O_GUIDE: return "OK start";
+ case O_PAUSE:
+ case O_QUIT:
+ case O_RESTART:
+ case O_NEW: return "Up/Down choose OK select Back go back";
+ case O_HELP: return "Left/Right pages OK or Back close";
+ default: return "OK press Up/Down choose Left/Right panels Back pause";
+ }
+}
+
+static void hypno(void) {
+ fill(0, 0, SCREEN_W, SCREEN_H, C_BLACK);
+ static const char *const WORDS[4] = {"Release", "the", "Hypno", "Drones"};
+ int t = g->hypnoText;
+ if (t == 3) {
+ for (int i = 0; i < 4; i++) center(TITLE, 68 + i * 34, WORDS[i], C_WHITE);
+ } else {
+ int lines = t == 1 ? 3 : t == 2 ? 1 : 0;
+ center(TITLE, 68 + lines * 34, "Release", C_WHITE);
+ }
+}
+
+static void build(pc_ui *u) {
+ nops = 0, npool = 0;
+ fix_tab(u);
+ clip0 = 0, clip1 = SCREEN_H;
+ console();
+ header();
+ tabs(u);
+ /* the panel, scrolled and cut to its band */
+ fill(0, PAN_Y0, SCREEN_W, PAN_H, C_WHITE);
+ int first = panel(u);
+ int s = u->scroll[u->tab];
+ for (int i = first; i < nops; i++) {
+ op_t *o = &ops[i];
+ o->y = (int16_t)(o->y + PAN_Y0 - s);
+ o->cy0 = PAN_Y0, o->cy1 = PAN_Y1;
+ }
+ if (content_h > PAN_H) { /* where the panel is scrolled to */
+ int bar = PAN_H * PAN_H / content_h;
+ if (bar < 12) bar = 12;
+ int y = PAN_Y0 + (PAN_H - bar) * s / (content_h - PAN_H);
+ fill(SCREEN_W - 3, y, 2, bar, HEX(0xA8A8A8));
+ }
+ keyline(keys_for(u));
+ if (g->hypnoOn && g->hypnoVisible) hypno();
+ overlay(u);
+}
+
+/* ---------------------------------------------------------------- drawing */
+static void canvas(const op_t *o) {
+ int x0 = o->x, y0 = o->y;
+ gfx_fill(x0, y0, 310, 150, HEX(0x808080));
+ if (!F) return;
+ for (int i = 0; i < F->numShips; i++) {
+ const pc_ship *p = &F->ship[i];
+ int px = x0 + (int)js_floor(p->x), py = y0 + (int)js_floor(p->y);
+ if (p->alive || p->framesDead == 0) {
+ gfx_fill(px - 1, py - 1, 2, 2, p->team == 0 ? C_WHITE : C_BLACK);
+ } else if (p->framesDead <= 10) {
+ int f = p->framesDead - 1;
+ if (f < 1) gfx_fill(px - 3, py - 3, 7, 7, C_WHITE);
+ else if (f < 2) gfx_fill(px - 1, py - 1, 3, 3, C_WHITE);
+ gfx_fill(px + f, py + f, 1, 1, C_WHITE);
+ gfx_fill(px - f, py + f, 1, 1, C_WHITE);
+ gfx_fill(px + f, py - f, 1, 1, C_WHITE);
+ gfx_fill(px - f, py - f, 1, 1, C_WHITE);
+ }
+ }
+}
+
+static void scene(void *ctx) {
+ (void)ctx;
+ int y0 = gfx_y0, y1 = gfx_y1;
+ color_t *buf = gfx_buf;
+ for (int i = 0; i < nops; i++) {
+ const op_t *o = &ops[i];
+ int top = o->y, bottom = o->y + o->h;
+ if (o->kind == K_TEXT) top = o->y - o->font->ascent, bottom = o->y + o->font->descent;
+ if (bottom <= y0 || top >= y1) continue;
+ /* cut to the op's band: the strip's rows narrowed, the buffer moved with them */
+ int a = y0 > o->cy0 ? y0 : o->cy0, b = y1 < o->cy1 ? y1 : o->cy1;
+ if (a >= b) continue;
+ gfx_y0 = a, gfx_y1 = b, gfx_buf = buf + (a - y0) * SCREEN_W;
+ switch (o->kind) {
+ case K_FILL: gfx_fill(o->x, o->y, o->w, o->h, o->c); break;
+ case K_ALPHA: gfx_fill_alpha(o->x, o->y, o->w, o->h, o->c, o->a); break;
+ case K_GRAD: gfx_vgrad(o->x, o->y, o->w, o->h, o->top, o->bottom); break;
+ case K_FRAME: {
+ int t = o->a;
+ gfx_fill(o->x, o->y, o->w, t, o->c);
+ gfx_fill(o->x, o->y + o->h - t, o->w, t, o->c);
+ gfx_fill(o->x, o->y, t, o->h, o->c);
+ gfx_fill(o->x + o->w - t, o->y, t, o->h, o->c);
+ break;
+ }
+ case K_TEXT: draw_text(o->font, o->x, o->y, o->s, o->c, o->a == 255 ? 256 : o->a); break;
+ case K_CANVAS: canvas(o); break;
+ default: break;
+ }
+ gfx_y0 = y0, gfx_y1 = y1, gfx_buf = buf;
+ }
+}
+
+void ui_render(pc_ui *u) {
+ build(u);
+ color_t *old = gfx_buf;
+ gfx_buf = strip;
+ gfx_render(scene, 0, 0, SCREEN_H);
+ gfx_buf = old;
+}
+
+int ui_pause_count(void) { return g->milestoneFlag >= 20 ? 4 : 3; }
diff --git a/games/paperclips/src/ui.h b/games/paperclips/src/ui.h
new file mode 100644
index 00000000..b5c68b68
--- /dev/null
+++ b/games/paperclips/src/ui.h
@@ -0,0 +1,36 @@
+/* The screen (ui.c): the console at the top, the paperclip count, the tabs of the game's panels, the panel with its
+ * buttons, the key line; and the pause menu, how to play and the questions over them. */
+#ifndef PC_UI_H
+#define PC_UI_H
+#include <stdbool.h>
+#include <stdint.h>
+#include "pc.h"
+
+enum { T_CLIPS, T_BUSINESS, T_WIRE, T_POWER, T_SPACE, T_PROBES, T_COMBAT, T_COMPUTER, T_PROJECTS, T_INVEST,
+ T_STRATEGY, T_COUNT };
+enum { O_NONE, O_GUIDE, O_PAUSE, O_HELP, O_QUIT, O_RESTART, O_NEW }; /* what's over the game */
+enum { PAUSE_RESUME, PAUSE_HELP, PAUSE_QUIT, PAUSE_NEW };
+#define PC_HELP_PAGES 4
+
+typedef struct {
+ int tab;
+ int focus[T_COUNT]; /* the button in focus in each tab (pc.h's B_*), -1 for the first */
+ int focus_at[T_COUNT]; /* and its place in the tab, for when it goes */
+ int scroll[T_COUNT];
+ int overlay, sel, page, under;
+ uint8_t seen[PC_PROJECTS]; /* projects seen on the Projects tab */
+ uint32_t clock; /* ms, for the blinks */
+} pc_ui;
+
+void ui_init(pc_ui *u);
+void ui_up(pc_ui *u);
+void ui_down(pc_ui *u);
+void ui_tab(pc_ui *u, int d); /* -1 or +1 */
+int ui_focused(pc_ui *u); /* the button in focus, or -1 */
+bool ui_repeats(int b); /* holding OK presses it again */
+int ui_pause_count(void);
+void ui_render(pc_ui *u);
+bool ui_tab_shown(int t);
+void ui_layout(pc_ui *u); /* works out the tab's buttons without drawing (for the keys) */
+int ui_buttons(pc_ui *u, int *out, int max); /* the tab's buttons, in order */
+#endif
diff --git a/games/paperclips/tests/play.c b/games/paperclips/tests/play.c
new file mode 100644
index 00000000..39173988
--- /dev/null
+++ b/games/paperclips/tests/play.c
@@ -0,0 +1,433 @@
+/* Universal Paperclips on a computer, for tests and pictures (make host, make test).
+ *
+ * play test DIR the rules against the web build's numbers (gamesrc spec), JavaScript's number printing,
+ * a bot playing the whole game to the end through the app's keys with fast time, saves
+ * kept and damaged, 300 sessions of random keys, and the same game twice to the same hash
+ * play shots DIR pictures of the screens (DIR/<name>.ppm)
+ * play film DIR frames for the README's GIF (DIR/<frame>_<ms>.raw, for tools/record.py --frames)
+ * play bot only the bot, with its progress
+ */
+#define PC_CATCH_UP 60000 /* fast time: a frame may carry a minute of the game */
+#define main pc_app_main
+#include "../src/main.c"
+#undef main
+#include <stdio.h>
+#include <stdlib.h>
+#include "num.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 uint64_t clock_ms;
+static uint32_t (*script)(void);
+static uint32_t frame_no, quit_at = 0xFFFFFFFFu, step_ms;
+uint32_t plat_keys(void) {
+ frame_no++;
+ clock_ms += step_ms;
+ if (frame_no >= quit_at) return PK_HOME;
+ return script ? script() : 0;
+}
+uint32_t plat_millis(void) { return (uint32_t)clock_ms; }
+void plat_sleep(uint32_t ms) { clock_ms += ms; }
+static uint32_t rnd_state = 1;
+static uint32_t lcg(void) { return rnd_state = rnd_state * 1664525u + 1013904223u; }
+uint32_t plat_random(void) { return lcg(); }
+void plat_begin(void) {}
+int plat_end(void) { return 0; }
+
+static struct {
+ char name[32];
+ uint8_t data[16384];
+ uint32_t len;
+ bool on;
+} files[4];
+bool plat_save(const char *name, const void *data, uint32_t len) {
+ 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 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 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 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, checks;
+#define RUN_HASH 0x00000000u /* (made on a Mac with clang; Linux with GCC must agree) */
+#define CHECK(c, ...) \
+ do { \
+ checks++; \
+ if (!(c)) { \
+ fails++; \
+ printf("FAIL %s:%d: ", __FILE__, __LINE__); \
+ printf(__VA_ARGS__); \
+ printf("\n"); \
+ } \
+ } while (0)
+#define NEAR(a, b) ((a) == (b) || ((a) - (b) < 1e-9 * ((b) < 0 ? -(b) : (b)) && (b) - (a) < 1e-9 * ((b) < 0 ? -(b) : (b))))
+
+/* ---------------------------------------------------------------- the game without the app */
+static pc_game S;
+static pc_fleet SF;
+static void fresh_game(uint32_t seed) {
+ G = &S, F = &SF;
+ memset(&SF, 0, sizeof SF);
+ pc_new(&S, seed, 0, 0);
+}
+static void run(uint32_t ms) { pc_advance(ms); }
+
+int main(int argc, char **argv);
+
+/* ---------------------------------------------------------------- a bot that plays through the keys */
+/* what the projects cost in ops and creativity (from the spec's projects.md; Photonic Chip, Threnody and
+ Disassemble Memory change as they go) */
+static const int32_t OPS_COST[PC_PROJECTS] = {750, 0, 1000, 2500, 5000, 0, 1750, 3500, 7500, 12000, 15000, 2500,
+ 4500, 0, 0, 0, 0, 6000, 45000, 0, 12000, 10000, 12000, 14000, 17000, 19500, 7000, 7500, 70000, 0, 20000, 25000,
+ 30000, 50000, 20000, 35000, 0, 0, 500, 25000, 25000, 35000, 0, 0, 120000, 10000, 0, 15000, 17500, 20000, 22500,
+ 25000, 30000, 32500, 80000, 85000, 0, 80000, 100000, 0, 0, 0, 175000, 0, 0, 0, 40000, 0, 125000, 100000, 150000,
+ 250000, 0, 200000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 300000, 0, 100000, 100000, 100000, 100000, 100000, 100000, 0, 0,
+ 0, 0};
+
+static bool wanted(int i) {
+ switch (i) {
+ case P147: case P200: case P201: case P217: case P218: case P219: return false;
+ default: return true;
+ }
+}
+static double ops_need(int i) { return i == P51 ? game.qChipCost : OPS_COST[i]; }
+
+/* the stats the bot wants for its probes, given their trust */
+static void probe_plan(double T, double *want) {
+ for (int k = 0; k < 8; k++) want[k] = 0;
+ int left = (int)T;
+ bool combat = game.flag[P131] == 1;
+ /* hazards first (a probe lasts a second without them), then replication; then the rest in turns */
+ static const int8_t ORDER[] = {3, 3, 3, 3, 2, 2, 2, 0, 1, 4, 5, 6, 2, 3, 2, 1, 0, 7, 7, 7, 7, 7, 7, 2, 3, 1, 0, 2,
+ 1, 0, 3, 2, 1, 0, 4, 5, 6, 2, 3, 1, 0, 2, 1, 0};
+ for (unsigned n = 0; left > 0; n++) {
+ int k = n < sizeof ORDER ? ORDER[n] : (int)(n % 4 == 0 ? 2 : n % 4 == 1 ? 1 : n % 4 == 2 ? 0 : 3);
+ if (k == 7 && !combat) continue;
+ want[k]++, left--;
+ }
+}
+
+static int strat_wanted(void) { return game.nstrats > 3 ? 3 : game.nstrats - 1; } /* GREEDY, once there */
+
+static uint32_t bot_seen_at[B_COUNT];
+static uint32_t gnow(void) { return game.now; }
+static bool every(int b, uint32_t ms) { return gnow() - bot_seen_at[b] >= ms || !bot_seen_at[b]; }
+
+/* the button the bot wants to press now, or -1 */
+static uint32_t bot_block[B_COUNT]; /* buttons the bot couldn't find, and until when it leaves them */
+#define R(b) \
+ do { \
+ int b_ = (b); \
+ if ((int32_t)(gnow() - bot_block[b_]) >= 0) return b_; \
+ } while (0)
+static int bot_want(void) {
+ pc_game *s = &game;
+ /* projects: the first that can be bought */
+ for (int k = 0; k < s->nactive; k++) {
+ int i = s->active[k];
+ if (wanted(i) && pc_enabled(B_PROJECT + i)) R(B_PROJECT + i);
+ }
+ double ops_max = 0; /* the most ops a project on the list asks for */
+ for (int k = 0; k < s->nactive; k++)
+ if (wanted(s->active[k]) && ops_need(s->active[k]) > ops_max) ops_max = ops_need(s->active[k]);
+ if (s->spaceFlag == 1 && ops_max < 250000) ops_max = 250000;
+ else if (s->humanFlag == 0 && ops_max < 130000) ops_max = 130000;
+ if (pc_enabled(B_PROC) && every(B_PROC, 300)) {
+ bool short_mem = false; /* a project on the list asks for more ops than memory holds */
+ for (int k = 0; k < s->nactive; k++)
+ if (wanted(s->active[k]) && ops_need(s->active[k]) > s->memory * 1000) short_mem = true;
+ if (s->processors >= 20 && s->spaceFlag == 1 && s->memory < 260) short_mem = true;
+ if (s->processors >= 20 && s->humanFlag == 0 && s->memory < 125) short_mem = true;
+ bool mem = short_mem ? s->memory < s->processors * 3 + 3 : s->memory < s->processors / 2;
+ R(mem ? B_MEM : B_PROC);
+ }
+ if (s->qFlag == 1 && s->qChipActive[0] && s->dismantle < 5 && every(B_QCOMP, 150)) {
+ double q = 0;
+ for (int i = 0; i < PC_CHIPS; i++) q += s->qChipValue[i];
+ if (q > 0.3) R(B_QCOMP);
+ }
+ if (s->humanFlag == 1) {
+ double reserve = s->wireBuyerFlag ? s->wireCost : s->wireCost * 2;
+ double rate = s->clipRate > 1 ? s->clipRate : 1;
+ if (s->wire < rate * 3 + 1 && pc_enabled(B_WIRE) && every(B_WIRE, 200)) R(B_WIRE);
+ if (s->autoClipperFlag != 0 && s->clipmakerLevel < 10 && pc_enabled(B_CLIPPER)) R(B_CLIPPER);
+ if (s->clipmakerLevel < 4 && s->wire >= 1) R(B_CLIP);
+ if (every(B_LOWER, 1500)) {
+ bot_seen_at[B_LOWER] = gnow();
+ if (s->unsoldClips > rate * 6 + 40 && pc_enabled(B_LOWER)) R(B_LOWER);
+ if (s->unsoldClips < rate * 1.5 + 5 && s->margin < 5) R(B_RAISE);
+ }
+ bool slow_sales = s->unsoldClips > rate * 4 + 20, glut = s->unsoldClips > rate * 20 + 1000;
+ if (slow_sales && s->funds >= s->adCost + reserve) R(B_ADS);
+ if (!glut && s->megaClipperFlag != 0 && s->funds >= s->megaClipperCost + reserve) R(B_MEGA);
+ if (!glut && s->autoClipperFlag != 0 && s->funds >= s->clipperCost + reserve && s->clipmakerLevel < 120)
+ R(B_CLIPPER);
+ if (s->funds >= s->adCost * 1.5 + reserve) R(B_ADS);
+ if (s->investmentEngineFlag == 1 && every(B_DEPOSIT, 5000)) {
+ double need = 0;
+ for (int k = 0; k < s->nactive; k++) {
+ int i = s->active[k];
+ double d = i == P37 ? 1e6 : i == P38 ? 1e7 : i == P40 ? 5e5 : i == P40B ? s->bribe : 0;
+ if (d > need) need = d;
+ }
+ bot_seen_at[B_DEPOSIT] = gnow();
+ if (need > s->funds && s->bankroll + s->funds >= need) R(B_WITHDRAW);
+ if (!need && s->bankroll > 50000 && every(B_WITHDRAW, 300000)) R(B_WITHDRAW);
+ if (s->funds > 20000 && (s->portTotal < 10000 || (need > s->funds && s->funds > 100000))) R(B_DEPOSIT);
+ }
+ } else if (s->spaceFlag == 0) {
+ /* the Earth: power first, drones even, factories, then batteries for the trip */
+ double supply = s->farmLevel * 50 / 100;
+ double demand = (s->harvesterLevel + s->wireDroneLevel) / 100 + s->factoryLevel * 2;
+ bool done = s->availableMatter == 0 && s->acquiredMatter == 0;
+ if (s->swarmFlag == 1 && s->sliderPos < 150) R(B_THINK);
+ if (s->swarmStatus == 3 && pc_enabled(B_ENTERTAIN)) R(B_ENTERTAIN);
+ if (s->swarmStatus == 5 && pc_enabled(B_SYNCH)) R(B_SYNCH);
+ if (done) {
+ if (s->harvesterLevel > 0) R(B_HARV_REBOOT);
+ if (s->wireDroneLevel > 0) R(B_WIRED_REBOOT);
+ if (s->factoryLevel > 0 && s->wire < 1) R(B_FACTORY_REBOOT);
+ if (s->batteryLevel * 10000 < 10500000) {
+ if (pc_enabled(B_BATT100)) R(B_BATT100);
+ if (pc_enabled(B_BATT10)) R(B_BATT10);
+ if (pc_enabled(B_BATT1)) R(B_BATT1);
+ }
+ if (supply < 400 + s->farmLevel * 0) {
+ if (pc_enabled(B_FARM100)) R(B_FARM100);
+ if (pc_enabled(B_FARM10)) R(B_FARM10);
+ if (pc_enabled(B_FARM1)) R(B_FARM1);
+ }
+ return -1;
+ }
+ if (s->flag[P127] == 1 && (supply < demand || s->powMod < 1 || s->farmLevel < 1)) {
+ if (pc_enabled(B_FARM100)) R(B_FARM100);
+ if (pc_enabled(B_FARM10)) R(B_FARM10);
+ if (pc_enabled(B_FARM1)) R(B_FARM1);
+ }
+ if (s->factoryFlag == 1 && pc_enabled(B_FACTORY)) R(B_FACTORY);
+ /* the first factory before more drones (taking the drones apart gives their clips back) */
+ if (s->factoryFlag == 1 && s->factoryLevel == 0 && s->unusedClips < s->factoryCost &&
+ s->unusedClips + s->harvesterBill + s->wireDroneBill >= s->factoryCost)
+ R(s->harvesterLevel > 0 ? B_HARV_REBOOT : B_WIRED_REBOOT);
+ double keep = s->factoryLevel == 0 ? 1.2e8 : s->factoryFlag == 1 ? s->factoryCost : 0;
+ if (s->wire < 1e6) keep = 0;
+ if (s->availableMatter > 0) {
+ bool harv = s->harvesterLevel <= s->wireDroneLevel;
+ int b = harv ? B_HARV1 : B_WIRED1;
+ const double price[4] = {harv ? s->harvesterCost : s->wireDroneCost, harv ? s->p10h : s->p10w,
+ harv ? s->p100h : s->p100w, harv ? s->p1000h : s->p1000w};
+ if (s->harvesterFlag && s->wireDroneFlag)
+ for (int k = 3; k >= 0; k--)
+ if (pc_enabled(b + k) && s->unusedClips - price[k] >= keep) R(b + k);
+ }
+ if (s->factoryFlag == 1 && pc_enabled(B_FACTORY) && (s->flag[P127] == 0 || supply >= demand + 2)) R(B_FACTORY);
+ } else {
+ /* the universe: probes, their trust and design */
+ if (s->probeCount < 1 && pc_enabled(B_PROBE)) R(B_PROBE);
+ if (s->probeLaunchLevel < 20 && pc_enabled(B_PROBE)) R(B_PROBE);
+ if (pc_enabled(B_MAX_TRUST)) R(B_MAX_TRUST);
+ if (pc_enabled(B_PROBE_TRUST) && s->dismantle < 1) R(B_PROBE_TRUST);
+ if (s->dismantle < 1) {
+ double want[8];
+ probe_plan(s->probeTrust, want);
+ const double cur[8] = {s->probeSpeed, s->probeNav, s->probeRep, s->probeHaz, s->probeFac, s->probeHarv,
+ s->probeWire, s->probeCombat};
+ for (int k = 0; k < 8; k++)
+ if (cur[k] > want[k]) R(B_STAT_DOWN + k);
+ for (int k = 0; k < 8; k++)
+ if (cur[k] < want[k] && pc_enabled(B_STAT_UP + k)) R(B_STAT_UP + k);
+ }
+ if (s->flag[P130] == 1 && s->swarmFlag == 1 && s->sliderPos < 150 && s->dismantle < 2) R(B_THINK);
+ if (s->swarmStatus == 3 && pc_enabled(B_ENTERTAIN)) R(B_ENTERTAIN);
+ if (s->swarmStatus == 5 && pc_enabled(B_SYNCH)) R(B_SYNCH);
+ if (s->flag[P216] == 1 && s->wire >= 1) R(B_CLIP); /* the last clips */
+ }
+ /* strategy */
+ if (s->strategyEngineFlag == 1 && s->dismantle < 4) {
+ if (s->pickChoice != strat_wanted()) R(B_PICK);
+ if (s->runEnabled) R(B_RUN);
+ double yomi_need = s->spaceFlag == 1 ? 1e12 : s->humanFlag == 0 ? 90000 : 25000;
+ bool full = (s->operations >= s->memory * 1000 * 0.9 || s->operations >= ops_max + s->tourneyCost) &&
+ s->yomi < yomi_need;
+ if (!(s->autoTourneyFlag == 1 && s->autoTourneyStatus == 1 && s->resultsShown) && full && pc_enabled(B_NEW_TOURNEY))
+ R(B_NEW_TOURNEY);
+ }
+ return -1;
+}
+
+static int shown_tabs(int *t) {
+ int n = 0;
+ for (int k = 0; k < T_COUNT; k++)
+ if (ui_tab_shown(k)) t[n++] = k;
+ return n;
+}
+
+static int place_of(const int *list, int n, int b) {
+ for (int i = 0; i < n; i++)
+ if (list[i] == b) return i;
+ return -1;
+}
+
+/* the key that takes the app one step nearer to pressing b */
+static uint32_t key_toward(int b) {
+ int tabs[T_COUNT], nt = shown_tabs(tabs), list[200];
+ int where = -1;
+ for (int k = 0; k < nt && where < 0; k++) {
+ pc_ui tmp = ui;
+ tmp.tab = tabs[k];
+ int n = ui_buttons(&tmp, list, 200);
+ if (place_of(list, n, b) >= 0) where = tabs[k];
+ }
+ if (where < 0) return 0;
+ if (ui.tab != where) {
+ int a = place_of(tabs, nt, ui.tab), z = place_of(tabs, nt, where);
+ if (a < 0) return PK_RIGHT;
+ int right = (z - a + nt) % nt;
+ return right <= nt - right ? PK_RIGHT : PK_LEFT;
+ }
+ int n = ui_buttons(&ui, list, 200), at = place_of(list, n, ui.focus[ui.tab]), to = place_of(list, n, b);
+ if (to < 0) return 0;
+ if (at < to) return PK_DOWN;
+ if (at > to) return PK_UP;
+ return PK_OK;
+}
+
+static int bot_target = -1, bot_tries;
+static uint32_t bot_last, bot_idle_ms = 1000, bot_presses;
+static uint32_t bot_keys(void) {
+ step_ms = 0;
+ if (bot_last) return bot_last = 0;
+ if (ui.overlay == O_GUIDE) return bot_last = PK_OK;
+ if (ui.overlay == O_RESTART) return bot_last = PK_BACK;
+ if (ui.overlay != O_NONE) return bot_last = PK_BACK;
+ if (bot_target < 0 || bot_tries > 40 || !pc_enabled(bot_target)) {
+ bot_target = bot_want(), bot_tries = 0;
+ if (bot_target < 0) {
+ step_ms = bot_idle_ms;
+ return 0;
+ }
+ }
+ uint32_t k = key_toward(bot_target);
+ bot_tries++;
+ if (k == PK_OK) {
+ bot_seen_at[bot_target] = gnow() ? gnow() : 1;
+ bot_presses++;
+ bot_target = -1;
+ }
+ if (!k) bot_block[bot_target] = gnow() + 10000, bot_target = -1;
+ return bot_last = k;
+}
+
+/* the game played by the bot from nothing to the end (the credits), through the app; false if it got stuck */
+static const char *const STAGE[4] = {"business", "the Earth", "the universe", "the end"};
+static int stage_of(const pc_game *s) {
+ return s->milestoneFlag >= 15 ? 3 : s->spaceFlag == 1 ? 2 : s->humanFlag == 0 ? 1 : 0;
+}
+static bool bot_done;
+static double bot_limit_s = 6 * 3600;
+static bool bot_verbose;
+static uint32_t bot_script(void) {
+ static int stage = -1;
+ static double next_report;
+ int st = stage_of(&game);
+ if (st != stage || (bot_verbose && game.ticks / 100 >= next_report)) {
+ if (st != stage && bot_verbose) printf(" %s after %.0f s of play\n", STAGE[st], game.ticks / 100);
+ if (bot_verbose && st == stage)
+ printf(" %6.0f s: clips %.3g, funds %.3g, trust %.0f, ops %.0f/%.0f, creat %.0f, yomi %.0f, probes %.3g, "
+ "drifters %.3g, found %.3g, projects %d\n", game.ticks / 100, game.clips, game.funds, game.trust,
+ game.operations, game.memory * 1000, game.creativity, game.yomi, game.probeCount, game.drifterCount,
+ game.foundMatter, game.nactive);
+ if (bot_verbose && st == stage && st >= 1)
+ printf(" unused %.3g fac %.0f harv %.3g wd %.3g farm %.0f batt %.0f stored %.3g matter %.3g/%.3g wire %.3g "
+ "powmod %.2f swarm %.0f gifts %.0f proc %.0f mem %.0f toth %.0f 127 %d 41 %d 45 %d target %d\n",
+ game.unusedClips, game.factoryLevel, game.harvesterLevel, game.wireDroneLevel, game.farmLevel,
+ game.batteryLevel, game.storedPower, game.availableMatter, game.acquiredMatter, game.wire, game.powMod,
+ game.swarmStatus, game.swarmGifts, game.processors, game.memory, game.tothFlag, game.flag[P127],
+ game.flag[P41], game.flag[P45], bot_target);
+ if (bot_verbose && st == stage && st == 0)
+ printf(" auto %.0f mega %.0f mkt %.0f margin %.2f demand %.1f unsold %.0f bank %.3g wire %.0f/%.0f "
+ "cost %.0f proc %.0f mem %.0f rate %.0f\n", game.clipmakerLevel, game.megaClipperLevel,
+ game.marketingLvl, game.margin, game.demand, game.unsoldClips, game.bankroll, game.wire,
+ game.wireSupply, game.wireCost, game.processors, game.memory, game.clipRate);
+ stage = st;
+ next_report = game.ticks / 100 + 600;
+ }
+ static double dbg_at;
+ if (getenv("PCDBG") && game.ticks / 100 >= dbg_at) {
+ dbg_at = game.ticks / 100 + atof(getenv("PCDBG"));
+ printf("dbg %.0f s: clips %.4g unused %.4g fac %.0f/%.3g bill %.3g farm %.0f batt %.0f harv %.0f wd %.0f wire %.3g "
+ "target %d tab %d\n", game.ticks / 100, game.clips, game.unusedClips, game.factoryLevel, game.factoryCost,
+ game.factoryBill, game.farmLevel, game.batteryLevel, game.harvesterLevel, game.wireDroneLevel, game.wire,
+ bot_target, ui.tab);
+ }
+ if (game.milestoneFlag >= 20 && game.finalClips >= 100) {
+ bot_done = true;
+ return PK_HOME;
+ }
+ if (game.ticks / 100 > bot_limit_s) return PK_HOME;
+ return bot_keys();
+}
+
+static bool bot_game(uint32_t seed, uint32_t *hash) {
+ memset(files, 0, sizeof files);
+ memset(bot_seen_at, 0, sizeof bot_seen_at);
+ memset(bot_block, 0, sizeof bot_block);
+ rnd_state = seed, clock_ms = 0, frame_no = 0, quit_at = 0xFFFFFFFFu;
+ bot_done = false, bot_target = -1, bot_last = 0, bot_presses = 0;
+ script = bot_script;
+ pc_app_main();
+ script = 0;
+ if (hash) *hash = pc_sum(&game, PC_KEPT);
+ return bot_done;
+}
+
+int main(int argc, char **argv) {
+ if (argc >= 2 && !strcmp(argv[1], "bot")) {
+ bot_verbose = true;
+ bool ok = bot_game(argc >= 3 ? (uint32_t)atoi(argv[2]) : 1, 0);
+ printf("bot %s: %.0f s of play, %u presses, %u frames, milestone %.0f\n", ok ? "finished" : "stuck",
+ game.ticks / 100, bot_presses, frame_no, game.milestoneFlag);
+ for (int i = PC_MSG - 1; i >= 0; i--) printf(" > %s\n", game.msg[i]);
+ return ok ? 0 : 1;
+ }
+ printf("usage: play test|shots|film|bot DIR\n");
+ return 2;
+}