Page 1 of 2

Moving a Token to a random coordinate

Posted: Sun Jan 21, 2018 8:40 pm
by chap126
How would one make a macro so that a token would move to a random coordinate within the visible window, or perhaps some set dimensions.

Re: Moving a Token to a random coordinate

Posted: Sun Jan 21, 2018 9:00 pm
by aliasmask
Depends on the nature of the move. If it's based on current position, then you'll need the current x,y. If you need it to be a visible square from your token then you'll need to use isVisible or canSeeToken depending on application. In either case, you'll need to use moveToken. The problem with random movement with no limitations is that you can move in to or through walls.

A basic scenario, move to random adjacent square:

Code: Select all

[H: moveToken(getTokenX(0)+1d3-2,getTokenY(0)+1d3-2)]
This will move token to current position from -1 to +1 in the X and Y direction with a chance of no movements.

Re: Moving a Token to a random coordinate

Posted: Sun Jan 21, 2018 10:06 pm
by chap126
I guess I should have mentioned what my aim is :oops:

I'm trying to simulate a drop chart in maptool, you know the one, where you have a sheet of paper with different boxes, and you throw some dice on there, and where the die lands and the number it shows on its face gives you some information. So I need a way to "throw" the dice in a confined area of the virtual table. Preferably on the .jpg die drop chart I pasted onto the virtual table. I'll look into moveToken more :D

I'm thinking outline my drop chart .jpg in vision blocking, put my "die" token within there, and somehow use the isVisible, to define the range of places the die can be moved to.

Re: Moving a Token to a random coordinate

Posted: Sun Jan 21, 2018 11:00 pm
by aliasmask
You may want to check out the animated dice thread: http://forums.rptools.net/viewtopic.php?f=8&t=18001

I also recall coveredInFish doing something.

edit: I just remember I made a map based off his drop-in here: http://forums.rptools.net/viewtopic.php ... 14#p193914

I'll share once I take a look at it.

Re: Moving a Token to a random coordinate

Posted: Mon Jan 22, 2018 9:15 am
by wolph42
well if you really want a visual dice roller strip (as it sounds like). The easiest is to create a strip of single line squares starting on x=0, y=0 (continuing with x=1, x=2, etc). Put the token on x=0 initially. And then you can use (lets call the token 'dice'):

Code: Select all

e.g. d6:
[H: moveToken(roll(1,6),0, 0, 'dice')]
3d4:
[H: moveToken(roll(3,4),0, 0, 'dice')]
etc

Re: Moving a Token to a random coordinate

Posted: Tue Jan 23, 2018 6:39 pm
by Phergus
I believe the OP is not just after dice roller but a drop chart or die drop table. Normally a piece of paper is divided into zones and then you drop a group of dice onto it. Each dice that drops into a zone has its value used to determine some attribute related to that zone. May use all the same type of die or different kinds.

Here is a basic die drop table:
DiceDrop1.jpg
DiceDrop1.jpg (144.6 KiB) Viewed 1326 times
[/url]
  • So place something like that as the background of a map.
  • Determine the dimensions of it from the top left corner as 'w' and 'h'.
  • In macro generate two rolls [xloc = 1dw] where w is the width
  • and [yloc = 1dh] where h is the height.
  • Determine die face with another roll to get image from table of die facs.
  • Plop token with correct image at that location.
  • Rinse, repeat.

Re: Moving a Token to a random coordinate

Posted: Tue Jan 23, 2018 7:34 pm
by metatheurgist
I think faced with that I'd just measure up the surface areas and convert it to percentages. :wink:

Re: Moving a Token to a random coordinate

Posted: Wed Jan 24, 2018 4:41 am
by wolph42
Phergus wrote:I believe the OP is not just after dice roller but a drop chart or die drop table.
roughly the same difference, instead of a line (1 dim) you have a surface (2 dim). So this:

Code: Select all

[H: moveToken(roll(1,6),0, 0, 'dice')] 
becomes this

Code: Select all

[moveToken(getTokenX(0,"roll_sheet") + roll(1,getTokenWidth("roll_sheet")), getTokenY(0,"roll_sheet") + roll(1,getTokenHeight("roll_sheet")), 1, "dice")] 
biggest diff is the fact that gettokenwidth can only return pixels not cells, so yo have to flag this in movetoken. The result will thus also not be bound by cells, but by pixel size.
Something that can be fixed with:

