]> git.lizzy.rs Git - dragonfireclient.git/blob - src/inventory.cpp
Add disable_jump and fall_damage_add_percent node groups
[dragonfireclient.git] / src / inventory.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 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                 else
546                 {
547                         throw SerializationError("Unknown inventory identifier");
548                 }
549         }
550 }
551
552 InventoryList::InventoryList(const InventoryList &other)
553 {
554         *this = other;
555 }
556
557 InventoryList & InventoryList::operator = (const InventoryList &other)
558 {
559         m_items = other.m_items;
560         m_size = other.m_size;
561         m_width = other.m_width;
562         m_name = other.m_name;
563         m_itemdef = other.m_itemdef;
564         //setDirty(true);
565
566         return *this;
567 }
568
569 const std::string &InventoryList::getName() const
570 {
571         return m_name;
572 }
573
574 u32 InventoryList::getSize() const
575 {
576         return m_items.size();
577 }
578
579 u32 InventoryList::getWidth() const
580 {
581         return m_width;
582 }
583
584 u32 InventoryList::getUsedSlots() const
585 {
586         u32 num = 0;
587         for(u32 i=0; i<m_items.size(); i++)
588         {
589                 if(!m_items[i].empty())
590                         num++;
591         }
592         return num;
593 }
594
595 u32 InventoryList::getFreeSlots() const
596 {
597         return getSize() - getUsedSlots();
598 }
599
600 const ItemStack& InventoryList::getItem(u32 i) const
601 {
602         assert(i < m_size);
603         return m_items[i];
604 }
605
606 ItemStack& InventoryList::getItem(u32 i)
607 {
608         assert(i < m_size);
609         return m_items[i];
610 }
611
612 ItemStack InventoryList::changeItem(u32 i, const ItemStack &newitem)
613 {
614         if(i >= m_items.size())
615                 return newitem;
616
617         ItemStack olditem = m_items[i];
618         m_items[i] = newitem;
619         //setDirty(true);
620         return olditem;
621 }
622
623 void InventoryList::deleteItem(u32 i)
624 {
625         assert(i < m_items.size());
626         m_items[i].clear();
627 }
628
629 ItemStack InventoryList::addItem(const ItemStack &newitem_)
630 {
631         ItemStack newitem = newitem_;
632
633         if(newitem.empty())
634                 return newitem;
635         
636         /*
637                 First try to find if it could be added to some existing items
638         */
639         for(u32 i=0; i<m_items.size(); i++)
640         {
641                 // Ignore empty slots
642                 if(m_items[i].empty())
643                         continue;
644                 // Try adding
645                 newitem = addItem(i, newitem);
646                 if(newitem.empty())
647                         return newitem; // All was eaten
648         }
649
650         /*
651                 Then try to add it to empty slots
652         */
653         for(u32 i=0; i<m_items.size(); i++)
654         {
655                 // Ignore unempty slots
656                 if(!m_items[i].empty())
657                         continue;
658                 // Try adding
659                 newitem = addItem(i, newitem);
660                 if(newitem.empty())
661                         return newitem; // All was eaten
662         }
663
664         // Return leftover
665         return newitem;
666 }
667
668 ItemStack InventoryList::addItem(u32 i, const ItemStack &newitem)
669 {
670         if(i >= m_items.size())
671                 return newitem;
672
673         ItemStack leftover = m_items[i].addItem(newitem, m_itemdef);
674         //if(leftover != newitem)
675         //      setDirty(true);
676         return leftover;
677 }
678
679 bool InventoryList::itemFits(const u32 i, const ItemStack &newitem,
680                 ItemStack *restitem) const
681 {
682         if(i >= m_items.size())
683         {
684                 if(restitem)
685                         *restitem = newitem;
686                 return false;
687         }
688
689         return m_items[i].itemFits(newitem, restitem, m_itemdef);
690 }
691
692 bool InventoryList::roomForItem(const ItemStack &item_) const
693 {
694         ItemStack item = item_;
695         ItemStack leftover;
696         for(u32 i=0; i<m_items.size(); i++)
697         {
698                 if(itemFits(i, item, &leftover))
699                         return true;
700                 item = leftover;
701         }
702         return false;
703 }
704
705 bool InventoryList::containsItem(const ItemStack &item) const
706 {
707         u32 count = item.count;
708         if(count == 0)
709                 return true;
710         for(std::vector<ItemStack>::const_reverse_iterator
711                         i = m_items.rbegin();
712                         i != m_items.rend(); i++)
713         {
714                 if(count == 0)
715                         break;
716                 if(i->name == item.name)
717                 {
718                         if(i->count >= count)
719                                 return true;
720                         else
721                                 count -= i->count;
722                 }
723         }
724         return false;
725 }
726
727 ItemStack InventoryList::removeItem(const ItemStack &item)
728 {
729         ItemStack removed;
730         for(std::vector<ItemStack>::reverse_iterator
731                         i = m_items.rbegin();
732                         i != m_items.rend(); i++)
733         {
734                 if(i->name == item.name)
735                 {
736                         u32 still_to_remove = item.count - removed.count;
737                         removed.addItem(i->takeItem(still_to_remove), m_itemdef);
738                         if(removed.count == item.count)
739                                 break;
740                 }
741         }
742         return removed;
743 }
744
745 ItemStack InventoryList::takeItem(u32 i, u32 takecount)
746 {
747         if(i >= m_items.size())
748                 return ItemStack();
749
750         ItemStack taken = m_items[i].takeItem(takecount);
751         //if(!taken.empty())
752         //      setDirty(true);
753         return taken;
754 }
755
756 ItemStack InventoryList::peekItem(u32 i, u32 peekcount) const
757 {
758         if(i >= m_items.size())
759                 return ItemStack();
760
761         return m_items[i].peekItem(peekcount);
762 }
763
764 void InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i, u32 count)
765 {
766         if(this == dest && i == dest_i)
767                 return;
768
769         // Take item from source list
770         ItemStack item1;
771         if(count == 0)
772                 item1 = changeItem(i, ItemStack());
773         else
774                 item1 = takeItem(i, count);
775
776         if(item1.empty())
777                 return;
778
779         // Try to add the item to destination list
780         u32 oldcount = item1.count;
781         item1 = dest->addItem(dest_i, item1);
782
783         // If something is returned, the item was not fully added
784         if(!item1.empty())
785         {
786                 // If olditem is returned, nothing was added.
787                 bool nothing_added = (item1.count == oldcount);
788
789                 // If something else is returned, part of the item was left unadded.
790                 // Add the other part back to the source item
791                 addItem(i, item1);
792
793                 // If olditem is returned, nothing was added.
794                 // Swap the items
795                 if(nothing_added)
796                 {
797                         // Take item from source list
798                         item1 = changeItem(i, ItemStack());
799                         // Adding was not possible, swap the items.
800                         ItemStack item2 = dest->changeItem(dest_i, item1);
801                         // Put item from destination list to the source list
802                         changeItem(i, item2);
803                 }
804         }
805 }
806
807 /*
808         Inventory
809 */
810
811 Inventory::~Inventory()
812 {
813         clear();
814 }
815
816 void Inventory::clear()
817 {
818         for(u32 i=0; i<m_lists.size(); i++)
819         {
820                 delete m_lists[i];
821         }
822         m_lists.clear();
823 }
824
825 void Inventory::clearContents()
826 {
827         for(u32 i=0; i<m_lists.size(); i++)
828         {
829                 InventoryList *list = m_lists[i];
830                 for(u32 j=0; j<list->getSize(); j++)
831                 {
832                         list->deleteItem(j);
833                 }
834         }
835 }
836
837 Inventory::Inventory(IItemDefManager *itemdef)
838 {
839         m_itemdef = itemdef;
840 }
841
842 Inventory::Inventory(const Inventory &other)
843 {
844         *this = other;
845 }
846
847 Inventory & Inventory::operator = (const Inventory &other)
848 {
849         // Gracefully handle self assignment
850         if(this != &other)
851         {
852                 clear();
853                 m_itemdef = other.m_itemdef;
854                 for(u32 i=0; i<other.m_lists.size(); i++)
855                 {
856                         m_lists.push_back(new InventoryList(*other.m_lists[i]));
857                 }
858         }
859         return *this;
860 }
861
862 void Inventory::serialize(std::ostream &os) const
863 {
864         for(u32 i=0; i<m_lists.size(); i++)
865         {
866                 InventoryList *list = m_lists[i];
867                 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
868                 list->serialize(os);
869         }
870
871         os<<"EndInventory\n";
872 }
873
874 void Inventory::deSerialize(std::istream &is)
875 {
876         clear();
877
878         for(;;)
879         {
880                 std::string line;
881                 std::getline(is, line, '\n');
882
883                 std::istringstream iss(line);
884
885                 std::string name;
886                 std::getline(iss, name, ' ');
887
888                 if(name == "EndInventory")
889                 {
890                         break;
891                 }
892                 // This is a temporary backwards compatibility fix
893                 else if(name == "end")
894                 {
895                         break;
896                 }
897                 else if(name == "List")
898                 {
899                         std::string listname;
900                         u32 listsize;
901
902                         std::getline(iss, listname, ' ');
903                         iss>>listsize;
904
905                         InventoryList *list = new InventoryList(listname, listsize, m_itemdef);
906                         list->deSerialize(is);
907
908                         m_lists.push_back(list);
909                 }
910                 else
911                 {
912                         throw SerializationError("Unknown inventory identifier");
913                 }
914         }
915 }
916
917 InventoryList * Inventory::addList(const std::string &name, u32 size)
918 {
919         s32 i = getListIndex(name);
920         if(i != -1)
921         {
922                 if(m_lists[i]->getSize() != size)
923                 {
924                         delete m_lists[i];
925                         m_lists[i] = new InventoryList(name, size, m_itemdef);
926                 }
927                 return m_lists[i];
928         }
929         else
930         {
931                 InventoryList *list = new InventoryList(name, size, m_itemdef);
932                 m_lists.push_back(list);
933                 return list;
934         }
935 }
936
937 InventoryList * Inventory::getList(const std::string &name)
938 {
939         s32 i = getListIndex(name);
940         if(i == -1)
941                 return NULL;
942         return m_lists[i];
943 }
944
945 std::vector<const InventoryList*> Inventory::getLists()
946 {
947         std::vector<const InventoryList*> lists;
948         for(u32 i=0; i<m_lists.size(); i++)
949         {
950                 InventoryList *list = m_lists[i];
951                 lists.push_back(list);
952         }
953         return lists;
954 }
955
956 bool Inventory::deleteList(const std::string &name)
957 {
958         s32 i = getListIndex(name);
959         if(i == -1)
960                 return false;
961         delete m_lists[i];
962         m_lists.erase(m_lists.begin() + i);
963         return true;
964 }
965
966 const InventoryList * Inventory::getList(const std::string &name) const
967 {
968         s32 i = getListIndex(name);
969         if(i == -1)
970                 return NULL;
971         return m_lists[i];
972 }
973
974 const s32 Inventory::getListIndex(const std::string &name) const
975 {
976         for(u32 i=0; i<m_lists.size(); i++)
977         {
978                 if(m_lists[i]->getName() == name)
979                         return i;
980         }
981         return -1;
982 }
983
984 //END