newgrf_commons.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 
00015 #ifndef NEWGRF_COMMONS_H
00016 #define NEWGRF_COMMONS_H
00017 
00018 #include "tile_type.h"
00019 #include "sprite.h"
00020 #include "core/alloc_type.hpp"
00021 
00023 enum TileContext {
00024   TCX_NORMAL,         
00025   TCX_UPPER_HALFTILE, 
00026   TCX_ON_BRIDGE,      
00027 };
00028 
00034 struct NewGRFSpriteLayout : ZeroedMemoryAllocator, DrawTileSprites {
00035   void Allocate(uint num_sprites);
00036   void Clone(const DrawTileSeqStruct *source);
00037 
00042   void Clone(const DrawTileSprites *source)
00043   {
00044     assert(source != NULL && this != source);
00045     this->ground = source->ground;
00046     this->Clone(source->seq);
00047   }
00048 
00049   virtual ~NewGRFSpriteLayout()
00050   {
00051     free(const_cast<DrawTileSeqStruct*>(this->seq));
00052   }
00053 };
00054 
00067 struct EntityIDMapping {
00068   uint32 grfid;          
00069   uint8  entity_id;      
00070   uint8  substitute_id;  
00071 };
00072 
00073 class OverrideManagerBase {
00074 protected:
00075   uint16 *entity_overrides;
00076   uint32 *grfid_overrides;
00077 
00078   uint16 max_offset;       
00079   uint16 max_new_entities; 
00080 
00081   uint16 invalid_ID;       
00082   virtual bool CheckValidNewID(uint16 testid) { return true; }
00083 
00084 public:
00085   EntityIDMapping *mapping_ID; 
00086 
00087   OverrideManagerBase(uint16 offset, uint16 maximum, uint16 invalid);
00088   virtual ~OverrideManagerBase();
00089 
00090   void ResetOverride();
00091   void ResetMapping();
00092 
00093   void Add(uint8 local_id, uint32 grfid, uint entity_type);
00094   virtual uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id);
00095 
00096   uint16 GetSubstituteID(uint16 entity_id) const;
00097   virtual uint16 GetID(uint8 grf_local_id, uint32 grfid) const;
00098 
00099   inline uint16 GetMaxMapping() const { return max_new_entities; }
00100   inline uint16 GetMaxOffset() const { return max_offset; }
00101 };
00102 
00103 
00104 struct HouseSpec;
00105 class HouseOverrideManager : public OverrideManagerBase {
00106 public:
00107   HouseOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
00108       OverrideManagerBase(offset, maximum, invalid) {}
00109   void SetEntitySpec(const HouseSpec *hs);
00110 };
00111 
00112 
00113 struct IndustrySpec;
00114 class IndustryOverrideManager : public OverrideManagerBase {
00115 public:
00116   IndustryOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
00117       OverrideManagerBase(offset, maximum, invalid) {}
00118 
00119   virtual uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id);
00120   virtual uint16 GetID(uint8 grf_local_id, uint32 grfid) const;
00121   void SetEntitySpec(IndustrySpec *inds);
00122 };
00123 
00124 
00125 struct IndustryTileSpec;
00126 class IndustryTileOverrideManager : public OverrideManagerBase {
00127 protected:
00128   virtual bool CheckValidNewID(uint16 testid) { return testid != 0xFF; }
00129 public:
00130   IndustryTileOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
00131       OverrideManagerBase(offset, maximum, invalid) {}
00132 
00133   void SetEntitySpec(const IndustryTileSpec *indts);
00134 };
00135 
00136 struct AirportSpec;
00137 class AirportOverrideManager : public OverrideManagerBase {
00138 public:
00139   AirportOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
00140       OverrideManagerBase(offset, maximum, invalid) {}
00141 
00142   void SetEntitySpec(AirportSpec *inds);
00143 };
00144 
00145 struct AirportTileSpec;
00146 class AirportTileOverrideManager : public OverrideManagerBase {
00147 protected:
00148   virtual bool CheckValidNewID(uint16 testid) { return testid != 0xFF; }
00149 public:
00150   AirportTileOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
00151       OverrideManagerBase(offset, maximum, invalid) {}
00152 
00153   void SetEntitySpec(const AirportTileSpec *ats);
00154 };
00155 
00156 struct ObjectSpec;
00157 class ObjectOverrideManager : public OverrideManagerBase {
00158 protected:
00159   virtual bool CheckValidNewID(uint16 testid) { return testid != 0xFF; }
00160 public:
00161   ObjectOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
00162       OverrideManagerBase(offset, maximum, invalid) {}
00163 
00164   void SetEntitySpec(ObjectSpec *spec);
00165 };
00166 
00167 extern HouseOverrideManager _house_mngr;
00168 extern IndustryOverrideManager _industry_mngr;
00169 extern IndustryTileOverrideManager _industile_mngr;
00170 extern AirportOverrideManager _airport_mngr;
00171 extern AirportTileOverrideManager _airporttile_mngr;
00172 extern ObjectOverrideManager _object_mngr;
00173 
00174 uint32 GetTerrainType(TileIndex tile, TileContext context = TCX_NORMAL);
00175 TileIndex GetNearbyTile(byte parameter, TileIndex tile, bool signed_offsets = true);
00176 uint32 GetNearbyTileInformation(TileIndex tile);
00177 
00182 template <size_t Tcnt>
00183 struct GRFFilePropsBase {
00184   GRFFilePropsBase() : local_id(0), grffile(0)
00185   {
00186     /* The lack of some compilers to provide default constructors complying to the specs
00187      * requires us to zero the stuff ourself. */
00188     memset(spritegroup, 0, sizeof(spritegroup));
00189   }
00190 
00191   uint16 local_id;                             
00192   const struct GRFFile *grffile;               
00193   const struct SpriteGroup *spritegroup[Tcnt]; 
00194 };
00195 
00197 struct GRFFileProps : GRFFilePropsBase<1> {
00199   GRFFileProps(uint16 subst_id) :
00200       GRFFilePropsBase<1>(), subst_id(subst_id), override(subst_id)
00201   {
00202   }
00203 
00205   GRFFileProps() : GRFFilePropsBase<1>() {}
00206   uint16 subst_id;
00207   uint16 override;                      
00208 };
00209 
00210 #endif /* NEWGRF_COMMONS_H */

Generated on Fri May 27 04:19:45 2011 for OpenTTD by  doxygen 1.6.1