BlueGit

timfa questward

Files

questward / shared / combat / maxHit.js

maxHit.js

maxHit.js 5.3 KiB Download Raw
1import { itemDefinition } from "../definitions/items.js";
2import { EquipmentKind } from "../definitions/equipmentKinds.js";
3import { SkillId, UNARMED_ATTACK } from "../definitions/skills.js";
4import { spellDefinition } from "../definitions/spells.js";
5import { equipmentBonusesFromSlots, effectiveSkillLevel } from "../equipment/stats.js";
6import { combatDamageRollLevel } from "./rolls.js";
7
8/** Attack profile from the equipped weapon (players). */
9export 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
21export 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
29export function isMagicStaffEquipped (equipmentSlots = {})
30{
31 return weaponEquipmentKindFromSlots(equipmentSlots) === EquipmentKind.staff;
32}
33
34export function physicalDamageSkillId (profile = UNARMED_ATTACK)
35{
36 return profile.damageSkillId ?? SkillId.strength;
37}
38
39export 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. */
55export 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
71function 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
82export 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
97export 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). */
110export function maxNeutralPhysicalHit (effectiveDamageLevel)
111{
112 return combatDamageRollLevel(effectiveDamageLevel);
113}
114
115/** Max magic damage hit on a neutral target after a successful cast. */
116export function maxNeutralMagicHit (effectiveMindLevel)
117{
118 return Math.max(1, combatDamageRollLevel(effectiveMindLevel));
119}
120
121/** Min magic damage on a successful neutral hit (mind roll can be 0). */
122export function minNeutralMagicHit ()
123{
124 return 1;
125}
126
127export function isPlayerAttackDamageSpell (spellId)
128{
129 const def = spellDefinition(spellId);
130 return !!def && def.effect === "damage" && !def.mobOnly;
131}
132
133/**
134 * UI label for bag panel max hit.
135 * @returns {{ text: string, title: string, statLabel: string|null }}
136 */
137export function bagMaxHitDisplay ({
138 skillLevels,
139 equipmentSlots = {},
140 settings = null,
141 knownSpellIds = [],
142 statusBonuses = null
143})
144{
145 if (isMagicStaffEquipped(equipmentSlots))
146 {
147 const mindLevel = effectiveMindDamageLevel({
148 skillLevels,
149 equipmentSlots,
150 settings,
151 statusBonuses
152 });
153 const attackSpells = (knownSpellIds ?? []).filter(isPlayerAttackDamageSpell);
154 if (!attackSpells.length)
155 {
156 return {
157 text: "—",
158 title: "No attack spells known",
159 statLabel: "Mind"
160 };
161 }
162 const max = maxNeutralMagicHit(mindLevel);
163 const min = minNeutralMagicHit();
164 return {
165 text: min === max ? String(max) : `${min} – ${max}`,
166 title: `Max spell hit on neutral targets (Mind ${mindLevel})`,
167 statLabel: "Mind"
168 };
169 }
170
171 const profile = attackProfileFromEquipment(equipmentSlots);
172 const damageSkillId = physicalDamageSkillId(profile);
173 const statLabel = physicalDamageSkillLabel(damageSkillId);
174 const damageLevel = effectivePhysicalDamageLevel({
175 skillLevels,
176 equipmentSlots,
177 settings,
178 profile,
179 statusBonuses
180 });
181 const max = maxNeutralPhysicalHit(damageLevel);
182 return {
183 text: String(max),
184 title: `Max hit on neutral targets (${statLabel} ${damageLevel})`,
185 statLabel
186 };
187}
188
189/** Max neutral hit for one damage spell (same for all spells today). */
190export function spellMaxHitDisplay ({
191 spellId,
192 skillLevels,
193 equipmentSlots = {},
194 settings = null,
195 statusBonuses = null
196})
197{
198 if (!isPlayerAttackDamageSpell(spellId))
199 { return null; }
200 const mindLevel = effectiveMindDamageLevel({
201 skillLevels,
202 equipmentSlots,
203 settings,
204 statusBonuses
205 });
206 return maxNeutralMagicHit(mindLevel);
207}