| 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 { skillLevel, UNARMED_ATTACK } from "../../../shared/definitions/skills.js"; |
| 5 | import { chunkBoundsAround, findPath } from "../navigation/astar.js"; |
| 6 | import { VISIBLE_CHUNK_RADIUS } from "../../../shared/protocol/constants.js"; |
| 7 | |
| 8 | export function chebyshev (ax, ay, bx, by) |
| 9 | { |
| 10 | return Math.max(Math.abs(ax - bx), Math.abs(ay - by)); |
| 11 | } |
| 12 | |
| 13 | export function attackProfileOf (ecs, handle) |
| 14 | { |
| 15 | const equipment = ecs.get(handle, Component.equipment); |
| 16 | const weapon = equipment?.slots?.weapon; |
| 17 | if (weapon?.itemId != null) |
| 18 | { |
| 19 | const attack = itemDefinition(weapon.itemId)?.equipment?.attack; |
| 20 | if (attack) |
| 21 | { return { ...attack }; } |
| 22 | } |
| 23 | const creature = ecs.get(handle, Component.creature); |
| 24 | const mobAttack = creature ? mobDefinition(creature.mobTypeId)?.defaultAttack : null; |
| 25 | if (mobAttack) |
| 26 | { return { ...mobAttack }; } |
| 27 | return { ...UNARMED_ATTACK }; |
| 28 | } |
| 29 | |
| 30 | export function isUntargetable (ecs, handle) |
| 31 | { |
| 32 | const creature = ecs.get(handle, Component.creature); |
| 33 | if (!creature) |
| 34 | { return false; } |
| 35 | return !!mobDefinition(creature.mobTypeId)?.untargetable; |
| 36 | } |
| 37 | |
| 38 | export function engageTarget (ecs, attackerId, targetId, now = Date.now()) |
| 39 | { |
| 40 | const handle = ecs.handleOf(attackerId); |
| 41 | if (handle == null) |
| 42 | { return false; } |
| 43 | let combat = ecs.get(handle, Component.combat); |
| 44 | if (!combat) |
| 45 | { combat = ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0 }); } |
| 46 | combat.targetEntityId = targetId; |
| 47 | combat.nextSwingAt = Math.min(combat.nextSwingAt || 0, now); |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | export function clearEngage (ecs, entityId) |
| 52 | { |
| 53 | const handle = ecs.handleOf(entityId); |
| 54 | const combat = handle != null ? ecs.get(handle, Component.combat) : null; |
| 55 | if (combat) |
| 56 | { combat.targetEntityId = null; } |
| 57 | } |
| 58 | |
| 59 | export function stepCombat (world, now = Date.now()) |
| 60 | { |
| 61 | const events = []; |
| 62 | for (const entity of world.ecs.query(Component.combat, Component.position)) |
| 63 | { |
| 64 | const combat = entity.combat; |
| 65 | const targetId = combat.targetEntityId; |
| 66 | if (targetId == null) |
| 67 | { continue; } |
| 68 | const targetHandle = world.ecs.handleOf(targetId); |
| 69 | if (targetHandle == null) |
| 70 | { |
| 71 | combat.targetEntityId = null; |
| 72 | continue; |
| 73 | } |
| 74 | if (isUntargetable(world.ecs, targetHandle)) |
| 75 | { |
| 76 | combat.targetEntityId = null; |
| 77 | continue; |
| 78 | } |
| 79 | const targetPos = world.ecs.get(targetHandle, Component.position); |
| 80 | const targetHealth = world.ecs.get(targetHandle, Component.health); |
| 81 | if (!targetPos || !targetHealth || targetHealth.current <= 0) |
| 82 | { |
| 83 | combat.targetEntityId = null; |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | const profile = attackProfileOf(world.ecs, entity.handle); |
| 88 | if (profile.mode !== "melee") |
| 89 | { continue; } |
| 90 | |
| 91 | const dist = chebyshev(entity.position.tileX, entity.position.tileY, targetPos.tileX, targetPos.tileY); |
| 92 | if (dist > 1) |
| 93 | { |
| 94 | queueMeleeApproach(world, entity.entityId, entity.handle, entity.position, targetPos); |
| 95 | continue; |
| 96 | } |
| 97 | if (dist !== 1) |
| 98 | { continue; } |
| 99 | if ((combat.nextSwingAt || 0) > now) |
| 100 | { continue; } |
| 101 | |
| 102 | combat.nextSwingAt = now + (profile.cooldownMs || 800); |
| 103 | events.push(...resolveMeleeSwing(world, entity.entityId, targetId, profile, now)); |
| 104 | } |
| 105 | return events; |
| 106 | } |
| 107 | |
| 108 | function queueMeleeApproach (world, entityId, handle, position, targetPos) |
| 109 | { |
| 110 | if (world.frozen?.has?.(entityId)) |
| 111 | { return; } |
| 112 | let path = world.ecs.get(handle, Component.path); |
| 113 | if (!path) |
| 114 | { path = world.ecs.add(handle, Component.path, { tiles: [] }); } |
| 115 | if (path.tiles?.length) |
| 116 | { return; } |
| 117 | const goal = bestAdjacentGoal(world, entityId, position, targetPos); |
| 118 | if (!goal) |
| 119 | { return; } |
| 120 | const bounds = chunkBoundsAround(position.tileX, position.tileY, VISIBLE_CHUNK_RADIUS); |
| 121 | path.tiles = findPath( |
| 122 | position.tileX, |
| 123 | position.tileY, |
| 124 | goal.x, |
| 125 | goal.y, |
| 126 | (x, y) => world.walkableFor(entityId, x, y), |
| 127 | bounds.minX, |
| 128 | bounds.minY, |
| 129 | bounds.maxX, |
| 130 | bounds.maxY |
| 131 | ); |
| 132 | const movement = world.ecs.get(handle, Component.movement); |
| 133 | if (movement) |
| 134 | { movement.walkCarry = 0; } |
| 135 | } |
| 136 | |
| 137 | function bestAdjacentGoal (world, entityId, position, targetPos) |
| 138 | { |
| 139 | const dirs = [ |
| 140 | { x: 1, y: 0 }, { x: -1, y: 0 }, { x: 0, y: 1 }, { x: 0, y: -1 }, |
| 141 | { x: 1, y: 1 }, { x: 1, y: -1 }, { x: -1, y: 1 }, { x: -1, y: -1 } |
| 142 | ]; |
| 143 | let best = null; |
| 144 | let bestDist = Infinity; |
| 145 | for (const dir of dirs) |
| 146 | { |
| 147 | const x = targetPos.tileX + dir.x; |
| 148 | const y = targetPos.tileY + dir.y; |
| 149 | if (!world.walkableFor(entityId, x, y) && !(x === position.tileX && y === position.tileY)) |
| 150 | { continue; } |
| 151 | const dist = chebyshev(position.tileX, position.tileY, x, y); |
| 152 | if (dist < bestDist) |
| 153 | { |
| 154 | bestDist = dist; |
| 155 | best = { x, y }; |
| 156 | } |
| 157 | } |
| 158 | return best; |
| 159 | } |
| 160 | |
| 161 | export function resolveMeleeSwing (world, attackerId, targetId, profile, now = Date.now()) |
| 162 | { |
| 163 | const events = []; |
| 164 | const attackerHandle = world.ecs.handleOf(attackerId); |
| 165 | const targetHandle = world.ecs.handleOf(targetId); |
| 166 | if (attackerHandle == null || targetHandle == null) |
| 167 | { return events; } |
| 168 | const from = world.ecs.get(attackerHandle, Component.position); |
| 169 | const to = world.ecs.get(targetHandle, Component.position); |
| 170 | if (!from || !to) |
| 171 | { return events; } |
| 172 | |
| 173 | events.push({ |
| 174 | type: "combat-swing", |
| 175 | attackerId, |
| 176 | targetId, |
| 177 | tileX: to.tileX, |
| 178 | tileY: to.tileY, |
| 179 | fromTileX: from.tileX, |
| 180 | fromTileY: from.tileY |
| 181 | }); |
| 182 | |
| 183 | const skills = world.ecs.get(attackerHandle, Component.skills); |
| 184 | const hitChance = Math.min(0.95, 0.45 + skillLevel(skills, profile.hitSkillId) * 0.03); |
| 185 | const hit = Math.random() < hitChance; |
| 186 | if (!hit) |
| 187 | { |
| 188 | events.push({ |
| 189 | type: "combat-miss", |
| 190 | attackerId, |
| 191 | targetId, |
| 192 | tileX: to.tileX, |
| 193 | tileY: to.tileY |
| 194 | }); |
| 195 | return events; |
| 196 | } |
| 197 | |
| 198 | const damage = Math.max(1, (profile.baseDamage | 0) + Math.floor(skillLevel(skills, profile.damageSkillId) / 5)); |
| 199 | const health = world.ecs.get(targetHandle, Component.health); |
| 200 | if (health) |
| 201 | { |
| 202 | health.current = Math.max(0, (health.current | 0) - damage); |
| 203 | } |
| 204 | events.push({ |
| 205 | type: "combat-hit", |
| 206 | attackerId, |
| 207 | targetId, |
| 208 | damage, |
| 209 | tileX: to.tileX, |
| 210 | tileY: to.tileY, |
| 211 | health: health?.current ?? 0 |
| 212 | }); |
| 213 | |
| 214 | if (world.ecs.get(targetHandle, Component.combat)) |
| 215 | { engageTarget(world.ecs, targetId, attackerId, now); } |
| 216 | |
| 217 | if (health && health.current <= 0) |
| 218 | { |
| 219 | clearEngage(world.ecs, attackerId); |
| 220 | clearEngage(world.ecs, targetId); |
| 221 | const creature = world.ecs.get(targetHandle, Component.creature); |
| 222 | if (creature && !world.ecs.get(targetHandle, Component.character)) |
| 223 | { |
| 224 | const view = world.destroyEntity?.(targetId) ?? null; |
| 225 | if (view) |
| 226 | { |
| 227 | events.push({ |
| 228 | type: "entity-despawn", |
| 229 | entityId: targetId, |
| 230 | tileX: view.tileX, |
| 231 | tileY: view.tileY |
| 232 | }); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | return events; |
| 237 | } |