Remove redundant year/month date globals

pull/171/head
Jonathan G Rennison 4 years ago
parent c7f6a711b3
commit 53f9fa139d

@ -185,7 +185,7 @@ static const CheatEntry _cheats_ui[] = {
{CNM_ALL, SLE_BOOL, STR_CHEAT_NO_JETCRASH, &_cheats.no_jetcrash.value, &_cheats.no_jetcrash.been_used, nullptr },
{CNM_LOCAL_ONLY, SLE_BOOL, STR_CHEAT_SETUP_PROD, &_cheats.setup_prod.value, &_cheats.setup_prod.been_used, &ClickSetProdCheat },
{CNM_LOCAL_ONLY, SLE_UINT8, STR_CHEAT_EDIT_MAX_HL, &_settings_game.construction.max_heightlevel, &_cheats.edit_max_hl.been_used, &ClickChangeMaxHlCheat },
{CNM_LOCAL_ONLY, SLE_INT32, STR_CHEAT_CHANGE_DATE, &_cur_year, &_cheats.change_date.been_used, &ClickChangeDateCheat },
{CNM_LOCAL_ONLY, SLE_INT32, STR_CHEAT_CHANGE_DATE, &_cur_date_ymd.year, &_cheats.change_date.been_used, &ClickChangeDateCheat },
{CNM_ALL, SLF_NOT_IN_SAVE, STR_CHEAT_INFLATION_COST, &_economy.inflation_prices, &_extra_cheats.inflation_cost.been_used, nullptr },
{CNM_ALL, SLF_NOT_IN_SAVE, STR_CHEAT_INFLATION_INCOME, &_economy.inflation_payment, &_extra_cheats.inflation_income.been_used, nullptr },
};

