OpenTTD Source
20241120-master-g6d3adc6169
|
Handling of the economy. More...
#include "stdafx.h"
#include "company_func.h"
#include "command_func.h"
#include "industry.h"
#include "town.h"
#include "news_func.h"
#include "network/network.h"
#include "network/network_func.h"
#include "ai/ai.hpp"
#include "aircraft.h"
#include "train.h"
#include "newgrf_engine.h"
#include "engine_base.h"
#include "ground_vehicle.hpp"
#include "newgrf_cargo.h"
#include "newgrf_sound.h"
#include "newgrf_industrytiles.h"
#include "newgrf_station.h"
#include "newgrf_airporttiles.h"
#include "newgrf_roadstop.h"
#include "object.h"
#include "strings_func.h"
#include "vehicle_func.h"
#include "sound_func.h"
#include "autoreplace_func.h"
#include "company_gui.h"
#include "signs_base.h"
#include "subsidy_base.h"
#include "subsidy_func.h"
#include "station_base.h"
#include "waypoint_base.h"
#include "economy_base.h"
#include "core/pool_func.hpp"
#include "core/backup_type.hpp"
#include "core/container_func.hpp"
#include "cargo_type.h"
#include "water.h"
#include "game/game.hpp"
#include "cargomonitor.h"
#include "goal_base.h"
#include "story_base.h"
#include "linkgraph/refresh.h"
#include "company_cmd.h"
#include "economy_cmd.h"
#include "vehicle_cmd.h"
#include "timer/timer.h"
#include "timer/timer_game_calendar.h"
#include "timer/timer_game_economy.h"
#include "table/strings.h"
#include "table/pricebase.h"
#include "safeguards.h"
Go to the source code of this file.
Data Structures | |
struct | IsEmptyAction |
Action to check if a vehicle has no stored cargo. More... | |
struct | PrepareRefitAction |
Refit preparation action. More... | |
struct | ReturnCargoAction |
Action for returning reserved cargo. More... | |
struct | FinalizeRefitAction |
Action for finalizing a refit. More... | |
struct | ReserveCargoAction |
Typedefs | |
typedef std::vector< Industry * > | SmallIndustryList |
Functions | |
static int32_t | BigMulS (const int32_t a, const int32_t b, const uint8_t shift) |
Multiply two integer values and shift the results to right. More... | |
static Money | CalculateCompanyAssetValue (const Company *c) |
Calculate the value of the assets of a company. More... | |
Money | CalculateCompanyValue (const Company *c, bool including_loan) |
Calculate the value of the company. More... | |
Money | CalculateHostileTakeoverValue (const Company *c) |
Calculate what you have to pay to take over a company. More... | |
int | UpdateCompanyRatingAndValue (Company *c, bool update) |
if update is set to true, the economy is updated with this score (also the house is updated, should only be true in the on-tick event) More... | |
void | ChangeOwnershipOfCompanyItems (Owner old_owner, Owner new_owner) |
Change the ownership of all the items of a company. More... | |
static void | CompanyCheckBankrupt (Company *c) |
Check for bankruptcy of a company. More... | |
static void | CompaniesGenStatistics () |
Update the finances of all companies. More... | |
bool | AddInflation (bool check_year) |
Add monthly inflation. More... | |
void | RecomputePrices () |
Computes all prices, payments and maximum loan. | |
static void | CompaniesPayInterest () |
Let all companies pay the monthly interest on their loan. | |
static void | HandleEconomyFluctuations () |
void | ResetPriceBaseMultipliers () |
Reset changes to the price base multipliers. | |
void | SetPriceBaseMultiplier (Price price, int factor) |
Change a price base by the given factor. More... | |
void | StartupIndustryDailyChanges (bool init_counter) |
Initialize the variables that will maintain the daily industry change system. More... | |
void | StartupEconomy () |
void | InitializeEconomy () |
Resets economy to initial values. | |
Money | GetPrice (Price index, uint cost_factor, const GRFFile *grf_file, int shift) |
Determine a certain price. More... | |
Money | GetTransportedGoodsIncome (uint num_pieces, uint dist, uint16_t transit_periods, CargoID cargo_type) |
static uint | DeliverGoodsToIndustry (const Station *st, CargoID cargo_type, uint num_pieces, IndustryID source, CompanyID company) |
Transfer goods from station to industry. More... | |
static Money | DeliverGoods (int num_pieces, CargoID cargo_type, StationID dest, uint distance, uint16_t periods_in_transit, Company *company, SourceType src_type, SourceID src) |
Delivers goods to industries/towns and calculates the payment. More... | |
static void | TriggerIndustryProduction (Industry *i) |
Inform the industry about just delivered cargo DeliverGoodsToIndustry() silently incremented incoming_cargo_waiting, now it is time to do something with the new cargo. More... | |
void | PrepareUnload (Vehicle *front_v) |
Prepare the vehicle to be unloaded. More... | |
static uint | GetLoadAmount (Vehicle *v) |
Gets the amount of cargo the given vehicle can load in the current tick. More... | |
template<class Taction > | |
bool | IterateVehicleParts (Vehicle *v, Taction action) |
Iterate the articulated parts of a vehicle, also considering the special cases of "normal" aircraft and double headed trains. More... | |
static void | HandleStationRefit (Vehicle *v, CargoArray &consist_capleft, Station *st, StationIDStack next_station, CargoID new_cid) |
Refit a vehicle in a station. More... | |
static bool | MayLoadUnderExclusiveRights (const Station *st, const Vehicle *v) |
Test whether a vehicle can load cargo at a station even if exclusive transport rights are present. More... | |
static void | ReserveConsist (Station *st, Vehicle *u, CargoArray *consist_capleft, StationIDStack *next_station) |
Reserves cargo if the full load order and improved_load is set or if the current order allows autorefit. More... | |
static void | UpdateLoadUnloadTicks (Vehicle *front, const Station *st, int ticks) |
Update the vehicle's load_unload_ticks, the time it will wait until it tries to load or unload again. More... | |
static void | LoadUnloadVehicle (Vehicle *front) |
Loads/unload the vehicle if possible. More... | |
void | LoadUnloadStation (Station *st) |
Load/unload the vehicles in this station according to the order they entered. More... | |
static void | DoAcquireCompany (Company *c, bool hostile_takeover) |
CommandCost | CmdBuyCompany (DoCommandFlag flags, CompanyID target_company, bool hostile_takeover) |
Buy up another company. More... | |
Variables | |
CargoPaymentPool | _cargo_payment_pool ("CargoPayment") |
const ScoreInfo | _score_info [] |
Score info, values used for computing the detailed performance rating. More... | |
int64_t | _score_part [MAX_COMPANIES][SCORE_END] |
Economy | _economy |
Prices | _price |
static PriceMultipliers | _price_base_multiplier |
static SmallIndustryList | _cargo_delivery_destinations |
The industries we've currently brought cargo to. | |
static IntervalTimer< TimerGameCalendar > | _calendar_inflation_monthly ({TimerGameCalendar::MONTH, TimerGameCalendar::Priority::COMPANY}, [](auto) { if(_settings_game.economy.inflation) { AddInflation();RecomputePrices();} }) |
Every calendar month update of inflation. | |
static IntervalTimer< TimerGameEconomy > | _economy_companies_monthly ({ TimerGameEconomy::MONTH, TimerGameEconomy::Priority::COMPANY }, [](auto) { CompaniesGenStatistics();CompaniesPayInterest();HandleEconomyFluctuations();}) |
Every economy month update of company economic data, plus economy fluctuations. | |
Handling of the economy.
Definition in file economy.cpp.
bool AddInflation | ( | bool | check_year | ) |
Add monthly inflation.
check_year | Shall the inflation get stopped after 170 years? |
Definition at line 724 of file economy.cpp.
References TimerGameConst< struct Calendar >::ORIGINAL_MAX_YEAR.
|
inlinestatic |
Multiply two integer values and shift the results to right.
This function multiplies two integer values. The result is shifted by the amount of shift to right.
a | The first integer |
b | The second integer |
shift | The amount to shift the value to right. |
Definition at line 80 of file economy.cpp.
Calculate the value of the assets of a company.
c | The company to calculate the value of. |
Definition at line 114 of file economy.cpp.
References CountBits(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, and SpecializedStation< Station, false >::Iterate().
Referenced by CalculateCompanyValue(), and CalculateHostileTakeoverValue().
Calculate the value of the company.
That is the value of all assets (vehicles, stations) and money (including loan), except when including_loan is false
which is useful when we want to calculate the value for bankruptcy.
c | the company to get the value of. |
including_loan | include the loan in the company value. |
Definition at line 149 of file economy.cpp.
References CalculateCompanyAssetValue(), CompanyProperties::current_loan, and CompanyProperties::money.
Calculate what you have to pay to take over a company.
This is different from bankruptcy and company value, and involves a few more parameters to make it more realistic.
You have to pay for:
And on top of that, they walk away with all the money they have in the bank.
c | the company to get the value of. |
Definition at line 176 of file economy.cpp.
References CalculateCompanyAssetValue(), CompanyProperties::current_loan, CompanyEconomyEntry::expenses, CompanyEconomyEntry::income, CompanyProperties::money, and CompanyProperties::old_economy.
Referenced by CmdBuyCompany().
Change the ownership of all the items of a company.
old_owner | The company that gets removed. |
new_owner | The company to merge to, or INVALID_OWNER to remove the company. |
Definition at line 334 of file economy.cpp.
References _current_company, _local_company, _networking, ClrBit(), GroupStatistics::CountEngine(), GroupStatistics::CountVehicle(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), HasBit(), INVALID_COMPANY, INVALID_OWNER, IsCompanyBuildableVehicleType(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_subsidy_pool >::Iterate(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_town_pool >::Iterate(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Iterate(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_vehicle_pool >::Iterate(), NetworkClientsToSpectators(), RATING_INITIAL, RebuildSubsidisedSourceAndDestinationCache(), RemoveAllEngineReplacementForCompany(), Backup< T >::Restore(), SetBit(), and SetLocalCompany().
Referenced by ResetLandscapeConfirmationCallback().
CommandCost CmdBuyCompany | ( | DoCommandFlag | flags, |
CompanyID | target_company, | ||
bool | hostile_takeover | ||
) |
Buy up another company.
When a competing company is gone bankrupt you get the chance to purchase that company.
flags | type of operation |
target_company | company to buy up |
hostile_takeover | whether to buy up the company even if it is not bankrupt |
Definition at line 2048 of file economy.cpp.
References _current_company, _local_company, _networking, CompanyProperties::bankrupt_asked, CalculateHostileTakeoverValue(), CheckTakeoverVehicleLimit(), CMD_ERROR, DC_EXEC, EXPENSES_OTHER, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::GetIfValid(), HasBit(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, CompanyProperties::is_ai, and return_cmd_error.
|
static |
Update the finances of all companies.
Pay for the stations, update the history graph, update ratings and company values, and deal with bankruptcy.
Definition at line 661 of file economy.cpp.
References _current_company, _settings_game, CommandCost::AddCost(), Backup< T >::Change(), CompanyCheckBankrupt(), GameSettings::economy, EXPENSES_PROPERTY, EconomySettings::infrastructure_maintenance, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Iterate(), RailMaintenanceCost(), RAILTYPE_BEGIN, RAILTYPE_END, RoadMaintenanceCost(), ROADTYPE_BEGIN, ROADTYPE_END, and SignalMaintenanceCost().
|
static |
Check for bankruptcy of a company.
Called every three months.
c | Company to check. |
Definition at line 568 of file economy.cpp.
References _settings_game, CompanyProperties::bankrupt_asked, CeilDiv(), CompanyNewsInformation::company_name, CompanyAdminUpdate(), CompanyProperties::current_loan, GameSettings::difficulty, Company::GetMaxLoan(), DifficultySettings::infinite_money, CompanyProperties::money, CompanyProperties::months_of_bankruptcy, SetDParam(), and SetDParamStr().
Referenced by CompaniesGenStatistics().
|
static |
Delivers goods to industries/towns and calculates the payment.
num_pieces | amount of cargo delivered |
cargo_type | the type of cargo that is delivered |
dest | Station the cargo has been unloaded |
distance | The distance the cargo has traveled. |
periods_in_transit | Travel time in cargo aging periods |
company | The company delivering the cargo |
src_type | Type of source of cargo (industry, town, headquarters) |
src | Index of source of cargo |
Definition at line 1114 of file economy.cpp.
Referenced by CargoPayment::PayFinalDelivery().
|
static |
Transfer goods from station to industry.
All cargo is delivered to the nearest (Manhattan) industry to the station sign, which is inside the acceptance rectangle and actually accepts the cargo.
st | The station that accepted the cargo |
cargo_type | Type of cargo delivered |
num_pieces | Amount of cargo delivered |
source | The source of the cargo |
company | The company delivering the cargo |
Definition at line 1059 of file economy.cpp.
References _cargo_delivery_destinations, Industry::accepted, AddCargoDelivery(), TimerGameEconomy::date, Industry::exclusive_supplier, Industry::GetCargoAccepted(), include(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, Station::industries_near, Industry, IndustryTemporarilyRefusesCargo(), INVALID_OWNER, and BaseStation::owner.
|
static |
Gets the amount of cargo the given vehicle can load in the current tick.
This is only about loading speed. The free capacity is ignored.
v | Vehicle to be queried. |
Definition at line 1323 of file economy.cpp.
References _settings_game, CeilDiv(), SpecializedVehicle< Aircraft, VEH_AIRCRAFT >::From(), Vehicle::GetEngine(), OrderSettings::gradual_loading, Aircraft::IsNormalAircraft(), GameSettings::order, BaseVehicle::type, and VEH_AIRCRAFT.
Determine a certain price.
index | Price base |
cost_factor | Price factor |
grf_file | NewGRF to use local price multipliers from. |
shift | Extra bit shifting after the computation |
Definition at line 969 of file economy.cpp.
Referenced by ObjectSpec::GetBuildCost(), RoadStopSpec::GetBuildCost(), ObjectSpec::GetClearCost(), and RoadStopSpec::GetClearCost().
|
static |
Refit a vehicle in a station.
v | Vehicle to be refitted. |
consist_capleft | Added cargo capacities in the consist. |
st | Station the vehicle is loading at. |
next_station | Possible next stations the vehicle can travel to. |
new_cid | Target cargo for refit. |
Definition at line 1500 of file economy.cpp.
References _current_company, CARGO_AUTO_REFIT, Vehicle::cargo_type, Vehicle::GetEngine(), Vehicle::GetFirstEnginePart(), IterateVehicleParts(), and Vehicle::owner.
bool IterateVehicleParts | ( | Vehicle * | v, |
Taction | action | ||
) |
Iterate the articulated parts of a vehicle, also considering the special cases of "normal" aircraft and double headed trains.
Apply an action to each vehicle and immediately return false if that action does so. Otherwise return true.
Taction | Class of action to be applied. Must implement bool operator()([const] Vehicle *). |
v | First articulated part. |
action | Instance of Taction. |
Definition at line 1368 of file economy.cpp.
References SpecializedVehicle< Aircraft, VEH_AIRCRAFT >::From(), SpecializedVehicle< T, Type >::From(), Vehicle::HasArticulatedPart(), GroundVehicle< T, Type >::IsMultiheaded(), Aircraft::IsNormalAircraft(), Vehicle::Next(), BaseVehicle::type, VEH_AIRCRAFT, and VEH_TRAIN.
Referenced by HandleStationRefit(), and ReserveConsist().
void LoadUnloadStation | ( | Station * | st | ) |
Load/unload the vehicles in this station according to the order they entered.
st | the station to do the loading/unloading for |
Definition at line 1953 of file economy.cpp.
References _cargo_delivery_destinations, Vehicle::load_unload_ticks, LoadUnloadVehicle(), TriggerIndustryProduction(), Vehicle::vehstatus, VS_CRASHED, and VS_STOPPED.
|
static |
Loads/unload the vehicle if possible.
front | the vehicle to be (un)loaded |
Definition at line 1640 of file economy.cpp.
References Vehicle::current_order, and Order::IsType().
Referenced by LoadUnloadStation().
Test whether a vehicle can load cargo at a station even if exclusive transport rights are present.
Definition at line 1560 of file economy.cpp.
References Town::exclusive_counter, Town::exclusivity, BaseStation::owner, Vehicle::owner, OWNER_NONE, and BaseStation::town.
void PrepareUnload | ( | Vehicle * | front_v | ) |
Prepare the vehicle to be unloaded.
front_v | the vehicle to be unloaded |
Definition at line 1281 of file economy.cpp.
References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_cargo_payment_pool >::CanAllocateItem(), GoodsEntry::cargo, Vehicle::cargo_payment, ClrBit(), Vehicle::current_order, GoodsEntry::GES_ACCEPTANCE, SpecializedStation< Station, false >::Get(), Vehicle::GetNextStoppingStation(), Order::GetUnloadType(), Station::goods, HasBit(), Vehicle::last_station_visited, Vehicle::load_unload_ticks, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::MAX_SIZE, Vehicle::Next(), Vehicle::orders, OUFB_NO_UNLOAD, SetBit(), GoodsEntry::status, BaseConsist::vehicle_flags, VF_CARGO_UNLOADING, and VF_LOADING_FINISHED.
|
static |
Reserves cargo if the full load order and improved_load is set or if the current order allows autorefit.
st | Station where the consist is loading at the moment. |
u | Front of the loading vehicle consist. |
consist_capleft | If given, save free capacities after reserving there. |
next_station | Station(s) the vehicle will stop at next. |
Definition at line 1592 of file economy.cpp.
References Vehicle::cargo_payment, Vehicle::current_order, SpecializedVehicle< T, Type >::From(), SpecializedVehicle< Aircraft, VEH_AIRCRAFT >::From(), Order::GetRefitCargo(), Aircraft::IsNormalAircraft(), Order::IsRefit(), IterateVehicleParts(), Vehicle::Next(), VEH_AIRCRAFT, and VEH_TRAIN.
void SetPriceBaseMultiplier | ( | Price | price, |
int | factor | ||
) |
Change a price base by the given factor.
The price base is altered by factors of two. NewBaseCost = OldBaseCost * 2^n
price | Index of price base to change. |
factor | Amount to change by. |
Definition at line 902 of file economy.cpp.
void StartupIndustryDailyChanges | ( | bool | init_counter | ) |
Initialize the variables that will maintain the daily industry change system.
init_counter | specifies if the counter is required to be initialized |
Definition at line 912 of file economy.cpp.
References Map::LogX(), and Map::LogY().
|
static |
Inform the industry about just delivered cargo DeliverGoodsToIndustry() silently incremented incoming_cargo_waiting, now it is time to do something with the new cargo.
i | The industry to process |
Definition at line 1164 of file economy.cpp.
References GetIndustrySpec(), and Industry::type.
Referenced by LoadUnloadStation().
int UpdateCompanyRatingAndValue | ( | Company * | c, |
bool | update | ||
) |
if update is set to true, the economy is updated with this score (also the house is updated, should only be true in the on-tick event)
update | the economy with calculated score |
c | company been evaluated |
Definition at line 201 of file economy.cpp.
References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index.
Update the vehicle's load_unload_ticks, the time it will wait until it tries to load or unload again.
Adjust for overhang of trains and set it at least to 1.
front | The vehicle to be updated. |
st | The station the vehicle is loading at. |
ticks | The time it would normally wait, based on cargo loaded and unloaded. |
Definition at line 1622 of file economy.cpp.
References _settings_game, GroundVehicleCache::cached_total_length, Vehicle::GetGroundVehicleCache(), Station::GetPlatformLength(), Vehicle::load_unload_ticks, GameSettings::order, OrderSettings::station_length_loading_penalty, Vehicle::tile, TILE_SIZE, BaseVehicle::type, and VEH_TRAIN.
const ScoreInfo _score_info[] |
Score info, values used for computing the detailed performance rating.
Definition at line 90 of file economy.cpp.