Thread: [Elvarg / OSRSPK] Pathfinding fix

Results 1 to 6 of 6
  1. #1 [Elvarg / OSRSPK] Pathfinding fix 
    Donator
    Join Date
    Sep 2010
    Posts
    5
    Thanks given
    0
    Thanks received
    1
    Rep Power
    0
    Hey guys, I ran into an issue in Elvarg/OSRSPK with larger NPCs moving through everything and couldn't find a fix anywhere.

    Here is the fix

    Replace your RS317PathFinder.java with mine (Portion of code changed is color coded):

    P.S: If you have more insight be sure to share

    Spoiler for View Code:

    Code:
    package com.sythium.game.model.movement.path;
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    
    import com.sythium.game.collision.RegionManager;
    import com.sythium.game.entity.impl.Character;
    import com.sythium.game.model.Position;
    import com.sythium.game.model.movement.MovementQueue;
    
    public class RS317PathFinder {
    
    	private static final int DEFAULT_PATH_LENGTH = 4000;
    	
    	public static boolean accessable(int x, int y, int z, int destX, int destY) {
    		Position p = new Position(x, y, z);
    		if (destX == p.getLocalX() && destY == p.getLocalY()) {
    			return false;
    		}
    
    		int[][] via = new int[104][104];
    		int[][] cost = new int[104][104];
    
    		List<Integer> tileQueueX = new ArrayList<Integer>(10000);
    		List<Integer> tileQueueY = new ArrayList<Integer>(10000);
    
    		int curX = p.getLocalX();
    		int curY = p.getLocalY();
    		via[curX][curY] = 99;
    		cost[curX][curY] = 1;
    		int tail = 0;
    		tileQueueX.add(curX);
    		tileQueueY.add(curY);
    
    		final int regionX = p.getRegionX() << 3;
    		final int regionY = p.getRegionY() << 3;
    
    		destX = destX - regionX;
    		destY = destY - regionY;
    
    		while (tail != tileQueueX.size()
    				&& tileQueueX.size() < DEFAULT_PATH_LENGTH) {
    
    			curX = tileQueueX.get(tail);
    			curY = tileQueueY.get(tail);
    
    			int curAbsX = regionX + curX;
    			int curAbsY = regionY + curY;
    
    			if (curX == destX && curY == destY) {
    				return true;
    			}
    
    			tail = (tail + 1) % DEFAULT_PATH_LENGTH;
    
    			int thisCost = cost[curX][curY] + 1 + 1;
    
    			if (curY > 0 && via[curX][curY - 1] == 0
    					&& (RegionManager.getClipping(curAbsX, curAbsY - 1, z) & 0x1280102) == 0) {
    				tileQueueX.add(curX);
    				tileQueueY.add(curY - 1);
    				via[curX][curY - 1] = 1;
    				cost[curX][curY - 1] = thisCost;
    			}
    
    			if (curX > 0 && via[curX - 1][curY] == 0
    					&& (RegionManager.getClipping(curAbsX - 1, curAbsY, z) & 0x1280108) == 0) {
    				tileQueueX.add(curX - 1);
    				tileQueueY.add(curY);
    				via[curX - 1][curY] = 2;
    				cost[curX - 1][curY] = thisCost;
    			}
    
    			if (curY < 104 - 1 && via[curX][curY + 1] == 0
    					&& (RegionManager.getClipping(curAbsX, curAbsY + 1, z) & 0x1280120) == 0) {
    				tileQueueX.add(curX);
    				tileQueueY.add(curY + 1);
    				via[curX][curY + 1] = 4;
    				cost[curX][curY + 1] = thisCost;
    			}
    
    			if (curX < 104 - 1 && via[curX + 1][curY] == 0
    					&& (RegionManager.getClipping(curAbsX + 1, curAbsY, z) & 0x1280180) == 0) {
    				tileQueueX.add(curX + 1);
    				tileQueueY.add(curY);
    				via[curX + 1][curY] = 8;
    				cost[curX + 1][curY] = thisCost;
    			}
    
    			if (curX > 0 && curY > 0 && via[curX - 1][curY - 1] == 0
    					&& (RegionManager.getClipping(curAbsX - 1, curAbsY - 1, z) & 0x128010e) == 0
    					&& (RegionManager.getClipping(curAbsX - 1, curAbsY, z) & 0x1280108) == 0
    					&& (RegionManager.getClipping(curAbsX, curAbsY - 1, z) & 0x1280102) == 0) {
    				tileQueueX.add(curX - 1);
    				tileQueueY.add(curY - 1);
    				via[curX - 1][curY - 1] = 3;
    				cost[curX - 1][curY - 1] = thisCost;
    			}
    
    			if (curX > 0 && curY < 104 - 1 && via[curX - 1][curY + 1] == 0
    					&& (RegionManager.getClipping(curAbsX - 1, curAbsY + 1, z) & 0x1280138) == 0
    					&& (RegionManager.getClipping(curAbsX - 1, curAbsY, z) & 0x1280108) == 0
    					&& (RegionManager.getClipping(curAbsX, curAbsY + 1, z) & 0x1280120) == 0) {
    				tileQueueX.add(curX - 1);
    				tileQueueY.add(curY + 1);
    				via[curX - 1][curY + 1] = 6;
    				cost[curX - 1][curY + 1] = thisCost;
    			}
    
    			if (curX < 104 - 1 && curY > 0 && via[curX + 1][curY - 1] == 0
    					&& (RegionManager.getClipping(curAbsX + 1, curAbsY - 1, z) & 0x1280183) == 0
    					&& (RegionManager.getClipping(curAbsX + 1, curAbsY, z) & 0x1280180) == 0
    					&& (RegionManager.getClipping(curAbsX, curAbsY - 1, z) & 0x1280102) == 0) {
    				tileQueueX.add(curX + 1);
    				tileQueueY.add(curY - 1);
    				via[curX + 1][curY - 1] = 9;
    				cost[curX + 1][curY - 1] = thisCost;
    			}
    
    			if (curX < 104 - 1 && curY < 104 - 1
    					&& via[curX + 1][curY + 1] == 0
    					&& (RegionManager.getClipping(curAbsX + 1, curAbsY + 1, z) & 0x12801e0) == 0
    					&& (RegionManager.getClipping(curAbsX + 1, curAbsY, z) & 0x1280180) == 0
    					&& (RegionManager.getClipping(curAbsX, curAbsY + 1, z) & 0x1280120) == 0) {
    				tileQueueX.add(curX + 1);
    				tileQueueY.add(curY + 1);
    				via[curX + 1][curY + 1] = 12;
    				cost[curX + 1][curY + 1] = thisCost;
    			}
    		}
    
    		return false;
    	}
    	
    	public static void findPath(Character gc, int destX, int destY, boolean moveNear,
    			int xLength, int yLength) {
    		try {
    			if (destX == gc.getPosition().getLocalX() && destY == gc.getPosition().getLocalY() && !moveNear) {
    				return;
    			}
    			
    			
    			final int height = gc.getPosition().getZ() % 4;
    			destX = destX - 8 * gc.getPosition().getRegionX();
    			destY = destY - 8 * gc.getPosition().getRegionY();
    			final int[][] via = new int[104][104];
    			final int[][] cost = new int[104][104];
    			final LinkedList<Integer> tileQueueX = new LinkedList<Integer>();
    			final LinkedList<Integer> tileQueueY = new LinkedList<Integer>();
    			for (int xx = 0; xx < 104; xx++)
    				for (int yy = 0; yy < 104; yy++)
    					cost[xx][yy] = 99999999;
    			int curX = gc.getPosition().getLocalX();
    			int curY = gc.getPosition().getLocalY();
    			if (curX > via.length - 1 || curY > via[curX].length - 1)
    				return;
    			if (curX < via.length && curY < via[0].length)
    				via[curX][curY] = 99;
    			if (curX < cost.length && curY < cost[0].length)
    				cost[curX][curY] = 0;
    			@SuppressWarnings("unused")
    			final int head = 0;
    			int tail = 0;
    			tileQueueX.add(curX);
    			tileQueueY.add(curY);
    			boolean foundPath = false;
    			while (tail != tileQueueX.size() && tileQueueX.size() < DEFAULT_PATH_LENGTH) {
    				curX = tileQueueX.get(tail);
    				curY = tileQueueY.get(tail);
    				final int curAbsX = gc.getPosition().getRegionX() * 8 + curX;
    				final int curAbsY = gc.getPosition().getRegionY() * 8 + curY;
    				if (curX == destX && curY == destY) {
    					foundPath = true;
    					break;
    				}
    				tail = (tail + 1) % DEFAULT_PATH_LENGTH;
    
    				if (cost.length < curX || cost[curX].length < curY)
    					return;
    				final int thisCost = cost[curX][curY] + 1;
    
    				if (curY > 0
    						&& via[curX][curY - 1] == 0
    						&& (RegionManager.getClipping(curAbsX, curAbsY - 1, height) & 0x1280102) == 0) {
    					tileQueueX.add(curX);
    					tileQueueY.add(curY - 1);
    					via[curX][curY - 1] = 1;
    					cost[curX][curY - 1] = thisCost;
    				}
    				if (curX > 0
    						&& via[curX - 1][curY] == 0
    						&& (RegionManager.getClipping(curAbsX - 1, curAbsY, height) & 0x1280108) == 0) {
    					tileQueueX.add(curX - 1);
    					tileQueueY.add(curY);
    					via[curX - 1][curY] = 2;
    					cost[curX - 1][curY] = thisCost;
    				}
    				if (curY < 104 - 1
    						&& via[curX][curY + 1] == 0
    						&& (RegionManager.getClipping(curAbsX, curAbsY + 1, height) & 0x1280120) == 0) {
    					tileQueueX.add(curX);
    					tileQueueY.add(curY + 1);
    					via[curX][curY + 1] = 4;
    					cost[curX][curY + 1] = thisCost;
    				}
    				if (curX < 104 - 1
    						&& via[curX + 1][curY] == 0
    						&& (RegionManager.getClipping(curAbsX + 1, curAbsY, height) & 0x1280180) == 0) {
    					tileQueueX.add(curX + 1);
    					tileQueueY.add(curY);
    					via[curX + 1][curY] = 8;
    					cost[curX + 1][curY] = thisCost;
    				}
    				if (curX > 0
    						&& curY > 0
    						&& via[curX - 1][curY - 1] == 0
    						&& (RegionManager.getClipping(curAbsX - 1, curAbsY - 1, height) & 0x128010e) == 0
    						&& (RegionManager.getClipping(curAbsX - 1, curAbsY, height) & 0x1280108) == 0
    						&& (RegionManager.getClipping(curAbsX, curAbsY - 1, height) & 0x1280102) == 0) {
    					tileQueueX.add(curX - 1);
    					tileQueueY.add(curY - 1);
    					via[curX - 1][curY - 1] = 3;
    					cost[curX - 1][curY - 1] = thisCost;
    				}
    				if (curX > 0
    						&& curY < 104 - 1
    						&& via[curX - 1][curY + 1] == 0
    						&& (RegionManager.getClipping(curAbsX - 1, curAbsY + 1, height) & 0x1280138) == 0
    						&& (RegionManager.getClipping(curAbsX - 1, curAbsY, height) & 0x1280108) == 0
    						&& (RegionManager.getClipping(curAbsX, curAbsY + 1, height) & 0x1280120) == 0) {
    					tileQueueX.add(curX - 1);
    					tileQueueY.add(curY + 1);
    					via[curX - 1][curY + 1] = 6;
    					cost[curX - 1][curY + 1] = thisCost;
    				}
    				if (curX < 104 - 1
    						&& curY > 0
    						&& via[curX + 1][curY - 1] == 0
    						&& (RegionManager.getClipping(curAbsX + 1, curAbsY - 1, height) & 0x1280183) == 0
    						&& (RegionManager.getClipping(curAbsX + 1, curAbsY, height) & 0x1280180) == 0
    						&& (RegionManager.getClipping(curAbsX, curAbsY - 1, height) & 0x1280102) == 0) {
    					tileQueueX.add(curX + 1);
    					tileQueueY.add(curY - 1);
    					via[curX + 1][curY - 1] = 9;
    					cost[curX + 1][curY - 1] = thisCost;
    				}
    				if (curX < 104 - 1
    						&& curY < 104 - 1
    						&& via[curX + 1][curY + 1] == 0
    						&& (RegionManager.getClipping(curAbsX + 1, curAbsY + 1, height) & 0x12801e0) == 0
    						&& (RegionManager.getClipping(curAbsX + 1, curAbsY, height) & 0x1280180) == 0
    						&& (RegionManager.getClipping(curAbsX, curAbsY + 1, height) & 0x1280120) == 0) {
    					tileQueueX.add(curX + 1);
    					tileQueueY.add(curY + 1);
    					via[curX + 1][curY + 1] = 12;
    					cost[curX + 1][curY + 1] = thisCost;
    				}
    			}
    			if (!foundPath)
    				if (moveNear) {
    					int i_223_ = 1000;
    					int thisCost = 100;
    					final int i_225_ = 10;
    					for (int x = destX - i_225_; x <= destX + i_225_; x++)
    						for (int y = destY - i_225_; y <= destY + i_225_; y++)
    							if (x >= 0 && y >= 0 && x < 104 && y < 104
    							&& cost[x][y] < 100) {
    								int i_228_ = 0;
    								if (x < destX)
    									i_228_ = destX - x;
    								else if (x > destX + xLength - 1)
    									i_228_ = x - (destX + xLength - 1);
    								int i_229_ = 0;
    								if (y < destY)
    									i_229_ = destY - y;
    								else if (y > destY + yLength - 1)
    									i_229_ = y - (destY + yLength - 1);
    								final int i_230_ = i_228_ * i_228_ + i_229_
    										* i_229_;
    								if (i_230_ < i_223_ || i_230_ == i_223_
    										&& cost[x][y] < thisCost) {
    									i_223_ = i_230_;
    									thisCost = cost[x][y];
    									curX = x;
    									curY = y;
    								}
    							}
    					if (i_223_ == 1000)
    						return;
    				} else {
    					return;
    				}
    			tail = 0;
    			tileQueueX.set(tail, curX);
    			tileQueueY.set(tail++, curY);
    			int l5;
    			for (int j5 = l5 = via[curX][curY]; curX != gc.getPosition().getLocalX()
    					|| curY != gc.getPosition().getLocalY(); j5 = via[curX][curY]) {
    				if (j5 != l5) {
    					l5 = j5;
    					tileQueueX.set(tail, curX);
    					tileQueueY.set(tail++, curY);
    				}
    				if ((j5 & 2) != 0)
    					curX++;
    				else if ((j5 & 8) != 0)
    					curX--;
    				if ((j5 & 1) != 0)
    					curY++;
    				else if ((j5 & 4) != 0)
    					curY--;
    			}
    			final int size = tail--;
    			int pathX = gc.getPosition().getRegionX() * 8 + tileQueueX.get(tail);
    			int pathY = gc.getPosition().getRegionY() * 8 + tileQueueY.get(tail);
    			Position firstStep = new Position(pathX, pathY, gc.getPosition().getZ());
    			gc.getMovementQueue();
    			if(MovementQueue.canWalk(gc.getPosition(), firstStep, gc.getSize()))
    					gc.getMovementQueue().addFirstStep(firstStep);
    			for (int i = 1; i < size; i++) {
    				tail--;
    				pathX = gc.getPosition().getRegionX() * 8 + tileQueueX.get(tail);
    				pathY = gc.getPosition().getRegionY() * 8 + tileQueueY.get(tail);
    				Position subsequentStep = new Position(pathX, pathY, gc.getPosition().getZ());
    				if(MovementQueue.canWalk(gc.getPosition(), subsequentStep, gc.getSize())) {
    					gc.getMovementQueue().addStep(subsequentStep);
    					break;
    				}
    			}
    		} catch(Exception e) {
    			e.printStackTrace();
    			System.out.println("Error finding route, destx: "+destX+", destY: "+destY+". Reseted queue.");
    			gc.getMovementQueue().setFollowCharacter(null);
    			gc.getMovementQueue().reset();
    		}
    	}
    	
    	/**
    	 * Checks if the given characters are in a diagonal block.
    	 * @param attacker
    	 * @param attacked
    	 * @return
    	 */
    	public final static boolean isInDiagonalBlock(Character attacker, Character attacked) {
    		return attacked.getPosition().getX() - 1 == attacker.getPosition()
    				.getX()
    				&& attacked.getPosition().getY() + 1 == attacker.getPosition()
    				.getY()
    				|| attacker.getPosition().getX() - 1 == attacked.getPosition()
    				.getX()
    				&& attacker.getPosition().getY() + 1 == attacked.getPosition()
    				.getY()
    				|| attacked.getPosition().getX() + 1 == attacker.getPosition()
    				.getX()
    				&& attacked.getPosition().getY() - 1 == attacker.getPosition()
    				.getY()
    				|| attacker.getPosition().getX() + 1 == attacked.getPosition()
    				.getX()
    				&& attacker.getPosition().getY() - 1 == attacked.getPosition()
    				.getY()
    				|| attacked.getPosition().getX() + 1 == attacker.getPosition()
    				.getX()
    				&& attacked.getPosition().getY() + 1 == attacker.getPosition()
    				.getY()
    				|| attacker.getPosition().getX() + 1 == attacked.getPosition()
    				.getX()
    				&& attacker.getPosition().getY() + 1 == attacked.getPosition()
    				.getY();
    	}
    
    	public static void solveDiagonalBlock(Character attacker, Character target) {
    		int aX = attacker.getPosition().getX();
    		int aY = attacker.getPosition().getY();
    
    		int vX = target.getPosition().getX();
    		int vY = target.getPosition().getY();
    
    		int stepX = 0;
    		int stepY = 0;
    
    		if(aX > vX) {
    			stepX = 1;
    		} else if(vX > aX) {
    			stepX = -1;
    		} else if(aY > vY) {
    			stepY = 1;
    		} else if(vY > aY) {
    			stepY = -1;
    		} else if(aX == vX && aY == vY) {
    			stepY = -1;
    		}
    
    		findPath(attacker, target.getPosition().getX() + stepX, target.getPosition().getY() + stepY, false, 8, 8);
    	}
    }
    Reply With Quote  
     

  2. Thankful user:


  3. #2  
    Registered Member
    Join Date
    Feb 2015
    Posts
    742
    Thanks given
    4
    Thanks received
    145
    Rep Power
    54
    Kudos. I've played about with this, certainly an improvement over the bog standard.

    Thank you.
    Reply With Quote  
     

  4. #3  
    Donator
    Join Date
    Sep 2010
    Posts
    5
    Thanks given
    0
    Thanks received
    1
    Rep Power
    0
    Glad to help
    Reply With Quote  
     

  5. #4  
    WhiteDownMb | HYBRID JEFE

    Join Date
    Dec 2009
    Posts
    1,872
    Thanks given
    269
    Thanks received
    395
    Rep Power
    1276
    You need to account for npc size

    All of humanity's problems stem from man's
    inability to sit quietly in a room by himself

    Reply With Quote  
     

  6. #5  
    Registered Member
    Join Date
    Dec 2011
    Posts
    704
    Thanks given
    9
    Thanks received
    3
    Rep Power
    30
    What does this fix?
    Reply With Quote  
     

  7. #6  
    Arix Developer

    Tutus Frutus's Avatar
    Join Date
    Feb 2018
    Posts
    290
    Thanks given
    29
    Thanks received
    117
    Rep Power
    361
    Quote Originally Posted by awesomenoob View Post
    What does this fix?
    "larger NPCs moving through everything"
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

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

Similar Threads

  1. Replies: 64
    Last Post: 09-04-2017, 09:22 PM
  2. Replies: 0
    Last Post: 07-09-2017, 02:59 PM
  3. Replies: 3
    Last Post: 07-09-2017, 12:31 PM
  4. [OSRSPK/Elvarg] Duel Glitch fix needed.
    By kev2017 in forum Help
    Replies: 3
    Last Post: 07-09-2017, 10:31 AM
  5. PathFinding Fixed
    By Jay Gatsby in forum Buying
    Replies: 1
    Last Post: 10-26-2014, 06:12 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
  •