Merge remote-tracking branch 'kahrl/dissector'
[oweals/minetest.git] / src / inventory.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 /*
21 (c) 2010 Perttu Ahola <celeron55@gmail.com>
22 */
23
24 #include "inventory.h"
25 #include "serialization.h"
26 #include "utility.h"
27 #include "debug.h"
28 #include <sstream>
29 #include "main.h"
30 #include "serverobject.h"
31 #include "content_mapnode.h"
32 #include "content_inventory.h"
33 #include "content_sao.h"
34 #include "player.h"
35
36 /*
37         InventoryItem
38 */
39
40 InventoryItem::InventoryItem(u16 count)
41 {
42         m_count = count;
43 }
44
45 InventoryItem::~InventoryItem()
46 {
47 }
48
49 content_t content_translate_from_19_to_internal(content_t c_from)
50 {
51         for(u32 i=0; i<sizeof(trans_table_19)/sizeof(trans_table_19[0]); i++)
52         {
53                 if(trans_table_19[i][1] == c_from)
54                 {
55                         return trans_table_19[i][0];
56                 }
57         }
58         return c_from;
59 }
60
61 InventoryItem* InventoryItem::deSerialize(std::istream &is)
62 {
63         DSTACK(__FUNCTION_NAME);
64
65         //is.imbue(std::locale("C"));
66         // Read name
67         std::string name;
68         std::getline(is, name, ' ');
69         
70         if(name == "MaterialItem")
71         {
72                 // u16 reads directly as a number (u8 doesn't)
73                 u16 material;
74                 is>>material;
75                 u16 count;
76                 is>>count;
77                 // Convert old materials
78                 if(material <= 0xff)
79                 {
80                         material = content_translate_from_19_to_internal(material);
81                 }
82                 if(material > MAX_CONTENT)
83                         throw SerializationError("Too large material number");
84                 return new MaterialItem(material, count);
85         }
86         else if(name == "MaterialItem2")
87         {
88                 u16 material;
89                 is>>material;
90                 u16 count;
91                 is>>count;
92                 if(material > MAX_CONTENT)
93                         throw SerializationError("Too large material number");
94                 return new MaterialItem(material, count);
95         }
96         else if(name == "MBOItem")
97         {
98                 std::string inventorystring;
99                 std::getline(is, inventorystring, '|');
100                 return new MapBlockObjectItem(inventorystring);
101         }
102         else if(name == "CraftItem")
103         {
104                 std::string subname;
105                 std::getline(is, subname, ' ');
106                 u16 count;
107                 is>>count;
108                 return new CraftItem(subname, count);
109         }
110         else if(name == "ToolItem")
111         {
112                 std::string toolname;
113                 std::getline(is, toolname, ' ');
114                 u16 wear;
115                 is>>wear;
116                 return new ToolItem(toolname, wear);
117         }
118         else
119         {
120                 dstream<<"Unknown InventoryItem name=\""<<name<<"\""<<std::endl;
121                 throw SerializationError("Unknown InventoryItem name");
122         }
123 }
124
125 ServerActiveObject* InventoryItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
126 {
127         /*
128                 Create an ItemSAO
129         */
130         // Get item string
131         std::ostringstream os(std::ios_base::binary);
132         serialize(os);
133         // Create object
134         ServerActiveObject *obj = new ItemSAO(env, 0, pos, os.str());
135         return obj;
136 }
137
138 /*
139         MaterialItem
140 */
141
142 bool MaterialItem::isCookable() const
143 {
144         return item_material_is_cookable(m_content);
145 }
146
147 InventoryItem *MaterialItem::createCookResult() const
148 {
149         return item_material_create_cook_result(m_content);
150 }
151
152 /*
153         CraftItem
154 */
155
156 #ifndef SERVER
157 video::ITexture * CraftItem::getImage()
158 {
159         if(g_texturesource == NULL)
160                 return NULL;
161         
162         std::string name = item_craft_get_image_name(m_subname);
163
164         // Get such a texture
165         return g_texturesource->getTextureRaw(name);
166 }
167 #endif
168
169 ServerActiveObject* CraftItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
170 {
171         // Special cases
172         ServerActiveObject *obj = item_craft_create_object(m_subname, env, id, pos);
173         if(obj)
174                 return obj;
175         // Default
176         return InventoryItem::createSAO(env, id, pos);
177 }
178
179 u16 CraftItem::getDropCount() const
180 {
181         // Special cases
182         s16 dc = item_craft_get_drop_count(m_subname);
183         if(dc != -1)
184                 return dc;
185         // Default
186         return InventoryItem::getDropCount();
187 }
188
189 bool CraftItem::isCookable() const
190 {
191         return item_craft_is_cookable(m_subname);
192 }
193
194 InventoryItem *CraftItem::createCookResult() const
195 {
196         return item_craft_create_cook_result(m_subname);
197 }
198
199 bool CraftItem::use(ServerEnvironment *env, Player *player)
200 {
201         if(item_craft_is_eatable(m_subname))
202         {
203                 s16 hp_change = item_craft_eat_hp_change(m_subname);
204                 if(player->hp + hp_change > 20)
205                         player->hp = 20;
206                 else
207                         player->hp += hp_change;
208                 return true;
209         }
210         return false;
211 }
212
213 /*
214         MapBlockObjectItem DEPRECATED
215         TODO: Remove
216 */
217 #ifndef SERVER
218 video::ITexture * MapBlockObjectItem::getImage()
219 {
220         if(m_inventorystring.substr(0,3) == "Rat")
221                 return g_texturesource->getTextureRaw("rat.png");
222         
223         if(m_inventorystring.substr(0,4) == "Sign")
224                 return g_texturesource->getTextureRaw("sign.png");
225
226         return NULL;
227 }
228 #endif
229 std::string MapBlockObjectItem::getText()
230 {
231         if(m_inventorystring.substr(0,3) == "Rat")
232                 return "";
233         
234         if(m_inventorystring.substr(0,4) == "Sign")
235                 return "";
236
237         return "obj";
238 }
239
240 MapBlockObject * MapBlockObjectItem::createObject
241                 (v3f pos, f32 player_yaw, f32 player_pitch)
242 {
243         std::istringstream is(m_inventorystring);
244         std::string name;
245         std::getline(is, name, ' ');
246         
247         if(name == "None")
248         {
249                 return NULL;
250         }
251         else if(name == "Sign")
252         {
253                 std::string text;
254                 std::getline(is, text, '|');
255                 SignObject *obj = new SignObject(NULL, -1, pos);
256                 obj->setText(text);
257                 obj->setYaw(-player_yaw);
258                 return obj;
259         }
260         else if(name == "Rat")
261         {
262                 RatObject *obj = new RatObject(NULL, -1, pos);
263                 return obj;
264         }
265         else if(name == "ItemObj")
266         {
267                 /*
268                         Now we are an inventory item containing the serialization
269                         string of an object that contains the serialization
270                         string of an inventory item. Fuck this.
271                 */
272                 //assert(0);
273                 dstream<<__FUNCTION_NAME<<": WARNING: Ignoring ItemObj "
274                                 <<"because an item-object should never be inside "
275                                 <<"an object-item."<<std::endl;
276                 return NULL;
277         }
278         else
279         {
280                 return NULL;
281         }
282 }
283
284 /*
285         Inventory
286 */
287
288 InventoryList::InventoryList(std::string name, u32 size)
289 {
290         m_name = name;
291         m_size = size;
292         clearItems();
293         //m_dirty = false;
294 }
295
296 InventoryList::~InventoryList()
297 {
298         for(u32 i=0; i<m_items.size(); i++)
299         {
300                 if(m_items[i])
301                         delete m_items[i];
302         }
303 }
304
305 void InventoryList::clearItems()
306 {
307         for(u32 i=0; i<m_items.size(); i++)
308         {
309                 if(m_items[i])
310                         delete m_items[i];
311         }
312
313         m_items.clear();
314
315         for(u32 i=0; i<m_size; i++)
316         {
317                 m_items.push_back(NULL);
318         }
319
320         //setDirty(true);
321 }
322
323 void InventoryList::serialize(std::ostream &os) const
324 {
325         //os.imbue(std::locale("C"));
326         
327         for(u32 i=0; i<m_items.size(); i++)
328         {
329                 InventoryItem *item = m_items[i];
330                 if(item != NULL)
331                 {
332                         os<<"Item ";
333                         item->serialize(os);
334                 }
335                 else
336                 {
337                         os<<"Empty";
338                 }
339                 os<<"\n";
340         }
341
342         os<<"EndInventoryList\n";
343 }
344
345 void InventoryList::deSerialize(std::istream &is)
346 {
347         //is.imbue(std::locale("C"));
348
349         clearItems();
350         u32 item_i = 0;
351
352         for(;;)
353         {
354                 std::string line;
355                 std::getline(is, line, '\n');
356
357                 std::istringstream iss(line);
358                 //iss.imbue(std::locale("C"));
359
360                 std::string name;
361                 std::getline(iss, name, ' ');
362
363                 if(name == "EndInventoryList")
364                 {
365                         break;
366                 }
367                 // This is a temporary backwards compatibility fix
368                 else if(name == "end")
369                 {
370                         break;
371                 }
372                 else if(name == "Item")
373                 {
374                         if(item_i > getSize() - 1)
375                                 throw SerializationError("too many items");
376                         InventoryItem *item = InventoryItem::deSerialize(iss);
377                         m_items[item_i++] = item;
378                 }
379                 else if(name == "Empty")
380                 {
381                         if(item_i > getSize() - 1)
382                                 throw SerializationError("too many items");
383                         m_items[item_i++] = NULL;
384                 }
385                 else
386                 {
387                         throw SerializationError("Unknown inventory identifier");
388                 }
389         }
390 }
391
392 InventoryList::InventoryList(const InventoryList &other)
393 {
394         /*
395                 Do this so that the items get cloned. Otherwise the pointers
396                 in the array will just get copied.
397         */
398         *this = other;
399 }
400
401 InventoryList & InventoryList::operator = (const InventoryList &other)
402 {
403         m_name = other.m_name;
404         m_size = other.m_size;
405         clearItems();
406         for(u32 i=0; i<other.m_items.size(); i++)
407         {
408                 InventoryItem *item = other.m_items[i];
409                 if(item != NULL)
410                 {
411                         m_items[i] = item->clone();
412                 }
413         }
414         //setDirty(true);
415
416         return *this;
417 }
418
419 const std::string &InventoryList::getName() const
420 {
421         return m_name;
422 }
423
424 u32 InventoryList::getSize()
425 {
426         return m_items.size();
427 }
428
429 u32 InventoryList::getUsedSlots()
430 {
431         u32 num = 0;
432         for(u32 i=0; i<m_items.size(); i++)
433         {
434                 InventoryItem *item = m_items[i];
435                 if(item != NULL)
436                         num++;
437         }
438         return num;
439 }
440
441 u32 InventoryList::getFreeSlots()
442 {
443         return getSize() - getUsedSlots();
444 }
445
446 const InventoryItem * InventoryList::getItem(u32 i) const
447 {
448         if(i > m_items.size() - 1)
449                 return NULL;
450         return m_items[i];
451 }
452
453 InventoryItem * InventoryList::getItem(u32 i)
454 {
455         if(i > m_items.size() - 1)
456                 return NULL;
457         return m_items[i];
458 }
459
460 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
461 {
462         assert(i < m_items.size());
463
464         InventoryItem *olditem = m_items[i];
465         m_items[i] = newitem;
466         //setDirty(true);
467         return olditem;
468 }
469
470 void InventoryList::deleteItem(u32 i)
471 {
472         assert(i < m_items.size());
473         InventoryItem *item = changeItem(i, NULL);
474         if(item)
475                 delete item;
476 }
477
478 InventoryItem * InventoryList::addItem(InventoryItem *newitem)
479 {
480         if(newitem == NULL)
481                 return NULL;
482         
483         /*
484                 First try to find if it could be added to some existing items
485         */
486         for(u32 i=0; i<m_items.size(); i++)
487         {
488                 // Ignore empty slots
489                 if(m_items[i] == NULL)
490                         continue;
491                 // Try adding
492                 newitem = addItem(i, newitem);
493                 if(newitem == NULL)
494                         return NULL; // All was eaten
495         }
496
497         /*
498                 Then try to add it to empty slots
499         */
500         for(u32 i=0; i<m_items.size(); i++)
501         {
502                 // Ignore unempty slots
503                 if(m_items[i] != NULL)
504                         continue;
505                 // Try adding
506                 newitem = addItem(i, newitem);
507                 if(newitem == NULL)
508                         return NULL; // All was eaten
509         }
510
511         // Return leftover
512         return newitem;
513 }
514
515 InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
516 {
517         if(newitem == NULL)
518                 return NULL;
519         
520         //setDirty(true);
521         
522         // If it is an empty position, it's an easy job.
523         InventoryItem *to_item = getItem(i);
524         if(to_item == NULL)
525         {
526                 m_items[i] = newitem;
527                 return NULL;
528         }
529         
530         // If not addable, return the item
531         if(newitem->addableTo(to_item) == false)
532                 return newitem;
533         
534         // If the item fits fully in the slot, add counter and delete it
535         if(newitem->getCount() <= to_item->freeSpace())
536         {
537                 to_item->add(newitem->getCount());
538                 delete newitem;
539                 return NULL;
540         }
541         // Else the item does not fit fully. Add all that fits and return
542         // the rest.
543         else
544         {
545                 u16 freespace = to_item->freeSpace();
546                 to_item->add(freespace);
547                 newitem->remove(freespace);
548                 return newitem;
549         }
550 }
551
552 bool InventoryList::itemFits(const u32 i, const InventoryItem *newitem)
553 {
554         // If it is an empty position, it's an easy job.
555         const InventoryItem *to_item = getItem(i);
556         if(to_item == NULL)
557         {
558                 return true;
559         }
560         
561         // If not addable, fail
562         if(newitem->addableTo(to_item) == false)
563                 return false;
564         
565         // If the item fits fully in the slot, pass
566         if(newitem->getCount() <= to_item->freeSpace())
567         {
568                 return true;
569         }
570
571         return false;
572 }
573
574 bool InventoryList::roomForItem(const InventoryItem *item)
575 {
576         for(u32 i=0; i<m_items.size(); i++)
577                 if(itemFits(i, item))
578                         return true;
579         return false;
580 }
581
582 bool InventoryList::roomForCookedItem(const InventoryItem *item)
583 {
584         const InventoryItem *cook = item->createCookResult();
585         if(!cook)
586                 return false;
587         bool room = roomForItem(cook);
588         delete cook;
589         return room;
590 }
591
592 InventoryItem * InventoryList::takeItem(u32 i, u32 count)
593 {
594         if(count == 0)
595                 return NULL;
596         
597         //setDirty(true);
598
599         InventoryItem *item = getItem(i);
600         // If it is an empty position, return NULL
601         if(item == NULL)
602                 return NULL;
603         
604         if(count >= item->getCount())
605         {
606                 // Get the item by swapping NULL to its place
607                 return changeItem(i, NULL);
608         }
609         else
610         {
611                 InventoryItem *item2 = item->clone();
612                 item->remove(count);
613                 item2->setCount(count);
614                 return item2;
615         }
616         
617         return false;
618 }
619
620 void InventoryList::decrementMaterials(u16 count)
621 {
622         for(u32 i=0; i<m_items.size(); i++)
623         {
624                 InventoryItem *item = takeItem(i, count);
625                 if(item)
626                         delete item;
627         }
628 }
629
630 void InventoryList::print(std::ostream &o)
631 {
632         o<<"InventoryList:"<<std::endl;
633         for(u32 i=0; i<m_items.size(); i++)
634         {
635                 InventoryItem *item = m_items[i];
636                 if(item != NULL)
637                 {
638                         o<<i<<": ";
639                         item->serialize(o);
640                         o<<"\n";
641                 }
642         }
643 }
644
645 /*
646         Inventory
647 */
648
649 Inventory::~Inventory()
650 {
651         clear();
652 }
653
654 void Inventory::clear()
655 {
656         for(u32 i=0; i<m_lists.size(); i++)
657         {
658                 delete m_lists[i];
659         }
660         m_lists.clear();
661 }
662
663 Inventory::Inventory()
664 {
665 }
666
667 Inventory::Inventory(const Inventory &other)
668 {
669         *this = other;
670 }
671
672 Inventory & Inventory::operator = (const Inventory &other)
673 {
674         clear();
675         for(u32 i=0; i<other.m_lists.size(); i++)
676         {
677                 m_lists.push_back(new InventoryList(*other.m_lists[i]));
678         }
679         return *this;
680 }
681
682 void Inventory::serialize(std::ostream &os) const
683 {
684         for(u32 i=0; i<m_lists.size(); i++)
685         {
686                 InventoryList *list = m_lists[i];
687                 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
688                 list->serialize(os);
689         }
690
691         os<<"EndInventory\n";
692 }
693
694 void Inventory::deSerialize(std::istream &is)
695 {
696         clear();
697
698         for(;;)
699         {
700                 std::string line;
701                 std::getline(is, line, '\n');
702
703                 std::istringstream iss(line);
704
705                 std::string name;
706                 std::getline(iss, name, ' ');
707
708                 if(name == "EndInventory")
709                 {
710                         break;
711                 }
712                 // This is a temporary backwards compatibility fix
713                 else if(name == "end")
714                 {
715                         break;
716                 }
717                 else if(name == "List")
718                 {
719                         std::string listname;
720                         u32 listsize;
721
722                         std::getline(iss, listname, ' ');
723                         iss>>listsize;
724
725                         InventoryList *list = new InventoryList(listname, listsize);
726                         list->deSerialize(is);
727
728                         m_lists.push_back(list);
729                 }
730                 else
731                 {
732                         throw SerializationError("Unknown inventory identifier");
733                 }
734         }
735 }
736
737 InventoryList * Inventory::addList(const std::string &name, u32 size)
738 {
739         s32 i = getListIndex(name);
740         if(i != -1)
741         {
742                 if(m_lists[i]->getSize() != size)
743                 {
744                         delete m_lists[i];
745                         m_lists[i] = new InventoryList(name, size);
746                 }
747                 return m_lists[i];
748         }
749         else
750         {
751                 m_lists.push_back(new InventoryList(name, size));
752                 return m_lists.getLast();
753         }
754 }
755
756 InventoryList * Inventory::getList(const std::string &name)
757 {
758         s32 i = getListIndex(name);
759         if(i == -1)
760                 return NULL;
761         return m_lists[i];
762 }
763
764 const InventoryList * Inventory::getList(const std::string &name) const
765 {
766         s32 i = getListIndex(name);
767         if(i == -1)
768                 return NULL;
769         return m_lists[i];
770 }
771
772 const s32 Inventory::getListIndex(const std::string &name) const
773 {
774         for(u32 i=0; i<m_lists.size(); i++)
775         {
776                 if(m_lists[i]->getName() == name)
777                         return i;
778         }
779         return -1;
780 }
781
782 /*
783         InventoryAction
784 */
785
786 InventoryAction * InventoryAction::deSerialize(std::istream &is)
787 {
788         std::string type;
789         std::getline(is, type, ' ');
790
791         InventoryAction *a = NULL;
792
793         if(type == "Move")
794         {
795                 a = new IMoveAction(is);
796         }
797
798         return a;
799 }
800
801 void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr)
802 {
803 #if 1
804
805         /*dstream<<"from_inv="<<from_inv<<" to_inv="<<to_inv<<std::endl;
806         dstream<<"from_list="<<from_list<<" to_list="<<to_list<<std::endl;
807         dstream<<"from_i="<<from_i<<" to_i="<<to_i<<std::endl;*/
808
809         Inventory *inv_from = mgr->getInventory(c, from_inv);
810         Inventory *inv_to = mgr->getInventory(c, to_inv);
811
812         if(!inv_from || !inv_to)
813         {
814                 dstream<<__FUNCTION_NAME<<": Operation not allowed "
815                                 <<"(inventories not found)"<<std::endl;
816                 return;
817         }
818
819         InventoryList *list_from = inv_from->getList(from_list);
820         InventoryList *list_to = inv_to->getList(to_list);
821
822         /*dstream<<"list_from="<<list_from<<" list_to="<<list_to
823                         <<std::endl;*/
824         /*if(list_from)
825                 dstream<<" list_from->getItem(from_i)="<<list_from->getItem(from_i)
826                                 <<std::endl;
827         if(list_to)
828                 dstream<<" list_to->getItem(to_i)="<<list_to->getItem(to_i)
829                                 <<std::endl;*/
830         
831         /*
832                 If a list doesn't exist or the source item doesn't exist
833         */
834         if(!list_from || !list_to)
835         {
836                 dstream<<__FUNCTION_NAME<<": Operation not allowed "
837                                 <<"(a list doesn't exist)"
838                                 <<std::endl;
839                 return;
840         }
841         if(list_from->getItem(from_i) == NULL)
842         {
843                 dstream<<__FUNCTION_NAME<<": Operation not allowed "
844                                 <<"(the source item doesn't exist)"
845                                 <<std::endl;
846                 return;
847         }
848         /*
849                 If the source and the destination slots are the same
850         */
851         if(inv_from == inv_to && list_from == list_to && from_i == to_i)
852         {
853                 dstream<<__FUNCTION_NAME<<": Operation not allowed "
854                                 <<"(source and the destination slots are the same)"<<std::endl;
855                 return;
856         }
857         
858         // Take item from source list
859         InventoryItem *item1 = NULL;
860         if(count == 0)
861                 item1 = list_from->changeItem(from_i, NULL);
862         else
863                 item1 = list_from->takeItem(from_i, count);
864
865         // Try to add the item to destination list
866         InventoryItem *olditem = item1;
867         item1 = list_to->addItem(to_i, item1);
868
869         // If something is returned, the item was not fully added
870         if(item1 != NULL)
871         {
872                 // If olditem is returned, nothing was added.
873                 bool nothing_added = (item1 == olditem);
874                 
875                 // If something else is returned, part of the item was left unadded.
876                 // Add the other part back to the source item
877                 list_from->addItem(from_i, item1);
878
879                 // If olditem is returned, nothing was added.
880                 // Swap the items
881                 if(nothing_added)
882                 {
883                         // Take item from source list
884                         item1 = list_from->changeItem(from_i, NULL);
885                         // Adding was not possible, swap the items.
886                         InventoryItem *item2 = list_to->changeItem(to_i, item1);
887                         // Put item from destination list to the source list
888                         list_from->changeItem(from_i, item2);
889                 }
890         }
891
892         mgr->inventoryModified(c, from_inv);
893         if(from_inv != to_inv)
894                 mgr->inventoryModified(c, to_inv);
895 #endif
896 }
897
898 /*
899         Craft checking system
900 */
901
902 bool ItemSpec::checkItem(const InventoryItem *item) const
903 {
904         if(type == ITEM_NONE)
905         {
906                 // Has to be no item
907                 if(item != NULL)
908                         return false;
909                 return true;
910         }
911         
912         // There should be an item
913         if(item == NULL)
914                 return false;
915
916         std::string itemname = item->getName();
917
918         if(type == ITEM_MATERIAL)
919         {
920                 if(itemname != "MaterialItem")
921                         return false;
922                 MaterialItem *mitem = (MaterialItem*)item;
923                 if(mitem->getMaterial() != num)
924                         return false;
925         }
926         else if(type == ITEM_CRAFT)
927         {
928                 if(itemname != "CraftItem")
929                         return false;
930                 CraftItem *mitem = (CraftItem*)item;
931                 if(mitem->getSubName() != name)
932                         return false;
933         }
934         else if(type == ITEM_TOOL)
935         {
936                 // Not supported yet
937                 assert(0);
938         }
939         else if(type == ITEM_MBO)
940         {
941                 // Not supported yet
942                 assert(0);
943         }
944         else
945         {
946                 // Not supported yet
947                 assert(0);
948         }
949         return true;
950 }
951
952 bool checkItemCombination(InventoryItem const * const *items, const ItemSpec *specs)
953 {
954         u16 items_min_x = 100;
955         u16 items_max_x = 100;
956         u16 items_min_y = 100;
957         u16 items_max_y = 100;
958         for(u16 y=0; y<3; y++)
959         for(u16 x=0; x<3; x++)
960         {
961                 if(items[y*3 + x] == NULL)
962                         continue;
963                 if(items_min_x == 100 || x < items_min_x)
964                         items_min_x = x;
965                 if(items_min_y == 100 || y < items_min_y)
966                         items_min_y = y;
967                 if(items_max_x == 100 || x > items_max_x)
968                         items_max_x = x;
969                 if(items_max_y == 100 || y > items_max_y)
970                         items_max_y = y;
971         }
972         // No items at all, just return false
973         if(items_min_x == 100)
974                 return false;
975         
976         u16 items_w = items_max_x - items_min_x + 1;
977         u16 items_h = items_max_y - items_min_y + 1;
978
979         u16 specs_min_x = 100;
980         u16 specs_max_x = 100;
981         u16 specs_min_y = 100;
982         u16 specs_max_y = 100;
983         for(u16 y=0; y<3; y++)
984         for(u16 x=0; x<3; x++)
985         {
986                 if(specs[y*3 + x].type == ITEM_NONE)
987                         continue;
988                 if(specs_min_x == 100 || x < specs_min_x)
989                         specs_min_x = x;
990                 if(specs_min_y == 100 || y < specs_min_y)
991                         specs_min_y = y;
992                 if(specs_max_x == 100 || x > specs_max_x)
993                         specs_max_x = x;
994                 if(specs_max_y == 100 || y > specs_max_y)
995                         specs_max_y = y;
996         }
997         // No specs at all, just return false
998         if(specs_min_x == 100)
999                 return false;
1000
1001         u16 specs_w = specs_max_x - specs_min_x + 1;
1002         u16 specs_h = specs_max_y - specs_min_y + 1;
1003
1004         // Different sizes
1005         if(items_w != specs_w || items_h != specs_h)
1006                 return false;
1007
1008         for(u16 y=0; y<specs_h; y++)
1009         for(u16 x=0; x<specs_w; x++)
1010         {
1011                 u16 items_x = items_min_x + x;
1012                 u16 items_y = items_min_y + y;
1013                 u16 specs_x = specs_min_x + x;
1014                 u16 specs_y = specs_min_y + y;
1015                 const InventoryItem *item = items[items_y * 3 + items_x];
1016                 const ItemSpec &spec = specs[specs_y * 3 + specs_x];
1017
1018                 if(spec.checkItem(item) == false)
1019                         return false;
1020         }
1021
1022         return true;
1023 }
1024         
1025 //END