diff --git a/docs/modding/resources/loot_tables.md b/docs/modding/resources/loot_tables.md index b0b2a40..fa71ebc 100644 --- a/docs/modding/resources/loot_tables.md +++ b/docs/modding/resources/loot_tables.md @@ -147,14 +147,14 @@ The fishing table, `bworld:gameplay/fishing`, isn't a file: the base game builds } ``` -| Field | What | -| ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `weight` | How often it's caught against the other fish, 1 when not given. | -| `seasons` | The [seasons](../worldgen/biomes.md#seasons) it's caught in where the hook is, every one when not given. | -| `biomes` | The [biomes](../worldgen/biomes.md#biome-ids) and [biome tags](../worldgen/biomes.md#biome-tags) it's caught in where the hook is, every one when not given. | -| `time` | The hours it's caught at on a 24 hour clock, from `from` up to `to`, like `6.5` for 06:30. When `from` is past `to` it wraps around midnight. All day when not given. | -| `minigame` | What the player does to land it once it's reeled in, in a screen that holds the fish until they win. Closing it lets the fish get away. Caught right away when not given. The base game's are `"test"`, a Catch button, `"hold"`, a bar that fills while the mouse is held and empties while it isn't: full lands the fish, empty loses it, `"valley"`, Stardew Valley's, where holding the mouse lifts a bar to keep the fish in it, and `"ring"`, where a green ring grows out toward a gray one and clicking while it's inside lands the fish. | -| `difficulty` | How hard its minigame is, 10 when not given. In `"hold"`, the bar empties `1 + difficulty / 100` times as fast as it fills: 1.1 at 10, 2 at 100. In `"ring"`, the green ring grows `1 + difficulty / 100` times as fast, and there's a gray ring to click for every 30 difficulty: 1 up to 30, 2 up to 60, 3 up to 90, 4 up to 120. In `"valley"`, it's Stardew Valley's fish difficulty: the higher, the more the fish darts around. | +| Field | What | +| ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `weight` | How often it's caught against the other fish, 1 when not given. | +| `seasons` | The [seasons](../worldgen/biomes.md#seasons) it's caught in where the hook is, every one when not given. | +| `biomes` | The [biomes](../worldgen/biomes.md#biome-ids) and [biome tags](../worldgen/biomes.md#biome-tags) it's caught in where the hook is, every one when not given. | +| `time` | The hours it's caught at on a 24 hour clock, from `from` up to `to`, like `6.5` for 06:30. When `from` is past `to` it wraps around midnight. All day when not given. | +| `minigame` | What the player does to land it once it's reeled in, in a screen that holds the fish until they win. Closing it lets the fish get away. Caught right away when not given. The base game's are `"test"`, a Catch button, `"hold"`, a bar that fills while the mouse is held and empties while it isn't: full lands the fish, empty loses it, `"valley"`, Stardew Valley's, where holding the mouse lifts a bar to keep the fish in it, `"ring"`, where a green ring grows out toward gray ones and clicking while it's inside each lands the fish, and `"maze"`, where the mouse goes from the start to the end of a maze without touching its walls, in 8 seconds. | +| `difficulty` | How hard its minigame is, 10 when not given. In `"hold"`, the bar empties `1 + difficulty / 100` times as fast as it fills: 1.1 at 10, 2 at 100. In `"ring"`, the green ring grows `1 + difficulty / 100` times as fast, and there's a gray ring to click for every 30 difficulty: 1 up to 30, 2 up to 60, 3 up to 90, 4 up to 120. In `"maze"`, the maze is 4 cells a side and 1 more for every 25 difficulty. In `"valley"`, it's Stardew Valley's fish difficulty: the higher, the more the fish darts around. | When every fish can be ruled out, nothing is caught, so keep one that's always caught. diff --git a/mods/bworld/items/cod.json b/mods/bworld/items/cod.json index 60159f4..07ab895 100644 --- a/mods/bworld/items/cod.json +++ b/mods/bworld/items/cod.json @@ -12,7 +12,7 @@ "weight": 25, "seasons": ["spring", "summer", "autumn", "winter"], "biomes": ["#bworld:is_ocean"], - "minigame": "ring", + "minigame": "maze", "difficulty": 100 } }, diff --git a/mods/bworld/scripts/client.ts b/mods/bworld/scripts/client.ts index 0b54f49..e9f221d 100644 --- a/mods/bworld/scripts/client.ts +++ b/mods/bworld/scripts/client.ts @@ -169,6 +169,58 @@ const MINIGAME_SCREENS: Record Me }, }; }, + // a maze over the world. the mouse starts it by going onto the yellow start, wherever it was when it opened, then + // touching a wall or leaving the maze sends button 0, and reaching the green end sends button 1. it's a grid of + // 4 cells a side, and 1 more for every 20 difficulty. the time left shrinks along a bar under it + maze: (menu) => { + const { difficulty } = menu.open_data as { difficulty: number }; + const cells = 4 + Math.floor(difficulty / 20); + const size = cells * MAZE_CELL + MAZE_WALL; + const walls = maze_walls(cells); + const cell = ( + x: number, + y: number, + ) => [x * MAZE_CELL + MAZE_WALL, y * MAZE_CELL + MAZE_WALL, MAZE_CELL - MAZE_WALL]; + const [start_x, start_y, pad] = cell(0, 0); + const [end_x, end_y] = cell(cells - 1, cells - 1); + const inside = (x: number, y: number, rx: number, ry: number, w: number, h: number) => + x >= rx && x < rx + w && y >= ry && y < ry + h; + let started = false; + let over = false; + let last = { x: 0, y: 0 }; + return { + width: size, + height: size + 2 * MAZE_WALL, + dim_background: false, + inventory_overlays: false, + on_render_labels(g) { + g.rect(0, 0, size, size, [0.05, 0.1, 0.2, 0.6]); + const time_left = menu.data.ticks_left / menu.data.time || 0; + g.rect(0, size + MAZE_WALL, size * time_left, MAZE_WALL, [1 - time_left, time_left, 0.2, 1]); + g.rect(start_x, start_y, pad, pad, started ? [0.5, 0.5, 0.2, 0.6] : [0.9, 0.8, 0.2, 1]); + g.rect(end_x, end_y, pad, pad, [0.3, 0.85, 0.3, 1]); + for (const [x, y, w, h] of walls) g.rect(x, y, w, h, [0.8, 0.8, 0.8, 1]); + + const mouse = { x: g.mouse.x, y: g.mouse.y }; + if (!over && !started && inside(mouse.x, mouse.y, start_x, start_y, pad, pad)) started = true; + else if (!over && started) { + // every pixel along the way since last frame, so moving fast doesn't skip a wall + const steps = Math.max(1, Math.ceil(Math.hypot(mouse.x - last.x, mouse.y - last.y))); + for (let i = 1; i <= steps && !over; i++) { + const x = last.x + (mouse.x - last.x) * i / steps; + const y = last.y + (mouse.y - last.y) * i / steps; + const hit = !inside(x, y, 0, 0, size, size) || + walls.some(([wx, wy, ww, wh]) => inside(x, y, wx, wy, ww, wh)); + if (hit || inside(x, y, end_x, end_y, pad, pad)) { + over = true; + menu.click_button(hit ? 0 : 1); + } + } + } + last = mouse; + }, + }; + }, }; // sends the server button 1 when the mouse is pressed and 0 when it's let go, for minigames you hold the mouse in. @@ -192,3 +244,52 @@ function ring(g: Graphics, x: number, y: number, radius: number, thickness: numb g.rect(px, py, thickness, thickness, color); } } + +// the maze minigame's cells and walls, in gui pixels +const MAZE_CELL = 20; +const MAZE_WALL = 4; + +// a maze of cells by cells, as its walls' [x, y, width, height]: a random depth first one, like a recursive +// backtracker, so there's exactly one way between any two cells +function maze_walls(cells: number): [number, number, number, number][] { + // whether each cell has a wall on its right and below it + const right = new Array(cells * cells).fill(true); + const below = new Array(cells * cells).fill(true); + const visited = new Array(cells * cells).fill(false); + const stack = [0]; + visited[0] = true; + while (stack.length > 0) { + const at = stack.at(-1)!; + const [x, y] = [at % cells, Math.floor(at / cells)]; + const next = [[x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1]] + .filter(([nx, ny]) => nx >= 0 && ny >= 0 && nx < cells && ny < cells && !visited[ny * cells + nx]) + .map(([nx, ny]) => ny * cells + nx); + if (next.length === 0) { + stack.pop(); + continue; + } + const to = next[Math.floor(Math.random() * next.length)]; + if (to === at + 1) right[at] = false; + else if (to === at - 1) right[to] = false; + else if (to > at) below[at] = false; + else below[to] = false; + visited[to] = true; + stack.push(to); + } + + const size = cells * MAZE_CELL + MAZE_WALL; + const walls: [number, number, number, number][] = [ + [0, 0, size, MAZE_WALL], + [0, size - MAZE_WALL, size, MAZE_WALL], + [0, 0, MAZE_WALL, size], + [size - MAZE_WALL, 0, MAZE_WALL, size], + ]; + for (let i = 0; i < cells * cells; i++) { + const [x, y] = [(i % cells) * MAZE_CELL, Math.floor(i / cells) * MAZE_CELL]; + if (right[i] && i % cells < cells - 1) walls.push([x + MAZE_CELL, y, MAZE_WALL, MAZE_CELL + MAZE_WALL]); + if (below[i] && Math.floor(i / cells) < cells - 1) { + walls.push([x, y + MAZE_CELL, MAZE_CELL + MAZE_WALL, MAZE_WALL]); + } + } + return walls; +} diff --git a/mods/bworld/scripts/fishing.ts b/mods/bworld/scripts/fishing.ts index 43b68c1..8b7fa4a 100644 --- a/mods/bworld/scripts/fishing.ts +++ b/mods/bworld/scripts/fishing.ts @@ -396,6 +396,27 @@ class RingMinigame extends FishingMinigame { } } +// a maze to take the mouse through without touching its walls, bigger the harder the fish is, in 8 seconds from when +// it opens whatever the difficulty. it's all played on the screen, which says how it went: button 1 when the mouse +// reaches the end, 0 when it touches a wall +const MAZE_TIME = 8 * 20; +class MazeMinigame extends FishingMinigame { + #ticks_left = MAZE_TIME; + + constructor(...args: MinigameArgs) { + super(...args); + this.menu.add_data("ticks_left", () => this.#ticks_left); + this.menu.add_data("time", () => MAZE_TIME); + this.menu.on_button((_player, id) => { + if (id === 1) this.win(); + else if (id === 0) this.lose(); + }); + this.menu.on_tick(() => { + if (--this.#ticks_left <= 0) this.lose(); + }); + } +} + // stardew valley's, ported from ports/stardewfishing's FishingMinigame: holding the mouse lifts a green bar up a track // and letting go drops it, while the fish darts up and down. points go up 1 a tick while the bar is on the fish and // down 1 while it isn't, starting at a fifth: all of them lands it, none loses it. the bar and points are in the @@ -506,4 +527,5 @@ const MINIGAMES: Record FishingMinigame> hold: HoldMinigame, valley: ValleyMinigame, ring: RingMinigame, + maze: MazeMinigame, }; diff --git a/tests/fishing_test.ts b/tests/fishing_test.ts index a9e5dad..2353de6 100644 --- a/tests/fishing_test.ts +++ b/tests/fishing_test.ts @@ -504,3 +504,40 @@ Deno.test("the ring minigame needs every ring checked, and missing one loses it" assertEquals(t.fish().length, 1); }), "ring"); }); + +Deno.test("the maze minigame lands the fish when its screen says the mouse got through, and loses it on a wall", async () => { + const t = await setup(); + const closes = () => t.take(1).some((m: Msg) => m.type === "close_menu"); + await with_minigames(async () => { + t.send(1, { type: "chat", text: "/fish cod" }); + let opened = opened_menu(t.take(1)); + t.send(1, { type: "menu_button", menu: opened.id, button: 0 }); + t.game.tick(); + assert(closes()); + assertEquals(t.fish().length, 0); + + t.send(1, { type: "chat", text: "/fish cod" }); + opened = opened_menu(t.take(1)); + t.send(1, { type: "menu_button", menu: opened.id, button: 1 }); + assertEquals(t.fish().map((i: Msg) => i.id), ["bworld:cod"]); + }, "maze"); +}); + +Deno.test("the maze minigame loses the fish after 8 seconds, whatever the difficulty", async () => { + const t = await setup(); + const closes = () => t.take(1).some((m: Msg) => m.type === "close_menu"); + await with_minigames(async () => { + for (const difficulty of [0, 100]) { + await with_difficulty(difficulty, async () => { + t.send(1, { type: "chat", text: "/fish cod" }); + assertEquals(opened_menu(t.take(1)).data, { ticks_left: 160, time: 160 }); + for (let i = 0; i < 159; i++) t.game.tick(); + assert(!closes()); + t.game.tick(); + t.game.tick(); + assert(closes()); + assertEquals(t.fish().length, 0); + }); + } + }, "maze"); +});