OpenTTD Source 20241224-master-gf74b0cf984
town.h File Reference

Base of the town class. More...

#include "viewport_type.h"
#include "timer/timer_game_tick.h"
#include "town_map.h"
#include "subsidy_type.h"
#include "newgrf_storage.h"
#include "cargotype.h"

Go to the source code of this file.

Data Structures

struct  BuildingCounts< T >
 
struct  TownCache
 Data structure with cached data of towns. More...
 
struct  Town
 Town data structure. More...
 

Typedefs

typedef Pool< Town, TownID, 64, 64000 > TownPool
 

Enumerations

enum  TownCouncilAttitudes { TOWN_COUNCIL_LENIENT = 0 , TOWN_COUNCIL_TOLERANT = 1 , TOWN_COUNCIL_HOSTILE = 2 , TOWN_COUNCIL_PERMISSIVE = 3 }
 Settings for town council attitudes. More...
 
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  TownDirectoryInvalidateWindowData { TDIWD_FORCE_REBUILD , TDIWD_POPULATION_CHANGE , TDIWD_FORCE_RESORT }
 Special values for town list window for the data parameter of InvalidateWindowData. More...
 
enum  TownFlags { TOWN_IS_GROWING = 0 , TOWN_HAS_CHURCH = 1 , TOWN_HAS_STADIUM = 2 , TOWN_CUSTOM_GROWTH = 3 }
 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_t GetWorldPopulation ()
 Get the total population, the sum of all towns in the world.
 
void UpdateAllTownVirtCoords ()
 Update the virtual coords needed to draw the town sign for all towns.
 
void ClearAllTownCachedNames ()
 Clear the cached_name of all towns.
 
void ShowTownViewWindow (TownID town)
 
void ExpandTown (Town *t)
 
void RebuildTownKdtree ()
 
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.
 
TownCalcClosestTownFromTile (TileIndex tile, uint threshold=UINT_MAX)
 Return the town closest to the given tile within threshold.
 
void ResetHouses ()
 
void ClearTownHouse (Town *t, TileIndex tile)
 Clear a town house.
 
void UpdateTownMaxPass (Town *t)
 Update the maximum amount of montly passengers and mail for a town, based on its population.
 
void UpdateTownRadius (Town *t)
 Update the cached town zone radii of a town, based on the number of 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.
 
