Jump to content

How to tell if player is attacking me


Recommended Posts

Posted (edited)
script.players.getAll().stream().filter(x -> script.myPlayer().equals(x.getInteracting())).findFirst().isPresent(); 
// there is a player ATTEMPTING to attack, not necessarily already attacked

script.myPlayer().isHitBarVisible(); 
// only works if the attack triggered health bar display (not a splashed spell)

script.projectiles.getAll().stream().filter(x -> script.myPlayer().equals(x.getTargetEntity())).findFirst().isPresent();
// a ranged/magic attack was launched against the bot

Combining the 3 will give you a relatively good coverage on simple cases but unfortunately the topic of accurately determining combat is very complex. Imagine a player casts wind strike on you in the wilderness (he's interacting with you in this case) then quickly eats food (he's no longer interacting with you), the health bar is not visible but there is a projectile launched with your player as its target, if it hits then your health bar will be visible but if it splashes, your health bar will not be visible, the player is not interacting with you because the interaction was interrupted by eating and there is no projectile launched towards your player. But you are in combat with him for 8 seconds or whatever the timer is. The only way to determine if you really are under attack 100% is by caching this state once you detect an attacker and clearing the cache once the combat timer is up so if you want the maximum precision (eg pking scripts) you will have to multithread your script and dedicate a whole thread to managing the combat state and determining the opponent.

Edited by Token
  • Like 6
Posted
6 minutes ago, Token said:

script.players.getAll().stream().filter(x -> script.myPlayer().equals(x.getInteracting())).findFirst().isPresent(); 
// there is a player ATTEMPTING to attack, not necessarily already attacked

script.myPlayer().isHitBarVisible(); 
// only works if the attack triggered health bar display (not a splashed spell)

script.projectiles().getAll().stream().filter(x -> script.myPlayer().equals(x.getTargetEntity())).findFirst().isPresent();
// a ranged/magic attack was launched against the bot

Combining the 3 will give you a relatively good coverage on simple cases but unfortunately the topic of accurately determining combat is very complex. Imagine a player casts wind strike on you in the wilderness (he's interacting with you in this case) then quickly eats food (he's no longer interacting with you), the health bar is not visible but there is a projectile launched with your player as its target, if it hits then your health bar will be visible but if it splashes, your health bar will not be visible, the player is not interacting with you because the interaction was interrupted by eating and there is no projectile launched towards your player. But you are in combat with him for 8 seconds or whatever the timer is. The only way to determine if you really are under attack 100% is by caching this state once you detect an attacker and clearing the cache once the combat timer is up so if you want the maximum precision (eg pking scripts) you will have to multithread your script and dedicate a whole thread to managing the combat state and determining the opponent.

Thank you mr token <3

Posted
57 minutes ago, Token said:

script.players.getAll().stream().filter(x -> script.myPlayer().equals(x.getInteracting())).findFirst().isPresent(); 
// there is a player ATTEMPTING to attack, not necessarily already attacked

script.myPlayer().isHitBarVisible(); 
// only works if the attack triggered health bar display (not a splashed spell)

script.projectiles.getAll().stream().filter(x -> script.myPlayer().equals(x.getTargetEntity())).findFirst().isPresent();
// a ranged/magic attack was launched against the bot

Combining the 3 will give you a relatively good coverage on simple cases but unfortunately the topic of accurately determining combat is very complex. Imagine a player casts wind strike on you in the wilderness (he's interacting with you in this case) then quickly eats food (he's no longer interacting with you), the health bar is not visible but there is a projectile launched with your player as its target, if it hits then your health bar will be visible but if it splashes, your health bar will not be visible, the player is not interacting with you because the interaction was interrupted by eating and there is no projectile launched towards your player. But you are in combat with him for 8 seconds or whatever the timer is. The only way to determine if you really are under attack 100% is by caching this state once you detect an attacker and clearing the cache once the combat timer is up so if you want the maximum precision (eg pking scripts) you will have to multithread your script and dedicate a whole thread to managing the combat state and determining the opponent.

The master has answered

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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