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