Jump to content

Pandemic's Scripting Series: Part II - Path Walking and Simple Banking [UPDATED FOR OSBOT 2]


Pandemic

Recommended Posts

Awesome in-depth guide, thank you.

 

No problem :)

 

 

I'm trying to make my script walk from the mine back to the bank and it's acting weird. I have this for the positions:

private Position[] path = {
		     new Position(3289, 3371, 0),
		     new Position(3290, 3374, 0),
		     new Position(3294, 3386, 0),
		     new Position(3290, 3391, 0),
		     new Position(3291, 3401, 0),
		     new Position(3289, 3406, 0),
		     new Position(3286, 3418, 0),
		     new Position(3276, 3426, 0),
		     new Position(3264, 3429, 0),
		     new Position(3254, 3421, 0)
	};

I manually put them in so I don't know why it's acting odd. Once it gets to the 3rd position it clicks back  towards the mine then starts walking South. Any idea what's causing this?

 

Here is my whole script for reference.


import java.awt.Graphics;


import org.osbot.script.Script;
import org.osbot.script.ScriptManifest;
import org.osbot.script.mouse.MinimapTileDestination;
import org.osbot.script.rs2.map.Position;
import org.osbot.script.rs2.model.RS2Object;
import org.osbot.script.rs2.utility.Area;
@ScriptManifest(author ="Lingoling", info="Mines Tin in Varrock east. Start in bank or at mine.", name = "BasicMiner", version = 0)

public class BasicMiner extends Script {
	
	private static final int[] TIN_ID = {11636,11634, 11635};
	
	
	private enum State {
		MINE, WALK_TO_BANK, BANK, WALK_TO_MINE};
	
	private State getState() {
		if (client.getInventory().isFull() && MINE_AREA.contains(myPlayer()))
			return State.WALK_TO_BANK;
		if (!client.getInventory().isFull() && BANK_AREA.contains(myPlayer()))
			return State.WALK_TO_MINE;
		if (client.getInventory().isFull() && BANK_AREA.contains(myPlayer()))
			return State.BANK;
		return State.MINE;
	}
	private Position[] path = {
		     new Position(3289, 3371, 0),
		     new Position(3290, 3374, 0),
		     new Position(3294, 3386, 0),
		     new Position(3290, 3391, 0),
		     new Position(3291, 3401, 0),
		     new Position(3289, 3406, 0),
		     new Position(3286, 3418, 0),
		     new Position(3276, 3426, 0),
		     new Position(3264, 3429, 0),
		     new Position(3254, 3421, 0)
	};
	private static final Area MINE_AREA = new Area(3277, 3358, 3293, 3371);
    private static final Area BANK_AREA = new Area(3250, 3419, 3257, 3423);
    
    private void traversePath(Position[] path, boolean reversed) throws InterruptedException {
    	if (!reversed) {
    		for (int i = 1; i < path.length; i++)
    			if (!walkTile(path[i]))
    				i--;
    	} else {
    		for (int i = path.length-2; i > 0; i--)
    			if (!walkTile(path[i]))
    				i++;
    	}
    } 
    private boolean walkTile(Position p) throws InterruptedException {
    	client.moveMouse(new MinimapTileDestination(bot, p), false);
    	sleep(random(150, 250));
    	client.pressMouse();
    	int failsafe = 0;
    	while (failsafe < 10 && myPlayer().getPosition().distance(p) > 2) {
    		sleep(200);
    		failsafe++;
    		if (myPlayer().isMoving())
    			failsafe = 0;
    	}
    	if (failsafe == 10)
    		return false;
    	return true;
    }
	@Override
	public void onStart() {
		log("I like women but they don't like me, and it's hard, yes siree");
	}
	@Override
	public int onLoop() throws InterruptedException {
		switch (getState()) {
		case MINE:
			if (!myPlayer().isAnimating()) {
				RS2Object vein = closestObject(TIN_ID);
				if (vein != null) {
					if (vein.interact("Mine"))
						sleep(random(1000, 1500));
				}
			}
		break;
		case WALK_TO_BANK:
			traversePath(path, false);
			sleep(random(1500, 2500));
		break;
		case WALK_TO_MINE:
			traversePath(path, true);
			sleep(random(1500, 2500));
		break;
		case BANK:
			RS2Object bank = closestObjectForName("Bank booth");
			if (bank != null) {
				if (bank.interact("Bank")) {
					while (!client.getBank().isOpen())
						sleep(250);
					client.getBank().depositAll();
				}
			}
		break;
		}
		return random(200, 300);
	}
	@Override
	public void onExit() {
		log("Thanks ********a.");
		
	}
	@Override
	public void onPaint(Graphics g) {
		

	}
}

 

I'm not too sure, did you change the areas at all, and double check your path.

Link to comment
Share on other sites

while (failsafe < 10 && myPlayer().getPosition().distance(p) > 2) {
	sleep(200);
	if (!myPlayer().isMoving())
		failsafe++;
}
No need to set it to 0 every time you move. Once you start moving, you won't stop unless you get interupted or something (causing the while loop to fail anyway).

Considering how many times this portion of the code is run O(n^2), this should be taught in the tutorial to at least have code efficiency running through their minds while coding

Edited by dreamliner
Link to comment
Share on other sites

