]> git.lizzy.rs Git - minetest.git/blob - src/inventory.cpp
0a0a29cd550f782d6276d208212bf011d0bb36df
[minetest.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_itemdef = itemdef;
435         clearItems();
436         //m_dirty = false;
437 }
438
439 InventoryList::~InventoryList()
440 {
441 }
442
443 void InventoryList::clearItems()
444 {
445         m_items.clear();
446
447         for(u32 i=0; i<m_size; i++)
448         {
449                 m_items.push_back(ItemStack());
450         }
451
452         //setDirty(true);
453 }
454
455 void InventoryList::setSize(u32 newsize)
456 {
457         if(newsize != m_items.size())
458                 m_items.resize(newsize);
459         m_size = newsize;
460 }
461
462 void InventoryList::setName(const std::string &name)
463 {
464         m_name = name;
465 }
466
467 void InventoryList::serialize(std::ostream &os) const
468 {
469         //os.imbue(std::locale("C"));
470         
471         for(u32 i=0; i<m_items.size(); i++)
472         {
473                 const ItemStack &item = m_items[i];
474                 if(item.empty())
475                 {
476                         os<<"Empty";
477                 }
478                 else
479                 {
480                         os<<"Item ";
481                         item.serialize(os);
482                 }
483                 os<<"\n";
484         }
485
486         os<<"EndInventoryList\n";
487 }
488
489 void InventoryList::deSerialize(std::istream &is)
490 {
491         //is.imbue(std::locale("C"));
492
493         clearItems();
494         u32 item_i = 0;
495
496         for(;;)
497         {
498                 std::string line;
499                 std::getline(is, line, '\n');
500
501                 std::istringstream iss(line);
502                 //iss.imbue(std::locale("C"));
503
504                 std::string name;
505                 std::getline(iss, name, ' ');
506
507                 if(name == "EndInventoryList")
508                 {
509                         break;
510                 }
511                 // This is a temporary backwards compatibility fix
512                 else if(name == "end")
513                 {
514                         break;
515                 }
516                 else if(name == "Item")
517                 {
518                         if(item_i > getSize() - 1)
519                                 throw SerializationError("too many items");
520                         ItemStack item;
521                         item.deSerialize(iss, m_itemdef);
522                         m_items[item_i++] = item;
523                 }
524                 else if(name == "Empty")
525                 {
526                         if(item_i > getSize() - 1)
527                                 throw SerializationError("too many items");
528                         m_items[item_i++].clear();
529                 }
530                 else
531                 {
532                         throw SerializationError("Unknown inventory identifier");
533                 }
534         }
535 }
536
537 InventoryList::InventoryList(const InventoryList &other)
538 {
539         *this = other;
540 }
541
542 InventoryList & InventoryList::operator = (const InventoryList &other)
543 {
544         m_items = other.m_items;
545         m_size = other.m_size;
546         m_name = other.m_name;
547         m_itemdef = other.m_itemdef;
548         //setDirty(true);
549
550         return *this;
551 }
552
553 const std::string &InventoryList::getName() const
554 {
555         return m_name;
556 }
557
558 u32 InventoryList::getSize() const
559 {
560         return m_items.size();
561 }
562
563 u32 InventoryList::getUsedSlots() const
564 {
565         u32 num = 0;
566         for(u32 i=0; i<m_items.size(); i++)
567         {
568                 if(!m_items[i].empty())
569                         num++;
570         }
571         return num;
572 }
573
574 u32 InventoryList::getFreeSlots() const
575 {
576         return getSize() - getUsedSlots();
577 }
578
579 const ItemStack& InventoryList::getItem(u32 i) const
580 {
581         assert(i < m_size);
582         return m_items[i];
583 }
584
585 ItemStack& InventoryList::getItem(u32 i)
586 {
587         assert(i < m_size);
588         return m_items[i];
589 }
590
591 ItemStack InventoryList::changeItem(u32 i, const ItemStack &newitem)
592 {
593         if(i >= m_items.size())
594                 return newitem;
595
596         ItemStack olditem = m_items[i];
597         m_items[i] = newitem;
598         //setDirty(true);
599         return olditem;
600 }
601
602 void InventoryList::deleteItem(u32 i)
603 {
604         assert(i < m_items.size());
605         m_items[i].clear();
606 }
607
608 ItemStack InventoryList::addItem(const ItemStack &newitem_)
609 {
610         ItemStack newitem = newitem_;
611
612         if(newitem.empty())
613                 return newitem;
614         
615         /*
616                 First try to find if it could be added to some existing items
617         */
618         for(u32 i=0; i<m_items.size(); i++)
619         {
620                 // Ignore empty slots
621                 if(m_items[i].empty())
622                         continue;
623                 // Try adding
624                 newitem = addItem(i, newitem);
625                 if(newitem.empty())
626                         return newitem; // All was eaten
627         }
628
629         /*
630                 Then try to add it to empty slots
631         */
632         for(u32 i=0; i<m_items.size(); i++)
633         {
634                 // Ignore unempty slots
635                 if(!m_items[i].empty())
636                         continue;
637                 // Try adding
638                 newitem = addItem(i, newitem);
639                 if(newitem.empty())
640                         return newitem; // All was eaten
641         }
642
643         // Return leftover
644         return newitem;
645 }
646
647 ItemStack InventoryList::addItem(u32 i, const ItemStack &newitem)
648 {
649         if(i >= m_items.size())
650                 return newitem;
651
652         ItemStack leftover = m_items[i].addItem(newitem, m_itemdef);
653         //if(leftover != newitem)
654         //      setDirty(true);
655         return leftover;
656 }
657
658 bool InventoryList::itemFits(const u32 i, const ItemStack &newitem,
659                 ItemStack *restitem) const
660 {
661         if(i >= m_items.size())
662         {
663                 if(restitem)
664                         *restitem = newitem;
665                 return false;
666         }
667
668         return m_items[i].itemFits(newitem, restitem, m_itemdef);
669 }
670
671 bool InventoryList::roomForItem(const ItemStack &item_) const
672 {
673         ItemStack item = item_;
674         ItemStack leftover;
675         for(u32 i=0; i<m_items.size(); i++)
676         {
677                 if(itemFits(i, item, &leftover))
678                         return true;
679                 item = leftover;
680         }
681         return false;
682 }
683
684 bool InventoryList::containsItem(const ItemStack &item) const
685 {
686         u32 count = item.count;
687         if(count == 0)
688                 return true;
689         for(std::vector<ItemStack>::const_reverse_iterator
690                         i = m_items.rbegin();
691                         i != m_items.rend(); i++)
692         {
693                 if(count == 0)
694                         break;
695                 if(i->name == item.name)
696                 {
697                         if(i->count >= count)
698                                 return true;
699                         else
700                                 count -= i->count;
701                 }
702         }
703         return false;
704 }
705
706 ItemStack InventoryList::removeItem(const ItemStack &item)
707 {
708         ItemStack removed;
709         for(std::vector<ItemStack>::reverse_iterator
710                         i = m_items.rbegin();
711                         i != m_items.rend(); i++)
712         {
713                 if(i->name == item.name)
714                 {
715                         u32 still_to_remove = item.count - removed.count;
716                         removed.addItem(i->takeItem(still_to_remove), m_itemdef);
717                         if(removed.count == item.count)
718                                 break;
719                 }
720         }
721         return removed;
722 }
723
724 ItemStack InventoryList::takeItem(u32 i, u32 takecount)
725 {
726         if(i >= m_items.size())
727                 return ItemStack();
728
729         ItemStack taken = m_items[i].takeItem(takecount);
730         //if(!taken.empty())
731         //      setDirty(true);
732         return taken;
733 }
734
735 ItemStack InventoryList::peekItem(u32 i, u32 peekcount) const
736 {
737         if(i >= m_items.size())
738                 return ItemStack();
739
740         return m_items[i].peekItem(peekcount);
741 }
742
743 void InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i, u32 count)
744 {
745         if(this == dest && i == dest_i)
746                 return;
747
748         // Take item from source list
749         ItemStack item1;
750         if(count == 0)
751                 item1 = changeItem(i, ItemStack());
752         else
753                 item1 = takeItem(i, count);
754
755         if(item1.empty())
756                 return;
757
758         // Try to add the item to destination list
759         u32 oldcount = item1.count;
760         item1 = dest->addItem(dest_i, item1);
761
762         // If something is returned, the item was not fully added
763         if(!item1.empty())
764         {
765                 // If olditem is returned, nothing was added.
766                 bool nothing_added = (item1.count == oldcount);
767
768                 // If something else is returned, part of the item was left unadded.
769                 // Add the other part back to the source item
770                 addItem(i, item1);
771
772                 // If olditem is returned, nothing was added.
773                 // Swap the items
774                 if(nothing_added)
775                 {
776                         // Take item from source list
777                         item1 = changeItem(i, ItemStack());
778                         // Adding was not possible, swap the items.
779                         ItemStack item2 = dest->changeItem(dest_i, item1);
780                         // Put item from destination list to the source list
781                         changeItem(i, item2);
782                 }
783         }
784 }
785
786 /*
787         Inventory
788 */
789
790 Inventory::~Inventory()
791 {
792         clear();
793 }
794
795 void Inventory::clear()
796 {
797         for(u32 i=0; i<m_lists.size(); i++)
798         {
799                 delete m_lists[i];
800         }
801         m_lists.clear();
802 }
803
804 void Inventory::clearContents()
805 {
806         for(u32 i=0; i<m_lists.size(); i++)
807         {
808                 InventoryList *list = m_lists[i];
809                 for(u32 j=0; j<list->getSize(); j++)
810                 {
811                         list->deleteItem(j);
812                 }
813         }
814 }
815
816 Inventory::Inventory(IItemDefManager *itemdef)
817 {
818         m_itemdef = itemdef;
819 }
820
821 Inventory::Inventory(const Inventory &other)
822 {
823         *this = other;
824 }
825
826 Inventory & Inventory::operator = (const Inventory &other)
827 {
828         // Gracefully handle self assignment
829         if(this != &other)
830         {
831                 clear();
832                 m_itemdef = other.m_itemdef;
833                 for(u32 i=0; i<other.m_lists.size(); i++)
834                 {
835                         m_lists.push_back(new InventoryList(*other.m_lists[i]));
836                 }
837         }
838         return *this;
839 }
840
841 void Inventory::serialize(std::ostream &os) const
842 {
843         for(u32 i=0; i<m_lists.size(); i++)
844         {
845                 InventoryList *list = m_lists[i];
846                 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
847                 list->serialize(os);
848         }
849
850         os<<"EndInventory\n";
851 }
852
853 void Inventory::deSerialize(std::istream &is)
854 {
855         clear();
856
857         for(;;)
858         {
859                 std::string line;
860                 std::getline(is, line, '\n');
861
862                 std::istringstream iss(line);
863
864                 std::string name;
865                 std::getline(iss, name, ' ');
866
867                 if(name == "EndInventory")
868                 {
869                         break;
870                 }
871                 // This is a temporary backwards compatibility fix
872                 else if(name == "end")
873                 {
874                         break;
875                 }
876                 else if(name == "List")
877                 {
878                         std::string listname;
879                         u32 listsize;
880
881                         std::getline(iss, listname, ' ');
882                         iss>>listsize;
883
884                         InventoryList *list = new InventoryList(listname, listsize, m_itemdef);
885                         list->deSerialize(is);
886
887                         m_lists.push_back(list);
888                 }
889                 else
890                 {
891                         throw SerializationError("Unknown inventory identifier");
892                 }
893         }
894 }
895
896 InventoryList * Inventory::addList(const std::string &name, u32 size)
897 {
898         s32 i = getListIndex(name);
899         if(i != -1)
900         {
901                 if(m_lists[i]->getSize() != size)
902                 {
903                         delete m_lists[i];
904                         m_lists[i] = new InventoryList(name, size, m_itemdef);
905                 }
906                 return m_lists[i];
907         }
908         else
909         {
910                 InventoryList *list = new InventoryList(name, size, m_itemdef);
911                 m_lists.push_back(list);
912                 return list;
913         }
914 }
915
916 InventoryList * Inventory::getList(const std::string &name)
917 {
918         s32 i = getListIndex(name);
919         if(i == -1)
920                 return NULL;
921         return m_lists[i];
922 }
923
924 std::vector<const InventoryList*> Inventory::getLists()
925 {
926         std::vector<const InventoryList*> lists;
927         for(u32 i=0; i<m_lists.size(); i++)
928         {
929                 InventoryList *list = m_lists[i];
930                 lists.push_back(list);
931         }
932         return lists;
933 }
934
935 bool Inventory::deleteList(const std::string &name)
936 {
937         s32 i = getListIndex(name);
938         if(i == -1)
939                 return false;
940         delete m_lists[i];
941         m_lists.erase(m_lists.begin() + i);
942         return true;
943 }
944
945 const InventoryList * Inventory::getList(const std::string &name) const
946 {
947         s32 i = getListIndex(name);
948         if(i == -1)
949                 return NULL;
950         return m_lists[i];
951 }
952
953 const s32 Inventory::getListIndex(const std::string &name) const
954 {
955         for(u32 i=0; i<m_lists.size(); i++)
956         {
957                 if(m_lists[i]->getName() == name)
958                         return i;
959         }
960         return -1;
961 }
962
963 //END