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