Changeset - r18146:ad1a95532d6a
[Not reviewed]
master
0 9 0
frosch - 13 years ago 2011-10-03 17:23:41
frosch@openttd.org
(svn r22981) -Add: GroupStatistics for the ALL_GROUP.
9 files changed with 19 insertions and 23 deletions:
0 comments (0 inline, 0 general)
src/ai/api/ai_engine.cpp
Show inline comments
 
@@ -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)
src/autoreplace_gui.cpp
Show inline comments
 
@@ -74,10 +74,7 @@ static int CDECL EngineNumberSorter(cons
 
 */
 
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 */
src/company_base.h
Show inline comments
 
@@ -105,7 +105,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_all[VEH_COMPANY_END];      ///< NOSAVE: Statistics for the ALL_GROUP group.
 
	GroupStatistics group_default[VEH_COMPANY_END];  ///< NOSAVE: Statistics for the DEFAULT_GROUP group.
 

	
 
	/**
src/company_cmd.cpp
Show inline comments
 
@@ -68,8 +68,6 @@ Company::Company(uint16 name_1, bool is_
 
/** Destructor. */
 
Company::~Company()
 
{
 
	free(this->num_engines);
 

	
 
	if (CleaningPool()) return;
 

	
 
	DeleteCompanyWindows(this->index);
 
@@ -565,8 +563,6 @@ Company *DoStartupNewCompany(bool is_ai,
 

	
 
	if (is_ai && (!_networking || _network_server)) AI::StartNew(c->index);
 

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

	
 
	return c;
 
}
 

	
src/economy.cpp
Show inline comments
 
@@ -407,7 +407,6 @@ void ChangeOwnershipOfCompanyItems(Owner
 
					v->colourmap = PAL_NONE;
 

	
 
					if (v->IsEngineCountable()) {
 
						Company::Get(new_owner)->num_engines[v->engine_type]++;
 
						GroupStatistics::CountEngine(v, 1);
 
					}
 
					if (v->IsPrimaryVehicle()) {
src/group.h
Show inline comments
 
@@ -33,6 +33,7 @@ struct GroupStatistics {
 

	
 
	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);
src/group_cmd.cpp
Show inline comments
 
@@ -71,6 +71,7 @@ void GroupStatistics::Clear()
 
	}
 

	
 
	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 @@ void GroupStatistics::Clear()
 
}
 

	
 
/**
 
 * 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 @@ void GroupStatistics::Clear()
 
	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 @@ void GroupStatistics::Clear()
 
{
 
	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 @@ void GroupStatistics::Clear()
 
/* 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 @@ void UpdateTrainGroupID(Train *v)
 
 */
 
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];
 
}
src/vehicle.cpp
Show inline comments
 
@@ -693,7 +693,6 @@ void Vehicle::PreDestructor()
 
	}
 

	
 
	if (this->IsEngineCountable()) {
 
		Company::Get(this->owner)->num_engines[this->engine_type]--;
 
		GroupStatistics::CountEngine(this, -1);
 
		if (this->IsPrimaryVehicle()) GroupStatistics::CountVehicle(this, -1);
 

	
src/vehicle_cmd.cpp
Show inline comments
 
@@ -142,7 +142,6 @@ CommandCost CmdBuildVehicle(TileIndex ti
 
			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()) {
0 comments (0 inline, 0 general)