OpenTTD Source 20250312-master-gcdcc6b491d
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 "source_type.h"
20#include "core/random_func.hpp"
21
23void SetPriceBaseMultiplier(Price price, int factor);
24
25extern const ScoreInfo _score_info[];
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
34Money GetTransportedGoodsIncome(uint num_pieces, uint dist, uint16_t transit_periods, CargoType cargo_type);
35uint MoveGoodsToStation(CargoType type, uint amount, Source source, const StationList &all_stations, Owner exclusivity = INVALID_OWNER);
36
37void PrepareUnload(Vehicle *front_v);
39
40Money GetPrice(Price index, uint cost_factor, const struct GRFFile *grf_file, int shift = 0);
41
43void RecomputePrices();
44bool AddInflation(bool check_year = true);
45
51{
52 return _economy.fluct <= 0;
53}
54
61static uint ScaleByInverseCargoScale(uint num, bool town)
62{
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
78inline 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
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 */
Types related to cargoes...
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:23
A sort-of mixin that adds 'at(pos)' and 'operator[](pos)' implementations for 'ConvertibleThroughBase...
Types related to companies.
static constexpr Owner INVALID_OWNER
An invalid owner.
Money GetPrice(Price index, uint cost_factor, const GRFFile *grf_file, int shift)
Determine a certain price.
Definition economy.cpp:952
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
const ScoreInfo _score_info[]
Score info, values used for computing the detailed performance rating.
Definition economy.cpp:91
static uint ScaleByInverseCargoScale(uint num, bool town)
Scale a number by the inverse of the cargo scale setting, e.g.
bool AddInflation(bool check_year=true)
Add monthly inflation.
Definition economy.cpp:707
void LoadUnloadStation(Station *st)
Load/unload the vehicles in this station according to the order they entered.
Definition economy.cpp:1935
void RecomputePrices()
Computes all prices, payments and maximum loan.
Definition economy.cpp:745
void PrepareUnload(Vehicle *front_v)
Prepare the vehicle to be unloaded.
Definition economy.cpp:1263
bool EconomyIsInRecession()
Is the economy in recession?
void SetPriceBaseMultiplier(Price price, int factor)
Change a price base by the given factor.
Definition economy.cpp:885
void InitializeEconomy()
Resets economy to initial values.
Definition economy.cpp:937
void ResetPriceBaseMultipliers()
Reset changes to the price base multipliers.
Definition economy.cpp:873
uint ScaleByCargoScale(uint num, bool town)
Scale a number by the cargo scale setting.
void StartupIndustryDailyChanges(bool init_counter)
Initialize the variables that will maintain the daily industry change system.
Definition economy.cpp:895
Types related to the economy.
Money Prices[PR_END]
Prices of everything.
Price
Enumeration of all base prices for use with Prices.
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:58
Types related to global configuration settings.
Type for the source of cargo.
Types related to stations.
std::set< Station *, StationCompare > StationList
List of stations.
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.
int16_t fluct
Economy fluctuation status.
Dynamic data of a loaded NewGRF.
Definition newgrf.h:111
EconomySettings economy
settings to change the economy
Data structure for storing how the score is computed for a single score id.
A location from where cargo can come from (or go to).
Definition source_type.h:32
Station data structure.
Vehicle data structure.
Types related to vehicles.