Codechange: Optimize FlowsDown (#13262)
[openttd-github.git] / src / script / api / script_tile.hpp
blob8abf47a793a7945e73fb531b7ff60ad87c9bdcc1
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_tile.hpp Everything to query and manipulate tiles. */
10 #ifndef SCRIPT_TILE_HPP
11 #define SCRIPT_TILE_HPP
13 #include "script_error.hpp"
14 #include "script_company.hpp"
15 #include "../../slope_type.h"
16 #include "../../transport_type.h"
18 /**
19 * Class that handles all tile related functions.
20 * @api ai game
22 class ScriptTile : public ScriptObject {
23 public:
24 /**
25 * Error messages related to modifying tiles.
27 * @see ScriptErrorType
29 enum ErrorMessages {
31 /** Base for tile related errors */
32 ERR_TILE_BASE = ScriptError::ERR_CAT_TILE << ScriptError::ERR_CAT_BIT_SIZE,
34 /** Tile can't be raised any higher */
35 ERR_TILE_TOO_HIGH, // [STR_ERROR_ALREADY_AT_SEA_LEVEL]
37 /** Tile can't be lowered any lower */
38 ERR_TILE_TOO_LOW, // [STR_ERROR_ALREADY_AT_SEA_LEVEL]
40 /** The area was already flat */
41 ERR_AREA_ALREADY_FLAT, // [STR_ERROR_ALREADY_LEVELLED]
43 /** There is a tunnel underneath */
44 ERR_EXCAVATION_WOULD_DAMAGE, // [STR_ERROR_EXCAVATION_WOULD_DAMAGE]
46 /** Reached the limit for terraforming/clearing/planting */
47 ERR_LIMIT_REACHED, // [STR_ERROR_TERRAFORM_LIMIT_REACHED, STR_ERROR_CLEARING_LIMIT_REACHED, STR_ERROR_TREE_PLANT_LIMIT_REACHED]
50 /**
51 * Enumeration for corners of tiles.
53 enum Corner {
54 /* Note: these values represent part of the in-game Corner enum */
55 CORNER_W = ::CORNER_W, ///< West corner
56 CORNER_S = ::CORNER_S, ///< South corner
57 CORNER_E = ::CORNER_E, ///< East corner
58 CORNER_N = ::CORNER_N, ///< North corner
60 CORNER_INVALID = ::CORNER_INVALID, ///< An invalid corner
63 /**
64 * Enumeration for the slope-type.
66 * This enumeration use the chars N, E, S, W corresponding the
67 * direction North, East, South and West. The top corner of a tile
68 * is the north-part of the tile.
70 enum Slope {
71 /* Note: these values represent part of the in-game Slope enum */
72 SLOPE_FLAT = ::SLOPE_FLAT, ///< A flat tile
73 SLOPE_W = ::SLOPE_W, ///< The west corner of the tile is raised
74 SLOPE_S = ::SLOPE_S, ///< The south corner of the tile is raised
75 SLOPE_E = ::SLOPE_E, ///< The east corner of the tile is raised
76 SLOPE_N = ::SLOPE_N, ///< The north corner of the tile is raised
77 SLOPE_STEEP = ::SLOPE_STEEP, ///< Indicates the slope is steep (The corner opposite of the not-raised corner is raised two times)
78 SLOPE_NW = ::SLOPE_NW, ///< North and west corner are raised
79 SLOPE_SW = ::SLOPE_SW, ///< South and west corner are raised
80 SLOPE_SE = ::SLOPE_SE, ///< South and east corner are raised
81 SLOPE_NE = ::SLOPE_NE, ///< North and east corner are raised
82 SLOPE_EW = ::SLOPE_EW, ///< East and west corner are raised
83 SLOPE_NS = ::SLOPE_NS, ///< North and south corner are raised
84 SLOPE_ELEVATED = ::SLOPE_ELEVATED, ///< Bit mask containing all 'simple' slopes. Does not appear as a slope.
85 SLOPE_NWS = ::SLOPE_NWS, ///< North, west and south corner are raised
86 SLOPE_WSE = ::SLOPE_WSE, ///< West, south and east corner are raised
87 SLOPE_SEN = ::SLOPE_SEN, ///< South, east and north corner are raised
88 SLOPE_ENW = ::SLOPE_ENW, ///< East, north and west corner are raised
89 SLOPE_STEEP_W = ::SLOPE_STEEP_W, ///< A steep slope falling to east (from west)
90 SLOPE_STEEP_S = ::SLOPE_STEEP_S, ///< A steep slope falling to north (from south)
91 SLOPE_STEEP_E = ::SLOPE_STEEP_E, ///< A steep slope falling to west (from east)
92 SLOPE_STEEP_N = ::SLOPE_STEEP_N, ///< A steep slope falling to south (from north)
94 /* Custom added value, only valid for this API */
95 SLOPE_INVALID = 0xFFFF, ///< An invalid slope
98 /**
99 * The different transport types a tile can have.
101 enum TransportType {
102 /* Note: these values represent part of the in-game TransportType enum */
103 TRANSPORT_RAIL = ::TRANSPORT_RAIL, ///< Tile with rail.
104 TRANSPORT_ROAD = ::TRANSPORT_ROAD, ///< Tile with road.
105 TRANSPORT_WATER = ::TRANSPORT_WATER, ///< Tile with navigable waterways.
106 TRANSPORT_AIR = ::TRANSPORT_AIR, ///< Tile with airport.
108 /* Custom added value, only valid for this API */
109 TRANSPORT_INVALID = -1, ///< Tile without any transport type.
113 * Get the base cost for building/clearing several things.
115 enum BuildType {
116 BT_FOUNDATION, ///< Build a foundation under something
117 BT_TERRAFORM, ///< Terraform
118 BT_BUILD_TREES, ///< Build trees
119 BT_CLEAR_GRASS, ///< Clear a tile with just grass
120 BT_CLEAR_ROUGH, ///< Clear a rough tile
121 BT_CLEAR_ROCKY, ///< Clear a tile with rocks
122 BT_CLEAR_FIELDS, ///< Clear a tile with farm fields
123 BT_CLEAR_HOUSE, ///< Clear a tile with a house
124 BT_CLEAR_WATER, ///< Clear a tile with either river or sea
128 * The types of terrain a tile can have.
130 * @note When a desert or rainforest tile are changed, their terrain type will remain the same. In other words, a sea tile can be of the desert terrain type.
131 * @note The snow terrain type can change to the normal terrain type and vice versa based on landscaping or variable snow lines from NewGRFs.
133 enum TerrainType {
134 TERRAIN_NORMAL, ///< A normal tile (default); not desert, rainforest or snow.
135 TERRAIN_DESERT, ///< A tile in the desert (manually set in in scenarios, below certain height and certain distance from water in random games).
136 TERRAIN_RAINFOREST, ///< A tile in the rainforest (manually set in scenarios, certain distance away from deserts in random games),
137 TERRAIN_SNOW ///< A tile on or above the snowline level.
141 * Check if this tile is buildable, i.e. no things on it that needs
142 * demolishing.
143 * @param tile The tile to check on.
144 * @pre ScriptMap::IsValidTile(tile).
145 * @return True if it is buildable, false if not.
146 * @note For trams you also might want to check for ScriptRoad::IsRoad(),
147 * as you can build tram-rails on road-tiles.
148 * @note For rail you also might want to check for ScriptRoad::IsRoad(),
149 * as in some cases you can build rails on road-tiles.
151 static bool IsBuildable(TileIndex tile);
154 * Check if this tile is buildable in a rectangle around a tile, with the
155 * entry in the list as top-left.
156 * @param tile The tile to check on.
157 * @param width The width of the rectangle.
158 * @param height The height of the rectangle.
159 * @pre ScriptMap::IsValidTile(tile).
160 * @pre width >= 0.
161 * @pre height >= 0.
162 * @return True if it is buildable, false if not.
164 static bool IsBuildableRectangle(TileIndex tile, SQInteger width, SQInteger height);
167 * Checks whether the given tile is actually a sea tile.
168 * @param tile The tile to check on.
169 * @pre ScriptMap::IsValidTile(tile).
170 * @return True if and only if the tile is a sea tile.
172 static bool IsSeaTile(TileIndex tile);
175 * Checks whether the given tile is actually a river tile.
176 * @param tile The tile to check on.
177 * @pre ScriptMap::IsValidTile(tile).
178 * @return True if and only if the tile is a river tile.
180 static bool IsRiverTile(TileIndex tile);
183 * Checks whether the given tile is actually a water tile.
184 * @param tile The tile to check on.
185 * @pre ScriptMap::IsValidTile(tile).
186 * @return True if and only if the tile is a water tile.
187 * @note Returns false when a buoy is on the tile.
189 static bool IsWaterTile(TileIndex tile);
192 * Checks whether the given tile is actually a coast tile.
193 * @param tile The tile to check.
194 * @pre ScriptMap::IsValidTile(tile).
195 * @return True if and only if the tile is a coast tile.
196 * @note Building on coast tiles in general is more expensive. This is not
197 * true if there are also trees on the tile, see #HasTreeOnTile.
199 static bool IsCoastTile(TileIndex tile);
202 * Checks whether the given tile is a station tile of any station.
203 * @param tile The tile to check.
204 * @pre ScriptMap::IsValidTile(tile).
205 * @return True if and only if the tile is a station tile.
207 static bool IsStationTile(TileIndex tile);
210 * Check if a tile has a steep slope.
211 * Steep slopes are slopes with a height difference of 2 across one diagonal of the tile.
212 * @param slope The slope to check on.
213 * @pre slope != SLOPE_INVALID.
214 * @return True if the slope is a steep slope.
216 static bool IsSteepSlope(Slope slope);
219 * Check if a tile has a halftile slope.
220 * Halftile slopes appear on top of halftile foundations. E.g. the slope you get when building a horizontal railtrack on the top of a SLOPE_N or SLOPE_STEEP_N.
221 * @param slope The slope to check on.
222 * @pre slope != SLOPE_INVALID.
223 * @return True if the slope is a halftile slope.
224 * @note Currently there is no API function that would return or accept a halftile slope.
226 static bool IsHalftileSlope(Slope slope);
229 * Check if the tile has any tree on it.
230 * @param tile The tile to check on.
231 * @pre ScriptMap::IsValidTile(tile).
232 * @return True if and only if there is a tree on the tile.
234 static bool HasTreeOnTile(TileIndex tile);
237 * Check if the tile is a farmland tile.
238 * @param tile The tile to check on.
239 * @pre ScriptMap::IsValidTile(tile).
240 * @return True if and only if the tile is farmland.
242 static bool IsFarmTile(TileIndex tile);
245 * Check if the tile is a rock tile.
246 * @param tile The tile to check on.
247 * @pre ScriptMap::IsValidTile(tile).
248 * @return True if and only if the tile is rock tile.
250 static bool IsRockTile(TileIndex tile);
253 * Check if the tile is a rough tile.
254 * @param tile The tile to check on.
255 * @pre ScriptMap::IsValidTile(tile).
256 * @return True if and only if the tile is rough tile.
258 static bool IsRoughTile(TileIndex tile);
261 * Check if the tile without buildings or infrastructure is a snow tile.
262 * @note If you want to know if a tile (with or without buildings and infrastructure) is on or above the snowline, use ScriptTile::GetTerrainType(tile).
263 * @param tile The tile to check on.
264 * @pre ScriptMap::IsValidTile(tile).
265 * @return True if and only if the tile is snow tile.
267 static bool IsSnowTile(TileIndex tile);
270 * Check if the tile without buildings or infrastructure is a desert tile.
271 * @note If you want to know if a tile (with or without buildings and infrastructure) is in a desert, use ScriptTile::GetTerrainType(tile).
272 * @param tile The tile to check on.
273 * @pre ScriptMap::IsValidTile(tile).
274 * @return True if and only if the tile is desert tile.
276 static bool IsDesertTile(TileIndex tile);
279 * Get the type of terrain regardless of buildings or infrastructure.
280 * @note When a desert or rainforest tile are changed, their terrain type will remain the same. In other words, a sea tile can be of the desert terrain type.
281 * @note The snow terrain type can change to the normal terrain type and vice versa based on landscaping or variable snow lines from NewGRFs.
282 * @param tile The tile to check on.
283 * @pre ScriptMap::IsValidTile(tile).
284 * @return The #TerrainType.
286 static TerrainType GetTerrainType(TileIndex tile);
289 * Get the slope of a tile.
290 * This is the slope of the bare tile. A possible foundation on the tile does not influence this slope.
291 * @param tile The tile to check on.
292 * @pre ScriptMap::IsValidTile(tile).
293 * @return Bit mask encoding the slope. See #Slope for a description of the returned values.
295 static Slope GetSlope(TileIndex tile);
298 * Get the complement of the slope.
299 * @param slope The slope to get the complement of.
300 * @pre slope != SLOPE_INVALID.
301 * @pre !IsSteepSlope(slope).
302 * @pre !IsHalftileSlope(slope).
303 * @return The complement of a slope. This means that all corners that
304 * weren't raised, are raised, and visa versa.
306 static Slope GetComplementSlope(Slope slope);
309 * Get the minimal height on a tile.
310 * The returned height is the height of the bare tile. A possible foundation on the tile does not influence this height.
311 * @param tile The tile to check on.
312 * @pre ScriptMap::IsValidTile(tile).
313 * @return The height of the lowest corner of the tile, ranging from 0 to 15.
315 static SQInteger GetMinHeight(TileIndex tile);
318 * Get the maximal height on a tile.
319 * The returned height is the height of the bare tile. A possible foundation on the tile does not influence this height.
320 * @param tile The tile to check on.
321 * @pre ScriptMap::IsValidTile(tile).
322 * @return The height of the highest corner of the tile, ranging from 0 to 15.
324 static SQInteger GetMaxHeight(TileIndex tile);
327 * Get the height of a certain corner of a tile.
328 * The returned height is the height of the bare tile. A possible foundation on the tile does not influence this height.
329 * @param tile The tile to check on.
330 * @param corner The corner to query.
331 * @pre ScriptMap::IsValidTile(tile).
332 * @return The height of the lowest corner of the tile, ranging from 0 to 15.
334 static SQInteger GetCornerHeight(TileIndex tile, Corner corner);
337 * Get the owner of the tile.
338 * @param tile The tile to get the owner from.
339 * @pre ScriptMap::IsValidTile(tile).
340 * @return The CompanyID of the owner of the tile, or COMPANY_INVALID if
341 * there is no owner (grass/industry/water tiles, etc.).
343 static ScriptCompany::CompanyID GetOwner(TileIndex tile);
346 * Checks whether the given tile contains parts suitable for the given
347 * TransportType.
348 * @param tile The tile to check.
349 * @param transport_type The TransportType to check against.
350 * @pre ScriptMap::IsValidTile(tile).
351 * @pre transport_type != TRANSPORT_AIR.
352 * @note Returns false on tiles with roadworks and on road tiles with only
353 * a single piece of road as these tiles cannot be used to transport
354 * anything on. It furthermore returns true on some coast tile for
355 * TRANSPORT_WATER because ships can navigate over them.
356 * @note Use ScriptAirport.IsAirportTile to check for airport tiles. Aircraft
357 * can fly over every tile on the map so using HasTransportType
358 * doesn't make sense for TRANSPORT_AIR.
359 * @return True if and only if the tile has the given TransportType.
361 static bool HasTransportType(TileIndex tile, TransportType transport_type);
364 * Check how much cargo this tile accepts.
365 * It creates a radius around the tile, and adds up all acceptance of this
366 * cargo.
367 * @param tile The tile to check on.
368 * @param cargo_type The cargo to check the acceptance of.
369 * @param width The width of the station.
370 * @param height The height of the station.
371 * @param radius The radius of the station.
372 * @pre ScriptMap::IsValidTile(tile).
373 * @pre ScriptCargo::IsValidCargo(cargo_type)
374 * @pre width > 0.
375 * @pre height > 0.
376 * @pre radius >= 0.
377 * @return Values below 8 mean no acceptance; the more the better.
379 static SQInteger GetCargoAcceptance(TileIndex tile, CargoID cargo_type, SQInteger width, SQInteger height, SQInteger radius);
382 * Checks how many producers in the radius produces this cargo.
383 * It creates a radius around the tile, and counts all producer of this cargo.
384 * @param tile The tile to check on.
385 * @param cargo_type The cargo to check the production of.
386 * @param width The width of the station.
387 * @param height The height of the station.
388 * @param radius The radius of the station.
389 * @pre ScriptMap::IsValidTile(tile).
390 * @pre ScriptCargo::IsValidCargo(cargo_type)
391 * @pre width > 0.
392 * @pre height > 0.
393 * @pre radius >= 0.
394 * @return The number of producers that produce this cargo within radius of the tile.
396 static SQInteger GetCargoProduction(TileIndex tile, CargoID cargo_type, SQInteger width, SQInteger height, SQInteger radius);
399 * Get the manhattan distance from the tile to the tile.
400 * @param tile_from The tile to get the distance to.
401 * @param tile_to The tile to get the distance to.
402 * @return The distance between the two tiles.
404 static SQInteger GetDistanceManhattanToTile(TileIndex tile_from, TileIndex tile_to);
407 * Get the square distance from the tile to the tile.
408 * @param tile_from The tile to get the distance to.
409 * @param tile_to The tile to get the distance to.
410 * @return The distance between the two tiles.
412 static SQInteger GetDistanceSquareToTile(TileIndex tile_from, TileIndex tile_to);
415 * Raise the given corners of the tile. The corners can be combined,
416 * for example: SLOPE_N | SLOPE_W (= SLOPE_NW) will raise the west and the north corner.
417 * @note The corners will be modified in the order west (first), south, east, north (last).
418 * Changing one corner might cause another corner to be changed too. So modifiing
419 * multiple corners may result in changing some corners by multiple steps.
420 * @param tile The tile to raise.
421 * @param slope Corners to raise (SLOPE_xxx).
422 * @pre tile < ScriptMap::GetMapSize().
423 * @game @pre ScriptCompanyMode::IsValid().
424 * @exception ScriptError::ERR_AREA_NOT_CLEAR
425 * @exception ScriptError::ERR_TOO_CLOSE_TO_EDGE
426 * @exception ScriptTile::ERR_TILE_TOO_HIGH
427 * @return 0 means failed, 1 means success.
429 static bool RaiseTile(TileIndex tile, Slope slope);
432 * Lower the given corners of the tile. The corners can be combined,
433 * for example: SLOPE_N | SLOPE_W (= SLOPE_NW) will lower the west and the north corner.
434 * @note The corners will be modified in the order west (first), south, east, north (last).
435 * Changing one corner might cause another corner to be changed too. So modifiing
436 * multiple corners may result in changing some corners by multiple steps.
437 * @param tile The tile to lower.
438 * @param slope Corners to lower (SLOPE_xxx).
439 * @pre tile < ScriptMap::GetMapSize().
440 * @game @pre ScriptCompanyMode::IsValid().
441 * @exception ScriptError::ERR_AREA_NOT_CLEAR
442 * @exception ScriptError::ERR_TOO_CLOSE_TO_EDGE
443 * @exception ScriptTile::ERR_TILE_TOO_LOW
444 * @return 0 means failed, 1 means success.
446 static bool LowerTile(TileIndex tile, Slope slope);
449 * Level all tiles in the rectangle between start_tile and end_tile so they
450 * are at the same height. All tiles will be raised or lowered until
451 * they are at height ScriptTile::GetCornerHeight(start_tile, ScriptTile::CORNER_N).
452 * @param start_tile One corner of the rectangle to level.
453 * @param end_tile The opposite corner of the rectangle.
454 * @pre start_tile < ScriptMap::GetMapSize().
455 * @pre end_tile < ScriptMap::GetMapSize().
456 * @game @pre ScriptCompanyMode::IsValid().
457 * @exception ScriptError::ERR_AREA_NOT_CLEAR
458 * @exception ScriptError::ERR_TOO_CLOSE_TO_EDGE
459 * @return True if one or more tiles were leveled.
460 * @note Even if leveling some part fails, some other part may have been
461 * successfully leveled already.
462 * @note This function may return true in ScriptTestMode, although it fails in
463 * ScriptExecMode.
465 static bool LevelTiles(TileIndex start_tile, TileIndex end_tile);
468 * Destroy everything on the given tile.
469 * @param tile The tile to demolish.
470 * @pre ScriptMap::IsValidTile(tile).
471 * @exception ScriptError::ERR_AREA_NOT_CLEAR
472 * @return True if and only if the tile was demolished.
474 static bool DemolishTile(TileIndex tile);
477 * Create a random tree on a tile.
478 * @param tile The tile to build a tree on.
479 * @pre ScriptMap::IsValidTile(tile).
480 * @game @pre ScriptCompanyMode::IsValid().
481 * @return True if and only if a tree was added on the tile.
483 static bool PlantTree(TileIndex tile);
486 * Create a random tree on a rectangle of tiles.
487 * @param tile The top left tile of the rectangle.
488 * @param width The width of the rectangle.
489 * @param height The height of the rectangle.
490 * @pre ScriptMap::IsValidTile(tile).
491 * @pre width >= 1 && width <= 20.
492 * @pre height >= 1 && height <= 20.
493 * @game @pre ScriptCompanyMode::IsValid().
494 * @return True if and only if a tree was added on any of the tiles in the rectangle.
496 static bool PlantTreeRectangle(TileIndex tile, SQInteger width, SQInteger height);
499 * Find out if this tile is within the rating influence of a town.
500 * If a station sign would be on this tile, the servicing quality of the station would
501 * influence the rating of the town.
502 * @param tile The tile to check.
503 * @param town_id The town to check.
504 * @return True if the tile is within the rating influence of the town.
506 static bool IsWithinTownInfluence(TileIndex tile, TownID town_id);
509 * Find the town which has authority for the tile.
510 * The rating of your company in this town will be checked and affected when
511 * building stations, trees etc.
512 * @param tile The tile to check.
513 * @return The TownID of the town which has authority on this tile.
515 static TownID GetTownAuthority(TileIndex tile);
518 * Find the town that is closest to a tile. Stations you build at this tile
519 * will belong to this town.
520 * @param tile The tile to check.
521 * @return The TownID of the town closest to the tile.
523 static TownID GetClosestTown(TileIndex tile);
526 * Get the baseprice of building/clearing various tile-related things.
527 * @param build_type the type to build
528 * @return The baseprice of building or removing the given object.
530 static Money GetBuildCost(BuildType build_type);
533 #endif /* SCRIPT_TILE_HPP */