OpenTTD Source: src/group_cmd.cpp Source File

OpenTTD Source 13.2.1

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
27 GroupPool _group_pool("Group");
28 INSTANTIATE_POOL_METHODS(Group)
29
30 GroupStatistics::GroupStatistics()
31 {
32 this->num_engines = CallocT<uint16>(Engine::GetPoolSize());
33 }
34
35 GroupStatistics::~GroupStatistics()
36 {
37 free(this->num_engines);
38 }
39
43 void GroupStatistics::Clear()
44 {
45 this->num_vehicle = 0;
46 this->profit_last_year = 0;
47 this->num_vehicle_min_age = 0;
48 this->profit_last_year_min_age = 0;
49
50 /* This is also called when NewGRF change. So the number of engines might have changed. Reallocate. */
51 free(this->num_engines);
52 this->num_engines = CallocT<uint16>(Engine::GetPoolSize());
53 }
54
62 /* static */ GroupStatistics &GroupStatistics::Get(CompanyID company, GroupID id_g, VehicleType type)
63 {
64 if (Group::IsValidID(id_g)) {
65 Group *g = Group::Get(id_g);
66 assert(g->owner == company);
67 assert(g->vehicle_type == type);
68 return g->statistics;
69 }
70
71 if (IsDefaultGroupID(id_g)) return Company::Get(company)->group_default[type];
72 if (IsAllGroupID(id_g)) return Company::Get(company)->group_all[type];
73
74 NOT_REACHED();
75 }
76
82 /* static */ GroupStatistics &GroupStatistics::Get(const Vehicle *v)
83 {
84 return GroupStatistics::Get(v->owner, v->group_id, v->type);
85 }
86
92 /* static */ GroupStatistics &GroupStatistics::GetAllGroup(const Vehicle *v)
93 {
94 return GroupStatistics::Get(v->owner, ALL_GROUP, v->type);
95 }
96
100 /* static */ void GroupStatistics::UpdateAfterLoad()
101 {
102 /* Set up the engine count for all companies */
103 for (Company *c : Company::Iterate()) {
104 for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
105 c->group_all[type].Clear();
106 c->group_default[type].Clear();
107 }
108 }
109
110 /* Recalculate */
111 for (Group *g : Group::Iterate()) {
112 g->statistics.Clear();
113 }
114
115 for (const Vehicle *v : Vehicle::Iterate()) {
116 if (!v->IsEngineCountable()) continue;
117
118 GroupStatistics::CountEngine(v, 1);
119 if (v->IsPrimaryVehicle()) GroupStatistics::CountVehicle(v, 1);
120 }
121
122 for (const Company *c : Company::Iterate()) {
123 GroupStatistics::UpdateAutoreplace(c->index);
124 }
125 }
126
132 /* static */ void GroupStatistics::CountVehicle(const Vehicle *v, int delta)
133 {
134 assert(delta == 1 || delta == -1);
135
136 GroupStatistics &stats_all = GroupStatistics::GetAllGroup(v);
137 GroupStatistics &stats = GroupStatistics::Get(v);
138
139 stats_all.num_vehicle += delta;
140 stats_all.profit_last_year += v->GetDisplayProfitLastYear() * delta;
141 stats.num_vehicle += delta;
142 stats.profit_last_year += v->GetDisplayProfitLastYear() * delta;
143
144 if (v->age > VEHICLE_PROFIT_MIN_AGE) {
145 stats_all.num_vehicle_min_age += delta;
146 stats_all.profit_last_year_min_age += v->GetDisplayProfitLastYear() * delta;
147 stats.num_vehicle_min_age += delta;
148 stats.profit_last_year_min_age += v->GetDisplayProfitLastYear() * delta;
149 }
150 }
151
157 /* static */ void GroupStatistics::CountEngine(const Vehicle *v, int delta)
158 {
159 assert(delta == 1 || delta == -1);
160 GroupStatistics::GetAllGroup(v).num_engines[v->engine_type] += delta;
161 GroupStatistics::Get(v).num_engines[v->engine_type] += delta;
162 }
163
167 /* static */ void GroupStatistics::AddProfitLastYear(const Vehicle *v)
168 {
169 GroupStatistics &stats_all = GroupStatistics::GetAllGroup(v);
170 GroupStatistics &stats = GroupStatistics::Get(v);
171
172 stats_all.profit_last_year += v->GetDisplayProfitLastYear();
173 stats.profit_last_year += v->GetDisplayProfitLastYear();
174 }
175
179 /* static */ void GroupStatistics::VehicleReachedMinAge(const Vehicle *v)
180 {
181 GroupStatistics &stats_all = GroupStatistics::GetAllGroup(v);
182 GroupStatistics &stats = GroupStatistics::Get(v);
183
184 stats_all.num_vehicle_min_age++;
185 stats_all.profit_last_year_min_age += v->GetDisplayProfitLastYear();
186 stats.num_vehicle_min_age++;
187 stats.profit_last_year_min_age += v->GetDisplayProfitLastYear();
188 }
189
193 /* static */ void GroupStatistics::UpdateProfits()
194 {
195 /* Set up the engine count for all companies */
196 for (Company *c : Company::Iterate()) {
197 for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
198 c->group_all[type].ClearProfits();
199 c->group_default[type].ClearProfits();
200 }
201 }
202
203 /* Recalculate */
204 for (Group *g : Group::Iterate()) {
205 g->statistics.ClearProfits();
206 }
207
208 for (const Vehicle *v : Vehicle::Iterate()) {
209 if (v->IsPrimaryVehicle()) {
210 GroupStatistics::AddProfitLastYear(v);
211 if (v->age > VEHICLE_PROFIT_MIN_AGE) GroupStatistics::VehicleReachedMinAge(v);
212 }
213 }
214 }
215
220 /* static */ void GroupStatistics::UpdateAutoreplace(CompanyID company)
221 {
222 /* Set up the engine count for all companies */
223 Company *c = Company::Get(company);
224 for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
225 c->group_all[type].ClearAutoreplace();
226 c->group_default[type].ClearAutoreplace();
227 }
228
229 /* Recalculate */
230 for (Group *g : Group::Iterate()) {
231 if (g->owner != company) continue;
232 g->statistics.ClearAutoreplace();
233 }
234
235 for (EngineRenewList erl = c->engine_renew_list; erl != nullptr; erl = erl->next) {
236 const Engine *e = Engine::Get(erl->from);
237 GroupStatistics &stats = GroupStatistics::Get(company, erl->group_id, e->type);
238 if (!stats.autoreplace_defined) {
239 stats.autoreplace_defined = true;
240 stats.autoreplace_finished = true;
241 }
242 if (GetGroupNumEngines(company, erl->group_id, erl->from) > 0) stats.autoreplace_finished = false;
243 }
244 }
245
253 static inline void UpdateNumEngineGroup(const Vehicle *v, GroupID old_g, GroupID new_g)
254 {
255 if (old_g != new_g) {
256 /* Decrease the num engines in the old group */
257 GroupStatistics::Get(v->owner, old_g, v->type).num_engines[v->engine_type]--;
258
259 /* Increase the num engines in the new group */
260 GroupStatistics::Get(v->owner, new_g, v->type).num_engines[v->engine_type]++;
261 }
262 }
263
264
265 const Livery *GetParentLivery(const Group *g)
266 {
267 if (g->parent == INVALID_GROUP) {
268 const Company *c = Company::Get(g->owner);
269 return &c->livery[LS_DEFAULT];
270 }
271
272 const Group *pg = Group::Get(g->parent);
273 return &pg->livery;
274 }
275
276
281 void PropagateChildLivery(const Group *g)
282 {
283 /* Company colour data is indirectly cached. */
284 for (Vehicle *v : Vehicle::Iterate()) {
285 if (v->group_id == g->index && (!v->IsGroundVehicle() || v->IsFrontEngine())) {
286 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
287 u->colourmap = PAL_NONE;
288 u->InvalidateNewGRFCache();
289 }
290 }
291 }
292
293 for (Group *cg : Group::Iterate()) {
294 if (cg->parent == g->index) {
295 if (!HasBit(cg->livery.in_use, 0)) cg->livery.colour1 = g->livery.colour1;
296 if (!HasBit(cg->livery.in_use, 1)) cg->livery.colour2 = g->livery.colour2;
297 PropagateChildLivery(cg);
298 }
299 }
300 }
301
302
303 Group::Group(Owner owner)
304 {
305 this->owner = owner;
306 this->folded = false;
307 }
308
309
317 std::tuple<CommandCost, GroupID> CmdCreateGroup(DoCommandFlag flags, VehicleType vt, GroupID parent_group)
318 {
319 if (!IsCompanyBuildableVehicleType(vt)) return { CMD_ERROR, INVALID_GROUP };
320
321 if (!Group::CanAllocateItem()) return { CMD_ERROR, INVALID_GROUP };
322
323 const Group *pg = Group::GetIfValid(parent_group);
324 if (pg != nullptr) {
325 if (pg->owner != _current_company) return { CMD_ERROR, INVALID_GROUP };
326 if (pg->vehicle_type != vt) return { CMD_ERROR, INVALID_GROUP };
327 }
328
329 if (flags & DC_EXEC) {
330 Group *g = new Group(_current_company);
331 g->vehicle_type = vt;
332 g->parent = INVALID_GROUP;
333
334 if (pg == nullptr) {
335 const Company *c = Company::Get(_current_company);
336 g->livery.colour1 = c->livery[LS_DEFAULT].colour1;
337 g->livery.colour2 = c->livery[LS_DEFAULT].colour2;
338 if (c->settings.renew_keep_length) SetBit(g->flags, GroupFlags::GF_REPLACE_WAGON_REMOVAL);
339 } else {
340 g->parent = pg->index;
341 g->livery.colour1 = pg->livery.colour1;
342 g->livery.colour2 = pg->livery.colour2;
343 g->flags = pg->flags;
344 }
345
346 InvalidateWindowData(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_GROUP_LIST, vt, _current_company).Pack());
347 InvalidateWindowData(WC_COMPANY_COLOUR, g->owner, g->vehicle_type);
348
349 return { CommandCost(), g->index };
350 }
351
352 return { CommandCost(), INVALID_GROUP};
353 }
354
355
362 CommandCost CmdDeleteGroup(DoCommandFlag flags, GroupID group_id)
363 {
364 Group *g = Group::GetIfValid(group_id);
365 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
366
367 /* Remove all vehicles from the group */
368 Command<CMD_REMOVE_ALL_VEHICLES_GROUP>::Do(flags, group_id);
369
370 /* Delete sub-groups */
371 for (const Group *gp : Group::Iterate()) {
372 if (gp->parent == g->index) {
373 Command<CMD_DELETE_GROUP>::Do(flags, gp->index);
374 }
375 }
376
377 if (flags & DC_EXEC) {
378 /* Update backupped orders if needed */
379 OrderBackup::ClearGroup(g->index);
380
381 /* If we set an autoreplace for the group we delete, remove it. */
382 if (_current_company < MAX_COMPANIES) {
383 Company *c;
384
385 c = Company::Get(_current_company);
386 for (EngineRenew *er : EngineRenew::Iterate()) {
387 if (er->group_id == g->index) RemoveEngineReplacementForCompany(c, er->from, g->index, flags);
388 }
389 }
390
391 VehicleType vt = g->vehicle_type;
392
393 /* Delete the Replace Vehicle Windows */
394 CloseWindowById(WC_REPLACE_VEHICLE, g->vehicle_type);
395 delete g;
396
397 InvalidateWindowData(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_GROUP_LIST, vt, _current_company).Pack());
398 InvalidateWindowData(WC_COMPANY_COLOUR, _current_company, vt);
399 }
400
401 return CommandCost();
402 }
403
413 CommandCost CmdAlterGroup(DoCommandFlag flags, AlterGroupMode mode, GroupID group_id, GroupID parent_id, const std::string &text)
414 {
415 Group *g = Group::GetIfValid(group_id);
416 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
417
418 if (mode == AlterGroupMode::Rename) {
419 /* Rename group */
420 bool reset = text.empty();
421
422 if (!reset) {
423 if (Utf8StringLength(text) >= MAX_LENGTH_GROUP_NAME_CHARS) return CMD_ERROR;
424 }
425
426 if (flags & DC_EXEC) {
427 /* Assign the new one */
428 if (reset) {
429 g->name.clear();
430 } else {
431 g->name = text;
432 }
433 }
434 } else if (mode == AlterGroupMode::SetParent) {
435 /* Set group parent */
436 const Group *pg = Group::GetIfValid(parent_id);
437
438 if (pg != nullptr) {
439 if (pg->owner != _current_company) return CMD_ERROR;
440 if (pg->vehicle_type != g->vehicle_type) return CMD_ERROR;
441
442 /* Ensure request parent isn't child of group.
443 * This is the only place that infinite loops are prevented. */
444 if (GroupIsInGroup(pg->index, g->index)) return_cmd_error(STR_ERROR_GROUP_CAN_T_SET_PARENT_RECURSION);
445 }
446
447 if (flags & DC_EXEC) {
448 g->parent = (pg == nullptr) ? INVALID_GROUP : pg->index;
449 GroupStatistics::UpdateAutoreplace(g->owner);
450
451 if (g->livery.in_use == 0) {
452 const Livery *livery = GetParentLivery(g);
453 g->livery.colour1 = livery->colour1;
454 g->livery.colour2 = livery->colour2;
455
456 PropagateChildLivery(g);
457 MarkWholeScreenDirty();
458 }
459 }
460 } else {
461 return CMD_ERROR;
462 }
463
464 if (flags & DC_EXEC) {
465 InvalidateWindowData(WC_REPLACE_VEHICLE, g->vehicle_type, 1);
466 InvalidateWindowData(GetWindowClassForVehicleType(g->vehicle_type), VehicleListIdentifier(VL_GROUP_LIST, g->vehicle_type, _current_company).Pack());
467 InvalidateWindowData(WC_COMPANY_COLOUR, g->owner, g->vehicle_type);
468 InvalidateWindowClassesData(WC_VEHICLE_VIEW);
469 InvalidateWindowClassesData(WC_VEHICLE_DETAILS);
470 }
471
472 return CommandCost();
473 }
474
475
481 static void AddVehicleToGroup(Vehicle *v, GroupID new_g)
482 {
483 GroupStatistics::CountVehicle(v, -1);
484
485 switch (v->type) {
486 default: NOT_REACHED();
487 case VEH_TRAIN:
488 SetTrainGroupID(Train::From(v), new_g);
489 break;
490
491 case VEH_ROAD:
492 case VEH_SHIP:
493 case VEH_AIRCRAFT:
494 if (v->IsEngineCountable()) UpdateNumEngineGroup(v, v->group_id, new_g);
495 v->group_id = new_g;
496 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
497 u->colourmap = PAL_NONE;
498 u->InvalidateNewGRFCache();
499 u->UpdateViewport(true);
500 }
501 break;
502 }
503
504 GroupStatistics::CountVehicle(v, 1);
505 }
506
515 std::tuple<CommandCost, GroupID> CmdAddVehicleGroup(DoCommandFlag flags, GroupID group_id, VehicleID veh_id, bool add_shared)
516 {
517 Vehicle *v = Vehicle::GetIfValid(veh_id);
518 GroupID new_g = group_id;
519
520 if (v == nullptr || (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g) && new_g != NEW_GROUP)) return { CMD_ERROR, INVALID_GROUP };
521
522 if (Group::IsValidID(new_g)) {
523 Group *g = Group::Get(new_g);
524 if (g->owner != _current_company || g->vehicle_type != v->type) return { CMD_ERROR, INVALID_GROUP };
525 }
526
527 if (v->owner != _current_company || !v->IsPrimaryVehicle()) return { CMD_ERROR, INVALID_GROUP };
528
529 if (new_g == NEW_GROUP) {
530 /* Create new group. */
531 auto [ret, group_id] = CmdCreateGroup(flags, v->type, INVALID_GROUP);
532 if (ret.Failed()) return { ret, group_id };
533
534 new_g = group_id;
535 }
536
537 if (flags & DC_EXEC) {
538 AddVehicleToGroup(v, new_g);
539
540 if (add_shared) {
541 /* Add vehicles in the shared order list as well. */
542 for (Vehicle *v2 = v->FirstShared(); v2 != nullptr; v2 = v2->NextShared()) {
543 if (v2->group_id != new_g) AddVehicleToGroup(v2, new_g);
544 }
545 }
546
547 GroupStatistics::UpdateAutoreplace(v->owner);
548
549 /* Update the Replace Vehicle Windows */
550 SetWindowDirty(WC_REPLACE_VEHICLE, v->type);
551 SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
552 SetWindowDirty(WC_VEHICLE_VIEW, v->index);
553 SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
554 InvalidateWindowData(GetWindowClassForVehicleType(v->type), VehicleListIdentifier(VL_GROUP_LIST, v->type, _current_company).Pack());
555 InvalidateWindowData(WC_VEHICLE_VIEW, v->index);
556 InvalidateWindowData(WC_VEHICLE_DETAILS, v->index);
557 }
558
559 return { CommandCost(), new_g };
560 }
561
569 CommandCost CmdAddSharedVehicleGroup(DoCommandFlag flags, GroupID id_g, VehicleType type)
570 {
571 if (!Group::IsValidID(id_g) || !IsCompanyBuildableVehicleType(type)) return CMD_ERROR;
572
573 if (flags & DC_EXEC) {
574 /* Find the first front engine which belong to the group id_g
575 * then add all shared vehicles of this front engine to the group id_g */
576 for (const Vehicle *v : Vehicle::Iterate()) {
577 if (v->type == type && v->IsPrimaryVehicle()) {
578 if (v->group_id != id_g) continue;
579
580 /* For each shared vehicles add it to the group */
581 for (Vehicle *v2 = v->FirstShared(); v2 != nullptr; v2 = v2->NextShared()) {
582 if (v2->group_id != id_g) Command<CMD_ADD_VEHICLE_GROUP>::Do(flags, id_g, v2->index, false);
583 }
584 }
585 }
586
587 InvalidateWindowData(GetWindowClassForVehicleType(type), VehicleListIdentifier(VL_GROUP_LIST, type, _current_company).Pack());
588 }
589
590 return CommandCost();
591 }
592
593
600 CommandCost CmdRemoveAllVehiclesGroup(DoCommandFlag flags, GroupID group_id)
601 {
602 Group *g = Group::GetIfValid(group_id);
603
604 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
605
606 if (flags & DC_EXEC) {
607 /* Find each Vehicle that belongs to the group old_g and add it to the default group */
608 for (const Vehicle *v : Vehicle::Iterate()) {
609 if (v->IsPrimaryVehicle()) {
610 if (v->group_id != group_id) continue;
611
612 /* Add The Vehicle to the default group */
613 Command<CMD_ADD_VEHICLE_GROUP>::Do(flags, DEFAULT_GROUP, v->index, false);
614 }
615 }
616
617 InvalidateWindowData(GetWindowClassForVehicleType(g->vehicle_type), VehicleListIdentifier(VL_GROUP_LIST, g->vehicle_type, _current_company).Pack());
618 }
619
620 return CommandCost();
621 }
622
630 CommandCost CmdSetGroupLivery(DoCommandFlag flags, GroupID group_id, bool primary, Colours colour)
631 {
632 Group *g = Group::GetIfValid(group_id);
633
634 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
635
636 if (colour >= COLOUR_END && colour != INVALID_COLOUR) return CMD_ERROR;
637
638 if (flags & DC_EXEC) {
639 if (primary) {
640 SB(g->livery.in_use, 0, 1, colour != INVALID_COLOUR);
641 if (colour == INVALID_COLOUR) colour = (Colours)GetParentLivery(g)->colour1;
642 g->livery.colour1 = colour;
643 } else {
644 SB(g->livery.in_use, 1, 1, colour != INVALID_COLOUR);
645 if (colour == INVALID_COLOUR) colour = (Colours)GetParentLivery(g)->colour2;
646 g->livery.colour2 = colour;
647 }
648
649 PropagateChildLivery(g);
650 MarkWholeScreenDirty();
651 }
652
653 return CommandCost();
654 }
655
661 static void SetGroupFlag(Group *g, GroupFlags flag, bool set, bool children)
662 {
663 if (set) {
664 SetBit(g->flags, flag);
665 } else {
666 ClrBit(g->flags, flag);
667 }
668
669 if (!children) return;
670
671 for (Group *pg : Group::Iterate()) {
672 if (pg->parent == g->index) SetGroupFlag(pg, flag, set, true);
673 }
674 }
675
685 CommandCost CmdSetGroupFlag(DoCommandFlag flags, GroupID group_id, GroupFlags flag, bool value, bool recursive)
686 {
687 Group *g = Group::GetIfValid(group_id);
688 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
689
690 if (flag >= GroupFlags::GF_END) return CMD_ERROR;
691
692 if (flags & DC_EXEC) {
693 SetGroupFlag(g, flag, value, recursive);
694
695 SetWindowDirty(GetWindowClassForVehicleType(g->vehicle_type), VehicleListIdentifier(VL_GROUP_LIST, g->vehicle_type, _current_company).Pack());
696 InvalidateWindowData(WC_REPLACE_VEHICLE, g->vehicle_type);
697 }
698
699 return CommandCost();
700 }
701
707 void RemoveVehicleFromGroup(const Vehicle *v)
708 {
709 if (!v->IsPrimaryVehicle()) return;
710
711 if (!IsDefaultGroupID(v->group_id)) GroupStatistics::CountVehicle(v, -1);
712 }
713
714
721 void SetTrainGroupID(Train *v, GroupID new_g)
722 {
723 if (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g)) return;
724
725 assert(v->IsFrontEngine() || IsDefaultGroupID(new_g));
726
727 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
728 if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
729
730 u->group_id = new_g;
731 u->colourmap = PAL_NONE;
732 u->InvalidateNewGRFCache();
733 u->UpdateViewport(true);
734 }
735
736 /* Update the Replace Vehicle Windows */
737 GroupStatistics::UpdateAutoreplace(v->owner);
738 SetWindowDirty(WC_REPLACE_VEHICLE, VEH_TRAIN);
739 }
740
741
749 void UpdateTrainGroupID(Train *v)
750 {
751 assert(v->IsFrontEngine() || v->IsFreeWagon());
752
753 GroupID new_g = v->IsFrontEngine() ? v->group_id : (GroupID)DEFAULT_GROUP;
754 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
755 if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
756
757 u->group_id = new_g;
758 u->colourmap = PAL_NONE;
759 u->InvalidateNewGRFCache();
760 }
761
762 /* Update the Replace Vehicle Windows */
763 GroupStatistics::UpdateAutoreplace(v->owner);
764 SetWindowDirty(WC_REPLACE_VEHICLE, VEH_TRAIN);
765 }
766
775 uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e)
776 {
777 uint count = 0;
778 const Engine *e = Engine::Get(id_e);
779 for (const Group *g : Group::Iterate()) {
780 if (g->parent == id_g) count += GetGroupNumEngines(company, g->index, id_e);
781 }
782 return count + GroupStatistics::Get(company, id_g, e->type).num_engines[id_e];
783 }
784
793 uint GetGroupNumVehicle(CompanyID company, GroupID id_g, VehicleType type)
794 {
795 uint count = 0;
796 for (const Group *g : Group::Iterate()) {
797 if (g->parent == id_g) count += GetGroupNumVehicle(company, g->index, type);
798 }
799 return count + GroupStatistics::Get(company, id_g, type).num_vehicle;
800 }
801
810 uint GetGroupNumVehicleMinAge(CompanyID company, GroupID id_g, VehicleType type)
811 {
812 uint count = 0;
813 for (const Group *g : Group::Iterate()) {
814 if (g->parent == id_g) count += GetGroupNumVehicleMinAge(company, g->index, type);
815 }
816 return count + GroupStatistics::Get(company, id_g, type).num_vehicle_min_age;
817 }
818
827 Money GetGroupProfitLastYearMinAge(CompanyID company, GroupID id_g, VehicleType type)
828 {
829 Money sum = 0;
830 for (const Group *g : Group::Iterate()) {
831 if (g->parent == id_g) sum += GetGroupProfitLastYearMinAge(company, g->index, type);
832 }
833 return sum + GroupStatistics::Get(company, id_g, type).profit_last_year_min_age;
834 }
835
836 void RemoveAllGroupsForCompany(const CompanyID company)
837 {
838 for (Group *g : Group::Iterate()) {
839 if (company == g->owner) delete g;
840 }
841 }
842
843
850 bool GroupIsInGroup(GroupID search, GroupID group)
851 {
852 if (!Group::IsValidID(search)) return search == group;
853
854 do {
855 if (search == group) return true;
856 search = Group::Get(search)->parent;
857 } while (search != INVALID_GROUP);
858
859 return false;
860 }
VEH_AIRCRAFT
@ VEH_AIRCRAFT
Aircraft vehicle type.
Definition: vehicle_type.h:27
CmdAddSharedVehicleGroup
CommandCost CmdAddSharedVehicleGroup(DoCommandFlag flags, GroupID id_g, VehicleType type)
Add all shared vehicles of all vehicles from a group.
Definition: group_cmd.cpp:569
IsCompanyBuildableVehicleType
static bool IsCompanyBuildableVehicleType(VehicleType type)
Is the given vehicle type buildable by a company?
Definition: vehicle_func.h:89
InvalidateWindowData
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:3254
autoreplace_base.h
Pool::PoolItem<&_group_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:337
SetWindowDirty
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3156
GroupStatistics::CountEngine
static void CountEngine(const Vehicle *v, int delta)
Update num_engines when adding/removing an engine.
Definition: group_cmd.cpp:157
Company::group_all
GroupStatistics group_all[VEH_COMPANY_END]
NOSAVE: Statistics for the ALL_GROUP group.
Definition: company_base.h:127
train.h
command_func.h
Pool::PoolItem<&_group_pool >::GetIfValid
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:348
CMD_ERROR
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:28
CmdCreateGroup
std::tuple< CommandCost, GroupID > CmdCreateGroup(DoCommandFlag flags, VehicleType vt, GroupID parent_group)
Create a new vehicle group.
Definition: group_cmd.cpp:317
CmdDeleteGroup
CommandCost CmdDeleteGroup(DoCommandFlag flags, GroupID group_id)
Add all vehicles in the given group to the default group and then deletes the group.
Definition: group_cmd.cpp:362
Vehicle::IsEngineCountable
bool IsEngineCountable() const
Check if a vehicle is counted in num_engines in each company struct.
Definition: vehicle.cpp:708
AlterGroupMode
AlterGroupMode
Action for CmdAlterGroup.
Definition: group_cmd.h:21
VehicleListIdentifier
The information about a vehicle list.
Definition: vehiclelist.h:29
Vehicle::Next
Vehicle * Next() const
Get the next vehicle of this vehicle.
Definition: vehicle_base.h:610
SpecializedVehicle::Next
T * Next() const
Get next vehicle in the chain.
Definition: vehicle_base.h:1098
WC_COMPANY_COLOUR
@ WC_COMPANY_COLOUR
Company colour selection; Window numbers:
Definition: window_type.h:223
vehiclelist.h
Group::parent
GroupID parent
Parent group.
Definition: group.h:85
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:235
Vehicle::group_id
GroupID group_id
Index of group Pool array.
Definition: vehicle_base.h:341
UpdateNumEngineGroup
static void UpdateNumEngineGroup(const Vehicle *v, GroupID old_g, GroupID new_g)
Update the num engines of a groupID.
Definition: group_cmd.cpp:253
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
GroupStatistics::CountVehicle
static void CountVehicle(const Vehicle *v, int delta)
Update num_vehicle when adding or removing a vehicle.
Definition: group_cmd.cpp:132
ClrBit
static T ClrBit(T &x, const uint8 y)
Clears a bit in a variable.
Definition: bitmath_func.hpp:151
GroupStatistics::VehicleReachedMinAge
static void VehicleReachedMinAge(const Vehicle *v)
Add a vehicle to the profit sum of its group.
Definition: group_cmd.cpp:179
GroupStatistics::autoreplace_finished
bool autoreplace_finished
Have all autoreplacement finished?
Definition: group.h:32
Group::livery
Livery livery
Custom colour scheme for vehicles in this group.
Definition: group.h:80
MAX_LENGTH_GROUP_NAME_CHARS
static const uint MAX_LENGTH_GROUP_NAME_CHARS
The maximum length of a group name in characters including '\0'.
Definition: group_type.h:20
Engine
Definition: engine_base.h:36
VEH_ROAD
@ VEH_ROAD
Road vehicle type.
Definition: vehicle_type.h:25
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:224
GroupStatistics::profit_last_year
Money profit_last_year
Sum of profits for all vehicles.
Definition: group.h:26
Vehicle::IsPrimaryVehicle
virtual bool IsPrimaryVehicle() const
Whether this is the primary vehicle in the chain.
Definition: vehicle_base.h:460
Vehicle::owner
Owner owner
Which company owns the vehicle?
Definition: vehicle_base.h:288
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
DC_EXEC
@ DC_EXEC
execute the given command
Definition: command_type.h:357
Group::vehicle_type
VehicleType vehicle_type
Vehicle type of the group.
Definition: group.h:77
NEW_GROUP
static const GroupID NEW_GROUP
Sentinel for a to-be-created group.
Definition: group_type.h:15
DoCommandFlag
DoCommandFlag
List of flags for a command.
Definition: command_type.h:355
GetGroupProfitLastYearMinAge
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...
Definition: group_cmd.cpp:827
GroupStatistics::num_vehicle
uint16 num_vehicle
Number of vehicles.
Definition: group.h:29
SetTrainGroupID
void SetTrainGroupID(Train *v, GroupID new_g)
Affect the groupID of a train to new_g.
Definition: group_cmd.cpp:721
UpdateTrainGroupID
void UpdateTrainGroupID(Train *v)
Recalculates the groupID of a train.
Definition: group_cmd.cpp:749
ALL_GROUP
static const GroupID ALL_GROUP
All vehicles are in this group.
Definition: group_type.h:16
GroupStatistics::num_vehicle_min_age
uint16 num_vehicle_min_age
Number of vehicles considered for profit statistics;.
Definition: group.h:30
Group
Group data.
Definition: group.h:74
CompanySettings::renew_keep_length
bool renew_keep_length
sell some wagons if after autoreplace the train is longer than before
Definition: settings_type.h:580
Utf8StringLength
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:435
AlterGroupMode::Rename
@ Rename
Change group name.
Pool::PoolItem<&_engine_pool >::GetPoolSize
static size_t GetPoolSize()
Returns first unused index.
Definition: pool_type.hpp:358
GroupStatistics
Statistics and caches on the vehicles in a group.
Definition: group.h:25
EngineID
uint16 EngineID
Unique identification number of an engine.
Definition: engine_type.h:21
return_cmd_error
#define return_cmd_error(errcode)
Returns from a function with a specific StringID as error.
Definition: command_func.h:38
INVALID_GROUP
static const GroupID INVALID_GROUP
Sentinel for invalid groups.
Definition: group_type.h:18
CommandCost
Common return value for all commands.
Definition: command_type.h:24
WC_VEHICLE_VIEW
@ WC_VEHICLE_VIEW
Vehicle view; Window numbers:
Definition: window_type.h:332
GroupFlags
GroupFlags
Definition: group.h:67
VEH_COMPANY_END
@ VEH_COMPANY_END
Last company-ownable type.
Definition: vehicle_type.h:29
Group::statistics
GroupStatistics statistics
NOSAVE: Statistics and caches on the vehicles in the group.
Definition: group.h:81
Company::group_default
GroupStatistics group_default[VEH_COMPANY_END]
NOSAVE: Statistics for the DEFAULT_GROUP group.
Definition: company_base.h:128
Vehicle::tile
TileIndex tile
Current tile index.
Definition: vehicle_base.h:245
Vehicle::engine_type
EngineID engine_type
The type of engine used for this vehicle.
Definition: vehicle_base.h:302
GetGroupNumVehicle
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.
Definition: group_cmd.cpp:793
SB
static T SB(T &x, const uint8 s, const uint8 n, const U d)
Set n bits in x starting at bit s to d.
Definition: bitmath_func.hpp:58
WC_REPLACE_VEHICLE
@ WC_REPLACE_VEHICLE
Replace vehicle window; Window numbers:
Definition: window_type.h:211
Group::owner
Owner owner
Group Owner.
Definition: group.h:76
VehicleListIdentifier::Pack
uint32 Pack() const
Pack a VehicleListIdentifier in a single uint32.
Definition: vehiclelist.cpp:21
Vehicle::IsFrontEngine
bool IsFrontEngine() const
Check if the vehicle is a front engine.
Definition: vehicle_base.h:913
Livery::in_use
byte 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
CompanyProperties::settings
CompanySettings settings
settings specific for each company
Definition: company_base.h:106
PropagateChildLivery
void PropagateChildLivery(const Group *g)
Propagate a livery change to a group's children.
Definition: group_cmd.cpp:281
WC_VEHICLE_DETAILS
@ WC_VEHICLE_DETAILS
Vehicle details; Window numbers:
Definition: window_type.h:193
GetGroupNumVehicleMinAge
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...
Definition: group_cmd.cpp:810
MAX_COMPANIES
@ MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:23
safeguards.h
GroupStatistics::UpdateAutoreplace
static void UpdateAutoreplace(CompanyID company)
Update autoreplace_defined and autoreplace_finished of all statistics of a company.
Definition: group_cmd.cpp:220
Train
'Train' is either a loco or a wagon.
Definition: train.h:89
GroupStatistics::Get
static GroupStatistics & Get(CompanyID company, GroupID id_g, VehicleType type)
Returns the GroupStatistics for a specific group.
Definition: group_cmd.cpp:62
DEFAULT_GROUP
static const GroupID DEFAULT_GROUP
Ungrouped vehicles are in this group.
Definition: group_type.h:17
GroupStatistics::num_engines
uint16 * num_engines
Caches the number of engines of each type the company owns.
Definition: group.h:28
GroupStatistics::GetAllGroup
static GroupStatistics & GetAllGroup(const Vehicle *v)
Returns the GroupStatistic for the ALL_GROUPO of a vehicle type.
Definition: group_cmd.cpp:92
GroupStatistics::profit_last_year_min_age
Money profit_last_year_min_age
Sum of profits for vehicles considered for profit statistics.
Definition: group.h:27
IsAllGroupID
static bool IsAllGroupID(GroupID id_g)
Checks if a GroupID stands for all vehicles of a company.
Definition: group.h:101
_group_pool
GroupPool _group_pool("Group")
Pool of groups.
Group::flags
uint8 flags
Group flags.
Definition: group.h:79
EngineRenew
Struct to store engine replacements.
Definition: autoreplace_base.h:33
stdafx.h
VehicleType
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
CmdRemoveAllVehiclesGroup
CommandCost CmdRemoveAllVehiclesGroup(DoCommandFlag flags, GroupID group_id)
Remove all vehicles from a group.
Definition: group_cmd.cpp:600
RemoveEngineReplacementForCompany
static CommandCost RemoveEngineReplacementForCompany(Company *c, EngineID engine, GroupID group, DoCommandFlag flags)
Remove an engine replacement for the company.
Definition: autoreplace_func.h:93
GroupStatistics::autoreplace_defined
bool autoreplace_defined
Are any autoreplace rules set?
Definition: group.h:31
group_cmd.h
CmdSetGroupFlag
CommandCost CmdSetGroupFlag(DoCommandFlag flags, GroupID group_id, GroupFlags flag, bool value, bool recursive)
(Un)set group flag from a group
Definition: group_cmd.cpp:685
GetWindowClassForVehicleType
static WindowClass GetWindowClassForVehicleType(VehicleType vt)
Get WindowClass for vehicle list of given vehicle type.
Definition: vehicle_gui.h:96
CmdSetGroupLivery
CommandCost CmdSetGroupLivery(DoCommandFlag flags, GroupID group_id, bool primary, Colours colour)
Set the livery for a vehicle group.
Definition: group_cmd.cpp:630
string_func.h
RemoveVehicleFromGroup
void RemoveVehicleFromGroup(const Vehicle *v)
Decrease the num_vehicle variable before delete an front engine from a group.
Definition: group_cmd.cpp:707
Vehicle::FirstShared
Vehicle * FirstShared() const
Get the first vehicle of this vehicle chain.
Definition: vehicle_base.h:704
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
vehicle_func.h
Pool::PoolItem<&_company_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:386
Pool
Base class for all pools.
Definition: pool_type.hpp:81
GroupID
uint16 GroupID
Type for all group identifiers.
Definition: group_type.h:13
GroupStatistics::UpdateProfits
static void UpdateProfits()
Recompute the profits for all groups.
Definition: group_cmd.cpp:193
SetGroupFlag
static void SetGroupFlag(Group *g, GroupFlags flag, bool set, bool children)
Set group flag for a group and its sub-groups.
Definition: group_cmd.cpp:661
SpecializedVehicle< Train, Type >::From
static Train * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
Definition: vehicle_base.h:1183
GetGroupNumEngines
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.
Definition: group_cmd.cpp:775
InvalidateWindowClassesData
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:3271
CmdAlterGroup
CommandCost CmdAlterGroup(DoCommandFlag flags, AlterGroupMode mode, GroupID group_id, GroupID parent_id, const std::string &text)
Alter a group.
Definition: group_cmd.cpp:413
Vehicle::NextShared
Vehicle * NextShared() const
Get the next vehicle of the shared vehicle chain.
Definition: vehicle_base.h:692
GroupIsInGroup
bool GroupIsInGroup(GroupID search, GroupID group)
Test if GroupID group is a descendant of (or is) GroupID search.
Definition: group_cmd.cpp:850
WC_VEHICLE_DEPOT
@ WC_VEHICLE_DEPOT
Depot view; Window numbers:
Definition: window_type.h:344
Vehicle::age
Date age
Age in days.
Definition: vehicle_base.h:273
Pool::PoolItem<&_group_pool >::CanAllocateItem
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Definition: pool_type.hpp:307
OrderBackup::ClearGroup
static void ClearGroup(GroupID group)
Clear the group of all backups having this group ID.
Definition: order_backup.cpp:218
Group::name
std::string name
Group Name.
Definition: group.h:75
company_func.h
CmdAddVehicleGroup
std::tuple< CommandCost, GroupID > CmdAddVehicleGroup(DoCommandFlag flags, GroupID group_id, VehicleID veh_id, bool add_shared)
Add a vehicle to a group.
Definition: group_cmd.cpp:515
INSTANTIATE_POOL_METHODS
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
Definition: pool_func.hpp:224
VehicleID
uint32 VehicleID
The type all our vehicle IDs have.
Definition: vehicle_type.h:16
CommandHelper
Definition: command_func.h:94
AlterGroupMode::SetParent
@ SetParent
Change group parent.
GroupStatistics::Clear
void Clear()
Clear all caches.
Definition: group_cmd.cpp:43
SetBit
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
MarkWholeScreenDirty
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1767
OverflowSafeInt< int64 >
CloseWindowById
void CloseWindowById(WindowClass cls, WindowNumber number, bool force)
Close a window by its class and window number (if it is open).
Definition: window.cpp:1191
Group::folded
bool folded
NOSAVE: Is this group folded in the group view?
Definition: group.h:83
GroundVehicle::IsFreeWagon
bool IsFreeWagon() const
Check if the vehicle is a free wagon (got no engine in front of it).
Definition: ground_vehicle.hpp:310
VEH_TRAIN
@ VEH_TRAIN
Train vehicle type.
Definition: vehicle_type.h:24
Pool::PoolItem<&_group_pool >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:326
BaseVehicle::type
VehicleType type
Type of vehicle.
Definition: vehicle_type.h:52
CompanyProperties::engine_renew_list
EngineRenewList engine_renew_list
Engine renewals of this company.
Definition: company_base.h:105
autoreplace_func.h
pool_func.hpp
order_backup.h
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:470
GF_REPLACE_WAGON_REMOVAL
@ GF_REPLACE_WAGON_REMOVAL
If set, autoreplace will perform wagon removal on vehicles in this group.
Definition: group.h:69
VEH_SHIP
@ VEH_SHIP
Ship vehicle type.
Definition: vehicle_type.h:26
Company
Definition: company_base.h:117
AddVehicleToGroup
static void AddVehicleToGroup(Vehicle *v, GroupID new_g)
Do add a vehicle to a group.
Definition: group_cmd.cpp:481
Livery
Information about a particular livery.
Definition: livery.h:78
Vehicle::GetDisplayProfitLastYear
Money GetDisplayProfitLastYear() const
Gets the profit vehicle had last year.
Definition: vehicle_base.h:601
Engine::type
VehicleType type
Vehicle type, ie VEH_ROAD, VEH_TRAIN, etc.
Definition: engine_base.h:55
GroupStatistics::UpdateAfterLoad
static void UpdateAfterLoad()
Update all caches after loading a game, changing NewGRF, etc.
Definition: group_cmd.cpp:100
VEHICLE_PROFIT_MIN_AGE
static const int VEHICLE_PROFIT_MIN_AGE
Only vehicles older than this have a meaningful profit.
Definition: vehicle_func.h:27
GroupStatistics::AddProfitLastYear
static void AddProfitLastYear(const Vehicle *v)
Add a vehicle's last year profit to the profit sum of its group.
Definition: group_cmd.cpp:167
Livery::colour2
byte colour2
Second colour, for vehicles with 2CC support.
Definition: livery.h:81
Livery::colour1
byte colour1
First colour, for all vehicles.
Definition: livery.h:80