Damages parts roll

This commit is contained in:
Dapoolp 2025-07-18 02:29:31 +02:00
parent a336220281
commit a7d2916e93
7 changed files with 145 additions and 38 deletions

View file

@ -46,7 +46,7 @@ export default class DamageDialog extends HandlebarsApplicationMixin(Application
context.title = this.config.title
? this.config.title
: game.i18n.localize('DAGGERHEART.EFFECTS.ApplyLocations.damageRoll.name');
context.extraFormula = this.config.extraFormula;
// context.extraFormula = this.config.extraFormula;
context.formula = this.roll.constructFormula(this.config);
context.directDamage = this.config.directDamage;
context.selectedRollMode = this.config.selectedRollMode;
@ -61,7 +61,7 @@ export default class DamageDialog extends HandlebarsApplicationMixin(Application
static updateRollConfiguration(_event, _, formData) {
const { ...rest } = foundry.utils.expandObject(formData.object);
this.config.extraFormula = rest.extraFormula;
foundry.utils.mergeObject(this.config.roll, rest.roll)
this.config.selectedRollMode = rest.selectedRollMode;
this.render();

View file

@ -1,3 +1,4 @@
import { setsEqual } from '../../helpers/utils.mjs';
import DHBaseAction from './baseAction.mjs';
export default class DHDamageAction extends DHBaseAction {
@ -18,27 +19,53 @@ export default class DHDamageAction extends DHBaseAction {
return formulaValue;
}
formatFormulas(formulas, systemData) {
const formattedFormulas = [];
formulas.forEach(formula => {
if (isNaN(formula.formula)) formula.formula = Roll.replaceFormulaData(formula.formula, this.getRollData(systemData));
const same = formattedFormulas.find(f => setsEqual(f.damageTypes, formula.damageTypes) && f.applyTo === formula.applyTo);
if(same)
same.formula += ` + ${formula.formula}`;
else
formattedFormulas.push(formula);
})
return formattedFormulas;
}
async rollDamage(event, data) {
const systemData = data.system ?? data;
let formula = this.damage.parts.map(p => this.getFormulaValue(p, data).getFormula(this.actor)).join(' + '),
/* let formula = this.damage.parts.map(p => this.getFormulaValue(p, data).getFormula(this.actor)).join(' + '),
damageTypes = [...new Set(this.damage.parts.reduce((a, c) => a.concat([...c.type]), []))];
damageTypes = !damageTypes.length ? ['physical'] : damageTypes;
if (!formula || formula == '') return;
let roll = { formula: formula, total: formula };
if (!formula || formula == '') return; */
if (isNaN(formula)) formula = Roll.replaceFormulaData(formula, this.getRollData(systemData));
let formulas = this.damage.parts.map(p => ({
formula: this.getFormulaValue(p, data).getFormula(this.actor),
damageTypes: p.type,
applyTo: p.applyTo
}));
if(!formulas.length) return;
formulas = this.formatFormulas(formulas, systemData);
/* let roll = { formula: formula, total: formula };
if (isNaN(formula)) formula = Roll.replaceFormulaData(formula, this.getRollData(systemData)); */
const config = {
title: game.i18n.format('DAGGERHEART.UI.Chat.damageRoll.title', { damage: this.name }),
roll: { formula },
// roll: { formula },
// roll: { formulas },
roll: formulas,
targets: systemData.targets.filter(t => t.hit) ?? data.targets,
hasSave: this.hasSave,
isCritical: systemData.roll?.isCritical ?? false,
source: systemData.source,
data: this.getRollData(),
damageTypes,
// damageTypes,
event
};
if (this.hasSave) config.onSave = this.save.damageMod;
@ -49,6 +76,7 @@ export default class DHDamageAction extends DHBaseAction {
config.directDamage = true;
}
roll = CONFIG.Dice.daggerheart.DamageRoll.build(config);
// roll = CONFIG.Dice.daggerheart.DamageRoll.build(config);
CONFIG.Dice.daggerheart.DamageRoll.build(config);
}
}

View file

@ -24,12 +24,13 @@ export default class DamageRoll extends DHRoll {
}
}
applyBaseBonus() {
applyBaseBonus(part) {
const modifiers = [],
type = this.options.messageType ?? 'damage';
type = this.options.messageType ?? 'damage',
options = part ?? this.options;
modifiers.push(...this.getBonus(`${type}`, `${type.capitalize()} Bonus`));
this.options.damageTypes?.forEach(t => {
options.damageTypes?.forEach(t => {
modifiers.push(...this.getBonus(`${type}.${t}`, `${t.capitalize()} ${type.capitalize()} Bonus`));
});
const weapons = ['primaryWeapon', 'secondaryWeapon'];
@ -42,13 +43,70 @@ export default class DamageRoll extends DHRoll {
}
constructFormula(config) {
super.constructFormula(config);
this.options.roll.forEach( part => {
part.roll = new Roll(part.formula);
this.constructFormulaPart(config, part)
})
return this.options.roll;
}
if (config.isCritical) {
const tmpRoll = new Roll(this._formula)._evaluateSync({ maximize: true }),
constructFormulaPart(config, part) {
part.roll.terms = Roll.parse(part.roll.formula, config.data);
if(part.applyTo === CONFIG.DH.GENERAL.healingTypes.hitPoints.id) {
part.modifiers = this.applyBaseBonus(part);
this.addModifiers(part);
part.modifiers?.forEach(m => {
part.roll.terms.push(...this.formatModifier(m.value));
});
}
if (part.extraFormula) {
part.roll.terms.push(
new foundry.dice.terms.OperatorTerm({ operator: '+' }),
...this.constructor.parse(part.extraFormula, this.options.data)
);
}
if (config.isCritical && part.applyTo === CONFIG.DH.GENERAL.healingTypes.hitPoints.id) {
const tmpRoll = Roll.fromTerms(part.roll.terms)._evaluateSync({ maximize: true }),
criticalBonus = tmpRoll.total - this.constructor.calculateTotalModifiers(tmpRoll);
this.terms.push(...this.formatModifier(criticalBonus));
part.roll.terms.push(...this.formatModifier(criticalBonus));
}
return (this._formula = this.constructor.getFormula(this.terms));
return (part.roll._formula = this.constructor.getFormula(part.roll.terms));
}
async evaluate({minimize=false, maximize=false, allowStrings=false, allowInteractive=true, ...options}={}) {
if ( this._evaluated ) {
throw new Error(`The ${this.constructor.name} has already been evaluated and is now immutable`);
}
this._evaluated = true;
if ( CONFIG.debug.dice ) console.debug(`Evaluating roll with formula "${this.formula}"`);
// Migration path for async rolls
if ( "async" in options ) {
foundry.utils.logCompatibilityWarning("The async option for Roll#evaluate has been removed. "
+ "Use Roll#evaluateSync for synchronous roll evaluation.");
}
this.options.roll.forEach( async part => {
await part.roll.evaluate({minimize, maximize, allowStrings, allowInteractive, ...options})
})
// return this._evaluate({minimize, maximize, allowStrings, allowInteractive});
}
static postEvaluate(roll, config = {}) {
if (!config.roll) config.roll = {};
config.roll.total = roll.total;
config.roll.formula = roll.formula;
config.roll.dice = [];
roll.dice.forEach(d => {
config.roll.dice.push({
dice: d.denomination,
total: d.total,
formula: d.formula,
results: d.results
});
});
}
}

View file

@ -47,7 +47,7 @@ export default class DHRoll extends Roll {
static async buildEvaluate(roll, config = {}, message = {}) {
if (config.evaluate !== false) await roll.evaluate();
this.postEvaluate(roll, config);
config.roll = this.postEvaluate(roll);
}
static async buildPost(roll, config, message) {
@ -63,19 +63,17 @@ export default class DHRoll extends Roll {
}
}
static postEvaluate(roll, config = {}) {
if (!config.roll) config.roll = {};
config.roll.total = roll.total;
config.roll.formula = roll.formula;
config.roll.dice = [];
roll.dice.forEach(d => {
config.roll.dice.push({
static postEvaluate(roll) {
return {
total: roll.total,
formula: roll.formula,
dice: roll.dice.map(d => ({
dice: d.denomination,
total: d.total,
formula: d.formula,
results: d.results
});
});
}))
}
}
static async toMessage(roll, config) {
@ -118,8 +116,9 @@ export default class DHRoll extends Roll {
return [];
}
addModifiers() {
this.options.roll.modifiers?.forEach(m => {
addModifiers(roll) {
roll = roll ?? this.options.roll;
roll.modifiers?.forEach(m => {
this.terms.push(...this.formatModifier(m.value));
});
}

View file

@ -311,3 +311,15 @@ export const itemAbleRollParse = (value, actor, item) => {
return '';
}
};
export const arraysEqual = (a, b) =>
a.length === b.length &&
[...new Set([...a, ...b])].every(
v => a.filter(e => e === v).length === b.filter(e => e === v).length
);
export const setsEqual = (a, b) =>
a.size === b.size &&
[...a].every(
value => b.has(value)
);

View file

@ -22,8 +22,12 @@
{{formField ../fields.value.fields.bonus value=dmg.value.bonus name=(concat ../path "damage.parts." index ".value.bonus") localize=true classes="inline-child"}}
</div>
{{/if}}
<div class="nest-inputs">
{{formField ../fields.applyTo value=dmg.applyTo name=(concat ../path "damage.parts." realIndex ".applyTo") localize=true}}
{{#if (eq dmg.applyTo 'hitPoints')}}
{{formField ../fields.type value=dmg.type name=(concat ../path "damage.parts." index ".type") localize=true}}
{{/if}}
</div>
{{#if ../horde}}
<fieldset class="one-column">
<legend>{{localize "DAGGERHEART.ACTORS.Adversary.hordeDamage"}}</legend>
@ -57,8 +61,12 @@
{{> formula fields=../../fields.value.fields type=../fields.type dmg=dmg source=dmg.value target="value" realIndex=realIndex}}
</fieldset>
{{/if}}
<div class="nest-inputs">
{{formField ../../fields.applyTo value=dmg.applyTo name=(concat "damage.parts." realIndex ".applyTo") localize=true}}
{{#if (eq dmg.applyTo 'hitPoints')}}
{{formField ../../fields.type value=dmg.type name=(concat "damage.parts." realIndex ".type") localize=true}}
{{/if}}
</div>
<input type="hidden" name="damage.parts.{{realIndex}}.base" value="{{dmg.base}}">
</fieldset>
{{#unless dmg.base}}<div class="fas fa-trash" data-action="removeDamage" data-index="{{realIndex}}"></div>{{/unless}}

View file

@ -2,10 +2,12 @@
<header class="dialog-header">
<h1>{{title}}</h1>
</header>
<span class="formula-label"><b>Formula:</b> {{@root.formula}}</span>
{{#each @root.formula}}
<span class="formula-label"><b>Formula:</b> {{roll.formula}}</span>
<div class="form-group">
<input type="text" value="{{extraFormula}}" name="extraFormula" placeholder="Situational Bonus">
<input type="text" value="{{extraFormula}}" name="roll.{{ @index }}.extraFormula" placeholder="Situational Bonus">
</div>
{{/each}}
<div class="damage-section-controls">
{{#if directDamage}}
<select class="roll-mode-select" name="selectedRollMode">