# town.h File Reference

Base of the town class. More...

`#include "oldpool.h"`
`#include "core/bitmath_func.hpp"`
`#include "core/random_func.hpp"`
`#include "cargo_type.h"`
`#include "tile_type.h"`
`#include "date_type.h"`
`#include "town_type.h"`
`#include "company_type.h"`
`#include "settings_type.h"`
`#include "strings_type.h"`
`#include "viewport_type.h"`
`#include "economy_type.h"`
`#include "map_type.h"`
`#include "command_type.h"`

Go to the source code of this file.

## Data Structures
struct | BuildingCounts< T >
struct | Town
struct | HouseSpec
## Defines
#define | FOR_ALL_TOWNS_FROM(t, start) for (t = GetTown(start); t != NULL; t = (t->index + 1U < GetTownPoolSize()) ? GetTown(t->index + 1U) : NULL) if (t->IsValid())
#define | FOR_ALL_TOWNS(t) FOR_ALL_TOWNS_FROM(t, 0)
## Enumerations
enum | { HOUSE_NO_CLASS = 0, NEW_HOUSE_OFFSET = 110, HOUSE_MAX = 512, INVALID_TOWN = 0xFFFF, INVALID_HOUSE_ID = 0xFFFF, HOUSE_CLASS_MAX = HOUSE_MAX - NEW_HOUSE_OFFSET + 1 }
enum | BuildingFlags { TILE_NO_FLAG = 0, TILE_SIZE_1x1 = 1U << 0, TILE_NOT_SLOPED = 1U << 1, TILE_SIZE_2x1 = 1U << 2, TILE_SIZE_1x2 = 1U << 3, TILE_SIZE_2x2 = 1U << 4, BUILDING_IS_ANIMATED = 1U << 5, BUILDING_IS_CHURCH = 1U << 6, BUILDING_IS_STADIUM = 1U << 7, BUILDING_HAS_1_TILE = TILE_SIZE_1x1 | TILE_SIZE_2x1 | TILE_SIZE_1x2 | TILE_SIZE_2x2, BUILDING_2_TILES_X = TILE_SIZE_2x1 | TILE_SIZE_2x2, BUILDING_2_TILES_Y = TILE_SIZE_1x2 | TILE_SIZE_2x2, BUILDING_HAS_4_TILES = TILE_SIZE_2x2 }
enum | HouseZonesBits { HZB_BEGIN = 0, HZB_TOWN_EDGE = 0, HZB_TOWN_OUTSKIRT, HZB_TOWN_OUTER_SUBURB, HZB_TOWN_INNER_SUBURB, HZB_TOWN_CENTRE, HZB_END }
enum | HouseZones { HZ_NOZNS = 0x0000, HZ_ZON1 = 1U << HZB_TOWN_EDGE, HZ_ZON2 = 1U << HZB_TOWN_OUTSKIRT, HZ_ZON3 = 1U << HZB_TOWN_OUTER_SUBURB, HZ_ZON4 = 1U << HZB_TOWN_INNER_SUBURB, HZ_ZON5 = 1U << HZB_TOWN_CENTRE, HZ_ZONALL = 0x001F, HZ_SUBARTC_ABOVE = 0x0800, HZ_TEMP = 0x1000, HZ_SUBARTC_BELOW = 0x2000, HZ_SUBTROPIC = 0x4000, HZ_TOYLND = 0x8000 }
enum | HouseExtraFlags { NO_EXTRA_FLAG = 0, BUILDING_IS_HISTORICAL = 1U << 0, BUILDING_IS_PROTECTED = 1U << 1, SYNCHRONISED_CALLBACK_1B = 1U << 2, CALLBACK_1A_RANDOM_BITS = 1U << 3 }
enum | { ROAD_REMOVE = 0, UNMOVEABLE_REMOVE = 1, TUNNELBRIDGE_REMOVE = 1, INDUSTRY_REMOVE = 2 }
enum | { TOWN_IS_FUNDED = 0, TOWN_HAS_CHURCH = 1, TOWN_HAS_STADIUM = 2 }
| This enum is used in conjonction with town->flags12. More...
## Functions
| assert_compile (HZB_END==5)
uint32 | GetWorldPopulation ()
| Determines the world population Basically, count population of all towns, one by one.
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.
void | InitializeTown ()
void | ShowTownViewWindow (TownID town)
void | ExpandTown (Town *t)
| Called from GUI.
Town * | CreateRandomTown (uint attempts, TownSize size, bool city, TownLayout layout)
bool | CheckforTownRating (DoCommandFlag flags, Town *t, byte type)
static HouseSpec * | GetHouseSpecs (HouseID house_id)
TileIndexDiff | GetHouseNorthPart (HouseID &house)
| Determines if a given HouseID is part of a multitile house.
static bool | IsValidTownID (TownID index)
| Check if a TownID is valid.
static TownID | GetMaxTownIndex ()
static uint | GetNumTowns ()
static Town * | GetRandomTown ()
| Return a random valid town.
Town * | CalcClosestTownFromTile (TileIndex tile, uint threshold=UINT_MAX)
void | ResetHouses ()
void | ClearTownHouse (Town *t, TileIndex tile)
void | UpdateTownMaxPass (Town *t)
void | UpdateTownRadius (Town *t)
bool | CheckIfAuthorityAllowsNewStation (TileIndex tile, DoCommandFlag flags)
| Checks whether the local authority allows construction of a new station (rail, road, airport, dock) on the given tile.
Town * | ClosestTownFromTile (TileIndex tile, uint threshold)
void | ChangeTownRating (Town *t, int add, int max, DoCommandFlag flags)
| Changes town rating of the current company.
HouseZonesBits | GetTownRadiusGroup (const Town *t, TileIndex tile)
| Returns the bit corresponding to the town zone of the specified tile.
void | SetTownRatingTestMode (bool mode)
uint | GetMaskOfTownActions (int *nump, CompanyID cid, const Town *t)
| Get a list of available actions to do at a town.
bool | GenerateTowns (TownLayout layout)
bool | GenerateTownName (uint32 *townnameparts)
| Generates valid town name.
static uint | TileHash (uint x, uint y)
| Calculate a hash value from a tile position.
static uint | TileHash2Bit (uint x, uint y)
| Get the last two bits of the TileHash from a tile position.
## Variables
static const uint | CUSTOM_TOWN_NUMBER_DIFFICULTY = 4
| value for custom town number in difficulty settings
static const uint | CUSTOM_TOWN_MAX_NUMBER = 5000
| this is the maximum number of towns a user can specify in customisation
HouseSpec | _house_specs [HOUSE_MAX]
static const byte | TOWN_GROWTH_FREQUENCY = 70
| This is the number of ticks between towns being processed for building new houses or roads.
static const byte | TOWN_HOUSE_COMPLETED = 3
| Simple value that indicates the house has reached the final stage of construction.
Town * | _cleared_town
int | _cleared_town_rating
uint32 | _cur_town_ctr
| iterator through all towns in OnTick_Town
uint32 | _cur_town_iter
| frequency iterator at the same place

