import { Component } from "../ecs/components.js"; import { itemDefinition } from "../../../shared/definitions/items.js"; import { mobDefinition } from "../../../shared/definitions/mobs.js"; import { mobCanFightType, mobMeleeAttackProfile, mobSkillLevels } from "../../../shared/definitions/mobCombat.js"; import { resolvePhysicalAttack } from "../../../shared/combat/rolls.js"; import { skillLevel, SkillId, UNARMED_ATTACK } from "../../../shared/definitions/skills.js"; import { effectiveSkillLevel, equipmentBonusesFromSlots } from "../../../shared/equipment/stats.js"; import { awardCombatHitXp, awardDamageHealthXp, syncPlayerHealth } from "../skills/service.js"; import { chunkBoundsAround, findPath } from "../navigation/astar.js"; import { VISIBLE_CHUNK_RADIUS } from "../../../shared/protocol/constants.js"; export function chebyshev (ax, ay, bx, by) { return Math.max(Math.abs(ax - bx), Math.abs(ay - by)); } export function attackProfileOf (ecs, handle) { const equipment = ecs.get(handle, Component.equipment); const weapon = equipment?.slots?.weapon; if (weapon?.itemId != null) { const attack = itemDefinition(weapon.itemId)?.equipment?.attack; if (attack) { return { ...attack }; } } const creature = ecs.get(handle, Component.creature); if (creature) { const def = mobDefinition(creature.mobTypeId); if (mobCanFightType(creature.mobTypeId)) { return mobMeleeAttackProfile(def); } } return { ...UNARMED_ATTACK }; } export function ensureMobCombat (ecs, entityId) { const handle = ecs.handleOf(entityId); if (handle == null) { return false; } const creature = ecs.get(handle, Component.creature); if (!creature || !mobCanFightType(creature.mobTypeId)) { return false; } const def = mobDefinition(creature.mobTypeId); if (!ecs.get(handle, Component.combat)) { ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0 }); } if (!ecs.get(handle, Component.skills)) { ecs.add(handle, Component.skills, { levels: mobSkillLevels(def) }); } return true; } function attackerSkillLevel (skills, profile, skillId) { if (profile.mobAttackSkill != null) { return profile.mobAttackSkill; } return skillLevel(skills, skillId); } function defenderSkillLevel (skills, skillId, equipment = null) { const bonuses = equipment ? equipmentBonusesFromSlots(equipment.slots) : {}; return effectiveSkillLevel(skills, skillId, bonuses); } function playerCombatLevels (ecs, handle, profile) { const skills = ecs.get(handle, Component.skills); const equipment = ecs.get(handle, Component.equipment); const bonuses = equipmentBonusesFromSlots(equipment?.slots); return { hitLevel: skillLevel(skills, profile.hitSkillId), damageLevel: effectiveSkillLevel(skills, profile.damageSkillId, bonuses), agilityLevel: effectiveSkillLevel(skills, SkillId.dexterity, bonuses), defenceLevel: effectiveSkillLevel(skills, SkillId.defence, bonuses) }; } export function isUntargetable (ecs, handle) { const creature = ecs.get(handle, Component.creature); if (!creature) { return false; } return !!mobDefinition(creature.mobTypeId)?.untargetable; } export function engageTarget (ecs, attackerId, targetId, now = Date.now()) { const handle = ecs.handleOf(attackerId); if (handle == null) { return false; } let combat = ecs.get(handle, Component.combat); if (!combat) { combat = ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0 }); } const targetChanged = combat.targetEntityId !== targetId; combat.targetEntityId = targetId; combat.nextSwingAt = Math.min(combat.nextSwingAt || 0, now); if (targetChanged) { const path = ecs.get(handle, Component.path); if (path) { path.tiles = []; } } return true; } export function clearEngage (ecs, entityId) { const handle = ecs.handleOf(entityId); const combat = handle != null ? ecs.get(handle, Component.combat) : null; if (combat) { combat.targetEntityId = null; } } export function stepCombat (world, now = Date.now()) { const events = []; for (const entity of world.ecs.query(Component.combat, Component.position)) { if (!world.chunkIsSimulating?.(entity.position.tileX, entity.position.tileY)) { continue; } const combat = entity.combat; const targetId = combat.targetEntityId; if (targetId == null) { continue; } const targetHandle = world.ecs.handleOf(targetId); if (targetHandle == null) { combat.targetEntityId = null; continue; } if (isUntargetable(world.ecs, targetHandle)) { combat.targetEntityId = null; continue; } const targetPos = world.ecs.get(targetHandle, Component.position); const targetHealth = world.ecs.get(targetHandle, Component.health); if (!targetPos || !targetHealth || targetHealth.current <= 0) { combat.targetEntityId = null; continue; } const profile = attackProfileOf(world.ecs, entity.handle); if (profile.mode !== "melee") { continue; } const dist = chebyshev(entity.position.tileX, entity.position.tileY, targetPos.tileX, targetPos.tileY); if (dist > 1) { queueMeleeApproach(world, entity.entityId, entity.handle, entity.position, targetPos); continue; } if (dist !== 1) { continue; } if ((combat.nextSwingAt || 0) > now) { continue; } combat.nextSwingAt = now + (profile.cooldownMs || 800); events.push(...resolveMeleeSwing(world, entity.entityId, targetId, profile, now)); } return events; } function queueMeleeApproach (world, entityId, handle, position, targetPos) { if (world.frozen?.has?.(entityId)) { return; } let path = world.ecs.get(handle, Component.path); if (!path) { path = world.ecs.add(handle, Component.path, { tiles: [] }); } if (chebyshev(position.tileX, position.tileY, targetPos.tileX, targetPos.tileY) <= 1) { path.tiles = []; return; } const goal = bestAdjacentGoal(world, entityId, position, targetPos); if (!goal) { path.tiles = []; clearEngage(world.ecs, entityId); return; } if (path.tiles?.length && !meleePathStale(world, entityId, position, targetPos, path, goal)) { return; } const bounds = chunkBoundsAround(position.tileX, position.tileY, VISIBLE_CHUNK_RADIUS); const tiles = findPath( position.tileX, position.tileY, goal.x, goal.y, (x, y) => world.walkableFor(entityId, x, y), bounds.minX, bounds.minY, bounds.maxX, bounds.maxY ); if (!tiles.length) { path.tiles = []; clearEngage(world.ecs, entityId); return; } path.tiles = tiles; const movement = world.ecs.get(handle, Component.movement); if (movement) { movement.walkCarry = 0; } } function meleePathStale (world, entityId, position, targetPos, path, goal) { const dest = path.tiles[path.tiles.length - 1]; if (dest.x !== goal.x || dest.y !== goal.y) { return true; } if (chebyshev(dest.x, dest.y, targetPos.tileX, targetPos.tileY) !== 1) { return true; } const next = path.tiles[0]; if (!next) { return true; } const blockedByOther = !world.walkableFor(entityId, next.x, next.y) && !(next.x === targetPos.tileX && next.y === targetPos.tileY); return blockedByOther; } function bestAdjacentGoal (world, entityId, position, targetPos) { const dirs = [ { x: 1, y: 0 }, { x: -1, y: 0 }, { x: 0, y: 1 }, { x: 0, y: -1 }, { x: 1, y: 1 }, { x: 1, y: -1 }, { x: -1, y: 1 }, { x: -1, y: -1 } ]; let best = null; let bestDist = Infinity; for (const dir of dirs) { const x = targetPos.tileX + dir.x; const y = targetPos.tileY + dir.y; if (!world.walkableFor(entityId, x, y) && !(x === position.tileX && y === position.tileY)) { continue; } const dist = chebyshev(position.tileX, position.tileY, x, y); if (dist < bestDist) { bestDist = dist; best = { x, y }; } } return best; } export function resolveMeleeSwing (world, attackerId, targetId, profile, now = Date.now()) { const events = []; const attackerHandle = world.ecs.handleOf(attackerId); const targetHandle = world.ecs.handleOf(targetId); if (attackerHandle == null || targetHandle == null) { return events; } const from = world.ecs.get(attackerHandle, Component.position); const to = world.ecs.get(targetHandle, Component.position); if (!from || !to) { return events; } events.push({ type: "combat-swing", attackerId, targetId, tileX: to.tileX, tileY: to.tileY, fromTileX: from.tileX, fromTileY: from.tileY }); const attackerCharacter = world.ecs.get(attackerHandle, Component.character); if (attackerCharacter && ensureMobCombat(world.ecs, targetId)) { engageTarget(world.ecs, targetId, attackerId, now); } const skills = world.ecs.get(attackerHandle, Component.skills); const defenderSkills = world.ecs.get(targetHandle, Component.skills); const defenderEquipment = world.ecs.get(targetHandle, Component.equipment); const attackerLevels = attackerCharacter ? playerCombatLevels(world.ecs, attackerHandle, profile) : { hitLevel: attackerSkillLevel(skills, profile, profile.hitSkillId), damageLevel: attackerSkillLevel(skills, profile, profile.damageSkillId), agilityLevel: skillLevel(skills, SkillId.dexterity), defenceLevel: skillLevel(skills, SkillId.defence) }; const hitLevel = attackerLevels.hitLevel; const damageLevel = attackerLevels.damageLevel; const agilityLevel = defenderSkillLevel( defenderSkills, SkillId.dexterity, defenderEquipment ); const defenceLevel = defenderSkillLevel( defenderSkills, SkillId.defence, defenderEquipment ); const resolved = resolvePhysicalAttack({ hitSkillLevel: hitLevel, agilitySkillLevel: agilityLevel, damageSkillLevel: damageLevel, defenceSkillLevel: defenceLevel, baseDamage: profile.baseDamage ?? 1 }); if (resolved.outcome === "miss") { events.push({ type: "combat-miss", attackerId, targetId, tileX: to.tileX, tileY: to.tileY }); return events; } if (resolved.outcome === "block") { events.push({ type: "combat-block", attackerId, targetId, tileX: to.tileX, tileY: to.tileY }); return events; } const damage = resolved.damage; const targetCharacter = world.ecs.get(targetHandle, Component.character); if (targetCharacter) { syncPlayerHealth(world.ecs, targetHandle); } const health = world.ecs.get(targetHandle, Component.health); if (health) { health.current = Math.max(0, (health.current | 0) - damage); } events.push({ type: "combat-hit", attackerId, targetId, damage, tileX: to.tileX, tileY: to.tileY, health: health?.current ?? 0, maxHealth: health?.max ?? 0 }); const healthXp = awardDamageHealthXp(world.ecs, targetHandle, damage); if (healthXp) { events.push({ type: "skills-updated", entityId: targetId, characterId: healthXp.characterId, skills: healthXp.view, stored: healthXp.stored }); if (health) { const hitEvent = events[events.length - 2]; if (hitEvent?.type === "combat-hit") { hitEvent.health = health.current; hitEvent.maxHealth = health.max; } } } if (attackerCharacter) { const xpAward = awardCombatHitXp(world.ecs, attackerHandle, profile); if (xpAward) { events.push({ type: "skills-updated", entityId: attackerId, characterId: attackerCharacter.characterId, skills: xpAward.view, stored: xpAward.stored }); } } if (health && health.current <= 0) { clearEngage(world.ecs, attackerId); clearEngage(world.ecs, targetId); const creature = world.ecs.get(targetHandle, Component.creature); if (creature && !world.ecs.get(targetHandle, Component.character)) { const view = world.destroyEntity?.(targetId) ?? null; if (view) { events.push({ type: "entity-despawn", entityId: targetId, tileX: view.tileX, tileY: view.tileY }); } } } return events; }