diff --git a/src/company_base.h b/src/company_base.h index d90e525eda..c8979a35d1 100644 --- a/src/company_base.h +++ b/src/company_base.h @@ -20,6 +20,7 @@ #include "economy_type.h" #include "tile_type.h" #include "settings_type.h" +#include "group.h" struct CompanyEconomyEntry { Money income; @@ -105,6 +106,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_default[VEH_COMPANY_END]; ///< NOSAVE: Statistics for the DEFAULT_GROUP group. /** * Is this company a valid company, controlled by the computer (a NoAI program)? diff --git a/src/economy.cpp b/src/economy.cpp index 9703597862..fa626cf949 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -400,10 +400,20 @@ void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner) if (new_owner == INVALID_OWNER) { if (v->Previous() == NULL) delete v; } else { + if (v->IsEngineCountable()) GroupStatistics::CountEngine(v, -1); + if (v->IsPrimaryVehicle()) GroupStatistics::CountVehicle(v, -1); + v->owner = new_owner; 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". */ if (v->cargo_payment != NULL) v->cargo_payment->owner = NULL; diff --git a/src/group_cmd.cpp b/src/group_cmd.cpp index be80df1359..777def0e5c 100644 --- a/src/group_cmd.cpp +++ b/src/group_cmd.cpp @@ -70,6 +70,8 @@ void GroupStatistics::Clear() return g->statistics; } + if (IsDefaultGroupID(id_g)) return Company::Get(company)->group_default[type]; + NOT_REACHED(); } @@ -95,6 +97,9 @@ void GroupStatistics::Clear() FOR_ALL_COMPANIES(c) { free(c->num_engines); c->num_engines = CallocT(engines); + for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) { + c->group_default[type].Clear(); + } } /* Recalculate */ @@ -124,7 +129,6 @@ void GroupStatistics::Clear() /* 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); @@ -139,7 +143,6 @@ void GroupStatistics::Clear() /* static */ void GroupStatistics::CountEngine(const Vehicle *v, int delta) { assert(delta == 1 || delta == -1); - if (!Group::IsValidID(v->group_id)) return; 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) { /* 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 */ - 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) { - 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->statistics.num_engines[id_e]; - } - return num; + 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]; } void RemoveAllGroupsForCompany(const CompanyID company) diff --git a/src/vehicle.cpp b/src/vehicle.cpp index ca9f36c1b3..9c362e448e 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -694,11 +694,11 @@ 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); GroupStatistics::CountEngine(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()) { diff --git a/src/vehicle_cmd.cpp b/src/vehicle_cmd.cpp index 46d8ba8e04..8dd84328c3 100644 --- a/src/vehicle_cmd.cpp +++ b/src/vehicle_cmd.cpp @@ -139,12 +139,16 @@ CommandCost CmdBuildVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint InvalidateWindowClassesData(GetWindowClassForVehicleType(type), 0); SetWindowDirty(WC_COMPANY, _current_company); 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]++; + GroupStatistics::CountEngine(v, 1); - if (v->IsPrimaryVehicle()) OrderBackup::Restore(v, p2); + if (v->IsPrimaryVehicle()) { + GroupStatistics::CountVehicle(v, 1); + OrderBackup::Restore(v, p2); + } } return value; diff --git a/src/vehicle_type.h b/src/vehicle_type.h index 99231194b5..35f45320f9 100644 --- a/src/vehicle_type.h +++ b/src/vehicle_type.h @@ -19,14 +19,20 @@ typedef uint32 VehicleID; /** Available vehicle types. */ enum VehicleType { - VEH_TRAIN, ///< %Train vehicle type. - VEH_ROAD, ///< Road vehicle type. - VEH_SHIP, ///< %Ship vehicle type. - VEH_AIRCRAFT, ///< %Aircraft vehicle type. - VEH_EFFECT, ///< Effect vehicle type (smoke, explosions, sparks, bubbles) - VEH_DISASTER, ///< Disaster vehicle type. + VEH_BEGIN, + + VEH_TRAIN = VEH_BEGIN, ///< %Train vehicle type. + VEH_ROAD, ///< Road vehicle type. + VEH_SHIP, ///< %Ship 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_INVALID = 0xFF, ///< Non-existing type of vehicle. + VEH_INVALID = 0xFF, ///< Non-existing type of vehicle. }; DECLARE_POSTFIX_INCREMENT(VehicleType) /** Helper information for extract tool. */