Skip to content

Instantly share code, notes, and snippets.

@palikhov
Forked from keithcurtis1/Mass Sight
Created January 4, 2022 06:58
Show Gist options
  • Save palikhov/2f759dad08f2703c6e24cabadd1696da to your computer and use it in GitHub Desktop.
Save palikhov/2f759dad08f2703c6e24cabadd1696da 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);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment