Pol  Revision:3cfda13
corpse.cpp
Go to the documentation of this file.
1 
10 #include "corpse.h"
11 
12 #include <stddef.h>
13 
14 #include "../../clib/cfgelem.h"
15 #include "../../clib/passert.h"
16 #include "../../clib/streamsaver.h"
17 #include "../containr.h"
18 #include "../layers.h"
19 #include "../ufunc.h"
20 #include "../uobject.h"
21 
22 
23 namespace Pol
24 {
25 namespace Core
26 {
28  : UContainer( descriptor ), corpsetype( 0 ), ownerserial( 0 )
29 {
30  movable( false );
31  layer_list_.resize( HIGHEST_LAYER + 1, EMPTY_ELEM );
32 }
33 
35 {
37 }
38 
39 void UCorpse::take_contents_to_grave( bool newvalue )
40 {
42 }
43 
44 void UCorpse::add( Item* item )
45 {
46  // When an item is added, check if it's equippable and add to the appropriate layer
47  if ( Items::valid_equip_layer( item ) && GetItemOnLayer( item->tile_layer ) == EMPTY_ELEM )
48  {
49  PutItemOnLayer( item );
50  }
51 
52  // plus the defaults from UContainer
53  base::add( item );
54 }
55 
57 {
58  Item* item = GET_ITEM_PTR( itr );
59 
60  if ( Items::valid_equip_layer( item ) )
61  {
62  Item* item_on_layer = GetItemOnLayer( item->tile_layer );
63 
64  if ( item_on_layer != EMPTY_ELEM && item_on_layer->serial == item->serial )
65  {
66  RemoveItemFromLayer( item );
67  }
68  }
69  base::remove( itr );
70 }
71 
72 
74 {
75  return corpsetype;
76 }
77 
79 {
80  bool any;
81  do
82  {
83  any = false;
84  for ( iterator itr = begin(); itr != end(); ++itr )
85  {
86  Item* item = GET_ITEM_PTR( itr );
87  if ( item->tile_layer == LAYER_HAIR || item->tile_layer == LAYER_BEARD ||
88  item->tile_layer == LAYER_FACE || item->movable() == false )
89  {
90  Core::destroy_item( item );
91  any = true;
92  break; // our iterators are now useless, so start over
93  }
94  }
95  } while ( any );
96 
97  if ( !take_contents_to_grave() )
98  base::spill_contents( multi );
99 }
100 
102 {
104  item ) ); // Calling code must make sure that item->tile_layer is valid!
105 
106  item->set_dirty();
107  set_dirty();
108  item->layer = item->tile_layer;
109  layer_list_[item->tile_layer] = Contents::value_type( item );
110  add_bulk( item );
111 }
112 
114 {
116  item ) ); // Calling code must make sure that item->tile_layer is valid!
117 
118  item->set_dirty();
119  set_dirty();
121  item->layer = 0;
122 }
123 
125 {
126  base::printProperties( sw );
127  sw() << "\tCorpseType\t" << corpsetype << pf_endl;
128  sw() << "\tOwnerSerial\t" << ownerserial << pf_endl;
129  sw() << "\tTakeContentsToGrave\t" << take_contents_to_grave() << pf_endl;
130 }
131 
133 {
134  // corpses can be the same color as mobiles
135  u16 savecolor = elem.remove_ushort( "COLOR", 0 );
136 
137  base::readProperties( elem );
138 
139  color = savecolor;
140 
141  elem.remove_prop( "CorpseType", &corpsetype );
142  elem.remove_prop( "OwnerSerial", &ownerserial );
143  take_contents_to_grave( elem.remove_bool( "TakeContentsToGrave", false ) );
144  movable( false );
145 }
146 
148 {
149  size_t size = base::estimatedSize() + sizeof( u16 ) /*corpsetype*/
150  + sizeof( u32 ) /*ownerserial*/
151  // no estimateSize here element is in objhash
152  + 3 * sizeof( Items::Item** ) + layer_list_.capacity() * sizeof( Items::Item* );
153  return size;
154 }
155 
157 {
158  // If we are a corpse and the item has a valid_equip_layer, we equipped it and need to send an
159  // update
160  if ( Items::valid_equip_layer( new_item ) )
161  {
162  UCorpse* corpse = static_cast<UCorpse*>( this );
163  Item* item_on_layer = corpse->GetItemOnLayer( new_item->tile_layer );
164  if ( item_on_layer != NULL && item_on_layer->serial == new_item->serial )
165  {
166  send_corpse_equip_inrange( corpse );
167  }
168  }
169 
170  base::on_insert_add_item( mob, move, new_item );
171 }
172 }
173 }
virtual void on_insert_add_item(Mobile::Character *mob, MoveType move, Items::Item *new_item)
Definition: containr.cpp:808
virtual void spill_contents(Multi::UMulti *supporting_multi) POL_OVERRIDE
Definition: corpse.cpp:78
virtual u16 get_senditem_amount() const POL_OVERRIDE
Definition: corpse.cpp:73
virtual void on_insert_add_item(Mobile::Character *mob, MoveType move, Items::Item *new_item) POL_OVERRIDE
Definition: corpse.cpp:156
virtual void printProperties(Clib::StreamWriter &sw) const POL_OVERRIDE
Definition: containr.cpp:842
void PutItemOnLayer(Items::Item *item)
Definition: corpse.cpp:101
virtual void add_bulk(int item_count_delta, int weight_delta)
Definition: containr.cpp:214
Items::Item * GetItemOnLayer(unsigned idx) const
Definition: corpse.h:80
virtual void readProperties(Clib::ConfigElem &elem) POL_OVERRIDE
Definition: corpse.cpp:132
virtual Items::Item * remove(u32 serial, UContainer **found_in=NULL)
Definition: containr.cpp:485
virtual void printProperties(Clib::StreamWriter &sw) const POL_OVERRIDE
Definition: corpse.cpp:124
bool get(ENUM flag) const
Definition: uobject.h:108
Contents::iterator iterator
Definition: containr.h:114
virtual void readProperties(Clib::ConfigElem &elem) POL_OVERRIDE
Definition: containr.cpp:855
Contents layer_list_
Definition: corpse.h:77
virtual void add(Items::Item *item)
Definition: containr.cpp:193
virtual size_t estimatedSize() const POL_OVERRIDE
Definition: corpse.cpp:147
void set_dirty()
Definition: uobject.h:291
bool valid_equip_layer(int layer)
Definition: item.h:383
unsigned short u16
Definition: rawtypes.h:26
unsigned int u32
Definition: rawtypes.h:27
void destroy_item(Item *item)
Definition: ufunc.cpp:1538
AttributeFlags< OBJ_FLAGS > flags_
Definition: uobject.h:274
UCorpse(const Items::ContainerDesc &desc)
Definition: corpse.cpp:27
#define passert(exp)
Definition: passert.h:62
void send_corpse_equip_inrange(const Item *item)
Definition: ufunc.cpp:667
#define GET_ITEM_PTR(itr)
Definition: containr.h:38
virtual void spill_contents(Multi::UMulti *supporting_multi) POL_OVERRIDE
Definition: containr.cpp:713
bool movable() const
Definition: item.h:300
bool remove_prop(const char *propname, std::string *value)
Definition: cfgfile.cpp:141
void RemoveItemFromLayer(Items::Item *item)
Definition: corpse.cpp:113
unsigned short remove_ushort(const char *propname)
Definition: cfgfile.cpp:331
void change(ENUM flag, bool value)
Definition: uobject.h:115
virtual size_t estimatedSize() const POL_OVERRIDE
Definition: containr.cpp:66
#define EMPTY_ELEM
Definition: containr.h:40
#define pf_endl
Definition: proplist.cpp:25
Definition: berror.cpp:12
bool remove_bool(const char *propname)
Definition: cfgfile.cpp:439
virtual void add(Item *item) POL_OVERRIDE
Definition: corpse.cpp:44
bool take_contents_to_grave() const
Definition: corpse.cpp:34
virtual void remove(iterator itr) POL_OVERRIDE
Definition: corpse.cpp:56