diff --git a/scripts/actions/sw5e/sw5e-actions.js b/scripts/actions/sw5e/sw5e-actions.js index 5bf4f33..728f74a 100644 --- a/scripts/actions/sw5e/sw5e-actions.js +++ b/scripts/actions/sw5e/sw5e-actions.js @@ -470,6 +470,8 @@ export class ActionHandlerSw5e extends ActionHandler { let rests = this.initializeEmptySubcategory() let utility = this.initializeEmptySubcategory(); + this._addIntiativeSubcategory(macroType, result, tokenId); + if (actor.data.type === 'character') { let shortRestValue = [macroType, tokenId, 'shortRest'].join(this.delimiter); rests.actions.push({id:'shortRest', encodedValue: shortRestValue, name: this.i18n('tokenactionhud.shortRest')}) @@ -498,10 +500,13 @@ export class ActionHandlerSw5e extends ActionHandler { _addMultiUtilities(list, tokenId, actors) { let category = this.initializeEmptyCategory('utility'); let macroType = 'utility'; + + this._addMultiIntiativeSubcategory(macroType, tokenId, category); let rests = this.initializeEmptySubcategory(); let utility = this.initializeEmptySubcategory(); + if (actors.every(a => a.data.type === 'character')) { let shortRestValue = [macroType, tokenId, 'shortRest'].join(this.delimiter); rests.actions.push({id:'shortRest', encodedValue: shortRestValue, name: this.i18n('tokenactionhud.shortRest')}) @@ -552,6 +557,56 @@ export class ActionHandlerSw5e extends ActionHandler { this._combineSubcategoryWithCategory(category, this.i18n('tokenactionhud.conditions'), conditions); } + + /** @private */ + _addIntiativeSubcategory(macroType, category, tokenId) { + const combat = game.combat; + let combatant, currentInitiative; + if (combat) { + combatant = combat.combatants.find(c => c.tokenId === tokenId); + currentInitiative = combatant?.initiative; + } + + let initiative = this.initializeEmptySubcategory(); + + let initiativeValue = [macroType, tokenId, 'initiative'].join(this.delimiter); + let initiativeName = `${this.i18n('tokenactionhud.rollInitiative')}`; + + let initiativeAction = {id:'rollInitiative', encodedValue: initiativeValue, name: initiativeName}; + + if (currentInitiative) + initiativeAction.info1 = currentInitiative; + initiativeAction.cssClass = currentInitiative ? 'active' : ''; + + initiative.actions.push(initiativeAction); + + this._combineSubcategoryWithCategory(category, this.i18n('tokenactionhud.initiative'), initiative); + } + + /** @private */ + _addMultiIntiativeSubcategory(macroType, tokenId, category) { + const combat = game.combat; + + let initiative = this.initializeEmptySubcategory(); + + let initiativeValue = [macroType, tokenId, 'initiative'].join(this.delimiter); + let initiativeName = `${this.i18n('tokenactionhud.rollInitiative')}`; + + let initiativeAction = {id:'rollInitiative', encodedValue: initiativeValue, name: initiativeName}; + + let isActive; + if (combat) { + let tokenIds = canvas.tokens.controlled.map(t => t.id); + let tokenCombatants = tokenIds.map(id => combat.combatants.find(c => c.tokenId === id)); + isActive = tokenCombatants.every(c => !!c?.initiative) + } + + initiativeAction.cssClass = isActive ? 'active' : ''; + + initiative.actions.push(initiativeAction); + + this._combineSubcategoryWithCategory(category, this.i18n('tokenactionhud.initiative'), initiative); + } /** @private */ _addMultiConditions(list, tokenId) { diff --git a/scripts/rollHandlers/sw5e/sw5e-base.js b/scripts/rollHandlers/sw5e/sw5e-base.js index 2b9208e..c03aabf 100644 --- a/scripts/rollHandlers/sw5e/sw5e-base.js +++ b/scripts/rollHandlers/sw5e/sw5e-base.js @@ -57,7 +57,7 @@ export class RollHandlerBaseSw5e extends RollHandler { this.rollItemMacro(event, tokenId, actionId); break; case "utility": - this.performUtilityMacro(event, tokenId, actionId); + await this.performUtilityMacro(event, tokenId, actionId); break; case 'effect': await this.toggleEffect(event, tokenId, actionId); @@ -108,7 +108,7 @@ export class RollHandlerBaseSw5e extends RollHandler { return (item.data.data.recharge && !item.data.data.recharge.charged && item.data.data.recharge.value); } - performUtilityMacro(event, tokenId, actionId) { + async performUtilityMacro(event, tokenId, actionId) { let actor = super.getActor(tokenId); let token = super.getToken(tokenId); @@ -133,10 +133,21 @@ export class RollHandlerBaseSw5e extends RollHandler { case 'deathSave': actor.rollDeathSave({event}); break; + case 'initiative': + await this.performInitiativeMacro(tokenId); + break; } } + + async performInitiativeMacro(tokenId) { + let actor = super.getActor(tokenId); + + await actor.rollInitiative({createCombatants: true}); + + Hooks.callAll('forceUpdateTokenActionHUD') + } - async toggleCondition(event, tokenId, effectId) { + async toggleCondition(event, tokenId, effectId) { const token = super.getToken(tokenId); const isRightClick = this.isRightClick(event); if (effectId.includes('combat-utility-belt.') && game.cub && !isRightClick) {