Crabpot !!

This commit is contained in:
2026-10-11 22:38:20 -03:00
parent aa8257ad86
commit 3c80938e91
4 changed files with 173 additions and 2 deletions
@@ -0,0 +1,14 @@
{
"format_version": 1,
"loot_table": {
"pools": [
{
"entries": [
{ "item": "bworld:crayfish" },
{ "item": "bworld:chorus_snail" },
{ "item": "bworld:leech" }
]
}
]
}
}
@@ -0,0 +1,20 @@
{
"format_version": 1,
"loot_table": {
"pools": [
{
"entries": [
{ "item": "bworld:ocean_hermit_crab" },
{ "item": "bworld:cold_ocean_hermit_crab" },
{ "item": "bworld:frozen_ocean_hermit_crab" },
{ "item": "bworld:lukewarm_ocean_hermit_crab" },
{ "item": "bworld:warm_ocean_hermit_crab" },
{ "item": "bworld:clam" },
{ "item": "bworld:lobster" },
{ "item": "bworld:shrimp" },
{ "item": "bworld:crab" }
]
}
]
}
}
+39 -1
View File
@@ -263,9 +263,12 @@ const ADDITION = 2;
const RESULT = 3;
// the crab pot's menu: its bait slot at the top, and a row of 9 slots under it for what it catches, which players can
// only take from. it doesn't catch anything yet
// only take from. every morning a crab pot in water with worms in it uses one up to catch something: from the
// saltwater table in an ocean, the freshwater one anywhere else. a catch that doesn't fit is let go, and keeps the
// worm
const CRABPOT_BAIT = 0;
const CRABPOT_OUTPUT = 9;
const CRABPOT_WORMS = "bworld:worms";
function register_crabpot(ctx: ServerContext) {
const menu_type = ctx.menus.register<BlockRef>("bworld:crabpot", (menu, block) => {
@@ -287,6 +290,41 @@ function register_crabpot(ctx: ServerContext) {
on_break(block) {
drop_contents(ctx, block);
},
on_new_day(block) {
const { x, y, z } = block;
if (ctx.world.get_state(x, y, z, "waterlogged") !== 1) {
return;
}
// one that's never been opened has nothing in it
const container = block.data?.container && ctx.containers.get(block.data.container);
const bait = container && container.get(CRABPOT_BAIT);
if (!bait || bait.id !== CRABPOT_WORMS) {
return;
}
const table = ctx.tags.has("biomes", "bworld:is_ocean", block.biome)
? "bworld:gameplay/crabpot_saltwater"
: "bworld:gameplay/crabpot_freshwater";
const position = { x: x + 0.5, y: y + 0.5, z: z + 0.5 };
for (const item of ctx.loot.roll(table, { position })) {
// onto a stack of it with room, or into an empty output slot
let slot = -1;
for (let i = 1; i <= CRABPOT_OUTPUT && slot < 0; i++) {
const there = container.get(i);
if (there?.id === item.id && there.count + item.count <= ctx.items.max_stack(item.id)) {
slot = i;
}
}
for (let i = 1; i <= CRABPOT_OUTPUT && slot < 0; i++) {
if (!container.get(i)) slot = i;
}
if (slot < 0) return;
const there = container.get(slot);
if (there) there.count += item.count;
else container.set(slot, item);
}
bait.count -= 1;
},
});
}
+100 -1
View File
@@ -3,7 +3,10 @@ import { assert, assertEquals } from "@std/assert";
import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
import { bake_block, type ModelJson } from "$/common/block_models.ts";
import { shape_at } from "$/common/block_shapes.ts";
import { get_state_value } from "$/common/utils.ts";
import { get_state_value, set_state_value } from "$/common/utils.ts";
import { ItemStack } from "$/common/inventory.ts";
import { CYCLE_TICKS } from "$/common/time.ts";
import { tag_api } from "$/common/tags.ts";
import { opened_menu, test_game } from "./helpers.ts";
Deno.test("a crab pot is a box 10 pixels tall and deep, drawn with its textures, turned by its facing", async () => {
@@ -73,3 +76,99 @@ Deno.test("right clicking a crab pot opens its menu: a bait slot on top and 9 ou
);
assert(opened.slots.slice(1, 10).every((s: { output?: boolean }) => s.output));
});
Deno.test("crab pots have a freshwater and a saltwater loot table, one catch a roll", async () => {
const { game } = await test_game("mods");
const catches = (table: string) => {
const items = new Set<string>();
for (let i = 0; i < 300; i++) {
const loot = game.mods.loot.roll(table, {});
assertEquals(loot.length, 1);
items.add(loot[0].id);
}
return [...items].sort();
};
assertEquals(catches("bworld:gameplay/crabpot_freshwater"), [
"bworld:chorus_snail",
"bworld:crayfish",
"bworld:leech",
]);
assertEquals(catches("bworld:gameplay/crabpot_saltwater"), [
"bworld:clam",
"bworld:cold_ocean_hermit_crab",
"bworld:crab",
"bworld:frozen_ocean_hermit_crab",
"bworld:lobster",
"bworld:lukewarm_ocean_hermit_crab",
"bworld:ocean_hermit_crab",
"bworld:shrimp",
"bworld:warm_ocean_hermit_crab",
]);
});
Deno.test("every morning a crab pot in water uses a worm to catch something, from its water's table", async () => {
const t = await test_game("mods");
t.join(1, "alice");
const tags = tag_api();
const saltwater = ["bworld:ocean_hermit_crab", "bworld:cold_ocean_hermit_crab", "bworld:frozen_ocean_hermit_crab"]
.concat(["bworld:lukewarm_ocean_hermit_crab", "bworld:warm_ocean_hermit_crab", "bworld:clam"])
.concat(["bworld:lobster", "bworld:shrimp", "bworld:crab"]);
const freshwater = ["bworld:crayfish", "bworld:chorus_snail", "bworld:leech"];
// a column whose biome is an ocean, or isn't
const column = (ocean: boolean) => {
for (let x = -2048; x <= 2048; x += 32) {
for (let z = -2048; z <= 2048; z += 32) {
if (tags.has("biomes", "bworld:is_ocean", t.game.world.biome_at(x, z)) === ocean) return [x, z];
}
}
throw new Error("no such column near spawn");
};
// a crab pot high in the air there, opened once so it has its slots, maybe in water, with bait
const pot = ([x, z]: number[], waterlogged: boolean, bait?: ItemStack) => {
const y = 150;
t.game.set_block(x, y, z, "bworld:crabpot");
const info = t.game.world.get_block_info(x, y, z)!;
t.game.set_block_state(
x,
y,
z,
set_state_value(t.game.world.get_block_value(x, y, z), info, "waterlogged", waterlogged ? 1 : 0)! >>> 16,
);
t.send(1, { type: "move", x: x + 0.5, y: y - 2, z: z + 2.5, yaw: 0, pitch: 0 });
t.send(1, { type: "use_block", x, y, z, face: "south", hit: [0.5, 0.5, 1] });
const data = t.game.world.get_tile(x, y, z)!.mod_data as { container: string };
const container = t.game.containers.get(data.container)!;
container.set_item(0, bait);
return container;
};
const next_morning = () => {
t.game.set_time((t.game.world.day + 1) * CYCLE_TICKS - 1);
t.game.tick();
};
const caught = (container: { get_item(slot: number): ItemStack | undefined }) =>
[1, 2, 3, 4, 5, 6, 7, 8, 9].flatMap((slot) => container.get_item(slot)?.type_id ?? []);
const ocean = pot(column(true), true, new ItemStack("bworld:worms", 2));
const river = pot(column(false), true, new ItemStack("bworld:worms", 1));
const [dry_x, dry_z] = column(false);
const dry = pot([dry_x + 3, dry_z], false, new ItemStack("bworld:worms", 1));
const unbaited = pot([dry_x + 6, dry_z], true, new ItemStack("bworld:dirt", 1));
next_morning();
assertEquals(caught(ocean).length, 1);
assert(saltwater.includes(caught(ocean)[0]), caught(ocean)[0]);
assertEquals(ocean.get_item(0)?.amount, 1);
assertEquals(caught(river).length, 1);
assert(freshwater.includes(caught(river)[0]), caught(river)[0]);
// its last worm is gone
assertEquals(river.get_item(0), undefined);
// not in water, or without worms, nothing
assertEquals([caught(dry), dry.get_item(0)?.amount], [[], 1]);
assertEquals([caught(unbaited), unbaited.get_item(0)?.type_id], [[], "bworld:dirt"]);
// with no room for the catch it's let go, and the worm kept
for (let slot = 1; slot <= 9; slot++) ocean.set_item(slot, new ItemStack("bworld:stone", 64));
next_morning();
assertEquals(ocean.get_item(0)?.amount, 1);
assertEquals(caught(ocean).filter((id) => id !== "bworld:stone"), []);
});