// monsters spawning on their own in the dark near players, like undergarden's rotwalkers, see natural_spawner.ts and // mods/undergarden/worldgen/spawns.json import { assert, assertEquals } from "@std/assert"; import { CHUNK_HEIGHT, CHUNK_SIZE } from "$/common/constants.ts"; import { decode_level_chunk } from "$/common/chunk_streaming.ts"; import { block_light_emission, block_light_opacity, BlockRegistry, EverythingRegistry, } from "$/common/everything_registry.ts"; import { LightRegion, REGION_LAYER, REGION_SIZE, REGION_VOID } from "$/client/workers/lighting.ts"; import { create_entity } from "$/server/game/entity/entity_types.ts"; import { Mob } from "$/server/game/entity/mob.ts"; import { NaturalSpawner } from "$/server/game/natural_spawner.ts"; import { test_game } from "./helpers.ts"; // chunk 6, 6: a closed stone room with its floor at y 60 and its roof at y 64, and alice standing 40 blocks away. // the rest of the chunk is solid stone below it and open sky above, so wherever worldgen puts caves or shade, the // room is the only dark place const CHUNK = 6; const FLOOR = 60; const ROOF = 64; async function setup(roof = "bworld:stone") { const t = await test_game("mods"); const world = t.game.world; const x0 = CHUNK * CHUNK_SIZE; for (let x = x0; x < x0 + CHUNK_SIZE; x++) { for (let z = x0; z < x0 + CHUNK_SIZE; z++) { for (let y = 0; y < FLOOR; y++) world.set_block(x, y, z, "bworld:stone"); for (let y = ROOF + 1; y < CHUNK_HEIGHT; y++) world.set_block(x, y, z, "bworld:air"); } } for (let x = x0 - 1; x <= x0 + CHUNK_SIZE; x++) { for (let z = x0 - 1; z <= x0 + CHUNK_SIZE; z++) { const wall = x < x0 || z < x0 || x >= x0 + CHUNK_SIZE || z >= x0 + CHUNK_SIZE; world.set_block(x, FLOOR, z, "bworld:stone"); world.set_block(x, ROOF, z, wall ? "bworld:stone" : roof); for (let y = FLOOR + 1; y < ROOF; y++) world.set_block(x, y, z, wall ? "bworld:stone" : "bworld:air"); } } // only the rotwalker, so what else the undergarden spawns doesn't change what can spawn here t.game.spawner = new NaturalSpawner(t.game.spawner.spawns.filter((s) => s.entity === "undergarden:rotwalker")); t.join(1, "alice"); const alice = t.game.players()[0]; world.set_block(x0 + 48, FLOOR, x0 + 8, "bworld:stone"); // far enough to see the room and around it, it's 3 chunks away t.send(1, { type: "view_distance", distance: 5 }); t.send(1, { type: "move", x: x0 + 48.5, y: FLOOR + 1, z: x0 + 8.5, yaw: 0, pitch: 0 }); // her client loads everything around her, the room too for (let i = 0; i < 100 && alice.chunks.pending_count > 0; i++) { t.game.tick(); t.send(1, { type: "chunk_batch_received", chunks_per_tick: 64 }); } // what spawning would keep if it had been on, see NaturalSpawner.tick for (let dx = -1; dx <= 1; dx++) { for (let dz = -1; dz <= 1; dz++) world.get_block_nid(x0 + dx * CHUNK_SIZE, 0, x0 + dz * CHUNK_SIZE); } t.take(1); return { ...t, world, alice, x0 }; } // up to tries times, until something spawns function try_spawning(game: Awaited>["game"], tries: number): Mob[] { for (let i = 0; i < tries; i++) { const spawned = game.spawner.spawn_in_chunk(game, CHUNK, CHUNK); if (spawned.length) return spawned; } return []; } Deno.test("the server's light is the same as what clients work out", async () => { const { world, x0 } = await setup(); world.set_block(x0 + 3, FLOOR + 1, x0 + 3, "bworld:light"); world.set_block(x0 + 12, ROOF, x0 + 12, "bworld:air"); const blocks = EverythingRegistry.get_registry("blocks"); const opacity = new Uint8Array(REGION_VOID + 1); const emission = new Uint8Array(REGION_VOID + 1); blocks.forEach((block, nid) => { opacity[nid] = block_light_opacity(block); emission[nid] = block_light_emission(block); }); opacity[REGION_VOID] = 15; const region = new LightRegion(); const chunks = []; for (let dz = -1; dz <= 1; dz++) { for (let dx = -1; dx <= 1; dx++) { chunks.push(decode_level_chunk(world.level_chunk(CHUNK + dx, CHUNK + dz)).blocks); } } region.fill(chunks); region.compute({ opacity, emission }); const check = (x: number, y: number, z: number) => { const index = y * REGION_LAYER + (z - x0 + CHUNK_SIZE) * REGION_SIZE + x - x0 + CHUNK_SIZE; const expected = { sky: region.sky[index], block: region.block_light[index] }; assertEquals(world.light_at(x, y, z), expected, `at ${x}, ${y}, ${z}`); }; // the room, under the hole and around the light for (let x = x0; x < x0 + CHUNK_SIZE; x++) { for (let z = x0; z < x0 + CHUNK_SIZE; z++) { for (let y = FLOOR; y <= ROOF + 1; y++) check(x, y, z); } } // and anywhere else in the chunk for (let i = 0; i < 2000; i++) { check( x0 + Math.floor(Math.random() * 16), Math.floor(Math.random() * 256), x0 + Math.floor(Math.random() * 16), ); } }); Deno.test("rotwalkers spawn in groups in the dark, on the ground and away from players", async () => { const { game, alice } = await setup(); const spawned = try_spawning(game, 20000); assert(spawned.length > 0, "something spawned"); assert(spawned.length <= 4); for (const mob of spawned) { assertEquals(mob.type, "undergarden:rotwalker"); assertEquals(mob.y, FLOOR + 1); assert(Math.hypot(mob.x - alice.x, mob.y - alice.y, mob.z - alice.z) > 24); assert(game.entities.get_entity(mob.id), "it's in the world"); } }); Deno.test("nothing spawns where there's block light", async () => { const { game } = await setup("bworld:light"); assertEquals(try_spawning(game, 20000), []); }); Deno.test("nothing spawns within 24 blocks of a player", async () => { const { game, x0, send } = await setup(); send(1, { type: "move", x: x0 + 8.5, y: FLOOR + 1, z: x0 + 8.5, yaw: 0, pitch: 0 }); assertEquals(try_spawning(game, 20000), []); }); Deno.test("monsters stop spawning at the cap", async () => { const { game, alice } = await setup(); const rotwalkers = () => game.entities.all().filter((e) => e.type === "undergarden:rotwalker").length; // alice's chunks are loaded, so she's surrounded by them. nobody can spawn closer than 24 blocks anyway for (let i = 0; i < 40; i++) { const mob = create_entity(game.entities, "undergarden:rotwalker")!; mob.set_position(alice.x, alice.y + 100, alice.z); (mob as Mob).no_ai = true; game.entities.add_entity(mob); } game.do_mob_spawning = true; for (let i = 0; i < 200; i++) game.tick(); assertEquals(rotwalkers(), 40); }); Deno.test("with mob spawning on, monsters turn up as the game ticks", async () => { const { game } = await setup(); const rotwalkers = () => game.entities.all().filter((e) => e.type === "undergarden:rotwalker"); game.do_mob_spawning = true; for (let i = 0; i < 3000 && rotwalkers().length === 0; i++) game.tick(); assert(rotwalkers().length > 0); game.do_mob_spawning = false; const before = rotwalkers().length; for (let i = 0; i < 100; i++) game.tick(); assert(rotwalkers().length <= before, "none spawn with it off"); });