Guest User

Untitled

a guest
Dec 11th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.13 KB | None | 0 0
  1. package org.dementhium.util;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.List;
  5.  
  6. import org.dementhium.model.Location;
  7.  
  8. /**
  9. * Represents an area.
  10. * @author Emperor
  11. *
  12. */
  13. public class Area {
  14.  
  15. /**
  16. * The south-western corner location.
  17. */
  18. private Location southWest;
  19.  
  20. /**
  21. * The north-eastern corner location.
  22. */
  23. private Location northEast;
  24.  
  25. /**
  26. * The centre point of the circle.
  27. */
  28. private Location centre;
  29.  
  30. /**
  31. * The radius of the circle.
  32. */
  33. private int radius;
  34.  
  35. /**
  36. * The area the triangle is in.
  37. */
  38. private Area triangleArea;
  39.  
  40. /**
  41. * The area type.
  42. */
  43. private AreaType type;
  44.  
  45. /**
  46. * The area type.
  47. *
  48. * @author Emperor
  49. *
  50. */
  51. public static enum AreaType {
  52. RECTANGLE, CIRCLE, TRIANGLE
  53. }
  54.  
  55. /**
  56. * Constructs an area.
  57. *
  58. * @param areaType
  59. * The type of the area.
  60. * @param firstLocation
  61. * The first location: <br>
  62. * <i>Represents the south-western location on a rectangle/square<br>
  63. * The center location on a circle</i>
  64. * @param secondLocation
  65. * The second location: <br>
  66. * <i>Represents the south-western location on a rectangle/square<br>
  67. * The center location on a circle</i>
  68. * @see AreaType
  69. */
  70. public Area(AreaType areaType, Location firstLocation,
  71. Location secondLocation, Location thirdLocation) {
  72. this.type = areaType;
  73. if (areaType == AreaType.RECTANGLE) {
  74. this.southWest = firstLocation;
  75. this.northEast = secondLocation;
  76. } else if (areaType == AreaType.CIRCLE){
  77. int radiusX = secondLocation.getX() - firstLocation.getX();
  78. int radiusY = secondLocation.getY() - firstLocation.getY();
  79. if (radiusX < 0) {
  80. radiusX *= -1;
  81. }
  82. if (radiusY < 0) {
  83. radiusY *= -1;
  84. }
  85. if (radiusX > radiusY) {
  86. this.radius = radiusX;
  87. } else {
  88. this.radius = radiusY;
  89. }
  90. this.centre = firstLocation;
  91. } else if (areaType == AreaType.TRIANGLE) {
  92. if (triangleArea == null) { //TODO remove this, it's useless.
  93. this.centre = firstLocation;
  94. this.southWest = secondLocation;
  95. this.northEast = thirdLocation;
  96. }
  97. }
  98. }
  99.  
  100. /**
  101. * Creates a new rectangle-formed area.
  102. *
  103. * @param southWest
  104. * The south western corner.
  105. * @param northEast
  106. * The north eastern corner.
  107. * @return The area instance.
  108. */
  109. public static Area rectangle(Location southWest, Location northEast) {
  110. return new Area(AreaType.RECTANGLE, southWest, northEast, null);
  111. }
  112.  
  113. /**
  114. * Creates a circle-formed area.
  115. *
  116. * @param centre
  117. * The centre point.
  118. * @param otherLocation
  119. * The other location.
  120. * @return The area instance.
  121. */
  122. public static Area circle(Location centre, Location otherLocation) {
  123. return new Area(AreaType.CIRCLE, centre, otherLocation, null);
  124. }
  125.  
  126. /**
  127. * Creates a circle-formed area.
  128. *
  129. * @param centre
  130. * The centre.
  131. * @param radius
  132. * The radius.
  133. * @return The area instance.
  134. */
  135. public static Area circle(Location centre, int radius) {
  136. return new Area(AreaType.CIRCLE, centre, centre.transform(radius, 0, 0), null);
  137. }
  138.  
  139. /**
  140. * Creates a triangular area.
  141. * @param centre The centre point.
  142. * @param first The first location.
  143. * @param second The second location.
  144. * @return The constructed area.
  145. */
  146. public static Area triangle(Location centre, Location first, Location second) {
  147. return new Area(AreaType.TRIANGLE, centre, first, second);
  148. }
  149.  
  150. /**
  151. * Gets all the tiles in this area.
  152. *
  153. * @return The array of tiles.
  154. */
  155. public List<Location> getTiles() {
  156. if (type == AreaType.RECTANGLE) {
  157. List<Location> tiles = new LinkedList<Location>();
  158. for (int x = southWest.getX(); x < northEast.getX() + 1; x++) {
  159. for (int y = southWest.getY(); y < northEast.getY() + 1; y++) {
  160. tiles.add(Location.locate(x, y, 0));
  161. }
  162. }
  163. return tiles;
  164. }
  165. List<Location> tiles = new LinkedList<Location>();
  166. short westX = (short) (centre.getX() - radius);
  167. short southY = (short) (centre.getY() - radius);
  168. short eastX = (short) (centre.getX() + radius);
  169. short northY = (short) (centre.getY() + radius);
  170. for (short x = westX; x < eastX + 1; x++) {
  171. for (short y = southY; y < northY + 1; y++) {
  172. Location loc = Location.locate(x, y, 0);
  173. if (centre.withinDistance(loc, radius)) {
  174. tiles.add(loc);
  175. }
  176. }
  177. }
  178. return tiles;
  179. }
  180.  
  181. public List<Location> getWalkableTiles() {
  182. List<Location> tiles = new LinkedList<Location>();
  183. List<Location> area = getTiles();
  184. //byte[][] flags = ClippingFlags.getClippingFlags(southWest);
  185. for (Location l : area) {
  186. if (l == null) {
  187. continue;
  188. }
  189. /*byte f = flags[l.getLocalX()][l.getLocalY()];
  190. if ((f & 0x12) == 0 && (f & 0x11) == 0 && (f & 0x14) == 0
  191. && (f & 0x18) == 0 && (f & 0x16) == 0 && (f & 0x15) == 0
  192. && (f & 0x16) == 0 && (f & 0x1A) == 0 && (f & 0x19) == 0) {*/
  193. tiles.add(l);
  194. //}
  195. }
  196. return tiles;
  197. }
  198.  
  199. /**
  200. * Checks if the given location is in the constructed area.
  201. *
  202. * @param location
  203. * The location.
  204. * @return {@code True} if so, {@code false} if not.
  205. */
  206. public boolean isInArea(Location location) {
  207. if (location == null) {
  208. return false;
  209. }
  210. if (type == AreaType.RECTANGLE) {
  211. return location.getX() >= southWest.getX()
  212. && location.getY() >= southWest.getY()
  213. && location.getX() <= northEast.getX()
  214. && location.getY() <= northEast.getY();
  215. }
  216. return centre.withinDistance(location, radius);
  217. }
  218.  
  219. /**
  220. * Checks if the given area is in the constructed area.
  221. *
  222. * @param location
  223. * The other area.
  224. * @return {@code True} if so, {@code false} if not.
  225. */
  226. public boolean isInArea(Area area) {
  227. for (Location l : getTiles()) {
  228. for (Location al : area.getTiles()) {
  229. if (l.getX() == al.getX() && l.getY() == al.getY() && l.getZ() == al.getZ()) {
  230. return true;
  231. }
  232. }
  233. }
  234. return false;
  235. }
  236.  
  237. /**
  238. * Checks if the location given is within distance of the area.
  239. * @param location The location given.
  240. * @param minimumDistance The minimum distance.
  241. * @return {@code True} if the location is closer than (or has the same) minimum distance, {@code false} if the distance is larger.
  242. */
  243. public boolean isInRange(Location location, int minimumDistance) {
  244. for (Location l : getTiles()) {
  245. if (l.getDistance(location) <= minimumDistance) {
  246. return true;
  247. }
  248. }
  249. return false;
  250. }
  251.  
  252. public boolean isInRange(Area area, int minimumDistance) {
  253. for (Location l : getTiles()) {
  254. for (Location al : area.getTiles()) {
  255. if (l.getDistance(al) <= minimumDistance) {
  256. return true;
  257. }
  258. }
  259. }
  260. return true;
  261. }
  262.  
  263. public int getClosestDistance(Location l) {
  264. int lastDistance = Integer.MAX_VALUE;
  265. for (Location loc : getTiles()) {
  266. int distance = loc.getDistance(l);
  267. if (distance <= lastDistance) {
  268. lastDistance = distance;
  269. }
  270. }
  271. return lastDistance;
  272. }
  273.  
  274. public Location getClosest(Location l) {
  275. if (isInArea(l)) {
  276. return getClosestExternLocation(l);
  277. }
  278. return getClosestLocation(l);
  279. }
  280.  
  281. public Location getClosestLocation(Location l) {
  282. Location lastLocation = southWest;
  283. int lastDistance = Integer.MAX_VALUE;
  284. List<Location> tiles = getTiles();
  285. for (Location loc : tiles) {
  286. int distance = loc.getDistance(l);
  287. if (distance <= lastDistance) {
  288. lastDistance = distance;
  289. lastLocation = loc;
  290. }
  291. }
  292. return lastLocation;
  293. }
  294.  
  295. public Location getClosestExternLocation(Location l) {
  296. Location lastLocation = southWest;
  297. int lastDistance = Integer.MAX_VALUE;
  298. List<Location> tiles = getExternTiles();
  299. //byte[][] flags = ClippingFlags.getClippingFlags(Location.locate(southWest.getX() - 1, southWest.getY() - 1, southWest.getZ(), southWest.getStaticLocation()));
  300. for (Location loc : tiles) {
  301. /*byte f = flags[loc.getLocalX()][loc.getLocalY()];
  302. if (!((f & 0x12) == 0 && (f & 0x11) == 0 && (f & 0x14) == 0
  303. && (f & 0x18) == 0 && (f & 0x16) == 0 && (f & 0x15) == 0
  304. && (f & 0x16) == 0 && (f & 0x1A) == 0 && (f & 0x19) == 0)) {
  305. continue;
  306. }*/
  307. if (lastLocation == southWest) {
  308. lastLocation = loc;
  309. }
  310. int distance = loc.getDistance(l);
  311. if (distance <= lastDistance) {
  312. lastDistance = distance;
  313. lastLocation = loc;
  314. }
  315. }
  316. return lastLocation;
  317. }
  318.  
  319. /**
  320. * Gets all the tiles in this area.
  321. *
  322. * @return The array of tiles.
  323. */
  324. public List<Location> getExternTiles() {
  325. List<Location> tiles = new LinkedList<Location>();
  326. for (int x = southWest.getX(); x < northEast.getX() + 1; x++) {
  327. tiles.add(Location.locate(x, southWest.getY() - 1, 0));
  328. tiles.add(Location.locate(x, northEast.getY() + 1, 0));
  329. }
  330. for (int y = southWest.getY(); y < northEast.getY() + 1; y++) {
  331. tiles.add(Location.locate(southWest.getX() - 1, y, 0));
  332. tiles.add(Location.locate(northEast.getX() + 1, y, 0));
  333. }
  334. return tiles;
  335. }
  336.  
  337. /**
  338. * @param southWest
  339. * the southWest to set
  340. */
  341. public void setSouthWest(Location southWest) {
  342. this.southWest = southWest;
  343. }
  344.  
  345. /**
  346. * @return the southWest
  347. */
  348. public Location getSouthWest() {
  349. return southWest;
  350. }
  351.  
  352. /**
  353. * @param northEast
  354. * the northEast to set
  355. */
  356. public void setNorthEast(Location northEast) {
  357. this.northEast = northEast;
  358. }
  359.  
  360. /**
  361. * @return the northEast
  362. */
  363. public Location getNorthEast() {
  364. return northEast;
  365. }
  366.  
  367. /**
  368. * @param centre
  369. * the centre to set
  370. */
  371. public void setCentre(Location centre) {
  372. this.centre = centre;
  373. }
  374.  
  375. /**
  376. * @return the centre
  377. */
  378. public Location getCentre() {
  379. return centre;
  380. }
  381.  
  382. /**
  383. * @param type
  384. * the type to set
  385. */
  386. public void setType(AreaType type) {
  387. this.type = type;
  388. }
  389.  
  390. /**
  391. * @return the type
  392. */
  393. public AreaType getType() {
  394. return type;
  395. }
  396.  
  397. /**
  398. * Sets the radius.
  399. *
  400. * @param radius
  401. * The radius.
  402. */
  403. public void setRadius(int radius) {
  404. this.radius = radius;
  405. }
  406.  
  407. /**
  408. * @return the radius
  409. */
  410. public int getRadius() {
  411. return radius;
  412. }
  413. }
Add Comment
Please, Sign In to add comment