Codechange: Replace CircularTileSearch with SpiralTileSequence.
This commit is contained in:
@@ -351,37 +351,6 @@ void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_setti
|
||||
_GenerateWorld();
|
||||
}
|
||||
|
||||
/** Town data imported from JSON files and used to place towns. */
|
||||
struct ExternalTownData {
|
||||
TownID town_id; ///< The TownID of the town in OpenTTD. Not imported, but set during the founding proceess and stored here for convenience.
|
||||
std::string name; ///< The name of the town.
|
||||
uint population; ///< The target population of the town when created in OpenTTD. If input is blank, defaults to 0.
|
||||
bool is_city; ///< Should it be created as a city in OpenTTD? If input is blank, defaults to false.
|
||||
float x_proportion; ///< The X coordinate of the town, as a proportion 0..1 of the maximum X coordinate.
|
||||
float y_proportion; ///< The Y coordinate of the town, as a proportion 0..1 of the maximum Y coordinate.
|
||||
};
|
||||
|
||||
/**
|
||||
* Helper for CircularTileSearch to found a town on or near a given tile.
|
||||
* @param tile The tile to try founding the town upon.
|
||||
* @param user_data The ExternalTownData to attempt to found.
|
||||
* @return True if the town was founded successfully.
|
||||
*/
|
||||
static bool TryFoundTownNearby(TileIndex tile, void *user_data)
|
||||
{
|
||||
ExternalTownData &town = *static_cast<ExternalTownData *>(user_data);
|
||||
std::tuple<CommandCost, Money, TownID> result = Command<CMD_FOUND_TOWN>::Do(DoCommandFlag::Execute, tile, TSZ_SMALL, town.is_city, _settings_game.economy.town_layout, false, 0, town.name);
|
||||
|
||||
TownID id = std::get<TownID>(result);
|
||||
|
||||
/* Check if the command failed. */
|
||||
if (id == TownID::Invalid()) return false;
|
||||
|
||||
/* The command succeeded, send the ID back through user_data. */
|
||||
town.town_id = id;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load town data from _file_to_saveload, place towns at the appropriate locations, and expand them to their target populations.
|
||||
*/
|
||||
@@ -426,7 +395,11 @@ void LoadTownData()
|
||||
|
||||
/* Iterate through towns and attempt to found them. */
|
||||
for (auto &feature : town_data) {
|
||||
ExternalTownData town;
|
||||
std::string name; // The name of the town.
|
||||
uint population; // The target population of the town when created in OpenTTD. If input is blank, defaults to 0.
|
||||
bool is_city; // Should it be created as a city in OpenTTD? If input is blank, defaults to false.
|
||||
float x_proportion; // The X coordinate of the town, as a proportion 0..1 of the maximum X coordinate.
|
||||
float y_proportion; // The Y coordinate of the town, as a proportion 0..1 of the maximum Y coordinate.
|
||||
|
||||
/* Ensure JSON is formatted properly. */
|
||||
if (!feature.is_object()) {
|
||||
@@ -446,56 +419,63 @@ void LoadTownData()
|
||||
!feature.contains("city") || !feature.at("city").is_boolean() ||
|
||||
!feature.contains("x") || !feature.at("x").is_number() ||
|
||||
!feature.contains("y") || !feature.at("y").is_number()) {
|
||||
feature.at("name").get_to(town.name);
|
||||
feature.at("name").get_to(name);
|
||||
ShowErrorMessage(GetEncodedString(STR_TOWN_DATA_ERROR_LOAD_FAILED),
|
||||
GetEncodedString(STR_TOWN_DATA_ERROR_TOWN_FORMATTED_INCORRECTLY, town.name), WL_ERROR);
|
||||
GetEncodedString(STR_TOWN_DATA_ERROR_TOWN_FORMATTED_INCORRECTLY, name), WL_ERROR);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Set town properties. */
|
||||
feature.at("name").get_to(town.name);
|
||||
feature.at("population").get_to(town.population);
|
||||
feature.at("city").get_to(town.is_city);
|
||||
feature.at("name").get_to(name);
|
||||
feature.at("population").get_to(population);
|
||||
feature.at("city").get_to(is_city);
|
||||
|
||||
/* Set town coordinates. */
|
||||
feature.at("x").get_to(town.x_proportion);
|
||||
feature.at("y").get_to(town.y_proportion);
|
||||
feature.at("x").get_to(x_proportion);
|
||||
feature.at("y").get_to(y_proportion);
|
||||
|
||||
/* Check for improper coordinates and warn the player. */
|
||||
if (town.x_proportion <= 0.0f || town.y_proportion <= 0.0f || town.x_proportion >= 1.0f || town.y_proportion >= 1.0f) {
|
||||
if (x_proportion <= 0.0f || y_proportion <= 0.0f || x_proportion >= 1.0f || y_proportion >= 1.0f) {
|
||||
ShowErrorMessage(GetEncodedString(STR_TOWN_DATA_ERROR_LOAD_FAILED),
|
||||
GetEncodedString(STR_TOWN_DATA_ERROR_BAD_COORDINATE, town.name), WL_ERROR);
|
||||
GetEncodedString(STR_TOWN_DATA_ERROR_BAD_COORDINATE, name), WL_ERROR);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Find the target tile for the town. */
|
||||
TileIndex tile;
|
||||
TileIndex target_tile;
|
||||
switch (_settings_game.game_creation.heightmap_rotation) {
|
||||
case HM_CLOCKWISE:
|
||||
/* Tile coordinates align with what we expect. */
|
||||
tile = TileXY(town.x_proportion * Map::MaxX(), town.y_proportion * Map::MaxY());
|
||||
target_tile = TileXY(x_proportion * Map::MaxX(), y_proportion * Map::MaxY());
|
||||
break;
|
||||
case HM_COUNTER_CLOCKWISE:
|
||||
/* Tile coordinates are rotated and must be adjusted. */
|
||||
tile = TileXY((1 - town.y_proportion * Map::MaxX()), town.x_proportion * Map::MaxY());
|
||||
target_tile = TileXY((1 - y_proportion * Map::MaxX()), x_proportion * Map::MaxY());
|
||||
break;
|
||||
default: NOT_REACHED();
|
||||
}
|
||||
|
||||
TownID town_id; // The TownID of the town in OpenTTD. Not imported, but set during the founding proceess and stored here for convenience.
|
||||
/* Try founding on the target tile, and if that doesn't work, find the nearest suitable tile up to 16 tiles away.
|
||||
* The target might be on water, blocked somehow, or on a steep slope that can't be terraformed by the founding command. */
|
||||
TileIndex search_tile = tile;
|
||||
bool success = CircularTileSearch(&search_tile, 16, 0, 0, TryFoundTownNearby, &town);
|
||||
for (auto tile : SpiralTileSequence(target_tile, 16, 0, 0)) {
|
||||
std::tuple<CommandCost, Money, TownID> result = Command<CMD_FOUND_TOWN>::Do(DoCommandFlag::Execute, tile, TSZ_SMALL, is_city, _settings_game.economy.town_layout, false, 0, name);
|
||||
|
||||
town_id = std::get<TownID>(result);
|
||||
|
||||
/* Check if the command succeeded. */
|
||||
if (town_id != TownID::Invalid()) break;
|
||||
}
|
||||
|
||||
/* If we still fail to found the town, we'll create a sign at the intended location and tell the player how many towns we failed to create in an error message.
|
||||
* This allows the player to diagnose a heightmap misalignment, if towns end up in the sea, or place towns manually, if in rough terrain. */
|
||||
if (!success) {
|
||||
Command<CMD_PLACE_SIGN>::Post(tile, town.name);
|
||||
if (town_id == TownID::Invalid()) {
|
||||
Command<CMD_PLACE_SIGN>::Post(target_tile, name);
|
||||
failed_towns++;
|
||||
continue;
|
||||
}
|
||||
|
||||
towns.emplace_back(std::make_pair(Town::Get(town.town_id), town.population));
|
||||
towns.emplace_back(std::make_pair(Town::Get(town_id), population));
|
||||
}
|
||||
|
||||
/* If we couldn't found a town (or multiple), display a message to the player with the number of failed towns. */
|
||||
|
||||
Reference in New Issue
Block a user