OpenTTD Source  13.0-beta2
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");
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 
44 {
45  this->num_vehicle = 0;
46  this->num_profit_vehicle = 0;
47  this->profit_last_year = 0;
48 
49  /* This is also called when NewGRF change. So the number of engines might have changed. Reallocate. */
50  free(this->num_engines);
51  this->num_engines = CallocT<uint16>(Engine::GetPoolSize());
52 }
53 
62 {
63  if (Group::IsValidID(id_g)) {
64  Group *g = Group::Get(id_g);
65  assert(g->owner == company);
66  assert(g->vehicle_type == type);
67  return g->statistics;
68  }
69 
70  if (IsDefaultGroupID(id_g)) return Company::Get(company)->group_default[type];
71  if (IsAllGroupID(id_g)) return Company::Get(company)->group_all[type];
72 
73  NOT_REACHED();
74 }
75 
82 {
83  return GroupStatistics::Get(v->owner, v->group_id, v->type);
84 }
85 
92 {
93  return GroupStatistics::Get(v->owner, ALL_GROUP, v->type);
94 }
95 
100 {
101  /* Set up the engine count for all companies */
102  for (Company *c : Company::Iterate()) {
103  for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
104  c->group_all[type].Clear();
105  c->group_default[type].Clear();
106  }
107  }
108 
109  /* Recalculate */
110  for (Group *g : Group::Iterate()) {
111  g->statistics.Clear();
112  }
113 
114  for (const Vehicle *v : Vehicle::Iterate()) {
115  if (!v->IsEngineCountable()) continue;
116 
118  if (v->IsPrimaryVehicle()) GroupStatistics::CountVehicle(v, 1);
119  }
120 
121  for (const Company *c : Company::Iterate()) {
123  }
124 }
125 
131 /* static */ void GroupStatistics::CountVehicle(const Vehicle *v, int delta)
132 {
133  assert(delta == 1 || delta == -1);
134 
137 
138  stats_all.num_vehicle += delta;
139  stats.num_vehicle += delta;
140 
141  if (v->age > VEHICLE_PROFIT_MIN_AGE) {
142  stats_all.num_profit_vehicle += delta;
143  stats_all.profit_last_year += v->GetDisplayProfitLastYear() * delta;
144  stats.num_profit_vehicle += delta;
145  stats.profit_last_year += v->GetDisplayProfitLastYear() * delta;
146  }
147 }
148 
154 /* static */ void GroupStatistics::CountEngine(const Vehicle *v, int delta)
155 {
156  assert(delta == 1 || delta == -1);
159 }
160 
165 {
168 
169  stats_all.num_profit_vehicle++;
170  stats_all.profit_last_year += v->GetDisplayProfitLastYear();
171  stats.num_profit_vehicle++;
173 }
174 
179 {
180  /* Set up the engine count for all companies */
181  for (Company *c : Company::Iterate()) {
182  for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
183  c->group_all[type].ClearProfits();
184  c->group_default[type].ClearProfits();
185  }
186  }
187 
188  /* Recalculate */
189  for (Group *g : Group::Iterate()) {
190  g->statistics.ClearProfits();
191  }
192 
193  for (const Vehicle *v : Vehicle::Iterate()) {
194  if (v->IsPrimaryVehicle() && v->age > VEHICLE_PROFIT_MIN_AGE) GroupStatistics::VehicleReachedProfitAge(v);
195  }
196 }
197 
203 {
204  /* Set up the engine count for all companies */
205  Company *c = Company::Get(company);
206  for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
207  c->group_all[type].ClearAutoreplace();
208  c->group_default[type].ClearAutoreplace();
209  }
210 
211  /* Recalculate */
212  for (Group *g : Group::Iterate()) {
213  if (g->owner != company) continue;
214  g->statistics.ClearAutoreplace();
215  }
216 
217  for (EngineRenewList erl = c->engine_renew_list; erl != nullptr; erl = erl->next) {
218  const Engine *e = Engine::Get(erl->from);
219  GroupStatistics &stats = GroupStatistics::Get(company, erl->group_id, e->type);
220  if (!stats.autoreplace_defined) {
221  stats.autoreplace_defined = true;
222  stats.autoreplace_finished = true;
223  }
224  if (GetGroupNumEngines(company, erl->group_id, erl->from) > 0) stats.autoreplace_finished = false;
225  }
226 }
227 
235 static inline void UpdateNumEngineGroup(const Vehicle *v, GroupID old_g, GroupID new_g)
236 {
237  if (old_g != new_g) {
238  /* Decrease the num engines in the old group */
240 
241  /* Increase the num engines in the new group */
243  }
244 }
245 
246 
247 const Livery *GetParentLivery(const Group *g)
248 {
249  if (g->parent == INVALID_GROUP) {
250  const Company *c = Company::Get(g->owner);
251  return &c->livery[LS_DEFAULT];
252  }
253 
254  const Group *pg = Group::Get(g->parent);
255  return &pg->livery;
256 }
257 
258 
264 {
265  /* Company colour data is indirectly cached. */
266  for (Vehicle *v : Vehicle::Iterate()) {
267  if (v->group_id == g->index && (!v->IsGroundVehicle() || v->IsFrontEngine())) {
268  for (Vehicle *u = v; u != nullptr; u = u->Next()) {
269  u->colourmap = PAL_NONE;
270  u->InvalidateNewGRFCache();
271  }
272  }
273  }
274 
275  for (Group *cg : Group::Iterate()) {
276  if (cg->parent == g->index) {
277  if (!HasBit(cg->livery.in_use, 0)) cg->livery.colour1 = g->livery.colour1;
278  if (!HasBit(cg->livery.in_use, 1)) cg->livery.colour2 = g->livery.colour2;
280  }
281  }
282 }
283 
284 
285 Group::Group(Owner owner)
286 {
287  this->owner = owner;
288  this->folded = false;
289 }
290 
291 
299 std::tuple<CommandCost, GroupID> CmdCreateGroup(DoCommandFlag flags, VehicleType vt, GroupID parent_group)
300 {
302 
303  if (!Group::CanAllocateItem()) return { CMD_ERROR, INVALID_GROUP };
304 
305  const Group *pg = Group::GetIfValid(parent_group);
306  if (pg != nullptr) {
307  if (pg->owner != _current_company) return { CMD_ERROR, INVALID_GROUP };
308  if (pg->vehicle_type != vt) return { CMD_ERROR, INVALID_GROUP };
309  }
310 
311  if (flags & DC_EXEC) {
312  Group *g = new Group(_current_company);
313  g->vehicle_type = vt;
314  g->parent = INVALID_GROUP;
315 
316  if (pg == nullptr) {
318  g->livery.colour1 = c->livery[LS_DEFAULT].colour1;
319  g->livery.colour2 = c->livery[LS_DEFAULT].colour2;
321  } else {
322  g->parent = pg->index;
323  g->livery.colour1 = pg->livery.colour1;
324  g->livery.colour2 = pg->livery.colour2;
325  g->flags = pg->flags;
326  }
327 
330 
331  return { CommandCost(), g->index };
332  }
333 
334  return { CommandCost(), INVALID_GROUP};
335 }
336 
337 
345 {
346  Group *g = Group::GetIfValid(group_id);
347  if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
348 
349  /* Remove all vehicles from the group */
351 
352  /* Delete sub-groups */
353  for (const Group *gp : Group::Iterate()) {
354  if (gp->parent == g->index) {
355  Command<CMD_DELETE_GROUP>::Do(flags, gp->index);
356  }
357  }
358 
359  if (flags & DC_EXEC) {
360  /* Update backupped orders if needed */
362 
363  /* If we set an autoreplace for the group we delete, remove it. */
365  Company *c;
366 
368  for (EngineRenew *er : EngineRenew::Iterate()) {
369  if (er->group_id == g->index) RemoveEngineReplacementForCompany(c, er->from, g->index, flags);
370  }
371  }
372 
373  VehicleType vt = g->vehicle_type;
374 
375  /* Delete the Replace Vehicle Windows */
377  delete g;
378 
381  }
382 
383  return CommandCost();
384 }
385 
395 CommandCost CmdAlterGroup(DoCommandFlag flags, AlterGroupMode mode, GroupID group_id, GroupID parent_id, const std::string &text)
396 {
397  Group *g = Group::GetIfValid(group_id);
398  if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
399 
400  if (mode == AlterGroupMode::Rename) {
401  /* Rename group */
402  bool reset = text.empty();
403 
404  if (!reset) {
406  }
407 
408  if (flags & DC_EXEC) {
409  /* Assign the new one */
410  if (reset) {
411  g->name.clear();
412  } else {
413  g->name = text;
414  }
415  }
416  } else if (mode == AlterGroupMode::SetParent) {
417  /* Set group parent */
418  const Group *pg = Group::GetIfValid(parent_id);
419 
420  if (pg != nullptr) {
421  if (pg->owner != _current_company) return CMD_ERROR;
422  if (pg->vehicle_type != g->vehicle_type) return CMD_ERROR;
423 
424  /* Ensure request parent isn't child of group.
425  * This is the only place that infinite loops are prevented. */
426  if (GroupIsInGroup(pg->index, g->index)) return_cmd_error(STR_ERROR_GROUP_CAN_T_SET_PARENT_RECURSION);
427  }
428 
429  if (flags & DC_EXEC) {
430  g->parent = (pg == nullptr) ? INVALID_GROUP : pg->index;
432 
433  if (g->livery.in_use == 0) {
434  const Livery *livery = GetParentLivery(g);
435  g->livery.colour1 = livery->colour1;
436  g->livery.colour2 = livery->colour2;
437 
440  }
441  }
442  } else {
443  return CMD_ERROR;
444  }
445 
446  if (flags & DC_EXEC) {
452  }
453 
454  return CommandCost();
455 }
456 
457 
463 static void AddVehicleToGroup(Vehicle *v, GroupID new_g)
464 {
466 
467  switch (v->type) {
468  default: NOT_REACHED();
469  case VEH_TRAIN:
470  SetTrainGroupID(Train::From(v), new_g);
471  break;
472 
473  case VEH_ROAD:
474  case VEH_SHIP:
475  case VEH_AIRCRAFT:
476  if (v->IsEngineCountable()) UpdateNumEngineGroup(v, v->group_id, new_g);
477  v->group_id = new_g;
478  for (Vehicle *u = v; u != nullptr; u = u->Next()) {
479  u->colourmap = PAL_NONE;
480  u->InvalidateNewGRFCache();
481  u->UpdateViewport(true);
482  }
483  break;
484  }
485 
487 }
488 
497 std::tuple<CommandCost, GroupID> CmdAddVehicleGroup(DoCommandFlag flags, GroupID group_id, VehicleID veh_id, bool add_shared)
498 {
499  Vehicle *v = Vehicle::GetIfValid(veh_id);
500  GroupID new_g = group_id;
501 
502  if (v == nullptr || (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g) && new_g != NEW_GROUP)) return { CMD_ERROR, INVALID_GROUP };
503 
504  if (Group::IsValidID(new_g)) {
505  Group *g = Group::Get(new_g);
506  if (g->owner != _current_company || g->vehicle_type != v->type) return { CMD_ERROR, INVALID_GROUP };
507  }
508 
509  if (v->owner != _current_company || !v->IsPrimaryVehicle()) return { CMD_ERROR, INVALID_GROUP };
510 
511  if (new_g == NEW_GROUP) {
512  /* Create new group. */
513  auto [ret, group_id] = CmdCreateGroup(flags, v->type, INVALID_GROUP);
514  if (ret.Failed()) return { ret, group_id };
515 
516  new_g = group_id;
517  }
518 
519  if (flags & DC_EXEC) {
520  AddVehicleToGroup(v, new_g);
521 
522  if (add_shared) {
523  /* Add vehicles in the shared order list as well. */
524  for (Vehicle *v2 = v->FirstShared(); v2 != nullptr; v2 = v2->NextShared()) {
525  if (v2->group_id != new_g) AddVehicleToGroup(v2, new_g);
526  }
527  }
528 
530 
531  /* Update the Replace Vehicle Windows */
539  }
540 
541  return { CommandCost(), new_g };
542 }
543 
552 {
553  if (!Group::IsValidID(id_g) || !IsCompanyBuildableVehicleType(type)) return CMD_ERROR;
554 
555  if (flags & DC_EXEC) {
556  /* Find the first front engine which belong to the group id_g
557  * then add all shared vehicles of this front engine to the group id_g */
558  for (const Vehicle *v : Vehicle::Iterate()) {
559  if (v->type == type && v->IsPrimaryVehicle()) {
560  if (v->group_id != id_g) continue;
561 
562  /* For each shared vehicles add it to the group */
563  for (Vehicle *v2 = v->FirstShared(); v2 != nullptr; v2 = v2->NextShared()) {
564  if (v2->group_id != id_g) Command<CMD_ADD_VEHICLE_GROUP>::Do(flags, id_g, v2->index, false);
565  }
566  }
567  }
568 
570  }
571 
572  return CommandCost();
573 }
574 
575 
583 {
584  Group *g = Group::GetIfValid(group_id);
585 
586  if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
587 
588  if (flags & DC_EXEC) {
589  /* Find each Vehicle that belongs to the group old_g and add it to the default group */
590  for (const Vehicle *v : Vehicle::Iterate()) {
591  if (v->IsPrimaryVehicle()) {
592  if (v->group_id != group_id) continue;
593 
594  /* Add The Vehicle to the default group */
595  Command<CMD_ADD_VEHICLE_GROUP>::Do(flags, DEFAULT_GROUP, v->index, false);
596  }
597  }
598 
600  }
601 
602  return CommandCost();
603 }
604 
612 CommandCost CmdSetGroupLivery(DoCommandFlag flags, GroupID group_id, bool primary, Colours colour)
613 {
614  Group *g = Group::GetIfValid(group_id);
615 
616  if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
617 
618  if (colour >= COLOUR_END && colour != INVALID_COLOUR) return CMD_ERROR;
619 
620  if (flags & DC_EXEC) {
621  if (primary) {
622  SB(g->livery.in_use, 0, 1, colour != INVALID_COLOUR);
623  if (colour == INVALID_COLOUR) colour = (Colours)GetParentLivery(g)->colour1;
624  g->livery.colour1 = colour;
625  } else {
626  SB(g->livery.in_use, 1, 1, colour != INVALID_COLOUR);
627  if (colour == INVALID_COLOUR) colour = (Colours)GetParentLivery(g)->colour2;
628  g->livery.colour2 = colour;
629  }
630 
633  }
634 
635  return CommandCost();
636 }
637 
643 static void SetGroupFlag(Group *g, GroupFlags flag, bool set, bool children)
644 {
645  if (set) {
646  SetBit(g->flags, flag);
647  } else {
648  ClrBit(g->flags, flag);
649  }
650 
651  if (!children) return;
652 
653  for (Group *pg : Group::Iterate()) {
654  if (pg->parent == g->index) SetGroupFlag(pg, flag, set, true);
655  }
656 }
657 
667 CommandCost CmdSetGroupFlag(DoCommandFlag flags, GroupID group_id, GroupFlags flag, bool value, bool recursive)
668 {
669  Group *g = Group::GetIfValid(group_id);
670  if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
671 
672  if (flag >= GroupFlags::GF_END) return CMD_ERROR;
673 
674  if (flags & DC_EXEC) {
675  SetGroupFlag(g, flag, value, recursive);
676 
679  }
680 
681  return CommandCost();
682 }
683 
690 {
691  if (!v->IsPrimaryVehicle()) return;
692 
693  if (!IsDefaultGroupID(v->group_id)) GroupStatistics::CountVehicle(v, -1);
694 }
695 
696 
704 {
705  if (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g)) return;
706 
707  assert(v->IsFrontEngine() || IsDefaultGroupID(new_g));
708 
709  for (Vehicle *u = v; u != nullptr; u = u->Next()) {
710  if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
711 
712  u->group_id = new_g;
713  u->colourmap = PAL_NONE;
714  u->InvalidateNewGRFCache();
715  u->UpdateViewport(true);
716  }
717 
718  /* Update the Replace Vehicle Windows */
721 }
722 
723 
732 {
733  assert(v->IsFrontEngine() || v->IsFreeWagon());
734 
735  GroupID new_g = v->IsFrontEngine() ? v->group_id : (GroupID)DEFAULT_GROUP;
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  }
743 
744  /* Update the Replace Vehicle Windows */
747 }
748 
757 uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e)
758 {
759  uint count = 0;
760  const Engine *e = Engine::Get(id_e);
761  for (const Group *g : Group::Iterate()) {
762  if (g->parent == id_g) count += GetGroupNumEngines(company, g->index, id_e);
763  }
764  return count + GroupStatistics::Get(company, id_g, e->type).num_engines[id_e];
765 }
766 
776 {
777  uint count = 0;
778  for (const Group *g : Group::Iterate()) {
779  if (g->parent == id_g) count += GetGroupNumVehicle(company, g->index, type);
780  }
781  return count + GroupStatistics::Get(company, id_g, type).num_vehicle;
782 }
783 
793 {
794  uint count = 0;
795  for (const Group *g : Group::Iterate()) {
796  if (g->parent == id_g) count += GetGroupNumProfitVehicle(company, g->index, type);
797  }
798  return count + GroupStatistics::Get(company, id_g, type).num_profit_vehicle;
799 }
800 
810 {
811  Money sum = 0;
812  for (const Group *g : Group::Iterate()) {
813  if (g->parent == id_g) sum += GetGroupProfitLastYear(company, g->index, type);
814  }
815  return sum + GroupStatistics::Get(company, id_g, type).profit_last_year;
816 }
817 
818 void RemoveAllGroupsForCompany(const CompanyID company)
819 {
820  for (Group *g : Group::Iterate()) {
821  if (company == g->owner) delete g;
822  }
823 }
824 
825 
832 bool GroupIsInGroup(GroupID search, GroupID group)
833 {
834  if (!Group::IsValidID(search)) return search == group;
835 
836  do {
837  if (search == group) return true;
838  search = Group::Get(search)->parent;
839  } while (search != INVALID_GROUP);
840 
841  return false;
842 }
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:551
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:3241
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:3143
GroupStatistics::CountEngine
static void CountEngine(const Vehicle *v, int delta)
Update num_engines when adding/removing an engine.
Definition: group_cmd.cpp:154
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:299
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:344
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
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:792
Group::parent
GroupID parent
Parent group.
Definition: group.h:83
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:235
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:131
ClrBit
static T ClrBit(T &x, const uint8 y)
Clears a bit in a variable.
Definition: bitmath_func.hpp:151
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:78
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:27
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:33
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:356
Group::vehicle_type
VehicleType vehicle_type
Vehicle type of the group.
Definition: group.h:75
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:354
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:703
UpdateTrainGroupID
void UpdateTrainGroupID(Train *v)
Recalculates the groupID of a train.
Definition: group_cmd.cpp:731
ALL_GROUP
static const GroupID ALL_GROUP
All vehicles are in this group.
Definition: group_type.h:16
Group
Group data.
Definition: group.h:72
CompanySettings::renew_keep_length
bool renew_keep_length
sell some wagons if after autoreplace the train is longer than before
Definition: settings_type.h:577
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:65
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:79
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:775
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:809
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:74
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:263
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:202
Train
'Train' is either a loco or a wagon.
Definition: train.h:86
GroupStatistics::Get
static GroupStatistics & Get(CompanyID company, GroupID id_g, VehicleType type)
Returns the GroupStatistics for a specific group.
Definition: group_cmd.cpp:61
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
GroupStatistics::GetAllGroup
static GroupStatistics & GetAllGroup(const Vehicle *v)
Returns the GroupStatistic for the ALL_GROUPO of a vehicle type.
Definition: group_cmd.cpp:91
GroupStatistics::VehicleReachedProfitAge
static void VehicleReachedProfitAge(const Vehicle *v)
Add a vehicle to the profit sum of its group.
Definition: group_cmd.cpp:164
IsAllGroupID
static bool IsAllGroupID(GroupID id_g)
Checks if a GroupID stands for all vehicles of a company.
Definition: group.h:99
_group_pool
GroupPool _group_pool("Group")
Pool of groups.
Group::flags
uint8 flags
Group flags.
Definition: group.h:77
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:582
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
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:667
GetWindowClassForVehicleType
static WindowClass GetWindowClassForVehicleType(VehicleType vt)
Get WindowClass for vehicle list of given vehicle type.
Definition: vehicle_gui.h:93
CmdSetGroupLivery
CommandCost CmdSetGroupLivery(DoCommandFlag flags, GroupID group_id, bool primary, Colours colour)
Set the livery for a vehicle group.
Definition: group_cmd.cpp:612
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:689
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:178
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:643
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:757
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:3258
CmdAlterGroup
CommandCost CmdAlterGroup(DoCommandFlag flags, AlterGroupMode mode, GroupID group_id, GroupID parent_id, const std::string &text)
Alter a group.
Definition: group_cmd.cpp:395
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:832
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:214
Group::name
std::string name
Group Name.
Definition: group.h:73
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:497
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:1752
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:1176
Group::folded
bool folded
NOSAVE: Is this group folded in the group view?
Definition: group.h:81
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:67
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:463
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:46
GroupStatistics::UpdateAfterLoad
static void UpdateAfterLoad()
Update all caches after loading a game, changing NewGRF, etc.
Definition: group_cmd.cpp:99
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