BlueGit

timfa questward

Files

questward / server / src / simulation / spawners.js

spawners.js

spawners.js 5.0 KiB Download Raw
1import { GAIT, CHUNK_SIZE } from "../../../shared/protocol/constants.js";
2import { BlockType } from "../../../shared/definitions/tiles.js";
3import { mobCanFight, mobSkillLevels } from "../../../shared/definitions/mobCombat.js";
4import {
5 defaultSpawnerConfig,
6 listAmbientMobTypes,
7 mobDefinition,
8 mobSpriteName
9} from "../../../shared/definitions/mobs.js";
10import { Component } from "../ecs/components.js";
11import { tileToChunk, tileToRegion } from "../world/coordinates.js";
12
13export function collectSpawnRequests (world)
14{
15 const requests = [];
16 const counts = countBySpawner(world);
17 for (const [key, chunk] of world.chunks)
18 {
19 if (chunk.replica || !chunk.simulating || !chunk.data.open)
20 { continue; }
21 const [chunkX, chunkY] = key.split(",").map(Number);
22 for (let localY = 0; localY < CHUNK_SIZE; localY++)
23 {
24 for (let localX = 0; localX < CHUNK_SIZE; localX++)
25 {
26 const block = chunk.data.blocks[localY * CHUNK_SIZE + localX];
27 if (block?.typeId !== BlockType.spawner)
28 { continue; }
29 const config = { ...defaultSpawnerConfig(`${chunkX}:${chunkY}:${localY * CHUNK_SIZE + localX}`), ...(block.config ?? {}) };
30 if ((counts.get(config.spawnerId) ?? 0) >= config.maxAlive)
31 { continue; }
32 const originX = chunkX * CHUNK_SIZE + localX;
33 const originY = chunkY * CHUNK_SIZE + localY;
34 const flying = !!mobDefinition(config.mobTypeId)?.flying;
35 const tile = pickSpawnTile(world, originX, originY, config.spawnRangeTiles, flying);
36 if (!tile)
37 { continue; }
38 requests.push({
39 ...tile,
40 mobTypeId: config.mobTypeId,
41 spawnerId: config.spawnerId,
42 persistent: !!mobDefinition(config.mobTypeId)?.persistent
43 });
44 counts.set(config.spawnerId, (counts.get(config.spawnerId) ?? 0) + 1);
45 }
46 }
47 }
48 requests.push(...collectAmbientRequests(world));
49 return requests;
50}
51
52export function mobComponents (request)
53{
54 const def = mobDefinition(request.mobTypeId) ?? mobDefinition("deer");
55 const components = {
56 position: { tileX: request.tileX, tileY: request.tileY },
57 movement: { gait: def.gait ?? GAIT.walk, walkCarry: 0 },
58 path: { tiles: [] },
59 appearance: {
60 sex: def.sex ?? "male",
61 firstName: def.displayName,
62 lastName: "",
63 sprite: mobSpriteName(def.mobTypeId)
64 },
65 creature: { mobTypeId: def.mobTypeId },
66 health: { current: def.health ?? 8, max: def.health ?? 8 }
67 };
68 if (request.spawnerId)
69 { components.spawnOrigin = { spawnerId: request.spawnerId }; }
70 if (def.persistent)
71 { components.persistent = {}; }
72 if (def.freeFlight)
73 {
74 components.flight = {
75 x: request.tileX + 0.5,
76 y: request.tileY + 0.5,
77 targetX: request.tileX + 0.5,
78 targetY: request.tileY + 0.5,
79 speed: def.flightSpeedTilesPerSec ?? 2.5,
80 nextPickAt: 0,
81 lastAt: 0
82 };
83 }
84 if (mobCanFight(def))
85 {
86 components.combat = { targetEntityId: null, nextSwingAt: 0 };
87 components.skills = { levels: mobSkillLevels(def) };
88 }
89 return components;
90}
91
92function collectAmbientRequests (world)
93{
94 const requests = [];
95 const ambientTypes = listAmbientMobTypes();
96 if (!ambientTypes.length)
97 { return requests; }
98
99 const loaders = world.ecs.query(Component.position, Component.chunkLoader);
100 if (!loaders.length)
101 { return requests; }
102
103 for (const def of ambientTypes)
104 {
105 const maxPerLoader = Math.max(0, def.ambientMaxPerLoader ?? 1);
106 const alive = countCreatures(world, def.mobTypeId);
107 const budget = maxPerLoader * loaders.length;
108 if (alive >= budget)
109 { continue; }
110
111 for (const loader of loaders)
112 {
113 if (requests.length + alive >= budget)
114 { break; }
115 const tile = pickSpawnTile(
116 world,
117 loader.position.tileX,
118 loader.position.tileY,
119 8,
120 !!def.flying
121 );
122 if (!tile)
123 { continue; }
124 requests.push({
125 ...tile,
126 mobTypeId: def.mobTypeId,
127 spawnerId: null,
128 persistent: false
129 });
130 }
131 }
132 return requests;
133}
134
135function countBySpawner (world)
136{
137 const counts = new Map();
138 for (const entity of world.ecs.query(Component.spawnOrigin))
139 {
140 const id = entity.spawnOrigin.spawnerId;
141 counts.set(id, (counts.get(id) ?? 0) + 1);
142 }
143 return counts;
144}
145
146function countCreatures (world, mobTypeId)
147{
148 let count = 0;
149 for (const entity of world.ecs.query(Component.creature))
150 {
151 if (entity.creature.mobTypeId === mobTypeId)
152 { count += 1; }
153 }
154 return count;
155}
156
157function pickSpawnTile (world, originX, originY, range, flying = false)
158{
159 const radius = Math.max(1, range | 0);
160 for (let attempt = 0; attempt < 24; attempt++)
161 {
162 const tileX = originX + randInt(-radius, radius);
163 const tileY = originY + randInt(-radius, radius);
164 if (!world.ownsRegion(tileToRegion(tileX), tileToRegion(tileY)))
165 { continue; }
166 if (flying)
167 {
168 if (!chunkIsOpen(world, tileX, tileY))
169 { continue; }
170 return { tileX, tileY };
171 }
172 if (!world.isEnterable(tileX, tileY) || world.occupied(tileX, tileY))
173 { continue; }
174 return { tileX, tileY };
175 }
176 return null;
177}
178
179function chunkIsOpen (world, tileX, tileY)
180{
181 const data = world.chunkData(tileToChunk(tileX), tileToChunk(tileY));
182 return !!data?.open;
183}
184
185function randInt (min, max)
186{
187 return min + Math.floor(Math.random() * (max - min + 1));
188}