[Feature] Damage Iterrable Rework (#1685)

* Initial

* More

* Fixed current actionConfig damage

* Reworked ActionConfig damage ui

* .

* Updated all Adversary compendium damage entries

* more

* The rest

* Fixed misses

* .

* .

* Also migrate sub fields of MappingField

* Removed MappingField

* Fix regression with re-tiering adversaries when dealing non-hp damage

* Allow iterable object to be detected as an object by foundry

---------

Co-authored-by: Carlos Fernandez <cfern1990@gmail.com>
This commit is contained in:
WBHarry 2026-03-08 00:58:24 +01:00 committed by GitHub
parent d3ebd30e59
commit 5a4bbc91f5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
536 changed files with 2476 additions and 2368 deletions

View file

@ -67,7 +67,7 @@ export default class DhCompanionLevelUp extends BaseLevelUp {
break;
case 'summary':
const levelKeys = Object.keys(this.levelup.levels);
const actorDamageDice = this.actor.system.attack.damage.parts[0].value.dice;
const actorDamageDice = this.actor.system.attack.damage.parts.hitPoints.value.dice;
const actorRange = this.actor.system.attack.range;
let achievementExperiences = [];

View file

@ -1,3 +1,4 @@
import { getUnusedDamageTypes } from '../../helpers/utils.mjs';
import DaggerheartSheet from '../sheets/daggerheart-sheet.mjs';
const { ApplicationV2 } = foundry.applications.api;
@ -103,7 +104,7 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2)
}
};
static CLEAN_ARRAYS = ['damage.parts', 'cost', 'effects', 'summon'];
static CLEAN_ARRAYS = ['cost', 'effects', 'summon'];
_getTabs(tabs) {
for (const v of Object.values(tabs)) {
@ -155,6 +156,7 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2)
revealed: this.openTrigger === index
};
});
context.allDamageTypesUsed = !getUnusedDamageTypes(this.action.damage.parts).length;
const settingsTiers = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.LevelTiers).tiers;
context.tierOptions = [
@ -268,18 +270,61 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2)
static addDamage(_event) {
if (!this.action.damage.parts) return;
const data = this.action.toObject(),
part = {};
if (this.action.actor?.isNPC) part.value = { multiplier: 'flat' };
data.damage.parts.push(part);
this.constructor.updateForm.bind(this)(null, null, { object: foundry.utils.flattenObject(data) });
const choices = getUnusedDamageTypes(this.action.damage.parts);
const content = new foundry.data.fields.StringField({
label: game.i18n.localize('Damage Type'),
choices,
required: true
}).toFormGroup(
{},
{
name: 'type',
localize: true,
nameAttr: 'value',
labelAttr: 'label'
}
).outerHTML;
const callback = (_, button) => {
const data = this.action.toObject();
const type = choices[button.form.elements.type.value].value;
const part = { applyTo: type };
if (this.action.actor?.isNPC) part.value = { multiplier: 'flat' };
data.damage.parts[type] = part;
this.constructor.updateForm.bind(this)(null, null, { object: foundry.utils.flattenObject(data) });
};
const typeDialog = new foundry.applications.api.DialogV2({
buttons: [
foundry.utils.mergeObject(
{
action: 'ok',
label: 'Confirm',
icon: 'fas fa-check',
default: true
},
{ callback: callback }
)
],
content: content,
rejectClose: false,
modal: false,
window: {
title: game.i18n.localize('Add Damage')
},
position: { width: 300 }
});
typeDialog.render(true);
}
static removeDamage(_event, button) {
if (!this.action.damage.parts) return;
const data = this.action.toObject(),
index = button.dataset.index;
data.damage.parts.splice(index, 1);
const data = this.action.toObject();
const key = button.dataset.key;
delete data.damage.parts[key];
data.damage.parts[`-=${key}`] = null;
this.constructor.updateForm.bind(this)(null, null, { object: foundry.utils.flattenObject(data) });
}

View file

