PepijnMC / ElevationDragRuler

A Foundry VTT module which adds a speedprovider for Drag Ruler to pick between different movement options based on elevation and terrain from Enhanced Terrain Layer.
MIT License
6 stars 4 forks source link

API toggle terrain #41

Closed rinnocenti closed 2 years ago

rinnocenti commented 2 years ago

I would like to make some macros that suspend difficult terrains for a given token by active effects (using duration to avoid difficult terrains).

Basically I needed an API in code format to do the same as the "toggle Terrain" token HUD button does, but that I can use in macros.

as a function already exists in the button, it would only need to be used externally (API)

PepijnMC commented 2 years ago

The token HUD button toggles the ignoredEnvironments.all.any flag of the token, which is the flag that is checked when calculating difficult terrain. This flag can already be safely controlled by a macro too.

For example, this macro will toggle terrain for the selected tokens:

//Retrieve the controlled tokens.
const tokens = canvas.tokens.controlled;
//Iterate over each token.
tokens.forEach((token) => {
        //Grab the current configured terrain settings, or if they don't exist provide a default configuration.
    const configuredEnvironments = token.document.getFlag('elevation-drag-ruler', 'ignoredEnvironments') || {'all': {'any': false, 'walk': false, 'swim': false, 'fly': false, 'burrow': false, 'climb': false}, 'arctic': {'any': false, 'walk': false, 'swim': false, 'fly': false, 'burrow': false, 'climb': false}, 'coast': {'any': false, 'walk': false, 'swim': false, 'fly': false, 'burrow': false, 'climb': false}, 'desert': {'any': false, 'walk': false, 'swim': false, 'fly': false, 'burrow': false, 'climb': false}, 'forest': {'any': false, 'walk': false, 'swim': false, 'fly': false, 'burrow': false, 'climb': false}, 'grassland': {'any': false, 'walk': false, 'swim': false, 'fly': false, 'burrow': false, 'climb': false}, 'jungle': {'any': false, 'walk': false, 'swim': false, 'fly': false, 'burrow': false, 'climb': false}, 'mountain': {'any': false, 'walk': false, 'swim': false, 'fly': false, 'burrow': true, 'climb': true}, 'swamp': {'any': false, 'walk': false, 'swim': false, 'fly': false, 'burrow': false, 'climb': false}, 'underdark': {'any': false, 'walk': false, 'swim': false, 'fly': false, 'burrow': false, 'climb': false}, 'urban': {'any': false, 'walk': false, 'swim': false, 'fly': false, 'burrow': false, 'climb': false}, 'water': {'any': false, 'walk': false, 'swim': true, 'fly': false, 'burrow': false, 'climb': false}};
    //Toggle the setting to ignore all terrain for any movemement speed.
    if (configuredEnvironments.all.any) configuredEnvironments.all.any = false;
    else configuredEnvironments.all.any = true;
    //Update the token flag.
    token.document.setFlag('elevation-drag-ruler', 'ignoredEnvironments', configuredEnvironments);
});

The only thing to look out for with this flag is that it is not always set by default for newly placed tokens. The module provides a default configuration for these situations, hence why the above macro also provides a default fallback option.

Similarly, the following macro will flat out disable difficult terrain for selected tokens, instead of toggling it.

//Retrieve the controlled tokens.
const tokens = canvas.tokens.controlled;
//Iterate over each token.
tokens.forEach((token) => {
        //Grab the current configured terrain settings, or if they don't exist provide a default configuration.
    const configuredEnvironments = token.document.getFlag('elevation-drag-ruler', 'ignoredEnvironments') || {'all': {'any': false, 'walk': false, 'swim': false, 'fly': false, 'burrow': false, 'climb': false}, 'arctic': {'any': false, 'walk': false, 'swim': false, 'fly': false, 'burrow': false, 'climb': false}, 'coast': {'any': false, 'walk': false, 'swim': false, 'fly': false, 'burrow': false, 'climb': false}, 'desert': {'any': false, 'walk': false, 'swim': false, 'fly': false, 'burrow': false, 'climb': false}, 'forest': {'any': false, 'walk': false, 'swim': false, 'fly': false, 'burrow': false, 'climb': false}, 'grassland': {'any': false, 'walk': false, 'swim': false, 'fly': false, 'burrow': false, 'climb': false}, 'jungle': {'any': false, 'walk': false, 'swim': false, 'fly': false, 'burrow': false, 'climb': false}, 'mountain': {'any': false, 'walk': false, 'swim': false, 'fly': false, 'burrow': true, 'climb': true}, 'swamp': {'any': false, 'walk': false, 'swim': false, 'fly': false, 'burrow': false, 'climb': false}, 'underdark': {'any': false, 'walk': false, 'swim': false, 'fly': false, 'burrow': false, 'climb': false}, 'urban': {'any': false, 'walk': false, 'swim': false, 'fly': false, 'burrow': false, 'climb': false}, 'water': {'any': false, 'walk': false, 'swim': true, 'fly': false, 'burrow': false, 'climb': false}};
    //Configure the setting to ignore extra movement costs for ALL terrain for ANY movement speed.
    configuredEnvironments.all.any = true;
    //Update the token flag.
    token.document.setFlag('elevation-drag-ruler', 'ignoredEnvironments', configuredEnvironments);
});