Feature: Setting for minutes per calendar year (#11428)

This commit is contained in:
Tyler Trahan
2024-01-23 18:33:54 -05:00
committed by GitHub
parent be8ed26db6
commit 21581b6ab3
15 changed files with 119 additions and 19 deletions

View File

@@ -32,6 +32,7 @@ TimerGameCalendar::Year TimerGameCalendar::year = {};
TimerGameCalendar::Month TimerGameCalendar::month = {};
TimerGameCalendar::Date TimerGameCalendar::date = {};
TimerGameCalendar::DateFract TimerGameCalendar::date_fract = {};
uint16_t TimerGameCalendar::sub_date_fract = {};
/**
* Converts a Date to a Year, Month & Day.
@@ -93,28 +94,42 @@ void TimeoutTimer<TimerGameCalendar>::Elapsed(TimerGameCalendar::TElapsed trigge
}
template<>
void TimerManager<TimerGameCalendar>::Elapsed([[maybe_unused]] TimerGameCalendar::TElapsed delta)
bool TimerManager<TimerGameCalendar>::Elapsed([[maybe_unused]] TimerGameCalendar::TElapsed delta)
{
assert(delta == 1);
if (_game_mode == GM_MENU) return;
if (_game_mode == GM_MENU) return false;
/* If calendar day progress is frozen, don't try to advance time. */
if (_settings_game.economy.minutes_per_calendar_year == CalendarTime::FROZEN_MINUTES_PER_YEAR) return false;
/* If we are using a non-default calendar progression speed, we need to check the sub_date_fract before updating date_fract. */
if (_settings_game.economy.minutes_per_calendar_year != CalendarTime::DEF_MINUTES_PER_YEAR) {
TimerGameCalendar::sub_date_fract++;
/* Check if we are ready to increment date_fract */
if (TimerGameCalendar::sub_date_fract < (Ticks::DAY_TICKS * _settings_game.economy.minutes_per_calendar_year) / CalendarTime::DEF_MINUTES_PER_YEAR) return false;
}
TimerGameCalendar::date_fract++;
if (TimerGameCalendar::date_fract < Ticks::DAY_TICKS) return;
TimerGameCalendar::date_fract = 0;
/* increase day counter */
/* Check if we entered a new day. */
if (TimerGameCalendar::date_fract < Ticks::DAY_TICKS) return true;
TimerGameCalendar::date_fract = 0;
TimerGameCalendar::sub_date_fract = 0;
/* Increase day counter. */
TimerGameCalendar::date++;
TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date);
/* check if we entered a new month? */
/* Check if we entered a new month. */
bool new_month = ymd.month != TimerGameCalendar::month;
/* check if we entered a new year? */
/* Check if we entered a new year. */
bool new_year = ymd.year != TimerGameCalendar::year;
/* update internal variables before calling the daily/monthly/yearly loops */
/* Update internal variables before calling the daily/monthly/yearly loops. */
TimerGameCalendar::month = ymd.month;
TimerGameCalendar::year = ymd.year;
@@ -137,7 +152,7 @@ void TimerManager<TimerGameCalendar>::Elapsed([[maybe_unused]] TimerGameCalendar
}
}
/* check if we reached the maximum year, decrement dates by a year */
/* If we reached the maximum year, decrement dates by a year. */
if (TimerGameCalendar::year == CalendarTime::MAX_YEAR + 1) {
int days_this_year;
@@ -145,6 +160,8 @@ void TimerManager<TimerGameCalendar>::Elapsed([[maybe_unused]] TimerGameCalendar
days_this_year = TimerGameCalendar::IsLeapYear(TimerGameCalendar::year) ? CalendarTime::DAYS_IN_LEAP_YEAR : CalendarTime::DAYS_IN_YEAR;
TimerGameCalendar::date -= days_this_year;
}
return true;
}
#ifdef WITH_ASSERT

View File

