OpenTTD Source 20261112-master-gbcbec5e63f
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 "train.h"
13#include "vehiclelist.h"
14#include "vehicle_func.h"
15#include "vehicle_gui.h"
16#include "autoreplace_base.h"
17#include "autoreplace_func.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
243/* static */ void GroupStatistics::UpdateAutoreplace(CompanyID company)
244{
245 /* Set up the engine count for all companies */
246 Company *c = Company::Get(company);
248 c->group_all[type].ClearAutoreplace();
249 c->group_default[type].ClearAutoreplace();
250 }
251
252 /* Recalculate */
253 for (Group *g : Group::Iterate()) {
254 if (g->owner != company) continue;
255 g->statistics.ClearAutoreplace();
256 }
257
258 for (EngineRenewList erl = c->engine_renew_list; erl != nullptr; erl = erl->next) {
259 const Engine *e = Engine::Get(erl->from);
260 GroupStatistics &stats = GroupStatistics::Get(company, erl->group_id, e->type);
261 if (!stats.autoreplace_defined) {
262 stats.autoreplace_defined = true;
263 stats.autoreplace_finished = true;
264 }
265 if (GetGroupNumEngines(company, erl->group_id, erl->from) > 0) stats.autoreplace_finished = false;
266 }
267}
268
276static inline void UpdateNumEngineGroup(const Vehicle *v, GroupID old_g, GroupID new_g)
277{
278 if (old_g != new_g) {
279 /* Decrease the num engines in the old group */
281
282 /* Increase the num engines in the new group */
284 }
285}
286
287
288const Livery *GetParentLivery(const Group *g)
289{
290 if (g->parent == GroupID::Invalid()) {
291 const Company *c = Company::Get(g->owner);
292 return &c->livery[LiveryScheme::Default];
293 }
294
295 const Group *pg = Group::Get(g->parent);
296 return &pg->livery;
297}
298
299
305static void PropagateChildLivery(const Group *g, bool reset_cache)
306{
307 if (reset_cache) {
308 /* Company colour data is indirectly cached. */
309 for (Vehicle *v : Vehicle::Iterate()) {
310 if (v->group_id == g->index && (!v->IsGroundVehicle() || v->IsFrontEngine())) {
311 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
312 u->colourmap = PAL_NONE;
313 u->InvalidateNewGRFCache();
314 }
315 }
316 }
317 }
318
319 for (const GroupID &childgroup : g->children) {
320 Group *cg = Group::Get(childgroup);
323 PropagateChildLivery(cg, reset_cache);
324 }
325}
326
333{
334 for (Group *g : Group::Iterate()) {
335 if (g->owner == c->index && g->parent == GroupID::Invalid()) {
338 PropagateChildLivery(g, false);
339 }
340 }
341}
342
343
351std::tuple<CommandCost, GroupID> CmdCreateGroup(DoCommandFlags flags, VehicleType vt, GroupID parent_group)
352{
353 if (!IsCompanyBuildableVehicleType(vt)) return { CMD_ERROR, GroupID::Invalid() };
354
355 if (!Group::CanAllocateItem()) return { CMD_ERROR, GroupID::Invalid() };
356
357 Group *pg = Group::GetIfValid(parent_group);
358 if (pg != nullptr) {
359 if (pg->owner != _current_company) return { CMD_ERROR, GroupID::Invalid() };
360 if (pg->vehicle_type != vt) return { CMD_ERROR, GroupID::Invalid() };
361 }
362
363 if (flags.Test(DoCommandFlag::Execute)) {
365
366 Company *c = Company::Get(g->owner);
367 g->number = c->freegroups.UseID(c->freegroups.NextID());
368 if (pg == nullptr) {
369 g->livery.colour1 = c->livery[LiveryScheme::Default].colour1;
370 g->livery.colour2 = c->livery[LiveryScheme::Default].colour2;
372 } else {
373 g->parent = pg->index;
374 g->livery.colour1 = pg->livery.colour1;
375 g->livery.colour2 = pg->livery.colour2;
376 g->flags = pg->flags;
377 pg->children.insert(g->index);
378 }
379
381 InvalidateWindowData(WindowClass::CompanyLivery, g->owner, g->vehicle_type);
382
383 return { CommandCost(), g->index };
384 }
385
386 return { CommandCost(), GroupID::Invalid()};
387}
388
389
397{
398 Group *g = Group::GetIfValid(group_id);
399 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
400
401 /* Remove all vehicles from the group */
402 Command<Commands::RemoveAllVehiclesGroup>::Do(flags, group_id);
403
404 /* Delete sub-groups, using a copy to avoid invalid iteration. */
405 FlatSet<GroupID> children = g->children;
406 for (const GroupID &childgroup : children) {
407 Command<Commands::DeleteGroup>::Do(flags, childgroup);
408 }
409
410 if (flags.Test(DoCommandFlag::Execute)) {
411 /* Update backupped orders if needed */
412 OrderBackup::ClearGroup(g->index);
413
414 if (g->owner < MAX_COMPANIES) {
415 Company *c = Company::Get(g->owner);
416
417 /* If we set an autoreplace for the group we delete, remove it. */
418 for (const EngineRenew *er : EngineRenew::Iterate()) {
419 if (er->group_id == g->index) RemoveEngineReplacementForCompany(c, er->from, g->index, flags);
420 }
421
422 c->freegroups.ReleaseID(g->number);
423 }
424
425 if (g->parent != GroupID::Invalid()) {
426 Group *pg = Group::Get(g->parent);
427 pg->children.erase(g->index);
428 }
429
431
432 /* Delete the Replace Vehicle Windows */
433 CloseWindowById(WindowClass::ReplaceVehicle, g->vehicle_type);
434 delete g;
435
437 InvalidateWindowData(WindowClass::CompanyLivery, _current_company, vt);
438 }
439
440 return CommandCost();
441}
442
452CommandCost CmdAlterGroup(DoCommandFlags flags, AlterGroupMode mode, GroupID group_id, GroupID parent_id, const std::string &text)
453{
454 Group *g = Group::GetIfValid(group_id);
455 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
456
457 if (mode == AlterGroupMode::Rename) {
458 /* Rename group */
459 bool reset = text.empty();
460
461 if (!reset) {
463 }
464
465 if (flags.Test(DoCommandFlag::Execute)) {
466 /* Assign the new one */
467 if (reset) {
468 g->name.clear();
469 } else {
470 g->name = text;
471 }
472 }
473 } else if (mode == AlterGroupMode::SetParent) {
474 /* Do nothing if the parent group isn't actually changed. */
475 if (g->parent == parent_id) return CommandCost();
476
477 /* Set group parent */
478 const Group *pg = Group::GetIfValid(parent_id);
479
480 if (pg != nullptr) {
481 if (pg->owner != _current_company) return CMD_ERROR;
482 if (pg->vehicle_type != g->vehicle_type) return CMD_ERROR;
483
484 /* Ensure request parent isn't child of group.
485 * This is the only place that infinite loops are prevented. */
486 if (GroupIsInGroup(pg->index, g->index)) return CommandCost(STR_ERROR_GROUP_CAN_T_SET_PARENT_RECURSION);
487 }
488
489 if (flags.Test(DoCommandFlag::Execute)) {
490 if (g->parent != GroupID::Invalid()) Group::Get(g->parent)->children.erase(g->index);
491 g->parent = (pg == nullptr) ? GroupID::Invalid() : pg->index;
492 if (g->parent != GroupID::Invalid()) Group::Get(g->parent)->children.insert(g->index);
493
495
496 if (!g->livery.in_use.All({Livery::Flag::Primary, Livery::Flag::Secondary})) {
497 /* Update livery with new parent's colours if either colour is default. */
498 const Livery *livery = GetParentLivery(g);
501
502 PropagateChildLivery(g, true);
504 }
505 }
506 } else {
507 return CMD_ERROR;
508 }
509
510 if (flags.Test(DoCommandFlag::Execute)) {
511 InvalidateWindowData(WindowClass::ReplaceVehicle, g->vehicle_type, 1);
513 InvalidateWindowData(WindowClass::CompanyLivery, g->owner, g->vehicle_type);
514 InvalidateWindowClassesData(WindowClass::VehicleView);
515 InvalidateWindowClassesData(WindowClass::VehicleDetails);
516 }
517
518 return CommandCost();
519}
520
521
527static void AddVehicleToGroup(Vehicle *v, GroupID new_g)
528{
530
531 switch (v->type) {
532 default: NOT_REACHED();
534 SetTrainGroupID(Train::From(v), new_g);
535 break;
536
540 if (v->IsEngineCountable()) UpdateNumEngineGroup(v, v->group_id, new_g);
541 v->group_id = new_g;
542 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
543 u->colourmap = PAL_NONE;
544 u->InvalidateNewGRFCache();
545 u->UpdateViewport(true);
546 }
547 break;
548 }
549
550 InvalidateWindowData(WindowClass::VehicleView, v->index);
551 InvalidateWindowData(WindowClass::VehicleDetails, v->index);
552
554}
555
565std::tuple<CommandCost, GroupID> CmdAddVehicleGroup(DoCommandFlags flags, GroupID group_id, VehicleID veh_id, bool add_shared, const VehicleListIdentifier &vli)
566{
567 GroupID new_g = group_id;
568 if (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g) && new_g != NEW_GROUP) return { CMD_ERROR, GroupID::Invalid() };
569
570 VehicleList list;
571 if (veh_id == VehicleID::Invalid() && vli.Valid()) {
572 if (!GenerateVehicleSortList(&list, vli) || list.empty()) return { CMD_ERROR, GroupID::Invalid() };
573 } else {
574 const Vehicle *v = Vehicle::GetIfValid(veh_id);
575 if (v == nullptr || !IsCompanyBuildableVehicleType(v)) return { CMD_ERROR, GroupID::Invalid() };
576 list.push_back(v);
577 }
578
579 VehicleType vtype = list.front()->type;
580 for (const Vehicle *v : list) {
581 if (v->owner != _current_company || !v->IsPrimaryVehicle()) return { CMD_ERROR, GroupID::Invalid() };
582 }
583
584 if (Group::IsValidID(new_g)) {
585 const Group *g = Group::Get(new_g);
586 if (g->owner != _current_company || g->vehicle_type != vtype) return { CMD_ERROR, GroupID::Invalid() };
587 }
588
589 if (new_g == NEW_GROUP) {
590 /* Create new group. */
591 auto [ret, new_group_id] = CmdCreateGroup(flags, vtype, GroupID::Invalid());
592 if (ret.Failed()) return { ret, new_group_id };
593
594 new_g = new_group_id;
595 }
596
597 if (flags.Test(DoCommandFlag::Execute)) {
598 for (const Vehicle *vc : list) {
599 /* VehicleList is const but we need to modify the vehicle. */
600 Vehicle *v = Vehicle::Get(vc->index);
601 AddVehicleToGroup(v, new_g);
602
603 if (add_shared) {
604 /* Add vehicles in the shared order list as well. */
605 for (Vehicle *v2 = v->FirstShared(); v2 != nullptr; v2 = v2->NextShared()) {
606 if (v2->group_id != new_g) AddVehicleToGroup(v2, new_g);
607 }
608 }
609
610 SetWindowDirty(WindowClass::VehicleDepot, v->tile);
611 }
612
614
615 /* Update the Replace Vehicle Windows */
616 SetWindowDirty(WindowClass::ReplaceVehicle, vtype);
618 }
619
620 return { CommandCost(), new_g };
621}
622
631{
632 if (!Group::IsValidID(id_g) || !IsCompanyBuildableVehicleType(type)) return CMD_ERROR;
633
634 if (flags.Test(DoCommandFlag::Execute)) {
635 /* Find the first front engine which belong to the group id_g
636 * then add all shared vehicles of this front engine to the group id_g */
637 for (const Vehicle *v : Vehicle::Iterate()) {
638 if (v->type == type && v->IsPrimaryVehicle()) {
639 if (v->group_id != id_g) continue;
640
641 /* For each shared vehicles add it to the group */
642 for (Vehicle *v2 = v->FirstShared(); v2 != nullptr; v2 = v2->NextShared()) {
643 if (v2->group_id != id_g) Command<Commands::AddVehicleToGroup>::Do(flags, id_g, v2->index, false, VehicleListIdentifier{});
644 }
645 }
646 }
647
649 }
650
651 return CommandCost();
652}
653
654
662{
663 const Group *g = Group::GetIfValid(group_id);
664
665 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
666
667 if (flags.Test(DoCommandFlag::Execute)) {
668 /* Find each Vehicle that belongs to the group old_g and add it to the default group */
669 for (const Vehicle *v : Vehicle::Iterate()) {
670 if (v->IsPrimaryVehicle()) {
671 if (v->group_id != group_id) continue;
672
673 /* Add The Vehicle to the default group */
674 Command<Commands::AddVehicleToGroup>::Do(flags, DEFAULT_GROUP, v->index, false, VehicleListIdentifier{});
675 }
676 }
677
679 }
680
681 return CommandCost();
682}
683
692CommandCost CmdSetGroupLivery(DoCommandFlags flags, GroupID group_id, bool primary, Colours colour)
693{
694 Group *g = Group::GetIfValid(group_id);
695
696 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
697
698 if (colour >= Colours::End && colour != Colours::Invalid) return CMD_ERROR;
699
700 if (flags.Test(DoCommandFlag::Execute)) {
701 if (primary) {
703 if (colour == Colours::Invalid) colour = GetParentLivery(g)->colour1;
704 g->livery.colour1 = colour;
705 } else {
707 if (colour == Colours::Invalid) colour = GetParentLivery(g)->colour2;
708 g->livery.colour2 = colour;
709 }
710
711 PropagateChildLivery(g, true);
713 }
714
715 return CommandCost();
716}
717
725static void SetGroupFlag(Group *g, GroupFlag flag, bool set, bool children)
726{
727 if (set) {
728 g->flags.Set(flag);
729 } else {
730 g->flags.Reset(flag);
731 }
732
733 if (!children) return;
734
735 for (const GroupID &childgroup : g->children) {
736 SetGroupFlag(Group::Get(childgroup), flag, set, true);
737 }
738}
739
749CommandCost CmdSetGroupFlag(DoCommandFlags flags, GroupID group_id, GroupFlag flag, bool value, bool recursive)
750{
751 Group *g = Group::GetIfValid(group_id);
752 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
753
755
756 if (flags.Test(DoCommandFlag::Execute)) {
757 SetGroupFlag(g, flag, value, recursive);
758
760 InvalidateWindowData(WindowClass::ReplaceVehicle, g->vehicle_type);
761 }
762
763 return CommandCost();
764}
765
772void SetTrainGroupID(Train *v, GroupID new_g)
773{
774 if (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g)) return;
775
776 assert(v->IsFrontEngine() || IsDefaultGroupID(new_g));
777
778 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
779 if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
780
781 u->group_id = new_g;
782 u->colourmap = PAL_NONE;
783 u->InvalidateNewGRFCache();
784 u->UpdateViewport(true);
785 }
786
787 /* Update the Replace Vehicle Windows */
789 SetWindowDirty(WindowClass::ReplaceVehicle, VehicleType::Train);
790}
791
792
801{
802 assert(v->IsFrontEngine() || v->IsFreeWagon());
803
804 GroupID new_g = v->IsFrontEngine() ? v->group_id : (GroupID)DEFAULT_GROUP;
805 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
806 if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
807
808 u->group_id = new_g;
809 u->colourmap = PAL_NONE;
810 u->InvalidateNewGRFCache();
811 }
812
813 /* Update the Replace Vehicle Windows */
815 SetWindowDirty(WindowClass::ReplaceVehicle, VehicleType::Train);
816}
817
826uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e)
827{
828 uint count = 0;
829
830 if (const Group *g = Group::GetIfValid(id_g); g != nullptr) {
831 for (const GroupID &childgroup : g->children) {
832 count += GetGroupNumEngines(company, childgroup, id_e);
833 }
834 }
835
836 return count + GroupStatistics::Get(company, id_g, Engine::Get(id_e)->type).GetNumEngines(id_e);
837}
838
847uint GetGroupNumVehicle(CompanyID company, GroupID id_g, VehicleType type)
848{
849 uint count = 0;
850
851 if (const Group *g = Group::GetIfValid(id_g); g != nullptr) {
852 for (const GroupID &childgroup : g->children) {
853 count += GetGroupNumVehicle(company, childgroup, type);
854 }
855 }
856
857 return count + GroupStatistics::Get(company, id_g, type).num_vehicle;
858}
859
868uint GetGroupNumVehicleMinAge(CompanyID company, GroupID id_g, VehicleType type)
869{
870 uint count = 0;
871
872 if (const Group *g = Group::GetIfValid(id_g); g != nullptr) {
873 for (const GroupID &childgroup : g->children) {
874 count += GetGroupNumVehicleMinAge(company, childgroup, type);
875 }
876 }
877
878 return count + GroupStatistics::Get(company, id_g, type).num_vehicle_min_age;
879}
880
889Money GetGroupProfitLastYearMinAge(CompanyID company, GroupID id_g, VehicleType type)
890{
891 Money sum = 0;
892
893 if (const Group *g = Group::GetIfValid(id_g); g != nullptr) {
894 for (const GroupID &childgroup : g->children) {
895 sum += GetGroupProfitLastYearMinAge(company, childgroup, type);
896 }
897 }
898
899 return sum + GroupStatistics::Get(company, id_g, type).profit_last_year_min_age;
900}
901
902void RemoveAllGroupsForCompany(const CompanyID company)
903{
904 for (Group *g : Group::Iterate()) {
905 if (company == g->owner) delete g;
906 }
907}
908
909
916bool GroupIsInGroup(GroupID search, GroupID group)
917{
918 if (!Group::IsValidID(search)) return search == group;
919
920 do {
921 if (search == group) return true;
922 search = Group::Get(search)->parent;
923 } while (search != GroupID::Invalid());
924
925 return false;
926}
Base class for autoreplaces/autorenews.
Functions related to autoreplacing.
CommandCost RemoveEngineReplacementForCompany(Company *c, EngineID engine, GroupID group, DoCommandFlags flags)
Remove an engine replacement for the company.
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:1890
void ReleaseID(UnitID index)
Release a unit number.
Definition vehicle.cpp:1907
UnitID NextID() const
Find first unused unit number.
Definition vehicle.cpp:1875
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.
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
GroupFlag
Configuration flags for a group.
Definition group.h:67
@ ReplaceProtection
If set, the global autoreplace has no effect on the group.
Definition group.h:68
@ ReplaceWagonRemoval
If set, autoreplace will perform wagon removal on vehicles in this group.
Definition group.h:69
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.
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:112
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.
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 engine_renew_list
Engine renewals of this company.
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.
Struct to store engine replacements.
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:25
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:26
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:29
Money profit_last_year_min_age
Sum of profits for vehicles considered for profit statistics.
Definition group.h:27
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:31
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:32
std::map< EngineID, uint16_t > num_engines
Caches the number of engines of each type the company owns.
Definition group.h:28
uint16_t num_vehicle_min_age
Number of vehicles considered for profit statistics;.
Definition group.h:30
Group data.
Definition group.h:76
GroupFlags flags
Group flags.
Definition group.h:81
Livery livery
Custom colour scheme for vehicles in this group.
Definition group.h:82
GroupID parent
Parent group.
Definition group.h:88
GroupStatistics statistics
NOSAVE: Statistics and caches on the vehicles in the group.
Definition group.h:83
VehicleType vehicle_type
Vehicle type of the group.
Definition group.h:79
std::string name
Group Name.
Definition group.h:77
uint16_t number
Per-company group number.
Definition group.h:89
Owner owner
Group Owner.
Definition group.h:78
FlatSet< GroupID > children
NOSAVE: child groups belonging to this group.
Definition group.h:85
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:717
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