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