Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Question How to collect groundItems on different tiles in specified order

that went well
Joined
Feb 21, 2017
Messages
7
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:
public enum ToadSpawn {
    toad1,
    toad2,
    toad3,
    toad4,
    toad5,
    toad6,
    toad7,
    toad8,
    toad9,
    toad10,
    toad11,
    toad12,
    toad13,
    toad14,
    toad15,
    toad16,
    toad17,
    toad18
}

TreeToads.java
Code:
public class TreeToads extends TreeBot implements InventoryListener  {
    ...
    public ToadSpawn currentToad;
    ...
    public final Coordinate toad1Coordinate = new Coordinate(2421,3509,0);
    public final Coordinate toad2Coordinate = new Coordinate(2417,3508,0);
    public final Coordinate toad3Coordinate = new Coordinate(2418,3511,0);
    public final Coordinate toad4Coordinate = new Coordinate(2417,3512,0);
    public final Coordinate toad5Coordinate = new Coordinate(2416,3512,0);
    public final Coordinate toad6Coordinate = new Coordinate(2413,3511,0);
    public final Coordinate toad7Coordinate = new Coordinate(2411,3512,0);
    public final Coordinate toad8Coordinate = new Coordinate(2409,3514,0);
    public final Coordinate toad9Coordinate = new Coordinate(2407,3516,0);
    public final Coordinate toad10Coordinate = new Coordinate(2412,3519,0);
    public final Coordinate toad11Coordinate = new Coordinate(2415,3518,0);
    public final Coordinate toad12Coordinate = new Coordinate(2417,3516,0);
    public final Coordinate toad13Coordinate = new Coordinate(2417,3515,0);
    public final Coordinate toad14Coordinate = new Coordinate(2418,3517,0);
    public final Coordinate toad15Coordinate = new Coordinate(2421,3519,0);
    public final Coordinate toad16Coordinate = new Coordinate(2424,3517,0);
    public final Coordinate toad17Coordinate = new Coordinate(2424,3514,0);
    public final Coordinate toad18Coordinate = new Coordinate(2428,3501,0);

    ...

    public TreeToads(){
        currentToad = toad1;
    }

    @Override
    public TreeTask createRootTask() {
        return new Root(this, currentToad);
    }

    ...

    @Override
     public void onItemAdded(ItemEvent event) {
          ItemDefinition definition = event.getItem().getDefinition();
            if (definition != null) {
                if (definition.getName().contains("Swamp toad")) {
                    updateCurrentToad(false);
                }
            }
       }

}


Root.java
Code:
public class Root extends BranchTask {
    private TreeToads bot;
    private static ToadSpawn currentToad;


    public Root(TreeToads bot, ToadSpawn currentToad){
        this.bot = bot;
        this.currentToad = currentToad;
    }

   ...

    public static void updateCurrentToad(boolean reset){
        if(reset){
            System.out.println("Leaving, resetting toad");
            currentToad = toad1;
     } else {
        System.out.println("Toad added, determineCurrentToad called");
        switch (currentToad){
            case toad1: currentToad = toad2;
                 break;
            case toad2: currentToad = toad3;
                break;
            case toad3: currentToad = toad4;
                break;
            case toad4: currentToad = toad5;
                break;
            case toad5: currentToad = toad6;
                break;
            case toad6: currentToad = toad7;
                break;
            case toad7: currentToad = toad8;
                break;
            case toad8: currentToad = toad9;
                break;
            case toad9: currentToad = toad10;
                break;
            case toad10: currentToad = toad11;
                break;
            case toad11: currentToad = toad12;
                break;
            case toad12: currentToad = toad13;
                break;
            case toad13: currentToad = toad14;
                break;
            case toad14: currentToad = toad15;
                break;
            case toad15: currentToad = toad16;
                break;
            case toad16: currentToad = toad17;
                break;
            case toad17: currentToad = toad18;
                break;
            case toad18: currentToad = toad1;
                break;

        }

    }
}


}

