newgrf_spritegroup.h

Go to the documentation of this file.
00001 /* $Id: newgrf_spritegroup.h 21454 2010-12-10 21:32:52Z 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 
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 "core/pool_type.hpp"
00019 #include "house_type.h"
00020 
00021 #include "newgrf_callbacks.h"
00022 #include "newgrf_generic.h"
00023 #include "newgrf_storage.h"
00024 #include "newgrf_commons.h"
00025 
00032 static inline uint32 GetRegister(uint i)
00033 {
00034   extern TemporaryStorageArray<int32, 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   DSGA_OP_SHL,  
00155   DSGA_OP_SHR,  
00156   DSGA_OP_SAR,  
00157 };
00158 
00159 
00160 struct DeterministicSpriteGroupAdjust {
00161   DeterministicSpriteGroupAdjustOperation operation;
00162   DeterministicSpriteGroupAdjustType type;
00163   byte variable;
00164   byte parameter; 
00165   byte shift_num;
00166   uint32 and_mask;
00167   uint32 add_val;
00168   uint32 divmod_val;
00169   const SpriteGroup *subroutine;
00170 };
00171 
00172 
00173 struct DeterministicSpriteGroupRange {
00174   const SpriteGroup *group;
00175   uint32 low;
00176   uint32 high;
00177 };
00178 
00179 
00180 struct DeterministicSpriteGroup : SpriteGroup {
00181   DeterministicSpriteGroup() : SpriteGroup(SGT_DETERMINISTIC) {}
00182   ~DeterministicSpriteGroup();
00183 
00184   VarSpriteGroupScope var_scope;
00185   DeterministicSpriteGroupSize size;
00186   byte num_adjusts;
00187   byte num_ranges;
00188   DeterministicSpriteGroupAdjust *adjusts;
00189   DeterministicSpriteGroupRange *ranges; // Dynamically allocated
00190 
00191   /* Dynamically allocated, this is the sole owner */
00192   const SpriteGroup *default_group;
00193 
00194 protected:
00195   const SpriteGroup *Resolve(ResolverObject *object) const;
00196 };
00197 
00198 enum RandomizedSpriteGroupCompareMode {
00199   RSG_CMP_ANY,
00200   RSG_CMP_ALL,
00201 };
00202 
00203 struct RandomizedSpriteGroup : SpriteGroup {
00204   RandomizedSpriteGroup() : SpriteGroup(SGT_RANDOMIZED) {}
00205   ~RandomizedSpriteGroup();
00206 
00207   VarSpriteGroupScope var_scope;  
00208 
00209   RandomizedSpriteGroupCompareMode cmp_mode; 
00210   byte triggers;
00211   byte count;
00212 
00213   byte lowest_randbit; 
00214   byte num_groups; 
00215 
00216   const SpriteGroup **groups; 
00217 
00218 protected:
00219   const SpriteGroup *Resolve(ResolverObject *object) const;
00220 };
00221 
00222 
00223 /* This contains a callback result. A failed callback has a value of
00224  * CALLBACK_FAILED */
00225 struct CallbackResultSpriteGroup : SpriteGroup {
00230   CallbackResultSpriteGroup(uint16 value) :
00231     SpriteGroup(SGT_CALLBACK),
00232     result(value)
00233   {
00234     /* Old style callback results have the highest byte 0xFF so signify it is a callback result
00235      * New style ones only have the highest bit set (allows 15-bit results, instead of just 8) */
00236     if ((this->result >> 8) == 0xFF) {
00237       this->result &= ~0xFF00;
00238     } else {
00239       this->result &= ~0x8000;
00240     }
00241   }
00242 
00243   uint16 result;
00244   uint16 GetCallbackResult() const { return this->result; }
00245 };
00246 
00247 
00248 /* A result sprite group returns the first SpriteID and the number of
00249  * sprites in the set */
00250 struct ResultSpriteGroup : SpriteGroup {
00257   ResultSpriteGroup(SpriteID sprite, byte num_sprites) :
00258     SpriteGroup(SGT_RESULT),
00259     sprite(sprite),
00260     num_sprites(num_sprites)
00261   {
00262   }
00263 
00264   SpriteID sprite;
00265   byte num_sprites;
00266   SpriteID GetResult() const { return this->sprite; }
00267   byte GetNumResults() const { return this->num_sprites; }
00268 };
00269 
00270 struct TileLayoutSpriteGroup : SpriteGroup {
00271   TileLayoutSpriteGroup() : SpriteGroup(SGT_TILELAYOUT) {}
00272   ~TileLayoutSpriteGroup();
00273 
00274   byte num_building_stages;    
00275   struct DrawTileSprites *dts;
00276 };
00277 
00278 struct IndustryProductionSpriteGroup : SpriteGroup {
00279   IndustryProductionSpriteGroup() : SpriteGroup(SGT_INDUSTRY_PRODUCTION) {}
00280 
00281   uint8 version;
00282   int16 subtract_input[3];  // signed
00283   uint16 add_output[2];     // unsigned
00284   uint8 again;
00285 };
00286 
00287 
00288 struct ResolverObject {
00289   CallbackID callback;
00290   uint32 callback_param1;
00291   uint32 callback_param2;
00292 
00293   byte trigger;
00294 
00295   uint32 last_value;          
00296   uint32 reseed;              
00297 
00298   VarSpriteGroupScope scope;  
00299   byte count;                 
00300 
00301   BaseStorageArray *psa;      
00302 
00303   const GRFFile *grffile;     
00304 
00305   union {
00306     struct {
00307       const struct Vehicle *self;
00308       const struct Vehicle *parent;
00309       EngineID self_type;
00310       bool info_view;                
00311     } vehicle;
00312     struct {
00313       TileIndex tile;
00314     } canal;
00315     struct {
00316       TileIndex tile;
00317       const struct BaseStation *st;
00318       const struct StationSpec *statspec;
00319       CargoID cargo_type;
00320     } station;
00321     struct {
00322       TileIndex tile;
00323       const Town *town;
00324       HouseID house_id;
00325       uint16 initial_random_bits;    
00326       bool not_yet_constructed;      
00327     } house;
00328     struct {
00329       TileIndex tile;
00330       Industry *ind;
00331       IndustryGfx gfx;
00332       IndustryType type;
00333     } industry;
00334     struct {
00335       const struct CargoSpec *cs;
00336     } cargo;
00337     struct {
00338       CargoID cargo_type;
00339       uint8 default_selection;
00340       uint8 src_industry;            
00341       uint8 dst_industry;            
00342       uint8 distance;
00343       AIConstructionEvent event;
00344       uint8 count;
00345       uint8 station_size;
00346     } generic;
00347     struct {
00348       TileIndex tile;                
00349       TileContext context;           
00350     } routes;
00351     struct {
00352       const struct Station *st;      
00353       byte airport_id;               
00354       byte layout;                   
00355       TileIndex tile;                
00356     } airport;
00357     struct {
00358       const struct Object *o;        
00359       TileIndex tile;                
00360       uint8 view;                    
00361     } object;
00362   } u;
00363 
00364   uint32 (*GetRandomBits)(const struct ResolverObject*);
00365   uint32 (*GetTriggers)(const struct ResolverObject*);
00366   void (*SetTriggers)(const struct ResolverObject*, int);
00367   uint32 (*GetVariable)(const struct ResolverObject*, byte, byte, bool*);
00368   const SpriteGroup *(*ResolveReal)(const struct ResolverObject*, const RealSpriteGroup*);
00369 };
00370 
00371 #endif /* NEWGRF_SPRITEGROUP_H */

Generated on Thu Jan 20 22:57:37 2011 for OpenTTD by  doxygen 1.6.1