BlueGit

timfa questward

Files

questward / server / src / simulation / spawners.js

spawners.js

spawners.js 3.0 KiB Download Raw
1import { GAIT, CHUNK_SIZE } from "../../../shared/protocol/constants.js";
2import { BlockType } from "../../../shared/definitions/tiles.js";
3import { defaultSpawnerConfig, mobDefinition, mobSpriteName } from "../../../shared/definitions/mobs.js";
4import { Component } from "../ecs/components.js";
5import { tileToRegion } from "../world/coordinates.js";
6
7export function collectSpawnRequests (world)
8{
9 const requests = [];
10 const counts = countBySpawner(world);
11 for (const [key, chunk] of world.chunks)
12 {
13 if (chunk.replica || !chunk.simulating || !chunk.data.open)
14 { continue; }
15 const [chunkX, chunkY] = key.split(",").map(Number);
16 for (let localY = 0; localY < CHUNK_SIZE; localY++)
17 {
18 for (let localX = 0; localX < CHUNK_SIZE; localX++)
19 {
20 const block = chunk.data.blocks[localY * CHUNK_SIZE + localX];
21 if (block?.typeId !== BlockType.spawner)
22 { continue; }
23 const config = { ...defaultSpawnerConfig(`${chunkX}:${chunkY}:${localY * CHUNK_SIZE + localX}`), ...(block.config ?? {}) };
24 if ((counts.get(config.spawnerId) ?? 0) >= config.maxAlive)
25 { continue; }
26 const originX = chunkX * CHUNK_SIZE + localX;
27 const originY = chunkY * CHUNK_SIZE + localY;
28 const tile = pickSpawnTile(world, originX, originY, config.spawnRangeTiles);
29 if (!tile)
30 { continue; }
31 requests.push({
32 ...tile,
33 mobTypeId: config.mobTypeId,
34 spawnerId: config.spawnerId,
35 persistent: !!mobDefinition(config.mobTypeId)?.persistent
36 });
37 counts.set(config.spawnerId, (counts.get(config.spawnerId) ?? 0) + 1);
38 }
39 }
40 }
41 return requests;
42}
43
44export function mobComponents (request)
45{
46 const def = mobDefinition(request.mobTypeId) ?? mobDefinition("deer");
47 const components = {
48 position: { tileX: request.tileX, tileY: request.tileY },
49 movement: { gait: def.gait ?? GAIT.walk, walkCarry: 0 },
50 path: { tiles: [] },
51 appearance: {
52 sex: def.sex ?? "male",
53 firstName: def.displayName,
54 lastName: "",
55 sprite: mobSpriteName(def.mobTypeId)
56 },
57 creature: { mobTypeId: def.mobTypeId },
58 health: { current: def.health ?? 8, max: def.health ?? 8 },
59 spawnOrigin: { spawnerId: request.spawnerId }
60 };
61 if (def.persistent)
62 { components.persistent = {}; }
63 return components;
64}
65
66function countBySpawner (world)
67{
68 const counts = new Map();
69 for (const entity of world.ecs.query(Component.spawnOrigin))
70 {
71 const id = entity.spawnOrigin.spawnerId;
72 counts.set(id, (counts.get(id) ?? 0) + 1);
73 }
74 return counts;
75}
76
77function pickSpawnTile (world, originX, originY, range)
78{
79 const radius = Math.max(1, range | 0);
80 for (let attempt = 0; attempt < 24; attempt++)
81 {
82 const tileX = originX + randInt(-radius, radius);
83 const tileY = originY + randInt(-radius, radius);
84 if (!world.ownsRegion(tileToRegion(tileX), tileToRegion(tileY)))
85 { continue; }
86 if (!world.isEnterable(tileX, tileY) || world.occupied(tileX, tileY))
87 { continue; }
88 return { tileX, tileY };
89 }
90 return null;
91}
92
93function randInt (min, max)
94{
95 return min + Math.floor(Math.random() * (max - min + 1));
96}