OpenTTD Source: src/economy_func.h Source File

OpenTTD Source 15.0-RC2

economy_func.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 ECONOMY_FUNC_H
11#define ECONOMY_FUNC_H
12
13#include "economy_type.h"
14#include "station_type.h"
15#include "cargo_type.h"
16#include "vehicle_type.h"
17#include "company_type.h"
18#include "settings_type.h"
19#include "source_type.h"
20#include "core/random_func.hpp"
21
22void ResetPriceBaseMultipliers();
23void SetPriceBaseMultiplier(Price price, int factor);
24
25extern const ScoreInfo _score_info[];
26extern TypedIndexContainer<std::array<std::array<int64_t, SCORE_END>, MAX_COMPANIES>, CompanyID> _score_part;
27extern Economy _economy;
28/* Prices and also the fractional part. */
29extern Prices _price;
30
31int UpdateCompanyRatingAndValue(Company *c, bool update);
32void StartupIndustryDailyChanges(bool init_counter);
33
34 Money GetTransportedGoodsIncome(uint num_pieces, uint dist, uint16_t transit_periods, CargoType cargo_type);
35uint MoveGoodsToStation(CargoType cargo, uint amount, Source source, const StationList &all_stations, Owner exclusivity = INVALID_OWNER);
36
37void PrepareUnload(Vehicle *front_v);
38void LoadUnloadStation(Station *st);
39
40 Money GetPrice(Price index, uint cost_factor, const struct GRFFile *grf_file, int shift = 0);
41
42void InitializeEconomy();
43void RecomputePrices();
44bool AddInflation(bool check_year = true);
45
50 inline bool EconomyIsInRecession()
51{
52 return _economy.fluct <= 0;
53}
54
61 static uint ScaleByInverseCargoScale(uint num, bool town)
62{
63 uint16_t percentage = (town ? _settings_game.economy.town_cargo_scale : _settings_game.economy.industry_cargo_scale);
64
65 /* We might not need to do anything. */
66 if (percentage == 100) return num;
67
68 /* Never return 0, since we often divide by this number. */
69 return std::max((num * 100) / percentage, 1u);
70}
71
78 inline uint ScaleByCargoScale(uint num, bool town)
79{
80 /* Don't bother scaling in the menu, especially since settings don't exist when starting OpenTTD and trying to read them crashes the game. */
81 if (_game_mode == GM_MENU) return num;
82
83 if (num == 0) return num;
84
85 uint16_t percentage = (town ? _settings_game.economy.town_cargo_scale : _settings_game.economy.industry_cargo_scale);
86
87 /* We might not need to do anything. */
88 if (percentage == 100) return num;
89
90 uint scaled = (num * percentage) / 100;
91
92 /* We might round down to 0, so we compensate with a random chance approximately equal to the economy scale,
93 * e.g. at 25% scale there's a 1/4 chance to round up to 1. */
94 if (scaled == 0 && Chance16(1, ScaleByInverseCargoScale(1, town))) return 1;
95
96 return scaled;
97}
98
99#endif /* ECONOMY_FUNC_H */
cargo_type.h
Types related to cargoes...
CargoType
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:21
OverflowSafeInt< int64_t >
TypedIndexContainer
A sort-of mixin that implements 'at(pos)' and 'operator[](pos)' only for a specific type.
Definition convertible_through_base.hpp:38
company_type.h
Types related to companies.
INVALID_OWNER
static constexpr Owner INVALID_OWNER
An invalid owner.
Definition company_type.h:30
GetPrice
Money GetPrice(Price index, uint cost_factor, const GRFFile *grf_file, int shift)
Determine a certain price.
Definition economy.cpp:949
UpdateCompanyRatingAndValue
int UpdateCompanyRatingAndValue(Company *c, bool update)
if update is set to true, the economy is updated with this score (also the house is updated,...
Definition economy.cpp:202
_score_info
const ScoreInfo _score_info[]
Score info, values used for computing the detailed performance rating.
Definition economy.cpp:91
ScaleByInverseCargoScale
static uint ScaleByInverseCargoScale(uint num, bool town)
Scale a number by the inverse of the cargo scale setting, e.g.
Definition economy_func.h:61
AddInflation
bool AddInflation(bool check_year=true)
Add monthly inflation.
Definition economy.cpp:704
LoadUnloadStation
void LoadUnloadStation(Station *st)
Load/unload the vehicles in this station according to the order they entered.
Definition economy.cpp:1929
RecomputePrices
void RecomputePrices()
Computes all prices, payments and maximum loan.
Definition economy.cpp:742
PrepareUnload
void PrepareUnload(Vehicle *front_v)
Prepare the vehicle to be unloaded.
Definition economy.cpp:1255
EconomyIsInRecession
bool EconomyIsInRecession()
Is the economy in recession?
Definition economy_func.h:50
SetPriceBaseMultiplier
void SetPriceBaseMultiplier(Price price, int factor)
Change a price base by the given factor.
Definition economy.cpp:882
InitializeEconomy
void InitializeEconomy()
Resets economy to initial values.
Definition economy.cpp:934
ResetPriceBaseMultipliers
void ResetPriceBaseMultipliers()
Reset changes to the price base multipliers.
Definition economy.cpp:870
ScaleByCargoScale
uint ScaleByCargoScale(uint num, bool town)
Scale a number by the cargo scale setting.
Definition economy_func.h:78
StartupIndustryDailyChanges
void StartupIndustryDailyChanges(bool init_counter)
Initialize the variables that will maintain the daily industry change system.
Definition economy.cpp:892
economy_type.h
Types related to the economy.
Prices
Money Prices[PR_END]
Prices of everything.
Definition economy_type.h:174
Price
Price
Enumeration of all base prices for use with Prices.
Definition economy_type.h:95
random_func.hpp
Pseudo random number generator.
Chance16
bool Chance16(const uint32_t a, const uint32_t b, const std::source_location location=std::source_location::current())
Flips a coin with given probability.
Definition random_func.hpp:134
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
settings_type.h
Types related to global configuration settings.
source_type.h
Type for the source of cargo.
station_type.h
Types related to stations.
StationList
std::set< Station *, StationCompare > StationList
List of stations.
Definition station_type.h:131
Company
Definition company_base.h:127
EconomySettings::industry_cargo_scale
uint16_t industry_cargo_scale
scale cargo production of industries by this percentage.
Definition settings_type.h:567
EconomySettings::town_cargo_scale
uint16_t town_cargo_scale
scale cargo production of towns by this percentage.
Definition settings_type.h:566
Economy
Data of the economy.
Definition economy_type.h:44
Economy::fluct
int16_t fluct
Economy fluctuation status.
Definition economy_type.h:46
GRFFile
Dynamic data of a loaded NewGRF.
Definition newgrf.h:114
GameSettings::economy
EconomySettings economy
settings to change the economy
Definition settings_type.h:642
PoolID< uint8_t, struct CompanyIDTag, 0xF, 0xFF >
ScoreInfo
Data structure for storing how the score is computed for a single score id.
Definition economy_type.h:86
Source
A location from where cargo can come from (or go to).
Definition source_type.h:32
Station
Station data structure.
Definition station_base.h:500
Vehicle
Vehicle data structure.
Definition vehicle_base.h:205
vehicle_type.h
Types related to vehicles.