25 #include "table/strings.h" 34 GroupStatistics::GroupStatistics()
39 GroupStatistics::~GroupStatistics()
69 assert(g->
owner == company);
74 if (IsDefaultGroupID(id_g))
return Company::Get(company)->group_default[type];
107 FOR_ALL_COMPANIES(c) {
128 FOR_ALL_COMPANIES(c) {
140 assert(delta == 1 || delta == -1);
163 assert(delta == 1 || delta == -1);
189 FOR_ALL_COMPANIES(c) {
224 if (g->
owner != company)
continue;
248 if (old_g != new_g) {
259 Group::Group(
Owner owner)
292 _new_group_id = g->
index;
337 FOR_ALL_ENGINE_RENEWS(er) {
354 static bool IsUniqueGroupNameForVehicleType(
const char *name,
VehicleType type)
434 default: NOT_REACHED();
479 if (ret.
Failed())
return ret;
481 new_g = _new_group_id;
704 void RemoveAllGroupsForCompany(
const CompanyID company)
709 if (company == g->
owner)
delete g;
725 if (search == group)
return true;
static void UpdateAfterLoad()
Update all caches after loading a game, changing NewGRF etc.
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
bool replace_protection
If set to true, the global autoreplace have no effect on the group.
The information about a vehicle list.
static const int VEHICLE_PROFIT_MIN_AGE
Only vehicles older than this have a meaningful profit.
GroupStatistics statistics
NOSAVE: Statistics and caches on the vehicles in the group.
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
static void AddVehicleToGroup(Vehicle *v, GroupID new_g)
Do add a vehicle to a group.
CommandCost CmdCreateGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Create a new vehicle group.
Functions and type for generating vehicle lists.
static Titem * Get(size_t index)
Returns Titem with given index.
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
EngineRenewList engine_renew_list
Engine renewals of this company.
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
void SetTrainGroupID(Train *v, GroupID new_g)
Affect the groupID of a train to new_g.
void UpdateTrainGroupID(Train *v)
Recalculates the groupID of a train.
Replace vehicle window; Window numbers:
Last company-ownable type.
uint16 * num_engines
Caches the number of engines of each type the company owns.
Functions related to vehicles.
Tindex index
Index of this pool item.
Money profit_last_year
Sum of profits for all vehicles.
Helper functions to extract data from command parameters.
bool autoreplace_finished
Have all autoreplacement finished?
uint16 num_profit_vehicle
Number of vehicles considered for profit statistics;.
static GroupStatistics & GetAllGroup(const Vehicle *v)
Returns the GroupStatistic for the ALL_GROUPO of a vehicle type.
Common return value for all commands.
static Train * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
Vehicle * FirstShared() const
Get the first vehicle of this vehicle chain.
static const GroupID NEW_GROUP
Sentinel for a to-be-created group.
static void UpdateNumEngineGroup(const Vehicle *v, GroupID old_g, GroupID new_g)
Update the num engines of a groupID.
static bool IsAllGroupID(GroupID id_g)
Checks if a GroupID stands for all vehicles of a company.
static const uint MAX_LENGTH_GROUP_NAME_CHARS
The maximum length of a group name in characters including '\0'.
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.
GroupPool _group_pool("Group")
Pool of groups.
virtual bool IsPrimaryVehicle() const
Whether this is the primary vehicle in the chain.
remove all vehicles from a group
CommandCost DoCommand(const CommandContainer *container, DoCommandFlag flags)
Shorthand for calling the long DoCommand with a container.
static size_t GetPoolSize()
Returns first unused index.
Statistics and caches on the vehicles in a group.
VehicleType
Available vehicle types.
DoCommandFlag
List of flags for a command.
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.
A number of safeguards to prevent using unsafe methods.
uint16 GroupID
Type for all group identifiers.
VehicleType type
Vehicle type, ie VEH_ROAD, VEH_TRAIN, etc.
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
VehicleTypeByte vehicle_type
Vehicle type of the group.
GroupStatistics group_all[VEH_COMPANY_END]
NOSAVE: Statistics for the ALL_GROUP group.
Functions related to order backups.
bool IsFrontEngine() const
Check if the vehicle is a front engine.
static GroupStatistics & Get(CompanyID company, GroupID id_g, VehicleType type)
Returns the GroupStatistics for a specific group.
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...
static void CountVehicle(const Vehicle *v, int delta)
Update num_vehicle when adding or removing a vehicle.
static void SetGroupReplaceProtection(Group *g, bool protect)
Set replace protection for a group and its sub-groups.
static const GroupID INVALID_GROUP
Sentinel for invalid groups.
void Clear()
Clear all caches.
uint16 num_vehicle
Number of vehicles.
CommandCost CmdRemoveAllVehiclesGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Remove all vehicles from a group.
bool Failed() const
Did this command fail?
Maximum number of companies.
#define return_cmd_error(errcode)
Returns from a function with a specific StringID as error.
Base class for all pools.
'Train' is either a loco or a wagon.
bool IsEngineCountable() const
Check if a vehicle is counted in num_engines in each company struct.
bool GroupIsInGroup(GroupID search, GroupID group)
Test if GroupID group is a descendant of (or is) GroupID search.
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
Delete a window by its class and window number (if it is open).
static void UpdateProfits()
Recompute the profits for all groups.
execute the given command
static const GroupID DEFAULT_GROUP
Ungrouped vehicles are in this group.
Functions related to companies.
CommandCost CmdAlterGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Alter a group.
GroupID parent
Parent group.
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
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...
uint16 EngineID
Unique identification number of an engine.
CompanyByte _current_company
Company currently doing an action.
uint32 TileIndex
The index/ID of a Tile.
uint32 Pack() const
Pack a VehicleListIdentifier in a single uint32.
bool autoreplace_defined
Are any autoreplace rules set?
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.
Struct to store engine replacements.
OwnerByte owner
Which company owns the vehicle?
static void UpdateAutoreplace(CompanyID company)
Update autoreplace_defined and autoreplace_finished of all statistics of a company.
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.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() ...
Functions related to commands.
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-NULL) Titem.
static void CountEngine(const Vehicle *v, int delta)
Update num_engines when adding/removing an engine.
static WindowClass GetWindowClassForVehicleType(VehicleType vt)
Get WindowClass for vehicle list of given vehicle type.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
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.
OwnerByte owner
Group Owner.
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.
#define FOR_ALL_VEHICLES(var)
Iterate over all vehicles.
CommandCost CmdAddSharedVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Add all shared vehicles of all vehicles from a group.
static void VehicleReachedProfitAge(const Vehicle *v)
Add a vehicle to the profit sum of its group.
Base class for autoreplaces/autorenews.
Vehicle * NextShared() const
Get the next vehicle of the shared vehicle chain.
Owner
Enum for all companies/owners.
static CommandCost RemoveEngineReplacementForCompany(Company *c, EngineID engine, GroupID group, DoCommandFlag flags)
Remove an engine replacement for the company.
Functions related to autoreplacing.
static bool IsCompanyBuildableVehicleType(VehicleType type)
Is the given vehicle type buildable by a company?
VehicleTypeByte type
Type of vehicle.
Money GetDisplayProfitLastYear() const
Gets the profit vehicle had last year.
GroupID group_id
Index of group Pool array.
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-...
static const GroupID ALL_GROUP
All vehicles are in this group.