Changeset - r18143:604ab298ba0c
[Not reviewed]
master
0 4 0
frosch - 13 years ago 2011-10-03 17:21:41
frosch@openttd.org
(svn r22978) -Codechange: Replace IncreaseGroupNumVehicle() and DecreaseGroupNumVehicle() with GroupStatistics::CountVehicle().
4 files changed with 59 insertions and 30 deletions:
0 comments (0 inline, 0 general)
src/group.h
Show inline comments
 
@@ -28,12 +28,17 @@ struct GroupStatistics {
 

	
 
	GroupStatistics();
 
	~GroupStatistics();
 

	
 
	void Clear();
 

	
 
	static GroupStatistics &Get(CompanyID company, GroupID id_g, VehicleType type);
 
	static GroupStatistics &Get(const Vehicle *v);
 

	
 
	static void CountVehicle(const Vehicle *v, int delta);
 

	
 
	static void UpdateAfterLoad();
 
};
 

	
 
/** Group data. */
 
struct Group : GroupPool::PoolItem<&_group_pool> {
 
	char *name;                             ///< Group Name
 
@@ -78,33 +83,12 @@ static inline uint GetGroupArraySize()
 

	
 
	return num;
 
}
 

	
 
uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e);
 

	
 
/**
 
 * 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->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->statistics.num_vehicle--;
 
}
 

	
 

	
 
void SetTrainGroupID(Train *v, GroupID grp);
 
void UpdateTrainGroupID(Train *v);
 
void RemoveVehicleFromGroup(const Vehicle *v);
 
void RemoveAllGroupsForCompany(const CompanyID company);
 

	
 
extern GroupID _new_group_id;
src/group_cmd.cpp
Show inline comments
 
@@ -52,12 +52,41 @@ void GroupStatistics::Clear()
 
	/* 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());
 
}
 

	
 
/**
 
 * Returns the GroupStatistics for a specific group.
 
 * @param company Owner of the group.
 
 * @param id_g    GroupID of the group.
 
 * @param type    VehicleType of the vehicles in the group.
 
 * @return Statistics for the group.
 
 */
 
/* static */ GroupStatistics &GroupStatistics::Get(CompanyID company, GroupID id_g, VehicleType type)
 
{
 
	if (Group::IsValidID(id_g)) {
 
		Group *g = Group::Get(id_g);
 
		assert(g->owner == company);
 
		assert(g->vehicle_type == type);
 
		return g->statistics;
 
	}
 

	
 
	NOT_REACHED();
 
}
 

	
 
/**
 
 * Returns the GroupStatistic for the group of a vehicle.
 
 * @param v Vehicle.
 
 * @return GroupStatistics for the group of the vehicle.
 
 */
 
/* static */ GroupStatistics &GroupStatistics::Get(const Vehicle *v)
 
{
 
	return GroupStatistics::Get(v->owner, v->group_id, v->type);
 
}
 

	
 
/**
 
 * Update all caches after loading a game, changing NewGRF etc..
 
 */
 
/* static */ void GroupStatistics::UpdateAfterLoad()
 
{
 
	size_t engines = Engine::GetPoolSize();
 

	
 
@@ -86,16 +115,31 @@ void GroupStatistics::Clear()
 

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

	
 
		g->statistics.num_engines[v->engine_type]++;
 
		if (v->IsPrimaryVehicle()) g->statistics.num_vehicle++;
 
		if (v->IsPrimaryVehicle()) GroupStatistics::CountVehicle(v, 1);
 
	}
 
}
 

	
 
/**
 
 * Update num_vehicle when adding or removing a vehicle.
 
 * @param v Vehicle to count.
 
 * @param delta +1 to add, -1 to remove.
 
 */
 
/* 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);
 

	
 
	stats.num_vehicle += delta;
 
}
 

	
 

	
 
/**
 
 * 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
 
@@ -273,14 +317,13 @@ CommandCost CmdAddVehicleGroup(TileIndex
 
		if (g->owner != _current_company || g->vehicle_type != v->type) return CMD_ERROR;
 
	}
 

	
 
	if (v->owner != _current_company || !v->IsPrimaryVehicle()) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		DecreaseGroupNumVehicle(v->group_id);
 
		IncreaseGroupNumVehicle(new_g);
 
		GroupStatistics::CountVehicle(v, -1);
 

	
 
		switch (v->type) {
 
			default: NOT_REACHED();
 
			case VEH_TRAIN:
 
				SetTrainGroupID(Train::From(v), new_g);
 
				break;
 
@@ -289,12 +332,14 @@ CommandCost CmdAddVehicleGroup(TileIndex
 
			case VEH_AIRCRAFT:
 
				if (v->IsEngineCountable()) UpdateNumEngineGroup(v->engine_type, v->group_id, new_g);
 
				v->group_id = new_g;
 
				break;
 
		}
 

	
 
		GroupStatistics::CountVehicle(v, 1);
 

	
 
		/* Update the Replace Vehicle Windows */
 
		SetWindowDirty(WC_REPLACE_VEHICLE, v->type);
 
		InvalidateWindowData(GetWindowClassForVehicleType(v->type), VehicleListIdentifier(VL_GROUP_LIST, v->type, _current_company).Pack());
 
	}
 

	
 
	return CommandCost();
 
