From 31573bd522dca36d8c611999f9ebe0760d976782 Mon Sep 17 00:00:00 2001 From: LeRatierBretonnier Date: Mon, 26 Jan 2026 17:47:53 +0100 Subject: [PATCH] Fix initiative --- module/applications/combat.mjs | 191 --------------------------------- module/utils.mjs | 5 - prism-rpg.mjs | 8 +- 3 files changed, 1 insertion(+), 203 deletions(-) diff --git a/module/applications/combat.mjs b/module/applications/combat.mjs index f401c05..044080b 100644 --- a/module/applications/combat.mjs +++ b/module/applications/combat.mjs @@ -1,104 +1,6 @@ -/* -------------------------------------------- */ -export class PrismRPGCombatTracker extends foundry.applications.sidebar.tabs.CombatTracker { - - static PARTS = { - "header": { - "template": "systems/fvtt-prism-rpg/templates/combat-tracker-header-v2.hbs" - }, - "tracker": { - "template": "systems/fvtt-prism-rpg/templates/combat-tracker-v2.hbs" - }, - "footer": { - "template": "systems/fvtt-prism-rpg/templates/combat-tracker-footer-v2.hbs" - } - } - - static DEFAULT_OPTIONS = foundry.utils.mergeObject(super.DEFAULT_OPTIONS, { - actions: { - initiativePlus: PrismRPGCombatTracker.#initiativePlus, - initiativeMinus: PrismRPGCombatTracker.#initiativeMinus, - }, - }); - - async _prepareContext(options) { - let data = await super._prepareContext(options); - console?.log("Combat Tracker Data", data); - /*for (let u of data.turns) { - let c = game.combat.combatants.get(u.id); - u.progressionCount = c.system.progressionCount - u.isMonster = c.actor.type === "monster" - } - console.log("Combat Data", data);*/ - return data; - } - - static #initiativePlus(ev) { - ev.preventDefault(); - let cId = ev.target.closest(".combatant").dataset.combatantId; - let c = game.combat.combatants.get(cId); - c.update({ 'initiative': c.initiative + 1 }); - console.log("Initiative Plus"); - } - - static #initiativeMinus(ev) { - ev.preventDefault(); - let cId = ev.target.closest(".combatant").dataset.combatantId; - let c = game.combat.combatants.get(cId); - let newInit = Math.max(c.initiative - 1, 0); - c.update({ 'initiative': newInit }); - } - - activateListeners(html) { - super.activateListeners(html); - // Display Combat settings - html.find(".initiative-plus").click(ev => { - ev.preventDefault(); - let cId = ev.currentTarget.closest(".combatant").dataset.combatantId; - let c = game.combat.combatants.get(cId); - c.update({ 'initiative': c.initiative + 1 }); - }); - - html.find(".initiative-minus").click(ev => { - ev.preventDefault(); - let cId = ev.currentTarget.closest(".combatant").dataset.combatantId; - let c = game.combat.combatants.get(cId); - c.update({ 'initiative': c.initiative - 1 }); - console.log("Initiative Minus"); - }); - } - - /* -------------------------------------------- */ - static get defaultOptions() { - let path = "systems/fvtt-prism-rpg/templates/combat-tracker.hbs"; - return foundry.utils.mergeObject(super.defaultOptions, { - template: path, - }); - } -} - export class PrismRPGCombat extends Combat { - /** - * Return the Array of combatants sorted into initiative order, breaking ties alphabetically by name. - * @returns {Combatant[]} - */ - setupTurns() { - console?.log("Setup Turns...."); - this.turns ||= []; - - // Determine the turn order and the current turn - const turns = this.combatants.contents.sort(this.sortCombatantsLF); - if (this.turn !== null) this.turn = Math.clamp(this.turn, 0, turns.length - 1); - - // Update state tracking - let c = turns[this.turn]; - this.current = this._getCurrentState(c); - if (!this.previous) this.previous = this.current; - - // Return the array of prepared turns - return this.turns = turns; - } async rollInitiative(ids, options) { console.log("%%%%%%%%% Roll Initiative", ids, options); @@ -123,98 +25,5 @@ export class PrismRPGCombat extends Combat { return this; } - resetProgression(cId) { - let c = this.combatants.get(cId); - c.update({ 'system.progressionCount': 0 }); - } - setCasting(cId) { - let c = this.combatants.get(cId); - c.setFlag(SYSTEM.id, "casting", true); - } - - resetCasting(cId) { - let c = this.combatants.get(cId); - c.setFlag(SYSTEM.id, "casting", false); - } - - isCasting(cId) { - let c = this.combatants.get(cId); - return c.getFlag(SYSTEM.id, "casting"); - } - - async nextTurn() { - console.log("NEXT TURN"); - - let turn = this.turn ?? -1; - let skipDefeated = this.settings.skipDefeated; - - // Determine the next turn number - let next = null; - for (let [i, t] of this.turns.entries()) { - console.log("Turn", t); - if (i <= turn) continue; - if (skipDefeated && t.isDefeated) continue; - next = i; - break; - } - - // Maybe advance to the next round - let round = this.round; - if ((this.round === 0) || (next === null) || (next >= this.turns.length)) { - return this.nextRound(); - } - - // Update the document, passing data through a hook first - const updateData = { round, turn: next }; - const updateOptions = { advanceTime: CONFIG.time.turnTime, direction: 1 }; - Hooks.callAll("combatTurn", this, updateData, updateOptions); - return this.update(updateData, updateOptions); - } - - async nextRound() { - this.turnsDone = false - - let turn = this.turn === null ? null : 0; // Preserve the fact that it's no-one's turn currently. - console.log("ROUND", this); - - let advanceTime = Math.max(this.turns.length - this.turn, 0) * CONFIG.time.turnTime; - advanceTime += CONFIG.time.roundTime; - let nextRound = this.round + 1; - - let initOK = true; - for (let c of this.combatants) { - if (c.initiative === null) { - initOK = false; - break; - } - } - if (!initOK) { - ui.notifications.error("All combatants must have initiative rolled before the round can advance."); - return this; - } - - for (let c of this.combatants) { - if (nextRound >= c.initiative) { - let user = game.users.find(u => u.active && u.character && u.character.id === c.actor.id); - if (user?.hasPlayerOwner) { - game.socket.emit(`system.${SYSTEM.id}`, { type: "rollProgressionDice", progressionCount: c.system.progressionCount + 1, actorId: c.actor.id, combatId: this.id, combatantId: c.id }); - } else { - user = game.users.find(u => u.active && u.isGM); - c.actor.system.rollProgressionDice(this.id, c.id); - } - } - } - - // Update the document, passing data through a hook first - const updateData = { round: nextRound, turn }; - const updateOptions = { advanceTime, direction: 1 }; - Hooks.callAll("combatRound", this, updateData, updateOptions); - return this.update(updateData, updateOptions); - } - - - sortCombatantsLF(a, b) { - return a.initiative - b.initiative; - } } diff --git a/module/utils.mjs b/module/utils.mjs index ac12e3d..ac5abe2 100644 --- a/module/utils.mjs +++ b/module/utils.mjs @@ -13,11 +13,6 @@ export default class PrismRPGUtils { return compendiumData.filter(filter) } - /* -------------------------------------------- */ - static pushCombatOptions(html, options) { - options.push({ name: "Reset Progression", condition: true, icon: '', callback: target => { game.combat.resetProgression(target.data('combatant-id')); } }) - } - /* -------------------------------------------- */ static setHookListeners() { diff --git a/prism-rpg.mjs b/prism-rpg.mjs index c464fe4..1434873 100644 --- a/prism-rpg.mjs +++ b/prism-rpg.mjs @@ -11,7 +11,7 @@ import * as models from "./module/models/_module.mjs" import * as documents from "./module/documents/_module.mjs" import * as applications from "./module/applications/_module.mjs" -import { PrismRPGCombatTracker, PrismRPGCombat } from "./module/applications/combat.mjs" +import { PrismRPGCombat } from "./module/applications/combat.mjs" import { Macros } from "./module/macros.mjs" import { setupTextEnrichers } from "./module/enrichers.mjs" import { default as PrismRPGUtils } from "./module/utils.mjs" @@ -36,7 +36,6 @@ Hooks.once("init", function () { documents, } - CONFIG.ui.combat = PrismRPGCombatTracker CONFIG.Combat.documentClass = PrismRPGCombat CONFIG.Combat.initiative = { formula: "1d20 + @subAttributes.initiative.value", @@ -212,11 +211,6 @@ Hooks.on(hookName, (message, html, data) => { await actor.prepareRoll("weapon-damage-medium", weaponId) }) }) - -Hooks.on("getCombatTrackerEntryContext", (html, options) => { - PrismRPGUtils.pushCombatOptions(html, options); -}); - /** * Create a macro when dropping an entity on the hotbar * Item - open roll dialog