Improve handling of minions and hordes

This commit is contained in:
Carlos Fernandez 2026-02-10 00:18:16 -05:00
parent c61c90b372
commit 9220a4ad1e
2 changed files with 164 additions and 78 deletions

View file

@ -485,34 +485,45 @@ export function htmlToText(html) {
/**
* Given a simple flavor-less formula with only +/- operators, returns a list of damage partial terms.
* All subtracted terms become negative terms.
* If there are no dice, it returns 0d1 for that term.
*/
export function parseTermsFromSimpleFormula(formula) {
const roll = formula instanceof Roll ? formula : new Roll(formula);
// Parse from right to left so that when we hit an operator, we already have the term.
return roll.terms.reduceRight(
(result, term) => {
// Ignore + terms, we assume + by default
if (term.expression === " + ") return result;
return roll.terms.reduceRight((result, term) => {
// Ignore + terms, we assume + by default
if (term.expression === ' + ') return result;
// - terms modify the last term we parsed
if (term.expression === " - ") {
const termToModify = result[0];
if (termToModify) {
if (termToModify.modifier) termToModify.modifier *= -1;
if (termToModify.dice) termToModify.dice *= -1;
}
return result;
// - terms modify the last term we parsed
if (term.expression === ' - ') {
const termToModify = result[0];
if (termToModify) {
if (termToModify.bonus) termToModify.bonus *= -1;
if (termToModify.dice) termToModify.dice *= -1;
}
result.unshift({
modifier: term instanceof foundry.dice.terms.NumericTerm ? term.number : 0,
dice: term instanceof foundry.dice.terms.Die ? term.number : 0,
faces: term.faces ?? null,
});
return result;
},
[],
);
}
}
result.unshift({
bonus: term instanceof foundry.dice.terms.NumericTerm ? term.number : 0,
diceQuantity: term instanceof foundry.dice.terms.Die ? term.number : 0,
faces: term.faces ?? 1
});
return result;
}, []);
}
/**
* Calculates the expectede value from a formula or the results of parseTermsFromSimpleFormula.
* @returns {number} the average result of rolling the given dice
*/
export function calculateExpectedValue(formulaOrTerms) {
const terms = Array.isArray(formulaOrTerms)
? formulaOrTerms
: typeof formulaOrTerms === 'string'
? parseTermsFromSimpleFormula(formulaOrTerms)
: [formulaOrTerms];
return terms.reduce((r, t) => r + (t.bonus ?? 0) + (t.diceQuantity ? (t.diceQuantity * (t.faces + 1)) / 2 : 0), 0);
}