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