diff options
author | frosch <frosch@openttd.org> | 2011-10-03 17:22:09 +0000 |
---|---|---|
committer | frosch <frosch@openttd.org> | 2011-10-03 17:22:09 +0000 |
commit | fafa06e821d27eaa6345d8a043a4a21b7c6e6381 (patch) | |
tree | 8fad99ceaf5d07334ddbea28cb1526b09eee2cab | |
parent | 68f969282e646b3fde7797a91beb3e38701564a6 (diff) | |
download | openttd-fafa06e821d27eaa6345d8a043a4a21b7c6e6381.tar.xz |
(svn r22979) -Codechange: Add GroupStatistics::CountEngine().
-rw-r--r-- | src/group.h | 1 | ||||
-rw-r--r-- | src/group_cmd.cpp | 38 | ||||
-rw-r--r-- | src/vehicle.cpp | 2 |
3 files changed, 24 insertions, 17 deletions
diff --git a/src/group.h b/src/group.h index bb87760c6..b543cbca4 100644 --- a/src/group.h +++ b/src/group.h @@ -35,6 +35,7 @@ struct GroupStatistics { static GroupStatistics &Get(const Vehicle *v); static void CountVehicle(const Vehicle *v, int delta); + static void CountEngine(const Vehicle *v, int delta); static void UpdateAfterLoad(); }; diff --git a/src/group_cmd.cpp b/src/group_cmd.cpp index df625e69e..be80df135 100644 --- a/src/group_cmd.cpp +++ b/src/group_cmd.cpp @@ -111,13 +111,7 @@ void GroupStatistics::Clear() Company::Get(v->owner)->num_engines[v->engine_type]++; - if (v->group_id == DEFAULT_GROUP) continue; - - g = Group::Get(v->group_id); - assert(v->type == g->vehicle_type); - assert(v->owner == g->owner); - - g->statistics.num_engines[v->engine_type]++; + GroupStatistics::CountEngine(v, 1); if (v->IsPrimaryVehicle()) GroupStatistics::CountVehicle(v, 1); } } @@ -137,22 +131,34 @@ void GroupStatistics::Clear() stats.num_vehicle += delta; } +/** + * Update num_engines when adding/removing an engine. + * @param v Engine to count. + * @param delta +1 to add, -1 to remove. + */ +/* 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; +} + /** * 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 v Vehicle we have to update * @param old_g index of the old group * @param new_g index of the new group */ -static inline void UpdateNumEngineGroup(EngineID i, GroupID old_g, GroupID new_g) +static inline void UpdateNumEngineGroup(const Vehicle *v, GroupID old_g, GroupID new_g) { if (old_g != new_g) { - /* Decrease the num engines of EngineID i of the old group if it's not the default one */ - if (!IsDefaultGroupID(old_g) && Group::IsValidID(old_g)) Group::Get(old_g)->statistics.num_engines[i]--; + /* 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]--; - /* Increase the num engines of EngineID i of the new group if it's not the default one */ - if (!IsDefaultGroupID(new_g) && Group::IsValidID(new_g)) Group::Get(new_g)->statistics.num_engines[i]++; + /* 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]++; } } @@ -330,7 +336,7 @@ CommandCost CmdAddVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, u case VEH_ROAD: case VEH_SHIP: case VEH_AIRCRAFT: - if (v->IsEngineCountable()) UpdateNumEngineGroup(v->engine_type, v->group_id, new_g); + if (v->IsEngineCountable()) UpdateNumEngineGroup(v, v->group_id, new_g); v->group_id = new_g; break; } @@ -474,7 +480,7 @@ void SetTrainGroupID(Train *v, GroupID new_g) assert(v->IsFrontEngine() || IsDefaultGroupID(new_g)); for (Vehicle *u = v; u != NULL; u = u->Next()) { - if (u->IsEngineCountable()) UpdateNumEngineGroup(u->engine_type, u->group_id, new_g); + if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g); u->group_id = new_g; } @@ -497,7 +503,7 @@ void UpdateTrainGroupID(Train *v) GroupID new_g = v->IsFrontEngine() ? v->group_id : (GroupID)DEFAULT_GROUP; for (Vehicle *u = v; u != NULL; u = u->Next()) { - if (u->IsEngineCountable()) UpdateNumEngineGroup(u->engine_type, u->group_id, new_g); + if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g); u->group_id = new_g; } diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 731c31395..ca9f36c1b 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -697,7 +697,7 @@ void Vehicle::PreDestructor() 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]--; + GroupStatistics::CountEngine(this, -1); if (this->IsPrimaryVehicle()) GroupStatistics::CountVehicle(this, -1); } |