Add scheduled dispatch support (edit + authoritative view)
All checks were successful
Continuous Integration / lint-and-security (pull_request) Successful in 22s
Continuous Integration / tests-and-coverage (pull_request) Successful in 24s

Editing (game port, OpenTTDClient): a core of JGRPP's scheduled dispatch
DoCommands — set_scheduled_dispatch (enable/disable), add/remove schedule,
add/remove/clear slots, and set duration/start date. Adds the command IDs
to protocol.py.

Viewing (admin, OpenTTDAdminClient.get_dispatch): the GameScript API has no
dispatch support, so a new server patch (docker/patches/0002-*) adds
read-only GSOrder.GetScheduledDispatch* / IsScheduledDispatchEnabled
getters, an AdminBridge GameScript get_dispatch handler exposes them, and
get_dispatch() returns the live schedules and slots (mirrors get_timetable).

Note: set_dispatch_start_date values are normalised by the engine relative
to current game time, so they read back offset from the requested value.

Includes unit + e2e tests, a demo in main.py, and protocol/timetable docs.
The AdminBridge GameScript and the patched OpenTTD-patches clone live
outside this repo; the 0002 patch file is the durable source for the latter.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-24 22:56:37 +02:00
parent 81a4d9333d
commit 2eea541158
11 changed files with 822 additions and 6 deletions

28
main.py
View File

@@ -21,6 +21,8 @@ COMPANY_PASSWORD = "asd123"
# A vehicle owned by COMPANY_ID, used to demonstrate timetable get/set below. Set to a real
# vehicle id to see it in action; leave as None to skip the demonstration.
DEMO_VEHICLE_ID = 7
# A station DEMO_VEHICLE_ID can legally serve, used to demonstrate add_order/remove_order.
DEMO_STATION_ID = 6
async def demo_timetable_workflow(client, vehicle_id):
"""A deliberately thorough walk-through of the timetable API: every ModifyTimetableFlags
@@ -104,6 +106,32 @@ async def demo_timetable_workflow(client, vehicle_id):
await asyncio.sleep(0.5)
show("lateness reset (whole group)")
# 10. Add an order to the front of the list, then remove it again (net-zero, so the
# vehicle's route is left unchanged). Inserting before position 0 and deleting
# position 0 needs no knowledge of the existing order count.
print(f"--- Step 10: add then remove a 'go to station {DEMO_STATION_ID}' order ---")
await client.add_order(vehicle_id, DEMO_STATION_ID, before_position=0)
await asyncio.sleep(0.5)
print(" -> inserted a goto-station order at position 0")
await client.remove_order(vehicle_id, 0)
await asyncio.sleep(0.5)
print(" -> removed it again (route restored)")
# 11. Scheduled dispatch: create a schedule with two departure slots, enable it, then tear it
# all down again so the vehicle is left as it started. Read it back with
# OpenTTDAdminClient.get_dispatch() (see main_admin.py); the game port has no dispatch read.
print("--- Step 11: scheduled dispatch create/enable, then clean up ---")
await client.add_dispatch_schedule(vehicle_id, start_tick=0, duration=3000)
await client.add_dispatch_slot(vehicle_id, 0, 500)
await client.add_dispatch_slot(vehicle_id, 0, 1500)
await client.set_scheduled_dispatch(vehicle_id, True)
await asyncio.sleep(0.5)
print(" -> created schedule 0 with 2 slots and enabled scheduled dispatch")
await client.set_scheduled_dispatch(vehicle_id, False)
await client.remove_dispatch_schedule(vehicle_id, 0)
await asyncio.sleep(0.5)
print(" -> disabled and removed the schedule (restored)")
print(f"=== Timetable demo finished. Final state for vehicle {vehicle_id}: ===")
print(f" {client.get_vehicle_timetable(vehicle_id)}")