Water flowing
This commit is contained in:
@@ -9,6 +9,7 @@ import {
|
||||
TICKS_PER_SECOND,
|
||||
} from "$/common/constants.ts";
|
||||
import { BlockRegistry, EverythingRegistry, is_liquid_block, ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import { LiquidFlow } from "./liquids.ts";
|
||||
import { Container, ItemData, ItemStack } from "$/common/inventory.ts";
|
||||
import {
|
||||
AIR_ID,
|
||||
@@ -161,6 +162,8 @@ export class GameServer {
|
||||
recipes: RecipeBook;
|
||||
// monsters spawning on their own near players, see natural_spawner.ts
|
||||
spawner: NaturalSpawner;
|
||||
// water and other liquids flowing, see liquids.ts
|
||||
liquids: LiquidFlow;
|
||||
// minecraft's doMobSpawning game rule. tests turn it off so nothing turns up they didn't put there
|
||||
do_mob_spawning = true;
|
||||
// what runs tick(), for /tps. tests call tick() themselves
|
||||
@@ -186,6 +189,20 @@ export class GameServer {
|
||||
);
|
||||
}
|
||||
this.world = new ServerWorld(saved?.seed ?? default_seed, mods.worldgen);
|
||||
this.liquids = new LiquidFlow({
|
||||
value_at: (x, y, z) => {
|
||||
if (y < 0 || y >= CHUNK_HEIGHT) return undefined;
|
||||
const loaded = this.world.has_chunk(Math.floor(x / CHUNK_SIZE), Math.floor(z / CHUNK_SIZE));
|
||||
return loaded ? this.world.get_block_value(x, y, z) : undefined;
|
||||
},
|
||||
block: (nid) => EverythingRegistry.get_by_id<BlockRegistry>("blocks", nid),
|
||||
nid: (id) => this.world.block_ids[id],
|
||||
set_block: (x, y, z, id, state) => {
|
||||
// a level changing is only a state, the block's components don't see it come and go
|
||||
if (id === this.world.get_block_id(x, y, z)) this.set_block_state(x, y, z, state);
|
||||
else this.set_block(x, y, z, id, undefined, state);
|
||||
},
|
||||
});
|
||||
// deno-lint-ignore no-this-alias
|
||||
const game = this;
|
||||
this.entities = new EntityManager({
|
||||
@@ -231,6 +248,7 @@ export class GameServer {
|
||||
|
||||
// players without the chunk get the block with it when they do, like minecraft
|
||||
this.world.on_block_change = (x, y, z, id, state) => {
|
||||
this.liquids.changed(x, y, z, this.#tick);
|
||||
const data = JSON.stringify(
|
||||
(state
|
||||
? { type: "set_block", x, y, z, id, state }
|
||||
@@ -331,6 +349,7 @@ export class GameServer {
|
||||
}
|
||||
|
||||
this.#new_days();
|
||||
this.liquids.tick(this.#tick);
|
||||
this.#tick_vitals();
|
||||
this.#tick_entities();
|
||||
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
// liquids flowing, like minecraft's FlowingFluid: a change next to a liquid gets the cells around it looked at again
|
||||
// after the liquid's ticks, and each works out what it should be from its neighbors (see next_level). that changes
|
||||
// more cells, and so it spreads one block a step, and drains away the same way once its source is gone
|
||||
import { AIR, CHUNK_HEIGHT, ID_MASK } from "$/common/constants.ts";
|
||||
import type { BlockRegistry } from "$/common/everything_registry.ts";
|
||||
import { AIR_ID } from "$/common/protocol.ts";
|
||||
import { get_state_value, set_state_value } from "$/common/utils.ts";
|
||||
|
||||
// what the flow needs of the world
|
||||
export interface LiquidWorld {
|
||||
// undefined where the chunk isn't loaded, so liquid stops at the edge of the world instead of making new chunks
|
||||
value_at(x: number, y: number, z: number): number | undefined;
|
||||
// by numeric id
|
||||
block(nid: number): BlockRegistry | undefined;
|
||||
nid(id: string): number;
|
||||
// changes a block, with its components and neighbors told like any other change
|
||||
set_block(x: number, y: number, z: number, id: string, state: number): void;
|
||||
}
|
||||
|
||||
// the most cells looked at in one tick, the rest wait for the next
|
||||
const MAX_PER_TICK = 4096;
|
||||
|
||||
const HORIZONTAL = [[1, 0], [-1, 0], [0, 1], [0, -1]];
|
||||
const NEIGHBORS = [[0, 0, 0], [0, 1, 0], [0, -1, 0], [1, 0, 0], [-1, 0, 0], [0, 0, 1], [0, 0, -1]];
|
||||
|
||||
// a liquid in a cell: which, and its level
|
||||
interface Liquid {
|
||||
nid: number;
|
||||
block: BlockRegistry;
|
||||
level: number;
|
||||
}
|
||||
|
||||
const key = (x: number, y: number, z: number) => `${x},${y},${z}`;
|
||||
|
||||
export class LiquidFlow {
|
||||
#world: LiquidWorld;
|
||||
// cells to look at, by key, with the tick they're due
|
||||
#due = new Map<string, { x: number; y: number; z: number; tick: number }>();
|
||||
|
||||
constructor(world: LiquidWorld) {
|
||||
this.#world = world;
|
||||
}
|
||||
|
||||
// how many cells are waiting, for tests
|
||||
get pending() {
|
||||
return this.#due.size;
|
||||
}
|
||||
|
||||
// a block changed at x y z: if there's liquid there or beside it, it and its neighbors are looked at again after
|
||||
// that liquid's ticks
|
||||
changed(x: number, y: number, z: number, now: number) {
|
||||
let ticks = Infinity;
|
||||
for (const [dx, dy, dz] of NEIGHBORS) {
|
||||
const liquid = this.#liquid(x + dx, y + dy, z + dz);
|
||||
if (liquid) ticks = Math.min(ticks, liquid.block.liquid!.ticks);
|
||||
}
|
||||
if (ticks === Infinity) return;
|
||||
for (const [dx, dy, dz] of NEIGHBORS) {
|
||||
const k = key(x + dx, y + dy, z + dz);
|
||||
const due = this.#due.get(k);
|
||||
if (due && due.tick <= now + ticks) continue;
|
||||
this.#due.set(k, { x: x + dx, y: y + dy, z: z + dz, tick: now + ticks });
|
||||
}
|
||||
}
|
||||
|
||||
// looks at the cells due by now. changing them schedules their neighbors through changed
|
||||
tick(now: number) {
|
||||
const ready: { x: number; y: number; z: number }[] = [];
|
||||
for (const [k, due] of this.#due) {
|
||||
if (due.tick > now) continue;
|
||||
this.#due.delete(k);
|
||||
ready.push(due);
|
||||
if (ready.length >= MAX_PER_TICK) break;
|
||||
}
|
||||
for (const { x, y, z } of ready) this.#update(x, y, z);
|
||||
}
|
||||
|
||||
#update(x: number, y: number, z: number) {
|
||||
const value = this.#world.value_at(x, y, z);
|
||||
if (value === undefined) return;
|
||||
const nid = value & ID_MASK;
|
||||
const here = this.#liquid(x, y, z);
|
||||
if (here?.level === 0) return;
|
||||
// only air and the liquid's own flowing cells are flowed into
|
||||
if (!here && nid !== AIR) return;
|
||||
|
||||
// the liquid that would be here: what's here, or one from above or beside
|
||||
const candidates = here ? [here] : this.#liquids_around(x, y, z);
|
||||
for (const liquid of candidates) {
|
||||
const level = this.next_level(liquid, x, y, z);
|
||||
if (level === undefined) continue;
|
||||
if (!here || here.level !== level) this.#set(liquid.block, x, y, z, level);
|
||||
return;
|
||||
}
|
||||
if (here) this.#world.set_block(x, y, z, AIR_ID, 0);
|
||||
}
|
||||
|
||||
// what level the liquid would be at x y z, from the cells around it, or undefined for none there
|
||||
next_level(liquid: Liquid, x: number, y: number, z: number): number | undefined {
|
||||
const props = liquid.block.liquid!;
|
||||
const same = (cx: number, cy: number, cz: number) => {
|
||||
const other = this.#liquid(cx, cy, cz);
|
||||
return other?.nid === liquid.nid ? other : undefined;
|
||||
};
|
||||
|
||||
// two sources beside it over something solid or a source make another, like minecraft's infinite water
|
||||
if (props.renewable) {
|
||||
const sources = HORIZONTAL.filter(([dx, dz]) => same(x + dx, y, z + dz)?.level === 0).length;
|
||||
const below = this.#world.value_at(x, y - 1, z);
|
||||
const below_block = below === undefined ? undefined : this.#world.block(below & ID_MASK);
|
||||
if (sources >= 2 && (below_block?.has_collision || same(x, y - 1, z)?.level === 0)) return 0;
|
||||
}
|
||||
|
||||
// falling, with more of it above. it's drawn full whatever its level, see liquid_mesh.ts
|
||||
if (same(x, y + 1, z)) return 1;
|
||||
|
||||
let lowest = Infinity;
|
||||
for (const [dx, dz] of HORIZONTAL) {
|
||||
const nx = x + dx;
|
||||
const nz = z + dz;
|
||||
const neighbor = same(nx, y, nz);
|
||||
if (!neighbor || !this.#spreads_sideways(neighbor, nx, y, nz)) continue;
|
||||
// falling liquid spreads like a source once it lands
|
||||
const level = same(nx, y + 1, nz) ? 0 : neighbor.level;
|
||||
lowest = Math.min(lowest, level);
|
||||
}
|
||||
const level = lowest + 1;
|
||||
return level <= props.range ? level : undefined;
|
||||
}
|
||||
|
||||
// sources spread sideways, and flowing liquid that can't fall any further. over air or more of itself it falls
|
||||
// instead, like minecraft's isWaterHole
|
||||
#spreads_sideways(liquid: Liquid, x: number, y: number, z: number) {
|
||||
if (liquid.level === 0) return true;
|
||||
const below = this.#world.value_at(x, y - 1, z);
|
||||
if (below === undefined) return false;
|
||||
const nid = below & ID_MASK;
|
||||
return nid !== AIR && nid !== liquid.nid;
|
||||
}
|
||||
|
||||
// the liquids above and beside x y z that could flow into it, above first
|
||||
#liquids_around(x: number, y: number, z: number): Liquid[] {
|
||||
const found: Liquid[] = [];
|
||||
for (const [dx, dy, dz] of [[0, 1, 0], ...HORIZONTAL.map(([dx, dz]) => [dx, 0, dz])]) {
|
||||
const liquid = this.#liquid(x + dx, y + dy, z + dz);
|
||||
if (liquid && !found.some((other) => other.nid === liquid.nid)) found.push(liquid);
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
#liquid(x: number, y: number, z: number): Liquid | undefined {
|
||||
if (y < 0 || y >= CHUNK_HEIGHT) return undefined;
|
||||
const value = this.#world.value_at(x, y, z);
|
||||
if (value === undefined) return undefined;
|
||||
const nid = value & ID_MASK;
|
||||
const block = this.#world.block(nid);
|
||||
if (!block?.liquid) return undefined;
|
||||
return { nid, block, level: get_state_value(value, block, "level")! };
|
||||
}
|
||||
|
||||
#set(block: BlockRegistry, x: number, y: number, z: number, level: number) {
|
||||
const value = set_state_value(this.#world.nid(block.id), block, "level", level)!;
|
||||
this.#world.set_block(x, y, z, block.id, value >>> 16);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user