summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorfrosch <frosch@openttd.org>2011-10-03 17:20:26 +0000
committerfrosch <frosch@openttd.org>2011-10-03 17:20:26 +0000
commitb6766c2ed5fa7e3cb703e1dcfdeacb26cde7d07e (patch)
treee488522e1e02e74e1b01223fde083e4223eb6c5c /src
parent7133887ed66a55527037b2c314cf3dfb048e10cd (diff)
downloadopenttd-b6766c2ed5fa7e3cb703e1dcfdeacb26cde7d07e.tar.xz
(svn r22976) -Codechange: Split group statistics into separate struct.
Diffstat (limited to 'src')
-rw-r--r--src/engine.cpp8
-rw-r--r--src/group.h19
-rw-r--r--src/group_cmd.cpp36
-rw-r--r--src/group_gui.cpp6
-rw-r--r--src/vehicle.cpp2
5 files changed, 48 insertions, 23 deletions
diff --git a/src/engine.cpp b/src/engine.cpp
index 4e212640e..6f5ea40a8 100644
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -477,9 +477,7 @@ void SetCachedEngineCounts()
/* 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 @@ void SetCachedEngineCounts()
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++;
}
}
diff --git a/src/group.h b/src/group.h
index 15087d59d..e28d36120 100644
--- 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 @@ uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e);
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 IncreaseGroupNumVehicle(GroupID id_g)
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--;
}
diff --git a/src/group_cmd.cpp b/src/group_cmd.cpp
index 3cd6f6757..1dfa8e258 100644
--- a/src/group_cmd.cpp
+++ b/src/group_cmd.cpp
@@ -32,6 +32,29 @@ GroupID _new_group_id;
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 @@ static inline void UpdateNumEngineGroup(EngineID i, 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)->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 @@ static inline void UpdateNumEngineGroup(EngineID i, GroupID old_g, GroupID new_g
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 @@ void UpdateTrainGroupID(Train *v)
*/
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;
}
diff --git a/src/group_gui.cpp b/src/group_gui.cpp
index e5762ca26..7ab0f82d8 100644
--- a/src/group_gui.cpp
+++ b/src/group_gui.cpp
@@ -303,8 +303,8 @@ public:
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 @@ public:
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;
diff --git a/src/vehicle.cpp b/src/vehicle.cpp
index e9177f092..c6539ef67 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)->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);
}