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