Skip to content

Commit

Permalink
Improved sameRound behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
wbjo committed May 17, 2022
1 parent 27107a0 commit 1013f14
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 40 deletions.
29 changes: 29 additions & 0 deletions join-combat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { createToken } from './scripts/createToken.js';
import { renderSceneControls } from './scripts/renderSceneControls.js';

Hooks.once('init', () => {
game.settings.register('join-combat', 'active', {
name: 'Join Combat Active',
hint: 'Roll Initiative for new tokens or not',
scope: 'world',
default: false,
config: false,
type: Boolean,
});
game.settings.register('join-combat', 'join-same-round', {
name: 'Same Round',
hint: '',
scope: 'world',
default: false,
config: false,
type: Boolean,
});
});

Hooks.on('renderSceneControls', async (controls, html) => {
await renderSceneControls(html);
});

Hooks.on('createToken', async (document, _, userId) => {
await createToken(document, userId);
});
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "If the toggle is checked, tokens placed by the DM will automatically be added to combat and roll initiative.",
"author": "HarryBoy",
"esmodules": [
"scripts/join-combat.js"
"join-combat.js"
],
"styles": ["styles/join-combat.css"],
"languages": [
Expand Down
11 changes: 11 additions & 0 deletions scripts/createToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { getInitiativeOptions } from './helpers.js';

export const createToken = async (document, userId) => {
const dmCreated = game.user.isGM && game.user.id === userId;
const activeCombat = Boolean(game.combat);
if(activeCombat && dmCreated && game.settings.get('join-combat', 'active')){
const token = await document.object.toggleCombat();
const newCombatant = game.combat.combatants.find(x => x.token.id === token.id);
await game.combat.rollInitiative([newCombatant.id], getInitiativeOptions());
}
};
16 changes: 7 additions & 9 deletions scripts/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ export const getInitiativeOptions = () => {
return {};
}

const lowestInitiative = game.combat.data.combatants.reduce((acc, c) => {
if(acc === null){
return c.data.initiative ? c : null;
}
const currentCombatantId = game.combat.current.combatantId;
if(!currentCombatantId){
return {};
}

return acc.data.initiative > c.data.initiative ? c : acc;
}, null)?.data?.initiative;

if(!lowestInitiative){
const currentInitiative = game.combat.data.combatants.find(x => x.id === currentCombatantId)?.data?.initiative;
if(currentInitiative === undefined){
return {};
}

const dice = Math.max(lowestInitiative-1, 0);
const dice = Math.max(currentInitiative-1, 0);

return { formula: dice ? `1d${dice}` : '0' };
}
33 changes: 3 additions & 30 deletions scripts/join-combat.js → scripts/renderSceneControls.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,6 @@
import { getInitiativeOptions, isSystemSameRoundCompatible } from './helpers.js';
import { isSystemSameRoundCompatible } from './helpers.js';

Hooks.once('init', () => {
game.settings.register('join-combat', 'active', {
name: 'Join Combat Active',
hint: 'Roll Initiative for new tokens or not',
scope: 'world',
default: false,
config: false,
type: Boolean,
});
game.settings.register('join-combat', 'join-same-round', {
name: 'Same Round',
hint: '',
scope: 'world',
default: false,
config: false,
type: Boolean,
});
});

Hooks.on('renderSceneControls', async (controls, html) => {
export const renderSceneControls = async (html) => {
if(game.user.isGM){
const tokenSublist = $(html).find('ol.sub-controls').first();
tokenSublist.append(await renderTemplate('modules/join-combat/templates/TokenButtons.hbs', {
Expand Down Expand Up @@ -57,12 +38,4 @@ Hooks.on('renderSceneControls', async (controls, html) => {
}
});
}
});

Hooks.on('createToken', (document, _, userId) => {
const dmCreated = game.user.isGM && game.user.id === userId;
const activeCombat = Boolean(game.combat);
if(activeCombat && dmCreated && game.settings.get('join-combat', 'active')){
document.actor.rollInitiative({createCombatants: true, initiativeOptions: getInitiativeOptions()});
}
});
};

0 comments on commit 1013f14

Please sign in to comment.