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/>.
8 /** @file company_base.h Definition of stuff that is very close to a company, like the company struct itself. */
10 #ifndef COMPANY_BASE_H
11 #define COMPANY_BASE_H
13 #include "road_type.h"
15 #include "autoreplace_type.h"
16 #include "tile_type.h"
17 #include "timer/timer_game_economy.h"
18 #include "settings_type.h"
21 static const Money COMPANY_MAX_LOAN_DEFAULT
= INT64_MIN
;
23 /** Statistics about the economy. */
24 struct CompanyEconomyEntry
{
25 Money income
; ///< The amount of income.
26 Money expenses
; ///< The amount of expenses.
27 CargoArray delivered_cargo
{}; ///< The amount of delivered cargo.
28 int32_t performance_history
; ///< Company score (scale 0-1000)
29 Money company_value
; ///< The value of the company.
32 struct CompanyInfrastructure
{
33 std::array
<uint32_t, RAILTYPE_END
> rail
{}; ///< Count of company owned track bits for each rail type.
34 std::array
<uint32_t, ROADTYPE_END
> road
{}; ///< Count of company owned track bits for each road type.
35 uint32_t signal
; ///< Count of company owned signals.
36 uint32_t water
; ///< Count of company owned track bits for canals.
37 uint32_t station
; ///< Count of company owned station tiles.
38 uint32_t airport
; ///< Count of company owned airports.
40 auto operator<=>(const CompanyInfrastructure
&) const = default;
42 /** Get total sum of all owned track bits. */
43 uint32_t GetRailTotal() const
45 return std::accumulate(std::begin(this->rail
), std::end(this->rail
), 0U);
48 uint32_t GetRoadTotal() const;
49 uint32_t GetTramTotal() const;
52 class FreeUnitIDGenerator
{
54 UnitID
NextID() const;
55 UnitID
UseID(UnitID index
);
56 void ReleaseID(UnitID index
);
59 using BitmapStorage
= size_t;
60 static constexpr size_t BITMAP_SIZE
= std::numeric_limits
<BitmapStorage
>::digits
;
62 std::vector
<BitmapStorage
> used_bitmap
;
65 typedef Pool
<Company
, CompanyID
, 1, MAX_COMPANIES
> CompanyPool
;
66 extern CompanyPool _company_pool
;
68 /** Statically loadable part of Company pool item */
69 struct CompanyProperties
{
70 uint32_t name_2
; ///< Parameter of #name_1.
71 StringID name_1
; ///< Name of the company if the user did not change it.
72 std::string name
; ///< Name of the company if the user changed it.
74 StringID president_name_1
; ///< Name of the president if the user did not change it.
75 uint32_t president_name_2
; ///< Parameter of #president_name_1
76 std::string president_name
; ///< Name of the president if the user changed it.
78 NetworkAuthorizedKeys allow_list
; ///< Public keys of clients that are allowed to join this company.
80 CompanyManagerFace face
; ///< Face description of the president.
82 Money money
; ///< Money owned by the company.
83 uint8_t money_fraction
; ///< Fraction of money of the company, too small to represent in #money.
84 Money current_loan
; ///< Amount of money borrowed from the bank.
85 Money max_loan
; ///< Max allowed amount of the loan or COMPANY_MAX_LOAN_DEFAULT.
87 Colours colour
; ///< Company colour.
89 uint8_t block_preview
; ///< Number of quarters that the company is not allowed to get new exclusive engine previews (see CompaniesGenStatistics).
91 TileIndex location_of_HQ
; ///< Northern tile of HQ; #INVALID_TILE when there is none.
92 TileIndex last_build_coordinate
; ///< Coordinate of the last build thing by this company.
94 TimerGameEconomy::Year inaugurated_year
; ///< Economy year of starting the company.
95 TimerGameCalendar::Year inaugurated_year_calendar
; ///< Calendar year of starting the company. Used to display proper Inauguration year while in wallclock mode.
97 uint8_t months_empty
= 0; ///< NOSAVE: Number of months this company has not had a client in multiplayer.
98 uint8_t months_of_bankruptcy
; ///< Number of months that the company is unable to pay its debts
99 CompanyMask bankrupt_asked
; ///< which companies were asked about buying it?
100 int16_t bankrupt_timeout
; ///< If bigger than \c 0, amount of time to wait for an answer on an offer to buy this company.
101 Money bankrupt_value
;
103 uint32_t terraform_limit
; ///< Amount of tileheights we can (still) terraform (times 65536).
104 uint32_t clear_limit
; ///< Amount of tiles we can (still) clear (times 65536).
105 uint32_t tree_limit
; ///< Amount of trees we can (still) plant (times 65536).
106 uint32_t build_object_limit
; ///< Amount of tiles we can (still) build objects on (times 65536). Also applies to buying land.
109 * If \c true, the company is (also) controlled by the computer (a NoAI program).
110 * @note It is possible that the user is also participating in such a company.
114 std::array
<Expenses
, 3> yearly_expenses
{}; ///< Expenses of the company for the last three years.
115 CompanyEconomyEntry cur_economy
; ///< Economic data of the company of this quarter.
116 CompanyEconomyEntry old_economy
[MAX_HISTORY_QUARTERS
]; ///< Economic data of the company of the last #MAX_HISTORY_QUARTERS quarters.
117 uint8_t num_valid_stat_ent
; ///< Number of valid statistical entries in #old_economy.
119 Livery livery
[LS_END
];
121 EngineRenewList engine_renew_list
; ///< Engine renewals of this company.
122 CompanySettings settings
; ///< settings specific for each company
124 // TODO: Change some of these member variables to use relevant INVALID_xxx constants
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) {}
133 struct Company
: CompanyProperties
, CompanyPool::PoolItem
<&_company_pool
> {
134 Company(uint16_t name_1
= 0, bool is_ai
= false);
137 RailTypes avail_railtypes
; ///< Rail types available to this company.
138 RoadTypes avail_roadtypes
; ///< Road types available to this company.
140 std::unique_ptr
<class AIInstance
> ai_instance
;
141 class AIInfo
*ai_info
;
142 std::unique_ptr
<class AIConfig
> ai_config
;
144 GroupStatistics group_all
[VEH_COMPANY_END
]; ///< NOSAVE: Statistics for the ALL_GROUP group.
145 GroupStatistics group_default
[VEH_COMPANY_END
]; ///< NOSAVE: Statistics for the DEFAULT_GROUP group.
147 CompanyInfrastructure infrastructure
; ///< NOSAVE: Counts of company owned infrastructure.
149 FreeUnitIDGenerator freeunits
[VEH_COMPANY_END
];
150 FreeUnitIDGenerator freegroups
;
152 Money
GetMaxLoan() const;
155 * Is this company a valid company, controlled by the computer (a NoAI program)?
156 * @param index Index in the pool.
157 * @return \c true if it is a valid, computer controlled company, else \c false.
159 static inline bool IsValidAiID(size_t index
)
161 const Company
*c
= Company::GetIfValid(index
);
162 return c
!= nullptr && c
->is_ai
;
166 * Is this company a valid company, not controlled by a NoAI program?
167 * @param index Index in the pool.
168 * @return \c true if it is a valid, human controlled company, else \c false.
169 * @note If you know that \a index refers to a valid company, you can use #IsHumanID() instead.
171 static inline bool IsValidHumanID(size_t index
)
173 const Company
*c
= Company::GetIfValid(index
);
174 return c
!= nullptr && !c
->is_ai
;
178 * Is this company a company not controlled by a NoAI program?
179 * @param index Index in the pool.
180 * @return \c true if it is a human controlled company, else \c false.
181 * @pre \a index must be a valid CompanyID.
182 * @note If you don't know whether \a index refers to a valid company, you should use #IsValidHumanID() instead.
184 static inline bool IsHumanID(size_t index
)
186 return !Company::Get(index
)->is_ai
;
189 static void PostDestructor(size_t index
);
192 Money
CalculateCompanyValue(const Company
*c
, bool including_loan
= true);
193 Money
CalculateHostileTakeoverValue(const Company
*c
);
195 extern uint _cur_company_tick_index
;
197 #endif /* COMPANY_BASE_H */