Split date types into calendar and economy dates

See: 735abfe1
This commit is contained in:
Jonathan G Rennison
2024-02-13 21:34:09 +00:00
parent fad5ee56e7
commit 7ce06e22b8
141 changed files with 1325 additions and 1082 deletions

View File

@@ -131,7 +131,7 @@ void CheckGameCompatibility(NetworkGameInfo &ngi, bool extended)
void FillStaticNetworkServerGameInfo()
{
_network_game_info.use_password = !_settings_client.network.server_password.empty();
_network_game_info.start_date = ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1);
_network_game_info.start_date = CalTime::ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1);
_network_game_info.clients_max = _settings_client.network.max_clients;
_network_game_info.companies_max = _settings_client.network.max_companies;
_network_game_info.map_width = MapSizeX();
@@ -157,7 +157,7 @@ const NetworkServerGameInfo *GetCurrentNetworkServerGameInfo()
*/
_network_game_info.companies_on = (byte)Company::GetNumItems();
_network_game_info.spectators_on = NetworkSpectatorCount();
_network_game_info.game_date = _date;
_network_game_info.game_date = CalTime::CurDate();
_network_game_info.ticks_playing = _scaled_tick_counter;
return &_network_game_info;
}
@@ -336,7 +336,7 @@ void SerializeNetworkGameInfoExtended(Packet *p, const NetworkServerGameInfo *in
*/
void DeserializeNetworkGameInfo(Packet *p, NetworkGameInfo *info, const GameInfoNewGRFLookupTable *newgrf_lookup_table)
{
static const Date MAX_DATE = ConvertYMDToDate(MAX_YEAR, 11, 31); // December is month 11
static const CalTime::Date MAX_DATE = CalTime::ConvertYMDToDate(CalTime::MAX_YEAR, 11, 31); // December is month 11
byte game_info_version = p->Recv_uint8();
NewGRFSerializationType newgrf_serialisation = NST_GRFID_MD5;
@@ -427,8 +427,8 @@ void DeserializeNetworkGameInfo(Packet *p, NetworkGameInfo *info, const GameInfo
info->clients_on = p->Recv_uint8 ();
info->spectators_on = p->Recv_uint8 ();
if (game_info_version < 3) { // 16 bits dates got scrapped and are read earlier
info->game_date = p->Recv_uint16() + DAYS_TILL_ORIGINAL_BASE_YEAR;
info->start_date = p->Recv_uint16() + DAYS_TILL_ORIGINAL_BASE_YEAR;
info->game_date = p->Recv_uint16() + CalTime::DAYS_TILL_ORIGINAL_BASE_YEAR;
info->start_date = p->Recv_uint16() + CalTime::DAYS_TILL_ORIGINAL_BASE_YEAR;
}
if (game_info_version < 6) while (p->Recv_uint8() != 0) {} // Used to contain the map-name.
@@ -456,7 +456,7 @@ void DeserializeNetworkGameInfo(Packet *p, NetworkGameInfo *info, const GameInfo
*/
void DeserializeNetworkGameInfoExtended(Packet *p, NetworkGameInfo *info)
{
static const Date MAX_DATE = ConvertYMDToDate(MAX_YEAR, 11, 31); // December is month 11
static const CalTime::Date MAX_DATE = CalTime::ConvertYMDToDate(CalTime::MAX_YEAR, 11, 31); // December is month 11
const uint8_t version = p->Recv_uint8();
if (version > SERVER_GAME_INFO_EXTENDED_MAX_VERSION) return; // Unknown version

View File

@@ -94,8 +94,8 @@ enum NewGRFSerializationType {
*/
struct NetworkServerGameInfo {
GRFConfig *grfconfig; ///< List of NewGRF files used
Date start_date; ///< When the game started
Date game_date; ///< Current date
CalTime::Date start_date; ///< When the game started
CalTime::Date game_date; ///< Current date
uint64_t ticks_playing; ///< Amount of ticks the game has been running unpaused.
uint32_t map_width; ///< Map width
uint32_t map_height; ///< Map height

View File

@@ -87,8 +87,8 @@ NetworkAddressList _broadcast_list; ///< List of broadcast a
uint32_t _sync_seed_1; ///< Seed to compare during sync checks.
uint64_t _sync_state_checksum; ///< State checksum to compare during sync checks.
uint32_t _sync_frame; ///< The frame to perform the sync check.
Date _last_sync_date; ///< The game date of the last successfully received sync frame
DateFract _last_sync_date_fract; ///< "
EconTime::Date _last_sync_date; ///< The game date of the last successfully received sync frame
EconTime::DateFract _last_sync_date_fract; ///< "
uint8_t _last_sync_tick_skip_counter; ///< "
uint32_t _last_sync_frame_counter; ///< "
bool _network_first_time; ///< Whether we have finished joining or not.
@@ -1160,19 +1160,19 @@ void NetworkGameLoop()
if (_network_server) {
/* Log the sync state to check for in-syncedness of replays. */
if (_date_fract == 0 && _tick_skip_counter == 0) {
if (EconTime::CurDateFract() == 0 && TickSkipCounter() == 0) {
/* We don't want to log multiple times if paused. */
static Date last_log;
if (last_log != _date) {
static EconTime::Date last_log;
if (last_log != EconTime::CurDate()) {
DEBUG(desync, 2, "sync: %s; %08x; %08x", debug_date_dumper().HexDate(), _random.state[0], _random.state[1]);
last_log = _date;
last_log = EconTime::CurDate();
}
}
#ifdef DEBUG_DUMP_COMMANDS
/* Loading of the debug commands from -ddesync>=1 */
static FILE *f = FioFOpenFile("commands.log", "rb", SAVE_DIR);
static Date next_date = 0;
static EconTime::Date next_date = 0;
static uint next_date_fract;
static uint next_tick_skip_counter;
static std::unique_ptr<CommandPacket> cp;
@@ -1184,7 +1184,7 @@ void NetworkGameLoop()
}
while (f != nullptr && !feof(f)) {
if (_date == next_date && _date_fract == next_date_fract) {
if (EconTime::CurDate() == next_date && EconTime::CurDateFract() == next_date_fract) {
if (cp != nullptr) {
NetworkSendCommand(cp->tile, cp->p1, cp->p2, cp->p3, cp->cmd & ~CMD_FLAGS_MASK, nullptr, cp->text.c_str(), cp->company, cp->aux_data.get());
DEBUG(net, 0, "injecting: %s; %02x; %06x; %08x; %08x; " OTTD_PRINTFHEX64PAD " %08x; \"%s\"%s (%s)",

View File

@@ -179,7 +179,7 @@ NetworkRecvStatus ServerNetworkAdminSocketHandler::SendWelcome()
p->Send_string(""); // Used to be map-name.
p->Send_uint32(_settings_game.game_creation.generation_seed);
p->Send_uint8 (_settings_game.game_creation.landscape);
p->Send_uint32(ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1).base());
p->Send_uint32(CalTime::ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1).base());
p->Send_uint16(MapSizeX());
p->Send_uint16(MapSizeY());
@@ -209,7 +209,7 @@ NetworkRecvStatus ServerNetworkAdminSocketHandler::SendDate()
{
Packet *p = new Packet(ADMIN_PACKET_SERVER_DATE);
p->Send_uint32(_date.base());
p->Send_uint32(CalTime::CurDate().base());
this->SendPacket(p);
return NETWORK_RECV_STATUS_OKAY;
@@ -330,7 +330,7 @@ NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCompanyInfo(const Company
p->Send_string(GetString(STR_PRESIDENT_NAME));
p->Send_uint8 (c->colour);
p->Send_bool (NetworkCompanyIsPassworded(c->index));
p->Send_uint32(c->inaugurated_year);
p->Send_uint32(c->inaugurated_year.base());
p->Send_bool (c->is_ai);
p->Send_uint8 (CeilDiv(c->months_of_bankruptcy, 3)); // send as quarters_of_bankruptcy

View File

@@ -25,8 +25,8 @@ struct NetworkClientInfo : NetworkClientInfoPool::PoolItem<&_networkclientinfo_p
ClientID client_id; ///< Client identifier (same as ClientState->client_id)
std::string client_name; ///< Name of the client
CompanyID client_playas; ///< As which company is this client playing (CompanyID)
Date join_date; ///< Gamedate the client has joined
DateFract join_date_fract;
EconTime::Date join_date; ///< Gamedate the client has joined
EconTime::DateFract join_date_fract;
uint8_t join_tick_skip_counter;
uint32_t join_frame;

View File

@@ -347,9 +347,9 @@ void ClientNetworkGameSocketHandler::ClientError(NetworkRecvStatus res)
CrashLog::WriteDesyncSavegame(desync_log.c_str(), deferred_save.name_buffer.c_str());
return false;
}
_last_sync_date = _date;
_last_sync_date_fract = _date_fract;
_last_sync_tick_skip_counter = _tick_skip_counter;
_last_sync_date = EconTime::CurDate();
_last_sync_date_fract = EconTime::CurDateFract();
_last_sync_tick_skip_counter = TickSkipCounter();
_last_sync_frame_counter = _sync_frame;
_network_sync_records.clear();
_network_sync_record_counts.clear();
@@ -636,9 +636,9 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::SendDesyncLog(const std::strin
NetworkRecvStatus ClientNetworkGameSocketHandler::SendDesyncMessage(const char *msg)
{
Packet *p = new Packet(PACKET_CLIENT_DESYNC_MSG, SHRT_MAX);
p->Send_uint32(_date.base());
p->Send_uint16(_date_fract);
p->Send_uint8(_tick_skip_counter);
p->Send_uint32(EconTime::CurDate().base());
p->Send_uint16(EconTime::CurDateFract());
p->Send_uint8(TickSkipCounter());
p->Send_string(msg);
my_client->SendPacket(p);
return NETWORK_RECV_STATUS_OKAY;

View File

@@ -70,9 +70,10 @@ void NetworkPrintClients();
void NetworkHandlePauseChange(PauseMode prev_mode, PauseMode changed_mode);
/*** Commands ran by the server ***/
void NetworkServerDailyLoop();
void NetworkServerMonthlyLoop();
void NetworkServerYearlyLoop();
void NetworkServerEconomyDailyLoop();
void NetworkServerEconomyMonthlyLoop();
void NetworkServerEconomyYearlyLoop();
void NetworkServerCalendarYearlyLoop();
void NetworkServerSendConfigUpdate();
void NetworkServerUpdateGameInfo();
void NetworkServerShowStatusToConsole();

View File

@@ -397,7 +397,7 @@ protected:
if (const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(WID_NG_DATE); nwid->current_x != 0) {
/* current date */
Rect date = nwid->GetCurrentRect();
YearMonthDay ymd = ConvertDateToYMD(cur_item->info.game_date);
CalTime::YearMonthDay ymd = CalTime::ConvertDateToYMD(cur_item->info.game_date);
SetDParam(0, ymd.year);
DrawString(date.left, date.right, y + text_y_offset, STR_JUST_INT, TC_BLACK, SA_HOR_CENTER);
}
@@ -405,8 +405,8 @@ protected:
if (const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(WID_NG_YEARS); nwid->current_x != 0) {
/* number of years the game is running */
Rect years = nwid->GetCurrentRect();
YearMonthDay ymd_cur = ConvertDateToYMD(cur_item->info.game_date);
YearMonthDay ymd_start = ConvertDateToYMD(cur_item->info.start_date);
CalTime::YearMonthDay ymd_cur = CalTime::ConvertDateToYMD(cur_item->info.game_date);
CalTime::YearMonthDay ymd_start = CalTime::ConvertDateToYMD(cur_item->info.start_date);
SetDParam(0, ymd_cur.year - ymd_start.year);
DrawString(years.left, years.right, y + text_y_offset, STR_JUST_INT, TC_BLACK, SA_HOR_CENTER);
}

View File

@@ -31,7 +31,7 @@ void ShowSurveyResultTextfileWindow();
/** Company information stored at the client side */
struct NetworkCompanyInfo : NetworkCompanyStats {
std::string company_name; ///< Company name
Year inaugurated_year; ///< What year the company started in
CalTime::Year inaugurated_year; ///< What year the company started in
Money company_value; ///< The company value
Money money; ///< The amount of money the company has
Money income; ///< How much did the company earn last year

View File

@@ -76,10 +76,10 @@ extern NetworkAddressList _broadcast_list;
extern uint32_t _sync_seed_1;
extern uint64_t _sync_state_checksum;
extern uint32_t _sync_frame;
extern Date _last_sync_date;
extern DateFract _last_sync_date_fract;
extern uint8_t _last_sync_tick_skip_counter;
extern uint32_t _last_sync_frame_counter;
extern EconTime::Date _last_sync_date;
extern EconTime::DateFract _last_sync_date_fract;
extern uint8_t _last_sync_tick_skip_counter;
extern uint32_t _last_sync_frame_counter;
extern bool _network_first_time;
/* Vars needed for the join-GUI */
extern NetworkJoinStatus _network_join_status;

View File

@@ -1017,9 +1017,9 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_JOIN(Packet *p)
assert(NetworkClientInfo::CanAllocateItem());
NetworkClientInfo *ci = new NetworkClientInfo(this->client_id);
this->SetInfo(ci);
ci->join_date = _date;
ci->join_date_fract = _date_fract;
ci->join_tick_skip_counter = _tick_skip_counter;
ci->join_date = EconTime::CurDate();
ci->join_date_fract = EconTime::CurDateFract();
ci->join_tick_skip_counter = TickSkipCounter();
ci->join_frame = _frame_counter;
ci->client_name = client_name;
ci->client_playas = playas;
@@ -1322,13 +1322,13 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_DESYNC_LOG(Pack
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_DESYNC_MSG(Packet *p)
{
Date date = p->Recv_uint32();
DateFract date_fract = p->Recv_uint16();
EconTime::Date date = p->Recv_uint32();
EconTime::DateFract date_fract = p->Recv_uint16();
uint8_t tick_skip_counter = p->Recv_uint8();
std::string msg;
p->Recv_string(msg);
DEBUG(desync, 0, "Client-id %d desync msg: %s", this->client_id, msg.c_str());
extern void LogRemoteDesyncMsg(Date date, DateFract date_fract, uint8_t tick_skip_counter, uint32_t src_id, std::string msg);
extern void LogRemoteDesyncMsg(EconTime::Date date, EconTime::DateFract date_fract, uint8_t tick_skip_counter, uint32_t src_id, std::string msg);
LogRemoteDesyncMsg(date, date_fract, tick_skip_counter, this->client_id, std::move(msg));
return NETWORK_RECV_STATUS_OKAY;
}
@@ -1829,8 +1829,8 @@ void NetworkUpdateClientInfo(ClientID client_id)
/** Check if we want to restart the map */
static void NetworkCheckRestartMap()
{
if (_settings_client.network.restart_game_year != 0 && _cur_year >= _settings_client.network.restart_game_year) {
DEBUG(net, 3, "Auto-restarting map: year %d reached", _cur_year);
if (_settings_client.network.restart_game_year != 0 && CalTime::CurYear() >= _settings_client.network.restart_game_year) {
DEBUG(net, 3, "Auto-restarting map: year %d reached", CalTime::CurYear().base());
_settings_newgame.game_creation.generation_seed = GENERATE_NEW_SEED;
switch(_file_to_saveload.abstract_ftype) {
@@ -2149,25 +2149,30 @@ void NetworkServer_Tick(bool send_frame)
}
/** Yearly "callback". Called whenever the year changes. */
void NetworkServerYearlyLoop()
void NetworkServerCalendarYearlyLoop()
{
NetworkCheckRestartMap();
}
/** Yearly "callback". Called whenever the year changes. */
void NetworkServerEconomyYearlyLoop()
{
NetworkAdminUpdate(ADMIN_FREQUENCY_ANUALLY);
}
/** Monthly "callback". Called whenever the month changes. */
void NetworkServerMonthlyLoop()
void NetworkServerEconomyMonthlyLoop()
{
NetworkAutoCleanCompanies();
NetworkAdminUpdate(ADMIN_FREQUENCY_MONTHLY);
if ((_cur_date_ymd.month % 3) == 0) NetworkAdminUpdate(ADMIN_FREQUENCY_QUARTERLY);
if ((CalTime::CurMonth() % 3) == 0) NetworkAdminUpdate(ADMIN_FREQUENCY_QUARTERLY);
}
/** Daily "callback". Called whenever the date changes. */
void NetworkServerDailyLoop()
void NetworkServerEconomyDailyLoop()
{
NetworkAdminUpdate(ADMIN_FREQUENCY_DAILY);
if ((_date.base() % 7) == 3) NetworkAdminUpdate(ADMIN_FREQUENCY_WEEKLY);
if ((CalTime::CurDate().base() % 7) == 3) NetworkAdminUpdate(ADMIN_FREQUENCY_WEEKLY);
}
/**
@@ -2458,9 +2463,9 @@ char *NetworkServerDumpClients(char *buffer, const char *last)
ci->client_name.c_str(),
ci->client_playas);
if (ci->join_date != 0) {
YearMonthDay ymd = ConvertDateToYMD(ci->join_date);
EconTime::YearMonthDay ymd = EconTime::ConvertDateToYMD(ci->join_date);
buffer += seprintf(buffer, last, ", joined: %4i-%02i-%02i, %i, %i, frame: %08X",
ymd.year, ymd.month + 1, ymd.day, ci->join_date_fract, ci->join_tick_skip_counter, ci->join_frame);
ymd.year.base(), ymd.month + 1, ymd.day, ci->join_date_fract, ci->join_tick_skip_counter, ci->join_frame);
}
buffer += seprintf(buffer, last, "\n");
}