RuneHive-Game
Loading...
Searching...
No Matches
Position.java
Go to the documentation of this file.
1package com.runehive.game.world.position;
2
3import com.runehive.game.world.Interactable;
4import com.runehive.game.world.World;
5import com.runehive.game.world.entity.mob.Mob;
6import com.runehive.game.world.region.Region;
7import com.runehive.util.Utility;
8
9/**
10 * Represents a single tile on the game world.
11 *
12 * @author Graham Edgecombe
13 */
14public class Position {
15
16 /** The maximum amount of height-planes. */
17 public static final int HEIGHT_LEVELS = 4;
18
19 /** The x coordinate. */
20 private final int x;
21
22 /** The y coordinate. */
23 private final int y;
24
25 /** The height coordinate. */
26 private final int height;
27
28 /** Creates a location with a default height of 0. */
29 public Position(int x, int y) {
30 this(x, y, 0);
31 }
32
33 /** Creates a location. */
34 public Position(int x, int y, int height) {
35 this.x = x;
36 this.y = y;
37 this.height = height < 0 ? 0 : height % 4;
38 }
39
40 /** Gets the absolute x coordinate. */
41 public int getX() {
42 return x;
43 }
44
45 /** Gets the absolute y coordinate. */
46 public int getY() {
47 return y;
48 }
49
50 /** Gets the height coordinate, or height. */
51 public int getHeight() {
52 return height;
53 }
54
55 /** Gets the local x coordinate relative to this region. */
56 public int getLocalX() {
57 return getLocalX(this);
58 }
59
60 /** Gets the local y coordinate relative to this region. */
61 public int getLocalY() {
62 return getLocalY(this);
63 }
64
65 /** Gets the local x coordinate relative to a specific region. */
66 public int getLocalX(Position origin) {
67 return x - 8 * origin.getChunkX();
68 }
69
70 /** Gets the local y coordinate relative to a specific region. */
71 public int getLocalY(Position origin) {
72 return y - 8 * origin.getChunkY();
73 }
74
75 /** Gets the chunk x coordinate. */
76 public int getChunkX() {
77 return (x >> 3) - 6;
78 }
79
80 /** Gets the chunk y coordinate. */
81 public int getChunkY() {
82 return (y >> 3) - 6;
83 }
84
85 public int getRegionX() {
86 return getChunkX() / 8;
87 }
88
89 public int getRegionY() {
90 return getChunkY() / 8;
91 }
92
93 public Region getRegion() {
94 return World.getRegions().getRegion(this);
95 }
96
97 public Position north() {
98 return transform(0, 1);
99 }
100
101 public Position east() {
102 return transform(1, 0);
103 }
104
105 public Position south() {
106 return transform(0, -1);
107 }
108
109 public Position west() {
110 return transform(-1, 0);
111 }
112
114 return transform(1, 1);
115 }
116
118 return transform(-1, 1);
119 }
120
122 return transform(1, -1);
123 }
124
126 return transform(-1, -1);
127 }
128
129 /**
130 * Gets the manhattan distance between two interactable object.
131 *
132 * @param origin The originating location.
133 * @param target The target location.
134 * @return The distance between the origin and target.
135 */
136 public static int getManhattanDistance(Interactable origin, Interactable target) {
137 return Utility.getDistance(origin, target);
138 }
139
140 /**
141 * Checks if this location is within range of another.
142 *
143 * @param other The other location.
144 * @param radius The radius from the origin point.
145 * @return {@code True} if the location is within the radius.
146 */
147 public boolean isWithinDistance(Position other, int radius) {
148 if (height != other.height) {
149 return false;
150 }
151
152 final int deltaX = Math.abs(other.x - x);
153 final int deltaY = Math.abs(other.y - y);
154 return deltaX <= radius && deltaY <= radius;
155 }
156
157 /**
158 * Gets the distance between this location and another location.
159 *
160 * @param other The other location.
161 * @return The distance between the two locations.
162 */
163 public double getDistance(Position other) {
164 if (height != other.height) return 0;
165 int dx = other.x - x;
166 int dy = other.y - y;
167 return Math.sqrt(dx * dx + dy * dy);
168 }
169
170 /**
171 * Absolute distance between this Coordiante and another.
172 *
173 * @param other The other Coordiante.
174 * @return The distance between the 2 Coordinates.
175 */
176 public int getDistances(Position other) {
177 return (int) Math.sqrt(Math.pow(x - other.x, 2) + Math.pow(y - other.y, 2) + Math.pow(height - other.height, 2));
178 }
179
180 public int getChevDistance(Position other) {
181 return Math.max(Math.abs(other.getX() - getX()), Math.abs(other.getY() - getY()));
182 }
183
184 /**
185 * Gets the Euclidean (straight-line) distance between two {@link Position}
186 * s.
187 *
188 * @return The distance in tiles between the two locations.
189 */
190 public static double getDistance(Position first, Position second) {
191 final int dx = second.getX() - first.getX();
192 final int dy = second.getY() - first.getY();
193 return Math.sqrt(dx * dx + dy * dy);
194 }
195
196 /**
197 * Gets the distance between this location and another location without
198 * diagonals.
199 *
200 * @param other The other location.
201 * @return The distance between the two locations.
202 */
203 public int getManhattanDistance(Position other) { //dont use this, it calcs wrong btw
204 if (other == null || height != other.height) return Integer.MAX_VALUE;
205 int dx = Math.abs(other.x - x);
206 int dy = Math.abs(other.y - y);
207 return dx + dy;
208 }
209
210 public boolean isViewableFrom(Position other) {
211 if (this.getHeight() != other.getHeight())
212 return false;
213 Position p = this.getDelta(this, other);
214 return p.x <= 14 && p.x >= -15 && p.y <= 14 &&
215 p.y >= -15;
216 }
217
218 public Position getDelta(Position location, Position other) {
219 return new Position(other.x - location.x, other.y - location.y, other.getHeight() - location.getHeight());
220 }
221
222 /**
223 * Gets the Euclidean (straight-line) distance between two {@link Position}
224 * s.
225 *
226 * @return The distance in tiles between the two locations.
227 */
228 public static int getManhattanDistance(Position first, Position second) {
229 final int dx = Math.abs(second.getX() - first.getX());
230 final int dy = Math.abs(second.getY() - first.getY());
231 return dx + dy;
232 }
233
234 /**
235 * Gets the longest horizontal or vertical delta between the two positions.
236 *
237 * @param other The other position.
238 * @return The longest horizontal or vertical delta.
239 */
240 public int getLongestDelta(Position other) {
241 if (height != other.height) return 0;
242 int deltaX = Math.abs(getX() - other.getX());
243 int deltaY = Math.abs(getY() - other.getY());
244 return Math.max(deltaX, deltaY);
245 }
246
247 /**
248 * Creates a location.
249 *
250 * @param x The x coordinate.
251 * @param y The y coordinate.
252 * @param z The height coordinate.
253 * @return The location.
254 */
255 public static Position create(int x, int y, int z) {
256 return new Position(x, y, z);
257 }
258
259 /**
260 * Creates a location.
261 *
262 * @param x The x coordinate.
263 * @param y The y coordinate.
264 * @return The location.
265 */
266 public static Position create(int x, int y) {
267 return new Position(x, y);
268 }
269
270 /**
271 * Creates a new location based on this location.
272 *
273 * @param diffX X difference.
274 * @param diffY Y difference.
275 * @param diffZ Z difference.
276 * @return The new location.
277 */
278 public Position transform(int diffX, int diffY, int diffZ) {
279 return new Position(x + diffX, y + diffY, height + diffZ);
280 }
281
282 /**
283 * Creates a new location based on this location.
284 *
285 * @param diffX X difference.
286 * @param diffY Y difference.
287 * @return The new location.
288 */
289 public Position transform(int diffX, int diffY) {
290 return new Position(x + diffX, y + diffY, height);
291 }
292
293
294 public boolean inLocation(Position southWest, Position northEast, boolean inclusive) {
295 return !inclusive ? this.x > southWest.getX() && this.x < northEast.getX() && this.y > southWest.getY() && this.y < northEast.getY() : this.x >= southWest.getX() && this.x <= northEast.getX() && this.y >= southWest.getY() && this.y <= northEast.getY();
296 }
297
298 /**
299 * Creates a new location based on this location.
300 *
301 * @param other The difference.
302 * @return The new location.
303 */
305 if (other == null) return this;
306 return transform(other.getX(), other.getY(), other.getHeight());
307 }
308
309 /**
310 * Creates a deep copy of this location.
311 *
312 * @return A deep copy of this location.
313 */
314 public Position copy() {
315 return new Position(x, y, height);
316 }
317
318 @Override
319 public boolean equals(Object obj) {
320 if (obj == this) return true;
321
322 if (obj instanceof Position) {
323 Position other = (Position) obj;
324 return x == other.x && y == other.y && height == other.height;
325 }
326
327 return false;
328 }
329
330 public boolean matches(int x, int y) {
331 return this.x == x && this.y == y;
332 }
333
334 public boolean matches(int x, int y, int z) {
335 return this.x == x && this.y == y && this.height == z;
336 }
337
338 @Override
339 public int hashCode() {
340 return hash(x, y, height);
341 }
342
343 @Override
344 public String toString() {
345 return String.format("pos[x=%d, y=%d, z=%d]", x, y, height);
346 }
347
348 public static int hash(int x, int y, int z) {
349 return (y << 16) | (x << 8) | z;
350 }
351
352 public static boolean isWithinDiagonalDistance(Mob attacker, Mob defender, int distance) {
353 int attackerSize = 1;
354 int defenderSize = 1;
355
356 if(attacker.isNpc())
357 attackerSize = attacker.getNpc().definition.getSize();
358 if(defender.isNpc())
359 defenderSize = defender.getNpc().definition.getSize();
360
361 int e_offset_x = attackerSize - 1 + distance;
362 int e_offset_y = attackerSize - 1 + distance;
363
364 int o_offset_x = defenderSize - 1 + distance;
365 int o_offset_y = defenderSize - 1 + distance;
366
367 Position entity_pos = attacker.getPosition().copy();
368 Position other_pos = defender.getPosition().copy();
369
370 boolean inside_entity =
371 (other_pos.getX() <= entity_pos.getX() + e_offset_x && other_pos.getX() >= (entity_pos.getX() - distance)) &&
372 (other_pos.getY() <= entity_pos.getY() + e_offset_y && other_pos.getY() >= (entity_pos.getY() - distance));
373
374 boolean inside_other =
375 (entity_pos.getX() <= other_pos.getX() + o_offset_x && entity_pos.getX() >= (other_pos.getX() - distance)) &&
376 (entity_pos.getY() <= other_pos.getY() + o_offset_y && entity_pos.getY() >= (other_pos.getY() - distance));
377
378
379 return inside_entity || inside_other;
380 }
381
382 public int getChebyshevDistance(Position other) {
383 return getChebyshevDistance(getX(), getY(), other.getX(), other.getY());
384 }
385
386 public int getChebyshevDistance(int x1, int y1, int x2, int y2) {
387 return Math.max(Math.abs(x2 - x1), Math.abs(y2 - y1));
388 }
389
390 public int getCoordFaceX(final int sizeX) {
391 return getCoordFaceX(sizeX, -1, -1);
392 }
393
394 public int getCoordFaceX(final int sizeX, final int sizeY, final int rotation) {
395 return getX() + ((rotation == 1 || rotation == 3 ? sizeY : sizeX) - 1) / 2;
396 }
397
398 public int getCoordFaceY(final int sizeY) {
399 return getCoordFaceY(-1, sizeY, -1);
400 }
401
402 public int getCoordFaceY(final int sizeX, final int sizeY, final int rotation) {
403 return getY() + ((rotation == 1 || rotation == 3 ? sizeX : sizeY) - 1) / 2;
404 }
405
406}
Represents the game world.
Definition World.java:46
static RegionManager getRegions()
Definition World.java:552
Handles the mob class.
Definition Mob.java:66
final boolean isNpc()
Check if an entity is an npc.
Definition Mob.java:550
Position transform(Position other)
Creates a new location based on this location.
Position(int x, int y, int height)
Creates a location.
Definition Position.java:34
int getLongestDelta(Position other)
Gets the longest horizontal or vertical delta between the two positions.
static int getManhattanDistance(Interactable origin, Interactable target)
Gets the manhattan distance between two interactable object.
Position getDelta(Position location, Position other)
boolean inLocation(Position southWest, Position northEast, boolean inclusive)
boolean isWithinDistance(Position other, int radius)
Checks if this location is within range of another.
final int height
The height coordinate.
Definition Position.java:26
static boolean isWithinDiagonalDistance(Mob attacker, Mob defender, int distance)
int getHeight()
Gets the height coordinate, or height.
Definition Position.java:51
int getLocalX(Position origin)
Gets the local x coordinate relative to a specific region.
Definition Position.java:66
Position transform(int diffX, int diffY)
Creates a new location based on this location.
int getY()
Gets the absolute y coordinate.
Definition Position.java:46
static int hash(int x, int y, int z)
boolean isViewableFrom(Position other)
int getLocalY(Position origin)
Gets the local y coordinate relative to a specific region.
Definition Position.java:71
Position(int x, int y)
Creates a location with a default height of 0.
Definition Position.java:29
int getX()
Gets the absolute x coordinate.
Definition Position.java:41
int getDistances(Position other)
Absolute distance between this Coordiante and another.
Position transform(int diffX, int diffY, int diffZ)
Creates a new location based on this location.
static final int HEIGHT_LEVELS
The maximum amount of height-planes.
Definition Position.java:17
int getManhattanDistance(Position other)
Gets the distance between this location and another location without diagonals.
int getCoordFaceX(final int sizeX, final int sizeY, final int rotation)
int getLocalY()
Gets the local y coordinate relative to this region.
Definition Position.java:61
static int getManhattanDistance(Position first, Position second)
Gets the Euclidean (straight-line) distance between two Position s.
static Position create(int x, int y, int z)
Creates a location.
int getChunkX()
Gets the chunk x coordinate.
Definition Position.java:76
int getLocalX()
Gets the local x coordinate relative to this region.
Definition Position.java:56
int getChebyshevDistance(int x1, int y1, int x2, int y2)
int getCoordFaceY(final int sizeX, final int sizeY, final int rotation)
static double getDistance(Position first, Position second)
Gets the Euclidean (straight-line) distance between two Position s.
double getDistance(Position other)
Gets the distance between this location and another location.
static Position create(int x, int y)
Creates a location.
int getChunkY()
Gets the chunk y coordinate.
Definition Position.java:81
Position copy()
Creates a deep copy of this location.
boolean matches(int x, int y, int z)
Represents a single region.
Definition Region.java:21
Region getRegion(Position position)
Gets a region by position.
Handles miscellaneous methods.
Definition Utility.java:27
static int getDistance(Interactable source, Position target)
Definition Utility.java:363
An object implementing Interactable has uses.