Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 KB | None | 0 0
  1. // Computes the distances between 2 tokens on the active player page
  2. // Assumes each square grid is 70 pixels, scaling is used to calculate hex grid maps.
  3. // Script command
  4. // !range @{target|1st Target|token_id},@{target|2nd Target|token_id}
  5. // Set useHeight = true to use Bar2 to calculate range including height difference between targets (bar2 is units of page scale eg. 5ft.) false to ignore height.
  6. // Height calculation currently assumes a medium sized creature (1 unit, eg. 5ft) so a target with 1 unit (5 ft) elevation in an adjacent square is 1 unit away.
  7.  
  8. on("chat:message", function(msg) {
  9. if(msg.type == "api" && msg.content.indexOf("!range") != -1)
  10. {
  11. var useHeight = true; // true = use Target height difference, false to ignore height
  12.  
  13. if (msg.content.indexOf("!range ") != -1)
  14. {
  15. var m = msg.content;
  16. m = m.replace("!range ","");
  17. var l = m.length;
  18.  
  19. var p = m.indexOf(",");
  20. tokenid1 = m.substring(0,p);
  21. tokenid2 = m.substring(p+1,l);
  22. }
  23.  
  24. var curPageID = findObjs({_type: "campaign"})[0].get("playerpageid");
  25. var curPage = findObjs({_type: "page", _id: curPageID})[0]; //var currentPage = getObj("page", Campaign().get("playerpageid"));
  26. var curScale = curPage.get("scale_number"); // scale for 1 unit, eg. 1 unit = 5ft
  27. var curUnit = curPage.get("scale_units"); // ft, m, km, mi etc.
  28. var curDiagonalType = curPage.get("diagonaltype"); // One of "foure", "pythagorean" (Euclidean), "threefive", or "manhattan"
  29. var curGridType = curPage.get("grid_type"); // One of "square", "hex", or "hexr". (hex corresponds to Hex(V), and hexr corresponds to Hex(H))
  30. var gridSize = 70;
  31.  
  32. var token1 = findObjs({_type: "graphic", layer:"objects", _pageid: curPageID, _id: tokenid1})[0];
  33. var token2 = findObjs({_type: "graphic", layer:"objects", _pageid: curPageID, _id: tokenid2})[0];
  34. var name1 = token1.get("name");
  35. var name2 = token2.get("name");
  36.  
  37. if (token1 && token2)
  38. {
  39. // DnD 4.0 Distance Calculation
  40. if (curDiagonalType == "foure" && curGridType == "square")
  41. {
  42. var lDist = Math.abs(token1.get("left")-token2.get("left"))/gridSize;
  43. var tDist = Math.abs(token1.get("top")-token2.get("top"))/gridSize;
  44. var zDist = Math.abs((token1.get("bar2_value")-token2.get("bar2_value"))/curScale);//gridSize;
  45. var t1Height = token1.get("height");// to use for token size variation in the fiture
  46. if (zDist >= 1)
  47. {
  48. zDist = zDist - 1;// based on medium size tokens reduce effective height difference by 1 unit as token is a cube
  49. }
  50. if (useHeight != true) zDist = 0;
  51. var dist = 0;
  52. dist = Math.floor(Math.max(Math.min(lDist, tDist) + Math.abs(lDist - tDist), zDist));
  53. //dist = Math.floor(Math.min(lDist, tDist) + Math.abs(lDist - tDist));
  54. var distSQ = dist;
  55. dist = dist * curScale;
  56. var finalMessage = ("Distance between "+name1+" and "+name2+" = (" +distSQ+") "+dist+" "+curUnit);
  57. sendChat("RangeFinder",'<div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;">'+finalMessage+'</div>');
  58. }
  59.  
  60. // DnD 3.5 / Pathfinder Distance Calculation
  61. if (curDiagonalType == "threefive" && curGridType == "square")
  62. {
  63. var lDist = Math.abs(token1.get("left")-token2.get("left"));
  64. var tDist = Math.abs(token1.get("top")-token2.get("top"));
  65. var zDist = Math.abs((token1.get("bar2_value")-token2.get("bar2_value"))/curScale)*gridSize;
  66. var t1Height = token1.get("height");// to use for token size variation in the fiture
  67. if (zDist >= 70)
  68. {
  69. zDist = zDist - 70;// based on medium size tokens reduce effective height difference by 1 unit as token is a cube
  70. }
  71. if (useHeight != true) zDist = 0;
  72. var dist = 0;
  73. dist = Math.floor(1.5 * Math.min(lDist, tDist) + Math.abs(lDist - tDist));
  74. zDist = 1.118033988749895 * zDist;//scaling factor for height calculation
  75. dist = Math.sqrt(dist * dist + zDist * zDist);
  76. dist = dist * curScale / gridSize;
  77. dist = curScale * (Math.round(dist / curScale));// rounds to nearest scale unit eg. 5 ft.
  78. var finalMessage = ("Distance between "+name1+" and "+name2+" = " +dist+" "+curUnit);
  79. sendChat("RangeFinder",'<div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;">'+finalMessage+'</div>');
  80. }
  81.  
  82. // Euclidean Distance Calculation
  83. if (curDiagonalType == "pythagorean" && curGridType == "square")
  84. {
  85. var lDist = Math.abs(token1.get("left")-token2.get("left"))/gridSize;
  86. var tDist = Math.abs(token1.get("top")-token2.get("top"))/gridSize;
  87. var zDist = Math.abs((token1.get("bar2_value")-token2.get("bar2_value"))/curScale);//gridSize;
  88. var dist = 0;
  89. //dist = Math.sqrt(lDist * lDist + tDist * tDist);
  90. dist = Math.sqrt(lDist * lDist + tDist * tDist + zDist * zDist);
  91. var distSQ = dist;
  92. dist = dist * curScale;
  93. dist = Math.round(dist * 10) / 10;
  94. var finalMessage = ("Distance between "+name1+" and "+name2+" = " +dist+" "+curUnit);
  95. sendChat("RangeFinder",'<div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;">'+finalMessage+'</div>');
  96. }
  97.  
  98. // Manhattan Distance Calculation
  99. if (curDiagonalType == "manhattan" && curGridType == "square")
  100. {
  101. var lDist = Math.abs(token1.get("left")-token2.get("left"))/gridSize;
  102. var tDist = Math.abs(token1.get("top")-token2.get("top"))/gridSize;
  103. var dist = 0;
  104. dist = Math.round(lDist + tDist);
  105. var distSQ = dist;
  106. dist = dist * curScale;
  107. var finalMessage = ("Distance between "+name1+" and "+name2+" = (" +distSQ+") "+dist+" "+curUnit);
  108. sendChat("RangeFinder",'<div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;">'+finalMessage+'</div>');
  109. }
  110.  
  111. // Hex(V) Distance Calculation - Including distance between "Is Drawing" objects to 3 decimal places
  112. if (curGridType == "hex")
  113. {
  114. var lDist = Math.abs(token1.get("left")-token2.get("left"));
  115. var tDist = Math.abs(token1.get("top")-token2.get("top"));
  116. var dist = 0;
  117. lDist = lDist / 75.19856198446026; // Hex Scaling Factor
  118. tDist = tDist / 66.96582782426833; // Hex Scaling Factor
  119. dist = 1.5 * Math.min(lDist, tDist) + Math.abs(lDist - tDist);
  120. dist = Math.round(dist * curScale * 1000) / 1000; // Decimal point accuracy limit
  121. var finalMessage = ("Distance between "+name1+" and "+name2+" = " +dist+" "+curUnit);
  122. sendChat("RangeFinder",'<div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;">'+finalMessage+'</div>');
  123. }
  124.  
  125. // Hex(H) Distance Calculation - Including distance between "Is Drawing" objects to 3 decimal places
  126. if (curGridType == "hexr")
  127. {
  128. var lDist = Math.abs(token1.get("left")-token2.get("left"));
  129. var tDist = Math.abs(token1.get("top")-token2.get("top"));
  130. var dist = 0;
  131. lDist = lDist / 69.58512749037783; // Hex Scaling Factor
  132. tDist = tDist / 79.68878998350463; // Hex Scaling Factor
  133. dist = 1.5 * Math.min(lDist, tDist) + Math.abs(lDist - tDist);
  134. dist = Math.round(dist * curScale * 1000) / 1000; // Decimal point accuracy limit
  135. // var finalMessage = ("Distance between "+name1+" and "+name2+" = " +dist+" "+curUnit+" left "+lDist+" top "+tDist);
  136. var finalMessage = ("Distance between "+name1+" and "+name2+" = " +dist+" "+curUnit);
  137. sendChat("RangeFinder",'<div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;">'+finalMessage+'</div>');
  138. }
  139. }
  140. else
  141. {
  142. if (!token1)
  143. {
  144. sendChat("","Token not found "+ name1 );
  145.  
  146. }
  147. if (!token2)
  148. {
  149. sendChat("","Token not found "+ name2 );
  150.  
  151. }
  152. }
  153. }
  154.  
  155.  
  156. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement