Jump to content

Result of walking to position is returning true on failure


Recommended Posts

Posted (edited)

I'm writing a sand crab script and before I run to the correct position to begin afking, I want to walk to an area first. I run to the area fine, but when I try and walk to the specific position, I get stuck in an infinite loop as getWalking().walk(desiredPosition) is falsely returning true. It always leaves me about 2-3 tiles away.

private Area currentArea;
private Position currentSpot;

...

private void takeSpot() {
  currentArea = spotManager.freeArea();
  currentSpot = spotManager.freeSpot();

  log("Found a spot at " + currentSpot.toString());
  log("Taking spot ...");

  while(!currentArea.contains(myPlayer())) {
    log("Moving to area ...");
    getWalking().walk(currentArea);
    new Sleep(() -> myPlayer().isMoving(), 6000).sleep();
    sleep(200);
  }
  log("Moved to area!");

  log("Current position: " + myPlayer().getPosition().toString());
  log("Desired position: " + currentSpot.toString());

  while(myPlayer().getPosition() != currentSpot) {
    log("Moving to position");
    log("Current position: " + myPlayer().getPosition().toString());
    log("Desired position: " + currentSpot.toString());
    log("Result of attempting to walk to position is: " + getWalking().walk(currentSpot));

    getWalking().walk(currentSpot);
    new Sleep(() -> !myPlayer().isMoving(), 3000).sleep();
  }
}

Example outputs of logs:

[INFO][Bot #1][11/30 07:41:28 PM]: Moving to position
[INFO][Bot #1][11/30 07:41:28 PM]: Current position: [x=1696, y=3470, z=0]
[INFO][Bot #1][11/30 07:41:28 PM]: Desired position: [x=1695, y=3471, z=0]
[INFO][Bot #1][11/30 07:41:28 PM]: Result of attempting to walk to position is: true
[INFO][Bot #1][11/30 07:41:28 PM]: Moving to position
[INFO][Bot #1][11/30 07:41:28 PM]: Current position: [x=1696, y=3470, z=0]
[INFO][Bot #1][11/30 07:41:28 PM]: Desired position: [x=1695, y=3471, z=0]
[INFO][Bot #1][11/30 07:41:28 PM]: Result of attempting to walk to position is: true
[INFO][Bot #1][11/30 07:41:28 PM]: Moving to position
[INFO][Bot #1][11/30 07:41:28 PM]: Current position: [x=1696, y=3470, z=0]
[INFO][Bot #1][11/30 07:41:28 PM]: Desired position: [x=1695, y=3471, z=0]
[INFO][Bot #1][11/30 07:41:28 PM]: Result of attempting to walk to position is: true

It says that the call to getWalking().walk(currentSpot) is true, however I'm still 2-3 squares away. I can move into the area containing this spot fine, but get stuck in an infinite loop in my second while loop.

I'm only using a static sleep(200) for debugging, as the client crashes otherwise.

I'm new to scripting and a Java nooblet, so any help is welcomed 

Edited by R I F T
Posted (edited)
getWalking().walk(Position p);

Has a minimum distance threshold greater than zero, so if you’re within the threshold (I’m not 100% what it is) the event is considered successful.

You need a custom walk event. 

WalkingEvent myWalk = new WalkingEvent(Position p); 

myWalk.setMinDistanceThreshold(0);

execute(myWalk);

Also you should restructure to avoid while() loops. 

Edited by jca
  • Like 3
Posted
17 minutes ago, jca said:

getWalking().walk(Position p) has a minimum distance threshold greater than zero, so if you’re within the threshold (I’m not 100% what it is, the event considered successful.

You need a custom walk event. 

WalkingEvent myWalk = new WalkingEvent(Position p); 

myWalk.setMinDistanceThreshold(0);

execute(myWalk); 

Easy fix. Thanks so much!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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