1
0
Fork 0

Codechange: Do not assign _grow_town_result in GrowTownInTile, instead return the new value.

pull/14149/head
frosch 2025-05-05 13:11:31 +02:00 committed by frosch
parent 22af391064
commit 5af877d12d
1 changed files with 47 additions and 43 deletions

View File

@ -231,13 +231,6 @@ Money HouseSpec::GetRemovalCost() const
/* Local */ /* Local */
static int _grow_town_result; static int _grow_town_result;
/* The possible states of town growth. */
enum TownGrowthResult {
GROWTH_SUCCEED = -1,
GROWTH_SEARCH_STOPPED = 0
/* GROWTH_SEARCH_RUNNING >= 1 */
};
static bool TryBuildTownHouse(Town *t, TileIndex tile); static bool TryBuildTownHouse(Town *t, TileIndex tile);
static Town *CreateRandomTown(uint attempts, uint32_t townnameparts, TownSize size, bool city, TownLayout layout); static Town *CreateRandomTown(uint attempts, uint32_t townnameparts, TownSize size, bool city, TownLayout layout);
@ -1509,6 +1502,13 @@ static inline bool TownAllowedToBuildRoads()
return _settings_game.economy.allow_town_roads || _generating_world || _game_mode == GM_EDITOR; return _settings_game.economy.allow_town_roads || _generating_world || _game_mode == GM_EDITOR;
} }
/* The possible states of town growth. */
enum class TownGrowthResult {
Succeed,
SearchStopped,
Continue,
};
/** /**
* Grows the given town. * Grows the given town.
* There are at the moment 3 possible way's for * There are at the moment 3 possible way's for
@ -1525,8 +1525,9 @@ static inline bool TownAllowedToBuildRoads()
* @param cur_rb The current tiles RoadBits * @param cur_rb The current tiles RoadBits
* @param target_dir The target road dir * @param target_dir The target road dir
* @param t1 The current town * @param t1 The current town
* @return Result so far.
*/ */
static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection target_dir, Town *t1) static TownGrowthResult GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection target_dir, Town *t1)
{ {
RoadBits rcmd = ROAD_NONE; // RoadBits for the road construction command RoadBits rcmd = ROAD_NONE; // RoadBits for the road construction command
TileIndex tile = *tile_ptr; // The main tile on which we base our growth TileIndex tile = *tile_ptr; // The main tile on which we base our growth
@ -1534,12 +1535,11 @@ static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection t
assert(tile < Map::Size()); assert(tile < Map::Size());
if (cur_rb == ROAD_NONE) { if (cur_rb == ROAD_NONE) {
/* Tile has no road. First reset the status counter /* Tile has no road.
* to say that this is the last iteration. */ * We will return TownGrowthResult::SearchStopped to say that this is the last iteration. */
_grow_town_result = GROWTH_SEARCH_STOPPED;
if (!TownAllowedToBuildRoads()) return; if (!TownAllowedToBuildRoads()) return TownGrowthResult::SearchStopped;
if (!_settings_game.economy.allow_town_level_crossings && IsTileType(tile, MP_RAILWAY)) return; if (!_settings_game.economy.allow_town_level_crossings && IsTileType(tile, MP_RAILWAY)) return TownGrowthResult::SearchStopped;
/* Remove hills etc */ /* Remove hills etc */
if (!_settings_game.construction.build_on_slopes || Chance16(1, 6)) LevelTownLand(tile); if (!_settings_game.construction.build_on_slopes || Chance16(1, 6)) LevelTownLand(tile);
@ -1551,12 +1551,12 @@ static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection t
case TL_3X3_GRID: case TL_3X3_GRID:
case TL_2X2_GRID: case TL_2X2_GRID:
rcmd = GetTownRoadGridElement(t1, tile, target_dir); rcmd = GetTownRoadGridElement(t1, tile, target_dir);
if (rcmd == ROAD_NONE) return; if (rcmd == ROAD_NONE) return TownGrowthResult::SearchStopped;
break; break;
case TL_BETTER_ROADS: case TL_BETTER_ROADS:
case TL_ORIGINAL: case TL_ORIGINAL:
if (!IsRoadAllowedHere(t1, tile, target_dir)) return; if (!IsRoadAllowedHere(t1, tile, target_dir)) return TownGrowthResult::SearchStopped;
DiagDirection source_dir = ReverseDiagDir(target_dir); DiagDirection source_dir = ReverseDiagDir(target_dir);
@ -1568,12 +1568,12 @@ static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection t
if (!IsRoadAllowedHere(t1, TileAddByDiagDir(tile, target_dir), target_dir)) { if (!IsRoadAllowedHere(t1, TileAddByDiagDir(tile, target_dir), target_dir)) {
/* A road is not allowed to continue the randomized road, /* A road is not allowed to continue the randomized road,
* return if the road we're trying to build is curved. */ * return if the road we're trying to build is curved. */
if (target_dir != ReverseDiagDir(source_dir)) return; if (target_dir != ReverseDiagDir(source_dir)) return TownGrowthResult::SearchStopped;
/* Return if neither side of the new road is a house */ /* Return if neither side of the new road is a house */
if (!IsTileType(TileAddByDiagDir(tile, ChangeDiagDir(target_dir, DIAGDIRDIFF_90RIGHT)), MP_HOUSE) && if (!IsTileType(TileAddByDiagDir(tile, ChangeDiagDir(target_dir, DIAGDIRDIFF_90RIGHT)), MP_HOUSE) &&
!IsTileType(TileAddByDiagDir(tile, ChangeDiagDir(target_dir, DIAGDIRDIFF_90LEFT)), MP_HOUSE)) { !IsTileType(TileAddByDiagDir(tile, ChangeDiagDir(target_dir, DIAGDIRDIFF_90LEFT)), MP_HOUSE)) {
return; return TownGrowthResult::SearchStopped;
} }
/* That means that the road is only allowed if there is a house /* That means that the road is only allowed if there is a house
@ -1585,15 +1585,13 @@ static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection t
} }
} else if (target_dir < DIAGDIR_END && !(cur_rb & DiagDirToRoadBits(ReverseDiagDir(target_dir)))) { } else if (target_dir < DIAGDIR_END && !(cur_rb & DiagDirToRoadBits(ReverseDiagDir(target_dir)))) {
if (!TownCanGrowRoad(tile)) return; if (!TownCanGrowRoad(tile)) return TownGrowthResult::Continue;
if (!TownAllowedToBuildRoads()) return TownGrowthResult::SearchStopped;
/* Continue building on a partial road. /* Continue building on a partial road.
* Should be always OK, so we only generate * Should be always OK, so we only generate
* the fitting RoadBits */ * the fitting RoadBits */
_grow_town_result = GROWTH_SEARCH_STOPPED;
if (!TownAllowedToBuildRoads()) return;
switch (t1->layout) { switch (t1->layout) {
default: NOT_REACHED(); default: NOT_REACHED();
@ -1616,7 +1614,7 @@ static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection t
if (GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD && (target_dir != DIAGDIR_END || Chance16(1, 2))) { if (GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD && (target_dir != DIAGDIR_END || Chance16(1, 2))) {
*tile_ptr = GetOtherTunnelBridgeEnd(tile); *tile_ptr = GetOtherTunnelBridgeEnd(tile);
} }
return; return TownGrowthResult::Continue;
} }
/* Possibly extend the road in a direction. /* Possibly extend the road in a direction.
@ -1633,7 +1631,7 @@ static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection t
* one common bit with a straight road so it has the same * one common bit with a straight road so it has the same
* chance to be chosen as the house on the side of a road. * chance to be chosen as the house on the side of a road.
*/ */
if ((cur_rb & ROAD_X) != target_rb) return; if ((cur_rb & ROAD_X) != target_rb) return TownGrowthResult::Continue;
/* Check whether it is a turn and if so determine /* Check whether it is a turn and if so determine
* position of the corner tile */ * position of the corner tile */
@ -1651,7 +1649,7 @@ static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection t
house_tile = TileAddByDir(tile, DIR_E); house_tile = TileAddByDir(tile, DIR_E);
break; break;
default: default:
return; // not a turn return TownGrowthResult::Continue; // not a turn
} }
target_dir = DIAGDIR_END; target_dir = DIAGDIR_END;
} else { } else {
@ -1659,9 +1657,11 @@ static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection t
} }
/* Don't walk into water. */ /* Don't walk into water. */
if (HasTileWaterGround(house_tile)) return; if (HasTileWaterGround(house_tile)) return TownGrowthResult::Continue;
if (!IsValidTile(house_tile)) return; if (!IsValidTile(house_tile)) return TownGrowthResult::Continue;
TownGrowthResult result = TownGrowthResult::Continue;
if (target_dir != DIAGDIR_END && TownAllowedToBuildRoads()) { if (target_dir != DIAGDIR_END && TownAllowedToBuildRoads()) {
switch (t1->layout) { switch (t1->layout) {
@ -1669,7 +1669,7 @@ static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection t
case TL_3X3_GRID: // Use 2x2 grid afterwards! case TL_3X3_GRID: // Use 2x2 grid afterwards!
if (GrowTownWithExtraHouse(t1, TileAddByDiagDir(house_tile, target_dir))) { if (GrowTownWithExtraHouse(t1, TileAddByDiagDir(house_tile, target_dir))) {
_grow_town_result = GROWTH_SUCCEED; result = TownGrowthResult::Succeed;
} }
[[fallthrough]]; [[fallthrough]];
@ -1680,7 +1680,7 @@ static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection t
case TL_BETTER_ROADS: // Use original afterwards! case TL_BETTER_ROADS: // Use original afterwards!
if (GrowTownWithExtraHouse(t1, TileAddByDiagDir(house_tile, target_dir))) { if (GrowTownWithExtraHouse(t1, TileAddByDiagDir(house_tile, target_dir))) {
_grow_town_result = GROWTH_SUCCEED; result = TownGrowthResult::Succeed;
} }
[[fallthrough]]; [[fallthrough]];
@ -1704,39 +1704,36 @@ static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection t
/* And build a house. /* And build a house.
* Set result to -1 if we managed to build it. */ * Set result to -1 if we managed to build it. */
if (TryBuildTownHouse(t1, house_tile)) { if (TryBuildTownHouse(t1, house_tile)) {
_grow_town_result = GROWTH_SUCCEED; result = TownGrowthResult::Succeed;
} }
} }
return; return result;
} }
if (!TownCanGrowRoad(tile)) return; if (!TownCanGrowRoad(tile)) return result;
_grow_town_result = GROWTH_SEARCH_STOPPED;
} }
/* Return if a water tile */ /* Return if a water tile */
if (HasTileWaterGround(tile)) return; if (HasTileWaterGround(tile)) return TownGrowthResult::SearchStopped;
/* Make the roads look nicer */ /* Make the roads look nicer */
rcmd = CleanUpRoadBits(tile, rcmd); rcmd = CleanUpRoadBits(tile, rcmd);
if (rcmd == ROAD_NONE) return; if (rcmd == ROAD_NONE) return TownGrowthResult::SearchStopped;
/* Only use the target direction for bridges and tunnels to ensure they're connected. /* Only use the target direction for bridges and tunnels to ensure they're connected.
* The target_dir is as computed previously according to town layout, so * The target_dir is as computed previously according to town layout, so
* it will match it perfectly. */ * it will match it perfectly. */
if (GrowTownWithBridge(t1, tile, target_dir)) { if (GrowTownWithBridge(t1, tile, target_dir)) {
_grow_town_result = GROWTH_SUCCEED; return TownGrowthResult::Succeed;
return;
} }
if (GrowTownWithTunnel(t1, tile, target_dir)) { if (GrowTownWithTunnel(t1, tile, target_dir)) {
_grow_town_result = GROWTH_SUCCEED; return TownGrowthResult::Succeed;
return;
} }
if (GrowTownWithRoad(t1, tile, rcmd)) { if (GrowTownWithRoad(t1, tile, rcmd)) {
_grow_town_result = GROWTH_SUCCEED; return TownGrowthResult::Succeed;
} }
return TownGrowthResult::SearchStopped;
} }
/** /**
@ -1819,8 +1816,15 @@ static bool GrowTownAtRoad(Town *t, TileIndex tile)
RoadBits cur_rb = GetTownRoadBits(tile); // The RoadBits of the current tile RoadBits cur_rb = GetTownRoadBits(tile); // The RoadBits of the current tile
/* Try to grow the town from this point */ /* Try to grow the town from this point */
GrowTownInTile(&tile, cur_rb, target_dir, t); switch (GrowTownInTile(&tile, cur_rb, target_dir, t)) {
if (_grow_town_result == GROWTH_SUCCEED) return true; case TownGrowthResult::Succeed:
return true;
case TownGrowthResult::SearchStopped:
_grow_town_result = 0;
break;
default:
break;
};
/* Exclude the source position from the bitmask /* Exclude the source position from the bitmask
* and return if no more road blocks available */ * and return if no more road blocks available */