diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index 0dd06a3edb..9279abdbee 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -967,7 +967,7 @@ int DrawVehiclePurchaseInfo(int left, int right, int y, EngineID engine_number, if (e->type != VEH_TRAIN || e->u.rail.railveh_type != RAILVEH_WAGON) { /* Design date - Life length */ SetDParam(0, ymd.year); - SetDParam(1, e->GetLifeLengthInDays() / DAYS_IN_LEAP_YEAR); + SetDParam(1, DateToYear(e->GetLifeLengthInDays())); DrawString(left, right, y, STR_PURCHASE_INFO_DESIGNED_LIFE); y += FONT_HEIGHT_NORMAL; diff --git a/src/date_type.h b/src/date_type.h index c88b4f2061..3f69f6050b 100644 --- a/src/date_type.h +++ b/src/date_type.h @@ -63,6 +63,16 @@ static constexpr TimerGameCalendar::Date DateAtStartOfYear(TimerGameCalendar::Ye return (DAYS_IN_YEAR * year) + number_of_leap_years; } +/** + * Calculate the year of a given date. + * @param date The date to consider. + * @return the year. + */ +static inline TimerGameCalendar::Year DateToYear(TimerGameCalendar::Date date) +{ + return date / DAYS_IN_LEAP_YEAR; +} + /** * The date of the first day of the original base year. */ diff --git a/src/order_cmd.cpp b/src/order_cmd.cpp index 038f630126..a8f9017e49 100644 --- a/src/order_cmd.cpp +++ b/src/order_cmd.cpp @@ -1941,10 +1941,10 @@ VehicleOrderID ProcessConditionalOrder(const Order *order, const Vehicle *v) case OCV_RELIABILITY: skip_order = OrderConditionCompare(occ, ToPercent16(v->reliability), value); break; case OCV_MAX_RELIABILITY: skip_order = OrderConditionCompare(occ, ToPercent16(v->GetEngine()->reliability), value); break; case OCV_MAX_SPEED: skip_order = OrderConditionCompare(occ, v->GetDisplayMaxSpeed() * 10 / 16, value); break; - case OCV_AGE: skip_order = OrderConditionCompare(occ, v->age / DAYS_IN_LEAP_YEAR, value); break; + case OCV_AGE: skip_order = OrderConditionCompare(occ, DateToYear(v->age), value); break; case OCV_REQUIRES_SERVICE: skip_order = OrderConditionCompare(occ, v->NeedsServicing(), value); break; case OCV_UNCONDITIONALLY: skip_order = true; break; - case OCV_REMAINING_LIFETIME: skip_order = OrderConditionCompare(occ, std::max(v->max_age - v->age + DAYS_IN_LEAP_YEAR - 1, 0) / DAYS_IN_LEAP_YEAR, value); break; + case OCV_REMAINING_LIFETIME: skip_order = OrderConditionCompare(occ, std::max(DateToYear(v->max_age - v->age + DAYS_IN_LEAP_YEAR - 1), 0), value); break; default: NOT_REACHED(); } diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 607140f31b..5d1a705f3c 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -2495,9 +2495,9 @@ struct VehicleDetailsWindow : Window { Rect tr = r.Shrink(WidgetDimensions::scaled.framerect); /* Draw running cost */ - SetDParam(1, v->age / DAYS_IN_LEAP_YEAR); + SetDParam(1, DateToYear(v->age)); SetDParam(0, (v->age + DAYS_IN_YEAR < v->max_age) ? STR_VEHICLE_INFO_AGE : STR_VEHICLE_INFO_AGE_RED); - SetDParam(2, v->max_age / DAYS_IN_LEAP_YEAR); + SetDParam(2, DateToYear(v->max_age)); SetDParam(3, v->GetDisplayRunningCost()); DrawString(tr, STR_VEHICLE_INFO_AGE_RUNNING_COST_YR); tr.top += FONT_HEIGHT_NORMAL;