Jump to content

Proper way to handle tasks where there is a -1 animation between each activity e.g stringing bows crafting hide bodies etc.


chardragon

Recommended Posts

Hi as title says I know that it is discouraged to use "while" in the loop, but for the activities mentioned you cant use !myPlayer().isAnimating() due to the character hitting -1 animation during the creation of items. For example, when stringing magic longbows every 5 or so I get a -1 animation and so the bot tries to string again. I currently am getting around this using a while loop with a counter which basically will break the loop when it hits a number which means something isnt working properly for whatver reason but I know this isnt a good way to handle it. I am struggling to see anything in the api to help (although i am not the best at reading apis). I was wondering what the consensus is on how to work around this issue?

  • Like 1
Link to comment
Share on other sites

8 minutes ago, chardragon said:

Hey thanks for the reply is there any chance you can give an example? :)

public class AnimationCheck extends Script {
    
    private final Timer animationTimer = new Timer(3_000);

    @Override
    public void onStart() {
        
    }

    @Override
    public int onLoop() {
        
        if(myPlayer().isAnimating()){
            animationTimer.reset();
        }
        
        if(animationTimer.isRunning()){
            log("Animating");
        }else{
            //Start doing whatever you wanted to do
        }
        
        return 50;
    }
}


Basic Timer class

public class Timer {

     private long period;
     private long startTime;

     public Timer(long period) {
          this.period = period;
          startTime = System.currentTimeMillis();
     }

     public boolean isRunning() {
          return getElapsed() < period;
     }

      public long getElapsed() {
           return System.currentTimeMillis() - startTime;
     }

     public long getRemaining() {
           return period - getElapsed();
     }

     public void reset() {
          startTime = System.currentTimeMillis();
     }

     public void stop() {
           period = 0;
     }

    public static String formatTime(long ms) {
        long sec = ms / 1000L;
        return String.format("%02d:%02d:%02d", Long.valueOf(sec / 3600L), Long.valueOf((sec % 3600L) / 60L), Long.valueOf(sec % 60L));
    }
}

 

Edited by Khaleesi
Link to comment
Share on other sites

9 hours ago, chardragon said:

Hi as title says I know that it is discouraged to use "while" in the loop, but for the activities mentioned you cant use !myPlayer().isAnimating() due to the character hitting -1 animation during the creation of items. For example, when stringing magic longbows every 5 or so I get a -1 animation and so the bot tries to string again. I currently am getting around this using a while loop with a counter which basically will break the loop when it hits a number which means something isnt working properly for whatver reason but I know this isnt a good way to handle it. I am struggling to see anything in the api to help (although i am not the best at reading apis). I was wondering what the consensus is on how to work around this issue?

Look at the ConditionalSleep class, it uses a loop but allows interruption

You basically want a nested condition, sleep until you haven't animated for x seconds (sleep until the nested sleep fails)

Link to comment
Share on other sites

import org.osbot.rs07.utility.ConditionalSleep;

import java.util.function.BooleanSupplier;

public class Sleep extends ConditionalSleep {
    private final BooleanSupplier condition;

    public Sleep(final BooleanSupplier condition, final int timeout) {
        super(timeout);
        this.condition = condition;
    }

    public static boolean sleepUntil(final BooleanSupplier condition, final int timeout) {
        return new Sleep(condition, timeout).sleep();
    }

    @Override
    public final boolean condition() {
        return condition.getAsBoolean();
    }
}

Usage

Sleep.sleepUntil(() -> !Sleep.sleepUntil(api.myPlayer()::isAnimating, random(4000, 8000)), random(20000, 40000))

Sleep until haven't animated for 4-8 seconds, returns boolean whether condition was met or timed out

Link to comment
Share on other sites

  • 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...