1
0
Fork 0

Codechange: Split daily/monthly/yearly timers into economy and calendar timers

pull/11588/head
Tyler Trahan 2023-04-23 14:52:44 -04:00
parent 31352caba1
commit feacf0c2fc
12 changed files with 58 additions and 39 deletions

View File

@ -92,7 +92,7 @@ static int32_t ClickSetProdCheat(int32_t new_value, int32_t)
return _cheats.setup_prod.value; return _cheats.setup_prod.value;
} }
extern void EnginesMonthlyLoop(); extern void CalendarEnginesMonthlyLoop();
/** /**
* Handle changing of the current year. * Handle changing of the current year.
@ -119,7 +119,7 @@ static int32_t ClickChangeDateCheat(int32_t new_value, int32_t)
TimerGameCalendar::SetDate(new_calendar_date, TimerGameCalendar::date_fract); TimerGameCalendar::SetDate(new_calendar_date, TimerGameCalendar::date_fract);
TimerGameEconomy::SetDate(new_economy_date, TimerGameEconomy::date_fract); TimerGameEconomy::SetDate(new_economy_date, TimerGameEconomy::date_fract);
EnginesMonthlyLoop(); CalendarEnginesMonthlyLoop();
SetWindowDirty(WC_STATUS_BAR, 0); SetWindowDirty(WC_STATUS_BAR, 0);
InvalidateWindowClassesData(WC_BUILD_STATION, 0); InvalidateWindowClassesData(WC_BUILD_STATION, 0);
InvalidateWindowClassesData(WC_BUS_STATION, 0); InvalidateWindowClassesData(WC_BUS_STATION, 0);

View File

@ -36,7 +36,7 @@
#include "widgets/statusbar_widget.h" #include "widgets/statusbar_widget.h"
#include "company_cmd.h" #include "company_cmd.h"
#include "timer/timer.h" #include "timer/timer.h"
#include "timer/timer_game_calendar.h" #include "timer/timer_game_economy.h"
#include "timer/timer_game_tick.h" #include "timer/timer_game_tick.h"
#include "table/strings.h" #include "table/strings.h"
@ -746,7 +746,7 @@ void OnTick_Companies()
* A year has passed, update the economic data of all companies, and perhaps show the * A year has passed, update the economic data of all companies, and perhaps show the
* financial overview window of the local company. * financial overview window of the local company.
*/ */
static IntervalTimer<TimerGameCalendar> _companies_yearly({TimerGameCalendar::YEAR, TimerGameCalendar::Priority::COMPANY}, [](auto) static IntervalTimer<TimerGameEconomy> _economy_companies_yearly({TimerGameEconomy::YEAR, TimerGameEconomy::Priority::COMPANY}, [](auto)
{ {
/* Copy statistics */ /* Copy statistics */
for (Company *c : Company::Iterate()) { for (Company *c : Company::Iterate()) {

View File

@ -47,7 +47,7 @@
#include "core/backup_type.hpp" #include "core/backup_type.hpp"
#include "landscape_cmd.h" #include "landscape_cmd.h"
#include "timer/timer.h" #include "timer/timer.h"
#include "timer/timer_game_calendar.h" #include "timer/timer_game_economy.h"
#include "table/strings.h" #include "table/strings.h"
@ -936,7 +936,7 @@ static void ResetDisasterDelay()
_disaster_delay = GB(Random(), 0, 9) + 730; _disaster_delay = GB(Random(), 0, 9) + 730;
} }
static IntervalTimer<TimerGameCalendar> _disaster_daily({TimerGameCalendar::DAY, TimerGameCalendar::Priority::DISASTER}, [](auto) static IntervalTimer<TimerGameEconomy> _economy_disaster_daily({TimerGameEconomy::DAY, TimerGameEconomy::Priority::DISASTER}, [](auto)
{ {
if (--_disaster_delay != 0) return; if (--_disaster_delay != 0) return;

View File

@ -54,6 +54,7 @@
#include "vehicle_cmd.h" #include "vehicle_cmd.h"
#include "timer/timer.h" #include "timer/timer.h"
#include "timer/timer_game_calendar.h" #include "timer/timer_game_calendar.h"
#include "timer/timer_game_economy.h"
#include "table/strings.h" #include "table/strings.h"
#include "table/pricebase.h" #include "table/pricebase.h"
@ -1980,16 +1981,23 @@ void LoadUnloadStation(Station *st)
} }
/** /**
* Monthly update of the economic data (of the companies as well as economic fluctuations). * Every calendar month update of inflation.
*/ */
static IntervalTimer<TimerGameCalendar> _companies_monthly({TimerGameCalendar::MONTH, TimerGameCalendar::Priority::COMPANY}, [](auto) static IntervalTimer<TimerGameCalendar> _calendar_inflation_monthly({TimerGameCalendar::MONTH, TimerGameCalendar::Priority::COMPANY}, [](auto)
{ {
CompaniesPayInterest();
CompaniesGenStatistics();
if (_settings_game.economy.inflation) { if (_settings_game.economy.inflation) {
AddInflation(); AddInflation();
RecomputePrices(); RecomputePrices();
} }
});
/**
* Every economy month update of company economic data, plus economy fluctuations.
*/
static IntervalTimer<TimerGameEconomy> _economy_companies_monthly({ TimerGameEconomy::MONTH, TimerGameEconomy::Priority::COMPANY }, [](auto)
{
CompaniesGenStatistics();
CompaniesPayInterest();
HandleEconomyFluctuations(); HandleEconomyFluctuations();
}); });

View File

@ -904,7 +904,7 @@ static bool IsVehicleTypeDisabled(VehicleType type, bool ai)
} }
/** Daily check to offer an exclusive engine preview to the companies. */ /** Daily check to offer an exclusive engine preview to the companies. */
static IntervalTimer<TimerGameCalendar> _engines_daily({TimerGameCalendar::DAY, TimerGameCalendar::Priority::ENGINE}, [](auto) static IntervalTimer<TimerGameCalendar> _calendar_engines_daily({TimerGameCalendar::DAY, TimerGameCalendar::Priority::ENGINE}, [](auto)
{ {
for (Company *c : Company::Iterate()) { for (Company *c : Company::Iterate()) {
c->avail_railtypes = AddDateIntroducedRailTypes(c->avail_railtypes, TimerGameCalendar::date); c->avail_railtypes = AddDateIntroducedRailTypes(c->avail_railtypes, TimerGameCalendar::date);
@ -1089,7 +1089,7 @@ static void NewVehicleAvailable(Engine *e)
} }
/** Monthly update of the availability, reliability, and preview offers of the engines. */ /** Monthly update of the availability, reliability, and preview offers of the engines. */
void EnginesMonthlyLoop() void CalendarEnginesMonthlyLoop()
{ {
if (TimerGameCalendar::year < _year_engine_aging_stops) { if (TimerGameCalendar::year < _year_engine_aging_stops) {
bool refresh = false; bool refresh = false;
@ -1136,9 +1136,9 @@ void EnginesMonthlyLoop()
} }
} }
static IntervalTimer<TimerGameCalendar> _engines_monthly({TimerGameCalendar::MONTH, TimerGameCalendar::Priority::ENGINE}, [](auto) static IntervalTimer<TimerGameCalendar> _calendar_engines_monthly({TimerGameCalendar::MONTH, TimerGameCalendar::Priority::ENGINE}, [](auto)
{ {
EnginesMonthlyLoop(); CalendarEnginesMonthlyLoop();
}); });
/** /**

View File

@ -296,7 +296,7 @@ struct IndustryBuildData {
void SetupTargetCount(); void SetupTargetCount();
void TryBuildNewIndustry(); void TryBuildNewIndustry();
void MonthlyLoop(); void EconomyMonthlyLoop();
}; };
extern IndustryBuildData _industry_builder; extern IndustryBuildData _industry_builder;

View File

@ -45,6 +45,7 @@
#include "terraform_cmd.h" #include "terraform_cmd.h"
#include "timer/timer.h" #include "timer/timer.h"
#include "timer/timer_game_calendar.h" #include "timer/timer_game_calendar.h"
#include "timer/timer_game_economy.h"
#include "timer/timer_game_tick.h" #include "timer/timer_game_tick.h"
#include "table/strings.h" #include "table/strings.h"
@ -2393,7 +2394,7 @@ void IndustryBuildData::Reset()
} }
/** Monthly update of industry build data. */ /** Monthly update of industry build data. */
void IndustryBuildData::MonthlyLoop() void IndustryBuildData::EconomyMonthlyLoop()
{ {
static const int NEWINDS_PER_MONTH = 0x38000 / (10 * 12); // lower 16 bits is a float fraction, 3.5 industries per decade, divided by 10 * 12 months. static const int NEWINDS_PER_MONTH = 0x38000 / (10 * 12); // lower 16 bits is a float fraction, 3.5 industries per decade, divided by 10 * 12 months.
if (_settings_game.difficulty.industry_density == ID_FUND_ONLY) return; // 'no industries' setting. if (_settings_game.difficulty.industry_density == ID_FUND_ONLY) return; // 'no industries' setting.
@ -2970,13 +2971,13 @@ static void ChangeIndustryProduction(Industry *i, bool monthly)
} }
/** /**
* Daily handler for the industry changes * Every economy day handler for the industry changes
* Taking the original map size of 256*256, the number of random changes was always of just one unit. * Taking the original map size of 256*256, the number of random changes was always of just one unit.
* But it cannot be the same on smaller or bigger maps. That number has to be scaled up or down. * But it cannot be the same on smaller or bigger maps. That number has to be scaled up or down.
* For small maps, it implies that less than one change per month is required, while on bigger maps, * For small maps, it implies that less than one change per month is required, while on bigger maps,
* it would be way more. The daily loop handles those changes. * it would be way more. The daily loop handles those changes.
*/ */
static IntervalTimer<TimerGameCalendar> _industries_daily({TimerGameCalendar::DAY, TimerGameCalendar::Priority::INDUSTRY}, [](auto) static IntervalTimer<TimerGameEconomy> _economy_industries_daily({TimerGameEconomy::DAY, TimerGameEconomy::Priority::INDUSTRY}, [](auto)
{ {
_economy.industry_daily_change_counter += _economy.industry_daily_increment; _economy.industry_daily_change_counter += _economy.industry_daily_increment;
@ -3018,11 +3019,11 @@ static IntervalTimer<TimerGameCalendar> _industries_daily({TimerGameCalendar::DA
InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, IDIWD_PRODUCTION_CHANGE); InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, IDIWD_PRODUCTION_CHANGE);
}); });
static IntervalTimer<TimerGameCalendar> _industries_monthly({TimerGameCalendar::MONTH, TimerGameCalendar::Priority::INDUSTRY}, [](auto) static IntervalTimer<TimerGameEconomy> _economy_industries_monthly({TimerGameEconomy::MONTH, TimerGameEconomy::Priority::INDUSTRY}, [](auto)
{ {
Backup<CompanyID> cur_company(_current_company, OWNER_NONE, FILE_LINE); Backup<CompanyID> cur_company(_current_company, OWNER_NONE, FILE_LINE);
_industry_builder.MonthlyLoop(); _industry_builder.EconomyMonthlyLoop();
for (Industry *i : Industry::Iterate()) { for (Industry *i : Industry::Iterate()) {
UpdateIndustryStatistics(i); UpdateIndustryStatistics(i);

View File

@ -32,6 +32,7 @@
#include "../rev.h" #include "../rev.h"
#include "../timer/timer.h" #include "../timer/timer.h"
#include "../timer/timer_game_calendar.h" #include "../timer/timer_game_calendar.h"
#include "../timer/timer_game_economy.h"
#include <mutex> #include <mutex>
#include <condition_variable> #include <condition_variable>
@ -1813,17 +1814,23 @@ void NetworkServer_Tick(bool send_frame)
} }
} }
/** Yearly "callback". Called whenever the year changes. */ /** Calendar yearly "callback". Called whenever the calendar year changes. */
static IntervalTimer<TimerGameCalendar> _network_yearly({TimerGameCalendar::YEAR, TimerGameCalendar::Priority::NONE}, [](auto) static IntervalTimer<TimerGameCalendar> _calendar_network_yearly({ TimerGameCalendar::YEAR, TimerGameCalendar::Priority::NONE }, [](auto) {
{
if (!_network_server) return; if (!_network_server) return;
NetworkCheckRestartMap(); NetworkCheckRestartMap();
});
/** Economy yearly "callback". Called whenever the economy year changes. */
static IntervalTimer<TimerGameEconomy> _economy_network_yearly({TimerGameEconomy::YEAR, TimerGameEconomy::Priority::NONE}, [](auto)
{
if (!_network_server) return;
NetworkAdminUpdate(ADMIN_FREQUENCY_ANUALLY); NetworkAdminUpdate(ADMIN_FREQUENCY_ANUALLY);
}); });
/** Quarterly "callback". Called whenever the quarter changes. */ /** Quarterly "callback". Called whenever the economy quarter changes. */
static IntervalTimer<TimerGameCalendar> _network_quarterly({TimerGameCalendar::QUARTER, TimerGameCalendar::Priority::NONE}, [](auto) static IntervalTimer<TimerGameEconomy> _network_quarterly({TimerGameEconomy::QUARTER, TimerGameEconomy::Priority::NONE}, [](auto)
{ {
if (!_network_server) return; if (!_network_server) return;
@ -1831,8 +1838,8 @@ static IntervalTimer<TimerGameCalendar> _network_quarterly({TimerGameCalendar::Q
NetworkAdminUpdate(ADMIN_FREQUENCY_QUARTERLY); NetworkAdminUpdate(ADMIN_FREQUENCY_QUARTERLY);
}); });
/** Monthly "callback". Called whenever the month changes. */ /** Economy monthly "callback". Called whenever the economy month changes. */
static IntervalTimer<TimerGameCalendar> _network_monthly({TimerGameCalendar::MONTH, TimerGameCalendar::Priority::NONE}, [](auto) static IntervalTimer<TimerGameEconomy> _network_monthly({TimerGameEconomy::MONTH, TimerGameEconomy::Priority::NONE}, [](auto)
{ {
if (!_network_server) return; if (!_network_server) return;
@ -1840,16 +1847,16 @@ static IntervalTimer<TimerGameCalendar> _network_monthly({TimerGameCalendar::MON
NetworkAdminUpdate(ADMIN_FREQUENCY_MONTHLY); NetworkAdminUpdate(ADMIN_FREQUENCY_MONTHLY);
}); });
/** Weekly "callback". Called whenever the week changes. */ /** Economy weekly "callback". Called whenever the economy week changes. */
static IntervalTimer<TimerGameCalendar> _network_weekly({TimerGameCalendar::WEEK, TimerGameCalendar::Priority::NONE}, [](auto) static IntervalTimer<TimerGameEconomy> _network_weekly({TimerGameEconomy::WEEK, TimerGameEconomy::Priority::NONE}, [](auto)
{ {
if (!_network_server) return; if (!_network_server) return;
NetworkAdminUpdate(ADMIN_FREQUENCY_WEEKLY); NetworkAdminUpdate(ADMIN_FREQUENCY_WEEKLY);
}); });
/** Daily "callback". Called whenever the date changes. */ /** Daily "callback". Called whenever the economy date changes. */
static IntervalTimer<TimerGameCalendar> _network_daily({TimerGameCalendar::DAY, TimerGameCalendar::Priority::NONE}, [](auto) static IntervalTimer<TimerGameEconomy> _economy_network_daily({TimerGameEconomy::DAY, TimerGameEconomy::Priority::NONE}, [](auto)
{ {
if (!_network_server) return; if (!_network_server) return;

View File

@ -63,6 +63,7 @@
#include "newgrf_roadstop.h" #include "newgrf_roadstop.h"
#include "timer/timer.h" #include "timer/timer.h"
#include "timer/timer_game_calendar.h" #include "timer/timer_game_calendar.h"
#include "timer/timer_game_economy.h"
#include "timer/timer_game_tick.h" #include "timer/timer_game_tick.h"
#include "cheat_type.h" #include "cheat_type.h"
@ -4002,8 +4003,8 @@ void OnTick_Station()
} }
} }
/** Monthly loop for stations. */ /** Economy monthly loop for stations. */
static IntervalTimer<TimerGameCalendar> _stations_monthly({TimerGameCalendar::MONTH, TimerGameCalendar::Priority::STATION}, [](auto) static IntervalTimer<TimerGameEconomy> _economy_stations_monthly({TimerGameEconomy::MONTH, TimerGameEconomy::Priority::STATION}, [](auto)
{ {
for (Station *st : Station::Iterate()) { for (Station *st : Station::Iterate()) {
for (GoodsEntry &ge : st->goods) { for (GoodsEntry &ge : st->goods) {

View File

@ -27,7 +27,7 @@
#include "tile_cmd.h" #include "tile_cmd.h"
#include "subsidy_cmd.h" #include "subsidy_cmd.h"
#include "timer/timer.h" #include "timer/timer.h"
#include "timer/timer_game_calendar.h" #include "timer/timer_game_economy.h"
#include "table/strings.h" #include "table/strings.h"
@ -474,8 +474,8 @@ bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src)
return true; return true;
} }
/** Perform the monthly update of open subsidies, and try to create a new one. */ /** Perform the economy monthly update of open subsidies, and try to create a new one. */
static IntervalTimer<TimerGameCalendar> _subsidies_monthly({TimerGameCalendar::MONTH, TimerGameCalendar::Priority::SUBSIDY}, [](auto) static IntervalTimer<TimerGameEconomy> _economy_subsidies_monthly({TimerGameEconomy::MONTH, TimerGameEconomy::Priority::SUBSIDY}, [](auto)
{ {
bool modified = false; bool modified = false;

View File

@ -54,6 +54,7 @@
#include "tunnelbridge_cmd.h" #include "tunnelbridge_cmd.h"
#include "timer/timer.h" #include "timer/timer.h"
#include "timer/timer_game_calendar.h" #include "timer/timer_game_calendar.h"
#include "timer/timer_game_economy.h"
#include "timer/timer_game_tick.h" #include "timer/timer_game_tick.h"
#include "table/strings.h" #include "table/strings.h"
@ -3837,7 +3838,7 @@ CommandCost CheckforTownRating(DoCommandFlag flags, Town *t, TownRatingCheckType
return CommandCost(); return CommandCost();
} }
static IntervalTimer<TimerGameCalendar> _towns_monthly({TimerGameCalendar::MONTH, TimerGameCalendar::Priority::TOWN}, [](auto) static IntervalTimer<TimerGameEconomy> _economy_towns_monthly({TimerGameEconomy::MONTH, TimerGameEconomy::Priority::TOWN}, [](auto)
{ {
for (Town *t : Town::Iterate()) { for (Town *t : Town::Iterate()) {
/* Check for active town actions and decrement their counters. */ /* Check for active town actions and decrement their counters. */
@ -3864,7 +3865,7 @@ static IntervalTimer<TimerGameCalendar> _towns_monthly({TimerGameCalendar::MONTH
} }
}); });
static IntervalTimer<TimerGameCalendar> _towns_yearly({TimerGameCalendar::YEAR, TimerGameCalendar::Priority::TOWN}, [](auto) static IntervalTimer<TimerGameEconomy> _economy_towns_yearly({TimerGameEconomy::YEAR, TimerGameEconomy::Priority::TOWN}, [](auto)
{ {
/* Increment house ages */ /* Increment house ages */
for (TileIndex t = 0; t < Map::Size(); t++) { for (TileIndex t = 0; t < Map::Size(); t++) {

View File

@ -58,6 +58,7 @@
#include "newgrf_roadstop.h" #include "newgrf_roadstop.h"
#include "timer/timer.h" #include "timer/timer.h"
#include "timer/timer_game_calendar.h" #include "timer/timer_game_calendar.h"
#include "timer/timer_game_economy.h"
#include "timer/timer_game_tick.h" #include "timer/timer_game_tick.h"
#include "table/strings.h" #include "table/strings.h"
@ -2800,7 +2801,7 @@ void Vehicle::RemoveFromShared()
this->previous_shared = nullptr; this->previous_shared = nullptr;
} }
static IntervalTimer<TimerGameCalendar> _vehicles_yearly({TimerGameCalendar::YEAR, TimerGameCalendar::Priority::VEHICLE}, [](auto) static IntervalTimer<TimerGameEconomy> _economy_vehicles_yearly({TimerGameEconomy::YEAR, TimerGameEconomy::Priority::VEHICLE}, [](auto)
{ {
for (Vehicle *v : Vehicle::Iterate()) { for (Vehicle *v : Vehicle::Iterate()) {
if (v->IsPrimaryVehicle()) { if (v->IsPrimaryVehicle()) {