Change how ticking works a little
This commit is contained in:
@@ -4,6 +4,7 @@ import {
|
||||
FACE_OFFSETS,
|
||||
Faces,
|
||||
faces,
|
||||
ID_MASK,
|
||||
PLAYER_WIDTH,
|
||||
TICK_DELTA,
|
||||
TICKS_PER_SECOND,
|
||||
@@ -28,6 +29,7 @@ import {
|
||||
box_overlaps_block,
|
||||
can_place,
|
||||
can_stand_on,
|
||||
chunk_key,
|
||||
display_name,
|
||||
is_waterlogged,
|
||||
liquid_block,
|
||||
@@ -558,20 +560,31 @@ export class GameServer {
|
||||
this.#day = today;
|
||||
}
|
||||
|
||||
// every block with data runs on_new_day, wherever it is, so crops grow while nobody's around
|
||||
// every block with data runs on_new_day, wherever it is, so crops grow while nobody's around. blocks without data,
|
||||
// like ones world generation made, run it near players, like the blocks minecraft random ticks
|
||||
#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);
|
||||
const run = (x: number, y: number, z: number, block_id: string) => {
|
||||
const components = this.mods.components_of(block_id).filter(({ component }) => component.on_new_day);
|
||||
if (components.length === 0) {
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
const block = this.mods.block_ref(tile.x, tile.y, tile.z, tile.id);
|
||||
const block = this.mods.block_ref(x, y, z, block_id);
|
||||
for (const { mod, id, component, params } of components) {
|
||||
run_guarded(mod, `${id} on_new_day`, () => component.on_new_day!(block, params, day));
|
||||
}
|
||||
};
|
||||
// found before any runs, so a block one of them places, like a cactus growing, waits for tomorrow
|
||||
const without_data = this.#blocks_without_data_near_players();
|
||||
for (const tile of [...this.world.tiles.values()]) {
|
||||
if (tile.mod_data !== undefined) {
|
||||
run(tile.x, tile.y, tile.z, tile.id);
|
||||
}
|
||||
}
|
||||
for (const [x, y, z, block_id] of without_data) {
|
||||
// unless an earlier one changed it, or gave it data and it ran above
|
||||
if (this.world.get_block_id(x, y, z) === block_id && this.world.get_tile(x, y, z)?.mod_data === undefined) {
|
||||
run(x, y, z, block_id);
|
||||
}
|
||||
}
|
||||
this.mods.after.new_day.emit({ day });
|
||||
this.world.dirty = true;
|
||||
@@ -1938,6 +1951,36 @@ export class GameServer {
|
||||
return dx * dx + dy * dy + dz * dz <= MAX_REACH * MAX_REACH;
|
||||
}
|
||||
|
||||
// the blocks within SIMULATION_DISTANCE of a player that have on_new_day and no data, in chunks that are built
|
||||
#blocks_without_data_near_players(): [number, number, number, string][] {
|
||||
const wanted = new Uint8Array(ID_MASK + 1);
|
||||
for (const [id, nid] of Object.entries(this.world.block_ids)) {
|
||||
if (this.mods.components_of(id).some(({ component }) => component.on_new_day)) wanted[nid] = 1;
|
||||
}
|
||||
if (!wanted.includes(1)) {
|
||||
return [];
|
||||
}
|
||||
const chunks = new Map<number, [number, number]>();
|
||||
for (const player of this.#players.values()) {
|
||||
const px = Math.floor(player.x / CHUNK_SIZE);
|
||||
const pz = Math.floor(player.z / CHUNK_SIZE);
|
||||
for (let cx = px - SIMULATION_DISTANCE; cx <= px + SIMULATION_DISTANCE; cx++) {
|
||||
for (let cz = pz - SIMULATION_DISTANCE; cz <= pz + SIMULATION_DISTANCE; cz++) {
|
||||
chunks.set(chunk_key(cx, cz), [cx, cz]);
|
||||
}
|
||||
}
|
||||
}
|
||||
const found: [number, number, number, string][] = [];
|
||||
for (const [cx, cz] of chunks.values()) {
|
||||
for (const [x, y, z] of this.world.find_blocks(cx, cz, wanted)) {
|
||||
if (this.world.get_tile(x, y, z)?.mod_data === undefined) {
|
||||
found.push([x, y, z, this.world.get_block_id(x, y, z)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
#near_any_player(x: number, z: number) {
|
||||
const chunk_x = Math.floor(x / CHUNK_SIZE);
|
||||
const chunk_z = Math.floor(z / CHUNK_SIZE);
|
||||
|
||||
@@ -190,6 +190,25 @@ export class ServerWorld {
|
||||
return this.#chunks.get(chunk_key(chunk_x, chunk_z)) !== undefined;
|
||||
}
|
||||
|
||||
// where in a kept chunk (see has_chunk) the blocks are whose numeric id is set in wanted, as x, y, z. nothing when
|
||||
// the chunk isn't kept
|
||||
find_blocks(chunk_x: number, chunk_z: number, wanted: Uint8Array): [number, number, number][] {
|
||||
const blocks = this.#chunks.get(chunk_key(chunk_x, chunk_z));
|
||||
if (!blocks) return [];
|
||||
const found: [number, number, number][] = [];
|
||||
for (let i = 0; i < blocks.length; i++) {
|
||||
if (!wanted[blocks[i] & ID_MASK]) continue;
|
||||
const y = Math.floor(i / CHUNK_AREA);
|
||||
const rest = i % CHUNK_AREA;
|
||||
found.push([
|
||||
chunk_x * CHUNK_SIZE + rest % CHUNK_SIZE,
|
||||
y,
|
||||
chunk_z * CHUNK_SIZE + Math.floor(rest / CHUNK_SIZE),
|
||||
]);
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
// whether every chunk within radius blocks of x, z is, see has_chunk
|
||||
has_chunks_around(x: number, z: number, radius: number) {
|
||||
for (let cx = Math.floor((x - radius) / CHUNK_SIZE); cx <= Math.floor((x + radius) / CHUNK_SIZE); cx++) {
|
||||
|
||||
Reference in New Issue
Block a user