From e8b1913e97720a2828a541d0ce4bde1bc44a549d Mon Sep 17 00:00:00 2001 From: Tanner Date: Sun, 15 Nov 2026 15:35:56 +0000 Subject: [PATCH] Keep movement physics ticking while idle --- mosfet/bot.py | 15 ++++++---- tests/test_idle_physics.py | 58 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 tests/test_idle_physics.py diff --git a/mosfet/bot.py b/mosfet/bot.py index 82df416..b6fec59 100644 --- a/mosfet/bot.py +++ b/mosfet/bot.py @@ -160,8 +160,11 @@ def tick(global_state): print('Chunks loaded in', round(time.time() - g.chunks.loading, 2), 's') g.chunks.loading = False g.chunk_wait_report_at = 0 + job = g.get('job') + job_active = job is not None and job.state != job.idle status = ('draining_inventory' if g.get('draining') else - 'following_path' if g.path else 'running_job') + 'following_path' if g.path else + 'running_job' if job_active else 'idle') if g.get('movement_status') != status: print('[movement] status={}'.format(status), flush=True) g.movement_status = status @@ -563,9 +566,11 @@ def start_automation(global_state, initialize=True): bool(g.get('drop_waiting'))) if g.get('movement_paused'): set_status('paused') - elif has_work and not movement_ready: - set_status('waiting_for_chunks') - elif has_work and movement_ready: + elif not movement_ready: + set_status('waiting_for_chunks' if has_work else 'idle') + else: + # Keep local physics running even when there is no path or job: + # gravity and position packets must continue while standing idle. try: reload_lock = g.get('reload_lock') if reload_lock is None: @@ -581,8 +586,6 @@ def start_automation(global_state, initialize=True): print('[movement] stopped after navigation error', flush=True) return - elif not has_work and not g.get('movement_paused'): - set_status('idle') delay = max(0.0, utils.TICK - (time.monotonic() - started)) time.sleep(delay) diff --git a/tests/test_idle_physics.py b/tests/test_idle_physics.py new file mode 100644 index 0000000..08e5ceb --- /dev/null +++ b/tests/test_idle_physics.py @@ -0,0 +1,58 @@ +import unittest +from unittest.mock import patch + +from munch import Munch + +from mosfet.bot import start_automation + + +class IdleJob: + def idle(self): + pass + + def __init__(self): + self.state = self.idle + + +class IdlePhysicsTests(unittest.TestCase): + def test_movement_worker_ticks_physics_without_active_work(self): + connection = Munch(connected=True) + job = IdleJob() + monitor = Munch(tick=lambda: None, suppress_job=False) + game = Munch(_advance_world_time=lambda: None) + state = Munch( + running=True, + connection=connection, + pos=(0, 64, 0), + game=game, + job=job, + path=[], + breaking=None, + threat_monitor=monitor, + movement_status='idle', + ) + ticks = [] + + def tick_when_idle(global_state): + ticks.append(True) + global_state.running = False + + worker = None + try: + with patch('mosfet.bot.missing_movement_chunks', return_value=[]), \ + patch('mosfet.bot.tick', side_effect=tick_when_idle): + worker = start_automation(state, initialize=False) + worker.join(timeout=2.0) + finally: + state.running = False + if state.get('movement_stop_event'): + state.movement_stop_event.set() + if worker is not None: + worker.join(timeout=1.0) + + self.assertFalse(worker.is_alive()) + self.assertTrue(ticks) + + +if __name__ == '__main__': + unittest.main()