From 7a50d7795270d19251bff416126a8ee44a11f98f Mon Sep 17 00:00:00 2001 From: Nick Salyzyn Date: Sat, 13 Dec 2025 07:18:21 -0700 Subject: [PATCH] Pulling out hope method and always updating the resources. (#1417) Making sure stress decreases on critical. --- .../applications/sheets/actors/character.mjs | 2 - module/applications/ui/chatLog.mjs | 1 - module/dice/dhRoll.mjs | 80 +++++++++++-------- module/dice/dualityRoll.mjs | 3 +- module/documents/actor.mjs | 4 + 5 files changed, 51 insertions(+), 39 deletions(-) diff --git a/module/applications/sheets/actors/character.mjs b/module/applications/sheets/actors/character.mjs index 016cff13..953a0cf6 100644 --- a/module/applications/sheets/actors/character.mjs +++ b/module/applications/sheets/actors/character.mjs @@ -685,8 +685,6 @@ export default class CharacterSheet extends DHBaseActorSheet { ability: abilityLabel }) }); - - if (result) game.system.api.fields.ActionFields.CostField.execute.call(this, result); } //TODO: redo toggleEquipItem method diff --git a/module/applications/ui/chatLog.mjs b/module/applications/ui/chatLog.mjs index 871ff173..47dfe500 100644 --- a/module/applications/ui/chatLog.mjs +++ b/module/applications/ui/chatLog.mjs @@ -245,7 +245,6 @@ export default class DhpChatLog extends foundry.applications.sidebar.tabs.ChatLo }); if (!result) return; - await game.system.api.fields.ActionFields.CostField.execute.call({ actor }, result); const newMessageData = foundry.utils.deepClone(message.system); foundry.utils.setProperty(newMessageData, `${path}.result`, result.roll); diff --git a/module/dice/dhRoll.mjs b/module/dice/dhRoll.mjs index e6856dab..abb75fd5 100644 --- a/module/dice/dhRoll.mjs +++ b/module/dice/dhRoll.mjs @@ -237,6 +237,51 @@ export default class DHRoll extends Roll { } } +async function automateHopeFear(config) { + const automationSettings = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.Automation); + const hopeFearAutomation = automationSettings.hopeFear; + if (!config.source?.actor || + (game.user.isGM ? !hopeFearAutomation.gm : !hopeFearAutomation.players) || + config.actionType === 'reaction' || + config.tagTeamSelected || + config.skips?.resources) + return; + const actor = await fromUuid(config.source.actor); + let updates = []; + if (!actor) return; + + if (config.rerolledRoll) { + if (config.roll.result.duality != config.rerolledRoll.result.duality) { + const hope = (config.roll.isCritical || config.roll.result.duality === 1 ? 1 : 0) + - (config.rerolledRoll.isCritical || config.rerolledRoll.result.duality === 1 ? 1 : 0); + const stress = (config.roll.isCritical ? 1 : 0) - (config.rerolledRoll.isCritical ? 1 : 0); + const fear = (config.roll.result.duality === -1 ? 1 : 0) + - (config.rerolledRoll.result.duality === -1 ? 1 : 0) + + if (hope !== 0) + updates.push({ key: 'hope', value: hope, total: -1 * hope, enabled: true }); + if (stress !== 0) + updates.push({ key: 'stress', value: -1 * stress, total: stress, enabled: true }); + if (fear !== 0) + updates.push({ key: 'fear', value: fear, total: -1 * fear, enabled: true }); + } + } else { + if (config.roll.isCritical || config.roll.result.duality === 1) + updates.push({ key: 'hope', value: 1, total: -1, enabled: true }); + if (config.roll.isCritical) + updates.push({ key: 'stress', value: -1, total: 1, enabled: true }); + if (config.roll.result.duality === -1) + updates.push({ key: 'fear', value: 1, total: -1, enabled: true }); + } + + if (updates.length) { + const target = actor.system.partner ?? actor; + if (!['dead', 'defeated', 'unconscious'].some(x => actor.statuses.has(x))) { + await target.modifyResource(updates); + } + } +} + export const registerRollDiceHooks = () => { Hooks.on(`${CONFIG.DH.id}.postRollDuality`, async (config, message) => { const automationSettings = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.Automation); @@ -254,38 +299,7 @@ export const registerRollDiceHooks = () => { } } - const hopeFearAutomation = automationSettings.hopeFear; - if ( - !config.source?.actor || - (game.user.isGM ? !hopeFearAutomation.gm : !hopeFearAutomation.players) || - config.actionType === 'reaction' || - config.tagTeamSelected || - config.skips?.resources - ) - return; - const actor = await fromUuid(config.source.actor); - let updates = []; - if (!actor) return; - if (config.roll.isCritical || config.roll.result.duality === 1) - updates.push({ key: 'hope', value: 1, total: -1, enabled: true }); - if (config.roll.isCritical) updates.push({ key: 'stress', value: 1, total: -1, enabled: true }); - if (config.roll.result.duality === -1) updates.push({ key: 'fear', value: 1, total: -1, enabled: true }); - - if (config.rerolledRoll) { - if (config.rerolledRoll.isCritical || config.rerolledRoll.result.duality === 1) - updates.push({ key: 'hope', value: -1, total: 1, enabled: true }); - if (config.rerolledRoll.isCritical) updates.push({ key: 'stress', value: -1, total: 1, enabled: true }); - if (config.rerolledRoll.result.duality === -1) - updates.push({ key: 'fear', value: -1, total: 1, enabled: true }); - } - - if (updates.length) { - const target = actor.system.partner ?? actor; - if (!['dead', 'defeated', 'unconscious'].some(x => actor.statuses.has(x))) { - if (config.rerolledRoll) target.modifyResource(updates); - else config.costs = [...(config.costs ?? []), ...updates]; - } - } + await automateHopeFear(config); if (!config.roll.hasOwnProperty('success') && !config.targets?.length) return; @@ -296,7 +310,5 @@ export const registerRollDiceHooks = () => { const currentCombatant = game.combat.combatants.get(game.combat.current?.combatantId); if (currentCombatant?.actorId == actor.id) ui.combat.setCombatantSpotlight(currentCombatant.id); } - - return; }); }; diff --git a/module/dice/dualityRoll.mjs b/module/dice/dualityRoll.mjs index 813c913b..59cb6e02 100644 --- a/module/dice/dualityRoll.mjs +++ b/module/dice/dualityRoll.mjs @@ -262,8 +262,7 @@ export default class DualityRoll extends D20Roll { targets: message.system.targets, tagTeamSelected: Object.values(tagTeamSettings.members).some(x => x.messageId === message._id), roll: newRoll, - rerolledRoll: - newRoll.result.duality !== message.system.roll.result.duality ? message.system.roll : undefined + rerolledRoll: message.system.roll }); return { newRoll, parsedRoll }; } diff --git a/module/documents/actor.mjs b/module/documents/actor.mjs index 8999fdd8..5ffaf744 100644 --- a/module/documents/actor.mjs +++ b/module/documents/actor.mjs @@ -679,6 +679,10 @@ export default class DhpActor extends Actor { return updates; } + /** + * Resources are modified asynchronously, so be careful not to update the same resource in + * quick succession. + */ async modifyResource(resources) { if (!resources?.length) return;