| 1 | import { GAIT, CHUNK_SIZE } from "../../../shared/protocol/constants.js"; |
| 2 | import { mobCanFight, mobSkillLevels } from "../../../shared/definitions/mobCombat.js"; |
| 3 | import { |
| 4 | normalizeSpawnerConfig, |
| 5 | listAmbientMobTypes, |
| 6 | mobDefinition, |
| 7 | mobSpriteName, |
| 8 | MobId, |
| 9 | spawnerEquipmentSlot |
| 10 | } from "../../../shared/definitions/mobs.js"; |
| 11 | import { CENTRAL_EXCHANGE_STAFF } from "../../../shared/market/exchange.js"; |
| 12 | import { emptyEquipment } from "../../../shared/items/inventory.js"; |
| 13 | import { itemDefinition } from "../../../shared/definitions/items.js"; |
| 14 | import { appearanceHairFields, hairFromNameSeed } from "../../../shared/appearance/hair.js"; |
| 15 | import { Component } from "../ecs/components.js"; |
| 16 | import { tileToChunk, tileToRegion } from "../world/coordinates.js"; |
| 17 | |
| 18 | export function collectSpawnRequests (world) |
| 19 | { |
| 20 | const requests = []; |
| 21 | const counts = countBySpawner(world); |
| 22 | const staffAlive = staffNamesBySpawner(world); |
| 23 | const reserved = new Set(); |
| 24 | for (const [key, chunk] of world.chunks) |
| 25 | { |
| 26 | if (chunk.replica || !chunk.simulating || !chunk.data.open) |
| 27 | { continue; } |
| 28 | const [chunkX, chunkY] = key.split(",").map(Number); |
| 29 | for (const entry of chunk.data.spawners ?? []) |
| 30 | { |
| 31 | const localX = entry.localX; |
| 32 | const localY = entry.localY; |
| 33 | const index = localY * CHUNK_SIZE + localX; |
| 34 | const config = normalizeSpawnerConfig( |
| 35 | entry.config, |
| 36 | `${chunkX}:${chunkY}:${index}` |
| 37 | ); |
| 38 | const flying = !!mobDefinition(config.mobTypeId)?.flying; |
| 39 | const originX = chunkX * CHUNK_SIZE + localX; |
| 40 | const originY = chunkY * CHUNK_SIZE + localY; |
| 41 | const persistent = !!mobDefinition(config.mobTypeId)?.persistent; |
| 42 | |
| 43 | if (config.centralExchange) |
| 44 | { |
| 45 | const alive = staffAlive.get(config.spawnerId) ?? new Set(); |
| 46 | for (const staff of CENTRAL_EXCHANGE_STAFF) |
| 47 | { |
| 48 | if (alive.has(staff.name)) |
| 49 | { continue; } |
| 50 | if ((counts.get(config.spawnerId) ?? 0) >= config.maxAlive) |
| 51 | { break; } |
| 52 | const tile = pickSpawnTile(world, originX, originY, config.spawnRangeTiles, flying, reserved); |
| 53 | if (!tile) |
| 54 | { break; } |
| 55 | reserved.add(`${tile.tileX},${tile.tileY}`); |
| 56 | alive.add(staff.name); |
| 57 | staffAlive.set(config.spawnerId, alive); |
| 58 | requests.push({ |
| 59 | ...tile, |
| 60 | mobTypeId: MobId.centralExchange, |
| 61 | spawnerId: config.spawnerId, |
| 62 | originTileX: originX, |
| 63 | originTileY: originY, |
| 64 | wanderRangeTiles: config.maxWanderRangeTiles, |
| 65 | persistent, |
| 66 | sex: staff.sex, |
| 67 | displayName: staff.name, |
| 68 | equipment: config.equipment, |
| 69 | sells: [], |
| 70 | buysItems: false, |
| 71 | hiddenDiscount: config.hiddenDiscount, |
| 72 | centralExchange: true |
| 73 | }); |
| 74 | counts.set(config.spawnerId, (counts.get(config.spawnerId) ?? 0) + 1); |
| 75 | } |
| 76 | continue; |
| 77 | } |
| 78 | |
| 79 | while ((counts.get(config.spawnerId) ?? 0) < config.maxAlive) |
| 80 | { |
| 81 | const tile = pickSpawnTile(world, originX, originY, config.spawnRangeTiles, flying, reserved); |
| 82 | if (!tile) |
| 83 | { break; } |
| 84 | reserved.add(`${tile.tileX},${tile.tileY}`); |
| 85 | requests.push({ |
| 86 | ...tile, |
| 87 | mobTypeId: config.mobTypeId, |
| 88 | spawnerId: config.spawnerId, |
| 89 | originTileX: originX, |
| 90 | originTileY: originY, |
| 91 | wanderRangeTiles: config.maxWanderRangeTiles, |
| 92 | persistent, |
| 93 | sex: config.sex, |
| 94 | displayName: config.displayName, |
| 95 | equipment: config.equipment, |
| 96 | sells: config.sells, |
| 97 | buysItems: config.buysItems, |
| 98 | hiddenDiscount: config.hiddenDiscount, |
| 99 | centralExchange: false |
| 100 | }); |
| 101 | counts.set(config.spawnerId, (counts.get(config.spawnerId) ?? 0) + 1); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | requests.push(...collectAmbientRequests(world, reserved)); |
| 106 | return requests; |
| 107 | } |
| 108 | |
| 109 | export function mobComponents (request) |
| 110 | { |
| 111 | const def = mobDefinition(request.mobTypeId) ?? mobDefinition("deer"); |
| 112 | const sex = request.sex === "female" || request.sex === "male" |
| 113 | ? request.sex |
| 114 | : (def.sex ?? "male"); |
| 115 | const displayName = String(request.displayName || def.displayName || "NPC").trim() || def.displayName; |
| 116 | const appearance = { |
| 117 | sex, |
| 118 | firstName: displayName, |
| 119 | lastName: "" |
| 120 | }; |
| 121 | if (def.usePlayerBody) |
| 122 | { |
| 123 | Object.assign(appearance, appearanceHairFields(hairFromNameSeed(displayName, sex))); |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | const sprite = mobSpriteName(def.mobTypeId); |
| 128 | if (sprite) |
| 129 | { appearance.sprite = sprite; } |
| 130 | } |
| 131 | |
| 132 | const components = { |
| 133 | position: { tileX: request.tileX, tileY: request.tileY }, |
| 134 | movement: { gait: def.gait ?? GAIT.walk, walkCarry: 0 }, |
| 135 | path: { tiles: [] }, |
| 136 | appearance, |
| 137 | creature: { mobTypeId: def.mobTypeId }, |
| 138 | health: { current: def.health ?? 8, max: def.health ?? 8 } |
| 139 | }; |
| 140 | if (request.spawnerId) |
| 141 | { |
| 142 | components.spawnOrigin = { |
| 143 | spawnerId: request.spawnerId, |
| 144 | originTileX: request.originTileX ?? request.tileX, |
| 145 | originTileY: request.originTileY ?? request.tileY, |
| 146 | wanderRangeTiles: request.wanderRangeTiles ?? 4, |
| 147 | centralExchange: !!request.centralExchange, |
| 148 | stationTileX: null, |
| 149 | stationTileY: null |
| 150 | }; |
| 151 | } |
| 152 | if (def.persistent) |
| 153 | { components.persistent = {}; } |
| 154 | if (def.freeFlight) |
| 155 | { |
| 156 | components.flight = { |
| 157 | x: request.tileX + 0.5, |
| 158 | y: request.tileY + 0.5, |
| 159 | targetX: request.tileX + 0.5, |
| 160 | targetY: request.tileY + 0.5, |
| 161 | speed: def.flightSpeedTilesPerSec ?? 2.5, |
| 162 | nextPickAt: 0, |
| 163 | lastAt: 0 |
| 164 | }; |
| 165 | } |
| 166 | if (mobCanFight(def)) |
| 167 | { |
| 168 | components.combat = { targetEntityId: null, nextSwingAt: 0 }; |
| 169 | components.skills = { levels: mobSkillLevels(def) }; |
| 170 | } |
| 171 | |
| 172 | const equipmentSlots = emptyEquipment().slots; |
| 173 | const authored = request.equipment ?? {}; |
| 174 | for (const slot of Object.keys(equipmentSlots)) |
| 175 | { |
| 176 | const { itemId, dyeColor } = spawnerEquipmentSlot(authored[slot]); |
| 177 | if (!itemId) |
| 178 | { continue; } |
| 179 | const itemDef = itemDefinition(itemId); |
| 180 | if (!itemDef?.equipment || itemDef.equipment.slot !== slot) |
| 181 | { continue; } |
| 182 | const stack = { itemId, quantity: 1 }; |
| 183 | if (dyeColor && itemDef.dyeable) |
| 184 | { stack.instance = { kind: "clothing", dyeColor }; } |
| 185 | equipmentSlots[slot] = stack; |
| 186 | } |
| 187 | if (def.usePlayerBody || Object.values(equipmentSlots).some(Boolean)) |
| 188 | { components.equipment = { slots: equipmentSlots }; } |
| 189 | |
| 190 | const catalog = Array.isArray(request.sells) |
| 191 | ? request.sells.map((entry) => |
| 192 | { |
| 193 | if (entry && typeof entry === "object") |
| 194 | { |
| 195 | return { |
| 196 | itemId: entry.itemId | 0, |
| 197 | quantity: Math.max(0, entry.quantity | 0 || 0) |
| 198 | }; |
| 199 | } |
| 200 | return { itemId: entry | 0, quantity: 0 }; |
| 201 | }).filter((line) => line.itemId > 0) |
| 202 | : []; |
| 203 | const buysItems = !!request.buysItems; |
| 204 | const isExchange = !!request.centralExchange || def.centralExchange; |
| 205 | if (!isExchange && (catalog.length || buysItems || def.talkable)) |
| 206 | { |
| 207 | components.shop = { |
| 208 | catalog, |
| 209 | buysItems, |
| 210 | hiddenDiscount: Number(request.hiddenDiscount) || 0.9, |
| 211 | displayName, |
| 212 | remaining: Object.create(null), |
| 213 | boughtStock: Object.create(null), |
| 214 | restockDay: null |
| 215 | }; |
| 216 | } |
| 217 | return components; |
| 218 | } |
| 219 | |
| 220 | function collectAmbientRequests (world, reserved = new Set()) |
| 221 | { |
| 222 | const requests = []; |
| 223 | const ambientTypes = listAmbientMobTypes(); |
| 224 | if (!ambientTypes.length) |
| 225 | { return requests; } |
| 226 | |
| 227 | const loaders = world.ecs.query(Component.position, Component.chunkLoader); |
| 228 | if (!loaders.length) |
| 229 | { return requests; } |
| 230 | |
| 231 | for (const def of ambientTypes) |
| 232 | { |
| 233 | const maxPerLoader = Math.max(0, def.ambientMaxPerLoader ?? 1); |
| 234 | const alive = countCreatures(world, def.mobTypeId); |
| 235 | const budget = maxPerLoader * loaders.length; |
| 236 | if (alive >= budget) |
| 237 | { continue; } |
| 238 | |
| 239 | for (const loader of loaders) |
| 240 | { |
| 241 | if (requests.length + alive >= budget) |
| 242 | { break; } |
| 243 | const tile = pickSpawnTile( |
| 244 | world, |
| 245 | loader.position.tileX, |
| 246 | loader.position.tileY, |
| 247 | 8, |
| 248 | !!def.flying, |
| 249 | reserved |
| 250 | ); |
| 251 | if (!tile) |
| 252 | { continue; } |
| 253 | reserved.add(`${tile.tileX},${tile.tileY}`); |
| 254 | requests.push({ |
| 255 | ...tile, |
| 256 | mobTypeId: def.mobTypeId, |
| 257 | spawnerId: null, |
| 258 | persistent: false |
| 259 | }); |
| 260 | } |
| 261 | } |
| 262 | return requests; |
| 263 | } |
| 264 | |
| 265 | function countBySpawner (world) |
| 266 | { |
| 267 | const counts = new Map(); |
| 268 | for (const entity of world.ecs.query(Component.spawnOrigin)) |
| 269 | { |
| 270 | const id = entity.spawnOrigin.spawnerId; |
| 271 | counts.set(id, (counts.get(id) ?? 0) + 1); |
| 272 | } |
| 273 | return counts; |
| 274 | } |
| 275 | |
| 276 | function staffNamesBySpawner (world) |
| 277 | { |
| 278 | const map = new Map(); |
| 279 | for (const entity of world.ecs.query(Component.spawnOrigin, Component.appearance)) |
| 280 | { |
| 281 | if (!entity.spawnOrigin?.centralExchange) |
| 282 | { continue; } |
| 283 | const id = entity.spawnOrigin.spawnerId; |
| 284 | const name = entity.appearance?.firstName; |
| 285 | if (!id || !name) |
| 286 | { continue; } |
| 287 | const set = map.get(id) ?? new Set(); |
| 288 | set.add(name); |
| 289 | map.set(id, set); |
| 290 | } |
| 291 | return map; |
| 292 | } |
| 293 | |
| 294 | function countCreatures (world, mobTypeId) |
| 295 | { |
| 296 | let count = 0; |
| 297 | for (const entity of world.ecs.query(Component.creature)) |
| 298 | { |
| 299 | if (entity.creature.mobTypeId === mobTypeId) |
| 300 | { count += 1; } |
| 301 | } |
| 302 | return count; |
| 303 | } |
| 304 | |
| 305 | function pickSpawnTile (world, originX, originY, range, flying = false, reserved = null) |
| 306 | { |
| 307 | const radius = Math.max(1, range | 0); |
| 308 | for (let attempt = 0; attempt < 48; attempt++) |
| 309 | { |
| 310 | const tileX = originX + randInt(-radius, radius); |
| 311 | const tileY = originY + randInt(-radius, radius); |
| 312 | const key = `${tileX},${tileY}`; |
| 313 | if (reserved?.has(key)) |
| 314 | { continue; } |
| 315 | if (!world.ownsRegion(tileToRegion(tileX), tileToRegion(tileY))) |
| 316 | { continue; } |
| 317 | if (flying) |
| 318 | { |
| 319 | if (!chunkIsOpen(world, tileX, tileY)) |
| 320 | { continue; } |
| 321 | return { tileX, tileY }; |
| 322 | } |
| 323 | if (!world.isEnterable(tileX, tileY) || world.occupied(tileX, tileY)) |
| 324 | { continue; } |
| 325 | return { tileX, tileY }; |
| 326 | } |
| 327 | return null; |
| 328 | } |
| 329 | |
| 330 | function chunkIsOpen (world, tileX, tileY) |
| 331 | { |
| 332 | const data = world.chunkData(tileToChunk(tileX), tileToChunk(tileY)); |
| 333 | return !!data?.open; |
| 334 | } |
| 335 | |
| 336 | function randInt (min, max) |
| 337 | { |
| 338 | return min + Math.floor(Math.random() * (max - min + 1)); |
| 339 | } |