Jump to content
Lucas_Larson

Mining bot not running from combat

Recommended Posts

I am learning how to script and have started with a mining script. I have some previous programming knowledge but I'm learning the api.

 

The bot walks to the mining spot then banks when he is done and walks back. When he is getting attacked by a mugger he just keeps trying to mine. It shows in the logs when I am under attack. Not sure how to make him run.

(I would appreciate any tips on how to make my code more efficient as I know it's pretty messy)

 

LINK TO CODE:

https://pastebin.com/1imvcf7D

  • Like 1
Link to post
Share on other sites

You should add the "if under attack" condition to getState() for the state to switch properly. You need to use webwalk to walk to your mining spot. You should use conditional sleeps to detect if your bot has done something, instead of sleeping every time because sometimes the bot doesn't manage to do some things and you have to keep waiting.

I recommend you take a look at this guide and try to make use of conditional sleeps and if statements properly.

https://osbot.org/forum/topic/115124-explvs-scripting-101/

Link to post
Share on other sites

My 2 cents:

you can get better perfomance with a looping bot...and they are easier to write imo 

Quote


       Area miningspot = new Area(3181, 3380, 3185, 3374);

        if (inventory.isFull()) {
            if (Banks.VARROCK_WEST.contains(myPlayer())) {
                if (bank.isOpen()) {
                    bank.depositAll();
                } else {
                    bank.open();
                }
            } else {
                walking.webWalk(Banks.VARROCK_WEST);
            }
        } else {

            if (miningspot.contains(myPlayer())) {
                if (myPlayer().isUnderAttack()) {
                    walking.webWalk(new Position(3169, 3399, 0));
                    // or walking.webWalk(new Area(3169, 3399, 3169, 3399));

                    Thread.sleep(10000);//so it stays there
                } else {
                    if (!myPlayer().isAnimating()) {
                        RS2Object vein = objects.closest(11361);
                        if (vein != null) {
                            vein.interact("Mine");
//                                sleep(random(2000, 3000));
                        }
                    }


                }

            }else{
                getWalking().walk(miningspot);
            }
            
        }

 

 

comments on your code.

 

Quote



public class BasicMiner extends Script {

    private enum State {
        MINE, BANK, RUN
    }

    public State getState() {
        
        //add the under attack here
//        if ((myPlayer().isUnderAttack())){
//            return State.RUN;
//        }
        if (inventory.isFull())
            return State.BANK;
        return State.MINE;
    }

//    Area miningspot = new Area(3181, 3380, 3185, 3374);(1)
    @Override
    public void onStart() {

        //walks to mining area
        Area miningspot = new Area(3181, 3380, 3185, 3374);//make this a class var(1)

        WalkingEvent walkingEvent = new WalkingEvent(miningspot);//(0)
        walkingEvent.setMinDistanceThreshold(0);//(0)
        execute(walkingEvent);//(0)
        
        //just use walking.webWalk(miningspot); it will get you close (0)

        log("I can't believe script writing is this easy! I love learning!");
    }

    public State running() {
        return State.RUN;
    }

    @Override
    public int onLoop() throws InterruptedException {
        switch (getState()) {
            case MINE:
                if (!myPlayer().isAnimating()) {
                    RS2Object vein = objects.closest(11361);
                    if (vein != null) {
                        vein.interact("Mine");
                        sleep(random(2000, 3000));
                    }
                    if ((myPlayer().isUnderAttack())){//geting attacked is an Animaion
                        running();//return statement does nothing and no state changes...
                        log("under attack");
                    }
                }
                break;
            case BANK: //FIX INVENTORY IS FULL
                if (inventory.isFull()) {//messy 
                    walking.webWalk(Banks.VARROCK_WEST);
                    bank.open();
                    sleep(random(2500, 4000));//try not rely on sleeps and try using the state of the 
                                             //world 
                    bank.depositAll();
                    sleep(random(2000, 2500));
                    bank.close();
                    sleep(random(3000, 5000));

                    getWalking().walk(new Area(3181, 3380, 3185, 3374));//walking.webWalk(miningspot)
                    
                    log("walking to mining spot");
                }
                break;
            case RUN:
                if ((myPlayer().isUnderAttack() == true)){//can just be if (myPlayer().isUnderAttack())
                    Position hide = new Position(3169, 3399, 0);//make this a class var

                    WalkingEvent walkingEvent = new WalkingEvent(hide);
                    walkingEvent.setMinDistanceThreshold(0);
                    execute(walkingEvent);  
                    log("running away");
                }

                break;

        }
        return 123;//make this a random(0, 5000)
    }
    
}

 

p.s. uses getter...

 

 

 

 

 

 

 

Edited by Nbacon
  • Like 1
Link to post
Share on other sites
On 1/10/2021 at 4:47 AM, Lucas_Larson said:

 

Based on the logic, once you deposit, your inventory isn't full anymore so you go into mining state and the bot will move to the location without closing the bank. (I think it is good practice to close the bank though, it would be a pattern if you always close the bank by clicking on minimap to walk. (But this is assuming your code works)

The problem was with you not using webwalk for the mining spot.

On 1/10/2021 at 5:03 AM, Nbacon said:

}else{ if(getBank().isOpen()){ getBank().close() }else{ getWalking().walk(miningspot); } }

This would not work. He needs getWalking().webWalk(miningspot) to generate a path to walk on. "walk()" only works if the location is close enough. Another solution is to manually make your path with a list of positions and use walkPath. Again, the guide I linked you to is really useful and could make your script better.

Edited by Jarl
  • Like 1
Link to post
Share on other sites

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.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...