Pol  Revision:cb584c9
uobject.h
Go to the documentation of this file.
1 
12 #ifndef __UOBJECT_H
13 #define __UOBJECT_H
14 
15 #ifndef __CLIB_RAWTYPES_H
16 #include "../clib/rawtypes.h"
17 #endif
18 
19 #include <atomic>
20 #include <boost/any.hpp>
21 #include <boost/flyweight.hpp>
22 #include <iosfwd>
23 #include <map>
24 #include <set>
25 #include <string>
26 #include <type_traits>
27 
28 #include "../clib/boostutils.h"
29 #include "../clib/refptr.h"
30 #include "baseobject.h"
31 #include "dynproperties.h"
32 #include "proplist.h"
33 
34 #define pf_endl '\n'
35 
36 
37 namespace Pol
38 {
39 namespace Bscript
40 {
41 class BObjectImp;
42 class Executor;
43 }
44 namespace Clib
45 {
46 class ConfigElem;
47 }
48 namespace Items
49 {
50 class Item;
51 class UArmor;
52 class UWeapon;
53 }
54 namespace Mobile
55 {
56 class Character;
57 class NPC;
58 }
59 namespace Multi
60 {
61 class UMulti;
62 class UBoat;
63 }
64 namespace Core
65 {
66 class UContainer;
67 class WornItemsContainer;
68 
69 #pragma pack( push, 1 )
71 {
77 };
78 
80 {
87 };
88 
90 {
96 };
97 
98 #pragma pack( pop )
99 
100 template <typename ENUM,
101  typename std::enable_if<
102  std::is_enum<ENUM>::value && !std::is_convertible<ENUM, int>::value, int>::type = 0>
104 {
105  typedef typename std::underlying_type<ENUM>::type enum_t;
106  AttributeFlags() : flags_( 0 ){};
107 
108  bool get( ENUM flag ) const
109  {
110  // no implicit conversion to bool, to be able to check against all bits set
111  return ( flags_ & static_cast<enum_t>( flag ) ) == static_cast<enum_t>( flag );
112  };
113  void set( ENUM flag ) { flags_ |= static_cast<enum_t>( flag ); };
114  void remove( ENUM flag ) { flags_ &= ~static_cast<enum_t>( flag ); };
115  void change( ENUM flag, bool value )
116  {
117  if ( value )
118  set( flag );
119  else
120  remove( flag );
121  }
122  void reset() { flags_ = 0; };
123 
124 private:
125  enum_t flags_;
126 };
127 
128 enum class OBJ_FLAGS : u16
129 {
130  DIRTY = 1 << 0, // UObject flags
131  SAVE_ON_EXIT = 1 << 1,
132  NEWBIE = 1 << 2, // Item flags
133  INSURED = 1 << 3,
134  MOVABLE = 1 << 4,
135  IN_USE = 1 << 5,
136  INVISIBLE = 1 << 6,
137  LOCKED = 1 << 7, // ULockable flag
138  CONTENT_TO_GRAVE = 1 << 8, // UCorpse flag
139  NO_DROP = 1 << 9, // Item flag
140  NO_DROP_EXCEPTION = 1 << 10, // Container/Character flag
141 };
142 
146 class UObject : protected ref_counted, public ULWObject, public DynamicPropsHolder
147 {
148 public:
149  virtual std::string name() const;
150  virtual std::string description() const;
151 
152  bool specific_name() const;
153  void setname( const std::string& );
154 
155  bool getprop( const std::string& propname, std::string& propvalue ) const;
156  void setprop( const std::string& propname, const std::string& propvalue );
157  void eraseprop( const std::string& propname );
158  void copyprops( const UObject& obj );
159  void copyprops( const PropertyList& proplist );
160  void getpropnames( std::vector<std::string>& propnames ) const;
161  const PropertyList& getprops() const;
162 
163  virtual void destroy();
164 
165  virtual unsigned int weight() const = 0;
166 
167  virtual UObject* toplevel_owner(); // this isn't really right, it returns the WornItemsContainer
168  virtual UObject* owner();
169  virtual const UObject* owner() const;
170  virtual UObject* self_as_owner();
171  virtual const UObject* self_as_owner() const;
172  virtual const UObject* toplevel_owner() const;
173  virtual bool setgraphic( u16 newobjtype );
174  virtual bool setcolor( u16 newcolor );
175  virtual void on_color_changed();
176 
177  virtual void setfacing( u8 newfacing ) = 0;
178  virtual void on_facing_changed();
179 
180  bool saveonexit() const;
181  void saveonexit( bool newvalue );
182 
183  virtual void printOn( Clib::StreamWriter& ) const;
184  virtual void printSelfOn( Clib::StreamWriter& sw ) const;
185 
186  virtual void printOnDebug( Clib::StreamWriter& sw ) const;
187  virtual void fixInvalidGraphic();
188  virtual void readProperties( Clib::ConfigElem& elem );
189  // virtual Bscript::BObjectImp* script_member( const char *membername );
190  virtual Bscript::BObjectImp* make_ref() = 0;
191  virtual Bscript::BObjectImp* get_script_member( const char* membername ) const;
192  virtual Bscript::BObjectImp* get_script_member_id( const int id ) const;
193 
194  virtual Bscript::BObjectImp* set_script_member( const char* membername,
195  const std::string& value );
196  virtual Bscript::BObjectImp* set_script_member( const char* membername, int value );
197  virtual Bscript::BObjectImp* set_script_member_double( const char* membername, double value );
198 
199  virtual Bscript::BObjectImp* set_script_member_id( const int id, const std::string& value );
200  virtual Bscript::BObjectImp* set_script_member_id( const int id, int value );
201  virtual Bscript::BObjectImp* set_script_member_id_double( const int id, double value );
202 
203  virtual Bscript::BObjectImp* script_method( const char* methodname, Bscript::Executor& ex );
204  virtual Bscript::BObjectImp* script_method_id( const int id, Bscript::Executor& ex );
205 
206  virtual Bscript::BObjectImp* custom_script_method( const char* methodname,
207  Bscript::Executor& ex );
208  virtual bool script_isa( unsigned isatype ) const;
209  virtual const char* target_tag() const;
210 
211  virtual const char* classname() const = 0;
212 
213  virtual size_t estimatedSize() const;
214 
215  void ref_counted_add_ref();
216  void ref_counted_release();
217  unsigned ref_counted_count() const;
218  ref_counted* as_ref_counted() { return this; }
219  inline void increv() { _rev++; };
220  inline u32 rev() const { return _rev; };
221  bool dirty() const;
222  void set_dirty();
223  void clear_dirty() const;
224  static std::atomic<unsigned int> dirty_writes;
225  static std::atomic<unsigned int> clean_writes;
226 
227 protected:
228  virtual void printProperties( Clib::StreamWriter& sw ) const;
229  virtual void printDebugProperties( Clib::StreamWriter& sw ) const;
230 
231  UObject( u32 objtype, UOBJ_CLASS uobj_class );
232  virtual ~UObject();
233 
234  friend class ref_ptr<UObject>;
235  friend class ref_ptr<Mobile::Character>;
236  friend class ref_ptr<Items::Item>;
237  friend class ref_ptr<Multi::UBoat>;
238  friend class ref_ptr<Multi::UMulti>;
239  friend class ref_ptr<Mobile::NPC>;
240  friend class ref_ptr<UContainer>;
241  friend class ref_ptr<Items::UWeapon>;
242  friend class ref_ptr<Items::UArmor>;
243  friend class ref_ptr<WornItemsContainer>;
244 
245  // DATA:
246 public:
247  u32 serial_ext;
248 
249  const u32 objtype_;
250  u16 color;
251 
252  u8 facing; // not always used for items.
253  // always used for characters
254 
255  DYN_PROPERTY( maxhp_mod, s16, PROP_MAXHP_MOD, 0 );
256  DYN_PROPERTY( fire_resist, ValueModPack, PROP_RESIST_FIRE, ValueModPack::DEFAULT );
257  DYN_PROPERTY( cold_resist, ValueModPack, PROP_RESIST_COLD, ValueModPack::DEFAULT );
258  DYN_PROPERTY( energy_resist, ValueModPack, PROP_RESIST_ENERGY, ValueModPack::DEFAULT );
259  DYN_PROPERTY( poison_resist, ValueModPack, PROP_RESIST_POISON, ValueModPack::DEFAULT );
260  DYN_PROPERTY( physical_resist, ValueModPack, PROP_RESIST_PHYSICAL, ValueModPack::DEFAULT );
261 
262  DYN_PROPERTY( fire_damage, ValueModPack, PROP_DAMAGE_FIRE, ValueModPack::DEFAULT );
263  DYN_PROPERTY( cold_damage, ValueModPack, PROP_DAMAGE_COLD, ValueModPack::DEFAULT );
264  DYN_PROPERTY( energy_damage, ValueModPack, PROP_DAMAGE_ENERGY, ValueModPack::DEFAULT );
265  DYN_PROPERTY( poison_damage, ValueModPack, PROP_DAMAGE_POISON, ValueModPack::DEFAULT );
266  DYN_PROPERTY( physical_damage, ValueModPack, PROP_DAMAGE_PHYSICAL, ValueModPack::DEFAULT );
267 
268 private:
269  u32 _rev;
270 
271 protected:
273  // mutable due to dirty flag
274  mutable AttributeFlags<OBJ_FLAGS> flags_;
275 
276 private:
277  PropertyList proplist_;
278 
279 private: // not implemented:
280  UObject( const UObject& );
281  UObject& operator=( const UObject& );
282 };
283 
284 extern Clib::StreamWriter& operator<<( Clib::StreamWriter&, const UObject& );
285 
286 inline bool UObject::specific_name() const
287 {
288  return !name_.get().empty();
289 }
290 
291 inline void UObject::set_dirty()
292 {
293  flags_.set( OBJ_FLAGS::DIRTY );
294 }
295 
296 inline void UObject::ref_counted_add_ref()
297 {
298  ref_counted::add_ref( REFERER_PARAM( this ) );
299 }
300 
301 inline void UObject::ref_counted_release()
302 {
303  ref_counted::release( REFERER_PARAM( this ) );
304 }
305 
306 inline unsigned UObject::ref_counted_count() const
307 {
308  return ref_counted::count();
309 }
310 
311 inline bool IsCharacter( u32 serial )
312 {
313  return ( ~serial & 0x40000000Lu ) ? true : false;
314 }
315 
316 inline bool IsItem( u32 serial )
317 {
318  return ( serial & 0x40000000Lu ) ? true : false;
319 }
320 }
321 }
322 
323 #endif
unsigned char u8
Definition: rawtypes.h:25
static std::atomic< unsigned int > dirty_writes
Definition: uobject.h:224
ref_counted * as_ref_counted()
Definition: uobject.h:218
unsigned short u16
Definition: rawtypes.h:26
unsigned int u32
Definition: rawtypes.h:27
static std::atomic< unsigned int > clean_writes
Definition: uobject.h:225
std::underlying_type< ENUM >::type enum_t
Definition: uobject.h:105
signed short s16
Definition: rawtypes.h:30
Definition: refptr.h:65
u32 rev() const
Definition: uobject.h:220
#define DYN_PROPERTY(name, type, id, defaultvalue)
Definition: dynproperties.h:62
std::string name
Definition: osmod.cpp:943
void change(ENUM flag, bool value)
Definition: uobject.h:115
ElementalType
Definition: uobject.h:79
boost::flyweight< std::string, boost::flyweights::tag< object_name_tag >, FLYWEIGHT_HASH_FACTORY > object_name_flystring
Definition: boostutils.h:142
Definition: berror.cpp:12