summaryrefslogtreecommitdiff
path: root/src/group_cmd.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/group_cmd.cpp')
-rw-r--r--src/group_cmd.cpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/group_cmd.cpp b/src/group_cmd.cpp
index 1dfa8e258..d4148474f 100644
--- a/src/group_cmd.cpp
+++ b/src/group_cmd.cpp
@@ -54,6 +54,45 @@ void GroupStatistics::Clear()
this->num_engines = CallocT<uint16>(Engine::GetPoolSize());
}
+/**
+ * Update all caches after loading a game, changing NewGRF etc..
+ */
+/* static */ void GroupStatistics::UpdateAfterLoad()
+{
+ size_t engines = Engine::GetPoolSize();
+
+ /* Set up the engine count for all companies */
+ Company *c;
+ FOR_ALL_COMPANIES(c) {
+ free(c->num_engines);
+ c->num_engines = CallocT<EngineID>(engines);
+ }
+
+ /* Recalculate */
+ Group *g;
+ FOR_ALL_GROUPS(g) {
+ g->statistics.Clear();
+ }
+
+ const Vehicle *v;
+ FOR_ALL_VEHICLES(v) {
+ if (!v->IsEngineCountable()) continue;
+
+ assert(v->engine_type < engines);
+
+ 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]++;
+ if (v->IsPrimaryVehicle()) g->statistics.num_vehicle++;
+ }
+}
+
/**
* Update the num engines of a groupID. Decrease the old one and increase the new one