Jump to content

getRandomizedPath(Position[], int), getLocalPath(Position, Position[])


Recommended Posts

Posted
    /**
     * Returns an array of {@link org.osbot.rs07.api.map.Position} nodes that
     * are randomized within {@code radius}. The {@code path} will not be
     * modified, nor will it hold any references to the returned array.
     *
     * @param path
     *            the path that will be randomized.
     * @param radius
     *            the radius in which the path will be randomized.
     * @return the new randomized path.
     */
    public static Position[] getRandomizedPath(Position[] path, int radius) {
        Position[] newPath = new Position[path.length];
        int idx = 0;
        for (Position node : path)
            newPath[idx++] = node.translate(MethodProvider.random(radius), MethodProvider.random(radius));
        return newPath;
    }

    /**
     * Returns an array of {@link org.osbot.rs07.api.map.Position} nodes that
     * are localized for the {@code node} point. This method is very useful for
     * when the script needs to cut out certain positions that aren't relevant;
     * for example, if the player starts a script when they are in the middle of
     * a path instead of the beginning or ending. Take for instance, this
     * pseudo-code.
     * 
     * <pre>
     * Position[] path = { position1, position2, position3, position4, position5, position6, position7 };
     * </pre>
     * 
     * If {@code node} is closest to "position3" in distance, the returned array
     * will look like...
     * 
     * <pre>
     * ... = { position3, position4, position5, position6, position7 };
     * </pre>
     * 
     * @param node
     *            the node in which the path should be localized for.
     * @param path
     *            the path that will be localized.
     * @return the new localized path.
     */
    public static Position[] getLocalPath(Position node, Position[] path) {
        List<Position> newPath = new LinkedList<>();
        int dist = -1;
        int index = -1;
        for (int i = 0; i < path.length; i++) {
            int newDist = node.distance(path[i]);
            if (dist == -1 || newDist < dist) {
                dist = newDist;
                index = i;
            }
        }
        IntStream.range(index, path.length).forEach(x -> newPath.add(path[x]));
        return newPath.toArray(new Position[newPath.size()]);
    }

Could come in handy for scripts that utilize walking, everything is explained in the documentation.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...