Data Structures | Defines | Typedefs | Enumerations | Functions | Variables

# town.h File Reference

Base of the town class. More...

`#include "viewport_type.h"`
`#include "town_map.h"`
`#include "subsidy_type.h"`
`#include "newgrf_storage.h"`
`#include "cargotype.h"`
`#include "tilematrix_type.hpp"`
`#include <list>`

Go to the source code of this file.

## Data Structures
struct | BuildingCounts< T >
struct | Town
| Town data structure. More...
## Defines
#define | FOR_ALL_TOWNS_FROM(var, start) FOR_ALL_ITEMS_FROM(Town, town_index, var, start)
#define | FOR_ALL_TOWNS(var) FOR_ALL_TOWNS_FROM(var, 0)
## Typedefs
typedef TileMatrix< uint32, 4 > | AcceptanceMatrix
typedef Pool< Town, TownID, 64, 64000 > | TownPool
## Enumerations
enum | TownRatingCheckType { ROAD_REMOVE = 0, TUNNELBRIDGE_REMOVE = 1, TOWN_RATING_CHECK_TYPE_COUNT }
| Action types that a company must ask permission for to a town authority. More...
enum | TownFlags { TOWN_IS_FUNDED = 0, TOWN_HAS_CHURCH = 1, TOWN_HAS_STADIUM = 2 }
| This enum is used in conjunction with town->flags. More...
enum | TownActions { TACT_NONE = 0x00, TACT_ADVERTISE_SMALL = 0x01, TACT_ADVERTISE_MEDIUM = 0x02, TACT_ADVERTISE_LARGE = 0x04, TACT_ROAD_REBUILD = 0x08, TACT_BUILD_STATUE = 0x10, TACT_FUND_BUILDINGS = 0x20, TACT_BUY_RIGHTS = 0x40, TACT_BRIBE = 0x80, TACT_COUNT = 8, TACT_ADVERTISE = TACT_ADVERTISE_SMALL | TACT_ADVERTISE_MEDIUM | TACT_ADVERTISE_LARGE, TACT_CONSTRUCTION = TACT_ROAD_REBUILD | TACT_BUILD_STATUE | TACT_FUND_BUILDINGS, TACT_FUNDS = TACT_BUY_RIGHTS | TACT_BRIBE, TACT_ALL = TACT_ADVERTISE | TACT_CONSTRUCTION | TACT_FUNDS }
| Town actions of a company. More...
## Functions
uint32 | GetWorldPopulation ()
| Determines the world population Basically, count population of all towns, one by one.
void | UpdateAllTownVirtCoords ()
| Update the virtual coords needed to draw the town sign for all towns.
void | ShowTownViewWindow (TownID town)
void | ExpandTown (Town *t)
CommandCost | CheckforTownRating (DoCommandFlag flags, Town *t, TownRatingCheckType type)
| Does the town authority allow the (destructive) action of the current company?
TileIndexDiff | GetHouseNorthPart (HouseID &house)
| Determines if a given HouseID is part of a multitile house.
Town * | CalcClosestTownFromTile (TileIndex tile, uint threshold=UINT_MAX)
| Return the town closest to the given tile within threshold.
void | ResetHouses ()
void | ClearTownHouse (Town *t, TileIndex tile)
void | UpdateTownMaxPass (Town *t)
void | UpdateTownRadius (Town *t)
void | UpdateTownCargoes (Town *t)
| Update cargo acceptance for the complete town.
void | UpdateTownCargoTotal (Town *t)
| Update the total cargo acceptance of the whole town.
void | UpdateTownCargoBitmap ()
| Updates the bitmap of all cargoes accepted by houses.
CommandCost | 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)
| Return the town closest (in distance or ownership) to a given tile, within a given 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)
| Switch the town rating to test-mode, to allow commands to be tested without affecting current ratings.
uint | GetMaskOfTownActions (int *nump, CompanyID cid, const Town *t)
| Get a list of available actions to do at a town.
bool | GenerateTowns (TownLayout layout)
| This function will generate a certain amount of towns, with a certain layout It can be called from the scenario editor (i.e.
const CargoSpec * | FindFirstCargoWithTownEffect (TownEffect effect)
| Determines the first cargo with a certain town effect.
template<class T >
void | MakeDefaultName (T *obj)
| Set the default name for a depot/waypoint.
## 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
static const uint | INVALID_TOWN = 0xFFFF
static const uint | TOWN_GROWTH_WINTER = 0xFFFFFFFE
| The town only needs this cargo in the winter (any amount).
static const uint | TOWN_GROWTH_DESERT = 0xFFFFFFFF
| The town needs the cargo for growth when on desert (any amount).
static const uint16 | TOWN_GROW_RATE_CUSTOM = 0x8000
| If this mask is applied to Town::grow_counter, the grow_counter will not be calculated by the system (but assumed to be set by scripts).
TownPool | _town_pool
const byte | _town_action_costs [TACT_COUNT]
| Factor in the cost of each town action.
TownID | _new_town_id
uint32 | _town_cargoes_accepted
| Bitmap of all cargoes accepted by houses.

## Detailed Description

Base of the town class.

Definition in file town.h.

## Enumeration Type Documentation

enum TownActions

Town actions of a company.

Enumerator:

  TACT_NONE | Empty action set.
  TACT_ADVERTISE_SMALL | Small advertising campaign.
  TACT_ADVERTISE_MEDIUM | Medium advertising campaign.
  TACT_ADVERTISE_LARGE | Large advertising campaign.
  TACT_ROAD_REBUILD | Rebuild the roads.
  TACT_BUILD_STATUE | Build a statue.
  TACT_FUND_BUILDINGS | Fund new buildings.
  TACT_BUY_RIGHTS | Buy exclusive transport rights.
  TACT_BRIBE | Try to bribe the counsil.
  TACT_COUNT | Number of available town actions.
  TACT_ADVERTISE | All possible advertising actions.
  TACT_CONSTRUCTION | All possible construction actions.
  TACT_FUNDS | All possible funding actions.
  TACT_ALL | All possible actions.

Definition at line 201 of file town.h.

enum TownFlags

This enum is used in conjunction with town->flags.

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 flags...

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 166 of file town.h.

enum TownRatingCheckType

Action types that a company must ask permission for to a town authority.

See also:
  CheckforTownRating

Enumerator:

  ROAD_REMOVE | Removal of a road owned by the town.
  TUNNELBRIDGE_REMOVE | Removal of a tunnel or bridge owned by the towb.
  TOWN_RATING_CHECK_TYPE_COUNT | Number of town checking action types.

Definition at line 153 of file town.h.

## Function Documentation

Town* CalcClosestTownFromTile | ( | TileIndex | tile,
|  | uint | threshold |
| ) |  |  |

Return the town closest to the given tile within threshold.

Parameters:

  | tile | Starting point of the search.
  | threshold | Biggest allowed distance to the town.

Returns:
  Closest town to tile within threshold, or `NULL` if there is no such town.

Note:
  This function only uses distance, the ClosestTownFromTile function also takes town ownership into account.

Definition at line 3108 of file town_cmd.cpp.

References DistanceManhattan(), and Town::xy.

Referenced by AfterLoadGame(), BuildObject(), ClosestTownFromTile(), CmdBuildBridge(), CmdBuildRoad(), CmdBuildTunnel(), Town::PostDestructor(), RemoveRoad(), and UpdateNearestTownForRoadTiles().

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 achievable with this change.
  | flags | Command flags, especially DC_NO_MODIFY_TOWN_RATING is tested

Definition at line 3212 of file town_cmd.cpp.

References _cheats, _current_company, DC_NO_MODIFY_TOWN_RATING, GetRating(), Town::have_ratings, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), Cheats::magic_bulldozer, Town::ratings, SetBit(), SetWindowDirty(), Cheat::value, and WC_TOWN_AUTHORITY.

