# town_cmd.cpp File Reference

`#include "stdafx.h"`
`#include "openttd.h"`
`#include "tile_cmd.h"`
`#include "debug.h"`
`#include "road_map.h"`
`#include "road_internal.h"`
`#include "landscape.h"`
`#include "town_map.h"`
`#include "tunnel_map.h"`
`#include "viewport_func.h"`
`#include "town.h"`
`#include "command_func.h"`
`#include "industry.h"`
`#include "station.h"`
`#include "player_base.h"`
`#include "news.h"`
`#include "saveload.h"`
`#include "gui.h"`
`#include "unmovable_map.h"`
`#include "water_map.h"`
`#include "variables.h"`
`#include "bridge.h"`
`#include "bridge_map.h"`
`#include "genworld.h"`
`#include "newgrf.h"`
`#include "newgrf_callbacks.h"`
`#include "newgrf_house.h"`
`#include "newgrf_commons.h"`
`#include "newgrf_townname.h"`
`#include "autoslope.h"`
`#include "waypoint.h"`
`#include "transparency.h"`
`#include "tunnelbridge_map.h"`
`#include "strings_func.h"`
`#include "window_func.h"`
`#include "string_func.h"`
`#include "table/strings.h"`
`#include "table/sprites.h"`
`#include "table/town_land.h"`
`#include <map>`

Go to the source code of this file.

