# 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
    00021 template <typename T>
    00022 struct BuildingCounts {
    00023   T id_count[HOUSE_MAX];
    00024   T class_count[HOUSE_CLASS_MAX];
    00025 };
    00026
    00027 static const uint CUSTOM_TOWN_NUMBER_DIFFICULTY  = 4;
    00028 static const uint CUSTOM_TOWN_MAX_NUMBER = 5000;
    00029
    00030 static const uint INVALID_TOWN = 0xFFFF;
    00031
    00032 typedef Pool<Town, TownID, 64, 64000> TownPool;
    00033 extern TownPool _town_pool;
    00034
    00036 struct Town : TownPool::PoolItem<&_town_pool> {
    00037   TileIndex xy;
    00038
    00039   /* Current population of people and amount of houses. */
    00040   uint32 num_houses;
    00041   uint32 population;
    00042
    00043   /* Town name */
    00044   uint32 townnamegrfid;
    00045   uint16 townnametype;
    00046   uint32 townnameparts;
    00047   char *name;
    00048
    00049   /* NOSAVE: Location of name sign, UpdateVirtCoord updates this. */
    00050   ViewportSign sign;
    00051
    00052   /* Makes sure we don't build certain house types twice.
    00053    * bit 0 = Building funds received
    00054    * bit 1 = CHURCH
    00055    * bit 2 = STADIUM */
    00056   byte flags;
    00057
    00058   /* level of noise that all the airports are generating */
    00059   uint16 noise_reached;
    00060
    00061   /* Which companies have a statue? */
    00062   CompanyMask statues;
    00063
    00064   /* Company ratings as well as a mask that determines which companies have a rating. */
    00065   CompanyMask have_ratings;
    00066   uint8 unwanted[MAX_COMPANIES];
    00067   CompanyByte exclusivity;
    00068   uint8 exclusive_counter;
    00069   int16 ratings[MAX_COMPANIES];
    00070
    00071   /* Maximum amount of passengers and mail that can be transported. */
    00072   uint32 max_pass;
    00073   uint32 max_mail;
    00074   uint32 new_max_pass;
    00075   uint32 new_max_mail;
    00076   uint32 act_pass;
    00077   uint32 act_mail;
    00078   uint32 new_act_pass;
    00079   uint32 new_act_mail;
    00080
    00081   /* Amount of passengers that were transported. */
    00082   byte pct_pass_transported;
    00083   byte pct_mail_transported;
    00084
    00085   /* Amount of food and paper that was transported. Actually a bit mask would be enough. */
    00086   uint16 act_food;
    00087   uint16 act_water;
    00088   uint16 new_act_food;
    00089   uint16 new_act_water;
    00090
    00091   /* Time until we rebuild a house. */
    00092   uint16 time_until_rebuild;
    00093
    00094   /* When to grow town next time. */
    00095   uint16 grow_counter;
    00096   int16 growth_rate;
    00097
    00098   /* Fund buildings program in action? */
    00099   byte fund_buildings_months;
    00100
    00101   /* Fund road reconstruction in action? */
    00102   byte road_build_months;
    00103
    00104   /* If this is a larger town, and should grow more quickly. */
    00105   bool larger_town;
    00106   TownLayoutByte layout;
    00107
    00108   PartOfSubsidyByte part_of_subsidy;
    00109
    00110   /* NOSAVE: UpdateTownRadius updates this given the house count. */
    00111   uint32 squared_town_zone_radius[HZB_END];
    00112
    00113   BuildingCounts<uint16> building_counts;
    00114
    00118   Town(TileIndex tile = INVALID_TILE) : xy(tile) { }
    00119
    00121   ~Town();
    00122
    00123   void InitializeLayout(TownLayout layout);
    00124
    00131   inline uint16 MaxTownNoise() const
    00132   {
    00133     if (this->population == 0) return 0; // no population? no noise
    00134
    00135     /* 3 is added (the noise of the lowest airport), so the  user can at least build a small airfield. */
    00136     return (this->population / _settings_game.economy.town_noise_population[_settings_game.difficulty.town_council_tolerance]) + 3;
    00137   }
    00138
    00139   void UpdateVirtCoord();
    00140
    00141   static FORCEINLINE Town *GetByTile(TileIndex tile)
    00142   {
    00143     return Town::Get(GetTownIndex(tile));
    00144   }
    00145
    00146   static Town *GetRandom();
    00147   static void PostDestructor(size_t index);
    00148 };
    00149
    00150 uint32 GetWorldPopulation();
    00151
    00152 void UpdateAllTownVirtCoords();
    00153 void ShowTownViewWindow(TownID town);
    00154 void ExpandTown(Town *t);
    00155
    00160 enum TownRatingCheckType {
    00161   ROAD_REMOVE         = 0,
    00162   TUNNELBRIDGE_REMOVE = 1,
    00163   TOWN_RATING_CHECK_TYPE_COUNT,
    00164 };
    00165
    00171 static const byte TOWN_GROWTH_FREQUENCY = 70;
    00172
    00180 enum TownFlags {
    00181   TOWN_IS_FUNDED      = 0,
    00182   TOWN_HAS_CHURCH     = 1,
    00183   TOWN_HAS_STADIUM    = 2
    00184 };
    00185
    00186 CommandCost CheckforTownRating(DoCommandFlag flags, Town *t, TownRatingCheckType type);
    00187
    00188
    00189 TileIndexDiff GetHouseNorthPart(HouseID &house);
    00190
    00191 Town *CalcClosestTownFromTile(TileIndex tile, uint threshold = UINT_MAX);
    00192
    00193 #define FOR_ALL_TOWNS_FROM(var, start) FOR_ALL_ITEMS_FROM(Town, town_index, var, start)
    00194 #define FOR_ALL_TOWNS(var) FOR_ALL_TOWNS_FROM(var, 0)
    00195
    00196 void ResetHouses();
    00197
    00198 void ClearTownHouse(Town *t, TileIndex tile);
    00199 void UpdateTownMaxPass(Town *t);
    00200 void UpdateTownRadius(Town *t);
    00201 CommandCost CheckIfAuthorityAllowsNewStation(TileIndex tile, DoCommandFlag flags);
    00202 Town *ClosestTownFromTile(TileIndex tile, uint threshold);
    00203 void ChangeTownRating(Town *t, int add, int max, DoCommandFlag flags);
    00204 HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile);
    00205 void SetTownRatingTestMode(bool mode);
    00206 uint GetMaskOfTownActions(int *nump, CompanyID cid, const Town *t);
    00207 bool GenerateTowns(TownLayout layout);
    00208
    00209
    00211 enum TownActions {
    00212   TACT_NONE             = 0x00,
    00213
    00214   TACT_ADVERTISE_SMALL  = 0x01,
    00215   TACT_ADVERTISE_MEDIUM = 0x02,
    00216   TACT_ADVERTISE_LARGE  = 0x04,
    00217   TACT_ROAD_REBUILD     = 0x08,
    00218   TACT_BUILD_STATUE     = 0x10,
    00219   TACT_FOUND_BUILDINGS  = 0x20,
    00220   TACT_BUY_RIGHTS       = 0x40,
    00221   TACT_BRIBE            = 0x80,
    00222
    00223   TACT_COUNT            = 8,
    00224
    00225   TACT_ADVERTISE        = TACT_ADVERTISE_SMALL | TACT_ADVERTISE_MEDIUM | TACT_ADVERTISE_LARGE,
    00226   TACT_CONSTRUCTION     = TACT_ROAD_REBUILD | TACT_BUILD_STATUE | TACT_FOUND_BUILDINGS,
    00227   TACT_FUNDS            = TACT_BUY_RIGHTS | TACT_BRIBE,
    00228   TACT_ALL              = TACT_ADVERTISE | TACT_CONSTRUCTION | TACT_FUNDS,
    00229 };
    00230 DECLARE_ENUM_AS_BIT_SET(TownActions)
    00231
    00232 extern const byte _town_action_costs[TACT_COUNT];
    00233 extern TownID _new_town_id;
    00234
    00240 template <class T>
    00241 void MakeDefaultName(T *obj)
    00242 {
    00243   /* We only want to set names if it hasn't been set before, or when we're calling from afterload. */
    00244   assert(obj->name == NULL || obj->town_cn == UINT16_MAX);
    00245
    00246   obj->town = ClosestTownFromTile(obj->xy, UINT_MAX);
    00247
    00248   /* Find first unused number belonging to this town. This can never fail,
    00249    * as long as there can be at most 65535 waypoints/depots in total.
    00250    *
    00251    * This does 'n * m' search, but with 32bit 'used' bitmap, it needs at
    00252    * most 'n * (1 + ceil(m / 32))' steps (n - number of waypoints in pool,
    00253    * m - number of waypoints near this town).
    00254    * Usually, it needs only 'n' steps.
    00255    *
    00256    * If it wasn't using 'used' and 'idx', it would just search for increasing 'next',
    00257    * but this way it is faster */
    00258
    00259   uint32 used = 0; // bitmap of used waypoint numbers, sliding window with 'next' as base
    00260   uint32 next = 0; // first number in the bitmap
    00261   uint32 idx  = 0; // index where we will stop
    00262   uint32 cid  = 0; // current index, goes to T::GetPoolSize()-1, then wraps to 0
    00263
    00264   do {
    00265     T *lobj = T::GetIfValid(cid);
    00266
    00267     /* check only valid waypoints... */
    00268     if (lobj != NULL && obj != lobj) {
    00269       /* only objects within the same city and with the same type */
    00270       if (lobj->town == obj->town && lobj->IsOfType(obj)) {
    00271         /* if lobj->town_cn < next, uint will overflow to '+inf' */
    00272         uint i = (uint)lobj->town_cn - next;
    00273
    00274         if (i < 32) {
    00275           SetBit(used, i); // update bitmap
    00276           if (i == 0) {
    00277             /* shift bitmap while the lowest bit is '1';
    00278              * increase the base of the bitmap too */
    00279             do {
    00280               used >>= 1;
    00281               next++;
    00282             } while (HasBit(used, 0));
    00283             /* when we are at 'idx' again at end of the loop and
    00284              * 'next' hasn't changed, then no object had town_cn == next,
    00285              * so we can safely use it */
    00286             idx = cid;
    00287           }
    00288         }
    00289       }
    00290     }
    00291
    00292     cid++;
    00293     if (cid == T::GetPoolSize()) cid = 0; // wrap to zero...
    00294   } while (cid != idx);
    00295
    00296   obj->town_cn = (uint16)next; // set index...
    00297 }
    00298
    00299 #endif /* TOWN_H */