(svn r22981) -Add: GroupStatistics for the ALL_GROUP.

This commit is contained in:
frosch 2011-10-03 17:23:41 +00:00
parent 5be8f73204
commit dd74536bef
9 changed files with 19 additions and 23 deletions

View File

@ -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)

View File

@ -74,10 +74,7 @@ static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b)
*/
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 */

View File

@ -105,7 +105,7 @@ struct Company : CompanyPool::PoolItem<&_company_pool>, CompanyProperties {
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.
/**

View File

@ -68,8 +68,6 @@ Company::Company(uint16 name_1, bool is_ai)
/** Destructor. */
Company::~Company()
{
free(this->num_engines);
if (CleaningPool()) return;
DeleteCompanyWindows(this->index);
@ -565,8 +563,6 @@ Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY)
if (is_ai && (!_networking || _network_server)) AI::StartNew(c->index);
c->num_engines = CallocT<uint16>(Engine::GetPoolSize());
return c;
}

View File

@ -407,7 +407,6 @@ void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner)
v->colourmap = PAL_NONE;
if (v->IsEngineCountable()) {
Company::Get(new_owner)->num_engines[v->engine_type]++;
GroupStatistics::CountEngine(v, 1);
}
if (v->IsPrimaryVehicle()) {

View File

@ -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);

View File

@ -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();
}
@ -85,19 +86,26 @@ void GroupStatistics::Clear()
return GroupStatistics::Get(v->owner, v->group_id, v->type);
}
/**
* 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];
}

View File

@ -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);

View File

@ -142,7 +142,6 @@ CommandCost CmdBuildVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint
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()) {