Add natural mob spawning for rotwalker
This commit is contained in:
+1
-1
@@ -240,7 +240,7 @@ Deno.test("the atlas fits textures of any size, tallest first, without overlappi
|
||||
Deno.test("a mod with only data packs and reads", () => {
|
||||
const bmod = {
|
||||
manifest: { format_version: 1, id: "tiny", name: "Tiny", version: "1.0.0" },
|
||||
data: { blocks: [], models: [], items: [], recipes: [], ores: [], entities: [], entity_models: [], tags: [] },
|
||||
data: { blocks: [], models: [], items: [], recipes: [], ores: [], spawns: [], entities: [], entity_models: [], tags: [] },
|
||||
scripts: {},
|
||||
textures: new Map(),
|
||||
};
|
||||
|
||||
@@ -32,6 +32,7 @@ export function mod_sources(mods_dir: string): ServerModSource[] {
|
||||
items: mod.items.map((i) => i.json),
|
||||
recipes: mod.recipes.map((r) => r.json),
|
||||
ores: mod.ores.map((o) => o.json),
|
||||
spawns: mod.spawns.map((s) => s.json),
|
||||
entities: mod.entities.map((e) => e.json),
|
||||
entity_models: mod.entity_models.map((m) => m.json),
|
||||
tags: mod.tags.map((t) => t.json),
|
||||
@@ -67,6 +68,8 @@ export async function test_game(mods_dir: string, save?: WorldSave, seed = "test
|
||||
};
|
||||
const game: GameServer = await start_game(host, save, seed, mod_sources(mods_dir));
|
||||
game.chunk_send_budget_ms = Infinity;
|
||||
// tests that want natural spawning turn it back on
|
||||
game.do_mob_spawning = false;
|
||||
// deno-lint-ignore no-explicit-any
|
||||
const take = (conn: number): any[] => {
|
||||
const messages = outbox.get(conn) ?? [];
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
// 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_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 { 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
|
||||
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 - 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");
|
||||
}
|
||||
}
|
||||
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<ReturnType<typeof setup>>["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<BlockRegistry>("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");
|
||||
});
|
||||
@@ -202,6 +202,7 @@ Deno.test("tags survive packing into a .bmod", () => {
|
||||
items: [],
|
||||
recipes: [],
|
||||
ores: [],
|
||||
spawns: [],
|
||||
entities: [],
|
||||
entity_models: [],
|
||||
tags: [tag],
|
||||
|
||||
Reference in New Issue
Block a user