newgrf_spritegroup.h

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

Generated on Sun May 15 19:20:11 2011 for OpenTTD by  doxygen 1.6.1