Jump to content

Need some help with my first script


KlaAz0r

Recommended Posts

Hey guys, today I started taking a look at script writing and I came across a few problems. My idea is to create a Lumbridge cow killer (just for learning) I get the idea of working in states en switches. But how do I make a path and walk stairs in them for example? path.WALK_TO_STAIR --> INTERACT.stairs --> path.WALK_TO_BANK or can I interact with objects in localWalker? the code below is just thrown together and not tested but I hope some one can tell me if this is going the right way

 
 
UPDATE:
So I worked out some things, the script checks if we are at the cow fields and then attacks the closest cow if it is not already in combat or the hit points are zero. but what should the script do while the player is in combat? 
package bot;

import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.api.model.Player;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

import java.awt.*;

@ScriptManifest(author = "KlaAz0r", info = "My first script", name = "Cow Killer", version = 0.01, logo = "")
public class main extends Script {

	@Override
	public void onStart() {
		log("Welcome to Simple Cow killer");

	}

	// all the states we can be in
	private enum State {
		GOING_DOWN_STAIRS, IDLE, IN_COMBAT, AT_COWS, ATTACKING_COW, AT_BANK, WALKING_FIELD_TO_STAIRS, GOING_UP_STAIRS, WALKING_TO_BANK, WALKING_BANK_TO_STAIRS, WALKING_STAIRS_TO_FIELDS, BANKING
	};

	// check if the player is at the bank
	public boolean atBank() {
		Player player = myPlayer();
		int x = player.getPosition().getX();
		int y = player.getPosition().getY();
		// NOT THE RIGHT COORDS YET!
		if ((x > 3426) && (x < 3431) && (y > 2889) && (y < 2894)) {
			return true;
		}

		return false;
	}

	// check if the player is at the fields
	public boolean atField() {
		Player player = myPlayer();
		int x = player.getPosition().getX();
		int y = player.getPosition().getY();

		if ((x > 3253) && (x < 3265) && (y > 3255) && (y < 3298)) {
			log("x : " + x + " y : " + y);
			log("we are at the fields!");
			return true;
		}
		log("x : " + x + " y : " + y);
		log("we are lost!");
		return false;
	}

	// checking if we are in combat
	public boolean inCombat() {
		if (myPlayer().isUnderAttack()) {
			log("we are in combat");
			return true;
		}
		log("we are not in combat");
		return false;
	}

	// if a cow is near, we wil attack it
	public void killingCow() {
		NPC cow = npcs.closest("Cow");
		if (cow != null && cow.getHealth() != 0 && cow.isAttackable()
				&& cow.getName().contains("Cow")) {
			if (map.canReach(cow)) {
				log("attacking cow!");
				cow.interact("Attack");
			}

		} else {
			log("no cow found!");
		}
	}

	// getting the state we are in
	private State getState() {
		// so if we are at the fields and are not in combat the state will be
		// ATTACKING_COW
		if (atField() && !inCombat())
			return State.ATTACKING_COW;

		// if we are under attack the state will go to IN_COMBAT
		if (myPlayer().isUnderAttack())
			return State.IN_COMBAT;

		// if we are at the fields and our inventory is full we should start
		// walking to the lumbridge stairs
		if (inventory.isFull() && atField())
			return State.WALKING_FIELD_TO_STAIRS;

		// if we are at the bank and our inventory is still full we should start
		// banking
		if (atBank() && inventory.isFull())
			return State.BANKING;

		// if we are at the bank and the inventory is empty we should start
		// making our way to the stairs again
		if (atBank() && inventory.isEmpty())
			return State.WALKING_BANK_TO_STAIRS;

		// error state if nothing
		return State.IDLE;
	}

	// all the walking paths NYI
	private Position[] fieldToStairs = { new Position(3254, 3421, 0),
			new Position(3256, 3428, 0), new Position(3264, 3428, 0),
			new Position(3273, 3428, 0), new Position(3277, 3426, 0),
			new Position(3281, 3422, 0) };

	private Position[] stairsToBank = { new Position(3254, 3421, 0),
			new Position(3256, 3428, 0), new Position(3264, 3428, 0),
			new Position(3273, 3428, 0), new Position(3277, 3426, 0),
			new Position(3281, 3422, 0) };

	private Position[] bankToStairs = { new Position(3254, 3421, 0),
			new Position(3256, 3428, 0), new Position(3264, 3428, 0),
			new Position(3273, 3428, 0), new Position(3277, 3426, 0),
			new Position(3281, 3422, 0) };

	private Position[] stairsToField = { new Position(3254, 3421, 0),
			new Position(3256, 3428, 0), new Position(3264, 3428, 0),
			new Position(3273, 3428, 0), new Position(3277, 3426, 0),
			new Position(3281, 3422, 0) };

