OpenTTD Source: src/economy_func.h Source File

OpenTTD Source 14.0-beta1

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
21 void ResetPriceBaseMultipliers();
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 {
62 uint16_t percentage = (town ? _settings_game.economy.town_cargo_scale : _settings_game.economy.industry_cargo_scale);
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
84 uint16_t percentage = (town ? _settings_game.economy.town_cargo_scale : _settings_game.economy.industry_cargo_scale);
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 */
GetPrice
Money GetPrice(Price index, uint cost_factor, const GRFFile *grf_file, int shift)
Determine a certain price.
Definition: economy.cpp:970
LoadUnloadStation
void LoadUnloadStation(Station *st)
Load/unload the vehicles in this station according to the order they entered.
Definition: economy.cpp:1954
Station
Station data structure.
Definition: station_base.h:442
Price
Price
Enumeration of all base prices for use with Prices.
Definition: economy_type.h:89
EconomySettings::town_cargo_scale
uint16_t town_cargo_scale
scale cargo production of towns by this percentage.
Definition: settings_type.h:563
PrepareUnload
void PrepareUnload(Vehicle *front_v)
Prepare the vehicle to be unloaded.
Definition: economy.cpp:1280
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:240
Chance16
bool Chance16(const uint a, const uint b)
Flips a coin with given probability.
Definition: random_func.hpp:131
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
ResetPriceBaseMultipliers
void ResetPriceBaseMultipliers()
Reset changes to the price base multipliers.
Definition: economy.cpp:891
Economy::fluct
int16_t fluct
Economy fluctuation status.
Definition: economy_type.h:45
ScaleByCargoScale
uint ScaleByCargoScale(uint num, bool town)
Scale a number by the cargo scale setting.
Definition: economy_func.h:77
InitializeEconomy
void InitializeEconomy()
Resets economy to initial values.
Definition: economy.cpp:955
StartupIndustryDailyChanges
void StartupIndustryDailyChanges(bool init_counter)
Initialize the variables that will maintain the daily industry change system.
Definition: economy.cpp:913
SCORE_END
@ SCORE_END
How many scores are there..
Definition: economy_type.h:72
cargo_type.h
INVALID_OWNER
@ INVALID_OWNER
An invalid owner.
Definition: company_type.h:29
SourceID
uint16_t SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
Definition: cargo_type.h:126
SetPriceBaseMultiplier
void SetPriceBaseMultiplier(Price price, int factor)
Change a price base by the given factor.
Definition: economy.cpp:903
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:55
GameSettings::economy
EconomySettings economy
settings to change the economy
Definition: settings_type.h:628
MAX_COMPANIES
@ MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:23
_score_info
const ScoreInfo _score_info[]
Score info, values used for computing the detailed performance rating.
Definition: economy.cpp:90
settings_type.h
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:60
StationList
std::set< Station *, StationCompare > StationList
List of stations.
Definition: station_type.h:94
RecomputePrices
void RecomputePrices()
Computes all prices, payments and maximum loan.
Definition: economy.cpp:763
vehicle_type.h
SourceType
SourceType
Types of cargo source and destination.
Definition: cargo_type.h:120
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:201
EconomySettings::industry_cargo_scale
uint16_t industry_cargo_scale
scale cargo production of industries by this percentage.
Definition: settings_type.h:564
Economy
Data of the economy.
Definition: economy_type.h:43
random_func.hpp
OverflowSafeInt< int64_t >
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
EconomyIsInRecession
bool EconomyIsInRecession()
Is the economy in recession?
Definition: economy_func.h:49
economy_type.h
Company
Definition: company_base.h:116
AddInflation
bool AddInflation(bool check_year=true)
Add monthly inflation.
Definition: economy.cpp:725
company_type.h
GRFFile
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:107
Prices
Money Prices[PR_END]
Prices of everything.
Definition: economy_type.h:168
ScoreInfo
Data structure for storing how the score is computed for a single score id.
Definition: economy_type.h:80
station_type.h