From fd9a54e67701e6d5ed02b9119cc89a0d6e11c60e Mon Sep 17 00:00:00 2001 From: sladecraven Date: Wed, 2 Sep 2020 22:00:35 +0200 Subject: [PATCH] Compute initiative --- module/actor-sheet.js | 10 ++++++- module/rdd-main.js | 65 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/module/actor-sheet.js b/module/actor-sheet.js index c4bccc6c..fc9564d9 100644 --- a/module/actor-sheet.js +++ b/module/actor-sheet.js @@ -75,7 +75,15 @@ export class RdDActorSheet extends ActorSheet { if (melee.name == arme.data.competence ) arme.data.niveau = melee.data.niveau } - } + for ( const tir of data.competenceByCategory.tir ) { + if (tir.name == arme.data.competence ) + arme.data.niveau = tir.data.niveau + } + for ( const lancer of data.competenceByCategory.lancer ) { + if (lancer.name == arme.data.competence ) + arme.data.niveau = lancer.data.niveau + } + } // To avoid armour and so on... data.data.armes_seules = duplicate( this._checkNull(data.itemsByType['arme'])); diff --git a/module/rdd-main.js b/module/rdd-main.js index 1ed8368a..e64cff74 100644 --- a/module/rdd-main.js +++ b/module/rdd-main.js @@ -18,6 +18,68 @@ import { TMRUtility } from "./tmr-utility.js"; /* Foundry VTT Initialization */ /* -------------------------------------------- */ +/************************************************************************************/ +const _patch_initiative = () => { + Combat.prototype.rollInitiative = async function ( + ids, + formula = null, + messageOptions = {} + ) { + console.log( + `${game.data.system.data.title} | Combat.rollInitiative()`, + ids, + formula, + messageOptions + ); + // Structure input data + ids = typeof ids === "string" ? [ids] : ids; + const currentId = this.combatant._id; + + // calculate initiative + for ( let cId = 0; cId < ids.length; cId++) { + const c = this.getCombatant( ids[cId] ); + //if (!c) return results; + + let armeCombat; + for (const item of c.actor.data.items) { + if (item.type == "arme" && item.data.equipe) { + armeCombat = duplicate(item); + } + } + + //console.log("Combatat", c); + let compName = ( armeCombat == undefined ) ? "Corps à corps" : armeCombat.data.competence; + let competence = RdDUtility.findCompetence( c.actor.data.items, compName ); + //const cf = formula || this._getInitiativeFormula(c); + let rollFormula = "1d6+" + competence.data.niveau + "+" + Math.ceil(c.actor.data.data.carac[competence.data.defaut_carac].value/2); + const roll = this._getInitiativeRoll(c, rollFormula); + //console.log("Compute init for", armeCombat, competence, rollFormula, roll.total); + + await this.updateEmbeddedEntity("Combatant", { _id: c._id, initiative: roll.total }); + + // Send a chat message + let rollMode = + messageOptions.rollMode || game.settings.get("core", "rollMode"); + let messageData = mergeObject( + { + speaker: { + scene: canvas.scene._id, + actor: c.actor ? c.actor._id : null, + token: c.token._id, + alias: c.token.name, + sound: CONFIG.sounds.dice, + }, + flavor: `${c.token.name} a fait son jet d'Initiative (Compétence ${competence.name})`, + }, + messageOptions + ); + roll.toMessage(messageData, { rollMode, create: true }); + } + return this; + }; +} + +/************************************************************************************/ Hooks.once("init", async function() { console.log(`Initializing Reve de Dragon System`); @@ -60,6 +122,9 @@ Hooks.once("init", async function() { Actors.registerSheet("foundryvtt-reve-de-dragon", RdDActorSheet, { makeDefault: true }); Items.unregisterSheet("core", ItemSheet); Items.registerSheet("foundryvtt-reve-de-dragon", RdDItemSheet, {makeDefault: true}); + + // Patch the initiative formula + _patch_initiative(); }); /* -------------------------------------------- */