town.h

Go to the documentation of this file.
00001 /* $Id$ */
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[HOUSE_MAX];
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 extern uint8 _head_to_head;
00035 static const uint INVALID_TOWN = 0xFFFF;
00036 
00037 static const uint TOWN_GROWTH_WINTER = 0xFFFFFFFE; 
00038 static const uint TOWN_GROWTH_DESERT = 0xFFFFFFFF; 
00039 static const uint16 TOWN_GROW_RATE_CUSTOM = 0x8000; 
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   /* Makes sure we don't build certain house types twice.
00067    * bit 0 = Building funds received
00068    * bit 1 = CHURCH
00069    * bit 2 = STADIUM */
00070   byte flags;
00071 
00072   uint16 noise_reached;          
00073 
00074   CompanyMask statues;           
00075 
00076   /* Company ratings. */
00077   CompanyMask have_ratings;      
00078   uint8 unwanted[MAX_COMPANIES]; 
00079   CompanyByte exclusivity;       
00080   uint8 exclusive_counter;       
00081   int16 ratings[MAX_COMPANIES];  
00082 
00083   TransportedCargoStat<uint32> supplied[NUM_CARGO]; 
00084   TransportedCargoStat<uint16> received[NUM_TE];    
00085   uint32 goal[NUM_TE];                              
00086 
00087   char *text; 
00088 
00089   inline byte GetPercentTransported(CargoID cid) const { return this->supplied[cid].old_act * 256 / (this->supplied[cid].old_max + 1); }
00090 
00091   /* Cargo production and acceptance stats. */
00092   uint32 cargo_produced;           
00093   AcceptanceMatrix cargo_accepted; 
00094   uint32 cargo_accepted_total;     
00095 
00096   uint16 time_until_rebuild;     
00097 
00098   uint16 grow_counter;           
00099   uint16 growth_rate;            
00100 
00101   byte fund_buildings_months;    
00102   byte road_build_months;        
00103 
00104   bool larger_town;              
00105   TownLayoutByte layout;         
00106 
00107   std::list<PersistentStorage *> psa_list;
00108 
00109   uint8 head_to_head;
00110   uint8 town_growth_counter;
00111 
00116   Town(TileIndex tile = INVALID_TILE) : xy(tile) { }
00117 
00119   ~Town();
00120 
00121   void InitializeLayout(TownLayout layout);
00122 
00129   inline uint16 MaxTownNoise() const
00130   {
00131     if (this->cache.population == 0) return 0; // no population? no noise
00132 
00133     /* 3 is added (the noise of the lowest airport), so the  user can at least build a small airfield. */
00134     return (this->cache.population / _settings_game.economy.town_noise_population[_settings_game.difficulty.town_council_tolerance]) + 3;
00135   }
00136 
00137   void UpdateVirtCoord();
00138 
00139   static inline Town *GetByTile(TileIndex tile)
00140   {
00141     return Town::Get(GetTownIndex(tile));
00142   }
00143 
00144   static Town *GetRandom();
00145   static void PostDestructor(size_t index);
00146 };
00147 
00148 uint32 GetWorldPopulation();
00149 
00150 void UpdateAllTownVirtCoords();
00151 void ShowTownViewWindow(TownID town);
00152 void ExpandTown(Town *t);
00153 
00158 enum TownRatingCheckType {
00159   ROAD_REMOVE         = 0,      
00160   TUNNELBRIDGE_REMOVE = 1,      
00161   TOWN_RATING_CHECK_TYPE_COUNT, 
00162 };
00163 
00171 enum TownFlags {
00172   TOWN_IS_FUNDED      = 0,   
00173   TOWN_HAS_CHURCH     = 1,   
00174   TOWN_HAS_STADIUM    = 2,   
00175 };
00176 
00177 CommandCost CheckforTownRating(DoCommandFlag flags, Town *t, TownRatingCheckType type);
00178 
00179 
00180 TileIndexDiff GetHouseNorthPart(HouseID &house);
00181 
00182 Town *CalcClosestTownFromTile(TileIndex tile, uint threshold = UINT_MAX);
00183 
00184 #define FOR_ALL_TOWNS_FROM(var, start) FOR_ALL_ITEMS_FROM(Town, town_index, var, start) if (_head_to_head == 0 || var->head_to_head == _head_to_head)
00185 #define FOR_ALL_TOWNS(var) FOR_ALL_TOWNS_FROM(var, 0)
00186 
00187 void ResetHouses();
00188 
00189 void ClearTownHouse(Town *t, TileIndex tile);
00190 void UpdateTownMaxPass(Town *t);
00191 void UpdateTownRadius(Town *t);
00192 void UpdateTownCargoes(Town *t);
00193 void UpdateTownCargoTotal(Town *t);
00194 void UpdateTownCargoBitmap();
00195 CommandCost CheckIfAuthorityAllowsNewStation(TileIndex tile, DoCommandFlag flags);
00196 Town *ClosestTownFromTile(TileIndex tile, uint threshold);
00197 void ChangeTownRating(Town *t, int add, int max, DoCommandFlag flags);
00198 HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile);
00199 void SetTownRatingTestMode(bool mode);
00200 uint GetMaskOfTownActions(int *nump, CompanyID cid, const Town *t);
00201 bool GenerateTowns(TownLayout layout);
00202 const CargoSpec *FindFirstCargoWithTownEffect(TownEffect effect);
00203 
00204 
00206 enum TownActions {
00207   TACT_NONE             = 0x00, 
00208 
00209   TACT_ADVERTISE_SMALL  = 0x01, 
00210   TACT_ADVERTISE_MEDIUM = 0x02, 
00211   TACT_ADVERTISE_LARGE  = 0x04, 
00212   TACT_ROAD_REBUILD     = 0x08, 
00213   TACT_BUILD_STATUE     = 0x10, 
00214   TACT_FUND_BUILDINGS   = 0x20, 
00215   TACT_BUY_RIGHTS       = 0x40, 
00216   TACT_BRIBE            = 0x80, 
00217 
00218   TACT_COUNT            = 8,    
00219 
00220   TACT_ADVERTISE        = TACT_ADVERTISE_SMALL | TACT_ADVERTISE_MEDIUM | TACT_ADVERTISE_LARGE, 
00221   TACT_CONSTRUCTION     = TACT_ROAD_REBUILD | TACT_BUILD_STATUE | TACT_FUND_BUILDINGS,         
00222   TACT_FUNDS            = TACT_BUY_RIGHTS | TACT_BRIBE,                                        
00223   TACT_ALL              = TACT_ADVERTISE | TACT_CONSTRUCTION | TACT_FUNDS,                     
00224 };
00225 DECLARE_ENUM_AS_BIT_SET(TownActions)
00226 
00227 extern const byte _town_action_costs[TACT_COUNT];
00228 extern TownID _new_town_id;
00229 
00235 template <class T>
00236 void MakeDefaultName(T *obj)
00237 {
00238   /* We only want to set names if it hasn't been set before, or when we're calling from afterload. */
00239   assert(obj->name == NULL || obj->town_cn == UINT16_MAX);
00240 
00241   obj->town = ClosestTownFromTile(obj->xy, UINT_MAX);
00242 
00243   /* Find first unused number belonging to this town. This can never fail,
00244    * as long as there can be at most 65535 waypoints/depots in total.
00245    *
00246    * This does 'n * m' search, but with 32bit 'used' bitmap, it needs at
00247    * most 'n * (1 + ceil(m / 32))' steps (n - number of waypoints in pool,
00248    * m - number of waypoints near this town).
00249    * Usually, it needs only 'n' steps.
00250    *
00251    * If it wasn't using 'used' and 'idx', it would just search for increasing 'next',
00252    * but this way it is faster */
00253 
00254   uint32 used = 0; // bitmap of used waypoint numbers, sliding window with 'next' as base
00255   uint32 next = 0; // first number in the bitmap
00256   uint32 idx  = 0; // index where we will stop
00257   uint32 cid  = 0; // current index, goes to T::GetPoolSize()-1, then wraps to 0
00258 
00259   do {
00260     T *lobj = T::GetIfValid(cid);
00261 
00262     /* check only valid waypoints... */
00263     if (lobj != NULL && obj != lobj) {
00264       /* only objects within the same city and with the same type */
00265       if (lobj->town == obj->town && lobj->IsOfType(obj)) {
00266         /* if lobj->town_cn < next, uint will overflow to '+inf' */
00267         uint i = (uint)lobj->town_cn - next;
00268 
00269         if (i < 32) {
00270           SetBit(used, i); // update bitmap
00271           if (i == 0) {
00272             /* shift bitmap while the lowest bit is '1';
00273              * increase the base of the bitmap too */
00274             do {
00275               used >>= 1;
00276               next++;
00277             } while (HasBit(used, 0));
00278             /* when we are at 'idx' again at end of the loop and
00279              * 'next' hasn't changed, then no object had town_cn == next,
00280              * so we can safely use it */
00281             idx = cid;
00282           }
00283         }
00284       }
00285     }
00286 
00287     cid++;
00288     if (cid == T::GetPoolSize()) cid = 0; // wrap to zero...
00289   } while (cid != idx);
00290 
00291   obj->town_cn = (uint16)next; // set index...
00292 }
00293 
00294 extern uint32 _town_cargoes_accepted;
00295 
00296 #endif /* TOWN_H */