industry.h

Go to the documentation of this file.
00001 /* $Id: industry.h 23795 2012-01-13 21:54:59Z rubidium $ */
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 INDUSTRY_H
00013 #define INDUSTRY_H
00014 
00015 #include "core/pool_type.hpp"
00016 #include "newgrf_storage.h"
00017 #include "subsidy_type.h"
00018 #include "industry_map.h"
00019 #include "tilearea_type.h"
00020 
00021 
00022 typedef Pool<Industry, IndustryID, 64, 64000> IndustryPool;
00023 extern IndustryPool _industry_pool;
00024 
00030 enum ProductionLevels {
00031   PRODLEVEL_CLOSURE = 0x00,  
00032   PRODLEVEL_MINIMUM = 0x04,  
00033   PRODLEVEL_DEFAULT = 0x10,  
00034   PRODLEVEL_MAXIMUM = 0x80,  
00035 };
00036 
00040 struct Industry : IndustryPool::PoolItem<&_industry_pool> {
00041   typedef PersistentStorageArray<int32, 16> PersistentStorage;
00042 
00043   TileArea location;                  
00044   const Town *town;                   
00045   CargoID produced_cargo[2];          
00046   uint16 produced_cargo_waiting[2];   
00047   uint16 incoming_cargo_waiting[3];   
00048   byte production_rate[2];            
00049   byte prod_level;                    
00050   CargoID accepts_cargo[3];           
00051   uint16 this_month_production[2];    
00052   uint16 this_month_transported[2];   
00053   byte last_month_pct_transported[2]; 
00054   uint16 last_month_production[2];    
00055   uint16 last_month_transported[2];   
00056   uint16 counter;                     
00057 
00058   IndustryType type;                  
00059   OwnerByte owner;                    
00060   byte random_colour;                 
00061   Year last_prod_year;                
00062   byte was_cargo_delivered;           
00063 
00064   PartOfSubsidyByte part_of_subsidy;  
00065 
00066   OwnerByte founder;                  
00067   Date construction_date;             
00068   uint8 construction_type;            
00069   Date last_cargo_accepted_at;        
00070   byte selected_layout;               
00071 
00072   byte random_triggers;               
00073   uint16 random;                      
00074 
00075   PersistentStorage psa;              
00076 
00077   Industry(TileIndex tile = INVALID_TILE) : location(tile, 0, 0) {}
00078   ~Industry();
00079 
00080   void RecomputeProductionMultipliers();
00081 
00087   inline bool TileBelongsToIndustry(TileIndex tile) const
00088   {
00089     return IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == this->index;
00090   }
00091 
00098   static FORCEINLINE Industry *GetByTile(TileIndex tile)
00099   {
00100     return Industry::Get(GetIndustryIndex(tile));
00101   }
00102 
00103   static Industry *GetRandom();
00104   static void PostDestructor(size_t index);
00105 
00111   static inline void IncIndustryTypeCount(IndustryType type)
00112   {
00113     assert(type < NUM_INDUSTRYTYPES);
00114     counts[type]++;
00115   }
00116 
00122   static inline void DecIndustryTypeCount(IndustryType type)
00123   {
00124     assert(type < NUM_INDUSTRYTYPES);
00125     counts[type]--;
00126   }
00127 
00133   static inline uint16 GetIndustryTypeCount(IndustryType type)
00134   {
00135     assert(type < NUM_INDUSTRYTYPES);
00136     return counts[type];
00137   }
00138 
00140   static inline void ResetIndustryCounts()
00141   {
00142     memset(&counts, 0, sizeof(counts));
00143   }
00144 
00145 protected:
00146   static uint16 counts[NUM_INDUSTRYTYPES]; 
00147 };
00148 
00149 void PlantRandomFarmField(const Industry *i);
00150 
00151 void ReleaseDisastersTargetingIndustry(IndustryID);
00152 
00153 bool IsTileForestIndustry(TileIndex tile);
00154 
00155 #define FOR_ALL_INDUSTRIES_FROM(var, start) FOR_ALL_ITEMS_FROM(Industry, industry_index, var, start)
00156 #define FOR_ALL_INDUSTRIES(var) FOR_ALL_INDUSTRIES_FROM(var, 0)
00157 
00159 struct IndustryTypeBuildData {
00160   uint32 probability;  
00161   byte   min_number;   
00162   uint16 target_count; 
00163   uint16 max_wait;     
00164   uint16 wait_count;   
00165 
00166   void Reset();
00167 
00168   bool GetIndustryTypeData(IndustryType it);
00169 };
00170 
00174 struct IndustryBuildData {
00175   IndustryTypeBuildData builddata[NUM_INDUSTRYTYPES]; 
00176   uint32 wanted_inds; 
00177 
00178   void Reset();
00179 
00180   void SetupTargetCount();
00181   void TryBuildNewIndustry();
00182 
00183   void MonthlyLoop();
00184 };
00185 
00186 extern IndustryBuildData _industry_builder;
00187 
00188 #endif /* INDUSTRY_H */