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

Calculate Range Between 2 Objects on Player Active Page - Including Hex Grid Maps

1489881453

Edited 1490769925
March 29, 2017 Updated to v1.03 including suggestions/updates from 'The Aaron', many thanks to him and 'Jim W.' for finding and resolving some issues. 2D only version <a href="https://gist.github.com/kilthar/724f03cf2ad327d71f" rel="nofollow">https://gist.github.com/kilthar/724f03cf2ad327d71f</a>... 3D version <a href="https://gist.github.com/kilthar/4decaa1d45def15929" rel="nofollow">https://gist.github.com/kilthar/4decaa1d45def15929</a>... !range @{target|1st Target|token_id},@{target|2nd Target|token_id} March 19, 2017 I've just finished (hopefully) a modified version of "Range Finder by Peter W." to include all measurement types for Square and Hex grid maps. For Hex maps it also calculates the distance between objects in the same hex (typically "Is Drawing" objects) to 3 decimal places. File can be found <a href="https://gist.github.com/kilthar/724f03cf2ad327d71f" rel="nofollow">https://gist.github.com/kilthar/724f03cf2ad327d71f</a>... Usage is changed to token_id - thanks SkyCaptainXIII: !range @{target|1st Target|token_name},@{target|2nd Target|token_name} !range @{target|1st Target|token_id},@{target|2nd Target|token_id} 1st version of calculating distance including height for 4e and 3e/pathfinder taking into account that you are 1unit square in size. Others to follow. This uses Bar 2 (blue bar left box) for the tokens height for 4e/3e/pathfinder in feet File can be found <a href="https://gist.github.com/kilthar/4decaa1d45def15929" rel="nofollow">https://gist.github.com/kilthar/4decaa1d45def15929</a>... Usage is: !range @{target|1st Target|token_id},@{target|2nd Target|token_id} Set the Bar 2 (blue bar left box) to the tokens height in the units you use eg. 30 for 30 feet
It might be better to use token_id instead of token_name since token_id should be unique.
Great script - modified it to give me distance in feet; rounded to hex size as that is what I need for my game - the game range.&nbsp;
Next project for me is to change this to calculate range to a Flying creature!
1490031912

Edited 1490032007
I was thinking on that one as well probably utilising Bar3 and a something like !range @{target|1st Target|token_id},@{target|2nd Target|token_id},3D to enable that calculation.
1490045058

Edited 1490045088
Mine uses the fluffy-wings and angel-outfit settings for 'hex' and '10xhex' height - so can reach 9x5+9x50 feet
1490461988

