Jump to content

Conditional Sleep


Heist

Recommended Posts

They do different things so I cant think of any reason to ever use a random sleep instead of a conditional sleep.

Random sleeps are for stuff like cutting down trees or mining a rock so it doesnt instantly go to the next one as soon as its finished, but you have to see for yourself if you want to add that to your scripts.

Link to comment
Share on other sites

5 hours ago, Heist said:

Is there any circumstance I should use a random sleep instead of a conditional sleep?

When i only use static sleeps in few cases

  • things just take forever
    • Cannonballs
  • things with no acctions/var bits
    • Pizza making
    • Dough making 
  • Anti-ban?
    • Make your own chat completer that does not use the sleep times that are built. (I think this has lower my bans the most out anyhing ive ever done) 
    • Afking
    • Alching
5 hours ago, Heist said:

Will there be a conditional sleep condition for every circumstance I will come across?

Yea, mostly likly if you program in a looping paterner you can just use the not of the case that gets you into that block of code. 

 

Link to comment
Share on other sites

Just a scenario:

Let's say you are fletching logs. You implement a conditional sleep to check if your inventory doesn't contain anymore logs, or you're not animating anymore. When the conditional sleep has been met, you add a random sleep because it's not human-like to instantly bank for new logs, all the time. This is because humans tend to AFK fletching and it can be a couple of seconds before the human notices the inventory with logs has been fully fletched.

Link to comment
Share on other sites

This thread is perfect for me - a complete noob who doesn't even get conditional sleep. Can someone tell me why the script below leads to my account spam-clicking trees while running towards them, and only starts sleeping only I'm chopping? How should conditional sleeps be initiated and stopped?

 

package core;

import java.awt.Graphics2D;

import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;
import java.util.function.BooleanSupplier;

@ScriptManifest(author = "You", info = "My first script", name = "Tree chopper", version = 0, logo = "")


public class Console extends Script {

    @Override
    public void onStart() {
        log("Let's get started!");
    }

    @Override
    public int onLoop() throws InterruptedException {
        Entity tree = getObjects().closest("Tree");
        if (tree != null && tree.interact("Chop down")) {
            new ConditionalSleep(2000) {
                @Override
                public boolean condition() throws InterruptedException {
                    return !myPlayer().isAnimating();
                }
            }.sleep();
        }
        return 500;
    }

    @Override
    public void onExit() {
        log("This is a wood chopper!");
    }

    @Override
    public void onPaint(Graphics2D g) {

    }

}

 

6 hours ago, Wolk said:

Just a scenario:

Let's say you are fletching logs. You implement a conditional sleep to check if your inventory doesn't contain anymore logs, or you're not animating anymore. When the conditional sleep has been met, you add a random sleep because it's not human-like to instantly bank for new logs, all the time. This is because humans tend to AFK fletching and it can be a couple of seconds before the human notices the inventory with logs has been fully fletched.

How would a method for this look like (in code)? What are the arguments to start/stop the sleeps?

Thanks a lot mates

Link to comment
Share on other sites

11 hours ago, Jebemvammater said:

Can someone tell me why the script below leads to my account spam-clicking trees while running towards them, and only starts sleeping only I'm chopping?

Becuse walking is not an animation.

        if (  !myPlayer().isMoving() && tree != null && tree.interact("Chop down")) {
11 hours ago, Jebemvammater said:

How should conditional sleeps be initiated and stopped?


Here is a good thread on how sleep works.

 

 

11 hours ago, Jebemvammater said:

How would a method for this look like (in code)? What are the arguments to start/stop the sleeps?

You can make your 

 return 500;

into a 

 return random(500,3000);

 

 

 

 

Link to comment
Share on other sites

4 hours ago, Nbacon said:

Becuse walking is not an animation.


        if (  !myPlayer().isMoving() && tree != null && tree.interact("Chop down")) {

Thanks a lot, Nbacon - I inserted that condition in my code above, but the thing was still spamclicking.

I looked at the thread you referenced, and the video in there.

Tried the Lambda thing from Explv's thread below - bot is still clicking on trees more than once ... I really don't seem to understand it.

Entity tree = getObjects().closest("Tree");
        if (tree != null && tree.interact("Chop down") && !myPlayer().isMoving()) {
            Sleep.sleepUntil(() -> !myPlayer().isAnimating(), 5000);
        }

Could you tell me what's wrong in my understanding of the code:

1) Defines the object tree as closest tree to the player

2) The if statement works both to initiate the chopping, but also starts the sleep if the tree exists, if the interaction returned a true (so this means my player started chopping already or is this from the point that it clicked once?) and if my player is not moving

3) The sleep runs for either 5s or until my played has stopped chopping

Really would appreciate any help on this - I've actually read through all threads on sleep on here and have a science degree but seems I'm a brainlet for scripting  

Link to comment
Share on other sites

Hello Jebemvammater,

1 hour ago, Jebemvammater said:

I really don't seem to understand it.

line by line 

Quote

Entity tree = getObjects().closest("Tree");

