Hold and valley minigames

This commit is contained in:
2026-10-11 14:58:01 -03:00
parent 21442fb822
commit c71584eeea
10 changed files with 617 additions and 30 deletions
+223 -11
View File
@@ -1,12 +1,12 @@
// fishing: throw the hook into water, wait for a bite, reel it in before it gets away
import { assert, assertEquals } from "@std/assert";
import { assert, assertAlmostEquals, assertEquals } from "@std/assert";
import { EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import { ItemStack } from "$/common/inventory.ts";
import { item_texture } from "$/common/utils.ts";
import { tag_api } from "$/common/tags.ts";
import { CYCLE_TICKS } from "$/common/time.ts";
import { biome_season } from "$/common/worldgen/biomes.ts";
import { test_game } from "./helpers.ts";
import { menu_data, opened_menu, test_game } from "./helpers.ts";
// deno-lint-ignore no-explicit-any
type Msg = any;
@@ -77,15 +77,17 @@ Deno.test("the hook flies forward from the player and floats where it lands in w
Deno.test("cast into water, reel in a fish once something bites", async () => {
const t = await setup();
t.cast();
const floating = t.hooks()[0].y;
assert(t.wait_for_bite());
assert(t.hooks()[0].y < floating - 0.1, String(t.hooks()[0].y));
t.reel();
assertEquals(t.fish().length, 1);
assertEquals(t.rod()?.data, undefined);
t.game.tick();
assertEquals(t.hooks().length, 0);
await with_minigames(async () => {
t.cast();
const floating = t.hooks()[0].y;
assert(t.wait_for_bite());
assert(t.hooks()[0].y < floating - 0.1, String(t.hooks()[0].y));
t.reel();
assertEquals(t.fish().length, 1);
assertEquals(t.rod()?.data, undefined);
t.game.tick();
assertEquals(t.hooks().length, 0);
}, null);
});
Deno.test("right clicking a block throws the hook too", async () => {
@@ -177,3 +179,213 @@ Deno.test("a fish with biomes is only caught in them, by id or #tag", async () =
.has("bworld:salmon"),
);
});
// every fish plays the minigame until the test is done, or none with null
async function with_minigames(test: () => Promise<void>, minigame: string | null = "test") {
const fishes = EverythingRegistry.entries<ItemRegistry>("items")
.map(([, item]) => item.components?.["bworld:fish"] as { minigame?: string } | undefined)
.filter((fish) => fish !== undefined);
const before = fishes.map((fish) => fish.minigame);
const set = (fish: { minigame?: string }, value: string | undefined) => {
if (value === undefined) delete fish.minigame;
else fish.minigame = value;
};
fishes.forEach((fish) => set(fish, minigame ?? undefined));
try {
await test();
} finally {
fishes.forEach((fish, i) => set(fish, before[i]));
}
}
Deno.test("a fish with a minigame is caught once it's won, in its menu", async () => {
const t = await setup();
await with_minigames(async () => {
t.cast();
assert(t.wait_for_bite());
t.take(1);
t.reel();
const opened = opened_menu(t.take(1));
assertEquals(opened?.menu_type, "bworld:fishing_minigame");
assertEquals(opened.open_data.minigame, "test");
assertEquals(t.fish().length, 0);
t.send(1, { type: "menu_button", menu: opened.id, button: 1 });
assertEquals(t.fish().length, 0);
t.send(1, { type: "menu_button", menu: opened.id, button: 0 });
assertEquals(t.fish().length, 1);
assertEquals(t.fish()[0]?.id, opened.open_data.fish.id);
t.game.tick();
assert(t.take(1).some((m: Msg) => m.type === "close_menu"));
// once
t.send(1, { type: "menu_button", menu: opened.id, button: 0 });
assertEquals(t.fish().length, 1);
});
});
Deno.test("closing a fish's minigame lets it get away", async () => {
const t = await setup();
await with_minigames(async () => {
t.cast();
assert(t.wait_for_bite());
t.take(1);
t.reel();
const opened = opened_menu(t.take(1));
t.send(1, { type: "close_menu", menu: opened.id });
t.send(1, { type: "menu_button", menu: opened.id, button: 0 });
assertEquals(t.fish().length, 0);
});
});
Deno.test("/fish reels in a fish without casting, its minigame and all", async () => {
const t = await setup();
await with_minigames(async () => {
t.send(1, { type: "chat", text: "/fish bworld:cod" });
assertEquals(t.fish().map((i: Msg) => i.id), ["bworld:cod"]);
}, null);
t.send(1, { type: "chat", text: "/fish stone" });
assertEquals(t.fish().length, 1);
assert(t.take(1).some((m: Msg) => JSON.stringify(m).includes("bworld:stone isn't a fish")));
await with_minigames(async () => {
t.send(1, { type: "chat", text: "/fish salmon" });
const opened = opened_menu(t.take(1));
assertEquals(opened?.menu_type, "bworld:fishing_minigame");
assertEquals(opened.open_data.fish, { id: "bworld:salmon", count: 1 });
t.send(1, { type: "menu_button", menu: opened.id, button: 0 });
assertEquals(t.fish().map((i: Msg) => i.id).sort(), ["bworld:cod", "bworld:salmon"]);
});
});
// cod's bworld:fish difficulty until the test is done, or none with undefined
async function with_difficulty(difficulty: number | undefined, test: () => Promise<void>) {
const fish = EverythingRegistry.get<ItemRegistry>("items", "bworld:cod")!.components!["bworld:fish"] as {
difficulty?: number;
};
const before = fish.difficulty;
if (difficulty === undefined) delete fish.difficulty;
else fish.difficulty = difficulty;
try {
await test();
} finally {
if (before === undefined) delete fish.difficulty;
else fish.difficulty = before;
}
}
Deno.test("the hold minigame fills while the mouse is held and empties by the fish's difficulty, landing it when full", async () => {
const t = await setup();
const progress = () => menu_data(t.take(1))?.progress;
await with_minigames(() =>
with_difficulty(100, async () => {
t.send(1, { type: "chat", text: "/fish cod" });
const opened = opened_menu(t.take(1));
assertEquals(opened.data, { progress: 100, max: 200 });
assertEquals(opened.open_data.difficulty, 100);
t.send(1, { type: "menu_button", menu: opened.id, button: 1 });
for (let i = 0; i < 50; i++) t.game.tick();
assertEquals(progress(), 150);
// let go for a bit: at 100 it empties twice as fast as it fills
t.send(1, { type: "menu_button", menu: opened.id, button: 0 });
for (let i = 0; i < 10; i++) t.game.tick();
assertEquals(progress(), 130);
t.send(1, { type: "menu_button", menu: opened.id, button: 1 });
for (let i = 0; i < 69; i++) t.game.tick();
assertEquals(t.fish().length, 0);
t.game.tick();
assertEquals(t.fish().map((i: Msg) => i.id), ["bworld:cod"]);
t.game.tick();
assert(t.take(1).some((m: Msg) => m.type === "close_menu"));
}), "hold");
});
Deno.test("the hold minigame empties 1.1 seconds a second at the default difficulty, losing the fish when empty", async () => {
const t = await setup();
await with_minigames(() =>
with_difficulty(undefined, async () => {
t.send(1, { type: "chat", text: "/fish cod" });
const opened = opened_menu(t.take(1));
assertEquals(opened.open_data.difficulty, 10);
for (let i = 0; i < 10; i++) t.game.tick();
assertAlmostEquals(menu_data(t.take(1))?.progress, 89);
// 100 / 1.1 is 90.9 ticks
for (let i = 0; i < 80; i++) t.game.tick();
assert(!t.take(1).some((m: Msg) => m.type === "close_menu"));
for (let i = 0; i < 2; i++) t.game.tick();
assert(t.take(1).some((m: Msg) => m.type === "close_menu"));
t.send(1, { type: "menu_button", menu: opened.id, button: 1 });
for (let i = 0; i < 200; i++) t.game.tick();
assertEquals(t.fish().length, 0);
}), "hold");
});
// with Math.random at 0.99, the valley minigame's fish never moves, staying at the bottom
async function with_still_fish(test: () => Promise<void>) {
const random = Math.random;
Math.random = () => 0.99;
try {
await test();
} finally {
Math.random = random;
}
}
Deno.test("the valley minigame loses the fish while the bar is off it", async () => {
const t = await setup();
await with_minigames(() =>
with_still_fish(async () => {
t.send(1, { type: "chat", text: "/fish cod" });
const opened = opened_menu(t.take(1));
assertEquals(opened.data, { bar: 0, bar_size: 36, fish: 0, points: 24, max_points: 120 });
// the bar goes up to the top, away from the fish
t.send(1, { type: "menu_button", menu: opened.id, button: 1 });
let closed = false;
for (let i = 0; i < 200 && !closed; i++) {
t.game.tick();
closed = t.take(1).some((m: Msg) => m.type === "close_menu");
}
assert(closed);
assertEquals(t.fish().length, 0);
}), "valley");
});
Deno.test("the valley minigame lands the fish when the bar stays on it", async () => {
const t = await setup();
await with_minigames(() =>
with_still_fish(async () => {
t.send(1, { type: "chat", text: "/fish cod" });
// the bar stays at the bottom with the fish. 96 points to go, one a tick
for (let i = 0; i < 95; i++) t.game.tick();
assertEquals(t.fish().length, 0);
t.game.tick();
assertEquals(t.fish().map((i: Msg) => i.id), ["bworld:cod"]);
}), "valley");
});
Deno.test("harder valley fish move around more", async () => {
const t = await setup();
// how far the fish moves a tick on average at a difficulty, over a few minigames of up to 10 seconds
const speed = async (difficulty: number) => {
let moved = 0;
let ticks = 0;
await with_difficulty(difficulty, async () => {
for (let game = 0; game < 5; game++) {
t.send(1, { type: "chat", text: "/fish cod" });
let fish = opened_menu(t.take(1)).data.fish;
for (let i = 0; i < 200; i++) {
t.game.tick();
const messages = t.take(1);
const now = menu_data(messages)?.fish ?? fish;
moved += Math.abs(now - fish);
fish = now;
ticks++;
if (messages.some((m: Msg) => m.type === "close_menu")) break;
}
}
});
return moved / ticks;
};
await with_minigames(async () => {
const easy = await speed(5);
const hard = await speed(100);
assert(hard > easy * 2, `${hard} isn't much more than ${easy}`);
}, "valley");
});