Jump to content

Path Walking [OSB2]


Pug

Recommended Posts

needed this and @Pandemic was kind enough to donate his. So in the interest of the community and for new scriptwriters to learn the art of walking in there script which is pretty pivotal im posting an example here so everyone can use it in the future of OSB2.

 

feel free to post improvements.

import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.util.LocalPathFinder;
import org.osbot.rs07.input.mouse.MiniMapTileDestination;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.Area;

@ScriptManifest(name="name", author="author", info="info", logo="", version=0.1D)
public final class walker
  extends Script
{
	// area declarations
	 private static final Area vWestBank = new Area(3186, 3433, 3180, 3437);
	 private static final Area vEastBank = new Area(3256,3420,3250,3423);
	 
	 // walking path ( walks from varrock west to varrock east bank
	 private final Position[] pathToWalk = 
		 { 
			 new Position(3182, 3436,0), 
			 new Position(3189, 3430,0), 
			 new Position(3200, 3429,0), 
			 new Position(3211, 3428,0), 
			 new Position(3223, 3429,0), 
			 new Position(3233, 3430,0),
			 new Position(3244, 3429,0),
			 new Position(3254, 3428,0),
			 new Position(3254, 3420,0)
		 };
	  
	 
	@Override
	public int onLoop() throws InterruptedException {

		if(!vEastBank.contains(myPlayer()))
		{
			log("not in east bank, walking");
			walkPath(pathToWalk);
                }
		
		if(vEastBank.contains(myPlayer()))
		{
			log("made it!");
			stop();
		}
		
		
		return 300;
	}
	
	
	
	//walking methods
	public void walkPath(Position[] path) throws InterruptedException {
	    for (Position p : path) {
	        if (myPosition().distance(p) > 16 || myPosition().distance(p) < 3) continue;
	        boolean success;
	        do {
	            success = walkTile(p);
	        } while (!success);
	    }
	}

	public boolean walkTile(Position p) throws InterruptedException {
	    if (myPosition().distance(p) > 13) {
	        Position pos = new Position(((p.getX() + myPosition().getX()) / 2) + random(-3, 3),
	                ((p.getY() + myPosition().getY()) / 2) + random(-3, 3),
	                myPosition().getZ());
	        walkTile(pos);
	    }
	    mouse.click(new MiniMapTileDestination(bot, p), false);
	    int fail = 0;
	    while (myPosition().distance(p) > 2 && fail < 10) {
	        sleep(500);
	        if (!myPlayer().isMoving())
	            fail++;
	    }
	    return fail != 10;
	}

	
}

Edited by Pug
Link to comment
Share on other sites

Personally I just use this if I am walking any normal paths without any obstacles:
The walk method is simply walking a MinimapTileDestination.

The code simply finds the best tile (It requires that the destination is at the end of the array, you can always reverse it yourself smile.png), then if it manages to click the tile and the destination is somewhat close to the tile you wanted to walk, it will sleep until you are 5 tiles away from it (You can always randomize it)

	public boolean walkPath(Position[] path) {
		for (int i = path.length - 1; i >= 0; i--) {
			final Position pos = path[i];
			if (pos != null && pos.distance(ctx.myPosition()) <= 16 && ctx.getMap().canReach(pos)) {
				if (walk(pos) && ctx.getMap().getDestination().distance(pos) <= 2) {
					ctx.sleep(new Condition() {

						@Override
						public boolean evaluate() {
							return pos.distance(ctx.myPosition()) > 5;
						}
						
					}, 3500);
					return true;
				} else {
					return false;
				}
			}
		}
		return false;
	}

You would also need some sort of conditional sleeping method for this to work.

 

You can also create a new instance of the LocalPathFinder which will allow you to generate a path between two points using A* (I am pretty sure its using A*)

Link to comment
Share on other sites

Personally I just use this if I am walking any normal paths without any obstacles:

The walk method is simply walking a MinimapTileDestination.

The code simply finds the best tile (It requires that the destination is at the end of the array, you can always reverse it yourself smile.png), then if it manages to click the tile and the destination is somewhat close to the tile you wanted to walk, it will sleep until you are 5 tiles away from it (You can always randomize it)

	public boolean walkPath(Position[] path) {
		for (int i = path.length - 1; i >= 0; i--) {
			final Position pos = path[i];
			if (pos != null && pos.distance(ctx.myPosition()) <= 16 && ctx.getMap().canReach(pos)) {
				if (walk(pos) && ctx.getMap().getDestination().distance(pos) <= 2) {
					ctx.sleep(new Condition() {

						@Override
						public boolean evaluate() {
							return pos.distance(ctx.myPosition()) > 5;
						}
						
					}, 3500);
					return true;
				} else {
					return false;
				}
			}
		}
		return false;
	}

You would also need some sort of conditional sleeping method for this to work.

 

You can also create a new instance of the LocalPathFinder which will allow you to generate a path between two points using A* (I am pretty sure its using A*)

 

LocalPathFinder throws a NPE and crashes my script.

Link to comment
Share on other sites

For those of u using this pathwalking and experience obstacles, to handle doors you would use this snippet to walk to the obstacle in mind (albeit a door or gate or w/ever), then you would check if the door is open (for example check its actions for a specific action eg check if the doors right click contains 'open' and that way you know it's closed. You would then, if the door is closed, interact open with the door and proceed to commence your next path.

Link to comment
Share on other sites

For those of u using this pathwalking and experience obstacles, to handle doors you would use this snippet to walk to the obstacle in mind (albeit a door or gate or w/ever), then you would check if the door is open (for example check its actions for a specific action eg check if the doors right click contains 'open' and that way you know it's closed. You would then, if the door is closed, interact open with the door and proceed to commence your next path.

 

that would work, but what if the door closes behind you? you would be spamming the door. but that can be easily fixed ^_^

 

Link to comment
Share on other sites

Meh it's nice but kind of useless if it cannot handle obstacles :/

 

if useless means then it cant be used to walk from A to B across 50% of runescape without encountering an obstacle then yes you are right lol. 99% of my scripts need this. It can also be adapted very easily to handle any obstacle when in range. But for those just starting out with a script that needs to make it walk, its just the job ;)

  • Like 1
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...