RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
Direction.java
1package com.osroyale.game.world.entity.mob;
2
3import com.google.common.collect.ImmutableList;
4import com.osroyale.game.world.position.Position;
5import com.osroyale.util.Utility;
6
43
44public enum Direction {
45 NORTH(0, 1),
46 NORTH_EAST(1, 1),
47 EAST(1, 0),
48 SOUTH_EAST(1, -1),
49 SOUTH(0, -1),
50 SOUTH_WEST(-1, -1),
51 WEST(-1, 0),
52 NORTH_WEST(-1, 1),
53 NONE(0, 0);
54
55 private final int directionX;
56 private final int directionY;
57 private final Position faceLocation;
58 Direction(int directionX, int directionY) {
59 this.directionX = directionX;
60 this.directionY = directionY;
61 this.faceLocation = new Position(directionX, directionY);
62 }
63
64
65 public static final ImmutableList<Direction> DIRECTIONS = ImmutableList.of(NORTH_WEST, NORTH, NORTH_EAST, WEST, EAST, SOUTH_WEST, SOUTH, SOUTH_EAST);
66 private static final Direction[] VALID = new Direction[]{NORTH, NORTH_EAST, EAST, SOUTH_EAST, SOUTH, SOUTH_WEST, WEST, NORTH_WEST};
67
68 public Position getFaceLocation() {
69 return faceLocation;
70 }
71
75 public static final byte[] DELTA_X = new byte[]{-1, 0, 1, -1, 1, -1, 0, 1};
76
80 public static final byte[] DELTA_Y = new byte[]{1, 1, 1, 0, 0, -1, -1, -1};
81
87 public static Direction getDirection(int deltaX, int deltaY) {
88 if (deltaY >= 1) {
89 if (deltaX >= 1) {
90 return NORTH_EAST;
91 } else if (deltaX == 0) {
92 return NORTH;
93 } else if (deltaX <= -1) {
94 return NORTH_WEST;
95 }
96 } else if (deltaY == 0) {
97 if (deltaX >= 1) {
98 return EAST;
99 } else if (deltaX == 0) {
100 return NONE;
101 } else if (deltaX <= -1) {
102 return WEST;
103 }
104 } else if (deltaY <= -1) {
105 if (deltaX >= 1) {
106 return SOUTH_EAST;
107 } else if (deltaX == 0) {
108 return SOUTH;
109 } else if (deltaX <= -1) {
110 return SOUTH_WEST;
111 }
112 }
113
114 return Direction.NONE;
115
116 }
117
125 public static Direction getDirection(Position position, Position other) {
126 final int deltaX = other.getX() - position.getX();
127 final int deltaY = other.getY() - position.getY();
128
129 if (deltaY >= 1) {
130 if (deltaX >= 1) {
131 return NORTH_EAST;
132 } else if (deltaX == 0) {
133 return NORTH;
134 } else if (deltaX <= -1) {
135 return NORTH_WEST;
136 }
137 } else if (deltaY == 0) {
138 if (deltaX >= 1) {
139 return EAST;
140 } else if (deltaX == 0) {
141 return NONE;
142 } else if (deltaX <= -1) {
143 return WEST;
144 }
145 } else if (deltaY <= -1) {
146 if (deltaX >= 1) {
147 return SOUTH_EAST;
148 } else if (deltaX == 0) {
149 return SOUTH;
150 } else if (deltaX <= -1) {
151 return SOUTH_WEST;
152 }
153 }
154
155 return Direction.NONE;
156
157 }
158
159 public static Direction getRandomDirection() {
160 int random = Utility.random(4);
161 switch (random) {
162 case 1: return NORTH;
163 case 2: return EAST;
164 case 3: return SOUTH;
165 case 4: return WEST;
166 default: return SOUTH;
167 }
168 }
169
177 public static Direction getManhattanDirection(Position position, Position other) {
178 final int deltaX = other.getX() - position.getX();
179 final int deltaY = other.getY() - position.getY();
180 if (deltaY >= 1) {
181 if (deltaX == 0) {
182 return NORTH;
183 }
184 } else if (deltaY == 0) {
185 if (deltaX >= 1) {
186 return Direction.EAST;
187 } else if (deltaX == 0) {
188 return Direction.SOUTH;
189 } else if (deltaX <= -1) {
190 return Direction.WEST;
191 }
192 } else if (deltaY <= -1) {
193 if (deltaX == 0) {
194 return SOUTH;
195 }
196 }
197 return Direction.SOUTH;
198
199 }
200
201 public static Direction getOppositeDirection(Direction direction) {
202 switch (direction) {
203 case EAST:
204 return WEST;
205 case NORTH:
206 return SOUTH;
207 case NORTH_EAST:
208 return SOUTH_WEST;
209 case NORTH_WEST:
210 return SOUTH_EAST;
211 case SOUTH:
212 return NORTH;
213 case SOUTH_EAST:
214 return NORTH_WEST;
215 case SOUTH_WEST:
216 return NORTH_EAST;
217 case WEST:
218 return EAST;
219 default:
220 return NONE;
221 }
222 }
223
224 public static Direction getFollowDirection(Position source, Position target) {
225 Direction dir = getDirection(source, target);
226 int deltaX = Math.abs(source.getX() - target.getX());
227 int deltaY = Math.abs(source.getY() - target.getY());
228 boolean vertical = deltaY > deltaX;
229 switch (dir) {
230 case NORTH_EAST:
231 if (vertical) return NORTH;
232 else return EAST;
233 case NORTH_WEST:
234 if (vertical) return NORTH;
235 else return WEST;
236 case SOUTH_EAST:
237 if (vertical) return SOUTH;
238 else return EAST;
239 case SOUTH_WEST:
240 if (vertical) return SOUTH;
241 else return WEST;
242 default: return dir;
243 }
244 }
245
252 public static int getDoorOrientation(Direction direction) {
253 switch (direction) {
254 case WEST:
255 return 0;
256 case NORTH:
257 return 1;
258 case EAST:
259 return 2;
260 case SOUTH:
261 return 3;
262 default:
263 return 3;
264 }
265 }
266
275 public static Direction ofManhattan(int orientation) {
276 switch (orientation) {
277 case 0:
278 return EAST;
279 case 1:
280 return SOUTH;
281 case 2:
282 return WEST;
283 case 3:
284 return NORTH;
285 }
286 return NONE;
287 }
288
289 public int getManhattanOrientation() {
290 switch (this) {
291 case NORTH_WEST:
292 return 0;
293 case NORTH:
294 return 1;
295 case NORTH_EAST:
296 return 2;
297 case WEST:
298 return 3;
299 case EAST:
300 return 4;
301 case SOUTH_WEST:
302 return 5;
303 case SOUTH:
304 return 6;
305 case SOUTH_EAST:
306 return 7;
307 default:
308 return -1;
309 }
310 }
311
312 public static int direction(int dx, int dy) {
313 if (dx < 0) {
314 if (dy < 0) {
315 return 5;
316 } else if (dy > 0) {
317 return 0;
318 } else {
319 return 3;
320 }
321 } else if (dx > 0) {
322 if (dy < 0) {
323 return 7;
324 } else if (dy > 0) {
325 return 2;
326 } else {
327 return 4;
328 }
329 } else {
330 if (dy < 0) {
331 return 6;
332 } else if (dy > 0) {
333 return 1;
334 } else {
335 return -1;
336 }
337 }
338 }
339
340 public int getDirectionX() {
341 return directionX;
342 }
343 public int getDirectionY() {
344 return directionY;
345 }
346
347 public static Direction[] valid() {
348 return VALID;
349 }
350
351}
static Direction getManhattanDirection(Position position, Position other)
static Direction ofManhattan(int orientation)
static int getDoorOrientation(Direction direction)
static Direction getDirection(int deltaX, int deltaY)
static Direction getDirection(Position position, Position other)