Alright i need some help fellas. My walking file seems to be very inconsistent. webWalk usually does not work that well for example. Anyone see what I'm doing wrong?
import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.map.Area;
import java.util.List;
import org.osbot.rs07.script.MethodProvider;
public class MasterWalker extends MethodProvider {
private static final int WEB_WALK_TIMEOUT = 5000; // Increased timeout for longer paths
private static final int INTERACT_DISTANCE_THRESHOLD = 10; // Proximity threshold for target position
private static final int MAX_REGULAR_WALK_ATTEMPTS = 3; // Max attempts for regular walking before using webWalk
private static final int STEP_DIVISION_UNIT = 5; // Smaller step distances for finer control
// Overloaded walkTo method that accepts a list of waypoints
public boolean walkTo(Position destination, List<Position> waypoints, CheckInterrupt interrupt) throws InterruptedException {
// Check if the player is already at the destination
if (myPosition().equals(destination)) {
log("Player is already at the destination: " + destination);
return true;
}
for (Position waypoint : waypoints) {
if (divideAndWalk(waypoint, interrupt)) {
if (getWalking().webWalk(destination)) {
return true;
}
}
}
log("Fallback to direct webWalk to final destination.");
return webWalkWithTimeout(destination); // Fallback if waypoints don’t work
}
public boolean walkTo(Object destination, CheckInterrupt interrupt) throws InterruptedException {
checkRunEnergy();
if (destination instanceof Position) {
Position position = (Position) destination;
// Check if the player is already at the position
if (myPosition().equals(position)) {
log("Player is already at the target position: " + position);
return true;
}
if (canReach(position)) {
log("Position is reachable via regular walk.");
return divideAndWalk(position, interrupt);
} else {
log("Position appears unreachable via regular walk, attempting web walk as fallback.");
return webWalkWithTimeout(position);
}
} else if (destination instanceof Area) {
Area area = (Area) destination;
// Check if the player is already within the area
if (area.contains(myPosition())) {
log("Player is already within the target area: " + area);
return true;
}
Position randomPosition = area.getRandomPosition();
if (canReach(randomPosition)) {
log("Area is reachable via regular walk.");
return divideAndWalk(randomPosition, interrupt);
} else {
log("Area appears unreachable via regular walk, attempting web walk as fallback.");
return webWalkWithTimeout(randomPosition);
}
} else if (destination instanceof List) {
return handlePath((List<Position>) destination, interrupt);
}
return false; // Invalid input
}
private boolean divideAndWalk(Position target, CheckInterrupt interrupt) throws InterruptedException {
double distance = myPosition().distance(target);
int steps = (int) Math.ceil(distance / STEP_DIVISION_UNIT);
log("Calculated steps to target: " + steps);
Position currentPosition = myPosition();
for (int i = 0; i < steps; i++) {
checkRunEnergy();
if (interrupt.shouldInterrupt()) {
log("Interrupt condition met; stopping walk.");
return false;
}
Position stepTarget = getIntermediatePosition(currentPosition, target, STEP_DIVISION_UNIT);
log("Attempting regular walk to intermediate point: " + stepTarget);
if (!walkToPosition(stepTarget)) {
log("Failed to reach intermediate point: " + stepTarget);
return false;
}
currentPosition = stepTarget;
waitUntilStopped(3000);
}
log("Successfully divided and reached target position: " + target);
return true;
}
private Position getIntermediatePosition(Position from, Position to, int stepDistance) {
double distance = from.distance(to);
double ratio = stepDistance / distance;
int x = (int) (from.getX() + (to.getX() - from.getX()) * ratio);
int y = (int) (from.getY() + (to.getY() - from.getY()) * ratio);
return new Position(x, y, from.getZ());
}
private boolean walkToPosition(Position position) throws InterruptedException {
checkRunEnergy();
log("Attempting regular walk to: " + position);
if (getWalking().walk(position)) {
customSleep(5000);
return true;
}
log("Regular walking to " + position + " failed.");
return false;
}
private boolean webWalkWithTimeout(Position position) throws InterruptedException {
long startTime = System.currentTimeMillis();
log("Attempting webWalk to position: " + position);
while (System.currentTimeMillis() - startTime < WEB_WALK_TIMEOUT) {
if (getWalking().webWalk(position)) {
customSleep(10000);
return true;
}
log("Attempting to reach position: " + position + " - Time elapsed: " + (System.currentTimeMillis() - startTime) + "ms");
sleep(200);
}
log("Web walk to position timed out after " + WEB_WALK_TIMEOUT + "ms.");
return false;
}
private void checkRunEnergy() {
if (!getSettings().isRunning() && getSettings().getRunEnergy() > 15) {
getSettings().setRunning(true);
log("Run energy above 15%; enabling run mode.");
}
}
private void waitUntilStopped(int timeout) throws InterruptedException {
int timeWaited = 0;
while (myPlayer().isMoving() && timeWaited < timeout) {
sleep(100);
timeWaited += 100;
}
}
// Utility method to simulate custom sleep
private void customSleep(int timeout) throws InterruptedException {
int timeWaited = 0;
while (myPlayer().isMoving() && timeWaited < timeout) {
sleep(100);
timeWaited += 100;
}
}
private boolean handlePath(List<Position> path, CheckInterrupt interrupt) throws InterruptedException {
for (Position position : path) {
checkRunEnergy();
if (interrupt.shouldInterrupt()) {
log("Interrupt condition met; stopping path.");
return false;
}
if (!walkToPosition(position)) {
log("Failed to walk to position in path: " + position);
return false;
}
waitUntilStopped(3000);
}
return true;
}
public interface CheckInterrupt {
boolean shouldInterrupt();
}
private boolean canReach(Position position) {
return getMap().canReach(position);
}
private boolean canReach(Area area) {
for (Position pos : area.getPositions()) {
if (getMap().canReach(pos)) {
return true;
}
}
return false;
}
}