BlueGit

timfa questward

Files

questward / server / src / spells / summons.js

summons.js

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