Keep movement physics ticking while idle
This commit is contained in:
1 parent
8d2374346e
commit
e8b1913e97
2 files changed
+67
-6
No files matched your search
+9
-6
@@ -160,8 +160,11 @@ def tick(global_state):
|
|||||||
print('Chunks loaded in', round(time.time() - g.chunks.loading, 2), 's')
|
print('Chunks loaded in', round(time.time() - g.chunks.loading, 2), 's')
|
||||||
g.chunks.loading = False
|
g.chunks.loading = False
|
||||||
g.chunk_wait_report_at = 0
|
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
|
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:
|
if g.get('movement_status') != status:
|
||||||
print('[movement] status={}'.format(status), flush=True)
|
print('[movement] status={}'.format(status), flush=True)
|
||||||
g.movement_status = status
|
g.movement_status = status
|
||||||
@@ -563,9 +566,11 @@ def start_automation(global_state, initialize=True):
|
|||||||
bool(g.get('drop_waiting')))
|
bool(g.get('drop_waiting')))
|
||||||
if g.get('movement_paused'):
|
if g.get('movement_paused'):
|
||||||
set_status('paused')
|
set_status('paused')
|
||||||
elif has_work and not movement_ready:
|
elif not movement_ready:
|
||||||
set_status('waiting_for_chunks')
|
set_status('waiting_for_chunks' if has_work else 'idle')
|
||||||
elif has_work and movement_ready:
|
else:
|
||||||
|
# Keep local physics running even when there is no path or job:
|
||||||
|
# gravity and position packets must continue while standing idle.
|
||||||
try:
|
try:
|
||||||
reload_lock = g.get('reload_lock')
|
reload_lock = g.get('reload_lock')
|
||||||
if reload_lock is None:
|
if reload_lock is None:
|
||||||
@@ -581,8 +586,6 @@ def start_automation(global_state, initialize=True):
|
|||||||
print('[movement] stopped after navigation error',
|
print('[movement] stopped after navigation error',
|
||||||
flush=True)
|
flush=True)
|
||||||
return
|
return
|
||||||
elif not has_work and not g.get('movement_paused'):
|
|
||||||
set_status('idle')
|
|
||||||
delay = max(0.0, utils.TICK - (time.monotonic() - started))
|
delay = max(0.0, utils.TICK - (time.monotonic() - started))
|
||||||
time.sleep(delay)
|
time.sleep(delay)
|
||||||
|
|
||||||
|
|||||||
@@ -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()
|
||||||
Reference in new issue
Block a user