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