ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/deliantra/dclient/src/frontend/client/inventory_handler.cpp
Revision: 1.2
Committed: Mon Oct 18 06:54:04 2010 UTC (16 years ago) by sf-pippijn
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +0 -0 lines
State: FILE REMOVED
Log Message:
gcc 4.5; moved some code; background colour support

File Contents

# Content
1 #include "dclient/inventory_handler.h"
2 #include "dclient/widgets.h"
3 #include "dclient/widgets/inventory.h"
4
5 #include "deliantra/data/constants.h"
6
7 #include "adt/array.h"
8
9 #include <cassert>
10
11 static bool
12 type_name_compare (data::item const &a, data::item const &b)
13 {
14 return a.type != b.type ? a.type < b.type : a.singular < b.singular;
15 }
16
17
18 template<typename T>
19 void
20 unordered_erase (std::vector<T> &vec, size_t index)
21 {
22 vec.at (index) = vec.back ();
23 vec.pop_back ();
24 }
25
26 template<typename T>
27 void
28 unordered_erase (std::vector<T> &vec, T const &element)
29 {
30 unordered_erase (vec, &element - &vec.front ());
31 }
32
33 inventory_handler::inventory_handler (interface::communicator &comm, widgets &ui)
34 : sender (comm)
35 , ui (&ui)
36 , player_item { }
37 , ground_item { "Floor" }
38 , container_item (&ground_item)
39 {
40 }
41
42
43 void
44 inventory_handler::update_container ()
45 {
46 container_item = &ground_item;
47 for (data::item const &item : inventories[player_item.tag])
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_handler::sort ()
60 {
61 for (u32 tag : array (player_item.tag, container_item->tag))
62 {
63 std::vector<data::item> &inv = inventories[tag];
64 std::stable_sort (inv.begin (), inv.end (), type_name_compare);
65 }
66 }
67
68 void
69 inventory_handler::update_view () const
70 {
71 for (auto pair : array (std::make_pair (&player_item, ui->inventory.get ()),
72 std::make_pair (container_item, ui->container.get ())))
73 {
74 auto found = inventories.find (pair.first->tag);
75 if (found != inventories.end ())
76 {
77 pair.second->update (found->second, pair.first->singular);
78 pair.second->visualize ();
79 }
80 }
81 }
82
83 void
84 inventory_handler::update ()
85 {
86 update_container ();
87 sort ();
88 update_view ();
89 }
90
91
92 data::item &
93 inventory_handler::get (u32 tag)
94 {
95 if (tag == player_item.tag)
96 return player_item;
97 for (auto &pair : inventories)
98 for (data::item &item : pair.second)
99 if (item.tag == tag)
100 return item;
101 throw "item not found";
102 }
103
104 void
105 inventory_handler::delinv (u32 tag)
106 {
107 inventories.erase (tag);
108 }
109
110 void
111 inventory_handler::delitem (std::vector<u32> const &itemtags)
112 {
113 assert (player_item.tag != 0);
114
115 for (auto &pair : inventories)
116 for (data::item &item : pair.second)
117 for (u32 tag : itemtags)
118 if (item.tag == tag)
119 {
120 assert (tag != player_item.tag);
121 unordered_erase (pair.second, item);
122 }
123
124 update ();
125 }
126
127 static data::item &
128 merge (std::vector<data::item> &inv, data::item const &item)
129 {
130 for (data::item &inv_item : inv)
131 if (inv_item.tag == item.tag)
132 return inv_item = item;
133 inv.push_back (item);
134 return inv.back ();
135 }
136
137 void
138 inventory_handler::item2 (u32 location, std::vector<data::item> const &items)
139 {
140 assert (player_item.tag != 0);
141
142 std::vector<data::item> &inv = inventories[location];
143 for (data::item const &item : items)
144 merge (inv, item);
145
146 update ();
147 }
148
149 void
150 inventory_handler::player (u32 tag, u32 weight, u32 face, std::string const &name)
151 {
152 player_item.tag = tag;
153 player_item.weight = weight;
154 player_item.face = face;
155 player_item.singular = name;
156 }
157
158 void
159 inventory_handler::upditem (u08 flags, u32 location, data::item const &item)
160 {
161 data::item *current = &get (item.tag);
162
163 if (flags & UPD_LOCATION)
164 {
165 data::item save = *current;
166 delitem ({ item.tag });
167 current = &merge (inventories[location], save);
168 }
169
170 if (flags & UPD_ANIM) current->anim = item.anim;
171 if (flags & UPD_ANIMSPEED) current->animspeed = item.animspeed;
172 if (flags & UPD_FACE) current->face = item.face;
173 if (flags & UPD_FLAGS) current->flags = item.flags;
174 if (flags & UPD_NROF) current->nrof = item.nrof;
175 if (flags & UPD_WEIGHT) current->weight = item.weight;
176
177 if (flags & UPD_NAME)
178 {
179 current->singular = item.singular;
180 current->plural = item.plural;
181 }
182
183 update ();
184 }