211 lines
6.7 KiB
Python
211 lines
6.7 KiB
Python
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from munch import Munch
|
|
|
|
from mosfet.commands import Commands
|
|
from mosfet.info import blocks, mobs
|
|
from mosfet.job import JobStates
|
|
from mosfet.jobs.check_threats import CheckThreatsStates, ThreatMonitor
|
|
from mosfet.world import World
|
|
|
|
|
|
class FakeChunks:
|
|
def __init__(self, solid=()):
|
|
self.solid = set(solid)
|
|
|
|
def get_block_at(self, x, y, z):
|
|
return blocks.STONE if (x, y, z) in self.solid else blocks.AIR
|
|
|
|
|
|
class FakeChat:
|
|
def __init__(self):
|
|
self.handler = None
|
|
self.messages = []
|
|
|
|
def set_handler(self, handler):
|
|
self.handler = handler
|
|
|
|
def send(self, message):
|
|
self.messages.append(message)
|
|
|
|
|
|
class TestThreatDetection(unittest.TestCase):
|
|
def test_find_threats_filters_passive_and_distant_entities(self):
|
|
zombie = mobs.MOBS['minecraft:zombie']['protocol_id']
|
|
villager = mobs.MOBS['minecraft:villager']['protocol_id']
|
|
world = World(Munch(mobs={
|
|
1: Munch(entity_id=1, type=zombie, x=3.2, y=64, z=0),
|
|
2: Munch(entity_id=2, type=villager, x=2, y=64, z=0),
|
|
3: Munch(entity_id=3, type=zombie, x=30, y=64, z=0),
|
|
}))
|
|
world.check_air_column = lambda pos, distance: True
|
|
|
|
result = world.find_threats((0, 64, 0), 12)
|
|
|
|
self.assertEqual([threat.entity_id for threat in result], [1])
|
|
|
|
def test_air_column_excludes_mobs_under_a_solid_roof(self):
|
|
world = World(Munch(chunks=FakeChunks(solid={(0, 67, 0)})))
|
|
self.assertFalse(world.check_air_column((0, 64, 0), 5))
|
|
self.assertTrue(world.check_air_column((2, 64, 0), 5))
|
|
|
|
def test_threat_state_falls_back_to_reachable_clear_area(self):
|
|
spot = (8, 64, 0)
|
|
|
|
class SafeWorld:
|
|
def find_blocks_indexed(self, center, block_ids, distance=0):
|
|
return []
|
|
|
|
def find_bed_areas(self, center, distance):
|
|
return [spot]
|
|
|
|
def find_threats(self, center, distance):
|
|
return []
|
|
|
|
def path_to_place(self, start, target):
|
|
return [start, target]
|
|
|
|
g = Munch(world=SafeWorld(), pos=(0, 64, 0), path=[], look_at=None)
|
|
state = CheckThreatsStates(g)
|
|
state.state = state.find_safety
|
|
state.threats = [Munch(entity_id=7)]
|
|
|
|
state.find_safety()
|
|
|
|
self.assertEqual(state.state, state.going_to_safety)
|
|
self.assertEqual(state.safety, spot)
|
|
self.assertFalse(state.external_safety)
|
|
self.assertEqual(g.path[-1], spot)
|
|
|
|
g.pos = spot
|
|
state.going_to_safety()
|
|
self.assertEqual(state.state, state.wait)
|
|
self.assertEqual(state.wait_time, 3)
|
|
|
|
def test_threat_monitor_waits_for_valid_spawn_position(self):
|
|
class UnavailableWorld:
|
|
@staticmethod
|
|
def find_threats(*_args):
|
|
raise AssertionError('world queried before player position')
|
|
|
|
monitor = ThreatMonitor(Munch(world=UnavailableWorld(), pos=False))
|
|
|
|
monitor.tick()
|
|
|
|
self.assertIs(monitor.state.__func__, monitor.watching.__func__)
|
|
|
|
def test_threat_monitor_polls_every_half_second(self):
|
|
now = [0.0]
|
|
scans = []
|
|
|
|
class QuietWorld:
|
|
@staticmethod
|
|
def find_threats(center, distance):
|
|
scans.append((center, distance))
|
|
return []
|
|
|
|
monitor = ThreatMonitor(Munch(
|
|
world=QuietWorld(), pos=(1, 64, 2), job=None))
|
|
with patch('mosfet.jobs.check_threats.time.monotonic',
|
|
side_effect=lambda: now[0]):
|
|
monitor.tick()
|
|
now[0] = 0.49
|
|
monitor.tick()
|
|
self.assertEqual(len(scans), 1)
|
|
now[0] = 0.5
|
|
monitor.tick()
|
|
|
|
self.assertEqual(len(scans), 2)
|
|
self.assertEqual(scans, [((1, 64, 2), monitor.THREAT_DISTANCE)] * 2)
|
|
|
|
def test_monitor_evacuates_waits_for_clear_area_then_resumes_job(self):
|
|
threat = Munch(entity_id=7)
|
|
start = (0, 64, 0)
|
|
marker = (5, 63, 0)
|
|
safe = (5, 64, 0)
|
|
now = [0.0]
|
|
|
|
class FakeWorld:
|
|
@staticmethod
|
|
def find_threats(center, distance):
|
|
if tuple(center) == start and distance == ThreatMonitor.THREAT_DISTANCE:
|
|
return [threat]
|
|
return []
|
|
|
|
@staticmethod
|
|
def find_blocks_indexed(_center, _block_ids, _distance):
|
|
return [marker]
|
|
|
|
@staticmethod
|
|
def path_to_place(origin, target):
|
|
return [origin, target]
|
|
|
|
class FakeJob:
|
|
def idle(self):
|
|
pass
|
|
|
|
def farm_wart(self):
|
|
pass
|
|
|
|
def __init__(self):
|
|
self.state = self.farm_wart
|
|
self.stop_calls = 0
|
|
|
|
def stop(self):
|
|
self.stop_calls += 1
|
|
self.state = self.idle
|
|
|
|
job = FakeJob()
|
|
g = Munch(world=FakeWorld(), pos=start, path=[], look_at=None,
|
|
job=job)
|
|
monitor = ThreatMonitor(g)
|
|
with patch('mosfet.jobs.check_threats.time.monotonic',
|
|
side_effect=lambda: now[0]):
|
|
monitor.tick()
|
|
self.assertIs(monitor.state.__func__, monitor.evacuating.__func__)
|
|
self.assertTrue(monitor.suppress_job)
|
|
self.assertEqual(job.stop_calls, 1)
|
|
self.assertEqual(g.path[-1], safe)
|
|
|
|
g.pos = safe
|
|
now[0] = 0.1
|
|
monitor.tick()
|
|
self.assertIs(monitor.state.__func__, monitor.waiting.__func__)
|
|
monitor.tick()
|
|
now[0] = 10.2
|
|
monitor.tick()
|
|
self.assertIs(monitor.state.__func__, monitor.resuming.__func__)
|
|
now[0] = 10.3
|
|
monitor.tick()
|
|
|
|
self.assertIs(monitor.state.__func__, monitor.watching.__func__)
|
|
self.assertIs(job.state.__func__, job.farm_wart.__func__)
|
|
self.assertFalse(monitor.suppress_job)
|
|
|
|
def test_loiter_leaves_threat_checks_to_background_monitor(self):
|
|
sleep = Munch(silent=False)
|
|
eat = object()
|
|
job = JobStates.__new__(JobStates)
|
|
job.sleep_with_bed_states = sleep
|
|
job.eat_food_states = eat
|
|
|
|
self.assertEqual(JobStates.loiter(job), [sleep, eat])
|
|
self.assertTrue(sleep.silent)
|
|
|
|
def test_loiter_command_is_no_longer_rejected(self):
|
|
def loiter():
|
|
return []
|
|
|
|
chat = FakeChat()
|
|
job = Munch(loiter=loiter, state=None)
|
|
commands = Commands(Munch(name='mosfet1', chat=chat, job=job))
|
|
commands.handle_chat(('CHAT', 'player-uuid', 'tanner: 1loiter'))
|
|
|
|
self.assertIs(job.state, loiter)
|
|
self.assertEqual(chat.messages, ['ok'])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|