@@ -409,13 +454,13 @@ CommandCost CmdSetGroupReplaceProtection
 
 * @param v     FrontEngine of the train we want to remove.
 
 */
 
void RemoveVehicleFromGroup(const Vehicle *v)
 
{
 
	if (!v->IsPrimaryVehicle()) return;
 

	
 
	if (!IsDefaultGroupID(v->group_id)) DecreaseGroupNumVehicle(v->group_id);
 
	if (!IsDefaultGroupID(v->group_id)) GroupStatistics::CountVehicle(v, -1);
 
}
 

	
 

	
 
/**
 
 * Affect the groupID of a train to new_g.
 
 * @note called in CmdAddVehicleGroup and CmdMoveRailVehicle
src/train_cmd.cpp
Show inline comments
 
@@ -1242,32 +1242,32 @@ CommandCost CmdMoveRailVehicle(TileIndex
 
			DeleteWindowById(WC_VEHICLE_DETAILS, src->index);
 
			DeleteWindowById(WC_VEHICLE_TIMETABLE, src->index);
 

	
 
			/* We are going to be moved to a different train, and
 
			 * we were the front engine of the original train. */
 
			if (dst_head != NULL && dst_head != src && (src_head == NULL || !src_head->IsFrontEngine())) {
 
				DecreaseGroupNumVehicle(src->group_id);
 
				GroupStatistics::CountVehicle(src, -1);
 
			}
 

	
 
			/* The front engine is going to be moved later in the
 
			 * current train, and it will not be a train anymore. */
 
			if (dst_head == NULL && !src_head->IsFrontEngine()) {
 
				DecreaseGroupNumVehicle(src->group_id);
 
				GroupStatistics::CountVehicle(src, -1);
 
			}
 

	
 
			/* Delete orders, group stuff and the unit number as we're not the
 
			 * front of any vehicle anymore. */
 
			DeleteVehicleOrders(src);
 
			RemoveVehicleFromGroup(src);
 
			src->unitnumber = 0;
 
		}
 

	
 
		/* We were a front engine and we are becoming one for a different train.
 
		 * Increase the group counter accordingly. */
 
		if (original_src_head == src && dst_head == src) {
 
			IncreaseGroupNumVehicle(src->group_id);
 
			GroupStatistics::CountVehicle(src, 1);
 
		}
 

	
 
		/* We weren't a front engine but are becoming one. So
 
		 * we should be put in the default group. */
 
		if (original_src_head != src && dst_head == src) {
 
			SetTrainGroupID(src, DEFAULT_GROUP);
 
@@ -1351,13 +1351,13 @@ CommandCost CmdSellRailWagon(DoCommandFl
 
			new_head->orders.list = first->orders.list;
 
			new_head->AddToShared(first);
 
			DeleteVehicleOrders(first);
 

	
 
			/* Copy other important data from the front engine */
 
			new_head->CopyVehicleConfigAndStatistics(first);
 
			IncreaseGroupNumVehicle(new_head->group_id);
 
			GroupStatistics::CountVehicle(new_head, 1);
 

	
 
			/* If we deleted a window then open a new one for the 'new' train */
 
			if (IsLocalCompany() && w != NULL) ShowVehicleViewWindow(new_head);
 
		} else if (v->IsPrimaryVehicle() && data & (MAKE_ORDER_BACKUP_FLAG >> 20)) {
 
			OrderBackup::Backup(v, user);
 
		}
src/vehicle.cpp
Show inline comments
 
@@ -695,13 +695,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)->statistics.num_engines[this->engine_type]--;
 
		if (this->IsPrimaryVehicle()) DecreaseGroupNumVehicle(this->group_id);
 
		if (this->IsPrimaryVehicle()) GroupStatistics::CountVehicle(this, -1);
 
	}
 

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