RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
SimplePathFinder.java
1package com.osroyale.game.world.pathfinding.path.impl;
2
3import com.osroyale.game.world.Interactable;
4import com.osroyale.game.world.entity.mob.Direction;
5import com.osroyale.game.world.entity.mob.Mob;
6import com.osroyale.game.world.pathfinding.path.Path;
7import com.osroyale.game.world.pathfinding.path.PathFinder;
8import com.osroyale.game.world.position.Position;
9import com.osroyale.game.world.region.Region;
10import com.osroyale.util.Utility;
11
12import java.util.ArrayDeque;
13import java.util.Deque;
14
15import static com.osroyale.game.world.entity.mob.Direction.NONE;
16import static com.osroyale.game.world.entity.mob.Direction.getDirection;
17
62
63public final class SimplePathFinder extends PathFinder {
64
71 @Override
72 public Path find(Mob source, Position end, int targetWidth, int targetLength) {
73 int approximation = (int) (source.getPosition().getLongestDelta(end) * 1.5);
74 Deque<Position> positions = new ArrayDeque<>(approximation);
75 return new Path(addWalks(source, end, targetWidth, targetLength, positions));
76 }
77
87 private Deque<Position> addWalks(Mob source, Position target, int targetWidth, int targetLength, Deque<Position> positions) {
88 Position current = source.getPosition();
89 Position targ = target;
90 Interactable targInt = Interactable.create(target, targetWidth, targetLength);
91
92 if (targetWidth > 0 || targetLength > 0)
93 target = Utility.findBestInside(source, targInt);
94
95 boolean projectiles = source.pathfinderProjectiles;
96
97 while (!Region.reachable(targ, targetWidth, targetLength, current, source.width(), source.length())) {
98 Direction direction = getDirection(current, target);
99
100 if (!traversable(source, current, targInt, direction, projectiles)) {
101 if (direction == Direction.NORTH_WEST) {
102 if (traversable(source, current, targInt, Direction.WEST, projectiles)) {
103 direction = Direction.WEST;
104 } else if (traversable(source, current, targInt, Direction.NORTH, projectiles)) {
105 direction = Direction.NORTH;
106 } else
107 break;
108 } else if (direction == Direction.NORTH_EAST) {
109 if (traversable(source, current, targInt, Direction.NORTH, projectiles)) {
110 direction = Direction.NORTH;
111 } else if (traversable(source, current, targInt, Direction.EAST, projectiles)) {
112 direction = Direction.EAST;
113 } else
114 break;
115 } else if (direction == Direction.SOUTH_WEST) {
116 if (traversable(source, current, targInt, Direction.SOUTH, projectiles)) {
117 direction = Direction.SOUTH;
118 } else if (traversable(source, current, targInt, Direction.WEST, projectiles)) {
119 direction = Direction.WEST;
120 } else
121 break;
122 } else if (direction == Direction.SOUTH_EAST) {
123 if (traversable(source, current, targInt, Direction.SOUTH, projectiles)) {
124 direction = Direction.SOUTH;
125 } else if (traversable(source, current, targInt, Direction.EAST, projectiles)) {
126 direction = Direction.EAST;
127 } else
128 break;
129 } else
130 break;
131 }
132
133 if (direction == NONE)
134 break;
135
136 current = current.transform(direction.getFaceLocation());
137 positions.addLast(current);
138 }
139 return positions;
140 }
141
142 private boolean traversable(Mob source, Position current, Interactable target, Direction direction, boolean projectiles) {
143 Position next = current.transform(direction.getFaceLocation());
144 return (projectiles ? projectileCheck(current, next) : traversable(current, source.width(), direction))
145 && !Utility.inside(next, source.width(), source.length(), target.getPosition(), target.width(), target.length());
146 }
147
148}
boolean projectileCheck(Position current, Position going)
Path find(Mob source, Position end, int targetWidth, int targetLength)
Position transform(int diffX, int diffY, int diffZ)
static Interactable create(Position position)