January 23, 20179 yr Hi, I'm very new to scripting and I'm trying to make a woodcutting bot that runs from danger. The whole banking and cutting works, but the running from danger doesn't. Currently I only have this, and it just doesn't seem to work at all. case HEALTHCHECK: int checkHealth = (getSkills().getDynamic(Skill.HITPOINTS)); if (checkHealth <= 7) { getWalking().walk(new Position(3096,3241,0)); break; } Please help me figure this out.
January 23, 20179 yr Hi, I'm very new to scripting and I'm trying to make a woodcutting bot that runs from danger. The whole banking and cutting works, but the running from danger doesn't. Currently I only have this, and it just doesn't seem to work at all. case HEALTHCHECK: int checkHealth = (getSkills().getDynamic(Skill.HITPOINTS)); if (checkHealth <= 7) { getWalking().walk(new Position(3096,3241,0)); break; } Please help me figure this out. case HEALTHCHECK: int checkHealth = (getSkills().getDynamic(Skill.HITPOINTS)); if (checkHealth <= 7) { getWalking().walk(new Position(3096,3241,0)); } break; Try
January 23, 20179 yr Author case HEALTHCHECK: int checkHealth = (getSkills().getDynamic(Skill.HITPOINTS)); if (checkHealth <= 7) { getWalking().walk(new Position(3096,3241,0)); } break; Try It didn't work I tried making my code this public int onLoop() throws InterruptedException { int HP = (getSkills().getDynamic(Skill.HITPOINTS)); boolean lowHealth = (HP <= 7); switch (getState()) { case CUT: Entity tree = objects.closest("Tree"); if (tree.exists()) { tree.interact("Chop down"); while (tree.exists()) { sleep(random(500, 700)); } } if (lowHealth == true) { getWalking().walk(new Position(3096,3241,0)); } break; case BANK: if (getBank().open()) { getBank().depositAll("Logs"); getBank().close(); } if (!getBank().open()) { getBank().open(); } getWalking().walk(new Position(3101, 3246, 0)); if (lowHealth == true) { getWalking().walk(new Position(3096,3241,0)); } break; case WAIT: sleep(random(500, 700)); if (lowHealth == true) { getWalking().walk(new Position(3096,3241,0)); } break; } return random(200, 300); } But to no avail.
January 23, 20179 yr It didn't work I tried making my code this public int onLoop() throws InterruptedException { int HP = (getSkills().getDynamic(Skill.HITPOINTS)); boolean lowHealth = (HP <= 7); switch (getState()) { case CUT: Entity tree = objects.closest("Tree"); if (tree.exists()) { tree.interact("Chop down"); while (tree.exists()) { sleep(random(500, 700)); } } if (lowHealth == true) { getWalking().walk(new Position(3096,3241,0)); } break; case BANK: if (getBank().open()) { getBank().depositAll("Logs"); getBank().close(); } if (!getBank().open()) { getBank().open(); } getWalking().walk(new Position(3101, 3246, 0)); if (lowHealth == true) { getWalking().walk(new Position(3096,3241,0)); } break; case WAIT: sleep(random(500, 700)); if (lowHealth == true) { getWalking().walk(new Position(3096,3241,0)); } break; } return random(200, 300); } But to no avail. Where is rest of the code or is this all ? Using switching like this is .. pointless
January 23, 20179 yr Author Where is rest of the code or is this all ? Using switching like this is .. pointless import org.osbot.rs07.api.map.Position; import org.osbot.rs07.api.model.Entity; import org.osbot.rs07.api.ui.Skill; import org.osbot.rs07.script.Script; import org.osbot.rs07.script.ScriptManifest; import java.awt.*; @ScriptManifest(author = "Jacareverde", info = "My first script", name = "Woodcutting", version = 0, logo = "") public class main extends Script { @[member=Override] public void onStart() { log("Welcome to Jacareverde's Woodcutter!"); log("If you experience any issues while running this script please report them to me on the forums."); } public int checkHealth = 0; private enum State { CUT, BANK, WAIT }; public State getState() { Entity tree = objects.closest("Tree"); if (inventory.isFull()) return State.BANK; if (tree != null) return State.CUT; return State.WAIT; } @[member=Override] public int onLoop() throws InterruptedException {; switch (getState()) { case CUT: Entity tree = objects.closest("Tree"); if (tree.exists()) { tree.interact("Chop down"); while (tree.exists()) { sleep(random(500, 700)); } } break; case BANK: if (getBank().open()) { getBank().depositAll("Logs"); getBank().close(); } if (!getBank().open()) { getBank().open(); } getWalking().walk(new Position(3101, 3246, 0)); break; case WAIT: sleep(random(500, 700)); break; } return random(200, 300); } @[member=Override] public void onExit() { log("Thanks for running my Woodcutter!"); } @[member=Override] public void onPaint(Graphics2D g) { } } I used @@Apaec's guide for the formatting of it. Sorry for the confusion everyone, but this is my code from now. import org.osbot.rs07.api.map.Position; import org.osbot.rs07.api.model.Entity; import org.osbot.rs07.api.ui.Skill; import org.osbot.rs07.script.Script; import org.osbot.rs07.script.ScriptManifest; import java.awt.*; @ScriptManifest(author = "Jacareverde", info = "My first script", name = "Woodcutting", version = 0, logo = "") public class main extends Script { @[member=Override] public void onStart() { log("Welcome to Jacareverde's Woodcutter!"); log("If you experience any issues while running this script please report them to me on the forums."); } public int checkHealth = 0; private enum State { CUT, BANK, WAIT, HEALTHCHECK }; public State getState() { Entity tree = objects.closest("Tree"); if (inventory.isFull()) return State.BANK; if (tree != null) return State.CUT; return State.WAIT; } @[member=Override] public int onLoop() throws InterruptedException {; int HP = (getSkills().getDynamic(Skill.HITPOINTS)); boolean lowHealth = (HP <= 7); switch (getState()) { case CUT: Entity tree = objects.closest("Tree"); if (tree.exists()) { tree.interact("Chop down"); while (tree.exists()) { sleep(random(500, 700)); } } break; case BANK: if (getBank().open()) { getBank().depositAll("Logs"); getBank().close(); } if (!getBank().open()) { getBank().open(); } getWalking().walk(new Position(3101, 3246, 0)); break; case WAIT: sleep(random(500, 700)); break; case HEALTHCHECK: if (lowHealth == true) { getWalking().walk(new Position(3096,3241,0)); } break; } return random(200, 300); } @[member=Override] public void onExit() { log("Thanks for running my Woodcutter!"); } @[member=Override] public void onPaint(Graphics2D g) { } }
January 23, 20179 yr Hi, I'm very new to scripting and I'm trying to make a woodcutting bot that runs from danger. The whole banking and cutting works, but the running from danger doesn't. Currently I only have this, and it just doesn't seem to work at all. case HEALTHCHECK: int checkHealth = (getSkills().getDynamic(Skill.HITPOINTS)); if (checkHealth <= 7) { getWalking().walk(new Position(3096,3241,0)); break; } Please help me figure this out. can we see your getState()
January 23, 20179 yr I posted my full code above. yeah just saw it... you need to put 'healthcheck' in your getstate if you want it to be run. it can't know to run it otherwise...
January 23, 20179 yr Author yeah just saw it... you need to put 'healthcheck' in your getstate if you want it to be run. it can't know to run it otherwise... Damn, I knew it was some stupid mistake I made. Thank you! But then where should I put these variables? int HP = (getSkills().getDynamic(Skill.HITPOINTS)); boolean lowHealth = (HP <= 7); Edited January 23, 20179 yr by Jacareverde
January 23, 20179 yr while (tree.exists()) { sleep(random(500, 700)); } This is very bad. If you are chopping and under attack the while loop will never exit. Look into a conditional sleep instead, and also add a condition if you suddenly get under attack. new ConditionalSleep(5_000) { @[member='Override'] public boolean condition() throws InterruptedException { return !tree.exists() || myPlayer().isUnderAttack(); } }.sleep(); Edited January 23, 20179 yr by Hayase
January 23, 20179 yr Sorry for the confusion everyone, but this is my code from now. import org.osbot.rs07.api.map.Position; import org.osbot.rs07.api.model.Entity; import org.osbot.rs07.api.ui.Skill; import org.osbot.rs07.script.Script; import org.osbot.rs07.script.ScriptManifest; import java.awt.*; @ScriptManifest(author = "Jacareverde", info = "My first script", name = "Woodcutting", version = 0, logo = "") public class main extends Script { @[member='Override'] public void onStart() { log("Welcome to Jacareverde's Woodcutter!"); log("If you experience any issues while running this script please report them to me on the forums."); } public int checkHealth = 0; private enum State { CUT, BANK, WAIT, HEALTHCHECK }; public State getState() { Entity tree = objects.closest("Tree"); if (inventory.isFull()) return State.BANK; if (tree != null) return State.CUT; return State.WAIT; } @[member='Override'] public int onLoop() throws InterruptedException {; int HP = (getSkills().getDynamic(Skill.HITPOINTS)); boolean lowHealth = (HP <= 7); switch (getState()) { case CUT: Entity tree = objects.closest("Tree"); if (tree.exists()) { tree.interact("Chop down"); while (tree.exists()) { sleep(random(500, 700)); } } break; case BANK: if (getBank().open()) { getBank().depositAll("Logs"); getBank().close(); } if (!getBank().open()) { getBank().open(); } getWalking().walk(new Position(3101, 3246, 0)); break; case WAIT: sleep(random(500, 700)); break; case HEALTHCHECK: if (lowHealth == true) { getWalking().walk(new Position(3096,3241,0)); } break; } return random(200, 300); } @[member='Override'] public void onExit() { log("Thanks for running my Woodcutter!"); } @[member='Override'] public void onPaint(Graphics2D g) { } } Base code is looking good, nice work The health checking needs some work still...! The idea of the case based structure is to make the code more readable and easier to debug. It also makes adding additional functions to the script a little bit easier. As you rightly guessed, we are going to need a new state, which you've called HEALTHCHECK. Perhaps would be better to call this state RUN_FROM_COMBAT or something similar, as the state determines the 'action' code rather than the check. Additionally, you're messing around with global variables and relying on these values in other functions. While this is perfectly valid, in this situation it's somewhat unnecessary - you can simply do the health checking in the getState() function (after all, that's what it's there for!). Finally, it would be alot cleaner and universal if you were to use hp percentage thresholds rather than straight hp thresholds. While i'm not a fan of giving code straight up, I feel that in this case it's important that you get this right to continue your journey! I'll show you how you can go about solving this issue. Here's a simple way to go about the issue which follows the initial state based structure: > Note that i've changed the state name to RUN_FROM_COMBAT and the calculations are done in the getState() function. (Also, i've written this all in the reply box and haven't tested it, so there may well be typos / errors. Sorry!) import org.osbot.rs07.api.map.Position; import org.osbot.rs07.api.model.Entity; import org.osbot.rs07.api.ui.Skill; import org.osbot.rs07.script.Script; import org.osbot.rs07.script.ScriptManifest; import java.awt.*; @ScriptManifest(author = "Jacareverde", info = "My first script", name = "Woodcutting", version = 0, logo = "") public class main extends Script { public int hpThreshold = 30; // Percent! @[member='Override'] public void onStart() { log("Welcome to Jacareverde's Woodcutter!"); log("If you experience any issues while running this script please report them to me on the forums."); } private enum State { CUT, BANK, WAIT, RUN_FROM_COMBAT }; public State getState() { Entity tree = objects.closest("Tree"); int hpPercentage = (getSkills().getDynamic(Skill.HITPOINTS) * 100) / getSkills().getStatic(Skill.HITPOINTS); if (hpPercentage < hpThreshold) return State.RUN_FROM_COMBAT; if (inventory.isFull()) return State.BANK; if (tree != null) return State.CUT; return State.WAIT; } @[member='Override'] public int onLoop() throws InterruptedException {; int HP = (getSkills().getDynamic(Skill.HITPOINTS)); boolean lowHealth = (HP <= 7); switch (getState()) { case CUT: Entity tree = objects.closest("Tree"); if (tree.exists()) { tree.interact("Chop down"); while (tree.exists()) { sleep(random(500, 700)); } } break; case BANK: if (getBank().open()) { getBank().depositAll("Logs"); getBank().close(); } if (!getBank().open()) { getBank().open(); } getWalking().walk(new Position(3101, 3246, 0)); break; case WAIT: sleep(random(500, 700)); break; case RUN_FROM_COMBAT: if (lowHealth == true) { getWalking().walk(new Position(3096,3241,0)); } break; } return random(200, 300); } @[member='Override'] public void onExit() { log("Thanks for running my Woodcutter!"); } @[member='Override'] public void onPaint(Graphics2D g) { } } Let me know if that does/doesn't work, and good luck!! ~apa
January 23, 20179 yr Author Base code is looking good, nice work The health checking needs some work still...! The idea of the case based structure is to make the code more readable and easier to debug. It also makes adding additional functions to the script a little bit easier. As you rightly guessed, we are going to need a new state, which you've called HEALTHCHECK. Perhaps would be better to call this state RUN_FROM_COMBAT or something similar, as the state determines the 'action' code rather than the check. Additionally, you're messing around with global variables and relying on these values in other functions. While this is perfectly valid, in this situation it's somewhat unnecessary - you can simply do the health checking in the getState() function (after all, that's what it's there for!). Finally, it would be alot cleaner and universal if you were to use hp percentage thresholds rather than straight hp thresholds. While i'm not a fan of giving code straight up, I feel that in this case it's important that you get this right to continue your journey! I'll show you how you can go about solving this issue. Here's a simple way to go about the issue which follows the initial state based structure: > Note that i've changed the state name to RUN_FROM_COMBAT and the calculations are done in the getState() function. (Also, i've written this all in the reply box and haven't tested it, so there may well be typos / errors. Sorry!) import org.osbot.rs07.api.map.Position; import org.osbot.rs07.api.model.Entity; import org.osbot.rs07.api.ui.Skill; import org.osbot.rs07.script.Script; import org.osbot.rs07.script.ScriptManifest; import java.awt.*; @ScriptManifest(author = "Jacareverde", info = "My first script", name = "Woodcutting", version = 0, logo = "") public class main extends Script { public int hpThreshold = 30; // Percent! @[member='Override'] public void onStart() { log("Welcome to Jacareverde's Woodcutter!"); log("If you experience any issues while running this script please report them to me on the forums."); } private enum State { CUT, BANK, WAIT, RUN_FROM_COMBAT }; public State getState() { Entity tree = objects.closest("Tree"); int hpPercentage = (getSkills().getDynamic(Skill.HITPOINTS) * 100) / getSkills().getStatic(Skill.HITPOINTS); if (hpPercentage < hpThreshold) return State.RUN_FROM_COMBAT; if (inventory.isFull()) return State.BANK; if (tree != null) return State.CUT; return State.WAIT; } @[member='Override'] public int onLoop() throws InterruptedException {; int HP = (getSkills().getDynamic(Skill.HITPOINTS)); boolean lowHealth = (HP <= 7); switch (getState()) { case CUT: Entity tree = objects.closest("Tree"); if (tree.exists()) { tree.interact("Chop down"); while (tree.exists()) { sleep(random(500, 700)); } } break; case BANK: if (getBank().open()) { getBank().depositAll("Logs"); getBank().close(); } if (!getBank().open()) { getBank().open(); } getWalking().walk(new Position(3101, 3246, 0)); break; case WAIT: sleep(random(500, 700)); break; case RUN_FROM_COMBAT: if (lowHealth == true) { getWalking().walk(new Position(3096,3241,0)); } break; } return random(200, 300); } @[member='Override'] public void onExit() { log("Thanks for running my Woodcutter!"); } @[member='Override'] public void onPaint(Graphics2D g) { } } Let me know if that does/doesn't work, and good luck!! ~apa while (tree.exists()) { sleep(random(500, 700)); } This is very bad. If you are chopping and under attack the while loop will never exit. Look into a conditional sleep instead, and also add a condition if you suddenly get under attack. new ConditionalSleep(5_000) { @[member='Override'] public boolean condition() throws InterruptedException { return !tree.exists() || myPlayer().isUnderAttack(); } }.sleep(); Thank you both so much for your help
January 23, 20179 yr Entity tree = objects.closest("Tree"); if (tree.exists()) { This'll give you a nice ol'e NullPointerException if there aren't any trees around Nullcheck it Edited January 23, 20179 yr by FrostBug
January 28, 20179 yr Instead of checking your health, check if youre under attack. Also use dynamic sleep, not that while static sleep crap
Create an account or sign in to comment