OpenTTD Source 20241224-master-gf74b0cf984
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
22void SetPriceBaseMultiplier(Price price, int factor);
23
24extern const ScoreInfo _score_info[];
25extern int64_t _score_part[MAX_COMPANIES][SCORE_END];
26extern Economy _economy;
27/* Prices and also the fractional part. */
28extern Prices _price;
29
30int UpdateCompanyRatingAndValue(Company *c, bool update);
31void StartupIndustryDailyChanges(bool init_counter);
32
33Money GetTransportedGoodsIncome(uint num_pieces, uint dist, uint16_t transit_periods, CargoID cargo_type);
34uint MoveGoodsToStation(CargoID type, uint amount, SourceType source_type, SourceID source_id, const StationList &all_stations, Owner exclusivity = INVALID_OWNER);
35
36void PrepareUnload(Vehicle *front_v);
38
39Money GetPrice(Price index, uint cost_factor, const struct GRFFile *grf_file, int shift = 0);
40
42void RecomputePrices();
43bool AddInflation(bool check_year = true);
44
50{
51 return _economy.fluct <= 0;
52}
53
60static 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
77inline 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.
@ INVALID_OWNER
An invalid owner.
@ MAX_COMPANIES
Maximum number of companies.
Money GetPrice(Price index, uint cost_factor, const GRFFile *grf_file, int shift)
Determine a certain price.
Definition economy.cpp:966
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.
bool AddInflation(bool check_year=true)
Add monthly inflation.
Definition economy.cpp:721
void LoadUnloadStation(Station *st)
Load/unload the vehicles in this station according to the order they entered.
Definition economy.cpp:1950
void RecomputePrices()
Computes all prices, payments and maximum loan.
Definition economy.cpp:759
void PrepareUnload(Vehicle *front_v)
Prepare the vehicle to be unloaded.
Definition economy.cpp:1278
bool EconomyIsInRecession()
Is the economy in recession?
void SetPriceBaseMultiplier(Price price, int factor)
Change a price base by the given factor.
Definition economy.cpp:899
void InitializeEconomy()
Resets economy to initial values.
Definition economy.cpp:951
void ResetPriceBaseMultipliers()
Reset changes to the price base multipliers.
Definition economy.cpp:887
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:909
Types related to the economy.
@ SCORE_END
How many scores are there..
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:57
Types related to global configuration settings.
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:108
EconomySettings economy
settings to change the economy
Data structure for storing how the score is computed for a single score id.
Station data structure.
Vehicle data structure.
Types related to vehicles.