import { itemDefinition } from "../definitions/items.js"; import { EquipmentKind } from "../definitions/equipmentKinds.js"; import { SkillId, UNARMED_ATTACK } from "../definitions/skills.js"; import { spellDefinition } from "../definitions/spells.js"; import { equipmentBonusesFromSlots, effectiveSkillLevel } from "../equipment/stats.js"; import { combatDamageRollLevel } from "./rolls.js"; /** Attack profile from the equipped weapon (players). */ export function attackProfileFromEquipment (equipmentSlots = {}) { const weapon = equipmentSlots?.weapon; if (weapon?.itemId != null) { const attack = itemDefinition(weapon.itemId)?.equipment?.attack; if (attack) { return { ...attack }; } } return { ...UNARMED_ATTACK }; } export function weaponEquipmentKindFromSlots (equipmentSlots = {}) { const weapon = equipmentSlots?.weapon; if (!weapon?.itemId) { return null; } return itemDefinition(weapon.itemId)?.equipment?.kind ?? null; } export function isMagicStaffEquipped (equipmentSlots = {}) { return weaponEquipmentKindFromSlots(equipmentSlots) === EquipmentKind.staff; } export function physicalDamageSkillId (profile = UNARMED_ATTACK) { return profile.damageSkillId ?? SkillId.strength; } export function physicalDamageSkillLabel (skillId) { switch (skillId) { case SkillId.dexterity: return "Dexterity"; case SkillId.strength: return "Strength"; case SkillId.mind: return "Mind"; default: return skillId; } } /** Build `{ skillId: level }` from client skill rows or a plain level map. */ export function skillLevelsMap (skills) { if (Array.isArray(skills)) { const out = {}; for (const row of skills) { out[row.skillId] = row.level | 0 || 1; } return out; } if (skills?.levels) { return { ...skills.levels }; } if (skills && typeof skills === "object") { return { ...skills }; } return {}; } function mergeEquipmentBonuses (equipmentSlots, settings, statusBonuses = null) { const bonuses = equipmentBonusesFromSlots(equipmentSlots ?? {}, settings); if (statusBonuses) { bonuses[SkillId.strength] = (bonuses[SkillId.strength] ?? 0) + (statusBonuses.strength ?? 0); bonuses[SkillId.mind] = (bonuses[SkillId.mind] ?? 0) + (statusBonuses.mind ?? 0); } return bonuses; } export function effectivePhysicalDamageLevel ({ skillLevels, equipmentSlots = {}, settings = null, profile = null, statusBonuses = null }) { const levels = skillLevelsMap(skillLevels); const bonuses = mergeEquipmentBonuses(equipmentSlots, settings, statusBonuses); const attack = profile ?? attackProfileFromEquipment(equipmentSlots); const damageSkillId = physicalDamageSkillId(attack); return effectiveSkillLevel({ levels }, damageSkillId, bonuses); } export function effectiveMindDamageLevel ({ skillLevels, equipmentSlots = {}, settings = null, statusBonuses = null }) { const levels = skillLevelsMap(skillLevels); const bonuses = mergeEquipmentBonuses(equipmentSlots, settings, statusBonuses); return effectiveSkillLevel({ levels }, SkillId.mind, bonuses); } /** Max melee/archery hit on a neutral target (can still roll 0). */ export function maxNeutralPhysicalHit (effectiveDamageLevel) { return combatDamageRollLevel(effectiveDamageLevel); } /** Max magic damage hit on a neutral target after a successful cast. */ export function maxNeutralMagicHit (effectiveMindLevel) { return Math.max(1, combatDamageRollLevel(effectiveMindLevel)); } /** Min magic damage on a successful neutral hit (mind roll can be 0). */ export function minNeutralMagicHit () { return 1; } export function isPlayerAttackDamageSpell (spellId) { const def = spellDefinition(spellId); return !!def && def.effect === "damage" && !def.mobOnly; } /** * UI label for bag panel max hit. * @returns {{ text: string, title: string, statLabel: string|null }} */ export function bagMaxHitDisplay ({ skillLevels, equipmentSlots = {}, settings = null, knownSpellIds = [], statusBonuses = null }) { if (isMagicStaffEquipped(equipmentSlots)) { const mindLevel = effectiveMindDamageLevel({ skillLevels, equipmentSlots, settings, statusBonuses }); const attackSpells = (knownSpellIds ?? []).filter(isPlayerAttackDamageSpell); if (!attackSpells.length) { return { text: "—", title: "No attack spells known", statLabel: "Mind" }; } const max = maxNeutralMagicHit(mindLevel); const min = minNeutralMagicHit(); return { text: min === max ? String(max) : `${min} – ${max}`, title: `Max spell hit on neutral targets (Mind ${mindLevel})`, statLabel: "Mind" }; } const profile = attackProfileFromEquipment(equipmentSlots); const damageSkillId = physicalDamageSkillId(profile); const statLabel = physicalDamageSkillLabel(damageSkillId); const damageLevel = effectivePhysicalDamageLevel({ skillLevels, equipmentSlots, settings, profile, statusBonuses }); const max = maxNeutralPhysicalHit(damageLevel); return { text: String(max), title: `Max hit on neutral targets (${statLabel} ${damageLevel})`, statLabel }; } /** Max neutral hit for one damage spell (same for all spells today). */ export function spellMaxHitDisplay ({ spellId, skillLevels, equipmentSlots = {}, settings = null, statusBonuses = null }) { if (!isPlayerAttackDamageSpell(spellId)) { return null; } const mindLevel = effectiveMindDamageLevel({ skillLevels, equipmentSlots, settings, statusBonuses }); return maxNeutralMagicHit(mindLevel); }