OpenTTD
group_cmd.cpp
Go to the documentation of this file.
1 /* $Id$ */
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 (GetGroupNumEngines(company, erl->group_id, 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 const Livery *GetParentLivery(const Group *g)
259 {
260  if (g->parent == INVALID_GROUP) {
261  const Company *c = Company::Get(g->owner);
262  return &c->livery[LS_DEFAULT];
263  }
264 
265  const Group *pg = Group::Get(g->parent);
266  return &pg->livery;
267 }
268 
269 
275 {
276  /* Company colour data is indirectly cached. */
277  Vehicle *v;
278  FOR_ALL_VEHICLES(v) {
279  if (v->group_id == g->index && (!v->IsGroundVehicle() || v->IsFrontEngine())) {
280  for (Vehicle *u = v; u != NULL; u = u->Next()) {
281  u->colourmap = PAL_NONE;
282  u->InvalidateNewGRFCache();
283  }
284  }
285  }
286 
287  Group *cg;
288  FOR_ALL_GROUPS(cg) {
289  if (cg->parent == g->index) {
290  if (!HasBit(cg->livery.in_use, 0)) cg->livery.colour1 = g->livery.colour1;
291  if (!HasBit(cg->livery.in_use, 1)) cg->livery.colour2 = g->livery.colour2;
293  }
294  }
295 }
296 
297 
298 Group::Group(Owner owner)
299 {
300  this->owner = owner;
301 }
302 
303 Group::~Group()
304 {
305  free(this->name);
306 }
307 
308 
318 CommandCost CmdCreateGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
319 {
320  VehicleType vt = Extract<VehicleType, 0, 3>(p1);
321  if (!IsCompanyBuildableVehicleType(vt)) return CMD_ERROR;
322 
323  if (!Group::CanAllocateItem()) return CMD_ERROR;
324 
325  const Group *pg = Group::GetIfValid(GB(p2, 0, 16));
326  if (pg != NULL) {
327  if (pg->owner != _current_company) return CMD_ERROR;
328  if (pg->vehicle_type != vt) return CMD_ERROR;
329  }
330 
331  if (flags & DC_EXEC) {
332  Group *g = new Group(_current_company);
333  g->replace_protection = false;
334  g->vehicle_type = vt;
335  g->parent = INVALID_GROUP;
336 
337  if (pg == NULL) {
339  g->livery.colour1 = c->livery[LS_DEFAULT].colour1;
340  g->livery.colour2 = c->livery[LS_DEFAULT].colour2;
341  } else {
342  g->parent = pg->index;
343  g->livery.colour1 = pg->livery.colour1;
344  g->livery.colour2 = pg->livery.colour2;
345  }
346 
347  _new_group_id = g->index;
348 
351  }
352 
353  return CommandCost();
354 }
355 
356 
367 CommandCost CmdDeleteGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
368 {
369  Group *g = Group::GetIfValid(p1);
370  if (g == NULL || g->owner != _current_company) return CMD_ERROR;
371 
372  /* Remove all vehicles from the group */
373  DoCommand(0, p1, 0, flags, CMD_REMOVE_ALL_VEHICLES_GROUP);
374 
375  /* Delete sub-groups */
376  Group *gp;
377  FOR_ALL_GROUPS(gp) {
378  if (gp->parent == g->index) {
379  DoCommand(0, gp->index, 0, flags, CMD_DELETE_GROUP);
380  }
381  }
382 
383  if (flags & DC_EXEC) {
384  /* Update backupped orders if needed */
386 
387  /* If we set an autoreplace for the group we delete, remove it. */
389  Company *c;
390  EngineRenew *er;
391 
393  FOR_ALL_ENGINE_RENEWS(er) {
394  if (er->group_id == g->index) RemoveEngineReplacementForCompany(c, er->from, g->index, flags);
395  }
396  }
397 
398  VehicleType vt = g->vehicle_type;
399 
400  /* Delete the Replace Vehicle Windows */
402  delete g;
403 
406  }
407 
408  return CommandCost();
409 }
410 
423 CommandCost CmdAlterGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
424 {
425  Group *g = Group::GetIfValid(GB(p1, 0, 16));
426  if (g == NULL || g->owner != _current_company) return CMD_ERROR;
427 
428  if (!HasBit(p1, 16)) {
429  /* Rename group */
430  bool reset = StrEmpty(text);
431 
432  if (!reset) {
434  }
435 
436  if (flags & DC_EXEC) {
437  /* Delete the old name */
438  free(g->name);
439  /* Assign the new one */
440  g->name = reset ? NULL : stredup(text);
441  }
442  } else {
443  /* Set group parent */
444  const Group *pg = Group::GetIfValid(GB(p2, 0, 16));
445 
446  if (pg != NULL) {
447  if (pg->owner != _current_company) return CMD_ERROR;
448  if (pg->vehicle_type != g->vehicle_type) return CMD_ERROR;
449 
450  /* Ensure request parent isn't child of group.
451  * This is the only place that infinite loops are prevented. */
452  if (GroupIsInGroup(pg->index, g->index)) return CMD_ERROR;
453  }
454 
455  if (flags & DC_EXEC) {
456  g->parent = (pg == NULL) ? INVALID_GROUP : pg->index;
458 
459  if (g->livery.in_use == 0) {
460  const Livery *livery = GetParentLivery(g);
461  g->livery.colour1 = livery->colour1;
462  g->livery.colour2 = livery->colour2;
463 
466  }
467  }
468  }
469 
470  if (flags & DC_EXEC) {
474  }
475 
476  return CommandCost();
477 }
478 
479 
485 static void AddVehicleToGroup(Vehicle *v, GroupID new_g)
486 {
488 
489  switch (v->type) {
490  default: NOT_REACHED();
491  case VEH_TRAIN:
492  SetTrainGroupID(Train::From(v), new_g);
493  break;
494 
495  case VEH_ROAD:
496  case VEH_SHIP:
497  case VEH_AIRCRAFT:
498  if (v->IsEngineCountable()) UpdateNumEngineGroup(v, v->group_id, new_g);
499  v->group_id = new_g;
500  for (Vehicle *u = v; u != NULL; u = u->Next()) {
501  u->colourmap = PAL_NONE;
502  u->InvalidateNewGRFCache();
503  u->UpdateViewport(true);
504  }
505  break;
506  }
507 
509 }
510 
523 CommandCost CmdAddVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
524 {
525  Vehicle *v = Vehicle::GetIfValid(GB(p2, 0, 20));
526  GroupID new_g = p1;
527 
528  if (v == NULL || (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g) && new_g != NEW_GROUP)) return CMD_ERROR;
529 
530  if (Group::IsValidID(new_g)) {
531  Group *g = Group::Get(new_g);
532  if (g->owner != _current_company || g->vehicle_type != v->type) return CMD_ERROR;
533  }
534 
535  if (v->owner != _current_company || !v->IsPrimaryVehicle()) return CMD_ERROR;
536 
537  if (new_g == NEW_GROUP) {
538  /* Create new group. */
539  CommandCost ret = CmdCreateGroup(0, flags, v->type, INVALID_GROUP, NULL);
540  if (ret.Failed()) return ret;
541 
542  new_g = _new_group_id;
543  }
544 
545  if (flags & DC_EXEC) {
546  AddVehicleToGroup(v, new_g);
547 
548  if (HasBit(p2, 31)) {
549  /* Add vehicles in the shared order list as well. */
550  for (Vehicle *v2 = v->FirstShared(); v2 != NULL; v2 = v2->NextShared()) {
551  if (v2->group_id != new_g) AddVehicleToGroup(v2, new_g);
552  }
553  }
554 
556 
557  /* Update the Replace Vehicle Windows */
563  }
564 
565  return CommandCost();
566 }
567 
578 CommandCost CmdAddSharedVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
579 {
580  VehicleType type = Extract<VehicleType, 0, 3>(p2);
581  GroupID id_g = p1;
582  if (!Group::IsValidID(id_g) || !IsCompanyBuildableVehicleType(type)) return CMD_ERROR;
583 
584  if (flags & DC_EXEC) {
585  Vehicle *v;
586 
587  /* Find the first front engine which belong to the group id_g
588  * then add all shared vehicles of this front engine to the group id_g */
589  FOR_ALL_VEHICLES(v) {
590  if (v->type == type && v->IsPrimaryVehicle()) {
591  if (v->group_id != id_g) continue;
592 
593  /* For each shared vehicles add it to the group */
594  for (Vehicle *v2 = v->FirstShared(); v2 != NULL; v2 = v2->NextShared()) {
595  if (v2->group_id != id_g) DoCommand(tile, id_g, v2->index, flags, CMD_ADD_VEHICLE_GROUP, text);
596  }
597  }
598  }
599 
601  }
602 
603  return CommandCost();
604 }
605 
606 
617 CommandCost CmdRemoveAllVehiclesGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
618 {
619  GroupID old_g = p1;
620  Group *g = Group::GetIfValid(old_g);
621 
622  if (g == NULL || g->owner != _current_company) return CMD_ERROR;
623 
624  if (flags & DC_EXEC) {
625  Vehicle *v;
626 
627  /* Find each Vehicle that belongs to the group old_g and add it to the default group */
628  FOR_ALL_VEHICLES(v) {
629  if (v->IsPrimaryVehicle()) {
630  if (v->group_id != old_g) continue;
631 
632  /* Add The Vehicle to the default group */
633  DoCommand(tile, DEFAULT_GROUP, v->index, flags, CMD_ADD_VEHICLE_GROUP, text);
634  }
635  }
636 
638  }
639 
640  return CommandCost();
641 }
642 
653 CommandCost CmdSetGroupLivery(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
654 {
655  Group *g = Group::GetIfValid(p1);
656  bool primary = !HasBit(p2, 8);
657  Colours colour = Extract<Colours, 16, 8>(p2);
658 
659  if (g == NULL || g->owner != _current_company) return CMD_ERROR;
660 
661  if (colour >= COLOUR_END && colour != INVALID_COLOUR) return CMD_ERROR;
662 
663  if (flags & DC_EXEC) {
664  if (primary) {
665  SB(g->livery.in_use, 0, 1, colour != INVALID_COLOUR);
666  if (colour == INVALID_COLOUR) colour = (Colours)GetParentLivery(g)->colour1;
667  g->livery.colour1 = colour;
668  } else {
669  SB(g->livery.in_use, 1, 1, colour != INVALID_COLOUR);
670  if (colour == INVALID_COLOUR) colour = (Colours)GetParentLivery(g)->colour2;
671  g->livery.colour2 = colour;
672  }
673 
676  }
677 
678  return CommandCost();
679 }
680 
686 static void SetGroupReplaceProtection(Group *g, bool protect)
687 {
688  g->replace_protection = protect;
689 
690  Group *pg;
691  FOR_ALL_GROUPS(pg) {
692  if (pg->parent == g->index) SetGroupReplaceProtection(pg, protect);
693  }
694 }
695 
708 CommandCost CmdSetGroupReplaceProtection(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
709 {
710  Group *g = Group::GetIfValid(p1);
711  if (g == NULL || g->owner != _current_company) return CMD_ERROR;
712 
713  if (flags & DC_EXEC) {
714  if (HasBit(p2, 1)) {
716  } else {
717  g->replace_protection = HasBit(p2, 0);
718  }
719 
722  }
723 
724  return CommandCost();
725 }
726 
733 {
734  if (!v->IsPrimaryVehicle()) return;
735 
736  if (!IsDefaultGroupID(v->group_id)) GroupStatistics::CountVehicle(v, -1);
737 }
738 
739 
747 {
748  if (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g)) return;
749 
750  assert(v->IsFrontEngine() || IsDefaultGroupID(new_g));
751 
752  for (Vehicle *u = v; u != NULL; u = u->Next()) {
753  if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
754 
755  u->group_id = new_g;
756  u->colourmap = PAL_NONE;
757  u->InvalidateNewGRFCache();
758  u->UpdateViewport(true);
759  }
760 
761  /* Update the Replace Vehicle Windows */
764 }
765 
766 
775 {
776  assert(v->IsFrontEngine() || v->IsFreeWagon());
777 
778  GroupID new_g = v->IsFrontEngine() ? v->group_id : (GroupID)DEFAULT_GROUP;
779  for (Vehicle *u = v; u != NULL; u = u->Next()) {
780  if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
781 
782  u->group_id = new_g;
783  u->colourmap = PAL_NONE;
784  u->InvalidateNewGRFCache();
785  }
786 
787  /* Update the Replace Vehicle Windows */
790 }
791 
800 uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e)
801 {
802  uint count = 0;
803  const Engine *e = Engine::Get(id_e);
804  const Group *g;
805  FOR_ALL_GROUPS(g) {
806  if (g->parent == id_g) count += GetGroupNumEngines(company, g->index, id_e);
807  }
808  return count + GroupStatistics::Get(company, id_g, e->type).num_engines[id_e];
809 }
810 
811 void RemoveAllGroupsForCompany(const CompanyID company)
812 {
813  Group *g;
814 
815  FOR_ALL_GROUPS(g) {
816  if (company == g->owner) delete g;
817  }
818 }
819 
820 
827 bool GroupIsInGroup(GroupID search, GroupID group)
828 {
829  if (!Group::IsValidID(search)) return search == group;
830 
831  do {
832  if (search == group) return true;
833  search = Group::Get(search)->parent;
834  } while (search != INVALID_GROUP);
835 
836  return false;
837 }
static void UpdateAfterLoad()
Update all caches after loading a game, changing NewGRF, etc.
Definition: group_cmd.cpp:103
CommandCost CmdSetGroupLivery(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Set the livery for a vehicle group.
Definition: group_cmd.cpp:653
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:72
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:74
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3201
static void AddVehicleToGroup(Vehicle *v, GroupID new_g)
Do add a vehicle to a group.
Definition: group_cmd.cpp:485
CommandCost CmdCreateGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Create a new vehicle group.
Definition: group_cmd.cpp:318
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:708
EngineRenewList engine_renew_list
Engine renewals of this company.
Definition: company_base.h:125
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:25
Depot view; Window numbers:
Definition: window_type.h:346
void SetTrainGroupID(Train *v, GroupID new_g)
Affect the groupID of a train to new_g.
Definition: group_cmd.cpp:746
void UpdateTrainGroupID(Train *v)
Recalculates the groupID of a train.
Definition: group_cmd.cpp:774
Replace vehicle window; Window numbers:
Definition: window_type.h:213
Last company-ownable type.
Definition: vehicle_type.h:31
uint16 * num_engines
Caches the number of engines of each type the company owns.
Definition: group.h:28
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:34
Helper functions to extract data from command parameters.
bool autoreplace_finished
Have all autoreplacement finished?
Definition: group.h:31
uint16 num_profit_vehicle
Number of vehicles considered for profit statistics;.
Definition: group.h:33
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.
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.
delete a group
Definition: command_type.h:319
Vehicle * FirstShared() const
Get the first vehicle of this vehicle chain.
Definition: vehicle_base.h:675
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:93
Aircraft vehicle type.
Definition: vehicle_type.h:29
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:128
GroupPool _group_pool("Group")
Pool of groups.
virtual bool IsPrimaryVehicle() const
Whether this is the primary vehicle in the chain.
Definition: vehicle_base.h:433
remove all vehicles from a group
Definition: command_type.h:323
CommandCost DoCommand(const CommandContainer *container, DoCommandFlag flags)
Shorthand for calling the long DoCommand with a container.
Definition: command.cpp:440
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:26
void PropagateChildLivery(const Group *g)
Propagate a livery change to a group&#39;s children.
Definition: group_cmd.cpp:274
VehicleType
Available vehicle types.
Definition: vehicle_type.h:23
char * name
Group Name.
Definition: group.h:68
DoCommandFlag
List of flags for a command.
Definition: command_type.h:343
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:732
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
Information about a particular livery.
Definition: livery.h:80
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:138
VehicleTypeByte vehicle_type
Vehicle type of the group.
Definition: group.h:70
Vehicle view; Window numbers:
Definition: window_type.h:334
Company colour selection; Window numbers:
Definition: window_type.h:225
GroupStatistics group_all[VEH_COMPANY_END]
NOSAVE: Statistics for the ALL_GROUP group.
Definition: company_base.h:127
Functions related to order backups.
bool IsFrontEngine() const
Check if the vehicle is a front engine.
Definition: vehicle_base.h:883
static GroupStatistics & Get(CompanyID company, GroupID id_g, VehicleType type)
Returns the GroupStatistics for a specific group.
Definition: group_cmd.cpp:65
TileIndex tile
Current tile index.
Definition: vehicle_base.h:230
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:800
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:686
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:27
CommandCost CmdRemoveAllVehiclesGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Remove all vehicles from a group.
Definition: group_cmd.cpp:617
Road vehicle type.
Definition: vehicle_type.h:27
bool Failed() const
Did this command fail?
Definition: command_type.h:161
byte colour2
Second colour, for vehicles with 2CC support.
Definition: livery.h:83
Maximum number of companies.
Definition: company_type.h:25
Base class for all pools.
Definition: pool_type.hpp:83
Ship vehicle type.
Definition: vehicle_type.h:28
&#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:711
Livery livery
Custom colour scheme for vehicles in this group.
Definition: group.h:73
bool GroupIsInGroup(GroupID search, GroupID group)
Test if GroupID group is a descendant of (or is) GroupID search.
Definition: group_cmd.cpp:827
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don&#39;t get linker errors.
Definition: pool_func.hpp:226
void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
Delete a window by its class and window number (if it is open).
Definition: window.cpp:1146
static void UpdateProfits()
Recompute the profits for all groups.
Definition: group_cmd.cpp:185
execute the given command
Definition: command_type.h:345
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:423
add a vehicle to a group
Definition: command_type.h:321
bool IsGroundVehicle() const
Check if the vehicle is a ground vehicle.
Definition: vehicle_base.h:471
GroupID parent
Parent group.
Definition: group.h:76
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:312
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
Vehicle * Next() const
Get the next vehicle of this vehicle.
Definition: vehicle_base.h:581
bool autoreplace_defined
Are any autoreplace rules set?
Definition: group.h:30
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:367
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:67
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
Vehicle details; Window numbers:
Definition: window_type.h:195
Functions related to commands.
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:81
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:69
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:523
#define FOR_ALL_VEHICLES(var)
Iterate over all vehicles.
Definition: vehicle_base.h:987
byte colour1
First colour, for all vehicles.
Definition: livery.h:82
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:578
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:663
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:56
Money GetDisplayProfitLastYear() const
Gets the profit vehicle had last year.
Definition: vehicle_base.h:572
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:3301
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1463
Train vehicle type.
Definition: vehicle_type.h:26
static const GroupID ALL_GROUP
All vehicles are in this group.
Definition: group_type.h:18