Advertisement
OSBotMerccy

Untitled

Jul 4th, 2013
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.61 KB | None | 0 0
  1. package Walker;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.awt.Point;
  7. import java.awt.Polygon;
  8.  
  9. import org.osbot.script.Script;
  10. import org.osbot.script.rs2.map.Position;
  11. import org.osbot.script.rs2.model.Player;
  12. import org.osbot.script.rs2.model.RS2Object;
  13.  
  14. public class Walker {
  15.  
  16.     private Script s;
  17.     private boolean openDoors;
  18.     private int doorDistance;
  19.     private Path lastPath;
  20.  
  21.     public Walker(Script scr, int doorDistance1) {
  22.     this.s = scr;
  23.     if (doorDistance1 > 0) {
  24.         this.doorDistance = doorDistance1;
  25.         this.openDoors = true;
  26.     } else {
  27.         this.doorDistance = 1;
  28.         this.openDoors = false;
  29.     }
  30.     }
  31.  
  32.     public boolean recover() throws InterruptedException {
  33.     PathTile[] tiles = this.lastPath.tiles;
  34.     Position current = this.s.client.getMyPlayer().getPosition();
  35.     int lowestDistance = 999;
  36.     int lowestId = -1;
  37.     for (int x = 0; x < tiles.length; x++) {
  38.         PathTile t = tiles[x];
  39.         if(current.distance(t) < lowestDistance){
  40.         lowestDistance = current.distance(t);
  41.         lowestId = x;
  42.         }
  43.     }
  44.     if(lowestId != -1){
  45.         if(lowestId != tiles.length - 1){
  46.         lowestId += 1;
  47.         }
  48.         Path newPath = this.lastPath;
  49.         newPath.index = lowestId;
  50.         this.walkPath(newPath);
  51.         return true;
  52.     }else{
  53.         this.s.warn("[Walker] Failed to recover path!");
  54.         return false;
  55.     }
  56.     }
  57.  
  58.     public void walkPath(Path path) throws InterruptedException {
  59.     this.lastPath = path;
  60.     Player p = this.s.client.getMyPlayer();
  61.     this.manageRun(50, 10);
  62.     if (!path.isWalking()) {
  63.         path.start();
  64.     }
  65.     if (!path.shouldStop()) {
  66.         if (p.isMoving()) {
  67.         if (path.shouldNext()) {
  68.             this.walk(path.next());
  69.         }
  70.         } else {
  71.         this.walk(path.next());
  72.         }
  73.     } else {
  74.         path.stop();
  75.     }
  76.     }
  77.  
  78.     private void walk(PathTile p) throws InterruptedException {
  79.     if (!this.detectDoor()) {
  80.         this.s.walkMiniMap(p);
  81.     } else {
  82.         this.openDoor();
  83.     }
  84.     }
  85.  
  86.     private boolean detectDoor() {
  87.     if (!this.openDoors) {
  88.         return false;
  89.     }
  90.     RS2Object door = this.s.closestObjectForName("Door");
  91.     if (door != null
  92.         && this.s.client.getMyPlayer().getPosition()
  93.             .distance(door.getPosition()) <= this.doorDistance
  94.         && !this.checkOpenDoor(door)) {
  95.         return true;
  96.     }
  97.     return false;
  98.     }
  99.  
  100.     // True == open False == close
  101.     private boolean checkOpenDoor(RS2Object door) {
  102.     int orientation = door.getOrientation();
  103.     if (orientation == 0 || orientation == 2) {
  104.         Position check1 = new Position(door.getX() - 1, door.getY(),
  105.             door.getZ());
  106.         Position check2 = new Position(door.getX() + 1, door.getY(),
  107.             door.getZ());
  108.         if (this.s.canReach(check1) && this.s.canReach(check2)) {
  109.         return true;
  110.         }
  111.     }
  112.     if (orientation == 1 || orientation == 3) {
  113.         Position check1 = new Position(door.getX(), door.getY() - 1,
  114.             door.getZ());
  115.         Position check2 = new Position(door.getX(), door.getY() + 1,
  116.             door.getZ());
  117.         if (this.s.canReach(check1) && this.s.canReach(check2)) {
  118.         return true;
  119.         }
  120.     }
  121.     return false;
  122.     }
  123.  
  124.     private void openDoor() throws InterruptedException {
  125.     RS2Object door = this.s.closestObjectForName("Door");
  126.     if (door != null
  127.         && this.s.client.getMyPlayer().getPosition()
  128.             .distance(door.getPosition()) <= 2) {
  129.         door.interact("Open");
  130.         this.s.sleep(2500);
  131.     }
  132.     }
  133.  
  134.     private void manageRun(int start, int stop) throws InterruptedException {
  135.     if (this.s.client.getRunEnergy() >= start
  136.         && this.s.client.getRunEnergy() > stop) {
  137.         this.s.setRunning(true);
  138.     }
  139.     if (this.s.client.getRunEnergy() < stop && this.s.isRunning()) {
  140.         this.s.setRunning(false);
  141.     }
  142.     }
  143.  
  144.     public void drawDebug(Graphics g, Path p) {
  145.     try {
  146.         this.drawPath(g, p, Color.CYAN, Color.RED);
  147.         int widthAcross = this.s.client.getInterface(548).getChild(98)
  148.             .getWidth();
  149.         int heightAcross = this.s.client.getInterface(548).getChild(98)
  150.             .getHeight();
  151.         int paintWidth = widthAcross;
  152.         g.setColor(new Color(0, 0, 0, 150));
  153.         g.fill3DRect(547, 205, paintWidth, heightAcross, true);
  154.         int x = 559;
  155.         int y = 224;
  156.         Color white = new Color(255, 255, 255);
  157.         Font arial = new Font("Arial", 0, 12);
  158.         g.setColor(white);
  159.         g.setFont(arial);
  160.         g.drawString("should Walk Next:" + p.shouldNext(), x, y);
  161.         y += 15;
  162.         g.drawString("Walking: " + p.isWalking(), x, y);
  163.         y += 15;
  164.         g.drawString("Path Index: " + p.index, x, y);
  165.         y += 15;
  166.         g.drawString("Max Index: " + p.maxIndex, x, y);
  167.         y += 15;
  168.         g.drawString("Should stop: " + p.shouldStop(), x, y);
  169.         y += 15;
  170.     } catch (Exception e) {
  171.         e.printStackTrace();
  172.     }
  173.     }
  174.  
  175.     // Credits to jelknab
  176.     public void drawPath(Graphics g, Path p, Color c1, Color c2) {
  177.     for (int a = 0; a < p.tiles.length; a++) {
  178.         Position position = p.tiles[a];
  179.         g.setColor(c1);
  180.  
  181.         if (position.isVisible(this.s.bot)) {
  182.         g.drawPolygon(position.getPolygon(this.s.bot));
  183.  
  184.         g.setColor(c2);
  185.         if (a != 0 && p.tiles[a - 1].isVisible(this.s.bot)) {
  186.             g.drawLine(getMiddle(position.getPolygon(this.s.bot)).x,
  187.                 getMiddle(position.getPolygon(this.s.bot)).y,
  188.                 getMiddle(p.tiles[a - 1].getPolygon(this.s.bot)).x,
  189.                 getMiddle(p.tiles[a - 1].getPolygon(this.s.bot)).y);
  190.         }
  191.         }
  192.     }
  193.     }
  194.  
  195.     // Credits to jelknab
  196.     private Point getMiddle(Polygon polygon) {
  197.     int totalX = 0;
  198.     int totalY = 0;
  199.  
  200.     for (Integer x : polygon.xpoints) {
  201.         totalX += x;
  202.     }
  203.     for (Integer y : polygon.ypoints) {
  204.         totalY += y;
  205.     }
  206.  
  207.     return new Point(totalX / polygon.xpoints.length, totalY
  208.         / polygon.ypoints.length);
  209.     }
  210.  
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement