OpenTTD Source: src/company_base.h Source File

OpenTTD Source 15.0-RC2

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
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 = 0;
26 Money expenses = 0;
27 CargoArray delivered_cargo{};
28 int32_t performance_history = 0;
29 Money company_value = 0;
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 = 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
43 uint32_t GetRailTotal() const
44 {
45 return std::accumulate(std::begin(this->rail), std::end(this->rail), 0U);
46 }
47
48 uint32_t GetRoadTramTotal(RoadTramType rtt) const;
49
50 inline uint32_t GetRoadTotal() const { return GetRoadTramTotal(RTT_ROAD); }
51 inline uint32_t GetTramTotal() const { return GetRoadTramTotal(RTT_TRAM); }
52};
53
54 class FreeUnitIDGenerator {
55public:
56 UnitID NextID() const;
57 UnitID UseID(UnitID index);
58 void ReleaseID(UnitID index);
59
60private:
61 using BitmapStorage = size_t;
62 static constexpr size_t BITMAP_SIZE = std::numeric_limits<BitmapStorage>::digits;
63
64 std::vector<BitmapStorage> used_bitmap{};
65};
66
67typedef Pool<Company, CompanyID, 1> CompanyPool;
68extern CompanyPool _company_pool;
69
71 struct CompanyProperties {
72 uint32_t name_2 = 0;
73 StringID name_1 = INVALID_STRING_ID;
74 std::string name{};
75
76 StringID president_name_1 = INVALID_STRING_ID;
77 uint32_t president_name_2 = 0;
78 std::string president_name{};
79
80 NetworkAuthorizedKeys allow_list{};
81
82 CompanyManagerFace face{};
83
84 Money money = 0;
85 uint8_t money_fraction = 0;
86 Money current_loan = 0;
87 Money max_loan = COMPANY_MAX_LOAN_DEFAULT;
88
89 Colours colour = COLOUR_BEGIN;
90
91 uint8_t block_preview = 0;
92
93 TileIndex location_of_HQ = INVALID_TILE;
94 TileIndex last_build_coordinate{};
95
96 TimerGameEconomy::Year inaugurated_year{};
97 TimerGameCalendar::Year inaugurated_year_calendar{};
98
99 uint8_t months_empty = 0;
100 uint8_t months_of_bankruptcy = 0;
101 CompanyMask bankrupt_asked{};
102 int16_t bankrupt_timeout = 0;
103 Money bankrupt_value = 0;
104
105 uint32_t terraform_limit = 0;
106 uint32_t clear_limit = 0;
107 uint32_t tree_limit = 0;
108 uint32_t build_object_limit = 0;
109
114 bool is_ai = false;
115
116 std::array<Expenses, 3> yearly_expenses{};
117 CompanyEconomyEntry cur_economy{};
118 std::array<CompanyEconomyEntry, MAX_HISTORY_QUARTERS> old_economy{};
119 uint8_t num_valid_stat_ent = 0;
120
121 std::array<Livery, LS_END> livery{};
122
123 EngineRenewList engine_renew_list = nullptr;
124 CompanySettings settings{};
125};
126
127 struct Company : CompanyProperties, CompanyPool::PoolItem<&_company_pool> {
128 Company(StringID name_1 = {}, bool is_ai = false);
129 ~Company();
130
131 RailTypes avail_railtypes{};
132 RoadTypes avail_roadtypes{};
133
134 std::unique_ptr<class AIInstance> ai_instance{};
135 class AIInfo *ai_info = nullptr;
136 std::unique_ptr<class AIConfig> ai_config{};
137
138 std::array<GroupStatistics, VEH_COMPANY_END> group_all{};
139 std::array<GroupStatistics, VEH_COMPANY_END> group_default{};
140
141 CompanyInfrastructure infrastructure{};
142
143 std::array<FreeUnitIDGenerator, VEH_COMPANY_END> freeunits{};
144 FreeUnitIDGenerator freegroups{};
145
146 Money GetMaxLoan() const;
147
153 static inline bool IsValidAiID(auto index)
154 {
155 const Company *c = Company::GetIfValid(index);
156 return c != nullptr && c->is_ai;
157 }
158
165 static inline bool IsValidHumanID(auto index)
166 {
167 const Company *c = Company::GetIfValid(index);
168 return c != nullptr && !c->is_ai;
169 }
170
178 static inline bool IsHumanID(auto index)
179 {
180 return !Company::Get(index)->is_ai;
181 }
182
189 inline uint8_t GetCompanyRecolourOffset(LiveryScheme livery_scheme, bool use_secondary = true) const
190 {
191 const Livery &l = this->livery[livery_scheme];
192 return use_secondary ? l.colour1 + l.colour2 * 16 : l.colour1;
193 }
194
195 static void PostDestructor(size_t index);
196};
197
198 Money CalculateCompanyValue(const Company *c, bool including_loan = true);
199 Money CalculateHostileTakeoverValue(const Company *c);
200
201extern uint _cur_company_tick_index;
202
203#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
CompanyMask
Definition company_type.h:43
EnumBitSet< RailType, uint64_t >
FreeUnitIDGenerator
Definition company_base.h:54
FreeUnitIDGenerator::UseID
UnitID UseID(UnitID index)
Use a unit number.
Definition vehicle.cpp:1844
FreeUnitIDGenerator::ReleaseID
void ReleaseID(UnitID index)
Release a unit number.
Definition vehicle.cpp:1861
FreeUnitIDGenerator::NextID
UnitID NextID() const
Find first unused unit number.
Definition vehicle.cpp:1829
NetworkAuthorizedKeys
Simple helper to (more easily) manage authorized keys.
Definition network_type.h:137
OverflowSafeInt< int64_t >
CalculateHostileTakeoverValue
Money CalculateHostileTakeoverValue(const Company *c)
Calculate what you have to pay to take over a company.
Definition economy.cpp:177
CalculateCompanyValue
Money CalculateCompanyValue(const Company *c, bool including_loan=true)
Calculate the value of the company.
Definition economy.cpp:150
_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:59
group.h
Base class for groups and group functions.
livery.h
Functions/types related to livery colours.
LiveryScheme
LiveryScheme
List of different livery schemes.
Definition livery.h:22
road_type.h
Enums and other types related to roads.
RoadTramType
RoadTramType
The different types of road type.
Definition road_type.h:37
RTT_ROAD
@ RTT_ROAD
Road road type.
Definition road_type.h:38
RTT_TRAM
@ RTT_TRAM
Tram road type.
Definition road_type.h:39
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:19
INVALID_STRING_ID
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition strings_type.h:20
CargoArray
Class for storing amounts of cargo.
Definition cargo_type.h:111
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::GetRoadTramTotal
uint32_t GetRoadTramTotal(RoadTramType rtt) const
Get total sum of all owned road bits.
Definition company_cmd.cpp:1297
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::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::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
CompanyManagerFace
Definition company_type.h:53
CompanyProperties
Statically loadable part of Company pool item.
Definition company_base.h:71
CompanyProperties::location_of_HQ
TileIndex location_of_HQ
Northern tile of HQ; INVALID_TILE when there is none.
Definition company_base.h:93
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:100
CompanyProperties::clear_limit
uint32_t clear_limit
Amount of tiles we can (still) clear (times 65536).
Definition company_base.h:106
CompanyProperties::bankrupt_asked
CompanyMask bankrupt_asked
which companies were asked about buying it?
Definition company_base.h:101
CompanyProperties::president_name
std::string president_name
Name of the president if the user changed it.
Definition company_base.h:78
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:102
CompanyProperties::settings
CompanySettings settings
settings specific for each company
Definition company_base.h:124
CompanyProperties::allow_list
NetworkAuthorizedKeys allow_list
Public keys of clients that are allowed to join this company.
Definition company_base.h:80
CompanyProperties::months_empty
uint8_t months_empty
NOSAVE: Number of months this company has not had a client in multiplayer.
Definition company_base.h:99
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:108
CompanyProperties::name_2
uint32_t name_2
Parameter of name_1.
Definition company_base.h:72
CompanyProperties::money_fraction
uint8_t money_fraction
Fraction of money of the company, too small to represent in money.
Definition company_base.h:85
CompanyProperties::is_ai
bool is_ai
If true, the company is (also) controlled by the computer (a NoAI program).
Definition company_base.h:114
CompanyProperties::president_name_2
uint32_t president_name_2
Parameter of president_name_1.
Definition company_base.h:77
CompanyProperties::name_1
StringID name_1
Name of the company if the user did not change it.
Definition company_base.h:73
CompanyProperties::current_loan
Money current_loan
Amount of money borrowed from the bank.
Definition company_base.h:86
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:97
CompanyProperties::terraform_limit
uint32_t terraform_limit
Amount of tileheights we can (still) terraform (times 65536).
Definition company_base.h:105
CompanyProperties::inaugurated_year
TimerGameEconomy::Year inaugurated_year
Economy year of starting the company.
Definition company_base.h:96
CompanyProperties::cur_economy
CompanyEconomyEntry cur_economy
Economic data of the company of this quarter.
Definition company_base.h:117
CompanyProperties::colour
Colours colour
Company colour.
Definition company_base.h:89
CompanyProperties::tree_limit
uint32_t tree_limit
Amount of trees we can (still) plant (times 65536).
Definition company_base.h:107
CompanyProperties::old_economy
std::array< CompanyEconomyEntry, MAX_HISTORY_QUARTERS > old_economy
Economic data of the company of the last MAX_HISTORY_QUARTERS quarters.
Definition company_base.h:118
CompanyProperties::face
CompanyManagerFace face
Face description of the president.
Definition company_base.h:82
CompanyProperties::engine_renew_list
EngineRenewList engine_renew_list
Engine renewals of this company.
Definition company_base.h:123
CompanyProperties::max_loan
Money max_loan
Max allowed amount of the loan or COMPANY_MAX_LOAN_DEFAULT.
Definition company_base.h:87
CompanyProperties::yearly_expenses
std::array< Expenses, 3 > yearly_expenses
Expenses of the company for the last three years.
Definition company_base.h:116
CompanyProperties::last_build_coordinate
TileIndex last_build_coordinate
Coordinate of the last build thing by this company.
Definition company_base.h:94
CompanyProperties::president_name_1
StringID president_name_1
Name of the president if the user did not change it.
Definition company_base.h:76
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:91
CompanyProperties::name
std::string name
Name of the company if the user changed it.
Definition company_base.h:74
CompanyProperties::money
Money money
Money owned by the company.
Definition company_base.h:84
CompanyProperties::num_valid_stat_ent
uint8_t num_valid_stat_ent
Number of valid statistical entries in old_economy.
Definition company_base.h:119
CompanySettings
Settings that can be set per company.
Definition settings_type.h:610
Company
Definition company_base.h:127
Company::GetMaxLoan
Money GetMaxLoan() const
Calculate the max allowed loan for this company.
Definition company_cmd.cpp:107
Company::infrastructure
CompanyInfrastructure infrastructure
NOSAVE: Counts of company owned infrastructure.
Definition company_base.h:141
Company::group_default
std::array< GroupStatistics, VEH_COMPANY_END > group_default
NOSAVE: Statistics for the DEFAULT_GROUP group.
Definition company_base.h:139
Company::IsHumanID
static bool IsHumanID(auto index)
Is this company a company not controlled by a NoAI program?
Definition company_base.h:178
Company::GetCompanyRecolourOffset
uint8_t GetCompanyRecolourOffset(LiveryScheme livery_scheme, bool use_secondary=true) const
Get offset for recolour palette of specific company.
Definition company_base.h:189
Company::IsValidAiID
static bool IsValidAiID(auto index)
Is this company a valid company, controlled by the computer (a NoAI program)?
Definition company_base.h:153
Company::IsValidHumanID
static bool IsValidHumanID(auto index)
Is this company a valid company, not controlled by a NoAI program?
Definition company_base.h:165
Company::group_all
std::array< GroupStatistics, VEH_COMPANY_END > group_all
NOSAVE: Statistics for the ALL_GROUP group.
Definition company_base.h:138
Company::avail_roadtypes
RoadTypes avail_roadtypes
Road types available to this company.
Definition company_base.h:132
Company::~Company
~Company()
Destructor.
Definition company_cmd.cpp:82
Company::avail_railtypes
RailTypes avail_railtypes
Rail types available to this company.
Definition company_base.h:131
Company::PostDestructor
static void PostDestructor(size_t index)
Invalidating some stuff after removing item from the pool.
Definition company_cmd.cpp:93
EngineRenew
Struct to store engine replacements.
Definition autoreplace_base.h:33
Livery
Information about a particular livery.
Definition livery.h:79
Livery::colour2
Colours colour2
Second colour, for vehicles with 2CC support.
Definition livery.h:88
Livery::colour1
Colours colour1
First colour, for all vehicles.
Definition livery.h:87
Pool::PoolItem
Base class for all PoolItems.
Definition pool_type.hpp:285
Pool::PoolItem<&_company_pool >::Get
static Titem * Get(auto index)
Returns Titem with given index.
Definition pool_type.hpp:387
Pool::PoolItem<&_company_pool >::index
Tindex index
Index of this pool item.
Definition pool_type.hpp:286
Pool::PoolItem<&_company_pool >::GetIfValid
static Titem * GetIfValid(auto index)
Returns Titem with given index.
Definition pool_type.hpp:398
Pool
Base class for all pools.
Definition pool_type.hpp:133
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.
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
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:14