fvtt-pegasus-rpg/modules/pegasus-utility.js

1351 lines
49 KiB
JavaScript
Raw Permalink Normal View History

2022-01-28 10:05:54 +01:00
/* -------------------------------------------- */
import { PegasusCombat } from "./pegasus-combat.js";
2022-01-06 18:22:05 +01:00
import { PegasusCommands } from "./pegasus-commands.js";
import { PegasusActorCreate } from "./pegasus-create-char.js";
2022-08-14 15:27:54 +02:00
import { PegasusRollDialog } from "./pegasus-roll-dialog.js";
2022-01-06 18:22:05 +01:00
2022-01-28 10:05:54 +01:00
/* -------------------------------------------- */
2022-07-28 18:45:04 +02:00
const __level2Dice = ["d0", "d4", "d6", "d8", "d10", "d12"]
2022-01-28 10:05:54 +01:00
const __name2DiceValue = { "0": 0, "d0": 0, "d4": 4, "d6": 6, "d8": 8, "d10": 10, "d12": 12 }
2022-09-21 16:54:34 +02:00
const __dice2Level = { "d0": 0, "d4": 1, "d6": 2, "d8": 3, "d10": 4, "d12": 5 }
2022-09-29 13:07:57 +02:00
const __rangeKeyToText = {
notapplicable: "N/A", touch: "Self Only", touchself: "Touch/Self", tz: "Threat Zone", close: "Close", medium: "Medium",
long: "Long", extreme: "Extreme", sight: "Lineof Sight", tz_close: "TZ/Close", close_medium: "Close/Medium", medium_long: "Medium/Long",
long_extreme: "Long/Extreme"
}
2022-01-28 10:05:54 +01:00
/* -------------------------------------------- */
export class PegasusUtility {
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
static async init() {
2022-08-14 15:27:54 +02:00
Hooks.on('renderChatLog', (log, html, data) => PegasusUtility.chatListeners(html))
Hooks.on('targetToken', (user, token, flag) => PegasusUtility.targetToken(user, token, flag))
Hooks.on('renderSidebarTab', (app, html, data) => PegasusUtility.addDiceRollButton(app, html, data))
2023-09-13 17:37:30 +02:00
/* Deprecated, no more used in rules Hooks.on("getCombatTrackerEntryContext", (html, options) => {
2022-01-28 10:05:54 +01:00
PegasusUtility.pushInitiativeOptions(html, options);
2023-09-13 17:37:30 +02:00
});*/
2023-09-13 21:25:44 +02:00
Hooks.once('diceSoNiceReady', (dice3d) => {
dice3d.addSystem({ id: "pegasus-hindrance", name: "Hindrance Die" }, "preferred");
dice3d.addDicePreset({
type: "dh",
labels: [
'systems/fvtt-pegasus-rpg/images/dice/hindrance-dice-empty.png',
'systems/fvtt-pegasus-rpg/images/dice/hindrance-dice-empty.png',
'systems/fvtt-pegasus-rpg/images/dice/hindrance-dice-empty.png',
'systems/fvtt-pegasus-rpg/images/dice/hindrance-dice-empty.png',
'systems/fvtt-pegasus-rpg/images/dice/hindrance-dice-empty.png',
'systems/fvtt-pegasus-rpg/images/dice/hindrance-dice.png'
],
bumpMaps: [
'systems/fvtt-pegasus-rpg/images/dice/hindrance-dice-empty.png',
'systems/fvtt-pegasus-rpg/images/dice/hindrance-dice-empty.png',
'systems/fvtt-pegasus-rpg/images/dice/hindrance-dice-empty.png',
'systems/fvtt-pegasus-rpg/images/dice/hindrance-dice-empty.png',
'systems/fvtt-pegasus-rpg/images/dice/hindrance-dice-empty.png',
'systems/fvtt-pegasus-rpg/images/dice/hindrance-dice.png'
],
system: "pegasus-hindrance"
}, "d6")
})
2022-01-30 09:44:37 +01:00
Hooks.on("dropCanvasData", (canvas, data) => {
PegasusUtility.dropItemOnToken(canvas, data)
});
2022-02-10 21:58:19 +01:00
2021-12-02 07:38:59 +01:00
this.rollDataStore = {}
this.defenderStore = {}
2021-12-02 20:18:21 +01:00
this.diceList = [];
this.diceFoundryList = [];
2022-10-03 10:01:41 +02:00
this.optionsDiceList = ""
this.lastRoleEffectProcess = Date.now()
this.buildDiceLists()
PegasusCommands.init()
2022-01-12 16:25:55 +01:00
2022-02-16 12:14:34 +01:00
Handlebars.registerHelper('count', function (list) {
2022-08-16 21:21:37 +02:00
return (list) ? list.length : 0;
2022-03-12 16:35:32 +01:00
})
2022-02-16 12:14:34 +01:00
Handlebars.registerHelper('includes', function (array, val) {
return array.includes(val);
2022-03-12 16:35:32 +01:00
})
2022-01-12 16:25:55 +01:00
Handlebars.registerHelper('upper', function (text) {
return text.toUpperCase();
2022-03-12 16:35:32 +01:00
})
2022-03-06 20:07:41 +01:00
Handlebars.registerHelper('lower', function (text) {
return text.toLowerCase()
2022-03-12 16:35:32 +01:00
})
2022-01-12 17:21:37 +01:00
Handlebars.registerHelper('upperFirst', function (text) {
if (typeof text !== 'string') return text
return text.charAt(0).toUpperCase() + text.slice(1)
2022-03-12 16:35:32 +01:00
})
2022-01-28 17:27:01 +01:00
Handlebars.registerHelper('notEmpty', function (list) {
return list.length > 0;
2022-03-12 16:35:32 +01:00
})
Handlebars.registerHelper('mul', function (a, b) {
return parseInt(a) * parseInt(b);
})
Handlebars.registerHelper('add', function (a, b) {
return parseInt(a) + parseInt(b);
});
Handlebars.registerHelper('sub', function (a, b) {
return parseInt(a) - parseInt(b);
})
2022-09-28 20:04:04 +02:00
Handlebars.registerHelper('getDice', function (a) {
return PegasusUtility.getDiceFromLevel(a)
})
2023-09-11 20:34:33 +02:00
Handlebars.registerHelper('getStatusConfig', function (a) {
let key = a + "Status"
2023-09-13 08:06:08 +02:00
//console.log("TABE", key, game.system.pegasus.config[key] )
2023-09-11 20:34:33 +02:00
return game.system.pegasus.config[key]
})
2023-09-13 08:06:08 +02:00
Handlebars.registerHelper('valueAtIndex', function (arr, idx) {
return arr[idx];
})
Handlebars.registerHelper('for', function (from, to, incr, block) {
let accum = '';
for (let i = from; i <= to; i += incr)
accum += block.fn(i);
return accum;
})
Handlebars.registerHelper('isGM', function () {
return game.user.isGM
})
2023-09-13 17:37:30 +02:00
Handlebars.registerHelper('isCharacter', function (id) {
return game.combat.isCharacter(id)
})
2022-09-29 13:07:57 +02:00
2022-01-28 10:05:54 +01:00
}
2022-01-06 18:22:05 +01:00
2022-08-14 15:27:54 +02:00
/* -------------------------------------------- */
static initGenericRoll() {
let rollData = PegasusUtility.getBasicRollData()
2022-09-21 16:54:34 +02:00
rollData.alias = "Dice Pool Roll",
rollData.mode = "generic"
2022-08-14 15:27:54 +02:00
rollData.title = `Dice Pool Roll`
rollData.img = "icons/dice/d12black.svg"
rollData.isGeneric = true
rollData.diceList = PegasusUtility.getDiceList()
rollData.dicePool = []
rollData.traumaState = "none"
return rollData
}
/* -------------------------------------------- */
static async addDiceRollButton(app, html, data) {
if (app.tabName !== 'chat') return
let $chat_form = html.find('#chat-form')
const template = 'systems/fvtt-pegasus-rpg/templates/chat-roll-button.html'
renderTemplate(template, {}).then(c => {
if (c.length > 0) {
let $content = $(c)
$chat_form.before($content)
$content.find('#pegasus-chat-roll-button').on('click', async event => {
event.preventDefault()
let rollData = PegasusUtility.initGenericRoll()
rollData.isChatRoll = true
2022-09-21 16:54:34 +02:00
let rollDialog = await PegasusRollDialog.create(undefined, rollData)
rollDialog.render(true)
2022-08-14 15:27:54 +02:00
})
2022-09-21 16:54:34 +02:00
2022-08-14 15:27:54 +02:00
}
})
2022-09-21 16:54:34 +02:00
}
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
2022-01-28 10:05:54 +01:00
static pushInitiativeOptions(html, options) {
2022-01-30 09:44:37 +01:00
options.push({ name: "Apply -10", condition: true, icon: '<i class="fas fa-plus"></i>', callback: target => { PegasusCombat.decInitBy10(target.data('combatant-id'), -10); } })
2022-01-28 10:05:54 +01:00
}
2022-09-27 20:31:01 +02:00
/* -------------------------------------------- */
2022-09-29 13:07:57 +02:00
static getRangeText(rangeKey) {
2022-09-27 20:31:01 +02:00
return __rangeKeyToText[rangeKey] || "N/A"
}
2022-01-28 10:05:54 +01:00
/* -------------------------------------------- */
2022-07-10 10:22:04 +02:00
static getDiceList() {
2022-07-19 00:18:46 +02:00
return [{ key: "d4", level: 1, img: "systems/fvtt-pegasus-rpg/images/dice/d4.webp" }, { key: "d6", level: 2, img: "systems/fvtt-pegasus-rpg/images/dice/d6.webp" },
{ key: "d8", level: 3, img: "systems/fvtt-pegasus-rpg/images/dice/d8.webp" }, { key: "d10", level: 4, img: "systems/fvtt-pegasus-rpg/images/dice/d10.webp" },
{ key: "d12", level: 5, img: "systems/fvtt-pegasus-rpg/images/dice/d12.webp" }]
2022-07-10 10:22:04 +02:00
}
2022-09-25 09:26:12 +02:00
/* -------------------------------------------- */
static buildDicePool(name, level, mod = 0, effectName = undefined) {
let dicePool = []
let diceKey = PegasusUtility.getDiceFromLevel(level)
let diceList = diceKey.split(" ")
for (let myDice of diceList) {
2023-09-13 08:06:08 +02:00
let myDiceTrim = myDice.trim()
2022-09-25 09:26:12 +02:00
let newDice = {
2023-09-13 08:06:08 +02:00
name: name, key: myDiceTrim, level: PegasusUtility.getLevelFromDice(myDiceTrim), mod: mod, effect: effectName,
img: `systems/fvtt-pegasus-rpg/images/dice/${myDiceTrim}.webp`
2022-09-25 09:26:12 +02:00
}
dicePool.push(newDice)
mod = 0 // Only first dice has modifier
}
return dicePool
}
2022-07-10 10:22:04 +02:00
/* -------------------------------------------- */
2022-07-19 20:51:48 +02:00
static updateEffectsBonusDice(rollData) {
2022-07-10 10:22:04 +02:00
let newDicePool = rollData.dicePool.filter(dice => dice.name != "effect-bonus-dice")
for (let effect of rollData.effectsList) {
2023-09-13 08:06:08 +02:00
if (effect?.applied && effect.type == "effect" && !effect.effect?.system?.hindrance && effect.effect && effect.effect.system.bonusdice) {
2022-09-29 13:07:57 +02:00
newDicePool = newDicePool.concat(this.buildDicePool("effect-bonus-dice", effect.effect.system.effectlevel, 0, effect.effect.name))
2022-07-10 10:22:04 +02:00
}
2023-09-13 08:06:08 +02:00
if (effect?.applied && effect.type == "effect" && effect.value && effect.isdynamic && !effect.effect?.system?.hindrance) {
2022-09-29 13:07:57 +02:00
newDicePool = newDicePool.concat(this.buildDicePool("effect-bonus-dice", effect.value, 0, effect.name))
2022-09-28 20:14:24 +02:00
}
2022-07-10 10:22:04 +02:00
}
rollData.dicePool = newDicePool
}
2022-07-13 22:47:07 +02:00
/* -------------------------------------------- */
2022-07-19 20:51:48 +02:00
static updateHindranceBonusDice(rollData) {
let newDicePool = rollData.dicePool.filter(dice => dice.name != "effect-hindrance")
for (let hindrance of rollData.effectsList) {
2023-09-13 08:06:08 +02:00
if (hindrance?.applied && (hindrance.type == "hindrance" || (hindrance.type == "effect" && hindrance.effect?.system?.hindrance))) {
2022-10-05 14:38:31 +02:00
console.log("Adding Hindrance 1", hindrance, newDicePool)
2022-09-29 13:07:57 +02:00
newDicePool = newDicePool.concat(this.buildDicePool("effect-hindrance", (hindrance.value) ? hindrance.value : hindrance.effect.system.effectlevel, 0, hindrance.name))
2022-10-05 14:38:31 +02:00
console.log("Adding Hindrance 2", newDicePool)
2022-07-19 20:51:48 +02:00
}
}
rollData.dicePool = newDicePool
}
/* -------------------------------------------- */
static updateArmorDicePool(rollData) {
2022-07-19 21:56:59 +02:00
let newDicePool = rollData.dicePool.filter(dice => dice.name != "armor-shield")
2022-07-19 20:51:48 +02:00
for (let armor of rollData.armorsList) {
2022-07-19 21:56:59 +02:00
if (armor.applied) {
2022-09-29 13:07:57 +02:00
newDicePool = newDicePool.concat(this.buildDicePool("armor-shield", armor.value, 0))
2022-09-25 09:26:12 +02:00
}
}
2022-10-05 09:13:17 +02:00
newDicePool = newDicePool.filter(dice => dice.name != "vehicle-shield")
2022-09-25 09:26:12 +02:00
for (let shield of rollData.vehicleShieldList) {
if (shield.applied) {
2022-09-29 13:07:57 +02:00
newDicePool = newDicePool.concat(this.buildDicePool("vehicle-shield", shield.value, 0))
2022-07-13 22:47:07 +02:00
}
2022-07-19 20:51:48 +02:00
}
2022-10-05 09:13:17 +02:00
console.log(">>>>Dicepoool", newDicePool)
2022-07-19 20:51:48 +02:00
rollData.dicePool = newDicePool
}
/* -------------------------------------------- */
static updateDamageDicePool(rollData) {
if (rollData.isDamage) {
let newDicePool = rollData.dicePool.filter(dice => dice.name != "damage")
for (let weapon of rollData.weaponsList) {
if (weapon.applied && weapon.type == "damage") {
2022-09-29 13:07:57 +02:00
newDicePool = newDicePool.concat(this.buildDicePool("damage", weapon.value, 0))
2022-09-21 16:54:34 +02:00
}
}
for (let weapon of rollData.vehicleWeapons) {
if (weapon.applied) {
2022-09-29 13:07:57 +02:00
newDicePool = newDicePool.concat(this.buildDicePool("damage", weapon.value, 0))
2023-09-13 21:25:44 +02:00
if (weapon.weapon.system.extradamage) {
for (let i = 0; i < weapon.weapon.system.extradamagevalue; i++) {
newDicePool = newDicePool.concat(this.buildDicePool("damage", 5, 0))
2022-11-11 08:58:00 +01:00
}
}
2022-07-19 20:51:48 +02:00
}
}
2022-07-13 22:47:07 +02:00
rollData.dicePool = newDicePool
2022-09-29 13:07:57 +02:00
}
2022-07-13 22:47:07 +02:00
}
2022-07-20 23:53:37 +02:00
/* -------------------------------------------- */
2022-09-21 16:54:34 +02:00
static updateStatDicePool(rollData) {
2022-07-20 23:53:37 +02:00
let newDicePool = rollData.dicePool.filter(dice => dice.name != "stat")
let statDice = rollData.dicePool.find(dice => dice.name == "stat")
2022-07-28 18:45:04 +02:00
if (statDice.level > 0) {
2022-09-29 13:07:57 +02:00
newDicePool = newDicePool.concat(this.buildDicePool("stat", rollData.statDicesLevel, statDice.mod))
2022-09-25 09:26:12 +02:00
}
2022-09-29 13:07:57 +02:00
2022-09-25 09:26:12 +02:00
if (rollData.vehicleStat) {
newDicePool = rollData.dicePool.filter(dice => dice.name != "vehiclestat")
2022-09-29 13:07:57 +02:00
if (rollData.vehicleStat.currentlevel > 0) {
newDicePool = newDicePool.concat(this.buildDicePool("vehiclestat", rollData.vehicleStat.currentlevel, 0))
2022-07-20 23:53:37 +02:00
}
2022-09-25 09:26:12 +02:00
rollData.dicePool = newDicePool
2022-07-20 23:53:37 +02:00
}
}
2022-07-10 10:22:04 +02:00
/* -------------------------------------------- */
static updateSpecDicePool(rollData) {
let newDicePool = rollData.dicePool.filter(dice => dice.name != "spec")
if (rollData.specDicesLevel > 0) {
2022-09-29 13:07:57 +02:00
newDicePool = newDicePool.concat(this.buildDicePool("spec", rollData.specDicesLevel, 0))
2022-07-10 10:22:04 +02:00
}
rollData.dicePool = newDicePool
}
/* -------------------------------------------- */
2022-07-19 00:18:46 +02:00
static addDicePool(rollData, diceKey, level) {
2022-07-10 10:22:04 +02:00
let newDice = {
2022-07-19 00:18:46 +02:00
name: "dice-click", key: diceKey, level: level,
2022-07-10 10:22:04 +02:00
img: `systems/fvtt-pegasus-rpg/images/dice/${diceKey}.webp`
}
rollData.dicePool.push(newDice)
}
2022-07-19 20:51:48 +02:00
2022-07-10 10:22:04 +02:00
/*-------------------------------------------- */
2022-07-19 20:51:48 +02:00
static removeFromDicePool(rollData, diceIdx) {
2022-07-10 10:22:04 +02:00
let toRemove = rollData.dicePool[diceIdx]
2022-07-13 22:47:07 +02:00
if (toRemove && toRemove.name != "spec" && toRemove.name != "stat" && toRemove.name != "damage") {
2022-07-10 10:22:04 +02:00
let newDicePool = []
2022-07-19 20:51:48 +02:00
for (let i = 0; i < rollData.dicePool.length; i++) {
if (i != diceIdx) {
newDicePool.push(rollData.dicePool[i])
2022-07-10 10:22:04 +02:00
}
}
rollData.dicePool = newDicePool
if (toRemove.name == "effect-bonus-dice") {
for (let effect of rollData.effectsList) {
2022-07-19 20:51:48 +02:00
if (effect.effect.name == toRemove.effect && effect.applied) {
2022-07-10 10:22:04 +02:00
effect.applied = false //Remove the effect
}
}
}
}
}
/*-------------------------------------------- */
2022-01-28 10:05:54 +01:00
static getSpecs() {
2021-12-02 07:38:59 +01:00
return this.specs;
}
/* -------------------------------------------- */
static async ready() {
const specs = await PegasusUtility.loadCompendium("fvtt-pegasus-rpg.specialisations");
this.specs = specs.map(i => i.toObject());
2022-10-03 10:01:41 +02:00
if (game.user.isGM) {
Hooks.on('sightRefresh', (app, html, data) => PegasusUtility.refreshSightForEffect(app, html, data))
}
2021-12-02 07:38:59 +01:00
}
2022-03-06 20:07:41 +01:00
/* -------------------------------------------- */
static async addItemDropToActor(actor, item) {
2022-09-29 13:07:57 +02:00
console.log("ITEM DROPPED", actor, item)
2022-03-06 20:07:41 +01:00
actor.preprocessItem("none", item, false)
let chatData = {
user: game.user.id,
rollMode: game.settings.get("core", "rollMode"),
whisper: [game.user.id].concat(ChatMessage.getWhisperRecipients('GM')),
content: `<div>The item ${item.name} has been dropped on the actor ${actor.name}</div`
}
ChatMessage.create(chatData);
}
2022-01-30 09:44:37 +01:00
/* -------------------------------------------- */
static async dropItemOnToken(canvas, data) {
if (data.type != "Item") {
return
}
2022-02-10 21:58:19 +01:00
2022-01-30 09:44:37 +01:00
const position = canvas.grid.getTopLeft(data.x, data.y)
let x = position[0]
let y = position[1]
const tokensList = [...canvas.tokens.placeables]
2022-02-10 21:58:19 +01:00
for (let token of tokensList) {
if (x >= token.x && x <= (token.x + token.width)
&& y >= token.y && y <= (token.y + token.height)) {
2022-09-29 13:07:57 +02:00
const item = fromUuidSync(data.uuid)
if (item == undefined) {
item = this.actor.items.get(data.uuid)
}
let itemFull = await PegasusUtility.searchItem(item)
2022-09-29 20:04:27 +02:00
//console.log("DROPPED DATA", data.uuid)
2022-03-06 20:07:41 +01:00
if (game.user.isGM || token.actor.isOwner) {
2022-09-29 13:07:57 +02:00
this.addItemDropToActor(token.actor, itemFull)
2022-03-06 20:07:41 +01:00
} else {
2022-09-29 13:07:57 +02:00
game.socket.emit("system.fvtt-pegasus-rpg", { name: "msg_gm_item_drop", data: { actorId: token.actor.id, itemId: itemFull.id, isPack: item.pack } })
2022-03-06 20:07:41 +01:00
}
2022-02-10 21:58:19 +01:00
return
2022-01-30 09:44:37 +01:00
}
}
}
2022-01-06 18:22:05 +01:00
/* -------------------------------------------- */
static async loadCompendiumData(compendium) {
2022-09-05 11:32:21 +02:00
const pack = game.packs.get(compendium)
console.log("PACK", pack, compendium)
return await pack?.getDocuments() ?? []
2022-01-06 18:22:05 +01:00
}
/* -------------------------------------------- */
static async loadCompendium(compendium, filter = item => true) {
2022-09-05 11:32:21 +02:00
let compendiumData = await PegasusUtility.loadCompendiumData(compendium)
//console.log("Comp data", compendiumData)
return compendiumData.filter(filter)
2022-01-06 18:22:05 +01:00
}
2021-12-02 20:18:21 +01:00
/* -------------------------------------------- */
static buildDiceLists() {
let maxLevel = game.settings.get("fvtt-pegasus-rpg", "dice-max-level");
2022-01-28 10:05:54 +01:00
let diceList = ["0"];
2022-01-11 23:35:23 +01:00
let diceValues = [0];
2022-01-28 10:05:54 +01:00
let diceFoundryList = ["d0"];
2021-12-02 20:18:21 +01:00
let diceLevel = 1;
let concat = "";
let concatFoundry = "";
let optionsDiceList = '<option value="0">0</option>';
let optionsLevel = '<option value="0">0</option>';
2022-01-28 10:05:54 +01:00
for (let i = 1; i <= maxLevel; i++) {
2021-12-02 20:18:21 +01:00
let currentDices = concat + __level2Dice[diceLevel];
2022-01-28 10:05:54 +01:00
diceList.push(currentDices);
diceFoundryList.push(concatFoundry + __level2Dice[diceLevel] + "x");
if (__level2Dice[diceLevel] == "d12") {
concat = concat + "d12 ";
concatFoundry = concatFoundry + "d12x, ";
2021-12-02 20:18:21 +01:00
diceLevel = 1;
} else {
diceLevel++;
}
optionsDiceList += `<option value="${i}">${currentDices}</option>`;
optionsLevel += `<option value="${i}">${i}</option>`;
}
this.diceList = diceList;
this.diceFoundryList = diceFoundryList;
this.optionsDiceList = optionsDiceList;
this.optionsLevel = optionsLevel;
2021-12-05 20:36:34 +01:00
2021-12-02 20:18:21 +01:00
}
2022-01-28 10:05:54 +01:00
2021-12-02 20:18:21 +01:00
/* -------------------------------------------- */
static getOptionsDiceList() {
return this.optionsDiceList;
}
/* -------------------------------------------- */
static getOptionsLevel() {
return this.optionsLevel;
}
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
static computeAttackDefense(defenseRollId) {
2022-01-28 10:05:54 +01:00
let defenseRollData = this.getRollData(defenseRollId);
2021-12-02 07:38:59 +01:00
let attackRollData = this.getRollData(defenseRollData.linkedRollId);
2022-01-28 10:05:54 +01:00
let defender = game.actors.get(defenseRollData.actorId);
2021-12-02 07:38:59 +01:00
defender.processDefenseResult(defenseRollData, attackRollData);
}
/* -------------------------------------------- */
2022-01-28 10:05:54 +01:00
static applyDamage(defenseRollId) {
let defenseRollData = this.getRollData(defenseRollId);
let defender = game.actors.get(defenseRollData.actorId);
defender.applyDamageLoss(defenseRollData.finalDamage);
2021-12-02 07:38:59 +01:00
}
/* -------------------------------------------- */
2022-01-28 10:05:54 +01:00
static applyNoDefense(actorId, attackRollId) {
let attackRollData = this.getRollData(attackRollId);
let defender = game.actors.get(actorId);
defender.processNoDefense(attackRollData);
2021-12-02 07:38:59 +01:00
}
2022-07-19 21:56:59 +02:00
/* -------------------------------------------- */
static rerollHeroRemaining(userId, rollId) {
if (game.user.isGM) {
let user = game.users.get(userId)
let character = user.character
if (!character) {
ui.notifications.warn(`No character linked to the player : reroll not allowed.`)
return
}
2022-08-14 15:27:54 +02:00
console.log("Going to reroll", character, rollId)
2022-07-19 21:56:59 +02:00
let rollData = this.getRollData(rollId)
if (character.getLevelRemaining() > 0) {
rollData.rerollHero = true
this.rollPegasus(rollData)
character.modifyHeroLevelRemaining(-1)
} else {
2022-09-16 17:36:58 +02:00
ui.notifications.warn(`No more Hero Level for ${character.name} ! Unable to reroll.`)
2022-07-19 21:56:59 +02:00
}
}
}
/* -------------------------------------------- */
static sendRerollHeroRemaining(userId, rollId) {
game.socket.emit("system.fvtt-pegasus-rpg", { name: "msg_reroll_hero", data: { userId: userId, rollId: rollId } })
}
2022-08-14 15:27:54 +02:00
/* -------------------------------------------- */
2022-09-21 16:54:34 +02:00
static targetToken(user, token, flag) {
2022-08-14 15:27:54 +02:00
if (flag) {
token.actor.checkIfPossible()
}
}
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
static async chatListeners(html) {
2022-01-06 18:22:05 +01:00
html.on("click", '.chat-create-actor', event => {
2022-01-28 10:05:54 +01:00
game.system.pegasus.creator.processChatEvent(event);
2022-07-13 22:47:07 +02:00
})
2022-01-13 21:05:55 +01:00
html.on("click", '.view-item-from-chat', event => {
2022-01-28 10:05:54 +01:00
game.system.pegasus.creator.openItemView(event)
2022-07-13 22:47:07 +02:00
})
html.on("click", '.reroll-level-remaining', event => {
let rollId = $(event.currentTarget).data("roll-id")
2022-07-19 21:56:59 +02:00
if (game.user.isGM) {
PegasusUtility.rerollHeroRemaining(game.user.id, rollId)
} else {
PegasusUtility.sendRerollHeroRemaining(game.user.id, rollId)
}
2022-07-13 22:47:07 +02:00
})
2022-07-19 20:51:48 +02:00
2021-12-02 07:38:59 +01:00
}
2022-01-28 10:05:54 +01:00
/* -------------------------------------------- */
2021-12-02 07:38:59 +01:00
static async preloadHandlebarsTemplates() {
2022-01-28 10:05:54 +01:00
2021-12-02 07:38:59 +01:00
const templatePaths = [
'systems/fvtt-pegasus-rpg/templates/editor-notes-gm.html',
2022-01-28 10:05:54 +01:00
'systems/fvtt-pegasus-rpg/templates/partial-roll-select-effects.html',
2021-12-02 07:38:59 +01:00
'systems/fvtt-pegasus-rpg/templates/partial-options-statistics.html',
'systems/fvtt-pegasus-rpg/templates/partial-options-level.html',
2021-12-05 20:36:34 +01:00
'systems/fvtt-pegasus-rpg/templates/partial-options-range.html',
2022-02-16 17:43:51 +01:00
'systems/fvtt-pegasus-rpg/templates/partial-options-equipment-types.html',
2022-03-06 20:07:41 +01:00
'systems/fvtt-pegasus-rpg/templates/partial-equipment-effects.html',
'systems/fvtt-pegasus-rpg/templates/partial-actor-stat-block.html',
2022-03-06 22:18:08 +01:00
'systems/fvtt-pegasus-rpg/templates/partial-actor-status.html',
2022-09-05 08:57:02 +02:00
'systems/fvtt-pegasus-rpg/templates/partial-vehicle-stat-block.html',
'systems/fvtt-pegasus-rpg/templates/partial-vehicle-arc.html',
2022-03-06 22:18:08 +01:00
'systems/fvtt-pegasus-rpg/templates/partial-item-nav.html',
'systems/fvtt-pegasus-rpg/templates/partial-item-description.html',
2022-08-16 21:21:37 +02:00
'systems/fvtt-pegasus-rpg/templates/partial-actor-equipment.html',
"systems/fvtt-pegasus-rpg/templates/partial-options-vehicle-speed.html"
2021-12-02 07:38:59 +01:00
]
2022-01-28 10:05:54 +01:00
return loadTemplates(templatePaths);
2021-12-02 07:38:59 +01:00
}
2022-02-10 21:58:19 +01:00
/* -------------------------------------------- */
static async getEffectFromCompendium(effectName) {
effectName = effectName.toLowerCase()
2022-03-06 20:07:41 +01:00
let effect = game.items.contents.find(item => item.type == 'effect' && item.name.toLowerCase() == effectName)
2022-09-21 16:54:34 +02:00
if (!effect) {
2022-09-05 11:32:21 +02:00
let effects = await this.loadCompendium('fvtt-pegasus-rpg.effects', item => item.name.toLowerCase() == effectName)
2022-02-10 21:58:19 +01:00
let objs = effects.map(i => i.toObject())
effect = objs[0]
} else {
effect = duplicate(effect);
}
console.log("Effect", effect)
return effect
}
2022-01-28 10:05:54 +01:00
/* -------------------------------------------- */
static removeChatMessageId(messageId) {
if (messageId) {
game.messages.get(messageId)?.delete();
2022-01-07 20:40:40 +01:00
}
2022-01-28 10:05:54 +01:00
}
static findChatMessageId(current) {
return PegasusUtility.getChatMessageId(PegasusUtility.findChatMessage(current));
}
static getChatMessageId(node) {
return node?.attributes.getNamedItem('data-message-id')?.value;
}
static findChatMessage(current) {
return PegasusUtility.findNodeMatching(current, it => it.classList.contains('chat-message') && it.attributes.getNamedItem('data-message-id'));
}
static findNodeMatching(current, predicate) {
if (current) {
if (predicate(current)) {
return current;
2022-01-07 20:40:40 +01:00
}
2022-01-28 10:05:54 +01:00
return PegasusUtility.findNodeMatching(current.parentElement, predicate);
2022-01-07 20:40:40 +01:00
}
2022-01-28 10:05:54 +01:00
return undefined;
}
2022-01-10 08:00:27 +01:00
/* -------------------------------------------- */
2022-01-28 10:05:54 +01:00
static getDiceValue(level = 0) {
2022-01-11 23:35:23 +01:00
let diceString = this.diceList[level]
2022-07-10 10:22:04 +02:00
if (!diceString) {
2022-07-13 22:47:07 +02:00
return __name2DiceValue[level]
2022-03-16 15:08:34 +01:00
}
2022-01-11 23:35:23 +01:00
let diceTab = diceString.split(" ")
2022-01-10 08:00:27 +01:00
let diceValue = 0
2022-01-11 23:35:23 +01:00
for (let dice of diceTab) {
diceValue += __name2DiceValue[dice]
2022-01-10 08:00:27 +01:00
}
return diceValue
}
2022-07-28 18:45:04 +02:00
/* -------------------------------------------- */
static getLevelFromDice(dice) {
return __dice2Level[dice]
}
2022-01-10 08:00:27 +01:00
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
2021-12-02 20:18:21 +01:00
static getDiceFromLevel(level = 0) {
2023-09-13 21:25:44 +02:00
level = Math.max(Number(level), 0)
2021-12-02 20:18:21 +01:00
return this.diceList[level];
2021-12-02 07:38:59 +01:00
}
/* -------------------------------------------- */
2021-12-02 20:18:21 +01:00
static getFoundryDiceFromLevel(level = 0) {
level = Number(level)
2022-01-28 10:05:54 +01:00
//console.log(this.diceFoundryList);
2021-12-02 20:18:21 +01:00
return this.diceFoundryList[level];
2021-12-02 07:38:59 +01:00
}
2022-01-28 10:05:54 +01:00
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
2022-01-28 10:05:54 +01:00
static createDirectOptionList(min, max) {
2021-12-02 07:38:59 +01:00
let options = {};
2022-01-28 10:05:54 +01:00
for (let i = min; i <= max; i++) {
2021-12-02 07:38:59 +01:00
options[`${i}`] = `${i}`;
}
return options;
}
/* -------------------------------------------- */
static buildListOptions(min, max) {
let options = ""
for (let i = min; i <= max; i++) {
options += `<option value="${i}">${i}</option>`
}
return options;
}
/* -------------------------------------------- */
static getTarget() {
2022-08-14 15:27:54 +02:00
if (game.user.targets) {
2021-12-02 07:38:59 +01:00
for (let target of game.user.targets) {
2022-07-27 22:26:48 +02:00
return target
2021-12-02 07:38:59 +01:00
}
}
return undefined;
}
2022-01-28 10:05:54 +01:00
2022-09-29 20:04:27 +02:00
/* -------------------------------------------- */
static computeDistance() {
let mytarget = game.user.targets.first()
console.log("target", mytarget, mytarget)
let mytoken = _token
if (mytarget) {
let dist = canvas.grid.measureDistances(
[{ ray: new Ray(mytoken.center, mytarget.center) }],
{ gridSpaces: true });
console.log("DIST", dist)
} else {
console.log("NO TARGET")
}
}
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
2022-01-28 10:05:54 +01:00
static getDefenseState(actorId) {
2021-12-02 07:38:59 +01:00
return this.defenderStore[actorId];
}
2022-01-28 10:05:54 +01:00
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
2022-07-27 22:26:48 +02:00
static async updateDefenseState(defenderTokenId, rollId) {
this.defenderStore[defenderTokenId] = rollId
let defender = game.canvas.tokens.get(defenderTokenId).actor
if (game.user.character && game.user.character.id == defender.id) {
2021-12-02 07:38:59 +01:00
let chatData = {
user: game.user.id,
2022-01-28 10:05:54 +01:00
alias: defender.name,
2021-12-02 07:38:59 +01:00
rollMode: game.settings.get("core", "rollMode"),
2022-01-28 10:05:54 +01:00
whisper: [game.user.id].concat(ChatMessage.getWhisperRecipients('GM')),
2021-12-02 07:38:59 +01:00
content: `<div>${defender.name} is under attack. He must roll a skill/weapon/technique to defend himself or suffer damages (button below).
2022-07-27 22:26:48 +02:00
<button class="chat-card-button apply-nodefense" data-actor-id="${defender.id}" data-roll-id="${rollId}" >No defense</button></div`
2022-01-28 10:05:54 +01:00
};
2021-12-02 07:38:59 +01:00
//console.log("Apply damage chat", chatData );
2022-01-28 10:05:54 +01:00
await ChatMessage.create(chatData);
2021-12-02 07:38:59 +01:00
}
}
/* -------------------------------------------- */
2022-01-28 10:05:54 +01:00
static updateRollData(rollData) {
2022-07-19 00:18:46 +02:00
let id = rollData.rollId
let oldRollData = this.rollDataStore[id] || {}
let newRollData = mergeObject(oldRollData, rollData)
2022-08-14 15:27:54 +02:00
console.log("Rolldata saved", id)
2022-07-19 00:18:46 +02:00
this.rollDataStore[id] = newRollData
2021-12-02 07:38:59 +01:00
}
/* -------------------------------------------- */
2022-01-28 10:05:54 +01:00
static saveRollData(rollData) {
2022-08-14 15:27:54 +02:00
game.socket.emit("system.fvtt-pegasus-rpg", {
2022-01-28 10:05:54 +01:00
name: "msg_update_roll", data: rollData
}); // Notify all other clients of the roll
2022-07-19 20:51:48 +02:00
this.updateRollData(rollData)
2021-12-02 07:38:59 +01:00
}
/* -------------------------------------------- */
2022-01-28 10:05:54 +01:00
static getRollData(id) {
2022-07-19 00:18:46 +02:00
return this.rollDataStore[id]
2021-12-02 07:38:59 +01:00
}
/* -------------------------------------------- */
2022-03-06 20:07:41 +01:00
static async onSocketMesssage(msg) {
console.log("SOCKET MESSAGE", msg.name)
2022-01-28 10:05:54 +01:00
if (msg.name == "msg_update_defense_state") {
2022-07-27 22:26:48 +02:00
this.updateDefenseState(msg.data.defenderTokenId, msg.data.rollId)
2022-01-28 10:05:54 +01:00
}
if (msg.name == "msg_update_roll") {
2022-03-06 20:07:41 +01:00
this.updateRollData(msg.data)
}
if (msg.name == "msg_gm_item_drop" && game.user.isGM) {
2022-07-10 10:22:04 +02:00
let actor = game.actors.get(msg.data.actorId)
2022-03-06 20:07:41 +01:00
let item
if (msg.data.isPack) {
item = await fromUuid("Compendium." + msg.data.isPack + "." + msg.data.itemId)
} else {
item = game.items.get(msg.data.itemId)
}
2022-07-10 10:22:04 +02:00
this.addItemDropToActor(actor, item)
2021-12-02 07:38:59 +01:00
}
2022-07-19 21:56:59 +02:00
if (msg.name == "msg_reroll_hero") {
2022-08-14 15:27:54 +02:00
console.log("Reroll requested")
2022-07-19 21:56:59 +02:00
this.rerollHeroRemaining(msg.data.userId, msg.data.rollId)
}
2022-07-20 23:53:37 +02:00
if (msg.name == "msg_gm_remove_effect") {
this.removeForeignEffect(msg.data)
}
2021-12-02 07:38:59 +01:00
}
2022-07-20 23:53:37 +02:00
/* -------------------------------------------- */
2022-09-21 16:54:34 +02:00
static removeForeignEffect(effectData) {
2022-07-20 23:53:37 +02:00
if (game.user.isGM) {
console.log("Remote removal of effects", effectData)
2022-07-27 22:26:48 +02:00
let actor = game.canvas.tokens.get(effectData.defenderTokenId).actor
2022-07-20 23:53:37 +02:00
actor.deleteEmbeddedDocuments('Item', [effectData.effect._id])
}
}
2022-07-27 22:26:48 +02:00
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
static chatDataSetup(content, modeOverride, isRoll = false, forceWhisper) {
let chatData = {
user: game.user.id,
rollMode: modeOverride || game.settings.get("core", "rollMode"),
content: content
};
if (["gmroll", "blindroll"].includes(chatData.rollMode)) chatData["whisper"] = ChatMessage.getWhisperRecipients("GM").map(u => u.id);
if (chatData.rollMode === "blindroll") chatData["blind"] = true;
else if (chatData.rollMode === "selfroll") chatData["whisper"] = [game.user];
if (forceWhisper) { // Final force !
chatData["speaker"] = ChatMessage.getSpeaker();
chatData["whisper"] = ChatMessage.getWhisperRecipients(forceWhisper);
}
return chatData;
}
2022-01-28 10:05:54 +01:00
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
static async showDiceSoNice(roll, rollMode) {
if (game.modules.get("dice-so-nice")?.active) {
if (game.dice3d) {
let whisper = null;
let blind = false;
rollMode = rollMode ?? game.settings.get("core", "rollMode");
switch (rollMode) {
case "blindroll": //GM only
blind = true;
case "gmroll": //GM + rolling player
whisper = this.getUsers(user => user.isGM);
break;
case "roll": //everybody
whisper = this.getUsers(user => user.active);
break;
case "selfroll":
whisper = [game.user.id];
break;
}
await game.dice3d.showForRoll(roll, game.user, true, whisper, blind);
}
}
}
2022-02-10 19:03:09 +01:00
/* -------------------------------------------- */
2022-02-10 21:58:19 +01:00
static removeUsedPerkEffects(rollData) {
2022-02-10 19:03:09 +01:00
// De-actived used effects from perks
let toRem = []
2022-02-10 21:58:19 +01:00
for (let effect of rollData.effectsList) {
2022-09-04 09:58:51 +02:00
if (effect.effect.system.perkId && effect.effect.system.isUsed) {
2022-02-10 21:58:19 +01:00
toRem.push(effect.effect._id)
2022-02-10 19:03:09 +01:00
}
}
if (toRem.length > 0) {
let actor = game.actors.get(rollData.actorId)
actor.deleteEmbeddedDocuments('Item', toRem)
2022-02-10 21:58:19 +01:00
}
2022-02-10 19:03:09 +01:00
}
2021-12-02 07:38:59 +01:00
2022-07-10 10:22:04 +02:00
/* -------------------------------------------- */
static removeOneUseEffects(rollData) {
// De-actived used effects from perks
let toRem = []
for (let effect of rollData.effectsList) {
2022-09-04 09:58:51 +02:00
if (effect.effect && effect.effect.system.isUsed && effect.effect.system.oneuse) {
2022-07-27 22:26:48 +02:00
effect.defenderTokenId = rollData.defenderTokenId
2022-07-20 23:53:37 +02:00
if (effect.foreign) {
if (game.user.isGM) {
this.removeForeignEffect(effect)
2022-09-21 16:54:34 +02:00
} else {
2022-08-14 15:27:54 +02:00
game.socket.emit("system.fvtt-pegasus-rpg", { msg: "msg_gm_remove_effect", data: effect })
2022-07-20 23:53:37 +02:00
}
} else {
toRem.push(effect.effect._id)
ChatMessage.create({ content: `One used effect ${effect.effect.name} has been auto-deleted.` })
}
2022-07-10 10:22:04 +02:00
}
}
if (toRem.length > 0) {
2022-07-19 00:18:46 +02:00
//console.log("Going to remove one use effects", toRem)
2022-07-10 10:22:04 +02:00
let actor = game.actors.get(rollData.actorId)
actor.deleteEmbeddedDocuments('Item', toRem)
}
}
2022-07-19 00:18:46 +02:00
/* -------------------------------------------- */
static async momentumReroll(actorId) {
let actor = game.actors.get(actorId)
let rollData = actor.lastRoll
if (rollData) {
rollData.rerollMomentum = true
PegasusUtility.rollPegasus(rollData)
this.actor.lastRoll = undefined
} else {
ui.notifications.warn("No last roll registered....")
}
}
/* -------------------------------------------- */
2022-07-19 20:51:48 +02:00
static async showMomentumDialog(actorId) {
2022-07-19 00:18:46 +02:00
let d = new Dialog({
title: "Momentum reroll",
content: "<p>Do you want to re-roll your last roll ?</p>",
buttons: {
2022-07-19 20:51:48 +02:00
one: {
icon: '<i class="fas fa-check"></i>',
label: "Cancel",
2022-08-01 21:38:42 +02:00
callback: () => d.close()
2022-07-19 20:51:48 +02:00
},
two: {
icon: '<i class="fas fa-times"></i>',
label: "Reroll",
callback: () => PegasusUtility.momentumReroll(actorId)
}
2022-07-19 00:18:46 +02:00
},
default: "Reroll",
2022-07-19 20:51:48 +02:00
})
d.render(true)
2022-07-19 00:18:46 +02:00
}
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
2022-01-28 10:05:54 +01:00
static async rollPegasus(rollData) {
2022-07-13 22:47:07 +02:00
let actor = game.actors.get(rollData.actorId)
2022-11-30 12:00:09 +01:00
if (rollData.tokenId) {
let token = canvas.tokens.placeables.find(t => t.id == rollData.tokenId)
if (token) {
actor = token.actor
}
}
2022-07-13 22:47:07 +02:00
let diceFormulaTab = []
for (let dice of rollData.dicePool) {
2022-07-19 00:18:46 +02:00
let level = dice.level
2022-07-19 20:51:48 +02:00
diceFormulaTab.push(this.getFoundryDiceFromLevel(level))
2022-07-13 22:47:07 +02:00
}
let diceFormula = '{' + diceFormulaTab.join(', ') + '}kh + ' + (rollData.stat?.mod || 0)
2021-12-02 07:38:59 +01:00
// Performs roll
2022-07-13 22:47:07 +02:00
let myRoll = rollData.roll
2022-07-19 21:56:59 +02:00
if (!myRoll || rollData.rerollHero || rollData.rerollMomentum) { // New rolls only of no rerolls
2022-07-13 22:47:07 +02:00
myRoll = new Roll(diceFormula).roll({ async: false })
2021-12-02 07:38:59 +01:00
rollData.roll = myRoll
}
2023-09-13 21:25:44 +02:00
if (rollData.hindranceDices > 0) {
2023-09-13 21:26:47 +02:00
rollData.hindranceRoll = new Roll(rollData.hindranceDices + "dh").roll({ async: false })
2023-09-13 08:06:08 +02:00
this.showDiceSoNice(rollData.hindranceRoll, game.settings.get("core", "rollMode"))
for (let res of rollData.hindranceRoll.terms[0].results) {
if (res.result == 6) {
rollData.hindranceFailure = true
2023-09-13 17:37:30 +02:00
rollData.img = `systems/fvtt-pegasus-rpg/images/dice/hindrance-dice.png`
2023-09-13 08:06:08 +02:00
}
}
2023-09-13 21:25:44 +02:00
}
2023-09-13 08:06:08 +02:00
await this.showDiceSoNice(myRoll, game.settings.get("core", "rollMode"))
2021-12-02 07:38:59 +01:00
// Final score and keep data
2023-09-13 17:37:30 +02:00
rollData.finalScore = (rollData.hindranceFailure) ? 0 : myRoll.total
2022-01-11 23:35:23 +01:00
if (rollData.damages) {
2022-01-28 10:05:54 +01:00
let dmgFormula = this.getFoundryDiceFromLevel(rollData.damages.value)
2022-07-13 22:47:07 +02:00
let dmgRoll = new Roll(dmgFormula).roll({ async: false })
await this.showDiceSoNice(dmgRoll, game.settings.get("core", "rollMode"))
rollData.dmgResult = dmgRoll.total
2022-01-11 23:35:23 +01:00
}
2022-01-28 10:05:54 +01:00
this.createChatWithRollMode(rollData.alias, {
2021-12-02 20:18:21 +01:00
content: await renderTemplate(`systems/fvtt-pegasus-rpg/templates/chat-generic-result.html`, rollData)
2021-12-02 07:38:59 +01:00
});
2022-01-28 10:05:54 +01:00
// Init stuf
if (rollData.isInit) {
2022-01-30 09:44:37 +01:00
let combat = game.combats.get(rollData.combatId)
2023-09-13 17:37:30 +02:00
await combat.setTic(rollData.combatantId, rollData)
2023-09-13 21:25:44 +02:00
combat.updateEmbeddedDocuments("Combatant", [{ _id: rollData.combatantId, initiative: rollData.finalScore }])
2021-12-02 07:38:59 +01:00
}
2022-02-10 19:03:09 +01:00
2022-09-30 11:54:48 +02:00
// Stun specific -> Suffer a stun level when dmg-res for character
2022-07-10 10:22:04 +02:00
if (rollData.subKey && rollData.subKey == "dmg-res") {
2022-07-19 20:51:48 +02:00
actor.modifyStun(+1)
2022-07-10 10:22:04 +02:00
}
2022-09-30 11:54:48 +02:00
if (rollData.isVehicleStun) {
2022-09-30 15:35:04 +02:00
actor.modifyVehicleStun(1)
2022-09-30 11:54:48 +02:00
}
2022-07-10 10:22:04 +02:00
2022-02-10 19:03:09 +01:00
//this.removeUsedPerkEffects( rollData) // Unused for now
2022-07-10 10:22:04 +02:00
this.removeOneUseEffects(rollData) // Unused for now
2022-02-10 19:03:09 +01:00
2022-01-28 10:05:54 +01:00
// And save the roll
2022-07-19 00:18:46 +02:00
this.saveRollData(rollData)
actor.lastRoll = rollData
2022-09-21 16:54:34 +02:00
console.log("Rolldata performed ", rollData, diceFormula)
2021-12-02 07:38:59 +01:00
}
/* -------------------------------------------- */
2022-01-28 10:05:54 +01:00
static getDamageDice(result) {
if (result < 0) return 0;
return Math.floor(result / 5) + 1;
2021-12-02 07:38:59 +01:00
}
/* ------------------------- ------------------- */
2022-01-28 10:05:54 +01:00
static async updateRoll(rollData) {
2021-12-02 07:38:59 +01:00
let diceResults = rollData.diceResults;
let sortedRoll = [];
2022-01-28 10:05:54 +01:00
for (let i = 0; i < 10; i++) {
2021-12-02 07:38:59 +01:00
sortedRoll[i] = 0;
}
for (let dice of diceResults) {
sortedRoll[dice.result]++;
}
let index = 0;
2022-01-28 10:05:54 +01:00
let bestRoll = 0;
for (let i = 0; i < 10; i++) {
if (sortedRoll[i] > bestRoll) {
2021-12-02 07:38:59 +01:00
bestRoll = sortedRoll[i];
index = i;
}
}
2022-03-06 20:07:41 +01:00
let bestScore = (bestRoll * 10) + index
rollData.bestScore = bestScore
rollData.finalScore = bestScore + rollData.negativeModifier + rollData.positiveModifier
2021-12-02 07:38:59 +01:00
2022-03-06 20:07:41 +01:00
this.saveRollData(rollData)
2022-01-28 10:05:54 +01:00
this.createChatWithRollMode(rollData.alias, {
2021-12-02 07:38:59 +01:00
content: await renderTemplate(`systems/fvtt-weapons-of-the-gods/templates/chat-generic-result.html`, rollData)
});
}
/* ------------------------- ------------------- */
2022-01-28 10:05:54 +01:00
static async rerollDice(actorId, diceIndex = -1) {
2021-12-02 07:38:59 +01:00
let actor = game.actors.get(actorId);
2022-01-28 10:05:54 +01:00
let rollData = actor.getRollData();
2021-12-02 07:38:59 +01:00
2022-01-28 10:05:54 +01:00
if (diceIndex == -1) {
2021-12-02 07:38:59 +01:00
rollData.hasWillpower = actor.decrementWillpower();
rollData.roll = undefined;
2022-01-28 10:05:54 +01:00
} else {
let myRoll = new Roll("1d6").roll({ async: false });
await this.showDiceSoNice(myRoll, game.settings.get("core", "rollMode"));
2021-12-02 07:38:59 +01:00
console.log("Result: ", myRoll);
rollData.roll.dice[0].results[diceIndex].result = myRoll.total; // Patch
rollData.nbStrongHitUsed++;
}
2022-01-28 10:05:54 +01:00
this.rollFraggedKingdom(rollData);
2021-12-02 07:38:59 +01:00
}
/* -------------------------------------------- */
static getUsers(filter) {
2022-09-04 09:58:51 +02:00
return game.users.filter(filter).map(user => user.id);
2021-12-02 07:38:59 +01:00
}
/* -------------------------------------------- */
static getWhisperRecipients(rollMode, name) {
switch (rollMode) {
case "blindroll": return this.getUsers(user => user.isGM);
case "gmroll": return this.getWhisperRecipientsAndGMs(name);
case "selfroll": return [game.user.id];
}
return undefined;
}
/* -------------------------------------------- */
static getWhisperRecipientsAndGMs(name) {
let recep1 = ChatMessage.getWhisperRecipients(name) || [];
return recep1.concat(ChatMessage.getWhisperRecipients('GM'));
}
/* -------------------------------------------- */
static blindMessageToGM(chatOptions) {
let chatGM = duplicate(chatOptions);
chatGM.whisper = this.getUsers(user => user.isGM);
chatGM.content = "Blinde message of " + game.user.name + "<br>" + chatOptions.content;
console.log("blindMessageToGM", chatGM);
2022-08-14 15:27:54 +02:00
game.socket.emit("system.fvtt-pegasus-rpg", { msg: "msg_gm_chat_message", data: chatGM });
2021-12-02 07:38:59 +01:00
}
2022-03-06 20:07:41 +01:00
2022-01-30 09:44:37 +01:00
/* -------------------------------------------- */
static async searchItem(dataItem) {
2022-03-06 20:07:41 +01:00
let item
2022-01-30 09:44:37 +01:00
if (dataItem.pack) {
2022-09-25 09:26:12 +02:00
let id = dataItem.id || dataItem._id
2022-09-29 13:07:57 +02:00
let items = await this.loadCompendium(dataItem.pack, item => item.id == id)
2022-09-25 09:26:12 +02:00
//console.log(">>>>>> PACK", items)
item = items[0] || undefined
//item = await fromUuid(dataItem.pack + "." + id)
2022-01-30 09:44:37 +01:00
} else {
item = game.items.get(dataItem.id)
2022-07-10 10:22:04 +02:00
}
2022-03-06 20:07:41 +01:00
return item
2022-01-30 09:44:37 +01:00
}
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
static split3Columns(data) {
2022-01-28 10:05:54 +01:00
let array = [[], [], []];
if (data == undefined) return array;
2021-12-02 07:38:59 +01:00
let col = 0;
for (let key in data) {
let keyword = data[key];
keyword.key = key; // Self-reference
2022-01-28 10:05:54 +01:00
array[col].push(keyword);
2021-12-02 07:38:59 +01:00
col++;
if (col == 3) col = 0;
2022-01-28 10:05:54 +01:00
}
2021-12-02 07:38:59 +01:00
return array;
}
/* -------------------------------------------- */
static createChatMessage(name, rollMode, chatOptions) {
switch (rollMode) {
case "blindroll": // GM only
if (!game.user.isGM) {
this.blindMessageToGM(chatOptions);
chatOptions.whisper = [game.user.id];
chatOptions.content = "Message only to the GM";
}
else {
chatOptions.whisper = this.getUsers(user => user.isGM);
}
break;
default:
chatOptions.whisper = this.getWhisperRecipients(rollMode, name);
break;
}
chatOptions.alias = chatOptions.alias || name;
ChatMessage.create(chatOptions);
}
2022-01-28 10:05:54 +01:00
/* -------------------------------------------- */
2022-07-20 23:53:37 +02:00
static getBasicRollData(isInit) {
2022-01-30 09:44:37 +01:00
let rollData = {
2022-01-28 10:05:54 +01:00
rollId: randomID(16),
rollMode: game.settings.get("core", "rollMode"),
bonusDicesLevel: 0,
2022-07-19 00:18:46 +02:00
statLevelBonus: 0,
damageLevelBonus: 0,
specLevelBonus: 0,
hindranceLevelBonus: 0,
2022-01-28 10:05:54 +01:00
hindranceDicesLevel: 0,
2022-09-27 17:18:41 +02:00
modifiers: "none",
2022-01-28 10:05:54 +01:00
otherDicesLevel: 0,
statDicesLevel: 0,
specDicesLevel: 0,
effectsList: [],
armorsList: [],
2022-01-28 17:27:01 +01:00
weaponsList: [],
2022-09-21 16:54:34 +02:00
vehicleWeapons: [],
2022-01-28 17:27:01 +01:00
equipmentsList: [],
2022-09-25 09:26:12 +02:00
vehicleShieldList: [],
2022-01-30 09:44:37 +01:00
optionsDiceList: PegasusUtility.getOptionsDiceList()
2022-01-28 10:05:54 +01:00
}
2022-09-21 16:54:34 +02:00
if (!isInit) { // For init, do not display target hindrances
2022-07-20 23:53:37 +02:00
PegasusUtility.updateWithTarget(rollData)
}
2022-01-28 10:05:54 +01:00
return rollData
}
/* -------------------------------------------- */
static updateWithTarget(rollData) {
2022-07-27 22:26:48 +02:00
let target = PegasusUtility.getTarget()
2022-01-28 10:05:54 +01:00
if (target) {
2022-07-27 22:26:48 +02:00
let defenderActor = target.actor
rollData.defenderTokenId = target.id
2022-10-05 09:40:34 +02:00
rollData.defenderSize = 0
2023-09-13 21:25:44 +02:00
if (defenderActor.type == "character") {
2022-10-05 09:40:34 +02:00
rollData.defenderSize = Number(defenderActor.system.biodata.sizenum) + Number(defenderActor.system.biodata.sizebonus)
2023-09-13 21:25:44 +02:00
} else if (defenderActor.type == "vehicle") {
2022-10-05 09:40:34 +02:00
rollData.defenderSize = Number(defenderActor.system.statistics.hr.size)
}
2022-08-14 15:27:54 +02:00
//rollData.attackerId = this.id
2022-09-28 20:04:04 +02:00
console.log("Target/DEFENDER", defenderActor)
2023-09-13 08:06:08 +02:00
//defenderActor.addHindrancesList(rollData.effectsList) /* No more used */
2022-01-28 10:05:54 +01:00
}
}
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
static createChatWithRollMode(name, chatOptions) {
this.createChatMessage(name, game.settings.get("core", "rollMode"), chatOptions);
}
/* -------------------------------------------- */
static async confirmDelete(actorSheet, li) {
let itemId = li.data("item-id");
let msgTxt = "<p>Are you sure to remove this Item ?";
let buttons = {
delete: {
2022-01-28 10:05:54 +01:00
icon: '<i class="fas fa-check"></i>',
label: "Yes, remove it",
callback: () => {
2022-09-27 16:45:07 +02:00
actorSheet.actor.deleteEmbeddedDocuments("Item", [itemId])
2022-01-28 10:05:54 +01:00
li.slideUp(200, () => actorSheet.render(false));
2021-12-02 07:38:59 +01:00
}
2022-01-28 10:05:54 +01:00
},
cancel: {
icon: '<i class="fas fa-times"></i>',
label: "Cancel"
2021-12-02 07:38:59 +01:00
}
2022-01-28 10:05:54 +01:00
}
msgTxt += "</p>";
let d = new Dialog({
title: "Confirm removal",
content: msgTxt,
buttons: buttons,
default: "cancel"
});
d.render(true);
2021-12-02 07:38:59 +01:00
}
2022-10-03 10:01:41 +02:00
/* -------------------------------------------- */
2022-10-05 09:40:34 +02:00
static checkIsVehicleCrew(actorId) {
2023-09-13 21:25:44 +02:00
let vehicles = game.actors.filter(actor => actor.type == "vehicle") || []
for (let vehicle of vehicles) {
2022-10-05 09:58:07 +02:00
console.log("Checking", vehicle.name)
2023-09-13 21:25:44 +02:00
if (vehicle.inCrew(actorId)) {
2022-10-05 09:40:34 +02:00
return vehicle
}
}
return false
2022-10-03 10:01:41 +02:00
}
/* -------------------------------------------- */
static async getRelevantTokens() {
if (!_token) { return }
let tokens = canvas.tokens.placeables.filter(token => token.document.disposition == 1)
for (let token of tokens) {
console.log("Parsing tokens", token.name)
let dist = canvas.grid.measureDistances(
[{ ray: new Ray(_token.center, token.center) }], { gridSpaces: false })
if (dist && dist[0] && dist[0] > 0) {
console.log(" Friendly Tokens at : ", token.name, dist / canvas.grid.grid.options.dimensions.distance)
}
let visible = canvas.effects.visibility.testVisibility(token.center, { object: _token })
if (visible && dist[0] > 0) {
this.glowToken(token)
}
console.log(" Visible!", visible)
}
}
/* -------------------------------------------- */
2022-10-04 08:15:54 +02:00
static async processTactician() {
2022-10-03 10:01:41 +02:00
// Tactician management
let toApply = {}
2022-10-05 09:13:17 +02:00
let tacticianTokens = canvas.tokens.placeables.filter(token => token.actor.isTactician() && !token.document.hidden)
2022-10-03 10:01:41 +02:00
for (let token of tacticianTokens) {
token.refresh()
2023-09-13 21:25:44 +02:00
let friends = canvas.tokens.placeables.filter(newToken => newToken.actor.type == "character" && !newToken.document.hidden && newToken.document.disposition == token.document.disposition)
2022-10-03 10:01:41 +02:00
for (let friend of friends) {
if (friend.actor.id != token.actor.id) {
2022-11-30 12:00:09 +01:00
let existing = toApply[friend.id] || { token: friend, add: false, level: 0, names: [] }
2022-10-03 10:01:41 +02:00
let visible = canvas.effects.visibility.testVisibility(friend.center, { object: token })
console.log("parse visible TACTICIAN : ", visible, token.name, friend.name)
if (visible) {
existing.add = true
existing.level += token.actor.getRoleLevel()
existing.names.push(token.actor.name)
}
2022-11-30 12:00:09 +01:00
toApply[friend.id] = existing
2022-10-03 10:01:41 +02:00
}
}
}
for (let id in toApply) {
let applyDef = toApply[id]
2022-11-30 12:00:09 +01:00
let hasBonus = applyDef.token.actor.hasTacticianBonus()
2022-10-03 10:01:41 +02:00
if (applyDef.add) {
if (!hasBonus) {
2022-11-30 12:00:09 +01:00
await applyDef.token.actor.addTacticianEffect(applyDef.names.toString(), applyDef.level)
2022-10-03 10:01:41 +02:00
} else if (applyDef.level != hasBonus.system.effectlevel) {
2022-11-30 12:00:09 +01:00
await applyDef.token.actor.removeTacticianEffect()
await applyDef.token.actor.addTacticianEffect(applyDef.names.toString(), applyDef.level)
2022-10-03 10:01:41 +02:00
}
} else if (hasBonus) {
2022-11-30 12:00:09 +01:00
await applyDef.token.actor.removeTacticianEffect()
2022-10-03 10:01:41 +02:00
}
}
2022-10-03 15:32:22 +02:00
//Delete all effects if no more tacticians (ie deleted case)
if (tacticianTokens.length == 0) {
let allTokens = canvas.tokens.placeables.filter(token => token.actor.type == "character")
for (let token of allTokens) {
if (token.actor.hasTacticianBonus()) {
2022-11-30 12:00:09 +01:00
await token.actor.removeTacticianEffect()
2022-10-03 15:32:22 +02:00
}
}
}
2022-10-03 10:01:41 +02:00
}
/* -------------------------------------------- */
2022-10-04 08:15:54 +02:00
static async processEnhancer() {
2022-10-03 10:01:41 +02:00
// Enhancer management
let toApply = {}
2022-10-05 09:13:17 +02:00
let enhancerTokens = canvas.tokens.placeables.filter(token => token.actor.isEnhancer() && !token.document.hidden)
2022-10-03 10:01:41 +02:00
for (let token of enhancerTokens) {
token.refresh()
2023-09-13 21:25:44 +02:00
let friends = canvas.tokens.placeables.filter(newToken => newToken.actor.type == "character" && !newToken.document.hidden && newToken.document.disposition == token.document.disposition)
2022-10-03 10:01:41 +02:00
for (let friend of friends) {
if (friend.actor.id != token.actor.id) {
2022-11-30 12:00:09 +01:00
let existing = toApply[friend.id] || { token: friend, add: false, level: 0, names: [] }
2022-10-03 10:01:41 +02:00
let visible = canvas.effects.visibility.testVisibility(friend.center, { object: token })
console.log("parse visible ENHANCER: ", visible, token.name, friend.name)
if (visible) {
let dist = canvas.grid.measureDistances([{ ray: new Ray(token.center, friend.center) }], { gridSpaces: false })
if (dist && dist[0] && (dist[0] / canvas.grid.grid.options.dimensions.distance) <= 5) {
existing.add = true
existing.level += token.actor.getRoleLevel()
existing.names.push(token.actor.name)
}
}
2022-11-30 12:00:09 +01:00
toApply[friend.id] = existing
2022-10-03 10:01:41 +02:00
}
}
}
for (let id in toApply) {
let applyDef = toApply[id]
2022-11-30 12:00:09 +01:00
let hasBonus = applyDef.token.actor.hasEnhancerBonus()
2022-10-03 10:01:41 +02:00
if (applyDef.add) {
if (!hasBonus) {
2022-11-30 12:00:09 +01:00
await applyDef.token.actor.addEnhancerEffect(applyDef.names.toString(), applyDef.level)
2022-10-03 10:01:41 +02:00
} else if (applyDef.level != hasBonus.system.effectlevel) {
2022-11-30 12:00:09 +01:00
await applyDef.token.actor.removeEnhancerEffect()
await applyDef.token.actor.addEnhancerEffect(applyDef.names.toString(), applyDef.level)
2022-10-03 10:01:41 +02:00
}
} else if (hasBonus) {
2022-11-30 12:00:09 +01:00
await applyDef.token.actor.removeEnhancerEffect()
2022-10-03 10:01:41 +02:00
}
}
2022-10-03 15:32:22 +02:00
// Delete all effects if no more tacticians (ie deleted case)
if (enhancerTokens.length == 0) {
let allTokens = canvas.tokens.placeables.filter(token => token.actor.type == "character")
for (let token of allTokens) {
if (token.actor.hasEnhancerBonus()) {
2022-11-30 12:00:09 +01:00
await token.actor.removeEnhancerEffect()
2022-10-03 15:32:22 +02:00
}
}
}
2022-10-03 10:01:41 +02:00
}
/* -------------------------------------------- */
2022-10-04 08:15:54 +02:00
static async processAgitator() {
2022-10-03 10:01:41 +02:00
// Agitator management
let toApply = {}
2022-10-05 09:13:17 +02:00
let agitatorTokens = canvas.tokens.placeables.filter(token => token.actor.isAgitator() && !token.document.hidden)
2022-10-03 10:01:41 +02:00
for (let token of agitatorTokens) {
token.refresh()
2022-10-05 14:44:12 +02:00
let ennemies = []
if (token.document.disposition == -1) {
2023-09-13 21:25:44 +02:00
ennemies = canvas.tokens.placeables.filter(newToken => newToken.actor.type == "character" && !newToken.document.hidden && (newToken.document.disposition == 1 || newToken.document.disposition == 0))
2022-10-05 14:44:12 +02:00
}
if (token.document.disposition == 1) {
2023-09-13 21:25:44 +02:00
ennemies = canvas.tokens.placeables.filter(newToken => newToken.actor.type == "character" && !newToken.document.hidden && (newToken.document.disposition == -1 || newToken.document.disposition == 0))
2022-10-05 14:44:12 +02:00
}
if (token.document.disposition == 0) {
2023-09-13 21:25:44 +02:00
ennemies = canvas.tokens.placeables.filter(newToken => newToken.actor.type == "character" && !newToken.document.hidden && (newToken.document.disposition == -1 || newToken.document.disposition == 1))
2022-10-04 08:15:54 +02:00
}
2022-11-30 12:00:09 +01:00
console.log("Ennemies for token", token.actor.name, ennemies)
2022-10-03 10:01:41 +02:00
for (let ennemy of ennemies) {
if (ennemy.actor.id != token.actor.id) {
2022-11-30 12:00:09 +01:00
//console.log("Adding ennemy", ennemy.id)
let existing = toApply[ennemy.id] || { token: ennemy, add: false, level: 0, names: [] }
2022-10-03 10:01:41 +02:00
let visible = canvas.effects.visibility.testVisibility(ennemy.center, { object: token })
if (visible) {
let dist = canvas.grid.measureDistances([{ ray: new Ray(token.center, ennemy.center) }], { gridSpaces: false })
if (dist && dist[0] && (dist[0] / canvas.grid.grid.options.dimensions.distance) <= 5) {
existing.add = true
existing.level += token.actor.getRoleLevel()
existing.names.push(token.actor.name)
}
}
2022-11-30 12:00:09 +01:00
toApply[ennemy.id] = existing
2022-10-03 10:01:41 +02:00
}
}
}
2022-11-30 12:00:09 +01:00
//console.log("To apply stuff : ", toApply)
2022-10-03 10:01:41 +02:00
for (let id in toApply) {
let applyDef = toApply[id]
2022-11-30 12:00:09 +01:00
let hasHindrance = applyDef.token.actor.hasAgitatorHindrance()
2022-10-03 10:01:41 +02:00
if (applyDef.add) {
if (!hasHindrance) {
2022-11-30 12:00:09 +01:00
await applyDef.token.actor.addAgitatorHindrance(applyDef.names.toString(), applyDef.level)
2022-10-03 10:01:41 +02:00
} else if (applyDef.level != hasHindrance.system.effectlevel) {
2022-11-30 12:00:09 +01:00
await applyDef.token.actor.removeAgitatorHindrance()
await applyDef.token.actor.addAgitatorHindrance(applyDef.names.toString(), applyDef.level)
2022-10-03 10:01:41 +02:00
}
} else if (hasHindrance) {
2022-11-30 12:00:09 +01:00
await applyDef.token.actor.removeAgitatorHindrance()
2022-10-03 10:01:41 +02:00
}
}
2022-11-30 12:00:09 +01:00
// Delete all effects if no more agitators (ie deleted case)
2022-10-03 15:32:22 +02:00
if (agitatorTokens.length == 0) {
let allTokens = canvas.tokens.placeables.filter(token => token.actor.type == "character")
for (let token of allTokens) {
2022-10-05 09:19:59 +02:00
if (token.actor.hasAgitatorHindrance()) {
2022-11-30 12:00:09 +01:00
await token.actor.removeAgitatorHindrance()
2022-10-03 15:32:22 +02:00
}
}
}
2022-10-03 10:01:41 +02:00
}
/* -------------------------------------------- */
static async processRoleEffects() {
// Small optimization
let now = Date.now()
2022-10-03 15:32:22 +02:00
if (now - this.lastRoleEffectProcess < 300) {
2022-10-03 10:01:41 +02:00
return // Save some processing
}
this.lastRoleEffectProcess = now
console.log("=========================+> Searching/Processing roles effects")
2023-09-11 20:34:33 +02:00
/*NO MORE USED : await this.processTactician()*/
2022-10-04 08:15:54 +02:00
await this.processEnhancer()
2023-09-11 20:34:33 +02:00
/*NO MORE USED : await this.processAgitator()*/
2022-10-03 10:01:41 +02:00
}
/* -------------------------------------------- */
static async refreshSightForEffect() {
setTimeout(500, this.processRoleEffects())
}
2021-12-02 07:38:59 +01:00
}