## Detailed Description

Base of the town class.

Definition in file town.h.

## Enumeration Type Documentation

anonymous enum

This enum is used in conjonction with town->flags12.

IT simply states what bit is used for. It is pretty unrealistic (IMHO) to only have one church/stadium per town, NO MATTER the population of it. And there are 5 more bits available on flags12...

Enumerator:

  TOWN_IS_FUNDED | Town has received some funds for.
  TOWN_HAS_CHURCH | There can be only one church by town.
  TOWN_HAS_STADIUM | There can be only one stadium by town.

Definition at line 282 of file town.h.

enum HouseExtraFlags

Enumerator:

  BUILDING_IS_HISTORICAL | this house will only appear during town generation in random games, thus the historical
  BUILDING_IS_PROTECTED | towns and AI will not remove this house, while human players will be able to
  SYNCHRONISED_CALLBACK_1B | synchronized callback 1B will be performed, on multi tile houses
  CALLBACK_1A_RANDOM_BITS | callback 1A needs random bits

Definition at line 83 of file town.h.

enum HouseZones

Enumerator:

  HZ_NOZNS | Bit Value Meaning. 0 This is just to get rid of zeros, meaning none
  HZ_ZON1 | 0..4 1,2,4,8,10 which town zones the building can be built in, Zone1 been the further suburb
  HZ_ZON5 | center of town
  HZ_ZONALL | 1F This is just to englobe all above types at once
  HZ_SUBARTC_ABOVE | 11 800 can appear in sub-arctic climate above the snow line
  HZ_TEMP | 12 1000 can appear in temperate climate
  HZ_SUBARTC_BELOW | 13 2000 can appear in sub-arctic climate below the snow line
  HZ_SUBTROPIC | 14 4000 can appear in subtropical climate
  HZ_TOYLND | 15 8000 can appear in toyland climate

Definition at line 66 of file town.h.

## Function Documentation

void ChangeTownRating | ( | Town * | t,
|  | int | add,
|  | int | max,
|  | DoCommandFlag | flags |
| ) |  |  |

Changes town rating of the current company.

