diff --git a/src/group_cmd.cpp b/src/group_cmd.cpp index 12cce41f7..9a19090a2 100644 --- a/src/group_cmd.cpp +++ b/src/group_cmd.cpp @@ -140,16 +140,27 @@ void GroupStatistics::Clear() assert(delta == 1 || delta == -1); GroupStatistics &stats_all = GroupStatistics::GetAllGroup(v); - GroupStatistics &stats = GroupStatistics::Get(v); - stats_all.num_vehicle += delta; - stats.num_vehicle += delta; - if (v->age > VEHICLE_PROFIT_MIN_AGE) { stats_all.num_profit_vehicle += delta; stats_all.profit_last_year += v->GetDisplayProfitLastYear() * delta; - stats.num_profit_vehicle += delta; - stats.profit_last_year += v->GetDisplayProfitLastYear() * delta; + } + + if (IsDefaultGroupID(v->group_id)) { + GroupStatistics &stats = GroupStatistics::Get(v); + stats.num_vehicle += delta; + if (v->age > VEHICLE_PROFIT_MIN_AGE) { + stats.num_profit_vehicle += delta; + stats.profit_last_year += v->GetDisplayProfitLastYear() * delta; + } + } else { + for (Group *g = Group::Get(v->group_id); g != NULL; g = Group::GetIfValid(g->parent)) { + g->statistics.num_vehicle += delta; + if (v->age > VEHICLE_PROFIT_MIN_AGE) { + g->statistics.num_profit_vehicle += delta; + g->statistics.profit_last_year += v->GetDisplayProfitLastYear() * delta; + } + } } } @@ -171,12 +182,19 @@ void GroupStatistics::Clear() /* static */ void GroupStatistics::VehicleReachedProfitAge(const Vehicle *v) { GroupStatistics &stats_all = GroupStatistics::GetAllGroup(v); - GroupStatistics &stats = GroupStatistics::Get(v); - stats_all.num_profit_vehicle++; stats_all.profit_last_year += v->GetDisplayProfitLastYear(); - stats.num_profit_vehicle++; - stats.profit_last_year += v->GetDisplayProfitLastYear(); + + if (IsDefaultGroupID(v->group_id)) { + GroupStatistics &stats = GroupStatistics::Get(v); + stats.num_profit_vehicle++; + stats.profit_last_year += v->GetDisplayProfitLastYear(); + } else { + for (Group *g = Group::Get(v->group_id); g != NULL; g = Group::GetIfValid(g->parent)) { + g->statistics.num_profit_vehicle++; + g->statistics.profit_last_year += v->GetDisplayProfitLastYear(); + } + } } /** @@ -396,7 +414,7 @@ CommandCost CmdAlterGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 } } else { /* Set group parent */ - const Group *pg = Group::GetIfValid(GB(p2, 0, 16)); + Group *pg = Group::GetIfValid(GB(p2, 0, 16)); if (pg != NULL) { if (pg->owner != _current_company) return CMD_ERROR; @@ -408,7 +426,21 @@ CommandCost CmdAlterGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 } if (flags & DC_EXEC) { + /* The vehicles of a group affect not only the group's stats, but also the stats of parent groups. + * When moving the group, the stats of the parent groups at the old and new position need to be updated. */ + for (Group *g2 = Group::GetIfValid(g->parent); g2 != NULL; g2 = Group::GetIfValid(g2->parent)) { + g2->statistics.num_vehicle -= g->statistics.num_vehicle; + g2->statistics.num_profit_vehicle -= g->statistics.num_profit_vehicle; + g2->statistics.profit_last_year -= g->statistics.profit_last_year; + } + g->parent = (pg == NULL) ? INVALID_GROUP : pg->index; + + for (Group *g2 = pg; g2 != NULL; g2 = Group::GetIfValid(g2->parent)) { + g2->statistics.num_vehicle += g->statistics.num_vehicle; + g2->statistics.num_profit_vehicle += g->statistics.num_profit_vehicle; + g2->statistics.profit_last_year += g->statistics.profit_last_year; + } } }