Real ctx.world.is_loaded implementation
This commit is contained in:
@@ -241,7 +241,8 @@ export interface ServerAfterEvents {
|
||||
// world and players
|
||||
|
||||
export interface ServerWorld {
|
||||
get_block(x: number, y: number, z: number): Id | undefined; // undefined when the chunk isn't loaded
|
||||
// generates the chunk if it never was, which takes a while, see is_loaded
|
||||
get_block(x: number, y: number, z: number): Id | undefined;
|
||||
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, which it returns (copies). undefined when there
|
||||
// was nothing to break. see BreakOptions for machines breaking it
|
||||
@@ -300,6 +301,8 @@ export interface ServerWorld {
|
||||
// which way the liquid at x y z flows, a vector 1 long, or 0 0 0 where it's still or there's none, like minecraft's
|
||||
// FluidState.getFlow: toward lower cells of it beside it, and down where it falls. for what it pushes, like a wheel
|
||||
liquid_flow(x: number, y: number, z: number): Position;
|
||||
// whether the chunk the column is in was ever generated, so reading its blocks generates nothing. for code that
|
||||
// reaches far, like a walk along something long, which can stop at the rest: nobody has built anything there
|
||||
is_loaded(x: number, z: number): boolean;
|
||||
// the column's biome, like "bworld:plains", and that biome's season. in chunks that aren't loaded it's what
|
||||
// generation makes there, which takes a while the first time
|
||||
|
||||
@@ -117,6 +117,11 @@ Two more things in `ctx.world` are for blocks that tell others themselves:
|
||||
around it, for a script that tells them itself, so a long line of blocks changing together tells each block beside it
|
||||
once instead of once per block. `on_state_change` still runs.
|
||||
|
||||
Code that reaches far from a block, like a walk along something long, checks `ctx.world.is_loaded(x, z)` before it reads
|
||||
a column: whether that column's chunk was ever generated. Reading a block anywhere else generates its chunk on the spot,
|
||||
which takes a while, and nobody can have built anything there, so a walk stops at those chunks. A chunk that's saved but
|
||||
not in memory counts as loaded, and reading it reads it from the save.
|
||||
|
||||
## Rules
|
||||
|
||||
A component's rules decide a block's states from where it's placed, what's around it and being right clicked. They're
|
||||
|
||||
@@ -8,7 +8,7 @@ import { Container as EngineContainer, ItemStack as EngineItemStack } from "$/co
|
||||
import { block_states, get_state_value, is_waterlogged, liquid_block, set_state_value } from "$/common/utils.ts";
|
||||
import { LootTables } from "./loot.ts";
|
||||
import { collision_shape_at, covers_face, raycast_blocks, type Shape } from "$/common/block_shapes.ts";
|
||||
import { AIR, faces, ID_MASK } from "$/common/constants.ts";
|
||||
import { AIR, CHUNK_SIZE, faces, ID_MASK } from "$/common/constants.ts";
|
||||
import { physics } from "$/common/physics.ts";
|
||||
import { clock } from "$/common/time.ts";
|
||||
import { BlockRules } from "$/common/block_rules.ts";
|
||||
@@ -848,7 +848,7 @@ export class ModRuntime {
|
||||
// without any -0
|
||||
return { x: fx + 0, y: fy + 0, z: fz + 0 };
|
||||
},
|
||||
is_loaded: () => true,
|
||||
is_loaded: (x, z) => game().world.is_generated(Math.floor(x / CHUNK_SIZE), Math.floor(z / CHUNK_SIZE)),
|
||||
biome_at: (x, z) => game().world.biome_at(Math.floor(x), Math.floor(z)),
|
||||
season_at: (x, z) =>
|
||||
biome_season(game().world.biome_at(Math.floor(x), Math.floor(z)), game().world.time),
|
||||
|
||||
@@ -272,6 +272,13 @@ export class ServerWorld {
|
||||
return this.#chunks.has(chunk_key(chunk_x, chunk_z));
|
||||
}
|
||||
|
||||
// whether the chunk was ever generated: it's kept, or saved, so reading it generates nothing. it may not be in
|
||||
// memory, then reading it decodes it from the save
|
||||
is_generated(chunk_x: number, chunk_z: number) {
|
||||
const key = chunk_key(chunk_x, chunk_z);
|
||||
return this.#chunks.has(key) || this.#is_saved(key);
|
||||
}
|
||||
|
||||
// counts as using the chunk so it's kept longer, if it's there. returns whether it is, see has_chunk
|
||||
keep_chunk(chunk_x: number, chunk_z: number) {
|
||||
return this.#chunks.get(chunk_key(chunk_x, chunk_z)) !== undefined;
|
||||
|
||||
@@ -99,6 +99,11 @@ export function setup(ctx: ServerContext) {
|
||||
},
|
||||
});
|
||||
|
||||
ctx.commands.register("loaded", {
|
||||
description: "", usage: "",
|
||||
run([x, z], player) { player.send_message("loaded " + ctx.world.is_loaded(Number(x), Number(z))); },
|
||||
});
|
||||
|
||||
// brigadier's way, like neoforge's
|
||||
const { literal, argument, types } = ctx.commands;
|
||||
ctx.commands.register(
|
||||
@@ -294,6 +299,32 @@ Deno.test("block states start at their defaults, can be changed, and are synced
|
||||
Deno.removeSync(again.dir, { recursive: true });
|
||||
});
|
||||
|
||||
Deno.test("is_loaded says which chunks were ever generated, saved ones too, without generating any", async () => {
|
||||
const { game, send, take, dir } = await setup();
|
||||
const loaded = (x: number, z: number) => {
|
||||
send(1, { type: "chat", text: `/loaded ${x} ${z}` });
|
||||
return take(1).filter((m) => m.type === "chat").map((m) => m.text).at(-1);
|
||||
};
|
||||
assertEquals(loaded(0.5, 0.5), "loaded true");
|
||||
assertEquals(loaded(16 * 1000, 0), "loaded false");
|
||||
assertEquals(loaded(16 * 1000, 0), "loaded false", "asking didn't generate it");
|
||||
assert(!game.world.has_chunk(1000, 0));
|
||||
|
||||
game.world.build(-500, 0);
|
||||
assertEquals(loaded(-16 * 500 + 3, 15.5), "loaded true");
|
||||
const saved = game.save();
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
|
||||
const again = await setup(saved);
|
||||
assert(!again.game.world.has_chunk(-500, 0));
|
||||
const fallbacks = again.game.world.fallbacks;
|
||||
again.send(1, { type: "chat", text: `/loaded ${-16 * 500} 0` });
|
||||
assertEquals(again.take(1).filter((m) => m.type === "chat").at(-1).text, "loaded true", "it's saved");
|
||||
again.game.world.get_block_id(-16 * 500, 100, 0);
|
||||
assertEquals(again.game.world.fallbacks, fallbacks, "reading it decoded it from the save, generating nothing");
|
||||
Deno.removeSync(again.dir, { recursive: true });
|
||||
});
|
||||
|
||||
Deno.test("inventory on_change fires for the slots that changed", async () => {
|
||||
const { game, send, log, dir } = await setup();
|
||||
send(1, { type: "chat", text: "/give bworld:stone 3" });
|
||||
|
||||
Reference in New Issue
Block a user