OpenTTD Source 20260421-master-gc2fbc6fdeb
TimerGame< T > Class Template Reference

Template class for all TimerGame based timers. More...

#include <timer_game_common.h>

Data Structures

struct  DateTag
 The tag for making Date StrongType. More...
struct  YearTag
 The tag for making Year StrongType. More...
struct  YearMonthDay
 Data structure to convert between Date and triplet (year, month, and day). More...
struct  TPeriod
struct  TStorage

Public Types

enum class  Trigger : uint8_t {
  Day , Week , Month , Quarter ,
  Year
}
 Trigger reasons for the timer based triggers. More...
enum class  Priority : uint8_t {
  None , Company , Disaster , Engine ,
  Industry , Station , Subsidy , Town ,
  Vehicle
}
 Different levels of priority to run the timers in. More...
using Date = StrongType::Typedef<int32_t, DateTag<T>, StrongType::Compare, StrongType::Integer>
 The type to store our dates in.
using DateFract = uint16_t
 The fraction of a date we're in, i.e.
using Year = StrongType::Typedef<int32_t, struct YearTag<T>, StrongType::Compare, StrongType::Integer>
 Type for the year, note: 0 based, i.e.
using Month = uint8_t
 Type for the month, note: 0 based, i.e.
using Day = uint8_t
 Type for the day of the month, note: 1 based, first day of a month is 1.
using TElapsed = Trigger

Static Public Member Functions

static constexpr bool IsLeapYear (Year year)
 Checks whether the given year is a leap year or not.
static YearMonthDay CalendarConvertDateToYMD (Date date)
 Converts a Date to a Year, Month & Day.
static Date CalendarConvertYMDToDate (Year year, Month month, Day day)
 Converts a tuple of Year, Month and Day to a Date.
static constexpr Year DateToYear (Date date)
 Calculate the year of a given date.
static constexpr Date DateAtStartOfYear (Year year)
 Calculate the date of the first day of a given year.

Detailed Description

template<class T>
class TimerGame< T >

Template class for all TimerGame based timers.

As Calendar and Economy are very similar, this class is used to share code between them.

IntervalTimer and TimeoutTimer based on this Timer are a bit unusual, as their count is always one. You create those timers based on a transition: a new day, a new month or a new year.

Additionally, you need to set a priority. To ensure deterministic behaviour, events are executed in priority. It is important that if you assign NONE, you do not use Random() in your callback. Other than that, make sure you only set one callback per priority.

For example: IntervalTimer<TimerGameCalendar>({TimerGameCalendar::Trigger::Day, TimerGameCalendar::Priority::None}, [](uint count) {});

Note
Callbacks are executed in the game-thread.

Definition at line 31 of file timer_game_common.h.

Member Typedef Documentation

◆ Date

template<class T>
using TimerGame< T >::Date = StrongType::Typedef<int32_t, DateTag<T>, StrongType::Compare, StrongType::Integer>

The type to store our dates in.

Definition at line 36 of file timer_game_common.h.

◆ DateFract

template<class T>
using TimerGame< T >::DateFract = uint16_t

The fraction of a date we're in, i.e.

the number of ticks since the last date changeover.

Definition at line 39 of file timer_game_common.h.

◆ Day

template<class T>
using TimerGame< T >::Day = uint8_t

Type for the day of the month, note: 1 based, first day of a month is 1.

Definition at line 48 of file timer_game_common.h.

◆ Month

template<class T>
using TimerGame< T >::Month = uint8_t

Type for the month, note: 0 based, i.e.

0 = January, 11 = December.

Definition at line 46 of file timer_game_common.h.

◆ TElapsed

template<class T>
using TimerGame< T >::TElapsed = Trigger

Definition at line 144 of file timer_game_common.h.

◆ Year

template<class T>
using TimerGame< T >::Year = StrongType::Typedef<int32_t, struct YearTag<T>, StrongType::Compare, StrongType::Integer>

Type for the year, note: 0 based, i.e.

starts at the year 0.

Definition at line 44 of file timer_game_common.h.

Member Enumeration Documentation

◆ Priority

template<class T>
enum class TimerGame::Priority : uint8_t
strong

Different levels of priority to run the timers in.

Enumerator
None 

These timers can be executed in any order; there is no Random() in them, so order is not relevant.

Company 

Changes to companies.

Disaster 

Running disaster logic.

Engine 

Running engine availability updates.

Industry 

Running industry production changes.

Station 

Processing of goods statistics.

Subsidy 

Creating new subsidies.

Town 

Town growth and rating management.

Vehicle 

Income and profit warnings.

Definition at line 109 of file timer_game_common.h.

◆ Trigger

template<class T>
enum class TimerGame::Trigger : uint8_t
strong

Trigger reasons for the timer based triggers.

Enumerator
Day 

Triggeres daily.

Week 

Triggers every Tuesday.

Month 

Triggered at the first of the month.

Quarter 

Triggered every first of January, April, July and October.

Year 

Triggered every first of January.

Definition at line 100 of file timer_game_common.h.

Member Function Documentation

◆ CalendarConvertDateToYMD()

template<class T>
template TimerGame< structEconomy >::YearMonthDay TimerGame< T >::CalendarConvertDateToYMD ( Date date)
static

Converts a Date to a Year, Month & Day.

Parameters
datethe date to convert from
Returns
YearMonthDay representation of the Date.

Definition at line 61 of file timer_game_common.cpp.

References TimerGameConst< T >::DAYS_IN_YEAR.

◆ CalendarConvertYMDToDate()

template<class T>
template TimerGame< structEconomy >::Date TimerGame< T >::CalendarConvertYMDToDate ( Year year,
Month month,
Day day )
static

Converts a tuple of Year, Month and Day to a Date.

Parameters
yearis a number between 0..MAX_YEAR
monthis a number between 0..11
dayis a number between 1..31
Returns
The equivalent date.

Definition at line 119 of file timer_game_common.cpp.

References _accum_days_for_month, DateAtStartOfYear(), and IsLeapYear().

◆ DateAtStartOfYear()

template<class T>
constexpr Date TimerGame< T >::DateAtStartOfYear ( Year year)
inlinestaticconstexpr

Calculate the date of the first day of a given year.

Parameters
yearthe year to get the first day of.
Returns
the date.

Definition at line 90 of file timer_game_common.h.

Referenced by CalendarConvertYMDToDate().

◆ DateToYear()

template<class T>
constexpr Year TimerGame< T >::DateToYear ( Date date)
inlinestaticconstexpr

Calculate the year of a given date.

Parameters
dateThe date to consider.
Returns
the year.

Definition at line 79 of file timer_game_common.h.

◆ IsLeapYear()

template<class T>
constexpr bool TimerGame< T >::IsLeapYear ( Year year)
inlinestaticconstexpr

Checks whether the given year is a leap year or not.

Parameters
yearThe year to check.
Returns
True if year is a leap year, otherwise false.

Definition at line 65 of file timer_game_common.h.

Referenced by CalendarConvertYMDToDate().


The documentation for this class was generated from the following files: