Improve crops system
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user