August 15, 20169 yr 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
August 15, 20169 yr 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 August 15, 20169 yr by Chris
August 15, 20169 yr 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 new ConditionalSleep(5000) { @ Override public boolean condition() throws InterruptedException { return myPlayer().isUnderAttack(); } }.sleep(); Edited August 15, 20169 yr by IHB
August 15, 20169 yr Author 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
August 15, 20169 yr 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
August 20, 20169 yr Author 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
August 20, 20169 yr Okay thanks man. do you have another link for the api? that ones dead now and I need it :3 The link just doesn't work, try this http://prnt.sc/6xcei0, sleepDeviation isn't used however. Edited August 20, 20169 yr by GaetanoH
August 20, 20169 yr Okay thanks man. do you have another link for the api? that ones dead now and I need it :3 Here: https://web.archive.org/web/20151029095223/http://osbot.org/api/org/osbot/rs07/utility/ConditionalSleep.html
Create an account or sign in to comment