Jump to content

Running from damage help


Arthur

Recommended Posts

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.

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 It didn't work sad.png

 

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

Link to comment
Share on other sites

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) {

	}

}
Link to comment
Share on other sites

 

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()

Link to comment
Share on other sites

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 by Jacareverde
Link to comment
Share on other sites

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 by Hayase
Link to comment
Share on other sites


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

  • Like 1
Link to comment
Share on other sites

Base code is looking good, nice work smile.png 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!! smile.png

 

~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 :D

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