| 1 | import { GAIT, CHUNK_SIZE } from "../../../shared/protocol/constants.js"; |
| 2 | import |
| 3 | { |
| 4 | mobCanFight, |
| 5 | mobSkillLevels, |
| 6 | mobHealthForLevel, |
| 7 | mobSpellCastChancePercent |
| 8 | } from "../../../shared/definitions/mobCombat.js"; |
| 9 | import |
| 10 | { |
| 11 | normalizeSpawnerConfig, |
| 12 | listAmbientMobTypes, |
| 13 | mobDefinition, |
| 14 | mobSpriteName, |
| 15 | MobId, |
| 16 | spawnerEquipmentSlot, |
| 17 | isTownGuardMob, |
| 18 | isHumanoidMob |
| 19 | } from "../../../shared/definitions/mobs.js"; |
| 20 | import { spellDefinition, spellMinMagicLevel, SPRITE_BOLT_SPELL_BY_ELEMENT } from "../../../shared/definitions/spells.js"; |
| 21 | import { MAGIC_ELEMENT_ORDER, magicElementTint } from "../../../shared/definitions/attackTypes.js"; |
| 22 | import { essenceElementDisplayName } from "../../../shared/definitions/essenceItems.js"; |
| 23 | import { LeatherItemId } from "../../../shared/definitions/leatherItems.js"; |
| 24 | import { CENTRAL_EXCHANGE_STAFF } from "../../../shared/market/exchange.js"; |
| 25 | import { emptyEquipment } from "../../../shared/items/inventory.js"; |
| 26 | import { itemDefinition } from "../../../shared/definitions/items.js"; |
| 27 | import { appearanceHairFields, hairFromNameSeed, randomNpcHairColor, randomBrightOrPastelColor } from "../../../shared/appearance/hair.js"; |
| 28 | import { npcDefaultEquipment } from "../../../shared/appearance/npcEquipment.js"; |
| 29 | import { Component } from "../ecs/components.js"; |
| 30 | import { tileToChunk, tileToRegion, parseChunkKey } from "../world/coordinates.js"; |
| 31 | import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js"; |
| 32 | |
| 33 | export function collectSpawnRequests (world, now = Date.now()) |
| 34 | { |
| 35 | const requests = []; |
| 36 | const counts = countBySpawner(world); |
| 37 | const staffAlive = staffNamesBySpawner(world); |
| 38 | const reserved = new Set(); |
| 39 | ensurePendingRespawns(world); |
| 40 | for (const [key, chunk] of world.chunks) |
| 41 | { |
| 42 | if (chunk.replica || !chunk.simulating || !chunk.data.open) |
| 43 | { continue; } |
| 44 | const { chunkX, chunkY, dimensionId: keyDimensionId } = parseChunkKey(key); |
| 45 | const dimensionId = normalizeDimensionId(chunk.dimensionId ?? keyDimensionId ?? OVERWORLD_ID); |
| 46 | for (const entry of chunk.data.spawners ?? []) |
| 47 | { |
| 48 | const localX = entry.localX; |
| 49 | const localY = entry.localY; |
| 50 | const index = localY * CHUNK_SIZE + localX; |
| 51 | const rawSpawnerId = typeof entry.config?.spawnerId === "string" && entry.config.spawnerId |
| 52 | ? entry.config.spawnerId |
| 53 | : `${chunkX}:${chunkY}:${index}`; |
| 54 | const scopedSpawnerId = rawSpawnerId.startsWith(`${dimensionId}:`) |
| 55 | ? rawSpawnerId |
| 56 | : `${dimensionId}:${rawSpawnerId}`; |
| 57 | const config = normalizeSpawnerConfig(entry.config, scopedSpawnerId); |
| 58 | config.spawnerId = scopedSpawnerId; |
| 59 | const flying = !!mobDefinition(config.mobTypeId)?.flying; |
| 60 | const originX = chunkX * CHUNK_SIZE + localX; |
| 61 | const originY = chunkY * CHUNK_SIZE + localY; |
| 62 | const persistent = !!mobDefinition(config.mobTypeId)?.persistent; |
| 63 | const delayMs = Math.max(0, (config.respawnDelaySeconds | 0) * 1000); |
| 64 | |
| 65 | if (config.centralExchange) |
| 66 | { |
| 67 | const alive = staffAlive.get(config.spawnerId) ?? new Set(); |
| 68 | for (const staff of CENTRAL_EXCHANGE_STAFF) |
| 69 | { |
| 70 | if (alive.has(staff.name)) |
| 71 | { continue; } |
| 72 | if ((counts.get(config.spawnerId) ?? 0) >= config.maxAlive) |
| 73 | { break; } |
| 74 | if (!consumeSpawnSlot(world, config.spawnerId, delayMs, now)) |
| 75 | { break; } |
| 76 | const tile = pickSpawnTile( |
| 77 | world, |
| 78 | originX, |
| 79 | originY, |
| 80 | config.spawnRangeTiles, |
| 81 | flying, |
| 82 | reserved, |
| 83 | dimensionId |
| 84 | ); |
| 85 | if (!tile) |
| 86 | { break; } |
| 87 | reserved.add(`${tile.tileX},${tile.tileY}`); |
| 88 | alive.add(staff.name); |
| 89 | staffAlive.set(config.spawnerId, alive); |
| 90 | requests.push({ |
| 91 | ...tile, |
| 92 | dimensionId, |
| 93 | mobTypeId: MobId.centralExchange, |
| 94 | spawnerId: config.spawnerId, |
| 95 | originTileX: originX, |
| 96 | originTileY: originY, |
| 97 | wanderRangeTiles: config.maxWanderRangeTiles, |
| 98 | level: config.level, |
| 99 | bounty: config.bounty, |
| 100 | aggressive: !!config.aggressive, |
| 101 | spells: config.spells, |
| 102 | persistent, |
| 103 | sex: staff.sex, |
| 104 | displayName: staff.name, |
| 105 | equipment: config.equipment, |
| 106 | sells: [], |
| 107 | drops: config.drops, |
| 108 | buysItems: false, |
| 109 | hiddenDiscount: config.hiddenDiscount, |
| 110 | centralExchange: true, |
| 111 | townGuard: false, |
| 112 | dialogueId: config.dialogueId || null, |
| 113 | respawnDelaySeconds: config.respawnDelaySeconds |
| 114 | }); |
| 115 | counts.set(config.spawnerId, (counts.get(config.spawnerId) ?? 0) + 1); |
| 116 | } |
| 117 | continue; |
| 118 | } |
| 119 | |
| 120 | while ((counts.get(config.spawnerId) ?? 0) < config.maxAlive) |
| 121 | { |
| 122 | if (!consumeSpawnSlot(world, config.spawnerId, delayMs, now)) |
| 123 | { break; } |
| 124 | const tile = pickSpawnTile( |
| 125 | world, |
| 126 | originX, |
| 127 | originY, |
| 128 | config.spawnRangeTiles, |
| 129 | flying, |
| 130 | reserved, |
| 131 | dimensionId |
| 132 | ); |
| 133 | if (!tile) |
| 134 | { break; } |
| 135 | reserved.add(`${tile.tileX},${tile.tileY}`); |
| 136 | requests.push({ |
| 137 | ...tile, |
| 138 | dimensionId, |
| 139 | mobTypeId: config.mobTypeId, |
| 140 | spawnerId: config.spawnerId, |
| 141 | originTileX: originX, |
| 142 | originTileY: originY, |
| 143 | wanderRangeTiles: config.maxWanderRangeTiles, |
| 144 | level: config.level, |
| 145 | bounty: config.bounty, |
| 146 | aggressive: isHumanoidMob(config.mobTypeId) || config.townGuard |
| 147 | ? !!config.aggressive |
| 148 | : undefined, |
| 149 | spells: config.spells, |
| 150 | persistent, |
| 151 | sex: config.sex, |
| 152 | displayName: config.displayName, |
| 153 | equipment: config.equipment, |
| 154 | sells: config.sells, |
| 155 | drops: config.drops, |
| 156 | buysItems: config.buysItems, |
| 157 | hiddenDiscount: config.hiddenDiscount, |
| 158 | centralExchange: false, |
| 159 | townGuard: !!config.townGuard || isTownGuardMob(config.mobTypeId), |
| 160 | dialogueId: config.dialogueId || null, |
| 161 | respawnDelaySeconds: config.respawnDelaySeconds |
| 162 | }); |
| 163 | counts.set(config.spawnerId, (counts.get(config.spawnerId) ?? 0) + 1); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | requests.push(...collectAmbientRequests(world, reserved)); |
| 168 | return requests; |
| 169 | } |
| 170 | |
| 171 | function ensurePendingRespawns (world) |
| 172 | { |
| 173 | if (!world.pendingSpawnerRespawns) |
| 174 | { world.pendingSpawnerRespawns = new Map(); } |
| 175 | return world.pendingSpawnerRespawns; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Schedule a delayed refill slot after a spawned mob dies. |
| 180 | * Delay 0 means the next collect pass may refill immediately (no pending entry). |
| 181 | */ |
| 182 | export function scheduleSpawnerRespawn (world, spawnerId, respawnDelaySeconds, now = Date.now()) |
| 183 | { |
| 184 | if (!spawnerId) |
| 185 | { return; } |
| 186 | const delayMs = Math.max(0, (respawnDelaySeconds | 0) * 1000); |
| 187 | if (delayMs <= 0) |
| 188 | { return; } |
| 189 | const pending = ensurePendingRespawns(world); |
| 190 | const list = pending.get(spawnerId) ?? []; |
| 191 | list.push(now + delayMs); |
| 192 | list.sort((a, b) => a - b); |
| 193 | pending.set(spawnerId, list); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Decide whether one more mob may spawn for this spawner right now. |
| 198 | * - delay 0 / empty pending: allow (initial populate + instant refill) |
| 199 | * - otherwise: only if a pending timer is due; consume one due timer |
| 200 | */ |
| 201 | function consumeSpawnSlot (world, spawnerId, delayMs, now) |
| 202 | { |
| 203 | if (delayMs <= 0) |
| 204 | { return true; } |
| 205 | const pending = ensurePendingRespawns(world); |
| 206 | const list = pending.get(spawnerId); |
| 207 | if (!list?.length) |
| 208 | { |
| 209 | // No death timers yet — first populate / recovery after unload. |
| 210 | return true; |
| 211 | } |
| 212 | const readyIndex = list.findIndex((readyAt) => readyAt <= now); |
| 213 | if (readyIndex < 0) |
| 214 | { return false; } |
| 215 | list.splice(readyIndex, 1); |
| 216 | if (!list.length) |
| 217 | { pending.delete(spawnerId); } |
| 218 | else |
| 219 | { pending.set(spawnerId, list); } |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | export function mobComponents (request) |
| 224 | { |
| 225 | const def = mobDefinition(request.mobTypeId) ?? mobDefinition("deer"); |
| 226 | const sex = request.sex === "female" || request.sex === "male" |
| 227 | ? request.sex |
| 228 | : (def.sex ?? "male"); |
| 229 | const displayName = String(request.displayName || def.displayName || "NPC").trim() || def.displayName; |
| 230 | const level = Math.max(1, Math.min(99, Number(request.level) || 1)) | 0; |
| 231 | const appearance = { |
| 232 | sex, |
| 233 | firstName: displayName, |
| 234 | lastName: "" |
| 235 | }; |
| 236 | if (def.usePlayerBody) |
| 237 | { |
| 238 | Object.assign(appearance, appearanceHairFields(hairFromNameSeed(displayName, sex))); |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | const fleeceState = def.shearable?.fleeceState ?? null; |
| 243 | const sprite = mobSpriteName(def.mobTypeId, fleeceState); |
| 244 | if (sprite) |
| 245 | { appearance.sprite = sprite; } |
| 246 | } |
| 247 | |
| 248 | if (def.dualChannelAppearance || def.mobTypeId === MobId.fairy) |
| 249 | { |
| 250 | appearance.dualTint = { |
| 251 | /** Red channel — shirt. */ |
| 252 | shaft: randomBrightOrPastelColor(), |
| 253 | /** Blue channel — skirt. */ |
| 254 | tip: randomBrightOrPastelColor(), |
| 255 | /** Green channel — hair. */ |
| 256 | coat: randomNpcHairColor() |
| 257 | }; |
| 258 | appearance.bloomBody = !!def.bloomBody; |
| 259 | } |
| 260 | |
| 261 | let element = request.element ?? null; |
| 262 | if ((def.randomElementOnSpawn || def.mobTypeId === MobId.sprite) && !element) |
| 263 | { |
| 264 | element = MAGIC_ELEMENT_ORDER[Math.floor(Math.random() * MAGIC_ELEMENT_ORDER.length)]; |
| 265 | } |
| 266 | if (element) |
| 267 | { |
| 268 | const tint = magicElementTint(element); |
| 269 | appearance.tint = { r: tint.r, g: tint.g, b: tint.b }; |
| 270 | appearance.bloomBody = !!def.bloomBody; |
| 271 | if ((def.mobTypeId === MobId.sprite || def.randomElementOnSpawn) |
| 272 | && !String(request.displayName || "").trim()) |
| 273 | { |
| 274 | const label = essenceElementDisplayName(element); |
| 275 | appearance.firstName = `${label} Sprite`; |
| 276 | appearance.lastName = ""; |
| 277 | } |
| 278 | } |
| 279 | else if (def.bloomBody) |
| 280 | { appearance.bloomBody = true; } |
| 281 | |
| 282 | const maxHp = mobHealthForLevel(def, level); |
| 283 | const components = { |
| 284 | position: { |
| 285 | tileX: request.tileX, |
| 286 | tileY: request.tileY, |
| 287 | dimensionId: normalizeDimensionId(request.dimensionId ?? OVERWORLD_ID) |
| 288 | }, |
| 289 | movement: { gait: def.gait ?? GAIT.walk, walkCarry: 0 }, |
| 290 | path: { tiles: [] }, |
| 291 | appearance, |
| 292 | creature: { |
| 293 | mobTypeId: def.mobTypeId, |
| 294 | level, |
| 295 | ...(element ? { element } : {}), |
| 296 | ...(def.lightRadiusTiles ? { lightRadiusTiles: def.lightRadiusTiles } : {}), |
| 297 | ...(def.shearable |
| 298 | ? { |
| 299 | fleeceState: def.shearable.fleeceState, |
| 300 | woolReadyAt: null |
| 301 | } |
| 302 | : {}) |
| 303 | }, |
| 304 | health: { current: maxHp, max: maxHp } |
| 305 | }; |
| 306 | if (request.spawnerId) |
| 307 | { |
| 308 | const aggressive = request.aggressive != null |
| 309 | ? !!request.aggressive |
| 310 | : !!def.aggressive; |
| 311 | // Humanoid Aggressive flag forces bounty 0; wildlife can be aggressive and still pay kill bounty. |
| 312 | const bounty = request.aggressive === true |
| 313 | ? 0 |
| 314 | : (Math.max(0, Number(request.bounty) || 0) | 0); |
| 315 | components.spawnOrigin = { |
| 316 | spawnerId: request.spawnerId, |
| 317 | originTileX: request.originTileX ?? request.tileX, |
| 318 | originTileY: request.originTileY ?? request.tileY, |
| 319 | wanderRangeTiles: request.wanderRangeTiles ?? 4, |
| 320 | bounty, |
| 321 | level, |
| 322 | aggressive, |
| 323 | centralExchange: !!request.centralExchange, |
| 324 | townGuard: !!request.townGuard || isTownGuardMob(def.mobTypeId), |
| 325 | stationTileX: null, |
| 326 | stationTileY: null, |
| 327 | dialogueId: request.dialogueId ? String(request.dialogueId) : null, |
| 328 | drops: Array.isArray(request.drops) ? request.drops : [], |
| 329 | respawnDelaySeconds: Math.max(0, request.respawnDelaySeconds | 0) |
| 330 | }; |
| 331 | } |
| 332 | if (def.persistent) |
| 333 | { components.persistent = {}; } |
| 334 | if (def.freeFlight) |
| 335 | { |
| 336 | components.flight = { |
| 337 | x: request.tileX + 0.5, |
| 338 | y: request.tileY + 0.5, |
| 339 | targetX: request.tileX + 0.5, |
| 340 | targetY: request.tileY + 0.5, |
| 341 | speed: def.flightSpeedTilesPerSec ?? 2.5, |
| 342 | nextPickAt: 0, |
| 343 | lastAt: 0 |
| 344 | }; |
| 345 | } |
| 346 | if (mobCanFight(def)) |
| 347 | { |
| 348 | components.combat = { targetEntityId: null, nextSwingAt: 0, seekCover: false, fleeFrom: null }; |
| 349 | components.skills = { levels: mobSkillLevels(def, level) }; |
| 350 | } |
| 351 | |
| 352 | const knownSpells = normalizeMobSpells(request.spells, level); |
| 353 | if (def.randomElementSpell) |
| 354 | { |
| 355 | /** Fairies know every elemental sprite bolt and pick one at random each cast. */ |
| 356 | for (const boltId of Object.values(SPRITE_BOLT_SPELL_BY_ELEMENT)) |
| 357 | { |
| 358 | if (!knownSpells.some((entry) => entry.spellId === boltId)) |
| 359 | { knownSpells.push({ spellId: boltId }); } |
| 360 | } |
| 361 | } |
| 362 | else if (element && SPRITE_BOLT_SPELL_BY_ELEMENT[element]) |
| 363 | { |
| 364 | const boltId = SPRITE_BOLT_SPELL_BY_ELEMENT[element]; |
| 365 | if (!knownSpells.some((entry) => entry.spellId === boltId)) |
| 366 | { knownSpells.push({ spellId: boltId }); } |
| 367 | } |
| 368 | if (knownSpells.length) |
| 369 | { |
| 370 | const castChance = (element || def.randomElementSpell) ? 100 : mobSpellCastChancePercent(level); |
| 371 | components.spells = { |
| 372 | known: knownSpells.map((entry) => entry.spellId), |
| 373 | favorites: [], |
| 374 | autoCast: false, |
| 375 | castChances: Object.fromEntries(knownSpells.map((entry) => [entry.spellId, castChance])) |
| 376 | }; |
| 377 | components.spellcasting = { |
| 378 | armedSpellId: null, |
| 379 | repeatSpellId: null, |
| 380 | repeatTargetEntityId: null, |
| 381 | nextCastAt: 0, |
| 382 | pendingCast: null |
| 383 | }; |
| 384 | } |
| 385 | |
| 386 | const equipmentSlots = emptyEquipment().slots; |
| 387 | const authored = request.equipment ?? {}; |
| 388 | const defaults = def.usePlayerBody ? npcDefaultEquipment(displayName, sex) : null; |
| 389 | for (const slot of Object.keys(equipmentSlots)) |
| 390 | { |
| 391 | const authoredEntry = authored[slot]; |
| 392 | let { itemId, dyeColor } = spawnerEquipmentSlot(authoredEntry); |
| 393 | if (!itemId && defaults?.[slot]) |
| 394 | { |
| 395 | itemId = defaults[slot].itemId; |
| 396 | dyeColor = defaults[slot].dyeColor ?? null; |
| 397 | } |
| 398 | if (!itemId) |
| 399 | { continue; } |
| 400 | const itemDef = itemDefinition(itemId); |
| 401 | if (!itemDef?.equipment || itemDef.equipment.slot !== slot) |
| 402 | { continue; } |
| 403 | const stack = { itemId, quantity: 1 }; |
| 404 | if (dyeColor && itemDef.dyeable) |
| 405 | { stack.instance = { kind: "clothing", dyeColor }; } |
| 406 | equipmentSlots[slot] = stack; |
| 407 | } |
| 408 | if (def.usePlayerBody || Object.values(equipmentSlots).some(Boolean)) |
| 409 | { components.equipment = { slots: equipmentSlots }; } |
| 410 | |
| 411 | const catalog = Array.isArray(request.sells) |
| 412 | ? request.sells.map((entry) => |
| 413 | { |
| 414 | if (entry && typeof entry === "object") |
| 415 | { |
| 416 | return { |
| 417 | itemId: entry.itemId | 0, |
| 418 | quantity: Math.max(0, entry.quantity | 0 || 0) |
| 419 | }; |
| 420 | } |
| 421 | return { itemId: entry | 0, quantity: 0 }; |
| 422 | }).filter((line) => line.itemId > 0) |
| 423 | : []; |
| 424 | const buysItems = !!request.buysItems; |
| 425 | const isExchange = !!request.centralExchange || def.centralExchange; |
| 426 | if (!isExchange && (catalog.length || buysItems || def.talkable)) |
| 427 | { |
| 428 | components.shop = { |
| 429 | catalog, |
| 430 | buysItems, |
| 431 | hiddenDiscount: Number(request.hiddenDiscount) || 0.9, |
| 432 | displayName, |
| 433 | remaining: Object.create(null), |
| 434 | boughtStock: Object.create(null), |
| 435 | restockDay: null |
| 436 | }; |
| 437 | } |
| 438 | return components; |
| 439 | } |
| 440 | |
| 441 | function normalizeMobSpells (raw, level) |
| 442 | { |
| 443 | if (!Array.isArray(raw)) |
| 444 | { return []; } |
| 445 | const out = []; |
| 446 | const seen = new Set(); |
| 447 | const lv = Math.max(1, level | 0 || 1); |
| 448 | for (const entry of raw) |
| 449 | { |
| 450 | if (!entry || typeof entry !== "object") |
| 451 | { continue; } |
| 452 | const spellId = String(entry.spellId ?? "").trim(); |
| 453 | if (!spellId || seen.has(spellId)) |
| 454 | { continue; } |
| 455 | const def = spellDefinition(spellId); |
| 456 | if (!def) |
| 457 | { continue; } |
| 458 | const minLevel = spellMinMagicLevel(def); |
| 459 | if (lv < minLevel) |
| 460 | { continue; } |
| 461 | seen.add(spellId); |
| 462 | out.push({ spellId }); |
| 463 | } |
| 464 | return out; |
| 465 | } |
| 466 | |
| 467 | function collectAmbientRequests (world, reserved = new Set()) |
| 468 | { |
| 469 | const requests = []; |
| 470 | const ambientTypes = listAmbientMobTypes(); |
| 471 | if (!ambientTypes.length) |
| 472 | { return requests; } |
| 473 | |
| 474 | const loaders = world.ecs.query(Component.position, Component.chunkLoader); |
| 475 | if (!loaders.length) |
| 476 | { return requests; } |
| 477 | |
| 478 | for (const def of ambientTypes) |
| 479 | { |
| 480 | const maxPerLoader = Math.max(0, def.ambientMaxPerLoader ?? 1); |
| 481 | const alive = countCreatures(world, def.mobTypeId); |
| 482 | const budget = maxPerLoader * loaders.length; |
| 483 | if (alive >= budget) |
| 484 | { continue; } |
| 485 | |
| 486 | for (const loader of loaders) |
| 487 | { |
| 488 | if (requests.length + alive >= budget) |
| 489 | { break; } |
| 490 | const dim = normalizeDimensionId(loader.position.dimensionId ?? OVERWORLD_ID); |
| 491 | const tile = pickSpawnTile( |
| 492 | world, |
| 493 | loader.position.tileX, |
| 494 | loader.position.tileY, |
| 495 | 8, |
| 496 | !!def.flying, |
| 497 | reserved, |
| 498 | dim |
| 499 | ); |
| 500 | if (!tile) |
| 501 | { continue; } |
| 502 | reserved.add(`${tile.tileX},${tile.tileY}`); |
| 503 | requests.push({ |
| 504 | ...tile, |
| 505 | dimensionId: dim, |
| 506 | mobTypeId: def.mobTypeId, |
| 507 | spawnerId: null, |
| 508 | level: 1, |
| 509 | bounty: 0, |
| 510 | spells: [], |
| 511 | persistent: false |
| 512 | }); |
| 513 | } |
| 514 | } |
| 515 | return requests; |
| 516 | } |
| 517 | |
| 518 | function countBySpawner (world) |
| 519 | { |
| 520 | const counts = new Map(); |
| 521 | for (const entity of world.ecs.query(Component.spawnOrigin)) |
| 522 | { |
| 523 | const id = entity.spawnOrigin.spawnerId; |
| 524 | counts.set(id, (counts.get(id) ?? 0) + 1); |
| 525 | } |
| 526 | return counts; |
| 527 | } |
| 528 | |
| 529 | function staffNamesBySpawner (world) |
| 530 | { |
| 531 | const map = new Map(); |
| 532 | for (const entity of world.ecs.query(Component.spawnOrigin, Component.appearance)) |
| 533 | { |
| 534 | if (!entity.spawnOrigin?.centralExchange) |
| 535 | { continue; } |
| 536 | const id = entity.spawnOrigin.spawnerId; |
| 537 | const name = entity.appearance?.firstName; |
| 538 | if (!id || !name) |
| 539 | { continue; } |
| 540 | const set = map.get(id) ?? new Set(); |
| 541 | set.add(name); |
| 542 | map.set(id, set); |
| 543 | } |
| 544 | return map; |
| 545 | } |
| 546 | |
| 547 | function countCreatures (world, mobTypeId) |
| 548 | { |
| 549 | let count = 0; |
| 550 | for (const entity of world.ecs.query(Component.creature)) |
| 551 | { |
| 552 | if (entity.creature.mobTypeId === mobTypeId) |
| 553 | { count += 1; } |
| 554 | } |
| 555 | return count; |
| 556 | } |
| 557 | |
| 558 | function pickSpawnTile (world, originX, originY, range, flying = false, reserved = null, dimensionId = OVERWORLD_ID) |
| 559 | { |
| 560 | const dim = normalizeDimensionId(dimensionId); |
| 561 | const radius = Math.max(0, range | 0); |
| 562 | const tryTile = (tileX, tileY) => |
| 563 | { |
| 564 | const key = `${tileX},${tileY}`; |
| 565 | if (reserved?.has(key)) |
| 566 | { return null; } |
| 567 | if (!world.ownsRegion(tileToRegion(tileX), tileToRegion(tileY), dim)) |
| 568 | { return null; } |
| 569 | if (flying) |
| 570 | { |
| 571 | if (!chunkIsOpen(world, tileX, tileY, dim)) |
| 572 | { return null; } |
| 573 | return { tileX, tileY, dimensionId: dim }; |
| 574 | } |
| 575 | if (!world.isEnterable(tileX, tileY, dim) || world.occupied(tileX, tileY, dim)) |
| 576 | { return null; } |
| 577 | return { tileX, tileY, dimensionId: dim }; |
| 578 | }; |
| 579 | if (radius === 0) |
| 580 | { return tryTile(originX, originY); } |
| 581 | for (let attempt = 0; attempt < 48; attempt++) |
| 582 | { |
| 583 | const tile = tryTile( |
| 584 | originX + randInt(-radius, radius), |
| 585 | originY + randInt(-radius, radius) |
| 586 | ); |
| 587 | if (tile) |
| 588 | { return tile; } |
| 589 | } |
| 590 | return null; |
| 591 | } |
| 592 | |
| 593 | function chunkIsOpen (world, tileX, tileY, dimensionId = OVERWORLD_ID) |
| 594 | { |
| 595 | const data = world.chunkData(tileToChunk(tileX), tileToChunk(tileY), dimensionId); |
| 596 | return !!data?.open; |
| 597 | } |
| 598 | |
| 599 | function randInt (min, max) |
| 600 | { |
| 601 | return min + Math.floor(Math.random() * (max - min + 1)); |
| 602 | } |