BlueGit

timfa questward

Files

questward / server / src / spells / mobCast.js

mobCast.js

mobCast.js 3.4 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 const dim = normalizeDimensionId(from.dimensionId ?? OVERWORLD_ID);
71 if (!cast.success)
72 {
73 events.push({
74 type: "spell-fizzle",
75 casterId,
76 spellId,
77 fromTileX: from.tileX,
78 fromTileY: from.tileY,
79 tileX: targetPos.tileX,
80 tileY: targetPos.tileY,
81 dimensionId: dim
82 });
83 return events;
84 }
85
86 events.push({
87 type: "spell-cast-flash",
88 casterId,
89 tileX: from.tileX,
90 tileY: from.tileY,
91 radiusTiles: castFlashRadiusTiles(def.tier),
92 durationMs: CAST_FLASH_MS,
93 tier: def.tier,
94 dimensionId: dim
95 });
96
97 world.pendingSpellImpacts = world.pendingSpellImpacts ?? [];
98 world.pendingSpellImpacts.push({
99 executeAt: now + SPELL_PROJECTILE_MS,
100 casterEntityId: casterId,
101 casterCharacterId: null,
102 spellId,
103 targetTileX: targetPos.tileX,
104 targetTileY: targetPos.tileY,
105 targetEntityId: targetId,
106 dimensionId: dim,
107 levels
108 });
109
110 events.push({
111 type: "spell-projectile",
112 casterId,
113 spellId,
114 targetId,
115 fromTileX: from.tileX,
116 fromTileY: from.tileY,
117 tileX: targetPos.tileX,
118 tileY: targetPos.tileY,
119 durationMs: SPELL_PROJECTILE_MS,
120 sprite: def.projectileSprite,
121 dimensionId: dim
122 });
123 return events;
124}