This a getter. In api code it would be something like  for loop a that gose though all objects on your map title and with matching names it finds the cloes object.

Quote

 if (tree != null && tree.interact("Chop down") && !myPlayer().isMoving()) {

So this is fundmental flaw in your code and I hope you can follow becuase i suck at explaining things.

Java is short circuiting with double operations aka && and || following lines will explain how I under stand it.

Short circuiting is the easy way to fail to skip a statement or go into a statement. What does this mean? In the case of "or" it will run all bools till it hits a true and go inside. For "and" it will run all bools for till it hits a flase and skip the code.(Take way A&&B != B&&A and A||B != B||A In the case of code that interacts with code/changes states. )

 

You use a  short circuiting blocker when you do this. Its why you never get null pointers.


tree != null && tree.interact("Chop down")

Visual of what  Short circuiting  are....

Short circuiting in and staments

Look at this code 


if(state0 && state1){
	Block of code
}

To java this looks like 


if(state0){
	if(state1){
		Block of code
	}
}

What does this mean? 

  •  if state0 is false then State1 will never run.

 

Short circuiting in or staments

Look at this code 


If(state0 || state1 ){

	Block of code

}

To java this looks like 


If(state0){
	Block of code
}else if(state1){
	Block of code

}

What does this mean? 

  •  if state0 is true then State1 will never run.

 

So what is flaw in your code


    if (tree != null && tree.interact("Chop down") && !myPlayer().isMoving()) {
            Sleep.sleepUntil(() -> !myPlayer().isAnimating(), 5000);
        }

 

Explaining how it works first using the short circuiting and idea above.

 


if(tree != null ){ (*1)
	if( tree.interact("Chop down") ){(*2)
		if( !myPlayer().isMoving()){(*3)
			 code
		}
	}
}

 

  1. Is a block and if true will run the code inside
  2. This only returns true if it clicked on the item
  3. This says if the player is moving.

What is the fundmental flaw?

  • once the bot clicks on the tree the bot starts to move making the whole if statment false. Never going into the sleep code.

The fix 


if ( !myPlayer().isAnimating() &&!myPlayer().isMoving() && tree != null && tree.interact("Chop down")) {

 

Why 

!myPlayer().isAnimating() is a block IF this is false then the Im do chopping a tree

!myPlayer().isMoving() is a block IF this is false then Im walking towards a tree

 

 

 

Quote

Sleep.sleepUntil(() -> !myPlayer().isAnimating(), 5000);

 [()-> !myPlayer().isAnimating()]  is a lambda statment

So you can write these like this if that helps

()->{return !myPlayer().isAnimating()}

and think of them like this 

public boolean something() {return !myPlayer().isAnimating();}

and you tell the sleepuntil to check this funtion to know when to stop if it true it will stop. 

 

 

 

Link to comment
Share on other sites

26 minutes ago, Nbacon said:

 

Thanks a lot for taking the time, my dude. I think I'm slowly getting there - but shouldn't it be 

myPlayer().isAnimating() && myPlayer().isMoving() && tree != null && tree.interact("Chop down"))

so without the !negation - i.e. if

  • my player is animating (chopping)
  • and if he is moving (walking to the tree after clicking)
  • and if tree exists
  • and if tree chopping returns true

character is going to sleep? I don't really understand now why I had the !myPlayer().isAnimating and !myPlayer().isMoving() to start with (because I want to start checking the sleep if I am walking/interacting, not if I'm not?) 

But now I don't understand the sequence anymore - why do it in this sequence and not other way around?

Anyways - employing the line above signifiantly reduced the clicking - but at times, it clicks on the tree again just after having finished to walk to it but prior to starting the chopping animination. Is there any way to limit the tree.interact to just once before checking sleep conditions?

Or can I increase the "return" value at the end of my entire onLoop function, so it runs "slower" i.e. less clicking?

 

Sry I have two science degrees but never had any programming apart from 1 course for C++ that I forgot entirely.

Thanks a lot for your help

 

Link to comment
Share on other sites

12 minutes ago, Jebemvammater said:

but shouldn't it be 


myPlayer().isAnimating() && myPlayer().isMoving() && tree != null && tree.interact("Chop down"))

 why do it in this sequence and not other way around?

No. Things are not instantaneous and they are blocking you from clicking the tree

  • !myPlayer().isAnimating()    my player is not choping a tree
    • ! myPlayer().isMoving()  my player is not moving
      • tree != null The tree exist.
        • tree.interact("Chop down")) 

maybe the de morgan's law of the stament will help you get it.

!(myPlayer().isAnimating() || myPlayer().isMoving() || tree == null) && tree.interact("Chop down"))

The important part is 

!(myPlayer().isAnimating() || myPlayer().isMoving() || tree == null)

If any of myPlayer().isAnimating() || myPlayer().isMoving() || tree == null are true you should never click a tree.   so this says they must all most be false for full statment to be true and do the sleep

 

 

 

 

 

 

 

 

Link to comment
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.
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...