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 
8 /** @file company_base.h Definition of stuff that is very close to a company, like the company struct itself. */
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"
17 #include "settings_type.h"
18 #include "group.h"
19 #include <string>
20 
21 /** Statistics about the economy. */
22 struct CompanyEconomyEntry {
23 	Money income;               ///< The amount of income.
24 	Money expenses;             ///< The amount of expenses.
25 	CargoArray delivered_cargo; ///< The amount of delivered cargo.
26 	int32 performance_history;  ///< Company score (scale 0-1000)
27 	Money company_value;        ///< The value of the company.
28 };
29 
30 struct CompanyInfrastructure {
31 	uint32 road[ROADTYPE_END]; ///< Count of company owned track bits for each road type.
32 	uint32 signal;             ///< Count of company owned signals.
33 	uint32 rail[RAILTYPE_END]; ///< Count of company owned track bits for each rail type.
34 	uint32 water;              ///< Count of company owned track bits for canals.
35 	uint32 station;            ///< Count of company owned station tiles.
36 	uint32 airport;            ///< Count of company owned airports.
37 
38 	/** Get total sum of all owned track bits. */
GetRailTotalCompanyInfrastructure39 	uint32 GetRailTotal() const
40 	{
41 		uint32 total = 0;
42 		for (RailType rt =  RAILTYPE_BEGIN; rt < RAILTYPE_END; rt++) total += this->rail[rt];
43 		return total;
44 	}
45 
46 	uint32 GetRoadTotal() const;
47 	uint32 GetTramTotal() const;
48 };
49 
50 typedef Pool<Company, CompanyID, 1, MAX_COMPANIES> CompanyPool;
51 extern CompanyPool _company_pool;
52 
53 
54 /** Statically loadable part of Company pool item */
55 struct CompanyProperties {
56 	uint32 name_2;                   ///< Parameter of #name_1.
57 	StringID name_1;                 ///< Name of the company if the user did not change it.
58 	std::string name;                ///< Name of the company if the user changed it.
59 
60 	StringID president_name_1;       ///< Name of the president if the user did not change it.
61 	uint32 president_name_2;         ///< Parameter of #president_name_1
62 	std::string president_name;      ///< Name of the president if the user changed it.
63 
64 	CompanyManagerFace face;         ///< Face description of the president.
65 
66 	Money money;                     ///< Money owned by the company.
67 	byte money_fraction;             ///< Fraction of money of the company, too small to represent in #money.
68 	Money current_loan;              ///< Amount of money borrowed from the bank.
69 
70 	byte colour;                     ///< Company colour.
71 
72 	byte block_preview;              ///< Number of quarters that the company is not allowed to get new exclusive engine previews (see CompaniesGenStatistics).
73 
74 	TileIndex location_of_HQ;        ///< Northern tile of HQ; #INVALID_TILE when there is none.
75 	TileIndex last_build_coordinate; ///< Coordinate of the last build thing by this company.
76 
77 	Owner share_owners[4];           ///< Owners of the 4 shares of the company. #INVALID_OWNER if nobody has bought them yet.
78 
79 	Year inaugurated_year;           ///< Year of starting the company.
80 
81 	byte months_of_bankruptcy;       ///< Number of months that the company is unable to pay its debts
82 	CompanyMask bankrupt_asked;      ///< which companies were asked about buying it?
83 	int16 bankrupt_timeout;          ///< If bigger than \c 0, amount of time to wait for an answer on an offer to buy this company.
84 	Money bankrupt_value;
85 
86 	uint32 terraform_limit;          ///< Amount of tileheights we can (still) terraform (times 65536).
87 	uint32 clear_limit;              ///< Amount of tiles we can (still) clear (times 65536).
88 	uint32 tree_limit;               ///< Amount of trees we can (still) plant (times 65536).
89 
90 	/**
91 	 * If \c true, the company is (also) controlled by the computer (a NoAI program).
92 	 * @note It is possible that the user is also participating in such a company.
93 	 */
94 	bool is_ai;
95 
96 	Money yearly_expenses[3][EXPENSES_END];                ///< Expenses of the company for the last three years, in every #ExpensesType category.
97 	CompanyEconomyEntry cur_economy;                       ///< Economic data of the company of this quarter.
98 	CompanyEconomyEntry old_economy[MAX_HISTORY_QUARTERS]; ///< Economic data of the company of the last #MAX_HISTORY_QUARTERS quarters.
99 	byte num_valid_stat_ent;                               ///< Number of valid statistical entries in #old_economy.
100 
101 	Livery livery[LS_END];
102 
103 	EngineRenewList engine_renew_list; ///< Engine renewals of this company.
104 	CompanySettings settings;          ///< settings specific for each company
105 
106 	// TODO: Change some of these member variables to use relevant INVALID_xxx constants
CompanyPropertiesCompanyProperties107 	CompanyProperties()
108 		: name_2(0), name_1(0), president_name_1(0), president_name_2(0),
109 		  face(0), money(0), money_fraction(0), current_loan(0), colour(0), block_preview(0),
110 		  location_of_HQ(0), last_build_coordinate(0), share_owners(), inaugurated_year(0),
111 		  months_of_bankruptcy(0), bankrupt_asked(0), bankrupt_timeout(0), bankrupt_value(0),
112 		  terraform_limit(0), clear_limit(0), tree_limit(0), is_ai(false), engine_renew_list(nullptr) {}
113 };
114 
115 struct Company : CompanyProperties, CompanyPool::PoolItem<&_company_pool> {
116 	Company(uint16 name_1 = 0, bool is_ai = false);
117 	~Company();
118 
119 	RailTypes avail_railtypes;         ///< Rail types available to this company.
120 	RoadTypes avail_roadtypes;         ///< Road types available to this company.
121 
122 	class AIInstance *ai_instance;
123 	class AIInfo *ai_info;
124 
125 	GroupStatistics group_all[VEH_COMPANY_END];      ///< NOSAVE: Statistics for the ALL_GROUP group.
126 	GroupStatistics group_default[VEH_COMPANY_END];  ///< NOSAVE: Statistics for the DEFAULT_GROUP group.
127 
128 	CompanyInfrastructure infrastructure; ///< NOSAVE: Counts of company owned infrastructure.
129 
130 	/**
131 	 * Is this company a valid company, controlled by the computer (a NoAI program)?
132 	 * @param index Index in the pool.
133 	 * @return \c true if it is a valid, computer controlled company, else \c false.
134 	 */
IsValidAiIDCompany135 	static inline bool IsValidAiID(size_t index)
136 	{
137 		const Company *c = Company::GetIfValid(index);
138 		return c != nullptr && c->is_ai;
139 	}
140 
141 	/**
142 	 * Is this company a valid company, not controlled by a NoAI program?
143 	 * @param index Index in the pool.
144 	 * @return \c true if it is a valid, human controlled company, else \c false.
145 	 * @note If you know that \a index refers to a valid company, you can use #IsHumanID() instead.
146 	 */
IsValidHumanIDCompany147 	static inline bool IsValidHumanID(size_t index)
148 	{
149 		const Company *c = Company::GetIfValid(index);
150 		return c != nullptr && !c->is_ai;
151 	}
152 
153 	/**
154 	 * Is this company a company not controlled by a NoAI program?
155 	 * @param index Index in the pool.
156 	 * @return \c true if it is a human controlled company, else \c false.
157 	 * @pre \a index must be a valid CompanyID.
158 	 * @note If you don't know whether \a index refers to a valid company, you should use #IsValidHumanID() instead.
159 	 */
IsHumanIDCompany160 	static inline bool IsHumanID(size_t index)
161 	{
162 		return !Company::Get(index)->is_ai;
163 	}
164 
165 	static void PostDestructor(size_t index);
166 };
167 
168 Money CalculateCompanyValue(const Company *c, bool including_loan = true);
169 
170 extern uint _next_competitor_start;
171 extern uint _cur_company_tick_index;
172 
173 #endif /* COMPANY_BASE_H */
174