- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Point;
- import java.awt.RenderingHints;
- import java.util.logging.Level;
- import org.rsbot.event.listeners.PaintListener;
- import org.rsbot.script.Script;
- import org.rsbot.script.ScriptManifest;
- import org.rsbot.script.methods.Game;
- import org.rsbot.script.util.Filter;
- import org.rsbot.script.wrappers.RSArea;
- import org.rsbot.script.wrappers.RSGroundItem;
- import org.rsbot.script.wrappers.RSItem;
- import org.rsbot.script.wrappers.RSNPC;
- import org.rsbot.script.wrappers.RSObject;
- import org.rsbot.script.wrappers.RSPlayer;
- import org.rsbot.script.wrappers.RSTile;
- @ScriptManifest(authors = { "Pervy Shuya, Fusion89k" }, name = "AutoCowKiller", keywords = "Combat", version = 1.35, description = ("Kills cows at at any location"))
- public class AIOCows extends Script implements PaintListener {
- private final char beefyDir = 's', craftingDir = 's', farmingDir = 'n',
- lumbyDir = 'e';
- private final int beefyGate = 45206, craftingGate = 1553,
- farmingGate = 7050, lumbyGate = 45210, wait1 = 150, wait2 = 200,
- hideID = 1739, tolerance = 6;
- private final String[] cows = { "Cow", "Cow calf" };
- private final RSArea beefyArea = new RSArea(3152, 3315, 3210, 3343),
- craftingArea = new RSArea(new RSTile(2914, 3267), new RSTile(2938,
- 3291)), farmingArea = new RSArea(new RSTile(3021, 3297),
- new RSTile(3043, 3313)), lumbyArea = new RSArea(new RSTile(
- 3253, 3255), new RSTile(3266, 3299));
- private final RSTile atStair = new RSTile(3205, 3229);
- RSNPC beefyBill;
- private final RSTile[] beefyPath = { new RSTile(3206, 3228),
- new RSTile(3219, 3218), new RSTile(3234, 3226),
- new RSTile(3224, 3237), new RSTile(3218, 3250),
- new RSTile(3216, 3263), new RSTile(3214, 3277),
- new RSTile(3199, 3279), new RSTile(3190, 3290),
- new RSTile(3187, 3305), new RSTile(3176, 3314) }, craftingPath = {
- new RSTile(3012, 3356), new RSTile(3007, 3341),
- new RSTile(3007, 3325), new RSTile(2998, 3314),
- new RSTile(2986, 3304), new RSTile(2971, 3299),
- new RSTile(2958, 3299), new RSTile(2944, 3300),
- new RSTile(2929, 3297), new RSTile(2922, 3293) }, farmingPath = {
- new RSTile(3012, 3356), new RSTile(3007, 3341),
- new RSTile(3007, 3325), new RSTile(3021, 3319),
- new RSTile(3032, 3315) }, lumbyPath = { new RSTile(3206, 3228),
- new RSTile(3216, 3219), new RSTile(3228, 3219),
- new RSTile(3234, 3226), new RSTile(3226, 3237),
- new RSTile(3229, 3248), new RSTile(3235, 3261),
- new RSTile(3246, 3262), new RSTile(3251, 3267) };
- private final int[] foodID = { 1895, 1893, 1891, 4293, 2142, 291, 2140,
- 3228, 9980, 7223, 6297, 6293, 6295, 6299, 7521, 9988, 7228, 2878,
- 7568, 2343, 1861, 13433, 315, 325, 319, 3144, 347, 355, 333, 339,
- 351, 329, 3381, 361, 10136, 5003, 379, 365, 373, 7946, 385, 397,
- 391, 3369, 3371, 3373, 2309, 2325, 2333, 2327, 2331, 2323, 2335,
- 7178, 7180, 7188, 7190, 7198, 7200, 7208, 7210, 7218, 7220, 2003,
- 2011, 2289, 2291, 2293, 2295, 2297, 2299, 2301, 2303, 1891, 1893,
- 1895, 1897, 1899, 1901, 7072, 7062, 7078, 7064, 7084, 7082, 7066,
- 7068, 1942, 6701, 6703, 7054, 6705, 7056, 7060, 2130, 1985, 1993,
- 1989, 1978, 5763, 5765, 1913, 5747, 1905, 5739, 1909, 5743, 1907,
- 1911, 5745, 2955, 5749, 5751, 5753, 5755, 5757, 5759, 5761, 2084,
- 2034, 2048, 2036, 2217, 2213, 2205, 2209, 2054, 2040, 2080, 2277,
- 2225, 2255, 2221, 2253, 2219, 2281, 2227, 2223, 2191, 2233, 2092,
- 2032, 2074, 2030, 2281, 2235, 2064, 2028, 2187, 2185, 2229, 6883,
- 1971, 4608, 1883, 1885, 15266 };
- private char dir;
- private RSNPC cow;
- private int bankID = 11758, gate, count, oldCount = -1, runRandom = random(
- 30, 60);
- private RSArea area;
- private RSTile[] path;
- private String doing = "Starting Up", where;
- private long startTime;
- private boolean bankAgain;
- private enum ACTION {
- TOBANK, FROMBANK, GATE, ATTACK, BANK, CLIMBUP, CLIMBDOWN, BANKBEEFY
- };
- @Override
- public boolean onStart() {
- getNearest();
- startTime = System.currentTimeMillis();
- return true;
- }
- @Override
- public void onFinish() {
- }
- @Override
- public int loop() {
- beefyBill = npcs.getNearest(246);
- cow = npcs.getNearest(new Filter<RSNPC>() {
- @Override
- public boolean accept(RSNPC npc) {
- for (String name : cows)
- if (npc.getName().equals(name) && !npc.isInCombat())
- return true;
- return false;
- }
- });
- camera.setPitch(true);
- if (getMyPlayer().isMoving()) {
- if (area.contains(getMyPlayer().getLocation()))
- return random(wait1, wait2);
- else if (isMoving())
- return random(wait1, wait2);
- }
- if (path == null)
- getNearest();
- switch (getAction()) {
- case TOBANK:
- walk(true);
- break;
- case FROMBANK:
- walk(false);
- break;
- case GATE:
- if (objects.getNearest(gate) != null
- && objects.getNearest(gate).doAction("Open")) {
- if (objects.getNearest(gate) == null)
- walking.walkTileMM(area.getCentralTile());
- } else {
- walking.walkTileMM(area.getCentralTile());
- }
- return random(1400, 1600);
- case BANKBEEFY:
- if (bankAgain) {
- if (inventory.contains(hideID)) {
- if (beefyBill == null) {
- walking.walkTileMM(walking.getClosestTileOnMap(area
- .getCentralTile()));
- } else if (beefyBill != null && beefyBill.isOnScreen()) {
- if (interfaces.get(236).isValid()) {
- interfaces.getComponent(236, 1).doClick();
- bankAgain = false;
- } else {
- useItem(inventory.getItem(hideID), beefyBill);
- }
- } else {
- walking.walkTileMM(beefyBill.getLocation());
- }
- }
- } else {
- if (inventory.isFull()) {
- if (beefyBill == null) {
- walking.walkTileMM(walking.getClosestTileOnMap(area
- .getCentralTile()));
- } else if (beefyBill != null && beefyBill.isOnScreen()) {
- if (interfaces.get(451).isValid()) {
- interfaces.getComponent(451, 1).doClick();
- bankAgain = true;
- } else {
- useItem(inventory.getItem(hideID), beefyBill);
- }
- } else {
- walking.walkTileMM(beefyBill.getLocation());
- }
- }
- }
- return random(1400, 1600);
- case BANK:
- if (objects.getNearest(bankID) != null
- && objects.getNearest(bankID).isOnScreen()) {
- if (!bank.isOpen()) {
- doing = "Opening Bank";
- objects.getNearest(bankID).doAction("Use-quickly");
- } else {
- doing = "Depositing";
- bank.depositAll();
- }
- } else {
- if (!getMyPlayer().isMoving())
- walking.walkTileMM(objects.getNearest(bankID).getLocation());
- }
- return random(1400, 1600);
- case ATTACK:
- if (peopleCheck()) {
- switchPlace();
- } else {
- if (getMyPlayer().getInteracting() == null
- && getNearestLootInArea(area, hideID) != null) {
- hideCheck();
- return random(1500, 1700);
- }
- while (getMyPlayer().getInteracting() instanceof RSNPC
- || getMyPlayer().isInCombat()) {
- }
- attack();
- }
- return random(1400, 1800);
- case CLIMBUP:
- doing = "Climbing Up";
- switch (game.getPlane()) {
- case 0:
- atTile(new RSTile(atStair.getX() - 1, atStair.getY()),
- "Climb-up");
- return random(1400, 1800);
- case 1:
- atTile(atStair, "Climb-up");
- return random(1400, 1800);
- }
- break;
- case CLIMBDOWN:
- doing = "Climbing Down";
- atTile(atStair, "Climb-down");
- return random(1400, 1800);
- }
- return random(wait1, wait2);
- }
- private ACTION getAction() {
- if (bankAgain) {
- return ACTION.BANKBEEFY;
- }
- if (where.equals("Beefy Bill")) {
- if (inventory.isFull()) {
- return ACTION.BANKBEEFY;
- }
- }
- if (inventory.isFull() && !where.equals("Beefy Bill")) {
- if (calc.distanceTo(path[0]) > 4) {
- if (game.getPlane() == 2)
- return ACTION.BANK;
- if (area.contains(getMyPlayer().getLocation())) {
- if (gateCheck())
- return ACTION.GATE;
- else
- return ACTION.FROMBANK;
- } else
- return ACTION.TOBANK;
- } else if (dir == lumbyDir && game.getPlane() != 2)
- return ACTION.CLIMBUP;
- return ACTION.BANK;
- } else {
- if (calc.distanceTo(path[path.length - 1]) > 4
- && !area.contains(getMyPlayer().getLocation())) {
- if (game.getPlane() != 0)
- return ACTION.CLIMBDOWN;
- else
- return ACTION.FROMBANK;
- } else {
- if (!area.contains(getMyPlayer().getLocation()))
- return ACTION.GATE;
- else
- return ACTION.ATTACK;
- }
- }
- }
- private boolean attack() {
- runCheck();
- if (getMyPlayer().getInteracting() != null
- || getMyPlayer().isInteractingWithLocalPlayer())
- return false;
- if (combat.getLifePoints() < combat.getLifePoints() / random(3, 5))
- return inventory.getItem(foodID).doAction("Eat");
- doing = "Finding Cow";
- if (cow != null && getNearestLootInArea(area, hideID) == null) {
- if (cow.isOnScreen() && clickNPC(cow, "Attack")) {
- doing = "Attacking Cow";
- return true;
- } else {
- if (area.contains(cow.getLocation()) && !cow.isOnScreen())
- walking.walkTileMM(cow.getLocation());
- return false;
- }
- } else {
- hideCheck();
- }
- return false;
- }
- private boolean hideCheck() {
- doing = "Picking up hides";
- RSGroundItem hide = groundItems.getNearest(hideID);
- if (hide != null && area.contains(hide.getLocation())) {
- if (calc.tileOnScreen(hide.getLocation())) {
- if (getMyPlayer().getInteracting() == null)
- hide.doAction("Take Cowhide");
- return true;
- } else if (!calc.tileOnScreen(hide.getLocation())) {
- walking.walkTo(hide.getLocation());
- return false;
- }
- }
- return false;
- }
- @SuppressWarnings("unused")
- private boolean findLootItem(final int range, final int ids) {
- int myX = getMyPlayer().getLocation().getX();
- int myY = getMyPlayer().getLocation().getY();
- int minX = myX - range;
- int minY = myY - range;
- int maxX = myX + range;
- int maxY = myY + range;
- for (int x = minX; x <= maxX; x++) {
- for (int y = minY; y <= maxY; y++) {
- final RSGroundItem[] items = groundItems.getAllAt(x, y);
- for (final RSGroundItem item : items) {
- int itemID = item.getItem().getID();
- if (itemID == ids)
- return true;
- }
- }
- }
- return false;
- }
- private void getNearest() {
- if (game.isLoggedIn()) {
- int farm = calc.distanceTo(farmingPath[farmingPath.length - 1]), craft = calc
- .distanceTo(craftingPath[craftingPath.length - 1]), lumby = calc
- .distanceTo(lumbyPath[lumbyPath.length - 1]), beefy = calc
- .distanceTo(beefyPath[beefyPath.length - 1]);
- if (farm <= craft && farm <= lumby && farm <= beefy) {
- dir = farmingDir;
- gate = farmingGate;
- area = farmingArea;
- path = farmingPath;
- where = "Farming";
- } else if (craft <= lumby && farm <= beefy) {
- dir = craftingDir;
- gate = craftingGate;
- area = craftingArea;
- path = craftingPath;
- where = "Crafting";
- } else if (beefy <= lumby) {
- dir = beefyDir;
- gate = beefyGate;
- area = beefyArea;
- path = beefyPath;
- where = "Beefy Bill";
- } else {
- dir = lumbyDir;
- gate = lumbyGate;
- area = lumbyArea;
- path = lumbyPath;
- bankID = 36786;
- where = "Lumby";
- }
- log("Starting in: " + where);
- } else
- path = null;
- }
- private boolean useItem(RSItem item, RSNPC npc) {
- if (game.getCurrentTab() != Game.TAB_INVENTORY) {
- game.openTab(Game.TAB_INVENTORY);
- }
- return item.doAction("Use") && npc.doAction("Use");
- }
- private boolean peopleCheck() {
- if (dir == lumbyDir)
- return false;
- int pCount = 0;
- final RSPlayer[] validPlayers = players.getAll();
- for (final RSPlayer player : validPlayers) {
- if (player == null)
- continue;
- if (area.contains(player.getLocation()))
- pCount++;
- }
- return pCount >= tolerance;
- }
- private void switchPlace() {
- doing = "Switching Location";
- if (area.contains(getMyPlayer().getLocation())
- && !calc.canReach(path[path.length - 1], false)) {
- objects.getNearest(gate).doAction("Open");
- switchPlace();
- } else {
- if (dir == craftingDir) {
- dir = farmingDir;
- gate = farmingGate;
- area = farmingArea;
- path = farmingPath;
- } else {
- dir = craftingDir;
- gate = craftingGate;
- area = craftingArea;
- path = craftingPath;
- }
- }
- }
- private boolean isMoving() {
- if (!getMyPlayer().isMoving())
- return false;
- if (walking.getDestination() != null
- && (calc.distanceBetween(path[0], walking.getDestination()) > 5 && calc
- .distanceBetween(path[path.length - 1],
- walking.getDestination()) > 5)) {
- if (calc.distanceTo(walking.getDestination()) < (walking
- .isRunEnabled() ? 6 : 8))
- return false;
- }
- return true;
- }
- private boolean gateCheck() {
- final RSObject o = objects.getNearest(gate);
- if (o == null)
- return false;
- if (o.getLocation().getX() >= path[path.length - 1].getX())
- return true;
- return false;
- }
- private RSGroundItem getNearestLootInArea(RSArea area, final int id) {
- RSGroundItem[] all = groundItems.getAll();
- int dis = 1000;
- RSGroundItem myLoot = null;
- for (RSGroundItem loot : all) {
- if (area.contains(loot.getLocation())) {
- if (loot.getItem().getID() == id) {
- if (calc.distanceBetween(getMyPlayer().getLocation(),
- loot.getLocation()) < dis) {
- dis = (int) calc.distanceBetween(getMyPlayer()
- .getLocation(), loot.getLocation());
- myLoot = loot;
- }
- }
- }
- }
- return myLoot;
- }
- @SuppressWarnings("unused")
- private RSNPC getNearestNPCInArea(RSArea area, final int... ids) {
- RSNPC[] all = npcs.getAll();
- int dis = 1000;
- RSNPC myNPC = null;
- for (RSNPC npc : all) {
- if (area.contains(npc.getLocation())) {
- for (int i : ids) {
- if (npc.getID() == i) {
- if (calc.distanceBetween(getMyPlayer().getLocation(),
- npc.getLocation()) < dis) {
- dis = (int) calc.distanceBetween(getMyPlayer()
- .getLocation(), npc.getLocation());
- myNPC = npc;
- }
- }
- }
- }
- }
- return myNPC;
- }
- private boolean atTile(RSTile tile, String action) {
- if (tile == null)
- return false;
- if (menu.isOpen()) {
- menu.doAction("Cancel");
- atTile(tile, action);
- }
- if (!calc.tileOnScreen(tile)) {
- walking.walkTo(checkTile(randomTile(tile)));
- return false;
- }
- Point click = calc.tileToScreen(tile);
- mouse.move(randomPoint(click));
- long begin = System.currentTimeMillis();
- boolean exit = false;
- while ((System.currentTimeMillis() - begin) < 2000) {
- for (String act : menu.getItems()) {
- if (act.contains(action)) {
- exit = true;
- break;
- }
- }
- if (exit)
- break;
- if (!calc.tileOnScreen(tile))
- walking.walkTo(randomTile(tile));
- else
- mouse.move(randomPoint(click));
- }
- if (!menu.getItems()[0].contains(action)) {
- mouse.click(randomPoint(click), false);
- return menu.doAction(action);
- } else
- mouse.click(randomPoint(click), true);
- return true;
- }
- private boolean clickNPC(final RSNPC npc, final String action) {
- if (npc == null)
- return false;
- final RSTile tile = npc.getLocation();
- if (tile == null)
- return false;
- try {
- Point screenLoc = npc.getScreenLocation();
- if (calc.distanceTo(tile) > 6 || !calc.pointOnScreen(screenLoc))
- camera.turnToTile(tile);
- if (calc.distanceTo(tile) > 20) {
- walking.walkTileMM(tile);
- return false;
- }
- for (int i = 0; i < 12; i++) {
- screenLoc = npc.getScreenLocation();
- if (!npc.isValid() || !calc.pointOnScreen(screenLoc))
- return false;
- mouse.move(randomPoint(screenLoc));
- if (menu.getItems()[0].toLowerCase().contains(
- npc.getName().toLowerCase()))
- break;
- if (mouse.getLocation().equals(screenLoc))
- break;
- }
- String[] menuItems = menu.getItems();
- if (menuItems.length == 0)
- return false;
- for (String menuItem : menuItems)
- if (menuItem.toLowerCase()
- .contains(npc.getName().toLowerCase()))
- if (menuItems[0].toLowerCase().contains(
- action.toLowerCase())) {
- mouse.click(true);
- return true;
- } else {
- mouse.click(false);
- return menu.doAction(action);
- }
- } catch (final Exception e) {
- log.log(Level.SEVERE, "clickNPC(RSNPC, String) model error: ", e);
- return false;
- }
- return false;
- }
- private void walk(boolean toBank) {
- doing = (walking.isRunEnabled() ? "Running" : "Walking") + " to "
- + (toBank ? "Bank" : "Cows");
- camera.setCompass('n');
- runCheck();
- RSTile start = locate();
- int loc = 0;
- for (int i = 0; i < path.length; i++) {
- if (start.equals(path[i])) {
- loc = i;
- break;
- }
- }
- if (toBank) {
- if (loc == 0)
- walking.walkTo(checkTile(randomTile(path[0], 1)));
- else
- walking.walkTo(checkTile(randomTile(path[loc - 1])));
- } else {
- if (loc == path.length - 1)
- walking.walkTo(checkTile(randomTile(path[path.length - 1], 1)));
- else
- walking.walkTo(checkTile(randomTile(path[loc + 1])));
- }
- }
- private RSTile locate() {
- RSTile min = path[0];
- for (int i = 1; i < path.length; i++) {
- if (calc.distanceTo(path[i]) < calc.distanceTo(min))
- min = path[i];
- }
- return min;
- }
- private RSTile randomTile(RSTile location) {
- return randomTile(location, 1);
- }
- private RSTile randomTile(RSTile location, int rand) {
- return randomTile(location, rand, rand);
- }
- // Randomizes tile by given x and y randoms
- private RSTile randomTile(RSTile location, int xRand, int yRand) {
- int x = location.getX(), y = location.getY();
- return new RSTile(x + random((-1 * xRand), xRand), y
- + random((-1 * yRand), yRand));
- }
- private Point randomPoint(Point click) {
- int dif = 2;
- return new Point(click.x + random(-dif, dif), click.y
- + random(-dif, dif));
- }
- // Keeps finding midtile until the tile is onMap
- private RSTile checkTile(RSTile tile) {
- if (calc.tileOnMap(tile))
- return tile;
- RSTile loc = getMyPlayer().getLocation();
- RSTile walk = new RSTile((loc.getX() + tile.getX()) / 2,
- (loc.getY() + tile.getY()) / 2);
- return calc.tileOnMap(walk) ? walk : checkTile(walk);
- }
- private void runCheck() {
- if (!walking.isRunEnabled() && walking.getEnergy() >= runRandom) {
- doing = "Setting run";
- walking.setRun(true);
- runRandom = random(30, 70);
- } else {
- if (walking.getEnergy() < random(15, 20)) {
- doing = "Resting";
- walking.rest(random(70, 90));
- }
- }
- }
- @Override
- public void onRepaint(Graphics g) {
- ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- if (game.isLoggedIn()) {
- final int invCount = inventory.getCount(hideID);
- if (oldCount < 0)
- oldCount = invCount;
- else {
- if (invCount > oldCount)
- count += invCount - oldCount;
- oldCount = invCount;
- }
- long millis = System.currentTimeMillis() - startTime;
- long hours = millis / (1000 * 60 * 60);
- millis -= hours * (1000 * 60 * 60);
- long minutes = millis / (1000 * 60);
- millis -= minutes * (1000 * 60);
- long seconds = millis / 1000;
- int topX = 515 - 173, topY = 337 - 50, x = topX + 5, y = topY;
- g.setColor(new Color(0, 100, 0, 100));
- g.fill3DRect(topX, topY, 515 - topX, 337 - topY, true);
- g.setColor(Color.green);
- g.drawString("Runtime: " + hours + "h " + minutes + "min "
- + seconds + "sec.", x, y += 15);
- g.drawString("Hides: " + count, x, y += 15);
- g.drawString("Status: " + doing, x, y += 15);
- }
- }
- }