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