1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-28 06:34:33 +01:00

Codechange: use Date/Year constructor explicitly

This commit is contained in:
Rubidium
2025-01-01 09:13:39 +01:00
committed by rubidium42
parent f55ba40b13
commit 3956ed086a
22 changed files with 46 additions and 48 deletions

View File

@@ -70,7 +70,7 @@ uint16_t TimerGameCalendar::sub_date_fract = {};
TimerGameCalendar::date = date;
TimerGameCalendar::date_fract = fract;
TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(date);
TimerGameCalendar::year = ymd.year;
TimerGameCalendar::year = TimerGameCalendar::Year{ymd.year};
TimerGameCalendar::month = ymd.month;
}
@@ -157,10 +157,8 @@ bool TimerManager<TimerGameCalendar>::Elapsed([[maybe_unused]] TimerGameCalendar
/* If we reached the maximum year, decrement dates by a year. */
if (TimerGameCalendar::year == CalendarTime::MAX_YEAR + 1) {
int days_this_year;
TimerGameCalendar::year--;
days_this_year = TimerGameCalendar::IsLeapYear(TimerGameCalendar::year) ? CalendarTime::DAYS_IN_LEAP_YEAR : CalendarTime::DAYS_IN_YEAR;
TimerGameCalendar::Date days_this_year{TimerGameCalendar::IsLeapYear(TimerGameCalendar::year) ? CalendarTime::DAYS_IN_LEAP_YEAR : CalendarTime::DAYS_IN_YEAR};
TimerGameCalendar::date -= days_this_year;
}

View File

@@ -68,28 +68,28 @@ template <class T>
*/
/* There are 97 leap years in 400 years */
Year yr = 400 * (date.base() / (TimerGameConst<T>::DAYS_IN_YEAR * 400 + 97));
Year yr{400 * (date.base() / (TimerGameConst<T>::DAYS_IN_YEAR * 400 + 97))};
int rem = date.base() % (TimerGameConst<T>::DAYS_IN_YEAR * 400 + 97);
if (rem >= TimerGameConst<T>::DAYS_IN_YEAR * 100 + 25) {
/* There are 25 leap years in the first 100 years after
* every 400th year, as every 400th year is a leap year */
yr += 100;
yr += Year{100};
rem -= TimerGameConst<T>::DAYS_IN_YEAR * 100 + 25;
/* There are 24 leap years in the next couple of 100 years */
yr += 100 * (rem / (TimerGameConst<T>::DAYS_IN_YEAR * 100 + 24));
yr += Year{100 * (rem / (TimerGameConst<T>::DAYS_IN_YEAR * 100 + 24))};
rem = (rem % (TimerGameConst<T>::DAYS_IN_YEAR * 100 + 24));
}
if (!IsLeapYear(yr) && rem >= TimerGameConst<T>::DAYS_IN_YEAR * 4) {
/* The first 4 year of the century are not always a leap year */
yr += 4;
yr += Year{4};
rem -= TimerGameConst<T>::DAYS_IN_YEAR * 4;
}
/* There is 1 leap year every 4 years */
yr += 4 * (rem / (TimerGameConst<T>::DAYS_IN_YEAR * 4 + 1));
yr += Year{4 * (rem / (TimerGameConst<T>::DAYS_IN_YEAR * 4 + 1))};
rem = rem % (TimerGameConst<T>::DAYS_IN_YEAR * 4 + 1);
/* The last (max 3) years to account for; the first one

View File

@@ -77,7 +77,7 @@ public:
static constexpr Year DateToYear(Date date)
{
/* Hardcode the number of days in a year because we can't access CalendarTime from here. */
return date.base() / 366;
return Year{date.base() / 366};
}
/**
@@ -88,10 +88,10 @@ public:
static constexpr Date DateAtStartOfYear(Year year)
{
int32_t year_as_int = year.base();
uint number_of_leap_years = (year == 0) ? 0 : ((year_as_int - 1) / 4 - (year_as_int - 1) / 100 + (year_as_int - 1) / 400 + 1);
int32_t number_of_leap_years = (year == 0) ? 0 : ((year_as_int - 1) / 4 - (year_as_int - 1) / 100 + (year_as_int - 1) / 400 + 1);
/* Hardcode the number of days in a year because we can't access CalendarTime from here. */
return (365 * year_as_int) + number_of_leap_years;
return Date{(365 * year_as_int) + number_of_leap_years};
}
enum Trigger {

View File

@@ -70,7 +70,7 @@ TimerGameEconomy::DateFract TimerGameEconomy::date_fract = {};
/* If we're using wallclock units, economy months have 30 days and an economy year has 360 days. */
const int total_months = (year.base() * EconomyTime::MONTHS_IN_YEAR) + month;
return (total_months * EconomyTime::DAYS_IN_ECONOMY_MONTH) + day - 1; // Day is 1-indexed but Date is 0-indexed, hence the - 1.
return TimerGameEconomy::Date{(total_months * EconomyTime::DAYS_IN_ECONOMY_MONTH) + day - 1}; // Day is 1-indexed but Date is 0-indexed, hence the - 1.
}
/**
@@ -179,13 +179,11 @@ bool TimerManager<TimerGameEconomy>::Elapsed([[maybe_unused]] TimerGameEconomy::
/* check if we reached the maximum year, decrement dates by a year */
if (TimerGameEconomy::year == EconomyTime::MAX_YEAR + 1) {
int days_this_year;
TimerGameEconomy::year--;
days_this_year = TimerGameEconomy::IsLeapYear(TimerGameEconomy::year) ? EconomyTime::DAYS_IN_LEAP_YEAR : EconomyTime::DAYS_IN_YEAR;
TimerGameEconomy::date -= days_this_year;
for (Vehicle *v : Vehicle::Iterate()) v->ShiftDates(-days_this_year);
for (LinkGraph *lg : LinkGraph::Iterate()) lg->ShiftDates(-days_this_year);
int days_this_year = TimerGameEconomy::IsLeapYear(TimerGameEconomy::year) ? EconomyTime::DAYS_IN_LEAP_YEAR : EconomyTime::DAYS_IN_YEAR;
TimerGameEconomy::date -= TimerGameEconomy::Date{days_this_year};
for (Vehicle *v : Vehicle::Iterate()) v->ShiftDates(TimerGameEconomy::Date{-days_this_year});
for (LinkGraph *lg : LinkGraph::Iterate()) lg->ShiftDates(TimerGameEconomy::Date{-days_this_year});
}
return true;