BlueGit

timfa questward

Files

questward / server / src / spells / mobCast.js

mobCast.js

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