Jump to content

Path and area recording scripts


Recommended Posts

Posted (edited)

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
Posted (edited)

Thanks for the contribution although there is already one its always nice to see users who aren't well known contributing something happy.png

i couldnt have said it better.

 

@normangorman nice to see i convinced you to switch into the dark side tongue.png

Edited by josedpay

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