Jump to content

Path and area recording scripts


Normangorman

Recommended Posts

Hi all! Two scripts for you here - one to record the path your character walks and another to record an area of squares that you walk over. I'm sure scripts like this exist out there already but I couldn't find any, and wanted to try making my own. Both are useful for generating arrays of positions to be copy-pasted into your project.

 

WALK RECORDER:

Records the path your character walks, and then logs it as an array of positions.

 

Code:

import org.osbot.rs07.Bot;
import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

import java.awt.*;
import java.util.ArrayList;

@ScriptManifest(name ="OSBot Walk Recorder", author = "Normangorman", version = 1.0, info = "", logo = "")
public class WalkRecorderScript extends Script {
    private java.util.List<Position> positions;
    private Position lastPosition;

    @Override
    public void onStart() {
        positions = new ArrayList<Position>();
        lastPosition = myPosition();
        positions.add(lastPosition);
    }

    @Override
    public void onExit() {
        log("///// Path as Position array: /////");
        String str = "\nPosition[] path = {";
        for (int i=0; i<positions.size(); i++) {
            Position p = positions.get(i);
            // Treat the last one differently to finish off the array.
            if (i == positions.size() - 1) {
                str += String.format("new Position(%d,%d,%d)};", p.getX(), p.getY(), p.getZ());
            }
            else {
                str += String.format("new Position(%d,%d,%d), ", p.getX(), p.getY(), p.getZ());
            }
        }
        log(str);
        log("//////////////////////////////////");
    }

    @Override
    public int onLoop() throws InterruptedException {
        Position currentPosition = myPosition();
        log(currentPosition.distance(lastPosition));
        if (currentPosition.distance(lastPosition) > 1) {
            positions.add(currentPosition);
            lastPosition = currentPosition;
        }
        sleep(random(900,1000));
        return 1;
    }

    @Override
    public void onPaint(Graphics2D g) {
        super.onPaint(g);
        g.setColor(Color.CYAN);
        g.drawString("Number of points in path: " + positions.size(), 10, 280);
        g.drawString(String.format("Most recent point added: (%d, %d, %d)", lastPosition.getX(), lastPosition.getY(), lastPosition.getZ()), 10, 295);

        g.setColor(Color.YELLOW);
        Bot b = getBot();
        for (Position p : positions) {
            if (p.isVisible(b)) {
                g.fillPolygon(p.getPolygon(b));
            }
        }
    }
}

Screenshot:

walk_recorder.PNG

 

The output you get from the log for this script looks something like this:

Position[] path = {new Position(3198,3279,0), new Position(3200,3279,0), new Position(3203,3278,0), new Position(3205,3278,0), new Position(3207,3279,0), new Position(3209,3279,0), new Position(3211,3278,0)};

AREA RECORDER:

This second script uses basically the same principle, however by using a more efficient data structure it allows much greater areas to be recorded. It is especially good for mapping the walkable regions of areas filled with obstacles such as the stronghold of security. It is also capable of handling large areas (I was able to record the whole of the Lumbridge cow field with it which is around 400 squares).

 

Code:

import org.osbot.rs07.Bot;
import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

import java.awt.*;
import java.util.HashSet;
import java.util.Iterator;

@ScriptManifest(name = "OSBot Area Recorder", author = "Normangorman", version = 1.0, info = "", logo = "")
public class AreaRecorderScript extends Script {
    private java.util.HashSet<Position> positions = new HashSet<Position>();
    private Position currentPosition;

    @Override
    public void onStart() {
        currentPosition = myPosition();
    }

    @Override
    public void onExit() {
        log("////// Position array: //////");
        String str = "\nnew Area(new Position[] {";
        Iterator<Position> posIterator = positions.iterator();

        while (posIterator.hasNext()) {
            Position p = posIterator.next();

            if (posIterator.hasNext()) {
                str += String.format("new Position(%d,%d,%d), ", p.getX(), p.getY(), p.getZ());
            }
            else {
                str += String.format("new Position(%d,%d,%d)});", p.getX(), p.getY(), p.getZ());
            }
        }

        log(str);
        log("/////////////////////////////");
    }

    @Override
    public int onLoop() throws InterruptedException {
        currentPosition = myPosition();
        positions.add(currentPosition);
        return 40;
    }

    @Override
    public void onPaint(Graphics2D g) {
        // Debug the number of positions in the area.
        g.setColor(Color.CYAN);
        g.drawString("Number of positions in area: " + positions.size(), 10, 280);

        // Highlight all positions in the area.
        Bot b = getBot();
        Iterator<Position> posIterator = positions.iterator();
        Position p;
        while (posIterator.hasNext()) {
            p = posIterator.next();

            if (p.isVisible(b)) {
                g.fillPolygon(p.getPolygon(b));
            }
        }

        // Fill in the current square.
        g.setColor(Color.RED);
        g.fillPolygon(currentPosition.getPolygon(b));
        super.onPaint(g);
    }
}

Screenshot:

 

area_recorder.PNG

 

Example log output:

new Area({new Position(3197,3289,0), new Position(3197,3291,0), new Position(3197,3290,0), new Position(3197,3287,0), new Position(3197,3288,0));

Hope that someone finds these useful!

Edited by Normangorman
  • Like 2
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...