town.h

Go to the documentation of this file.
00001 /* $Id: town.h 18809 2010-01-15 16:41:15Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #ifndef TOWN_H
00013 #define TOWN_H
00014 
00015 #include "core/pool_type.hpp"
00016 #include "viewport_type.h"
00017 #include "command_type.h"
00018 #include "town_map.h"
00019 #include "subsidy_type.h"
00020 
00021 template <typename T>
00022 struct BuildingCounts {
00023   T id_count[HOUSE_MAX];
00024   T class_count[HOUSE_CLASS_MAX];
00025 };
00026 
00027 static const uint CUSTOM_TOWN_NUMBER_DIFFICULTY  = 4; 
00028 static const uint CUSTOM_TOWN_MAX_NUMBER = 5000;  
00029 
00030 static const uint INVALID_TOWN = 0xFFFF;
00031 
00032 typedef Pool<Town, TownID, 64, 64000> TownPool;
00033 extern TownPool _town_pool;
00034 
00035 struct Town : TownPool::PoolItem<&_town_pool> {
00036   TileIndex xy;
00037 
00038   /* Current population of people and amount of houses. */
00039   uint32 num_houses;
00040   uint32 population;
00041 
00042   /* Town name */
00043   uint32 townnamegrfid;
00044   uint16 townnametype;
00045   uint32 townnameparts;
00046   char *name;
00047 
00048   /* NOSAVE: Location of name sign, UpdateVirtCoord updates this. */
00049   ViewportSign sign;
00050 
00051   /* Makes sure we don't build certain house types twice.
00052    * bit 0 = Building funds received
00053    * bit 1 = CHURCH
00054    * bit 2 = STADIUM */
00055   byte flags;
00056 
00057   /* level of noise that all the airports are generating */
00058   uint16 noise_reached;
00059 
00060   /* Which companies have a statue? */
00061   CompanyMask statues;
00062 
00063   /* Company ratings as well as a mask that determines which companies have a rating. */
00064   CompanyMask have_ratings;
00065   uint8 unwanted[MAX_COMPANIES]; 
00066   CompanyByte exclusivity;       
00067   uint8 exclusive_counter;       
00068   int16 ratings[MAX_COMPANIES];
00069 
00070   /* Maximum amount of passengers and mail that can be transported. */
00071   uint32 max_pass;
00072   uint32 max_mail;
00073   uint32 new_max_pass;
00074   uint32 new_max_mail;
00075   uint32 act_pass;
00076   uint32 act_mail;
00077   uint32 new_act_pass;
00078   uint32 new_act_mail;
00079 
00080   /* Amount of passengers that were transported. */
00081   byte pct_pass_transported;
00082   byte pct_mail_transported;
00083 
00084   /* Amount of food and paper that was transported. Actually a bit mask would be enough. */
00085   uint16 act_food;
00086   uint16 act_water;
00087   uint16 new_act_food;
00088   uint16 new_act_water;
00089 
00090   /* Time until we rebuild a house. */
00091   uint16 time_until_rebuild;
00092 
00093   /* When to grow town next time. */
00094   uint16 grow_counter;
00095   int16 growth_rate;
00096 
00097   /* Fund buildings program in action? */
00098   byte fund_buildings_months;
00099 
00100   /* Fund road reconstruction in action? */
00101   byte road_build_months;
00102 
00103   /* If this is a larger town, and should grow more quickly. */
00104   bool larger_town;
00105   TownLayoutByte layout; 
00106 
00107   PartOfSubsidyByte part_of_subsidy; 
00108 
00109   /* NOSAVE: UpdateTownRadius updates this given the house count. */
00110   uint32 squared_town_zone_radius[HZB_END];
00111 
00112   /* NOSAVE: The number of each type of building in the town. */
00113   BuildingCounts<uint16> building_counts;
00114 
00118   Town(TileIndex tile = INVALID_TILE) : xy(tile) { }
00119 
00121   ~Town();
00122 
00123   void InitializeLayout(TownLayout layout);
00124 
00131   inline uint16 MaxTownNoise() const
00132   {
00133     if (this->population == 0) return 0; // no population? no noise
00134 
00135     return ((this->population / _settings_game.economy.town_noise_population[_settings_game.difficulty.town_council_tolerance]) + 3);
00136   }
00137 
00138   void UpdateVirtCoord();
00139 
00140   static FORCEINLINE Town *GetByTile(TileIndex tile)
00141   {
00142     return Town::Get(GetTownIndex(tile));
00143   }
00144 
00145   static Town *GetRandom();
00146   static void PostDestructor(size_t index);
00147 };
00148 
00149 uint32 GetWorldPopulation();
00150 
00151 void UpdateAllTownVirtCoords();
00152 void InitializeTown();
00153 void ShowTownViewWindow(TownID town);
00154 void ExpandTown(Town *t);
00155 
00156 enum TownRatingCheckType {
00157   ROAD_REMOVE         = 0,
00158   TUNNELBRIDGE_REMOVE = 1,
00159   TOWN_RATING_CHECK_TYPE_COUNT,
00160 };
00161 
00165 static const byte TOWN_GROWTH_FREQUENCY = 70;
00166 
00173 enum {
00174   TOWN_IS_FUNDED      = 0,   
00175   TOWN_HAS_CHURCH     = 1,   
00176   TOWN_HAS_STADIUM    = 2    
00177 };
00178 
00179 bool CheckforTownRating(DoCommandFlag flags, Town *t, TownRatingCheckType type);
00180 
00181 
00182 TileIndexDiff GetHouseNorthPart(HouseID &house);
00183 
00184 Town *CalcClosestTownFromTile(TileIndex tile, uint threshold = UINT_MAX);
00185 
00186 #define FOR_ALL_TOWNS_FROM(var, start) FOR_ALL_ITEMS_FROM(Town, town_index, var, start)
00187 #define FOR_ALL_TOWNS(var) FOR_ALL_TOWNS_FROM(var, 0)
00188 
00189 void ResetHouses();
00190 
00191 void ClearTownHouse(Town *t, TileIndex tile);
00192 void UpdateTownMaxPass(Town *t);
00193 void UpdateTownRadius(Town *t);
00194 bool CheckIfAuthorityAllowsNewStation(TileIndex tile, DoCommandFlag flags);
00195 Town *ClosestTownFromTile(TileIndex tile, uint threshold);
00196 void ChangeTownRating(Town *t, int add, int max, DoCommandFlag flags);
00197 HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile);
00198 void SetTownRatingTestMode(bool mode);
00199 uint GetMaskOfTownActions(int *nump, CompanyID cid, const Town *t);
00200 bool GenerateTowns(TownLayout layout);
00201 
00202 
00204 enum TownActions {
00205   TACT_NONE             = 0x00, 
00206 
00207   TACT_ADVERTISE_SMALL  = 0x01, 
00208   TACT_ADVERTISE_MEDIUM = 0x02, 
00209   TACT_ADVERTISE_LARGE  = 0x04, 
00210   TACT_ROAD_REBUILD     = 0x08, 
00211   TACT_BUILD_STATUE     = 0x10, 
00212   TACT_FOUND_BUILDINGS  = 0x20, 
00213   TACT_BUY_RIGHTS       = 0x40, 
00214   TACT_BRIBE            = 0x80, 
00215 
00216   TACT_COUNT            = 8,    
00217 
00218   TACT_ADVERTISE        = TACT_ADVERTISE_SMALL | TACT_ADVERTISE_MEDIUM | TACT_ADVERTISE_LARGE, 
00219   TACT_CONSTRUCTION     = TACT_ROAD_REBUILD | TACT_BUILD_STATUE | TACT_FOUND_BUILDINGS,        
00220   TACT_FUNDS            = TACT_BUY_RIGHTS | TACT_BRIBE,                                        
00221   TACT_ALL              = TACT_ADVERTISE | TACT_CONSTRUCTION | TACT_FUNDS,                     
00222 };
00223 DECLARE_ENUM_AS_BIT_SET(TownActions);
00224 
00225 extern const byte _town_action_costs[TACT_COUNT];
00226 extern TownID _new_town_id;
00227 
00235 static inline uint TileHash(uint x, uint y)
00236 {
00237   uint hash = x >> 4;
00238   hash ^= x >> 6;
00239   hash ^= y >> 4;
00240   hash -= y >> 6;
00241   return hash;
00242 }
00243 
00253 static inline uint TileHash2Bit(uint x, uint y)
00254 {
00255   return GB(TileHash(x, y), 0, 2);
00256 }
00257 
00258 #endif /* TOWN_H */

Generated on Wed Apr 21 20:31:55 2010 for OpenTTD by  doxygen 1.6.1