Skip to content

Commit

Permalink
New Damage Splits
Browse files Browse the repository at this point in the history
- Can now choose split types in settings between `none`, `by-damage-type`, and `all`
  • Loading branch information
ChasarooniZ committed Aug 8, 2023
1 parent e4bad21 commit 0312aa4
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 3 deletions.
9 changes: 9 additions & 0 deletions languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@
"percent-max-health": "% of Max HP",
"none": "None"
}
},
"damage-split": {
"name": "Damage Split",
"hint": "How to split damage numbers from roll (None: total only, By Type: By damage type, All: Splits all terms)",
"choices": {
"none": "None",
"split-by-type": "By Type",
"split-all": "All"
}
}
}
}
Expand Down
99 changes: 96 additions & 3 deletions scripts/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Hooks.on("ready", async () => {
Hooks.on("createChatMessage", async function (msg, status, id) {
//console.log({ msg })
if (!msg.isDamageRoll || !game.user.isGM) return;
const dmg_list = extractDamageInfoCombined(msg.rolls);
const dmg_list = getDamageList(msg.rolls);
const targets = getTargetList(msg);
//console.log({ targets, dmg_list })
generateDamageScroll(dmg_list, targets);
Expand All @@ -23,6 +23,26 @@ export function getTargetList(msg) {
}
}

export function getDamageList(rolls) {
const split_type = game.settings.get("pf2e-rpg-numbers", 'damage-split');
let dmg_list = [];
switch (split_type) {
case 'none':
dmg_list = extractDamageInfoSimple(rolls);
break;
case 'split-by-type':
dmg_list = extractDamageInfoCombined(rolls);
break;
case 'split-all':
dmg_list = extractDamageInfoAll(rolls);
break;
default:
dmg_list = extractDamageInfoSimple(rolls);
break;
}
return dmg_list;
}

//TODO settings on visuals (colors)
//TODO settings on size etc.
//TODO add scaling based on % health
Expand Down Expand Up @@ -98,14 +118,87 @@ export function extractDamageInfoCombined(rolls) {
for (const inp of rolls) {
for (const term of inp.terms) {
for (const roll of term.rolls) {
const dmg = { type: roll.type, value: roll.total };
result.push(dmg);
result.push({ type: roll.type, value: roll.total });
}
}
}
return result;
}

export function extractDamageInfoAll(rolls) {
let result = [];

for (const inp of rolls) {
for (const term of inp.terms) {
result = result.concat(extractTerm(term))
}
}
console.log(result)
return result;
}

export function extractDamageInfoSimple(rolls) {
return [{ type: '', value: rolls.total }]
}

export function extractTerm(term, flavor = '') {
let result = [];
switch (term.class) {
case 'PoolTerm':
for (const roll of term.rolls) {
result = result.concat(extractTerm(roll, term.flavor || flavor));
}
break;
case '_DamageInstance2':
for (const item of term.terms) {
result = result.concat(extractTerm(item, term.types || flavor));
}
break;
case 'Grouping':
result = result.concat(extractTerm(term.term, term.flavor || flavor));
break;
case '_ArithmeticExpression2':
switch (term.operator) {
case '+':
for (const op of term.operands) {
result = result.concat(extractTerm(op, term.flavor || flavor));
}
break;
case '-':
result = result.concat(extractTerm(term.operands[0], term.flavor || flavor));
result = result.concat(extractTerm(term.operands[1], term.flavor || flavor)).map(t => { return { value: -t.value, type: t.type } });
case '*':
if (['NumericTerm', 'Die'].includes(term.operands[0].class)) {
result = result.concat(extractTerm(term.operands[1], term.flavor || flavor).flatMap(i => [i, i]));
} else if (['NumericTerm', 'Die'].includes(term.operands[1].class)) {
result = result.concat(extractTerm(term.operands[0], term.flavor || flavor).flatMap(i => [i, i]));
} else {
result.push({ value: term.total, type: term.flavor || flavor })
}

break;
default:
break;
}
break;
case 'Die':
for (const dice of term.results) {
result.push({ value: dice.result, type: term.flavor || flavor })
}
break;
case 'NumericTerm':
result.push({ value: term.number, type: term.flavor || flavor })
break;

default:
console.error({ msg: "Unrecognized Term when extracting parts", term })
result.push({ value: term.total, type: term.flavor || flavor })
break;
}

return result;
}

export function getFontScale(scaleType, dmg, tok) {
const maxFontScale = game.settings.get("pf2e-rpg-numbers", 'max-font-scale');
let scale = maxFontScale - 1;
Expand Down
14 changes: 14 additions & 0 deletions scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,18 @@ Hooks.on("init", () => {
},
});

game.settings.register("pf2e-rpg-numbers", "damage-split", {
name: game.i18n.localize("pf2e-rpg-numbers.module-settings.damage-split.name"),
hint: game.i18n.localize("pf2e-rpg-numbers.module-settings.damage-split.hint"),
scope: "world",
config: true,
default: "split-by-type",
type: String,
choices: {
["none"]: game.i18n.localize("pf2e-rpg-numbers.module-settings.damage-split.choices.none"),
["split-by-type"]: game.i18n.localize("pf2e-rpg-numbers.module-settings.damage-split.split-by-type"),
["split-all"]: game.i18n.localize("pf2e-rpg-numbers.module-settings.damage-split.choices.split-all"),
},
});

});

0 comments on commit 0312aa4

Please sign in to comment.