town.h

Go to the documentation of this file.
00001 /* $Id: town.h 25968 2013-11-12 17:57:12Z frosch $ */
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 "viewport_type.h"
00016 #include "town_map.h"
00017 #include "subsidy_type.h"
00018 #include "newgrf_storage.h"
00019 #include "cargotype.h"
00020 #include "tilematrix_type.hpp"
00021 #include <list>
00022 
00023 template <typename T>
00024 struct BuildingCounts {
00025   T id_count[NUM_HOUSES];
00026   T class_count[HOUSE_CLASS_MAX];
00027 };
00028 
00029 typedef TileMatrix<uint32, 4> AcceptanceMatrix;
00030 
00031 static const uint CUSTOM_TOWN_NUMBER_DIFFICULTY  = 4; 
00032 static const uint CUSTOM_TOWN_MAX_NUMBER = 5000;  
00033 
00034 static const uint INVALID_TOWN = 0xFFFF;
00035 
00036 static const uint TOWN_GROWTH_WINTER = 0xFFFFFFFE; 
00037 static const uint TOWN_GROWTH_DESERT = 0xFFFFFFFF; 
00038 static const uint16 TOWN_GROW_RATE_CUSTOM      = 0x8000; 
00039 static const uint16 TOWN_GROW_RATE_CUSTOM_NONE = 0xFFFF; 
00040 
00041 typedef Pool<Town, TownID, 64, 64000> TownPool;
00042 extern TownPool _town_pool;
00043 
00045 struct TownCache {
00046   uint32 num_houses;                        
00047   uint32 population;                        
00048   ViewportSign sign;                        
00049   PartOfSubsidyByte part_of_subsidy;        
00050   uint32 squared_town_zone_radius[HZB_END]; 
00051   BuildingCounts<uint16> building_counts;   
00052 };
00053 
00055 struct Town : TownPool::PoolItem<&_town_pool> {
00056   TileIndex xy;                  
00057 
00058   TownCache cache; 
00059 
00060   /* Town name */
00061   uint32 townnamegrfid;
00062   uint16 townnametype;
00063   uint32 townnameparts;
00064   char *name;                    
00065 
00066   byte flags;                    
00067 
00068   uint16 noise_reached;          
00069 
00070   CompanyMask statues;           
00071 
00072   /* Company ratings. */
00073   CompanyMask have_ratings;      
00074   uint8 unwanted[MAX_COMPANIES]; 
00075   CompanyByte exclusivity;       
00076   uint8 exclusive_counter;       
00077   int16 ratings[MAX_COMPANIES];  
00078 
00079   TransportedCargoStat<uint32> supplied[NUM_CARGO]; 
00080   TransportedCargoStat<uint16> received[NUM_TE];    
00081   uint32 goal[NUM_TE];                              
00082 
00083   char *text; 
00084 
00085   inline byte GetPercentTransported(CargoID cid) const { return this->supplied[cid].old_act * 256 / (this->supplied[cid].old_max + 1); }
00086 
00087   /* Cargo production and acceptance stats. */
00088   uint32 cargo_produced;           
00089   AcceptanceMatrix cargo_accepted; 
00090   uint32 cargo_accepted_total;     
00091 
00092   uint16 time_until_rebuild;     
00093 
00094   uint16 grow_counter;           
00095   uint16 growth_rate;            
00096 
00097   byte fund_buildings_months;    
00098   byte road_build_months;        
00099 
00100   bool larger_town;              
00101   TownLayoutByte layout;         
00102 
00103   std::list<PersistentStorage *> psa_list;
00104 
00109   Town(TileIndex tile = INVALID_TILE) : xy(tile) { }
00110 
00112   ~Town();
00113 
00114   void InitializeLayout(TownLayout layout);
00115 
00122   inline uint16 MaxTownNoise() const
00123   {
00124     if (this->cache.population == 0) return 0; // no population? no noise
00125 
00126     /* 3 is added (the noise of the lowest airport), so the  user can at least build a small airfield. */
00127     return (this->cache.population / _settings_game.economy.town_noise_population[_settings_game.difficulty.town_council_tolerance]) + 3;
00128   }
00129 
00130   void UpdateVirtCoord();
00131 
00132   static inline Town *GetByTile(TileIndex tile)
00133   {
00134     return Town::Get(GetTownIndex(tile));
00135   }
00136 
00137   static Town *GetRandom();
00138   static void PostDestructor(size_t index);
00139 };
00140 
00141 uint32 GetWorldPopulation();
00142 
00143 void UpdateAllTownVirtCoords();
00144 void ShowTownViewWindow(TownID town);
00145 void ExpandTown(Town *t);
00146 
00151 enum TownRatingCheckType {
00152   ROAD_REMOVE         = 0,      
00153   TUNNELBRIDGE_REMOVE = 1,      
00154   TOWN_RATING_CHECK_TYPE_COUNT, 
00155 };
00156 
00164 enum TownFlags {
00165   TOWN_IS_GROWING     = 0,   
00166   TOWN_HAS_CHURCH     = 1,   
00167   TOWN_HAS_STADIUM    = 2,   
00168 };
00169 
00170 CommandCost CheckforTownRating(DoCommandFlag flags, Town *t, TownRatingCheckType type);
00171 
00172 
00173 TileIndexDiff GetHouseNorthPart(HouseID &house);
00174 
00175 Town *CalcClosestTownFromTile(TileIndex tile, uint threshold = UINT_MAX);
00176 
00177 #define FOR_ALL_TOWNS_FROM(var, start) FOR_ALL_ITEMS_FROM(Town, town_index, var, start)
00178 #define FOR_ALL_TOWNS(var) FOR_ALL_TOWNS_FROM(var, 0)
00179 
00180 void ResetHouses();
00181 
00182 void ClearTownHouse(Town *t, TileIndex tile);
00183 void UpdateTownMaxPass(Town *t);
00184 void UpdateTownRadius(Town *t);
00185 void UpdateTownCargoes(Town *t);
00186 void UpdateTownCargoTotal(Town *t);
00187 void UpdateTownCargoBitmap();
00188 CommandCost CheckIfAuthorityAllowsNewStation(TileIndex tile, DoCommandFlag flags);
00189 Town *ClosestTownFromTile(TileIndex tile, uint threshold);
00190 void ChangeTownRating(Town *t, int add, int max, DoCommandFlag flags);
00191 HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile);
00192 void SetTownRatingTestMode(bool mode);
00193 uint GetMaskOfTownActions(int *nump, CompanyID cid, const Town *t);
00194 bool GenerateTowns(TownLayout layout);
00195 const CargoSpec *FindFirstCargoWithTownEffect(TownEffect effect);
00196 
00197 
00199 enum TownActions {
00200   TACT_NONE             = 0x00, 
00201 
00202   TACT_ADVERTISE_SMALL  = 0x01, 
00203   TACT_ADVERTISE_MEDIUM = 0x02, 
00204   TACT_ADVERTISE_LARGE  = 0x04, 
00205   TACT_ROAD_REBUILD     = 0x08, 
00206   TACT_BUILD_STATUE     = 0x10, 
00207   TACT_FUND_BUILDINGS   = 0x20, 
00208   TACT_BUY_RIGHTS       = 0x40, 
00209   TACT_BRIBE            = 0x80, 
00210 
00211   TACT_COUNT            = 8,    
00212 
00213   TACT_ADVERTISE        = TACT_ADVERTISE_SMALL | TACT_ADVERTISE_MEDIUM | TACT_ADVERTISE_LARGE, 
00214   TACT_CONSTRUCTION     = TACT_ROAD_REBUILD | TACT_BUILD_STATUE | TACT_FUND_BUILDINGS,         
00215   TACT_FUNDS            = TACT_BUY_RIGHTS | TACT_BRIBE,                                        
00216   TACT_ALL              = TACT_ADVERTISE | TACT_CONSTRUCTION | TACT_FUNDS,                     
00217 };
00218 DECLARE_ENUM_AS_BIT_SET(TownActions)
00219 
00220 extern const byte _town_action_costs[TACT_COUNT];
00221 extern TownID _new_town_id;
00222 
00228 template <class T>
00229 void MakeDefaultName(T *obj)
00230 {
00231   /* We only want to set names if it hasn't been set before, or when we're calling from afterload. */
00232   assert(obj->name == NULL || obj->town_cn == UINT16_MAX);
00233 
00234   obj->town = ClosestTownFromTile(obj->xy, UINT_MAX);
00235 
00236   /* Find first unused number belonging to this town. This can never fail,
00237    * as long as there can be at most 65535 waypoints/depots in total.
00238    *
00239    * This does 'n * m' search, but with 32bit 'used' bitmap, it needs at
00240    * most 'n * (1 + ceil(m / 32))' steps (n - number of waypoints in pool,
00241    * m - number of waypoints near this town).
00242    * Usually, it needs only 'n' steps.
00243    *
00244    * If it wasn't using 'used' and 'idx', it would just search for increasing 'next',
00245    * but this way it is faster */
00246 
00247   uint32 used = 0; // bitmap of used waypoint numbers, sliding window with 'next' as base
00248   uint32 next = 0; // first number in the bitmap
00249   uint32 idx  = 0; // index where we will stop
00250   uint32 cid  = 0; // current index, goes to T::GetPoolSize()-1, then wraps to 0
00251 
00252   do {
00253     T *lobj = T::GetIfValid(cid);
00254 
00255     /* check only valid waypoints... */
00256     if (lobj != NULL && obj != lobj) {
00257       /* only objects within the same city and with the same type */
00258       if (lobj->town == obj->town && lobj->IsOfType(obj)) {
00259         /* if lobj->town_cn < next, uint will overflow to '+inf' */
00260         uint i = (uint)lobj->town_cn - next;
00261 
00262         if (i < 32) {
00263           SetBit(used, i); // update bitmap
00264           if (i == 0) {
00265             /* shift bitmap while the lowest bit is '1';
00266              * increase the base of the bitmap too */
00267             do {
00268               used >>= 1;
00269               next++;
00270             } while (HasBit(used, 0));
00271             /* when we are at 'idx' again at end of the loop and
00272              * 'next' hasn't changed, then no object had town_cn == next,
00273              * so we can safely use it */
00274             idx = cid;
00275           }
00276         }
00277       }
00278     }
00279 
00280     cid++;
00281     if (cid == T::GetPoolSize()) cid = 0; // wrap to zero...
00282   } while (cid != idx);
00283 
00284   obj->town_cn = (uint16)next; // set index...
00285 }
00286 
00287 extern uint32 _town_cargoes_accepted;
00288 
00289 #endif /* TOWN_H */