modify weapon data to split formula.

This commit is contained in:
IrkTheImp 2025-07-03 11:28:04 -05:00
parent b9be9b9667
commit 4d64a99bb9
6 changed files with 26 additions and 50 deletions

View file

@ -123,6 +123,18 @@ export class DHRoll extends Roll {
}
return (this._formula = this.constructor.getFormula(this.terms));
}
static calculateTotalModifiers(roll, config) {
config.roll.modifierTotal = 0;
for (let i = 0; i < roll.terms.length; i++) {
if (
roll.terms[i] instanceof foundry.dice.terms.NumericTerm &&
!!roll.terms[i - 1] &&
roll.terms[i - 1] instanceof foundry.dice.terms.OperatorTerm
)
config.roll.modifierTotal += Number(`${roll.terms[i - 1].operator}${roll.terms[i].total}`);
}
}
}
export class DualityDie extends foundry.dice.terms.Die {
@ -298,15 +310,8 @@ export class D20Roll extends DHRoll {
value: d.total
};
});
config.roll.modifierTotal = 0;
for (let i = 0; i < roll.terms.length; i++) {
if (
roll.terms[i] instanceof foundry.dice.terms.NumericTerm &&
!!roll.terms[i - 1] &&
roll.terms[i - 1] instanceof foundry.dice.terms.OperatorTerm
)
config.roll.modifierTotal += Number(`${roll.terms[i - 1].operator}${roll.terms[i].total}`);
}
this.calculateTotalModifiers(roll, config);
}
resetFormula() {
@ -464,17 +469,10 @@ export class DamageRoll extends DHRoll {
static DefaultDialog = DamageDialog;
static async getDamageModifiers(roll) {
const modifierFormula = roll.formula.replace(/[\d]+d[\d]+([a-zA-Z0-9.<>!=]*)?/g, '').replace(/\s*\+\s*/, '');
const modifierRoll = new Roll(modifierFormula);
await modifierRoll.evaluate();
return modifierRoll.total;
}
static async postEvaluate(roll, config = {}) {
super.postEvaluate(roll, config);
config.roll.type = config.type;
config.roll.modifierTotal = await this.getDamageModifiers(roll);
this.calculateTotalModifiers(roll, config);
if (config.source?.message) {
const chatMessage = ui.chat.collection.get(config.source.message);
chatMessage.update({ 'system.damage': config });

View file

@ -621,43 +621,15 @@ export class DHAttackAction extends DHDamageAction {
super.prepareData();
if (this.damage.includeBase && !!this.item?.system?.damage) {
const baseDamage = this.getParentDamage();
this.cleanBaseDamage(baseDamage);
this.damage.parts.unshift(new DHDamageData(baseDamage));
}
}
cleanBaseDamage(baseDamage) {
let possibleRoll = new Roll(baseDamage.value.dice);
let die = 'd6';
let modifier = 0;
let nextOperator = '+';
possibleRoll.terms.forEach(term => {
if (term instanceof DiceTerm) {
die = `d${term._faces}`;
}
if (term instanceof OperatorTerm) {
nextOperator = term.operator;
}
if (term instanceof NumericTerm) {
if (nextOperator == '+') {
modifier += term.number;
}
if (nextOperator == '-') {
modifier -= term.number;
}
}
});
return (baseDamage.value = { ...baseDamage.value, dice: die, bonus: modifier });
}
getParentDamage() {
return {
value: {
multiplier: 'prof',
dice: this.item?.system?.damage.value,
dice: this.item?.system?.damage.dice,
bonus: this.item?.system?.damage.bonus ?? 0
},
type: this.item?.system?.damage.type,

View file

@ -32,6 +32,8 @@ export default class DHWeapon extends BaseDataItem {
burden: new fields.StringField({ required: true, choices: SYSTEM.GENERAL.burden, initial: 'oneHanded' }),
//DAMAGE
damage: new fields.SchemaField({
dice: new fields.StringField({ choices: SYSTEM.GENERAL.diceTypes, initial: 'd6' }),
bonus: new fields.NumberField({ nullable: true, initial: null }),
value: new FormulaField({ initial: 'd6' }),
type: new fields.StringField({
required: true,