Jump to content

interactAndWait(), castSpellOn(Spell spell, Entity entity)


darkxor

Recommended Posts

Want to share some snippets i'm using. Dont sure if they are needed by scripters, but...

 

The first is function that interacts with object, and then waits until movement and animations will end.

If interact insuccessful, returns false without waiting.

public static boolean interactAndWait(Script scp, Player player, RS2Object obj, String action) throws InterruptedException{
		if(player.isMoving() || player.isAnimating()){
    		do{
    			scp.sleep(100);
    		} while(player.isMoving() || player.isAnimating());
    		scp.sleep(200);
    	}
    	//if(!obj.isVisible()){
    	//	scp.client.moveCameraToEntity(obj);
    	//	return false;
    	//}
    	
    	if(obj.interact(action)){
    		scp.sleep(500);
    		while(player.isMoving()){
    			scp.sleep(100);
    		}
    		int x = player.getX();
    		int y = player.getY();
    		int z = player.getZ();
    		scp.sleep(1500);
    		while(player.isAnimating())
    			scp.sleep(100);
    		if(action == "Pull"){
    			Date start = new Date();
    			while(true)
    			{
    				if((new Date().getTime() - start.getTime()) >= 5000){
    					scp.log("break by time");
    					break;
    				}
    				if(player.getX()!=x || player.getY()!=y || player.getZ()!=z){
    					scp.log("break by loc");
    					break; //teleported
    				}
    				scp.sleep(100);
    			}
    		}
    		return true;
    	}
    	return false;
	}

The second is casting spell on entity routine, with improved accuracy (tries to click by horizontal and vertical around bounding box center of entity). CHECK IT in your case!

public boolean castSpellOn(Spell spell, Entity entity) throws InterruptedException{
		scp.magicTab.castSpell(spell);
		sleep(200, 300);
		for(int i = 0; i<7; i++){
			Rectangle bb = entity.getMouseDestination().getBoundingBox();
			//Rectangle bbmodified = new Rectangle(bb.x - bb.width/2, bb.y - bb.height/2, bb.width, bb.height);
			int x = (int)bb.getCenterX();
			int y = (int)bb.getCenterY();
			if(i==0){
				if(!client.moveMouseTo(entity.getMouseDestination(), false, false, false))
					return false;
			}
			if(i==1){
				if(!client.moveMouseTo(new RectangleDestination(x - 10, y - 10, 4, 4), false, false, false))
					return false;
			}
			if(i==2){
				if(!client.moveMouseTo(new RectangleDestination(x - 2, y - 12, 4, 4), false, false, false))
					return false;
			}
			if(i==3){
				if(!client.moveMouseTo(new RectangleDestination(x + 10, y - 10, 4, 4), false, false, false))
					return false;
			}
			if(i==4){
				if(!client.moveMouseTo(new RectangleDestination(x + 10, y + 10, 4, 4), false, false, false))
					return false;
			}
			if(i==5){
				if(!client.moveMouseTo(new RectangleDestination(x - 2, y + 12, 4, 4), false, false, false))
					return false;
			}
			if(i==6){
				if(!client.moveMouseTo(new RectangleDestination(x - 10, y + 10, 4, 4), false, false, false))
					return false;
			}
			sleep(100, 150);
			Option opt = client.getMenu().get(0);
			if(opt==null)
				continue;
			if(!opt.action.equals("Cast"))
				continue;
			if(!opt.noun.contains(entity.getName()))
				continue;
			client.clickMouse(false);
			return true;
		}
		return false;
	}
	
Link to comment
Share on other sites

For dynamic sleeping:

 

 1. Create a new interface XDynamicSleep with an abstract canAwake method.

public interface XDynamicSleep {

	public boolean canAwake();

}

 2. If you've got a script framework, implement this into it, else just throw it in your script somewhere:

public boolean sleep(long base, long offset, XDynamicSleep sleep)
	throws InterruptedException {
	
	if (base < 0 || sleep == null)
		throw new IllegalArgumentException();
	
	long start = System.currentTimeMillis();
	
	base += MethodProvider((int) offset);
	offset = (long) (base * 0.1);
	
	while (start + base > System.currentTimeMillis()) {
		if (sleep.canAwake())
			return true;
		else
			sleep(offset);
	}
	return false;
}

 3. Time to sleep! Example:

	. . .
	
	if (goblin.interact("attack"))
		if (sleep(3000, 2000, new XDynamicSleep() {
				@Override
				public boolean canAwake() {
					return !goblin.exists();
				}
			})
		) {
			//Sleep was interrupted, goblin must have died
		} else {
			//Sleep slept to the full length... Time to revalidate
			//our situation!
		}

	. . .

I wrote this up in NotePad++ so there might be syntax errors.

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...