@@ -33,6 +33,7 @@ public:
static Month month; ///< Current month (0..11).
static Date date; ///< Current date in days (day counter).
static DateFract date_fract; ///< Fractional part of the day.
static uint16_t sub_date_fract; ///< Subpart of date_fract that we use when calendar days are slower than economy days.
static YearMonthDay ConvertDateToYMD(Date date);
static Date ConvertYMDToDate(Year year, Month month, Day day);
@@ -42,6 +43,11 @@ public:
/**
* Storage class for Calendar time constants.
*/
class CalendarTime : public TimerGameConst<struct Calendar> {};
class CalendarTime : public TimerGameConst<struct Calendar> {
public:
static constexpr int DEF_MINUTES_PER_YEAR = 12;
static constexpr int FROZEN_MINUTES_PER_YEAR = 0;
static constexpr int MAX_MINUTES_PER_YEAR = 10080; // One week of real time. The actual max that doesn't overflow TimerGameCalendar::sub_date_fract is 10627, but this is neater.
};
#endif /* TIMER_GAME_CALENDAR_H */

View File

@@ -121,14 +121,14 @@ void TimeoutTimer<TimerGameEconomy>::Elapsed(TimerGameEconomy::TElapsed trigger)
}
template<>
void TimerManager<TimerGameEconomy>::Elapsed([[maybe_unused]] TimerGameEconomy::TElapsed delta)
bool TimerManager<TimerGameEconomy>::Elapsed([[maybe_unused]] TimerGameEconomy::TElapsed delta)
{
assert(delta == 1);
if (_game_mode == GM_MENU) return;
if (_game_mode == GM_MENU) return false;
TimerGameEconomy::date_fract++;
if (TimerGameEconomy::date_fract < Ticks::DAY_TICKS) return;
if (TimerGameEconomy::date_fract < Ticks::DAY_TICKS) return true;
TimerGameEconomy::date_fract = 0;
/* increase day counter */
@@ -187,6 +187,8 @@ void TimerManager<TimerGameEconomy>::Elapsed([[maybe_unused]] TimerGameEconomy::
for (Vehicle *v : Vehicle::Iterate()) v->ShiftDates(-days_this_year);
for (LinkGraph *lg : LinkGraph::Iterate()) lg->ShiftDates(-days_this_year);
}
return true;
}
#ifdef WITH_ASSERT

View File

@@ -54,11 +54,13 @@ void TimeoutTimer<TimerGameRealtime>::Elapsed(TimerGameRealtime::TElapsed delta)
}
template<>
void TimerManager<TimerGameRealtime>::Elapsed(TimerGameRealtime::TElapsed delta)
bool TimerManager<TimerGameRealtime>::Elapsed(TimerGameRealtime::TElapsed delta)
{
for (auto timer : TimerManager<TimerGameRealtime>::GetTimers()) {
timer->Elapsed(delta);
}
return true;
}
#ifdef WITH_ASSERT

View File

@@ -51,13 +51,15 @@ void TimeoutTimer<TimerGameTick>::Elapsed(TimerGameTick::TElapsed delta)
}
template<>
void TimerManager<TimerGameTick>::Elapsed(TimerGameTick::TElapsed delta)
bool TimerManager<TimerGameTick>::Elapsed(TimerGameTick::TElapsed delta)
{
TimerGameTick::counter++;
for (auto timer : TimerManager<TimerGameTick>::GetTimers()) {
timer->Elapsed(delta);
}
return true;
}
#ifdef WITH_ASSERT

View File

@@ -78,8 +78,9 @@ public:
* Call the Elapsed() method of all active timers.
*
* @param value The amount of time that has elapsed.
* @return True iff time has progressed.
*/
static void Elapsed(TElapsed value);
static bool Elapsed(TElapsed value);
private:
/**

View File

@@ -49,7 +49,7 @@ void TimeoutTimer<TimerWindow>::Elapsed(TimerWindow::TElapsed delta)
}
template<>
void TimerManager<TimerWindow>::Elapsed(TimerWindow::TElapsed delta)
bool TimerManager<TimerWindow>::Elapsed(TimerWindow::TElapsed delta)
{
/* Make a temporary copy of the timers, as a timer's callback might add/remove other timers. */
auto timers = TimerManager<TimerWindow>::GetTimers();
@@ -57,6 +57,8 @@ void TimerManager<TimerWindow>::Elapsed(TimerWindow::TElapsed delta)
for (auto timer : timers) {
timer->Elapsed(delta);
}
return true;
}
#ifdef WITH_ASSERT