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