just savin'
[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 InventoryList::InventoryList(std::string name, u32 size)
140 {
141         m_name = name;
142         m_size = size;
143         clearItems();
144 }
145
146 InventoryList::~InventoryList()
147 {
148         for(u32 i=0; i<m_items.size(); i++)
149         {
150                 if(m_items[i])
151                         delete m_items[i];
152         }
153 }
154
155 void InventoryList::clearItems()
156 {
157         for(u32 i=0; i<m_items.size(); i++)
158         {
159                 if(m_items[i])
160                         delete m_items[i];
161         }
162
163         m_items.clear();
164
165         for(u32 i=0; i<m_size; i++)
166         {
167                 m_items.push_back(NULL);
168         }
169 }
170
171 void InventoryList::serialize(std::ostream &os)
172 {
173         //os.imbue(std::locale("C"));
174         
175         for(u32 i=0; i<m_items.size(); i++)
176         {
177                 InventoryItem *item = m_items[i];
178                 if(item != NULL)
179                 {
180                         os<<"Item ";
181                         item->serialize(os);
182                 }
183                 else
184                 {
185                         os<<"Empty";
186                 }
187                 os<<"\n";
188         }
189
190         os<<"end\n";
191 }
192
193 void InventoryList::deSerialize(std::istream &is)
194 {
195         //is.imbue(std::locale("C"));
196
197         clearItems();
198         u32 item_i = 0;
199
200         for(;;)
201         {
202                 std::string line;
203                 std::getline(is, line, '\n');
204
205                 std::istringstream iss(line);
206                 //iss.imbue(std::locale("C"));
207
208                 std::string name;
209                 std::getline(iss, name, ' ');
210
211                 if(name == "end")
212                 {
213                         break;
214                 }
215                 else if(name == "Item")
216                 {
217                         if(item_i > getSize() - 1)
218                                 throw SerializationError("too many items");
219                         InventoryItem *item = InventoryItem::deSerialize(iss);
220                         m_items[item_i++] = item;
221                 }
222                 else if(name == "Empty")
223                 {
224                         if(item_i > getSize() - 1)
225                                 throw SerializationError("too many items");
226                         m_items[item_i++] = NULL;
227                 }
228                 else
229                 {
230                         throw SerializationError("Unknown inventory identifier");
231                 }
232         }
233 }
234
235 InventoryList::InventoryList(const InventoryList &other)
236 {
237         /*
238                 Do this so that the items get cloned. Otherwise the pointers
239                 in the array will just get copied.
240         */
241         *this = other;
242 }
243
244 InventoryList & InventoryList::operator = (const InventoryList &other)
245 {
246         m_name = other.m_name;
247         m_size = other.m_size;
248         clearItems();
249         for(u32 i=0; i<other.m_items.size(); i++)
250         {
251                 InventoryItem *item = other.m_items[i];
252                 if(item != NULL)
253                 {
254                         m_items[i] = item->clone();
255                 }
256         }
257
258         return *this;
259 }
260
261 std::string InventoryList::getName()
262 {
263         return m_name;
264 }
265
266 u32 InventoryList::getSize()
267 {
268         return m_items.size();
269 }
270
271 u32 InventoryList::getUsedSlots()
272 {
273         u32 num = 0;
274         for(u32 i=0; i<m_items.size(); i++)
275         {
276                 InventoryItem *item = m_items[i];
277                 if(item != NULL)
278                         num++;
279         }
280         return num;
281 }
282
283 InventoryItem * InventoryList::getItem(u32 i)
284 {
285         if(i > m_items.size() - 1)
286                 return NULL;
287         return m_items[i];
288 }
289
290 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
291 {
292         assert(i < m_items.size());
293
294         InventoryItem *olditem = m_items[i];
295         m_items[i] = newitem;
296         return olditem;
297 }
298
299 void InventoryList::deleteItem(u32 i)
300 {
301         assert(i < m_items.size());
302         InventoryItem *item = changeItem(i, NULL);
303         if(item)
304                 delete item;
305 }
306
307 bool InventoryList::addItem(InventoryItem *newitem)
308 {
309         // If it is a MaterialItem, try to find an already existing one
310         // and just increment the counter
311         if(std::string("MaterialItem") == newitem->getName())
312         {
313                 u8 material = ((MaterialItem*)newitem)->getMaterial();
314                 u8 count = ((MaterialItem*)newitem)->getCount();
315                 for(u32 i=0; i<m_items.size(); i++)
316                 {
317                         InventoryItem *item2 = m_items[i];
318                         if(item2 == NULL)
319                                 continue;
320                         if(std::string("MaterialItem") != item2->getName())
321                                 continue;
322                         // Found one. Check if it is of the right material and has
323                         // free space
324                         MaterialItem *mitem2 = (MaterialItem*)item2;
325                         if(mitem2->getMaterial() != material)
326                                 continue;
327                         //TODO: Add all that can be added and add remaining part
328                         // to another place
329                         if(mitem2->freeSpace() < count)
330                                 continue;
331                         // Add to the counter
332                         mitem2->add(count);
333                         // Dump the parameter
334                         delete newitem;
335                         return true;
336                 }
337         }
338         // Else find an empty position
339         for(u32 i=0; i<m_items.size(); i++)
340         {
341                 InventoryItem *item = m_items[i];
342                 if(item != NULL)
343                         continue;
344                 m_items[i] = newitem;
345                 return true;
346         }
347         // Failed
348         return false;
349 }
350
351 void InventoryList::print(std::ostream &o)
352 {
353         o<<"InventoryList:"<<std::endl;
354         for(u32 i=0; i<m_items.size(); i++)
355         {
356                 InventoryItem *item = m_items[i];
357                 if(item != NULL)
358                 {
359                         o<<i<<": ";
360                         item->serialize(o);
361                         o<<"\n";
362                 }
363         }
364 }
365
366 /*
367         Inventory
368 */
369
370 Inventory::~Inventory()
371 {
372         clear();
373 }
374
375 void Inventory::clear()
376 {
377         for(u32 i=0; i<m_lists.size(); i++)
378         {
379                 delete m_lists[i];
380         }
381         m_lists.clear();
382 }
383
384 Inventory::Inventory()
385 {
386 }
387
388 Inventory::Inventory(const Inventory &other)
389 {
390         *this = other;
391 }
392
393 Inventory & Inventory::operator = (const Inventory &other)
394 {
395         clear();
396         for(u32 i=0; i<other.m_lists.size(); i++)
397         {
398                 m_lists.push_back(new InventoryList(*other.m_lists[i]));
399         }
400         return *this;
401 }
402
403 void Inventory::serialize(std::ostream &os)
404 {
405         for(u32 i=0; i<m_lists.size(); i++)
406         {
407                 InventoryList *list = m_lists[i];
408                 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
409                 list->serialize(os);
410         }
411
412         os<<"end\n";
413 }
414
415 void Inventory::deSerialize(std::istream &is)
416 {
417         clear();
418
419         for(;;)
420         {
421                 std::string line;
422                 std::getline(is, line, '\n');
423
424                 std::istringstream iss(line);
425
426                 std::string name;
427                 std::getline(iss, name, ' ');
428
429                 if(name == "end")
430                 {
431                         break;
432                 }
433                 else if(name == "List")
434                 {
435                         std::string listname;
436                         u32 listsize;
437
438                         std::getline(iss, listname, ' ');
439                         iss>>listsize;
440
441                         InventoryList *list = new InventoryList(listname, listsize);
442                         list->deSerialize(is);
443
444                         m_lists.push_back(list);
445                 }
446                 else
447                 {
448                         throw SerializationError("Unknown inventory identifier");
449                 }
450         }
451 }
452
453 InventoryList * Inventory::addList(const std::string &name, u32 size)
454 {
455         s32 i = getListIndex(name);
456         if(i != -1)
457         {
458                 if(m_lists[i]->getSize() != size)
459                 {
460                         delete m_lists[i];
461                         m_lists[i] = new InventoryList(name, size);
462                 }
463                 return m_lists[i];
464         }
465         else
466         {
467                 m_lists.push_back(new InventoryList(name, size));
468                 return m_lists.getLast();
469         }
470 }
471
472 InventoryList * Inventory::getList(const std::string &name)
473 {
474         s32 i = getListIndex(name);
475         if(i == -1)
476                 return NULL;
477         return m_lists[i];
478 }
479
480 s32 Inventory::getListIndex(const std::string &name)
481 {
482         for(u32 i=0; i<m_lists.size(); i++)
483         {
484                 if(m_lists[i]->getName() == name)
485                         return i;
486         }
487         return -1;
488 }
489
490         
491 //END