From 221b84726d30ba9a6e25f24bc2f422e283a68ffb Mon Sep 17 00:00:00 2001
From: WBHarry <williambjrklund@gmail.com>
Date: Wed, 15 Jul 2026 14:58:02 +0200
Subject: [PATCH] Added aseDie.rerollResult method to specifically reroll a
 specific result die in a grouping

---
 daggerheart.mjs                             |  3 +++
 module/data/chat-message/chatDamageData.mjs |  3 +--
 module/dice/die/_module.mjs                 |  1 +
 module/dice/die/advantageDie.mjs            |  4 +++-
 module/dice/die/baseDie.mjs                 | 12 ++++++++++++
 module/dice/die/disadvantageDie.mjs         |  4 +++-
 module/dice/die/dualityDie.mjs              |  3 ++-
 7 files changed, 25 insertions(+), 5 deletions(-)
 create mode 100644 module/dice/die/baseDie.mjs

diff --git a/daggerheart.mjs b/daggerheart.mjs
index 63127aa4..31c9f63d 100644
--- a/daggerheart.mjs
+++ b/daggerheart.mjs
@@ -6,6 +6,7 @@ import * as documents from './module/documents/_module.mjs';
 import { macros } from './module/_module.mjs';
 import * as collections from './module/documents/collections/_module.mjs';
 import * as dice from './module/dice/_module.mjs';
+import * as die from './module/dice/die/_module.mjs';
 import * as fields from './module/data/fields/_module.mjs';
 import RegisterHandlebarsHelpers from './module/helpers/handlebarsHelper.mjs';
 import { enricherConfig, enricherRenderSetup } from './module/enrichers/_module.mjs';
@@ -38,6 +39,8 @@ CONFIG.RegionBehavior.dataModels = {
 };
 
 Object.assign(CONFIG.Dice.termTypes, dice.diceTypes);
+CONFIG.Dice.terms.d = die.BaseDie;
+CONFIG.Dice.types = [die.BaseDie, CONFIG.Dice.terms.f];
 
 CONFIG.Actor.documentClass = documents.DhpActor;
 CONFIG.Actor.dataModels = models.actors.config;
diff --git a/module/data/chat-message/chatDamageData.mjs b/module/data/chat-message/chatDamageData.mjs
index 3ba771b4..3c4c6b95 100644
--- a/module/data/chat-message/chatDamageData.mjs
+++ b/module/data/chat-message/chatDamageData.mjs
@@ -35,8 +35,7 @@ export class ChatDamageData extends foundry.abstract.DataModel {
     async rerollDamageDie(damageType, dice, resultIndex) {
         const reroll = this.types[damageType].roll;
         const rerollDice = reroll.dice[dice];
-        const diceResult = rerollDice.results[resultIndex];
-        await rerollDice.reroll(`/r1=${diceResult.result}`);
+        await rerollDice.rerollResult(resultIndex);
         await reroll._evaluate();
     
         const rerolledResult = rerollDice.results[rerollDice.results.length - 1];
diff --git a/module/dice/die/_module.mjs b/module/dice/die/_module.mjs
index 19ca951a..b84cef86 100644
--- a/module/dice/die/_module.mjs
+++ b/module/dice/die/_module.mjs
@@ -3,6 +3,7 @@ import HopeDie from './hopeDie.mjs';
 import FearDie from './fearDie.mjs';
 import AdvantageDie from './advantageDie.mjs';
 import DisadvantageDie from './disadvantageDie.mjs';
+export { default as BaseDie } from './baseDie.mjs';
 
 export const diceTypes = {
     DualityDie,
diff --git a/module/dice/die/advantageDie.mjs b/module/dice/die/advantageDie.mjs
index 9c2f0b03..66b6f124 100644
--- a/module/dice/die/advantageDie.mjs
+++ b/module/dice/die/advantageDie.mjs
@@ -1,4 +1,6 @@
-export default class AdvantageDie extends foundry.dice.terms.Die {
+import BaseDie from './baseDie.mjs';
+
+export default class AdvantageDie extends BaseDie {
     constructor(options) {
         super(options);
 
diff --git a/module/dice/die/baseDie.mjs b/module/dice/die/baseDie.mjs
new file mode 100644
index 00000000..cc88f753
--- /dev/null
+++ b/module/dice/die/baseDie.mjs
@@ -0,0 +1,12 @@
+export default class BaseDie extends foundry.dice.terms.Die {
+    async rerollResult(resultIndex) {
+        const result = this.results[resultIndex];
+        result.rerolled = true;
+        result.active = false;
+        await this.roll({ reroll: true });
+
+        const rerolledResult = this.results[this.results.length - 1];
+        this.results.splice(this.results.length - 1, 1);
+        this.results.splice(resultIndex, 0, rerolledResult);
+    }
+}
\ No newline at end of file
diff --git a/module/dice/die/disadvantageDie.mjs b/module/dice/die/disadvantageDie.mjs
index f56ebe96..e79845f3 100644
--- a/module/dice/die/disadvantageDie.mjs
+++ b/module/dice/die/disadvantageDie.mjs
@@ -1,4 +1,6 @@
-export default class DisadvantageDie extends foundry.dice.terms.Die {
+import BaseDie from './baseDie.mjs';
+
+export default class DisadvantageDie extends BaseDie {
     constructor(options) {
         super(options);
 
diff --git a/module/dice/die/dualityDie.mjs b/module/dice/die/dualityDie.mjs
index cc7ee75e..2f4dae77 100644
--- a/module/dice/die/dualityDie.mjs
+++ b/module/dice/die/dualityDie.mjs
@@ -1,6 +1,7 @@
+import BaseDie from './baseDie.mjs';
 import { updateResourcesForDualityReroll } from '../helpers.mjs';
 
-export default class DualityDie extends foundry.dice.terms.Die {
+export default class DualityDie extends BaseDie {
     constructor(options) {
         super(options);
 
