OpenTTD
group_cmd.cpp
Go to the documentation of this file.
1 /* $Id: group_cmd.cpp 27090 2014-12-24 16:49:57Z frosch $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "stdafx.h"
13 #include "cmd_helper.h"
14 #include "command_func.h"
15 #include "train.h"
16 #include "vehiclelist.h"
17 #include "vehicle_func.h"
18 #include "autoreplace_base.h"
19 #include "autoreplace_func.h"
20 #include "string_func.h"
21 #include "company_func.h"
22 #include "core/pool_func.hpp"
23 #include "order_backup.h"
24 
25 #include "table/strings.h"
26 
27 #include "safeguards.h"
28 
29 GroupID _new_group_id;
30 
31 GroupPool _group_pool("Group");
33 
34 GroupStatistics::GroupStatistics()
35 {
36  this->num_engines = CallocT<uint16>(Engine::GetPoolSize());
37 }
38 
39 GroupStatistics::~GroupStatistics()
40 {
41  free(this->num_engines);
42 }
43 
48 {
49  this->num_vehicle = 0;
50  this->num_profit_vehicle = 0;
51  this->profit_last_year = 0;
52 
53  /* This is also called when NewGRF change. So the number of engines might have changed. Reallocate. */
54  free(this->num_engines);
55  this->num_engines = CallocT<uint16>(Engine::GetPoolSize());
56 }
57 
66 {
67  if (Group::IsValidID(id_g)) {
68  Group *g = Group::Get(id_g);
69  assert(g->owner == company);
70  assert(g->vehicle_type == type);
71  return g->statistics;
72  }
73 
74  if (IsDefaultGroupID(id_g)) return Company::Get(company)->group_default[type];
75  if (IsAllGroupID(id_g)) return Company::Get(company)->group_all[type];
76 
77  NOT_REACHED();
78 }
79 
86 {
87  return GroupStatistics::Get(v->owner, v->group_id, v->type);
88 }
89 
96 {
97  return GroupStatistics::Get(v->owner, ALL_GROUP, v->type);
98 }
99 
104 {
105  /* Set up the engine count for all companies */
106  Company *c;
107  FOR_ALL_COMPANIES(c) {
108  for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
109  c->group_all[type].Clear();
110  c->group_default[type].Clear();
111  }
112  }
113 
114  /* Recalculate */
115  Group *g;
116  FOR_ALL_GROUPS(g) {
117  g->statistics.Clear();
118  }
119 
120  const Vehicle *v;
121  FOR_ALL_VEHICLES(v) {
122  if (!v->IsEngineCountable()) continue;
123 
126  }
127 
128  FOR_ALL_COMPANIES(c) {
130  }
131 }
132 
138 /* static */ void GroupStatistics::CountVehicle(const Vehicle *v, int delta)
139 {
140  assert(delta == 1 || delta == -1);
141 
144 
145  stats_all.num_vehicle += delta;
146  stats.num_vehicle += delta;
147 
148  if (v->age > VEHICLE_PROFIT_MIN_AGE) {
149  stats_all.num_profit_vehicle += delta;
150  stats_all.profit_last_year += v->GetDisplayProfitLastYear() * delta;
151  stats.num_profit_vehicle += delta;
152  stats.profit_last_year += v->GetDisplayProfitLastYear() * delta;
153  }
154 }
155 
161 /* static */ void GroupStatistics::CountEngine(const Vehicle *v, int delta)
162 {
163  assert(delta == 1 || delta == -1);
166 }
167 
172 {
175 
176  stats_all.num_profit_vehicle++;
177  stats_all.profit_last_year += v->GetDisplayProfitLastYear();
178  stats.num_profit_vehicle++;
180 }
181 
186 {
187  /* Set up the engine count for all companies */
188  Company *c;
189  FOR_ALL_COMPANIES(c) {
190  for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
191  c->group_all[type].ClearProfits();
192  c->group_default[type].ClearProfits();
193  }
194  }
195 
196  /* Recalculate */
197  Group *g;
198  FOR_ALL_GROUPS(g) {
199  g->statistics.ClearProfits();
200  }
201 
202  const Vehicle *v;
203  FOR_ALL_VEHICLES(v) {
205  }
206 }
207 
213 {
214  /* Set up the engine count for all companies */
215  Company *c = Company::Get(company);
216  for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
217  c->group_all[type].ClearAutoreplace();
218  c->group_default[type].ClearAutoreplace();
219  }
220 
221  /* Recalculate */
222  Group *g;
223  FOR_ALL_GROUPS(g) {
224  if (g->owner != company) continue;
225  g->statistics.ClearAutoreplace();
226  }
227 
228  for (EngineRenewList erl = c->engine_renew_list; erl != NULL; erl = erl->next) {
229  const Engine *e = Engine::Get(erl->from);
230  GroupStatistics &stats = GroupStatistics::Get(company, erl->group_id, e->type);
231  if (!stats.autoreplace_defined) {
232  stats.autoreplace_defined = true;
233  stats.autoreplace_finished = true;
234  }
235  if (stats.num_engines[erl->from] > 0) stats.autoreplace_finished = false;
236  }
237 }
238 
246 static inline void UpdateNumEngineGroup(const Vehicle *v, GroupID old_g, GroupID new_g)
247 {
248  if (old_g != new_g) {
249  /* Decrease the num engines in the old group */
251 
252  /* Increase the num engines in the new group */
254  }
255 }
256 
257 
258 
259 Group::Group(Owner owner)
260 {
261  this->owner = owner;
262 }
263 
264 Group::~Group()
265 {
266  free(this->name);
267 }
268 
269 
279 CommandCost CmdCreateGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
280 {
281  VehicleType vt = Extract<VehicleType, 0, 3>(p1);
282  if (!IsCompanyBuildableVehicleType(vt)) return CMD_ERROR;
283 
284  if (!Group::CanAllocateItem()) return CMD_ERROR;
285 
286  if (flags & DC_EXEC) {
287  Group *g = new Group(_current_company);
288  g->replace_protection = false;
289  g->vehicle_type = vt;
290  g->parent = INVALID_GROUP;
291 
292  _new_group_id = g->index;
293 
295  }
296 
297  return CommandCost();
298 }
299 
300 
311 CommandCost CmdDeleteGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
312 {
313  Group *g = Group::GetIfValid(p1);
314  if (g == NULL || g->owner != _current_company) return CMD_ERROR;
315 
316  /* Remove all vehicles from the group */
317  DoCommand(0, p1, 0, flags, CMD_REMOVE_ALL_VEHICLES_GROUP);
318 
319  /* Delete sub-groups */
320  Group *gp;
321  FOR_ALL_GROUPS(gp) {
322  if (gp->parent == g->index) {
323  DoCommand(0, gp->index, 0, flags, CMD_DELETE_GROUP);
324  }
325  }
326 
327  if (flags & DC_EXEC) {
328  /* Update backupped orders if needed */
330 
331  /* If we set an autoreplace for the group we delete, remove it. */
333  Company *c;
334  EngineRenew *er;
335 
337  FOR_ALL_ENGINE_RENEWS(er) {
338  if (er->group_id == g->index) RemoveEngineReplacementForCompany(c, er->from, g->index, flags);
339  }
340  }
341 
342  VehicleType vt = g->vehicle_type;
343 
344  /* Delete the Replace Vehicle Windows */
346  delete g;
347 
349  }
350 
351  return CommandCost();
352 }
353 
354 static bool IsUniqueGroupNameForVehicleType(const char *name, VehicleType type)
355 {
356  const Group *g;
357 
358  FOR_ALL_GROUPS(g) {
359  if (g->name != NULL && g->vehicle_type == type && strcmp(g->name, name) == 0) return false;
360  }
361 
362  return true;
363 }
364 
377 CommandCost CmdAlterGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
378 {
379  Group *g = Group::GetIfValid(GB(p1, 0, 16));
380  if (g == NULL || g->owner != _current_company) return CMD_ERROR;
381 
382  if (!HasBit(p1, 16)) {
383  /* Rename group */
384  bool reset = StrEmpty(text);
385 
386  if (!reset) {
388  if (!IsUniqueGroupNameForVehicleType(text, g->vehicle_type)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
389  }
390 
391  if (flags & DC_EXEC) {
392  /* Delete the old name */
393  free(g->name);
394  /* Assign the new one */
395  g->name = reset ? NULL : stredup(text);
396  }
397  } else {
398  /* Set group parent */
399  const Group *pg = Group::GetIfValid(GB(p2, 0, 16));
400 
401  if (pg != NULL) {
402  if (pg->owner != _current_company) return CMD_ERROR;
403  if (pg->vehicle_type != g->vehicle_type) return CMD_ERROR;
404 
405  /* Ensure request parent isn't child of group.
406  * This is the only place that infinite loops are prevented. */
407  if (GroupIsInGroup(pg->index, g->index)) return CMD_ERROR;
408  }
409 
410  if (flags & DC_EXEC) {
411  g->parent = (pg == NULL) ? INVALID_GROUP : pg->index;
412  }
413  }
414 
415  if (flags & DC_EXEC) {
418  }
419 
420  return CommandCost();
421 }
422 
423 
429 static void AddVehicleToGroup(Vehicle *v, GroupID new_g)
430 {
432 
433  switch (v->type) {
434  default: NOT_REACHED();
435  case VEH_TRAIN:
436  SetTrainGroupID(Train::From(v), new_g);
437  break;
438 
439  case VEH_ROAD:
440  case VEH_SHIP:
441  case VEH_AIRCRAFT:
442  if (v->IsEngineCountable()) UpdateNumEngineGroup(v, v->group_id, new_g);
443  v->group_id = new_g;
444  break;
445  }
446 
448 }
449 
462 CommandCost CmdAddVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
463 {
464  Vehicle *v = Vehicle::GetIfValid(GB(p2, 0, 20));
465  GroupID new_g = p1;
466 
467  if (v == NULL || (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g) && new_g != NEW_GROUP)) return CMD_ERROR;
468 
469  if (Group::IsValidID(new_g)) {
470  Group *g = Group::Get(new_g);
471  if (g->owner != _current_company || g->vehicle_type != v->type) return CMD_ERROR;
472  }
473 
474  if (v->owner != _current_company || !v->IsPrimaryVehicle()) return CMD_ERROR;
475 
476  if (new_g == NEW_GROUP) {
477  /* Create new group. */
478  CommandCost ret = CmdCreateGroup(0, flags, v->type, 0, NULL);
479  if (ret.Failed()) return ret;
480 
481  new_g = _new_group_id;
482  }
483 
484  if (flags & DC_EXEC) {
485  AddVehicleToGroup(v, new_g);
486 
487  if (HasBit(p2, 31)) {
488  /* Add vehicles in the shared order list as well. */
489  for (Vehicle *v2 = v->FirstShared(); v2 != NULL; v2 = v2->NextShared()) {
490  if (v2->group_id != new_g) AddVehicleToGroup(v2, new_g);
491  }
492  }
493 
495 
496  /* Update the Replace Vehicle Windows */
499  }
500 
501  return CommandCost();
502 }
503 
514 CommandCost CmdAddSharedVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
515 {
516  VehicleType type = Extract<VehicleType, 0, 3>(p2);
517  GroupID id_g = p1;
518  if (!Group::IsValidID(id_g) || !IsCompanyBuildableVehicleType(type)) return CMD_ERROR;
519 
520  if (flags & DC_EXEC) {
521  Vehicle *v;
522 
523  /* Find the first front engine which belong to the group id_g
524  * then add all shared vehicles of this front engine to the group id_g */
525  FOR_ALL_VEHICLES(v) {
526  if (v->type == type && v->IsPrimaryVehicle()) {
527  if (v->group_id != id_g) continue;
528 
529  /* For each shared vehicles add it to the group */
530  for (Vehicle *v2 = v->FirstShared(); v2 != NULL; v2 = v2->NextShared()) {
531  if (v2->group_id != id_g) DoCommand(tile, id_g, v2->index, flags, CMD_ADD_VEHICLE_GROUP, text);
532  }
533  }
534  }
535 
537  }
538 
539  return CommandCost();
540 }
541 
542 
553 CommandCost CmdRemoveAllVehiclesGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
554 {
555  GroupID old_g = p1;
556  Group *g = Group::GetIfValid(old_g);
557 
558  if (g == NULL || g->owner != _current_company) return CMD_ERROR;
559 
560  if (flags & DC_EXEC) {
561  Vehicle *v;
562 
563  /* Find each Vehicle that belongs to the group old_g and add it to the default group */
564  FOR_ALL_VEHICLES(v) {
565  if (v->IsPrimaryVehicle()) {
566  if (v->group_id != old_g) continue;
567 
568  /* Add The Vehicle to the default group */
569  DoCommand(tile, DEFAULT_GROUP, v->index, flags, CMD_ADD_VEHICLE_GROUP, text);
570  }
571  }
572 
574  }
575 
576  return CommandCost();
577 }
578 
584 static void SetGroupReplaceProtection(Group *g, bool protect)
585 {
586  g->replace_protection = protect;
587 
588  Group *pg;
589  FOR_ALL_GROUPS(pg) {
590  if (pg->parent == g->index) SetGroupReplaceProtection(pg, protect);
591  }
592 }
593 
606 CommandCost CmdSetGroupReplaceProtection(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
607 {
608  Group *g = Group::GetIfValid(p1);
609  if (g == NULL || g->owner != _current_company) return CMD_ERROR;
610 
611  if (flags & DC_EXEC) {
612  if (HasBit(p2, 1)) {
614  } else {
615  g->replace_protection = HasBit(p2, 0);
616  }
617 
620  }
621 
622  return CommandCost();
623 }
624 
631 {
632  if (!v->IsPrimaryVehicle()) return;
633 
634  if (!IsDefaultGroupID(v->group_id)) GroupStatistics::CountVehicle(v, -1);
635 }
636 
637 
645 {
646  if (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g)) return;
647 
648  assert(v->IsFrontEngine() || IsDefaultGroupID(new_g));
649 
650  for (Vehicle *u = v; u != NULL; u = u->Next()) {
651  if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
652 
653  u->group_id = new_g;
654  }
655 
656  /* Update the Replace Vehicle Windows */
659 }
660 
661 
670 {
671  assert(v->IsFrontEngine() || v->IsFreeWagon());
672 
673  GroupID new_g = v->IsFrontEngine() ? v->group_id : (GroupID)DEFAULT_GROUP;
674  for (Vehicle *u = v; u != NULL; u = u->Next()) {
675  if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
676 
677  u->group_id = new_g;
678  }
679 
680  /* Update the Replace Vehicle Windows */
683 }
684 
693 uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e)
694 {
695  uint count = 0;
696  const Engine *e = Engine::Get(id_e);
697  const Group *g;
698  FOR_ALL_GROUPS(g) {
699  if (g->parent == id_g) count += GetGroupNumEngines(company, g->index, id_e);
700  }
701  return count + GroupStatistics::Get(company, id_g, e->type).num_engines[id_e];
702 }
703 
704 void RemoveAllGroupsForCompany(const CompanyID company)
705 {
706  Group *g;
707 
708  FOR_ALL_GROUPS(g) {
709  if (company == g->owner) delete g;
710  }
711 }
712 
713 
720 bool GroupIsInGroup(GroupID search, GroupID group)
721 {
722  if (!Group::IsValidID(search)) return search == group;
723 
724  do {
725  if (search == group) return true;
726  search = Group::Get(search)->parent;
727  } while (search != INVALID_GROUP);
728 
729  return false;
730 }
static void UpdateAfterLoad()
Update all caches after loading a game, changing NewGRF etc.
Definition: group_cmd.cpp:103
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:257
bool replace_protection
If set to true, the global autoreplace have no effect on the group.
Definition: group.h:71
The information about a vehicle list.
Definition: vehiclelist.h:31
static const int VEHICLE_PROFIT_MIN_AGE
Only vehicles older than this have a meaningful profit.
Definition: vehicle_func.h:29
GroupStatistics statistics
NOSAVE: Statistics and caches on the vehicles in the group.
Definition: group.h:72
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3120
static void AddVehicleToGroup(Vehicle *v, GroupID new_g)
Do add a vehicle to a group.
Definition: group_cmd.cpp:429
CommandCost CmdCreateGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Create a new vehicle group.
Definition: group_cmd.cpp:279
Functions and type for generating vehicle lists.
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:246
Base for the train class.
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:606
EngineRenewList engine_renew_list
Engine renewals of this company.
Definition: company_base.h:120
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:25
void SetTrainGroupID(Train *v, GroupID new_g)
Affect the groupID of a train to new_g.
Definition: group_cmd.cpp:644
void UpdateTrainGroupID(Train *v)
Recalculates the groupID of a train.
Definition: group_cmd.cpp:669
Replace vehicle window; Window numbers:
Definition: window_type.h:213
Last company-ownable type.
Definition: vehicle_type.h:29
uint16 * num_engines
Caches the number of engines of each type the company owns.
Definition: group.h:27
Functions related to vehicles.
Vehicle data structure.
Definition: vehicle_base.h:212
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
Money profit_last_year
Sum of profits for all vehicles.
Definition: group.h:33
Helper functions to extract data from command parameters.
bool autoreplace_finished
Have all autoreplacement finished?
Definition: group.h:30
uint16 num_profit_vehicle
Number of vehicles considered for profit statistics;.
Definition: group.h:32
static GroupStatistics & GetAllGroup(const Vehicle *v)
Returns the GroupStatistic for the ALL_GROUPO of a vehicle type.
Definition: group_cmd.cpp:95
Common return value for all commands.
Definition: command_type.h:25
static Train * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
delete a group
Definition: command_type.h:317
Vehicle * FirstShared() const
Get the first vehicle of this vehicle chain.
Definition: vehicle_base.h:676
static const GroupID NEW_GROUP
Sentinel for a to-be-created group.
Definition: group_type.h:17
static void UpdateNumEngineGroup(const Vehicle *v, GroupID old_g, GroupID new_g)
Update the num engines of a groupID.
Definition: group_cmd.cpp:246
static bool IsAllGroupID(GroupID id_g)
Checks if a GroupID stands for all vehicles of a company.
Definition: group.h:91
Aircraft vehicle type.
Definition: vehicle_type.h:27
static const uint MAX_LENGTH_GROUP_NAME_CHARS
The maximum length of a group name in characters including &#39;\0&#39;.
Definition: group_type.h:22
Functions related to low-level strings.
Some methods of Pool are placed here in order to reduce compilation time and binary size...
GroupStatistics group_default[VEH_COMPANY_END]
NOSAVE: Statistics for the DEFAULT_GROUP group.
Definition: company_base.h:123
GroupPool _group_pool("Group")
Pool of groups.
virtual bool IsPrimaryVehicle() const
Whether this is the primary vehicle in the chain.
Definition: vehicle_base.h:434
remove all vehicles from a group
Definition: command_type.h:321
CommandCost DoCommand(const CommandContainer *container, DoCommandFlag flags)
Shorthand for calling the long DoCommand with a container.
Definition: command.cpp:436
static size_t GetPoolSize()
Returns first unused index.
Definition: pool_type.hpp:267
Statistics and caches on the vehicles in a group.
Definition: group.h:25
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
char * name
Group Name.
Definition: group.h:67
DoCommandFlag
List of flags for a command.
Definition: command_type.h:340
T * Next() const
Get next vehicle in the chain.
Definition of base types and functions in a cross-platform compatible way.
void RemoveVehicleFromGroup(const Vehicle *v)
Decrease the num_vehicle variable before delete an front engine from a group.
Definition: group_cmd.cpp:630
A number of safeguards to prevent using unsafe methods.
uint16 GroupID
Type for all group identifiers.
Definition: group_type.h:15
VehicleType type
Vehicle type, ie VEH_ROAD, VEH_TRAIN, etc.
Definition: engine_base.h:42
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:126
VehicleTypeByte vehicle_type
Vehicle type of the group.
Definition: group.h:69
GroupStatistics group_all[VEH_COMPANY_END]
NOSAVE: Statistics for the ALL_GROUP group.
Definition: company_base.h:122
Functions related to order backups.
bool IsFrontEngine() const
Check if the vehicle is a front engine.
Definition: vehicle_base.h:882
static GroupStatistics & Get(CompanyID company, GroupID id_g, VehicleType type)
Returns the GroupStatistics for a specific group.
Definition: group_cmd.cpp:65
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:693
static void CountVehicle(const Vehicle *v, int delta)
Update num_vehicle when adding or removing a vehicle.
Definition: group_cmd.cpp:138
static void SetGroupReplaceProtection(Group *g, bool protect)
Set replace protection for a group and its sub-groups.
Definition: group_cmd.cpp:584
static const GroupID INVALID_GROUP
Sentinel for invalid groups.
Definition: group_type.h:20
void Clear()
Clear all caches.
Definition: group_cmd.cpp:47
uint16 num_vehicle
Number of vehicles.
Definition: group.h:26
CommandCost CmdRemoveAllVehiclesGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Remove all vehicles from a group.
Definition: group_cmd.cpp:553
Road vehicle type.
Definition: vehicle_type.h:25
bool Failed() const
Did this command fail?
Definition: command_type.h:161
Maximum number of companies.
Definition: company_type.h:25
#define return_cmd_error(errcode)
Returns from a function with a specific StringID as error.
Definition: command_func.h:35
Base class for all pools.
Definition: pool_type.hpp:83
Ship vehicle type.
Definition: vehicle_type.h:26
&#39;Train&#39; is either a loco or a wagon.
Definition: train.h:88
bool IsEngineCountable() const
Check if a vehicle is counted in num_engines in each company struct.
Definition: vehicle.cpp:710
bool GroupIsInGroup(GroupID search, GroupID group)
Test if GroupID group is a descendant of (or is) GroupID search.
Definition: group_cmd.cpp:720
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don&#39;t get linker errors.
Definition: pool_func.hpp:224
void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
Delete a window by its class and window number (if it is open).
Definition: window.cpp:1137
static void UpdateProfits()
Recompute the profits for all groups.
Definition: group_cmd.cpp:185
execute the given command
Definition: command_type.h:342
static const GroupID DEFAULT_GROUP
Ungrouped vehicles are in this group.
Definition: group_type.h:19
Functions related to companies.
CommandCost CmdAlterGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Alter a group.
Definition: group_cmd.cpp:377
add a vehicle to a group
Definition: command_type.h:319
GroupID parent
Parent group.
Definition: group.h:74
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:59
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:300
uint16 EngineID
Unique identification number of an engine.
Definition: engine_type.h:22
CompanyByte _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
uint32 Pack() const
Pack a VehicleListIdentifier in a single uint32.
Definition: vehiclelist.cpp:23
bool autoreplace_defined
Are any autoreplace rules set?
Definition: group.h:29
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:311
Struct to store engine replacements.
OwnerByte owner
Which company owns the vehicle?
Definition: vehicle_base.h:273
static void UpdateAutoreplace(CompanyID company)
Update autoreplace_defined and autoreplace_finished of all statistics of a company.
Definition: group_cmd.cpp:212
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
static void ClearGroup(GroupID group)
Clear the group of all backups having this group ID.
Group data.
Definition: group.h:66
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() ...
Definition: pool_type.hpp:216
Functions related to commands.
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-NULL) Titem.
Definition: pool_type.hpp:235
static void CountEngine(const Vehicle *v, int delta)
Update num_engines when adding/removing an engine.
Definition: group_cmd.cpp:161
static WindowClass GetWindowClassForVehicleType(VehicleType vt)
Get WindowClass for vehicle list of given vehicle type.
Definition: vehicle_gui.h:85
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:114
bool IsFreeWagon() const
Check if the vehicle is a free wagon (got no engine in front of it).
EngineID engine_type
The type of engine used for this vehicle.
Definition: vehicle_base.h:288
OwnerByte owner
Group Owner.
Definition: group.h:68
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
CommandCost CmdAddVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Add a vehicle to a group.
Definition: group_cmd.cpp:462
#define FOR_ALL_VEHICLES(var)
Iterate over all vehicles.
Definition: vehicle_base.h:986
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:514
static void VehicleReachedProfitAge(const Vehicle *v)
Add a vehicle to the profit sum of its group.
Definition: group_cmd.cpp:171
Base class for autoreplaces/autorenews.
Vehicle * NextShared() const
Get the next vehicle of the shared vehicle chain.
Definition: vehicle_base.h:664
Owner
Enum for all companies/owners.
Definition: company_type.h:20
static CommandCost RemoveEngineReplacementForCompany(Company *c, EngineID engine, GroupID group, DoCommandFlag flags)
Remove an engine replacement for the company.
Functions related to autoreplacing.
Date age
Age in days.
Definition: vehicle_base.h:258
static bool IsCompanyBuildableVehicleType(VehicleType type)
Is the given vehicle type buildable by a company?
Definition: vehicle_func.h:91
VehicleTypeByte type
Type of vehicle.
Definition: vehicle_type.h:54
Money GetDisplayProfitLastYear() const
Gets the profit vehicle had last year.
Definition: vehicle_base.h:573
GroupID group_id
Index of group Pool array.
Definition: vehicle_base.h:326
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:3220
Train vehicle type.
Definition: vehicle_type.h:24
static const GroupID ALL_GROUP
All vehicles are in this group.
Definition: group_type.h:18