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

[Script] Hex Grid Range Finder

July 02 (11 years ago)

Edited July 02 (11 years ago)
I just managed to create a API Script that allows you to determine the distance between two tokens on a Hex (H) Grid. I just managed to finish this and I will be updating this later to include Hex (V) Grids. For now this is a proof of concept that you guys can test for yourselves. For right now the Macro I am currently using to run this is "!token @{target|Token1|token_id} @{target|Token2|token_id}".

https://gist.github.com/e5124cfa7c8c331c70a9.git
July 02 (11 years ago)
The Aaron
Roll20 Production Team
API Scripter
Cool!

For the two error sendChat() calls, you might want to /w the caller to cut down on chat spam. Also, I don't think name2 will be defined in the second one.
The extra output was meant for debugging purposes so that I knew where the tokens were located on the Hex Grid. Planning on cutting that when I update it later.

As for name2 it was left over from the Range Finder API Script since I heavily modified that to get this working.
Just updated my script to include Hex (V) Grids and created some functions to simplify some of the math. Should be easier to follow how things work but I will list what is going on with the math.

  1. Locate where on the grid the token is located.
  2. Convert coordinates to either "Odd-Q" or "Odd-R" coordinates depending on which grid layout is used.
  3. Convert the "Odd-Q" or "Odd-R" coordinates to a 3D coordinate system.
  4. Locate distance between the two points.
Hex Grid Range Finder Source
July 02 (11 years ago)
Gold
Forum Champion
Is the Ruler tool wrong when you're on Hex grids?

I've been using it, just the regular non-API Ruler tool.
From what I can tell there is nothing wrong with the Ruler Tool on Hex Grids, however it can be useful for others who want to create scripts that need to determine the distance between two tokens automatically. For instance I am creating a script for my own GURPS Campaign that helps users in determining the range penalty for shooting at targets so far away and such. However since there is no easy way to determine distances between tokens via API you have to create your own method of figuring it out.

Also I read somewhere when I was looking into this that Roll20 counts the individual hexes between the two points and if they are using a brute force method like that then something like this might make it more optimized. Of course I don't know if that has changed or not, but this can be useful to some people who need it.
July 03 (11 years ago)
The Aaron
Roll20 Production Team
API Scripter
The major problem I have with the ruler in hex mode is that it only measures in units, no fractions. On the square grid, you can hold alt to measure more precise distances. On the hex grid, the same is not true.
Unfortunately unless Roll20 decides to redo the hexes it might never be possible to measure more precisely. At the moment it seems that hexes aren't 100% perfect and are either a little taller or wider than they should be depending on orientation.
July 03 (11 years ago)
The Aaron
Roll20 Production Team
API Scripter
I don't need pixel perfect, but it would be nice to be able to measure the distance between 2 features that happen to be on the same hex and get a number that is at least plausible.

Aaron said:

I don't need pixel perfect, but it would be nice to be able to measure the distance between 2 features that happen to be on the same hex and get a number that is at least plausible.

If you look at the Hex (H) Grid at examine the distance between the center of one hex to the center of each of it's 6 neighbors you will find that there is some inconsistencies. For the North/South neighbors the distance is going to be 79.68878998350459, while the 4 other neighbors are going to be 80.18519676418406. On a small scale the error is going to be less noticeable, however the second you start doing any measuring on a large scale the error is going to start sticking out as a sore thumb.