Apply sign work zones before candidate routing

This commit is contained in:
Tanner
2026-11-06 01:10:31 +00:00
parent a60d913c60
commit ab8f59a89b
11 changed files with 309 additions and 12 deletions
+8
View File
@@ -36,6 +36,14 @@ class GatherWoodPortTests(unittest.TestCase):
def find_trees(self, _position, _distance):
return [(0, 64, 0), (3, 64, 0)]
@staticmethod
def work_position_allowed(_position):
return True
def filter_work_positions(self, positions):
return [position for position in positions
if self.work_position_allowed(position)]
def block_at(self, x, y, z):
return (blocks.MCD_BLOCKS['crimson_stem']['minStateId']
if x == 0 else
+68
View File
@@ -0,0 +1,68 @@
import unittest
from munch import Munch
from mosfet.info import blocks
from mosfet.jobs.gather_crop import GatherCropStates
from mosfet.jobs.gather_wood import GatherWoodStates
class SignWorkTargetTests(unittest.TestCase):
def test_crop_search_filters_out_of_bounds_candidates_before_navigation(self):
outside = (10, 64, 0)
mature_wheat = blocks.MATURE_WHEAT_ID
crop_state = blocks.MCD_BLOCKS['wheat']['minStateId']
class FakeWorld:
def find_blocks_indexed(self, _center, _block_ids, _distance):
return [outside]
@staticmethod
def work_position_allowed(position):
return position != outside
def filter_work_positions(self, positions):
return [position for position in positions
if self.work_position_allowed(position)]
@staticmethod
def block_at(*_position):
return crop_state
state = GatherCropStates(Munch(world=FakeWorld(), pos=(0, 64, 0)))
state.find_new_crop()
self.assertIs(state.state.__func__, state.wait_for_crops.__func__)
self.assertEqual(state.bad_crops, [])
def test_tree_search_skips_out_of_bounds_tree_without_blacklisting_it(self):
outside = (10, 64, 0)
inside = (2, 64, 0)
oak_log = blocks.MCD_BLOCKS['oak_log']['minStateId']
class FakeWorld:
@staticmethod
def find_trees(_center, _distance):
return [outside, inside]
@staticmethod
def work_position_allowed(position):
return position != outside
def filter_work_positions(self, positions):
return [position for position in positions
if self.work_position_allowed(position)]
@staticmethod
def block_at(*_position):
return oak_log
state = GatherWoodStates(Munch(world=FakeWorld(), pos=(0, 64, 0)))
state.find_new_tree()
self.assertEqual(state.tree, inside)
self.assertEqual(state.type, 'oak')
self.assertEqual(state.bad_trees, [])
if __name__ == '__main__':
unittest.main()
+83
View File
@@ -1,4 +1,5 @@
import unittest
from unittest.mock import patch
from munch import Munch
from mosfet.commands import Commands
@@ -44,6 +45,88 @@ class SignReadingTests(unittest.TestCase):
signs = World(Munch(chunks=chunks)).find_signs((4, 65, -2), 1)
self.assertEqual(signs[0]['lines'], ('Farm', '', '', ''))
def test_group_and_individual_sign_addresses(self):
group_world = World(Munch(chunks=Munch(block_entities={
(0, 64, 0): {'data': {'front_text': {'messages': [
'mosfet', 'stay within25', 'square', '',
]}}},
}), pos=(0, 64, 0)))
for bot_name in ('mosfet1', 'mosfet2', 'mosfet3'):
self.assertEqual([p['shape'] for p in
group_world.sign_policies(bot_name)], ['square'])
self.assertEqual(group_world.sign_policies('mosfet4'), [])
individual_world = World(Munch(chunks=Munch(block_entities={
(1, 64, 0): {'data': {'front_text': {'messages': [
'mosfet2', 'stay within 10 circle', '', '',
]}}},
}), pos=(1, 64, 0)))
self.assertEqual([p['shape'] for p in
individual_world.sign_policies('mosfet2')], ['circle'])
self.assertEqual(individual_world.sign_policies('mosfet1'), [])
def test_nearest_sign_policy_overrides_farther_matching_sign(self):
chunks = Munch(block_entities={
(-138, 71, -268): {'data': {'front_text': {'messages': [
'mosfet1', 'stay within 25 square', '', '',
]}}},
(-158, 71, -248): {'data': {'front_text': {'messages': [
'mosfet1', 'stay within 6 squares', '', '',
]}}},
})
world = World(Munch(chunks=chunks, pos=(-158, 71, -248)))
self.assertEqual(world.sign_policies('mosfet1'), [{
'position': (-158, 71, -248), 'distance': 6,
'shape': 'square', 'side': 'front',
}])
def test_work_paths_obey_sign_boundary_but_cache_paths_do_not(self):
sign_data = {'front_text': {'messages': [
'mosfet1', 'stay within 25 square', '', '',
]}}
chunks = Munch(block_entities={(0, 64, 0): {'data': sign_data}})
work_machine = object()
cache_machine = object()
job = Munch(active_machine=work_machine,
cache_items_states=cache_machine)
world = World(Munch(chunks=chunks, job=job, name='mosfet1',
pos=(0, 64, 0)))
class FixedPathfinder:
@staticmethod
def astar(_start, _place):
return [(0, 64, 0), (20, 64, 0), (30, 64, 0)]
with patch('mosfet.world.path.Pathfinder', return_value=FixedPathfinder()):
self.assertIsNone(world.path_to_place((0, 64, 0), (30, 64, 0)))
job.active_machine = cache_machine
self.assertEqual(world.path_to_place(
(0, 64, 0), (30, 64, 0)),
[(0, 64, 0), (20, 64, 0), (30, 64, 0)])
policy = {'position': (0, 64, 0), 'distance': 25, 'shape': 'square'}
self.assertTrue(World._path_allowed_by_sign(
[(30, 64, 0), (27, 64, 0), (24, 64, 0)], policy))
self.assertFalse(World._path_allowed_by_sign(
[(30, 64, 0), (32, 64, 0), (24, 64, 0)], policy))
six_square = {'position': (-158, 71, -248),
'distance': 6, 'shape': 'square'}
six_circle = dict(six_square, shape='circle')
crop = (-162, 68, -247)
self.assertEqual(World._sign_policy_distance(crop, six_square), 4)
self.assertAlmostEqual(World._sign_policy_distance(crop, six_circle),
(17 ** 0.5))
four_square = {'position': (0, 64, 0),
'distance': 4, 'shape': 'square'}
self.assertEqual(World._sign_policy_distance((4, 70, 4), four_square), 4)
self.assertTrue(World._inside_sign_policy((4, 70, 4), four_square))
self.assertFalse(World._inside_sign_policy((5, 64, 4), four_square))
same_xz_far_y = (-158, -20, -248)
self.assertEqual(
World._sign_policy_distance(same_xz_far_y, six_square), 0)
def test_signs_command_replies_with_nearby_text(self):
class FakeChat:
def __init__(self):