Jump to content

Conditional Sleeps?


Tanks

Recommended Posts

Hey I'm making my second script which basically just farms cows for xp. I've been using static sleeps everywhere and was wondering how to implement conditional sleeps? I've tried checking the API but for whatever reason it doesn't let me access it saying there's a server error.

 

Was wondering if someone could give me a quick example and some sample code on how to implement them?

 

My code is here if anyone wants to have a look:

import org.osbot.rs07.api.filter.Filter;
import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.api.ui.Skill;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import java.awt.*;

@ScriptManifest(name = "Cow Killer", author = "Tank", version = 1.0, info = "", logo = "") 
public class TanksCowKiller extends Script {
	
	private final Area cowField = new Area(3043,3313,3021,3298);
	private long startTime;
	
	private enum State {
		ATTACK, WAIT
	};
	
	private State getState(){
    	if(myPlayer().isMoving() || combat.isFighting() || myPlayer().isUnderAttack())
    		return State.WAIT;
    	else
    		return State.ATTACK;
    }
	
	@SuppressWarnings("unchecked")
	private NPC getTarget(){
		NPC targetCow = npcs.closest(new Filter<NPC>(){
			public boolean match(NPC theCow){
				return theCow != null && (theCow.getName().equals("Cow") || theCow.getName().equals("Cow calf")) && 
						theCow.isAttackable() && (theCow.getHealth() != 0) && cowField.contains(theCow);
			}
		});
    	return targetCow;
    }

	
    @[member=Override]
    public void onStart() {
    	
    	startTime = System.currentTimeMillis();
    	
    	for(final Skill skill : new Skill[]{Skill.ATTACK, Skill.STRENGTH, Skill.DEFENCE, Skill.HITPOINTS}) {
    	    getExperienceTracker().start(skill);
    	}
 
    }

    @[member=Override]
    public int onLoop() throws InterruptedException{
    	switch(getState()) {
    	case ATTACK:	
    		NPC cowToKill = getTarget();
    		
    		if(cowToKill != null && !cowToKill.isOnScreen())
    			camera.toEntity(cowToKill);
    		
    		cowToKill.interact("Attack");
    		sleep(random(600,1000));
    		
    		while(cowToKill.isInteracting(myPlayer())) {
    			sleep(random(1500,3000));
    		}
    		
    		break;
    			
    	case WAIT:
    		sleep(random(400,600));
    		break;   
    	}
        return 100;
    }
    
    
    @[member=Override]
    public void onExit() {

    }
    
    //method taken from Explv's paint tutorial -> osbot.org/forum/topic/87697-explvs-dank-paint-tutorial/
    public final String formatTime(final long ms){
        long s = ms / 1000, m = s / 60, h = m / 60;
        s %= 60; m %= 60; h %= 24;
        return String.format("%02d:%02d:%02d", h, m, s);
    }

    @[member=Override]
    public void onPaint(Graphics2D g) {
    	
    	double attackXP = getExperienceTracker().getGainedXP(Skill.ATTACK);
    	double strengthXP = getExperienceTracker().getGainedXP(Skill.STRENGTH);
    	double defenceXP = getExperienceTracker().getGainedXP(Skill.DEFENCE);
    	double hitpointsXP = getExperienceTracker().getGainedXP(Skill.HITPOINTS);
    	int attackLvls = getExperienceTracker().getGainedLevels(Skill.ATTACK);
    	int strengthLvls = getExperienceTracker().getGainedLevels(Skill.STRENGTH);
    	int defenceLvls = getExperienceTracker().getGainedLevels(Skill.DEFENCE);
    	int hitpointsLvls = getExperienceTracker().getGainedLevels(Skill.HITPOINTS);
    	
    	g.drawString("Attack XP gained: " + attackXP, 25, 100);
    	g.drawString("Strength XP gained: " + strengthXP, 25, 120);
    	g.drawString("Defence XP gained: " + defenceXP, 25, 140);
    	g.drawString("HP XP gained: " + hitpointsXP, 25, 160);
    	g.drawString("---", 25, 180);
    	g.drawString("Attack Lvls gained: " + attackLvls, 25, 200);
    	g.drawString("Strength Lvls gained: " + strengthLvls, 25, 220);
    	g.drawString("Defence Lvls gained: " + defenceLvls, 25, 240);
    	g.drawString("HP Lvls gained: " + hitpointsLvls, 25, 260);
    	g.drawString("---", 25, 280);
    	g.drawString("Runtime: " + formatTime((System.currentTimeMillis() - startTime)), 25, 300);

    }


}

Cheers

Link to comment
Share on other sites

try dis but u can change the boolean condition to whatever suits you

if(cowToKill != null){
   if (cowToKill.interact("Attack")){
  	new ConditionalSleep(5_000) { //5 second timeout
            @[member='Override']
            public boolean condition() throws InterruptedException {
                return cowToKill.isInteracting(myPlayer()) && myPlayer().isUnderAttack();
            }
        }.sleep();
   }
}

interact already does checks for distance and onScreen

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

case ATTACK:	
    		NPC cowToKill = getTarget();
    		
    		if(cowToKill != null && !cowToKill.isOnScreen())
    			camera.toEntity(cowToKill);
    		
    		cowToKill.interact("Attack");
    		sleep(random(600,1000));
    		
    		while(cowToKill.isInteracting(myPlayer())) {
    			sleep(random(1500,3000));
    		}
    		
    		break;

In your attack case, you can implement something like this smile.png

new ConditionalSleep(5000) {
		@ Override
		public boolean condition() throws InterruptedException {
		return  myPlayer().isUnderAttack();
		}
		}.sleep();
Edited by IHB
  • Like 1
Link to comment
Share on other sites

Thanks for the replies guys. New to programming so sorry but just to confirm the parameter ConditionalSleep takes is the timeout, so the script will sleep until whatever condition I put in returns false or until the timeout is reached? if the timeout is reached but the boolean still returns true will it loop or would I just use a while loop for it?

 

thanks again guys big help

Link to comment
Share on other sites

Thanks for the replies guys. New to programming so sorry but just to confirm the parameter ConditionalSleep takes is the timeout, so the script will sleep until whatever condition I put in returns false or until the timeout is reached? if the timeout is reached but the boolean still returns true will it loop or would I just use a while loop for it?

 

thanks again guys big help

No, it will sleep until your condition returns true or the timeout is reached. You can check more about it here: https://webcache.googleusercontent.com/search?q=cache:http://osbot.org/api/org/osbot/rs07/utility/ConditionalSleep.html

Link to comment
Share on other sites

No, it will sleep until your condition returns true or the timeout is reached. You can check more about it here: https://webcache.googleusercontent.com/search?q=cache:http://osbot.org/api/org/osbot/rs07/utility/ConditionalSleep.html

Okay thanks man. do you have another link for the api? that ones dead now and I need it :3 

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