diff --git a/mosfet/job.py b/mosfet/job.py index 29a795e..329d16c 100644 --- a/mosfet/job.py +++ b/mosfet/job.py @@ -357,6 +357,7 @@ class JobStates(LegacyJobStates): self.g.game.close_window() self.g.path = [] self.g.look_at = None + self.g.yaw_target_override = None if self.sleep_with_bed_states.state == self.sleep_with_bed_states.sleep_bed: self.g.game.leave_bed() self.gather_wood_states.state = self.gather_wood_states.idle diff --git a/mosfet/jobs/sleep_with_bed.py b/mosfet/jobs/sleep_with_bed.py index 37907fd..79061ee 100644 --- a/mosfet/jobs/sleep_with_bed.py +++ b/mosfet/jobs/sleep_with_bed.py @@ -26,6 +26,10 @@ class SleepWithBedStates: self.area = None self.opening = None self.bed_head = None + self.bed_facing = None + self.bed_aim = None + self.alignment_deadline = 0.0 + self.bed_target_yaw = None self.beds = [] self.sleep_use_attempts = 0 self.waiting_for_night = False @@ -174,43 +178,26 @@ class SleepWithBedStates: w = self.g.world p = utils.pint(self.g.pos) - for area in w.find_bed_areas(p, 100): - print('Found area:', area) - if area not in self.bad_areas: - break - else: # for - print('Unable to find area') - self.state = self.cleanup - return - - self.area = area - openings = w.find_bed_openings(self.area) - - for o in openings: - toward_player = (o[0] - self.area[0], 0, - o[2] - self.area[2]) - # The bed head points away from the player when placing from this - # opening, so require that footprint and its support to be clear. - head = (self.area[0] - toward_player[0], self.area[1], - self.area[2] - toward_player[2]) - if (w.block_at(*head) != blocks.AIR or - w.block_at(*utils.padd(head, path.BLOCK_ABOVE)) != blocks.AIR): + for layout in w.find_bed_areas(p, 100, include_layout=True): + print('Found bed lane:', layout) + if layout.foot in self.bad_areas: continue - head_support = w.block_at(*utils.padd(head, path.BLOCK_BELOW)) - if (head_support is None or - head_support in blocks.NON_SOLID_IDS or - head_support in blocks.AVOID_IDS): - continue - navpath = w.path_to_place(p, o) - self.opening = o + navpath = w.path_to_place(p, layout.opening) if navpath is not None: break else: # for - print('Unable to get to bed area', self.area) - self.bad_areas.append(self.area) + print('Unable to find a reachable three-block bed lane') self.state = self.cleanup return + self.area = layout.foot + self.bed_head = layout.head + self.opening = layout.opening + self.bed_facing = layout.facing + # Aim at the far end of the lane, not the adjacent foot tile. The + # movement controller ignores yaw targets within 0.6 blocks, and the + # foot tile can be that close when arrival stops near its edge. + self.bed_aim = layout.head self.g.path = navpath self.opening_deadline = time.monotonic() + max( 8.0, len(navpath) * 0.75) @@ -218,25 +205,47 @@ class SleepWithBedStates: self.state = self.going_to_area def going_to_area(self): - if self._at_opening(): - print('At bed area') + if utils.pint(self.g.pos) == self.opening: + print('At bed lane end', self.opening) self.g.path = [] - self.g.look_at = self.area - self.wait_time = 0.5 + self.g.look_at = self.bed_aim + self.bed_target_yaw = self._yaw_for_facing(self.bed_facing) + self.g.yaw_target_override = self.bed_target_yaw + self.alignment_deadline = time.monotonic() + 8.0 self.state = self.turn_to_bed_area elif time.monotonic() >= self.opening_deadline: - print('Could not reach bed placement opening', self.opening) + print('Could not reach bed lane end', self.opening) self.bad_areas.append(self.area) self.g.look_at = None - self.state = self.cleanup + self.state = self.find_bed_spot else: self.g.look_at = None + @staticmethod + def _yaw_for_facing(facing): + # Match the 26.3 movement adapter's Minecraft yaw convention. + return { + (0, 0, -1): 180.0, + (0, 0, 1): 0.0, + (1, 0, 0): -90.0, + (-1, 0, 0): 90.0, + }[tuple(facing)] + def turn_to_bed_area(self): - self.g.look_at = self.area - self.wait_time -= utils.TICK - if self.wait_time <= 0: + self.g.look_at = self.bed_aim + target_yaw = self.bed_target_yaw + self.g.yaw_target_override = target_yaw + delta = (target_yaw - float(self.g.get('yaw', 0.0)) + 180) % 360 - 180 + if abs(delta) <= 5.0: + print('Aligned with bed lane; facing', self.bed_facing) self.state = self.place_bed + elif time.monotonic() >= self.alignment_deadline: + print('Could not align with bed lane; rejecting', self.area, + '(yaw={}, target={})'.format(self.g.get('yaw'), target_yaw)) + self.bad_areas.append(self.area) + self.g.look_at = None + self.g.yaw_target_override = None + self.state = self.find_bed_spot def place_bed(self): support = utils.padd(self.area, path.BLOCK_BELOW) @@ -253,11 +262,13 @@ class SleepWithBedStates: # chunk state for several seconds instead of giving up after 0.5s. if self.g.world.block_at(*self.area) in blocks.BED_IDS: self.bed_head = self._bed_head(self.area) + self.g.yaw_target_override = None self.state = self.use_bed elif time.monotonic() >= self.bed_place_deadline: print('Bed placement was not confirmed at', self.area) self.bad_areas.append(self.area) self.my_bed = False + self.g.yaw_target_override = None self.state = self.cleanup def use_bed(self): @@ -378,6 +389,7 @@ class SleepWithBedStates: return self.g.look_at = None self.g.path = [] + self.g.yaw_target_override = None if self.sleep_confirmed and self.area is not None: self.g.post_sleep_cache_bed = tuple(self.area) self.my_bed = False @@ -403,6 +415,10 @@ class SleepWithBedStates: self.area = None self.opening = None self.bed_head = None + self.bed_facing = None + self.bed_aim = None + self.alignment_deadline = 0.0 + self.bed_target_yaw = None self.bad_areas = [] self.wait_time = 0 self.sleep_use_attempts = 0 diff --git a/mosfet/world.py b/mosfet/world.py index aeb4b69..a6e963c 100644 --- a/mosfet/world.py +++ b/mosfet/world.py @@ -1,5 +1,6 @@ import collections import json +from collections import namedtuple import re import time import random @@ -15,6 +16,7 @@ from mosfet.info import mobs from minecraft.networking.types import BlockFace CHEST_OPENING_RADIUS = 3 +BedArea = namedtuple('BedArea', 'foot head opening facing') class World: @@ -479,51 +481,59 @@ class World: return navpath - def find_bed_areas(self, center, distance): - """Yield nearby two-block bed footprints on solid, safe ground.""" - # Only search loaded-neighborhood distance. The old 3-D flood fill - # explored millions of air blocks and used overly broad 5x5 clearance. + def find_bed_areas(self, center, distance, include_layout=False): + """Find bed lanes with a stand tile and two supported bed tiles. + + The three-block lane is ordered from the standing tile toward the bed + head. Default callers receive the standing tile; placement callers can + request the full layout to retain its intended facing. + """ + center = tuple(center) distance = min(distance, 32) headroom = path.BLOCK_ABOVE support = path.BLOCK_BELOW + seen = set() for offset in utils.search_2d(distance): if utils.hypot(offset[0], offset[2]) > distance: continue for dy in (0, 1, -1, 2, -2): - area = (center[0] + offset[0], center[1] + dy, - center[2] + offset[2]) - if area == center: + opening = (center[0] + offset[0], center[1] + dy, + center[2] + offset[2]) + if opening == center: continue - if self.block_at(*area) != blocks.AIR: - continue - if self.block_at(*utils.padd(area, headroom)) != blocks.AIR: - continue - support_block = self.block_at(*utils.padd(area, support)) - if (support_block is None or - support_block in blocks.NON_SOLID_IDS or - support_block in blocks.AVOID_IDS): - continue - - footprint_found = False - for direction in path.CHECK_DIRECTIONS: - head = utils.padd(area, direction) - if (self.block_at(*head) != blocks.AIR or - self.block_at(*utils.padd(head, headroom)) != blocks.AIR): + for facing in path.CHECK_DIRECTIONS: + foot = utils.padd(opening, facing) + head = utils.padd(foot, facing) + lane = (opening, foot, head) + if lane in seen: continue - head_support = self.block_at(*utils.padd(head, support)) - if (head_support is None or - head_support in blocks.NON_SOLID_IDS or - head_support in blocks.AVOID_IDS): - continue - footprint_found = True - break - if not footprint_found: - continue + seen.add(lane) - # Avoid placing a bed within range of nearby monsters. - if self.find_monsters(area, 20): - continue - yield area + valid = True + for index, position in enumerate(lane): + if self.block_at(*position) != blocks.AIR: + valid = False + break + if self.block_at(*utils.padd(position, headroom)) != blocks.AIR: + valid = False + break + support_block = self.block_at(*utils.padd(position, support)) + if (support_block is None or + support_block in blocks.NON_SOLID_IDS or + support_block in blocks.AVOID_IDS): + valid = False + break + if (index == 0 and self.block_at(*utils.padd( + position, path.BLOCK_ABOVE2)) != blocks.AIR): + valid = False + break + if not valid or self.find_monsters(foot, 20): + continue + + if include_layout: + yield BedArea(foot, head, opening, facing) + else: + yield opening def find_cache_areas(self, center, distance): """Yield nearby, supported air blocks suitable for placing a chest.""" diff --git a/tests/test_bed_lane.py b/tests/test_bed_lane.py new file mode 100644 index 0000000..4dfad17 --- /dev/null +++ b/tests/test_bed_lane.py @@ -0,0 +1,105 @@ +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() diff --git a/tests/test_gather_wood.py b/tests/test_gather_wood.py index 24e6498..c8a3cd3 100644 --- a/tests/test_gather_wood.py +++ b/tests/test_gather_wood.py @@ -76,7 +76,7 @@ class GatherWoodPortTests(unittest.TestCase): [(0, 64, 0)]) def test_gather_wood_job_keeps_existing_machine_sequence(self): - job = JobStates(Munch()) + job = JobStates(Munch(yaw_target_override=90.0)) self.assertIsNotNone(job.gather_wood_states) self.assertEqual(job.gather_wood(), [ job.gather_wood_states, @@ -88,6 +88,7 @@ class GatherWoodPortTests(unittest.TestCase): 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))