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