Fix adversary damage

This commit is contained in:
Carlos Fernandez 2026-07-17 23:55:05 -04:00
parent 6ae07bd3a7
commit 23ebe6f6b5
6 changed files with 114 additions and 99 deletions

View file

@ -159,7 +159,8 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2)
context.tabs = this._getTabs(this.constructor.TABS); context.tabs = this._getTabs(this.constructor.TABS);
context.config = CONFIG.DH; context.config = CONFIG.DH;
if (this.action.damage) { if (this.action.damage) {
context.allDamageTypesUsed = !this.#getUnusedDamageTypes().length; const allKeys = Object.keys(CONFIG.DH.GENERAL.healingTypes);
context.allDamageTypesUsed = allKeys.every(k => k in this.action._source.damage.resources);
if (this.action.damage?.main?.hasOwnProperty('includeBase') && this.action.type === 'attack') if (this.action.damage?.main?.hasOwnProperty('includeBase') && this.action.type === 'attack')
context.hasBaseDamage = !!this.action.parent.attack; context.hasBaseDamage = !!this.action.parent.attack;
} }
@ -232,23 +233,6 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2)
return filtered; return filtered;
} }
/**
* Gets unused resource types of the damage field
* @returns {{ value: string; label: string }[]}
*/
#getUnusedDamageTypes() {
const usedKeys = Object.keys(this.action._source.damage.resources);
return Object.keys(CONFIG.DH.GENERAL.healingTypes).reduce((acc, key) => {
if (!usedKeys.includes(key))
acc.push({
value: key,
label: game.i18n.localize(CONFIG.DH.GENERAL.healingTypes[key].label)
});
return acc;
}, []);
}
_prepareSubmitData(_event, formData) { _prepareSubmitData(_event, formData) {
const submitData = foundry.utils.expandObject(formData.object); const submitData = foundry.utils.expandObject(formData.object);
@ -317,6 +301,7 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2)
this.constructor.updateForm.bind(this)(null, null, { object: foundry.utils.flattenObject(data) }); this.constructor.updateForm.bind(this)(null, null, { object: foundry.utils.flattenObject(data) });
} }
/** @this DHActionBaseConfig */
static #onAddDamage() { static #onAddDamage() {
if (!this.action.damage || this.action.damage?.main) return; if (!this.action.damage || this.action.damage?.main) return;
@ -329,6 +314,7 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2)
this.constructor.updateForm.bind(this)(null, null, { object: foundry.utils.flattenObject(data) }); this.constructor.updateForm.bind(this)(null, null, { object: foundry.utils.flattenObject(data) });
} }
/** @this DHActionBaseConfig */
static #onRemoveDamage() { static #onRemoveDamage() {
if (!this.action.damage?.main) return; if (!this.action.damage?.main) return;
const data = this.action.toObject(); const data = this.action.toObject();
@ -340,50 +326,46 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2)
static #onAddDamageResource(_event) { static #onAddDamageResource(_event) {
if (!this.action.damage) return; if (!this.action.damage) return;
const choices = this.#getUnusedDamageTypes(); const allKeys = Object.keys(CONFIG.DH.GENERAL.healingTypes);
const unused = allKeys.filter(k => !(k in this.action._source.damage.resources));
const choices = unused.map(k => ({ value: k, label: _loc(CONFIG.DH.GENERAL.healingTypes[k].label) }));
const content = new foundry.data.fields.StringField({ const content = new foundry.data.fields.StringField({
label: game.i18n.localize('Damage Type'), label: _loc('DAGGERHEART.GENERAL.damageType'),
choices, choices,
required: true required: true
}).toFormGroup( }).toFormGroup({}, {
{}, name: 'type',
{ localize: true,
name: 'type', nameAttr: 'value',
localize: true, labelAttr: 'label'
nameAttr: 'value', }).outerHTML;
labelAttr: 'label'
}
).outerHTML;
const callback = (_, button) => { const callback = (_, button) => {
const data = this.action.toObject(); const data = this.action.toObject();
const type = choices[button.form.elements.type.value].value; const type = choices[button.form.elements.type.value].value;
const part = this.action.schema.fields.damage.fields.resources.element.getInitialValue(); data.damage.resources[type] = {
part.applyTo = type; ...this.action.schema.fields.damage.fields.resources.element.getInitialValue(),
if (type === CONFIG.DH.GENERAL.healingTypes.hitPoints.id) applyTo: type
part.type = this.action.schema.fields.damage.fields.resources.element.fields.type.element.initial; };
data.damage.resources[type] = part;
this.constructor.updateForm.bind(this)(null, null, { object: foundry.utils.flattenObject(data) }); this.constructor.updateForm.bind(this)(null, null, { object: foundry.utils.flattenObject(data) });
}; };
const typeDialog = new foundry.applications.api.DialogV2({ const typeDialog = new foundry.applications.api.DialogV2({
buttons: [ buttons: [
foundry.utils.mergeObject( {
{ action: 'ok',
action: 'ok', label: 'Confirm',
label: 'Confirm', icon: 'fas fa-check',
icon: 'fas fa-check', default: true,
default: true callback
}, }
{ callback: callback }
)
], ],
content: content, content: content,
rejectClose: false, rejectClose: false,
modal: false, modal: false,
window: { window: {
title: game.i18n.localize('Add Damage') /** @todo localize */
title: 'Add Damage'
}, },
position: { width: 300 } position: { width: 300 }
}); });
@ -391,6 +373,7 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2)
typeDialog.render(true); typeDialog.render(true);
} }
/** @this DHActionBaseConfig */
static #onRemoveDamageResource(_event, button) { static #onRemoveDamageResource(_event, button) {
if (!this.action.damage?.resources) return; if (!this.action.damage?.resources) return;
const data = this.action.toObject(); const data = this.action.toObject();

