| 1 | import { Component } from "../ecs/components.js"; |
| 2 | import { SkillId, skillLevel } from "../../../shared/definitions/skills.js"; |
| 3 | import { spellDefinition, spellDifficulty } from "../../../shared/definitions/spells.js"; |
| 4 | import { |
| 5 | CAST_FLASH_MS, |
| 6 | SPELL_COOLDOWN_MS, |
| 7 | SPELL_PROJECTILE_MS, |
| 8 | castFlashRadiusTiles |
| 9 | } from "../../../shared/spells/constants.js"; |
| 10 | import { resolveSpellCast } from "../../../shared/combat/magic.js"; |
| 11 | import { hasCombatLineOfSight, chebyshev } from "../combat/service.js"; |
| 12 | import { COMBAT_RANGE_TILES } from "../../../shared/spells/constants.js"; |
| 13 | |
| 14 | /** |
| 15 | * Server-side attack-spell cast for mobs (no player session / inventory). |
| 16 | * Queues the same projectile + impact pipeline as player casts. |
| 17 | */ |
| 18 | export function castMobAttackSpell (world, casterId, targetId, spellId, now = Date.now()) |
| 19 | { |
| 20 | const events = []; |
| 21 | const handle = world.ecs.handleOf(casterId); |
| 22 | if (handle == null) |
| 23 | { return events; } |
| 24 | const def = spellDefinition(spellId); |
| 25 | if (!def || def.target !== "entity") |
| 26 | { return events; } |
| 27 | const from = world.ecs.get(handle, Component.position); |
| 28 | const targetHandle = world.ecs.handleOf(targetId); |
| 29 | const targetPos = targetHandle != null ? world.ecs.get(targetHandle, Component.position) : null; |
| 30 | if (!from || !targetPos) |
| 31 | { return events; } |
| 32 | const dist = chebyshev(from.tileX, from.tileY, targetPos.tileX, targetPos.tileY); |
| 33 | if (dist < 1 || dist > COMBAT_RANGE_TILES) |
| 34 | { return events; } |
| 35 | if (!hasCombatLineOfSight(world, from.tileX, from.tileY, targetPos.tileX, targetPos.tileY)) |
| 36 | { return events; } |
| 37 | |
| 38 | const skills = world.ecs.get(handle, Component.skills); |
| 39 | const levels = { |
| 40 | magic: skillLevel(skills, SkillId.magic), |
| 41 | mind: skillLevel(skills, SkillId.mind) |
| 42 | }; |
| 43 | const difficulty = spellDifficulty(def); |
| 44 | const cast = resolveSpellCast({ |
| 45 | magicSkillLevel: levels.magic, |
| 46 | spellDifficulty: difficulty |
| 47 | }); |
| 48 | |
| 49 | let casting = world.ecs.get(handle, Component.spellcasting); |
| 50 | if (!casting) |
| 51 | { |
| 52 | casting = world.ecs.add(handle, Component.spellcasting, { |
| 53 | armedSpellId: null, |
| 54 | repeatSpellId: null, |
| 55 | repeatTargetEntityId: null, |
| 56 | nextCastAt: 0, |
| 57 | pendingCast: null |
| 58 | }); |
| 59 | } |
| 60 | casting.nextCastAt = now + SPELL_COOLDOWN_MS; |
| 61 | |
| 62 | if (!cast.success) |
| 63 | { |
| 64 | events.push({ |
| 65 | type: "spell-fizzle", |
| 66 | casterId, |
| 67 | spellId, |
| 68 | fromTileX: from.tileX, |
| 69 | fromTileY: from.tileY, |
| 70 | tileX: targetPos.tileX, |
| 71 | tileY: targetPos.tileY |
| 72 | }); |
| 73 | return events; |
| 74 | } |
| 75 | |
| 76 | events.push({ |
| 77 | type: "spell-cast-flash", |
| 78 | casterId, |
| 79 | tileX: from.tileX, |
| 80 | tileY: from.tileY, |
| 81 | radiusTiles: castFlashRadiusTiles(def.tier), |
| 82 | durationMs: CAST_FLASH_MS, |
| 83 | tier: def.tier |
| 84 | }); |
| 85 | |
| 86 | world.pendingSpellImpacts = world.pendingSpellImpacts ?? []; |
| 87 | world.pendingSpellImpacts.push({ |
| 88 | executeAt: now + SPELL_PROJECTILE_MS, |
| 89 | casterEntityId: casterId, |
| 90 | casterCharacterId: null, |
| 91 | spellId, |
| 92 | targetTileX: targetPos.tileX, |
| 93 | targetTileY: targetPos.tileY, |
| 94 | targetEntityId: targetId, |
| 95 | levels |
| 96 | }); |
| 97 | |
| 98 | events.push({ |
| 99 | type: "spell-projectile", |
| 100 | casterId, |
| 101 | spellId, |
| 102 | targetId, |
| 103 | fromTileX: from.tileX, |
| 104 | fromTileY: from.tileY, |
| 105 | tileX: targetPos.tileX, |
| 106 | tileY: targetPos.tileY, |
| 107 | durationMs: SPELL_PROJECTILE_MS, |
| 108 | sprite: def.projectileSprite |
| 109 | }); |
| 110 | return events; |
| 111 | } |