Skip to content

Commit

Permalink
Add support for rolling death saving throws
Browse files Browse the repository at this point in the history
  • Loading branch information
aaclayton committed Mar 1, 2020
1 parent bd9932e commit ffaec9b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@
"DND5E.DamImm": "Damage Immunities",
"DND5E.DamRes": "Damage Resistances",
"DND5E.DamVuln": "Damage Vulnerabilities",
"DND5E.DeathSave": "Death Saving Throws",
"DND5E.DeathSave": "Death Saves",
"DND5E.Description": "Description",
"DND5E.Details": "Details",
"DND5E.Equipped": "Equipped",
Expand Down
50 changes: 50 additions & 0 deletions module/actor/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,56 @@ export class Actor5e extends Actor {

/* -------------------------------------------- */

/**
* Perform a death saving throw, rolling a d20 plus any global save bonuses
* @param {Object} options Additional options which modify the roll
* @return {Promise<Roll>} A Promise which resolves to the Roll instance
*/
async rollDeathSave(options={}) {
const bonus = getProperty(this.data.data.bonuses, "abilities.save");
const parts = !!bonus ? ["@saveBonus"] : [];
const speaker = ChatMessage.getSpeaker({actor: this});
const roll = await Dice5e.d20Roll({
event: options.event,
parts: parts,
data: {saveBonus: parseInt(bonus)},
title: `Death Saving Throw`,
speaker: speaker
});

// Take action depending on the result
const success = roll.total >= 10;
const death = this.data.data.attributes.death;

// Save success
if ( success ) {
let successes = (death.success || 0) + (roll.total === 20 ? 2 : 1);
if ( successes === 3 ) { // Survival
await this.update({
"data.attributes.death.success": 0,
"data.attributes.death.failure": 0,
"data.attributes.hp.value": 1
});
await ChatMessage.create({content: `${this.name} has survived with 3 death save successes!`, speaker});
}
else await this.update({"data.attributes.death.success": Math.clamped(successes, 0, 3)});
}

// Save failure
else {
let failures = (death.failure || 0) + (roll.total === 1 ? 2 : 1);
await this.update({"data.attributes.death.failure": Math.clamped(failures, 0, 3)});
if ( failures === 3 ) { // Death
await ChatMessage.create({content: `${this.name} has died with 3 death save failures!`, speaker});
}
}

// Return the rolled result
return roll;
}

/* -------------------------------------------- */

/**
* Roll a hit die of the appropriate type, gaining hit points equal to the die roll plus your CON modifier
* @param {string} formula The hit die type to roll. Example "d8"
Expand Down
15 changes: 15 additions & 0 deletions module/actor/sheets/character.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,21 @@ export class ActorSheet5eCharacter extends ActorSheet5e {
// Short and Long Rest
html.find('.short-rest').click(this._onShortRest.bind(this));
html.find('.long-rest').click(this._onLongRest.bind(this));

// Death saving throws
html.find('.death-save').click(this._onDeathSave.bind(this));
}

/* -------------------------------------------- */

/**
* Handle rolling a death saving throw for the Character
* @param {MouseEvent} event The originating click event
* @private
*/
_onDeathSave(event) {
event.preventDefault();
return this.actor.rollDeathSave({event: event});
}

/* -------------------------------------------- */
Expand Down
2 changes: 1 addition & 1 deletion templates/actors/character-sheet.html
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ <h4 class="attribute-name box-title">{{ localize "DND5E.Initiative" }}</h4>
{{!-- Counters --}}
<div class="counters flexrow">
<div class="counter flexrow death-saves">
<h4>{{ localize "DND5E.DeathSave" }}</h4>
<h4 class="death-save rollable">{{ localize "DND5E.DeathSave" }}</h4>
<div class="counter-value">
<i class="fas fa-check"></i>
<input type="text" name="data.attributes.death.success" data-dtype="Number" placeholder="0"
Expand Down

0 comments on commit ffaec9b

Please sign in to comment.