OpenTTD Source: src/group_cmd.cpp Source File

OpenTTD Source 1.11.0-RC1

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 "cmd_helper.h"
12 #include "command_func.h"
13 #include "train.h"
14 #include "vehiclelist.h"
15 #include "vehicle_func.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
23 #include "table/strings.h"
24
25 #include "safeguards.h"
26
27 GroupID _new_group_id;
28
29 GroupPool _group_pool("Group");
30 INSTANTIATE_POOL_METHODS(Group)
31
32 GroupStatistics::GroupStatistics()
33 {
34 this->num_engines = CallocT<uint16>(Engine::GetPoolSize());
35 }
36
37 GroupStatistics::~GroupStatistics()
38 {
39 free(this->num_engines);
40 }
41
45 void GroupStatistics::Clear()
46 {
47 this->num_vehicle = 0;
48 this->num_profit_vehicle = 0;
49 this->profit_last_year = 0;
50
51 /* This is also called when NewGRF change. So the number of engines might have changed. Reallocate. */
52 free(this->num_engines);
53 this->num_engines = CallocT<uint16>(Engine::GetPoolSize());
54 }
55
63 /* static */ GroupStatistics &GroupStatistics::Get(CompanyID company, GroupID id_g, VehicleType type)
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
83 /* static */ GroupStatistics &GroupStatistics::Get(const Vehicle *v)
84 {
85 return GroupStatistics::Get(v->owner, v->group_id, v->type);
86 }
87
93 /* static */ GroupStatistics &GroupStatistics::GetAllGroup(const Vehicle *v)
94 {
95 return GroupStatistics::Get(v->owner, ALL_GROUP, v->type);
96 }
97
101 /* static */ void GroupStatistics::UpdateAfterLoad()
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
119 GroupStatistics::CountEngine(v, 1);
120 if (v->IsPrimaryVehicle()) GroupStatistics::CountVehicle(v, 1);
121 }
122
123 for (const Company *c : Company::Iterate()) {
124 GroupStatistics::UpdateAutoreplace(c->index);
125 }
126 }
127
133 /* static */ void GroupStatistics::CountVehicle(const Vehicle *v, int delta)
134 {
135 assert(delta == 1 || delta == -1);
136
137 GroupStatistics &stats_all = GroupStatistics::GetAllGroup(v);
138 GroupStatistics &stats = GroupStatistics::Get(v);
139
140 stats_all.num_vehicle += delta;
141 stats.num_vehicle += delta;
142
143 if (v->age > VEHICLE_PROFIT_MIN_AGE) {
144 stats_all.num_profit_vehicle += delta;
145 stats_all.profit_last_year += v->GetDisplayProfitLastYear() * delta;
146 stats.num_profit_vehicle += delta;
147 stats.profit_last_year += v->GetDisplayProfitLastYear() * delta;
148 }
149 }
150
156 /* static */ void GroupStatistics::CountEngine(const Vehicle *v, int delta)
157 {
158 assert(delta == 1 || delta == -1);
159 GroupStatistics::GetAllGroup(v).num_engines[v->engine_type] += delta;
160 GroupStatistics::Get(v).num_engines[v->engine_type] += delta;
161 }
162
166 /* static */ void GroupStatistics::VehicleReachedProfitAge(const Vehicle *v)
167 {
168 GroupStatistics &stats_all = GroupStatistics::GetAllGroup(v);
169 GroupStatistics &stats = GroupStatistics::Get(v);
170
171 stats_all.num_profit_vehicle++;
172 stats_all.profit_last_year += v->GetDisplayProfitLastYear();
173 stats.num_profit_vehicle++;
174 stats.profit_last_year += v->GetDisplayProfitLastYear();
175 }
176
180 /* static */ void GroupStatistics::UpdateProfits()
181 {
182 /* Set up the engine count for all companies */
183 for (Company *c : Company::Iterate()) {
184 for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
185 c->group_all[type].ClearProfits();
186 c->group_default[type].ClearProfits();
187 }
188 }
189
190 /* Recalculate */
191 for (Group *g : Group::Iterate()) {
192 g->statistics.ClearProfits();
193 }
194
195 for (const Vehicle *v : Vehicle::Iterate()) {
196 if (v->IsPrimaryVehicle() && v->age > VEHICLE_PROFIT_MIN_AGE) GroupStatistics::VehicleReachedProfitAge(v);
197 }
198 }
199
204 /* static */ void GroupStatistics::UpdateAutoreplace(CompanyID company)
205 {
206 /* Set up the engine count for all companies */
207 Company *c = Company::Get(company);
208 for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
209 c->group_all[type].ClearAutoreplace();
210 c->group_default[type].ClearAutoreplace();
211 }
212
213 /* Recalculate */
214 for (Group *g : Group::Iterate()) {
215 if (g->owner != company) continue;
216 g->statistics.ClearAutoreplace();
217 }
218
219 for (EngineRenewList erl = c->engine_renew_list; erl != nullptr; erl = erl->next) {
220 const Engine *e = Engine::Get(erl->from);
221 GroupStatistics &stats = GroupStatistics::Get(company, erl->group_id, e->type);
222 if (!stats.autoreplace_defined) {
223 stats.autoreplace_defined = true;
224 stats.autoreplace_finished = true;
225 }
226 if (GetGroupNumEngines(company, erl->group_id, erl->from) > 0) stats.autoreplace_finished = false;
227 }
228 }
229
237 static inline void UpdateNumEngineGroup(const Vehicle *v, GroupID old_g, GroupID new_g)
238 {
239 if (old_g != new_g) {
240 /* Decrease the num engines in the old group */
241 GroupStatistics::Get(v->owner, old_g, v->type).num_engines[v->engine_type]--;
242
243 /* Increase the num engines in the new group */
244 GroupStatistics::Get(v->owner, new_g, v->type).num_engines[v->engine_type]++;
245 }
246 }
247
248
249 const Livery *GetParentLivery(const Group *g)
250 {
251 if (g->parent == INVALID_GROUP) {
252 const Company *c = Company::Get(g->owner);
253 return &c->livery[LS_DEFAULT];
254 }
255
256 const Group *pg = Group::Get(g->parent);
257 return &pg->livery;
258 }
259
260
265 void PropagateChildLivery(const Group *g)
266 {
267 /* Company colour data is indirectly cached. */
268 for (Vehicle *v : Vehicle::Iterate()) {
269 if (v->group_id == g->index && (!v->IsGroundVehicle() || v->IsFrontEngine())) {
270 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
271 u->colourmap = PAL_NONE;
272 u->InvalidateNewGRFCache();
273 }
274 }
275 }
276
277 for (Group *cg : Group::Iterate()) {
278 if (cg->parent == g->index) {
279 if (!HasBit(cg->livery.in_use, 0)) cg->livery.colour1 = g->livery.colour1;
280 if (!HasBit(cg->livery.in_use, 1)) cg->livery.colour2 = g->livery.colour2;
281 PropagateChildLivery(cg);
282 }
283 }
284 }
285
286
287 Group::Group(Owner owner)
288 {
289 this->owner = owner;
290 this->folded = false;
291 }
292
293
303 CommandCost CmdCreateGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
304 {
305 VehicleType vt = Extract<VehicleType, 0, 3>(p1);
306 if (!IsCompanyBuildableVehicleType(vt)) return CMD_ERROR;
307
308 if (!Group::CanAllocateItem()) return CMD_ERROR;
309
310 const Group *pg = Group::GetIfValid(GB(p2, 0, 16));
311 if (pg != nullptr) {
312 if (pg->owner != _current_company) return CMD_ERROR;
313 if (pg->vehicle_type != vt) return CMD_ERROR;
314 }
315
316 if (flags & DC_EXEC) {
317 Group *g = new Group(_current_company);
318 g->replace_protection = false;
319 g->vehicle_type = vt;
320 g->parent = INVALID_GROUP;
321
322 if (pg == nullptr) {
323 const Company *c = Company::Get(_current_company);
324 g->livery.colour1 = c->livery[LS_DEFAULT].colour1;
325 g->livery.colour2 = c->livery[LS_DEFAULT].colour2;
326 } else {
327 g->parent = pg->index;
328 g->livery.colour1 = pg->livery.colour1;
329 g->livery.colour2 = pg->livery.colour2;
330 }
331
332 _new_group_id = g->index;
333
334 InvalidateWindowData(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_GROUP_LIST, vt, _current_company).Pack());
335 InvalidateWindowData(WC_COMPANY_COLOUR, g->owner, g->vehicle_type);
336 }
337
338 return CommandCost();
339 }
340
341
352 CommandCost CmdDeleteGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
353 {
354 Group *g = Group::GetIfValid(p1);
355 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
356
357 /* Remove all vehicles from the group */
358 DoCommand(0, p1, 0, flags, CMD_REMOVE_ALL_VEHICLES_GROUP);
359
360 /* Delete sub-groups */
361 for (const Group *gp : Group::Iterate()) {
362 if (gp->parent == g->index) {
363 DoCommand(0, gp->index, 0, flags, CMD_DELETE_GROUP);
364 }
365 }
366
367 if (flags & DC_EXEC) {
368 /* Update backupped orders if needed */
369 OrderBackup::ClearGroup(g->index);
370
371 /* If we set an autoreplace for the group we delete, remove it. */
372 if (_current_company < MAX_COMPANIES) {
373 Company *c;
374
375 c = Company::Get(_current_company);
376 for (EngineRenew *er : EngineRenew::Iterate()) {
377 if (er->group_id == g->index) RemoveEngineReplacementForCompany(c, er->from, g->index, flags);
378 }
379 }
380
381 VehicleType vt = g->vehicle_type;
382
383 /* Delete the Replace Vehicle Windows */
384 DeleteWindowById(WC_REPLACE_VEHICLE, g->vehicle_type);
385 delete g;
386
387 InvalidateWindowData(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_GROUP_LIST, vt, _current_company).Pack());
388 InvalidateWindowData(WC_COMPANY_COLOUR, _current_company, vt);
389 }
390
391 return CommandCost();
392 }
393
406 CommandCost CmdAlterGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
407 {
408 Group *g = Group::GetIfValid(GB(p1, 0, 16));
409 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
410
411 if (!HasBit(p1, 16)) {
412 /* Rename group */
413 bool reset = StrEmpty(text);
414
415 if (!reset) {
416 if (Utf8StringLength(text) >= MAX_LENGTH_GROUP_NAME_CHARS) return CMD_ERROR;
417 }
418
419 if (flags & DC_EXEC) {
420 /* Assign the new one */
421 if (reset) {
422 g->name.clear();
423 } else {
424 g->name = text;
425 }
426 }
427 } else {
428 /* Set group parent */
429 const Group *pg = Group::GetIfValid(GB(p2, 0, 16));
430
431 if (pg != nullptr) {
432 if (pg->owner != _current_company) return CMD_ERROR;
433 if (pg->vehicle_type != g->vehicle_type) return CMD_ERROR;
434
435 /* Ensure request parent isn't child of group.
436 * This is the only place that infinite loops are prevented. */
437 if (GroupIsInGroup(pg->index, g->index)) return_cmd_error(STR_ERROR_GROUP_CAN_T_SET_PARENT_RECURSION);
438 }
439
440 if (flags & DC_EXEC) {
441 g->parent = (pg == nullptr) ? INVALID_GROUP : pg->index;
442 GroupStatistics::UpdateAutoreplace(g->owner);
443
444 if (g->livery.in_use == 0) {
445 const Livery *livery = GetParentLivery(g);
446 g->livery.colour1 = livery->colour1;
447 g->livery.colour2 = livery->colour2;
448
449 PropagateChildLivery(g);
450 MarkWholeScreenDirty();
451 }
452 }
453 }
454
455 if (flags & DC_EXEC) {
456 InvalidateWindowData(WC_REPLACE_VEHICLE, g->vehicle_type, 1);
457 InvalidateWindowData(GetWindowClassForVehicleType(g->vehicle_type), VehicleListIdentifier(VL_GROUP_LIST, g->vehicle_type, _current_company).Pack());
458 InvalidateWindowData(WC_COMPANY_COLOUR, g->owner, g->vehicle_type);
459 InvalidateWindowClassesData(WC_VEHICLE_VIEW);
460 InvalidateWindowClassesData(WC_VEHICLE_DETAILS);
461 }
462
463 return CommandCost();
464 }
465
466
472 static void AddVehicleToGroup(Vehicle *v, GroupID new_g)
473 {
474 GroupStatistics::CountVehicle(v, -1);
475
476 switch (v->type) {
477 default: NOT_REACHED();
478 case VEH_TRAIN:
479 SetTrainGroupID(Train::From(v), new_g);
480 break;
481
482 case VEH_ROAD:
483 case VEH_SHIP:
484 case VEH_AIRCRAFT:
485 if (v->IsEngineCountable()) UpdateNumEngineGroup(v, v->group_id, new_g);
486 v->group_id = new_g;
487 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
488 u->colourmap = PAL_NONE;
489 u->InvalidateNewGRFCache();
490 u->UpdateViewport(true);
491 }
492 break;
493 }
494
495 GroupStatistics::CountVehicle(v, 1);
496 }
497
510 CommandCost CmdAddVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
511 {
512 Vehicle *v = Vehicle::GetIfValid(GB(p2, 0, 20));
513 GroupID new_g = p1;
514
515 if (v == nullptr || (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g) && new_g != NEW_GROUP)) return CMD_ERROR;
516
517 if (Group::IsValidID(new_g)) {
518 Group *g = Group::Get(new_g);
519 if (g->owner != _current_company || g->vehicle_type != v->type) return CMD_ERROR;
520 }
521
522 if (v->owner != _current_company || !v->IsPrimaryVehicle()) return CMD_ERROR;
523
524 if (new_g == NEW_GROUP) {
525 /* Create new group. */
526 CommandCost ret = CmdCreateGroup(0, flags, v->type, INVALID_GROUP, nullptr);
527 if (ret.Failed()) return ret;
528
529 new_g = _new_group_id;
530 }
531
532 if (flags & DC_EXEC) {
533 AddVehicleToGroup(v, new_g);
534
535 if (HasBit(p2, 31)) {
536 /* Add vehicles in the shared order list as well. */
537 for (Vehicle *v2 = v->FirstShared(); v2 != nullptr; v2 = v2->NextShared()) {
538 if (v2->group_id != new_g) AddVehicleToGroup(v2, new_g);
539 }
540 }
541
542 GroupStatistics::UpdateAutoreplace(v->owner);
543
544 /* Update the Replace Vehicle Windows */
545 SetWindowDirty(WC_REPLACE_VEHICLE, v->type);
546 SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
547 SetWindowDirty(WC_VEHICLE_VIEW, v->index);
548 SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
549 InvalidateWindowData(GetWindowClassForVehicleType(v->type), VehicleListIdentifier(VL_GROUP_LIST, v->type, _current_company).Pack());
550 InvalidateWindowData(WC_VEHICLE_VIEW, v->index);
551 InvalidateWindowData(WC_VEHICLE_DETAILS, v->index);
552 }
553
554 return CommandCost();
555 }
556
567 CommandCost CmdAddSharedVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
568 {
569 VehicleType type = Extract<VehicleType, 0, 3>(p2);
570 GroupID id_g = p1;
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) DoCommand(tile, id_g, v2->index, flags, CMD_ADD_VEHICLE_GROUP, text);
583 }
584 }
585 }
586
587 InvalidateWindowData(GetWindowClassForVehicleType(type), VehicleListIdentifier(VL_GROUP_LIST, type, _current_company).Pack());
588 }
589
590 return CommandCost();
591 }
592
593
604 CommandCost CmdRemoveAllVehiclesGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
605 {
606 GroupID old_g = p1;
607 Group *g = Group::GetIfValid(old_g);
608
609 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
610
611 if (flags & DC_EXEC) {
612 /* Find each Vehicle that belongs to the group old_g and add it to the default group */
613 for (const Vehicle *v : Vehicle::Iterate()) {
614 if (v->IsPrimaryVehicle()) {
615 if (v->group_id != old_g) continue;
616
617 /* Add The Vehicle to the default group */
618 DoCommand(tile, DEFAULT_GROUP, v->index, flags, CMD_ADD_VEHICLE_GROUP, text);
619 }
620 }
621
622 InvalidateWindowData(GetWindowClassForVehicleType(g->vehicle_type), VehicleListIdentifier(VL_GROUP_LIST, g->vehicle_type, _current_company).Pack());
623 }
624
625 return CommandCost();
626 }
627
638 CommandCost CmdSetGroupLivery(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
639 {
640 Group *g = Group::GetIfValid(p1);
641 bool primary = !HasBit(p2, 8);
642 Colours colour = Extract<Colours, 16, 8>(p2);
643
644 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
645
646 if (colour >= COLOUR_END && colour != INVALID_COLOUR) return CMD_ERROR;
647
648 if (flags & DC_EXEC) {
649 if (primary) {
650 SB(g->livery.in_use, 0, 1, colour != INVALID_COLOUR);
651 if (colour == INVALID_COLOUR) colour = (Colours)GetParentLivery(g)->colour1;
652 g->livery.colour1 = colour;
653 } else {
654 SB(g->livery.in_use, 1, 1, colour != INVALID_COLOUR);
655 if (colour == INVALID_COLOUR) colour = (Colours)GetParentLivery(g)->colour2;
656 g->livery.colour2 = colour;
657 }
658
659 PropagateChildLivery(g);
660 MarkWholeScreenDirty();
661 }
662
663 return CommandCost();
664 }
665
671 static void SetGroupReplaceProtection(Group *g, bool protect)
672 {
673 g->replace_protection = protect;
674
675 for (Group *pg : Group::Iterate()) {
676 if (pg->parent == g->index) SetGroupReplaceProtection(pg, protect);
677 }
678 }
679
692 CommandCost CmdSetGroupReplaceProtection(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
693 {
694 Group *g = Group::GetIfValid(p1);
695 if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
696
697 if (flags & DC_EXEC) {
698 if (HasBit(p2, 1)) {
699 SetGroupReplaceProtection(g, HasBit(p2, 0));
700 } else {
701 g->replace_protection = HasBit(p2, 0);
702 }
703
704 SetWindowDirty(GetWindowClassForVehicleType(g->vehicle_type), VehicleListIdentifier(VL_GROUP_LIST, g->vehicle_type, _current_company).Pack());
705 InvalidateWindowData(WC_REPLACE_VEHICLE, g->vehicle_type);
706 }
707
708 return CommandCost();
709 }
710
716 void RemoveVehicleFromGroup(const Vehicle *v)
717 {
718 if (!v->IsPrimaryVehicle()) return;
719
720 if (!IsDefaultGroupID(v->group_id)) GroupStatistics::CountVehicle(v, -1);
721 }
722
723
730 void SetTrainGroupID(Train *v, GroupID new_g)
731 {
732 if (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g)) return;
733
734 assert(v->IsFrontEngine() || IsDefaultGroupID(new_g));
735
736 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
737 if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
738
739 u->group_id = new_g;
740 u->colourmap = PAL_NONE;
741 u->InvalidateNewGRFCache();
742 u->UpdateViewport(true);
743 }
744
745 /* Update the Replace Vehicle Windows */
746 GroupStatistics::UpdateAutoreplace(v->owner);
747 SetWindowDirty(WC_REPLACE_VEHICLE, VEH_TRAIN);
748 }
749
750
758 void UpdateTrainGroupID(Train *v)
759 {
760 assert(v->IsFrontEngine() || v->IsFreeWagon());
761
762 GroupID new_g = v->IsFrontEngine() ? v->group_id : (GroupID)DEFAULT_GROUP;
763 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
764 if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
765
766 u->group_id = new_g;
767 u->colourmap = PAL_NONE;
768 u->InvalidateNewGRFCache();
769 }
770
771 /* Update the Replace Vehicle Windows */
772 GroupStatistics::UpdateAutoreplace(v->owner);
773 SetWindowDirty(WC_REPLACE_VEHICLE, VEH_TRAIN);
774 }
775
784 uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e)
785 {
786 uint count = 0;
787 const Engine *e = Engine::Get(id_e);
788 for (const Group *g : Group::Iterate()) {
789 if (g->parent == id_g) count += GetGroupNumEngines(company, g->index, id_e);
790 }
791 return count + GroupStatistics::Get(company, id_g, e->type).num_engines[id_e];
792 }
793
802 uint GetGroupNumVehicle(CompanyID company, GroupID id_g, VehicleType type)
803 {
804 uint count = 0;
805 for (const Group *g : Group::Iterate()) {
806 if (g->parent == id_g) count += GetGroupNumVehicle(company, g->index, type);
807 }
808 return count + GroupStatistics::Get(company, id_g, type).num_vehicle;
809 }
810
819 uint GetGroupNumProfitVehicle(CompanyID company, GroupID id_g, VehicleType type)
820 {
821 uint count = 0;
822 for (const Group *g : Group::Iterate()) {
823 if (g->parent == id_g) count += GetGroupNumProfitVehicle(company, g->index, type);
824 }
825 return count + GroupStatistics::Get(company, id_g, type).num_profit_vehicle;
826 }
827
836 Money GetGroupProfitLastYear(CompanyID company, GroupID id_g, VehicleType type)
837 {
838 Money sum = 0;
839 for (const Group *g : Group::Iterate()) {
840 if (g->parent == id_g) sum += GetGroupProfitLastYear(company, g->index, type);
841 }
842 return sum + GroupStatistics::Get(company, id_g, type).profit_last_year;
843 }
844
845 void RemoveAllGroupsForCompany(const CompanyID company)
846 {
847 for (Group *g : Group::Iterate()) {
848 if (company == g->owner) delete g;
849 }
850 }
851
852
859 bool GroupIsInGroup(GroupID search, GroupID group)
860 {
861 if (!Group::IsValidID(search)) return search == group;
862
863 do {
864 if (search == group) return true;
865 search = Group::Get(search)->parent;
866 } while (search != INVALID_GROUP);
867
868 return false;
869 }
VEH_AIRCRAFT
@ VEH_AIRCRAFT
Aircraft vehicle type.
Definition: vehicle_type.h:27
IsCompanyBuildableVehicleType
static bool IsCompanyBuildableVehicleType(VehicleType type)
Is the given vehicle type buildable by a company?
Definition: vehicle_func.h:89
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
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:3321
CmdAddSharedVehicleGroup
CommandCost CmdAddSharedVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Add all shared vehicles of all vehicles from a group.
Definition: group_cmd.cpp:567
DoCommand
CommandCost DoCommand(const CommandContainer *container, DoCommandFlag flags)
Shorthand for calling the long DoCommand with a container.
Definition: command.cpp:450
autoreplace_base.h
Pool::PoolItem<&_group_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:329
SetWindowDirty
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3220
GroupStatistics::CountEngine
static void CountEngine(const Vehicle *v, int delta)
Update num_engines when adding/removing an engine.
Definition: group_cmd.cpp:156
Company::group_all
GroupStatistics group_all[VEH_COMPANY_END]
NOSAVE: Statistics for the ALL_GROUP group.
Definition: company_base.h:123
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
CMD_ADD_VEHICLE_GROUP
@ CMD_ADD_VEHICLE_GROUP
add a vehicle to a group
Definition: command_type.h:324
train.h
command_func.h
CmdSetGroupLivery
CommandCost CmdSetGroupLivery(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Set the livery for a vehicle group.
Definition: group_cmd.cpp:638
Pool::PoolItem<&_group_pool >::GetIfValid
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:340
CMD_ERROR
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:23
Vehicle::IsEngineCountable
bool IsEngineCountable() const
Check if a vehicle is counted in num_engines in each company struct.
Definition: vehicle.cpp:708
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:592
SpecializedVehicle::Next
T * Next() const
Get next vehicle in the chain.
Definition: vehicle_base.h:1077
WC_COMPANY_COLOUR
@ WC_COMPANY_COLOUR
Company colour selection; Window numbers:
Definition: window_type.h:223
CMD_REMOVE_ALL_VEHICLES_GROUP
@ CMD_REMOVE_ALL_VEHICLES_GROUP
remove all vehicles from a group
Definition: command_type.h:326
vehiclelist.h
CmdCreateGroup
CommandCost CmdCreateGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Create a new vehicle group.
Definition: group_cmd.cpp:303
GetGroupNumProfitVehicle
uint GetGroupNumProfitVehicle(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:819
Group::parent
GroupID parent
Parent group.
Definition: group.h:77
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:227
Vehicle::group_id
GroupID group_id
Index of group Pool array.
Definition: vehicle_base.h:335
UpdateNumEngineGroup
static void UpdateNumEngineGroup(const Vehicle *v, GroupID old_g, GroupID new_g)
Update the num engines of a groupID.
Definition: group_cmd.cpp:237
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:133
GroupStatistics::autoreplace_finished
bool autoreplace_finished
Have all autoreplacement finished?
Definition: group.h:30
Group::livery
Livery livery
Custom colour scheme for vehicles in this group.
Definition: group.h:72
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:21
VEH_ROAD
@ VEH_ROAD
Road vehicle type.
Definition: vehicle_type.h:25
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:222
GroupStatistics::profit_last_year
Money profit_last_year
Sum of profits for all vehicles.
Definition: group.h:33
Vehicle::IsPrimaryVehicle
virtual bool IsPrimaryVehicle() const
Whether this is the primary vehicle in the chain.
Definition: vehicle_base.h:444
Vehicle::owner
Owner owner
Which company owns the vehicle?
Definition: vehicle_base.h:283
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
DC_EXEC
@ DC_EXEC
execute the given command
Definition: command_type.h:348
Group::vehicle_type
VehicleType vehicle_type
Vehicle type of the group.
Definition: group.h:69
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:346
CmdSetGroupReplaceProtection
CommandCost CmdSetGroupReplaceProtection(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
(Un)set global replace protection from a group
Definition: group_cmd.cpp:692
GroupStatistics::num_vehicle
uint16 num_vehicle
Number of vehicles.
Definition: group.h:26
SetTrainGroupID
void SetTrainGroupID(Train *v, GroupID new_g)
Affect the groupID of a train to new_g.
Definition: group_cmd.cpp:730
UpdateTrainGroupID
void UpdateTrainGroupID(Train *v)
Recalculates the groupID of a train.
Definition: group_cmd.cpp:758
ALL_GROUP
static const GroupID ALL_GROUP
All vehicles are in this group.
Definition: group_type.h:16
Group
Group data.
Definition: group.h:66
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:334
Pool::PoolItem<&_engine_pool >::GetPoolSize
static size_t GetPoolSize()
Returns first unused index.
Definition: pool_type.hpp:350
Group::replace_protection
bool replace_protection
If set to true, the global autoreplace have no effect on the group.
Definition: group.h:71
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:33
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:23
cmd_helper.h
WC_VEHICLE_VIEW
@ WC_VEHICLE_VIEW
Vehicle view; Window numbers:
Definition: window_type.h:332
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:73
Company::group_default
GroupStatistics group_default[VEH_COMPANY_END]
NOSAVE: Statistics for the DEFAULT_GROUP group.
Definition: company_base.h:124
Vehicle::tile
TileIndex tile
Current tile index.
Definition: vehicle_base.h:240
Vehicle::engine_type
EngineID engine_type
The type of engine used for this vehicle.
Definition: vehicle_base.h:297
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:802
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
GetGroupProfitLastYear
Money GetGroupProfitLastYear(CompanyID company, GroupID id_g, VehicleType type)
Get last year's profit for the group with GroupID id_g and its sub-groups.
Definition: group_cmd.cpp:836
CommandCost::Failed
bool Failed() const
Did this command fail?
Definition: command_type.h:159
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:68
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:895
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
PropagateChildLivery
void PropagateChildLivery(const Group *g)
Propagate a livery change to a group's children.
Definition: group_cmd.cpp:265
WC_VEHICLE_DETAILS
@ WC_VEHICLE_DETAILS
Vehicle details; Window numbers:
Definition: window_type.h:193
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:204
Train
'Train' is either a loco or a wagon.
Definition: train.h:85
CmdRemoveAllVehiclesGroup
CommandCost CmdRemoveAllVehiclesGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Remove all vehicles from a group.
Definition: group_cmd.cpp:604
GroupStatistics::Get
static GroupStatistics & Get(CompanyID company, GroupID id_g, VehicleType type)
Returns the GroupStatistics for a specific group.
Definition: group_cmd.cpp:63
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:27
StrEmpty
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:60
CmdAlterGroup
CommandCost CmdAlterGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Alter a group.
Definition: group_cmd.cpp:406
GroupStatistics::GetAllGroup
static GroupStatistics & GetAllGroup(const Vehicle *v)
Returns the GroupStatistic for the ALL_GROUPO of a vehicle type.
Definition: group_cmd.cpp:93
GroupStatistics::VehicleReachedProfitAge
static void VehicleReachedProfitAge(const Vehicle *v)
Add a vehicle to the profit sum of its group.
Definition: group_cmd.cpp:166
IsAllGroupID
static bool IsAllGroupID(GroupID id_g)
Checks if a GroupID stands for all vehicles of a company.
Definition: group.h:93
_group_pool
GroupPool _group_pool("Group")
Pool of groups.
Company::engine_renew_list
EngineRenewList engine_renew_list
Engine renewals of this company.
Definition: company_base.h:121
EngineRenew
Struct to store engine replacements.
Definition: autoreplace_base.h:33
stdafx.h
CMD_DELETE_GROUP
@ CMD_DELETE_GROUP
delete a group
Definition: command_type.h:322
VehicleType
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
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:29
GetWindowClassForVehicleType
static WindowClass GetWindowClassForVehicleType(VehicleType vt)
Get WindowClass for vehicle list of given vehicle type.
Definition: vehicle_gui.h:91
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:716
Vehicle::FirstShared
Vehicle * FirstShared() const
Get the first vehicle of this vehicle chain.
Definition: vehicle_base.h:686
_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:378
Pool
Base class for all pools.
Definition: pool_type.hpp:81
DeleteWindowById
void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
Delete a window by its class and window number (if it is open).
Definition: window.cpp:1165
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:180
SpecializedVehicle< Train, Type >::From
static Train * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
Definition: vehicle_base.h:1162
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:784
SetGroupReplaceProtection
static void SetGroupReplaceProtection(Group *g, bool protect)
Set replace protection for a group and its sub-groups.
Definition: group_cmd.cpp:671
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:3339
Vehicle::NextShared
Vehicle * NextShared() const
Get the next vehicle of the shared vehicle chain.
Definition: vehicle_base.h:674
GroupIsInGroup
bool GroupIsInGroup(GroupID search, GroupID group)
Test if GroupID group is a descendant of (or is) GroupID search.
Definition: group_cmd.cpp:859
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:268
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:299
OrderBackup::ClearGroup
static void ClearGroup(GroupID group)
Clear the group of all backups having this group ID.
Definition: order_backup.cpp:214
Group::name
std::string name
Group Name.
Definition: group.h:67
company_func.h
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
CmdAddVehicleGroup
CommandCost CmdAddVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Add a vehicle to a group.
Definition: group_cmd.cpp:510
GroupStatistics::Clear
void Clear()
Clear all caches.
Definition: group_cmd.cpp:45
MarkWholeScreenDirty
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1597
OverflowSafeInt< int64, INT64_MAX, INT64_MIN >
Group::folded
bool folded
NOSAVE: Is this group folded in the group view?
Definition: group.h:75
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:318
BaseVehicle::type
VehicleType type
Type of vehicle.
Definition: vehicle_type.h:52
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:454
CmdDeleteGroup
CommandCost CmdDeleteGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Add all vehicles in the given group to the default group and then deletes the group.
Definition: group_cmd.cpp:352
VEH_SHIP
@ VEH_SHIP
Ship vehicle type.
Definition: vehicle_type.h:26
Company
Definition: company_base.h:110
AddVehicleToGroup
static void AddVehicleToGroup(Vehicle *v, GroupID new_g)
Do add a vehicle to a group.
Definition: group_cmd.cpp:472
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:583
Engine::type
VehicleType type
Vehicle type, ie VEH_ROAD, VEH_TRAIN, etc.
Definition: engine_base.h:40
GroupStatistics::UpdateAfterLoad
static void UpdateAfterLoad()
Update all caches after loading a game, changing NewGRF, etc.
Definition: group_cmd.cpp:101
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::num_profit_vehicle
uint16 num_profit_vehicle
Number of vehicles considered for profit statistics;.
Definition: group.h:32
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