OpenTTD Source 20261129-master-g72a8bdc861
group_cmd.cpp
Go to the documentation of this file.
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11#include "command_func.h"
12#include "company_base.h"
13#include "group.h"
14#include "train.h"
15#include "vehiclelist.h"
16#include "vehicle_func.h"
17#include "vehicle_gui.h"
18#include "string_func.h"
19#include "company_func.h"
20#include "core/pool_func.hpp"
21#include "order_backup.h"
22#include "group_cmd.h"
23
24#include "table/strings.h"
25
26#include "safeguards.h"
27
28GroupPool _group_pool("Group");
30
31
35{
36 this->num_vehicle = 0;
37 this->profit_last_year = 0;
38 this->num_vehicle_min_age = 0;
40
41 /* This is also called when NewGRF change. So the number of engines might have changed. Reset. */
42 this->num_engines.clear();
43}
44
49{
50 for (Group *g : Group::Iterate()) {
51 if (g->parent == GroupID::Invalid()) continue;
52 Group *pg = Group::GetIfValid(g->parent);
53 if (pg == nullptr || pg->owner != g->owner || pg->vehicle_type != g->vehicle_type) {
54 /* Due to a bug, groups which should have been deleted could be left with an invalid parent.
55 * Keep the group but clear the invalid parent so that the game is recoverable. */
56 Debug(Facility::Misc, Severity::Warning, "Group {} has invalid parent {}", g->index, g->parent);
57 g->parent = GroupID::Invalid();
58 } else {
59 pg->children.insert(g->index);
60 }
61 }
62}
63
70{
71 auto found = this->num_engines.find(engine);
72 if (found != std::end(this->num_engines)) return found->second;
73 return 0;
74}
75
83/* static */ GroupStatistics &GroupStatistics::Get(CompanyID company, GroupID id_g, VehicleType type)
84{
85 if (Group::IsValidID(id_g)) {
86 Group *g = Group::Get(id_g);
87 assert(g->owner == company);
88 assert(g->vehicle_type == type);
89 return g->statistics;
90 }
91
92 if (IsDefaultGroupID(id_g)) return Company::Get(company)->group_default[type];
93 if (IsAllGroupID(id_g)) return Company::Get(company)->group_all[type];
94
95 NOT_REACHED();
96}
97
104{
105 return GroupStatistics::Get(v->owner, v->group_id, v->type);
106}
107
114{
116}
117
122{
123 /* Set up the engine count for all companies */
124 for (Company *c : Company::Iterate()) {
126 c->group_all[type].Clear();
127 c->group_default[type].Clear();
128 }
129 }
130
131 /* Recalculate */
132 for (Group *g : Group::Iterate()) {
133 g->statistics.Clear();
134 }
135
136 for (const Vehicle *v : Vehicle::Iterate()) {
137 if (!v->IsEngineCountable()) continue;
138
140 if (v->IsPrimaryVehicle()) GroupStatistics::CountVehicle(v, 1);
141 }
142
143 for (const Company *c : Company::Iterate()) {
145 }
146}
147
153/* static */ void GroupStatistics::CountVehicle(const Vehicle *v, int delta)
154{
155 assert(delta == 1 || delta == -1);
156
159
160 stats_all.num_vehicle += delta;
161 stats_all.profit_last_year += v->GetDisplayProfitLastYear() * delta;
162 stats.num_vehicle += delta;
163 stats.profit_last_year += v->GetDisplayProfitLastYear() * delta;
164
166 stats_all.num_vehicle_min_age += delta;
167 stats_all.profit_last_year_min_age += v->GetDisplayProfitLastYear() * delta;
168 stats.num_vehicle_min_age += delta;
170 }
171}
172
178/* static */ void GroupStatistics::CountEngine(const Vehicle *v, int delta)
179{
180 assert(delta == 1 || delta == -1);
183}
184
197
212
217{
218 /* Set up the engine count for all companies */
219 for (Company *c : Company::Iterate()) {
221 c->group_all[type].ClearProfits();
222 c->group_default[type].ClearProfits();
223 }
224 }
225
226 /* Recalculate */
227 for (Group *g : Group::Iterate()) {
228 g->statistics.ClearProfits();
229 }
230
231 for (const Vehicle *v : Vehicle::Iterate()) {
232 if (v->IsPrimaryVehicle()) {
235 }
236 }
237}
238
245static void UpdateAutoreplaceStatus(const EngineRenewList &erl, CompanyID company, GroupID group)
246{
247 for (const EngineRenew &er : erl) {
248 if (er.from == er.to) continue;
249
250 const Engine *e = Engine::Get(er.from);
251 GroupStatistics &stats = GroupStatistics::Get(company, group, e->type);
252
253 if (!stats.autoreplace_defined) {
254 stats.autoreplace_defined = true;
255 stats.autoreplace_finished = true;
256 }
257
258 if (GetGroupNumEngines(company, group, er.from) > 0) stats.autoreplace_finished = false;
259 }
260}
261
266/* static */ void GroupStatistics::UpdateAutoreplace(CompanyID company)
267{
268 /* Set up the engine count for all companies */
269 Company *c = Company::Get(company);
271 c->group_all[type].ClearAutoreplace();
272 c->group_default[type].ClearAutoreplace();
273 }
274
275 /* Recalculate */
276 for (Group *g : Group::Iterate()) {
277 if (g->owner != company) continue;
278 g->statistics.ClearAutoreplace();
279 UpdateAutoreplaceStatus(g->engine_renew_list, g->owner, g->index);
280 }
281
284}
285
293static inline void UpdateNumEngineGroup(const Vehicle *v, GroupID old_g, GroupID new_g)
294{
295 if (old_g != new_g) {
296 /* Decrease the num engines in the old group */
298
299 /* Increase the num engines in the new group */
301 }
302}
303
304
305const Livery *GetParentLivery(const Group *g)
306{
307 if (g->parent == GroupID::Invalid()) {
308 const Company *c = Company::Get(g->owner);
309 return &c->livery[LiveryScheme::Default];
310 }
311
312 const Group *pg = Group::Get(g->parent);
313 return &pg->livery;
314}
315
316
322static void PropagateChildLivery(const Group *g, bool reset_cache)
323{
324 if (reset_cache) {
325 /* Company colour data is indirectly cached. */
326 for (Vehicle *v : Vehicle::Iterate()) {
327 if (v->group_id == g->index && (!v->IsGroundVehicle() || v->IsFrontEngine())) {
328 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
329 u->colourmap = PAL_NONE;
330 u->InvalidateNewGRFCache();
331 }
332 }
333 }
334 }
335
336 for (const GroupID &childgroup : g->children) {
337 Group *cg = Group::Get(childgroup);
340 PropagateChildLivery(cg, reset_cache);
341 }
342}
343
350{
351 for (Group *g : Group::Iterate()) {
352 if (g->owner == c->index && g->parent == GroupID::Invalid()) {
355 PropagateChildLivery(g, false);
356 }
357 }
358}
359
360
368std::tuple<CommandCost, GroupID> CmdCreateGroup(DoCommandFlags flags, VehicleType vt, GroupID parent_group)
369{
370 if (!IsCompanyBuildableVehicleType(vt)) return { CMD_ERROR, GroupID::Invalid() };
371
372 if (!Group::CanAllocateItem()) return { CMD_ERROR, GroupID::Invalid() };
373
374 Group *pg = Group::GetIfValid(parent_group);
375 if (pg != nullptr) {
376 if (pg->owner != _current_company) return { CMD_ERROR, GroupID::Invalid() };
377 if (pg->vehicle_type != vt) return { CMD_ERROR, GroupID::Invalid() };
378 }
379
380 if (flags.Test(DoCommandFlag::Execute)) {
382
383 Company *c = Company::Get(g->owner);
384 g->number = c->freegroups.UseID(c->freegroups.NextID());
385 if (pg == nullptr) {
386 g->livery.colour1 = c->livery[LiveryScheme::Default].colour1;
387 g->livery.colour2 = c->livery[LiveryScheme::Default].colour2;
389 } else {
390 g->parent = pg->index;
391 g->livery.colour1 = pg->livery.colour1;
392 g->livery.colour2 = pg->livery.colour2;
393 g->flags = pg->flags;
394 pg->children.insert(g->index);
395 }
396
398 InvalidateWindowData(WindowClass::CompanyLivery, g->owner, g->vehicle_type);
399
400 return { CommandCost(), g->index };
401 }
402
403 return { CommandCost(), GroupID::Invalid()};
404}
405
406
414{
415 Group *g = Group::GetIfValid(group_id);
416 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
417
418 /* Remove all vehicles from the group */
419 Command<Commands::RemoveAllVehiclesGroup>::Do(flags, group_id);
420
421 /* Delete sub-groups, using a copy to avoid invalid iteration. */
422 FlatSet<GroupID> children = g->children;
423 for (const GroupID &childgroup : children) {
424 Command<Commands::DeleteGroup>::Do(flags, childgroup);
425 }
426
427 if (flags.Test(DoCommandFlag::Execute)) {
428 /* Update backupped orders if needed */
429 OrderBackup::ClearGroup(g->index);
430
431 if (g->owner < MAX_COMPANIES) {
432 Company *c = Company::Get(g->owner);
433 c->freegroups.ReleaseID(g->number);
434 }
435
436 if (g->parent != GroupID::Invalid()) {
437 Group *pg = Group::Get(g->parent);
438 pg->children.erase(g->index);
439 }
440
442
443 /* Delete the Replace Vehicle Windows */
444 CloseWindowById(WindowClass::ReplaceVehicle, g->vehicle_type);
445 delete g;
446
448 InvalidateWindowData(WindowClass::CompanyLivery, _current_company, vt);
449 }
450
451 return CommandCost();
452}
453
463CommandCost CmdAlterGroup(DoCommandFlags flags, AlterGroupMode mode, GroupID group_id, GroupID parent_id, const std::string &text)
464{
465 Group *g = Group::GetIfValid(group_id);
466 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
467
468 if (mode == AlterGroupMode::Rename) {
469 /* Rename group */
470 bool reset = text.empty();
471
472 if (!reset) {
474 }
475
476 if (flags.Test(DoCommandFlag::Execute)) {
477 /* Assign the new one */
478 if (reset) {
479 g->name.clear();
480 } else {
481 g->name = text;
482 }
483 }
484 } else if (mode == AlterGroupMode::SetParent) {
485 /* Do nothing if the parent group isn't actually changed. */
486 if (g->parent == parent_id) return CommandCost();
487
488 /* Set group parent */
489 const Group *pg = Group::GetIfValid(parent_id);
490
491 if (pg != nullptr) {
492 if (pg->owner != _current_company) return CMD_ERROR;
493 if (pg->vehicle_type != g->vehicle_type) return CMD_ERROR;
494
495 /* Ensure request parent isn't child of group.
496 * This is the only place that infinite loops are prevented. */
497 if (GroupIsInGroup(pg->index, g->index)) return CommandCost(STR_ERROR_GROUP_CAN_T_SET_PARENT_RECURSION);
498 }
499
500 if (flags.Test(DoCommandFlag::Execute)) {
501 if (g->parent != GroupID::Invalid()) Group::Get(g->parent)->children.erase(g->index);
502 g->parent = (pg == nullptr) ? GroupID::Invalid() : pg->index;
503 if (g->parent != GroupID::Invalid()) Group::Get(g->parent)->children.insert(g->index);
504
506
507 if (!g->livery.in_use.All({Livery::Flag::Primary, Livery::Flag::Secondary})) {
508 /* Update livery with new parent's colours if either colour is default. */
509 const Livery *livery = GetParentLivery(g);
512
513 PropagateChildLivery(g, true);
515 }
516 }
517 } else {
518 return CMD_ERROR;
519 }
520
521 if (flags.Test(DoCommandFlag::Execute)) {
522 InvalidateWindowData(WindowClass::ReplaceVehicle, g->vehicle_type, 1);
524 InvalidateWindowData(WindowClass::CompanyLivery, g->owner, g->vehicle_type);
525 InvalidateWindowClassesData(WindowClass::VehicleView);
526 InvalidateWindowClassesData(WindowClass::VehicleDetails);
527 }
528
529 return CommandCost();
530}
531
532
538static void AddVehicleToGroup(Vehicle *v, GroupID new_g)
539{
541
542 switch (v->type) {
543 default: NOT_REACHED();
545 SetTrainGroupID(Train::From(v), new_g);
546 break;
547
551 if (v->IsEngineCountable()) UpdateNumEngineGroup(v, v->group_id, new_g);
552 v->group_id = new_g;
553 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
554 u->colourmap = PAL_NONE;
555 u->InvalidateNewGRFCache();
556 u->UpdateViewport(true);
557 }
558 break;
559 }
560
561 InvalidateWindowData(WindowClass::VehicleView, v->index);
562 InvalidateWindowData(WindowClass::VehicleDetails, v->index);
563
565}
566
576std::tuple<CommandCost, GroupID> CmdAddVehicleGroup(DoCommandFlags flags, GroupID group_id, VehicleID veh_id, bool add_shared, const VehicleListIdentifier &vli)
577{
578 GroupID new_g = group_id;
579 if (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g) && new_g != NEW_GROUP) return { CMD_ERROR, GroupID::Invalid() };
580
581 VehicleList list;
582 if (veh_id == VehicleID::Invalid() && vli.Valid()) {
583 if (!GenerateVehicleSortList(&list, vli) || list.empty()) return { CMD_ERROR, GroupID::Invalid() };
584 } else {
585 const Vehicle *v = Vehicle::GetIfValid(veh_id);
586 if (v == nullptr || !IsCompanyBuildableVehicleType(v)) return { CMD_ERROR, GroupID::Invalid() };
587 list.push_back(v);
588 }
589
590 VehicleType vtype = list.front()->type;
591 for (const Vehicle *v : list) {
592 if (v->owner != _current_company || !v->IsPrimaryVehicle()) return { CMD_ERROR, GroupID::Invalid() };
593 }
594
595 if (Group::IsValidID(new_g)) {
596 const Group *g = Group::Get(new_g);
597 if (g->owner != _current_company || g->vehicle_type != vtype) return { CMD_ERROR, GroupID::Invalid() };
598 }
599
600 if (new_g == NEW_GROUP) {
601 /* Create new group. */
602 auto [ret, new_group_id] = CmdCreateGroup(flags, vtype, GroupID::Invalid());
603 if (ret.Failed()) return { ret, new_group_id };
604
605 new_g = new_group_id;
606 }
607
608 if (flags.Test(DoCommandFlag::Execute)) {
609 for (const Vehicle *vc : list) {
610 /* VehicleList is const but we need to modify the vehicle. */
611 Vehicle *v = Vehicle::Get(vc->index);
612 AddVehicleToGroup(v, new_g);
613
614 if (add_shared) {
615 /* Add vehicles in the shared order list as well. */
616 for (Vehicle *v2 = v->FirstShared(); v2 != nullptr; v2 = v2->NextShared()) {
617 if (v2->group_id != new_g) AddVehicleToGroup(v2, new_g);
618 }
619 }
620
621 SetWindowDirty(WindowClass::VehicleDepot, v->tile);
622 }
623
625
626 /* Update the Replace Vehicle Windows */
627 SetWindowDirty(WindowClass::ReplaceVehicle, vtype);
629 }
630
631 return { CommandCost(), new_g };
632}
633
642{
643 if (!Group::IsValidID(id_g) || !IsCompanyBuildableVehicleType(type)) return CMD_ERROR;
644
645 if (flags.Test(DoCommandFlag::Execute)) {
646 /* Find the first front engine which belong to the group id_g
647 * then add all shared vehicles of this front engine to the group id_g */
648 for (const Vehicle *v : Vehicle::Iterate()) {
649 if (v->type == type && v->IsPrimaryVehicle()) {
650 if (v->group_id != id_g) continue;
651
652 /* For each shared vehicles add it to the group */
653 for (Vehicle *v2 = v->FirstShared(); v2 != nullptr; v2 = v2->NextShared()) {
654 if (v2->group_id != id_g) Command<Commands::AddVehicleToGroup>::Do(flags, id_g, v2->index, false, VehicleListIdentifier{});
655 }
656 }
657 }
658
660 }
661
662 return CommandCost();
663}
664
665
673{
674 const Group *g = Group::GetIfValid(group_id);
675
676 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
677
678 if (flags.Test(DoCommandFlag::Execute)) {
679 /* Find each Vehicle that belongs to the group old_g and add it to the default group */
680 for (const Vehicle *v : Vehicle::Iterate()) {
681 if (v->IsPrimaryVehicle()) {
682 if (v->group_id != group_id) continue;
683
684 /* Add The Vehicle to the default group */
685 Command<Commands::AddVehicleToGroup>::Do(flags, DEFAULT_GROUP, v->index, false, VehicleListIdentifier{});
686 }
687 }
688
690 }
691
692 return CommandCost();
693}
694
703CommandCost CmdSetGroupLivery(DoCommandFlags flags, GroupID group_id, bool primary, Colours colour)
704{
705 Group *g = Group::GetIfValid(group_id);
706
707 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
708
709 if (colour >= Colours::End && colour != Colours::Invalid) return CMD_ERROR;
710
711 if (flags.Test(DoCommandFlag::Execute)) {
712 if (primary) {
714 if (colour == Colours::Invalid) colour = GetParentLivery(g)->colour1;
715 g->livery.colour1 = colour;
716 } else {
718 if (colour == Colours::Invalid) colour = GetParentLivery(g)->colour2;
719 g->livery.colour2 = colour;
720 }
721
722 PropagateChildLivery(g, true);
724 }
725
726 return CommandCost();
727}
728
736static void SetGroupFlag(Group *g, GroupFlag flag, bool set, bool children)
737{
738 if (set) {
739 g->flags.Set(flag);
740 } else {
741 g->flags.Reset(flag);
742 }
743
744 if (!children) return;
745
746 for (const GroupID &childgroup : g->children) {
747 SetGroupFlag(Group::Get(childgroup), flag, set, true);
748 }
749}
750
760CommandCost CmdSetGroupFlag(DoCommandFlags flags, GroupID group_id, GroupFlag flag, bool value, bool recursive)
761{
762 Group *g = Group::GetIfValid(group_id);
763 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
764
766
767 if (flags.Test(DoCommandFlag::Execute)) {
768 SetGroupFlag(g, flag, value, recursive);
769
771 InvalidateWindowData(WindowClass::ReplaceVehicle, g->vehicle_type);
772 }
773
774 return CommandCost();
775}
776
783void SetTrainGroupID(Train *v, GroupID new_g)
784{
785 if (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g)) return;
786
787 assert(v->IsFrontEngine() || IsDefaultGroupID(new_g));
788
789 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
790 if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
791
792 u->group_id = new_g;
793 u->colourmap = PAL_NONE;
794 u->InvalidateNewGRFCache();
795 u->UpdateViewport(true);
796 }
797
798 /* Update the Replace Vehicle Windows */
800 SetWindowDirty(WindowClass::ReplaceVehicle, VehicleType::Train);
801}
802
803
812{
813 assert(v->IsFrontEngine() || v->IsFreeWagon());
814
815 GroupID new_g = v->IsFrontEngine() ? v->group_id : (GroupID)DEFAULT_GROUP;
816 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
817 if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
818
819 u->group_id = new_g;
820 u->colourmap = PAL_NONE;
821 u->InvalidateNewGRFCache();
822 }
823
824 /* Update the Replace Vehicle Windows */
826 SetWindowDirty(WindowClass::ReplaceVehicle, VehicleType::Train);
827}
828
837uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e)
838{
839 uint count = 0;
840
841 if (const Group *g = Group::GetIfValid(id_g); g != nullptr) {
842 for (const GroupID &childgroup : g->children) {
843 count += GetGroupNumEngines(company, childgroup, id_e);
844 }
845 }
846
847 return count + GroupStatistics::Get(company, id_g, Engine::Get(id_e)->type).GetNumEngines(id_e);
848}
849
858uint GetGroupNumVehicle(CompanyID company, GroupID id_g, VehicleType type)
859{
860 uint count = 0;
861
862 if (const Group *g = Group::GetIfValid(id_g); g != nullptr) {
863 for (const GroupID &childgroup : g->children) {
864 count += GetGroupNumVehicle(company, childgroup, type);
865 }
866 }
867
868 return count + GroupStatistics::Get(company, id_g, type).num_vehicle;
869}
870
879uint GetGroupNumVehicleMinAge(CompanyID company, GroupID id_g, VehicleType type)
880{
881 uint count = 0;
882
883 if (const Group *g = Group::GetIfValid(id_g); g != nullptr) {
884 for (const GroupID &childgroup : g->children) {
885 count += GetGroupNumVehicleMinAge(company, childgroup, type);
886 }
887 }
888
889 return count + GroupStatistics::Get(company, id_g, type).num_vehicle_min_age;
890}
891
900Money GetGroupProfitLastYearMinAge(CompanyID company, GroupID id_g, VehicleType type)
901{
902 Money sum = 0;
903
904 if (const Group *g = Group::GetIfValid(id_g); g != nullptr) {
905 for (const GroupID &childgroup : g->children) {
906 sum += GetGroupProfitLastYearMinAge(company, childgroup, type);
907 }
908 }
909
910 return sum + GroupStatistics::Get(company, id_g, type).profit_last_year_min_age;
911}
912
913void RemoveAllGroupsForCompany(const CompanyID company)
914{
915 for (Group *g : Group::Iterate()) {
916 if (company == g->owner) delete g;
917 }
918}
919
920
927bool GroupIsInGroup(GroupID search, GroupID group)
928{
929 if (!Group::IsValidID(search)) return search == group;
930
931 do {
932 if (search == group) return true;
933 search = Group::Get(search)->parent;
934 } while (search != GroupID::Invalid());
935
936 return false;
937}
std::vector< EngineRenew > EngineRenewList
A list to group EngineRenew directives together (such as per-company).
constexpr bool All(const Timpl &other) const
Test if all of the values are set.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Timpl & Reset()
Reset all bits.
constexpr Timpl & Set()
Set all bits.
Common return value for all commands.
VehicleType type
Vehicle type, ie VehicleType::Road, VehicleType::Train, etc.
Definition engine_base.h:64
Iterate a range of enum values.
Flat set implementation that uses a sorted vector for storage.
UnitID UseID(UnitID index)
Use a unit number.
Definition vehicle.cpp:1892
void ReleaseID(UnitID index)
Release a unit number.
Definition vehicle.cpp:1909
UnitID NextID() const
Find first unused unit number.
Definition vehicle.cpp:1877
Functions related to commands.
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
@ Execute
execute the given command
EnumBitSet< DoCommandFlag, uint16_t > DoCommandFlags
Bitset of DoCommandFlag elements.
Definition of stuff that is very close to a company, like the company struct itself.
CompanyID _current_company
Company currently doing an action.
Functions related to companies.
#define Debug(facility, severity, format_string,...)
Output a line of debugging information.
Definition debug.h:36
@ Misc
Misc message facility.
Definition debug_type.h:32
@ Warning
Warning, wrong but okay if you don't know.
Definition debug_type.h:17
PoolID< uint16_t, struct EngineIDTag, 64000, 0xFFFF > EngineID
Unique identification number of an engine.
Definition engine_type.h:25
Colours
One of 16 base colours used for companies and windows/widgets.
Definition gfx_type.h:283
@ Invalid
Invalid marker.
Definition gfx_type.h:302
@ End
End-of-array marker.
Definition gfx_type.h:301
Base class for groups and group functions.
GroupFlag
Configuration flags for a group.
Definition group.h:68
@ ReplaceProtection
If set, the global autoreplace has no effect on the group.
Definition group.h:69
@ ReplaceWagonRemoval
If set, autoreplace will perform wagon removal on vehicles in this group.
Definition group.h:70
GroupPool _group_pool
Pool of groups.
bool IsAllGroupID(GroupID id_g)
Checks if a GroupID stands for all vehicles of a company.
Definition group.h:115
Money GetGroupProfitLastYearMinAge(CompanyID company, GroupID id_g, VehicleType type)
Get last year's profit of vehicles above minimum age for the group with GroupID id_g and its sub-grou...
CommandCost CmdAddSharedVehicleGroup(DoCommandFlags flags, GroupID id_g, VehicleType type)
Add all shared vehicles of all vehicles from a group.
CommandCost CmdSetGroupFlag(DoCommandFlags flags, GroupID group_id, GroupFlag flag, bool value, bool recursive)
(Un)set group flag from a group
std::tuple< CommandCost, GroupID > CmdCreateGroup(DoCommandFlags flags, VehicleType vt, GroupID parent_group)
Create a new vehicle group.
static void SetGroupFlag(Group *g, GroupFlag flag, bool set, bool children)
Set group flag for a group and its sub-groups.
void UpdateCompanyGroupLiveries(const Company *c)
Update group liveries for a company.
CommandCost CmdAlterGroup(DoCommandFlags flags, AlterGroupMode mode, GroupID group_id, GroupID parent_id, const std::string &text)
Alter a group.
void SetTrainGroupID(Train *v, GroupID new_g)
Affect the groupID of a train to new_g.
void UpdateGroupChildren()
Update children list for each group.
Definition group_cmd.cpp:48
CommandCost CmdSetGroupLivery(DoCommandFlags flags, GroupID group_id, bool primary, Colours colour)
Set the livery for a vehicle group.
CommandCost CmdRemoveAllVehiclesGroup(DoCommandFlags flags, GroupID group_id)
Remove all vehicles from a group.
void UpdateTrainGroupID(Train *v)
Recalculates the groupID of a train.
CommandCost CmdDeleteGroup(DoCommandFlags flags, GroupID group_id)
Add all vehicles in the given group to the default group and then deletes the group.
uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e)
Get the number of engines with EngineID id_e in the group with GroupID id_g and its sub-groups.
static void UpdateNumEngineGroup(const Vehicle *v, GroupID old_g, GroupID new_g)
Update the num engines of a groupID.
static void UpdateAutoreplaceStatus(const EngineRenewList &erl, CompanyID company, GroupID group)
Update group autoreplace status flags for a group.
std::tuple< CommandCost, GroupID > CmdAddVehicleGroup(DoCommandFlags flags, GroupID group_id, VehicleID veh_id, bool add_shared, const VehicleListIdentifier &vli)
Add a vehicle to a group.
static void AddVehicleToGroup(Vehicle *v, GroupID new_g)
Do add a vehicle to a group.
static void PropagateChildLivery(const Group *g, bool reset_cache)
Propagate a livery change to a group's children, and optionally update cached vehicle colourmaps.
uint GetGroupNumVehicle(CompanyID company, GroupID id_g, VehicleType type)
Get the number of vehicles in the group with GroupID id_g and its sub-groups.
uint GetGroupNumVehicleMinAge(CompanyID company, GroupID id_g, VehicleType type)
Get the number of vehicles above profit minimum age in the group with GroupID id_g and its sub-groups...
bool GroupIsInGroup(GroupID search, GroupID group)
Test if GroupID group is a descendant of (or is) GroupID search.
Command definitions related to engine groups.
AlterGroupMode
Action for CmdAlterGroup.
Definition group_cmd.h:23
@ Rename
Change group name.
Definition group_cmd.h:24
@ SetParent
Change group parent.
Definition group_cmd.h:25
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition gfx.cpp:1548
static constexpr GroupID DEFAULT_GROUP
Ungrouped vehicles are in this group.
Definition group_type.h:18
static constexpr GroupID ALL_GROUP
All vehicles are in this group.
Definition group_type.h:17
static constexpr GroupID NEW_GROUP
Sentinel for a to-be-created group.
Definition group_type.h:16
static const uint MAX_LENGTH_GROUP_NAME_CHARS
The maximum length of a group name in characters including '\0'.
Definition group_type.h:20
@ Default
Default scheme.
Definition livery.h:24
Functions related to order backups.
Some methods of Pool are placed here in order to reduce compilation time and binary size.
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
size_t Utf8StringLength(std::string_view str)
Get the length of an UTF-8 encoded string in number of characters and thus not the number of bytes th...
Definition string.cpp:350
Functions related to low-level strings.
VehicleType type
Type of vehicle.
CompanySettings settings
settings specific for each company
EngineRenewList all_engine_renew_list
Engine renewals of this company for all vehicles.
EngineRenewList ungrouped_engine_renew_list
Engine renewals of this company for ungrouped vehicles.
bool renew_keep_length
sell some wagons if after autoreplace the train is longer than before
VehicleTypeIndexArray< GroupStatistics > group_all
NOSAVE: Statistics for the ALL_GROUP group.
VehicleTypeIndexArray< GroupStatistics > group_default
NOSAVE: Statistics for the DEFAULT_GROUP group.
Information about engine autorenews and autoreplacements.
bool IsFreeWagon() const
Check if the vehicle is a free wagon (got no engine in front of it).
Statistics and caches on the vehicles in a group.
Definition group.h:26
static void CountVehicle(const Vehicle *v, int delta)
Update num_vehicle when adding or removing a vehicle.
static void AddProfitLastYear(const Vehicle *v)
Add a vehicle's last year profit to the profit sum of its group.
static void VehicleReachedMinAge(const Vehicle *v)
Add a vehicle to the profit sum of its group.
uint16_t GetNumEngines(EngineID engine) const
Get number of vehicles of a specific engine ID.
Definition group_cmd.cpp:69
Money profit_last_year
Sum of profits for all vehicles.
Definition group.h:27
static GroupStatistics & GetAllGroup(const Vehicle *v)
Returns the GroupStatistic for the ALL_GROUP of a vehicle type.
static void UpdateAfterLoad()
Update all caches after loading a game, changing NewGRF, etc.
static void CountEngine(const Vehicle *v, int delta)
Update num_engines when adding/removing an engine.
uint16_t num_vehicle
Number of vehicles.
Definition group.h:30
Money profit_last_year_min_age
Sum of profits for vehicles considered for profit statistics.
Definition group.h:28
static void UpdateAutoreplace(CompanyID company)
Update autoreplace_defined and autoreplace_finished of all statistics of a company.
static void UpdateProfits()
Recompute the profits for all groups.
void Clear()
Clear all caches.
Definition group_cmd.cpp:34
bool autoreplace_defined
Are any autoreplace rules set?
Definition group.h:32
static GroupStatistics & Get(CompanyID company, GroupID id_g, VehicleType type)
Returns the GroupStatistics for a specific group.
Definition group_cmd.cpp:83
bool autoreplace_finished
Have all autoreplacement finished?
Definition group.h:33
std::map< EngineID, uint16_t > num_engines
Caches the number of engines of each type the company owns.
Definition group.h:29
uint16_t num_vehicle_min_age
Number of vehicles considered for profit statistics;.
Definition group.h:31
Group data.
Definition group.h:77
GroupFlags flags
Group flags.
Definition group.h:82
Livery livery
Custom colour scheme for vehicles in this group.
Definition group.h:83
GroupID parent
Parent group.
Definition group.h:91
GroupStatistics statistics
NOSAVE: Statistics and caches on the vehicles in the group.
Definition group.h:86
VehicleType vehicle_type
Vehicle type of the group.
Definition group.h:80
std::string name
Group Name.
Definition group.h:78
uint16_t number
Per-company group number.
Definition group.h:92
Owner owner
Group Owner.
Definition group.h:79
FlatSet< GroupID > children
NOSAVE: child groups belonging to this group.
Definition group.h:88
Information about a particular livery.
Definition livery.h:82
Flags in_use
Livery flags.
Definition livery.h:92
Colours colour2
Second colour, for vehicles with 2CC support.
Definition livery.h:94
Colours colour1
First colour, for all vehicles.
Definition livery.h:93
@ Primary
Primary colour is set.
Definition livery.h:85
@ Secondary
Secondary colour is set.
Definition livery.h:86
static void ClearGroup(GroupID group)
Clear the group of all backups having this group ID.
static Pool::IterateWrapper< Group > Iterate(size_t from=0)
static T * Create(Targs &&... args)
static Group * Get(auto index)
static bool CanAllocateItem(size_t n=1)
static bool IsValidID(auto index)
static Group * GetIfValid(auto index)
T * Next() const
Get next vehicle in the chain.
static Train * From(Vehicle *v)
'Train' is either a loco or a wagon.
Definition train.h:97
The information about a vehicle list.
Definition vehiclelist.h:32
WindowNumber ToWindowNumber() const
Pack a VehicleListIdentifier in 32 bits so it can be used as unique WindowNumber.
Vehicle data structure.
EngineID engine_type
The type of engine used for this vehicle.
TimerGameEconomy::Date economy_age
Age in economy days.
GroupID group_id
Index of group Pool array.
bool IsEngineCountable() const
Check if a vehicle is counted in num_engines in each company struct.
Definition vehicle.cpp:719
Vehicle * Next() const
Get the next vehicle of this vehicle.
Vehicle * NextShared() const
Get the next vehicle of the shared vehicle chain.
Money GetDisplayProfitLastYear() const
Gets the profit vehicle had last year.
bool IsFrontEngine() const
Check if the vehicle is a front engine.
Vehicle * FirstShared() const
Get the first vehicle of this vehicle chain.
TileIndex tile
Current tile index.
Owner owner
Which company owns the vehicle?
Base for the train class.
Functions related to vehicles.
static const TimerGameEconomy::Date VEHICLE_PROFIT_MIN_AGE
Only vehicles older than this have a meaningful profit.
bool IsCompanyBuildableVehicleType(VehicleType type)
Is the given vehicle type buildable by a company?
Functions related to the vehicle's GUIs.
WindowClass GetWindowClassForVehicleType(VehicleType vt)
Get WindowClass for vehicle list of given vehicle type.
Definition vehicle_gui.h:96
PoolID< uint32_t, struct VehicleIDTag, 0xFF000, 0xFFFFF > VehicleID
The type all our vehicle IDs have.
VehicleType
Available vehicle types.
@ Ship
Ship vehicle type.
@ CompanyEnd
Last company-ownable type.
@ Aircraft
Aircraft vehicle type.
@ Road
Road vehicle type.
@ Train
Train vehicle type.
bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli)
Generate a list of vehicles based on window type.
Functions and type for generating vehicle lists.
std::vector< const Vehicle * > VehicleList
A list of vehicles.
Definition vehiclelist.h:68
@ Group
Index is the group.
Definition vehiclelist.h:27
void CloseWindowById(WindowClass cls, WindowNumber number, bool force, int data)
Close a window by its class and window number (if it is open).
Definition window.cpp:1242
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
Definition window.cpp:3377
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting).
Definition window.cpp:3255
void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
Mark window data of all windows of a given class as invalid (in need of re-computing) Note that by de...
Definition window.cpp:3395