import { ChatUtility } from "./chat-utility.js"; import { HIDE_DICE, SYSTEM_RDD, SYSTEM_SOCKET_ID } from "./constants.js"; import { RdDItemArme } from "./item-arme.js"; import { RdDItemCompetence } from "./item-competence.js"; import { RdDItemCompetenceCreature } from "./item-competencecreature.js"; import { Misc } from "./misc.js"; import { RdDBonus } from "./rdd-bonus.js"; import { RdDResolutionTable } from "./rdd-resolution-table.js"; import { RdDRoll } from "./rdd-roll.js"; import { RdDRollTables } from "./rdd-rolltables.js"; import { ReglesOptionelles } from "./regles-optionelles.js"; /* -------------------------------------------- */ const premierRoundInit = [ { pattern: 'hast', init: 5.90 }, { pattern: 'lance', init: 5.85 }, { pattern: 'baton', init: 5.80 }, { pattern: 'doubledragonne', init: 5.75 }, { pattern: 'esparlongue', init: 5.70 }, { pattern: 'epeedragonne', init: 5.65 }, { pattern: 'epeebatarde', init: 5.60 }, { pattern: 'epeecyane', init: 5.55 }, { pattern: 'epeesorde', init: 5.50 }, { pattern: 'grandehache', init: 5.45 }, { pattern: 'bataille', init: 5.40 }, { pattern: 'epeegnome', init: 5.35 }, { pattern: 'masse', init: 5.30 }, { pattern: 'gourdin', init: 5.25 }, { pattern: 'fléau', init: 5.20 }, { pattern: 'dague', init: 5.15 }, { pattern: 'autre', init: 5.10 }, ]; /* -------------------------------------------- */ export class RdDCombatManager extends Combat { static init() { /* -------------------------------------------- */ Hooks.on("getCombatTrackerEntryContext", (html, options) => { RdDCombatManager.pushInitiativeOptions(html, options); }); Hooks.on("preDeleteCombat", (combat, html, id) => { combat.onPreDeleteCombat() }); } /* -------------------------------------------- */ cleanItemUse() { for (let turn of this.turns) { turn.actor.resetItemUse() } } /* -------------------------------------------- */ async nextRound() { this.cleanItemUse(); await this.finDeRound(); return await super.nextRound(); } /* -------------------------------------------- */ async onPreDeleteCombat() { await this.finDeRound({ terminer: true }); } /* -------------------------------------------- */ async finDeRound(options = { terminer: false }) { for (let combatant of this.data.combatants) { if (combatant.actor) { await combatant.actor.finDeRound(options); } else { ui.notifications.warn(`Le combatant ${combatant.name} n'est pas associé à un acteur!`) } } } /************************************************************************************/ async rollInitiative(ids, formula = undefined, 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 combatant = this.combatants.get(ids[cId]); //if (!c) return results; let rollFormula = formula; // Init per default if (!rollFormula) { let armeCombat, competence; if (combatant.actor.data.type == 'creature' || combatant.actor.data.type == 'entite') { for (const competenceItemData of combatant.actor.data.items) { if (competenceItemData.data.data.iscombat) { competence = duplicate(competenceItemData); } } rollFormula = "2+( (" + RdDCombatManager.calculInitiative(competence.data.niveau, competence.data.carac_value) + ")/100)"; } else { for (const itemData of combatant.actor.data.items) { if (itemData.type == "arme" && itemData.data.equipe) { armeCombat = duplicate(itemData); } } let compName = (armeCombat == undefined) ? "Corps à corps" : armeCombat.data.competence; competence = RdDItemCompetence.findCompetence(combatant.actor.data.items, compName); let bonusEcaille = (armeCombat && armeCombat.data.magique) ? armeCombat.data.ecaille_efficacite : 0; rollFormula = "2+( (" + RdDCombatManager.calculInitiative(competence.data.niveau, Misc.data(combatant.actor).data.carac[competence.data.defaut_carac].value, bonusEcaille) + ")/100)"; } } //console.log("Combatat", c); const roll = combatant.getInitiativeRoll(rollFormula); if ( !roll.total) { roll.evaluate( {async: false}); } if (roll.total <= 0) 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: roll.total }]); // Send a chat message let rollMode = messageOptions.rollMode || game.settings.get("core", "rollMode"); let messageData = mergeObject( { speaker: { scene: canvas.scene._id, actor: combatant.actor ? combatant.actor._id : null, token: combatant.token._id, alias: combatant.token.name, sound: CONFIG.sounds.dice, }, flavor: `${combatant.token.name} a fait son jet d'Initiative (${messageOptions.initInfo})
`, }, messageOptions ); roll.toMessage(messageData, { rollMode, create: true }); RdDCombatManager.processPremierRoundInit(); } return this; }; /* -------------------------------------------- */ static calculInitiative(niveau, caracValue, bonusEcaille = 0) { let base = niveau + Math.floor(caracValue / 2); base += bonusEcaille; return "1d6" + (base >= 0 ? "+" : "") + base; } /* -------------------------------------------- */ /** Retourne une liste triée d'armes avec le split arme1 main / arme 2 main */ static finalizeArmeList(armes, competences, carac) { // Gestion des armes 1/2 mains let armesEquipe = []; for (const arme of armes) { let armeData = duplicate(Misc.data(arme)); if (armeData.data.equipe) { let compData = competences.map(c => Misc.data(c)).find(c => c.name == armeData.data.competence); armesEquipe.push(armeData); armeData.data.dommagesReels = Number(armeData.data.dommages); armeData.data.niveau = compData.data.niveau; armeData.data.initiative = RdDCombatManager.calculInitiative(compData.data.niveau, carac[compData.data.defaut_carac].value); // Dupliquer les armes pouvant être à 1 main et 2 mains en patchant la compétence if (armeData.data.unemain && !armeData.data.deuxmains) { armeData.data.mainInfo = "(1m)"; } else if (!armeData.data.unemain && armeData.data.deuxmains) { armeData.data.mainInfo = "(2m)"; } else if (armeData.data.unemain && armeData.data.deuxmains) { armeData.data.mainInfo = "(1m)"; const comp2m = armeData.data.competence.replace(" 1 main", " 2 mains"); // Replace ! const comp = Misc.data(competences.find(c => c.name == comp2m)); const arme2main = duplicate(armeData); arme2main.data.mainInfo = "(2m)"; arme2main.data.niveau = comp.data.niveau; arme2main.data.competence = comp2m; arme2main.data.initiative = RdDCombatManager.calculInitiative(arme2main.data.niveau, carac[comp.data.defaut_carac].value); armesEquipe.push(arme2main); const containsSlash = armeData.data.dommages.includes("/"); if (containsSlash) { const tableauDegats = armeData.data.dommages.split("/"); armeData.data.dommagesReels = Number(tableauDegats[0]); arme2main.data.dommagesReels = Number(tableauDegats[1]); } else{ ui.notifications.info("Les dommages de l'arme à 1/2 mains " + armeData.name + " ne sont pas corrects (ie sous la forme X/Y)"); } } } } return armesEquipe.sort(Misc.ascending(armeData => armeData.name + (armeData.data.mainInfo ?? ''))); } /* -------------------------------------------- */ static buildListeActionsCombat(combatant) { if (combatant.actor == undefined) { ui.notifications.warn(`Le combatant ${combatant.name} n'est pas associé à un acteur, impossible de déterminer ses actions de combat!`) return []; } const actorData = Misc.data(combatant.actor); let items = combatant.actor.data.items; let actions = [] if (combatant.actor.isCreature()) { actions = actions.concat(items.filter(it => RdDItemCompetenceCreature.isCompetenceAttaque(it)) .map(competence => RdDItemCompetenceCreature.toArme(competence))); } else { // Recupération des items 'arme' let armes = items.filter(it => RdDItemArme.isArmeUtilisable(it)) //.concat(RdDItemArme.empoignade()) .concat(RdDItemArme.mainsNues()); let competences = items.filter(it => it.type == 'competence'); actions = actions.concat(RdDCombatManager.finalizeArmeList(armes, competences, actorData.data.carac)); if (actorData.data.attributs.hautrevant.value) { actions.push({ name: "Draconic", data: { initOnly: true, competence: "Draconic" } }); } } actions.push({ name: "Autre action", data: { initOnly: true, competence: "Autre action" } }); for (let index = 0; index < actions.length; index++) { actions[index].index = index; } return actions; } /* -------------------------------------------- */ static processPremierRoundInit() { // Check if we have the whole init ! if (Misc.isUniqueConnectedGM() && game.combat.current.round == 1) { let initMissing = game.combat.data.combatants.find(it => !it.initiative); if (!initMissing) { // Premier round ! for (let combatant of game.combat.data.combatants) { let arme = combatant.initiativeData?.arme; //console.log("Parsed !!!", combatant, initDone, game.combat.current, arme); if (arme && arme.type == "arme") { for (let initData of premierRoundInit) { if (arme.data.initpremierround.toLowerCase().includes(initData.pattern)) { let msg = `

L'initiative de ${combatant.actor.name} a été modifiée !


Etant donné son ${arme.name}, son initative pour ce premier round est désormais de ${initData.init}.
` ChatMessage.create({ content: msg }); game.combat.setInitiative(combatant._id, initData.init); } } } } } } } /* -------------------------------------------- */ static incDecInit(combatantId, incDecValue) { const combatant = game.combat.combatants.get(combatantId); let initValue = combatant.initiative + incDecValue; game.combat.setInitiative(combatantId, initValue); } /* -------------------------------------------- */ static pushInitiativeOptions(html, options) { for (let i = 0; i < options.length; i++) { let option = options[i]; if (option.name == 'COMBAT.CombatantReroll') { // Replace ! option.name = "Sélectionner l'initiative..."; option.condition = true; option.icon = ''; option.callback = target => { RdDCombatManager.displayInitiativeMenu(html, target.data('combatant-id')); } } } options = [ { name: "Incrémenter initiative", condition: true, icon: '', callback: target => { RdDCombatManager.incDecInit(target.data('combatant-id'), +0.01); } }, { name: "Décrémenter initiative", condition: true, icon: '', callback: target => { RdDCombatManager.incDecInit(target.data('combatant-id'), -0.01); } } ].concat(options); } /* -------------------------------------------- */ static rollInitiativeCompetence(combatantId, arme) { const combatant = game.combat.combatants.get(combatantId); if (combatant.actor == undefined) { ui.notifications.warn(`Le combatant ${combatant.name} n'est pas associé à un acteur, impossible de déterminer ses actions de combat!`) return []; } let initInfo = ""; let initOffset = 0; let caracForInit = 0; let compNiveau = 0; let compData = { name: "Aucune" }; if (combatant.actor.getSurprise() == "totale") { initOffset = -1; // To force 0 initInfo = "Surprise Totale" } else if (combatant.actor.getSurprise() == "demi") { initOffset = 0; initInfo = "Demi Surprise" } else if (arme.name == "Autre action") { initOffset = 2; initInfo = "Autre Action" } else if (arme.name == "Draconic") { initOffset = 9; initInfo = "Draconic" } else { compData = Misc.data(RdDItemCompetence.findCompetence(combatant.actor.data.items, arme.data.competence)); compNiveau = compData.data.niveau; initInfo = arme.name + " / " + arme.data.competence; if (combatant.actor.data.type == 'creature' || combatant.actor.data.type == 'entite') { caracForInit = compData.data.carac_value; if (compData.data.categorie == "lancer") { initOffset = 7; } else { initOffset = 5; } } else { caracForInit = Misc.data(combatant.actor).data.carac[compData.data.defaut_carac].value; initOffset = RdDCombatManager._baseInitOffset(compData.data.categorie, arme); } } let malus = combatant.actor.getEtatGeneral(); // Prise en compte état général // Cas des créatures et entités vs personnages let rollFormula = initOffset + "+ ( (" + RdDCombatManager.calculInitiative(compNiveau, caracForInit) + " + " + malus + ") /100)"; // Garder la trace de l'arme/compétence utilisée pour l'iniative combatant.initiativeData = { arme: arme } // pour reclasser l'init au round 0 game.combat.rollInitiative(combatantId, rollFormula, { initInfo: initInfo }); } static _baseInitOffset(categorie, arme) { if (categorie == "tir") { // Offset de principe pour les armes de jet return 8; } if (categorie == "lancer") { // Offset de principe pour les armes de jet return 7; } // Offset de principe pour les armes de jet switch (arme.data.cac) { case "empoignade": return 3; case "pugilat": return 4; } return 5; } /* -------------------------------------------- */ static displayInitiativeMenu(html, combatantId) { console.log("Combatant ; ", combatantId); const combatant = game.combat.combatants.get(combatantId); let armesList = RdDCombatManager.buildListeActionsCombat(combatant); // Build the relevant submenu if (armesList) { let menuItems = []; for (let arme of armesList) { menuItems.push({ name: arme.data.competence, icon: "", callback: target => { RdDCombatManager.rollInitiativeCompetence(combatantId, arme) } }); } new ContextMenu(html, ".directory-list", menuItems).render(); } } } /* -------------------------------------------- */ export class RdDCombat { static init() { Hooks.on("updateCombat", (combat, change, options, userId) => { RdDCombat.onUpdateCombat(combat, change, options, userId) }); Hooks.on("preDeleteCombat", (combat, options, userId) => { RdDCombat.onPreDeleteCombat(combat, options, userId); }); } /* -------------------------------------------- */ static onSocketMessage(sockmsg) { switch (sockmsg.msg) { case "msg_encaisser": return RdDCombat.onMsgEncaisser(sockmsg.data); case "msg_defense": return RdDCombat.onMsgDefense(sockmsg.data); } } /* -------------------------------------------- */ static onUpdateCombat(combat, change, options, userId) { if (combat.data.round != 0 && combat.turns && combat.data.active) { RdDCombat.combatNouveauTour(combat); } } /* -------------------------------------------- */ static onPreDeleteCombat(combat, options, userId) { if (Misc.isUniqueConnectedGM()) { combat.cleanItemUse(); ChatUtility.removeChatMessageContaining(`
`) game.messages.filter(m => ChatUtility.getMessageData(m, 'attacker-roll') != undefined && ChatUtility.getMessageData(m, 'defender-roll') != undefined) .forEach(it => it.delete()); } } /* -------------------------------------------- */ static combatNouveauTour(combat) { if (Misc.isUniqueConnectedGM()) { let turn = combat.turns.find(t => t.token?.id == combat.current.tokenId); if (turn?.actor) { RdDCombat.displayActorCombatStatus(combat, turn.actor); // TODO Playaudio for player?? } } } /* -------------------------------------------- */ static isActive() { return true; } /* -------------------------------------------- */ static createUsingTarget(attacker) { const target = RdDCombat.getTarget(); if (target == undefined) { ui.notifications.warn((game.user.targets?.size ?? 0) > 1 ? "Vous devez choisir une seule cible à attaquer!" : "Vous devez choisir une cible à attaquer!"); } else { const defender = target?.actor; const defenderTokenId = target?.data._id; if ( defender.type == 'entite' && defender.data.data.definition.typeentite == 'nonincarne') { ui.notifications.warn("Vous ne pouvez pas cibler une entité non incarnée !!!!"); } else { return this.create(attacker, defender, defenderTokenId, target) } } } /* -------------------------------------------- */ static getTarget() { if (game.user.targets && game.user.targets.size == 1) { for (let target of game.user.targets) { return target; } } return undefined; } /* -------------------------------------------- */ static create(attacker, defender, defenderTokenId, target = undefined) { return new RdDCombat(attacker, defender, defenderTokenId, target) } /* -------------------------------------------- */ static createForAttackerAndDefender(attackerId, defenderTokenId) { const attacker = game.actors.get(attackerId); if (defenderTokenId) { const defenderToken = canvas.tokens.get(defenderTokenId); const defender = defenderToken.actor; return RdDCombat.create(attacker, defender, defenderTokenId); } return RdDCombat.createUsingTarget(attacker) } /* -------------------------------------------- */ static onMsgEncaisser(data) { let defender = canvas.tokens.get(data.defenderTokenId).actor; if (Misc.isOwnerPlayerOrUniqueConnectedGM()) { let attackerRoll = data.attackerRoll; let attacker = data.attackerId ? game.actors.get(data.attackerId) : null; defender.encaisserDommages(attackerRoll, attacker); RdDCombat._deleteDefense(attackerRoll.passeArme); RdDCombat._deleteAttaque(data.attackerId); } } /* -------------------------------------------- */ static onMsgDefense(msg) { let defenderToken = canvas.tokens.get(msg.defenderTokenId); if (defenderToken && Misc.isUniqueConnectedGM()) { const rddCombat = RdDCombat.createForAttackerAndDefender(msg.attackerId, msg.defenderTokenId); if (rddCombat) { const defenderRoll = msg.defenderRoll; rddCombat.removeChatMessageActionsPasseArme(defenderRoll.passeArme); rddCombat._chatMessageDefense(msg.paramChatDefense, msg.defenderRoll); } } } /* -------------------------------------------- */ static _callJetDeVie(event) { let actorId = event.currentTarget.attributes['data-actorId'].value; let actor = game.actors.get(actorId); actor.jetVie(); } /* -------------------------------------------- */ static registerChatCallbacks(html) { for (let button of [ '#parer-button', '#esquiver-button', '#particuliere-attaque', '#encaisser-button', '#appel-chance-defense', '#appel-destinee-defense', '#appel-chance-attaque', '#appel-destinee-attaque', '#echec-total-attaque', ]) { html.on("click", button, event => { const rddCombat = RdDCombat.createForAttackerAndDefender( event.currentTarget.attributes['data-attackerId']?.value, event.currentTarget.attributes['data-defenderTokenId']?.value); if (rddCombat) { rddCombat.onEvent(button, event); event.preventDefault(); } }); } html.on("click", '#chat-jet-vie', event => { event.preventDefault(); RdDCombat._callJetDeVie(event); }); } /* -------------------------------------------- */ constructor(attacker, defender, defenderTokenId, target) { this.attacker = attacker; this.defender = defender; this.target = target; this.attackerId = this.attacker.data._id; this.defenderId = this.defender.data._id; this.defenderTokenId = defenderTokenId; } /* -------------------------------------------- */ async onEvent(button, event) { const chatMessage = ChatUtility.getChatMessage(event); const defenderRoll = ChatUtility.getMessageData(chatMessage, 'defender-roll'); const attackerRoll = defenderRoll?.attackerRoll ?? ChatUtility.getMessageData(chatMessage, 'attacker-roll') ; console.log('RdDCombat', attackerRoll, defenderRoll); const defenderTokenId = event.currentTarget.attributes['data-defenderTokenId']?.value; const armeParadeId = event.currentTarget.attributes['data-armeid']?.value; const competence = event.currentTarget.attributes['data-competence']?.value; const compId = event.currentTarget.attributes['data-compid']?.value; switch (button) { case '#particuliere-attaque': return await this.choixParticuliere(attackerRoll, event.currentTarget.attributes['data-mode'].value); case '#parer-button': return this.parade(attackerRoll, armeParadeId); case '#esquiver-button': return this.esquive(attackerRoll, compId, competence); case '#encaisser-button': return this.encaisser(attackerRoll, defenderRoll, defenderTokenId); case '#echec-total-attaque': return this._onEchecTotal(attackerRoll); case '#appel-chance-attaque': return this.attacker.rollAppelChance( () => this.attaqueChanceuse(attackerRoll), () => this._onEchecTotal(attackerRoll)); case '#appel-chance-defense': return this.defender.rollAppelChance( () => this.defenseChanceuse(attackerRoll, defenderRoll), () => this.afficherOptionsDefense(attackerRoll, defenderRoll, { defenseChance: true })); case '#appel-destinee-attaque': return this.attacker.appelDestinee( () => this.attaqueSignificative(attackerRoll), () => { }); case '#appel-destinee-defense': return this.defender.appelDestinee( () => this.defenseDestinee(defenderRoll), () => { }); } } /* -------------------------------------------- */ attaqueChanceuse(attackerRoll) { ui.notifications.info("L'attaque est rejouée grâce à la chance") attackerRoll.essais.attaqueChance = true; this.attaque(attackerRoll, attackerRoll.arme); } /* -------------------------------------------- */ attaqueDestinee(attackerRoll) { ui.notifications.info('Attaque significative grâce à la destinée') RdDResolutionTable.significativeRequise(attackerRoll.rolled); this.removeChatMessageActionsPasseArme(attackerRoll.passeArme); this._onAttaqueNormale(attackerRoll); } /* -------------------------------------------- */ defenseChanceuse(attackerRoll, defenderRoll) { ui.notifications.info("La défense est rejouée grâce à la chance") attackerRoll.essais.defenseChance = true; attackerRoll.essais.defense = false; this.removeChatMessageActionsPasseArme(attackerRoll.passeArme); this._sendMessageDefense(attackerRoll, defenderRoll, attackerRoll.essais); } /* -------------------------------------------- */ defenseDestinee(defenderRoll) { if (defenderRoll) { ui.notifications.info('Défense significative grâce à la destinée') RdDResolutionTable.significativeRequise(defenderRoll.rolled); this.removeChatMessageActionsPasseArme(defenderRoll.passeArme); if (defenderRoll.arme) { this._onParadeNormale(defenderRoll); } else { this._onEsquiveNormale(defenderRoll); } } else { ui.notifications.warn("Appel à la destinée impossible, la passe d'armes est déjà terminée!") } } /* -------------------------------------------- */ afficherOptionsDefense(attackerRoll, defenderRoll, essais) { ui.notifications.info("La chance n'est pas avec vous"); this._sendMessageDefense(attackerRoll, defenderRoll, essais); } /* -------------------------------------------- */ removeChatMessageActionsPasseArme(passeArme) { if (game.settings.get(SYSTEM_RDD, "supprimer-dialogues-combat-chat")) { ChatUtility.removeChatMessageContaining(`
`); } } /* -------------------------------------------- */ static isEchec(rollData) { switch (rollData.ajustements.surprise.used) { case 'totale': return true; case 'demi': return !rollData.rolled.isSign; } return rollData.rolled.isEchec; } /* -------------------------------------------- */ static isEchecTotal(rollData) { if (!rollData.attackerRoll && rollData.ajustements.surprise.used) { return rollData.rolled.isEchec && rollData.rolled.code != 'notSign'; } return rollData.rolled.isETotal; } /* -------------------------------------------- */ static isParticuliere(rollData) { if (!rollData.attackerRoll && rollData.ajustements.surprise.used) { return false; } return rollData.rolled.isPart; } /* -------------------------------------------- */ static isReussite(rollData) { switch (rollData.ajustements.surprise.used) { case 'totale': return false; case 'demi': return rollData.rolled.isSign; } return rollData.rolled.isSuccess; } /* -------------------------------------------- */ async attaque(competence, arme) { if (!await this.accorderEntite('avant-attaque')) { return; } if (arme.data.cac == 'empoignade' && this.attacker.isCombatTouche()) { ChatMessage.create({ alias: this.attacker.name, whisper: ChatUtility.getWhisperRecipientsAndGMs(this.attacker.name), content: await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/chat-actor-perte-empoignade.html', { attacker: this.attacker, competence: competence }) }); return; } let rollData = this._prepareAttaque(competence, arme); console.log("RdDCombat.attaque >>>", rollData); if (arme) { this.attacker.verifierForceMin(arme); } const dialog = await RdDRoll.create(this.attacker, rollData, { html: 'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-competence.html', options: { height: 540 } }, { name: 'jet-attaque', label: 'Attaque: ' + (arme?.name ?? competence.name), callbacks: [ this.attacker.createCallbackExperience(), this.attacker.createCallbackAppelAuMoral(), { action: r => this.removeChatMessageActionsPasseArme(r.passeArme) }, { condition: r => arme && !RdDCombat.isParticuliere(r), action: r => this.attacker.incDecItemUse(arme._id) }, { condition: r => (RdDCombat.isReussite(r) && !RdDCombat.isParticuliere(r)), action: r => this._onAttaqueNormale(r) }, { condition: RdDCombat.isParticuliere, action: r => this._onAttaqueParticuliere(r) }, { condition: RdDCombat.isEchec, action: r => this._onAttaqueEchec(r) }, { condition: RdDCombat.isEchecTotal, action: r => this._onAttaqueEchecTotal(r) }, ] }); dialog.render(true); } /* -------------------------------------------- */ _prepareAttaque(competence, arme) { let rollData = { passeArme: randomID(16), mortalite: arme?.data.mortalite, coupsNonMortels: false, competence: competence, surprise: this.attacker.getSurprise(true), surpriseDefenseur: this.defender.getSurprise(true), essais: {} }; if (this.attacker.isCreature()) { RdDItemCompetenceCreature.setRollDataCreature(rollData); } else if (arme) { // Usual competence rollData.arme = RdDItemArme.armeUneOuDeuxMains(arme, RdDItemCompetence.isArmeUneMain(competence)); } else { // sans armes: à mains nues const niveau = competence.data.niveau; const init = RdDCombatManager.calculInitiative(niveau, Misc.templateData(this.attacker).carac['melee'].value); rollData.arme = RdDItemArme.mainsNues({ niveau: niveau, initiative: init }); } return rollData; } /* -------------------------------------------- */ async _onAttaqueParticuliere(rollData) { const isMeleeDiffNegative = (rollData.competence.type == 'competencecreature' || rollData.selectedCarac.label == "Mêlée") && rollData.diffLibre < 0; // force toujours, sauf empoignade // finesse seulement en mélée, pour l'empoignade, ou si la difficulté libre est de -1 minimum // rapidité seulement en mêlée, si l'arme le permet, et si la difficulté libre est de -1 minimum const isForce = !rollData.arme.data.empoignade; const isFinesse = rollData.arme.data.empoignade || isMeleeDiffNegative; const isRapide = !rollData.arme.data.empoignade && isMeleeDiffNegative && rollData.arme.data.rapide; // si un seul choix possible, le prendre if (isForce && !isFinesse && !isRapide) { return await this.choixParticuliere(rollData, "force"); } else if (!isForce && isFinesse && !isRapide) { return await this.choixParticuliere(rollData, "finesse"); } else if (!isForce && !isFinesse && isRapide) { return await this.choixParticuliere(rollData, "rapidite"); } const choixParticuliere = await ChatMessage.create({ alias: this.attacker.name, whisper: ChatUtility.getWhisperRecipientsAndGMs(this.attacker.name), content: await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/chat-demande-attaque-particuliere.html', { alias: this.attacker.name, attackerId: this.attackerId, defenderTokenId: this.defenderTokenId, isForce: isForce, isFinesse: isFinesse, isRapide: isRapide, passeArme: rollData.passeArme }) }); ChatUtility.setMessageData(choixParticuliere, 'attacker-roll', rollData); } /* -------------------------------------------- */ async _onAttaqueNormale(attackerRoll) { console.log("RdDCombat.onAttaqueNormale >>>", attackerRoll); attackerRoll.dmg = RdDBonus.dmg(attackerRoll, this.attacker.getBonusDegat(), this.defender.isEntiteCauchemar()); let defenderRoll = { attackerRoll: attackerRoll, passeArme: attackerRoll.passeArme, show: {} } attackerRoll.show = { cible: this.target ? this.defender.data.name : 'la cible', isRecul: (attackerRoll.particuliere == 'force' || attackerRoll.tactique == 'charge') } await RdDResolutionTable.displayRollData(attackerRoll, this.attacker, 'chat-resultat-attaque.html'); if (!await this.accorderEntite('avant-defense')) { return; } if (this.target) { await this._sendMessageDefense(attackerRoll, defenderRoll); } } /* -------------------------------------------- */ isPossession( attackerRoll) { return attackerRoll.selectedCarac.label.toLowerCase() == 'possession'; } /* -------------------------------------------- */ async _sendMessageDefense(attackerRoll, defenderRoll, essaisPrecedents = undefined) { console.log("RdDCombat._sendMessageDefense", attackerRoll, defenderRoll, essaisPrecedents, " / ", this.attacker, this.target, this.attackerId, attackerRoll.competence.data.categorie); this.removeChatMessageActionsPasseArme(attackerRoll.passeArme); if (essaisPrecedents) { mergeObject(attackerRoll.essais, essaisPrecedents, { overwrite: true }); } // # utilisation esquive const corpsACorps = Misc.data(this.defender.getCompetence("Corps à corps", { onMessage: it => console.info(it, this.defender) })); const esquives = duplicate(this.defender.getCompetences("esquive", { onMessage: it => console.info(it, this.defender) }).map(c => Misc.data(c))); esquives.forEach(e => e.usages = e?.id ? this.defender.getItemUse(e.id) : 0); const paramChatDefense = { passeArme: attackerRoll.passeArme, essais: attackerRoll.essais, isPossession: this.isPossession(attackerRoll), defender: Misc.data(this.defender), attacker: Misc.data(this.attacker), attackerId: this.attackerId, esquives: esquives, defenderTokenId: this.defenderTokenId, mainsNues: attackerRoll.dmg.mortalite != 'mortel' && corpsACorps, armes: this._filterArmesParade(this.defender, attackerRoll.competence, attackerRoll.arme), diffLibre: attackerRoll.ajustements?.diffLibre?.value ?? 0, attaqueParticuliere: attackerRoll.particuliere, attaqueCategorie: attackerRoll.competence.data.categorie, attaqueArme: attackerRoll.arme, surprise: this.defender.getSurprise(true), dmg: attackerRoll.dmg, }; if (Misc.isUniqueConnectedGM()) { await this._chatMessageDefense(paramChatDefense, defenderRoll); } else { this._socketSendMessageDefense(paramChatDefense, defenderRoll); } } /* -------------------------------------------- */ async _chatMessageDefense(paramDemandeDefense, defenderRoll) { const choixDefense = await ChatMessage.create({ // message privé: du défenseur à lui même (et aux GMs) speaker: ChatMessage.getSpeaker(this.defender, canvas.tokens.get(this.defenderTokenId)), alias: this.attacker.name, whisper: ChatUtility.getWhisperRecipientsAndGMs(this.defender.name), content: await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/chat-demande-defense.html', paramDemandeDefense), }); // flag pour garder les jets d'attaque/defense ChatUtility.setMessageData(choixDefense, 'defender-roll', defenderRoll); } /* -------------------------------------------- */ _socketSendMessageDefense(paramChatDefense, defenderRoll) { // envoyer le message au destinataire game.socket.emit(SYSTEM_SOCKET_ID, { msg: "msg_defense", data: { attackerId: this.attacker?.data._id, defenderId: this.defender?.data._id, defenderTokenId: this.defenderTokenId, defenderRoll: defenderRoll, paramChatDefense: paramChatDefense, rollMode: true } }); } /* -------------------------------------------- */ _filterArmesParade(defender, competence) { let items = defender.data.items; items = items.filter(it => RdDItemArme.isArmeUtilisable(it) || RdDItemCompetenceCreature.isCompetenceParade(it)) .map(Misc.data); for (let item of items) { item.data.nbUsage = defender.getItemUse(item._id); // Ajout du # d'utilisation ce round } switch (competence.data.categorie) { case 'tir': case 'lancer': return items.filter(item => RdDItemArme.getCategorieParade(item) == 'boucliers') default: // Le fléau ne peut être paré qu’au bouclier p115 if (competence.name == "Fléau") { return items.filter(item => RdDItemArme.getCategorieParade(item) == 'boucliers') } return items.filter(item => RdDItemArme.getCategorieParade(item)); } } /* -------------------------------------------- */ async _onAttaqueEchecTotal(attackerRoll) { const choixEchecTotal = await ChatMessage.create({ whisper: ChatUtility.getWhisperRecipientsAndGMs(this.attacker.name), content: await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/chat-demande-attaque-etotal.html', { attackerId: this.attackerId, attacker: Misc.data(this.attacker), defenderTokenId: this.defenderTokenId, essais: attackerRoll.essais }) }); ChatUtility.setMessageData(choixEchecTotal, 'attacker-roll', attackerRoll); } /* -------------------------------------------- */ async _onEchecTotal(rollData) { console.log("RdDCombat._onEchecTotal >>>", rollData); const arme = rollData.arme; const avecArme = !['', 'sans-armes', 'armes-naturelles'].includes(arme?.data.categorie_parade ?? ''); const action = (rollData.attackerRoll ? (arme ? "la parade" : "l'esquive") : "l'attaque"); ChatUtility.createChatWithRollMode(this.defender.name, { content: `Maladresse à ${action}! ` + await RdDRollTables.getMaladresse({ arme: avecArme }) }); } /* -------------------------------------------- */ async _onAttaqueEchec(rollData) { console.log("RdDCombat.onAttaqueEchec >>>", rollData); await RdDResolutionTable.displayRollData(rollData, this.attacker, 'chat-resultat-attaque.html'); } /* -------------------------------------------- */ async choixParticuliere(rollData, choix) { console.log("RdDCombat.choixParticuliere >>>", rollData, choix); if (choix != "rapidite") { this.attacker.incDecItemUse(rollData.arme._id); } this.removeChatMessageActionsPasseArme(rollData.passeArme); rollData.particuliere = choix; await this._onAttaqueNormale(rollData); } /* -------------------------------------------- */ async parade(attackerRoll, armeParadeId) { const arme = this.defender.getArmeParade(armeParadeId); console.log("RdDCombat.parade >>>", attackerRoll, armeParadeId, arme); const competence = Misc.templateData(arme)?.competence; if (competence == undefined) { console.error("Pas de compétence de parade associée à ", arme) ; return; } let rollData = this._prepareParade(attackerRoll, arme, competence); const dialog = await RdDRoll.create(this.defender, rollData, { html: 'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-competence.html', options: { height: 540 } }, { name: 'jet-parade', label: 'Parade: ' + (arme ? arme.name : rollData.competence.name), callbacks: [ this.defender.createCallbackExperience(), this.defender.createCallbackAppelAuMoral(), { action: r => this.removeChatMessageActionsPasseArme(r.passeArme) }, { condition: r => !RdDCombat.isParticuliere(r), action: r => this.defender.incDecItemUse(armeParadeId) }, { condition: RdDCombat.isReussite, action: r => this._onParadeNormale(r) }, { condition: RdDCombat.isParticuliere, action: r => this._onParadeParticuliere(r) }, { condition: RdDCombat.isEchec, action: r => this._onParadeEchec(r) }, ] }); dialog.render(true); } /* -------------------------------------------- */ _prepareParade(attackerRoll, armeParade, competenceParade) { let defenderRoll = { passeArme: attackerRoll.passeArme, diffLibre: attackerRoll.diffLibre, attackerRoll: attackerRoll, competence: Misc.data(this.defender.getCompetence(competenceParade)), arme: armeParade, surprise: this.defender.getSurprise(true), needParadeSignificative: ReglesOptionelles.isUsing('categorieParade') && RdDItemArme.needParadeSignificative(attackerRoll.arme, armeParade), needResist: RdDItemArme.needArmeResist(attackerRoll.arme, armeParade), carac: Misc.templateData(this.defender).carac, show: {} }; if (this.defender.isCreature()) { RdDItemCompetenceCreature.setRollDataCreature(defenderRoll); } return defenderRoll; } /* -------------------------------------------- */ _onParadeParticuliere(defenderRoll) { console.log("RdDCombat._onParadeParticuliere >>>", defenderRoll); if (!defenderRoll.attackerRoll.isPart) { // TODO: attaquant doit jouer résistance et peut être désarmé p132 ChatUtility.createChatWithRollMode(this.defender.name, { content: `(à gérer) L'attaquant doit jouer résistance et peut être désarmé (p132)` }); } } /* -------------------------------------------- */ async _onParadeNormale(defenderRoll) { console.log("RdDCombat._onParadeNormale >>>", defenderRoll); await this.computeRecul(defenderRoll); await this.computeDeteriorationArme(defenderRoll); await RdDResolutionTable.displayRollData(defenderRoll, this.defender, 'chat-resultat-parade.html'); RdDCombat._deleteDefense(defenderRoll.passeArme); } /* -------------------------------------------- */ async _onParadeEchec(defenderRoll) { console.log("RdDCombat._onParadeEchec >>>", defenderRoll); await RdDResolutionTable.displayRollData(defenderRoll, this.defender, 'chat-resultat-parade.html'); this.removeChatMessageActionsPasseArme(defenderRoll.passeArme); this._sendMessageDefense(defenderRoll.attackerRoll, defenderRoll, { defense: true }); } /* -------------------------------------------- */ async esquive(attackerRoll, compId, compName) { const esquive = Misc.data(this.defender.getCompetence(compId) ?? this.defender.getCompetence(compName)); if (esquive == undefined) { ui.notifications.error(this.defender.name + " n'a pas de compétence " + compName); return; } console.log("RdDCombat.esquive >>>", attackerRoll, esquive); let rollData = this._prepareEsquive(attackerRoll, esquive); const dialog = await RdDRoll.create(this.defender, rollData, { html: 'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-competence.html' }, { name: 'jet-esquive', label: 'Esquiver', callbacks: [ this.defender.createCallbackExperience(), this.defender.createCallbackAppelAuMoral(), { condition: r => !RdDCombat.isParticuliere(r), action: r => this.defender.incDecItemUse(esquive._id) }, { action: r => this.removeChatMessageActionsPasseArme(r.passeArme) }, { condition: RdDCombat.isReussite, action: r => this._onEsquiveNormale(r) }, { condition: RdDCombat.isParticuliere, action: r => this._onEsquiveParticuliere(r) }, { condition: RdDCombat.isEchec, action: r => this._onEsquiveEchec(r) }, ] }); dialog.render(true); } /* -------------------------------------------- */ _prepareEsquive(attackerRoll, competence) { let rollData = { passeArme: attackerRoll.passeArme, diffLibre: attackerRoll.diffLibre, attackerRoll: attackerRoll, competence: competence, surprise: this.defender.getSurprise(true), surpriseDefenseur: this.defender.getSurprise(true), carac: Misc.templateData(this.defender).carac, show: {} }; if (this.defender.isCreature()) { RdDItemCompetenceCreature.setRollDataCreature(rollData); } return rollData; } /* -------------------------------------------- */ _onEsquiveParticuliere(rollData) { console.log("RdDCombat._onEsquiveParticuliere >>>", rollData); ChatUtility.createChatWithRollMode(this.defender.name, { content: "Vous pouvez esquiver une deuxième fois!" }); } /* -------------------------------------------- */ async _onEsquiveNormale(defenderRoll) { console.log("RdDCombat._onEsquiveNormal >>>", defenderRoll); await RdDResolutionTable.displayRollData(defenderRoll, this.defender, 'chat-resultat-esquive.html'); RdDCombat._deleteDefense(defenderRoll.passeArme); } /* -------------------------------------------- */ async _onEsquiveEchec(defenderRoll) { console.log("RdDCombat._onEsquiveEchec >>>", defenderRoll); await RdDResolutionTable.displayRollData(defenderRoll, this.defender, 'chat-resultat-esquive.html'); this.removeChatMessageActionsPasseArme(defenderRoll.passeArme); this._sendMessageDefense(defenderRoll.attackerRoll, defenderRoll, { defense: true }) } /* -------------------------------------------- */ async computeDeteriorationArme(defenderRoll) { if (!ReglesOptionelles.isUsing('resistanceArmeParade')) { return; } const attackerRoll = defenderRoll.attackerRoll; // Est-ce une parade normale? if (defenderRoll.arme && attackerRoll && !defenderRoll.rolled.isPart) { // Est-ce que l'attaque est une particulière en force ou une charge if (defenderRoll.needResist || this._isForceOuCharge(attackerRoll)) { defenderRoll.show = defenderRoll.show || {} const dmg = attackerRoll.dmg.dmgArme + attackerRoll.dmg.dmgActor; let arme = defenderRoll.arme; let resistance = Misc.toInt(arme.data.resistance); if (arme.data.magique) { defenderRoll.show.deteriorationArme = 'resiste'; // Par défaut if (arme.data.resistance_magique == undefined) arme.data.resistance_magique = 0; // Quick fix if (dmg > arme.data.resistance_magique) { // Jet uniquement si dommages supérieur à résistance magique (cf. 274) // Jet de résistance de l'arme de parade (p.132) let resistRoll = await RdDResolutionTable.rollData({ caracValue: resistance, finalLevel: - dmg, showDice: HIDE_DICE }); if (!resistRoll.rolled.isSuccess) { let perteResistance = (dmg - arme.data.resistance_magique) resistance -= perteResistance; defenderRoll.show.deteriorationArme = resistance <= 0 ? 'brise' : 'perte'; defenderRoll.show.perteResistance = perteResistance; this.defender.updateEmbeddedDocuments('Item', [{ _id: defenderRoll.arme._id, 'data.resistance': resistance }]); } } } else { // Jet de résistance de l'arme de parade (p.132) let resistRoll = await RdDResolutionTable.rollData({ caracValue: resistance, finalLevel: - dmg, showDice: HIDE_DICE }); if (resistRoll.rolled.isSuccess) { // Perte de résistance defenderRoll.show.deteriorationArme = 'resiste'; } else { resistance -= dmg; defenderRoll.show.deteriorationArme = resistance <= 0 ? 'brise' : 'perte'; defenderRoll.show.perteResistance = dmg; this.defender.updateEmbeddedDocuments('Item', [{ _id: defenderRoll.arme._id, 'data.resistance': resistance }]); } } // Si l'arme de parade n'est pas un bouclier, jet de désarmement (p.132) if (ReglesOptionelles.isUsing('defenseurDesarme') && resistance > 0 && RdDItemArme.getCategorieParade(defenderRoll.arme) != 'boucliers') { let desarme = await RdDResolutionTable.rollData({ caracValue: this.defender.getForce(), finalLevel: Misc.toInt(defenderRoll.competence.data.niveau) - dmg, showDice: HIDE_DICE }); defenderRoll.show.desarme = desarme.rolled.isEchec; } } } } /* -------------------------------------------- */ async computeRecul(defenderRoll) { // Calcul du recul (p. 132) const attackerRoll = defenderRoll.attackerRoll; if (ReglesOptionelles.isUsing('recul') && this._isForceOuCharge(attackerRoll)) { const impact = this._computeImpactRecul(attackerRoll); const rollRecul = await RdDResolutionTable.rollData({ caracValue: 10, finalLevel: impact }); if (rollRecul.rolled.isSuccess) { defenderRoll.show.recul = 'encaisse'; } else if (rollRecul.rolled.isETotal || this._isReculCauseChute(impact)) { defenderRoll.show.recul = 'chute'; await this.defender.setStatusEffect("EFFECT.StatusProne", true); } else { defenderRoll.show.recul = 'recul'; } } } /* -------------------------------------------- */ async _isReculCauseChute(impact) { const agilite = this.defender.getAgilite(); const chute = await RdDResolutionTable.rollData({ caracValue: agilite, finalLevel: impact }); return chute.rolled.isEchec; } /* -------------------------------------------- */ _isForceOuCharge(attaque) { return attaque.particuliere == 'force' || attaque.tactique == 'charge'; } /* -------------------------------------------- */ _computeImpactRecul(attaque) { const taille = this.defender.getTaille(); const force = this.attacker.getForce(); const dommages = attaque.arme.data.dommagesReels ?? attaque.arme.data.dommages; return taille - (force + dommages); } /* -------------------------------------------- */ async encaisser(attackerRoll, defenderRoll, defenderTokenId) { defenderTokenId = defenderTokenId || this.defenderTokenId; console.log("RdDCombat.encaisser >>>", attackerRoll, defenderTokenId); if (defenderRoll?.rolled && RdDCombat.isEchecTotal(defenderRoll)) { this._onEchecTotal(defenderRoll); } if (Misc.isOwnerPlayerOrUniqueConnectedGM(this.defender)) { attackerRoll.attackerId = this.attackerId; attackerRoll.defenderTokenId = defenderTokenId; await this.computeRecul(defenderRoll); this.defender.encaisserDommages(attackerRoll, this.attacker, defenderRoll); } else { // envoi à un GM: les joueurs n'ont pas le droit de modifier les personnages qu'ils ne possèdent pas game.socket.emit(SYSTEM_SOCKET_ID, { msg: "msg_encaisser", data: { attackerId: this.attackerId, defenderTokenId: defenderTokenId, attackerRoll: attackerRoll } }); } RdDCombat._deleteDefense(attackerRoll.passeArme); this.removeChatMessageActionsPasseArme(attackerRoll.passeArme); } /* -------------------------------------------- */ /* retourne true si on peut continuer, false si on ne peut pas continuer */ async accorderEntite(when = 'avant-encaissement') { if (when != game.settings.get(SYSTEM_RDD, "accorder-entite-cauchemar") || this.defender == undefined || !this.defender.isEntiteCauchemar() || this.defender.isEntiteCauchemarAccordee(this.attacker)) { return true; } let rolled = await RdDResolutionTable.roll(this.attacker.getReveActuel(), - Number(Misc.templateData(this.defender).carac.niveau.value)); let message = { content: "Jet de points actuels de rêve à " + rolled.finalLevel + RdDResolutionTable.explain(rolled) + "
", whisper: ChatMessage.getWhisperRecipients(this.attacker.name) }; if (rolled.isSuccess) { await this.defender.setEntiteReveAccordee(this.attacker); message.content += this.attacker.name + " s'est accordé avec " + this.defender.name; } else { message.content += this.attacker.name + " n'est pas accordé avec " + this.defender.name; } ChatMessage.create(message); return rolled.isSuccess; } /* -------------------------------------------- */ static async displayActorCombatStatus(combat, actor) { let data = { combatId: combat._id, alias: actor.name, etatGeneral: actor.getEtatGeneral(), isSonne: actor.getSonne(), blessuresStatus: actor.computeResumeBlessure(), SConst: actor.getSConst(), actorId: actor.data._id, isGrave: false, isCritique: false } if (actor.countBlessuresNonSoigneeByName("critiques") > 0) { // Pour éviter le cumul grave + critique data.isCritique = true; } else if (actor.countBlessuresNonSoigneeByName("graves") > 0) { data.isGrave = true; } ChatUtility.createChatWithRollMode(actor.name, { content: await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/chat-actor-turn-summary.html`, data) }); } }