import { Component } from "../ecs/components.js"; import { MobId, mobDefinition } from "../../../shared/definitions/mobs.js"; import { mobCanFight, mobIsAggressive } from "../../../shared/definitions/mobCombat.js"; import { chebyshev, engageTarget, ensureMobCombat, hasCombatLineOfSight } from "../combat/service.js"; import { mobComponents } from "../simulation/spawners.js"; import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js"; const SHADE_AGGRO_RANGE = 12; /** * Expire summons and pick combat targets for Shade companions. * Jadeite fairies are handled by {@link stepJadeiteFairies}. * @returns {{ events: object[], despawnIds: number[] }} */ export function stepSummons (world, now = Date.now()) { const events = []; const despawnIds = []; for (const entity of world.ecs.query(Component.summon, Component.creature, Component.position)) { const { handle, entityId, summon, position } = entity; if (summon?.kind === "jadeite-fairy") { continue; } if (summon.expiresAt == null) { continue; } if (now >= Number(summon.expiresAt)) { despawnIds.push(entityId); continue; } if (world.ecs.has(handle, Component.dead)) { continue; } const combat = world.ecs.get(handle, Component.combat); if (combat?.targetEntityId != null) { const stillValid = resolveSummonTarget(world, combat.targetEntityId, summon.ownerEntityId); if (stillValid) { continue; } combat.targetEntityId = null; } const targetId = pickShadeTarget(world, entityId, handle, position, summon.ownerEntityId); if (targetId == null) { continue; } if (!ensureMobCombat(world.ecs, entityId)) { continue; } engageTarget(world.ecs, entityId, targetId, now); } return { events, despawnIds }; } function resolveSummonTarget (world, targetId, ownerEntityId) { if (targetId == null || targetId === ownerEntityId) { return false; } const handle = world.ecs.handleOf(targetId); if (handle == null) { return false; } if (world.ecs.has(handle, Component.dead)) { return false; } const health = world.ecs.get(handle, Component.health); if (health && health.current <= 0) { return false; } return true; } function pickShadeTarget (world, shadeId, shadeHandle, shadePos, ownerEntityId) { const ownerHandle = world.ecs.handleOf(ownerEntityId); if (ownerHandle == null || world.ecs.has(ownerHandle, Component.dead)) { return null; } const ownerCombat = world.ecs.get(ownerHandle, Component.combat); if (ownerCombat?.targetEntityId != null && ownerCombat.targetEntityId !== shadeId && resolveSummonTarget(world, ownerCombat.targetEntityId, ownerEntityId)) { return ownerCombat.targetEntityId; } // Anything attacking the owner. for (const entity of world.ecs.query(Component.combat, Component.creature, Component.position)) { if (entity.entityId === shadeId || entity.entityId === ownerEntityId) { continue; } if (entity.combat.targetEntityId !== ownerEntityId) { continue; } if (world.ecs.has(entity.handle, Component.dead)) { continue; } if (!inRangeLos(world, shadePos, entity.position)) { continue; } return entity.entityId; } // Aggressive mobs nearby. let bestId = null; let bestDist = Infinity; for (const entity of world.ecs.query(Component.creature, Component.position)) { if (entity.entityId === shadeId || entity.entityId === ownerEntityId) { continue; } if (world.ecs.has(entity.handle, Component.dead)) { continue; } if (world.ecs.get(entity.handle, Component.summon)) { continue; } const def = mobDefinition(entity.creature.mobTypeId); const origin = world.ecs.get(entity.handle, Component.spawnOrigin); const aggressive = origin ? !!origin.aggressive : mobIsAggressive(def); if (!aggressive || !mobCanFight(def)) { continue; } const dist = chebyshev( shadePos.tileX, shadePos.tileY, entity.position.tileX, entity.position.tileY ); if (dist < 1 || dist > SHADE_AGGRO_RANGE || dist >= bestDist) { continue; } if (!inRangeLos(world, shadePos, entity.position)) { continue; } bestDist = dist; bestId = entity.entityId; } return bestId; } function inRangeLos (world, fromPos, toPos) { const dist = chebyshev(fromPos.tileX, fromPos.tileY, toPos.tileX, toPos.tileY); if (dist < 1 || dist > SHADE_AGGRO_RANGE) { return false; } return hasCombatLineOfSight( world, fromPos.tileX, fromPos.tileY, toPos.tileX, toPos.tileY, normalizeDimensionId(fromPos.dimensionId ?? OVERWORLD_ID) ); } /** Build ECS components for a Shade summon spawn. */ export function shadeSummonComponents ({ tileX, tileY, level, ownerEntityId, expiresAt, dimensionId }) { const components = mobComponents({ mobTypeId: MobId.shade, tileX, tileY, level, dimensionId, displayName: "Shade", aggressive: false }); components.summon = { ownerEntityId, expiresAt }; return components; } export function publicSummonDespawn (entityId, view = null) { const event = { type: "entity-despawn", entityId }; if (view) { event.tileX = view.tileX; event.tileY = view.tileY; event.dimensionId = view.dimensionId; } return event; }