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: tests, wiring, a tab less

Mason363c55ad8bdc667
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index a78ff161..a25ede29 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -92,6 +92,7 @@ jobs:
make -C games/turborush host test BUILD_DIR=../../build/data/games/turborush
make -C games/angrybirds host test
make -C games/polytrack host test
+ make -C games/paperclips host test
diff --git a/Makefile b/Makefile
index 035f9642..adab0761 100644
--- a/Makefile
+++ b/Makefile
@@ -18,7 +18,7 @@ PY ?= python3
CARGO ?= cargo
B = build/data
# Conventional games honor a configurable output directory; special layouts are kept local.
-GAMES_REDIRECT = angrybirds balatro blockblast breakout buckshot celeste championisland chess connectfour cookieclicker coreball dinorun donkeykong doodlejump doom dune finalearth2 flappybird flappydunk fnaf fnf g2048 hillclimb hollowknight invaders isaac jetpackjoyride leveldevil minesweeper monkeymart numblocks numpowder numvisuals pacman polytrack portal pvz snake sokoban solitaire sonic stickmanhook subway sudoku suika tombofthemask towerblock turborush twoplayer undertale uno uttt
+GAMES_REDIRECT = angrybirds balatro blockblast breakout buckshot celeste championisland chess connectfour cookieclicker coreball dinorun donkeykong doodlejump doom dune finalearth2 flappybird flappydunk fnaf fnf g2048 hillclimb hollowknight invaders isaac jetpackjoyride leveldevil minesweeper monkeymart numblocks numpowder numvisuals pacman paperclips polytrack portal pvz snake sokoban solitaire sonic stickmanhook subway sudoku suika tombofthemask towerblock turborush twoplayer undertale uno uttt
GAMES_OUT = dinorun
GAME_REDIRECT_ARG = $(if $(filter $(1),$(GAMES_OUT)),OUT=../../$(B)/games/$(1),BUILD_DIR=../../$(B)/games/$(1))
# the RAM the oldest calculator software that runs apps gives them (23.2: 148928 bytes; 25.2 gives 153676).
@@ -29,7 +29,7 @@ RAM_LIMIT = 148928
# custom builds of Epsilon 25.2's source give apps less: 133652 bytes; every game fits there too, in NumPlay and on
# its own (their big buffers are on the stack, 32 KB apart from that RAM, or in the RAM the software has left)
CUSTOM_RAM = 133652
-BIG_APPS = Celeste HollowKnight Undertale ChampionIsland FinalEarth2 NumPowder NumBlocks NumDrive SubwaySurfers FiveNightsAtFreddys Doom BindingOfIsaac HillClimbRacing PlantsVsZombies SonicTheHedgehog CookieClicker LevelDevil JetpackJoyride StickmanHook DonkeyKong Dune MonkeyMart TombOfTheMask UNO FridayNightFunkin TurboRush AngryBirds PolyTrack
+BIG_APPS = Celeste HollowKnight Undertale ChampionIsland FinalEarth2 NumPowder NumBlocks NumDrive SubwaySurfers FiveNightsAtFreddys Doom BindingOfIsaac HillClimbRacing PlantsVsZombies SonicTheHedgehog CookieClicker LevelDevil JetpackJoyride StickmanHook DonkeyKong Dune MonkeyMart TombOfTheMask UNO FridayNightFunkin TurboRush AngryBirds PolyTrack UniversalPaperclips
# the calculator's space for installed apps: from the end of its software (64 KiB aligned) to 64 KiB before the
# end of its 4 MiB slot of flash, 0x903F0000. The software's size sets the start: 0x90200000 is the latest a
# NumPlay counts on (1.94 MB, APP_SPACE); Epsilon 26.4 on an N0120 ends at 0x90180000 (2.44 MB, the
@@ -47,7 +47,7 @@ XL_APP_SPACE = 6291456
# the spaces the customizer website offers (kit.json "spaces"; tools/kit.py links a NumPlay at each)
SPACES = std:$(APP_START):$(APP_END) e26:$(APP_START_26):$(APP_END) xl:$(APP_START):$(XL_END) xl26:$(APP_START_26):$(XL_END)
# big games that must install on their own without Enlarged Extra Apps: make check holds them to APP_SPACE
-APP_SPACE_APPS = HillClimbRacing FridayNightFunkin TurboRush AngryBirds PolyTrack
+APP_SPACE_APPS = HillClimbRacing FridayNightFunkin TurboRush AngryBirds PolyTrack UniversalPaperclips
# Upsilon, the N0110 and N0115's custom software, gives .nwa apps less RAM: NumPlay-Upsilon.nwa has
# the games that fit it (NumPlay draws their text there: see launcher/src/compat.c)
UPSILON_RAM = 107674
@@ -327,6 +327,7 @@ APP_fnf = $(B)/games/fnf/fnf.nwa:FridayNightFunkin.nwa
APP_turborush = $(B)/games/turborush/turborush.nwa:TurboRush.nwa
APP_angrybirds = $(B)/games/angrybirds/angrybirds.nwa:AngryBirds.nwa
APP_polytrack = $(B)/games/polytrack/polytrack.nwa:PolyTrack.nwa
+APP_paperclips = $(B)/games/paperclips/paperclips.nwa:UniversalPaperclips.nwa
# the big games' modules are made from these (the kit)
$(foreach g,$(XL_GAMES),$(eval MOD_$(g) = $$(word 1,$$(subst :, ,$$(APP_$(g))))))
@@ -365,6 +366,7 @@ apps: | $(B)
$(MAKE) -C games/turborush build NWLINK="$(NWLINK)" BUILD_DIR=../../$(B)/games/turborush
$(MAKE) -C games/angrybirds build NWLINK="$(NWLINK)" BUILD_DIR=../../$(B)/games/angrybirds
$(MAKE) -C games/polytrack build NWLINK="$(NWLINK)" BUILD_DIR=../../$(B)/games/polytrack
+ $(MAKE) -C games/paperclips build NWLINK="$(NWLINK)" BUILD_DIR=../../$(B)/games/paperclips
cd games/tetris/tetris && NWLINK="$(NWLINK)" $(CARGO) build --release --quiet
cp $(word 1,$(subst :, ,$(APP_numdash))) $(B)/apps/$(word 2,$(subst :, ,$(APP_numdash)))
cp $(word 1,$(subst :, ,$(APP_crossyroad))) $(B)/apps/$(word 2,$(subst :, ,$(APP_crossyroad)))
@@ -401,6 +403,7 @@ apps: | $(B)
cp $(word 1,$(subst :, ,$(APP_turborush))) $(B)/apps/$(word 2,$(subst :, ,$(APP_turborush)))
cp $(word 1,$(subst :, ,$(APP_angrybirds))) $(B)/apps/$(word 2,$(subst :, ,$(APP_angrybirds)))
cp $(word 1,$(subst :, ,$(APP_polytrack))) $(B)/apps/$(word 2,$(subst :, ,$(APP_polytrack)))
+ cp $(word 1,$(subst :, ,$(APP_paperclips))) $(B)/apps/$(word 2,$(subst :, ,$(APP_paperclips)))
arm-none-eabi-strip --strip-unneeded $(B)/apps/Tetris.nwa
STD_APPS = $(foreach g,$(STD_GAMES),$(B)/apps/$(NWA_$(g)))
@@ -478,5 +481,6 @@ clean:
-$(MAKE) -C games/turborush clean
-$(MAKE) -C games/angrybirds clean
-$(MAKE) -C games/polytrack clean
+ -$(MAKE) -C games/paperclips clean
-cd games/tetris/tetris && $(CARGO) clean
-$(foreach g,$(STD_GAMES),$(MAKE) -C games/$(g) clean;)
diff --git a/games/games.json b/games/games.json
index ae87589b..b54a6064 100644
--- a/games/games.json
+++ b/games/games.json
@@ -1214,5 +1214,26 @@
"repo": "https://github.com/Mason363/NumPlay/tree/main/games/polytrack",
"default": false,
"xl": true
+ },
+ {
+ "id": "paperclips",
+ "rank": 27,
+ "title": "Universal Paperclips",
+ "tagline": "Turn everything into paperclips",
+ "credit": "Based on Universal Paperclips by Frank Lantz",
+ "top": "0x8E96A3",
+ "bottom": "0x24282F",
+ "accent": "0xE9EDF3",
+ "colors": 16,
+ "records": [
+ "paperclips.sav",
+ "paperclips_saves.py"
+ ],
+ "shots": [
+ "docs/shot.png"
+ ],
+ "repo": "https://github.com/Mason363/NumPlay/tree/main/games/paperclips",
+ "default": false,
+ "xl": true
}
]
diff --git a/games/paperclips/src/ui.c b/games/paperclips/src/ui.c
index 7fef04ff..9aec47b1 100644
--- a/games/paperclips/src/ui.c
+++ b/games/paperclips/src/ui.c
@@ -1,7 +1,7 @@
/* 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
+ * Manufacturing), Business, Wire (Wire Production), Power, Space (Space Exploration and 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
@@ -58,6 +58,11 @@ static int text_w(const np_font_t *f, const char *s) {
for (; *s; s++) w += f->glyphs[glyph_at((uint8_t)*s)].advance;
return w;
}
+static int text_wn(const np_font_t *f, const char *s, int n) {
+ int w = 0;
+ for (int i = 0; i < n && s[i]; i++) w += f->glyphs[glyph_at((uint8_t)s[i])].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++) {
@@ -172,9 +177,17 @@ static void heading(const char *s) {
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(const char *s) { /* a line of text; too long, it goes on under, indented */
+ int x = LEFT, w = WIDTH;
+ while (*s) {
+ const char *next;
+ int n = wrap(BODY, s, w, &next);
+ char *t = P(n + 1);
+ memcpy(t, s, (size_t)n), t[n] = 0;
+ text(BODY, x, cy + 11, t, C_TEXT);
+ cy += 14;
+ s = next, x = LEFT + 12, w = WIDTH - 12;
+ }
}
static void line_small(const char *s, color_t c) {
const char *p = s;
@@ -198,6 +211,7 @@ static int button(int id, int x, const char *label) {
text(BODY, x + 6, cy + 12, label, C_BLACK);
} else {
grad(x, cy, w, BTN_H, 0xFFFFFF, 0xD7D7D7);
+ frame(x, cy, w, BTN_H, 1, HEX(0xC4C4C4));
text(BODY, x + 6, cy + 12, label, HEX(0x8C8C8C));
}
if (f) frame(x - 2, cy - 2, w + 4, BTN_H + 4, 2, C_FOCUS);
@@ -332,7 +346,13 @@ static void tab_power(void) {
drone_rows(B_BATT1, "Battery Tower", g->batteryLevel, g->batteryBill, g->batteryCost);
}
+static void tab_probes(void);
+static void space_exploration(void);
static void tab_space(void) {
+ if (!(g->dismantle >= 1 && g->endTimer1 >= 150)) space_exploration();
+ tab_probes();
+}
+static void space_exploration(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");
@@ -624,9 +644,9 @@ bool ui_tab_shown(int t) {
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_SPACE: /* Space Exploration and the probes' design */
+ return g->spaceFlag == 1 && (g->dismantle < 1 || g->endTimer1 < 150);
+ case T_PROBES: return false;
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;
@@ -843,12 +863,18 @@ static void header(void) {
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 {
+ } else if (text_w(SMALL, digits) <= WIDTH) { /* the count spelled out, then its digits */
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 + 9, S2("Paperclips: ", crunched), C_BLACK);
text(SMALL, LEFT, HDR_Y + 20, digits, C_TEXT);
+ } else { /* the digits on two lines, cut after a comma */
+ int n = (int)strlen(digits), cut = 0;
+ for (int i = 0; i < n; i++)
+ if (digits[i] == ',' && text_w(SMALL, "Paperclips: ") + text_wn(SMALL, digits, i + 1) <= WIDTH) cut = i + 1;
+ char *a = P(cut + 1);
+ memcpy(a, digits, (size_t)cut), a[cut] = 0;
+ text(SMALL, LEFT, HDR_Y + 9, S2("Paperclips: ", a), C_BLACK);
+ text(SMALL, LEFT + text_w(SMALL, "Paperclips: "), HDR_Y + 20, digits + cut, C_BLACK);
}
}
@@ -863,9 +889,11 @@ static void tabs(pc_ui *u) {
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]);
+ bool marked = new_projects(u) && u->tab != T_PROJECTS && ui_tab_shown(T_PROJECTS);
+ if (marked) total += 6;
int pad = n ? (SCREEN_W - total) / n : 0;
if (pad > 16) pad = 16;
- if (pad < 4) pad = 4;
+ if (pad < 2) pad = 2;
int x = 0;
for (int k = 0; k < n; k++) {
int t = shown[k], w = text_w(SMALL, TAB_NAME[t]) + pad;
diff --git a/games/paperclips/tests/play.c b/games/paperclips/tests/play.c
index 39173988..f071af98 100644
--- a/games/paperclips/tests/play.c
+++ b/games/paperclips/tests/play.c
@@ -3,9 +3,10 @@
* 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
+ * play docs PICS FILM the README's pictures (PICS/<name>.ppm) and the frames of its GIF (FILM/<frame>_<ms>.raw,
+ * for tools/record.py --frames), from the bot's game
+ * play bot [SEED] only the bot, with its progress
+ * play states DIR saves from the bot's game, for the emulator (DIR/<stage>.sav)
*/
#define PC_CATCH_UP 60000 /* fast time: a frame may carry a minute of the game */
#define main pc_app_main
@@ -87,7 +88,7 @@ static void shot(const char *dir, const char *name) {
}
static int fails, checks;
-#define RUN_HASH 0x00000000u /* (made on a Mac with clang; Linux with GCC must agree) */
+#define RUN_HASH 0x87B339A5u /* (made on a Mac with clang; Linux with GCC must agree) */
#define CHECK(c, ...) \
do { \
checks++; \
@@ -362,6 +363,16 @@ 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 const char *states_dir; /* play states: saves taken from the bot's game, for the emulator */
+static void dump_state(const char *name) {
+ if (!states_dir) return;
+ static uint8_t buf[PC_FILE_SIZE];
+ save_pack(&game, 1, (uint8_t)ui.tab, buf);
+ char path[512];
+ snprintf(path, sizeof path, "%s/%s.sav", states_dir, name);
+ FILE *f = fopen(path, "wb");
+ if (f) fwrite(buf, 1, sizeof buf, f), fclose(f);
+}
static double bot_limit_s = 6 * 3600;
static bool bot_verbose;
static uint32_t bot_script(void) {
@@ -398,7 +409,15 @@ static uint32_t bot_script(void) {
game.factoryBill, game.farmLevel, game.batteryLevel, game.harvesterLevel, game.wireDroneLevel, game.wire,
bot_target, ui.tab);
}
+ static int dumped;
+ if (states_dir) {
+ if (dumped == 0 && game.strategyEngineFlag == 1 && game.investmentEngineFlag == 1) dump_state("business"), dumped++;
+ if (dumped == 1 && game.humanFlag == 0 && game.factoryLevel >= 5) dump_state("earth"), dumped++;
+ if (dumped == 2 && game.spaceFlag == 1 && game.battles > 0 && game.flag[P121]) dump_state("battle"), dumped++;
+ if (dumped == 3 && game.flag[P148] == 1) dump_state("reject"), dumped++;
+ }
if (game.milestoneFlag >= 20 && game.finalClips >= 100) {
+ if (states_dir) dump_state("end");
bot_done = true;
return PK_HOME;
}
@@ -419,15 +438,762 @@ static bool bot_game(uint32_t seed, uint32_t *hash) {
return bot_done;
}
+/* ---------------------------------------------------------------- the rules, against the spec */
+static void press(int b) { CHECK(pc_press(b), "button %d pressed", b); }
+
+static void rules_business(void) {
+ fresh_game(1);
+ CHECK(S.wire == 1000 && S.wireCost == 20 && S.margin == .25 && S.clipperCost == 5 && S.trust == 2 &&
+ S.nextTrust == 3000 && S.processors == 1 && S.memory == 1 && S.funds == 0 && S.adCost == 100,
+ "the start (globals.js)");
+ CHECK(S.availableMatter == 6e27 && S.totalMatter == 3e55 && S.probeCost == 1e17 && S.probeTrustCost == 500 &&
+ S.maxTrust == 20 && S.tourneyCost == 1000, "matter, probes and tournaments at the start");
+ run(10);
+ CHECK(NEAR(S.demand, 3.2), "demand = .8 / .25: %g", S.demand);
+ press(B_CLIP);
+ CHECK(S.clips == 1 && S.wire == 999 && S.unsoldClips == 1 && S.unusedClips == 1, "Make Paperclip: a clip from an inch");
+ press(B_RAISE);
+ CHECK(S.margin == .26, "raise: .26 (%g)", S.margin);
+ press(B_LOWER);
+ CHECK(S.margin == .25, "lower: .25");
+ for (int i = 0; i < 30; i++) pc_press(B_LOWER);
+ CHECK(S.margin == .01 && !pc_enabled(B_LOWER), "the price stops at $.01");
+ S.margin = .25;
+ CHECK(!pc_enabled(B_CLIPPER), "AutoClippers need $5");
+ S.funds = 1000;
+ press(B_CLIPPER);
+ CHECK(S.clipmakerLevel == 1 && S.funds == 995 && NEAR(S.clipperCost, 1.1 + 5), "an AutoClipper, then 1.1^1 + 5");
+ press(B_CLIPPER);
+ CHECK(NEAR(S.clipperCost, 1.21 + 5), "1.1^2 + 5 = %g", S.clipperCost);
+ double c0 = S.clips;
+ S.wire = 1000000;
+ run(1000);
+ CHECK(NEAR(S.clips - c0, 2), "2 AutoClippers: 2 clips a second (%g)", S.clips - c0);
+ S.megaClipperFlag = 1, S.funds = 10000;
+ press(B_MEGA);
+ CHECK(S.megaClipperLevel == 1 && NEAR(S.megaClipperCost, 1070), "a MegaClipper, then 1.07^1 * 1000");
+ c0 = S.clips;
+ run(1000);
+ CHECK(NEAR(S.clips - c0, 2 + 500), "a MegaClipper: 500 a second (%g)", S.clips - c0);
+ S.funds = 1000;
+ press(B_ADS);
+ CHECK(S.marketingLvl == 2 && S.adCost == 200 && S.funds == 900, "Marketing: level 2, then $200");
+ run(10);
+ CHECK(NEAR(S.demand, .8 / .25 * 1.1), "demand x 1.1 a level");
+ S.funds = 100, S.wireCost = 20;
+ double w = S.wire;
+ press(B_WIRE);
+ CHECK(S.wire == w + 1000 && S.funds == 80 && S.wirePurchase == 1 && NEAR(S.wireBasePrice, 20.05), "a spool of wire");
+ /* sales, over a minute: P(sale) = demand / 100 every 100 ms, floor(.7 demand^1.15) each */
+ fresh_game(2);
+ S.unsoldClips = 1e9, S.wire = 0;
+ run(10);
+ double expect = 600 * (S.demand / 100) * js_floor(.7 * js_pow(S.demand, 1.15));
+ run(60000);
+ CHECK(S.clipsSold > expect * .75 && S.clipsSold < expect * 1.25, "sales in a minute: %g, about %g", S.clipsSold, expect);
+ CHECK(NEAR(S.funds, js_floor(S.clipsSold * .25 * 100 + .5) / 100) || (S.funds > S.clipsSold * .249 && S.funds < S.clipsSold * .251),
+ "the money is a quarter a clip");
+ /* trust: 3000, 5000, 8000, 13000 clips (Fibonacci x 1000) */
+ fresh_game(3);
+ S.clips = 2999, run(10);
+ CHECK(S.trust == 2, "no trust before 3000 clips");
+ S.clips = 3000, run(10);
+ CHECK(S.trust == 3 && S.nextTrust == 5000, "trust at 3000, next at 5000");
+ S.clips = 5000, run(10);
+ S.clips = 8000, run(10);
+ CHECK(S.trust == 5 && S.nextTrust == 13000, "then 8000 and 13000");
+ CHECK(strstr(S.msg[0], "TRUST INCREASED") != 0, "its message");
+ /* operations: processors / 10 a tick, up to memory * 1000 */
+ fresh_game(4);
+ S.compFlag = 1;
+ run(50000);
+ CHECK(S.operations >= 499 && S.operations <= 501, "a processor: 10 ops a second (%g)", S.operations);
+ run(60000);
+ CHECK(S.operations == 1000, "memory 1: 1000 at most");
+ S.creativityOn = 1;
+ run(40000);
+ CHECK(S.creativity == 10, "creativity: 1 every 4 s with full ops (%g)", S.creativity);
+ S.trust = 10;
+ press(B_PROC);
+ CHECK(S.processors == 2 && NEAR(S.creativitySpeed, js_log10(2) * js_pow(2, 1.1) + 1), "a processor: creativity speed");
+ press(B_MEM);
+ CHECK(S.memory == 2, "memory");
+ S.trust = 4;
+ CHECK(!pc_enabled(B_PROC) && !pc_enabled(B_MEM), "no trust left: no more");
+ /* milestones */
+ fresh_game(5);
+ S.funds = 5, run(10);
+ CHECK(S.milestoneFlag == 1 && S.autoClipperFlag == 1, "$5: AutoClippers");
+ S.clips = 2000, run(10);
+ CHECK(S.milestoneFlag == 3 && S.compFlag == 1 && S.projectsFlag == 1, "2,000 clips: projects (%g)", S.milestoneFlag);
+ CHECK(!strcmp(S.msg[0], "Trust-Constrained Self-Modification enabled"), "its message: %s", S.msg[0]);
+}
+
+static void rules_projects(void) {
+ fresh_game(6);
+ S.compFlag = 1, S.projectsFlag = 1;
+ run(10);
+ CHECK(S.nactive == 1 && S.active[0] == P42, "RevTracker first");
+ S.clipmakerLevel = 1, S.standardOps = 750;
+ run(10);
+ CHECK(S.nactive == 2 && S.active[1] == P1 && S.uses[P1] == 0, "Improved AutoClippers when there's an AutoClipper");
+ CHECK(pc_enabled(B_PROJECT + P1), "750 ops: it can be bought");
+ press(B_PROJECT + P1);
+ CHECK(S.clipperBoost == 1.25 && S.flag[P1] == 1 && S.nactive == 1 && S.standardOps < 10, "+25%%, ops spent");
+ run(10);
+ CHECK(S.uses[P4] == 0 && S.active[S.nactive - 1] == P4, "then Even Better AutoClippers");
+ S.wirePurchase = 1, S.standardOps = 1750, run(10);
+ press(B_PROJECT + P7);
+ CHECK(S.wireSupply == 1500 && !strcmp(S.msg[0], "Wire extrusion technique improved, 1,500 supply from every spool"),
+ "Improved Wire Extrusion: %s", S.msg[0]);
+ /* Beg for More Wire: when nothing's left, and again */
+ fresh_game(7);
+ S.wire = 0;
+ run(10);
+ bool beg = false;
+ for (int k = 0; k < S.nactive; k++) beg |= S.active[k] == P2;
+ CHECK(beg, "Beg for More Wire when out of everything");
+ press(B_PROJECT + P2);
+ CHECK(S.trust == 1 && S.wire == 1000 && S.uses[P2] == 1, "a spool for a trust, and it can come back");
+ /* the creativity projects and trust */
+ fresh_game(8);
+ S.creativityOn = 1, S.creativity = 250, S.compFlag = 1, S.projectsFlag = 1;
+ run(10);
+ int got = 0;
+ for (int k = 0; k < S.nactive; k++) got += S.active[k] == P13 || S.active[k] == P14 || S.active[k] == P15 ||
+ S.active[k] == P17 || S.active[k] == P19 || S.active[k] == P6;
+ CHECK(got == 6, "Limerick, Lexical Processing, Combinatory Harmonics, Hadwiger, Toth, Donkey Space (%d)", got);
+ press(B_PROJECT + P19);
+ CHECK(S.trust == 3 && S.creativity == 0, "Donkey Space: +1 trust for 250 creat");
+ run(10);
+ bool sm = false;
+ for (int k = 0; k < S.nactive; k++) sm |= S.active[k] == P20;
+ CHECK(sm, "Strategic Modeling after Donkey Space");
+ char t[64];
+ pc_project_price(P51, t, sizeof t);
+ CHECK(!strcmp(t, "(10,000 ops)"), "Photonic Chip's price: %s", t);
+ S.flag[P50] = 1, S.qFlag = 1, S.memory = 20, S.standardOps = 10000, run(10);
+ press(B_PROJECT + P51);
+ pc_project_price(P51, t, sizeof t);
+ CHECK(S.qChipActive[0] == 1 && S.nextQchip == 1 && S.qChipCost == 15000 && !strcmp(t, "(15000 ops)"),
+ "a chip, then 15000 (no commas after, as the page): %s", t);
+ /* the hypnodrones */
+ fresh_game(9);
+ S.flag[P70] = 1, S.trust = 100, S.clipmakerLevel = 50;
+ run(10);
+ S.wire = 123;
+ press(B_PROJECT + P35);
+ CHECK(S.humanFlag == 0 && S.trust == 0 && S.clipmakerLevel == 0 && S.nanoWire == 123 && S.hypnoOn, "Release the HypnoDrones");
+ run(4000);
+ CHECK(!S.hypnoOn && S.hypnoText == 3, "the HypnoDrone screen: 120 blinks of 32 ms");
+}
+
+static void rules_quantum_tourney(void) {
+ fresh_game(10);
+ S.qFlag = 1, S.qChipActive[0] = 1, S.qChipActive[3] = 1, S.compFlag = 1, S.memory = 50;
+ run(1000);
+ double q = js_sin(S.qClock * .1) + js_sin(S.qClock * .4);
+ CHECK(NEAR(S.qClock, 1.0) || (S.qClock > .99 && S.qClock < 1.01), "the quantum clock: .01 a tick (%g)", S.qClock);
+ double ops = S.standardOps;
+ press(B_QCOMP);
+ CHECK(S.standardOps == ops + js_ceil(q * 360) && S.qShown == 2, "Compute: ceil(360 x the chips) ops");
+ S.qChipActive[0] = 0;
+ press(B_QCOMP);
+ CHECK(S.qShown == 1, "no chip: Need Photonic Chips");
+ /* a tournament: RANDOM, A100, B100 */
+ fresh_game(11);
+ S.strategyEngineFlag = 1, S.compFlag = 1, S.memory = 20, S.standardOps = 10000, S.nstrats = 3;
+ run(10);
+ CHECK(!pc_enabled(B_RUN), "Run waits for a tournament");
+ press(B_NEW_TOURNEY);
+ CHECK(S.tourneyInProg == 1 && S.rounds == 9 && S.runEnabled && !pc_enabled(B_NEW_TOURNEY), "a tournament: 3 x 3 rounds");
+ S.aa = 3, S.ab = 1, S.ba = 9, S.bb = 2;
+ S.pickChoice = 1;
+ run(200);
+ press(B_RUN);
+ static const int8_t H[9] = {0, 0, 0, 1, 1, 1, 2, 2, 2}, V[9] = {0, 1, 2, 1, 2, 0, 1, 2, 0};
+ bool order = true;
+ for (int r = 0; r < 9; r++) {
+ order &= S.h == H[r] && S.v == V[r];
+ run(1000);
+ }
+ CHECK(order, "the pairings in the game's order (h, v)");
+ run(200);
+ CHECK(S.tourneyInProg == 0 && S.resultsShown && S.nresults == 3, "the results");
+ double a = S.stratScore[1] - 20 * S.aa - 20 * S.ab; /* A100: aa against A, ab against B */
+ CHECK(a >= 20 * 1 && a <= 20 * 3 && S.stratScore[2] - 20 * S.bb - 20 * S.ba >= 20 * 2, "A100's and B100's scores");
+ int place = 0;
+ for (int i = 0; i < 3; i++)
+ if (S.results[i] == 1) place = i;
+ double boost = 3 - place - 1;
+ if (boost == 0) boost = 1;
+ CHECK(S.yomi == S.stratScore[1] * boost, "yomi: the pick's score x the strategies it beat (%g)", S.yomi);
+ for (int i = 1; i < 3; i++) CHECK(S.stratScore[S.results[i]] <= S.stratScore[S.results[i - 1]], "results in order");
+ /* GREEDY with these payoffs (the biggest is BA): always B */
+ fresh_game(12);
+ S.strategyEngineFlag = 1, S.compFlag = 1, S.memory = 20, S.standardOps = 10000, S.nstrats = 4;
+ run(10);
+ press(B_NEW_TOURNEY);
+ S.aa = 3, S.ab = 1, S.ba = 9, S.bb = 2;
+ press(B_RUN);
+ bool greedy = true;
+ for (int t = 0; t < 16 * 20; t++) {
+ run(50);
+ if (S.h == 3 && S.rCounter > 0) greedy &= S.hMove == 2;
+ }
+ CHECK(greedy, "GREEDY picks B when BA pays most");
+}
+
+static void rules_investments(void) {
+ fresh_game(13);
+ S.investmentEngineFlag = 1, S.funds = 1000;
+ press(B_DEPOSIT);
+ CHECK(S.bankroll == 1000 && S.funds == 0 && S.ledger == -1000, "deposit");
+ run(200);
+ CHECK(S.riskiness == 7 && S.portTotal == 1000, "low risk: 7");
+ run(120000);
+ CHECK(S.nstocks >= 1, "stocks bought in two minutes (%d)", S.nstocks);
+ for (int i = 0; i < S.nstocks; i++)
+ CHECK(S.stocks[i].amount >= 1 && strlen(S.stocks[i].symbol) >= 1 && strlen(S.stocks[i].symbol) <= 4, "a stock");
+ press(B_WITHDRAW);
+ CHECK(S.bankroll == 0 && S.funds > 0, "withdraw the cash");
+ S.yomi = 1000;
+ press(B_INVEST_UP);
+ CHECK(S.investLevel == 1 && S.investUpgradeCost == 658 && S.yomi == 900 && NEAR(S.stockGainThreshold, .51),
+ "an upgrade: then floor(2^e x 100) = %g", S.investUpgradeCost);
+ press(B_RISK);
+ run(100);
+ CHECK(S.riskiness == 5, "medium risk: 5");
+}
+
+static void rules_earth(void) {
+ fresh_game(14);
+ S.humanFlag = 0, S.factoryFlag = 1, S.unusedClips = 1e20;
+ static const double COST[9] = {1e8, 1e9, 9e9, 7.2e10, 5.04e11, 3.024e12, 1.512e13, 6.048e13, 1.2096e14};
+ bool ok = true;
+ for (int i = 0; i < 9; i++) ok &= NEAR(S.factoryCost, COST[i]), press(B_FACTORY);
+ CHECK(ok && S.factoryLevel == 9, "factories: 1e8, then x10, x9, ... x4, then x2");
+ pc_factory_reboot();
+ CHECK(S.factoryLevel == 0 && S.factoryCost == 1e8 && NEAR(S.unusedClips, 1e20), "Disassemble All: the clips back");
+ S.harvesterFlag = S.wireDroneFlag = 1;
+ press(B_HARV1);
+ CHECK(S.harvesterLevel == 1 && NEAR(S.harvesterCost, js_pow(2, 2.25) * 1e6), "a harvester drone: then 2^2.25 x 1e6");
+ press(B_HARV1000);
+ CHECK(S.harvesterLevel == 1001, "+1k");
+ press(B_WIRED10);
+ CHECK(S.wireDroneLevel == 10, "+10 wire drones");
+ /* power: farms 50 MW, factories 200, drones 1 each */
+ fresh_game(15);
+ S.humanFlag = 0, S.farmLevel = 2, S.factoryLevel = 1;
+ run(10);
+ CHECK(NEAR(S.powMod, .5), "1 MW of farms for 2 of factory: half power (%g)", S.powMod);
+ S.farmLevel = 10, S.batteryLevel = 1;
+ run(1000);
+ CHECK(NEAR(S.storedPower, 100 * 3) && S.powMod >= 1, "the extra 3 MW into the battery each tick (%g)", S.storedPower);
+ /* matter into wire */
+ fresh_game(16);
+ S.humanFlag = 0, S.harvesterLevel = 10, S.wireDroneLevel = 10, S.farmLevel = 10, S.wire = 0;
+ run(10);
+ double m = S.acquiredMatter + S.wire;
+ CHECK(NEAR(m, 10 * 26180337.0 * 2), "harvesters: 26180337 g each a tick, x2 with the slider on Work (%g)", m);
+ /* the swarm */
+ fresh_game(17);
+ S.humanFlag = 0, S.swarmFlag = 1, S.harvesterLevel = 500, S.wireDroneLevel = 500, S.farmLevel = 100, S.sliderPos = 100;
+ run(10);
+ CHECK(S.swarmStatus == 0 && S.giftCountdown > 0, "the swarm: active");
+ double bits = 125000 / (js_log(1000) * 1.0);
+ run((uint32_t)(bits * 10) + 100);
+ CHECK(S.swarmGifts == js_round(js_log10(1000) * 100 / 100), "a gift of round(log10(drones) x slider / 100) = %g", S.swarmGifts);
+ S.harvesterLevel = 5000;
+ run(1100000);
+ CHECK(S.disorgFlag == 1 && S.swarmStatus == 5, "uneven drones: disorganized (%g)", S.disorgCounter);
+ S.yomi = 5000;
+ press(B_SYNCH);
+ CHECK(S.disorgFlag == 0 && S.yomi == 0, "Synchronize: 5,000 yomi");
+}
+
+static void rules_space(void) {
+ fresh_game(18);
+ S.spaceFlag = 1, S.humanFlag = 0, S.yomi = 100000;
+ static const double COST[4] = {500, 1385, 2513, 3837};
+ bool ok = true;
+ for (int i = 0; i < 4; i++) ok &= S.probeTrustCost == COST[i], press(B_PROBE_TRUST);
+ CHECK(ok && S.probeTrust == 4, "probe trust: floor((n + 1)^1.47 x 500)");
+ run(10);
+ press(B_STAT_UP + 3), run(10), press(B_STAT_UP + 3), run(10), press(B_STAT_UP + 2), run(10), press(B_STAT_UP + 0), run(10);
+ CHECK(S.probeHaz == 2 && S.probeRep == 1 && S.probeSpeed == 1 && S.probeUsedTrust == 4 && !pc_enabled(B_STAT_UP + 1),
+ "the design: 4 of 4");
+ CHECK(NEAR(S.attackSpeed, .3), "speed raises attack speed by .1");
+ S.unusedClips = 1e30;
+ press(B_PROBE);
+ CHECK(S.probeCount == 1 && S.probeLaunchLevel == 1 && NEAR(S.unusedClips, 1e30 - 1e17), "a probe: 100 quadrillion clips");
+ S.probeCount = 1e6, S.probeHaz = 0;
+ double before = S.probeCount;
+ run(10);
+ CHECK(S.probesLostHaz > before * .009 && S.probesLostHaz < before * .011, "no hazard remediation: 1%% a tick (%g)", S.probesLostHaz);
+ /* drift, and battles */
+ fresh_game(19);
+ S.spaceFlag = 1, S.humanFlag = 0, S.probeCount = 1e9, S.drifterCount = 2e9, S.probeTrust = 20, S.probeHaz = 40;
+ S.probeCombat = 8, S.probeRep = 10, S.unusedClips = 1e40;
+ run(100);
+ CHECK(S.battles == 1 && S.battleFlag == 1, "drifters over a million: a battle");
+ CHECK(S.unitSize > .9e7 && S.unitSize < 1.1e7 && F->numShips == (int)(S.battleLeftShips + S.battleRightShips) && S.battleRightShips <= 200,
+ "a battle: ships of unitSize probes (%g, %d ships)", S.unitSize, F->numShips);
+ run(60000);
+ CHECK(S.probesLostCombat > 0 && S.driftersKilled > 0, "combat 8: both sides lose some (%g, %g)", S.probesLostCombat,
+ S.driftersKilled);
+ CHECK(!strncmp(S.battleName, "Drifter Attack ", 15), "its name: %s", S.battleName);
+ S.flag[P121] = 1, S.battleNameFlag = 1;
+ for (int i = 0; i < 2000 && S.battles; i++) run(100);
+ for (int i = 0; i < 2000 && !S.battles; i++) S.drifterCount = 2e9, run(10);
+ CHECK(strncmp(S.battleName, "Drifter Attack", 14) != 0 && strchr(S.battleName, ' '), "named battles: %s", S.battleName);
+}
+
+static void rules_endings(void) {
+ /* Reject, then the dismantling */
+ fresh_game(20);
+ S.spaceFlag = 1, S.humanFlag = 0, S.compFlag = 1, S.projectsFlag = 1, S.processors = 100, S.memory = 200;
+ S.milestoneFlag = 15, S.standardOps = 200000, S.foundMatter = S.totalMatter, S.availableMatter = 0, S.wire = 0;
+ S.uses[P46] = 0, S.flag[P46] = 1;
+ run(10);
+ for (int p = P140; p <= P146; p++) press(B_PROJECT + p), run(10);
+ int choice = 0;
+ for (int k = 0; k < S.nactive; k++) choice += S.active[k] == P147 || S.active[k] == P148;
+ CHECK(choice == 2, "Accept or Reject");
+ press(B_PROJECT + P148);
+ choice = 0;
+ for (int k = 0; k < S.nactive; k++) choice += S.active[k] == P147 || S.active[k] == P148;
+ CHECK(S.flag[P148] && choice == 0, "Reject: both go");
+ run(10010);
+ press(B_PROJECT + P210);
+ CHECK(S.dismantle == 1 && S.probeCount == 0, "Disassemble the Probes after 10 s");
+ run(3510), S.standardOps = 200000, run(10);
+ press(B_PROJECT + P211);
+ run(3010), S.standardOps = 200000, run(10);
+ press(B_PROJECT + P212);
+ run(1510), S.standardOps = 200000, run(10);
+ press(B_PROJECT + P213);
+ CHECK(S.dismantle == 4 && S.wire == 50, "the strategy engine: 50 inches of wire");
+ run(1010), S.standardOps = 200000, run(10);
+ press(B_PROJECT + P214);
+ run(3010), S.standardOps = 200000, run(10);
+ CHECK(S.wire == 60, "the photonic chips: 10 more inches (%g)", S.wire);
+ press(B_PROJECT + P215);
+ run(1510);
+ press(B_PROJECT + P216);
+ CHECK(S.dismantle == 7 && S.memory == 0 && S.processors == 0 && S.wire == 100, "memory, processors: 100 inches in all");
+ for (int i = 0; i < 100; i++) press(B_CLIP);
+ CHECK(S.finalClips == 100 && S.wire == 0, "the last 100 clips");
+ run(9100);
+ CHECK(S.milestoneFlag == 20 && !strcmp(S.msg[0], "\xA9 2017 Everybody House Games") &&
+ !strcmp(S.msg[2], "a game by Frank Lantz"), "the credits: %s", S.msg[1]);
+ /* Accept: a new universe */
+ fresh_game(21);
+ S.flag[P147] = 1, S.compFlag = 1, S.projectsFlag = 1, S.memory = 400, S.standardOps = 300000;
+ run(10);
+ press(B_PROJECT + P200);
+ CHECK(S.resetPending && S.prestigeU == 1, "The Universe Next Door");
+ pc_new(&S, 5, S.prestigeU, S.prestigeS);
+ run(10);
+ CHECK(NEAR(S.demand, 3.2 * 1.1), "the next universe: demand +10%% (%g)", S.demand);
+ /* the sim level: creativity 10% faster */
+ pc_new(&S, 5, 0, 1);
+ S.compFlag = 1, S.creativityOn = 1, S.standardOps = 1000;
+ run(36400);
+ CHECK(S.creativity == 10, "Sim Level 1: 1 creat every 364 ticks (%g)", S.creativity);
+}
+
+static void numbers(void) {
+ char b[400];
+ static const struct {
+ double v;
+ const char *s;
+ } TS[] = {{0.1 + 0.2, "0.30000000000000004"}, {1e21, "1e+21"}, {123456789012345680000.0, "123456789012345680000"},
+ {1.0 / 3, "0.3333333333333333"}, {5e-7, "5e-7"}, {0.000001, "0.000001"}, {3e55, "3e+55"}, {100, "100"},
+ {-12.5, "-12.5"}, {1.5e300, "1.5e+300"}};
+ for (unsigned i = 0; i < sizeof TS / sizeof TS[0]; i++)
+ CHECK(!strcmp(js_tostring(TS[i].v, b, sizeof b), TS[i].s), "toString %s: %s", TS[i].s, b);
+ CHECK(!strcmp(js_tofixed(1.005, 2, b, sizeof b), "1.00") && !strcmp(js_tofixed(2.5, 0, b, sizeof b), "3") &&
+ !strcmp(js_tofixed(.25, 2, b, sizeof b), "0.25"), "toFixed");
+ CHECK(!strcmp(js_commas(1234567.891, 2, b, sizeof b), "1,234,567.89"), "formatWithCommas: %s", b);
+ CHECK(!strcmp(js_commas(1.5e25, 0, b, sizeof b), "15,000,000,000,000,000,000,000,000"), "with e+: %s", b);
+ CHECK(!strcmp(js_commas(999.99, 0, b, sizeof b), "999"), "cut, not rounded");
+ CHECK(!strcmp(js_spellf(0, b, sizeof b), "0") && !strcmp(js_spellf(5, b, sizeof b), "5.0") &&
+ !strcmp(js_spellf(1234, b, sizeof b), "1.2 thousand"), "spellf small: %s", b);
+ CHECK(!strcmp(js_spellf(3e55, b, sizeof b), "30.0 septendecillion") && !strcmp(js_spellf(6e27, b, sizeof b), "6.0 octillion"),
+ "spellf big: %s", b);
+ CHECK(!strcmp(js_cruncher(1234567, 2, b, sizeof b), "1.23 million") && !strcmp(js_cruncher(12, 2, b, sizeof b), "12"),
+ "numberCruncher: %s", b);
+ CHECK(!strcmp(js_locale(2625, b, sizeof b), "2,625") && !strcmp(js_locale(12345.6789, b, sizeof b), "12,345.679"),
+ "toLocaleString: %s", b);
+ CHECK(!strcmp(js_time(370012, b, sizeof b), "1 hour 1 minute 40 seconds"), "timeCruncher: %s", b);
+ double e = 0;
+ for (int i = 1; i < 2000; i++) {
+ double x = i * 0.37, d = js_log(js_exp(x / 100)) - x / 100;
+ if (d < 0) d = -d;
+ if (d > e) e = d;
+ }
+ CHECK(e < 1e-14, "log(exp(x)) = x (%g)", e);
+ CHECK(js_log10(1000) == 3 && js_pow(2, 10) == 1024 && js_pow(3, .5) > 1.7320508 && js_pow(3, .5) < 1.7320509, "log10, pow");
+ CHECK(js_sin(0) == 0 && js_sin(3.14159265358979) < 1e-14 && js_sin(1.5707963267948966) == 1, "sin");
+}
+/* ---------------------------------------------------------------- the app: keys, the pause, saves */
+static const struct {
+ uint32_t from, to, keys;
+} *keyplan;
+static int nkeyplan;
+static uint32_t plan_keys(void) {
+ uint32_t k = 0;
+ for (int i = 0; i < nkeyplan; i++)
+ if (frame_no >= keyplan[i].from && frame_no < keyplan[i].to) k |= keyplan[i].keys;
+ return k;
+}
+#define PLAN(...) \
+ do { \
+ static const struct { \
+ uint32_t from, to, keys; \
+ } p_[] = {__VA_ARGS__}; \
+ keyplan = (const void *)p_, nkeyplan = (int)(sizeof p_ / sizeof p_[0]); \
+ } while (0)
+static void session(uint32_t frames) {
+ frame_no = 0, quit_at = frames, script = plan_keys, step_ms = 0;
+ pc_app_main();
+ script = 0, quit_at = 0xFFFFFFFFu;
+}
+static bool save_ok(void) {
+ uint32_t n;
+ const uint8_t *d = file_data(PC_SAVE_NAME, &n);
+ return d && pc_file_ok(d, n);
+}
+
+static void app(void) {
+ memset(files, 0, sizeof files);
+ rnd_state = 77, clock_ms = 0;
+ /* the first time: how to play, OK starts; then OK held on Make Paperclip for 2 s (33 ms frames) */
+ PLAN({5, 6, PK_OK}, {10, 70, PK_OK});
+ session(80);
+ CHECK(save_ok(), "saved on Home");
+ uint32_t n;
+ CHECK(file_data(PC_COPY_NAME, &n) && n > PC_FILE_SIZE, "and copied into paperclips_saves.py");
+ CHECK(game.clips >= 15 && game.clips <= 30, "OK held: a clip, then one every 70 ms after 400 ms (%g)", game.clips);
+ double clips = game.clips;
+ /* again: no guide; Back pauses, How to play, its pages, back; Quit game asks, then quits */
+ PLAN({5, 6, PK_BACK}, {10, 11, PK_DOWN}, {15, 16, PK_OK}, {20, 21, PK_RIGHT}, {25, 26, PK_RIGHT}, {30, 31, PK_BACK},
+ {35, 36, PK_DOWN}, {40, 41, PK_OK}, {45, 46, PK_OK});
+ session(400);
+ CHECK(frame_no < 60, "Quit game, then Quit: out (%u)", frame_no);
+ CHECK(game.clips == clips && save_ok(), "the clips kept (%g)", game.clips);
+ /* the tabs: Right goes on to Business, Left comes back */
+ PLAN({5, 6, PK_RIGHT});
+ session(10);
+ CHECK(ui.tab == T_BUSINESS, "Right: the next tab");
+ PLAN({5, 6, PK_LEFT}, {8, 9, PK_LEFT});
+ session(12);
+ CHECK(ui.tab == T_BUSINESS, "Left twice: round to the last tab (%d)", ui.tab);
+ /* Quit game, then Keep playing: still in */
+ PLAN({5, 6, PK_BACK}, {10, 11, PK_DOWN}, {12, 13, PK_DOWN}, {15, 16, PK_OK}, {20, 21, PK_DOWN}, {25, 26, PK_OK},
+ {30, 31, PK_BACK});
+ session(60);
+ CHECK(frame_no == 60 && ui.overlay == O_NONE, "Keep playing, then Back: playing again");
+}
+
+static uint8_t keep_main[PC_FILE_SIZE + 16], keep_copy[16384];
+static uint32_t keep_main_n, keep_copy_n;
+static void keep(void) {
+ memcpy(keep_main, file_data(PC_SAVE_NAME, &keep_main_n), keep_main_n);
+ memcpy(keep_copy, file_data(PC_COPY_NAME, &keep_copy_n), keep_copy_n);
+}
+static void restore(void) {
+ memset(files, 0, sizeof files);
+ plat_save(PC_SAVE_NAME, keep_main, keep_main_n);
+ plat_save(PC_COPY_NAME, keep_copy, keep_copy_n);
+}
+static void resum(uint8_t *f) {
+ uint32_t sum = pc_sum(f, PC_FILE_SIZE - 4);
+ memcpy(f + PC_FILE_SIZE - 4, &sum, 4);
+}
+
+static void saves(void) {
+ memset(files, 0, sizeof files);
+ rnd_state = 5, clock_ms = 0;
+ PLAN({5, 6, PK_OK}, {10, 200, PK_OK});
+ session(220);
+ double clips = game.clips;
+ CHECK(clips > 50 && save_ok(), "a game saved");
+ keep();
+ nkeyplan = 0;
+ /* damaged: the copy is read, and written back */
+ restore();
+ uint32_t n;
+ uint8_t *d = file_data(PC_SAVE_NAME, &n);
+ d[200] ^= 0x40;
+ session(10);
+ CHECK(game.clips == clips && save_ok(), "a damaged save: from the copy (%g)", game.clips);
+ restore();
+ forget(PC_SAVE_NAME);
+ session(10);
+ CHECK(game.clips == clips && save_ok(), "no save, the copy: from it");
+ restore();
+ d = file_data(PC_SAVE_NAME, &n);
+ files[0].len = n - 1;
+ for (int i = 0; i < 4; i++)
+ if (files[i].on && !strcmp(files[i].name, PC_SAVE_NAME)) files[i].len = n - 1;
+ session(10);
+ CHECK(game.clips == clips, "cut short: from the copy");
+ /* both damaged: a fresh start, with how to play */
+ restore();
+ file_data(PC_SAVE_NAME, &n)[100] ^= 1;
+ uint8_t *c = file_data(PC_COPY_NAME, &n);
+ c[n / 2] = '!';
+ session(10);
+ CHECK(game.clips == 0 && ui.overlay == O_GUIDE, "both damaged: a new game (%g)", game.clips);
+ /* a sum that's right over numbers that aren't: not read */
+ restore();
+ forget(PC_COPY_NAME);
+ d = file_data(PC_SAVE_NAME, &n);
+ pc_game *s = (pc_game *)(void *)(keep_main + sizeof(pc_head));
+ (void)s;
+ int32_t bad = 99;
+ memcpy(d + sizeof(pc_head) + offsetof(pc_game, nstrats), &bad, 4);
+ resum(d);
+ CHECK(!pc_file_ok(d, n), "a strategy past the eight: refused");
+ session(10);
+ CHECK(game.clips == 0, "and a new game instead");
+ restore();
+ forget(PC_COPY_NAME);
+ d = file_data(PC_SAVE_NAME, &n);
+ uint16_t v = PC_VERSION + 1;
+ memcpy(d + 4, &v, 2);
+ resum(d);
+ session(10);
+ CHECK(game.clips == 0, "another version: not read");
+ restore();
+ forget(PC_COPY_NAME);
+ d = file_data(PC_SAVE_NAME, &n);
+ uint8_t idx = 200;
+ memcpy(d + sizeof(pc_head) + offsetof(pc_game, active), &idx, 1);
+ memcpy(d + sizeof(pc_head) + offsetof(pc_game, nactive), &(int32_t){1}, 4);
+ resum(d);
+ CHECK(!pc_file_ok(d, n), "a project past the 96: refused");
+ /* a full storage: the game goes on */
+ memset(files, 0, sizeof files);
+ for (int i = 0; i < 4; i++) {
+ char name[16];
+ snprintf(name, sizeof name, "full%d.py", i);
+ plat_save(name, "x", 1);
+ }
+ PLAN({5, 6, PK_OK}, {10, 40, PK_OK});
+ session(50);
+ CHECK(game.clips > 0 && !file_data(PC_SAVE_NAME, &n), "no room to save: it plays on");
+}
+
+/* ---------------------------------------------------------------- random keys */
+#define FUZZ_STATES 6
+static uint8_t fuzz_state[FUZZ_STATES][PC_FILE_SIZE];
+static int fuzz_states;
+static uint32_t fuzz_rng;
+static uint32_t fuzz_next(void) { return fuzz_rng = fuzz_rng * 1103515245u + 12345u; }
+static uint32_t fuzz_held, fuzz_until;
+static uint32_t fuzz_keys(void) {
+ step_ms = (fuzz_next() >> 16) % 8 == 0 ? (fuzz_next() >> 8) % 20000 : (fuzz_next() >> 8) % 200;
+ if (frame_no >= fuzz_until) {
+ static const uint32_t K[] = {PK_OK, PK_UP, PK_DOWN, PK_LEFT, PK_RIGHT, PK_BACK, PK_OK, PK_DOWN, PK_OK, 0, 0};
+ fuzz_held = K[(fuzz_next() >> 12) % (sizeof K / sizeof K[0])];
+ if ((fuzz_next() >> 10) % 6 == 0) fuzz_held |= K[(fuzz_next() >> 14) % 9];
+ fuzz_until = frame_no + 1 + (fuzz_next() >> 12) % 12;
+ }
+ return fuzz_held;
+}
+static void fuzz(int sessions) {
+ int bad = 0;
+ for (int k = 0; k < sessions; k++) {
+ memset(files, 0, sizeof files);
+ fuzz_rng = 0x5EED + (uint32_t)k * 7919u, rnd_state = (uint32_t)k, clock_ms = 0;
+ int from = k % (fuzz_states + 1);
+ if (from < fuzz_states) plat_save(PC_SAVE_NAME, fuzz_state[from], PC_FILE_SIZE);
+ frame_no = 0, quit_at = 250 + (uint32_t)(k % 7) * 50, script = fuzz_keys, fuzz_until = 0;
+ pc_app_main();
+ script = 0;
+ if (!pc_game_ok(&game) || !save_ok()) bad++;
+ }
+ quit_at = 0xFFFFFFFFu;
+ CHECK(!bad, "%d random-key sessions: every save good (%d bad)", sessions, bad);
+}
+
+/* ---------------------------------------------------------------- the bot's game, to the end */
+static void take_states(void) {
+ static int taken;
+ pc_game *s = &game;
+ bool now = (taken == 0 && s->strategyEngineFlag == 1) || (taken == 1 && s->humanFlag == 0 && s->factoryLevel >= 3) ||
+ (taken == 2 && s->spaceFlag == 1 && s->probeCount > 1000) || (taken == 3 && s->battles > 0) ||
+ (taken == 4 && s->flag[P148] == 1) || (taken == 5 && s->milestoneFlag >= 20);
+ if (now && fuzz_states < FUZZ_STATES) save_pack(s, 1, (uint8_t)ui.tab, fuzz_state[fuzz_states++]), taken++;
+}
+
+static void whole_game(void) {
+ void (*old)(void) = on_frame;
+ on_frame = take_states;
+ uint32_t h1, h2;
+ bool done = bot_game(1, &h1);
+ on_frame = old;
+ double t1 = game.ticks / 100;
+ CHECK(done, "the bot plays the whole game to the credits (%.0f s of play, milestone %.0f)", t1, game.milestoneFlag);
+ CHECK(game.milestoneFlag == 20 && game.finalClips >= 100 && game.dismantle == 7 && game.flag[P148] == 1,
+ "the end: rejected the drift, took everything apart, made the last clips");
+ bool credits = false;
+ for (int i = 0; i < PC_MSG; i++) credits |= !strcmp(game.msg[i], "a game by Frank Lantz");
+ CHECK(credits, "the credits in the console");
+ CHECK(fuzz_states == FUZZ_STATES, "states kept for the random keys (%d)", fuzz_states);
+ printf(" the whole game in %.0f s of play (%.1f h), %u presses, its hash %08X\n", t1, t1 / 3600, bot_presses, h1);
+ /* twenty minutes of it again, twice: the same bits */
+ bot_limit_s = 1200;
+ bot_game(1, &h2);
+ uint32_t h3;
+ bot_game(1, &h3);
+ bot_limit_s = 6 * 3600;
+ CHECK(h2 == h3, "the same game twice: %08X and %08X", h2, h3);
+ CHECK(h1 == RUN_HASH, "the whole game's hash, as on every computer: %08X", h1);
+}
+
+/* ---------------------------------------------------------------- the README's pictures and film, from the bot's game */
+static const char *pic_dir;
+static bool drawing;
+static void view(int tab) { /* the screen with another tab open (the app's own tab put back after) */
+ int old = ui.tab;
+ drawing = true;
+ if (tab >= 0 && ui_tab_shown(tab)) ui.tab = tab;
+ ui_render(&ui);
+ ui.tab = old;
+ drawing = false;
+}
+static bool pic_done[16];
+static const struct {
+ const char *name;
+ int tab;
+} PICS[] = {{"shot", T_PROJECTS}, {"business", T_BUSINESS}, {"computer", T_COMPUTER}, {"strategy", T_STRATEGY},
+ {"invest", T_INVEST}, {"hypno", -1}, {"earth", T_CLIPS}, {"wire", T_WIRE}, {"power", T_POWER},
+ {"swarm", T_COMPUTER}, {"space", T_SPACE}, {"probes", T_SPACE}, {"combat", T_COMBAT}, {"drift", T_PROJECTS},
+ {"end", T_CLIPS}};
+static bool pic_when(int i) {
+ pc_game *s = &game;
+ switch (i) {
+ case 0: return s->humanFlag == 1 && s->nactive >= 4 && s->creativityOn && s->qFlag && s->milestoneFlag >= 5;
+ case 1: return s->humanFlag == 1 && s->revPerSecFlag && s->marketingLvl >= 6 && s->unsoldClips > 0;
+ case 2: return s->humanFlag == 1 && s->nextQchip >= 4 && s->qShown == 2 && s->qFade > .8;
+ case 3: return s->humanFlag == 1 && s->resultsShown && s->nstrats >= 6;
+ case 4: return s->humanFlag == 1 && s->nstocks >= 4;
+ case 5: return s->hypnoOn && s->hypnoVisible && s->hypnoText == 3;
+ case 6: return s->humanFlag == 0 && s->spaceFlag == 0 && s->factoryLevel >= 12 && s->tothFlag;
+ case 7: return s->humanFlag == 0 && s->spaceFlag == 0 && s->harvesterLevel >= 600 && s->maps > 0;
+ case 8: return s->humanFlag == 0 && s->spaceFlag == 0 && s->farmLevel >= 40 && s->batteryLevel >= 1;
+ case 9: return s->humanFlag == 0 && s->swarmFlag && s->swarmStatus == 0 && s->spaceFlag == 0 && s->swarmGifts > 0;
+ case 10: return s->spaceFlag == 1 && s->probesLostDrift > 1000 && s->probesLostCombat > 0 && s->dismantle == 0;
+ case 11: return s->spaceFlag == 1 && s->flag[P131] && s->probeTrust >= 20 && s->dismantle == 0;
+ case 12: return s->battles > 0 && s->battleNameFlag && F->numLeft > 60 && F->numRight > 60 && s->battleResult == 0;
+ case 13:
+ for (int k = 0; k < s->nactive; k++)
+ if (s->active[k] == P143) return true;
+ return false;
+ default: return s->milestoneFlag >= 20;
+ }
+}
+static void pictures(void) {
+ if (drawing || !pic_dir) return;
+ for (unsigned i = 0; i < sizeof PICS / sizeof PICS[0]; i++)
+ if (!pic_done[i] && pic_when((int)i)) {
+ view(PICS[i].tab);
+ shot(pic_dir, PICS[i].name);
+ pic_done[i] = true;
+ }
+}
+
+/* the film: a few seconds of each part of the game, at 15 frames a second */
+static const char *film_dir;
+static int film_part = -1, film_tab;
+static uint32_t film_end, film_t, film_frames;
+static bool film_when(int part) {
+ pc_game *s = &game;
+ switch (part) {
+ case 0: return true;
+ case 1: return s->humanFlag == 1 && s->tourneyInProg && s->nstrats >= 4 && s->currentRound >= 2;
+ case 2: return s->hypnoOn;
+ case 3: return s->battles > 0 && s->battleNameFlag && F->numLeft > 100 && F->numRight > 100;
+ default: return s->flag[P216] == 1 && s->wire >= 60;
+ }
+}
+static const int FILM_TAB[5] = {T_CLIPS, T_STRATEGY, -1, T_COMBAT, T_CLIPS};
+static const uint32_t FILM_MS[5] = {6000, 3500, 4000, 4500, 5000};
+static int film_next;
+static void filming(void) {
+ if (drawing || !film_dir) return;
+ if (film_part < 0 && film_next < 5 && film_when(film_next))
+ film_part = film_next++, film_tab = FILM_TAB[film_part], film_end = game.now + FILM_MS[film_part];
+ if (film_part < 0) return;
+ if (!(frame_no & 1)) {
+ view(film_tab);
+ char path[512];
+ snprintf(path, sizeof path, "%s/%06u_%06u.raw", film_dir, film_frames++, film_t);
+ FILE *f = fopen(path, "wb");
+ if (!f) exit(2);
+ fwrite(fb, 2, 320 * 240, f);
+ fclose(f);
+ film_t += 66;
+ }
+ if ((int32_t)(game.now - film_end) >= 0) film_part = -1;
+}
+static uint32_t film_script(void) {
+ uint32_t k = bot_script();
+ if (film_part >= 0) step_ms = 0;
+ return k;
+}
+static void docs_frame(void) {
+ pictures();
+ filming();
+}
+static void docs(const char *pics, const char *film) {
+ pic_dir = pics, film_dir = film;
+ on_frame = docs_frame;
+ memset(files, 0, sizeof files);
+ memset(bot_seen_at, 0, sizeof bot_seen_at);
+ rnd_state = 1, clock_ms = 0, frame_no = 0, quit_at = 0xFFFFFFFFu;
+ bot_done = false, bot_target = -1, bot_last = 0;
+ script = film_script;
+ pc_app_main();
+ on_frame = 0, script = 0;
+ for (unsigned i = 0; i < sizeof PICS / sizeof PICS[0]; i++)
+ if (!pic_done[i]) printf("no picture: %s\n", PICS[i].name);
+}
+
int main(int argc, char **argv) {
+ if (argc >= 4 && !strcmp(argv[1], "docs")) {
+ docs(argv[2], argv[3]);
+ return 0;
+ }
+ if (argc >= 3 && !strcmp(argv[1], "states")) {
+ states_dir = argv[2];
+ return bot_game(1, 0) ? 0 : 1;
+ }
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);
+ printf("bot %s: %.0f s of play, %u presses, %u frames, milestone %.0f, hash %08X\n", ok ? "finished" : "stuck",
+ game.ticks / 100, bot_presses, frame_no, game.milestoneFlag, pc_sum(&game, PC_KEPT));
for (int i = PC_MSG - 1; i >= 0; i--) printf(" > %s\n", game.msg[i]);
return ok ? 0 : 1;
}
+ if (argc >= 3 && !strcmp(argv[1], "test")) {
+ printf("rules\n");
+ rules_business(), rules_projects(), rules_quantum_tourney(), rules_investments(), rules_earth(), rules_space();
+ rules_endings(), numbers();
+ printf("the app\n");
+ app(), saves();
+ printf("the whole game\n");
+ whole_game();
+ printf("random keys\n");
+ fuzz(300);
+ printf("%d checks, %d failed\n", checks, fails);
+ return fails ? 1 : 0;
+ }
printf("usage: play test|shots|film|bot DIR\n");
return 2;
}
diff --git a/tests/test_games.py b/tests/test_games.py
index 1ae7ea40..8a876812 100644
--- a/tests/test_games.py
+++ b/tests/test_games.py
@@ -268,6 +268,11 @@ GAMES = {
"polytrack": ("PolyTrack.nwa", "polytrack.sav",
presses((1500, "ok"), (2200, "ok"), (2900, "ok"), (3600, "ok"), (4300, "ok"), (9500, "back"))
+ [(5000, 9000, K["up"])], 11000, presses((1500, "ok"), (2200, "ok")) + [(3000, 6500, K["up"])], 7500),
+ # how to play (the first time: OK starts), then Make Paperclip held, Back to pause (that saves); Home keeps the
+ # clips. Next time, from the copy, no guide: a clip more, quit with On/Off
+ "paperclips": ("UniversalPaperclips.nwa", "paperclips.sav",
+ presses((1500, "ok"), (4500, "back")) + [(2200, 4000, K["ok"])], 6000,
+ presses((1500, "ok"), (2000, "ok")), 4000),
# a level, its message, then play; then back to the level select
"portal": ("PortalReturns.nwa", "portal.sav",
presses((1500, "ok"), (2500, "ok"), (3500, "ok")) + every(4000, 7000, 400, "right", 300), 8000,
@@ -335,7 +340,17 @@ COPIES = {"championisland": "champion_saves.py", "numblocks": "numblocks_saves.p
"donkeykong": "donkeykong_saves.py", "dune": "dune_saves.py", "monkeymart": "monkeymart_saves.py",
"tombofthemask": "tombofthemask_saves.py", "uno": "uno_saves.py",
"fnf": "fnf_saves.py", "turborush": "turborush_saves.py",
- "angrybirds": "angrybirds_saves.py", "polytrack": "polytrack_saves.py"}
+ "angrybirds": "angrybirds_saves.py", "polytrack": "polytrack_saves.py", "paperclips": "paperclips_saves.py"}
+
+
+def paperclips_ok(v):
+ """Universal Paperclips' save: "UPC1", version 1, 3400 bytes, an FNV-1a sum of the rest at the end"""
+ if len(v) != 3400 or v[:4] != b"UPC1" or struct.unpack_from("<HH", v, 4) != (1, 3400):
+ return False
+ h = 2166136261
+ for x in v[:-4]:
+ h = (h ^ x) * 16777619 & 0xFFFFFFFF
+ return struct.unpack_from("<I", v, len(v) - 4)[0] == h
def polytrack_ok(v):
@@ -514,6 +529,10 @@ CHECKS = {
# after the reinstall, from the copy, the same
"polytrack": (lambda v: polytrack_ok(v) and v[763] == 1 and v[760] == 0,
lambda v: polytrack_ok(v) and v[763] == 1),
+ # "UPC1", version 1, its size (3400): how to play shown (guide at 8), clips made (the first number, at 12); after the
+ # reinstall, from the copy, one more
+ "paperclips": (lambda v: paperclips_ok(v) and v[8] == 1 and struct.unpack_from("<d", v, 12)[0] >= 10,
+ lambda v: paperclips_ok(v) and v[8] == 1 and struct.unpack_from("<d", v, 12)[0] >= 11),
"breakout": (lambda v: len(v) == 12 and v[:2] == b"B\2" and v[4] == 1 and struct.unpack_from("<I", v, 8)[0] >= 10,
lambda v: v[4] == 0 and struct.unpack_from("<I", v, 8)[0] >= 1), # best kept, shake off
# Air Hockey and the easy computer chosen; then the normal computer and Tanks, the tallies of the six games kept
diff --git a/tools/release.py b/tools/release.py
index 8e8398c4..0cd046ed 100644
--- a/tools/release.py
+++ b/tools/release.py
@@ -53,6 +53,7 @@ ALONE = {
"turborush": ("Turbo Rush", "an arcade racer by Bobydeluxe: six circuits, a Grand Prix, single races and Free Ride"),
"angrybirds": ("Angry Birds", "the 429 levels of the Chrome edition, its seven birds and the Mighty Eagle"),
"polytrack": ("PolyTrack", "Kodub's low-poly time trial racer: its 17 tracks, loops, wall rides and jumps, against your ghost"),
+ "paperclips": ("Universal Paperclips", "Frank Lantz's game of an AI making paperclips: business, the Earth, then the universe"),
}
VARIANTS = [
("NumPlay-Invisible.nwa", "The same app, hidden: a blank icon with no name"),