(svn r22980) -Add: GroupStatistics for DEFAULT_GROUP.

This commit is contained in:
frosch 2011-10-03 17:22:56 +00:00
parent fafa06e821
commit 5be8f73204
6 changed files with 46 additions and 28 deletions

View File

@ -20,6 +20,7 @@
#include "economy_type.h" #include "economy_type.h"
#include "tile_type.h" #include "tile_type.h"
#include "settings_type.h" #include "settings_type.h"
#include "group.h"
struct CompanyEconomyEntry { struct CompanyEconomyEntry {
Money income; Money income;
@ -105,6 +106,7 @@ struct Company : CompanyPool::PoolItem<&_company_pool>, CompanyProperties {
EngineRenewList engine_renew_list; ///< Engine renewals of this company. EngineRenewList engine_renew_list; ///< Engine renewals of this company.
CompanySettings settings; ///< settings specific for each 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) 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)? * Is this company a valid company, controlled by the computer (a NoAI program)?

View File

@ -400,10 +400,20 @@ void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner)
if (new_owner == INVALID_OWNER) { if (new_owner == INVALID_OWNER) {
if (v->Previous() == NULL) delete v; if (v->Previous() == NULL) delete v;
} else { } else {
if (v->IsEngineCountable()) GroupStatistics::CountEngine(v, -1);
if (v->IsPrimaryVehicle()) GroupStatistics::CountVehicle(v, -1);
v->owner = new_owner; v->owner = new_owner;
v->colourmap = PAL_NONE; 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". */ /* Invalidate the vehicle's cargo payment "owner cache". */
if (v->cargo_payment != NULL) v->cargo_payment->owner = NULL; if (v->cargo_payment != NULL) v->cargo_payment->owner = NULL;

View File

@ -70,6 +70,8 @@ void GroupStatistics::Clear()
return g->statistics; return g->statistics;
} }
if (IsDefaultGroupID(id_g)) return Company::Get(company)->group_default[type];
NOT_REACHED(); NOT_REACHED();
} }
@ -95,6 +97,9 @@ void GroupStatistics::Clear()
FOR_ALL_COMPANIES(c) { FOR_ALL_COMPANIES(c) {
free(c->num_engines); free(c->num_engines);
c->num_engines = CallocT<EngineID>(engines); c->num_engines = CallocT<EngineID>(engines);
for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
c->group_default[type].Clear();
}
} }
/* Recalculate */ /* Recalculate */
@ -124,7 +129,6 @@ void GroupStatistics::Clear()
/* static */ void GroupStatistics::CountVehicle(const Vehicle *v, int delta) /* static */ void GroupStatistics::CountVehicle(const Vehicle *v, int delta)
{ {
assert(delta == 1 || delta == -1); assert(delta == 1 || delta == -1);
if (!Group::IsValidID(v->group_id)) return;
GroupStatistics &stats = GroupStatistics::Get(v); GroupStatistics &stats = GroupStatistics::Get(v);
@ -139,7 +143,6 @@ void GroupStatistics::Clear()
/* static */ void GroupStatistics::CountEngine(const Vehicle *v, int delta) /* static */ void GroupStatistics::CountEngine(const Vehicle *v, int delta)
{ {
assert(delta == 1 || delta == -1); assert(delta == 1 || delta == -1);
if (!Group::IsValidID(v->group_id)) return;
GroupStatistics::Get(v).num_engines[v->engine_type] += delta; GroupStatistics::Get(v).num_engines[v->engine_type] += delta;
} }
@ -155,10 +158,10 @@ static inline void UpdateNumEngineGroup(const Vehicle *v, GroupID old_g, GroupID
{ {
if (old_g != new_g) { if (old_g != new_g) {
/* Decrease the num engines in the old group */ /* 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 */ /* 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) 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]; if (IsAllGroupID(id_g)) return Company::Get(company)->num_engines[id_e];
const Engine *e = Engine::Get(id_e);
uint num = Company::Get(company)->num_engines[id_e]; return GroupStatistics::Get(company, id_g, e->type).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;
} }
void RemoveAllGroupsForCompany(const CompanyID company) void RemoveAllGroupsForCompany(const CompanyID company)

View File

@ -694,11 +694,11 @@ void Vehicle::PreDestructor()
if (this->IsEngineCountable()) { if (this->IsEngineCountable()) {
Company::Get(this->owner)->num_engines[this->engine_type]--; 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); GroupStatistics::CountEngine(this, -1);
if (this->IsPrimaryVehicle()) GroupStatistics::CountVehicle(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()) { if (this->type == VEH_AIRCRAFT && this->IsPrimaryVehicle()) {

View File

@ -139,12 +139,16 @@ CommandCost CmdBuildVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint
InvalidateWindowClassesData(GetWindowClassForVehicleType(type), 0); InvalidateWindowClassesData(GetWindowClassForVehicleType(type), 0);
SetWindowDirty(WC_COMPANY, _current_company); SetWindowDirty(WC_COMPANY, _current_company);
if (IsLocalCompany()) { 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]++; 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; return value;

View File

@ -19,14 +19,20 @@ typedef uint32 VehicleID;
/** Available vehicle types. */ /** Available vehicle types. */
enum VehicleType { enum VehicleType {
VEH_TRAIN, ///< %Train vehicle type. VEH_BEGIN,
VEH_ROAD, ///< Road vehicle type.
VEH_SHIP, ///< %Ship vehicle type. VEH_TRAIN = VEH_BEGIN, ///< %Train vehicle type.
VEH_AIRCRAFT, ///< %Aircraft vehicle type. VEH_ROAD, ///< Road vehicle type.
VEH_EFFECT, ///< Effect vehicle type (smoke, explosions, sparks, bubbles) VEH_SHIP, ///< %Ship vehicle type.
VEH_DISASTER, ///< Disaster 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_END,
VEH_INVALID = 0xFF, ///< Non-existing type of vehicle. VEH_INVALID = 0xFF, ///< Non-existing type of vehicle.
}; };
DECLARE_POSTFIX_INCREMENT(VehicleType) DECLARE_POSTFIX_INCREMENT(VehicleType)
/** Helper information for extract tool. */ /** Helper information for extract tool. */