Mercurial > hg > openttd
changeset 18151:98b63013cba3 draft
(svn r22976) -Codechange: Split group statistics into separate struct.
author | frosch <frosch@openttd.org> |
---|---|
date | Mon, 03 Oct 2011 17:20:26 +0000 |
parents | 3900e58346da |
children | 545be3b5f43e |
files | src/engine.cpp src/group.h src/group_cmd.cpp src/group_gui.cpp src/vehicle.cpp |
diffstat | 5 files changed, 48 insertions(+), 23 deletions(-) [+] |
line wrap: on
line diff
--- a/src/engine.cpp +++ b/src/engine.cpp @@ -477,9 +477,7 @@ /* Recalculate */ Group *g; FOR_ALL_GROUPS(g) { - g->num_vehicle = 0; - free(g->num_engines); - g->num_engines = CallocT<EngineID>(engines); + g->statistics.Clear(); } const Vehicle *v; @@ -496,8 +494,8 @@ assert(v->type == g->vehicle_type); assert(v->owner == g->owner); - g->num_engines[v->engine_type]++; - if (v->IsPrimaryVehicle()) g->num_vehicle++; + g->statistics.num_engines[v->engine_type]++; + if (v->IsPrimaryVehicle()) g->statistics.num_vehicle++; } }
--- a/src/group.h +++ b/src/group.h @@ -21,16 +21,25 @@ typedef Pool<Group, GroupID, 16, 64000> GroupPool; extern GroupPool _group_pool; ///< Pool of groups. +/** Statistics and caches on the vehicles in a group. */ +struct GroupStatistics { + uint16 num_vehicle; ///< Number of vehicles. + uint16 *num_engines; ///< Caches the number of engines of each type the company owns. + + GroupStatistics(); + ~GroupStatistics(); + + void Clear(); +}; + /** Group data. */ struct Group : GroupPool::PoolItem<&_group_pool> { char *name; ///< Group Name - - uint16 num_vehicle; ///< Number of vehicles in the group OwnerByte owner; ///< Group Owner VehicleTypeByte vehicle_type; ///< Vehicle type of the group bool replace_protection; ///< If set to true, the global autoreplace have no effect on the group - uint16 *num_engines; ///< Caches the number of engines of each type the company owns (no need to save this) + GroupStatistics statistics; ///< NOSAVE: Statistics and caches on the vehicles in the group. Group(CompanyID owner = INVALID_COMPANY); ~Group(); @@ -77,7 +86,7 @@ static inline void IncreaseGroupNumVehicle(GroupID id_g) { Group *g = Group::GetIfValid(id_g); - if (g != NULL) g->num_vehicle++; + if (g != NULL) g->statistics.num_vehicle++; } /** @@ -87,7 +96,7 @@ static inline void DecreaseGroupNumVehicle(GroupID id_g) { Group *g = Group::GetIfValid(id_g); - if (g != NULL) g->num_vehicle--; + if (g != NULL) g->statistics.num_vehicle--; }
--- a/src/group_cmd.cpp +++ b/src/group_cmd.cpp @@ -32,6 +32,29 @@ GroupPool _group_pool("Group"); INSTANTIATE_POOL_METHODS(Group) +GroupStatistics::GroupStatistics() +{ + this->num_engines = CallocT<uint16>(Engine::GetPoolSize()); +} + +GroupStatistics::~GroupStatistics() +{ + free(this->num_engines); +} + +/** + * Clear all caches. + */ +void GroupStatistics::Clear() +{ + this->num_vehicle = 0; + + /* This is also called when NewGRF change. So the number of engines might have changed. Reallocate. */ + free(this->num_engines); + this->num_engines = CallocT<uint16>(Engine::GetPoolSize()); +} + + /** * Update the num engines of a groupID. Decrease the old one and increase the new one * @note called in SetTrainGroupID and UpdateTrainGroupID @@ -43,10 +66,10 @@ { 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)->num_engines[i]--; + if (!IsDefaultGroupID(old_g) && Group::IsValidID(old_g)) Group::Get(old_g)->statistics.num_engines[i]--; /* 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)->num_engines[i]++; + if (!IsDefaultGroupID(new_g) && Group::IsValidID(new_g)) Group::Get(new_g)->statistics.num_engines[i]++; } } @@ -55,16 +78,11 @@ Group::Group(Owner owner) { this->owner = owner; - - if (!Company::IsValidID(owner)) return; - - this->num_engines = CallocT<uint16>(Engine::GetPoolSize()); } Group::~Group() { free(this->name); - free(this->num_engines); } @@ -414,14 +432,14 @@ */ uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e) { - if (Group::IsValidID(id_g)) return Group::Get(id_g)->num_engines[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->num_engines[id_e]; + if (g->owner == company) num -= g->statistics.num_engines[id_e]; } return num; }
--- a/src/group_gui.cpp +++ b/src/group_gui.cpp @@ -303,8 +303,8 @@ SetDParam(0, STR_GROUP_NAME); SetDParam(1, g->index); - SetDParam(2, g->num_vehicle); - SetDParam(3, g->num_vehicle); + SetDParam(2, g->statistics.num_vehicle); + SetDParam(3, g->statistics.num_vehicle); } break; } @@ -391,7 +391,7 @@ DrawString(r.left + WD_FRAMERECT_LEFT + 8, r.right - WD_FRAMERECT_RIGHT - 8, y1, STR_GROUP_NAME, (this->vli.index == g->index) ? TC_WHITE : TC_BLACK); /* draw the number of vehicles of the group */ - SetDParam(0, g->num_vehicle); + SetDParam(0, g->statistics.num_vehicle); DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y1 + 1, STR_TINY_COMMA, (this->vli.index == g->index) ? TC_WHITE : TC_BLACK, SA_RIGHT); y1 += this->tiny_step_height;
--- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -697,7 +697,7 @@ 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)->num_engines[this->engine_type]--; + if (Group::IsValidID(this->group_id)) Group::Get(this->group_id)->statistics.num_engines[this->engine_type]--; if (this->IsPrimaryVehicle()) DecreaseGroupNumVehicle(this->group_id); }