OpenTTD
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");
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 
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 
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 
84 {
85  return GroupStatistics::Get(v->owner, v->group_id, v->type);
86 }
87 
94 {
95  return GroupStatistics::Get(v->owner, ALL_GROUP, v->type);
96 }
97 
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 
120  if (v->IsPrimaryVehicle()) GroupStatistics::CountVehicle(v, 1);
121  }
122 
123  for (const Company *c : Company::Iterate()) {
125  }
126 }
127 
133 /* static */ void GroupStatistics::CountVehicle(const Vehicle *v, int delta)
134 {
135  assert(delta == 1 || delta == -1);
136 
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);
161 }
162 
167 {
170 
171  stats_all.num_profit_vehicle++;
172  stats_all.profit_last_year += v->GetDisplayProfitLastYear();
173  stats.num_profit_vehicle++;
175 }
176 
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 
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 */
242 
243  /* Increase the num engines in the new group */
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 
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;
282  }
283  }
284 }
285 
286 
287 Group::Group(Owner owner)
288 {
289  this->owner = owner;
290  this->folded = false;
291 }
292 
293 Group::~Group()
294 {
295  free(this->name);
296 }
297 
298 
308 CommandCost CmdCreateGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
309 {
310  VehicleType vt = Extract<VehicleType, 0, 3>(p1);
311  if (!IsCompanyBuildableVehicleType(vt)) return CMD_ERROR;
312 
313  if (!Group::CanAllocateItem()) return CMD_ERROR;
314 
315  const Group *pg = Group::GetIfValid(GB(p2, 0, 16));
316  if (pg != nullptr) {
317  if (pg->owner != _current_company) return CMD_ERROR;
318  if (pg->vehicle_type != vt) return CMD_ERROR;
319  }
320 
321  if (flags & DC_EXEC) {
322  Group *g = new Group(_current_company);
323  g->replace_protection = false;
324  g->vehicle_type = vt;
325  g->parent = INVALID_GROUP;
326 
327  if (pg == nullptr) {
329  g->livery.colour1 = c->livery[LS_DEFAULT].colour1;
330  g->livery.colour2 = c->livery[LS_DEFAULT].colour2;
331  } else {
332  g->parent = pg->index;
333  g->livery.colour1 = pg->livery.colour1;
334  g->livery.colour2 = pg->livery.colour2;
335  }
336 
337  _new_group_id = g->index;
338 
341  }
342 
343  return CommandCost();
344 }
345 
346 
357 CommandCost CmdDeleteGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
358 {
359  Group *g = Group::GetIfValid(p1);
360  if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
361 
362  /* Remove all vehicles from the group */
363  DoCommand(0, p1, 0, flags, CMD_REMOVE_ALL_VEHICLES_GROUP);
364 
365  /* Delete sub-groups */
366  for (const Group *gp : Group::Iterate()) {
367  if (gp->parent == g->index) {
368  DoCommand(0, gp->index, 0, flags, CMD_DELETE_GROUP);
369  }
370  }
371 
372  if (flags & DC_EXEC) {
373  /* Update backupped orders if needed */
375 
376  /* If we set an autoreplace for the group we delete, remove it. */
378  Company *c;
379 
381  for (EngineRenew *er : EngineRenew::Iterate()) {
382  if (er->group_id == g->index) RemoveEngineReplacementForCompany(c, er->from, g->index, flags);
383  }
384  }
385 
386  VehicleType vt = g->vehicle_type;
387 
388  /* Delete the Replace Vehicle Windows */
390  delete g;
391 
394  }
395 
396  return CommandCost();
397 }
398 
411 CommandCost CmdAlterGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
412 {
413  Group *g = Group::GetIfValid(GB(p1, 0, 16));
414  if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
415 
416  if (!HasBit(p1, 16)) {
417  /* Rename group */
418  bool reset = StrEmpty(text);
419 
420  if (!reset) {
422  }
423 
424  if (flags & DC_EXEC) {
425  /* Delete the old name */
426  free(g->name);
427  /* Assign the new one */
428  g->name = reset ? nullptr : stredup(text);
429  }
430  } else {
431  /* Set group parent */
432  const Group *pg = Group::GetIfValid(GB(p2, 0, 16));
433 
434  if (pg != nullptr) {
435  if (pg->owner != _current_company) return CMD_ERROR;
436  if (pg->vehicle_type != g->vehicle_type) return CMD_ERROR;
437 
438  /* Ensure request parent isn't child of group.
439  * This is the only place that infinite loops are prevented. */
440  if (GroupIsInGroup(pg->index, g->index)) return_cmd_error(STR_ERROR_GROUP_CAN_T_SET_PARENT_RECURSION);
441  }
442 
443  if (flags & DC_EXEC) {
444  g->parent = (pg == nullptr) ? INVALID_GROUP : pg->index;
446 
447  if (g->livery.in_use == 0) {
448  const Livery *livery = GetParentLivery(g);
449  g->livery.colour1 = livery->colour1;
450  g->livery.colour2 = livery->colour2;
451 
454  }
455  }
456  }
457 
458  if (flags & DC_EXEC) {
462  }
463 
464  return CommandCost();
465 }
466 
467 
473 static void AddVehicleToGroup(Vehicle *v, GroupID new_g)
474 {
476 
477  switch (v->type) {
478  default: NOT_REACHED();
479  case VEH_TRAIN:
480  SetTrainGroupID(Train::From(v), new_g);
481  break;
482 
483  case VEH_ROAD:
484  case VEH_SHIP:
485  case VEH_AIRCRAFT:
486  if (v->IsEngineCountable()) UpdateNumEngineGroup(v, v->group_id, new_g);
487  v->group_id = new_g;
488  for (Vehicle *u = v; u != nullptr; u = u->Next()) {
489  u->colourmap = PAL_NONE;
490  u->InvalidateNewGRFCache();
491  u->UpdateViewport(true);
492  }
493  break;
494  }
495 
497 }
498 
511 CommandCost CmdAddVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
512 {
513  Vehicle *v = Vehicle::GetIfValid(GB(p2, 0, 20));
514  GroupID new_g = p1;
515 
516  if (v == nullptr || (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g) && new_g != NEW_GROUP)) return CMD_ERROR;
517 
518  if (Group::IsValidID(new_g)) {
519  Group *g = Group::Get(new_g);
520  if (g->owner != _current_company || g->vehicle_type != v->type) return CMD_ERROR;
521  }
522 
523  if (v->owner != _current_company || !v->IsPrimaryVehicle()) return CMD_ERROR;
524 
525  if (new_g == NEW_GROUP) {
526  /* Create new group. */
527  CommandCost ret = CmdCreateGroup(0, flags, v->type, INVALID_GROUP, nullptr);
528  if (ret.Failed()) return ret;
529 
530  new_g = _new_group_id;
531  }
532 
533  if (flags & DC_EXEC) {
534  AddVehicleToGroup(v, new_g);
535 
536  if (HasBit(p2, 31)) {
537  /* Add vehicles in the shared order list as well. */
538  for (Vehicle *v2 = v->FirstShared(); v2 != nullptr; v2 = v2->NextShared()) {
539  if (v2->group_id != new_g) AddVehicleToGroup(v2, new_g);
540  }
541  }
542 
544 
545  /* Update the Replace Vehicle Windows */
551  }
552 
553  return CommandCost();
554 }
555 
566 CommandCost CmdAddSharedVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
567 {
568  VehicleType type = Extract<VehicleType, 0, 3>(p2);
569  GroupID id_g = p1;
570  if (!Group::IsValidID(id_g) || !IsCompanyBuildableVehicleType(type)) return CMD_ERROR;
571 
572  if (flags & DC_EXEC) {
573  /* Find the first front engine which belong to the group id_g
574  * then add all shared vehicles of this front engine to the group id_g */
575  for (const Vehicle *v : Vehicle::Iterate()) {
576  if (v->type == type && v->IsPrimaryVehicle()) {
577  if (v->group_id != id_g) continue;
578 
579  /* For each shared vehicles add it to the group */
580  for (Vehicle *v2 = v->FirstShared(); v2 != nullptr; v2 = v2->NextShared()) {
581  if (v2->group_id != id_g) DoCommand(tile, id_g, v2->index, flags, CMD_ADD_VEHICLE_GROUP, text);
582  }
583  }
584  }
585 
587  }
588 
589  return CommandCost();
590 }
591 
592 
603 CommandCost CmdRemoveAllVehiclesGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
604 {
605  GroupID old_g = p1;
606  Group *g = Group::GetIfValid(old_g);
607 
608  if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
609 
610  if (flags & DC_EXEC) {
611  /* Find each Vehicle that belongs to the group old_g and add it to the default group */
612  for (const Vehicle *v : Vehicle::Iterate()) {
613  if (v->IsPrimaryVehicle()) {
614  if (v->group_id != old_g) continue;
615 
616  /* Add The Vehicle to the default group */
617  DoCommand(tile, DEFAULT_GROUP, v->index, flags, CMD_ADD_VEHICLE_GROUP, text);
618  }
619  }
620 
622  }
623 
624  return CommandCost();
625 }
626 
637 CommandCost CmdSetGroupLivery(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
638 {
639  Group *g = Group::GetIfValid(p1);
640  bool primary = !HasBit(p2, 8);
641  Colours colour = Extract<Colours, 16, 8>(p2);
642 
643  if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
644 
645  if (colour >= COLOUR_END && colour != INVALID_COLOUR) return CMD_ERROR;
646 
647  if (flags & DC_EXEC) {
648  if (primary) {
649  SB(g->livery.in_use, 0, 1, colour != INVALID_COLOUR);
650  if (colour == INVALID_COLOUR) colour = (Colours)GetParentLivery(g)->colour1;
651  g->livery.colour1 = colour;
652  } else {
653  SB(g->livery.in_use, 1, 1, colour != INVALID_COLOUR);
654  if (colour == INVALID_COLOUR) colour = (Colours)GetParentLivery(g)->colour2;
655  g->livery.colour2 = colour;
656  }
657 
660  }
661 
662  return CommandCost();
663 }
664 
670 static void SetGroupReplaceProtection(Group *g, bool protect)
671 {
672  g->replace_protection = protect;
673 
674  for (Group *pg : Group::Iterate()) {
675  if (pg->parent == g->index) SetGroupReplaceProtection(pg, protect);
676  }
677 }
678 
691 CommandCost CmdSetGroupReplaceProtection(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
692 {
693  Group *g = Group::GetIfValid(p1);
694  if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
695 
696  if (flags & DC_EXEC) {
697  if (HasBit(p2, 1)) {
699  } else {
700  g->replace_protection = HasBit(p2, 0);
701  }
702 
705  }
706 
707  return CommandCost();
708 }
709 
716 {
717  if (!v->IsPrimaryVehicle()) return;
718 
719  if (!IsDefaultGroupID(v->group_id)) GroupStatistics::CountVehicle(v, -1);
720 }
721 
722 
730 {
731  if (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g)) return;
732 
733  assert(v->IsFrontEngine() || IsDefaultGroupID(new_g));
734 
735  for (Vehicle *u = v; u != nullptr; u = u->Next()) {
736  if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
737 
738  u->group_id = new_g;
739  u->colourmap = PAL_NONE;
740  u->InvalidateNewGRFCache();
741  u->UpdateViewport(true);
742  }
743 
744  /* Update the Replace Vehicle Windows */
747 }
748 
749 
758 {
759  assert(v->IsFrontEngine() || v->IsFreeWagon());
760 
761  GroupID new_g = v->IsFrontEngine() ? v->group_id : (GroupID)DEFAULT_GROUP;
762  for (Vehicle *u = v; u != nullptr; u = u->Next()) {
763  if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
764 
765  u->group_id = new_g;
766  u->colourmap = PAL_NONE;
767  u->InvalidateNewGRFCache();
768  }
769 
770  /* Update the Replace Vehicle Windows */
773 }
774 
783 uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e)
784 {
785  uint count = 0;
786  const Engine *e = Engine::Get(id_e);
787  for (const Group *g : Group::Iterate()) {
788  if (g->parent == id_g) count += GetGroupNumEngines(company, g->index, id_e);
789  }
790  return count + GroupStatistics::Get(company, id_g, e->type).num_engines[id_e];
791 }
792 
802 {
803  uint count = 0;
804  for (const Group *g : Group::Iterate()) {
805  if (g->parent == id_g) count += GetGroupNumVehicle(company, g->index, type);
806  }
807  return count + GroupStatistics::Get(company, id_g, type).num_vehicle;
808 }
809 
819 {
820  uint count = 0;
821  for (const Group *g : Group::Iterate()) {
822  if (g->parent == id_g) count += GetGroupNumProfitVehicle(company, g->index, type);
823  }
824  return count + GroupStatistics::Get(company, id_g, type).num_profit_vehicle;
825 }
826 
836 {
837  Money sum = 0;
838  for (const Group *g : Group::Iterate()) {
839  if (g->parent == id_g) sum += GetGroupProfitLastYear(company, g->index, type);
840  }
841  return sum + GroupStatistics::Get(company, id_g, type).profit_last_year;
842 }
843 
844 void RemoveAllGroupsForCompany(const CompanyID company)
845 {
846  for (Group *g : Group::Iterate()) {
847  if (company == g->owner) delete g;
848  }
849 }
850 
851 
858 bool GroupIsInGroup(GroupID search, GroupID group)
859 {
860  if (!Group::IsValidID(search)) return search == group;
861 
862  do {
863  if (search == group) return true;
864  search = Group::Get(search)->parent;
865  } while (search != INVALID_GROUP);
866 
867  return false;
868 }
Last company-ownable type.
Definition: vehicle_type.h:29
Owner
Enum for all companies/owners.
Definition: company_type.h:18
static void UpdateAfterLoad()
Update all caches after loading a game, changing NewGRF, etc.
Definition: group_cmd.cpp:101
CommandCost CmdSetGroupLivery(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Set the livery for a vehicle group.
Definition: group_cmd.cpp:637
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:302
bool replace_protection
If set to true, the global autoreplace have no effect on the group.
Definition: group.h:70
The information about a vehicle list.
Definition: vehiclelist.h:29
Owner owner
Group Owner.
Definition: group.h:67
static const int VEHICLE_PROFIT_MIN_AGE
Only vehicles older than this have a meaningful profit.
Definition: vehicle_func.h:27
VehicleType vehicle_type
Vehicle type of the group.
Definition: group.h:68
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:3215
static void AddVehicleToGroup(Vehicle *v, GroupID new_g)
Do add a vehicle to a group.
Definition: group_cmd.cpp:473
CommandCost CmdCreateGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Create a new vehicle group.
Definition: group_cmd.cpp:308
Functions and type for generating vehicle lists.
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:818
Train vehicle type.
Definition: vehicle_type.h:24
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:291
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:691
EngineRenewList engine_renew_list
Engine renewals of this company.
Definition: company_base.h:126
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:23
Ship vehicle type.
Definition: vehicle_type.h:26
Depot view; Window numbers:
Definition: window_type.h:344
void SetTrainGroupID(Train *v, GroupID new_g)
Affect the groupID of a train to new_g.
Definition: group_cmd.cpp:729
void UpdateTrainGroupID(Train *v)
Recalculates the groupID of a train.
Definition: group_cmd.cpp:757
Replace vehicle window; Window numbers:
Definition: window_type.h:211
uint16 * num_engines
Caches the number of engines of each type the company owns.
Definition: group.h:26
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
Functions related to vehicles.
Vehicle data structure.
Definition: vehicle_base.h:210
Tindex index
Index of this pool item.
Definition: pool_type.hpp:189
Money profit_last_year
Sum of profits for all vehicles.
Definition: group.h:32
Helper functions to extract data from command parameters.
bool autoreplace_finished
Have all autoreplacement finished?
Definition: group.h:29
uint16 num_profit_vehicle
Number of vehicles considered for profit statistics;.
Definition: group.h:31
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:801
static GroupStatistics & GetAllGroup(const Vehicle *v)
Returns the GroupStatistic for the ALL_GROUPO of a vehicle type.
Definition: group_cmd.cpp:93
Common return value for all commands.
Definition: command_type.h:23
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:318
Vehicle * FirstShared() const
Get the first vehicle of this vehicle chain.
Definition: vehicle_base.h:673
static const GroupID NEW_GROUP
Sentinel for a to-be-created group.
Definition: group_type.h:15
static void UpdateNumEngineGroup(const Vehicle *v, GroupID old_g, GroupID new_g)
Update the num engines of a groupID.
Definition: group_cmd.cpp:237
static bool IsAllGroupID(GroupID id_g)
Checks if a GroupID stands for all vehicles of a company.
Definition: group.h:93
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:20
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:129
GroupPool _group_pool("Group")
Pool of groups.
virtual bool IsPrimaryVehicle() const
Whether this is the primary vehicle in the chain.
Definition: vehicle_base.h:431
remove all vehicles from a group
Definition: command_type.h:322
CommandCost DoCommand(const CommandContainer *container, DoCommandFlag flags)
Shorthand for calling the long DoCommand with a container.
Definition: command.cpp:441
static size_t GetPoolSize()
Returns first unused index.
Definition: pool_type.hpp:312
Statistics and caches on the vehicles in a group.
Definition: group.h:24
void PropagateChildLivery(const Group *g)
Propagate a livery change to a group&#39;s children.
Definition: group_cmd.cpp:265
char * name
Group Name.
Definition: group.h:66
DoCommandFlag
List of flags for a command.
Definition: command_type.h:342
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:715
A number of safeguards to prevent using unsafe methods.
uint16 GroupID
Type for all group identifiers.
Definition: group_type.h:13
VehicleType type
Vehicle type, ie VEH_ROAD, VEH_TRAIN, etc.
Definition: engine_base.h:40
Information about a particular livery.
Definition: livery.h:78
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:136
Vehicle view; Window numbers:
Definition: window_type.h:332
Company colour selection; Window numbers:
Definition: window_type.h:223
GroupStatistics group_all[VEH_COMPANY_END]
NOSAVE: Statistics for the ALL_GROUP group.
Definition: company_base.h:128
Functions related to order backups.
bool IsFrontEngine() const
Check if the vehicle is a front engine.
Definition: vehicle_base.h:881
static GroupStatistics & Get(CompanyID company, GroupID id_g, VehicleType type)
Returns the GroupStatistics for a specific group.
Definition: group_cmd.cpp:63
TileIndex tile
Current tile index.
Definition: vehicle_base.h:228
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:783
static void CountVehicle(const Vehicle *v, int delta)
Update num_vehicle when adding or removing a vehicle.
Definition: group_cmd.cpp:133
static void SetGroupReplaceProtection(Group *g, bool protect)
Set replace protection for a group and its sub-groups.
Definition: group_cmd.cpp:670
static const GroupID INVALID_GROUP
Sentinel for invalid groups.
Definition: group_type.h:18
void Clear()
Clear all caches.
Definition: group_cmd.cpp:45
uint16 num_vehicle
Number of vehicles.
Definition: group.h:25
Owner owner
Which company owns the vehicle?
Definition: vehicle_base.h:271
CommandCost CmdRemoveAllVehiclesGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Remove all vehicles from a group.
Definition: group_cmd.cpp:603
Money GetGroupProfitLastYear(CompanyID company, GroupID id_g, VehicleType type)
Get last year&#39;s profit for the group with GroupID id_g and its sub-groups.
Definition: group_cmd.cpp:835
bool Failed() const
Did this command fail?
Definition: command_type.h:159
byte colour2
Second colour, for vehicles with 2CC support.
Definition: livery.h:81
#define return_cmd_error(errcode)
Returns from a function with a specific StringID as error.
Definition: command_func.h:33
Base class for all pools.
Definition: pool_type.hpp:82
&#39;Train&#39; is either a loco or a wagon.
Definition: train.h:85
bool IsEngineCountable() const
Check if a vehicle is counted in num_engines in each company struct.
Definition: vehicle.cpp:708
Livery livery
Custom colour scheme for vehicles in this group.
Definition: group.h:71
bool GroupIsInGroup(GroupID search, GroupID group)
Test if GroupID group is a descendant of (or is) GroupID search.
Definition: group_cmd.cpp:858
#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:1162
static void UpdateProfits()
Recompute the profits for all groups.
Definition: group_cmd.cpp:180
execute the given command
Definition: command_type.h:344
static const GroupID DEFAULT_GROUP
Ungrouped vehicles are in this group.
Definition: group_type.h:17
Functions related to companies.
CommandCost CmdAlterGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Alter a group.
Definition: group_cmd.cpp:411
add a vehicle to a group
Definition: command_type.h:320
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:340
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:57
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:310
uint16 EngineID
Unique identification number of an engine.
Definition: engine_type.h:21
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
uint32 Pack() const
Pack a VehicleListIdentifier in a single uint32.
Definition: vehiclelist.cpp:21
Vehicle * Next() const
Get the next vehicle of this vehicle.
Definition: vehicle_base.h:579
bool autoreplace_defined
Are any autoreplace rules set?
Definition: group.h:28
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:357
Struct to store engine replacements.
static void UpdateAutoreplace(CompanyID company)
Update autoreplace_defined and autoreplace_finished of all statistics of a company.
Definition: group_cmd.cpp:204
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
VehicleType type
Type of vehicle.
Definition: vehicle_type.h:52
Maximum number of companies.
Definition: company_type.h:23
static void ClearGroup(GroupID group)
Clear the group of all backups having this group ID.
Group data.
Definition: group.h:65
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() ...
Definition: pool_type.hpp:261
Vehicle details; Window numbers:
Definition: window_type.h:193
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:79
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:45
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:280
static void CountEngine(const Vehicle *v, int delta)
Update num_engines when adding/removing an engine.
Definition: group_cmd.cpp:156
static WindowClass GetWindowClassForVehicleType(VehicleType vt)
Get WindowClass for vehicle list of given vehicle type.
Definition: vehicle_gui.h:91
Aircraft vehicle type.
Definition: vehicle_type.h:27
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:129
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:286
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:511
byte colour1
First colour, for all vehicles.
Definition: livery.h:80
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:566
static void VehicleReachedProfitAge(const Vehicle *v)
Add a vehicle to the profit sum of its group.
Definition: group_cmd.cpp:166
Base class for autoreplaces/autorenews.
Vehicle * NextShared() const
Get the next vehicle of the shared vehicle chain.
Definition: vehicle_base.h:661
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:256
static bool IsCompanyBuildableVehicleType(VehicleType type)
Is the given vehicle type buildable by a company?
Definition: vehicle_func.h:89
Money GetDisplayProfitLastYear() const
Gets the profit vehicle had last year.
Definition: vehicle_base.h:570
Road vehicle type.
Definition: vehicle_type.h:25
GroupID group_id
Index of group Pool array.
Definition: vehicle_base.h:324
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:3316
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1462
static const GroupID ALL_GROUP
All vehicles are in this group.
Definition: group_type.h:16