Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.zenscripting.api.framework.script.utils;
- import com.zenscripting.api.framework.script.LoopScript;
- import com.zenscripting.api.framework.webwalker.web.WebNode;
- import org.osbot.rs07.api.map.Position;
- import org.osbot.rs07.api.model.Entity;
- import org.osbot.rs07.input.mouse.MiniMapTileDestination;
- import org.osbot.rs07.input.mouse.RectangleDestination;
- import java.util.ArrayList;
- public class Walker {
- private LoopScript s;
- public Walker(LoopScript scriptInstance) {
- this.s = scriptInstance;
- }
- public boolean walkPathMM(ArrayList<WebNode> path) {
- return walkPathMM(path, 6);
- }
- private boolean walkPathMM(ArrayList<WebNode> path, int distance) {
- WebNode next = nextPosition(path, 14);
- WebNode last = path.get(path.size() - 1);
- Position mapDestination = s.getMap().getDestination();
- if (next != null) {
- s.log("Found next position [" + next.toString() + "]");
- if (noObstacleBlocking(next, path)) {
- s.log("No obstacle between [" + next.toString() + "], walking");
- next = nextPositionOnMinimap(path, 14);
- if (mapDestination == null || mapDestination.distance(s.myPosition()) < distance) {
- s.setPaintedPosition(next.construct());
- s.log("Walking to [" + last.toString() + "] via [" + next.toString() + "]");
- return next.distance(s.myPosition()) > distance && clickMiniMapPosition(next.construct());
- }
- }
- }
- WebNode lastNode = path.get(path.size() - 1);
- return s.map.distance(lastNode.construct()) <= distance;
- }
- private WebNode nextPosition(ArrayList<WebNode> path, int skipDist) {
- int dist = -1, closest = -1;
- for (int i = path.size() - 1; i >= 0; i--) {
- WebNode tile = path.get(i);
- int d = s.map.distance(tile.construct());
- if (d < dist || dist == -1) {
- dist = d;
- closest = i;
- }
- }
- int feasiblePositionIndex = -1;
- for (int i = closest; i < path.size(); i++) {
- if (s.map.distance(path.get(i).construct()) <= skipDist) {
- feasiblePositionIndex = i;
- } else {
- break;
- }
- }
- return (feasiblePositionIndex == -1) ? null
- : path.get(feasiblePositionIndex);
- }
- private WebNode nextPositionOnMinimap(ArrayList<WebNode> path, int skipDist) {
- int dist = -1, closest = -1;
- for (int i = path.size() - 1; i >= 0; i--) {
- WebNode tile = path.get(i);
- if (tile.getZ() == s.myPosition().getZ()) {
- int d = s.map.distance(tile.construct());
- if (d < dist || dist == -1) {
- dist = d;
- closest = i;
- }
- }
- }
- int feasiblePositionIndex = -1;
- for (int i = closest; i < path.size(); i++) {
- if (s.map.distance(path.get(i).construct()) <= skipDist) {
- feasiblePositionIndex = i;
- } else {
- break;
- }
- }
- return (feasiblePositionIndex == -1) ? null
- : path.get(feasiblePositionIndex);
- }
- private boolean noObstacleBlocking(WebNode next, ArrayList<WebNode> path) {
- s.log("Searching for obstacles between [" + next.toString() + "]");
- WebNode obstacleNode = getNextObjectIn(path, next);
- if (obstacleNode != null) {
- Entity obstacleEntity = null;
- String option = null;
- s.log("Found obstacle [" + obstacleNode.toString() + "] between [" + next.toString() + "], searching for entity");
- // Find obstacle entity
- for (Entity e : s.getAllEntities()) {
- if (e.getName().equalsIgnoreCase(obstacleNode.getName()))
- if (obstacleEntity == null || obstacleNode.distance(e.getPosition()) < obstacleNode.distance(obstacleEntity.getPosition()))
- obstacleEntity = e;
- }
- // Process obstacle interaction
- if (obstacleEntity != null) {
- s.log("Found entity for [" + obstacleNode.toString() + "], searching for action");
- // Get right-click option
- for (String action : obstacleEntity.getActions()) {
- if (action != null && !action.equals("null")) {
- option = action;
- s.log("Found action for [" + obstacleNode.toString() + "], parsing action");
- break;
- }
- }
- if (option != null) {
- // Process stairs
- if (obstacleNode.getName().equals("Staircase") || obstacleNode.getName().equals("Stairs") || obstacleNode.getName().equals("Ladder") || obstacleNode.getName().equals("Manhole")) {
- if (next.getZ() != s.myPosition().getZ()) {
- if (next.getZ() > s.myPosition().getZ()) {
- option = "Climb-up";
- } else if (next.getZ() < s.myPosition().getZ()) {
- option = "Climb-down";
- }
- } else {
- return true;
- }
- }
- // Avoid invalid obstacles
- if (option.equals("Close") || (option.equals("Open") && !obstacleNode.getName().toLowerCase().contains("door") && !obstacleNode.getName().toLowerCase().contains("gate"))) {
- s.log("Invalid obstacle [" + obstacleNode.toString() + "]");
- return true;
- }
- // Paint obstacle
- s.setPaintedEntity(obstacleEntity);
- if (obstacleEntity.getPosition().isOnMiniMap(s.getBot())) {
- s.log("Interacting with obstacle obstructing [" + next.toString() + "]: [" + obstacleNode.toString() + "] using action \"" + option + "\"");
- if (!obstacleEntity.isVisible())
- s.getCamera().toEntity(obstacleEntity);
- // Interact with obstacle
- if (obstacleEntity.interact(option)) {
- s.log("Interaction with obstacle [" + obstacleNode.toString() + "] successful!");
- } else {
- s.log("Interaction with obstacle [" + obstacleNode.toString() + "] failed!");
- }
- return false;
- }
- }
- }
- }
- return true;
- }
- private WebNode getNextObjectIn(ArrayList<WebNode> path, WebNode next) {
- WebNode obstacle = null;
- for (WebNode node : path) {
- if (s.getWebWalker().getWeb().getObstacles().contains(node) && path.indexOf(node) <= path.indexOf(next)) {
- if (obstacle == null) {
- obstacle = node;
- }
- }
- }
- return obstacle;
- }
- public WebNode getNextObjectIn(ArrayList<WebNode> path) {
- for (WebNode node : path) {
- if (s.getWebWalker().getWeb().getObstacles().contains(node))
- return node;
- }
- return null;
- }
- public boolean clickMiniMapPosition(Position position) {
- if (position == null) return false;
- return s.getMap().realDistance(position) == 0 || s.mouse.click(new RectangleDestination(s.bot, new MiniMapTileDestination(s.bot, position).getBoundingBox()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement