town.h

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