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