1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Bug My bot just walks between 2 tiles. I can't figure out why.

Discussion in 'Developer Support' started by Sinatra, Nov 4, 2015.

  1. Sinatra

    Joined:
    Nov 2, 2015
    Messages:
    17
    Likes Received:
    1
    So my bot is meant to run from Burthorpe bank down to the flax pen, pick a whole inventory and return back and bank them.

    Whenever I load the bot up it just runs just a little north of the bank then south then back north then south in an endless cycle.

    I'm sure it's a minor thing. I used AesBerries old script bot as a model and made a note to re-type it all out so I can get a better understanding of how it's working. Although this has me puzzled.

    Code (Text):
    1. package com.sinatra.bots.sFlaxNSpin;
    2.  
    3. import com.runemate.game.api.client.paint.PaintListener;
    4. import com.runemate.game.api.hybrid.entities.GameObject;
    5. import com.runemate.game.api.hybrid.entities.Player;
    6. import com.runemate.game.api.hybrid.entities.definitions.ItemDefinition;
    7. import com.runemate.game.api.hybrid.local.hud.interfaces.Bank;
    8. import com.runemate.game.api.hybrid.local.hud.interfaces.Inventory;
    9. import com.runemate.game.api.hybrid.location.Area;
    10. import com.runemate.game.api.hybrid.location.Coordinate;
    11. import com.runemate.game.api.hybrid.location.navigation.Traversal;
    12. import com.runemate.game.api.hybrid.location.navigation.basic.BresenhamPath;
    13. import com.runemate.game.api.hybrid.location.navigation.web.WebPath;
    14. import com.runemate.game.api.hybrid.region.GameObjects;
    15. import com.runemate.game.api.hybrid.region.Players;
    16. import com.runemate.game.api.hybrid.util.StopWatch;
    17. import com.runemate.game.api.hybrid.util.calculations.CommonMath;
    18. import com.runemate.game.api.script.Execution;
    19. import com.runemate.game.api.script.framework.LoopingScript;
    20. import com.runemate.game.api.script.framework.listeners.InventoryListener;
    21. import com.runemate.game.api.script.framework.listeners.events.ItemEvent;
    22.  
    23. import java.awt.*;
    24. import java.util.concurrent.TimeUnit;
    25.  
    26. public class sFlaxNSpin extends LoopingScript implements PaintListener, InventoryListener {
    27.  
    28.     private final static Player player = Players.getLocal();
    29.     private final static StopWatch runtime = new StopWatch();
    30.     private final static Area flaxArea = new Area.Rectangular(new Coordinate(2886, 3464, 0), new Coordinate(2886, 3460, 0));
    31.     private final static Area bankArea = new Area.Rectangular(new Coordinate(2888, 3536, 0), new Coordinate(2888, 3538, 0));
    32.     private static int pickedFlax = 0;
    33.  
    34.     @Override
    35.     public void onStart(String... args) {
    36.         getEventDispatcher().addListener(this);
    37.         setLoopDelay(250, 750);
    38.         runtime.start();
    39.     }
    40.  
    41.     @Override
    42.     public void onLoop() {
    43.         GameObject flax = GameObjects.getLoaded(67263, 67264, 67265).nearestTo(player);
    44.  
    45.         if (Inventory.isFull() && flaxArea.contains(player) && isIdle()) { // Picking Flax
    46.             if (flax != null && flax.isValid()) {
    47.                 flax.interact("Pick");
    48.             }
    49.         } else if (!Inventory.isFull() && !bankArea.contains(player)) { // Walking to Bank
    50.             final WebPath bankPath = Traversal.getDefaultWeb().getPathBuilder().buildTo(bankArea.getCenter());
    51.             final BresenhamPath failBankPath = BresenhamPath.buildTo(bankArea.getCenter());
    52.  
    53.             if (bankPath != null) {
    54.                 bankPath.step();
    55.             } else if (failBankPath != null) {
    56.                 failBankPath.step();
    57.             }
    58.         } else if (Inventory.isFull() && bankArea.contains(player)) { //Banking
    59.             if (!Bank.isOpen()) {
    60.                 Bank.open();
    61.                 Execution.delay(250, 450);
    62.             } else {
    63.                 Bank.depositInventory();
    64.                 Execution.delay(200, 500);
    65.             }
    66.         } else if (!Inventory.isFull() && !flaxArea.contains(player)) { // Walking to Flax
    67.             final WebPath flaxPath = Traversal.getDefaultWeb().getPathBuilder().buildTo(flaxArea.getCenter());
    68.             final BresenhamPath failFlaxPath = BresenhamPath.buildTo(flaxArea.getCenter());
    69.  
    70.             if (flaxPath != null) {
    71.                 flaxPath.step();
    72.             } else if (failFlaxPath != null) {
    73.                 failFlaxPath.step();
    74.             }
    75.         }
    76.     }
    77.  
    78.  
    79.     @Override
    80.     public void onPaint(Graphics2D g) {
    81.         Color transBlack = new Color(0, 0, 0, 150);
    82.         //Draw trans rect
    83.         g.setColor(transBlack);
    84.         g.fillRect(0, 0, 150, 70);
    85.         //Draw border of rect
    86.         g.setColor(Color.blue);
    87.         g.drawRect(0, 0, 150, 70);
    88.         //Draw green underline under title
    89.         g.drawLine(5, 20, 145, 20);
    90.         //Draw text
    91.         g.setColor(Color.white);
    92.         g.drawString("sFlaxNSpin", 50, 15);
    93.         g.drawString("Run time: " + runtime.getRuntimeAsString(), 5, 35);
    94.         g.drawString("Picked Flax: " + pickedFlax + " ,P/H: " + (int) CommonMath.rate(TimeUnit.HOURS, runtime.getRuntime(), pickedFlax), 5, 50);
    95.         g.drawString("Bowstring Strung: SOON", 5, 65);
    96.     }
    97.  
    98.     @Override
    99.     public void onItemAdded(ItemEvent event) {
    100.         ItemDefinition definition = event.getItem().getDefinition();
    101.  
    102.         if (definition != null && definition.getName().equals("Flax")) {
    103.             ++pickedFlax;
    104.         }
    105.     }
    106.  
    107.     private boolean isIdle() {
    108.         return player.getAnimationId() == -1 && !player.isMoving();
    109.     }
    110.  
    111. }
    112.  
     
    #1 Sinatra, Nov 4, 2015
    Last edited: Nov 4, 2015
  2. waxyboy98

    Joined:
    Oct 29, 2015
    Messages:
    6
    Likes Received:
    2
    It's because your logic is off. You want the bot to walk back when Inventory.isFull() not !Inventory.isFull(). Also it is because it is trying to walk to the bank and walk to the flax area one after the other so it will keep going back and forth. so you should probably make an enum to control the state of the bot. Then you could use a switch statement to determine where you want it to walk.
     
  3. Sinatra

    Joined:
    Nov 2, 2015
    Messages:
    17
    Likes Received:
    1
    Ahh okay I understand. Where would I be using !Inventory.isFull() then in most cases? I'm not sure what the symbol '!' is doing in my code, I'm trying to figure that out.

    As for making an enum could you expand on that a little bit? I'm not after the resolution to my issue but rather an example on how it works that I can dissect and learn from.
     
  4. DarkenedSoul

    Joined:
    Oct 2, 2015
    Messages:
    31
    Likes Received:
    5
    I gotchu fam hang on
    Code (Text):
    1. package com.sinatra.bots.sFlaxNSpin;
    2.  
    3.  
    4. public class sFlaxNSpin extends LoopingScript implements PaintListener, InventoryListener {
    5.  
    6.     @Override
    7.     public void onLoop() {
    8.         if (!Inventory.isFull() && !bankArea.contains(player)) { // Walking to Bank
    9.             final WebPath bankPath = Traversal.getDefaultWeb().getPathBuilder().buildTo(bankArea.getCenter());
    10.             final BresenhamPath failBankPath = BresenhamPath.buildTo(bankArea.getCenter());
    11.  
    12.             if (bankPath != null) {
    13.                 bankPath.step();
    14.             } else if (failBankPath != null) {
    15.                 failBankPath.step();
    16.             }
    17.         } else if (!Inventory.isFull() && !flaxArea.contains(player)) { // Walking to Flax
    18.             final WebPath flaxPath = Traversal.getDefaultWeb().getPathBuilder().buildTo(flaxArea.getCenter());
    19.             final BresenhamPath failFlaxPath = BresenhamPath.buildTo(flaxArea.getCenter());
    20.  
    21.             if (flaxPath != null) {
    22.                 flaxPath.step();
    23.             } else if (failFlaxPath != null) {
    24.                 failFlaxPath.step();
    25.             }
    26.         }
    27.     }
    28.  
    29.     }    
    30.  

    due to your logic your script bot is basically only just spamming this.
    Also nice to see you here too ^_^

    You're pretty much just telling it, if its not at the bank, walk to the bank, and if it is, walk to the flax.
    So the moment it tries to walk to the flax and leaves the bank it'll walk back in


    EDIT: "!" on a booleans means NOT so it's like NOTinventory.isFull()
    AKA your script bot would work if you flipped the !'s on your inventory checks
     
  5. Sinatra

    Joined:
    Nov 2, 2015
    Messages:
    17
    Likes Received:
    1
    Ahh silly mistake of mine I guess.

    Is Inventory.isEmpty a better alternative then? Or is my bot not actually getting that far?

    Code (Text):
    1.     @Override
    2.     public void onLoop() {
    3.         GameObject flax = GameObjects.getLoaded(67263, 67264, 67265).nearestTo(player);
    4.  
    5.         if (Inventory.isFull() && flaxArea.contains(player) && isIdle()) { // Picking Flax
    6.             if (flax != null && flax.isValid()) {
    7.                 flax.interact("Pick");
    8.             }
    9.         } else if (Inventory.isFull() && bankArea.contains(player)) { // Walking to Bank
    10.             final WebPath bankPath = Traversal.getDefaultWeb().getPathBuilder().buildTo(bankArea.getCenter());
    11.             final BresenhamPath failBankPath = BresenhamPath.buildTo(bankArea.getCenter());
    12.  
    13.             if (bankPath != null) {
    14.                 bankPath.step();
    15.             } else if (failBankPath != null) {
    16.                 failBankPath.step();
    17.             }
    18.         } else if (Inventory.isFull() && bankArea.contains(player)) { //Banking
    19.             if (!Bank.isOpen()) {
    20.                 Bank.open();
    21.                 Execution.delay(250, 450);
    22.             } else {
    23.                 Bank.depositInventory();
    24.                 Execution.delay(200, 500);
    25.             }
    26.         } else if (Inventory.isEmpty() && flaxArea.contains(player)) { // Walking to Flax
    27.             final WebPath flaxPath = Traversal.getDefaultWeb().getPathBuilder().buildTo(flaxArea.getCenter());
    28.             final BresenhamPath failFlaxPath = BresenhamPath.buildTo(flaxArea.getCenter());
    29.  
    30.             if (flaxPath != null) {
    31.                 flaxPath.step();
    32.             } else if (failFlaxPath != null) {
    33.                 failFlaxPath.step();
    34.             }
    35.         }
    36.     }
     
  6. waxyboy98

    Joined:
    Oct 29, 2015
    Messages:
    6
    Likes Received:
    2
    Ok, so what ! does is it is the opposite of whatever it returns by default so it is searching for if inventory is not full, but you want the bot to walk back when it is full. Also you should make a boolean that gets activated when it reaches the area and check if the boolean is false in the else ifs.
    --- Double Post Merged, Nov 4, 2015, Original Post Date: Nov 4, 2015 ---

    Code (Text):
    1. public boolean atbank=false;
    2. @Override
    3.     public void onLoop() {
    4.         GameObject flax = GameObjects.getLoaded(67263, 67264, 67265).nearestTo(player);
    5.  
    6.         if (!Inventory.isFull() && flaxArea.contains(player) && isIdle()) { // Picking Flax
    7.             if (flax != null && flax.isValid()) {
    8.                 flax.interact("Pick");
    9.             }
    10.         }else if (Inventory.isFull() && !bankArea.contains(player) && atbank==false) { // Walking to Bank
    11.             final WebPath bankPath = Traversal.getDefaultWeb().getPathBuilder().buildTo(bankArea.getCenter());
    12.             final BresenhamPath failBankPath = BresenhamPath.buildTo(bankArea.getCenter());
    13.  
    14.             if (bankPath != null) {
    15.                 bankPath.step();
    16.             } else if (failBankPath != null) {
    17.                 failBankPath.step();
    18.             }
    19.         } else if (Inventory.isFull() && bankArea.contains(player)) { //Banking
    20.             atbank=true;
    21.             if (!Bank.isOpen()) {
    22.                 Bank.open();
    23.                 Execution.delay(250, 450);
    24.             } else {
    25.                 while(!Inventory.isEmpty()){
    26.                  Bank.depositInventory();
    27.                 }
    28.                 Execution.delay(200, 500);
    29.             }
    30.         } else if (Inventory.isEmpty() && !flaxArea.contains(player) && atbank==true) { // Walking to Flax
    31.             final WebPath flaxPath = Traversal.getDefaultWeb().getPathBuilder().buildTo(flaxArea.getCenter());
    32.             final BresenhamPath failFlaxPath = BresenhamPath.buildTo(flaxArea.getCenter());
    33.  
    34.             if (flaxPath != null) {
    35.                 flaxPath.step();
    36.             } else if (failFlaxPath != null) {
    37.                 failFlaxPath.step();
    38.             }
    39.             atbank=false
    40.         }
    41.     }
    This is an example that should work, make sure to use booleans or anything that will disable an if statement from firing once it arrives at the destination.
     
  7. DarkenedSoul

    Joined:
    Oct 2, 2015
    Messages:
    31
    Likes Received:
    5
    Two birds one stone.
    The enum reference earlier was most likely just aimed at neatness and readability.
    Example In Your Case:

    Code (Text):
    1. public class EnumStateExample extends LoopingScript {
    2.     Area bank = new Area.Rectangular(new Coordinate(0,0,0), new Coordinate(0,0,0));
    3.  
    4.     enum State{
    5.         WALK_TO_BANK, WALK_TO_FIELD, PICK, BANK;
    6.     }
    7.  
    8.     @Override
    9.     public void onLoop() {
    10.     switch (currentState()){
    11.         case BANK:
    12.             handleBank();
    13.             break;
    14.         case WALK_TO_BANK:
    15.             walkToBank();
    16.             break;
    17.     }
    18.     }
    19.  
    20.     private void handleBank() {
    21.         //do banking here
    22.     }
    23.  
    24.     private void walkToBank() {
    25.         //do walking to bank here
    26.     }
    27.  
    28.     private State currentState(){
    29.         if(Inventory.isFull()){
    30.             if(bank.contains(Players.getLocal())){
    31.                 return State.BANK;
    32.             }else{
    33.                 return State.WALK_TO_BANK;
    34.             }
    35.         }else{
    36.             //picking flax and walking to field checks
    37.             //and so on
    38.             return null;
    39.         }
    40.     }
    41. }
    but since we have task script bot this'd be slightly redundant now.

    And you have the right checks just in the wrong place.
    You're telling to to bank if the inventory isn't full.
    And to pick when the inventory is full
    you can still use
    Inventory.isFull() and !Inventory.isFull() but you just have them in the wrong places :D
     
    Sinatra likes this.
  8. Sinatra

    Joined:
    Nov 2, 2015
    Messages:
    17
    Likes Received:
    1
    Amazing! Thank you so much for the help I really do appreciate it!

    I ran into some issues re-coding it to the layout you suggested but I managed to figure them out.

    How is this code looking? Anything you can see straight away that will cause any issues?

    Code (Text):
    1. package com.sinatra.bots.sFlaxNSpin;
    2.  
    3. import com.runemate.game.api.client.paint.PaintListener;
    4. import com.runemate.game.api.hybrid.entities.GameObject;
    5. import com.runemate.game.api.hybrid.entities.Player;
    6. import com.runemate.game.api.hybrid.entities.definitions.ItemDefinition;
    7. import com.runemate.game.api.hybrid.local.hud.interfaces.Bank;
    8. import com.runemate.game.api.hybrid.local.hud.interfaces.Inventory;
    9. import com.runemate.game.api.hybrid.location.Area;
    10. import com.runemate.game.api.hybrid.location.Coordinate;
    11. import com.runemate.game.api.hybrid.location.navigation.Traversal;
    12. import com.runemate.game.api.hybrid.location.navigation.basic.BresenhamPath;
    13. import com.runemate.game.api.hybrid.location.navigation.web.WebPath;
    14. import com.runemate.game.api.hybrid.region.GameObjects;
    15. import com.runemate.game.api.hybrid.region.Players;
    16. import com.runemate.game.api.hybrid.util.StopWatch;
    17. import com.runemate.game.api.hybrid.util.calculations.CommonMath;
    18. import com.runemate.game.api.script.Execution;
    19. import com.runemate.game.api.script.framework.LoopingScript;
    20. import com.runemate.game.api.script.framework.listeners.InventoryListener;
    21. import com.runemate.game.api.script.framework.listeners.events.ItemEvent;
    22.  
    23. import java.awt.*;
    24. import java.util.concurrent.TimeUnit;
    25.  
    26. public class sFlaxNSpin extends LoopingScript implements PaintListener, InventoryListener {
    27.  
    28.     private final static Player player = Players.getLocal();
    29.     private final static StopWatch runtime = new StopWatch();
    30.     private final static Area flaxArea = new Area.Rectangular(new Coordinate(2886, 3464, 0), new Coordinate(2886, 3460, 0));
    31.     private final static Area bankArea = new Area.Rectangular(new Coordinate(2888, 3536, 0), new Coordinate(2888, 3538, 0));
    32.     private static int pickedFlax = 0;
    33.  
    34.     enum State{
    35.         WALK_TO_BANK, WALK_TO_FIELD, PICK, BANK;
    36.     }
    37.  
    38.     @Override
    39.     public void onStart(String... args) {
    40.         getEventDispatcher().addListener(this);
    41.         setLoopDelay(250, 750);
    42.         runtime.start();
    43.     }
    44.  
    45.     @Override
    46.     public void onLoop() {
    47.     switch (currentState()){
    48.         case BANK:
    49.             handleBank();
    50.             break;
    51.         case WALK_TO_BANK:
    52.             walkToBank();
    53.             break;
    54.         case PICK:
    55.             handlePick();
    56.             break;
    57.         case WALK_TO_FIELD:
    58.             walkToField();
    59.         }
    60.     }
    61.  
    62.     private void handleBank() {
    63.         if (Inventory.isFull() && bankArea.contains(player)) { //Banking
    64.             if (!Bank.isOpen()) {
    65.                 Bank.open();
    66.                 Execution.delay(250, 450);
    67.             } else {
    68.                 Bank.depositInventory();
    69.                 Execution.delay(200, 500);
    70.             }
    71.         }
    72.     }
    73.  
    74.     private void walkToBank() {
    75.         if (Inventory.isFull() && !bankArea.contains(player)) { // Walking to Bank
    76.             final WebPath bankPath = Traversal.getDefaultWeb().getPathBuilder().buildTo(bankArea.getCenter());
    77.             final BresenhamPath failBankPath = BresenhamPath.buildTo(bankArea.getCenter());
    78.  
    79.             if (bankPath != null) {
    80.                 bankPath.step();
    81.             } else if (failBankPath != null) {
    82.                 failBankPath.step();
    83.             }
    84.         }
    85.     }
    86.  
    87.     private void handlePick() {
    88.         GameObject flax = GameObjects.getLoaded(67263, 67264, 67265).nearestTo(player);
    89.  
    90.         if (!Inventory.isFull() && flaxArea.contains(player) && isIdle()) { // Picking Flax
    91.             if (flax != null && flax.isValid()) {
    92.                 flax.interact("Pick");
    93.             }
    94.         }
    95.     }
    96.  
    97.     private void walkToField() {
    98.         if (!Inventory.isFull() && !flaxArea.contains(player)) {
    99.             final WebPath flaxPath = Traversal.getDefaultWeb().getPathBuilder().buildTo(flaxArea.getCenter());
    100.             final BresenhamPath failFlaxPath = BresenhamPath.buildTo(flaxArea.getCenter());
    101.  
    102.             if (flaxPath != null) {
    103.                 flaxPath.step();
    104.             } else if (failFlaxPath != null) {
    105.                 failFlaxPath.step();
    106.             }
    107.         }
    108.     }
    109.  
    110.     private State currentState(){
    111.         if (Inventory.isFull()){
    112.             if (bankArea.contains(Players.getLocal())){
    113.                 return State.BANK;
    114.             }else{
    115.                 return State.WALK_TO_BANK;
    116.             }
    117.         }
    118.         if (!Inventory.isFull()){
    119.             if (flaxArea.contains(Players.getLocal())){
    120.                 return State.PICK;
    121.             }else{
    122.                 return State.WALK_TO_FIELD;
    123.             }
    124.         }
    125.         return null;
    126.     }
    127.  
    128.  
    129.     @Override
    130.     public void onPaint(Graphics2D g) {
    131.         Color transBlack = new Color(0, 0, 0, 150);
    132.         //Draw trans rect
    133.         g.setColor(transBlack);
    134.         g.fillRect(0, 0, 150, 70);
    135.         //Draw border of rect
    136.         g.setColor(Color.blue);
    137.         g.drawRect(0, 0, 150, 70);
    138.         //Draw green underline under title
    139.         g.drawLine(5, 20, 145, 20);
    140.         //Draw text
    141.         g.setColor(Color.white);
    142.         g.drawString("sFlaxNSpin", 50, 15);
    143.         g.drawString("Run time: " + runtime.getRuntimeAsString(), 5, 35);
    144.         g.drawString("Picked Flax: " + pickedFlax + " ,P/H: " + (int) CommonMath.rate(TimeUnit.HOURS, runtime.getRuntime(), pickedFlax), 5, 50);
    145.         g.drawString("Bowstring Strung: SOON", 5, 65);
    146.     }
    147.  
    148.     @Override
    149.     public void onItemAdded(ItemEvent event) {
    150.         ItemDefinition definition = event.getItem().getDefinition();
    151.  
    152.         if (definition != null && definition.getName().equals("Flax")) {
    153.             ++pickedFlax;
    154.         }
    155.     }
    156.  
    157.     private boolean isIdle() {
    158.         return player.getAnimationId() == -1 && !player.isMoving();
    159.     }
    160.  
    161.  
    162. }
    163.  
     
  9. DarkenedSoul

    Joined:
    Oct 2, 2015
    Messages:
    31
    Likes Received:
    5
    Can't see any major problems although I'm a bit out of it atm.
    Just a random kinda note thing.
    Where you've done this.
    (aka adding the checks into the functions after reaching the function)
    (namely the isFull and contains line)

    Are redundant to write again since if those conditions aren't true that method wouldn't even be reached as the state :)
    Just if you ever want to save production time, any checks in the currentState() don't need to be added again in the state method.
    --- Double Post Merged, Nov 4, 2015, Original Post Date: Nov 4, 2015 ---
    Also A small Neaten

    Code (Text):
    1.  
    2. private State currentState(){
    3.         if (Inventory.isFull()){
    4.             if (bankArea.contains(Players.getLocal())){
    5.                 return State.BANK;
    6.             }else{
    7.                 return State.WALK_TO_BANK;
    8.             }
    9.         }else{
    10.             if (flaxArea.contains(Players.getLocal())){
    11.                 return State.PICK;
    12.             }else{
    13.                 return State.WALK_TO_FIELD;
    14.             }
    15.         }
    16.     }
     
  10. Sinatra

    Joined:
    Nov 2, 2015
    Messages:
    17
    Likes Received:
    1
    Ahh right okay.

    Say for example if I didn't want the bot to bank after inventory is full but rather full of bowstrings (after spinning them near by).

    I'm assuming I would tackle this the same way right? add on another 2 states, (WALK_TO_WHEEL and SPIN)

    can Inventory.isFull() work like the following?
    private void walkToWheel() {
    if (Inventory.isFull("Flax") or am I going to have to somehow go down the definition.getName().equals("Flax")) route?
     
  11. DarkenedSoul

    Joined:
    Oct 2, 2015
    Messages:
    31
    Likes Received:
    5
    IMO, The Simplest Modification To Accommodate :


    Code (Text):
    1. private State currentState(){
    2.         if (Inventory.isFull()){
    3.             if(Inventory.containsAnyOf("Flax")){
    4.                 if(wheelArea.contains(Players.getLocal())){
    5.                     return State.SPIN;
    6.                 }else{
    7.                     return State.WALK_TO_WHEEL;
    8.                 }
    9.             }else {
    10.                 if (bankArea.contains(Players.getLocal())) {
    11.                     return State.BANK;
    12.                 } else {
    13.                     return State.WALK_TO_BANK;
    14.                 }
    15.             }
    16.         }else{
    17.             if (flaxArea.contains(Players.getLocal())){
    18.                 return State.PICK;
    19.             }else{
    20.                 return State.WALK_TO_FIELD;
    21.             }
    22.         }
    23.     }
    Wherever possible try to utilize old checks if they're also relative and just slip the logic in where it seems right :p
     
  12. Sinatra

    Joined:
    Nov 2, 2015
    Messages:
    17
    Likes Received:
    1
    Nice! I'm testing my bot now for a couple hours to see what comes up.

    I'm worried about what happens when a strange rock appears in my inventory but I guess I'll have to wait and see.
     
    #12 Sinatra, Nov 5, 2015
    Last edited: Nov 5, 2015
  13. DarkenedSoul

    Joined:
    Oct 2, 2015
    Messages:
    31
    Likes Received:
    5
    The script bot will auto destroy it causing you to walk back to the field for 1 flax :p.
    There are two ways you could overcome this :)
    1.
    GameEvents.RS3.UNEXPECTED_ITEM_HANDLER.disable();
    in your onstart

    2.
    Code (Text):
    1. private State currentState(){
    2.         if (Inventory.isFull() || Inventory.containsAnyOf("Bowstring")){
    3.             if(Inventory.containsAnyOf("Flax")){
    4.                 if(wheelArea.contains(Players.getLocal())){
    5.                     return State.SPIN;
    6.                 }else{
    7.                     return State.WALK_TO_WHEEL;
    8.                 }
    9.             }else {
    10.                 if (bankArea.contains(Players.getLocal())) {
    11.                     return State.BANK;
    12.                 } else {
    13.                     return State.WALK_TO_BANK;
    14.                 }
    15.             }
    16.         }else{
    17.             if (flaxArea.contains(Players.getLocal())){
    18.                 return State.PICK;
    19.             }else{
    20.                 return State.WALK_TO_FIELD;
    21.             }
    22.         }
    23.     }
    --- Double Post Merged, Nov 5, 2015, Original Post Date: Nov 5, 2015 ---
    also bowstring might be spelt wrong, idk how its spelt in game
     
  14. Sinatra

    Joined:
    Nov 2, 2015
    Messages:
    17
    Likes Received:
    1
    Yeah I just watched my bot destroy one and run back for 1 more flax.

    Unfortunately it doesn't click on the spinning wheel to open up the spin menu. However if I open up the spin interface myself by manually clicking on the wheel it will select all the bowstrings and wait for them to finish before running to the bank. I'm not sure what is actually causing this issue?

    Code (Text):
    1.     private void handleSpin() {
    2.         GameObject spinningWheel = GameObjects.getLoaded(66850).nearestTo(player);
    3.  
    4.         if (Inventory.isFull() && wheelArea.contains(player)) {
    5.             spinningWheel.interact("Spin");
    6.             final InterfaceComponent spinInterface = Interfaces.newQuery().texts("Spin").visible().results().first();
    7.             if (spinInterface != null) {
    8.                 spinInterface.click();
    9.             }
    10.             final Player local = Players.getLocal();
    11.             Execution.delayUntil(() -> !Inventory.contains("Flax"), () -> local != null && local.getAnimationId() != -1, 3000);
    12.         }
    13.     }
     
  15. DarkenedSoul

    Joined:
    Oct 2, 2015
    Messages:
    31
    Likes Received:
    5
    Might be the way you query it.
    Also with your checks since this will just be looping you can format everything with more conditions.
    It doesn't need to all happen in one run through, especially accounting for error.
    I'd imagine it to look more like this
    Code (Text):
    1.  private void handleSpin() {      
    2.         GameObject spinningWheel = GameObjects.newQuery().within(wheelArea).names("Spinning wheel").results().nearest();
    3.         final InterfaceComponent spinInterface = Interfaces.newQuery().texts("Spin").visible().results().first();
    4.         final Player local = Players.getLocal();
    5.  
    6.         if(spinInterface != null && spinInterface.isVisible()){
    7.             if(spinInterface.click()){
    8.                 Execution.delayUntil(() -> !Inventory.contains("Flax"), () -> local != null && local.getAnimationId() != -1, 3000);
    9.             }
    10.         }else{          
    11.             if(spinningWheel != null){
    12.                 if(spinningWheel.isVisible()){
    13.                     if(spinningWheel.interact("Spin")){
    14.                         Execution.delayUntil(() -> spinInterface != null && spinInterface.isVisible(), 2500, 3500);
    15.                     }
    16.                 }else{
    17.                     Camera.turnTo(spinningWheel);
    18.                 }
    19.             }
    20.         }          
    21.     }
     
  16. Sinatra

    Joined:
    Nov 2, 2015
    Messages:
    17
    Likes Received:
    1
    My bot just seems to stand in the center of the wheelArea and doesn't move. When I manually move the mouse it tries to move back to the center using the minimap?

    Any ideas?

    current code:

    Code (Text):
    1.     private void handleSpin() {
    2.         GameObject spinningWheel = GameObjects.newQuery().within(wheelArea).names("Spinning wheel").results().nearest();
    3.  
    4.         final InterfaceComponent spinInterface = Interfaces.newQuery().texts("Spin").visible().results().first();
    5.         final Player local = Players.getLocal();
    6.  
    7.         if(spinInterface != null && spinInterface.isVisible()){
    8.             if(spinInterface.click()){
    9.                 Execution.delayUntil(() -> !Inventory.contains("Flax"), () -> local != null && local.getAnimationId() != -1, 3000);
    10.             }
    11.         }else{
    12.             if(spinningWheel != null){
    13.                 if(spinningWheel.isVisible()){
    14.                     if(spinningWheel.interact("Spin")){
    15.                         Execution.delayUntil(() -> spinInterface != null && spinInterface.isVisible(), 2500, 3500);
    16.                     }
    17.                 }else{
    18.                     Camera.turnTo(spinningWheel);
    19.                 }
    20.             }
    21.         }
    22.     }
    I've also tried

    GameObject spinningWheel = GameObjects.newQuery().within(wheelArea).names("Spinning wheel").results().nearestTo(player);

    Still producing the same results.
     
  17. DarkenedSoul

    Joined:
    Oct 2, 2015
    Messages:
    31
    Likes Received:
    5
    Is it stuck trying to walk to the area?
     
  18. Sinatra

    Joined:
    Nov 2, 2015
    Messages:
    17
    Likes Received:
    1
    I'm not sure.

    After gathering a full inventory of flax is walks to the wheel area but then keeps clicking the minimap around the center of the wheelarea.
    It doesn't seem to click on the Spinning wheel. However if I open up the spinning interface myself by manually clicking on the spinning wheel it will proceed to spin them and then move to the bank when finished, bank and return to the flax field and gather again all the way upto the wheelarea I guess.

    I'm thinking it is having trouble clicking the spinning wheel or finding it near. It gets to the center of the area fine but then just keeps clicking on the minimap endlessly.
     
  19. DarkenedSoul

    Joined:
    Oct 2, 2015
    Messages:
    31
    Likes Received:
    5
    there's something wrong with your
    >Walk To Wheel Area
    >Open Interface
    Bit
    Maybe your checks are a bit iffy on it?
     
  20. Sinatra

    Joined:
    Nov 2, 2015
    Messages:
    17
    Likes Received:
    1
    Yeah I'm not quite sure though. Everything works apart from this one thing.

    Code (Java):
    1. package com.sinatra.bots.sFlaxNSpin;
    2.  
    3. import com.runemate.game.api.client.paint.PaintListener;
    4. import com.runemate.game.api.hybrid.entities.GameObject;
    5. import com.runemate.game.api.hybrid.entities.Player;
    6. import com.runemate.game.api.hybrid.entities.definitions.ItemDefinition;
    7. import com.runemate.game.api.hybrid.local.Camera;
    8. import com.runemate.game.api.hybrid.local.Skill;
    9. import com.runemate.game.api.hybrid.local.hud.interfaces.*;
    10. import com.runemate.game.api.hybrid.location.Area;
    11. import com.runemate.game.api.hybrid.location.Coordinate;
    12. import com.runemate.game.api.hybrid.location.navigation.Traversal;
    13. import com.runemate.game.api.hybrid.location.navigation.basic.BresenhamPath;
    14. import com.runemate.game.api.hybrid.location.navigation.web.WebPath;
    15. import com.runemate.game.api.hybrid.region.GameObjects;
    16. import com.runemate.game.api.hybrid.region.Players;
    17. import com.runemate.game.api.hybrid.util.StopWatch;
    18.  
    19. import com.runemate.game.api.hybrid.util.calculations.CommonMath;
    20. import com.runemate.game.api.script.Execution;
    21. import com.runemate.game.api.script.framework.LoopingScript;
    22. import com.runemate.game.api.script.framework.listeners.InventoryListener;
    23. import com.runemate.game.api.script.framework.listeners.SkillListener;
    24. import com.runemate.game.api.script.framework.listeners.events.ItemEvent;
    25. import com.runemate.game.api.script.framework.listeners.events.SkillEvent;
    26.  
    27.  
    28. import java.awt.*;
    29. import java.util.concurrent.TimeUnit;
    30.  
    31. public class sFlaxNSpin extends LoopingScript implements PaintListener, InventoryListener, SkillListener {
    32.  
    33.     private final static Player player = Players.getLocal();
    34.     private final static StopWatch runtime = new StopWatch();
    35.     private final static Area flaxArea = new Area.Rectangular(new Coordinate(2886, 3464, 0), new Coordinate(2886, 3460, 0));
    36.     private final static Area bankArea = new Area.Rectangular(new Coordinate(2888, 3536, 0), new Coordinate(2888, 3538, 0));
    37.     private final static Area wheelArea = new Area.Rectangular(new Coordinate(2890, 3495, 0), new Coordinate(2880, 3495, 0));
    38.     private static int pickedFlax = 0;
    39.     private static int spunBowstrings = 0;
    40.     public int startExp = -1;
    41.     public int expGained = 0;
    42.  
    43.     enum State{
    44.         WALK_TO_BANK, WALK_TO_FIELD, WALK_TO_WHEEL, PICK, SPIN, BANK
    45.  
    46.     }
    47.  
    48.     @Override
    49.     public void onStart(String... args) {
    50.         getEventDispatcher().addListener(this);
    51.         setLoopDelay(250, 500);
    52.         runtime.start();
    53.     }
    54.  
    55.     @Override
    56.     public void onLoop() {
    57.     switch (currentState()){
    58.         case PICK:
    59.             handlePick();
    60.             break;
    61.         case WALK_TO_FIELD:
    62.             walkToField();
    63.         case SPIN:
    64.             handleSpin();
    65.         case WALK_TO_WHEEL:
    66.             walkToWheel();
    67.         case BANK:
    68.             handleBank();
    69.             break;
    70.         case WALK_TO_BANK:
    71.             walkToBank();
    72.             break;
    73.         }
    74.     }
    75.  
    76.  
    77.     private void handlePick() {
    78.         GameObject flax = GameObjects.getLoaded(67263, 67264, 67265).nearestTo(player);
    79.  
    80.         if (!Inventory.isFull() && flaxArea.contains(player) && isIdle()) { // Picking Flax
    81.             if (flax != null && flax.isValid()) {
    82.                 flax.interact("Pick");
    83.             }
    84.         }
    85.     }
    86.  
    87.     private void walkToField() {
    88.         if (!Inventory.isFull() && !flaxArea.contains(player)) {
    89.             final WebPath flaxPath = Traversal.getDefaultWeb().getPathBuilder().buildTo(flaxArea.getCenter());
    90.             final BresenhamPath failFlaxPath = BresenhamPath.buildTo(flaxArea.getCenter());
    91.  
    92.             if (flaxPath != null) {
    93.                 flaxPath.step();
    94.             } else if (failFlaxPath != null) {
    95.                 failFlaxPath.step();
    96.             }
    97.         }
    98.     }
    99.  
    100.     private void handleSpin() {
    101.         GameObject spinningWheel = GameObjects.newQuery().within(wheelArea).names("Spinning wheel").results().nearest();
    102.  
    103.         final InterfaceComponent spinInterface = Interfaces.newQuery().texts("Spin").visible().results().first();
    104.         final Player local = Players.getLocal();
    105.  
    106.         if(spinInterface != null && spinInterface.isVisible()){
    107.             if(spinInterface.click()){
    108.                 Execution.delayUntil(() -> !Inventory.contains("Flax"), () -> local != null && local.getAnimationId() != -1, 3000);
    109.             }
    110.         }else{
    111.             if(spinningWheel != null){
    112.                 if(spinningWheel.isVisible()){
    113.                     if(spinningWheel.interact("Spin")){
    114.                         Execution.delayUntil(() -> spinInterface != null && spinInterface.isVisible(), 2500, 3500);
    115.                     }
    116.                 }else{
    117.                     Camera.turnTo(spinningWheel);
    118.                 }
    119.             }
    120.         }
    121.     }
    122.  
    123.     private void walkToWheel() {
    124.         if (Inventory.isFull() && !wheelArea.contains(player)) {
    125.             final WebPath wheelPath = Traversal.getDefaultWeb().getPathBuilder().buildTo(wheelArea.getCenter());
    126.             final BresenhamPath failWheelPath = BresenhamPath.buildTo(wheelArea.getCenter());
    127.  
    128.             if (wheelPath != null) {
    129.                 wheelPath.step();
    130.             } else if (failWheelPath != null) {
    131.                 failWheelPath.step();
    132.             }
    133.         }
    134.     }
    135.  
    136.     private void handleBank() {
    137.         if (Inventory.isFull() && bankArea.contains(player)) { //Banking
    138.             if (!Bank.isOpen()) {
    139.                 Bank.open();
    140.                 Execution.delay(250, 450);
    141.             } else {
    142.                 Bank.depositInventory();
    143.                 Execution.delay(200, 500);
    144.             }
    145.         }
    146.     }
    147.  
    148.     private void walkToBank() {
    149.         if (Inventory.isFull() && !bankArea.contains(player)) { // Walking to Bank
    150.             final WebPath bankPath = Traversal.getDefaultWeb().getPathBuilder().buildTo(bankArea.getCenter());
    151.             final BresenhamPath failBankPath = BresenhamPath.buildTo(bankArea.getCenter());
    152.  
    153.             if (bankPath != null) {
    154.                 bankPath.step();
    155.             } else if (failBankPath != null) {
    156.                 failBankPath.step();
    157.             }
    158.         }
    159.     }
    160.  
    161.     private State currentState(){
    162.         if (Inventory.isFull()){
    163.             if(Inventory.containsAnyOf("Flax")){
    164.                 if(wheelArea.contains(Players.getLocal())){
    165.                     return State.SPIN;
    166.                 }else{
    167.                     return State.WALK_TO_WHEEL;
    168.                 }
    169.             }
    170.  
    171.             else {
    172.                 if (bankArea.contains(Players.getLocal())) {
    173.                     return State.BANK;
    174.                 } else {
    175.                     return State.WALK_TO_BANK;
    176.                 }
    177.             }
    178.         }else{
    179.             if (flaxArea.contains(Players.getLocal())){
    180.                 return State.PICK;
    181.             }else{
    182.                 return State.WALK_TO_FIELD;
    183.             }
    184.         }
    185.     }
    186.  
    187.  
    188.     @Override
    189.     public void onPaint(Graphics2D g) {
    190.  
    191.         Color transBlack = new Color(0, 0, 0, 150);
    192.         //Draw trans rect
    193.         g.setColor(transBlack);
    194.         g.fillRect(0, 0, 200, 100);
    195.         //Draw border of rect
    196.         g.setColor(Color.blue);
    197.         g.drawRect(0, 0, 200, 100);
    198.         //Draw green underline under title
    199.         g.drawLine(5, 20, 190, 20);
    200.         //Draw text
    201.         g.setColor(Color.white);
    202.  
    203.         g.drawString("sFlaxNSpin", 50, 15);
    204.         g.drawString("Run time: " + runtime.getRuntimeAsString(), 5, 35);
    205.         g.drawString("Picked Flax: " + pickedFlax + " ,P/H: " + (int) CommonMath.rate(TimeUnit.HOURS, runtime.getRuntime(), pickedFlax), 5, 50);
    206.         g.drawString("Bowstrings: " + spunBowstrings + " ,P/H: " + (int) CommonMath.rate(TimeUnit.HOURS, runtime.getRuntime(), spunBowstrings), 5, 65);
    207.         g.drawString("Experience Gained: " + expGained + " ,P/H: " + (int) CommonMath.rate(TimeUnit.HOURS, runtime.getRuntime(), expGained), 5, 80);
    208.     }
    209.  
    210.     @Override
    211.     public void onItemAdded(ItemEvent event) {
    212.         ItemDefinition definition = event.getItem().getDefinition();
    213.  
    214.         if (definition != null && definition.getName().equals("Flax")) {
    215.             ++pickedFlax;
    216.         }
    217.  
    218.         if (definition != null && definition.getName().equals("Bowstring")) {
    219.             ++spunBowstrings;
    220.         }
    221.     }
    222.  
    223.     @Override
    224.     public void onExperienceGained(SkillEvent event) {
    225.         expGained = startExp == -1 ? 0 : Skill.CRAFTING.getExperience() - startExp;
    226.     }
    227.  
    228.     private boolean isIdle() {
    229.         return player.getAnimationId() == -1 && !player.isMoving();
    230.     }
    231.  
    232.     @Override
    233.     public void onPause() {
    234.         runtime.stop();
    235.     }
    236.  
    237.     @Override
    238.     public void onResume() {
    239.         runtime.start();
    240.     }
    241.  
    242.     @Override
    243.  
    244. public void onStop() {
    245. System.out.println("Thank you for using sFlaxNSpin");
    246. System.out.println("Author: Sinatra");
    247. System.out.println("Massive thanks to DarkenedSoul for helping me out!");
    248. System.out.println("Total Runtime: " + runtime.getRuntimeAsString());
    249. }
    250.  
    251.  
    252. }
    253.  
     
    #20 Sinatra, Nov 5, 2015
    Last edited by a moderator: Nov 5, 2015

Share This Page

Loading...