industry.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 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   TileArea location;                  
00042   Town *town;                         
00043   CargoID produced_cargo[2];          
00044   uint16 produced_cargo_waiting[2];   
00045   uint16 incoming_cargo_waiting[3];   
00046   byte production_rate[2];            
00047   byte prod_level;                    
00048   CargoID accepts_cargo[3];           
00049   uint16 this_month_production[2];    
00050   uint16 this_month_transported[2];   
00051   byte last_month_pct_transported[2]; 
00052   uint16 last_month_production[2];    
00053   uint16 last_month_transported[2];   
00054   uint16 counter;                     
00055 
00056   IndustryType type;                  
00057   OwnerByte owner;                    
00058   byte random_colour;                 
00059   Year last_prod_year;                
00060   byte was_cargo_delivered;           
00061 
00062   PartOfSubsidyByte part_of_subsidy;  
00063 
00064   OwnerByte founder;                  
00065   Date construction_date;             
00066   uint8 construction_type;            
00067   Date last_cargo_accepted_at;        
00068   byte selected_layout;               
00069 
00070   byte random_triggers;               
00071   uint16 random;                      
00072 
00073   PersistentStorage *psa;             
00074 
00075   Industry(TileIndex tile = INVALID_TILE) : location(tile, 0, 0) {}
00076   ~Industry();
00077 
00078   void RecomputeProductionMultipliers();
00079 
00085   inline bool TileBelongsToIndustry(TileIndex tile) const
00086   {
00087     return IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == this->index;
00088   }
00089 
00096   static FORCEINLINE Industry *GetByTile(TileIndex tile)
00097   {
00098     return Industry::Get(GetIndustryIndex(tile));
00099   }
00100 
00101   static Industry *GetRandom();
00102   static void PostDestructor(size_t index);
00103 
00109   static inline void IncIndustryTypeCount(IndustryType type)
00110   {
00111     assert(type < NUM_INDUSTRYTYPES);
00112     counts[type]++;
00113   }
00114 
00120   static inline void DecIndustryTypeCount(IndustryType type)
00121   {
00122     assert(type < NUM_INDUSTRYTYPES);
00123     counts[type]--;
00124   }
00125 
00131   static inline uint16 GetIndustryTypeCount(IndustryType type)
00132   {
00133     assert(type < NUM_INDUSTRYTYPES);
00134     return counts[type];
00135   }
00136 
00138   static inline void ResetIndustryCounts()
00139   {
00140     memset(&counts, 0, sizeof(counts));
00141   }
00142 
00143 protected:
00144   static uint16 counts[NUM_INDUSTRYTYPES]; 
00145 };
00146 
00147 void PlantRandomFarmField(const Industry *i);
00148 
00149 void ReleaseDisastersTargetingIndustry(IndustryID);
00150 
00151 bool IsTileForestIndustry(TileIndex tile);
00152 
00153 #define FOR_ALL_INDUSTRIES_FROM(var, start) FOR_ALL_ITEMS_FROM(Industry, industry_index, var, start)
00154 #define FOR_ALL_INDUSTRIES(var) FOR_ALL_INDUSTRIES_FROM(var, 0)
00155 
00157 struct IndustryTypeBuildData {
00158   uint32 probability;  
00159   byte   min_number;   
00160   uint16 target_count; 
00161   uint16 max_wait;     
00162   uint16 wait_count;   
00163 
00164   void Reset();
00165 
00166   bool GetIndustryTypeData(IndustryType it);
00167 };
00168 
00172 struct IndustryBuildData {
00173   IndustryTypeBuildData builddata[NUM_INDUSTRYTYPES]; 
00174   uint32 wanted_inds; 
00175 
00176   void Reset();
00177 
00178   void SetupTargetCount();
00179   void TryBuildNewIndustry();
00180 
00181   void MonthlyLoop();
00182 };
00183 
00184 extern IndustryBuildData _industry_builder;
00185 
00186 #endif /* INDUSTRY_H */