isNearby.java
Code:
public class IsNearby extends BranchTask {

private TreeToads bot;
private Locatables locatable;
private ToadSpawn currentToad;

private GameObject obj;
private GroundItem obj2;

public IsNearby(TreeToads bot, Locatables locatable, ToadSpawn currentToad){
    this.bot = bot;
    this.locatable = locatable;
    this.currentToad = currentToad;
}

@Override
public boolean validate() {

    if(locatable == Locatables.bank) {

        obj = GameObjects.newQuery().actions("Bank").within(bot.bankArea).results().nearest();

    } else if(locatable == Locatables.toad) {
        switch (currentToad) {
            case toad1: obj2 = GroundItems.getLoadedOn(bot.toad1Coordinate, "Swamp toad").first();
                break;
            case toad2: obj2 = GroundItems.getLoadedOn(bot.toad2Coordinate, "Swamp toad").first();
                break;
            case toad3: obj2 = GroundItems.getLoadedOn(bot.toad3Coordinate, "Swamp toad").first();
                break;
            case toad4: obj2 = GroundItems.getLoadedOn(bot.toad4Coordinate, "Swamp toad").first();
                break;
            case toad5: obj2 = GroundItems.getLoadedOn(bot.toad5Coordinate, "Swamp toad").first();
                break;
            case toad6: obj2 = GroundItems.getLoadedOn(bot.toad6Coordinate, "Swamp toad").first();
                break;
            case toad7: obj2 = GroundItems.getLoadedOn(bot.toad7Coordinate, "Swamp toad").first();
                break;
            case toad8: obj2 = GroundItems.getLoadedOn(bot.toad8Coordinate, "Swamp toad").first();
                break;
            case toad9: obj2 = GroundItems.getLoadedOn(bot.toad9Coordinate, "Swamp toad").first();
                break;
            case toad10: obj2 = GroundItems.getLoadedOn(bot.toad10Coordinate, "Swamp toad").first();
                break;
            case toad11: obj2 = GroundItems.getLoadedOn(bot.toad11Coordinate, "Swamp toad").first();
                break;
            case toad12: obj2 = GroundItems.getLoadedOn(bot.toad12Coordinate, "Swamp toad").first();
                break;
            case toad13: obj2 = GroundItems.getLoadedOn(bot.toad13Coordinate, "Swamp toad").first();
                break;
            case toad14: obj2 = GroundItems.getLoadedOn(bot.toad14Coordinate, "Swamp toad").first();
                break;
            case toad15: obj2 = GroundItems.getLoadedOn(bot.toad15Coordinate, "Swamp toad").first();
                break;
            case toad16: obj2 = GroundItems.getLoadedOn(bot.toad16Coordinate, "Swamp toad").first();
                break;
            case toad17: obj2 = GroundItems.getLoadedOn(bot.toad17Coordinate, "Swamp toad").first();
                break;
            case toad18: obj2 = GroundItems.getLoadedOn(bot.toad18Coordinate, "Swamp toad").first();
                break;
        }
    }
    return (obj != null && obj.distanceTo(Players.getLocal()) < 12) || (obj2 != null && obj2.distanceTo(Players.getLocal()) < 25);
}

@Override
public TreeTask successTask() {
    if(locatable == Locatables.bank)
        return new OpenBankLeaf(bot);
    else if(locatable == Locatables.toad)
        return new TakeToad(bot, currentToad, obj2);
    else
        return new EmptyLeaf();
}

@Override
public TreeTask failureTask() {
    if(locatable == Locatables.bank)
        return new TraversalLeaf(bot, TraversalLocation.bankArea);
    else if(locatable == Locatables.toad)
        return new TraversalLeaf(bot, TraversalLocation.toadArea);
    else
        return new EmptyLeaf();
}

takeToad.java
Code:
public class TakeToad extends LeafTask {

    private TreeToads bot;
    private ToadSpawn currentToad;

    private GroundItem toad;

    public TakeToad(TreeToads bot, ToadSpawn currentToad, GroundItem obj2){
        this.bot = bot;
        this.currentToad = currentToad;
        this.toad = obj2;
    }



