Jump to content

Automatic Path Mapping Snippet


Omicron

Recommended Posts

Hey everyone, I began scripting about a day back and I quickly realized it was a hassle to map each individual tile for your path, so I quickly wrote up a class which can be added to any script or be made into a script of it's on in order to map the tiles walked upon. 

package main;

import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.script.Script;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @author Omicron
 */
public class PathMapper {

    private Script script;
    private BufferedWriter writer;
    private ArrayList<Position> positions = new ArrayList<>();


    /**
     * @param script - Script instance which will be used
     * @param path - Path to where  you want the tile dump to be generated
     */

    public PathMapper(Script script, String path) {
        this.script = script;
        try {
            writer = new BufferedWriter(new FileWriter(path, true));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void execute() {
        Position position = script.myPlayer().getPosition();
        positions.add(position);
    }

    /**
     * Writes all information to the file given by the path
     */

    public void dump() {
        try {
            writer.newLine();
            writer.write("ArrayList<Position> path = new ArrayList<Position>();");
            writePositions();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * Writes each individual position to the file
     */

    private void writePositions() {
        positions.forEach(position -> {
            int x = position.getX();
            int y = position.getY();
            int z = position.getZ();
            try {
                writer.newLine();
                writer.write("path.add(new Position("
                        + x + ","
                        + y + ","
                        + z + "));");
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

}

An example of how the class be used is below : 

package main;

import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

/**
 * @author Omicron
 */

@ScriptManifest(author = "Omicron", info = "Test", name = "Path mapper", version = .1,  logo = "")

public class Test extends Script {

    PathMapper mapper = new PathMapper(this, "C:\\Users\\Omicron\\Desktop\\tiledump.txt");

    @Override
    public int onLoop() throws InterruptedException {
        mapper.execute();
        return 5000;
    }

    @Override
    public void onExit() throws InterruptedException {
        mapper.dump();
    }

}

I walked from Varrock west bank to the East bank with the script running to show you an example of what a tile dump would look like:  

ArrayList<Position> path = new ArrayList<Position>();
path.add(new Position(3253,3420,0));
path.add(new Position(3253,3426,0));
path.add(new Position(3246,3429,0));
path.add(new Position(3240,3429,0));
path.add(new Position(3234,3430,0));
path.add(new Position(3227,3429,0));
path.add(new Position(3222,3429,0));
path.add(new Position(3215,3429,0));
path.add(new Position(3208,3429,0));
path.add(new Position(3201,3428,0));
path.add(new Position(3194,3429,0));
path.add(new Position(3186,3429,0));
path.add(new Position(3182,3434,0));

NOTES: 

 

*It should be noted with this running you could walk around normally and it would still mapthe path (you don't need to click on individual tiles) 

 

*If enough people want me to I might build upon this to make path loading automated as well, such as (mapper.loadPath()), you wouldn't need to hard code the Positions at all, it'd automatically load from a generated file

 

 

 

 

Link to comment
Share on other sites

why you no check SDN? http://osbot.org/forum/topic/57725-divine-utility/ it does both record path for us and you cna manually collect. Added support for area as well. On top of all that my script doesnt create a file but opens a window and allow the user to copy and paste

 

edit: my recording check the distance between current pos and last pos set. It will use a random distance every time.

Edited by josedpay
  • Like 1
Link to comment
Share on other sites


public class PathCollector extends BasicScript {

private LinkedList<Position> path;

private int granularity = 4;

@Override

public void initialize() {

path = new LinkedList<>();

}

@Override

public void loop() {

Position p = myPosition();

Position l = path.peekLast();

if(l == null || p.distance(l) > granularity) {

path.add(p);

}

}

@Override

public void onExit() throws InterruptedException {

StringBuilder positions = new StringBuilder();

positions.append("new Position[]{");

for (Position position : path) {

String sp = "new Position(" + position.getX() + ", " + position.getY() + ", " + position.getZ() + ") ,";

positions.append(sp);

}

positions.append("}");

StringSelection s = new StringSelection (positions.toString());

Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard ();

c.setContents(s, null);

log("Path copied to clipboard");

log(positions.toString());

}

}

  • Like 2
Link to comment
Share on other sites

why you no check SDN? http://osbot.org/forum/topic/57725-divine-utility/ it does both record path for us and you cna manually collect. Added support for area as well. On top of all that my script doesnt create a file but opens a window and allow the user to copy and paste

 

edit: my recording check the distance between current pos and last pos set. It will use a random distance every time.

 

I just tried it out it seems very nifty! "my recording checks the distance between current pos and last pos set. It will use a random distance every time",  is this not done by the walkPath method already? I assume it is since it automatically tries to get you back on path if it can't find the next tile

Link to comment
Share on other sites

I just tried it out it seems very nifty! "my recording checks the distance between current pos and last pos set. It will use a random distance every time",  is this not done by the walkPath method already? I assume it is since it automatically tries to get you back on path if it can't find the next tile

im talking about the recording. you walk around it records your path using random distance every time.

 

@Botre you forgot to add (currentPos != lastPos)

Link to comment
Share on other sites

im talking about the recording. you walk around it records your path using random distance every time.

 

@Botre you forgot to add (currentPos != lastPos)

 

I'm still a bit confused sorry, if you mean that it picks a random distance between which two tiles to record it seems a bit redundant as I see no point in that (Unless you want to save memory or something)  

Link to comment
Share on other sites

@Botre you forgot to add (currentPos != lastPos)

Position p = myPosition();
        Position l = path.peekLast();
        if(l == null || p.distance(l) > granularity) {
            path.add(p);
        }

A position is only added to a non-empty path if the player's current position is a specified distance away from the last added position. There's no need to check for equality (for which you would use the equals(Object o) but whatever) because my condition already excludes that (2 points that have a distance greater than zero between them can't be the same).

Edited by Botre
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...