bol/module/controllers/bol-rolls.js

483 lines
17 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 __adv2dice = { ["1B"]: 3, ["2B"]: 4, ["2"]: 2, ["1M"]: 3, ["2M"]: 4 }
const _apt2attr = { init: "mind", melee: "agility", ranged: "agility", def: "vigor" }
2022-01-16 22:53:41 +01:00
export class BoLRoll {
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-01-17 23:50:57 +01:00
static convertToAdv(adv) {
2022-01-16 22:06:49 +01:00
if (adv == 0) return "2"
2022-01-17 23:50:57 +01:00
return Math.abs(adv) + (adv < 0) ? 'M' : 'B';
2022-01-16 22:06:49 +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
/* -------------------------------------------- */
2022-01-01 23:32:48 +01:00
static attributeCheck(actor, actorData, dataset, event) {
const key = dataset.key;
const adv = dataset.adv;
let attribute = eval(`actor.data.data.attributes.${key}`);
let label = (attribute.label) ? game.i18n.localize(attribute.label) : null;
let description = actor.name + " - " + game.i18n.localize('BOL.ui.attributeCheck') + " - " + game.i18n.localize(attribute.label);
2022-01-16 22:06:49 +01:00
return this.displayRollDialog(
{
2022-01-17 23:50:57 +01:00
mode: "attribute",
2022-01-16 22:06:49 +01:00
actor: actor,
actorData: actorData,
attribute: attribute,
2022-02-18 21:58:53 +01:00
attrValue: attribute.value,
aptValue: 0,
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,
adv: this.convertToAdv(adv),
mod: 0
});
2022-01-01 23:32:48 +01:00
}
2022-02-18 21:58:53 +01:00
/* -------------------------------------------- */
2022-01-01 23:32:48 +01:00
static aptitudeCheck(actor, actorData, dataset, event) {
const key = dataset.key;
const adv = dataset.adv;
2022-01-16 22:53:41 +01:00
2022-01-01 23:32:48 +01:00
let aptitude = eval(`actor.data.data.aptitudes.${key}`);
2022-01-16 22:53:41 +01:00
let attrKey = this.getDefaultAttribute(key)
let attribute = eval(`actor.data.data.attributes.${attrKey}`);
2022-01-01 23:32:48 +01:00
let label = (aptitude.label) ? game.i18n.localize(aptitude.label) : null;
let description = actor.name + " - " + 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-01-16 22:06:49 +01:00
actor: actor,
actorData: actorData,
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,
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,
adv: this.convertToAdv(adv),
mod: 0
});
2021-12-25 23:26:27 +01:00
}
2022-02-18 21:58:53 +01:00
/* -------------------------------------------- */
2022-01-01 23:32:48 +01:00
static weaponCheck(actor, actorData, dataset, event) {
let target = BoLUtility.getTarget()
const li = $(event.currentTarget).parents(".item");
const weapon = actor.items.get(li.data("item-id"));
if (!weapon) {
ui.notifications.warn("Unable to find weapon !");
return;
}
2022-01-01 23:32:48 +01:00
let weaponData = weapon.data.data;
2022-02-18 21:58:53 +01:00
let attribute = eval(`actor.data.data.attributes.${weaponData.properties.attackAttribute}`)
let aptitude = eval(`actor.data.data.aptitudes.${weaponData.properties.attackAptitude}`)
2022-01-01 23:32:48 +01:00
let attackDef = {
2022-01-16 22:06:49 +01:00
mode: "weapon",
actor: actor,
actorData: actorData,
2022-01-01 23:32:48 +01:00
weapon: weapon,
target: target,
2022-02-18 21:58:53 +01:00
careerBonus: 0,
2022-01-01 23:32:48 +01:00
defender: (target) ? game.actors.get(target.data.actorId) : undefined,
2022-02-18 21:58:53 +01:00
attribute: attribute,
aptitude: aptitude,
attrValue: attribute.value,
aptValue: aptitude.value,
mod: 0,
2022-01-01 23:32:48 +01:00
label: (weapon.name) ? weapon.name : game.i18n.localize('BOL.ui.noWeaponName'),
description: actor.name + " - " + game.i18n.localize('BOL.ui.weaponAttack'),
2022-01-16 22:06:49 +01:00
adv: "2",
2022-01-01 23:32:48 +01:00
}
console.debug("WEAPON!", attackDef, weaponData);
2022-01-16 22:06:49 +01:00
return this.displayRollDialog(attackDef);
2022-01-01 23:32:48 +01:00
}
2022-02-18 21:58:53 +01:00
/* -------------------------------------------- */
static alchemyCheck(actor, actorData, dataset, event) {
2022-01-23 09:25:09 +01:00
const li = $(event.currentTarget).parents(".item");
const alchemy = actor.items.get(li.data("item-id"));
if (!alchemy) {
ui.notifications.warn("Unable to find Alchemy !");
return;
}
let alchemyData = alchemy.data.data
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",
actor: actor,
actorData: actorData,
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(),
pcCost: alchemyData.properties.pccost,
pcCostCurrent: alchemyData.properties.pccurrent,
mod: alchemyData.properties.difficulty,
label: alchemy.name,
adv: "2",
description: actor.name + " - " + game.i18n.localize('BOL.ui.makeAlchemy'),
}
console.log("ALCHEMY!", alchemyDef);
return this.displayRollDialog(alchemyDef);
}
2022-02-18 21:58:53 +01:00
/* -------------------------------------------- */
static spellCheck(actor, actorData, dataset, event) {
2022-01-23 09:25:09 +01:00
if (actor.data.data.resources.power.value <= 0) {
ui.notifications.warn("Plus assez de points de Pouvoir !")
return
}
const li = $(event.currentTarget).parents(".item");
const spell = actor.items.get(li.data("item-id"));
if (!spell) {
ui.notifications.warn("Unable to find spell !");
return;
}
let spellData = spell.data.data;
let spellDef = {
mode: "spell",
actor: actor,
actorData: actorData,
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,
ppCurrent: actor.data.data.resources.power.value,
2022-01-23 09:25:09 +01:00
careerBonus: actor.getSorcererBonus(),
ppCost: spell.data.data.properties.ppcost,
mod: spellData.properties.difficulty,
label: spell.name,
adv: "2",
description: actor.name + " - " + game.i18n.localize('BOL.ui.focusSpell'),
}
console.log("SPELL!", spellDef);
return this.displayRollDialog(spellDef);
}
2022-01-01 23:32:48 +01:00
/* -------------------------------------------- */
2022-02-18 21:58:53 +01:00
static updateTotalDice() {
this.rollData.bmDice = this.rollData.nbBoons - this.rollData.nbFlaws + this.rollData.bDice - this.rollData.mDice
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 )
}
$('#roll-modifier').val( this.rollData.attrValue + "+" + this.rollData.aptValue + "+" + this.rollData.careerBonus + "+" + this.rollData.mod + "+" +
this.rollData.weaponModifier + "-" + this.rollData.defence + "+" + this.rollData.shieldMalus )
}
2022-01-23 09:25:09 +01:00
2022-02-18 21:58:53 +01:00
/* -------------------------------------------- */
static rollDialogListener(html) {
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)
$('#ppcost').html(pp)
this.rollData.ppCost = pp
2022-02-18 21:58:53 +01:00
})
html.find('#mod').change((event) => {
this.rollData.mod = Number(event.currentTarget.value)
this.updateTotalDice()
})
html.find('#attr').change((event) => {
let attrKey = event.currentTarget.value
this.rollData.attribute = duplicate(this.rollData.actor.data.data.attributes[attrKey])
this.rollData.attrValue = this.rollData.actor.data.data.attributes[attrKey].value
this.updateTotalDice()
} )
html.find('#apt').change((event) => {
let aptKey = event.currentTarget.value
this.rollData.aptitude = duplicate(this.rollData.actor.data.data.aptitudes[aptKey])
this.rollData.aptValue = this.rollData.actor.data.data.aptitudes[aptKey].value
this.updateTotalDice()
} )
html.find('#applyShieldMalus').click( (event) => {
if (event.currentTarget.checked) {
this.rollData.shieldMalus = this.rollData.shieldAttackMalus
} else {
this.rollData.shieldMalus = 0
}
})
html.find('#career').change((event) => {
let careers = $('#career').val()
this.rollData.careerBonus = (!careers || careers.length == 0) ? 0 : Math.max(...careers.map(i => parseInt(i)))
this.updateTotalDice()
})
html.find('#boon').change((event) => {
let boons = $('#boon').val()
this.rollData.nbBoons = (!boons || boons.length == 0) ? 0 : Math.max(...boons.map(i => parseInt(i)))
this.updateTotalDice()
})
html.find('#flaw').change((event) => {
let flaws = $('#flaw').val()
this.rollData.nbFlaws = (!flaws || flaws.length == 0) ? 0 : Math.max(...flaws.map(i => parseInt(i)))
this.updateTotalDice()
})
html.find('.bdice').click((event) => {
this.rollData.mDice = 0
this.rollData.bDice = Number(event.currentTarget.value)
this.updateTotalDice()
})
html.find('.mdice').click((event) => {
this.rollData.bDice = 0
this.rollData.mDice = Number(event.currentTarget.value)
this.updateTotalDice()
})
2022-01-23 09:25:09 +01:00
}
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-01-16 22:06:49 +01:00
const rollOptionTpl = `systems/bol/templates/dialogs/${rollData.mode}-roll-dialog.hbs`
rollData.careers = rollData.actorData.features.careers
2022-02-18 21:58:53 +01:00
rollData.boons = rollData.actor.bonusBoons
rollData.flaws = rollData.actor.malusFlaws
2022-01-16 22:06:49 +01:00
rollData.defence = 0
2022-02-18 21:58:53 +01:00
rollData.bDice = 0
rollData.mDice = 0
rollData.nbBoons = 0
rollData.nbFlaws = 0
rollData.nbDice = 0
if (rollData.shieldBlock == 'blockall') {
rollData.shieldMalus = rollData.shieldAttackMalus;
} else {
rollData.shieldMalus = 0
}
rollData.careerBonus = rollData.careerBonus ?? 0
rollData.mod = rollData.mod ?? 0
2022-01-16 22:06:49 +01:00
rollData.id = randomID(16)
2022-01-01 23:32:48 +01:00
2022-01-16 22:06:49 +01:00
// Weapon mode specific management
2022-01-19 21:57:34 +01:00
rollData.weaponModifier = 0
rollData.attackBonusDice = false
2022-02-18 21:58:53 +01:00
// Saves
this.rollData = rollData
console.log("ROLLDATA", rollData)
2022-01-16 22:06:49 +01:00
if (rollData.mode == "weapon") {
2022-02-18 21:58:53 +01:00
rollData.weaponModifier = rollData.weapon.data.data.properties.attackModifiers ?? 0;
2022-01-19 21:57:34 +01:00
rollData.attackBonusDice = rollData.weapon.data.data.properties.attackBonusDice
2022-01-16 22:06:49 +01:00
if (rollData.defender) { // If target is selected
2022-02-15 20:53:13 +01:00
rollData.defence = rollData.defender.defenseValue
rollData.shieldBlock = 'none'
2022-01-16 22:06:49 +01:00
let shields = rollData.defender.shields
2022-02-15 20:53:13 +01:00
//console.log("Shields", shields)
2022-01-16 22:06:49 +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
}
2021-12-29 20:33:59 +01:00
}
2022-01-01 23:32:48 +01:00
}
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-01-17 23:50:57 +01:00
rollData.registerInit = (rollData.aptKey == 'init') ? $('#register-init').is(":checked") : false;
2022-02-18 21:58:53 +01:00
const isMalus = rollData.mDice > 0
rollData.nbDice += (rollData.attackBonusDice) ? 1 : 0
2021-12-29 20:33:59 +01:00
2022-02-18 21:58:53 +01:00
const modifiers = rollData.attrValue + rollData.aptValue + rollData.careerBonus + rollData.mod + rollData.weaponModifier - rollData.defence + rollData.shieldMalus
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) {
2022-01-17 23:50:57 +01:00
BoLUtility.storeRoll(rollData)
2022-01-16 22:06:49 +01:00
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-01-17 23:50:57 +01:00
$(`#${this.rollData.optionsId}`).hide() // Hide the options roll buttons
2022-02-18 21:58:53 +01:00
}
if (this.rollData.applyId) {
2022-01-17 23:50:57 +01:00
$(`#${this.rollData.applyId}`).hide() // Hide the options roll buttons
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
const r = new Roll(this.rollData.formula);
2022-01-01 23:32:48 +01:00
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-01-16 22:06:49 +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-01-16 22:06:49 +01:00
this.rollData.isFumble = (diceTotal === 2);
this.rollData.isFailure = !this.rollData.isSuccess
2022-01-17 23:50:57 +01:00
if (this.rollData.reroll == undefined) {
this.rollData.reroll = this.rollData.actor.heroReroll()
}
2022-01-16 22:06:49 +01:00
2022-01-16 22:53:41 +01:00
if (this.rollData.registerInit) {
2022-01-17 23:50:57 +01:00
this.rollData.actor.registerInit(r.total, this.rollData.isCritical);
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
this.rollData.actor.spendPowerPoint(this.rollData.ppCost)
}
if (this.rollData.mode == "alchemy") { // PP cost management
this.rollData.actor.resetAlchemyStatus(this.rollData.alchemy.id)
}
2022-01-16 22:53:41 +01:00
2022-01-17 23:50:57 +01:00
await this.sendChatMessage()
}
async sendChatMessage() {
2022-01-16 22:06:49 +01:00
this._buildChatMessage(this.rollData).then(msgFlavor => {
2022-01-17 23:50:57 +01:00
this.rollData.roll.toMessage({
2022-01-01 23:32:48 +01:00
user: game.user.id,
flavor: msgFlavor,
2022-01-16 22:06:49 +01:00
speaker: ChatMessage.getSpeaker({ actor: this.rollData.actor }),
2022-01-01 23:32:48 +01:00
flags: { msgType: "default" }
2022-01-17 23:50:57 +01:00
})
2022-01-01 23:32:48 +01:00
});
2022-01-09 13:23:20 +01:00
}
2022-01-17 23:50:57 +01:00
upgradeToCritical() {
// Force to Critical roll
this.rollData.isCritical = true
this.rollData.isRealCritical = false
this.rollData.isSuccess = true
this.rollData.isFailure = false
this.rollData.reroll = 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
setSuccess(flag) {
2022-01-16 22:06:49 +01:00
this.rollData.isSuccess = flag
}
2022-01-17 23:50:57 +01:00
async sendDamageMessage() {
this._buildDamageChatMessage(this.rollData).then(msgFlavor => {
this.rollData.damageRoll.toMessage({
user: game.user.id,
flavor: msgFlavor,
speaker: ChatMessage.getSpeaker({ actor: this.rollData.actor }),
flags: { msgType: "default" }
})
});
}
2022-02-18 21:58:53 +01:00
getDamageAttributeValue(attrDamage) {
2022-02-02 09:35:32 +01:00
let attrDamageValue = 0
2022-02-18 21:58:53 +01:00
if (attrDamage.includes("vigor")) {
attrDamageValue = this.rollData.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-01-16 22:06:49 +01:00
return;
}
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-02-02 09:35:32 +01:00
let attrDamageValue = this.getDamageAttributeValue(this.rollData.weapon.data.data.properties.damageAttribute)
2022-01-17 23:50:57 +01:00
let weaponFormula = BoLUtility.getDamageFormula(this.rollData.weapon.data.data.properties.damage,
2022-02-18 21:58:53 +01:00
this.rollData.weapon.data.data.properties.damageModifiers,
this.rollData.weapon.data.data.properties.damageMultiplier)
let damageFormula = weaponFormula + "+" + bonusDmg + "+" + attrDamageValue
2022-02-02 09:35:32 +01:00
console.log("DAMAGE !!!", damageFormula, attrDamageValue)
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)
this.rollData.damageTotal = this.rollData.damageRoll.total
await this.rollData.damageRoll.roll({ "async": false })
}
$(`#${this.rollData.optionsId}`).hide() // Hide the options roll buttons
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';
return renderTemplate(rollMessageTpl, rollData);
}
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
}