src/landscape.cpp
changeset 5584 1111b4d36e35
parent 5475 2e6990a8c7c4
child 5587 167d9a91ef02
equal deleted inserted replaced
5583:136d8764c7e6 5584:1111b4d36e35
       
     1 /* $Id$ */
       
     2 
       
     3 #include "stdafx.h"
       
     4 #include "openttd.h"
       
     5 #include "bridge_map.h"
       
     6 #include "heightmap.h"
       
     7 #include "clear_map.h"
       
     8 #include "functions.h"
       
     9 #include "map.h"
       
    10 #include "player.h"
       
    11 #include "spritecache.h"
       
    12 #include "table/sprites.h"
       
    13 #include "tile.h"
       
    14 #include <stdarg.h>
       
    15 #include "viewport.h"
       
    16 #include "command.h"
       
    17 #include "vehicle.h"
       
    18 #include "variables.h"
       
    19 #include "void_map.h"
       
    20 #include "water_map.h"
       
    21 #include "tgp.h"
       
    22 #include "genworld.h"
       
    23 #include "heightmap.h"
       
    24 
       
    25 extern const TileTypeProcs
       
    26 	_tile_type_clear_procs,
       
    27 	_tile_type_rail_procs,
       
    28 	_tile_type_road_procs,
       
    29 	_tile_type_town_procs,
       
    30 	_tile_type_trees_procs,
       
    31 	_tile_type_station_procs,
       
    32 	_tile_type_water_procs,
       
    33 	_tile_type_dummy_procs,
       
    34 	_tile_type_industry_procs,
       
    35 	_tile_type_tunnelbridge_procs,
       
    36 	_tile_type_unmovable_procs;
       
    37 
       
    38 const TileTypeProcs * const _tile_type_procs[16] = {
       
    39 	&_tile_type_clear_procs,
       
    40 	&_tile_type_rail_procs,
       
    41 	&_tile_type_road_procs,
       
    42 	&_tile_type_town_procs,
       
    43 	&_tile_type_trees_procs,
       
    44 	&_tile_type_station_procs,
       
    45 	&_tile_type_water_procs,
       
    46 	&_tile_type_dummy_procs,
       
    47 	&_tile_type_industry_procs,
       
    48 	&_tile_type_tunnelbridge_procs,
       
    49 	&_tile_type_unmovable_procs,
       
    50 };
       
    51 
       
    52 /* landscape slope => sprite */
       
    53 const byte _tileh_to_sprite[32] = {
       
    54 	0, 1, 2, 3, 4, 5, 6,  7, 8, 9, 10, 11, 12, 13, 14, 0,
       
    55 	0, 0, 0, 0, 0, 0, 0, 16, 0, 0,  0, 17,  0, 15, 18, 0,
       
    56 };
       
    57 
       
    58 const byte _inclined_tileh[] = {
       
    59 	SLOPE_SW,  SLOPE_NW,  SLOPE_SW,  SLOPE_SE, SLOPE_NE, SLOPE_SE, SLOPE_NE, SLOPE_NW,
       
    60 	SLOPE_E,   SLOPE_N,   SLOPE_W,   SLOPE_S,
       
    61 	SLOPE_NWS, SLOPE_WSE, SLOPE_SEN, SLOPE_ENW
       
    62 };
       
    63 
       
    64 
       
    65 uint GetPartialZ(int x, int y, Slope corners)
       
    66 {
       
    67 	int z = 0;
       
    68 
       
    69 	switch (corners) {
       
    70 	case SLOPE_W:
       
    71 		if (x - y >= 0)
       
    72 			z = (x - y) >> 1;
       
    73 		break;
       
    74 
       
    75 	case SLOPE_S:
       
    76 		y^=0xF;
       
    77 		if ( (x - y) >= 0)
       
    78 			z = (x - y) >> 1;
       
    79 		break;
       
    80 
       
    81 	case SLOPE_SW:
       
    82 		z = (x>>1) + 1;
       
    83 		break;
       
    84 
       
    85 	case SLOPE_E:
       
    86 		if (y - x >= 0)
       
    87 			z = (y - x) >> 1;
       
    88 		break;
       
    89 
       
    90 	case SLOPE_EW:
       
    91 	case SLOPE_NS:
       
    92 	case SLOPE_ELEVATED:
       
    93 		z = 4;
       
    94 		break;
       
    95 
       
    96 	case SLOPE_SE:
       
    97 		z = (y>>1) + 1;
       
    98 		break;
       
    99 
       
   100 	case SLOPE_WSE:
       
   101 		z = 8;
       
   102 		y^=0xF;
       
   103 		if (x - y < 0)
       
   104 			z += (x - y) >> 1;
       
   105 		break;
       
   106 
       
   107 	case SLOPE_N:
       
   108 		y ^= 0xF;
       
   109 		if (y - x >= 0)
       
   110 			z = (y - x) >> 1;
       
   111 		break;
       
   112 
       
   113 	case SLOPE_NW:
       
   114 		z = (y^0xF)>>1;
       
   115 		break;
       
   116 
       
   117 	case SLOPE_NWS:
       
   118 		z = 8;
       
   119 		if (x - y < 0)
       
   120 			z += (x - y) >> 1;
       
   121 		break;
       
   122 
       
   123 	case SLOPE_NE:
       
   124 		z = (x^0xF)>>1;
       
   125 		break;
       
   126 
       
   127 	case SLOPE_ENW:
       
   128 		z = 8;
       
   129 		y ^= 0xF;
       
   130 		if (y - x < 0)
       
   131 			z += (y - x) >> 1;
       
   132 		break;
       
   133 
       
   134 	case SLOPE_SEN:
       
   135 		z = 8;
       
   136 		if (y - x < 0)
       
   137 			z += (y - x) >> 1;
       
   138 		break;
       
   139 
       
   140 	case SLOPE_STEEP_S:
       
   141 		z = 1 + ((x+y)>>1);
       
   142 		break;
       
   143 
       
   144 	case SLOPE_STEEP_W:
       
   145 		z = 1 + ((x+(y^0xF))>>1);
       
   146 		break;
       
   147 
       
   148 	case SLOPE_STEEP_N:
       
   149 		z = 1 + (((x^0xF)+(y^0xF))>>1);
       
   150 		break;
       
   151 
       
   152 	case SLOPE_STEEP_E:
       
   153 		z = 1 + (((x^0xF)+(y^0xF))>>1);
       
   154 		break;
       
   155 
       
   156 		default: break;
       
   157 	}
       
   158 
       
   159 	return z;
       
   160 }
       
   161 
       
   162 uint GetSlopeZ(int x, int y)
       
   163 {
       
   164 	TileIndex tile = TileVirtXY(x, y);
       
   165 
       
   166 	return _tile_type_procs[GetTileType(tile)]->get_slope_z_proc(tile, x, y);
       
   167 }
       
   168 
       
   169 
       
   170 static Slope GetFoundationSlope(TileIndex tile, uint* z)
       
   171 {
       
   172 	Slope tileh = GetTileSlope(tile, z);
       
   173 	Slope slope = _tile_type_procs[GetTileType(tile)]->get_slope_tileh_proc(tile, tileh);
       
   174 
       
   175 	// Flatter slope -> higher base height
       
   176 	if (slope < tileh) *z += TILE_HEIGHT;
       
   177 	return slope;
       
   178 }
       
   179 
       
   180 
       
   181 static bool HasFoundationNW(TileIndex tile, Slope slope_here, uint z_here)
       
   182 {
       
   183 	uint z;
       
   184 	Slope slope = GetFoundationSlope(TILE_ADDXY(tile, 0, -1), &z);
       
   185 
       
   186 	return
       
   187 		(
       
   188 			z_here + (slope_here & SLOPE_N ? TILE_HEIGHT : 0) + (slope_here == SLOPE_STEEP_N ? TILE_HEIGHT : 0) >
       
   189 			z      + (slope      & SLOPE_E ? TILE_HEIGHT : 0) + (slope      == SLOPE_STEEP_E ? TILE_HEIGHT : 0)
       
   190 		) || (
       
   191 			z_here + (slope_here & SLOPE_W ? TILE_HEIGHT : 0) + (slope_here == SLOPE_STEEP_W ? TILE_HEIGHT : 0) >
       
   192 			z      + (slope      & SLOPE_S ? TILE_HEIGHT : 0) + (slope      == SLOPE_STEEP_S ? TILE_HEIGHT : 0)
       
   193 		);
       
   194 }
       
   195 
       
   196 
       
   197 static bool HasFoundationNE(TileIndex tile, Slope slope_here, uint z_here)
       
   198 {
       
   199 	uint z;
       
   200 	Slope slope = GetFoundationSlope(TILE_ADDXY(tile, -1, 0), &z);
       
   201 
       
   202 	return
       
   203 		(
       
   204 			z_here + (slope_here & SLOPE_N ? TILE_HEIGHT : 0) + (slope_here == SLOPE_STEEP_N ? TILE_HEIGHT : 0) >
       
   205 			z      + (slope      & SLOPE_W ? TILE_HEIGHT : 0) + (slope      == SLOPE_STEEP_W ? TILE_HEIGHT : 0)
       
   206 		) || (
       
   207 			z_here + (slope_here & SLOPE_E ? TILE_HEIGHT : 0) + (slope_here == SLOPE_STEEP_E ? TILE_HEIGHT : 0) >
       
   208 			z      + (slope      & SLOPE_S ? TILE_HEIGHT : 0) + (slope      == SLOPE_STEEP_S ? TILE_HEIGHT : 0)
       
   209 		);
       
   210 }
       
   211 
       
   212 
       
   213 void DrawFoundation(TileInfo *ti, uint f)
       
   214 {
       
   215 	uint32 sprite_base = SPR_SLOPES_BASE - 15;
       
   216 	Slope slope;
       
   217 	uint z;
       
   218 
       
   219 	slope = GetFoundationSlope(ti->tile, &z);
       
   220 	if (!HasFoundationNW(ti->tile, slope, z)) sprite_base += 22;
       
   221 	if (!HasFoundationNE(ti->tile, slope, z)) sprite_base += 44;
       
   222 
       
   223 	if (IsSteepSlope(ti->tileh)) {
       
   224 		uint32 lower_base;
       
   225 
       
   226 		// Lower part of foundation
       
   227 		lower_base = sprite_base;
       
   228 		if (lower_base == SPR_SLOPES_BASE - 15) lower_base = SPR_FOUNDATION_BASE;
       
   229 		AddSortableSpriteToDraw(
       
   230 			lower_base + (ti->tileh & ~SLOPE_STEEP), ti->x, ti->y, 16, 16, 7, ti->z
       
   231 		);
       
   232 		ti->z += TILE_HEIGHT;
       
   233 		ti->tileh = _inclined_tileh[f - 15];
       
   234 		if (f < 15 + 8) {
       
   235 			// inclined
       
   236 			AddSortableSpriteToDraw(sprite_base + f, ti->x, ti->y, 16, 16, 1, ti->z);
       
   237 			OffsetGroundSprite(31, 9);
       
   238 		} else if (f >= 15 + 8 + 4) {
       
   239 			// three corners raised
       
   240 			uint32 upper = sprite_base + 15 + (f - 15 - 8 - 4) * 2;
       
   241 
       
   242 			AddSortableSpriteToDraw(upper, ti->x, ti->y, 16, 16, 1, ti->z);
       
   243 			AddChildSpriteScreen(upper + 1, 31, 9);
       
   244 			OffsetGroundSprite(31, 9);
       
   245 		} else {
       
   246 			// one corner raised
       
   247 			OffsetGroundSprite(31, 1);
       
   248 		}
       
   249 	} else {
       
   250 		if (f < 15) {
       
   251 			// leveled foundation
       
   252 			// Use the original slope sprites if NW and NE borders should be visible
       
   253 			if (sprite_base  == SPR_SLOPES_BASE - 15) sprite_base = SPR_FOUNDATION_BASE;
       
   254 
       
   255 			AddSortableSpriteToDraw(sprite_base + f, ti->x, ti->y, 16, 16, 7, ti->z);
       
   256 			ti->z += TILE_HEIGHT;
       
   257 			ti->tileh = SLOPE_FLAT;
       
   258 			OffsetGroundSprite(31, 1);
       
   259 		} else {
       
   260 			// inclined foundation
       
   261 			AddSortableSpriteToDraw(sprite_base + f, ti->x, ti->y, 16, 16, 1, ti->z);
       
   262 			ti->tileh = _inclined_tileh[f - 15];
       
   263 			OffsetGroundSprite(31, 9);
       
   264 		}
       
   265 	}
       
   266 }
       
   267 
       
   268 void DoClearSquare(TileIndex tile)
       
   269 {
       
   270 	MakeClear(tile, CLEAR_GRASS, _generating_world ? 3 : 0);
       
   271 	MarkTileDirtyByTile(tile);
       
   272 }
       
   273 
       
   274 uint32 GetTileTrackStatus(TileIndex tile, TransportType mode)
       
   275 {
       
   276 	return _tile_type_procs[GetTileType(tile)]->get_tile_track_status_proc(tile, mode);
       
   277 }
       
   278 
       
   279 void ChangeTileOwner(TileIndex tile, byte old_player, byte new_player)
       
   280 {
       
   281 	_tile_type_procs[GetTileType(tile)]->change_tile_owner_proc(tile, old_player, new_player);
       
   282 }
       
   283 
       
   284 void GetAcceptedCargo(TileIndex tile, AcceptedCargo ac)
       
   285 {
       
   286 	memset(ac, 0, sizeof(AcceptedCargo));
       
   287 	_tile_type_procs[GetTileType(tile)]->get_accepted_cargo_proc(tile, ac);
       
   288 }
       
   289 
       
   290 void AnimateTile(TileIndex tile)
       
   291 {
       
   292 	_tile_type_procs[GetTileType(tile)]->animate_tile_proc(tile);
       
   293 }
       
   294 
       
   295 void ClickTile(TileIndex tile)
       
   296 {
       
   297 	_tile_type_procs[GetTileType(tile)]->click_tile_proc(tile);
       
   298 }
       
   299 
       
   300 void GetTileDesc(TileIndex tile, TileDesc *td)
       
   301 {
       
   302 	_tile_type_procs[GetTileType(tile)]->get_tile_desc_proc(tile, td);
       
   303 }
       
   304 
       
   305 /** Clear a piece of landscape
       
   306  * @param tile tile to clear
       
   307  * @param p1 unused
       
   308  * @param p2 unused
       
   309  */
       
   310 int32 CmdLandscapeClear(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
       
   311 {
       
   312 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
       
   313 
       
   314 	return _tile_type_procs[GetTileType(tile)]->clear_tile_proc(tile, flags);
       
   315 }
       
   316 
       
   317 /** Clear a big piece of landscape
       
   318  * @param tile end tile of area dragging
       
   319  * @param p1 start tile of area dragging
       
   320  * @param p2 unused
       
   321  */
       
   322 int32 CmdClearArea(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
       
   323 {
       
   324 	int32 cost, ret, money;
       
   325 	int ex;
       
   326 	int ey;
       
   327 	int sx,sy;
       
   328 	int x,y;
       
   329 	bool success = false;
       
   330 
       
   331 	if (p1 >= MapSize()) return CMD_ERROR;
       
   332 
       
   333 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
       
   334 
       
   335 	// make sure sx,sy are smaller than ex,ey
       
   336 	ex = TileX(tile);
       
   337 	ey = TileY(tile);
       
   338 	sx = TileX(p1);
       
   339 	sy = TileY(p1);
       
   340 	if (ex < sx) intswap(ex, sx);
       
   341 	if (ey < sy) intswap(ey, sy);
       
   342 
       
   343 	money = GetAvailableMoneyForCommand();
       
   344 	cost = 0;
       
   345 
       
   346 	for (x = sx; x <= ex; ++x) {
       
   347 		for (y = sy; y <= ey; ++y) {
       
   348 			ret = DoCommand(TileXY(x, y), 0, 0, flags & ~DC_EXEC, CMD_LANDSCAPE_CLEAR);
       
   349 			if (CmdFailed(ret)) continue;
       
   350 			cost += ret;
       
   351 			success = true;
       
   352 
       
   353 			if (flags & DC_EXEC) {
       
   354 				if (ret > 0 && (money -= ret) < 0) {
       
   355 					_additional_cash_required = ret;
       
   356 					return cost - ret;
       
   357 				}
       
   358 				DoCommand(TileXY(x, y), 0, 0, flags, CMD_LANDSCAPE_CLEAR);
       
   359 
       
   360 				// draw explosion animation...
       
   361 				if ((x == sx || x == ex) && (y == sy || y == ey)) {
       
   362 					// big explosion in each corner, or small explosion for single tiles
       
   363 					CreateEffectVehicleAbove(x * TILE_SIZE + TILE_SIZE / 2, y * TILE_SIZE + TILE_SIZE / 2, 2,
       
   364 						sy == ey && sx == ex ? EV_EXPLOSION_SMALL : EV_EXPLOSION_LARGE
       
   365 					);
       
   366 				}
       
   367 			}
       
   368 		}
       
   369 	}
       
   370 
       
   371 	return (success) ? cost : CMD_ERROR;
       
   372 }
       
   373 
       
   374 
       
   375 #define TILELOOP_BITS 4
       
   376 #define TILELOOP_SIZE (1 << TILELOOP_BITS)
       
   377 #define TILELOOP_ASSERTMASK ((TILELOOP_SIZE-1) + ((TILELOOP_SIZE-1) << MapLogX()))
       
   378 #define TILELOOP_CHKMASK (((1 << (MapLogX() - TILELOOP_BITS))-1) << TILELOOP_BITS)
       
   379 
       
   380 void RunTileLoop(void)
       
   381 {
       
   382 	TileIndex tile;
       
   383 	uint count;
       
   384 
       
   385 	tile = _cur_tileloop_tile;
       
   386 
       
   387 	assert( (tile & ~TILELOOP_ASSERTMASK) == 0);
       
   388 	count = (MapSizeX() / TILELOOP_SIZE) * (MapSizeY() / TILELOOP_SIZE);
       
   389 	do {
       
   390 		_tile_type_procs[GetTileType(tile)]->tile_loop_proc(tile);
       
   391 
       
   392 		if (TileX(tile) < MapSizeX() - TILELOOP_SIZE) {
       
   393 			tile += TILELOOP_SIZE; /* no overflow */
       
   394 		} else {
       
   395 			tile = TILE_MASK(tile - TILELOOP_SIZE * (MapSizeX() / TILELOOP_SIZE - 1) + TileDiffXY(0, TILELOOP_SIZE)); /* x would overflow, also increase y */
       
   396 		}
       
   397 	} while (--count);
       
   398 	assert( (tile & ~TILELOOP_ASSERTMASK) == 0);
       
   399 
       
   400 	tile += 9;
       
   401 	if (tile & TILELOOP_CHKMASK)
       
   402 		tile = (tile + MapSizeX()) & TILELOOP_ASSERTMASK;
       
   403 	_cur_tileloop_tile = tile;
       
   404 }
       
   405 
       
   406 void InitializeLandscape(void)
       
   407 {
       
   408 	uint maxx = MapMaxX();
       
   409 	uint maxy = MapMaxY();
       
   410 	uint sizex = MapSizeX();
       
   411 	uint x;
       
   412 	uint y;
       
   413 
       
   414 	for (y = 0; y < maxy; y++) {
       
   415 		for (x = 0; x < maxx; x++) {
       
   416 			MakeClear(sizex * y + x, CLEAR_GRASS, 3);
       
   417 			SetTileHeight(sizex * y + x, 0);
       
   418 			_m[sizex * y + x].extra = 0;
       
   419 			ClearBridgeMiddle(sizex * y + x);
       
   420 		}
       
   421 		MakeVoid(sizex * y + x);
       
   422 	}
       
   423 	for (x = 0; x < sizex; x++) MakeVoid(sizex * y + x);
       
   424 }
       
   425 
       
   426 void ConvertGroundTilesIntoWaterTiles(void)
       
   427 {
       
   428 	TileIndex tile;
       
   429 	uint z;
       
   430 	Slope slope;
       
   431 
       
   432 	for (tile = 0; tile < MapSize(); ++tile) {
       
   433 		slope = GetTileSlope(tile, &z);
       
   434 		if (IsTileType(tile, MP_CLEAR) && z == 0) {
       
   435 			/* Make both water for tiles at level 0
       
   436 			 * and make shore, as that looks much better
       
   437 			 * during the generation. */
       
   438 			switch (slope) {
       
   439 				case SLOPE_FLAT:
       
   440 					MakeWater(tile);
       
   441 					break;
       
   442 
       
   443 				case SLOPE_N:
       
   444 				case SLOPE_E:
       
   445 				case SLOPE_S:
       
   446 				case SLOPE_W:
       
   447 				case SLOPE_NW:
       
   448 				case SLOPE_SW:
       
   449 				case SLOPE_SE:
       
   450 				case SLOPE_NE:
       
   451 					MakeShore(tile);
       
   452 					break;
       
   453 
       
   454 				default:
       
   455 					break;
       
   456 			}
       
   457 		}
       
   458 	}
       
   459 }
       
   460 
       
   461 static const byte _genterrain_tbl_1[5] = { 10, 22, 33, 37, 4  };
       
   462 static const byte _genterrain_tbl_2[5] = {  0,  0,  0,  0, 33 };
       
   463 
       
   464 static void GenerateTerrain(int type, int flag)
       
   465 {
       
   466 	uint32 r;
       
   467 	uint x;
       
   468 	uint y;
       
   469 	uint w;
       
   470 	uint h;
       
   471 	const Sprite* template;
       
   472 	const byte *p;
       
   473 	Tile* tile;
       
   474 	byte direction;
       
   475 
       
   476 	r = Random();
       
   477 	template = GetSprite((((r >> 24) * _genterrain_tbl_1[type]) >> 8) + _genterrain_tbl_2[type] + 4845);
       
   478 
       
   479 	x = r & MapMaxX();
       
   480 	y = (r >> MapLogX()) & MapMaxY();
       
   481 
       
   482 
       
   483 	if (x < 2 || y < 2) return;
       
   484 
       
   485 	direction = GB(r, 22, 2);
       
   486 	if (direction & 1) {
       
   487 		w = template->height;
       
   488 		h = template->width;
       
   489 	} else {
       
   490 		w = template->width;
       
   491 		h = template->height;
       
   492 	}
       
   493 	p = template->data;
       
   494 
       
   495 	if (flag & 4) {
       
   496 		uint xw = x * MapSizeY();
       
   497 		uint yw = y * MapSizeX();
       
   498 		uint bias = (MapSizeX() + MapSizeY()) * 16;
       
   499 
       
   500 		switch (flag & 3) {
       
   501 			case 0:
       
   502 				if (xw + yw > MapSize() - bias) return;
       
   503 				break;
       
   504 
       
   505 			case 1:
       
   506 				if (yw < xw + bias) return;
       
   507 				break;
       
   508 
       
   509 			case 2:
       
   510 				if (xw + yw < MapSize() + bias) return;
       
   511 				break;
       
   512 
       
   513 			case 3:
       
   514 				if (xw < yw + bias) return;
       
   515 				break;
       
   516 		}
       
   517 	}
       
   518 
       
   519 	if (x + w >= MapMaxX() - 1) return;
       
   520 	if (y + h >= MapMaxY() - 1) return;
       
   521 
       
   522 	tile = &_m[TileXY(x, y)];
       
   523 
       
   524 	switch (direction) {
       
   525 		case 0:
       
   526 			do {
       
   527 				Tile* tile_cur = tile;
       
   528 				uint w_cur;
       
   529 
       
   530 				for (w_cur = w; w_cur != 0; --w_cur) {
       
   531 					if (*p >= tile_cur->type_height) tile_cur->type_height = *p;
       
   532 					p++;
       
   533 					tile_cur++;
       
   534 				}
       
   535 				tile += TileDiffXY(0, 1);
       
   536 			} while (--h != 0);
       
   537 			break;
       
   538 
       
   539 		case 1:
       
   540 			do {
       
   541 				Tile* tile_cur = tile;
       
   542 				uint h_cur;
       
   543 
       
   544 				for (h_cur = h; h_cur != 0; --h_cur) {
       
   545 					if (*p >= tile_cur->type_height) tile_cur->type_height = *p;
       
   546 					p++;
       
   547 					tile_cur += TileDiffXY(0, 1);
       
   548 				}
       
   549 				tile++;
       
   550 			} while (--w != 0);
       
   551 			break;
       
   552 
       
   553 		case 2:
       
   554 			tile += TileDiffXY(w - 1, 0);
       
   555 			do {
       
   556 				Tile* tile_cur = tile;
       
   557 				uint w_cur;
       
   558 
       
   559 				for (w_cur = w; w_cur != 0; --w_cur) {
       
   560 					if (*p >= tile_cur->type_height) tile_cur->type_height = *p;
       
   561 					p++;
       
   562 					tile_cur--;
       
   563 				}
       
   564 				tile += TileDiffXY(0, 1);
       
   565 			} while (--h != 0);
       
   566 			break;
       
   567 
       
   568 		case 3:
       
   569 			tile += TileDiffXY(0, h - 1);
       
   570 			do {
       
   571 				Tile* tile_cur = tile;
       
   572 				uint h_cur;
       
   573 
       
   574 				for (h_cur = h; h_cur != 0; --h_cur) {
       
   575 					if (*p >= tile_cur->type_height) tile_cur->type_height = *p;
       
   576 					p++;
       
   577 					tile_cur -= TileDiffXY(0, 1);
       
   578 				}
       
   579 				tile++;
       
   580 			} while (--w != 0);
       
   581 			break;
       
   582 	}
       
   583 }
       
   584 
       
   585 
       
   586 #include "table/genland.h"
       
   587 
       
   588 static void CreateDesertOrRainForest(void)
       
   589 {
       
   590 	TileIndex tile;
       
   591 	TileIndex update_freq = MapSize() / 4;
       
   592 	const TileIndexDiffC *data;
       
   593 	uint i;
       
   594 
       
   595 	for (tile = 0; tile != MapSize(); ++tile) {
       
   596 		if ((tile % update_freq) == 0) IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
       
   597 
       
   598 		for (data = _make_desert_or_rainforest_data;
       
   599 				data != endof(_make_desert_or_rainforest_data); ++data) {
       
   600 			TileIndex t = TILE_MASK(tile + ToTileIndexDiff(*data));
       
   601 			if (TileHeight(t) >= 4 || IsTileType(t, MP_WATER)) break;
       
   602 		}
       
   603 		if (data == endof(_make_desert_or_rainforest_data))
       
   604 			SetTropicZone(tile, TROPICZONE_DESERT);
       
   605 	}
       
   606 
       
   607 	for (i = 0; i != 256; i++) {
       
   608 		if ((i % 64) == 0) IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
       
   609 
       
   610 		RunTileLoop();
       
   611 	}
       
   612 
       
   613 	for (tile = 0; tile != MapSize(); ++tile) {
       
   614 		if ((tile % update_freq) == 0) IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
       
   615 
       
   616 		for (data = _make_desert_or_rainforest_data;
       
   617 				data != endof(_make_desert_or_rainforest_data); ++data) {
       
   618 			TileIndex t = TILE_MASK(tile + ToTileIndexDiff(*data));
       
   619 			if (IsTileType(t, MP_CLEAR) && IsClearGround(t, CLEAR_DESERT)) break;
       
   620 		}
       
   621 		if (data == endof(_make_desert_or_rainforest_data))
       
   622 			SetTropicZone(tile, TROPICZONE_RAINFOREST);
       
   623 	}
       
   624 }
       
   625 
       
   626 void GenerateLandscape(byte mode)
       
   627 {
       
   628 	const int gwp_desert_amount = 4 + 8;
       
   629 	uint i;
       
   630 	uint flag;
       
   631 	uint32 r;
       
   632 
       
   633 	if (mode == GW_HEIGHTMAP) {
       
   634 		SetGeneratingWorldProgress(GWP_LANDSCAPE, (_opt.landscape == LT_DESERT) ? 1 + gwp_desert_amount : 1);
       
   635 		LoadHeightmap(_file_to_saveload.name);
       
   636 		IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
       
   637 	} else if (_patches.land_generator == LG_TERRAGENESIS) {
       
   638 		SetGeneratingWorldProgress(GWP_LANDSCAPE, (_opt.landscape == LT_DESERT) ? 3 + gwp_desert_amount : 3);
       
   639 		GenerateTerrainPerlin();
       
   640 	} else {
       
   641 		switch (_opt.landscape) {
       
   642 			case LT_HILLY:
       
   643 				SetGeneratingWorldProgress(GWP_LANDSCAPE, 2);
       
   644 
       
   645 				for (i = ScaleByMapSize((Random() & 0x7F) + 950); i != 0; --i) {
       
   646 					GenerateTerrain(2, 0);
       
   647 				}
       
   648 				IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
       
   649 
       
   650 				r = Random();
       
   651 				flag = GB(r, 0, 2) | 4;
       
   652 				for (i = ScaleByMapSize(GB(r, 16, 7) + 450); i != 0; --i) {
       
   653 					GenerateTerrain(4, flag);
       
   654 				}
       
   655 				IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
       
   656 				break;
       
   657 
       
   658 			case LT_DESERT:
       
   659 				SetGeneratingWorldProgress(GWP_LANDSCAPE, 3 + gwp_desert_amount);
       
   660 
       
   661 				for (i = ScaleByMapSize((Random() & 0x7F) + 170); i != 0; --i) {
       
   662 					GenerateTerrain(0, 0);
       
   663 				}
       
   664 				IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
       
   665 
       
   666 				r = Random();
       
   667 				flag = GB(r, 0, 2) | 4;
       
   668 				for (i = ScaleByMapSize(GB(r, 16, 8) + 1700); i != 0; --i) {
       
   669 					GenerateTerrain(0, flag);
       
   670 				}
       
   671 				IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
       
   672 
       
   673 				flag ^= 2;
       
   674 
       
   675 				for (i = ScaleByMapSize((Random() & 0x7F) + 410); i != 0; --i) {
       
   676 					GenerateTerrain(3, flag);
       
   677 				}
       
   678 				IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
       
   679 				break;
       
   680 
       
   681 			default:
       
   682 				SetGeneratingWorldProgress(GWP_LANDSCAPE, 1);
       
   683 
       
   684 				i = ScaleByMapSize((Random() & 0x7F) + (3 - _opt.diff.quantity_sea_lakes) * 256 + 100);
       
   685 				for (; i != 0; --i) {
       
   686 					GenerateTerrain(_opt.diff.terrain_type, 0);
       
   687 				}
       
   688 				IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
       
   689 				break;
       
   690 		}
       
   691 	}
       
   692 
       
   693 	ConvertGroundTilesIntoWaterTiles();
       
   694 
       
   695 	if (_opt.landscape == LT_DESERT) CreateDesertOrRainForest();
       
   696 }
       
   697 
       
   698 void OnTick_Town(void);
       
   699 void OnTick_Trees(void);
       
   700 void OnTick_Station(void);
       
   701 void OnTick_Industry(void);
       
   702 
       
   703 void OnTick_Players(void);
       
   704 void OnTick_Train(void);
       
   705 
       
   706 void CallLandscapeTick(void)
       
   707 {
       
   708 	OnTick_Town();
       
   709 	OnTick_Trees();
       
   710 	OnTick_Station();
       
   711 	OnTick_Industry();
       
   712 
       
   713 	OnTick_Players();
       
   714 	OnTick_Train();
       
   715 }
       
   716 
       
   717 TileIndex AdjustTileCoordRandomly(TileIndex a, byte rng)
       
   718 {
       
   719 	int rn = rng;
       
   720 	uint32 r = Random();
       
   721 
       
   722 	return TILE_MASK(TileXY(
       
   723 		TileX(a) + (GB(r, 0, 8) * rn * 2 >> 8) - rn,
       
   724 		TileY(a) + (GB(r, 8, 8) * rn * 2 >> 8) - rn
       
   725 	));
       
   726 }
       
   727 
       
   728 bool IsValidTile(TileIndex tile)
       
   729 {
       
   730 	return (tile < MapSizeX() * MapMaxY() && TileX(tile) != MapMaxX());
       
   731 }