changeset 18155:a7b36ae3de21 draft

(svn r22980) -Add: GroupStatistics for DEFAULT_GROUP.
author frosch <frosch@openttd.org>
date Mon, 03 Oct 2011 17:22:56 +0000
parents ebcb3f30bd4a
children c1d6dd2fc6f6
files src/company_base.h src/economy.cpp src/group_cmd.cpp src/vehicle.cpp src/vehicle_cmd.cpp src/vehicle_type.h
diffstat 6 files changed, 46 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/src/company_base.h
+++ b/src/company_base.h
@@ -20,6 +20,7 @@
 #include "economy_type.h"
 #include "tile_type.h"
 #include "settings_type.h"
+#include "group.h"
 
 struct CompanyEconomyEntry {
 	Money income;
@@ -105,6 +106,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_default[VEH_COMPANY_END];  ///< NOSAVE: Statistics for the DEFAULT_GROUP group.
 
 	/**
 	 * Is this company a valid company, controlled by the computer (a NoAI program)?
--- a/src/economy.cpp
+++ b/src/economy.cpp
@@ -400,10 +400,20 @@
 				if (new_owner == INVALID_OWNER) {
 					if (v->Previous() == NULL) delete v;
 				} else {
+					if (v->IsEngineCountable()) GroupStatistics::CountEngine(v, -1);
+					if (v->IsPrimaryVehicle()) GroupStatistics::CountVehicle(v, -1);
+
 					v->owner = new_owner;
 					v->colourmap = PAL_NONE;
-					if (v->IsEngineCountable()) Company::Get(new_owner)->num_engines[v->engine_type]++;
-					if (v->IsPrimaryVehicle()) v->unitnumber = unitidgen[v->type].NextID();
+
+					if (v->IsEngineCountable()) {
+						Company::Get(new_owner)->num_engines[v->engine_type]++;
+						GroupStatistics::CountEngine(v, 1);
+					}
+					if (v->IsPrimaryVehicle()) {
+						GroupStatistics::CountVehicle(v, 1);
+						v->unitnumber = unitidgen[v->type].NextID();
+					}
 
 					/* Invalidate the vehicle's cargo payment "owner cache". */
 					if (v->cargo_payment != NULL) v->cargo_payment->owner = NULL;
--- a/src/group_cmd.cpp
+++ b/src/group_cmd.cpp
@@ -70,6 +70,8 @@
 		return g->statistics;
 	}
 
+	if (IsDefaultGroupID(id_g)) return Company::Get(company)->group_default[type];
+
 	NOT_REACHED();
 }
 
@@ -95,6 +97,9 @@
 	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_default[type].Clear();
+		}
 	}
 
 	/* Recalculate */
@@ -124,7 +129,6 @@
 /* static */ void GroupStatistics::CountVehicle(const Vehicle *v, int delta)
 {
 	assert(delta == 1 || delta == -1);
-	if (!Group::IsValidID(v->group_id)) return;
 
 	GroupStatistics &stats = GroupStatistics::Get(v);
 
@@ -139,7 +143,6 @@
 /* static */ void GroupStatistics::CountEngine(const Vehicle *v, int delta)
 {
 	assert(delta == 1 || delta == -1);
-	if (!Group::IsValidID(v->group_id)) return;
 	GroupStatistics::Get(v).num_engines[v->engine_type] += delta;
 }
 
@@ -155,10 +158,10 @@
 {
 	if (old_g != new_g) {
 		/* Decrease the num engines in the old group */
-		if (Group::IsValidID(old_g)) GroupStatistics::Get(v->owner, old_g, v->type).num_engines[v->engine_type]--;
+		GroupStatistics::Get(v->owner, old_g, v->type).num_engines[v->engine_type]--;
 
 		/* Increase the num engines in the new group */
-		if (Group::IsValidID(new_g)) GroupStatistics::Get(v->owner, new_g, v->type).num_engines[v->engine_type]++;
+		GroupStatistics::Get(v->owner, new_g, v->type).num_engines[v->engine_type]++;
 	}
 }
 
@@ -522,16 +525,9 @@
  */
 uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e)
 {
-	if (Group::IsValidID(id_g)) return Group::Get(id_g)->statistics.num_engines[id_e];
-
-	uint num = Company::Get(company)->num_engines[id_e];
-	if (!IsDefaultGroupID(id_g)) return num;
-
-	const Group *g;
-	FOR_ALL_GROUPS(g) {
-		if (g->owner == company) num -= g->statistics.num_engines[id_e];
-	}
-	return num;
+	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];
 }
 
 void RemoveAllGroupsForCompany(const CompanyID company)
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -694,11 +694,11 @@
 
 	if (this->IsEngineCountable()) {
 		Company::Get(this->owner)->num_engines[this->engine_type]--;
-		if (this->owner == _local_company) InvalidateAutoreplaceWindow(this->engine_type, this->group_id);
-
-		DeleteGroupHighlightOfVehicle(this);
 		GroupStatistics::CountEngine(this, -1);
 		if (this->IsPrimaryVehicle()) GroupStatistics::CountVehicle(this, -1);
+
+		if (this->owner == _local_company) InvalidateAutoreplaceWindow(this->engine_type, this->group_id);
+		DeleteGroupHighlightOfVehicle(this);
 	}
 
 	if (this->type == VEH_AIRCRAFT && this->IsPrimaryVehicle()) {
--- a/src/vehicle_cmd.cpp
+++ b/src/vehicle_cmd.cpp
@@ -139,12 +139,16 @@
 		InvalidateWindowClassesData(GetWindowClassForVehicleType(type), 0);
 		SetWindowDirty(WC_COMPANY, _current_company);
 		if (IsLocalCompany()) {
-			InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the auto replace window
+			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()) OrderBackup::Restore(v, p2);
+		if (v->IsPrimaryVehicle()) {
+			GroupStatistics::CountVehicle(v, 1);
+			OrderBackup::Restore(v, p2);
+		}
 	}
 
 	return value;
--- a/src/vehicle_type.h
+++ b/src/vehicle_type.h
@@ -19,14 +19,20 @@
 
 /** Available vehicle types. */
 enum VehicleType {
-	VEH_TRAIN,          ///< %Train vehicle type.
-	VEH_ROAD,           ///< Road vehicle type.
-	VEH_SHIP,           ///< %Ship vehicle type.
-	VEH_AIRCRAFT,       ///< %Aircraft vehicle type.
-	VEH_EFFECT,         ///< Effect vehicle type (smoke, explosions, sparks, bubbles)
-	VEH_DISASTER,       ///< Disaster vehicle type.
+	VEH_BEGIN,
+
+	VEH_TRAIN = VEH_BEGIN,        ///< %Train vehicle type.
+	VEH_ROAD,                     ///< Road vehicle type.
+	VEH_SHIP,                     ///< %Ship vehicle type.
+	VEH_AIRCRAFT,                 ///< %Aircraft vehicle type.
+
+	VEH_COMPANY_END,              ///< Last company-ownable type.
+
+	VEH_EFFECT = VEH_COMPANY_END, ///< Effect vehicle type (smoke, explosions, sparks, bubbles)
+	VEH_DISASTER,                 ///< Disaster vehicle type.
+
 	VEH_END,
-	VEH_INVALID = 0xFF, ///< Non-existing type of vehicle.
+	VEH_INVALID = 0xFF,           ///< Non-existing type of vehicle.
 };
 DECLARE_POSTFIX_INCREMENT(VehicleType)
 /** Helper information for extract tool. */