Jump to content

How to tell if player is attacking me


k9thebeast

Recommended Posts

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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