]> git.lizzy.rs Git - minetest.git/blob - src/inventorymanager.cpp
Inventory manager style cleanup and further checks
[minetest.git] / src / inventorymanager.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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 "inventorymanager.h"
21 #include "log.h"
22 #include "environment.h"
23 #include "scripting_game.h"
24 #include "serverobject.h"
25 #include "settings.h"
26 #include "craftdef.h"
27 #include "rollback_interface.h"
28 #include "strfnd.h"
29
30 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
31
32 #define PLAYER_TO_SA(p)   p->getEnv()->getScriptIface()
33
34 /*
35         InventoryLocation
36 */
37
38 std::string InventoryLocation::dump() const
39 {
40         std::ostringstream os(std::ios::binary);
41         serialize(os);
42         return os.str();
43 }
44
45 void InventoryLocation::serialize(std::ostream &os) const
46 {
47         switch(type){
48         case InventoryLocation::UNDEFINED:
49                 os<<"undefined";
50                 break;
51         case InventoryLocation::CURRENT_PLAYER:
52                 os<<"current_player";
53                 break;
54         case InventoryLocation::PLAYER:
55                 os<<"player:"<<name;
56                 break;
57         case InventoryLocation::NODEMETA:
58                 os<<"nodemeta:"<<p.X<<","<<p.Y<<","<<p.Z;
59                 break;
60         case InventoryLocation::DETACHED:
61                 os<<"detached:"<<name;
62                 break;
63         default:
64                 FATAL_ERROR("Unhandled inventory location type");
65         }
66 }
67
68 void InventoryLocation::deSerialize(std::istream &is)
69 {
70         std::string tname;
71         std::getline(is, tname, ':');
72         if(tname == "undefined")
73         {
74                 type = InventoryLocation::UNDEFINED;
75         }
76         else if(tname == "current_player")
77         {
78                 type = InventoryLocation::CURRENT_PLAYER;
79         }
80         else if(tname == "player")
81         {
82                 type = InventoryLocation::PLAYER;
83                 std::getline(is, name, '\n');
84         }
85         else if(tname == "nodemeta")
86         {
87                 type = InventoryLocation::NODEMETA;
88                 std::string pos;
89                 std::getline(is, pos, '\n');
90                 Strfnd fn(pos);
91                 p.X = stoi(fn.next(","));
92                 p.Y = stoi(fn.next(","));
93                 p.Z = stoi(fn.next(","));
94         }
95         else if(tname == "detached")
96         {
97                 type = InventoryLocation::DETACHED;
98                 std::getline(is, name, '\n');
99         }
100         else
101         {
102                 infostream<<"Unknown InventoryLocation type=\""<<tname<<"\""<<std::endl;
103                 throw SerializationError("Unknown InventoryLocation type");
104         }
105 }
106
107 void InventoryLocation::deSerialize(std::string s)
108 {
109         std::istringstream is(s, std::ios::binary);
110         deSerialize(is);
111 }
112
113 /*
114         InventoryAction
115 */
116
117 InventoryAction * InventoryAction::deSerialize(std::istream &is)
118 {
119         std::string type;
120         std::getline(is, type, ' ');
121
122         InventoryAction *a = NULL;
123
124         if (type == "Move") {
125                 a = new IMoveAction(is, false);
126         } else if (type == "MoveSomewhere") {
127                 a = new IMoveAction(is, true);
128         } else if (type == "Drop") {
129                 a = new IDropAction(is);
130         } else if(type == "Craft") {
131                 a = new ICraftAction(is);
132         }
133
134         return a;
135 }
136
137 /*
138         IMoveAction
139 */
140
141 IMoveAction::IMoveAction(std::istream &is, bool somewhere)
142 {
143         std::string ts;
144         move_somewhere = somewhere;
145         caused_by_move_somewhere = false;
146         move_count = 0;
147
148         std::getline(is, ts, ' ');
149         count = stoi(ts);
150
151         std::getline(is, ts, ' ');
152         from_inv.deSerialize(ts);
153
154         std::getline(is, from_list, ' ');
155
156         std::getline(is, ts, ' ');
157         from_i = stoi(ts);
158
159         std::getline(is, ts, ' ');
160         to_inv.deSerialize(ts);
161
162         std::getline(is, to_list, ' ');
163
164         if (!somewhere) {
165                 std::getline(is, ts, ' ');
166                 to_i = stoi(ts);
167         }
168 }
169
170 void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
171 {
172         Inventory *inv_from = mgr->getInventory(from_inv);
173         Inventory *inv_to = mgr->getInventory(to_inv);
174         
175         if (!inv_from) {
176                 infostream << "IMoveAction::apply(): FAIL: source inventory not found: "
177                         << "from_inv=\""<<from_inv.dump() << "\""
178                         << ", to_inv=\"" << to_inv.dump() << "\"" << std::endl;
179                 return;
180         }
181         if (!inv_to) {
182                 infostream << "IMoveAction::apply(): FAIL: destination inventory not found: "
183                         << "from_inv=\"" << from_inv.dump() << "\""
184                         << ", to_inv=\"" << to_inv.dump() << "\"" << std::endl;
185                 return;
186         }
187
188         InventoryList *list_from = inv_from->getList(from_list);
189         InventoryList *list_to = inv_to->getList(to_list);
190
191         /*
192                 If a list doesn't exist or the source item doesn't exist
193         */
194         if (!list_from) {
195                 infostream << "IMoveAction::apply(): FAIL: source list not found: "
196                         << "from_inv=\"" << from_inv.dump() << "\""
197                         << ", from_list=\"" << from_list << "\"" << std::endl;
198                 return;
199         }
200         if (!list_to) {
201                 infostream << "IMoveAction::apply(): FAIL: destination list not found: "
202                         << "to_inv=\""<<to_inv.dump() << "\""
203                         << ", to_list=\"" << to_list << "\"" << std::endl;
204                 return;
205         }
206
207         if (move_somewhere) {
208                 s16 old_to_i = to_i;
209                 u16 old_count = count;
210                 caused_by_move_somewhere = true;
211                 move_somewhere = false;
212
213                 infostream << "IMoveAction::apply(): moving item somewhere"
214                         << " msom=" << move_somewhere
215                         << " count=" << count
216                         << " from inv=\"" << from_inv.dump() << "\""
217                         << " list=\"" << from_list << "\""
218                         << " i=" << from_i
219                         << " to inv=\"" << to_inv.dump() << "\""
220                         << " list=\"" << to_list << "\""
221                         << std::endl;
222
223                 // Try to add the item to destination list
224                 s16 dest_size = list_to->getSize();
225                 // First try all the non-empty slots
226                 for (s16 dest_i = 0; dest_i < dest_size && count > 0; dest_i++) {
227                         if (!list_to->getItem(dest_i).empty()) {
228                                 to_i = dest_i;
229                                 apply(mgr, player, gamedef);
230                                 count -= move_count;
231                         }
232                 }
233
234                 // Then try all the empty ones
235                 for (s16 dest_i = 0; dest_i < dest_size && count > 0; dest_i++) {
236                         if (list_to->getItem(dest_i).empty()) {
237                                 to_i = dest_i;
238                                 apply(mgr, player, gamedef);
239                                 count -= move_count;
240                         }
241                 }
242
243                 to_i = old_to_i;
244                 count = old_count;
245                 caused_by_move_somewhere = false;
246                 move_somewhere = true;
247                 return;
248         }
249
250         if ((u16)to_i > list_to->getSize()) {
251                 infostream << "IMoveAction::apply(): FAIL: destination index out of bounds: "
252                         << "to_i=" << to_i
253                         << ", size=" << list_to->getSize() << std::endl;
254                 return;
255         }
256         /*
257                 Do not handle rollback if both inventories are that of the same player
258         */
259         bool ignore_rollback = (
260                 from_inv.type == InventoryLocation::PLAYER &&
261                 to_inv.type == InventoryLocation::PLAYER &&
262                 from_inv.name == to_inv.name);
263
264         /*
265                 Collect information of endpoints
266         */
267
268         int try_take_count = count;
269         if(try_take_count == 0)
270                 try_take_count = list_from->getItem(from_i).count;
271
272         int src_can_take_count = 0xffff;
273         int dst_can_put_count = 0xffff;
274         
275         /* Query detached inventories */
276
277         // Move occurs in the same detached inventory
278         if(from_inv.type == InventoryLocation::DETACHED &&
279                         to_inv.type == InventoryLocation::DETACHED &&
280                         from_inv.name == to_inv.name)
281         {
282                 src_can_take_count = PLAYER_TO_SA(player)->detached_inventory_AllowMove(
283                                 from_inv.name, from_list, from_i,
284                                 to_list, to_i, try_take_count, player);
285                 dst_can_put_count = src_can_take_count;
286         }
287         else
288         {
289                 // Destination is detached
290                 if(to_inv.type == InventoryLocation::DETACHED)
291                 {
292                         ItemStack src_item = list_from->getItem(from_i);
293                         src_item.count = try_take_count;
294                         dst_can_put_count = PLAYER_TO_SA(player)->detached_inventory_AllowPut(
295                                         to_inv.name, to_list, to_i, src_item, player);
296                 }
297                 // Source is detached
298                 if(from_inv.type == InventoryLocation::DETACHED)
299                 {
300                         ItemStack src_item = list_from->getItem(from_i);
301                         src_item.count = try_take_count;
302                         src_can_take_count = PLAYER_TO_SA(player)->detached_inventory_AllowTake(
303                                         from_inv.name, from_list, from_i, src_item, player);
304                 }
305         }
306
307         /* Query node metadata inventories */
308
309         // Both endpoints are nodemeta
310         // Move occurs in the same nodemeta inventory
311         if(from_inv.type == InventoryLocation::NODEMETA &&
312                         to_inv.type == InventoryLocation::NODEMETA &&
313                         from_inv.p == to_inv.p)
314         {
315                 src_can_take_count = PLAYER_TO_SA(player)->nodemeta_inventory_AllowMove(
316                                 from_inv.p, from_list, from_i,
317                                 to_list, to_i, try_take_count, player);
318                 dst_can_put_count = src_can_take_count;
319         }
320         else
321         {
322                 // Destination is nodemeta
323                 if(to_inv.type == InventoryLocation::NODEMETA)
324                 {
325                         ItemStack src_item = list_from->getItem(from_i);
326                         src_item.count = try_take_count;
327                         dst_can_put_count = PLAYER_TO_SA(player)->nodemeta_inventory_AllowPut(
328                                         to_inv.p, to_list, to_i, src_item, player);
329                 }
330                 // Source is nodemeta
331                 if(from_inv.type == InventoryLocation::NODEMETA)
332                 {
333                         ItemStack src_item = list_from->getItem(from_i);
334                         src_item.count = try_take_count;
335                         src_can_take_count = PLAYER_TO_SA(player)->nodemeta_inventory_AllowTake(
336                                         from_inv.p, from_list, from_i, src_item, player);
337                 }
338         }
339
340         int old_count = count;
341         
342         /* Modify count according to collected data */
343         count = try_take_count;
344         if(src_can_take_count != -1 && count > src_can_take_count)
345                 count = src_can_take_count;
346         if(dst_can_put_count != -1 && count > dst_can_put_count)
347                 count = dst_can_put_count;
348         /* Limit according to source item count */
349         if(count > list_from->getItem(from_i).count)
350                 count = list_from->getItem(from_i).count;
351         
352         /* If no items will be moved, don't go further */
353         if(count == 0)
354         {
355                 infostream<<"IMoveAction::apply(): move was completely disallowed:"
356                                 <<" count="<<old_count
357                                 <<" from inv=\""<<from_inv.dump()<<"\""
358                                 <<" list=\""<<from_list<<"\""
359                                 <<" i="<<from_i
360                                 <<" to inv=\""<<to_inv.dump()<<"\""
361                                 <<" list=\""<<to_list<<"\""
362                                 <<" i="<<to_i
363                                 <<std::endl;
364                 return;
365         }
366
367         ItemStack src_item = list_from->getItem(from_i);
368         src_item.count = count;
369         ItemStack from_stack_was = list_from->getItem(from_i);
370         ItemStack to_stack_was = list_to->getItem(to_i);
371
372         /*
373                 Perform actual move
374
375                 If something is wrong (source item is empty, destination is the
376                 same as source), nothing happens
377         */
378         move_count = list_from->moveItem(from_i,
379                 list_to, to_i, count, !caused_by_move_somewhere);
380
381         // If source is infinite, reset it's stack
382         if(src_can_take_count == -1){
383                 // If destination stack is of different type and there are leftover
384                 // items, attempt to put the leftover items to a different place in the
385                 // destination inventory.
386                 // The client-side GUI will try to guess if this happens.
387                 if(from_stack_was.name != to_stack_was.name){
388                         for(u32 i=0; i<list_to->getSize(); i++){
389                                 if(list_to->getItem(i).empty()){
390                                         list_to->changeItem(i, to_stack_was);
391                                         break;
392                                 }
393                         }
394                 }
395                 list_from->deleteItem(from_i);
396                 list_from->addItem(from_i, from_stack_was);
397         }
398         // If destination is infinite, reset it's stack and take count from source
399         if(dst_can_put_count == -1){
400                 list_to->deleteItem(to_i);
401                 list_to->addItem(to_i, to_stack_was);
402                 list_from->deleteItem(from_i);
403                 list_from->addItem(from_i, from_stack_was);
404                 list_from->takeItem(from_i, count);
405         }
406
407         infostream << "IMoveAction::apply(): moved"
408                         << " msom=" << move_somewhere
409                         << " caused=" << caused_by_move_somewhere
410                         << " count=" << count
411                         << " from inv=\"" << from_inv.dump() << "\""
412                         << " list=\"" << from_list << "\""
413                         << " i=" << from_i
414                         << " to inv=\"" << to_inv.dump() << "\""
415                         << " list=\"" << to_list << "\""
416                         << " i=" << to_i
417                         << std::endl;
418
419         /*
420                 Record rollback information
421         */
422         if(!ignore_rollback && gamedef->rollback())
423         {
424                 IRollbackManager *rollback = gamedef->rollback();
425
426                 // If source is not infinite, record item take
427                 if(src_can_take_count != -1){
428                         RollbackAction action;
429                         std::string loc;
430                         {
431                                 std::ostringstream os(std::ios::binary);
432                                 from_inv.serialize(os);
433                                 loc = os.str();
434                         }
435                         action.setModifyInventoryStack(loc, from_list, from_i, false,
436                                         src_item);
437                         rollback->reportAction(action);
438                 }
439                 // If destination is not infinite, record item put
440                 if(dst_can_put_count != -1){
441                         RollbackAction action;
442                         std::string loc;
443                         {
444                                 std::ostringstream os(std::ios::binary);
445                                 to_inv.serialize(os);
446                                 loc = os.str();
447                         }
448                         action.setModifyInventoryStack(loc, to_list, to_i, true,
449                                         src_item);
450                         rollback->reportAction(action);
451                 }
452         }
453
454         /*
455                 Report move to endpoints
456         */
457         
458         /* Detached inventories */
459
460         // Both endpoints are same detached
461         if(from_inv.type == InventoryLocation::DETACHED &&
462                         to_inv.type == InventoryLocation::DETACHED &&
463                         from_inv.name == to_inv.name)
464         {
465                 PLAYER_TO_SA(player)->detached_inventory_OnMove(
466                                 from_inv.name, from_list, from_i,
467                                 to_list, to_i, count, player);
468         }
469         else
470         {
471                 // Destination is detached
472                 if(to_inv.type == InventoryLocation::DETACHED)
473                 {
474                         PLAYER_TO_SA(player)->detached_inventory_OnPut(
475                                         to_inv.name, to_list, to_i, src_item, player);
476                 }
477                 // Source is detached
478                 if(from_inv.type == InventoryLocation::DETACHED)
479                 {
480                         PLAYER_TO_SA(player)->detached_inventory_OnTake(
481                                         from_inv.name, from_list, from_i, src_item, player);
482                 }
483         }
484
485         /* Node metadata inventories */
486
487         // Both endpoints are same nodemeta
488         if(from_inv.type == InventoryLocation::NODEMETA &&
489                         to_inv.type == InventoryLocation::NODEMETA &&
490                         from_inv.p == to_inv.p)
491         {
492                 PLAYER_TO_SA(player)->nodemeta_inventory_OnMove(
493                                 from_inv.p, from_list, from_i,
494                                 to_list, to_i, count, player);
495         }
496         else{
497                 // Destination is nodemeta
498                 if(to_inv.type == InventoryLocation::NODEMETA)
499                 {
500                         PLAYER_TO_SA(player)->nodemeta_inventory_OnPut(
501                                         to_inv.p, to_list, to_i, src_item, player);
502                 }
503                 // Source is nodemeta
504                 else if(from_inv.type == InventoryLocation::NODEMETA)
505                 {
506                         PLAYER_TO_SA(player)->nodemeta_inventory_OnTake(
507                                         from_inv.p, from_list, from_i, src_item, player);
508                 }
509         }
510         
511         mgr->setInventoryModified(from_inv, false);
512         if(inv_from != inv_to)
513                 mgr->setInventoryModified(to_inv, false);
514 }
515
516 void IMoveAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
517 {
518         // Optional InventoryAction operation that is run on the client
519         // to make lag less apparent.
520
521         Inventory *inv_from = mgr->getInventory(from_inv);
522         Inventory *inv_to = mgr->getInventory(to_inv);
523         if(!inv_from || !inv_to)
524                 return;
525
526         InventoryLocation current_player;
527         current_player.setCurrentPlayer();
528         Inventory *inv_player = mgr->getInventory(current_player);
529         if(inv_from != inv_player || inv_to != inv_player)
530                 return;
531
532         InventoryList *list_from = inv_from->getList(from_list);
533         InventoryList *list_to = inv_to->getList(to_list);
534         if(!list_from || !list_to)
535                 return;
536
537         if (!move_somewhere)
538                 list_from->moveItem(from_i, list_to, to_i, count);
539         else
540                 list_from->moveItemSomewhere(from_i, list_to, count);
541
542         mgr->setInventoryModified(from_inv);
543         if(inv_from != inv_to)
544                 mgr->setInventoryModified(to_inv);
545 }
546
547 /*
548         IDropAction
549 */
550
551 IDropAction::IDropAction(std::istream &is)
552 {
553         std::string ts;
554
555         std::getline(is, ts, ' ');
556         count = stoi(ts);
557
558         std::getline(is, ts, ' ');
559         from_inv.deSerialize(ts);
560
561         std::getline(is, from_list, ' ');
562
563         std::getline(is, ts, ' ');
564         from_i = stoi(ts);
565 }
566
567 void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
568 {
569         Inventory *inv_from = mgr->getInventory(from_inv);
570         
571         if(!inv_from){
572                 infostream<<"IDropAction::apply(): FAIL: source inventory not found: "
573                                 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
574                 return;
575         }
576
577         InventoryList *list_from = inv_from->getList(from_list);
578
579         /*
580                 If a list doesn't exist or the source item doesn't exist
581         */
582         if(!list_from){
583                 infostream<<"IDropAction::apply(): FAIL: source list not found: "
584                                 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
585                 return;
586         }
587         if(list_from->getItem(from_i).empty())
588         {
589                 infostream<<"IDropAction::apply(): FAIL: source item not found: "
590                                 <<"from_inv=\""<<from_inv.dump()<<"\""
591                                 <<", from_list=\""<<from_list<<"\""
592                                 <<" from_i="<<from_i<<std::endl;
593                 return;
594         }
595
596         /*
597                 Do not handle rollback if inventory is player's
598         */
599         bool ignore_src_rollback = (from_inv.type == InventoryLocation::PLAYER);
600
601         /*
602                 Collect information of endpoints
603         */
604
605         int take_count = list_from->getItem(from_i).count;
606         if(count != 0 && count < take_count)
607                 take_count = count;
608         int src_can_take_count = take_count;
609
610         // Source is detached
611         if(from_inv.type == InventoryLocation::DETACHED)
612         {
613                 ItemStack src_item = list_from->getItem(from_i);
614                 src_item.count = take_count;
615                 src_can_take_count = PLAYER_TO_SA(player)->detached_inventory_AllowTake(
616                                 from_inv.name, from_list, from_i, src_item, player);
617         }
618
619         // Source is nodemeta
620         if(from_inv.type == InventoryLocation::NODEMETA)
621         {
622                 ItemStack src_item = list_from->getItem(from_i);
623                 src_item.count = take_count;
624                 src_can_take_count = PLAYER_TO_SA(player)->nodemeta_inventory_AllowTake(
625                                 from_inv.p, from_list, from_i, src_item, player);
626         }
627
628         if(src_can_take_count != -1 && src_can_take_count < take_count)
629                 take_count = src_can_take_count;
630         
631         int actually_dropped_count = 0;
632
633         ItemStack src_item = list_from->getItem(from_i);
634
635         // Drop the item
636         ItemStack item1 = list_from->getItem(from_i);
637         item1.count = take_count;
638         if(PLAYER_TO_SA(player)->item_OnDrop(item1, player,
639                                 player->getBasePosition() + v3f(0,1,0)))
640         {
641                 actually_dropped_count = take_count - item1.count;
642
643                 if(actually_dropped_count == 0){
644                         infostream<<"Actually dropped no items"<<std::endl;
645                         return;
646                 }
647                 
648                 // If source isn't infinite
649                 if(src_can_take_count != -1){
650                         // Take item from source list
651                         ItemStack item2 = list_from->takeItem(from_i, actually_dropped_count);
652
653                         if(item2.count != actually_dropped_count)
654                                 errorstream<<"Could not take dropped count of items"<<std::endl;
655
656                         mgr->setInventoryModified(from_inv, false);
657                 }
658         }
659
660         infostream<<"IDropAction::apply(): dropped "
661                         <<" from inv=\""<<from_inv.dump()<<"\""
662                         <<" list=\""<<from_list<<"\""
663                         <<" i="<<from_i
664                         <<std::endl;
665         
666         src_item.count = actually_dropped_count;
667
668         /*
669                 Report drop to endpoints
670         */
671         
672         // Source is detached
673         if(from_inv.type == InventoryLocation::DETACHED)
674         {
675                 PLAYER_TO_SA(player)->detached_inventory_OnTake(
676                                 from_inv.name, from_list, from_i, src_item, player);
677         }
678
679         // Source is nodemeta
680         if(from_inv.type == InventoryLocation::NODEMETA)
681         {
682                 PLAYER_TO_SA(player)->nodemeta_inventory_OnTake(
683                                 from_inv.p, from_list, from_i, src_item, player);
684         }
685
686         /*
687                 Record rollback information
688         */
689         if(!ignore_src_rollback && gamedef->rollback())
690         {
691                 IRollbackManager *rollback = gamedef->rollback();
692
693                 // If source is not infinite, record item take
694                 if(src_can_take_count != -1){
695                         RollbackAction action;
696                         std::string loc;
697                         {
698                                 std::ostringstream os(std::ios::binary);
699                                 from_inv.serialize(os);
700                                 loc = os.str();
701                         }
702                         action.setModifyInventoryStack(loc, from_list, from_i,
703                                         false, src_item);
704                         rollback->reportAction(action);
705                 }
706         }
707 }
708
709 void IDropAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
710 {
711         // Optional InventoryAction operation that is run on the client
712         // to make lag less apparent.
713
714         Inventory *inv_from = mgr->getInventory(from_inv);
715         if(!inv_from)
716                 return;
717
718         InventoryLocation current_player;
719         current_player.setCurrentPlayer();
720         Inventory *inv_player = mgr->getInventory(current_player);
721         if(inv_from != inv_player)
722                 return;
723
724         InventoryList *list_from = inv_from->getList(from_list);
725         if(!list_from)
726                 return;
727
728         if(count == 0)
729                 list_from->changeItem(from_i, ItemStack());
730         else
731                 list_from->takeItem(from_i, count);
732
733         mgr->setInventoryModified(from_inv);
734 }
735
736 /*
737         ICraftAction
738 */
739
740 ICraftAction::ICraftAction(std::istream &is)
741 {
742         std::string ts;
743
744         std::getline(is, ts, ' ');
745         count = stoi(ts);
746
747         std::getline(is, ts, ' ');
748         craft_inv.deSerialize(ts);
749 }
750
751 void ICraftAction::apply(InventoryManager *mgr,
752         ServerActiveObject *player, IGameDef *gamedef)
753 {
754         Inventory *inv_craft = mgr->getInventory(craft_inv);
755         
756         if (!inv_craft) {
757                 infostream << "ICraftAction::apply(): FAIL: inventory not found: "
758                                 << "craft_inv=\"" << craft_inv.dump() << "\"" << std::endl;
759                 return;
760         }
761
762         InventoryList *list_craft = inv_craft->getList("craft");
763         InventoryList *list_craftresult = inv_craft->getList("craftresult");
764         InventoryList *list_main = inv_craft->getList("main");
765
766         /*
767                 If a list doesn't exist or the source item doesn't exist
768         */
769         if (!list_craft) {
770                 infostream << "ICraftAction::apply(): FAIL: craft list not found: "
771                                 << "craft_inv=\"" << craft_inv.dump() << "\"" << std::endl;
772                 return;
773         }
774         if (!list_craftresult) {
775                 infostream << "ICraftAction::apply(): FAIL: craftresult list not found: "
776                                 << "craft_inv=\"" << craft_inv.dump() << "\"" << std::endl;
777                 return;
778         }
779         if (list_craftresult->getSize() < 1) {
780                 infostream << "ICraftAction::apply(): FAIL: craftresult list too short: "
781                                 << "craft_inv=\"" << craft_inv.dump() << "\"" << std::endl;
782                 return;
783         }
784
785         ItemStack crafted;
786         ItemStack craftresultitem;
787         int count_remaining = count;
788         std::vector<ItemStack> output_replacements;
789         getCraftingResult(inv_craft, crafted, output_replacements, false, gamedef);
790         PLAYER_TO_SA(player)->item_CraftPredict(crafted, player, list_craft, craft_inv);
791         bool found = !crafted.empty();
792
793         while (found && list_craftresult->itemFits(0, crafted)) {
794                 InventoryList saved_craft_list = *list_craft;
795
796                 std::vector<ItemStack> temp;
797                 // Decrement input and add crafting output
798                 getCraftingResult(inv_craft, crafted, temp, true, gamedef);
799                 PLAYER_TO_SA(player)->item_OnCraft(crafted, player, &saved_craft_list, craft_inv);
800                 list_craftresult->addItem(0, crafted);
801                 mgr->setInventoryModified(craft_inv);
802
803                 // Add the new replacements to the list
804                 IItemDefManager *itemdef = gamedef->getItemDefManager();
805                 for (std::vector<ItemStack>::iterator it = temp.begin();
806                                 it != temp.end(); it++) {
807                         for (std::vector<ItemStack>::iterator jt = output_replacements.begin();
808                                         jt != output_replacements.end(); jt++) {
809                                 if (it->name == jt->name) {
810                                         *it = jt->addItem(*it, itemdef);
811                                         if (it->empty())
812                                                 continue;
813                                 }
814                         }
815                         output_replacements.push_back(*it);
816                 }
817
818                 actionstream << player->getDescription()
819                                 << " crafts "
820                                 << crafted.getItemString()
821                                 << std::endl;
822
823                 // Decrement counter
824                 if (count_remaining == 1)
825                         break;
826                 else if (count_remaining > 1)
827                         count_remaining--;
828
829                 // Get next crafting result
830                 found = getCraftingResult(inv_craft, crafted, temp, false, gamedef);
831                 PLAYER_TO_SA(player)->item_CraftPredict(crafted, player, list_craft, craft_inv);
832                 found = !crafted.empty();
833         }
834
835         // Put the replacements in the inventory or drop them on the floor, if
836         // the invenotry is full
837         for (std::vector<ItemStack>::iterator it = output_replacements.begin();
838                         it != output_replacements.end(); it++) {
839                 if (list_main)
840                         *it = list_main->addItem(*it);
841                 if (it->empty())
842                         continue;
843                 u16 count = it->count;
844                 do {
845                         PLAYER_TO_SA(player)->item_OnDrop(*it, player,
846                                 player->getBasePosition() + v3f(0,1,0));
847                         if (count >= it->count) {
848                                 errorstream << "Couldn't drop replacement stack " <<
849                                         it->getItemString() << " because drop loop didn't "
850                                         "decrease count." << std::endl;
851
852                                 break;
853                         }
854                 } while (!it->empty());
855         }
856
857         infostream<<"ICraftAction::apply(): crafted "
858                         <<" craft_inv=\""<<craft_inv.dump()<<"\""
859                         <<std::endl;
860 }
861
862 void ICraftAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
863 {
864         // Optional InventoryAction operation that is run on the client
865         // to make lag less apparent.
866 }
867
868
869 // Crafting helper
870 bool getCraftingResult(Inventory *inv, ItemStack& result,
871                 std::vector<ItemStack> &output_replacements,
872                 bool decrementInput, IGameDef *gamedef)
873 {
874         DSTACK(__FUNCTION_NAME);
875         
876         result.clear();
877
878         // Get the InventoryList in which we will operate
879         InventoryList *clist = inv->getList("craft");
880         if(!clist)
881                 return false;
882
883         // Mangle crafting grid to an another format
884         CraftInput ci;
885         ci.method = CRAFT_METHOD_NORMAL;
886         ci.width = clist->getWidth() ? clist->getWidth() : 3;
887         for(u16 i=0; i<clist->getSize(); i++)
888                 ci.items.push_back(clist->getItem(i));
889
890         // Find out what is crafted and add it to result item slot
891         CraftOutput co;
892         bool found = gamedef->getCraftDefManager()->getCraftResult(
893                         ci, co, output_replacements, decrementInput, gamedef);
894         if(found)
895                 result.deSerialize(co.item, gamedef->getItemDefManager());
896
897         if(found && decrementInput)
898         {
899                 // CraftInput has been changed, apply changes in clist
900                 for(u16 i=0; i<clist->getSize(); i++)
901                 {
902                         clist->changeItem(i, ci.items[i]);
903                 }
904         }
905
906         return found;
907 }
908