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