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.

NumBlocks: superflat worlds

Mason Chenb53853f9bfc9
diff --git a/games/numblocks/README.md b/games/numblocks/README.md
index 82bafbb8..385f6e7e 100644
--- a/games/numblocks/README.md
+++ b/games/numblocks/README.md
@@ -4,7 +4,7 @@ Mine, craft and survive on your NumWorks calculator, in a world like Minecraft 1
<img src="docs/shot.png" width="320" alt="NumBlocks: oak trees and lilacs, with a sheep, a cow, a pig, a chicken and a creeper">
-- **A real 1.8 world:** the same biomes, hills, caves, lakes and trees as Minecraft 1.8.8, for any seed.
+- **A real 1.8 world:** the same biomes, hills, caves, lakes and trees as Minecraft 1.8.8, for any seed. Or a superflat world to build on.
- **Survival:** health, hunger, day and night, zombies, skeletons, creepers and spiders, and pigs, cows, sheep and chickens.
- **Weather:** rain, snow and thunderstorms with lightning, as often as in 1.8.
- **Crafting:** 129 items and all their 1.8 recipes, a furnace, chests, tools that wear out, armor.
diff --git a/games/numblocks/last.png b/games/numblocks/last.png
new file mode 100644
index 00000000..a51cf4c9
Binary files /dev/null and b/games/numblocks/last.png differ
diff --git a/games/numblocks/src/gen.c b/games/numblocks/src/gen.c
index 824d98b2..55d8f2a8 100644
--- a/games/numblocks/src/gen.c
+++ b/games/numblocks/src/gen.c
@@ -5451,7 +5451,13 @@ static void slab_caves(int cx, int cz, const ColSum *sum, uint8_t *out, int y0,
/* Ahead of gen_slab(cx, cz): one of the summaries of the chunks around it that it will need, if
* one is not kept (each is a chunk's terrain, so the work of a chunk can be spread over frames).
* 0 when they are all there. */
+/* ---------------------------------------------------------------- superflat (ChunkProviderFlat) */
+static int g_flat;
+void gen_set_flat(int flat) { g_flat = flat; }
+static int flat_block(int y) { return y == 0 ? B_BEDROCK : y < 3 ? B_DIRT : y == 3 ? B_GRASS : B_AIR; }
+
int gen_prepare(int cx, int cz) {
+ if (g_flat) return 0;
for (int dz = -1; dz <= 1; dz++)
for (int dx = -1; dx <= 1; dx++)
if ((dx || dz) && !sum_find(cx + dx, cz + dz)) {
@@ -5468,6 +5474,10 @@ void gen_slab(int cx, int cz, int y0, int h, uint8_t *out) {
}
if (y0 + h > 128) h = 128 - y0;
if (h <= 0) return;
+ if (g_flat) {
+ for (int y = 0; y < h; y++) memset(out + y * 256, flat_block(y0 + y), 256);
+ return;
+ }
c_out = out;
c_y0 = y0;
c_h = h;
@@ -5511,6 +5521,7 @@ void gen_slab(int cx, int cz, int y0, int h, uint8_t *out) {
}
int gen_biome(int x, int z) {
+ if (g_flat) return BI_PLAINS;
int cx = x >> 4, cz = z >> 4, i = (x & 15) + (z & 15) * 16;
if (cx == last_cx && cz == last_cz) return c_b16[i];
if (cx != bc_cx || cz != bc_cz) chunk_biomes(cx, cz);
@@ -5518,6 +5529,7 @@ int gen_biome(int x, int z) {
}
int gen_top(int x, int z) {
+ if (g_flat) return 3;
int cx = x >> 4, cz = z >> 4, i = (x & 15) + (z & 15) * 16;
if (cx != last_cx || cz != last_cz) gen_slab(cx, cz, 127, 1, col_tmp);
int t = c_top[i];
@@ -5537,6 +5549,13 @@ static int can_spawn(int x, int z) {
void gen_spawn(int *px, int *py, int *pz) {
JRand r;
jr_seed(&r, g_seed);
+ if (g_flat) {
+ /* WorldChunkManagerHell.findBiomePosition(0, 0, 256): anywhere within 256, all of it grass */
+ *px = -256 + jr_int(&r, 513);
+ *pz = -256 + jr_int(&r, 513);
+ *py = 4;
+ return;
+ }
/* WorldChunkManager.a(0, 0, 256, spawn biomes, random): 129 x 129 cells of rivermix */
int found = 0, fx = 0, fz = 0, j2 = 0;
uint8_t *rows = (uint8_t *)U.lay + sizeof(i32) * LAY_INTS; /* 8 rows x 129, after the layer scratch */
diff --git a/games/numblocks/src/gen.h b/games/numblocks/src/gen.h
index 3e737760..a9643e4b 100644
--- a/games/numblocks/src/gen.h
+++ b/games/numblocks/src/gen.h
@@ -9,6 +9,10 @@
/* Starts a world: seed as a Java long (what /seed prints). Call before anything else. */
void gen_init(int64_t seed);
+/* The world type: 0 Minecraft's default, 1 superflat (FlatGeneratorInfo's default preset
+ * "2;7,2x3,2;1": bedrock, two dirt, grass, all plains, nothing else). Kept across gen_init. */
+void gen_set_flat(int flat);
+
/* Blocks of chunk (cx, cz) for y in [y0, y0 + h), after terrain, caves, ravines
* and the decoration (population) that Minecraft would put in this chunk:
* out[(y - y0) * 256 + z * 16 + x], x and z in 0..15, values from blocks.h (B_*).
diff --git a/games/numblocks/src/gui.c b/games/numblocks/src/gui.c
index 476d0d59..b37d0918 100644
--- a/games/numblocks/src/gui.c
+++ b/games/numblocks/src/gui.c
@@ -849,10 +849,11 @@ 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_NAME, B_RENAME, B_DORENAME, B_RECREATE, B_CHEATS, B_LAN, B_LAN_MODE,
- B_LAN_CHEATS, B_LAN_START };
+ B_LAN_CHEATS, B_LAN_START, B_TYPE };
int menu_choice; /* an action for main.c (ACT_*) */
int create_mode; /* the new world's game mode */
bool create_cheats; /* and whether it allows cheats */
+int create_type; /* and its type (WT_*) */
static bool cheats_set; /* (chosen, not following the game mode) */
static bool lan_mode, lan_allow; /* Open to LAN's settings: the game mode for others, cheats */
char seed_text[21]; /* the new world's seed, as typed */
@@ -1123,7 +1124,9 @@ static void menu(int screen) {
cat(t, "Allow Cheats: ", create_cheats ? "ON" : "OFF");
add_button(w / 2 + 5, 100, 150, 1, B_CHEATS, t);
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 - 100, 146, 200, 1, B_SEED, t);
+ cat(t, "World Type: ", create_type == WT_FLAT ? "Superflat" : "Default");
+ add_button(w / 2 - 100, 182, 200, 1, B_TYPE, 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;
@@ -1222,7 +1225,7 @@ static void press(int id) {
if (selected(&wi)) play_slot = wlist[wsel], menu_choice = ACT_PLAY;
break;
case B_CREATE:
- create_mode = 0;
+ create_mode = 0, create_type = WT_DEFAULT;
create_cheats = cheats_set = false;
seed_text[0] = 0;
world_unique_name("New World", name_text);
@@ -1236,7 +1239,7 @@ static void press(int id) {
n[WORLD_NAME] = 0;
world_unique_name(n, name_text);
create_mode = wi.mode;
- create_cheats = wi.cheats, cheats_set = false;
+ create_cheats = wi.cheats, cheats_set = false, create_type = wi.type;
/* (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];
@@ -1272,6 +1275,7 @@ static void press(int id) {
if (!cheats_set) create_cheats = create_mode;
break;
case B_CHEATS: create_cheats = !create_cheats, cheats_set = true; break;
+ case B_TYPE: create_type = create_type == WT_FLAT ? WT_DEFAULT : WT_FLAT; break;
case B_LAN:
lan_mode = lan_allow = false;
gui_menu(GUI_LAN);
@@ -1710,7 +1714,7 @@ static void menu_screen(void) {
text_center(create_mode ? "destroy blocks instantly" : "levels, health and hunger", w / 2, 134,
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));
+ w / 2, 169, RGB(0xA0, 0xA0, 0xA0));
break;
case GUI_RENAME:
text_center("Rename World", w / 2, 20, 0xFFFF);
@@ -1743,7 +1747,8 @@ static void menu_screen(void) {
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[2];
+ Button *b = &buttons[0];
+ while (b->id != B_SEED) b++;
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);
diff --git a/games/numblocks/src/main.c b/games/numblocks/src/main.c
index 3b8f0af6..f058faf2 100644
--- a/games/numblocks/src/main.c
+++ b/games/numblocks/src/main.c
@@ -16,6 +16,7 @@ uint32_t ticks_run; /* ticks played (what waits a few ticks counts these: the
uint32_t game_time = 1000; /* Minecraft's time of day: 0 sunrise, 6000 noon */
bool start_in_world; /* tests: straight into a new world */
int64_t start_seed; /* (its seed) */
+int start_type; /* (and type) */
static uint32_t last, acc, keys_held, autosave;
static float px, py, pz;
@@ -31,6 +32,7 @@ void camera_reset(void) {
static void title(void) {
in_world = false;
if (!opt.last_world || !load_world(opt.last_world)) {
+ world_type = WT_DEFAULT;
world_new(0, "nbt");
player_spawn();
}
@@ -93,7 +95,7 @@ void game_init(void) {
#endif
last = plat_millis();
if (start_in_world) {
- new_world(1, start_seed, 0, false, "New World");
+ new_world(1, start_seed, 0, false, start_type, "New World");
game_time = 1000;
#ifdef BENCH_FALL
pl.mode = 1, pl.y += BENCH_FALL; /* (timing a fall from that high, in Creative: no harm) */
@@ -146,7 +148,7 @@ bool game_frame(void) {
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, create_cheats, name);
+ new_world(slot, parse_seed(), create_mode, create_cheats, create_type, name);
game_time = 0;
loading();
save_world();
diff --git a/games/numblocks/src/nb.h b/games/numblocks/src/nb.h
index a1b64c09..ccf29ab6 100644
--- a/games/numblocks/src/nb.h
+++ b/games/numblocks/src/nb.h
@@ -60,6 +60,7 @@ static inline bool is_lava(int b) { return b == B_LAVA || b == B_FLOWING_LAVA ||
/* ---------------------------------------------------------------- the generator (gen.c) */
void gen_init(int64_t seed);
+void gen_set_flat(int flat); /* superflat (kept across gen_init) */
void gen_slab(int cx, int cz, int y0, int h, uint8_t *out); /* out[(y - y0) * 256 + z * 16 + x] */
int gen_prepare(int cx, int cz); /* a slice of the work for gen_slab(cx, cz) ahead; 0: nothing left */
int gen_biome(int x, int z);
@@ -265,6 +266,7 @@ 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 bool create_cheats;
+extern int create_type;
extern char seed_text[21];
/* ---------------------------------------------------------------- saves and options (save.c) */
@@ -286,6 +288,8 @@ void keys_reset(void); /* the keys as they start */
#define KEY_SHEET 3 /* (2: jump and sneak on shift and alpha; 3: chat and commands) */
extern Options opt;
extern int64_t world_seed;
+enum { WT_DEFAULT, WT_FLAT };
+extern uint8_t world_type; /* the world type (WT_*): Default, or Superflat */
/* 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 */
@@ -293,6 +297,7 @@ typedef struct {
char name[WORLD_NAME + 1];
int mode; /* 0 survival, 1 creative */
bool cheats;
+ int type; /* WT_* */
int64_t seed;
uint32_t played; /* opt.plays when it was last played (the latest first in the list) */
} WorldInfo;
@@ -301,7 +306,7 @@ 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(int slot);
-void new_world(int slot, int64_t seed, int mode, bool cheats, const char *name);
+void new_world(int slot, int64_t seed, int mode, bool cheats, int type, const char *name);
bool rename_world(int slot, const char *name);
void delete_world(int slot);
void load_options(void);
diff --git a/games/numblocks/src/save.c b/games/numblocks/src/save.c
index 2527175c..d2fc9e94 100644
--- a/games/numblocks/src/save.c
+++ b/games/numblocks/src/save.c
@@ -12,6 +12,7 @@
Options opt = {2, 0, 100, 1, 1, 0, 0, 0, {0}};
int64_t world_seed;
+uint8_t world_type;
/* the record's head: version 1 had no name (it was "New World") */
typedef struct {
@@ -28,7 +29,7 @@ typedef struct {
} Head2; /* version 2 had no cheats or game rules */
typedef struct {
Head2 h2;
- uint8_t cheats, rules, pad[2];
+ uint8_t cheats, rules, type, pad; /* (type: 0 in worlds from before there were others) */
} Head;
extern uint32_t game_time;
@@ -90,6 +91,7 @@ bool world_info(int s, WorldInfo *w) {
copy_name(w->name, h.h2.name);
w->mode = p.mode;
w->cheats = h.cheats;
+ w->type = h.type;
w->seed = h.h2.h.seed;
w->played = h.h2.played;
return true;
@@ -135,7 +137,7 @@ bool save_world(void) {
h.h2.h = (Head1){{'N', 'B', 'W', '3'}, (uint16_t)sizeof pl, (uint16_t)tl, world_seed, game_time, weather};
copy_name(h.h2.name, cur_name);
h.h2.played = cur_played;
- h.cheats = world_cheats, h.rules = game_rules;
+ h.cheats = world_cheats, h.rules = game_rules, h.type = world_type;
/* 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);
@@ -168,7 +170,7 @@ bool load_world(int s) {
cur_played = h.h2.played;
world_seed = h.h2.h.seed;
game_time = h.h2.h.time;
- world_cheats = h.cheats, lan_open = lan_cheats = false, game_rules = h.rules;
+ world_cheats = h.cheats, lan_open = lan_cheats = false, game_rules = h.rules, world_type = h.type;
/* World.calculateInitialWeather */
weather = h.h2.h.weather;
rain_str = weather.raining ? 1 : 0;
@@ -183,7 +185,7 @@ bool load_world(int s) {
return true;
}
-void new_world(int s, int64_t seed, int mode, bool cheats, const char *name) {
+void new_world(int s, int64_t seed, int mode, bool cheats, int type, const char *name) {
char pre[4];
prefix_of(s, pre);
plat_remove_prefix(pre);
@@ -195,7 +197,7 @@ void new_world(int s, int64_t seed, int mode, bool cheats, const char *name) {
memset(ents, 0, sizeof ents);
world_seed = seed;
game_time = 0;
- world_cheats = cheats, lan_open = lan_cheats = false, game_rules = GR_DEFAULT;
+ world_cheats = cheats, lan_open = lan_cheats = false, game_rules = GR_DEFAULT, world_type = (uint8_t)type;
memset(&weather, 0, sizeof weather);
rain_str = thunder_str = 0;
world_new(seed, pre);
diff --git a/games/numblocks/src/world.c b/games/numblocks/src/world.c
index 9f286aa9..e3fc889a 100644
--- a/games/numblocks/src/world.c
+++ b/games/numblocks/src/world.c
@@ -334,6 +334,7 @@ static void recenter(int nx0, int ny0, int nz0, int px, int pz) {
}
void world_new(int64_t seed, const char *name) {
+ gen_set_flat(world_type == WT_FLAT);
gen_init(seed);
edits_setup(name, slab32, sizeof slab32 / 4);
edits_clear();
diff --git a/games/numblocks/tests/unit.c b/games/numblocks/tests/unit.c
index 071aa9de..ac5d3f2e 100644
--- a/games/numblocks/tests/unit.c
+++ b/games/numblocks/tests/unit.c
@@ -340,12 +340,12 @@ int main(void) {
WorldInfo w;
world_unique_name("New World", n);
CHECK(!strcmp(n, "New World"));
- new_world(world_free_slot(), 5, 0, false, n);
+ new_world(world_free_slot(), 5, 0, false, WT_DEFAULT, 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, true, n);
+ new_world(s2, 6, 1, true, WT_DEFAULT, 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);
@@ -385,7 +385,7 @@ int main(void) {
{
/* commands: what they do; most need cheats */
extern uint32_t game_time;
- new_world(1, 9, 0, false, "Commands");
+ new_world(1, 9, 0, false, WT_DEFAULT, "Commands");
world_follow(pl.x, pl.y, pl.z);
while (world_pending()) world_follow(pl.x, pl.y, pl.z);
command_run("/gamemode 1");
@@ -495,6 +495,24 @@ int main(void) {
CHECK(world_get(16, 70, 17) == B_FIRE && pl.inv[0].aux == 1);
plat_remove_prefix("ut12");
}
+ {
+ /* superflat: bedrock, two dirt, grass; the player on it; kept as superflat */
+ new_world(2, 77, 0, false, WT_FLAT, "Flat");
+ world_follow(pl.x, pl.y, pl.z);
+ while (world_pending()) world_follow(pl.x, pl.y, pl.z);
+ int x = (int)floorf(pl.x) + 7, z = (int)floorf(pl.z) - 5;
+ CHECK(pl.y == 4 && world_get(x, 0, z) == B_BEDROCK && world_get(x, 1, z) == B_DIRT && world_get(x, 2, z) == B_DIRT &&
+ world_get(x, 3, z) == B_GRASS && world_get(x, 4, z) == B_AIR && world_get(x, 40, z) == B_AIR);
+ save_world();
+ world_type = WT_DEFAULT;
+ WorldInfo wi;
+ CHECK(load_world(2) && world_type == WT_FLAT && world_info(2, &wi) && wi.type == WT_FLAT);
+ world_follow(pl.x, pl.y, pl.z);
+ while (world_pending()) world_follow(pl.x, pl.y, pl.z);
+ CHECK(world_get(x + 30, 3, z) == B_GRASS || !world_loaded(x + 30, 3, z));
+ delete_world(2);
+ world_type = WT_DEFAULT;
+ }
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.py
index 95c0c0fc..f24fcb3f 100644
--- a/tests/test_games.py
+++ b/tests/test_games.py
@@ -105,7 +105,7 @@ GAMES = {
# Play Selected World
"numblocks": ("NumBlocks.nwa", "nb1.nbw",
presses((3000, "ok"), (3600, "ok"), (4200, "ok"), (4800, "down"), (5100, "down"), (5400, "down"),
- (5700, "down"), (6000, "ok"), (8400, "comma", 1500)), 11900,
+ (5700, "down"), (6000, "down"), (6300, "ok"), (8700, "comma", 1500)), 12200,
presses((3000, "ok"), (3600, "ok")), 8000),
# a level, its message, then play; then back to the level select
"portal": ("PortalReturns.nwa", "portal.sav",