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.

Kingdom Rush: new enemies and tips with their first spawn, the save kept in a script, screenshots

Mason3635e590e75a9d8
diff --git a/games/kingdomrush/docs/map.png b/games/kingdomrush/docs/map.png
new file mode 100644
index 00000000..145a7f5f
Binary files /dev/null and b/games/kingdomrush/docs/map.png differ
diff --git a/games/kingdomrush/docs/ring.png b/games/kingdomrush/docs/ring.png
new file mode 100644
index 00000000..389547d9
Binary files /dev/null and b/games/kingdomrush/docs/ring.png differ
diff --git a/games/kingdomrush/docs/shot.png b/games/kingdomrush/docs/shot.png
new file mode 100644
index 00000000..eaf70e1c
Binary files /dev/null and b/games/kingdomrush/docs/shot.png differ
diff --git a/games/kingdomrush/docs/upgrades.png b/games/kingdomrush/docs/upgrades.png
new file mode 100644
index 00000000..09b44974
Binary files /dev/null and b/games/kingdomrush/docs/upgrades.png differ
diff --git a/games/kingdomrush/docs/veznan.png b/games/kingdomrush/docs/veznan.png
new file mode 100644
index 00000000..f5d9cf53
Binary files /dev/null and b/games/kingdomrush/docs/veznan.png differ
diff --git a/games/kingdomrush/src/kr.h b/games/kingdomrush/src/kr.h
index 0bb115d8..540d5a5a 100644
--- a/games/kingdomrush/src/kr.h
+++ b/games/kingdomrush/src/kr.h
@@ -181,6 +181,7 @@ typedef struct {
int16_t between_t;
int16_t ival_t, index_cant, index_swap;
uint8_t cur_creep;
+ uint8_t note_sent; /* notificationSent: the wave's second notification went out */
} ActiveWave;
typedef struct {
@@ -211,6 +212,7 @@ typedef struct {
uint8_t fire_on, reinf_on; /* unlocked */
int16_t fire_open_t, reinf_open_t;
uint8_t pending_note; /* a notification to show (NOTE_*) */
+ uint8_t pending_more; /* and one more after it */
/* the field */
uint8_t nspots;
Spot spot[MAX_SPOTS];
diff --git a/games/kingdomrush/src/level.c b/games/kingdomrush/src/level.c
index 25de818b..5f32d6c5 100644
--- a/games/kingdomrush/src/level.c
+++ b/games/kingdomrush/src/level.c
@@ -9,6 +9,7 @@
#include "anim.h"
#include "special.h"
#include "res.h"
+#include "strings.h"
Battle B;
uint8_t upgrades[6];
@@ -151,6 +152,8 @@ bool tower_locked(int kind, int level) {
}
/* ------------------------------------------------------------------ waves */
+static void notify(int note);
+
static bool spawn_step(ActiveWave *a) {
const uint8_t *w = wave_at(a->wave), *s = w + WAVE_HDR + SPAWN_SZ * a->spawn;
int creep = s[0], aux = s[1], max_same = s[2], cant = (int)rd16(s + 3), ival = (int)rd16(s + 5);
@@ -173,6 +176,12 @@ static bool spawn_step(ActiveWave *a) {
if (lane < 0) lane = 0;
if (lane >= B.nlanes[path]) lane = B.nlanes[path] - 1;
enemy_spawn(a->cur_creep, path, lane, 0);
+ /* WaveSpawn: the second notification (a new enemy, or a tip) with the first of its kind; the original puts
+ * an icon at the side to open, here it opens itself, the first time only */
+ if (w[4] && !a->note_sent) {
+ int arg = -1, kind = note_pic(w[4], &arg);
+ if (kind == 0 || (kind == 1 && arg == a->cur_creep)) notify(w[4]), a->note_sent = 1;
+ }
a->index_swap++, a->index_cant++, a->ival_t = 0;
return true;
}
@@ -226,7 +235,9 @@ static void show_flags(int wi) {
static void notify(int note) {
if (note == NOTE_POWERREINFORCEMENT) power_unlock_reinf();
if (note == NOTE_POWERFIREBALL) power_unlock_fire();
- if (note != NOTE_NONE) B.pending_note = (uint8_t)note;
+ if (note == NOTE_NONE || note == B.pending_note) return;
+ if (B.pending_note) B.pending_more = (uint8_t)note;
+ else B.pending_note = (uint8_t)note;
}
static bool has_enemies(void) {
diff --git a/games/kingdomrush/src/play.c b/games/kingdomrush/src/play.c
index a46d2b82..9d94866c 100644
--- a/games/kingdomrush/src/play.c
+++ b/games/kingdomrush/src/play.c
@@ -338,7 +338,7 @@ void play_step(void) {
battle_step();
if (B.pending_note) {
int n = B.pending_note;
- B.pending_note = 0;
+ B.pending_note = B.pending_more, B.pending_more = 0;
if (!(g.p.seen_note[n >> 3] & (1 << (n & 7))) && note_text(n)) {
g.p.seen_note[n >> 3] = (uint8_t)(g.p.seen_note[n >> 3] | 1 << (n & 7));
P.note = (uint8_t)n, P.mode = M_NOTE;
diff --git a/games/kingdomrush/src/save.c b/games/kingdomrush/src/save.c
index 9a4814e7..d93ee1f0 100644
--- a/games/kingdomrush/src/save.c
+++ b/games/kingdomrush/src/save.c
@@ -1,10 +1,15 @@
-/* The save: kingdomrush.sav, "KRS", a version, then the progress (game.h's Progress), and a check byte. */
+/* The save: kingdomrush.sav, "KRS", a version, then the progress (game.h's Progress), and a check byte; and a
+ * copy of it in a Python script, kingdomrush_saves.py: installing apps from the NumWorks website keeps only Python
+ * scripts, so the save comes back from it when the file itself is gone. The copy is a comment line
+ * "#>kingdomrush.sav:base64" (the way NumPlay and its big games keep theirs). */
#include <string.h>
#include "game.h"
#include "plat.h"
#define SAVE_NAME "kingdomrush.sav"
+#define COPY_NAME "kingdomrush_saves.py"
#define SAVE_VERSION 1
+#define NAME_LEN (sizeof SAVE_NAME - 1)
static uint8_t sum(const uint8_t *p, uint32_t n) {
uint8_t s = 0x5A;
@@ -12,6 +17,84 @@ static uint8_t sum(const uint8_t *p, uint32_t n) {
return s;
}
+static const char copy_head[] = "# Kingdom Rush's save: keep this file\n";
+static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+static void copy_write(void) {
+ uint32_t len;
+ const uint8_t *d = plat_load(SAVE_NAME, &len);
+ plat_remove(COPY_NAME);
+ if (!d || !len) return;
+ uint32_t total = 1 + (sizeof copy_head - 1) + 2 + NAME_LEN + 1 + (len + 2) / 3 * 4 + 1 + 1;
+ uint8_t *o = plat_reserve(COPY_NAME, total), *p = o;
+ if (!o) return;
+ d = plat_load(SAVE_NAME, &len); /* (the storage may have moved) */
+ if (!d) return;
+ *p++ = 0; /* (the script's status byte: not imported) */
+ memcpy(p, copy_head, sizeof copy_head - 1), p += sizeof copy_head - 1;
+ *p++ = '#', *p++ = '>';
+ memcpy(p, SAVE_NAME, NAME_LEN), p += NAME_LEN;
+ *p++ = ':';
+ for (uint32_t i = 0; i < len; i += 3, p += 4) {
+ /* (written out: GCC 15.2 for the calculator got a loop over the four wrong, see NumPlay's
+ * launcher/src/storage.c) */
+ uint32_t left = len - i, v = (uint32_t)d[i] << 16 | (left > 1 ? d[i + 1] << 8 : 0) | (left > 2 ? d[i + 2] : 0);
+ p[0] = (uint8_t)b64[v >> 18 & 63];
+ p[1] = (uint8_t)b64[v >> 12 & 63];
+ p[2] = left > 1 ? (uint8_t)b64[v >> 6 & 63] : '=';
+ p[3] = left > 2 ? (uint8_t)b64[v & 63] : '=';
+ }
+ *p++ = '\n';
+ *p++ = 0;
+ plat_reserved(COPY_NAME, (uint32_t)(p - o));
+}
+
+static int b64_value(uint8_t c) {
+ for (int i = 0; i < 64; i++)
+ if ((uint8_t)b64[i] == c) return i;
+ return -1;
+}
+
+static const uint8_t *copy_line(uint32_t *len) {
+ uint32_t n;
+ const uint8_t *c = plat_load(COPY_NAME, &n);
+ if (!c) return NULL;
+ for (uint32_t i = 0; i + NAME_LEN + 3 <= n; i++)
+ if (c[i] == '#' && c[i + 1] == '>' && !memcmp(c + i + 2, SAVE_NAME, NAME_LEN) && c[i + 2 + NAME_LEN] == ':') {
+ const uint8_t *l = c + i + 3 + NAME_LEN;
+ uint32_t m = 0;
+ while (l + m < c + n && l[m] != '\n' && l[m]) m++;
+ *len = m;
+ return l;
+ }
+ return NULL;
+}
+
+/* the save back from the script when the file is gone */
+static void restore_copy(void) {
+ uint32_t have, len;
+ if (plat_load(SAVE_NAME, &have)) return;
+ const uint8_t *l = copy_line(&len);
+ if (!l || !len || len % 4) return;
+ uint32_t size = len / 4 * 3 - (l[len - 1] == '=') - (l[len - 2] == '=');
+ uint8_t *out = plat_reserve(SAVE_NAME, size);
+ if (!out) return;
+ l = copy_line(&len);
+ bool ok = l != NULL && len / 4 * 3 - (l[len - 1] == '=') - (l[len - 2] == '=') == size;
+ uint32_t o = 0;
+ for (uint32_t i = 0; ok && i < len; i += 4) {
+ int v0 = l[i] == '=' ? 0 : b64_value(l[i]), v1 = l[i + 1] == '=' ? 0 : b64_value(l[i + 1]);
+ int v2 = l[i + 2] == '=' ? 0 : b64_value(l[i + 2]), v3 = l[i + 3] == '=' ? 0 : b64_value(l[i + 3]);
+ if (v0 < 0 || v1 < 0 || v2 < 0 || v3 < 0) ok = false;
+ uint32_t wd = (uint32_t)(v0 << 18 | v1 << 12 | v2 << 6 | v3);
+ if (o < size) out[o++] = (uint8_t)(wd >> 16);
+ if (o < size) out[o++] = (uint8_t)(wd >> 8);
+ if (o < size) out[o++] = (uint8_t)wd;
+ }
+ if (ok && o == size) plat_reserved(SAVE_NAME, size);
+ else plat_remove(SAVE_NAME);
+}
+
void save_progress(void) {
uint8_t buf[4 + sizeof(Progress) + 1];
memcpy(buf, "KRS", 3);
@@ -19,11 +102,13 @@ void save_progress(void) {
memcpy(buf + 4, &g.p, sizeof g.p);
buf[sizeof buf - 1] = sum(buf, sizeof buf - 1);
plat_save(SAVE_NAME, buf, sizeof buf);
+ copy_write();
}
void load_progress(void) {
memset(&g.p, 0, sizeof g.p);
g.p.difficulty = 1;
+ restore_copy();
uint32_t n = 0;
const uint8_t *d = plat_load(SAVE_NAME, &n);
if (!d || n != 4 + sizeof(Progress) + 1 || memcmp(d, "KRS", 3) || d[3] != SAVE_VERSION || sum(d, n - 1) != d[n - 1])
diff --git a/games/kingdomrush/tests/rules.c b/games/kingdomrush/tests/rules.c
index 485bafcd..5dab9a7c 100644
--- a/games/kingdomrush/tests/rules.c
+++ b/games/kingdomrush/tests/rules.c
@@ -180,7 +180,7 @@ static int play_battle(int level, int mode, bool full_upgrades, int *lives) {
for (int t = 0; t < 30 * 60 * 60 && B.state != LS_WIN && B.state != LS_OVER; t++) {
battle_step();
if (t % 15 == 0) general();
- if (B.pending_note) B.pending_note = 0;
+ B.pending_note = B.pending_more = 0;
if (B.flags_up && (mode == MODE_IRON || alive_enemies() < 4)) battle_call_wave(); /* (the general is in a hurry) */
}
*lives = B.lives;
@@ -241,6 +241,11 @@ static void test_save(void) {
memset(&g.p, 0, sizeof g.p);
load_progress();
CHECK(g.p.camp[0] == 3 && g.p.camp[1] == 2 && g.p.heroic[0] == 1 && g.p.ups[UP_RAIN] == 2, "the save comes back");
+ /* the file gone (apps installed from the website): back from the Python script */
+ remove("build/host-saves/kingdomrush.sav");
+ memset(&g.p, 0, sizeof g.p);
+ load_progress();
+ CHECK(g.p.camp[0] == 3 && g.p.camp[1] == 2 && g.p.heroic[0] == 1 && g.p.ups[UP_RAIN] == 2, "and from its copy");
FILE *f = fopen("build/host-saves/kingdomrush.sav", "r+b");
if (f) {
fseek(f, 6, SEEK_SET);
@@ -251,6 +256,7 @@ static void test_save(void) {
CHECK(g.p.camp[0] == 0 && g.p.difficulty == 1, "a broken save: a fresh start");
CHECK(stars_won() == 0, "no stars");
remove("build/host-saves/kingdomrush.sav");
+ remove("build/host-saves/kingdomrush_saves.py");
}
int main(int argc, char **argv) {
@@ -259,6 +265,7 @@ int main(int argc, char **argv) {
system("mkdir -p build/host-saves");
host_save_dir("build/host-saves");
remove("build/host-saves/kingdomrush.sav");
+ remove("build/host-saves/kingdomrush_saves.py");
game_init();
if (argc > 1 && !strcmp(argv[1], "sizes")) {
printf("Battle %zu: Enemy %zu x %d, Soldier %zu x %d, Bullet %zu x %d, Fx %zu x %d, Spot %zu\n", sizeof B, sizeof(Enemy),