    @Override
    public void execute()
    {

    switch (currentToad) {
    case toad1: toad = GroundItems.getLoadedOn(bot.toad1Coordinate, "Swamp toad").first();
        break;
    case toad2: toad = GroundItems.getLoadedOn(bot.toad2Coordinate, "Swamp toad").first();
        break;
    case toad3: toad = GroundItems.getLoadedOn(bot.toad3Coordinate, "Swamp toad").first();
        break;
    case toad4: toad = GroundItems.getLoadedOn(bot.toad4Coordinate, "Swamp toad").first();
        break;
    case toad5: toad = GroundItems.getLoadedOn(bot.toad5Coordinate, "Swamp toad").first();
        break;
    case toad6: toad = GroundItems.getLoadedOn(bot.toad6Coordinate, "Swamp toad").first();
        break;
    case toad7: toad = GroundItems.getLoadedOn(bot.toad7Coordinate, "Swamp toad").first();
        break;
    case toad8: toad = GroundItems.getLoadedOn(bot.toad8Coordinate, "Swamp toad").first();
        break;
    case toad9: toad = GroundItems.getLoadedOn(bot.toad9Coordinate, "Swamp toad").first();
        break;
    case toad10: toad = GroundItems.getLoadedOn(bot.toad10Coordinate, "Swamp toad").first();
        break;
    case toad11: toad = GroundItems.getLoadedOn(bot.toad11Coordinate, "Swamp toad").first();
        break;
    case toad12: toad = GroundItems.getLoadedOn(bot.toad12Coordinate, "Swamp toad").first();
        break;
    case toad13: toad = GroundItems.getLoadedOn(bot.toad13Coordinate, "Swamp toad").first();
        break;
    case toad14: toad = GroundItems.getLoadedOn(bot.toad14Coordinate, "Swamp toad").first();
        break;
    case toad15: toad = GroundItems.getLoadedOn(bot.toad15Coordinate, "Swamp toad").first();
        break;
    case toad16: toad = GroundItems.getLoadedOn(bot.toad16Coordinate, "Swamp toad").first();
        break;
    case toad17: toad = GroundItems.getLoadedOn(bot.toad17Coordinate, "Swamp toad").first();
        break;
    case toad18: toad = GroundItems.getLoadedOn(bot.toad18Coordinate, "Swamp toad").first();
        break;
}


    if(toad != null)
        {

            if (!toad.isVisible())
                Camera.concurrentlyTurnTo(toad);

            if (toad.interact("Take"))
                Execution.delay(100, 500);

        }
    }
}

map of toads:
https://s1.postimg.org/ww848tsjz/Swamp_Toad_Spawns.png
Swamp_Toad_Spawns.png

Credit to proxi, who's code I'm building ondestroying
 
Last edited:
Joined
Dec 23, 2016
Messages
221
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
 
that went well
Joined
Feb 21, 2017
Messages
7
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
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.
 
Joined
Dec 20, 2016
Messages
37
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.

PHP:
Map<Integer,Coordinate> toadspawns = new HashMap<>();
toadspawns.put(1, new Coordinate(123,123,0));
toadspawns.put(2, new Coordinate(234,234,0));
toadspawns.put(3, new Coordinate(345,345,0));
toadspawns.put(4, new Coordinate(456,456,0));
toadspawns.put(5, new Coordinate(567,567,0));

collectToadAt(toadspawns.get(1));
collectToadAt(toadspawns.get(2));

int[] path1 = new int[] { 1,3,4,2,5 };
collectToadsUsing(path1);
 
Last edited:
Java Warlord
Joined
Nov 17, 2014
Messages
4,906
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 as an integer array, and then use a for loop to add them into a hashmap.

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.

PHP:
Coordinate[] coordinates = new Coordinate[] { new Coordinate(123,123,0), new Coordinate(234,234,1), new Coordinate(345,345,2) };
            Map<Integer,Coordinate> Toadspawns = new HashMap<>();
            for (int i = 0; i < coordinates.length; i++)
            {
                Toadspawns.put(i,coordinates[i]);
            }

            lootAtCoordinate(Toadspawns.get(1));
            lootAtCoordinate(Toadspawns.get(2));
            lootAtCoordinate(Toadspawns.get(3));
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.
 
Joined
Dec 20, 2016
Messages
37
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.
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:
List<Coordinate> coordinates = new ArrayList<Coordinate>();
coordinates.add(new Coordinate(2342,1472,0));
coordinates.add(new Coordinate(2374,1765,0));
coordinates.add(new Coordinate(2395,1151,0));
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.
 
Last edited:
Top