newgrf_object.h

Go to the documentation of this file.
00001 /* $Id: newgrf_object.h 24693 2012-11-10 20:46:39Z alberth $ */
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_OBJECT_H
00013 #define NEWGRF_OBJECT_H
00014 
00015 #include "newgrf_callbacks.h"
00016 #include "newgrf_spritegroup.h"
00017 #include "newgrf_town.h"
00018 #include "economy_func.h"
00019 #include "date_type.h"
00020 #include "object_type.h"
00021 #include "newgrf_animation_type.h"
00022 #include "newgrf_class.h"
00023 #include "newgrf_commons.h"
00024 
00026 enum ObjectFlags {
00027   OBJECT_FLAG_NONE               =       0, 
00028   OBJECT_FLAG_ONLY_IN_SCENEDIT   = 1 <<  0, 
00029   OBJECT_FLAG_CANNOT_REMOVE      = 1 <<  1, 
00030   OBJECT_FLAG_AUTOREMOVE         = 1 <<  2, 
00031   OBJECT_FLAG_BUILT_ON_WATER     = 1 <<  3, 
00032   OBJECT_FLAG_CLEAR_INCOME       = 1 <<  4, 
00033   OBJECT_FLAG_HAS_NO_FOUNDATION  = 1 <<  5, 
00034   OBJECT_FLAG_ANIMATION          = 1 <<  6, 
00035   OBJECT_FLAG_ONLY_IN_GAME       = 1 <<  7, 
00036   OBJECT_FLAG_2CC_COLOUR         = 1 <<  8, 
00037   OBJECT_FLAG_NOT_ON_LAND        = 1 <<  9, 
00038   OBJECT_FLAG_DRAW_WATER         = 1 << 10, 
00039   OBJECT_FLAG_ALLOW_UNDER_BRIDGE = 1 << 11, 
00040   OBJECT_FLAG_ANIM_RANDOM_BITS   = 1 << 12, 
00041 };
00042 DECLARE_ENUM_AS_BIT_SET(ObjectFlags)
00043 
00044 void ResetObjects();
00045 
00047 enum ObjectClassID {
00048   OBJECT_CLASS_BEGIN   =    0, 
00049   OBJECT_CLASS_MAX     =   32, 
00050   INVALID_OBJECT_CLASS = 0xFF, 
00051 };
00053 DECLARE_POSTFIX_INCREMENT(ObjectClassID)
00054 
00055 
00056 struct ObjectSpec {
00057   /* 2 because of the "normal" and "buy" sprite stacks. */
00058   GRFFilePropsBase<2> grf_prop; 
00059   ObjectClassID cls_id;         
00060   StringID name;                
00061 
00062   uint8 climate;                
00063   uint8 size;                   
00064   uint8 build_cost_multiplier;  
00065   uint8 clear_cost_multiplier;  
00066   Date introduction_date;       
00067   Date end_of_life_date;        
00068   ObjectFlags flags;            
00069   AnimationInfo animation;      
00070   uint16 callback_mask;         
00071   uint8 height;                 
00072   uint8 views;                  
00073   bool enabled;                 
00074 
00079   Money GetBuildCost() const { return GetPrice(PR_BUILD_OBJECT, this->build_cost_multiplier, this->grf_prop.grffile, 0); }
00080 
00085   Money GetClearCost() const { return GetPrice(PR_CLEAR_OBJECT, this->clear_cost_multiplier, this->grf_prop.grffile, 0); }
00086 
00087   bool IsEverAvailable() const;
00088   bool IsAvailable() const;
00089   uint Index() const;
00090 
00091   static const ObjectSpec *Get(ObjectType index);
00092   static const ObjectSpec *GetByTile(TileIndex tile);
00093 };
00094 
00096 struct ObjectScopeResolver : public ScopeResolver {
00097   struct Object *obj; 
00098   TileIndex tile;     
00099   uint8 view;         
00100 
00101   ObjectScopeResolver(ResolverObject *ro, Object *obj, TileIndex tile, uint8 view = 0);
00102 
00103   /* virtual */ uint32 GetRandomBits() const;
00104   /* virtual */ uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
00105 };
00106 
00108 struct ObjectResolverObject : public ResolverObject {
00109   ObjectScopeResolver object_scope; 
00110   TownScopeResolver *town_scope;    
00111 
00112   ObjectResolverObject(const ObjectSpec *spec, Object *o, TileIndex tile, uint8 view = 0,
00113       CallbackID callback = CBID_NO_CALLBACK, uint32 param1 = 0, uint32 param2 = 0);
00114   ~ObjectResolverObject();
00115 
00116   /* virtual */ ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0)
00117   {
00118     switch (scope) {
00119       case VSG_SCOPE_SELF:
00120         return &this->object_scope;
00121 
00122       case VSG_SCOPE_PARENT: {
00123         TownScopeResolver *tsr = this->GetTown();
00124         if (tsr != NULL) return tsr;
00125         /* FALL-THROUGH */
00126       }
00127 
00128       default: return ResolverObject::GetScope(scope, relative);
00129     }
00130   }
00131 
00132 private:
00133   TownScopeResolver *GetTown();
00134 };
00135 
00137 typedef NewGRFClass<ObjectSpec, ObjectClassID, OBJECT_CLASS_MAX> ObjectClass;
00138 
00140 static const CargoID CT_PURCHASE_OBJECT = 1;
00141 
00142 uint16 GetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, uint8 view = 0);
00143 
00144 void DrawNewObjectTile(TileInfo *ti, const ObjectSpec *spec);
00145 void DrawNewObjectTileInGUI(int x, int y, const ObjectSpec *spec, uint8 view);
00146 void AnimateNewObjectTile(TileIndex tile);
00147 void TriggerObjectTileAnimation(Object *o, TileIndex tile, ObjectAnimationTrigger trigger, const ObjectSpec *spec);
00148 void TriggerObjectAnimation(Object *o, ObjectAnimationTrigger trigger, const ObjectSpec *spec);
00149 
00150 #endif /* NEWGRF_OBJECT_H */