BlueGit

timfa questward

Files

questward / server / src / spells / jadeiteFairy.js

jadeiteFairy.js

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