Changeset - r18145:767c15d9debd
[Not reviewed]
master
0 6 0
frosch - 13 years ago 2011-10-03 17:22:56
frosch@openttd.org
(svn r22980) -Add: GroupStatistics for DEFAULT_GROUP.
6 files changed with 46 insertions and 28 deletions:
0 comments (0 inline, 0 general)
src/company_base.h
Show inline comments
 
@@ -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 @@ struct Company : CompanyPool::PoolItem<&
 
	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)?
src/economy.cpp
Show inline comments
 
@@ -400,10 +400,20 @@ void ChangeOwnershipOfCompanyItems(Owner
 
				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;
src/group_cmd.cpp
Show inline comments
 
@@ -70,6 +70,8 @@ void GroupStatistics::Clear()
 
		return g->statistics;
 
	}
 

	
 
	if (IsDefaultGroupID(id_g)) return Company::Get(company)->group_default[type];
 

	
 
	NOT_REACHED();
 
}
 

	
 
@@ -95,6 +97,9 @@ void GroupStatistics::Clear()
 
	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 @@ void GroupStatistics::Clear()
 
/* 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 @@ void GroupStatistics::Clear()
 
/* 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 @@ static inline void UpdateNumEngineGroup(
 
{
 
	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 @@ void UpdateTrainGroupID(Train *v)
 
 */
 
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)
src/vehicle.cpp
Show inline comments
 
@@ -694,11 +694,11 @@ void Vehicle::PreDestructor()
 

	
 
	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()) {
src/vehicle_cmd.cpp
Show inline comments
 
@@ -139,12 +139,16 @@ CommandCost CmdBuildVehicle(TileIndex ti
 
		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;
src/vehicle_type.h
Show inline comments
 
@@ -19,14 +19,20 @@ typedef uint32 VehicleID;
 

	
 
/** 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. */
0 comments (0 inline, 0 general)