Fix assert failure in DoStartupNewCompany when called from AfterLoadGame
This commit is contained in:
@@ -545,14 +545,16 @@ void ResetCompanyLivery(Company *c)
|
||||
/**
|
||||
* Create a new company and sets all company variables default values
|
||||
*
|
||||
* @param is_ai is an AI company?
|
||||
* @param flags oepration flags
|
||||
* @param company CompanyID to use for the new company
|
||||
* @return the company struct
|
||||
*/
|
||||
Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY)
|
||||
Company *DoStartupNewCompany(DoStartupNewCompanyFlag flags, CompanyID company)
|
||||
{
|
||||
if (!Company::CanAllocateItem()) return nullptr;
|
||||
|
||||
const bool is_ai = (flags & DSNC_AI);
|
||||
|
||||
/* we have to generate colour before this company is valid */
|
||||
Colours colour = GenerateCompanyColour();
|
||||
|
||||
@@ -595,7 +597,7 @@ Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY)
|
||||
AI::BroadcastNewEvent(new ScriptEventCompanyNew(c->index), c->index);
|
||||
Game::NewEvent(new ScriptEventCompanyNew(c->index));
|
||||
|
||||
if (!is_ai) UpdateAllTownVirtCoords();
|
||||
if (!is_ai && !(flags & DSNC_DURING_LOAD)) UpdateAllTownVirtCoords();
|
||||
|
||||
return c;
|
||||
}
|
||||
@@ -849,7 +851,7 @@ CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
|
||||
/* Delete multiplayer progress bar */
|
||||
DeleteWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
|
||||
|
||||
Company *c = DoStartupNewCompany(false);
|
||||
Company *c = DoStartupNewCompany(DSNC_NONE);
|
||||
|
||||
/* A new company could not be created, revert to being a spectator */
|
||||
if (c == nullptr) {
|
||||
@@ -884,7 +886,7 @@ CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
|
||||
if (!(flags & DC_EXEC)) return CommandCost();
|
||||
|
||||
if (company_id != INVALID_COMPANY && (company_id >= MAX_COMPANIES || Company::IsValidID(company_id))) return CMD_ERROR;
|
||||
Company *c = DoStartupNewCompany(true, company_id);
|
||||
Company *c = DoStartupNewCompany(DSNC_AI, company_id);
|
||||
if (c != nullptr) {
|
||||
NetworkServerNewCompany(c, nullptr);
|
||||
DEBUG(desync, 1, "new_company_ai: date{%08x; %02x; %02x}, company_id: %u", _date, _date_fract, _tick_skip_counter, c->index);
|
||||
|
@@ -60,4 +60,16 @@ int CompanyServiceInterval(const Company *c, VehicleType type);
|
||||
|
||||
CompanyID GetDefaultLocalCompany();
|
||||
|
||||
/**
|
||||
* List of flags for DoStartupNewCompany.
|
||||
*/
|
||||
enum DoStartupNewCompanyFlag {
|
||||
DSNC_NONE = 0x000, ///< no flag is set
|
||||
DSNC_AI = 0x001, ///< start an AI company
|
||||
DSNC_DURING_LOAD = 0x002, ///< the save is currently being loaded, do not perform operations which require that loading has completed
|
||||
};
|
||||
DECLARE_ENUM_AS_BIT_SET(DoStartupNewCompanyFlag)
|
||||
|
||||
Company *DoStartupNewCompany(DoStartupNewCompanyFlag flags, CompanyID company = INVALID_COMPANY);
|
||||
|
||||
#endif /* COMPANY_FUNC_H */
|
||||
|
@@ -94,7 +94,6 @@ void ResetMusic();
|
||||
void CallWindowGameTickEvent();
|
||||
bool HandleBootstrap();
|
||||
|
||||
extern Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY);
|
||||
extern void ShowOSErrorBox(const char *buf, bool system);
|
||||
extern char *_config_file;
|
||||
|
||||
@@ -1041,7 +1040,7 @@ static void MakeNewGameDone()
|
||||
}
|
||||
|
||||
/* Create a single company */
|
||||
DoStartupNewCompany(false);
|
||||
DoStartupNewCompany(DSNC_NONE);
|
||||
|
||||
Company *c = Company::Get(COMPANY_FIRST);
|
||||
c->settings = _settings_client.company;
|
||||
|
@@ -64,6 +64,7 @@
|
||||
#include "../water.h"
|
||||
#include "../settings_func.h"
|
||||
#include "../animated_tile.h"
|
||||
#include "../company_func.h"
|
||||
|
||||
|
||||
#include "saveload_internal.h"
|
||||
@@ -73,8 +74,6 @@
|
||||
|
||||
#include "../safeguards.h"
|
||||
|
||||
extern Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY);
|
||||
|
||||
/**
|
||||
* Makes a tile canal or water depending on the surroundings.
|
||||
*
|
||||
@@ -972,7 +971,7 @@ bool AfterLoadGame()
|
||||
* 1 exception: network-games. Those can have 0 companies
|
||||
* But this exception is not true for non-dedicated network servers! */
|
||||
if (!Company::IsValidID(GetDefaultLocalCompany()) && (!_networking || (_networking && _network_server && !_network_dedicated))) {
|
||||
Company *c = DoStartupNewCompany(false);
|
||||
Company *c = DoStartupNewCompany(DSNC_DURING_LOAD);
|
||||
c->settings = _settings_client.company;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user