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

Function GrowTown

src/town_cmd.cpp:1897–1950  ·  view source on GitHub ↗

* Grow the town. * @param t The town to grow * @return true if we successfully grew the town with a road or house. */

Source from the content-addressed store, hash-verified

1895 * @return true if we successfully grew the town with a road or house.
1896 */
1897static bool GrowTown(Town *t, TownExpandModes modes)
1898{
1899 static const TileIndexDiffC _town_coord_mod[] = {
1900 {-1, 0},
1901 { 1, 1},
1902 { 1, -1},
1903 {-1, -1},
1904 {-1, 0},
1905 { 0, 2},
1906 { 2, 0},
1907 { 0, -2},
1908 {-1, -1},
1909 {-2, 2},
1910 { 2, 2},
1911 { 2, -2},
1912 { 0, 0}
1913 };
1914
1915 /* Current "company" is a town */
1916 Backup<CompanyID> cur_company(_current_company, OWNER_TOWN);
1917
1918 TileIndex tile = t->xy; // The tile we are working with ATM
1919
1920 /* Find a road that we can base the construction on. */
1921 for (const auto &ptr : _town_coord_mod) {
1922 if (GetTownRoadBits(tile) != ROAD_NONE) {
1923 bool success = GrowTownAtRoad(t, tile, modes);
1924 cur_company.Restore();
1925 return success;
1926 }
1927 tile = TileAdd(tile, ToTileIndexDiff(ptr));
1928 }
1929
1930 /* No road available, try to build a random road block by
1931 * clearing some land and then building a road there. */
1932 if (TownAllowedToBuildRoads(modes)) {
1933 tile = t->xy;
1934 for (const auto &ptr : _town_coord_mod) {
1935 /* Only work with plain land that not already has a house */
1936 if (!IsTileType(tile, MP_HOUSE) && IsTileFlat(tile)) {
1937 if (Command<CMD_LANDSCAPE_CLEAR>::Do({DoCommandFlag::Auto, DoCommandFlag::NoWater}, tile).Succeeded()) {
1938 RoadType rt = GetTownRoadType();
1939 Command<CMD_BUILD_ROAD>::Do({DoCommandFlag::Execute, DoCommandFlag::Auto}, tile, GenRandomRoadBits(), rt, DRD_NONE, t->index);
1940 cur_company.Restore();
1941 return true;
1942 }
1943 }
1944 tile = TileAdd(tile, ToTileIndexDiff(ptr));
1945 }
1946 }
1947
1948 cur_company.Restore();
1949 return false;
1950}
1951
1952/**
1953 * Update the cached town zone radii of a town, based on the number of houses.

Callers 3

TownTickHandlerFunction · 0.85
DoCreateTownFunction · 0.85
CmdExpandTownFunction · 0.85

Calls 11

GetTownRoadBitsFunction · 0.85
GrowTownAtRoadFunction · 0.85
ToTileIndexDiffFunction · 0.85
TownAllowedToBuildRoadsFunction · 0.85
IsTileTypeFunction · 0.85
IsTileFlatFunction · 0.85
GetTownRoadTypeFunction · 0.85
GenRandomRoadBitsFunction · 0.85
SucceededMethod · 0.80
TileAddFunction · 0.70
RestoreMethod · 0.45

Tested by

no test coverage detected