Austin B. posted a great script at the link below that lets you automatically determine the distance between any two tokens on a hex grid. <a href="https://app.roll20.net/forum/post/973044/script-he" rel="nofollow">https://app.roll20.net/forum/post/973044/script-he</a>... The problem I had with the script, however, is that it does not account for changing the grid unit size. For example, I'm planning to run a GURPS game and want to change the hex unit size to 0.6 so that I can use GURPS' 1 yd. hexes with maps scaled to D&D's 5 ft. squares. I thought I would share my modified script, and the referenced thread is locked. The script is below. EDIT: Is there a better way to post code in this forum? Like a way to add a <pre> tag or something? // Computes the distances between 2 tokens by ID
// Use the macro target picker to fill in the ID
// Script command
// !range @{target|1st Target|token_id} @{target|2nd Target|token_id}
on("chat:message", function(msg) {
if(msg.type == "api" && msg.content.indexOf("!range") != -1) {
log(msg);
var n = msg.content.split(" ", 4)
var cmd = n[0];
var id1 = n[1];
var id2 = n[2];
var token1 = getObj("graphic", id1);
var token2 = getObj("graphic", id2);
if (token1 && token2) {
var distance = tokenDistance(token1, token2);
sendChat("", "/desc Distance between Token1 & Token2 is " + distance);
} else {
if (!token1)
sendChat("Range Finder","/w " + msg.who + " Could not locate first token!");
if (!token2)
sendChat("Range Finder","/w " + msg.who + " Could not locate second token!");
}
}
});
function tokenDistance(token1, token2) {
if (token1.get('pageid') != token2.get('pageid')) {
log('Cannot measure distance between tokens on different pages');
return;
}
var distance;
var page = getObj('page', token1.get('pageid'));
var gridType = page.get('grid_type');
var scale = page.get('snapping_increment');
switch(gridType) {
case 'hex':
distance = hexVDistance([token1.get("left"), token1.get("top")], [token2.get("left"), token2.get("top")], scale);
break;
case 'hexr':
distance = hexHDistance([token1.get("left"), token1.get("top")], [token2.get("left"), token2.get("top")], scale);
break;
}
return distance;
}
function hexHDistance(unit1, unit2, scale) {
var q1, q2, r1, r2;
q1 = Math.round((unit1[0] - 46.48512749037782) / (69.58512749037783 * scale));
r1 = Math.round((unit1[1] - (39.8443949917523 * scale)) / (39.8443949917523 * scale));
r1 = Math.floor(r1 / 2);
q2 = Math.round((unit2[0] - 46.48512749037782) / (69.58512749037783 * scale));
r2 = Math.round((unit2[1] - (39.8443949917523 * scale)) / (39.8443949917523 * scale));
r2 = Math.floor(r2 / 2);
return cubeDistance(oddQToCube(q1, r1), oddQToCube(q2, r2));
}
function hexVDistance(unit1, unit2, scale) {
var q1, q2, r1, r2;
q1 = Math.round((unit1[0] - (37.59928099223013 * scale)) / (37.59928099223013 * scale));
r1 = Math.round((unit1[1] - 43.86582782426834) / (66.96582782426833 * scale));
q1 = Math.floor(q1 / 2);
q2 = Math.round((unit2[0] - (37.59928099223013 * scale)) / (37.59928099223013 * scale));
r2 = Math.round((unit2[1] - 43.86582782426834) / (66.96582782426833 * scale));
q2 = Math.floor(q2 / 2);
return cubeDistance(oddRToCube(q1, r1), oddRToCube(q2, r2));
}
function oddRToCube(q, r) {
var x, y, z;
x = q - (r - (r & 1)) / 2;
z = r;
y = -x - z;
return [x, y, z];
}
function oddQToCube(q, r) {
var x, y, z;
x = q;
z = r - (q - (q & 1)) / 2;
y = -x - z;
return [x, y, z];
}
function cubeDistance(cube1, cube2) {
return Math.max(Math.abs(cube1[0] - cube2[0]), Math.abs(cube1[1] - cube2[1]), Math.abs(cube1[2] - cube2[2]));
}