BlueGit

timfa questward

Files

questward / server / src / spells / jadeiteFairy.js

jadeiteFairy.js

jadeiteFairy.js 9.5 KiB Download Raw
1import { Component } from "../ecs/components.js";
2import { MobId } from "../../../shared/definitions/mobs.js";
3import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";
4import
5 {
6 JADEITE_FAIRY_LEASH_TILES,
7 JADEITE_FAIRY_LEVEL_FRACTION,
8 JADEITE_FAIRY_RESPAWN_MS,
9 summonsJadeiteFairy
10 } from "../../../shared/equipment/jewelryEffects.js";
11import { combatLevelFromSkills } from "../../../shared/combat/combatLevel.js";
12import
13 {
14 chebyshev,
15 engageTarget,
16 ensureMobCombat,
17 hasCombatLineOfSight
18 } from "../combat/service.js";
19import { mobComponents } from "../simulation/spawners.js";
20import { clearFollow, setFollow } from "../simulation/follow.js";
21
22export const JADEITE_FAIRY_KIND = "jadeite-fairy";
23
24function ensureFairyTimers (world)
25{
26 if (!world.jadeiteFairyTimers)
27 { world.jadeiteFairyTimers = new Map(); }
28 return world.jadeiteFairyTimers;
29}
30
31/** Persist respawn-at on the character so unequip/re-equip does not clear it. */
32export function noteJadeiteFairyDeath (world, summon, now = Date.now())
33{
34 const respawnAt = now + JADEITE_FAIRY_RESPAWN_MS;
35 const characterId = summon?.ownerCharacterId | 0;
36 if (characterId)
37 { ensureFairyTimers(world).set(characterId, respawnAt); }
38 const ownerId = summon?.ownerEntityId;
39 if (ownerId == null)
40 { return; }
41 const ownerHandle = world.ecs.handleOf(ownerId);
42 if (ownerHandle == null)
43 { return; }
44 const character = world.ecs.get(ownerHandle, Component.character);
45 if (character)
46 { character.jadeiteFairyRespawnAt = respawnAt; }
47}
48
49export function jadeiteFairyRespawnAt (world, characterId, characterComponent)
50{
51 const fromCharacter = Number(characterComponent?.jadeiteFairyRespawnAt || 0);
52 const fromWorld = Number(ensureFairyTimers(world).get(characterId) || 0);
53 return Math.max(fromCharacter, fromWorld);
54}
55
56export function clearJadeiteFairyRespawnIfReady (world, characterId, characterComponent, now)
57{
58 const until = jadeiteFairyRespawnAt(world, characterId, characterComponent);
59 if (until > 0 && now >= until)
60 {
61 if (characterComponent)
62 { characterComponent.jadeiteFairyRespawnAt = null; }
63 ensureFairyTimers(world).delete(characterId);
64 return true;
65 }
66 return until <= 0;
67}
68
69function ownerDisplayName (world, ownerHandle)
70{
71 const appearance = world.ecs.get(ownerHandle, Component.appearance);
72 const name = [appearance?.firstName, appearance?.lastName].filter(Boolean).join(" ").trim();
73 return name || "Adventurer";
74}
75
76function fairyLevelForOwner (skills)
77{
78 const combatLevel = combatLevelFromSkills(skills);
79 return Math.max(1, Math.floor(combatLevel * JADEITE_FAIRY_LEVEL_FRACTION));
80}
81
82/** Build ECS components for a jadeite-amulet fairy companion. */
83export function jadeiteFairyComponents ({
84 tileX,
85 tileY,
86 level,
87 ownerEntityId,
88 ownerCharacterId,
89 ownerName,
90 dimensionId
91})
92{
93 const label = `${String(ownerName || "Adventurer").trim() || "Adventurer"}'s Fairy`;
94 const components = mobComponents({
95 mobTypeId: MobId.fairy,
96 tileX,
97 tileY,
98 level,
99 dimensionId,
100 displayName: label,
101 aggressive: false
102 });
103 components.summon = {
104 kind: JADEITE_FAIRY_KIND,
105 ownerEntityId,
106 ownerCharacterId: ownerCharacterId ?? null
107 };
108 return components;
109}
110
111export function isJadeiteFairySummon (summon)
112{
113 return summon?.kind === JADEITE_FAIRY_KIND;
114}
115
116function resolveFairyTarget (world, targetId, ownerEntityId, fairyId)
117{
118 if (targetId == null || targetId === ownerEntityId || targetId === fairyId)
119 { return false; }
120 const handle = world.ecs.handleOf(targetId);
121 if (handle == null)
122 { return false; }
123 if (world.ecs.has(handle, Component.dead))
124 { return false; }
125 const health = world.ecs.get(handle, Component.health);
126 if (health && health.current <= 0)
127 { return false; }
128 return true;
129}
130
131/** Owner's target, or anything currently targeting the owner. */
132function pickFairyTarget (world, fairyId, fairyPos, ownerEntityId)
133{
134 const ownerHandle = world.ecs.handleOf(ownerEntityId);
135 if (ownerHandle == null || world.ecs.has(ownerHandle, Component.dead))
136 { return null; }
137
138 const ownerCombat = world.ecs.get(ownerHandle, Component.combat);
139 if (ownerCombat?.targetEntityId != null
140 && resolveFairyTarget(world, ownerCombat.targetEntityId, ownerEntityId, fairyId))
141 { return ownerCombat.targetEntityId; }
142
143 for (const entity of world.ecs.query(Component.combat, Component.creature, Component.position))
144 {
145 if (entity.entityId === fairyId || entity.entityId === ownerEntityId)
146 { continue; }
147 if (entity.combat.targetEntityId !== ownerEntityId)
148 { continue; }
149 if (world.ecs.has(entity.handle, Component.dead))
150 { continue; }
151 const dist = chebyshev(
152 fairyPos.tileX,
153 fairyPos.tileY,
154 entity.position.tileX,
155 entity.position.tileY
156 );
157 if (dist < 1)
158 { continue; }
159 if (!hasCombatLineOfSight(
160 world,
161 fairyPos.tileX,
162 fairyPos.tileY,
163 entity.position.tileX,
164 entity.position.tileY
165 ))
166 { continue; }
167 return entity.entityId;
168 }
169 return null;
170}
171
172function findLivingFairyForOwner (world, ownerEntityId)
173{
174 for (const entity of world.ecs.query(Component.summon, Component.creature, Component.position))
175 {
176 if (!isJadeiteFairySummon(entity.summon))
177 { continue; }
178 if (entity.summon.ownerEntityId !== ownerEntityId)
179 { continue; }
180 if (world.ecs.has(entity.handle, Component.dead))
181 { continue; }
182 const health = world.ecs.get(entity.handle, Component.health);
183 if (health && health.current <= 0)
184 { continue; }
185 return entity;
186 }
187 return null;
188}
189
190function teleportFairyToOwner (world, fairy, ownerPos, now)
191{
192 const fromTileX = fairy.position.tileX;
193 const fromTileY = fairy.position.tileY;
194 fairy.position.tileX = ownerPos.tileX;
195 fairy.position.tileY = ownerPos.tileY;
196 fairy.position.dimensionId = ownerPos.dimensionId ?? fairy.position.dimensionId;
197 const path = world.ecs.get(fairy.handle, Component.path);
198 if (path)
199 { path.tiles = []; }
200 return {
201 type: "entity-move",
202 entityId: fairy.entityId,
203 fromTileX,
204 fromTileY,
205 tileX: fairy.position.tileX,
206 tileY: fairy.position.tileY,
207 dimensionId: fairy.position.dimensionId,
208 gait: world.ecs.get(fairy.handle, Component.movement)?.gait ?? "walk",
209 lerpMs: 50
210 };
211}
212
213/**
214 * Maintain jadeite-amulet fairy companions: spawn / despawn / leash / combat assist.
215 * @returns {{ events: object[], despawnIds: number[] }}
216 */
217export function stepJadeiteFairies (world, now = Date.now())
218{
219 const events = [];
220 const despawnIds = [];
221 const ownersWithFairy = new Set();
222
223 for (const entity of world.ecs.query(Component.summon, Component.creature, Component.position))
224 {
225 if (!isJadeiteFairySummon(entity.summon))
226 { continue; }
227 const ownerEntityId = entity.summon.ownerEntityId;
228 const ownerHandle = ownerEntityId != null ? world.ecs.handleOf(ownerEntityId) : null;
229 const ownerDead = ownerHandle == null || world.ecs.has(ownerHandle, Component.dead);
230 const equipment = ownerHandle != null ? world.ecs.get(ownerHandle, Component.equipment) : null;
231 const wearing = !ownerDead && summonsJadeiteFairy(equipment);
232
233 if (!wearing)
234 {
235 despawnIds.push(entity.entityId);
236 continue;
237 }
238
239 ownersWithFairy.add(ownerEntityId);
240 const characterId = entity.summon.ownerCharacterId | 0;
241 if (characterId && world.pendingJadeiteFairySpawns)
242 { world.pendingJadeiteFairySpawns.delete(characterId); }
243 const ownerPos = world.ecs.get(ownerHandle, Component.position);
244 if (!ownerPos)
245 { continue; }
246
247 const dist = chebyshev(
248 entity.position.tileX,
249 entity.position.tileY,
250 ownerPos.tileX,
251 ownerPos.tileY
252 );
253 if (dist > CHUNK_SIZE)
254 {
255 events.push(teleportFairyToOwner(world, entity, ownerPos, now));
256 }
257 else if (dist > JADEITE_FAIRY_LEASH_TILES)
258 {
259 setFollow(world.ecs, entity.entityId, ownerEntityId);
260 }
261
262 if (world.ecs.has(entity.handle, Component.dead))
263 { continue; }
264
265 const combat = world.ecs.get(entity.handle, Component.combat);
266 if (combat?.targetEntityId != null)
267 {
268 const stillValid = resolveFairyTarget(
269 world,
270 combat.targetEntityId,
271 ownerEntityId,
272 entity.entityId
273 );
274 if (stillValid)
275 {
276 clearFollow(world.ecs, entity.entityId);
277 continue;
278 }
279 combat.targetEntityId = null;
280 }
281
282 const targetId = pickFairyTarget(world, entity.entityId, entity.position, ownerEntityId);
283 if (targetId != null)
284 {
285 clearFollow(world.ecs, entity.entityId);
286 if (ensureMobCombat(world.ecs, entity.entityId))
287 { engageTarget(world.ecs, entity.entityId, targetId, now); }
288 continue;
289 }
290
291 setFollow(world.ecs, entity.entityId, ownerEntityId);
292 }
293
294 for (const entity of world.ecs.query(Component.character, Component.equipment, Component.position, Component.skills))
295 {
296 if (world.ecs.has(entity.handle, Component.dead))
297 { continue; }
298 if (!summonsJadeiteFairy(entity.equipment))
299 { continue; }
300 if (ownersWithFairy.has(entity.entityId))
301 { continue; }
302 if (findLivingFairyForOwner(world, entity.entityId))
303 { continue; }
304
305 const characterId = entity.character.characterId | 0;
306 if (!clearJadeiteFairyRespawnIfReady(world, characterId, entity.character, now))
307 {
308 if (jadeiteFairyRespawnAt(world, characterId, entity.character) > now)
309 { continue; }
310 }
311
312 if (!world.pendingJadeiteFairySpawns)
313 { world.pendingJadeiteFairySpawns = new Map(); }
314 const pendingAt = world.pendingJadeiteFairySpawns.get(characterId);
315 if (pendingAt != null && now - pendingAt < 2000)
316 { continue; }
317 world.pendingJadeiteFairySpawns.set(characterId, now);
318
319 events.push({
320 type: "summon-entity",
321 ownerEntityId: entity.entityId,
322 ownerCharacterId: characterId || null,
323 ownerName: ownerDisplayName(world, entity.handle),
324 tileX: entity.position.tileX | 0,
325 tileY: entity.position.tileY | 0,
326 dimensionId: entity.position.dimensionId,
327 level: fairyLevelForOwner(entity.skills),
328 mobTypeId: MobId.fairy
329 });
330 }
331
332 return { events, despawnIds };
333}