Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Walking handler - Automated obstacle handling

Featured Replies

Walking handler - Automated obstacle handling

 

 

For my AI project I've worked out a general movement handler.

One that can handle obstacles on it path and overcome height changes (e.g. when going to lumbridge bank)

 

 

 

Use it yourself, code in the spoiler:

Movement.java

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.event.WalkingEvent;
import org.osbot.rs07.script.Script;

/**
 * 
 * @author randqm
 * 
 * @info Handles movement for a bot while handling possible obstacles on the way.
 * 
 * @version 1.0 General system worked out.
 * @version 1.1 Fixed infinite loop on failed obstacle handling.
 * @version 1.2 Added stairs handling
 *
 */

public class Movement {
	
	/* Whether we log debugging messages or not. */
	private static final boolean DEBUG = false;
	
	/* The current path index when walking a path. */
	private static int pathIndex = -1;
	
	/* The position to go to when walking to an area. */
	private static Position areaPosition;
	
	/* Whether we're using a straight walking event for movement or not. */
	private static boolean usingWalkingEvent;
	
	
	/**
	 * Walks a bot to a given position.
	 * 
	 * @param script The script.
	 * 
	 * @param position The position to walk to.
	 * 
	 * @return Whether the position has been reached or not.
	 */
	public static boolean walk(Script script, Position position) {
		if (StairHandler.handleStair(script, position)) {
			if (handleObstacles(script, position)) {
				return (moveToPosition(script, position);
			}
		}
		return false;
	}
	
	/**
	 * Walks the bot to a random position in a given area.
	 * 
	 * @param script The script.
	 * 
	 * @param area The area to move to.
	 * 
	 * @return Whether the area has been reached or not.
	 */
	public static boolean walk(Script script, Area area) {
		if (areaPosition == null) {
			areaPosition = area.getRandomPosition();
		}
		if (walk(script, areaPosition)) {
			areaPosition = null;
			return true;
		}
		return false;
	}
	
	/**
	 * Makes the bot walk a specified path.
	 * 
	 * @param script The script.
	 * 
	 * @param path The path to walk.
	 * 
	 * @return Whether the end of the path has been reached or not.
	 */
	public static boolean walk(Script script, Position[] path) {
		if (pathIndex == -1) {
			int closest = -1;
			
			for (int i = 0; i < path.length; i ++) {
				int distance = path[i].distance(script.myPosition());
					
				if (closest == -1 || distance < closest) {
					closest = distance;
					pathIndex = i;
				}
			}
		}
		if (pathIndex == path.length) {
			pathIndex = -1;
			return true;
		}
		if (walk(script, path[pathIndex])) {
			pathIndex ++;
		}
		return false;
	}
	
	/**
	 * Handles any possible obstacles required to reach our destination.
	 * 
	 * @param script The script.
	 * 
	 * @param position The position to reach.
	 * 
	 * @return Whether the obstacles have been handled or not.
	 */
	private static boolean handleObstacles(Script script, Position position) {
		if (script.getMap().canReach(position)
				&& script.myPosition().getZ() == position.getZ()) {
			return true;
		}
		RS2Object obstacle = script.getDoorHandler().getNextObstacle(position);
		 
		if (obstacle == null) {
			return true;
		}
		if (!script.getDoorHandler().canReachOrOpen(position)) {
			log(script, "Unable to reach obstacle " + obstacle.getName() + " at" + obstacle.getPosition());
			return true;
		}
		if (moveToPosition(script, obstacle.getPosition())) {
			log(script, "Reached obstacle " + obstacle.getName() + " at " + obstacle.getPosition());
			
			if (script.getDoorHandler().handleNextObstacle(position)) {
				log(script, "Handled obstacle " + obstacle.getName() + " at " + obstacle.getPosition());
				return true;
			}
			log(script, "Failed to handle obstacle " + obstacle.getName() + " at" + obstacle.getPosition());
		}
		return false;
	}
	
	/**
	 * Moves the bot to a given position.
	 * 
	 * @param script The script.
	 * 
	 * @param position The position to move to.
	 * 
	 * @return Whether the position has been reached or not.
	 */
	public static boolean moveToPosition(Script script, Position position) {
		if (!script.myPlayer().isMoving()) {
			//TODO: Figure out a better way than distance since web walker goes way to random.
			if (script.myPosition().distance(position) < 2) {
				log(script, "Reached: " + position);
				usingWalkingEvent = false;
				return true;
			}
			if (!script.getWalking().webWalk(position)) {
				log(script, "Unable to web walk, attempting regular walking.");
				if (!script.getWalking().walk(position)) {
					if (usingWalkingEvent) {
						log(script, "Failed to move to: " + position);
						usingWalkingEvent = false;
						return true;
					}
					WalkingEvent walkingPath = new WalkingEvent(position);
					walkingPath.setMiniMapDistanceThreshold(0);
					walkingPath.setMinDistanceThreshold(0);
					script.execute(walkingPath);
					usingWalkingEvent = true;
					return false;
				}
			}
		}
		return false;
	}
	
	/**
	 * Logs a message specific for the movement class.
	 * 
	 * @param script The script.
	 * 
	 * @param message The logged message.
	 */
	private static void log(Script script, String message) {
		if (DEBUG) {
			script.log("[Movement]: " + message);
		}
	}

}
 

StairHandler.java

package rqtasker.movement;

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;

/**
 * 
 * @author randqm
 * 
 * @info Handles stairs for a bot while moving to a position.
 *
 */

public class StairHandler {
	
