forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contents_change_handler.cpp
49 lines (43 loc) · 1.39 KB
/
contents_change_handler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "character.h"
#include "contents_change_handler.h"
void contents_change_handler::add_unsealed( const item_location &loc )
{
if( std::find( unsealed.begin(), unsealed.end(), loc ) == unsealed.end() ) {
unsealed.emplace_back( loc );
}
}
void contents_change_handler::unseal_pocket_containing( const item_location &loc )
{
if( loc.has_parent() ) {
item_location parent = loc.parent_item();
item_pocket *const pocket = parent->contained_where( *loc );
if( pocket ) {
// on_contents_changed restacks the pocket and should be called later
// in Character::handle_contents_changed
pocket->unseal();
} else {
debugmsg( "parent container does not contain item" );
}
parent.on_contents_changed();
add_unsealed( parent );
}
}
void contents_change_handler::handle_by( Character &guy )
{
// some containers could have been destroyed by e.g. monster attack
auto it = std::remove_if( unsealed.begin(), unsealed.end(),
[]( const item_location & loc ) -> bool {
return !loc;
} );
unsealed.erase( it, unsealed.end() );
guy.handle_contents_changed( unsealed );
unsealed.clear();
}
void contents_change_handler::serialize( JsonOut &jsout ) const
{
jsout.write( unsealed );
}
void contents_change_handler::deserialize( JsonIn &jsin )
{
jsin.read( unsealed );
}