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