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 #include "cargodest_base.h"
00021 
00022 
00023 typedef Pool<Industry, IndustryID, 64, 64000> IndustryPool;
00024 extern IndustryPool _industry_pool;
00025 
00031 enum ProductionLevels {
00032   PRODLEVEL_CLOSURE = 0x00,  
00033   PRODLEVEL_MINIMUM = 0x04,  
00034   PRODLEVEL_DEFAULT = 0x10,  
00035   PRODLEVEL_MAXIMUM = 0x80,  
00036 };
00037 
00041 struct Industry : IndustryPool::PoolItem<&_industry_pool>, CargoSourceSink {
00042   typedef PersistentStorageArray<int32, 16> PersistentStorage;
00043 
00044   TileArea location;                  
00045   const Town *town;                   
00046   CargoID produced_cargo[2];          
00047   uint16 produced_cargo_waiting[2];   
00048   uint16 incoming_cargo_waiting[3];   
00049   byte production_rate[2];            
00050   byte prod_level;                    
00051   CargoID accepts_cargo[3];           
00052   uint32 produced_accepted_mask;      
00053   uint16 this_month_production[2];    
00054   uint16 this_month_transported[2];   
00055   byte last_month_pct_transported[2]; 
00056   uint16 last_month_production[2];    
00057   uint16 last_month_transported[2];   
00058   uint16 average_production[2];       
00059   uint16 counter;                     
00060 
00061   IndustryType type;                  
00062   OwnerByte owner;                    
00063   byte random_colour;                 
00064   Year last_prod_year;                
00065   byte was_cargo_delivered;           
00066 
00067   PartOfSubsidyByte part_of_subsidy;  
00068 
00069   OwnerByte founder;                  
00070   Date construction_date;             
00071   uint8 construction_type;            
00072   Date last_cargo_accepted_at;        
00073   byte selected_layout;               
00074 
00075   byte random_triggers;               
00076   uint16 random;                      
00077 
00078   PersistentStorage psa;              
00079 
00080   Industry(TileIndex tile = INVALID_TILE) : location(tile, 0, 0) {}
00081   ~Industry();
00082 
00083   void RecomputeProductionMultipliers();
00084 
00085   /* virtual */ SourceType GetType() const
00086   {
00087     return ST_INDUSTRY;
00088   }
00089 
00090   /* virtual */ SourceID GetID() const
00091   {
00092     return this->index;
00093   }
00094 
00095   /* virtual */ bool AcceptsCargo(CargoID cid) const
00096   {
00097     if (HasBit(this->produced_accepted_mask, cid)) return true;
00098 
00099     for (uint i = 0; i < lengthof(this->accepts_cargo); i++) {
00100       if (this->accepts_cargo[i] == cid) return true;
00101     }
00102     return false;
00103   }
00104 
00105   /* virtual */ bool SuppliesCargo(CargoID cid) const
00106   {
00107     for (uint i = 0; i < lengthof(this->produced_cargo); i++) {
00108       if (this->produced_cargo[i] == cid) return true;
00109     }
00110     return false;
00111   }
00112 
00113   /* virtual */ uint GetDestinationWeight(CargoID cid, byte weight_mod) const;
00114 
00115   /* virtual */ TileArea GetTileForDestination(CargoID cid)
00116   {
00117     return this->location;
00118   }
00119 
00126   static FORCEINLINE Industry *GetByTile(TileIndex tile)
00127   {
00128     return Industry::Get(GetIndustryIndex(tile));
00129   }
00130 
00132   typedef bool (*EnumIndustryProc)(const Industry *ind, void *data);
00133 
00134   static Industry *GetRandom(EnumIndustryProc enum_proc = NULL, IndustryID skip = INVALID_INDUSTRY, void *data = NULL);
00135   static void PostDestructor(size_t index);
00136 
00142   static inline void IncIndustryTypeCount(IndustryType type)
00143   {
00144     assert(type < NUM_INDUSTRYTYPES);
00145     counts[type]++;
00146   }
00147 
00153   static inline void DecIndustryTypeCount(IndustryType type)
00154   {
00155     assert(type < NUM_INDUSTRYTYPES);
00156     counts[type]--;
00157   }
00158 
00164   static inline uint16 GetIndustryTypeCount(IndustryType type)
00165   {
00166     assert(type < NUM_INDUSTRYTYPES);
00167     return counts[type];
00168   }
00169 
00171   static inline void ResetIndustryCounts()
00172   {
00173     memset(&counts, 0, sizeof(counts));
00174   }
00175 
00176 protected:
00177   static uint16 counts[NUM_INDUSTRYTYPES]; 
00178 };
00179 
00180 void PlantRandomFarmField(const Industry *i);
00181 
00182 void ReleaseDisastersTargetingIndustry(IndustryID);
00183 
00184 void UpdateIndustryAcceptance(Industry *ind);
00185 
00186 #define FOR_ALL_INDUSTRIES_FROM(var, start) FOR_ALL_ITEMS_FROM(Industry, industry_index, var, start)
00187 #define FOR_ALL_INDUSTRIES(var) FOR_ALL_INDUSTRIES_FROM(var, 0)
00188 
00190 struct IndustryTypeBuildData {
00191   uint32 probability;  
00192   byte   min_number;   
00193   uint16 target_count; 
00194   uint16 max_wait;     
00195   uint16 wait_count;   
00196 
00197   void Reset();
00198 
00199   bool GetIndustryTypeData(IndustryType it);
00200 };
00201 
00205 struct IndustryBuildData {
00206   IndustryTypeBuildData builddata[NUM_INDUSTRYTYPES]; 
00207   uint32 wanted_inds; 
00208 
00209   void Reset();
00210 
00211   void SetupTargetCount();
00212   void TryBuildNewIndustry();
00213 
00214   void MonthlyLoop();
00215 };
00216 
00217 extern IndustryBuildData _industry_builder;
00218 
00219 #endif /* INDUSTRY_H */

Generated on Mon May 9 05:18:53 2011 for OpenTTD by  doxygen 1.6.1