Added the ability for effects to automatically activate/deactivate depending on range between tokens

This commit is contained in:
WBHarry 2025-07-29 14:50:14 +02:00
parent 4defe69c21
commit b80f598625
13 changed files with 218 additions and 30 deletions

View file

@ -6,7 +6,7 @@ import * as dice from './module/dice/_module.mjs';
import * as fields from './module/data/fields/_module.mjs';
import RegisterHandlebarsHelpers from './module/helpers/handlebarsHelper.mjs';
import { enricherConfig, enricherRenderSetup } from './module/enrichers/_module.mjs';
import { getCommandTarget, rollCommandToJSON } from './module/helpers/utils.mjs';
import { compareValues, getCommandTarget, rollCommandToJSON } from './module/helpers/utils.mjs';
import { NarrativeCountdowns } from './module/applications/ui/countdowns.mjs';
import { DualityRollColor } from './module/data/settings/Appearance.mjs';
import { DHRoll, DualityRoll, D20Roll, DamageRoll, DualityDie } from './module/dice/_module.mjs';
@ -235,3 +235,73 @@ Hooks.on('renderJournalDirectory', async (tab, html, _, options) => {
};
}
});
Hooks.on('moveToken', async (movedToken, data) => {
const effectsAutomation = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.Automation).effects;
if (!effectsAutomation.rangeDependent) return;
const rangeDependantEffects = movedToken.actor.effects.filter(effect => effect.system.rangeDependence.enabled);
const { x, y, height, width } = data.destination;
const getDimensions = (x, y, height, width) => {
const heightModifier = Math.max(Math.round(height) - 1, 0);
const widthModifier = Math.max(Math.round(width) - 1, 0);
return {
minX: x - widthModifier,
maxX: x + widthModifier,
minY: y - heightModifier,
maxY: y + widthModifier
};
};
const { minX: dMinX, maxX: dMaxX, minY: dMinY, maxY: dMaxY } = getDimensions(x, y, height, width);
const updateEffects = async (token, dimensions, effects, effectUpdates) => {
const { minX, maxX, minY, maxY } = dimensions;
const rangeMeasurement = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.RangeMeasurement);
for (let effect of effects.filter(x => x.system.rangeDependence.enabled)) {
const { target, range, type } = effect.system.rangeDependence;
if (
(target === 'friendly' && token.disposition !== 1) ||
(target === 'hostile' && token.disposition !== -1)
)
return false;
const distance = rangeMeasurement[range] * 20; // x/y scale is 100. Grid in feet is 5.
const xOkay =
compareValues(Math.abs(minX - dMinX), distance, type) ||
compareValues(Math.abs(maxX - dMaxX), distance, type);
const yOkay =
compareValues(Math.abs(minY - dMinY), distance, type) ||
compareValues(Math.abs(maxY - dMaxY), distance, type);
const reverse = ['moreThan', 'moreThanEqual'].includes(type);
const newDisabled = reverse ? !xOkay && !yOkay : !xOkay || !yOkay;
const oldDisabled = effectUpdates[effect.uuid] ? effectUpdates[effect.uuid].disabled : newDisabled;
effectUpdates[effect.uuid] = {
disabled: reverse ? oldDisabled || newDisabled : oldDisabled && newDisabled,
value: effect
};
}
};
const effectUpdates = {};
for (let token of game.scenes.find(x => x.active).tokens) {
if (token.id === movedToken.id) continue;
const { x, y, height, width } = token;
const dimensions = getDimensions(x, y, height, width);
await updateEffects(token, dimensions, rangeDependantEffects, effectUpdates);
if (token.actor) await updateEffects(token, dimensions, token.actor.effects, effectUpdates);
}
for (let key in effectUpdates) {
const effect = effectUpdates[key];
await effect.value.update({ disabled: effect.disabled });
}
});