| 1 | import { itemDefinition } from "../definitions/items.js"; |
| 2 | import { EquipmentKind } from "../definitions/equipmentKinds.js"; |
| 3 | import { SkillId, UNARMED_ATTACK } from "../definitions/skills.js"; |
| 4 | import { spellDefinition } from "../definitions/spells.js"; |
| 5 | import { equipmentBonusesFromSlots, effectiveSkillLevel } from "../equipment/stats.js"; |
| 6 | import { combatDamageRollLevel, spellDamageRollLevel } from "./rolls.js"; |
| 7 | |
| 8 | /** Attack profile from the equipped weapon (players). */ |
| 9 | export function attackProfileFromEquipment (equipmentSlots = {}) |
| 10 | { |
| 11 | const weapon = equipmentSlots?.weapon; |
| 12 | if (weapon?.itemId != null) |
| 13 | { |
| 14 | const attack = itemDefinition(weapon.itemId)?.equipment?.attack; |
| 15 | if (attack) |
| 16 | { return { ...attack }; } |
| 17 | } |
| 18 | return { ...UNARMED_ATTACK }; |
| 19 | } |
| 20 | |
| 21 | export function weaponEquipmentKindFromSlots (equipmentSlots = {}) |
| 22 | { |
| 23 | const weapon = equipmentSlots?.weapon; |
| 24 | if (!weapon?.itemId) |
| 25 | { return null; } |
| 26 | return itemDefinition(weapon.itemId)?.equipment?.kind ?? null; |
| 27 | } |
| 28 | |
| 29 | export function isMagicStaffEquipped (equipmentSlots = {}) |
| 30 | { |
| 31 | return weaponEquipmentKindFromSlots(equipmentSlots) === EquipmentKind.staff; |
| 32 | } |
| 33 | |
| 34 | export function physicalDamageSkillId (profile = UNARMED_ATTACK) |
| 35 | { |
| 36 | return profile.damageSkillId ?? SkillId.strength; |
| 37 | } |
| 38 | |
| 39 | export function physicalDamageSkillLabel (skillId) |
| 40 | { |
| 41 | switch (skillId) |
| 42 | { |
| 43 | case SkillId.dexterity: |
| 44 | return "Dexterity"; |
| 45 | case SkillId.strength: |
| 46 | return "Strength"; |
| 47 | case SkillId.mind: |
| 48 | return "Mind"; |
| 49 | default: |
| 50 | return skillId; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** Build `{ skillId: level }` from client skill rows or a plain level map. */ |
| 55 | export function skillLevelsMap (skills) |
| 56 | { |
| 57 | if (Array.isArray(skills)) |
| 58 | { |
| 59 | const out = {}; |
| 60 | for (const row of skills) |
| 61 | { out[row.skillId] = row.level | 0 || 1; } |
| 62 | return out; |
| 63 | } |
| 64 | if (skills?.levels) |
| 65 | { return { ...skills.levels }; } |
| 66 | if (skills && typeof skills === "object") |
| 67 | { return { ...skills }; } |
| 68 | return {}; |
| 69 | } |
| 70 | |
| 71 | function mergeEquipmentBonuses (equipmentSlots, settings, statusBonuses = null) |
| 72 | { |
| 73 | const bonuses = equipmentBonusesFromSlots(equipmentSlots ?? {}, settings); |
| 74 | if (statusBonuses) |
| 75 | { |
| 76 | bonuses[SkillId.strength] = (bonuses[SkillId.strength] ?? 0) + (statusBonuses.strength ?? 0); |
| 77 | bonuses[SkillId.mind] = (bonuses[SkillId.mind] ?? 0) + (statusBonuses.mind ?? 0); |
| 78 | } |
| 79 | return bonuses; |
| 80 | } |
| 81 | |
| 82 | export function effectivePhysicalDamageLevel ({ |
| 83 | skillLevels, |
| 84 | equipmentSlots = {}, |
| 85 | settings = null, |
| 86 | profile = null, |
| 87 | statusBonuses = null |
| 88 | }) |
| 89 | { |
| 90 | const levels = skillLevelsMap(skillLevels); |
| 91 | const bonuses = mergeEquipmentBonuses(equipmentSlots, settings, statusBonuses); |
| 92 | const attack = profile ?? attackProfileFromEquipment(equipmentSlots); |
| 93 | const damageSkillId = physicalDamageSkillId(attack); |
| 94 | return effectiveSkillLevel({ levels }, damageSkillId, bonuses); |
| 95 | } |
| 96 | |
| 97 | export function effectiveMindDamageLevel ({ |
| 98 | skillLevels, |
| 99 | equipmentSlots = {}, |
| 100 | settings = null, |
| 101 | statusBonuses = null |
| 102 | }) |
| 103 | { |
| 104 | const levels = skillLevelsMap(skillLevels); |
| 105 | const bonuses = mergeEquipmentBonuses(equipmentSlots, settings, statusBonuses); |
| 106 | return effectiveSkillLevel({ levels }, SkillId.mind, bonuses); |
| 107 | } |
| 108 | |
| 109 | /** Max melee/archery hit on a neutral target (can still roll 0). */ |
| 110 | export function maxNeutralPhysicalHit (effectiveDamageLevel) |
| 111 | { |
| 112 | return combatDamageRollLevel(effectiveDamageLevel); |
| 113 | } |
| 114 | |
| 115 | /** Max magic damage on a neutral target after a successful cast. */ |
| 116 | export function maxNeutralMagicHit (effectiveMindLevel, spellTier = 1) |
| 117 | { |
| 118 | return Math.max(1, spellDamageRollLevel(effectiveMindLevel, spellTier)); |
| 119 | } |
| 120 | |
| 121 | export function isPlayerAttackDamageSpell (spellId) |
| 122 | { |
| 123 | const def = spellDefinition(spellId); |
| 124 | return !!def && def.effect === "damage" && !def.mobOnly; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * UI label for bag panel max hit. |
| 129 | * @returns {{ text: string, title: string, statLabel: string|null }} |
| 130 | */ |
| 131 | export function bagMaxHitDisplay ({ |
| 132 | skillLevels, |
| 133 | equipmentSlots = {}, |
| 134 | settings = null, |
| 135 | knownSpellIds = [], |
| 136 | statusBonuses = null |
| 137 | }) |
| 138 | { |
| 139 | if (isMagicStaffEquipped(equipmentSlots)) |
| 140 | { |
| 141 | const mindLevel = effectiveMindDamageLevel({ |
| 142 | skillLevels, |
| 143 | equipmentSlots, |
| 144 | settings, |
| 145 | statusBonuses |
| 146 | }); |
| 147 | const attackSpells = (knownSpellIds ?? []).filter(isPlayerAttackDamageSpell); |
| 148 | if (!attackSpells.length) |
| 149 | { |
| 150 | return { |
| 151 | text: "—", |
| 152 | title: "No attack spells known", |
| 153 | statLabel: "Mind" |
| 154 | }; |
| 155 | } |
| 156 | const caps = attackSpells.map((spellId) => |
| 157 | { |
| 158 | const tier = spellDefinition(spellId)?.tier ?? 1; |
| 159 | return maxNeutralMagicHit(mindLevel, tier); |
| 160 | }); |
| 161 | const min = Math.min(...caps); |
| 162 | const max = Math.max(...caps); |
| 163 | return { |
| 164 | text: min === max ? String(max) : `${min} – ${max}`, |
| 165 | title: `Max spell hit on neutral targets (Mind ${mindLevel})`, |
| 166 | statLabel: "Mind" |
| 167 | }; |
| 168 | } |
| 169 | |
| 170 | const profile = attackProfileFromEquipment(equipmentSlots); |
| 171 | const damageSkillId = physicalDamageSkillId(profile); |
| 172 | const statLabel = physicalDamageSkillLabel(damageSkillId); |
| 173 | const damageLevel = effectivePhysicalDamageLevel({ |
| 174 | skillLevels, |
| 175 | equipmentSlots, |
| 176 | settings, |
| 177 | profile, |
| 178 | statusBonuses |
| 179 | }); |
| 180 | const max = maxNeutralPhysicalHit(damageLevel); |
| 181 | return { |
| 182 | text: String(max), |
| 183 | title: `Max hit on neutral targets (${statLabel} ${damageLevel})`, |
| 184 | statLabel |
| 185 | }; |
| 186 | } |
| 187 | |
| 188 | /** Max neutral hit for one damage spell. */ |
| 189 | export function spellMaxHitDisplay ({ |
| 190 | spellId, |
| 191 | skillLevels, |
| 192 | equipmentSlots = {}, |
| 193 | settings = null, |
| 194 | statusBonuses = null |
| 195 | }) |
| 196 | { |
| 197 | if (!isPlayerAttackDamageSpell(spellId)) |
| 198 | { return null; } |
| 199 | const def = spellDefinition(spellId); |
| 200 | const mindLevel = effectiveMindDamageLevel({ |
| 201 | skillLevels, |
| 202 | equipmentSlots, |
| 203 | settings, |
| 204 | statusBonuses |
| 205 | }); |
| 206 | return maxNeutralMagicHit(mindLevel, def?.tier ?? 1); |
| 207 | } |