diff --git a/MODS.md b/MODS.md index e1546ac..565df6b 100644 --- a/MODS.md +++ b/MODS.md @@ -334,6 +334,7 @@ texture per face. `models/slab.json`: | `elements[].faces` | `top`, `bottom`, `north`, `south`, `west` and `east`. Faces are only drawn from the front, so a flat plane needs one face each way. | | `faces.*.texture` | A variable (`"#side"`, filled in by the block's `textures`) or a texture id. | | `faces.*.uv` | `[u1, v1, u2, v2]`, the part of the texture in pixels. Defaults to the part the face covers. | +| `faces.*.rotation` | Turns the texture on the face clockwise by 0, 90, 180 or 270 degrees. | | `faces.*.cullface` | Hidden when the neighbor on that side is a solid block. Only for faces on the block's edge. | Faces flat against the block's edge get smooth lighting and ambient occlusion like a full block's. Anything inside the @@ -478,8 +479,29 @@ A shaped recipe, for the crafting grid in the player's inventory screen: ``` Two furnace recipes with the same `input`, two smithing recipes with the same tool, material and addition, or two -shaped recipes with the same pattern and key, are a load error. Mods can add recipe types of their own for their -machines, see [Common scripts](#common-scripts). +shaped recipes with the same pattern and key, are a load error. + +Mods can use recipe types of their own for their machines: any `type` with a namespace, like Farmer's Delight's +`farmersdelight:cooking` for its cooking pot. The engine only stores them, the mod's server script reads them with +`ctx.recipes.list("farmersdelight:cooking")` and checks the rest of the fields itself (throwing in `setup` makes a bad +recipe a load error). Other mods can add recipes of that type too. `recipes/cooking_cabbage_rolls.json`: + +```json +{ + "format_version": 1, + "recipe": { + "type": "farmersdelight:cooking", + "ingredients": ["farmersdelight:cabbage", ["bworld:carrot", "bworld:potato", "farmersdelight:onion"]], + "result": { "id": "farmersdelight:cabbage_rolls", "count": 1 }, + "cook_time": 100 + } +} +``` + +The cooking pot takes up to 6 `ingredients` in any slots, each an item id or a list of ids any of which will do. The +optional `container` (like `"bworld:bowl"`) is what the meal is served in, and `cook_time` is in ticks, 200 when not +set. It cooks while the block under it is hot: a lit stove (placed lit, relit with coal, put out with a shovel), or a +burning furnace, whose `lit` state is 1 while it has fuel. ## Overrides diff --git a/common/block_models.ts b/common/block_models.ts index 78f85f7..ad79262 100644 --- a/common/block_models.ts +++ b/common/block_models.ts @@ -11,6 +11,8 @@ export interface ModelFaceJson { texture: string; // the part of the texture, [u1, v1, u2, v2] in pixels (0-16). defaults to the part the face covers uv?: [number, number, number, number]; + // turns the texture on the face clockwise: 0, 90, 180 or 270 + rotation?: number; // hidden when the neighbor on this side hides faces (a solid block) cullface?: ModelFace; } @@ -234,6 +236,8 @@ export function bake_model(model: ModelJson, textures: BlockTextures | undefined if (!face_json) continue; const index = FACE_INDEX[face]; const uv = face_json.uv ?? default_uv(face, element.from, element.to); + // each corner takes the uv of the corner a quarter turn along, per 90 degrees + const uv_turns = Math.round((face_json.rotation ?? 0) / 90) % 4; const positions: number[] = []; const uvs: number[] = []; @@ -242,7 +246,7 @@ export function bake_model(model: ModelJson, textures: BlockTextures | undefined point = rotate(point); for (let t = 0; t < turns; t++) point = [1 - point[2], point[1], point[0]]; positions.push(...point); - const [cu, cv] = CORNER_UVS[k]; + const [cu, cv] = CORNER_UVS[(k + uv_turns) % 4]; uvs.push(cu ? uv[2] : uv[0], cv ? uv[3] : uv[1]); }); diff --git a/common/mod_api/server.ts b/common/mod_api/server.ts index 185597c..afacefb 100644 --- a/common/mod_api/server.ts +++ b/common/mod_api/server.ts @@ -252,6 +252,8 @@ export interface ItemApi { exists(id: Id): boolean; // how many fit in one slot max_stack(id: Id): number; + // the tool type it is, like "pickaxe" or "shovel", from the item's "tool" + tool(id: Id): string | undefined; } export interface RecipeApi { @@ -261,6 +263,9 @@ export interface RecipeApi { is_smeltable(item: Id): boolean; // the smithing recipe for these items, whatever the material's count. addition is the optional third item smithing(tool: Id, material: Id, addition?: Id): { result: Id; material_count: number } | undefined; + // every recipe of a mod's own type, like "farmersdelight:cooking", as its json. copies, in load order + // deno-lint-ignore no-explicit-any + list(type: Id): any[]; } // guis diff --git a/common/mod_data.ts b/common/mod_data.ts index 11c8169..e5dfc2f 100644 --- a/common/mod_data.ts +++ b/common/mod_data.ts @@ -92,7 +92,20 @@ export type RecipeJson = material: { id: string; count: number }; addition?: string; result: string; - }; + } + // a mod's own recipe type, like "farmersdelight:cooking" for its cooking pot. the engine only stores these, the mod's + // server script reads them with ctx.recipes.list and checks the rest + | ModRecipeJson; + +export interface ModRecipeJson { + type: `${string}:${string}`; + [field: string]: unknown; +} + +// a recipe type a mod made up, as opposed to the engine's shaped, furnace, fuel and smithing +export function is_mod_recipe(recipe: RecipeJson): recipe is ModRecipeJson { + return recipe.type.includes(":"); +} // the crafting grid's format, see server/game/crafting.ts export interface GridRecipe { @@ -417,6 +430,9 @@ export function validate_model(json: unknown): Problems { if (face.uv !== undefined && (!Array.isArray(face.uv) || face.uv.length !== 4)) { problems.push(`${where}.faces.${side}.uv must be [u1, v1, u2, v2] in pixels`); } + if (face.rotation !== undefined && ![0, 90, 180, 270].includes(face.rotation as number)) { + problems.push(`${where}.faces.${side}.rotation must be 0, 90, 180 or 270`); + } if (face.cullface !== undefined && !MODEL_FACES.includes(face.cullface as ModelFace)) { problems.push(`${where}.faces.${side}.cullface isn't one of ${MODEL_FACES.join(", ")}`); } @@ -505,7 +521,13 @@ export function validate_recipe(json: unknown): Problems { if (json.addition !== undefined) problems.push(...validate_id(json.addition, "addition")); break; default: - problems.push('type must be "shaped", "furnace", "fuel" or "smithing"'); + if (typeof json.type !== "string" || !json.type.includes(":")) { + problems.push( + 'type must be "shaped", "furnace", "fuel", "smithing" or a mod\'s own type like "farmersdelight:cooking"', + ); + } else { + problems.push(...validate_id(json.type, "type")); + } } return problems; } diff --git a/common/mod_loader.ts b/common/mod_loader.ts index 8a51478..2d43362 100644 --- a/common/mod_loader.ts +++ b/common/mod_loader.ts @@ -5,8 +5,10 @@ import { BlockJson, grid_recipe_from_json, GridRecipe, + is_mod_recipe, item_from_json, ItemJson, + ModRecipeJson, OreJson, RecipeJson, } from "./mod_data.ts"; @@ -40,6 +42,8 @@ export class RecipeBook { furnace = new Map(); fuel = new Map(); smithing: Extract[] = []; + // recipes of mods' own types, by type + by_type = new Map(); ores: OreJson[] = []; } @@ -77,6 +81,12 @@ export function register_mod_data(mods: { id: string; data: ModData }[]): Recipe } for (const recipe of data.recipes) { + if (is_mod_recipe(recipe)) { + const list = recipes.by_type.get(recipe.type) ?? []; + recipes.by_type.set(recipe.type, list); + list.push(recipe); + continue; + } switch (recipe.type) { case "shaped": recipes.shaped.push(grid_recipe_from_json(recipe)); diff --git a/mods/bworld/blocks/furnace.json b/mods/bworld/blocks/furnace.json index 08b56c7..35ecb77 100644 --- a/mods/bworld/blocks/furnace.json +++ b/mods/bworld/blocks/furnace.json @@ -13,6 +13,13 @@ }, "drops": "bworld:furnace", "interactive": true, + "states": [ + { + "name": "lit", + "bits": 1, + "default": 0 + } + ], "components": { "bworld:furnace": {} } diff --git a/mods/bworld/items/bowl.json b/mods/bworld/items/bowl.json new file mode 100644 index 0000000..8016789 --- /dev/null +++ b/mods/bworld/items/bowl.json @@ -0,0 +1,7 @@ +{ + "format_version": 1, + "item": { + "id": "bworld:bowl", + "texture": "bworld:bowl" + } +} diff --git a/mods/bworld/recipes/crafting_bowl.json b/mods/bworld/recipes/crafting_bowl.json new file mode 100644 index 0000000..9448b95 --- /dev/null +++ b/mods/bworld/recipes/crafting_bowl.json @@ -0,0 +1,17 @@ +{ + "format_version": 1, + "recipe": { + "type": "shaped", + "pattern": [ + "P P", + " P " + ], + "key": { + "P": "bworld:planks" + }, + "result": { + "id": "bworld:bowl", + "count": 4 + } + } +} diff --git a/mods/bworld/scripts/server.ts b/mods/bworld/scripts/server.ts index cf25100..f7e9ae7 100644 --- a/mods/bworld/scripts/server.ts +++ b/mods/bworld/scripts/server.ts @@ -191,6 +191,12 @@ function register_furnace(ctx: ServerContext) { } } + // burning, so it heats what's on top, like farmer's delight's cooking pot + const lit = data.fuel > 0 ? 1 : 0; + if (ctx.world.get_state(block.x, block.y, block.z, "lit") !== lit) { + ctx.world.set_state(block.x, block.y, block.z, "lit", lit); + } + for (const screen of viewers.get(key(block)) ?? []) show(screen, data); }, }); diff --git a/mods/bworld/textures/bowl.png b/mods/bworld/textures/bowl.png new file mode 100644 index 0000000..52cde8d Binary files /dev/null and b/mods/bworld/textures/bowl.png differ diff --git a/mods/farmersdelight/blocks/cooking_pot.json b/mods/farmersdelight/blocks/cooking_pot.json new file mode 100644 index 0000000..fa37f89 --- /dev/null +++ b/mods/farmersdelight/blocks/cooking_pot.json @@ -0,0 +1,24 @@ +{ + "format_version": 1, + "block": { + "id": "farmersdelight:cooking_pot", + "model": "farmersdelight:cooking_pot", + "textures": { + "side": "farmersdelight:cooking_pot_side", + "top": "farmersdelight:cooking_pot_top", + "bottom": "farmersdelight:cooking_pot_bottom", + "parts": "farmersdelight:cooking_pot_parts" + }, + "render_layer": "cutout", + "mining": { + "toughness": 2, + "tool": "pickaxe" + }, + "drops": "farmersdelight:cooking_pot", + "item": false, + "interactive": true, + "components": { + "farmersdelight:cooking_pot": {} + } + } +} diff --git a/mods/farmersdelight/blocks/stove.json b/mods/farmersdelight/blocks/stove.json new file mode 100644 index 0000000..39e6c62 --- /dev/null +++ b/mods/farmersdelight/blocks/stove.json @@ -0,0 +1,38 @@ +{ + "format_version": 1, + "block": { + "id": "farmersdelight:stove", + "textures": { + "top": "farmersdelight:stove_top", + "bottom": "farmersdelight:stove_bottom", + "front": "farmersdelight:stove_front", + "side": "farmersdelight:stove_side" + }, + "variants": { + "lit=1": { + "textures": { + "top": "farmersdelight:stove_top_on", + "bottom": "farmersdelight:stove_bottom", + "front": "farmersdelight:stove_front_on", + "side": "farmersdelight:stove_side" + } + } + }, + "mining": { + "toughness": 2, + "tool": "pickaxe", + "requires_tool": true + }, + "drops": "farmersdelight:stove", + "states": [ + { + "name": "lit", + "bits": 1, + "default": 1 + } + ], + "components": { + "farmersdelight:stove": {} + } + } +} diff --git a/mods/farmersdelight/items/cabbage_rolls.json b/mods/farmersdelight/items/cabbage_rolls.json new file mode 100644 index 0000000..c6b92ec --- /dev/null +++ b/mods/farmersdelight/items/cabbage_rolls.json @@ -0,0 +1,11 @@ +{ + "format_version": 1, + "item": { + "id": "farmersdelight:cabbage_rolls", + "texture": "farmersdelight:cabbage_rolls", + "food": { + "nutrition": 5, + "saturation_modifier": 0.5 + } + } +} diff --git a/mods/farmersdelight/items/cooked_rice.json b/mods/farmersdelight/items/cooked_rice.json new file mode 100644 index 0000000..54ff563 --- /dev/null +++ b/mods/farmersdelight/items/cooked_rice.json @@ -0,0 +1,13 @@ +{ + "format_version": 1, + "item": { + "id": "farmersdelight:cooked_rice", + "texture": "farmersdelight:cooked_rice", + "max_stack": 16, + "food": { + "nutrition": 6, + "saturation_modifier": 0.4, + "returns": "bworld:bowl" + } + } +} diff --git a/mods/farmersdelight/items/cooking_pot.json b/mods/farmersdelight/items/cooking_pot.json new file mode 100644 index 0000000..0089185 --- /dev/null +++ b/mods/farmersdelight/items/cooking_pot.json @@ -0,0 +1,9 @@ +{ + "format_version": 1, + "item": { + "id": "farmersdelight:cooking_pot", + "texture": "farmersdelight:cooking_pot", + "places": "farmersdelight:cooking_pot", + "max_stack": 1 + } +} diff --git a/mods/farmersdelight/items/tomato_sauce.json b/mods/farmersdelight/items/tomato_sauce.json new file mode 100644 index 0000000..5b155e5 --- /dev/null +++ b/mods/farmersdelight/items/tomato_sauce.json @@ -0,0 +1,12 @@ +{ + "format_version": 1, + "item": { + "id": "farmersdelight:tomato_sauce", + "texture": "farmersdelight:tomato_sauce", + "food": { + "nutrition": 4, + "saturation_modifier": 0.4, + "returns": "bworld:bowl" + } + } +} diff --git a/mods/farmersdelight/manifest.json b/mods/farmersdelight/manifest.json index a422e18..8e69009 100644 --- a/mods/farmersdelight/manifest.json +++ b/mods/farmersdelight/manifest.json @@ -2,7 +2,7 @@ "format_version": 1, "id": "farmersdelight", "name": "Farmer's Delight", - "description": "A port of vectorwing's Farmer's Delight: its crops for now, cooking to come.", + "description": "A port of vectorwing's Farmer's Delight: its crops and the cooking pot.", "version": "0.1.0", "authors": [ "vectorwing (original)", @@ -15,5 +15,8 @@ "version": "*" } ], - "credits": "CREDITS.md" + "credits": "CREDITS.md", + "scripts": { + "server": "scripts/server.ts" + } } diff --git a/mods/farmersdelight/models/cooking_pot.json b/mods/farmersdelight/models/cooking_pot.json new file mode 100644 index 0000000..ce0b6f0 --- /dev/null +++ b/mods/farmersdelight/models/cooking_pot.json @@ -0,0 +1,134 @@ +{ + "format_version": 1, + "model": { + "id": "farmersdelight:cooking_pot", + "textures": { + "side": "farmersdelight:cooking_pot_side", + "top": "farmersdelight:cooking_pot_top", + "bottom": "farmersdelight:cooking_pot_bottom", + "parts": "farmersdelight:cooking_pot_parts" + }, + "elements": [ + { + "from": [2, 0, 2], + "to": [14, 10, 14], + "faces": { + "north": { + "texture": "#side", + "uv": [2, 6, 14, 16] + }, + "east": { + "texture": "#side", + "uv": [2, 6, 14, 16] + }, + "south": { + "texture": "#side", + "uv": [2, 6, 14, 16] + }, + "west": { + "texture": "#side", + "uv": [2, 6, 14, 16] + }, + "top": { + "texture": "#top", + "uv": [2, 2, 14, 14] + }, + "bottom": { + "texture": "#bottom", + "uv": [2, 2, 14, 14], + "cullface": "bottom" + } + } + }, + { + "from": [7, 3, 7], + "to": [9, 15, 9], + "rotation": { + "angle": -22.5, + "axis": "z", + "origin": [8, 3, 8] + }, + "faces": { + "north": { + "texture": "#parts", + "uv": [2, 2, 0, 14] + }, + "east": { + "texture": "#parts", + "uv": [0, 2, 2, 14] + }, + "south": { + "texture": "#parts", + "uv": [2, 2, 0, 14] + }, + "west": { + "texture": "#parts", + "uv": [0, 2, 2, 14] + }, + "top": { + "texture": "#parts", + "uv": [0, 0, 2, 2] + } + } + }, + { + "from": [14, 7, 5], + "to": [16, 9, 11], + "faces": { + "north": { + "texture": "#parts", + "uv": [10, 0, 12, 2] + }, + "east": { + "texture": "#parts", + "uv": [4, 2, 10, 4], + "cullface": "east" + }, + "south": { + "texture": "#parts", + "uv": [2, 0, 4, 2] + }, + "top": { + "texture": "#parts", + "uv": [4, 0, 10, 2], + "rotation": 90 + }, + "bottom": { + "texture": "#parts", + "uv": [4, 2, 10, 4], + "rotation": 90 + } + } + }, + { + "from": [0, 7, 5], + "to": [2, 9, 11], + "faces": { + "north": { + "texture": "#parts", + "uv": [2, 0, 4, 2] + }, + "south": { + "texture": "#parts", + "uv": [10, 0, 12, 2] + }, + "west": { + "texture": "#parts", + "uv": [4, 2, 10, 4], + "cullface": "west" + }, + "top": { + "texture": "#parts", + "uv": [4, 0, 10, 2], + "rotation": 270 + }, + "bottom": { + "texture": "#parts", + "uv": [4, 2, 10, 4], + "rotation": 270 + } + } + } + ] + } +} diff --git a/mods/farmersdelight/recipes/cooking_cabbage_rolls.json b/mods/farmersdelight/recipes/cooking_cabbage_rolls.json new file mode 100644 index 0000000..6bffa0f --- /dev/null +++ b/mods/farmersdelight/recipes/cooking_cabbage_rolls.json @@ -0,0 +1,12 @@ +{ + "format_version": 1, + "recipe": { + "type": "farmersdelight:cooking", + "ingredients": [ + "farmersdelight:cabbage", + ["bworld:carrot", "bworld:potato", "farmersdelight:onion", "farmersdelight:tomato"] + ], + "result": { "id": "farmersdelight:cabbage_rolls", "count": 1 }, + "cook_time": 100 + } +} diff --git a/mods/farmersdelight/recipes/cooking_cooked_rice.json b/mods/farmersdelight/recipes/cooking_cooked_rice.json new file mode 100644 index 0000000..75e209b --- /dev/null +++ b/mods/farmersdelight/recipes/cooking_cooked_rice.json @@ -0,0 +1,10 @@ +{ + "format_version": 1, + "recipe": { + "type": "farmersdelight:cooking", + "ingredients": ["farmersdelight:rice"], + "result": { "id": "farmersdelight:cooked_rice", "count": 1 }, + "container": "bworld:bowl", + "cook_time": 100 + } +} diff --git a/mods/farmersdelight/recipes/cooking_tomato_sauce.json b/mods/farmersdelight/recipes/cooking_tomato_sauce.json new file mode 100644 index 0000000..c7561f4 --- /dev/null +++ b/mods/farmersdelight/recipes/cooking_tomato_sauce.json @@ -0,0 +1,10 @@ +{ + "format_version": 1, + "recipe": { + "type": "farmersdelight:cooking", + "ingredients": ["farmersdelight:tomato", "farmersdelight:tomato"], + "result": { "id": "farmersdelight:tomato_sauce", "count": 1 }, + "container": "bworld:bowl", + "cook_time": 100 + } +} diff --git a/mods/farmersdelight/recipes/crafting_cooking_pot.json b/mods/farmersdelight/recipes/crafting_cooking_pot.json new file mode 100644 index 0000000..b930c34 --- /dev/null +++ b/mods/farmersdelight/recipes/crafting_cooking_pot.json @@ -0,0 +1,9 @@ +{ + "format_version": 1, + "recipe": { + "type": "shaped", + "pattern": ["iSi", "i i", "iii"], + "key": { "i": "bworld:iron_ingot", "S": "bworld:stick" }, + "result": { "id": "farmersdelight:cooking_pot", "count": 1 } + } +} diff --git a/mods/farmersdelight/recipes/crafting_stove.json b/mods/farmersdelight/recipes/crafting_stove.json new file mode 100644 index 0000000..13637b5 --- /dev/null +++ b/mods/farmersdelight/recipes/crafting_stove.json @@ -0,0 +1,9 @@ +{ + "format_version": 1, + "recipe": { + "type": "shaped", + "pattern": ["iii", "B B", "BCB"], + "key": { "i": "bworld:iron_ingot", "B": "bworld:stone", "C": "bworld:coal" }, + "result": { "id": "farmersdelight:stove", "count": 1 } + } +} diff --git a/mods/farmersdelight/scripts/server.ts b/mods/farmersdelight/scripts/server.ts new file mode 100644 index 0000000..ed27e59 --- /dev/null +++ b/mods/farmersdelight/scripts/server.ts @@ -0,0 +1,314 @@ +// farmer's delight's kitchen. the stove is a heat source, like its StoveBlock. the cooking pot, like its +// CookingPotBlockEntity, cooks its recipes while something hot is under it, keeps the meal, and serves it into the +// container the meal needs (a bowl), or straight out when it needs none +import type { BlockRef, Container, ScreenHandle, ServerContext } from "bworld/server"; + +export function setup(ctx: ServerContext) { + register_stove(ctx); + register_cooking_pot(ctx); +} + +// stove + +// what lights an unlit stove, used up. the java mod's are flint and steel and fire charges, which bworld doesn't have +const STOVE_FUEL = "bworld:coal"; +// what puts it out: any shovel, like the java mod (a water bucket too, once there are buckets) +const STOVE_EXTINGUISHER_TOOL = "shovel"; +// standing on a lit stove hurts, like a magma block: 1 health every 10 ticks +const STOVE_BURN_DAMAGE = 1; +const STOVE_BURN_TICKS = 10; + +function register_stove(ctx: ServerContext) { + const lit = (block: BlockRef) => ctx.world.get_state(block.x, block.y, block.z, "lit") === 1; + const set_lit = (block: BlockRef, value: boolean) => + ctx.world.set_state(block.x, block.y, block.z, "lit", value ? 1 : 0); + + ctx.components.register_block("farmersdelight:stove", { + // placed lit, like the java mod. block data so it ticks, to burn whoever stands on it + on_create(block) { + block.data ??= {}; + }, + on_interact(block, _params, player) { + const held = player.inventory.get(player.selected_slot); + if (!held) return false; + if (lit(block)) { + if (ctx.items.tool(held.id) !== STOVE_EXTINGUISHER_TOOL) return false; + set_lit(block, false); + return true; + } + if (held.id !== STOVE_FUEL) return false; + held.count -= 1; + set_lit(block, true); + return true; + }, + on_tick(block) { + if (ctx.system.current_tick % STOVE_BURN_TICKS !== 0 || !lit(block)) return; + for (const player of ctx.players.all()) { + const { x, y, z } = player.position; + if (Math.floor(x) === block.x && Math.floor(z) === block.z && Math.abs(y - (block.y + 1)) < 0.01) { + player.damage(STOVE_BURN_DAMAGE, "hot_floor"); + } + } + }, + }); +} + +// cooking pot + +// slots 0-5 are the ingredients, 6 the meal waiting to be served, 7 containers for it, 8 the served meal +const INGREDIENT_SLOTS = 6; +const MEAL_SLOT = 6; +const CONTAINER_SLOT = 7; +const OUTPUT_SLOT = 8; +const POT_SIZE = 9; +// ticks, for recipes that don't say +const DEFAULT_COOK_TIME = 200; + +// what's under the pot that heats it, and the state that has to be 1 for it to, like minecraft's heat_sources tag +const HEAT_SOURCES: Record = { + "bworld:furnace": "lit", + "farmersdelight:stove": "lit", +}; + +// a farmersdelight:cooking recipe: the ingredients in any slots, each an item id or a list of ids any of which will do +interface CookingRecipe { + ingredients: (string | string[])[]; + result: { id: string; count: number }; + // what the meal is served in, like a bowl. without it the meal comes out as soon as it's cooked + container?: string; + cook_time: number; +} + +interface PotData { + container: string; + // ticks, counting up to cook_time_total + cook_time: number; + cook_time_total: number; + // what the meal in the meal slot is served in, "" for nothing + meal_container: string; +} + +const POT_BARS = [ + { + x: 2.2, + y: 2, + value: "heat", + max: "heat_max", + direction: "up" as const, + empty_texture: "bworld:fire_empty", + full_texture: "bworld:fire_full", + }, + { + x: 4.6, + y: 0.5, + value: "progress", + max: "progress_max", + direction: "right" as const, + empty_texture: "bworld:arrow_empty", + full_texture: "bworld:arrow_full", + }, +]; + +// checked when the server starts, so a broken recipe is a load error naming it +function load_recipes(ctx: ServerContext): CookingRecipe[] { + return ctx.recipes.list("farmersdelight:cooking").map((json, i) => { + const where = `farmersdelight:cooking recipe ${i + 1} (${json.result?.id ?? "no result"})`; + const item = (id: unknown) => { + if (typeof id !== "string" || !ctx.items.exists(id)) throw new Error(`${where}: there's no item ${id}`); + return id; + }; + const ingredients = json.ingredients; + if (!Array.isArray(ingredients) || ingredients.length < 1 || ingredients.length > INGREDIENT_SLOTS) { + throw new Error(`${where}: ingredients must be 1 to ${INGREDIENT_SLOTS} items`); + } + for (const ingredient of ingredients) { + if (Array.isArray(ingredient)) { + if (ingredient.length === 0) throw new Error(`${where}: an ingredient's list of items is empty`); + ingredient.forEach(item); + } else { + item(ingredient); + } + } + item(json.result?.id); + if (!Number.isInteger(json.result.count) || json.result.count < 1) { + throw new Error(`${where}: result.count must be a positive whole number`); + } + if (json.container !== undefined) item(json.container); + const cook_time = json.cook_time ?? DEFAULT_COOK_TIME; + if (!Number.isInteger(cook_time) || cook_time < 1) { + throw new Error(`${where}: cook_time must be positive ticks`); + } + return { ingredients, result: json.result, container: json.container, cook_time }; + }); +} + +// the recipe whose ingredients are exactly what's in the ingredient slots, one item from each, in any order +function find_recipe(recipes: CookingRecipe[], container: Container): CookingRecipe | undefined { + const items: string[] = []; + for (let slot = 0; slot < INGREDIENT_SLOTS; slot++) { + const item = container.get(slot); + if (item) items.push(item.id); + } + if (items.length === 0) return undefined; + return recipes.find((recipe) => recipe.ingredients.length === items.length && matches(recipe.ingredients, items)); +} + +// pairs every ingredient with a different item, trying each way round, like a shapeless recipe +function matches(ingredients: (string | string[])[], items: string[], used = new Set()): boolean { + if (ingredients.length === 0) return true; + const [first, ...rest] = ingredients; + const fits = (id: string) => Array.isArray(first) ? first.includes(id) : first === id; + for (let i = 0; i < items.length; i++) { + if (used.has(i) || !fits(items[i])) continue; + used.add(i); + if (matches(rest, items, used)) return true; + used.delete(i); + } + return false; +} + +function register_cooking_pot(ctx: ServerContext) { + const recipes = load_recipes(ctx); + // who's looking at each pot, by position, to keep their bars moving + const viewers = new Map>(); + const key = (block: BlockRef) => `${block.x},${block.y},${block.z}`; + + const pot_data = (block: BlockRef): PotData => { + const existing = block.data?.container && ctx.containers.get(block.data.container); + const container = existing || ctx.containers.create(POT_SIZE); + block.data = { cook_time: 0, cook_time_total: 0, meal_container: "", ...block.data, container: container.id }; + return block.data; + }; + + const is_heated = (block: BlockRef) => { + const below = ctx.world.get_block(block.x, block.y - 1, block.z); + if (!below || !(below in HEAT_SOURCES)) return false; + const state = HEAT_SOURCES[below]; + return state === undefined || ctx.world.get_state(block.x, block.y - 1, block.z, state) === 1; + }; + + const show = (screen: ScreenHandle, data: PotData, heated: boolean) => { + screen.set_property("progress", data.cook_time); + screen.set_property("progress_max", data.cook_time_total); + screen.set_property("heat", heated ? 1 : 0); + screen.set_property("heat_max", 1); + }; + + // there's room in the meal slot for what it makes + const can_cook = (container: Container, recipe: CookingRecipe) => { + const meal = container.get(MEAL_SLOT); + if (!meal) return true; + return meal.id === recipe.result.id && meal.count + recipe.result.count <= ctx.items.max_stack(meal.id); + }; + + const cook = (data: PotData, container: Container, recipe: CookingRecipe) => { + const meal = container.get(MEAL_SLOT); + if (meal) { + meal.count += recipe.result.count; + } else { + container.set(MEAL_SLOT, { ...recipe.result }); + } + data.meal_container = recipe.container ?? ""; + for (let slot = 0; slot < INGREDIENT_SLOTS; slot++) { + const item = container.get(slot); + if (item) item.count -= 1; + } + }; + + // the meal goes to the output, into its containers when it needs them, as much as fits + const serve = (data: PotData, container: Container) => { + const meal = container.get(MEAL_SLOT); + if (!meal) { + data.meal_container = ""; + return; + } + const output = container.get(OUTPUT_SLOT); + if (output && output.id !== meal.id) return; + const room = ctx.items.max_stack(meal.id) - (output?.count ?? 0); + let count = Math.min(meal.count, room); + const bowls = container.get(CONTAINER_SLOT); + if (data.meal_container) { + count = bowls?.id === data.meal_container ? Math.min(count, bowls.count) : 0; + } + if (count <= 0) return; + if (data.meal_container) bowls!.count -= count; + if (output) { + output.count += count; + } else { + container.set(OUTPUT_SLOT, { id: meal.id, count }); + } + meal.count -= count; + }; + + ctx.components.register_block("farmersdelight:cooking_pot", { + on_create(block) { + pot_data(block); + }, + on_interact(block, _params, player) { + const data = pot_data(block); + const container = ctx.containers.get(data.container)!; + + // holding what the meal is served in serves one straight into the hand, like the java mod + const meal = container.get(MEAL_SLOT); + const held = player.inventory.get(player.selected_slot); + if (meal && data.meal_container && held?.id === data.meal_container) { + held.count -= 1; + meal.count -= 1; + player.give_item(meal.id, 1); + return true; + } + + const screen = ctx.ui.open_container(player, { + container, + layout: [ + ...Array.from( + { length: INGREDIENT_SLOTS }, + (_, slot) => ({ slot, x: 1.2 + slot % 3, y: Math.floor(slot / 3) }), + ), + // the meal stays in the pot until it's served + { slot: MEAL_SLOT, x: 6.4, y: 0.5, output_only: true, on_take: () => false }, + { slot: CONTAINER_SLOT, x: 4.6, y: 2 }, + { slot: OUTPUT_SLOT, x: 6.4, y: 2, output_only: true }, + ], + rows: 3, + bars: POT_BARS, + }); + show(screen, data, is_heated(block)); + const screens = viewers.get(key(block)) ?? new Set(); + viewers.set(key(block), screens); + screens.add(screen); + screen.on_close(() => screens.delete(screen)); + return true; + }, + on_break(block) { + const container = block.data?.container && ctx.containers.get(block.data.container); + if (!container) return; + for (let slot = 0; slot < container.size; slot++) { + const item = container.get(slot); + if (item) ctx.world.drop_item(block.x, block.y, block.z, item); + } + ctx.containers.delete(container.id); + }, + on_tick(block) { + const data = pot_data(block); + const container = ctx.containers.get(data.container)!; + const heated = is_heated(block); + const recipe = heated ? find_recipe(recipes, container) : undefined; + + if (recipe && can_cook(container, recipe)) { + data.cook_time_total = recipe.cook_time; + data.cook_time += 1; + if (data.cook_time >= recipe.cook_time) { + data.cook_time = 0; + cook(data, container, recipe); + } + } else { + // cools off, losing progress slowly + data.cook_time = Math.max(0, data.cook_time - 2); + } + serve(data, container); + + for (const screen of viewers.get(key(block)) ?? []) show(screen, data, heated); + }, + }); +} diff --git a/mods/farmersdelight/textures/cabbage_rolls.png b/mods/farmersdelight/textures/cabbage_rolls.png new file mode 100644 index 0000000..441c19e Binary files /dev/null and b/mods/farmersdelight/textures/cabbage_rolls.png differ diff --git a/mods/farmersdelight/textures/cooked_rice.png b/mods/farmersdelight/textures/cooked_rice.png new file mode 100644 index 0000000..df688f2 Binary files /dev/null and b/mods/farmersdelight/textures/cooked_rice.png differ diff --git a/mods/farmersdelight/textures/cooking_pot.png b/mods/farmersdelight/textures/cooking_pot.png new file mode 100644 index 0000000..20429ef Binary files /dev/null and b/mods/farmersdelight/textures/cooking_pot.png differ diff --git a/mods/farmersdelight/textures/cooking_pot_bottom.png b/mods/farmersdelight/textures/cooking_pot_bottom.png new file mode 100644 index 0000000..2facbc7 Binary files /dev/null and b/mods/farmersdelight/textures/cooking_pot_bottom.png differ diff --git a/mods/farmersdelight/textures/cooking_pot_parts.png b/mods/farmersdelight/textures/cooking_pot_parts.png new file mode 100644 index 0000000..884b7a9 Binary files /dev/null and b/mods/farmersdelight/textures/cooking_pot_parts.png differ diff --git a/mods/farmersdelight/textures/cooking_pot_side.png b/mods/farmersdelight/textures/cooking_pot_side.png new file mode 100644 index 0000000..64fec9a Binary files /dev/null and b/mods/farmersdelight/textures/cooking_pot_side.png differ diff --git a/mods/farmersdelight/textures/cooking_pot_top.png b/mods/farmersdelight/textures/cooking_pot_top.png new file mode 100644 index 0000000..d911e18 Binary files /dev/null and b/mods/farmersdelight/textures/cooking_pot_top.png differ diff --git a/mods/farmersdelight/textures/stove_bottom.png b/mods/farmersdelight/textures/stove_bottom.png new file mode 100644 index 0000000..31b6a69 Binary files /dev/null and b/mods/farmersdelight/textures/stove_bottom.png differ diff --git a/mods/farmersdelight/textures/stove_front.png b/mods/farmersdelight/textures/stove_front.png new file mode 100644 index 0000000..ae46260 Binary files /dev/null and b/mods/farmersdelight/textures/stove_front.png differ diff --git a/mods/farmersdelight/textures/stove_front_on.png b/mods/farmersdelight/textures/stove_front_on.png new file mode 100644 index 0000000..7502046 Binary files /dev/null and b/mods/farmersdelight/textures/stove_front_on.png differ diff --git a/mods/farmersdelight/textures/stove_side.png b/mods/farmersdelight/textures/stove_side.png new file mode 100644 index 0000000..616c1f6 Binary files /dev/null and b/mods/farmersdelight/textures/stove_side.png differ diff --git a/mods/farmersdelight/textures/stove_top.png b/mods/farmersdelight/textures/stove_top.png new file mode 100644 index 0000000..05e3dca Binary files /dev/null and b/mods/farmersdelight/textures/stove_top.png differ diff --git a/mods/farmersdelight/textures/stove_top_on.png b/mods/farmersdelight/textures/stove_top_on.png new file mode 100644 index 0000000..bf1bb3e Binary files /dev/null and b/mods/farmersdelight/textures/stove_top_on.png differ diff --git a/mods/farmersdelight/textures/tomato_sauce.png b/mods/farmersdelight/textures/tomato_sauce.png new file mode 100644 index 0000000..0d5c933 Binary files /dev/null and b/mods/farmersdelight/textures/tomato_sauce.png differ diff --git a/server/game/game_server.ts b/server/game/game_server.ts index 071ef0f..192ff97 100644 --- a/server/game/game_server.ts +++ b/server/game/game_server.ts @@ -1431,6 +1431,9 @@ function death_message(name: string, cause: string) { return `${name} fell out of the world`; case "starve": return `${name} starved to death`; + // standing on something hot, like a lit stove + case "hot_floor": + return `${name} discovered the floor was lava`; default: return `${name} died`; } diff --git a/server/game/mod_runtime.ts b/server/game/mod_runtime.ts index d2f875a..60320bf 100644 --- a/server/game/mod_runtime.ts +++ b/server/game/mod_runtime.ts @@ -467,6 +467,11 @@ export class ModRuntime { if (!item) throw new Error(`unknown item ${id}`); return item.max_stack ?? 64; }, + tool: (id) => { + const item = EverythingRegistry.get("items", id); + if (!item) throw new Error(`unknown item ${id}`); + return item.tool_type; + }, }, recipes: { furnace_result: (input) => { @@ -482,6 +487,7 @@ export class ModRuntime { ); return recipe && { result: recipe.result, material_count: recipe.material.count }; }, + list: (type) => structuredClone(game().recipes.by_type.get(type) ?? []), }, containers: { create: (size) => { diff --git a/tests/commands_test.ts b/tests/commands_test.ts index 701731f..2813da4 100644 --- a/tests/commands_test.ts +++ b/tests/commands_test.ts @@ -135,6 +135,7 @@ Deno.test("/give suggests item ids as they're typed", async () => { assert(!all.includes("bworld:air")); assertEquals(suggest("/give farmersdelight:tom").suggestions.map((s: { text: string }) => s.text), [ "farmersdelight:tomato", + "farmersdelight:tomato_sauce", "farmersdelight:tomato_seeds", ]); diff --git a/tests/cooking_test.ts b/tests/cooking_test.ts new file mode 100644 index 0000000..72fb272 --- /dev/null +++ b/tests/cooking_test.ts @@ -0,0 +1,184 @@ +// farmer's delight's cooking pot: cooks its recipes over a lit furnace, and serves meals into bowls +import { assert, assertEquals } from "@std/assert"; +import { ItemStack } from "$/common/inventory.ts"; +import { bake_model } from "$/common/block_models.ts"; +import { validate_recipe } from "$/common/mod_data.ts"; +import { get_state_value, set_state_value } from "$/common/utils.ts"; +import { test_game } from "./helpers.ts"; + +const POT = [0, 101, 0] as const; +const FURNACE = [0, 100, 0] as const; + +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.send(1, { type: "move", x: 2.5, y: 100, z: 2.5, yaw: 0, pitch: 0 }); + t.game.set_block(...FURNACE, "bworld:furnace"); + t.game.set_block(...POT, "farmersdelight:cooking_pot"); + + const container = (at: readonly [number, number, number]) => { + const id = (t.game.world.get_tile(...at)?.mod_data as { container: string }).container; + return t.game.containers.get(id)!; + }; + const pot = container(POT); + const put = (slot: number, id: string, count = 1) => pot.set_item(slot, new ItemStack(id, count)); + const slot = (slot: number) => { + const item = pot.get_item(slot); + return item ? `${item.type_id} ${item.amount}` : ""; + }; + // something to smelt and coal, so the furnace burns + const light_furnace = () => { + container(FURNACE).set_item(0, new ItemStack("bworld:log", 64)); + container(FURNACE).set_item(1, new ItemStack("bworld:coal", 64)); + }; + const ticks = (n: number) => { + for (let i = 0; i < n; i++) t.game.tick(); + }; + return { ...t, pot, put, slot, light_furnace, ticks }; +} + +Deno.test("the pot only cooks over a lit furnace, then serves the meal into bowls", async () => { + const t = await setup(); + t.put(0, "farmersdelight:tomato", 2); + t.put(3, "farmersdelight:tomato", 2); + + // cold: nothing happens + t.ticks(150); + assertEquals(t.slot(0), "farmersdelight:tomato 2"); + assertEquals(t.slot(6), ""); + + t.light_furnace(); + t.ticks(1); + assertEquals(t.game.world.get_block_id(...FURNACE), "bworld:furnace"); + t.ticks(100); + // one sauce, one tomato from each slot, waiting in the pot for a bowl + assertEquals(t.slot(6), "farmersdelight:tomato_sauce 1"); + assertEquals(t.slot(0), "farmersdelight:tomato 1"); + assertEquals(t.slot(3), "farmersdelight:tomato 1"); + assertEquals(t.slot(8), ""); + + t.ticks(100); + assertEquals(t.slot(6), "farmersdelight:tomato_sauce 2"); + assertEquals(t.slot(0), ""); + + // bowls take it out + t.put(7, "bworld:bowl", 5); + t.ticks(1); + assertEquals(t.slot(6), ""); + assertEquals(t.slot(7), "bworld:bowl 3"); + assertEquals(t.slot(8), "farmersdelight:tomato_sauce 2"); +}); + +Deno.test("ingredients go in any slots, lists take any of their items, and meals without a container come right out", async () => { + const t = await setup(); + t.light_furnace(); + t.put(5, "bworld:potato"); + t.put(1, "farmersdelight:cabbage"); + t.ticks(101); + assertEquals(t.slot(8), "farmersdelight:cabbage_rolls 1"); + assertEquals(t.slot(6), ""); + + // something extra matches nothing + t.put(0, "farmersdelight:cabbage"); + t.put(1, "farmersdelight:onion"); + t.put(2, "farmersdelight:rice"); + t.ticks(300); + assertEquals(t.slot(8), "farmersdelight:cabbage_rolls 1"); + assertEquals(t.slot(0), "farmersdelight:cabbage 1"); + + // a different meal waits until the pot's empty of the other + t.pot.set_item(0, undefined); + t.pot.set_item(1, undefined); + t.ticks(100); + assertEquals(t.slot(6), "farmersdelight:cooked_rice 1"); + t.put(0, "farmersdelight:tomato", 2); + t.put(1, "farmersdelight:tomato", 2); + t.ticks(300); + assertEquals(t.slot(6), "farmersdelight:cooked_rice 1"); + assertEquals(t.slot(0), "farmersdelight:tomato 2"); +}); + +Deno.test("holding a bowl serves the meal straight into your hand, and breaking the pot drops everything", async () => { + const t = await setup(); + t.light_furnace(); + t.put(0, "farmersdelight:rice", 3); + t.ticks(301); + assertEquals(t.slot(6), "farmersdelight:cooked_rice 3"); + + t.send(1, { type: "chat", text: "/give bworld:bowl 2" }); + const inventory = () => t.game.players()[0].inventory.to_data().filter((i) => i); + t.send(1, { type: "select_slot", slot: 0 }); + t.send(1, { type: "use_block", x: POT[0], y: POT[1], z: POT[2], face: "top" }); + assertEquals(t.slot(6), "farmersdelight:cooked_rice 2"); + assertEquals(inventory().map((i) => `${i!.id} ${i!.count}`), ["bworld:bowl 1", "farmersdelight:cooked_rice 1"]); + + t.game.destroy_block(...POT); + const dropped = t.game.item_entities().map((e) => `${e.item.type_id} ${e.item.amount}`); + assert(dropped.includes("farmersdelight:cooked_rice 2"), String(dropped)); +}); + +Deno.test("mods' own recipe types are accepted, and model faces can turn their texture", () => { + assertEquals(validate_recipe({ type: "farmersdelight:cooking", anything: 1 }), []); + assertEquals(validate_recipe({ type: "cooking" }).length, 1); + + const face = (rotation?: number) => + bake_model({ + id: "test:m", + elements: [{ + from: [0, 0, 0], + to: [16, 16, 16], + faces: { top: { texture: "test:t", uv: [0, 0, 8, 4], rotation } }, + }], + }, undefined)[0].uvs; + assertEquals(face(), [0, 4, 8, 4, 8, 0, 0, 0]); + // each corner gets the next corner's uv + assertEquals(face(90), [8, 4, 8, 0, 0, 0, 0, 4]); + assertEquals(face(180), [8, 0, 0, 0, 0, 4, 8, 4]); +}); + +Deno.test("a stove is placed lit and heats the pot, coal relights it, and standing on it burns", async () => { + const t = await setup(); + const info = () => t.game.world.get_block_info(...FURNACE)!; + const lit = () => get_state_value(t.game.world.get_block_value(...FURNACE), info(), "lit"); + t.game.set_block(...FURNACE, "farmersdelight:stove"); + assertEquals(lit(), 1); + + t.put(0, "farmersdelight:rice"); + t.ticks(101); + assertEquals(t.slot(6), "farmersdelight:cooked_rice 1"); + + // out, it doesn't cook + t.game.set_block_state( + ...FURNACE, + set_state_value(t.game.world.get_block_value(...FURNACE), info(), "lit", 0)! >>> 16, + ); + t.put(0, "farmersdelight:rice"); + t.ticks(150); + assertEquals(t.slot(6), "farmersdelight:cooked_rice 1"); + + // coal lights it again, used up + t.send(1, { type: "chat", text: "/give bworld:coal 2" }); + t.send(1, { type: "select_slot", slot: 0 }); + t.send(1, { type: "use_block", x: FURNACE[0], y: FURNACE[1], z: FURNACE[2], face: "north" }); + assertEquals(lit(), 1); + assertEquals(t.game.players()[0].inventory.get_item(0)?.amount, 1); + // already lit, coal does nothing + t.send(1, { type: "use_block", x: FURNACE[0], y: FURNACE[1], z: FURNACE[2], face: "north" }); + assertEquals(t.game.players()[0].inventory.get_item(0)?.amount, 1); + + // standing on it: 1 every 10 ticks + t.game.destroy_block(...POT); + t.send(1, { type: "move", x: 0.5, y: 101, z: 0.5, yaw: 0, pitch: 0 }); + const health = () => t.game.players()[0].vitals.health; + // (full hunger heals some of it back meanwhile, like minecraft) + const before = health(); + t.ticks(40); + assert(health() < before, `${health()} of ${before}`); + t.send(1, { type: "move", x: 2.5, y: 100, z: 2.5, yaw: 0, pitch: 0 }); + const off = health(); + t.ticks(40); + assert(health() >= off); +}); diff --git a/tests/mod_loading_test.ts b/tests/mod_loading_test.ts index 87fd08b..9492789 100644 --- a/tests/mod_loading_test.ts +++ b/tests/mod_loading_test.ts @@ -25,9 +25,10 @@ Deno.test("the base game loads from mods/bworld", async () => { const { game } = await test_game("mods"); const bworld = (entries: [string, unknown][]) => entries.filter(([id]) => id.startsWith("bworld:")).length; assertEquals(bworld(EverythingRegistry.entries("blocks")), 23); - // 17 items plus the item forms of the 18 blocks that have one - assertEquals(bworld(EverythingRegistry.entries("items")), 35); - assertEquals(game.recipes.shaped.length, 5); + // 18 items plus the item forms of the 18 blocks that have one + assertEquals(bworld(EverythingRegistry.entries("items")), 36); + // the bowl is bworld's 6th, the cooking pot and stove are farmersdelight's + assertEquals(game.recipes.shaped.length, 8); assertEquals(game.recipes.furnace.size, 6); assertEquals(game.recipes.fuel.size, 2); assertEquals(EverythingRegistry.get("blocks", "bworld:water")?.replaceable, true);