use of org.powerbot.script.Tile in project powerbot by powerbot.
the class Movement method closestOnMap.
/**
* Determines the closest tile on the map to the provided {@link Locatable}.
*
* @param locatable the {@link Locatable}
* @return the closest {@link Tile} on map to the provided {@link Locatable}
*/
public Tile closestOnMap(final Locatable locatable) {
final Tile local = ctx.players.local().tile();
final Tile tile = locatable.tile();
if (local == Tile.NIL || tile == Tile.NIL) {
return Tile.NIL;
}
if (new TileMatrix(ctx, tile).onMap()) {
return tile;
}
final int x2 = local.x();
final int y2 = local.y();
int x1 = tile.x();
int y1 = tile.y();
final int dx = Math.abs(x2 - x1);
final int dy = Math.abs(y2 - y1);
final int sx = (x1 < x2) ? 1 : -1;
final int sy = (y1 < y2) ? 1 : -1;
int off = dx - dy;
for (; ; ) {
final Tile t = new Tile(x1, y1, local.floor());
if (new TileMatrix(ctx, t).onMap()) {
return t;
}
if (x1 == x2 && y1 == y2) {
break;
}
final int e2 = 2 * off;
if (e2 > -dy) {
off = off - dy;
x1 = x1 + sx;
}
if (e2 < dx) {
off = off + dx;
y1 = y1 + sy;
}
}
return Tile.NIL;
}
use of org.powerbot.script.Tile in project powerbot by powerbot.
the class Movement method step.
/**
* Steps towards the provided {@link Locatable}.
*
* @param locatable the locatable to step towards
* @return {@code true} if stepped; otherwise {@code false}
*/
public boolean step(final Locatable locatable) {
Tile loc = locatable.tile();
if (!new TileMatrix(ctx, loc).onMap()) {
loc = closestOnMap(loc);
}
final Tile t = loc;
final Filter<Point> f = new Filter<Point>() {
@Override
public boolean accept(final Point point) {
return ctx.input.click(true);
}
};
return ctx.input.apply(new Targetable() {
private final TileMatrix tile = new TileMatrix(ctx, t);
@Override
public Point nextPoint() {
return tile.mapPoint();
}
@Override
public boolean contains(final Point point) {
final Point p = tile.mapPoint();
final Rectangle t = new Rectangle(p.x - 2, p.y - 2, 4, 4);
return t.contains(point);
}
}, f);
}
use of org.powerbot.script.Tile in project powerbot by powerbot.
the class LocalPath method getCosts.
static double[][] getCosts(final ClientContext ctx, final int w, final int h) {
final Client client = ctx.client();
final Landscape landscape = client.getLandscape();
final org.powerbot.bot.rt4.client.Tile[][][] tiles;
final int floor = client.getFloor();
final org.powerbot.bot.rt4.client.Tile[][] rows;
if (landscape == null || (tiles = landscape.getTiles()) == null || floor < 0 || floor > tiles.length || (rows = tiles[floor]) == null) {
return new double[0][0];
}
final double[][] arr = new double[w][h];
for (int x = 0; x < Math.min(w, rows.length); x++) {
final org.powerbot.bot.rt4.client.Tile[] row = rows[x];
if (row == null) {
continue;
}
final int h2 = row.length;
for (int y = 0; y < Math.min(h, h2); y++) {
final org.powerbot.bot.rt4.client.Tile tile = row[y];
if (tile == null) {
continue;
}
if (tile.getGameObjectLength() > 0 || tile.getBoundaryObject() != null || tile.getWallObject() != null) {
for (int dx = Math.max(0, x - 1); dx <= Math.min(w - 1, x + 1); dx++) {
for (int dy = Math.max(0, y - 1); dy <= Math.min(h - 1, y + 1); dy++) {
arr[dx][dy] += Random.nextDouble();
}
}
}
}
}
return arr;
}
use of org.powerbot.script.Tile in project powerbot by powerbot.
the class LocalPath method valid.
@Override
public boolean valid() {
Tile end = destination.tile();
if (end == null || end == Tile.NIL) {
return false;
}
if (end.equals(tile) && tilePath != null) {
return true;
}
tile = end;
Tile start = ctx.players.local().tile();
final Tile base = ctx.game.mapOffset();
if (base == Tile.NIL || start == Tile.NIL || end == Tile.NIL) {
return false;
}
start = start.derive(-base.x(), -base.y());
end = end.derive(-base.x(), -base.y());
final Graph graph = getGraph(ctx);
final Node[] path;
final Node nodeStart, nodeStop;
if (graph != null && (nodeStart = graph.getNode(start.x(), start.y())) != null && (nodeStop = graph.getNode(end.x(), end.y())) != null) {
dijkstra(graph, nodeStart, nodeStop);
path = follow(nodeStop);
} else {
path = new Node[0];
}
if (path.length > 0) {
final Tile[] arr = new Tile[path.length];
for (int i = 0; i < path.length; i++) {
arr[i] = base.derive(path[i].x, path[i].y);
}
tilePath = new TilePath(ctx, arr);
return true;
}
return false;
}
use of org.powerbot.script.Tile in project powerbot by powerbot.
the class Movement method distance.
/**
* Returns the amount of steps between two locations. This will return -1 if
* the amount of steps was indeterminate (for example, one of the locations wasn't loaded or
* they are both not on the same floor).
* <br><br>
* Note: this can be resource intensive, as it generates a path between these
* two locations to find the distance. This should only be used if you need an accurate
* measurement for the amount of steps required. Please consider using
* {@link Tile#distanceTo(Locatable)} for euclidean distance for the length of a straight
* line between these two points.
* @param l1 Location A
* @param l2 Location B
* @return The amount of steps required to traverse between these two locations.
*/
public int distance(final Locatable l1, final Locatable l2) {
final Tile b = ctx.game.mapOffset();
Tile t1, t2;
if (b == null || l1 == null || (t1 = l1.tile()) == null || l2 == null || (t2 = l2.tile()) == null || b == Tile.NIL || t1 == Tile.NIL || t2 == Tile.NIL || l1.tile().floor() != l2.tile().floor() || l1.tile().floor() != b.floor()) {
return -1;
}
t1 = t1.derive(-b.x(), -b.y());
t2 = t2.derive(-b.x(), -b.y());
final LocalPath.Graph graph = LocalPath.getGraph(ctx);
final LocalPath.Node[] path;
final LocalPath.Node nodeStart, nodeStop;
if (graph != null && (nodeStart = graph.getNode(t1.x(), t1.y())) != null && (nodeStop = graph.getNode(t2.x(), t2.y())) != null) {
LocalPath.dijkstra(graph, nodeStart, nodeStop);
path = LocalPath.follow(nodeStop);
} else {
path = new LocalPath.Node[0];
}
final int l = path.length;
return l > 0 ? l : -1;
}
Aggregations