Added initiative roll (only tactics for the moment)

This commit is contained in:
Vlyan
2021-01-12 16:55:49 +01:00
parent bf80ad0e31
commit 92b60f78f7
5 changed files with 69 additions and 4 deletions

56
system/scripts/combat.js Normal file
View File

@@ -0,0 +1,56 @@
/**
* Roll initiative for one or multiple Combatants within the Combat entity
* @param {string|string[]} ids A Combatant id or Array of ids for which to roll
* @param {string|null} [formula] A non-default initiative formula to roll. Otherwise the system default is used.
* @param {boolean} [updateTurn] Update the Combat turn after adding new initiative scores to keep the turn on
* the same Combatant.
* @param {object} [messageOptions] Additional options with which to customize created Chat Messages
* @return {Promise<Combat>} A promise which resolves to the updated Combat entity once updates are complete.
*/
export async function rollInitiative(ids, { formula = null, updateTurn = true, messageOptions = {} } = {}) {
if (!Array.isArray(ids)) {
ids = [ids];
}
const updatedCombatants = [];
ids.forEach((combatantId) => {
const combatant = game.combat.combatants.find((c) => c._id === combatantId);
if (!combatant || !combatant.actor) {
return;
}
const data = combatant.actor.data.data;
const skillValue =
combatant.actor.data.type === "npc" ? data.skills["martial"] : data.skills["martial"]["tactics"];
const roll = new game.l5r5e.RollL5r5e(
`${data.rings[data.stance]}dr[${data.stance}] + ${skillValue}ds[tactics]`
);
roll.actor = combatant.actor;
roll.l5r5e.summary.difficulty = 1;
roll.roll();
roll.toMessage({ flavor: game.i18n.localize("l5r5e.chatdices.initiative_roll") });
updatedCombatants.push({
_id: combatant._id,
initiative: roll.l5r5e.summary.success,
});
});
// Update all combatants at once
await this.updateEmbeddedEntity("Combatant", updatedCombatants);
return this;
}
/**
* Define how the array of Combatants is sorted in the displayed list of the tracker.
* This method can be overridden by a system or module which needs to display combatants in an alternative order.
* By default sort by initiative, falling back to name
* @private
*/
export function _sortCombatants(a, b) {
// if tie, sort by honor, less honorable first
if (a.initiative === b.initiative) {
return a.actor.data.data.social.honor - b.actor.data.data.social.honor;
}
return b.initiative - a.initiative;
}