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

Question How to collect groundItems on different tiles in specified order

Discussion in 'Developer Support' started by urbex07, Apr 18, 2017.

  1. urbex07

    urbex07 that went well

    Joined:
    Feb 21, 2017
    Messages:
    7
    Likes Received:
    1
    Hi, I'm attempting to make a simple swamp toad collecting bot that collects toads efficiently in a path, as a player would. Eventually I'd like to implement some randomization and configuration for different paths, but for now I'm just trying to get it working with one order.

    I'm wondering if there's a best practice for determining the next toad to collect based on the last toad collected.

    As of now, my onItemAdded method is calling a public function in my root class that sets the bot property currrentToad to the next toad to be picked up. This property is then passed through the necessary branches in my tree when it reaches isNearby which nullchecks and takeToad which attempts to collect the toad. I use switch statements in both of those nodes to determine the coordinates of the currentToad.

    My implementation seems to skip the last few toads, randomly breaks and overall just looks like a mess. I'm thinking there has to be a better way to do this, ideally one that makes it easy for modular toad orders and some randomization.

    Below are some snippets of my source code:

    toadSpawn.java
    Code (Text):
    1. public enum ToadSpawn {
    2.     toad1,
    3.     toad2,
    4.     toad3,
    5.     toad4,
    6.     toad5,
    7.     toad6,
    8.     toad7,
    9.     toad8,
    10.     toad9,
    11.     toad10,
    12.     toad11,
    13.     toad12,
    14.     toad13,
    15.     toad14,
    16.     toad15,
    17.     toad16,
    18.     toad17,
    19.     toad18
    20. }
    TreeToads.java
    Code (Text):
    1. public class TreeToads extends TreeBot implements InventoryListener  {
    2.     ...
    3.     public ToadSpawn currentToad;
    4.     ...
    5.     public final Coordinate toad1Coordinate = new Coordinate(2421,3509,0);
    6.     public final Coordinate toad2Coordinate = new Coordinate(2417,3508,0);
    7.     public final Coordinate toad3Coordinate = new Coordinate(2418,3511,0);
    8.     public final Coordinate toad4Coordinate = new Coordinate(2417,3512,0);
    9.     public final Coordinate toad5Coordinate = new Coordinate(2416,3512,0);
    10.     public final Coordinate toad6Coordinate = new Coordinate(2413,3511,0);
    11.     public final Coordinate toad7Coordinate = new Coordinate(2411,3512,0);
    12.     public final Coordinate toad8Coordinate = new Coordinate(2409,3514,0);
    13.     public final Coordinate toad9Coordinate = new Coordinate(2407,3516,0);
    14.     public final Coordinate toad10Coordinate = new Coordinate(2412,3519,0);
    15.     public final Coordinate toad11Coordinate = new Coordinate(2415,3518,0);
    16.     public final Coordinate toad12Coordinate = new Coordinate(2417,3516,0);
    17.     public final Coordinate toad13Coordinate = new Coordinate(2417,3515,0);
    18.     public final Coordinate toad14Coordinate = new Coordinate(2418,3517,0);
    19.     public final Coordinate toad15Coordinate = new Coordinate(2421,3519,0);
    20.     public final Coordinate toad16Coordinate = new Coordinate(2424,3517,0);
    21.     public final Coordinate toad17Coordinate = new Coordinate(2424,3514,0);
    22.     public final Coordinate toad18Coordinate = new Coordinate(2428,3501,0);
    23.  
    24.     ...
    25.  
    26.     public TreeToads(){
    27.         currentToad = toad1;
    28.     }
    29.  
    30.     @Override
    31.     public TreeTask createRootTask() {
    32.         return new Root(this, currentToad);
    33.     }
    34.  
    35.     ...
    36.  
    37.     @Override
    38.      public void onItemAdded(ItemEvent event) {
    39.           ItemDefinition definition = event.getItem().getDefinition();
    40.             if (definition != null) {
    41.                 if (definition.getName().contains("Swamp toad")) {
    42.                     updateCurrentToad(false);
    43.                 }
    44.             }
    45.        }
    46.  
    47. }
    48.  

    Root.java
    Code (Text):
    1. public class Root extends BranchTask {
    2.     private TreeToads bot;
    3.     private static ToadSpawn currentToad;
    4.  
    5.  
    6.     public Root(TreeToads bot, ToadSpawn currentToad){
    7.         this.bot = bot;
    8.         this.currentToad = currentToad;
    9.     }
    10.  
    11.    ...
    12.  
    13.     public static void updateCurrentToad(boolean reset){
    14.         if(reset){
    15.             System.out.println("Leaving, resetting toad");
    16.             currentToad = toad1;
    17.      } else {
    18.         System.out.println("Toad added, determineCurrentToad called");
    19.         switch (currentToad){
    20.             case toad1: currentToad = toad2;
    21.                  break;
    22.             case toad2: currentToad = toad3;
    23.                 break;
    24.             case toad3: currentToad = toad4;
    25.                 break;
    26.             case toad4: currentToad = toad5;
    27.                 break;
    28.             case toad5: currentToad = toad6;
    29.                 break;
    30.             case toad6: currentToad = toad7;
    31.                 break;
    32.             case toad7: currentToad = toad8;
    33.                 break;
    34.             case toad8: currentToad = toad9;
    35.                 break;
    36.             case toad9: currentToad = toad10;
    37.                 break;
    38.             case toad10: currentToad = toad11;
    39.                 break;
    40.             case toad11: currentToad = toad12;
    41.                 break;
    42.             case toad12: currentToad = toad13;
    43.                 break;
    44.             case toad13: currentToad = toad14;
    45.                 break;
    46.             case toad14: currentToad = toad15;
    47.                 break;
    48.             case toad15: currentToad = toad16;
    49.                 break;
    50.             case toad16: currentToad = toad17;
    51.                 break;
    52.             case toad17: currentToad = toad18;
    53.                 break;
    54.             case toad18: currentToad = toad1;
    55.                 break;
    56.  
    57.         }
    58.  
    59.     }
    60. }
    61.  
    62.  
    63. }
    isNearby.java
    Code (Text):
    1.  
    2. public class IsNearby extends BranchTask {
    3.  
    4. private TreeToads bot;
    5. private Locatables locatable;
    6. private ToadSpawn currentToad;
    7.  
    8. private GameObject obj;
    9. private GroundItem obj2;
    10.  
    11. public IsNearby(TreeToads bot, Locatables locatable, ToadSpawn currentToad){
    12.     this.bot = bot;
    13.     this.locatable = locatable;
    14.     this.currentToad = currentToad;
    15. }
    16.  
    17. @Override
    18. public boolean validate() {
    19.  
    20.     if(locatable == Locatables.bank) {
    21.  
    22.         obj = GameObjects.newQuery().actions("Bank").within(bot.bankArea).results().nearest();
    23.  
    24.     } else if(locatable == Locatables.toad) {
    25.         switch (currentToad) {
    26.             case toad1: obj2 = GroundItems.getLoadedOn(bot.toad1Coordinate, "Swamp toad").first();
    27.                 break;
    28.             case toad2: obj2 = GroundItems.getLoadedOn(bot.toad2Coordinate, "Swamp toad").first();
    29.                 break;
    30.             case toad3: obj2 = GroundItems.getLoadedOn(bot.toad3Coordinate, "Swamp toad").first();
    31.                 break;
    32.             case toad4: obj2 = GroundItems.getLoadedOn(bot.toad4Coordinate, "Swamp toad").first();
    33.                 break;
    34.             case toad5: obj2 = GroundItems.getLoadedOn(bot.toad5Coordinate, "Swamp toad").first();
    35.                 break;
    36.             case toad6: obj2 = GroundItems.getLoadedOn(bot.toad6Coordinate, "Swamp toad").first();
    37.                 break;
    38.             case toad7: obj2 = GroundItems.getLoadedOn(bot.toad7Coordinate, "Swamp toad").first();
    39.                 break;
    40.             case toad8: obj2 = GroundItems.getLoadedOn(bot.toad8Coordinate, "Swamp toad").first();
    41.                 break;
    42.             case toad9: obj2 = GroundItems.getLoadedOn(bot.toad9Coordinate, "Swamp toad").first();
    43.                 break;
    44.             case toad10: obj2 = GroundItems.getLoadedOn(bot.toad10Coordinate, "Swamp toad").first();
    45.                 break;
    46.             case toad11: obj2 = GroundItems.getLoadedOn(bot.toad11Coordinate, "Swamp toad").first();
    47.                 break;
    48.             case toad12: obj2 = GroundItems.getLoadedOn(bot.toad12Coordinate, "Swamp toad").first();
    49.                 break;
    50.             case toad13: obj2 = GroundItems.getLoadedOn(bot.toad13Coordinate, "Swamp toad").first();
    51.                 break;
    52.             case toad14: obj2 = GroundItems.getLoadedOn(bot.toad14Coordinate, "Swamp toad").first();
    53.                 break;
    54.             case toad15: obj2 = GroundItems.getLoadedOn(bot.toad15Coordinate, "Swamp toad").first();
    55.                 break;
    56.             case toad16: obj2 = GroundItems.getLoadedOn(bot.toad16Coordinate, "Swamp toad").first();
    57.                 break;
    58.             case toad17: obj2 = GroundItems.getLoadedOn(bot.toad17Coordinate, "Swamp toad").first();
    59.                 break;
    60.             case toad18: obj2 = GroundItems.getLoadedOn(bot.toad18Coordinate, "Swamp toad").first();
    61.                 break;
    62.         }
    63.     }
    64.     return (obj != null && obj.distanceTo(Players.getLocal()) < 12) || (obj2 != null && obj2.distanceTo(Players.getLocal()) < 25);
    65. }
    66.  
    67. @Override
    68. public TreeTask successTask() {
    69.     if(locatable == Locatables.bank)
    70.         return new OpenBankLeaf(bot);
    71.     else if(locatable == Locatables.toad)
    72.         return new TakeToad(bot, currentToad, obj2);
    73.     else
    74.         return new EmptyLeaf();
    75. }
    76.  
    77. @Override
    78. public TreeTask failureTask() {
    79.     if(locatable == Locatables.bank)
    80.         return new TraversalLeaf(bot, TraversalLocation.bankArea);
    81.     else if(locatable == Locatables.toad)
    82.         return new TraversalLeaf(bot, TraversalLocation.toadArea);
    83.     else
    84.         return new EmptyLeaf();
    85. }
    86.  
    87.  
    takeToad.java
    Code (Text):
    1.  
    2. public class TakeToad extends LeafTask {
    3.  
    4.     private TreeToads bot;
    5.     private ToadSpawn currentToad;
    6.  
    7.     private GroundItem toad;
    8.  
    9.     public TakeToad(TreeToads bot, ToadSpawn currentToad, GroundItem obj2){
    10.         this.bot = bot;
    11.         this.currentToad = currentToad;
    12.         this.toad = obj2;
    13.     }
    14.  
    15.  
    16.  
    17.     @Override
    18.     public void execute()
    19.     {
    20.  
    21.     switch (currentToad) {
    22.     case toad1: toad = GroundItems.getLoadedOn(bot.toad1Coordinate, "Swamp toad").first();
    23.         break;
    24.     case toad2: toad = GroundItems.getLoadedOn(bot.toad2Coordinate, "Swamp toad").first();
    25.         break;
    26.     case toad3: toad = GroundItems.getLoadedOn(bot.toad3Coordinate, "Swamp toad").first();
    27.         break;
    28.     case toad4: toad = GroundItems.getLoadedOn(bot.toad4Coordinate, "Swamp toad").first();
    29.         break;
    30.     case toad5: toad = GroundItems.getLoadedOn(bot.toad5Coordinate, "Swamp toad").first();
    31.         break;
    32.     case toad6: toad = GroundItems.getLoadedOn(bot.toad6Coordinate, "Swamp toad").first();
    33.         break;
    34.     case toad7: toad = GroundItems.getLoadedOn(bot.toad7Coordinate, "Swamp toad").first();
    35.         break;
    36.     case toad8: toad = GroundItems.getLoadedOn(bot.toad8Coordinate, "Swamp toad").first();
    37.         break;
    38.     case toad9: toad = GroundItems.getLoadedOn(bot.toad9Coordinate, "Swamp toad").first();
    39.         break;
    40.     case toad10: toad = GroundItems.getLoadedOn(bot.toad10Coordinate, "Swamp toad").first();
    41.         break;
    42.     case toad11: toad = GroundItems.getLoadedOn(bot.toad11Coordinate, "Swamp toad").first();
    43.         break;
    44.     case toad12: toad = GroundItems.getLoadedOn(bot.toad12Coordinate, "Swamp toad").first();
    45.         break;
    46.     case toad13: toad = GroundItems.getLoadedOn(bot.toad13Coordinate, "Swamp toad").first();
    47.         break;
    48.     case toad14: toad = GroundItems.getLoadedOn(bot.toad14Coordinate, "Swamp toad").first();
    49.         break;
    50.     case toad15: toad = GroundItems.getLoadedOn(bot.toad15Coordinate, "Swamp toad").first();
    51.         break;
    52.     case toad16: toad = GroundItems.getLoadedOn(bot.toad16Coordinate, "Swamp toad").first();
    53.         break;
    54.     case toad17: toad = GroundItems.getLoadedOn(bot.toad17Coordinate, "Swamp toad").first();
    55.         break;
    56.     case toad18: toad = GroundItems.getLoadedOn(bot.toad18Coordinate, "Swamp toad").first();
    57.         break;
    58. }
    59.  
    60.  
    61.     if(toad != null)
    62.         {
    63.  
    64.             if (!toad.isVisible())
    65.                 Camera.concurrentlyTurnTo(toad);
    66.  
    67.             if (toad.interact("Take"))
    68.                 Execution.delay(100, 500);
    69.  
    70.         }
    71.     }
    72. }
    73.  
    map of toads:
    https://s1.postimg.org/ww848tsjz/Swamp_Toad_Spawns.png
    [​IMG]
    Credit to proxi, who's code I'm building ondestroying
     
    #1 urbex07, Apr 18, 2017
    Last edited: Apr 18, 2017
  2. Savior

    Savior Java Warlord

    Joined:
    Nov 17, 2014
    Messages:
    4,906
    Likes Received:
    2,748
    Holy mother of god
     
    tyb51 and Derk like this.
  3. urbex07

    urbex07 that went well

    Joined:
    Feb 21, 2017
    Messages:
    7
    Likes Received:
    1
    yeah dude I know, I'm sure there's a really easy way I'm missing
     
  4. SlashnHax

    Joined:
    Dec 10, 2014
    Messages:
    3,216
    Likes Received:
    1,043
    No offense dude, but I have no words.
     
  5. SkyGuy

    Joined:
    Dec 23, 2016
    Messages:
    221
    Likes Received:
    94
    Have you thought about just getting the nearest one? seems like the easier way, and its pretty efficient of a path as long as they spawn quick enough that you wont cut yourself off somehow. Sorry if it doesnt really answer your question :p
     
  6. urbex07

    urbex07 that went well

    Joined:
    Feb 21, 2017
    Messages:
    7
    Likes Received:
    1
    Yeah I was doing that initially, but I was trying to come up with a more efficient(ingame) solution. When you just pick up the nearest toad the player runs in a very random and bot-like pattern and retraces their steps.
     
  7. CodeNinja

    Joined:
    Dec 20, 2016
    Messages:
    37
    Likes Received:
    3
    You can easily make your own path, or even a set of efficient paths. What you need is a structure to simplify this process.

    I thought of a HashMap, just add the Coordinates manually.

    Now you can just use 1 for a specific coordinate, 3 for a specific coordinate, etc. You can make a path 1,3,2,4,5,0,6 for example.

    Code (PHP):
    1. Map<Integer,Coordinate> toadspawns = new HashMap<>();
    2. toadspawns.put(1, new Coordinate(123,123,0));
    3. toadspawns.put(2, new Coordinate(234,234,0));
    4. toadspawns.put(3, new Coordinate(345,345,0));
    5. toadspawns.put(4, new Coordinate(456,456,0));
    6. toadspawns.put(5, new Coordinate(567,567,0));
    7.  
    8. collectToadAt(toadspawns.get(1));
    9. collectToadAt(toadspawns.get(2));
    10.  
    11. int[] path1 = new int[] { 1,3,4,2,5 };
    12. collectToadsUsing(path1);
     
    #7 CodeNinja, Apr 28, 2017
    Last edited: Apr 28, 2017
  8. Savior

    Savior Java Warlord

    Joined:
    Nov 17, 2014
    Messages:
    4,906
    Likes Received:
    2,748
    Why would you want to use a map if the values are in the order from 0..n anyway... What you're creating is basically a list.
     
    sickness0666 likes this.
  9. CodeNinja

    Joined:
    Dec 20, 2016
    Messages:
    37
    Likes Received:
    3
    I see what you are saying, I fixed the HashMap example for what I meant. Ty for pointing that out.

    A list would also work:

    PHP:
    1. List<Coordinate> coordinates = new ArrayList<Coordinate>();
    2. coordinates.add(new Coordinate(2342,1472,0));
    3. coordinates.add(new Coordinate(2374,1765,0));
    4. coordinates.add(new Coordinate(2395,1151,0));
    5. coordinates.add(new Coordinate(2415,1249,0));
    But I find that hashmap is more intuitive because he could say ok coordinate 1 is: <insert coordinate>, 2 is: <insert coordinate>

    With the array list it is the same concept, but you will need to add the coordinates in order.
     
    #9 CodeNinja, Apr 28, 2017
    Last edited: Apr 28, 2017
  10. Infinite Monkeys

    Joined:
    Jun 20, 2015
    Messages:
    160
    Likes Received:
    48
    You can set list members by index.
     

Share This Page

Loading...