Files
@ r28530:9741a9739150
Branch filter:
Location: cpp/openttd-patchpack/source/src/town.h - annotation
r28530:9741a9739150
12.0 KiB
text/x-c
Fix bd85f61a: [Linux] don't include sys/random.h on older glibc systems (#11844)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r9111:983de9c5a848 r6422:5983361e241a r5475:3f5cd13d1b63 r5475:3f5cd13d1b63 r5475:3f5cd13d1b63 r9127:933c13888121 r27883:ddbd33508a8a r12239:4ede50b8e95b r12648:24c912bd53f6 r17773:70894fef31f4 r18444:c1e881f954d6 r6332:7393965b5322 r11396:4adf79ba94c4 r6332:7393965b5322 r20794:ace4f2fce9a6 r11396:4adf79ba94c4 r5475:3f5cd13d1b63 r5475:3f5cd13d1b63 r11342:c131f3f0617b r11342:c131f3f0617b r11340:d21bd0c025b2 r23138:948aff8483b9 r12239:4ede50b8e95b r18446:7b57061bbb9d r18446:7b57061bbb9d r27737:728d55b97775 r27737:728d55b97775 r18446:7b57061bbb9d r11967:df0600d2c7e7 r11967:df0600d2c7e7 r7386:610d1afd3946 r19276:ad854f3053cd r19276:ad854f3053cd r27737:728d55b97775 r27737:728d55b97775 r23942:2580b984b22e r23677:3b8ed1534593 r27737:728d55b97775 r27737:728d55b97775 r19276:ad854f3053cd r19276:ad854f3053cd r15606:aa81fc543ca1 r11967:df0600d2c7e7 r18059:522168673869 r5475:3f5cd13d1b63 r19276:ad854f3053cd r5475:3f5cd13d1b63 r6422:5983361e241a r27737:728d55b97775 r27737:728d55b97775 r27737:728d55b97775 r24214:a65c412aafcc r24054:96f94ee4b3e1 r5475:3f5cd13d1b63 r20770:032049fe3e4c r5475:3f5cd13d1b63 r27737:728d55b97775 r9334:6d079081ec24 r18059:522168673869 r5475:3f5cd13d1b63 r18059:522168673869 r18059:522168673869 r27737:728d55b97775 r23674:a5bafa1a02c5 r27737:728d55b97775 r27737:728d55b97775 r5475:3f5cd13d1b63 r27737:728d55b97775 r27737:728d55b97775 r27737:728d55b97775 r5475:3f5cd13d1b63 r24214:a65c412aafcc r18768:3747fd5dec42 r18444:c1e881f954d6 r5475:3f5cd13d1b63 r23431:60a46e3b7cd7 r18550:9defbb1193d0 r27737:728d55b97775 r5475:3f5cd13d1b63 r27737:728d55b97775 r27737:728d55b97775 r5475:3f5cd13d1b63 r23664:6060dcf415e4 r23664:6060dcf415e4 r5475:3f5cd13d1b63 r23664:6060dcf415e4 r23664:6060dcf415e4 r6486:0604267ae5f1 r23800:f8cc74842443 r23800:f8cc74842443 r17773:70894fef31f4 r17773:70894fef31f4 r7386:610d1afd3946 r18059:522168673869 r18059:522168673869 r7386:610d1afd3946 r11967:df0600d2c7e7 r7386:610d1afd3946 r18059:522168673869 r7386:610d1afd3946 r7386:610d1afd3946 r11002:ca78de6e5555 r8796:7940548db691 r15606:aa81fc543ca1 r15606:aa81fc543ca1 r9334:6d079081ec24 r15606:aa81fc543ca1 r15606:aa81fc543ca1 r15606:aa81fc543ca1 r27737:728d55b97775 r11000:d03bad32156f r19276:ad854f3053cd r9334:6d079081ec24 r15606:aa81fc543ca1 r28275:8873154cda89 r9334:6d079081ec24 r12240:9443a1e602a7 r12381:5e06b887e97b r12381:5e06b887e97b r27202:5a7c474a171e r24054:96f94ee4b3e1 r27202:5a7c474a171e r24054:96f94ee4b3e1 r27202:5a7c474a171e r24054:96f94ee4b3e1 r24054:96f94ee4b3e1 r18782:6453522c2154 r12240:9443a1e602a7 r12240:9443a1e602a7 r12240:9443a1e602a7 r12242:f11b2e8973ab r12242:f11b2e8973ab r12316:322a47b70dfd r24054:96f94ee4b3e1 r24054:96f94ee4b3e1 r24054:96f94ee4b3e1 r5475:3f5cd13d1b63 r5475:3f5cd13d1b63 r27737:728d55b97775 r5475:3f5cd13d1b63 r7969:47922a82c8f7 r24054:96f94ee4b3e1 r5475:3f5cd13d1b63 r5475:3f5cd13d1b63 r5475:3f5cd13d1b63 r23435:0f8f3f8f40ec r23435:0f8f3f8f40ec r26201:fc753b42fb89 r26201:fc753b42fb89 r26201:fc753b42fb89 r26201:fc753b42fb89 r26201:fc753b42fb89 r26201:fc753b42fb89 r26201:fc753b42fb89 r23435:0f8f3f8f40ec r15610:623a23fb6560 r15610:623a23fb6560 r14799:0bcfe312e825 r14799:0bcfe312e825 r11874:0c1573de8851 r14799:0bcfe312e825 r14799:0bcfe312e825 r14799:0bcfe312e825 r5475:3f5cd13d1b63 r5475:3f5cd13d1b63 r23786:da4f1a3ef2bb r23786:da4f1a3ef2bb r23786:da4f1a3ef2bb r24052:244e2e7792f4 r24027:b9909505e530 r23786:da4f1a3ef2bb r23786:da4f1a3ef2bb r15610:623a23fb6560 r15645:b579711f0ab7 r5475:3f5cd13d1b63 r5475:3f5cd13d1b63 r5475:3f5cd13d1b63 r12208:4a6d754234c5 r5475:3f5cd13d1b63 r15173:a59afd6301a6 r20878:f8e3794f90de r6422:5983361e241a r18737:1ee35da7fa2f r22857:c20a34513570 r5475:3f5cd13d1b63 r5475:3f5cd13d1b63 r14800:3e915e992538 r5475:3f5cd13d1b63 r6332:7393965b5322 r10269:00d11ecda622 r9594:e12e48163543 r12316:322a47b70dfd r5969:076c978402fa r6332:7393965b5322 r6332:7393965b5322 r13072:40bbf04b49b4 r13072:40bbf04b49b4 r13072:40bbf04b49b4 r13072:40bbf04b49b4 r13072:40bbf04b49b4 r13072:40bbf04b49b4 r13072:40bbf04b49b4 r13072:40bbf04b49b4 r13072:40bbf04b49b4 r18442:52be4e794608 r13072:40bbf04b49b4 r19944:25a78576fb5e r13072:40bbf04b49b4 r13072:40bbf04b49b4 r13072:40bbf04b49b4 r13072:40bbf04b49b4 r18442:52be4e794608 r13072:40bbf04b49b4 r13072:40bbf04b49b4 r13072:40bbf04b49b4 r14900:c679fdaeebe6 r13072:40bbf04b49b4 r26538:db23b44cf1a6 r26538:db23b44cf1a6 r26538:db23b44cf1a6 r26538:db23b44cf1a6 r26538:db23b44cf1a6 r26538:db23b44cf1a6 r26538:db23b44cf1a6 r26538:db23b44cf1a6 r26538:db23b44cf1a6 r26538:db23b44cf1a6 r26538:db23b44cf1a6 r26538:db23b44cf1a6 r13072:40bbf04b49b4 r13072:40bbf04b49b4 r8796:7940548db691 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r16110:0eb4e685b16c r24214:a65c412aafcc r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r27737:728d55b97775 r27737:728d55b97775 r27737:728d55b97775 r27737:728d55b97775 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r23607:36c15679007d r16110:0eb4e685b16c r16110:0eb4e685b16c r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r15159:901498571693 r27737:728d55b97775 r15159:901498571693 r15159:901498571693 r22857:c20a34513570 r22857:c20a34513570 r22857:c20a34513570 r22857:c20a34513570 r28410:d9c73d685bbc r28127:a210f6ce658a r27883:ddbd33508a8a r22857:c20a34513570 r22857:c20a34513570 r22857:c20a34513570 r27944:a5c70dad53db r23698:1872cc5b7dd7 r5475:3f5cd13d1b63 | /*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file town.h Base of the town class. */
#ifndef TOWN_H
#define TOWN_H
#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"
template <typename T>
struct BuildingCounts {
T id_count[NUM_HOUSES];
T class_count[HOUSE_CLASS_MAX];
};
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
typedef Pool<Town, TownID, 64, 64000> TownPool;
extern TownPool _town_pool;
/** Data structure with cached data of towns. */
struct TownCache {
uint32_t num_houses; ///< Amount of houses
uint32_t population; ///< Current population of people
TrackedViewportSign sign; ///< Location of name sign, UpdateVirtCoord updates this
PartOfSubsidy part_of_subsidy; ///< Is this town a source/destination of a subsidy?
uint32_t squared_town_zone_radius[HZB_END]; ///< UpdateTownRadius updates this given the house count
BuildingCounts<uint16_t> building_counts; ///< The number of each type of building in the town
};
/** Town data structure. */
struct Town : TownPool::PoolItem<&_town_pool> {
TileIndex xy; ///< town center tile
TownCache cache; ///< Container for all cacheable data.
/* Town name */
uint32_t townnamegrfid;
uint16_t townnametype;
uint32_t townnameparts;
std::string name; ///< Custom town name. If empty, the town was not renamed and uses the generated name.
mutable std::string cached_name; ///< NOSAVE: Cache of the resolved name of the town, if not using a custom town name
byte flags; ///< See #TownFlags.
uint16_t noise_reached; ///< level of noise that all the airports are generating
CompanyMask statues; ///< which companies have a statue?
/* Company ratings. */
CompanyMask have_ratings; ///< which companies have a rating
uint8_t unwanted[MAX_COMPANIES]; ///< how many months companies aren't wanted by towns (bribe)
CompanyID exclusivity; ///< which company has exclusivity
uint8_t exclusive_counter; ///< months till the exclusivity expires
int16_t ratings[MAX_COMPANIES]; ///< ratings of each company for this town
TransportedCargoStat<uint32_t> supplied[NUM_CARGO]; ///< Cargo statistics about supplied cargo.
TransportedCargoStat<uint16_t> received[NUM_TE]; ///< Cargo statistics about received cargotypes.
uint32_t goal[NUM_TE]; ///< Amount of cargo required for the town to grow.
std::string text; ///< General text with additional information.
inline byte GetPercentTransported(CargoID cid) const { return this->supplied[cid].old_act * 256 / (this->supplied[cid].old_max + 1); }
StationList stations_near; ///< NOSAVE: List of nearby stations.
uint16_t time_until_rebuild; ///< time until we rebuild a house
uint16_t grow_counter; ///< counter to count when to grow, value is smaller than or equal to growth_rate
uint16_t growth_rate; ///< town growth rate
byte fund_buildings_months; ///< fund buildings program in action?
byte road_build_months; ///< fund road reconstruction in action?
bool larger_town; ///< if this is a larger town and should grow more quickly
TownLayout layout; ///< town specific road layout
bool show_zone; ///< NOSAVE: mark town to show the local authority zone in the viewports
std::list<PersistentStorage *> psa_list;
/**
* Creates a new town.
* @param tile center tile of the town
*/
Town(TileIndex tile = INVALID_TILE) : xy(tile) { }
/** Destroy the town. */
~Town();
void InitializeLayout(TownLayout layout);
/**
* Calculate the max town noise.
* The value is counted using the population divided by the content of the
* entry in town_noise_population corresponding to the town's tolerance.
* @return the maximum noise level the town will tolerate.
*/
inline uint16_t MaxTownNoise() const
{
if (this->cache.population == 0) return 0; // no population? no noise
/* 3 is added (the noise of the lowest airport), so the user can at least build a small airfield. */
return ClampTo<uint16_t>((this->cache.population / _settings_game.economy.town_noise_population[_settings_game.difficulty.town_council_tolerance]) + 3);
}
void UpdateVirtCoord();
inline const std::string &GetCachedName() const
{
if (!this->name.empty()) return this->name;
if (this->cached_name.empty()) this->FillCachedName();
return this->cached_name;
}
static inline Town *GetByTile(TileIndex tile)
{
return Town::Get(GetTownIndex(tile));
}
static Town *GetRandom();
static void PostDestructor(size_t index);
private:
void FillCachedName() const;
};
uint32_t GetWorldPopulation();
void UpdateAllTownVirtCoords();
void ClearAllTownCachedNames();
void ShowTownViewWindow(TownID town);
void ExpandTown(Town *t);
void RebuildTownKdtree();
/** Settings for town council attitudes. */
enum TownCouncilAttitudes {
TOWN_COUNCIL_LENIENT = 0,
TOWN_COUNCIL_TOLERANT = 1,
TOWN_COUNCIL_HOSTILE = 2,
TOWN_COUNCIL_PERMISSIVE = 3,
};
/**
* Action types that a company must ask permission for to a town authority.
* @see CheckforTownRating
*/
enum TownRatingCheckType {
ROAD_REMOVE = 0, ///< Removal of a road owned by the town.
TUNNELBRIDGE_REMOVE = 1, ///< Removal of a tunnel or bridge owned by the towb.
TOWN_RATING_CHECK_TYPE_COUNT, ///< Number of town checking action types.
};
/** Special values for town list window for the data parameter of #InvalidateWindowData. */
enum TownDirectoryInvalidateWindowData {
TDIWD_FORCE_REBUILD,
TDIWD_POPULATION_CHANGE,
TDIWD_FORCE_RESORT,
};
/**
* 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...
*/
enum TownFlags {
TOWN_IS_GROWING = 0, ///< Conditions for town growth are met. Grow according to Town::growth_rate.
TOWN_HAS_CHURCH = 1, ///< There can be only one church by town.
TOWN_HAS_STADIUM = 2, ///< There can be only one stadium by town.
TOWN_CUSTOM_GROWTH = 3, ///< Growth rate is controlled by GS.
};
CommandCost CheckforTownRating(DoCommandFlag flags, Town *t, TownRatingCheckType type);
TileIndexDiff GetHouseNorthPart(HouseID &house);
Town *CalcClosestTownFromTile(TileIndex tile, uint threshold = UINT_MAX);
void ResetHouses();
/** Town actions of a company. */
enum TownActions {
TACT_NONE = 0x00, ///< Empty action set.
TACT_ADVERTISE_SMALL = 0x01, ///< Small advertising campaign.
TACT_ADVERTISE_MEDIUM = 0x02, ///< Medium advertising campaign.
TACT_ADVERTISE_LARGE = 0x04, ///< Large advertising campaign.
TACT_ROAD_REBUILD = 0x08, ///< Rebuild the roads.
TACT_BUILD_STATUE = 0x10, ///< Build a statue.
TACT_FUND_BUILDINGS = 0x20, ///< Fund new buildings.
TACT_BUY_RIGHTS = 0x40, ///< Buy exclusive transport rights.
TACT_BRIBE = 0x80, ///< Try to bribe the council.
TACT_COUNT = 8, ///< Number of available town actions.
TACT_ADVERTISE = TACT_ADVERTISE_SMALL | TACT_ADVERTISE_MEDIUM | TACT_ADVERTISE_LARGE, ///< All possible advertising actions.
TACT_CONSTRUCTION = TACT_ROAD_REBUILD | TACT_BUILD_STATUE | TACT_FUND_BUILDINGS, ///< All possible construction actions.
TACT_FUNDS = TACT_BUY_RIGHTS | TACT_BRIBE, ///< All possible funding actions.
TACT_ALL = TACT_ADVERTISE | TACT_CONSTRUCTION | TACT_FUNDS, ///< All possible actions.
};
DECLARE_ENUM_AS_BIT_SET(TownActions)
void ClearTownHouse(Town *t, TileIndex tile);
void UpdateTownMaxPass(Town *t);
void UpdateTownRadius(Town *t);
CommandCost CheckIfAuthorityAllowsNewStation(TileIndex tile, DoCommandFlag flags);
Town *ClosestTownFromTile(TileIndex tile, uint threshold);
void ChangeTownRating(Town *t, int add, int max, DoCommandFlag flags);
HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile);
void SetTownRatingTestMode(bool mode);
TownActions GetMaskOfTownActions(CompanyID cid, const Town *t);
bool GenerateTowns(TownLayout layout);
const CargoSpec *FindFirstCargoWithTownEffect(TownEffect effect);
extern const byte _town_action_costs[TACT_COUNT];
/**
* Set the default name for a depot/waypoint
* @tparam T The type/class to make a default name for
* @param obj The object/instance we want to find the name for
*/
template <class T>
void MakeDefaultName(T *obj)
{
/* We only want to set names if it hasn't been set before, or when we're calling from afterload. */
assert(obj->name.empty() || obj->town_cn == UINT16_MAX);
obj->town = ClosestTownFromTile(obj->xy, UINT_MAX);
/* Find first unused number belonging to this town. This can never fail,
* as long as there can be at most 65535 waypoints/depots in total.
*
* This does 'n * m' search, but with 32bit 'used' bitmap, it needs at
* most 'n * (1 + ceil(m / 32))' steps (n - number of waypoints in pool,
* m - number of waypoints near this town).
* Usually, it needs only 'n' steps.
*
* If it wasn't using 'used' and 'idx', it would just search for increasing 'next',
* but this way it is faster */
uint32_t used = 0; // bitmap of used waypoint numbers, sliding window with 'next' as base
uint32_t next = 0; // first number in the bitmap
uint32_t idx = 0; // index where we will stop
uint32_t cid = 0; // current index, goes to T::GetPoolSize()-1, then wraps to 0
do {
T *lobj = T::GetIfValid(cid);
/* check only valid waypoints... */
if (lobj != nullptr && obj != lobj) {
/* only objects within the same city and with the same type */
if (lobj->town == obj->town && lobj->IsOfType(obj)) {
/* if lobj->town_cn < next, uint will overflow to '+inf' */
uint i = (uint)lobj->town_cn - next;
if (i < 32) {
SetBit(used, i); // update bitmap
if (i == 0) {
/* shift bitmap while the lowest bit is '1';
* increase the base of the bitmap too */
do {
used >>= 1;
next++;
} while (HasBit(used, 0));
/* when we are at 'idx' again at end of the loop and
* 'next' hasn't changed, then no object had town_cn == next,
* so we can safely use it */
idx = cid;
}
}
}
}
cid++;
if (cid == T::GetPoolSize()) cid = 0; // wrap to zero...
} while (cid != idx);
obj->town_cn = (uint16_t)next; // set index...
}
/*
* Converts original town ticks counters to plain game ticks. Note that
* tick 0 is a valid tick so actual amount is one more than the counter value.
*/
inline uint16_t TownTicksToGameTicks(uint16_t ticks)
{
return (std::min(ticks, MAX_TOWN_GROWTH_TICKS) + 1) * Ticks::TOWN_GROWTH_TICKS - 1;
}
RoadType GetTownRoadType();
#endif /* TOWN_H */
|