Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Request: Using API to change token depth.

Request #1: It would be really great if I could use API so that a simple command would bring whatever is selected to the front. It would save time in-game to not have to click through menus. Request #2: Is it possible to randomize a token's depth? I feel like it would be a lot easier to make card decks interactive if they could be placed on the table and shuffled right there. Assuming it's not possible (I'm guessing "To Front" and "To Back" are the only options), is it possible so that every selected token (for example a stack of 52 cards) can have a random chance of moving to front, to back, or staying where it is? I feel like decks could be sufficiently randomized if you did this about 7 times. If anyone has time to write either of these, I would really appreciate it.
1418580397
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
on('chat:message', function(msg) { if(msg.type == 'api' && msg.selected && msg.content.indexOf('!rotate') == 0){ var selectedObjs = msg.selected; _.each(selectedObjs, function(obj) { if(obj._type == 'graphic'){ var token = getObj('graphic', obj._id); var rotation = token.get("rotation"); //Find the current rotation value rotation = rotation + 45; //add 45 degrees, this could also be 90. token.set({rotation: rotation}); //set the rotation }; }); }; })
1418580421
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
Something like that... Thought there was a script around here for that.
Thanks, but I think you might have posted this on the wrong thread. I recently asked a question about rotation, but this one is about depth.
1418581494
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
Thought I was posting in this thread: <a href="https://app.roll20.net/forum/post/1422312/request-" rel="nofollow">https://app.roll20.net/forum/post/1422312/request-</a>...
1418581616

Edited 1418581656
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
And the above would basically work for this too. on('chat:message', function(msg) { if(msg.type == 'api' && msg.selected && msg.content.indexOf('!shuffle') == 0){ var selectedObjs = msg.selected; _.each(selectedObjs, function(obj) { if(obj._type == 'graphic'){ var token = getObj('graphic', obj._id); //random chance to be pushed forward or backward code here. }; }); }; })
And what would be the code for the random chance of being pushed forward or backward?
1418582306
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
So its a coin toss.... google "javascript 50 50 chance"... find some examples.
Even if I found it, I wouldn't know what to run that would change token depth. Sorry, but I'm pretty much API illiterate at this point.
1418587686

Edited 1418588721
Well, I found a way to trick it a little bit. I put all the cards on the table, highlighted the deck, and hit Ctrl+U (by the way, is there a way to do this function with API so I can put it all into one macro?) I randomized the turn order using this script: <a href="https://app.roll20.net/forum/post/439144/script-ra" rel="nofollow">https://app.roll20.net/forum/post/439144/script-ra</a>... Used the !eot (end of turn) function on Aaron's Turn Marker script (can't find the source) to pass the turn 100 times, moving a token to the front each time. I'm not sure which script is causing the problem here, but I still only get semi-randomization, and clumps in the card deck tend to not really move. Also, the big turn marker icon in the backs a little clunky, but for now it's a good enough fix. Also, since there's so much going on, it tends to crash when it's done shuffling. Anybody have any thoughts on this? Or if there is a better way to pull this off, please do tell. Edit: I'm sure the cleaner solution somehow uses toFront(obj) and toBack(obj) , but I'm just not clear about the syntax.
1418590707

Edited 1418590717
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
Are you trying to shuffle cards or tokens?
1418591567

Edited 1418591698
Cards as tokens. On the table. Edit: It's the only way to do deck-building games like Dominion or Legendary, plus it's the only way I know to do effects like Scry in Magic, among others. Plus, it's a way to keep a card deck in a static place on the table instead of holding it in a sidebar. If you can put every token in a deck on the table, stack them together, and randomize depth, the deck shuffles itself.
1418591841
The Aaron
Roll20 Production Team
API Scripter
Robert, those are some nice ideas. ToFront/ToBack I will definitely add to TokenMod. I like the random depth ordering idea. It would actually be quite easy, and wouldn't require you to do it 7 times to be random. Here is a version that uses _.shuffle(): var RandomDepth = RandomDepth || (function() { 'use strict'; var version = 0.1, handleMessages = function(msg) { if('api' !== msg.type ) { return; } var args = msg.content.split(/\s+/); switch(args.shift()) { case '!random-depth': var objs =_.chain(msg.selected) .map(function(o){ return getObj(o._type,o._id); }) .reject(_.isUndefined) .shuffle() .each(function(o){ toFront(o); }); break; } }, registerEventHandlers = function(){ on('chat:message',handleMessages); }; return { RegisterEventHandlers: registerEventHandlers }; }()); on('ready',function(){ 'use strict'; RandomDepth.RegisterEventHandlers(); }); I tried to make a version that used randomInteger(), but for some reason, couldn't get _.reduce() to change the state of the memo without getting a sandbox error. I suspect there is a bug in _.reduce() (but that might just be hubris... ) Anyway, this works well enough, though perhaps is not cryptographically strong.
1418592460

Edited 1418592821
When I fan out a deck of 30 cards, highlight all of them, and run !random-depth , it appears that only one of the 30 cards changes depth at a time. And clumps still remain in the deck. I tested it by putting all 4 aces at the highest layer, dropped them on top of the deck, and shuffled several times. The 4s tended to stay together after every shuffle. Edit: It may be tedious, but I've sort of solved this the same way as before. My shuffle macro runs !random-depth 100 times, and that seems to help.
1418606070
The Aaron
Roll20 Production Team
API Scripter
I'm thinking if I wrote a recursive version with set timeout it might work better. I'll give thst a try....
1418659899
The Aaron
Roll20 Production Team
API Scripter
Ok, recursive setTimeout() version seems to do a better job. See what you think... var RandomDepth = RandomDepth || (function() { 'use strict'; var version = 0.2, randomToFrontList = function (l) { if( l.length ) { var i = randomInteger(l.length)-1; toFront(l[i]); if( l.length &gt; 1) { setTimeout(_.partial(randomToFrontList,_.without(l,l[i])),5); } } }, handleMessages = function(msg) { if('api' !== msg.type ) { return; } var args = msg.content.split(/\s+/), objs; switch(args.shift()) { case '!random-depth': objs = _.chain(msg.selected) .map(function(o){ return getObj(o._type,o._id); }) .reject(_.isUndefined) .value(); randomToFrontList(objs); break; } }, registerEventHandlers = function(){ on('chat:message',handleMessages); }; return { RegisterEventHandlers: registerEventHandlers }; }()); on('ready',function(){ 'use strict'; RandomDepth.RegisterEventHandlers(); });
1418661685

Edited 1418662600
Lithl
Pro
Sheet Author
API Scripter
Robert R. said: I feel like decks could be sufficiently randomized if you did this about 7 times. A perfect* riffle shuffle will completely randomize a deck of 52 cards when performed approximately 7 times. With the GSR model, you need 3/2 ln(N)/ln(2) shuffles (ln(N)/ln(2) == base-2 logarithm of N). With N=52, the GSR model predicts ~8.55 shuffles, although GSR tends to slightly overestimate the requirement. (Perfect* riffling a deck too many times never makes the deck less random, though, while doing it too few times easily causes the deck to be not random enough, so an overestimate is much preferred.) Note that randomly putting a card on top of the stack or bottom of the stack is not the same as a riffle shuffle, however. As a computer program, we can take the collection C, randomly select one item to move to the front (or the back, it does not matter so long as we always pick the same direction), and then remove the item from C. Repeat until C is empty, and the items that were in C originally will be layered randomly. There is no need to repeat the full process at all. * Perfect meaning "perfectly random" not "perfectly interleaved".
1418663619
The Aaron
Roll20 Production Team
API Scripter
Brian said: Note that randomly putting a card on top of the stack or bottom of the stack is not the same as a riffle shuffle, however. As a computer program, we can take the collection C, randomly select one item to move to the front (or the back, it does not matter so long as we always pick the same direction), and then remove the item from C. Repeat until C is empty, and the items that were in C originally will be layered randomly. There is no need to repeat the full process at all. Which is basically the Fisher-Yates shuffle that _.shuffle() implements. I think the issue I was having with using it in version 0.1 was due to the API's toFront() calls getting dropped, as the results of the shuffle looked reasonably random in the array, but not on the table. I didn't check that the ordering was the same between them. I should test that. Adding the 5ms wait between toFront() calls seems to allow the random ordering to take though.
Adding the 5ms wait between toFront() calls seems to allow the random ordering to take though. On a somewhat unrelated note, would a 5ms wait also be a solution to your !emas problem? Seems useful.
1418669764
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
Robert R. said: Adding the 5ms wait between toFront() calls seems to allow the random ordering to take though. On a somewhat unrelated note, would a 5ms wait also be a solution to your !emas problem? Seems useful. 5ms you say? I think I noticed delete items don't leave the Z Order field.
1418670636
The Aaron
Roll20 Production Team
API Scripter
Robert R. said: Adding the 5ms wait between toFront() calls seems to allow the random ordering to take though. On a somewhat unrelated note, would a 5ms wait also be a solution to your !emas problem? Seems useful. No, you'd have to introduce the 5ms delay to the chat messages that result in text. The issue is that they are sent to the clients before the API has a chance to process and send it's own message. Better in that regard would be to implement a !say or similar that would then be in the same pipeline but would do nothing more than imitate the regular chat commands. Stephen S. said: 5ms you say? I think I noticed delete items don't leave the Z Order field. That shouldn't be affecting the toFront()s though, as they should establish the new top order regardless of the Z Ordered ghosts.
If I wanted to force a token to front or back (no random chance) is there any way I could use this script to do it, or would I need something entirely new?
1418793832
The Aaron
Roll20 Production Team
API Scripter
This script only moved things to the front. You could select a single thing and use it to move it to the front, but it would be a bit overkill. Can write a simple command to to it tomorrow if you like.