Edited 1490462072
While amending/Testing my changes 4 days ago I suddenly ran into strange errors that did not relate to changes I was making .. and as seemed so random, gave up. Today I have reinstalled the api script from the github and disabled other scripts and still getting these errors. Tested in a new game without any other api scripts or char sheet - same issue:- TypeError: Cannot read property 'get' of undefined TypeError: Cannot read property 'get' of undefined at apiscript.js:34:28 at eval (eval at &lt;anonymous&gt; (/home/node/d20-api-server/api.js:146:1), &lt;anonymous&gt;:65:16) at Object.publish (eval at &lt;anonymous&gt; (/home/node/d20-api-server/api.js:146:1), &lt;anonymous&gt;:70:8) at /home/node/d20-api-server/api.js:1510:12 at /home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:560 at hc (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:39:147) at Kd (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:546) at Id.Mb (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:489) at Zd.Ld.Mb (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:94:425) at /home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:111:400 Something must(?) have changed on the Roll20 implementation?
Are you using !range @{target|1st Target|token_id},@{target|2nd Target|token_id}
d100GM said: Are you using !range @{target|1st Target|token_id},@{target|2nd Target|token_id} Yes and both your script and my modified one broke at same time!
Strange I've been using it without any issues over the past week. Just tried the original and 3D versions and both worked.
Odd then.. must be something my end.&nbsp; Cannot even guess what it might be though.
Could it be a conflict with another API? Try creating a new game with only that API loaded, if that works start adding each other API you use and see if you can identify any conflict.
As stated above - already did that :(
Was that a new game or an existing one? (just in case there is/was something corrupt)
Now I have 3 games, all with the identical script.&nbsp; 2 work and one does not.&nbsp; Oddly all 3 did not work yesterday, only the SKT game does not work now ..crashes with the error as above. I use hex maps and one reason I changed the code [I no longer have the changes as I deleted this version!] was that I needed the range to come in hex sized answers, not to 3 decimal places.
1490622021
The Aaron
Pro
API Scripter
My guess is that one of the tokens (token1 or token2) is not found and the .get() on line 34 or 35 is causing the error. &nbsp;Moving them down a few lines inside the if check should resolve it: &nbsp; &nbsp; &nbsp; &nbsp; var token1 = &nbsp;findObjs({_type: "graphic", layer:"objects", _pageid: curPageID, _id: tokenid1})[0]; &nbsp; &nbsp; &nbsp; &nbsp; var token2 = &nbsp;findObjs({_type: "graphic", layer:"objects", _pageid: curPageID, _id: tokenid2})[0]; &nbsp; &nbsp; &nbsp; &nbsp; var name1 = &nbsp;token1.get("name"); &nbsp; &nbsp; &nbsp; &nbsp; var name2 = &nbsp;token2.get("name"); &nbsp; &nbsp; &nbsp; &nbsp; if (token1 && token2) &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; var name1 = &nbsp;token1.get("name"); &nbsp; &nbsp; &nbsp; &nbsp; var name2 = &nbsp;token2.get("name"); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // DnD 4.0 Distance Calculation if (curDiagonalType == "foure" && curGridType == "square") { You could also add some checks in other places you're calling .get(), though they are less likely to be issues. &nbsp;Using the short-circuiting OR (||) for this works really well: &nbsp; &nbsp; &nbsp; &nbsp; var curPageID = ( findObjs({_type: "campaign"})[0] ||{get:()=&gt;''}) .get("playerpageid"); &nbsp; &nbsp; &nbsp; &nbsp; var curPage = ( findObjs({_type: "page", _id: curPageID})[0] ||{get:()=&gt;''}) ; //var currentPage = getObj("page", Campaign().get("playerpageid")); &nbsp; &nbsp; &nbsp; &nbsp; var curScale = ( curPage.get("scale_number") ||{get:()=&gt;1}) ; // scale for 1 unit, eg. 1 unit = 5ft&nbsp; var curUnit = ( curPage.get("scale_units") ||{get:()=&gt;'ft'}) ; // ft, m, km, mi etc. var curDiagonalType = ( curPage.get("diagonaltype") ||{get:()=&gt;'foure'}) ; // One of "foure", "pythagorean" (Euclidean), "threefive", or "manhattan" var curGridType = ( curPage.get("grid_type") ||{get:()=&gt;'square'}) ; // One of "square", "hex", or "hexr". (hex corresponds to Hex(V), and hexr corresponds to Hex(H)) I'm not sure why you're doing a findObjs() for campaign, when you can get it directly with Campaign(). &nbsp;My guess is it's just left over from a bughunt.
I'll test that change now.&nbsp; Although the rest of the comments are for the author not me!
1490622926

Edited 1490623002
OK so getting token not defined, but there is a token as I am clicking on it. Token not found undefined Token not found undefined I get two errors so seems every (?) token in SKT is not found .&nbsp; Ah hang on a minute, reading the code it seems to look on the current player page.&nbsp; As GM I am often on a different page.&nbsp; So moving the Players marker to the same page seems to fix this. So limits are: Current page must be the current Players Page and tokens must be on Token layer. Given I have started to use mapchange to allow players to roam the maps....something to know! Thanks TheAaron for helping me debug to find what was happening!
1490623654
The Aaron
Pro
API Scripter
This should fix the player page restriction. &nbsp;( UNTESTED ): on("chat:message", function(msg) { var findPlayerPage = function(playerid){ if(playerIsGM(playerid)){ let player=getObj('player',playerid), page=getObj('page',player.get('lastpage')); if(page){ return page; } } if(_.has(Campaign().get('playerspecificpages'),msg.playerid)) { let page=getObj('page',Campaign().get('playerspecificpages')[msg.playerid]); if(page){ return page; } } return getObj('page',Campaign().get('playerpageid')); }; &nbsp; &nbsp; if(msg.type == "api" && msg.content.indexOf("!range") != -1) &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; var useHeight = true; // true = use Target height difference, false to ignore height &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (msg.content.indexOf("!range ") != -1) &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var m = msg.content; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m = m.replace("!range ",""); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var l = m.length; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var p = m.indexOf(","); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tokenid1 = m.substring(0,p); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tokenid2 = m.substring(p+1,l); &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; var curPage = findPlayerPage(msg.playerid); &nbsp; &nbsp; &nbsp; &nbsp; var curScale = curPage.get("scale_number"); // scale for 1 unit, eg. 1 unit = 5ft&nbsp; var curUnit = curPage.get("scale_units"); // ft, m, km, mi etc. var curDiagonalType = curPage.get("diagonaltype"); // One of "foure", "pythagorean" (Euclidean), "threefive", or "manhattan" var curGridType = curPage.get("grid_type"); // One of "square", "hex", or "hexr". (hex corresponds to Hex(V), and hexr corresponds to Hex(H)) var gridSize = 70; &nbsp; &nbsp; &nbsp; &nbsp; var token1 = &nbsp;findObjs({_type: "graphic", layer:"objects", _pageid: curPage.id , _id: tokenid1})[0]; &nbsp; &nbsp; &nbsp; &nbsp; var token2 = &nbsp;findObjs({_type: "graphic", layer:"objects", _pageid: curPage.id , _id: tokenid2})[0];
While on this, the SKT map I am using has 10' per unit, and 0.5 unit grid, yet range is specified in terms of 10' grids.&nbsp; Roll20 correctly adds a 5' grid.&nbsp; So need to add the Grid Size to the calculations?
Thanks! Changing those 3 sections does fix the GM/Player page difference.&nbsp;
Thanks "The Aaron" I'll implement those changes
Jim W. said: While on this, the SKT map I am using has 10' per unit, and 0.5 unit grid, yet range is specified in terms of 10' grids.&nbsp; Roll20 correctly adds a 5' grid.&nbsp; So need to add the Grid Size to the calculations? It should be using the scale and unit in the calculatiions. I'll have a look tonight using your setting to test. Currently I've only used 1 unit and 5 feet.