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