Jump to content

Sample Code


Isolate

Recommended Posts

Saw a post by @faintdrugs and decided to make something because I had a moment.

Don't know if anything's wrong with it, if anyone sees a mistake feel free to call me out.

 

Some Example Code:

import org.osbot.rs07.api.filter.Filter;
import org.osbot.rs07.api.map.PolygonArea;
import org.osbot.rs07.api.model.GroundItem;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;


/**
 * Created by ~Anon on 31/03/2015.
 */

@ScriptManifest(author = "~Anon", name = "Ash Picker", version = 0.1, info = "Picks Up And Banks Ashes!", logo = "")
public class Ashes  extends Script{
    final PolygonArea ZONE = new PolygonArea(/*tiles boardering area*/);
    String lastLoggedStatus = " ";
    String Status = " ";
    private enum States{
        PICK, BANK;
    }

    @Override
    public void onStart() throws InterruptedException {
        log("Welcome To "+getAuthor()+"'s Ash Picker!");
        if(!ZONE.contains(myPlayer())){
            log("Please Start in ZONE!");
            stop(false);
        }
    }


    @Override
    public int onLoop() throws InterruptedException {
        printStatus();
        switch (getState()){
            case BANK:
                Status = "Banking.";
                doBanking();
                break;
            case PICK:
                Status = "Picking.";
                doPicking();
                break;
        }

        return random(250, 500);
    }

    private States getState(){
        if(inventory.isFull()){
            return States.BANK;
        }else{
            return States.PICK;
        }
    }

    private void doBanking() throws InterruptedException {
        if(!bank.isOpen()){
            Status = "Banking: Opening Bank.";
            NPC banker = npcs.closest("Banker");
            if(banker != null) {
                if (banker.isOnScreen()) {
                    Status = "Banking: Interacting Banker.";
                    banker.interact("Bank");
                    sleep(random(500, 900));
                } else {
                    Status = "Banking: Locating Banker.";
                    if (banker.getPosition().distance(myPosition()) > 6) {
                        localWalker.walk(banker);
                    } else {
                        camera.toEntity(banker);
                    }
                }
            }
        }else{
            Status = "Banking: Depositing.";
            bank.depositAll();
          sleep(random(250, 450));
        }
    }

    private void doPicking() throws InterruptedException {
        if(bank.isOpen()){
            Status = "Picking: Closing Bank.";
            bank.close();
        }else{
            GroundItem ashes = groundItems.closest(new Filter<GroundItem>() {
                @Override
                public boolean match(GroundItem groundItem) {
                    return groundItem != null && groundItem.getName().equals("Ashes") && ZONE.contains(groundItem);
                }
            });
            if(ashes != null){
                if(ashes.isOnScreen()){
                    Status = "Picking: Picking Up Ashes.";
                    ashes.interact("Take");
                    sleep(random(500, 900));
                }else{
                    Status = "Picking: Locating Ashes.";
                    if(ashes.getPosition().distance(myPosition())> 6){
                        localWalker.walk(ashes);
                    }else{
                        camera.toEntity(ashes);
                    }
                }
            }
        }
    }

    private void printStatus(){
        if(lastLoggedStatus != Status){
            log("Current Status: " + Status);
            lastLoggedStatus = Status;
        }
    }
}


Note: I'm used to coding everything in the main loop and did my best to convert the code to a more manageable style for newer people

Edited by Isolate
  • Like 1
Link to comment
Share on other sites

  • 3 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.
Note: Your post will require moderator approval before it will be visible.

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.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...