Jump to content
Dick

Get current tree

Recommended Posts


import org.osbot.script.Script;

import org.osbot.script.ScriptManifest;

import org.osbot.script.rs2.map.Position;

import org.osbot.script.rs2.model.RS2Object;

import org.osbot.script.rs2.utility.Condition;

import java.awt.*;

import java.util.List;

/**

* Created by Robert on 2014-04-18.

* Tip: if you want to get trees fast you will need to dump tree positions data.

* Like you walk to cutting area, dump positions of trees you want to cut, and then you can get objects by:

* client.getCurrentRegion().instance.getTiles()[position.getZ()][localX][localY].getObjects();

* Its alot faster.

*/

@ScriptManifest(name = "Simple chopper", info = "", author = "PolishCivil", version = 1.0D)

public class Chopper extends Script {

final static int[] WOODCUTTING_ANIMS_IDS = new int[]{

867, 868, 869, 870, 871, 872, 873, 874, 875, 876, 877, 878, 879, 880

};

private String treeName = "Tree";

private RS2Object currentTree = null, nextTree = null;

private long lastChopTime;

@Override

public int onLoop() throws InterruptedException {

if (isChoppingTree()) {

lastChopTime = System.currentTimeMillis();

}

if (currentTree == null || !currentTree.exists()) {

log("Searching for: " + treeName);

if (nextTree != null && nextTree.exists()) {

currentTree = nextTree;

} else {

currentTree = getClosestTree(null);

}

if (currentTree == null) {

warn("Couldt find - " + treeName);

return 2500;

}

/*

We are chopping tree now because getting next tree takes a while, so we are saving time.

*/

chopTree();

nextTree = getClosestTree(currentTree.getPosition());

return 300;

}

if (System.currentTimeMillis() - lastChopTime > 2000) {

chopTree();

} else {

return 600;

}

return 100;

}

/**

* Interacts with tree

*

* @throws InterruptedException

*/

private void chopTree() throws InterruptedException {

if (currentTree.interact("Chop down")) {

int dist = distance(currentTree);

if (waitFor(dist * 600 + 1000, new Condition() {

@Override

public boolean evaluate() {

return isChoppingTree() || (currentTree == null || !currentTree.exists());

}

})) {

lastChopTime = System.currentTimeMillis();

}

}

}

@Override

public void onPaint(Graphics a) {

Graphics2D g2d = (Graphics2D) a;

if (currentTree != null && currentTree.exists()) {

Polygon polygon = currentTree.getPosition().getPolygon(bot);

Rectangle bounds = polygon.getBounds();

String text = "Current";

g2d.drawString(text, (int) (bounds.getCenterX() - (g2d.getFontMetrics().stringWidth(text) / 2)), (int) bounds.getCenterY());

g2d.draw(polygon);

}

if (nextTree != null && nextTree.exists() && !currentTree.equals(nextTree)) {

Polygon polygon = nextTree.getPosition().getPolygon(bot);

Rectangle bounds = polygon.getBounds();

String text = "Next";

g2d.drawString(text, (int) (bounds.getCenterX() - (g2d.getFontMetrics().stringWidth(text) / 2)), (int) bounds.getCenterY());

g2d.draw(polygon);

}

}

/**

* Gets closest tree around player.

*

* @param exclude - the position exclude - we dont want tree at this position.

* @return - tree object.

*/

private RS2Object getClosestTree(Position exclude) {

List<RS2Object> rs2Objects = closestObjectListForName(treeName);

RS2Object closest = null;

for (RS2Object rs2Object : rs2Objects) {

if ((closest == null || distance(rs2Object) < distance(closest)) && (exclude == null || rs2Object.getPosition().distance(exclude) != 0)) {

closest = rs2Object;

}

}

return closest;

}

/**

* Checks if player is chopping tree.

*

* @return whether he is.

*/

private boolean isChoppingTree() {

for (int i : WOODCUTTING_ANIMS_IDS) {

if (myPlayer().getAnimation() == i) {

return true;

}

}

return false;

}

/**

* Waits for condition.

*

* @param time - the max time to wait.

* @param c - the condition to check.

* @return - whether condition evaluated successfuly.

*/

public boolean waitFor(int time, Condition c) {

for (int i = 0; (i < time / 50) && !c.evaluate(); i++) {

try {

sleep(50);

} catch (InterruptedException ignore) {

}

}

return c.evaluate();

}

}

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