Merge branch 'origin/master' commit 'a499e9acdd385b57dd43caf88af3a6f7f53716ba'

This commit is contained in:
pelya
2020-03-20 00:10:46 +02:00
1448 changed files with 32573 additions and 27146 deletions
+73 -74
View File
@@ -1,5 +1,3 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
@@ -43,8 +41,8 @@
void ClearEnginesHiddenFlagOfCompany(CompanyID cid);
CompanyByte _local_company; ///< Company controlled by the human player at this client. Can also be #COMPANY_SPECTATOR.
CompanyByte _current_company; ///< Company currently doing an action.
CompanyID _local_company; ///< Company controlled by the human player at this client. Can also be #COMPANY_SPECTATOR.
CompanyID _current_company; ///< Company currently doing an action.
Colours _company_colours[MAX_COMPANIES]; ///< NOSAVE: can be determined from company structs.
CompanyManagerFace _company_manager_face; ///< for company manager face storage in openttd.cfg
uint _next_competitor_start; ///< the number of ticks before the next AI is started
@@ -104,17 +102,18 @@ void SetLocalCompany(CompanyID new_company)
/* company could also be COMPANY_SPECTATOR or OWNER_NONE */
assert(Company::IsValidID(new_company) || new_company == COMPANY_SPECTATOR || new_company == OWNER_NONE);
#ifdef ENABLE_NETWORK
/* If actually changing to another company, several windows need closing */
bool switching_company = _local_company != new_company;
/* Delete the chat window, if you were team chatting. */
InvalidateWindowData(WC_SEND_NETWORK_MSG, DESTTYPE_TEAM, _local_company);
#endif
if (switching_company) InvalidateWindowData(WC_SEND_NETWORK_MSG, DESTTYPE_TEAM, _local_company);
assert(IsLocalCompany());
_current_company = _local_company = new_company;
/* Delete any construction windows... */
DeleteConstructionWindows();
if (switching_company) DeleteConstructionWindows();
/* ... and redraw the whole screen. */
MarkWholeScreenDirty();
@@ -195,7 +194,7 @@ bool CheckCompanyHasMoney(CommandCost &cost)
{
if (cost.GetCost() > 0) {
const Company *c = Company::GetIfValid(_current_company);
if (c != NULL && cost.GetCost() > c->money) {
if (c != nullptr && cost.GetCost() > c->money) {
SetDParam(0, cost.GetCost());
cost.MakeError(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY);
return false;
@@ -241,7 +240,7 @@ static void SubtractMoneyFromAnyCompany(Company *c, CommandCost cost)
void SubtractMoneyFromCompany(CommandCost cost)
{
Company *c = Company::GetIfValid(_current_company);
if (c != NULL) SubtractMoneyFromAnyCompany(c, cost);
if (c != nullptr) SubtractMoneyFromAnyCompany(c, cost);
}
/**
@@ -264,8 +263,7 @@ void SubtractMoneyFromCompanyFract(CompanyID company, CommandCost cst)
/** Update the landscaping limits per company. */
void UpdateLandscapingLimits()
{
Company *c;
FOR_ALL_COMPANIES(c) {
for (Company *c : Company::Iterate()) {
c->terraform_limit = min(c->terraform_limit + _settings_game.construction.terraform_per_64k_frames, (uint32)_settings_game.construction.terraform_frame_burst << 16);
c->clear_limit = min(c->clear_limit + _settings_game.construction.clear_per_64k_frames, (uint32)_settings_game.construction.clear_frame_burst << 16);
c->tree_limit = min(c->tree_limit + _settings_game.construction.tree_per_64k_frames, (uint32)_settings_game.construction.tree_frame_burst << 16);
@@ -355,14 +353,13 @@ static void GenerateCompanyName(Company *c)
StringID str;
uint32 strp;
if (t->name == NULL && IsInsideMM(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST + 1)) {
if (t->name == nullptr && IsInsideMM(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST + 1)) {
str = t->townnametype - SPECSTR_TOWNNAME_START + SPECSTR_COMPANY_NAME_START;
strp = t->townnameparts;
verify_name:;
/* No companies must have this name already */
Company *cc;
FOR_ALL_COMPANIES(cc) {
for (const Company *cc : Company::Iterate()) {
if (cc->name_1 == str && cc->name_2 == strp) goto bad_town_name;
}
@@ -448,8 +445,7 @@ static Colours GenerateCompanyColour()
}
/* Move the colours that look similar to each company's colour to the side */
Company *c;
FOR_ALL_COMPANIES(c) {
for (const Company *c : Company::Iterate()) {
Colours pcolour = (Colours)c->colour;
for (uint i = 0; i < COLOUR_END; i++) {
@@ -495,8 +491,7 @@ restart:;
GetString(buffer, STR_PRESIDENT_NAME, lastof(buffer));
if (Utf8StringLength(buffer) >= MAX_LENGTH_PRESIDENT_NAME_CHARS) continue;
Company *cc;
FOR_ALL_COMPANIES(cc) {
for (const Company *cc : Company::Iterate()) {
if (c != cc) {
/* Reserve extra space so even overlength president names can be compared. */
char buffer2[(MAX_LENGTH_PRESIDENT_NAME_CHARS + 1) * MAX_CHAR_LENGTH];
@@ -522,8 +517,7 @@ void ResetCompanyLivery(Company *c)
c->livery[scheme].colour2 = c->colour;
}
Group *g;
FOR_ALL_GROUPS(g) {
for (Group *g : Group::Iterate()) {
if (g->owner == c->index) {
g->livery.in_use = 0;
g->livery.colour1 = c->colour;
@@ -541,7 +535,7 @@ void ResetCompanyLivery(Company *c)
*/
Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY)
{
if (!Company::CanAllocateItem()) return NULL;
if (!Company::CanAllocateItem()) return nullptr;
/* we have to generate colour before this company is valid */
Colours colour = GenerateCompanyColour();
@@ -550,7 +544,7 @@ Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY)
if (company == INVALID_COMPANY) {
c = new Company(STR_SV_UNNAMED, is_ai);
} else {
if (Company::IsValidID(company)) return NULL;
if (Company::IsValidID(company)) return nullptr;
c = new (company) Company(STR_SV_UNNAMED, is_ai);
}
@@ -564,7 +558,7 @@ Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY)
c->share_owners[0] = c->share_owners[1] = c->share_owners[2] = c->share_owners[3] = INVALID_OWNER;
c->avail_railtypes = GetCompanyRailtypes(c->index);
c->avail_roadtypes = GetCompanyRoadtypes(c->index);
c->avail_roadtypes = GetCompanyRoadTypes(c->index);
c->inaugurated_year = _cur_year;
RandomCompanyManagerFaceBits(c->face, (GenderEthnicity)Random(), false, false); // create a random company manager face
@@ -597,15 +591,11 @@ void StartupCompanies()
/** Start a new competitor company if possible. */
static void MaybeStartNewCompany()
{
#ifdef ENABLE_NETWORK
if (_networking && Company::GetNumItems() >= _settings_client.network.max_companies) return;
#endif /* ENABLE_NETWORK */
Company *c;
if (_networking && Company::GetNumItems() >= _settings_client.network.max_companies) return false;
/* count number of competitors */
uint n = 0;
FOR_ALL_COMPANIES(c) {
for (const Company *c : Company::Iterate()) {
if (c->is_ai) n++;
}
@@ -672,11 +662,11 @@ static void HandleBankruptcyTakeover(Company *c)
/* Did we ask everyone for bankruptcy? If so, bail out. */
if (c->bankrupt_asked == MAX_UVALUE(CompanyMask)) return;
Company *c2, *best = NULL;
Company *best = nullptr;
int32 best_performance = -1;
/* Ask the company with the highest performance history first */
FOR_ALL_COMPANIES(c2) {
for (Company *c2 : Company::Iterate()) {
if (c2->bankrupt_asked == 0 && // Don't ask companies going bankrupt themselves
!HasBit(c->bankrupt_asked, c2->index) &&
best_performance < c2->old_economy[1].performance_history &&
@@ -708,7 +698,7 @@ void OnTick_Companies()
if (_game_mode == GM_EDITOR) return;
Company *c = Company::GetIfValid(_cur_company_tick_index);
if (c != NULL) {
if (c != nullptr) {
if (c->name_1 != 0) GenerateCompanyName(c);
if (c->bankrupt_asked != 0) HandleBankruptcyTakeover(c);
}
@@ -730,10 +720,8 @@ void OnTick_Companies()
*/
void CompaniesYearlyLoop()
{
Company *c;
/* Copy statistics */
FOR_ALL_COMPANIES(c) {
for (Company *c : Company::Iterate()) {
memmove(&c->yearly_expenses[1], &c->yearly_expenses[0], sizeof(c->yearly_expenses) - sizeof(c->yearly_expenses[0]));
memset(&c->yearly_expenses[0], 0, sizeof(c->yearly_expenses[0]));
SetWindowDirty(WC_FINANCES, c->index);
@@ -741,7 +729,7 @@ void CompaniesYearlyLoop()
if (_settings_client.gui.show_finances && _local_company != COMPANY_SPECTATOR) {
ShowCompanyFinances(_local_company);
c = Company::Get(_local_company);
Company *c = Company::Get(_local_company);
if (c->num_valid_stat_ent > 5 && c->old_economy[0].performance_history < c->old_economy[4].performance_history) {
if (_settings_client.sound.new_year) SndPlayFx(SND_01_BAD_YEAR);
} else {
@@ -753,14 +741,14 @@ void CompaniesYearlyLoop()
/**
* Fill the CompanyNewsInformation struct with the required data.
* @param c the current company.
* @param other the other company (use \c NULL if not relevant).
* @param other the other company (use \c nullptr if not relevant).
*/
void CompanyNewsInformation::FillData(const Company *c, const Company *other)
{
SetDParam(0, c->index);
GetString(this->company_name, STR_COMPANY_NAME, lastof(this->company_name));
if (other == NULL) {
if (other == nullptr) {
*this->other_company_name = '\0';
} else {
SetDParam(0, other->index);
@@ -782,9 +770,7 @@ void CompanyNewsInformation::FillData(const Company *c, const Company *other)
*/
void CompanyAdminUpdate(const Company *company)
{
#ifdef ENABLE_NETWORK
if (_network_server) NetworkAdminCompanyUpdate(company);
#endif /* ENABLE_NETWORK */
}
/**
@@ -794,9 +780,7 @@ void CompanyAdminUpdate(const Company *company)
*/
void CompanyAdminRemove(CompanyID company_id, CompanyRemoveReason reason)
{
#ifdef ENABLE_NETWORK
if (_network_server) NetworkAdminCompanyRemove(company_id, (AdminCompanyRemoveReason)reason);
#endif /* ENABLE_NETWORK */
}
/**
@@ -805,10 +789,9 @@ void CompanyAdminRemove(CompanyID company_id, CompanyRemoveReason reason)
* @param flags operation to perform
* @param p1 various functionality
* - bits 0..15: CompanyCtrlAction
* - bits 16..24: CompanyID
* @param p2 various depending on CompanyCtrlAction
* - bits 0..31: ClientID (with CCA_NEW)
* - bits 0..1: CompanyRemoveReason (with CCA_DELETE)
* - bits 16..23: CompanyID
* - bits 24..31: CompanyRemoveReason (with CCA_DELETE)
* @param p2 ClientID
* @param text unused
* @return the cost of this operation or an error
*/
@@ -822,7 +805,6 @@ CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
/* This command is only executed in a multiplayer game */
if (!_networking) return CMD_ERROR;
#ifdef ENABLE_NETWORK
/* Has the network client a correct ClientIndex? */
if (!(flags & DC_EXEC)) return CommandCost();
@@ -832,8 +814,8 @@ CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
/* When replaying the client ID is not a valid client; there
* are actually no clients at all. However, the company has to
* be created, otherwise we cannot rerun the game properly.
* So only allow a NULL client info in that case. */
if (ci == NULL) return CommandCost();
* So only allow a nullptr client info in that case. */
if (ci == nullptr) return CommandCost();
#endif /* NOT DEBUG_DUMP_COMMANDS */
/* Delete multiplayer progress bar */
@@ -842,7 +824,7 @@ CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
Company *c = DoStartupNewCompany(false);
/* A new company could not be created, revert to being a spectator */
if (c == NULL) {
if (c == nullptr) {
if (_network_server) {
ci->client_playas = COMPANY_SPECTATOR;
NetworkUpdateClientInfo(ci->client_id);
@@ -866,7 +848,6 @@ CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
}
NetworkServerNewCompany(c, ci);
#endif /* ENABLE_NETWORK */
break;
}
@@ -875,18 +856,16 @@ CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
if (company_id != INVALID_COMPANY && (company_id >= MAX_COMPANIES || Company::IsValidID(company_id))) return CMD_ERROR;
Company *c = DoStartupNewCompany(true, company_id);
#ifdef ENABLE_NETWORK
if (c != NULL) NetworkServerNewCompany(c, NULL);
#endif /* ENABLE_NETWORK */
if (c != nullptr) NetworkServerNewCompany(c, nullptr);
break;
}
case CCA_DELETE: { // Delete a company
CompanyRemoveReason reason = (CompanyRemoveReason)GB(p2, 0, 2);
CompanyRemoveReason reason = (CompanyRemoveReason)GB(p1, 24, 8);
if (reason >= CRR_END) return CMD_ERROR;
Company *c = Company::GetIfValid(company_id);
if (c == NULL) return CMD_ERROR;
if (c == nullptr) return CMD_ERROR;
if (!(flags & DC_EXEC)) return CommandCost();
@@ -976,8 +955,7 @@ CommandCost CmdSetCompanyColour(TileIndex tile, DoCommandFlag flags, uint32 p1,
/* Ensure no two companies have the same primary colour */
if (scheme == LS_DEFAULT && !second) {
const Company *cc;
FOR_ALL_COMPANIES(cc) {
for (const Company *cc : Company::Iterate()) {
if (cc != c && cc->colour == colour) return CMD_ERROR;
}
}
@@ -1040,8 +1018,7 @@ CommandCost CmdSetCompanyColour(TileIndex tile, DoCommandFlag flags, uint32 p1,
InvalidateWindowData(WC_SMALLMAP, 0, 1);
/* Company colour data is indirectly cached. */
Vehicle *v;
FOR_ALL_VEHICLES(v) {
for (Vehicle *v : Vehicle::Iterate()) {
if (v->owner == _current_company) v->InvalidateNewGRFCache();
}
@@ -1058,10 +1035,8 @@ CommandCost CmdSetCompanyColour(TileIndex tile, DoCommandFlag flags, uint32 p1,
*/
static bool IsUniqueCompanyName(const char *name)
{
const Company *c;
FOR_ALL_COMPANIES(c) {
if (c->name != NULL && strcmp(c->name, name) == 0) return false;
for (const Company *c : Company::Iterate()) {
if (c->name != nullptr && strcmp(c->name, name) == 0) return false;
}
return true;
@@ -1088,7 +1063,7 @@ CommandCost CmdRenameCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, uin
if (flags & DC_EXEC) {
Company *c = Company::Get(_current_company);
free(c->name);
c->name = reset ? NULL : stredup(text);
c->name = reset ? nullptr : stredup(text);
MarkWholeScreenDirty();
CompanyAdminUpdate(c);
}
@@ -1103,10 +1078,8 @@ CommandCost CmdRenameCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, uin
*/
static bool IsUniquePresidentName(const char *name)
{
const Company *c;
FOR_ALL_COMPANIES(c) {
if (c->president_name != NULL && strcmp(c->president_name, name) == 0) return false;
for (const Company *c : Company::Iterate()) {
if (c->president_name != nullptr && strcmp(c->president_name, name) == 0) return false;
}
return true;
@@ -1135,11 +1108,11 @@ CommandCost CmdRenamePresident(TileIndex tile, DoCommandFlag flags, uint32 p1, u
free(c->president_name);
if (reset) {
c->president_name = NULL;
c->president_name = nullptr;
} else {
c->president_name = stredup(text);
if (c->name_1 == STR_SV_UNNAMED && c->name == NULL) {
if (c->name_1 == STR_SV_UNNAMED && c->name == nullptr) {
char buf[80];
seprintf(buf, lastof(buf), "%s Transport", text);
@@ -1156,13 +1129,13 @@ CommandCost CmdRenamePresident(TileIndex tile, DoCommandFlag flags, uint32 p1, u
/**
* Get the service interval for the given company and vehicle type.
* @param c The company, or NULL for client-default settings.
* @param c The company, or nullptr for client-default settings.
* @param type The vehicle type to get the interval for.
* @return The service interval.
*/
int CompanyServiceInterval(const Company *c, VehicleType type)
{
const VehicleDefaultSettings *vds = (c == NULL) ? &_settings_client.company.vehicle : &c->settings.vehicle;
const VehicleDefaultSettings *vds = (c == nullptr) ? &_settings_client.company.vehicle : &c->settings.vehicle;
switch (type) {
default: NOT_REACHED();
case VEH_TRAIN: return vds->servint_trains;
@@ -1171,3 +1144,29 @@ int CompanyServiceInterval(const Company *c, VehicleType type)
case VEH_SHIP: return vds->servint_ships;
}
}
/**
* Get total sum of all owned road bits.
* @return Combined total road road bits.
*/
uint32 CompanyInfrastructure::GetRoadTotal() const
{
uint32 total = 0;
for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) {
if (RoadTypeIsRoad(rt)) total += this->road[rt];
}
return total;
}
/**
* Get total sum of all owned tram bits.
* @return Combined total of tram road bits.
*/
uint32 CompanyInfrastructure::GetTramTotal() const
{
uint32 total = 0;
for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) {
if (RoadTypeIsTram(rt)) total += this->road[rt];
}
return total;
}