summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ai/api/ai_engine.cpp3
-rw-r--r--src/autoreplace_gui.cpp5
-rw-r--r--src/company_base.h2
-rw-r--r--src/company_cmd.cpp4
-rw-r--r--src/economy.cpp1
-rw-r--r--src/group.h1
-rw-r--r--src/group_cmd.cpp24
-rw-r--r--src/vehicle.cpp1
-rw-r--r--src/vehicle_cmd.cpp1
9 files changed, 19 insertions, 23 deletions
diff --git a/src/ai/api/ai_engine.cpp b/src/ai/api/ai_engine.cpp
index 564c77b6b..904b7fe33 100644
--- a/src/ai/api/ai_engine.cpp
+++ b/src/ai/api/ai_engine.cpp
@@ -24,8 +24,7 @@
/* static */ bool AIEngine::IsValidEngine(EngineID engine_id)
{
const Engine *e = ::Engine::GetIfValid(engine_id);
- return e != NULL && (::IsEngineBuildable(engine_id, e->type, _current_company) || ::Company::Get(_current_company)->num_engines[engine_id] > 0);
-
+ return e != NULL && (::IsEngineBuildable(engine_id, e->type, _current_company) || ::Company::Get(_current_company)->group_all[e->type].num_engines[engine_id] > 0);
}
/* static */ bool AIEngine::IsBuildable(EngineID engine_id)
diff --git a/src/autoreplace_gui.cpp b/src/autoreplace_gui.cpp
index e95606494..6f29b6deb 100644
--- a/src/autoreplace_gui.cpp
+++ b/src/autoreplace_gui.cpp
@@ -74,10 +74,7 @@ static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b)
*/
void InvalidateAutoreplaceWindow(EngineID e, GroupID id_g)
{
- Company *c = Company::Get(_local_company);
- uint num_engines = GetGroupNumEngines(_local_company, id_g, e);
-
- if (num_engines == 0 || c->num_engines[e] == 0) {
+ if (GetGroupNumEngines(_local_company, id_g, e) || GetGroupNumEngines(_local_company, ALL_GROUP, e) == 0) {
/* We don't have any of this engine type.
* Either we just sold the last one, we build a new one or we stopped replacing it.
* In all cases, we need to update the left list */
diff --git a/src/company_base.h b/src/company_base.h
index c8979a35d..bf655ea37 100644
--- a/src/company_base.h
+++ b/src/company_base.h
@@ -105,7 +105,7 @@ struct Company : CompanyPool::PoolItem<&_company_pool>, CompanyProperties {
EngineRenewList engine_renew_list; ///< Engine renewals of this company.
CompanySettings settings; ///< settings specific for each company
- uint16 *num_engines; ///< caches the number of engines of each type the company owns (no need to save this)
+ GroupStatistics group_all[VEH_COMPANY_END]; ///< NOSAVE: Statistics for the ALL_GROUP group.
GroupStatistics group_default[VEH_COMPANY_END]; ///< NOSAVE: Statistics for the DEFAULT_GROUP group.
/**
diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp
index f5bdd473d..f72d131e0 100644
--- a/src/company_cmd.cpp
+++ b/src/company_cmd.cpp
@@ -68,8 +68,6 @@ Company::Company(uint16 name_1, bool is_ai)
/** Destructor. */
Company::~Company()
{
- free(this->num_engines);
-
if (CleaningPool()) return;
DeleteCompanyWindows(this->index);
@@ -565,8 +563,6 @@ Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY)
if (is_ai && (!_networking || _network_server)) AI::StartNew(c->index);
- c->num_engines = CallocT<uint16>(Engine::GetPoolSize());
-
return c;
}
diff --git a/src/economy.cpp b/src/economy.cpp
index fa626cf94..edc84dc76 100644
--- a/src/economy.cpp
+++ b/src/economy.cpp
@@ -407,7 +407,6 @@ void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner)
v->colourmap = PAL_NONE;
if (v->IsEngineCountable()) {
- Company::Get(new_owner)->num_engines[v->engine_type]++;
GroupStatistics::CountEngine(v, 1);
}
if (v->IsPrimaryVehicle()) {
diff --git a/src/group.h b/src/group.h
index b543cbca4..557fdfbde 100644
--- a/src/group.h
+++ b/src/group.h
@@ -33,6 +33,7 @@ struct GroupStatistics {
static GroupStatistics &Get(CompanyID company, GroupID id_g, VehicleType type);
static GroupStatistics &Get(const Vehicle *v);
+ static GroupStatistics &GetAllGroup(const Vehicle *v);
static void CountVehicle(const Vehicle *v, int delta);
static void CountEngine(const Vehicle *v, int delta);
diff --git a/src/group_cmd.cpp b/src/group_cmd.cpp
index 777def0e5..a919ca53f 100644
--- a/src/group_cmd.cpp
+++ b/src/group_cmd.cpp
@@ -71,6 +71,7 @@ void GroupStatistics::Clear()
}
if (IsDefaultGroupID(id_g)) return Company::Get(company)->group_default[type];
+ if (IsAllGroupID(id_g)) return Company::Get(company)->group_all[type];
NOT_REACHED();
}
@@ -86,18 +87,25 @@ void GroupStatistics::Clear()
}
/**
+ * Returns the GroupStatistic for the ALL_GROUPO of a vehicle type.
+ * @param v Vehicle.
+ * @return GroupStatistics for the ALL_GROUP of the vehicle type.
+ */
+/* static */ GroupStatistics &GroupStatistics::GetAllGroup(const Vehicle *v)
+{
+ return GroupStatistics::Get(v->owner, ALL_GROUP, v->type);
+}
+
+/**
* Update all caches after loading a game, changing NewGRF etc..
*/
/* static */ void GroupStatistics::UpdateAfterLoad()
{
- size_t engines = Engine::GetPoolSize();
-
/* Set up the engine count for all companies */
Company *c;
FOR_ALL_COMPANIES(c) {
- free(c->num_engines);
- c->num_engines = CallocT<EngineID>(engines);
for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
+ c->group_all[type].Clear();
c->group_default[type].Clear();
}
}
@@ -112,10 +120,6 @@ void GroupStatistics::Clear()
FOR_ALL_VEHICLES(v) {
if (!v->IsEngineCountable()) continue;
- assert(v->engine_type < engines);
-
- Company::Get(v->owner)->num_engines[v->engine_type]++;
-
GroupStatistics::CountEngine(v, 1);
if (v->IsPrimaryVehicle()) GroupStatistics::CountVehicle(v, 1);
}
@@ -130,8 +134,10 @@ void GroupStatistics::Clear()
{
assert(delta == 1 || delta == -1);
+ GroupStatistics &stats_all = GroupStatistics::GetAllGroup(v);
GroupStatistics &stats = GroupStatistics::Get(v);
+ stats_all.num_vehicle += delta;
stats.num_vehicle += delta;
}
@@ -143,6 +149,7 @@ void GroupStatistics::Clear()
/* static */ void GroupStatistics::CountEngine(const Vehicle *v, int delta)
{
assert(delta == 1 || delta == -1);
+ GroupStatistics::GetAllGroup(v).num_engines[v->engine_type] += delta;
GroupStatistics::Get(v).num_engines[v->engine_type] += delta;
}
@@ -525,7 +532,6 @@ void UpdateTrainGroupID(Train *v)
*/
uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e)
{
- if (IsAllGroupID(id_g)) return Company::Get(company)->num_engines[id_e];
const Engine *e = Engine::Get(id_e);
return GroupStatistics::Get(company, id_g, e->type).num_engines[id_e];
}
diff --git a/src/vehicle.cpp b/src/vehicle.cpp
index 9c362e448..6c34d51f3 100644
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -693,7 +693,6 @@ void Vehicle::PreDestructor()
}
if (this->IsEngineCountable()) {
- Company::Get(this->owner)->num_engines[this->engine_type]--;
GroupStatistics::CountEngine(this, -1);
if (this->IsPrimaryVehicle()) GroupStatistics::CountVehicle(this, -1);
diff --git a/src/vehicle_cmd.cpp b/src/vehicle_cmd.cpp
index 8dd84328c..41e5e4309 100644
--- a/src/vehicle_cmd.cpp
+++ b/src/vehicle_cmd.cpp
@@ -142,7 +142,6 @@ CommandCost CmdBuildVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint
InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the auto replace window (must be called before incrementing num_engines)
}
- Company::Get(_current_company)->num_engines[eid]++;
GroupStatistics::CountEngine(v, 1);
if (v->IsPrimaryVehicle()) {