newgrf_commons.h

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