import { Component } from "../ecs/components.js"; import { SkillId, skillLevel } from "../../../shared/definitions/skills.js"; import { spellDefinition, spellDifficulty } from "../../../shared/definitions/spells.js"; import { CAST_FLASH_MS, SPELL_COOLDOWN_MS, SPELL_PROJECTILE_MS, castFlashRadiusTiles } from "../../../shared/spells/constants.js"; import { resolveSpellCast } from "../../../shared/combat/magic.js"; import { hasCombatLineOfSight, chebyshev } from "../combat/service.js"; import { COMBAT_RANGE_TILES } from "../../../shared/spells/constants.js"; /** * Server-side attack-spell cast for mobs (no player session / inventory). * Queues the same projectile + impact pipeline as player casts. */ export function castMobAttackSpell (world, casterId, targetId, spellId, now = Date.now()) { const events = []; const handle = world.ecs.handleOf(casterId); if (handle == null) { return events; } const def = spellDefinition(spellId); if (!def || def.target !== "entity") { return events; } const from = world.ecs.get(handle, Component.position); const targetHandle = world.ecs.handleOf(targetId); const targetPos = targetHandle != null ? world.ecs.get(targetHandle, Component.position) : null; if (!from || !targetPos) { return events; } const dist = chebyshev(from.tileX, from.tileY, targetPos.tileX, targetPos.tileY); if (dist < 1 || dist > COMBAT_RANGE_TILES) { return events; } if (!hasCombatLineOfSight(world, from.tileX, from.tileY, targetPos.tileX, targetPos.tileY)) { return events; } const skills = world.ecs.get(handle, Component.skills); const levels = { magic: skillLevel(skills, SkillId.magic), mind: skillLevel(skills, SkillId.mind) }; const difficulty = spellDifficulty(def); const cast = resolveSpellCast({ magicSkillLevel: levels.magic, spellDifficulty: difficulty }); let casting = world.ecs.get(handle, Component.spellcasting); if (!casting) { casting = world.ecs.add(handle, Component.spellcasting, { armedSpellId: null, repeatSpellId: null, repeatTargetEntityId: null, nextCastAt: 0, pendingCast: null }); } casting.nextCastAt = now + SPELL_COOLDOWN_MS; if (!cast.success) { events.push({ type: "spell-fizzle", casterId, spellId, fromTileX: from.tileX, fromTileY: from.tileY, tileX: targetPos.tileX, tileY: targetPos.tileY }); return events; } events.push({ type: "spell-cast-flash", casterId, tileX: from.tileX, tileY: from.tileY, radiusTiles: castFlashRadiusTiles(def.tier), durationMs: CAST_FLASH_MS, tier: def.tier }); world.pendingSpellImpacts = world.pendingSpellImpacts ?? []; world.pendingSpellImpacts.push({ executeAt: now + SPELL_PROJECTILE_MS, casterEntityId: casterId, casterCharacterId: null, spellId, targetTileX: targetPos.tileX, targetTileY: targetPos.tileY, targetEntityId: targetId, levels }); events.push({ type: "spell-projectile", casterId, spellId, fromTileX: from.tileX, fromTileY: from.tileY, tileX: targetPos.tileX, tileY: targetPos.tileY, durationMs: SPELL_PROJECTILE_MS, sprite: def.projectileSprite }); return events; }