Skip to content

Instantly share code, notes, and snippets.

@Bradsta
Created September 23, 2012 19:12
Show Gist options
  • Save Bradsta/3772715 to your computer and use it in GitHub Desktop.
Save Bradsta/3772715 to your computer and use it in GitHub Desktop.
STAChinBoxer V1.06
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.text.DecimalFormat;
import java.util.ArrayList;
import org.powerbot.core.event.listeners.PaintListener;
import org.powerbot.core.script.ActiveScript;
import org.powerbot.core.script.job.Task;
import org.powerbot.core.script.job.state.Node;
import org.powerbot.core.script.job.state.Tree;
import org.powerbot.game.api.Manifest;
import org.powerbot.game.api.methods.Game;
import org.powerbot.game.api.methods.Tabs;
import org.powerbot.game.api.methods.Walking;
import org.powerbot.game.api.methods.input.Mouse;
import org.powerbot.game.api.methods.interactive.NPCs;
import org.powerbot.game.api.methods.interactive.Players;
import org.powerbot.game.api.methods.node.GroundItems;
import org.powerbot.game.api.methods.node.Menu;
import org.powerbot.game.api.methods.node.SceneEntities;
import org.powerbot.game.api.methods.tab.Inventory;
import org.powerbot.game.api.methods.tab.Skills;
import org.powerbot.game.api.methods.widget.Camera;
import org.powerbot.game.api.util.Filter;
import org.powerbot.game.api.util.Random;
import org.powerbot.game.api.wrappers.Entity;
import org.powerbot.game.api.wrappers.Tile;
import org.powerbot.game.api.wrappers.node.GroundItem;
import org.powerbot.game.api.wrappers.node.Item;
import org.powerbot.game.api.wrappers.node.SceneObject;
@Manifest(name = "STAChinBoxer",
version = 1.06,
description = "Hunts red & grey chinchompa",
authors = { "Bradsta" })
public class STAChinBoxer extends ActiveScript implements PaintListener {
//Paint vars
private double version = 1.06D;
private String status = "Setup";
private int chinPrice = 0;
private long startTime;
private int startChins;
private int currentChins;
private int startCash;
private int currentCash;
private int startExp;
private int currentExp;
private int chinID;
//IDs
private int trapID = 10008;
private int[] lootableTraps = { /*fails*/ 19192, 19198, /*fulls*/ 19190, 19189 };
private int[] activeTraps = { 19187, 19198, 19200, 19197, 19188, 19199,
19196, 19194, 19193, 19195 };
private int[] allTraps = { 19187, 19198, 19200, 19197, 19188, 19199,
19196, 19194, 19193, 19195, 19192, 19198, 19190, 19189 };
//Other
private int trapCount = 0;
private ArrayList<Tile> tiles = new ArrayList<Tile>(); // Recommended tiles
private ArrayList<Tile> active = new ArrayList<Tile>(); // Active traps
private final Tree scriptTree = new Tree(new Node[] {
new Start(),
new Wait(),
new GrabTrap(),
new LootTrap(),
new SetTrap()
});
@Override
public void onStart() {
startTime = currentTime = System.currentTimeMillis();
}
@Override
public int loop() {
scriptTree.set(scriptTree.state());
Node node = scriptTree.get();
if (node != null) node.execute();
return Random.nextInt(100, 200);
}
/** CORE **/
private class Start extends Node {
@Override
public void execute() {
Task.sleep(5000); //Fail-safe
log.info("Setting up script...");
if (Skills.getLevel(Skills.HUNTER) < 53) {
log.info("Your hunting level must be over 53 to use this script.");
stop();
}
if (NPCs.getNearest(5079) != null) {
log.info("Going to hunt gray chinchompa");
chinPrice = getItemPrice(10033);
chinID = 10033;
} else {
log.info("Going to hunt red chinchompa");
chinPrice = getItemPrice(10034);
chinID = 10034;
}
log.info("Price for the chins we are hunting is " + chinPrice);
trapCount = ((Skills.getLevel(Skills.HUNTER) / 20) + 1);
log.info("Using " + trapCount + " traps.");
Tile base = Players.getLocal().getLocation();
tiles.add(new Tile(base.getX()+1, base.getY()+1, 0));
tiles.add(new Tile(base.getX()-1, base.getY()+1, 0));
tiles.add(new Tile(base.getX()+1, base.getY()-1, 0));
tiles.add(new Tile(base.getX()-1, base.getY()-1, 0));
tiles.add(base);
tiles.add(new Tile(base.getX(), base.getY()+1, 0));
tiles.add(new Tile(base.getX(), base.getY()-1, 0));
tiles.add(new Tile(base.getX()+1, base.getY(), 0));
tiles.add(new Tile(base.getX()-1, base.getY(), 0));
for (int a=(-2); a<=2; a++) {
for (int b=(-2); b<=2; b++) {
Tile next = new Tile(base.getX() + a, base.getY() + b, 0);
if (!tiles.contains(next)) tiles.add(next); // creates 4x4 square for backup tiles
}
}
startChins = currentChins = Inventory.getItem(chinID).getStackSize();
startCash = currentCash = startChins * chinPrice;
startExp = currentExp = Skills.getExperience(Skills.HUNTER);
}
@Override
public boolean activate() {
return (chinPrice == 0 && Game.isLoggedIn() && Players.getLocal() != null && Players.getLocal().isOnScreen());
}
}
private class SetTrap extends Node {
@Override
public void execute() {
status = "Setting trap";
Tile trapTile = null;
for (Tile t : tiles) {
if (t.canReach() && tileCheck(t)) {
if (active.contains(t) && getObj(allTraps, t) == null) {
active.remove(t);
}
trapTile = t;
break;
}
}
while (Players.getLocal().isMoving()) Task.sleep(200);
if (trapTile != null && walk(trapTile, true)) {
if (trapTile.equals(Players.getLocal().getLocation())) {
final int invCount = Inventory.getCount(trapID);
final Item trap = Inventory.getItem(trapID);
if (trap != null) {
if (!Menu.contains("Lay")) {
trap.getWidgetChild().interact("Lay");
} else if (Mouse.getLocation() != null) {
Mouse.click(Mouse.getLocation(), true);
}
for (int i=0; i<10 && invCount == Inventory.getCount(trapID); i++) {
Task.sleep(400);
}
if (invCount != Inventory.getCount(trapID)) {
hover();
for (int i=0; i<10 && tileCheck(trapTile); i++) {
Task.sleep(600);
}
if (!tileCheck(trapTile)) {
if (!active.contains(trapTile)) active.add(trapTile);
Task.sleep(Random.nextInt(500, 600)); //Fail-safe
}
}
}
}
}
}
@Override
public boolean activate() {
return (Game.isLoggedIn() && (active.size() < trapCount) && getLootable() == null && getGroundTrap() == null);
}
}
private class LootTrap extends Node {
@Override
public void execute() {
status = "Checking trap";
SceneObject trap = getLootable();
if (trap != null) {
final int invCount = Inventory.getCount(trapID);
boolean interacted = false;
if (trap.getId() >= 19192) {
interacted = trap.interact("Dismantle");
} else {
interacted = trap.interact("Check");
}
if (interacted) {
for (int j=0; j<10 && invCount == Inventory.getCount(trapID); j++) Task.sleep(500);
if (invCount != Inventory.getCount(trapID)) {
currentChins = Inventory.getItem(chinID).getStackSize();
currentCash = currentChins * chinPrice;
currentExp = Skills.getExperience(Skills.HUNTER);
for (int i=0; i<active.size(); i++) {
if (active.get(i).equals(trap.getLocation())) {
active.remove(i);
break;
}
}
}
}
}
}
@Override
public boolean activate() {
return (Game.isLoggedIn() && getLootable() != null && getGroundTrap() == null);
}
}
private class GrabTrap extends Node {
@Override
public void execute() {
GroundItem fallenTrap = getGroundTrap();
if (fallenTrap != null) {
status = "Getting trap";
int invCount = Inventory.getCount(trapID);
if (getObj(activeTraps, fallenTrap.getLocation()) != null && fallenTrap.interact("Take")) {
for (int i=0; i<10 && (invCount == Inventory.getCount(trapID)); i++) Task.sleep(500);
active.remove(fallenTrap.getLocation());
} else if (fallenTrap.interact("Lay")) {
for (int i=0; i<10 && tileCheck(fallenTrap.getLocation()); i++) Task.sleep(600);
if (!tileCheck(fallenTrap.getLocation()) && !active.contains(fallenTrap.getLocation())) {
active.add(fallenTrap.getLocation());
}
}
}
}
@Override
public boolean activate() {
return (Game.isLoggedIn() && getGroundTrap() != null);
}
}
private class Wait extends Node {
@Override
public boolean activate() {
return (Game.isLoggedIn() && active.size() > 0 && (active.size() == trapCount) && trapCheck());
}
@Override
public void execute() {
status = "Waiting";
Task.sleep(Random.nextInt(500, 1000));
switch (Random.nextInt(1, 10)) {
case 3:
Mouse.move(new Point(Random.nextInt(0, Game.getDimensions().width), Random.nextInt(0, Game.getDimensions().height)));
break;
case 5:
Tabs.values()[Random.nextInt(0, Tabs.values().length-1)].open();
break;
case 7:
Camera.turnTo(Players.getLocal().getLocation().derive(Random.nextInt(-2, 2), Random.nextInt(-2, 2)));
break;
}
}
}
/** METHODS/PAINT **/
//Gets empty/full traps
private SceneObject getLootable() {
return SceneEntities.getNearest(new Filter<SceneObject>() {
@Override
public boolean accept(SceneObject arg0) {
for (Tile t : active) {
for (int id : lootableTraps) {
if (arg0.getId() == id && arg0.getLocation().equals(t)) {
return true;
}
}
}
return false;
}
});
}
//Used for making sure there isn't already a trap when getting a ground trap.
private SceneObject getObj(final int[] ids, final Tile t) {
return SceneEntities.getNearest(new Filter<SceneObject>() {
@Override
public boolean accept(SceneObject arg0) {
for (int id : ids) {
if (arg0.getId() == id && arg0.getLocation().equals(t)) {
return true;
}
}
return false;
}
});
}
//Returns the fallen trap in a tile where a standing trap should be
private GroundItem getGroundTrap() {
return GroundItems.getNearest(new Filter<GroundItem>() {
@Override
public boolean accept(GroundItem arg0) {
for (Tile t : active) {
if (arg0.getId() == trapID && arg0.getLocation().equals(t)) {
return true;
}
}
return false;
}
});
}
//Clicks tile on screen to walk otherwise uses Walking.walk - hover on trap
private boolean walk(Tile t, boolean hoverTrap) {
if (t.isOnScreen() && t.canReach()) {
for (int i=0; i<3 && !Players.getLocal().getLocation().equals(t); i++) {
t.interact("Walk");
for (int j=0; j<10 && !Players.getLocal().isMoving(); j++) Task.sleep(200);
if (hoverTrap && Players.getLocal().isMoving()) {
final Item trap = Inventory.getItem(trapID);
if (trap != null && Tabs.INVENTORY.open()) {
Point random = new Point(trap.getWidgetChild().getCentralPoint().x + Random.nextInt(-8, 8),
trap.getWidgetChild().getCentralPoint().y + Random.nextInt(-8, 8));
Mouse.move(random);
}
}
while (Players.getLocal().isMoving()) Task.sleep(600);
}
} else {
Walking.walk(t);
}
return Players.getLocal().getLocation().equals(t);
}
//Checks whether or not all traps are up.
private boolean trapCheck() {
final ArrayList<SceneObject> objs = new ArrayList<SceneObject>();
for (final Tile trapTile : active) {
SceneEntities.getNearest(new Filter<SceneObject>() {
@Override
public boolean accept(SceneObject arg0) {
for (int id : activeTraps) {
if (arg0.getId() == id && arg0.getLocation().equals(trapTile)) {
objs.add(arg0);
return true;
}
}
return false;
}
});
}
return objs.size() == trapCount;
}
//Checks whether or not a tile is occupied with a trap
private boolean tileCheck(final Tile t) {
SceneObject object = SceneEntities.getAt(t);
// SceneObject object = SceneEntities.getNearest(new Filter<SceneObject>() {
//
// @Override
// public boolean accept(SceneObject arg0) {
// for (int id : allTraps) {
// if (arg0.getId() == id && arg0.getLocation().equals(t)) return true;
// }
//
// return false;
// }
//
// });
if (object != null) {
for (int id : allTraps) {
if (object.getId() == id) return false;
}
}
return true;
}
//Hovers over the next trap/ground trap
private void hover() {
Entity ent = null;
if ((ent = getGroundTrap()) != null
|| (ent = getLootable()) != null) {
ent.hover();
}
}
//Created by bradsta
private int getItemPrice(int itemID) {
try {
InetAddress add = InetAddress.getByName("services.runescape.com");
Socket sock = new Socket(add, 80);
BufferedWriter buff = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream(), "UTF8"));
buff.write("GET /m=itemdb_rs/viewitem.ws?obj=" + itemID + " HTTP/1.1" + "\r\n");
buff.write("Host: services.runescape.com" + "\r\n");
buff.write("Connection: close" + "\r\n");
buff.write("\r\n");
buff.flush();
BufferedReader read = new BufferedReader(new InputStreamReader(sock.getInputStream()));
String line = null;
int price = 0;
while ((line = read.readLine()) != null) {
if (line.contains("Current guide price:")) {
line = getBetween("<td>", "</td>", read.readLine());
if (!line.contains("k") && !line.contains("m")) {
price = Integer.parseInt(line.replace(",", ""));
} else if (line.contains("k")) {
price = Integer.parseInt(line.replace(".", "").replace("k", "") + "00");
} else if (line.contains("m")) {
price = Integer.parseInt(line.replace(".", "").replace("m", "") + "00000");
}
break;
}
}
read.close();
buff.close();
sock.close();
return price;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
//Credits smitteh
private String getBetween(String s, String e, String whole) {
return whole.substring(whole.indexOf(s) + s.length(), whole.indexOf(e, whole.indexOf(s) + s.length()));
}
//Credits Enfilade's Easel for the quick design.
private final RenderingHints antialiasing = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
private final Color background = new Color(0, 0, 0, 160);
private final Color border1 = new Color(0, 0, 0);
private final Color border2 = new Color(255, 255, 255);
private final Font titleFont = new Font("Arial", 0, 9);
private final Font infoFont = new Font("Arial", 0, 10);
private long currentTime = 0;
public void onRepaint(Graphics g1) {
Graphics2D g = (Graphics2D)g1;
g.setRenderingHints(antialiasing);
for (final Tile t : active) {
t.draw(g);
}
g.setColor(Color.WHITE);
g.drawOval(Mouse.getX()-5, Mouse.getY()-5, 10, 10);
g.drawOval(Mouse.getX(), Mouse.getY(), 1, 1);
g.translate(0, 50); //RS Toolbar at the top
g.setColor(background);
g.fillRoundRect(3, 161, 156, 175, 16, 16);
g.setColor(border1);
g.drawRoundRect(3, 161, 156, 175, 16, 16);
g.setColor(border2);
g.drawRoundRect(6, 164, 150, 169, 16, 16);
g.setFont(titleFont);
g.drawString("STAChinBoxer V" + version, 39, 182); // Should be using font metrics
g.drawLine(11, 186, 151, 186);
g.setFont(infoFont);
g.drawString("Time: " + timeToString(System.currentTimeMillis() - startTime), 14, 205);
g.drawString("Cash/hr: " + formatNumber((int) anHour((double) currentCash-startCash)), 14, 222);
g.drawString("Total cash: " + formatNumber(currentCash-startCash), 14, 239);
g.drawString("Chins/hr: " + formatNumber((int) anHour((double) currentChins-startChins)), 14, 256);
g.drawString("Total chins: " + formatNumber(currentChins-startChins), 14, 273);
g.drawLine(11, 313, 151, 313);
g.drawLine(684, 328, 684, 328);
g.drawString("Exp/hr: " + formatNumber((int) anHour((double) currentExp-startExp)), 14, 290);
g.drawString("Total exp: " + formatNumber(currentExp-startExp), 14, 307);
g.drawString("Status: " + status, 16, 326);
}
private String formatNumber(int number) {
DecimalFormat format = new DecimalFormat("#0.0");
if (number >= 10000) {
return String.valueOf(format.format((float) number / 1000.00f))
+ "K";
}
return String.valueOf(number);
}
private long anHour(double number) {
currentTime = System.currentTimeMillis();
if ((currentTime-startTime) < 1)
return 0;
return (long) ((float) number / ((float) (currentTime-startTime) / 1000.00f / 3600.00f));
}
private String timeToString(long timePassed) {
int hours = (int) (timePassed / 3600000);
int minutes = (int) ((timePassed % 3600000) / 60000);
int seconds = (int) ((((timePassed % 3600000) % 60000)) / 1000);
String time = "";
if (hours < 10) time += "0" + hours + ":";
else time += hours + ":";
if (minutes < 10) time += "0" + minutes + ":";
else time += minutes + ":";
if (seconds < 10) time += "0" + seconds;
else time += seconds;
return time;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment