Jump to content

A Beginners Guide to Writing OSBot Scripts (where to get started!) by Apaec


Apaec

Recommended Posts

True, but i was just checking out how the code works and all. I REALLLYY like how random the mouse movements are for this API, it really makes it more human like smile.png

 

And Thank You very much for replying! im glad to see that people are willing to help others on here! This is just what i like, friendly people helping others with their code smile.png

 

If you could help me with my current scripting problem then that would be nice:

 

Basically the script I am creating requires for the user to be at 1 of 2 different locations for a while, then walk to a bank. How would I format this to properly see my distance from these spaces, and randomly select one of the two positions, and walk to it?

 

i have looked at this guide: http://osbot.org/forum/topic/60536-tutorial-how-to-walk-a-path-in-one-line-of-code/

 

but i dont know how to make it randomly go to 1 of 2 locations (both locations are near/diagonal from each other)

 

thank you smile.png

 

Hey!

 

Not so sure what you mean. Do you mean, like my rock crabs script, that you want there to be a 'random' option where the script would walk to either west or east rock crabs based on a random returned boolean or similar?

 

If so, it's not that simple. But ultimately, you need to record two different paths going from the bank, eg BANK_TO_NORTH, BANK_TO_SOUTH

 

then, when the code comes up to walk to the area, just do

switch(random(1,2)) {
case 1:
localWalker.walkPath(BANK_TO_NORTH);
//any additional code here
break;
case 2:
localWalker.walkPath(BANK_TO_NORTH);
//any additional code here
break;
}

Or something similar. Remember, that will only start walking. You need do find some method of telling the script to continue walking that path if for whatever reason it stops. A good but somewhat looked down on method for doing this is usng flags. There's no real problem with doing this so i suggest you do :)

 

So you would just put a boolean set to true after you call the walkPath method, and then just in your getState ask if that same boolean is true, if so walk north, otherwise walk south etc. remember to reset the boolean when you get to the desired area though!

 

Let me know if i'm talking about the wrong thing or you have any other problems. 

Apaec :)

  • Like 1
Link to comment
Share on other sites

Hey!

 

Not so sure what you mean. Do you mean, like my rock crabs script, that you want there to be a 'random' option where the script would walk to either west or east rock crabs based on a random returned boolean or similar?

 

If so, it's not that simple. But ultimately, you need to record two different paths going from the bank, eg BANK_TO_NORTH, BANK_TO_SOUTH

 

then, when the code comes up to walk to the area, just do

switch(random(1,2)) {
case 1:
localWalker.walkPath(BANK_TO_NORTH);
//any additional code here
break;
case 2:
localWalker.walkPath(BANK_TO_NORTH);
//any additional code here
break;
}

Or something similar. Remember, that will only start walking. You need do find some method of telling the script to continue walking that path if for whatever reason it stops. A good but somewhat looked down on method for doing this is usng flags. There's no real problem with doing this so i suggest you do smile.png

 

So you would just put a boolean set to true after you call the walkPath method, and then just in your getState ask if that same boolean is true, if so walk north, otherwise walk south etc. remember to reset the boolean when you get to the desired area though!

 

Let me know if i'm talking about the wrong thing or you have any other problems. 

Apaec smile.png

Thank you soo much! this really helps!!! :D

Also, wouldnt putting down the same coords for a walk path make the bot more detectable? maybe I should set an integer on random (limited to the number of paths i have) and depending on the value, it will randomly select the 1st coordinate or whichever path it chooses, then select the 2nd coord from these paths, etc.

 

im trying to implement a very good antiban to make it more human like. would making the coordinates within an array list help me out more, and is that even possible? Please let me know!

Anyways, thank you so much already, ill make sure to return the favor for anyone who has any questions in the future! :)

Link to comment
Share on other sites

Thank you soo much! this really helps!!! biggrin.png

Also, wouldnt putting down the same coords for a walk path make the bot more detectable? maybe I should set an integer on random (limited to the number of paths i have) and depending on the value, it will randomly select the 1st coordinate or whichever path it chooses, then select the 2nd coord from these paths, etc.

 

im trying to implement a very good antiban to make it more human like. would making the coordinates within an array list help me out more, and is that even possible? Please let me know!

Anyways, thank you so much already, ill make sure to return the favor for anyone who has any questions in the future! smile.png

 

To walk a path you would first define a posiion array:

 

Position[] path = new Position[]{new Position(0,0,0), new Position(0,0,0), ...};

 

Either way, adding more paths it could use would not help (at all) with making the bot less detectable / humanlike! Just stick to one path. If anything, imo, it may even make the script more detectable.

 

Apaec

Link to comment
Share on other sites

Aint it redundant to use the same if-statement inside the STEAL case, that you use to get the state. 

 

Since the if-statement is used to get the state, should it not be sufficient to just write something like this :

case STEAL:
    Entity stall = objects.closest("Tea stall");
    stall.interact("Steal-from");
    break;
