Jump to content

First Script Help


poptartjake

Recommended Posts

I don't feel like I'm learning much by just remaking the same fisher/miner/woodcutter as the tutorials, so I like to try and make something that's a bit more demanding. With that being said, this is my failed attempt at an auto fighter for some cows...Script just stands there no matter what changes I've made (posted source is after 3-4 revisions/attempts). And I didn't originally use ID's for anyone that's going to post about it. 

public class main extends Script {

	private static final int[] COW_ID = { 2808, 
		2806, 2805 };	
	
	private enum State {
		IN_COMBAT, ATTACKING, IDLE
	};
	
	private State getState() {
		if(myPlayer().isAnimating())
			return State.IN_COMBAT;
		if(myPlayer().isMoving())
			return State.ATTACKING;
		return State.IDLE;
	}
	
	@Override
	public void onStart() {
		log("Let's get started!");
	}
	
	@Override
	public int onLoop() throws InterruptedException {
		switch (getState()) {
			case ATTACKING:
				if(!myPlayer().isUnderAttack()){
				NPC cow = npcs.closest(COW_ID);
				if (cow.isAttackable()) {
					cow.interact("Attack-Cow");
				}
			}	
				break;
			case IN_COMBAT:
				sleep(random(300, 400));
				break;	
			case IDLE:
				sleep(random(50, 200));
				break;
		}
		return random(200, 300);
	}
	
	@Override
	public void onExit() {
		log("Thanks for running my cow killer!");
	}
}

Thanks in advanced. 

Edited by poptartjake
Link to comment
Share on other sites

Read the getState method, look at when State.ATTACKING is actually set: When the player is moving. So whenever the player is moving, it will attack, if its not moving, it will be idle. 

        if(myPlayer().isUnderAttack()) {
            return State.IN_COMBAT;
        } else {
            return State.ATTACKING;
        }

That is just an example, you can find a suitable way of setting the state to attack biggrin.png

 

EDIT: 

 

"Attack" << Correct 

"Attack-Cow" << Incorrect

Edited by Czar
Link to comment
Share on other sites

Read the getState method, look at when State.ATTACKING is actually set: When the player is moving. So whenever the player is moving, it will attack, if its not moving, it will be idle. 

        if(myPlayer().isUnderAttack()) {
            return State.IN_COMBAT;
        } else {
            return State.ATTACKING;
        }

That is just an example, you can find a suitable way of setting the state to attack biggrin.png

 

EDIT: 

 

"Attack" << Correct 

"Attack-Cow" << Incorrect

 

Much appreciation. I've made the changes suggested and I'm now successfully murdering them naughty naughty cows. :)  Was just worried that my logic was completely off and that combat was going to be handled differently. 

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