From 541beb386ca31875eb3b8ec050d4a713653cfc46 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Tue, 27 Dec 2022 00:13:37 +0000 Subject: [PATCH] Scheduled dispatch: Allow wrapping at midnight when bulk inserting slots For 24 hour dispatch schedules See: #467 --- src/schdispatch_cmd.cpp | 1 + src/schdispatch_gui.cpp | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/schdispatch_cmd.cpp b/src/schdispatch_cmd.cpp index bfb87330fc..51716a2626 100644 --- a/src/schdispatch_cmd.cpp +++ b/src/schdispatch_cmd.cpp @@ -98,6 +98,7 @@ CommandCost CmdScheduledDispatchAdd(TileIndex tile, DoCommandFlag flags, uint32 ds.AddScheduledDispatch(p2); for (uint i = 0; i < extra_slots; i++) { p2 += offset; + if (p2 >= ds.GetScheduledDispatchDuration()) p2 -= ds.GetScheduledDispatchDuration(); ds.AddScheduledDispatch(p2); } SetTimetableWindowsDirty(v, true); diff --git a/src/schdispatch_gui.cpp b/src/schdispatch_gui.cpp index d18e883898..7fe4871214 100644 --- a/src/schdispatch_gui.cpp +++ b/src/schdispatch_gui.cpp @@ -84,7 +84,7 @@ static void SetScheduleStartDateCallback(const Window *w, DateTicksScaled date) * @param p1 The p1 parameter to send to CmdScheduledDispatchAdd * @param date the actually chosen date */ -static void ScheduleAddIntl(uint32 p1, DateTicksScaled date, uint extra_slots, uint offset) +static void ScheduleAddIntl(uint32 p1, DateTicksScaled date, uint extra_slots, uint offset, bool wrap_mode = false) { VehicleID veh = GB(p1, 0, 20); uint schedule_index = GB(p1, 20, 12); @@ -99,7 +99,7 @@ static void ScheduleAddIntl(uint32 p1, DateTicksScaled date, uint extra_slots, u while (date > start_tick) date -= duration; while (date < start_tick) date += duration; - if (extra_slots > 0 && offset > 0) { + if (extra_slots > 0 && offset > 0 && !wrap_mode) { DateTicksScaled end_tick = start_tick + duration; DateTicksScaled max_extra_slots = (end_tick - 1 - date) / offset; if (max_extra_slots < extra_slots) extra_slots = static_cast(std::max(0, max_extra_slots)); @@ -882,12 +882,21 @@ struct SchdispatchWindow : GeneralVehicleWindow { void AddMultipleDepartureSlots(uint start, uint step, uint end) { + bool wrap_mode = false; + if (end < start) { + const DispatchSchedule &ds = this->GetSelectedSchedule(); + if (ds.GetScheduledDispatchDuration() == (1440 * _settings_time.ticks_per_minute)) { + /* 24 hour timetabling */ + end += 1440; + wrap_mode = true; + } + } if (end < start || step == 0 || !this->IsScheduleSelected()) return; DateTicksScaled slot = MINUTES_DATE(MINUTES_DAY(CURRENT_MINUTE), 0, start); slot -= _settings_time.clock_offset; slot *= _settings_time.ticks_per_minute; - ScheduleAddIntl(this->vehicle->index | (this->schedule_index << 20), slot, (end - start) / step, step * _settings_time.ticks_per_minute); + ScheduleAddIntl(this->vehicle->index | (this->schedule_index << 20), slot, (end - start) / step, step * _settings_time.ticks_per_minute, wrap_mode); } };