Merge pull request #2223 from kacpherek/fix/invisibility-mob-detection

🤖 Agent: Merged by hand by Ludovic's AI agent (orchestrator) after the contribution gate. rdbtCVS's change request is addressed at this head; a re-review was asked, with no new objection since. Proven in wave 5's union run (cg21 union 65b0cb252 (main c1ebf5d1b + 72 kacpherek PRs) playhorizon 94/95, use_item_on fixed (#2126 523280e78).
This commit is contained in:
Ludovic Marie
2026-10-27 12:17:11 +01:00
committed by GitHub
8 changed files with 43 additions and 12 deletions
@@ -1050,6 +1050,7 @@ pub fn main() -> horizon_client::Result<()> {
off_hand: None,
head: None,
armor_cover: 0.0,
invisible: false,
spectator: false,
gold_armor: false,
hurt_by: None,
@@ -199,6 +199,7 @@ impl Game<'_> {
off_hand: None,
head: self.armor[0].as_ref().map(|s| s.item.clone()),
armor_cover: self.armor.iter().filter(|a| a.is_some()).count() as f32 / 4.0,
invisible: self.potions.effects.get("invisibility").is_some(),
spectator: self.spectator(),
gold_armor: self.wearing_gold(),
hurt_by: self.player_hurt_by(),
@@ -358,6 +358,7 @@ mod tests {
off_hand: None,
head: None,
armor_cover: 0.0,
invisible: false,
spectator: false,
gold_armor: false,
hurt_by: None,
+13
View File
@@ -1577,6 +1577,8 @@ pub struct PlayerView {
pub off_hand: Option<Arc<str>>,
pub head: Option<Arc<str>>,
pub armor_cover: f32,
/// The Invisibility effect (`isInvisible`).
pub invisible: bool,
pub spectator: bool,
/// `PiglinAi.isWearingSafeArmor`: some golden armour.
pub gold_armor: bool,
@@ -1602,6 +1604,17 @@ pub struct PlayerView {
pub passenger: bool,
}
impl PlayerView {
/// `LivingEntity.getVisibilityPercent` without the worn-skull scaling.
pub fn visibility_percent(&self) -> f64 {
let mut visibility = 1.0;
if self.sneaking {
visibility *= 0.8;
}
if self.invisible {
visibility *= 0.7 * f64::from(self.armor_cover).max(0.1);
}
visibility
}
/// `LivingEntity.hasEffect`, by id without namespace.
pub fn has_effect(&self, effect: &str) -> bool {
self.effects.contains(&effect)
@@ -625,8 +625,7 @@ fn targetable(m: &mut Mob, w: &MobWorld, who: Who) -> bool {
// TargetingConditions.forNonCombat(): in range (scaled by the
// player's visibility, at least 2) and in sight.
let Some(p) = &w.player else { return false };
let visibility = if p.sneaking { 0.8 } else { 1.0 };
let r = (range * visibility).max(2.0);
let r = (range * p.visibility_percent()).max(2.0);
p.alive && !p.spectator && m.distance_sqr(p.position) <= r * r && m.sees(w, who)
}
Who::Mob(id) => {
+22 -8
View File
@@ -588,24 +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 = 1.0;
if p.sneaking {
visibility *= 0.8;
}
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"
@@ -364,8 +364,7 @@ impl Goal for GhastTempt {
let Some(p) = &cx.w.player else { return false };
// getNearestPlayer(TargetingConditions.forNonCombat()
// .ignoreLineOfSight().range(TEMPT_RANGE 16)): creative players too.
let visibility = if p.sneaking { 0.8 } else { 1.0 };
let range = (16.0_f64 * visibility).max(2.0);
let range = (16.0_f64 * p.visibility_percent()).max(2.0);
p.alive
&& !p.spectator
&& (Self::wants(m, &p.main_hand) || Self::wants(m, &p.off_hand))
@@ -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 };