tron@2186: /* $Id$ */ tron@2186: rubidium@10455: /** @file town.h Base of the town class. */ glx@9574: truelight@0: #ifndef TOWN_H truelight@0: #define TOWN_H truelight@0: matthijs@5216: #include "oldpool.h" rubidium@9869: #include "core/bitmath_func.hpp" rubidium@9723: #include "core/random_func.hpp" rubidium@9723: #include "cargo_type.h" rubidium@9723: #include "tile_type.h" rubidium@9723: #include "date_type.h" rubidium@9723: #include "town_type.h" rubidium@9724: #include "player_type.h" rubidium@9869: #include "settings_type.h" rubidium@10249: #include "strings_type.h" rubidium@10455: #include "viewport_type.h" rubidium@10867: #include "economy_type.h" truelight@0: rubidium@5299: enum { truelight@9476: HOUSE_NO_CLASS = 0, truelight@9476: NEW_HOUSE_OFFSET = 110, truelight@9476: HOUSE_MAX = 512, truelight@9476: INVALID_TOWN = 0xFFFF, truelight@9476: INVALID_HOUSE_ID = 0xFFFF, truelight@9476: truelight@9476: /* There can only be as many classes as there are new houses, plus one for truelight@9476: * NO_CLASS, as the original houses don't have classes. */ truelight@9476: HOUSE_CLASS_MAX = HOUSE_MAX - NEW_HOUSE_OFFSET + 1, truelight@9476: }; truelight@9476: truelight@9476: enum BuildingFlags { truelight@9476: TILE_NO_FLAG = 0, truelight@9476: TILE_SIZE_1x1 = 1U << 0, truelight@9476: TILE_NOT_SLOPED = 1U << 1, truelight@9476: TILE_SIZE_2x1 = 1U << 2, truelight@9476: TILE_SIZE_1x2 = 1U << 3, truelight@9476: TILE_SIZE_2x2 = 1U << 4, truelight@9476: BUILDING_IS_ANIMATED = 1U << 5, truelight@9476: BUILDING_IS_CHURCH = 1U << 6, truelight@9476: BUILDING_IS_STADIUM = 1U << 7, truelight@9476: BUILDING_HAS_1_TILE = TILE_SIZE_1x1 | TILE_SIZE_2x1 | TILE_SIZE_1x2 | TILE_SIZE_2x2, truelight@9476: BUILDING_2_TILES_X = TILE_SIZE_2x1 | TILE_SIZE_2x2, truelight@9476: BUILDING_2_TILES_Y = TILE_SIZE_1x2 | TILE_SIZE_2x2, truelight@9476: BUILDING_HAS_4_TILES = TILE_SIZE_2x2, truelight@9476: }; truelight@9476: truelight@9476: DECLARE_ENUM_AS_BIT_SET(BuildingFlags) truelight@9476: rubidium@9724: enum HouseZonesBits { rubidium@9869: HZB_BEGIN = 0, rubidium@9724: HZB_TOWN_EDGE = 0, rubidium@9724: HZB_TOWN_OUTSKIRT, rubidium@9724: HZB_TOWN_OUTER_SUBURB, rubidium@9724: HZB_TOWN_INNER_SUBURB, rubidium@9724: HZB_TOWN_CENTRE, rubidium@9869: HZB_END, rubidium@9724: }; rubidium@9869: assert_compile(HZB_END == 5); rubidium@9869: rubidium@9869: DECLARE_POSTFIX_INCREMENT(HouseZonesBits) rubidium@9724: truelight@9476: enum HouseZones { ///< Bit Value Meaning truelight@9476: HZ_NOZNS = 0x0000, ///< 0 This is just to get rid of zeros, meaning none rubidium@9724: HZ_ZON1 = 1U << HZB_TOWN_EDGE, ///< 0..4 1,2,4,8,10 which town zones the building can be built in, Zone1 been the further suburb rubidium@9724: HZ_ZON2 = 1U << HZB_TOWN_OUTSKIRT, rubidium@9724: HZ_ZON3 = 1U << HZB_TOWN_OUTER_SUBURB, rubidium@9724: HZ_ZON4 = 1U << HZB_TOWN_INNER_SUBURB, rubidium@9724: HZ_ZON5 = 1U << HZB_TOWN_CENTRE, ///< center of town truelight@9476: HZ_ZONALL = 0x001F, ///< 1F This is just to englobe all above types at once truelight@9476: HZ_SUBARTC_ABOVE = 0x0800, ///< 11 800 can appear in sub-arctic climate above the snow line truelight@9476: HZ_TEMP = 0x1000, ///< 12 1000 can appear in temperate climate truelight@9476: HZ_SUBARTC_BELOW = 0x2000, ///< 13 2000 can appear in sub-arctic climate below the snow line truelight@9476: HZ_SUBTROPIC = 0x4000, ///< 14 4000 can appear in subtropical climate truelight@9476: HZ_TOYLND = 0x8000 ///< 15 8000 can appear in toyland climate truelight@9476: }; truelight@9476: truelight@9476: DECLARE_ENUM_AS_BIT_SET(HouseZones) truelight@9476: truelight@9476: enum HouseExtraFlags { truelight@9476: NO_EXTRA_FLAG = 0, truelight@9476: BUILDING_IS_HISTORICAL = 1U << 0, ///< this house will only appear during town generation in random games, thus the historical truelight@9476: BUILDING_IS_PROTECTED = 1U << 1, ///< towns and AI will not remove this house, while human players will be able tp truelight@9476: SYNCHRONISED_CALLBACK_1B = 1U << 2, ///< synchronized callback 1B will be performed, on multi tile houses truelight@9476: CALLBACK_1A_RANDOM_BITS = 1U << 3, ///< callback 1A needs random bits truelight@9476: }; truelight@9476: truelight@9476: DECLARE_ENUM_AS_BIT_SET(HouseExtraFlags) truelight@9476: truelight@9476: struct BuildingCounts { truelight@9476: uint8 id_count[HOUSE_MAX]; truelight@9476: uint8 class_count[HOUSE_CLASS_MAX]; rubidium@5299: }; rubidium@5299: rubidium@9694: DECLARE_OLD_POOL(Town, Town, 3, 8000) rubidium@9694: rubidium@9694: struct Town : PoolItem { truelight@0: TileIndex xy; truelight@0: glx@9574: /* Current population of people and amount of houses. */ rubidium@9826: uint32 num_houses; truelight@0: uint32 population; truelight@193: glx@9574: /* Town name */ glx@9629: uint32 townnamegrfid; truelight@0: uint16 townnametype; truelight@0: uint32 townnameparts; rubidium@9724: char *name; truelight@193: glx@9574: /* NOSAVE: Location of name sign, UpdateTownVirtCoord updates this. */ truelight@0: ViewportSign sign; truelight@193: glx@9574: /* Makes sure we don't build certain house types twice. glx@9574: * bit 0 = Building funds received glx@9574: * bit 1 = CHURCH glx@9574: * bit 2 = STADIUM */ truelight@0: byte flags12; truelight@0: rubidium@10715: /* level of noise that all the airports are generating */ rubidium@10715: uint16 noise_reached; rubidium@10715: glx@9574: /* Which players have a statue? */ truelight@0: byte statues; truelight@0: glx@9574: /* Player ratings as well as a mask that determines which players have a rating. */ truelight@0: byte have_ratings; glx@9574: uint8 unwanted[MAX_PLAYERS]; ///< how many months companies aren't wanted by towns (bribe) glx@9574: PlayerByte exclusivity; ///< which player has exslusivity glx@9574: uint8 exclusive_counter; ///< months till the exclusivity expires truelight@0: int16 ratings[MAX_PLAYERS]; truelight@193: glx@9574: /* Maximum amount of passengers and mail that can be transported. */ celestar@1377: uint32 max_pass; celestar@1377: uint32 max_mail; celestar@1377: uint32 new_max_pass; celestar@1377: uint32 new_max_mail; celestar@1377: uint32 act_pass; celestar@1377: uint32 act_mail; celestar@1377: uint32 new_act_pass; celestar@1377: uint32 new_act_mail; truelight@0: glx@9574: /* Amount of passengers that were transported. */ truelight@0: byte pct_pass_transported; truelight@0: byte pct_mail_transported; truelight@0: glx@9574: /* Amount of food and paper that was transported. Actually a bit mask would be enough. */ truelight@0: uint16 act_food; darkvater@4: uint16 act_water; truelight@0: uint16 new_act_food; darkvater@4: uint16 new_act_water; truelight@193: glx@9574: /* Time until we rebuild a house. */ rubidium@9599: uint16 time_until_rebuild; truelight@0: glx@9574: /* When to grow town next time. */ rubidium@9599: uint16 grow_counter; rubidium@9599: int16 growth_rate; truelight@0: glx@9574: /* Fund buildings program in action? */ truelight@0: byte fund_buildings_months; truelight@193: glx@9574: /* Fund road reconstruction in action? */ truelight@0: byte road_build_months; truelight@0: rubidium@9601: /* If this is a larger town, and should grow more quickly. */ rubidium@9601: bool larger_town; rubidium@9601: glx@9574: /* NOSAVE: UpdateTownRadius updates this given the house count. */ rubidium@10249: uint32 squared_town_zone_radius[HZB_END]; truelight@9476: glx@9574: /* NOSAVE: The number of each type of building in the town. */ truelight@9476: BuildingCounts building_counts; rubidium@9694: rubidium@9869: /* NOSAVE: The town specific road layout */ rubidium@9869: TownLayout layout; rubidium@9869: rubidium@9694: /** rubidium@9694: * Creates a new town rubidium@9694: */ rubidium@9694: Town(TileIndex tile = 0); rubidium@9694: rubidium@9694: /** Destroy the town */ rubidium@9694: ~Town(); rubidium@9694: rubidium@9701: inline bool IsValid() const { return this->xy != 0; } rubidium@9869: rubidium@9869: void InitializeLayout(); rubidium@9869: rubidium@9869: inline TownLayout GetActiveLayout() const; rubidium@10715: rubidium@10715: /** Calculate the max town noise rubidium@10715: * The value is counted using the population divided by the content of the rubidium@10715: * entry in town_noise_population corespondig to the town's tolerance. rubidium@10715: * To this result, we add 3, which is the noise of the lowest airport. rubidium@10715: * So user can at least buld that airport rubidium@10715: * @return the maximum noise level the town will tolerate */ rubidium@10715: inline uint16 MaxTownNoise() const { rubidium@10715: if (this->population == 0) return 0; // no population? no noise rubidium@10715: glx@10776: return ((this->population / _settings_game.economy.town_noise_population[_settings_game.difficulty.town_council_tolerance]) + 3); rubidium@10715: } truelight@0: }; truelight@0: rubidium@9869: /** rubidium@9869: * Get the current valid layout for the town rubidium@9869: * @return the active layout for this town rubidium@9869: */ rubidium@9869: inline TownLayout Town::GetActiveLayout() const rubidium@9869: { glx@10776: return (_settings_game.economy.town_layout == TL_RANDOM) ? this->layout : _settings_game.economy.town_layout; rubidium@9869: } rubidium@9869: truelight@9476: struct HouseSpec { truelight@9476: /* Standard properties */ glx@10829: Year min_year; ///< introduction year of the house glx@10829: Year max_year; ///< last year it can be built truelight@9476: byte population; ///< population (Zero on other tiles in multi tile house.) truelight@9476: byte removal_cost; ///< cost multiplier for removing it rubidium@10249: StringID building_name; ///< building name truelight@9476: uint16 remove_rating_decrease; ///< rating decrease if removed truelight@9476: byte mail_generation; ///< mail generation multiplier (tile based, as the acceptances below) glx@9505: byte cargo_acceptance[3]; ///< acceptance level for the cargo slots glx@9505: CargoID accepts_cargo[3]; ///< 3 input cargo slots truelight@9476: BuildingFlags building_flags; ///< some flags that describe the house (size, stadium etc...) truelight@9476: HouseZones building_availability; ///< where can it be built (climates, zones) glx@9624: bool enabled; ///< the house is available to build (true by default, but can be disabled by newgrf) truelight@9476: truelight@9476: /* NewHouses properties */ glx@9624: HouseID substitute_id; ///< which original house this one is based on truelight@9476: struct SpriteGroup *spritegroup; ///< pointer to the different sprites of the house truelight@9476: HouseID override; ///< which house this one replaces truelight@9476: uint16 callback_mask; ///< House callback flags truelight@9476: byte random_colour[4]; ///< 4 "random" colours truelight@9476: byte probability; ///< Relative probability of appearing (16 is the standard value) truelight@9476: HouseExtraFlags extra_flags; ///< some more flags truelight@9476: HouseClassID class_id; ///< defines the class this house has (grf file based) @See HouseGetVariable, prop 0x44 truelight@9476: byte animation_frames; ///< number of animation frames truelight@9476: byte animation_speed; ///< amount of time between each of those frames truelight@9476: byte processing_time; ///< Periodic refresh multiplier glx@9624: byte minimum_life; ///< The minimum number of years this house will survive before the town rebuilds it truelight@9476: truelight@9476: /* grf file related properties*/ truelight@9476: uint8 local_id; ///< id defined by the grf file for this house truelight@9476: const struct GRFFile *grffile; ///< grf file that introduced this house rubidium@10867: rubidium@10867: /** rubidium@10867: * Get the cost for removing this house rubidium@10867: * @return the cost (inflation corrected etc) rubidium@10867: */ rubidium@10867: Money GetRemovalCost() const; rubidium@10867: truelight@9476: }; truelight@9476: rubidium@9724: extern HouseSpec _house_specs[HOUSE_MAX]; truelight@9476: rubidium@6573: uint32 GetWorldPopulation(); truelight@0: truelight@835: void UpdateTownVirtCoord(Town *t); rubidium@9723: void UpdateAllTownVirtCoords(); rubidium@6573: void InitializeTown(); Darkvater@3346: void ShowTownViewWindow(TownID town); truelight@0: void ExpandTown(Town *t); rubidium@9601: Town *CreateRandomTown(uint attempts, TownSizeMode mode, uint size); truelight@0: truelight@0: enum { truelight@0: ROAD_REMOVE = 0, truelight@0: UNMOVEABLE_REMOVE = 1, truelight@0: TUNNELBRIDGE_REMOVE = 1, truelight@0: INDUSTRY_REMOVE = 2 truelight@0: }; truelight@0: rubidium@9599: /** This is the number of ticks between towns being processed for building new rubidium@9599: * houses or roads. This value originally came from the size of the town array rubidium@9599: * in TTD. */ rubidium@9599: static const byte TOWN_GROWTH_FREQUENCY = 70; rubidium@9599: rubidium@9599: /** Simple value that indicates the house has reached the final stage of rubidium@9599: * construction. */ rubidium@9599: static const byte TOWN_HOUSE_COMPLETED = 3; belugas@3432: glx@9574: /** This enum is used in conjonction with town->flags12. belugas@3432: * IT simply states what bit is used for. belugas@3432: * It is pretty unrealistic (IMHO) to only have one church/stadium belugas@3432: * per town, NO MATTER the population of it. belugas@3432: * And there are 5 more bits available on flags12... belugas@3432: */ belugas@3432: enum { glx@9574: TOWN_IS_FUNDED = 0, ///< Town has received some funds for glx@9574: TOWN_HAS_CHURCH = 1, ///< There can be only one church by town. glx@9574: TOWN_HAS_STADIUM = 2 ///< There can be only one stadium by town. celestar@1005: }; celestar@1005: tron@2958: bool CheckforTownRating(uint32 flags, Town *t, byte type); truelight@0: truelight@9476: static inline HouseSpec *GetHouseSpecs(HouseID house_id) truelight@9476: { truelight@9476: assert(house_id < HOUSE_MAX); truelight@9476: return &_house_specs[house_id]; truelight@9476: } truelight@9476: rubidium@11126: TileIndex GetHouseNorthPart(HouseID &house); rubidium@11126: truelight@1260: /** glx@9574: * Check if a TownID is valid. rubidium@9601: * @param index to inquiry in the pool of town glx@9574: * @return true if it exists glx@9574: */ rubidium@5299: static inline bool IsValidTownID(TownID index) rubidium@5299: { rubidium@9694: return index < GetTownPoolSize() && GetTown(index)->IsValid(); rubidium@5299: } rubidium@5299: rubidium@6573: static inline TownID GetMaxTownIndex() truelight@4354: { truelight@4354: /* TODO - This isn't the real content of the function, but truelight@4354: * with the new pool-system this will be replaced with one that matthijs@5247: * _really_ returns the highest index. Now it just returns truelight@4354: * the next safe value we are sure about everything is below. truelight@4354: */ rubidium@5298: return GetTownPoolSize() - 1; matthijs@5247: } matthijs@5247: rubidium@6573: static inline uint GetNumTowns() matthijs@5247: { rubidium@9724: extern uint _total_towns; rubidium@9724: truelight@4357: return _total_towns; truelight@4354: } truelight@4354: truelight@4356: /** truelight@4356: * Return a random valid town. truelight@4356: */ rubidium@6573: static inline Town *GetRandomTown() truelight@4356: { rubidium@5299: int num = RandomRange(GetNumTowns()); rubidium@5299: TownID index = INVALID_TOWN; truelight@4356: rubidium@5299: while (num >= 0) { truelight@4356: num--; truelight@4356: truelight@4356: index++; rubidium@6233: /* Make sure we have a valid town */ rubidium@5299: while (!IsValidTownID(index)) { truelight@4356: index++; rubidium@5299: assert(index <= GetMaxTownIndex()); truelight@4356: } truelight@4356: } truelight@4356: truelight@4356: return GetTown(index); truelight@4356: } truelight@4356: rubidium@9724: Town *CalcClosestTownFromTile(TileIndex tile, uint threshold); rubidium@6220: rubidium@9694: #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()) truelight@1260: #define FOR_ALL_TOWNS(t) FOR_ALL_TOWNS_FROM(t, 0) truelight@1260: rubidium@9724: extern Town *_cleared_town; rubidium@9724: extern int _cleared_town_rating; truelight@0: truelight@9476: void ResetHouses(); truelight@9476: truelight@9476: void ClearTownHouse(Town *t, TileIndex tile); rubidium@9723: void AfterLoadTown(); rubidium@9723: void UpdateTownMaxPass(Town *t); rubidium@9826: void UpdateTownRadius(Town *t); rubidium@9723: bool CheckIfAuthorityAllows(TileIndex tile); rubidium@9723: Town *ClosestTownFromTile(TileIndex tile, uint threshold); rubidium@9723: void ChangeTownRating(Town *t, int add, int max); rubidium@9724: HouseZonesBits GetTownRadiusGroup(const Town* t, TileIndex tile); rubidium@9723: void SetTownRatingTestMode(bool mode); truelight@9476: rubidium@9869: /** rubidium@9869: * Calculate a hash value from a tile position rubidium@9869: * rubidium@9869: * @param x The X coordinate rubidium@9869: * @param y The Y coordinate rubidium@9869: * @return The hash of the tile rubidium@9869: */ rubidium@9869: static inline uint TileHash(uint x, uint y) rubidium@9869: { rubidium@9869: uint hash = x >> 4; rubidium@9869: hash ^= x >> 6; rubidium@9869: hash ^= y >> 4; rubidium@9869: hash -= y >> 6; rubidium@9869: return hash; rubidium@9869: } rubidium@9869: rubidium@9869: /** rubidium@9869: * Get the last two bits of the TileHash rubidium@9869: * from a tile position. rubidium@9869: * rubidium@9869: * @see TileHash() rubidium@9869: * @param x The X coordinate rubidium@9869: * @param y The Y coordinate rubidium@9869: * @return The last two bits from hash of the tile rubidium@9869: */ rubidium@9869: static inline uint TileHash2Bit(uint x, uint y) rubidium@9869: { rubidium@9869: return GB(TileHash(x, y), 0, 2); rubidium@9869: } rubidium@9869: truelight@0: #endif /* TOWN_H */