	/* Whether we log debugging messages or not. */
	private static final boolean DEBUG = true;
	
	/* The stairs to handle. */
	private static RS2Object stairs;
	
	/* The stair choice to use. */
	private static StairChoice choice;
	
	
	/**
	 * Attempts to handle stairs if the target position has
	 * a different plane than the bot and stairs are found.
	 * 
	 * @param script The script.
	 * 
	 * @param position The target position.
	 * 
	 * @return Whether stairs have been handled or not.
	 */
	public static boolean handleStair(Script script, Position position) {
		log(script, position.getZ() + " " + script.myPosition().getZ());
		if (position.getZ() == script.myPosition().getZ()) {
			stairs = null;
			return true;
		}
		if (stairs == null) {
			stairs = searchStairs(script, position);
					
			if (stairs == null) {
				log(script, "No stairs were found for: " + position);
				return true;
			}
			return false;
		}
		if (Movement.moveToPosition(script, stairs.getPosition())) {
			if (!stairs.interact(choice.getAction())) {
				log(script, "Failed to handle stairs at " + position);
				stairs = null;
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Searches the stairs for a position.
	 * 
	 * @param script The script.
	 * 
	 * @param position The position.
	 * 
	 * @return The stairs.
	 */
	private static RS2Object searchStairs(Script script, Position position) {
		int posX = position.getX();
		int posY = position.getY();
		int playerZ = script.myPosition().getZ();
		choice = position.getZ() > playerZ ? StairChoice.UP : StairChoice.DOWN;
		RS2Object found = null;
		
		Area area = new Area(new Position(posX + 15, posY + 15, playerZ)
		, new Position(posX - 15, posY - 15, playerZ));
		area.setPlane(playerZ);
		
		for (RS2Object obj : script.getObjects().getAll()) {
			if (obj.getPosition().getZ() == playerZ) {
				int deltaX = obj.getPosition().getX() - script.myPosition().getX();
				int deltaY = obj.getPosition().getY() - script.myPosition().getY();
				
				if (deltaX < 15 && deltaX > -15 && deltaY < 15 && deltaY > -15) {
					if (obj.hasAction(choice.getAction())) {
						found = obj;
						break;
					}
				}
			}
		}
		return found;
	}
	
	/**
	 * Logs a message specific for the movement class.
	 * 
	 * @param script The script.
	 * 
	 * @param message The logged message.
	 */
	private static void log(Script script, String message) {
		if (DEBUG) {
			script.log("[StairHandler]: " + message);
		}
	}

}

StairChoice.java

/**
 * 
 * @author randqm
 * 
 * @info Represents available choices when handling stairs.
 *
 */

public enum StairChoice {
	
	UP("Climb-up"),
	DOWN("Climb-down");
	

	/* The action on the stairs. */
	private final String action;
	
	
	/**
	 * Creates a new stair choice.
	 * 
	 * @param action The action on the stairs.
	 */
	private StairChoice(String action) {
		this.action = action;
	}
	
	
	/**
	 * Retrieves the action on the stairs.
	 * 
	 * @return The action.
	 */
	public String getAction() {
		return action;
	}
	
}
 

 

 

Sample usage code: (lumbridge bank position)

if (Movement.walk(this, new Position(3208, 3219, 2))) {
    log("Im there");
    //Dostuff
}

Handling stairs:

giphy.gif

 

Handling doors/gates:

giphy.gif

 

 

 

 

This has only been tested in a few areas by myself, if you have any suggestions on improving the system or/and found bugs don't hesitate to let me know or post your code. Thanks

Edited by lisabe96

  • Author

StairHandler.java has been updated, fixing going down on stairs.

If you use this code, update your stairhandler to the new version:

 

package rqtasker.movement;

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;

/**
 * 
 * @author randqm
 * 
 * @info Handles stairs for a bot while moving to a position.
 *
 */

public class StairHandler {
	
	/* Whether we log debugging messages or not. */
	private static final boolean DEBUG = true;
	
	/* The stairs to handle. */
	private static RS2Object stairs;
	
	/* The stair choice to use. */
	private static StairChoice choice;
	
	
	/**
	 * Attempts to handle stairs if the target position has
	 * a different plane than the bot and stairs are found.
	 * 
	 * @param script The script.
	 * 
	 * @param position The target position.
	 * 
	 * @return Whether stairs have been handled or not.
	 */
	public static boolean handleStair(Script script, Position position) {
		log(script, position.getZ() + " " + script.myPosition().getZ());
		if (position.getZ() == script.myPosition().getZ()) {
			stairs = null;
			return true;
		}
		if (stairs == null) {
			stairs = searchStairs(script, position);
					
			if (stairs == null) {
				log(script, "No stairs were found for: " + position);
				return true;
			}
			return false;
		}
		if (Movement.moveToPosition(script, stairs.getPosition())) {
			if (!stairs.interact(choice.getAction())) {
				log(script, "Failed to handle stairs at " + position);
				stairs = null;
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Searches the stairs for a position.
	 * 
	 * @param script The script.
	 * 
	 * @param position The position.
	 * 
	 * @return The stairs.
	 */
	private static RS2Object searchStairs(Script script, Position position) {
		int posX = position.getX();
		int posY = position.getY();
		int playerZ = script.myPosition().getZ();
		choice = position.getZ() > playerZ ? StairChoice.UP : StairChoice.DOWN;
		RS2Object found = null;
		
		Area area = new Area(new Position(posX + 15, posY + 15, playerZ)
		, new Position(posX - 15, posY - 15, playerZ));
		area.setPlane(playerZ);
		
		for (RS2Object obj : script.getObjects().getAll()) {
			if (obj.getPosition().getZ() == playerZ) {
				int deltaX = obj.getPosition().getX() - script.myPosition().getX();
				int deltaY = obj.getPosition().getY() - script.myPosition().getY();
				
				if (deltaX < 15 && deltaX > -15 && deltaY < 15 && deltaY > -15) {
					if (obj.hasAction(choice.getAction())) {
						found = obj;
						break;
					}
				}
			}
		}
		return found;
	}
	
	/**
	 * Logs a message specific for the movement class.
	 * 
	 * @param script The script.
	 * 
	 * @param message The logged message.
	 */
	private static void log(Script script, String message) {
		if (DEBUG) {
			script.log("[StairHandler]: " + message);
		}
	}

}

  • 9 months later...


if (handleObstacles(script, position)) {

return (moveToPosition(script, position);

}

This line is missing a ) btw :)

if (handleObstacles(script, position)) {
				return (moveToPosition(script, position);
			}

This line is missing a ) btw 

 

Web walking already handles obstacles, so this code is pretty redundant anyway

Edited by Explv

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.