From c7b51a8c3a2b1067f66fc01806549686b4917dbc Mon Sep 17 00:00:00 2001 From: Tyler Trahan Date: Fri, 11 Aug 2023 09:32:42 -0400 Subject: [PATCH] Codechange: Use proper date types in various places (#11177) --- src/currency.h | 5 +++-- src/graph_gui.cpp | 2 +- src/newgrf.cpp | 2 +- src/order_cmd.cpp | 2 +- src/settings_gui.cpp | 8 ++++---- src/toolbar_gui.cpp | 2 +- src/vehicle.cpp | 2 +- 7 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/currency.h b/src/currency.h index 33d8d088a2..d2e864d964 100644 --- a/src/currency.h +++ b/src/currency.h @@ -14,8 +14,9 @@ #include "string_func.h" #include "strings_type.h" -static const int CF_NOEURO = 0; ///< Currency never switches to the Euro (as far as known). -static const int CF_ISEURO = 1; ///< Currency _is_ the Euro. +static constexpr TimerGameCalendar::Year CF_NOEURO = 0; ///< Currency never switches to the Euro (as far as known). +static constexpr TimerGameCalendar::Year CF_ISEURO = 1; ///< Currency _is_ the Euro. +static constexpr TimerGameCalendar::Year MIN_EURO_YEAR = 2000; ///< The earliest year custom currencies may switch to the Euro. /** * This enum gives the currencies a unique id which must be maintained for diff --git a/src/graph_gui.cpp b/src/graph_gui.cpp index 6559ad485e..dba31fe4fe 100644 --- a/src/graph_gui.cpp +++ b/src/graph_gui.cpp @@ -582,7 +582,7 @@ public: } int mo = (TimerGameCalendar::month / 3 - nums) * 3; - int yr = TimerGameCalendar::year; + auto yr = TimerGameCalendar::year; while (mo < 0) { yr--; mo += 12; diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 8108f2eee1..34a443bb1f 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -6512,7 +6512,7 @@ bool GetGlobalVariable(byte param, uint32_t *value, const GRFFile *grffile) { switch (param) { case 0x00: // current date - *value = std::max(TimerGameCalendar::date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0); + *value = std::max(TimerGameCalendar::date - DAYS_TILL_ORIGINAL_BASE_YEAR, TimerGameCalendar::Date(0)); return true; case 0x01: // current year diff --git a/src/order_cmd.cpp b/src/order_cmd.cpp index a8f9017e49..8a6490bd76 100644 --- a/src/order_cmd.cpp +++ b/src/order_cmd.cpp @@ -1944,7 +1944,7 @@ VehicleOrderID ProcessConditionalOrder(const Order *order, const Vehicle *v) 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(DateToYear(v->max_age - v->age + DAYS_IN_LEAP_YEAR - 1), 0), value); break; + case OCV_REMAINING_LIFETIME: skip_order = OrderConditionCompare(occ, std::max(DateToYear(v->max_age - v->age + DAYS_IN_LEAP_YEAR - 1), TimerGameCalendar::Date(0)), value); break; default: NOT_REACHED(); } diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index a34651397c..8718f70e24 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -2844,13 +2844,13 @@ struct CustomCurrencyWindow : Window { break; case WID_CC_YEAR_DOWN: - _custom_currency.to_euro = (_custom_currency.to_euro <= 2000) ? CF_NOEURO : _custom_currency.to_euro - 1; + _custom_currency.to_euro = (_custom_currency.to_euro <= MIN_EURO_YEAR) ? CF_NOEURO : _custom_currency.to_euro - 1; if (_custom_currency.to_euro == CF_NOEURO) this->DisableWidget(WID_CC_YEAR_DOWN); this->EnableWidget(WID_CC_YEAR_UP); break; case WID_CC_YEAR_UP: - _custom_currency.to_euro = Clamp(_custom_currency.to_euro + 1, 2000, MAX_YEAR); + _custom_currency.to_euro = Clamp(_custom_currency.to_euro + 1, MIN_EURO_YEAR, MAX_YEAR); if (_custom_currency.to_euro == MAX_YEAR) this->DisableWidget(WID_CC_YEAR_UP); this->EnableWidget(WID_CC_YEAR_DOWN); break; @@ -2895,9 +2895,9 @@ struct CustomCurrencyWindow : Window { break; case WID_CC_YEAR: { // Year to switch to euro - int val = atoi(str); + TimerGameCalendar::Year val = atoi(str); - _custom_currency.to_euro = (val < 2000 ? CF_NOEURO : std::min(val, MAX_YEAR)); + _custom_currency.to_euro = (val < MIN_EURO_YEAR ? CF_NOEURO : std::min(val, MAX_YEAR)); break; } } diff --git a/src/toolbar_gui.cpp b/src/toolbar_gui.cpp index 9198d182e8..766da5b151 100644 --- a/src/toolbar_gui.cpp +++ b/src/toolbar_gui.cpp @@ -2505,7 +2505,7 @@ struct ScenarioEditorToolbarWindow : Window { /* Was 'cancel' pressed? */ if (str == nullptr) return; - int32_t value; + TimerGameCalendar::Year value; if (!StrEmpty(str)) { value = atoi(str); } else { diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 768b819ca3..0979b2c1f8 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -761,7 +761,7 @@ uint32_t Vehicle::GetGRFID() const */ void Vehicle::ShiftDates(int interval) { - this->date_of_last_service = std::max(this->date_of_last_service + interval, 0); + this->date_of_last_service = std::max(this->date_of_last_service + interval, TimerGameCalendar::Date(0)); /* date_of_last_service_newgrf is not updated here as it must stay stable * for vehicles outside of a depot. */ }