newgrf_spritegroup.h

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

Generated on Tue Jan 5 21:02:56 2010 for OpenTTD by  doxygen 1.5.6