## Typedefs
typedef void | TownDrawTileProc (const TileInfo *ti)
typedef void | TownActionProc (Town *t)
## Enumerations
enum | TownGrowthResult { GROWTH_SUCCEED = -1, GROWTH_SEARCH_STOPPED = 0 }
## Functions
static bool | BuildTownHouse (Town *t, TileIndex tile)
| Tries to build a house at this tile.
static void | TownDrawHouseLift (const TileInfo *ti)
uint | OriginalTileRandomiser (uint x, uint y)
static DiagDirection | RandomDiagDir ()
| Return a random direction.
static void | DrawTile_Town (TileInfo *ti)
| House Tile drawing handler.
static uint | GetSlopeZ_Town (TileIndex tile, uint x, uint y)
static Foundation | GetFoundation_Town (TileIndex tile, Slope tileh)
static void | AnimateTile_Town (TileIndex tile)
| Animate a tile for a town Only certain houses can be animated The newhouses animation superseeds regular ones.
static bool | IsCloseToTown (TileIndex tile, uint dist)
| Determines if a town is close to a tile.
static void | MarkTownSignDirty (Town *t)
| Marks the town sign as needing a repaint.
void | UpdateTownVirtCoord (Town *t)
| Resize the sign(label) of the town after changes in population (creation or growth or else).
void | UpdateAllTownVirtCoords ()
| Update the virtual coords needed to draw the town sign for all towns.
static void | ChangePopulation (Town *t, int mod)
| Change the towns population.
uint32 | GetWorldPopulation ()
| Determines the world population Basically, count population of all towns, one by one.
static void | MakeSingleHouseBigger (TileIndex tile)
| Helper function for house completion stages progression.
static void | MakeTownHouseBigger (TileIndex tile)
| Make the house advances in its construction stages until completion.
static void | TileLoop_Town (TileIndex tile)
| Periodic tic handler for houses and town.
static void | ClickTile_Town (TileIndex tile)
| Unused handler.
static CommandCost | ClearTile_Town (TileIndex tile, byte flags)
static void | GetAcceptedCargo_Town (TileIndex tile, AcceptedCargo ac)
static void | GetTileDesc_Town (TileIndex tile, TileDesc *td)
static TrackStatus | GetTileTrackStatus_Town (TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
static void | ChangeTileOwner_Town (TileIndex tile, PlayerID old_player, PlayerID new_player)
static bool | GrowTown (Town *t)
| Grow the town true if a house was built, or no if the build failed.
static void | TownTickHandler (Town *t)
void | OnTick_Town ()
static RoadBits | GetTownRoadBits (TileIndex tile)
| Return the RoadBits of a tile.
static bool | IsNeighborRoadTile (TileIndex tile, const DiagDirection dir, uint dist_multi)
| Check if a neighboring tile has a road.
static bool | IsRoadAllowedHere (TileIndex tile, DiagDirection dir)
| Check if a Road is allowed on a given tile.
static bool | TerraformTownTile (TileIndex tile, int edges, int dir)
static void | LevelTownLand (TileIndex tile)
static RoadBits | GetTownRoadGridElement (Town *t, TileIndex tile, DiagDirection dir)
| Generate the RoadBits of a grid tile.
static bool | GrowTownWithExtraHouse (Town *t, TileIndex tile)
| Grows the town with an extra house.
static bool | GrowTownWithRoad (const Town *t, TileIndex tile, RoadBits rcmd)
| Grows the town with a road piece.
static bool | GrowTownWithBridge (const Town *t, const TileIndex tile, const DiagDirection bridge_dir)
| Grows the town with a bridge.
static void | GrowTownInTile (TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection target_dir, Town *t1)
| Grows the given town.
static int | GrowTownAtRoad (Town *t, TileIndex tile)
| Returns "growth" if a house was built, or no if the build failed.
static RoadBits | GenRandomRoadBits ()
| Generate a random road block.
void | UpdateTownRadius (Town *t)
static bool | CreateTownName (uint32 *townnameparts)
void | UpdateTownMaxPass (Town *t)
static void | DoCreateTown (Town *t, TileIndex tile, uint32 townnameparts, TownSizeMode size_mode, uint size)
| Does the actual town creation.
CommandCost | CmdBuildTown (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Create a new town.
Town * | CreateRandomTown (uint attempts, TownSizeMode mode, uint size)
bool | GenerateTowns ()
HouseZonesBits | GetTownRadiusGroup (const Town *t, TileIndex tile)
| Returns the bit corresponding to the town zone of the specified tile.
static void | ClearMakeHouseTile (TileIndex tile, Town *t, byte counter, byte stage, HouseID type, byte random_bits)
| Clears tile and builds a house or house part.
static void | MakeTownHouse (TileIndex t, Town *town, byte counter, byte stage, HouseID type, byte random_bits)
| Write house information into the map.
static bool | CanBuildHouseHere (TileIndex tile, TownID town, bool noslope)
| Checks if a house can be built here.
static bool | CheckBuildHouseSameZ (TileIndex tile, TownID town, uint z, bool noslope)
| Checks if a house can be built at this tile, must have the same max z as parameter.
static bool | CheckFree2x2Area (TileIndex tile, TownID town, uint z, bool noslope)
| Checks if a house of size 2x2 can be built at this tile.
static bool | TownLayoutAllowsHouseHere (Town *t, TileIndex tile)
| Checks if current town layout allows building here.
static bool | TownLayoutAllows2x2HouseHere (Town *t, TileIndex tile)
| Checks if current town layout allows 2x2 building here.
static bool | CheckTownBuild2House (TileIndex *tile, Town *t, uint maxz, bool noslope, DiagDirection second)
| Checks if 1x2 or 2x1 building is allowed here, also takes into account current town layout Also, tests both building positions that occupy this tile.
static bool | CheckTownBuild2x2House (TileIndex *tile, Town *t, uint maxz, bool noslope)
| Checks if 2x2 building is allowed here, also takes into account current town layout Also, tests all four building positions that occupy this tile.
static void | DoClearTownHouseHelper (TileIndex tile, Town *t, HouseID house)
TileIndex | GetHouseNorthPart (HouseID &house)
| Determines if a given HouseID is part of a multitile house.
void | ClearTownHouse (Town *t, TileIndex tile)
static bool | IsUniqueTownName (const char *name)
CommandCost | CmdRenameTown (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Rename a town (server-only).
void | ExpandTown (Town *t)
| Called from GUI.
static void | TownActionAdvertiseSmall (Town *t)
static void | TownActionAdvertiseMedium (Town *t)
static void | TownActionAdvertiseLarge (Town *t)
static void | TownActionRoadRebuild (Town *t)
static bool | DoBuildStatueOfCompany (TileIndex tile, TownID town_id)
static bool | SearchTileForStatue (TileIndex tile, uint32 town_id)
| Search callback function for TownActionBuildStatue.
static void | TownActionBuildStatue (Town *t)
| Perform a 9x9 tiles circular search from the center of the town in order to find a free tile to place a statue.
static void | TownActionFundBuildings (Town *t)
static void | TownActionBuyRights (Town *t)
static void | TownActionBribe (Town *t)
uint | GetMaskOfTownActions (int *nump, PlayerID pid, const Town *t)
| Get a list of available actions to do at a town.
CommandCost | CmdDoTownAction (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Do a town action.
static void | UpdateTownGrowRate (Town *t)
static void | UpdateTownAmounts (Town *t)
static void | UpdateTownUnwanted (Town *t)
bool | CheckIfAuthorityAllows (TileIndex tile)
Town * | CalcClosestTownFromTile (TileIndex tile, uint threshold)
Town * | ClosestTownFromTile (TileIndex tile, uint threshold)
void | SetTownRatingTestMode (bool mode)
static int | GetRating (const Town *t)
void | ChangeTownRating (Town *t, int add, int max)
bool | CheckforTownRating (uint32 flags, Town *t, byte type)
void | TownsMonthlyLoop ()
void | InitializeTowns ()
static CommandCost | TerraformTile_Town (TileIndex tile, uint32 flags, uint z_new, Slope tileh_new)
static void | Save_HOUSEIDS ()
static void | Load_HOUSEIDS ()
static void | Save_TOWN ()
static void | Load_TOWN ()
void | AfterLoadTown ()
void | ResetHouses ()
## Variables
uint | _total_towns
HouseSpec | _house_specs [HOUSE_MAX]
bool | _town_sort_dirty
byte | _town_sort_order
const Town ** | _town_sort
Town * | _cleared_town
int | _cleared_town_rating
static int | _grow_town_result
static TownDrawTileProc *const | _town_draw_tile_procs [1]
static const byte | _num_initial_towns [4] = {5, 11, 23, 46}
const byte | _town_action_costs [8]
static TownActionProc *const | _town_action_proc []
static bool | _town_rating_test = false
std::map< const Town *, int > | _town_test_ratings
static const int | _default_rating_settings [3][3]
const TileTypeProcs | _tile_type_town_procs
static const SaveLoad | _town_desc []
| Save and load of towns.
static const SaveLoad | _house_id_mapping_desc []
const ChunkHandler | _town_chunk_handlers []

## Detailed Description

Definition in file town_cmd.cpp.

## Function Documentation

static void AnimateTile_Town | ( | TileIndex | tile | ) | `[static]`

Animate a tile for a town Only certain houses can be animated The newhouses animation superseeds regular ones.

Parameters:

  | tile | TileIndex of the house to animate

Definition at line 225 of file town_cmd.cpp.

References DeleteAnimatedTile(), GetHouseType(), GetLiftDestination(), GetLiftPosition(), HaltLift(), LiftHasDestination(), MarkTileDirtyByTile(), SetLiftDestination(), and SetLiftPosition().

static bool BuildTownHouse | ( | Town * | t,
|  | TileIndex | tile |
| ) |  |  | `[static]`

Tries to build a house at this tile.

Parameters:

  | t | town the house will belong to
  | tile | where the house will be built

Returns:
  false iff no house can be built at this tile

Definition at line 1837 of file town_cmd.cpp.

References BUILDING_IS_HISTORICAL, CALLBACK_FAILED, CanBuildHouseHere(), CBID_HOUSE_ALLOW_CONSTRUCTION, Chance16(), ChangePopulation(), CheckTownBuild2House(), CheckTownBuild2x2House(), DIAGDIR_SE, DIAGDIR_SW, GB(), GetTownRadiusGroup(), HasBit(), HASBITS, MakeTownHouse(), SetBit(), SLOPE_FLAT, TOWN_HAS_CHURCH, TOWN_HAS_STADIUM, TOWN_HOUSE_COMPLETED, and TownLayoutAllowsHouseHere().

Referenced by GrowTownInTile(), GrowTownWithExtraHouse(), and TileLoop_Town().

static bool CanBuildHouseHere | ( | TileIndex | tile,
|  | TownID | town,
|  | bool | noslope |
| ) |  |  | `[inline, static]`

Checks if a house can be built here.

Important is slope, bridge above and ability to clear the land.

Parameters:

  | tile | tile to check
  | town | town that is checking
  | noslope | are slopes (foundations) allowed?

Returns:
  true iff house can be built here

Definition at line 1661 of file town_cmd.cpp.

References CMD_LANDSCAPE_CLEAR, CmdSucceeded(), DC_AUTO, DC_NO_WATER, DoCommand(), GetTownIndex(), IsBridgeAbove(), IsSteepSlope(), IsTileType(), MayHaveBridgeAbove(), MP_HOUSE, and SLOPE_FLAT.

Referenced by BuildTownHouse(), and CheckBuildHouseSameZ().

static void ChangePopulation | ( | Town * | t,
|  | int | mod |
| ) |  |  | `[static]`

Change the towns population.

Parameters:

  | t | Town which polulation has changed
  | mod | polulation change (can be positive or negative)

Definition at line 335 of file town_cmd.cpp.

References UpdateTownVirtCoord().

Referenced by BuildTownHouse(), and MakeSingleHouseBigger().

static bool CheckBuildHouseSameZ | ( | TileIndex | tile,
|  | TownID | town,
|  | uint | z,
|  | bool | noslope |
| ) |  |  | `[inline, static]`

Checks if a house can be built at this tile, must have the same max z as parameter.

Parameters:

  | tile | tile to check
  | town | town that is checking
  | z | max z of this tile so more parts of a house are at the same height (with foundation)
  | noslope | are slopes (foundations) allowed?

Returns:
  true iff house can be built here

See also:
  CanBuildHouseHere()

Definition at line 1687 of file town_cmd.cpp.

References CanBuildHouseHere().

Referenced by CheckFree2x2Area(), and CheckTownBuild2House().

static bool CheckFree2x2Area | ( | TileIndex | tile,
|  | TownID | town,
|  | uint | z,
|  | bool | noslope |
| ) |  |  | `[static]`

Checks if a house of size 2x2 can be built at this tile.

Parameters:

  | tile | tile, N corner
  | town | town that is checking
  | z | maximum tile z so all tile have the same max z
  | noslope | are slopes (foundations) allowed?

Returns:
  true iff house can be built

See also:
  CheckBuildHouseSameZ()

Definition at line 1707 of file town_cmd.cpp.

References CheckBuildHouseSameZ(), DIAGDIR_END, DIAGDIR_SE, and TileOffsByDiagDir().

Referenced by CheckTownBuild2x2House().

static bool CheckTownBuild2House | ( | TileIndex * | tile,
|  | Town * | t,
|  | uint | maxz,
|  | bool | noslope,
|  | DiagDirection | second |
| ) |  |  | `[static]`

Checks if 1x2 or 2x1 building is allowed here, also takes into account current town layout Also, tests both building positions that occupy this tile.

Parameters:

  | tile | tile where the building should be built
  | t | town
  | maxz | all tiles should have the same height
  | noslope | are slopes forbidden?
  | second | diagdir from first tile to second tile

Definition at line 1789 of file town_cmd.cpp.

References CheckBuildHouseSameZ(), ReverseDiagDir(), TileOffsByDiagDir(), and TownLayoutAllowsHouseHere().

Referenced by BuildTownHouse().

static bool CheckTownBuild2x2House | ( | TileIndex * | tile,
|  | Town * | t,
|  | uint | maxz,
|  | bool | noslope |
| ) |  |  | `[static]`

Checks if 2x2 building is allowed here, also takes into account current town layout Also, tests all four building positions that occupy this tile.

Parameters:

  | tile | tile where the building should be built
  | t | town
  | maxz | all tiles should have the same height
  | noslope | are slopes forbidden?

Definition at line 1814 of file town_cmd.cpp.

References CheckFree2x2Area(), DIAGDIR_END, DIAGDIR_SE, ReverseDiagDir(), TileOffsByDiagDir(), and TownLayoutAllows2x2HouseHere().

Referenced by BuildTownHouse().

static void ClearMakeHouseTile | ( | TileIndex | tile,
|  | Town * | t,
|  | byte | counter,
|  | byte | stage,
|  | HouseID | type,
|  | byte | random_bits |
| ) |  |  | `[inline, static]`

Clears tile and builds a house or house part.

Parameters:

  | t | tile index
  | tid | Town index
  | counter | of construction step
  | stage | of construction (used for drawing)
  | type | of house. Index into house specs array
  | random_bits | required for newgrf houses

Precondition:
  house can be built here

Definition at line 1617 of file town_cmd.cpp.

References CMD_LANDSCAPE_CLEAR, CmdSucceeded(), DC_AUTO, DC_EXEC, DC_NO_WATER, DoCommand(), IncreaseBuildingCount(), and MakeHouseTile().

Referenced by MakeTownHouse().

static void ClickTile_Town | ( | TileIndex | tile | ) | `[static]`

Unused handler.

Parameters:

  | tile | unused

Definition at line 504 of file town_cmd.cpp.

CommandCost CmdBuildTown | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Create a new town.

This obviously only works in the scenario editor. Function not removed as it might be possible in the future to fund your own town :)

Parameters:

  | tile | coordinates where town is built
  | flags | type of operation
  | p1 | size of the town (0 = small, 1 = medium, 2 = large)
  | p2 | size mode (

See also:
  TownSizeMode)

Definition at line 1486 of file town_cmd.cpp.

References CMD_ERROR, DC_EXEC, DistanceFromEdge(), DoCreateTown(), IsCloseToTown(), IsTileType(), MP_CLEAR, MP_TREES, return_cmd_error, and SLOPE_FLAT.

CommandCost CmdDoTownAction | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Do a town action.

This performs an action such as advertising, building a statue, funding buildings, but also bribing the town-council

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | town to do the action at
  | p2 | action to perform,

See also:
  _town_action_proc for the list of available actions

Definition at line 2261 of file town_cmd.cpp.

References CMD_ERROR, DC_EXEC, GetMaskOfTownActions(), HasBit(), and IsValidTownID().

CommandCost CmdRenameTown | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Rename a town (server-only).

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | town ID to rename
  | p2 | unused

Definition at line 2061 of file town_cmd.cpp.

References _cmd_text, CMD_ERROR, DC_EXEC, IsValidTownID(), MarkWholeScreenDirty(), return_cmd_error, UpdateAllStationVirtCoord(), UpdateAllWaypointSigns(), and UpdateTownVirtCoord().

static void DoCreateTown | ( | Town * | t,
|  | TileIndex | tile,
|  | uint32 | townnameparts,
|  | TownSizeMode | size_mode,
|  | uint | size |
| ) |  |  | `[static]`

Does the actual town creation.

Parameters:

  | t | The town
  | tile | Where to put it
  | townnameparts | The town name
  | size_mode | How the size should be determined
  | size | Parameter for size determination

Definition at line 1392 of file town_cmd.cpp.

References GrowTown(), INVALID_PLAYER, MAX_PLAYERS, and UpdateTownVirtCoord().

Referenced by CmdBuildTown().

static void DrawTile_Town | ( | TileInfo * | ti | ) | `[static]`

House Tile drawing handler.

Part of the tile loop process

Parameters:

  | ti | TileInfo of the tile to draw

Definition at line 158 of file town_cmd.cpp.

References _town_draw_tile_data, AddSortableSpriteToDraw(), DrawBuildingsTileStruct::building, DrawBuildingsTileStruct::draw_proc, DrawGroundSprite(), DrawBuildingsTileStruct::dz, FOUNDATION_LEVELED, GetHouseBuildingStage(), GetHouseType(), DrawBuildingsTileStruct::ground, DrawBuildingsTileStruct::height, IsTransparencySet(), SLOPE_FLAT, DrawBuildingsTileStruct::subtile_x, DrawBuildingsTileStruct::subtile_y, TO_HOUSES, and DrawBuildingsTileStruct::width.

static RoadBits GenRandomRoadBits | ( |  | ) | `[static]`

Generate a random road block.

The probability of a straight road is somewhat higher than a curved.

Returns:
  A RoadBits value with 2 bits set

Definition at line 1211 of file town_cmd.cpp.

References GB(), and ROAD_NW.

Referenced by GrowTown().

TileIndex GetHouseNorthPart | ( | HouseID & | house | ) |

Determines if a given HouseID is part of a multitile house.

The given ID is set to the ID of the north tile and the TileDiff to the north tile is returned.

Parameters:

  | house | Is changed to the HouseID of the north tile of the same house

Returns:
  TileDiff from the tile of the given HouseID to the north tile

Definition at line 1986 of file town_cmd.cpp.

References TileDiffXY().

Referenced by UpdateHousesAndTowns().

uint GetMaskOfTownActions | ( | int * | nump,
|  | PlayerID | pid,
|  | const Town * | t |
| ) |  |  |

Get a list of available actions to do at a town.

Parameters:

  | nump | if not NULL add put the number of available actions in it
  | pid | the player that is querying the town
  | t | the town that is queried

Returns:
  bitmasked value of enabled actions

Definition at line 77 of file town_gui.cpp.

References HasBit(), and PLAYER_SPECTATOR.

Referenced by CmdDoTownAction(), and TownAuthorityWndProc().

HouseZonesBits GetTownRadiusGroup | ( | const Town * | t,
|  | TileIndex | tile |
| ) |  |  |

Returns the bit corresponding to the town zone of the specified tile.

Parameters:

  | t | Town on which radius is to be found
  | tile | TileIndex where radius needs to be found

Returns:
  the bit position of the given zone, as defined in HouseZones

Definition at line 1591 of file town_cmd.cpp.

References DistanceSquare().

Referenced by BuildTownHouse(), HouseGetVariable(), and IndustryGetVariable().

static RoadBits GetTownRoadBits | ( | TileIndex | tile | ) | `[static]`

Return the RoadBits of a tile.

Note:
  There are many other functions doing things like that.

  Needs to be checked for needlessness.

Parameters:

  | tile | The tile we want to analyse

Returns:
  The roadbits of the given tile

Definition at line 650 of file town_cmd.cpp.

References GetAnyRoadTrackBits(), ROAD_NE, ROAD_NONE, ROAD_NW, ROAD_SE, ROAD_SW, ROAD_X, ROAD_Y, ROADTYPE_ROAD, TRACK_BIT_LEFT, TRACK_BIT_LOWER, TRACK_BIT_NONE, TRACK_BIT_RIGHT, TRACK_BIT_UPPER, TRACK_BIT_X, and TRACK_BIT_Y.

Referenced by GrowTown(), GrowTownAtRoad(), GrowTownWithBridge(), IsNeighborRoadTile(), and IsRoadAllowedHere().

static RoadBits GetTownRoadGridElement | ( | Town * | t,
|  | TileIndex | tile,
|  | DiagDirection | dir |
| ) |  |  | `[static]`

Generate the RoadBits of a grid tile.

Parameters:

  | t | current town
  | tile | tile in reference to the town
  | dir | The direction to which we are growing ATM

Returns:
  the RoadBit of the current tile regarding the selected town layout

Definition at line 801 of file town_cmd.cpp.

References DiagDirToRoadBits(), ReverseDiagDir(), ROAD_ALL, ROAD_NE, ROAD_NONE, ROAD_NW, ROAD_SE, ROAD_SW, ROAD_X, ROAD_Y, SLOPE_E, SLOPE_N, SLOPE_NE, SLOPE_NW, SLOPE_S, SLOPE_SE, SLOPE_STEEP_E, SLOPE_STEEP_N, SLOPE_STEEP_S, SLOPE_STEEP_W, SLOPE_SW, SLOPE_W, TileIndexToTileIndexDiffC(), TL_2X2_GRID, TL_3X3_GRID, TileIndexDiffC::x, and TileIndexDiffC::y.

Referenced by GrowTownInTile().

uint32 GetWorldPopulation | ( |  | ) |

Determines the world population Basically, count population of all towns, one by one.

Returns:
  uint32 the calculated population of the world

Definition at line 349 of file town_cmd.cpp.

static bool GrowTown | ( | Town * | t | ) | `[static]`

Grow the town true if a house was built, or no if the build failed.

Definition at line 1222 of file town_cmd.cpp.

References CMD_BUILD_ROAD, CMD_LANDSCAPE_CLEAR, CmdSucceeded(), DC_AUTO, DC_EXEC, DC_NO_WATER, DoCommand(), GenRandomRoadBits(), GetTownRoadBits(), GrowTownAtRoad(), IsTileType(), MP_HOUSE, OWNER_TOWN, ROAD_NONE, SLOPE_FLAT, TILE_ADD, TL_NO_ROADS, and ToTileIndexDiff().

Referenced by DoCreateTown(), and ExpandTown().

static int GrowTownAtRoad | ( | Town * | t,
|  | TileIndex | tile |
| ) |  |  | `[static]`

Returns "growth" if a house was built, or no if the build failed.

Parameters:

  | t | town to inquiry
  | tile | to inquiry

Returns:
  something other than zero(0)if town expansion was possible

Definition at line 1142 of file town_cmd.cpp.

References DIAGDIR_END, DiagDirToRoadBits(), GetTownByTile(), GetTownRoadBits(), GrowTownInTile(), IsTileOwner(), IsTileType(), MP_ROAD, OWNER_NONE, OWNER_TOWN, RandomDiagDir(), ReverseDiagDir(), ROAD_NONE, SetTileOwner(), SetTownIndex(), TILE_ASSERT, TileAddByDiagDir(), TL_2X2_GRID, TL_3X3_GRID, and TL_BETTER_ROADS.

Referenced by GrowTown().

static void GrowTownInTile | ( | TileIndex * | tile_ptr,
|  | RoadBits | cur_rb,
|  | DiagDirection | target_dir,
|  | Town * | t1 |
| ) |  |  | `[static]`

Grows the given town.

There are at the moment 3 possible way's for the town expansion:

- Generate a random tile and check if there is a road allowed
- TL_ORIGINAL
- TL_BETTER_ROADS
- Check if the town geometry allows a road and which one
- TL_2X2_GRID
- TL_3X3_GRID
- Forbid roads, only build houses
- TL_NO_ROADS

Parameters:

  | tile_ptr | The current tile
  | cur_rb | The current tiles RoadBits
  | target_dir | The target road dir
  | t1 | The current town

Definition at line 974 of file town_cmd.cpp.

References BuildTownHouse(), Chance16(), ChangeDiagDir(), DIAGDIR_END, DIAGDIRDIFF_90LEFT, DIAGDIRDIFF_90RIGHT, DiagDirToRoadBits(), GetOtherTunnelBridgeEnd(), GetTownRoadGridElement(), GetTunnelBridgeTransportType(), GrowTownWithBridge(), GrowTownWithExtraHouse(), GrowTownWithRoad(), IsRoadAllowedHere(), IsTileType(), MP_HOUSE, MP_TUNNELBRIDGE, RandomDiagDir(), ReverseDiagDir(), ROAD_NONE, TILE_ASSERT, TileAddByDiagDir(), TL_2X2_GRID, TL_3X3_GRID, TL_BETTER_ROADS, TL_NO_ROADS, and TL_ORIGINAL.

Referenced by GrowTownAtRoad().

static bool GrowTownWithBridge | ( | const Town * | t,
|  | const TileIndex | tile,
|  | const DiagDirection | bridge_dir |
| ) |  |  | `[static]`

Grows the town with a bridge.

At first we check if a bridge is reasonable. If so we check if we are able to build it.

Parameters:

  | t | The current town
  | tile | The current tile
  | bridge_dir | The valid direction in which to grow a bridge

Returns:
  true if a bridge has been build else false

Definition at line 911 of file town_cmd.cpp.

References CMD_BUILD_BRIDGE, CmdSucceeded(), DC_AUTO, DC_EXEC, DIAGDIR_END, DiagDirToRoadBits(), DoCommand(), GetTownRoadBits(), HASBITS, InclinedSlope(), ReverseDiagDir(), ROADTYPES_ROAD, SLOPE_FLAT, TileAddByDiagDir(), TileOffsByDiagDir(), TileX(), and TileY().

Referenced by GrowTownInTile().

static bool GrowTownWithExtraHouse | ( | Town * | t,
|  | TileIndex | tile |
| ) |  |  | `[static]`

Grows the town with an extra house.

Check if there are enough neighbor house tiles next to the current tile. If there are enough add another house.

Parameters:

  | t | The current town
  | tile | The target tile for the extra house

Returns:
  true if an extra house has been added

Definition at line 860 of file town_cmd.cpp.

References BuildTownHouse(), DIAGDIR_BEGIN, DIAGDIR_END, IsTileType(), MapMaxX(), MapMaxY(), MP_HOUSE, TileAddByDiagDir(), TileX(), and TileY().

Referenced by GrowTownInTile().

static bool GrowTownWithRoad | ( | const Town * | t,
|  | TileIndex | tile,
|  | RoadBits | rcmd |
| ) |  |  | `[static]`

Grows the town with a road piece.

Parameters:

  | t | The current town
  | tile | The current tile
  | rcmd | The RoadBits we want to build on the tile

Returns:
  true if the RoadBits have been added else false

Definition at line 892 of file town_cmd.cpp.

References CMD_BUILD_ROAD, CmdSucceeded(), DC_AUTO, DC_EXEC, DC_NO_WATER, and DoCommand().

Referenced by GrowTownInTile().

static bool IsCloseToTown | ( | TileIndex | tile,
|  | uint | dist |
| ) |  |  | `[static]`

Determines if a town is close to a tile.

Parameters:

  | tile | TileIndex of the tile to query
  | dist | maximum distance to be accepted

Returns:
  true if the tile correspond to the distance criteria

Definition at line 275 of file town_cmd.cpp.

References DistanceManhattan().

Referenced by CmdBuildTown().

static bool IsNeighborRoadTile | ( | TileIndex | tile,
|  | const DiagDirection | dir,
|  | uint | dist_multi |
| ) |  |  | `[static]`

Check if a neighboring tile has a road.

Parameters:

  | tile | curent tile
  | dir | target direction
  | dist_multi | distance multiplyer

Returns:
  true if one of the neighboring tiles at the given distance is a road tile else false

Definition at line 674 of file town_cmd.cpp.

References ChangeDiagDir(), DIAGDIRDIFF_90LEFT, DIAGDIRDIFF_90RIGHT, DiagDirToRoadBits(), GetTownRoadBits(), ReverseDiagDir(), TILE_ADD, and TileOffsByDiagDir().

Referenced by IsRoadAllowedHere().

static bool IsRoadAllowedHere | ( | TileIndex | tile,
|  | DiagDirection | dir |
| ) |  |  | `[static]`

Check if a Road is allowed on a given tile.

Parameters:

  | tile | The target tile
  | dir | The direction in which we want to extend the town

Returns:
  true if it is allowed else false

Definition at line 709 of file town_cmd.cpp.

References Chance16(), CMD_BUILD_ROAD, CMD_ERROR, CMD_LANDSCAPE_CLEAR, CMD_TERRAFORM_LAND, CmdFailed(), ComplementSlope(), DC_AUTO, DC_EXEC, DC_NO_WATER, DIAGDIR_NW, DIAGDIR_SE, DoCommand(), GetTownRoadBits(), IsNeighborRoadTile(), MapMaxX(), MapMaxY(), ROAD_NONE, ROAD_X, ROAD_Y, SLOPE_ELEVATED, SLOPE_FLAT, SLOPE_NE, SLOPE_NW, TileX(), TileY(), TL_BETTER_ROADS, and TL_ORIGINAL.

Referenced by GrowTownInTile().

static void MakeSingleHouseBigger | ( | TileIndex | tile | ) | `[static]`

Helper function for house completion stages progression.

Parameters:

  | tile | TileIndex of the house (or parts of it) to "grow"

Definition at line 363 of file town_cmd.cpp.

References CALLBACK_FAILED, CBID_HOUSE_CONSTRUCTION_STATE_CHANGE, ChangePopulation(), GetHouseConstructionTick(), GetHouseType(), GetTownByTile(), HasBit(), IncHouseConstructionTick(), IsHouseCompleted(), IsTileType(), LiftHasDestination(), MarkTileDirtyByTile(), and MP_HOUSE.

Referenced by MakeTownHouseBigger().

static void MakeTownHouse | ( | TileIndex | t,
|  | Town * | town,
|  | byte | counter,
|  | byte | stage,
|  | HouseID | type,
|  | byte | random_bits |
| ) |  |  | `[static]`

Write house information into the map.

For houses > 1 tile, all tiles are marked.

Parameters:

  | t | tile index
  | tid | Town index
  | counter | of construction step
  | stage | of construction (used for drawing)
  | type | of house. Index into house specs array
  | random_bits | required for newgrf houses

Precondition:
  house can be built here

Definition at line 1642 of file town_cmd.cpp.

References ClearMakeHouseTile(), and TileDiffXY().

Referenced by BuildTownHouse().

static void MakeTownHouseBigger | ( | TileIndex | tile | ) | `[static]`

Make the house advances in its construction stages until completion.

Parameters:

  | tile | TileIndex of house

Definition at line 391 of file town_cmd.cpp.

References GetHouseType(), MakeSingleHouseBigger(), and TILE_ADDXY.

Referenced by TileLoop_Town().

static DiagDirection RandomDiagDir | ( |  | ) | `[inline, static]`

Return a random direction.

Returns:
  a random direction

Definition at line 148 of file town_cmd.cpp.

Referenced by GrowTownAtRoad(), and GrowTownInTile().

static bool SearchTileForStatue | ( | TileIndex | tile,
|  | uint32 | town_id |
| ) |  |  | `[static]`

Search callback function for TownActionBuildStatue.

Parameters:

  | tile | on which to perform the search
  | town_id | The town_id for which we want a statue

Returns:
  the result of the test

Definition at line 2169 of file town_cmd.cpp.

Referenced by TownActionBuildStatue().

static void TileLoop_Town | ( | TileIndex | tile | ) | `[static]`

Periodic tic handler for houses and town.

Parameters:

  | tile | been asked to do its stuff

Definition at line 404 of file town_cmd.cpp.

References AddAnimatedTile(), BuildTownHouse(), CALLBACK_FAILED, CALLBACK_HOUSEPRODCARGO_END, CBID_HOUSE_PRODUCE_CARGO, Chance16(), GB(), GetHouseConstructionYear(), GetHouseType(), GetTownByTile(), HasBit(), IsHouseCompleted(), LiftHasDestination(), MakeTownHouseBigger(), max(), OWNER_NONE, OWNER_TOWN, and TOWN_IS_FUNDED.

static void TownActionBuildStatue | ( | Town * | t | ) | `[static]`

Perform a 9x9 tiles circular search from the center of the town in order to find a free tile to place a statue.

Parameters:

  | t | town to search in

Definition at line 2179 of file town_cmd.cpp.

References CircularTileSearch(), SearchTileForStatue(), and SetBit().

static bool TownLayoutAllows2x2HouseHere | ( | Town * | t,
|  | TileIndex | tile |
| ) |  |  | `[inline, static]`

Checks if current town layout allows 2x2 building here.

Parameters:

  | t | town
  | tile | tile to check

Returns:
  true iff town layout allows 2x2 building here

Note:
  see layouts

Definition at line 1756 of file town_cmd.cpp.

References MapSize(), TileX(), TileY(), TL_2X2_GRID, and TL_3X3_GRID.

Referenced by CheckTownBuild2x2House().

static bool TownLayoutAllowsHouseHere | ( | Town * | t,
|  | TileIndex | tile |
| ) |  |  | `[inline, static]`

Checks if current town layout allows building here.

Parameters:

  | t | town
  | tile | tile to check

Returns:
  true iff town layout allows building here

Note:
  see layouts

Definition at line 1728 of file town_cmd.cpp.

References TileIndexToTileIndexDiffC(), TL_2X2_GRID, TL_3X3_GRID, TileIndexDiffC::x, and TileIndexDiffC::y.

Referenced by BuildTownHouse(), and CheckTownBuild2House().

void UpdateAllTownVirtCoords | ( |  | ) |

Update the virtual coords needed to draw the town sign for all towns.

Definition at line 322 of file town_cmd.cpp.

References UpdateTownVirtCoord().

Referenced by InitializeWindowsAndCaches().

static void UpdateTownGrowRate | ( | Town * | t | ) | `[static]`

Towns are processed every TOWN_GROWTH_FREQUENCY ticks, and this is the number of times towns are processed before a new building is built.

Definition at line 2281 of file town_cmd.cpp.

References Chance16(), Clamp(), ClrBit(), DistanceSquare(), GetTropicZone(), max(), MAX_PLAYERS, min(), SetBit(), TilePixelHeight(), TOWN_IS_FUNDED, and TROPICZONE_DESERT.

void UpdateTownVirtCoord | ( | Town * | t | ) |

Resize the sign(label) of the town after changes in population (creation or growth or else).

Parameters:

  | t | Town to update

Definition at line 308 of file town_cmd.cpp.

References MarkTownSignDirty(), TILE_SIZE, TileX(), and TileY().

Referenced by ChangePopulation(), CmdRenameTown(), DoCreateTown(), and UpdateAllTownVirtCoords().

## Variable Documentation

const int _default_rating_settings[3][3]`[static]`

Initial value:

     {

      {  0, 128, 384},
      { 48, 192, 480},
      { 96, 384, 768},
    }

Definition at line 2493 of file town_cmd.cpp.

const SaveLoad _house_id_mapping_desc[]`[static]`

Initial value:

     {
      SLE_VAR(EntityIDMapping, grfid,         SLE_UINT32),
      SLE_VAR(EntityIDMapping, entity_id,     SLE_UINT8),
      SLE_VAR(EntityIDMapping, substitute_id, SLE_UINT8),

    }

Definition at line 2664 of file town_cmd.cpp.

const TileTypeProcs _tile_type_town_procs

Initial value:

     {
      DrawTile_Town,
      GetSlopeZ_Town,
      ClearTile_Town,
      GetAcceptedCargo_Town,
      GetTileDesc_Town,
      GetTileTrackStatus_Town,
      ClickTile_Town,
      AnimateTile_Town,
      TileLoop_Town,
      ChangeTileOwner_Town,
      NULL,
      NULL,
      GetFoundation_Town,
      TerraformTile_Town,
    }

const byte _town_action_costs[8]

Initial value:

     {
      2, 4, 9, 35, 48, 53, 117, 175
    }

TownActionProc* const _town_action_proc[]`[static]`

Initial value:

     {
      TownActionAdvertiseSmall,
      TownActionAdvertiseMedium,
      TownActionAdvertiseLarge,
      TownActionRoadRebuild,
      TownActionBuildStatue,
      TownActionFundBuildings,
      TownActionBuyRights,
      TownActionBribe
    }

Definition at line 2240 of file town_cmd.cpp.

const ChunkHandler _town_chunk_handlers[]

Initial value:

     {
      { 'HIDS', Save_HOUSEIDS, Load_HOUSEIDS, CH_ARRAY },
      { 'CITY', Save_TOWN,     Load_TOWN,     CH_ARRAY | CH_LAST},
    }

const SaveLoad _town_desc[]`[static]`

Save and load of towns.

Definition at line 2591 of file town_cmd.cpp.

TownDrawTileProc* const _town_draw_tile_procs[1]`[static]`

Initial value:

     {
      TownDrawHouseLift
    }

Definition at line 128 of file town_cmd.cpp.