Jump to content

Flawless Path Walking


Divine

Recommended Posts

I don't know why you used a 2d array instead of an array of positions..

 

public boolean WalkAlongPath(Position[] path, boolean forward, int distanceFromEnd) {
		if (distanceToPoint(forward ? path[path.length - 1].getX() : path[0].getX(),
				forward ? path[path.length - 1].getY() : path[0].getY()) <= distanceFromEnd)
			return true;
		else {
			walkPath(path, forward);
			return false;
		}
	}

	public void walkPath(Position[] path, boolean forward) {
		int destination = 0;
		for (int i = 0; i < path.length; i++)
			if (distanceToPoint(path[i].getX(), path[i].getY()) < distanceToPoint(path[destination].getX(), path[destination].getY()))
				destination = i;
		if (script.client.getMyPlayer().isMoving() && distanceToPoint(path[destination].getX(), path[destination].getY()) > (script.isRunning() ? 3 : 2))
			return;
		if (forward && destination != path.length - 1 || !forward && destination != 0)
			destination += (forward ? 1 : -1);
		try {
			script.walk(new Position(path[destination].getX(), path[destination].getY(), 0));
			script.sleep(600);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private int distanceToPoint(int pointX, int pointY) {
		return (int) Math.sqrt(Math.pow(script.client.getMyPlayer().getX() - pointX, 2)
				+ Math.pow(script.client.getMyPlayer().getY() - pointY, 2));
	}

Also, not trying to hijack or anything.. but is there a position.isReachable() or position.canReach() ?

Edited by Kenneh
Link to comment
Share on other sites

I use something like this all the time, never had any trouble with it:

private void walkPath(ArrayList<Position> path, boolean reverse) throws InterruptedException {
    Position p = (reverse) ? getFirstReachablePosition(path) : getLastReachablePosition(path);

    if(p != null) {
        walk(p);
    } else {
        log("Path position is null.");
    }
}

private Position getLastReachablePosition(ArrayList<Position> path) {
    for (int i = path.size() - 1; i > 0; i--) {
        Position pos = path.get(i);
        if(canReach(pos)) {
            return pos;
        }
    }
    return null;
}

private Position getFirstReachablePosition(ArrayList<Position> path) {
    for (Position pos : path) {
        if(canReach(pos)) {
            return pos;
        }
    }
    return null;
}

I know the code could be a bit cleaner. :P But it works great and it is not that much code.

Link to comment
Share on other sites

I use something like this all the time, never had any trouble with it:

private void walkPath(ArrayList<Position> path, boolean reverse) throws InterruptedException {
    Position p = (reverse) ? getFirstReachablePosition(path) : getLastReachablePosition(path);

    if(p != null) {
        walk(p);
    } else {
        log("Path position is null.");
    }
}

private Position getLastReachablePosition(ArrayList<Position> path) {
    for (int i = path.size() - 1; i > 0; i--) {
        Position pos = path.get(i);
        if(canReach(pos)) {
            return pos;
        }
    }
    return null;
}

private Position getFirstReachablePosition(ArrayList<Position> path) {
    for (Position pos : path) {
        if(canReach(pos)) {
            return pos;
        }
    }
    return null;
}
I know the code could be a bit cleaner. tongue.png But it works great and it is not that much code.

Mind sharing that canReach? wink.png;)

Link to comment
Share on other sites

 

I use something like this all the time, never had any trouble with it:

private void walkPath(ArrayList<Position> path, boolean reverse) throws InterruptedException {
    Position p = (reverse) ? getFirstReachablePosition(path) : getLastReachablePosition(path);

    if(p != null) {
        walk(p);
    } else {
        log("Path position is null.");
    }
}

private Position getLastReachablePosition(ArrayList<Position> path) {
    for (int i = path.size() - 1; i > 0; i--) {
        Position pos = path.get(i);
        if(canReach(pos)) {
            return pos;
        }
    }
    return null;
}

private Position getFirstReachablePosition(ArrayList<Position> path) {
    for (Position pos : path) {
        if(canReach(pos)) {
            return pos;
        }
    }
    return null;
}
I know the code could be a bit cleaner. tongue.png But it works great and it is not that much code.

 

Mind sharing that canReach? wink.pngwink.png

 

it's in the MethodProvider here, mostly everything is in there happy.png

Link to comment
Share on other sites

I use something like this all the time, never had any trouble with it:

private void walkPath(ArrayList<Position> path, boolean reverse) throws InterruptedException {
    Position p = (reverse) ? getFirstReachablePosition(path) : getLastReachablePosition(path);

    if(p != null) {
        walk(p);
    } else {
        log("Path position is null.");
    }
}

private Position getLastReachablePosition(ArrayList<Position> path) {
    for (int i = path.size() - 1; i > 0; i--) {
        Position pos = path.get(i);
        if(canReach(pos)) {
            return pos;
        }
    }
    return null;
}

private Position getFirstReachablePosition(ArrayList<Position> path) {
    for (Position pos : path) {
        if(canReach(pos)) {
            return pos;
        }
    }
    return null;
}
I know the code could be a bit cleaner. tongue.png But it works great and it is not that much code.

Mind sharing that canReach? wink.png;)

