Skip to content

Commit

Permalink
Adjust dice to avoid deprecated global terms
Browse files Browse the repository at this point in the history
  • Loading branch information
arbron committed Aug 6, 2024
1 parent ecc053a commit b06d693
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 18 deletions.
5 changes: 0 additions & 5 deletions dnd5e.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ Hooks.once("init", function() {

// TODO: Remove when v11 support is dropped.
CONFIG.compatibility.excludePatterns.push(/filePicker|select/);
CONFIG.compatibility.excludePatterns.push(/foundry\.dice\.terms/);
CONFIG.compatibility.excludePatterns.push(
/aggregateDamageRoll|configureDamage|preprocessFormula|simplifyRollFormula/
);
CONFIG.compatibility.excludePatterns.push(/core\.sourceId/);

// Record Configuration Values
CONFIG.DND5E = DND5E;
Expand Down
1 change: 0 additions & 1 deletion module/data/item/fields/summons-field.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ export class SummonsData extends foundry.abstract.DataModel {
// Template actor (linked) found in world, create a copy for this user's item.
return actor.clone({
"flags.dnd5e.summonedCopy": true,
"flags.core.sourceId": actor.uuid,
"_stats.compendiumSource": actor.uuid
}, {save: true});
}
Expand Down
4 changes: 2 additions & 2 deletions module/dice/aggregate-damage-rolls.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ function chunkTerms(terms, type) {

for ( let term of terms ) {
// Plus or minus operators split chunks
if ( (term instanceof OperatorTerm) && ["+", "-"].includes(term.operator) ) {
if ( (term instanceof foundry.dice.terms.OperatorTerm) && ["+", "-"].includes(term.operator) ) {
if ( currentChunk ) pushChunk();
if ( term.operator === "-" ) negative = !negative;
continue;
}

// All other terms get added to the current chunk
term = RollTerm.fromData(foundry.utils.deepClone(term.toJSON()));
term = foundry.dice.terms.RollTerm.fromData(foundry.utils.deepClone(term.toJSON()));
currentChunk ??= { terms: [], negative, type: null };
currentChunk.terms.push(term);
const flavor = term.flavor?.toLowerCase().trim();
Expand Down
2 changes: 2 additions & 0 deletions module/dice/d20-roll.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { Die, NumericTerm, OperatorTerm } = foundry.dice.terms;

/**
* A type of Roll specific to a d20-based check, save, or attack roll in the 5e system.
* @param {string} formula The string formula to parse
Expand Down
4 changes: 3 additions & 1 deletion module/dice/damage-roll.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { DiceTerm, FunctionTerm, NumericTerm, OperatorTerm, ParentheticalTerm } = foundry.dice.terms;

/**
* A type of Roll specific to a damage (or healing) roll in the 5e system.
* @param {string} formula The string formula to parse
Expand Down Expand Up @@ -89,7 +91,7 @@ export default class DamageRoll extends Roll {
}

// Merge any parenthetical terms followed by string terms
else if ( (term instanceof ParentheticalTerm || term instanceof MathTerm) && (nextTerm instanceof StringTerm)
else if ( (term instanceof ParentheticalTerm || term instanceof FunctionTerm) && (nextTerm instanceof StringTerm)
&& nextTerm.term.match(/^d[0-9]*$/)) {
if ( term.isDeterministic ) {
const newFormula = `${term.evaluate().total}${nextTerm.term}`;
Expand Down
10 changes: 7 additions & 3 deletions module/dice/simplify-roll-formula.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const {
Coin, DiceTerm, Die, FunctionTerm, NumericTerm, OperatorTerm, ParentheticalTerm, RollTerm
} = foundry.dice.terms;

/**
* A standardized helper function for simplifying the constant parts of a multipart roll formula.
*
Expand Down Expand Up @@ -196,8 +200,8 @@ function _expandParentheticalTerms(terms) {
/* -------------------------------------------- */

/**
* A helper function to group terms into PoolTerms, DiceTerms, MathTerms, and NumericTerms.
* MathTerms are included as NumericTerms if they are deterministic.
* A helper function to group terms into PoolTerms, DiceTerms, FunctionTerms, and NumericTerms.
* FunctionTerms are included as NumericTerms if they are deterministic.
* @param {RollTerm[]} terms An array of roll terms.
* @returns {object} An object mapping term types to arrays containing roll terms of that type.
*/
Expand All @@ -208,7 +212,7 @@ function _groupTermsByType(terms) {
return terms.reduce((obj, term, i) => {
let type;
if ( term instanceof DiceTerm ) type = DiceTerm;
else if ( (term instanceof MathTerm) && (term.isDeterministic) ) type = NumericTerm;
else if ( (term instanceof FunctionTerm) && (term.isDeterministic) ) type = NumericTerm;
else type = term.constructor;
const key = `${type.name.charAt(0).toLowerCase()}${type.name.substring(1)}s`;

Expand Down
8 changes: 5 additions & 3 deletions module/documents/chat-message.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -481,9 +481,11 @@ export default class ChatMessage5e extends ChatMessage {
const aggregate = { type: roll.options.type, total: roll.total, constant: 0, dice: [] };
for ( let i = roll.terms.length - 1; i >= 0; ) {
const term = roll.terms[i--];
if ( !(term instanceof NumericTerm) && !(term instanceof DiceTerm) ) continue;
if ( !(term instanceof foundry.dice.terms.NumericTerm) && !(term instanceof foundry.dice.terms.DiceTerm) ) {
continue;
}
const value = term.total;
if ( term instanceof DiceTerm ) aggregate.dice.push(...term.results.map(r => ({
if ( term instanceof foundry.dice.terms.DiceTerm ) aggregate.dice.push(...term.results.map(r => ({
result: term.getResultLabel(r), classes: term.getResultCSS(r).filterJoin(" ")
})));
let multiplier = 1;
Expand All @@ -492,7 +494,7 @@ export default class ChatMessage5e extends ChatMessage {
if ( operator.operator === "-" ) multiplier *= -1;
operator = roll.terms[--i];
}
if ( term instanceof NumericTerm ) aggregate.constant += value * multiplier;
if ( term instanceof foundry.dice.terms.NumericTerm ) aggregate.constant += value * multiplier;
}

return aggregate;
Expand Down
4 changes: 2 additions & 2 deletions module/documents/item.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1682,10 +1682,10 @@ export default class Item5e extends SystemDocumentMixin(Item) {

// Attempt to simplify by combining like dice terms
let simplified = false;
if ( (s.terms[0] instanceof Die) && (s.terms.length === 1) ) {
if ( (s.terms[0] instanceof foundry.dice.terms.Die) && (s.terms.length === 1) ) {
const d0 = p0.terms[0];
const s0 = s.terms[0];
if ( (d0 instanceof Die) && (d0.faces === s0.faces) && d0.modifiers.equals(s0.modifiers) ) {
if ( (d0 instanceof foundry.dice.terms.Die) && (d0.faces === s0.faces) && d0.modifiers.equals(s0.modifiers) ) {
d0.number += s0.number;
parts[0] = p0.formula;
simplified = true;
Expand Down
1 change: 0 additions & 1 deletion module/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,6 @@ function _synchronizeActorSpells(actor, spellsMap) {
Object.assign(spellData.system, {preparation, uses});
spellData.system.save.dc = save.dc;
foundry.utils.setProperty(spellData, "_stats.compendiumSource", source.uuid);
foundry.utils.setProperty(spellData, "flags.core.sourceId", source.uuid);

// Record spells to be deleted and created
toDelete.push(spell.id);
Expand Down

0 comments on commit b06d693

Please sign in to comment.