diff --git a/daggerheart.mjs b/daggerheart.mjs index c13719f6..0d9d5ae1 100644 --- a/daggerheart.mjs +++ b/daggerheart.mjs @@ -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 { compareValues, getCommandTarget, rollCommandToJSON } from './module/helpers/utils.mjs'; +import { 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'; @@ -242,47 +242,25 @@ Hooks.on('moveToken', async (movedToken, data) => { 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 updateEffects = async (disposition, token, effects, effectUpdates) => { 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) - ) + if ((target === 'friendly' && disposition !== 1) || (target === 'hostile' && 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 distanceBetween = canvas.grid.measurePath([ + { ...movedToken.toObject(), x: data.destination.x, y: data.destination.y }, + token + ]).distance; + const distance = rangeMeasurement[range]; - const reverse = ['moreThan', 'moreThanEqual'].includes(type); - const newDisabled = reverse ? !xOkay && !yOkay : !xOkay || !yOkay; + const reverse = type === CONFIG.DH.GENERAL.rangeInclusion.outsideRange.id; + const newDisabled = reverse ? distanceBetween <= distance : distanceBetween > distance; const oldDisabled = effectUpdates[effect.uuid] ? effectUpdates[effect.uuid].disabled : newDisabled; effectUpdates[effect.uuid] = { - disabled: reverse ? oldDisabled || newDisabled : oldDisabled && newDisabled, + disabled: oldDisabled || newDisabled, value: effect }; } @@ -290,14 +268,11 @@ Hooks.on('moveToken', async (movedToken, data) => { const effectUpdates = {}; for (let token of game.scenes.find(x => x.active).tokens) { - if (token.id === movedToken.id) continue; + if (token.id !== movedToken.id) { + await updateEffects(token.disposition, token, rangeDependantEffects, effectUpdates); + } - 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); + if (token.actor) await updateEffects(movedToken.disposition, token, token.actor.effects, effectUpdates); } for (let key in effectUpdates) { diff --git a/lang/en.json b/lang/en.json index d8ce2e2b..30a19588 100755 --- a/lang/en.json +++ b/lang/en.json @@ -633,12 +633,9 @@ "oneHanded": "One-Handed", "twoHanded": "Two-Handed" }, - "CompareOperator": { - "lessThan": "Less Than", - "lessThanEqual": "Less Than Equal", - "equal": "Equal", - "moreThanEqual": "More Than Equal", - "moreThan": "More Than" + "RangeInclusion": { + "withinRange": "Within Range", + "outsideRange": "Outside Range" }, "Condition": { "dead": { diff --git a/module/config/generalConfig.mjs b/module/config/generalConfig.mjs index 005ac4a3..dcc96c64 100644 --- a/module/config/generalConfig.mjs +++ b/module/config/generalConfig.mjs @@ -43,26 +43,14 @@ export const range = { } }; -export const compareOperator = { - lessThan: { - id: 'lessThan', - label: 'DAGGERHEART.CONFIG.CompareOperator.lessThan' +export const rangeInclusion = { + withinRange: { + id: 'withinRange', + label: 'DAGGERHEART.CONFIG.RangeInclusion.withinRange' }, - lessThanEqual: { - id: 'lessThanEqual', - label: 'DAGGERHEART.CONFIG.CompareOperator.lessThanEqual' - }, - equal: { - id: 'equal', - label: 'DAGGERHEART.CONFIG.CompareOperator.equal' - }, - moreThanEqual: { - id: 'moreThanEqual', - label: 'DAGGERHEART.CONFIG.CompareOperator.moreThanEqual' - }, - moreThan: { - id: 'moreThan', - label: 'DAGGERHEART.CONFIG.CompareOperator.moreThan' + outsideRange: { + id: 'outsideRange', + label: 'DAGGERHEART.CONFIG.RangeInclusion.outsideRange' } }; diff --git a/module/data/activeEffect/baseEffect.mjs b/module/data/activeEffect/baseEffect.mjs index 178b7fc4..0ac87de0 100644 --- a/module/data/activeEffect/baseEffect.mjs +++ b/module/data/activeEffect/baseEffect.mjs @@ -11,8 +11,8 @@ export default class BaseEffect extends foundry.abstract.TypeDataModel { }), type: new fields.StringField({ required: true, - choices: CONFIG.DH.GENERAL.compareOperator, - initial: CONFIG.DH.GENERAL.compareOperator.lessThanEqual.id, + choices: CONFIG.DH.GENERAL.rangeInclusion, + initial: CONFIG.DH.GENERAL.rangeInclusion.withinRange.id, label: 'DAGGERHEART.GENERAL.type' }), target: new fields.StringField({ diff --git a/module/helpers/utils.mjs b/module/helpers/utils.mjs index 120b51fb..ac9ac9e7 100644 --- a/module/helpers/utils.mjs +++ b/module/helpers/utils.mjs @@ -321,18 +321,3 @@ export const arraysEqual = (a, b) => [...new Set([...a, ...b])].every(v => a.filter(e => e === v).length === b.filter(e => e === v).length); export const setsEqual = (a, b) => a.size === b.size && [...a].every(value => b.has(value)); - -export function compareValues(a, b, compare) { - switch (compare) { - case CONFIG.DH.GENERAL.compareOperator.lessThan.id: - return a < b; - case CONFIG.DH.GENERAL.compareOperator.lessThanEqual.id: - return a <= b; - case CONFIG.DH.GENERAL.compareOperator.equal.id: - return a === b; - case CONFIG.DH.GENERAL.compareOperator.moreThanEqual.id: - return a >= b; - case CONFIG.DH.GENERAL.compareOperator.moreThan.id: - return a > b; - } -} diff --git a/templates/settings/range-measurement-settings.hbs b/templates/settings/range-measurement-settings.hbs index 617d1899..e6373d92 100644 --- a/templates/settings/range-measurement-settings.hbs +++ b/templates/settings/range-measurement-settings.hbs @@ -9,6 +9,7 @@ {{formGroup settingFields.schema.fields.close value=settingFields._source.close localize=true}} {{formGroup settingFields.schema.fields.far value=settingFields._source.far localize=true}} {{formGroup settingFields.schema.fields.veryFar value=settingFields._source.veryFar localize=true}} + {{formGroup settingFields.schema.fields.diagonalMeasuring value=settingFields._source.diagonalMeasuring localize=true}}