@ -24,8 +24,6 @@
#include "safeguards.h"
Year _cur_year; ///< Current year, starting at 0
Month _cur_month; ///< Current month (0..11)
YearMonthDay _cur_date_ymd; ///< Current date as YearMonthDay struct
Date _date; ///< Current date in days (day counter)
DateFract _date_fract; ///< Fractional part of the day.
@ -55,8 +53,6 @@ void SetDate(Date date, DateFract fract)
_date = date;
_date_fract = fract;
ConvertDateToYMD(date, &ymd);
_cur_year = ymd.year;
_cur_month = ymd.month;
_cur_date_ymd = ymd;
SetScaledTickVariables();
}
@ -217,17 +213,17 @@ static void OnNewYear()
InvalidateWindowClassesData(WC_BUILD_STATION);
if (_network_server) NetworkServerYearlyLoop();
if (_cur_year == _settings_client.gui.semaphore_build_before) ResetSignalVariant();
if (_cur_date_ymd.year == _settings_client.gui.semaphore_build_before) ResetSignalVariant();
/* check if we reached end of the game (end of ending year) */
if (_cur_year == _settings_game.game_creation.ending_year + 1) {
if (_cur_date_ymd.year == _settings_game.game_creation.ending_year + 1) {
ShowEndGameChart();
/* check if we reached the maximum year, decrement dates by a year */
} else if (_cur_year == MAX_YEAR + 1) {
} else if (_cur_date_ymd.year == MAX_YEAR + 1) {
int days_this_year;
_cur_year--;
days_this_year = IsLeapYear(_cur_year) ? DAYS_IN_LEAP_YEAR : DAYS_IN_YEAR;
_cur_date_ymd.year--;
days_this_year = IsLeapYear(_cur_date_ymd.year) ? DAYS_IN_LEAP_YEAR : DAYS_IN_YEAR;
_date -= days_this_year;
for (Vehicle *v : Vehicle::Iterate()) v->date_of_last_service -= days_this_year;
for (LinkGraph *lg : LinkGraph::Iterate()) lg->ShiftDates(-days_this_year);
@ -247,7 +243,7 @@ static void OnNewYear()
*/
static void OnNewMonth()
{
if (_settings_client.gui.autosave != 0 && (_cur_month % _autosave_months[_settings_client.gui.autosave]) == 0) {
if (_settings_client.gui.autosave != 0 && (_cur_date_ymd.month % _autosave_months[_settings_client.gui.autosave]) == 0) {
_do_autosave = true;
SetWindowDirty(WC_STATUS_BAR, 0);
}
@ -316,15 +312,13 @@ void IncreaseDate()
ConvertDateToYMD(_date, &ymd);
/* check if we entered a new month? */
bool new_month = ymd.month != _cur_month;
bool new_month = ymd.month != _cur_date_ymd.month;
/* check if we entered a new year? */
bool new_year = ymd.year != _cur_year;
bool new_year = ymd.year != _cur_date_ymd.year;
/* update internal variables before calling the daily/monthly/yearly loops */
_cur_date_ymd = ymd;
_cur_month = ymd.month;
_cur_year = ymd.year;
/* yes, call various daily loops */
OnNewDay();

@ -12,8 +12,6 @@
#include "date_type.h"
extern Year _cur_year;
extern Month _cur_month;
extern YearMonthDay _cur_date_ymd;
extern Date _date;
extern DateFract _date_fract;
@ -32,7 +30,12 @@ void ConvertDateToYMD(Date date, YearMonthDay *ymd);
Date ConvertYMDToDate(Year year, Month month, Day day);
void SetScaledTickVariables();
#define YMD_TO_DATE(ymd) (ConvertYMDToDate(ymd.year, ymd.month, ymd.day))
inline Date ConvertYMDToDate(const YearMonthDay &ymd)
{
return ConvertYMDToDate(ymd.year, ymd.month, ymd.day);
}
#define _cur_year (static_cast<Year>(_cur_date_ymd.year))
/**
* Checks whether the given year is a leap year or not.

@ -720,7 +720,7 @@ static void CompaniesGenStatistics()
cur_company.Restore();
/* Only run the economic statics and update company stats every 3rd month (1st of quarter). */
if (!HasBit(1 << 0 | 1 << 3 | 1 << 6 | 1 << 9, _cur_month)) return;
if (!HasBit(1 << 0 | 1 << 3 | 1 << 6 | 1 << 9, _cur_date_ymd.month)) return;
for (Company *c : Company::Iterate()) {
/* Drop the oldest history off the end */
@ -874,8 +874,8 @@ static void CompaniesPayInterest()
if (c->money < 0) {
yearly_fee += -c->money *_economy.interest_rate / 100;
}
Money up_to_previous_month = yearly_fee * _cur_month / 12;
Money up_to_this_month = yearly_fee * (_cur_month + 1) / 12;
Money up_to_previous_month = yearly_fee * _cur_date_ymd.month / 12;
Money up_to_this_month = yearly_fee * (_cur_date_ymd.month + 1) / 12;
SubtractMoneyFromCompany(CommandCost(EXPENSES_LOAN_INT, up_to_this_month - up_to_previous_month));

@ -571,7 +571,7 @@ public:
nums = min(this->num_vert_lines, max(nums, c->num_valid_stat_ent));
}
int mo = (_cur_month / 3 - nums) * 3;
int mo = (_cur_date_ymd.month / 3 - nums) * 3;
int yr = _cur_year;
while (mo < 0) {
yr--;

@ -2018,7 +2018,7 @@ void NetworkServerMonthlyLoop()
{
NetworkAutoCleanCompanies();
NetworkAdminUpdate(ADMIN_FREQUENCY_MONTHLY);
if ((_cur_month % 3) == 0) NetworkAdminUpdate(ADMIN_FREQUENCY_QUARTERLY);
if ((_cur_date_ymd.month % 3) == 0) NetworkAdminUpdate(ADMIN_FREQUENCY_QUARTERLY);
}
/** Daily "callback". Called whenever the date changes. */

@ -10318,16 +10318,16 @@ void LoadNewGRF(uint load_index, uint file_index, uint num_baseset)
* so all NewGRFs are loaded equally. For this we use the
* start date of the game and we set the counters, etc. to
* 0 so they're the same too. */
YearMonthDay date_ymd = _cur_date_ymd;
Date date = _date;
Year year = _cur_year;
DateFract date_fract = _date_fract;
uint16 tick_counter = _tick_counter;
uint8 tick_skip_counter = _tick_skip_counter;
byte display_opt = _display_opt;
if (_networking) {
_cur_year = _settings_game.game_creation.starting_year;
_date = ConvertYMDToDate(_cur_year, 0, 1);
_cur_date_ymd = { _settings_game.game_creation.starting_year, 0, 1};
_date = ConvertYMDToDate(_cur_date_ymd);
_date_fract = 0;
_tick_counter = 0;
_tick_skip_counter = 0;
@ -10422,7 +10422,7 @@ void LoadNewGRF(uint load_index, uint file_index, uint num_baseset)
AfterLoadGRFs();
/* Now revert back to the original situation */
_cur_year = year;
_cur_date_ymd = date_ymd;
_date = date;
_date_fract = date_fract;
_tick_counter = tick_counter;

@ -1007,9 +1007,9 @@ void NewsLoop()
static byte _last_clean_month = 0;
if (_last_clean_month != _cur_month) {
if (_last_clean_month != _cur_date_ymd.month) {
RemoveOldNewsItems();
_last_clean_month = _cur_month;
_last_clean_month = _cur_date_ymd.month;
}
if (ReadyForNextTickerItem()) MoveToNextTickerItem();

@ -1661,7 +1661,6 @@ bool AfterLoadGame()
* Account for this in older games by adding an offset */
if (IsSavegameVersionBefore(SLV_31)) {
_date += DAYS_TILL_ORIGINAL_BASE_YEAR;
_cur_year += ORIGINAL_BASE_YEAR;
SetScaledTickVariables();
ConvertDateToYMD(_date, &_cur_date_ymd);

Loading…
Cancel
Save