diff --git a/MODS.md b/MODS.md index 24c3e20..2720260 100644 --- a/MODS.md +++ b/MODS.md @@ -418,6 +418,7 @@ clicked on somewhere it can go and eaten otherwise. | `id` | required | registry key | The item's id. | | `name` | from the id | `name` | What players see, like `"Copper Pickaxe"`. Made from the id when not set. | | `texture` | required | `texture_id` | Texture id. | +| `data_textures` | none | `data_textures` | `{ "cast": "bworld:fishing_rod_cast" }`: a stack whose `data` has that key set is drawn with that texture instead, like a cast fishing rod. The first key that's set wins. | | `tool` | none | `tool_type` | Tool type used by blocks' `mining.tool`. | | `places` | none | `block_id` | Block placed when used on a block face. | | `max_stack` | `64` | new | Largest stack size. | diff --git a/client/rendering/item_renderer.ts b/client/rendering/item_renderer.ts index 26b951a..ee5d9f0 100644 --- a/client/rendering/item_renderer.ts +++ b/client/rendering/item_renderer.ts @@ -7,6 +7,7 @@ import type { ItemStack } from "$/common/inventory.ts"; import { push_vertex, Texture } from "$/client/renderer/mod.ts"; import { block_item_texture, cube_face_texture, cube_face_tint } from "$/common/block_models.ts"; import { item_model } from "./item_model.ts"; +import { item_texture } from "$/common/utils.ts"; // how big items on the ground are drawn, minecraft's ground transform const BLOCK_SCALE = 0.25; @@ -113,11 +114,3 @@ function push_textured_quad(atlas: Texture, region: { x: number; y: number }, co push_vertex(px, py, pz, cu ? u1 : u0, cv ? v1 : v0, color[0], color[1], color[2], 1); } } - -function item_texture(item: ItemStack) { - const texture_id = EverythingRegistry.get("items", item.type_id)?.texture_id; - if (typeof texture_id === "function") { - return texture_id(item); - } - return texture_id ?? "engine:missing"; -} diff --git a/client/rendering/render_utils.ts b/client/rendering/render_utils.ts index d8d4def..8de0a99 100644 --- a/client/rendering/render_utils.ts +++ b/client/rendering/render_utils.ts @@ -3,6 +3,7 @@ import { get_sprite_region } from "$/client/sprites.ts"; import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts"; import { AssetManager } from "$/client/assets.ts"; import { ItemStack } from "$/common/inventory.ts"; +import { item_texture } from "$/common/utils.ts"; import { block_item_texture, cube_face_texture, cube_face_tint } from "$/common/block_models.ts"; import { draw_text, draw_texture_region, draw_texture_region_skewed, Texture } from "$/client/renderer/mod.ts"; @@ -93,12 +94,7 @@ export function draw_item(item: ItemStack, x: number, y: number) { // ey its not a bad name function draw_item_item(item: ItemStack, item_info: ItemRegistry, x: number, y: number) { - let texture_id = "engine:missing"; - if (typeof item_info?.texture_id === "string") { - texture_id = item_info.texture_id; - } else if (typeof item_info?.texture_id === "function") { - texture_id = item_info.texture_id(item); - } + const texture_id = item_texture(item, item_info); const sprite_region = get_sprite_region(texture_id); draw_texture_region( AssetManager.instance.get("bworld:textures"), diff --git a/common/everything_registry.ts b/common/everything_registry.ts index fddb4c7..f3880e1 100644 --- a/common/everything_registry.ts +++ b/common/everything_registry.ts @@ -158,6 +158,8 @@ export interface FoodProperties { export interface ItemRegistry { texture_id: string | ((item: ItemStack) => string); + // shown in place of texture_id while the stack's data has that key set, like a cast fishing rod. the first wins + data_textures?: Record; // what players see, like "Iron Ingot". made from the id when not set, see display_name in common/utils.ts name?: string; block_id?: string; diff --git a/common/mod_check.ts b/common/mod_check.ts index 3da43ab..edbb98c 100644 --- a/common/mod_check.ts +++ b/common/mod_check.ts @@ -129,6 +129,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable texture, see ItemRegistry.data_textures + data_textures?: Record; tool?: string; places?: string; max_stack?: number; @@ -192,6 +194,7 @@ export function item_to_json(id: string, item: ItemRegistry): ItemJson { throw new Error(`${id} picks its texture with a function, which json can't hold`); } const json: ItemJson = { id, texture: item.texture_id }; + if (item.data_textures !== undefined) json.data_textures = item.data_textures; if (item.tool_type !== undefined) json.tool = item.tool_type; if (item.block_id !== undefined) json.places = item.block_id; if (item.max_stack !== undefined) json.max_stack = item.max_stack; @@ -204,6 +207,7 @@ export function item_to_json(id: string, item: ItemRegistry): ItemJson { export function item_from_json(json: ItemJson): ItemRegistry { const item: ItemRegistry = { texture_id: json.texture }; + if (json.data_textures !== undefined) item.data_textures = json.data_textures; if (json.tool !== undefined) item.tool_type = json.tool; if (json.places !== undefined) item.block_id = json.places; if (json.max_stack !== undefined) item.max_stack = json.max_stack; @@ -477,6 +481,14 @@ export function validate_item(json: unknown): Problems { if (!is_object(json)) return ["item must be an object"]; const problems = validate_id(json.id, "id"); problems.push(...validate_id(json.texture, "texture")); + if (json.data_textures !== undefined) { + if (!is_object(json.data_textures)) problems.push("data_textures must be { data key: texture id }"); + else { + for (const [key, id] of Object.entries(json.data_textures)) { + problems.push(...validate_id(id, `data_textures.${key}`)); + } + } + } if (json.places !== undefined) problems.push(...validate_id(json.places, "places")); problems.push(...validate_text(json.name, "name"), ...validate_text(json.lore, "lore")); if (json.max_stack !== undefined && (!Number.isInteger(json.max_stack) || (json.max_stack as number) < 1)) { diff --git a/common/utils.ts b/common/utils.ts index 8478e89..48ca210 100644 --- a/common/utils.ts +++ b/common/utils.ts @@ -1,6 +1,7 @@ import { ID_MASK, STATE_SHIFT } from "./constants.ts"; import { BlockRegistry, EverythingRegistry, ItemRegistry } from "./everything_registry.ts"; import { block_item_texture, cube_face_texture } from "./block_models.ts"; +import type { ItemStack } from "./inventory.ts"; export function point_inside_rec( point_x: number, @@ -46,6 +47,19 @@ export function register_block_item(block: BlockRegistry) { EverythingRegistry.register("items", block.id, item); } +// the texture a stack is drawn with: one of its data_textures when its data has that key set, or its texture +export function item_texture( + item: ItemStack, + info = EverythingRegistry.get("items", item.type_id), +): string { + if (!info) return "engine:missing"; + const data = item.data as Record | undefined; + for (const [key, texture] of Object.entries(info.data_textures ?? {})) { + if (data?.[key]) return texture; + } + return typeof info.texture_id === "function" ? info.texture_id(item) : info.texture_id; +} + // what players see an item called: its name, or one made from its id, so bworld:iron_ingot is "Iron Ingot" export function display_name(item_id: string): string { const name = EverythingRegistry.get("items", item_id)?.name; diff --git a/mods/bworld/blocks/water.json b/mods/bworld/blocks/water.json index 7ec7ca3..1b85cdb 100644 --- a/mods/bworld/blocks/water.json +++ b/mods/bworld/blocks/water.json @@ -10,7 +10,8 @@ "item": false, "replaceable": true, "components": { - "bworld:water_source": {} + "bworld:water_source": {}, + "bworld:fishable": {} } } } diff --git a/mods/bworld/items/cod.json b/mods/bworld/items/cod.json new file mode 100644 index 0000000..3fd9109 --- /dev/null +++ b/mods/bworld/items/cod.json @@ -0,0 +1,11 @@ +{ + "format_version": 1, + "item": { + "id": "bworld:cod", + "texture": "bworld:cod", + "food": { + "nutrition": 2, + "saturation_modifier": 0.1 + } + } +} diff --git a/mods/bworld/items/cooked_cod.json b/mods/bworld/items/cooked_cod.json new file mode 100644 index 0000000..f8d9018 --- /dev/null +++ b/mods/bworld/items/cooked_cod.json @@ -0,0 +1,11 @@ +{ + "format_version": 1, + "item": { + "id": "bworld:cooked_cod", + "texture": "bworld:cooked_cod", + "food": { + "nutrition": 5, + "saturation_modifier": 0.6 + } + } +} diff --git a/mods/bworld/items/cooked_salmon.json b/mods/bworld/items/cooked_salmon.json new file mode 100644 index 0000000..4ad911c --- /dev/null +++ b/mods/bworld/items/cooked_salmon.json @@ -0,0 +1,11 @@ +{ + "format_version": 1, + "item": { + "id": "bworld:cooked_salmon", + "texture": "bworld:cooked_salmon", + "food": { + "nutrition": 6, + "saturation_modifier": 0.8 + } + } +} diff --git a/mods/bworld/items/fishing_rod.json b/mods/bworld/items/fishing_rod.json new file mode 100644 index 0000000..cbc4d5f --- /dev/null +++ b/mods/bworld/items/fishing_rod.json @@ -0,0 +1,22 @@ +{ + "format_version": 1, + "item": { + "id": "bworld:fishing_rod", + "texture": "bworld:fishing_rod", + "data_textures": { + "cast": "bworld:fishing_rod_cast" + }, + "max_stack": 1, + "lore": "Right click water to cast, and again when something bites.", + "components": { + "bworld:fishing_rod": { + "wait": [100, 600], + "bite": [20, 40], + "loot": [ + { "item": "bworld:cod", "weight": 60 }, + { "item": "bworld:salmon", "weight": 25 } + ] + } + } + } +} diff --git a/mods/bworld/items/salmon.json b/mods/bworld/items/salmon.json new file mode 100644 index 0000000..9b72606 --- /dev/null +++ b/mods/bworld/items/salmon.json @@ -0,0 +1,11 @@ +{ + "format_version": 1, + "item": { + "id": "bworld:salmon", + "texture": "bworld:salmon", + "food": { + "nutrition": 2, + "saturation_modifier": 0.1 + } + } +} diff --git a/mods/bworld/recipes/crafting_fishing_rod.json b/mods/bworld/recipes/crafting_fishing_rod.json new file mode 100644 index 0000000..0a4ebc6 --- /dev/null +++ b/mods/bworld/recipes/crafting_fishing_rod.json @@ -0,0 +1,19 @@ +{ + "format_version": 1, + "recipe": { + "type": "shaped", + "pattern": [ + " S", + " SW", + "S W" + ], + "key": { + "S": "bworld:stick", + "W": "bworld:wheat" + }, + "result": { + "id": "bworld:fishing_rod", + "count": 1 + } + } +} diff --git a/mods/bworld/recipes/smelting_cod.json b/mods/bworld/recipes/smelting_cod.json new file mode 100644 index 0000000..621bf04 --- /dev/null +++ b/mods/bworld/recipes/smelting_cod.json @@ -0,0 +1,12 @@ +{ + "format_version": 1, + "recipe": { + "type": "furnace", + "input": "bworld:cod", + "output": { + "id": "bworld:cooked_cod", + "count": 1 + }, + "cook_time": 200 + } +} diff --git a/mods/bworld/recipes/smelting_salmon.json b/mods/bworld/recipes/smelting_salmon.json new file mode 100644 index 0000000..6822e0b --- /dev/null +++ b/mods/bworld/recipes/smelting_salmon.json @@ -0,0 +1,12 @@ +{ + "format_version": 1, + "recipe": { + "type": "furnace", + "input": "bworld:salmon", + "output": { + "id": "bworld:cooked_salmon", + "count": 1 + }, + "cook_time": 200 + } +} diff --git a/mods/bworld/scripts/server.ts b/mods/bworld/scripts/server.ts index 958d4b6..575df6c 100644 --- a/mods/bworld/scripts/server.ts +++ b/mods/bworld/scripts/server.ts @@ -7,6 +7,7 @@ export function setup(ctx: ServerContext) { register_furnace(ctx); register_smithing_table(ctx); register_farming(ctx); + register_fishing(ctx); } // right clicking with a type of tool turns the block into another, like any hoe on grass @@ -506,3 +507,125 @@ function register_farming(ctx: ServerContext) { }, }); } + +// fishing, like minecraft's without the bobber: right click water with a rod to cast, wait for a bite, and right click +// again before it gets away to reel it in. there's no hook entity yet, so a chat message says when something bites + +const FISHING_ROD = "bworld:fishing_rod"; +// further than this from where they cast, the line snaps +const LINE_LENGTH = 16; + +interface FishingRodParams { + // ticks until something bites, the shortest and longest, like minecraft's 5 to 30 seconds + wait: [number, number]; + // ticks it stays on the hook before it gets away + bite: [number, number]; + loot: { item: string; weight: number }[]; +} + +interface Cast { + x: number; + y: number; + z: number; + // the block they cast into, the line comes out when it's gone + block: string; + // the hotbar slot the rod is in, switching away reels it in + slot: number; + params: FishingRodParams; + // ticks, when something bites and when it gets away + bite_at: number; + escape_at?: number; +} + +function register_fishing(ctx: ServerContext) { + // by player id + const casts = new Map(); + // the water each player right clicked this tick, for their rod's on_use, which doesn't get the block + const aimed = new Map(); + + const between = ([min, max]: [number, number]) => min + Math.floor(Math.random() * (max - min + 1)); + + const pick = (loot: FishingRodParams["loot"]) => { + const total = loot.reduce((sum, entry) => sum + entry.weight, 0); + let roll = Math.random() * total; + for (const entry of loot) { + roll -= entry.weight; + if (roll < 0) return entry.item; + } + return loot.at(-1)?.item; + }; + + // takes the line out of the water, the rod in the slot looks uncast again + const stop = (player: Player | undefined, id: string) => { + const cast = casts.get(id); + casts.delete(id); + const rod = cast && player?.inventory.get(cast.slot); + if (rod?.id === FISHING_ROD) rod.data = undefined; + }; + + ctx.components.register_block("bworld:fishable", { + on_interact(block, _params, player) { + aimed.set(player.id, { + x: block.x, + y: block.y, + z: block.z, + block: block.id, + tick: ctx.system.current_tick, + }); + return false; + }, + }); + + ctx.components.register_item(FISHING_ROD, { + on_use(rod, params, player) { + const cast = casts.get(player.id); + if (cast) { + if (cast.escape_at !== undefined) { + const item = pick(params.loot); + if (item) player.give_item(item); + } + stop(player, player.id); + return; + } + const water = aimed.get(player.id); + aimed.delete(player.id); + if (water?.tick !== ctx.system.current_tick) { + // cast before the server restarted + rod.data = undefined; + return; + } + casts.set(player.id, { + x: water.x, + y: water.y, + z: water.z, + block: water.block, + slot: player.selected_slot, + params, + bite_at: ctx.system.current_tick + between(params.wait), + }); + rod.data = { cast: true }; + }, + }); + + ctx.events.after.tick.subscribe(() => { + const now = ctx.system.current_tick; + for (const [id, cast] of casts) { + const player = ctx.players.get(id); + const { x, y, z } = player?.position ?? cast; + const distance = Math.hypot(x - cast.x, y - cast.y, z - cast.z); + if ( + !player || player.selected_slot !== cast.slot || player.held_item?.id !== FISHING_ROD || + distance > LINE_LENGTH || ctx.world.get_block(cast.x, cast.y, cast.z) !== cast.block + ) { + stop(player, id); + } else if (cast.escape_at === undefined && now >= cast.bite_at) { + cast.escape_at = now + between(cast.params.bite); + player.send_message("Something's biting! Right click to reel it in"); + } else if (cast.escape_at !== undefined && now >= cast.escape_at) { + // it got away, wait for another + cast.escape_at = undefined; + cast.bite_at = now + between(cast.params.wait); + } + } + }); +} diff --git a/mods/bworld/textures/cod.png b/mods/bworld/textures/cod.png new file mode 100644 index 0000000..be17046 Binary files /dev/null and b/mods/bworld/textures/cod.png differ diff --git a/mods/bworld/textures/cooked_cod.png b/mods/bworld/textures/cooked_cod.png new file mode 100644 index 0000000..9307a89 Binary files /dev/null and b/mods/bworld/textures/cooked_cod.png differ diff --git a/mods/bworld/textures/cooked_salmon.png b/mods/bworld/textures/cooked_salmon.png new file mode 100644 index 0000000..9c1ce5a Binary files /dev/null and b/mods/bworld/textures/cooked_salmon.png differ diff --git a/mods/bworld/textures/fishing_hook.png b/mods/bworld/textures/fishing_hook.png new file mode 100644 index 0000000..a7473f9 Binary files /dev/null and b/mods/bworld/textures/fishing_hook.png differ diff --git a/mods/bworld/textures/fishing_rod.png b/mods/bworld/textures/fishing_rod.png new file mode 100644 index 0000000..13e72f8 Binary files /dev/null and b/mods/bworld/textures/fishing_rod.png differ diff --git a/mods/bworld/textures/fishing_rod_cast.png b/mods/bworld/textures/fishing_rod_cast.png new file mode 100644 index 0000000..abe806c Binary files /dev/null and b/mods/bworld/textures/fishing_rod_cast.png differ diff --git a/mods/bworld/textures/grass.png b/mods/bworld/textures/grass.png deleted file mode 100644 index 65954b6..0000000 Binary files a/mods/bworld/textures/grass.png and /dev/null differ diff --git a/mods/bworld/textures/salmon.png b/mods/bworld/textures/salmon.png new file mode 100644 index 0000000..8c145f3 Binary files /dev/null and b/mods/bworld/textures/salmon.png differ diff --git a/tests/fishing_test.ts b/tests/fishing_test.ts new file mode 100644 index 0000000..e92dc38 --- /dev/null +++ b/tests/fishing_test.ts @@ -0,0 +1,94 @@ +// fishing: cast into water, wait for a bite, reel it in before it gets away +import { assert, 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 { test_game } from "./helpers.ts"; + +// deno-lint-ignore no-explicit-any +type Msg = any; + +const ROD = "bworld:fishing_rod"; + +// a player on a stone floor at y 99 with water next to them and a rod in hand +async function setup() { + const t = await test_game("mods"); + t.join(1, "alice"); + for (let x = -3; x <= 3; x++) { + for (let z = -3; z <= 3; z++) t.game.set_block(x, 99, z, "bworld:stone"); + } + t.game.set_block(2, 100, 0, "bworld:water"); + t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 }); + t.send(1, { type: "chat", text: `/give ${ROD}` }); + const inventory = () => t.game.players()[0].inventory.to_data(); + const slot = inventory().findIndex((i: Msg) => i?.id === ROD); + t.send(1, { type: "select_slot", slot }); + const rod = () => inventory()[slot]; + const cast = () => t.send(1, { type: "use_block", x: 2, y: 100, z: 0, face: "top" }); + const reel = () => t.send(1, { type: "use_item" }); + const fish = () => inventory().filter((i: Msg) => i?.id === "bworld:cod" || i?.id === "bworld:salmon"); + // ticks until something bites, false if nothing did in 35 seconds + const wait_for_bite = () => { + t.take(1); + for (let i = 0; i < 700; i++) { + t.game.tick(); + if (t.take(1).some((m: Msg) => m.type === "chat" && m.text.includes("biting"))) return true; + } + return false; + }; + return { ...t, slot, rod, cast, reel, fish, wait_for_bite }; +} + +Deno.test("cast into water, reel in a fish once something bites", async () => { + const t = await setup(); + t.cast(); + assertEquals(t.rod()?.data, { cast: true }); + + assert(t.wait_for_bite()); + t.reel(); + assertEquals(t.fish().length, 1); + assertEquals(t.rod()?.data, undefined); +}); + +Deno.test("reeling in before a bite catches nothing", async () => { + const t = await setup(); + t.cast(); + t.reel(); + assertEquals(t.fish().length, 0); + assertEquals(t.rod()?.data, undefined); +}); + +Deno.test("right clicking air with an uncast rod does nothing", async () => { + const t = await setup(); + t.reel(); + assertEquals(t.rod()?.data, undefined); + for (let i = 0; i < 700; i++) t.game.tick(); + assertEquals(t.fish().length, 0); +}); + +Deno.test("switching away from the rod takes the line out", async () => { + const t = await setup(); + t.cast(); + t.send(1, { type: "select_slot", slot: (t.slot + 1) % 9 }); + t.game.tick(); + assertEquals(t.rod()?.data, undefined); + t.send(1, { type: "select_slot", slot: t.slot }); + assert(!t.wait_for_bite()); +}); + +Deno.test("the line comes out when the water is gone", async () => { + const t = await setup(); + t.cast(); + t.game.set_block(2, 100, 0, "bworld:stone"); + t.game.tick(); + assertEquals(t.rod()?.data, undefined); +}); + +Deno.test("a cast rod shows its cast texture", async () => { + await test_game("mods"); + const rod = new ItemStack(ROD, 1); + assertEquals(item_texture(rod), "bworld:fishing_rod"); + rod.data = { cast: true }; + assertEquals(item_texture(rod), "bworld:fishing_rod_cast"); + assertEquals(EverythingRegistry.get("items", ROD)?.max_stack, 1); +}); diff --git a/tests/mod_loading_test.ts b/tests/mod_loading_test.ts index 7935e56..896047e 100644 --- a/tests/mod_loading_test.ts +++ b/tests/mod_loading_test.ts @@ -27,12 +27,12 @@ Deno.test("the base game loads from mods/bworld", async () => { const bworld = (entries: [string, unknown][]) => entries.filter(([id]) => id.startsWith("bworld:")).length; // 7 kinds of log, planks and leaves assertEquals(bworld(EverythingRegistry.entries("blocks")), 42); - // 33 items (20 of them tools) plus the item forms of the 37 blocks that have one - assertEquals(bworld(EverythingRegistry.entries("items")), 70); - // bworld's 15 (4 wood tools, 7 planks), the cooking pot and stove are farmersdelight's - assertEquals(game.recipes.shaped.length, 17); - // every log smelts and burns - assertEquals(game.recipes.furnace.size, 12); + // 38 items (20 of them tools) plus the item forms of the 37 blocks that have one + assertEquals(bworld(EverythingRegistry.entries("items")), 75); + // bworld's 16 (4 wood tools, 7 planks, the fishing rod), the cooking pot and stove are farmersdelight's + assertEquals(game.recipes.shaped.length, 18); + // every log and fish smelts, every log burns + assertEquals(game.recipes.furnace.size, 14); assertEquals(game.recipes.fuel.size, 8); assertEquals(EverythingRegistry.get("blocks", "bworld:water")?.replaceable, true); // still has its code until it's a component