Update: Translations from eints
[openttd-github.git] / src / group_cmd.cpp
blob1bc3c6621d3260a149597c9896fa9c0ff6f85eb8
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file group_cmd.cpp Handling of the engine groups */
10 #include "stdafx.h"
11 #include "command_func.h"
12 #include "train.h"
13 #include "vehiclelist.h"
14 #include "vehicle_func.h"
15 #include "autoreplace_base.h"
16 #include "autoreplace_func.h"
17 #include "string_func.h"
18 #include "company_func.h"
19 #include "core/pool_func.hpp"
20 #include "order_backup.h"
21 #include "group_cmd.h"
23 #include "table/strings.h"
25 #include "safeguards.h"
27 GroupPool _group_pool("Group");
28 INSTANTIATE_POOL_METHODS(Group)
30 /**
31 * Clear all caches.
33 void GroupStatistics::Clear()
35 this->num_vehicle = 0;
36 this->profit_last_year = 0;
37 this->num_vehicle_min_age = 0;
38 this->profit_last_year_min_age = 0;
40 /* This is also called when NewGRF change. So the number of engines might have changed. Reset. */
41 this->num_engines.clear();
44 /**
45 * Get number of vehicles of a specific engine ID.
46 * @param engine Engine ID.
47 * @returns number of vehicles of this engine ID.
49 uint16_t GroupStatistics::GetNumEngines(EngineID engine) const
51 auto found = this->num_engines.find(engine);
52 if (found != std::end(this->num_engines)) return found->second;
53 return 0;
56 /**
57 * Returns the GroupStatistics for a specific group.
58 * @param company Owner of the group.
59 * @param id_g GroupID of the group.
60 * @param type VehicleType of the vehicles in the group.
61 * @return Statistics for the group.
63 /* static */ GroupStatistics &GroupStatistics::Get(CompanyID company, GroupID id_g, VehicleType type)
65 if (Group::IsValidID(id_g)) {
66 Group *g = Group::Get(id_g);
67 assert(g->owner == company);
68 assert(g->vehicle_type == type);
69 return g->statistics;
72 if (IsDefaultGroupID(id_g)) return Company::Get(company)->group_default[type];
73 if (IsAllGroupID(id_g)) return Company::Get(company)->group_all[type];
75 NOT_REACHED();
78 /**
79 * Returns the GroupStatistic for the group of a vehicle.
80 * @param v Vehicle.
81 * @return GroupStatistics for the group of the vehicle.
83 /* static */ GroupStatistics &GroupStatistics::Get(const Vehicle *v)
85 return GroupStatistics::Get(v->owner, v->group_id, v->type);
88 /**
89 * Returns the GroupStatistic for the ALL_GROUPO of a vehicle type.
90 * @param v Vehicle.
91 * @return GroupStatistics for the ALL_GROUP of the vehicle type.
93 /* static */ GroupStatistics &GroupStatistics::GetAllGroup(const Vehicle *v)
95 return GroupStatistics::Get(v->owner, ALL_GROUP, v->type);
98 /**
99 * Update all caches after loading a game, changing NewGRF, etc.
101 /* static */ void GroupStatistics::UpdateAfterLoad()
103 /* Set up the engine count for all companies */
104 for (Company *c : Company::Iterate()) {
105 for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
106 c->group_all[type].Clear();
107 c->group_default[type].Clear();
111 /* Recalculate */
112 for (Group *g : Group::Iterate()) {
113 g->statistics.Clear();
116 for (const Vehicle *v : Vehicle::Iterate()) {
117 if (!v->IsEngineCountable()) continue;
119 GroupStatistics::CountEngine(v, 1);
120 if (v->IsPrimaryVehicle()) GroupStatistics::CountVehicle(v, 1);
123 for (const Company *c : Company::Iterate()) {
124 GroupStatistics::UpdateAutoreplace(c->index);
129 * Update num_vehicle when adding or removing a vehicle.
130 * @param v Vehicle to count.
131 * @param delta +1 to add, -1 to remove.
133 /* static */ void GroupStatistics::CountVehicle(const Vehicle *v, int delta)
135 assert(delta == 1 || delta == -1);
137 GroupStatistics &stats_all = GroupStatistics::GetAllGroup(v);
138 GroupStatistics &stats = GroupStatistics::Get(v);
140 stats_all.num_vehicle += delta;
141 stats_all.profit_last_year += v->GetDisplayProfitLastYear() * delta;
142 stats.num_vehicle += delta;
143 stats.profit_last_year += v->GetDisplayProfitLastYear() * delta;
145 if (v->economy_age > VEHICLE_PROFIT_MIN_AGE) {
146 stats_all.num_vehicle_min_age += delta;
147 stats_all.profit_last_year_min_age += v->GetDisplayProfitLastYear() * delta;
148 stats.num_vehicle_min_age += delta;
149 stats.profit_last_year_min_age += v->GetDisplayProfitLastYear() * delta;
154 * Update num_engines when adding/removing an engine.
155 * @param v Engine to count.
156 * @param delta +1 to add, -1 to remove.
158 /* static */ void GroupStatistics::CountEngine(const Vehicle *v, int delta)
160 assert(delta == 1 || delta == -1);
161 GroupStatistics::GetAllGroup(v).num_engines[v->engine_type] += delta;
162 GroupStatistics::Get(v).num_engines[v->engine_type] += delta;
166 * Add a vehicle's last year profit to the profit sum of its group.
168 /* static */ void GroupStatistics::AddProfitLastYear(const Vehicle *v)
170 GroupStatistics &stats_all = GroupStatistics::GetAllGroup(v);
171 GroupStatistics &stats = GroupStatistics::Get(v);
173 stats_all.profit_last_year += v->GetDisplayProfitLastYear();
174 stats.profit_last_year += v->GetDisplayProfitLastYear();
178 * Add a vehicle to the profit sum of its group.
180 /* static */ void GroupStatistics::VehicleReachedMinAge(const Vehicle *v)
182 GroupStatistics &stats_all = GroupStatistics::GetAllGroup(v);
183 GroupStatistics &stats = GroupStatistics::Get(v);
185 stats_all.num_vehicle_min_age++;
186 stats_all.profit_last_year_min_age += v->GetDisplayProfitLastYear();
187 stats.num_vehicle_min_age++;
188 stats.profit_last_year_min_age += v->GetDisplayProfitLastYear();
192 * Recompute the profits for all groups.
194 /* static */ void GroupStatistics::UpdateProfits()
196 /* Set up the engine count for all companies */
197 for (Company *c : Company::Iterate()) {
198 for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
199 c->group_all[type].ClearProfits();
200 c->group_default[type].ClearProfits();
204 /* Recalculate */
205 for (Group *g : Group::Iterate()) {
206 g->statistics.ClearProfits();
209 for (const Vehicle *v : Vehicle::Iterate()) {
210 if (v->IsPrimaryVehicle()) {
211 GroupStatistics::AddProfitLastYear(v);
212 if (v->economy_age > VEHICLE_PROFIT_MIN_AGE) GroupStatistics::VehicleReachedMinAge(v);
218 * Update autoreplace_defined and autoreplace_finished of all statistics of a company.
219 * @param company Company to update statistics for.
221 /* static */ void GroupStatistics::UpdateAutoreplace(CompanyID company)
223 /* Set up the engine count for all companies */
224 Company *c = Company::Get(company);
225 for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
226 c->group_all[type].ClearAutoreplace();
227 c->group_default[type].ClearAutoreplace();
230 /* Recalculate */
231 for (Group *g : Group::Iterate()) {
232 if (g->owner != company) continue;
233 g->statistics.ClearAutoreplace();
236 for (EngineRenewList erl = c->engine_renew_list; erl != nullptr; erl = erl->next) {
237 const Engine *e = Engine::Get(erl->from);
238 GroupStatistics &stats = GroupStatistics::Get(company, erl->group_id, e->type);
239 if (!stats.autoreplace_defined) {
240 stats.autoreplace_defined = true;
241 stats.autoreplace_finished = true;
243 if (GetGroupNumEngines(company, erl->group_id, erl->from) > 0) stats.autoreplace_finished = false;
248 * Update the num engines of a groupID. Decrease the old one and increase the new one
249 * @note called in SetTrainGroupID and UpdateTrainGroupID
250 * @param v Vehicle we have to update
251 * @param old_g index of the old group
252 * @param new_g index of the new group
254 static inline void UpdateNumEngineGroup(const Vehicle *v, GroupID old_g, GroupID new_g)
256 if (old_g != new_g) {
257 /* Decrease the num engines in the old group */
258 GroupStatistics::Get(v->owner, old_g, v->type).num_engines[v->engine_type]--;
260 /* Increase the num engines in the new group */
261 GroupStatistics::Get(v->owner, new_g, v->type).num_engines[v->engine_type]++;
266 const Livery *GetParentLivery(const Group *g)
268 if (g->parent == INVALID_GROUP) {
269 const Company *c = Company::Get(g->owner);
270 return &c->livery[LS_DEFAULT];
273 const Group *pg = Group::Get(g->parent);
274 return &pg->livery;
279 * Propagate a livery change to a group's children, and optionally update cached vehicle colourmaps.
280 * @param g Group to propagate colours to children.
281 * @param reset_cache Reset colourmap of vehicles in this group.
283 static void PropagateChildLivery(const Group *g, bool reset_cache)
285 if (reset_cache) {
286 /* Company colour data is indirectly cached. */
287 for (Vehicle *v : Vehicle::Iterate()) {
288 if (v->group_id == g->index && (!v->IsGroundVehicle() || v->IsFrontEngine())) {
289 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
290 u->colourmap = PAL_NONE;
291 u->InvalidateNewGRFCache();
297 for (Group *cg : Group::Iterate()) {
298 if (cg->parent == g->index) {
299 if (!HasBit(cg->livery.in_use, 0)) cg->livery.colour1 = g->livery.colour1;
300 if (!HasBit(cg->livery.in_use, 1)) cg->livery.colour2 = g->livery.colour2;
301 PropagateChildLivery(cg, reset_cache);
307 * Update group liveries for a company. This is called when the LS_DEFAULT scheme is changed, to update groups with
308 * colours set to default.
309 * @param c Company to update.
311 void UpdateCompanyGroupLiveries(const Company *c)
313 for (Group *g : Group::Iterate()) {
314 if (g->owner == c->index && g->parent == INVALID_GROUP) {
315 if (!HasBit(g->livery.in_use, 0)) g->livery.colour1 = c->livery[LS_DEFAULT].colour1;
316 if (!HasBit(g->livery.in_use, 1)) g->livery.colour2 = c->livery[LS_DEFAULT].colour2;
317 PropagateChildLivery(g, false);
322 Group::Group(Owner owner)
324 this->owner = owner;
325 this->folded = false;
330 * Create a new vehicle group.
331 * @param flags type of operation
332 * @param vt vehicle type
333 * @param parent_group parent groupid
334 * @return the cost of this operation or an error
336 std::tuple<CommandCost, GroupID> CmdCreateGroup(DoCommandFlag flags, VehicleType vt, GroupID parent_group)
338 if (!IsCompanyBuildableVehicleType(vt)) return { CMD_ERROR, INVALID_GROUP };
340 if (!Group::CanAllocateItem()) return { CMD_ERROR, INVALID_GROUP };
342 const Group *pg = Group::GetIfValid(parent_group);
343 if (pg != nullptr) {
344 if (pg->owner != _current_company) return { CMD_ERROR, INVALID_GROUP };
345 if (pg->vehicle_type != vt) return { CMD_ERROR, INVALID_GROUP };
348 if (flags & DC_EXEC) {
349 Group *g = new Group(_current_company);
350 g->vehicle_type = vt;
351 g->parent = INVALID_GROUP;
353 Company *c = Company::Get(g->owner);
354 g->number = c->freegroups.UseID(c->freegroups.NextID());
355 if (pg == nullptr) {
356 g->livery.colour1 = c->livery[LS_DEFAULT].colour1;
357 g->livery.colour2 = c->livery[LS_DEFAULT].colour2;
358 if (c->settings.renew_keep_length) SetBit(g->flags, GroupFlags::GF_REPLACE_WAGON_REMOVAL);
359 } else {
360 g->parent = pg->index;
361 g->livery.colour1 = pg->livery.colour1;
362 g->livery.colour2 = pg->livery.colour2;
363 g->flags = pg->flags;
366 InvalidateWindowData(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_GROUP_LIST, vt, _current_company).Pack());
367 InvalidateWindowData(WC_COMPANY_COLOUR, g->owner, g->vehicle_type);
369 return { CommandCost(), g->index };
372 return { CommandCost(), INVALID_GROUP};
377 * Add all vehicles in the given group to the default group and then deletes the group.
378 * @param flags type of operation
379 * @param group_id index of group
380 * @return the cost of this operation or an error
382 CommandCost CmdDeleteGroup(DoCommandFlag flags, GroupID group_id)
384 Group *g = Group::GetIfValid(group_id);
385 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
387 /* Remove all vehicles from the group */
388 Command<CMD_REMOVE_ALL_VEHICLES_GROUP>::Do(flags, group_id);
390 /* Delete sub-groups */
391 for (const Group *gp : Group::Iterate()) {
392 if (gp->parent == g->index) {
393 Command<CMD_DELETE_GROUP>::Do(flags, gp->index);
397 if (flags & DC_EXEC) {
398 /* Update backupped orders if needed */
399 OrderBackup::ClearGroup(g->index);
401 if (g->owner < MAX_COMPANIES) {
402 Company *c = Company::Get(g->owner);
404 /* If we set an autoreplace for the group we delete, remove it. */
405 for (EngineRenew *er : EngineRenew::Iterate()) {
406 if (er->group_id == g->index) RemoveEngineReplacementForCompany(c, er->from, g->index, flags);
409 c->freegroups.ReleaseID(g->number);
412 VehicleType vt = g->vehicle_type;
414 /* Delete the Replace Vehicle Windows */
415 CloseWindowById(WC_REPLACE_VEHICLE, g->vehicle_type);
416 delete g;
418 InvalidateWindowData(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_GROUP_LIST, vt, _current_company).Pack());
419 InvalidateWindowData(WC_COMPANY_COLOUR, _current_company, vt);
422 return CommandCost();
426 * Alter a group
427 * @param flags type of operation
428 * @param mode Operation to perform.
429 * @param group_id GroupID
430 * @param parent_id parent group index
431 * @param text the new name or an empty string when resetting to the default
432 * @return the cost of this operation or an error
434 CommandCost CmdAlterGroup(DoCommandFlag flags, AlterGroupMode mode, GroupID group_id, GroupID parent_id, const std::string &text)
436 Group *g = Group::GetIfValid(group_id);
437 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
439 if (mode == AlterGroupMode::Rename) {
440 /* Rename group */
441 bool reset = text.empty();
443 if (!reset) {
444 if (Utf8StringLength(text) >= MAX_LENGTH_GROUP_NAME_CHARS) return CMD_ERROR;
447 if (flags & DC_EXEC) {
448 /* Assign the new one */
449 if (reset) {
450 g->name.clear();
451 } else {
452 g->name = text;
455 } else if (mode == AlterGroupMode::SetParent) {
456 /* Set group parent */
457 const Group *pg = Group::GetIfValid(parent_id);
459 if (pg != nullptr) {
460 if (pg->owner != _current_company) return CMD_ERROR;
461 if (pg->vehicle_type != g->vehicle_type) return CMD_ERROR;
463 /* Ensure request parent isn't child of group.
464 * This is the only place that infinite loops are prevented. */
465 if (GroupIsInGroup(pg->index, g->index)) return_cmd_error(STR_ERROR_GROUP_CAN_T_SET_PARENT_RECURSION);
468 if (flags & DC_EXEC) {
469 g->parent = (pg == nullptr) ? INVALID_GROUP : pg->index;
470 GroupStatistics::UpdateAutoreplace(g->owner);
472 if (!HasBit(g->livery.in_use, 0) || !HasBit(g->livery.in_use, 1)) {
473 /* Update livery with new parent's colours if either colour is default. */
474 const Livery *livery = GetParentLivery(g);
475 if (!HasBit(g->livery.in_use, 0)) g->livery.colour1 = livery->colour1;
476 if (!HasBit(g->livery.in_use, 1)) g->livery.colour2 = livery->colour2;
478 PropagateChildLivery(g, true);
479 MarkWholeScreenDirty();
482 } else {
483 return CMD_ERROR;
486 if (flags & DC_EXEC) {
487 InvalidateWindowData(WC_REPLACE_VEHICLE, g->vehicle_type, 1);
488 InvalidateWindowData(GetWindowClassForVehicleType(g->vehicle_type), VehicleListIdentifier(VL_GROUP_LIST, g->vehicle_type, _current_company).Pack());
489 InvalidateWindowData(WC_COMPANY_COLOUR, g->owner, g->vehicle_type);
490 InvalidateWindowClassesData(WC_VEHICLE_VIEW);
491 InvalidateWindowClassesData(WC_VEHICLE_DETAILS);
494 return CommandCost();
499 * Do add a vehicle to a group.
500 * @param v Vehicle to add.
501 * @param new_g Group to add to.
503 static void AddVehicleToGroup(Vehicle *v, GroupID new_g)
505 GroupStatistics::CountVehicle(v, -1);
507 switch (v->type) {
508 default: NOT_REACHED();
509 case VEH_TRAIN:
510 SetTrainGroupID(Train::From(v), new_g);
511 break;
513 case VEH_ROAD:
514 case VEH_SHIP:
515 case VEH_AIRCRAFT:
516 if (v->IsEngineCountable()) UpdateNumEngineGroup(v, v->group_id, new_g);
517 v->group_id = new_g;
518 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
519 u->colourmap = PAL_NONE;
520 u->InvalidateNewGRFCache();
521 u->UpdateViewport(true);
523 break;
526 GroupStatistics::CountVehicle(v, 1);
530 * Add a vehicle to a group
531 * @param flags type of operation
532 * @param group_id index of group
533 * @param veh_id vehicle to add to a group
534 * @param add_shared Add shared vehicles as well.
535 * @return the cost of this operation or an error
537 std::tuple<CommandCost, GroupID> CmdAddVehicleGroup(DoCommandFlag flags, GroupID group_id, VehicleID veh_id, bool add_shared, const VehicleListIdentifier &vli)
539 GroupID new_g = group_id;
540 if (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g) && new_g != NEW_GROUP) return { CMD_ERROR, INVALID_GROUP };
542 VehicleList list;
543 if (veh_id == INVALID_VEHICLE && vli.Valid()) {
544 if (!GenerateVehicleSortList(&list, vli) || list.empty()) return { CMD_ERROR, INVALID_GROUP };
545 } else {
546 Vehicle *v = Vehicle::GetIfValid(veh_id);
547 if (v == nullptr) return { CMD_ERROR, INVALID_GROUP };
548 list.push_back(v);
551 VehicleType vtype = list.front()->type;
552 for (const Vehicle *v : list) {
553 if (v->owner != _current_company || !v->IsPrimaryVehicle()) return { CMD_ERROR, INVALID_GROUP };
556 if (Group::IsValidID(new_g)) {
557 Group *g = Group::Get(new_g);
558 if (g->owner != _current_company || g->vehicle_type != vtype) return { CMD_ERROR, INVALID_GROUP };
561 if (new_g == NEW_GROUP) {
562 /* Create new group. */
563 auto [ret, new_group_id] = CmdCreateGroup(flags, vtype, INVALID_GROUP);
564 if (ret.Failed()) return { ret, new_group_id };
566 new_g = new_group_id;
569 if (flags & DC_EXEC) {
570 for (const Vehicle *vc : list) {
571 /* VehicleList is const but we need to modify the vehicle. */
572 Vehicle *v = Vehicle::Get(vc->index);
573 AddVehicleToGroup(v, new_g);
575 if (add_shared) {
576 /* Add vehicles in the shared order list as well. */
577 for (Vehicle *v2 = v->FirstShared(); v2 != nullptr; v2 = v2->NextShared()) {
578 if (v2->group_id != new_g) AddVehicleToGroup(v2, new_g);
582 SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
583 SetWindowDirty(WC_VEHICLE_VIEW, v->index);
584 SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
585 InvalidateWindowData(WC_VEHICLE_VIEW, v->index);
586 InvalidateWindowData(WC_VEHICLE_DETAILS, v->index);
589 GroupStatistics::UpdateAutoreplace(_current_company);
591 /* Update the Replace Vehicle Windows */
592 SetWindowDirty(WC_REPLACE_VEHICLE, vtype);
593 InvalidateWindowData(GetWindowClassForVehicleType(vtype), VehicleListIdentifier(VL_GROUP_LIST, vtype, _current_company).Pack());
596 return { CommandCost(), new_g };
600 * Add all shared vehicles of all vehicles from a group
601 * @param flags type of operation
602 * @param id_g index of group
603 * @param type type of vehicles
604 * @return the cost of this operation or an error
606 CommandCost CmdAddSharedVehicleGroup(DoCommandFlag flags, GroupID id_g, VehicleType type)
608 if (!Group::IsValidID(id_g) || !IsCompanyBuildableVehicleType(type)) return CMD_ERROR;
610 if (flags & DC_EXEC) {
611 /* Find the first front engine which belong to the group id_g
612 * then add all shared vehicles of this front engine to the group id_g */
613 for (const Vehicle *v : Vehicle::Iterate()) {
614 if (v->type == type && v->IsPrimaryVehicle()) {
615 if (v->group_id != id_g) continue;
617 /* For each shared vehicles add it to the group */
618 for (Vehicle *v2 = v->FirstShared(); v2 != nullptr; v2 = v2->NextShared()) {
619 if (v2->group_id != id_g) Command<CMD_ADD_VEHICLE_GROUP>::Do(flags, id_g, v2->index, false, VehicleListIdentifier{});
624 InvalidateWindowData(GetWindowClassForVehicleType(type), VehicleListIdentifier(VL_GROUP_LIST, type, _current_company).Pack());
627 return CommandCost();
632 * Remove all vehicles from a group
633 * @param flags type of operation
634 * @param group_id index of group
635 * @return the cost of this operation or an error
637 CommandCost CmdRemoveAllVehiclesGroup(DoCommandFlag flags, GroupID group_id)
639 Group *g = Group::GetIfValid(group_id);
641 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
643 if (flags & DC_EXEC) {
644 /* Find each Vehicle that belongs to the group old_g and add it to the default group */
645 for (const Vehicle *v : Vehicle::Iterate()) {
646 if (v->IsPrimaryVehicle()) {
647 if (v->group_id != group_id) continue;
649 /* Add The Vehicle to the default group */
650 Command<CMD_ADD_VEHICLE_GROUP>::Do(flags, DEFAULT_GROUP, v->index, false, VehicleListIdentifier{});
654 InvalidateWindowData(GetWindowClassForVehicleType(g->vehicle_type), VehicleListIdentifier(VL_GROUP_LIST, g->vehicle_type, _current_company).Pack());
657 return CommandCost();
661 * Set the livery for a vehicle group.
662 * @param flags Command flags.
663 * @param group_id Group ID.
664 * @param primary Set primary instead of secondary colour
665 * @param colour Colour.
667 CommandCost CmdSetGroupLivery(DoCommandFlag flags, GroupID group_id, bool primary, Colours colour)
669 Group *g = Group::GetIfValid(group_id);
671 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
673 if (colour >= COLOUR_END && colour != INVALID_COLOUR) return CMD_ERROR;
675 if (flags & DC_EXEC) {
676 if (primary) {
677 AssignBit(g->livery.in_use, 0, colour != INVALID_COLOUR);
678 if (colour == INVALID_COLOUR) colour = GetParentLivery(g)->colour1;
679 g->livery.colour1 = colour;
680 } else {
681 AssignBit(g->livery.in_use, 1, colour != INVALID_COLOUR);
682 if (colour == INVALID_COLOUR) colour = GetParentLivery(g)->colour2;
683 g->livery.colour2 = colour;
686 PropagateChildLivery(g, true);
687 MarkWholeScreenDirty();
690 return CommandCost();
694 * Set group flag for a group and its sub-groups.
695 * @param g initial group.
696 * @param set 1 to set or 0 to clear protection.
698 static void SetGroupFlag(Group *g, GroupFlags flag, bool set, bool children)
700 if (set) {
701 SetBit(g->flags, flag);
702 } else {
703 ClrBit(g->flags, flag);
706 if (!children) return;
708 for (Group *pg : Group::Iterate()) {
709 if (pg->parent == g->index) SetGroupFlag(pg, flag, set, true);
714 * (Un)set group flag from a group
715 * @param flags type of operation
716 * @param group_id index of group array
717 * @param flag flag to set, by value not bit.
718 * @param value value to set the flag to.
719 * @param recursive to apply to sub-groups.
720 * @return the cost of this operation or an error
722 CommandCost CmdSetGroupFlag(DoCommandFlag flags, GroupID group_id, GroupFlags flag, bool value, bool recursive)
724 Group *g = Group::GetIfValid(group_id);
725 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
727 if (flag >= GroupFlags::GF_END) return CMD_ERROR;
729 if (flags & DC_EXEC) {
730 SetGroupFlag(g, flag, value, recursive);
732 SetWindowDirty(GetWindowClassForVehicleType(g->vehicle_type), VehicleListIdentifier(VL_GROUP_LIST, g->vehicle_type, _current_company).Pack());
733 InvalidateWindowData(WC_REPLACE_VEHICLE, g->vehicle_type);
736 return CommandCost();
740 * Affect the groupID of a train to new_g.
741 * @note called in CmdAddVehicleGroup and CmdMoveRailVehicle
742 * @param v First vehicle of the chain.
743 * @param new_g index of array group
745 void SetTrainGroupID(Train *v, GroupID new_g)
747 if (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g)) return;
749 assert(v->IsFrontEngine() || IsDefaultGroupID(new_g));
751 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
752 if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
754 u->group_id = new_g;
755 u->colourmap = PAL_NONE;
756 u->InvalidateNewGRFCache();
757 u->UpdateViewport(true);
760 /* Update the Replace Vehicle Windows */
761 GroupStatistics::UpdateAutoreplace(v->owner);
762 SetWindowDirty(WC_REPLACE_VEHICLE, VEH_TRAIN);
767 * Recalculates the groupID of a train. Should be called each time a vehicle is added
768 * to/removed from the chain,.
769 * @note this needs to be called too for 'wagon chains' (in the depot, without an engine)
770 * @note Called in CmdBuildRailVehicle, CmdBuildRailWagon, CmdMoveRailVehicle, CmdSellRailWagon
771 * @param v First vehicle of the chain.
773 void UpdateTrainGroupID(Train *v)
775 assert(v->IsFrontEngine() || v->IsFreeWagon());
777 GroupID new_g = v->IsFrontEngine() ? v->group_id : (GroupID)DEFAULT_GROUP;
778 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
779 if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
781 u->group_id = new_g;
782 u->colourmap = PAL_NONE;
783 u->InvalidateNewGRFCache();
786 /* Update the Replace Vehicle Windows */
787 GroupStatistics::UpdateAutoreplace(v->owner);
788 SetWindowDirty(WC_REPLACE_VEHICLE, VEH_TRAIN);
792 * Get the number of engines with EngineID id_e in the group with GroupID
793 * id_g and its sub-groups.
794 * @param company The company the group belongs to
795 * @param id_g The GroupID of the group used
796 * @param id_e The EngineID of the engine to count
797 * @return The number of engines with EngineID id_e in the group
799 uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e)
801 uint count = 0;
802 const Engine *e = Engine::Get(id_e);
803 for (const Group *g : Group::Iterate()) {
804 if (g->parent == id_g) count += GetGroupNumEngines(company, g->index, id_e);
806 return count + GroupStatistics::Get(company, id_g, e->type).GetNumEngines(id_e);
810 * Get the number of vehicles in the group with GroupID
811 * id_g and its sub-groups.
812 * @param company The company the group belongs to
813 * @param id_g The GroupID of the group used
814 * @param type The vehicle type of the group
815 * @return The number of vehicles in the group
817 uint GetGroupNumVehicle(CompanyID company, GroupID id_g, VehicleType type)
819 uint count = 0;
820 for (const Group *g : Group::Iterate()) {
821 if (g->parent == id_g) count += GetGroupNumVehicle(company, g->index, type);
823 return count + GroupStatistics::Get(company, id_g, type).num_vehicle;
827 * Get the number of vehicles above profit minimum age in the group with GroupID
828 * id_g and its sub-groups.
829 * @param company The company the group belongs to
830 * @param id_g The GroupID of the group used
831 * @param type The vehicle type of the group
832 * @return The number of vehicles above profit minimum age in the group
834 uint GetGroupNumVehicleMinAge(CompanyID company, GroupID id_g, VehicleType type)
836 uint count = 0;
837 for (const Group *g : Group::Iterate()) {
838 if (g->parent == id_g) count += GetGroupNumVehicleMinAge(company, g->index, type);
840 return count + GroupStatistics::Get(company, id_g, type).num_vehicle_min_age;
844 * Get last year's profit of vehicles above minimum age
845 * for the group with GroupID id_g and its sub-groups.
846 * @param company The company the group belongs to
847 * @param id_g The GroupID of the group used
848 * @param type The vehicle type of the group
849 * @return Last year's profit of vehicles above minimum age for the group
851 Money GetGroupProfitLastYearMinAge(CompanyID company, GroupID id_g, VehicleType type)
853 Money sum = 0;
854 for (const Group *g : Group::Iterate()) {
855 if (g->parent == id_g) sum += GetGroupProfitLastYearMinAge(company, g->index, type);
857 return sum + GroupStatistics::Get(company, id_g, type).profit_last_year_min_age;
860 void RemoveAllGroupsForCompany(const CompanyID company)
862 for (Group *g : Group::Iterate()) {
863 if (company == g->owner) delete g;
869 * Test if GroupID group is a descendant of (or is) GroupID search
870 * @param search The GroupID to search in
871 * @param group The GroupID to search for
872 * @return True iff group is search or a descendant of search
874 bool GroupIsInGroup(GroupID search, GroupID group)
876 if (!Group::IsValidID(search)) return search == group;
878 do {
879 if (search == group) return true;
880 search = Group::Get(search)->parent;
881 } while (search != INVALID_GROUP);
883 return false;