Skip to content

Commit

Permalink
[foundryvtt#1846] Add ability score defaults to config
Browse files Browse the repository at this point in the history
  • Loading branch information
arbron committed Jan 26, 2023
1 parent 84f8608 commit 34a90c6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
31 changes: 22 additions & 9 deletions module/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ ______ ______ _____ _____
|___/ \\___/\\/___/ \\____/\\____/
_______________________________`;

/**
* Configuration data for abilities.
*
* @typedef {object} AbilityConfiguration
* @property {string} label Localized label.
* @property {string} abbreviation Localized abbreviation.
* @property {Object<string, number|string>} defaults Default values for this ability based on actor type.
* If a string is used, the system will attempt to fetch the value of the
* specified ability.
*/

/**
* The set of Ability Scores used within the system.
* @enum {{
* label: string,
* abbreviation: string
* }}
* @enum {AbilityConfiguration}
*/
DND5E.abilities = {
str: {
Expand All @@ -36,23 +44,28 @@ DND5E.abilities = {
},
int: {
label: "DND5E.AbilityInt",
abbreviation: "DND5E.AbilityIntAbbr"
abbreviation: "DND5E.AbilityIntAbbr",
defaults: { vehicle: 0 }
},
wis: {
label: "DND5E.AbilityWis",
abbreviation: "DND5E.AbilityWisAbbr"
abbreviation: "DND5E.AbilityWisAbbr",
defaults: { vehicle: 0 }
},
cha: {
label: "DND5E.AbilityCha",
abbreviation: "DND5E.AbilityChaAbbr"
abbreviation: "DND5E.AbilityChaAbbr",
defaults: { vehicle: 0 }
},
hon: {
label: "DND5E.AbilityHon",
abbreviation: "DND5E.AbilityHonAbbr"
abbreviation: "DND5E.AbilityHonAbbr",
defaults: { npc: "cha", vehicle: 0 }
},
san: {
label: "DND5E.AbilitySan",
abbreviation: "DND5E.AbilitySanAbbr"
abbreviation: "DND5E.AbilitySanAbbr",
defaults: { npc: "wis", vehicle: 0 }
}
};
preLocalize("abilities", { keys: ["label", "abbreviation"] });
Expand Down
16 changes: 5 additions & 11 deletions module/documents/actor/actor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -229,22 +229,16 @@ export default class Actor5e extends Actor {
_prepareBaseAbilities() {
if ( !("abilities" in this.system) ) return;
const abilities = {};
for ( const key of Object.keys(CONFIG.DND5E.abilities) ) {
for ( const [key, config] of Object.entries(CONFIG.DND5E.abilities) ) {
abilities[key] = this.system.abilities[key];
if ( !abilities[key] ) {
abilities[key] = foundry.utils.deepClone(game.system.template.Actor.templates.common.abilities.cha);

// Honor: Charisma for NPC, 0 for vehicles
if ( key === "hon" ) {
if ( this.type === "vehicle" ) abilities[key].value = 0;
else if ( this.type === "npc" ) abilities[key].value = this.system.abilities.cha?.value ?? 10;
}

// Sanity: Wisdom for NPC, 0 for vehicles
else if ( key === "san" ) {
if ( this.type === "vehicle" ) abilities[key].value = 0;
else if ( this.type === "npc" ) abilities[key].value = this.system.abilities.wis?.value ?? 10;
let defaultValue = config.defaults?.[this.type] ?? 10;
if ( typeof defaultValue === "string" ) {
defaultValue = abilities[defaultValue].value ?? this.system.abilities[defaultValue] ?? 10;
}
abilities[key].value = defaultValue;
}
}
this.system.abilities = abilities;
Expand Down

0 comments on commit 34a90c6

Please sign in to comment.