Guest User

Untitled

a guest
Dec 7th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.23 KB | None | 0 0
  1. package org.rsbot.script.methods;
  2.  
  3. import org.rsbot.script.internal.wrappers.TileData;
  4. import org.rsbot.script.web.Route;
  5. import org.rsbot.script.web.RouteStep;
  6. import org.rsbot.script.web.Teleport;
  7. import org.rsbot.script.web.TransportationHandler;
  8. import org.rsbot.script.wrappers.RSTile;
  9. import org.rsbot.script.wrappers.RSWeb;
  10.  
  11. import java.util.*;
  12.  
  13. /**
  14. * The web class.
  15. *
  16. * @author Timer
  17. */
  18. public class Web extends MethodProvider {
  19. public static final HashMap<RSTile, Integer> rs_map = new HashMap<RSTile, Integer>();
  20. public static boolean loaded = false;
  21.  
  22. Web(final MethodContext ctx) {
  23. super(ctx);
  24. }
  25.  
  26. /**
  27. * Gets the closest supported bank in runescape that is usable.
  28. *
  29. * @param tile The tile to look off of.
  30. * @return The closest bank's tile.
  31. */
  32. public RSTile getNearestBank(final RSTile tile) {
  33. double dist = -1.0D;
  34. RSTile finalTile = null;
  35. final RSTile[] BANKS = {new RSTile(3093, 3243, 0), new RSTile(3209, 3219, 2), new RSTile(3270, 3167, 0),
  36. new RSTile(3253, 3421, 0), new RSTile(3188, 3437, 0), new RSTile(3094, 3491, 0),
  37. new RSTile(3097, 3496, 0), new RSTile(2946, 3369, 0), new RSTile(3012, 3356, 0)};
  38. for (RSTile bank : BANKS) {
  39. double cdist = methods.calc.distanceBetween(tile, bank);
  40. if ((dist > cdist || dist == -1.0D) && (tile.getZ() == bank.getZ())) {
  41. dist = cdist;
  42. finalTile = bank;
  43. }
  44. }
  45. return finalTile;
  46. }
  47.  
  48. /**
  49. * Gets the closest supported bank in runescape that is usable.
  50. *
  51. * @return The closest bank's tile.
  52. */
  53. public RSTile getNearestBank() {
  54. return getNearestBank(methods.players.getMyPlayer().getLocation());
  55. }
  56.  
  57. /**
  58. * Generates a path between two nodes.
  59. *
  60. * @param start The starting tile.
  61. * @param end The ending tile.
  62. * @return The path.
  63. */
  64. public RSTile[] generateNodePath(final RSTile start, final RSTile end) {
  65. if (start.getZ() != end.getZ()) {
  66. return null;
  67. }
  68. if (start.equals(end)) {
  69. return new RSTile[]{};
  70. }
  71. final HashSet<Node> open = new HashSet<Node>();
  72. final HashSet<Node> closed = new HashSet<Node>();
  73. Node curr = new Node(start.getX(), start.getY(), start.getZ());
  74. final Node dest = new Node(end.getX(), end.getY(), end.getZ());
  75. curr.f = hCost(curr, dest);
  76. open.add(curr);
  77. while (!open.isEmpty()) {
  78. curr = Lowest_f(open);
  79. if (curr.equals(dest)) {
  80. return Path(curr);
  81. }
  82. open.remove(curr);
  83. closed.add(curr);
  84. for (final Node next : Web.Successors(curr)) {
  85. if (!closed.contains(next)) {
  86. final double t = curr.g + gCost(curr, next);
  87. boolean use_t = false;
  88. if (!open.contains(next)) {
  89. open.add(next);
  90. use_t = true;
  91. } else if (t < next.g) {
  92. use_t = true;
  93. }
  94. if (use_t) {
  95. next.prev = curr;
  96. next.g = t;
  97. next.f = t + hCost(next, dest);
  98. }
  99. }
  100. }
  101. }
  102. return null;
  103. }
  104.  
  105. /**
  106. * Generates a route between two tiles.
  107. *
  108. * @param start The start tile.
  109. * @param end The ending tile.
  110. * @return The generated route.
  111. */
  112. public Route generateRoute(RSTile start, final RSTile end) {
  113. TransportationHandler transportationHandler = new TransportationHandler(methods);
  114. List<RouteStep> routeSteps = new ArrayList<RouteStep>();
  115. if (transportationHandler.canTeleport(end)) {
  116. Teleport teleport = transportationHandler.getTeleport(end);
  117. if (teleport.teleportationLocation().getZ() == end.getZ()) {
  118. RouteStep teleportStep = new RouteStep(methods, transportationHandler.getTeleport(end));
  119. start = teleport.teleportationLocation();
  120. routeSteps.add(teleportStep);
  121. }
  122. }
  123. RSTile[] nodePath = generateNodePath(start, end);
  124. if (nodePath != null) {
  125. RouteStep walkingStep = new RouteStep(methods, nodePath);
  126. routeSteps.add(walkingStep);
  127. return new Route(routeSteps.toArray(new RouteStep[routeSteps.size()]));
  128. }
  129. return null;
  130. }
  131.  
  132. /**
  133. * Returns a web instance to traverse.
  134. *
  135. * @param start The starting tile.
  136. * @param end The end tile.
  137. * @return The web constructed. <code>null</code> if it cannot be done.
  138. */
  139. public RSWeb getWeb(RSTile start, final RSTile end) {
  140. Route onlyRoute = generateRoute(start, end);
  141. return new RSWeb(new Route[]{onlyRoute});
  142. }
  143.  
  144. /**
  145. * Returns a web instance to traverse.
  146. *
  147. * @param end The end tile.
  148. * @return The web constructed. <code>null</code> if it cannot be done.
  149. */
  150. public RSWeb getWeb(final RSTile end) {
  151. return getWeb(methods.players.getMyPlayer().getLocation(), end);
  152. }
  153.  
  154. /**
  155. * Node class.
  156. *
  157. * @author Jacmob
  158. */
  159. private static class Node {
  160. public int x, y, z;
  161. public Node prev;
  162. public double g, f;
  163.  
  164. public Node(final int x, final int y, final int z) {
  165. this.x = x;
  166. this.y = y;
  167. this.z = z;
  168. g = f = 0;
  169. }
  170.  
  171. @Override
  172. public int hashCode() {
  173. return x << 4 | y;
  174. }
  175.  
  176. @Override
  177. public boolean equals(final Object o) {
  178. if (o instanceof Node) {
  179. final Node n = (Node) o;
  180. return x == n.x && y == n.y;
  181. }
  182. return false;
  183. }
  184.  
  185. @Override
  186. public String toString() {
  187. return "(" + x + "," + y + "," + z + ")";
  188. }
  189.  
  190. public RSTile toRSTile() {
  191. return new RSTile(x, y, z);
  192. }
  193. }
  194.  
  195. /**
  196. * Gets the heuristic distance.
  197. *
  198. * @param start Start node.
  199. * @param end End node.
  200. * @return The distance.
  201. */
  202. private static double hCost(final Node start, final Node end) {
  203. /*double dx = start.x - end.x;
  204. double dy = start.y - end.y;
  205. if (dx < 0) {
  206. dx = -dx;
  207. }
  208. if (dy < 0) {
  209. dy = -dy;
  210. }
  211. return dx < dy ? dy : dx;*/
  212. return (Math.abs(end.x - start.x) + Math.abs(end.y - start.x)) * 10;
  213. }
  214.  
  215. /**
  216. * The distance between two tiles.
  217. *
  218. * @param start The start tile.
  219. * @param end The end tile.
  220. * @return The distance.
  221. */
  222. private static double gCost(final Node start, final Node end) {
  223. /*if (start.x != end.x && start.y != end.y) {
  224. return 1.41421356;
  225. } else {
  226. return 1.0;
  227. }*/
  228. return (int) (Math.sqrt(Math.pow(start.x - end.x, 2) + Math.pow(start.y - end.y, 2)) * 10);
  229. }
  230.  
  231. /**
  232. * Gets the lowest f score of a set.
  233. *
  234. * @param open The set.
  235. * @return The node that has the lowest f score.
  236. */
  237. private static Node Lowest_f(final Set<Node> open) {
  238. Node best = null;
  239. for (final Node t : open) {
  240. if (best == null || t.f < best.f) {
  241. best = t;
  242. }
  243. }
  244. return best;
  245. }
  246.  
  247. /**
  248. * Constructs a path from a node.
  249. *
  250. * @param end The end node.
  251. * @return The constructed path.
  252. */
  253. private static RSTile[] Path(final Node end) {
  254. final LinkedList<RSTile> path = new LinkedList<RSTile>();
  255. Node p = end;
  256. while (p != null) {
  257. path.addFirst(p.toRSTile());
  258. p = p.prev;
  259. }
  260. return path.toArray(new RSTile[path.size()]);
  261. }
  262.  
  263. /**
  264. * Gets successors of a tile.
  265. *
  266. * @param t The node.
  267. * @return The nodes.
  268. */
  269. private static List<Node> Successors(final Node t) {
  270. final LinkedList<Node> tiles = new LinkedList<Node>();
  271. final int x = t.x, y = t.y;
  272. final RSTile here = t.toRSTile();
  273. if (!Flag(here, TileData.Key.W_S) &&
  274. !Flag(new RSTile(here.getX(), here.getY() - 1), TileData.Key.BLOCKED | TileData.Key.WATER)) {
  275. tiles.add(new Node(x, y - 1, t.toRSTile().getZ()));
  276. }
  277. if (!Flag(here, TileData.Key.W_W) &&
  278. !Flag(new RSTile(here.getX() - 1, here.getY()), TileData.Key.BLOCKED | TileData.Key.WATER)) {
  279. tiles.add(new Node(x - 1, y, t.toRSTile().getZ()));
  280. }
  281. if (!Flag(here, TileData.Key.W_N) &&
  282. !Flag(new RSTile(here.getX(), here.getY() + 1), TileData.Key.BLOCKED | TileData.Key.WATER)) {
  283. tiles.add(new Node(x, y + 1, t.toRSTile().getZ()));
  284. }
  285. if (!Flag(here, TileData.Key.W_E) &&
  286. !Flag(new RSTile(here.getX() + 1, here.getY()), TileData.Key.BLOCKED | TileData.Key.WATER)) {
  287. tiles.add(new Node(x + 1, y, t.toRSTile().getZ()));
  288. }
  289. if (!Flag(here, TileData.Key.W_SW | TileData.Key.W_S | TileData.Key.W_W) &&
  290. !Flag(new RSTile(here.getX() - 1, here.getY() - 1), TileData.Key.BLOCKED | TileData.Key.WATER) &&
  291. !Flag(new RSTile(here.getX(), here.getY() - 1), TileData.Key.BLOCKED | TileData.Key.WATER | TileData.Key.W_W) &&
  292. !Flag(new RSTile(here.getX() - 1, here.getY()), TileData.Key.BLOCKED | TileData.Key.WATER | TileData.Key.W_S)) {
  293. tiles.add(new Node(x - 1, y - 1, t.toRSTile().getZ()));
  294. }
  295. if (!Flag(here, TileData.Key.W_NW | TileData.Key.W_N | TileData.Key.W_W) &&
  296. !Flag(new RSTile(here.getX() - 1, here.getY() + 1), TileData.Key.BLOCKED | TileData.Key.WATER) &&
  297. !Flag(new RSTile(here.getX(), here.getY() + 1), TileData.Key.BLOCKED | TileData.Key.WATER | TileData.Key.W_W) &&
  298. !Flag(new RSTile(here.getX() - 1, here.getY()), TileData.Key.BLOCKED | TileData.Key.WATER | TileData.Key.W_N)) {
  299. tiles.add(new Node(x - 1, y + 1, t.toRSTile().getZ()));
  300. }
  301. if (!Flag(here, TileData.Key.W_SE | TileData.Key.W_S | TileData.Key.W_E) &&
  302. !Flag(new RSTile(here.getX() + 1, here.getY() - 1), TileData.Key.BLOCKED | TileData.Key.WATER) &&
  303. !Flag(new RSTile(here.getX(), here.getY() - 1), TileData.Key.BLOCKED | TileData.Key.WATER | TileData.Key.W_E) &&
  304. !Flag(new RSTile(here.getX() + 1, here.getY()), TileData.Key.BLOCKED | TileData.Key.WATER | TileData.Key.W_S)) {
  305. tiles.add(new Node(x + 1, y - 1, t.toRSTile().getZ()));
  306. }
  307. if (!Flag(here, TileData.Key.W_NE | TileData.Key.W_N | TileData.Key.W_E) &&
  308. !Flag(new RSTile(here.getX() + 1, here.getY() + 1), TileData.Key.BLOCKED | TileData.Key.WATER) &&
  309. !Flag(new RSTile(here.getX(), here.getY() + 1), TileData.Key.BLOCKED | TileData.Key.WATER | TileData.Key.W_E) &&
  310. !Flag(new RSTile(here.getX() + 1, here.getY()), TileData.Key.BLOCKED | TileData.Key.WATER | TileData.Key.W_N)) {
  311. tiles.add(new Node(x + 1, y + 1, t.toRSTile().getZ()));
  312. }
  313. return tiles;
  314. }
  315.  
  316. /**
  317. * Gets the TileFlags of a tile.
  318. *
  319. * @param tile The tile.
  320. * @return The <code>TileFlags</code>.
  321. */
  322. public static int GetTileFlag(final RSTile tile) {
  323. return Web.rs_map.get(tile);
  324. }
  325.  
  326. /**
  327. * Checks the flags of a tile.
  328. *
  329. * @param tile The tile to check.
  330. * @param key Keys to look for.
  331. * @return <tt>true</tt> if the tile contains flags.
  332. */
  333. public static boolean Flag(final RSTile tile, final int key) {
  334. if (Web.rs_map.containsKey(tile)) {
  335. final int theTile = Web.rs_map.get(tile);
  336. return (theTile & key) != 0;
  337. }
  338. return false;
  339. }
  340. }
Add Comment
Please, Sign In to add comment