Add a bunch of crops and farmer's delight crops
@@ -26,7 +26,7 @@ bworld's modding takes from three places:
|
||||
- [Mod layout](#mod-layout)
|
||||
- [manifest.json](#manifestjson)
|
||||
- [Identifiers](#identifiers)
|
||||
- Data: [Textures](#textures), [Blocks](#blocks), [Block models](#block-models), [Items](#items),
|
||||
- Data: [Textures](#textures), [Blocks](#blocks), [Block models](#block-models), [Crops](#crops), [Items](#items),
|
||||
[Recipes](#recipes), [Overrides](#overrides)
|
||||
- Game API: [Common scripts](#common-scripts), [Server scripts](#server-scripts), [Client scripts](#client-scripts),
|
||||
[GUIs](#guis), [Mod channels](#mod-channels), [World generation](#world-generation)
|
||||
@@ -250,6 +250,7 @@ program**, so the server and each client can number blocks differently. Saves an
|
||||
| `item` | `true` | `register_block_item` | Also register an item that places this block, with the same id. |
|
||||
| `interactive` | `false` | `interactive` | Right clicking it does something (opens a screen), so clients don't guess it places a block. |
|
||||
| `replaceable` | `false` | new | Placing a block into it replaces it, like water. |
|
||||
| `place_in` | see meaning | `place_in` | Block ids it can be placed into, replacing them, like `["bworld:water"]` for rice. Air and `replaceable` blocks when not set. |
|
||||
| `place_on` | anywhere | `place_on` | Block ids it can stand on, like `["bworld:hoed_dirt"]` for crops. Players can't place it anywhere else, and it breaks (with its drops) when the block under it becomes something else, like Minecraft's `canSurvive`. |
|
||||
| `states` | none | `states` | Up to 16 bits of named state, like `hoed_dirt` has now. |
|
||||
| `components` | none | `components` | Custom components with parameters, handled by server scripts. |
|
||||
@@ -338,6 +339,60 @@ texture per face. `models/slab.json`:
|
||||
Faces flat against the block's edge get smooth lighting and ambient occlusion like a full block's. Anything inside the
|
||||
block is lit evenly with the block's own light.
|
||||
|
||||
## Crops
|
||||
|
||||
Like a Fabric `CropBlock`, a crop is a block with an `age` state and a seeds item that places it, so adding one is only
|
||||
data. The base game's `bworld:crop` component (in `mods/bworld/scripts/server.ts`) does the growing, with Stardew
|
||||
Valley's clock: every morning at 06:00 a crop whose farmland was watered the day before grows a day, and it shows the
|
||||
stage for how far along it is. `mods/farmersdelight` adds its cabbages, onions, tomatoes and rice this way.
|
||||
|
||||
`blocks/cabbages.json`, with its `variants` giving one texture per age (see [Block models](#block-models)):
|
||||
|
||||
```json
|
||||
{
|
||||
"format_version": 1,
|
||||
"block": {
|
||||
"id": "farmersdelight:cabbages",
|
||||
"model": "engine:cross",
|
||||
"render_layer": "cutout",
|
||||
"collision": false,
|
||||
"mining": { "toughness": 0 },
|
||||
"item": false,
|
||||
"place_on": ["bworld:hoed_dirt"],
|
||||
"states": [{ "name": "age", "bits": 3, "default": 0 }],
|
||||
"components": {
|
||||
"bworld:crop": {
|
||||
"days": 12,
|
||||
"stages": 8,
|
||||
"fertilizer": "bworld:bone_meal",
|
||||
"drops": {
|
||||
"young": "farmersdelight:cabbage_seeds",
|
||||
"mature": "farmersdelight:cabbage",
|
||||
"bonus": "farmersdelight:cabbage_seeds"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
and `items/cabbage_seeds.json` with `"places": "farmersdelight:cabbages"`.
|
||||
|
||||
| Param | Meaning |
|
||||
| --------------- | ------------------------------------------------------------------------------------------------------------ |
|
||||
| `days` | Watered mornings from planting to fully grown. |
|
||||
| `stages` | How many ages it shows, 0 to `stages - 1`, spread evenly over `days`. The `age` state must fit them. |
|
||||
| `drops` | Like a crop's loot table: `young` (not grown yet), `mature` (grown), and `bonus`, 0 to 3 more of an item when grown, like wheat's extra seeds. `young` and `mature` can be one id or a list. |
|
||||
| `fertilizer` | An item that grows it a day right away, like bone meal. |
|
||||
| `regrow_days`, `harvest` | Picked by right clicking once grown, dropping `harvest.item` (`count` is `[min, max]`), then grown again after `regrow_days`, like tomatoes. |
|
||||
| `water` | `"farmland"` (the default): needs watered farmland under it. `"always"`: grows every morning, like rice in water. |
|
||||
| `upper` | `{ block, stages, supporting_age }`: its last stages grow a second block on top, like rice's panicles. The top block uses the `bworld:crop_top` component with `{ stages, drops }`; breaking it harvests the whole plant. |
|
||||
| `leaves` | What the spot becomes after it's broken, like water where rice was. |
|
||||
|
||||
Players place blocks into the block they click when it's `replaceable`, like Minecraft, so rice (with
|
||||
`"place_in": ["bworld:water"]`) is planted by clicking water. Food that places a block, like potatoes, is planted when
|
||||
clicked on somewhere it can go and eaten otherwise.
|
||||
|
||||
## Items
|
||||
|
||||
`items/copper_pickaxe.json`:
|
||||
@@ -663,6 +718,7 @@ or place, the server tells that player's client to undo what it already showed.
|
||||
interface ServerWorld {
|
||||
get_block(x: number, y: number, z: number): string | undefined; // undefined when the chunk isn't loaded
|
||||
set_block(x: number, y: number, z: number, id: string): boolean; // runs on_break / on_create, synced to everyone
|
||||
break_block(x: number, y: number, z: number): boolean; // like breaking it by hand: on_break, then its drops
|
||||
get_state(x: number, y: number, z: number, name: string): number | undefined;
|
||||
set_state(x: number, y: number, z: number, name: string, value: number): boolean;
|
||||
get_block_data<T>(x: number, y: number, z: number): T | undefined;
|
||||
|
||||
@@ -4,7 +4,7 @@ import type { Client } from "./client.ts";
|
||||
import type { BlockHitResult } from "./level/client_level.ts";
|
||||
import type { Container } from "$/common/inventory.ts";
|
||||
import { AIR_ID, type ContainerKey } from "$/common/protocol.ts";
|
||||
import { can_stand_on } from "$/common/utils.ts";
|
||||
import { can_place, placement_target } from "$/common/utils.ts";
|
||||
|
||||
// breaking and using blocks against a server, like minecraft's MultiPlayerGameMode. the client times breaking
|
||||
// and guesses the results so they feel instant, the server decides what really happens
|
||||
@@ -54,20 +54,22 @@ export class MultiPlayerGameMode {
|
||||
const level = this.client.level;
|
||||
this.client.connection.send({ type: "use_block", x: hit.x, y: hit.y, z: hit.z, face: hit.face });
|
||||
|
||||
// guess that it places the held block, unless the block does something when used
|
||||
// guess that it places the held block, unless the block does something when used. the same rules as the server:
|
||||
// into the clicked block when it's replaceable, and only where place_in and place_on allow
|
||||
const block_info = EverythingRegistry.get_by_id<BlockRegistry>("blocks", hit.block)!;
|
||||
const offset = FACE_OFFSETS[hit.face];
|
||||
const target = { x: hit.x + offset.x, y: hit.y + offset.y, z: hit.z + offset.z };
|
||||
const target_id = level.get_block(target.x, target.y, target.z);
|
||||
const replaceable = target_id === AIR ||
|
||||
EverythingRegistry.get_by_id<BlockRegistry>("blocks", target_id)?.replaceable;
|
||||
const target = placement_target(hit.x, hit.y, hit.z, FACE_OFFSETS[hit.face], block_info);
|
||||
const current = level.get_block(target.x, target.y, target.z);
|
||||
const below = level.get_block(target.x, target.y - 1, target.z);
|
||||
const below_id = EverythingRegistry.get_by_id<BlockRegistry>("blocks", below)?.id ?? AIR_ID;
|
||||
const held = this.#held_item();
|
||||
// items with components might do something else on the server, like on_use
|
||||
const place_id = held?.components ? undefined : held?.block_id;
|
||||
const below = level.get_block(target.x, target.y - 1, target.z);
|
||||
const below_id = EverythingRegistry.get_by_id<BlockRegistry>("blocks", below)?.id ?? AIR_ID;
|
||||
const stands = can_stand_on(EverythingRegistry.get<BlockRegistry>("blocks", place_id ?? ""), below_id);
|
||||
if (!block_info.interactive && place_id && replaceable && stands) {
|
||||
const placeable = place_id !== undefined && can_place(
|
||||
EverythingRegistry.get<BlockRegistry>("blocks", place_id),
|
||||
current === AIR ? undefined : EverythingRegistry.get_by_id<BlockRegistry>("blocks", current),
|
||||
below_id,
|
||||
);
|
||||
if (!block_info.interactive && place_id && placeable) {
|
||||
level.add_block({ ...target, id: place_id });
|
||||
const slot = this.#held_slot();
|
||||
slot.amount = slot.amount! - 1;
|
||||
|
||||
@@ -132,6 +132,8 @@ export interface BlockRegistry {
|
||||
// the blocks it can stand on, like crops on farmland. it can't be placed elsewhere, and breaks when the block
|
||||
// under it becomes something else. anything when not set
|
||||
place_on?: string[];
|
||||
// the blocks it can be placed into, replacing them, like rice into water. air and replaceable blocks when not set
|
||||
place_in?: string[];
|
||||
// custom components from mods and their params, the server runs them
|
||||
components?: Record<string, unknown>;
|
||||
|
||||
|
||||
@@ -141,6 +141,8 @@ export interface ServerAfterEvents {
|
||||
export interface ServerWorld {
|
||||
get_block(x: number, y: number, z: number): Id | undefined; // undefined when the chunk isn't loaded
|
||||
set_block(x: number, y: number, z: number, id: Id): boolean; // runs on_break / on_create, synced to everyone
|
||||
// like breaking it by hand without a tool: on_break, then its drops. false when there was nothing to break
|
||||
break_block(x: number, y: number, z: number): boolean;
|
||||
get_state(x: number, y: number, z: number, name: string): number | undefined;
|
||||
set_state(x: number, y: number, z: number, name: string, value: number): boolean;
|
||||
get_block_data<T>(x: number, y: number, z: number): T | undefined;
|
||||
|
||||
@@ -107,7 +107,9 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
}
|
||||
}
|
||||
if (json.drops) uses(file, json.drops, "item");
|
||||
for (const id of json.place_on ?? []) uses(file, id, "block");
|
||||
for (const id of [...(json.place_on ?? []), ...(json.place_in ?? [])]) {
|
||||
if (id !== "bworld:air") uses(file, id, "block");
|
||||
}
|
||||
}
|
||||
for (const { file, json } of mod.models) {
|
||||
if ((model_owners.get(json.id)?.length ?? 0) > 1 || BUILTIN_MODELS[json.id]) {
|
||||
|
||||
@@ -50,6 +50,7 @@ export interface BlockJson {
|
||||
interactive?: boolean;
|
||||
replaceable?: boolean;
|
||||
place_on?: string[];
|
||||
place_in?: string[];
|
||||
states?: BlockStateDefinition[];
|
||||
components?: Record<string, unknown>;
|
||||
}
|
||||
@@ -124,6 +125,7 @@ export function block_to_json(block: BlockRegistry, has_item: boolean): BlockJso
|
||||
if (block.interactive) json.interactive = true;
|
||||
if (block.replaceable) json.replaceable = true;
|
||||
if (block.place_on) json.place_on = block.place_on;
|
||||
if (block.place_in) json.place_in = block.place_in;
|
||||
if (block.states) json.states = block.states;
|
||||
if (block.components) json.components = block.components;
|
||||
return json;
|
||||
@@ -153,6 +155,7 @@ export function block_from_json(json: BlockJson): { block: BlockRegistry; has_it
|
||||
if (json.interactive) block.interactive = true;
|
||||
if (json.replaceable) block.replaceable = true;
|
||||
if (json.place_on) block.place_on = json.place_on;
|
||||
if (json.place_in) block.place_in = json.place_in;
|
||||
if (json.states) block.states = json.states;
|
||||
if (json.components) block.components = json.components;
|
||||
return { block, has_item: json.item ?? true };
|
||||
@@ -344,11 +347,13 @@ export function validate_block(json: unknown): Problems {
|
||||
}
|
||||
}
|
||||
if (json.drops !== undefined) problems.push(...validate_id(json.drops, "drops"));
|
||||
if (json.place_on !== undefined) {
|
||||
if (!Array.isArray(json.place_on) || json.place_on.length === 0) {
|
||||
problems.push("place_on must be a list of block ids");
|
||||
for (const key of ["place_on", "place_in"]) {
|
||||
const list = json[key];
|
||||
if (list === undefined) continue;
|
||||
if (!Array.isArray(list) || list.length === 0) {
|
||||
problems.push(`${key} must be a list of block ids`);
|
||||
} else {
|
||||
for (const id of json.place_on) problems.push(...validate_id(id, "place_on"));
|
||||
for (const id of list) problems.push(...validate_id(id, key));
|
||||
}
|
||||
}
|
||||
if (json.states !== undefined) {
|
||||
|
||||
@@ -123,6 +123,31 @@ export function can_stand_on(block: BlockRegistry | undefined, below: string): b
|
||||
return !block?.place_on || block.place_on.includes(below);
|
||||
}
|
||||
|
||||
// where a block placed by clicking a face of another goes: into the clicked block when it can be replaced (like water),
|
||||
// like minecraft, otherwise next to it
|
||||
export function placement_target(
|
||||
x: number,
|
||||
y: number,
|
||||
z: number,
|
||||
offset: { x: number; y: number; z: number },
|
||||
clicked: BlockRegistry | undefined,
|
||||
) {
|
||||
return clicked?.replaceable ? { x, y, z } : { x: x + offset.x, y: y + offset.y, z: z + offset.z };
|
||||
}
|
||||
|
||||
// whether a player can place block where current is now, with below under it. current is undefined for air
|
||||
export function can_place(
|
||||
block: BlockRegistry | undefined,
|
||||
current: BlockRegistry | undefined,
|
||||
below: string,
|
||||
): boolean {
|
||||
if (!block) return false;
|
||||
const into = current?.id ?? "bworld:air";
|
||||
const replaceable = !current || current.replaceable === true;
|
||||
const in_ok = block.place_in ? block.place_in.includes(into) : replaceable;
|
||||
return in_ok && can_stand_on(block, below);
|
||||
}
|
||||
|
||||
// numeric so looking chunks up doesnt allocate a string every time, fine for |x|, |z| < 32768
|
||||
export function chunk_key(x: number, z: number) {
|
||||
return (x + 32768) * 65536 + (z + 32768);
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"block": {
|
||||
"id": "bworld:carrots",
|
||||
"textures": {
|
||||
"crop": "bworld:carrot_stage_0"
|
||||
},
|
||||
"model": "engine:crop",
|
||||
"variants": {
|
||||
"age=0": {
|
||||
"textures": {
|
||||
"crop": "bworld:carrot_stage_0"
|
||||
}
|
||||
},
|
||||
"age=1": {
|
||||
"textures": {
|
||||
"crop": "bworld:carrot_stage_1"
|
||||
}
|
||||
},
|
||||
"age=2": {
|
||||
"textures": {
|
||||
"crop": "bworld:carrot_stage_2"
|
||||
}
|
||||
},
|
||||
"age=3": {
|
||||
"textures": {
|
||||
"crop": "bworld:carrot_stage_3"
|
||||
}
|
||||
}
|
||||
},
|
||||
"render_layer": "cutout",
|
||||
"collision": false,
|
||||
"mining": {
|
||||
"toughness": 0
|
||||
},
|
||||
"item": false,
|
||||
"place_on": [
|
||||
"bworld:hoed_dirt"
|
||||
],
|
||||
"states": [
|
||||
{
|
||||
"name": "age",
|
||||
"bits": 2,
|
||||
"default": 0
|
||||
}
|
||||
],
|
||||
"components": {
|
||||
"bworld:crop": {
|
||||
"days": 3,
|
||||
"stages": 4,
|
||||
"fertilizer": "bworld:bone_meal",
|
||||
"drops": {
|
||||
"young": "bworld:carrot",
|
||||
"mature": "bworld:carrot",
|
||||
"bonus": "bworld:carrot"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"block": {
|
||||
"id": "bworld:potatoes",
|
||||
"textures": {
|
||||
"crop": "bworld:potatoes_stage_0"
|
||||
},
|
||||
"model": "engine:crop",
|
||||
"variants": {
|
||||
"age=0": {
|
||||
"textures": {
|
||||
"crop": "bworld:potatoes_stage_0"
|
||||
}
|
||||
},
|
||||
"age=1": {
|
||||
"textures": {
|
||||
"crop": "bworld:potatoes_stage_1"
|
||||
}
|
||||
},
|
||||
"age=2": {
|
||||
"textures": {
|
||||
"crop": "bworld:potatoes_stage_2"
|
||||
}
|
||||
},
|
||||
"age=3": {
|
||||
"textures": {
|
||||
"crop": "bworld:potatoes_stage_3"
|
||||
}
|
||||
}
|
||||
},
|
||||
"render_layer": "cutout",
|
||||
"collision": false,
|
||||
"mining": {
|
||||
"toughness": 0
|
||||
},
|
||||
"item": false,
|
||||
"place_on": [
|
||||
"bworld:hoed_dirt"
|
||||
],
|
||||
"states": [
|
||||
{
|
||||
"name": "age",
|
||||
"bits": 2,
|
||||
"default": 0
|
||||
}
|
||||
],
|
||||
"components": {
|
||||
"bworld:crop": {
|
||||
"days": 6,
|
||||
"stages": 4,
|
||||
"fertilizer": "bworld:bone_meal",
|
||||
"drops": {
|
||||
"young": "bworld:potato",
|
||||
"mature": "bworld:potato",
|
||||
"bonus": "bworld:potato"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,8 +53,10 @@
|
||||
"mining": {
|
||||
"toughness": 0
|
||||
},
|
||||
"drops": "bworld:wheat_seeds",
|
||||
"item": false,
|
||||
"place_on": [
|
||||
"bworld:hoed_dirt"
|
||||
],
|
||||
"states": [
|
||||
{
|
||||
"name": "age",
|
||||
@@ -64,14 +66,15 @@
|
||||
],
|
||||
"components": {
|
||||
"bworld:crop": {
|
||||
"max_age": 7,
|
||||
"produce": "bworld:wheat",
|
||||
"seeds": "bworld:wheat_seeds",
|
||||
"fertilizer": "bworld:bone_meal"
|
||||
"days": 7,
|
||||
"stages": 8,
|
||||
"fertilizer": "bworld:bone_meal",
|
||||
"drops": {
|
||||
"young": "bworld:wheat_seeds",
|
||||
"mature": "bworld:wheat",
|
||||
"bonus": "bworld:wheat_seeds"
|
||||
}
|
||||
}
|
||||
},
|
||||
"place_on": [
|
||||
"bworld:hoed_dirt"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
"food": {
|
||||
"nutrition": 3,
|
||||
"saturation_modifier": 0.6
|
||||
}
|
||||
},
|
||||
"places": "bworld:carrots"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
"food": {
|
||||
"nutrition": 1,
|
||||
"saturation_modifier": 0.3
|
||||
}
|
||||
},
|
||||
"places": "bworld:potatoes"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,13 +283,44 @@ interface WateringCanData {
|
||||
max_water: number;
|
||||
}
|
||||
|
||||
// items as they appear in crop params: one id, or several
|
||||
type Items = string | string[];
|
||||
|
||||
// what a crop drops when broken, like the loot tables of minecraft's and farmer's delight's crops
|
||||
interface CropDrops {
|
||||
// before it's fully grown, like wheat's seeds
|
||||
young?: Items;
|
||||
// fully grown, like wheat's wheat
|
||||
mature?: Items;
|
||||
// fully grown, 0 to 3 more of this, like the extra seeds from wheat (minecraft's binomial with 3 tries at 57%)
|
||||
bonus?: string;
|
||||
}
|
||||
|
||||
// a crop, like a fabric CropBlock: its "age" state is its stage, and it grows one day every morning its farmland was
|
||||
// watered the day before. see "Crops" in MODS.md
|
||||
interface CropParams {
|
||||
max_age: number;
|
||||
// what a fully grown crop drops besides its seeds, and the seeds for the extra ones
|
||||
produce?: string;
|
||||
seeds?: string;
|
||||
// grows it one stage right away, like bone meal
|
||||
// watered mornings from planting to fully grown
|
||||
days: number;
|
||||
// how many stages it shows, ages 0 to stages - 1. its "age" state must fit them
|
||||
stages: number;
|
||||
drops?: CropDrops;
|
||||
// grows back after it's harvested by right clicking, like tomatoes
|
||||
regrow_days?: number;
|
||||
harvest?: { item: string; count: [number, number] };
|
||||
// grows it a day right away, like bone meal
|
||||
fertilizer?: string;
|
||||
// "farmland" (the default) needs watered farmland under it, "always" grows every morning, like rice in water
|
||||
water?: "farmland" | "always";
|
||||
// the last stages grow a second block on top, like rice's panicles. the crop stays at supporting_age meanwhile
|
||||
upper?: { block: string; stages: number; supporting_age: number };
|
||||
// what the spot becomes when it's broken, like water where rice was
|
||||
leaves?: string;
|
||||
}
|
||||
|
||||
// the top of a two block crop. it grows with the crop under it, and breaking it breaks the whole plant
|
||||
interface CropTopParams {
|
||||
stages: number;
|
||||
drops?: CropDrops;
|
||||
}
|
||||
|
||||
function register_farming(ctx: ServerContext) {
|
||||
@@ -335,46 +366,115 @@ function register_farming(ctx: ServerContext) {
|
||||
},
|
||||
});
|
||||
|
||||
const list = (items: Items | undefined) => items === undefined ? [] : typeof items === "string" ? [items] : items;
|
||||
|
||||
const drop = (x: number, y: number, z: number, drops: CropDrops | undefined, mature: boolean) => {
|
||||
for (const id of list(mature ? drops?.mature : drops?.young)) ctx.world.drop_item(x, y, z, { id, count: 1 });
|
||||
if (mature && drops?.bonus) {
|
||||
let count = 0;
|
||||
for (let i = 0; i < 3; i++) if (Math.random() < 0.5714) count++;
|
||||
if (count > 0) ctx.world.drop_item(x, y, z, { id: drops.bonus, count });
|
||||
}
|
||||
};
|
||||
|
||||
// the stage for how many days it has grown: evenly spread, fully grown on the last day
|
||||
const stage_for = (params: CropParams, days: number) => {
|
||||
const stages = params.stages + (params.upper?.stages ?? 0);
|
||||
return Math.min(stages - 1, Math.floor(days * (stages - 1) / params.days));
|
||||
};
|
||||
|
||||
// days grown, kept in the block's data. crops from before this start at the first day of the stage they show
|
||||
const days_of = (block: BlockRef, params: CropParams) => {
|
||||
if (typeof block.data?.days === "number") return block.data.days as number;
|
||||
const age = ctx.world.get_state(block.x, block.y, block.z, "age") ?? 0;
|
||||
let days = 0;
|
||||
while (days < params.days && stage_for(params, days) < age) days++;
|
||||
return days;
|
||||
};
|
||||
|
||||
// shows the stage for days, growing or taking away the top block for two block crops
|
||||
const set_days = (block: BlockRef, params: CropParams, days: number) => {
|
||||
block.data = { ...block.data, days };
|
||||
const stage = stage_for(params, days);
|
||||
const upper = params.upper;
|
||||
const above = ctx.world.get_block(block.x, block.y + 1, block.z);
|
||||
if (!upper || stage < params.stages) {
|
||||
ctx.world.set_state(block.x, block.y, block.z, "age", stage);
|
||||
if (upper && above === upper.block) ctx.world.set_block(block.x, block.y + 1, block.z, "bworld:air");
|
||||
return;
|
||||
}
|
||||
// the top needs room, like farmer's delight's rice
|
||||
if (above !== upper.block && above !== "bworld:air") {
|
||||
ctx.world.set_state(block.x, block.y, block.z, "age", params.stages - 1);
|
||||
return;
|
||||
}
|
||||
ctx.world.set_state(block.x, block.y, block.z, "age", upper.supporting_age);
|
||||
if (above !== upper.block) ctx.world.set_block(block.x, block.y + 1, block.z, upper.block);
|
||||
ctx.world.set_state(block.x, block.y + 1, block.z, "age", stage - params.stages);
|
||||
};
|
||||
|
||||
const is_grown = (block: BlockRef, params: CropParams) => days_of(block, params) >= params.days;
|
||||
|
||||
ctx.components.register_block<CropParams>("bworld:crop", {
|
||||
on_create(block) {
|
||||
// block data so on_new_day runs for it. placed at a later stage (by a script), it has grown that long
|
||||
on_create(block, params) {
|
||||
block.data = {};
|
||||
block.data = { days: days_of(block, params) };
|
||||
},
|
||||
on_new_day(block, params) {
|
||||
const age = ctx.world.get_state(block.x, block.y, block.z, "age") ?? 0;
|
||||
if (age < params.max_age && is_watered(block.x, block.y - 1, block.z)) {
|
||||
ctx.world.set_state(block.x, block.y, block.z, "age", age + 1);
|
||||
}
|
||||
const days = days_of(block, params);
|
||||
if (days >= params.days) return;
|
||||
const watered = params.water === "always" || is_watered(block.x, block.y - 1, block.z);
|
||||
if (watered) set_days(block, params, days + 1);
|
||||
},
|
||||
on_interact(block, params, player) {
|
||||
block.data ??= {};
|
||||
const held = player.held_item;
|
||||
// watering a crop waters the farmland under it
|
||||
if (held?.id === WATERING_CAN) {
|
||||
block.data ??= { days: days_of(block, params) };
|
||||
return water(block.x, block.y - 1, block.z, player);
|
||||
}
|
||||
if (!params.fertilizer || held?.id !== params.fertilizer) {
|
||||
return false;
|
||||
const days = days_of(block, params);
|
||||
if (params.fertilizer && held?.id === params.fertilizer) {
|
||||
// fully grown, keep the fertilizer
|
||||
if (days >= params.days) return false;
|
||||
set_days(block, params, days + 1);
|
||||
held.count -= 1;
|
||||
return true;
|
||||
}
|
||||
const age = ctx.world.get_state(block.x, block.y, block.z, "age") ?? 0;
|
||||
// fully grown, keep the fertilizer
|
||||
if (age >= params.max_age) {
|
||||
return false;
|
||||
// regrowing crops are picked by hand and grow back, like farmer's delight's tomatoes
|
||||
if (params.regrow_days && params.harvest && days >= params.days) {
|
||||
const [min, max] = params.harvest.count;
|
||||
const count = min + Math.floor(Math.random() * (max - min + 1));
|
||||
ctx.world.drop_item(block.x, block.y, block.z, { id: params.harvest.item, count });
|
||||
set_days(block, params, Math.max(0, params.days - params.regrow_days));
|
||||
return true;
|
||||
}
|
||||
ctx.world.set_state(block.x, block.y, block.z, "age", age + 1);
|
||||
held.count -= 1;
|
||||
return true;
|
||||
return false;
|
||||
},
|
||||
// the block's own drops are one seed. fully grown it also drops its produce and 0 to 3 more seeds, like
|
||||
// minecraft's wheat
|
||||
on_break(block, params) {
|
||||
drop(block.x, block.y, block.z, params.drops, is_grown(block, params));
|
||||
if (params.leaves) {
|
||||
const { x, y, z } = block;
|
||||
const leaves = params.leaves;
|
||||
// after it's gone, it's still there while on_break runs
|
||||
ctx.system.run_timeout(() => {
|
||||
if (ctx.world.get_block(x, y, z) === "bworld:air") ctx.world.set_block(x, y, z, leaves);
|
||||
}, 0);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
ctx.components.register_block<CropTopParams>("bworld:crop_top", {
|
||||
on_break(block, params) {
|
||||
const age = ctx.world.get_state(block.x, block.y, block.z, "age") ?? 0;
|
||||
if (age < params.max_age) return;
|
||||
if (params.produce) ctx.world.drop_item(block.x, block.y, block.z, { id: params.produce, count: 1 });
|
||||
let extra = 0;
|
||||
for (let i = 0; i < 3; i++) if (Math.random() < 0.5714) extra++;
|
||||
if (params.seeds && extra > 0) {
|
||||
ctx.world.drop_item(block.x, block.y, block.z, { id: params.seeds, count: extra });
|
||||
}
|
||||
drop(block.x, block.y, block.z, params.drops, age >= params.stages - 1);
|
||||
// the plant under it goes too, once this is gone
|
||||
const { x, y, z } = block;
|
||||
const lower = ctx.world.get_block(x, y - 1, z);
|
||||
ctx.system.run_timeout(() => {
|
||||
if (lower && ctx.world.get_block(x, y - 1, z) === lower) ctx.world.break_block(x, y - 1, z);
|
||||
}, 0);
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
|
After Width: | Height: | Size: 192 B |
|
After Width: | Height: | Size: 202 B |
|
After Width: | Height: | Size: 212 B |
|
After Width: | Height: | Size: 240 B |
|
After Width: | Height: | Size: 134 B |
|
After Width: | Height: | Size: 150 B |
|
After Width: | Height: | Size: 146 B |
|
After Width: | Height: | Size: 185 B |
@@ -0,0 +1,28 @@
|
||||
# Farmer's Delight
|
||||
|
||||
A bworld port of [Farmer's Delight](https://github.com/vectorwing/FarmersDelight) by vectorwing. Its textures and game
|
||||
design come from the original, version 1.3.4 for NeoForge 1.21.1, under the MIT license:
|
||||
|
||||
```
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2020 vectorwing
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
```
|
||||
@@ -0,0 +1,80 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"block": {
|
||||
"id": "farmersdelight:cabbages",
|
||||
"textures": {
|
||||
"cross": "farmersdelight:cabbages_stage0"
|
||||
},
|
||||
"model": "engine:cross",
|
||||
"variants": {
|
||||
"age=0": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:cabbages_stage0"
|
||||
}
|
||||
},
|
||||
"age=1": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:cabbages_stage1"
|
||||
}
|
||||
},
|
||||
"age=2": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:cabbages_stage2"
|
||||
}
|
||||
},
|
||||
"age=3": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:cabbages_stage3"
|
||||
}
|
||||
},
|
||||
"age=4": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:cabbages_stage4"
|
||||
}
|
||||
},
|
||||
"age=5": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:cabbages_stage5"
|
||||
}
|
||||
},
|
||||
"age=6": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:cabbages_stage6"
|
||||
}
|
||||
},
|
||||
"age=7": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:cabbages_stage7"
|
||||
}
|
||||
}
|
||||
},
|
||||
"render_layer": "cutout",
|
||||
"collision": false,
|
||||
"mining": {
|
||||
"toughness": 0
|
||||
},
|
||||
"item": false,
|
||||
"place_on": [
|
||||
"bworld:hoed_dirt"
|
||||
],
|
||||
"states": [
|
||||
{
|
||||
"name": "age",
|
||||
"bits": 3,
|
||||
"default": 0
|
||||
}
|
||||
],
|
||||
"components": {
|
||||
"bworld:crop": {
|
||||
"days": 12,
|
||||
"stages": 8,
|
||||
"fertilizer": "bworld:bone_meal",
|
||||
"drops": {
|
||||
"young": "farmersdelight:cabbage_seeds",
|
||||
"mature": "farmersdelight:cabbage",
|
||||
"bonus": "farmersdelight:cabbage_seeds"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"block": {
|
||||
"id": "farmersdelight:onions",
|
||||
"textures": {
|
||||
"crop": "farmersdelight:onions_stage0"
|
||||
},
|
||||
"model": "engine:crop",
|
||||
"variants": {
|
||||
"age=0": {
|
||||
"textures": {
|
||||
"crop": "farmersdelight:onions_stage0"
|
||||
}
|
||||
},
|
||||
"age=1": {
|
||||
"textures": {
|
||||
"crop": "farmersdelight:onions_stage1"
|
||||
}
|
||||
},
|
||||
"age=2": {
|
||||
"textures": {
|
||||
"crop": "farmersdelight:onions_stage2"
|
||||
}
|
||||
},
|
||||
"age=3": {
|
||||
"textures": {
|
||||
"crop": "farmersdelight:onions_stage3"
|
||||
}
|
||||
}
|
||||
},
|
||||
"render_layer": "cutout",
|
||||
"collision": false,
|
||||
"mining": {
|
||||
"toughness": 0
|
||||
},
|
||||
"item": false,
|
||||
"place_on": [
|
||||
"bworld:hoed_dirt"
|
||||
],
|
||||
"states": [
|
||||
{
|
||||
"name": "age",
|
||||
"bits": 2,
|
||||
"default": 0
|
||||
}
|
||||
],
|
||||
"components": {
|
||||
"bworld:crop": {
|
||||
"days": 4,
|
||||
"stages": 4,
|
||||
"fertilizer": "bworld:bone_meal",
|
||||
"drops": {
|
||||
"young": "farmersdelight:onion",
|
||||
"mature": "farmersdelight:onion",
|
||||
"bonus": "farmersdelight:onion"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"block": {
|
||||
"id": "farmersdelight:rice",
|
||||
"textures": {
|
||||
"cross": "farmersdelight:rice_stage0"
|
||||
},
|
||||
"model": "engine:cross",
|
||||
"variants": {
|
||||
"age=0": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:rice_stage0"
|
||||
}
|
||||
},
|
||||
"age=1": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:rice_stage1"
|
||||
}
|
||||
},
|
||||
"age=2": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:rice_stage2"
|
||||
}
|
||||
},
|
||||
"age=3": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:rice_stage3"
|
||||
}
|
||||
},
|
||||
"age=4": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:rice_supporting"
|
||||
}
|
||||
}
|
||||
},
|
||||
"render_layer": "cutout",
|
||||
"collision": false,
|
||||
"mining": {
|
||||
"toughness": 0
|
||||
},
|
||||
"item": false,
|
||||
"place_on": [
|
||||
"bworld:dirt",
|
||||
"bworld:grass",
|
||||
"bworld:hoed_dirt"
|
||||
],
|
||||
"states": [
|
||||
{
|
||||
"name": "age",
|
||||
"bits": 3,
|
||||
"default": 0
|
||||
}
|
||||
],
|
||||
"place_in": [
|
||||
"bworld:water"
|
||||
],
|
||||
"components": {
|
||||
"bworld:crop": {
|
||||
"days": 6,
|
||||
"stages": 4,
|
||||
"fertilizer": "bworld:bone_meal",
|
||||
"water": "always",
|
||||
"upper": {
|
||||
"block": "farmersdelight:rice_panicles",
|
||||
"stages": 4,
|
||||
"supporting_age": 4
|
||||
},
|
||||
"leaves": "bworld:water",
|
||||
"drops": {
|
||||
"young": "farmersdelight:rice",
|
||||
"mature": "farmersdelight:rice"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"block": {
|
||||
"id": "farmersdelight:rice_panicles",
|
||||
"textures": {
|
||||
"cross": "farmersdelight:rice_panicles_stage0"
|
||||
},
|
||||
"model": "engine:cross",
|
||||
"variants": {
|
||||
"age=0": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:rice_panicles_stage0"
|
||||
}
|
||||
},
|
||||
"age=1": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:rice_panicles_stage1"
|
||||
}
|
||||
},
|
||||
"age=2": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:rice_panicles_stage2"
|
||||
}
|
||||
},
|
||||
"age=3": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:rice_panicles_stage3"
|
||||
}
|
||||
}
|
||||
},
|
||||
"render_layer": "cutout",
|
||||
"collision": false,
|
||||
"mining": {
|
||||
"toughness": 0
|
||||
},
|
||||
"item": false,
|
||||
"place_on": [
|
||||
"farmersdelight:rice"
|
||||
],
|
||||
"states": [
|
||||
{
|
||||
"name": "age",
|
||||
"bits": 2,
|
||||
"default": 0
|
||||
}
|
||||
],
|
||||
"components": {
|
||||
"bworld:crop_top": {
|
||||
"stages": 4,
|
||||
"drops": {
|
||||
"mature": "farmersdelight:rice_panicle"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"block": {
|
||||
"id": "farmersdelight:tomatoes",
|
||||
"textures": {
|
||||
"cross": "farmersdelight:budding_tomatoes_stage0"
|
||||
},
|
||||
"model": "engine:cross",
|
||||
"variants": {
|
||||
"age=0": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:budding_tomatoes_stage0"
|
||||
}
|
||||
},
|
||||
"age=1": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:budding_tomatoes_stage1"
|
||||
}
|
||||
},
|
||||
"age=2": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:budding_tomatoes_stage2"
|
||||
}
|
||||
},
|
||||
"age=3": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:budding_tomatoes_stage3"
|
||||
}
|
||||
},
|
||||
"age=4": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:tomatoes_stage0"
|
||||
}
|
||||
},
|
||||
"age=5": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:tomatoes_stage1"
|
||||
}
|
||||
},
|
||||
"age=6": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:tomatoes_stage2"
|
||||
}
|
||||
},
|
||||
"age=7": {
|
||||
"textures": {
|
||||
"cross": "farmersdelight:tomatoes_stage3"
|
||||
}
|
||||
}
|
||||
},
|
||||
"render_layer": "cutout",
|
||||
"collision": false,
|
||||
"mining": {
|
||||
"toughness": 0
|
||||
},
|
||||
"item": false,
|
||||
"place_on": [
|
||||
"bworld:hoed_dirt"
|
||||
],
|
||||
"states": [
|
||||
{
|
||||
"name": "age",
|
||||
"bits": 3,
|
||||
"default": 0
|
||||
}
|
||||
],
|
||||
"components": {
|
||||
"bworld:crop": {
|
||||
"days": 11,
|
||||
"stages": 8,
|
||||
"fertilizer": "bworld:bone_meal",
|
||||
"regrow_days": 4,
|
||||
"harvest": {
|
||||
"item": "farmersdelight:tomato",
|
||||
"count": [
|
||||
1,
|
||||
2
|
||||
]
|
||||
},
|
||||
"drops": {
|
||||
"young": "farmersdelight:tomato_seeds",
|
||||
"mature": [
|
||||
"farmersdelight:tomato",
|
||||
"farmersdelight:tomato_seeds"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"item": {
|
||||
"id": "farmersdelight:cabbage",
|
||||
"texture": "farmersdelight:cabbage",
|
||||
"food": {
|
||||
"nutrition": 2,
|
||||
"saturation_modifier": 0.4
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"item": {
|
||||
"id": "farmersdelight:cabbage_seeds",
|
||||
"texture": "farmersdelight:cabbage_seeds",
|
||||
"places": "farmersdelight:cabbages"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"item": {
|
||||
"id": "farmersdelight:onion",
|
||||
"texture": "farmersdelight:onion",
|
||||
"places": "farmersdelight:onions",
|
||||
"food": {
|
||||
"nutrition": 2,
|
||||
"saturation_modifier": 0.4
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"item": {
|
||||
"id": "farmersdelight:rice",
|
||||
"texture": "farmersdelight:rice",
|
||||
"places": "farmersdelight:rice"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"item": {
|
||||
"id": "farmersdelight:rice_panicle",
|
||||
"texture": "farmersdelight:rice_panicle"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"item": {
|
||||
"id": "farmersdelight:tomato",
|
||||
"texture": "farmersdelight:tomato",
|
||||
"food": {
|
||||
"nutrition": 1,
|
||||
"saturation_modifier": 0.3
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"item": {
|
||||
"id": "farmersdelight:tomato_seeds",
|
||||
"texture": "farmersdelight:tomato_seeds",
|
||||
"places": "farmersdelight:tomatoes"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"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.",
|
||||
"version": "0.1.0",
|
||||
"authors": [
|
||||
"vectorwing (original)",
|
||||
"paula (port)"
|
||||
],
|
||||
"game_version": ">=0.1.0",
|
||||
"dependencies": [
|
||||
{
|
||||
"id": "bworld",
|
||||
"version": "*"
|
||||
}
|
||||
],
|
||||
"credits": "CREDITS.md"
|
||||
}
|
||||
|
After Width: | Height: | Size: 151 B |
|
After Width: | Height: | Size: 207 B |
|
After Width: | Height: | Size: 290 B |
|
After Width: | Height: | Size: 342 B |
|
After Width: | Height: | Size: 417 B |
|
After Width: | Height: | Size: 236 B |
|
After Width: | Height: | Size: 123 B |
|
After Width: | Height: | Size: 152 B |
|
After Width: | Height: | Size: 157 B |
|
After Width: | Height: | Size: 226 B |
|
After Width: | Height: | Size: 286 B |
|
After Width: | Height: | Size: 297 B |
|
After Width: | Height: | Size: 318 B |
|
After Width: | Height: | Size: 399 B |
|
After Width: | Height: | Size: 383 B |
|
After Width: | Height: | Size: 125 B |
|
After Width: | Height: | Size: 168 B |
|
After Width: | Height: | Size: 235 B |
|
After Width: | Height: | Size: 325 B |
|
After Width: | Height: | Size: 411 B |
|
After Width: | Height: | Size: 394 B |
|
After Width: | Height: | Size: 174 B |
|
After Width: | Height: | Size: 296 B |
|
After Width: | Height: | Size: 433 B |
|
After Width: | Height: | Size: 568 B |
|
After Width: | Height: | Size: 123 B |
|
After Width: | Height: | Size: 200 B |
|
After Width: | Height: | Size: 324 B |
|
After Width: | Height: | Size: 362 B |
|
After Width: | Height: | Size: 473 B |
|
After Width: | Height: | Size: 361 B |
|
After Width: | Height: | Size: 288 B |
|
After Width: | Height: | Size: 395 B |
|
After Width: | Height: | Size: 448 B |
|
After Width: | Height: | Size: 443 B |
|
After Width: | Height: | Size: 509 B |
@@ -27,7 +27,7 @@ import {
|
||||
import type { ModListing, RecipeBook } from "$/common/mod_loader.ts";
|
||||
import type { WorldgenSetup } from "$/common/generation.ts";
|
||||
import { CYCLE_TICKS, format_clock, time_of_day, TIMES_OF_DAY } from "$/common/time.ts";
|
||||
import { can_stand_on } from "$/common/utils.ts";
|
||||
import { can_place, can_stand_on, placement_target } from "$/common/utils.ts";
|
||||
import { EXHAUSTION, MAX_HEALTH } from "./vitals.ts";
|
||||
import { ModRuntime, run_guarded } from "./mod_runtime.ts";
|
||||
import type { GameLoop } from "./game_loop.ts";
|
||||
@@ -705,15 +705,17 @@ export class GameServer {
|
||||
}
|
||||
}
|
||||
|
||||
// the held item's on_use handlers, returns whether it has any
|
||||
// right click at nothing: eating, or the item's on_use
|
||||
#use_item(player: ServerPlayer): boolean {
|
||||
return this.#eat(player) || this.#use_item_components(player);
|
||||
}
|
||||
|
||||
// the held item's on_use handlers, returns whether it has any
|
||||
#use_item_components(player: ServerPlayer): boolean {
|
||||
const held = player.held_item;
|
||||
if (!held) {
|
||||
return false;
|
||||
}
|
||||
if (this.#eat(player)) {
|
||||
return true;
|
||||
}
|
||||
const components = this.mods.item_components_of(held.type_id).filter(({ component }) => component.on_use);
|
||||
if (components.length === 0) {
|
||||
return false;
|
||||
@@ -727,10 +729,10 @@ export class GameServer {
|
||||
}
|
||||
|
||||
#use_block(player: ServerPlayer, x: number, y: number, z: number, face: Faces) {
|
||||
const offset = FACE_OFFSETS[face];
|
||||
const [tx, ty, tz] = [x + offset.x, y + offset.y, z + offset.z];
|
||||
|
||||
const info = this.world.get_block_info(x, y, z);
|
||||
const target = placement_target(x, y, z, FACE_OFFSETS[face], info);
|
||||
const [tx, ty, tz] = [target.x, target.y, target.z];
|
||||
|
||||
if (!info || !this.#in_reach(player, x, y, z)) {
|
||||
this.#correct(player, x, y, z);
|
||||
this.#correct(player, tx, ty, tz);
|
||||
@@ -763,21 +765,23 @@ export class GameServer {
|
||||
}
|
||||
|
||||
// an item that does something when used does that instead of being placed
|
||||
if (this.#use_item(player)) {
|
||||
if (this.#use_item_components(player)) {
|
||||
this.#correct(player, tx, ty, tz);
|
||||
return;
|
||||
}
|
||||
|
||||
// place the held block against the face
|
||||
// place the held block against the face, or into the clicked block if it's replaceable. food that can be planted,
|
||||
// like a potato on farmland, is planted, and eaten when it can't be
|
||||
const held_slot = player.inventory.get_slot(player.selected_slot);
|
||||
const held = EverythingRegistry.get<ItemRegistry>("items", held_slot.type_id ?? "");
|
||||
if (
|
||||
!held?.block_id || ty < 0 || ty >= CHUNK_HEIGHT || !this.#replaceable(tx, ty, tz) ||
|
||||
!can_stand_on(
|
||||
const placeable = held?.block_id !== undefined && ty >= 0 && ty < CHUNK_HEIGHT &&
|
||||
can_place(
|
||||
EverythingRegistry.get<BlockRegistry>("blocks", held.block_id),
|
||||
this.world.get_block_info(tx, ty, tz),
|
||||
this.world.get_block_id(tx, ty - 1, tz),
|
||||
)
|
||||
) {
|
||||
);
|
||||
if (!placeable || !held?.block_id) {
|
||||
this.#eat(player);
|
||||
this.#correct(player, tx, ty, tz);
|
||||
return;
|
||||
}
|
||||
@@ -1282,11 +1286,6 @@ export class GameServer {
|
||||
this.#send(player, { type: "set_block", x, y, z, id: this.world.get_block_id(x, y, z) });
|
||||
}
|
||||
|
||||
#replaceable(x: number, y: number, z: number) {
|
||||
const nid = this.world.get_block_nid(x, y, z);
|
||||
return nid === AIR || (EverythingRegistry.get_by_id<BlockRegistry>("blocks", nid)?.replaceable ?? false);
|
||||
}
|
||||
|
||||
#in_reach(player: ServerPlayer, x: number, y: number, z: number) {
|
||||
const dx = x + 0.5 - player.x;
|
||||
const dy = y + 0.5 - (player.y + PLAYER_EYE_HEIGHT);
|
||||
|
||||
@@ -345,6 +345,11 @@ export class ModRuntime {
|
||||
game().set_block(x, y, z, id);
|
||||
return true;
|
||||
},
|
||||
break_block: (x, y, z) => {
|
||||
if (game().world.get_block_id(x, y, z) === AIR_ID) return false;
|
||||
game().destroy_block(x, y, z);
|
||||
return true;
|
||||
},
|
||||
get_state: (x, y, z, name) => {
|
||||
const id = game().world.get_block_id(x, y, z);
|
||||
if (id === AIR_ID) return undefined;
|
||||
|
||||
@@ -41,7 +41,7 @@ Deno.test("packing a mod keeps its data, scripts, textures and credits, and give
|
||||
assert(bmod.credits);
|
||||
|
||||
const bworld = read_bmod(Deno.readFileSync(`${folder}/bworld.bmod`), "bworld.bmod");
|
||||
assertEquals(bworld.data.blocks.length, 21);
|
||||
assertEquals(bworld.data.blocks.length, 23);
|
||||
assertEquals(Object.keys(bworld.scripts), ["server"]);
|
||||
|
||||
Deno.removeSync(sources, { recursive: true });
|
||||
|
||||
@@ -141,3 +141,102 @@ Deno.test("crops grow far from every player, and mornings skipped by /time still
|
||||
// the world's day is the same one scripts see
|
||||
assertEquals(t.game.world.day, 4);
|
||||
});
|
||||
|
||||
// plants a crop on farmland at x, 100, 0 and waters it every day for days mornings, returning its age each morning
|
||||
function grow(t: Awaited<ReturnType<typeof setup>>, x: number, crop: string, days: number) {
|
||||
t.game.set_block(x, 100, 0, "bworld:hoed_dirt");
|
||||
t.game.set_block(x, 101, 0, crop);
|
||||
const ages: number[] = [];
|
||||
for (let day = 0; day < days; day++) {
|
||||
t.game.set_block_state(x, 100, 0, 1);
|
||||
t.next_morning();
|
||||
ages.push(t.state(x, 101, 0, "age")!);
|
||||
}
|
||||
return ages;
|
||||
}
|
||||
|
||||
Deno.test("each crop takes its own number of watered days to grow", async () => {
|
||||
const t = await setup();
|
||||
// the stage for each morning, spread over its days, fully grown on the last one
|
||||
assertEquals(grow(t, 0, "bworld:carrots", 3), [1, 2, 3]);
|
||||
assertEquals(grow(t, 1, "bworld:potatoes", 6), [0, 1, 1, 2, 2, 3]);
|
||||
assertEquals(grow(t, 2, "bworld:wheat", 7), [1, 2, 3, 4, 5, 6, 7]);
|
||||
assertEquals(grow(t, -1, "farmersdelight:onions", 4), [0, 1, 2, 3]);
|
||||
const cabbage = grow(t, -2, "farmersdelight:cabbages", 12);
|
||||
assertEquals(cabbage.at(-2), 6, "not grown a day early");
|
||||
assertEquals(cabbage.at(-1), 7);
|
||||
|
||||
// grown crops stay grown
|
||||
t.game.set_block_state(0, 100, 0, 1);
|
||||
t.next_morning();
|
||||
assertEquals(t.state(0, 101, 0, "age"), 3);
|
||||
});
|
||||
|
||||
Deno.test("potatoes and carrots are planted on farmland, and eaten anywhere else", async () => {
|
||||
const t = await setup(["bworld:potato 3"]);
|
||||
t.game.set_block(1, 100, 0, "bworld:hoed_dirt");
|
||||
t.game.players()[0].vitals.food = 10;
|
||||
t.hold("bworld:potato");
|
||||
t.use(1, 100, 0);
|
||||
assertEquals(t.game.world.get_block_id(1, 101, 0), "bworld:potatoes");
|
||||
assertEquals(t.game.players()[0].vitals.food, 10, "planted, not eaten");
|
||||
// on stone it can't be planted, so it's eaten
|
||||
t.use(0, 99, 1);
|
||||
assertEquals(t.game.world.get_block_id(0, 100, 1), "bworld:air");
|
||||
assertEquals(t.game.players()[0].vitals.food, 11);
|
||||
});
|
||||
|
||||
Deno.test("tomatoes take 11 days, are picked by hand, and grow back in 4", async () => {
|
||||
const t = await setup();
|
||||
const ages = grow(t, 1, "farmersdelight:tomatoes", 11);
|
||||
assertEquals(ages.at(-1), 7);
|
||||
assertEquals(ages.at(-2), 6);
|
||||
|
||||
t.use(1, 101, 0);
|
||||
const picked = t.game.item_entities().map((e) => e.item.to_data());
|
||||
assertEquals(picked.length, 1);
|
||||
assertEquals(picked[0].id, "farmersdelight:tomato");
|
||||
assert(picked[0].count >= 1 && picked[0].count <= 2);
|
||||
assertEquals(t.game.world.get_block_id(1, 101, 0), "farmersdelight:tomatoes", "the plant stays");
|
||||
assertEquals(t.state(1, 101, 0, "age"), 4, "back to a young vine");
|
||||
|
||||
// 3 watered mornings isn't enough, the 4th is
|
||||
for (let day = 0; day < 4; day++) {
|
||||
t.game.set_block_state(1, 100, 0, 1);
|
||||
t.next_morning();
|
||||
assertEquals(t.state(1, 101, 0, "age"), day < 3 ? Math.floor((7 + day + 1) * 7 / 11) : 7);
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("rice is planted in water, grows panicles on top without watering, and leaves its water when harvested", async () => {
|
||||
const t = await setup(["farmersdelight:rice 2"]);
|
||||
t.game.set_block(1, 99, 0, "bworld:dirt");
|
||||
t.game.set_block(1, 100, 0, "bworld:water");
|
||||
t.game.set_block(2, 100, 0, "bworld:hoed_dirt");
|
||||
|
||||
// not on land
|
||||
t.hold("farmersdelight:rice");
|
||||
t.use(2, 100, 0);
|
||||
assertEquals(t.game.world.get_block_id(2, 101, 0), "bworld:air");
|
||||
// into the water, standing on the dirt under it
|
||||
t.use(1, 100, 0);
|
||||
assertEquals(t.game.world.get_block_id(1, 100, 0), "farmersdelight:rice");
|
||||
|
||||
for (let day = 0; day < 3; day++) t.next_morning();
|
||||
assertEquals(t.state(1, 100, 0, "age"), 3);
|
||||
assertEquals(t.game.world.get_block_id(1, 101, 0), "bworld:air");
|
||||
for (let day = 0; day < 3; day++) t.next_morning();
|
||||
assertEquals(t.game.world.get_block_id(1, 101, 0), "farmersdelight:rice_panicles");
|
||||
assertEquals(t.state(1, 101, 0, "age"), 3, "fully grown after 6 days");
|
||||
assertEquals(t.state(1, 100, 0, "age"), 4, "the stalk holding them up");
|
||||
|
||||
// breaking the panicles harvests the whole plant
|
||||
t.send(1, { type: "break_block", x: 1, y: 101, z: 0 });
|
||||
// the stalk goes the next tick, and its water comes back the one after
|
||||
t.game.tick();
|
||||
t.game.tick();
|
||||
const dropped = t.game.item_entities().map((e) => e.item.type_id).sort();
|
||||
assertEquals(dropped, ["farmersdelight:rice", "farmersdelight:rice_panicle"]);
|
||||
assertEquals(t.game.world.get_block_id(1, 100, 0), "bworld:water");
|
||||
assertEquals(t.game.world.get_block_id(1, 101, 0), "bworld:air");
|
||||
});
|
||||
|
||||
@@ -29,7 +29,7 @@ Deno.test("welcome lists what to download, and nothing happens until ready", asy
|
||||
send(2, { type: "hello", name: "bob", protocol: PROTOCOL_VERSION });
|
||||
const [welcome] = take(2);
|
||||
assertEquals(welcome.type, "welcome");
|
||||
assertEquals(welcome.mods.map((m: { id: string }) => m.id), ["bworld"]);
|
||||
assertEquals(welcome.mods.map((m: { id: string }) => m.id), ["bworld", "farmersdelight"]);
|
||||
assert(welcome.mods.every((m: { sha256: string; file: string }) => m.sha256 && m.file));
|
||||
assert(!("players" in welcome) && !("changes" in welcome), "welcome shouldn't have world state");
|
||||
|
||||
|
||||
@@ -23,9 +23,10 @@ const inventory_of = (messages: Msg[]) =>
|
||||
|
||||
Deno.test("the base game loads from mods/bworld", async () => {
|
||||
const { game } = await test_game("mods");
|
||||
assertEquals(EverythingRegistry.entries("blocks").length, 21);
|
||||
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(EverythingRegistry.entries("items").length, 35);
|
||||
assertEquals(bworld(EverythingRegistry.entries("items")), 35);
|
||||
assertEquals(game.recipes.shaped.length, 5);
|
||||
assertEquals(game.recipes.furnace.size, 6);
|
||||
assertEquals(game.recipes.fuel.size, 2);
|
||||
@@ -77,6 +78,8 @@ Deno.test("a mod made from the template loads and runs", async () => {
|
||||
send(1, { type: "select_slot", slot: inventory.findIndex((i: Msg) => i?.id === "copper_tools:example_block") });
|
||||
let y = CHUNK_HEIGHT - 1;
|
||||
while (game.world.get_block_nid(1, y, 1) === AIR) y--;
|
||||
// solid ground to build on: placing into water would replace it
|
||||
game.set_block(1, y, 1, "bworld:stone");
|
||||
send(1, { type: "move", x: 2.5, y: y + 1, z: 2.5, yaw: 0, pitch: 0 });
|
||||
send(1, { type: "use_block", x: 1, y, z: 1, face: "top" });
|
||||
assertEquals(game.world.get_block_id(1, y + 1, 1), "copper_tools:example_block");
|
||||
|
||||