2014-04-09 11:16:35 +01:00
|
|
|
/*****************************************************************************
|
2020-07-21 15:04:34 +02:00
|
|
|
* Copyright (c) 2014-2020 OpenRCT2 developers
|
2014-04-09 11:16:35 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* For a complete list of all authors, please refer to contributors.md
|
|
|
|
|
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
2014-04-09 11:16:35 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2014-04-09 11:16:35 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
2017-11-30 18:17:06 +01:00
|
|
|
#include "../Game.h"
|
2021-10-09 21:43:06 +02:00
|
|
|
#include "../util/Math.hpp"
|
2018-01-06 18:32:25 +01:00
|
|
|
#include "Date.h"
|
|
|
|
|
#include "StringIds.h"
|
2014-04-09 11:16:35 +01:00
|
|
|
|
2018-08-12 12:50:40 +01:00
|
|
|
#include <algorithm>
|
2018-06-22 23:01:56 +02:00
|
|
|
#include <time.h>
|
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint16_t gDateMonthTicks;
|
2020-08-25 19:06:38 -03:00
|
|
|
int32_t gDateMonthsElapsed;
|
2016-09-03 17:40:23 +00:00
|
|
|
|
2014-05-01 22:30:44 +02:00
|
|
|
// rct2: 0x00993988
|
2021-10-17 15:31:56 +02:00
|
|
|
const int16_t days_in_month[MONTH_COUNT] = {
|
|
|
|
|
31, 30, 31, 30, 31, 31, 30, 31,
|
|
|
|
|
};
|
2014-04-29 22:51:51 +02:00
|
|
|
|
2015-09-26 14:35:26 +01:00
|
|
|
const rct_string_id DateFormatStringIds[] = {
|
2017-06-06 23:24:18 +02:00
|
|
|
STR_DATE_FORMAT_DAY_MONTH_YEAR,
|
|
|
|
|
STR_DATE_FORMAT_MONTH_DAY_YEAR,
|
|
|
|
|
STR_DATE_FORMAT_YEAR_MONTH_DAY,
|
2021-10-17 15:31:56 +02:00
|
|
|
STR_DATE_FORMAT_YEAR_DAY_MONTH,
|
2015-09-26 14:35:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const rct_string_id DateFormatStringFormatIds[] = {
|
2017-06-06 23:24:18 +02:00
|
|
|
STR_DATE_FORMAT_DMY,
|
|
|
|
|
STR_DATE_FORMAT_MDY,
|
|
|
|
|
STR_DATE_FORMAT_YMD,
|
2021-10-17 15:31:56 +02:00
|
|
|
STR_DATE_FORMAT_YDM,
|
2015-09-26 14:35:26 +01:00
|
|
|
};
|
2014-04-29 22:51:51 +02:00
|
|
|
|
2020-06-08 15:11:07 +02:00
|
|
|
openrct2_timeofday gRealTimeOfDay;
|
2016-05-20 20:06:04 +01:00
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t date_get_month(int32_t months)
|
2014-04-09 11:16:35 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return months % MONTH_COUNT;
|
2014-04-09 11:16:35 +01:00
|
|
|
}
|
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t date_get_year(int32_t months)
|
2014-04-09 11:16:35 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return months / MONTH_COUNT;
|
2014-04-09 11:16:35 +01:00
|
|
|
}
|
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t date_get_total_months(int32_t month, int32_t year)
|
2014-04-10 03:23:12 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return (year - 1) * MONTH_COUNT + month;
|
2014-04-10 03:23:12 +01:00
|
|
|
}
|
|
|
|
|
|
2014-04-09 11:16:35 +01:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* rct2: 0x006C4494
|
|
|
|
|
*/
|
|
|
|
|
void date_reset()
|
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
gDateMonthsElapsed = 0;
|
|
|
|
|
gDateMonthTicks = 0;
|
2018-12-01 16:36:00 +01:00
|
|
|
gCurrentRealTimeTicks = 0;
|
2018-01-05 23:10:50 -05:00
|
|
|
}
|
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
void date_set(int32_t year, int32_t month, int32_t day)
|
2018-01-05 23:10:50 -05:00
|
|
|
{
|
2019-03-31 19:48:51 +02:00
|
|
|
year = std::clamp(year, 1, MAX_YEAR);
|
2020-04-22 17:09:29 +02:00
|
|
|
month = std::clamp(month, 1, static_cast<int>(MONTH_COUNT));
|
|
|
|
|
day = std::clamp(day, 1, static_cast<int>(days_in_month[month - 1]));
|
2018-01-05 23:10:50 -05:00
|
|
|
gDateMonthsElapsed = (year - 1) * MONTH_COUNT + month - 1;
|
2020-10-21 02:05:27 +02:00
|
|
|
gDateMonthTicks = TICKS_PER_MONTH / days_in_month[month - 1] * (day - 1) + 4;
|
2014-04-21 14:57:48 +05:30
|
|
|
}
|
2016-05-20 20:06:04 +01:00
|
|
|
|
2017-10-25 12:37:20 +01:00
|
|
|
void date_update()
|
|
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t monthTicks = gDateMonthTicks + 4;
|
2020-10-21 02:05:27 +02:00
|
|
|
if (monthTicks >= TICKS_PER_MONTH)
|
2017-10-25 12:37:20 +01:00
|
|
|
{
|
|
|
|
|
gDateMonthTicks = 0;
|
|
|
|
|
gDateMonthsElapsed++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-22 17:09:29 +02:00
|
|
|
gDateMonthTicks = floor2(static_cast<uint16_t>(monthTicks), 4);
|
2017-10-25 12:37:20 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-20 20:06:04 +01:00
|
|
|
void date_update_real_time_of_day()
|
|
|
|
|
{
|
2018-01-07 21:43:07 +01:00
|
|
|
time_t timestamp = time(nullptr);
|
2018-06-22 23:01:56 +02:00
|
|
|
struct tm* now = localtime(×tamp);
|
2016-05-20 20:06:04 +01:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
gRealTimeOfDay.second = now->tm_sec;
|
|
|
|
|
gRealTimeOfDay.minute = now->tm_min;
|
|
|
|
|
gRealTimeOfDay.hour = now->tm_hour;
|
2016-05-20 20:06:04 +01:00
|
|
|
}
|
2017-10-25 12:37:20 +01:00
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
bool date_is_day_start(int32_t monthTicks)
|
2017-10-25 12:37:20 +01:00
|
|
|
{
|
|
|
|
|
if (monthTicks < 4)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t prevMonthTick = monthTicks - 4;
|
|
|
|
|
int32_t currentMonth = date_get_month(gDateMonthsElapsed);
|
|
|
|
|
int32_t currentDaysInMonth = days_in_month[currentMonth];
|
2017-10-25 12:37:20 +01:00
|
|
|
return ((currentDaysInMonth * monthTicks) >> 16 != (currentDaysInMonth * prevMonthTick) >> 16);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
bool date_is_week_start(int32_t monthTicks)
|
2017-10-25 12:37:20 +01:00
|
|
|
{
|
|
|
|
|
return (monthTicks & 0x3FFF) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
bool date_is_fortnight_start(int32_t monthTicks)
|
2017-10-25 12:37:20 +01:00
|
|
|
{
|
|
|
|
|
return (monthTicks & 0x7FFF) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
bool date_is_month_start(int32_t monthTicks)
|
2017-10-25 12:37:20 +01:00
|
|
|
{
|
|
|
|
|
return (monthTicks == 0);
|
|
|
|
|
}
|