import { GAIT, CHUNK_SIZE } from "../../../shared/protocol/constants.js"; import { BlockType } from "../../../shared/definitions/tiles.js"; import { defaultSpawnerConfig, listAmbientMobTypes, mobDefinition, mobSpriteName } from "../../../shared/definitions/mobs.js"; import { Component } from "../ecs/components.js"; import { tileToChunk, tileToRegion } from "../world/coordinates.js"; export function collectSpawnRequests (world) { const requests = []; const counts = countBySpawner(world); for (const [key, chunk] of world.chunks) { if (chunk.replica || !chunk.simulating || !chunk.data.open) { continue; } const [chunkX, chunkY] = key.split(",").map(Number); for (let localY = 0; localY < CHUNK_SIZE; localY++) { for (let localX = 0; localX < CHUNK_SIZE; localX++) { const block = chunk.data.blocks[localY * CHUNK_SIZE + localX]; if (block?.typeId !== BlockType.spawner) { continue; } const config = { ...defaultSpawnerConfig(`${chunkX}:${chunkY}:${localY * CHUNK_SIZE + localX}`), ...(block.config ?? {}) }; if ((counts.get(config.spawnerId) ?? 0) >= config.maxAlive) { continue; } const originX = chunkX * CHUNK_SIZE + localX; const originY = chunkY * CHUNK_SIZE + localY; const flying = !!mobDefinition(config.mobTypeId)?.flying; const tile = pickSpawnTile(world, originX, originY, config.spawnRangeTiles, flying); if (!tile) { continue; } requests.push({ ...tile, mobTypeId: config.mobTypeId, spawnerId: config.spawnerId, persistent: !!mobDefinition(config.mobTypeId)?.persistent }); counts.set(config.spawnerId, (counts.get(config.spawnerId) ?? 0) + 1); } } } requests.push(...collectAmbientRequests(world)); return requests; } export function mobComponents (request) { const def = mobDefinition(request.mobTypeId) ?? mobDefinition("deer"); const components = { position: { tileX: request.tileX, tileY: request.tileY }, movement: { gait: def.gait ?? GAIT.walk, walkCarry: 0 }, path: { tiles: [] }, appearance: { sex: def.sex ?? "male", firstName: def.displayName, lastName: "", sprite: mobSpriteName(def.mobTypeId) }, creature: { mobTypeId: def.mobTypeId }, health: { current: def.health ?? 8, max: def.health ?? 8 } }; if (request.spawnerId) { components.spawnOrigin = { spawnerId: request.spawnerId }; } if (def.persistent) { components.persistent = {}; } if (def.freeFlight) { components.flight = { x: request.tileX + 0.5, y: request.tileY + 0.5, targetX: request.tileX + 0.5, targetY: request.tileY + 0.5, speed: def.flightSpeedTilesPerSec ?? 2.5, nextPickAt: 0, lastAt: 0 }; } if (!def.untargetable && (def.hostile || def.defaultAttack || def.skillLevels)) { components.combat = { targetEntityId: null, nextSwingAt: 0 }; components.skills = { levels: { ...(def.skillLevels ?? {}) } }; } return components; } function collectAmbientRequests (world) { const requests = []; const ambientTypes = listAmbientMobTypes(); if (!ambientTypes.length) { return requests; } const loaders = world.ecs.query(Component.position, Component.chunkLoader); if (!loaders.length) { return requests; } for (const def of ambientTypes) { const maxPerLoader = Math.max(0, def.ambientMaxPerLoader ?? 1); const alive = countCreatures(world, def.mobTypeId); const budget = maxPerLoader * loaders.length; if (alive >= budget) { continue; } for (const loader of loaders) { if (requests.length + alive >= budget) { break; } const tile = pickSpawnTile( world, loader.position.tileX, loader.position.tileY, 8, !!def.flying ); if (!tile) { continue; } requests.push({ ...tile, mobTypeId: def.mobTypeId, spawnerId: null, persistent: false }); } } return requests; } function countBySpawner (world) { const counts = new Map(); for (const entity of world.ecs.query(Component.spawnOrigin)) { const id = entity.spawnOrigin.spawnerId; counts.set(id, (counts.get(id) ?? 0) + 1); } return counts; } function countCreatures (world, mobTypeId) { let count = 0; for (const entity of world.ecs.query(Component.creature)) { if (entity.creature.mobTypeId === mobTypeId) { count += 1; } } return count; } function pickSpawnTile (world, originX, originY, range, flying = false) { const radius = Math.max(1, range | 0); for (let attempt = 0; attempt < 24; attempt++) { const tileX = originX + randInt(-radius, radius); const tileY = originY + randInt(-radius, radius); if (!world.ownsRegion(tileToRegion(tileX), tileToRegion(tileY))) { continue; } if (flying) { if (!chunkIsOpen(world, tileX, tileY)) { continue; } return { tileX, tileY }; } if (!world.isEnterable(tileX, tileY) || world.occupied(tileX, tileY)) { continue; } return { tileX, tileY }; } return null; } function chunkIsOpen (world, tileX, tileY) { const data = world.chunkData(tileToChunk(tileX), tileToChunk(tileY)); return !!data?.open; } function randInt (min, max) { return min + Math.floor(Math.random() * (max - min + 1)); }