import { Component } from "../ecs/components.js"; import { MobId } from "../../../shared/definitions/mobs.js"; import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; import { JADEITE_FAIRY_LEASH_TILES, JADEITE_FAIRY_LEVEL_FRACTION, JADEITE_FAIRY_RESPAWN_MS, summonsJadeiteFairy } from "../../../shared/equipment/jewelryEffects.js"; import { combatLevelFromSkills } from "../../../shared/combat/combatLevel.js"; import { chebyshev, engageTarget, ensureMobCombat, hasCombatLineOfSight } from "../combat/service.js"; import { mobComponents } from "../simulation/spawners.js"; import { clearFollow, setFollow } from "../simulation/follow.js"; import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js"; export const JADEITE_FAIRY_KIND = "jadeite-fairy"; function ensureFairyTimers (world) { if (!world.jadeiteFairyTimers) { world.jadeiteFairyTimers = new Map(); } return world.jadeiteFairyTimers; } /** Persist respawn-at on the character so unequip/re-equip does not clear it. */ export function noteJadeiteFairyDeath (world, summon, now = Date.now()) { const respawnAt = now + JADEITE_FAIRY_RESPAWN_MS; const characterId = summon?.ownerCharacterId | 0; if (characterId) { ensureFairyTimers(world).set(characterId, respawnAt); } const ownerId = summon?.ownerEntityId; if (ownerId == null) { return; } const ownerHandle = world.ecs.handleOf(ownerId); if (ownerHandle == null) { return; } const character = world.ecs.get(ownerHandle, Component.character); if (character) { character.jadeiteFairyRespawnAt = respawnAt; } } export function jadeiteFairyRespawnAt (world, characterId, characterComponent) { const fromCharacter = Number(characterComponent?.jadeiteFairyRespawnAt || 0); const fromWorld = Number(ensureFairyTimers(world).get(characterId) || 0); return Math.max(fromCharacter, fromWorld); } export function clearJadeiteFairyRespawnIfReady (world, characterId, characterComponent, now) { const until = jadeiteFairyRespawnAt(world, characterId, characterComponent); if (until > 0 && now >= until) { if (characterComponent) { characterComponent.jadeiteFairyRespawnAt = null; } ensureFairyTimers(world).delete(characterId); return true; } return until <= 0; } function ownerDisplayName (world, ownerHandle) { const appearance = world.ecs.get(ownerHandle, Component.appearance); const name = [appearance?.firstName, appearance?.lastName].filter(Boolean).join(" ").trim(); return name || "Adventurer"; } function fairyLevelForOwner (skills) { const combatLevel = combatLevelFromSkills(skills); return Math.max(1, Math.floor(combatLevel * JADEITE_FAIRY_LEVEL_FRACTION)); } /** Build ECS components for a jadeite-amulet fairy companion. */ export function jadeiteFairyComponents ({ tileX, tileY, level, ownerEntityId, ownerCharacterId, ownerName, dimensionId }) { const label = `${String(ownerName || "Adventurer").trim() || "Adventurer"}'s Fairy`; const components = mobComponents({ mobTypeId: MobId.fairy, tileX, tileY, level, dimensionId, displayName: label, aggressive: false }); components.summon = { kind: JADEITE_FAIRY_KIND, ownerEntityId, ownerCharacterId: ownerCharacterId ?? null }; return components; } export function isJadeiteFairySummon (summon) { return summon?.kind === JADEITE_FAIRY_KIND; } function resolveFairyTarget (world, targetId, ownerEntityId, fairyId) { if (targetId == null || targetId === ownerEntityId || targetId === fairyId) { 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; } /** Owner's target, or anything currently targeting the owner. */ function pickFairyTarget (world, fairyId, fairyPos, 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 && resolveFairyTarget(world, ownerCombat.targetEntityId, ownerEntityId, fairyId)) { return ownerCombat.targetEntityId; } for (const entity of world.ecs.query(Component.combat, Component.creature, Component.position)) { if (entity.entityId === fairyId || entity.entityId === ownerEntityId) { continue; } if (entity.combat.targetEntityId !== ownerEntityId) { continue; } if (world.ecs.has(entity.handle, Component.dead)) { continue; } const dist = chebyshev( fairyPos.tileX, fairyPos.tileY, entity.position.tileX, entity.position.tileY ); if (dist < 1) { continue; } if (!hasCombatLineOfSight( world, fairyPos.tileX, fairyPos.tileY, entity.position.tileX, entity.position.tileY, normalizeDimensionId(fairyPos.dimensionId ?? OVERWORLD_ID) )) { continue; } return entity.entityId; } return null; } function findLivingFairyForOwner (world, ownerEntityId) { for (const entity of world.ecs.query(Component.summon, Component.creature, Component.position)) { if (!isJadeiteFairySummon(entity.summon)) { continue; } if (entity.summon.ownerEntityId !== ownerEntityId) { continue; } if (world.ecs.has(entity.handle, Component.dead)) { continue; } const health = world.ecs.get(entity.handle, Component.health); if (health && health.current <= 0) { continue; } return entity; } return null; } function teleportFairyToOwner (world, fairy, ownerPos, now) { const fromTileX = fairy.position.tileX; const fromTileY = fairy.position.tileY; fairy.position.tileX = ownerPos.tileX; fairy.position.tileY = ownerPos.tileY; fairy.position.dimensionId = ownerPos.dimensionId ?? fairy.position.dimensionId; const path = world.ecs.get(fairy.handle, Component.path); if (path) { path.tiles = []; } return { type: "entity-move", entityId: fairy.entityId, fromTileX, fromTileY, tileX: fairy.position.tileX, tileY: fairy.position.tileY, dimensionId: fairy.position.dimensionId, gait: world.ecs.get(fairy.handle, Component.movement)?.gait ?? "walk", lerpMs: 50 }; } /** * Maintain jadeite-amulet fairy companions: spawn / despawn / leash / combat assist. * @returns {{ events: object[], despawnIds: number[] }} */ export function stepJadeiteFairies (world, now = Date.now()) { const events = []; const despawnIds = []; const ownersWithFairy = new Set(); for (const entity of world.ecs.query(Component.summon, Component.creature, Component.position)) { if (!isJadeiteFairySummon(entity.summon)) { continue; } const ownerEntityId = entity.summon.ownerEntityId; const ownerHandle = ownerEntityId != null ? world.ecs.handleOf(ownerEntityId) : null; const ownerDead = ownerHandle == null || world.ecs.has(ownerHandle, Component.dead); const equipment = ownerHandle != null ? world.ecs.get(ownerHandle, Component.equipment) : null; const wearing = !ownerDead && summonsJadeiteFairy(equipment); if (!wearing) { despawnIds.push(entity.entityId); continue; } ownersWithFairy.add(ownerEntityId); const characterId = entity.summon.ownerCharacterId | 0; if (characterId && world.pendingJadeiteFairySpawns) { world.pendingJadeiteFairySpawns.delete(characterId); } const ownerPos = world.ecs.get(ownerHandle, Component.position); if (!ownerPos) { continue; } const dist = chebyshev( entity.position.tileX, entity.position.tileY, ownerPos.tileX, ownerPos.tileY ); if (dist > CHUNK_SIZE) { events.push(teleportFairyToOwner(world, entity, ownerPos, now)); } else if (dist > JADEITE_FAIRY_LEASH_TILES) { setFollow(world.ecs, entity.entityId, ownerEntityId); } if (world.ecs.has(entity.handle, Component.dead)) { continue; } const combat = world.ecs.get(entity.handle, Component.combat); if (combat?.targetEntityId != null) { const stillValid = resolveFairyTarget( world, combat.targetEntityId, ownerEntityId, entity.entityId ); if (stillValid) { clearFollow(world.ecs, entity.entityId); continue; } combat.targetEntityId = null; } const targetId = pickFairyTarget(world, entity.entityId, entity.position, ownerEntityId); if (targetId != null) { clearFollow(world.ecs, entity.entityId); if (ensureMobCombat(world.ecs, entity.entityId)) { engageTarget(world.ecs, entity.entityId, targetId, now); } continue; } setFollow(world.ecs, entity.entityId, ownerEntityId); } for (const entity of world.ecs.query(Component.character, Component.equipment, Component.position, Component.skills)) { if (world.ecs.has(entity.handle, Component.dead)) { continue; } if (!summonsJadeiteFairy(entity.equipment)) { continue; } if (ownersWithFairy.has(entity.entityId)) { continue; } if (findLivingFairyForOwner(world, entity.entityId)) { continue; } const characterId = entity.character.characterId | 0; if (!clearJadeiteFairyRespawnIfReady(world, characterId, entity.character, now)) { if (jadeiteFairyRespawnAt(world, characterId, entity.character) > now) { continue; } } if (!world.pendingJadeiteFairySpawns) { world.pendingJadeiteFairySpawns = new Map(); } const pendingAt = world.pendingJadeiteFairySpawns.get(characterId); if (pendingAt != null && now - pendingAt < 2000) { continue; } world.pendingJadeiteFairySpawns.set(characterId, now); events.push({ type: "summon-entity", ownerEntityId: entity.entityId, ownerCharacterId: characterId || null, ownerName: ownerDisplayName(world, entity.handle), tileX: entity.position.tileX | 0, tileY: entity.position.tileY | 0, dimensionId: entity.position.dimensionId, level: fairyLevelForOwner(entity.skills), mobTypeId: MobId.fairy }); } return { events, despawnIds }; }