Edited by Sir
Link to comment
Share on other sites

To walk a path you would first define a posiion array:

 

Position[] path = new Position[]{new Position(0,0,0), new Position(0,0,0), ...};

 

Either way, adding more paths it could use would not help (at all) with making the bot less detectable / humanlike! Just stick to one path. If anything, imo, it may even make the script more detectable.

 

Apaec

oh okay!

but why exactly would it make it more detectable? im sorry if im asking asking too many questions, im just curious about is because it will help me when creating my script and expanding my knowledge.

it just seems like the morn random/human like a bot is, the less chance of has of being detected. after all, it seems like if it isnt random then it will be more noticeable

Link to comment
Share on other sites

 

Aint it redundant to use the same if-statement inside the STEAL case that you use to get the state. 

 

Since the if-statement is used to get the state, should it not be sufficient to just write something like this :

case STEAL:
    Entity stall = objects.closest("Tea stall");
    stall.interact("Steal-from");
    break;

 

You're actually defining two seperate instances of stall in the code. What you name as stall in the case may not necessarily be what you name as stall in the getState. For this reason, you need to add a nullcheck in otherwise you will be recieving frequent NPEs.

 

Generally it's a good idea to nullcheck anything you define before interacting with it though. Even if you consider it unnecessary, it's always an added level of check which can impact the stability of your script :)

 

Apa

  • Like 1
Link to comment
Share on other sites

 

Aint it redundant to use the same if-statement inside the STEAL case that you use to get the state. 

 

Since the if-statement is used to get the state, should it not be sufficient to just write something like this :

case STEAL:
    Entity stall = objects.closest("Tea stall");
    stall.interact("Steal-from");
    break;

im pretty sure the if statements help it from messing up because if the conditions that weren't met the second time for some reason(some error or something happened that you have no control over) then it wouldnt do anything and prevent a critical error from occurring

Link to comment
Share on other sites

Thank you soo much! this really helps!!! biggrin.png

Also, wouldnt putting down the same coords for a walk path make the bot more detectable? maybe I should set an integer on random (limited to the number of paths i have) and depending on the value, it will randomly select the 1st coordinate or whichever path it chooses, then select the 2nd coord from these paths, etc.

 

im trying to implement a very good antiban to make it more human like. would making the coordinates within an array list help me out more, and is that even possible? Please let me know!

Anyways, thank you so much already, ill make sure to return the favor for anyone who has any questions in the future! smile.png

 

Now to start, I don't code (yet...) but wouldn't it be easier to have the bot use the minimap to travel? Then, have it click randomly within a small circle that gets it going in the right direction?

 

For example, clicking randomly inside the red circle in this picture.

post-157873-0-82933800-1427800248_thumb.png

 

I get it's heading into a bank, but the same concept could be used for walking around, and has the added benefit of being more human like for actually using the minimap like a real person would.

Link to comment
Share on other sites

Now to start, I don't code (yet...) but wouldn't it be easier to have the bot use the minimap to travel? Then, have it click randomly within a small circle that gets it going in the right direction?

 

For example, clicking randomly inside the red circle in this picture.

attachicon.gifmap.png

 

I get it's heading into a bank, but the same concept could be used for walking around, and has the added benefit of being more human like for actually using the minimap like a real person would.

 

That will work but you may well have problems with the reachability of some areas. The walkPath method in the localWalker api will use the minimap as well as the screen depending on the distance it needs to move (i believe). The clicking isn't always accurate to the exact tile either, so its pretty good at looking human like c:

 

apa

Link to comment
Share on other sites

  • 4 weeks later...

Heya Apaec,

Thanks for your guide, because of this i'm actually looking into making my own scripts.

I'm actually trying to make a simple Shrimp powerfisher (catch the shrimp and drop all once inv is full, EXCEPT the fishing net)
I'm struggling trying to drop everything BUT the fishing net.

Could you help me out?

Code i have right now:

 

import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
 
import java.awt.*;
 
@ScriptManifest(author = "Labyrinth", info = "PowerFisher", name = "Simple Fisher", version = 0.1, logo = "https://bloggermymaze.files.wordpress.com/2010/12/mymaze_2010_tudor_labyrinth.jpg")
public class main extends Script {
    
    @Override
    public void onStart() {
        log("Let's catch some fish!");
        log("If you encounter any bugs or errors please report them on the forums");
        log("Goodluck gaining!");
    }
   
    private enum State {
		FISH, DROP, WAIT
	};

	private State getState() {
		Entity FS = objects.closest("Fishing spot");
		if (!inventory.isEmpty())
			return State.DROP;
		if (FS != null)
			return State.FISH;
		return State.WAIT;
	}

	@Override
	public int onLoop() throws InterruptedException {
		switch (getState()) {
		case FISH:
			Entity FS = objects.closest("Fishing spot");
			if (FS!= null) {
				FS.interact("Net-from");
			}
			break;
		case DROP:
			inventory.dropAll();
			break;
		case WAIT:
			sleep(random(500, 1300));
			break;
		}
		return random(300, 900);
	}
	
 
    @Override
    public void onExit() {
        log("Thanks for running my SimpleFisher!");
    }
 
