Jump to content

Flawless path walking


Mr Asshole

Recommended Posts

import org.osbot.script.Script;
import org.osbot.script.rs2.map.Position;

/**
 * Created by Pain aka Exarticus
 * Credit me if you use
 * Walks path, randomizes path
 */
public class Walk {
    private Script script;

    public Walk(Script s){
        this.script = s;
    }

    public Position[] randomizePath(Position [] path, int maxX, int maxY){
        Position [] randomizedPath = new Position[path.length];
        for (int i = 0; i < path.length; i++){
            int x = path[i].getX();
            int y = path[i].getY();
            if (maxX > 0){
                double d = Math.random() * 2 - 1.0;
                d *= maxX;
                x += (int)d;
            }
            if (maxY > 0){
                double d = Math.random() * 2 -1.0;
                d *= maxY;
                y += (int)d;
            }
            randomizedPath[i] = new Position(x,y,path[i].getZ());
        }
        return randomizedPath;
    }

    public Position getNextPosition(Position [] path){
        int dist = 99; int closest = -1;
        for (int i = path.length - 1; i >= 0; i--){
            Position pos = path[i];
            int d = script.distance(pos);
            if (d < dist){
                dist = d;
                closest = i;
            }
        }
        if (closest == -1)return null;
        int index = -1;

        for (int i = closest; i < path.length; i++) {

            if (script.distance(path[i]) <= 16) {
                index = i;
            } else {
                break;
            }
        }

        if (index == -1) {
            return null;
        } else {
            return path[index];
        }
    }
    public void walkPath(Position[] path) throws InterruptedException {
        Position nextPosition = getNextPosition(path);
        if (nextPosition == null)script.log("Could not find path");
        if (nextPosition != null && script.canReach(nextPosition)){
            script.log("Walking to "+nextPosition.getX()+","+nextPosition.getY());
            script.walkMiniMap(nextPosition);
        }
        while (script.distance(nextPosition) > 5 || script.client.getMyPlayer().isMoving()){
            script.sleep(50);
        }


    }

}

How to use:

walker.walkPath(walker.randomizePath(toBank,2,2));
Position[] toBank = new Position[]{new Position(2611,3101,0),new Position(2612,3112,0),new Position(2604,3115,0)};
    
    public void onStart() {
        try {
            Walk walker = new Walk(this);
            startTime = System.currentTimeMillis();

        } catch (Exception e) {
            log("EXCEPTION"  + e.getStackTrace()[0]);
        }


    }
Edited by Mr Asshole
Link to comment
Share on other sites

You should have it handle Doors and Gates if you want it to stand out...there's already a path walker, and you can add randomization it in like 5 minutes.

Not sure what other path walker your talking about. This is easier as you can use cory's path grabber to grab the path array and then you can feed it as a parameter. The other path walkers use multi dimensional arrays which isn't practical.

Link to comment
Share on other sites

 

You should have it handle Doors and Gates if you want it to stand out...there's already a path walker, and you can add randomization it in like 5 minutes.

Not sure what other path walker your talking about. This is easier as you can use cory's path grabber to grab the path array and then you can feed it as a parameter. The other path walkers use multi dimensional arrays which isn't practical.

 

How is a multidimensional array not practical? Yours uses an array of Position objects. I'd say the multidimensional array is more practical in that it uses less memory because each "element" stores 2 ints rather than a Position object that is (slightly) larger than 2 ints. 

Link to comment
Share on other sites

I'm saying in terms of usability it's easier as you don't have to write the array.

You should have it handle Doors and Gates if you want it to stand out...there's already a path walker, and you can add randomization it in like 5 minutes.

Not sure what other path walker your talking about. This is easier as you can use cory's path grabber to grab the path array and then you can feed it as a parameter. The other path walkers use multi dimensional arrays which isn't practical.

How is a multidimensional array not practical? Yours uses an array of Position objects. I'd say the multidimensional array is more practical in that it uses less memory because each "element" stores 2 ints rather than a Position object that is (slightly) larger than 2 ints.
Link to comment
Share on other sites

Oh also, when using 

walker.walkPath(walker.randomizePath(toBank,2,2));

in your loop, you're effectively calling the randomizePath function every time you loop around. Not only is this a huge waste of time/resources, but it also leads to the path being less "random". As you randomize it many times per tile walked possibly, you'd get a distribution centered toward your actual tile, rather than closer to true random. It's more like gaussian random.

  • Like 1
Link to comment
Share on other sites

Oh also, when using

walker.walkPath(walker.randomizePath(toBank,2,2));
in your loop, you're effectively calling the randomizePath function every time you loop around. Not only is this a huge waste of time/resources, but it also leads to the path being less "random". As you randomize it many times per tile walked possibly, you'd get a distribution centered toward your actual tile, rather than closer to true random. It's more like gaussian random.
You could always call the randomize path method once in onStart.
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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