Merge remote branch 'origin/master'
[oweals/minetest.git] / src / inventory.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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 #include "inventory.h"
21 #include "serialization.h"
22 #include "debug.h"
23 #include <sstream>
24 #include "log.h"
25 #include "itemdef.h"
26 #include "strfnd.h"
27 #include "content_mapnode.h" // For loading legacy MaterialItems
28 #include "nameidmapping.h" // For loading legacy MaterialItems
29 #include "util/serialize.h"
30 #include "util/string.h"
31
32 /*
33         ItemStack
34 */
35
36 static content_t content_translate_from_19_to_internal(content_t c_from)
37 {
38         for(u32 i=0; i<sizeof(trans_table_19)/sizeof(trans_table_19[0]); i++)
39         {
40                 if(trans_table_19[i][1] == c_from)
41                 {
42                         return trans_table_19[i][0];
43                 }
44         }
45         return c_from;
46 }
47
48 // If the string contains spaces, quotes or control characters, encodes as JSON.
49 // Else returns the string unmodified.
50 static std::string serializeJsonStringIfNeeded(const std::string &s)
51 {
52         for(size_t i = 0; i < s.size(); ++i)
53         {
54                 if(s[i] <= 0x1f || s[i] >= 0x7f || s[i] == ' ' || s[i] == '\"')
55                         return serializeJsonString(s);
56         }
57         return s;
58 }
59
60 // Parses a string serialized by serializeJsonStringIfNeeded.
61 static std::string deSerializeJsonStringIfNeeded(std::istream &is)
62 {
63         std::ostringstream tmp_os;
64         bool expect_initial_quote = true;
65         bool is_json = false;
66         bool was_backslash = false;
67         for(;;)
68         {
69                 char c = is.get();
70                 if(is.eof())
71                         break;
72                 if(expect_initial_quote && c == '"')
73                 {
74                         tmp_os << c;
75                         is_json = true;
76                 }
77                 else if(is_json)
78                 {
79                         tmp_os << c;
80                         if(was_backslash)
81                                 was_backslash = false;
82                         else if(c == '\\')
83                                 was_backslash = true;
84                         else if(c == '"')
85                                 break; // Found end of string
86                 }
87                 else
88                 {
89                         if(c == ' ')
90                         {
91                                 // Found end of word
92                                 is.unget();
93                                 break;
94                         }
95                         else
96                         {
97                                 tmp_os << c;
98                         }
99                 }
100                 expect_initial_quote = false;
101         }
102         if(is_json)
103         {
104                 std::istringstream tmp_is(tmp_os.str(), std::ios::binary);
105                 return deSerializeJsonString(tmp_is);
106         }
107         else
108                 return tmp_os.str();
109 }
110
111
112 ItemStack::ItemStack(std::string name_, u16 count_,
113                 u16 wear_, std::string metadata_,
114                 IItemDefManager *itemdef)
115 {
116         name = itemdef->getAlias(name_);
117         count = count_;
118         wear = wear_;
119         metadata = metadata_;
120
121         if(name.empty() || count == 0)
122                 clear();
123         else if(itemdef->get(name).type == ITEM_TOOL)
124                 count = 1;
125 }
126
127 void ItemStack::serialize(std::ostream &os) const
128 {
129         DSTACK(__FUNCTION_NAME);
130
131         if(empty())
132                 return;
133
134         // Check how many parts of the itemstring are needed
135         int parts = 1;
136         if(count != 1)
137                 parts = 2;
138         if(wear != 0)
139                 parts = 3;
140         if(metadata != "")
141                 parts = 4;
142
143         os<<serializeJsonStringIfNeeded(name);
144         if(parts >= 2)
145                 os<<" "<<count;
146         if(parts >= 3)
147                 os<<" "<<wear;
148         if(parts >= 4)
149                 os<<" "<<serializeJsonStringIfNeeded(metadata);
150 }
151
152 void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
153 {
154         DSTACK(__FUNCTION_NAME);
155
156         clear();
157
158         // Read name
159         name = deSerializeJsonStringIfNeeded(is);
160
161         // Skip space
162         std::string tmp;
163         std::getline(is, tmp, ' ');
164         if(!tmp.empty())
165                 throw SerializationError("Unexpected text after item name");
166         
167         if(name == "MaterialItem")
168         {
169                 // Obsoleted on 2011-07-30
170
171                 u16 material;
172                 is>>material;
173                 u16 materialcount;
174                 is>>materialcount;
175                 // Convert old materials
176                 if(material <= 0xff)
177                         material = content_translate_from_19_to_internal(material);
178                 if(material > MAX_CONTENT)
179                         throw SerializationError("Too large material number");
180                 // Convert old id to name
181                 NameIdMapping legacy_nimap;
182                 content_mapnode_get_name_id_mapping(&legacy_nimap);
183                 legacy_nimap.getName(material, name);
184                 if(name == "")
185                         name = "unknown_block";
186                 name = itemdef->getAlias(name);
187                 count = materialcount;
188         }
189         else if(name == "MaterialItem2")
190         {
191                 // Obsoleted on 2011-11-16
192
193                 u16 material;
194                 is>>material;
195                 u16 materialcount;
196                 is>>materialcount;
197                 if(material > MAX_CONTENT)
198                         throw SerializationError("Too large material number");
199                 // Convert old id to name
200                 NameIdMapping legacy_nimap;
201                 content_mapnode_get_name_id_mapping(&legacy_nimap);
202                 legacy_nimap.getName(material, name);
203                 if(name == "")
204                         name = "unknown_block";
205                 name = itemdef->getAlias(name);
206                 count = materialcount;
207         }
208         else if(name == "node" || name == "NodeItem" || name == "MaterialItem3"
209                         || name == "craft" || name == "CraftItem")
210         {
211                 // Obsoleted on 2012-01-07
212
213                 std::string all;
214                 std::getline(is, all, '\n');
215                 // First attempt to read inside ""
216                 Strfnd fnd(all);
217                 fnd.next("\"");
218                 // If didn't skip to end, we have ""s
219                 if(!fnd.atend()){
220                         name = fnd.next("\"");
221                 } else { // No luck, just read a word then
222                         fnd.start(all);
223                         name = fnd.next(" ");
224                 }
225                 fnd.skip_over(" ");
226                 name = itemdef->getAlias(name);
227                 count = stoi(trim(fnd.next("")));
228                 if(count == 0)
229                         count = 1;
230         }
231         else if(name == "MBOItem")
232         {
233                 // Obsoleted on 2011-10-14
234                 throw SerializationError("MBOItem not supported anymore");
235         }
236         else if(name == "tool" || name == "ToolItem")
237         {
238                 // Obsoleted on 2012-01-07
239
240                 std::string all;
241                 std::getline(is, all, '\n');
242                 // First attempt to read inside ""
243                 Strfnd fnd(all);
244                 fnd.next("\"");
245                 // If didn't skip to end, we have ""s
246                 if(!fnd.atend()){
247                         name = fnd.next("\"");
248                 } else { // No luck, just read a word then
249                         fnd.start(all);
250                         name = fnd.next(" ");
251                 }
252                 count = 1;
253                 // Then read wear
254                 fnd.skip_over(" ");
255                 name = itemdef->getAlias(name);
256                 wear = stoi(trim(fnd.next("")));
257         }
258         else
259         {
260                 do  // This loop is just to allow "break;"
261                 {
262                         // The real thing
263
264                         // Apply item aliases
265                         name = itemdef->getAlias(name);
266
267                         // Read the count
268                         std::string count_str;
269                         std::getline(is, count_str, ' ');
270                         if(count_str.empty())
271                         {
272                                 count = 1;
273                                 break;
274                         }
275                         else
276                                 count = stoi(count_str);
277
278                         // Read the wear
279                         std::string wear_str;
280                         std::getline(is, wear_str, ' ');
281                         if(wear_str.empty())
282                                 break;
283                         else
284                                 wear = stoi(wear_str);
285
286                         // Read metadata
287                         metadata = deSerializeJsonStringIfNeeded(is);
288
289                         // In case fields are added after metadata, skip space here:
290                         //std::getline(is, tmp, ' ');
291                         //if(!tmp.empty())
292                         //      throw SerializationError("Unexpected text after metadata");
293
294                 } while(false);
295         }
296
297         if(name.empty() || count == 0)
298                 clear();
299         else if(itemdef->get(name).type == ITEM_TOOL)
300                 count = 1;
301 }
302
303 void ItemStack::deSerialize(const std::string &str, IItemDefManager *itemdef)
304 {
305         std::istringstream is(str, std::ios::binary);
306         deSerialize(is, itemdef);
307 }
308
309 std::string ItemStack::getItemString() const
310 {
311         // Get item string
312         std::ostringstream os(std::ios::binary);
313         serialize(os);
314         return os.str();
315 }
316
317 ItemStack ItemStack::addItem(const ItemStack &newitem_,
318                 IItemDefManager *itemdef)
319 {
320         ItemStack newitem = newitem_;
321
322         // If the item is empty or the position invalid, bail out
323         if(newitem.empty())
324         {
325                 // nothing can be added trivially
326         }
327         // If this is an empty item, it's an easy job.
328         else if(empty())
329         {
330                 *this = newitem;
331                 newitem.clear();
332         }
333         // If item name differs, bail out
334         else if(name != newitem.name)
335         {
336                 // cannot be added
337         }
338         // If the item fits fully, add counter and delete it
339         else if(newitem.count <= freeSpace(itemdef))
340         {
341                 add(newitem.count);
342                 newitem.clear();
343         }
344         // Else the item does not fit fully. Add all that fits and return
345         // the rest.
346         else
347         {
348                 u16 freespace = freeSpace(itemdef);
349                 add(freespace);
350                 newitem.remove(freespace);
351         }
352
353         return newitem;
354 }
355
356 bool ItemStack::itemFits(const ItemStack &newitem_,
357                 ItemStack *restitem,
358                 IItemDefManager *itemdef) const
359 {
360         ItemStack newitem = newitem_;
361
362         // If the item is empty or the position invalid, bail out
363         if(newitem.empty())
364         {
365                 // nothing can be added trivially
366         }
367         // If this is an empty item, it's an easy job.
368         else if(empty())
369         {
370                 newitem.clear();
371         }
372         // If item name differs, bail out
373         else if(name != newitem.name)
374         {
375                 // cannot be added
376         }
377         // If the item fits fully, delete it
378         else if(newitem.count <= freeSpace(itemdef))
379         {
380                 newitem.clear();
381         }
382         // Else the item does not fit fully. Return the rest.
383         // the rest.
384         else
385         {
386                 u16 freespace = freeSpace(itemdef);
387                 newitem.remove(freespace);
388         }
389
390         if(restitem)
391                 *restitem = newitem;
392         return newitem.empty();
393 }
394
395 ItemStack ItemStack::takeItem(u32 takecount)
396 {
397         if(takecount == 0 || count == 0)
398                 return ItemStack();
399
400         ItemStack result = *this;
401         if(takecount >= count)
402         {
403                 // Take all
404                 clear();
405         }
406         else
407         {
408                 // Take part
409                 remove(takecount);
410                 result.count = takecount;
411         }
412         return result;
413 }
414
415 ItemStack ItemStack::peekItem(u32 peekcount) const
416 {
417         if(peekcount == 0 || count == 0)
418                 return ItemStack();
419
420         ItemStack result = *this;
421         if(peekcount < count)
422                 result.count = peekcount;
423         return result;
424 }
425
426 /*
427         Inventory
428 */
429
430 InventoryList::InventoryList(std::string name, u32 size, IItemDefManager *itemdef)
431 {
432         m_name = name;
433         m_size = size;
434         m_width = 0;
435         m_itemdef = itemdef;
436         clearItems();
437         //m_dirty = false;
438 }
439
440 InventoryList::~InventoryList()
441 {
442 }
443
444 void InventoryList::clearItems()
445 {
446         m_items.clear();
447
448         for(u32 i=0; i<m_size; i++)
449         {
450                 m_items.push_back(ItemStack());
451         }
452
453         //setDirty(true);
454 }
455
456 void InventoryList::setSize(u32 newsize)
457 {
458         if(newsize != m_items.size())
459                 m_items.resize(newsize);
460         m_size = newsize;
461 }
462
463 void InventoryList::setWidth(u32 newwidth)
464 {
465         m_width = newwidth;
466 }
467
468 void InventoryList::setName(const std::string &name)
469 {
470         m_name = name;
471 }
472
473 void InventoryList::serialize(std::ostream &os) const
474 {
475         //os.imbue(std::locale("C"));
476         
477         os<<"Width "<<m_width<<"\n";
478
479         for(u32 i=0; i<m_items.size(); i++)
480         {
481                 const ItemStack &item = m_items[i];
482                 if(item.empty())
483                 {
484                         os<<"Empty";
485                 }
486                 else
487                 {
488                         os<<"Item ";
489                         item.serialize(os);
490                 }
491                 os<<"\n";
492         }
493
494         os<<"EndInventoryList\n";
495 }
496
497 void InventoryList::deSerialize(std::istream &is)
498 {
499         //is.imbue(std::locale("C"));
500
501         clearItems();
502         u32 item_i = 0;
503         m_width = 0;
504
505         for(;;)
506         {
507                 std::string line;
508                 std::getline(is, line, '\n');
509
510                 std::istringstream iss(line);
511                 //iss.imbue(std::locale("C"));
512
513                 std::string name;
514                 std::getline(iss, name, ' ');
515
516                 if(name == "EndInventoryList")
517                 {
518                         break;
519                 }
520                 // This is a temporary backwards compatibility fix
521                 else if(name == "end")
522                 {
523                         break;
524                 }
525                 else if(name == "Width")
526                 {
527                         iss >> m_width;
528                         if (iss.fail())
529                                 throw SerializationError("incorrect width property");
530                 }
531                 else if(name == "Item")
532                 {
533                         if(item_i > getSize() - 1)
534                                 throw SerializationError("too many items");
535                         ItemStack item;
536                         item.deSerialize(iss, m_itemdef);
537                         m_items[item_i++] = item;
538                 }
539                 else if(name == "Empty")
540                 {
541                         if(item_i > getSize() - 1)
542                                 throw SerializationError("too many items");
543                         m_items[item_i++].clear();
544                 }
545         }
546 }
547
548 InventoryList::InventoryList(const InventoryList &other)
549 {
550         *this = other;
551 }
552
553 InventoryList & InventoryList::operator = (const InventoryList &other)
554 {
555         m_items = other.m_items;
556         m_size = other.m_size;
557         m_width = other.m_width;
558         m_name = other.m_name;
559         m_itemdef = other.m_itemdef;
560         //setDirty(true);
561
562         return *this;
563 }
564
565 const std::string &InventoryList::getName() const
566 {
567         return m_name;
568 }
569
570 u32 InventoryList::getSize() const
571 {
572         return m_items.size();
573 }
574
575 u32 InventoryList::getWidth() const
576 {
577         return m_width;
578 }
579
580 u32 InventoryList::getUsedSlots() const
581 {
582         u32 num = 0;
583         for(u32 i=0; i<m_items.size(); i++)
584         {
585                 if(!m_items[i].empty())
586                         num++;
587         }
588         return num;
589 }
590
591 u32 InventoryList::getFreeSlots() const
592 {
593         return getSize() - getUsedSlots();
594 }
595
596 const ItemStack& InventoryList::getItem(u32 i) const
597 {
598         assert(i < m_size);
599         return m_items[i];
600 }
601
602 ItemStack& InventoryList::getItem(u32 i)
603 {
604         assert(i < m_size);
605         return m_items[i];
606 }
607
608 ItemStack InventoryList::changeItem(u32 i, const ItemStack &newitem)
609 {
610         if(i >= m_items.size())
611                 return newitem;
612
613         ItemStack olditem = m_items[i];
614         m_items[i] = newitem;
615         //setDirty(true);
616         return olditem;
617 }
618
619 void InventoryList::deleteItem(u32 i)
620 {
621         assert(i < m_items.size());
622         m_items[i].clear();
623 }
624
625 ItemStack InventoryList::addItem(const ItemStack &newitem_)
626 {
627         ItemStack newitem = newitem_;
628
629         if(newitem.empty())
630                 return newitem;
631         
632         /*
633                 First try to find if it could be added to some existing items
634         */
635         for(u32 i=0; i<m_items.size(); i++)
636         {
637                 // Ignore empty slots
638                 if(m_items[i].empty())
639                         continue;
640                 // Try adding
641                 newitem = addItem(i, newitem);
642                 if(newitem.empty())
643                         return newitem; // All was eaten
644         }
645
646         /*
647                 Then try to add it to empty slots
648         */
649         for(u32 i=0; i<m_items.size(); i++)
650         {
651                 // Ignore unempty slots
652                 if(!m_items[i].empty())
653                         continue;
654                 // Try adding
655                 newitem = addItem(i, newitem);
656                 if(newitem.empty())
657                         return newitem; // All was eaten
658         }
659
660         // Return leftover
661         return newitem;
662 }
663
664 ItemStack InventoryList::addItem(u32 i, const ItemStack &newitem)
665 {
666         if(i >= m_items.size())
667                 return newitem;
668
669         ItemStack leftover = m_items[i].addItem(newitem, m_itemdef);
670         //if(leftover != newitem)
671         //      setDirty(true);
672         return leftover;
673 }
674
675 bool InventoryList::itemFits(const u32 i, const ItemStack &newitem,
676                 ItemStack *restitem) const
677 {
678         if(i >= m_items.size())
679         {
680                 if(restitem)
681                         *restitem = newitem;
682                 return false;
683         }
684
685         return m_items[i].itemFits(newitem, restitem, m_itemdef);
686 }
687
688 bool InventoryList::roomForItem(const ItemStack &item_) const
689 {
690         ItemStack item = item_;
691         ItemStack leftover;
692         for(u32 i=0; i<m_items.size(); i++)
693         {
694                 if(itemFits(i, item, &leftover))
695                         return true;
696                 item = leftover;
697         }
698         return false;
699 }
700
701 bool InventoryList::containsItem(const ItemStack &item) const
702 {
703         u32 count = item.count;
704         if(count == 0)
705                 return true;
706         for(std::vector<ItemStack>::const_reverse_iterator
707                         i = m_items.rbegin();
708                         i != m_items.rend(); i++)
709         {
710                 if(count == 0)
711                         break;
712                 if(i->name == item.name)
713                 {
714                         if(i->count >= count)
715                                 return true;
716                         else
717                                 count -= i->count;
718                 }
719         }
720         return false;
721 }
722
723 ItemStack InventoryList::removeItem(const ItemStack &item)
724 {
725         ItemStack removed;
726         for(std::vector<ItemStack>::reverse_iterator
727                         i = m_items.rbegin();
728                         i != m_items.rend(); i++)
729         {
730                 if(i->name == item.name)
731                 {
732                         u32 still_to_remove = item.count - removed.count;
733                         removed.addItem(i->takeItem(still_to_remove), m_itemdef);
734                         if(removed.count == item.count)
735                                 break;
736                 }
737         }
738         return removed;
739 }
740
741 ItemStack InventoryList::takeItem(u32 i, u32 takecount)
742 {
743         if(i >= m_items.size())
744                 return ItemStack();
745
746         ItemStack taken = m_items[i].takeItem(takecount);
747         //if(!taken.empty())
748         //      setDirty(true);
749         return taken;
750 }
751
752 ItemStack InventoryList::peekItem(u32 i, u32 peekcount) const
753 {
754         if(i >= m_items.size())
755                 return ItemStack();
756
757         return m_items[i].peekItem(peekcount);
758 }
759
760 void InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i, u32 count)
761 {
762         if(this == dest && i == dest_i)
763                 return;
764
765         // Take item from source list
766         ItemStack item1;
767         if(count == 0)
768                 item1 = changeItem(i, ItemStack());
769         else
770                 item1 = takeItem(i, count);
771
772         if(item1.empty())
773                 return;
774
775         // Try to add the item to destination list
776         u32 oldcount = item1.count;
777         item1 = dest->addItem(dest_i, item1);
778
779         // If something is returned, the item was not fully added
780         if(!item1.empty())
781         {
782                 // If olditem is returned, nothing was added.
783                 bool nothing_added = (item1.count == oldcount);
784
785                 // If something else is returned, part of the item was left unadded.
786                 // Add the other part back to the source item
787                 addItem(i, item1);
788
789                 // If olditem is returned, nothing was added.
790                 // Swap the items
791                 if(nothing_added)
792                 {
793                         // Take item from source list
794                         item1 = changeItem(i, ItemStack());
795                         // Adding was not possible, swap the items.
796                         ItemStack item2 = dest->changeItem(dest_i, item1);
797                         // Put item from destination list to the source list
798                         changeItem(i, item2);
799                 }
800         }
801 }
802
803 /*
804         Inventory
805 */
806
807 Inventory::~Inventory()
808 {
809         clear();
810 }
811
812 void Inventory::clear()
813 {
814         for(u32 i=0; i<m_lists.size(); i++)
815         {
816                 delete m_lists[i];
817         }
818         m_lists.clear();
819 }
820
821 void Inventory::clearContents()
822 {
823         for(u32 i=0; i<m_lists.size(); i++)
824         {
825                 InventoryList *list = m_lists[i];
826                 for(u32 j=0; j<list->getSize(); j++)
827                 {
828                         list->deleteItem(j);
829                 }
830         }
831 }
832
833 Inventory::Inventory(IItemDefManager *itemdef)
834 {
835         m_itemdef = itemdef;
836 }
837
838 Inventory::Inventory(const Inventory &other)
839 {
840         *this = other;
841 }
842
843 Inventory & Inventory::operator = (const Inventory &other)
844 {
845         // Gracefully handle self assignment
846         if(this != &other)
847         {
848                 clear();
849                 m_itemdef = other.m_itemdef;
850                 for(u32 i=0; i<other.m_lists.size(); i++)
851                 {
852                         m_lists.push_back(new InventoryList(*other.m_lists[i]));
853                 }
854         }
855         return *this;
856 }
857
858 void Inventory::serialize(std::ostream &os) const
859 {
860         for(u32 i=0; i<m_lists.size(); i++)
861         {
862                 InventoryList *list = m_lists[i];
863                 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
864                 list->serialize(os);
865         }
866
867         os<<"EndInventory\n";
868 }
869
870 void Inventory::deSerialize(std::istream &is)
871 {
872         clear();
873
874         for(;;)
875         {
876                 std::string line;
877                 std::getline(is, line, '\n');
878
879                 std::istringstream iss(line);
880
881                 std::string name;
882                 std::getline(iss, name, ' ');
883
884                 if(name == "EndInventory")
885                 {
886                         break;
887                 }
888                 // This is a temporary backwards compatibility fix
889                 else if(name == "end")
890                 {
891                         break;
892                 }
893                 else if(name == "List")
894                 {
895                         std::string listname;
896                         u32 listsize;
897
898                         std::getline(iss, listname, ' ');
899                         iss>>listsize;
900
901                         InventoryList *list = new InventoryList(listname, listsize, m_itemdef);
902                         list->deSerialize(is);
903
904                         m_lists.push_back(list);
905                 }
906                 else
907                 {
908                         throw SerializationError("invalid inventory specifier");
909                 }
910         }
911 }
912
913 InventoryList * Inventory::addList(const std::string &name, u32 size)
914 {
915         s32 i = getListIndex(name);
916         if(i != -1)
917         {
918                 if(m_lists[i]->getSize() != size)
919                 {
920                         delete m_lists[i];
921                         m_lists[i] = new InventoryList(name, size, m_itemdef);
922                 }
923                 return m_lists[i];
924         }
925         else
926         {
927                 InventoryList *list = new InventoryList(name, size, m_itemdef);
928                 m_lists.push_back(list);
929                 return list;
930         }
931 }
932
933 InventoryList * Inventory::getList(const std::string &name)
934 {
935         s32 i = getListIndex(name);
936         if(i == -1)
937                 return NULL;
938         return m_lists[i];
939 }
940
941 std::vector<const InventoryList*> Inventory::getLists()
942 {
943         std::vector<const InventoryList*> lists;
944         for(u32 i=0; i<m_lists.size(); i++)
945         {
946                 InventoryList *list = m_lists[i];
947                 lists.push_back(list);
948         }
949         return lists;
950 }
951
952 bool Inventory::deleteList(const std::string &name)
953 {
954         s32 i = getListIndex(name);
955         if(i == -1)
956                 return false;
957         delete m_lists[i];
958         m_lists.erase(m_lists.begin() + i);
959         return true;
960 }
961
962 const InventoryList * Inventory::getList(const std::string &name) const
963 {
964         s32 i = getListIndex(name);
965         if(i == -1)
966                 return NULL;
967         return m_lists[i];
968 }
969
970 const s32 Inventory::getListIndex(const std::string &name) const
971 {
972         for(u32 i=0; i<m_lists.size(); i++)
973         {
974                 if(m_lists[i]->getName() == name)
975                         return i;
976         }
977         return -1;
978 }
979
980 //END