Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. package org.rsbot.script.wrappers;
  2. import org.rsbot.bot.Bot;
  3. import java.awt.*;
  4. import java.util.ArrayList;
  5.  
  6. /**
  7. * Represents a shape made of Tiles.
  8. *
  9. * @author SpeedWing, Emeleo, Parnassian
  10. */
  11. public class Area {
  12. private final Polygon area;
  13. private final int plane;
  14. private final Tile[] tiles;
  15.  
  16. /**
  17. * @param tiles An Array containing of <b>Tiles</b> forming a polygon shape.
  18. * @param plane The plane of the <b>Area</b>.
  19. */
  20. public Area(final Tile[] tiles, final int plane) {
  21. this.tiles = tiles;
  22. area = tileArrayToPolygon(this.tiles);
  23. this.plane = plane;
  24. }
  25.  
  26. /**
  27. * @param tiles An Array containing of <b>Tiles</b> forming a polygon shape.
  28. */
  29. public Area(final Tile[] tiles) {
  30. this(tiles, 0);
  31. }
  32.  
  33. /**
  34. * @param sw The <i>South West</i> <b>Tile</b> of the <b>Area</b>
  35. * @param ne The <i>North East</i> <b>Tile</b> of the <b>Area</b>
  36. * @param plane The plane of the <b>Area</b>.
  37. */
  38. public Area(final Tile sw, final Tile ne, final int plane) {
  39. this(new Tile[]{sw, new Tile(ne.getX() + 1, sw.getY()),
  40. new Tile(ne.getX() + 1, ne.getY() + 1),
  41. new Tile(sw.getX(), ne.getY() + 1)}, plane);
  42. }
  43.  
  44. /**
  45. * @param sw The <i>South West</i> <b>Tile</b> of the <b>Area</b>
  46. * @param ne The <i>North East</i> <b>Tile</b> of the <b>Area</b>
  47. */
  48. public Area(final Tile sw, final Tile ne) {
  49. this(sw, ne, 0);
  50. }
  51.  
  52. /**
  53. * @param swX The X axle of the <i>South West</i> <b>Tile</b> of the
  54. * <b>Area</b>
  55. * @param swY The Y axle of the <i>South West</i> <b>Tile</b> of the
  56. * <b>Area</b>
  57. * @param neX The X axle of the <i>North East</i> <b>Tile</b> of the
  58. * <b>Area</b>
  59. * @param neY The Y axle of the <i>North East</i> <b>Tile</b> of the
  60. * <b>Area</b>
  61. */
  62. public Area(final int swX, final int swY, final int neX, final int neY) {
  63. this(new Tile(swX, swY), new Tile(neX, neY), 0);
  64. }
  65.  
  66. /**
  67. * @param x The x location of the <b>Tile</b> that will be checked.
  68. * @param y The y location of the <b>Tile</b> that will be checked.
  69. * @return True if the <b>Area</b> contains the given <b>Tile</b>.
  70. */
  71. public boolean contains(final int x, final int y) {
  72. return this.contains(new Tile(x, y));
  73. }
  74.  
  75. /**
  76. * @param plane The plane to check.
  77. * @param tiles The <b>Tile(s)</b> that will be checked.
  78. * @return True if the <b>Area</b> contains the given <b>Tile(s)</b>.
  79. */
  80. public boolean contains(final int plane, final Tile... tiles) {
  81. return this.plane == plane && this.contains(tiles);
  82. }
  83.  
  84. /**
  85. * @param tiles The <b>Tile(s)</b> that will be checked.
  86. * @return True if the <b>Area</b> contains the given <b>Tile(s)</b>.
  87. */
  88. public boolean contains(final Tile... tiles) {
  89. final Tile[] areaTiles = getTileArray();
  90. for (final Tile check : tiles) {
  91. for (final Tile space : areaTiles) {
  92. if (check.equals(space)) {
  93. return true;
  94. }
  95. }
  96. }
  97. return false;
  98. }
  99.  
  100. /**
  101. * @return The bounding box of the <b>Area</b>.
  102. */
  103. public Rectangle getBounds() {
  104. return new Rectangle(area.getBounds().x + 1,
  105. area.getBounds().y + 1, getWidth(), getHeight());
  106. }
  107.  
  108. /**
  109. * @return The central <b>Tile</b> of the <b>Area</b>.
  110. */
  111. public Tile getCentralTile() {
  112. if (area.npoints < 1) {
  113. return null;
  114. }
  115. int totalX = 0, totalY = 0;
  116. for (int i = 0; i < area.npoints; i++) {
  117. totalX += area.xpoints[i];
  118. totalY += area.ypoints[i];
  119. }
  120. return new Tile(Math.round(totalX / area.npoints),
  121. Math.round(totalY / area.npoints));
  122. }
  123.  
  124. /**
  125. * @param base The base tile to measure the closest tile off of.
  126. * @return The nearest <b>Tile</b> in the <b>Area</b>
  127. * to the given <b>Tile</b>.
  128. */
  129. public Tile getNearestTile(final Tile base) {
  130. final Tile[] tiles = getTileArray();
  131. Tile cur = null;
  132. double dist = -1;
  133. for (final Tile tile : tiles) {
  134. final double distTmp = distanceBetween(tile, base);
  135. if (cur == null) {
  136. dist = distTmp;
  137. cur = tile;
  138. } else if (distTmp < dist) {
  139. cur = tile;
  140. dist = distTmp;
  141. }
  142. }
  143. return cur;
  144. }
  145.  
  146. /**
  147. * @return The plane of the <b>Area</b>.
  148. */
  149. public int getPlane() {
  150. return plane;
  151. }
  152.  
  153. /**
  154. * @return The <b>Tiles</b> the <b>Area</b> contains.
  155. */
  156. public Tile[] getTileArray() {
  157. final ArrayList<Tile> list = new ArrayList<Tile>();
  158. for (int x = getX(); x <= getX() + getWidth(); x++) {
  159. for (int y = getY(); y <= getY() + getHeight(); y++) {
  160. if (area.contains(x, y)) {
  161. list.add(new Tile(x, y, plane));
  162. }
  163. }
  164. }
  165. final Tile[] tiles = new Tile[list.size()];
  166. for (int i = 0; i < list.size(); i++) {
  167. tiles[i] = list.get(i);
  168. }
  169. return tiles;
  170. }
  171.  
  172. /**
  173. * @return The <b>Tiles</b> the <b>Area</b> contains.
  174. */
  175. public Tile[][] getTiles() {
  176. final Tile[][] tiles = new Tile[getWidth()][getHeight()];
  177. for (int i = 0; i < getWidth(); ++i) {
  178. for (int j = 0; j < getHeight(); ++j) {
  179. if (area.contains(getX() + i, getY() + j)) {
  180. tiles[i][j] = new Tile(getX() + i, getY() + j);
  181. }
  182. }
  183. }
  184. return tiles;
  185. }
  186.  
  187. /**
  188. * @return The distance between the the <b>Tile</b> that's most
  189. * <i>East</i> and the <b>Tile</b> that's most <i>West</i>.
  190. */
  191. public int getWidth() {
  192. return area.getBounds().width;
  193. }
  194.  
  195. /**
  196. * @return The distance between the the <b>Tile</b> that's most
  197. * <i>South</i> and the <b>Tile</b> that's most <i>North</i>.
  198. */
  199. public int getHeight() {
  200. return area.getBounds().height;
  201. }
  202.  
  203. /**
  204. * @return The X axle of the <b>Tile</b> that's most <i>West</i>.
  205. */
  206. public int getX() {
  207. return area.getBounds().x;
  208. }
  209.  
  210. /**
  211. * @return The Y axle of the <b>Tile</b> that's most <i>South</i>.
  212. */
  213. public int getY() {
  214. return area.getBounds().y;
  215. }
  216.  
  217. /**
  218. * @return An array of <b>Tile<b>'s that make the area.
  219. */
  220. public Tile[] getAreaTiles() {
  221. return this.tiles;
  222. }
  223.  
  224. /**
  225. * Converts an shape made of <b>Tile</b> to a polygon.
  226. *
  227. * @param tiles The <b>Tile</b> of the Polygon.
  228. * @return The Polygon of the <b>Tile</b>.
  229. */
  230. private Polygon tileArrayToPolygon(final Tile[] tiles) {
  231. final Polygon poly = new Polygon();
  232. for (final Tile t : tiles) {
  233. poly.addPoint(t.getX(), t.getY());
  234. }
  235. return poly;
  236. }
  237.  
  238. /**
  239. * @param curr first <b>Tile</b>
  240. * @param dest second <b>Tile</b>
  241. * @return the distance between the first and the second Tile
  242. */
  243. private double distanceBetween(final Tile curr, final Tile dest) {
  244. return Math.sqrt((curr.getX() - dest.getX())
  245. * (curr.getX() - dest.getX()) + (curr.getY() - dest.getY())
  246. * (curr.getY() - dest.getY()));
  247. }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement