OpenTTD Source: src/company_base.h Source File

OpenTTD Source 15.0-beta1

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 <http://www.gnu.org/licenses/>.
6 */
7
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"
17#include "timer/timer_game_economy.h"
18#include "settings_type.h"
19#include "group.h"
20
21static const Money COMPANY_MAX_LOAN_DEFAULT = INT64_MIN;
22
24 struct CompanyEconomyEntry {
25 Money income;
26 Money expenses;
27 CargoArray delivered_cargo{};
28 int32_t performance_history;
29 Money company_value;
30};
31
32 struct CompanyInfrastructure {
33 std::array<uint32_t, RAILTYPE_END> rail{};
34 std::array<uint32_t, ROADTYPE_END> road{};
35 uint32_t signal;
36 uint32_t water;
37 uint32_t station;
38 uint32_t airport;
39
40 auto operator<=>(const CompanyInfrastructure &) const = default;
41
43 uint32_t GetRailTotal() const
44 {
45 return std::accumulate(std::begin(this->rail), std::end(this->rail), 0U);
46 }
47
48 uint32_t GetRoadTotal() const;
49 uint32_t GetTramTotal() const;
50};
51
52 class FreeUnitIDGenerator {
53public:
54 UnitID NextID() const;
55 UnitID UseID(UnitID index);
56 void ReleaseID(UnitID index);
57
58private:
59 using BitmapStorage = size_t;
60 static constexpr size_t BITMAP_SIZE = std::numeric_limits<BitmapStorage>::digits;
61
62 std::vector<BitmapStorage> used_bitmap;
63};
64
65typedef Pool<Company, CompanyID, 1, MAX_COMPANIES> CompanyPool;
66extern CompanyPool _company_pool;
67
69 struct CompanyProperties {
70 uint32_t name_2;
71 StringID name_1;
72 std::string name;
73
74 StringID president_name_1;
75 uint32_t president_name_2;
76 std::string president_name;
77
78 NetworkAuthorizedKeys allow_list;
79
80 CompanyManagerFace face;
81
82 Money money;
83 uint8_t money_fraction;
84 Money current_loan;
85 Money max_loan;
86
87 Colours colour;
88
89 uint8_t block_preview;
90
91 TileIndex location_of_HQ;
92 TileIndex last_build_coordinate;
93
94 TimerGameEconomy::Year inaugurated_year;
95 TimerGameCalendar::Year inaugurated_year_calendar;
96
97 uint8_t months_empty = 0;
98 uint8_t months_of_bankruptcy;
99 CompanyMask bankrupt_asked;
100 int16_t bankrupt_timeout;
101 Money bankrupt_value;
102
103 uint32_t terraform_limit;
104 uint32_t clear_limit;
105 uint32_t tree_limit;
106 uint32_t build_object_limit;
107
112 bool is_ai;
113
114 std::array<Expenses, 3> yearly_expenses{};
115 CompanyEconomyEntry cur_economy;
116 CompanyEconomyEntry old_economy[MAX_HISTORY_QUARTERS];
117 uint8_t num_valid_stat_ent;
118
119 Livery livery[LS_END];
120
121 EngineRenewList engine_renew_list;
122 CompanySettings settings;
123
124 // TODO: Change some of these member variables to use relevant INVALID_xxx constants
125 CompanyProperties()
126 : name_2(0), name_1(0), president_name_1(0), president_name_2(0),
127 face(0), money(0), money_fraction(0), current_loan(0), max_loan(COMPANY_MAX_LOAN_DEFAULT),
128 colour(COLOUR_BEGIN), block_preview(0), location_of_HQ(0), last_build_coordinate(0), inaugurated_year(0),
129 months_of_bankruptcy(0), bankrupt_asked(0), bankrupt_timeout(0), bankrupt_value(0),
130 terraform_limit(0), clear_limit(0), tree_limit(0), build_object_limit(0), is_ai(false), engine_renew_list(nullptr) {}
131};
132
133 struct Company : CompanyProperties, CompanyPool::PoolItem<&_company_pool> {
134 Company(uint16_t name_1 = 0, bool is_ai = false);
135 ~Company();
136
137 RailTypes avail_railtypes;
138 RoadTypes avail_roadtypes;
139
140 std::unique_ptr<class AIInstance> ai_instance;
141 class AIInfo *ai_info;
142 std::unique_ptr<class AIConfig> ai_config;
143
144 GroupStatistics group_all[VEH_COMPANY_END];
145 GroupStatistics group_default[VEH_COMPANY_END];
146
147 CompanyInfrastructure infrastructure;
148
149 FreeUnitIDGenerator freeunits[VEH_COMPANY_END];
150 FreeUnitIDGenerator freegroups;
151
152 Money GetMaxLoan() const;
153
159 static inline bool IsValidAiID(size_t index)
160 {
161 const Company *c = Company::GetIfValid(index);
162 return c != nullptr && c->is_ai;
163 }
164
171 static inline bool IsValidHumanID(size_t index)
172 {
173 const Company *c = Company::GetIfValid(index);
174 return c != nullptr && !c->is_ai;
175 }
176
184 static inline bool IsHumanID(size_t index)
185 {
186 return !Company::Get(index)->is_ai;
187 }
188
189 static void PostDestructor(size_t index);
190};
191
192 Money CalculateCompanyValue(const Company *c, bool including_loan = true);
193 Money CalculateHostileTakeoverValue(const Company *c);
194
195extern uint _cur_company_tick_index;
196
197#endif /* COMPANY_BASE_H */
autoreplace_type.h
Types related to autoreplacing.
AIInfo
All static information from an AI like name, version, etc.
Definition ai_info.hpp:16
FreeUnitIDGenerator
Definition company_base.h:52
FreeUnitIDGenerator::UseID
UnitID UseID(UnitID index)
Use a unit number.
Definition vehicle.cpp:1863
FreeUnitIDGenerator::ReleaseID
void ReleaseID(UnitID index)
Release a unit number.
Definition vehicle.cpp:1880
FreeUnitIDGenerator::NextID
UnitID NextID() const
Find first unused unit number.
Definition vehicle.cpp:1848
NetworkAuthorizedKeys
Simple helper to (more easily) manage authorized keys.
Definition network_type.h:148
OverflowSafeInt< int64_t >
CalculateHostileTakeoverValue
Money CalculateHostileTakeoverValue(const Company *c)
Calculate what you have to pay to take over a company.
Definition economy.cpp:176
CalculateCompanyValue
Money CalculateCompanyValue(const Company *c, bool including_loan=true)
Calculate the value of the company.
Definition economy.cpp:149
_cur_company_tick_index
uint _cur_company_tick_index
used to generate a name for one company that doesn't have a name yet per tick
Definition company_cmd.cpp:56
MAX_HISTORY_QUARTERS
static const uint MAX_HISTORY_QUARTERS
The maximum number of quarters kept as performance's history.
Definition company_type.h:43
CompanyManagerFace
uint32_t CompanyManagerFace
Company manager face bits, info see in company_manager_face.h.
Definition company_type.h:53
group.h
Base class for groups and group functions.
livery.h
Functions/types related to livery colours.
RailTypes
RailTypes
Allow incrementing of Track variables.
Definition rail_type.h:44
road_type.h
Enums and other types related to roads.
RoadTypes
RoadTypes
The different roadtypes we support, but then a bitmask of them.
Definition road_type.h:38
settings_type.h
Types related to global configuration settings.
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:16
CargoArray
Class for storing amounts of cargo.
Definition cargo_type.h:114
CompanyEconomyEntry
Statistics about the economy.
Definition company_base.h:24
CompanyEconomyEntry::income
Money income
The amount of income.
Definition company_base.h:25
CompanyEconomyEntry::expenses
Money expenses
The amount of expenses.
Definition company_base.h:26
CompanyEconomyEntry::company_value
Money company_value
The value of the company.
Definition company_base.h:29
CompanyEconomyEntry::delivered_cargo
CargoArray delivered_cargo
The amount of delivered cargo.
Definition company_base.h:27
CompanyEconomyEntry::performance_history
int32_t performance_history
Company score (scale 0-1000)
Definition company_base.h:28
CompanyInfrastructure
Definition company_base.h:32
CompanyInfrastructure::road
std::array< uint32_t, ROADTYPE_END > road
Count of company owned track bits for each road type.
Definition company_base.h:34
CompanyInfrastructure::GetRailTotal
uint32_t GetRailTotal() const
Get total sum of all owned track bits.
Definition company_base.h:43
CompanyInfrastructure::GetRoadTotal
uint32_t GetRoadTotal() const
Get total sum of all owned road bits.
Definition company_cmd.cpp:1264
CompanyInfrastructure::station
uint32_t station
Count of company owned station tiles.
Definition company_base.h:37
CompanyInfrastructure::signal
uint32_t signal
Count of company owned signals.
Definition company_base.h:35
CompanyInfrastructure::rail
std::array< uint32_t, RAILTYPE_END > rail
Count of company owned track bits for each rail type.
Definition company_base.h:33
CompanyInfrastructure::GetTramTotal
uint32_t GetTramTotal() const
Get total sum of all owned tram bits.
Definition company_cmd.cpp:1277
CompanyInfrastructure::airport
uint32_t airport
Count of company owned airports.
Definition company_base.h:38
CompanyInfrastructure::water
uint32_t water
Count of company owned track bits for canals.
Definition company_base.h:36
CompanyProperties
Statically loadable part of Company pool item.
Definition company_base.h:69
CompanyProperties::location_of_HQ
TileIndex location_of_HQ
Northern tile of HQ; INVALID_TILE when there is none.
Definition company_base.h:91
CompanyProperties::months_of_bankruptcy
uint8_t months_of_bankruptcy
Number of months that the company is unable to pay its debts.
Definition company_base.h:98
CompanyProperties::clear_limit
uint32_t clear_limit
Amount of tiles we can (still) clear (times 65536).
Definition company_base.h:104
CompanyProperties::bankrupt_asked
CompanyMask bankrupt_asked
which companies were asked about buying it?
Definition company_base.h:99
CompanyProperties::president_name
std::string president_name
Name of the president if the user changed it.
Definition company_base.h:76
CompanyProperties::bankrupt_timeout
int16_t bankrupt_timeout
If bigger than 0, amount of time to wait for an answer on an offer to buy this company.
Definition company_base.h:100
CompanyProperties::settings
CompanySettings settings
settings specific for each company
Definition company_base.h:122
CompanyProperties::allow_list
NetworkAuthorizedKeys allow_list
Public keys of clients that are allowed to join this company.
Definition company_base.h:78
CompanyProperties::months_empty
uint8_t months_empty
NOSAVE: Number of months this company has not had a client in multiplayer.
Definition company_base.h:97
CompanyProperties::old_economy
CompanyEconomyEntry old_economy[MAX_HISTORY_QUARTERS]
Economic data of the company of the last MAX_HISTORY_QUARTERS quarters.
Definition company_base.h:116
CompanyProperties::build_object_limit
uint32_t build_object_limit
Amount of tiles we can (still) build objects on (times 65536). Also applies to buying land.
Definition company_base.h:106
CompanyProperties::name_2
uint32_t name_2
Parameter of name_1.
Definition company_base.h:70
CompanyProperties::money_fraction
uint8_t money_fraction
Fraction of money of the company, too small to represent in money.
Definition company_base.h:83
CompanyProperties::is_ai
bool is_ai
If true, the company is (also) controlled by the computer (a NoAI program).
Definition company_base.h:112
CompanyProperties::president_name_2
uint32_t president_name_2
Parameter of president_name_1.
Definition company_base.h:75
CompanyProperties::name_1
StringID name_1
Name of the company if the user did not change it.
Definition company_base.h:71
CompanyProperties::current_loan
Money current_loan
Amount of money borrowed from the bank.
Definition company_base.h:84
CompanyProperties::inaugurated_year_calendar
TimerGameCalendar::Year inaugurated_year_calendar
Calendar year of starting the company. Used to display proper Inauguration year while in wallclock mo...
Definition company_base.h:95
CompanyProperties::terraform_limit
uint32_t terraform_limit
Amount of tileheights we can (still) terraform (times 65536).
Definition company_base.h:103
CompanyProperties::inaugurated_year
TimerGameEconomy::Year inaugurated_year
Economy year of starting the company.
Definition company_base.h:94
CompanyProperties::cur_economy
CompanyEconomyEntry cur_economy
Economic data of the company of this quarter.
Definition company_base.h:115
CompanyProperties::colour
Colours colour
Company colour.
Definition company_base.h:87
CompanyProperties::tree_limit
uint32_t tree_limit
Amount of trees we can (still) plant (times 65536).
Definition company_base.h:105
CompanyProperties::face
CompanyManagerFace face
Face description of the president.
Definition company_base.h:80
CompanyProperties::engine_renew_list
EngineRenewList engine_renew_list
Engine renewals of this company.
Definition company_base.h:121
CompanyProperties::max_loan
Money max_loan
Max allowed amount of the loan or COMPANY_MAX_LOAN_DEFAULT.
Definition company_base.h:85
CompanyProperties::yearly_expenses
std::array< Expenses, 3 > yearly_expenses
Expenses of the company for the last three years.
Definition company_base.h:114
CompanyProperties::last_build_coordinate
TileIndex last_build_coordinate
Coordinate of the last build thing by this company.
Definition company_base.h:92
CompanyProperties::president_name_1
StringID president_name_1
Name of the president if the user did not change it.
Definition company_base.h:74
CompanyProperties::block_preview
uint8_t block_preview
Number of quarters that the company is not allowed to get new exclusive engine previews (see Companie...
Definition company_base.h:89
CompanyProperties::name
std::string name
Name of the company if the user changed it.
Definition company_base.h:72
CompanyProperties::money
Money money
Money owned by the company.
Definition company_base.h:82
CompanyProperties::num_valid_stat_ent
uint8_t num_valid_stat_ent
Number of valid statistical entries in old_economy.
Definition company_base.h:117
CompanySettings
Settings that can be set per company.
Definition settings_type.h:582
Company
Definition company_base.h:133
Company::GetMaxLoan
Money GetMaxLoan() const
Calculate the max allowed loan for this company.
Definition company_cmd.cpp:105
Company::infrastructure
CompanyInfrastructure infrastructure
NOSAVE: Counts of company owned infrastructure.
Definition company_base.h:147
Company::group_default
GroupStatistics group_default[VEH_COMPANY_END]
NOSAVE: Statistics for the DEFAULT_GROUP group.
Definition company_base.h:145
Company::IsHumanID
static bool IsHumanID(size_t index)
Is this company a company not controlled by a NoAI program?
Definition company_base.h:184
Company::IsValidAiID
static bool IsValidAiID(size_t index)
Is this company a valid company, controlled by the computer (a NoAI program)?
Definition company_base.h:159
Company::avail_roadtypes
RoadTypes avail_roadtypes
Road types available to this company.
Definition company_base.h:138
Company::~Company
~Company()
Destructor.
Definition company_cmd.cpp:80
Company::avail_railtypes
RailTypes avail_railtypes
Rail types available to this company.
Definition company_base.h:137
Company::PostDestructor
static void PostDestructor(size_t index)
Invalidating some stuff after removing item from the pool.
Definition company_cmd.cpp:91
Company::IsValidHumanID
static bool IsValidHumanID(size_t index)
Is this company a valid company, not controlled by a NoAI program?
Definition company_base.h:171
Company::group_all
GroupStatistics group_all[VEH_COMPANY_END]
NOSAVE: Statistics for the ALL_GROUP group.
Definition company_base.h:144
EngineRenew
Struct to store engine replacements.
Definition autoreplace_base.h:33
GroupStatistics
Statistics and caches on the vehicles in a group.
Definition group.h:24
Livery
Information about a particular livery.
Definition livery.h:78
Pool::PoolItem
Base class for all PoolItems.
Definition pool_type.hpp:237
Pool::PoolItem<&_company_pool >::index
Tindex index
Index of this pool item.
Definition pool_type.hpp:238
Pool::PoolItem<&_company_pool >::GetIfValid
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition pool_type.hpp:350
Pool::PoolItem<&_company_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition pool_type.hpp:339
Pool
Base class for all pools.
Definition pool_type.hpp:80
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
tile_type.h
Types related to tiles.
timer_game_economy.h
Definition of the game-economy-timer.
UnitID
uint16_t UnitID
Type for the company global vehicle unit number.
Definition transport_type.h:16
VEH_COMPANY_END
@ VEH_COMPANY_END
Last company-ownable type.
Definition vehicle_type.h:29