ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/dclient/src/backend/communicator/inventory_manager.cpp
Revision: 1.6
Committed: Mon Oct 25 01:18:05 2010 UTC (16 years ago) by sf-pippijn
Branch: MAIN
Changes since 1.5: +2 -0 lines
Log Message:
do not crash if inventory manager has no views attached

File Contents

# User Rev Content
1 sf-pippijn 1.1 #include "deliantra/inventory_manager.h"
2     #include "deliantra/data/constants.h"
3    
4     #include "adt/array.h"
5    
6     #include <algorithm>
7     #include <cassert>
8    
9     static bool
10     type_name_compare (data::item const &a, data::item const &b)
11     {
12     return a.type != b.type ? a.type < b.type : a.singular < b.singular;
13     }
14    
15    
16     inventory_manager::inventory_manager ()
17     : player_item { }
18     , ground_item { "Floor" }
19     , container_item (&ground_item)
20     , inventory_view (nullptr)
21     , container_view (nullptr)
22     {
23     }
24    
25     inventory_manager::~inventory_manager ()
26     {
27     }
28    
29     void
30     inventory_manager::set_inventory_view (interface::inventory_handler &view)
31     {
32     inventory_view = &view;
33     }
34    
35     void
36     inventory_manager::set_container_view (interface::inventory_handler &view)
37     {
38     container_view = &view;
39     }
40    
41    
42     void
43     inventory_manager::update_container ()
44     {
45     container_item = &ground_item;
46     foreach (data::item const &item, inventories[player_item.tag])
47     if (item.flags & F_OPEN)
48     {
49     assert (container_item == &ground_item);
50     container_item = &item;
51     #if NDEBUG
52     break;
53     #endif
54     }
55     }
56    
57     void
58     inventory_manager::sort ()
59     {
60     foreach (u32 tag, array (player_item.tag, container_item->tag))
61 sf-pippijn 1.4 if (tag)
62     {
63     data::inventory &inv = inventories[tag];
64     std::stable_sort (inv.begin (), inv.end (), type_name_compare);
65     }
66 sf-pippijn 1.1 }
67    
68     void
69     inventory_manager::update_view () const
70     {
71     foreach (auto pair, array (std::make_pair (&player_item, inventory_view),
72     std::make_pair (container_item, container_view)))
73     {
74 sf-pippijn 1.6 if (!pair.second)
75     continue;
76 sf-pippijn 1.1 auto found = inventories.find (pair.first->tag);
77     if (found != inventories.end ())
78     pair.second->update (found->second, pair.first->singular);
79     }
80     }
81    
82     void
83     inventory_manager::update ()
84     {
85     update_container ();
86     sort ();
87     update_view ();
88     }
89    
90    
91 sf-pippijn 1.5 data::item *
92 sf-pippijn 1.1 inventory_manager::get (u32 tag)
93     {
94     if (tag == player_item.tag)
95 sf-pippijn 1.5 return &player_item;
96 sf-pippijn 1.1 foreach (auto &pair, inventories)
97     foreach (data::item &item, pair.second)
98     if (item.tag == tag)
99 sf-pippijn 1.5 return &item;
100     return NULL;
101 sf-pippijn 1.1 }
102    
103     void
104     inventory_manager::delinv (u32 tag)
105     {
106     inventories.erase (tag);
107     }
108    
109     void
110     inventory_manager::delitem (std::vector<u32> const &itemtags)
111     {
112     assert (player_item.tag != 0);
113    
114     foreach (auto &pair, inventories)
115 sf-pippijn 1.2 foreach (u32 tag, itemtags)
116     {
117     auto found = std::find_if (
118     pair.second.begin (),
119     pair.second.end (),
120     [tag] (data::item const &item) {
121     return item.tag == tag;
122 sf-pippijn 1.1 }
123 sf-pippijn 1.2 );
124    
125     if (found != pair.second.end ())
126     pair.second.erase (found);
127     }
128 sf-pippijn 1.1
129     update ();
130     }
131    
132     static data::item &
133     merge (data::inventory &inv, data::item const &item)
134     {
135     foreach (data::item &inv_item, inv)
136     if (inv_item.tag == item.tag)
137     return inv_item = item;
138     inv.push_back (item);
139     return inv.back ();
140     }
141    
142     void
143     inventory_manager::item2 (u32 location, std::vector<data::item> const &items)
144     {
145     assert (player_item.tag != 0);
146    
147     data::inventory &inv = inventories[location];
148     foreach (data::item const &item, items)
149     merge (inv, item);
150    
151     update ();
152     }
153    
154     void
155     inventory_manager::player (u32 tag, u32 weight, u32 face, std::string const &name)
156     {
157     player_item.tag = tag;
158     player_item.weight = weight;
159     player_item.face = face;
160     player_item.singular = name;
161     }
162    
163     void
164     inventory_manager::upditem (u08 flags, u32 location, data::item const &item)
165     {
166 sf-pippijn 1.5 data::item *current = get (item.tag);
167    
168     return_unless (current != NULL);
169 sf-pippijn 1.1
170     if (flags & UPD_LOCATION)
171     {
172     data::item save = *current;
173     delitem ({ item.tag });
174     current = &merge (inventories[location], save);
175     }
176    
177     if (flags & UPD_ANIM) current->anim = item.anim;
178     if (flags & UPD_ANIMSPEED) current->animspeed = item.animspeed;
179     if (flags & UPD_FACE) current->face = item.face;
180     if (flags & UPD_FLAGS) current->flags = item.flags;
181     if (flags & UPD_NROF) current->nrof = item.nrof;
182     if (flags & UPD_WEIGHT) current->weight = item.weight;
183    
184     if (flags & UPD_NAME)
185     {
186     current->singular = item.singular;
187     current->plural = item.plural;
188     }
189    
190     update ();
191     }