changeset 18156:c1d6dd2fc6f6 draft

(svn r22981) -Add: GroupStatistics for the ALL_GROUP.
author frosch <frosch@openttd.org>
date Mon, 03 Oct 2011 17:23:41 +0000
parents a7b36ae3de21
children f20ba7062226
files src/ai/api/ai_engine.cpp src/autoreplace_gui.cpp src/company_base.h src/company_cmd.cpp src/economy.cpp src/group.h src/group_cmd.cpp src/vehicle.cpp src/vehicle_cmd.cpp
diffstat 9 files changed, 19 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- 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)
--- a/src/autoreplace_gui.cpp
+++ b/src/autoreplace_gui.cpp
@@ -74,10 +74,7 @@
  */
 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 */
--- a/src/company_base.h
+++ b/src/company_base.h
@@ -105,7 +105,7 @@
 
 	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.
 
 	/**
--- a/src/company_cmd.cpp
+++ b/src/company_cmd.cpp
@@ -68,8 +68,6 @@
 /** Destructor. */
 Company::~Company()
 {
-	free(this->num_engines);
-
 	if (CleaningPool()) return;
 
 	DeleteCompanyWindows(this->index);
@@ -565,8 +563,6 @@
 
 	if (is_ai && (!_networking || _network_server)) AI::StartNew(c->index);
 
-	c->num_engines = CallocT<uint16>(Engine::GetPoolSize());
-
 	return c;
 }
 
--- a/src/economy.cpp
+++ b/src/economy.cpp
@@ -407,7 +407,6 @@
 					v->colourmap = PAL_NONE;
 
 					if (v->IsEngineCountable()) {
-						Company::Get(new_owner)->num_engines[v->engine_type]++;
 						GroupStatistics::CountEngine(v, 1);
 					}
 					if (v->IsPrimaryVehicle()) {
--- a/src/group.h
+++ b/src/group.h
@@ -33,6 +33,7 @@
 
 	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);
--- a/src/group_cmd.cpp
+++ b/src/group_cmd.cpp
@@ -71,6 +71,7 @@
 	}
 
 	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 @@
 }
 
 /**
+ * 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 @@
 	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 @@
 {
 	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 @@
 /* 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 @@
  */
 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];
 }
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -693,7 +693,6 @@
 	}
 
 	if (this->IsEngineCountable()) {
-		Company::Get(this->owner)->num_engines[this->engine_type]--;
 		GroupStatistics::CountEngine(this, -1);
 		if (this->IsPrimaryVehicle()) GroupStatistics::CountVehicle(this, -1);
 
--- a/src/vehicle_cmd.cpp
+++ b/src/vehicle_cmd.cpp
@@ -142,7 +142,6 @@
 			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()) {