MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / FixSlopes

Function FixSlopes

src/heightmap.cpp:403–470  ·  view source on GitHub ↗

* This function takes care of the fact that land in OpenTTD can never differ * more than 1 in height */

Source from the content-addressed store, hash-verified

401 * more than 1 in height
402 */
403void FixSlopes()
404{
405 uint width, height;
406 int row, col;
407 uint8_t current_height;
408 uint8_t max_height = _settings_game.construction.map_height_limit;
409
410 /* Adjust height difference to maximum one horizontal/vertical change. */
411 width = Map::SizeX();
412 height = Map::SizeY();
413
414 /* Top and left edge */
415 for (row = 0; (uint)row < height; row++) {
416 for (col = 0; (uint)col < width; col++) {
417 current_height = MAX_TILE_HEIGHT;
418 if (col != 0) {
419 /* Find lowest tile; either the top or left one */
420 current_height = TileHeight(TileXY(col - 1, row)); // top edge
421 }
422 if (row != 0) {
423 if (TileHeight(TileXY(col, row - 1)) < current_height) {
424 current_height = TileHeight(TileXY(col, row - 1)); // left edge
425 }
426 }
427
428 /* Does the height differ more than one? */
429 TileIndex tile = TileXY(col, row);
430 if (TileHeight(tile) >= (uint)current_height + 2) {
431 /* Then change the height to be no more than one */
432 SetTileHeight(tile, current_height + 1);
433 /* Height was changed so now there's a chance, more likely at higher altitude, of the
434 * tile turning into rock. */
435 if (IsInnerTile(tile) && RandomRange(max_height) <= current_height) {
436 MakeClear(tile, CLEAR_ROCKS, 3);
437 }
438 }
439 }
440 }
441
442 /* Bottom and right edge */
443 for (row = height - 1; row >= 0; row--) {
444 for (col = width - 1; col >= 0; col--) {
445 current_height = MAX_TILE_HEIGHT;
446 if ((uint)col != width - 1) {
447 /* Find lowest tile; either the bottom and right one */
448 current_height = TileHeight(TileXY(col + 1, row)); // bottom edge
449 }
450
451 if ((uint)row != height - 1) {
452 if (TileHeight(TileXY(col, row + 1)) < current_height) {
453 current_height = TileHeight(TileXY(col, row + 1)); // right edge
454 }
455 }
456
457 /* Does the height differ more than one? */
458 TileIndex tile = TileXY(col, row);
459 if (TileHeight(tile) >= (uint)current_height + 2) {
460 /* Then change the height to be no more than one */

Callers 3

GenerateLandscapeFunction · 0.85
LoadHeightmapFunction · 0.85
FlatEmptyWorldFunction · 0.85

Calls 6

TileHeightFunction · 0.85
TileXYFunction · 0.85
SetTileHeightFunction · 0.85
IsInnerTileFunction · 0.85
RandomRangeFunction · 0.85
MakeClearFunction · 0.85

Tested by

no test coverage detected