Jump to content

first script... the chop and burn


nubsrevenge

Recommended Posts

major update, first of all its personalized so its prioritizing willow logs now that i have the level for it, will do more trees after i get the level too.  the biggest challenge on this script is finding a blank spot to light the fire, because its not looking for a long line(which i will do later) i have it look for tiles on the map with 0 objects on them, but this means certain ground decoration that you actually are allowed to light fires on will be avoided.  

import java.util.ArrayList;
import java.util.List;

import org.osbot.script.Script;
import org.osbot.script.ScriptManifest;
import org.osbot.script.rs2.map.Position;
import org.osbot.script.rs2.model.RS2Object;

@ScriptManifest(author="nubsrevenge", info="chops and burns trees", name="chopNburn", version=1.3)
public class ChopNBurn extends Script{
	public ChopNBurn(){}

	int tinderbox = 590, fire = 2716;
	int oak = 12608, willow = 12604;
	int[] trees = {1276, 1278};
	int[] logs = {1511, 1521, 1519};
	RS2Object tree;

	enum State {
		BURN, CHOP, WAIT;
	}

	private State state;

	public void onStart(){

	}

	public int onLoop() throws InterruptedException{
		tree = closestObject(willow);
		if(tree == null)
			tree = closestObject(oak);
		if(tree == null)
			tree = closestObject(trees);
		if(tree != null)
			state = State.CHOP;
		if(inventoryContains(logs) && client.getInventory().contains(tinderbox))
			state = State.BURN;
		if(client.getMyPlayer().getAnimation() != -1)
			state = State.WAIT;
		if(this.closestNPCForName("Ent") != null && this.closestNPCForName("Ent").getPosition().distance(this.client.getMyPlayer().getPosition()) <= 1)
			state = State.CHOP;

		switch (state){
		case WAIT:
			sleep(random(300,400));
			break;
		case CHOP:
			chop();
			break;
		case BURN:
			burn();
			break;
		default:
			log("something wrong");
			stop();
		}
		return random(100, 200);
	}

	public void onMessage(String message) throws InterruptedException{
		if(message.contains("level to use"))
			stop();
		if(message.contains("light a fire here"))
			walkToBlankTile();
	}

	void chop() throws InterruptedException{
		tree.interact("Chop down");
		sleep(random(300,400));
	}

	void burn() throws InterruptedException{
		if(getObjectsAt(client.getMyPlayer().getPosition()).size() != 0)
			walkToBlankTile();
		client.getInventory().interactWithId(tinderbox, "Use", false);
		client.getInventory().interactWithNameThatContains("logs", "Use", null, true);
		sleep(random(700,800));
	}

	void walkToBlankTile() throws InterruptedException{
		int x = client.getMyPlayer().getPosition().getX(), y = client.getMyPlayer().getPosition().getY(), z = client.getMyPlayer().getPosition().getZ();

		for(int i = 10; i > 0; i--){
			for(int j = 1; j < 10; j++){
				if(getObjectsAt(new Position(x+i,y+j,z)).size() == 0){
					walkExact(new Position(x+i,y+j,z));
					return;
				}
			}
		}
	}

	public ArrayList<RS2Object> getObjectsAt(Position p){
		List<RS2Object> objects = client.getCurrentRegion().getObjects();
		ArrayList<RS2Object> list = new ArrayList<RS2Object>();
		for(int x = 0; x < objects.size();x++)
			if(objects.get(x).getPosition().equals(p))
				list.add(objects.get(x));
		return list;
	}

	public boolean inventoryContains(int [] ids){
		for(int x = 0; x < ids.length; x++)
			if(client.getInventory().contains(ids[x]))
				return true;
		return false;
	}

	public void onExit(){
		log("died or stopped");
	}
}
Edited by nubsrevenge
  • Like 1
Link to comment
Share on other sites

Why this?

void chop() throws InterruptedException{
		tree.interact("Chop down");
		sleep(random(300,400));
	}

You have no null checks.

 

Change to this ; 

void chop() throws InterruptedException{
                if(tree != null);
                if(tree.isVisible());
		tree.interact("Chop down");
		sleep(random(300,400));
	}

You got this but I prefer to check it again for nulls.

if(tree != null)
			state = State.CHOP;
Edited by jonkohoofd
Link to comment
Share on other sites

 

Why this?

void chop() throws InterruptedException{
		tree.interact("Chop down");
		sleep(random(300,400));
	}

You have no null checks.

 

Change to this ; 

void chop() throws InterruptedException{
                if(tree != null);
                if(tree.isVisible());
		tree.interact("Chop down");
		sleep(random(300,400));
	}

You got this but I prefer to check it again for nulls.

if(tree != null)
			state = State.CHOP;

very true, if somebody took down the targeted tree in the time it took for me to call those it would break.

Link to comment
Share on other sites

  • 2 weeks later...

in the chop method you should find the tree and check for nulls and stuff

RS2Object tree = this.closestObjectForName(area,"Tree"); //dont really know the method w.e. it is you get what i am saying
if(tree != null)
   if(tree.isVisible() && tree.exsists())
       tree.interact("Chop Down");

Something along those lines

also you should make a getState() method in which returns the current state instead of having one global state that you are going off of. and do

switch(getState())
{
case CHOP:
}
Link to comment
Share on other sites

 

in the chop method you should find the tree and check for nulls and stuff

RS2Object tree = this.closestObjectForName(area,"Tree"); //dont really know the method w.e. it is you get what i am saying
if(tree != null)
   if(tree.isVisible() && tree.exsists())
       tree.interact("Chop Down");

Something along those lines

also you should make a getState() method in which returns the current state instead of having one global state that you are going off of. and do

switch(getState())
{
case CHOP:
}

oh you're right i never did add null checks.  as for a getState(), why make a getter method when the global variable can be seen at all times?

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...