Use std::set to keep track of the flow area of a river
[openttd/fttd.git] / src / landscape.cpp
blob8ef935d4af17e78cd55df5d7a08de4ad7ecd5a08
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file landscape.cpp Functions related to the landscape (slopes etc.). */
12 /** @defgroup SnowLineGroup Snowline functions and data structures */
14 #include "stdafx.h"
15 #include "map/ground.h"
16 #include "map/slope.h"
17 #include "heightmap.h"
18 #include "spritecache.h"
19 #include "viewport_func.h"
20 #include "command_func.h"
21 #include "landscape.h"
22 #include "tgp.h"
23 #include "genworld.h"
24 #include "fios.h"
25 #include "date_func.h"
26 #include "water.h"
27 #include "effectvehicle_func.h"
28 #include "landscape_type.h"
29 #include "animated_tile_func.h"
30 #include "core/random_func.hpp"
31 #include "object_base.h"
32 #include "company_func.h"
33 #include "pathfinder/yapf/astar.hpp"
34 #include <list>
35 #include <set>
37 #include "table/strings.h"
38 #include "table/sprites.h"
40 extern const TileTypeProcs
41 _tile_type_clear_procs,
42 _tile_type_rail_procs,
43 _tile_type_road_procs,
44 _tile_type_town_procs,
45 _tile_type_misc_procs,
46 _tile_type_station_procs,
47 _tile_type_water_procs,
48 _tile_type_industry_procs,
49 _tile_type_object_procs;
51 /**
52 * Tile callback functions for each type of tile.
53 * @ingroup TileCallbackGroup
54 * @see TileType
56 extern const TileTypeProcs * const _tile_type_procs[16] = {
57 &_tile_type_clear_procs, ///< Callback functions for clear tiles
58 &_tile_type_object_procs, ///< Callback functions for object tiles
59 &_tile_type_water_procs, ///< Callback functions for water tiles
60 NULL,
61 &_tile_type_rail_procs, ///< Callback functions for railway tiles
62 &_tile_type_road_procs, ///< Callback functions for road tiles
63 &_tile_type_misc_procs, ///< Callback functions for misc tiles
64 &_tile_type_station_procs, ///< Callback functions for station tiles
65 &_tile_type_industry_procs, ///< Callback functions for industry tiles
66 &_tile_type_industry_procs,
67 &_tile_type_industry_procs,
68 &_tile_type_industry_procs,
69 &_tile_type_town_procs, ///< Callback functions for house tiles
70 &_tile_type_town_procs,
71 &_tile_type_town_procs,
72 &_tile_type_town_procs,
75 /** landscape slope => sprite */
76 extern const byte _slope_to_sprite_offset[32] = {
77 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0,
78 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 17, 0, 15, 18, 0,
81 /**
82 * Description of the snow line throughout the year.
84 * If it is \c NULL, a static snowline height is used, as set by \c _settings_game.game_creation.snow_line_height.
85 * Otherwise it points to a table loaded from a newGRF file that describes the variable snowline.
86 * @ingroup SnowLineGroup
87 * @see GetSnowLine() GameCreationSettings
89 static SnowLine *_snow_line = NULL;
91 /**
92 * Applies a foundation to a slope.
94 * @pre Foundation and slope must be valid combined.
95 * @param f The #Foundation.
96 * @param s The #Slope to modify.
97 * @return Increment to the tile Z coordinate.
99 uint ApplyFoundationToSlope(Foundation f, Slope *s)
101 if (!IsFoundation(f)) return 0;
103 if (IsLeveledFoundation(f)) {
104 uint dz = 1 + (IsSteepSlope(*s) ? 1 : 0);
105 *s = SLOPE_FLAT;
106 return dz;
109 if (f != FOUNDATION_STEEP_BOTH && IsNonContinuousFoundation(f)) {
110 *s = HalftileSlope(*s, GetHalftileFoundationCorner(f));
111 return 0;
114 if (IsSpecialRailFoundation(f)) {
115 *s = SlopeWithThreeCornersRaised(OppositeCorner(GetRailFoundationCorner(f)));
116 return 0;
119 uint dz = IsSteepSlope(*s) ? 1 : 0;
120 Corner highest_corner = GetHighestSlopeCorner(*s);
122 switch (f) {
123 case FOUNDATION_INCLINED_X:
124 *s = (((highest_corner == CORNER_W) || (highest_corner == CORNER_S)) ? SLOPE_SW : SLOPE_NE);
125 break;
127 case FOUNDATION_INCLINED_Y:
128 *s = (((highest_corner == CORNER_S) || (highest_corner == CORNER_E)) ? SLOPE_SE : SLOPE_NW);
129 break;
131 case FOUNDATION_STEEP_LOWER:
132 *s = SlopeWithOneCornerRaised(highest_corner);
133 break;
135 case FOUNDATION_STEEP_BOTH:
136 *s = HalftileSlope(SlopeWithOneCornerRaised(highest_corner), highest_corner);
137 break;
139 default: NOT_REACHED();
141 return dz;
146 * Determines height at given coordinate of a slope
147 * @param x x coordinate
148 * @param y y coordinate
149 * @param corners slope to examine
150 * @return height of given point of given slope
152 uint GetPartialPixelZ(int x, int y, Slope corners)
154 if (IsHalftileSlope(corners)) {
155 switch (GetHalftileSlopeCorner(corners)) {
156 case CORNER_W:
157 if (x - y >= 0) return GetSlopeMaxPixelZ(corners);
158 break;
160 case CORNER_S:
161 if (x - (y ^ 0xF) >= 0) return GetSlopeMaxPixelZ(corners);
162 break;
164 case CORNER_E:
165 if (y - x >= 0) return GetSlopeMaxPixelZ(corners);
166 break;
168 case CORNER_N:
169 if ((y ^ 0xF) - x >= 0) return GetSlopeMaxPixelZ(corners);
170 break;
172 default: NOT_REACHED();
176 int z = 0;
178 switch (RemoveHalftileSlope(corners)) {
179 case SLOPE_W:
180 if (x - y >= 0) {
181 z = (x - y) >> 1;
183 break;
185 case SLOPE_S:
186 y ^= 0xF;
187 if ((x - y) >= 0) {
188 z = (x - y) >> 1;
190 break;
192 case SLOPE_SW:
193 z = (x >> 1) + 1;
194 break;
196 case SLOPE_E:
197 if (y - x >= 0) {
198 z = (y - x) >> 1;
200 break;
202 case SLOPE_EW:
203 case SLOPE_NS:
204 case SLOPE_ELEVATED:
205 z = 4;
206 break;
208 case SLOPE_SE:
209 z = (y >> 1) + 1;
210 break;
212 case SLOPE_WSE:
213 z = 8;
214 y ^= 0xF;
215 if (x - y < 0) {
216 z += (x - y) >> 1;
218 break;
220 case SLOPE_N:
221 y ^= 0xF;
222 if (y - x >= 0) {
223 z = (y - x) >> 1;
225 break;
227 case SLOPE_NW:
228 z = (y ^ 0xF) >> 1;
229 break;
231 case SLOPE_NWS:
232 z = 8;
233 if (x - y < 0) {
234 z += (x - y) >> 1;
236 break;
238 case SLOPE_NE:
239 z = (x ^ 0xF) >> 1;
240 break;
242 case SLOPE_ENW:
243 z = 8;
244 y ^= 0xF;
245 if (y - x < 0) {
246 z += (y - x) >> 1;
248 break;
250 case SLOPE_SEN:
251 z = 8;
252 if (y - x < 0) {
253 z += (y - x) >> 1;
255 break;
257 case SLOPE_STEEP_S:
258 z = 1 + ((x + y) >> 1);
259 break;
261 case SLOPE_STEEP_W:
262 z = 1 + ((x + (y ^ 0xF)) >> 1);
263 break;
265 case SLOPE_STEEP_N:
266 z = 1 + (((x ^ 0xF) + (y ^ 0xF)) >> 1);
267 break;
269 case SLOPE_STEEP_E:
270 z = 1 + (((x ^ 0xF) + y) >> 1);
271 break;
273 default: break;
276 return z;
279 int GetSlopePixelZ(int x, int y)
281 TileIndex tile = TileVirtXY(x, y);
283 return GetTileProcs(tile)->get_slope_z_proc(tile, x, y);
287 * Determine the Z height of a corner relative to TileZ.
289 * @pre The slope must not be a halftile slope.
291 * @param tileh The slope.
292 * @param corner The corner.
293 * @return Z position of corner relative to TileZ.
295 int GetSlopeZInCorner(Slope tileh, Corner corner)
297 assert(!IsHalftileSlope(tileh));
298 return ((tileh & SlopeWithOneCornerRaised(corner)) != 0 ? 1 : 0) + (tileh == SteepSlope(corner) ? 1 : 0);
302 * Determine the Z height of the corners of a specific tile edge
304 * @note If a tile has a non-continuous halftile foundation, a corner can have different heights wrt. its edges.
306 * @pre z1 and z2 must be initialized (typ. with TileZ). The corner heights just get added.
308 * @param tileh The slope of the tile.
309 * @param edge The edge of interest.
310 * @param z1 Gets incremented by the height of the first corner of the edge. (near corner wrt. the camera)
311 * @param z2 Gets incremented by the height of the second corner of the edge. (far corner wrt. the camera)
313 void GetSlopePixelZOnEdge(Slope tileh, DiagDirection edge, int *z1, int *z2)
315 static const Slope corners[4][4] = {
316 /* corner | steep slope
317 * z1 z2 | z1 z2 */
318 {SLOPE_E, SLOPE_N, SLOPE_STEEP_E, SLOPE_STEEP_N}, // DIAGDIR_NE, z1 = E, z2 = N
319 {SLOPE_S, SLOPE_E, SLOPE_STEEP_S, SLOPE_STEEP_E}, // DIAGDIR_SE, z1 = S, z2 = E
320 {SLOPE_S, SLOPE_W, SLOPE_STEEP_S, SLOPE_STEEP_W}, // DIAGDIR_SW, z1 = S, z2 = W
321 {SLOPE_W, SLOPE_N, SLOPE_STEEP_W, SLOPE_STEEP_N}, // DIAGDIR_NW, z1 = W, z2 = N
324 int halftile_test = (IsHalftileSlope(tileh) ? SlopeWithOneCornerRaised(GetHalftileSlopeCorner(tileh)) : 0);
325 if (halftile_test == corners[edge][0]) *z2 += TILE_HEIGHT; // The slope is non-continuous in z2. z2 is on the upper side.
326 if (halftile_test == corners[edge][1]) *z1 += TILE_HEIGHT; // The slope is non-continuous in z1. z1 is on the upper side.
328 if ((tileh & corners[edge][0]) != 0) *z1 += TILE_HEIGHT; // z1 is raised
329 if ((tileh & corners[edge][1]) != 0) *z2 += TILE_HEIGHT; // z2 is raised
330 if (RemoveHalftileSlope(tileh) == corners[edge][2]) *z1 += TILE_HEIGHT; // z1 is highest corner of a steep slope
331 if (RemoveHalftileSlope(tileh) == corners[edge][3]) *z2 += TILE_HEIGHT; // z2 is highest corner of a steep slope
335 * Get slope of a tile on top of a (possible) foundation
336 * If a tile does not have a foundation, the function returns the same as GetTileSlope.
338 * @param tile The tile of interest.
339 * @param z returns the z of the foundation slope. (Can be NULL, if not needed)
340 * @return The slope on top of the foundation.
342 Slope GetFoundationSlope(TileIndex tile, int *z)
344 Slope tileh = GetTileSlope(tile, z);
345 Foundation f = GetTileProcs(tile)->get_foundation_proc(tile, tileh);
346 uint z_inc = ApplyFoundationToSlope(f, &tileh);
347 if (z != NULL) *z += z_inc;
348 return tileh;
352 bool HasFoundationNW(TileIndex tile, Slope slope_here, uint z_here)
354 int z;
356 int z_W_here = z_here;
357 int z_N_here = z_here;
358 GetSlopePixelZOnEdge(slope_here, DIAGDIR_NW, &z_W_here, &z_N_here);
360 Slope slope = GetFoundationPixelSlope(TILE_ADDXY(tile, 0, -1), &z);
361 int z_W = z;
362 int z_N = z;
363 GetSlopePixelZOnEdge(slope, DIAGDIR_SE, &z_W, &z_N);
365 return (z_N_here > z_N) || (z_W_here > z_W);
369 bool HasFoundationNE(TileIndex tile, Slope slope_here, uint z_here)
371 int z;
373 int z_E_here = z_here;
374 int z_N_here = z_here;
375 GetSlopePixelZOnEdge(slope_here, DIAGDIR_NE, &z_E_here, &z_N_here);
377 Slope slope = GetFoundationPixelSlope(TILE_ADDXY(tile, -1, 0), &z);
378 int z_E = z;
379 int z_N = z;
380 GetSlopePixelZOnEdge(slope, DIAGDIR_SW, &z_E, &z_N);
382 return (z_N_here > z_N) || (z_E_here > z_E);
386 * Draw foundation \a f at tile \a ti. Updates \a ti.
387 * @param ti Tile to draw foundation on
388 * @param f Foundation to draw
389 * @param side Side to skip
391 void DrawFoundation(TileInfo *ti, Foundation f, DiagDirection side)
393 if (!IsFoundation(f)) return;
395 /* Two part foundations must be drawn separately */
396 assert(f != FOUNDATION_STEEP_BOTH);
398 uint sprite_block = 0;
399 int z;
400 Slope slope = GetFoundationPixelSlope(ti->tile, &z);
402 /* Select the needed block of foundations sprites
403 * Block 0: Walls at NW and NE edge
404 * Block 1: Wall at NE edge
405 * Block 2: Wall at NW edge
406 * Block 3: No walls at NW or NE edge
408 if (side == DIAGDIR_NW || !HasFoundationNW(ti->tile, slope, z)) sprite_block += 1;
409 if (side == DIAGDIR_NE || !HasFoundationNE(ti->tile, slope, z)) sprite_block += 2;
411 /* Use the original slope sprites if NW and NE borders should be visible */
412 SpriteID leveled_base = (sprite_block == 0 ? (int)SPR_FOUNDATION_BASE : (SPR_SLOPES_VIRTUAL_BASE + sprite_block * SPR_TRKFOUND_BLOCK_SIZE));
413 SpriteID inclined_base = SPR_SLOPES_VIRTUAL_BASE + SPR_SLOPES_INCLINED_OFFSET + sprite_block * SPR_TRKFOUND_BLOCK_SIZE;
414 SpriteID halftile_base = SPR_HALFTILE_FOUNDATION_BASE + sprite_block * SPR_HALFTILE_BLOCK_SIZE;
416 if (IsSteepSlope(ti->tileh)) {
417 if (!IsNonContinuousFoundation(f)) {
418 /* Lower part of foundation */
419 AddSortableSpriteToDraw(
420 leveled_base + (ti->tileh & ~SLOPE_STEEP), PAL_NONE, ti->x, ti->y, 16, 16, 7, ti->z
424 Corner highest_corner = GetHighestSlopeCorner(ti->tileh);
425 ti->z += ApplyPixelFoundationToSlope(f, &ti->tileh);
427 if (IsInclinedFoundation(f)) {
428 /* inclined foundation */
429 byte inclined = highest_corner * 2 + (f == FOUNDATION_INCLINED_Y ? 1 : 0);
431 AddSortableSpriteToDraw(inclined_base + inclined, PAL_NONE, ti->x, ti->y,
432 f == FOUNDATION_INCLINED_X ? 16 : 1,
433 f == FOUNDATION_INCLINED_Y ? 16 : 1,
434 TILE_HEIGHT, ti->z
436 OffsetGroundSprite(31, 9);
437 } else if (IsLeveledFoundation(f)) {
438 AddSortableSpriteToDraw(leveled_base + SlopeWithOneCornerRaised(highest_corner), PAL_NONE, ti->x, ti->y, 16, 16, 7, ti->z - TILE_HEIGHT);
439 OffsetGroundSprite(31, 1);
440 } else if (f == FOUNDATION_STEEP_LOWER) {
441 /* one corner raised */
442 OffsetGroundSprite(31, 1);
443 } else {
444 /* halftile foundation */
445 int x_bb = (((highest_corner == CORNER_W) || (highest_corner == CORNER_S)) ? 8 : 0);
446 int y_bb = (((highest_corner == CORNER_S) || (highest_corner == CORNER_E)) ? 8 : 0);
448 AddSortableSpriteToDraw(halftile_base + highest_corner, PAL_NONE, ti->x + x_bb, ti->y + y_bb, 8, 8, 7, ti->z + TILE_HEIGHT);
449 OffsetGroundSprite(31, 9);
451 } else {
452 if (IsLeveledFoundation(f)) {
453 /* leveled foundation */
454 AddSortableSpriteToDraw(leveled_base + ti->tileh, PAL_NONE, ti->x, ti->y, 16, 16, 7, ti->z);
455 OffsetGroundSprite(31, 1);
456 } else if (IsNonContinuousFoundation(f)) {
457 /* halftile foundation */
458 Corner halftile_corner = GetHalftileFoundationCorner(f);
459 int x_bb = (((halftile_corner == CORNER_W) || (halftile_corner == CORNER_S)) ? 8 : 0);
460 int y_bb = (((halftile_corner == CORNER_S) || (halftile_corner == CORNER_E)) ? 8 : 0);
462 AddSortableSpriteToDraw(halftile_base + halftile_corner, PAL_NONE, ti->x + x_bb, ti->y + y_bb, 8, 8, 7, ti->z);
463 OffsetGroundSprite(31, 9);
464 } else if (IsSpecialRailFoundation(f)) {
465 /* anti-zig-zag foundation */
466 SpriteID spr;
467 if (ti->tileh == SLOPE_NS || ti->tileh == SLOPE_EW) {
468 /* half of leveled foundation under track corner */
469 spr = leveled_base + SlopeWithThreeCornersRaised(GetRailFoundationCorner(f));
470 } else {
471 /* tile-slope = sloped along X/Y, foundation-slope = three corners raised */
472 spr = inclined_base + 2 * GetRailFoundationCorner(f) + ((ti->tileh == SLOPE_SW || ti->tileh == SLOPE_NE) ? 1 : 0);
474 AddSortableSpriteToDraw(spr, PAL_NONE, ti->x, ti->y, 16, 16, 7, ti->z);
475 OffsetGroundSprite(31, 9);
476 } else {
477 /* inclined foundation */
478 byte inclined = GetHighestSlopeCorner(ti->tileh) * 2 + (f == FOUNDATION_INCLINED_Y ? 1 : 0);
480 AddSortableSpriteToDraw(inclined_base + inclined, PAL_NONE, ti->x, ti->y,
481 f == FOUNDATION_INCLINED_X ? 16 : 1,
482 f == FOUNDATION_INCLINED_Y ? 16 : 1,
483 TILE_HEIGHT, ti->z
485 OffsetGroundSprite(31, 9);
487 ti->z += ApplyPixelFoundationToSlope(f, &ti->tileh);
491 void DoClearSquare(TileIndex tile)
493 /* If the tile can have animation and we clear it, delete it from the animated tile list. */
494 if (GetTileProcs(tile)->animate_tile_proc != NULL) DeleteAnimatedTile(tile);
496 MakeClear(tile, GROUND_GRASS, _generating_world ? 3 : 0);
497 MarkTileDirtyByTile(tile);
501 * Returns information about railway trackdirs and signal states.
502 * If there is any trackbit at 'side', return all trackdirbits.
503 * @param tile tile to get info about
504 * @param side side we are entering from, INVALID_DIAGDIR to return all trackbits
505 * @return trackdirbits and other info
507 TrackStatus GetTileRailwayStatus(TileIndex tile, DiagDirection side)
509 GetTileTrackStatusProc *proc = GetTileProcs(tile)->get_tile_railway_status_proc;
510 return proc != NULL ? proc(tile, side) : 0;
514 * Returns information about road trackdirs and signal states.
515 * If there is any trackbit at 'side', return all trackdirbits.
516 * Return no trackbits if there is no roadbit (of given subtype) at given side.
517 * @param tile tile to get info about
518 * @param sub_mode roadtypes to check
519 * @param side side we are entering from, INVALID_DIAGDIR to return all trackbits
520 * @return trackdirbits and other info
522 TrackStatus GetTileRoadStatus(TileIndex tile, uint sub_mode, DiagDirection side)
524 GetTileRoadStatusProc *proc = GetTileProcs(tile)->get_tile_road_status_proc;
525 return proc != NULL ? proc(tile, sub_mode, side) : 0;
529 * Returns information about waterway trackdirs.
530 * If there is any trackbit at 'side', return all trackdirbits.
531 * @param tile tile to get info about
532 * @param side side we are entering from, INVALID_DIAGDIR to return all trackbits
533 * @return trackdirbits
535 TrackdirBits GetTileWaterwayStatus(TileIndex tile, DiagDirection side)
537 GetTileWaterStatusProc *proc = GetTileProcs(tile)->get_tile_waterway_status_proc;
538 return proc != NULL ? proc(tile, side) : TRACKDIR_BIT_NONE;
542 * Change the owner of a tile
543 * @param tile Tile to change
544 * @param old_owner Current owner of the tile
545 * @param new_owner New owner of the tile
547 void ChangeTileOwner(TileIndex tile, Owner old_owner, Owner new_owner)
549 GetTileProcs(tile)->change_tile_owner_proc(tile, old_owner, new_owner);
552 void GetTileDesc(TileIndex tile, TileDesc *td)
554 GetTileProcs(tile)->get_tile_desc_proc(tile, td);
558 * Has a snow line table already been loaded.
559 * @return true if the table has been loaded already.
560 * @ingroup SnowLineGroup
562 bool IsSnowLineSet()
564 return _snow_line != NULL;
568 * Set a variable snow line, as loaded from a newgrf file.
569 * @param table the 12 * 32 byte table containing the snowline for each day
570 * @ingroup SnowLineGroup
572 void SetSnowLine(byte table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS])
574 _snow_line = CallocT<SnowLine>(1);
575 _snow_line->lowest_value = 0xFF;
576 memcpy(_snow_line->table, table, sizeof(_snow_line->table));
578 for (uint i = 0; i < SNOW_LINE_MONTHS; i++) {
579 for (uint j = 0; j < SNOW_LINE_DAYS; j++) {
580 _snow_line->highest_value = max(_snow_line->highest_value, table[i][j]);
581 _snow_line->lowest_value = min(_snow_line->lowest_value, table[i][j]);
587 * Get the current snow line, either variable or static.
588 * @return the snow line height.
589 * @ingroup SnowLineGroup
591 byte GetSnowLine()
593 if (_snow_line == NULL) return _settings_game.game_creation.snow_line_height;
595 YearMonthDay ymd;
596 ConvertDateToYMD(_date, &ymd);
597 return _snow_line->table[ymd.month][ymd.day];
601 * Get the highest possible snow line height, either variable or static.
602 * @return the highest snow line height.
603 * @ingroup SnowLineGroup
605 byte HighestSnowLine()
607 return _snow_line == NULL ? _settings_game.game_creation.snow_line_height : _snow_line->highest_value;
611 * Get the lowest possible snow line height, either variable or static.
612 * @return the lowest snow line height.
613 * @ingroup SnowLineGroup
615 byte LowestSnowLine()
617 return _snow_line == NULL ? _settings_game.game_creation.snow_line_height : _snow_line->lowest_value;
621 * Clear the variable snow line table and free the memory.
622 * @ingroup SnowLineGroup
624 void ClearSnowLine()
626 free(_snow_line);
627 _snow_line = NULL;
631 * Clear a piece of landscape
632 * @param tile tile to clear
633 * @param flags of operation to conduct
634 * @param p1 unused
635 * @param p2 unused
636 * @param text unused
637 * @return the cost of this operation or an error
639 CommandCost CmdLandscapeClear(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
641 CommandCost cost(EXPENSES_CONSTRUCTION);
642 bool do_clear = false;
643 /* Test for stuff which results in water when cleared. Then add the cost to also clear the water. */
644 if ((flags & DC_FORCE_CLEAR_TILE) && HasTileWaterClass(tile) && IsTileOnWater(tile) && !IsPlainWaterTile(tile) && !IsCoastTile(tile)) {
645 if ((flags & DC_AUTO) && GetWaterClass(tile) == WATER_CLASS_CANAL) return_cmd_error(STR_ERROR_MUST_DEMOLISH_CANAL_FIRST);
646 do_clear = true;
647 cost.AddCost(GetWaterClass(tile) == WATER_CLASS_CANAL ? _price[PR_CLEAR_CANAL] : _price[PR_CLEAR_WATER]);
650 Company *c = (flags & (DC_AUTO | DC_BANKRUPT)) ? NULL : Company::GetIfValid(_current_company);
651 if (c != NULL && (int)GB(c->clear_limit, 16, 16) < 1) {
652 return_cmd_error(STR_ERROR_CLEARING_LIMIT_REACHED);
655 const ClearedObjectArea *coa = FindClearedObject(tile);
657 /* If this tile was the first tile which caused object destruction, always
658 * pass it on to the tile_type_proc. That way multiple test runs and the exec run stay consistent. */
659 if (coa != NULL && coa->first_tile != tile) {
660 /* If this tile belongs to an object which was already cleared via another tile, pretend it has been
661 * already removed.
662 * However, we need to check stuff, which is not the same for all object tiles. (e.g. being on water or not) */
664 /* If a object is removed, it leaves either bare land or water. */
665 if ((flags & DC_NO_WATER) && HasTileWaterClass(tile) && IsTileOnWater(tile)) {
666 return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER);
668 } else {
669 cost.AddCost(GetTileProcs(tile)->clear_tile_proc(tile, flags));
672 if (flags & DC_EXEC) {
673 if (c != NULL) c->clear_limit -= 1 << 16;
674 if (do_clear) DoClearSquare(tile);
676 return cost;
680 * Clear a big piece of landscape
681 * @param tile end tile of area dragging
682 * @param flags of operation to conduct
683 * @param p1 start tile of area dragging
684 * @param p2 various bitstuffed data.
685 * bit 0: Whether to use the Orthogonal (0) or Diagonal (1) iterator.
686 * @param text unused
687 * @return the cost of this operation or an error
689 CommandCost CmdClearArea(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
691 if (p1 >= MapSize()) return CMD_ERROR;
693 Money money = GetAvailableMoneyForCommand();
694 CommandCost cost(EXPENSES_CONSTRUCTION);
695 CommandCost last_error = CMD_ERROR;
696 bool had_success = false;
698 const Company *c = (flags & (DC_AUTO | DC_BANKRUPT)) ? NULL : Company::GetIfValid(_current_company);
699 int limit = (c == NULL ? INT32_MAX : GB(c->clear_limit, 16, 16));
701 TileArea ta(tile, p1);
702 TileIterator *iter = HasBit(p2, 0) ? (TileIterator *)new DiagonalTileIterator(tile, p1) : new OrthogonalTileIterator(ta);
703 for (; *iter != INVALID_TILE; ++(*iter)) {
704 TileIndex t = *iter;
705 CommandCost ret = DoCommand(t, 0, 0, flags & ~DC_EXEC, CMD_LANDSCAPE_CLEAR);
706 if (ret.Failed()) {
707 last_error = ret;
709 /* We may not clear more tiles. */
710 if (c != NULL && GB(c->clear_limit, 16, 16) < 1) break;
711 continue;
714 had_success = true;
715 if (flags & DC_EXEC) {
716 money -= ret.GetCost();
717 if (ret.GetCost() > 0 && money < 0) {
718 _additional_cash_required = ret.GetCost();
719 delete iter;
720 return cost;
722 DoCommand(t, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
724 /* draw explosion animation...
725 * Disable explosions when game is paused. Looks silly and blocks the view. */
726 TileIndex off = t - ta.tile;
727 if ((TileX(off) == 0 || TileX(off) == ta.w - 1U) && (TileY(off) == 0 || TileY(off) == ta.h - 1U) && _pause_mode == PM_UNPAUSED) {
728 /* big explosion in each corner, or small explosion for single tiles */
729 CreateEffectVehicleAbove(TileX(t) * TILE_SIZE + TILE_SIZE / 2, TileY(t) * TILE_SIZE + TILE_SIZE / 2, 2,
730 ta.w == 1 && ta.h == 1 ? EV_EXPLOSION_SMALL : EV_EXPLOSION_LARGE
733 } else {
734 /* When we're at the clearing limit we better bail (unneed) testing as well. */
735 if (ret.GetCost() != 0 && --limit <= 0) break;
737 cost.AddCost(ret);
740 delete iter;
741 return had_success ? cost : last_error;
745 TileIndex _cur_tileloop_tile;
748 * Gradually iterate over all tiles on the map, calling their TileLoopProcs once every 256 ticks.
750 void RunTileLoop()
752 /* The pseudorandom sequence of tiles is generated using a Galois linear feedback
753 * shift register (LFSR). This allows a deterministic pseudorandom ordering, but
754 * still with minimal state and fast iteration. */
756 /* Maximal length LFSR feedback terms, from 12-bit (for 64x64 maps) to 22-bit (for 2048x2048 maps).
757 * Extracted from http://www.ece.cmu.edu/~koopman/lfsr/ */
758 static const uint32 feedbacks[] = {
759 0xD8F, 0x1296, 0x2496, 0x4357, 0x8679, 0x1030E, 0x206CD, 0x403FE, 0x807B8, 0x1004B2, 0x2006A8
761 const uint32 feedback = feedbacks[MapLogX() + MapLogY() - 12];
763 /* We update every tile every 256 ticks, so divide the map size by 2^8 = 256 */
764 uint count = 1 << (MapLogX() + MapLogY() - 8);
766 TileIndex tile = _cur_tileloop_tile;
767 /* The LFSR cannot have a zeroed state. */
768 assert(tile != 0);
770 /* Manually update tile 0 every 256 ticks - the LFSR never iterates over it itself. */
771 if (_tick_counter % 256 == 0) {
772 GetTileProcs(0)->tile_loop_proc(0);
773 count--;
776 while (count--) {
777 GetTileProcs(tile)->tile_loop_proc(tile);
779 /* Get the next tile in sequence using a Galois LFSR. */
780 tile = (tile >> 1) ^ (-(int32)(tile & 1) & feedback);
783 _cur_tileloop_tile = tile;
786 void InitializeLandscape()
788 uint maxx = MapMaxX();
789 uint maxy = MapMaxY();
790 uint sizex = MapSizeX();
792 uint y;
793 for (y = _settings_game.construction.freeform_edges ? 1 : 0; y < maxy; y++) {
794 uint x;
795 for (x = _settings_game.construction.freeform_edges ? 1 : 0; x < maxx; x++) {
796 MakeClear(sizex * y + x, GROUND_GRASS, 3);
797 SetTileHeight(sizex * y + x, 0);
798 SetTropicZone(sizex * y + x, TROPICZONE_NORMAL);
799 ClearBridgeMiddle(sizex * y + x);
801 MakeVoid(sizex * y + x);
803 for (uint x = 0; x < sizex; x++) MakeVoid(sizex * y + x);
806 static const byte _genterrain_tbl_1[5] = { 10, 22, 33, 37, 4 };
807 static const byte _genterrain_tbl_2[5] = { 0, 0, 0, 0, 33 };
809 static void GenerateTerrain(int type, uint flag)
811 uint32 r = Random();
813 const Sprite *templ = GetSprite((((r >> 24) * _genterrain_tbl_1[type]) >> 8) + _genterrain_tbl_2[type] + 4845, ST_MAPGEN);
814 if (templ == NULL) usererror("Map generator sprites could not be loaded");
816 uint x = r & MapMaxX();
817 uint y = (r >> MapLogX()) & MapMaxY();
819 if (x < 2 || y < 2) return;
821 DiagDirection direction = (DiagDirection)GB(r, 22, 2);
822 uint w = templ->width;
823 uint h = templ->height;
825 if (DiagDirToAxis(direction) == AXIS_Y) Swap(w, h);
827 const byte *p = templ->data;
829 if ((flag & 4) != 0) {
830 uint xw = x * MapSizeY();
831 uint yw = y * MapSizeX();
832 uint bias = (MapSizeX() + MapSizeY()) * 16;
834 switch (flag & 3) {
835 default: NOT_REACHED();
836 case 0:
837 if (xw + yw > MapSize() - bias) return;
838 break;
840 case 1:
841 if (yw < xw + bias) return;
842 break;
844 case 2:
845 if (xw + yw < MapSize() + bias) return;
846 break;
848 case 3:
849 if (xw < yw + bias) return;
850 break;
854 if (x + w >= MapMaxX() - 1) return;
855 if (y + h >= MapMaxY() - 1) return;
857 TileIndex tile = TileXY(x, y);
859 switch (direction) {
860 default: NOT_REACHED();
861 case DIAGDIR_NE:
862 do {
863 TileIndex tile_cur = tile;
865 for (uint w_cur = w; w_cur != 0; --w_cur) {
866 if (GB(*p, 0, 4) >= TileHeight(tile_cur)) SetTileHeight(tile_cur, GB(*p, 0, 4));
867 p++;
868 tile_cur++;
870 tile += TileDiffXY(0, 1);
871 } while (--h != 0);
872 break;
874 case DIAGDIR_SE:
875 do {
876 TileIndex tile_cur = tile;
878 for (uint h_cur = h; h_cur != 0; --h_cur) {
879 if (GB(*p, 0, 4) >= TileHeight(tile_cur)) SetTileHeight(tile_cur, GB(*p, 0, 4));
880 p++;
881 tile_cur += TileDiffXY(0, 1);
883 tile += TileDiffXY(1, 0);
884 } while (--w != 0);
885 break;
887 case DIAGDIR_SW:
888 tile += TileDiffXY(w - 1, 0);
889 do {
890 TileIndex tile_cur = tile;
892 for (uint w_cur = w; w_cur != 0; --w_cur) {
893 if (GB(*p, 0, 4) >= TileHeight(tile_cur)) SetTileHeight(tile_cur, GB(*p, 0, 4));
894 p++;
895 tile_cur--;
897 tile += TileDiffXY(0, 1);
898 } while (--h != 0);
899 break;
901 case DIAGDIR_NW:
902 tile += TileDiffXY(0, h - 1);
903 do {
904 TileIndex tile_cur = tile;
906 for (uint h_cur = h; h_cur != 0; --h_cur) {
907 if (GB(*p, 0, 4) >= TileHeight(tile_cur)) SetTileHeight(tile_cur, GB(*p, 0, 4));
908 p++;
909 tile_cur -= TileDiffXY(0, 1);
911 tile += TileDiffXY(1, 0);
912 } while (--w != 0);
913 break;
918 #include "table/genland.h"
920 static void CreateDesertOrRainForest()
922 TileIndex update_freq = MapSize() / 4;
923 const CoordDiff *data;
925 for (TileIndex tile = 0; tile != MapSize(); ++tile) {
926 if ((tile % update_freq) == 0) IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
928 if (!IsValidTile(tile)) continue;
930 for (data = _make_desert_or_rainforest_data;
931 data != endof(_make_desert_or_rainforest_data); ++data) {
932 TileIndex t = AddCoordDiffWrap(tile, *data);
933 if (t != INVALID_TILE && (TileHeight(t) >= 4 || IsWaterTile(t))) break;
935 if (data == endof(_make_desert_or_rainforest_data)) {
936 SetTropicZone(tile, TROPICZONE_DESERT);
940 for (uint i = 0; i != 256; i++) {
941 if ((i % 64) == 0) IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
943 RunTileLoop();
946 for (TileIndex tile = 0; tile != MapSize(); ++tile) {
947 if ((tile % update_freq) == 0) IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
949 if (!IsValidTile(tile)) continue;
951 for (data = _make_desert_or_rainforest_data;
952 data != endof(_make_desert_or_rainforest_data); ++data) {
953 TileIndex t = AddCoordDiffWrap(tile, *data);
954 if (t != INVALID_TILE && IsClearTile(t) && IsClearGround(t, GROUND_DESERT)) break;
956 if (data == endof(_make_desert_or_rainforest_data)) {
957 SetTropicZone(tile, TROPICZONE_RAINFOREST);
963 * Find the spring of a river.
964 * @param tile The tile to consider for being the spring.
965 * @param user_data Ignored data.
966 * @return True iff it is suitable as a spring.
968 static bool FindSpring(TileIndex tile, void *user_data)
970 int referenceHeight;
971 if (!IsTileFlat(tile, &referenceHeight) || IsPlainWaterTile(tile)) return false;
973 /* In the tropics rivers start in the rainforest. */
974 if (_settings_game.game_creation.landscape == LT_TROPIC && GetTropicZone(tile) != TROPICZONE_RAINFOREST) return false;
976 /* Are there enough higher tiles to warrant a 'spring'? */
977 uint num = 0;
978 for (int dx = -1; dx <= 1; dx++) {
979 for (int dy = -1; dy <= 1; dy++) {
980 TileIndex t = TileAddWrap(tile, dx, dy);
981 if (t != INVALID_TILE && GetTileMaxZ(t) > referenceHeight) num++;
985 if (num < 4) return false;
987 /* Are we near the top of a hill? */
988 for (int dx = -16; dx <= 16; dx++) {
989 for (int dy = -16; dy <= 16; dy++) {
990 TileIndex t = TileAddWrap(tile, dx, dy);
991 if (t != INVALID_TILE && GetTileMaxZ(t) > referenceHeight + 2) return false;
995 return true;
999 * Make a connected lake; fill all tiles in the circular tile search that are connected.
1000 * @param tile The tile to consider for lake making.
1001 * @param user_data The height of the lake.
1002 * @return Always false, so it continues searching.
1004 static bool MakeLake(TileIndex tile, void *user_data)
1006 uint height = *(uint*)user_data;
1007 if (!IsValidTile(tile) || TileHeight(tile) != height || !IsTileFlat(tile)) return false;
1008 if (_settings_game.game_creation.landscape == LT_TROPIC && GetTropicZone(tile) == TROPICZONE_DESERT) return false;
1010 for (DiagDirection d = DIAGDIR_BEGIN; d < DIAGDIR_END; d++) {
1011 TileIndex t2 = tile + TileOffsByDiagDir(d);
1012 if (IsPlainWaterTile(t2)) {
1013 MakeRiver(tile, Random());
1014 return false;
1018 return false;
1022 * Check whether a river at begin could (logically) flow down to end.
1023 * @param begin The origin of the flow.
1024 * @param end The destination of the flow.
1025 * @return True iff the water can be flowing down.
1027 static bool FlowsDown(TileIndex begin, TileIndex end)
1029 assert(DistanceManhattan(begin, end) == 1);
1031 int heightBegin;
1032 int heightEnd;
1033 Slope slopeBegin = GetTileSlope(begin, &heightBegin);
1034 Slope slopeEnd = GetTileSlope(end, &heightEnd);
1036 /* Slope is either inclined or flat; rivers don't support other slopes. */
1037 if (slopeEnd == SLOPE_FLAT) {
1038 return heightEnd <= heightBegin;
1039 } else if (slopeBegin == SLOPE_FLAT) {
1040 return heightEnd <= heightBegin && IsInclinedSlope(slopeEnd);
1041 } else {
1042 /* Slope continues, then it must be lower. */
1043 assert (IsInclinedSlope(slopeBegin));
1044 return heightEnd < heightBegin && slopeEnd == slopeBegin;
1048 /** River node struct for Astar. */
1049 struct RiverNode : AstarNodeBase<RiverNode> {
1050 typedef AstarNodeBase<RiverNode> Base;
1051 typedef RiverNode Key; // we are our own key
1053 TileIndex tile;
1055 void Set (RiverNode *parent, TileIndex t)
1057 Base::Set (parent);
1058 tile = t;
1061 bool operator == (const RiverNode &other) const
1063 return tile == other.tile;
1066 const Key& GetKey() const
1068 return *this;
1071 int CalcHash() const
1073 return TileHash (TileX(tile), TileY(tile));
1077 /** River pathfinder. */
1078 struct RiverAstar : Astar <RiverNode, 8, 8>
1080 const TileIndex target;
1082 RiverAstar (TileIndex target) : target(target) { }
1086 * River neighbour finder for the A-star algorithm
1088 static void RiverFollow (RiverAstar *a, RiverNode *n)
1090 for (DiagDirection d = DIAGDIR_BEGIN; d < DIAGDIR_END; d++) {
1091 TileIndex tile = TileAddByDiagDir (n->tile, d);
1092 if (IsValidTile(tile) && FlowsDown(n->tile, tile)) {
1093 RiverNode *m = a->CreateNewNode (n, tile);
1094 m->m_cost = n->m_cost + 1 + RandomRange(_settings_game.game_creation.river_route_random);
1095 if (tile == a->target) {
1096 m->m_estimate = m->m_cost;
1097 a->FoundTarget(m);
1098 } else {
1099 m->m_estimate = m->m_cost + DistanceManhattan(tile, a->target);
1100 a->InsertNode(m);
1107 * Actually build the river between the begin and end tiles using AyStar.
1108 * @param begin The begin of the river.
1109 * @param end The end of the river.
1111 static void BuildRiver(TileIndex begin, TileIndex end)
1113 RiverAstar finder (end);
1114 finder.InsertInitialNode (finder.CreateNewNode (NULL, begin));
1116 if (finder.FindPath(&RiverFollow)) {
1117 for (RiverNode *n = finder.best; n != NULL; n = n->m_parent) {
1118 TileIndex tile = n->tile;
1119 if (!IsPlainWaterTile(tile)) {
1120 MakeRiver(tile, Random());
1121 /* Remove desert directly around the river tile. */
1122 CircularTileSearch(&tile, 5, RiverModifyDesertZone, NULL);
1129 * Try to flow the river down from a given begin.
1130 * @param spring The springing point of the river.
1131 * @param begin The begin point we are looking from; somewhere down hill from the spring.
1132 * @return True iff a river could/has been built, otherwise false.
1134 static bool FlowRiver(TileIndex spring, TileIndex begin)
1136 #define SET_MARK(x) marks.insert(x)
1137 #define IS_MARKED(x) (marks.find(x) != marks.end())
1139 uint height = TileHeight(begin);
1140 if (IsPlainWaterTile(begin)) return DistanceManhattan(spring, begin) > _settings_game.game_creation.min_river_length;
1142 std::set<TileIndex> marks;
1143 SET_MARK(begin);
1145 /* Breadth first search for the closest tile we can flow down to. */
1146 std::list<TileIndex> queue;
1147 queue.push_back(begin);
1149 bool found = false;
1150 uint count = 0; // Number of tiles considered; to be used for lake location guessing.
1151 TileIndex end;
1152 do {
1153 end = queue.front();
1154 queue.pop_front();
1156 uint height2 = TileHeight(end);
1157 if (IsTileFlat(end) && (height2 < height || (height2 == height && IsPlainWaterTile(end)))) {
1158 found = true;
1159 break;
1162 for (DiagDirection d = DIAGDIR_BEGIN; d < DIAGDIR_END; d++) {
1163 TileIndex t2 = end + TileOffsByDiagDir(d);
1164 if (IsValidTile(t2) && !IS_MARKED(t2) && FlowsDown(end, t2)) {
1165 SET_MARK(t2);
1166 count++;
1167 queue.push_back(t2);
1170 } while (!queue.empty());
1172 if (found) {
1173 /* Flow further down hill. */
1174 found = FlowRiver(spring, end);
1175 } else if (count > 32) {
1176 /* Maybe we can make a lake. Find the Nth of the considered tiles. */
1177 TileIndex lakeCenter = 0;
1178 int i = RandomRange(count - 1) + 1;
1179 std::set<TileIndex>::const_iterator cit = marks.begin();
1180 while (--i) cit++;
1181 lakeCenter = *cit;
1183 if (IsValidTile(lakeCenter) &&
1184 /* A river, or lake, can only be built on flat slopes. */
1185 IsTileFlat(lakeCenter) &&
1186 /* We want the lake to be built at the height of the river. */
1187 TileHeight(begin) == TileHeight(lakeCenter) &&
1188 /* We don't want the lake at the entry of the valley. */
1189 lakeCenter != begin &&
1190 /* We don't want lakes in the desert. */
1191 (_settings_game.game_creation.landscape != LT_TROPIC || GetTropicZone(lakeCenter) != TROPICZONE_DESERT) &&
1192 /* We only want a lake if the river is long enough. */
1193 DistanceManhattan(spring, lakeCenter) > _settings_game.game_creation.min_river_length) {
1194 end = lakeCenter;
1195 MakeRiver(lakeCenter, Random());
1196 uint range = RandomRange(8) + 3;
1197 CircularTileSearch(&lakeCenter, range, MakeLake, &height);
1198 /* Call the search a second time so artefacts from going circular in one direction get (mostly) hidden. */
1199 lakeCenter = end;
1200 CircularTileSearch(&lakeCenter, range, MakeLake, &height);
1201 found = true;
1205 marks.clear();
1206 if (found) BuildRiver(begin, end);
1207 return found;
1211 * Actually (try to) create some rivers.
1213 static void CreateRivers()
1215 int amount = _settings_game.game_creation.amount_of_rivers;
1216 if (amount == 0) return;
1218 uint wells = ScaleByMapSize(4 << _settings_game.game_creation.amount_of_rivers);
1219 SetGeneratingWorldProgress(GWP_RIVER, wells + 256 / 64); // Include the tile loop calls below.
1221 for (; wells != 0; wells--) {
1222 IncreaseGeneratingWorldProgress(GWP_RIVER);
1223 for (int tries = 0; tries < 128; tries++) {
1224 TileIndex t = RandomTile();
1225 if (!CircularTileSearch(&t, 8, FindSpring, NULL)) continue;
1226 if (FlowRiver(t, t)) break;
1230 /* Run tile loop to update the ground density. */
1231 for (uint i = 0; i != 256; i++) {
1232 if (i % 64 == 0) IncreaseGeneratingWorldProgress(GWP_RIVER);
1233 RunTileLoop();
1237 void GenerateLandscape(byte mode)
1239 /** Number of steps of landscape generation */
1240 enum GenLandscapeSteps {
1241 GLS_HEIGHTMAP = 3, ///< Loading a heightmap
1242 GLS_TERRAGENESIS = 5, ///< Terragenesis generator
1243 GLS_ORIGINAL = 2, ///< Original generator
1244 GLS_TROPIC = 12, ///< Extra steps needed for tropic landscape
1245 GLS_OTHER = 0, ///< Extra steps for other landscapes
1247 uint steps = (_settings_game.game_creation.landscape == LT_TROPIC) ? GLS_TROPIC : GLS_OTHER;
1249 if (mode == GWM_HEIGHTMAP) {
1250 SetGeneratingWorldProgress(GWP_LANDSCAPE, steps + GLS_HEIGHTMAP);
1251 LoadHeightmap(_file_to_saveload.name);
1252 IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
1253 } else if (_settings_game.game_creation.land_generator == LG_TERRAGENESIS) {
1254 SetGeneratingWorldProgress(GWP_LANDSCAPE, steps + GLS_TERRAGENESIS);
1255 GenerateTerrainPerlin();
1256 } else {
1257 SetGeneratingWorldProgress(GWP_LANDSCAPE, steps + GLS_ORIGINAL);
1258 if (_settings_game.construction.freeform_edges) {
1259 for (uint x = 0; x < MapSizeX(); x++) MakeVoid(TileXY(x, 0));
1260 for (uint y = 0; y < MapSizeY(); y++) MakeVoid(TileXY(0, y));
1262 switch (_settings_game.game_creation.landscape) {
1263 case LT_ARCTIC: {
1264 uint32 r = Random();
1266 for (uint i = ScaleByMapSize(GB(r, 0, 7) + 950); i != 0; --i) {
1267 GenerateTerrain(2, 0);
1270 uint flag = GB(r, 7, 2) | 4;
1271 for (uint i = ScaleByMapSize(GB(r, 9, 7) + 450); i != 0; --i) {
1272 GenerateTerrain(4, flag);
1274 break;
1277 case LT_TROPIC: {
1278 uint32 r = Random();
1280 for (uint i = ScaleByMapSize(GB(r, 0, 7) + 170); i != 0; --i) {
1281 GenerateTerrain(0, 0);
1284 uint flag = GB(r, 7, 2) | 4;
1285 for (uint i = ScaleByMapSize(GB(r, 9, 8) + 1700); i != 0; --i) {
1286 GenerateTerrain(0, flag);
1289 flag ^= 2;
1291 for (uint i = ScaleByMapSize(GB(r, 17, 7) + 410); i != 0; --i) {
1292 GenerateTerrain(3, flag);
1294 break;
1297 default: {
1298 uint32 r = Random();
1300 assert(_settings_game.difficulty.quantity_sea_lakes != CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY);
1301 uint i = ScaleByMapSize(GB(r, 0, 7) + (3 - _settings_game.difficulty.quantity_sea_lakes) * 256 + 100);
1302 for (; i != 0; --i) {
1303 GenerateTerrain(_settings_game.difficulty.terrain_type, 0);
1305 break;
1310 /* Do not call IncreaseGeneratingWorldProgress() before FixSlopes(),
1311 * it allows screen redraw. Drawing of broken slopes crashes the game */
1312 FixSlopes();
1313 IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
1314 ConvertGroundTilesIntoWaterTiles();
1315 IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
1317 if (_settings_game.game_creation.landscape == LT_TROPIC) CreateDesertOrRainForest();
1319 CreateRivers();
1322 void OnTick_Town();
1323 void OnTick_Trees();
1324 void OnTick_Station();
1325 void OnTick_Industry();
1327 void OnTick_Companies();
1328 void OnTick_LinkGraph();
1330 void CallLandscapeTick()
1332 OnTick_Town();
1333 OnTick_Trees();
1334 OnTick_Station();
1335 OnTick_Industry();
1337 OnTick_Companies();
1338 OnTick_LinkGraph();