	@Override
	public int onLoop() throws InterruptedException {
		switch (getState()) {
		case IN_COMBAT:
			// what should happen here?
			break;
		case ATTACKING_COW:
			log("start killing a cow");
			killingCow();
			break;

		case WALKING_FIELD_TO_STAIRS:
			localWalker.walkPath(fieldToStairs);
			break;
		case GOING_UP_STAIRS:
			// interacting with stairs
			break;
		case GOING_DOWN_STAIRS:
			// interacting with stairs
			break;
		case WALKING_TO_BANK:
			break;
		case WALKING_BANK_TO_STAIRS:
			localWalker.walkPath(bankToStairs);
			break;
		case WALKING_STAIRS_TO_FIELDS:
			localWalker.walkPath(stairsToField);
			break;
		case BANKING:
			RS2Object bankBooth = objects.closest("Bank booth");
			if (bankBooth != null) {
				if (bankBooth.interact("Bank")) {
					while (!bank.isOpen())
						sleep(250);
					// deposting items
					bank.depositAll();
				}
			} else {
				// error if the bank is not found
				log("error we could not find the bank?");
			}
			break;
		default:
			break;

		}
		return random(200, 300);
	}

	@Override
	public void onExit() {
		log("Thanks for running my Cow killer!");
	}

	@Override
	public void onPaint(Graphics2D g) {
		// NYI
	}

}

 

Edited by KlaAz0r
Link to comment
Share on other sites

Mkay lets start with what you have now.

 

npcs.closest("Cow").interact("Attack");

If there is no cow this will throw a NullPointerException. So what you want to do is

NPC cow = ...
if(cow != null)
  cow.interact....

But you're not checking if this particular cow is already under attack, dead or even reachable at all. And what if you're already attacking a cow?

I suggest you first write that part of the script, attacking a cow and waiting while in combat, then add looting and finally banking.


For banking, use bank.open() and never use a while loop like that.

If your current state is banking, you could check the distance to the closest "Stairs". If you want to bank and there is a stairs then "Climb-up" if myPosition().getZ() < 2 else bank.open()

Link to comment
Share on other sites

objects.closest("Staircase").interact("Climb-up")

 

 

Mkay lets start with what you have now.

 

npcs.closest("Cow").interact("Attack");

If there is no cow this will throw a NullPointerException. So what you want to do is

NPC cow = ...

if(cow != null)

  cow.interact....

But you're not checking if this particular cow is already under attack, dead or even reachable at all. And what if you're already attacking a cow?

I suggest you first write that part of the script, attacking a cow and waiting while in combat, then add looting and finally banking.

For banking, use bank.open() and never use a while loop like that.

If your current state is banking, you could check the distance to the closest "Stairs". If you want to bank and there is a stairs then "Climb-up" if myPosition().getZ() < 2 else bank.open()

 

Thank you very much, I de-compiled a few scripts and discovered how the combat system works.  Will work on this tomorrow and will post the final thing if it works :) 

Link to comment
Share on other sites

Mkay lets start with what you have now.

 

npcs.closest("Cow").interact("Attack");

If there is no cow this will throw a NullPointerException. So what you want to do is

NPC cow = ...

if(cow != null)

  cow.interact....

But you're not checking if this particular cow is already under attack, dead or even reachable at all. And what if you're already attacking a cow?

I suggest you first write that part of the script, attacking a cow and waiting while in combat, then add looting and finally banking.

For banking, use bank.open() and never use a while loop like that.

If your current state is banking, you could check the distance to the closest "Stairs". If you want to bank and there is a stairs then "Climb-up" if myPosition().getZ() < 2 else bank.open()

 

Just for reference, lumbridge castle is extremely fucky in the idea that the Z value does not change, I believe it is the Y value that changed (+3000 each time?)

 

  • Like 1
Link to comment
Share on other sites

case WALKING_TO_FIELDS:
			log("we should walk back to the fields!");
			localWalker.walkPath(fromBankToStairs);
			Player player = myPlayer();
			int x = player.getPosition().getX();
			int y = player.getPosition().getY();

			if ((x > 3205) && (x < 3207) && (y > 3209) && (y < 3211)) {
				log("x : " + x + " y : " + y);
				log("we are at the stairs!");
				objects.closest("Staircase").interact("Climb-down");
			
				int z = player.getPosition().getZ();
				if (z == 1) {
					objects.closest("Staircase").interact("Climb-down");
				}
			} else {
				log("we are lost again..");
			}

			break;

How do I walk to the stairs and wait till I am their before I interact with them? sometimes the script wants to go to the other stair 

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