# newgrf_spritegroup.h

Go to the documentation of this file.

    00001 /* $Id: newgrf_spritegroup.h 19056 2010-02-07 22:22:54Z peter1138 $ */
    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 NEWGRF_SPRITEGROUP_H
    00013 #define NEWGRF_SPRITEGROUP_H
    00014
    00015 #include "town_type.h"
    00016 #include "gfx_type.h"
    00017 #include "engine_type.h"
    00018 #include "tile_type.h"
    00019 #include "core/pool_type.hpp"
    00020 #include "house_type.h"
    00021
    00022 #include "newgrf_callbacks.h"
    00023 #include "newgrf_generic.h"
    00024 #include "newgrf_storage.h"
    00025
    00032 static inline uint32 GetRegister(uint i)
    00033 {
    00034   extern TemporaryStorageArray<uint32, 0x110> _temp_store;
    00035   return _temp_store.Get(i);
    00036 }
    00037
    00038 /* List of different sprite group types */
    00039 enum SpriteGroupType {
    00040   SGT_REAL,
    00041   SGT_DETERMINISTIC,
    00042   SGT_RANDOMIZED,
    00043   SGT_CALLBACK,
    00044   SGT_RESULT,
    00045   SGT_TILELAYOUT,
    00046   SGT_INDUSTRY_PRODUCTION,
    00047 };
    00048
    00049 struct SpriteGroup;
    00050 typedef uint32 SpriteGroupID;
    00051
    00052 /* SPRITE_WIDTH is 24. ECS has roughly 30 sprite groups per real sprite.
    00053  * Adding an 'extra' margin would be assuming 64 sprite groups per real
    00054  * sprite. 64 = 2^6, so 2^30 should be enough (for now) */
    00055 typedef Pool<SpriteGroup, SpriteGroupID, 1024, 1 << 30> SpriteGroupPool;
    00056 extern SpriteGroupPool _spritegroup_pool;
    00057
    00058 /* Common wrapper for all the different sprite group types */
    00059 struct SpriteGroup : SpriteGroupPool::PoolItem<&_spritegroup_pool> {
    00060 protected:
    00061   SpriteGroup(SpriteGroupType type) : type(type) {}
    00063   virtual const SpriteGroup *Resolve(struct ResolverObject *object) const { return this; };
    00064
    00065 public:
    00066   virtual ~SpriteGroup() {}
    00067
    00068   SpriteGroupType type;
    00069
    00070   virtual SpriteID GetResult() const { return 0; }
    00071   virtual byte GetNumResults() const { return 0; }
    00072   virtual uint16 GetCallbackResult() const { return CALLBACK_FAILED; }
    00073
    00083   static const SpriteGroup *Resolve(const SpriteGroup *group, ResolverObject *object)
    00084   {
    00085     return group == NULL ? NULL : group->Resolve(object);
    00086   }
    00087 };
    00088
    00089
    00090 /* 'Real' sprite groups contain a list of other result or callback sprite
    00091  * groups. */
    00092 struct RealSpriteGroup : SpriteGroup {
    00093   RealSpriteGroup() : SpriteGroup(SGT_REAL) {}
    00094   ~RealSpriteGroup();
    00095
    00096   /* Loaded = in motion, loading = not moving
    00097    * Each group contains several spritesets, for various loading stages */
    00098
    00099   /* XXX: For stations the meaning is different - loaded is for stations
    00100    * with small amount of cargo whilst loading is for stations with a lot
    00101    * of da stuff. */
    00102
    00103   byte num_loaded;
    00104   byte num_loading;
    00105   const SpriteGroup **loaded;
    00106   const SpriteGroup **loading;
    00107
    00108 protected:
    00109   const SpriteGroup *Resolve(ResolverObject *object) const;
    00110 };
    00111
    00112 /* Shared by deterministic and random groups. */
    00113 enum VarSpriteGroupScope {
    00114   VSG_SCOPE_SELF,
    00115   /* Engine of consists for vehicles, city for stations. */
    00116   VSG_SCOPE_PARENT,
    00117   /* Any vehicle in the consist (vehicles only) */
    00118   VSG_SCOPE_RELATIVE,
    00119 };
    00120
    00121 enum DeterministicSpriteGroupSize {
    00122   DSG_SIZE_BYTE,
    00123   DSG_SIZE_WORD,
    00124   DSG_SIZE_DWORD,
    00125 };
    00126
    00127 enum DeterministicSpriteGroupAdjustType {
    00128   DSGA_TYPE_NONE,
    00129   DSGA_TYPE_DIV,
    00130   DSGA_TYPE_MOD,
    00131 };
    00132
    00133 enum DeterministicSpriteGroupAdjustOperation {
    00134   DSGA_OP_ADD,
    00135   DSGA_OP_SUB,
    00136   DSGA_OP_SMIN,
    00137   DSGA_OP_SMAX,
    00138   DSGA_OP_UMIN,
    00139   DSGA_OP_UMAX,
    00140   DSGA_OP_SDIV,
    00141   DSGA_OP_SMOD,
    00142   DSGA_OP_UDIV,
    00143   DSGA_OP_UMOD,
    00144   DSGA_OP_MUL,
    00145   DSGA_OP_AND,
    00146   DSGA_OP_OR,
    00147   DSGA_OP_XOR,
    00148   DSGA_OP_STO,
    00149   DSGA_OP_RST,
    00150   DSGA_OP_STOP,
    00151   DSGA_OP_ROR,
    00152   DSGA_OP_SCMP,
    00153   DSGA_OP_UCMP,
    00154 };
    00155
    00156
    00157 struct DeterministicSpriteGroupAdjust {
    00158   DeterministicSpriteGroupAdjustOperation operation;
    00159   DeterministicSpriteGroupAdjustType type;
    00160   byte variable;
    00161   byte parameter;
    00162   byte shift_num;
    00163   uint32 and_mask;
    00164   uint32 add_val;
    00165   uint32 divmod_val;
    00166   const SpriteGroup *subroutine;
    00167 };
    00168
    00169
    00170 struct DeterministicSpriteGroupRange {
    00171   const SpriteGroup *group;
    00172   uint32 low;
    00173   uint32 high;
    00174 };
    00175
    00176
    00177 struct DeterministicSpriteGroup : SpriteGroup {
    00178   DeterministicSpriteGroup() : SpriteGroup(SGT_DETERMINISTIC) {}
    00179   ~DeterministicSpriteGroup();
    00180
    00181   VarSpriteGroupScope var_scope;
    00182   DeterministicSpriteGroupSize size;
    00183   byte num_adjusts;
    00184   byte num_ranges;
    00185   DeterministicSpriteGroupAdjust *adjusts;
    00186   DeterministicSpriteGroupRange *ranges; // Dynamically allocated
    00187
    00188   /* Dynamically allocated, this is the sole owner */
    00189   const SpriteGroup *default_group;
    00190
    00191 protected:
    00192   const SpriteGroup *Resolve(ResolverObject *object) const;
    00193 };
    00194
    00195 enum RandomizedSpriteGroupCompareMode {
    00196   RSG_CMP_ANY,
    00197   RSG_CMP_ALL,
    00198 };
    00199
    00200 struct RandomizedSpriteGroup : SpriteGroup {
    00201   RandomizedSpriteGroup() : SpriteGroup(SGT_RANDOMIZED) {}
    00202   ~RandomizedSpriteGroup();
    00203
    00204   VarSpriteGroupScope var_scope;
    00205
    00206   RandomizedSpriteGroupCompareMode cmp_mode;
    00207   byte triggers;
    00208   byte count;
    00209
    00210   byte lowest_randbit;
    00211   byte num_groups;
    00212
    00213   const SpriteGroup **groups;
    00214
    00215 protected:
    00216   const SpriteGroup *Resolve(ResolverObject *object) const;
    00217 };
    00218
    00219
    00220 /* This contains a callback result. A failed callback has a value of
    00221  * CALLBACK_FAILED */
    00222 struct CallbackResultSpriteGroup : SpriteGroup {
    00227   CallbackResultSpriteGroup(uint16 value) :
    00228     SpriteGroup(SGT_CALLBACK),
    00229     result(value)
    00230   {
    00231     /* Old style callback results have the highest byte 0xFF so signify it is a callback result
    00232      * New style ones only have the highest bit set (allows 15-bit results, instead of just 8) */
    00233     if ((this->result >> 8) == 0xFF) {
    00234       this->result &= ~0xFF00;
    00235     } else {
    00236       this->result &= ~0x8000;
    00237     }
    00238   }
    00239
    00240   uint16 result;
    00241   uint16 GetCallbackResult() const { return this->result; }
    00242 };
    00243
    00244
    00245 /* A result sprite group returns the first SpriteID and the number of
    00246  * sprites in the set */
    00247 struct ResultSpriteGroup : SpriteGroup {
    00254   ResultSpriteGroup(SpriteID sprite, byte num_sprites) :
    00255     SpriteGroup(SGT_RESULT),
    00256     sprite(sprite),
    00257     num_sprites(num_sprites)
    00258   {
    00259   }
    00260
    00261   SpriteID sprite;
    00262   byte num_sprites;
    00263   SpriteID GetResult() const { return this->sprite; }
    00264   byte GetNumResults() const { return this->num_sprites; }
    00265 };
    00266
    00267 struct TileLayoutSpriteGroup : SpriteGroup {
    00268   TileLayoutSpriteGroup() : SpriteGroup(SGT_TILELAYOUT) {}
    00269   ~TileLayoutSpriteGroup();
    00270
    00271   byte num_building_stages;
    00272   struct DrawTileSprites *dts;
    00273 };
    00274
    00275 struct IndustryProductionSpriteGroup : SpriteGroup {
    00276   IndustryProductionSpriteGroup() : SpriteGroup(SGT_INDUSTRY_PRODUCTION) {}
    00277
    00278   uint8 version;
    00279   int16 subtract_input[3];  // signed
    00280   uint16 add_output[2];     // unsigned
    00281   uint8 again;
    00282 };
    00283
    00284
    00285 struct ResolverObject {
    00286   CallbackID callback;
    00287   uint32 callback_param1;
    00288   uint32 callback_param2;
    00289
    00290   byte trigger;
    00291
    00292   uint32 last_value;
    00293   uint32 reseed;
    00294
    00295   VarSpriteGroupScope scope;
    00296   byte count;
    00297
    00298   BaseStorageArray *psa;
    00299
    00300   const GRFFile *grffile;
    00301
    00302   union {
    00303     struct {
    00304       const struct Vehicle *self;
    00305       const struct Vehicle *parent;
    00306       EngineID self_type;
    00307       bool info_view;
    00308     } vehicle;
    00309     struct {
    00310       TileIndex tile;
    00311     } canal;
    00312     struct {
    00313       TileIndex tile;
    00314       const struct BaseStation *st;
    00315       const struct StationSpec *statspec;
    00316       CargoID cargo_type;
    00317     } station;
    00318     struct {
    00319       TileIndex tile;
    00320       Town *town;
    00321       HouseID house_id;
    00322     } house;
    00323     struct {
    00324       TileIndex tile;
    00325       Industry *ind;
    00326       IndustryGfx gfx;
    00327       IndustryType type;
    00328     } industry;
    00329     struct {
    00330       const struct CargoSpec *cs;
    00331     } cargo;
    00332     struct {
    00333       CargoID cargo_type;
    00334       uint8 default_selection;
    00335       IndustryType src_industry;
    00336       IndustryType dst_industry;
    00337       uint8 distance;
    00338       AIConstructionEvent event;
    00339       uint8 count;
    00340       uint8 station_size;
    00341     } generic;
    00342     struct {
    00343       TileIndex tile;
    00344     } routes;
    00345   } u;
    00346
    00347   uint32 (*GetRandomBits)(const struct ResolverObject*);
    00348   uint32 (*GetTriggers)(const struct ResolverObject*);
    00349   void (*SetTriggers)(const struct ResolverObject*, int);
    00350   uint32 (*GetVariable)(const struct ResolverObject*, byte, byte, bool*);
    00351   const SpriteGroup *(*ResolveReal)(const struct ResolverObject*, const RealSpriteGroup*);
    00352 };
    00353
    00354 #endif /* NEWGRF_SPRITEGROUP_H */