Jump to content

Traverse path with position and obstacle snippet


thepecher

Recommended Posts

First off, thanks for explaining the logic of such a path handling method and his obstacle class

//list is made with positions you want to reach and obstacles you want to handle
public void TraversePath(Script sI, ArrayList list) throws InterruptedException {
		sI=this.sI;
		
		int entry=findEntry(sI,list);
		for (int i = entry; i < (list.size());) {

		sI.log("Tackling path/obstacle number: " +i);
		if(list.get(i) instanceof Position ){
			sI.log("Instance position");
			if(sI.map.canReach((Position)list.get(i)) && sI.myPlayer().getPosition() != list.get(i)){
				sI.log("CanReach is true, first if()");
				sI.localWalker.walk((Position) list.get(i));
				clickMiniMapPosition((Position) list.get(i));
				i++;
			} else{
				sI.log("CanReach is false, second if()");
				i--;
				
				}
			
		}
		else if(list.get(i) instanceof Obstacle){
			
			sI.log("Instance Obstacle");
			String obstacleName=((Obstacle)list.get(i)).getName();
			RS2Object obstacleObject=sI.objects.closest(obstacleName);
			
		
			
			if(sI.map.canReach(((Obstacle)list.get(i)).getPosition()) && sI.myPlayer().getPosition() != ((Obstacle)list.get(i)).getPosition()){
				sI.log("CanReach is true, first if()");
				sI.localWalker.walk(((Obstacle)list.get(i)).getPosition());
				clickMiniMapPosition(((Obstacle)list.get(i)).getPosition());
				i++;
			} else if(obstacleObject !=null && obstacleObject.exists() && (obstacleObject.hasAction(((Obstacle)list.get(i)).getAction()) || obstacleName=="Trapdoor")){
			
				sI.log("CanReach is false, second if()");
				
				if(obstacleObject.getOrientation()==0 || obstacleObject.getOrientation()==2){
					sI.camera.moveYaw(MethodProvider.random(246,300));
				} else{sI.camera.moveYaw(MethodProvider.random(150,200));}
				//^for broken interaction, thanks OsBot

				obstacleObject.interact(((Obstacle) list.get(i)).getAction());		
				
					i++;
			} else{
				i--;
			}
			}
		sI.sleep(MethodProvider.random(1000,1500));
		if(i==list.size()+1){
			i=0;
		}
		if(i==-1){
			i=list.size();
		}
	
	}
	}
public static int findEntry(Script script, ArrayList path) {
		int index = -1;
		for (int i = 0; i < path.size(); i++) {
			Object o = path.get(i);
			if (o instanceof Position && script.getMap().canReach(((Position) o))) {
				index = i;
			}
		}
		return index;
	}

public boolean clickMiniMapPosition(Position position) throws InterruptedException {
		return sI.mouse.click(new MiniMapTileDestination(sI.bot, position), false);
	}
 
public class Obstacle {
	
	
        private final String name;
        private final String action;
        private final Position position;
       
        public Obstacle(final String name, final String action, final Position position) {
                this.name = name;
                this.action = action;
                this.position = position;
        }
        
        public Position getPosition()
        {
            return this.position;
        }
        public String getAction()
        {
        	return this.action;
        }
        public String getName()
        {
        	return this.name;
        }
     
}
Edited by thepecher
  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

It's great practice if you use the interface list rather than the class arraylist. Also if you cast your list<E>. You don't have to add in the instanceof checks it actually casting a variable off the list.

I'm assuming he did it because the list contains more than one type of data.

it contains positions AND obstacles, so of course it will need to check which one it's currently

dealing with.

Link to comment
Share on other sites

I'm assuming he did it because the list contains more than one type of data.

it contains positions AND obstacles, so of course it will need to check which one it's currently

dealing with.

then create new class that support position entity and obstacle and cast it using the new class he created

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...