@ -472,7 +472,10 @@ export default function DHApplicationMixin(Base) {
icon: 'fa-solid fa-explosion',
condition: target => {
const doc = getDocFromElementSync(target);
return doc?.system?.attack?.damage.parts.length || doc?.damage?.parts.length;
return (
!foundry.utils.isEmpty(doc?.system?.attack?.damage.parts) ||
!foundry.utils.isEmpty(doc?.damage?.parts)
);
},
callback: async (target, event) => {
const doc = await getDocFromElement(target),

View file

@ -245,8 +245,8 @@ export const defaultRestOptions = {
type: 'friendly'
},
damage: {
parts: [
{
parts: {
hitPoints: {
applyTo: healingTypes.hitPoints.id,
value: {
custom: {
@ -255,7 +255,7 @@ export const defaultRestOptions = {
}
}
}
]
}
}
}
},
@ -279,8 +279,8 @@ export const defaultRestOptions = {
type: 'self'
},
damage: {
parts: [
{
parts: {
stress: {
applyTo: healingTypes.stress.id,
value: {
custom: {
@ -289,7 +289,7 @@ export const defaultRestOptions = {
}
}
}
]
}
}
}
},
@ -314,8 +314,8 @@ export const defaultRestOptions = {
type: 'friendly'
},
damage: {
parts: [
{
parts: {
armor: {
applyTo: healingTypes.armor.id,
value: {
custom: {
@ -324,7 +324,7 @@ export const defaultRestOptions = {
}
}
}
]
}
}
}
},
@ -348,8 +348,8 @@ export const defaultRestOptions = {
type: 'self'
},
damage: {
parts: [
{
parts: {
hope: {
applyTo: healingTypes.hope.id,
value: {
custom: {
@ -358,7 +358,7 @@ export const defaultRestOptions = {
}
}
}
]
}
}
},
prepareWithFriends: {
@ -372,8 +372,8 @@ export const defaultRestOptions = {
type: 'self'
},
damage: {
parts: [
{
parts: {
hope: {
applyTo: healingTypes.hope.id,
value: {
custom: {
@ -382,7 +382,7 @@ export const defaultRestOptions = {
}
}
}
]
}
}
}
},
@ -409,8 +409,8 @@ export const defaultRestOptions = {
type: 'friendly'
},
damage: {
parts: [
{
parts: {
hitPoints: {
applyTo: healingTypes.hitPoints.id,
value: {
custom: {
@ -419,7 +419,7 @@ export const defaultRestOptions = {
}
}
}
]
}
}
}
},
@ -443,8 +443,8 @@ export const defaultRestOptions = {
type: 'self'
},
damage: {
parts: [
{
parts: {
stress: {
applyTo: healingTypes.stress.id,
value: {
custom: {
@ -453,7 +453,7 @@ export const defaultRestOptions = {
}
}
}
]
}
}
}
},
@ -478,8 +478,8 @@ export const defaultRestOptions = {
type: 'friendly'
},
damage: {
parts: [
{
parts: {
armor: {
applyTo: healingTypes.armor.id,
value: {
custom: {
@ -488,7 +488,7 @@ export const defaultRestOptions = {
}
}
}
]
}
}
}
},
@ -512,8 +512,8 @@ export const defaultRestOptions = {
type: 'self'
},
damage: {
parts: [
{
parts: {
hope: {
applyTo: healingTypes.hope.id,
value: {
custom: {
@ -522,7 +522,7 @@ export const defaultRestOptions = {
}
}
}
]
}
}
},
prepareWithFriends: {
@ -536,8 +536,8 @@ export const defaultRestOptions = {
type: 'self'
},
damage: {
parts: [
{
parts: {
hope: {
applyTo: healingTypes.hope.id,
value: {
custom: {
@ -546,7 +546,7 @@ export const defaultRestOptions = {
}
}
}
]
}
}
}
},

View file

@ -14,8 +14,8 @@ export const armorFeatures = {
type: 'hostile'
},
damage: {
parts: [
{
parts: {
stress: {
applyTo: 'stress',
value: {
custom: {
@ -24,7 +24,7 @@ export const armorFeatures = {
}
}
}
]
}
}
}
]
@ -732,8 +732,8 @@ export const weaponFeatures = {
type: 'hostile'
},
damage: {
parts: [
{
parts: {
stress: {
applyTo: 'stress',
value: {
custom: {
@ -742,7 +742,7 @@ export const weaponFeatures = {
}
}
}
]
}
}
}
],
@ -914,8 +914,8 @@ export const weaponFeatures = {
type: 'self'
},
damage: {
parts: [
{
parts: {
hitPoints: {
applyTo: 'hitPoints',
value: {
custom: {
@ -924,7 +924,7 @@ export const weaponFeatures = {
}
}
}
]
}
}
}
]

View file

@ -26,23 +26,23 @@ export default class DHAttackAction extends DHDamageAction {
return {
value: {
multiplier: 'prof',
dice: this.item?.system?.attack.damage.parts[0].value.dice,
bonus: this.item?.system?.attack.damage.parts[0].value.bonus ?? 0
dice: this.item?.system?.attack.damage.parts.hitPoints.value.dice,
bonus: this.item?.system?.attack.damage.parts.hitPoints.value.bonus ?? 0
},
type: this.item?.system?.attack.damage.parts[0].type,
type: this.item?.system?.attack.damage.parts.hitPoints.type,
base: true
};
}
get damageFormula() {
const hitPointsPart = this.damage.parts.find(x => x.applyTo === CONFIG.DH.GENERAL.healingTypes.hitPoints.id);
const hitPointsPart = this.damage.parts.hitPoints;
if (!hitPointsPart) return '0';
return hitPointsPart.value.getFormula();
}
get altDamageFormula() {
const hitPointsPart = this.damage.parts.find(x => x.applyTo === CONFIG.DH.GENERAL.healingTypes.hitPoints.id);
const hitPointsPart = this.damage.parts.hitPoints;
if (!hitPointsPart) return '0';
return hitPointsPart.valueAlt.getFormula();

View file

@ -352,11 +352,11 @@ export default class DHBaseAction extends ActionMixin(foundry.abstract.DataModel
}
get hasDamage() {
return this.damage?.parts?.length && this.type !== 'healing';
return !foundry.utils.isEmpty(this.damage.parts) && this.type !== 'healing';
}
get hasHealing() {
return this.damage?.parts?.length && this.type === 'healing';
return !foundry.utils.isEmpty(this.damage.parts) && this.type === 'healing';
}
get hasSave() {
@ -376,6 +376,15 @@ export default class DHBaseAction extends ActionMixin(foundry.abstract.DataModel
return tags;
}
static migrateData(source) {
if (source.damage?.parts && Array.isArray(source.damage.parts)) {
source.damage.parts = source.damage.parts.reduce((acc, part) => {
acc[part.applyTo] = part;
return acc;
}, {});
}
}
}
export class ResourceUpdateMap extends Map {

View file

@ -89,14 +89,14 @@ export default class DhpAdversary extends DhCreature {
type: 'attack'
},
damage: {
parts: [
{
parts: {
hitPoints: {
type: ['physical'],
value: {
multiplier: 'flat'
}
}
]
}
}
}
}),
@ -268,12 +268,12 @@ export default class DhpAdversary extends DhCreature {
}
// Update damage in item actions
// Parse damage, and convert all formula matches in the descriptions to the new damage
for (const action of Object.values(item.system.actions)) {
if (!action.damage) continue;
// Parse damage, and convert all formula matches in the descriptions to the new damage
try {
const result = this.#adjustActionDamage(action, { ...damageMeta, type: 'action' });
if (!result) continue;
for (const { previousFormula, formula } of Object.values(result)) {
const oldFormulaRegexp = new RegExp(
previousFormula.replace(' ', '').replace('+', '(?:\\s)?\\+(?:\\s)?')
@ -375,16 +375,14 @@ export default class DhpAdversary extends DhCreature {
/**
* Updates damage to reflect a specific value.
* @throws if damage structure is invalid for conversion
* @returns the converted formula and value as a simplified term
* @returns the converted formula and value as a simplified term, or null if it doesn't deal HP damage
*/
#adjustActionDamage(action, damageMeta) {
// The current algorithm only returns a value if there is a single damage part
const hpDamageParts = action.damage.parts.filter(d => d.applyTo === 'hitPoints');
if (hpDamageParts.length !== 1) throw new Error('incorrect number of hp parts');
if (!action.damage?.parts.hitPoints) return null;
const result = {};
for (const property of ['value', 'valueAlt']) {
const data = hpDamageParts[0][property];
const data = action.damage.parts.hitPoints[property];
const previousFormula = data.custom.enabled
? data.custom.formula
: [data.flatMultiplier ? `${data.flatMultiplier}${data.dice}` : 0, data.bonus ?? 0]

View file

@ -118,8 +118,8 @@ export default class DhCharacter extends DhCreature {
trait: 'strength'
},
damage: {
parts: [
{
parts: {
hitPoints: {
type: ['physical'],
value: {
custom: {
@ -128,7 +128,7 @@ export default class DhCharacter extends DhCreature {
}
}
}
]
}
}
}
}),
@ -704,7 +704,7 @@ export default class DhCharacter extends DhCreature {
isReversed: true
};
this.attack.damage.parts[0].value.custom.formula = `@prof${this.basicAttackDamageDice}${this.rules.attack.damage.bonus ? ` + ${this.rules.attack.damage.bonus}` : ''}`;
this.attack.damage.parts.hitPoints.value.custom.formula = `@prof${this.basicAttackDamageDice}${this.rules.attack.damage.bonus ? ` + ${this.rules.attack.damage.bonus}` : ''}`;
}
getRollData() {

View file

@ -85,15 +85,15 @@ export default class DhCompanion extends DhCreature {
bonus: 0
},
damage: {
parts: [
{
parts: {
hitPoints: {
type: ['physical'],
value: {
dice: 'd6',
multiplier: 'prof'
}
}
]
}
}
}
}),
@ -138,7 +138,9 @@ export default class DhCompanion extends DhCreature {
break;
case 'vicious':
if (selection.data[0] === 'damage') {
this.attack.damage.parts[0].value.dice = adjustDice(this.attack.damage.parts[0].value.dice);
this.attack.damage.parts.hitPoints.value.dice = adjustDice(
this.attack.damage.parts.hitPoints.value.dice
);
} else {
this.attack.range = adjustRange(this.attack.range).id;
}

View file

@ -1,4 +1,5 @@
export { ActionCollection } from './actionField.mjs';
export { default as IterableTypedObjectField } from './iterableTypedObjectField.mjs';
export { default as FormulaField } from './formulaField.mjs';
export { default as ForeignDocumentUUIDField } from './foreignDocumentUUIDField.mjs';
export { default as ForeignDocumentUUIDArrayField } from './foreignDocumentUUIDArrayField.mjs';

View file

@ -1,5 +1,6 @@
import FormulaField from '../formulaField.mjs';
import { setsEqual } from '../../../helpers/utils.mjs';
import IterableTypedObjectField from '../iterableTypedObjectField.mjs';
const fields = foundry.data.fields;
@ -12,7 +13,7 @@ export default class DamageField extends fields.SchemaField {
/** @inheritDoc */
constructor(options, context = {}) {
const damageFields = {
parts: new fields.ArrayField(new fields.EmbeddedDataField(DHDamageData)),
parts: new IterableTypedObjectField(DHDamageData),
includeBase: new fields.BooleanField({
initial: false,
label: 'DAGGERHEART.ACTIONS.Settings.includeBase.label'

View file

@ -0,0 +1,32 @@
export default class IterableTypedObjectField extends foundry.data.fields.TypedObjectField {
constructor(model, options = { collectionClass: foundry.utils.Collection }, context = {}) {
super(new foundry.data.fields.EmbeddedDataField(model), options, context);
this.#elementClass = model;
}
#elementClass;
/** Initializes an object with an iterator. This modifies the prototype instead of */
initialize(values) {
const object = Object.create(IterableObjectPrototype);
for (const [key, value] of Object.entries(values)) {
object[key] = new this.#elementClass(value);
}
return object;
}
}
/**
* The prototype of an iterable object.
* This allows the functionality of a class but also allows foundry.utils.getType() to return "Object" instead of "Unknown".
*/
const IterableObjectPrototype = {
[Symbol.iterator]: function*() {
for (const value of Object.values(this)) {
yield value;
}
},
map: function (func) {
return Array.from(this, func);
}
};

View file

@ -63,15 +63,15 @@ export default class DHWeapon extends AttachableItem {
type: 'attack'
},
damage: {
parts: [
{
parts: {
hitPoints: {
type: ['physical'],
value: {
multiplier: 'prof',
dice: 'd8'
}
}
]
}
}
}
}),

View file

@ -49,9 +49,7 @@ export default class RegisterHandlebarsHelpers {
}
static damageSymbols(damageParts) {
const symbols = [...new Set(damageParts.reduce((a, c) => a.concat([...c.type]), []))].map(
p => CONFIG.DH.GENERAL.damageTypes[p].icon
);
const symbols = [...new Set(damageParts.map(x => x.type))].map(p => CONFIG.DH.GENERAL.damageTypes[p].icon);
return new Handlebars.SafeString(Array.from(symbols).map(symbol => `<i class="fa-solid ${symbol}"></i>`));
}

View file

@ -722,3 +722,16 @@ export async function RefreshFeatures(
return refreshedActors;
}
export function getUnusedDamageTypes(parts) {
const usedKeys = Object.keys(parts);
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;
}, []);
}