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