Keep the current attack target regardless of invisibility
This commit is contained in:
@@ -588,21 +588,38 @@ impl Mob {
|
||||
if p.creative || p.allies.contains(&self.id) {
|
||||
return false;
|
||||
}
|
||||
self.player_in_range(w, range, check_sight)
|
||||
self.player_in_range(w, range, check_sight, true)
|
||||
}
|
||||
/// forCombat().ignoreInvisibilityTesting() for the player: the check for
|
||||
/// a target the mob already has, which ignores the visibility scaling.
|
||||
pub fn can_keep_target_player(&mut self, w: &MobWorld, range: f64) -> bool {
|
||||
let Some(p) = &w.player else { return false };
|
||||
if p.creative || p.allies.contains(&self.id) {
|
||||
return false;
|
||||
}
|
||||
self.player_in_range(w, range, true, false)
|
||||
}
|
||||
/// TargetingConditions.forNonCombat().range(range).test for the player
|
||||
/// (seen by anyone, within range scaled by visibility, in sight).
|
||||
pub fn can_see_player(&mut self, w: &MobWorld, range: f64) -> bool {
|
||||
self.player_in_range(w, range, true)
|
||||
self.player_in_range(w, range, true, true)
|
||||
}
|
||||
fn player_in_range(&mut self, w: &MobWorld, range: f64, check_sight: bool) -> bool {
|
||||
/// `scale` is `TargetingConditions.testInvisible`: scale the range by
|
||||
/// the player's visibility.
|
||||
fn player_in_range(
|
||||
&mut self,
|
||||
w: &MobWorld,
|
||||
range: f64,
|
||||
check_sight: bool,
|
||||
scale: bool,
|
||||
) -> bool {
|
||||
let Some(p) = &w.player else { return false };
|
||||
if !p.alive || p.spectator {
|
||||
return false;
|
||||
}
|
||||
// LivingEntity.getVisibilityPercent
|
||||
let mut visibility = p.visibility_percent();
|
||||
if let Some(head) = &p.head {
|
||||
let mut visibility = if scale { p.visibility_percent() } else { 1.0 };
|
||||
if let Some(head) = p.head.as_ref().filter(|_| scale) {
|
||||
let skull = match self.species() {
|
||||
Species::Zombie | Species::Husk | Species::Drowned | Species::ZombieVillager => {
|
||||
"minecraft:zombie_head"
|
||||
|
||||
@@ -359,11 +359,14 @@ fn can_attack(m: &Mob, w: &MobWorld, who: Who) -> bool {
|
||||
/// `Sensor.isEntityAttackable`: `TargetingConditions.forCombat()` within the
|
||||
/// follow range, in sight.
|
||||
fn attackable(m: &mut Mob, w: &MobWorld, who: Who) -> bool {
|
||||
// The current ATTACK_TARGET is tested with ignoreInvisibilityTesting.
|
||||
let current = mem_ref(m).attack_target.value() == Some(who);
|
||||
let range = m.attrs.follow_range;
|
||||
if !can_attack(m, w, who) {
|
||||
return false;
|
||||
}
|
||||
match who {
|
||||
Who::Player if current => m.can_keep_target_player(w, range),
|
||||
Who::Player => m.can_target_player(w, range, true),
|
||||
Who::Mob(id) => {
|
||||
let Some(s) = w.seen(id) else { return false };
|
||||
|
||||
Reference in New Issue
Block a user