Botre Posted April 7, 2015 Share Posted April 7, 2015 package org.bjornkrols.lesserdemonkiller; import java.awt.Color; import java.awt.Graphics2D; import org.osbot.rs07.api.model.NPC; import org.osbot.rs07.script.Script; import org.osbot.rs07.script.ScriptManifest; import org.osbot.rs07.utility.ConditionalSleep; /** * A simple combat script that kills lesser demons, perfect for the caged Lesser demon in the wizard tower. * Does not eat. * For educational purposes. * * @author Bjorn Krols (Botre) */ @ScriptManifest(author = "Botre", info = "Kills lesser demons", logo = "", name = "Lesser demon killer", version = 0) public class LesserDemonKiller extends Script { /** * The name of the monster we will be looking for. */ private static final String NAME = "Lesser demon"; /** * The current time in milliseconds, used to calculate the elapsed amount of seconds. */ private long startTime = System.currentTimeMillis(); /** * The possible states of this script. */ private enum State { DIALOGUE, ATTACK, IDLE; } /** * The current state of this script. */ private State state = State.IDLE; @Override public int onLoop() throws InterruptedException { /* * Find the current state and react accordingly. */ switch (state = getState()) { case DIALOGUE: // Sleep for 600 - 6000 milliseconds (so it looks like we're reading the dialogue). sleep(random.nextInt(6000) + 600); // Click continue on any message with a continue option. getDialogues().clickContinue(); break; case ATTACK: // Find the closest NPC with the name NAME. NPC target = getNpcs().closest(NAME); // Attack the target if it isn't null and exists. if (target != null && target.exists() && target.interact("Attack")) { // If the target was successfully attacked, sleep until player is animating or interacting. new ConditionalSleep(5000, 100) { @Override public boolean condition() throws InterruptedException { return myPlayer().isAnimating() || myPlayer().getInteracting() != null; } }.sleep(); // Don't forget the .sleep(); ! } break; case IDLE: // Nothing is done here, you could add some random behavior though. break; } //Sleep for 300 - 600 milliseconds every loop. return random(300) + 300; } private State getState() { // If a dialogue is pending continuation, go continue it! if (getDialogues().isPendingContinuation()) return State.DIALOGUE; // If we are currently attacking a monster, idle. if (myPlayer().isAnimating() || myPlayer().getInteracting() != null) return State.IDLE; // We are not attacking a monster, let's stop being lazy and get to work! return State.ATTACK; } @Override public void onPaint(Graphics2D g2d) { super.onPaint(g2d); // Set the drawing color to yellow. g2d.setColor(Color.YELLOW); // Draw the current state, useful for debugging. g2d.drawString("State: " + state, 20, 250); // Draw the elapsed amount of seconds since the script was started. g2d.drawString("Elapsed seconds: " + (int) ((System.currentTimeMillis() - startTime) / 1000), 20, 265); } } 3 Quote Link to comment Share on other sites More sharing options...
Solace Posted April 7, 2015 Share Posted April 7, 2015 i dont understand anything of this but u are doing a great job doing script for the people (f2p) nice Quote Link to comment Share on other sites More sharing options...
Mysteryy Posted April 7, 2015 Share Posted April 7, 2015 States though Botre? States..? 3 Quote Link to comment Share on other sites More sharing options...
Botre Posted April 7, 2015 Author Share Posted April 7, 2015 States though Botre? States..? Went back to basics for simplicity-sake. States are perfect for beginners. This is what an actual Botre onLoop() looks like : @Override public int onLoop() throws InterruptedException { if (isOnline()) { if (eventChopTree.canDo()) eventChopTree.execute(); else if (eventBurnLogs.canDo()) eventBurnLogs.execute(); else eventDropAll.execute(); } return super.onLoop(); } 1 Quote Link to comment Share on other sites More sharing options...
Reid Posted April 7, 2015 Share Posted April 7, 2015 States though Botre? States..? Quote Link to comment Share on other sites More sharing options...
Botre Posted April 7, 2015 Author Share Posted April 7, 2015 States though Botre? States..? Went back to basics for simplicity-sake. States are perfect for beginners. This is what an actual Botre onLoop() looks like : @Override public int onLoop() throws InterruptedException { if (isOnline()) { if (eventChopTree.canDo()) eventChopTree.execute(); else if (eventBurnLogs.canDo()) eventBurnLogs.execute(); else eventDropAll.execute(); } return super.onLoop(); } Quote Link to comment Share on other sites More sharing options...
idntbot Posted April 7, 2015 Share Posted April 7, 2015 Thanks for the code. I need to learn soon lol. Quote Link to comment Share on other sites More sharing options...
Twin Posted April 7, 2015 Share Posted April 7, 2015 States though Botre? States..? What's wrong with states? :,( Quote Link to comment Share on other sites More sharing options...
Mysteryy Posted April 7, 2015 Share Posted April 7, 2015 What's wrong with states? :,( The problem lies in the question. :p Its just an opinion, but nodes/task based tends to be cleaner, and you can reuse more code with it. Overall it has more capabilities and usually is more organized. Thats just my opinion but most people have used both, and I would bet you that most people choose node/task based in the end over states. Quote Link to comment Share on other sites More sharing options...
Twin Posted April 7, 2015 Share Posted April 7, 2015 The problem lies in the question. Its just an opinion, but nodes/task based tends to be cleaner, and you can reuse more code with it. Overall it has more capabilities and usually is more organized. Thats just my opinion but most people have used both, and I would bet you that most people choose node/task based in the end over states. I know I was just kidding. I write all my things in states just because it's a lot easier for me to follow. I know that makes like no sense since its really sloppy but for some reason I can follow everything easily. Quote Link to comment Share on other sites More sharing options...
Botre Posted April 7, 2015 Author Share Posted April 7, 2015 I know I was just kidding. I write all my things in states just because it's a lot easier for me to follow. I know that makes like no sense since its really sloppy but for some reason I can follow everything easily. One-class-state-based scripts tend to have low re-usability. All designs have their flaws, I still like to take out the state machine for little 5-minutes scripts like these :p Quote Link to comment Share on other sites More sharing options...
VladBots Posted April 7, 2015 Share Posted April 7, 2015 States though Botre? States..? for a small script such as this I think his choice to use states was for the better 1 Quote Link to comment Share on other sites More sharing options...
Mysteryy Posted April 7, 2015 Share Posted April 7, 2015 for a small script such as this I think his choice to use states was for the better It was I'm just giving him shit. Botre knows I love him. new scripters wouled have a hard time understanding task based anyway, that is why everyone makes tutorials using states. 1 Quote Link to comment Share on other sites More sharing options...
Precise Posted April 7, 2015 Share Posted April 7, 2015 I'm sure this will benefit many users! ^_^ Quote Link to comment Share on other sites More sharing options...
Joseph Posted April 7, 2015 Share Posted April 7, 2015 Learn learn! Quote Link to comment Share on other sites More sharing options...