BlueGit

timfa questward

Files

questward / server / src / combat / service.js

service.js

service.js 39.2 KiB Download Raw
1import { Component } from "../ecs/components.js";
2import { itemDefinition } from "../../../shared/definitions/items.js";
3import { EquipmentKind } from "../../../shared/definitions/equipmentKinds.js";
4import { mobDefinition } from "../../../shared/definitions/mobs.js";
5import {
6 mobCanFightType,
7 mobCanFight,
8 mobMeleeAttackProfile,
9 mobSkillLevels,
10 mobIsAggressive,
11 mobAggroRangeTiles
12} from "../../../shared/definitions/mobCombat.js";
13import { spellDefinition } from "../../../shared/definitions/spells.js";
14import { resolvePhysicalAttack } from "../../../shared/combat/rolls.js";
15import {
16 effectiveHitSkillInDarkness,
17 aggroDistanceMultiplier
18} from "../../../shared/combat/darkness.js";
19import {
20 bakedTileLight01,
21 entityLightAtTile,
22 tileCombatLight01,
23 tileIndexLocal
24} from "../../../shared/lighting/combatLight.js";
25import { skillLevel, SkillId, UNARMED_ATTACK } from "../../../shared/definitions/skills.js";
26import {
27 effectiveSkillLevel,
28 equipmentBonusesFromSlots
29} from "../../../shared/equipment/stats.js";
30import { statusSkillBonuses } from "../../../shared/statusEffects/rules.js";
31import { COMBAT_RANGE_TILES, RANGED_PROJECTILE_MS, SPELL_COOLDOWN_MS } from "../../../shared/spells/constants.js";
32import { awardCombatHitXp, awardCombatAvoidanceXp, awardDamageHealthXp, syncPlayerHealth } from "../skills/service.js";
33import { chunkBoundsAround, findPath } from "../navigation/astar.js";
34import { VISIBLE_CHUNK_RADIUS } from "../../../shared/protocol/constants.js";
35import { rayReaches } from "../../../shared/lighting/raycast.js";
36import { tileToChunk } from "../world/coordinates.js";
37import { stackQuantity } from "../../../shared/items/inventory.js";
38import { addGroundItem, publicGroundItem } from "../items/bags.js";
39import { resolveItemDualTint } from "../../../shared/render/tint.js";
40import { castMobAttackSpell } from "../spells/mobCast.js";
41
42export function chebyshev (ax, ay, bx, by)
43{
44 return Math.max(Math.abs(ax - bx), Math.abs(ay - by));
45}
46
47export function attackProfileOf (ecs, handle)
48{
49 const creature = ecs.get(handle, Component.creature);
50 const level = creatureLevel(ecs, handle);
51 const equipment = ecs.get(handle, Component.equipment);
52 const weapon = equipment?.slots?.weapon;
53 if (weapon?.itemId != null)
54 {
55 const attack = itemDefinition(weapon.itemId)?.equipment?.attack;
56 if (attack)
57 {
58 const profile = { ...attack };
59 if (creature)
60 { profile.mobAttackSkill = level; }
61 return profile;
62 }
63 }
64 if (creature)
65 {
66 const def = mobDefinition(creature.mobTypeId);
67 if (mobCanFightType(creature.mobTypeId))
68 { return mobMeleeAttackProfile(def, level); }
69 }
70 return { ...UNARMED_ATTACK };
71}
72
73function creatureLevel (ecs, handle)
74{
75 const creature = ecs.get(handle, Component.creature);
76 if (!creature)
77 { return 1; }
78 if (creature.level != null)
79 { return Math.max(1, creature.level | 0 || 1); }
80 const origin = ecs.get(handle, Component.spawnOrigin);
81 if (origin?.level != null)
82 { return Math.max(1, origin.level | 0 || 1); }
83 const def = mobDefinition(creature.mobTypeId);
84 return Math.max(1, def?.attackSkill | 0 || 1);
85}
86
87export function weaponEquipmentKind (ecs, handle)
88{
89 const equipment = ecs.get(handle, Component.equipment);
90 const weapon = equipment?.slots?.weapon;
91 if (!weapon?.itemId)
92 { return null; }
93 return itemDefinition(weapon.itemId)?.equipment?.kind ?? null;
94}
95
96export function ensureMobCombat (ecs, entityId)
97{
98 const handle = ecs.handleOf(entityId);
99 if (handle == null)
100 { return false; }
101 const creature = ecs.get(handle, Component.creature);
102 if (!creature || !mobCanFightType(creature.mobTypeId))
103 { return false; }
104 const def = mobDefinition(creature.mobTypeId);
105 const level = creatureLevel(ecs, handle);
106 if (!ecs.get(handle, Component.combat))
107 { ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0 }); }
108 if (!ecs.get(handle, Component.skills))
109 { ecs.add(handle, Component.skills, { levels: mobSkillLevels(def, level) }); }
110 return true;
111}
112
113function attackerSkillLevel (skills, profile, skillId)
114{
115 if (profile.mobAttackSkill != null)
116 { return profile.mobAttackSkill; }
117 return skillLevel(skills, skillId);
118}
119
120function defenderSkillLevel (skills, skillId, equipment = null)
121{
122 const bonuses = equipment ? equipmentBonusesFromSlots(equipment.slots) : {};
123 return effectiveSkillLevel(skills, skillId, bonuses);
124}
125
126function playerCombatLevels (ecs, handle, profile)
127{
128 const skills = ecs.get(handle, Component.skills);
129 const equipment = ecs.get(handle, Component.equipment);
130 const bonuses = equipmentBonusesFromSlots(equipment?.slots);
131 const status = statusSkillBonuses(ecs.get(handle, Component.statusEffects), Date.now());
132 bonuses[SkillId.strength] = (bonuses[SkillId.strength] ?? 0) + (status.strength ?? 0);
133 bonuses[SkillId.mind] = (bonuses[SkillId.mind] ?? 0) + (status.mind ?? 0);
134
135 return {
136 hitLevel: skillLevel(skills, profile.hitSkillId ?? SkillId.unarmed),
137 damageLevel: effectiveSkillLevel(skills, profile.damageSkillId ?? SkillId.strength, bonuses),
138 agilityLevel: effectiveSkillLevel(skills, SkillId.agility, bonuses),
139 defenceLevel: effectiveSkillLevel(skills, SkillId.defence, bonuses)
140 };
141}
142
143export function isUntargetable (ecs, handle)
144{
145 const creature = ecs.get(handle, Component.creature);
146 if (!creature)
147 { return false; }
148 return !!mobDefinition(creature.mobTypeId)?.untargetable;
149}
150
151export function engageTarget (ecs, attackerId, targetId, now = Date.now())
152{
153 const handle = ecs.handleOf(attackerId);
154 if (handle == null)
155 { return false; }
156 let combat = ecs.get(handle, Component.combat);
157 if (!combat)
158 { combat = ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0 }); }
159 const targetChanged = combat.targetEntityId !== targetId;
160 combat.targetEntityId = targetId;
161 if (targetChanged)
162 {
163 // New engage (or switch): allow an immediate swing; keep cadence if already on this target.
164 combat.nextSwingAt = Math.min(combat.nextSwingAt || 0, now);
165 const path = ecs.get(handle, Component.path);
166 if (path)
167 { path.tiles = []; }
168 }
169 return true;
170}
171
172export function clearEngage (ecs, entityId)
173{
174 const handle = ecs.handleOf(entityId);
175 const combat = handle != null ? ecs.get(handle, Component.combat) : null;
176 if (combat)
177 { combat.targetEntityId = null; }
178}
179
180export function hasCombatLineOfSight (world, fromX, fromY, toX, toY)
181{
182 const isClear = world.isClearForRangedAttack
183 ? (x, y) => world.isClearForRangedAttack(x, y)
184 : (x, y) => world.isEnterable(x, y);
185 return rayReaches(fromX, fromY, toX, toY, isClear);
186}
187
188/** Target-tile lighting 0–1 (baked + entity lights + time of day). */
189export function tileLightAt (world, tileX, tileY, now = Date.now())
190{
191 const data = world.chunkData?.(tileToChunk(tileX), tileToChunk(tileY));
192 const index = tileIndexLocal(tileX, tileY);
193 const baked = bakedTileLight01(data?.light, index);
194 const entity = entityLightAtTile(world.ecs, Component, tileX, tileY, now);
195 const worldTime = world.worldTime?.(now);
196 return tileCombatLight01({
197 bakedLight01: baked,
198 entityLight01: entity,
199 worldHour: worldTime?.hour ?? 12,
200 nightDarkness: world.settings?.nightDarkness ?? 0.72
201 });
202}
203
204function arrowStackInOffhand (equipment)
205{
206 const stack = equipment?.slots?.offhand;
207 if (!stack)
208 { return null; }
209 const def = itemDefinition(stack.itemId);
210 if (def?.equipment?.kind !== EquipmentKind.arrowAmmo)
211 { return null; }
212 return stack;
213}
214
215function consumeArrow (ecs, handle)
216{
217 const equipment = ecs.get(handle, Component.equipment);
218 const stack = arrowStackInOffhand(equipment);
219 if (!stack || stackQuantity(stack.quantity) <= 0)
220 { return false; }
221 if (stackQuantity(stack.quantity) <= 1)
222 { equipment.slots.offhand = null; }
223 else
224 { stack.quantity = stackQuantity(stack.quantity) - 1; }
225 return true;
226}
227
228function spawnMobDrops (world, targetHandle, tileX, tileY, now)
229{
230 const creature = world.ecs.get(targetHandle, Component.creature);
231 if (!creature)
232 { return []; }
233 const def = mobDefinition(creature.mobTypeId);
234 const events = [];
235 for (const drop of def?.drops ?? [])
236 {
237 if (Math.random() > (drop.chance ?? 1))
238 { continue; }
239 const groundItemId = (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1);
240 const item = {
241 groundItemId,
242 tileX,
243 tileY,
244 stack: { itemId: drop.itemId, quantity: drop.quantity ?? 1 },
245 droppedAt: now,
246 expiresAt: now + (world.settings?.groundItemDespawnDurationMs ?? 600_000),
247 source: "mob-drop"
248 };
249 addGroundItem(world, item);
250 events.push({
251 type: "ground-item-appear",
252 ...publicGroundItem(item),
253 stack: item.stack,
254 droppedAt: item.droppedAt,
255 source: item.source,
256 expiresAt: item.expiresAt
257 });
258 }
259 return events;
260}
261
262function projectileDualTintFromAttacker (ecs, handle)
263{
264 const equipment = ecs.get(handle, Component.equipment);
265 const stack = arrowStackInOffhand(equipment);
266 if (!stack)
267 { return null; }
268 return resolveItemDualTint(stack, itemDefinition(stack.itemId));
269}
270
271function publicDualTint (dualTint)
272{
273 if (!dualTint)
274 { return null; }
275 return {
276 shaftTint: dualTint.shaft,
277 tipTint: dualTint.tip
278 };
279}
280
281function missProjectileTile (targetTileX, targetTileY, rng = Math.random)
282{
283 const angle = rng() * Math.PI * 2;
284 const dist = rng();
285 return {
286 tileX: Math.round(targetTileX + Math.cos(angle) * dist),
287 tileY: Math.round(targetTileY + Math.sin(angle) * dist)
288 };
289}
290
291/**
292 * Aggressive mobs auto-engage the nearest player character within aggro range
293 * when they have line of sight. Does nothing while already engaged.
294 */
295export function stepAggression (world, now = Date.now())
296{
297 for (const entity of world.ecs.query(Component.creature, Component.position))
298 {
299 if (!world.chunkIsSimulating?.(entity.position.tileX, entity.position.tileY))
300 { continue; }
301 const def = mobDefinition(entity.creature.mobTypeId);
302 const origin = world.ecs.get(entity.handle, Component.spawnOrigin);
303 const aggressive = origin
304 ? !!origin.aggressive
305 : mobIsAggressive(def);
306 if (!aggressive || !mobCanFight(def))
307 { continue; }
308 if (world.ecs.has(entity.handle, Component.dead))
309 { continue; }
310 const health = world.ecs.get(entity.handle, Component.health);
311 if (health && health.current <= 0)
312 { continue; }
313 const combat = world.ecs.get(entity.handle, Component.combat);
314 if (combat?.targetEntityId != null)
315 { continue; }
316
317 const range = mobAggroRangeTiles(def);
318 let bestId = null;
319 let bestDist = Infinity;
320 for (const player of world.ecs.query(Component.character, Component.position))
321 {
322 if (world.ecs.has(player.handle, Component.dead))
323 { continue; }
324 const playerHealth = world.ecs.get(player.handle, Component.health);
325 if (playerHealth && playerHealth.current <= 0)
326 { continue; }
327 const dist = chebyshev(
328 entity.position.tileX,
329 entity.position.tileY,
330 player.position.tileX,
331 player.position.tileY
332 );
333 if (dist < 1 || dist > range)
334 { continue; }
335 let effectiveDist = dist;
336 if (dist > 1)
337 {
338 const light = tileLightAt(world, player.position.tileX, player.position.tileY);
339 effectiveDist = dist * aggroDistanceMultiplier(light);
340 }
341 if (effectiveDist > range)
342 { continue; }
343 if (!hasCombatLineOfSight(
344 world,
345 entity.position.tileX,
346 entity.position.tileY,
347 player.position.tileX,
348 player.position.tileY
349 ))
350 { continue; }
351 if (dist < bestDist)
352 {
353 bestDist = dist;
354 bestId = player.entityId;
355 }
356 }
357 if (bestId == null)
358 { continue; }
359 if (!ensureMobCombat(world.ecs, entity.entityId))
360 { continue; }
361 engageTarget(world.ecs, entity.entityId, bestId, now);
362 }
363}
364
365/** Local entity handle or a nearby ghost snapshot for cross-worker combat. */
366export function resolveCombatTarget (world, targetId)
367{
368 const handle = world.ecs.handleOf(targetId);
369 if (handle != null)
370 {
371 const pos = world.ecs.get(handle, Component.position);
372 if (!pos)
373 { return null; }
374 if (isUntargetable(world.ecs, handle))
375 { return null; }
376 return {
377 local: true,
378 handle,
379 pos,
380 health: world.ecs.get(handle, Component.health)
381 };
382 }
383 const ghost = world.ghosts?.get?.(targetId);
384 if (!ghost || ghost.untargetable)
385 { return null; }
386 const health = ghost.health != null
387 ? { current: ghost.health, max: ghost.maxHealth ?? ghost.health }
388 : null;
389 return {
390 local: false,
391 handle: null,
392 pos: { tileX: ghost.tileX, tileY: ghost.tileY },
393 health
394 };
395}
396
397function attackerCombatLevels (ecs, attackerHandle, profile)
398{
399 const attackerCharacter = ecs.get(attackerHandle, Component.character);
400 if (attackerCharacter)
401 { return playerCombatLevels(ecs, attackerHandle, profile); }
402 const skills = ecs.get(attackerHandle, Component.skills);
403 return {
404 hitLevel: attackerSkillLevel(skills, profile, profile.hitSkillId),
405 damageLevel: attackerSkillLevel(skills, profile, profile.damageSkillId),
406 agilityLevel: skillLevel(skills, SkillId.agility),
407 defenceLevel: skillLevel(skills, SkillId.defence)
408 };
409}
410
411export function stepCombat (world, now = Date.now())
412{
413 const events = [];
414 for (const entity of world.ecs.query(Component.combat, Component.position))
415 {
416 if (!world.chunkIsSimulating?.(entity.position.tileX, entity.position.tileY))
417 { continue; }
418 const combat = entity.combat;
419 const targetId = combat.targetEntityId;
420 if (targetId == null)
421 { continue; }
422 const target = resolveCombatTarget(world, targetId);
423 if (!target)
424 {
425 combat.targetEntityId = null;
426 continue;
427 }
428 if (target.health && target.health.current <= 0)
429 {
430 combat.targetEntityId = null;
431 continue;
432 }
433
434 const profile = attackProfileOf(world.ecs, entity.handle);
435 const targetPos = target.pos;
436 const isCreature = !!world.ecs.get(entity.handle, Component.creature)
437 && !world.ecs.get(entity.handle, Component.character);
438 const weaponKind = weaponEquipmentKind(world.ecs, entity.handle);
439 const isStaff = weaponKind === EquipmentKind.staff;
440
441 if (profile.mode === "ranged")
442 {
443 const dist = chebyshev(entity.position.tileX, entity.position.tileY, targetPos.tileX, targetPos.tileY);
444 const maxRange = profile.rangeTiles | 0 || COMBAT_RANGE_TILES;
445 const equipment = world.ecs.get(entity.handle, Component.equipment);
446 const hasArrows = isCreature
447 || (arrowStackInOffhand(equipment) && stackQuantity(arrowStackInOffhand(equipment).quantity) > 0);
448 const inRange = dist >= 1 && dist <= maxRange
449 && hasCombatLineOfSight(
450 world,
451 entity.position.tileX,
452 entity.position.tileY,
453 targetPos.tileX,
454 targetPos.tileY
455 );
456 if (!inRange || !hasArrows)
457 {
458 if (inRange && !hasArrows && now >= (combat.nextSwingAt || 0)
459 && world.ecs.get(entity.handle, Component.character))
460 {
461 combat.nextSwingAt = now + (profile.cooldownMs | 0 || 1200);
462 events.push({
463 type: "combat-no-arrows",
464 attackerId: entity.entityId,
465 sessionId: world.ecs.get(entity.handle, Component.networkIdentity)?.sessionId ?? null
466 });
467 }
468 queueRangedApproach(world, entity.entityId, entity.handle, entity.position, targetPos, maxRange);
469 continue;
470 }
471 clearCombatApproachPath(world.ecs, entity.handle);
472 if (now < (combat.nextSwingAt || 0))
473 { continue; }
474 combat.nextSwingAt = now + (profile.cooldownMs | 0 || 1200);
475 events.push(...resolveRangedShot(world, entity.entityId, targetId, profile, now, targetPos));
476 continue;
477 }
478
479 if (profile.mode !== "melee" && !isStaff)
480 { continue; }
481
482 const dist = chebyshev(entity.position.tileX, entity.position.tileY, targetPos.tileX, targetPos.tileY);
483 const canSwing = !isStaff
484 && dist === 1
485 && world.isClearStanding?.(entity.entityId, entity.position.tileX, entity.position.tileY);
486 const attackSpells = isCreature ? eligibleMobAttackSpells(world.ecs, entity.handle) : [];
487 const shouldCast = isCreature && attackSpells.length > 0 && (isStaff || !canSwing);
488
489 if (shouldCast)
490 {
491 const inSpellRange = dist >= 1 && dist <= COMBAT_RANGE_TILES
492 && hasCombatLineOfSight(
493 world,
494 entity.position.tileX,
495 entity.position.tileY,
496 targetPos.tileX,
497 targetPos.tileY
498 );
499 if (!inSpellRange)
500 {
501 queueStandInRangeApproach(
502 world,
503 entity.entityId,
504 entity.handle,
505 entity.position,
506 targetPos.tileX,
507 targetPos.tileY,
508 COMBAT_RANGE_TILES,
509 1
510 );
511 continue;
512 }
513 clearCombatApproachPath(world.ecs, entity.handle);
514 if (now < (combat.nextSwingAt || 0))
515 { continue; }
516 const picked = pickMobAttackSpell(attackSpells);
517 if (!picked)
518 {
519 combat.nextSwingAt = now + SPELL_COOLDOWN_MS;
520 continue;
521 }
522 combat.nextSwingAt = now + SPELL_COOLDOWN_MS;
523 events.push(...castMobAttackSpell(world, entity.entityId, targetId, picked.spellId, now));
524 continue;
525 }
526
527 if (!canSwing)
528 {
529 if (isStaff)
530 {
531 queueStandInRangeApproach(
532 world,
533 entity.entityId,
534 entity.handle,
535 entity.position,
536 targetPos.tileX,
537 targetPos.tileY,
538 COMBAT_RANGE_TILES,
539 1
540 );
541 }
542 else
543 { queueMeleeApproach(world, entity.entityId, entity.handle, entity.position, targetPos); }
544 continue;
545 }
546 clearCombatApproachPath(world.ecs, entity.handle);
547 const attackerHealth = world.ecs.get(entity.handle, Component.health);
548 if (attackerHealth && attackerHealth.current <= 0)
549 { continue; }
550 if ((combat.nextSwingAt || 0) > now)
551 { continue; }
552
553 combat.nextSwingAt = now + (profile.cooldownMs || 800);
554 events.push(...resolveMeleeSwing(world, entity.entityId, targetId, profile, now));
555 }
556 return events;
557}
558
559function eligibleMobAttackSpells (ecs, handle)
560{
561 const spells = ecs.get(handle, Component.spells);
562 if (!spells?.known?.length)
563 { return []; }
564 const level = creatureLevel(ecs, handle);
565 const chances = spells.castChances ?? {};
566 const out = [];
567 for (const spellId of spells.known)
568 {
569 const def = spellDefinition(spellId);
570 if (!def || def.target !== "entity")
571 { continue; }
572 const minLevel = Math.max(1, def.minMagicLevel | 0 || 1);
573 if (level < minLevel)
574 { continue; }
575 const chance = Math.max(0, Math.min(100, Number(chances[spellId]) || 0)) | 0;
576 if (chance <= 0)
577 { continue; }
578 out.push({ spellId, chance });
579 }
580 return out;
581}
582
583function pickMobAttackSpell (spells)
584{
585 if (!spells.length)
586 { return null; }
587 const pick = spells[Math.floor(Math.random() * spells.length)];
588 if ((Math.random() * 100) >= pick.chance)
589 { return null; }
590 return pick;
591}
592
593function clearCombatApproachPath (ecs, handle)
594{
595 const path = ecs.get(handle, Component.path);
596 if (path?.tiles?.length)
597 { path.tiles = []; }
598}
599
600export function queueMeleeApproach (world, entityId, handle, position, targetPos)
601{
602 if (world.frozen?.has?.(entityId))
603 { return; }
604 let path = world.ecs.get(handle, Component.path);
605 if (!path)
606 { path = world.ecs.add(handle, Component.path, { tiles: [] }); }
607
608 const aloneHere = world.isClearStanding?.(entityId, position.tileX, position.tileY)
609 ?? true;
610 if (chebyshev(position.tileX, position.tileY, targetPos.tileX, targetPos.tileY) === 1 && aloneHere)
611 {
612 path.tiles = [];
613 return;
614 }
615
616 const goal = bestAdjacentGoal(world, entityId, position, targetPos);
617 if (!goal)
618 {
619 path.tiles = [];
620 return;
621 }
622
623 if (goal.x === position.tileX && goal.y === position.tileY)
624 {
625 path.tiles = [];
626 return;
627 }
628
629 if (path.tiles?.length && !meleePathStale(world, entityId, position, targetPos, path, goal))
630 { return; }
631
632 const bounds = chunkBoundsAround(position.tileX, position.tileY, VISIBLE_CHUNK_RADIUS);
633 const preferVertical = !!world.ecs.get(handle, Component.character);
634 const tiles = findPath(
635 position.tileX,
636 position.tileY,
637 goal.x,
638 goal.y,
639 (x, y) =>
640 {
641 if (x === goal.x && y === goal.y)
642 {
643 return world.isClearStanding?.(entityId, x, y)
644 ?? world.walkableFor(entityId, x, y);
645 }
646 return world.walkableFor(entityId, x, y);
647 },
648 bounds.minX,
649 bounds.minY,
650 bounds.maxX,
651 bounds.maxY,
652 { preferVertical }
653 );
654 if (!tiles.length)
655 {
656 path.tiles = [];
657 return;
658 }
659 path.tiles = tiles;
660 const movement = world.ecs.get(handle, Component.movement);
661 if (movement)
662 { movement.walkCarry = 0; }
663}
664
665function standInRangePathStale (world, entityId, path, goal, targetTileX, targetTileY, maxRange, minStandDist)
666{
667 const dest = path.tiles[path.tiles.length - 1];
668 if (dest.x !== goal.x || dest.y !== goal.y)
669 { return true; }
670 const standOff = chebyshev(dest.x, dest.y, targetTileX, targetTileY);
671 if (standOff < minStandDist || standOff > maxRange)
672 { return true; }
673 if (!hasCombatLineOfSight(world, dest.x, dest.y, targetTileX, targetTileY))
674 { return true; }
675 const next = path.tiles[0];
676 if (!next)
677 { return true; }
678 return !world.walkableFor(entityId, next.x, next.y);
679}
680
681/**
682 * Path toward the nearest tile from which `targetTile` is in Chebyshev range with LOS.
683 * @param {number} minStandDist Minimum stand-off from target (0 = may stand on target tile).
684 */
685export function queueStandInRangeApproach (
686 world,
687 entityId,
688 handle,
689 position,
690 targetTileX,
691 targetTileY,
692 maxRange,
693 minStandDist = 1
694)
695{
696 if (world.frozen?.has?.(entityId))
697 { return; }
698 let path = world.ecs.get(handle, Component.path);
699 if (!path)
700 { path = world.ecs.add(handle, Component.path, { tiles: [] }); }
701
702 const distToTarget = chebyshev(position.tileX, position.tileY, targetTileX, targetTileY);
703 if (
704 distToTarget <= maxRange
705 && distToTarget >= minStandDist
706 && hasCombatLineOfSight(world, position.tileX, position.tileY, targetTileX, targetTileY)
707 )
708 {
709 path.tiles = [];
710 return;
711 }
712
713 let bestGoal = null;
714 let bestDist = Infinity;
715 for (let dy = -maxRange; dy <= maxRange; dy++)
716 {
717 for (let dx = -maxRange; dx <= maxRange; dx++)
718 {
719 const x = targetTileX + dx;
720 const y = targetTileY + dy;
721 const standOff = chebyshev(x, y, targetTileX, targetTileY);
722 if (standOff < minStandDist || standOff > maxRange)
723 { continue; }
724 if (!world.walkableFor(entityId, x, y))
725 { continue; }
726 if (!hasCombatLineOfSight(world, x, y, targetTileX, targetTileY))
727 { continue; }
728 const d = chebyshev(position.tileX, position.tileY, x, y);
729 if (d < bestDist)
730 {
731 bestDist = d;
732 bestGoal = { x, y };
733 }
734 }
735 }
736 if (!bestGoal)
737 {
738 path.tiles = [];
739 return;
740 }
741 if (bestGoal.x === position.tileX && bestGoal.y === position.tileY)
742 {
743 path.tiles = [];
744 return;
745 }
746 if (path.tiles?.length && !standInRangePathStale(
747 world,
748 entityId,
749 path,
750 bestGoal,
751 targetTileX,
752 targetTileY,
753 maxRange,
754 minStandDist
755 ))
756 { return; }
757 const bounds = chunkBoundsAround(position.tileX, position.tileY, VISIBLE_CHUNK_RADIUS);
758 const preferVertical = !!world.ecs.get(handle, Component.character);
759 const tiles = findPath(
760 position.tileX,
761 position.tileY,
762 bestGoal.x,
763 bestGoal.y,
764 (x, y) => world.walkableFor(entityId, x, y),
765 bounds.minX,
766 bounds.minY,
767 bounds.maxX,
768 bounds.maxY,
769 { preferVertical }
770 );
771 path.tiles = tiles.length ? tiles : [];
772 const movement = world.ecs.get(handle, Component.movement);
773 if (movement)
774 { movement.walkCarry = 0; }
775}
776
777function queueRangedApproach (world, entityId, handle, position, targetPos, maxRange)
778{
779 queueStandInRangeApproach(
780 world,
781 entityId,
782 handle,
783 position,
784 targetPos.tileX,
785 targetPos.tileY,
786 maxRange,
787 1
788 );
789}
790
791export function resolveRangedShot (world, attackerId, targetId, profile, now = Date.now(), targetPos)
792{
793 const events = [];
794 const attackerHandle = world.ecs.handleOf(attackerId);
795 if (attackerHandle == null)
796 { return events; }
797 const from = world.ecs.get(attackerHandle, Component.position);
798 if (!from || !targetPos)
799 { return events; }
800 const isCreature = !!world.ecs.get(attackerHandle, Component.creature)
801 && !world.ecs.get(attackerHandle, Component.character);
802 if (!isCreature && !consumeArrow(world.ecs, attackerHandle))
803 { return events; }
804
805 const snapshot = { tileX: targetPos.tileX, tileY: targetPos.tileY };
806 const target = resolveCombatTarget(world, targetId);
807 if (!target)
808 { return events; }
809
810 if (!target.local)
811 {
812 events.push({
813 type: "remote-ranged-resolve",
814 attackerId,
815 targetId,
816 profile,
817 attackerLevels: attackerCombatLevels(world.ecs, attackerHandle, profile),
818 attackerIsPlayer: !!world.ecs.get(attackerHandle, Component.character),
819 now,
820 tileX: snapshot.tileX,
821 tileY: snapshot.tileY,
822 fromTileX: from.tileX,
823 fromTileY: from.tileY,
824 ...(publicDualTint(projectileDualTintFromAttacker(world.ecs, attackerHandle)) ?? {})
825 });
826 return events;
827 }
828
829 const hitEvents = applyLocalMeleeHit(
830 world,
831 attackerId,
832 targetId,
833 attackerHandle,
834 target.handle,
835 profile,
836 now,
837 snapshot
838 );
839 const outcome = hitEvents.find((e) =>
840 e.type === "combat-hit" || e.type === "combat-miss" || e.type === "combat-block");
841 let endX = snapshot.tileX;
842 let endY = snapshot.tileY;
843 if (outcome?.type === "combat-miss")
844 {
845 const miss = missProjectileTile(snapshot.tileX, snapshot.tileY);
846 endX = miss.tileX;
847 endY = miss.tileY;
848 }
849 else
850 {
851 const currentPos = world.ecs.get(target.handle, Component.position);
852 if (currentPos)
853 {
854 endX = currentPos.tileX;
855 endY = currentPos.tileY;
856 }
857 }
858 events.push({
859 type: "combat-projectile",
860 attackerId,
861 targetId,
862 fromTileX: from.tileX,
863 fromTileY: from.tileY,
864 tileX: endX,
865 tileY: endY,
866 durationMs: RANGED_PROJECTILE_MS,
867 ...publicDualTint(projectileDualTintFromAttacker(world.ecs, attackerHandle)) ?? {}
868 });
869 return events.concat(hitEvents);
870}
871
872function meleePathStale (world, entityId, position, targetPos, path, goal)
873{
874 const dest = path.tiles[path.tiles.length - 1];
875 if (dest.x !== goal.x || dest.y !== goal.y)
876 { return true; }
877 if (chebyshev(dest.x, dest.y, targetPos.tileX, targetPos.tileY) !== 1)
878 { return true; }
879 if (!(world.isClearStanding?.(entityId, dest.x, dest.y) ?? true))
880 { return true; }
881 const next = path.tiles[0];
882 if (!next)
883 { return true; }
884 return !world.walkableFor(entityId, next.x, next.y);
885}
886
887function bestAdjacentGoal (world, entityId, position, targetPos)
888{
889 const dirs = [
890 { x: 1, y: 0 }, { x: -1, y: 0 }, { x: 0, y: 1 }, { x: 0, y: -1 },
891 { x: 1, y: 1 }, { x: 1, y: -1 }, { x: -1, y: 1 }, { x: -1, y: -1 }
892 ];
893 let best = null;
894 let bestDist = Infinity;
895 for (const dir of dirs)
896 {
897 const x = targetPos.tileX + dir.x;
898 const y = targetPos.tileY + dir.y;
899 const isHere = x === position.tileX && y === position.tileY;
900 const clear = world.isClearStanding?.(entityId, x, y)
901 ?? world.walkableFor(entityId, x, y);
902 if (!clear && !isHere)
903 { continue; }
904 if (isHere && !(world.isClearStanding?.(entityId, x, y) ?? true))
905 { continue; }
906 const dist = chebyshev(position.tileX, position.tileY, x, y);
907 if (dist < bestDist)
908 {
909 bestDist = dist;
910 best = { x, y };
911 }
912 }
913 return best;
914}
915
916export function resolveMeleeSwing (world, attackerId, targetId, profile, now = Date.now())
917{
918 const events = [];
919 const attackerHandle = world.ecs.handleOf(attackerId);
920 if (attackerHandle == null)
921 { return events; }
922 const target = resolveCombatTarget(world, targetId);
923 if (!target)
924 { return events; }
925 const from = world.ecs.get(attackerHandle, Component.position);
926 const to = target.pos;
927 if (!from || !to)
928 { return events; }
929 if (chebyshev(from.tileX, from.tileY, to.tileX, to.tileY) !== 1)
930 { return events; }
931 if (world.isClearStanding && !world.isClearStanding(attackerId, from.tileX, from.tileY))
932 { return events; }
933
934 events.push({
935 type: "combat-swing",
936 attackerId,
937 targetId,
938 tileX: to.tileX,
939 tileY: to.tileY,
940 fromTileX: from.tileX,
941 fromTileY: from.tileY
942 });
943
944 if (!target.local)
945 {
946 const attackerCharacter = world.ecs.get(attackerHandle, Component.character);
947 events.push({
948 type: "remote-melee-resolve",
949 attackerId,
950 targetId,
951 profile,
952 attackerLevels: attackerCombatLevels(world.ecs, attackerHandle, profile),
953 attackerIsPlayer: !!attackerCharacter,
954 now,
955 tileX: to.tileX,
956 tileY: to.tileY
957 });
958 return events;
959 }
960
961 return events.concat(applyLocalMeleeHit(
962 world,
963 attackerId,
964 targetId,
965 attackerHandle,
966 target.handle,
967 profile,
968 now,
969 to
970 ));
971}
972
973/** Authoritative ranged hit when the defender lives on this worker. */
974export function applyRemoteRangedShot (world, payload)
975{
976 const targetHandle = world.ecs.handleOf(payload.targetId);
977 if (targetHandle == null)
978 { return { events: [], forwards: [] }; }
979 const snapshot = {
980 tileX: payload.tileX,
981 tileY: payload.tileY
982 };
983
984 const hitEvents = applyLocalMeleeHit(
985 world,
986 payload.attackerId,
987 payload.targetId,
988 null,
989 targetHandle,
990 payload.profile,
991 payload.now ?? Date.now(),
992 snapshot,
993 payload.attackerLevels,
994 payload.attackerIsPlayer
995 );
996
997 const outcome = hitEvents.find((e) =>
998 e.type === "combat-hit" || e.type === "combat-miss" || e.type === "combat-block");
999 let endX = snapshot.tileX;
1000 let endY = snapshot.tileY;
1001 if (outcome?.type === "combat-miss")
1002 {
1003 const miss = missProjectileTile(snapshot.tileX, snapshot.tileY);
1004 endX = miss.tileX;
1005 endY = miss.tileY;
1006 }
1007 else
1008 {
1009 const currentPos = world.ecs.get(targetHandle, Component.position);
1010 if (currentPos)
1011 {
1012 endX = currentPos.tileX;
1013 endY = currentPos.tileY;
1014 }
1015 }
1016
1017 const events = [{
1018 type: "combat-projectile",
1019 attackerId: payload.attackerId,
1020 targetId: payload.targetId,
1021 fromTileX: payload.fromTileX,
1022 fromTileY: payload.fromTileY,
1023 tileX: endX,
1024 tileY: endY,
1025 durationMs: RANGED_PROJECTILE_MS,
1026 shaftTint: payload.shaftTint ?? null,
1027 tipTint: payload.tipTint ?? null
1028 }, ...hitEvents];
1029
1030 const forwards = [];
1031 const hitEvent = hitEvents.find((event) => event.type === "combat-hit");
1032 if (hitEvent && payload.attackerIsPlayer)
1033 {
1034 forwards.push({
1035 type: "forward-combat-xp",
1036 entityId: payload.attackerId,
1037 profile: payload.profile,
1038 damage: hitEvent.damage
1039 });
1040 }
1041 return { events, forwards };
1042}
1043
1044/** Authoritative hit resolution when the defender lives on this worker. */
1045export function applyRemoteMeleeSwing (world, payload)
1046{
1047 const targetHandle = world.ecs.handleOf(payload.targetId);
1048 if (targetHandle == null)
1049 { return { events: [], forwards: [] }; }
1050 const to = world.ecs.get(targetHandle, Component.position);
1051 if (!to)
1052 { return { events: [], forwards: [] }; }
1053
1054 const events = applyLocalMeleeHit(
1055 world,
1056 payload.attackerId,
1057 payload.targetId,
1058 null,
1059 targetHandle,
1060 payload.profile,
1061 payload.now ?? Date.now(),
1062 to,
1063 payload.attackerLevels,
1064 payload.attackerIsPlayer
1065 );
1066
1067 const forwards = [];
1068 const hitEvent = events.find((event) => event.type === "combat-hit");
1069 if (hitEvent && payload.attackerIsPlayer)
1070 {
1071 forwards.push({
1072 type: "forward-combat-xp",
1073 entityId: payload.attackerId,
1074 profile: payload.profile,
1075 damage: hitEvent.damage
1076 });
1077 }
1078 return { events, forwards };
1079}
1080
1081function applyLocalMeleeHit (
1082 world,
1083 attackerId,
1084 targetId,
1085 attackerHandle,
1086 targetHandle,
1087 profile,
1088 now,
1089 to,
1090 attackerLevelsOverride = null,
1091 attackerIsPlayerOverride = null
1092)
1093{
1094 const events = [];
1095 if (targetHandle == null)
1096 { return events; }
1097
1098 const attackerIsPlayer = attackerIsPlayerOverride != null
1099 ? !!attackerIsPlayerOverride
1100 : (attackerHandle != null ? !!world.ecs.get(attackerHandle, Component.character) : null);
1101
1102 // Retaliate on the first resolved swing (hit or miss), not on Attack-click engage.
1103 if (attackerIsPlayer === true && ensureMobCombat(world.ecs, targetId))
1104 { engageTarget(world.ecs, targetId, attackerId, now); }
1105 if (attackerIsPlayer === false)
1106 {
1107 const targetCharacter = world.ecs.get(targetHandle, Component.character);
1108 if (targetCharacter)
1109 {
1110 engageTarget(world.ecs, targetId, attackerId, now);
1111 const follow = world.ecs.get(targetHandle, Component.follow);
1112 if (follow)
1113 {
1114 follow.targetEntityId = null;
1115 follow.trail = [];
1116 }
1117 if (world.ecs.has(targetHandle, Component.gather))
1118 { world.ecs.remove(targetHandle, Component.gather); }
1119 }
1120 }
1121
1122 const defenderSkills = world.ecs.get(targetHandle, Component.skills);
1123 const defenderEquipment = world.ecs.get(targetHandle, Component.equipment);
1124 const attackerLevels = attackerLevelsOverride
1125 ?? (attackerHandle != null ? attackerCombatLevels(world.ecs, attackerHandle, profile) : null);
1126 if (!attackerLevels)
1127 { return events; }
1128
1129 const agilityLevel = defenderSkillLevel(
1130 defenderSkills,
1131 SkillId.agility,
1132 defenderEquipment
1133 );
1134 const defenceLevel = defenderSkillLevel(
1135 defenderSkills,
1136 SkillId.defence,
1137 defenderEquipment
1138 );
1139
1140 const resolved = resolvePhysicalAttack({
1141 hitSkillLevel: effectiveHitSkillInDarkness(
1142 attackerLevels.hitLevel,
1143 tileLightAt(world, to.tileX, to.tileY)
1144 ),
1145 agilitySkillLevel: agilityLevel,
1146 damageSkillLevel: attackerLevels.damageLevel,
1147 defenceSkillLevel: defenceLevel
1148 });
1149
1150 if (resolved.outcome === "miss")
1151 {
1152 events.push({
1153 type: "combat-miss",
1154 attackerId,
1155 targetId,
1156 tileX: to.tileX,
1157 tileY: to.tileY
1158 });
1159 const dodgeXp = awardCombatAvoidanceXp(
1160 world.ecs,
1161 targetHandle,
1162 SkillId.agility,
1163 resolved.damageRoll
1164 );
1165 if (dodgeXp)
1166 {
1167 events.push({
1168 type: "skills-updated",
1169 entityId: targetId,
1170 characterId: dodgeXp.characterId,
1171 skills: dodgeXp.view,
1172 stored: dodgeXp.stored,
1173 levelUps: dodgeXp.levelUps ?? []
1174 });
1175 }
1176 return events;
1177 }
1178
1179 if (resolved.outcome === "block")
1180 {
1181 events.push({
1182 type: "combat-block",
1183 attackerId,
1184 targetId,
1185 tileX: to.tileX,
1186 tileY: to.tileY
1187 });
1188 const blockXp = awardCombatAvoidanceXp(
1189 world.ecs,
1190 targetHandle,
1191 SkillId.defence,
1192 resolved.damageRoll
1193 );
1194 if (blockXp)
1195 {
1196 events.push({
1197 type: "skills-updated",
1198 entityId: targetId,
1199 characterId: blockXp.characterId,
1200 skills: blockXp.view,
1201 stored: blockXp.stored,
1202 levelUps: blockXp.levelUps ?? []
1203 });
1204 }
1205 return events;
1206 }
1207
1208 const damage = resolved.damage;
1209 const targetCharacter = world.ecs.get(targetHandle, Component.character);
1210 if (targetCharacter)
1211 { syncPlayerHealth(world.ecs, targetHandle); }
1212 const health = world.ecs.get(targetHandle, Component.health);
1213 if (health)
1214 {
1215 health.current = Math.max(0, (health.current | 0) - damage);
1216 }
1217 events.push({
1218 type: "combat-hit",
1219 attackerId,
1220 targetId,
1221 damage,
1222 tileX: to.tileX,
1223 tileY: to.tileY,
1224 health: health?.current ?? 0,
1225 maxHealth: health?.max ?? 0
1226 });
1227
1228 const healthXp = awardDamageHealthXp(world.ecs, targetHandle, damage);
1229 if (healthXp)
1230 {
1231 events.push({
1232 type: "skills-updated",
1233 entityId: targetId,
1234 characterId: healthXp.characterId,
1235 skills: healthXp.view,
1236 stored: healthXp.stored,
1237 levelUps: healthXp.levelUps ?? []
1238 });
1239 if (health)
1240 {
1241 const hitEvent = events[events.length - 2];
1242 if (hitEvent?.type === "combat-hit")
1243 {
1244 hitEvent.health = health.current;
1245 hitEvent.maxHealth = health.max;
1246 }
1247 }
1248 }
1249
1250 if (attackerHandle != null)
1251 {
1252 const attackerCharacter = world.ecs.get(attackerHandle, Component.character);
1253 if (attackerCharacter)
1254 {
1255 const xpAward = awardCombatHitXp(world.ecs, attackerHandle, profile, damage);
1256 if (xpAward)
1257 {
1258 events.push({
1259 type: "skills-updated",
1260 entityId: attackerId,
1261 characterId: attackerCharacter.characterId,
1262 skills: xpAward.view,
1263 stored: xpAward.stored,
1264 levelUps: xpAward.levelUps ?? []
1265 });
1266 }
1267 }
1268 }
1269
1270 if (health && health.current <= 0)
1271 {
1272 clearEngage(world.ecs, attackerId);
1273 clearEngage(world.ecs, targetId);
1274 for (const entity of world.ecs.query(Component.combat))
1275 {
1276 if (entity.combat.targetEntityId === targetId)
1277 { entity.combat.targetEntityId = null; }
1278 }
1279 if (targetCharacter && !world.ecs.has(targetHandle, Component.dead))
1280 {
1281 const path = world.ecs.get(targetHandle, Component.path);
1282 if (path)
1283 { path.tiles = []; }
1284 const follow = world.ecs.get(targetHandle, Component.follow);
1285 if (follow)
1286 {
1287 follow.targetEntityId = null;
1288 follow.trail = [];
1289 }
1290 world.ecs.add(targetHandle, Component.dead, {});
1291 const identity = world.ecs.get(targetHandle, Component.networkIdentity);
1292 events.push({
1293 type: "player-died",
1294 entityId: targetId,
1295 attackerId,
1296 characterId: targetCharacter.characterId,
1297 sessionId: identity?.sessionId ?? null,
1298 tileX: to.tileX,
1299 tileY: to.tileY,
1300 health: 0,
1301 maxHealth: health.max
1302 });
1303 }
1304 const creature = world.ecs.get(targetHandle, Component.creature);
1305 if (creature && !world.ecs.get(targetHandle, Component.character))
1306 {
1307 events.push(...finalizeMobKill(world, attackerId, targetId, targetHandle, to.tileX, to.tileY, now));
1308 }
1309 }
1310 return events;
1311}
1312
1313/**
1314 * Drops, despawn, and `mob-killed` for bounty / town-guard reaction.
1315 * Shared by melee and spell kill paths.
1316 */
1317export function finalizeMobKill (world, attackerId, targetId, targetHandle, tileX, tileY, now)
1318{
1319 const events = [];
1320 const handle = targetHandle ?? world.ecs.handleOf(targetId);
1321 if (handle == null)
1322 { return events; }
1323 const creature = world.ecs.get(handle, Component.creature);
1324 const origin = world.ecs.get(handle, Component.spawnOrigin);
1325 const appearance = world.ecs.get(handle, Component.appearance);
1326 events.push(...spawnMobDrops(world, handle, tileX, tileY, now));
1327 events.push({
1328 type: "mob-killed",
1329 entityId: targetId,
1330 attackerId,
1331 mobTypeId: creature?.mobTypeId ?? null,
1332 level: creature?.level ?? origin?.level ?? 1,
1333 bounty: Math.max(0, origin?.bounty | 0 || 0),
1334 spawnerId: origin?.spawnerId ?? null,
1335 originTileX: origin?.originTileX ?? tileX,
1336 originTileY: origin?.originTileY ?? tileY,
1337 wanderRangeTiles: origin?.wanderRangeTiles ?? 0,
1338 townGuard: !!origin?.townGuard,
1339 displayName: appearance?.firstName ?? null,
1340 tileX,
1341 tileY
1342 });
1343 const view = world.destroyEntity?.(targetId) ?? null;
1344 if (view)
1345 {
1346 events.push({
1347 type: "entity-despawn",
1348 entityId: targetId,
1349 tileX: view.tileX,
1350 tileY: view.tileY
1351 });
1352 }
1353 return events;
1354}
1355
1356/** Apply HP loss and shared death/loot side-effects (used by spell impacts). */
1357export function applyDamageAndKill (world, attackerId, targetId, damage, now = Date.now())
1358{
1359 const events = [];
1360 const targetHandle = world.ecs.handleOf(targetId);
1361 if (targetHandle == null)
1362 { return events; }
1363 const to = world.ecs.get(targetHandle, Component.position);
1364 if (!to)
1365 { return events; }
1366 const health = world.ecs.get(targetHandle, Component.health);
1367 if (health)
1368 { health.current = Math.max(0, (health.current | 0) - damage); }
1369 events.push({
1370 type: "combat-hit",
1371 attackerId,
1372 targetId,
1373 damage,
1374 tileX: to.tileX,
1375 tileY: to.tileY,
1376 health: health?.current ?? 0,
1377 maxHealth: health?.max ?? 0
1378 });
1379
1380 const attackerHandle = world.ecs.handleOf(attackerId);
1381 if (attackerHandle != null && ensureMobCombat(world.ecs, targetId))
1382 {
1383 const targetCombat = world.ecs.get(targetHandle, Component.combat);
1384 if (targetCombat && targetCombat.targetEntityId == null)
1385 { engageTarget(world.ecs, targetId, attackerId, now); }
1386 }
1387 if (attackerHandle != null && world.ecs.get(targetHandle, Component.character))
1388 {
1389 const attackerCreature = world.ecs.get(attackerHandle, Component.creature);
1390 if (attackerCreature)
1391 {
1392 engageTarget(world.ecs, targetId, attackerId, now);
1393 }
1394 }
1395
1396 const healthXp = awardDamageHealthXp(world.ecs, targetHandle, damage);
1397 if (healthXp)
1398 {
1399 events.push({
1400 type: "skills-updated",
1401 entityId: targetId,
1402 characterId: healthXp.characterId,
1403 skills: healthXp.view,
1404 stored: healthXp.stored,
1405 levelUps: healthXp.levelUps ?? []
1406 });
1407 }
1408
1409 if (health && health.current <= 0)
1410 {
1411 clearEngage(world.ecs, attackerId);
1412 clearEngage(world.ecs, targetId);
1413 for (const entity of world.ecs.query(Component.combat))
1414 {
1415 if (entity.combat.targetEntityId === targetId)
1416 { entity.combat.targetEntityId = null; }
1417 }
1418 const targetCharacter = world.ecs.get(targetHandle, Component.character);
1419 if (targetCharacter && !world.ecs.has(targetHandle, Component.dead))
1420 {
1421 const path = world.ecs.get(targetHandle, Component.path);
1422 if (path)
1423 { path.tiles = []; }
1424 world.ecs.add(targetHandle, Component.dead, {});
1425 const identity = world.ecs.get(targetHandle, Component.networkIdentity);
1426 events.push({
1427 type: "player-died",
1428 entityId: targetId,
1429 attackerId,
1430 characterId: targetCharacter.characterId,
1431 sessionId: identity?.sessionId ?? null,
1432 tileX: to.tileX,
1433 tileY: to.tileY,
1434 health: 0,
1435 maxHealth: health.max
1436 });
1437 }
1438 const creature = world.ecs.get(targetHandle, Component.creature);
1439 if (creature && !targetCharacter)
1440 { events.push(...finalizeMobKill(world, attackerId, targetId, targetHandle, to.tileX, to.tileY, now)); }
1441 }
1442 return events;
1443}