Changeset - r18141:0f3c67cfc488
[Not reviewed]
master
0 5 0
frosch - 13 years ago 2011-10-03 17:20:26
frosch@openttd.org
(svn r22976) -Codechange: Split group statistics into separate struct.
5 files changed with 48 insertions and 23 deletions:
0 comments (0 inline, 0 general)
src/engine.cpp
Show inline comments
 
@@ -474,15 +474,13 @@ void SetCachedEngineCounts()
 
		c->num_engines = CallocT<EngineID>(engines);
 
	}
 

	
 
	/* Recalculate */
 
	Group *g;
 
	FOR_ALL_GROUPS(g) {
 
		g->num_vehicle = 0;
 
		free(g->num_engines);
 
		g->num_engines = CallocT<EngineID>(engines);
 
		g->statistics.Clear();
 
	}
 

	
 
	const Vehicle *v;
 
	FOR_ALL_VEHICLES(v) {
 
		if (!v->IsEngineCountable()) continue;
 

	
 
@@ -493,14 +491,14 @@ void SetCachedEngineCounts()
 
		if (v->group_id == DEFAULT_GROUP) continue;
 

	
 
		g = Group::Get(v->group_id);
 
		assert(v->type == g->vehicle_type);
 
		assert(v->owner == g->owner);
 

	
 
		g->num_engines[v->engine_type]++;
 
		if (v->IsPrimaryVehicle()) g->num_vehicle++;
 
		g->statistics.num_engines[v->engine_type]++;
 
		if (v->IsPrimaryVehicle()) g->statistics.num_vehicle++;
 
	}
 
}
 

	
 
/**
 
 * Initialise the engine pool with the data from the original vehicles.
 
 */
src/group.h
Show inline comments
 
@@ -18,22 +18,31 @@
 
#include "vehicle_type.h"
 
#include "engine_type.h"
 

	
 
typedef Pool<Group, GroupID, 16, 64000> GroupPool;
 
extern GroupPool _group_pool; ///< Pool of groups.
 

	
 
/** Statistics and caches on the vehicles in a group. */
 
struct GroupStatistics {
 
	uint16 num_vehicle;                     ///< Number of vehicles.
 
	uint16 *num_engines;                    ///< Caches the number of engines of each type the company owns.
 

	
 
	GroupStatistics();
 
	~GroupStatistics();
 

	
 
	void Clear();
 
};
 

	
 
/** Group data. */
 
struct Group : GroupPool::PoolItem<&_group_pool> {
 
	char *name;                             ///< Group Name
 

	
 
	uint16 num_vehicle;                     ///< Number of vehicles in the group
 
	OwnerByte owner;                        ///< Group Owner
 
	VehicleTypeByte vehicle_type;           ///< Vehicle type of the group
 

	
 
	bool replace_protection;                ///< If set to true, the global autoreplace have no effect on the group
 
	uint16 *num_engines;                    ///< Caches the number of engines of each type the company owns (no need to save this)
 
	GroupStatistics statistics;             ///< NOSAVE: Statistics and caches on the vehicles in the group.
 

	
 
	Group(CompanyID owner = INVALID_COMPANY);
 
	~Group();
 
};
 

	
 

	
 
