Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Walker;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Point;
- import java.awt.Polygon;
- import org.osbot.script.Script;
- import org.osbot.script.rs2.map.Position;
- import org.osbot.script.rs2.model.Player;
- import org.osbot.script.rs2.model.RS2Object;
- public class Walker {
- private Script s;
- private boolean openDoors;
- private int doorDistance;
- private Path lastPath;
- public Walker(Script scr, int doorDistance1) {
- this.s = scr;
- if (doorDistance1 > 0) {
- this.doorDistance = doorDistance1;
- this.openDoors = true;
- } else {
- this.doorDistance = 1;
- this.openDoors = false;
- }
- }
- public boolean recover() throws InterruptedException {
- PathTile[] tiles = this.lastPath.tiles;
- Position current = this.s.client.getMyPlayer().getPosition();
- int lowestDistance = 999;
- int lowestId = -1;
- for (int x = 0; x < tiles.length; x++) {
- PathTile t = tiles[x];
- if(current.distance(t) < lowestDistance){
- lowestDistance = current.distance(t);
- lowestId = x;
- }
- }
- if(lowestId != -1){
- if(lowestId != tiles.length - 1){
- lowestId += 1;
- }
- Path newPath = this.lastPath;
- newPath.index = lowestId;
- this.walkPath(newPath);
- return true;
- }else{
- this.s.warn("[Walker] Failed to recover path!");
- return false;
- }
- }
- public void walkPath(Path path) throws InterruptedException {
- this.lastPath = path;
- Player p = this.s.client.getMyPlayer();
- this.manageRun(50, 10);
- if (!path.isWalking()) {
- path.start();
- }
- if (!path.shouldStop()) {
- if (p.isMoving()) {
- if (path.shouldNext()) {
- this.walk(path.next());
- }
- } else {
- this.walk(path.next());
- }
- } else {
- path.stop();
- }
- }
- private void walk(PathTile p) throws InterruptedException {
- if (!this.detectDoor()) {
- this.s.walkMiniMap(p);
- } else {
- this.openDoor();
- }
- }
- private boolean detectDoor() {
- if (!this.openDoors) {
- return false;
- }
- RS2Object door = this.s.closestObjectForName("Door");
- if (door != null
- && this.s.client.getMyPlayer().getPosition()
- .distance(door.getPosition()) <= this.doorDistance
- && !this.checkOpenDoor(door)) {
- return true;
- }
- return false;
- }
- // True == open False == close
- private boolean checkOpenDoor(RS2Object door) {
- int orientation = door.getOrientation();
- if (orientation == 0 || orientation == 2) {
- Position check1 = new Position(door.getX() - 1, door.getY(),
- door.getZ());
- Position check2 = new Position(door.getX() + 1, door.getY(),
- door.getZ());
- if (this.s.canReach(check1) && this.s.canReach(check2)) {
- return true;
- }
- }
- if (orientation == 1 || orientation == 3) {
- Position check1 = new Position(door.getX(), door.getY() - 1,
- door.getZ());
- Position check2 = new Position(door.getX(), door.getY() + 1,
- door.getZ());
- if (this.s.canReach(check1) && this.s.canReach(check2)) {
- return true;
- }
- }
- return false;
- }
- private void openDoor() throws InterruptedException {
- RS2Object door = this.s.closestObjectForName("Door");
- if (door != null
- && this.s.client.getMyPlayer().getPosition()
- .distance(door.getPosition()) <= 2) {
- door.interact("Open");
- this.s.sleep(2500);
- }
- }
- private void manageRun(int start, int stop) throws InterruptedException {
- if (this.s.client.getRunEnergy() >= start
- && this.s.client.getRunEnergy() > stop) {
- this.s.setRunning(true);
- }
- if (this.s.client.getRunEnergy() < stop && this.s.isRunning()) {
- this.s.setRunning(false);
- }
- }
- public void drawDebug(Graphics g, Path p) {
- try {
- this.drawPath(g, p, Color.CYAN, Color.RED);
- int widthAcross = this.s.client.getInterface(548).getChild(98)
- .getWidth();
- int heightAcross = this.s.client.getInterface(548).getChild(98)
- .getHeight();
- int paintWidth = widthAcross;
- g.setColor(new Color(0, 0, 0, 150));
- g.fill3DRect(547, 205, paintWidth, heightAcross, true);
- int x = 559;
- int y = 224;
- Color white = new Color(255, 255, 255);
- Font arial = new Font("Arial", 0, 12);
- g.setColor(white);
- g.setFont(arial);
- g.drawString("should Walk Next:" + p.shouldNext(), x, y);
- y += 15;
- g.drawString("Walking: " + p.isWalking(), x, y);
- y += 15;
- g.drawString("Path Index: " + p.index, x, y);
- y += 15;
- g.drawString("Max Index: " + p.maxIndex, x, y);
- y += 15;
- g.drawString("Should stop: " + p.shouldStop(), x, y);
- y += 15;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- // Credits to jelknab
- public void drawPath(Graphics g, Path p, Color c1, Color c2) {
- for (int a = 0; a < p.tiles.length; a++) {
- Position position = p.tiles[a];
- g.setColor(c1);
- if (position.isVisible(this.s.bot)) {
- g.drawPolygon(position.getPolygon(this.s.bot));
- g.setColor(c2);
- if (a != 0 && p.tiles[a - 1].isVisible(this.s.bot)) {
- g.drawLine(getMiddle(position.getPolygon(this.s.bot)).x,
- getMiddle(position.getPolygon(this.s.bot)).y,
- getMiddle(p.tiles[a - 1].getPolygon(this.s.bot)).x,
- getMiddle(p.tiles[a - 1].getPolygon(this.s.bot)).y);
- }
- }
- }
- }
- // Credits to jelknab
- private Point getMiddle(Polygon polygon) {
- int totalX = 0;
- int totalY = 0;
- for (Integer x : polygon.xpoints) {
- totalX += x;
- }
- for (Integer y : polygon.ypoints) {
- totalY += y;
- }
- return new Point(totalX / polygon.xpoints.length, totalY
- / polygon.ypoints.length);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement