Skip to content

Instantly share code, notes, and snippets.

@keithcurtis1
Created May 25, 2020 16:51
Show Gist options
  • Save keithcurtis1/e157b59425767ef2723211f985ac9610 to your computer and use it in GitHub Desktop.
Save keithcurtis1/e157b59425767ef2723211f985ac9610 to your computer and use it in GitHub Desktop.
This Roll20 script sets the has sight setting for mass tokens. Use with caution. Command is: !set-has-sight --[true|false] --[npc|pc|all]
on('ready',()=>{
const playerCanControl = (obj, playerid='any') => {
const playerInControlledByList = (list, playerid) => list.includes('all') || list.includes(playerid) || ('any'===playerid && list.length);
let players = obj.get('controlledby')
.split(/,/)
.filter(s=>s.length);
if(playerInControlledByList(players,playerid)){
return true;
}
if('' !== obj.get('represents') ) {
players = (getObj('character',obj.get('represents')) || {get: function(){return '';} } )
.get('controlledby').split(/,/)
.filter(s=>s.length);
return playerInControlledByList(players,playerid);
}
return false;
};
const setHasSightByClass = (type, value) => {
let filter = () => true;
switch(type.toLowerCase()){
default:
case 'npcs':
filter=(t)=> ! playerCanControl(t);
break;
case 'players':
filter=(t)=> playerCanControl(t);
break;
case 'all':
break;
}
let tokens = findObjs({
type: 'graphic',
subtype: 'token',
light_hassight: !value // only need to look at ones we would be changing =D
}).filter(filter);
let tokenCount = tokens.length;
const burndown = () => {
if(tokens.length) {
let t = tokens.shift();
if(t) {
t.set({
light_hassight: value
});
}
setTimeout(burndown,0);
} else {
sendChat('',`/w gm Set ${tokenCount} token(s) to ${value?'Has Sight':'No Sight'}`);
}
};
burndown();
};
on('chat:message',(msg)=>{
if('api' === msg.type && /!set-has-sight\b/i.test(msg.content)){
let args = msg.content.split(/\s+/).map(s=>s.toLowerCase());
let type = 'npcs';
let value = false;
if(args.includes('--npcs') || args.includes('--npc')){
type = 'npcs';
} else if(args.includes('--pcs') || args.includes('--pc') || args.includes('--players') || args.includes('--player') ){
type = 'players';
} else if(args.includes('--all') ){
type = 'all';
}
if(args.includes('--sight') || args.includes('--true') ) {
value = true;
} else if( args.includes('--blind') || args.includes('--false') ) {
value = false;
}
setHasSightByClass(type,value);
}
});
});
@keithcurtis1
Copy link
Author

This Roll20 script sets the has sight setting for mass tokens. Use with caution since it will affect all tokens in the game. Its intended use is to globally change sight settings in a purchased module.

Command is:

!set-has-sight --[true|false] --[npc|pc|all]

PC is defined as any token with a specified player controller

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment