Files
minecraft-bot/tests/test_signs.py
T

274 lines
11 KiB
Python

import unittest
from unittest.mock import patch
from munch import Munch
from mosfet.commands import Commands
from mosfet.info import blocks
from mosfet.protocol.managers import EntityManager, WorldState
from mosfet.world import World
class SignReadingTests(unittest.TestCase):
def test_world_decodes_front_and_back_sign_components(self):
chunks = Munch(block_entities={
(2, 64, 3): {
'type': 7,
'data': {
'front_text': {'messages': [
'{"text":"Hello","extra":[{"text":" world"}]}',
'{"text":"line two"}', '', ''],
},
'back_text': {'messages': [
'{"text":"back"}', '', '', ''],
},
},
},
})
signs = World(Munch(chunks=chunks)).find_signs((0, 64, 0), 10)
self.assertEqual(signs, [
{'position': (2, 64, 3), 'side': 'front',
'lines': ('Hello world', 'line two', '', '')},
{'position': (2, 64, 3), 'side': 'back',
'lines': ('back', '', '', '')},
])
def test_block_entity_update_is_cached_for_sign_search(self):
chunks = WorldState()
manager = object.__new__(EntityManager)
manager.g = Munch(chunks=chunks)
manager.handle_block_entity_data(Munch(
location=Munch(x=4, y=65, z=-2),
block_entity_type=7,
data={'front_text': {'messages': ['{"text":"Farm"}', '', '', '']}},
))
signs = World(Munch(chunks=chunks)).find_signs((4, 65, -2), 1)
self.assertEqual(signs[0]['lines'], ('Farm', '', '', ''))
def test_broken_sign_block_update_removes_cached_sign_entity(self):
position = (4, 65, -2)
sign_state = blocks.MCD_BLOCKS['oak_wall_sign']['minStateId']
class FakeColumn:
def __init__(self):
self.states = {}
def get_block_at(self, x, y, z):
return self.states.get((x, y, z), blocks.AIR)
def set_block_at(self, x, y, z, state_id):
previous = self.get_block_at(x, y, z)
self.states[(x, y, z)] = state_id
return previous
chunks = WorldState()
chunks.chunks[(0, -1)] = FakeColumn()
chunks.set_block_at(*position, sign_state)
chunks.set_block_entity(position, 7, {
'front_text': {'messages': ['mosfet1 stay', 'within 8 square', '', '']},
})
world = World(Munch(chunks=chunks))
self.assertEqual(len(world.find_signs(position, 1)), 1)
chunks.set_block_at(*position, blocks.AIR)
self.assertNotIn(position, chunks.block_entities)
self.assertEqual(world.find_signs(position, 1), [])
def test_sign_search_ignores_stale_block_entity_for_non_sign_block(self):
position = (4, 65, -2)
chunks = Munch(
block_entities={position: {'data': {'front_text': {
'messages': ['mosfet1 stay', 'within 8 square', '', ''],
}}}},
get_block_at=lambda *_position: blocks.AIR,
)
self.assertEqual(World(Munch(chunks=chunks)).find_signs(position, 1), [])
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_num in range(1, 10):
bot_name = 'mosfet{}'.format(bot_num)
self.assertEqual([p['shape'] for p in
group_world.sign_policies(bot_name)], ['square'])
self.assertEqual(group_world.sign_policies('mosfet10'), [])
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_sand_origin_signs_match_bot_and_numbered_group_addresses(self):
chunks = Munch(block_entities={
(1, 64, 0): {'data': {'front_text': {'messages': [
'mosfet', 'sand origin', '', '',
]}}},
(3, 64, 0): {'data': {'back_text': {'messages': [
'mosfet1 sand', 'origin', '', '',
]}}},
(2, 64, 0): {'data': {'front_text': {'messages': [
'mosfet2 sand origin', '', '', '',
]}}},
})
world = World(Munch(chunks=chunks))
origins = world.find_sand_origins((0, 64, 0), 'mosfet1')
self.assertEqual([origin['position'] for origin in origins], [
(1, 64, 0), (3, 64, 0),
])
self.assertEqual(
[origin['position'] for origin in
world.find_sand_origins((0, 64, 0), 'mosfet2')],
[(1, 64, 0), (2, 64, 0)],
)
self.assertEqual(
[origin['position'] for origin in
world.find_sand_origins((0, 64, 0), 'mosfet9')],
[(1, 64, 0)],
)
self.assertEqual(
world.find_sand_origins((0, 64, 0), 'mosfet10'), [])
def test_supply_sign_group_address_includes_bot_nine_only(self):
sign_pos = (0, 64, 0)
chest_pos = (1, 64, 0)
sign_state = blocks.MCD_BLOCKS['oak_wall_sign']['minStateId']
chest_state = next(
state for state in blocks.CHEST_IDS
if blocks.PROPS[state].get('type') == 'single')
block_states = {sign_pos: sign_state, chest_pos: chest_state}
chunks = Munch(
block_entities={sign_pos: {'data': {'front_text': {
'messages': ['mosfet supplies', '', '', ''],
}}}},
index={},
get_block_at=lambda *pos: block_states.get(tuple(pos), blocks.AIR),
)
world = World(Munch(chunks=chunks))
supplies = world.find_supply_chests(sign_pos, 'mosfet9', 10)
self.assertEqual(len(supplies), 1)
self.assertEqual(supplies[0]['positions'], (chest_pos,))
self.assertEqual(world.find_supply_chests(
sign_pos, 'mosfet10', 10), [])
def test_sign_policies_read_any_distance_within_loaded_signs(self):
position = (100, 64, 0)
chunks = Munch(block_entities={position: {
'data': {'front_text': {'messages': [
'mosfet2', 'stay within 35 square', '', '',
]}},
}})
world = World(Munch(chunks=chunks, pos=(0, 64, 0)))
policies = world.sign_policies('mosfet2')
self.assertEqual(policies, [{
'position': position, 'distance': 35,
'shape': 'square', 'side': 'front',
}])
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_check_destination_only_but_cache_paths_bypass_signs(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)))
route = [(0, 64, 0), (50, 64, 0), (24, 64, 0)]
astar_calls = []
class FixedPathfinder:
@staticmethod
def astar(start, place):
astar_calls.append((start, place))
return route
with patch('mosfet.world.path.Pathfinder', return_value=FixedPathfinder()):
# The work target is inside, so even a route well outside is okay.
self.assertEqual(world.path_to_place((0, 64, 0), (24, 64, 0)),
route)
# An out-of-bounds destination is rejected before pathfinding.
self.assertIsNone(world.path_to_place((0, 64, 0), (30, 64, 0)))
self.assertEqual(len(astar_calls), 1)
# Support/cache paths do not use work-sign limits.
job.active_machine = cache_machine
self.assertEqual(world.path_to_place(
(0, 64, 0), (30, 64, 0)), route)
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):
self.sent = []
def set_handler(self, handler):
self.handler = handler
def send(self, message):
self.sent.append(message)
chat = FakeChat()
world = Munch(find_signs=lambda _position, _distance: [{
'position': (1, 64, 2), 'side': 'front',
'lines': ('Hello', 'world', '', ''),
}])
commands = Commands(Munch(
name='mosfet1', chat=chat, world=world, pos=(0.5, 64.0, 0.5)))
commands.handle_chat(('CHAT', 'tester', 'tester: !signs'))
self.assertEqual(chat.sent, ['front (1, 64, 2): Hello | world'])
if __name__ == '__main__':
unittest.main()