Advertisement
Glaciation96

RuneScape fishing script (Bot) - Rewrite in progress

Nov 13th, 2018
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.62 KB | None | 0 0
  1. package klobWebPack; //I will be rewriting this in Java 8 once I've amassed the necessary knowledge on streams & lambdas  
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics2D;
  5. import java.awt.Point;
  6. import java.awt.event.KeyEvent;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. //Build path external jars/Client API imports
  11. import org.osbot.rs07.api.map.Area;
  12. import org.osbot.rs07.api.map.Position;
  13. import org.osbot.rs07.api.model.GroundItem;
  14. import org.osbot.rs07.api.model.Item;
  15. import org.osbot.rs07.api.model.NPC;
  16. import org.osbot.rs07.api.model.RS2Object;
  17. import org.osbot.rs07.api.ui.Skill;
  18. import org.osbot.rs07.script.Script;
  19. import org.osbot.rs07.script.ScriptManifest;
  20. import org.osbot.rs07.utility.ConditionalSleep;
  21.  
  22. @ScriptManifest(author = "Glaciation96", info = "SecondAttempt", name = "KlobsterWebWalk", version = 0, logo = "")
  23.  
  24. final class Sleep extends ConditionalSleep {
  25.  
  26.     private final BooleanSupplier condition;
  27.  
  28.     public Sleep(final BooleanSupplier condition, final int timeout) {
  29.         super(timeout);
  30.         this.condition = condition;
  31.     }
  32.  
  33.     public Sleep(final BooleanSupplier condition, final int timeout, final int interval) {
  34.         super(timeout, interval);
  35.         this.condition = condition;
  36.     }
  37.  
  38.     @Override
  39.     public final boolean condition() throws InterruptedException {
  40.         return condition.getAsBoolean();
  41.     }
  42.  
  43.     public static boolean sleepUntil(final BooleanSupplier condition, final int timeout) {
  44.         return new Sleep(condition, timeout).sleep();
  45.     }
  46.  
  47.     public static boolean sleepUntil(final BooleanSupplier condition, final int timeout, final int interval) {
  48.         return new Sleep(condition, timeout, interval).sleep();
  49.     }
  50. }
  51.  
  52. public class LobsterWebWalk extends Script {
  53.  
  54.     private fishInterpolation fishDelay = new fishInterpolation(random(20, 30), random(1800, 2300));
  55.  
  56.     public final Area KaramFishingSpot = new Area(2921, 3174, 2927, 3180);
  57.     public final Area KaramHarbour = new Area(2950, 3147, 2957, 3146);
  58.     public final Area PortSarim = new Area(3026, 3222, 3029, 3213);
  59.     public final Area DepositBox = new Area(3044, 3237, 3052, 3234);
  60.     public final Area b4DepositBox = new Area(3041, 3238, 3043, 3234);
  61.     public final Area karamBoat = new Area(2955, 3141, 2957, 3144).setPlane(1);
  62.     public final Area sarimBoat = new Area(3038, 3210, 3031, 3223).setPlane(1);
  63.     public final Area DraynorBank = new Area(3092, 3246, 3096, 3241);
  64.  
  65.     private karamFishing levelFishType;
  66.     volatile static boolean hasInteracted = false;
  67.     boolean breakFishing = false;
  68.     boolean isPoor = false;
  69.     long currentlyIdledFor;
  70.     long lastActionTime;
  71.  
  72.     public final String[] dank = { "Raw lobster", "Raw swordfish", "Lobster", "Swordfish" };
  73.     public final String[] SeamenOption = { "Yes please." };
  74.     public final String[] CustomsOptions = { "Can I journey on this ship?", "Search away, I have nothing to hide.",
  75.             "Ok." };
  76.  
  77.     private enum fishState {
  78.         fish, invFullTrash, invFullLobs, idle;
  79.     }
  80.  
  81.     private enum karamFishing {
  82.         smallNetFishing(new String[] { "Small fishing net", "Coins" }, "Net", 1521, 621),
  83.         baitFishing(new String[] { "Fishing rod", "Fishing bait", "Coins" }, "Bait", 1521, 623),
  84.         cageFishing(new String[] { "Lobster pot", "Coins" }, "Cage", 1522, 619);
  85.  
  86.         private final String[] tool;
  87.         private final String action;
  88.         private final int fishingSpot;
  89.         private final int fishingAnim;
  90.  
  91.         karamFishing(String[] t, String action, int f, int anim) {
  92.             this.tool = t;
  93.             this.action = action;
  94.             this.fishingSpot = f;
  95.             this.fishingAnim = anim;
  96.         }
  97.  
  98.         public String[] getTool() {
  99.             return tool;
  100.         }
  101.  
  102.         public String getAction() {
  103.             return action;
  104.         }
  105.  
  106.         public int getFishingSpot() {
  107.             return fishingSpot;
  108.         }
  109.  
  110.         public int getFishingAnim() {
  111.             return fishingAnim;
  112.         }
  113.     }
  114.  
  115.     private boolean idleFor(int millis) {
  116.         if (myPlayer().isAnimating() || myPlayer().isMoving()) {
  117.             lastActionTime = System.currentTimeMillis();
  118.         } else {
  119.             currentlyIdledFor = System.currentTimeMillis();
  120.         }
  121.         return lastActionTime + millis < currentlyIdledFor;
  122.     }
  123.  
  124.     @Override
  125.     public void onStart() {
  126.         log("Let's get glitched out!");
  127.         new Thread(fishDelay).start();
  128.     }
  129.  
  130.     // ============================================= onLoop
  131.     @Override
  132.     public int onLoop() throws InterruptedException {
  133.         fishingType();
  134.         if (!doWeHaveEverythingYet()) {
  135.             getNecessities();
  136.         }
  137.         if (getInventory().getAmount("Coins") <= 60) {
  138.             isPoor = true;
  139.             while (isPoor) {
  140.                 moneyMoneyMoneyTeeeam();
  141.             }
  142.         }
  143.         if (KaramFishingSpot.contains(myPosition())) {
  144.             breakFishing = false;
  145.             log("breakFishing = false");
  146.             while (!breakFishing) {
  147.                 switch (getFishState()) {
  148.                 case fish:
  149.                     beginFishing();
  150.                     new ConditionalSleep(1200, (int) (Math.random() * 500 + 250)) {
  151.                         @Override
  152.                         public boolean condition() throws InterruptedException {
  153.                             return !myPlayer().isAnimating();
  154.                         }
  155.                     }.sleep();
  156.                     break;
  157.                 case invFullTrash:
  158.                     dropTrash();
  159.                     break;
  160.                 case invFullLobs:
  161.                     doDeposit();
  162.                     break;
  163.                 case idle:
  164.                     // Do nothing
  165.                     break;
  166.                 }
  167.             }
  168.         } else {
  169.             log("onLoop - Walking to fishing spot...");
  170.             this.walking.webWalk(KaramFishingSpot);
  171.             Sleep.sleepUntil(() -> KaramFishingSpot.contains(myPosition()), (int) (Math.random() * 500 + 250));
  172.         }
  173.         log("We broke out the switch! Looping again...");
  174.         return random(200, 300);
  175.     }
  176.  
  177.     // ============================================= Inventory check
  178.     public boolean doWeHaveEverythingYet() {
  179.         for (String item : levelFishType.getTool()) {
  180.             if (!inventory.contains(item)) {
  181.                 return false;
  182.             }
  183.         }
  184.         return true;
  185.     }
  186.  
  187.     // ============================================= fishingType
  188.     private void fishingType() {
  189.         if (getSkills().getStatic(Skill.FISHING) >= 1 && getSkills().getStatic(Skill.FISHING) < 5) {
  190.             levelFishType = karamFishing.smallNetFishing;
  191.         } else if (getSkills().getStatic(Skill.FISHING) >= 5 && getSkills().getStatic(Skill.FISHING) < 40) {
  192.             levelFishType = karamFishing.baitFishing;
  193.         } else if (getSkills().getStatic(Skill.FISHING) >= 40) {
  194.             levelFishType = karamFishing.cageFishing;
  195.         }
  196.     }
  197.  
  198.     // ============================================= Fishing states
  199.     private fishState getFishState() throws InterruptedException {
  200.         log("Getting fishState...");
  201.         if (!getInventory().isFull() && this.myPlayer().getAnimation() == -1) {
  202.             return fishState.fish;
  203.         } else if (levelFishType.getFishingAnim() == this.myPlayer().getAnimation()) {
  204.             return fishState.fish;
  205.         } else if (getInventory().isFull() && levelFishType == karamFishing.cageFishing) {
  206.             return fishState.invFullLobs;
  207.         } else if (getInventory().isFull() && levelFishType != karamFishing.cageFishing) {
  208.             return fishState.invFullTrash;
  209.         } else {
  210.             return fishState.idle;
  211.         }
  212.     }
  213.  
  214.     // ============================================= Fishing begins!!!
  215.     private void beginFishing() throws InterruptedException {
  216.         log("beginFishing() - Attempting to check if fish exists.");
  217.         GroundItem freeFish = getGroundItems().closest(dank);
  218.         if (freeFish != null && !this.inventory.isFull() && levelFishType == karamFishing.cageFishing) {
  219.             log("beginFishing() - Fish exists, proceeding to pick up...");
  220.             if (getMap().canReach(freeFish)) {
  221.                 int lastCount = getInventory().getEmptySlots();
  222.                 freeFish.interact("Take");
  223.                 new ConditionalSleep(1500, (int) (Math.random() * 500 + 250)) {
  224.                     @Override
  225.                     public boolean condition() throws InterruptedException {
  226.                         return getInventory().getEmptySlots() < lastCount || idleFor(random(3200, 5000));
  227.                     }
  228.                 }.sleep();
  229.             }
  230.         } else if (!this.inventory.isFull() && !myPlayer().isAnimating()) {
  231.             if (fishInterpolation.canInteract) {
  232.                 log("beginFishing() - We are fishing!");
  233.                 NPC fishingSpot = fishingSpot(levelFishType.getFishingSpot());
  234.                 engageFishing(fishingSpot, levelFishType.getAction());
  235.                 hasInteracted = true;
  236.                 sleep(random(35, 50));
  237.             } else {
  238.                 sleep(random(35, 50));
  239.             }
  240.         }
  241.     }
  242.  
  243.     private void engageFishing(NPC fishingSpot, String action) {
  244.         int xFish = random(0, 1);
  245.         log("xFish = " + xFish);
  246.         switch (xFish) {
  247.         case 0:
  248.             if (fishingSpot != null && fishingSpot.exists()) {
  249.                 this.mouse.move(random(100, 500), random(100, 500));
  250.                 fishingSpot.interact(action);
  251.             }
  252.             break;
  253.         case 1:
  254.             if (fishingSpot != null && fishingSpot.exists()) {
  255.                 fishingSpot.hover();
  256.                 this.mouse.click(true);
  257.                 fishingSpot.interact(action);
  258.             }
  259.             break;
  260.         default:
  261.             fishingSpot.interact(action);
  262.         }
  263.     }
  264.  
  265.     // ============================================= Randomised drop pattern
  266.     private void dropTrash() throws InterruptedException {
  267.         int[] invSlotIndex = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  268.                 25, 26, 27, };
  269.  
  270.         funkyDropPattern(invSlotIndex);
  271.         getKeyboard().pressKey(KeyEvent.VK_SHIFT);
  272.         for (int i = 0; i < invSlotIndex.length; i++) {
  273.             Item item = getInventory().getItemInSlot(invSlotIndex[i]);
  274.             if (item != null && item.nameContains("Raw sardine", "Raw herring", "Raw shrimps", "Raw anchovies")) {
  275.                 getInventory().interact(invSlotIndex[i]);
  276.                 sleep(random(20, 30));
  277.             }
  278.         }
  279.         getKeyboard().releaseKey(KeyEvent.VK_SHIFT);
  280.         log("dropTrash - breakFishing = true");
  281.         breakFishing = true;
  282.     }
  283.  
  284.     private static void funkyDropPattern(int[] invSlotIndex) {
  285.         for (int i = 0; i < invSlotIndex.length; i++) {
  286.  
  287.             int s = i + (int) (Math.random() * (invSlotIndex.length - i));
  288.  
  289.             int temp = invSlotIndex[s];
  290.             invSlotIndex[s] = invSlotIndex[i];
  291.             invSlotIndex[i] = temp;
  292.         }
  293.     }
  294.  
  295.     // ============================================= Grabbing the necessary items
  296.     private void getNecessities() throws InterruptedException {
  297.         checkAtBank();
  298.         if (DraynorBank.contains(this.myPosition())) {
  299.             log("getNecessities - At Draynor bank");
  300.             if (!getBank().isOpen()) {
  301.                 while (!getBank().isOpen()) {
  302.                     openBank();
  303.                 }
  304.             }
  305.             if (getBank().isOpen()) {
  306.                 if (getInventory().getEmptySlots() <= 27) {
  307.                     log("getNecessities - Inventory contains less than or equal to 27 free slots");
  308.                     if (getInventory().contains("Coins")) {
  309.                         log("getNecessities - Inventory has coins");
  310.                         int lastCount = getInventory().getEmptySlots();
  311.                         bank.depositAllExcept("Coins");
  312.                         new ConditionalSleep(1500, (int) (Math.random() * 500 + 250)) {
  313.                             @Override
  314.                             public boolean condition() throws InterruptedException {
  315.                                 return getInventory().getEmptySlots() > lastCount;
  316.                             }
  317.                         }.sleep();
  318.                     } else {
  319.                         bank.depositAll();
  320.                         new ConditionalSleep(1500, (int) (Math.random() * 500 + 250)) {
  321.                             @Override
  322.                             public boolean condition() throws InterruptedException {
  323.                                 return getInventory().getEmptySlots() == 28;
  324.                             }
  325.                         }.sleep();
  326.                         bank.withdrawAll("Coins");
  327.                         new ConditionalSleep(1500, (int) (Math.random() * 500 + 250)) {
  328.                             @Override
  329.                             public boolean condition() throws InterruptedException {
  330.                                 return getInventory().getEmptySlots() == 27;
  331.                             }
  332.                         }.sleep();
  333.                     }
  334.                 }
  335.                 log("getNecessities - Checking if 'levelFishType.getTool()' exists in bank...");
  336.                 if (getBank().contains(levelFishType.getTool())) {
  337.                     bank.withdraw(levelFishType.getTool()[0], 1);
  338.                     new ConditionalSleep(1500, (int) (Math.random() * 500 + 250)) {
  339.  
  340.                         @Override
  341.                         public boolean condition() throws InterruptedException {
  342.                             return inventory.contains(levelFishType.getTool()[0]);
  343.                         }
  344.                     }.sleep();
  345.                 } else {
  346.                     log("getNecessities - 'levelFishType.getTool()' does not exist in bank, logging out");
  347.                     exitGame();
  348.                 }
  349.             }
  350.             if (levelFishType.getTool().length > 2) {
  351.                 log("getNecessities - karamFishing.baitFishing checked, withdrawing Fishing bait");
  352.                 if (!getBank().isOpen()) {
  353.                     while (!getBank().isOpen()) {
  354.                         openBank();
  355.                     }
  356.                 }
  357.                 if (getBank().contains(levelFishType.getTool()[1])) {
  358.                     getBank().withdrawAll(levelFishType.getTool()[1]);
  359.                     new ConditionalSleep(1500, (int) (Math.random() * 500 + 250)) {
  360.                         @Override
  361.                         public boolean condition() throws InterruptedException {
  362.                             return inventory.contains(levelFishType.getTool()[1]);
  363.                         }
  364.                     }.sleep();
  365.                 } else {
  366.                     log("getNecessities - Fishing bait does not exist in bank, logging out");
  367.                     exitGame();
  368.                 }
  369.             }
  370.             if (!KaramFishingSpot.contains(myPosition())) {
  371.                 log("getNecessities - Attempting to close bank and return to fishing spot...");
  372.                 while (getBank().isOpen()) {
  373.                     getBank().close();
  374.                 }
  375.                 this.walking.webWalk(KaramFishingSpot);
  376.                 new ConditionalSleep(1500, (int) (Math.random() * 500 + 250)) {
  377.                     @Override
  378.                     public boolean condition() throws InterruptedException {
  379.                         return KaramFishingSpot.contains(myPosition());
  380.                     }
  381.                 }.sleep();
  382.             }
  383.         }
  384.     }
  385.  
  386.     // ============================================= Check if we are at the bank
  387.     private void checkAtBank() {
  388.         if (!DraynorBank.contains(this.myPosition())) {
  389.             log("checkAtBank - Attempting to walk to Draynor bank");
  390.             this.walking.webWalk(DraynorBank);
  391.             new ConditionalSleep(1500, (int) (Math.random() * 500 + 250)) {
  392.                 @Override
  393.                 public boolean condition() throws InterruptedException {
  394.                     return DraynorBank.contains(myPosition());
  395.                 }
  396.             }.sleep();
  397.         }
  398.     }
  399.  
  400.     // ============================================= MONEY MAY ALL DAY
  401.     private boolean moneyMoneyMoneyTeeeam() {
  402.         if (getInventory().getAmount("Coins") < 60) {
  403.             if (DraynorBank.contains(this.myPosition())) {
  404.                 while (!getBank().isOpen()) {
  405.                     openBank();
  406.                 }
  407.                 if (getBank().isOpen()) {
  408.                     getBank().withdrawAll("Coins");
  409.                     new ConditionalSleep(1500, (int) (Math.random() * 500 + 250)) {
  410.                         @Override
  411.                         public boolean condition() throws InterruptedException {
  412.                             return getInventory().getAmount("Coins") > 60;
  413.                         }
  414.                     }.sleep();
  415.                 }
  416.             } else {
  417.                 checkAtBank();
  418.                 return isPoor = true;
  419.             }
  420.         }
  421.         return isPoor = false;
  422.     }
  423.  
  424.     // ============================================= Open bank
  425.     private boolean openBank() {
  426.         if (!getBank().isOpen()) {
  427.             log("Attempting to open bank");
  428.             RS2Object bankBooth = this.getObjects().closest("Bank booth");
  429.             if (bankBooth != null && bankBooth.exists()) {
  430.                 bankBooth.interact("Bank");
  431.                 new ConditionalSleep(1500, (int) (Math.random() * 500 + 250)) {
  432.                     @Override
  433.                     public boolean condition() throws InterruptedException {
  434.                         return getBank().isOpen();
  435.                     }
  436.                 }.sleep();
  437.             } else {
  438.                 log("We are not at bank, attempting to reach destination");
  439.                 this.walking.webWalk(DraynorBank);
  440.                 new ConditionalSleep(1500, (int) (Math.random() * 500 + 250)) {
  441.                     @Override
  442.                     public boolean condition() throws InterruptedException {
  443.                         return DraynorBank.contains(myPosition());
  444.                     }
  445.  
  446.                 }.sleep();
  447.                 return !getBank().isOpen();
  448.             }
  449.         }
  450.         return getBank().isOpen();
  451.     }
  452.  
  453.     // ============================================= Depositing at deposit box
  454.     private void doDeposit() {
  455.         if (!DepositBox.contains(this.myPosition())) {
  456.             this.walking.webWalk(DepositBox);
  457.             new ConditionalSleep(1500, (int) (Math.random() * 500 + 250)) {
  458.                 @Override
  459.                 public boolean condition() throws InterruptedException {
  460.                     return b4DepositBox.contains(myPosition());
  461.                 }
  462.             }.sleep();
  463.         }
  464.         log("doDeposit - Searching for Deposit Box...");
  465.         while (!this.depositBox.isOpen()) {
  466.             RS2Object Dbox = getObjects().closest("Bank deposit box");
  467.             if (Dbox != null && Dbox.exists()) {
  468.                 Dbox.interact("Deposit");
  469.                 new ConditionalSleep(2000, (int) (Math.random() * 500 + 250)) {
  470.                     @Override
  471.                     public boolean condition() throws InterruptedException {
  472.                         return depositBox.isOpen();
  473.                     }
  474.                 }.sleep();
  475.             }
  476.         }
  477.         log("Attempting to deposit goods...");
  478.         if (this.depositBox.isOpen() && getInventory().isFull()) {
  479.             this.depositBox.depositAllExcept(levelFishType.getTool());
  480.             new ConditionalSleep(2000, (int) (Math.random() * 500 + 250)) {
  481.                 @Override
  482.                 public boolean condition() throws InterruptedException {
  483.                     return inventory.isEmptyExcept(levelFishType.getTool());
  484.                 }
  485.             }.sleep();
  486.             this.depositBox.close();
  487.         }
  488.     }
  489.  
  490.     // ============================================= Create fishing spot to call
  491.     private NPC fishingSpot(int s) {
  492.         NPC fishingSpot = this.npcs.closest(s);
  493.         return fishingSpot;
  494.     }
  495.  
  496.     // ============================================= Something's wrong. Exit game :(
  497.     private void exitGame() {
  498.         logoutTab.logOut();
  499.     }
  500.  
  501.     @Override
  502.     public void onExit() {
  503.         new Thread(fishDelay).interrupt();
  504.         log("Houston, we have a problem...");
  505.     }
  506.  
  507.     @Override
  508.     public void onPaint(Graphics2D g) {
  509.         Point mP = getMouse().getPosition();
  510.         g.setColor(Color.WHITE);
  511.         g.drawLine(mP.x - 3, mP.y - 3, mP.x - 3, mP.y + 3);
  512.         g.drawLine(mP.x - 3, mP.y - 3, mP.x + 3, mP.y - 3);
  513.         g.drawLine(mP.x - 3, mP.y + 3, mP.x + 3, mP.y + 3);
  514.         g.drawLine(mP.x + 3, mP.y + 3, mP.x + 3, mP.y - 3);
  515.     }
  516. }
  517.  
  518. class fishInterpolation implements Runnable {
  519.  
  520.     volatile static boolean canInteract = false;
  521.     int delayTimer;
  522.     int randomTimer;
  523.  
  524.     public fishInterpolation(int randomTimer, int delayTimer) {
  525.         this.delayTimer = delayTimer;
  526.         this.randomTimer = randomTimer;
  527.     }
  528.  
  529.     public void run() {
  530.         //This is very bad practice... Should avoid (Still works)
  531.         for (int i = 0; i < 1; i = 0) {
  532.             while (LobsterWebWalk.hasInteracted == false) {
  533.                 canInteract = true;
  534.                 try {
  535.                     Thread.sleep(randomTimer);
  536.                 } catch (InterruptedException e) {
  537.                     System.out.println("This line will never run..." + e);
  538.                 }
  539.             }
  540.             canInteract = false;
  541.             LobsterWebWalk.hasInteracted = false;
  542.             try {
  543.                 Thread.sleep(delayTimer);
  544.             } catch (InterruptedException e) {
  545.                 System.out.println("This line will never run... Hopefully" + e);
  546.             }
  547.             canInteract = true;
  548.         }
  549.     }
  550. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement