ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/dclient/src/backend/communicator/inventory_manager.cpp
Revision: 1.3
Committed: Tue Oct 19 09:45:07 2010 UTC (16 years ago) by sf-pippijn
Branch: MAIN
Changes since 1.2: +0 -1 lines
Log Message:
- use wchar_t for single characters (glyphs and line drawing)
- merge ncurses and ndk wrappers
- simplify colour caching

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