Codechange: Move Ticks into their own class

This commit is contained in:
Tyler Trahan
2023-08-16 09:01:24 -04:00
parent 30172fc037
commit fca2b37726
34 changed files with 132 additions and 116 deletions

View File

@@ -151,7 +151,7 @@ static const uint16_t _accum_days_for_month[] = {
*/
/* static */ void TimerGameCalendar::SetDate(TimerGameCalendar::Date date, TimerGameCalendar::DateFract fract)
{
assert(fract < DAY_TICKS);
assert(fract < Ticks::DAY_TICKS);
YearMonthDay ymd;
@@ -189,7 +189,7 @@ void TimerManager<TimerGameCalendar>::Elapsed(TimerGameCalendar::TElapsed delta)
if (_game_mode == GM_MENU) return;
TimerGameCalendar::date_fract++;
if (TimerGameCalendar::date_fract < DAY_TICKS) return;
if (TimerGameCalendar::date_fract < Ticks::DAY_TICKS) return;
TimerGameCalendar::date_fract = 0;
/* increase day counter */

View File

@@ -14,9 +14,6 @@
#include <chrono>
/** Estimation of how many ticks fit in a single second. */
static const uint TICKS_PER_SECOND = 1000 / MILLISECONDS_PER_TICK;
/**
* Timer that represents the game-ticks. It will pause when the game is paused.
*
@@ -24,6 +21,8 @@ static const uint TICKS_PER_SECOND = 1000 / MILLISECONDS_PER_TICK;
*/
class TimerGameTick {
public:
using Ticks = int32_t; ///< The type to store ticks in
using TPeriod = uint;
using TElapsed = uint;
struct TStorage {
@@ -33,4 +32,28 @@ public:
static uint64_t counter; ///< Monotonic counter, in ticks, since start of game.
};
/**
* Storage class for Ticks constants.
*/
class Ticks {
public:
static constexpr TimerGameTick::Ticks INVALID_TICKS = -1; ///< Representation of an invalid number of ticks.
/**
* 1 day is 74 ticks; TimerGameCalendar::date_fract used to be uint16_t and incremented by 885. On an overflow the new day begun and 65535 / 885 = 74.
* 1 tick is approximately 27 ms.
* 1 day is thus about 2 seconds (74 * 27 = 1998) on a machine that can run OpenTTD normally
*/
static constexpr TimerGameTick::Ticks DAY_TICKS = 74; ///< ticks per day
static constexpr TimerGameTick::Ticks TICKS_PER_SECOND = 1000 / MILLISECONDS_PER_TICK; ///< Estimation of how many ticks fit in a single second.
static constexpr TimerGameTick::Ticks STATION_RATING_TICKS = 185; ///< Cycle duration for updating station rating.
static constexpr TimerGameTick::Ticks STATION_ACCEPTANCE_TICKS = 250; ///< Cycle duration for updating station acceptance.
static constexpr TimerGameTick::Ticks STATION_LINKGRAPH_TICKS = 504; ///< Cycle duration for cleaning dead links.
static constexpr TimerGameTick::Ticks CARGO_AGING_TICKS = 185; ///< Cycle duration for aging cargo.
static constexpr TimerGameTick::Ticks INDUSTRY_PRODUCE_TICKS = 256; ///< Cycle duration for industry production.
static constexpr TimerGameTick::Ticks TOWN_GROWTH_TICKS = 70; ///< Cycle duration for towns trying to grow (this originates from the size of the town array in TTD).
static constexpr TimerGameTick::Ticks INDUSTRY_CUT_TREE_TICKS = INDUSTRY_PRODUCE_TICKS * 2; ///< Cycle duration for lumber mill's extra action.
};
#endif /* TIMER_GAME_TICK_H */