RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
Position.java
1package com.osroyale.game.world.position;
2
3import com.osroyale.game.world.Interactable;
4import com.osroyale.game.world.World;
5import com.osroyale.game.world.entity.mob.Mob;
6import com.osroyale.game.world.region.Region;
7import com.osroyale.util.Utility;
8
50
51public class Position {
52
54 public static final int HEIGHT_LEVELS = 4;
55
57 private final int x;
58
60 private final int y;
61
63 private final int height;
64
66 public Position(int x, int y) {
67 this(x, y, 0);
68 }
69
71 public Position(int x, int y, int height) {
72 this.x = x;
73 this.y = y;
74 this.height = height < 0 ? 0 : height % 4;
75 }
76
78 public int getX() {
79 return x;
80 }
81
83 public int getY() {
84 return y;
85 }
86
88 public int getHeight() {
89 return height;
90 }
91
93 public int getLocalX() {
94 return getLocalX(this);
95 }
96
98 public int getLocalY() {
99 return getLocalY(this);
100 }
101
103 public int getLocalX(Position origin) {
104 return x - 8 * origin.getChunkX();
105 }
106
108 public int getLocalY(Position origin) {
109 return y - 8 * origin.getChunkY();
110 }
111
113 public int getChunkX() {
114 return (x >> 3) - 6;
115 }
116
118 public int getChunkY() {
119 return (y >> 3) - 6;
120 }
121
122 public int getRegionX() {
123 return getChunkX() / 8;
124 }
125
126 public int getRegionY() {
127 return getChunkY() / 8;
128 }
129
130 public Region getRegion() {
131 return World.getRegions().getRegion(this);
132 }
133
134 public Position north() {
135 return transform(0, 1);
136 }
137
138 public Position east() {
139 return transform(1, 0);
140 }
141
142 public Position south() {
143 return transform(0, -1);
144 }
145
146 public Position west() {
147 return transform(-1, 0);
148 }
149
150 public Position northEast() {
151 return transform(1, 1);
152 }
153
154 public Position northWest() {
155 return transform(-1, 1);
156 }
157
158 public Position southEast() {
159 return transform(1, -1);
160 }
161
162 public Position southWest() {
163 return transform(-1, -1);
164 }
165
173 public static int getManhattanDistance(Interactable origin, Interactable target) {
174 return Utility.getDistance(origin, target);
175 }
176
184 public boolean isWithinDistance(Position other, int radius) {
185 if (height != other.height) {
186 return false;
187 }
188
189 final int deltaX = Math.abs(other.x - x);
190 final int deltaY = Math.abs(other.y - y);
191 return deltaX <= radius && deltaY <= radius;
192 }
193
200 public double getDistance(Position other) {
201 if (height != other.height) return 0;
202 int dx = other.x - x;
203 int dy = other.y - y;
204 return Math.sqrt(dx * dx + dy * dy);
205 }
206
213 public int getDistances(Position other) {
214 return (int) Math.sqrt(Math.pow(x - other.x, 2) + Math.pow(y - other.y, 2) + Math.pow(height - other.height, 2));
215 }
216
217 public int getChevDistance(Position other) {
218 return Math.max(Math.abs(other.getX() - getX()), Math.abs(other.getY() - getY()));
219 }
220
227 public static double getDistance(Position first, Position second) {
228 final int dx = second.getX() - first.getX();
229 final int dy = second.getY() - first.getY();
230 return Math.sqrt(dx * dx + dy * dy);
231 }
232
240 public int getManhattanDistance(Position other) { //dont use this, it calcs wrong btw
241 if (other == null || height != other.height) return Integer.MAX_VALUE;
242 int dx = Math.abs(other.x - x);
243 int dy = Math.abs(other.y - y);
244 return dx + dy;
245 }
246
247 public boolean isViewableFrom(Position other) {
248 if (this.getHeight() != other.getHeight())
249 return false;
250 Position p = this.getDelta(this, other);
251 return p.x <= 14 && p.x >= -15 && p.y <= 14 &&
252 p.y >= -15;
253 }
254
255 public Position getDelta(Position location, Position other) {
256 return new Position(other.x - location.x, other.y - location.y, other.getHeight() - location.getHeight());
257 }
258
265 public static int getManhattanDistance(Position first, Position second) {
266 final int dx = Math.abs(second.getX() - first.getX());
267 final int dy = Math.abs(second.getY() - first.getY());
268 return dx + dy;
269 }
270
277 public int getLongestDelta(Position other) {
278 if (height != other.height) return 0;
279 int deltaX = Math.abs(getX() - other.getX());
280 int deltaY = Math.abs(getY() - other.getY());
281 return Math.max(deltaX, deltaY);
282 }
283
292 public static Position create(int x, int y, int z) {
293 return new Position(x, y, z);
294 }
295
303 public static Position create(int x, int y) {
304 return new Position(x, y);
305 }
306
315 public Position transform(int diffX, int diffY, int diffZ) {
316 return new Position(x + diffX, y + diffY, height + diffZ);
317 }
318
326 public Position transform(int diffX, int diffY) {
327 return new Position(x + diffX, y + diffY, height);
328 }
329
330
331 public boolean inLocation(Position southWest, Position northEast, boolean inclusive) {
332 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();
333 }
334
342 if (other == null) return this;
343 return transform(other.getX(), other.getY(), other.getHeight());
344 }
345
351 public Position copy() {
352 return new Position(x, y, height);
353 }
354
355 @Override
356 public boolean equals(Object obj) {
357 if (obj == this) return true;
358
359 if (obj instanceof Position) {
360 Position other = (Position) obj;
361 return x == other.x && y == other.y && height == other.height;
362 }
363
364 return false;
365 }
366
367 public boolean matches(int x, int y) {
368 return this.x == x && this.y == y;
369 }
370
371 public boolean matches(int x, int y, int z) {
372 return this.x == x && this.y == y && this.height == z;
373 }
374
375 @Override
376 public int hashCode() {
377 return hash(x, y, height);
378 }
379
380 @Override
381 public String toString() {
382 return String.format("pos[x=%d, y=%d, z=%d]", x, y, height);
383 }
384
385 public static int hash(int x, int y, int z) {
386 return (y << 16) | (x << 8) | z;
387 }
388
389 public static boolean isWithinDiagonalDistance(Mob attacker, Mob defender, int distance) {
390 int attackerSize = 1;
391 int defenderSize = 1;
392
393 if(attacker.isNpc())
394 attackerSize = attacker.getNpc().definition.getSize();
395 if(defender.isNpc())
396 defenderSize = defender.getNpc().definition.getSize();
397
398 int e_offset_x = attackerSize - 1 + distance;
399 int e_offset_y = attackerSize - 1 + distance;
400
401 int o_offset_x = defenderSize - 1 + distance;
402 int o_offset_y = defenderSize - 1 + distance;
403
404 Position entity_pos = attacker.getPosition().copy();
405 Position other_pos = defender.getPosition().copy();
406
407 boolean inside_entity =
408 (other_pos.getX() <= entity_pos.getX() + e_offset_x && other_pos.getX() >= (entity_pos.getX() - distance)) &&
409 (other_pos.getY() <= entity_pos.getY() + e_offset_y && other_pos.getY() >= (entity_pos.getY() - distance));
410
411 boolean inside_other =
412 (entity_pos.getX() <= other_pos.getX() + o_offset_x && entity_pos.getX() >= (other_pos.getX() - distance)) &&
413 (entity_pos.getY() <= other_pos.getY() + o_offset_y && entity_pos.getY() >= (other_pos.getY() - distance));
414
415
416 return inside_entity || inside_other;
417 }
418
419 public int getChebyshevDistance(Position other) {
420 return getChebyshevDistance(getX(), getY(), other.getX(), other.getY());
421 }
422
423 public int getChebyshevDistance(int x1, int y1, int x2, int y2) {
424 return Math.max(Math.abs(x2 - x1), Math.abs(y2 - y1));
425 }
426
427 public int getCoordFaceX(final int sizeX) {
428 return getCoordFaceX(sizeX, -1, -1);
429 }
430
431 public int getCoordFaceX(final int sizeX, final int sizeY, final int rotation) {
432 return getX() + ((rotation == 1 || rotation == 3 ? sizeY : sizeX) - 1) / 2;
433 }
434
435 public int getCoordFaceY(final int sizeY) {
436 return getCoordFaceY(-1, sizeY, -1);
437 }
438
439 public int getCoordFaceY(final int sizeX, final int sizeY, final int rotation) {
440 return getY() + ((rotation == 1 || rotation == 3 ? sizeX : sizeY) - 1) / 2;
441 }
442
443}
boolean isWithinDistance(Position other, int radius)
static int getManhattanDistance(Interactable origin, Interactable target)
static int getManhattanDistance(Position first, Position second)
static Position create(int x, int y, int z)
Position transform(int diffX, int diffY, int diffZ)
Position transform(int diffX, int diffY)
static Position create(int x, int y)
Position(int x, int y, int height)
Definition Position.java:71
static double getDistance(Position first, Position second)