Referenced by CheckAllowRemoveRoad(), CmdPlantTree(), DoClearBridge(), and DoClearTunnel().

CommandCost CheckforTownRating | ( | DoCommandFlag | flags,
|  | Town * | t,
|  | TownRatingCheckType | type |
| ) |  |  |

Does the town authority allow the (destructive) action of the current company?

Parameters:

  | flags | Checking flags of the command.
  | t | Town that must allow the company action.
  | type | Type of action that is wanted.

Returns:
  A succeeded command if the action is allowed, a failed command if it is not allowed.

Definition at line 3249 of file town_cmd.cpp.

References _cheats, _current_company, _settings_game, DC_NO_TEST_TOWN_RATING, GameSettings::difficulty, GetRating(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), Cheats::magic_bulldozer, RATING_TUNNEL_BRIDGE_NEEDED_HOSTILE, RATING_TUNNEL_BRIDGE_NEEDED_NEUTRAL, RATING_TUNNEL_BRIDGE_NEEDED_PERMISSIVE, return_cmd_error, SetDParam(), DifficultySettings::town_council_tolerance, and Cheat::value.

Referenced by CheckAllowRemoveRoad(), DoClearBridge(), and DoClearTunnel().

CommandCost 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.

Returns:
  Succeeded or failed command.

