From b02ff3b40aa250c4eb679f8d4f6d44f34248a59e Mon Sep 17 00:00:00 2001 From: phBalance Date: Mon, 14 Oct 2024 09:59:40 -0600 Subject: [PATCH] refactor(skill resources): skills can now use STUN and END reserves --- CHANGELOG.md | 1 + module/item/item-attack.mjs | 10 ++-- module/item/skill.mjs | 70 +++++++--------------- templates/chat/skill-success-roll-card.hbs | 6 ++ 4 files changed, 33 insertions(+), 54 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c46ae003..8435e0fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Version 4.0.2 (So far...) [Hero System 6e (Unofficial) v2](https://github.com/dmdorman/hero6e-foundryvtt) - Strength rolls now use endurance. [#1253](https://github.com/dmdorman/hero6e-foundryvtt/issues/1253) +- Skills can now use STUN for END and END reserves. - Encumbrance related improvements. - Fix for 5e DAMAGE RESISTANCE and PD/ED purchased as a power, where the PD/ED was counted twice. [#1297](https://github.com/dmdorman/hero6e-foundryvtt/issues/1297) - Improved KNOWLEDGE_SKILL descriptions. [#1278](https://github.com/dmdorman/hero6e-foundryvtt/issues/1278) diff --git a/module/item/item-attack.mjs b/module/item/item-attack.mjs index f23645ac..4922df43 100644 --- a/module/item/item-attack.mjs +++ b/module/item/item-attack.mjs @@ -527,7 +527,7 @@ export async function AttackToHit(item, options) { // (so we can be sneaky and not tell the target's DCV out loud). heroRoller.addDice(-3); - // Consume resources + // Make sure there are enough resources and consume them const { error: resourceError, warning: resourceWarning, @@ -3051,9 +3051,11 @@ async function _calcKnockback(body, item, options, knockbackMultiplier) { /** * Multistage helper function useful for most item activations. * 1. Make sure the actor associated with the item has enough resources to activate the item. - * 2. Display `ui.notification.error` prompts if actor does not have enough resources and return error. - * 3. If the user can use STUN in place of END prompt them for permission to do so - * 4. If there are enough resources, spend the resources and return full information. + * 2. Return an error if actor does not have enough resources. + * a. If the item doesn't have enough charges it is an error. + * b. If the item uses an endurance battery and doesn't have enough END it is an error. + * c. If the user can use STUN in place of END prompt them for permission to do so. Return a warning if they don't want to use STUN. + * 3. If there are enough resources, spend the resources and return full information. * * @param {HeroSystem6eItem} item * @param {Object} options diff --git a/module/item/skill.mjs b/module/item/skill.mjs index f2cab424..ae6a117d 100644 --- a/module/item/skill.mjs +++ b/module/item/skill.mjs @@ -1,5 +1,6 @@ import { HEROSYS } from "../herosystem6e.mjs"; import { HeroRoller } from "../utility/dice.mjs"; +import { userInteractiveVerifyOptionallyPromptThenSpendResources } from "./item-attack.mjs"; async function _renderSkillForm(item, actor, stateData) { const token = actor.token; @@ -84,41 +85,22 @@ async function skillRoll(item, actor, html) { const speaker = ChatMessage.getSpeaker({ actor: actor, token }); speaker.alias = actor.name; - // Charges? - const charges = item.findModsByXmlid("CHARGES"); - if (charges) { - if (!item.system.charges?.value || parseInt(item.system.charges?.value) <= 0) { - const chatData = { - user: game.user._id, - content: `${item.name} has no charges remaining.`, - speaker: speaker, - }; - - await ChatMessage.create(chatData); - return; - } - } - - // Cost END? - const endUse = parseInt(item.system.end); - const costEnd = item.findModsByXmlid("COSTSEND"); - if (costEnd && endUse && item.actor) { - const newEnd = parseInt(item.actor.system.characteristics.end.value) - endUse; + // Make sure there are enough resources and consume them + const { + error: resourceError, + warning: resourceWarning, + resourcesRequired, + resourcesUsedDescription, + } = await userInteractiveVerifyOptionallyPromptThenSpendResources(item, {}); + if (resourceError || resourceWarning) { + const chatData = { + user: game.user._id, + content: resourceError || resourceWarning, + speaker: speaker, + }; - if (newEnd >= 0) { - await item.actor.update({ - "system.characteristics.end.value": newEnd, - }); - } else { - const chatData = { - user: game.user._id, - content: `Insufficient END to use ${item.name}.`, - speaker: speaker, - }; - - await ChatMessage.create(chatData); - return; - } + await ChatMessage.create(chatData); + return; } const formElement = html[0].querySelector("form"); @@ -169,7 +151,7 @@ async function skillRoll(item, actor, html) { } await skillRoller.makeSuccessRoll(true, successValue).roll(); - let succeeded = skillRoller.getSuccess(); + const succeeded = skillRoller.getSuccess(); const autoSuccess = skillRoller.getAutoSuccess(); const total = skillRoller.getSuccessTotal(); const margin = successValue - total; @@ -177,21 +159,7 @@ async function skillRoll(item, actor, html) { const flavor = `${item.name.toUpperCase()} (${successValue}-) roll ${succeeded ? "succeeded" : "failed"} by ${ autoSuccess === undefined ? `${Math.abs(margin)}` : `rolling ${total}` }`; - let rollHtml = await skillRoller.render(flavor); - - // Charges - if (charges) { - await item.update({ - "system.charges.value": parseInt(item.system.charges.value) - 1, - }); - await item._postUpload(); - rollHtml += `

Spent 1 charge.

`; - } - - // END - if (costEnd && endUse && item.actor) { - rollHtml += `

Spent ${endUse} END.

`; - } + const rollHtml = await skillRoller.render(flavor); // render card const cardData = { @@ -200,6 +168,8 @@ async function skillRoll(item, actor, html) { }), rolls: skillRoller.rawRolls(), renderedRoll: rollHtml, + resourcesUsedDescription: + resourcesRequired.charges > 0 || resourcesRequired.end > 0 ? resourcesUsedDescription : undefined, user: game.user._id, speaker: speaker, }; diff --git a/templates/chat/skill-success-roll-card.hbs b/templates/chat/skill-success-roll-card.hbs index 3b1455f1..207d4727 100644 --- a/templates/chat/skill-success-roll-card.hbs +++ b/templates/chat/skill-success-roll-card.hbs @@ -8,6 +8,12 @@ {{/each}} + {{#if resourcesUsedDescription}} +
+
{{{ resourcesUsedDescription }}}
+
+ {{/if}} +
{{{ renderedRoll }}}