TownClosestTownFromTile (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.
 
TownActions GetMaskOfTownActions (CompanyID cid, const Town *t)
 Get a list of available town authority actions.
 
bool GenerateTowns (TownLayout layout)
 Generate a number of towns with a given layout.
 
const CargoSpecFindFirstCargoWithTownAcceptanceEffect (TownAcceptanceEffect effect)
 Determines the first cargo with a certain town effect.
 
CargoArray GetAcceptedCargoOfHouse (const HouseSpec *hs)
 Get accepted cargo of a house prototype.
 
template<class T >
void MakeDefaultName (T *obj)
 Set the default name for a depot/waypoint.
 
uint16_t TownTicksToGameTicks (uint16_t ticks)
 
RoadType GetTownRoadType ()
 Get the road type that towns should build at this current moment.
 
bool CheckTownRoadTypes ()
 Check if towns are able to build road.
 
std::span< const DrawBuildingsTileStructGetTownDrawTileData ()
 

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 TownID 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_t TOWN_GROWTH_RATE_NONE = 0xFFFF
 Special value for Town::growth_rate to disable town growth.
 
static const uint16_t MAX_TOWN_GROWTH_TICKS = 930
 Max amount of original town ticks that still fit into uint16_t, about equal to UINT16_MAX / TOWN_GROWTH_TICKS but slightly less to simplify calculations.
 
TownPool _town_pool
 
const uint8_t _town_action_costs [TACT_COUNT]
 Factor in the cost of each town action.
 

Detailed Description

Base of the town class.

Definition in file town.h.

Typedef Documentation

◆ TownPool

typedef Pool<Town, TownID, 64, 64000> TownPool

Definition at line 38 of file town.h.

Enumeration Type Documentation

◆ 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 council.

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

◆ TownCouncilAttitudes

Settings for town council attitudes.

Definition at line 164 of file town.h.

◆ TownDirectoryInvalidateWindowData

Special values for town list window for the data parameter of InvalidateWindowData.

Definition at line 182 of file town.h.

◆ TownFlags

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_GROWING 

Conditions for town growth are met. Grow according to Town::growth_rate.

TOWN_HAS_CHURCH 

There can be only one church by town.

TOWN_HAS_STADIUM 

There can be only one stadium by town.

TOWN_CUSTOM_GROWTH 

Growth rate is controlled by GS.

Definition at line 195 of file town.h.

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

Function Documentation

◆ CalcClosestTownFromTile()

Town * CalcClosestTownFromTile ( TileIndex  tile,
uint  threshold 
)

Return the town closest to the given tile within threshold.

Parameters
tileStarting point of the search.
thresholdBiggest allowed distance to the town.
Returns
Closest town to tile within threshold, or nullptr if there is no such town.
Note
This function only uses distance, the ClosestTownFromTile function also takes town ownership into account.

Definition at line 3878 of file town_cmd.cpp.

References DistanceManhattan(), Kdtree< T, TxyFunc, CoordT, DistT >::FindNearest(), 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(), TileX(), TileY(), and Town::xy.

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

◆ ChangeTownRating()

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

◆ CheckforTownRating()

◆ CheckIfAuthorityAllowsNewStation()

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
tileThe tile where the station shall be constructed.
flagsCommand flags. DC_NO_TEST_TOWN_RATING is tested.
Returns
Succeeded or failed command.

Definition at line 3854 of file town_cmd.cpp.

References _current_company, _settings_game, ClosestTownFromTile(), DC_NO_TEST_TOWN_RATING, GameSettings::difficulty, 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, SetDParam(), and DifficultySettings::town_council_tolerance.

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

◆ CheckTownRoadTypes()

bool CheckTownRoadTypes ( )

Check if towns are able to build road.

Returns
true iff the towns are currently able to build road.

Definition at line 1009 of file town_cmd.cpp.

References TimerGameCalendar::date, GetTownRoadTypeFirstIntroductionDate(), SetDParam(), ShowErrorMessage(), and WL_CRITICAL.

Referenced by _GenerateWorld().

◆ ClearAllTownCachedNames()

void ClearAllTownCachedNames ( )

◆ ClearTownHouse()

◆ ClosestTownFromTile()

Town * ClosestTownFromTile ( TileIndex  tile,
uint  threshold 
)

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

Parameters
tileStarting point of the search.
thresholdBiggest allowed distance to the town.
Returns
Closest town to tile within threshold, or nullptr if there is no such town.
Note
If you only care about distance, you can use the CalcClosestTownFromTile function.

Definition at line 3896 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(), BuildStationPart(), CheckAllowRemoveRoad(), CheckIfAuthorityAllowsNewStation(), CheckIfCallBackAllowsCreation(), CmdBuildAirport(), CmdBuildBridge(), CmdConvertRoad(), CmdPlantTree(), DisasterTick_Big_Ufo(), DoClearBridge(), DoClearTunnel(), FindTownForIndustry(), CHKPChunkHandler::FixPointers(), GenerateCompanyName(), AirportResolverObject::GetTown(), IndustriesResolverObject::GetTown(), ObjectResolverObject::GetTown(), StationResolverObject::GetTown(), AirportTileScopeResolver::GetVariable(), IndustryTileScopeResolver::GetVariable(), ObjectScopeResolver::GetVariable(), RailTypeScopeResolver::GetVariable(), RoadStopScopeResolver::GetVariable(), RoadTypeScopeResolver::GetVariable(), HighlightTownLocalAuthorityTiles(), MakeDefaultName(), LandInfoWindow::OnInit(), RemoveGenericRoadStop(), and SetDParamsForOwnedBy().

◆ FindFirstCargoWithTownAcceptanceEffect()

const CargoSpec * FindFirstCargoWithTownAcceptanceEffect ( TownAcceptanceEffect  effect)

Determines the first cargo with a certain town effect.

Parameters
effectTown effect of interest
Returns
first active cargo slot with that effect

Definition at line 3052 of file town_cmd.cpp.

References CargoSpec::Iterate().

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

◆ GenerateTowns()

◆ GetAcceptedCargoOfHouse()

CargoArray GetAcceptedCargoOfHouse ( const HouseSpec hs)

Get accepted cargo of a house prototype.

Parameters
hsSpec of the house.
Returns
CargoArray filled with cargo accepted by the house.

Definition at line 853 of file town_cmd.cpp.

References AddAcceptedCargoOfHouse(), HouseSpec::Index(), and INVALID_TILE.

Referenced by BuildHouseWindow::GetHouseInformation().

◆ GetHouseNorthPart()

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

References HouseSpec::Get(), and TileDiffXY().

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

◆ GetMaskOfTownActions()

TownActions GetMaskOfTownActions ( CompanyID  cid,
const Town t 
)

◆ GetTownDrawTileData()

std::span< const DrawBuildingsTileStruct > GetTownDrawTileData ( )

Definition at line 4125 of file town_cmd.cpp.

◆ GetTownRadiusGroup()

HouseZonesBits GetTownRadiusGroup ( const Town t,
TileIndex  tile 
)

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

Parameters
tTown on which town zone is to be found.
tileTileIndex where town zone needs to be found.
Returns
the bit position of the given zone, as defined in HouseZones.

Definition at line 2474 of file town_cmd.cpp.

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

Referenced by AirportTileScopeResolver::GetVariable(), HouseScopeResolver::GetVariable(), IndustriesScopeResolver::GetVariable(), IndustryTileScopeResolver::GetVariable(), ObjectScopeResolver::GetVariable(), RailTypeScopeResolver::GetVariable(), RoadStopScopeResolver::GetVariable(), RoadTypeScopeResolver::GetVariable(), and TryBuildTownHouse().

◆ GetTownRoadType()

◆ GetWorldPopulation()

uint32_t GetWorldPopulation ( )

Get the total population, the sum of all towns in the world.

Returns
The calculated population of the world

Definition at line 462 of file town_cmd.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_town_pool >::Iterate().

Referenced by TownDirectoryWindow::SetStringParameters().

◆ MakeDefaultName()

template<class T >
void MakeDefaultName ( T *  obj)

Set the default name for a depot/waypoint.

Template Parameters
TThe type/class to make a default name for
Parameters
objThe object/instance we want to find the name for

Definition at line 254 of file town.h.

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

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

◆ RebuildTownKdtree()

void RebuildTownKdtree ( )

Definition at line 73 of file town_cmd.cpp.

◆ ResetHouses()

void ResetHouses ( )

Definition at line 80 of file newgrf_house.cpp.

◆ SetTownRatingTestMode()

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
modeTest mode switch (true means go to test-mode, false means leave test-mode).

Definition at line 3937 of file town_cmd.cpp.

References _town_rating_test, and _town_test_ratings.

Referenced by CommandHelperBase::InternalDoAfter(), CommandHelperBase::InternalDoBefore(), CommandHelperBase::InternalExecutePrepTest(), and CommandHelperBase::InternalExecuteValidateTestAndPrepExec().

◆ ShowTownViewWindow()

void ShowTownViewWindow ( TownID  town)

Definition at line 689 of file town_gui.cpp.

◆ TownTicksToGameTicks()

uint16_t TownTicksToGameTicks ( uint16_t  ticks)
inline

Definition at line 316 of file town.h.

◆ UpdateAllTownVirtCoords()

void UpdateAllTownVirtCoords ( )

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

Definition at line 427 of file town_cmd.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_town_pool >::Iterate().

Referenced by UpdateAllVirtCoords().

◆ UpdateTownMaxPass()

void UpdateTownMaxPass ( Town t)

Update the maximum amount of montly passengers and mail for a town, based on its population.

Parameters
tThe town to update.

Definition at line 1993 of file town_cmd.cpp.

References Town::cache, TransportedCargoStat< Tstorage >::old_max, TownCache::population, ScaleByCargoScale(), Town::supplied, CargoSpec::town_production_cargoes, TPE_MAIL, and TPE_PASSENGERS.

Referenced by AfterLoadGame(), CmdExpandTown(), and DoCreateTown().

◆ UpdateTownRadius()

void UpdateTownRadius ( Town t)

Update the cached town zone radii of a town, based on the number of houses.

Parameters
tThe town to update.

Definition at line 1946 of file town_cmd.cpp.

References Town::cache, TownCache::num_houses, and TownCache::squared_town_zone_radius.

Referenced by BuildTownHouse(), ClearTownHouse(), CmdExpandTown(), DoCreateTown(), and RebuildTownCaches().

Variable Documentation

◆ _town_action_costs

const uint8_t _town_action_costs[TACT_COUNT]
extern

Factor in the cost of each town action.

See also
TownActions

Definition at line 3314 of file town_cmd.cpp.

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

◆ CUSTOM_TOWN_MAX_NUMBER

const uint CUSTOM_TOWN_MAX_NUMBER = 5000
static

this is the maximum number of towns a user can specify in customisation

Definition at line 29 of file town.h.

Referenced by GenerateLandscapeWindow::OnQueryTextFinished(), and GenerateLandscapeWindow::UpdateWidgetSize().

◆ CUSTOM_TOWN_NUMBER_DIFFICULTY

const uint CUSTOM_TOWN_NUMBER_DIFFICULTY = 4
static

value for custom town number in difficulty settings

Definition at line 28 of file town.h.

Referenced by GenerateTowns(), GenerateLandscapeWindow::OnDropdownSelect(), and GenerateLandscapeWindow::SetStringParameters().

◆ INVALID_TOWN

const TownID INVALID_TOWN = 0xFFFF
static

Definition at line 31 of file town.h.

◆ MAX_TOWN_GROWTH_TICKS

const uint16_t MAX_TOWN_GROWTH_TICKS = 930
static

Max amount of original town ticks that still fit into uint16_t, about equal to UINT16_MAX / TOWN_GROWTH_TICKS but slightly less to simplify calculations.

Definition at line 36 of file town.h.

◆ TOWN_GROWTH_DESERT

const uint TOWN_GROWTH_DESERT = 0xFFFFFFFF
static

The town needs the cargo for growth when on desert (any amount)

Definition at line 34 of file town.h.

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

◆ TOWN_GROWTH_RATE_NONE

const uint16_t TOWN_GROWTH_RATE_NONE = 0xFFFF
static

Special value for Town::growth_rate to disable town growth.

Definition at line 35 of file town.h.

Referenced by AfterLoadGame(), UpdateTownGrowCounter(), and UpdateTownGrowth().

◆ TOWN_GROWTH_WINTER

const uint TOWN_GROWTH_WINTER = 0xFFFFFFFE
static

The town only needs this cargo in the winter (any amount)

Definition at line 33 of file town.h.

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