(svn r25849) -Codechange: Introduce IsTileFlat to not compute full slope information for situations when we only want to know if a tile is flat or not (cirdan, LordAro)

This commit is contained in:
zuu 2013-10-12 22:07:58 +00:00
parent b35b8aa5bb
commit dfb5663313
11 changed files with 51 additions and 27 deletions

View File

@ -249,8 +249,7 @@ static void TileLoop_Clear(TileIndex tile)
/* If the tile is at any edge flood it to prevent maps without water. */ /* If the tile is at any edge flood it to prevent maps without water. */
if (_settings_game.construction.freeform_edges && DistanceFromEdge(tile) == 1) { if (_settings_game.construction.freeform_edges && DistanceFromEdge(tile) == 1) {
int z; int z;
Slope slope = GetTileSlope(tile, &z); if (IsTileFlat(tile, &z) && z == 0) {
if (z == 0 && slope == SLOPE_FLAT) {
DoFloodTile(tile); DoFloodTile(tile);
MarkTileDirtyByTile(tile); MarkTileDirtyByTile(tile);
return; return;

View File

@ -1386,7 +1386,7 @@ static CommandCost CheckIfIndustryTilesAreFree(TileIndex tile, const IndustryTil
if (gfx == GFX_WATERTILE_SPECIALCHECK) { if (gfx == GFX_WATERTILE_SPECIALCHECK) {
if (!IsTileType(cur_tile, MP_WATER) || if (!IsTileType(cur_tile, MP_WATER) ||
GetTileSlope(cur_tile) != SLOPE_FLAT) { !IsTileFlat(cur_tile)) {
return_cmd_error(STR_ERROR_SITE_UNSUITABLE); return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
} }
} else { } else {

View File

@ -937,8 +937,7 @@ static void CreateDesertOrRainForest()
static bool FindSpring(TileIndex tile, void *user_data) static bool FindSpring(TileIndex tile, void *user_data)
{ {
int referenceHeight; int referenceHeight;
Slope s = GetTileSlope(tile, &referenceHeight); if (!IsTileFlat(tile, &referenceHeight) || IsWaterTile(tile)) return false;
if (s != SLOPE_FLAT || IsWaterTile(tile)) return false;
/* In the tropics rivers start in the rainforest. */ /* In the tropics rivers start in the rainforest. */
if (_settings_game.game_creation.landscape == LT_TROPIC && GetTropicZone(tile) != TROPICZONE_RAINFOREST) return false; if (_settings_game.game_creation.landscape == LT_TROPIC && GetTropicZone(tile) != TROPICZONE_RAINFOREST) return false;
@ -974,7 +973,7 @@ static bool FindSpring(TileIndex tile, void *user_data)
static bool MakeLake(TileIndex tile, void *user_data) static bool MakeLake(TileIndex tile, void *user_data)
{ {
uint height = *(uint*)user_data; uint height = *(uint*)user_data;
if (!IsValidTile(tile) || TileHeight(tile) != height || GetTileSlope(tile) != SLOPE_FLAT) return false; if (!IsValidTile(tile) || TileHeight(tile) != height || !IsTileFlat(tile)) return false;
if (_settings_game.game_creation.landscape == LT_TROPIC && GetTropicZone(tile) == TROPICZONE_DESERT) return false; if (_settings_game.game_creation.landscape == LT_TROPIC && GetTropicZone(tile) == TROPICZONE_DESERT) return false;
for (DiagDirection d = DIAGDIR_BEGIN; d < DIAGDIR_END; d++) { for (DiagDirection d = DIAGDIR_BEGIN; d < DIAGDIR_END; d++) {
@ -1123,7 +1122,7 @@ static bool FlowRiver(bool *marks, TileIndex spring, TileIndex begin)
queue.pop_front(); queue.pop_front();
uint height2 = TileHeight(end); uint height2 = TileHeight(end);
if (GetTileSlope(end) == SLOPE_FLAT && (height2 < height || (height2 == height && IsWaterTile(end)))) { if (IsTileFlat(end) && (height2 < height || (height2 == height && IsWaterTile(end)))) {
found = true; found = true;
break; break;
} }
@ -1150,7 +1149,7 @@ static bool FlowRiver(bool *marks, TileIndex spring, TileIndex begin)
if (IsValidTile(lakeCenter) && if (IsValidTile(lakeCenter) &&
/* A river, or lake, can only be built on flat slopes. */ /* A river, or lake, can only be built on flat slopes. */
GetTileSlope(lakeCenter) == SLOPE_FLAT && IsTileFlat(lakeCenter) &&
/* We want the lake to be built at the height of the river. */ /* We want the lake to be built at the height of the river. */
TileHeight(begin) == TileHeight(lakeCenter) && TileHeight(begin) == TileHeight(lakeCenter) &&
/* We don't want the lake at the entry of the valley. */ /* We don't want the lake at the entry of the valley. */

View File

@ -303,7 +303,7 @@ CommandCost CmdBuildObject(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
switch (type) { switch (type) {
case OBJECT_TRANSMITTER: case OBJECT_TRANSMITTER:
case OBJECT_LIGHTHOUSE: case OBJECT_LIGHTHOUSE:
if (GetTileSlope(tile) != SLOPE_FLAT) return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED); if (!IsTileFlat(tile)) return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
break; break;
case OBJECT_OWNED_LAND: case OBJECT_OWNED_LAND:
@ -673,7 +673,7 @@ void GenerateObjects()
TileIndex tile = RandomTile(); TileIndex tile = RandomTile();
int h; int h;
if (IsTileType(tile, MP_CLEAR) && GetTileSlope(tile, &h) == SLOPE_FLAT && h >= 4 && !IsBridgeAbove(tile)) { if (IsTileType(tile, MP_CLEAR) && IsTileFlat(tile, &h) && h >= 4 && !IsBridgeAbove(tile)) {
TileIndex t = tile; TileIndex t = tile;
if (CircularTileSearch(&t, 9, HasTransmitter, NULL)) continue; if (CircularTileSearch(&t, 9, HasTransmitter, NULL)) continue;
@ -710,7 +710,7 @@ void GenerateObjects()
for (int j = 0; j < 19; j++) { for (int j = 0; j < 19; j++) {
int h; int h;
if (IsTileType(tile, MP_CLEAR) && GetTileSlope(tile, &h) == SLOPE_FLAT && h <= 2 && !IsBridgeAbove(tile)) { if (IsTileType(tile, MP_CLEAR) && IsTileFlat(tile, &h) && h <= 2 && !IsBridgeAbove(tile)) {
BuildObject(OBJECT_LIGHTHOUSE, tile); BuildObject(OBJECT_LIGHTHOUSE, tile);
IncreaseGeneratingWorldProgress(GWP_OBJECT); IncreaseGeneratingWorldProgress(GWP_OBJECT);
lighthouses_to_build--; lighthouses_to_build--;

View File

@ -74,7 +74,7 @@ void SetWaterClassDependingOnSurroundings(TileIndex t, bool include_invalid_wate
{ {
/* If the slope is not flat, we always assume 'land' (if allowed). Also for one-corner-raised-shores. /* If the slope is not flat, we always assume 'land' (if allowed). Also for one-corner-raised-shores.
* Note: Wrt. autosloping under industry tiles this is the most fool-proof behaviour. */ * Note: Wrt. autosloping under industry tiles this is the most fool-proof behaviour. */
if (GetTileSlope(t) != SLOPE_FLAT) { if (!IsTileFlat(t)) {
if (include_invalid_water_class) { if (include_invalid_water_class) {
SetWaterClass(t, WATER_CLASS_INVALID); SetWaterClass(t, WATER_CLASS_INVALID);
return; return;
@ -1106,7 +1106,7 @@ bool AfterLoadGame()
if (GB(_m[t].m5, 3, 2) == 0) { if (GB(_m[t].m5, 3, 2) == 0) {
MakeClear(t, CLEAR_GRASS, 3); MakeClear(t, CLEAR_GRASS, 3);
} else { } else {
if (GetTileSlope(t) != SLOPE_FLAT) { if (!IsTileFlat(t)) {
MakeShore(t); MakeShore(t);
} else { } else {
if (GetTileOwner(t) == OWNER_WATER) { if (GetTileOwner(t) == OWNER_WATER) {
@ -1712,7 +1712,7 @@ bool AfterLoadGame()
* on its neighbouring tiles. Done after river and canal updates to * on its neighbouring tiles. Done after river and canal updates to
* ensure neighbours are correct. */ * ensure neighbours are correct. */
for (TileIndex t = 0; t < map_size; t++) { for (TileIndex t = 0; t < map_size; t++) {
if (GetTileSlope(t) != SLOPE_FLAT) continue; if (!IsTileFlat(t)) continue;
if (IsTileType(t, MP_WATER) && IsLock(t)) SetWaterClassDependingOnSurroundings(t, false); if (IsTileType(t, MP_WATER) && IsLock(t)) SetWaterClassDependingOnSurroundings(t, false);
if (IsTileType(t, MP_STATION) && (IsDock(t) || IsBuoy(t))) SetWaterClassDependingOnSurroundings(t, false); if (IsTileType(t, MP_STATION) && (IsDock(t) || IsBuoy(t))) SetWaterClassDependingOnSurroundings(t, false);
@ -2390,7 +2390,7 @@ bool AfterLoadGame()
if (IsSavegameVersionBefore(149)) { if (IsSavegameVersionBefore(149)) {
for (TileIndex t = 0; t < map_size; t++) { for (TileIndex t = 0; t < map_size; t++) {
if (!IsTileType(t, MP_STATION)) continue; if (!IsTileType(t, MP_STATION)) continue;
if (!IsBuoy(t) && !IsOilRig(t) && !(IsDock(t) && GetTileSlope(t) == SLOPE_FLAT)) { if (!IsBuoy(t) && !IsOilRig(t) && !(IsDock(t) && IsTileFlat(t))) {
SetWaterClass(t, WATER_CLASS_INVALID); SetWaterClass(t, WATER_CLASS_INVALID);
} }
} }

View File

@ -2471,7 +2471,7 @@ CommandCost CmdBuildDock(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32
TileIndex tile_cur = tile + TileOffsByDiagDir(direction); TileIndex tile_cur = tile + TileOffsByDiagDir(direction);
if (!IsTileType(tile_cur, MP_WATER) || GetTileSlope(tile_cur) != SLOPE_FLAT) { if (!IsTileType(tile_cur, MP_WATER) || !IsTileFlat(tile_cur)) {
return_cmd_error(STR_ERROR_SITE_UNSUITABLE); return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
} }
@ -2484,7 +2484,7 @@ CommandCost CmdBuildDock(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32
if (ret.Failed()) return ret; if (ret.Failed()) return ret;
tile_cur += TileOffsByDiagDir(direction); tile_cur += TileOffsByDiagDir(direction);
if (!IsTileType(tile_cur, MP_WATER) || GetTileSlope(tile_cur) != SLOPE_FLAT) { if (!IsTileType(tile_cur, MP_WATER) || !IsTileFlat(tile_cur)) {
return_cmd_error(STR_ERROR_SITE_UNSUITABLE); return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
} }
@ -3069,7 +3069,7 @@ static void TileLoop_Station(TileIndex tile)
break; break;
case STATION_DOCK: case STATION_DOCK:
if (GetTileSlope(tile) != SLOPE_FLAT) break; // only handle water part if (!IsTileFlat(tile)) break; // only handle water part
/* FALL THROUGH */ /* FALL THROUGH */
case STATION_OILRIG: //(station part) case STATION_OILRIG: //(station part)
case STATION_BUOY: case STATION_BUOY:

View File

@ -58,6 +58,30 @@ Slope GetTileSlope(TileIndex tile, int *h)
return (Slope)r; return (Slope)r;
} }
/**
* Check if a given tile is flat
* @param tile Tile to check
* @param h If not \c NULL, pointer to storage of z height (only if tile is flat)
* @return Whether the tile is flat
*/
bool IsTileFlat(TileIndex tile, int *h)
{
assert(tile < MapSize());
if (!IsInnerTile(tile)) {
if (h != NULL) *h = TileHeight(tile);
return true;
}
uint z = TileHeight(tile);
if (TileHeight(tile + TileDiffXY(1, 0)) != z) return false;
if (TileHeight(tile + TileDiffXY(0, 1)) != z) return false;
if (TileHeight(tile + TileDiffXY(1, 1)) != z) return false;
if (h != NULL) *h = z;
return true;
}
/** /**
* Get bottom height of the tile * Get bottom height of the tile
* @param tile Tile to compute height of * @param tile Tile to compute height of

View File

@ -247,6 +247,8 @@ Slope GetTileSlope(TileIndex tile, int *h = NULL);
int GetTileZ(TileIndex tile); int GetTileZ(TileIndex tile);
int GetTileMaxZ(TileIndex tile); int GetTileMaxZ(TileIndex tile);
bool IsTileFlat(TileIndex tile, int *h = NULL);
/** /**
* Return the slope of a given tile * Return the slope of a given tile
* @param tile Tile to compute slope of * @param tile Tile to compute slope of

View File

@ -1411,7 +1411,7 @@ static bool GrowTown(Town *t)
tile = t->xy; tile = t->xy;
for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) { for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) {
/* Only work with plain land that not already has a house */ /* Only work with plain land that not already has a house */
if (!IsTileType(tile, MP_HOUSE) && GetTileSlope(tile) == SLOPE_FLAT) { if (!IsTileType(tile, MP_HOUSE) && IsTileFlat(tile)) {
if (DoCommand(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR).Succeeded()) { if (DoCommand(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR).Succeeded()) {
DoCommand(tile, GenRandomRoadBits(), t->index, DC_EXEC | DC_AUTO, CMD_BUILD_ROAD); DoCommand(tile, GenRandomRoadBits(), t->index, DC_EXEC | DC_AUTO, CMD_BUILD_ROAD);
cur_company.Restore(); cur_company.Restore();
@ -1574,7 +1574,7 @@ static CommandCost TownCanBePlacedHere(TileIndex tile)
} }
/* Can only build on clear flat areas, possibly with trees. */ /* Can only build on clear flat areas, possibly with trees. */
if ((!IsTileType(tile, MP_CLEAR) && !IsTileType(tile, MP_TREES)) || GetTileSlope(tile) != SLOPE_FLAT) { if ((!IsTileType(tile, MP_CLEAR) && !IsTileType(tile, MP_TREES)) || !IsTileFlat(tile)) {
return_cmd_error(STR_ERROR_SITE_UNSUITABLE); return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
} }
@ -1775,7 +1775,7 @@ static bool FindFurthestFromWater(TileIndex tile, void *user_data)
uint dist = GetClosestWaterDistance(tile, true); uint dist = GetClosestWaterDistance(tile, true);
if (IsTileType(tile, MP_CLEAR) && if (IsTileType(tile, MP_CLEAR) &&
GetTileSlope(tile) == SLOPE_FLAT && IsTileFlat(tile) &&
IsTileAlignedToGrid(tile, sp->layout) && IsTileAlignedToGrid(tile, sp->layout) &&
dist > sp->max_dist) { dist > sp->max_dist) {
sp->tile = tile; sp->tile = tile;

View File

@ -110,7 +110,7 @@ CommandCost CmdBuildShipDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, ui
if ((MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) || if ((MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) ||
(MayHaveBridgeAbove(tile2) && IsBridgeAbove(tile2))) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST); (MayHaveBridgeAbove(tile2) && IsBridgeAbove(tile2))) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
if (GetTileSlope(tile) != SLOPE_FLAT || GetTileSlope(tile2) != SLOPE_FLAT) { if (!IsTileFlat(tile) || !IsTileFlat(tile2)) {
/* Prevent depots on rapids */ /* Prevent depots on rapids */
return_cmd_error(STR_ERROR_SITE_UNSUITABLE); return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
} }
@ -270,7 +270,7 @@ static CommandCost DoBuildLock(TileIndex tile, DiagDirection dir, DoCommandFlag
cost.AddCost(ret); cost.AddCost(ret);
cost.AddCost(_price[PR_BUILD_CANAL]); cost.AddCost(_price[PR_BUILD_CANAL]);
} }
if (GetTileSlope(tile - delta) != SLOPE_FLAT) { if (!IsTileFlat(tile - delta)) {
return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION); return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
} }
@ -283,7 +283,7 @@ static CommandCost DoBuildLock(TileIndex tile, DiagDirection dir, DoCommandFlag
cost.AddCost(ret); cost.AddCost(ret);
cost.AddCost(_price[PR_BUILD_CANAL]); cost.AddCost(_price[PR_BUILD_CANAL]);
} }
if (GetTileSlope(tile + delta) != SLOPE_FLAT) { if (!IsTileFlat(tile + delta)) {
return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION); return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
} }
@ -590,7 +590,7 @@ bool IsWateredTile(TileIndex tile, Direction from)
return IsTileOnWater(tile); return IsTileOnWater(tile);
} }
return (IsDock(tile) && GetTileSlope(tile) == SLOPE_FLAT) || IsBuoy(tile); return (IsDock(tile) && IsTileFlat(tile)) || IsBuoy(tile);
case MP_INDUSTRY: { case MP_INDUSTRY: {
/* Do not draw waterborders inside of industries. /* Do not draw waterborders inside of industries.
@ -1240,7 +1240,7 @@ static TrackStatus GetTileTrackStatus_Water(TileIndex tile, TransportType mode,
if (mode != TRANSPORT_WATER) return 0; if (mode != TRANSPORT_WATER) return 0;
switch (GetWaterTileType(tile)) { switch (GetWaterTileType(tile)) {
case WATER_TILE_CLEAR: ts = (GetTileSlope(tile) == SLOPE_FLAT) ? TRACK_BIT_ALL : TRACK_BIT_NONE; break; case WATER_TILE_CLEAR: ts = IsTileFlat(tile) ? TRACK_BIT_ALL : TRACK_BIT_NONE; break;
case WATER_TILE_COAST: ts = (TrackBits)coast_tracks[GetTileSlope(tile) & 0xF]; break; case WATER_TILE_COAST: ts = (TrackBits)coast_tracks[GetTileSlope(tile) & 0xF]; break;
case WATER_TILE_LOCK: ts = DiagDirToDiagTrackBits(GetLockDirection(tile)); break; case WATER_TILE_LOCK: ts = DiagDirToDiagTrackBits(GetLockDirection(tile)); break;
case WATER_TILE_DEPOT: ts = AxisToTrackBits(GetShipDepotAxis(tile)); break; case WATER_TILE_DEPOT: ts = AxisToTrackBits(GetShipDepotAxis(tile)); break;

View File

@ -289,7 +289,7 @@ CommandCost CmdBuildBuoy(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32
if (tile == 0 || !HasTileWaterGround(tile)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE); if (tile == 0 || !HasTileWaterGround(tile)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST); if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
if (GetTileSlope(tile) != SLOPE_FLAT) return_cmd_error(STR_ERROR_SITE_UNSUITABLE); if (!IsTileFlat(tile)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
/* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */ /* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */
Waypoint *wp = FindDeletedWaypointCloseTo(tile, STR_SV_STNAME_BUOY, OWNER_NONE); Waypoint *wp = FindDeletedWaypointCloseTo(tile, STR_SV_STNAME_BUOY, OWNER_NONE);