while (failsafe < 10 && myPlayer().getPosition().distance(p) > 2) {		sleep(200);		if (!myPlayer().isMoving())			failsafe++;	}
What's this for?

Mine resets your failsafe if your character is actually moving, so a couple hundred ms of lag over the span of a few seconds won't kick you out of the loop, it'll basically restart it.

Edited by Pandemic
Link to comment
Share on other sites

What's this for?

Mine resets your failsafe if your character is actually moving, so a couple hundred ms of lag over the span of a few seconds won't kick you out of the loop, it'll basically restart it.

No need to set it to 0 every time you move. Once you start moving, you won't stop unless you get interrupted or something (causing the while loop to fail anyway).

Considering how many times this portion of the code is run O(n^2), this should be taught in the tutorial to at least have code efficiency running through their minds while coding

Also, if if kicks you out of the loop, a normal player would click again anyway. I've never once in my RS play time not try to click again after 2 seconds of lag

Link to comment
Share on other sites

No need to set it to 0 every time you move. Once you start moving, you won't stop unless you get interrupted or something (causing the while loop to fail anyway).

Considering how many times this portion of the code is run O(n^2), this should be taught in the tutorial to at least have code efficiency running through their minds while coding

Also, if if kicks you out of the loop, a normal player would click again anyway. I've never once in my RS play time not try to click again after 2 seconds of lag

Mine will click again after 2 seconds of lag, it's yours that won't. Your version will return false if there is a TOTAL of 2 seconds of lag, not 2 seconds of straight lag.

Link to comment
Share on other sites

No problem :)

I'm not too sure, did you change the areas at all, and double check your path.

I didn't change any of the areas, and I've experienced this problem using the positions you provided and custom ones. I've been told that it's a problem with the API though. It runs for hours on end and randomly starts going off the path. I've also experienced this in other scripts too.
Link to comment
Share on other sites

  • 2 weeks later...

No point in creating static variables that are not going to be used at the class level in any other classes, especially if they are inside a class that's parent is Script. Also, it's probably a good idea to be consistent with formatting, especially when making a tutorial (I am referring to your braces). It would be different if this was black box code, or something of the like.

 

Your walking method looks to be written poorly with static sleeps not accounting for movements or lag. I don't know how the walk method inside the api works, but you're explanation of it "tending to click until it's at the destination" can easily be remedied by not calling the method over and over if the current game destination is the tile you wanted to walk to.

 

Another thing, you may want to check if the object you're trying to click is on the screen before clicking it as that could cause problems.

 

Lastly, you could be stuck in an infinite loop with that banking method you've written. Instead, why not use a conditional sleep that times out after x seconds if the clients open method returns true?

Link to comment
Share on other sites

No point in creating static variables that are not going to be used at the class level in any other classes, especially if they are inside a class that's parent is Script. Also, it's probably a good idea to be consistent with formatting, especially when making a tutorial (I am referring to your braces). It would be different if this was black box code, or something of the like.

 

Your walking method looks to be written poorly with static sleeps not accounting for movements or lag. I don't know how the walk method inside the api works, but you're explanation of it "tending to click until it's at the destination" can easily be remedied by not calling the method over and over if the current game destination is the tile you wanted to walk to.

 

Another thing, you may want to check if the object you're trying to click is on the screen before clicking it as that could cause problems.

 

Lastly, you could be stuck in an infinite loop with that banking method you've written. Instead, why not use a conditional sleep that times out after x seconds if the clients open method returns true?

 

By static variables I'll assume you're talking about my constants, and they're not strictly needed, but they make it easier to look at.

 

Where are my braces not consistent? I'm not seeing it :o

 

My walking method is far from perfect, this is a tutorial for learning purposes, so I'm not going to write some sophisticated walking method that the new scripters here won't understand.

 

The API's walking will keep clicking the same spot (not because I'm calling it 100 times, you only have to call it once and it just spam clicks the position).

 

There's no need to check if the object is on screen, because the interact function handles that.

 

I agree it could get in an infinite loop, but once again, this is just a beginners tutorial so it'll work for learning purposes.

 

Thanks for reading.

Link to comment
Share on other sites

By static variables I'll assume you're talking about my constants, and they're not strictly needed, but they make it easier to look at.

 

Where are my braces not consistent? I'm not seeing it ohmy.png

 

My walking method is far from perfect, this is a tutorial for learning purposes, so I'm not going to write some sophisticated walking method that the new scripters here won't understand.

 

The API's walking will keep clicking the same spot (not because I'm calling it 100 times, you only have to call it once and it just spam clicks the position).

 

There's no need to check if the object is on screen, because the interact function handles that.

 

I agree it could get in an infinite loop, but once again, this is just a beginners tutorial so it'll work for learning purposes.

 

Thanks for reading.

Omitting braces with only one line inside the statement is ok... but explain why you do that, otherwise people looking at the tutorial will be confused. Sometimes you did it with more than one line, though.

I promise you that the walking method in the api does not recurse itself until the destination is reached, therefore you are able to easily fix it by doing what I stated above.

Lastly, if this is a tutorial, make sure the code is presentable and functional with no logic errors or syntax errors. The entire point of a tutorial is to teach people how to code correctly.

Edited by Sigma
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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