Advertisement
battleguard

FlyFisher.java

Apr 16th, 2012
940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.44 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.Graphics;
  4. import java.awt.Point;
  5. import java.awt.Rectangle;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseListener;
  8. import java.awt.event.MouseMotionListener;
  9. import java.text.DecimalFormat;
  10. import java.text.NumberFormat;
  11. import java.util.ArrayList;
  12.  
  13. import org.powerbot.concurrent.Task;
  14. import org.powerbot.concurrent.strategy.Strategy;
  15. import org.powerbot.game.api.ActiveScript;
  16. import org.powerbot.game.api.Manifest;
  17. import org.powerbot.game.api.methods.Tabs;
  18. import org.powerbot.game.api.methods.Walking;
  19. import org.powerbot.game.api.methods.Widgets;
  20. import org.powerbot.game.api.methods.input.Mouse;
  21. import org.powerbot.game.api.methods.interactive.NPCs;
  22. import org.powerbot.game.api.methods.interactive.Players;
  23. import org.powerbot.game.api.methods.node.Menu;
  24. import org.powerbot.game.api.methods.node.SceneEntities;
  25. import org.powerbot.game.api.methods.tab.Inventory;
  26. import org.powerbot.game.api.methods.tab.Skills;
  27. import org.powerbot.game.api.methods.widget.Camera;
  28. import org.powerbot.game.api.util.Filter;
  29. import org.powerbot.game.api.util.Random;
  30. import org.powerbot.game.api.util.Time;
  31. import org.powerbot.game.api.util.Timer;
  32. import org.powerbot.game.api.wrappers.Area;
  33. import org.powerbot.game.api.wrappers.Tile;
  34. import org.powerbot.game.api.wrappers.interactive.NPC;
  35. import org.powerbot.game.api.wrappers.node.Item;
  36. import org.powerbot.game.api.wrappers.widget.WidgetChild;
  37. import org.powerbot.game.bot.event.MessageEvent;
  38. import org.powerbot.game.bot.event.listener.MessageListener;
  39. import org.powerbot.game.bot.event.listener.PaintListener;
  40.  
  41. @Manifest(name = "Barbarian Village Fly Fisher", authors = { "Battleguard" }, description= "Fly Fishes at Barbarian Village Only.")
  42. public class FlyFisher extends ActiveScript implements PaintListener, MessageListener, MouseListener, MouseMotionListener {
  43.    
  44.     private final Timer RUN_TIME = new Timer(0);   
  45.    
  46.     private boolean hop = false;
  47.    
  48.     private int startXp;
  49.     private int startLvl;
  50.     private int fishCaught = 0;
  51.    
  52.     public Dropper r;
  53.     public FishDraw fishDraw;
  54.    
  55.    
  56.     @Override
  57.     protected void setup() {
  58.         startXp = Skills.getExperience(Skills.FISHING) - 1;
  59.         startLvl = Skills.getLevel(Skills.FISHING);
  60.        
  61.         setupRectangles();
  62.        
  63.         r = new Dropper();
  64.         Fisher m = new Fisher();
  65.         Cam c = new Cam(); 
  66.         StopCheck s = new StopCheck();
  67.         fishDraw = new FishDraw();
  68.        
  69.         provide(new Strategy(r, r));
  70.         provide(new Strategy(m, m));
  71.         provide(new Strategy(c, c));
  72.         provide(new Strategy(s, s));
  73.         provide(new Strategy(fishDraw, fishDraw));     
  74.     }
  75.    
  76.    
  77.     public class PlaceCannon extends Strategy implements Task{
  78.  
  79.         @Override
  80.         public void run() {
  81.             final Tile base = Players.getLocal().getLocation();
  82.            
  83.        
  84.            
  85.         }
  86.        
  87.         public Area getCannonSpot(final Tile base) {
  88.             for(int i = 1; i < 5; i++) {       
  89.                 final Rectangle spots = new Rectangle(base.getX() - i, base.getY() - i, i * 2 + 1, i * 2 + 1);         
  90.                 for(int y = spots.y; y < spots.y + spots.height; y++) {
  91.                     p1: for(int x = spots.x; y < spots.x + spots.width; x++) {
  92.                         final Area cannon = new Area(new Tile(base.getX() - 1, base.getY() - 1, 0), new Tile(base.getX() + 1, base.getY() + 1, 0));
  93.                         final Tile[] tiles = cannon.getTileArray();
  94.                         for (Tile tile : tiles) {
  95.                             if(SceneEntities.getAt(tile) != null)  continue p1;
  96.                         }
  97.                         return cannon;
  98.                     }
  99.                 }
  100.             }
  101.             return null;
  102.         }
  103.        
  104.         @Override
  105.         public boolean validate() {
  106.             return false;
  107.         }
  108.        
  109.     }
  110.    
  111.     public class Dropper extends Strategy implements Task{
  112.  
  113.         @Override
  114.         public void run() {
  115.             dropdown();
  116.         }
  117.        
  118.         @Override
  119.         public boolean validate(){
  120.             if(Tabs.getCurrent() != Tabs.INVENTORY) openInventoryTab();    
  121.             return  Inventory.getCount() == 28;        
  122.         }
  123.  
  124.         void dropdown() {          
  125.             final WidgetChild inv = Widgets.get(679, 0);
  126.             for(int x = 0; x < 4; x++) {
  127.                 for(int y = x; y < 28; y+=4) {
  128.                     final WidgetChild spot = inv.getChild(y);
  129.                     if(spot != null && spot.getChildId() != 314) {
  130.                         clickFish(spot);
  131.                     }
  132.                 }
  133.             }
  134.         }
  135.                
  136.         public void clickFish(final WidgetChild fish) {
  137.             if(!fish.getBoundingRectangle().contains(Mouse.getLocation())) {
  138.                 Move(fish.getCentralPoint().x, fish.getAbsoluteY() + 5, hop);
  139.             }
  140.             Mouse.click(false);
  141.             Move(Mouse.getX(), getDropActionYLocation(), hop);
  142.             Mouse.click(true);
  143.         }
  144.        
  145.         public void Move(final int x, final int y, final boolean Hop) {
  146.             if(Hop) Mouse.hop(x, y, 3, 3);
  147.             else    Mouse.move(x, y, 3, 3);
  148.         }
  149.        
  150.         public int getDropActionYLocation() {
  151.             final String []actions = Menu.getItems();
  152.             for(int i = 0; i < actions.length; i++) {
  153.                 if(actions[i].contains("Drop ")) {
  154.                     return Menu.getLocation().y + 21 + 16 * i + Random.nextInt(3, 6);
  155.                 }
  156.             }
  157.             return Menu.getLocation().y + 40;
  158.         }
  159.        
  160.         public void openInventoryTab() {
  161.             final WidgetChild inv = Widgets.get(548, 93);
  162.             Time.sleep(2000);
  163.             if(inv != null) Widgets.get(548, 93).click(true);
  164.             Time.sleep(500);
  165.            
  166.         }
  167.     }
  168.    
  169.    
  170.     public final class StopCheck extends Strategy implements Task{
  171.        
  172.         @Override
  173.         public void run() {
  174.             stop();
  175.         }
  176.        
  177.         @Override
  178.         public boolean validate(){
  179.             return Inventory.getCount(314) == 0;           
  180.         }
  181.     }
  182.    
  183.    
  184.     public final class Fisher extends Strategy implements Task{
  185.        
  186.         public final Filter<NPC> FishSpots = new Filter<NPC>() {
  187.             public boolean accept(NPC npc) {
  188.                 return npc.getId() == 328 || npc.getId() == 9645;
  189.             }
  190.         };
  191.        
  192.         @Override
  193.         public void run() {
  194.             final NPC BestSpot = NPCs.getNearest(FishSpots);
  195.             if(BestSpot.isOnScreen()) {
  196.                 BestSpot.interact("Lure");
  197.             } else {
  198.                 Walking.walk(BestSpot.getLocation());
  199.             }
  200.             Time.sleep(2000);
  201.         }
  202.        
  203.         @Override
  204.         public boolean validate(){
  205.             return Players.getLocal().getAnimation() == -1;
  206.         }  
  207.        
  208.     }
  209.    
  210.    
  211.     public final class Cam extends Strategy implements Task{   
  212.        
  213.         private Timer cameraTimer  = new Timer(2 * 60 * 1000);
  214.        
  215.         @Override
  216.         public void run() {
  217.             Camera.setPitch(Random.nextInt(70, 100));
  218.             Camera.setAngle(Random.nextInt(0, 180));
  219.             cameraTimer = new Timer(Random.nextInt(180, 240) * 1000);
  220.         }
  221.        
  222.         @Override
  223.         public boolean validate(){
  224.             return !cameraTimer.isRunning();       
  225.         }
  226.     }
  227.    
  228.     public final class FishDraw extends Strategy implements Task {
  229.        
  230.         public boolean draw = false;       
  231.         public ArrayList<Integer> spots = new ArrayList<Integer>();            
  232.         private static final int salmonID = 331;
  233.        
  234.        
  235.         public final Filter<Item> salmonFilter = new Filter<Item>() {
  236.             public boolean accept(Item inv) {
  237.                 return inv.getId() == salmonID;
  238.             }
  239.         };
  240.        
  241.         @Override
  242.         public void run() {
  243.             draw = false;                      
  244.             ArrayList<Integer> currentSalmon = new ArrayList<Integer>();
  245.            
  246.             for(int i = 0; i < 28; i++) {
  247.                 if(Inventory.getItemAt(i).getId() == salmonID) {
  248.                     if(spots.contains(i)) spots.remove(spots.indexOf(i));
  249.                     else currentSalmon.add(i);
  250.                 }
  251.             }
  252.            
  253.             for(int i = 0; i < spots.size() && !currentSalmon.isEmpty(); i++) {
  254.                 final Point dragPoint = Widgets.get(679, 0).getChild(spots.get(i)).getCentralPoint();
  255.                 final Point clickPoint = Inventory.getItemAt(currentSalmon.remove(0)).getWidgetChild().getCentralPoint();              
  256.                 r.Move(clickPoint.x, clickPoint.y, false);             
  257.                 Mouse.drag(dragPoint);
  258.             }
  259.             spots.clear();         
  260.         }
  261.        
  262.         @Override
  263.         public boolean validate(){
  264.             return draw && !spots.isEmpty();
  265.         }
  266.     }
  267.  
  268.  
  269.     private NumberFormat k = new DecimalFormat("###,###,###");
  270.    
  271.     @Override
  272.     public void onRepaint(Graphics g) {            
  273.         final int CUR_XP = Skills.getExperience(Skills.FISHING);
  274.         final int CUR_XP_GAIN = CUR_XP - startXp;
  275.         final double XP_PER_HOUR = (((double) CUR_XP_GAIN / (double) RUN_TIME.getElapsed()) * 3600000d);       
  276.         final int CUR_LVL = Skills.getLevel(Skills.FISHING);
  277.        
  278.         final double Percentage = (double)((double)(CUR_XP - Skills.XP_TABLE[CUR_LVL]) / (double)(Skills.XP_TABLE[CUR_LVL + 1] - Skills.XP_TABLE[CUR_LVL]));
  279.         final long TTL = (long) ((RUN_TIME.getElapsed() * (Skills.XP_TABLE[CUR_LVL+1] - CUR_XP)) / CUR_XP_GAIN);
  280.        
  281.         final Color OPAQUE_BLACK = new Color(0, 0, 0, 160);
  282.         final Color OPAQUE_BLACK2 = new Color(0, 0, 0, 180);
  283.         final Color GREEN = new Color(85, 110, 36, 180);
  284.                
  285.         int yComp = 277, xComp = 20;
  286.         g.setColor(OPAQUE_BLACK);
  287.         g.fill3DRect(0, yComp+=18, 290, 45, true);
  288.         g.setFont(new Font("Gayatri", 0, 12));     
  289.         final String l1 = "Run Time: " + RUN_TIME.toElapsedString() + " || Fish Caught: " + k.format(fishCaught);
  290.         shadowText(g, l1, xComp, yComp+=18);
  291.         g.setColor(Color.BLACK);
  292.         g.draw3DRect(xComp-5, yComp+5, 270, 17, true);
  293.         g.setColor(OPAQUE_BLACK2);
  294.         g.fill3DRect(xComp-5, yComp+5, 270, 17, true);
  295.         g.setColor(GREEN);
  296.         g.fillRect(xComp-4, yComp+6, (int) (268 * Percentage), 15);
  297.         final String l2 = "Fishing (" + CUR_LVL + ")" +  (CUR_LVL == startLvl ? "" : "+" + (CUR_LVL - startLvl))
  298.                 + " " + formatter(CUR_XP_GAIN) + " - " + formatter((int)XP_PER_HOUR) + "/H - TTL: " + Time.format(TTL);
  299.         shadowText(g, l2, xComp, yComp+=18);
  300.         drawMouse(g);
  301.         drawButton(g);
  302.         drawInventory(g);
  303.     }
  304.    
  305.     public void drawInventory(Graphics g) {
  306.         g.setColor(Color.BLUE);
  307.         for(int i = 0; i < fishDraw.spots.size(); i++) {
  308.             g.drawRect(invSpots[fishDraw.spots.get(i)].x, invSpots[fishDraw.spots.get(i)].y, invSpots[fishDraw.spots.get(i)].width, invSpots[fishDraw.spots.get(i)].height);
  309.         }
  310.     }
  311.    
  312.     public void drawMouse(Graphics g) {
  313.         g.setColor(Mouse.isPressed() ? Color.RED : Color.GREEN);
  314.         final Point m = Mouse.getLocation();
  315.         g.drawLine(m.x -5, m.y + 5, m.x + 5, m.y - 5);
  316.         g.drawLine(m.x -5, m.y - 5, m.x + 5, m.y + 5);     
  317.     }
  318.    
  319.     private final Rectangle REC = new Rectangle(372, 345, 125, 15);
  320.    
  321.     public void drawButton(Graphics g) {
  322.         g.setColor(Color.BLACK);
  323.         g.drawRect(REC.x-1, REC.y-1, REC.width+1, REC.height+1);
  324.         g.setColor(hop ? new Color(85, 110, 36, 180): new Color(153, 0, 36, 180));     
  325.         g.fillRect(REC.x, REC.y, REC.width, REC.height);       
  326.         g.setColor(Color.WHITE);
  327.         shadowText(g, "Fast Drop: " + (hop ? "Enabled" : "Disabled"), REC.x + 3, REC.y + 13);
  328.     }
  329.    
  330.     public void shadowText(Graphics g, final String line, final int x, final int y) {
  331.         g.setColor(Color.BLACK);
  332.         g.drawString(line, x+1, y+1);
  333.         g.setColor(Color.WHITE);
  334.         g.drawString(line, x, y);      
  335.     }
  336.    
  337.     public String formatter(final int num) {
  338.         return num / 1000 + "." + (num % 1000) / 100 + "K";
  339.     }
  340.  
  341.     @Override
  342.     public void messageReceived(MessageEvent e) {
  343.         if(e.getMessage().contains("You catch a ")) fishCaught++;      
  344.     }  
  345.    
  346.     @Override
  347.     public void mouseClicked(MouseEvent e) {
  348.         if(REC.contains(e.getPoint())) hop ^= true;
  349.     }
  350.  
  351.     @Override
  352.     public void mousePressed(MouseEvent e) {}
  353.  
  354.     @Override
  355.     public void mouseReleased(MouseEvent e) {
  356.         fishDraw.draw = true;
  357.     }
  358.  
  359.     @Override
  360.     public void mouseEntered(MouseEvent e) {}
  361.  
  362.     @Override
  363.     public void mouseExited(MouseEvent e) {}
  364.    
  365.     private Rectangle [] invSpots = new Rectangle[28];
  366.    
  367.     void setupRectangles() {
  368.         WidgetChild inv = Widgets.get(679, 0);
  369.         for(int i = 0; i < 28; i++) {
  370.             invSpots[i] = inv.getChild(i).getBoundingRectangle();
  371.         }
  372.     }
  373.    
  374.     @Override
  375.     public void mouseDragged(MouseEvent e) {       
  376.         final Point p = e.getPoint();
  377.         if(Inventory.getWidget(false).contains(p)) {
  378.             for(int i = 0; i < 28; i++) {
  379.                 if(invSpots[i].contains(p)) {
  380.                     for(int j = 0; j < fishDraw.spots.size(); j++) {
  381.                         if(fishDraw.spots.get(j) == i) return;
  382.                     }
  383.                     fishDraw.spots.add(i);
  384.                 }
  385.             }
  386.         }
  387.     }
  388.  
  389.     @Override
  390.     public void mouseMoved(MouseEvent e) {
  391.        
  392.     }
  393. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement