bol/module/controllers/bol-rolls.js

631 lines
22 KiB
JavaScript
Raw Normal View History

2021-12-25 23:26:27 +01:00
import { BoLUtility } from "../system/bol-utility.js";
2022-01-17 23:50:57 +01:00
const _apt2attr = { init: "mind", melee: "agility", ranged: "agility", def: "vigor" }
2022-01-16 22:53:41 +01:00
2022-03-10 21:05:53 +01:00
/* -------------------------------------------- */
export class BoLRoll {
2022-03-10 21:05:53 +01:00
/* -------------------------------------------- */
2022-01-01 23:32:48 +01:00
static options() {
2022-02-18 21:58:53 +01:00
return { classes: ["bol", "dialog"], width: 480, height: 540 };
2022-01-01 23:32:48 +01:00
}
2022-03-10 21:05:53 +01:00
/* -------------------------------------------- */
2022-01-17 23:50:57 +01:00
static getDefaultAttribute(key) {
2022-01-16 22:53:41 +01:00
return _apt2attr[key]
}
2022-02-18 21:58:53 +01:00
/* -------------------------------------------- */
static attributeCheck(actor, key) {
2022-03-10 21:05:53 +01:00
let attribute = eval(`actor.data.data.attributes.${key}`)
let label = (attribute.label) ? game.i18n.localize(attribute.label) : null
2022-03-21 23:21:05 +01:00
let description = game.i18n.localize('BOL.ui.attributeCheck') + " - " + game.i18n.localize(attribute.label)
2022-03-10 21:05:53 +01:00
let rollData = {
mode: "attribute",
2022-04-08 23:42:01 +02:00
actorId: actor.id,
2022-05-11 22:05:35 +02:00
img: actor.img,
2022-03-10 21:05:53 +01:00
attribute: attribute,
attrValue: attribute.value,
aptValue: 0,
label: label,
careerBonus: 0,
description: description,
armorAgiMalus: actor.getArmorAgiMalus(),
2022-03-27 22:56:43 +02:00
armorInitMalus: actor.getArmorInitMalus(),
2022-03-10 21:05:53 +01:00
mod: 0
}
2022-04-08 23:42:01 +02:00
console.log(">>>>>>>>>>", rollData, actor)
return this.displayRollDialog(rollData)
2022-01-01 23:32:48 +01:00
}
2022-02-18 21:58:53 +01:00
/* -------------------------------------------- */
static aptitudeCheck(actor, key) {
2022-01-16 22:53:41 +01:00
2022-03-21 23:21:05 +01:00
let aptitude = eval(`actor.data.data.aptitudes.${key}`)
2022-01-16 22:53:41 +01:00
let attrKey = this.getDefaultAttribute(key)
2022-03-21 23:21:05 +01:00
let attribute = eval(`actor.data.data.attributes.${attrKey}`)
2022-01-16 22:53:41 +01:00
2022-01-01 23:32:48 +01:00
let label = (aptitude.label) ? game.i18n.localize(aptitude.label) : null;
2022-03-21 23:21:05 +01:00
let description = game.i18n.localize('BOL.ui.aptitudeCheck') + " - " + game.i18n.localize(aptitude.label);
2022-01-16 22:06:49 +01:00
return this.displayRollDialog(
{
2022-01-17 23:50:57 +01:00
mode: "aptitude",
2022-04-08 23:42:01 +02:00
actorId: actor.id,
2022-05-11 22:05:35 +02:00
img: actor.img,
2022-01-16 22:53:41 +01:00
attribute: attribute,
2022-01-16 22:06:49 +01:00
aptitude: aptitude,
2022-02-18 21:58:53 +01:00
attrValue: attribute.value,
aptValue: aptitude.value,
armorAgiMalus: actor.getArmorAgiMalus(),
2022-03-27 22:56:43 +02:00
armorInitMalus: actor.getArmorInitMalus(),
2022-01-16 22:06:49 +01:00
label: label,
2022-02-18 21:58:53 +01:00
careerBonus: 0,
2022-01-16 22:06:49 +01:00
description: description,
mod: 0
2022-03-27 22:56:43 +02:00
})
2021-12-25 23:26:27 +01:00
}
2022-02-18 21:58:53 +01:00
/* -------------------------------------------- */
2022-03-21 23:21:05 +01:00
static weaponCheckWithWeapon(actor, weapon) {
2022-01-01 23:32:48 +01:00
let target = BoLUtility.getTarget()
2022-06-11 10:21:18 +02:00
let weaponData = weapon.data
let attribute = eval(`actor.data.data.attributes.${weaponData.properties.attackAttribute}`)
2022-02-18 21:58:53 +01:00
let aptitude = eval(`actor.data.data.aptitudes.${weaponData.properties.attackAptitude}`)
2022-03-10 21:05:53 +01:00
// Manage specific case
let fightOption = actor.getActiveFightOption()
if (fightOption && fightOption.data.fightoptiontype == "fulldefense") {
2022-03-10 21:05:53 +01:00
ui.notifications.warn(`{{actor.name}} est en Défense Totale ! Il ne peut pas attaquer ce round.`)
return
}
// Build the roll structure
let rolldata = {
2022-01-16 22:06:49 +01:00
mode: "weapon",
2022-04-08 23:42:01 +02:00
actorId: actor.id,
2022-05-11 22:05:35 +02:00
img: actor.img,
2022-01-01 23:32:48 +01:00
weapon: weapon,
2022-02-23 20:39:58 +01:00
isRanged: weaponData.properties.ranged || weaponData.properties.throwing,
2022-04-08 23:42:01 +02:00
targetId: target?.id,
2022-03-10 21:05:53 +01:00
fightOption: fightOption,
2022-02-18 21:58:53 +01:00
careerBonus: 0,
2022-04-08 23:42:01 +02:00
defenderId: target?.data?.actorId,
2022-02-18 21:58:53 +01:00
attribute: attribute,
aptitude: aptitude,
attrValue: attribute.value,
aptValue: aptitude.value,
armorAgiMalus: actor.getArmorAgiMalus(),
2022-03-27 22:56:43 +02:00
armorInitMalus: actor.getArmorInitMalus(),
2022-02-18 21:58:53 +01:00
mod: 0,
2022-02-23 20:39:58 +01:00
modRanged: 0,
2022-01-01 23:32:48 +01:00
label: (weapon.name) ? weapon.name : game.i18n.localize('BOL.ui.noWeaponName'),
2022-03-21 23:21:05 +01:00
description: game.i18n.localize('BOL.ui.weaponAttack') + " : " + weapon.name,
2022-01-01 23:32:48 +01:00
}
2022-03-10 21:05:53 +01:00
return this.displayRollDialog(rolldata)
2022-01-01 23:32:48 +01:00
}
2022-03-21 23:21:05 +01:00
/* -------------------------------------------- */
static weaponCheck(actor, event) {
const li = $(event.currentTarget).parents(".item")
2022-06-11 10:21:18 +02:00
let weapon = actor.items.get(li.data("item-id"))
2022-03-21 23:21:05 +01:00
if (!weapon) {
ui.notifications.warn("Unable to find weapon !")
return
}
2022-06-11 10:21:18 +02:00
weapon = duplicate(weapon)
2022-03-21 23:21:05 +01:00
return this.weaponCheckWithWeapon(actor, weapon)
}
2022-02-18 21:58:53 +01:00
/* -------------------------------------------- */
2022-03-21 23:21:05 +01:00
static alchemyCheck(actor, event) {
2022-01-23 09:25:09 +01:00
const li = $(event.currentTarget).parents(".item");
2022-06-11 10:21:18 +02:00
let alchemy = actor.items.get(li.data("item-id"));
2022-01-23 09:25:09 +01:00
if (!alchemy) {
ui.notifications.warn("Unable to find Alchemy !");
return;
}
2022-06-11 20:56:35 +02:00
alchemy = duplicate(alchemy)
2022-06-11 10:21:18 +02:00
let alchemyData = alchemy.data
2022-01-23 09:25:09 +01:00
if (alchemyData.properties.pccurrent < alchemyData.properties.pccost) {
ui.notifications.warn("Pas assez de Points de Cration investis dans la Préparation !")
return
}
let alchemyDef = {
mode: "alchemy",
2022-04-08 23:42:01 +02:00
actorId: actor.id,
2022-05-11 22:05:35 +02:00
img: actor.img,
2022-01-23 09:25:09 +01:00
alchemy: alchemy,
attribute: actor.data.data.attributes.mind,
2022-02-18 21:58:53 +01:00
attrValue: actor.data.data.attributes.mind.value,
aptValue: 0,
2022-01-23 09:25:09 +01:00
careerBonus: actor.getAlchemistBonus(),
2022-02-19 17:25:42 +01:00
pcCost: Number(alchemyData.properties.pccost),
pcCostCurrent: Number(alchemyData.properties.pccurrent),
mod: Number(alchemyData.properties.difficulty),
armorAgiMalus: actor.getArmorAgiMalus(),
2022-03-27 22:56:43 +02:00
armorInitMalus: actor.getArmorInitMalus(),
2022-01-23 09:25:09 +01:00
label: alchemy.name,
2022-03-21 23:21:05 +01:00
description: game.i18n.localize('BOL.ui.makeAlchemy') + "+" + alchemy.name,
2022-01-23 09:25:09 +01:00
}
console.log("ALCHEMY!", alchemyDef);
return this.displayRollDialog(alchemyDef);
}
2022-02-18 21:58:53 +01:00
/* -------------------------------------------- */
2022-03-27 22:56:43 +02:00
static spellCheckWithSpell( actor, spell ) {
2022-01-23 09:25:09 +01:00
let spellDef = {
mode: "spell",
2022-04-08 23:42:01 +02:00
actorId: actor.id,
2022-05-11 22:05:35 +02:00
img: actor.img,
2022-01-23 09:25:09 +01:00
spell: spell,
attribute: actor.data.data.attributes.mind,
2022-02-18 21:58:53 +01:00
attrValue: actor.data.data.attributes.mind.value,
aptValue: 0,
2022-02-19 17:25:42 +01:00
ppCurrent: Number(actor.data.data.resources.power.value),
2022-01-23 09:25:09 +01:00
careerBonus: actor.getSorcererBonus(),
ppCostArmor: actor.getPPCostArmor(),
2022-06-11 20:56:35 +02:00
ppCost: Number(spell.data.properties.ppcost),
mod: Number(spell.data.properties.difficulty),
armorAgiMalus: actor.getArmorAgiMalus(),
2022-03-27 22:56:43 +02:00
armorInitMalus: actor.getArmorInitMalus(),
2022-01-23 09:25:09 +01:00
label: spell.name,
2022-03-21 23:21:05 +01:00
description: game.i18n.localize('BOL.ui.focusSpell') + " : " + spell.name,
2022-01-23 09:25:09 +01:00
}
2022-06-11 20:56:35 +02:00
//console.log("SPELL!", spellDef)
2022-03-27 22:56:43 +02:00
return this.displayRollDialog(spellDef)
}
/* -------------------------------------------- */
static spellCheck(actor, event) {
if (actor.data.data.resources.power.value <= 0) {
ui.notifications.warn("Plus assez de points de Pouvoir !")
return
}
const li = $(event.currentTarget).parents(".item")
2022-06-11 10:21:18 +02:00
let spell = actor.items.get(li.data("item-id"))
2022-03-27 22:56:43 +02:00
if (!spell) {
ui.notifications.warn("Impossible de trouver ce sort !")
return
}
2022-06-11 10:21:18 +02:00
spell = duplicate(spell)
2022-03-27 22:56:43 +02:00
return this.spellCheckWithSpell( actor, spell)
2022-01-23 09:25:09 +01:00
}
2022-01-01 23:32:48 +01:00
/* -------------------------------------------- */
2022-02-18 21:58:53 +01:00
static updateTotalDice() {
this.updateArmorMalus(this.rollData)
this.updatePPCost(this.rollData)
this.rollData.bmDice = this.rollData.nbBoons - this.rollData.nbFlaws + this.rollData.bDice - this.rollData.mDice
2022-02-18 21:58:53 +01:00
this.rollData.nbDice = 2 + Math.abs(this.rollData.bmDice)
if (this.rollData.bmDice == 0) {
$('#roll-nbdice').val("2")
} else {
let letter = (this.rollData.bmDice > 0) ? "B" : "M"
$('#roll-nbdice').val("2 + " + String(Math.abs(this.rollData.bmDice)) + letter)
2022-02-18 21:58:53 +01:00
}
2022-04-10 16:38:09 +02:00
let rollbase = this.rollData.attrValue + "+" + this.rollData.aptValue
2022-06-11 10:21:18 +02:00
if ( this.rollData.weapon && this.rollData.weapon.data.properties.onlymodifier ) {
2022-04-10 16:38:09 +02:00
rollbase = ""
}
$('#roll-modifier').val(rollbase + "+" + this.rollData.careerBonus + "+" + this.rollData.mod + "+" +
this.rollData.modRanged + "+" + this.rollData.weaponModifier + "-" + this.rollData.defence + "-" + this.rollData.modArmorMalus + "-" +
this.rollData.shieldMalus + "+" + this.rollData.attackModifier + "+" + this.rollData.appliedArmorMalus)
2022-03-10 21:05:53 +01:00
}
2022-03-10 21:05:53 +01:00
/* -------------------------------------------- */
static preProcessFightOption(rollData) {
2022-03-10 21:05:53 +01:00
rollData.damagesIgnoresArmor = false // Always reset flags
rollData.modArmorMalus = 0
rollData.attackModifier = 0
let fgItem = rollData.fightOption
if (fgItem) {
2022-03-10 21:05:53 +01:00
console.log(fgItem)
if (fgItem.data.properties.fightoptiontype == "armordefault") {
rollData.modArmorMalus = rollData.armorMalus // Activate the armor malus
rollData.damagesIgnoresArmor = true
}
if (fgItem.data.properties.fightoptiontype == "intrepid") {
rollData.attackModifier += 2
}
if (fgItem.data.properties.fightoptiontype == "defense") {
rollData.attackModifier += -1
}
if (fgItem.data.properties.fightoptiontype == "attack") {
rollData.attackModifier += 1
}
if (fgItem.data.properties.fightoptiontype == "twoweaponsdef" || fgItem.data.properties.fightoptiontype == "twoweaponsatt") {
rollData.attackModifier += -1
}
}
2022-02-18 21:58:53 +01:00
}
/* -------------------------------------------- */
static updateArmorMalus(rollData) {
rollData.appliedArmorMalus = 0
if (rollData.attribute.key == "agility") {
$("#armor-agi-malus").show()
rollData.appliedArmorMalus += rollData.armorAgiMalus
} else {
$("#armor-agi-malus").hide()
}
if (rollData.aptitude && rollData.aptitude.key == "init") {
$("#armor-init-malus").show()
rollData.appliedArmorMalus += rollData.armorInitMalus
} else {
$("#armor-init-malus").hide()
}
}
/* ------------------------------ -------------- */
static updatePPCost(rollData) {
$('#ppcost').html(rollData.ppCost + " + Armor(" + rollData.ppCostArmor + ")=" + Number(rollData.ppCost + rollData.ppCostArmor))
}
/* ------------------------------ -------------- */
2022-02-18 21:58:53 +01:00
static rollDialogListener(html) {
2022-02-18 21:58:53 +01:00
this.updateTotalDice()
2022-01-23 09:25:09 +01:00
html.find('#optcond').change((event) => { // Dynamic change of PP cost of spell
let pp = BoLUtility.computeSpellCost(this.rollData.spell, event.currentTarget.selectedOptions.length)
this.rollData.ppCost = pp
this.updatePPCost( this.rollData)
2022-02-18 21:58:53 +01:00
})
html.find('#mod').change((event) => {
this.rollData.mod = Number(event.currentTarget.value)
2022-02-18 21:58:53 +01:00
this.updateTotalDice()
})
2022-02-23 20:39:58 +01:00
html.find('#modRanged').change((event) => {
this.rollData.modRanged = Number(event.currentTarget.value)
2022-02-23 20:39:58 +01:00
this.updateTotalDice()
})
2022-02-18 21:58:53 +01:00
html.find('#attr').change((event) => {
2022-02-18 21:58:53 +01:00
let attrKey = event.currentTarget.value
2022-04-08 23:42:01 +02:00
let actor = game.actors.get( this.rollData.actorId)
this.rollData.attribute = duplicate(actor.data.data.attributes[attrKey])
this.rollData.attrValue = actor.data.data.attributes[attrKey].value
2022-02-18 21:58:53 +01:00
this.updateTotalDice()
})
html.find('#apt').change((event) => {
2022-02-18 21:58:53 +01:00
let aptKey = event.currentTarget.value
2022-04-08 23:42:01 +02:00
let actor = game.actors.get( this.rollData.actorId)
this.rollData.aptitude = duplicate(actor.data.data.aptitudes[aptKey])
this.rollData.aptValue = actor.data.data.aptitudes[aptKey].value
2022-02-18 21:58:53 +01:00
this.updateTotalDice()
})
2022-02-18 21:58:53 +01:00
html.find('#applyShieldMalus').click((event) => {
2022-02-18 21:58:53 +01:00
if (event.currentTarget.checked) {
this.rollData.shieldMalus = this.rollData.shieldAttackMalus
} else {
this.rollData.shieldMalus = 0
}
})
html.find('#career').change((event) => {
2022-02-18 21:58:53 +01:00
let careers = $('#career').val()
this.rollData.careerBonus = (!careers || careers.length == 0) ? 0 : Math.max(...careers.map(i => parseInt(i)))
2022-02-18 21:58:53 +01:00
this.updateTotalDice()
})
html.find('#boon').change((event) => {
2022-02-18 21:58:53 +01:00
let boons = $('#boon').val()
2022-05-23 18:38:51 +02:00
this.rollData.nbBoons = (!boons || boons.length == 0) ? 0 : boons.length
2022-02-18 21:58:53 +01:00
this.updateTotalDice()
})
html.find('#flaw').change((event) => {
2022-02-18 21:58:53 +01:00
let flaws = $('#flaw').val()
2022-05-23 18:38:51 +02:00
this.rollData.nbFlaws = (!flaws || flaws.length == 0) ? 0 : flaws.length
2022-02-18 21:58:53 +01:00
this.updateTotalDice()
})
html.find('.bdice').click((event) => {
this.rollData.mDice = 0
this.rollData.bDice = Number(event.currentTarget.value)
2022-02-18 21:58:53 +01:00
this.updateTotalDice()
})
html.find('.mdice').click((event) => {
2022-02-18 21:58:53 +01:00
this.rollData.bDice = 0
this.rollData.mDice = Number(event.currentTarget.value)
2022-02-18 21:58:53 +01:00
this.updateTotalDice()
})
2022-01-23 09:25:09 +01:00
}
2022-03-10 21:05:53 +01:00
/* -------------------------------------------- */
2022-05-23 18:38:51 +02:00
static preProcessWeapon(rollData, defender) {
2022-03-10 21:05:53 +01:00
if (rollData.mode == "weapon") {
2022-06-11 10:21:18 +02:00
rollData.weaponModifier = rollData.weapon.data.properties.attackModifiers ?? 0;
rollData.attackBonusDice = rollData.weapon.data.properties.attackBonusDice
2022-05-23 18:38:51 +02:00
if (defender) { // If target is selected
rollData.defence = defender.defenseValue
rollData.armorMalus = defender.armorMalusValue
2022-03-10 21:05:53 +01:00
rollData.shieldBlock = 'none'
2022-05-23 18:38:51 +02:00
let shields = defender.shields
2022-03-10 21:05:53 +01:00
for (let shield of shields) {
rollData.shieldBlock = (shield.data.properties.blocking.blockingAll) ? 'blockall' : 'blockone';
rollData.shieldAttackMalus = (shield.data.properties.blocking.malus) ? shield.data.properties.blocking.malus : 1;
rollData.applyShieldMalus = false
}
}
}
}
2022-01-01 23:32:48 +01:00
/* ROLL DIALOGS */
/* -------------------------------------------- */
2022-01-16 22:06:49 +01:00
static async displayRollDialog(rollData, onEnter = "submit") {
2022-01-01 23:32:48 +01:00
2022-03-10 21:05:53 +01:00
// initialize default flags/values
2022-01-16 22:06:49 +01:00
const rollOptionTpl = `systems/bol/templates/dialogs/${rollData.mode}-roll-dialog.hbs`
2022-04-08 23:42:01 +02:00
let actor = game.actors.get( rollData.actorId )
2022-05-23 18:38:51 +02:00
let defender
if ( rollData.targetId) {
let token = game.scenes.current.tokens.get(rollData.targetId)
defender = token.actor
}
2022-04-08 23:42:01 +02:00
rollData.careers = actor.careers
rollData.boons = actor.bonusBoons
rollData.flaws = actor.malusFlaws
rollData.rollOwnerID = actor.id
2022-01-16 22:06:49 +01:00
rollData.defence = 0
2022-03-10 21:05:53 +01:00
rollData.attackModifier = 0 // Used for fight options
rollData.modArmorMalus = 0 // Used for fight options
rollData.bDice = 0
rollData.mDice = 0
2022-02-18 21:58:53 +01:00
rollData.nbBoons = 0
rollData.nbFlaws = 0
rollData.nbDice = 0
2022-02-18 21:58:53 +01:00
if (rollData.shieldBlock == 'blockall') {
rollData.shieldMalus = rollData.shieldAttackMalus;
} else {
rollData.shieldMalus = 0
}
rollData.careerBonus = rollData.careerBonus ?? 0
2022-02-23 20:39:58 +01:00
rollData.modRanged = rollData.modRanged ?? 0
2022-02-18 21:58:53 +01:00
rollData.mod = rollData.mod ?? 0
2022-01-16 22:06:49 +01:00
rollData.id = randomID(16)
2022-01-19 21:57:34 +01:00
rollData.weaponModifier = 0
rollData.attackBonusDice = false
2022-03-10 21:05:53 +01:00
rollData.armorMalus = 0
// Specific stuff
2022-05-23 18:38:51 +02:00
this.preProcessWeapon(rollData, defender)
2022-03-10 21:05:53 +01:00
this.preProcessFightOption(rollData)
this.updateArmorMalus(rollData)
this.updatePPCost(rollData)
2022-03-10 21:05:53 +01:00
// Save
2022-02-18 21:58:53 +01:00
this.rollData = rollData
console.log("ROLLDATA", rollData)
2022-03-10 21:05:53 +01:00
// Then display+process the dialog
2022-01-16 22:06:49 +01:00
const rollOptionContent = await renderTemplate(rollOptionTpl, rollData);
2022-01-01 23:32:48 +01:00
let d = new Dialog({
2022-01-16 22:06:49 +01:00
title: rollData.label,
2022-01-01 23:32:48 +01:00
content: rollOptionContent,
2022-01-16 22:06:49 +01:00
rollData: rollData,
2022-01-23 09:25:09 +01:00
render: html => this.rollDialogListener(html),
2022-01-01 23:32:48 +01:00
buttons: {
cancel: {
icon: '<i class="fas fa-times"></i>',
label: game.i18n.localize("BOL.ui.cancel"),
callback: () => {
}
},
submit: {
icon: '<i class="fas fa-check"></i>',
label: game.i18n.localize("BOL.ui.submit"),
callback: (html) => {
2022-01-23 09:25:09 +01:00
if (rollData.mode == 'spell' && rollData.ppCurrent < rollData.ppCost) { // Check PP available
ui.notifications.warn("Pas assez de Points de Pouvoir !")
return
}
2022-02-20 10:12:25 +01:00
rollData.registerInit = (rollData.aptitude && rollData.aptitude.key == 'init') ? $('#register-init').is(":checked") : false;
2022-01-17 23:50:57 +01:00
const isMalus = rollData.mDice > 0
2022-02-18 21:58:53 +01:00
rollData.nbDice += (rollData.attackBonusDice) ? 1 : 0
2021-12-29 20:33:59 +01:00
2022-04-10 16:38:09 +02:00
let rollbase = rollData.attrValue + rollData.aptValue
2022-06-11 10:21:18 +02:00
if ( rollData.weapon && rollData.weapon.data.properties.onlymodifier ) {
2022-04-10 16:38:09 +02:00
rollbase = 0
}
const modifiers = rollbase + rollData.careerBonus + rollData.mod + rollData.weaponModifier - rollData.defence - rollData.modArmorMalus + rollData.shieldMalus + rollData.attackModifier + rollData.appliedArmorMalus
2022-02-18 21:58:53 +01:00
const formula = (isMalus) ? rollData.nbDice + "d6kl2 + " + modifiers : rollData.nbDice + "d6kh2 + " + modifiers
rollData.formula = formula
2022-01-17 23:50:57 +01:00
rollData.modifiers = modifiers
2021-12-25 23:26:27 +01:00
2022-01-16 22:06:49 +01:00
let r = new BoLDefaultRoll(rollData);
r.roll();
2022-01-01 23:32:48 +01:00
}
}
},
default: onEnter,
close: () => { }
}, this.options());
2022-01-23 09:25:09 +01:00
2022-01-01 23:32:48 +01:00
return d.render(true);
}
}
/* -------------------------------------------- */
export class BoLDefaultRoll {
2022-02-18 21:58:53 +01:00
2022-01-16 22:06:49 +01:00
constructor(rollData) {
this.rollData = rollData
2022-02-18 21:58:53 +01:00
if (this.rollData.isSuccess == undefined) { // First init
2022-01-17 23:50:57 +01:00
this.rollData.isSuccess = false;
this.rollData.isCritical = false;
this.rollData.isFumble = false;
}
2022-02-18 21:58:53 +01:00
if (this.rollData.optionsId) {
2022-06-11 20:56:35 +02:00
BoLUtility.cleanupButtons( this.rollData.optionsId)
2022-02-18 21:58:53 +01:00
}
if (this.rollData.applyId) {
2022-06-11 20:56:35 +02:00
BoLUtility.cleanupButtons( this.rollData.applyId)
2022-02-18 21:58:53 +01:00
}
2022-01-17 23:50:57 +01:00
this.rollData.optionsId = randomID(16)
this.rollData.applyId = randomID(16)
2022-01-01 23:32:48 +01:00
}
2021-12-25 23:26:27 +01:00
/* -------------------------------------------- */
2022-01-01 23:32:48 +01:00
async roll() {
2022-01-16 22:06:49 +01:00
2022-03-10 21:05:53 +01:00
const r = new Roll(this.rollData.formula)
// console.log("Roll formula", this.rollData.formula)
await r.roll({ "async": false })
const activeDice = r.terms[0].results.filter(r => r.active)
const diceTotal = activeDice.map(r => r.result).reduce((a, b) => a + b)
2022-01-17 23:50:57 +01:00
this.rollData.roll = r
2022-03-10 21:05:53 +01:00
this.rollData.isSuccess = (r.total >= 9)
2022-01-17 23:50:57 +01:00
this.rollData.isCritical = (diceTotal === 12)
this.rollData.isRealCritical = (diceTotal === 12)
2022-03-27 22:56:43 +02:00
this.rollData.isHeroic = (diceTotal === 12)
this.rollData.isLegendary = false
2022-03-10 21:05:53 +01:00
this.rollData.isFumble = (diceTotal === 2)
2022-01-16 22:06:49 +01:00
this.rollData.isFailure = !this.rollData.isSuccess
2022-04-08 23:42:01 +02:00
let actor = game.actors.get( this.rollData.actorId)
2022-01-17 23:50:57 +01:00
if (this.rollData.reroll == undefined) {
2022-04-08 23:42:01 +02:00
this.rollData.reroll = actor.heroReroll()
2022-01-17 23:50:57 +01:00
}
2022-04-08 23:42:01 +02:00
2022-01-16 22:53:41 +01:00
if (this.rollData.registerInit) {
2022-04-08 23:42:01 +02:00
actor.registerInit(r.total, this.rollData.isCritical, this.rollData.isFumble)
2022-01-16 22:53:41 +01:00
}
2022-01-23 09:25:09 +01:00
if (this.rollData.isSuccess && this.rollData.mode == "spell") { // PP cost management
2022-06-11 20:56:35 +02:00
this.rollData.remainingPP = actor.spendPowerPoint(this.rollData.ppCost + this.rollData.ppCostArmor)
2022-01-23 09:25:09 +01:00
}
if (this.rollData.mode == "alchemy") { // PP cost management
2022-06-11 10:21:18 +02:00
actor.resetAlchemyStatus(this.rollData.alchemy._id)
2022-01-23 09:25:09 +01:00
}
2022-01-16 22:53:41 +01:00
2022-01-17 23:50:57 +01:00
await this.sendChatMessage()
}
/* -------------------------------------------- */
2022-01-17 23:50:57 +01:00
async sendChatMessage() {
2022-04-08 23:42:01 +02:00
let actor = game.actors.get( this.rollData.actorId)
2022-06-11 10:21:18 +02:00
this._buildChatMessage(this.rollData).then( async msgFlavor => {
let msg = await this.rollData.roll.toMessage({
2022-01-01 23:32:48 +01:00
user: game.user.id,
2022-03-27 22:56:43 +02:00
rollMode: game.settings.get("core", "rollMode"),
//whisper: BoLUtility.getWhisperRecipientsAndGMs(this.rollData.actor.name),
2022-01-01 23:32:48 +01:00
flavor: msgFlavor,
2022-04-08 23:42:01 +02:00
speaker: ChatMessage.getSpeaker({ actor: actor }),
2022-01-17 23:50:57 +01:00
})
2022-06-11 10:21:18 +02:00
msg.setFlag("world", "bol-roll-data", this.rollData)
2022-03-27 22:56:43 +02:00
})
2022-01-09 13:23:20 +01:00
}
/* -------------------------------------------- */
2022-03-27 22:56:43 +02:00
upgradeToLegendary() {
2022-01-17 23:50:57 +01:00
// Force to Critical roll
this.rollData.isCritical = true
2022-03-27 22:56:43 +02:00
this.rollData.isLegendary = true
2022-01-17 23:50:57 +01:00
this.rollData.isRealCritical = false
this.rollData.isSuccess = true
this.rollData.isFailure = false
this.rollData.roll = new Roll("12+" + this.rollData.modifiers)
this.rollData.reroll = false
this.sendChatMessage()
2022-01-09 13:23:20 +01:00
}
2022-01-17 23:50:57 +01:00
2022-03-27 22:56:43 +02:00
/* -------------------------------------------- */
upgradeToHeroic() {
// Force to Critical roll
this.rollData.isCritical = true
this.rollData.isHeroic = true
this.rollData.isLegendary = false
this.rollData.isRealCritical = false
this.rollData.isSuccess = true
this.rollData.isFailure = false
this.rollData.roll = new Roll("12+" + this.rollData.modifiers)
this.rollData.reroll = false
this.sendChatMessage()
}
/* -------------------------------------------- */
2022-01-17 23:50:57 +01:00
setSuccess(flag) {
2022-01-16 22:06:49 +01:00
this.rollData.isSuccess = flag
}
/* -------------------------------------------- */
2022-01-17 23:50:57 +01:00
async sendDamageMessage() {
2022-04-08 23:42:01 +02:00
let actor = game.actors.get( this.rollData.actorId)
2022-06-11 10:21:18 +02:00
this._buildDamageChatMessage(this.rollData).then(async msgFlavor => {
let msg = await this.rollData.damageRoll.toMessage({
2022-01-17 23:50:57 +01:00
user: game.user.id,
flavor: msgFlavor,
2022-04-08 23:42:01 +02:00
speaker: ChatMessage.getSpeaker({ actor: actor }),
2022-01-17 23:50:57 +01:00
flags: { msgType: "default" }
})
2022-07-16 11:03:33 +02:00
this.rollData.actor = undefined // Cleanup
2022-06-11 10:21:18 +02:00
msg.setFlag("world", "bol-roll-data", this.rollData)
})
2022-01-17 23:50:57 +01:00
}
2022-02-18 21:58:53 +01:00
/* -------------------------------------------- */
2022-05-10 23:04:04 +02:00
getDamageAttributeValue(attrDamage, actorId = undefined) {
2022-02-02 09:35:32 +01:00
let attrDamageValue = 0
2022-05-10 23:04:04 +02:00
let actor = game.actors.get( (actorId) ? actorId: this.rollData.actorId)
2022-02-18 21:58:53 +01:00
if (attrDamage.includes("vigor")) {
2022-04-08 23:42:01 +02:00
attrDamageValue = actor.data.data.attributes.vigor.value
2022-02-02 09:35:32 +01:00
if (attrDamage.includes("half")) {
attrDamageValue = Math.floor(attrDamageValue / 2)
}
}
return attrDamageValue
}
/* -------------------------------------------- */
2022-01-17 23:50:57 +01:00
async rollDamage() {
if (this.rollData.mode != "weapon") { // Only specific process in Weapon mode
2022-06-11 10:21:18 +02:00
return
2022-01-16 22:06:49 +01:00
}
if (this.rollData.isSuccess) {
2022-02-18 21:58:53 +01:00
if (!this.rollData.damageRoll) {
2022-01-17 23:50:57 +01:00
let bonusDmg = 0
2022-02-18 21:58:53 +01:00
if (this.rollData.damageMode == 'damage-plus-6') {
2022-01-17 23:50:57 +01:00
bonusDmg = 6
}
2022-02-18 21:58:53 +01:00
if (this.rollData.damageMode == 'damage-plus-12') {
2022-01-17 23:50:57 +01:00
bonusDmg = 12
}
2022-06-11 10:21:18 +02:00
let attrDamageValue = this.getDamageAttributeValue(this.rollData.weapon.data.properties.damageAttribute)
let weaponFormula = BoLUtility.getDamageFormula(this.rollData.weapon.data, this.rollData.fightOption)
2022-02-23 20:39:58 +01:00
2022-02-18 21:58:53 +01:00
let damageFormula = weaponFormula + "+" + bonusDmg + "+" + attrDamageValue
2022-03-10 21:05:53 +01:00
console.log("DAMAGE !!!", damageFormula, attrDamageValue, this.rollData)
2022-01-17 23:50:57 +01:00
//console.log("Formula", weaponFormula, damageFormula, this.rollData.weapon.data.data.properties.damage)
this.rollData.damageFormula = damageFormula
this.rollData.damageRoll = new Roll(damageFormula)
await this.rollData.damageRoll.roll({ "async": false })
2022-02-23 20:39:58 +01:00
this.rollData.damageTotal = this.rollData.damageRoll.total
2022-01-17 23:50:57 +01:00
}
2022-06-11 20:56:35 +02:00
BoLUtility.cleanupButtons(this.rollData.optionsId)
2022-01-17 23:50:57 +01:00
this.sendDamageMessage()
2021-12-29 19:15:06 +01:00
}
2022-01-01 23:32:48 +01:00
}
2021-12-25 23:26:27 +01:00
/* -------------------------------------------- */
2022-01-16 22:06:49 +01:00
_buildDamageChatMessage(rollData) {
const rollMessageTpl = 'systems/bol/templates/chat/rolls/damage-roll-card.hbs';
2022-03-10 21:05:53 +01:00
return renderTemplate(rollMessageTpl, rollData)
2022-01-16 22:06:49 +01:00
}
2021-12-25 23:26:27 +01:00
/* -------------------------------------------- */
2022-01-16 22:06:49 +01:00
_buildChatMessage(rollData) {
const rollMessageTpl = 'systems/bol/templates/chat/rolls/default-roll-card.hbs';
return renderTemplate(rollMessageTpl, rollData);
}
2021-12-25 23:26:27 +01:00
}