Skip to content

Instantly share code, notes, and snippets.

@riston
Created July 6, 2012 21:28
Show Gist options
  • Save riston/3062840 to your computer and use it in GitHub Desktop.
Save riston/3062840 to your computer and use it in GitHub Desktop.
Falador Chicken Killer
package org.jurka.fighting;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import org.powerbot.concurrent.Task;
import org.powerbot.concurrent.strategy.Condition;
import org.powerbot.concurrent.strategy.Strategy;
import org.powerbot.game.api.ActiveScript;
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.tab.Inventory;
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.util.Time;
import org.powerbot.game.api.wrappers.Area;
import org.powerbot.game.api.wrappers.Tile;
import org.powerbot.game.api.wrappers.interactive.NPC;
import org.powerbot.game.api.wrappers.interactive.Player;
import org.powerbot.game.api.wrappers.node.GroundItem;
import org.powerbot.game.bot.event.listener.PaintListener;
@Manifest(authors = { "Jurka" }, name = "Chicken killer", description = "Kill chickens near falador and take feathers", version = 1.0)
public class ChickenFight extends ActiveScript implements PaintListener {
public Area chickenArea = new Area(
new Tile(3014, 3295, 0),
new Tile(3020, 3283, 0));
public final static int FEATHER_ID = 314;
private Stats stats;
private StatsPaint paint;
@Override
protected void setup() {
stats = Stats.getInstance();
paint = new StatsPaint(stats);
provide(new FightChicken());
provide(new PickFeather());
provide(new AntiRandom());
}
@Override
public void onRepaint(Graphics g) {
paint.draw(g);
}
class FightChicken extends Strategy implements Task, Condition {
@Override
public void run() {
setSync(true);
setLock(false);
NPC npc = NPCs.getNearest(new Filter<NPC>() {
@Override
public boolean accept(NPC npc) {
return npc.getName().toLowerCase().contains("chick")
&& chickenArea.contains(npc);
}
});
if (npc != null && !npc.isInCombat()) {
stats.setStatus("Found new chick attack");
npc.interact("Attack");
Time.sleep(Random.nextGaussian(300, 1500, 65));
stats.incChickensAttacked();
}
}
@Override
public boolean validate() {
Player current = Players.getLocal();
return current.getAnimation() == -1
&& !current.isInCombat();
}
}
private class AntiRandom extends Strategy implements Task, Condition {
@Override
public void run() {
setLock(false);
setSync(true);
if (Random.nextBoolean()) return;
switch (Random.nextGaussian(1, 50, 85)) {
case 7:
stats.setStatus("Antirandom camera turn");
// Turn camera
Camera.setAngle(Random.nextGaussian(-360, 360, 65));
break;
case 8:
// Camera movement
stats.setStatus("Antirandom camera up/down");
Camera.setPitch(Random.nextGaussian(0, 100, 95));
Time.sleep(Random.nextInt(1000, 2500));
Camera.setPitch(true);
break;
case 11:
// Random tab looking
stats.setStatus("Antirandom random tab switching");
int randomTabIndex = Random.nextInt(0, Tabs.values().length);
Tabs randomTab = Tabs.values()[randomTabIndex];
// If the tab is invetory look the feather count
if (randomTab == Tabs.INVENTORY)
stats.setFeathers(Inventory.getCount(true, FEATHER_ID));
randomTab.open();
Time.sleep(Random.nextInt(1000, 3000));
break;
case 15:
Dimension gameDimension = Game.getDimensions();
Mouse.move(new Point(
Random.nextInt(0, (int) gameDimension.getHeight()),
Random.nextInt(0, (int) gameDimension.getWidth())));
Time.sleep(Random.nextGaussian(2000, 3500, 65));
break;
case 20:
// Turn running on if have enough energy
if (!Walking.isRunEnabled()) {
if (Walking.getEnergy() > Random.nextInt(20, 50)) {
Walking.setRun(true);
}
}
default:
}
}
@Override
public boolean validate() {
return Players.getLocal().getAnimation() == -1;
}
}
class PickFeather extends Strategy implements Task, Condition {
@Override
public void run() {
GroundItem feather = findFeather();
if (feather != null) {
feather.interact("Take", "Feather");
stats.setStatus("Get feathers");
Time.sleep(Random.nextGaussian(100, 300, 65));
}
}
@Override
public boolean validate() {
GroundItem feather = findFeather();
return feather != null
&& feather.isOnScreen()
&& !Players.getLocal().isMoving();
}
public GroundItem findFeather() {
GroundItem feather = GroundItems.getNearest(new Filter<GroundItem>() {
@Override
public boolean accept(GroundItem item) {
return item.getId() == FEATHER_ID && chickenArea.contains(item);
}
});
return feather;
}
}
}
package org.jurka.fighting;
public class Stats {
private static Stats instance = null;
private long startTime = 0;
private int chickensAttacked = 0;
private int feathers = 0;
private String status = "waiting";
public final static int CHICKEN_XP = 16;
protected Stats() {
setStartTime(System.currentTimeMillis());
}
public static Stats getInstance() {
if (instance == null) {
instance = new Stats();
}
return instance;
}
public long getStartTime() {
return startTime;
}
public void setStartTime(long startTime) {
this.startTime = startTime;
}
public int getChickensAttacked() {
return chickensAttacked;
}
public void incChickensAttacked() {
this.chickensAttacked++;
}
public int getFeathers() {
return feathers;
}
public void setFeathers(int feathers) {
this.feathers = feathers;
}
public void incFeathers(int feathers) {
this.feathers += feathers;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getTotalXP() {
return CHICKEN_XP * chickensAttacked;
}
}
package org.jurka.fighting;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import org.powerbot.game.api.methods.Widgets;
import org.powerbot.game.api.wrappers.widget.WidgetChild;
public class StatsPaint {
private Stats stats;
private Image feather;
private final static int CHAT_WIDGET = 137;
// I am not sure how often does the url changes
private final String FEATHER_IMG_URL = "http://services.runescape.com/m=itemdb_rs/3779_obj_big.gif?id=314";
private final int FEATHER_SIZE = 96;
public StatsPaint(Stats stats) {
this.stats = stats;
this.feather = Util.downloadImage(FEATHER_IMG_URL);
}
public void draw(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setFont(new Font("Sans serif", Font.BOLD, 13));
int xAxis = 45, yAxis = 45, sp = 15;
g2.drawString("Chicken killer & feather picker", xAxis, yAxis);
yAxis += sp + 2;
g2.setFont(new Font("Sans serif", Font.BOLD, 11));
g2.drawString(String.format("Chickens attacked %d", stats.getChickensAttacked()), xAxis, yAxis);
yAxis += sp;
g2.drawString(String.format("Feathers %d", stats.getFeathers()), xAxis, yAxis);
yAxis += sp;
g2.drawString(String.format("Running time %s", Util.formatTime(stats.getStartTime())), xAxis, yAxis);
yAxis += sp;
g2.drawString(String.format("Status: %s", stats.getStatus()), xAxis, yAxis);
yAxis += sp;
g2.drawString(String.format("Total XP: %s", Util.coolFormat((double) stats.getTotalXP(), 0)), xAxis, yAxis);
g2.setColor(new Color(0, 250, 0, 50));
g2.fillRect(40, 30, 230, 100);
drawFeather(g2);
}
private void drawFeather(Graphics2D g2) {
if (feather != null) {
WidgetChild chatWidget = Widgets.get(CHAT_WIDGET).getChild(0);
int width = chatWidget.getWidth(),
height = chatWidget.getHeight(),
absX = chatWidget.getAbsoluteX(),
absY = chatWidget.getAbsoluteY();
int featherX = chatWidget.getWidth() - FEATHER_SIZE / 2,
featherY = absY - FEATHER_SIZE / 2;
g2.drawImage(feather, featherX, featherY, null);
}
}
}
package org.jurka.fighting;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
public class Util {
/**
* Formats time from milliseconds to HH:MM:SS format
* @param startTime long
* @return String time
*/
public static String formatTime(long startTime) {
// Taken from ShantayX all credits to author
final long runTimeMilliseconds = System.currentTimeMillis() - startTime;
final long secondsTotal = runTimeMilliseconds / 1000;
final long minutesTotal = secondsTotal / 60;
final long hoursTotal = minutesTotal / 60;
int currentTimeHMS[] = new int[3];
currentTimeHMS[2] = (int) (secondsTotal % 60);
currentTimeHMS[1] = (int) (minutesTotal % 60);
currentTimeHMS[0] = (int) (hoursTotal % 500);
return String.format("%02d:%02d:%02d", currentTimeHMS[0], currentTimeHMS[1], currentTimeHMS[2]);
}
/**
* Recursive implementation, invokes itself for each factor of a thousand, increasing the class on each invokation.
* From http://stackoverflow.com/questions/4753251/how-to-go-about-formatting-1200-to-1-2k-in-java
* @param n the number to format
* @param iteration in fact this is the class from the array c
* @return a String representing the number n formatted in a cool looking way.
*/
public static String coolFormat(double n, int iteration) {
char[] c = new char[]{'k', 'm', 'b', 't'};
double d = ((long) n / 100) / 10.0;
boolean isRound = (d * 10) %10 == 0;//true if the decimal part is equal to 0 (then it's trimmed anyway)
return (d < 1000? //this determines the class, i.e. 'k', 'm' etc
((d > 99.9 || isRound || (!isRound && d > 9.99)? //this decides whether to trim the decimals
(int) d * 10 / 10 : d + "" // (int) d * 10 / 10 drops the decimal
) + "" + c[iteration])
: coolFormat(d, iteration+1));
}
public static Image downloadImage(String url) {
BufferedImage bufferedImage;
try {
bufferedImage = ImageIO.read(new URL(url));
} catch (Exception e) {
bufferedImage = null;
}
return bufferedImage;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment