OpenTTD Source  20241120-master-g6d3adc6169
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 <http://www.gnu.org/licenses/>.
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 "core/random_func.hpp"
20 
22 void SetPriceBaseMultiplier(Price price, int factor);
23 
24 extern const ScoreInfo _score_info[];
25 extern int64_t _score_part[MAX_COMPANIES][SCORE_END];
26 extern Economy _economy;
27 /* Prices and also the fractional part. */
28 extern Prices _price;
29 
30 int UpdateCompanyRatingAndValue(Company *c, bool update);
31 void StartupIndustryDailyChanges(bool init_counter);
32 
33 Money GetTransportedGoodsIncome(uint num_pieces, uint dist, uint16_t transit_periods, CargoID cargo_type);
34 uint MoveGoodsToStation(CargoID type, uint amount, SourceType source_type, SourceID source_id, const StationList &all_stations, Owner exclusivity = INVALID_OWNER);
35 
36 void PrepareUnload(Vehicle *front_v);
37 void LoadUnloadStation(Station *st);
38 
39 Money GetPrice(Price index, uint cost_factor, const struct GRFFile *grf_file, int shift = 0);
40 
41 void InitializeEconomy();
42 void RecomputePrices();
43 bool AddInflation(bool check_year = true);
44 
49 inline bool EconomyIsInRecession()
50 {
51  return _economy.fluct <= 0;
52 }
53 
60 static uint ScaleByInverseCargoScale(uint num, bool town)
61 {
63 
64  /* We might not need to do anything. */
65  if (percentage == 100) return num;
66 
67  /* Never return 0, since we often divide by this number. */
68  return std::max((num * 100) / percentage, 1u);
69 }
70 
77 inline uint ScaleByCargoScale(uint num, bool town)
78 {
79  /* Don't bother scaling in the menu, especially since settings don't exist when starting OpenTTD and trying to read them crashes the game. */
80  if (_game_mode == GM_MENU) return num;
81 
82  if (num == 0) return num;
83 
85 
86  /* We might not need to do anything. */
87  if (percentage == 100) return num;
88 
89  uint scaled = (num * percentage) / 100;
90 
91  /* We might round down to 0, so we compensate with a random chance approximately equal to the economy scale,
92  * e.g. at 25% scale there's a 1/4 chance to round up to 1. */
93  if (scaled == 0 && Chance16(1, ScaleByInverseCargoScale(1, town))) return 1;
94 
95  return scaled;
96 }
97 
98 #endif /* ECONOMY_FUNC_H */
Types related to cargoes...
uint8_t CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
uint16_t SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
Definition: cargo_type.h:143
SourceType
Types of cargo source and destination.
Definition: cargo_type.h:137
Types related to companies.
Owner
Enum for all companies/owners.
Definition: company_type.h:18
@ INVALID_OWNER
An invalid owner.
Definition: company_type.h:29
@ MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:23
Money GetPrice(Price index, uint cost_factor, const GRFFile *grf_file, int shift)
Determine a certain price.
Definition: economy.cpp:969
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:201
const ScoreInfo _score_info[]
Score info, values used for computing the detailed performance rating.
Definition: economy.cpp:90
static uint ScaleByInverseCargoScale(uint num, bool town)
Scale a number by the inverse of the cargo scale setting, e.g.
Definition: economy_func.h:60
bool AddInflation(bool check_year=true)
Add monthly inflation.
Definition: economy.cpp:724
void LoadUnloadStation(Station *st)
Load/unload the vehicles in this station according to the order they entered.
Definition: economy.cpp:1953
void RecomputePrices()
Computes all prices, payments and maximum loan.
Definition: economy.cpp:762
void PrepareUnload(Vehicle *front_v)
Prepare the vehicle to be unloaded.
Definition: economy.cpp:1281
bool EconomyIsInRecession()
Is the economy in recession?
Definition: economy_func.h:49
void SetPriceBaseMultiplier(Price price, int factor)
Change a price base by the given factor.
Definition: economy.cpp:902
void InitializeEconomy()
Resets economy to initial values.
Definition: economy.cpp:954
void ResetPriceBaseMultipliers()
Reset changes to the price base multipliers.
Definition: economy.cpp:890
uint ScaleByCargoScale(uint num, bool town)
Scale a number by the cargo scale setting.
Definition: economy_func.h:77
void StartupIndustryDailyChanges(bool init_counter)
Initialize the variables that will maintain the daily industry change system.
Definition: economy.cpp:912
Types related to the economy.
@ SCORE_END
How many scores are there..
Definition: economy_type.h:72
Money Prices[PR_END]
Prices of everything.
Definition: economy_type.h:168
Price
Enumeration of all base prices for use with Prices.
Definition: economy_type.h:89
Pseudo random number generator.
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.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:57
Types related to global configuration settings.
Types related to stations.
std::set< Station *, StationCompare > StationList
List of stations.
Definition: station_type.h:94
uint16_t industry_cargo_scale
scale cargo production of industries by this percentage.
uint16_t town_cargo_scale
scale cargo production of towns by this percentage.
Data of the economy.
Definition: economy_type.h:43
int16_t fluct
Economy fluctuation status.
Definition: economy_type.h:45
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:108
EconomySettings economy
settings to change the economy
Data structure for storing how the score is computed for a single score id.
Definition: economy_type.h:80
Station data structure.
Definition: station_base.h:439
Vehicle data structure.
Definition: vehicle_base.h:244
Types related to vehicles.