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