BlueGit

timfa questward

Files

questward / shared / combat / maxHit.js

maxHit.js

41289f67 History
maxHit.js 5.7 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, spellDamageRollLevel } 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 on a neutral target after a successful cast. */
116export function maxNeutralMagicHit (effectiveMindLevel, spellTier = 1)
117{
118 return Math.max(1, spellDamageRollLevel(effectiveMindLevel, spellTier));
119}
120
121export function isPlayerAttackDamageSpell (spellId)
122{
123 const def = spellDefinition(spellId);
124 return !!def && def.effect === "damage" && !def.mobOnly;
125}
126
127/** First favorited player attack spell, or null. */
128export function firstFavoriteAttackSpellId (spells)
129{
130 const favorites = Array.isArray(spells?.favorites) ? spells.favorites : [];
131 for (const spellId of favorites)
132 {
133 if (isPlayerAttackDamageSpell(spellId))
134 { return spellId; }
135 }
136 return null;
137}
138
139/**
140 * UI label for bag panel max hit.
141 * @returns {{ text: string, title: string, statLabel: string|null }}
142 */
143export function bagMaxHitDisplay ({
144 skillLevels,
145 equipmentSlots = {},
146 settings = null,
147 knownSpellIds = [],
148 statusBonuses = null
149})
150{
151 if (isMagicStaffEquipped(equipmentSlots))
152 {
153 const mindLevel = effectiveMindDamageLevel({
154 skillLevels,
155 equipmentSlots,
156 settings,
157 statusBonuses
158 });
159 const attackSpells = (knownSpellIds ?? []).filter(isPlayerAttackDamageSpell);
160 if (!attackSpells.length)
161 {
162 return {
163 text: "—",
164 title: "No attack spells known",
165 statLabel: "Mind"
166 };
167 }
168 const caps = attackSpells.map((spellId) =>
169 {
170 const tier = spellDefinition(spellId)?.tier ?? 1;
171 return maxNeutralMagicHit(mindLevel, tier);
172 });
173 const min = Math.min(...caps);
174 const max = Math.max(...caps);
175 return {
176 text: min === max ? String(max) : `${min} – ${max}`,
177 title: `Max spell hit on neutral targets (Mind ${mindLevel})`,
178 statLabel: "Mind"
179 };
180 }
181
182 const profile = attackProfileFromEquipment(equipmentSlots);
183 const damageSkillId = physicalDamageSkillId(profile);
184 const statLabel = physicalDamageSkillLabel(damageSkillId);
185 const damageLevel = effectivePhysicalDamageLevel({
186 skillLevels,
187 equipmentSlots,
188 settings,
189 profile,
190 statusBonuses
191 });
192 const max = maxNeutralPhysicalHit(damageLevel);
193 return {
194 text: String(max),
195 title: `Max hit on neutral targets (${statLabel} ${damageLevel})`,
196 statLabel
197 };
198}
199
200/** Max neutral hit for one damage spell. */
201export function spellMaxHitDisplay ({
202 spellId,
203 skillLevels,
204 equipmentSlots = {},
205 settings = null,
206 statusBonuses = null
207})
208{
209 if (!isPlayerAttackDamageSpell(spellId))
210 { return null; }
211 const def = spellDefinition(spellId);
212 const mindLevel = effectiveMindDamageLevel({
213 skillLevels,
214 equipmentSlots,
215 settings,
216 statusBonuses
217 });
218 return maxNeutralMagicHit(mindLevel, def?.tier ?? 1);
219}