View file

@ -1,3 +1,4 @@
import { DHDamageData } from '../../data/fields/action/damageField.mjs';
import DHBaseActorSettings from '../sheets/api/actor-setting.mjs'; import DHBaseActorSettings from '../sheets/api/actor-setting.mjs';
/**@typedef {import('@client/applications/_types.mjs').ApplicationClickAction} ApplicationClickAction */ /**@typedef {import('@client/applications/_types.mjs').ApplicationClickAction} ApplicationClickAction */
@ -8,8 +9,10 @@ export default class DHAdversarySettings extends DHBaseActorSettings {
classes: ['adversary-settings'], classes: ['adversary-settings'],
position: { width: 455, height: 'auto' }, position: { width: 455, height: 'auto' },
actions: { actions: {
addExperience: DHAdversarySettings.#addExperience, addExperience: DHAdversarySettings.#onAddExperience,
removeExperience: DHAdversarySettings.#removeExperience removeExperience: DHAdversarySettings.#onRemoveExperience,
addDamage: this.#onAddDamage,
removeDamage: this.#onRemoveDamage
} }
}; };
@ -71,7 +74,7 @@ export default class DHAdversarySettings extends DHBaseActorSettings {
* Adds a new experience entry to the actor. * Adds a new experience entry to the actor.
* @type {ApplicationClickAction} * @type {ApplicationClickAction}
*/ */
static async #addExperience() { static async #onAddExperience() {
const newExperience = { const newExperience = {
name: 'Experience', name: 'Experience',
modifier: 0 modifier: 0
@ -83,7 +86,7 @@ export default class DHAdversarySettings extends DHBaseActorSettings {
* Removes an experience entry from the actor. * Removes an experience entry from the actor.
* @type {ApplicationClickAction} * @type {ApplicationClickAction}
*/ */
static async #removeExperience(_, target) { static async #onRemoveExperience(_, target) {
const experience = this.actor.system.experiences[target.dataset.experience]; const experience = this.actor.system.experiences[target.dataset.experience];
const confirmed = await foundry.applications.api.DialogV2.confirm({ const confirmed = await foundry.applications.api.DialogV2.confirm({
window: { window: {
@ -98,4 +101,28 @@ export default class DHAdversarySettings extends DHBaseActorSettings {
await this.actor.update({ [`system.experiences.${target.dataset.experience}`]: _del }); await this.actor.update({ [`system.experiences.${target.dataset.experience}`]: _del });
} }
/**
* @this DHAdversarySettings
* @type {ApplicationClickAction}
*/
static #onAddDamage() {
this.actor.update({
'system.attack.damage.main': {
...DHDamageData.schema.getInitialValue(),
applyTo: 'hitPoints',
type: 'physical'
}
});
}
/**
* @this DHAdversarySettings
* @type {ApplicationClickAction}
*/
static #onRemoveDamage() {
this.actor.update({
'system.attack.damage.main': null
});
}
} }

View file

@ -82,7 +82,7 @@ export default class DHAttackAction extends DHDamageAction {
x: game.i18n.localize('DAGGERHEART.GENERAL.damage') x: game.i18n.localize('DAGGERHEART.GENERAL.damage')
}); });
const icons = Array.from(type) const icons = Array.from(type ?? [])
.map(t => CONFIG.DH.GENERAL.damageTypes[t]?.icon) .map(t => CONFIG.DH.GENERAL.damageTypes[t]?.icon)
.filter(Boolean); .filter(Boolean);

View file

@ -232,7 +232,7 @@ export default class DHWeapon extends AttachableItem {
const parts = value.custom.enabled ? [game.i18n.localize('DAGGERHEART.GENERAL.custom')] : [value.dice]; const parts = value.custom.enabled ? [game.i18n.localize('DAGGERHEART.GENERAL.custom')] : [value.dice];
if (!value.custom.enabled && value.bonus) parts.push(value.bonus.signedString()); if (!value.custom.enabled && value.bonus) parts.push(value.bonus.signedString());
if (type.size > 0) { if (type?.size) {
const typeTags = Array.from(type) const typeTags = Array.from(type)
.map(t => game.i18n.localize(`DAGGERHEART.CONFIG.DamageType.${t}.abbreviation`)) .map(t => game.i18n.localize(`DAGGERHEART.CONFIG.DamageType.${t}.abbreviation`))
.join(' | '); .join(' | ');
@ -259,7 +259,7 @@ export default class DHWeapon extends AttachableItem {
for (const { value, type } of [damage.main, ...damage.resources].filter(d => !!d)) { for (const { value, type } of [damage.main, ...damage.resources].filter(d => !!d)) {
const str = Roll.replaceFormulaData(value.getFormula(), this.actor?.getRollData() ?? {}); const str = Roll.replaceFormulaData(value.getFormula(), this.actor?.getRollData() ?? {});
const icons = Array.from(type) const icons = Array.from(type ?? [])
.map(t => CONFIG.DH.GENERAL.damageTypes[t]?.icon) .map(t => CONFIG.DH.GENERAL.damageTypes[t]?.icon)
.filter(Boolean); .filter(Boolean);

View file

@ -24,47 +24,53 @@
{{formField baseFields.main.fields.groupAttack value=source.main.groupAttack name=(concat path "damage.main.groupAttack") localize=true classes="select"}} {{formField baseFields.main.fields.groupAttack value=source.main.groupAttack name=(concat path "damage.main.groupAttack") localize=true classes="select"}}
{{/if}} {{/if}}
</div> </div>
{{> damageData data=source.main fields=fields.main.fields basePath=(concat path "damage.main")}} {{> damageData damage=source.main fields=fields.main.fields basePath=(concat path "damage.main")}}
{{#if horde}}
{{> hordeDamage source=source.main fields=fields.main.fields basePath=(concat path "damage.main")}}
{{/if}}
{{#if (ne @root.source.type 'healing')}} {{#if (ne @root.source.type 'healing')}}
{{formField fields.main.fields.type value=source.main.type name=(concat path "damage.main.type") localize=true}} {{formField fields.main.fields.type value=source.main.type name=(concat path "damage.main.type") localize=true}}
{{/if}} {{/if}}
{{/if}} {{/if}}
</fieldset> </fieldset>
<fieldset class="one-column"> {{#unless (eq path 'system.attack.')}}
<legend class="with-icon"> {{! In the future, consider allowing this even on NPCs}}
{{localize "DAGGERHEART.GENERAL.Resource.plural"}} <fieldset class="one-column">
{{#unless (eq path 'system.attack.')}}<a data-action="addDamageResource" {{#if @root.allDamageTypesUsed}}disabled{{/if}}><i class="fa-solid fa-plus icon-button"></i></a>{{/unless}} <legend class="with-icon">
</legend> {{localize "DAGGERHEART.GENERAL.Resource.plural"}}
{{#each source.resources as |dmg key|}} {{#unless @root.allDamageTypesUsed}}<a data-action="addDamageResource"><i class="fa-solid fa-plus icon-button"></i></a>{{/unless}}
<div class="nest-inputs"> </legend>
<fieldset{{#if dmg.base}} disabled{{/if}} class="one-column{{#if ../path}} no-style{{/if}}"> {{#each source.resources as |dmg key|}}
<legend class="with-icon"> <div class="nest-inputs">
{{localize (concat "DAGGERHEART.CONFIG.HealingType." dmg.applyTo ".name")}} <fieldset{{#if dmg.base}} disabled{{/if}} class="one-column">
{{#unless (or dmg.base ../path)}} <legend class="with-icon">
<a data-action="removeDamageResource" data-key="{{key}}"><i class="fas fa-trash"></i></a> {{localize (concat "DAGGERHEART.CONFIG.HealingType." dmg.applyTo ".name")}}
{{/unless}} {{#unless (or dmg.base ../path)}}
</legend> <a data-action="removeDamageResource" data-key="{{key}}"><i class="fas fa-trash"></i></a>
{{> damageData data=dmg fields=../fields.resources.element.fields basePath=(concat path "damage.resources." dmg.applyTo)}} {{/unless}}
</fieldset> </legend>
</div> {{> damageData damage=dmg fields=../fields.resources.element.fields basePath=(concat ../path "damage.resources." dmg.applyTo)}}
{{/each}} </fieldset>
</fieldset> </div>
{{/each}}
</fieldset>
{{/unless}}
{{#*inline "formula"}} {{#*inline "formula"}}
{{#unless dmg.base}} {{#unless isBase}}
{{formField fields.custom.fields.enabled value=source.custom.enabled name=(concat basePath "." target ".custom.enabled") classes="checkbox" localize=true}} {{formField fields.custom.fields.enabled value=source.custom.enabled name=(concat basePath ".custom.enabled") classes="checkbox" localize=true}}
{{/unless}} {{/unless}}
{{#if source.custom.enabled}} {{#if source.custom.enabled}}
{{formField fields.custom.fields.formula value=source.custom.formula name=(concat basePath "." target ".custom.formula") localize=true}} {{formField fields.custom.fields.formula value=source.custom.formula name=(concat basePath ".custom.formula") localize=true}}
{{else}} {{else}}
<div class="nest-inputs"> <div class="nest-inputs">
{{#unless @root.isNPC}} {{#unless @root.isNPC}}
{{formField fields.multiplier value=source.multiplier name=(concat basePath "." target ".multiplier") localize=true}} {{formField fields.multiplier value=source.multiplier name=(concat basePath ".multiplier") localize=true}}
{{/unless}} {{/unless}}
{{#if (eq source.multiplier 'flat')}}{{formField fields.flatMultiplier value=source.flatMultiplier name=(concat basePath "." target ".flatMultiplier") localize=true }}{{/if}} {{#if (eq source.multiplier 'flat')}}{{formField fields.flatMultiplier value=source.flatMultiplier name=(concat basePath ".flatMultiplier") localize=true }}{{/if}}
{{formField fields.dice value=source.dice name=(concat basePath "." target ".dice") localize=true}} {{formField fields.dice value=source.dice name=(concat basePath ".dice") localize=true}}
{{formField fields.bonus value=source.bonus name=(concat basePath "." target ".bonus") localize=true}} {{formField fields.bonus value=source.bonus name=(concat basePath ".bonus") localize=true}}
</div> </div>
{{/if}} {{/if}}
{{#if @root.isNPC}} {{#if @root.isNPC}}
@ -73,35 +79,34 @@
{{/inline}} {{/inline}}
{{#*inline "damageData"}} {{#*inline "damageData"}}
{{#if (and (not @root.isNPC) @root.hasRoll (not data.base))}} {{#if (and (not @root.isNPC) @root.hasRoll (not damage.base))}}
{{formField fields.resultBased value=data.resultBased name=(concat basePath ".resultBased") localize=true classes="checkbox"}} {{formField fields.resultBased value=damage.resultBased name=(concat basePath ".resultBased") localize=true classes="checkbox"}}
{{/if}} {{/if}}
{{#if (and (not @root.isNPC) @root.hasRoll (not data.base) data.resultBased)}} {{#if (and (not @root.isNPC) @root.hasRoll (not damage.base) damage.resultBased)}}
<div class="nest-inputs"> <div class="nest-inputs">
<fieldset class="one-column"> <fieldset class="one-column">
<legend>{{localize "DAGGERHEART.GENERAL.withThing" thing=(localize "DAGGERHEART.GENERAL.hope")}}</legend> <legend>{{localize "DAGGERHEART.GENERAL.withThing" thing=(localize "DAGGERHEART.GENERAL.hope")}}</legend>
{{> formula fields=fields.value.fields type=fields.type dmg=data source=data.value target="value" key=data.applyTo path=../path}} {{> formula key=damage.applyTo fields=fields.value.fields type=fields.type isBase=damage.base source=damage.value basePath=(concat basePath ".value")}}
</fieldset> </fieldset>
<fieldset class="one-column"> <fieldset class="one-column">
<legend>{{localize "DAGGERHEART.GENERAL.withThing" thing=(localize "DAGGERHEART.GENERAL.fear")}}</legend> <legend>{{localize "DAGGERHEART.GENERAL.withThing" thing=(localize "DAGGERHEART.GENERAL.fear")}}</legend>
{{> formula fields=fields.valueAlt.fields type=fields.type dmg=data source=data.valueAlt target="valueAlt" key=data.applyTo path=../path}} {{> formula key=damage.applyTo fields=fields.valueAlt.fields type=fields.type isBase=damage.base source=damage.valueAlt basePath=(concat basePath ".valueAlt")}}
</fieldset> </fieldset>
</div> </div>
{{else}} {{else}}
{{> formula fields=fields.value.fields type=fields.type dmg=data source=data.value basePath=basePath target="value" key=data.applyTo path=../path}} {{> formula key=damage.applyTo fields=fields.value.fields type=fields.type isBase=damage.base source=damage.value basePath=(concat basePath ".value")}}
{{/if}} {{/if}}
<input type="hidden" name="{{concat basePath ".base"}}" value="{{damage.base}}">
{{/inline}}
{{#if ../horde}} {{#*inline "hordeDamage"}}
<fieldset class="one-column"> <fieldset class="one-column">
<legend>{{localize "DAGGERHEART.ACTORS.Adversary.hordeDamage"}}</legend> <legend>{{localize "DAGGERHEART.ACTORS.Adversary.hordeDamage"}}</legend>
<div class="nest-inputs"> <div class="nest-inputs">
<input type="hidden" name="{{basePath}}.valueAlt.multiplier" value="flat">
<input type="hidden" name="{{../path}}damage.parts.{{data.applyTo}}.valueAlt.multiplier" value="flat"> {{formField fields.valueAlt.fields.flatMultiplier value=source.valueAlt.flatMultiplier name=(concat basePath ".valueAlt.flatMultiplier") label="DAGGERHEART.ACTIONS.Settings.multiplier" classes="inline-child" localize=true }}
{{formField fields.valueAlt.fields.flatMultiplier value=data.valueAlt.flatMultiplier name=(concat basePath ".valueAlt.flatMultiplier") label="DAGGERHEART.ACTIONS.Settings.multiplier" classes="inline-child" localize=true }} {{formField fields.valueAlt.fields.dice value=source.valueAlt.dice name=(concat basePath ".valueAlt.dice") classes="inline-child" localize=true}}
{{formField fields.valueAlt.fields.dice value=data.valueAlt.dice name=(concat basePath ".valueAlt.dice") classes="inline-child" localize=true}} {{formField fields.valueAlt.fields.bonus value=source.valueAlt.bonus name=(concat basePath ".valueAlt.bonus") localize=true classes="inline-child"}}
{{formField fields.valueAlt.fields.bonus value=data.valueAlt.bonus name=(concat basePath ".valueAlt.bonus") localize=true classes="inline-child"}} </div>
</div> </fieldset>
</fieldset>
{{/if}}
<input type="hidden" name="{{concat basePath ".base"}}" value="{{data.base}}">
{{/inline}} {{/inline}}

View file

@ -22,5 +22,5 @@
</div> </div>
{{formGroup systemFields.criticalThreshold value=document._source.system.criticalThreshold label="DAGGERHEART.ACTIONS.Settings.criticalThreshold" name="system.criticalThreshold" localize=true}} {{formGroup systemFields.criticalThreshold value=document._source.system.criticalThreshold label="DAGGERHEART.ACTIONS.Settings.criticalThreshold" name="system.criticalThreshold" localize=true}}
</fieldset> </fieldset>
{{> 'systems/daggerheart/templates/actionTypes/damage.hbs' fields=systemFields.attack.fields.damage.fields.parts.element.fields source=document.system.attack.damage path="system.attack." baseFields=systemFields.attack.fields.damage.fields horde=(eq document._source.system.type 'horde')}} {{> 'systems/daggerheart/templates/actionTypes/damage.hbs' fields=systemFields.attack.fields.damage.fields source=document.system.attack.damage path="system.attack." baseFields=systemFields.attack.fields.damage.fields horde=(eq document._source.system.type 'horde')}}
</section> </section>