Codechange: Optimize FlowsDown (#13262)
[openttd-github.git] / src / script / api / script_town.hpp
blobab2ce1f807eaa66993d0a3b70bbc82fa0ce4be60
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 script_town.hpp Everything to query towns. */
10 #ifndef SCRIPT_TOWN_HPP
11 #define SCRIPT_TOWN_HPP
13 #include "script_cargo.hpp"
14 #include "script_company.hpp"
15 #include "../../town_type.h"
17 /**
18 * Class that handles all town related functions.
19 * @api ai game
21 class ScriptTown : public ScriptObject {
22 public:
23 /**
24 * Actions that one can perform on a town.
26 enum TownAction {
27 /* Note: these values represent part of the in-game order of the _town_action_proc array */
29 /**
30 * The cargo ratings temporary gains 25% of rating (in
31 * absolute percentage, so 10% becomes 35%, with a max of 99%)
32 * for all stations within 10 tiles.
34 TOWN_ACTION_ADVERTISE_SMALL = 0,
36 /**
37 * The cargo ratings temporary gains 44% of rating (in
38 * absolute percentage, so 10% becomes 54%, with a max of 99%)
39 * for all stations within 15 tiles.
41 TOWN_ACTION_ADVERTISE_MEDIUM = 1,
43 /**
44 * The cargo ratings temporary gains 63% of rating (in
45 * absolute percentage, so 10% becomes 73%, with a max of 99%)
46 * for all stations within 20 tiles.
48 TOWN_ACTION_ADVERTISE_LARGE = 2,
50 /**
51 * Rebuild the roads of this town for 6 economy-months.
52 * @see \ref ScriptEconomyTime
54 TOWN_ACTION_ROAD_REBUILD = 3,
56 /**
57 * Build a statue in this town.
59 TOWN_ACTION_BUILD_STATUE = 4,
61 /**
62 * Fund the creation of extra buildings for 3 economy-months.
63 * @see \ref ScriptEconomyTime
65 TOWN_ACTION_FUND_BUILDINGS = 5,
67 /**
68 * Buy exclusive rights for this town for 12 economy-months.
69 * @see \ref ScriptEconomyTime
71 TOWN_ACTION_BUY_RIGHTS = 6,
73 /**
74 * Bribe the town in order to get a higher rating.
76 TOWN_ACTION_BRIBE = 7,
79 /**
80 * Different ratings one could have in a town.
82 enum TownRating {
83 TOWN_RATING_NONE, ///< The company got no rating in the town.
84 TOWN_RATING_APPALLING, ///< The company got an appalling rating in the town .
85 TOWN_RATING_VERY_POOR, ///< The company got an very poor rating in the town.
86 TOWN_RATING_POOR, ///< The company got an poor rating in the town.
87 TOWN_RATING_MEDIOCRE, ///< The company got an mediocre rating in the town.
88 TOWN_RATING_GOOD, ///< The company got an good rating in the town.
89 TOWN_RATING_VERY_GOOD, ///< The company got an very good rating in the town.
90 TOWN_RATING_EXCELLENT, ///< The company got an excellent rating in the town.
91 TOWN_RATING_OUTSTANDING, ///< The company got an outstanding rating in the town.
92 TOWN_RATING_INVALID = -1, ///< The town rating for invalid towns/companies.
95 /**
96 * Possible layouts for the roads in a town.
98 enum RoadLayout {
99 /* Note: these values represent part of the in-game TownLayout enum */
100 ROAD_LAYOUT_ORIGINAL = ::TL_ORIGINAL, ///< Original algorithm (min. 1 distance between roads).
101 ROAD_LAYOUT_BETTER_ROADS = ::TL_BETTER_ROADS, ///< Extended original algorithm (min. 2 distance between roads).
102 ROAD_LAYOUT_2x2 = ::TL_2X2_GRID, ///< Geometric 2x2 grid algorithm
103 ROAD_LAYOUT_3x3 = ::TL_3X3_GRID, ///< Geometric 3x3 grid algorithm
104 ROAD_LAYOUT_RANDOM = ::TL_RANDOM, ///< Random road layout
106 /* Custom added value, only valid for this API */
107 ROAD_LAYOUT_INVALID = -1, ///< The layout for invalid towns.
111 * Possible town construction sizes.
113 enum TownSize {
114 TOWN_SIZE_SMALL = ::TSZ_SMALL, ///< Small town.
115 TOWN_SIZE_MEDIUM = ::TSZ_MEDIUM, ///< Medium town.
116 TOWN_SIZE_LARGE = ::TSZ_LARGE, ///< Large town.
118 TOWN_SIZE_INVALID = -1, ///< Invalid town size.
122 * Special values for SetGrowthRate.
124 enum TownGrowth {
125 TOWN_GROWTH_NONE = 0xFFFF, ///< Town does not grow at all.
126 TOWN_GROWTH_NORMAL = 0x10000, ///< Use default town growth algorithm instead of custom growth rate.
130 * Gets the number of towns.
131 * @return The number of towns.
133 static SQInteger GetTownCount();
136 * Checks whether the given town index is valid.
137 * @param town_id The index to check.
138 * @return True if and only if the town is valid.
140 static bool IsValidTown(TownID town_id);
143 * Get the name of the town.
144 * @param town_id The town to get the name of.
145 * @pre IsValidTown(town_id).
146 * @return The name of the town.
148 static std::optional<std::string> GetName(TownID town_id);
151 * Rename a town.
152 * @param town_id The town to rename
153 * @param name The new name of the town. If null, or an empty string, is passed, the town name will be reset to the default name.
154 * @pre IsValidTown(town_id).
155 * @pre ScriptCompanyMode::IsDeity().
156 * @return True if the action succeeded.
157 * @api -ai
159 static bool SetName(TownID town_id, Text *name);
162 * Set the custom text of a town, shown in the GUI.
163 * @param town_id The town to set the custom text of.
164 * @param text The text to set it to (can be either a raw string, or a ScriptText object). If null, or an empty string, is passed, the text will be removed.
165 * @pre IsValidTown(town_id).
166 * @pre ScriptCompanyMode::IsDeity().
167 * @return True if the action succeeded.
168 * @api -ai
170 static bool SetText(TownID town_id, Text *text);
173 * Gets the number of inhabitants in the town.
174 * @param town_id The town to get the population of.
175 * @pre IsValidTown(town_id).
176 * @return The number of inhabitants.
178 static SQInteger GetPopulation(TownID town_id);
181 * Gets the number of houses in the town.
182 * @param town_id The town to get the number of houses of.
183 * @pre IsValidTown(town_id).
184 * @return The number of houses.
186 static SQInteger GetHouseCount(TownID town_id);
189 * Gets the location of the town.
190 * @param town_id The town to get the location of.
191 * @pre IsValidTown(town_id).
192 * @return The location of the town.
194 static TileIndex GetLocation(TownID town_id);
197 * Get the total last economy-month's production of the given cargo at a town.
198 * @param town_id The index of the town.
199 * @param cargo_id The index of the cargo.
200 * @pre IsValidTown(town_id).
201 * @pre ScriptCargo::IsValidCargo(cargo_id).
202 * @return The last economy-month's production of the given cargo for this town.
203 * @see \ref ScriptEconomyTime
205 static SQInteger GetLastMonthProduction(TownID town_id, CargoID cargo_id);
208 * Get the total amount of cargo supplied from a town last economy-month.
209 * @param town_id The index of the town.
210 * @param cargo_id The index of the cargo.
211 * @pre IsValidTown(town_id).
212 * @pre ScriptCargo::IsValidCargo(cargo_id).
213 * @return The amount of cargo supplied for transport from this town last economy-month.
214 * @see \ref ScriptEconomyTime
216 static SQInteger GetLastMonthSupplied(TownID town_id, CargoID cargo_id);
219 * Get the percentage of transported production of the given cargo at a town last economy-month.
220 * @param town_id The index of the town.
221 * @param cargo_id The index of the cargo.
222 * @pre IsValidTown(town_id).
223 * @pre ScriptCargo::IsValidCargo(cargo_id).
224 * @return The percentage of given cargo transported from this town last economy-month.
225 * @see \ref ScriptEconomyTime
227 static SQInteger GetLastMonthTransportedPercentage(TownID town_id, CargoID cargo_id);
230 * Get the total amount of cargo effects received by a town last economy-month.
231 * @param town_id The index of the town.
232 * @param towneffect_id The index of the cargo.
233 * @pre IsValidTown(town_id).
234 * @pre ScriptCargo::IsValidTownEffect(cargo_id).
235 * @return The amount of cargo received by this town last economy-month for this cargo effect.
236 * @see \ref ScriptEconomyTime
238 static SQInteger GetLastMonthReceived(TownID town_id, ScriptCargo::TownEffect towneffect_id);
241 * Set the goal of a cargo per economy-month for this town.
242 * @param town_id The index of the town.
243 * @param towneffect_id The index of the towneffect.
244 * @param goal The new goal amount for cargo delivered per economy-month.
245 * The value will be clamped to 0 .. MAX(uint32_t).
246 * @pre IsValidTown(town_id).
247 * @pre ScriptCargo::IsValidTownEffect(towneffect_id).
248 * @pre ScriptCompanyMode::IsDeity().
249 * @return True if the action succeeded.
250 * @see \ref ScriptEconomyTime
251 * @api -ai
253 static bool SetCargoGoal(TownID town_id, ScriptCargo::TownEffect towneffect_id, SQInteger goal);
256 * Get the amount of cargo per economy-month that needs to be delivered (per TownEffect) for a
257 * town to grow. All goals need to be reached before a town will grow.
258 * @param town_id The index of the town.
259 * @param towneffect_id The index of the towneffect.
260 * @pre IsValidTown(town_id).
261 * @pre ScriptCargo::IsValidTownEffect(towneffect_id).
262 * @return The goal of the cargo (amount per economy-month).
263 * @note Goals can change over time. For example with a changing snowline, or
264 * with a growing town.
265 * @see \ref ScriptEconomyTime
267 static SQInteger GetCargoGoal(TownID town_id, ScriptCargo::TownEffect towneffect_id);
270 * Set the amount of economy-days between town growth.
271 * @param town_id The index of the town.
272 * @param days_between_town_growth The amount of economy-days between town growth, TOWN_GROWTH_NONE or TOWN_GROWTH_NORMAL.
273 * @pre IsValidTown(town_id).
274 * @pre days_between_town_growth <= 880 || days_between_town_growth == TOWN_GROWTH_NONE || days_between_town_growth == TOWN_GROWTH_NORMAL.
275 * @return True if the action succeeded.
276 * @note Even when setting a growth rate, towns only grow when the conditions for growth (SetCargoCoal) are met,
277 * and the game settings (economy.town_growth_rate) allow town growth at all.
278 * @note When changing the growth rate, the relative progress is preserved and scaled to the new rate.
279 * @see \ref ScriptEconomyTime
280 * @api -ai
282 static bool SetGrowthRate(TownID town_id, SQInteger days_between_town_growth);
285 * Get the amount of economy-days between town growth.
286 * @param town_id The index of the town.
287 * @pre IsValidTown(town_id).
288 * @return Amount of economy-days between town growth, or TOWN_GROWTH_NONE.
289 * @note This function does not indicate when it will grow next. It only tells you the time between growths.
290 * @see \ref ScriptEconomyTime
292 static SQInteger GetGrowthRate(TownID town_id);
295 * Get the manhattan distance from the tile to the ScriptTown::GetLocation()
296 * of the town.
297 * @param town_id The town to get the distance to.
298 * @param tile The tile to get the distance to.
299 * @pre IsValidTown(town_id).
300 * @return The distance between town and tile.
302 static SQInteger GetDistanceManhattanToTile(TownID town_id, TileIndex tile);
305 * Get the square distance from the tile to the ScriptTown::GetLocation()
306 * of the town.
307 * @param town_id The town to get the distance to.
308 * @param tile The tile to get the distance to.
309 * @pre IsValidTown(town_id).
310 * @return The distance between town and tile.
312 static SQInteger GetDistanceSquareToTile(TownID town_id, TileIndex tile);
315 * Find out if this tile is within the rating influence of a town.
316 * If a station sign would be on this tile, the servicing quality of the station would
317 * influence the rating of the town.
318 * @param town_id The town to check.
319 * @param tile The tile to check.
320 * @pre IsValidTown(town_id).
321 * @return True if the tile is within the rating influence of the town.
323 static bool IsWithinTownInfluence(TownID town_id, TileIndex tile);
326 * Find out if this town has a statue for the current company.
327 * @param town_id The town to check.
328 * @pre IsValidTown(town_id).
329 * @game @pre ScriptCompanyMode::IsValid().
330 * @return True if the town has a statue.
332 static bool HasStatue(TownID town_id);
335 * Find out if the town is a city.
336 * @param town_id The town to check.
337 * @pre IsValidTown(town_id).
338 * @return True if the town is a city.
340 static bool IsCity(TownID town_id);
343 * Find out how long the town is undergoing road reconstructions.
344 * @param town_id The town to check.
345 * @pre IsValidTown(town_id).
346 * @return The number of economy-months the road reworks are still going to take.
347 * The value 0 means that there are currently no road reworks.
348 * @see \ref ScriptEconomyTime
350 static SQInteger GetRoadReworkDuration(TownID town_id);
353 * Find out how long new buildings are still being funded in a town.
354 * @param town_id The town to check.
355 * @pre IsValidTown(town_id).
356 * @return The number of economy-months building construction is still funded.
357 * The value 0 means that there is currently no funding.
358 * @see \ref ScriptEconomyTime
360 static SQInteger GetFundBuildingsDuration(TownID town_id);
363 * Find out which company currently has the exclusive rights of this town.
364 * @param town_id The town to check.
365 * @pre IsValidTown(town_id).
366 * @game @pre ScriptCompanyMode::IsValid().
367 * @return The company that has the exclusive rights. The value
368 * ScriptCompany::COMPANY_INVALID means that there are currently no
369 * exclusive rights given out to anyone.
371 static ScriptCompany::CompanyID GetExclusiveRightsCompany(TownID town_id);
374 * Find out how long the town is under influence of the exclusive rights.
375 * @param town_id The town to check.
376 * @pre IsValidTown(town_id).
377 * @return The number of economy-months the exclusive rights hold.
378 * The value 0 means that there are currently no exclusive rights
379 * given out to anyone.
380 * @see \ref ScriptEconomyTime
382 static SQInteger GetExclusiveRightsDuration(TownID town_id);
385 * Find out if an action can currently be performed on the town.
386 * @param town_id The town to perform the action on.
387 * @param town_action The action to perform on the town.
388 * @pre IsValidTown(town_id).
389 * @game @pre ScriptCompanyMode::IsValid().
390 * @return True if and only if the action can performed.
392 static bool IsActionAvailable(TownID town_id, TownAction town_action);
395 * Perform a town action on this town.
396 * @param town_id The town to perform the action on.
397 * @param town_action The action to perform on the town.
398 * @pre IsValidTown(town_id).
399 * @pre IsActionAvailable(town_id, town_action).
400 * @game @pre ScriptCompanyMode::IsValid().
401 * @return True if the action succeeded.
403 static bool PerformTownAction(TownID town_id, TownAction town_action);
406 * Expand the town.
407 * @param town_id The town to expand.
408 * @param houses The amount of houses to grow the town with.
409 * The value will be clamped to 0 .. MAX(uint32_t).
410 * @pre IsValidTown(town_id).
411 * @pre houses > 0.
412 * @pre ScriptCompanyMode::IsDeity().
413 * @return True if the action succeeded.
414 * @api -ai
416 static bool ExpandTown(TownID town_id, SQInteger houses);
419 * Found a new town.
420 * @param tile The location of the new town.
421 * @param size The town size of the new town.
422 * @param city True if the new town should be a city.
423 * @param layout The town layout of the new town.
424 * @param name The name of the new town. Pass null, or an empty string, to use a random town name.
425 * @game @pre ScriptCompanyMode::IsDeity() || ScriptSettings.GetValue("economy.found_town") != 0.
426 * @ai @pre ScriptSettings.GetValue("economy.found_town") != 0.
427 * @game @pre ScriptCompanyMode::IsDeity() || size != TOWN_SIZE_LARGE.
428 * @ai @pre size != TOWN_SIZE_LARGE.
429 * @pre size != TOWN_SIZE_INVALID.
430 * @pre layout != ROAD_LAYOUT_INVALID.
431 * @return True if the action succeeded.
432 * @game @note Companies are restricted by the advanced setting that controls if funding towns is allowed or not. If custom road layout is forbidden and there is a company mode in scope (ScriptCompanyMode::IsValid()), the layout parameter will be ignored.
433 * @ai @note AIs are restricted by the advanced setting that controls if funding towns is allowed or not. If custom road layout is forbidden, the layout parameter will be ignored.
435 static bool FoundTown(TileIndex tile, TownSize size, bool city, RoadLayout layout, Text *name);
438 * Get the rating of a company within a town.
439 * @param town_id The town to get the rating for.
440 * @param company_id The company to get the rating for.
441 * @pre IsValidTown(town_id).
442 * @pre ScriptCompany.ResolveCompanyID(company) != ScriptCompany::COMPANY_INVALID.
443 * @return The rating as shown to humans.
445 static TownRating GetRating(TownID town_id, ScriptCompany::CompanyID company_id);
448 * Get the accurate rating of a company within a town.
449 * @param town_id The town to get the rating for.
450 * @param company_id The company to get the rating for.
451 * @pre IsValidTown(town_id).
452 * @pre ScriptCompany.ResolveCompanyID(company) != ScriptCompany::COMPANY_INVALID.
453 * @return The rating as a number between -1000 (worst) and 1000 (best).
454 * @api -ai
456 static SQInteger GetDetailedRating(TownID town_id, ScriptCompany::CompanyID company_id);
459 * Change the rating of a company within a town.
460 * @param town_id The town to change the rating in.
461 * @param company_id The company to change the rating for.
462 * @param delta How much to change rating by (range -1000 to +1000).
463 * @return True if the rating was changed.
464 * @pre IsValidTown(town_id).
465 * @pre ScriptCompany.ResolveCompanyID(company) != ScriptCompany::COMPANY_INVALID.
466 * @pre ScriptCompanyMode::IsDeity().
467 * @api -ai
469 static bool ChangeRating(TownID town_id, ScriptCompany::CompanyID company_id, SQInteger delta);
472 * Get the maximum level of noise that still can be added by airports
473 * before the town start to refuse building a new airport.
474 * @param town_id The town to get the allowed noise from.
475 * @return The noise that still can be added.
477 static SQInteger GetAllowedNoise(TownID town_id);
480 * Get the road layout for a town.
481 * @param town_id The town to get the road layout from.
482 * @return The RoadLayout for the town.
484 static RoadLayout GetRoadLayout(TownID town_id);
487 #endif /* SCRIPT_TOWN_HPP */