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