Skip to content

Commit

Permalink
Merge pull request #177 from draconas1/rollData
Browse files Browse the repository at this point in the history
Roll data
  • Loading branch information
EndlesNights authored Feb 14, 2022
2 parents 363f081 + 8d06e37 commit 882fd57
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 135 deletions.
123 changes: 46 additions & 77 deletions module/actor/actor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { d20Roll, damageRoll } from "../dice.js";
import AbilityUseDialog from "../apps/ability-use-dialog.js";
import AbilityTemplate from "../pixi/ability-template.js"
import { d20Roll } from "../dice.js";
import { DND4EBETA } from "../config.js";
import { Helper } from "../helper.js"

Expand All @@ -9,44 +7,6 @@ import { Helper } from "../helper.js"
* @extends {Actor}
*/
export class Actor4e extends Actor {

/** @inheritdoc */
getRollData() {
const data = super.getRollData();
return data;
}
// getRollData() {
// const data = super.getRollData();
// const shorthand = game.settings.get("dnd4e", "macroShorthand");

// Re-map all attributes onto the base roll data
// if ( !!shorthand ) {
// for ( let [k, v] of Object.entries(data.attributes) ) {
// if ( !(k in data) ) data[k] = v.value;
// }
// delete data.attributes;
// }

// Map all items data using their slugified names
// data.items = this.data.items.reduce((obj, i) => {
// let key = i.name.slugify({strict: true});
// let itemData = duplicate(i.data);
// if ( !!shorthand ) {
// console.log( Object);
// console.log( itemData);

// for ( let [k, v] of Object.entries(itemData.attributes) ) {
// if ( !(k in itemData) ) itemData[k] = v.value;
// }
// delete itemData["attributes"];
// }
// obj[key] = itemData;
// return obj;
// }, {});

// return data;
// }

constructor(data, context) {
super(data, context);

Expand Down Expand Up @@ -96,46 +56,47 @@ export class Actor4e extends Actor {
return super.update(data, options);
}


/** @inheritdoc */
getRollData() {
this.prepareDerivedData();
const data = super.getRollData();
data["strMod"] = data.abilities["str"].mod
data["conMod"] = data.abilities["con"].mod
data["dexMod"] = data.abilities["dex"].mod
data["intMod"] = data.abilities["int"].mod
data["wisMod"] = data.abilities["wis"].mod
data["chaMod"] = data.abilities["cha"].mod

data["lvhalf"] = Math.floor(data.details.level/2)
data["lv"] = data.details.level
data["tier"] = data.details.tier

data["heroic"] = data.details.level < 11 ? 1 : 0
data["paragon"] = data.details.level >= 11 && data.details.level < 21 ? 1 : 0
data["epic"] = data.details.level >= 21 ? 1 : 0

data["heroicOrParagon"] = data.details.level < 21 ? 1 : 0
data["paragonOrEpic"] = data.details.level >= 11 ? 1 : 0
return data;
}

/**
* Augment the basic actor data with additional dynamic data.
*/
prepareData() {
super.prepareData();
// Get the Actor's data object
* Currently this only does attributes, but can increase it in future if there are more things we want in effects
*/
prepareDerivedData() {
const actorData = this.data;
const data = actorData.data;
const flags = actorData.flags.dnd4eBeta || {};
const bonuses = getProperty(data, "bonuses.abilities") || {};

let originalSaves = null;
let originalSkills = null;

this.data.data.halfLevelOptions = game.settings.get("dnd4e", "halfLevelOptions");

// If we are a polymorphed actor, retrieve the skills and saves data from
// the original actor for later merging.
if (this.isPolymorphed) {
const transformOptions = this.getFlag('dnd4eBeta', 'transformOptions');
const original = game.actors?.get(this.getFlag('dnd4eBeta', 'originalActor'));

if (original) {
if (transformOptions.mergeSaves) {
originalSaves = original.data.data.abilities;
}

if (transformOptions.mergeSkills) {
originalSkills = original.data.data.skills;
}
}
}

// Ability modifiers and saves
// Character All Ability Check" and All Ability Save bonuses added when rolled since not a fixed value.
// Character All Ability Check" and All Ability Save bonuses added when rolled since not a fixed value.
const saveBonus = Number.isNumeric(bonuses.save) ? parseInt(bonuses.save) : 0;
const checkBonus = Number.isNumeric(bonuses.check) ? parseInt(bonuses.check) : 0;

for (let [id, abl] of Object.entries(data.abilities)) {

for (let [id, abl] of Object.entries(data.abilities)) {
abl.mod = Math.floor((abl.value - 10) / 2);
abl.modHalf = abl.mod + Math.floor(data.details.level / 2);
abl.prof = (abl.proficient || 0);
Expand All @@ -147,14 +108,22 @@ export class Actor4e extends Actor {
abl.checkBonus = checkBonus + Math.floor(data.details.level / 2);
}
abl.save = abl.mod + abl.prof + abl.saveBonus;

abl.label = game.i18n.localize(DND4EBETA.abilities[id]); //.localize("");

// If we merged saves when transforming, take the highest bonus here.
if (originalSaves && abl.proficient) {
abl.save = Math.max(abl.save, originalSaves[id].save);
}

abl.label = game.i18n.localize(DND4EBETA.abilities[id]);
}
}


/**
* Augment the basic actor data with additional dynamic data.
*/
prepareData() {
super.prepareData();
// Get the Actor's data object
const actorData = this.data;
const data = actorData.data;

this.prepareDerivedData();

//HP auto calc
if(data.attributes.hp.autototal)
Expand Down
19 changes: 15 additions & 4 deletions module/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,13 @@ export class Helper {

newFormula = newFormula.replaceAll("@atkMod", actorInnerData.modifiers.attack.value);
newFormula = newFormula.replaceAll("@dmgMod", actorInnerData.modifiers.damage.value);

newFormula = newFormula.replaceAll("@heroic", actorInnerData.details.level < 11 ? 1 : 0);
newFormula = newFormula.replaceAll("@paragon", actorInnerData.details.level >= 11 && actorInnerData.details.level < 21 ? 1 : 0);
newFormula = newFormula.replaceAll("@epic", actorInnerData.details.level >= 21 ? 1 : 0);

newFormula = newFormula.replaceAll("@heroicOrParagon", actorInnerData.details.level < 21 ? 1 : 0);
newFormula = newFormula.replaceAll("@paragonOrEpic", actorInnerData.details.level >= 11 ? 1 : 0);
}
else {
console.log("An actor data object without a .data property was passed to common replace. Probably passed actor.data.data by mistake!. Replacing: " + formula)
Expand Down Expand Up @@ -348,7 +355,8 @@ export class Helper {
newFormula = this.replaceData (newFormula, weaponInnerData);


//deprecated, kept for legacy purposes
//deprecated, kept for legacy purposes and because it's really handy for High Crit Weapons!
// make sure to keep the dice formula same as main. Definite candidate for a future refactor.
if(newFormula.includes("@wepDice")) {
let parts = weaponInnerData.damageDice.parts;
let indexStart = newFormula.indexOf("@wepDice")+8;
Expand All @@ -371,7 +379,8 @@ export class Helper {
}
if (i < parts.length - 1) dice += '+';
}
dice = this.commonReplace(dice, actorData, powerInnerData, weaponInnerData, depth-1)
const possibleDice = this.commonReplace(dice, actorData, powerInnerData, weaponInnerData, depth-1)
dice = possibleDice !== 0 ? possibleDice : dice //there probably shouldn't be any formula left, because @wepDice is a formula contents under our command. So if we had hit the bottom of the recursion tree, just try the original
newFormula = newFormula.slice(0, indexStart) + newFormula.slice(indexEnd, newFormula.length);
newFormula = newFormula.replaceAll("@wepDice", dice);
}
Expand All @@ -381,6 +390,7 @@ export class Helper {
// - weapon based damage
// - flat damage
// - dice damage
// make sure to keep the weapon dice formula same as above. Definite candidate for a future refactor.
if(newFormula.includes("@powBase")) {
let quantity = powerInnerData.hit.baseQuantity;
let diceType = powerInnerData.hit.baseDiceType.toLowerCase();
Expand Down Expand Up @@ -444,13 +454,14 @@ export class Helper {
if(newFormula.includes("@powMax")) {
let dice = "";
let quantity = powerInnerData.hit.baseQuantity;
quantity = this.commonReplace(quantity, actorData, powerInnerData, weaponInnerData, 0)
let diceType = powerInnerData.hit.baseDiceType.toLowerCase();
let rQuantity = new Roll(`${quantity}`)
rQuantity.evaluate({maximize: true, async: false});

//check if is valid number
if(this._isNumber(rQuantity.result)){
quantity = rQuantity.result;
if(this._isNumber(rQuantity.total)){
quantity = rQuantity.total;
} else {
quantity = 1;
}
Expand Down
4 changes: 2 additions & 2 deletions module/roll/roll-with-expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class RollWithOriginalExpression extends Roll {
* new RollWithOriginalExpression(bracketFormula, {"bonus" : "1d6"}, {expressionArr: ["@wepAtk + @enhance", "@bonus"], formulaInnerData: {wepAtk: 3, enhance: 1}})
*/
constructor (formula, data={}, options={}) {
super(formula, data, foundry.utils.mergeObject({expression : formula}, options));
super(formula, data, foundry.utils.mergeObject({expression : formula, originalFormula: formula}, options));
this.expression = options.expression ? options.expression : formula
}

Expand Down Expand Up @@ -163,7 +163,7 @@ export class RollWithOriginalExpression extends Roll {

getChatData(isPrivate = false) {
if (!isPrivate && game.settings.get("dnd4e", "showRollExpression")) {
return this.surroundFormulaWithExpressionSpanTags(this._formula, this.options.expressionArr)
return this.surroundFormulaWithExpressionSpanTags(this.options.originalFormula ? this.options.originalFormula : this._formula, this.options.expressionArr)
}
else {
return {
Expand Down
2 changes: 1 addition & 1 deletion packs/example_character.db

Large diffs are not rendered by default.

Loading

0 comments on commit 882fd57

Please sign in to comment.