From 7a6d102c4bdb4372ae25e3ba6b1e167f8dfda76e Mon Sep 17 00:00:00 2001 From: Tyler Trahan Date: Mon, 13 Nov 2023 07:19:39 -0500 Subject: [PATCH] Codechange: Clean up timetable start checks (#11456) --- src/timetable_cmd.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/timetable_cmd.cpp b/src/timetable_cmd.cpp index a18ed54426..c1911bd49e 100644 --- a/src/timetable_cmd.cpp +++ b/src/timetable_cmd.cpp @@ -311,14 +311,21 @@ CommandCost CmdSetTimetableStart(DoCommandFlag flags, VehicleID veh_id, bool tim CommandCost ret = CheckOwnership(v->owner); if (ret.Failed()) return ret; - int total_duration = v->orders->GetTimetableTotalDuration(); + TimerGameTick::Ticks total_duration = v->orders->GetTimetableTotalDuration(); - /* Don't let a timetable start more than 15 years into the future or 1 year in the past. */ + /* Don't let a timetable start at an invalid date. */ if (start_date < 0 || start_date > CalendarTime::MAX_DATE) return CMD_ERROR; + + /* Don't let a timetable start more than 15 years into the future... */ if (start_date - TimerGameCalendar::date > TimerGameCalendar::DateAtStartOfYear(MAX_TIMETABLE_START_YEARS)) return CMD_ERROR; + /* ...or 1 year in the past. */ if (TimerGameCalendar::date - start_date > CalendarTime::DAYS_IN_LEAP_YEAR) return CMD_ERROR; + + /* If trying to distribute start dates over a shared order group, we need to know the total duration. */ if (timetable_all && !v->orders->IsCompleteTimetable()) return CommandCost(STR_ERROR_TIMETABLE_INCOMPLETE); - if (timetable_all && start_date + total_duration / Ticks::DAY_TICKS > CalendarTime::MAX_DATE) return CMD_ERROR; + + /* Don't allow invalid start dates for other vehicles in the shared order group. */ + if (timetable_all && start_date + (total_duration / Ticks::DAY_TICKS) > CalendarTime::MAX_DATE) return CMD_ERROR; if (flags & DC_EXEC) { std::vector vehs;