diff --git a/module/config/resourceConfig.mjs b/module/config/resourceConfig.mjs index 56ef6cd5..d33d7ab3 100644 --- a/module/config/resourceConfig.mjs +++ b/module/config/resourceConfig.mjs @@ -57,15 +57,9 @@ const companionBaseResources = Object.freeze({ stress: { id: 'stress', initial: 0, - max: 0, + max: 3, reverse: true, label: 'DAGGERHEART.GENERAL.stress' - }, - hope: { - id: 'hope', - initial: 0, - reverse: false, - label: 'DAGGERHEART.GENERAL.hope' } }); diff --git a/module/data/actor/adversary.mjs b/module/data/actor/adversary.mjs index d30e090a..d69e17ad 100644 --- a/module/data/actor/adversary.mjs +++ b/module/data/actor/adversary.mjs @@ -189,6 +189,9 @@ export default class DhpAdversary extends DhCreature { prepareDerivedData() { super.prepareDerivedData(); this.attack.roll.isStandardAttack = true; + + // Clamp resources (must be done last to ensure all updates occur) + this.resources.clamp(); } _getTags() { diff --git a/module/data/actor/character.mjs b/module/data/actor/character.mjs index 6f60356e..d42f2f64 100644 --- a/module/data/actor/character.mjs +++ b/module/data/actor/character.mjs @@ -778,6 +778,8 @@ export default class DhCharacter extends DhCreature { prepareDerivedData() { super.prepareDerivedData(); + + this.resources.hope.max -= this.scars; if (this.companion) { for (let levelKey in this.companion.system.levelData.levelups) { const level = this.companion.system.levelData.levelups[levelKey]; @@ -791,7 +793,6 @@ export default class DhCharacter extends DhCreature { } } - this.resources.hope.max -= this.scars; this.attack.roll.trait = this.rules.attack.roll.trait ?? this.attack.roll.trait; this.resources.armor = { @@ -801,6 +802,9 @@ export default class DhCharacter extends DhCreature { }; this.attack.damage.parts.hitPoints.value.custom.formula = `@prof${this.basicAttackDamageDice}${this.rules.attack.damage.bonus ? ` + ${this.rules.attack.damage.bonus}` : ''}`; + + // Clamp resources (must be done last to ensure all updates occur) + this.resources.clamp(); } getRollData() { diff --git a/module/data/actor/companion.mjs b/module/data/actor/companion.mjs index 538031fb..300bd698 100644 --- a/module/data/actor/companion.mjs +++ b/module/data/actor/companion.mjs @@ -144,9 +144,6 @@ export default class DhCompanion extends DhCreature { const level = this.levelData.levelups[levelKey]; for (let selection of level.selections) { switch (selection.type) { - case 'hope': - this.resources.hope += selection.value; - break; case 'vicious': if (selection.data[0] === 'damage') { this.attack.damage.parts.hitPoints.value.dice = adjustDice( @@ -183,6 +180,9 @@ export default class DhCompanion extends DhCreature { return acc; }, this.partner.system.companionData.levelupChoices); } + + // Clamp resources (must be done last to ensure all updates occur) + this.resources.clamp(); } async _preUpdate(changes, options, userId) { diff --git a/module/data/actor/creature.mjs b/module/data/actor/creature.mjs index 88646301..601068ad 100644 --- a/module/data/actor/creature.mjs +++ b/module/data/actor/creature.mjs @@ -60,14 +60,4 @@ export default class DhCreature extends BaseDataActor { } } } - - prepareDerivedData() { - const minLimitResource = resource => { - if (resource) resource.value = Math.min(resource.value, resource.max); - }; - - minLimitResource(this.resources.stress); - minLimitResource(this.resources.hitPoints); - minLimitResource(this.resources.hope); - } } diff --git a/module/data/fields/actorField.mjs b/module/data/fields/actorField.mjs index d6b58675..a5f6f379 100644 --- a/module/data/fields/actorField.mjs +++ b/module/data/fields/actorField.mjs @@ -80,6 +80,18 @@ class ResourcesField extends fields.TypedObjectField { value.isReversed = resources[key].reverse; value.max = typeof resource.max === 'number' ? (value.max ?? resource.max) : null; } + Object.defineProperty(data, 'clamp', { + value: function () { + for (const key of Object.keys(this)) { + const resource = this[key]; + if (typeof resource?.max === 'number') { + resource.value = Math.clamp(resource.value, 0, resource.max); + } + } + }, + enumerable: false + }); + return data; }