From 37f72d674f8b6c8403bc37b8792b31534b1e4a69 Mon Sep 17 00:00:00 2001 From: Tanner Date: Wed, 18 Nov 2026 20:39:47 +0000 Subject: [PATCH] Aim and click beds from grounded openings --- mosfet/game.py | 58 +++++++ mosfet/jobs/sleep_with_bed.py | 229 +++++++++++++++++++++++----- tests/test_bed_packets_and_world.py | 21 ++- 3 files changed, 264 insertions(+), 44 deletions(-) diff --git a/mosfet/game.py b/mosfet/game.py index d571190..2be7aab 100644 --- a/mosfet/game.py +++ b/mosfet/game.py @@ -1396,6 +1396,64 @@ class Game(LegacyGame): self.g.connection.write_packet(ClientTickEnd(), force=True) return True + def aim_angles_to_point(self, point): + """Return the yaw/pitch needed to look at a world-space point.""" + position = self.g.pos + if not hasattr(position, 'x'): + position = vector.Point3D(position) + target = vector.Point3D(point) + + yaw_direction = position - target + yaw_direction.y = 0 + yaw = self.g.get('yaw', 0.0) + if yaw_direction.length() > 0.6: + yaw = yaw_direction.normalized().signedAngleDeg( + other=bot.YAW_ANGLE_DIR, ref=bot.YAW_ANGLE_REF) + + look_direction = bot.look_direction_from_eye(position, target) + pitch = self.g.get('pitch', 0.0) + if look_direction.length() > 0.6: + pitch = (look_direction.normalized().angleDeg( + bot.PITCH_ANGLE_DIR) - 90) * -1 + pitch = min(pitch, bot.MAX_DOWNWARD_PITCH) + return yaw, pitch + + def aim_at_point(self, point): + """Aim the movement controller at an exact world-space point.""" + point = tuple(float(value) for value in point) + self.g.look_at = tuple(value - 0.5 for value in point) + return self.aim_angles_to_point(point) + + def bed_interaction_point(self, position): + """Return a point and cursor on the bed's actual top collision shape.""" + position = tuple(int(value) for value in position) + state_id = self.g.world.block_at(*position) + if state_id not in blocks.BED_IDS: + return None + boxes = blocks.COLLISION_BOXES.get(state_id, ()) + centered = [box for box in boxes + if box[0] <= 0.5 <= box[3] and + box[2] <= 0.5 <= box[5]] + if not centered: + return None + box = max(centered, key=lambda candidate: candidate[4]) + cursor = (0.5, float(box[4]), 0.5) + point = (position[0] + cursor[0], + position[1] + cursor[1], + position[2] + cursor[2]) + return cursor, point + + def click_bed(self, position, hand=0, cursor=None): + """Right-click a bed at its top surface using protocol-777 placement.""" + position = tuple(int(value) for value in position) + interaction = self.bed_interaction_point(position) + if interaction is None: + return False + if cursor is None: + cursor = interaction[0] + self.place_block(position, BlockFace.TOP, hand=hand, cursor=cursor) + return True + def place_block(self, location, face, hand=0, cursor_y=None, sneaking=False, cursor=None): location = tuple(int(value) for value in location) diff --git a/mosfet/jobs/sleep_with_bed.py b/mosfet/jobs/sleep_with_bed.py index 230f6ec..667a7bc 100644 --- a/mosfet/jobs/sleep_with_bed.py +++ b/mosfet/jobs/sleep_with_bed.py @@ -18,6 +18,8 @@ from mosfet.info import mobs from mosfet.jobs.work_boundary import WorkBoundaryReturnMixin class SleepWithBedStates(WorkBoundaryReturnMixin): + BED_AIM_STABLE_DELAY = 0.5 + def idle(self): return None @@ -27,12 +29,16 @@ class SleepWithBedStates(WorkBoundaryReturnMixin): def init(self): self._capture_work_boundary() self.sleep_confirmed = False + self.bed_aim_aligned_since = None self.my_bed = False self.area = None self.opening = None self.bed_head = None self.bed_facing = None self.bed_aim = None + self.bed_click_options = [] + self.bed_click_index = 0 + self.opening_repath_at = 0.0 self.alignment_deadline = 0.0 self.bed_target_yaw = None self.beds = [] @@ -84,6 +90,71 @@ class SleepWithBedStates(WorkBoundaryReturnMixin): facing = self._bed_facing(state_id) return utils.padd(foot, facing) if facing is not None else foot + def _bed_opening_standable(self, opening): + world = self.g.world + feet = world.block_at(*opening) + above = world.block_at(*utils.padd(opening, path.BLOCK_ABOVE)) + below = world.block_at(*utils.padd(opening, path.BLOCK_BELOW)) + return ( + feet in blocks.NON_SOLID_IDS and feet not in blocks.AVOID_IDS and + above in blocks.NON_SOLID_IDS and above not in blocks.AVOID_IDS and + below is not None and below not in blocks.NON_SOLID_IDS and + below not in blocks.AVOID_IDS) + + def _bed_click_options(self, opening, bed, head): + world = self.g.world + eye = (opening[0] + 0.5, opening[1] + 1.62, + opening[2] + 0.5) + options = [] + parts = list(dict.fromkeys((tuple(bed), tuple(head)))) + line_of_sight = getattr(world, '_has_cache_line_of_sight', None) + for part in parts: + interaction = self.g.game.bed_interaction_point(part) + if interaction is None: + continue + cursor, point = interaction + if (callable(line_of_sight) and + not line_of_sight(eye, point, tuple(part))): + continue + options.append({ + 'block': tuple(part), + 'cursor': cursor, + 'point': point, + 'distance': utils.phyp(eye, point), + }) + options.sort(key=lambda option: option['distance']) + return options + + def _bed_approaches(self, position, bed): + world = self.g.world + head = self._bed_head(bed) + parts = list(dict.fromkeys((tuple(bed), tuple(head)))) + approaches = {} + for part in parts: + for direction in path.CHECK_DIRECTIONS: + opening = tuple(utils.padd(part, direction)) + if opening in parts or opening in approaches: + continue + if not self._bed_opening_standable(opening): + continue + options = self._bed_click_options(opening, bed, head) + if not options: + continue + navpath = world.path_to_place(position, opening) + if navpath is None: + continue + route = list(navpath) + if route and tuple(route[0]) == tuple(position): + route = route[1:] + approaches[opening] = { + 'path': route, + 'options': options, + } + return sorted( + ((len(info['path']), utils.phyp(position, opening), opening, info) + for opening, info in approaches.items()), + key=lambda candidate: (candidate[0], candidate[1], candidate[2])) + def find_beds(self): print('Finding beds...') w = self.g.world @@ -141,24 +212,29 @@ class SleepWithBedStates(WorkBoundaryReturnMixin): while self.beds: bed = self.beds.pop(0) print('Chose:', bed) - navpath = w.path_to_place_faked(p, bed) - print('navpath:', navpath) - if navpath is None: + approaches = self._bed_approaches(p, bed) + if not approaches: if getattr(self, 'refuge_sign', None) is None: self.bad_beds.append(bed) - print('Cant get to bed, blacklisting') + print('No reachable, supported, visible bed opening; ' + 'blacklisting') else: - print('Cant reach refuge bed this attempt; skipping') + print('No reachable opening for refuge bed; skipping') continue + _length, _distance, self.opening, approach = approaches[0] self.area = bed self.bed_head = self._bed_head(bed) - self.opening = navpath[-2] if len(navpath) > 1 else p - self.g.path = navpath[:-1] + self.bed_click_options = approach['options'] + self.bed_click_index = 0 + self.g.path = approach['path'] self.opening_deadline = time.monotonic() + max( - 8.0, len(navpath) * 0.75) + 8.0, len(self.g.path) * 0.75) + self.opening_repath_at = 0.0 self.g.look_at = None self.state = self.going_to_bed + print('Going to supported bed opening:', self.opening, + 'click target:', self.bed_click_options[0]['block']) return if getattr(self, 'refuge_sign', None) is not None: @@ -170,32 +246,91 @@ class SleepWithBedStates(WorkBoundaryReturnMixin): self.state = self.select_bed def _at_opening(self): - position = utils.pint(self.g.pos) - return utils.phyp(position, self.opening) <= 1.25 + return ( + utils.pint(self.g.pos) == tuple(self.opening) and + self._bed_opening_standable(self.opening) and + self.g.get('on_ground', False) and + abs(float(self.g.get('y_v', 0.0) or 0.0)) < 0.1) def going_to_bed(self): + now = time.monotonic() if self._at_opening(): - print('At existing bed') + print('At grounded bed opening', self.opening) self.g.path = [] self.my_bed = False - self.g.look_at = self.area - self.wait_time = 0.5 - self.state = self.turn_to_bed - elif time.monotonic() >= self.opening_deadline: - print('Could not reach bed opening', self.opening, + self.bed_click_index = 0 + self.bed_aim_aligned_since = None + self.alignment_deadline = now + 6.0 + self.state = self.use_bed + elif now >= self.opening_deadline: + print('Could not reach grounded bed opening', self.opening, '; trying another bed') if getattr(self, 'refuge_sign', None) is None: self.bad_beds.append(self.area) + self.g.path = [] self.g.look_at = None self.state = self.choose_bed - else: + elif not self.g.path and now >= self.opening_repath_at: + position = utils.pint(self.g.pos) + navpath = self.g.world.path_to_place(position, self.opening) + if navpath: + self.g.path = (list(navpath[1:]) + if tuple(navpath[0]) == position + else list(navpath)) + self.opening_repath_at = now + 1.0 self.g.look_at = None def turn_to_bed(self): - self.g.look_at = self.area - self.wait_time -= utils.TICK - if self.wait_time <= 0: - self.state = self.use_bed + if not self._at_opening(): + self.bed_aim_aligned_since = None + self.g.look_at = None + self.state = self.going_to_bed + return + if not self.bed_click_options: + print('No visible bed surface from opening', self.opening) + self.state = self.choose_bed + return + + option = self.bed_click_options[self.bed_click_index] + target_yaw, target_pitch = self.g.game.aim_at_point(option['point']) + yaw_error = (target_yaw - float(self.g.get('yaw', 0.0)) + 180) % 360 - 180 + pitch_error = target_pitch - float(self.g.get('pitch', 0.0)) + if abs(yaw_error) <= 4.0 and abs(pitch_error) <= 3.0: + now = time.monotonic() + if self.bed_aim_aligned_since is None: + self.bed_aim_aligned_since = now + return + if now - self.bed_aim_aligned_since < self.BED_AIM_STABLE_DELAY: + return + if not self.g.game.click_bed( + option['block'], hand=self.g.get('holding_hand', 0), + cursor=option['cursor']): + print('Bed surface disappeared before click:', option['block']) + self.state = self.choose_bed + return + self.sleep_use_attempts += 1 + self.bed_aim_aligned_since = None + self.sleep_deadline = now + 5.0 + self.sleep_retry_at = now + 1.5 + if self.sleep_use_attempts == 1 and not self.silent: + self.g.chat.send('zzz') + print('Clicked bed surface', option['block'], + 'from grounded opening', self.opening, + '(yaw error {:.1f}, pitch error {:.1f})'.format( + yaw_error, pitch_error), flush=True) + self.state = self.sleep_bed + else: + self.bed_aim_aligned_since = None + if time.monotonic() >= self.alignment_deadline: + if self.bed_click_index + 1 < len(self.bed_click_options): + self.bed_click_index += 1 + self.bed_aim_aligned_since = None + self.alignment_deadline = time.monotonic() + 4.0 + print('Could not aim at bed surface; trying alternate part', + self.bed_click_options[self.bed_click_index]['block']) + else: + print('Could not align with bed; trying another opening') + self.state = self.choose_bed def select_bed(self): if self.g.game.select_item(items.BED_IDS): @@ -293,8 +428,16 @@ class SleepWithBedStates(WorkBoundaryReturnMixin): # 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.bed_click_options = self._bed_click_options( + self.opening, self.area, self.bed_head) + self.bed_click_index = 0 self.g.yaw_target_override = None - self.state = self.use_bed + if self.bed_click_options: + self.state = self.use_bed + else: + print('Placed bed has no visible surface from opening', + self.opening) + self.state = self.cleanup elif time.monotonic() >= self.bed_place_deadline: print('Bed placement was not confirmed at', self.area) self.bad_areas.append(self.area) @@ -335,18 +478,17 @@ class SleepWithBedStates(WorkBoundaryReturnMixin): self.waiting_for_night = False self.waiting_for_time_update = False - print('Using bed') - # Beds are only 9/16 block tall. Aim inside their collision shape; - # the near-top cursor used for placing a new block misses them. - self.g.game.place_block( - self.area, BlockFace.TOP, cursor_y=0.5) - self.sleep_deadline = time.monotonic() + 5.0 - self.sleep_retry_at = time.monotonic() + 1.5 - self.sleep_use_attempts = 1 + if not self._at_opening(): + self.state = self.going_to_bed + return + print('Aiming at bed from grounded opening', self.opening) + self.sleep_deadline = time.monotonic() + 12.0 + self.sleep_use_attempts = 0 self.sleep_confirmed = False - if not self.silent: - self.g.chat.send('zzz') - self.state = self.sleep_bed + self.bed_click_index = 0 + self.bed_aim_aligned_since = None + self.alignment_deadline = time.monotonic() + 6.0 + self.state = self.turn_to_bed def sleep_bed(self): w = self.g.world @@ -375,13 +517,18 @@ class SleepWithBedStates(WorkBoundaryReturnMixin): self.state = self.break_bed elif (not self.sleep_confirmed and self.sleep_use_attempts < 2 and + self.sleep_use_attempts > 0 and time.monotonic() >= self.sleep_retry_at): - print('Bed use not confirmed on foot; trying head block', - self.bed_head) - self.g.game.place_block( - self.bed_head, BlockFace.TOP, cursor_y=0.5) - self.sleep_use_attempts += 1 - self.sleep_retry_at = time.monotonic() + 1.5 + print('Bed use not confirmed; realigning before retry') + self.bed_aim_aligned_since = None + if self.bed_click_index + 1 < len(self.bed_click_options): + self.bed_click_index += 1 + self.alignment_deadline = time.monotonic() + 5.0 + self.state = self.turn_to_bed + elif (not self.sleep_confirmed and self.sleep_use_attempts == 0 and + time.monotonic() >= self.sleep_deadline): + print('Could not aim/click bed while grounded; aborting') + self.state = self.cleanup elif (not self.sleep_confirmed and time.monotonic() >= self.sleep_deadline): if getattr(self, 'refuge_sign', None) is None: @@ -447,6 +594,7 @@ class SleepWithBedStates(WorkBoundaryReturnMixin): def __init__(self, global_state): self.g = global_state self.state = self.idle + self.bed_aim_aligned_since = None self.silent = False @@ -458,6 +606,9 @@ class SleepWithBedStates(WorkBoundaryReturnMixin): self.bed_head = None self.bed_facing = None self.bed_aim = None + self.bed_click_options = [] + self.bed_click_index = 0 + self.opening_repath_at = 0.0 self.alignment_deadline = 0.0 self.bed_target_yaw = None self.bad_areas = [] diff --git a/tests/test_bed_packets_and_world.py b/tests/test_bed_packets_and_world.py index f75edba..fb21a4f 100644 --- a/tests/test_bed_packets_and_world.py +++ b/tests/test_bed_packets_and_world.py @@ -125,18 +125,29 @@ class BedPacketsAndWorldTests(unittest.TestCase): self.assertTrue(force) self.assertIsInstance(connection.packets[1][0], ClientTickEnd) - def test_existing_bed_use_aims_inside_the_short_bed_collision_shape(self): + def test_bed_click_targets_the_top_of_its_collision_shape(self): connection = FakeConnection() game = Game.__new__(Game) - game.g = Munch(connection=connection) + bed = (-175, 70, -312) + bed_state = next( + state for state in blocks.BED_IDS + if blocks.PROPS[state].get('part') == 'foot') + game.g = Munch( + connection=connection, + world=Munch(block_at=lambda *position: bed_state)) game.sequence = 0 - game.place_block((-175, 70, -312), 1, cursor_y=0.5) + interaction = game.bed_interaction_point(bed) + self.assertIsNotNone(interaction) + self.assertTrue(game.click_bed(bed, cursor=interaction[0])) packet = connection.packets[0][0] self.assertIsInstance(packet, UseItemOnBlockPacket) - self.assertEqual(packet.location, (-175, 70, -312)) - self.assertEqual(packet.cursor_y, 0.5) + self.assertEqual(packet.location, bed) + self.assertEqual(packet.face, 1) + self.assertEqual((packet.cursor_x, packet.cursor_y, packet.cursor_z), + interaction[0]) + self.assertAlmostEqual(packet.cursor_y, 0.5625) self.assertIsInstance(connection.packets[1][0], ClientTickEnd) def test_bed_in_offhand_is_selectable_without_swapping_inventory(self):