# 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 #include "core/smallvec_type.hpp"
    00022
    00024 enum TileContext {
    00025   TCX_NORMAL,
    00026   TCX_UPPER_HALFTILE,
    00027   TCX_ON_BRIDGE,
    00028 };
    00029
    00033 enum TileLayoutFlags {
    00034   TLF_NOTHING           = 0x00,
    00035
    00036   TLF_DODRAW            = 0x01,
    00037   TLF_SPRITE            = 0x02,
    00038   TLF_PALETTE           = 0x04,
    00039   TLF_CUSTOM_PALETTE    = 0x08,
    00040
    00041   TLF_BB_XY_OFFSET      = 0x10,
    00042   TLF_BB_Z_OFFSET       = 0x20,
    00043
    00044   TLF_CHILD_X_OFFSET    = 0x10,
    00045   TLF_CHILD_Y_OFFSET    = 0x20,
    00046
    00047   TLF_SPRITE_VAR10      = 0x40,
    00048   TLF_PALETTE_VAR10     = 0x80,
    00049
    00050   TLF_KNOWN_FLAGS       = 0x7F,
    00051
    00053   TLF_DRAWING_FLAGS     = ~TLF_CUSTOM_PALETTE,
    00054
    00056   TLF_NON_GROUND_FLAGS  = TLF_BB_XY_OFFSET | TLF_BB_Z_OFFSET | TLF_CHILD_X_OFFSET | TLF_CHILD_Y_OFFSET,
    00057
    00059   TLF_VAR10_FLAGS       = TLF_SPRITE_VAR10 | TLF_PALETTE_VAR10,
    00060
    00062   TLF_SPRITE_REG_FLAGS  = TLF_DODRAW | TLF_SPRITE | TLF_BB_XY_OFFSET | TLF_BB_Z_OFFSET | TLF_CHILD_X_OFFSET | TLF_CHILD_Y_OFFSET,
    00063
    00065   TLF_PALETTE_REG_FLAGS = TLF_PALETTE,
    00066 };
    00067 DECLARE_ENUM_AS_BIT_SET(TileLayoutFlags)
    00068
    00069
    00072 struct TileLayoutRegisters {
    00073   TileLayoutFlags flags;
    00074   uint8 dodraw;
    00075   uint8 sprite;
    00076   uint8 palette;
    00077   union {
    00078     uint8 parent[3];
    00079     uint8 child[2];
    00080   } delta;
    00081   uint8 sprite_var10;
    00082   uint8 palette_var10;
    00083 };
    00084
    00085 static const uint TLR_MAX_VAR10 = 7;
    00086
    00092 struct NewGRFSpriteLayout : ZeroedMemoryAllocator, DrawTileSprites {
    00093   const TileLayoutRegisters *registers;
    00094
    00095   void Allocate(uint num_sprites);
    00096   void AllocateRegisters();
    00097   void Clone(const DrawTileSeqStruct *source);
    00098   void Clone(const NewGRFSpriteLayout *source);
    00099
    00104   void Clone(const DrawTileSprites *source)
    00105   {
    00106     assert(source != NULL && this != source);
    00107     this->ground = source->ground;
    00108     this->Clone(source->seq);
    00109   }
    00110
    00111   virtual ~NewGRFSpriteLayout()
    00112   {
    00113     free(const_cast<DrawTileSeqStruct*>(this->seq));
    00114     free(const_cast<TileLayoutRegisters*>(this->registers));
    00115   }
    00116
    00123   bool NeedsPreprocessing() const
    00124   {
    00125     return this->registers != NULL;
    00126   }
    00127
    00128   uint32 PrepareLayout(uint32 orig_offset, uint32 newgrf_ground_offset, uint32 newgrf_offset, bool separate_ground) const;
    00129   void ProcessRegisters(uint8 resolved_var10, uint32 resolved_sprite, bool separate_ground) const;
    00130
    00136   const DrawTileSeqStruct *GetLayout(PalSpriteID *ground) const
    00137   {
    00138     DrawTileSeqStruct *front = result_seq.Begin();
    00139     *ground = front->image;
    00140     return front + 1;
    00141   }
    00142
    00143 private:
    00144   static SmallVector<DrawTileSeqStruct, 8> result_seq;
    00145 };
    00146
    00159 struct EntityIDMapping {
    00160   uint32 grfid;
    00161   uint8  entity_id;
    00162   uint8  substitute_id;
    00163 };
    00164
    00165 class OverrideManagerBase {
    00166 protected:
    00167   uint16 *entity_overrides;
    00168   uint32 *grfid_overrides;
    00169
    00170   uint16 max_offset;
    00171   uint16 max_new_entities;
    00172
    00173   uint16 invalid_ID;
    00174   virtual bool CheckValidNewID(uint16 testid) { return true; }
    00175
    00176 public:
    00177   EntityIDMapping *mapping_ID;
    00178
    00179   OverrideManagerBase(uint16 offset, uint16 maximum, uint16 invalid);
    00180   virtual ~OverrideManagerBase();
    00181
    00182   void ResetOverride();
    00183   void ResetMapping();
    00184
    00185   void Add(uint8 local_id, uint32 grfid, uint entity_type);
    00186   virtual uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id);
    00187
    00188   uint16 GetSubstituteID(uint16 entity_id) const;
    00189   virtual uint16 GetID(uint8 grf_local_id, uint32 grfid) const;
    00190
    00191   inline uint16 GetMaxMapping() const { return max_new_entities; }
    00192   inline uint16 GetMaxOffset() const { return max_offset; }
    00193 };
    00194
    00195
    00196 struct HouseSpec;
    00197 class HouseOverrideManager : public OverrideManagerBase {
    00198 public:
    00199   HouseOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
    00200       OverrideManagerBase(offset, maximum, invalid) {}
    00201   void SetEntitySpec(const HouseSpec *hs);
    00202 };
    00203
    00204
    00205 struct IndustrySpec;
    00206 class IndustryOverrideManager : public OverrideManagerBase {
    00207 public:
    00208   IndustryOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
    00209       OverrideManagerBase(offset, maximum, invalid) {}
    00210
    00211   virtual uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id);
    00212   virtual uint16 GetID(uint8 grf_local_id, uint32 grfid) const;
    00213   void SetEntitySpec(IndustrySpec *inds);
    00214 };
    00215
    00216
    00217 struct IndustryTileSpec;
    00218 class IndustryTileOverrideManager : public OverrideManagerBase {
    00219 protected:
    00220   virtual bool CheckValidNewID(uint16 testid) { return testid != 0xFF; }
    00221 public:
    00222   IndustryTileOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
    00223       OverrideManagerBase(offset, maximum, invalid) {}
    00224
    00225   void SetEntitySpec(const IndustryTileSpec *indts);
    00226 };
    00227
    00228 struct AirportSpec;
    00229 class AirportOverrideManager : public OverrideManagerBase {
    00230 public:
    00231   AirportOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
    00232       OverrideManagerBase(offset, maximum, invalid) {}
    00233
    00234   void SetEntitySpec(AirportSpec *inds);
    00235 };
    00236
    00237 struct AirportTileSpec;
    00238 class AirportTileOverrideManager : public OverrideManagerBase {
    00239 protected:
    00240   virtual bool CheckValidNewID(uint16 testid) { return testid != 0xFF; }
    00241 public:
    00242   AirportTileOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
    00243       OverrideManagerBase(offset, maximum, invalid) {}
    00244
    00245   void SetEntitySpec(const AirportTileSpec *ats);
    00246 };
    00247
    00248 struct ObjectSpec;
    00249 class ObjectOverrideManager : public OverrideManagerBase {
    00250 protected:
    00251   virtual bool CheckValidNewID(uint16 testid) { return testid != 0xFF; }
    00252 public:
    00253   ObjectOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
    00254       OverrideManagerBase(offset, maximum, invalid) {}
    00255
    00256   void SetEntitySpec(ObjectSpec *spec);
    00257 };
    00258
    00259 extern HouseOverrideManager _house_mngr;
    00260 extern IndustryOverrideManager _industry_mngr;
    00261 extern IndustryTileOverrideManager _industile_mngr;
    00262 extern AirportOverrideManager _airport_mngr;
    00263 extern AirportTileOverrideManager _airporttile_mngr;
    00264 extern ObjectOverrideManager _object_mngr;
    00265
    00266 uint32 GetTerrainType(TileIndex tile, TileContext context = TCX_NORMAL);
    00267 TileIndex GetNearbyTile(byte parameter, TileIndex tile, bool signed_offsets = true);
    00268 uint32 GetNearbyTileInformation(TileIndex tile);
    00269
    00274 template <size_t Tcnt>
    00275 struct GRFFilePropsBase {
    00276   GRFFilePropsBase() : local_id(0), grffile(0)
    00277   {
    00278     /* The lack of some compilers to provide default constructors complying to the specs
    00279      * requires us to zero the stuff ourself. */
    00280     memset(spritegroup, 0, sizeof(spritegroup));
    00281   }
    00282
    00283   uint16 local_id;
    00284   const struct GRFFile *grffile;
    00285   const struct SpriteGroup *spritegroup[Tcnt];
    00286 };
    00287
    00289 struct GRFFileProps : GRFFilePropsBase<1> {
    00291   GRFFileProps(uint16 subst_id) :
    00292       GRFFilePropsBase<1>(), subst_id(subst_id), override(subst_id)
    00293   {
    00294   }
    00295
    00297   GRFFileProps() : GRFFilePropsBase<1>() {}
    00298   uint16 subst_id;
    00299   uint16 override;
    00300 };
    00301
    00302 #endif /* NEWGRF_COMMONS_H */