Pol  Revision:cb584c9
wornitems.cpp
Go to the documentation of this file.
1 
2 #include "wornitems.h"
3 
4 #include "../../bscript/bobject.h"
5 #include "../../clib/passert.h"
6 #include "../containr.h"
7 #include "../extobj.h"
8 #include "../globals/settings.h"
9 #include "../item/item.h"
10 #include "../item/itemdesc.h"
11 #include "../layers.h"
12 #include "../objtype.h"
13 #include "charactr.h"
14 
15 
16 namespace Pol
17 {
18 namespace Core
19 {
21  : UContainer( Items::find_container_desc( settingsManager.extobj.wornitems_container ) ),
22  chr_owner( nullptr )
23 {
24  contents_.resize( HIGHEST_LAYER + 1, EMPTY_ELEM );
25 }
26 
28 {
29  return sizeof( Mobile::Character* ) /*chr_owner*/ + base::estimatedSize();
30 }
31 
32 
33 void WornItemsContainer::for_each_item( void ( *f )( Items::Item* item, void* a ), void* arg )
34 {
35  for ( auto& item : contents_ )
36  {
37  if ( item != nullptr )
38  {
39  if ( item->isa( UOBJ_CLASS::CLASS_CONTAINER ) )
40  {
41  UContainer* cont = static_cast<UContainer*>( item );
42  cont->for_each_item( f, arg );
43  }
44  ( *f )( item, arg );
45  }
46  }
47 }
48 
50 {
52  item ) ); // Calling code must make sure that item->tile_layer is valid!
53 
54  item->set_dirty();
55  item->container = this;
56  item->realm = realm;
57  item->layer = item->tile_layer;
58  contents_[item->tile_layer] = Contents::value_type( item );
59  add_bulk( item );
60 }
61 
63 {
65  item ) ); // Calling code must make sure that item->tile_layer is valid!
66 
67  item->set_dirty();
68  item->container = nullptr;
70  // 12-17-2008 MuadDib added to clear item.layer properties.
71  item->layer = 0;
72  remove_bulk( item );
73 }
74 
76 {
77  if ( !saveonexit() )
78  {
79  return;
80  }
81  for ( unsigned clayer = 0; clayer < contents_.size(); ++clayer )
82  {
83  const Items::Item* item = contents_[clayer];
84  if ( item )
85  {
86  if ( !item->itemdesc().save_on_exit || !item->saveonexit() )
87  continue;
88 
89  if ( ( clayer == LAYER_HAIR ) || ( clayer == LAYER_BEARD ) || ( clayer == LAYER_FACE ) ||
90  ( clayer == LAYER_ROBE_DRESS && item->objtype_ == UOBJ_DEATH_SHROUD ) )
91  {
92  sw_pc << *item;
93  item->clear_dirty();
94  }
95  else if ( clayer == LAYER_BACKPACK )
96  {
97  // write the backpack to the PC file,
98  // and the backpack contents to the PCEQUIP file
99  const UContainer* cont = static_cast<const UContainer*>( item );
100  cont->printSelfOn( sw_pc );
101  cont->clear_dirty();
102  cont->printContents( sw_equip );
103  }
104  else
105  {
106  sw_equip << *item;
107  item->clear_dirty();
108  }
109  }
110  }
111 }
112 
114 {
115  passert_always( chr_owner != nullptr );
116  return chr_owner->make_offline_ref();
117 }
118 
120 {
121  return chr_owner;
122 }
123 
125 {
126  return chr_owner;
127 }
128 
130 {
131  return chr_owner;
132 }
133 
135 {
136  return chr_owner;
137 }
138 }
139 }
#define UOBJ_DEATH_SHROUD
Definition: objtype.h:219
virtual Bscript::BObjectImp * make_ref() POL_OVERRIDE
Definition: wornitems.cpp:113
virtual void add_bulk(int item_count_delta, int weight_delta)
Definition: containr.cpp:215
virtual void for_each_item(void(*f)(Items::Item *item, void *a), void *arg) POL_OVERRIDE
Definition: wornitems.cpp:33
void PutItemOnLayer(Item *item)
Definition: wornitems.cpp:49
void RemoveItemFromLayer(Item *item)
Definition: wornitems.cpp:62
virtual size_t estimatedSize() const POL_OVERRIDE
Definition: wornitems.cpp:27
void remove_bulk(const Items::Item *item)
Definition: containr.cpp:210
void set_dirty()
Definition: uobject.h:291
bool valid_equip_layer(int layer)
Definition: item.h:383
Mobile::Character * chr_owner
Definition: wornitems.h:48
#define passert(exp)
Definition: passert.h:62
virtual UObject * owner() POL_OVERRIDE
Definition: wornitems.cpp:119
Core::UContainer * container
Definition: item.h:256
const u32 objtype_
Definition: uobject.h:249
SettingsManager settingsManager
Definition: settings.cpp:14
virtual void printSelfOn(Clib::StreamWriter &sw) const POL_OVERRIDE
Definition: containr.cpp:105
bool saveonexit() const
Definition: uobject.cpp:385
virtual UObject * self_as_owner() POL_OVERRIDE
Definition: wornitems.cpp:129
Realms::Realm * realm
Definition: baseobject.h:56
void clear_dirty() const
Definition: uobject.cpp:146
void print(Clib::StreamWriter &sw_pc, Clib::StreamWriter &sw_equip) const
Definition: wornitems.cpp:75
bool isa(UOBJ_CLASS uobj_class) const
Definition: baseobject.h:99
const ItemDesc & itemdesc() const
Definition: item.cpp:127
virtual size_t estimatedSize() const POL_OVERRIDE
Definition: containr.cpp:67
const ContainerDesc & find_container_desc(u32 objtype)
Definition: itemdesc.cpp:942
#define passert_always(exp)
Definition: passert.h:80
#define EMPTY_ELEM
Definition: containr.h:40
Definition: berror.cpp:12
Bscript::BObjectImp * make_offline_ref()
Definition: uoscrobj.cpp:1654
virtual void for_each_item(void(*f)(Item *item, void *a), void *arg)
Definition: containr.cpp:659
void printContents(Clib::StreamWriter &sw) const
Definition: containr.cpp:110