import { Component } from "../ecs/components.js"; import { itemDefinition } from "../../../shared/definitions/items.js"; import { mobDefinition } from "../../../shared/definitions/mobs.js"; import { skillLevel, UNARMED_ATTACK } from "../../../shared/definitions/skills.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); const mobAttack = creature ? mobDefinition(creature.mobTypeId)?.defaultAttack : null; if (mobAttack) { return { ...mobAttack }; } return { ...UNARMED_ATTACK }; } 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 }); } combat.targetEntityId = targetId; combat.nextSwingAt = Math.min(combat.nextSwingAt || 0, now); 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)) { 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 (path.tiles?.length) { return; } const goal = bestAdjacentGoal(world, entityId, position, targetPos); if (!goal) { return; } const bounds = chunkBoundsAround(position.tileX, position.tileY, VISIBLE_CHUNK_RADIUS); path.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 ); const movement = world.ecs.get(handle, Component.movement); if (movement) { movement.walkCarry = 0; } } 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 skills = world.ecs.get(attackerHandle, Component.skills); const hitChance = Math.min(0.95, 0.45 + skillLevel(skills, profile.hitSkillId) * 0.03); const hit = Math.random() < hitChance; if (!hit) { events.push({ type: "combat-miss", attackerId, targetId, tileX: to.tileX, tileY: to.tileY }); return events; } const damage = Math.max(1, (profile.baseDamage | 0) + Math.floor(skillLevel(skills, profile.damageSkillId) / 5)); 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 }); if (world.ecs.get(targetHandle, Component.combat)) { engageTarget(world.ecs, targetId, attackerId, now); } 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; }