Change how ticking works a little

This commit is contained in:
2026-10-10 20:11:03 -03:00
parent f4cf1d8fa8
commit be05f70fb5
6 changed files with 96 additions and 15 deletions
+5 -3
View File
@@ -67,7 +67,7 @@ component.
| `on_interact(block, params, player)` | A player right clicks it. | `true` if it did something |
| `on_tick(block, params, dt)` | Every tick, 20 times a second, near a player. | |
| `on_second(block, params, dt)` | Every second, near a player. | |
| `on_new_day(block, params, day)` | Every morning at 06:00, anywhere in the world. | |
| `on_new_day(block, params, day)` | Every morning at 06:00. See [Ticking](#ticking) for where. | |
`block` is `{ id, x, y, z, biome, season, data }`, where `data` is the block's [tile data](tiledata.md) and `biome` and
`season` are its column's (see [Seasons](../worldgen/biomes.md#seasons)). `params` is what the block's JSON lists for
@@ -161,8 +161,10 @@ clients guess that the click places the held block and show it for a moment befo
ticks from the start sets `block.data` in `on_create`, like `copper_tools:oxidizes` above.
`on_new_day` runs for every block with tile data in the whole world, near a player or not, so crops keep growing while
nobody's around. When time jumps forward past several mornings, like with `/time add`, each of them runs, up to the
last 64.
nobody's around. Blocks without tile data, like the cacti world generation makes, run it too, but only within 6 chunks
of a player, like the blocks Minecraft random ticks. Blocks one of them places that morning, like a cactus growing a
block taller, wait for the next one. When time jumps forward past several mornings, like with `/time add`, each of them
runs, up to the last 64.
The server runs a fixed 20 ticks a second. When a tick takes too long, up to ten of the next ones run back to back to
catch up. If it's further behind than that, it skips the rest and logs a warning. `/tps` shows how it's keeping up.
+2 -1
View File
@@ -27,7 +27,8 @@ Outside a handler, `ctx.world.get_block_data<T>(x, y, z)` reads it.
- Tile data is saved with the world, and goes away when the block is broken or replaced.
- It's **never sent to clients**. To show it to a player, open a [menu](../inventories/menus.md), or change a block
state that the block's `variants` show.
- Only blocks with tile data run `on_tick`, `on_second` and `on_new_day`.
- Only blocks with tile data run `on_tick` and `on_second`. `on_new_day` runs for blocks without it too, but only near
players, see [Ticking](components.md#ticking).
## Blocks With Containers
-3
View File
@@ -800,9 +800,6 @@ interface GrowableBlockParams {
function register_growable_block(ctx: ServerContext) {
ctx.components.register_block<GrowableBlockParams>("bworld:growable_block", {
on_create(block) {
block.data = {};
},
on_new_day(block, params, day) {
console.log(ctx.world.get_block(block.x, block.y + 1, block.z));
if (ctx.world.get_block(block.x, block.y + 1, block.z) !== "bworld:air") {
+51 -8
View File
@@ -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);
+19
View File
@@ -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++) {
+19
View File
@@ -331,3 +331,22 @@ Deno.test("a pumpkin plant becomes a pumpkin the morning it's grown, and a melon
assertEquals(slices.map((item) => item.id), ["bworld:melon_slice"]);
assert(slices[0].count >= 3 && slices[0].count <= 7);
});
Deno.test("blocks without data run on_new_day near players, once a morning", async () => {
const t = await setup();
// a cactus without data, like one world generation made
t.game.set_block(0, 100, 0, "bworld:sand");
t.game.set_block(0, 101, 0, "bworld:cactus");
t.game.world.remove_tile(0, 101, 0);
t.next_morning();
assertEquals(t.game.world.get_block_id(0, 102, 0), "bworld:cactus", "grew a block");
assertEquals(t.game.world.get_block_id(0, 103, 0), "bworld:air", "the new block waits for tomorrow");
// far from every player, in a chunk that isn't kept, nothing runs
const far = 16 * 40;
t.game.set_block(far, 100, 0, "bworld:sand");
t.game.set_block(far, 101, 0, "bworld:cactus");
t.game.world.remove_tile(far, 101, 0);
t.next_morning();
assertEquals(t.game.world.get_block_id(far, 102, 0), "bworld:air");
});