Advertisement
Guest User

Untitled

a guest
May 27th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.76 KB | None | 0 0
  1. package com.zenscripting.api.framework.script.utils;
  2.  
  3.  
  4. import com.zenscripting.api.framework.script.LoopScript;
  5. import com.zenscripting.api.framework.webwalker.web.WebNode;
  6. import org.osbot.rs07.api.map.Position;
  7. import org.osbot.rs07.api.model.Entity;
  8. import org.osbot.rs07.input.mouse.MiniMapTileDestination;
  9. import org.osbot.rs07.input.mouse.RectangleDestination;
  10.  
  11. import java.util.ArrayList;
  12.  
  13. public class Walker {
  14.  
  15.     private LoopScript s;
  16.  
  17.     public Walker(LoopScript scriptInstance) {
  18.         this.s = scriptInstance;
  19.     }
  20.  
  21.     public boolean walkPathMM(ArrayList<WebNode> path) {
  22.         return walkPathMM(path, 6);
  23.     }
  24.  
  25.     private boolean walkPathMM(ArrayList<WebNode> path, int distance) {
  26.         WebNode next = nextPosition(path, 14);
  27.         WebNode last = path.get(path.size() - 1);
  28.         Position mapDestination = s.getMap().getDestination();
  29.         if (next != null) {
  30.             s.log("Found next position [" + next.toString() + "]");
  31.             if (noObstacleBlocking(next, path)) {
  32.                 s.log("No obstacle between [" + next.toString() + "], walking");
  33.                 next = nextPositionOnMinimap(path, 14);
  34.                 if (mapDestination == null || mapDestination.distance(s.myPosition()) < distance) {
  35.                     s.setPaintedPosition(next.construct());
  36.                     s.log("Walking to [" + last.toString() + "] via [" + next.toString() + "]");
  37.                     return next.distance(s.myPosition()) > distance && clickMiniMapPosition(next.construct());
  38.                 }
  39.  
  40.             }
  41.         }
  42.         WebNode lastNode = path.get(path.size() - 1);
  43.         return s.map.distance(lastNode.construct()) <= distance;
  44.     }
  45.  
  46.     private WebNode nextPosition(ArrayList<WebNode> path, int skipDist) {
  47.         int dist = -1, closest = -1;
  48.         for (int i = path.size() - 1; i >= 0; i--) {
  49.             WebNode tile = path.get(i);
  50.             int d = s.map.distance(tile.construct());
  51.             if (d < dist || dist == -1) {
  52.                 dist = d;
  53.                 closest = i;
  54.             }
  55.         }
  56.  
  57.         int feasiblePositionIndex = -1;
  58.  
  59.         for (int i = closest; i < path.size(); i++) {
  60.             if (s.map.distance(path.get(i).construct()) <= skipDist) {
  61.                 feasiblePositionIndex = i;
  62.             } else {
  63.                 break;
  64.             }
  65.         }
  66.  
  67.         return (feasiblePositionIndex == -1) ? null
  68.                 : path.get(feasiblePositionIndex);
  69.     }
  70.  
  71.     private WebNode nextPositionOnMinimap(ArrayList<WebNode> path, int skipDist) {
  72.         int dist = -1, closest = -1;
  73.         for (int i = path.size() - 1; i >= 0; i--) {
  74.             WebNode tile = path.get(i);
  75.             if (tile.getZ() == s.myPosition().getZ()) {
  76.                 int d = s.map.distance(tile.construct());
  77.                 if (d < dist || dist == -1) {
  78.                     dist = d;
  79.                     closest = i;
  80.                 }
  81.             }
  82.         }
  83.  
  84.         int feasiblePositionIndex = -1;
  85.  
  86.         for (int i = closest; i < path.size(); i++) {
  87.             if (s.map.distance(path.get(i).construct()) <= skipDist) {
  88.                 feasiblePositionIndex = i;
  89.             } else {
  90.                 break;
  91.             }
  92.         }
  93.  
  94.         return (feasiblePositionIndex == -1) ? null
  95.                 : path.get(feasiblePositionIndex);
  96.     }
  97.  
  98.     private boolean noObstacleBlocking(WebNode next, ArrayList<WebNode> path) {
  99.         s.log("Searching for obstacles between [" + next.toString() + "]");
  100.         WebNode obstacleNode = getNextObjectIn(path, next);
  101.         if (obstacleNode != null) {
  102.             Entity obstacleEntity = null;
  103.             String option = null;
  104.             s.log("Found obstacle [" + obstacleNode.toString() + "] between [" + next.toString() + "], searching for entity");
  105.             // Find obstacle entity
  106.             for (Entity e : s.getAllEntities()) {
  107.                 if (e.getName().equalsIgnoreCase(obstacleNode.getName()))
  108.                     if (obstacleEntity == null || obstacleNode.distance(e.getPosition()) < obstacleNode.distance(obstacleEntity.getPosition()))
  109.                         obstacleEntity = e;
  110.             }
  111.             // Process obstacle interaction
  112.             if (obstacleEntity != null) {
  113.                 s.log("Found entity for [" + obstacleNode.toString() + "], searching for action");
  114.                 // Get right-click option
  115.                 for (String action : obstacleEntity.getActions()) {
  116.                     if (action != null && !action.equals("null")) {
  117.                         option = action;
  118.                         s.log("Found action for [" + obstacleNode.toString() + "], parsing action");
  119.                         break;
  120.                     }
  121.                 }
  122.                 if (option != null) {
  123.                     // Process stairs
  124.                     if (obstacleNode.getName().equals("Staircase") || obstacleNode.getName().equals("Stairs") || obstacleNode.getName().equals("Ladder") || obstacleNode.getName().equals("Manhole")) {
  125.                         if (next.getZ() != s.myPosition().getZ()) {
  126.                             if (next.getZ() > s.myPosition().getZ()) {
  127.                                 option = "Climb-up";
  128.                             } else if (next.getZ() < s.myPosition().getZ()) {
  129.                                 option = "Climb-down";
  130.                             }
  131.                         } else {
  132.                             return true;
  133.                         }
  134.                     }
  135.                     // Avoid invalid obstacles
  136.                     if (option.equals("Close") || (option.equals("Open") && !obstacleNode.getName().toLowerCase().contains("door") && !obstacleNode.getName().toLowerCase().contains("gate"))) {
  137.                         s.log("Invalid obstacle [" + obstacleNode.toString() + "]");
  138.                         return true;
  139.                     }
  140.                     // Paint obstacle
  141.                     s.setPaintedEntity(obstacleEntity);
  142.                     if (obstacleEntity.getPosition().isOnMiniMap(s.getBot())) {
  143.                         s.log("Interacting with obstacle obstructing [" + next.toString() + "]: [" + obstacleNode.toString() + "] using action \"" + option + "\"");
  144.                         if (!obstacleEntity.isVisible())
  145.                             s.getCamera().toEntity(obstacleEntity);
  146.                         // Interact with obstacle
  147.                         if (obstacleEntity.interact(option)) {
  148.                             s.log("Interaction with obstacle [" + obstacleNode.toString() + "] successful!");
  149.                         } else {
  150.                             s.log("Interaction with obstacle [" + obstacleNode.toString() + "] failed!");
  151.                         }
  152.                         return false;
  153.                     }
  154.                 }
  155.             }
  156.         }
  157.         return true;
  158.     }
  159.  
  160.     private WebNode getNextObjectIn(ArrayList<WebNode> path, WebNode next) {
  161.         WebNode obstacle = null;
  162.         for (WebNode node : path) {
  163.             if (s.getWebWalker().getWeb().getObstacles().contains(node) && path.indexOf(node) <= path.indexOf(next)) {
  164.                 if (obstacle == null) {
  165.                     obstacle = node;
  166.                 }
  167.             }
  168.         }
  169.         return obstacle;
  170.     }
  171.  
  172.     public WebNode getNextObjectIn(ArrayList<WebNode> path) {
  173.         for (WebNode node : path) {
  174.             if (s.getWebWalker().getWeb().getObstacles().contains(node))
  175.                 return node;
  176.         }
  177.         return null;
  178.     }
  179.  
  180.     public boolean clickMiniMapPosition(Position position) {
  181.         if (position == null) return false;
  182.         return s.getMap().realDistance(position) == 0 || s.mouse.click(new RectangleDestination(s.bot, new MiniMapTileDestination(s.bot, position).getBoundingBox()));
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement