| 1 | import { Component } from "../ecs/components.js"; |
| 2 | import { itemDefinition } from "../../../shared/definitions/items.js"; |
| 3 | import { mobDefinition } from "../../../shared/definitions/mobs.js"; |
| 4 | import { |
| 5 | mobCanFightType, |
| 6 | mobMeleeAttackProfile, |
| 7 | mobSkillLevels, |
| 8 | mobIsAggressive, |
| 9 | mobAggroRangeTiles |
| 10 | } from "../../../shared/definitions/mobCombat.js"; |
| 11 | import { resolvePhysicalAttack } from "../../../shared/combat/rolls.js"; |
| 12 | import { |
| 13 | effectiveHitSkillInDarkness, |
| 14 | aggroDistanceMultiplier |
| 15 | } from "../../../shared/combat/darkness.js"; |
| 16 | import { |
| 17 | bakedTileLight01, |
| 18 | entityLightAtTile, |
| 19 | tileCombatLight01, |
| 20 | tileIndexLocal |
| 21 | } from "../../../shared/lighting/combatLight.js"; |
| 22 | import { skillLevel, SkillId, UNARMED_ATTACK } from "../../../shared/definitions/skills.js"; |
| 23 | import { |
| 24 | effectiveSkillLevel, |
| 25 | equipmentBonusesFromSlots |
| 26 | } from "../../../shared/equipment/stats.js"; |
| 27 | import { tierStatBonus } from "../../../shared/definitions/materialTiers.js"; |
| 28 | import { EquipmentKind } from "../../../shared/definitions/equipmentKinds.js"; |
| 29 | import { statusSkillBonuses } from "../../../shared/statusEffects/rules.js"; |
| 30 | import { awardCombatHitXp, awardDamageHealthXp, syncPlayerHealth } from "../skills/service.js"; |
| 31 | import { chunkBoundsAround, findPath } from "../navigation/astar.js"; |
| 32 | import { VISIBLE_CHUNK_RADIUS } from "../../../shared/protocol/constants.js"; |
| 33 | import { rayReaches } from "../../../shared/lighting/raycast.js"; |
| 34 | import { tileToChunk } from "../world/coordinates.js"; |
| 35 | import { stackQuantity } from "../../../shared/items/inventory.js"; |
| 36 | import { addGroundItem, publicGroundItem } from "../items/bags.js"; |
| 37 | |
| 38 | export function chebyshev (ax, ay, bx, by) |
| 39 | { |
| 40 | return Math.max(Math.abs(ax - bx), Math.abs(ay - by)); |
| 41 | } |
| 42 | |
| 43 | export function attackProfileOf (ecs, handle) |
| 44 | { |
| 45 | const equipment = ecs.get(handle, Component.equipment); |
| 46 | const weapon = equipment?.slots?.weapon; |
| 47 | if (weapon?.itemId != null) |
| 48 | { |
| 49 | const attack = itemDefinition(weapon.itemId)?.equipment?.attack; |
| 50 | if (attack) |
| 51 | { return { ...attack }; } |
| 52 | } |
| 53 | const creature = ecs.get(handle, Component.creature); |
| 54 | if (creature) |
| 55 | { |
| 56 | const def = mobDefinition(creature.mobTypeId); |
| 57 | if (mobCanFightType(creature.mobTypeId)) |
| 58 | { return mobMeleeAttackProfile(def); } |
| 59 | } |
| 60 | return { ...UNARMED_ATTACK }; |
| 61 | } |
| 62 | |
| 63 | export function ensureMobCombat (ecs, entityId) |
| 64 | { |
| 65 | const handle = ecs.handleOf(entityId); |
| 66 | if (handle == null) |
| 67 | { return false; } |
| 68 | const creature = ecs.get(handle, Component.creature); |
| 69 | if (!creature || !mobCanFightType(creature.mobTypeId)) |
| 70 | { return false; } |
| 71 | const def = mobDefinition(creature.mobTypeId); |
| 72 | if (!ecs.get(handle, Component.combat)) |
| 73 | { ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0 }); } |
| 74 | if (!ecs.get(handle, Component.skills)) |
| 75 | { ecs.add(handle, Component.skills, { levels: mobSkillLevels(def) }); } |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | function attackerSkillLevel (skills, profile, skillId) |
| 80 | { |
| 81 | if (profile.mobAttackSkill != null) |
| 82 | { return profile.mobAttackSkill; } |
| 83 | return skillLevel(skills, skillId); |
| 84 | } |
| 85 | |
| 86 | function defenderSkillLevel (skills, skillId, equipment = null) |
| 87 | { |
| 88 | const bonuses = equipment ? equipmentBonusesFromSlots(equipment.slots) : {}; |
| 89 | return effectiveSkillLevel(skills, skillId, bonuses); |
| 90 | } |
| 91 | |
| 92 | function playerCombatLevels (ecs, handle, profile) |
| 93 | { |
| 94 | const skills = ecs.get(handle, Component.skills); |
| 95 | const equipment = ecs.get(handle, Component.equipment); |
| 96 | const bonuses = equipmentBonusesFromSlots(equipment?.slots); |
| 97 | const status = statusSkillBonuses(ecs.get(handle, Component.statusEffects), Date.now()); |
| 98 | bonuses[SkillId.strength] = (bonuses[SkillId.strength] ?? 0) + (status.strength ?? 0); |
| 99 | bonuses[SkillId.mind] = (bonuses[SkillId.mind] ?? 0) + (status.mind ?? 0); |
| 100 | |
| 101 | if (profile.mode === "ranged") |
| 102 | { |
| 103 | const weapon = equipment?.slots?.weapon; |
| 104 | const offhand = equipment?.slots?.offhand; |
| 105 | const bowDef = weapon ? itemDefinition(weapon.itemId) : null; |
| 106 | const arrowDef = offhand ? itemDefinition(offhand.itemId) : null; |
| 107 | const bowTier = bowDef?.equipment?.tier; |
| 108 | const arrowTier = arrowDef?.equipment?.tier; |
| 109 | return { |
| 110 | hitLevel: skillLevel(skills, SkillId.archery) + (bowTier ? tierStatBonus(bowTier) : 0), |
| 111 | damageLevel: effectiveSkillLevel(skills, SkillId.dexterity, bonuses) |
| 112 | + (arrowTier ? tierStatBonus(arrowTier) : 0), |
| 113 | agilityLevel: effectiveSkillLevel(skills, SkillId.dexterity, bonuses), |
| 114 | defenceLevel: effectiveSkillLevel(skills, SkillId.defence, bonuses) |
| 115 | }; |
| 116 | } |
| 117 | |
| 118 | return { |
| 119 | hitLevel: skillLevel(skills, profile.hitSkillId), |
| 120 | damageLevel: effectiveSkillLevel(skills, profile.damageSkillId, bonuses), |
| 121 | agilityLevel: effectiveSkillLevel(skills, SkillId.dexterity, bonuses), |
| 122 | defenceLevel: effectiveSkillLevel(skills, SkillId.defence, bonuses) |
| 123 | }; |
| 124 | } |
| 125 | |
| 126 | export function isUntargetable (ecs, handle) |
| 127 | { |
| 128 | const creature = ecs.get(handle, Component.creature); |
| 129 | if (!creature) |
| 130 | { return false; } |
| 131 | return !!mobDefinition(creature.mobTypeId)?.untargetable; |
| 132 | } |
| 133 | |
| 134 | export function engageTarget (ecs, attackerId, targetId, now = Date.now()) |
| 135 | { |
| 136 | const handle = ecs.handleOf(attackerId); |
| 137 | if (handle == null) |
| 138 | { return false; } |
| 139 | let combat = ecs.get(handle, Component.combat); |
| 140 | if (!combat) |
| 141 | { combat = ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0 }); } |
| 142 | const targetChanged = combat.targetEntityId !== targetId; |
| 143 | combat.targetEntityId = targetId; |
| 144 | if (targetChanged) |
| 145 | { |
| 146 | // New engage (or switch): allow an immediate swing; keep cadence if already on this target. |
| 147 | combat.nextSwingAt = Math.min(combat.nextSwingAt || 0, now); |
| 148 | const path = ecs.get(handle, Component.path); |
| 149 | if (path) |
| 150 | { path.tiles = []; } |
| 151 | } |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | export function clearEngage (ecs, entityId) |
| 156 | { |
| 157 | const handle = ecs.handleOf(entityId); |
| 158 | const combat = handle != null ? ecs.get(handle, Component.combat) : null; |
| 159 | if (combat) |
| 160 | { combat.targetEntityId = null; } |
| 161 | } |
| 162 | |
| 163 | export function hasCombatLineOfSight (world, fromX, fromY, toX, toY) |
| 164 | { |
| 165 | return rayReaches(fromX, fromY, toX, toY, (x, y) => world.isEnterable(x, y)); |
| 166 | } |
| 167 | |
| 168 | /** Target-tile lighting 0–1 (baked + entity lights + time of day). */ |
| 169 | export function tileLightAt (world, tileX, tileY, now = Date.now()) |
| 170 | { |
| 171 | const data = world.chunkData?.(tileToChunk(tileX), tileToChunk(tileY)); |
| 172 | const index = tileIndexLocal(tileX, tileY); |
| 173 | const baked = bakedTileLight01(data?.light, index); |
| 174 | const entity = entityLightAtTile(world.ecs, Component, tileX, tileY, now); |
| 175 | const worldTime = world.worldTime?.(now); |
| 176 | return tileCombatLight01({ |
| 177 | bakedLight01: baked, |
| 178 | entityLight01: entity, |
| 179 | worldHour: worldTime?.hour ?? 12, |
| 180 | nightDarkness: world.settings?.nightDarkness ?? 0.72 |
| 181 | }); |
| 182 | } |
| 183 | |
| 184 | function arrowStackInOffhand (equipment) |
| 185 | { |
| 186 | const stack = equipment?.slots?.offhand; |
| 187 | if (!stack) |
| 188 | { return null; } |
| 189 | const def = itemDefinition(stack.itemId); |
| 190 | if (def?.equipment?.kind !== EquipmentKind.arrowAmmo) |
| 191 | { return null; } |
| 192 | return stack; |
| 193 | } |
| 194 | |
| 195 | function consumeArrow (ecs, handle) |
| 196 | { |
| 197 | const equipment = ecs.get(handle, Component.equipment); |
| 198 | const stack = arrowStackInOffhand(equipment); |
| 199 | if (!stack || stackQuantity(stack.quantity) <= 0) |
| 200 | { return false; } |
| 201 | if (stackQuantity(stack.quantity) <= 1) |
| 202 | { equipment.slots.offhand = null; } |
| 203 | else |
| 204 | { stack.quantity = stackQuantity(stack.quantity) - 1; } |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | function spawnMobDrops (world, targetHandle, tileX, tileY, now) |
| 209 | { |
| 210 | const creature = world.ecs.get(targetHandle, Component.creature); |
| 211 | if (!creature) |
| 212 | { return []; } |
| 213 | const def = mobDefinition(creature.mobTypeId); |
| 214 | const events = []; |
| 215 | for (const drop of def?.drops ?? []) |
| 216 | { |
| 217 | if (Math.random() > (drop.chance ?? 1)) |
| 218 | { continue; } |
| 219 | const groundItemId = (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1); |
| 220 | const item = { |
| 221 | groundItemId, |
| 222 | tileX, |
| 223 | tileY, |
| 224 | stack: { itemId: drop.itemId, quantity: drop.quantity ?? 1 }, |
| 225 | droppedAt: now, |
| 226 | expiresAt: now + (world.settings?.groundItemDespawnDurationMs ?? 600_000), |
| 227 | source: "mob-drop" |
| 228 | }; |
| 229 | addGroundItem(world, item); |
| 230 | events.push({ |
| 231 | type: "ground-item-appear", |
| 232 | ...publicGroundItem(item), |
| 233 | stack: item.stack, |
| 234 | droppedAt: item.droppedAt, |
| 235 | source: item.source, |
| 236 | expiresAt: item.expiresAt |
| 237 | }); |
| 238 | } |
| 239 | return events; |
| 240 | } |
| 241 | |
| 242 | function missProjectileTile (targetTileX, targetTileY, rng = Math.random) |
| 243 | { |
| 244 | const angle = rng() * Math.PI * 2; |
| 245 | const dist = rng(); |
| 246 | return { |
| 247 | tileX: Math.round(targetTileX + Math.cos(angle) * dist), |
| 248 | tileY: Math.round(targetTileY + Math.sin(angle) * dist) |
| 249 | }; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Aggressive mobs auto-engage the nearest player character within aggro range |
| 254 | * when they have line of sight. Does nothing while already engaged. |
| 255 | */ |
| 256 | export function stepAggression (world, now = Date.now()) |
| 257 | { |
| 258 | for (const entity of world.ecs.query(Component.creature, Component.position)) |
| 259 | { |
| 260 | if (!world.chunkIsSimulating?.(entity.position.tileX, entity.position.tileY)) |
| 261 | { continue; } |
| 262 | const def = mobDefinition(entity.creature.mobTypeId); |
| 263 | if (!mobIsAggressive(def)) |
| 264 | { continue; } |
| 265 | if (world.ecs.has(entity.handle, Component.dead)) |
| 266 | { continue; } |
| 267 | const health = world.ecs.get(entity.handle, Component.health); |
| 268 | if (health && health.current <= 0) |
| 269 | { continue; } |
| 270 | const combat = world.ecs.get(entity.handle, Component.combat); |
| 271 | if (combat?.targetEntityId != null) |
| 272 | { continue; } |
| 273 | |
| 274 | const range = mobAggroRangeTiles(def); |
| 275 | let bestId = null; |
| 276 | let bestDist = Infinity; |
| 277 | for (const player of world.ecs.query(Component.character, Component.position)) |
| 278 | { |
| 279 | if (world.ecs.has(player.handle, Component.dead)) |
| 280 | { continue; } |
| 281 | const playerHealth = world.ecs.get(player.handle, Component.health); |
| 282 | if (playerHealth && playerHealth.current <= 0) |
| 283 | { continue; } |
| 284 | const dist = chebyshev( |
| 285 | entity.position.tileX, |
| 286 | entity.position.tileY, |
| 287 | player.position.tileX, |
| 288 | player.position.tileY |
| 289 | ); |
| 290 | if (dist < 1 || dist > range) |
| 291 | { continue; } |
| 292 | let effectiveDist = dist; |
| 293 | if (dist > 1) |
| 294 | { |
| 295 | const light = tileLightAt(world, player.position.tileX, player.position.tileY); |
| 296 | effectiveDist = dist * aggroDistanceMultiplier(light); |
| 297 | } |
| 298 | if (effectiveDist > range) |
| 299 | { continue; } |
| 300 | if (!hasCombatLineOfSight( |
| 301 | world, |
| 302 | entity.position.tileX, |
| 303 | entity.position.tileY, |
| 304 | player.position.tileX, |
| 305 | player.position.tileY |
| 306 | )) |
| 307 | { continue; } |
| 308 | if (dist < bestDist) |
| 309 | { |
| 310 | bestDist = dist; |
| 311 | bestId = player.entityId; |
| 312 | } |
| 313 | } |
| 314 | if (bestId == null) |
| 315 | { continue; } |
| 316 | if (!ensureMobCombat(world.ecs, entity.entityId)) |
| 317 | { continue; } |
| 318 | engageTarget(world.ecs, entity.entityId, bestId, now); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /** Local entity handle or a nearby ghost snapshot for cross-worker combat. */ |
| 323 | export function resolveCombatTarget (world, targetId) |
| 324 | { |
| 325 | const handle = world.ecs.handleOf(targetId); |
| 326 | if (handle != null) |
| 327 | { |
| 328 | const pos = world.ecs.get(handle, Component.position); |
| 329 | if (!pos) |
| 330 | { return null; } |
| 331 | if (isUntargetable(world.ecs, handle)) |
| 332 | { return null; } |
| 333 | return { |
| 334 | local: true, |
| 335 | handle, |
| 336 | pos, |
| 337 | health: world.ecs.get(handle, Component.health) |
| 338 | }; |
| 339 | } |
| 340 | const ghost = world.ghosts?.get?.(targetId); |
| 341 | if (!ghost || ghost.untargetable) |
| 342 | { return null; } |
| 343 | const health = ghost.health != null |
| 344 | ? { current: ghost.health, max: ghost.maxHealth ?? ghost.health } |
| 345 | : null; |
| 346 | return { |
| 347 | local: false, |
| 348 | handle: null, |
| 349 | pos: { tileX: ghost.tileX, tileY: ghost.tileY }, |
| 350 | health |
| 351 | }; |
| 352 | } |
| 353 | |
| 354 | function attackerCombatLevels (ecs, attackerHandle, profile) |
| 355 | { |
| 356 | const attackerCharacter = ecs.get(attackerHandle, Component.character); |
| 357 | if (attackerCharacter) |
| 358 | { return playerCombatLevels(ecs, attackerHandle, profile); } |
| 359 | const skills = ecs.get(attackerHandle, Component.skills); |
| 360 | return { |
| 361 | hitLevel: attackerSkillLevel(skills, profile, profile.hitSkillId), |
| 362 | damageLevel: attackerSkillLevel(skills, profile, profile.damageSkillId), |
| 363 | agilityLevel: skillLevel(skills, SkillId.dexterity), |
| 364 | defenceLevel: skillLevel(skills, SkillId.defence) |
| 365 | }; |
| 366 | } |
| 367 | |
| 368 | export function stepCombat (world, now = Date.now()) |
| 369 | { |
| 370 | const events = []; |
| 371 | for (const entity of world.ecs.query(Component.combat, Component.position)) |
| 372 | { |
| 373 | if (!world.chunkIsSimulating?.(entity.position.tileX, entity.position.tileY)) |
| 374 | { continue; } |
| 375 | const combat = entity.combat; |
| 376 | const targetId = combat.targetEntityId; |
| 377 | if (targetId == null) |
| 378 | { continue; } |
| 379 | const target = resolveCombatTarget(world, targetId); |
| 380 | if (!target) |
| 381 | { |
| 382 | combat.targetEntityId = null; |
| 383 | continue; |
| 384 | } |
| 385 | if (target.health && target.health.current <= 0) |
| 386 | { |
| 387 | combat.targetEntityId = null; |
| 388 | continue; |
| 389 | } |
| 390 | |
| 391 | const profile = attackProfileOf(world.ecs, entity.handle); |
| 392 | const targetPos = target.pos; |
| 393 | |
| 394 | if (profile.mode === "ranged") |
| 395 | { |
| 396 | const dist = chebyshev(entity.position.tileX, entity.position.tileY, targetPos.tileX, targetPos.tileY); |
| 397 | const maxRange = profile.rangeTiles | 0 || 8; |
| 398 | const equipment = world.ecs.get(entity.handle, Component.equipment); |
| 399 | const hasArrows = arrowStackInOffhand(equipment) && stackQuantity(arrowStackInOffhand(equipment).quantity) > 0; |
| 400 | const inRange = dist >= 1 && dist <= maxRange |
| 401 | && hasCombatLineOfSight( |
| 402 | world, |
| 403 | entity.position.tileX, |
| 404 | entity.position.tileY, |
| 405 | targetPos.tileX, |
| 406 | targetPos.tileY |
| 407 | ); |
| 408 | if (!inRange || !hasArrows) |
| 409 | { |
| 410 | if (inRange && !hasArrows && now >= (combat.nextSwingAt || 0) |
| 411 | && world.ecs.get(entity.handle, Component.character)) |
| 412 | { |
| 413 | combat.nextSwingAt = now + (profile.cooldownMs | 0 || 1200); |
| 414 | events.push({ |
| 415 | type: "combat-no-arrows", |
| 416 | attackerId: entity.entityId, |
| 417 | sessionId: world.ecs.get(entity.handle, Component.networkIdentity)?.sessionId ?? null |
| 418 | }); |
| 419 | } |
| 420 | queueRangedApproach(world, entity.entityId, entity.handle, entity.position, targetPos, maxRange); |
| 421 | continue; |
| 422 | } |
| 423 | clearCombatApproachPath(world.ecs, entity.handle); |
| 424 | if (now < (combat.nextSwingAt || 0)) |
| 425 | { continue; } |
| 426 | combat.nextSwingAt = now + (profile.cooldownMs | 0 || 1200); |
| 427 | events.push(...resolveRangedShot(world, entity.entityId, targetId, profile, now, targetPos)); |
| 428 | continue; |
| 429 | } |
| 430 | |
| 431 | if (profile.mode !== "melee") |
| 432 | { continue; } |
| 433 | |
| 434 | const dist = chebyshev(entity.position.tileX, entity.position.tileY, targetPos.tileX, targetPos.tileY); |
| 435 | const canSwing = dist === 1 |
| 436 | && world.isClearStanding?.(entity.entityId, entity.position.tileX, entity.position.tileY); |
| 437 | if (!canSwing) |
| 438 | { |
| 439 | queueMeleeApproach(world, entity.entityId, entity.handle, entity.position, targetPos); |
| 440 | continue; |
| 441 | } |
| 442 | clearCombatApproachPath(world.ecs, entity.handle); |
| 443 | const attackerHealth = world.ecs.get(entity.handle, Component.health); |
| 444 | if (attackerHealth && attackerHealth.current <= 0) |
| 445 | { continue; } |
| 446 | if ((combat.nextSwingAt || 0) > now) |
| 447 | { continue; } |
| 448 | |
| 449 | combat.nextSwingAt = now + (profile.cooldownMs || 800); |
| 450 | events.push(...resolveMeleeSwing(world, entity.entityId, targetId, profile, now)); |
| 451 | } |
| 452 | return events; |
| 453 | } |
| 454 | |
| 455 | function clearCombatApproachPath (ecs, handle) |
| 456 | { |
| 457 | const path = ecs.get(handle, Component.path); |
| 458 | if (path?.tiles?.length) |
| 459 | { path.tiles = []; } |
| 460 | } |
| 461 | |
| 462 | function queueMeleeApproach (world, entityId, handle, position, targetPos) |
| 463 | { |
| 464 | if (world.frozen?.has?.(entityId)) |
| 465 | { return; } |
| 466 | let path = world.ecs.get(handle, Component.path); |
| 467 | if (!path) |
| 468 | { path = world.ecs.add(handle, Component.path, { tiles: [] }); } |
| 469 | |
| 470 | const aloneHere = world.isClearStanding?.(entityId, position.tileX, position.tileY) |
| 471 | ?? true; |
| 472 | if (chebyshev(position.tileX, position.tileY, targetPos.tileX, targetPos.tileY) === 1 && aloneHere) |
| 473 | { |
| 474 | path.tiles = []; |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | const goal = bestAdjacentGoal(world, entityId, position, targetPos); |
| 479 | if (!goal) |
| 480 | { |
| 481 | path.tiles = []; |
| 482 | return; |
| 483 | } |
| 484 | |
| 485 | if (goal.x === position.tileX && goal.y === position.tileY) |
| 486 | { |
| 487 | path.tiles = []; |
| 488 | return; |
| 489 | } |
| 490 | |
| 491 | if (path.tiles?.length && !meleePathStale(world, entityId, position, targetPos, path, goal)) |
| 492 | { return; } |
| 493 | |
| 494 | const bounds = chunkBoundsAround(position.tileX, position.tileY, VISIBLE_CHUNK_RADIUS); |
| 495 | const preferVertical = !!world.ecs.get(handle, Component.character); |
| 496 | const tiles = findPath( |
| 497 | position.tileX, |
| 498 | position.tileY, |
| 499 | goal.x, |
| 500 | goal.y, |
| 501 | (x, y) => |
| 502 | { |
| 503 | if (x === goal.x && y === goal.y) |
| 504 | { |
| 505 | return world.isClearStanding?.(entityId, x, y) |
| 506 | ?? world.walkableFor(entityId, x, y); |
| 507 | } |
| 508 | return world.walkableFor(entityId, x, y); |
| 509 | }, |
| 510 | bounds.minX, |
| 511 | bounds.minY, |
| 512 | bounds.maxX, |
| 513 | bounds.maxY, |
| 514 | { preferVertical } |
| 515 | ); |
| 516 | if (!tiles.length) |
| 517 | { |
| 518 | path.tiles = []; |
| 519 | return; |
| 520 | } |
| 521 | path.tiles = tiles; |
| 522 | const movement = world.ecs.get(handle, Component.movement); |
| 523 | if (movement) |
| 524 | { movement.walkCarry = 0; } |
| 525 | } |
| 526 | |
| 527 | function queueRangedApproach (world, entityId, handle, position, targetPos, maxRange) |
| 528 | { |
| 529 | if (world.frozen?.has?.(entityId)) |
| 530 | { return; } |
| 531 | let path = world.ecs.get(handle, Component.path); |
| 532 | if (!path) |
| 533 | { path = world.ecs.add(handle, Component.path, { tiles: [] }); } |
| 534 | |
| 535 | const dist = chebyshev(position.tileX, position.tileY, targetPos.tileX, targetPos.tileY); |
| 536 | if (dist >= 1 && dist <= maxRange && hasCombatLineOfSight( |
| 537 | world, position.tileX, position.tileY, targetPos.tileX, targetPos.tileY |
| 538 | )) |
| 539 | { |
| 540 | path.tiles = []; |
| 541 | return; |
| 542 | } |
| 543 | |
| 544 | let bestGoal = null; |
| 545 | let bestDist = Infinity; |
| 546 | for (let dy = -maxRange; dy <= maxRange; dy++) |
| 547 | { |
| 548 | for (let dx = -maxRange; dx <= maxRange; dx++) |
| 549 | { |
| 550 | const x = targetPos.tileX + dx; |
| 551 | const y = targetPos.tileY + dy; |
| 552 | const toTarget = chebyshev(x, y, targetPos.tileX, targetPos.tileY); |
| 553 | if (toTarget < 1 || toTarget > maxRange) |
| 554 | { continue; } |
| 555 | if (!world.walkableFor(entityId, x, y)) |
| 556 | { continue; } |
| 557 | if (!hasCombatLineOfSight(world, x, y, targetPos.tileX, targetPos.tileY)) |
| 558 | { continue; } |
| 559 | const d = chebyshev(position.tileX, position.tileY, x, y); |
| 560 | if (d < bestDist) |
| 561 | { |
| 562 | bestDist = d; |
| 563 | bestGoal = { x, y }; |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | if (!bestGoal) |
| 568 | { |
| 569 | path.tiles = []; |
| 570 | return; |
| 571 | } |
| 572 | if (bestGoal.x === position.tileX && bestGoal.y === position.tileY) |
| 573 | { |
| 574 | path.tiles = []; |
| 575 | return; |
| 576 | } |
| 577 | const bounds = chunkBoundsAround(position.tileX, position.tileY, VISIBLE_CHUNK_RADIUS); |
| 578 | const preferVertical = !!world.ecs.get(handle, Component.character); |
| 579 | const tiles = findPath( |
| 580 | position.tileX, |
| 581 | position.tileY, |
| 582 | bestGoal.x, |
| 583 | bestGoal.y, |
| 584 | (x, y) => world.walkableFor(entityId, x, y), |
| 585 | bounds.minX, |
| 586 | bounds.minY, |
| 587 | bounds.maxX, |
| 588 | bounds.maxY, |
| 589 | { preferVertical } |
| 590 | ); |
| 591 | path.tiles = tiles.length ? tiles : []; |
| 592 | const movement = world.ecs.get(handle, Component.movement); |
| 593 | if (movement) |
| 594 | { movement.walkCarry = 0; } |
| 595 | } |
| 596 | |
| 597 | export function resolveRangedShot (world, attackerId, targetId, profile, now = Date.now(), targetPos) |
| 598 | { |
| 599 | const events = []; |
| 600 | const attackerHandle = world.ecs.handleOf(attackerId); |
| 601 | if (attackerHandle == null) |
| 602 | { return events; } |
| 603 | const from = world.ecs.get(attackerHandle, Component.position); |
| 604 | if (!from || !targetPos) |
| 605 | { return events; } |
| 606 | if (!consumeArrow(world.ecs, attackerHandle)) |
| 607 | { return events; } |
| 608 | |
| 609 | const snapshot = { tileX: targetPos.tileX, tileY: targetPos.tileY }; |
| 610 | const target = resolveCombatTarget(world, targetId); |
| 611 | if (!target) |
| 612 | { return events; } |
| 613 | |
| 614 | if (!target.local) |
| 615 | { |
| 616 | events.push({ |
| 617 | type: "remote-ranged-resolve", |
| 618 | attackerId, |
| 619 | targetId, |
| 620 | profile, |
| 621 | attackerLevels: attackerCombatLevels(world.ecs, attackerHandle, profile), |
| 622 | attackerIsPlayer: !!world.ecs.get(attackerHandle, Component.character), |
| 623 | now, |
| 624 | tileX: snapshot.tileX, |
| 625 | tileY: snapshot.tileY, |
| 626 | fromTileX: from.tileX, |
| 627 | fromTileY: from.tileY |
| 628 | }); |
| 629 | return events; |
| 630 | } |
| 631 | |
| 632 | const hitEvents = applyLocalMeleeHit( |
| 633 | world, |
| 634 | attackerId, |
| 635 | targetId, |
| 636 | attackerHandle, |
| 637 | target.handle, |
| 638 | profile, |
| 639 | now, |
| 640 | snapshot |
| 641 | ); |
| 642 | const outcome = hitEvents.find((e) => |
| 643 | e.type === "combat-hit" || e.type === "combat-miss" || e.type === "combat-block"); |
| 644 | let endX = snapshot.tileX; |
| 645 | let endY = snapshot.tileY; |
| 646 | if (outcome?.type === "combat-miss") |
| 647 | { |
| 648 | const miss = missProjectileTile(snapshot.tileX, snapshot.tileY); |
| 649 | endX = miss.tileX; |
| 650 | endY = miss.tileY; |
| 651 | } |
| 652 | else |
| 653 | { |
| 654 | const currentPos = world.ecs.get(target.handle, Component.position); |
| 655 | if (currentPos) |
| 656 | { |
| 657 | endX = currentPos.tileX; |
| 658 | endY = currentPos.tileY; |
| 659 | } |
| 660 | } |
| 661 | events.push({ |
| 662 | type: "combat-projectile", |
| 663 | attackerId, |
| 664 | targetId, |
| 665 | fromTileX: from.tileX, |
| 666 | fromTileY: from.tileY, |
| 667 | tileX: endX, |
| 668 | tileY: endY, |
| 669 | durationMs: 400 |
| 670 | }); |
| 671 | return events.concat(hitEvents); |
| 672 | } |
| 673 | |
| 674 | function meleePathStale (world, entityId, position, targetPos, path, goal) |
| 675 | { |
| 676 | const dest = path.tiles[path.tiles.length - 1]; |
| 677 | if (dest.x !== goal.x || dest.y !== goal.y) |
| 678 | { return true; } |
| 679 | if (chebyshev(dest.x, dest.y, targetPos.tileX, targetPos.tileY) !== 1) |
| 680 | { return true; } |
| 681 | if (!(world.isClearStanding?.(entityId, dest.x, dest.y) ?? true)) |
| 682 | { return true; } |
| 683 | const next = path.tiles[0]; |
| 684 | if (!next) |
| 685 | { return true; } |
| 686 | return !world.walkableFor(entityId, next.x, next.y); |
| 687 | } |
| 688 | |
| 689 | function bestAdjacentGoal (world, entityId, position, targetPos) |
| 690 | { |
| 691 | const dirs = [ |
| 692 | { x: 1, y: 0 }, { x: -1, y: 0 }, { x: 0, y: 1 }, { x: 0, y: -1 }, |
| 693 | { x: 1, y: 1 }, { x: 1, y: -1 }, { x: -1, y: 1 }, { x: -1, y: -1 } |
| 694 | ]; |
| 695 | let best = null; |
| 696 | let bestDist = Infinity; |
| 697 | for (const dir of dirs) |
| 698 | { |
| 699 | const x = targetPos.tileX + dir.x; |
| 700 | const y = targetPos.tileY + dir.y; |
| 701 | const isHere = x === position.tileX && y === position.tileY; |
| 702 | const clear = world.isClearStanding?.(entityId, x, y) |
| 703 | ?? world.walkableFor(entityId, x, y); |
| 704 | if (!clear && !isHere) |
| 705 | { continue; } |
| 706 | if (isHere && !(world.isClearStanding?.(entityId, x, y) ?? true)) |
| 707 | { continue; } |
| 708 | const dist = chebyshev(position.tileX, position.tileY, x, y); |
| 709 | if (dist < bestDist) |
| 710 | { |
| 711 | bestDist = dist; |
| 712 | best = { x, y }; |
| 713 | } |
| 714 | } |
| 715 | return best; |
| 716 | } |
| 717 | |
| 718 | export function resolveMeleeSwing (world, attackerId, targetId, profile, now = Date.now()) |
| 719 | { |
| 720 | const events = []; |
| 721 | const attackerHandle = world.ecs.handleOf(attackerId); |
| 722 | if (attackerHandle == null) |
| 723 | { return events; } |
| 724 | const target = resolveCombatTarget(world, targetId); |
| 725 | if (!target) |
| 726 | { return events; } |
| 727 | const from = world.ecs.get(attackerHandle, Component.position); |
| 728 | const to = target.pos; |
| 729 | if (!from || !to) |
| 730 | { return events; } |
| 731 | if (chebyshev(from.tileX, from.tileY, to.tileX, to.tileY) !== 1) |
| 732 | { return events; } |
| 733 | if (world.isClearStanding && !world.isClearStanding(attackerId, from.tileX, from.tileY)) |
| 734 | { return events; } |
| 735 | |
| 736 | events.push({ |
| 737 | type: "combat-swing", |
| 738 | attackerId, |
| 739 | targetId, |
| 740 | tileX: to.tileX, |
| 741 | tileY: to.tileY, |
| 742 | fromTileX: from.tileX, |
| 743 | fromTileY: from.tileY |
| 744 | }); |
| 745 | |
| 746 | if (!target.local) |
| 747 | { |
| 748 | const attackerCharacter = world.ecs.get(attackerHandle, Component.character); |
| 749 | events.push({ |
| 750 | type: "remote-melee-resolve", |
| 751 | attackerId, |
| 752 | targetId, |
| 753 | profile, |
| 754 | attackerLevels: attackerCombatLevels(world.ecs, attackerHandle, profile), |
| 755 | attackerIsPlayer: !!attackerCharacter, |
| 756 | now, |
| 757 | tileX: to.tileX, |
| 758 | tileY: to.tileY |
| 759 | }); |
| 760 | return events; |
| 761 | } |
| 762 | |
| 763 | return events.concat(applyLocalMeleeHit( |
| 764 | world, |
| 765 | attackerId, |
| 766 | targetId, |
| 767 | attackerHandle, |
| 768 | target.handle, |
| 769 | profile, |
| 770 | now, |
| 771 | to |
| 772 | )); |
| 773 | } |
| 774 | |
| 775 | /** Authoritative ranged hit when the defender lives on this worker. */ |
| 776 | export function applyRemoteRangedShot (world, payload) |
| 777 | { |
| 778 | const targetHandle = world.ecs.handleOf(payload.targetId); |
| 779 | if (targetHandle == null) |
| 780 | { return { events: [], forwards: [] }; } |
| 781 | const snapshot = { |
| 782 | tileX: payload.tileX, |
| 783 | tileY: payload.tileY |
| 784 | }; |
| 785 | |
| 786 | const hitEvents = applyLocalMeleeHit( |
| 787 | world, |
| 788 | payload.attackerId, |
| 789 | payload.targetId, |
| 790 | null, |
| 791 | targetHandle, |
| 792 | payload.profile, |
| 793 | payload.now ?? Date.now(), |
| 794 | snapshot, |
| 795 | payload.attackerLevels, |
| 796 | payload.attackerIsPlayer |
| 797 | ); |
| 798 | |
| 799 | const outcome = hitEvents.find((e) => |
| 800 | e.type === "combat-hit" || e.type === "combat-miss" || e.type === "combat-block"); |
| 801 | let endX = snapshot.tileX; |
| 802 | let endY = snapshot.tileY; |
| 803 | if (outcome?.type === "combat-miss") |
| 804 | { |
| 805 | const miss = missProjectileTile(snapshot.tileX, snapshot.tileY); |
| 806 | endX = miss.tileX; |
| 807 | endY = miss.tileY; |
| 808 | } |
| 809 | else |
| 810 | { |
| 811 | const currentPos = world.ecs.get(targetHandle, Component.position); |
| 812 | if (currentPos) |
| 813 | { |
| 814 | endX = currentPos.tileX; |
| 815 | endY = currentPos.tileY; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | const events = [{ |
| 820 | type: "combat-projectile", |
| 821 | attackerId: payload.attackerId, |
| 822 | targetId: payload.targetId, |
| 823 | fromTileX: payload.fromTileX, |
| 824 | fromTileY: payload.fromTileY, |
| 825 | tileX: endX, |
| 826 | tileY: endY, |
| 827 | durationMs: 400 |
| 828 | }, ...hitEvents]; |
| 829 | |
| 830 | const forwards = []; |
| 831 | const hitEvent = hitEvents.find((event) => event.type === "combat-hit"); |
| 832 | if (hitEvent && payload.attackerIsPlayer) |
| 833 | { |
| 834 | forwards.push({ |
| 835 | type: "forward-combat-xp", |
| 836 | entityId: payload.attackerId, |
| 837 | profile: payload.profile |
| 838 | }); |
| 839 | } |
| 840 | return { events, forwards }; |
| 841 | } |
| 842 | |
| 843 | /** Authoritative hit resolution when the defender lives on this worker. */ |
| 844 | export function applyRemoteMeleeSwing (world, payload) |
| 845 | { |
| 846 | const targetHandle = world.ecs.handleOf(payload.targetId); |
| 847 | if (targetHandle == null) |
| 848 | { return { events: [], forwards: [] }; } |
| 849 | const to = world.ecs.get(targetHandle, Component.position); |
| 850 | if (!to) |
| 851 | { return { events: [], forwards: [] }; } |
| 852 | |
| 853 | const events = applyLocalMeleeHit( |
| 854 | world, |
| 855 | payload.attackerId, |
| 856 | payload.targetId, |
| 857 | null, |
| 858 | targetHandle, |
| 859 | payload.profile, |
| 860 | payload.now ?? Date.now(), |
| 861 | to, |
| 862 | payload.attackerLevels, |
| 863 | payload.attackerIsPlayer |
| 864 | ); |
| 865 | |
| 866 | const forwards = []; |
| 867 | const hitEvent = events.find((event) => event.type === "combat-hit"); |
| 868 | if (hitEvent && payload.attackerIsPlayer) |
| 869 | { |
| 870 | forwards.push({ |
| 871 | type: "forward-combat-xp", |
| 872 | entityId: payload.attackerId, |
| 873 | profile: payload.profile |
| 874 | }); |
| 875 | } |
| 876 | return { events, forwards }; |
| 877 | } |
| 878 | |
| 879 | function applyLocalMeleeHit ( |
| 880 | world, |
| 881 | attackerId, |
| 882 | targetId, |
| 883 | attackerHandle, |
| 884 | targetHandle, |
| 885 | profile, |
| 886 | now, |
| 887 | to, |
| 888 | attackerLevelsOverride = null, |
| 889 | attackerIsPlayerOverride = null |
| 890 | ) |
| 891 | { |
| 892 | const events = []; |
| 893 | if (targetHandle == null) |
| 894 | { return events; } |
| 895 | |
| 896 | const attackerIsPlayer = attackerIsPlayerOverride != null |
| 897 | ? !!attackerIsPlayerOverride |
| 898 | : (attackerHandle != null ? !!world.ecs.get(attackerHandle, Component.character) : null); |
| 899 | |
| 900 | // Retaliate on the first resolved swing (hit or miss), not on Attack-click engage. |
| 901 | if (attackerIsPlayer === true && ensureMobCombat(world.ecs, targetId)) |
| 902 | { engageTarget(world.ecs, targetId, attackerId, now); } |
| 903 | if (attackerIsPlayer === false) |
| 904 | { |
| 905 | const targetCharacter = world.ecs.get(targetHandle, Component.character); |
| 906 | if (targetCharacter) |
| 907 | { |
| 908 | engageTarget(world.ecs, targetId, attackerId, now); |
| 909 | const follow = world.ecs.get(targetHandle, Component.follow); |
| 910 | if (follow) |
| 911 | { |
| 912 | follow.targetEntityId = null; |
| 913 | follow.trail = []; |
| 914 | } |
| 915 | if (world.ecs.has(targetHandle, Component.gather)) |
| 916 | { world.ecs.remove(targetHandle, Component.gather); } |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | const defenderSkills = world.ecs.get(targetHandle, Component.skills); |
| 921 | const defenderEquipment = world.ecs.get(targetHandle, Component.equipment); |
| 922 | const attackerLevels = attackerLevelsOverride |
| 923 | ?? (attackerHandle != null ? attackerCombatLevels(world.ecs, attackerHandle, profile) : null); |
| 924 | if (!attackerLevels) |
| 925 | { return events; } |
| 926 | |
| 927 | const agilityLevel = defenderSkillLevel( |
| 928 | defenderSkills, |
| 929 | SkillId.dexterity, |
| 930 | defenderEquipment |
| 931 | ); |
| 932 | const defenceLevel = defenderSkillLevel( |
| 933 | defenderSkills, |
| 934 | SkillId.defence, |
| 935 | defenderEquipment |
| 936 | ); |
| 937 | |
| 938 | const resolved = resolvePhysicalAttack({ |
| 939 | hitSkillLevel: effectiveHitSkillInDarkness( |
| 940 | attackerLevels.hitLevel, |
| 941 | tileLightAt(world, to.tileX, to.tileY) |
| 942 | ), |
| 943 | agilitySkillLevel: agilityLevel, |
| 944 | damageSkillLevel: attackerLevels.damageLevel, |
| 945 | defenceSkillLevel: defenceLevel, |
| 946 | baseDamage: profile.baseDamage ?? 1 |
| 947 | }); |
| 948 | |
| 949 | if (resolved.outcome === "miss") |
| 950 | { |
| 951 | events.push({ |
| 952 | type: "combat-miss", |
| 953 | attackerId, |
| 954 | targetId, |
| 955 | tileX: to.tileX, |
| 956 | tileY: to.tileY |
| 957 | }); |
| 958 | return events; |
| 959 | } |
| 960 | |
| 961 | if (resolved.outcome === "block") |
| 962 | { |
| 963 | events.push({ |
| 964 | type: "combat-block", |
| 965 | attackerId, |
| 966 | targetId, |
| 967 | tileX: to.tileX, |
| 968 | tileY: to.tileY |
| 969 | }); |
| 970 | return events; |
| 971 | } |
| 972 | |
| 973 | const damage = resolved.damage; |
| 974 | const targetCharacter = world.ecs.get(targetHandle, Component.character); |
| 975 | if (targetCharacter) |
| 976 | { syncPlayerHealth(world.ecs, targetHandle); } |
| 977 | const health = world.ecs.get(targetHandle, Component.health); |
| 978 | if (health) |
| 979 | { |
| 980 | health.current = Math.max(0, (health.current | 0) - damage); |
| 981 | } |
| 982 | events.push({ |
| 983 | type: "combat-hit", |
| 984 | attackerId, |
| 985 | targetId, |
| 986 | damage, |
| 987 | tileX: to.tileX, |
| 988 | tileY: to.tileY, |
| 989 | health: health?.current ?? 0, |
| 990 | maxHealth: health?.max ?? 0 |
| 991 | }); |
| 992 | |
| 993 | const healthXp = awardDamageHealthXp(world.ecs, targetHandle, damage); |
| 994 | if (healthXp) |
| 995 | { |
| 996 | events.push({ |
| 997 | type: "skills-updated", |
| 998 | entityId: targetId, |
| 999 | characterId: healthXp.characterId, |
| 1000 | skills: healthXp.view, |
| 1001 | stored: healthXp.stored, |
| 1002 | levelUps: healthXp.levelUps ?? [] |
| 1003 | }); |
| 1004 | if (health) |
| 1005 | { |
| 1006 | const hitEvent = events[events.length - 2]; |
| 1007 | if (hitEvent?.type === "combat-hit") |
| 1008 | { |
| 1009 | hitEvent.health = health.current; |
| 1010 | hitEvent.maxHealth = health.max; |
| 1011 | } |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | if (attackerHandle != null) |
| 1016 | { |
| 1017 | const attackerCharacter = world.ecs.get(attackerHandle, Component.character); |
| 1018 | if (attackerCharacter) |
| 1019 | { |
| 1020 | const xpAward = awardCombatHitXp(world.ecs, attackerHandle, profile); |
| 1021 | if (xpAward) |
| 1022 | { |
| 1023 | events.push({ |
| 1024 | type: "skills-updated", |
| 1025 | entityId: attackerId, |
| 1026 | characterId: attackerCharacter.characterId, |
| 1027 | skills: xpAward.view, |
| 1028 | stored: xpAward.stored, |
| 1029 | levelUps: xpAward.levelUps ?? [] |
| 1030 | }); |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | if (health && health.current <= 0) |
| 1036 | { |
| 1037 | clearEngage(world.ecs, attackerId); |
| 1038 | clearEngage(world.ecs, targetId); |
| 1039 | for (const entity of world.ecs.query(Component.combat)) |
| 1040 | { |
| 1041 | if (entity.combat.targetEntityId === targetId) |
| 1042 | { entity.combat.targetEntityId = null; } |
| 1043 | } |
| 1044 | if (targetCharacter && !world.ecs.has(targetHandle, Component.dead)) |
| 1045 | { |
| 1046 | const path = world.ecs.get(targetHandle, Component.path); |
| 1047 | if (path) |
| 1048 | { path.tiles = []; } |
| 1049 | const follow = world.ecs.get(targetHandle, Component.follow); |
| 1050 | if (follow) |
| 1051 | { |
| 1052 | follow.targetEntityId = null; |
| 1053 | follow.trail = []; |
| 1054 | } |
| 1055 | world.ecs.add(targetHandle, Component.dead, {}); |
| 1056 | const identity = world.ecs.get(targetHandle, Component.networkIdentity); |
| 1057 | events.push({ |
| 1058 | type: "player-died", |
| 1059 | entityId: targetId, |
| 1060 | attackerId, |
| 1061 | characterId: targetCharacter.characterId, |
| 1062 | sessionId: identity?.sessionId ?? null, |
| 1063 | tileX: to.tileX, |
| 1064 | tileY: to.tileY, |
| 1065 | health: 0, |
| 1066 | maxHealth: health.max |
| 1067 | }); |
| 1068 | } |
| 1069 | const creature = world.ecs.get(targetHandle, Component.creature); |
| 1070 | if (creature && !world.ecs.get(targetHandle, Component.character)) |
| 1071 | { |
| 1072 | events.push(...spawnMobDrops(world, targetHandle, to.tileX, to.tileY, now)); |
| 1073 | const view = world.destroyEntity?.(targetId) ?? null; |
| 1074 | if (view) |
| 1075 | { |
| 1076 | events.push({ |
| 1077 | type: "entity-despawn", |
| 1078 | entityId: targetId, |
| 1079 | tileX: view.tileX, |
| 1080 | tileY: view.tileY |
| 1081 | }); |
| 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | return events; |
| 1086 | } |