2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2006-08-14 14:21:15 +00:00
|
|
|
|
2007-12-26 13:50:40 +00:00
|
|
|
/** @file date_type.h Types related to the dates in OpenTTD. */
|
2007-02-23 18:55:07 +00:00
|
|
|
|
2007-12-26 13:50:40 +00:00
|
|
|
#ifndef DATE_TYPE_H
|
|
|
|
#define DATE_TYPE_H
|
2007-01-02 17:34:03 +00:00
|
|
|
|
2023-12-19 01:03:18 +00:00
|
|
|
#include "core/strong_typedef_type.hpp"
|
|
|
|
#include "core/math_func.hpp"
|
2010-05-13 09:44:44 +00:00
|
|
|
|
2006-08-14 14:21:15 +00:00
|
|
|
/**
|
2024-01-07 16:41:53 +00:00
|
|
|
* 1 day is 74 ticks; _date_fract used to be uint16_t and incremented by 885. On
|
2006-08-14 14:21:15 +00:00
|
|
|
* an overflow the new day begun and 65535 / 885 = 74.
|
2023-05-22 17:40:46 +00:00
|
|
|
* 1 tick is approximately 27 ms.
|
|
|
|
* 1 day is thus about 2 seconds (74 * 27 = 1998) on a machine that can run OpenTTD normally
|
2006-08-14 14:21:15 +00:00
|
|
|
*/
|
2010-05-13 09:44:44 +00:00
|
|
|
static const int DAY_TICKS = 74; ///< ticks per day
|
|
|
|
static const int DAYS_IN_YEAR = 365; ///< days per year
|
|
|
|
static const int DAYS_IN_LEAP_YEAR = 366; ///< sometimes, you need one day more...
|
2021-04-22 17:54:32 +00:00
|
|
|
static const int MONTHS_IN_YEAR = 12; ///< months per year
|
2006-08-14 14:21:15 +00:00
|
|
|
|
2023-04-14 20:49:12 +00:00
|
|
|
static const int SECONDS_PER_DAY = 2; ///< approximate seconds per day, not for precise calculations
|
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
typedef uint16_t DateFract; ///< The fraction of a date we're in, i.e. the number of ticks since the last date changeover
|
|
|
|
typedef int32_t Ticks; ///< The type to store ticks in
|
2023-12-19 01:03:18 +00:00
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
typedef int32_t Year; ///< Type for the year, note: 0 based, i.e. starts at the year 0.
|
|
|
|
typedef uint8_t Month; ///< Type for the month, note: 0 based, i.e. 0 = January, 11 = December.
|
|
|
|
typedef uint8_t Day; ///< Type for the day of the month, note: 1 based, first day of a month is 1.
|
2023-12-19 01:03:18 +00:00
|
|
|
|
|
|
|
/* The type to store our dates in */
|
|
|
|
using DateDelta = StrongType::Typedef<int32_t, struct DateDeltaTag, StrongType::Compare, StrongType::IntegerScalable>;
|
|
|
|
using Date = StrongType::Typedef<int32_t, struct DateTag, StrongType::Compare, StrongType::IntegerDelta<DateDelta>>;
|
|
|
|
|
|
|
|
/* Mixin for DateTicks */
|
|
|
|
struct DateTicksOperations {
|
|
|
|
template <typename TType, typename TBaseType>
|
|
|
|
struct mixin {
|
|
|
|
private:
|
|
|
|
TBaseType GetBase() const { return static_cast<const TType &>(*this).base(); }
|
|
|
|
|
|
|
|
public:
|
|
|
|
Date ToDate() const { return this->GetBase() / DAY_TICKS; }
|
|
|
|
DateFract ToDateFractRemainder() const { return this->GetBase() % DAY_TICKS; }
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/* The type to store dates in when tick-precision is required */
|
2024-01-08 23:16:54 +00:00
|
|
|
using DateTicksDelta = StrongType::Typedef<int64_t, struct DateTicksDeltaTag, StrongType::Compare, StrongType::IntegerScalable>;
|
|
|
|
using DateTicks = StrongType::Typedef<int64_t, struct DateTicksTag, StrongType::Compare, StrongType::IntegerDelta<DateTicksDelta>, DateTicksOperations>;
|
2023-12-19 01:03:18 +00:00
|
|
|
|
|
|
|
/* Mixin for DateTicksScaledDelta */
|
|
|
|
struct DateTicksScaledDeltaOperations {
|
|
|
|
template <typename TType, typename TBaseType>
|
|
|
|
struct mixin {
|
|
|
|
private:
|
|
|
|
TBaseType GetBase() const { return static_cast<const TType &>(*this).base(); }
|
|
|
|
|
|
|
|
public:
|
2024-01-27 16:28:45 +00:00
|
|
|
template<typename T>
|
|
|
|
T AsTicksT() const { return ClampTo<T>(this->GetBase()); }
|
|
|
|
|
|
|
|
Ticks AsTicks() const { return this->AsTicksT<Ticks>(); }
|
2023-12-19 01:03:18 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/* The type to store dates scaled by the day length factor in when tick-precision is required */
|
|
|
|
using DateTicksScaledDelta = StrongType::Typedef<int64_t, struct DateTicksScaledDeltaTag, StrongType::Compare, StrongType::IntegerScalable, DateTicksScaledDeltaOperations>;
|
|
|
|
using DateTicksScaled = StrongType::Typedef<int64_t, struct DateTicksScaledTag, StrongType::Compare, StrongType::IntegerDelta<DateTicksScaledDelta>>;
|
|
|
|
|
|
|
|
/* Mixin for TickMinutes, ClockFaceMinutes */
|
2024-01-06 17:33:42 +00:00
|
|
|
template <bool TNegativeCheck>
|
2023-12-19 01:03:18 +00:00
|
|
|
struct MinuteOperations {
|
|
|
|
template <typename TType, typename TBaseType>
|
|
|
|
struct mixin {
|
|
|
|
private:
|
2024-01-06 17:33:42 +00:00
|
|
|
TBaseType GetBase() const
|
|
|
|
{
|
|
|
|
TBaseType value = static_cast<const TType &>(*this).base();
|
|
|
|
if constexpr (TNegativeCheck) {
|
|
|
|
if (value < 0) {
|
|
|
|
value = (value % 1440) + 1440;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2023-12-19 01:03:18 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
int ClockMinute() const { return this->GetBase() % 60; }
|
|
|
|
int ClockHour() const { return (this->GetBase() / 60) % 24; }
|
|
|
|
int ClockHHMM() const { return (this->ClockHour() * 100) + this->ClockMinute(); }
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Mixin for ClockFaceMinutes */
|
|
|
|
struct ClockFaceMinuteOperations {
|
|
|
|
template <typename TType, typename TBaseType>
|
|
|
|
struct mixin {
|
|
|
|
static constexpr TType FromClockFace(int hours, int minutes)
|
|
|
|
{
|
|
|
|
return (TBaseType(hours) * 60) + minutes;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/* The type to store general clock-face minutes in (i.e. 0..1440) */
|
2024-01-06 17:33:42 +00:00
|
|
|
using ClockFaceMinutes = StrongType::Typedef<int, struct ClockFaceMinutesTag, StrongType::Compare, StrongType::Integer, MinuteOperations<false>, ClockFaceMinuteOperations>;
|
2023-12-19 01:03:18 +00:00
|
|
|
|
|
|
|
/* Mixin for TickMinutes */
|
|
|
|
struct TickMinuteOperations {
|
|
|
|
template <typename TType, typename TBaseType>
|
|
|
|
struct mixin {
|
|
|
|
private:
|
|
|
|
TBaseType GetBase() const { return static_cast<const TType &>(*this).base(); }
|
|
|
|
|
|
|
|
public:
|
|
|
|
TType ToSameDayClockTime(int hour, int minute) const
|
|
|
|
{
|
|
|
|
TBaseType day = DivTowardsNegativeInf<TBaseType>(this->GetBase(), 1440);
|
|
|
|
return (day * 1440) + (hour * 60) + minute;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClockFaceMinutes ToClockFaceMinutes() const
|
|
|
|
{
|
|
|
|
TBaseType minutes = this->GetBase() % 1440;
|
|
|
|
if (minutes < 0) minutes += 1440;
|
|
|
|
return minutes;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/* The type to store DateTicksScaled-based minutes in */
|
2024-01-06 17:33:42 +00:00
|
|
|
using TickMinutes = StrongType::Typedef<int64_t, struct TickMinutesTag, StrongType::Compare, StrongType::Integer, MinuteOperations<true>, TickMinuteOperations>;
|
2023-12-19 01:03:18 +00:00
|
|
|
|
2020-06-23 20:05:21 +00:00
|
|
|
#define DATE_UNIT_SIZE (_settings_time.time_in_minutes ? _settings_time.ticks_per_minute : (DAY_TICKS * _settings_game.economy.day_length_factor))
|
2015-08-01 18:47:09 +00:00
|
|
|
|
2011-05-21 11:26:37 +00:00
|
|
|
static const int STATION_RATING_TICKS = 185; ///< cycle duration for updating station rating
|
|
|
|
static const int STATION_ACCEPTANCE_TICKS = 250; ///< cycle duration for updating station acceptance
|
2013-05-19 14:26:14 +00:00
|
|
|
static const int STATION_LINKGRAPH_TICKS = 504; ///< cycle duration for cleaning dead links
|
2011-05-21 11:26:37 +00:00
|
|
|
static const int CARGO_AGING_TICKS = 185; ///< cycle duration for aging cargo
|
|
|
|
static const int INDUSTRY_PRODUCE_TICKS = 256; ///< cycle duration for industry production
|
|
|
|
static const int TOWN_GROWTH_TICKS = 70; ///< cycle duration for towns trying to grow. (this originates from the size of the town array in TTD
|
2011-06-09 16:35:09 +00:00
|
|
|
static const int INDUSTRY_CUT_TREE_TICKS = INDUSTRY_PRODUCE_TICKS * 2; ///< cycle duration for lumber mill's extra action
|
2011-05-21 11:26:37 +00:00
|
|
|
|
2006-08-20 12:39:17 +00:00
|
|
|
/*
|
|
|
|
* ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR and DAYS_TILL_ORIGINAL_BASE_YEAR are
|
|
|
|
* primarily used for loading newgrf and savegame data and returning some
|
|
|
|
* newgrf (callback) functions that were in the original (TTD) inherited
|
|
|
|
* format, where '_date == 0' meant that it was 1920-01-01.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** The minimum starting year/base year of the original TTD */
|
2023-12-19 01:03:18 +00:00
|
|
|
static constexpr Year ORIGINAL_BASE_YEAR = 1920;
|
2008-12-28 16:09:58 +00:00
|
|
|
/** The original ending year */
|
2023-12-19 01:03:18 +00:00
|
|
|
static constexpr Year ORIGINAL_END_YEAR = 2051;
|
2006-08-20 12:39:17 +00:00
|
|
|
/** The maximum year of the original TTD */
|
2023-12-19 01:03:18 +00:00
|
|
|
static constexpr Year ORIGINAL_MAX_YEAR = 2090;
|
2009-11-25 23:28:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the date of the first day of a given year.
|
|
|
|
* @param year the year to get the first day of.
|
|
|
|
* @return the date.
|
|
|
|
*/
|
2023-12-19 01:03:18 +00:00
|
|
|
static constexpr Date DateAtStartOfYear(Year year)
|
|
|
|
{
|
2024-01-07 16:41:53 +00:00
|
|
|
int32_t year_as_int = year;
|
2023-12-19 01:03:18 +00:00
|
|
|
uint 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;
|
|
|
|
}
|
2009-11-25 23:28:04 +00:00
|
|
|
|
2006-08-20 12:39:17 +00:00
|
|
|
/**
|
|
|
|
* The offset in days from the '_date == 0' till
|
|
|
|
* 'ConvertYMDToDate(ORIGINAL_BASE_YEAR, 0, 1)'
|
|
|
|
*/
|
2023-12-19 01:03:18 +00:00
|
|
|
static constexpr Date DAYS_TILL_ORIGINAL_BASE_YEAR = DateAtStartOfYear(ORIGINAL_BASE_YEAR);
|
2006-08-20 12:39:17 +00:00
|
|
|
|
2023-12-19 01:03:18 +00:00
|
|
|
static constexpr Date MIN_DATE = 0;
|
2023-11-29 20:32:54 +00:00
|
|
|
|
2009-11-25 23:28:04 +00:00
|
|
|
/** The absolute minimum & maximum years in OTTD */
|
2023-12-19 01:03:18 +00:00
|
|
|
static constexpr Year MIN_YEAR = 0;
|
2009-11-25 23:28:04 +00:00
|
|
|
|
2009-12-21 16:13:30 +00:00
|
|
|
/** The default starting year */
|
2023-12-19 01:03:18 +00:00
|
|
|
static constexpr Year DEF_START_YEAR = 1950;
|
2019-09-18 18:18:45 +00:00
|
|
|
/** The default scoring end year */
|
2023-12-19 01:03:18 +00:00
|
|
|
static constexpr Year DEF_END_YEAR = ORIGINAL_END_YEAR - 1;
|
2009-12-21 16:13:30 +00:00
|
|
|
|
2009-11-25 23:28:04 +00:00
|
|
|
/**
|
|
|
|
* MAX_YEAR, nicely rounded value of the number of years that can
|
|
|
|
* be encoded in a single 32 bits date, about 2^31 / 366 years.
|
|
|
|
*/
|
2010-05-13 09:44:44 +00:00
|
|
|
static const Year MAX_YEAR = 5000000;
|
2006-08-14 14:21:15 +00:00
|
|
|
|
2009-11-25 23:28:04 +00:00
|
|
|
/** The number of days till the last day */
|
2023-12-19 01:03:18 +00:00
|
|
|
static constexpr Date MAX_DATE = DateAtStartOfYear(MAX_YEAR + 1) - 1;
|
2015-08-01 18:47:09 +00:00
|
|
|
|
2009-01-04 11:32:26 +00:00
|
|
|
/**
|
|
|
|
* Data structure to convert between Date and triplet (year, month, and day).
|
|
|
|
* @see ConvertDateToYMD(), ConvertYMDToDate()
|
|
|
|
*/
|
2007-03-07 12:11:48 +00:00
|
|
|
struct YearMonthDay {
|
2009-01-04 11:32:26 +00:00
|
|
|
Year year; ///< Year (0...)
|
|
|
|
Month month; ///< Month (0..11)
|
|
|
|
Day day; ///< Day (1..31)
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2006-08-14 14:21:15 +00:00
|
|
|
|
2023-12-19 01:03:18 +00:00
|
|
|
static constexpr Year INVALID_YEAR = -1; ///< Representation of an invalid year
|
|
|
|
static constexpr Date INVALID_DATE = -1; ///< Representation of an invalid date
|
|
|
|
static constexpr DateTicks INVALID_DATE_TICKS = -1; ///< Representation of an invalid date ticks
|
|
|
|
static constexpr Ticks INVALID_TICKS = -1; ///< Representation of an invalid number of ticks
|
2006-09-28 18:42:35 +00:00
|
|
|
|
2007-12-26 13:50:40 +00:00
|
|
|
#endif /* DATE_TYPE_H */
|