Definition at line 3087 of file town_cmd.cpp.

References _current_company, _settings_game, ClosestTownFromTile(), DC_NO_TEST_TOWN_RATING, EconomySettings::dist_local_authority, GameSettings::economy, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), Town::ratings, return_cmd_error, and SetDParam().

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

Town* ClosestTownFromTile | ( | TileIndex | tile,
|  | uint | threshold |
| ) |  |  |

Return the town closest (in distance or ownership) to a given tile, within a given threshold.

Parameters:

  | tile | Starting point of the search.
  | threshold | Biggest allowed distance to the town.

Returns:
  Closest town to tile within threshold, or `NULL` if there is no such town.

Note:
  If you only care about distance, you can use the CalcClosestTownFromTile function.

Definition at line 3133 of file town_cmd.cpp.

References _generating_world, CalcClosestTownFromTile(), DistanceManhattan(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_town_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_town_pool >::GetNumItems(), GetTileType(), GetTownIndex(), HasTownOwnedRoad(), IsRoadDepot(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_town_pool >::IsValidID(), MP_HOUSE, MP_ROAD, and Town::xy.

Referenced by AfterLoadGame(), CheckAllowRemoveRoad(), CheckIfAuthorityAllowsNewStation(), CheckIfCallBackAllowsCreation(), CmdBuildAirport(), CmdBuildBridge(), CmdBuildDock(), CmdBuildRailStation(), CmdBuildRoadStop(), CmdDeleteTown(), CmdPlantTree(), CmdRemoveRoadStop(), DisasterTick_Big_Ufo(), DoClearBridge(), DoClearTunnel(), FindTownForIndustry(), GenerateCompanyName(), GetNameOfOwner(), IndustryGetVariable(), MakeDefaultName(), ObjectGetVariable(), LandInfoWindow::OnInit(), and Town::~Town().

const CargoSpec* FindFirstCargoWithTownEffect | ( | TownEffect | effect | ) |

Determines the first cargo with a certain town effect.

Parameters:

  | effect | Town effect of interest

Returns:
  first active cargo slot with that effect

Definition at line 2431 of file town_cmd.cpp.

References CargoSpec::town_effect.

Referenced by AfterLoadGame(), CmdTownCargoGoal(), DoCreateTown(), and TownViewWindow::DrawWidget().

bool GenerateTowns | ( | TownLayout | layout | ) |

This function will generate a certain amount of towns, with a certain layout It can be called from the scenario editor (i.e.

: generate Random Towns) as well as from world creation.

Parameters:

  | layout | which towns will be set to, when created

Returns:
  true if towns have been successfully created

Definition at line 1871 of file town_cmd.cpp.

References _settings_game, Chance16(), GameCreationSettings::custom_town_number, CUSTOM_TOWN_NUMBER_DIFFICULTY, GameSettings::difficulty, GameSettings::economy, GameSettings::game_creation, GenerateTownName(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_town_pool >::GetNumItems(), GWP_TOWN, IncreaseGeneratingWorldProgress(), INVALID_STRING_ID, EconomySettings::larger_towns, DifficultySettings::number_towns, ScaleByMapSize(), SetGeneratingWorldProgress(), ShowErrorMessage(), TSZ_RANDOM, and WL_CRITICAL.

Referenced by _GenerateWorld().

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

References TileDiffXY().

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

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

References _settings_game, EconomySettings::bribe, COMPANY_SPECTATOR, GameSettings::economy, EconomySettings::exclusive_rights, EconomySettings::fund_buildings, EconomySettings::fund_roads, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), HasBit(), lengthof, Town::ratings, Town::statues, TACT_BRIBE, TACT_BUILD_STATUE, TACT_BUY_RIGHTS, TACT_FUND_BUILDINGS, TACT_ROAD_REBUILD, and Town::unwanted.

Referenced by CmdDoTownAction(), TownAuthorityWindow::DrawWidget(), TownAuthorityWindow::OnClick(), and TownAuthorityWindow::OnPaint().

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

References DistanceSquare(), Town::fund_buildings_months, and Town::xy.

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

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

References Town::population.

Referenced by TownDirectoryWindow::SetStringParameters().

template<class T >

void MakeDefaultName | ( | T * | obj | ) |

Set the default name for a depot/waypoint.

Template Parameters:

  | T | The type/class to make a default name for

Parameters:

  | obj | The object/instance we want to find the name for

Definition at line 231 of file town.h.

References ClosestTownFromTile(), HasBit(), and SetBit().

Referenced by AfterLoadGame(), CmdBuildBuoy(), CmdBuildRailWaypoint(), CmdBuildRoadDepot(), CmdBuildShipDepot(), CmdBuildTrainDepot(), and CmdRenameDepot().

void SetTownRatingTestMode | ( | bool | mode | ) |

Switch the town rating to test-mode, to allow commands to be tested without affecting current ratings.

The function is safe to use in nested calls.

Parameters:

  | mode | Test mode switch (`true` means go to test-mode, `false` means leave test-mode).

Definition at line 3174 of file town_cmd.cpp.

References SmallVector< SmallPair< T, U >, S >::Clear().

Referenced by DoCommand(), and DoCommandPInternal().

void UpdateAllTownVirtCoords | ( |  | ) |

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

Definition at line 366 of file town_cmd.cpp.

References Town::UpdateVirtCoord().

Referenced by UpdateAllVirtCoords().

void UpdateTownCargoBitmap | ( |  | ) |

Updates the bitmap of all cargoes accepted by houses.

Definition at line 747 of file town_cmd.cpp.

References _town_cargoes_accepted, and Town::cargo_accepted_total.

Referenced by UpdateHousesAndTowns().

void UpdateTownCargoes | ( | Town * | t | ) |

Update cargo acceptance for the complete town.

Parameters:

  | t | The town to update.

Definition at line 728 of file town_cmd.cpp.

References Town::cargo_accepted, Town::cargo_produced, TileMatrix< T, N >::GetArea(), INVALID_TILE, TileArea::tile, TILE_AREA_LOOP, TileX(), TileY(), UpdateTownCargoes(), and UpdateTownCargoTotal().

Referenced by AfterLoadGame(), BuildTownHouse(), UpdateHousesAndTowns(), and UpdateTownCargoes().

void UpdateTownCargoTotal | ( | Town * | t | ) |

Update the total cargo acceptance of the whole town.

Parameters:

  | t | The town to update.

Definition at line 680 of file town_cmd.cpp.

References Town::cargo_accepted, Town::cargo_accepted_total, TileMatrix< T, N >::GetArea(), TILE_AREA_LOOP, TileX(), and TileY().

Referenced by UpdateTownCargoes().

## Variable Documentation

const byte _town_action_costs[TACT_COUNT]

Factor in the cost of each town action.

See also:
  TownActions

Definition at line 2658 of file town_cmd.cpp.

Referenced by TownAuthorityWindow::DrawWidget(), and TownAuthorityWindow::UpdateWidgetSize().