Parameters:

  | t | Town to affect
  | add | Value to add
  | max | Minimum (add < 0) resp. maximum (add > 0) rating that should be archievable with this change
  | flags | Command flags, especially DC_NO_MODIFY_TOWN_RATING is tested

Definition at line 2658 of file town_cmd.cpp.

References DC_NO_MODIFY_TOWN_RATING, InvalidateWindow(), Cheats::magic_bulldozer, SetBit(), and Cheat::value.

Referenced by CheckAllowRemoveRoad(), and CmdPlantTree().

bool CheckIfAuthorityAllowsNewStation | ( | TileIndex | tile,
|  | DoCommandFlag | flags |
| ) |  |  |

Checks whether the local authority allows construction of a new station (rail, road, airport, dock) on the given tile.

Parameters:

  | tile | The tile where the station shall be constructed.
  | flags | Command flags. DC_NO_TEST_TOWN_RATING is tested.

Definition at line 2557 of file town_cmd.cpp.

References _settings_game, DC_NO_TEST_TOWN_RATING, EconomySettings::dist_local_authority, and GameSettings::economy.

Referenced by CmdBuildAirport(), CmdBuildDock(), CmdBuildRailroadStation(), and CmdBuildRoadStop().

bool GenerateTownName | ( | uint32 * | townnameparts | ) |

Generates valid town name.

Parameters:

  | townnameparts | if a name is generated, it's stored there

Returns:
  true iff a name was generated

Definition at line 1428 of file town_cmd.cpp.

References _settings_game, GameSettings::game_creation, GameCreationSettings::town_name, and VerifyTownName().

TileIndexDiff 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 2083 of file town_cmd.cpp.

References TileDiffXY().

Referenced by GetDistanceFromNearbyHouse(), SearchNearbyHouseClass(), SearchNearbyHouseGRFID(), SearchNearbyHouseID(), and UpdateHousesAndTowns().

uint GetMaskOfTownActions | ( | int * | nump,
|  | CompanyID | cid,
|  | 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
  | cid | the company that is querying the town
  | t | the town that is queried

Returns:
  bitmasked value of enabled actions

Definition at line 2380 of file town_cmd.cpp.

References _settings_game, EconomySettings::bribe, COMPANY_SPECTATOR, GameSettings::economy, EconomySettings::exclusive_rights, HasBit(), and lengthof.

Referenced by CmdDoTownAction(), and AITown::IsActionAvailable().

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

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

Parameters:

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

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

Definition at line 1670 of file town_cmd.cpp.

References DistanceSquare().

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

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 362 of file town_cmd.cpp.

static bool IsValidTownID | ( | TownID | index | ) | `[inline, static]`

Check if a TownID is valid.

Parameters:

  | index | to inquiry in the pool of town

Returns:
  true if it exists

Definition at line 303 of file town.h.

Referenced by CmdBuildRoad(), CmdDoTownAction(), CmdRenameTown(), GetRandomTown(), and AITown::IsValidTown().

static uint TileHash | ( | uint | x,
|  | uint | y |
| ) |  |  | `[inline, static]`

Calculate a hash value from a tile position.

Parameters:

  | x | The X coordinate
  | y | The Y coordinate

Returns:
  The hash of the tile

Definition at line 378 of file town.h.

Referenced by TileHash2Bit().

static uint TileHash2Bit | ( | uint | x,
|  | uint | y |
| ) |  |  | `[inline, static]`

Get the last two bits of the TileHash from a tile position.

See also:
  TileHash()

Parameters:

  | x | The X coordinate
  | y | The Y coordinate

Returns:
  The last two bits from hash of the tile

Definition at line 396 of file town.h.

References GB(), and TileHash().

Referenced by DrawTile_Town(), and HouseGetVariable().

void UpdateAllTownVirtCoords | ( |  | ) |

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

Definition at line 335 of file town_cmd.cpp.

References UpdateTownVirtCoord().

Referenced by InitializeWindowsAndCaches().

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 323 of file town_cmd.cpp.

References _settings_client, ClientSettings::gui, MarkTownSignDirty(), GUISettings::population_in_label, TILE_SIZE, TileX(), TileY(), Point::x, and Point::y.

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

## Variable Documentation

const byte TOWN_GROWTH_FREQUENCY = 70`[static]`

This is the number of ticks between towns being processed for building new houses or roads.

This value originally came from the size of the town array in TTD.

Definition at line 270 of file town.h.

const byte TOWN_HOUSE_COMPLETED = 3`[static]`

Simple value that indicates the house has reached the final stage of construction.

Definition at line 274 of file town.h.

Referenced by BuildTownHouse(), GetHouseBuildingStage(), IncHouseConstructionTick(), and MakeHouseTile().