2009-08-21 21:21:05 +01:00
/*
* This file is part of OpenTTD .
* 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.
* 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 .
* 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/>.
*/
2008-09-30 21:39:50 +01:00
/** @file company_base.h Definition of stuff that is very close to a company, like the company struct itself. */
2007-03-21 17:42:43 +00:00
2008-09-30 21:39:50 +01:00
# ifndef COMPANY_BASE_H
# define COMPANY_BASE_H
2004-08-09 18:04:08 +01:00
2008-01-09 21:05:03 +00:00
# include "road_type.h"
2006-09-15 13:27:00 +01:00
# include "livery.h"
2008-01-07 09:19:53 +00:00
# include "autoreplace_type.h"
2008-01-09 21:27:39 +00:00
# include "tile_type.h"
2024-01-22 14:04:34 +00:00
# include "timer/timer_game_economy.h"
2009-05-16 00:55:06 +01:00
# include "settings_type.h"
2011-10-03 18:22:56 +01:00
# include "group.h"
2004-08-20 10:32:32 +01:00
2024-01-30 18:15:19 +00:00
static const Money COMPANY_MAX_LOAN_DEFAULT = INT64_MIN ;
2012-01-20 20:18:19 +00:00
/** Statistics about the economy. */
2008-09-30 21:39:50 +01:00
struct CompanyEconomyEntry {
2012-01-20 20:18:19 +00:00
Money income ; ///< The amount of income.
Money expenses ; ///< The amount of expenses.
2023-05-23 12:23:50 +01:00
CargoArray delivered_cargo { } ; ///< The amount of delivered cargo.
2023-05-08 18:01:06 +01:00
int32_t performance_history ; ///< Company score (scale 0-1000)
2012-01-20 20:18:19 +00:00
Money company_value ; ///< The value of the company.
2007-03-07 12:11:48 +00:00
} ;
2004-08-09 18:04:08 +01:00
2011-12-03 23:40:08 +00:00
struct CompanyInfrastructure {
2024-04-16 15:47:41 +01:00
std : : array < uint32_t , RAILTYPE_END > rail { } ; ///< Count of company owned track bits for each rail type.
std : : array < uint32_t , ROADTYPE_END > road { } ; ///< Count of company owned track bits for each road type.
2023-05-08 18:01:06 +01:00
uint32_t signal ; ///< Count of company owned signals.
uint32_t water ; ///< Count of company owned track bits for canals.
uint32_t station ; ///< Count of company owned station tiles.
uint32_t airport ; ///< Count of company owned airports.
2012-02-11 22:43:39 +00:00
2024-04-16 20:57:12 +01:00
auto operator < = > ( const CompanyInfrastructure & ) const = default ;
2012-02-11 22:43:39 +00:00
/** Get total sum of all owned track bits. */
2023-05-08 18:01:06 +01:00
uint32_t GetRailTotal ( ) const
2012-02-11 22:43:39 +00:00
{
2024-04-20 10:22:19 +01:00
return std : : accumulate ( std : : begin ( this - > rail ) , std : : end ( this - > rail ) , 0U ) ;
2012-02-11 22:43:39 +00:00
}
2019-04-06 07:46:15 +01:00
2023-05-08 18:01:06 +01:00
uint32_t GetRoadTotal ( ) const ;
uint32_t GetTramTotal ( ) const ;
2011-12-03 23:40:08 +00:00
} ;
2024-02-25 12:36:13 +00:00
class FreeUnitIDGenerator {
public :
UnitID NextID ( ) const ;
UnitID UseID ( UnitID index ) ;
void ReleaseID ( UnitID index ) ;
private :
using BitmapStorage = size_t ;
static constexpr size_t BITMAP_SIZE = std : : numeric_limits < BitmapStorage > : : digits ;
std : : vector < BitmapStorage > used_bitmap ;
} ;
2015-08-10 21:24:13 +01:00
typedef Pool < Company , CompanyID , 1 , MAX_COMPANIES > CompanyPool ;
2009-05-22 16:13:50 +01:00
extern CompanyPool _company_pool ;
2008-07-18 17:40:29 +01:00
2010-06-13 15:15:11 +01:00
/** Statically loadable part of Company pool item */
struct CompanyProperties {
2023-05-08 18:01:06 +01:00
uint32_t name_2 ; ///< Parameter of #name_1.
2017-02-26 19:41:14 +00:00
StringID name_1 ; ///< Name of the company if the user did not change it.
2020-05-17 22:31:59 +01:00
std : : string name ; ///< Name of the company if the user changed it.
2004-08-09 18:04:08 +01:00
2017-02-26 19:41:14 +00:00
StringID president_name_1 ; ///< Name of the president if the user did not change it.
2023-05-08 18:01:06 +01:00
uint32_t president_name_2 ; ///< Parameter of #president_name_1
2020-05-17 22:31:59 +01:00
std : : string president_name ; ///< Name of the president if the user changed it.
2004-08-09 18:04:08 +01:00
2024-03-23 10:53:08 +00:00
NetworkAuthorizedKeys allow_list ; ///< Public keys of clients that are allowed to join this company.
2010-08-02 21:32:39 +01:00
CompanyManagerFace face ; ///< Face description of the president.
2004-08-09 18:04:08 +01:00
2010-08-02 21:37:32 +01:00
Money money ; ///< Money owned by the company.
2024-03-16 22:59:32 +00:00
uint8_t money_fraction ; ///< Fraction of money of the company, too small to represent in #money.
2010-08-02 21:32:39 +01:00
Money current_loan ; ///< Amount of money borrowed from the bank.
2024-01-30 18:15:19 +00:00
Money max_loan ; ///< Max allowed amount of the loan or COMPANY_MAX_LOAN_DEFAULT.
2004-08-09 18:04:08 +01:00
2024-01-21 13:23:04 +00:00
Colours colour ; ///< Company colour.
2010-06-13 15:15:11 +01:00
2024-03-16 22:59:32 +00:00
uint8_t block_preview ; ///< Number of quarters that the company is not allowed to get new exclusive engine previews (see CompaniesGenStatistics).
2004-08-09 18:04:08 +01:00
2010-08-02 21:47:27 +01:00
TileIndex location_of_HQ ; ///< Northern tile of HQ; #INVALID_TILE when there is none.
2010-08-02 21:32:39 +01:00
TileIndex last_build_coordinate ; ///< Coordinate of the last build thing by this company.
2004-08-31 17:12:52 +01:00
2024-01-22 14:04:34 +00:00
TimerGameEconomy : : Year inaugurated_year ; ///< Economy year of starting the company.
2024-06-19 00:22:43 +01:00
TimerGameCalendar : : Year inaugurated_year_calendar ; ///< Calendar year of starting the company. Used to display proper Inauguration year while in wallclock mode.
2004-08-31 17:12:52 +01:00
2024-03-23 08:35:28 +00:00
uint8_t months_empty = 0 ; ///< NOSAVE: Number of months this company has not had a client in multiplayer.
2024-03-16 22:59:32 +00:00
uint8_t months_of_bankruptcy ; ///< Number of months that the company is unable to pay its debts
2010-08-02 21:37:32 +01:00
CompanyMask bankrupt_asked ; ///< which companies were asked about buying it?
2023-05-08 18:01:06 +01:00
int16_t bankrupt_timeout ; ///< If bigger than \c 0, amount of time to wait for an answer on an offer to buy this company.
2007-06-18 22:44:47 +01:00
Money bankrupt_value ;
2004-08-09 18:04:08 +01:00
2023-05-08 18:01:06 +01:00
uint32_t terraform_limit ; ///< Amount of tileheights we can (still) terraform (times 65536).
uint32_t clear_limit ; ///< Amount of tiles we can (still) clear (times 65536).
uint32_t tree_limit ; ///< Amount of trees we can (still) plant (times 65536).
uint32_t build_object_limit ; ///< Amount of tiles we can (still) build objects on (times 65536). Also applies to buying land.
2011-01-04 22:50:09 +00:00
2010-08-02 21:47:27 +01:00
/**
* If \ c true , the company is ( also ) controlled by the computer ( a NoAI program ) .
* @ note It is possible that the user is also participating in such a company .
*/
bool is_ai ;
2009-01-12 17:11:45 +00:00
2023-09-09 14:15:53 +01:00
std : : array < Expenses , 3 > yearly_expenses { } ; ///< Expenses of the company for the last three years.
2011-05-19 22:03:23 +01:00
CompanyEconomyEntry cur_economy ; ///< Economic data of the company of this quarter.
CompanyEconomyEntry old_economy [ MAX_HISTORY_QUARTERS ] ; ///< Economic data of the company of the last #MAX_HISTORY_QUARTERS quarters.
2024-03-16 22:59:32 +00:00
uint8_t num_valid_stat_ent ; ///< Number of valid statistical entries in #old_economy.
2010-06-13 15:15:11 +01:00
2021-06-09 19:04:53 +01:00
Livery livery [ LS_END ] ;
EngineRenewList engine_renew_list ; ///< Engine renewals of this company.
CompanySettings settings ; ///< settings specific for each company
2018-05-20 17:10:45 +01:00
// TODO: Change some of these member variables to use relevant INVALID_xxx constants
CompanyProperties ( )
2020-05-17 22:31:59 +01:00
: name_2 ( 0 ) , name_1 ( 0 ) , president_name_1 ( 0 ) , president_name_2 ( 0 ) ,
2024-01-30 18:15:19 +00:00
face ( 0 ) , money ( 0 ) , money_fraction ( 0 ) , current_loan ( 0 ) , max_loan ( COMPANY_MAX_LOAN_DEFAULT ) ,
colour ( COLOUR_BEGIN ) , block_preview ( 0 ) , location_of_HQ ( 0 ) , last_build_coordinate ( 0 ) , inaugurated_year ( 0 ) ,
2018-05-20 17:10:45 +01:00
months_of_bankruptcy ( 0 ) , bankrupt_asked ( 0 ) , bankrupt_timeout ( 0 ) , bankrupt_value ( 0 ) ,
2022-02-03 00:24:52 +00:00
terraform_limit ( 0 ) , clear_limit ( 0 ) , tree_limit ( 0 ) , build_object_limit ( 0 ) , is_ai ( false ) , engine_renew_list ( nullptr ) { }
2010-06-13 15:15:11 +01:00
} ;
2021-06-09 19:04:53 +01:00
struct Company : CompanyProperties , CompanyPool : : PoolItem < & _company_pool > {
2025-01-01 20:16:59 +00:00
Company ( StringID name_1 = STR_NULL , bool is_ai = false ) ;
2010-06-13 15:15:11 +01:00
~ Company ( ) ;
2018-05-19 22:31:46 +01:00
RailTypes avail_railtypes ; ///< Rail types available to this company.
2010-08-02 21:32:39 +01:00
RoadTypes avail_roadtypes ; ///< Road types available to this company.
2010-06-13 15:15:11 +01:00
2024-04-20 15:58:46 +01:00
std : : unique_ptr < class AIInstance > ai_instance ;
2010-06-13 15:15:11 +01:00
class AIInfo * ai_info ;
2024-02-09 21:55:49 +00:00
std : : unique_ptr < class AIConfig > ai_config ;
2010-06-13 15:15:11 +01:00
2011-10-03 18:23:41 +01:00
GroupStatistics group_all [ VEH_COMPANY_END ] ; ///< NOSAVE: Statistics for the ALL_GROUP group.
2011-10-03 18:22:56 +01:00
GroupStatistics group_default [ VEH_COMPANY_END ] ; ///< NOSAVE: Statistics for the DEFAULT_GROUP group.
2009-06-10 23:05:01 +01:00
2011-12-03 23:40:08 +00:00
CompanyInfrastructure infrastructure ; ///< NOSAVE: Counts of company owned infrastructure.
2024-02-25 12:36:13 +00:00
FreeUnitIDGenerator freeunits [ VEH_COMPANY_END ] ;
2024-05-07 20:01:28 +01:00
FreeUnitIDGenerator freegroups ;
2024-02-25 12:36:13 +00:00
2024-01-30 18:15:19 +00:00
Money GetMaxLoan ( ) const ;
2010-08-01 18:45:53 +01:00
/**
* Is this company a valid company , controlled by the computer ( a NoAI program ) ?
* @ param index Index in the pool .
* @ return \ c true if it is a valid , computer controlled company , else \ c false .
*/
2011-12-20 17:57:56 +00:00
static inline bool IsValidAiID ( size_t index )
2009-06-10 23:05:01 +01:00
{
2009-06-23 13:11:35 +01:00
const Company * c = Company : : GetIfValid ( index ) ;
2019-04-10 22:07:06 +01:00
return c ! = nullptr & & c - > is_ai ;
2009-06-10 23:05:01 +01:00
}
2010-08-01 18:45:53 +01:00
/**
2010-08-02 21:47:27 +01:00
* Is this company a valid company , not controlled by a NoAI program ?
2010-08-01 18:45:53 +01:00
* @ param index Index in the pool .
* @ return \ c true if it is a valid , human controlled company , else \ c false .
* @ note If you know that \ a index refers to a valid company , you can use # IsHumanID ( ) instead .
*/
2011-12-20 17:57:56 +00:00
static inline bool IsValidHumanID ( size_t index )
2009-06-10 23:05:01 +01:00
{
2009-06-23 13:11:35 +01:00
const Company * c = Company : : GetIfValid ( index ) ;
2019-04-10 22:07:06 +01:00
return c ! = nullptr & & ! c - > is_ai ;
2009-06-10 23:05:01 +01:00
}
2009-06-23 13:11:35 +01:00
2010-08-01 18:45:53 +01:00
/**
2010-08-02 21:47:27 +01:00
* Is this company a company not controlled by a NoAI program ?
2010-08-01 18:45:53 +01:00
* @ param index Index in the pool .
* @ return \ c true if it is a human controlled company , else \ c false .
* @ pre \ a index must be a valid CompanyID .
* @ note If you don ' t know whether \ a index refers to a valid company , you should use # IsValidHumanID ( ) instead .
*/
2011-12-20 17:57:56 +00:00
static inline bool IsHumanID ( size_t index )
2009-06-23 13:11:35 +01:00
{
return ! Company : : Get ( index ) - > is_ai ;
}
2009-09-08 13:27:27 +01:00
static void PostDestructor ( size_t index ) ;
2009-05-17 02:00:56 +01:00
} ;
2008-07-18 17:40:29 +01:00
2010-01-24 11:05:26 +00:00
Money CalculateCompanyValue ( const Company * c , bool including_loan = true ) ;
2023-06-05 18:32:22 +01:00
Money CalculateHostileTakeoverValue ( const Company * c ) ;
2006-09-15 13:27:00 +01:00
2009-02-25 21:45:14 +00:00
extern uint _cur_company_tick_index ;
2008-09-30 21:39:50 +01:00
# endif /* COMPANY_BASE_H */