Jump to content

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


Apaec

Recommended Posts

any beginner tutorials on NPC fighting?

 

I started a basic script that will attack the closest cow if it is not under attack already but now i'm trying to figure out how to find the closest cow that is not already being attacked, maybe in a given area or find the closest n number of cows and pick the closest one that isnt already under attack. Currently if a cow is next to me being attacked it will always return that cow as being the closest, see that it is already under attack and then start over returning the same cow again.

Link to comment
Share on other sites

any beginner tutorials on NPC fighting?

 

I started a basic script that will attack the closest cow if it is not under attack already but now i'm trying to figure out how to find the closest cow that is not already being attacked, maybe in a given area or find the closest n number of cows and pick the closest one that isnt already under attack. Currently if a cow is next to me being attacked it will always return that cow as being the closest, see that it is already under attack and then start over returning the same cow again.

if(cow.getHealth()<8)//im assuming you named the npc variable for cow cow.
//basically this will check the cows health, and if its under 8 it won't attack it.
{
cow.interact("Attack");
}


you can add an extra check as well like if(cow.getHealth()<8&&!cow.isUnderAttack)

that way you can have 2 checks to see, if the cow doesn't have damage and if the cow is not underattack. if you want to add a third check, you can use a message listenter to get the phrase in the chatbox "NPC is already under attack" or whatever by doing

@Override
	public void onMessage(Message message) throws InterruptedException
	{
	if(message.getMessage().toString().contains("Already under attack."))//make sure this has the exact phrase, case sensetive and all
	{
	log("This cow is already under attack!");
	 //then have your character move somewhere else or have it do another action after it gets this.
	}
	}

I'm not the best with the osbot api/java in general but this should help a bit, apaec will probably be able to give you more advice though.

 

I misread your question my bad, gonna leave that there though incase anyone else is curious.

 

What you can do is make an area of the cowpen you're killing the cows in, and then you can do something like

Area COWPEN = new Area(top right x, top right y, bottom left x, bottom left y);
if(cow.isUnderAttack)
{
localWalker.walk(COWPEN.getRandomPosition(0));//will get a random area in the cow pen to run off to, then will kill cows that aren't under attack, or keep doing this until it finds one.
}

Defiantly better ways, but this is something that will work.

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

any beginner tutorials on NPC fighting?

 

I started a basic script that will attack the closest cow if it is not under attack already but now i'm trying to figure out how to find the closest cow that is not already being attacked, maybe in a given area or find the closest n number of cows and pick the closest one that isnt already under attack. Currently if a cow is next to me being attacked it will always return that cow as being the closest, see that it is already under attack and then start over returning the same cow again.

 

You can use a filter:

	public static NPC getClosestAliveNotUnderAttackForNameAndActionInArea(
			Script script, String name, String action, Area area) {
		NPC closest = null;
		double lowest = Double.MAX_VALUE;
		for (NPC npc : script.getNpcs().getAll()) {
			if (npc != null && npc.exists() && npc.getHealth() > 0
					&& !npc.isUnderAttack()) {
				final String npcName = npc.getName();
				final List<String> actions = Arrays.asList(npc.getDefinition()
						.getActions());
				final double d = npc.getPosition()
						.distance(script.myPosition());
				if (npcName.equalsIgnoreCase(name) && actions.contains(action)
						&& d < lowest && area.contains(npc)) {
					closest = npc;
					lowest = d;
				}
			}
		}
		return closest;
	}

Stick that method in your code, then define your cow as:

NPC cow = this.getClosestAliveNotUnderAttackForNameAndActionInArea(this, "Cow", "Attack", COW_AREA);

//where cow area is defined as eviltwin mentioned

Otherwise what twin said works, although the messagelistener isn't 100% reliable/clean it works too.

 

Apaec

Link to comment
Share on other sites

  • 2 weeks later...

Hey dude thanks a lot for you guide , just wondering i downloaded latest oracle for 32bit windows,

my default settings differ from yours, my execution environment is different.( CDC-1.1/Foundation1.1)

does this matter? should i match your settings or leave defult?

thanks!

Edited by Nightchillz
Link to comment
Share on other sites

I'm not sure what you call them, but I was wondering if there is a list of commands you can use or are they just whatever you name the function to be?

 

Like above, you have npc.getHealth. Is "getHealth" defined somewhere that the code knows what you're telling it to look for? Or did you just make that up and somehow it works?

 

