LocalPlayer::accelerateHorizontal: cleanups
[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         name(itemdef->getAlias(name_)),
51         count(count_),
52         wear(wear_)
53 {
54         if (name.empty() || count == 0)
55                 clear();
56         else if (itemdef->get(name).type == ITEM_TOOL)
57                 count = 1;
58 }
59
60 void ItemStack::serialize(std::ostream &os) const
61 {
62         DSTACK(FUNCTION_NAME);
63
64         if(empty())
65                 return;
66
67         // Check how many parts of the itemstring are needed
68         int parts = 1;
69         if(count != 1)
70                 parts = 2;
71         if(wear != 0)
72                 parts = 3;
73         if (!metadata.empty())
74                 parts = 4;
75
76         os<<serializeJsonStringIfNeeded(name);
77         if(parts >= 2)
78                 os<<" "<<count;
79         if(parts >= 3)
80                 os<<" "<<wear;
81         if (parts >= 4) {
82                 os << " ";
83                 metadata.serialize(os);
84         }
85 }
86
87 void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
88 {
89         DSTACK(FUNCTION_NAME);
90
91         clear();
92
93         // Read name
94         name = deSerializeJsonStringIfNeeded(is);
95
96         // Skip space
97         std::string tmp;
98         std::getline(is, tmp, ' ');
99         if(!tmp.empty())
100                 throw SerializationError("Unexpected text after item name");
101
102         if(name == "MaterialItem")
103         {
104                 // Obsoleted on 2011-07-30
105
106                 u16 material;
107                 is>>material;
108                 u16 materialcount;
109                 is>>materialcount;
110                 // Convert old materials
111                 if(material <= 0xff)
112                         material = content_translate_from_19_to_internal(material);
113                 if(material > 0xfff)
114                         throw SerializationError("Too large material number");
115                 // Convert old id to name
116                 NameIdMapping legacy_nimap;
117                 content_mapnode_get_name_id_mapping(&legacy_nimap);
118                 legacy_nimap.getName(material, name);
119                 if(name == "")
120                         name = "unknown_block";
121                 if (itemdef)
122                         name = itemdef->getAlias(name);
123                 count = materialcount;
124         }
125         else if(name == "MaterialItem2")
126         {
127                 // Obsoleted on 2011-11-16
128
129                 u16 material;
130                 is>>material;
131                 u16 materialcount;
132                 is>>materialcount;
133                 if(material > 0xfff)
134                         throw SerializationError("Too large material number");
135                 // Convert old id to name
136                 NameIdMapping legacy_nimap;
137                 content_mapnode_get_name_id_mapping(&legacy_nimap);
138                 legacy_nimap.getName(material, name);
139                 if(name == "")
140                         name = "unknown_block";
141                 if (itemdef)
142                         name = itemdef->getAlias(name);
143                 count = materialcount;
144         }
145         else if(name == "node" || name == "NodeItem" || name == "MaterialItem3"
146                         || name == "craft" || name == "CraftItem")
147         {
148                 // Obsoleted on 2012-01-07
149
150                 std::string all;
151                 std::getline(is, all, '\n');
152                 // First attempt to read inside ""
153                 Strfnd fnd(all);
154                 fnd.next("\"");
155                 // If didn't skip to end, we have ""s
156                 if(!fnd.at_end()){
157                         name = fnd.next("\"");
158                 } else { // No luck, just read a word then
159                         fnd.start(all);
160                         name = fnd.next(" ");
161                 }
162                 fnd.skip_over(" ");
163                 if (itemdef)
164                         name = itemdef->getAlias(name);
165                 count = stoi(trim(fnd.next("")));
166                 if(count == 0)
167                         count = 1;
168         }
169         else if(name == "MBOItem")
170         {
171                 // Obsoleted on 2011-10-14
172                 throw SerializationError("MBOItem not supported anymore");
173         }
174         else if(name == "tool" || name == "ToolItem")
175         {
176                 // Obsoleted on 2012-01-07
177
178                 std::string all;
179                 std::getline(is, all, '\n');
180                 // First attempt to read inside ""
181                 Strfnd fnd(all);
182                 fnd.next("\"");
183                 // If didn't skip to end, we have ""s
184                 if(!fnd.at_end()){
185                         name = fnd.next("\"");
186                 } else { // No luck, just read a word then
187                         fnd.start(all);
188                         name = fnd.next(" ");
189                 }
190                 count = 1;
191                 // Then read wear
192                 fnd.skip_over(" ");
193                 if (itemdef)
194                         name = itemdef->getAlias(name);
195                 wear = stoi(trim(fnd.next("")));
196         }
197         else
198         {
199                 do  // This loop is just to allow "break;"
200                 {
201                         // The real thing
202
203                         // Apply item aliases
204                         if (itemdef)
205                                 name = itemdef->getAlias(name);
206
207                         // Read the count
208                         std::string count_str;
209                         std::getline(is, count_str, ' ');
210                         if(count_str.empty())
211                         {
212                                 count = 1;
213                                 break;
214                         }
215                         else
216                                 count = stoi(count_str);
217
218                         // Read the wear
219                         std::string wear_str;
220                         std::getline(is, wear_str, ' ');
221                         if(wear_str.empty())
222                                 break;
223                         else
224                                 wear = stoi(wear_str);
225
226                         // Read metadata
227                         metadata.deSerialize(is);
228
229                         // In case fields are added after metadata, skip space here:
230                         //std::getline(is, tmp, ' ');
231                         //if(!tmp.empty())
232                         //      throw SerializationError("Unexpected text after metadata");
233
234                 } while(false);
235         }
236
237         if (name.empty() || count == 0)
238                 clear();
239         else if (itemdef && itemdef->get(name).type == ITEM_TOOL)
240                 count = 1;
241 }
242
243 void ItemStack::deSerialize(const std::string &str, IItemDefManager *itemdef)
244 {
245         std::istringstream is(str, std::ios::binary);
246         deSerialize(is, itemdef);
247 }
248
249 std::string ItemStack::getItemString() const
250 {
251         std::ostringstream os(std::ios::binary);
252         serialize(os);
253         return os.str();
254 }
255
256
257 ItemStack ItemStack::addItem(ItemStack newitem, IItemDefManager *itemdef)
258 {
259         // If the item is empty or the position invalid, bail out
260         if(newitem.empty())
261         {
262                 // nothing can be added trivially
263         }
264         // If this is an empty item, it's an easy job.
265         else if(empty())
266         {
267                 const u16 stackMax = newitem.getStackMax(itemdef);
268
269                 *this = newitem;
270
271                 // If the item fits fully, delete it
272                 if (count <= stackMax) {
273                         newitem.clear();
274                 } else { // Else the item does not fit fully. Return the rest.
275                         count = stackMax;
276                         newitem.remove(count);
277                 }
278         }
279         // If item name or metadata differs, bail out
280         else if (name != newitem.name
281                 || metadata != newitem.metadata)
282         {
283                 // cannot be added
284         }
285         // If the item fits fully, add counter and delete it
286         else if(newitem.count <= freeSpace(itemdef))
287         {
288                 add(newitem.count);
289                 newitem.clear();
290         }
291         // Else the item does not fit fully. Add all that fits and return
292         // the rest.
293         else
294         {
295                 u16 freespace = freeSpace(itemdef);
296                 add(freespace);
297                 newitem.remove(freespace);
298         }
299
300         return newitem;
301 }
302
303 bool ItemStack::itemFits(ItemStack newitem,
304                 ItemStack *restitem,
305                 IItemDefManager *itemdef) const
306 {
307
308         // If the item is empty or the position invalid, bail out
309         if(newitem.empty())
310         {
311                 // nothing can be added trivially
312         }
313         // If this is an empty item, it's an easy job.
314         else if(empty())
315         {
316                 const u16 stackMax = newitem.getStackMax(itemdef);
317
318                 // If the item fits fully, delete it
319                 if (newitem.count <= stackMax) {
320                         newitem.clear();
321                 } else { // Else the item does not fit fully. Return the rest.
322                         newitem.remove(stackMax);
323                 }
324         }
325         // If item name or metadata differs, bail out
326         else if (name != newitem.name
327                 || metadata != newitem.metadata)
328         {
329                 // cannot be added
330         }
331         // If the item fits fully, delete it
332         else if(newitem.count <= freeSpace(itemdef))
333         {
334                 newitem.clear();
335         }
336         // Else the item does not fit fully. Return the rest.
337         else
338         {
339                 u16 freespace = freeSpace(itemdef);
340                 newitem.remove(freespace);
341         }
342
343         if(restitem)
344                 *restitem = newitem;
345         return newitem.empty();
346 }
347
348 ItemStack ItemStack::takeItem(u32 takecount)
349 {
350         if(takecount == 0 || count == 0)
351                 return ItemStack();
352
353         ItemStack result = *this;
354         if(takecount >= count)
355         {
356                 // Take all
357                 clear();
358         }
359         else
360         {
361                 // Take part
362                 remove(takecount);
363                 result.count = takecount;
364         }
365         return result;
366 }
367
368 ItemStack ItemStack::peekItem(u32 peekcount) const
369 {
370         if(peekcount == 0 || count == 0)
371                 return ItemStack();
372
373         ItemStack result = *this;
374         if(peekcount < count)
375                 result.count = peekcount;
376         return result;
377 }
378
379 /*
380         Inventory
381 */
382
383 InventoryList::InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef):
384         m_name(name),
385         m_size(size),
386         m_itemdef(itemdef)
387 {
388         clearItems();
389 }
390
391 InventoryList::~InventoryList()
392 {
393 }
394
395 void InventoryList::clearItems()
396 {
397         m_items.clear();
398
399         for(u32 i=0; i<m_size; i++)
400         {
401                 m_items.push_back(ItemStack());
402         }
403
404         //setDirty(true);
405 }
406
407 void InventoryList::setSize(u32 newsize)
408 {
409         if(newsize != m_items.size())
410                 m_items.resize(newsize);
411         m_size = newsize;
412 }
413
414 void InventoryList::setWidth(u32 newwidth)
415 {
416         m_width = newwidth;
417 }
418
419 void InventoryList::setName(const std::string &name)
420 {
421         m_name = name;
422 }
423
424 void InventoryList::serialize(std::ostream &os) const
425 {
426         //os.imbue(std::locale("C"));
427
428         os<<"Width "<<m_width<<"\n";
429
430         for(u32 i=0; i<m_items.size(); i++)
431         {
432                 const ItemStack &item = m_items[i];
433                 if(item.empty())
434                 {
435                         os<<"Empty";
436                 }
437                 else
438                 {
439                         os<<"Item ";
440                         item.serialize(os);
441                 }
442                 os<<"\n";
443         }
444
445         os<<"EndInventoryList\n";
446 }
447
448 void InventoryList::deSerialize(std::istream &is)
449 {
450         //is.imbue(std::locale("C"));
451
452         clearItems();
453         u32 item_i = 0;
454         m_width = 0;
455
456         for(;;)
457         {
458                 std::string line;
459                 std::getline(is, line, '\n');
460
461                 std::istringstream iss(line);
462                 //iss.imbue(std::locale("C"));
463
464                 std::string name;
465                 std::getline(iss, name, ' ');
466
467                 if(name == "EndInventoryList")
468                 {
469                         break;
470                 }
471                 // This is a temporary backwards compatibility fix
472                 else if(name == "end")
473                 {
474                         break;
475                 }
476                 else if(name == "Width")
477                 {
478                         iss >> m_width;
479                         if (iss.fail())
480                                 throw SerializationError("incorrect width property");
481                 }
482                 else if(name == "Item")
483                 {
484                         if(item_i > getSize() - 1)
485                                 throw SerializationError("too many items");
486                         ItemStack item;
487                         item.deSerialize(iss, m_itemdef);
488                         m_items[item_i++] = item;
489                 }
490                 else if(name == "Empty")
491                 {
492                         if(item_i > getSize() - 1)
493                                 throw SerializationError("too many items");
494                         m_items[item_i++].clear();
495                 }
496         }
497 }
498
499 InventoryList::InventoryList(const InventoryList &other)
500 {
501         *this = other;
502 }
503
504 InventoryList & InventoryList::operator = (const InventoryList &other)
505 {
506         m_items = other.m_items;
507         m_size = other.m_size;
508         m_width = other.m_width;
509         m_name = other.m_name;
510         m_itemdef = other.m_itemdef;
511         //setDirty(true);
512
513         return *this;
514 }
515
516 bool InventoryList::operator == (const InventoryList &other) const
517 {
518         if(m_size != other.m_size)
519                 return false;
520         if(m_width != other.m_width)
521                 return false;
522         if(m_name != other.m_name)
523                 return false;
524         for(u32 i=0; i<m_items.size(); i++)
525         {
526                 ItemStack s1 = m_items[i];
527                 ItemStack s2 = other.m_items[i];
528                 if(s1.name != s2.name || s1.wear!= s2.wear || s1.count != s2.count ||
529                                 s1.metadata != s2.metadata)
530                         return false;
531         }
532
533         return true;
534 }
535
536 const std::string &InventoryList::getName() const
537 {
538         return m_name;
539 }
540
541 u32 InventoryList::getSize() const
542 {
543         return m_items.size();
544 }
545
546 u32 InventoryList::getWidth() const
547 {
548         return m_width;
549 }
550
551 u32 InventoryList::getUsedSlots() const
552 {
553         u32 num = 0;
554         for(u32 i=0; i<m_items.size(); i++)
555         {
556                 if(!m_items[i].empty())
557                         num++;
558         }
559         return num;
560 }
561
562 u32 InventoryList::getFreeSlots() const
563 {
564         return getSize() - getUsedSlots();
565 }
566
567 const ItemStack& InventoryList::getItem(u32 i) const
568 {
569         assert(i < m_size); // Pre-condition
570         return m_items[i];
571 }
572
573 ItemStack& InventoryList::getItem(u32 i)
574 {
575         assert(i < m_size); // Pre-condition
576         return m_items[i];
577 }
578
579 ItemStack InventoryList::changeItem(u32 i, const ItemStack &newitem)
580 {
581         if(i >= m_items.size())
582                 return newitem;
583
584         ItemStack olditem = m_items[i];
585         m_items[i] = newitem;
586         //setDirty(true);
587         return olditem;
588 }
589
590 void InventoryList::deleteItem(u32 i)
591 {
592         assert(i < m_items.size()); // Pre-condition
593         m_items[i].clear();
594 }
595
596 ItemStack InventoryList::addItem(const ItemStack &newitem_)
597 {
598         ItemStack newitem = newitem_;
599
600         if(newitem.empty())
601                 return newitem;
602
603         /*
604                 First try to find if it could be added to some existing items
605         */
606         for(u32 i=0; i<m_items.size(); i++)
607         {
608                 // Ignore empty slots
609                 if(m_items[i].empty())
610                         continue;
611                 // Try adding
612                 newitem = addItem(i, newitem);
613                 if(newitem.empty())
614                         return newitem; // All was eaten
615         }
616
617         /*
618                 Then try to add it to empty slots
619         */
620         for(u32 i=0; i<m_items.size(); i++)
621         {
622                 // Ignore unempty slots
623                 if(!m_items[i].empty())
624                         continue;
625                 // Try adding
626                 newitem = addItem(i, newitem);
627                 if(newitem.empty())
628                         return newitem; // All was eaten
629         }
630
631         // Return leftover
632         return newitem;
633 }
634
635 ItemStack InventoryList::addItem(u32 i, const ItemStack &newitem)
636 {
637         if(i >= m_items.size())
638                 return newitem;
639
640         ItemStack leftover = m_items[i].addItem(newitem, m_itemdef);
641         //if(leftover != newitem)
642         //      setDirty(true);
643         return leftover;
644 }
645
646 bool InventoryList::itemFits(const u32 i, const ItemStack &newitem,
647                 ItemStack *restitem) const
648 {
649         if(i >= m_items.size())
650         {
651                 if(restitem)
652                         *restitem = newitem;
653                 return false;
654         }
655
656         return m_items[i].itemFits(newitem, restitem, m_itemdef);
657 }
658
659 bool InventoryList::roomForItem(const ItemStack &item_) const
660 {
661         ItemStack item = item_;
662         ItemStack leftover;
663         for(u32 i=0; i<m_items.size(); i++)
664         {
665                 if(itemFits(i, item, &leftover))
666                         return true;
667                 item = leftover;
668         }
669         return false;
670 }
671
672 bool InventoryList::containsItem(const ItemStack &item, bool match_meta) const
673 {
674         u32 count = item.count;
675         if(count == 0)
676                 return true;
677         for(std::vector<ItemStack>::const_reverse_iterator
678                         i = m_items.rbegin();
679                         i != m_items.rend(); ++i)
680         {
681                 if(count == 0)
682                         break;
683                 if (i->name == item.name
684                                 && (!match_meta || (i->metadata == item.metadata))) {
685                         if (i->count >= count)
686                                 return true;
687                         else
688                                 count -= i->count;
689                 }
690         }
691         return false;
692 }
693
694 ItemStack InventoryList::removeItem(const ItemStack &item)
695 {
696         ItemStack removed;
697         for(std::vector<ItemStack>::reverse_iterator
698                         i = m_items.rbegin();
699                         i != m_items.rend(); ++i)
700         {
701                 if(i->name == item.name)
702                 {
703                         u32 still_to_remove = item.count - removed.count;
704                         removed.addItem(i->takeItem(still_to_remove), m_itemdef);
705                         if(removed.count == item.count)
706                                 break;
707                 }
708         }
709         return removed;
710 }
711
712 ItemStack InventoryList::takeItem(u32 i, u32 takecount)
713 {
714         if(i >= m_items.size())
715                 return ItemStack();
716
717         ItemStack taken = m_items[i].takeItem(takecount);
718         //if(!taken.empty())
719         //      setDirty(true);
720         return taken;
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