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