Move to new sign-based threat refuge system

This commit is contained in:
Tanner committed 2026-11-10 23:10:37 +00:00
1 parent 038fc503f0
commit 586772cb3b
6 files changed
+418 -195

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
import unittest
from munch import Munch
from mosfet.info import blocks
from mosfet.world import World
def wall_sign_state(facing):
data = blocks.MCD_BLOCKS['oak_wall_sign']
return next(
state_id for state_id in range(data['minStateId'], data['maxStateId'] + 1)
if blocks.PROPS[state_id].get('facing') == facing and
blocks.PROPS[state_id].get('waterlogged') == 'false'
)
class FakeChunks:
def __init__(self, block_entities=None, states=None):
self.block_entities = block_entities or {}
self.states = states or {}
def get_block_at(self, *position):
return self.states.get(tuple(position), blocks.AIR)
class SafetySignTests(unittest.TestCase):
def test_safety_and_refuge_signs_follow_group_and_individual_addresses(self):
group_sign = (0, 64, 0)
individual_sign = (3, 64, 0)
world = World(Munch(chunks=FakeChunks(
block_entities={
group_sign: {'data': {'front_text': {'messages': [
'mosfet', 'safety', '', '',
]}}},
individual_sign: {'data': {'back_text': {'messages': [
'mosfet2 refuge', '', '', '',
]}}},
},
states={
group_sign: wall_sign_state('north'),
individual_sign: wall_sign_state('east'),
})))
bot2 = world.find_safety_signs((0, 64, 0), 'mosfet2', 10)
self.assertEqual([sign['position'] for sign in bot2], [
group_sign, individual_sign,
])
self.assertEqual(bot2[1]['normal'], (-1.0, 0.0, 0.0))
self.assertEqual([sign['position'] for sign in world.find_safety_signs(
(0, 64, 0), 'mosfet1', 10)], [group_sign])
self.assertEqual(world.find_safety_signs(
(0, 64, 0), 'mosfet4', 10), [])
def test_safety_openings_try_text_facing_standable_positions_first(self):
sign_pos = (0, 64, 0)
front = (0, 64, -1)
side = (1, 64, 0)
states = {
sign_pos: wall_sign_state('north'),
(front[0], front[1] - 1, front[2]): blocks.STONE,
(side[0], side[1] - 1, side[2]): blocks.STONE,
}
world = World(Munch(chunks=FakeChunks(states=states)))
sign = {'position': sign_pos, 'normal': (0.0, 0.0, -1.0)}
openings = world.find_safety_openings(sign)
self.assertEqual(openings[0], front)
self.assertIn(side, openings)
if __name__ == '__main__':
unittest.main()
+198 -24
View File
@@ -6,7 +6,7 @@ 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.jobs.check_threats import ThreatMonitor
from mosfet.world import World
@@ -18,6 +18,22 @@ class FakeChunks:
return blocks.STONE if (x, y, z) in self.solid else blocks.AIR
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
class FakeChat:
def __init__(self):
self.handler = None
@@ -50,38 +66,100 @@ class TestThreatDetection(unittest.TestCase):
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):
def test_waiting_monitor_sees_hostiles_even_when_surface_filter_hides_them(self):
refuge = (5, 64, 0)
pillager = Munch(entity_id=12, type=mobs.MOBS[
'minecraft:pillager']['protocol_id'], x=8.0, y=64.0, z=0.0)
class ShelteredWorld:
@staticmethod
def find_monsters(_center, _distance):
return [pillager]
@staticmethod
def find_threats(*_args):
return []
monitor = ThreatMonitor(Munch(
world=ShelteredWorld(), pos=refuge, job=None))
monitor.safety = refuge
monitor.safe_sign = {'position': refuge}
monitor.state = monitor.waiting
with patch('mosfet.jobs.check_threats.time.monotonic',
return_value=1.0):
monitor.tick()
self.assertIs(monitor.state.__func__, monitor.waiting.__func__)
self.assertIsNone(monitor.safe_since)
self.assertEqual(monitor.safety, refuge)
def test_threat_log_decodes_mob_type_and_position(self):
pillager_id = mobs.MOBS['minecraft:pillager']['protocol_id']
description = ThreatMonitor._describe_threat(Munch(
entity_id=15, type=pillager_id,
x=-130.5, y=68.0, z=-241.5))
self.assertEqual(description,
'pillager#15 at (-130.5, 68.0, -241.5)')
def test_monitor_falls_back_to_reachable_clear_area_without_signs(self):
start = (0, 64, 0)
spot = (8, 64, 0)
threat = Munch(entity_id=7)
class SafeWorld:
def find_blocks_indexed(self, center, block_ids, distance=0):
@staticmethod
def find_safety_signs(*_args):
return []
def find_bed_areas(self, center, distance):
@staticmethod
def find_safety_openings(*_args):
return []
@staticmethod
def find_bed_areas(_center, _distance):
return [spot]
def find_threats(self, center, distance):
return []
@staticmethod
def find_threats(center, distance):
return [threat] if tuple(center) == start else []
def path_to_place(self, start, target):
return [start, target]
@staticmethod
def path_to_place(origin, target):
return [origin, 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)]
g = Munch(world=SafeWorld(), pos=start, path=[], look_at=None,
job=FakeJob())
monitor = ThreatMonitor(g)
state.find_safety()
with patch('mosfet.jobs.check_threats.time.monotonic',
return_value=0.0):
monitor.tick()
self.assertEqual(state.state, state.going_to_safety)
self.assertEqual(state.safety, spot)
self.assertFalse(state.external_safety)
self.assertEqual(monitor.state.__func__, monitor.evacuating.__func__)
self.assertEqual(monitor.safety, spot)
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_does_not_scan_without_an_active_job(self):
class IdleJob:
def idle(self):
pass
def __init__(self):
self.state = self.idle
class UnusedWorld:
@staticmethod
def find_threats(*_args):
raise AssertionError('idle bot should not scan for threats')
monitor = ThreatMonitor(Munch(
world=UnusedWorld(), pos=(0, 64, 0), job=IdleJob()))
monitor.tick()
self.assertIs(monitor.state.__func__, monitor.watching.__func__)
def test_threat_monitor_waits_for_valid_spawn_position(self):
class UnavailableWorld:
@@ -95,6 +173,98 @@ class TestThreatDetection(unittest.TestCase):
self.assertIs(monitor.state.__func__, monitor.watching.__func__)
def test_addressed_refuge_sign_is_trusted_and_has_no_line_of_sight_check(self):
start = (0, 64, 0)
refuge = (5, 64, 0)
threat = Munch(entity_id=9, x=3.5, y=64.0, z=0.5)
sign = {'position': (6, 64, 0), 'side': 'front'}
paths = []
class ShelterWorld:
@staticmethod
def find_threats(_center, _distance):
return [threat]
@staticmethod
def find_safety_signs(center, bot_name, distance):
self.assertEqual(center, start)
self.assertEqual(bot_name, 'mosfet1')
self.assertEqual(distance, ThreatMonitor.SAFETY_SEARCH_DISTANCE)
return [sign]
@staticmethod
def find_safety_openings(found_sign):
self.assertEqual(found_sign, sign)
return [refuge]
@staticmethod
def path_to_place(origin, target):
paths.append((tuple(origin), tuple(target)))
return [origin, target]
monitor = ThreatMonitor(Munch(
world=ShelterWorld(), name='mosfet1', pos=start,
path=[], look_at=None, job=FakeJob()))
with patch('mosfet.jobs.check_threats.time.monotonic', return_value=0.0):
monitor.tick()
self.assertEqual(monitor.safety, refuge)
self.assertIs(monitor.safe_sign, sign)
self.assertEqual(paths, [(start, refuge)])
# Even if the hostile remains nearby, it does not replace the trusted
# sign refuge merely due to distance or a line-of-sight calculation.
monitor.g.pos = refuge
with patch('mosfet.jobs.check_threats.time.monotonic', return_value=0.1):
monitor.tick()
self.assertIs(monitor.state.__func__, monitor.waiting.__func__)
with patch('mosfet.jobs.check_threats.time.monotonic', return_value=0.2):
monitor.tick()
self.assertEqual(monitor.safety, refuge)
self.assertEqual(paths, [(start, refuge)])
def test_damage_does_not_blacklist_a_trusted_sign_refuge(self):
start = (0, 64, 0)
refuge = (5, 64, 0)
now = [0.0]
sign = {'position': (6, 64, 0), 'side': 'front'}
class FakeWorld:
@staticmethod
def find_threats(center, _distance):
return [Munch(entity_id=11)] if tuple(center) == start else []
@staticmethod
def find_safety_signs(*_args):
return [sign]
@staticmethod
def find_safety_openings(_sign):
return [refuge]
@staticmethod
def path_to_place(origin, target):
return [origin, target]
g = Munch(
world=FakeWorld(), name='mosfet1', pos=start, path=[],
look_at=None, job=FakeJob(), health=20.0)
monitor = ThreatMonitor(g)
with patch('mosfet.jobs.check_threats.time.monotonic',
side_effect=lambda: now[0]):
monitor.tick()
g.pos = refuge
now[0] = 0.1
monitor.tick()
self.assertIs(monitor.state.__func__, monitor.waiting.__func__)
g.health = 14.0
now[0] = 0.6
monitor.tick()
self.assertEqual(monitor.safety, refuge)
self.assertIs(monitor.state.__func__, monitor.waiting.__func__)
self.assertEqual(g.path, [])
def test_threat_monitor_polls_every_half_second(self):
now = [0.0]
scans = []
@@ -106,7 +276,7 @@ class TestThreatDetection(unittest.TestCase):
return []
monitor = ThreatMonitor(Munch(
world=QuietWorld(), pos=(1, 64, 2), job=None))
world=QuietWorld(), pos=(1, 64, 2), job=FakeJob()))
with patch('mosfet.jobs.check_threats.time.monotonic',
side_effect=lambda: now[0]):
monitor.tick()
@@ -122,8 +292,8 @@ class TestThreatDetection(unittest.TestCase):
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)
sign = {'position': (5, 64, 1), 'side': 'front'}
now = [0.0]
class FakeWorld:
@@ -134,8 +304,12 @@ class TestThreatDetection(unittest.TestCase):
return []
@staticmethod
def find_blocks_indexed(_center, _block_ids, _distance):
return [marker]
def find_safety_signs(*_args):
return [sign]
@staticmethod
def find_safety_openings(_sign):
return [safe]
@staticmethod
def path_to_place(origin, target):