I mostly write HTML and PHP, so this is new to me. Especially working with CSS files, I know you need to define your element before you can call upon it. I assume that the mainframe Osbot has is what "getHealth" refers to or checks?

 

Is there somewhere I can see all the possible commands or functions, or whatever you call them so that I can make something far more complicated than what you've explained.

 

Thanks!

Edited by gangstarasp
Link to comment
Share on other sites

I'm not sure what you call them, but I was wondering if there is a list of commands you can use or are they just whatever you name the function to be?

 

Like above, you have npc.getHealth. Is "getHealth" defined somewhere that the code knows what you're telling it to look for? Or did you just make that up and somehow it works?

 

I mostly write HTML and PHP, so this is new to me. Especially working with CSS files, I know you need to define your element before you can call upon it. I assume that the mainframe Osbot has is what "getHealth" refers to or checks?

 

Is there somewhere I can see all the possible commands or functions, or whatever you call them so that I can make something far more complicated than what you've explained.

 

Thanks!

http://osbot.org/api/

Link to comment
Share on other sites

I'm not sure what you call them, but I was wondering if there is a list of commands you can use or are they just whatever you name the function to be?

 

Like above, you have npc.getHealth. Is "getHealth" defined somewhere that the code knows what you're telling it to look for? Or did you just make that up and somehow it works?

 

I mostly write HTML and PHP, so this is new to me. Especially working with CSS files, I know you need to define your element before you can call upon it. I assume that the mainframe Osbot has is what "getHealth" refers to or checks?

 

Is there somewhere I can see all the possible commands or functions, or whatever you call them so that I can make something far more complicated than what you've explained.

 

Thanks!

 

Heya!

 

yes, osbot has an API found here: http://osbot.org/api/

 

GL!

Link to comment
Share on other sites

  • 2 weeks later...

hey Im new to scripting, so Im trying to do the simple things and see how each line of code works in game.

For example, I created a script using your script as a format, and I'm trying to make it Drop items. It shouldnt be a problem right? I mean, after all, I'm just removing the STEAL state right?

 

Well I tried using your guide to help me with this and its not doing anything? Can you please tell me what I'm doing wrong? Like, I made my inventory full of items, but nothing is working? I even tried to modify your Code for stealing to interact with a bank booth and use the "Collect" option and nothing happened. (I know theres an API for banking, but shouldnt it still work?) I used a Log output to see where the script is going to see when it stops and such, im not a total noob tongue.png

 

EDIT: So apparently My script doesn't run the onLoop? Does the OSBot API not support these methods anymore? I added a log output for the onLoop and I haven't received any text. I only receive a log output when I add it to the onStart & onExit Method.

EDIT2: EVERYTHING IS FINE NOW, I GOT IT WORKING, ALL I DID WAS CREATE A NEW PROJECT AND COPY THE CODE smile.png

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

 

hey Im new to scripting, so Im trying to do the simple things and see how each line of code works in game.

For example, I created a script using your script as a format, and I'm trying to make it Drop items. It shouldnt be a problem right? I mean, after all, I'm just removing the STEAL state right?

 

Well I tried using your guide to help me with this and its not doing anything? Can you please tell me what I'm doing wrong? Like, I made my inventory full of items, but nothing is working? I even tried to modify your Code for stealing to interact with a bank booth and use the "Collect" option and nothing happened. (I know theres an API for banking, but shouldnt it still work?) I used a Log output to see where the script is going to see when it stops and such, im not a total noob tongue.png

 

EDIT: So apparently My script doesn't run the onLoop? Does the OSBot API not support these methods anymore? I added a log output for the onLoop and I haven't received any text. I only receive a log output when I add it to the onStart & onExit Method.

EDIT2: EVERYTHING IS FINE NOW, I GOT IT WORKING, ALL I DID WAS CREATE A NEW PROJECT AND COPY THE CODE smile.png

FIXED

 

Glad you got it sorted :)

 

If you were wondering, an even simpler way would just be to put this.inventory.dropAll() in your onloop straight up and just get rid of the getstate enum and method. 

 

Sorry I was a bit late with the response, was sleepin

Apa

Link to comment
Share on other sites

Glad you got it sorted smile.png

 

If you were wondering, an even simpler way would just be to put this.inventory.dropAll() in your onloop straight up and just get rid of the getstate enum and method. 

 

Sorry I was a bit late with the response, was sleepin

Apa

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 :)

 

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 :)

 

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 :)

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