Skip to content

Commit

Permalink
Fixed group attacks with damage dice
Browse files Browse the repository at this point in the history
  • Loading branch information
Haxxer committed Oct 9, 2023
1 parent 74b9f1b commit 355e270
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 35 deletions.
2 changes: 1 addition & 1 deletion scripts/initiative.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function refreshInitiativeGroupGraphics(t) {
background.width = diameter;
background.height = diameter;
background.beginFill(0xFFFFFF)
.drawCircle((diameter / 2), (diameter / 2), Math.max(1, (diameter-2)/2))
.drawCircle((diameter / 2), (diameter / 2), Math.max(1, (diameter - 2) / 2))
.endFill();

sprite._background = background;
Expand Down
2 changes: 1 addition & 1 deletion scripts/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function isValidOverkillItem(item) {

}

export function getActiveGM(){
export function getActiveGM() {
return game.users
.filter(u => u.active && u.isGM)
.sort((a, b) => a.isGM && !b.isGM ? -1 : 1)?.[0];
Expand Down
27 changes: 14 additions & 13 deletions scripts/plugins/midiqol.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as lib from "../lib.js";
import CONSTANTS from "../constants.js";
import * as api from "../api.js";
import { isValidOverkillItem } from "../lib.js";

export default {

Expand Down Expand Up @@ -36,7 +35,6 @@ export default {

const attackHookId = Hooks.on("dnd5e.preRollAttack", (rolledItem, rollConfig) => {
if (rolledItem !== workflow.item) return true;
rollConfig.fastForward = true;
rollConfig.parts.push(numMinionsAttacked);
Hooks.off("dnd5e.preRollAttack", attackHookId);
return true;
Expand All @@ -50,19 +48,22 @@ export default {
if (!minionAttacks?.[workflow.id]) return true;
if (workflow.item.system?.damage?.parts?.length < 1) return true;

const numMinionsAttacked = minionAttacks[workflow.id];
delete minionAttacks[workflow.id];
const firstDamage = workflow.item.system.damage.parts[0][0];
const newFormula = isNaN(Number(firstDamage))
? firstDamage + " * " + numMinionsAttacked
: Number(firstDamage) * numMinionsAttacked;
const newDamageParts = [];

for (let index = 0; index < workflow.item.system.damage.parts.length; index++) {
const firstDamage = workflow.item.system.damage.parts[index][0];
const newFormula = isNaN(Number(firstDamage))
? "(" + firstDamage.toString() + " * " + minionAttacks[workflow.id].toString() + ")"
: Number(firstDamage) * minionAttacks[workflow.id];
const damageType = workflow.item.system.damage.parts[index][1];
newDamageParts.push(`${newFormula}${damageType ? `[${damageType}]` : ""}`);
}

const damageType = workflow.item.system.damage.parts[0][1];
delete minionAttacks[workflow.id];

const damageHookId = Hooks.on("dnd5e.preRollDamage", (rolledItem, rollConfig) => {
if (rolledItem !== workflow.item) return true;
rollConfig.fastForward = true;
rollConfig.parts[0] = [`${newFormula}${damageType ? `[${damageType}]` : ""}`];
rollConfig.parts = newDamageParts;
Hooks.off("dnd5e.preRollDamage", damageHookId);
return true;
});
Expand All @@ -81,7 +82,7 @@ export default {
Hooks.on("midi-qol.preDamageRollComplete", async (workflow) => {

const validAttack = lib.isValidOverkillItem(workflow.item);
if(!validAttack) return true;
if (!validAttack) return true;
if (!workflow.hitTargets.size) return true;

const hitTarget = Array.from(workflow.hitTargets)[0]
Expand Down Expand Up @@ -155,7 +156,7 @@ export default {
workflow.item
);

if(lib.getSetting(CONSTANTS.SETTING_KEYS.ENABLE_OVERKILL_MESSAGE)) {
if (lib.getSetting(CONSTANTS.SETTING_KEYS.ENABLE_OVERKILL_MESSAGE)) {
ChatMessage.create({
content: `<h2>${game.i18n.localize("MINIONMANAGER.Dialogs.OverkillDamage.Title")}</h2><p>${label1}</p>`
});
Expand Down
48 changes: 28 additions & 20 deletions scripts/plugins/vanilla.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as lib from "../lib.js";
import CONSTANTS from "../constants.js";
import * as api from "../api.js";
import { getActiveGM } from "../lib.js";

export default {

Expand Down Expand Up @@ -64,36 +63,45 @@ export default {

if (minionAttacks?.[item.parent.uuid]) {

const numMinionsAttacked = minionAttacks[item.parent.uuid].numMinionsAttacked;
delete minionAttacks[item.parent.uuid];
const firstDamage = item.system.damage.parts[0][0];
const newFormula = isNaN(Number(firstDamage))
? firstDamage + " * " + numMinionsAttacked
: Number(firstDamage) * numMinionsAttacked;
for (let index = 0; index < item.system.damage.parts.length; index++) {

const firstDamage = item.system.damage.parts[index][0];
const newFormula = isNaN(Number(firstDamage))
? "(" + firstDamage.toString() + " * " + minionOnlyDamages[item.parent.uuid].numMinionsAttacked.toString() + ")"
: Number(firstDamage) * minionOnlyDamages[item.parent.uuid].numMinionsAttacked;

const damageType = item.system.damage.parts[0][1];
const damageType = item.system.damage.parts[index][1];

if (lib.getSetting(CONSTANTS.SETTING_KEYS.ENABLE_GROUP_ATTACK_BONUS)) {
rollConfig.parts[index] = [`${newFormula}${damageType ? `[${damageType}]` : ""}`];
}
}

rollConfig.parts[0] = [`${newFormula}${damageType ? `[${damageType}]` : ""}`];
delete minionAttacks[item.parent.uuid];

return true;

} else {

// If we've already prompted the user, and the attack hasn't gone through, then we continue the original attack
if (minionOnlyDamages[item.parent.uuid] && !minionOnlyDamages[item.parent.uuid].attacked) {
const firstDamage = item.system.damage.parts[0][0];
const newFormula = isNaN(Number(firstDamage))
? firstDamage + " * " + minionOnlyDamages[item.parent.uuid].numMinionsAttacked
: Number(firstDamage) * minionOnlyDamages[item.parent.uuid].numMinionsAttacked;

delete minionOnlyDamages[item.parent.uuid];
for (let index = 0; index < item.system.damage.parts.length; index++) {

const damageType = item.system.damage.parts[0][1];
const firstDamage = item.system.damage.parts[index][0];
const newFormula = isNaN(Number(firstDamage))
? "(" + firstDamage.toString() + " * " + minionOnlyDamages[item.parent.uuid].numMinionsAttacked.toString() + ")"
: Number(firstDamage) * minionOnlyDamages[item.parent.uuid].numMinionsAttacked;

if (lib.getSetting(CONSTANTS.SETTING_KEYS.ENABLE_GROUP_ATTACK_BONUS)) {
rollConfig.parts[0] = [`${newFormula}${damageType ? `[${damageType}]` : ""}`];
const damageType = item.system.damage.parts[index][1];

if (lib.getSetting(CONSTANTS.SETTING_KEYS.ENABLE_GROUP_ATTACK_BONUS)) {
rollConfig.parts[index] = [`${newFormula}${damageType ? `[${damageType}]` : ""}`];
}
}

delete minionOnlyDamages[item.parent.uuid];

return true;
}

Expand Down Expand Up @@ -128,11 +136,11 @@ export default {
Hooks.on("dnd5e.rollDamage", async (item, damageRoll) => {

const validAttack = lib.isValidOverkillItem(item);
if(!validAttack) return true;
if (!validAttack) return true;

const hitTargets = Array.from(game.user.targets)

if(!hitTargets.length) return true;
if (!hitTargets.length) return true;

const hitTarget = hitTargets[0];

Expand All @@ -155,7 +163,7 @@ export default {
name: hitTarget.actor.name
});

if(lib.getSetting(CONSTANTS.SETTING_KEYS.ENABLE_OVERKILL_MESSAGE)) {
if (lib.getSetting(CONSTANTS.SETTING_KEYS.ENABLE_OVERKILL_MESSAGE)) {
ChatMessage.create({
content: `<h2>${game.i18n.localize("MINIONMANAGER.Dialogs.OverkillDamage.Title")}</h2><p>${label1}</p>`
});
Expand Down

0 comments on commit 355e270

Please sign in to comment.