newgrf_commons.h

Go to the documentation of this file.
00001 /* $Id: newgrf_commons.h 23198 2011-11-12 13:00:29Z 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 
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 #include "core/smallvec_type.hpp"
00022 #include "command_type.h"
00023 #include "direction_type.h"
00024 #include "company_type.h"
00025 
00027 enum TileContext {
00028   TCX_NORMAL,         
00029   TCX_UPPER_HALFTILE, 
00030   TCX_ON_BRIDGE,      
00031 };
00032 
00036 enum TileLayoutFlags {
00037   TLF_NOTHING           = 0x00,
00038 
00039   TLF_DODRAW            = 0x01,   
00040   TLF_SPRITE            = 0x02,   
00041   TLF_PALETTE           = 0x04,   
00042   TLF_CUSTOM_PALETTE    = 0x08,   
00043 
00044   TLF_BB_XY_OFFSET      = 0x10,   
00045   TLF_BB_Z_OFFSET       = 0x20,   
00046 
00047   TLF_CHILD_X_OFFSET    = 0x10,   
00048   TLF_CHILD_Y_OFFSET    = 0x20,   
00049 
00050   TLF_SPRITE_VAR10      = 0x40,   
00051   TLF_PALETTE_VAR10     = 0x80,   
00052 
00053   TLF_KNOWN_FLAGS       = 0x7F,   
00054 
00056   TLF_DRAWING_FLAGS     = ~TLF_CUSTOM_PALETTE,
00057 
00059   TLF_NON_GROUND_FLAGS  = TLF_BB_XY_OFFSET | TLF_BB_Z_OFFSET | TLF_CHILD_X_OFFSET | TLF_CHILD_Y_OFFSET,
00060 
00062   TLF_VAR10_FLAGS       = TLF_SPRITE_VAR10 | TLF_PALETTE_VAR10,
00063 
00065   TLF_SPRITE_REG_FLAGS  = TLF_DODRAW | TLF_SPRITE | TLF_BB_XY_OFFSET | TLF_BB_Z_OFFSET | TLF_CHILD_X_OFFSET | TLF_CHILD_Y_OFFSET,
00066 
00068   TLF_PALETTE_REG_FLAGS = TLF_PALETTE,
00069 };
00070 DECLARE_ENUM_AS_BIT_SET(TileLayoutFlags)
00071 
00072 
00078 static inline uint GetConstructionStageOffset(uint construction_stage, uint num_sprites)
00079 {
00080   assert(num_sprites > 0);
00081   if (num_sprites > 4) num_sprites = 4;
00082   switch (construction_stage) {
00083     case 0: return 0;
00084     case 1: return num_sprites > 2 ? 1 : 0;
00085     case 2: return num_sprites > 2 ? num_sprites - 2 : 0;
00086     case 3: return num_sprites - 1;
00087     default: NOT_REACHED();
00088   }
00089 }
00090 
00094 struct TileLayoutRegisters {
00095   TileLayoutFlags flags; 
00096   uint8 dodraw;          
00097   uint8 sprite;          
00098   uint8 palette;         
00099   uint16 max_sprite_offset;  
00100   uint16 max_palette_offset; 
00101   union {
00102     uint8 parent[3];   
00103     uint8 child[2];    
00104   } delta;
00105   uint8 sprite_var10;    
00106   uint8 palette_var10;   
00107 };
00108 
00109 static const uint TLR_MAX_VAR10 = 7; 
00110 
00116 struct NewGRFSpriteLayout : ZeroedMemoryAllocator, DrawTileSprites {
00117   const TileLayoutRegisters *registers;
00118 
00123   uint consistent_max_offset;
00124 
00125   void Allocate(uint num_sprites);
00126   void AllocateRegisters();
00127   void Clone(const DrawTileSeqStruct *source);
00128   void Clone(const NewGRFSpriteLayout *source);
00129 
00134   void Clone(const DrawTileSprites *source)
00135   {
00136     assert(source != NULL && this != source);
00137     this->ground = source->ground;
00138     this->Clone(source->seq);
00139   }
00140 
00141   virtual ~NewGRFSpriteLayout()
00142   {
00143     free(this->seq);
00144     free(this->registers);
00145   }
00146 
00153   bool NeedsPreprocessing() const
00154   {
00155     return this->registers != NULL;
00156   }
00157 
00158   uint32 PrepareLayout(uint32 orig_offset, uint32 newgrf_ground_offset, uint32 newgrf_offset, uint constr_stage, bool separate_ground) const;
00159   void ProcessRegisters(uint8 resolved_var10, uint32 resolved_sprite, bool separate_ground) const;
00160 
00166   const DrawTileSeqStruct *GetLayout(PalSpriteID *ground) const
00167   {
00168     DrawTileSeqStruct *front = result_seq.Begin();
00169     *ground = front->image;
00170     return front + 1;
00171   }
00172 
00173 private:
00174   static SmallVector<DrawTileSeqStruct, 8> result_seq; 
00175 };
00176 
00189 struct EntityIDMapping {
00190   uint32 grfid;          
00191   uint8  entity_id;      
00192   uint8  substitute_id;  
00193 };
00194 
00195 class OverrideManagerBase {
00196 protected:
00197   uint16 *entity_overrides;
00198   uint32 *grfid_overrides;
00199 
00200   uint16 max_offset;       
00201   uint16 max_new_entities; 
00202 
00203   uint16 invalid_ID;       
00204   virtual bool CheckValidNewID(uint16 testid) { return true; }
00205 
00206 public:
00207   EntityIDMapping *mapping_ID; 
00208 
00209   OverrideManagerBase(uint16 offset, uint16 maximum, uint16 invalid);
00210   virtual ~OverrideManagerBase();
00211 
00212   void ResetOverride();
00213   void ResetMapping();
00214 
00215   void Add(uint8 local_id, uint32 grfid, uint entity_type);
00216   virtual uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id);
00217 
00218   uint32 GetGRFID(uint16 entity_id) const;
00219   uint16 GetSubstituteID(uint16 entity_id) const;
00220   virtual uint16 GetID(uint8 grf_local_id, uint32 grfid) const;
00221 
00222   inline uint16 GetMaxMapping() const { return max_new_entities; }
00223   inline uint16 GetMaxOffset() const { return max_offset; }
00224 };
00225 
00226 
00227 struct HouseSpec;
00228 class HouseOverrideManager : public OverrideManagerBase {
00229 public:
00230   HouseOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
00231       OverrideManagerBase(offset, maximum, invalid) {}
00232   void SetEntitySpec(const HouseSpec *hs);
00233 };
00234 
00235 
00236 struct IndustrySpec;
00237 class IndustryOverrideManager : public OverrideManagerBase {
00238 public:
00239   IndustryOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
00240       OverrideManagerBase(offset, maximum, invalid) {}
00241 
00242   virtual uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id);
00243   virtual uint16 GetID(uint8 grf_local_id, uint32 grfid) const;
00244   void SetEntitySpec(IndustrySpec *inds);
00245 };
00246 
00247 
00248 struct IndustryTileSpec;
00249 class IndustryTileOverrideManager : public OverrideManagerBase {
00250 protected:
00251   virtual bool CheckValidNewID(uint16 testid) { return testid != 0xFF; }
00252 public:
00253   IndustryTileOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
00254       OverrideManagerBase(offset, maximum, invalid) {}
00255 
00256   void SetEntitySpec(const IndustryTileSpec *indts);
00257 };
00258 
00259 struct AirportSpec;
00260 class AirportOverrideManager : public OverrideManagerBase {
00261 public:
00262   AirportOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
00263       OverrideManagerBase(offset, maximum, invalid) {}
00264 
00265   void SetEntitySpec(AirportSpec *inds);
00266 };
00267 
00268 struct AirportTileSpec;
00269 class AirportTileOverrideManager : public OverrideManagerBase {
00270 protected:
00271   virtual bool CheckValidNewID(uint16 testid) { return testid != 0xFF; }
00272 public:
00273   AirportTileOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
00274       OverrideManagerBase(offset, maximum, invalid) {}
00275 
00276   void SetEntitySpec(const AirportTileSpec *ats);
00277 };
00278 
00279 struct ObjectSpec;
00280 class ObjectOverrideManager : public OverrideManagerBase {
00281 protected:
00282   virtual bool CheckValidNewID(uint16 testid) { return testid != 0xFF; }
00283 public:
00284   ObjectOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
00285       OverrideManagerBase(offset, maximum, invalid) {}
00286 
00287   void SetEntitySpec(ObjectSpec *spec);
00288 };
00289 
00290 extern HouseOverrideManager _house_mngr;
00291 extern IndustryOverrideManager _industry_mngr;
00292 extern IndustryTileOverrideManager _industile_mngr;
00293 extern AirportOverrideManager _airport_mngr;
00294 extern AirportTileOverrideManager _airporttile_mngr;
00295 extern ObjectOverrideManager _object_mngr;
00296 
00297 uint32 GetTerrainType(TileIndex tile, TileContext context = TCX_NORMAL);
00298 TileIndex GetNearbyTile(byte parameter, TileIndex tile, bool signed_offsets = true, Axis axis = INVALID_AXIS);
00299 uint32 GetNearbyTileInformation(TileIndex tile, bool grf_version8);
00300 uint32 GetCompanyInfo(CompanyID owner, const struct Livery *l = NULL);
00301 CommandCost GetErrorMessageFromLocationCallbackResult(uint16 cb_res, uint32 grfid, StringID default_error);
00302 
00303 void ErrorUnknownCallbackResult(uint32 grfid, uint16 cbid, uint16 cb_res);
00304 bool ConvertBooleanCallback(const struct GRFFile *grffile, uint16 cbid, uint16 cb_res);
00305 bool Convert8bitBooleanCallback(const struct GRFFile *grffile, uint16 cbid, uint16 cb_res);
00306 
00311 template <size_t Tcnt>
00312 struct GRFFilePropsBase {
00313   GRFFilePropsBase() : local_id(0), grffile(0)
00314   {
00315     /* The lack of some compilers to provide default constructors complying to the specs
00316      * requires us to zero the stuff ourself. */
00317     memset(spritegroup, 0, sizeof(spritegroup));
00318   }
00319 
00320   uint16 local_id;                             
00321   const struct GRFFile *grffile;               
00322   const struct SpriteGroup *spritegroup[Tcnt]; 
00323 };
00324 
00326 struct GRFFileProps : GRFFilePropsBase<1> {
00328   GRFFileProps(uint16 subst_id) :
00329       GRFFilePropsBase<1>(), subst_id(subst_id), override(subst_id)
00330   {
00331   }
00332 
00334   GRFFileProps() : GRFFilePropsBase<1>() {}
00335   uint16 subst_id;
00336   uint16 override;                      
00337 };
00338 
00339 #endif /* NEWGRF_COMMONS_H */