    @Override
    public void onPaint(Graphics2D g) {
 
    }
 
}
  • Like 1
Link to comment
Share on other sites

Heya Apaec,

Thanks for your guide, because of this i'm actually looking into making my own scripts.

I'm actually trying to make a simple Shrimp powerfisher (catch the shrimp and drop all once inv is full, EXCEPT the fishing net)

I'm struggling trying to drop everything BUT the fishing net.

Could you help me out?

Code i have right now:

import org.osbot.rs07.api.model.Entity;import org.osbot.rs07.script.Script;import org.osbot.rs07.script.ScriptManifest; import java.awt.*; @ScriptManifest(author = "Labyrinth", info = "PowerFisher", name = "Simple Fisher", version = 0.1, logo = "https://bloggermymaze.files.wordpress.com/2010/12/mymaze_2010_tudor_labyrinth.jpg")public class main extends Script {        @Override    public void onStart() {        log("Let's catch some fish!");        log("If you encounter any bugs or errors please report them on the forums");        log("Goodluck gaining!");    }       private enum State {		FISH, DROP, WAIT	};	private State getState() {		Entity FS = objects.closest("Fishing spot");		if (!inventory.isEmpty())			return State.DROP;		if (FS != null)			return State.FISH;		return State.WAIT;	}	@Override	public int onLoop() throws InterruptedException {		switch (getState()) {		case FISH:			Entity FS = objects.closest("Fishing spot");			if (FS!= null) {				FS.interact("Net-from");			}			break;		case DROP:			inventory.dropAll();			break;		case WAIT:			sleep(random(500, 1300));			break;		}		return random(300, 900);	}	     @Override    public void onExit() {        log("Thanks for running my SimpleFisher!");    }     @Override    public void onPaint(Graphics2D g) {     } } 
I'm sure Apaec won't mind me lending a hand ;)

Have a browse through the methods for inventory here: http://osbot.org/api/org/osbot/rs07/api/Inventory.html

You'll see in the api you can do: inventory.dropAllExpect(NAME HERE);

hope this helped.

Link to comment
Share on other sites

So thanks to some help from Precise I mentioned to make a little progress : The script now knows not to drop the net and starts fishing.
I'm still running into a problem --> It keeps spam clicking to fish (since I haven't given it a 'confirmation' on whether it is fishing or not).

Could you help me to make the script detect wether I am fishing or not, which prevents it from spam clicking the fishing option?

My Code so far :

 

import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
 
import java.awt.*;
 
@ScriptManifest(author = "Labyrinth", info = "PowerFisher", name = "Simple Fisher", version = 0.1, logo = "http://imgur.com/bXr0Is1")
public class main extends Script {
    
    @Override
    public void onStart() {
        log("Let's catch some fish!");
        log("If you encounter any bugs or errors please report them on the forums");
        log("Goodluck gaining!");
    }
   
    private enum State {
		FISH, DROP, WAIT
	};

	private State getState() {
		Entity FS = npcs.closest("Fishing spot");
		if (!inventory.isEmptyExcept("Small fishing net"))
			return State.DROP;
		if (FS != null)
			return State.FISH;
		return State.WAIT;
	}

	@Override
	public int onLoop() throws InterruptedException {
		log(getState());
	 switch(getState()) {
		case FISH:
			Entity FS = npcs.closest("Fishing spot");
			log("Fishing spot null?: " + (FS == null));
			if (FS!= null) {
			log("Fishing spot has Net action: " + FS.hasAction("Net"));
			FS.interact("Net");
			}
			break;
		case DROP:
			inventory.dropAllExcept("Small Fishing net");
			break;
		case WAIT:
			sleep(random(500, 1300));
			break;
		}
		return random(300, 900);
	}
	
 
    @Override
    public void onExit() {
        log("Thanks for running my SimpleFisher!");
    }
 
    @Override
    public void onPaint(Graphics2D g) {
 
    }
 
}
Edited by ragfr00b
Link to comment
Share on other sites

 

So thanks to some help from Precise I mentioned to make a little progress : The script now knows not to drop the net and starts fishing.

I'm still running into a problem --> It keeps spam clicking to fish (since I haven't given it a 'confirmation' on whether it is fishing or not).

Could you help me to make the script detect wether I am fishing or not, which prevents it from spam clicking the fishing option?

My Code so far :

 

 

Replace your getState() method with this

private State getState() {
		Entity FS = npcs.closest("Fishing spot");
		if (!inventory.isEmptyExcept("Small fishing net"))
			return State.DROP;
		if (FS != null && !myPlayer.isAnimating() && !myPlayer.isMoving())
			return State.FISH;
		return State.WAIT;
	}
Link to comment
Share on other sites

  • Alek unpinned this topic

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...