it's in the MethodProvider here, mostly everything is in there happy.png

No idea how I didn't notice that before :/ maybe if the API wasn't such a clusterfuck with everything thrown in one class I'd be able to find things easier :s

Link to comment
Share on other sites

I use something like this all the time, never had any trouble with it:

private void walkPath(ArrayList<Position> path, boolean reverse) throws InterruptedException {
    Position p = (reverse) ? getFirstReachablePosition(path) : getLastReachablePosition(path);

    if(p != null) {
        walk(p);
    } else {
        log("Path position is null.");
    }
}

private Position getLastReachablePosition(ArrayList<Position> path) {
    for (int i = path.size() - 1; i > 0; i--) {
        Position pos = path.get(i);
        if(canReach(pos)) {
            return pos;
        }
    }
    return null;
}

private Position getFirstReachablePosition(ArrayList<Position> path) {
    for (Position pos : path) {
        if(canReach(pos)) {
            return pos;
        }
    }
    return null;
}

I know the code could be a bit cleaner. tongue.png But it works great and it is not that much code.

 

Not bad, but there's still a few issues here. It might be useful for the method to return a boolean so that the caller can know if the path is complete or not. Also, if the path isn't reversed it will never walk to the first position in the path because of the for loop condition (i > 0) should be (i >= 0).

 

I'm pretty sure the native osbot walking method supports simple paths like this anyway, but it might be problematic. I'm not sure. Good work either way.

Link to comment
Share on other sites

 

 

 

I use something like this all the time, never had any trouble with it:

private void walkPath(ArrayList<Position> path, boolean reverse) throws InterruptedException {
    Position p = (reverse) ? getFirstReachablePosition(path) : getLastReachablePosition(path);

    if(p != null) {
        walk(p);
    } else {
        log("Path position is null.");
    }
}

private Position getLastReachablePosition(ArrayList<Position> path) {
    for (int i = path.size() - 1; i > 0; i--) {
        Position pos = path.get(i);
        if(canReach(pos)) {
            return pos;
        }
    }
    return null;
}

private Position getFirstReachablePosition(ArrayList<Position> path) {
    for (Position pos : path) {
        if(canReach(pos)) {
            return pos;
        }
    }
    return null;
}
I know the code could be a bit cleaner. tongue.png But it works great and it is not that much code.

 

Mind sharing that canReach? wink.pngwink.png

 

it's in the MethodProvider here, mostly everything is in there happy.png

 

No idea how I didn't notice that before :/ maybe if the API wasn't such a clusterfuck with everything thrown in one class I'd be able to find things easier :s

 

mfw rsbot v1 all over again

Link to comment
Share on other sites

  • 3 weeks later...

Say if I were to use this in my script, I would have to declare this like so:

 

public class MyScript extends Script {

 

private int[][] path1 = new int[][] { { 3206, 3209 }, { 3215, 3211 }, { 3217, 3218 }, { 3225, 3218 },

            { 3235, 3220 }, { 3242, 3226 }, { 3252, 3226 }, { 3251, 3235 }, };

 

//

//

 

public void onStart() {

 

 

As a final int on top of my screen? Of course my own coordinates for the path, which I know how to get. And then I can later use it in my method which would be

//do the following

else {

//walk to bank

WalkAlongPath(path1, true);

 

Help would be greatly appreciated, I'm new to this. It's my first script hehe. I only need to be able to open a gate and walk.

Edited by Athylus
Link to comment
Share on other sites

 

you wouldnt put 

WalkAlongPath(path1, true);

 in your onStart(). You put it in your onLoop();

 

if (BANK_AREA.contains(client.getMyPlayer())) {
RS2Object bank_booth = closestObject(BANK_BOOTH_ID);
 
if (client.getBank().isOpen()) {
client.getBank().depositAll();
 
} else {
 
if (bank_booth != null) {
if (bank_booth.isVisible()) {
bank_booth.interact("Bank");
sleep(random(350, 750));
} else {
client.moveCameraToEntity(bank_booth);
}
}
}
 
} else {
// Walk to the bank area
WalkAlongPath(path1, true);

 

 

This is in my onloop. That's how I'm going to try it now. smile.png

 

There's one thing I don't get tho. Why do

 

final int[][] path1 = new int[][]

 

instead of

 

final int[] path1 = new int[]

 

Correct me if I'm wring, but [] means that's it an array of, in this case, integers. If so, then I'm learning fast tongue.png

 

I got it to work! Need to work on my loop though. When I'm in the bank it won't do anything... mhm. I'll figure it out tomorrow probaly. Going to smoke a joint now with a friend. This snippet is awesome, I don't get much of it though. Let alone come up with it. o.O 

 

Thanks a lot!

 

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