Skip to content

Commit

Permalink
Include damage modifiers and dice in roll inspector (foundryvtt#5352)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosFdez committed Dec 29, 2022
1 parent 78d3fd6 commit 50815cd
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 20 deletions.
20 changes: 19 additions & 1 deletion src/module/chat-message/chat-roll-details.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BaseRawModifier, DamageDicePF2e } from "@actor/modifiers";
import { addSign } from "@util";
import { ChatMessagePF2e } from ".";

class ChatRollDetails extends Application {
Expand All @@ -23,7 +25,23 @@ class ChatRollDetails extends Application {
const remainingOptions = allOptions.filter((option) => option.includes(":"));
const rollOptions = [...topLevelOptions.sort(), ...remainingOptions.sort()];
const domains = context?.domains.sort();
return { context, domains, modifiers, rollOptions, hasModifiers: !!modifiers };
const preparedModifiers = modifiers ? this.prepareModifiers(modifiers) : [];
return { context, domains, modifiers: preparedModifiers, rollOptions, hasModifiers: !!modifiers };
}

protected prepareModifiers(modifiers: (BaseRawModifier | DamageDicePF2e)[]) {
return modifiers.map((mod) => {
const value = "dieSize" in mod ? `+${mod.diceNumber}${mod.dieSize}` : addSign(mod.modifier ?? 0);

return {
...mod,
value,
critical:
mod.critical !== null
? game.i18n.localize(`PF2E.RuleEditor.General.CriticalBehavior.${mod.critical}`)
: null,
};
});
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/module/chat-message/data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ItemType } from "@item/data";
import { MagicTradition } from "@item/spell/types";
import { RawModifier } from "@actor/modifiers";
import { BaseRawModifier } from "@actor/modifiers";
import { DegreeOfSuccessString } from "@system/degree-of-success";
import { ChatMessagePF2e } from ".";
import { RollNoteSource } from "@module/notes";
Expand All @@ -23,7 +23,7 @@ type ChatMessageFlagsPF2e = foundry.data.ChatMessageFlags & {
origin?: { type: ItemType; uuid: string } | null;
casting?: { id: string; level: number; tradition: MagicTradition } | null;
modifierName?: string;
modifiers?: RawModifier[];
modifiers?: BaseRawModifier[];
preformatted?: "flavor" | "content" | "both";
isFromConsumable?: boolean;
journalEntry?: DocumentUUID;
Expand Down
19 changes: 12 additions & 7 deletions src/module/item/spell/document.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
createAbilityModifier,
createProficiencyModifier,
DamageDicePF2e,
ensureProficiencyOption,
ModifierPF2e,
StatisticModifier,
Expand Down Expand Up @@ -45,7 +46,7 @@ interface SpellDamage {
roll: DamageRoll;
domains: string[];
options: Set<string>;
modifiers: ModifierPF2e[];
modifiers: (ModifierPF2e | DamageDicePF2e)[];
breakdownTags: string[];
}

Expand Down Expand Up @@ -274,6 +275,7 @@ class SpellPF2e extends ItemPF2e {

// Add modifiers and damage die adjustments
const modifiers: ModifierPF2e[] = [];
const damageDice: DamageDicePF2e[] = [];
if (actor?.isOfType("character", "npc")) {
const abilityModifiers = Object.entries(this.system.damage.value)
.filter(([, d]) => d.applyMod)
Expand Down Expand Up @@ -306,10 +308,12 @@ class SpellPF2e extends ItemPF2e {
}
}

const damageDice = extractDamageDice(actor.synthetics.damageDice, domains, {
test: options,
resolvables: { spell: this },
});
damageDice.push(
...extractDamageDice(actor.synthetics.damageDice, domains, {
test: options,
resolvables: { spell: this },
})
);
applyDamageDice(Object.values(formulas), damageDice);
}

Expand Down Expand Up @@ -359,7 +363,7 @@ class SpellPF2e extends ItemPF2e {

if (instances.length) {
const roll = DamageRoll.fromTerms([InstancePool.fromRolls(instances)]);
return { roll, breakdownTags, domains, options, modifiers };
return { roll, breakdownTags, domains, options, modifiers: [...modifiers, ...damageDice] };
}
} catch (err) {
console.error(err);
Expand Down Expand Up @@ -788,6 +792,7 @@ class SpellPF2e extends ItemPF2e {
notes: [],
materials: [],
traits: this.castingTraits,
modifiers,
};

const context: DamageRollContext = {
Expand All @@ -799,8 +804,8 @@ class SpellPF2e extends ItemPF2e {
self: {
actor: this.actor,
item: this,
modifiers: modifiers,
token: this.actor.token,
modifiers: [],
},
};

Expand Down
1 change: 1 addition & 0 deletions src/module/system/damage/damage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ export class DamagePF2e {
context: contextFlag,
damageRoll: rollData,
target: targetFlag,
modifiers: data.modifiers,
origin,
strike,
preformatted: "both",
Expand Down
1 change: 1 addition & 0 deletions src/module/system/damage/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ interface BaseDamageTemplate {
notes: RollNotePF2e[];
traits: string[];
materials: WeaponMaterialEffect[];
modifiers?: (ModifierPF2e | DamageDicePF2e)[];
}

interface WeaponDamageTemplate extends BaseDamageTemplate {
Expand Down
1 change: 1 addition & 0 deletions src/module/system/damage/weapon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ class WeaponDamagePF2e {
notes,
traits: (traits ?? []).map((t) => t.name),
materials: Array.from(materials),
modifiers: [...modifiers, ...damageDice],
damage: {
...damage,
formula: {
Expand Down
1 change: 1 addition & 0 deletions src/styles/ui/sidebar/chat/_chat-roll-details.scss
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@

.modifier-list {
@include scrollbar;
flex: 1 0 0;
display: flex;
flex-direction: column;
gap: 4px;
Expand Down
10 changes: 3 additions & 7 deletions src/util/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,13 @@ function isBlank(text: Optional<string>): text is null | undefined | "" {
return text === null || text === undefined || text.trim() === "";
}

/**
* Adds a + if positive, nothing if 0 or - if negative
*/
/** Returns a formatted number string with a preceding + if non-negative */
function addSign(number: number): string {
if (number < 0) {
return `${number}`;
}
if (number > 0) {
return `+${number}`;
}
return "0";

return `+${number}`;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions static/templates/chat/chat-roll-details.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
<div class="modifier {{disabled (not m.enabled)}}">
<h4>{{m.label}} <span class="slug">({{m.slug}})</span></h4>
<div>
<span>{{m.type}} {{numberFormat m.modifier sign=true}}</span>
<span>{{m.ability}}</span>
<span>{{m.type}} {{m.value}} {{m.damageType}} {{#if m.category}}({{m.category}}){{/if}}</span>
<span>{{m.critical}} {{m.ability}}</span>
</div>
</div>
{{/each}}
Expand Down
2 changes: 1 addition & 1 deletion tests/module/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { addSign, applyNTimes, padArray, zip } from "@util";

describe("format sign for numbers", () => {
test("0", () => {
expect(addSign(0)).toEqual("0");
expect(addSign(0)).toEqual("+0");
});

test("negative", () => {
Expand Down

0 comments on commit 50815cd

Please sign in to comment.