diff --git a/mosfet/jobs/check_threats.py b/mosfet/jobs/check_threats.py index cf5b6a0..3386290 100644 --- a/mosfet/jobs/check_threats.py +++ b/mosfet/jobs/check_threats.py @@ -52,6 +52,23 @@ class ThreatMonitor: return finder(position, distance) return self.g.world.find_threats(position, distance) + def _visible_hostiles(self, position, distance): + threats = self._nearby_hostiles(position, distance) + line_of_sight = getattr( + self.g.world, '_has_cache_line_of_sight', None) + if not callable(line_of_sight): + return threats + + player = self.g.pos + if hasattr(player, 'x'): + px, py, pz = player.x, player.y, player.z + else: + px, py, pz = player + target = (px, py + 1.62, pz) + target_block = utils.pint((px, py, pz)) + return [mob for mob in threats if line_of_sight( + (mob.x, mob.y + 1.5, mob.z), target, target_block)] + @staticmethod def _describe_threat(threat): mob_name = mobs.MOB_NAMES.get( @@ -111,7 +128,7 @@ class ThreatMonitor: if now < self.next_scan_at: return self.next_scan_at = now + self.CHECK_INTERVAL - threats = self._nearby_hostiles( + threats = self._visible_hostiles( utils.pint(self.g.pos), self.THREAT_DISTANCE) if not threats: return diff --git a/tests/test_threat_detection.py b/tests/test_threat_detection.py index 02a2267..0652f61 100644 --- a/tests/test_threat_detection.py +++ b/tests/test_threat_detection.py @@ -141,6 +141,31 @@ class TestThreatDetection(unittest.TestCase): self.assertEqual(monitor.safety, spot) self.assertEqual(g.path[-1], spot) + def test_watching_ignores_hostile_mobs_without_line_of_sight(self): + mob = Munch(entity_id=13, type=mobs.MOBS[ + 'minecraft:pillager']['protocol_id'], + x=3.0, y=64.0, z=0.0) + + class OccludedWorld: + @staticmethod + def find_monsters(_center, _distance): + return [mob] + + @staticmethod + def _has_cache_line_of_sight(_start, _end, _target): + return False + + job = FakeJob() + monitor = ThreatMonitor(Munch( + world=OccludedWorld(), pos=(0.5, 64.0, 0.5), job=job)) + + with patch('mosfet.jobs.check_threats.time.monotonic', + return_value=0.0): + monitor.tick() + + self.assertIs(monitor.state.__func__, monitor.watching.__func__) + self.assertEqual(job.stop_calls, 0) + def test_threat_monitor_does_not_scan_without_an_active_job(self): class IdleJob: def idle(self):