Thread: Path finder issue!

Page 1 of 2 12 LastLast
Results 1 to 10 of 15
  1. #1 Path finder issue! 
    Registered Member
    Join Date
    Dec 2013
    Posts
    419
    Thanks given
    127
    Thanks received
    85
    Rep Power
    349
    Hi, my path finder does not work when the players localX or localY is over 400 (e.g. localY > 400 is above Barb village that is north of map). It will take too long causing the server to crash. Everything from south of map works fine.

    Note: Issue is with AStarPathFinder #find

    Here's the code:
    Code:
    import java.util.HashSet;
    import java.util.Set;
    
    import net.scapeemulator.game.model.Position;
    import net.scapeemulator.game.model.World;
    
    /**
     * @author Graham Edgecombe
     */
    public final class AStarPathFinder extends PathFinder {
    
        /**
         * The cost of moving in a straight line.
         */
        private static final int COST_STRAIGHT = 10;
    
        /**
         * The radius to search if we can't find a path to our tile.
         */
        private static final int UNREACHABLE_SEARCH_DISTANCE = 10;
    
        /**
         * Represents a node used by the A* algorithm.
         *
         * @author Graham Edgecombe
         */
        private static class Node implements Comparable<Node> {
    
            /**
             * The parent node.
             */
            private Node parent;
    
            /**
             * The cost.
             */
            private int cost;
    
            /**
             * The heuristic.
             */
            private int heuristic;
    
            /**
             * The depth.
             */
            private int depth;
    
            /**
             * The x coordinate.
             */
            private final int x;
    
            /**
             * The y coordinate.
             */
            private final int y;
    
            /**
             * Creates a node.
             *
             * @param x
             *         The x coordinate.
             * @param y
             *         The y coordinate.
             */
            public Node(int x, int y) {
                this.x = x;
                this.y = y;
            }
    
            /**
             * Sets the parent.
             *
             * @param parent
             *         The parent.
             */
            public void setParent(Node parent) {
                this.parent = parent;
            }
    
            /**
             * Gets the parent node.
             *
             * @return The parent node.
             */
            public Node getParent() {
                return parent;
            }
    
            public void setCost(int cost) {
                this.cost = cost;
            }
    
            public int getCost() {
                return cost;
            }
    
            /**
             * Gets the X coordinate.
             *
             * @return The X coordinate.
             */
            public int getX() {
                return x;
            }
    
            /**
             * Gets the Y coordinate.
             *
             * @return The Y coordinate.
             */
            public int getY() {
                return y;
            }
            
            @Override
            public int hashCode() {
                final int prime = 31;
                int result = 1;
                result = prime * result + cost;
                result = prime * result + depth;
                result = prime * result + heuristic;
                result = prime * result + ((parent == null) ? 0 : parent.hashCode());
                result = prime * result + x;
                result = prime * result + y;
                return result;
            }
    
            @Override
            public boolean equals(Object obj) {
                if(this == obj)
                    return true;
                if(obj == null)
                    return false;
                if(getClass() != obj.getClass())
                    return false;
                Node other = (Node) obj;
                if(cost != other.cost)
                    return false;
                if(depth != other.depth)
                    return false;
                if(heuristic != other.heuristic)
                    return false;
                if(parent == null) {
                    if(other.parent != null)
                        return false;
                } else if(!parent.equals(other.parent))
                    return false;
                if(x != other.x)
                    return false;
                if(y != other.y)
                    return false;
                return true;
            }
    
            @Override
            public int compareTo(@NotNull Node other) {
                return cost - other.cost;
            }
    
        }
    
        private Node current;
        private Node[][] nodes;
        private Set<Node> closed = new HashSet<>();
        private Set<Node> open = new HashSet<>();
    
        @Override
        public Path find(Position position, int radius, int srcX, int srcY, int dstX, int dstY, int objWidth, int objLength, int size) {
        	/**
        	 * TODO: Fix - if the srcY is bigger then 400, this whole code will take too long and not work
        	 */
        	
        	/**
        	 * Ranges from 180-220 if in lumbridge
        	 */
        	int width = radius + (srcX + srcY);
        	int length = width;
        	
        	System.out.println(width + ":" + length);
        	
            /* Move our coordinates to the center so we don't run into bounds issues */
            srcX += radius; srcY += radius;
            dstX += radius; dstY += radius;
    
            if(dstX < 0 || dstY < 0 || dstX >= width || dstY >= length) {
                return null; // out of range
            }
            
            TraversalMap map = World.getWorld().getTraversalMap();
    
            open.clear();
            closed.clear();
            
            nodes = new Node[width][length];
            for(int x = 0; x < width; x++) {
                for(int y = 0; y < length; y++) {
                    nodes[x][y] = new Node(x, y);
                }
            }
            
            open.add(nodes[srcX][srcY]);
            
            while(open.size() > 0) {
                current = getLowestCost();
                if(current == nodes[dstX][dstY]) {
                    break;
                }
                open.remove(current);
                closed.add(current);
    
                int x = current.getX(), y = current.getY();
    
                // south
                if(y > 0 && map.isTraversableSouth(position.getHeight(), position.getX() + x - radius, position.getY() + y - radius, size)) {
                    Node n = nodes[x][y - 1];
                    examineNode(n);
                }
                // west
                if(x > 0 && map.isTraversableWest(position.getHeight(), position.getX() + x - radius, position.getY() + y - radius, size)) {
                    Node n = nodes[x - 1][y];
                    examineNode(n);
                }
                // north
                if(y < length - 1 && map.isTraversableNorth(position.getHeight(), position.getX() + x - radius, position.getY() + y - radius, size)) {
                    Node n = nodes[x][y + 1];
                    examineNode(n);
                }
                // east
                if(x < width - 1 && map.isTraversableEast(position.getHeight(), position.getX() + x - radius, position.getY() + y - radius, size)) {
                    Node n = nodes[x + 1][y];
                    examineNode(n);
                }
                // south west
                if(x > 0 && y > 0 && map.isTraversableSouthWest(position.getHeight(), position.getX() + x - radius, position.getY() + y - radius, size)) {
                    Node n = nodes[x - 1][y - 1];
                    examineNode(n);
                }
                // north west
                if(x > 0 && y < length - 1 && map.isTraversableNorthWest(position.getHeight(), position.getX() + x - radius, position.getY() + y - radius, size)) {
                    Node n = nodes[x - 1][y + 1];
                    examineNode(n);
                }
    
                // south east
                if(x < width - 1 && y > 0 && map.isTraversableSouthEast(position.getHeight(), position.getX() + x - radius, position.getY() + y - radius, size)) {
                    Node n = nodes[x + 1][y - 1];
                    examineNode(n);
                }
                // north east
                if(x < width - 1 && y < length - 1 && map.isTraversableNorthEast(position.getHeight(), position.getX() + x - radius, position.getY() + y - radius, size)) {
                    Node n = nodes[x + 1][y + 1];
                    examineNode(n);
                }
            }
            
            if(nodes[dstX][dstY].getParent() == null) {
                int newX = dstX;
                int newY = dstY;
    
                int minDistance = 999;
                int minCost = 999_999;
                for(int x = dstX - UNREACHABLE_SEARCH_DISTANCE; x <= dstX + UNREACHABLE_SEARCH_DISTANCE; x++) {
                    for(int y = dstY - UNREACHABLE_SEARCH_DISTANCE; y <= dstY + UNREACHABLE_SEARCH_DISTANCE; y++) {
                    	if((x >= 0 && x < width) && (y >= 0 && y < length) && nodes[x][y].parent != null) {
                            int deltaX = 0;
                            int deltaY = 0;
                            if(y < dstY) {
                                deltaY = dstY - y;
                            } else if(y > (dstY + objLength - 1)) {
                                deltaY = y + 1 - (dstY + objLength);
                            }
                            if(x >= dstX) {
                                if(x > (dstX + objWidth - 1)) {
                                    deltaX = 1 + (x - dstX - objWidth);
                                }
                            } else {
                                deltaX = dstX - x;
                            }
                            int dist = (int)Math.sqrt(deltaX * deltaX + deltaY + deltaY);
                            int cost = nodes[x][y].cost;
                            if(dist < minDistance || (dist == minDistance && cost < minCost)) {
                                minDistance = dist;
                                minCost = cost;
                                newX = x;
                                newY = y;
                            }
                        }
                    }
                }
    
                if(nodes[newX][newY].getParent() == null) {
                    System.out.println("Still no path");
                    return null;
                }
    
                dstX = newX; dstY = newY;
            }
    
            Path p = new Path();
            Node n = nodes[dstX][dstY];
            while(n != nodes[srcX][srcY]) {
                p.addFirst(new Position(n.getX() + position.getX() - radius, n.getY() + position.getY() - radius));
                n = n.getParent();
            }
            return p;
        }
    
        private Node getLowestCost() {
            Node curLowest = null;
            for(Node n : open) {
                if(curLowest == null) {
                    curLowest = n;
                } else {
                    if(n.getCost() < curLowest.getCost()) {
                        curLowest = n;
                    }
                }
            }
            return curLowest;
        }
    
        private void examineNode(Node n) {
            int heuristic = estimateDistance(current, n);
            int nextStepCost = current.getCost() + heuristic;
            if(nextStepCost < n.getCost()) {
                open.remove(n);
                closed.remove(n);
            }
            if(!open.contains(n) && !closed.contains(n)) {
                n.setParent(current);
                n.setCost(nextStepCost);
                open.add(n);
            }
        }
    
        /**
         * Estimates a distance between the two points.
         *
         * @param src
         *         The source node.
         * @param dst
         *         The distance node.
         *
         * @return The distance.
         */
        public int estimateDistance(Node src, Node dst) {
            int deltaX = src.getX() - dst.getX();
            int deltaY = src.getY() - dst.getY();
            return (Math.abs(deltaX) + Math.abs(deltaY)) * COST_STRAIGHT;
        }
    
    }
    Code:
    import net.scapeemulator.game.model.Mob;
    import net.scapeemulator.game.model.Position;
    import net.scapeemulator.game.model.object.GameObject;
    
    /**
     * Created by Hadyn Richard
     */
    public abstract class PathFinder {
    
        public Path find(Mob mob, GameObject object) {
            /* Get the current position of the player */
            Position position = mob.getPosition();
    
            /* Get the position of the object */
            Position obj = object.getPosition();
    
            /* Get the object's width and length */
            int objWidth = object.getDefinition().getWidth(), objLength = object.getDefinition().getLength();
            if(object.getRotation() == 1 || object.getRotation() == 3) {
                objWidth = object.getDefinition().getLength();
                objLength = object.getDefinition().getWidth();
            }
    
            if(object.getType() != 10 && object.getType() != 11 && object.getType() != 22) {
                objWidth = objLength = 0;
            }
    
            /* Get the scene base x and y coordinates */
            int baseLocalX = position.getBaseLocalX(), baseLocalY = position.getBaseLocalY();
    
            /* Calculate the local x and y coordinates */
            int destLocalX = obj.getX() - baseLocalX;
            int destLocalY = obj.getY() - baseLocalY;
    
            Path path = find(new Position(baseLocalX, baseLocalY, position.getHeight()), 64, position.getLocalX(), position.getLocalY(), destLocalX, destLocalY, objWidth, objLength, mob.getSize());
            return path;
        }
    
        public Path find(Mob mob, int destX, int destY) {
    
            /* Get the current position of the player */
            Position position = mob.getPosition();
    
            /* Get the scene base x and y coordinates */
            int baseLocalX = position.getBaseLocalX(), baseLocalY = position.getBaseLocalY();
    
            /* Calculate the local x and y coordinates */
            int destLocalX = destX - baseLocalX, destLocalY = destY - baseLocalY;
    
            return find(new Position(baseLocalX, baseLocalY, position.getHeight()), 64, position.getLocalX(), position.getLocalY(), destLocalX, destLocalY, 0, 0, mob.getSize());
        }
    
        public Path find(Position position, int radius, int srcX, int srcY, int destX, int destY) {
            return find(position, radius, srcX, srcY, destX, destY, 0, 0, 1);
        }
    
        public abstract Path find(Position position, int radius, int srcX, int srcY, int destX, int destY, int objWidth, int objHeight, int size);
    
    }

    Please note: This class has got a lot of unused crap.

    Code:
    import net.scapeemulator.game.model.Position;
    import net.scapeemulator.game.model.object.ObjectType;
    import net.scapeemulator.game.model.region.Region;
    import net.scapeemulator.game.model.region.RegionManager;
    
    import static net.scapeemulator.game.model.object.ObjectOrientation.*;
    import static net.scapeemulator.game.pf.Tile.*;
    
    /**
     * Created by Hadyn Richard
     */
    public final class TraversalMap {
    
        private final RegionManager regionManager;
    
        public TraversalMap(RegionManager manager) {
            this.regionManager = manager;
        }
    
        public void markWall(Position position, int rotation, ObjectType type, boolean impenetrable) {
            int plane = position.getHeight(), x = position.getX(), y = position.getY();
            switch(type) {
                case TYPE_0:
                    if (rotation == WEST) {
                        set(plane, x, y, WALL_WEST);
                        set(plane, x - 1, y, WALL_EAST);
                    }
                    if (rotation == NORTH) {
                        set(plane, x, y, WALL_NORTH);
                        set(plane, x, y + 1, WALL_SOUTH);
                    }
                    if (rotation == EAST) {
                        set(plane, x, y, WALL_EAST);
                        set(plane, x + 1, y, WALL_WEST);
                    }
                    if (rotation == SOUTH) {
                        set(plane, x, y, WALL_SOUTH);
                        set(plane, x, y - 1, WALL_NORTH);
                    }
                    break;
    
                case TYPE_2:
                    if (rotation == WEST) {
                        set(plane, x, y, WALL_WEST | WALL_NORTH);
                        set(plane, x - 1, y, WALL_EAST);
                        set(plane, x, y + 1, WALL_SOUTH);
                    }
                    if (rotation == NORTH) {
                        set(plane, x, y, WALL_EAST | WALL_NORTH);
                        set(plane, x, y + 1, WALL_SOUTH);
                        set(plane, x + 1, y, WALL_WEST);
                    }
                    if (rotation == EAST) {
                        set(plane, x, y, WALL_EAST | WALL_SOUTH);
                        set(plane, x + 1, y, WALL_WEST);
                        set(plane, x, y - 1, WALL_NORTH);
                    }
                    if (rotation == SOUTH) {
                        set(plane, x, y, WALL_WEST | WALL_SOUTH);
                        set(plane, x, y - 1, WALL_NORTH);
                        set(plane, x - 1, y, WALL_EAST);
                    }
                    break;
    
                case TYPE_1:
                case TYPE_3:
                    if (rotation == WEST) {
                        set(plane, x, y, WALL_NORTH_WEST);
                        set(plane, x - 1, y + 1, WALL_SOUTH_EAST);
                    }
                    if (rotation == NORTH) {
                        set(plane, x, y, WALL_NORTH_EAST);
                        set(plane, x + 1, y + 1, WALL_SOUTH_WEST);
                    }
                    if (rotation == EAST) {
                        set(plane, x, y, WALL_SOUTH_EAST);
                        set(plane, x + 1, y - 1, WALL_NORTH_WEST);
                    }
                    if (rotation == SOUTH) {
                        set(plane, x, y, WALL_SOUTH_WEST);
                        set(plane, x - 1, y - 1, WALL_NORTH_EAST);
                    }
                    break;
            }
        }
    
        public void unmarkWall(int rotation, int plane, int x, int y, ObjectType type, boolean impenetrable) {
            switch(type) {
                case TYPE_0:
                    if(rotation == WEST) {
                        unset(plane, x, y, impenetrable ? IMPENETRABLE_WALL_WEST : WALL_WEST);
                        unset(plane, x - 1, y, impenetrable ? IMPENETRABLE_WALL_EAST : WALL_EAST);
                    }
                    if(rotation == NORTH) {
                        unset(plane, x, y, impenetrable ? IMPENETRABLE_WALL_NORTH : WALL_NORTH);
                        unset(plane, x, y + 1, impenetrable ? IMPENETRABLE_WALL_SOUTH : WALL_SOUTH);
                    }
                    if(rotation == EAST) {
                        unset(plane, x, y, impenetrable ? IMPENETRABLE_WALL_EAST : WALL_EAST);
                        unset(plane, x + 1, y, impenetrable ? IMPENETRABLE_WALL_WEST : WALL_WEST);
                    }
                    if(rotation == SOUTH) {
                        unset(plane, x, y, impenetrable ? IMPENETRABLE_WALL_SOUTH : WALL_SOUTH);
                        unset(plane, x, y - 1, impenetrable ? IMPENETRABLE_WALL_NORTH : WALL_NORTH);
                    }
                    break;
    
                case TYPE_2:
                    if(rotation == WEST) {
                        if(impenetrable)
                            unset(plane, x, y, IMPENETRABLE_WALL_WEST | IMPENETRABLE_WALL_NORTH);
                        else
                            unset(plane, x, y, WALL_WEST | WALL_NORTH);
                        unset(plane, x - 1, y, impenetrable ? IMPENETRABLE_WALL_EAST : WALL_EAST);
                        unset(plane, x, y + 1, impenetrable ? IMPENETRABLE_WALL_SOUTH : WALL_SOUTH);
                    }
                    if(rotation == NORTH) {
                        if(impenetrable)
                            unset(plane, x, y, IMPENETRABLE_WALL_EAST | IMPENETRABLE_WALL_NORTH);
                        else
                            unset(plane, x, y, WALL_EAST | WALL_NORTH);
                        unset(plane, x, y + 1, impenetrable ? IMPENETRABLE_WALL_SOUTH : WALL_SOUTH);
                        unset(plane, x + 1, y, impenetrable ? IMPENETRABLE_WALL_WEST : WALL_WEST);
                    }
                    if(rotation == EAST) {
                        if(impenetrable)
                            unset(plane, x, y, IMPENETRABLE_WALL_EAST | IMPENETRABLE_WALL_SOUTH);
                        else
                            unset(plane, x, y, WALL_EAST | WALL_SOUTH);
                        unset(plane, x + 1, y, impenetrable ? IMPENETRABLE_WALL_WEST : WALL_WEST);
                        unset(plane, x, y - 1, impenetrable ? IMPENETRABLE_WALL_NORTH : WALL_NORTH);
                    }
                    if(rotation == SOUTH) {
                        if(impenetrable)
                            unset(plane, x, y, IMPENETRABLE_WALL_WEST | IMPENETRABLE_WALL_SOUTH);
                        else
                            unset(plane, x, y, WALL_WEST | WALL_SOUTH);
                        unset(plane, x, y - 1, impenetrable ? IMPENETRABLE_WALL_WEST : WALL_WEST);
                        unset(plane, x - 1, y, impenetrable ? IMPENETRABLE_WALL_NORTH : WALL_NORTH);
                    }
                    break;
    
                case TYPE_1:
                case TYPE_3:
                    if(rotation == WEST) {
                        unset(plane, x, y, impenetrable ? IMPENETRABLE_WALL_NORTH_WEST : WALL_NORTH_WEST);
                        unset(plane, x - 1, y + 1, impenetrable ? IMPENETRABLE_WALL_SOUTH_EAST : WALL_SOUTH_EAST);
                    }
                    if(rotation == NORTH) {
                        unset(plane, x, y, impenetrable ? IMPENETRABLE_WALL_NORTH_EAST : WALL_NORTH_EAST);
                        unset(plane, x + 1, y + 1, impenetrable ? IMPENETRABLE_WALL_SOUTH_WEST : WALL_SOUTH_WEST);
                    }
                    if(rotation == EAST) {
                        unset(plane, x, y, impenetrable ? IMPENETRABLE_WALL_SOUTH_EAST : WALL_SOUTH_EAST);
                        unset(plane, x + 1, y - 1, impenetrable ? IMPENETRABLE_WALL_NORTH_WEST : WALL_NORTH_WEST);
                    }
                    if(rotation == SOUTH) {
                        unset(plane, x, y, impenetrable ? IMPENETRABLE_WALL_SOUTH_WEST : WALL_SOUTH_WEST);
                        unset(plane, x - 1, y - 1, impenetrable ? IMPENETRABLE_WALL_SOUTH_WEST : WALL_SOUTH_WEST);
                    }
                    break;
            }
        }
    
        public void markBlocked(Position position) {
            int plane = position.getHeight();
            int x = position.getX();
            int y = position.getY();
    
            /* Calculate the local coordinates */
            int localX = x & 0x3f, localY = y & 0x3f;
    
            Region region = regionManager.getRegion(position);
            if(region == null) {
                return;
            }
    
            int modifiedPlane = plane;
            if((region.getTile(1, localX, localY).flags() & BRIDGE) != 0) {
                modifiedPlane = plane - 1;
            }
    
            region.getTile(modifiedPlane, x & 0x3f, y & 0x3f).set(BLOCKED);
        }
    
        public void markOccupant(Position position, int width, int length) {
            int plane = position.getHeight(), x = position.getX(), y = position.getY();
            for(int offsetX = 0; offsetX < width; offsetX++) {
                for(int offsetY = 0; offsetY < length; offsetY++) {
                    set(plane, x + offsetX, y + offsetY, OCCUPANT);
                }
            }
        }
    
        public void markBridge(Position position) {
            set(position.getHeight(), position.getX(), position.getY(), BRIDGE);
        }
    
        public boolean isTraversableNorth(int plane, int x, int y, int size) {
            for(int offsetX = 0; offsetX < size; offsetX++) {
                for(int offsetY = 0; offsetY < size; offsetY++) {
                    if(!isTraversableNorth(plane, x + offsetX, y + offsetY)) {
                        return false;
                    }
                }
            }
            return true;
        }
    
        public boolean isTraversableNorth(int plane, int x, int y) {
            return isTraversableNorth(plane, x, y, false);
        }
    
        /**
         * Tests if from the specified position it can be traversed north.
         */
        public boolean isTraversableNorth(int plane, int x, int y, boolean impenetrable) {
            if(impenetrable) {
                return isInactive(plane, x, y + 1, IMPENETRABLE_OCCUPANT | IMPENETRABLE_WALL_SOUTH);
            }
            return isInactive(plane, x, y + 1, WALL_SOUTH | OCCUPANT | BLOCKED);
        }
    
        public boolean isTraversableSouth(int plane, int x, int y, int size) {
            for(int offsetX = 0; offsetX < size; offsetX++) {
                for(int offsetY = 0; offsetY < size; offsetY++) {
                    if(!isTraversableSouth(plane, x + offsetX, y + offsetY)) {
                        return false;
                    }
                }
            }
            return true;
        }
    
        public boolean isTraversableSouth(int plane, int x, int y) {
            return isTraversableSouth(plane, x, y, false);
        }
    
        /**
         * Tests if from the specified position it can be traversed south.
         */
        public boolean isTraversableSouth(int plane, int x, int y, boolean impenetrable) {
            if(impenetrable) {
                return isInactive(plane, x, y - 1, IMPENETRABLE_OCCUPANT | IMPENETRABLE_WALL_NORTH);
            }
            return isInactive(plane, x, y - 1, WALL_NORTH | OCCUPANT | BLOCKED);
        }
    
        public boolean isTraversableEast(int plane, int x, int y, int size) {
            for(int offsetX = 0; offsetX < size; offsetX++) {
                for(int offsetY = 0; offsetY < size; offsetY++) {
                    if(!isTraversableEast(plane, x + offsetX, y + offsetY)) {
                        return false;
                    }
                }
            }
            return true;
        }
    
        public boolean isTraversableEast(int plane, int x, int y) {
            return isTraversableEast(plane, x, y, false);
        }
    
        /**
         * Tests if from the specified position it can be traversed south.
         */
        public boolean isTraversableEast(int plane, int x, int y, boolean impenetrable) {
            if(impenetrable) {
                return isInactive(plane, x + 1, y, IMPENETRABLE_OCCUPANT | IMPENETRABLE_WALL_WEST);
            }
            return isInactive(plane, x + 1, y, WALL_WEST | OCCUPANT | BLOCKED);
        }
    
        public boolean isTraversableWest(int plane, int x, int y, int size) {
            for(int offsetX = 0; offsetX < size; offsetX++) {
                for(int offsetY = 0; offsetY < size; offsetY++) {
                    if(!isTraversableWest(plane, x + offsetX, y + offsetY)) {
                        return false;
                    }
                }
            }
            return true;
        }
    
        public boolean isTraversableWest(int plane, int x, int y) {
            return isTraversableWest(plane, x, y, false);
        }
    
        /**
         * Tests if from the specified position it can be traversed south.
         */
        public boolean isTraversableWest(int plane, int x, int y, boolean impenetrable) {
            if(impenetrable) {
                return isInactive(plane, x - 1, y, IMPENETRABLE_OCCUPANT | IMPENETRABLE_WALL_EAST);
            }
            return isInactive(plane, x - 1, y, WALL_EAST | OCCUPANT | BLOCKED);
        }
    
        public boolean isTraversableNorthEast(int plane, int x, int y, int size) {
            for(int offsetX = 0; offsetX < size; offsetX++) {
                for(int offsetY = 0; offsetY < size; offsetY++) {
                    if(!isTraversableNorthEast(plane, x + offsetX, y + offsetY)) {
                        return false;
                    }
                }
            }
            return true;
        }
    
        public boolean isTraversableNorthEast(int plane, int x, int y) {
            return isTraversableNorthEast(plane, x, y, false);
        }
    
        /**
         * Tests if from the specified position it can be traversed south.
         */
        public boolean isTraversableNorthEast(int plane, int x, int y, boolean impenetrable) {
            if(impenetrable) {
                return isInactive(plane, x + 1, y + 1, IMPENETRABLE_WALL_WEST | IMPENETRABLE_WALL_SOUTH | IMPENETRABLE_WALL_SOUTH_WEST | OCCUPANT) && isInactive(plane, x + 1, y, IMPENETRABLE_WALL_WEST | IMPENETRABLE_OCCUPANT) && isInactive(plane, x, y + 1, IMPENETRABLE_WALL_SOUTH | IMPENETRABLE_OCCUPANT);
            }
            return isInactive(plane, x + 1, y + 1, WALL_WEST | WALL_SOUTH | WALL_SOUTH_WEST | OCCUPANT | BLOCKED) && isInactive(plane, x + 1, y, WALL_WEST | OCCUPANT | BLOCKED) && isInactive(plane, x, y + 1, WALL_SOUTH | OCCUPANT | BLOCKED);
        }
    
        public boolean isTraversableNorthWest(int plane, int x, int y, int size) {
            for(int offsetX = 0; offsetX < size; offsetX++) {
                for(int offsetY = 0; offsetY < size; offsetY++) {
                    if(!isTraversableNorthWest(plane, x + offsetX, y + offsetY)) {
                        return false;
                    }
                }
            }
            return true;
        }
    
        public boolean isTraversableNorthWest(int plane, int x, int y) {
            return isTraversableNorthWest(plane, x, y, false);
        }
    
        /**
         * Tests if from the specified position it can be traversed south.
         */
        public boolean isTraversableNorthWest(int plane, int x, int y, boolean impenetrable) {
            if(impenetrable) {
                return isInactive(plane, x - 1, y + 1, IMPENETRABLE_WALL_EAST | IMPENETRABLE_WALL_SOUTH | IMPENETRABLE_WALL_SOUTH_EAST | OCCUPANT) && isInactive(plane, x - 1, y, IMPENETRABLE_WALL_EAST | IMPENETRABLE_OCCUPANT) && isInactive(plane, x, y + 1, IMPENETRABLE_WALL_SOUTH | IMPENETRABLE_OCCUPANT);
            }
            return isInactive(plane, x - 1, y + 1, WALL_EAST | WALL_SOUTH | WALL_SOUTH_EAST | OCCUPANT | BLOCKED) && isInactive(plane, x - 1, y, WALL_EAST | OCCUPANT | BLOCKED) && isInactive(plane, x, y + 1, WALL_SOUTH | OCCUPANT | BLOCKED);
        }
    
        public boolean isTraversableSouthEast(int plane, int x, int y, int size) {
            for(int offsetX = 0; offsetX < size; offsetX++) {
                for(int offsetY = 0; offsetY < size; offsetY++) {
                    if(!isTraversableSouthEast(plane, x + offsetX, y + offsetY)) {
                        return false;
                    }
                }
            }
            return true;
        }
    
        public boolean isTraversableSouthEast(int plane, int x, int y) {
            return isTraversableSouthEast(plane, x, y, false);
        }
    
        /**
         * Tests if from the specified position it can be traversed south.
         */
        public boolean isTraversableSouthEast(int plane, int x, int y, boolean impenetrable) {
            if(impenetrable) {
                return isInactive(plane, x + 1, y - 1, IMPENETRABLE_WALL_WEST | IMPENETRABLE_WALL_NORTH | IMPENETRABLE_WALL_NORTH_WEST | OCCUPANT) && isInactive(plane, x + 1, y, IMPENETRABLE_WALL_WEST | IMPENETRABLE_OCCUPANT) && isInactive(plane, x, y - 1, IMPENETRABLE_WALL_NORTH | IMPENETRABLE_OCCUPANT);
            }
            return isInactive(plane, x + 1, y - 1, WALL_WEST | WALL_NORTH | WALL_NORTH_WEST | OCCUPANT | BLOCKED) && isInactive(plane, x + 1, y, WALL_WEST | OCCUPANT | BLOCKED) && isInactive(plane, x, y - 1, WALL_NORTH | OCCUPANT | BLOCKED);
        }
    
        public boolean isTraversableSouthWest(int plane, int x, int y, int size) {
            for(int offsetX = 0; offsetX < size; offsetX++) {
                for(int offsetY = 0; offsetY < size; offsetY++) {
                    if(!isTraversableSouthWest(plane, x + offsetX, y + offsetY)) {
                        return false;
                    }
                }
            }
            return true;
        }
    
        public boolean isTraversableSouthWest(int plane, int x, int y) {
            return isTraversableSouthWest(plane, x, y, false);
        }
    
        /**
         * Tests if from the specified position it can be traversed south.
         */
        public boolean isTraversableSouthWest(int plane, int x, int y, boolean impenetrable) {
            if(impenetrable) {
                return isInactive(plane, x - 1, y - 1, IMPENETRABLE_WALL_EAST | IMPENETRABLE_WALL_NORTH | IMPENETRABLE_WALL_NORTH_EAST | OCCUPANT) && isInactive(plane, x - 1, y, IMPENETRABLE_WALL_EAST | IMPENETRABLE_OCCUPANT) && isInactive(plane, x, y - 1, IMPENETRABLE_WALL_NORTH | IMPENETRABLE_OCCUPANT);
            }
            return isInactive(plane, x - 1, y - 1, WALL_EAST | WALL_NORTH | WALL_NORTH_EAST | OCCUPANT | BLOCKED) && isInactive(plane, x - 1, y, WALL_EAST | OCCUPANT | BLOCKED) && isInactive(plane, x, y - 1, WALL_NORTH | OCCUPANT | BLOCKED);
        }
    
        public void set(int plane, int x, int y, int flag) {
            Region region = regionManager.getRegion(x, y);
            if(region == null) {
                return;
            }
    
            region.getTile(plane, x & 0x3f, y & 0x3f).set(flag);
        }
    
        public boolean isInactive(int plane, int x, int y, int flag) {
            /* Calculate the local region coordinates */
            int localX = x & 0x3f, localY = y & 0x3f;
    
            Region region = regionManager.getRegion(x, y);
            if(region == null) {
                return false;
            }
    
            int modifiedPlane = plane;
            if(region.getTile(1, localX, localY).isActive(BRIDGE)) {
                modifiedPlane = plane + 1;
            }
    
            return region.getTile(modifiedPlane, localX, localY).isInactive(flag);
        }
    
        public void unset(int plane, int x, int y, int flag) {
            Region region = regionManager.getRegion(x, y);
            if(region == null) {
                return;
            }
    
            region.getTile(plane, x & 0x3f, y & 0x3f).unset(flag);
        }
    }
    Note:
    If I increase these values
    Code:
    int width = radius + (srcX + srcY);
    int length = width;
    the path finder will take too long and crash.

    Code useage:
    Code:
            Position destination = new Position(message.getDestination().getX(), message.getDestination().getY(), z);
            Position position = player.getPosition();
    
            int baseLocalX = position.getBaseLocalX();
            int baseLocalY = position.getBaseLocalY();
            int destLocalX = destination.getX() - baseLocalX;
            int destLocalY = destination.getY() - baseLocalY;
            
            Position base = new Position(baseLocalX, baseLocalY, position.getHeight());
            Path path = pathFinder.find(base, 104, position.getLocalX(), position.getLocalY(), destLocalX, destLocalY);
    Please help.
    Reply With Quote  
     

  2. #2  
    Registered Member
    Join Date
    Dec 2013
    Posts
    419
    Thanks given
    127
    Thanks received
    85
    Rep Power
    349
    Bump, I can't fix it :/
    Reply With Quote  
     

  3. #3  
    Registered Member
    Join Date
    Dec 2013
    Posts
    419
    Thanks given
    127
    Thanks received
    85
    Rep Power
    349
    b-u-m-p
    Reply With Quote  
     

  4. #4  
    Contributor

    clem585's Avatar
    Join Date
    Sep 2013
    Posts
    3,788
    Thanks given
    706
    Thanks received
    702
    Rep Power
    570
    Quote Originally Posted by Kiissmyswagb View Post
    b-u-m-p
    Try posting the full code, that would help.
    Project thread
    Reply With Quote  
     

  5. #5  
    Registered Member
    Join Date
    Dec 2013
    Posts
    419
    Thanks given
    127
    Thanks received
    85
    Rep Power
    349
    Quote Originally Posted by clem585 View Post
    Try posting the full code, that would help.
    I didn't think it would. I've updated the main thread.
    Reply With Quote  
     

  6. #6  
    Contributor

    clem585's Avatar
    Join Date
    Sep 2013
    Posts
    3,788
    Thanks given
    706
    Thanks received
    702
    Rep Power
    570
    Quote Originally Posted by Kiissmyswagb View Post
    I didn't think it would. I've updated the main thread.
    What's:

    Code:
    getLocalY
    in Position? Posting Position.java would be easier btw.
    Project thread
    Reply With Quote  
     

  7. #7  
    Registered Member
    Join Date
    Dec 2013
    Posts
    419
    Thanks given
    127
    Thanks received
    85
    Rep Power
    349
    Quote Originally Posted by clem585 View Post
    What's:

    Code:
    getLocalY
    in Position? Posting Position.java would be easier btw.
    Updated thread;

    Code:
    public final class Position {
    
    	private final int x, y, height;
    
    	public Position(int x, int y) {
    		this.x = x;
    		this.y = y;
    		this.height = 0;
    	}
    
    	public Position(int x, int y, int height) {
    		this.x = x;
    		this.y = y;
    		this.height = height;
    	}
    
    	public int getX() {
    		return x;
    	}
    
    	public int getY() {
    		return y;
    	}
    
    	public int getBaseLocalX() {
    		return getBaseLocalX(getCentralRegionX());
    	}
    
    	public int getBaseLocalY() {
    		return getBaseLocalY(getCentralRegionX());
    	}
    
    	public int getBaseLocalX(int centralRegionX) {
    		return (centralRegionX - 6) * 8;
    	}
    
    	public int getBaseLocalY(int centralRegionY) {
    		return (centralRegionY - 6) * 8;
    	}
    
    	public int getLocalX() {
    		return getLocalX(getCentralRegionX());
    	}
    
    	public int getLocalY() {
    		return getLocalY(getCentralRegionX());
    	}
    
    	public int getLocalX(int centralRegionX) {
    		return x - ((centralRegionX - 6) * 8);
    	}
    
    	public int getLocalY(int centralRegionY) {
    		return y - ((centralRegionY - 6) * 8);
    	}
    
    	public int getCentralRegionX() {
    		return x / 8;
    	}
    
    	public int getCentralRegionY() {
    		return y / 8;
    	}
    
    	public int getHeight() {
    		return height;
    	}
    
    	public boolean isWithinDistance(Position position) {
    		return isWithinDistance(position, 15);
    	}
    
    	public boolean isWithinDistance(Position position, int distance) {
    		int deltaX = position.getX() - x;
    		int deltaY = position.getY() - y;
    		return deltaX >= -(distance + 1) && deltaX <= distance && deltaY >= -(distance + 1) && deltaY <= distance;
    	}
    
    	public int distanceTo(Position other) {
    		int deltaX = other.getX() - x;
    		int deltaY = other.getY() - y;
    
    		if (deltaX == deltaY) {
    			deltaX *= 2;
    			deltaY *= 2;
    		}
    		return (int) Math.sqrt(deltaX * deltaX + deltaY * deltaY);
    	}
    
    	public Position transform(int diffX, int diffY, int diffZ) {
    		return new Position(x + diffX, y + diffY, height + diffZ);
    	}
    
    	@Override
    	public String toString() {
    		StringBuilder builder = new StringBuilder("Position[");
    
    		builder.append("X=");
    		builder.append(x);
    		builder.append(", Y=");
    		builder.append(y);
    		builder.append(", Z=");
    		builder.append(height);
    		builder.append("]");
    
    		return builder.toString();
    	}
    
    	@Override
    	public int hashCode() {
    		final int prime = 31;
    		int result = 1;
    		result = prime * result + height;
    		result = prime * result + x;
    		result = prime * result + y;
    		return result;
    	}
    
    	@Override
    	public boolean equals(Object obj) {
    		if (this == obj)
    			return true;
    		if (obj == null)
    			return false;
    		if (getClass() != obj.getClass())
    			return false;
    		Position other = (Position) obj;
    		if (height != other.height)
    			return false;
    		if (x != other.x)
    			return false;
    		if (y != other.y)
    			return false;
    		return true;
    	}
    
    }
    Reply With Quote  
     

  8. #8  
    Contributor

    clem585's Avatar
    Join Date
    Sep 2013
    Posts
    3,788
    Thanks given
    706
    Thanks received
    702
    Rep Power
    570
    Quote Originally Posted by Kiissmyswagb View Post
    Updated thread;

    Code:
    public final class Position {
    
    	private final int x, y, height;
    
    	public Position(int x, int y) {
    		this.x = x;
    		this.y = y;
    		this.height = 0;
    	}
    
    	public Position(int x, int y, int height) {
    		this.x = x;
    		this.y = y;
    		this.height = height;
    	}
    
    	public int getX() {
    		return x;
    	}
    
    	public int getY() {
    		return y;
    	}
    
    	public int getBaseLocalX() {
    		return getBaseLocalX(getCentralRegionX());
    	}
    
    	public int getBaseLocalY() {
    		return getBaseLocalY(getCentralRegionX());
    	}
    
    	public int getBaseLocalX(int centralRegionX) {
    		return (centralRegionX - 6) * 8;
    	}
    
    	public int getBaseLocalY(int centralRegionY) {
    		return (centralRegionY - 6) * 8;
    	}
    
    	public int getLocalX() {
    		return getLocalX(getCentralRegionX());
    	}
    
    	public int getLocalY() {
    		return getLocalY(getCentralRegionX());
    	}
    
    	public int getLocalX(int centralRegionX) {
    		return x - ((centralRegionX - 6) * 8);
    	}
    
    	public int getLocalY(int centralRegionY) {
    		return y - ((centralRegionY - 6) * 8);
    	}
    
    	public int getCentralRegionX() {
    		return x / 8;
    	}
    
    	public int getCentralRegionY() {
    		return y / 8;
    	}
    
    	public int getHeight() {
    		return height;
    	}
    
    	public boolean isWithinDistance(Position position) {
    		return isWithinDistance(position, 15);
    	}
    
    	public boolean isWithinDistance(Position position, int distance) {
    		int deltaX = position.getX() - x;
    		int deltaY = position.getY() - y;
    		return deltaX >= -(distance + 1) && deltaX <= distance && deltaY >= -(distance + 1) && deltaY <= distance;
    	}
    
    	public int distanceTo(Position other) {
    		int deltaX = other.getX() - x;
    		int deltaY = other.getY() - y;
    
    		if (deltaX == deltaY) {
    			deltaX *= 2;
    			deltaY *= 2;
    		}
    		return (int) Math.sqrt(deltaX * deltaX + deltaY * deltaY);
    	}
    
    	public Position transform(int diffX, int diffY, int diffZ) {
    		return new Position(x + diffX, y + diffY, height + diffZ);
    	}
    
    	@Override
    	public String toString() {
    		StringBuilder builder = new StringBuilder("Position[");
    
    		builder.append("X=");
    		builder.append(x);
    		builder.append(", Y=");
    		builder.append(y);
    		builder.append(", Z=");
    		builder.append(height);
    		builder.append("]");
    
    		return builder.toString();
    	}
    
    	@Override
    	public int hashCode() {
    		final int prime = 31;
    		int result = 1;
    		result = prime * result + height;
    		result = prime * result + x;
    		result = prime * result + y;
    		return result;
    	}
    
    	@Override
    	public boolean equals(Object obj) {
    		if (this == obj)
    			return true;
    		if (obj == null)
    			return false;
    		if (getClass() != obj.getClass())
    			return false;
    		Position other = (Position) obj;
    		if (height != other.height)
    			return false;
    		if (x != other.x)
    			return false;
    		if (y != other.y)
    			return false;
    		return true;
    	}
    
    }
    It looks alright and therefore, localY cannot in any given situation go over 64. If you really get something over 400, then you must have modified the y somewhere.
    Project thread
    Reply With Quote  
     

  9. #9  
    Registered Member
    Join Date
    Dec 2013
    Posts
    419
    Thanks given
    127
    Thanks received
    85
    Rep Power
    349
    Quote Originally Posted by clem585 View Post
    It looks alright and therefore, localY cannot in any given situation go over 64. If you really get something over 400, then you must have modified the y somewhere.
    ...

    Did you look at the AStarPathFinder class or even look at what the code does? lol
    Reply With Quote  
     

  10. #10  
    Contributor

    clem585's Avatar
    Join Date
    Sep 2013
    Posts
    3,788
    Thanks given
    706
    Thanks received
    702
    Rep Power
    570
    Quote Originally Posted by Kiissmyswagb View Post
    ...

    Did you look at the AStarPathFinder class or even look at what the code does? lol
    Yes. You said the issue was caused by y being over 400. It is theorically impossible. Let's pretend the y is 5000.

    Code:
    public int getLocalY() {
        return getLocalY(getCentralRegionX());
    }
    
    vvv
    
    public int getCentralRegionY() {
        return y / 8;//that would be 625
    }
    
    vvv
    
    public int getLocalY(625) {
        return y - ((625 - 6) * 8);//48
    }
    Code:
    Path path = pathFinder.find(base, 104, position.getLocalX(), position.getLocalY(), destLocalX, destLocalY);//"position.getLocalY()" is under 64
    I don't claim to know the error, but I know it's not related to y being over 400.
    Project thread
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Best Pathing Issue
    By Clank in forum Help
    Replies: 2
    Last Post: 09-16-2015, 08:51 AM
  2. Replies: 5
    Last Post: 02-28-2014, 01:34 AM
  3. paths and compilers! for xp
    By king swintell in forum Tutorials
    Replies: 8
    Last Post: 04-21-2008, 01:42 AM
  4. webclient issues[PLEASE HELP ME]
    By Spoonkitty in forum RS2 Client
    Replies: 5
    Last Post: 08-15-2007, 11:29 PM
  5. Environment Variables, An End to Paths.
    By Whitey in forum Tutorials
    Replies: 2
    Last Post: 07-23-2007, 09:28 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •