Jump to content

[Open Source] Cowhide Collector FSM


xkalibur13

Recommended Posts

This is an improvement to Zynava's open source project at this link
I hope that this code helps people who are somewhat new to java and the EB API understand an easy way to complete tasks in succession, while practicing good coding style and readability

import com.epicbot.api.shared.APIContext;
import com.epicbot.api.shared.GameType;
import com.epicbot.api.shared.entity.GroundItem;
import com.epicbot.api.shared.methods.*;
import com.epicbot.api.shared.model.Area;
import com.epicbot.api.shared.script.LoopScript;
import com.epicbot.api.shared.script.ScriptManifest;
import com.epicbot.api.shared.util.paint.frame.PaintFrame;
import com.epicbot.api.shared.util.time.Time;
import com.epicbot.api.shared.webwalking.model.RSBank;

import java.awt.Graphics2D;
import java.util.concurrent.ThreadLocalRandom;

@ScriptManifest(name = "Cowhide Collector FSM", gameType = GameType.OS)
public class TestScript extends LoopScript {

    /** CONSTANTS **/
    private static final Area COW_AREA = new Area(3244, 3295, 3262, 3277);
    private static final RSBank BANK = RSBank.LUMBRIDGE_TOP;

    /** VARIABLES **/
    private Status status = Status.LOGGING_IN;

    /** UTIL **/
    private int random(int min, int max) {return ThreadLocalRandom.current().nextInt(min, max + 1);}
    public IInventoryAPI myInventory() {return getAPIContext().inventory();}
    public ILocalPlayerAPI myPlayer() {return getAPIContext().localPlayer();}
    public IGroundItemsAPI groundItems() {return getAPIContext().groundItems();}
    public IWebWalkingAPI webWalking() {return getAPIContext().webWalking();}
    public ICalculationsAPI calculations() {return getAPIContext().calculations();}
    public IWalkingAPI walking() {return getAPIContext().walking();}
    public IBankAPI bank() {return getAPIContext().bank();}
    public ICameraAPI camera() {return getAPIContext().camera();}

    /** SCRIPT OBJECTS **/
    private enum Status {
        LOGGING_IN("Logging in"),
        WALKING_TO_COW_FIELD("Walking to cow field"),
        GATHERING_COW_HIDES("Gathering cow hides"),
        WALKING_TO_BANK("Walking to bank"),
        OPENING_BANK("Opening bank"),
        DEPOSITING_INVENTORY("Depositing inventory"),
        CLOSING_BANK("Closing bank");

        private final String msg;

        Status(String msg) {
            this.msg = msg;
        }

		@Override
        public String toString() {
            return this.msg;
        }
    }

    /** SCRIPT METHODS **/
    @Override
    public boolean onStart(String... strings) {
        System.out.println("Starting " + getManifest().name());
        return true;
    }

    @Override
    protected void onPaint(Graphics2D g, APIContext ctx) {
        PaintFrame frame = new PaintFrame(getManifest().name());
        frame.addLine("Status", status.toString());
        frame.draw(g, 0, 170, ctx);
    }

    @Override
    protected int loop() {
        doTasks();
        return random(50,100);
    }

    /** IMPLEMENTATION **/
    private void doTasks() {
        switch (status) {
            case LOGGING_IN:
                if (getAPIContext().client().isLoggedIn())
                    status = myInventory().isFull() ? Status.WALKING_TO_BANK : Status.WALKING_TO_COW_FIELD;
                break;
            case WALKING_TO_COW_FIELD:
                doWalkToCowField();
                break;
            case GATHERING_COW_HIDES:
                doGatherCowHides();
                break;
            case WALKING_TO_BANK:
                doWalkToBank();
                break;
            case OPENING_BANK:
                doOpenBank();
                break;
            case DEPOSITING_INVENTORY:
                doDepositInventory();
                break;
            case CLOSING_BANK:
                doCloseBank();
                break;
            default:
                break;
        }
    }

    /**
     * Walks to cow field
     */
    private void doWalkToCowField() {
        if(COW_AREA.contains(myPlayer().getLocation())) {
            // WALKING_TO_COW_FIELD -> GATHERING_COW_HIDES
            status = Status.GATHERING_COW_HIDES;
            return;
        }
        webWalking().walkTo(COW_AREA.getRandomTile());
    }

    /**
     * Gathers cow hides, potentially enabling run energy
     */
    private void doGatherCowHides() {
        if(myInventory().isFull()) {
            // GATHERING_COW_HIDES -> WALKING_TO_BANK
            status = Status.WALKING_TO_BANK;
            return;
        }
        if (myPlayer().isMoving())
            return;
        GroundItem cowHide = groundItems().query().named("Cowhide").results().nearest();
        if (cowHide == null)
            return;
        if (!cowHide.isVisible())
            camera().turnTo(cowHide.getLocation());
        if (!walking().isRunEnabled() && walking().getRunEnergy() > 10)
            walking().setRun(true);
        cowHide.interact("Take");
        Time.sleep(random(600,1800));
    }

    /**
     * Walks to bank
     */
    private void doWalkToBank() {
        if(calculations().distanceBetween(BANK.getTile(), myPlayer().getLocation()) < 3) {
            // WALKING_TO_BANK -> OPENING_BANK
            status = Status.OPENING_BANK;
            return;
        }
        webWalking().walkToBank(BANK);
    }

    /**
     * Opens bank
     */
    private void doOpenBank() {
        if(bank().isOpen()) {
            // OPENING_BANK -> DEPOSITING_INVENTORY
            status = Status.DEPOSITING_INVENTORY;
            return;
        }
        bank().open();
    }

    /**
     * Deposits inventory
     */
    private void doDepositInventory() {
        if (myInventory().isEmpty()) {
            // DEPOSITING_INVENTORY -> CLOSING_BANK
            status = Status.CLOSING_BANK;
            return;
        }
        bank().depositInventory();
    }

    /**
     * Closes bank
     */
    private void doCloseBank() {
        if (!bank().isOpen()) {
            // CLOSING_BANK -> WALKING_TO_COW_FIELD (completes the cycle)
            status = Status.WALKING_TO_COW_FIELD;
            return;
        }
        bank().close();
    }
}

There is one instance of Time.sleep(int a) to reduce the number of times the cowhide is clicked. Please drop your suggestions for improvements as though I'm familiar with java I am also somewhat new to using the EB API 🙂

Edited by xkalibur13
changed `getStatusMsg()` to `toString()` with override annotation
Link to comment
Share on other sites

  • 3 weeks later...
  • 2 weeks later...
  • 3 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...