ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/dclient/src/backend/communicator/inventory_manager.cpp
Revision: 1.7
Committed: Mon Oct 25 02:06:56 2010 UTC (16 years ago) by sf-pippijn
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +8 -7 lines
Log Message:
show content of open containers on the ground

File Contents

# Content
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 (auto &pair, inventories)
47 foreach (data::item const &item, pair.second)
48 if (item.flags & F_OPEN)
49 {
50 assert (container_item == &ground_item);
51 container_item = &item;
52 #if NDEBUG
53 break;
54 #endif
55 }
56 }
57
58 void
59 inventory_manager::sort ()
60 {
61 foreach (u32 tag, array (player_item.tag, container_item->tag))
62 if (tag)
63 {
64 data::inventory &inv = inventories[tag];
65 std::stable_sort (inv.begin (), inv.end (), type_name_compare);
66 }
67 }
68
69 void
70 inventory_manager::update_view () const
71 {
72 foreach (auto pair, array (std::make_pair (&player_item, inventory_view),
73 std::make_pair (container_item, container_view)))
74 {
75 if (!pair.second)
76 continue;
77 auto found = inventories.find (pair.first->tag);
78 if (found != inventories.end ())
79 pair.second->update (found->second, pair.first->singular);
80 }
81 }
82
83 void
84 inventory_manager::update ()
85 {
86 update_container ();
87 sort ();
88 update_view ();
89 }
90
91
92 data::item *
93 inventory_manager::get (u32 tag)
94 {
95 if (tag == player_item.tag)
96 return &player_item;
97 foreach (auto &pair, inventories)
98 foreach (data::item &item, pair.second)
99 if (item.tag == tag)
100 return &item;
101 return NULL;
102 }
103
104 void
105 inventory_manager::delinv (u32 tag)
106 {
107 inventories.erase (tag);
108 }
109
110 void
111 inventory_manager::delitem (std::vector<u32> const &itemtags)
112 {
113 assert (player_item.tag != 0);
114
115 foreach (auto &pair, inventories)
116 foreach (u32 tag, itemtags)
117 {
118 auto found = std::find_if (
119 pair.second.begin (),
120 pair.second.end (),
121 [tag] (data::item const &item) {
122 return item.tag == tag;
123 }
124 );
125
126 if (found != pair.second.end ())
127 pair.second.erase (found);
128 }
129
130 update ();
131 }
132
133 static data::item &
134 merge (data::inventory &inv, data::item const &item)
135 {
136 foreach (data::item &inv_item, inv)
137 if (inv_item.tag == item.tag)
138 return inv_item = item;
139 inv.push_back (item);
140 return inv.back ();
141 }
142
143 void
144 inventory_manager::item2 (u32 location, std::vector<data::item> const &items)
145 {
146 assert (player_item.tag != 0);
147
148 data::inventory &inv = inventories[location];
149 foreach (data::item const &item, items)
150 merge (inv, item);
151
152 update ();
153 }
154
155 void
156 inventory_manager::player (u32 tag, u32 weight, u32 face, std::string const &name)
157 {
158 player_item.tag = tag;
159 player_item.weight = weight;
160 player_item.face = face;
161 player_item.singular = name;
162 }
163
164 void
165 inventory_manager::upditem (u08 flags, u32 location, data::item const &item)
166 {
167 data::item *current = get (item.tag);
168
169 return_unless (current != NULL);
170
171 if (flags & UPD_LOCATION)
172 {
173 data::item save = *current;
174 delitem ({ item.tag });
175 current = &merge (inventories[location], save);
176 }
177
178 if (flags & UPD_ANIM) current->anim = item.anim;
179 if (flags & UPD_ANIMSPEED) current->animspeed = item.animspeed;
180 if (flags & UPD_FACE) current->face = item.face;
181 if (flags & UPD_FLAGS) current->flags = item.flags;
182 if (flags & UPD_NROF) current->nrof = item.nrof;
183 if (flags & UPD_WEIGHT) current->weight = item.weight;
184
185 if (flags & UPD_NAME)
186 {
187 current->singular = item.singular;
188 current->plural = item.plural;
189 }
190
191 update ();
192 }