Jump to content

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


lare96

Recommended Posts

    /**
     * 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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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