Pol  Revision:cb584c9
containr.h
Go to the documentation of this file.
1 
14 #ifndef CONTAINR_H
15 #define CONTAINR_H
16 
17 #include <stddef.h>
18 
19 #include "../clib/compilerspecifics.h"
20 #include "../clib/rawtypes.h"
21 #include "baseobject.h"
22 #include "dynproperties.h"
23 #ifndef ITEM_H
24 #include "item/item.h"
25 #endif
26 
27 #ifndef LOCKABLE_H
28 #include "lockable.h"
29 #endif
30 
31 #define CONTAINER_STORES_ITEMREF 0
32 
33 #if CONTAINER_STORES_ITEMREF
34 #define GET_ITEM_PTR( itr ) ( ( *itr ).get() )
35 #define ITEM_ELEM_PTR( elem ) ( elem.get() )
36 #define EMPTY_ELEM ItemRef( 0 )
37 #else
38 #define GET_ITEM_PTR( itr ) ( *itr )
39 #define ITEM_ELEM_PTR( elem ) ( elem )
40 #define EMPTY_ELEM 0
41 #endif
42 
43 
44 namespace Pol
45 {
46 namespace Clib
47 {
48 class ConfigElem;
49 namespace Pol
50 {
51 namespace Bscript
52 {
53 class BObjectImp;
54 class ObjArray;
55 } // namespace Bscript
56 namespace Clib
57 {
58 class StreamWriter;
59 } // namespace Clib
60 namespace Core
61 {
62 class UContainer;
63 } // namespace Core
64 namespace Items
65 {
66 class ItemDesc;
67 } // namespace Items
68 namespace Mobile
69 {
70 class NPC;
71 } // namespace Mobile
72 namespace Multi
73 {
74 class UMulti;
75 } // namespace Multi
76 namespace Network
77 {
78 class Client;
79 } // namespace Network
80 } // namespace Pol
81 } // namespace Clib
82 namespace Mobile
83 {
84 class Character;
85 }
86 namespace Items
87 {
88 class ContainerDesc;
89 }
90 namespace Module
91 {
93  bool send_aos_tooltip );
94 }
95 namespace Core
96 {
97 /* A container promises never to allow more than MAX_CONTAINER_ITEMS in it.
98  The user must check can_add() before adding, however, to make sure the
99  add would be legal, or an exception will be thrown.
100 
101  THAT IS: the user MUST check can_add() before adding.
102  */
103 
104 class UContainer : public ULockable
105 {
106 public:
107  typedef ULockable base;
108  virtual ~UContainer();
109  virtual size_t estimatedSize() const POL_OVERRIDE;
110 
111  virtual void destroy() POL_OVERRIDE;
112  void destroy_contents();
113  typedef std::vector<Items::Item*> Contents;
114  typedef Contents::iterator iterator;
115  typedef Contents::const_iterator const_iterator;
116 
117  virtual void builtin_on_use( Network::Client* client ) POL_OVERRIDE;
118 
119  // can_add(): doesn't look for matching stacks to add to.
120  virtual bool can_add( const Items::Item& item ) const;
121  virtual bool can_add( unsigned short more_weight ) const;
122 
123  virtual void spill_contents( Multi::UMulti* supporting_multi ) POL_OVERRIDE;
124 
125  virtual void add( Items::Item* item ); // NOTE: points item->container to self on insertion
126  void add_at_random_location( Items::Item* item );
127  unsigned count() const;
128 
129  virtual unsigned int item_count() const POL_OVERRIDE;
130  virtual unsigned int weight() const POL_OVERRIDE;
131 
132  bool can_add_bulk( int tli_diff, int item_count_diff, int weight_diff ) const;
133  virtual void add_bulk( int item_count_delta, int weight_delta );
134  void add_bulk( const Items::Item* item );
135  void remove_bulk( const Items::Item* item );
136 
137  bool can_add_to_slot( u8& slotIndex );
138  bool is_slot_empty( u8& slotIndex );
139  bool find_empty_slot( u8& slotIndex );
140 
141  Items::Item* find( u32 serial ) const;
142  Items::Item* find_toplevel( u32 serial ) const;
143  Items::Item* find_toplevel_objtype( u32 objtype ) const;
144  Items::Item* find_toplevel_objtype( u32 objtype, unsigned short maxamount ) const;
145  Items::Item* find_toplevel_polclass( unsigned int polclass ) const;
146  Items::Item* find_addable_stack( const Items::Item* adding_item ) const;
147 
148  Items::Item* find_toplevel_objtype_noninuse( u32 objtype ) const;
149  Items::Item* find_toplevel_objtype_noninuse( u32 objtype, unsigned short maxamount ) const;
150  Items::Item* find_objtype_noninuse( u32 objtype ) const;
151  Items::Item* find_objtype( u32 objtype, int flags ) const;
152 
153  virtual void for_each_item( void ( *f )( Item* item, void* a ), void* arg );
154 
155  virtual bool script_isa( unsigned isatype ) const POL_OVERRIDE;
156  virtual Items::Item* clone() const POL_OVERRIDE;
157 
158  unsigned int find_sumof_objtype_noninuse( u32 objtype ) const;
159  unsigned int find_sumof_objtype_noninuse( u32 objtype, u32 amtToGet, Contents& saveItemsTo,
160  int flags ) const;
161  void consume_sumof_objtype_noninuse( u32 objtype, unsigned int amount );
162 
163  UContainer* find_container( u32 serial ) const;
164 
165  // remove(): tells what subcontainer used to hold the item
166  // points item->container to nullptr on removal
167  virtual Items::Item* remove( u32 serial, UContainer** found_in = nullptr );
168  virtual void remove( Items::Item* item ); // item must be in this container
169  virtual void remove( iterator itr );
170 
171  enum MoveType
172  {
173  MT_PLAYER = 0, // default
174  MT_CORE_MOVED = 1,
175  MT_CORE_CREATED = 2
176  };
177 
178  // TODO: rework these too.
179  bool check_can_remove_script( Mobile::Character* chr, Items::Item* item,
180  MoveType move = MT_PLAYER );
181  void on_remove( Mobile::Character* chr, Items::Item* item, MoveType move = MT_PLAYER );
182  virtual void printProperties( Clib::StreamWriter& sw ) const POL_OVERRIDE;
183  virtual void readProperties( Clib::ConfigElem& elem ) POL_OVERRIDE;
184  virtual Bscript::BObjectImp* get_script_member( const char* membername ) const POL_OVERRIDE;
185  virtual Bscript::BObjectImp* get_script_member_id( const int id ) const POL_OVERRIDE;
186 
187  virtual Bscript::BObjectImp* set_script_member( const char* membername, int value ) POL_OVERRIDE;
188  virtual Bscript::BObjectImp* set_script_member_id( const int id,
189  int value ) POL_OVERRIDE; // id test
190 
191  bool can_insert_increase_stack( Mobile::Character* mob, MoveType move, Items::Item* existing_item,
192  unsigned short amt_to_add, Items::Item* adding_item );
193  bool can_insert_add_item( Mobile::Character* mob, MoveType move, Items::Item* new_item );
194 
195  void on_insert_increase_stack( Mobile::Character* mob, MoveType move, Items::Item* existing_item,
196  unsigned short amt_added );
197  virtual void on_insert_add_item( Mobile::Character* mob, MoveType move, Items::Item* new_item );
198 
199  virtual Mobile::Character* get_chr_owner() { return nullptr; };
200  // system_find: bypasses all locks, etc.
201  Items::Item* system_find( u32 serial ) const;
202 
203  u16 gump() const;
204  void get_random_location( u16* px, u16* py ) const;
205  bool is_legal_posn( const Items::Item* item, u16 x, u16 y ) const;
206  void enumerate_contents( Bscript::ObjArray* arr, int flags );
207  void extract( Contents& cnt );
208 
209  bool can_swap( const UContainer& cont ) const;
210  void swap( UContainer& cont );
211 
212  const Items::ContainerDesc& descriptor() const;
213 
214  iterator begin();
215  const_iterator begin() const;
216  iterator end();
217  const_iterator end() const;
218 
219  // Add max_items(), max_weight() and max_slots() functions to make the code more clear
220  unsigned short max_items() const;
221  unsigned short max_weight() const;
222  u8 max_slots() const;
223 
224  DYN_PROPERTY( max_items_mod, s16, Core::PROP_MAX_ITEMS_MOD, 0 );
225  DYN_PROPERTY( max_slots_mod, s8, Core::PROP_MAX_SLOTS_MOD, 0 );
226  DYN_PROPERTY( max_weight_mod, s16, Core::PROP_MAX_WEIGHT_MOD, 0 );
227 
228  bool no_drop_exception() const;
229  void no_drop_exception( bool newvalue );
230  bool default_no_drop_exception() const;
231 
232 protected:
234 
236 
237  u16 held_weight_; // in stones
238  unsigned int held_item_count_;
239 
240  Items::Item* operator[]( unsigned idx ) const;
241 
242  Items::Item* find(
243  u32 serial,
244  iterator& where_in_container ); // return the position in the array where it was found.
245 
246  // sticky places that currently need to know the internals:
247  friend class UContainerIterator;
248  // friend class Character; // uses the [] operator for quick layer access.
249  friend bool Module::send_vendorwindow_contents( Network::Client* client, UContainer* for_sale,
250  bool send_aos_tooltip ); // also []
251  friend bool send_vendorsell( Network::Client* client, Mobile::NPC* merchant, UContainer* sellfrom,
252  bool send_aos_tooltip );
253  friend Items::Item* Items::Item::create( const Items::ItemDesc& itemdesc, u32 serial );
254  friend class WornItemsContainer;
255  // friend class Item;
256 
257 protected:
258  explicit UContainer( const Items::ContainerDesc& descriptor );
259  // DAVE added this 11/17 so WornItemsContainer could pass up its class to UObject constructor
260  UContainer( u32 objtype, UOBJ_CLASS pol_class );
261  // uses Items::classname()
262  virtual void printOn( Clib::StreamWriter& sw ) const POL_OVERRIDE;
263  virtual void printSelfOn( Clib::StreamWriter& sw ) const POL_OVERRIDE;
264  void printContents( Clib::StreamWriter& sw ) const;
265 };
266 
267 inline unsigned UContainer::count() const
268 {
269  return static_cast<unsigned>( contents_.size() );
270 }
271 
272 
274 {
275  return desc;
276 }
277 
278 inline Items::Item* UContainer::operator[]( unsigned idx ) const
279 {
280  return ITEM_ELEM_PTR( contents_[idx] );
281 }
282 } // namespace Core
283 } // namespace Pol
284 #endif
Contents::const_iterator const_iterator
Definition: containr.h:115
unsigned char u8
Definition: rawtypes.h:25
std::vector< Items::Item * > Contents
Definition: containr.h:113
static Item * create(u32 objtype, u32 serial=0)
Definition: itemcr.cpp:53
const Items::ContainerDesc & desc
Definition: containr.h:235
#define POL_OVERRIDE
bool send_vendorwindow_contents(Network::Client *client, Core::UContainer *for_sale, bool send_aos_tooltip)
Definition: uomod2.cpp:173
const Items::ContainerDesc & descriptor() const
Definition: containr.h:273
virtual Mobile::Character * get_chr_owner()
Definition: containr.h:199
STL namespace.
Contents::iterator iterator
Definition: containr.h:114
#define ITEM_ELEM_PTR(elem)
Definition: containr.h:39
unsigned short u16
Definition: rawtypes.h:26
unsigned int u32
Definition: rawtypes.h:27
signed short s16
Definition: rawtypes.h:30
signed char s8
Definition: rawtypes.h:29
unsigned int held_item_count_
Definition: containr.h:238
bool send_vendorsell(Client *client, NPC *merchant, UContainer *sellfrom, UContainer *buyable, bool send_aos_tooltip)
Definition: uomod2.cpp:620
unsigned count() const
Definition: containr.h:267
#define DYN_PROPERTY(name, type, id, defaultvalue)
Definition: dynproperties.h:62
Definition: berror.cpp:12
Items::Item * operator[](unsigned idx) const
Definition: containr.h:278