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