Jump to content

Simple Animation Timer


Juggles

Recommended Posts

Very simple snippet of an animation timer I used in one of my scripts. 

Useful for things such as fletching, crafting, etc. Things where the animation returns -1 even when it is still doing an action.

Define variable

    long lastAnimation = 0;

 

Usage: 

 if (myPlayer().isAnimating()) {

                    lastAnimation = System.currentTimeMillis();

  } else if (System.currentTimeMillis() > (lastAnimation + int)){
                    
                    //do action 
}

int is the amount of time to sleep before it needs to click again if you haven't animated.

 

For example, if you wanted it to click after 2 seconds of not animating, you would put 2000 there. 

Edited by Juggles
  • Like 15
Link to comment
Share on other sites

  • 6 months later...
  • 3 months later...
On 2/9/2017 at 4:22 AM, Juggles said:

Very simple snippet of an animation timer I used in one of my scripts. 

Useful for things such as fletching, crafting, etc. Things where the animation returns -1 even when it is still doing an action.

Define variable


    long lastAnimation = 0;

 

Usage: 


 if (myPlayer().isAnimating()) {

                    lastAnimation = System.currentTimeMillis();

  } else if (System.currentTimeMillis() > (lastAnimation + int)){
                    
                    //do action 
}

int is the amount of time to sleep before it needs to click again if you haven't animated.

 

For example, if you wanted it to click after 2 seconds of not animating, you would put 2000 there. 

 

4 hours ago, jca said:

Thanks... helped a lot. 

 

This just looks like an over complicated, and less efficient way of writing:

if (myPlayer().isAnimating()) {
    sleep(2000);
} else {
    // do action 
}

 

  • Like 1
Link to comment
Share on other sites

On 05/12/2017 at 2:48 PM, Explv said:

 

 

This just looks like an over complicated, and less efficient way of writing:


if (myPlayer().isAnimating()) {
    sleep(2000);
} else {
    // do action 
}

 

I was under the impression sleep() was discouraged in place of ConditionalSleep() 

Then with ConditionalSleep() I wasn't able to find the right condition that would allow me to pick an item up after combat, thus a timer seemed to come in handy to delay the action of combat so bot has time to pick up item.

Please enlighten me if this is not the case.

Also thanks for the maps - helped heaps. 

EDIT: Also, surely this would not help in the case of picking up an item after combat? If the player is fighting then isAnimating() = true, but as soon as it stops fighting isAnimating() = false, which lets it proceed. 

Edited by jca
Link to comment
Share on other sites

1 hour ago, jca said:

I was under the impression sleep() was discouraged in place of ConditionalSleep() 

Then with ConditionalSleep() I wasn't able to find the right condition that would allow me to pick an item up after combat, thus a timer seemed to come in handy to delay the action of combat so bot has time to pick up item.

Please enlighten me if this is not the case.

Also thanks for the maps - helped heaps. 

EDIT: Also, surely this would not help in the case of picking up an item after combat? If the player is fighting then isAnimating() = true, but as soon as it stops fighting isAnimating() = false, which lets it proceed. 

Check if item exists 

Link to comment
Share on other sites

2 hours ago, jca said:

I was under the impression sleep() was discouraged in place of ConditionalSleep() 

Then with ConditionalSleep() I wasn't able to find the right condition that would allow me to pick an item up after combat, thus a timer seemed to come in handy to delay the action of combat so bot has time to pick up item.

Please enlighten me if this is not the case.

Also thanks for the maps - helped heaps. 

EDIT: Also, surely this would not help in the case of picking up an item after combat? If the player is fighting then isAnimating() = true, but as soon as it stops fighting isAnimating() = false, which lets it proceed. 

sleep() is not discouraged, the sleep method and the ConditionalSleep class serve two different purposes.

The first allows you to sleep for a fixed amount of time, and the second allows you to sleep until a condition is satisfied or a timeout exceeded.

As for your question about picking up an item after combat, isAnimating() is a shitty way to determine if you are in combat, you could do something like this instead (note, not tested at all) :

 

if (!myPlayer().isUnderAttack() && myPlayer().getInteracting() == null) {
    GroundItem bones = getGroundItems().closest("Bones");
    if (bones != null) {
        if (bones.interact("Take")) {
            new CondtionalSleep(5000) {
                @Override
                public boolean condition() {
                    return !bones.exists(); 
                }
            }.sleep();
        }
    } else {
        NPC monster = getNpcs().closest(npc -> npc.getName().equals("Monster") && npc.isAttackable());
        if (monster != null && monster.interact("Attack")) {
            new ConditionalSleep(5000) {
                @Override
                public boolean condition() {
                    return !monster.isAttackable() || myPlayer().getInteracting() == monster; 
                }
            }
        }
    }
}

 

Edited by Explv
Link to comment
Share on other sites

1 hour ago, Explv said:

sleep() is not discouraged, the sleep method and the ConditionalSleep class serve two different purposes.

The first allows you to sleep for a fixed amount of time, and the second allows you to sleep until a condition is satisfied or a timeout exceeded.

