Skip to content

Commit

Permalink
Tidy up style for toHit label merge
Browse files Browse the repository at this point in the history
  • Loading branch information
aaclayton committed Dec 31, 2020
1 parent 43e9533 commit 45502ba
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 47 deletions.
5 changes: 1 addition & 4 deletions module/actor/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,9 @@ export default class Actor5e extends Actor {
data.attributes.spelldc = data.attributes.spellcasting ? data.abilities[data.attributes.spellcasting].dc : 10;
this._computeSpellcastingProgression(this.data);

// recompute item labels now that the actor has data
// Compute owned item attributes which depend on prepared Actor data
this.items.forEach(item => {
// Compute ability save DCs that depend on the calling actor
item.getSaveDC();

// Compute item toHits that depend on the calling actor
item.getAttackToHit();
});
}
Expand Down
75 changes: 32 additions & 43 deletions module/item/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,59 +294,48 @@ export default class Item5e extends Item {
// Define Roll bonuses
const parts = [];

// add the item's innate attack bonus
// Include the item's innate attack bonus as the initial value and label
if ( itemData.attackBonus ) {
parts.push(itemData.attackBonus)

// Set toHit label to be the innate attack bonus
this.labels.toHit = itemData.attackBonus;
};

// if the item is an owned item get some more info from the actor
if (this.isOwned) {

// add the ability score modifier
parts.push(`@mod`);
}

// add prof if proficient or
// if this attack comes from an item type that isn't a weapon or consumable (e.g. spell, class feature)
if ( !["weapon", "consumable"].includes(this.data.type) || itemData.proficient ) {
parts.push("@prof");
}
// Take no further action for un-owned items
if ( !this.isOwned ) return {rollData, parts};

// add the actor's bonus to attacks with this item's actionType (Weapon Type)
const actorBonus = this.actor.data.data.bonuses?.[itemData.actionType] || {};
if ( actorBonus.attack ) parts.push(actorBonus.attack);

// check for an ammo bonus
if ( itemData.consume.type === 'ammo' && !!this.actor.items ) {
const ammoItemData = this.actor.items.get(itemData.consume.target)?.data;

const ammoItemQuantity = ammoItemData?.data.quantity;
const ammoCanBeConsumed = ammoItemQuantity && (ammoItemQuantity - (itemData.consume.amount ?? 0) >= 0);
const ammoItemAttackBonus = ammoItemData?.data.attackBonus;
const ammoIsTypeConsumable = (ammoItemData.type === "consumable") && (ammoItemData.data.consumableType === "ammo")

// only add the ammoBonus to the attack roll if
// the ammution is a consumable with type 'ammo'
// AND that ammo has a bonus
// AND the actor has enough ammo to use
if ( ammoCanBeConsumed && ammoItemAttackBonus && ammoIsTypeConsumable ) {
parts.push("@ammo");
rollData["ammo"] = ammoItemAttackBonus;
}
}
// Ability score modifier
parts.push(`@mod`);

let toHitLabel = new Roll(parts.join('+'), rollData).formula.trim();
// Add proficiency bonus if an explicit proficiency flag is present or for non-item features
if ( !["weapon", "consumable"].includes(this.data.type) || itemData.proficient ) {
parts.push("@prof");
}

if (toHitLabel.charAt(0) !== '-') {
toHitLabel = '+ ' + toHitLabel
// Actor-level global bonus to attack rolls
const actorBonus = this.actor.data.data.bonuses?.[itemData.actionType] || {};
if ( actorBonus.attack ) parts.push(actorBonus.attack);

// One-time bonus provided by consumed ammunition
if ( itemData.consume.type === 'ammo' && !!this.actor.items ) {
const ammoItemData = this.actor.items.get(itemData.consume.target)?.data;
const ammoItemQuantity = ammoItemData?.data.quantity;
const ammoCanBeConsumed = ammoItemQuantity && (ammoItemQuantity - (itemData.consume.amount ?? 0) >= 0);
const ammoItemAttackBonus = ammoItemData?.data.attackBonus;
const ammoIsTypeConsumable = (ammoItemData.type === "consumable") && (ammoItemData.data.consumableType === "ammo")
if ( ammoCanBeConsumed && ammoItemAttackBonus && ammoIsTypeConsumable ) {
parts.push("@ammo");
rollData["ammo"] = ammoItemAttackBonus;
}
}

// Update labels
this.labels.toHit = toHitLabel;
// Condense the resulting attack bonus formula into a simplified label
let toHitLabel = new Roll(parts.join('+'), rollData).formula.trim();
if (toHitLabel.charAt(0) !== '-') {
toHitLabel = '+ ' + toHitLabel
}
this.labels.toHit = toHitLabel;

// Update labels and return the prepared roll data
return {rollData, parts};
}

Expand Down Expand Up @@ -817,7 +806,7 @@ export default class Item5e extends Item {
const consumeAmount = consume.amount ?? 0;
if ( q && (q - consumeAmount >= 0) ) {
this._ammo = ammo;
title += ` [${this._ammo.name}]`;
title += ` [${ammo.name}]`;
}
}

Expand Down

0 comments on commit 45502ba

Please sign in to comment.