Delete job related tests
This commit is contained in:
1 parent
7d6fc8aa81
commit
971b98acde
21 files changed
+261
-3152
No files matched your search
@@ -1,105 +0,0 @@
|
||||
import unittest
|
||||
from munch import Munch
|
||||
|
||||
from mosfet.bot import update_yaw
|
||||
from mosfet.info import blocks
|
||||
from mosfet.jobs.sleep_with_bed import SleepWithBedStates
|
||||
from mosfet.vector import Vector3D
|
||||
from mosfet.world import BedArea, World
|
||||
|
||||
|
||||
class BedLaneTests(unittest.TestCase):
|
||||
def make_world(self, supported):
|
||||
class GridChunks:
|
||||
def get_block_at(self, x, y, z):
|
||||
if y == 63 and (x, y, z) in supported:
|
||||
return blocks.STONE
|
||||
return blocks.AIR
|
||||
|
||||
return World(Munch(chunks=GridChunks(), mobs={}))
|
||||
|
||||
def test_bed_areas_require_standing_and_two_bed_tiles(self):
|
||||
supported = {(x, 63, 0) for x in (1, 2, 3)}
|
||||
world = self.make_world(supported)
|
||||
|
||||
layouts = list(world.find_bed_areas(
|
||||
(0, 64, 0), 4, include_layout=True))
|
||||
|
||||
self.assertIn(BedArea(
|
||||
foot=(2, 64, 0), head=(3, 64, 0),
|
||||
opening=(1, 64, 0), facing=(1, 0, 0)), layouts)
|
||||
self.assertIn((1, 64, 0), list(world.find_bed_areas((0, 64, 0), 4)))
|
||||
|
||||
def test_bed_lane_is_rejected_without_third_supported_block(self):
|
||||
supported = {(1, 63, 0), (2, 63, 0)}
|
||||
world = self.make_world(supported)
|
||||
|
||||
layouts = list(world.find_bed_areas(
|
||||
(0, 64, 0), 4, include_layout=True))
|
||||
|
||||
self.assertFalse(any(
|
||||
layout.opening == (1, 64, 0) for layout in layouts))
|
||||
|
||||
def test_far_end_aim_avoids_movement_yaw_dead_zone(self):
|
||||
yaw = Munch(yaw=0.0)
|
||||
update_yaw(yaw, Vector3D((-0.5, 0, 0)))
|
||||
self.assertEqual(yaw.yaw, 0.0)
|
||||
|
||||
for _ in range(4):
|
||||
update_yaw(yaw, Vector3D((-1.5, 0, 0)))
|
||||
self.assertAlmostEqual(yaw.yaw, -90.0)
|
||||
|
||||
def test_sleep_job_navigates_to_lane_end_and_aligns_before_placing(self):
|
||||
layout = BedArea(
|
||||
foot=(2, 64, 0), head=(3, 64, 0),
|
||||
opening=(1, 64, 0), facing=(1, 0, 0))
|
||||
|
||||
class PlacementWorld:
|
||||
def find_bed_areas(self, center, distance, include_layout=False):
|
||||
self.search = (center, distance, include_layout)
|
||||
return [layout]
|
||||
|
||||
def path_to_place(self, start, destination):
|
||||
self.path_call = (start, destination)
|
||||
return [destination]
|
||||
|
||||
class FakeGame:
|
||||
def __init__(self):
|
||||
self.placements = []
|
||||
|
||||
def place_block(self, location, face, hand=0, cursor_y=None):
|
||||
self.placements.append((location, face, hand, cursor_y))
|
||||
|
||||
world = PlacementWorld()
|
||||
game = FakeGame()
|
||||
state = SleepWithBedStates(Munch(
|
||||
pos=(0, 64, 0), yaw=0.0, path=[], look_at=None,
|
||||
world=world, game=game, holding_hand=0))
|
||||
state.state = state.find_bed_spot
|
||||
|
||||
state.find_bed_spot()
|
||||
self.assertEqual(world.path_call, ((0, 64, 0), layout.opening))
|
||||
self.assertEqual(state.area, layout.foot)
|
||||
self.assertEqual(state.g.path, [layout.opening])
|
||||
|
||||
state.g.pos = layout.opening
|
||||
state.going_to_area()
|
||||
self.assertEqual(state.g.look_at, layout.head)
|
||||
self.assertEqual(state.g.yaw_target_override, -90.0)
|
||||
self.assertEqual(state.state, state.turn_to_bed_area)
|
||||
|
||||
state.turn_to_bed_area()
|
||||
self.assertEqual(state.state, state.turn_to_bed_area)
|
||||
for _ in range(3):
|
||||
update_yaw(
|
||||
state.g, Vector3D((-1.5, 0, 0)),
|
||||
fixed_target=state.g.yaw_target_override)
|
||||
state.turn_to_bed_area()
|
||||
self.assertEqual(state.state, state.place_bed)
|
||||
|
||||
state.place_bed()
|
||||
self.assertEqual(game.placements[0][0], (2, 63, 0))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,157 @@
|
||||
import io
|
||||
import unittest
|
||||
from contextlib import redirect_stdout
|
||||
from unittest.mock import patch
|
||||
|
||||
from munch import Munch
|
||||
from minecraft.networking.modern import ClientTickEnd, UseItemOnBlockPacket
|
||||
from minecraft.networking.packets import PacketBuffer
|
||||
from minecraft.networking.types import Float, Long, VarInt, VarLong
|
||||
|
||||
from mosfet.game import Game
|
||||
from mosfet.info import blocks, items
|
||||
from mosfet.protocol.modern import PlayerCommandPacket, UpdateTimePacket
|
||||
from mosfet.protocol.types import Slot
|
||||
from mosfet.world import World
|
||||
|
||||
|
||||
class FakeConnection:
|
||||
def __init__(self):
|
||||
self.packets = []
|
||||
|
||||
def write_packet(self, packet, force=False):
|
||||
self.packets.append((packet, force))
|
||||
|
||||
|
||||
class FakeChunks:
|
||||
def __init__(self, block):
|
||||
self.block = block
|
||||
|
||||
def get_block_at(self, *position):
|
||||
return self.block
|
||||
|
||||
|
||||
class BedPacketsAndWorldTests(unittest.TestCase):
|
||||
def test_bed_occupancy_only_logs_first_observation_and_changes(self):
|
||||
bed_states = [
|
||||
state for state in blocks.BED_IDS
|
||||
if blocks.BLOCKS[state] == 'white_bed' and
|
||||
blocks.PROPS[state].get('part') == 'foot' and
|
||||
blocks.PROPS[state].get('facing') == 'north'
|
||||
]
|
||||
free = next(state for state in bed_states
|
||||
if blocks.PROPS[state].get('occupied') == 'false')
|
||||
occupied = next(state for state in bed_states
|
||||
if blocks.PROPS[state].get('occupied') == 'true')
|
||||
chunks = FakeChunks(free)
|
||||
world = World(Munch(chunks=chunks))
|
||||
output = io.StringIO()
|
||||
|
||||
with redirect_stdout(output):
|
||||
self.assertFalse(world.check_bed_occupied((1, 64, 2)))
|
||||
self.assertFalse(world.check_bed_occupied((1, 64, 2)))
|
||||
chunks.block = occupied
|
||||
self.assertTrue(world.check_bed_occupied((1, 64, 2)))
|
||||
self.assertTrue(world.check_bed_occupied((1, 64, 2)))
|
||||
|
||||
lines = output.getvalue().splitlines()
|
||||
self.assertEqual(len(lines), 2)
|
||||
self.assertIn('-> free', lines[0])
|
||||
self.assertIn('-> occupied', lines[1])
|
||||
|
||||
def test_update_time_packet_decodes_clock_updates(self):
|
||||
payload = PacketBuffer()
|
||||
Long.send(123456, payload)
|
||||
VarInt.send(1, payload)
|
||||
VarInt.send(0, payload)
|
||||
VarLong.send(13000, payload)
|
||||
Float.send(0.5, payload)
|
||||
Float.send(1.0, payload)
|
||||
|
||||
packet = UpdateTimePacket()
|
||||
packet.read(io.BytesIO(payload.get_writable()))
|
||||
self.assertEqual(packet.id, 0x73)
|
||||
self.assertEqual(packet.age, 123456)
|
||||
self.assertEqual(packet.clock_updates, [{
|
||||
'clock_id': 0, 'total_ticks': 13000,
|
||||
'partial_tick': 0.5, 'rate': 1.0,
|
||||
}])
|
||||
|
||||
def test_game_advances_world_clock_between_server_updates(self):
|
||||
game = Game.__new__(Game)
|
||||
game.g = Munch(
|
||||
time_base=None, time_update_received_at=None, clock_rate=1.0,
|
||||
time=0.0,
|
||||
connection=Munch(registry_ids={'minecraft:world_clock': {
|
||||
'minecraft:the_end': 1,
|
||||
'minecraft:overworld': 0,
|
||||
}}),
|
||||
dimension='overworld')
|
||||
packet = Munch(age=900000, clock_updates=[
|
||||
{'clock_id': 1, 'total_ticks': 6000,
|
||||
'partial_tick': 0.0, 'rate': 1.0},
|
||||
{'clock_id': 0, 'total_ticks': 13000,
|
||||
'partial_tick': 0.5, 'rate': 1.0},
|
||||
])
|
||||
|
||||
with patch('mosfet.game.time.monotonic', return_value=100.0):
|
||||
game.handle_time_update(packet)
|
||||
self.assertEqual(game.g.time, 13000.5)
|
||||
self.assertEqual(game.g.clock_name, 'minecraft:overworld')
|
||||
|
||||
with patch('mosfet.game.time.monotonic', return_value=100.5):
|
||||
game._advance_world_time()
|
||||
|
||||
self.assertEqual(game.g.time, 13010.5)
|
||||
|
||||
def test_player_command_encodes_stop_sleeping(self):
|
||||
packet = PlayerCommandPacket(entity_id=7, action_id=0, jump_boost=0)
|
||||
payload = PacketBuffer()
|
||||
packet.write_fields(payload)
|
||||
self.assertEqual(packet.id, 0x2A)
|
||||
self.assertEqual(payload.get_writable(), b'\x07\x00\x00')
|
||||
|
||||
def test_game_leave_bed_sends_protocol_777_stop_sleeping(self):
|
||||
connection = FakeConnection()
|
||||
game = Game.__new__(Game)
|
||||
game.g = Munch(connection=connection, eid=42)
|
||||
|
||||
game.leave_bed()
|
||||
|
||||
packet, force = connection.packets[0]
|
||||
self.assertIsInstance(packet, PlayerCommandPacket)
|
||||
self.assertEqual((packet.entity_id, packet.action_id,
|
||||
packet.jump_boost), (42, 0, 0))
|
||||
self.assertTrue(force)
|
||||
self.assertIsInstance(connection.packets[1][0], ClientTickEnd)
|
||||
|
||||
def test_existing_bed_use_aims_inside_the_short_bed_collision_shape(self):
|
||||
connection = FakeConnection()
|
||||
game = Game.__new__(Game)
|
||||
game.g = Munch(connection=connection)
|
||||
game.sequence = 0
|
||||
|
||||
game.place_block((-175, 70, -312), 1, cursor_y=0.5)
|
||||
|
||||
packet = connection.packets[0][0]
|
||||
self.assertIsInstance(packet, UseItemOnBlockPacket)
|
||||
self.assertEqual(packet.location, (-175, 70, -312))
|
||||
self.assertEqual(packet.cursor_y, 0.5)
|
||||
self.assertIsInstance(connection.packets[1][0], ClientTickEnd)
|
||||
|
||||
def test_bed_in_offhand_is_selectable_without_swapping_inventory(self):
|
||||
connection = FakeConnection()
|
||||
game = Game.__new__(Game)
|
||||
bed_id = next(iter(items.BED_IDS))
|
||||
game.g = Munch(
|
||||
connection=connection, inv={45: Slot(True, bed_id, 1)},
|
||||
holding_hand=0)
|
||||
|
||||
self.assertTrue(game.select_item(items.BED_IDS))
|
||||
self.assertEqual(game.g.holding, bed_id)
|
||||
self.assertEqual(game.g.holding_hand, 1)
|
||||
self.assertIsInstance(connection.packets[0][0], ClientTickEnd)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,42 @@
|
||||
import unittest
|
||||
from munch import Munch
|
||||
|
||||
from mosfet.info import blocks
|
||||
from mosfet.world import BedArea, World
|
||||
|
||||
|
||||
class BedWorldTests(unittest.TestCase):
|
||||
def make_world(self, supported):
|
||||
class GridChunks:
|
||||
def get_block_at(self, x, y, z):
|
||||
if y == 63 and (x, y, z) in supported:
|
||||
return blocks.STONE
|
||||
return blocks.AIR
|
||||
|
||||
return World(Munch(chunks=GridChunks(), mobs={}))
|
||||
|
||||
def test_bed_areas_require_standing_and_two_bed_tiles(self):
|
||||
supported = {(x, 63, 0) for x in (1, 2, 3)}
|
||||
world = self.make_world(supported)
|
||||
|
||||
layouts = list(world.find_bed_areas(
|
||||
(0, 64, 0), 4, include_layout=True))
|
||||
|
||||
self.assertIn(BedArea(
|
||||
foot=(2, 64, 0), head=(3, 64, 0),
|
||||
opening=(1, 64, 0), facing=(1, 0, 0)), layouts)
|
||||
self.assertIn((1, 64, 0), list(world.find_bed_areas((0, 64, 0), 4)))
|
||||
|
||||
def test_bed_lane_is_rejected_without_third_supported_block(self):
|
||||
supported = {(1, 63, 0), (2, 63, 0)}
|
||||
world = self.make_world(supported)
|
||||
|
||||
layouts = list(world.find_bed_areas(
|
||||
(0, 64, 0), 4, include_layout=True))
|
||||
|
||||
self.assertFalse(any(
|
||||
layout.opening == (1, 64, 0) for layout in layouts))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,656 +0,0 @@
|
||||
import io
|
||||
import unittest
|
||||
from contextlib import redirect_stdout
|
||||
from munch import Munch
|
||||
|
||||
from minecraft.networking.types import BlockFace
|
||||
|
||||
from mosfet.info import items
|
||||
from mosfet.jobs.cache_items import CacheItemsStates
|
||||
from mosfet.world import World
|
||||
|
||||
|
||||
class CacheItemsTests(unittest.TestCase):
|
||||
def test_chest_search_log_distinguishes_labeled_and_trapped_counts(self):
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def find_labeled_chests(*_args):
|
||||
return [{} for _ in range(11)]
|
||||
|
||||
@staticmethod
|
||||
def find_blocks_indexed(*_args):
|
||||
return []
|
||||
|
||||
state = CacheItemsStates(Munch(
|
||||
world=FakeWorld(), pos=(0, 64, 0)))
|
||||
output = io.StringIO()
|
||||
with redirect_stdout(output):
|
||||
state.find_trapped_chests()
|
||||
|
||||
self.assertIn(
|
||||
'Found 11 labeled chests; found 0 trapped chests: []',
|
||||
output.getvalue())
|
||||
|
||||
def test_throw_signs_are_checked_before_cache_containers(self):
|
||||
directive = {
|
||||
'position': (2, 64, 0), 'side': 'front',
|
||||
'item_name': 'seeds', 'item_ids': {items.WHEAT_SEEDS_ID},
|
||||
}
|
||||
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def find_throw_signs(_center, _bot_name, _distance):
|
||||
return [directive]
|
||||
|
||||
@staticmethod
|
||||
def find_labeled_chests(*_args):
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
def find_blocks_indexed(*_args):
|
||||
return []
|
||||
|
||||
state = CacheItemsStates(Munch(
|
||||
world=FakeWorld(), pos=(0, 64, 0), name='mosfet1'))
|
||||
|
||||
state.find_trapped_chests()
|
||||
|
||||
self.assertIs(state.state.__func__, state.choose_throw_sign.__func__)
|
||||
self.assertEqual(state.throw_signs, [directive])
|
||||
|
||||
def test_throw_sign_does_not_bypass_cache_inventory_threshold(self):
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def find_throw_signs(*_args):
|
||||
raise AssertionError('throw signs are checked only at threshold')
|
||||
|
||||
class FakeGame:
|
||||
@staticmethod
|
||||
def count_inventory_slots():
|
||||
return 1
|
||||
|
||||
state = CacheItemsStates(Munch(
|
||||
world=FakeWorld(), pos=(0, 64, 0), name='mosfet2',
|
||||
game=FakeGame(), inv={
|
||||
9: Munch(present=True, item_id=items.WHEAT_SEEDS_ID,
|
||||
item_count=12),
|
||||
}, minimum_cache_slots=27))
|
||||
|
||||
state.init()
|
||||
|
||||
self.assertIs(state.state.__func__, state.cleanup.__func__)
|
||||
|
||||
def test_throw_sign_throws_target_item_at_cache_threshold(self):
|
||||
position = (2, 64, 0)
|
||||
opening = (1, 64, 0)
|
||||
seed = Munch(present=True, item_id=items.WHEAT_SEEDS_ID,
|
||||
item_count=12)
|
||||
dropped = []
|
||||
g = Munch(
|
||||
name='mosfet2', pos=(0, 64, 0), inv={9: seed},
|
||||
item_lock=False, drop_waiting=None, path=[], look_at=None,
|
||||
minimum_cache_slots=27,
|
||||
)
|
||||
directive = {
|
||||
'position': position, 'side': 'front', 'item_name': 'seeds',
|
||||
'item_ids': items.storage_item_ids_for_label('seeds'),
|
||||
}
|
||||
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def find_throw_signs(_center, bot_name, _distance):
|
||||
self.assertEqual(bot_name, 'mosfet2')
|
||||
return [directive]
|
||||
|
||||
@staticmethod
|
||||
def find_throw_openings(sign, center, radius):
|
||||
self.assertEqual(sign, directive)
|
||||
self.assertEqual(radius, 3)
|
||||
return [{'position': opening, 'alignment': 1.0}]
|
||||
|
||||
@staticmethod
|
||||
def find_labeled_chests(*_args):
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
def find_blocks_indexed(*_args):
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
def path_to_place(start, target):
|
||||
return [start, target]
|
||||
|
||||
class FakeGame:
|
||||
@staticmethod
|
||||
def count_inventory_slots():
|
||||
return 27
|
||||
|
||||
@staticmethod
|
||||
def drop_inventory_slot(slot, whole_stack=True):
|
||||
dropped.append((slot, whole_stack))
|
||||
g.drop_waiting = {'slot': slot}
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def select_item(_item_ids):
|
||||
return False
|
||||
|
||||
g.world = FakeWorld()
|
||||
g.game = FakeGame()
|
||||
state = CacheItemsStates(g)
|
||||
|
||||
state.init()
|
||||
self.assertIs(state.state.__func__, state.find_trapped_chests.__func__)
|
||||
state.find_trapped_chests()
|
||||
self.assertIs(state.state.__func__, state.choose_throw_sign.__func__)
|
||||
state.choose_throw_sign()
|
||||
self.assertEqual(state.opening, opening)
|
||||
g.pos = opening
|
||||
state.going_to_throw_sign()
|
||||
self.assertEqual(g.look_at, position)
|
||||
state.wait_time = 0
|
||||
state.turn_to_throw_sign()
|
||||
state.throw_items()
|
||||
self.assertEqual(dropped, [(9, True)])
|
||||
self.assertIs(state.state.__func__, state.wait_for_throw.__func__)
|
||||
|
||||
g.drop_waiting = None
|
||||
g.inv[9] = Munch(present=False, item_id=0, item_count=0)
|
||||
state.wait_for_throw()
|
||||
state.throw_items()
|
||||
self.assertIs(state.state.__func__, state.choose_throw_sign.__func__)
|
||||
state.choose_throw_sign()
|
||||
self.assertIs(state.state.__func__, state.choose_trapped_chest.__func__)
|
||||
state.choose_trapped_chest()
|
||||
state.select_chest()
|
||||
self.assertIs(state.state.__func__, state.cleanup.__func__)
|
||||
|
||||
def test_long_cache_trip_requires_a_valid_return_inside_work_boundary(self):
|
||||
start = (0, 64, 0)
|
||||
opening = (51, 64, 0)
|
||||
policy = {'position': start, 'distance': 10, 'shape': 'square'}
|
||||
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def _sign_policy_distance(position, boundary):
|
||||
return World._sign_policy_distance(position, boundary)
|
||||
|
||||
@staticmethod
|
||||
def _inside_sign_policy(position, boundary):
|
||||
return World._inside_sign_policy(position, boundary)
|
||||
|
||||
@staticmethod
|
||||
def _path_allowed_by_sign(points, boundary):
|
||||
return World._path_allowed_by_sign(points, boundary)
|
||||
|
||||
@staticmethod
|
||||
def _cache_opening_standable(_position):
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def sign_policies(_bot_name):
|
||||
return [policy]
|
||||
|
||||
@staticmethod
|
||||
def path_to_place(origin, target):
|
||||
return [tuple(origin), tuple(target)]
|
||||
|
||||
state = CacheItemsStates(Munch(
|
||||
world=FakeWorld(), name='mosfet2', pos=start))
|
||||
state._capture_work_policies()
|
||||
|
||||
self.assertTrue(state._accept_cache_trip(
|
||||
start, opening, [start, opening]))
|
||||
self.assertTrue(state.cache_return_required)
|
||||
self.assertTrue(state._inside_work_policies(
|
||||
state.cache_return_point))
|
||||
self.assertEqual(state.cache_trip_max_outside, 41)
|
||||
|
||||
def test_cache_trip_exactly_40_beyond_boundary_does_not_trigger_return(self):
|
||||
start = (0, 64, 0)
|
||||
opening = (50, 64, 0)
|
||||
policy = {'position': start, 'distance': 10, 'shape': 'square'}
|
||||
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def _sign_policy_distance(position, boundary):
|
||||
return World._sign_policy_distance(position, boundary)
|
||||
|
||||
@staticmethod
|
||||
def _inside_sign_policy(position, boundary):
|
||||
return World._inside_sign_policy(position, boundary)
|
||||
|
||||
@staticmethod
|
||||
def sign_policies(_bot_name):
|
||||
return [policy]
|
||||
|
||||
state = CacheItemsStates(Munch(
|
||||
world=FakeWorld(), name='mosfet2', pos=start))
|
||||
state._capture_work_policies()
|
||||
|
||||
self.assertTrue(state._accept_cache_trip(
|
||||
start, opening, [start, opening]))
|
||||
self.assertFalse(state.cache_return_required)
|
||||
self.assertEqual(state.cache_trip_max_outside, 40)
|
||||
|
||||
def test_cache_replans_return_path_after_finishing_chests(self):
|
||||
start = (0, 64, 0)
|
||||
opening = (51, 64, 0)
|
||||
return_point = (9, 64, 0)
|
||||
policy = {'position': start, 'distance': 10, 'shape': 'square'}
|
||||
path_calls = []
|
||||
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def _sign_policy_distance(position, boundary):
|
||||
return World._sign_policy_distance(position, boundary)
|
||||
|
||||
@staticmethod
|
||||
def _inside_sign_policy(position, boundary):
|
||||
return World._inside_sign_policy(position, boundary)
|
||||
|
||||
@staticmethod
|
||||
def _path_allowed_by_sign(points, boundary):
|
||||
return World._path_allowed_by_sign(points, boundary)
|
||||
|
||||
@staticmethod
|
||||
def _cache_opening_standable(_position):
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def path_to_place(origin, target):
|
||||
path_calls.append((tuple(origin), tuple(target)))
|
||||
return [tuple(origin), tuple(target)]
|
||||
|
||||
g = Munch(world=FakeWorld(), name='mosfet2', pos=opening,
|
||||
path=[], look_at=None)
|
||||
state = CacheItemsStates(g)
|
||||
state.work_policies = [policy]
|
||||
state.cache_return_point = return_point
|
||||
state.cache_return_required = True
|
||||
state.cleanup()
|
||||
self.assertIs(state.state.__func__, state.return_to_work_area.__func__)
|
||||
|
||||
state.return_to_work_area()
|
||||
|
||||
self.assertIn((opening, state.cache_return_point), path_calls)
|
||||
self.assertTrue(state._inside_work_policies(state.cache_return_point))
|
||||
self.assertIs(state.state.__func__, state.going_to_return_point.__func__)
|
||||
self.assertEqual(g.path[-1], state.cache_return_point)
|
||||
|
||||
g.pos = state.cache_return_point
|
||||
state.going_to_return_point()
|
||||
self.assertFalse(state.cache_return_required)
|
||||
self.assertIs(state.state.__func__, state.cleanup.__func__)
|
||||
|
||||
def test_wanted_single_stack_is_not_considered_transferable(self):
|
||||
wanted_id = items.WHEAT_SEEDS_ID
|
||||
old_wanted = items.WANTED_ITEMS
|
||||
items.WANTED_ITEMS = set(old_wanted) | {wanted_id}
|
||||
try:
|
||||
state = CacheItemsStates(Munch(inv={
|
||||
9: Munch(present=True, item_id=wanted_id, item_count=56),
|
||||
}))
|
||||
self.assertFalse(state._has_transferable_item({wanted_id}))
|
||||
|
||||
state.g.inv[10] = Munch(
|
||||
present=True, item_id=wanted_id, item_count=12)
|
||||
self.assertTrue(state._has_transferable_item({wanted_id}))
|
||||
finally:
|
||||
items.WANTED_ITEMS = old_wanted
|
||||
|
||||
def test_post_sleep_halves_threshold_only_with_compatible_nearby_chest(self):
|
||||
bed = (0, 64, 0)
|
||||
chest_position = (5, 64, 0)
|
||||
item_id = items.get_id('stone')
|
||||
|
||||
class FakeGame:
|
||||
@staticmethod
|
||||
def count_inventory_slots():
|
||||
return 14
|
||||
|
||||
class NearbyWorld:
|
||||
@staticmethod
|
||||
def find_labeled_chests(_center, _distance):
|
||||
return [{
|
||||
'position': chest_position,
|
||||
'positions': (chest_position,),
|
||||
'item_ids': {item_id},
|
||||
}]
|
||||
|
||||
g = Munch(
|
||||
game=FakeGame(), world=NearbyWorld(),
|
||||
minimum_cache_slots=27, post_sleep_cache_bed=bed,
|
||||
inv={9: Munch(present=True, item_id=item_id, item_count=64)})
|
||||
state = CacheItemsStates(g)
|
||||
|
||||
state.init()
|
||||
|
||||
self.assertIs(state.state.__func__, state.find_trapped_chests.__func__)
|
||||
self.assertNotIn('post_sleep_cache_bed', g)
|
||||
|
||||
def test_post_sleep_threshold_rounds_27_up_to_14(self):
|
||||
bed = (0, 64, 0)
|
||||
item_id = items.get_id('stone')
|
||||
|
||||
class FakeGame:
|
||||
@staticmethod
|
||||
def count_inventory_slots():
|
||||
return 13
|
||||
|
||||
class NearbyWorld:
|
||||
@staticmethod
|
||||
def find_labeled_chests(_center, _distance):
|
||||
return [{
|
||||
'position': (5, 64, 0),
|
||||
'positions': ((5, 64, 0),),
|
||||
'item_ids': {item_id},
|
||||
}]
|
||||
|
||||
g = Munch(
|
||||
game=FakeGame(), world=NearbyWorld(), minimum_cache_slots=27,
|
||||
post_sleep_cache_bed=bed,
|
||||
inv={9: Munch(present=True, item_id=item_id, item_count=64)})
|
||||
state = CacheItemsStates(g)
|
||||
|
||||
state.init()
|
||||
|
||||
self.assertIs(state.state.__func__, state.cleanup.__func__)
|
||||
|
||||
def test_post_sleep_does_not_lower_threshold_without_compatible_nearby_chest(self):
|
||||
class FakeGame:
|
||||
@staticmethod
|
||||
def count_inventory_slots():
|
||||
return 14
|
||||
|
||||
class EmptyWorld:
|
||||
@staticmethod
|
||||
def find_labeled_chests(_center, _distance):
|
||||
return []
|
||||
|
||||
g = Munch(
|
||||
game=FakeGame(), world=EmptyWorld(), minimum_cache_slots=27,
|
||||
post_sleep_cache_bed=(0, 64, 0), inv={})
|
||||
state = CacheItemsStates(g)
|
||||
|
||||
state.init()
|
||||
|
||||
self.assertIs(state.state.__func__, state.cleanup.__func__)
|
||||
self.assertNotIn('post_sleep_cache_bed', g)
|
||||
|
||||
def test_post_sleep_ignores_compatible_chest_beyond_16_blocks(self):
|
||||
item_id = items.get_id('stone')
|
||||
|
||||
class FakeGame:
|
||||
@staticmethod
|
||||
def count_inventory_slots():
|
||||
return 14
|
||||
|
||||
class FarWorld:
|
||||
@staticmethod
|
||||
def find_labeled_chests(_center, _distance):
|
||||
position = (17, 64, 0)
|
||||
return [{
|
||||
'position': position,
|
||||
'positions': (position,),
|
||||
'item_ids': {item_id},
|
||||
}]
|
||||
|
||||
state = CacheItemsStates(Munch(
|
||||
game=FakeGame(), world=FarWorld(), minimum_cache_slots=27,
|
||||
post_sleep_cache_bed=(0, 64, 0),
|
||||
inv={9: Munch(present=True, item_id=item_id, item_count=64)}))
|
||||
|
||||
state.init()
|
||||
|
||||
self.assertIs(state.state.__func__, state.cleanup.__func__)
|
||||
|
||||
def test_manual_cache_threshold_override_is_consumed_once(self):
|
||||
class FakeGame:
|
||||
@staticmethod
|
||||
def count_inventory_slots():
|
||||
return 5
|
||||
|
||||
state = CacheItemsStates(Munch(
|
||||
game=FakeGame(), minimum_cache_slots=27))
|
||||
state.minimum = 0
|
||||
|
||||
state.init()
|
||||
self.assertIs(state.state.__func__, state.find_trapped_chests.__func__)
|
||||
self.assertNotIn('minimum', state.__dict__)
|
||||
|
||||
state.init()
|
||||
self.assertIs(state.state.__func__, state.cleanup.__func__)
|
||||
|
||||
def test_successful_labeled_transfer_skips_placing_another_cache_chest(self):
|
||||
item_id = items.get_id('stone')
|
||||
stack = Munch(present=True, item_id=item_id, item_count=16)
|
||||
window = Munch(container_slots=27, contents={
|
||||
**dict.fromkeys(range(27)), 27: stack,
|
||||
})
|
||||
clicks = []
|
||||
|
||||
class FakeGame:
|
||||
@staticmethod
|
||||
def count_window_slots():
|
||||
return 0
|
||||
|
||||
@staticmethod
|
||||
def click_window(*args):
|
||||
clicks.append(args)
|
||||
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def find_blocks_indexed(*_args):
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
def find_labeled_chests(*_args):
|
||||
return []
|
||||
|
||||
g = Munch(
|
||||
game=FakeGame(), world=FakeWorld(), pos=(0, 64, 0),
|
||||
inv={}, window=window, item_lock=False)
|
||||
state = CacheItemsStates(g)
|
||||
state.is_labeled_container = True
|
||||
state.container_item_ids = {item_id}
|
||||
|
||||
state.move_items()
|
||||
self.assertEqual(len(clicks), 1)
|
||||
self.assertFalse(state.transferred_to_labeled_chest)
|
||||
|
||||
window.contents[27] = Munch(present=False, item_id=0, item_count=0)
|
||||
g.item_lock = False
|
||||
state.pending_transfer['confirm_after'] = 0
|
||||
state.move_items()
|
||||
self.assertTrue(state.transferred_to_labeled_chest)
|
||||
|
||||
state.labeled_chests = []
|
||||
state.trapped_chests = []
|
||||
state.choose_trapped_chest()
|
||||
self.assertIs(state.state.__func__, state.cleanup.__func__)
|
||||
|
||||
def test_full_chest_is_blacklisted_when_first_opened(self):
|
||||
position = (5, 64, 0)
|
||||
key = (position,)
|
||||
window = Munch(container_slots=27, contents={})
|
||||
closed = []
|
||||
|
||||
class FakeGame:
|
||||
@staticmethod
|
||||
def count_window_slots():
|
||||
return 27
|
||||
|
||||
@staticmethod
|
||||
def close_window():
|
||||
closed.append(True)
|
||||
|
||||
state = CacheItemsStates(Munch(
|
||||
game=FakeGame(), window=window, inv={}, item_lock=False,
|
||||
look_at=None))
|
||||
state.current_container_key = key
|
||||
state.is_labeled_container = True
|
||||
|
||||
state.move_items()
|
||||
|
||||
self.assertIn(key, state.useless_chests)
|
||||
self.assertTrue(state.container_was_full_on_open)
|
||||
self.assertEqual(closed, [True])
|
||||
|
||||
def test_chest_filled_by_this_cache_round_is_not_blacklisted(self):
|
||||
position = (5, 64, 0)
|
||||
key = (position,)
|
||||
item_id = items.get_id('dirt')
|
||||
stack = Munch(present=True, item_id=item_id, item_count=16)
|
||||
window = Munch(container_slots=27, contents={
|
||||
**dict.fromkeys(range(27)), 27: stack,
|
||||
})
|
||||
clicks = []
|
||||
|
||||
class FakeGame:
|
||||
used = 26
|
||||
|
||||
@classmethod
|
||||
def count_window_slots(cls):
|
||||
return cls.used
|
||||
|
||||
@classmethod
|
||||
def click_window(cls, *_args):
|
||||
clicks.append(True)
|
||||
cls.used = 27
|
||||
window.contents[27] = Munch(
|
||||
present=False, item_id=0, item_count=0)
|
||||
|
||||
@staticmethod
|
||||
def close_window():
|
||||
return None
|
||||
|
||||
state = CacheItemsStates(Munch(
|
||||
game=FakeGame(), window=window, inv={}, item_lock=False,
|
||||
look_at=None))
|
||||
state.current_container_key = key
|
||||
|
||||
state.move_items()
|
||||
self.assertFalse(state.container_was_full_on_open)
|
||||
self.assertEqual(clicks, [True])
|
||||
|
||||
state.pending_transfer['confirm_after'] = 0
|
||||
state.g.item_lock = False
|
||||
state.move_items()
|
||||
|
||||
self.assertNotIn(key, state.useless_chests)
|
||||
self.assertIs(state.state.__func__, state.cleanup.__func__)
|
||||
|
||||
def test_other_player_open_removes_chest_from_full_blacklist(self):
|
||||
position = (5, 64, 0)
|
||||
key = (position, (6, 64, 0))
|
||||
g = Munch(chest_open_events=[])
|
||||
state = CacheItemsStates(g)
|
||||
state.useless_chests.add(key)
|
||||
state.checked_labeled_chests.add(key)
|
||||
|
||||
g.chest_open_events.append({
|
||||
'position': (6, 64, 0), 'timestamp': 1.0,
|
||||
})
|
||||
state.run()
|
||||
|
||||
self.assertNotIn(key, state.useless_chests)
|
||||
self.assertNotIn(key, state.checked_labeled_chests)
|
||||
|
||||
def test_mutually_reachable_opening_is_skipped_after_path_failure(self):
|
||||
start = (0, 64, 0)
|
||||
first = (4, 64, 0)
|
||||
nearby = (4, 64, 1)
|
||||
item_id = items.get_id('stone')
|
||||
calls = []
|
||||
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def find_cache_openings(area, _center):
|
||||
position = first if area == (10, 64, 0) else nearby
|
||||
return [{'position': position}]
|
||||
|
||||
def path_to_place(self, origin, target):
|
||||
origin, target = tuple(origin), tuple(target)
|
||||
calls.append((origin, target))
|
||||
if origin == start and target == first:
|
||||
return None
|
||||
if (origin, target) in ((nearby, first), (first, nearby)):
|
||||
return [origin, target]
|
||||
return None
|
||||
|
||||
state = CacheItemsStates(Munch(
|
||||
world=FakeWorld(), pos=start,
|
||||
inv={9: Munch(present=True, item_id=item_id)}))
|
||||
state.labeled_chests = [
|
||||
{'position': (10, 64, 0), 'positions': ((10, 64, 0),),
|
||||
'item_ids': {item_id}, 'labels': {'Stone'}},
|
||||
{'position': (11, 64, 0), 'positions': ((11, 64, 0),),
|
||||
'item_ids': {item_id}, 'labels': {'Stone'}},
|
||||
]
|
||||
state.trapped_chests = []
|
||||
state.choose_trapped_chest()
|
||||
|
||||
self.assertEqual(calls, [
|
||||
(start, first), (nearby, first), (first, nearby),
|
||||
])
|
||||
self.assertIn(first, state.bad_openings)
|
||||
self.assertIn(nearby, state.bad_openings)
|
||||
|
||||
def test_one_way_reachability_does_not_blacklist_opening(self):
|
||||
bad = (1, 64, 0)
|
||||
candidate = (2, 64, 0)
|
||||
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def path_to_place(start, target):
|
||||
return [start, target] if tuple(start) == candidate else None
|
||||
|
||||
state = CacheItemsStates(Munch())
|
||||
state.bad_openings = {bad}
|
||||
|
||||
self.assertFalse(state._opening_in_bad_region(FakeWorld(), candidate))
|
||||
self.assertNotIn(candidate, state.bad_openings)
|
||||
|
||||
def test_cache_travel_looks_along_path_then_turns_to_chest(self):
|
||||
opening = (5, 64, 8)
|
||||
chest_look = (4.9, 64.1, 7.8)
|
||||
state = CacheItemsStates(Munch(
|
||||
pos=(2.5, 64, 2.5), look_at=(1, 64, 1)))
|
||||
state.opening = opening
|
||||
state.open_interaction = {'look_at': chest_look}
|
||||
|
||||
state.going_to_trapped_chest()
|
||||
self.assertIsNone(state.g.look_at)
|
||||
|
||||
state.g.pos = opening
|
||||
state.going_to_trapped_chest()
|
||||
self.assertEqual(state.g.look_at, chest_look)
|
||||
self.assertIs(state.state.__func__, state.turn_to_trapped_chest.__func__)
|
||||
|
||||
state.g.pos = (2.5, 64, 2.5)
|
||||
state.g.look_at = (1, 64, 1)
|
||||
state.going_to_area()
|
||||
self.assertIsNone(state.g.look_at)
|
||||
|
||||
def test_cache_chest_placement_sneaks_to_avoid_opening_support_container(self):
|
||||
calls = []
|
||||
|
||||
class FakeGame:
|
||||
@staticmethod
|
||||
def place_block(*args, **kwargs):
|
||||
calls.append((args, kwargs))
|
||||
|
||||
state = CacheItemsStates(Munch(game=FakeGame()))
|
||||
state.area = (3, 65, 7)
|
||||
|
||||
state.place_chest()
|
||||
|
||||
self.assertEqual(calls, [(
|
||||
((3, 64, 7), BlockFace.TOP), {'sneaking': True},
|
||||
)])
|
||||
self.assertIs(state.state.__func__, state.open_chest.__func__)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,37 +0,0 @@
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
from munch import Munch
|
||||
|
||||
from mosfet.jobs.clear_leaves import ClearLeavesStates
|
||||
|
||||
|
||||
class ClearLeavesTests(unittest.TestCase):
|
||||
def test_spaces_leaf_breaks_at_configured_interval(self):
|
||||
class FakeGame:
|
||||
def __init__(self):
|
||||
self.broken = []
|
||||
|
||||
def break_block(self, location):
|
||||
self.broken.append(location)
|
||||
return True
|
||||
|
||||
game = FakeGame()
|
||||
state = ClearLeavesStates(Munch(
|
||||
breaking=False, game=game, look_at=None))
|
||||
state.leaves = [(0, 64, 0), (1, 64, 0)]
|
||||
|
||||
with patch('mosfet.jobs.clear_leaves.time.monotonic', return_value=10):
|
||||
state.break_leaves()
|
||||
self.assertEqual(game.broken, [(0, 64, 0)])
|
||||
|
||||
with patch('mosfet.jobs.clear_leaves.time.monotonic', return_value=10.59):
|
||||
state.break_leaves()
|
||||
self.assertEqual(game.broken, [(0, 64, 0)])
|
||||
|
||||
with patch('mosfet.jobs.clear_leaves.time.monotonic', return_value=10.6):
|
||||
state.break_leaves()
|
||||
self.assertEqual(game.broken, [(0, 64, 0), (1, 64, 0)])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,214 +0,0 @@
|
||||
import io
|
||||
import time
|
||||
import unittest
|
||||
from contextlib import redirect_stdout
|
||||
from unittest.mock import patch
|
||||
|
||||
from munch import Munch
|
||||
|
||||
from mosfet.info import blocks, items
|
||||
from mosfet.jobs.gather_crop import GatherCropStates
|
||||
from mosfet.protocol.types import Slot
|
||||
|
||||
|
||||
class CropBatchTests(unittest.TestCase):
|
||||
def test_navigation_looks_at_the_crop_target(self):
|
||||
crop = (5, 64, 8)
|
||||
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def path_to_place(_start, _target):
|
||||
return [(1, 64, 1), crop]
|
||||
|
||||
g = Munch(world=FakeWorld(), pos=(1, 64, 1), path=[], look_at=None)
|
||||
state = GatherCropStates(g)
|
||||
state.crop = crop
|
||||
|
||||
state.nav_to_crop()
|
||||
|
||||
self.assertEqual(g.look_at, crop)
|
||||
self.assertEqual(g.path[-1], crop)
|
||||
|
||||
def test_found_crop_log_includes_search_duration(self):
|
||||
crop = (1, 64, 1)
|
||||
mature = blocks.MATURE_WHEAT_ID
|
||||
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def find_blocks_indexed(_center, _block_ids, _distance, sort=True):
|
||||
return [crop]
|
||||
|
||||
@staticmethod
|
||||
def filter_work_positions(positions):
|
||||
return list(positions)
|
||||
|
||||
@staticmethod
|
||||
def block_at(*_position):
|
||||
return mature
|
||||
|
||||
state = GatherCropStates(Munch(
|
||||
world=FakeWorld(), pos=(0, 64, 0)))
|
||||
output = io.StringIO()
|
||||
clock = [10.0]
|
||||
|
||||
def monotonic():
|
||||
clock[0] += 0.1
|
||||
return clock[0]
|
||||
|
||||
with patch('mosfet.jobs.gather_crop.time.monotonic',
|
||||
side_effect=monotonic), redirect_stdout(output):
|
||||
state.find_new_crop()
|
||||
|
||||
self.assertIn('Found crop after 0.90s:', output.getvalue())
|
||||
self.assertIn('Crop search timings:', output.getvalue())
|
||||
self.assertIn('ripe-index 0.100s (1/1 allowed)', output.getvalue())
|
||||
self.assertIn('all-crops+types 0.100s (1/1 allowed)', output.getvalue())
|
||||
self.assertIn('section-select 0.100s (1 ripe, 1 crops, selected section 1)',
|
||||
output.getvalue())
|
||||
|
||||
def test_selection_rechecks_maturity_after_candidate_search(self):
|
||||
position = (1, 64, 1)
|
||||
unripe = blocks.MCD_BLOCKS['wheat']['minStateId']
|
||||
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def find_blocks_indexed(_center, block_ids, _distance, sort=True):
|
||||
return [position]
|
||||
|
||||
@staticmethod
|
||||
def filter_work_positions(positions):
|
||||
return list(positions)
|
||||
|
||||
@staticmethod
|
||||
def block_at(*_position):
|
||||
return unripe
|
||||
|
||||
state = GatherCropStates(Munch(
|
||||
world=FakeWorld(), pos=(0, 64, 0)))
|
||||
state.find_new_crop()
|
||||
|
||||
self.assertIs(state.state.__func__, state.find_new_crop.__func__)
|
||||
self.assertIsNone(state.crop)
|
||||
|
||||
def test_break_skips_crop_that_became_unripe_on_the_way(self):
|
||||
position = (1, 64, 1)
|
||||
unripe = blocks.MCD_BLOCKS['wheat']['minStateId']
|
||||
mature = blocks.MATURE_WHEAT_ID
|
||||
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def block_at(*_position):
|
||||
return unripe
|
||||
|
||||
class FakeGame:
|
||||
def __init__(self):
|
||||
self.break_calls = []
|
||||
|
||||
def break_block(self, target):
|
||||
self.break_calls.append(target)
|
||||
return True
|
||||
|
||||
game = FakeGame()
|
||||
state = GatherCropStates(Munch(world=FakeWorld(), game=game))
|
||||
state.crop = position
|
||||
state.type_id = mature
|
||||
|
||||
state.break_crop()
|
||||
|
||||
self.assertEqual(game.break_calls, [])
|
||||
self.assertIs(state.state.__func__, state.find_new_crop.__func__)
|
||||
|
||||
def test_seed_selection_forces_slot_and_checks_inventory_hand(self):
|
||||
position = (1, 64, 1)
|
||||
seed = items.WHEAT_SEEDS_ID
|
||||
|
||||
class FakeGame:
|
||||
def __init__(self):
|
||||
self.placements = []
|
||||
self.selections = []
|
||||
|
||||
def select_item(self, item_ids, force=False):
|
||||
self.selections.append((set(item_ids), force))
|
||||
return True
|
||||
|
||||
def place_block(self, location, face, hand=0):
|
||||
self.placements.append((location, face, hand))
|
||||
|
||||
game = FakeGame()
|
||||
state = GatherCropStates(Munch(
|
||||
game=game, holding=seed, holding_hand=1,
|
||||
inv={45: Slot(True, seed, 1)}))
|
||||
state.crop = position
|
||||
state.type_id = blocks.MCD_BLOCKS['wheat']['minStateId']
|
||||
|
||||
state.select_seed()
|
||||
|
||||
self.assertEqual(game.selections, [({seed}, True)])
|
||||
self.assertIs(state.state.__func__, state.wait_select.__func__)
|
||||
state.wait_select()
|
||||
self.assertIs(state.state.__func__, state.place_crop.__func__)
|
||||
state.place_crop()
|
||||
self.assertEqual(game.placements, [((1, 63, 1), 1, 1)])
|
||||
|
||||
def test_wait_select_rejects_stale_local_held_item(self):
|
||||
seed = items.WHEAT_SEEDS_ID
|
||||
other_item = next(iter(items.AXE_IDS))
|
||||
state = GatherCropStates(Munch(
|
||||
holding=seed, holding_hand=0, holding_slot=0,
|
||||
inv={36: Slot(True, other_item, 1)}))
|
||||
state.target_seed = seed
|
||||
state.seed_deadline = time.monotonic() + 5.0
|
||||
state.state = state.wait_select
|
||||
|
||||
state.wait_select()
|
||||
|
||||
self.assertIs(state.state.__func__, state.wait_select.__func__)
|
||||
|
||||
def test_seed_selection_uses_crop_species_for_any_age_state(self):
|
||||
unripe_wheat = blocks.MCD_BLOCKS['wheat']['minStateId']
|
||||
|
||||
class FakeGame:
|
||||
def __init__(self):
|
||||
self.selected = None
|
||||
|
||||
def select_item(self, item_ids, force=False):
|
||||
self.selected = set(item_ids)
|
||||
self.force = force
|
||||
return True
|
||||
|
||||
game = FakeGame()
|
||||
state = GatherCropStates(Munch(game=game))
|
||||
state.type_id = unripe_wheat
|
||||
|
||||
state.select_seed()
|
||||
|
||||
self.assertEqual(state.target_seed, items.WHEAT_SEEDS_ID)
|
||||
self.assertEqual(game.selected, {items.WHEAT_SEEDS_ID})
|
||||
|
||||
def test_gather_crop_finishes_after_sixteen_successful_replants(self):
|
||||
unripe_wheat = blocks.MCD_BLOCKS['wheat']['minStateId']
|
||||
mature_wheat = blocks.MATURE_WHEAT_ID
|
||||
self.assertNotEqual(unripe_wheat, mature_wheat)
|
||||
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def block_at(*_position):
|
||||
return unripe_wheat
|
||||
|
||||
state = GatherCropStates(Munch(world=FakeWorld()))
|
||||
state.crop = (1, 64, 1)
|
||||
state.type_id = mature_wheat
|
||||
|
||||
for completed in range(1, state.CROPS_PER_BATCH):
|
||||
state.wait_place()
|
||||
self.assertEqual(state.completed_crops, completed)
|
||||
self.assertIs(state.state.__func__, state.find_new_crop.__func__)
|
||||
state.state = state.wait_place
|
||||
|
||||
state.wait_place()
|
||||
self.assertEqual(state.completed_crops, 0)
|
||||
self.assertIs(state.state.__func__, state.cleanup.__func__)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,40 +0,0 @@
|
||||
import unittest
|
||||
|
||||
from munch import Munch
|
||||
|
||||
from mosfet.info import items
|
||||
from mosfet.job import JobStates
|
||||
|
||||
|
||||
class TestCropCaching(unittest.TestCase):
|
||||
def test_crop_farm_runs_silent_cache_machine_and_keeps_seeds(self):
|
||||
old_wanted = items.WANTED_ITEMS
|
||||
supply_state = object()
|
||||
crop_state = object()
|
||||
cache_state = Munch(silent=False)
|
||||
sleep_state = Munch(silent=False)
|
||||
eat_state = object()
|
||||
jobs = JobStates.__new__(JobStates)
|
||||
jobs.grab_supplies_states = supply_state
|
||||
jobs.gather_crop_states = crop_state
|
||||
jobs.cache_items_states = cache_state
|
||||
jobs.sleep_with_bed_states = sleep_state
|
||||
jobs.eat_food_states = eat_state
|
||||
|
||||
try:
|
||||
machines = JobStates.farm_crop(jobs)
|
||||
self.assertEqual(
|
||||
machines, [supply_state, crop_state, sleep_state,
|
||||
eat_state, cache_state])
|
||||
self.assertTrue(cache_state.silent)
|
||||
self.assertTrue(sleep_state.silent)
|
||||
self.assertTrue({
|
||||
items.CARROT_ID, items.POTATO_ID,
|
||||
items.WHEAT_SEEDS_ID, items.BEETROOT_SEEDS_ID,
|
||||
}.issubset(items.WANTED_ITEMS))
|
||||
finally:
|
||||
items.WANTED_ITEMS = old_wanted
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,85 +0,0 @@
|
||||
import unittest
|
||||
|
||||
from munch import Munch
|
||||
|
||||
from mosfet.jobs.gather_crop import GatherCropStates
|
||||
|
||||
|
||||
class TestCropSectionPriority(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.job = GatherCropStates(Munch())
|
||||
self.job.bad_crops = []
|
||||
|
||||
def test_stays_in_active_patch_before_switching(self):
|
||||
wheat_a = (0, 64, 0)
|
||||
wheat_b = (1, 64, 0)
|
||||
carrot_a = (10, 64, 0)
|
||||
crop_types = {
|
||||
wheat_a: 'wheat',
|
||||
wheat_b: 'wheat',
|
||||
carrot_a: 'carrots',
|
||||
(10, 64, 1): 'carrots',
|
||||
(10, 64, -1): 'carrots',
|
||||
(11, 64, 0): 'carrots',
|
||||
}
|
||||
|
||||
first, first_size = self.job._select_crop(
|
||||
[wheat_a, carrot_a], crop_types, (0, 64, 0))
|
||||
self.assertEqual(first, wheat_a)
|
||||
self.assertEqual(first_size, 2)
|
||||
self.job.init()
|
||||
self.assertEqual(self.job.active_section_anchor, wheat_a)
|
||||
|
||||
# The carrot patch is now closer, but the bot should finish the wheat
|
||||
# patch it already selected first.
|
||||
next_crop, next_size = self.job._select_crop(
|
||||
[wheat_b, carrot_a], crop_types, (10, 64, 0))
|
||||
self.assertEqual(next_crop, wheat_b)
|
||||
self.assertEqual(next_size, 2)
|
||||
|
||||
# Once the active patch has no mature candidates, move to the next one.
|
||||
next_patch, next_patch_size = self.job._select_crop(
|
||||
[carrot_a], crop_types, (10, 64, 0))
|
||||
self.assertEqual(next_patch, carrot_a)
|
||||
self.assertEqual(next_patch_size, 4)
|
||||
|
||||
def test_farther_section_wins_when_much_more_mature(self):
|
||||
near_patch = {
|
||||
(3, 64, 0), (2, 64, 0), (1, 64, 0), (0, 64, 0),
|
||||
(3, 64, 1), (2, 64, 1), (1, 64, 1), (0, 64, 1),
|
||||
(3, 64, 2), (2, 64, 2),
|
||||
}
|
||||
near_mature = (3, 64, 0)
|
||||
far_patch = {
|
||||
(4, 64, 0), (5, 64, 0), (4, 64, 1), (5, 64, 1),
|
||||
}
|
||||
crop_types = {position: 'wheat' for position in near_patch}
|
||||
crop_types.update({position: 'carrots' for position in far_patch})
|
||||
|
||||
crop, size = self.job._select_crop(
|
||||
[near_mature, *far_patch], crop_types, (0, 64, 0))
|
||||
|
||||
self.assertEqual(crop, (4, 64, 0))
|
||||
self.assertEqual(size, len(far_patch))
|
||||
self.assertEqual(self.job.active_section_maturity, (4, 4))
|
||||
|
||||
def test_unripe_same_species_blocks_keep_patch_connected(self):
|
||||
mature = (0, 64, 0)
|
||||
replanted = (1, 64, 0)
|
||||
isolated = (4, 64, 0)
|
||||
crop_types = {
|
||||
mature: 'wheat',
|
||||
replanted: 'wheat',
|
||||
isolated: 'wheat',
|
||||
}
|
||||
|
||||
crop, size = self.job._select_crop([mature, isolated], crop_types,
|
||||
(0, 64, 0))
|
||||
|
||||
self.assertEqual(crop, mature)
|
||||
self.assertEqual(size, 2)
|
||||
self.assertEqual(self.job.active_section_anchor, mature)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,75 +0,0 @@
|
||||
import unittest
|
||||
from munch import Munch
|
||||
|
||||
from mosfet.info import blocks
|
||||
from mosfet.jobs.gather_crop import GatherCropStates
|
||||
|
||||
|
||||
class CropSectionCacheTests(unittest.TestCase):
|
||||
def make_world(self, crops):
|
||||
class FakeWorld:
|
||||
def __init__(self):
|
||||
self.crops = dict(crops)
|
||||
self.index_calls = 0
|
||||
|
||||
def find_blocks_indexed(self, _center, block_ids, _distance,
|
||||
sort=True):
|
||||
self.index_calls += 1
|
||||
return [position for position, state in self.crops.items()
|
||||
if state in block_ids]
|
||||
|
||||
@staticmethod
|
||||
def filter_work_positions(positions):
|
||||
return list(positions)
|
||||
|
||||
def block_at(self, *position):
|
||||
return self.crops.get(tuple(position), blocks.AIR)
|
||||
|
||||
return FakeWorld()
|
||||
|
||||
def test_reuses_active_section_without_rebuilding_world_indexes(self):
|
||||
first = (1, 64, 0)
|
||||
second = (2, 64, 0)
|
||||
unripe_wheat = blocks.MCD_BLOCKS['wheat']['minStateId']
|
||||
world = self.make_world({
|
||||
first: blocks.MATURE_WHEAT_ID,
|
||||
second: unripe_wheat,
|
||||
})
|
||||
state = GatherCropStates(Munch(world=world, pos=(0, 64, 0)))
|
||||
|
||||
state.find_new_crop()
|
||||
self.assertEqual(state.crop, first)
|
||||
self.assertEqual(state.active_section_positions, {first, second})
|
||||
self.assertEqual(world.index_calls, 2)
|
||||
|
||||
world.crops[first] = unripe_wheat
|
||||
world.crops[second] = blocks.MATURE_WHEAT_ID
|
||||
state.find_new_crop()
|
||||
|
||||
self.assertEqual(state.crop, second)
|
||||
self.assertEqual(world.index_calls, 2)
|
||||
self.assertEqual(state.active_section_maturity, (1, 2))
|
||||
|
||||
def test_rebuilds_cache_when_a_cached_crop_disappears(self):
|
||||
first = (1, 64, 0)
|
||||
second = (2, 64, 0)
|
||||
unripe_wheat = blocks.MCD_BLOCKS['wheat']['minStateId']
|
||||
world = self.make_world({
|
||||
first: blocks.MATURE_WHEAT_ID,
|
||||
second: unripe_wheat,
|
||||
})
|
||||
state = GatherCropStates(Munch(world=world, pos=(0, 64, 0)))
|
||||
state.find_new_crop()
|
||||
self.assertEqual(world.index_calls, 2)
|
||||
|
||||
world.crops.pop(first)
|
||||
world.crops[second] = blocks.MATURE_WHEAT_ID
|
||||
state.find_new_crop()
|
||||
|
||||
self.assertEqual(state.crop, second)
|
||||
self.assertEqual(world.index_calls, 4)
|
||||
self.assertEqual(state.active_section_positions, {second})
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,183 +0,0 @@
|
||||
import unittest
|
||||
from munch import Munch
|
||||
|
||||
from mosfet.commands import Commands
|
||||
from mosfet.info import blocks, items
|
||||
from mosfet.job import JobStates
|
||||
from mosfet.jobs.gather_wart import GatherWartStates
|
||||
|
||||
|
||||
class GatherWartTests(unittest.TestCase):
|
||||
def test_nether_wart_states_are_indexed_for_patch_search(self):
|
||||
self.assertTrue(blocks.NETHERWART_IDS.issubset(blocks.INDEXED_IDS))
|
||||
|
||||
def test_farm_wart_command_is_enabled_and_keeps_job_sequence(self):
|
||||
old_wanted = items.WANTED_ITEMS
|
||||
job = JobStates(Munch())
|
||||
|
||||
class FakeChat:
|
||||
def __init__(self):
|
||||
self.sent = []
|
||||
|
||||
def set_handler(self, _handler):
|
||||
pass
|
||||
|
||||
def send(self, message):
|
||||
self.sent.append(message)
|
||||
|
||||
chat = FakeChat()
|
||||
g = Munch(name='mosfet1', chat=chat, job=job,
|
||||
inv={0: Munch(item_id=next(iter(items.BED_IDS)))})
|
||||
try:
|
||||
Commands(g).handle_chat(('CHAT', 'tester',
|
||||
'tester: 1farm wart'))
|
||||
self.assertEqual(chat.sent, ['ok'])
|
||||
self.assertIs(job.state.__func__, job.farm_wart.__func__)
|
||||
self.assertIs(job.farm_wart()[0], job.gather_wart_states)
|
||||
self.assertIn(items.NETHERWART_ID, items.WANTED_ITEMS)
|
||||
finally:
|
||||
items.WANTED_ITEMS = old_wanted
|
||||
|
||||
def test_finds_only_indexed_mature_wart_allowed_by_work_signs(self):
|
||||
position = (5, 64, 8)
|
||||
mature = max(blocks.NETHERWART_IDS)
|
||||
|
||||
class FakeWorld:
|
||||
def __init__(self):
|
||||
self.search = None
|
||||
self.filter_input = None
|
||||
|
||||
def find_blocks_indexed(self, center, block_ids, distance):
|
||||
self.search = (center, block_ids, distance)
|
||||
return [position]
|
||||
|
||||
def filter_work_positions(self, positions):
|
||||
self.filter_input = list(positions)
|
||||
return list(positions)
|
||||
|
||||
world = FakeWorld()
|
||||
state = GatherWartStates(Munch(world=world, pos=(1.5, 64, 1.5)))
|
||||
|
||||
state.find_new_wart()
|
||||
|
||||
self.assertEqual(world.search, ((1, 64, 1), {mature}, 50))
|
||||
self.assertEqual(world.filter_input, [position])
|
||||
self.assertEqual(state.wart, position)
|
||||
self.assertIs(state.state.__func__, state.nav_to_wart.__func__)
|
||||
|
||||
def test_harvests_confirms_replant_on_soul_sand_below_wart(self):
|
||||
position = (5, 64, 8)
|
||||
mature = max(blocks.NETHERWART_IDS)
|
||||
unripe = min(blocks.NETHERWART_IDS)
|
||||
|
||||
class FakeWorld:
|
||||
def __init__(self):
|
||||
self.state_id = mature
|
||||
|
||||
def block_at(self, *_position):
|
||||
return self.state_id
|
||||
|
||||
class FakeGame:
|
||||
def __init__(self, global_state):
|
||||
self.g = global_state
|
||||
self.break_calls = []
|
||||
self.place_calls = []
|
||||
|
||||
def break_block(self, target):
|
||||
self.break_calls.append(target)
|
||||
return True
|
||||
|
||||
def select_item(self, item_ids):
|
||||
self.g.holding = items.NETHERWART_ID
|
||||
return items.NETHERWART_ID in item_ids
|
||||
|
||||
def place_block(self, target, face):
|
||||
self.place_calls.append((target, face))
|
||||
|
||||
world = FakeWorld()
|
||||
g = Munch(world=world, pos=position, holding=0, look_at=None)
|
||||
game = FakeGame(g)
|
||||
g.game = game
|
||||
state = GatherWartStates(g)
|
||||
state.wart = position
|
||||
state.type_id = mature
|
||||
|
||||
state.break_wart()
|
||||
self.assertIs(state.state.__func__, state.wait_break.__func__)
|
||||
self.assertEqual(game.break_calls, [position])
|
||||
|
||||
world.state_id = blocks.AIR
|
||||
state.wait_break()
|
||||
self.assertIs(state.state.__func__, state.select_wart.__func__)
|
||||
state.select_wart()
|
||||
state.wait_select()
|
||||
self.assertIs(state.state.__func__, state.place_wart.__func__)
|
||||
|
||||
state.place_wart()
|
||||
support = (position[0], position[1] - 1, position[2])
|
||||
self.assertEqual(game.place_calls, [(support, 1)])
|
||||
world.state_id = unripe
|
||||
state.wait_place()
|
||||
self.assertEqual(state.completed_warts, 1)
|
||||
self.assertIs(state.state.__func__, state.find_new_wart.__func__)
|
||||
|
||||
def test_navigation_looks_at_the_wart_target(self):
|
||||
position = (5, 64, 8)
|
||||
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def path_to_place(_start, _target):
|
||||
return [(1, 64, 1), position]
|
||||
|
||||
g = Munch(world=FakeWorld(), pos=(1, 64, 1), path=[], look_at=None)
|
||||
state = GatherWartStates(g)
|
||||
state.wart = position
|
||||
|
||||
state.nav_to_wart()
|
||||
|
||||
self.assertEqual(g.look_at, position)
|
||||
self.assertEqual(g.path[-1], position)
|
||||
|
||||
def test_gather_wart_finishes_after_sixteen_successful_replants(self):
|
||||
mature = max(blocks.NETHERWART_IDS)
|
||||
unripe = min(blocks.NETHERWART_IDS)
|
||||
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def block_at(*_position):
|
||||
return unripe
|
||||
|
||||
state = GatherWartStates(Munch(world=FakeWorld()))
|
||||
state.wart = (5, 64, 8)
|
||||
state.type_id = mature
|
||||
|
||||
for completed in range(1, state.WARTS_PER_BATCH):
|
||||
state.wait_place()
|
||||
self.assertEqual(state.completed_warts, completed)
|
||||
self.assertIs(state.state.__func__, state.find_new_wart.__func__)
|
||||
state.state = state.wait_place
|
||||
|
||||
state.wait_place()
|
||||
self.assertEqual(state.completed_warts, 0)
|
||||
self.assertIs(state.state.__func__, state.cleanup.__func__)
|
||||
|
||||
def test_unreachable_wart_is_blacklisted_without_undefined_variable(self):
|
||||
position = (5, 64, 8)
|
||||
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def path_to_place(_start, _target):
|
||||
return None
|
||||
|
||||
state = GatherWartStates(Munch(
|
||||
world=FakeWorld(), pos=(1, 64, 1), path=[]))
|
||||
state.wart = position
|
||||
|
||||
state.nav_to_wart()
|
||||
|
||||
self.assertIn(position, state.bad_warts)
|
||||
self.assertIs(state.state.__func__, state.find_new_wart.__func__)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,191 +0,0 @@
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from munch import Munch
|
||||
|
||||
from mosfet.bot import start_automation
|
||||
from mosfet.commands import Commands
|
||||
from mosfet.info import blocks, items
|
||||
from mosfet.job import JobStates
|
||||
from mosfet.jobs.gather_wood import GatherWoodStates
|
||||
from mosfet.jobs.grab_supplies import GrabSuppliesStates
|
||||
from mosfet.protocol.types import Slot
|
||||
from mosfet.world import World
|
||||
|
||||
|
||||
class FakeChunks:
|
||||
def __init__(self, states):
|
||||
self.states = states
|
||||
self.index = {}
|
||||
|
||||
def get_block_at(self, x, y, z):
|
||||
return self.states.get((x, y, z), blocks.AIR)
|
||||
|
||||
|
||||
class GatherWoodPortTests(unittest.TestCase):
|
||||
def test_log_states_are_indexed_for_modern_chunk_search(self):
|
||||
self.assertTrue(blocks.LOG_IDS)
|
||||
self.assertTrue(blocks.LOG_IDS.issubset(blocks.INDEXED_IDS))
|
||||
|
||||
def test_mangrove_uses_its_26_3_propagule_and_unplantable_logs_are_skipped(self):
|
||||
self.assertEqual(items.get_sapling_id('mangrove'),
|
||||
items.get_id('mangrove_propagule'))
|
||||
self.assertIsNone(items.get_sapling_id('crimson'))
|
||||
|
||||
class FakeWorld:
|
||||
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
|
||||
blocks.MCD_BLOCKS['mangrove_log']['minStateId'])
|
||||
|
||||
state = GatherWoodStates(
|
||||
Munch(world=FakeWorld(), pos=(0, 64, 0)))
|
||||
state.find_new_tree()
|
||||
self.assertEqual(state.tree, (3, 64, 0))
|
||||
self.assertEqual(state.type, 'mangrove')
|
||||
self.assertEqual(state.bad_trees, [(0, 64, 0)])
|
||||
|
||||
def test_tree_search_finds_indexed_26_3_log_states(self):
|
||||
oak_log = blocks.MCD_BLOCKS['oak_log']['minStateId']
|
||||
oak_leaves = blocks.MCD_BLOCKS['oak_leaves']['minStateId']
|
||||
stone = blocks.MCD_BLOCKS['stone']['minStateId']
|
||||
states = {
|
||||
(0, 63, 0): stone,
|
||||
(0, 64, 0): oak_log,
|
||||
(0, 65, 0): oak_log,
|
||||
(0, 66, 0): oak_log,
|
||||
(1, 66, 0): oak_leaves,
|
||||
}
|
||||
chunks = FakeChunks(states)
|
||||
chunks.index[oak_log] = {
|
||||
(0, 64, 0), (0, 65, 0), (0, 66, 0),
|
||||
}
|
||||
world = World(Munch(chunks=chunks))
|
||||
self.assertEqual(list(world.find_trees((0, 64, 0), 10)),
|
||||
[(0, 64, 0)])
|
||||
|
||||
def test_gather_wood_job_keeps_existing_machine_sequence(self):
|
||||
job = JobStates(Munch(yaw_target_override=90.0))
|
||||
self.assertIsNotNone(job.gather_wood_states)
|
||||
self.assertEqual(job.gather_wood(), [
|
||||
job.gather_wood_states,
|
||||
job.sleep_with_bed_states,
|
||||
job.eat_food_states,
|
||||
job.cache_items_states,
|
||||
])
|
||||
job.gather_wood_states.state = job.gather_wood_states.find_new_tree
|
||||
job.stop()
|
||||
self.assertIs(job.gather_wood_states.state.__func__,
|
||||
job.gather_wood_states.idle.__func__)
|
||||
self.assertIsNone(job.g.yaw_target_override)
|
||||
|
||||
def test_grab_supplies_reads_protocol_777_container_shape(self):
|
||||
axe_id = next(iter(items.AXE_IDS))
|
||||
axe = Slot(present=True, item_id=axe_id, item_count=1)
|
||||
clicks = []
|
||||
|
||||
class FakeGame:
|
||||
def count_inventory_slots(self):
|
||||
return 10
|
||||
|
||||
def click_window(self, *args):
|
||||
clicks.append(args)
|
||||
|
||||
g = Munch(
|
||||
item_lock=False,
|
||||
window=Munch(container_slots=27, contents={0: axe}),
|
||||
maximum_supply_slots=33,
|
||||
game=FakeGame(),
|
||||
)
|
||||
state = GrabSuppliesStates(g)
|
||||
state.target_items = {axe_id}
|
||||
state.maximum_stacks = 9
|
||||
state.grab_items()
|
||||
|
||||
self.assertTrue(g.item_lock)
|
||||
self.assertEqual(state.count, 1)
|
||||
self.assertEqual(clicks, [(0, 0, 1, axe)])
|
||||
|
||||
def test_modern_automation_sets_supply_inventory_limit(self):
|
||||
g = Munch()
|
||||
with patch('mosfet.bot.threading.Thread') as thread:
|
||||
start_automation(g, initialize=False)
|
||||
self.assertEqual(g.maximum_supply_slots, 33)
|
||||
thread.return_value.start.assert_called_once()
|
||||
|
||||
def test_farm_wood_job_keeps_legacy_machine_sequence(self):
|
||||
job = JobStates(Munch())
|
||||
try:
|
||||
self.assertEqual(job.farm_wood(), [
|
||||
job.grab_supplies_states,
|
||||
job.gather_wood_states,
|
||||
job.clear_leaves_states,
|
||||
job.plant_tree_states,
|
||||
job.grab_sapling_states,
|
||||
job.sleep_with_bed_states,
|
||||
job.eat_food_states,
|
||||
job.cache_items_states,
|
||||
])
|
||||
self.assertEqual(job.grab_supplies_states.supplies, {
|
||||
tuple(items.AXE_IDS): (1, 9),
|
||||
tuple(items.BED_IDS): (1, 1),
|
||||
(items.CHEST_ID,): (1, 1),
|
||||
tuple(items.FOOD_IDS): (1, 1),
|
||||
})
|
||||
self.assertTrue(job.sleep_with_bed_states.silent)
|
||||
self.assertTrue(job.cache_items_states.silent)
|
||||
self.assertTrue(items.AXE_IDS.issubset(items.NEEDED_ITEMS))
|
||||
self.assertTrue(items.SAPLING_IDS.issubset(items.WANTED_ITEMS))
|
||||
|
||||
job.grab_supplies_states.state = job.grab_supplies_states.find_supply_chests
|
||||
job.clear_leaves_states.state = job.clear_leaves_states.find_leaves
|
||||
job.plant_tree_states.state = job.plant_tree_states.check_feet
|
||||
job.grab_sapling_states.state = job.grab_sapling_states.find_saplings
|
||||
job.stop()
|
||||
for machine in (job.grab_supplies_states,
|
||||
job.clear_leaves_states,
|
||||
job.plant_tree_states,
|
||||
job.grab_sapling_states):
|
||||
self.assertIs(machine.state.__func__, machine.idle.__func__)
|
||||
finally:
|
||||
items.set_needed(set())
|
||||
items.set_wanted(set())
|
||||
|
||||
def test_gather_and_farm_wood_commands_are_enabled(self):
|
||||
class FakeChat:
|
||||
def __init__(self):
|
||||
self.sent = []
|
||||
|
||||
def set_handler(self, handler):
|
||||
self.handler = handler
|
||||
|
||||
def send(self, message):
|
||||
self.sent.append(message)
|
||||
|
||||
job = JobStates(Munch())
|
||||
chat = FakeChat()
|
||||
g = Munch(name='mosfet1', chat=chat, job=job,
|
||||
inv={0: Munch(item_id=next(iter(items.BED_IDS)))})
|
||||
Commands(g).handle_chat(('CHAT', 'tester', 'tester: 1gather wood'))
|
||||
self.assertEqual(chat.sent, ['ok'])
|
||||
self.assertIs(job.state.__func__, job.gather_wood.__func__)
|
||||
|
||||
chat.sent.clear()
|
||||
Commands(g).handle_chat(('CHAT', 'tester', 'tester: 1farm wood'))
|
||||
self.assertEqual(chat.sent, ['ok'])
|
||||
self.assertIs(job.state.__func__, job.farm_wood.__func__)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,243 +0,0 @@
|
||||
import unittest
|
||||
from munch import Munch
|
||||
|
||||
from mosfet.info import items
|
||||
from mosfet.jobs.grab_supplies import GrabSuppliesStates
|
||||
|
||||
|
||||
class GroundSupplyTests(unittest.TestCase):
|
||||
def test_default_needed_supplies_are_configured_by_constructor(self):
|
||||
state = GrabSuppliesStates(Munch())
|
||||
self.assertEqual(items.FOOD, [
|
||||
'cooked_porkchop', 'cooked_beef', 'bread', 'cooked_chicken',
|
||||
'cooked_cod', 'cooked_salmon',
|
||||
])
|
||||
self.assertEqual(items.FOOD_IDS, items.get_set({
|
||||
'cooked_porkchop', 'cooked_beef', 'bread', 'cooked_chicken',
|
||||
'cooked_cod', 'cooked_salmon',
|
||||
}))
|
||||
self.assertEqual(state.supplies, {
|
||||
tuple(items.BED_IDS): (1, 1),
|
||||
(items.CHEST_ID,): (1, 1),
|
||||
tuple(items.FOOD_IDS): (1, 1),
|
||||
})
|
||||
|
||||
def test_chest_drop_is_checked_even_when_bed_is_missing(self):
|
||||
chest = Munch(entity_id=8, item_id=items.CHEST_ID,
|
||||
x=2.0, y=64.0, z=0.0)
|
||||
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def find_objects(item_ids):
|
||||
return [chest] if items.CHEST_ID in item_ids else []
|
||||
|
||||
class FakeGame:
|
||||
@staticmethod
|
||||
def count_inventory_slots():
|
||||
return 0
|
||||
|
||||
@staticmethod
|
||||
def count_items(_item_ids):
|
||||
return 0
|
||||
|
||||
state = GrabSuppliesStates(Munch(
|
||||
pos=(0.5, 64.0, 0.5), world=FakeWorld(), game=FakeGame(),
|
||||
maximum_supply_slots=33))
|
||||
state.init()
|
||||
state.check_supplies()
|
||||
state.find_ground_items()
|
||||
self.assertIn(tuple(items.BED_IDS), state.checked_ground_groups)
|
||||
state.check_supplies()
|
||||
|
||||
self.assertEqual(state.target_items, (items.CHEST_ID,))
|
||||
self.assertIs(state.state.__func__, state.find_ground_items.__func__)
|
||||
|
||||
def test_grab_supplies_leaves_bed_drop_near_sibling_bot(self):
|
||||
bed = Munch(
|
||||
entity_id=13, item_id=next(iter(items.BED_IDS)),
|
||||
x=1.0, y=64.0, z=0.0)
|
||||
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def find_objects(_item_ids):
|
||||
return [bed]
|
||||
|
||||
@staticmethod
|
||||
def path_to_place(*_args):
|
||||
raise AssertionError('must not path to another bot\'s bed')
|
||||
|
||||
class FakeGame:
|
||||
@staticmethod
|
||||
def count_items(_item_ids):
|
||||
return 0
|
||||
|
||||
g = Munch(
|
||||
name='mosfet1', pos=(0.5, 64.0, 0.5), world=FakeWorld(),
|
||||
game=FakeGame(), players={
|
||||
2: Munch(player_uuid='other-uuid', x=2.0, y=64.0, z=0.0),
|
||||
}, player_names={'other-uuid': 'mosfet2'}, objects={13: bed},
|
||||
path=[], ground_stack_counts={},
|
||||
)
|
||||
state = GrabSuppliesStates(g)
|
||||
state.checked_ground_items = set()
|
||||
state.checked_ground_groups = set()
|
||||
state.ground_stack_counts = {}
|
||||
state.target_items = tuple(items.BED_IDS)
|
||||
state.target_maximum_stacks = 1
|
||||
|
||||
self.assertFalse(state._has_nearby_ground_items(
|
||||
state.target_items, state.target_maximum_stacks))
|
||||
state.find_ground_items()
|
||||
|
||||
self.assertIn(state.target_items, state.checked_ground_groups)
|
||||
self.assertIs(state.state.__func__, state.check_supplies.__func__)
|
||||
|
||||
def test_supply_selection_uses_only_addressed_supply_chests(self):
|
||||
position = (4, 64, 0)
|
||||
chest_record = {
|
||||
'position': position,
|
||||
'positions': (position,),
|
||||
'labels': {'mosfet supplies'},
|
||||
'sign_positions': {(4, 64, 1)},
|
||||
}
|
||||
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def find_supply_chests(_center, _bot_name, _distance):
|
||||
return [chest_record]
|
||||
|
||||
@staticmethod
|
||||
def find_blocks_indexed(*_args):
|
||||
raise AssertionError('grab_supplies must not search barrels')
|
||||
|
||||
state = GrabSuppliesStates(Munch(
|
||||
name='mosfet1', pos=(0.5, 64.0, 0.5), world=FakeWorld()))
|
||||
state.find_supply_chests()
|
||||
state.choose_supply_chest()
|
||||
|
||||
self.assertEqual(state.chest, position)
|
||||
self.assertIs(state.state.__func__, state.path_to_supply_chest.__func__)
|
||||
|
||||
def test_empty_supply_chest_is_skipped_until_another_player_opens_it(self):
|
||||
position = (4, 64, 0)
|
||||
key = (position,)
|
||||
event = {'position': position, 'timestamp': 1.0}
|
||||
g = Munch(chest_open_events=[])
|
||||
state = GrabSuppliesStates(g)
|
||||
state.supplies = {(999999,): (1, 1)}
|
||||
state.current_supply_chest_key = key
|
||||
|
||||
class FakeGame:
|
||||
@staticmethod
|
||||
def count_items(_item_ids):
|
||||
return 1
|
||||
|
||||
state.g.game = FakeGame()
|
||||
state.choose_items()
|
||||
self.assertIn(key, state.useless_chests)
|
||||
self.assertIs(state.state.__func__, state.close_supply_chest.__func__)
|
||||
|
||||
second_position = (8, 64, 0)
|
||||
state.supply_chests = [
|
||||
{'position': position, 'positions': key},
|
||||
{'position': second_position, 'positions': (second_position,)},
|
||||
]
|
||||
state.checked_supply_chests = {key}
|
||||
state.choose_supply_chest()
|
||||
self.assertEqual(state.chest, second_position)
|
||||
|
||||
g.chest_open_events.append(event)
|
||||
state.state = state.idle
|
||||
state.run()
|
||||
self.assertNotIn(key, state.useless_chests)
|
||||
self.assertNotIn(key, state.checked_supply_chests)
|
||||
|
||||
state.supply_chests = [{'position': position, 'positions': key}]
|
||||
state.choose_supply_chest()
|
||||
self.assertEqual(state.chest, position)
|
||||
|
||||
def test_unreachable_supply_chest_ends_task_without_searching_barrels(self):
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def path_to_place_faked(_start, _chest):
|
||||
return None
|
||||
|
||||
position = (10, 64, 10)
|
||||
state = GrabSuppliesStates(Munch(
|
||||
pos=(0.5, 64.0, 0.5), world=FakeWorld(), chunks=Munch()))
|
||||
state.supply_chests = [{
|
||||
'position': position, 'positions': (position,),
|
||||
'labels': {'mosfet supplies'},
|
||||
}]
|
||||
state.choose_supply_chest()
|
||||
state.path_to_supply_chest()
|
||||
state.choose_supply_chest()
|
||||
|
||||
self.assertIs(state.state.__func__, state.cleanup.__func__)
|
||||
|
||||
def test_job_walks_to_and_picks_up_dropped_supply_before_barrels(self):
|
||||
axe_id = next(iter(items.AXE_IDS))
|
||||
dropped_axe = Munch(
|
||||
entity_id=7, item_id=axe_id, item_count=1,
|
||||
x=3.2, y=64.0, z=0.2)
|
||||
|
||||
class FakeWorld:
|
||||
def __init__(self):
|
||||
self.path_requests = []
|
||||
|
||||
def find_objects(self, item_ids):
|
||||
return ([dropped_axe] if axe_id in item_ids else [])
|
||||
|
||||
def path_to_place(self, start, target):
|
||||
self.path_requests.append((start, target))
|
||||
return [start, target]
|
||||
|
||||
class FakeGame:
|
||||
def __init__(self):
|
||||
self.item_count = 0
|
||||
|
||||
def count_inventory_slots(self):
|
||||
return 5
|
||||
|
||||
def count_items(self, item_ids):
|
||||
return self.item_count if axe_id in item_ids else 0
|
||||
|
||||
world = FakeWorld()
|
||||
game = FakeGame()
|
||||
g = Munch(
|
||||
pos=(0.5, 64.0, 0.5), world=world, game=game,
|
||||
chunks=Munch(), objects={7: dropped_axe}, path=[],
|
||||
maximum_supply_slots=33,
|
||||
)
|
||||
state = GrabSuppliesStates(g)
|
||||
state.supplies = {tuple(items.AXE_IDS): (1, 9)}
|
||||
|
||||
state.init()
|
||||
state.check_supplies()
|
||||
state.find_ground_items()
|
||||
self.assertIs(state.state.__func__, state.going_to_ground_item.__func__)
|
||||
self.assertEqual(g.path[-1], (3, 64, 0))
|
||||
self.assertEqual(world.path_requests, [((0, 64, 0), (3, 64, 0))])
|
||||
|
||||
g.pos = (3.5, 64.0, 0.5)
|
||||
state.going_to_ground_item()
|
||||
self.assertIs(state.state.__func__, state.wait_for_ground_item.__func__)
|
||||
|
||||
game.item_count = 1
|
||||
state.wait_for_ground_item()
|
||||
self.assertIs(state.state.__func__, state.check_supplies.__func__)
|
||||
state.check_supplies()
|
||||
self.assertIs(state.state.__func__, state.cleanup.__func__)
|
||||
|
||||
# Nearby dropped tools are collected even when inventory already meets
|
||||
# the minimum, so tossing an axe during setup still gets noticed.
|
||||
game.item_count = 1
|
||||
state.init()
|
||||
state.check_supplies()
|
||||
self.assertIs(state.state.__func__, state.find_ground_items.__func__)
|
||||
state.find_ground_items()
|
||||
self.assertEqual(state.ground_required_count, 2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,205 +0,0 @@
|
||||
import unittest
|
||||
from munch import Munch
|
||||
|
||||
from mosfet.info import items
|
||||
from mosfet.jobs.grab_supplies import GrabSuppliesStates
|
||||
from mosfet.protocol.types import Slot
|
||||
|
||||
|
||||
class InventorySupplyRecoveryTests(unittest.TestCase):
|
||||
def make_inventory(self):
|
||||
return {
|
||||
36: Slot(True, next(iter(items.BED_IDS)), 1),
|
||||
37: Slot(True, items.CHEST_ID, 32),
|
||||
38: Slot(True, next(iter(items.FOOD_IDS)), 64),
|
||||
39: Slot(True, next(iter(items.AXE_IDS)), 1),
|
||||
40: Slot(True, items.SAND_ID, 16),
|
||||
41: Slot(True, items.get_id('stone'), 64),
|
||||
42: Slot(True, items.get_id('dirt'), 4),
|
||||
43: Slot(True, items.get_id('stick'), 1),
|
||||
44: Slot(True, items.get_id('cobblestone'), 2),
|
||||
}
|
||||
|
||||
def with_needed_axes(self, callback):
|
||||
previous_needed = items.NEEDED_ITEMS
|
||||
try:
|
||||
items.set_needed(items.AXE_IDS)
|
||||
callback()
|
||||
finally:
|
||||
items.NEEDED_ITEMS = previous_needed
|
||||
|
||||
def test_preserves_essential_stacks_and_selects_largest_junk_stack(self):
|
||||
def run():
|
||||
g = Munch(inv=self.make_inventory(), drop_waiting=None)
|
||||
state = GrabSuppliesStates(g)
|
||||
self.assertEqual(state._protected_inventory_slots(),
|
||||
{36, 37, 38, 39})
|
||||
self.assertEqual(state._least_important_inventory_slot(
|
||||
hotbar_only=True), 41)
|
||||
self.assertEqual(state._least_important_inventory_slot(), 41)
|
||||
|
||||
self.with_needed_axes(run)
|
||||
|
||||
def test_full_container_transfer_swaps_instead_of_aborting(self):
|
||||
def run():
|
||||
axe_id = next(iter(items.AXE_IDS))
|
||||
swaps = []
|
||||
|
||||
class FakeGame:
|
||||
def count_inventory_slots(self):
|
||||
return 33
|
||||
|
||||
def swap_container_slot_with_hotbar(self, container_slot,
|
||||
inventory_slot):
|
||||
swaps.append((container_slot, inventory_slot))
|
||||
return True
|
||||
|
||||
g = Munch(
|
||||
inv=self.make_inventory(), item_lock=False,
|
||||
window=Munch(container_slots=27, contents={
|
||||
0: Slot(True, axe_id, 1),
|
||||
}),
|
||||
maximum_supply_slots=33, game=FakeGame(),
|
||||
)
|
||||
state = GrabSuppliesStates(g)
|
||||
state.target_items = {axe_id}
|
||||
state.maximum_stacks = 1
|
||||
state.grab_items()
|
||||
|
||||
self.assertEqual(swaps, [(0, 41)])
|
||||
self.assertTrue(g.item_lock)
|
||||
self.assertEqual(state.count, 1)
|
||||
|
||||
self.with_needed_axes(run)
|
||||
|
||||
def test_full_ground_pickup_frees_room_and_waits_for_target(self):
|
||||
def run():
|
||||
class FakeGame:
|
||||
def __init__(self):
|
||||
self.inventory_slots = 33
|
||||
self.target_count = 0
|
||||
self.dropped = []
|
||||
|
||||
def count_inventory_slots(self):
|
||||
return self.inventory_slots
|
||||
|
||||
def count_items(self, _item_ids):
|
||||
return self.target_count
|
||||
|
||||
def drop_inventory_slot(self, slot, whole_stack=True):
|
||||
self.dropped.append((slot, whole_stack))
|
||||
return True
|
||||
|
||||
target_id = items.NETHERWART_ID
|
||||
obj = Munch(entity_id=99, item_id=target_id,
|
||||
x=0.5, y=64.0, z=0.5)
|
||||
game = FakeGame()
|
||||
g = Munch(
|
||||
inv=self.make_inventory(), drop_waiting=None,
|
||||
objects={99: obj}, path=[], pos=(0.5, 64, 0.5),
|
||||
maximum_supply_slots=33, game=game,
|
||||
)
|
||||
state = GrabSuppliesStates(g)
|
||||
state.checked_ground_items = set()
|
||||
state.checked_ground_groups = set()
|
||||
state.ground_stack_counts = {}
|
||||
state.target_items = (target_id,)
|
||||
state.target_minimum = 1
|
||||
state.target_maximum_stacks = 1
|
||||
state.ground_entity_id = 99
|
||||
state.ground_position = (0, 64, 0)
|
||||
state.ground_required_count = 1
|
||||
|
||||
state.going_to_ground_item()
|
||||
self.assertEqual(game.dropped, [(41, True)])
|
||||
self.assertIs(state.state.__func__,
|
||||
state.wait_for_inventory_space.__func__)
|
||||
|
||||
g.drop_waiting = None
|
||||
game.inventory_slots = 32
|
||||
state.wait_for_inventory_space()
|
||||
self.assertIs(state.state.__func__,
|
||||
state.wait_for_ground_item.__func__)
|
||||
|
||||
game.target_count = 1
|
||||
state.wait_for_ground_item()
|
||||
self.assertEqual(state.ground_stack_counts[(target_id,)], 1)
|
||||
self.assertIs(state.state.__func__, state.check_supplies.__func__)
|
||||
|
||||
self.with_needed_axes(run)
|
||||
|
||||
def test_container_swap_can_stage_a_main_inventory_stack_through_hotbar(self):
|
||||
previous_needed = items.NEEDED_ITEMS
|
||||
previous_wanted = items.WANTED_ITEMS
|
||||
try:
|
||||
items.set_needed(items.AXE_IDS | items.SHOVEL_IDS)
|
||||
saplings = list(items.SAPLING_IDS)[:4]
|
||||
items.set_wanted(set(saplings))
|
||||
axes = next(iter(items.AXE_IDS))
|
||||
inventory = {
|
||||
36: Slot(True, next(iter(items.BED_IDS)), 1),
|
||||
37: Slot(True, items.CHEST_ID, 1),
|
||||
38: Slot(True, next(iter(items.FOOD_IDS)), 64),
|
||||
39: Slot(True, axes, 1),
|
||||
40: Slot(True, next(iter(items.SHOVEL_IDS)), 1),
|
||||
41: Slot(True, saplings[0], 1),
|
||||
42: Slot(True, saplings[1], 1),
|
||||
43: Slot(True, saplings[2], 1),
|
||||
44: Slot(True, saplings[3], 1),
|
||||
9: Slot(True, items.get_id('stone'), 64),
|
||||
}
|
||||
swaps = []
|
||||
|
||||
class FakeGame:
|
||||
def count_inventory_slots(self):
|
||||
return 33
|
||||
|
||||
def swap_container_slot_with_hotbar(self, slot, hotbar):
|
||||
swaps.append((slot, hotbar))
|
||||
return True
|
||||
|
||||
target_id = items.PUMPKIN_ID
|
||||
g = Munch(
|
||||
inv=inventory, item_lock=False,
|
||||
window=Munch(container_slots=27, contents={
|
||||
0: Slot(True, target_id, 1),
|
||||
}),
|
||||
maximum_supply_slots=33, game=FakeGame(),
|
||||
)
|
||||
state = GrabSuppliesStates(g)
|
||||
state.target_items = {target_id}
|
||||
state.maximum_stacks = 1
|
||||
state.grab_items()
|
||||
|
||||
self.assertEqual(swaps, [(27, 36)])
|
||||
self.assertTrue(g.item_lock)
|
||||
self.assertEqual(state.count, 0)
|
||||
finally:
|
||||
items.NEEDED_ITEMS = previous_needed
|
||||
items.WANTED_ITEMS = previous_wanted
|
||||
|
||||
def test_full_ground_pickup_drops_largest_nonessential_stack(self):
|
||||
def run():
|
||||
dropped = []
|
||||
|
||||
class FakeGame:
|
||||
def drop_inventory_slot(self, slot, whole_stack=True):
|
||||
dropped.append((slot, whole_stack))
|
||||
return True
|
||||
|
||||
g = Munch(
|
||||
inv=self.make_inventory(), drop_waiting=None, path=[],
|
||||
game=FakeGame(),
|
||||
)
|
||||
state = GrabSuppliesStates(g)
|
||||
state.target_items = {items.NETHERWART_ID}
|
||||
self.assertTrue(state._drop_stack_for_ground_pickup())
|
||||
self.assertEqual(dropped, [(41, True)])
|
||||
self.assertIs(state.state.__func__,
|
||||
state.wait_for_inventory_space.__func__)
|
||||
|
||||
self.with_needed_axes(run)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -6,7 +6,6 @@ from minecraft.networking.packets import PacketBuffer
|
||||
|
||||
from mosfet.game import Game
|
||||
from mosfet.info import items
|
||||
from mosfet.jobs.eat_food import EatFoodStates
|
||||
from mosfet.protocol.modern import UseItemPacket
|
||||
from mosfet.protocol.types import Slot
|
||||
|
||||
@@ -19,25 +18,7 @@ class FakeConnection:
|
||||
self.packets.append((packet, force))
|
||||
|
||||
|
||||
class FakeGame:
|
||||
def __init__(self, state):
|
||||
self.state = state
|
||||
self.used_hands = []
|
||||
|
||||
def select_item(self, item_ids):
|
||||
for slot, item in self.state.g.inv.items():
|
||||
if item.present and item.item_id in item_ids:
|
||||
self.state.g.holding = item.item_id
|
||||
self.state.g.holding_hand = 0
|
||||
self.state.g.holding_slot = slot - 36
|
||||
return True
|
||||
return False
|
||||
|
||||
def use_item(self, hand=0):
|
||||
self.used_hands.append(hand)
|
||||
|
||||
|
||||
class TestEatFood(unittest.TestCase):
|
||||
class TestItemUsePackets(unittest.TestCase):
|
||||
def test_use_item_packet_encodes_hand_sequence_and_rotation(self):
|
||||
packet = UseItemPacket(hand=1, sequence=300, yaw=90.0, pitch=-30.0)
|
||||
buffer = PacketBuffer()
|
||||
@@ -80,39 +61,6 @@ class TestEatFood(unittest.TestCase):
|
||||
self.assertTrue(forced)
|
||||
self.assertIsInstance(connection.packets[1][0], ClientTickEnd)
|
||||
|
||||
def test_eat_uses_authoritative_hunger_and_waits_for_confirmation(self):
|
||||
food_id = next(iter(items.FOOD_IDS))
|
||||
state = EatFoodStates(Munch(
|
||||
food=8, health=20.0, food_saturation=0.0,
|
||||
inv={36: Slot(True, food_id, 3)}, holding_hand=0,
|
||||
holding_slot=0, job=None))
|
||||
fake_game = FakeGame(state)
|
||||
state.g.game = fake_game
|
||||
|
||||
state.init()
|
||||
self.assertEqual(state.state, state.select_food)
|
||||
state.select_food()
|
||||
self.assertEqual(state.state, state.wait_for_selection)
|
||||
state.wait_for_selection()
|
||||
self.assertEqual(state.state, state.eat_food)
|
||||
state.eat_food()
|
||||
self.assertEqual(fake_game.used_hands, [0])
|
||||
self.assertEqual(state.state, state.wait)
|
||||
|
||||
# Don't declare success until the server sends a hunger/saturation
|
||||
# update or confirms that it consumed the selected stack.
|
||||
state.wait()
|
||||
self.assertEqual(state.state, state.wait)
|
||||
state.g.food = 16
|
||||
state.g.food_saturation = 4.0
|
||||
state.wait()
|
||||
self.assertEqual(state.state, state.cleanup)
|
||||
|
||||
def test_no_hunger_packet_does_not_eat(self):
|
||||
state = EatFoodStates(Munch(food=None, health=None))
|
||||
state.init()
|
||||
self.assertEqual(state.state, state.cleanup)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,21 +0,0 @@
|
||||
import unittest
|
||||
from munch import Munch
|
||||
|
||||
from mosfet.jobs.plant_tree import PlantTreeStates
|
||||
|
||||
|
||||
class PlantTreeTests(unittest.TestCase):
|
||||
def test_missing_inventory_sapling_aborts_without_crashing(self):
|
||||
class FakeGame:
|
||||
@staticmethod
|
||||
def select_item(_item_ids):
|
||||
return False
|
||||
|
||||
state = PlantTreeStates(Munch(
|
||||
pos=(0, 64, 0), chopped_tree='oak', game=FakeGame()))
|
||||
state.select_sapling()
|
||||
self.assertIs(state.state.__func__, state.cleanup.__func__)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,71 +0,0 @@
|
||||
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,
|
||||
sort=True):
|
||||
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.completed_crops = 7
|
||||
state.find_new_crop()
|
||||
|
||||
self.assertIs(state.state.__func__, state.cleanup.__func__)
|
||||
self.assertEqual(state.completed_crops, 0)
|
||||
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()
|
||||
@@ -1,534 +0,0 @@
|
||||
import io
|
||||
import unittest
|
||||
from contextlib import redirect_stdout
|
||||
from unittest.mock import patch
|
||||
|
||||
from munch import Munch
|
||||
from minecraft.networking.packets import PacketBuffer
|
||||
from minecraft.networking.types import Float, Long, VarInt, VarLong
|
||||
|
||||
from mosfet.game import Game
|
||||
from mosfet.info import blocks, items
|
||||
from mosfet.protocol.types import Slot
|
||||
from mosfet.jobs.sleep_with_bed import SleepWithBedStates
|
||||
from mosfet.world import World
|
||||
from mosfet.protocol.modern import PlayerCommandPacket, UpdateTimePacket
|
||||
from minecraft.networking.modern import ClientTickEnd, UseItemOnBlockPacket
|
||||
|
||||
|
||||
class FakeConnection:
|
||||
def __init__(self):
|
||||
self.packets = []
|
||||
|
||||
def write_packet(self, packet, force=False):
|
||||
self.packets.append((packet, force))
|
||||
|
||||
|
||||
class FakeChunks:
|
||||
def __init__(self, block):
|
||||
self.block = block
|
||||
|
||||
def get_block_at(self, *position):
|
||||
return self.block
|
||||
|
||||
|
||||
class FakeWorld:
|
||||
def __init__(self, block=blocks.AIR):
|
||||
self.block = block
|
||||
|
||||
def block_at(self, *position):
|
||||
return self.block
|
||||
|
||||
def check_bed_occupied(self, position):
|
||||
return False
|
||||
|
||||
|
||||
class FakeGame:
|
||||
def __init__(self):
|
||||
self.placements = []
|
||||
|
||||
def place_block(self, location, face, hand=0, cursor_y=None):
|
||||
self.placements.append((location, face, hand, cursor_y))
|
||||
|
||||
|
||||
class TestSleepWithBed(unittest.TestCase):
|
||||
def test_confirmed_sleep_records_bed_for_following_cache_pass(self):
|
||||
bed = (1, 64, 2)
|
||||
g = Munch(world=FakeWorld(), path=[], look_at=None)
|
||||
state = SleepWithBedStates(g)
|
||||
state.area = bed
|
||||
state.sleep_confirmed = True
|
||||
|
||||
state.cleanup()
|
||||
|
||||
self.assertEqual(g.post_sleep_cache_bed, bed)
|
||||
|
||||
def test_unconfirmed_sleep_does_not_trigger_post_sleep_cache(self):
|
||||
g = Munch(world=FakeWorld(), path=[], look_at=None)
|
||||
state = SleepWithBedStates(g)
|
||||
state.area = (1, 64, 2)
|
||||
state.sleep_confirmed = False
|
||||
|
||||
state.cleanup()
|
||||
|
||||
self.assertNotIn('post_sleep_cache_bed', g)
|
||||
|
||||
def test_bed_occupancy_only_logs_first_observation_and_changes(self):
|
||||
bed_states = [
|
||||
state for state in blocks.BED_IDS
|
||||
if blocks.BLOCKS[state] == 'white_bed' and
|
||||
blocks.PROPS[state].get('part') == 'foot' and
|
||||
blocks.PROPS[state].get('facing') == 'north'
|
||||
]
|
||||
free = next(state for state in bed_states
|
||||
if blocks.PROPS[state].get('occupied') == 'false')
|
||||
occupied = next(state for state in bed_states
|
||||
if blocks.PROPS[state].get('occupied') == 'true')
|
||||
chunks = FakeChunks(free)
|
||||
world = World(Munch(chunks=chunks))
|
||||
output = io.StringIO()
|
||||
|
||||
with redirect_stdout(output):
|
||||
self.assertFalse(world.check_bed_occupied((1, 64, 2)))
|
||||
self.assertFalse(world.check_bed_occupied((1, 64, 2)))
|
||||
chunks.block = occupied
|
||||
self.assertTrue(world.check_bed_occupied((1, 64, 2)))
|
||||
self.assertTrue(world.check_bed_occupied((1, 64, 2)))
|
||||
|
||||
lines = output.getvalue().splitlines()
|
||||
self.assertEqual(len(lines), 2)
|
||||
self.assertIn('-> free', lines[0])
|
||||
self.assertIn('-> occupied', lines[1])
|
||||
|
||||
def test_update_time_packet_decodes_clock_updates(self):
|
||||
payload = PacketBuffer()
|
||||
Long.send(123456, payload)
|
||||
VarInt.send(1, payload)
|
||||
VarInt.send(0, payload)
|
||||
VarLong.send(13000, payload)
|
||||
Float.send(0.5, payload)
|
||||
Float.send(1.0, payload)
|
||||
|
||||
packet = UpdateTimePacket()
|
||||
packet.read(io.BytesIO(payload.get_writable()))
|
||||
self.assertEqual(packet.id, 0x73)
|
||||
self.assertEqual(packet.age, 123456)
|
||||
self.assertEqual(packet.clock_updates, [{
|
||||
'clock_id': 0, 'total_ticks': 13000,
|
||||
'partial_tick': 0.5, 'rate': 1.0,
|
||||
}])
|
||||
|
||||
def test_game_advances_world_clock_between_server_updates(self):
|
||||
game = Game.__new__(Game)
|
||||
game.g = Munch(
|
||||
time_base=None, time_update_received_at=None, clock_rate=1.0,
|
||||
time=0.0,
|
||||
connection=Munch(registry_ids={'minecraft:world_clock': {
|
||||
'minecraft:the_end': 1,
|
||||
'minecraft:overworld': 0,
|
||||
}}),
|
||||
dimension='overworld')
|
||||
packet = Munch(age=900000, clock_updates=[
|
||||
{'clock_id': 1, 'total_ticks': 6000,
|
||||
'partial_tick': 0.0, 'rate': 1.0},
|
||||
{'clock_id': 0, 'total_ticks': 13000,
|
||||
'partial_tick': 0.5, 'rate': 1.0},
|
||||
])
|
||||
|
||||
with patch('mosfet.game.time.monotonic', return_value=100.0):
|
||||
game.handle_time_update(packet)
|
||||
self.assertEqual(game.g.time, 13000.5)
|
||||
self.assertEqual(game.g.clock_name, 'minecraft:overworld')
|
||||
|
||||
with patch('mosfet.game.time.monotonic', return_value=100.5):
|
||||
game._advance_world_time()
|
||||
|
||||
self.assertEqual(game.g.time, 13010.5)
|
||||
|
||||
def test_player_command_encodes_stop_sleeping(self):
|
||||
packet = PlayerCommandPacket(
|
||||
entity_id=7, action_id=0, jump_boost=0)
|
||||
payload = PacketBuffer()
|
||||
packet.write_fields(payload)
|
||||
self.assertEqual(packet.id, 0x2A)
|
||||
self.assertEqual(payload.get_writable(), b'\x07\x00\x00')
|
||||
|
||||
def test_game_leave_bed_sends_protocol_777_stop_sleeping(self):
|
||||
connection = FakeConnection()
|
||||
game = Game.__new__(Game)
|
||||
game.g = Munch(connection=connection, eid=42)
|
||||
|
||||
game.leave_bed()
|
||||
|
||||
packet, force = connection.packets[0]
|
||||
self.assertIsInstance(packet, PlayerCommandPacket)
|
||||
self.assertEqual((packet.entity_id, packet.action_id,
|
||||
packet.jump_boost), (42, 0, 0))
|
||||
self.assertTrue(force)
|
||||
self.assertIsInstance(connection.packets[1][0], ClientTickEnd)
|
||||
|
||||
def test_existing_bed_use_aims_inside_the_short_bed_collision_shape(self):
|
||||
connection = FakeConnection()
|
||||
game = Game.__new__(Game)
|
||||
game.g = Munch(connection=connection)
|
||||
game.sequence = 0
|
||||
|
||||
game.place_block((-175, 70, -312), 1, cursor_y=0.5)
|
||||
|
||||
packet = connection.packets[0][0]
|
||||
self.assertIsInstance(packet, UseItemOnBlockPacket)
|
||||
self.assertEqual(packet.location, (-175, 70, -312))
|
||||
self.assertEqual(packet.cursor_y, 0.5)
|
||||
self.assertIsInstance(connection.packets[1][0], ClientTickEnd)
|
||||
|
||||
def test_bed_in_offhand_is_selectable_without_swapping_inventory(self):
|
||||
connection = FakeConnection()
|
||||
game = Game.__new__(Game)
|
||||
bed_id = next(iter(items.BED_IDS))
|
||||
game.g = Munch(
|
||||
connection=connection, inv={45: Slot(True, bed_id, 1)},
|
||||
holding_hand=0)
|
||||
|
||||
self.assertTrue(game.select_item(items.BED_IDS))
|
||||
self.assertEqual(game.g.holding, bed_id)
|
||||
self.assertEqual(game.g.holding_hand, 1)
|
||||
self.assertIsInstance(connection.packets[0][0], ClientTickEnd)
|
||||
|
||||
def test_bed_head_position_is_normalized_to_foot(self):
|
||||
bed_name = 'white_bed'
|
||||
foot_state = next(
|
||||
state for state in blocks.BED_IDS
|
||||
if blocks.BLOCKS[state] == bed_name and
|
||||
blocks.PROPS[state].get('part') == 'foot' and
|
||||
blocks.PROPS[state].get('facing') == 'west')
|
||||
head_state = next(
|
||||
state for state in blocks.BED_IDS
|
||||
if blocks.BLOCKS[state] == bed_name and
|
||||
blocks.PROPS[state].get('part') == 'head' and
|
||||
blocks.PROPS[state].get('facing') == 'west')
|
||||
foot, head = (-175, 70, -312), (-176, 70, -312)
|
||||
|
||||
class BedWorld:
|
||||
def block_at(self, *position):
|
||||
return {foot: foot_state, head: head_state}.get(tuple(position), blocks.AIR)
|
||||
|
||||
state = SleepWithBedStates(Munch(world=BedWorld()))
|
||||
self.assertEqual(state._bed_foot(head), foot)
|
||||
self.assertEqual(state._bed_foot(foot), foot)
|
||||
|
||||
def test_bed_search_falls_back_to_loaded_blocks_if_index_is_empty(self):
|
||||
bed = (1, 64, 0)
|
||||
bed_state = next(
|
||||
state for state in blocks.BED_IDS
|
||||
if blocks.PROPS[state].get('part') == 'foot')
|
||||
|
||||
class IndexedWorld:
|
||||
def find_blocks_indexed(self, center, block_ids, distance=0):
|
||||
return []
|
||||
|
||||
def find_blocks_3d(self, center, block_ids, distance, y_limit):
|
||||
self.assert_search = (center, block_ids, distance, y_limit)
|
||||
return iter([bed])
|
||||
|
||||
def block_at(self, *position):
|
||||
return bed_state if tuple(position) == bed else blocks.AIR
|
||||
|
||||
def check_bed_occupied(self, position):
|
||||
return False
|
||||
|
||||
world = IndexedWorld()
|
||||
state = SleepWithBedStates(Munch(
|
||||
world=world, pos=(0, 64, 0), time=13000,
|
||||
dimension='overworld'))
|
||||
|
||||
state.find_beds()
|
||||
|
||||
self.assertEqual(state.beds, [bed])
|
||||
self.assertEqual(state.state, state.choose_bed)
|
||||
|
||||
def test_force_new_bed_skips_existing_bed_scan(self):
|
||||
class IndexedWorld:
|
||||
def find_blocks_indexed(self, *args, **kwargs):
|
||||
raise AssertionError('forced placement must skip bed search')
|
||||
|
||||
def find_blocks_3d(self, *args, **kwargs):
|
||||
raise AssertionError('forced placement must skip local scan')
|
||||
|
||||
state = SleepWithBedStates(Munch(
|
||||
world=IndexedWorld(), pos=(0, 64, 0), time=13000,
|
||||
dimension='overworld'))
|
||||
state.force_new_bed = True
|
||||
|
||||
state.find_beds()
|
||||
|
||||
self.assertEqual(state.beds, [])
|
||||
self.assertEqual(state.state, state.choose_bed)
|
||||
|
||||
def test_bed_approach_follows_path_before_aiming_at_bed(self):
|
||||
bed = (4, 64, 2)
|
||||
opening = (3, 64, 0)
|
||||
state = SleepWithBedStates(Munch(
|
||||
pos=(0, 64, 0), path=[opening], look_at=bed,
|
||||
game=FakeGame(), world=FakeWorld()))
|
||||
state.area = bed
|
||||
state.opening = opening
|
||||
state.opening_deadline = 9999999999
|
||||
|
||||
state.going_to_bed()
|
||||
self.assertIsNone(state.g.look_at)
|
||||
|
||||
state.g.pos = opening
|
||||
state.going_to_bed()
|
||||
self.assertEqual(state.g.look_at, bed)
|
||||
self.assertEqual(state.state, state.turn_to_bed)
|
||||
|
||||
def test_bed_placement_approach_follows_path(self):
|
||||
opening = (3, 64, 0)
|
||||
state = SleepWithBedStates(Munch(
|
||||
pos=(0, 64, 0), path=[opening], look_at=(4, 64, 2),
|
||||
game=FakeGame(), world=FakeWorld()))
|
||||
state.area = (4, 64, 2)
|
||||
state.opening = opening
|
||||
state.opening_deadline = 9999999999
|
||||
|
||||
state.going_to_area()
|
||||
|
||||
self.assertIsNone(state.g.look_at)
|
||||
|
||||
def test_arrival_accepts_completed_path_with_small_position_error(self):
|
||||
state = SleepWithBedStates(Munch(
|
||||
pos=(0, 64, 0), path=[(1, 64, 0)], look_at=None,
|
||||
game=FakeGame(), world=FakeWorld()))
|
||||
state.opening = (1, 64, 0)
|
||||
state.opening_deadline = 9999999999
|
||||
|
||||
state.going_to_bed()
|
||||
|
||||
self.assertEqual(state.state, state.turn_to_bed)
|
||||
self.assertEqual(state.g.path, [])
|
||||
|
||||
def test_unconfirmed_sleep_retries_using_head_block(self):
|
||||
foot = (0, 64, 0)
|
||||
head = (0, 64, -1)
|
||||
bed_id = next(
|
||||
state for state in blocks.BED_IDS
|
||||
if blocks.PROPS[state].get('part') == 'foot')
|
||||
|
||||
class ThreatWorld(FakeWorld):
|
||||
def find_threats(self, center, distance):
|
||||
return []
|
||||
|
||||
game = FakeGame()
|
||||
state = SleepWithBedStates(Munch(
|
||||
time=13000, pos=(1, 64, 0), world=ThreatWorld(block=bed_id),
|
||||
game=game, path=[], look_at=None, correction_count=0))
|
||||
state.area = foot
|
||||
state.bed_head = head
|
||||
state.sleep_use_attempts = 1
|
||||
state.sleep_retry_at = 0
|
||||
state.sleep_deadline = 9999999999
|
||||
|
||||
state.sleep_bed()
|
||||
|
||||
self.assertEqual(game.placements, [(head, 1, 0, 0.5)])
|
||||
self.assertEqual(state.sleep_use_attempts, 2)
|
||||
|
||||
def test_failed_existing_bed_use_is_blacklisted(self):
|
||||
bed_id = next(iter(blocks.BED_IDS))
|
||||
|
||||
class ThreatWorld(FakeWorld):
|
||||
def find_threats(self, center, distance):
|
||||
return []
|
||||
|
||||
state = SleepWithBedStates(Munch(
|
||||
time=13000, pos=(1, 64, 0), world=ThreatWorld(block=bed_id),
|
||||
game=FakeGame(), path=[], look_at=None, correction_count=0))
|
||||
state.area = (0, 64, 0)
|
||||
state.sleep_use_attempts = 2
|
||||
state.sleep_retry_at = 9999999999
|
||||
state.sleep_deadline = 0
|
||||
|
||||
state.sleep_bed()
|
||||
|
||||
self.assertIn(state.area, state.bad_beds)
|
||||
self.assertEqual(state.state, state.cleanup)
|
||||
|
||||
def test_bed_waits_with_status_until_sleep_time(self):
|
||||
bed_id = next(iter(blocks.BED_IDS))
|
||||
state = SleepWithBedStates(Munch(
|
||||
time=12400, world=FakeWorld(block=bed_id), game=FakeGame(),
|
||||
path=[], look_at=None))
|
||||
state.area = (0, 64, 0)
|
||||
state.state = state.use_bed
|
||||
|
||||
state.use_bed()
|
||||
|
||||
self.assertEqual(state.state, state.use_bed)
|
||||
self.assertTrue(state.waiting_for_night)
|
||||
|
||||
def test_time_set_to_100_ends_confirmed_sleep(self):
|
||||
bed_id = next(iter(blocks.BED_IDS))
|
||||
|
||||
class VacatedWorld(FakeWorld):
|
||||
def find_threats(self, center, distance):
|
||||
return []
|
||||
|
||||
state = SleepWithBedStates(Munch(
|
||||
time=100, pos=(0, 64, 0), world=VacatedWorld(block=bed_id),
|
||||
game=FakeGame(), path=[], look_at=None, correction_count=0))
|
||||
state.area = (0, 64, 0)
|
||||
state.my_bed = True
|
||||
state.sleep_confirmed = True
|
||||
|
||||
state.sleep_bed()
|
||||
|
||||
self.assertEqual(state.state, state.break_bed)
|
||||
|
||||
def test_unoccupied_bed_ends_confirmed_sleep_even_with_stale_night_clock(self):
|
||||
bed_id = next(iter(blocks.BED_IDS))
|
||||
|
||||
class VacatedWorld(FakeWorld):
|
||||
def __init__(self):
|
||||
super().__init__(bed_id)
|
||||
self.occupancy = iter((True, False))
|
||||
|
||||
def check_bed_occupied(self, position):
|
||||
return next(self.occupancy)
|
||||
|
||||
def find_threats(self, center, distance):
|
||||
return []
|
||||
|
||||
state = SleepWithBedStates(Munch(
|
||||
time=12129, pos=(0, 64, 0), world=VacatedWorld(),
|
||||
game=FakeGame(), path=[], look_at=None, correction_count=0))
|
||||
state.area = (0, 64, 0)
|
||||
state.my_bed = True
|
||||
state.state = state.sleep_bed
|
||||
|
||||
state.sleep_bed()
|
||||
self.assertTrue(state.sleep_confirmed)
|
||||
state.sleep_bed()
|
||||
|
||||
self.assertEqual(state.state, state.break_bed)
|
||||
|
||||
def test_sleep_attempt_aborts_if_night_was_skipped_while_approaching(self):
|
||||
bed_id = next(iter(blocks.BED_IDS))
|
||||
world = FakeWorld(block=bed_id)
|
||||
state = SleepWithBedStates(Munch(
|
||||
time=13000, dimension='overworld', world=world,
|
||||
game=FakeGame(), path=[], look_at=None))
|
||||
state.init()
|
||||
state.area = (0, 64, 0)
|
||||
state.state = state.use_bed
|
||||
state.g.time = 25
|
||||
|
||||
state.use_bed()
|
||||
|
||||
self.assertEqual(state.state, state.cleanup)
|
||||
|
||||
def test_occupied_bed_is_abandoned_before_waiting_for_night(self):
|
||||
bed_id = next(iter(blocks.BED_IDS))
|
||||
|
||||
class OccupiedWorld(FakeWorld):
|
||||
@staticmethod
|
||||
def check_bed_occupied(_position):
|
||||
return True
|
||||
|
||||
state = SleepWithBedStates(Munch(
|
||||
time=13000, dimension='overworld',
|
||||
world=OccupiedWorld(block=bed_id), game=FakeGame(),
|
||||
path=[], look_at=None))
|
||||
state.init()
|
||||
state.area = (0, 64, 0)
|
||||
state.beds = [(2, 64, 0)]
|
||||
state.state = state.use_bed
|
||||
|
||||
state.use_bed()
|
||||
|
||||
self.assertEqual(state.state, state.choose_bed)
|
||||
|
||||
def test_daytime_sleep_aborts_cleanly(self):
|
||||
state = SleepWithBedStates(Munch(time=1000, dimension='overworld'))
|
||||
state.init()
|
||||
self.assertEqual(state.state, state.cleanup)
|
||||
|
||||
def test_daytime_thunderstorm_allows_sleep_attempt(self):
|
||||
state = SleepWithBedStates(Munch(
|
||||
time=1000, weather='thunderstorm', dimension='overworld'))
|
||||
|
||||
state.init()
|
||||
|
||||
self.assertEqual(state.state, state.find_beds)
|
||||
|
||||
def test_thunderstorm_allows_bed_use_during_daytime(self):
|
||||
bed_id = next(iter(blocks.BED_IDS))
|
||||
game = FakeGame()
|
||||
state = SleepWithBedStates(Munch(
|
||||
time=12400, weather='clear', dimension='overworld',
|
||||
world=FakeWorld(block=bed_id), game=game, path=[], look_at=None))
|
||||
state.init()
|
||||
state.area = (0, 64, 0)
|
||||
state.state = state.use_bed
|
||||
state.silent = True
|
||||
state.g.time = 1000
|
||||
state.g.weather = 'thunderstorm'
|
||||
|
||||
state.use_bed()
|
||||
|
||||
self.assertEqual(state.state, state.sleep_bed)
|
||||
self.assertEqual(len(game.placements), 1)
|
||||
|
||||
def test_placing_bed_uses_support_block_face_top(self):
|
||||
game = FakeGame()
|
||||
state = SleepWithBedStates(Munch(
|
||||
game=game, world=FakeWorld(), path=[], look_at=None,
|
||||
holding_hand=1))
|
||||
state.area = (4, 70, 5)
|
||||
|
||||
state.place_bed()
|
||||
|
||||
self.assertEqual(len(game.placements), 1)
|
||||
location, face, hand, cursor_y = game.placements[0]
|
||||
self.assertEqual(location, (4, 69, 5))
|
||||
self.assertEqual(int(face), 1)
|
||||
self.assertEqual(hand, 1)
|
||||
self.assertIsNone(cursor_y)
|
||||
self.assertTrue(state.my_bed)
|
||||
self.assertEqual(state.state, state.wait_use)
|
||||
|
||||
def test_wait_use_waits_for_delayed_chunk_update(self):
|
||||
bed_id = next(iter(blocks.BED_IDS))
|
||||
world = FakeWorld(block=blocks.AIR)
|
||||
state = SleepWithBedStates(Munch(
|
||||
game=FakeGame(), world=world, path=[], look_at=None))
|
||||
state.area = (4, 70, 5)
|
||||
state.my_bed = True
|
||||
state.bed_place_deadline = 9999999999
|
||||
state.state = state.wait_use
|
||||
|
||||
state.wait_use()
|
||||
self.assertEqual(state.state, state.wait_use)
|
||||
self.assertTrue(state.my_bed)
|
||||
|
||||
world.block = bed_id
|
||||
state.wait_use()
|
||||
self.assertEqual(state.state, state.use_bed)
|
||||
self.assertIsNotNone(state.bed_head)
|
||||
|
||||
def test_wait_use_aborts_after_update_timeout(self):
|
||||
state = SleepWithBedStates(Munch(
|
||||
game=FakeGame(), world=FakeWorld(block=blocks.AIR),
|
||||
path=[], look_at=None))
|
||||
state.area = (4, 70, 5)
|
||||
state.my_bed = True
|
||||
state.bed_place_deadline = 0
|
||||
|
||||
state.wait_use()
|
||||
|
||||
self.assertIn(state.area, state.bad_areas)
|
||||
self.assertFalse(state.my_bed)
|
||||
self.assertEqual(state.state, state.cleanup)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,12 +1,11 @@
|
||||
import unittest
|
||||
from munch import Munch
|
||||
|
||||
from mosfet.info import blocks, items
|
||||
from mosfet.jobs.cache_items import CacheItemsStates
|
||||
from mosfet.info import items
|
||||
from mosfet.world import World
|
||||
|
||||
|
||||
class LabeledStorageTests(unittest.TestCase):
|
||||
class StorageLabelTests(unittest.TestCase):
|
||||
def test_labels_resolve_to_item_categories(self):
|
||||
seeds = items.storage_item_ids_for_label('Seeds')
|
||||
wheat = items.storage_item_ids_for_label('Wheat')
|
||||
@@ -45,33 +44,6 @@ class LabeledStorageTests(unittest.TestCase):
|
||||
self.assertIn(items.WHEAT_SEEDS_ID, chests[0]['item_ids'])
|
||||
self.assertEqual(chests[0]['labels'], {'Seeds'})
|
||||
|
||||
def test_wheat_chest_does_not_cache_seeds(self):
|
||||
wheat = Munch(present=True, item_id=items.WHEAT_ID, item_count=64)
|
||||
seeds = Munch(present=True, item_id=items.WHEAT_SEEDS_ID, item_count=64)
|
||||
clicks = []
|
||||
|
||||
class FakeGame:
|
||||
@staticmethod
|
||||
def count_window_slots():
|
||||
return 0
|
||||
|
||||
@staticmethod
|
||||
def click_window(*args):
|
||||
clicks.append(args)
|
||||
|
||||
cache = CacheItemsStates(Munch(
|
||||
inv={9: wheat, 10: seeds}, item_lock=False,
|
||||
window=Munch(container_slots=27, contents={
|
||||
**dict.fromkeys(range(27)), 27: wheat, 28: seeds,
|
||||
}),
|
||||
game=FakeGame(),
|
||||
))
|
||||
cache.is_labeled_container = True
|
||||
cache.container_item_ids = items.storage_item_ids_for_label('Wheat')
|
||||
cache.move_items()
|
||||
|
||||
self.assertEqual(clicks, [(27, 0, 1, wheat)])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,409 +0,0 @@
|
||||
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 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 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
|
||||
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_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:
|
||||
@staticmethod
|
||||
def find_safety_signs(*_args):
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
def find_safety_openings(*_args):
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
def find_bed_areas(_center, _distance):
|
||||
return [spot]
|
||||
|
||||
@staticmethod
|
||||
def find_threats(center, distance):
|
||||
return [threat] if tuple(center) == start else []
|
||||
|
||||
@staticmethod
|
||||
def path_to_place(origin, target):
|
||||
return [origin, target]
|
||||
|
||||
g = Munch(world=SafeWorld(), pos=start, path=[], look_at=None,
|
||||
job=FakeJob())
|
||||
monitor = ThreatMonitor(g)
|
||||
|
||||
with patch('mosfet.jobs.check_threats.time.monotonic',
|
||||
return_value=0.0):
|
||||
monitor.tick()
|
||||
|
||||
self.assertEqual(monitor.state.__func__, monitor.evacuating.__func__)
|
||||
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):
|
||||
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:
|
||||
@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_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 = []
|
||||
|
||||
class QuietWorld:
|
||||
@staticmethod
|
||||
def find_threats(center, distance):
|
||||
scans.append((center, distance))
|
||||
return []
|
||||
|
||||
monitor = ThreatMonitor(Munch(
|
||||
world=QuietWorld(), pos=(1, 64, 2), job=FakeJob()))
|
||||
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)
|
||||
safe = (5, 64, 0)
|
||||
sign = {'position': (5, 64, 1), 'side': 'front'}
|
||||
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_safety_signs(*_args):
|
||||
return [sign]
|
||||
|
||||
@staticmethod
|
||||
def find_safety_openings(_sign):
|
||||
return [safe]
|
||||
|
||||
@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()
|
||||
@@ -163,6 +163,27 @@ class TestWebInventoryAPI(unittest.TestCase):
|
||||
'(12, 64, -7)': 1,
|
||||
})
|
||||
|
||||
def test_global_api_retries_if_live_dictionary_changes_during_snapshot(self):
|
||||
class ChangingDict(dict):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.first_read = True
|
||||
|
||||
def items(self):
|
||||
if self.first_read:
|
||||
self.first_read = False
|
||||
raise RuntimeError('dictionary changed size during iteration')
|
||||
return super().items()
|
||||
|
||||
self.g.chest_viewers = ChangingDict({(12, 64, -7): 1})
|
||||
|
||||
response = self.client.get('/api/global')
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.get_json()['chest_viewers'], {
|
||||
'(12, 64, -7)': 1,
|
||||
})
|
||||
|
||||
def test_drop_endpoint_supports_one_stack_and_matching_actions(self):
|
||||
one = self.client.post('/api/inventory/drop', json={
|
||||
'slot': 9, 'action': 'one'})
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import unittest
|
||||
from munch import Munch
|
||||
|
||||
from mosfet.info import blocks, mobs
|
||||
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 WorldThreatTests(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))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user