diff --git a/module/rdd-combat.js b/module/rdd-combat.js index e02260ae..6401f7a8 100644 --- a/module/rdd-combat.js +++ b/module/rdd-combat.js @@ -72,7 +72,7 @@ export class RdDCombatManager extends Combat { /* -------------------------------------------- */ async finDeRound(options = { terminer: false }) { - this.combatants.map(it => RdDCombatManager.getActorCombatant(it)) + this.combatants.map(it => RdDCombatManager.getActorCombatant(it, { warning: false })) .filter(it => it != undefined) .forEach(async actor => { await actor.finDeRound(options) @@ -80,13 +80,17 @@ export class RdDCombatManager extends Combat { }) } - static getActorCombatant(combatant) { + static getActorCombatant(combatant, options = { warning: true }) { if (!combatant.actor) { - ui.notifications.warn(`Le combatant ${combatant.name} n'est pas associé à un acteur!`) + if (options.warning) { + ui.notifications.warn(`Le combatant ${combatant.name} n'est pas associé à un acteur!`) + } return undefined } else if (!combatant.actor.isActorCombat()) { - ui.notifications.warn(`${combatant.name} ne peut pas combattre!`) + if (options.warning) { + ui.notifications.warn(`${combatant.name} ne peut pas combattre!`) + } return undefined } return combatant.actor @@ -100,56 +104,27 @@ export class RdDCombatManager extends Combat { } /************************************************************************************/ - async rollInitiative(ids, formula = undefined, messageOptions = {}) { - console.log(`${game.system.title} | Combat.rollInitiative()`, ids, formula, messageOptions); + async rollInitiative(ids, messageOptions = {}) { + console.log(`${game.system.title} | Combat.rollInitiative()`, ids, messageOptions) + ids = typeof ids === "string" ? [ids] : ids + ids.forEach(async id => + await this.rollInitRdD(id, undefined, messageOptions) + ) + return this + } - ids = typeof ids === "string" ? [ids] : ids; - // calculate initiative - for (let cId = 0; cId < ids.length; cId++) { - const combatant = this.combatants.get(ids[cId]); - const ajustement = RdDCombatManager.calculAjustementInit(combatant.actor, undefined); - let rollFormula = formula ?? RdDCombatManager.formuleInitiative(2, 10, 0, ajustement); - - if (!formula) { - if (combatant.actor.type == 'creature' || combatant.actor.type == 'entite') { - const competence = combatant.actor.items.find(it => RdDItemCompetenceCreature.isAttaque(it)) - if (competence) { - rollFormula = RdDCombatManager.formuleInitiative(2, competence.system.carac_value, competence.system.niveau, etatGeneral); - } - } else { - const armeCombat = combatant.actor.itemTypes['arme'].find(it => it.system.equipe) - let compName = "Corps à corps" - if (armeCombat) { - if (armeCombat.system.competence != "") { - compName = armeCombat.system.competence - } - if (armeCombat.system.lancer != "") { - compName = armeCombat.system.lancer - } - if (armeCombat.system.tir != "") { - compName = armeCombat.system.tir - } - } - const competence = RdDItemCompetence.findCompetence(combatant.actor.items, compName); - if (competence && competence.system.defaut_carac) { - const carac = combatant.actor.system.carac[competence.system.defaut_carac].value; - const niveau = competence.system.niveau; - - const ajustement = RdDCombatManager.calculAjustementInit(combatant.actor, armeCombat) - rollFormula = RdDCombatManager.formuleInitiative(2, carac, niveau, ajustement); - } else { - ui.notifications.warn(`Votre arme ${armeCombat.name} n'a pas de compétence renseignée`); - } - } - } + async rollInitRdD(id, formula, messageOptions = {}) { + const combatant = this.combatants.get(id); + const actor = RdDCombatManager.getActorCombatant(combatant) + if (actor) { + const rollFormula = formula ?? RdDCombatManager.getFirstInitRollFormula(actor) const roll = combatant.getInitiativeRoll(rollFormula); if (!roll.total) { await roll.evaluate(); } const total = Math.max(roll.total, 0.00); console.log("Compute init for", rollFormula, roll, total, combatant); - let id = combatant._id || combatant.id; - await this.updateEmbeddedDocuments("Combatant", [{ _id: id, initiative: total }]); + await this.updateEmbeddedDocuments("Combatant", [{ _id: combatant._id || combatant.id, initiative: total }]); // Send a chat message let rollMode = messageOptions.rollMode || game.settings.get("core", "rollMode"); @@ -161,7 +136,7 @@ export class RdDCombatManager extends Combat { alias: combatant.token.name, sound: CONFIG.sounds.dice, }, - flavor: `${combatant.token.name} a fait son jet d'Initiative (${messageOptions.info})
`, + flavor: `${combatant.token.name} a fait son jet d'Initiative (${messageOptions.info})
` }, messageOptions); roll.toMessage(messageData, { rollMode, create: true }); @@ -169,7 +144,20 @@ export class RdDCombatManager extends Combat { RdDCombatManager.processPremierRoundInit(); } return this; - }; + } + + static getFirstInitRollFormula(actor) { + const actions = actor.listActionsCombat() + if (actions.length > 0) { + const action = actions[0] + const init = RdDCombatManager.getInitData(actor, action) + const ajustement = RdDCombatManager.calculAjustementInit(actor, action) + return RdDCombatManager.formuleInitiative(init.offset, init.carac, init.niveau, ajustement); + } + + let ajustement = RdDCombatManager.calculAjustementInit(actor, undefined); + return RdDCombatManager.formuleInitiative(2, 10, 0, ajustement); + } static formuleInitiative(rang, carac, niveau, bonusMalus) { return `${rang} +( (${RdDCombatManager.calculInitiative(niveau, carac, bonusMalus)} )/100)`; @@ -316,7 +304,7 @@ export class RdDCombatManager extends Combat { const ajustement = RdDCombatManager.calculAjustementInit(actor, action) const rollFormula = RdDCombatManager.formuleInitiative(init.offset, init.carac, init.niveau, ajustement); - game.combat.rollInitiative(combatantId, rollFormula, init); + game.combat.rollInitRdD(combatantId, rollFormula, init); } static getInitData(actor, action) { @@ -352,35 +340,34 @@ export class RdDCombatManager extends Combat { /* -------------------------------------------- */ static displayInitiativeMenu(html, combatantId) { const combatant = game.combat.combatants.get(combatantId) - const actions = RdDCombatManager.listActionsCombatant(combatant); - // Build the relevant submenu - const menuItems = actions.map(action => { - return { - name: action.system.competence, - icon: "", - callback: target => { RdDCombatManager.rollInitiativeAction(combatantId, action) } + const actor = RdDCombatManager.getActorCombatant(combatant, { warning: false }) + if (actor) { + const actions = RdDCombatManager.listActionsActorCombatant(actor) + // Build the relevant submenu + const menuItems = actions.map(action => { + return { + name: action.system.competence, + icon: "", + callback: target => { RdDCombatManager.rollInitiativeAction(combatantId, action) } + } + }) + if (menuItems.length > 0) { + new ContextMenu(html, ".directory-list", menuItems).render(); } - }) - if (menuItems.length > 0) { - new ContextMenu(html, ".directory-list", menuItems).render(); } } /* -------------------------------------------- */ - static listActionsCombatant(combatant) { - const actor = RdDCombatManager.getActorCombatant(combatant) - if (actor) { - const possessions = actor.listActionsPossessions() - const actions = possessions.length > 0 - ? possessions - : actor.listActionsCombat() + static listActionsActorCombatant( actor) { + const possessions = actor.listActionsPossessions() + const actions = possessions.length > 0 + ? possessions + : actor.listActionsCombat() - for (let index = 0; index < actions.length; index++) { - actions[index].index = index - } - return actions + for (let index = 0; index < actions.length; index++) { + actions[index].index = index } - return [] + return actions } diff --git a/module/rdd-token-hud.js b/module/rdd-token-hud.js index d0040f0b..e7639743 100644 --- a/module/rdd-token-hud.js +++ b/module/rdd-token-hud.js @@ -28,16 +28,15 @@ export class RdDTokenHud { await RdDTokenHud.addExtensionHudSoins(html, actor); if (isCombat) { - let combatant = game.combat.combatants.find(c => c.tokenId == tokenId); - if (!(combatant?.actor)) { - ui.notifications.warn(`Le combatant ${token.name} n'est pas associé à un acteur, impossible de déterminer ses actions de combat!`) - return; + const combatant = game.combat.combatants.find(c => c.tokenId == tokenId) + const actor = RdDCombatManager.getActorCombatant(combatant, { warning: false }) + if (actor) { + let actions = RdDCombatManager.listActionsActorCombatant(actor) + // initiative + await RdDTokenHud.addExtensionHudInit(html, combatant, actions) + // combat + await RdDTokenHud.addExtensionHudCombat(html, combatant, token, actions) } - let actions = RdDCombatManager.listActionsCombatant(combatant); - // initiative - await RdDTokenHud.addExtensionHudInit(html, combatant, actions); - // combat - await RdDTokenHud.addExtensionHudCombat(html, combatant, token, actions); }