set token vision macro
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
const argBright = typeof args === 'undefined' ? null : args.length > 0 ? args[0] : null
|
||||
// argument can be one of 'bright', 'dim', 'dark', 'pitchdark'. Other values
|
||||
// will guess based on scene darkness
|
||||
const BRIGHT_LEVELS = ['bright', 'dim', 'dark', 'pitchdark']
|
||||
const THRESHOLDS = {
|
||||
dim: 0.4,
|
||||
dark: 0.6,
|
||||
pitchdark: 0.8
|
||||
}
|
||||
const RANGES = {
|
||||
basic: {
|
||||
bright: 25,
|
||||
dim: 25,
|
||||
dark: 10,
|
||||
pitchdark: 0
|
||||
},
|
||||
lowlight: {
|
||||
bright: 25,
|
||||
dim: 25,
|
||||
dark: 10,
|
||||
pitchdark: 0
|
||||
},
|
||||
darkvision: {
|
||||
bright: 25,
|
||||
dim: 25,
|
||||
dark: 10,
|
||||
pitchdark: 10
|
||||
},
|
||||
nightvision: {
|
||||
bright: 200,
|
||||
dim: 200,
|
||||
dark: 200,
|
||||
pitchdark: 200
|
||||
}
|
||||
}
|
||||
const SIGHT_NAMES = {
|
||||
lowlight: 'low-light-vision',
|
||||
darkvision: 'darkvision',
|
||||
nightvision: 'night-vision'
|
||||
}
|
||||
const SIGHT_MODES = {
|
||||
lowlight: 'lowlight',
|
||||
darkvision: 'darkvision',
|
||||
nightvision: 'darkvision',
|
||||
basic: 'basic'
|
||||
}
|
||||
|
||||
function findAbility (token, swid) {
|
||||
return token.actor.items.find(i => i.type === 'ability' && i.system.swid === swid)
|
||||
}
|
||||
|
||||
async function main () {
|
||||
const scene = game.scenes.current
|
||||
let sceneBright = BRIGHT_LEVELS[0]
|
||||
if (scene.darkness > THRESHOLDS.pitchdark) {
|
||||
sceneBright = BRIGHT_LEVELS[3]
|
||||
} else if (scene.darkness > THRESHOLDS.dark) {
|
||||
sceneBright = BRIGHT_LEVELS[2]
|
||||
} else if (scene.darkness > THRESHOLDS.dim) {
|
||||
sceneBright = BRIGHT_LEVELS[1]
|
||||
}
|
||||
let bright = sceneBright
|
||||
if (argBright && BRIGHT_LEVELS.includes(argBright)) {
|
||||
bright = argBright
|
||||
}
|
||||
const menuData = {
|
||||
inputs: [
|
||||
{ type: 'radio', label: 'Bright Light', options: ['bright', bright === BRIGHT_LEVELS[0]] },
|
||||
{ type: 'radio', label: 'Dim Light', options: ['bright', bright === BRIGHT_LEVELS[1]] },
|
||||
{ type: 'radio', label: 'Dark', options: ['bright', bright === BRIGHT_LEVELS[2]] },
|
||||
{ type: 'radio', label: 'Pitch Dark', options: ['bright', bright === BRIGHT_LEVELS[3]] }
|
||||
],
|
||||
buttons: [
|
||||
{ label: 'Select Scene Brightness', value: 'ok', default: true },
|
||||
{ label: 'Cancel', value: 'cancel' }
|
||||
]
|
||||
}
|
||||
const menuConfig = { title: 'Select scene brightness' }
|
||||
const result = await warpgate.menu(menuData, menuConfig)
|
||||
if (result.buttons !== 'ok') { return }
|
||||
|
||||
for (let i = 0; i < 4; i++) {
|
||||
if (result.inputs[i]) { bright = BRIGHT_LEVELS[i] }
|
||||
}
|
||||
|
||||
for (const tokenId of scene.tokens.map(t => t.id)) {
|
||||
const token = scene.tokens.get(tokenId)
|
||||
if (!token.sight.enabled) {
|
||||
console.log(`Skipping ${token.name}, vision not enabled`)
|
||||
continue
|
||||
// don't set sight on a token where it's not enabled
|
||||
}
|
||||
let sightType = 'basic'
|
||||
for (const sight in SIGHT_NAMES) {
|
||||
if (findAbility(token, SIGHT_NAMES[sight])) {
|
||||
sightType = sight
|
||||
}
|
||||
}
|
||||
const range = RANGES[sightType][bright]
|
||||
const sightMode = SIGHT_MODES[sightType]
|
||||
const visionModeData = CONFIG.Canvas.visionModes[sightMode].vision.defaults
|
||||
const data = {
|
||||
'sight.range': range,
|
||||
'sight.visionMode': sightMode,
|
||||
'sight.attenuation': visionModeData.attenuation,
|
||||
'sight.brightness': visionModeData.brightness,
|
||||
'sight.saturation': visionModeData.saturation,
|
||||
'sight.contrast': visionModeData.contrast
|
||||
}
|
||||
console.log(`Updating ${token.name}:`, sightType, bright, data)
|
||||
await token.update(data)
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
Reference in New Issue
Block a user