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