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