Created
July 5, 2012 19:30
-
-
Save riston/3055895 to your computer and use it in GitHub Desktop.
Fishing script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.jurka.fishing; | |
import java.awt.Dimension; | |
import java.awt.Graphics; | |
import java.awt.Point; | |
import java.util.Arrays; | |
import java.util.HashSet; | |
import java.util.Set; | |
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.SceneEntities; | |
import org.powerbot.game.api.methods.tab.Inventory; | |
import org.powerbot.game.api.methods.tab.Skills; | |
import org.powerbot.game.api.methods.widget.Bank; | |
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.Item; | |
import org.powerbot.game.api.wrappers.node.SceneObject; | |
import org.powerbot.game.bot.event.MessageEvent; | |
import org.powerbot.game.bot.event.listener.MessageListener; | |
import org.powerbot.game.bot.event.listener.PaintListener; | |
@Manifest( | |
authors = { "Jurka" }, | |
name = "Net fishing at Draynor Village", | |
description = "Net fisher", | |
version = 1.0) | |
public class DraynorNet extends ActiveScript | |
implements MessageListener, PaintListener { | |
public static final int STD = 65; | |
public static final int INVETORY_SIZE = 28; | |
public static final int[] FISHING_SPOTS = { 327 }; | |
public static final int[] INVETORY_FISH = { 317 }; | |
public static final int FISHING_ANIMATION = 621; | |
public static final Area BANK_AREA = new Area( | |
new Tile(3094, 3240, 0), | |
new Tile(3092, 3246, 0)); | |
// From bank to fishing spot | |
public static final Tile[] WALK_TO_FISH = { | |
new Tile(3092, 3243, 0), | |
new Tile(3086, 3231, 0) }; | |
public static final int[] RANDOM_INVETORY_ITEMS = { 14664 }; | |
public static final int[] FIRE_IDS = { 70758, 70755 }; | |
private Stats stats; | |
private StatsPaint paint; | |
@Override | |
protected void setup() { | |
stats = Stats.getInstance(); | |
stats.setFishLevel(Skills.getLevel(Skills.FISHING)); | |
paint = new StatsPaint(stats); | |
provide(new Fish()); | |
provide(new WalkBank()); | |
provide(new AntiRandom()); | |
} | |
public static void normalSleep() { | |
Time.sleep(Random.nextGaussian(700, 2000, 95)); | |
} | |
public Item[] getRawItems() { | |
Item[] items = Inventory.getItems(new Filter<Item>() { | |
@Override | |
public boolean accept(Item item) { | |
// Filter all items which has raw in name | |
return item.getName().toLowerCase().contains("raw"); | |
} | |
}); | |
return items; | |
} | |
private class Fish extends Strategy implements Task, Condition { | |
@Override | |
public void run() { | |
NPC fishingSpot = NPCs.getNearest(new Filter<NPC>() { | |
@Override | |
public boolean accept(NPC npc) { | |
return Arrays.binarySearch(FISHING_SPOTS, npc.getId()) == 0; | |
} | |
}); | |
if (fishingSpot != null) { | |
if (!fishingSpot.isOnScreen()) { | |
Camera.turnTo(fishingSpot); | |
normalSleep(); | |
} | |
stats.setStatus("Fishing"); | |
fishingSpot.interact("Net"); | |
} | |
Time.sleep(Random.nextGaussian(200, 1000, STD)); | |
} | |
@Override | |
public boolean validate() { | |
Player current = Players.getLocal(); | |
return Inventory.getCount() < INVETORY_SIZE | |
&& current.getAnimation() == -1; | |
} | |
} | |
private class AntiRandom extends Strategy implements Task, Condition { | |
@Override | |
public void run() { | |
if (Random.nextBoolean()) return; | |
stats.setStatus("Antirandom activated"); | |
switch (Random.nextGaussian(1, 50, 85)) { | |
case 7: | |
// Turn camera | |
Camera.setAngle(Random.nextGaussian(-360, 360, 65)); | |
stats.setStatus("Turn camera"); | |
break; | |
case 8: | |
// Camera movement | |
Camera.setPitch(Random.nextGaussian(0, 100, 95)); | |
stats.setStatus("Camera up/down"); | |
Time.sleep(Random.nextInt(2000, 3000)); | |
Camera.setPitch(true); | |
break; | |
case 11: | |
// Random tab looking | |
int randomTabIndex = Random.nextInt(0, Tabs.values().length); | |
Tabs randomTab = Tabs.values()[randomTabIndex]; | |
randomTab.open(); | |
stats.setStatus("Switching tabs"); | |
Time.sleep(Random.nextInt(3000, 8000)); | |
break; | |
case 15: | |
Dimension gameDimension = Game.getDimensions(); | |
Mouse.move(new Point( | |
Random.nextInt(0, (int) gameDimension.getHeight()), | |
Random.nextInt(0, (int) gameDimension.getWidth()))); | |
stats.setStatus("Mouse move"); | |
Time.sleep(Random.nextGaussian(2000, 3500, 65)); | |
break; | |
default: | |
} | |
} | |
@Override | |
public boolean validate() { | |
return Players.getLocal().getAnimation() == FISHING_ANIMATION; | |
} | |
} | |
private class WalkBank extends Strategy implements Task, Condition { | |
@Override | |
public void run() { | |
stats.setStatus("Walk to bank"); | |
if (!Walking.isRunEnabled() && Walking.getEnergy() > 10) { | |
Walking.setRun(true); | |
} | |
Walking.newTilePath(WALK_TO_FISH).reverse().traverse(); | |
if (BANK_AREA.contains(Players.getLocal().getLocation())) { | |
stats.setStatus("User in bank"); | |
Time.sleep(Random.nextInt(5000, 2500)); | |
if (Bank.open() && Bank.isOpen()) { | |
normalSleep(); | |
Bank.depositInventory(); | |
stats.incBankTrips(); | |
Bank.close(); | |
stats.setStatus("Walk to fishing spot"); | |
Walking.newTilePath(WALK_TO_FISH).traverse(); | |
} | |
} | |
} | |
@Override | |
public boolean validate() { | |
return Inventory.getCount() == INVETORY_SIZE; | |
} | |
} | |
private class Cook extends Strategy implements Task, Condition { | |
@Override | |
public void run() { | |
SceneObject fire = SceneEntities.getNearest(FIRE_IDS); | |
if (fire != null) { | |
if (Walking.findPath(fire.getLocation()).traverse()) { | |
Item[] items = getRawItems(); | |
Set<Item> uniqueItems = new HashSet<Item>(Arrays.asList(items)); | |
for (Item item : uniqueItems) { | |
log.info(item.getName() + " would need to be cooked"); | |
} | |
} | |
} | |
} | |
@Override | |
public boolean validate() { | |
SceneObject fire = SceneEntities.getNearest(FIRE_IDS); | |
int rawItemCount = Inventory.getCount(new Filter<Item>() { | |
@Override | |
public boolean accept(Item item) { | |
return item.getName().toLowerCase().contains("raw"); | |
} | |
}); | |
return fire != null && rawItemCount >= 10; | |
} | |
} | |
private class Drop extends Strategy implements Task, Condition { | |
@Override | |
public void run() { | |
Item[] items = Inventory.getItems(new Filter<Item>() { | |
@Override | |
public boolean accept(Item item) { | |
// Drop all the items which has raw in name | |
return item.getName().toLowerCase().contains("raw"); | |
} | |
}); | |
for (Item item : items) { | |
item.getWidgetChild().interact("Drop"); | |
Time.sleep(Random.nextGaussian(100, 1000, 95)); | |
} | |
} | |
@Override | |
public boolean validate() { | |
return Inventory.getCount() == INVETORY_SIZE; | |
} | |
} | |
@Override | |
public void messageReceived(MessageEvent event) { | |
String message = event.getMessage(); | |
if (message.contains("anchovies")) { | |
stats.incAnchovies(); | |
} else if (message.contains("shrimp")) { | |
stats.incShrimps(); | |
} else if (message.contains("a Fishing level")) { | |
stats.incFishLevel(); | |
} | |
} | |
@Override | |
public void onRepaint(Graphics g) { | |
paint.draw(g); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.jurka.fishing; | |
public class Stats { | |
private static Stats instance = null; | |
private long startTime = 0; | |
private int bankTrips = 0; | |
private int shrimps = 0; | |
private int anchovies = 0; | |
private int fishLevel = 0; | |
private String status = "waiting"; | |
public final static int SHRIMP_XP = 10; | |
public final static int ANCHOVIES_XP = 40; | |
protected Stats() { | |
startTime = 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 getBankTrips() { | |
return bankTrips; | |
} | |
public void incBankTrips() { | |
this.bankTrips++; | |
} | |
public int getShrimps() { | |
return shrimps; | |
} | |
public void incShrimps() { | |
this.shrimps++; | |
} | |
public int getAnchovies() { | |
return anchovies; | |
} | |
public void incAnchovies() { | |
this.anchovies++; | |
} | |
public void setFishLevel(int fishLevel) { | |
this.fishLevel = fishLevel; | |
} | |
public int getFishLevel() { | |
return fishLevel; | |
} | |
public void incFishLevel() { | |
this.fishLevel++; | |
} | |
public String getStatus() { | |
return this.status; | |
} | |
public void setStatus(String status) { | |
this.status = status; | |
} | |
public int getTotalXP() { | |
// Another way is to store starting experience and then subtract it from current | |
return SHRIMP_XP * this.shrimps + ANCHOVIES_XP * this.anchovies; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.jurka.fishing; | |
import java.awt.Font; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
public class StatsPaint { | |
private Stats stats; | |
public StatsPaint(Stats stats) { | |
this.stats = stats; | |
} | |
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(String.format("Fishing level %d", stats.getFishLevel()), xAxis, yAxis); | |
yAxis += sp; | |
g2.drawString(String.format("Bank trips %d", stats.getBankTrips()), xAxis, yAxis); | |
yAxis += sp; | |
g2.drawString(String.format("Anchovies %d", stats.getAnchovies()), xAxis, yAxis ); | |
yAxis += sp; | |
g2.drawString(String.format("Shrimp %d", stats.getShrimps()), xAxis, yAxis); | |
yAxis += sp; | |
g2.drawString(String.format("Running time %s", 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", coolFormat((double) stats.getTotalXP(), 0)), xAxis, yAxis); | |
} | |
/** | |
* Formats time from milliseconds to HH:MM:SS format | |
* @param startTime long | |
* @return String time | |
*/ | |
public 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. | |
*/ | |
private 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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to get script