Fix #10490: Allow ships to exit depots if another is not moving at the exit point...
[openttd-github.git] / src / economy_func.h
blobeb9a04f48760b82032902ed00e63ee65232fb3d2
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 */
8 /** @file economy_func.h Functions related to the economy. */
10 #ifndef ECONOMY_FUNC_H
11 #define ECONOMY_FUNC_H
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"
21 void ResetPriceBaseMultipliers();
22 void SetPriceBaseMultiplier(Price price, int factor);
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;
30 int UpdateCompanyRatingAndValue(Company *c, bool update);
31 void StartupIndustryDailyChanges(bool init_counter);
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);
36 void PrepareUnload(Vehicle *front_v);
37 void LoadUnloadStation(Station *st);
39 Money GetPrice(Price index, uint cost_factor, const struct GRFFile *grf_file, int shift = 0);
41 void InitializeEconomy();
42 void RecomputePrices();
43 bool AddInflation(bool check_year = true);
45 /**
46 * Is the economy in recession?
47 * @return \c True if economy is in recession, \c false otherwise.
49 inline bool EconomyIsInRecession()
51 return _economy.fluct <= 0;
54 /**
55 * Scale a number by the inverse of the cargo scale setting, e.g. a scale of 25% multiplies the number by 4.
56 * @param num The number to scale.
57 * @param town Are we scaling town production, or industry production?
58 * @return The number scaled by the inverse of the cargo scale setting, minimum of 1.
60 static uint ScaleByInverseCargoScale(uint num, bool town)
62 uint16_t percentage = (town ? _settings_game.economy.town_cargo_scale : _settings_game.economy.industry_cargo_scale);
64 /* We might not need to do anything. */
65 if (percentage == 100) return num;
67 /* Never return 0, since we often divide by this number. */
68 return std::max((num * 100) / percentage, 1u);
71 /**
72 * Scale a number by the cargo scale setting.
73 * @param num The number to scale.
74 * @param town Are we scaling town production, or industry production?
75 * @return The number scaled by the current cargo scale setting. May be 0.
77 inline uint ScaleByCargoScale(uint num, bool town)
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;
82 if (num == 0) return num;
84 uint16_t percentage = (town ? _settings_game.economy.town_cargo_scale : _settings_game.economy.industry_cargo_scale);
86 /* We might not need to do anything. */
87 if (percentage == 100) return num;
89 uint scaled = (num * percentage) / 100;
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;
95 return scaled;
98 #endif /* ECONOMY_FUNC_H */