Code: Select all

[h:gs = json.get(json.get(getInfo("map"),"grid"),"size")]
[moveToken(
    getTokenX(1,"roll_sheet") + roll(1,getTokenWidth("roll_sheet")/gs), 
    getTokenY(1,"roll_sheet") + roll(1,getTokenHeight("roll_sheet")/gs), 
    1, 
    "dice"
)]  
note that I've added getTokenX/Y so you don't necessarily have to place the sheet at the origin!

edit: forgot to make it random!

Re: Moving a Token to a random coordinate

Posted: Wed Jan 24, 2018 4:59 am
by aliasmask
Unless you're going for special effect I say random chart is way to go. I don't think coding time for the special effect makes it worth it. But that's just me.

Re: Moving a Token to a random coordinate

Posted: Wed Jan 24, 2018 8:27 am
by Phergus
I imagine it is a mechanic that is much more interesting at the game table with actual dice.

Re: Moving a Token to a random coordinate

Posted: Wed Jan 24, 2018 8:56 pm
by chap126
Yes it's exactly that, but I intend to use it to determine where something the players are looking for is. See "Vornheim" supplement for more details.

EG Players Ask: Where's the apothecary?
<drop die onto map of city..it lands in foreign distance>
GM Answers: It's here, in the foreign district.

Players Ask: What's the layout of this shop, I want to rob it.
<drop a d4 and draw lines from it = to the number it rolls, inside a rectangular shape>
GM Answers: It looks like this.

Re: Moving a Token to a random coordinate

Posted: Thu Jan 25, 2018 8:12 am
by chap126
I think the pseudo-code would be something like...

1. Make 2 "corner" tokens and 1 "dice" token
2. Manually move the "corner" tokens to upper right and lower left corner of your drop chart
3. Get Corner tokens x and y and set to upperRightX, upperRightY,lowerLeftX,LowerLeftY
4. Width = upperRightX-lowerLeftX
5. Length = upperRightY-lowerLeftY
6. randomly generate a new x coordinate between 0 and Width, set to randomX
7. randomly generate a new y coordinate between 0 and Height, set to randomY
8. newX=lowerLeftX+randomX
9. newY=lowerLeftY+randomY
10. move "dice" token to (newX,newY)

Re: Moving a Token to a random coordinate

Posted: Thu Jan 25, 2018 9:09 am
by wolph42
that's the extensive description of the method I posted earlier; in which its clear that steps 1-5 are redundant and 7-10 can be done in one line of code...

note that "dice" and "roll_sheet" are respectively the token and the background map.

Re: Moving a Token to a random coordinate

Posted: Thu Jan 25, 2018 8:12 pm
by chap126
Nice! What's the /gs do? When I leave that in, the dice only stays in the upper left corner of the drop_area. When I remove it, the dice will occasionally pop outside of the drop area.

Re: Moving a Token to a random coordinate

Posted: Fri Jan 26, 2018 2:02 am
by wolph42
gs has the gridsize (typically 50 pixels per cell) as getWidth/height can only return the size of a token in pixels. So you have to divide it by gs to get the number of cells. Its quite possible that I overlooked something or used a 1 where it should be a 0. You can check the fuctions in the wiki
You can pull that line of code apart by putting everything in a variable and check those in mt to see where it goes wrong. e.g. [r:x= getTokenX(0)] [r:h=getTokenHeight()] etc.

edit: as I find it kind of an interesting topic, I tested it myself, here's the code:

Code: Select all

<!-- gridless -->
[h:moveToken(
    getTokenX(1,"roll_sheet") + roll(1,getTokenWidth("roll_sheet")), 
    getTokenY(1,"roll_sheet") + roll(1,getTokenHeight("roll_sheet")), 
    1, 
    "dice"
)] 

<!-- grid -->
<!-- best to have roll_sheet NOT snapped to grid and sized such that its a few pixels within the borders of the outer gridlines -->
[h:gs = json.get(json.get(getInfo("map"),"grid"),"size")]<br>
[h:moveToken(
    getTokenX(0,"roll_sheet")-1 + roll(1,getTokenWidth("roll_sheet")/gs+1), 
    getTokenY(0,"roll_sheet")-1 + roll(1,getTokenHeight("roll_sheet")/gs+1), 
    0, 
    "dice_1"
)]