3899c9394186cdf3e5cac86ed81d115c64618c88
[oweals/minetest.git] / src / inventory.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 /*
21 (c) 2010 Perttu Ahola <celeron55@gmail.com>
22 */
23
24 #include "inventory.h"
25 #include "serialization.h"
26 #include "utility.h"
27 #include "debug.h"
28 #include <sstream>
29 #include "main.h"
30
31 /*
32         InventoryItem
33 */
34
35 InventoryItem::InventoryItem()
36 {
37 }
38
39 InventoryItem::~InventoryItem()
40 {
41 }
42
43 InventoryItem* InventoryItem::deSerialize(std::istream &is)
44 {
45         DSTACK(__FUNCTION_NAME);
46
47         //is.imbue(std::locale("C"));
48         // Read name
49         std::string name;
50         std::getline(is, name, ' ');
51         
52         if(name == "MaterialItem")
53         {
54                 // u16 reads directly as a number (u8 doesn't)
55                 u16 material;
56                 is>>material;
57                 u16 count;
58                 is>>count;
59                 if(material > 255)
60                         throw SerializationError("Too large material number");
61                 return new MaterialItem(material, count);
62         }
63         else if(name == "MBOItem")
64         {
65                 std::string inventorystring;
66                 std::getline(is, inventorystring, '|');
67                 return new MapBlockObjectItem(inventorystring);
68         }
69         else
70         {
71                 dstream<<"Unknown InventoryItem name=\""<<name<<"\""<<std::endl;
72                 throw SerializationError("Unknown InventoryItem name");
73         }
74 }
75
76 /*
77         MapBlockObjectItem
78 */
79 #ifndef SERVER
80 video::ITexture * MapBlockObjectItem::getImage()
81 {
82         if(m_inventorystring.substr(0,3) == "Rat")
83                 //return g_device->getVideoDriver()->getTexture("../data/rat.png");
84                 return g_irrlicht->getTexture("../data/rat.png");
85         
86         if(m_inventorystring.substr(0,4) == "Sign")
87                 //return g_device->getVideoDriver()->getTexture("../data/sign.png");
88                 return g_irrlicht->getTexture("../data/sign.png");
89
90         return NULL;
91 }
92 #endif
93 std::string MapBlockObjectItem::getText()
94 {
95         if(m_inventorystring.substr(0,3) == "Rat")
96                 return "";
97         
98         if(m_inventorystring.substr(0,4) == "Sign")
99                 return "";
100
101         return "obj";
102 }
103
104 MapBlockObject * MapBlockObjectItem::createObject
105                 (v3f pos, f32 player_yaw, f32 player_pitch)
106 {
107         std::istringstream is(m_inventorystring);
108         std::string name;
109         std::getline(is, name, ' ');
110         
111         if(name == "None")
112         {
113                 return NULL;
114         }
115         else if(name == "Sign")
116         {
117                 std::string text;
118                 std::getline(is, text, '|');
119                 SignObject *obj = new SignObject(NULL, -1, pos);
120                 obj->setText(text);
121                 obj->setYaw(-player_yaw);
122                 return obj;
123         }
124         else if(name == "Rat")
125         {
126                 RatObject *obj = new RatObject(NULL, -1, pos);
127                 return obj;
128         }
129         else
130         {
131                 return NULL;
132         }
133 }
134
135 /*
136         Inventory
137 */
138
139 Inventory::Inventory(u32 size)
140 {
141         m_size = size;
142         clearItems();
143 }
144
145 Inventory::~Inventory()
146 {
147         for(u32 i=0; i<m_items.size(); i++)
148         {
149                 delete m_items[i];
150         }
151 }
152
153 void Inventory::clearItems()
154 {
155         m_items.clear();
156         for(u32 i=0; i<m_size; i++)
157         {
158                 m_items.push_back(NULL);
159         }
160 }
161
162 void Inventory::serialize(std::ostream &os)
163 {
164         //os.imbue(std::locale("C"));
165         
166         for(u32 i=0; i<m_items.size(); i++)
167         {
168                 InventoryItem *item = m_items[i];
169                 if(item != NULL)
170                 {
171                         os<<"Item ";
172                         item->serialize(os);
173                 }
174                 else
175                 {
176                         os<<"Empty";
177                 }
178                 os<<"\n";
179         }
180
181         os<<"end\n";
182 }
183
184 void Inventory::deSerialize(std::istream &is)
185 {
186         //is.imbue(std::locale("C"));
187
188         clearItems();
189         u32 item_i = 0;
190
191         for(;;)
192         {
193                 std::string line;
194                 std::getline(is, line, '\n');
195
196                 std::istringstream iss(line);
197                 //iss.imbue(std::locale("C"));
198
199                 std::string name;
200                 std::getline(iss, name, ' ');
201
202                 if(name == "end")
203                 {
204                         break;
205                 }
206                 else if(name == "Item")
207                 {
208                         if(item_i > getSize() - 1)
209                                 throw SerializationError("too many items");
210                         InventoryItem *item = InventoryItem::deSerialize(iss);
211                         m_items[item_i++] = item;
212                 }
213                 else if(name == "Empty")
214                 {
215                         if(item_i > getSize() - 1)
216                                 throw SerializationError("too many items");
217                         m_items[item_i++] = NULL;
218                 }
219                 else
220                 {
221                         throw SerializationError("Unknown inventory identifier");
222                 }
223         }
224 }
225
226 Inventory & Inventory::operator = (Inventory &other)
227 {
228         m_size = other.m_size;
229         clearItems();
230         for(u32 i=0; i<other.m_items.size(); i++)
231         {
232                 InventoryItem *item = other.m_items[i];
233                 if(item != NULL)
234                 {
235                         m_items[i] = item->clone();
236                 }
237         }
238
239         return *this;
240 }
241
242 u32 Inventory::getSize()
243 {
244         return m_items.size();
245 }
246
247 u32 Inventory::getUsedSlots()
248 {
249         u32 num = 0;
250         for(u32 i=0; i<m_items.size(); i++)
251         {
252                 InventoryItem *item = m_items[i];
253                 if(item != NULL)
254                         num++;
255         }
256         return num;
257 }
258
259 InventoryItem * Inventory::getItem(u32 i)
260 {
261         if(i > m_items.size() - 1)
262                 return NULL;
263         return m_items[i];
264 }
265
266 InventoryItem * Inventory::changeItem(u32 i, InventoryItem *newitem)
267 {
268         assert(i < m_items.size());
269
270         InventoryItem *olditem = m_items[i];
271         m_items[i] = newitem;
272         return olditem;
273 }
274
275 void Inventory::deleteItem(u32 i)
276 {
277         assert(i < m_items.size());
278         InventoryItem *item = changeItem(i, NULL);
279         if(item)
280                 delete item;
281 }
282
283 bool Inventory::addItem(InventoryItem *newitem)
284 {
285         // If it is a MaterialItem, try to find an already existing one
286         // and just increment the counter
287         if(std::string("MaterialItem") == newitem->getName())
288         {
289                 u8 material = ((MaterialItem*)newitem)->getMaterial();
290                 u8 count = ((MaterialItem*)newitem)->getCount();
291                 for(u32 i=0; i<m_items.size(); i++)
292                 {
293                         InventoryItem *item2 = m_items[i];
294                         if(item2 == NULL)
295                                 continue;
296                         if(std::string("MaterialItem") != item2->getName())
297                                 continue;
298                         // Found one. Check if it is of the right material and has
299                         // free space
300                         MaterialItem *mitem2 = (MaterialItem*)item2;
301                         if(mitem2->getMaterial() != material)
302                                 continue;
303                         //TODO: Add all that can be added and add remaining part
304                         // to another place
305                         if(mitem2->freeSpace() < count)
306                                 continue;
307                         // Add to the counter
308                         mitem2->add(count);
309                         // Dump the parameter
310                         delete newitem;
311                         return true;
312                 }
313         }
314         // Else find an empty position
315         for(u32 i=0; i<m_items.size(); i++)
316         {
317                 InventoryItem *item = m_items[i];
318                 if(item != NULL)
319                         continue;
320                 m_items[i] = newitem;
321                 return true;
322         }
323         // Failed
324         return false;
325 }
326
327 void Inventory::print(std::ostream &o)
328 {
329         o<<"Player inventory:"<<std::endl;
330         for(u32 i=0; i<m_items.size(); i++)
331         {
332                 InventoryItem *item = m_items[i];
333                 if(item != NULL)
334                 {
335                         o<<i<<": ";
336                         item->serialize(o);
337                         o<<"\n";
338                 }
339         }
340 }
341         
342 //END