Improve crops system
This commit is contained in:
@@ -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_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. |
|
||||
|
||||
@@ -607,11 +608,14 @@ ctx.components.register_block("copper_tools:oxidizes", {
|
||||
| `on_interact(block, params, player)` | A player right clicks it. | `true` if handled, so no block gets placed |
|
||||
| `on_tick(block, params, dt)` | Every tick (20 per second) near a player. | ignored |
|
||||
| `on_second(block, params, dt)` | Every second near a player. | ignored |
|
||||
| `on_new_day(block, params, day)` | Every morning at 06:00, anywhere in the world. | ignored |
|
||||
|
||||
`block` is `{ id, x, y, z, data }`. `data` is tile data: any JSON value, `undefined` until a handler sets it, saved with
|
||||
the world and **never sent to clients**. To show tile data to a player, open a screen with it (see [GUIs](#guis)).
|
||||
`on_tick` and `on_second` only run for blocks that have tile data, so a component that ticks from the start sets
|
||||
`block.data` in `on_create`. They run within 6 chunks of a player. `dt` is in seconds: 0.05 for `on_tick`, 1 for
|
||||
`on_tick`, `on_second` and `on_new_day` only run for blocks that have tile data, so a component that ticks from the
|
||||
start sets `block.data` in `on_create`. `on_new_day` runs for every such block in the world, near a player or not, so
|
||||
things like crops keep growing while nobody's around (see the base game's farming in `mods/bworld/scripts/server.ts`).
|
||||
When time jumps forward past several mornings (`/time add`), each one runs, up to 64. They run within 6 chunks of a player. `dt` is in seconds: 0.05 for `on_tick`, 1 for
|
||||
`on_second`.
|
||||
|
||||
The server runs a fixed 20 ticks per second. When a tick takes too long, the next ones run back to back until it has
|
||||
@@ -645,6 +649,7 @@ events** fire once it has happened.
|
||||
| `player_leave` | | ✓ | `player` |
|
||||
| `server_start` | | ✓ | none |
|
||||
| `tick` | | ✓ | `dt` in seconds, 20 times per second |
|
||||
| `new_day` | | ✓ | `day`, at 06:00 after every block's `on_new_day` |
|
||||
|
||||
These fire for **every** player's actions, because they all go through the server. When a before event cancels a break
|
||||
or place, the server tells that player's client to undo what it already showed.
|
||||
@@ -662,6 +667,7 @@ interface ServerWorld {
|
||||
is_loaded(x: number, z: number): boolean;
|
||||
readonly seed: string;
|
||||
readonly time: number; // world time, see below
|
||||
readonly day: number; // days since the world started, going up at 06:00, 0 on the first day
|
||||
set_time(time: number): void; // synced to every player
|
||||
}
|
||||
|
||||
|
||||
+6
-2
@@ -3,7 +3,8 @@ import { AIR, FACE_OFFSETS, TICK_DELTA } from "$/common/constants.ts";
|
||||
import type { Client } from "./client.ts";
|
||||
import type { BlockHitResult } from "./level/client_level.ts";
|
||||
import type { Container } from "$/common/inventory.ts";
|
||||
import type { ContainerKey } from "$/common/protocol.ts";
|
||||
import { AIR_ID, type ContainerKey } from "$/common/protocol.ts";
|
||||
import { can_stand_on } 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
|
||||
@@ -63,7 +64,10 @@ export class MultiPlayerGameMode {
|
||||
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;
|
||||
if (!block_info.interactive && place_id && replaceable) {
|
||||
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) {
|
||||
level.add_block({ ...target, id: place_id });
|
||||
const slot = this.#held_slot();
|
||||
slot.amount = slot.amount! - 1;
|
||||
|
||||
@@ -129,6 +129,9 @@ export interface BlockRegistry {
|
||||
interactive?: boolean;
|
||||
// placing a block into it replaces it, like water
|
||||
replaceable?: boolean;
|
||||
// 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[];
|
||||
// custom components from mods and their params, the server runs them
|
||||
components?: Record<string, unknown>;
|
||||
|
||||
|
||||
@@ -42,6 +42,8 @@ export interface BlockComponent<P = any> {
|
||||
on_interact?(block: BlockRef, params: P, player: Player): boolean;
|
||||
on_tick?(block: BlockRef, params: P, dt: number): void;
|
||||
on_second?(block: BlockRef, params: P, dt: number): void;
|
||||
// every morning at 06:00, for every block with data in the world, near a player or not. day is ServerWorld.day
|
||||
on_new_day?(block: BlockRef, params: P, day: number): void;
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
@@ -114,6 +116,8 @@ export interface ServerAfterEvents {
|
||||
player_leave: EventSignal<{ readonly player: Player }>;
|
||||
server_start: EventSignal<Record<never, never>>;
|
||||
tick: EventSignal<{ readonly dt: number }>;
|
||||
// 06:00, after every block's on_new_day
|
||||
new_day: EventSignal<{ readonly day: number }>;
|
||||
}
|
||||
|
||||
// world and players
|
||||
@@ -130,6 +134,8 @@ export interface ServerWorld {
|
||||
readonly seed: string;
|
||||
// ticks since 06:00 of day 1. a day is 36000 ticks (06:00 to 18:00), a night 12000
|
||||
readonly time: number;
|
||||
// days since the world started, going up at 06:00 each morning: 0 on the first day
|
||||
readonly day: number;
|
||||
set_time(time: number): void;
|
||||
}
|
||||
|
||||
|
||||
@@ -107,6 +107,7 @@ 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 { file, json } of mod.models) {
|
||||
if ((model_owners.get(json.id)?.length ?? 0) > 1 || BUILTIN_MODELS[json.id]) {
|
||||
|
||||
@@ -48,6 +48,7 @@ export interface BlockJson {
|
||||
item?: boolean;
|
||||
interactive?: boolean;
|
||||
replaceable?: boolean;
|
||||
place_on?: string[];
|
||||
states?: BlockStateDefinition[];
|
||||
components?: Record<string, unknown>;
|
||||
}
|
||||
@@ -120,6 +121,7 @@ export function block_to_json(block: BlockRegistry, has_item: boolean): BlockJso
|
||||
if (!has_item) json.item = false;
|
||||
if (block.interactive) json.interactive = true;
|
||||
if (block.replaceable) json.replaceable = true;
|
||||
if (block.place_on) json.place_on = block.place_on;
|
||||
if (block.states) json.states = block.states;
|
||||
if (block.components) json.components = block.components;
|
||||
return json;
|
||||
@@ -148,6 +150,7 @@ export function block_from_json(json: BlockJson): { block: BlockRegistry; has_it
|
||||
if (json.drops !== undefined) block.drop_table = json.drops;
|
||||
if (json.interactive) block.interactive = true;
|
||||
if (json.replaceable) block.replaceable = true;
|
||||
if (json.place_on) block.place_on = json.place_on;
|
||||
if (json.states) block.states = json.states;
|
||||
if (json.components) block.components = json.components;
|
||||
return { block, has_item: json.item ?? true };
|
||||
@@ -337,6 +340,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");
|
||||
} else {
|
||||
for (const id of json.place_on) problems.push(...validate_id(id, "place_on"));
|
||||
}
|
||||
}
|
||||
if (json.states !== undefined) {
|
||||
const states = json.states;
|
||||
if (!Array.isArray(states)) {
|
||||
|
||||
@@ -118,6 +118,11 @@ function compile_block_states(block_info: BlockRegistry) {
|
||||
});
|
||||
}
|
||||
|
||||
// whether a block can be at a spot with below under it, see place_on
|
||||
export function can_stand_on(block: BlockRegistry | undefined, below: string): boolean {
|
||||
return !block?.place_on || block.place_on.includes(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);
|
||||
|
||||
@@ -18,6 +18,18 @@
|
||||
"bits": 1,
|
||||
"default": 0
|
||||
}
|
||||
]
|
||||
],
|
||||
"variants": {
|
||||
"watered=1": {
|
||||
"textures": {
|
||||
"side": "bworld:dirt",
|
||||
"top": "bworld:hoed_watered_dirt",
|
||||
"bottom": "bworld:dirt"
|
||||
}
|
||||
}
|
||||
},
|
||||
"components": {
|
||||
"bworld:farmland": {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,9 @@
|
||||
"alpha": 0.8,
|
||||
"collision": false,
|
||||
"item": false,
|
||||
"replaceable": true
|
||||
"replaceable": true,
|
||||
"components": {
|
||||
"bworld:water_source": {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,9 +64,14 @@
|
||||
],
|
||||
"components": {
|
||||
"bworld:crop": {
|
||||
"fertilizer": "bworld:bone_meal",
|
||||
"max_age": 7
|
||||
"max_age": 7,
|
||||
"produce": "bworld:wheat",
|
||||
"seeds": "bworld:wheat_seeds",
|
||||
"fertilizer": "bworld:bone_meal"
|
||||
}
|
||||
}
|
||||
},
|
||||
"place_on": [
|
||||
"bworld:hoed_dirt"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
+105
-14
@@ -1,13 +1,12 @@
|
||||
// the base game's block and item behavior, written against the same api as any other mod
|
||||
import type { BlockRef, Container, ScreenHandle, ServerContext } from "bworld/server";
|
||||
import type { BlockRef, Container, Player, ScreenHandle, ServerContext } from "bworld/server";
|
||||
|
||||
export function setup(ctx: ServerContext) {
|
||||
register_hoeable(ctx);
|
||||
register_storage(ctx);
|
||||
register_furnace(ctx);
|
||||
register_smithing_table(ctx);
|
||||
register_crop(ctx);
|
||||
register_watering_can(ctx);
|
||||
register_farming(ctx);
|
||||
}
|
||||
|
||||
// right clicking with a tool turns the block into another, like a hoe on grass
|
||||
@@ -272,12 +271,88 @@ function register_smithing_table(ctx: ServerContext) {
|
||||
});
|
||||
}
|
||||
|
||||
// grows one stage each time it's right clicked with the fertilizer, like bone meal on wheat
|
||||
function register_crop(ctx: ServerContext) {
|
||||
ctx.components.register_block<{ fertilizer: string; max_age: number }>("bworld:crop", {
|
||||
// farming: java's crop rules on stardew valley's clock. every morning at 06:00 a crop grows one stage if its farmland
|
||||
// was watered the day before, then all farmland dries. crops only stand on farmland (their place_on), and pop off when
|
||||
// it's dug up
|
||||
|
||||
const FARMLAND = "bworld:hoed_dirt";
|
||||
const WATERING_CAN = "bworld:watering_can";
|
||||
|
||||
interface WateringCanData {
|
||||
water: number;
|
||||
max_water: number;
|
||||
}
|
||||
|
||||
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
|
||||
fertilizer?: string;
|
||||
}
|
||||
|
||||
function register_farming(ctx: ServerContext) {
|
||||
const is_watered = (x: number, y: number, z: number) =>
|
||||
ctx.world.get_block(x, y, z) === FARMLAND && ctx.world.get_state(x, y, z, "watered") === 1;
|
||||
|
||||
// with the watering can the player holds. true when the click was about watering, so nothing gets placed
|
||||
const water = (x: number, y: number, z: number, player: Player) => {
|
||||
const can = player.held_item;
|
||||
if (can?.id !== WATERING_CAN || ctx.world.get_block(x, y, z) !== FARMLAND) {
|
||||
return false;
|
||||
}
|
||||
const data = can.data as WateringCanData;
|
||||
if (is_watered(x, y, z)) {
|
||||
return true;
|
||||
}
|
||||
if (data.water <= 0) {
|
||||
player.send_message("The watering can is empty, fill it at some water");
|
||||
return true;
|
||||
}
|
||||
can.data = { ...data, water: data.water - 1 };
|
||||
ctx.world.set_state(x, y, z, "watered", 1);
|
||||
return true;
|
||||
};
|
||||
|
||||
ctx.components.register_block("bworld:farmland", {
|
||||
// block data so on_new_day runs for it
|
||||
on_create(block) {
|
||||
block.data = {};
|
||||
},
|
||||
on_interact(block, _params, player) {
|
||||
block.data ??= {};
|
||||
return water(block.x, block.y, block.z, player);
|
||||
},
|
||||
on_new_day(block) {
|
||||
if (!is_watered(block.x, block.y, block.z)) return;
|
||||
// after every crop's on_new_day this morning, which look at it
|
||||
ctx.system.run_timeout(() => {
|
||||
if (ctx.world.get_block(block.x, block.y, block.z) === FARMLAND) {
|
||||
ctx.world.set_state(block.x, block.y, block.z, "watered", 0);
|
||||
}
|
||||
}, 0);
|
||||
},
|
||||
});
|
||||
|
||||
ctx.components.register_block<CropParams>("bworld:crop", {
|
||||
on_create(block) {
|
||||
block.data = {};
|
||||
},
|
||||
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);
|
||||
}
|
||||
},
|
||||
on_interact(block, params, player) {
|
||||
block.data ??= {};
|
||||
const held = player.held_item;
|
||||
if (held?.id !== params.fertilizer) {
|
||||
// watering a crop waters the farmland under it
|
||||
if (held?.id === WATERING_CAN) {
|
||||
return water(block.x, block.y - 1, block.z, player);
|
||||
}
|
||||
if (!params.fertilizer || held?.id !== params.fertilizer) {
|
||||
return false;
|
||||
}
|
||||
const age = ctx.world.get_state(block.x, block.y, block.z, "age") ?? 0;
|
||||
@@ -289,16 +364,32 @@ function register_crop(ctx: ServerContext) {
|
||||
held.count -= 1;
|
||||
return true;
|
||||
},
|
||||
// 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) {
|
||||
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 });
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
interface WateringCanData {
|
||||
water: number;
|
||||
max_water: number;
|
||||
}
|
||||
// right clicking water with the watering can fills it
|
||||
ctx.components.register_block("bworld:water_source", {
|
||||
on_interact(_block, _params, player) {
|
||||
const can = player.held_item;
|
||||
if (can?.id !== WATERING_CAN) return false;
|
||||
const data = can.data as WateringCanData;
|
||||
can.data = { ...data, water: data.max_water };
|
||||
return true;
|
||||
},
|
||||
});
|
||||
|
||||
function register_watering_can(ctx: ServerContext) {
|
||||
ctx.components.register_item<{ max_water: number }>("bworld:watering_can", {
|
||||
ctx.components.register_item<{ max_water: number }>(WATERING_CAN, {
|
||||
on_create(item, params) {
|
||||
item.data = { water: 0, max_water: params.max_water } satisfies WateringCanData;
|
||||
},
|
||||
|
||||
@@ -27,6 +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 { ModRuntime, run_guarded } from "./mod_runtime.ts";
|
||||
import type { GameLoop } from "./game_loop.ts";
|
||||
import { consume_recipe_items, update_crafting_result } from "./crafting.ts";
|
||||
@@ -51,6 +52,8 @@ const MERGE_DISTANCE = 0.5;
|
||||
// tiles further than this many chunks from every player don't tick
|
||||
const SIMULATION_DISTANCE = 6;
|
||||
const MAX_GIVE = 64 * 36;
|
||||
// the most mornings run at once after time jumps forward, the rest are skipped
|
||||
const MAX_CATCH_UP_DAYS = 64;
|
||||
// how long a client gets between welcome and ready to download and load everything
|
||||
const READY_TIMEOUT_TICKS = 60 * TICKS_PER_SECOND;
|
||||
|
||||
@@ -104,6 +107,8 @@ export class GameServer {
|
||||
#pending = new Map<number, { name: unknown; since: number }>();
|
||||
#saved_players: Record<string, SavedPlayer> = {};
|
||||
#tick = 0;
|
||||
// the last morning new day handlers ran for
|
||||
#day = 0;
|
||||
// items on the ground, by id
|
||||
#entities = new Map<string, ItemEntity>();
|
||||
#next_entity_id = 1;
|
||||
@@ -127,6 +132,7 @@ export class GameServer {
|
||||
this.mods.storage = saved?.mod_storage ?? {};
|
||||
this.world.load_changes(saved?.changes ?? []);
|
||||
this.world.time = saved?.time ?? 0;
|
||||
this.#day = this.world.day;
|
||||
for (const [id, items] of Object.entries(saved?.containers ?? {})) {
|
||||
const container = new Container(items.length);
|
||||
container.load(items);
|
||||
@@ -219,6 +225,7 @@ export class GameServer {
|
||||
this.world.dirty = true;
|
||||
}
|
||||
|
||||
this.#new_days();
|
||||
this.#tick_items();
|
||||
|
||||
this.mods.tick();
|
||||
@@ -354,6 +361,40 @@ export class GameServer {
|
||||
this.world.time = time;
|
||||
this.world.dirty = true;
|
||||
this.#broadcast({ type: "time", time });
|
||||
// going back in time doesn't repeat mornings, going forward runs the ones skipped (see #new_days)
|
||||
this.#day = Math.min(this.#day, this.world.day);
|
||||
}
|
||||
|
||||
// every morning that came since the last tick, usually none or one. /time can skip many
|
||||
#new_days() {
|
||||
const today = this.world.day;
|
||||
if (today <= this.#day) {
|
||||
return;
|
||||
}
|
||||
const first = Math.max(this.#day + 1, today - MAX_CATCH_UP_DAYS + 1);
|
||||
for (let day = first; day <= today; day++) {
|
||||
this.#new_day(day);
|
||||
}
|
||||
this.#day = today;
|
||||
}
|
||||
|
||||
// every block with data runs on_new_day, wherever it is, so crops grow while nobody's around
|
||||
#new_day(day: number) {
|
||||
for (const tile of [...this.world.tiles.values()]) {
|
||||
if (tile.mod_data === undefined) {
|
||||
continue;
|
||||
}
|
||||
const components = this.mods.components_of(tile.id).filter(({ component }) => component.on_new_day);
|
||||
if (components.length === 0) {
|
||||
continue;
|
||||
}
|
||||
const block = this.mods.block_ref(tile.x, tile.y, tile.z, tile.id);
|
||||
for (const { mod, id, component, params } of components) {
|
||||
run_guarded(mod, `${id} on_new_day`, () => component.on_new_day!(block, params, day));
|
||||
}
|
||||
}
|
||||
this.mods.after.new_day.emit({ day });
|
||||
this.world.dirty = true;
|
||||
}
|
||||
|
||||
// changes a block's state bits without anything else happening, see ServerWorld.get_block_value
|
||||
@@ -388,6 +429,29 @@ export class GameServer {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// a block standing on this one that can't stand on what it is now breaks, like a crop on farmland that was dug
|
||||
if (y + 1 < CHUNK_HEIGHT) {
|
||||
const above = this.world.get_block_info(x, y + 1, z);
|
||||
if (above && !can_stand_on(above, id)) {
|
||||
this.destroy_block(x, y + 1, z, player);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// breaks a block the way breaking it by hand does: its components' on_break, then what it drops. without the
|
||||
// right tool (or without a player) blocks that need one drop nothing
|
||||
destroy_block(x: number, y: number, z: number, player?: ServerPlayer) {
|
||||
const info = this.world.get_block_info(x, y, z);
|
||||
if (!info) {
|
||||
return;
|
||||
}
|
||||
const held = EverythingRegistry.get<ItemRegistry>("items", player?.held_item?.type_id ?? "");
|
||||
const drops = info.drop_table && (!info.requires_tool || held?.tool_type === info.tool_to_break);
|
||||
this.set_block(x, y, z, AIR_ID, player);
|
||||
if (drops) {
|
||||
this.pop_item(x, y, z, new ItemStack(info.drop_table!));
|
||||
}
|
||||
}
|
||||
|
||||
get_or_create_tile(x: number, y: number, z: number): Tile {
|
||||
@@ -604,13 +668,7 @@ export class GameServer {
|
||||
return;
|
||||
}
|
||||
|
||||
const held = EverythingRegistry.get<ItemRegistry>("items", player.held_item?.type_id ?? "");
|
||||
const drops = info.drop_table && (!info.requires_tool || held?.tool_type === info.tool_to_break);
|
||||
|
||||
this.set_block(x, y, z, AIR_ID, player);
|
||||
if (drops) {
|
||||
this.pop_item(x, y, z, new ItemStack(info.drop_table!));
|
||||
}
|
||||
this.destroy_block(x, y, z, player);
|
||||
this.mods.after.block_break.emit(event);
|
||||
}
|
||||
|
||||
@@ -693,7 +751,13 @@ export class GameServer {
|
||||
// place the held block against the face
|
||||
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)) {
|
||||
if (
|
||||
!held?.block_id || ty < 0 || ty >= CHUNK_HEIGHT || !this.#replaceable(tx, ty, tz) ||
|
||||
!can_stand_on(
|
||||
EverythingRegistry.get<BlockRegistry>("blocks", held.block_id),
|
||||
this.world.get_block_id(tx, ty - 1, tz),
|
||||
)
|
||||
) {
|
||||
this.#correct(player, tx, ty, tz);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -99,6 +99,7 @@ export class ModRuntime {
|
||||
player_leave: new Signal(),
|
||||
server_start: new Signal(),
|
||||
tick: new Signal(),
|
||||
new_day: new Signal(),
|
||||
};
|
||||
|
||||
// per mod key value storage, saved with the world
|
||||
@@ -368,6 +369,9 @@ export class ModRuntime {
|
||||
get time() {
|
||||
return game().world.time;
|
||||
},
|
||||
get day() {
|
||||
return game().world.day;
|
||||
},
|
||||
set_time: (time) => {
|
||||
if (!Number.isInteger(time) || time < 0) {
|
||||
throw new Error(`time must be a whole number of ticks, not ${time}`);
|
||||
|
||||
@@ -4,6 +4,7 @@ import { generate_raw_chunk, RawChunk, WorldgenSetup } from "$/common/generation
|
||||
import { AIR_ID, BlockChange } from "$/common/protocol.ts";
|
||||
import { block_value, chunk_key, default_block_value } from "$/common/utils.ts";
|
||||
import { LruMap } from "./lru.ts";
|
||||
import { CYCLE_TICKS } from "$/common/time.ts";
|
||||
|
||||
// generation only, rebuilt when needed. 256 KB each
|
||||
const RAW_CACHE_SIZE = 128;
|
||||
@@ -44,6 +45,11 @@ export class ServerWorld {
|
||||
// ticks since 06:00 of day 1, see common/time.ts
|
||||
time = 0;
|
||||
|
||||
// days since the world started, going up at 06:00. the farming day, unlike the clock's day that starts at midnight
|
||||
get day() {
|
||||
return Math.floor(this.time / CYCLE_TICKS);
|
||||
}
|
||||
|
||||
// set when anything that gets saved changes
|
||||
dirty = false;
|
||||
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
// farming: crops grow one stage every morning at 06:00 if their farmland was watered the day before, like stardew
|
||||
// valley, with minecraft's rules for where crops stand and what they drop
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import { CYCLE_TICKS } from "$/common/time.ts";
|
||||
import { get_state_value } from "$/common/utils.ts";
|
||||
import { test_game } from "./helpers.ts";
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
type Msg = any;
|
||||
|
||||
// a player on a stone floor at y 99, high above the terrain, with some items
|
||||
async function setup(items: string[] = []) {
|
||||
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: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
|
||||
for (const item of items) t.send(1, { type: "chat", text: `/give ${item}` });
|
||||
const inventory = () =>
|
||||
t.take(1).filter((m) => m.type === "container" && m.container === "inventory").at(-1)?.items ?? [];
|
||||
const hold = (id: string) => {
|
||||
const slot = inventory_now().findIndex((i: Msg) => i?.id === id);
|
||||
assert(slot >= 0 && slot < 9, `${id} isn't in the hotbar`);
|
||||
t.send(1, { type: "select_slot", slot });
|
||||
};
|
||||
// the inventory as the server has it
|
||||
const inventory_now = () => t.game.players()[0].inventory.to_data();
|
||||
const use = (x: number, y: number, z: number) => t.send(1, { type: "use_block", x, y, z, face: "top" });
|
||||
const state = (x: number, y: number, z: number, name: string) => {
|
||||
const info = t.game.world.get_block_info(x, y, z)!;
|
||||
return get_state_value(t.game.world.get_block_value(x, y, z), info, name);
|
||||
};
|
||||
// ticks until the next 06:00
|
||||
const next_morning = () => {
|
||||
t.game.set_time((t.game.world.day + 1) * CYCLE_TICKS - 1);
|
||||
t.game.tick();
|
||||
};
|
||||
return { ...t, inventory, inventory_now, hold, use, state, next_morning };
|
||||
}
|
||||
|
||||
Deno.test("hoe, fill the can, plant, water, and the crop grows the next morning", async () => {
|
||||
const t = await setup(["bworld:hoe", "bworld:watering_can", "bworld:wheat_seeds 4"]);
|
||||
t.game.set_block(1, 100, 0, "bworld:grass");
|
||||
t.game.set_block(0, 100, 1, "bworld:grass");
|
||||
t.game.set_block(2, 100, 0, "bworld:water");
|
||||
|
||||
t.hold("bworld:hoe");
|
||||
t.use(1, 100, 0);
|
||||
assertEquals(t.game.world.get_block_id(1, 100, 0), "bworld:hoed_dirt");
|
||||
|
||||
// seeds only go on farmland
|
||||
t.hold("bworld:wheat_seeds");
|
||||
t.use(0, 100, 1);
|
||||
assertEquals(t.game.world.get_block_id(0, 101, 1), "bworld:air");
|
||||
t.use(1, 100, 0);
|
||||
assertEquals(t.game.world.get_block_id(1, 101, 0), "bworld:wheat");
|
||||
|
||||
// a new can is empty, water fills it, and watering the crop waters the farmland under it
|
||||
t.hold("bworld:watering_can");
|
||||
t.take(1);
|
||||
t.use(1, 101, 0);
|
||||
assert(t.take(1).some((m) => m.type === "chat" && m.text.includes("empty")));
|
||||
assertEquals(t.state(1, 100, 0, "watered"), 0);
|
||||
t.use(2, 100, 0);
|
||||
t.use(1, 101, 0);
|
||||
assertEquals(t.state(1, 100, 0, "watered"), 1);
|
||||
const can = t.inventory_now().find((i) => i?.id === "bworld:watering_can");
|
||||
assertEquals(can?.data, { water: 31, max_water: 32 });
|
||||
|
||||
// nothing happens during the day
|
||||
for (let i = 0; i < 100; i++) t.game.tick();
|
||||
assertEquals(t.state(1, 101, 0, "age"), 0);
|
||||
|
||||
// the next morning it grows, and the farmland dries
|
||||
t.next_morning();
|
||||
assertEquals(t.state(1, 101, 0, "age"), 1);
|
||||
assertEquals(t.state(1, 100, 0, "watered"), 0);
|
||||
|
||||
// without watering it doesn't
|
||||
t.next_morning();
|
||||
assertEquals(t.state(1, 101, 0, "age"), 1);
|
||||
});
|
||||
|
||||
Deno.test("fully grown crops drop their produce and extra seeds, young ones only a seed", async () => {
|
||||
const t = await setup();
|
||||
const wheat = EverythingRegistry.get<BlockRegistry>("blocks", "bworld:wheat")!;
|
||||
const dropped = () => t.game.item_entities().map((e) => e.item.to_data());
|
||||
|
||||
t.game.set_block(1, 100, 0, "bworld:hoed_dirt");
|
||||
t.game.set_block(1, 101, 0, "bworld:wheat");
|
||||
t.send(1, { type: "break_block", x: 1, y: 101, z: 0 });
|
||||
assertEquals(dropped(), [{ id: "bworld:wheat_seeds", count: 1 }]);
|
||||
|
||||
t.game.set_block(2, 100, 0, "bworld:hoed_dirt");
|
||||
t.game.set_block(2, 101, 0, "bworld:wheat", undefined, 7);
|
||||
assertEquals(get_state_value(t.game.world.get_block_value(2, 101, 0), wheat, "age"), 7);
|
||||
t.send(1, { type: "break_block", x: 2, y: 101, z: 0 });
|
||||
const ids = dropped().map((i) => i.id);
|
||||
assert(ids.includes("bworld:wheat"), `dropped ${ids}`);
|
||||
assert(ids.filter((id) => id === "bworld:wheat_seeds").length >= 2);
|
||||
});
|
||||
|
||||
Deno.test("digging up the farmland pops the crop off with its drops", async () => {
|
||||
const t = await setup();
|
||||
t.game.set_block(1, 100, 0, "bworld:hoed_dirt");
|
||||
t.game.set_block(1, 101, 0, "bworld:wheat");
|
||||
t.send(1, { type: "break_block", x: 1, y: 100, z: 0 });
|
||||
assertEquals(t.game.world.get_block_id(1, 101, 0), "bworld:air");
|
||||
const ids = t.game.item_entities().map((e) => e.item.type_id).sort();
|
||||
assertEquals(ids, ["bworld:dirt", "bworld:wheat_seeds"]);
|
||||
});
|
||||
|
||||
Deno.test("crops grow far from every player, and mornings skipped by /time still come", async () => {
|
||||
const t = await setup();
|
||||
const days: number[] = [];
|
||||
t.game.mods.after.new_day.for_mod("test").subscribe(({ day }) => days.push(day));
|
||||
|
||||
// thousands of blocks from anyone
|
||||
t.game.set_block(5000, 100, 5000, "bworld:hoed_dirt");
|
||||
t.game.set_block(5000, 101, 5000, "bworld:wheat");
|
||||
t.game.set_block_state(5000, 100, 5000, 1);
|
||||
t.next_morning();
|
||||
assertEquals(days, [1]);
|
||||
assertEquals(t.state(5000, 101, 5000, "age"), 1);
|
||||
|
||||
// three mornings at once, one each
|
||||
t.game.set_time(4 * CYCLE_TICKS);
|
||||
t.game.tick();
|
||||
assertEquals(days, [1, 2, 3, 4]);
|
||||
|
||||
// going back is no morning, and the mornings after it come again as time passes them
|
||||
t.game.set_time(CYCLE_TICKS + 10);
|
||||
t.game.tick();
|
||||
assertEquals(days, [1, 2, 3, 4]);
|
||||
t.game.set_time(2 * CYCLE_TICKS);
|
||||
t.game.tick();
|
||||
assertEquals(days, [1, 2, 3, 4, 2]);
|
||||
t.game.set_time(4 * CYCLE_TICKS);
|
||||
// the world's day is the same one scripts see
|
||||
assertEquals(t.game.world.day, 4);
|
||||
});
|
||||
Reference in New Issue
Block a user