As for your question about picking up an item after combat, isAnimating() is a shitty way to determine if you are in combat, you could do something like this instead (note, not tested at all) :

 


if (!myPlayer().isUnderAttack() && myPlayer().getInteracting() == null) {
    GroundItem bones = getGroundItems().closest("Bones");
    if (bones != null) {
        if (bones.interact("Take")) {
            new CondtionalSleep(5000) {
                @Override
                public boolean condition() {
                    return !bones.exists(); 
                }
            }.sleep();
        }
    } else {
        NPC monster = getNpcs().closest(npc -> npc.getName().equals("Monster") && npc.isAttackable());
        if (monster != null && monster.interact("Attack")) {
            new ConditionalSleep(5000) {
                @Override
                public boolean condition() {
                    return !monster.isAttackable() || myPlayer().getInteracting() == monster; 
                }
            }
        }
    }
}

 

The problem with this is the dying animation of the NPC, if there's multiple NPCs and no item exists on the ground until the dying animation is complete. 

myPlayer().isUnderAttack() resolves to false when NPC is dead, but has not completed dying animation, which means there's no item on the ground and bones != null will be false. 

myPlayer().getInteracting() == null will also be true when the NPC is dead, but has not completed animation. 

Therefore is there's another NPC near by they will go on to attack that NPC as monster != null && monster.interact("Attack") is true. 

I also tried .exists(), isVisible() etc. but all have the same result, so the only option I've found is timer to allow for NPC to complete dying animation. 

 

Link to comment
Share on other sites

39 minutes ago, jca said:

The problem with this is the dying animation of the NPC, if there's multiple NPCs and no item exists on the ground until the dying animation is complete. 

myPlayer().isUnderAttack() resolves to false when NPC is dead, but has not completed dying animation, which means there's no item on the ground and bones != null will be false. 

myPlayer().getInteracting() == null will also be true when the NPC is dead, but has not completed animation. 

Therefore is there's another NPC near by they will go on to attack that NPC as monster != null && monster.interact("Attack") is true. 

I also tried .exists(), isVisible() etc. but all have the same result, so the only option I've found is timer to allow for NPC to complete dying animation. 

 

 

I didn't realise you wanted to wait for the animation to finish.

This is probably how I would approach such a problem (note I wrote this fucking quickly, so it probably doesn't even work, but it should at least give you an idea)

NPC currentMonster;

@Override
public int onLoop() throws InterruptedException {
    // If we need to attack a monster
    if (currentMonster == null) {
        // Get the closest monster that is attackable
        NPC monster = getNpcs().closest(npc -> npc.getName().equals("Monster") && npc.isAttackable());

        // If the attack interaction was successful
        if (monster != null && monster.interact("Attack")) {
          
            // Sleep until we're fighting the monster, or someone else is fighting it
            new ConditionalSleep(5000) {
                @Override
                public boolean condition() {
                    return !monster.isAttackable() || myPlayer().getInteracting() == monster; 
                }
            }.sleep();
      
            // If we're fighting the monster, set it as our currentMonster
       	    if (myPlayer().getInteracting() == monster) {
                currentMonster = monster;
            }
        }
    }
    // If we are fighting, do nothing
    else if (myPlayer().getInteracting() == currentMonster) {
        return 600;
    }
    // If we're not fighting, and the current monster is dying, wait until the animation has finished so we can pick up items
    else if (currentMonster.isAnimating() && currentMonster.getHealthPercent() == 0) {
        new ConditionalSleep(3000) {
            @Override
            public boolean condition() {
                return !currentMonster.isAnimating(); // Could change to exists(), not sure
            }
        }.sleep();
        sleep(200); // Not sure if there is a delay between death and items appearing
    } 
    else {
        GroundItem bones = getGroundItems().closest("Bones");
        // If loot exists, then we take it
      	if (bones != null) {
            if (bones.interact("Take")) {
                new CondtionalSleep(5000) {
                    @Override
                    public boolean condition() {
                        return !bones.exists(); 
                    }
                }.sleep();
            }
        } else { // Otherwise set current monster to null so we can attack a new one.
            currentMonster = null; 
        }
    }
    return 200;
}

 

Edited by Explv
Link to comment
Share on other sites

15 minutes ago, Explv said:

 

I didn't realise you wanted to wait for the animation to finish.

This is probably how I would approach such a problem (note I wrote this fucking quickly, so it probably doesn't even work, but it should at least give you an idea)


// If we're not fighting, and the current monster is dying, wait until the animation has finished so we can pick up items
else if (currentMonster.isAnimating() && currentMonster.getHealthPercent() == 0) {
  new ConditionalSleep(3000) {
    @Override
      public boolean condition() {
      return !currentMonster.isAnimating(); // Could change to exists(), not sure
    }
  }.sleep();
  sleep(200); // Not sure if there is a delay between death and items appearing
} 

 

I'll try switching out the timer for isAnimating() && getHealthPercent 

Thanks for the snippet. 

Link to comment
Share on other sites

  • 2 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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