Automatic item and node colorization (#5640)
[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(const ItemStack &newitem_,
258                 IItemDefManager *itemdef)
259 {
260         ItemStack newitem = newitem_;
261
262         // If the item is empty or the position invalid, bail out
263         if(newitem.empty())
264         {
265                 // nothing can be added trivially
266         }
267         // If this is an empty item, it's an easy job.
268         else if(empty())
269         {
270                 *this = newitem;
271                 newitem.clear();
272         }
273         // If item name or metadata differs, bail out
274         else if (name != newitem.name
275                 || metadata != newitem.metadata)
276         {
277                 // cannot be added
278         }
279         // If the item fits fully, add counter and delete it
280         else if(newitem.count <= freeSpace(itemdef))
281         {
282                 add(newitem.count);
283                 newitem.clear();
284         }
285         // Else the item does not fit fully. Add all that fits and return
286         // the rest.
287         else
288         {
289                 u16 freespace = freeSpace(itemdef);
290                 add(freespace);
291                 newitem.remove(freespace);
292         }
293
294         return newitem;
295 }
296
297 bool ItemStack::itemFits(const ItemStack &newitem_,
298                 ItemStack *restitem,
299                 IItemDefManager *itemdef) const
300 {
301         ItemStack newitem = newitem_;
302
303         // If the item is empty or the position invalid, bail out
304         if(newitem.empty())
305         {
306                 // nothing can be added trivially
307         }
308         // If this is an empty item, it's an easy job.
309         else if(empty())
310         {
311                 newitem.clear();
312         }
313         // If item name or metadata differs, bail out
314         else if (name != newitem.name
315                 || metadata != newitem.metadata)
316         {
317                 // cannot be added
318         }
319         // If the item fits fully, delete it
320         else if(newitem.count <= freeSpace(itemdef))
321         {
322                 newitem.clear();
323         }
324         // Else the item does not fit fully. Return the rest.
325         // the rest.
326         else
327         {
328                 u16 freespace = freeSpace(itemdef);
329                 newitem.remove(freespace);
330         }
331
332         if(restitem)
333                 *restitem = newitem;
334         return newitem.empty();
335 }
336
337 ItemStack ItemStack::takeItem(u32 takecount)
338 {
339         if(takecount == 0 || count == 0)
340                 return ItemStack();
341
342         ItemStack result = *this;
343         if(takecount >= count)
344         {
345                 // Take all
346                 clear();
347         }
348         else
349         {
350                 // Take part
351                 remove(takecount);
352                 result.count = takecount;
353         }
354         return result;
355 }
356
357 ItemStack ItemStack::peekItem(u32 peekcount) const
358 {
359         if(peekcount == 0 || count == 0)
360                 return ItemStack();
361
362         ItemStack result = *this;
363         if(peekcount < count)
364                 result.count = peekcount;
365         return result;
366 }
367
368 /*
369         Inventory
370 */
371
372 InventoryList::InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef):
373         m_name(name),
374         m_size(size),
375         m_itemdef(itemdef)
376 {
377         clearItems();
378 }
379
380 InventoryList::~InventoryList()
381 {
382 }
383
384 void InventoryList::clearItems()
385 {
386         m_items.clear();
387
388         for(u32 i=0; i<m_size; i++)
389         {
390                 m_items.push_back(ItemStack());
391         }
392
393         //setDirty(true);
394 }
395
396 void InventoryList::setSize(u32 newsize)
397 {
398         if(newsize != m_items.size())
399                 m_items.resize(newsize);
400         m_size = newsize;
401 }
402
403 void InventoryList::setWidth(u32 newwidth)
404 {
405         m_width = newwidth;
406 }
407
408 void InventoryList::setName(const std::string &name)
409 {
410         m_name = name;
411 }
412
413 void InventoryList::serialize(std::ostream &os) const
414 {
415         //os.imbue(std::locale("C"));
416
417         os<<"Width "<<m_width<<"\n";
418
419         for(u32 i=0; i<m_items.size(); i++)
420         {
421                 const ItemStack &item = m_items[i];
422                 if(item.empty())
423                 {
424                         os<<"Empty";
425                 }
426                 else
427                 {
428                         os<<"Item ";
429                         item.serialize(os);
430                 }
431                 os<<"\n";
432         }
433
434         os<<"EndInventoryList\n";
435 }
436
437 void InventoryList::deSerialize(std::istream &is)
438 {
439         //is.imbue(std::locale("C"));
440
441         clearItems();
442         u32 item_i = 0;
443         m_width = 0;
444
445         for(;;)
446         {
447                 std::string line;
448                 std::getline(is, line, '\n');
449
450                 std::istringstream iss(line);
451                 //iss.imbue(std::locale("C"));
452
453                 std::string name;
454                 std::getline(iss, name, ' ');
455
456                 if(name == "EndInventoryList")
457                 {
458                         break;
459                 }
460                 // This is a temporary backwards compatibility fix
461                 else if(name == "end")
462                 {
463                         break;
464                 }
465                 else if(name == "Width")
466                 {
467                         iss >> m_width;
468                         if (iss.fail())
469                                 throw SerializationError("incorrect width property");
470                 }
471                 else if(name == "Item")
472                 {
473                         if(item_i > getSize() - 1)
474                                 throw SerializationError("too many items");
475                         ItemStack item;
476                         item.deSerialize(iss, m_itemdef);
477                         m_items[item_i++] = item;
478                 }
479                 else if(name == "Empty")
480                 {
481                         if(item_i > getSize() - 1)
482                                 throw SerializationError("too many items");
483                         m_items[item_i++].clear();
484                 }
485         }
486 }
487
488 InventoryList::InventoryList(const InventoryList &other)
489 {
490         *this = other;
491 }
492
493 InventoryList & InventoryList::operator = (const InventoryList &other)
494 {
495         m_items = other.m_items;
496         m_size = other.m_size;
497         m_width = other.m_width;
498         m_name = other.m_name;
499         m_itemdef = other.m_itemdef;
500         //setDirty(true);
501
502         return *this;
503 }
504
505 bool InventoryList::operator == (const InventoryList &other) const
506 {
507         if(m_size != other.m_size)
508                 return false;
509         if(m_width != other.m_width)
510                 return false;
511         if(m_name != other.m_name)
512                 return false;
513         for(u32 i=0; i<m_items.size(); i++)
514         {
515                 ItemStack s1 = m_items[i];
516                 ItemStack s2 = other.m_items[i];
517                 if(s1.name != s2.name || s1.wear!= s2.wear || s1.count != s2.count ||
518                                 s1.metadata != s2.metadata)
519                         return false;
520         }
521
522         return true;
523 }
524
525 const std::string &InventoryList::getName() const
526 {
527         return m_name;
528 }
529
530 u32 InventoryList::getSize() const
531 {
532         return m_items.size();
533 }
534
535 u32 InventoryList::getWidth() const
536 {
537         return m_width;
538 }
539
540 u32 InventoryList::getUsedSlots() const
541 {
542         u32 num = 0;
543         for(u32 i=0; i<m_items.size(); i++)
544         {
545                 if(!m_items[i].empty())
546                         num++;
547         }
548         return num;
549 }
550
551 u32 InventoryList::getFreeSlots() const
552 {
553         return getSize() - getUsedSlots();
554 }
555
556 const ItemStack& InventoryList::getItem(u32 i) const
557 {
558         assert(i < m_size); // Pre-condition
559         return m_items[i];
560 }
561
562 ItemStack& InventoryList::getItem(u32 i)
563 {
564         assert(i < m_size); // Pre-condition
565         return m_items[i];
566 }
567
568 ItemStack InventoryList::changeItem(u32 i, const ItemStack &newitem)
569 {
570         if(i >= m_items.size())
571                 return newitem;
572
573         ItemStack olditem = m_items[i];
574         m_items[i] = newitem;
575         //setDirty(true);
576         return olditem;
577 }
578
579 void InventoryList::deleteItem(u32 i)
580 {
581         assert(i < m_items.size()); // Pre-condition
582         m_items[i].clear();
583 }
584
585 ItemStack InventoryList::addItem(const ItemStack &newitem_)
586 {
587         ItemStack newitem = newitem_;
588
589         if(newitem.empty())
590                 return newitem;
591
592         /*
593                 First try to find if it could be added to some existing items
594         */
595         for(u32 i=0; i<m_items.size(); i++)
596         {
597                 // Ignore empty slots
598                 if(m_items[i].empty())
599                         continue;
600                 // Try adding
601                 newitem = addItem(i, newitem);
602                 if(newitem.empty())
603                         return newitem; // All was eaten
604         }
605
606         /*
607                 Then try to add it to empty slots
608         */
609         for(u32 i=0; i<m_items.size(); i++)
610         {
611                 // Ignore unempty slots
612                 if(!m_items[i].empty())
613                         continue;
614                 // Try adding
615                 newitem = addItem(i, newitem);
616                 if(newitem.empty())
617                         return newitem; // All was eaten
618         }
619
620         // Return leftover
621         return newitem;
622 }
623
624 ItemStack InventoryList::addItem(u32 i, const ItemStack &newitem)
625 {
626         if(i >= m_items.size())
627                 return newitem;
628
629         ItemStack leftover = m_items[i].addItem(newitem, m_itemdef);
630         //if(leftover != newitem)
631         //      setDirty(true);
632         return leftover;
633 }
634
635 bool InventoryList::itemFits(const u32 i, const ItemStack &newitem,
636                 ItemStack *restitem) const
637 {
638         if(i >= m_items.size())
639         {
640                 if(restitem)
641                         *restitem = newitem;
642                 return false;
643         }
644
645         return m_items[i].itemFits(newitem, restitem, m_itemdef);
646 }
647
648 bool InventoryList::roomForItem(const ItemStack &item_) const
649 {
650         ItemStack item = item_;
651         ItemStack leftover;
652         for(u32 i=0; i<m_items.size(); i++)
653         {
654                 if(itemFits(i, item, &leftover))
655                         return true;
656                 item = leftover;
657         }
658         return false;
659 }
660
661 bool InventoryList::containsItem(const ItemStack &item, bool match_meta) const
662 {
663         u32 count = item.count;
664         if(count == 0)
665                 return true;
666         for(std::vector<ItemStack>::const_reverse_iterator
667                         i = m_items.rbegin();
668                         i != m_items.rend(); ++i)
669         {
670                 if(count == 0)
671                         break;
672                 if (i->name == item.name
673                                 && (!match_meta || (i->metadata == item.metadata))) {
674                         if (i->count >= count)
675                                 return true;
676                         else
677                                 count -= i->count;
678                 }
679         }
680         return false;
681 }
682
683 ItemStack InventoryList::removeItem(const ItemStack &item)
684 {
685         ItemStack removed;
686         for(std::vector<ItemStack>::reverse_iterator
687                         i = m_items.rbegin();
688                         i != m_items.rend(); ++i)
689         {
690                 if(i->name == item.name)
691                 {
692                         u32 still_to_remove = item.count - removed.count;
693                         removed.addItem(i->takeItem(still_to_remove), m_itemdef);
694                         if(removed.count == item.count)
695                                 break;
696                 }
697         }
698         return removed;
699 }
700
701 ItemStack InventoryList::takeItem(u32 i, u32 takecount)
702 {
703         if(i >= m_items.size())
704                 return ItemStack();
705
706         ItemStack taken = m_items[i].takeItem(takecount);
707         //if(!taken.empty())
708         //      setDirty(true);
709         return taken;
710 }
711
712 void InventoryList::moveItemSomewhere(u32 i, InventoryList *dest, u32 count)
713 {
714         // Take item from source list
715         ItemStack item1;
716         if (count == 0)
717                 item1 = changeItem(i, ItemStack());
718         else
719                 item1 = takeItem(i, count);
720
721         if (item1.empty())
722                 return;
723
724         // Try to add the item to destination list
725         u32 dest_size = dest->getSize();
726         // First try all the non-empty slots
727         for (u32 dest_i = 0; dest_i < dest_size; dest_i++) {
728                 if (!m_items[dest_i].empty()) {
729                         item1 = dest->addItem(dest_i, item1);
730                         if (item1.empty()) return;
731                 }
732         }
733
734         // Then try all the empty ones
735         for (u32 dest_i = 0; dest_i < dest_size; dest_i++) {
736                 if (m_items[dest_i].empty()) {
737                         item1 = dest->addItem(dest_i, item1);
738                         if (item1.empty()) return;
739                 }
740         }
741
742         // If we reach this, the item was not fully added
743         // Add the remaining part back to the source item
744         addItem(i, item1);
745 }
746
747 u32 InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i,
748                 u32 count, bool swap_if_needed, bool *did_swap)
749 {
750         if(this == dest && i == dest_i)
751                 return count;
752
753         // Take item from source list
754         ItemStack item1;
755         if(count == 0)
756                 item1 = changeItem(i, ItemStack());
757         else
758                 item1 = takeItem(i, count);
759
760         if(item1.empty())
761                 return 0;
762
763         // Try to add the item to destination list
764         u32 oldcount = item1.count;
765         item1 = dest->addItem(dest_i, item1);
766
767         // If something is returned, the item was not fully added
768         if(!item1.empty())
769         {
770                 // If olditem is returned, nothing was added.
771                 bool nothing_added = (item1.count == oldcount);
772
773                 // If something else is returned, part of the item was left unadded.
774                 // Add the other part back to the source item
775                 addItem(i, item1);
776
777                 // If olditem is returned, nothing was added.
778                 // Swap the items
779                 if (nothing_added && swap_if_needed) {
780                         // Tell that we swapped
781                         if (did_swap != NULL) {
782                                 *did_swap = true;
783                         }
784                         // Take item from source list
785                         item1 = changeItem(i, ItemStack());
786                         // Adding was not possible, swap the items.
787                         ItemStack item2 = dest->changeItem(dest_i, item1);
788                         // Put item from destination list to the source list
789                         changeItem(i, item2);
790                 }
791         }
792         return (oldcount - item1.count);
793 }
794
795 /*
796         Inventory
797 */
798
799 Inventory::~Inventory()
800 {
801         clear();
802 }
803
804 void Inventory::clear()
805 {
806         m_dirty = true;
807         for(u32 i=0; i<m_lists.size(); i++)
808         {
809                 delete m_lists[i];
810         }
811         m_lists.clear();
812 }
813
814 void Inventory::clearContents()
815 {
816         m_dirty = true;
817         for(u32 i=0; i<m_lists.size(); i++)
818         {
819                 InventoryList *list = m_lists[i];
820                 for(u32 j=0; j<list->getSize(); j++)
821                 {
822                         list->deleteItem(j);
823                 }
824         }
825 }
826
827 Inventory::Inventory(IItemDefManager *itemdef)
828 {
829         m_dirty = false;
830         m_itemdef = itemdef;
831 }
832
833 Inventory::Inventory(const Inventory &other)
834 {
835         *this = other;
836         m_dirty = false;
837 }
838
839 Inventory & Inventory::operator = (const Inventory &other)
840 {
841         // Gracefully handle self assignment
842         if(this != &other)
843         {
844                 m_dirty = true;
845                 clear();
846                 m_itemdef = other.m_itemdef;
847                 for(u32 i=0; i<other.m_lists.size(); i++)
848                 {
849                         m_lists.push_back(new InventoryList(*other.m_lists[i]));
850                 }
851         }
852         return *this;
853 }
854
855 bool Inventory::operator == (const Inventory &other) const
856 {
857         if(m_lists.size() != other.m_lists.size())
858                 return false;
859
860         for(u32 i=0; i<m_lists.size(); i++)
861         {
862                 if(*m_lists[i] != *other.m_lists[i])
863                         return false;
864         }
865         return true;
866 }
867
868 void Inventory::serialize(std::ostream &os) const
869 {
870         for(u32 i=0; i<m_lists.size(); i++)
871         {
872                 InventoryList *list = m_lists[i];
873                 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
874                 list->serialize(os);
875         }
876
877         os<<"EndInventory\n";
878 }
879
880 void Inventory::deSerialize(std::istream &is)
881 {
882         clear();
883
884         for(;;)
885         {
886                 std::string line;
887                 std::getline(is, line, '\n');
888
889                 std::istringstream iss(line);
890
891                 std::string name;
892                 std::getline(iss, name, ' ');
893
894                 if(name == "EndInventory")
895                 {
896                         break;
897                 }
898                 // This is a temporary backwards compatibility fix
899                 else if(name == "end")
900                 {
901                         break;
902                 }
903                 else if(name == "List")
904                 {
905                         std::string listname;
906                         u32 listsize;
907
908                         std::getline(iss, listname, ' ');
909                         iss>>listsize;
910
911                         InventoryList *list = new InventoryList(listname, listsize, m_itemdef);
912                         list->deSerialize(is);
913
914                         m_lists.push_back(list);
915                 }
916                 else
917                 {
918                         throw SerializationError("invalid inventory specifier: " + name);
919                 }
920         }
921 }
922
923 InventoryList * Inventory::addList(const std::string &name, u32 size)
924 {
925         m_dirty = true;
926         s32 i = getListIndex(name);
927         if(i != -1)
928         {
929                 if(m_lists[i]->getSize() != size)
930                 {
931                         delete m_lists[i];
932                         m_lists[i] = new InventoryList(name, size, m_itemdef);
933                 }
934                 return m_lists[i];
935         }
936         else
937         {
938                 //don't create list with invalid name
939                 if (name.find(" ") != std::string::npos) return NULL;
940
941                 InventoryList *list = new InventoryList(name, size, m_itemdef);
942                 m_lists.push_back(list);
943                 return list;
944         }
945 }
946
947 InventoryList * Inventory::getList(const std::string &name)
948 {
949         s32 i = getListIndex(name);
950         if(i == -1)
951                 return NULL;
952         return m_lists[i];
953 }
954
955 std::vector<const InventoryList*> Inventory::getLists()
956 {
957         std::vector<const InventoryList*> lists;
958         for(u32 i=0; i<m_lists.size(); i++)
959         {
960                 InventoryList *list = m_lists[i];
961                 lists.push_back(list);
962         }
963         return lists;
964 }
965
966 bool Inventory::deleteList(const std::string &name)
967 {
968         s32 i = getListIndex(name);
969         if(i == -1)
970                 return false;
971         m_dirty = true;
972         delete m_lists[i];
973         m_lists.erase(m_lists.begin() + i);
974         return true;
975 }
976
977 const InventoryList * Inventory::getList(const std::string &name) const
978 {
979         s32 i = getListIndex(name);
980         if(i == -1)
981                 return NULL;
982         return m_lists[i];
983 }
984
985 const s32 Inventory::getListIndex(const std::string &name) const
986 {
987         for(u32 i=0; i<m_lists.size(); i++)
988         {
989                 if(m_lists[i]->getName() == name)
990                         return i;
991         }
992         return -1;
993 }
994
995 //END