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