Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.
C calculatorcalculator-gamescasiochesscrossy-roaddrive-madfx-cg50gamesgeometry-dashgraphing-calculatorndlessnumworksnumworks-appsnumworks-calculatornumworks-gamenumworks-gamestetristi-84-plus-ceti-nspire
NumBlocks: several worlds, each with its name
Mason Chenddcda5047265
diff --git a/games/games.json b/games/games.jsonindex 6e976ffd..78867854 100644--- a/games/games.json+++ b/games/games.json@@ -97,11 +97,43 @@"records": ["numblocks.cfg","nb1.nbw",- "nb1r*"+ "nb1r*",+ "nb2.nbw",+ "nb2r*",+ "nb3.nbw",+ "nb3r*",+ "nb4.nbw",+ "nb4r*",+ "nb5.nbw",+ "nb5r*",+ "nb6.nbw",+ "nb6r*",+ "nb7.nbw",+ "nb7r*",+ "nb8.nbw",+ "nb8r*",+ "nb9.nbw",+ "nb9r*"],"reset": ["nb1.nbw",- "nb1r*"+ "nb1r*",+ "nb2.nbw",+ "nb2r*",+ "nb3.nbw",+ "nb3r*",+ "nb4.nbw",+ "nb4r*",+ "nb5.nbw",+ "nb5r*",+ "nb6.nbw",+ "nb6r*",+ "nb7.nbw",+ "nb7r*",+ "nb8.nbw",+ "nb8r*",+ "nb9.nbw",+ "nb9r*"],"shots": ["docs/shot.png"diff --git a/games/numblocks/src/gui.c b/games/numblocks/src/gui.cindex dbe16193..d7403d67 100644--- a/games/numblocks/src/gui.c+++ b/games/numblocks/src/gui.c@@ -749,13 +749,90 @@ static Button buttons[12];static int nbuttons, bcur;enum { B_NO, B_SINGLE, B_OPTIONS, B_QUITAPP, B_PLAY, B_CREATE, B_DELETE, B_CANCEL, B_MODE, B_SEED, B_DOCREATE,B_DODELETE, B_DIFF, B_GFX, B_LOOK, B_CLOUDS, B_BOB, B_DONE, B_BACK_GAME, B_SAVEQUIT, B_RESPAWN, B_TITLE,- B_CONTROLS, B_CONTROLS_DONE };+ B_CONTROLS, B_CONTROLS_DONE, B_NAME, B_RENAME, B_DORENAME, B_RECREATE };int menu_choice; /* an action for main.c (ACT_*) */int create_mode; /* the new world's game mode */char seed_text[21]; /* the new world's seed, as typed */+char name_text[WORLD_NAME + 1]; /* the new world's name, or the world being renamed's */+int play_slot; /* the world to play */static int options_from; /* the screen the options go back to */static int controls_from; /* and the key sheet */-static bool seed_focus;++/* the worlds, the one played last first, and the one selected */+static uint8_t wlist[MAX_WORLDS], nworlds, wsel;+static void list_worlds(void) {+ uint32_t when[MAX_WORLDS];+ WorldInfo w;+ nworlds = 0;+ for (int s = 1; s <= MAX_WORLDS; s++)+ if (world_info(s, &w)) {+ int k = nworlds++;+ for (; k > 0 && when[k - 1] < w.played; k--) wlist[k] = wlist[k - 1], when[k] = when[k - 1];+ wlist[k] = (uint8_t)s, when[k] = w.played;+ }+ if (wsel >= nworlds) wsel = nworlds ? nworlds - 1 : 0;+}++/* Typing in a field: the calculator's keys as its own keyboard has them, letters (its alpha+ * letters: e^x is a, + is z, - a space) or digits; alpha switches between them, shift makes the+ * next letter a capital (twice: all of them), backspace deletes, OK or Back is done */+enum { F_NONE, F_NAME, F_SEED };+static int field; /* the field being typed in */+static uint8_t f_digits, f_caps; /* (f_caps: 1 the next letter, 2 all of them) */+static bool f_sel; /* the text is selected: the first key typed replaces it (an arrow keeps it) */+static uint64_t raw_held;+static void press(int id);+static char key_char(int k) {+ static const char letters[53] = {[18] = 'a', [19] = 'b', [20] = 'c', [21] = 'd', [22] = 'e', [23] = 'f', [24] = 'g',+ [25] = 'h', [26] = 'i', [27] = 'j', [28] = 'k', [29] = 'l', [30] = 'm', [31] = 'n',+ [32] = 'o', [33] = 'p', [34] = 'q', [36] = 'r', [37] = 's', [38] = 't', [39] = 'u',+ [40] = 'v', [42] = 'w', [43] = 'x', [44] = 'y', [45] = 'z', [46] = ' ', [48] = '?',+ [49] = '!'};+ static const char digits[53] = {[42] = '1', [43] = '2', [44] = '3', [36] = '4', [37] = '5', [38] = '6', [30] = '7',+ [31] = '8', [32] = '9', [48] = '0', [49] = '.', [46] = '-', [45] = '+', [33] = '(',+ [34] = ')', [39] = '*', [40] = '/'};+ if (k < 0 || k > 52) return 0;+ char c = f_digits ? digits[k] : letters[k];+ if (c >= 'a' && c <= 'z' && f_caps) {+ c = (char)(c - 'a' + 'A');+ if (f_caps == 1) f_caps = 0;+ }+ return c;+}+static void field_open(int f) {+ field = f;+ f_digits = f == F_SEED, f_caps = 0;+ f_sel = (f == F_NAME ? name_text : seed_text)[0] != 0;+ raw_held = ~(uint64_t)0; /* (the key that opened it is not typed) */+}+static void field_input(void) {+ uint64_t raw = plat_scan(), down = raw & ~raw_held;+ raw_held = raw;+ char *t = field == F_NAME ? name_text : seed_text;+ int max = field == F_NAME ? WORLD_NAME : 20, n = (int)strlen(t);+ for (int k = 0; k < 64; k++) {+ if (!(down >> k & 1)) continue;+ if (k == RK_ALPHA) f_digits = !f_digits;+ else if (k == RK_SHIFT) f_caps = (uint8_t)((f_caps + 1) % 3);+ else if (k <= 3) f_sel = false; /* (the arrows) */+ else if (k == RK_BACKSPACE) {+ if (f_sel) t[n = 0] = 0, f_sel = false;+ else if (n > 0) t[--n] = 0;+ } else if (k == RK_OK || k == RK_EXE || k == RK_BACK) {+ if (gui == GUI_RENAME && k != RK_BACK && name_text[0]) {+ /* (OK in the rename screen's field: rename, as Enter does) */+ field = F_NONE;+ press(B_DORENAME);+ return;+ }+ field = F_NONE;+ } else {+ char c = key_char(k);+ if (c && f_sel) t[n = 0] = 0, f_sel = false;+ if (c && n < max) t[n++] = c, t[n] = 0;+ }+ }+}static void add_button(int x, int y, int w, int on, int id, const char *label) {Button *b = &buttons[nbuttons++];@@ -784,23 +861,30 @@ static void menu(int screen) {break;}case GUI_WORLDS: {- bool any = world_exists();+ bool any = nworlds > 0;add_button(w / 2 - 154, h - 52, 150, any, B_PLAY, "Play Selected World");- add_button(w / 2 + 4, h - 52, 150, 1, B_CREATE, "Create New World");- add_button(w / 2 - 154, h - 28, 72, 0, B_NO, "Rename");+ add_button(w / 2 + 4, h - 52, 150, world_free_slot() != 0, B_CREATE, "Create New World");+ add_button(w / 2 - 154, h - 28, 72, any, B_RENAME, "Rename");add_button(w / 2 - 76, h - 28, 72, any, B_DELETE, "Delete");- add_button(w / 2 + 4, h - 28, 72, 0, B_NO, "Re-Create");+ add_button(w / 2 + 4, h - 28, 72, any && world_free_slot() != 0, B_RECREATE, "Re-Create");add_button(w / 2 + 82, h - 28, 72, 1, B_CANCEL, "Cancel");break;}case GUI_CREATE:+ add_button(w / 2 - 100, 60, 200, 1, B_NAME, name_text); /* (drawn as a field) */cat(t, "Game Mode: ", create_mode ? "Creative" : "Survival");add_button(w / 2 - 75, 100, 150, 1, B_MODE, t);- cat(t, "Seed: ", seed_text[0] ? seed_text : (seed_focus ? "" : "(random)"));+ cat(t, "Seed: ", seed_text[0] ? seed_text : (field == F_SEED ? "" : "(random)"));add_button(w / 2 - 100, 150, 200, 1, B_SEED, t);add_button(w / 2 - 155, h - 28, 150, 1, B_DOCREATE, "Create New World");add_button(w / 2 + 5, h - 28, 150, 1, B_CANCEL, "Cancel");break;+ case GUI_RENAME:+ /* GuiRenameWorld */+ add_button(w / 2 - 100, 60, 200, 1, B_NAME, name_text);+ add_button(w / 2 - 100, h / 4 + 96 + 12, 200, name_text[0] != 0, B_DORENAME, "Rename");+ add_button(w / 2 - 100, h / 4 + 120 + 12, 200, 1, B_CANCEL, "Cancel");+ break;case GUI_CONFIRM:add_button(w / 2 - 155, h / 6 + 96, 150, 1, B_DODELETE, "Delete");add_button(w / 2 + 5, h / 6 + 96, 150, 1, B_CANCEL, "Cancel");@@ -849,10 +933,14 @@ void gui_menu(int screen) {if (screen == GUI_CONTROLS && gui != GUI_CONTROLS) controls_from = gui; /* Options, or the title */gui = screen;bcur = 0;- seed_focus = false;+ field = F_NONE;+ if (screen == GUI_WORLDS) list_worlds();menu(screen);}+/* the world selected in the list */+static int selected(WorldInfo *w) { return nworlds && world_info(wlist[wsel], w) ? wlist[wsel] : 0; }+static void button(const Button *b, bool hover) {int sp = !b->on ? SP_BUTTON_OFF : hover ? SP_BUTTON_HOVER : SP_BUTTON;/* GuiButton.drawButton: the left half from the texture's left, the right half from its right */@@ -863,6 +951,7 @@ static void button(const Button *b, bool hover) {}static void press(int id) {+ WorldInfo wi;switch (id) {case B_SINGLE: gui_menu(GUI_WORLDS); break;case B_OPTIONS:@@ -870,20 +959,55 @@ static void press(int id) {gui_menu(GUI_OPTIONS);break;case B_QUITAPP: menu_choice = ACT_QUIT_APP; break;- case B_PLAY: menu_choice = ACT_PLAY; break;+ case B_PLAY:+ if (selected(&wi)) play_slot = wlist[wsel], menu_choice = ACT_PLAY;+ break;case B_CREATE:create_mode = 0;seed_text[0] = 0;+ world_unique_name("New World", name_text);+ gui_menu(GUI_CREATE);+ break;+ case B_RECREATE: {+ /* GuiCreateWorld.func_146318_a: a new world as the selected one began, "Copy of" its name */+ if (!selected(&wi)) break;+ char n[WORLD_NAME + 9];+ cat(n, "Copy of ", wi.name);+ n[WORLD_NAME] = 0;+ world_unique_name(n, name_text);+ create_mode = wi.mode;+ /* (its seed, as it would be typed) */+ uint64_t v = wi.seed < 0 ? 0 - (uint64_t)wi.seed : (uint64_t)wi.seed;+ char d[21];+ int k = 0;+ do d[k++] = (char)('0' + v % 10), v /= 10;+ while (v);+ int j = 0;+ if (wi.seed < 0) seed_text[j++] = '-';+ while (k) seed_text[j++] = d[--k];+ seed_text[j] = 0;gui_menu(GUI_CREATE);break;+ }+ case B_RENAME:+ if (!selected(&wi)) break;+ memcpy(name_text, wi.name, sizeof wi.name);+ gui_menu(GUI_RENAME);+ field_open(F_NAME);+ break;+ case B_DORENAME:+ if (name_text[0] && nworlds) rename_world(wlist[wsel], name_text);+ gui_menu(GUI_WORLDS);+ break;case B_DELETE: gui_menu(GUI_CONFIRM); break;case B_DODELETE:- delete_world();+ if (nworlds) delete_world(wlist[wsel]);gui_menu(GUI_WORLDS);break;case B_CANCEL: gui_menu(gui == GUI_WORLDS ? GUI_TITLE : GUI_WORLDS); break;case B_MODE: create_mode = !create_mode; break;- case B_SEED: seed_focus = !seed_focus; break;+ case B_NAME: field_open(F_NAME); break;+ case B_SEED: field_open(F_SEED); break;case B_DOCREATE: menu_choice = ACT_NEW; break;case B_DIFF: opt.difficulty = (uint8_t)((opt.difficulty + 1) & 3); break;case B_GFX: opt.fancy = !opt.fancy; break;@@ -909,18 +1033,17 @@ static void press(int id) {static void menu_input(uint32_t keys, uint32_t pressed) {if (!nbuttons) menu(gui);- if (seed_focus) {- /* typing the seed: digits, a minus first; backspace deletes; OK is done */- int n = (int)strlen(seed_text);- for (int d = 0; d < 9; d++)- if (pressed & (K_SLOT1 << d) && n < 19) seed_text[n++] = (char)('1' + d), seed_text[n] = 0;- if (pressed & K_ZERO && n < 19) seed_text[n++] = '0', seed_text[n] = 0;- if (pressed & K_MINUS && n == 0) seed_text[n++] = '-', seed_text[n] = 0;- if ((pressed & K_SPRINT) && n > 0) seed_text[--n] = 0; /* (backspace) */- if (pressed & (K_USE | K_BACK)) seed_focus = false;- menu(gui);+ if (field) {+ field_input();+ if (gui != GUI_NONE) menu(gui);return;}+ raw_held = plat_scan();+ if (gui == GUI_WORLDS && (pressed & (K_UP | K_DOWN)) && nworlds) {+ /* up and down: the world; left and right: the buttons */+ wsel = (uint8_t)((wsel + ((pressed & K_DOWN) ? 1 : nworlds - 1)) % nworlds);+ pressed &= ~(uint32_t)(K_UP | K_DOWN);+ }int dir = (pressed & (K_DOWN | K_RIGHT)) ? 1 : (pressed & (K_UP | K_LEFT)) ? -1 : 0;if (dir)for (int k = 0; k < nbuttons; k++) {@@ -933,7 +1056,7 @@ static void menu_input(uint32_t keys, uint32_t pressed) {if (gui == GUI_PAUSE) gui = GUI_NONE;else if (gui == GUI_OPTIONS) press(B_DONE);else if (gui == GUI_CONTROLS) press(B_CONTROLS_DONE);- else if (gui == GUI_WORLDS || gui == GUI_CREATE || gui == GUI_CONFIRM) press(B_CANCEL);+ else if (gui == GUI_WORLDS || gui == GUI_CREATE || gui == GUI_CONFIRM || gui == GUI_RENAME) press(B_CANCEL);}}@@ -1074,6 +1197,20 @@ static void key_sheet(void) {for (int i = 0; i < 4; i++) text_center(notes[i], SCREEN_W / 2, 177 + i * 10, 0xFFFF);}+/* GuiTextField: grey framed (white when chosen), the text, and while typing a blinking cursor+ * and what the keys type (abc, Abc for one capital, ABC, 123) */+static void text_field(const Button *b, bool hover, bool typing) {+ fill(b->x - 1, b->y - 1, b->w + 2, 22, hover || typing ? 0xFFFF : RGB(0xA0, 0xA0, 0xA0));+ fill(b->x, b->y, b->w, 20, 0);+ if (typing && f_sel) fill(b->x + 3, b->y + 5, text_width(b->label) + 1, 10, RGB(0x00, 0x00, 0xA0)); /* (selected) */+ text(b->label, b->x + 4, b->y + 6, RGB(0xE0, 0xE0, 0xE0), true);+ if (typing) {+ if ((frame_no / 6) & 1) text("_", b->x + 4 + text_width(b->label), b->y + 6, RGB(0xE0, 0xE0, 0xE0), true);+ const char *m = f_digits ? "123" : f_caps == 2 ? "ABC" : f_caps ? "Abc" : "abc";+ text(m, b->x + b->w - 4 - text_width(m), b->y + 6, RGB(0x80, 0x80, 0x80), true);+ }+}+static void menu_screen(void) {int w = SCREEN_W, h = SCREEN_H;switch (gui) {@@ -1086,40 +1223,55 @@ static void menu_screen(void) {text("Made by Mason Chen as part of NumPlay", w - text_width("Made by Mason Chen as part of NumPlay") - 2, h - 10,0xFFFF, true);break;- case GUI_WORLDS:+ case GUI_WORLDS: {text_center("Select World", w / 2, 20, 0xFFFF);- if (world_exists()) {- /* the one world, selected */- int x = w / 2 - 110, y = 36;- fill(x - 2, y - 2, 220, 36, RGB(0x80, 0x80, 0x80));- fill(x - 1, y - 1, 218, 34, 0);- text("New World", x + 2, y + 1, 0xFFFF, true);- char t[48], n[12];- number((int)((30000 - (int)plat_storage_free()) / 1024), n);- cat(t, "nb1 (storage free: ", "");- number((int)(plat_storage_free() / 1024), n);- cat(t + strlen(t), n, " KB)");- text(t, x + 2, y + 12, RGB(0x80, 0x80, 0x80), true);- text(world_mode() == 1 ? "Creative Mode" : "Survival Mode", x + 2, y + 22, RGB(0x80, 0x80, 0x80), true);- } else text_center("No worlds yet", w / 2, 60, RGB(0x80, 0x80, 0x80));+ /* GuiSlot: 36 pixels a world, the selected one framed */+ int first = wsel > 3 ? wsel - 3 : 0;+ for (int k = first; k < nworlds && k < first + 4; k++) {+ WorldInfo wi;+ if (!world_info(wlist[k], &wi)) continue;+ int x = w / 2 - 110, y = 32 + (k - first) * 36 + 2;+ if (k == wsel) {+ fill(x - 2, y - 2, 220, 36, RGB(0x80, 0x80, 0x80));+ fill(x - 1, y - 1, 218, 34, 0);+ }+ text(wi.name, x + 2, y + 1, 0xFFFF, true);+ char f[4] = {'n', 'b', (char)('0' + wlist[k]), 0};+ text(f, x + 2, y + 12, RGB(0x80, 0x80, 0x80), true);+ text(wi.mode == 1 ? "Creative Mode" : "Survival Mode", x + 2, y + 22, RGB(0x80, 0x80, 0x80), true);+ }+ if (!nworlds) text_center("No worlds yet", w / 2, 60, RGB(0x80, 0x80, 0x80));+ char t[40], n[12];+ number((int)(plat_storage_free() / 1024), n);+ cat(t, "Storage free: ", n);+ cat(t + strlen(t), " KB", "");+ text_center(t, w / 2, h - 63, RGB(0x80, 0x80, 0x80));break;+ }case GUI_CREATE:text_center("Create New World", w / 2, 20, 0xFFFF);text("World Name", w / 2 - 100, 47, RGB(0xA0, 0xA0, 0xA0), true);- fill(w / 2 - 101, 59, 202, 22, RGB(0xA0, 0xA0, 0xA0));- fill(w / 2 - 100, 60, 200, 20, 0);- text("New World", w / 2 - 96, 66, RGB(0xE0, 0xE0, 0xE0), true);text_center(create_mode ? "Unlimited resources, free flying and" : "Search for resources, crafting, gain",w / 2, 122, RGB(0xA0, 0xA0, 0xA0));text_center(create_mode ? "destroy blocks instantly" : "levels, health and hunger", w / 2, 134,RGB(0xA0, 0xA0, 0xA0));- text_center(seed_focus ? "Type with the digits, then OK" : "Leave blank for a random seed", w / 2, 174,- RGB(0xA0, 0xA0, 0xA0));+ text_center(field ? "alpha: letters or digits, shift: capitals, OK: done" : "Leave blank for a random seed",+ w / 2, 174, RGB(0xA0, 0xA0, 0xA0));break;- case GUI_CONFIRM:+ case GUI_RENAME:+ text_center("Rename World", w / 2, 20, 0xFFFF);+ text("Enter Name", w / 2 - 100, 47, RGB(0xA0, 0xA0, 0xA0), true);+ if (field) text_center("alpha: letters or digits, shift: capitals, OK: done", w / 2, 90, RGB(0xA0, 0xA0, 0xA0));+ break;+ case GUI_CONFIRM: {+ WorldInfo wi;+ char t[64];text_center("Are you sure you want to delete this world?", w / 2, 70, 0xFFFF);- text_center("'New World' will be lost forever! (A long time!)", w / 2, 90, 0xFFFF);+ cat(t, "'", nworlds && world_info(wlist[wsel], &wi) ? wi.name : "");+ cat(t + strlen(t), "' will be lost forever! (A long time!)", "");+ text_center(t, w / 2, 90, 0xFFFF);break;+ }case GUI_OPTIONS: text_center("Options", w / 2, 15, 0xFFFF); break;case GUI_CONTROLS: key_sheet(); break;case GUI_PAUSE: text_center("Game menu", w / 2, 40, 0xFFFF); break;@@ -1128,10 +1280,12 @@ static void menu_screen(void) {text_center("Building terrain", w / 2, h / 2 - 20, 0xFFFF);break;}- for (int i = 0; i < nbuttons; i++) button(&buttons[i], i == bcur && !seed_focus);- if (seed_focus && gui == GUI_CREATE) {+ for (int i = 0; i < nbuttons; i++)+ if (buttons[i].id == B_NAME) text_field(&buttons[i], i == bcur, field == F_NAME);+ else button(&buttons[i], i == bcur && !field);+ if (field == F_SEED && gui == GUI_CREATE) {/* the field being typed in: a white frame and a blinking cursor */- Button *b = &buttons[1];+ Button *b = &buttons[2];fill(b->x, b->y, b->w, 1, 0xFFFF);fill(b->x, b->y + 19, b->w, 1, 0xFFFF);fill(b->x, b->y, 1, 20, 0xFFFF);@@ -1342,7 +1496,7 @@ void hud_strip(uint16_t *buf, int y0, int rows) {case GUI_TITLE:menu_screen();break;- case GUI_WORLDS: case GUI_CREATE: case GUI_CONFIRM: case GUI_LOADING:+ case GUI_WORLDS: case GUI_CREATE: case GUI_CONFIRM: case GUI_LOADING: case GUI_RENAME:dirt_background();menu_screen();break;diff --git a/games/numblocks/src/main.c b/games/numblocks/src/main.cindex c4328524..323d656b 100644--- a/games/numblocks/src/main.c+++ b/games/numblocks/src/main.c@@ -25,7 +25,7 @@ static void camera_reset(void) { px = pl.x, py = pl.y, pz = pl.z; }/* the title's backdrop: the saved world where the player is, else a world of seed 0 */static void title(void) {in_world = false;- if (!load_world()) {+ if (!opt.last_world || !load_world(opt.last_world)) {world_new(0, "nbt");player_spawn();}@@ -54,18 +54,30 @@ static void loading(void) {last = plat_millis();}+/* GuiCreateWorld: the seed typed, a number (Long.parseLong) or else words (String.hashCode);+ * none, or 0: random */static int64_t parse_seed(void) {- if (!seed_text[0] || (seed_text[0] == '-' && !seed_text[1])) {- /* random: the time, mixed */- uint64_t v = (uint64_t)plat_millis() * 0x9E3779B97F4A7C15ull + perf_frames;- v ^= v >> 29;- v *= 0xBF58476D1CE4E5B9ull;- return (int64_t)(v ^ v >> 32);+ const char *s = seed_text;+ bool neg = *s == '-';+ int n = 0;+ uint64_t v = 0;+ bool num = s[neg] != 0;+ for (const char *c = s + neg; *c && num; c++, n++) {+ if (*c < '0' || *c > '9' || n >= 19) num = false; /* (19 digits: no more than a long holds) */+ else v = v * 10 + (uint64_t)(*c - '0');}- int64_t v = 0;- bool neg = seed_text[0] == '-';- for (const char *c = seed_text + neg; *c; c++) v = (int64_t)((uint64_t)v * 10 + (uint64_t)(*c - '0'));- return neg ? -v : v;+ if (num && v > (uint64_t)INT64_MAX + neg) num = false;+ if (s[0] && !num) {+ int32_t h = 0;+ for (const char *c = s; *c; c++) h = (int32_t)((uint32_t)h * 31u + (uint8_t)*c);+ return h;+ }+ if (num && v) return neg ? (int64_t)(0 - v) : (int64_t)v;+ /* random: the time, mixed */+ uint64_t r = (uint64_t)plat_millis() * 0x9E3779B97F4A7C15ull + perf_frames;+ r ^= r >> 29;+ r *= 0xBF58476D1CE4E5B9ull;+ return (int64_t)(r ^ r >> 32);}void game_init(void) {@@ -76,7 +88,7 @@ void game_init(void) {#endiflast = plat_millis();if (start_in_world) {- new_world(start_seed, 0);+ new_world(1, start_seed, 0, "New World");game_time = 1000;loading();return;@@ -110,14 +122,24 @@ bool game_frame(void) {menu_choice = 0;switch (c) {case ACT_PLAY:- if (load_world()) loading();+ if (load_world(play_slot)) loading();break;- case ACT_NEW:- new_world(parse_seed(), create_mode);+ case ACT_NEW: {+ /* a free slot, the name made unique ("New World (2)") */+ int slot = world_free_slot();+ if (!slot) break;+ char name[WORLD_NAME + 1];+ int a = 0, b = (int)strlen(name_text);+ while (name_text[a] == ' ') a++;+ while (b > a && name_text[b - 1] == ' ') b--;+ name_text[b] = 0;+ world_unique_name(name_text + a, name);+ new_world(slot, parse_seed(), create_mode, name);game_time = 0;loading();save_world();break;+ }case ACT_QUIT_APP: return false;case ACT_SAVE_QUIT:case ACT_TITLE:diff --git a/games/numblocks/src/nb.h b/games/numblocks/src/nb.hindex 089a8ad4..aac4d90a 100644--- a/games/numblocks/src/nb.h+++ b/games/numblocks/src/nb.h@@ -31,6 +31,10 @@ enum {K_MINUS = 1u << 31 /* - (typing a seed) */};uint32_t plat_keys(void);+uint64_t plat_scan(void); /* the keys down as the calculator numbers them (bit RK_*), for typing */+enum {+ RK_OK = 4, RK_BACK = 5, RK_SHIFT = 12, RK_ALPHA = 13, RK_BACKSPACE = 17, RK_EXE = 52+};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);@@ -232,7 +236,8 @@ void break_block_at(int x, int y, int z, bool drops);/* ---------------------------------------------------------------- screens (gui.c) */enum { GUI_NONE, GUI_INVENTORY, GUI_CRAFTING, GUI_FURNACE, GUI_CHEST, GUI_CREATIVE,- GUI_PAUSE, GUI_DEATH, GUI_OPTIONS, GUI_TITLE, GUI_WORLDS, GUI_CREATE, GUI_CONFIRM, GUI_LOADING, GUI_CONTROLS };+ GUI_PAUSE, GUI_DEATH, GUI_OPTIONS, GUI_TITLE, GUI_WORLDS, GUI_CREATE, GUI_CONFIRM, GUI_LOADING, GUI_CONTROLS,+ GUI_RENAME };extern int gui; /* the screen open */void gui_open(int screen, int x, int y, int z);void gui_close(void);@@ -242,6 +247,8 @@ void tiles_removed(int x, int y, int z); /* a chest or furnace broken: its itevoid gui_menu(int screen); /* opens a menu screen */enum { ACT_NONE, ACT_PLAY, ACT_NEW, ACT_QUIT_APP, ACT_SAVE_QUIT, ACT_RESPAWN, ACT_TITLE };extern int menu_choice; /* what a menu asks main.c to do (ACT_*), 0 if nothing */+extern int play_slot; /* (ACT_PLAY) the world chosen */+extern char name_text[]; /* (ACT_NEW) the new world's name, as typed */extern int create_mode;extern char seed_text[21];@@ -252,16 +259,29 @@ typedef struct {uint8_t look; /* look speed, % */uint8_t clouds, bobbing;uint8_t keys_seen; /* the key sheet's version last shown (it opens when the keys change) */+ uint8_t last_world; /* the world played last (its slot, 0: none): the title's backdrop */+ uint32_t plays; /* worlds opened so far: each world keeps the count when it was last played */} Options;#define KEY_SHEET 2 /* (2: jump and sneak on shift and alpha) */extern Options opt;extern int64_t world_seed;+/* Worlds: up to MAX_WORLDS, each in a slot (1..MAX_WORLDS) whose records start "nb<slot>" */+#define MAX_WORLDS 9+#define WORLD_NAME 24 /* the longest name */+typedef struct {+ char name[WORLD_NAME + 1];+ int mode; /* 0 survival, 1 creative */+ int64_t seed;+ uint32_t played; /* opt.plays when it was last played (the latest first in the list) */+} WorldInfo;+bool world_info(int slot, WorldInfo *w); /* false: no world there */+int world_free_slot(void); /* 0: all taken */+void world_unique_name(const char *base, char *out); /* base, or "base (2)"... if taken */bool save_world(void);-bool load_world(void);-bool world_exists(void);-int world_mode(void);-void new_world(int64_t seed, int mode);-void delete_world(void);+bool load_world(int slot);+void new_world(int slot, int64_t seed, int mode, const char *name);+bool rename_world(int slot, const char *name);+void delete_world(int slot);void load_options(void);void save_options(void);diff --git a/games/numblocks/src/plat_eadk.c b/games/numblocks/src/plat_eadk.cindex 0dfaf72f..82e206fc 100644--- a/games/numblocks/src/plat_eadk.c+++ b/games/numblocks/src/plat_eadk.c@@ -46,6 +46,7 @@ uint32_t plat_keys(void) {return r;}+uint64_t plat_scan(void) { return eadk_keyboard_scan(); }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) {diff --git a/games/numblocks/src/plat_host.c b/games/numblocks/src/plat_host.cindex fa160e99..430ab0aa 100644--- a/games/numblocks/src/plat_host.c+++ b/games/numblocks/src/plat_host.c@@ -10,6 +10,8 @@ uint32_t host_time; /* ms */uint32_t host_keys;uint32_t plat_keys(void) { return host_keys; }+uint64_t host_raw; /* (tests: keys as the calculator numbers them) */+uint64_t plat_scan(void) { return host_raw; }uint32_t plat_millis(void) { return host_time; }void plat_sleep(uint32_t ms) { host_time += ms; }void plat_push(int x, int y, int w, int h, const uint16_t *px) {diff --git a/games/numblocks/src/save.c b/games/numblocks/src/save.cindex 21fe263a..aff60083 100644--- a/games/numblocks/src/save.c+++ b/games/numblocks/src/save.c@@ -1,35 +1,123 @@-/* Saving: the world's record (its seed, the time, the weather, the player, chests and- * furnaces) next to the records of its changed blocks (edits.c), and the- * options. A world saves every 45 seconds of play, as Minecraft does, and- * when you quit; block changes reach storage as they fill the journal. */+/* Saving: each world's record (its name, seed, the time, the weather, the player, chests and+ * furnaces) next to the records of its changed blocks (edits.c), and the options. A world+ * saves every 45 seconds of play, as Minecraft does, and when you quit; block changes reach+ * storage as they fill the journal. Worlds live in slots: the records of slot 3 are+ * "nb3.nbw" and "nb3r..." (its regions). */#include "nb.h"#include "edits.h"-#define WORLD "nb1"-#define WORLD_REC "nb1.nbw"#define OPTIONS_REC "numblocks.cfg"-Options opt = {2, 0, 100, 1, 1, 0};+Options opt = {2, 0, 100, 1, 1, 0, 0, 0};int64_t world_seed;+/* the record's head: version 1 had no name (it was "New World") */typedef struct {char magic[4];uint16_t player_size, tiles_size;int64_t seed;uint32_t time;Weather weather;+} Head1;+typedef struct {+ Head1 h;+ char name[WORLD_NAME + 1];+ uint32_t played;} Head;extern uint32_t game_time;void *tiles_data(uint32_t *len);-static uint8_t buf_head[sizeof(Head)];+static int slot = 1; /* the world being played */+static char cur_name[WORLD_NAME + 1]; /* its name */+static uint32_t cur_played;++static void prefix_of(int s, char *out) { out[0] = 'n', out[1] = 'b', out[2] = (char)('0' + s), out[3] = 0; }+static void record_of(int s, char *out) {+ prefix_of(s, out);+ out[3] = '.', out[4] = 'n', out[5] = 'b', out[6] = 'w', out[7] = 0;+}+static void copy_name(char *d, const char *s) {+ int i = 0;+ for (; s[i] && i < WORLD_NAME; i++) d[i] = s[i];+ d[i] = 0;+}++/* a slot's record, read as the latest head (a version 1 record named "New World"); NULL: none */+static const uint8_t *read_head(int s, Head *h, uint32_t *len, uint32_t *head_len) {+ char rec[8];+ record_of(s, rec);+ const uint8_t *d = plat_load(rec, len);+ if (!d || *len < sizeof(Head1)) return NULL;+ memset(h, 0, sizeof *h);+ if (!memcmp(d, "NBW1", 4)) {+ memcpy(&h->h, d, sizeof(Head1));+ copy_name(h->name, "New World");+ *head_len = sizeof(Head1);+ } else if (!memcmp(d, "NBW2", 4) && *len >= sizeof(Head)) {+ memcpy(h, d, sizeof(Head));+ h->name[WORLD_NAME] = 0;+ *head_len = sizeof(Head);+ } else+ return NULL;+ if (h->h.player_size != sizeof pl || *len < *head_len + sizeof pl) return NULL;+ return d;+}++bool world_info(int s, WorldInfo *w) {+ Head h;+ uint32_t len, hl;+ const uint8_t *d = read_head(s, &h, &len, &hl);+ if (!d) return false;+ Player p;+ memcpy(&p, d + hl, sizeof p);+ copy_name(w->name, h.name);+ w->mode = p.mode;+ w->seed = h.h.seed;+ w->played = h.played;+ return true;+}++int world_free_slot(void) {+ WorldInfo w;+ for (int s = 1; s <= MAX_WORLDS; s++)+ if (!world_info(s, &w)) return s;+ return 0;+}++static bool name_taken(const char *n) {+ WorldInfo w;+ for (int s = 1; s <= MAX_WORLDS; s++)+ if (world_info(s, &w) && !strcmp(w.name, n)) return true;+ return false;+}++void world_unique_name(const char *base, char *out) {+ char b[WORLD_NAME + 1];+ copy_name(b, base[0] ? base : "New World");+ copy_name(out, b);+ for (int k = 2; name_taken(out) && k < 100; k++) {+ /* "New World (2)": the number kept whole, the name cut to fit */+ char sfx[8] = {' ', '(', 0};+ int n = 2;+ if (k >= 10) sfx[n++] = (char)('0' + k / 10);+ sfx[n++] = (char)('0' + k % 10), sfx[n++] = ')', sfx[n] = 0;+ int bl = (int)strlen(b), room = WORLD_NAME - n;+ if (bl > room) bl = room;+ memcpy(out, b, (size_t)bl);+ memcpy(out + bl, sfx, (size_t)n + 1);+ }+}bool save_world(void) {bool ok = edits_flush();uint32_t tl;void *t = tiles_data(&tl);- Head h = {{'N', 'B', 'W', '1'}, (uint16_t)sizeof pl, (uint16_t)tl, world_seed, game_time, weather};+ Head h;+ memset(&h, 0, sizeof h);+ h.h = (Head1){{'N', 'B', 'W', '2'}, (uint16_t)sizeof pl, (uint16_t)tl, world_seed, game_time, weather};+ copy_name(h.name, cur_name);+ h.played = cur_played;/* one record: the head, the player, the tiles (written from a buffer made in the merge scratch) */uint32_t n = sizeof h + sizeof pl + tl;uint8_t *b = (uint8_t *)edits_scratch(n);@@ -37,52 +125,51 @@ bool save_world(void) {memcpy(b, &h, sizeof h);memcpy(b + sizeof h, &pl, sizeof pl);memcpy(b + sizeof h + sizeof pl, t, tl);- ok = plat_save(WORLD_REC, b, n) && ok;+ char rec[8];+ record_of(slot, rec);+ ok = plat_save(rec, b, n) && ok;edits_forget();- (void)buf_head;return ok;}-bool world_exists(void) {- uint32_t len = 0;- const uint8_t *d = plat_load(WORLD_REC, &len);- return d && len >= sizeof(Head) && !memcmp(d, "NBW1", 4);+/* the world becoming the one played: the latest in the list */+static void played(int s) {+ opt.last_world = (uint8_t)s;+ cur_played = ++opt.plays;+ save_options();}-/* the saved world's game mode, or -1 */-int world_mode(void) {- uint32_t len = 0;- const uint8_t *d = plat_load(WORLD_REC, &len);- if (!d || len < sizeof(Head) + sizeof pl || memcmp(d, "NBW1", 4)) return -1;- Player p;- memcpy(&p, d + sizeof(Head), sizeof p);- return p.mode;-}--bool load_world(void) {- uint32_t len = 0, tl;- const uint8_t *d = plat_load(WORLD_REC, &len);- void *t = tiles_data(&tl);+bool load_world(int s) {Head h;- if (!d || len < sizeof h) return false;- memcpy(&h, d, sizeof h);- if (memcmp(h.magic, "NBW1", 4) || h.player_size != sizeof pl || h.tiles_size != tl || len != sizeof h + sizeof pl + tl)- return false;- world_seed = h.seed;- game_time = h.time;+ uint32_t len, hl, tl;+ const uint8_t *d = read_head(s, &h, &len, &hl);+ void *t = tiles_data(&tl);+ if (!d || h.h.tiles_size != tl || len != hl + sizeof pl + tl) return false;+ slot = s;+ copy_name(cur_name, h.name);+ cur_played = h.played;+ world_seed = h.h.seed;+ game_time = h.h.time;/* World.calculateInitialWeather */- weather = h.weather;+ weather = h.h.weather;rain_str = weather.raining ? 1 : 0;thunder_str = weather.thundering ? 1 : 0;- memcpy(&pl, d + sizeof h, sizeof pl);- memcpy(t, d + sizeof h + sizeof pl, tl);+ memcpy(&pl, d + hl, sizeof pl);+ memcpy(t, d + hl + sizeof pl, tl);memset(ents, 0, sizeof ents);- world_new(world_seed, WORLD);+ char pre[4];+ prefix_of(s, pre);+ world_new(world_seed, pre);+ if (s != opt.last_world || h.played != opt.plays) played(s);return true;}-void new_world(int64_t seed, int mode) {- plat_remove_prefix(WORLD);+void new_world(int s, int64_t seed, int mode, const char *name) {+ char pre[4];+ prefix_of(s, pre);+ plat_remove_prefix(pre);+ slot = s;+ copy_name(cur_name, name);uint32_t tl;void *t = tiles_data(&tl); /* (first: the length is only known after the call) */memset(t, 0, tl);@@ -91,16 +178,44 @@ void new_world(int64_t seed, int mode) {game_time = 0;memset(&weather, 0, sizeof weather);rain_str = thunder_str = 0;- world_new(seed, WORLD);+ world_new(seed, pre);player_spawn();pl.mode = (uint8_t)mode;+ played(s);}-void delete_world(void) { plat_remove_prefix(WORLD); }+bool rename_world(int s, const char *name) {+ Head h;+ uint32_t len, hl;+ const uint8_t *d = read_head(s, &h, &len, &hl);+ if (!d) return false;+ /* the record again with the new head (made in the merge scratch, as saving does) */+ uint32_t body = len - hl, n = sizeof h + body;+ uint8_t *b = (uint8_t *)edits_scratch(n);+ if (!b) return false;+ memcpy(b + sizeof h, d + hl, body); /* (first: the scratch may be where nothing else is) */+ memcpy(h.h.magic, "NBW2", 4);+ copy_name(h.name, name);+ memcpy(b, &h, sizeof h);+ char rec[8];+ record_of(s, rec);+ bool ok = plat_save(rec, b, n);+ edits_forget();+ if (s == slot) copy_name(cur_name, name);+ return ok;+}++void delete_world(int s) {+ char pre[4];+ prefix_of(s, pre);+ plat_remove_prefix(pre);+ if (opt.last_world == s) opt.last_world = 0, save_options();+}void load_options(void) {uint32_t len = 0;const uint8_t *d = plat_load(OPTIONS_REC, &len);- if (d && len == sizeof opt) memcpy(&opt, d, sizeof opt);+ /* (an older, shorter record: what it has; the rest stays as it was) */+ if (d && len <= sizeof opt) memcpy(&opt, d, len);}void save_options(void) { plat_save(OPTIONS_REC, &opt, sizeof opt); }diff --git a/games/numblocks/tests/unit.c b/games/numblocks/tests/unit.cindex c4306978..d7dc4001 100644--- a/games/numblocks/tests/unit.c+++ b/games/numblocks/tests/unit.c@@ -312,6 +312,30 @@ int main(void) {CHECK(!memcmp(l, vl, sizeof l));plat_remove_prefix("ut9");}+ {+ /* worlds in slots: names kept, made unique, renamed; one deleted leaves the others */+ for (int s = 1; s <= MAX_WORLDS; s++) delete_world(s);+ char n[WORLD_NAME + 1];+ WorldInfo w;+ world_unique_name("New World", n);+ CHECK(!strcmp(n, "New World"));+ new_world(world_free_slot(), 5, 0, n);+ save_world();+ world_unique_name("New World", n);+ CHECK(!strcmp(n, "New World (2)"));+ int s2 = world_free_slot();+ new_world(s2, 6, 1, n);+ save_world();+ CHECK(s2 == 2 && world_info(1, &w) && !strcmp(w.name, "New World") && w.mode == 0 && w.seed == 5);+ CHECK(world_info(2, &w) && !strcmp(w.name, "New World (2)") && w.mode == 1 && w.seed == 6);+ CHECK(rename_world(1, "Base") && world_info(1, &w) && !strcmp(w.name, "Base"));+ world_unique_name("New World", n);+ CHECK(!strcmp(n, "New World"));+ delete_world(1);+ CHECK(!world_info(1, &w) && world_info(2, &w) && world_free_slot() == 1);+ CHECK(load_world(2) && pl.mode == 1 && world_seed == 6);+ for (int s = 1; s <= MAX_WORLDS; s++) delete_world(s);+ }printf("%s (%d recipes)\n", fails ? "FAILED" : "all good", N_RECIPES);return fails != 0;}diff --git a/tests/test_games.py b/tests/test_games.pyindex 346c9dbd..9090f304 100644--- a/tests/test_games.py+++ b/tests/test_games.py@@ -104,8 +104,8 @@ GAMES = {# past the key sheet, Singleplayer, Create New World (Survival, a random seed), walk; then# Play Selected World"numblocks": ("NumBlocks.nwa", "nb1.nbw",- presses((3000, "ok"), (3600, "ok"), (4200, "ok"), (4800, "down"), (5100, "down"), (5400, "ok"),- (8100, "comma", 1500)), 11600,+ presses((3000, "ok"), (3600, "ok"), (4200, "ok"), (4800, "down"), (5100, "down"), (5400, "down"),+ (5700, "ok"), (8100, "comma", 1500)), 11600,presses((3000, "ok"), (3600, "ok")), 8000),# a level, its message, then play; then back to the level select"portal": ("PortalReturns.nwa", "portal.sav",@@ -167,8 +167,8 @@ CHECKS = {and v[34] == 5 and struct.unpack_from("<H", v, 14)[0] == 1,lambda v: v[32] == 9 and struct.unpack_from("<H", v, 14)[0] == 2),# the world's record: its magic, then (offset 16) the time of day, which goes on from one session to the next- "numblocks": (lambda v: v[:4] == b"NBW1" and struct.unpack_from("<I", v, 16)[0] > 0,- lambda v: v[:4] == b"NBW1" and struct.unpack_from("<I", v, 16)[0] > 60),+ "numblocks": (lambda v: v[:4] == b"NBW2" and struct.unpack_from("<I", v, 16)[0] > 0,+ lambda v: v[:4] == b"NBW2" and struct.unpack_from("<I", v, 16)[0] > 60),"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}