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.

Balatro CE: a 6 KB stack, and a player for long tests

Mason363e7cb5e318411
diff --git a/games/balatro/ce/src/archive.S b/games/balatro/ce/src/archive.S
index 4964cc38..e2ce6fb9 100644
--- a/games/balatro/ce/src/archive.S
+++ b/games/balatro/ce/src/archive.S
@@ -29,3 +29,29 @@ _ce_archive:
pop iy
pop ix
ret
+
+/* void ce_on_stack(void (*f)(void), void *top): f called with the stack from top down (the game needs more than the
+ * OS's 4 KB: a Blueprint copying a Blueprint... goes deep), the OS's stack back after. */
+ .section .text.boot.ce_on_stack, "ax", @progbits
+ .global _ce_on_stack
+ .type _ce_on_stack, @function
+_ce_on_stack:
+ pop de
+ pop hl
+ pop bc
+ push bc
+ push hl
+ push de
+ push ix
+ ld (.L.sp), sp
+ ld sp, bc
+ call .L.call
+ ld sp, (.L.sp)
+ pop ix
+ ret
+.L.call:
+ jp (hl)
+
+ .section .bss.ce_on_stack, "aw", @nobits
+.L.sp:
+ .skip 3
diff --git a/games/balatro/ce/src/ce_main.c b/games/balatro/ce/src/ce_main.c
index 8a932169..ffa2a246 100644
--- a/games/balatro/ce/src/ce_main.c
+++ b/games/balatro/ce/src/ce_main.c
@@ -140,6 +140,9 @@ int np_end(void) { return 0; }
#define BOOTDATA __attribute__((section(".rodata.boot")))
extern uint8_t ce_vram_lo[], ce_vram_hi[], ce_pshadow_lo[], ce_pshadow_hi[], ce_bssx_lo[], ce_bssx_hi[];
void ce_archive(const char *op1);
+void ce_on_stack(void (*f)(void), void *top);
+static uint8_t stack[6144]; /* the game's (see archive.S) */
+static void game(void) { balatro_main(); }
/* the code's appvars (where each goes), then the others */
#define NCODE 4
@@ -200,7 +203,7 @@ BOOT int main(void) {
int ok = ce_art_init() && ce_bg_init();
if (ok) {
ce_set_palette(0, CE_NSTATIC, ce_palette);
- balatro_main();
+ ce_on_stack(game, stack + sizeof stack);
}
/* the OS's screen, its pixel shadow cleared */
memset((void *)0xD40000, 255, 320 * 240 * 2);
diff --git a/games/balatro/ce/src/gfx_ce.c b/games/balatro/ce/src/gfx_ce.c
index fbe70ed6..dd82094c 100644
--- a/games/balatro/ce/src/gfx_ce.c
+++ b/games/balatro/ce/src/gfx_ce.c
@@ -15,7 +15,7 @@
#define SH 16
#define NSTRIP (SCR_H / SH)
-#define MAXI 300
+#define MAXI 360
#define MAXCLIP 16
#define NDYN 16
/* the fonts, read in the archive (BALLUT) */
@@ -38,7 +38,7 @@ typedef struct {
} item_t;
static item_t items[MAXI];
-static char strbuf[4096];
+static char strbuf[2560]; /* (a frame takes under 1 KB: the How to play page) */
static int strpos, nitems;
static int16_t clips[MAXCLIP][4];
static int nclips, cur_clip;
@@ -648,7 +648,16 @@ static unsigned item_hash(const item_t *it) {
return h;
}
+#ifdef NP_HOST
+/* how full the frame's lists get (tools/host.c prints it) */
+int ce_max_items, ce_max_str;
+#endif
+
void g_end(void) {
+#ifdef NP_HOST
+ if (nitems > ce_max_items) ce_max_items = nitems;
+ if (strpos > ce_max_str) ce_max_str = strpos;
+#endif
unsigned hs[NSTRIP];
unsigned base = mix(mix((unsigned)bg_swirl, (unsigned)bg_cover_x), (unsigned)bg_solid ^ (unsigned)ce_bg_ver << 8);
for (int s = 0; s < NSTRIP; s++) hs[s] = base + (unsigned)s;
diff --git a/games/balatro/ce/tools/cekeys.py b/games/balatro/ce/tools/cekeys.py
index a006c230..7131f4d8 100644
--- a/games/balatro/ce/tools/cekeys.py
+++ b/games/balatro/ce/tools/cekeys.py
@@ -37,6 +37,8 @@ def main():
s = s.replace(a, b)
head = "/* Made by tools/cekeys.py from src/main.c: do not edit. */\n"
s = head + s.replace('#include "', '#include "../../src/', s.count('#include "'))
+ # (for tools/host.c's player: which screen shows)
+ s += "\nint ce_main_screen(void) { return screen == SCR_GAME ? 1 + overlay : 0; }\n"
open(dst, "w", encoding="utf-8").write(s)
diff --git a/games/balatro/ce/tools/host.c b/games/balatro/ce/tools/host.c
index 1612f2e2..9d68e128 100644
--- a/games/balatro/ce/tools/host.c
+++ b/games/balatro/ce/tools/host.c
@@ -209,11 +209,123 @@ static const struct {
{SDL_SCANCODE_V, eadk_key_var}};
#endif
+/* ---- NP_BOT=1: a player for long tests. It acts through the rules' own calls (what the screens' keys call),
+ when the screens have shown the last move; it starts a run from the title with the OK key. NP_BOT_ANTE: the ante
+ it plays to (then holds Home). */
+#include "ev.h"
+#include "game.h"
+#include "ui.h"
+int bot_ante_reached, bot_wins, bot_runs;
+static int bot_value(const uint8_t *cards, int n, uint8_t *keep) {
+ handinfo_t hi;
+ evaluate_hand(cards, n, &hi);
+ int chips = (int)hand_chips(hi.hand);
+ for (int i = 0; i < hi.nscoring; i++) chips += card_chip_nominal(R.cards[cards[hi.scoring[i]]].rank);
+ if (keep) {
+ memset(keep, 0, 8);
+ for (int i = 0; i < hi.nscoring; i++) keep[hi.scoring[i]] = 1;
+ }
+ return chips * (int)hand_mult(hi.hand) + (11 - hi.hand) * 10000;
+}
+static void bot_round(void) {
+ for (int i = 0; i < R.ncons; i++)
+ if (R.cons[i].id >= C_MERCURY && R.cons[i].id <= C_ERIS && use_cons(i)) return;
+ int n = R.nhand, best = -1, bmask = 0;
+ for (int m = 1; m < 1 << n; m++) {
+ int c = __builtin_popcount((unsigned)m);
+ if (c > 5) continue;
+ uint8_t cards[5];
+ int k = 0;
+ for (int i = 0; i < n; i++)
+ if (m >> i & 1) cards[k++] = R.hand[i];
+ int v = bot_value(cards, k, 0);
+ if (v > best) best = v, bmask = m;
+ }
+ for (int i = 0; i < n; i++) R.sel[i] = 0;
+ if (best < (11 - H_TWO_PAIR) * 10000 && R.discards_left > 0 && R.hands_left > 1) {
+ /* a weak hand: away with the lowest cards outside it */
+ int k = 0;
+ for (int r = 2; r <= 14 && k < 5; r++)
+ for (int i = 0; i < n && k < 5; i++)
+ if (!(bmask >> i & 1) && R.cards[R.hand[i]].rank == r && !(R.cards[R.hand[i]].flags & CF_FORCED)) R.sel[i] = 1, k++;
+ if (k && discard_hand()) return;
+ for (int i = 0; i < n; i++) R.sel[i] = 0;
+ }
+ for (int i = 0; i < n; i++)
+ if (bmask >> i & 1) R.sel[i] = 1;
+ if (!play_hand()) { /* (The Psychic, a forced card...): the first five */
+ for (int i = 0; i < n; i++) R.sel[i] = i < 5;
+ play_hand();
+ }
+}
+static void bot_shop(void) {
+ for (int i = 0; i < R.nshop; i++) {
+ sitem_t *it = &R.shop[i];
+ int cost = item_cost(it);
+ if (cost > R.money) continue;
+ if (it->kind == IT_JOKER && R.njokers < joker_slots() && shop_buy(0, i, 0)) return;
+ if (it->kind == IT_CONS && it->id >= C_MERCURY && it->id <= C_ERIS && shop_buy(0, i, 1)) return;
+ }
+ if (R.nshop_v && R.money >= item_cost(&R.shop_v[0]) + 10 && shop_buy(1, 0, 0)) return;
+ if (R.nshop_p && R.money >= item_cost(&R.shop_p[0]) + 8 && shop_buy(2, 0, 0)) return;
+ if (R.money >= R.reroll_cost + 20 && R.rerolls % 3 == 0 && shop_reroll()) return;
+ shop_leave();
+}
+static eadk_keyboard_state_t bot(uint64_t now) {
+ static uint64_t next;
+ static int quit;
+ const char *top = getenv("NP_BOT_ANTE");
+ if (quit) return (eadk_keyboard_state_t)1 << eadk_key_home;
+ if (now < next) return 0;
+ extern int ce_main_screen(void);
+ if (ce_main_screen() == 0) { /* the title: OK opens the run's panel, OK plays (or continues) */
+ next = now + 400;
+ return (eadk_keyboard_state_t)1 << eadk_key_ok;
+ }
+ if (ce_main_screen() != 1) return 0; /* (a panel open) */
+ static uint64_t idle_since;
+ static int last_round = -1, last_phase = -1;
+ if (R.round != last_round || R.phase != last_phase) idle_since = now, last_round = R.round, last_phase = R.phase;
+ if (now - idle_since > 60000 && getenv("NP_BOT_DEBUG")) {
+ fprintf(stderr, "bot: stuck in phase %d round %d busy %d nhand %d sel %d hands %d events %d..%d (%d)\n", R.phase,
+ R.round, ui_busy(), R.nhand, nselected(), R.hands_left, ev_head, ev_tail, ev_head < ev_tail ? evq[ev_head].type : -1);
+ idle_since = now;
+ }
+ if (ui_busy()) return 0;
+ next = now + 250;
+ if (getenv("NP_BOT_DEBUG") && now < 120000)
+ fprintf(stderr, "bot %llu: phase %d round %d money %ld shop %d/%d/%d pack %d\n", (unsigned long long)now, R.phase,
+ R.round, (long)R.money, R.nshop, R.nshop_v, R.nshop_p, R.npack);
+ if (R.ante > bot_ante_reached) bot_ante_reached = R.ante;
+ if (top && R.ante > atoi(top)) quit = 1;
+ switch (R.phase) {
+ case PH_BLIND_SELECT: blind_select(); break;
+ case PH_ROUND: bot_round(); break;
+ case PH_CASHOUT: cash_out(); break;
+ case PH_SHOP: bot_shop(); break;
+ case PH_PACK:
+ for (int i = 0; i < R.npack; i++)
+ if (pack_choose(i)) return 0;
+ pack_skip();
+ break;
+ case PH_WIN:
+ bot_wins++;
+ continue_endless();
+ break;
+ case PH_GAMEOVER:
+ bot_runs++;
+ quit = 1;
+ break;
+ }
+ return 0;
+}
+
eadk_keyboard_state_t eadk_keyboard_scan(void) {
uint64_t now = eadk_timing_millis();
frames++;
script_shots(now);
eadk_keyboard_state_t k = script_keys(now);
+ if (getenv("NP_BOT")) k |= bot(now);
#ifdef HAVE_SDL
if (!headless) {
SDL_Event e;
@@ -273,6 +385,11 @@ int main(void) {
}
ce_set_palette(0, CE_NSTATIC, ce_palette);
balatro_main();
- fprintf(stderr, "host: %ld frames in %llu ms\n", frames, (unsigned long long)eadk_timing_millis());
+ extern int ce_max_items, ce_max_str;
+ fprintf(stderr, "host: %ld frames in %llu ms (at most %d items, %d bytes of text a frame)\n", frames,
+ (unsigned long long)eadk_timing_millis(), ce_max_items, ce_max_str);
+ if (getenv("NP_BOT"))
+ fprintf(stderr, "bot: ante %d reached, %d wins, round %d, best hand %.0f, money %ld\n", bot_ante_reached, bot_wins,
+ R.round, R.best_hand, (long)R.money);
return 0;
}