BlueGit

timfa questward

Files

questward / server / src / spells / summons.js

summons.js

summons.js 5.0 KiB Download Raw
1import { Component } from "../ecs/components.js";
2import { MobId, mobDefinition } from "../../../shared/definitions/mobs.js";
3import { mobCanFight, mobIsAggressive } from "../../../shared/definitions/mobCombat.js";
4import
5 {
6 chebyshev,
7 engageTarget,
8 ensureMobCombat,
9 hasCombatLineOfSight
10 } from "../combat/service.js";
11import { mobComponents } from "../simulation/spawners.js";
12import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js";
13
14const SHADE_AGGRO_RANGE = 12;
15
16/**
17 * Expire summons and pick combat targets for Shade companions.
18 * Jadeite fairies are handled by {@link stepJadeiteFairies}.
19 * @returns {{ events: object[], despawnIds: number[] }}
20 */
21export function stepSummons (world, now = Date.now())
22{
23 const events = [];
24 const despawnIds = [];
25 for (const entity of world.ecs.query(Component.summon, Component.creature, Component.position))
26 {
27 const { handle, entityId, summon, position } = entity;
28 if (summon?.kind === "jadeite-fairy")
29 { continue; }
30 if (summon.expiresAt == null)
31 { continue; }
32 if (now >= Number(summon.expiresAt))
33 {
34 despawnIds.push(entityId);
35 continue;
36 }
37 if (world.ecs.has(handle, Component.dead))
38 { continue; }
39 const combat = world.ecs.get(handle, Component.combat);
40 if (combat?.targetEntityId != null)
41 {
42 const stillValid = resolveSummonTarget(world, combat.targetEntityId, summon.ownerEntityId);
43 if (stillValid)
44 { continue; }
45 combat.targetEntityId = null;
46 }
47
48 const targetId = pickShadeTarget(world, entityId, handle, position, summon.ownerEntityId);
49 if (targetId == null)
50 { continue; }
51 if (!ensureMobCombat(world.ecs, entityId))
52 { continue; }
53 engageTarget(world.ecs, entityId, targetId, now);
54 }
55 return { events, despawnIds };
56}
57
58function resolveSummonTarget (world, targetId, ownerEntityId)
59{
60 if (targetId == null || targetId === ownerEntityId)
61 { return false; }
62 const handle = world.ecs.handleOf(targetId);
63 if (handle == null)
64 { return false; }
65 if (world.ecs.has(handle, Component.dead))
66 { return false; }
67 const health = world.ecs.get(handle, Component.health);
68 if (health && health.current <= 0)
69 { return false; }
70 return true;
71}
72
73function pickShadeTarget (world, shadeId, shadeHandle, shadePos, ownerEntityId)
74{
75 const ownerHandle = world.ecs.handleOf(ownerEntityId);
76 if (ownerHandle == null || world.ecs.has(ownerHandle, Component.dead))
77 { return null; }
78
79 const ownerCombat = world.ecs.get(ownerHandle, Component.combat);
80 if (ownerCombat?.targetEntityId != null
81 && ownerCombat.targetEntityId !== shadeId
82 && resolveSummonTarget(world, ownerCombat.targetEntityId, ownerEntityId))
83 { return ownerCombat.targetEntityId; }
84
85 // Anything attacking the owner.
86 for (const entity of world.ecs.query(Component.combat, Component.creature, Component.position))
87 {
88 if (entity.entityId === shadeId || entity.entityId === ownerEntityId)
89 { continue; }
90 if (entity.combat.targetEntityId !== ownerEntityId)
91 { continue; }
92 if (world.ecs.has(entity.handle, Component.dead))
93 { continue; }
94 if (!inRangeLos(world, shadePos, entity.position))
95 { continue; }
96 return entity.entityId;
97 }
98
99 // Aggressive mobs nearby.
100 let bestId = null;
101 let bestDist = Infinity;
102 for (const entity of world.ecs.query(Component.creature, Component.position))
103 {
104 if (entity.entityId === shadeId || entity.entityId === ownerEntityId)
105 { continue; }
106 if (world.ecs.has(entity.handle, Component.dead))
107 { continue; }
108 if (world.ecs.get(entity.handle, Component.summon))
109 { continue; }
110 const def = mobDefinition(entity.creature.mobTypeId);
111 const origin = world.ecs.get(entity.handle, Component.spawnOrigin);
112 const aggressive = origin ? !!origin.aggressive : mobIsAggressive(def);
113 if (!aggressive || !mobCanFight(def))
114 { continue; }
115 const dist = chebyshev(
116 shadePos.tileX,
117 shadePos.tileY,
118 entity.position.tileX,
119 entity.position.tileY
120 );
121 if (dist < 1 || dist > SHADE_AGGRO_RANGE || dist >= bestDist)
122 { continue; }
123 if (!inRangeLos(world, shadePos, entity.position))
124 { continue; }
125 bestDist = dist;
126 bestId = entity.entityId;
127 }
128 return bestId;
129}
130
131function inRangeLos (world, fromPos, toPos)
132{
133 const dist = chebyshev(fromPos.tileX, fromPos.tileY, toPos.tileX, toPos.tileY);
134 if (dist < 1 || dist > SHADE_AGGRO_RANGE)
135 { return false; }
136 return hasCombatLineOfSight(
137 world,
138 fromPos.tileX,
139 fromPos.tileY,
140 toPos.tileX,
141 toPos.tileY,
142 normalizeDimensionId(fromPos.dimensionId ?? OVERWORLD_ID)
143 );
144}
145
146/** Build ECS components for a Shade summon spawn. */
147export function shadeSummonComponents ({ tileX, tileY, level, ownerEntityId, expiresAt, dimensionId })
148{
149 const components = mobComponents({
150 mobTypeId: MobId.shade,
151 tileX,
152 tileY,
153 level,
154 dimensionId,
155 displayName: "Shade",
156 aggressive: false
157 });
158 components.summon = {
159 ownerEntityId,
160 expiresAt
161 };
162 return components;
163}
164
165export function publicSummonDespawn (entityId, view = null)
166{
167 const event = { type: "entity-despawn", entityId };
168 if (view)
169 {
170 event.tileX = view.tileX;
171 event.tileY = view.tileY;
172 event.dimensionId = view.dimensionId;
173 }
174 return event;
175}