Change whole gui system

This commit is contained in:
2026-10-01 16:22:02 -03:00
parent 013a4fbb97
commit deba36260e
48 changed files with 1925 additions and 1302 deletions
+58 -54
View File
@@ -2,7 +2,7 @@
import { assert, assertEquals } from "@std/assert";
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import { display_name, get_state_value } from "$/common/utils.ts";
import { test_game } from "./helpers.ts";
import { menu_data, menu_items, opened_menu, test_game } from "./helpers.ts";
// deno-lint-ignore no-explicit-any
type Msg = any;
@@ -30,10 +30,18 @@ function slot_of(messages: Msg[], id: string) {
return slot;
}
// picks a whole inventory stack up and puts it into a slot of the open screen
function move_to_screen(t: Game, from: number, to: number) {
t.send(1, { type: "click", container: "inventory", index: from, button: 0 });
t.send(1, { type: "click", container: "screen", index: to, button: 0 });
// clicks a slot of the open menu
function click(t: Game, menu: Msg, slot: number) {
t.send(1, { type: "click", menu: menu.id, slot, button: 0 });
}
// the menu slot of an inventory slot: the base game's menus add the player's inventory last
const inventory_slot = (menu: Msg, index: number) => menu.slots.length - 36 + index;
// picks a whole inventory stack up and puts it into a slot of the open menu
function move_to_menu(t: Game, menu: Msg, from: number, to: number) {
click(t, menu, inventory_slot(menu, from));
click(t, menu, to);
}
Deno.test("a hoe turns grass and dirt into hoed dirt, anything else places nothing", async () => {
@@ -62,27 +70,28 @@ Deno.test("a chest keeps items, shows them to everyone, and drops them when brok
const coal = slot_of(t.take(1), "bworld:coal");
t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
const opened = t.take(1).find((m) => m.type === "open_screen");
assertEquals(opened.layout.rows, 3);
assertEquals(opened.layout.slots.length, 27);
const opened = opened_menu(t.take(1));
assertEquals(opened.menu_type, "bworld:storage");
assertEquals(opened.open_data, { rows: 3 });
assertEquals(opened.slots.length, 27 + 36);
// nothing got placed on top
assertEquals(t.game.world.get_block_id(1, 101, 0), "bworld:air");
move_to_screen(t, coal, 4);
assertEquals(last_container(t.take(1), "screen")[4], { id: "bworld:coal", count: 10 });
t.send(1, { type: "close_screen" });
move_to_menu(t, opened, coal, 4);
assertEquals(menu_items(t.take(1))[4], { id: "bworld:coal", count: 10 });
t.send(1, { type: "close_menu", menu: opened.id });
// still there after opening it again, and after saving and loading
const loaded = await test_game("mods", t.game.save());
loaded.join(1, "alice");
loaded.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
loaded.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
assertEquals(last_container(loaded.take(1), "screen")[4], { id: "bworld:coal", count: 10 });
assertEquals(menu_items(loaded.take(1))[4], { id: "bworld:coal", count: 10 });
// breaking it closes the screen and drops the chest and what was inside
// breaking it closes the menu and drops the chest and what was inside
loaded.send(1, { type: "break_block", x: 1, y: 100, z: 0 });
const messages = loaded.take(1);
assert(messages.some((m) => m.type === "close_screen"));
assert(messages.some((m) => m.type === "close_menu"));
const dropped = loaded.game.item_entities().map((e) => e.item.to_data());
assertEquals(
dropped.sort((a, b) => a.id.localeCompare(b.id)),
@@ -98,36 +107,35 @@ Deno.test("a furnace burns fuel to smelt, shows its progress, and only lets the
const coal = slot_of(messages, "bworld:coal");
t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
const opened = t.take(1).find((m) => m.type === "open_screen");
assertEquals(opened.layout.slots.map((s: Msg) => s.index), [0, 1, 2]);
assertEquals(opened.layout.slots[2].output, true);
assertEquals(opened.layout.bars.map((b: Msg) => b.value), ["fuel", "progress"]);
const opened = opened_menu(t.take(1));
assertEquals(opened.slots.slice(0, 3).map((s: Msg) => s.output ?? false), [false, false, true]);
assertEquals(Object.keys(opened.data), ["progress", "progress_max", "fuel", "fuel_max"]);
move_to_screen(t, ore, 0);
move_to_screen(t, coal, 1);
move_to_menu(t, opened, ore, 0);
move_to_menu(t, opened, coal, 1);
t.take(1);
// iron takes 200 ticks
for (let i = 0; i < 100; i++) t.game.tick();
const halfway = t.take(1).filter((m) => m.type === "screen_properties").at(-1).properties;
const halfway = menu_data(t.take(1));
assertEquals(halfway.progress_max, 200);
assert(halfway.progress >= 98 && halfway.progress <= 100, `progress ${halfway.progress}`);
assertEquals(halfway.fuel_max, 1000);
for (let i = 0; i < 110; i++) t.game.tick();
const screen = last_container(t.take(1), "screen");
assertEquals(screen[0], { id: "bworld:iron_ore", count: 1 });
assertEquals(screen[1], null, "the coal got used");
assertEquals(screen[2], { id: "bworld:iron_ingot", count: 1 });
const items = menu_items(t.take(1));
assertEquals(items[0], { id: "bworld:iron_ore", count: 1 });
assertEquals(items[1], null, "the coal got used");
assertEquals(items[2], { id: "bworld:iron_ingot", count: 1 });
// nothing can be put into the result slot, but the result can be taken
t.send(1, { type: "click", container: "screen", index: 0, button: 0 });
t.send(1, { type: "click", container: "screen", index: 2, button: 0 });
assertEquals(last_container(t.take(1), "screen")[2], { id: "bworld:iron_ingot", count: 1 });
t.send(1, { type: "click", container: "screen", index: 0, button: 0 });
t.send(1, { type: "click", container: "screen", index: 2, button: 0 });
click(t, opened, 0);
click(t, opened, 2);
assertEquals(menu_items(t.take(1))[2], { id: "bworld:iron_ingot", count: 1 });
click(t, opened, 0);
click(t, opened, 2);
const after = t.take(1);
assertEquals(last_container(after, "screen")[2], null);
assertEquals(menu_items(after)[2], null);
assertEquals(after.filter((m) => m.type === "cursor").at(-1).item, { id: "bworld:iron_ingot", count: 1 });
});
@@ -192,31 +200,26 @@ Deno.test("a smithing table upgrades a wooden pickaxe with 5 stone, using up exa
const stone = slot_of(messages, "bworld:stone");
t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
const opened = t.take(1).find((m) => m.type === "open_screen");
assertEquals(opened.layout.slots.map((s: Msg) => [s.index, s.output ?? false]), [
[0, false],
[1, false],
[2, false],
[3, true],
]);
const opened = opened_menu(t.take(1));
assertEquals(opened.slots.slice(0, 4).map((s: Msg) => s.output ?? false), [false, false, false, true]);
// with only the tool there's nothing to take
move_to_screen(t, pickaxe, 0);
move_to_menu(t, opened, pickaxe, 0);
t.game.tick();
t.send(1, { type: "click", container: "screen", index: 3, button: 0 });
click(t, opened, 3);
assertEquals(t.take(1).filter((m) => m.type === "cursor").at(-1)?.item ?? null, null);
// the result shows up once there's enough stone, and taking it uses up the pickaxe and 5 stone
move_to_screen(t, stone, 1);
move_to_menu(t, opened, stone, 1);
t.game.tick();
assertEquals(last_container(t.take(1), "screen")[3], { id: "bworld:stone_pickaxe", count: 1 });
t.send(1, { type: "click", container: "screen", index: 3, button: 0 });
assertEquals(menu_items(t.take(1))[3], { id: "bworld:stone_pickaxe", count: 1 });
click(t, opened, 3);
messages = t.take(1);
assertEquals(messages.filter((m) => m.type === "cursor").at(-1).item, { id: "bworld:stone_pickaxe", count: 1 });
assertEquals(last_container(messages, "screen"), [null, { id: "bworld:stone", count: 2 }, null, null]);
assertEquals(menu_items(messages).slice(0, 4), [null, { id: "bworld:stone", count: 2 }, null, null]);
// closing gives back the leftover stone, and the cursor's pickaxe goes into the inventory
t.send(1, { type: "close_screen" });
t.send(1, { type: "close_menu", menu: opened.id });
const inventory = last_container(t.take(1), "inventory").filter((i: Msg) => i);
assertEquals(
inventory.sort((a: Msg, b: Msg) => a.id.localeCompare(b.id)),
@@ -230,23 +233,24 @@ Deno.test("a smithing result can't be taken once its inputs are gone, even befor
t.game.set_block(1, 100, 0, "bworld:smithing_table");
const messages = t.take(1);
t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
move_to_screen(t, slot_of(messages, "bworld:wood_pickaxe"), 0);
move_to_screen(t, slot_of(messages, "bworld:stone"), 1);
const opened = opened_menu(t.take(1));
move_to_menu(t, opened, slot_of(messages, "bworld:wood_pickaxe"), 0);
move_to_menu(t, opened, slot_of(messages, "bworld:stone"), 1);
t.game.tick();
// take the pickaxe back out and try the result in the same tick
t.send(1, { type: "click", container: "screen", index: 0, button: 0 });
t.send(1, { type: "click", container: "inventory", index: 20, button: 0 });
t.send(1, { type: "click", container: "screen", index: 3, button: 0 });
click(t, opened, 0);
click(t, opened, inventory_slot(opened, 20));
click(t, opened, 3);
const cursor = t.take(1).filter((m) => m.type === "cursor").at(-1).item;
assertEquals(cursor, null);
// and an extra item in the third slot doesn't match a recipe without one
t.send(1, { type: "click", container: "inventory", index: 20, button: 0 });
t.send(1, { type: "click", container: "screen", index: 0, button: 0 });
click(t, opened, inventory_slot(opened, 20));
click(t, opened, 0);
t.send(1, { type: "chat", text: "/give bworld:coal 1" });
const coal = slot_of(t.take(1), "bworld:coal");
move_to_screen(t, coal, 2);
move_to_menu(t, opened, coal, 2);
t.game.tick();
assertEquals(last_container(t.take(1), "screen")[3], null);
assertEquals(menu_items(t.take(1))[3], null);
});