Looks like there has been some change to the way the grid type is specified since this script was created (or perhaps it was just never used with Hex (H) back then). Anyway, here's a corrected script, along with a few error message improvements: // 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) {
var n = msg.content.split(/\s+/, 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')) {
return 'Cannot measure distance between tokens on different pages';
}
var page = getObj('page', token1.get('pageid'));
var gridType = page.get('grid_type');
switch(gridType) {
case 'hex':
case 'hexv':
return hexVDistance([token1.get("left"), token1.get("top")], [token2.get("left"), token2.get("top")]);
case 'hexr':
case 'hexh':
return hexHDistance([token1.get("left"), token1.get("top")], [token2.get("left"), token2.get("top")]);
default:
return '(square grid not supported)';
}
}
function hexHDistance(unit1, unit2) {
var q1, q2, r1, r2;
q1 = Math.round((unit1[0] - 46.48512749037782) / 69.58512749037783);
r1 = Math.round((unit1[1] - 39.8443949917523) / 39.8443949917523);
r1 = Math.floor(r1 / 2);
q2 = Math.round((unit2[0] - 46.48512749037782) / 69.58512749037783);
r2 = Math.round((unit2[1] - 39.8443949917523) / 39.8443949917523);
r2 = Math.floor(r2 / 2);
return cubeDistance(oddQToCube(q1, r1), oddQToCube(q2, r2));
}
function hexVDistance(unit1, unit2) {
var q1, q2, r1, r2;
q1 = Math.round((unit1[0] - 37.59928099223013) / 37.59928099223013);
r1 = Math.round((unit1[1] - 43.86582782426834) / 66.96582782426833);
q1 = Math.floor(q1 / 2);
q2 = Math.round((unit2[0] - 37.59928099223013) / 37.59928099223013);
r2 = Math.round((unit2[1] - 43.86582782426834) / 66.96582782426833);
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]));
}