OpenTTD Source 20250312-master-gcdcc6b491d
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 <http://www.gnu.org/licenses/>.
6 */
7
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"
22
23#include "table/strings.h"
24
25#include "safeguards.h"
26
27GroupPool _group_pool("Group");
29
30
33void GroupStatistics::Clear()
34{
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;
39
40 /* This is also called when NewGRF change. So the number of engines might have changed. Reset. */
41 this->num_engines.clear();
42}
43
50{
51 auto found = this->num_engines.find(engine);
52 if (found != std::end(this->num_engines)) return found->second;
53 return 0;
54}
55
64{
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;
70 }
71
72 if (IsDefaultGroupID(id_g)) return Company::Get(company)->group_default[type];
73 if (IsAllGroupID(id_g)) return Company::Get(company)->group_all[type];
74
75 NOT_REACHED();
76}
77
84{
85 return GroupStatistics::Get(v->owner, v->group_id, v->type);
86}
87
97
102{
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();
108 }
109 }
110
111 /* Recalculate */
112 for (Group *g : Group::Iterate()) {
113 g->statistics.Clear();
114 }
115
116 for (const Vehicle *v : Vehicle::Iterate()) {
117 if (!v->IsEngineCountable()) continue;
118
120 if (v->IsPrimaryVehicle()) GroupStatistics::CountVehicle(v, 1);
121 }
122
123 for (const Company *c : Company::Iterate()) {
125 }
126}
127
133/* static */ void GroupStatistics::CountVehicle(const Vehicle *v, int delta)
134{
135 assert(delta == 1 || delta == -1);
136
139
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;
144
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;
150 }
151}
152
158/* static */ void GroupStatistics::CountEngine(const Vehicle *v, int delta)
159{
160 assert(delta == 1 || delta == -1);
163}
164
176
190
195{
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();
201 }
202 }
203
204 /* Recalculate */
205 for (Group *g : Group::Iterate()) {
206 g->statistics.ClearProfits();
207 }
208
209 for (const Vehicle *v : Vehicle::Iterate()) {
210 if (v->IsPrimaryVehicle()) {
213 }
214 }
215}
216
222{
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();
228 }
229
230 /* Recalculate */
231 for (Group *g : Group::Iterate()) {
232 if (g->owner != company) continue;
233 g->statistics.ClearAutoreplace();
234 }
235
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;
242 }
243 if (GetGroupNumEngines(company, erl->group_id, erl->from) > 0) stats.autoreplace_finished = false;
244 }
245}
246
254static inline void UpdateNumEngineGroup(const Vehicle *v, GroupID old_g, GroupID new_g)
255{
256 if (old_g != new_g) {
257 /* Decrease the num engines in the old group */
259
260 /* Increase the num engines in the new group */
262 }
263}
264
265
266const Livery *GetParentLivery(const Group *g)
267{
268 if (g->parent == GroupID::Invalid()) {
269 const Company *c = Company::Get(g->owner);
270 return &c->livery[LS_DEFAULT];
271 }
272
273 const Group *pg = Group::Get(g->parent);
274 return &pg->livery;
275}
276
277
283static void PropagateChildLivery(const Group *g, bool reset_cache)
284{
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();
292 }
293 }
294 }
295 }
296
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);
302 }
303 }
304}
305
312{
313 for (Group *g : Group::Iterate()) {
314 if (g->owner == c->index && g->parent == GroupID::Invalid()) {
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);
318 }
319 }
320}
321
322
330std::tuple<CommandCost, GroupID> CmdCreateGroup(DoCommandFlags flags, VehicleType vt, GroupID parent_group)
331{
332 if (!IsCompanyBuildableVehicleType(vt)) return { CMD_ERROR, GroupID::Invalid() };
333
334 if (!Group::CanAllocateItem()) return { CMD_ERROR, GroupID::Invalid() };
335
336 const Group *pg = Group::GetIfValid(parent_group);
337 if (pg != nullptr) {
338 if (pg->owner != _current_company) return { CMD_ERROR, GroupID::Invalid() };
339 if (pg->vehicle_type != vt) return { CMD_ERROR, GroupID::Invalid() };
340 }
341
342 if (flags.Test(DoCommandFlag::Execute)) {
343 Group *g = new Group(_current_company, vt);
344
345 Company *c = Company::Get(g->owner);
346 g->number = c->freegroups.UseID(c->freegroups.NextID());
347 if (pg == nullptr) {
348 g->livery.colour1 = c->livery[LS_DEFAULT].colour1;
349 g->livery.colour2 = c->livery[LS_DEFAULT].colour2;
351 } else {
352 g->parent = pg->index;
353 g->livery.colour1 = pg->livery.colour1;
354 g->livery.colour2 = pg->livery.colour2;
355 g->flags = pg->flags;
356 }
357
360
361 return { CommandCost(), g->index };
362 }
363
364 return { CommandCost(), GroupID::Invalid()};
365}
366
367
375{
376 Group *g = Group::GetIfValid(group_id);
377 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
378
379 /* Remove all vehicles from the group */
381
382 /* Delete sub-groups */
383 for (const Group *gp : Group::Iterate()) {
384 if (gp->parent == g->index) {
385 Command<CMD_DELETE_GROUP>::Do(flags, gp->index);
386 }
387 }
388
389 if (flags.Test(DoCommandFlag::Execute)) {
390 /* Update backupped orders if needed */
392
393 if (g->owner < MAX_COMPANIES) {
394 Company *c = Company::Get(g->owner);
395
396 /* If we set an autoreplace for the group we delete, remove it. */
397 for (EngineRenew *er : EngineRenew::Iterate()) {
398 if (er->group_id == g->index) RemoveEngineReplacementForCompany(c, er->from, g->index, flags);
399 }
400
401 c->freegroups.ReleaseID(g->number);
402 }
403
405
406 /* Delete the Replace Vehicle Windows */
408 delete g;
409
412 }
413
414 return CommandCost();
415}
416
426CommandCost CmdAlterGroup(DoCommandFlags flags, AlterGroupMode mode, GroupID group_id, GroupID parent_id, const std::string &text)
427{
428 Group *g = Group::GetIfValid(group_id);
429 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
430
431 if (mode == AlterGroupMode::Rename) {
432 /* Rename group */
433 bool reset = text.empty();
434
435 if (!reset) {
437 }
438
439 if (flags.Test(DoCommandFlag::Execute)) {
440 /* Assign the new one */
441 if (reset) {
442 g->name.clear();
443 } else {
444 g->name = text;
445 }
446 }
447 } else if (mode == AlterGroupMode::SetParent) {
448 /* Set group parent */
449 const Group *pg = Group::GetIfValid(parent_id);
450
451 if (pg != nullptr) {
452 if (pg->owner != _current_company) return CMD_ERROR;
453 if (pg->vehicle_type != g->vehicle_type) return CMD_ERROR;
454
455 /* Ensure request parent isn't child of group.
456 * This is the only place that infinite loops are prevented. */
457 if (GroupIsInGroup(pg->index, g->index)) return CommandCost(STR_ERROR_GROUP_CAN_T_SET_PARENT_RECURSION);
458 }
459
460 if (flags.Test(DoCommandFlag::Execute)) {
461 g->parent = (pg == nullptr) ? GroupID::Invalid() : pg->index;
463
464 if (!HasBit(g->livery.in_use, 0) || !HasBit(g->livery.in_use, 1)) {
465 /* Update livery with new parent's colours if either colour is default. */
466 const Livery *livery = GetParentLivery(g);
467 if (!HasBit(g->livery.in_use, 0)) g->livery.colour1 = livery->colour1;
468 if (!HasBit(g->livery.in_use, 1)) g->livery.colour2 = livery->colour2;
469
470 PropagateChildLivery(g, true);
472 }
473 }
474 } else {
475 return CMD_ERROR;
476 }
477
478 if (flags.Test(DoCommandFlag::Execute)) {
484 }
485
486 return CommandCost();
487}
488
489
495static void AddVehicleToGroup(Vehicle *v, GroupID new_g)
496{
498
499 switch (v->type) {
500 default: NOT_REACHED();
501 case VEH_TRAIN:
502 SetTrainGroupID(Train::From(v), new_g);
503 break;
504
505 case VEH_ROAD:
506 case VEH_SHIP:
507 case VEH_AIRCRAFT:
508 if (v->IsEngineCountable()) UpdateNumEngineGroup(v, v->group_id, new_g);
509 v->group_id = new_g;
510 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
511 u->colourmap = PAL_NONE;
512 u->InvalidateNewGRFCache();
513 u->UpdateViewport(true);
514 }
515 break;
516 }
517
519}
520
529std::tuple<CommandCost, GroupID> CmdAddVehicleGroup(DoCommandFlags flags, GroupID group_id, VehicleID veh_id, bool add_shared, const VehicleListIdentifier &vli)
530{
531 GroupID new_g = group_id;
532 if (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g) && new_g != NEW_GROUP) return { CMD_ERROR, GroupID::Invalid() };
533
534 VehicleList list;
535 if (veh_id == VehicleID::Invalid() && vli.Valid()) {
536 if (!GenerateVehicleSortList(&list, vli) || list.empty()) return { CMD_ERROR, GroupID::Invalid() };
537 } else {
538 Vehicle *v = Vehicle::GetIfValid(veh_id);
539 if (v == nullptr) return { CMD_ERROR, GroupID::Invalid() };
540 list.push_back(v);
541 }
542
543 VehicleType vtype = list.front()->type;
544 for (const Vehicle *v : list) {
545 if (v->owner != _current_company || !v->IsPrimaryVehicle()) return { CMD_ERROR, GroupID::Invalid() };
546 }
547
548 if (Group::IsValidID(new_g)) {
549 Group *g = Group::Get(new_g);
550 if (g->owner != _current_company || g->vehicle_type != vtype) return { CMD_ERROR, GroupID::Invalid() };
551 }
552
553 if (new_g == NEW_GROUP) {
554 /* Create new group. */
555 auto [ret, new_group_id] = CmdCreateGroup(flags, vtype, GroupID::Invalid());
556 if (ret.Failed()) return { ret, new_group_id };
557
558 new_g = new_group_id;
559 }
560
561 if (flags.Test(DoCommandFlag::Execute)) {
562 for (const Vehicle *vc : list) {
563 /* VehicleList is const but we need to modify the vehicle. */
564 Vehicle *v = Vehicle::Get(vc->index);
565 AddVehicleToGroup(v, new_g);
566
567 if (add_shared) {
568 /* Add vehicles in the shared order list as well. */
569 for (Vehicle *v2 = v->FirstShared(); v2 != nullptr; v2 = v2->NextShared()) {
570 if (v2->group_id != new_g) AddVehicleToGroup(v2, new_g);
571 }
572 }
573
579 }
580
582
583 /* Update the Replace Vehicle Windows */
586 }
587
588 return { CommandCost(), new_g };
589}
590
599{
600 if (!Group::IsValidID(id_g) || !IsCompanyBuildableVehicleType(type)) return CMD_ERROR;
601
602 if (flags.Test(DoCommandFlag::Execute)) {
603 /* Find the first front engine which belong to the group id_g
604 * then add all shared vehicles of this front engine to the group id_g */
605 for (const Vehicle *v : Vehicle::Iterate()) {
606 if (v->type == type && v->IsPrimaryVehicle()) {
607 if (v->group_id != id_g) continue;
608
609 /* For each shared vehicles add it to the group */
610 for (Vehicle *v2 = v->FirstShared(); v2 != nullptr; v2 = v2->NextShared()) {
611 if (v2->group_id != id_g) Command<CMD_ADD_VEHICLE_GROUP>::Do(flags, id_g, v2->index, false, VehicleListIdentifier{});
612 }
613 }
614 }
615
617 }
618
619 return CommandCost();
620}
621
622
630{
631 Group *g = Group::GetIfValid(group_id);
632
633 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
634
635 if (flags.Test(DoCommandFlag::Execute)) {
636 /* Find each Vehicle that belongs to the group old_g and add it to the default group */
637 for (const Vehicle *v : Vehicle::Iterate()) {
638 if (v->IsPrimaryVehicle()) {
639 if (v->group_id != group_id) continue;
640
641 /* Add The Vehicle to the default group */
643 }
644 }
645
647 }
648
649 return CommandCost();
650}
651
659CommandCost CmdSetGroupLivery(DoCommandFlags flags, GroupID group_id, bool primary, Colours colour)
660{
661 Group *g = Group::GetIfValid(group_id);
662
663 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
664
665 if (colour >= COLOUR_END && colour != INVALID_COLOUR) return CMD_ERROR;
666
667 if (flags.Test(DoCommandFlag::Execute)) {
668 if (primary) {
669 AssignBit(g->livery.in_use, 0, colour != INVALID_COLOUR);
670 if (colour == INVALID_COLOUR) colour = GetParentLivery(g)->colour1;
671 g->livery.colour1 = colour;
672 } else {
673 AssignBit(g->livery.in_use, 1, colour != INVALID_COLOUR);
674 if (colour == INVALID_COLOUR) colour = GetParentLivery(g)->colour2;
675 g->livery.colour2 = colour;
676 }
677
678 PropagateChildLivery(g, true);
680 }
681
682 return CommandCost();
683}
684
690static void SetGroupFlag(Group *g, GroupFlag flag, bool set, bool children)
691{
692 if (set) {
693 g->flags.Set(flag);
694 } else {
695 g->flags.Reset(flag);
696 }
697
698 if (!children) return;
699
700 for (Group *pg : Group::Iterate()) {
701 if (pg->parent == g->index) SetGroupFlag(pg, flag, set, true);
702 }
703}
704
714CommandCost CmdSetGroupFlag(DoCommandFlags flags, GroupID group_id, GroupFlag flag, bool value, bool recursive)
715{
716 Group *g = Group::GetIfValid(group_id);
717 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
718
720
721 if (flags.Test(DoCommandFlag::Execute)) {
722 SetGroupFlag(g, flag, value, recursive);
723
726 }
727
728 return CommandCost();
729}
730
738{
739 if (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g)) return;
740
741 assert(v->IsFrontEngine() || IsDefaultGroupID(new_g));
742
743 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
744 if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
745
746 u->group_id = new_g;
747 u->colourmap = PAL_NONE;
748 u->InvalidateNewGRFCache();
749 u->UpdateViewport(true);
750 }
751
752 /* Update the Replace Vehicle Windows */
755}
756
757
766{
767 assert(v->IsFrontEngine() || v->IsFreeWagon());
768
770 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
771 if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
772
773 u->group_id = new_g;
774 u->colourmap = PAL_NONE;
775 u->InvalidateNewGRFCache();
776 }
777
778 /* Update the Replace Vehicle Windows */
781}
782
792{
793 uint count = 0;
794 const Engine *e = Engine::Get(id_e);
795 for (const Group *g : Group::Iterate()) {
796 if (g->parent == id_g) count += GetGroupNumEngines(company, g->index, id_e);
797 }
798 return count + GroupStatistics::Get(company, id_g, e->type).GetNumEngines(id_e);
799}
800
810{
811 uint count = 0;
812 for (const Group *g : Group::Iterate()) {
813 if (g->parent == id_g) count += GetGroupNumVehicle(company, g->index, type);
814 }
815 return count + GroupStatistics::Get(company, id_g, type).num_vehicle;
816}
817
827{
828 uint count = 0;
829 for (const Group *g : Group::Iterate()) {
830 if (g->parent == id_g) count += GetGroupNumVehicleMinAge(company, g->index, type);
831 }
832 return count + GroupStatistics::Get(company, id_g, type).num_vehicle_min_age;
833}
834
844{
845 Money sum = 0;
846 for (const Group *g : Group::Iterate()) {
847 if (g->parent == id_g) sum += GetGroupProfitLastYearMinAge(company, g->index, type);
848 }
849 return sum + GroupStatistics::Get(company, id_g, type).profit_last_year_min_age;
850}
851
852void RemoveAllGroupsForCompany(const CompanyID company)
853{
854 for (Group *g : Group::Iterate()) {
855 if (company == g->owner) delete g;
856 }
857}
858
859
866bool GroupIsInGroup(GroupID search, GroupID group)
867{
868 if (!Group::IsValidID(search)) return search == group;
869
870 do {
871 if (search == group) return true;
872 search = Group::Get(search)->parent;
873 } while (search != GroupID::Invalid());
874
875 return false;
876}
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.
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T AssignBit(T &x, const uint8_t y, bool value)
Assigns a bit in a variable.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Timpl & Set()
Set all bits.
constexpr Timpl & Reset(Tvalue_type value)
Reset the value-th bit.
Common return value for all commands.
Enum-as-bit-set wrapper.
UnitID UseID(UnitID index)
Use a unit number.
Definition vehicle.cpp:1854
void ReleaseID(UnitID index)
Release a unit number.
Definition vehicle.cpp:1871
UnitID NextID() const
Find first unused unit number.
Definition vehicle.cpp:1839
Functions related to commands.
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
@ Execute
execute the given command
CompanyID _current_company
Company currently doing an action.
Functions related to companies.
GroupFlag
Definition group.h:65
@ ReplaceProtection
If set, the global autoreplace has no effect on the group.
@ ReplaceWagonRemoval
If set, autoreplace will perform wagon removal on vehicles in this 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.
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:101
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.
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.
@ SetParent
Change group parent.
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition gfx.cpp:1500
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
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(const char *s)
Get the length of an UTF-8 encoded string in number of characters and thus not the number of bytes th...
Definition string.cpp:377
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
std::array< GroupStatistics, VEH_COMPANY_END > group_default
NOSAVE: Statistics for the DEFAULT_GROUP group.
std::array< GroupStatistics, VEH_COMPANY_END > group_all
NOSAVE: Statistics for the ALL_GROUP group.
Struct to store engine replacements.
VehicleType type
Vehicle type, ie VEH_ROAD, VEH_TRAIN, etc.
Definition engine_base.h:61
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:24
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:49
Money profit_last_year
Sum of profits for all vehicles.
Definition group.h:25
static GroupStatistics & GetAllGroup(const Vehicle *v)
Returns the GroupStatistic for the ALL_GROUPO of a vehicle type.
Definition group_cmd.cpp:93
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:28
Money profit_last_year_min_age
Sum of profits for vehicles considered for profit statistics.
Definition group.h:26
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.
bool autoreplace_defined
Are any autoreplace rules set?
Definition group.h:30
static GroupStatistics & Get(CompanyID company, GroupID id_g, VehicleType type)
Returns the GroupStatistics for a specific group.
Definition group_cmd.cpp:63
bool autoreplace_finished
Have all autoreplacement finished?
Definition group.h:31
std::map< EngineID, uint16_t > num_engines
Caches the number of engines of each type the company owns.
Definition group.h:27
uint16_t num_vehicle_min_age
Number of vehicles considered for profit statistics;.
Definition group.h:29
Group data.
Definition group.h:72
GroupFlags flags
Group flags.
Definition group.h:77
Livery livery
Custom colour scheme for vehicles in this group.
Definition group.h:78
GroupID parent
Parent group.
Definition group.h:83
GroupStatistics statistics
NOSAVE: Statistics and caches on the vehicles in the group.
Definition group.h:79
VehicleType vehicle_type
Vehicle type of the group.
Definition group.h:75
std::string name
Group Name.
Definition group.h:73
uint16_t number
Per-company group number.
Definition group.h:84
Owner owner
Group Owner.
Definition group.h:74
Information about a particular livery.
Definition livery.h:78
Colours colour2
Second colour, for vehicles with 2CC support.
Definition livery.h:81
Colours colour1
First colour, for all vehicles.
Definition livery.h:80
uint8_t in_use
Bit 0 set if this livery should override the default livery first colour, Bit 1 for the second colour...
Definition livery.h:79
static void ClearGroup(GroupID group)
Clear the group of all backups having this group ID.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
static Titem * Get(auto index)
Returns Titem with given index.
Tindex index
Index of this pool item.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
static bool IsValidID(auto index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
static Titem * GetIfValid(auto index)
Returns Titem with given index.
Base class for all pools.
T * Next() const
Get next vehicle in the chain.
static T * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
'Train' is either a loco or a wagon.
Definition train.h:90
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:711
debug_inline bool IsFrontEngine() const
Check if the vehicle is a front engine.
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.
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?
WindowClass GetWindowClassForVehicleType(VehicleType vt)
Get WindowClass for vehicle list of given vehicle type.
Definition vehicle_gui.h:97
VehicleType
Available vehicle types.
@ VEH_ROAD
Road vehicle type.
@ VEH_AIRCRAFT
Aircraft vehicle type.
@ VEH_SHIP
Ship vehicle type.
@ VEH_TRAIN
Train vehicle type.
@ VEH_COMPANY_END
Last company-ownable 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
@ VL_GROUP_LIST
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:1143
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:3224
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition window.cpp:3106
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:3241
@ WC_COMPANY_COLOUR
Company colour selection; Window numbers:
@ WC_VEHICLE_DEPOT
Depot view; Window numbers:
@ WC_REPLACE_VEHICLE
Replace vehicle window; Window numbers:
@ WC_VEHICLE_DETAILS
Vehicle details; Window numbers:
@ WC_VEHICLE_VIEW
Vehicle view; Window numbers: