OpenTTD Source  20241108-master-g80f628063a
TimerGame< T > Class Template Reference

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

#include <timer_game_common.h>

Data Structures

struct  DateTag
 The type to store our dates in. More...
 
struct  TPeriod
 
struct  TStorage
 
struct  YearMonthDay
 Data structure to convert between Date and triplet (year, month, and day). More...
 
struct  YearTag
 Type for the year, note: 0 based, i.e. More...
 

Public Types

enum  Trigger {
  DAY , WEEK , MONTH , QUARTER ,
  YEAR
}
 
enum  Priority {
  NONE , COMPANY , DISASTER , ENGINE ,
  INDUSTRY , STATION , SUBSIDY , TOWN ,
  VEHICLE
}
 
using Date = StrongType::Typedef< int32_t, DateTag< T >, StrongType::Compare, StrongType::Integer >
 
using DateFract = uint16_t
 The fraction of a date we're in, i.e. More...
 
using Year = StrongType::Typedef< int32_t, struct YearTag< T >, StrongType::Compare, StrongType::Integer >
 
using Month = uint8_t
 Type for the month, note: 0 based, i.e. More...
 
using Day = uint8_t
 Type for the day of the month, note: 1 based, first day of a month is 1.
 
using TElapsed = uint
 

Static Public Member Functions

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

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::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

◆ 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 38 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 44 of file timer_game_common.h.

Member Enumeration Documentation

◆ Priority

template<class T >
enum TimerGame::Priority
Enumerator
NONE 

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

Definition at line 105 of file timer_game_common.h.

Member Function Documentation

◆ CalendarConvertDateToYMD()

template<class T >
TimerGame< T >::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 64 of file timer_game_common.cpp.

◆ CalendarConvertYMDToDate()

template<class T >
TimerGame< T >::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 122 of file timer_game_common.cpp.

References _accum_days_for_month.

◆ DateAtStartOfYear()

template<class T >
static 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 88 of file timer_game_common.h.

◆ DateToYear()

template<class T >
static 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 77 of file timer_game_common.h.

◆ IsLeapYear()

template<class T >
static 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 63 of file timer_game_common.h.


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