@@ -74,23 +83,23 @@ uint GetGroupNumEngines(CompanyID compan
 
 * Increase the number of vehicles by one in a group.
 
 * @param id_g Group id.
 
 */
 
static inline void IncreaseGroupNumVehicle(GroupID id_g)
 
{
 
	Group *g = Group::GetIfValid(id_g);
 
	if (g != NULL) g->num_vehicle++;
 
	if (g != NULL) g->statistics.num_vehicle++;
 
}
 

	
 
/**
 
 * Decrease the number of vehicles by one in a group.
 
 * @param id_g Group id.
 
 */
 
static inline void DecreaseGroupNumVehicle(GroupID id_g)
 
{
 
	Group *g = Group::GetIfValid(id_g);
 
	if (g != NULL) g->num_vehicle--;
 
	if (g != NULL) g->statistics.num_vehicle--;
 
}
 

	
 

	
 
void SetTrainGroupID(Train *v, GroupID grp);
 
void UpdateTrainGroupID(Train *v);
 
void RemoveVehicleFromGroup(const Vehicle *v);
src/group_cmd.cpp
Show inline comments
 
@@ -29,45 +29,63 @@
 

	
 
GroupID _new_group_id;
 

	
 
GroupPool _group_pool("Group");
 
INSTANTIATE_POOL_METHODS(Group)
 

	
 
GroupStatistics::GroupStatistics()
 
{
 
	this->num_engines = CallocT<uint16>(Engine::GetPoolSize());
 
}
 

	
 
GroupStatistics::~GroupStatistics()
 
{
 
	free(this->num_engines);
 
}
 

	
 
/**
 
 * Clear all caches.
 
 */
 
void GroupStatistics::Clear()
 
{
 
	this->num_vehicle = 0;
 

	
 
	/* This is also called when NewGRF change. So the number of engines might have changed. Reallocate. */
 
	free(this->num_engines);
 
	this->num_engines = CallocT<uint16>(Engine::GetPoolSize());
 
}
 

	
 

	
 
/**
 
 * Update the num engines of a groupID. Decrease the old one and increase the new one
 
 * @note called in SetTrainGroupID and UpdateTrainGroupID
 
 * @param i     EngineID we have to update
 
 * @param old_g index of the old group
 
 * @param new_g index of the new group
 
 */
 
static inline void UpdateNumEngineGroup(EngineID i, GroupID old_g, GroupID new_g)
 
{
 
	if (old_g != new_g) {
 
		/* Decrease the num engines of EngineID i of the old group if it's not the default one */
 
		if (!IsDefaultGroupID(old_g) && Group::IsValidID(old_g)) Group::Get(old_g)->num_engines[i]--;
 
		if (!IsDefaultGroupID(old_g) && Group::IsValidID(old_g)) Group::Get(old_g)->statistics.num_engines[i]--;
 

	
 
		/* Increase the num engines of EngineID i of the new group if it's not the default one */
 
		if (!IsDefaultGroupID(new_g) && Group::IsValidID(new_g)) Group::Get(new_g)->num_engines[i]++;
 
		if (!IsDefaultGroupID(new_g) && Group::IsValidID(new_g)) Group::Get(new_g)->statistics.num_engines[i]++;
 
	}
 
}
 

	
 

	
 

	
 
Group::Group(Owner owner)
 
{
 
	this->owner = owner;
 

	
 
	if (!Company::IsValidID(owner)) return;
 

	
 
	this->num_engines = CallocT<uint16>(Engine::GetPoolSize());
 
}
 

	
 
Group::~Group()
 
{
 
	free(this->name);
 
	free(this->num_engines);
 
}
 

	
 

	
 
/**
 
 * Create a new vehicle group.
 
 * @param tile unused
 
@@ -411,20 +429,20 @@ void UpdateTrainGroupID(Train *v)
 
 * @param id_g The GroupID of the group used
 
 * @param id_e The EngineID of the engine to count
 
 * @return The number of engines with EngineID id_e in the group
 
 */
 
uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e)
 
{
 
	if (Group::IsValidID(id_g)) return Group::Get(id_g)->num_engines[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->num_engines[id_e];
 
		if (g->owner == company) num -= g->statistics.num_engines[id_e];
 
	}
 
	return num;
 
}
 

	
 
void RemoveAllGroupsForCompany(const CompanyID company)
 
{
src/group_gui.cpp
Show inline comments
 
@@ -300,14 +300,14 @@ public:
 
					SetDParam(3, this->vehicles.Length());
 
				} else {
 
					const Group *g = Group::Get(this->vli.index);
 

	
 
					SetDParam(0, STR_GROUP_NAME);
 
					SetDParam(1, g->index);
 
					SetDParam(2, g->num_vehicle);
 
					SetDParam(3, g->num_vehicle);
 
					SetDParam(2, g->statistics.num_vehicle);
 
					SetDParam(3, g->statistics.num_vehicle);
 
				}
 
				break;
 
		}
 
	}
 

	
 
	virtual void OnPaint()
 
@@ -388,13 +388,13 @@ public:
 

	
 
					/* draw the selected group in white, else we draw it in black */
 
					SetDParam(0, g->index);
 
					DrawString(r.left + WD_FRAMERECT_LEFT + 8, r.right - WD_FRAMERECT_RIGHT - 8, y1, STR_GROUP_NAME, (this->vli.index == g->index) ? TC_WHITE : TC_BLACK);
 

	
 
					/* draw the number of vehicles of the group */
 
					SetDParam(0, g->num_vehicle);
 
					SetDParam(0, g->statistics.num_vehicle);
 
					DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y1 + 1, STR_TINY_COMMA, (this->vli.index == g->index) ? TC_WHITE : TC_BLACK, SA_RIGHT);
 

	
 
					y1 += this->tiny_step_height;
 
				}
 
				break;
 
			}
src/vehicle.cpp
Show inline comments
 
@@ -694,13 +694,13 @@ 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);
 
		if (Group::IsValidID(this->group_id)) Group::Get(this->group_id)->num_engines[this->engine_type]--;
 
		if (Group::IsValidID(this->group_id)) Group::Get(this->group_id)->statistics.num_engines[this->engine_type]--;
 
		if (this->IsPrimaryVehicle()) DecreaseGroupNumVehicle(this->group_id);
 
	}
 

	
 
	if (this->type == VEH_AIRCRAFT && this->IsPrimaryVehicle()) {
 
		Aircraft *a = Aircraft::From(this);
 
		Station *st = GetTargetAirportIfValid(a);
0 comments (0 inline, 0 general)