Search in sources :

Example 26 with Tile

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;
}
Also used : Tile(org.powerbot.script.Tile) Point(java.awt.Point)

Example 27 with Tile

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);
}
Also used : Targetable(org.powerbot.script.Targetable) Filter(org.powerbot.script.Filter) Rectangle(java.awt.Rectangle) Tile(org.powerbot.script.Tile) Point(java.awt.Point)

Example 28 with Tile

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;
}
Also used : Landscape(org.powerbot.bot.rt4.client.Landscape) Tile(org.powerbot.script.Tile) Client(org.powerbot.bot.rt4.client.Client)

Example 29 with Tile

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;
}
Also used : Tile(org.powerbot.script.Tile)

Example 30 with Tile

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;
}
Also used : Tile(org.powerbot.script.Tile) Point(java.awt.Point)

Aggregations

Tile (org.powerbot.script.Tile)40 Point (java.awt.Point)16 ArrayList (java.util.ArrayList)5 Client (org.powerbot.bot.rt4.client.Client)5 FontMetrics (java.awt.FontMetrics)4 Condition (org.powerbot.script.Condition)4 Rectangle (java.awt.Rectangle)3 Client (org.powerbot.bot.rt6.client.Client)3 Player (org.powerbot.script.rt4.Player)3 Player (org.powerbot.script.rt6.Player)3 HashMap (java.util.HashMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 NodeDeque (org.powerbot.bot.rt4.client.NodeDeque)2 Filter (org.powerbot.script.Filter)2 Locatable (org.powerbot.script.Locatable)2 Targetable (org.powerbot.script.Targetable)2 TileMatrix (org.powerbot.script.rt6.TileMatrix)2 LinkedList (java.util.LinkedList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 ItemNode (org.powerbot.bot.rt4.client.ItemNode)1