OpenTTD Source 20260218-master-g2123fca5ea
company_base.h
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef COMPANY_BASE_H
11#define COMPANY_BASE_H
12
13#include "road_type.h"
14#include "livery.h"
15#include "autoreplace_type.h"
16#include "tile_type.h"
18#include "settings_type.h"
19#include "group.h"
20
21static const Money COMPANY_MAX_LOAN_DEFAULT = INT64_MIN;
22
31
33 std::array<uint32_t, RAILTYPE_END> rail{};
34 std::array<uint32_t, ROADTYPE_END> road{};
35 uint32_t signal = 0;
36 uint32_t water = 0;
37 uint32_t station = 0;
38 uint32_t airport = 0;
39
40 auto operator<=>(const CompanyInfrastructure &) const = default;
41
46 uint32_t GetRailTotal() const
47 {
48 return std::accumulate(std::begin(this->rail), std::end(this->rail), 0U);
49 }
50
51 uint32_t GetRoadTramTotal(RoadTramType rtt) const;
52
53 inline uint32_t GetRoadTotal() const { return GetRoadTramTotal(RTT_ROAD); }
54 inline uint32_t GetTramTotal() const { return GetRoadTramTotal(RTT_TRAM); }
55};
56
58public:
59 UnitID NextID() const;
60 UnitID UseID(UnitID index);
61 void ReleaseID(UnitID index);
62
63private:
64 using BitmapStorage = size_t;
65 static constexpr size_t BITMAP_SIZE = std::numeric_limits<BitmapStorage>::digits;
66
67 std::vector<BitmapStorage> used_bitmap{};
68};
69
70typedef Pool<Company, CompanyID, 1> CompanyPool;
71extern CompanyPool _company_pool;
72
75 uint32_t name_2 = 0;
77 std::string name{};
78
80 uint32_t president_name_2 = 0;
81 std::string president_name{};
82
84 bool allow_any = false;
85
87
88 Money money = 0;
89 uint8_t money_fraction = 0;
90 Money current_loan = 0;
91 Money max_loan = COMPANY_MAX_LOAN_DEFAULT;
92
93 Colours colour = COLOUR_BEGIN;
94
95 uint8_t block_preview = 0;
96
99
100 TimerGameEconomy::Year inaugurated_year{};
101 TimerGameCalendar::Year inaugurated_year_calendar{};
102
103 uint8_t months_empty = 0;
106 int16_t bankrupt_timeout = 0;
107 Money bankrupt_value = 0;
108
109 uint32_t terraform_limit = 0;
110 uint32_t clear_limit = 0;
111 uint32_t tree_limit = 0;
112 uint32_t build_object_limit = 0;
113
118 bool is_ai = false;
119
120 std::array<Expenses, 3> yearly_expenses{};
122 std::array<CompanyEconomyEntry, MAX_HISTORY_QUARTERS> old_economy{};
123 uint8_t num_valid_stat_ent = 0;
124
125 std::array<Livery, LS_END> livery{};
126
129};
130
131struct Company : CompanyProperties, CompanyPool::PoolItem<&_company_pool> {
132 Company(CompanyID index, StringID name_1 = {}, bool is_ai = false);
133 ~Company();
134
136 RoadTypes avail_roadtypes{};
137
138 std::unique_ptr<class AIInstance> ai_instance{};
139 class AIInfo *ai_info = nullptr;
140 std::unique_ptr<class AIConfig> ai_config{};
141
142 std::array<GroupStatistics, VEH_COMPANY_END> group_all{};
143 std::array<GroupStatistics, VEH_COMPANY_END> group_default{};
144
146
147 std::array<FreeUnitIDGenerator, VEH_COMPANY_END> freeunits{};
148 FreeUnitIDGenerator freegroups{};
149
150 Money GetMaxLoan() const;
151
157 static inline bool IsValidAiID(auto index)
158 {
160 return c != nullptr && c->is_ai;
161 }
162
169 static inline bool IsValidHumanID(auto index)
170 {
172 return c != nullptr && !c->is_ai;
173 }
174
182 static inline bool IsHumanID(auto index)
183 {
184 return !Company::Get(index)->is_ai;
185 }
186
193 inline uint8_t GetCompanyRecolourOffset(LiveryScheme livery_scheme, bool use_secondary = true) const
194 {
195 const Livery &l = this->livery[livery_scheme];
196 return use_secondary ? l.colour1 + l.colour2 * 16 : l.colour1;
197 }
198
199 static void PostDestructor(size_t index);
200};
201
202Money CalculateCompanyValue(const Company *c, bool including_loan = true);
204
205extern uint _cur_company_tick_index;
206
207#endif /* COMPANY_BASE_H */
Types related to autoreplacing.
EngineRenew * EngineRenewList
A list to group EngineRenew directives together (such as per-company).
UnitID UseID(UnitID index)
Use a unit number.
Definition vehicle.cpp:1856
void ReleaseID(UnitID index)
Release a unit number.
Definition vehicle.cpp:1873
UnitID NextID() const
Find first unused unit number.
Definition vehicle.cpp:1841
Simple helper to (more easily) manage authorized keys.
Money CalculateHostileTakeoverValue(const Company *c)
Calculate what you have to pay to take over a company.
Definition economy.cpp:177
Money CalculateCompanyValue(const Company *c, bool including_loan=true)
Calculate the value of the company.
Definition economy.cpp:150
uint _cur_company_tick_index
used to generate a name for one company that doesn't have a name yet per tick
Base class for groups and group functions.
Functions/types related to livery colours.
LiveryScheme
List of different livery schemes.
Definition livery.h:22
EnumBitSet< RailType, uint64_t > RailTypes
Allow incrementing of Track variables.
Definition rail_type.h:38
Enums and other types related to roads.
RoadTramType
The different types of road type.
Definition road_type.h:37
@ RTT_ROAD
Road road type.
Definition road_type.h:38
@ RTT_TRAM
Tram road type.
Definition road_type.h:39
Types related to global configuration settings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames).
Class for storing amounts of cargo.
Definition cargo_type.h:115
Statistics about the economy.
Money income
The amount of income.
Money expenses
The amount of expenses.
Money company_value
The value of the company.
CargoArray delivered_cargo
The amount of delivered cargo.
int32_t performance_history
Company score (scale 0-1000).
uint32_t GetRoadTramTotal(RoadTramType rtt) const
Get total sum of all owned road bits.
std::array< uint32_t, ROADTYPE_END > road
Count of company owned track bits for each road type.
uint32_t GetRailTotal() const
Get total sum of all owned track bits.
uint32_t station
Count of company owned station tiles.
uint32_t signal
Count of company owned signals.
std::array< uint32_t, RAILTYPE_END > rail
Count of company owned track bits for each rail type.
uint32_t airport
Count of company owned airports.
uint32_t water
Count of company owned track bits for canals.
Statically loadable part of Company pool item.
TileIndex location_of_HQ
Northern tile of HQ; INVALID_TILE when there is none.
uint8_t months_of_bankruptcy
Number of months that the company is unable to pay its debts.
uint32_t clear_limit
Amount of tiles we can (still) clear (times 65536).
CompanyMask bankrupt_asked
which companies were asked about buying it?
std::string president_name
Name of the president if the user changed it.
int16_t bankrupt_timeout
If bigger than 0, amount of time to wait for an answer on an offer to buy this company.
CompanySettings settings
settings specific for each company
NetworkAuthorizedKeys allow_list
Public keys of clients that are allowed to join this company.
bool allow_any
Set if anyone is allowed to join this company.
uint8_t months_empty
NOSAVE: Number of months this company has not had a client in multiplayer.
uint32_t build_object_limit
Amount of tiles we can (still) build objects on (times 65536). Also applies to buying land and placin...
uint32_t name_2
Parameter of name_1.
uint8_t money_fraction
Fraction of money of the company, too small to represent in money.
bool is_ai
If true, the company is (also) controlled by the computer (a NoAI program).
uint32_t president_name_2
Parameter of president_name_1.
StringID name_1
Name of the company if the user did not change it.
Money current_loan
Amount of money borrowed from the bank.
TimerGameCalendar::Year inaugurated_year_calendar
Calendar year of starting the company. Used to display proper Inauguration year while in wallclock mo...
uint32_t terraform_limit
Amount of tileheights we can (still) terraform (times 65536).
TimerGameEconomy::Year inaugurated_year
Economy year of starting the company.
CompanyEconomyEntry cur_economy
Economic data of the company of this quarter.
Colours colour
Company colour.
uint32_t tree_limit
Amount of trees we can (still) plant (times 65536).
std::array< CompanyEconomyEntry, MAX_HISTORY_QUARTERS > old_economy
Economic data of the company of the last MAX_HISTORY_QUARTERS quarters.
CompanyManagerFace face
Face description of the president.
EngineRenewList engine_renew_list
Engine renewals of this company.
Money max_loan
Max allowed amount of the loan or COMPANY_MAX_LOAN_DEFAULT.
std::array< Expenses, 3 > yearly_expenses
Expenses of the company for the last three years.
TileIndex last_build_coordinate
Coordinate of the last build thing by this company.
StringID president_name_1
Name of the president if the user did not change it.
uint8_t block_preview
Number of quarters that the company is not allowed to get new exclusive engine previews (see Companie...
std::string name
Name of the company if the user changed it.
Money money
Money owned by the company.
uint8_t num_valid_stat_ent
Number of valid statistical entries in old_economy.
Settings that can be set per company.
Money GetMaxLoan() const
Calculate the max allowed loan for this company.
CompanyInfrastructure infrastructure
NOSAVE: Counts of company owned infrastructure.
std::array< GroupStatistics, VEH_COMPANY_END > group_default
NOSAVE: Statistics for the DEFAULT_GROUP group.
static bool IsHumanID(auto index)
Is this company a company not controlled by a NoAI program?
uint8_t GetCompanyRecolourOffset(LiveryScheme livery_scheme, bool use_secondary=true) const
Get offset for recolour palette of specific company.
static bool IsValidAiID(auto index)
Is this company a valid company, controlled by the computer (a NoAI program)?
static bool IsValidHumanID(auto index)
Is this company a valid company, not controlled by a NoAI program?
std::array< GroupStatistics, VEH_COMPANY_END > group_all
NOSAVE: Statistics for the ALL_GROUP group.
RoadTypes avail_roadtypes
Road types available to this company.
~Company()
Destructor.
RailTypes avail_railtypes
Rail types available to this company.
static void PostDestructor(size_t index)
Invalidating some stuff after removing item from the pool.
Company(CompanyID index, StringID name_1={}, bool is_ai=false)
Constructor.
Information about a particular livery.
Definition livery.h:79
Colours colour2
Second colour, for vehicles with 2CC support.
Definition livery.h:89
Colours colour1
First colour, for all vehicles.
Definition livery.h:88
static Company * Get(auto index)
static Company * GetIfValid(auto index)
Base class for all pools.
Types related to tiles.
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:100
Definition of the game-economy-timer.
uint16_t UnitID
Type for the company global vehicle unit number.