3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "inventorymanager.h"
22 #include "environment.h"
23 #include "scripting_game.h"
24 #include "serverobject.h"
25 #include "main.h" // for g_settings
28 #include "rollback_interface.h"
31 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
33 #define PLAYER_TO_SA(p) p->getEnv()->getScriptIface()
39 std::string InventoryLocation::dump() const
41 std::ostringstream os(std::ios::binary);
46 void InventoryLocation::serialize(std::ostream &os) const
49 case InventoryLocation::UNDEFINED:
52 case InventoryLocation::CURRENT_PLAYER:
55 case InventoryLocation::PLAYER:
58 case InventoryLocation::NODEMETA:
59 os<<"nodemeta:"<<p.X<<","<<p.Y<<","<<p.Z;
61 case InventoryLocation::DETACHED:
62 os<<"detached:"<<name;
69 void InventoryLocation::deSerialize(std::istream &is)
72 std::getline(is, tname, ':');
73 if(tname == "undefined")
75 type = InventoryLocation::UNDEFINED;
77 else if(tname == "current_player")
79 type = InventoryLocation::CURRENT_PLAYER;
81 else if(tname == "player")
83 type = InventoryLocation::PLAYER;
84 std::getline(is, name, '\n');
86 else if(tname == "nodemeta")
88 type = InventoryLocation::NODEMETA;
90 std::getline(is, pos, '\n');
92 p.X = stoi(fn.next(","));
93 p.Y = stoi(fn.next(","));
94 p.Z = stoi(fn.next(","));
96 else if(tname == "detached")
98 type = InventoryLocation::DETACHED;
99 std::getline(is, name, '\n');
103 infostream<<"Unknown InventoryLocation type=\""<<tname<<"\""<<std::endl;
104 throw SerializationError("Unknown InventoryLocation type");
108 void InventoryLocation::deSerialize(std::string s)
110 std::istringstream is(s, std::ios::binary);
118 InventoryAction * InventoryAction::deSerialize(std::istream &is)
121 std::getline(is, type, ' ');
123 InventoryAction *a = NULL;
127 a = new IMoveAction(is);
129 else if(type == "Drop")
131 a = new IDropAction(is);
133 else if(type == "Craft")
135 a = new ICraftAction(is);
145 IMoveAction::IMoveAction(std::istream &is)
149 std::getline(is, ts, ' ');
152 std::getline(is, ts, ' ');
153 from_inv.deSerialize(ts);
155 std::getline(is, from_list, ' ');
157 std::getline(is, ts, ' ');
160 std::getline(is, ts, ' ');
161 to_inv.deSerialize(ts);
163 std::getline(is, to_list, ' ');
165 std::getline(is, ts, ' ');
169 void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
171 Inventory *inv_from = mgr->getInventory(from_inv);
172 Inventory *inv_to = mgr->getInventory(to_inv);
175 infostream<<"IMoveAction::apply(): FAIL: source inventory not found: "
176 <<"from_inv=\""<<from_inv.dump()<<"\""
177 <<", to_inv=\""<<to_inv.dump()<<"\""<<std::endl;
181 infostream<<"IMoveAction::apply(): FAIL: destination inventory not found: "
182 <<"from_inv=\""<<from_inv.dump()<<"\""
183 <<", to_inv=\""<<to_inv.dump()<<"\""<<std::endl;
187 InventoryList *list_from = inv_from->getList(from_list);
188 InventoryList *list_to = inv_to->getList(to_list);
191 If a list doesn't exist or the source item doesn't exist
194 infostream<<"IMoveAction::apply(): FAIL: source list not found: "
195 <<"from_inv=\""<<from_inv.dump()<<"\""
196 <<", from_list=\""<<from_list<<"\""<<std::endl;
200 infostream<<"IMoveAction::apply(): FAIL: destination list not found: "
201 <<"to_inv=\""<<to_inv.dump()<<"\""
202 <<", to_list=\""<<to_list<<"\""<<std::endl;
207 Do not handle rollback if both inventories are that of the same player
209 bool ignore_rollback = (
210 from_inv.type == InventoryLocation::PLAYER &&
211 to_inv.type == InventoryLocation::PLAYER &&
212 from_inv.name == to_inv.name);
215 Collect information of endpoints
218 int try_take_count = count;
219 if(try_take_count == 0)
220 try_take_count = list_from->getItem(from_i).count;
222 int src_can_take_count = 0xffff;
223 int dst_can_put_count = 0xffff;
225 /* Query detached inventories */
227 // Move occurs in the same detached inventory
228 if(from_inv.type == InventoryLocation::DETACHED &&
229 to_inv.type == InventoryLocation::DETACHED &&
230 from_inv.name == to_inv.name)
232 src_can_take_count = PLAYER_TO_SA(player)->detached_inventory_AllowMove(
233 from_inv.name, from_list, from_i,
234 to_list, to_i, try_take_count, player);
235 dst_can_put_count = src_can_take_count;
239 // Destination is detached
240 if(to_inv.type == InventoryLocation::DETACHED)
242 ItemStack src_item = list_from->getItem(from_i);
243 src_item.count = try_take_count;
244 dst_can_put_count = PLAYER_TO_SA(player)->detached_inventory_AllowPut(
245 to_inv.name, to_list, to_i, src_item, player);
247 // Source is detached
248 if(from_inv.type == InventoryLocation::DETACHED)
250 ItemStack src_item = list_from->getItem(from_i);
251 src_item.count = try_take_count;
252 src_can_take_count = PLAYER_TO_SA(player)->detached_inventory_AllowTake(
253 from_inv.name, from_list, from_i, src_item, player);
257 /* Query node metadata inventories */
259 // Both endpoints are nodemeta
260 // Move occurs in the same nodemeta inventory
261 if(from_inv.type == InventoryLocation::NODEMETA &&
262 to_inv.type == InventoryLocation::NODEMETA &&
263 from_inv.p == to_inv.p)
265 src_can_take_count = PLAYER_TO_SA(player)->nodemeta_inventory_AllowMove(
266 from_inv.p, from_list, from_i,
267 to_list, to_i, try_take_count, player);
268 dst_can_put_count = src_can_take_count;
272 // Destination is nodemeta
273 if(to_inv.type == InventoryLocation::NODEMETA)
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)->nodemeta_inventory_AllowPut(
278 to_inv.p, to_list, to_i, src_item, player);
280 // Source is nodemeta
281 if(from_inv.type == InventoryLocation::NODEMETA)
283 ItemStack src_item = list_from->getItem(from_i);
284 src_item.count = try_take_count;
285 src_can_take_count = PLAYER_TO_SA(player)->nodemeta_inventory_AllowTake(
286 from_inv.p, from_list, from_i, src_item, player);
290 int old_count = count;
292 /* Modify count according to collected data */
293 count = try_take_count;
294 if(src_can_take_count != -1 && count > src_can_take_count)
295 count = src_can_take_count;
296 if(dst_can_put_count != -1 && count > dst_can_put_count)
297 count = dst_can_put_count;
298 /* Limit according to source item count */
299 if(count > list_from->getItem(from_i).count)
300 count = list_from->getItem(from_i).count;
302 /* If no items will be moved, don't go further */
305 infostream<<"IMoveAction::apply(): move was completely disallowed:"
306 <<" count="<<old_count
307 <<" from inv=\""<<from_inv.dump()<<"\""
308 <<" list=\""<<from_list<<"\""
310 <<" to inv=\""<<to_inv.dump()<<"\""
311 <<" list=\""<<to_list<<"\""
317 ItemStack src_item = list_from->getItem(from_i);
318 src_item.count = count;
319 ItemStack from_stack_was = list_from->getItem(from_i);
320 ItemStack to_stack_was = list_to->getItem(to_i);
325 If something is wrong (source item is empty, destination is the
326 same as source), nothing happens
328 list_from->moveItem(from_i, list_to, to_i, count);
330 // If source is infinite, reset it's stack
331 if(src_can_take_count == -1){
332 // If destination stack is of different type and there are leftover
333 // items, attempt to put the leftover items to a different place in the
334 // destination inventory.
335 // The client-side GUI will try to guess if this happens.
336 if(from_stack_was.name != to_stack_was.name){
337 for(u32 i=0; i<list_to->getSize(); i++){
338 if(list_to->getItem(i).empty()){
339 list_to->changeItem(i, to_stack_was);
344 list_from->deleteItem(from_i);
345 list_from->addItem(from_i, from_stack_was);
347 // If destination is infinite, reset it's stack and take count from source
348 if(dst_can_put_count == -1){
349 list_to->deleteItem(to_i);
350 list_to->addItem(to_i, to_stack_was);
351 list_from->deleteItem(from_i);
352 list_from->addItem(from_i, from_stack_was);
353 list_from->takeItem(from_i, count);
356 infostream<<"IMoveAction::apply(): moved"
358 <<" from inv=\""<<from_inv.dump()<<"\""
359 <<" list=\""<<from_list<<"\""
361 <<" to inv=\""<<to_inv.dump()<<"\""
362 <<" list=\""<<to_list<<"\""
367 Record rollback information
369 if(!ignore_rollback && gamedef->rollback())
371 IRollbackReportSink *rollback = gamedef->rollback();
373 // If source is not infinite, record item take
374 if(src_can_take_count != -1){
375 RollbackAction action;
378 std::ostringstream os(std::ios::binary);
379 from_inv.serialize(os);
382 action.setModifyInventoryStack(loc, from_list, from_i, false,
383 src_item.getItemString());
384 rollback->reportAction(action);
386 // If destination is not infinite, record item put
387 if(dst_can_put_count != -1){
388 RollbackAction action;
391 std::ostringstream os(std::ios::binary);
392 to_inv.serialize(os);
395 action.setModifyInventoryStack(loc, to_list, to_i, true,
396 src_item.getItemString());
397 rollback->reportAction(action);
402 Report move to endpoints
405 /* Detached inventories */
407 // Both endpoints are same detached
408 if(from_inv.type == InventoryLocation::DETACHED &&
409 to_inv.type == InventoryLocation::DETACHED &&
410 from_inv.name == to_inv.name)
412 PLAYER_TO_SA(player)->detached_inventory_OnMove(
413 from_inv.name, from_list, from_i,
414 to_list, to_i, count, player);
418 // Destination is detached
419 if(to_inv.type == InventoryLocation::DETACHED)
421 PLAYER_TO_SA(player)->detached_inventory_OnPut(
422 to_inv.name, to_list, to_i, src_item, player);
424 // Source is detached
425 if(from_inv.type == InventoryLocation::DETACHED)
427 PLAYER_TO_SA(player)->detached_inventory_OnTake(
428 from_inv.name, from_list, from_i, src_item, player);
432 /* Node metadata inventories */
434 // Both endpoints are same nodemeta
435 if(from_inv.type == InventoryLocation::NODEMETA &&
436 to_inv.type == InventoryLocation::NODEMETA &&
437 from_inv.p == to_inv.p)
439 PLAYER_TO_SA(player)->nodemeta_inventory_OnMove(
440 from_inv.p, from_list, from_i,
441 to_list, to_i, count, player);
444 // Destination is nodemeta
445 if(to_inv.type == InventoryLocation::NODEMETA)
447 PLAYER_TO_SA(player)->nodemeta_inventory_OnPut(
448 to_inv.p, to_list, to_i, src_item, player);
450 // Source is nodemeta
451 else if(from_inv.type == InventoryLocation::NODEMETA)
453 PLAYER_TO_SA(player)->nodemeta_inventory_OnTake(
454 from_inv.p, from_list, from_i, src_item, player);
458 mgr->setInventoryModified(from_inv);
459 if(inv_from != inv_to)
460 mgr->setInventoryModified(to_inv);
463 void IMoveAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
465 // Optional InventoryAction operation that is run on the client
466 // to make lag less apparent.
468 Inventory *inv_from = mgr->getInventory(from_inv);
469 Inventory *inv_to = mgr->getInventory(to_inv);
470 if(!inv_from || !inv_to)
473 InventoryLocation current_player;
474 current_player.setCurrentPlayer();
475 Inventory *inv_player = mgr->getInventory(current_player);
476 if(inv_from != inv_player || inv_to != inv_player)
479 InventoryList *list_from = inv_from->getList(from_list);
480 InventoryList *list_to = inv_to->getList(to_list);
481 if(!list_from || !list_to)
484 list_from->moveItem(from_i, list_to, to_i, count);
486 mgr->setInventoryModified(from_inv);
487 if(inv_from != inv_to)
488 mgr->setInventoryModified(to_inv);
495 IDropAction::IDropAction(std::istream &is)
499 std::getline(is, ts, ' ');
502 std::getline(is, ts, ' ');
503 from_inv.deSerialize(ts);
505 std::getline(is, from_list, ' ');
507 std::getline(is, ts, ' ');
511 void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
513 Inventory *inv_from = mgr->getInventory(from_inv);
516 infostream<<"IDropAction::apply(): FAIL: source inventory not found: "
517 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
521 InventoryList *list_from = inv_from->getList(from_list);
524 If a list doesn't exist or the source item doesn't exist
527 infostream<<"IDropAction::apply(): FAIL: source list not found: "
528 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
531 if(list_from->getItem(from_i).empty())
533 infostream<<"IDropAction::apply(): FAIL: source item not found: "
534 <<"from_inv=\""<<from_inv.dump()<<"\""
535 <<", from_list=\""<<from_list<<"\""
536 <<" from_i="<<from_i<<std::endl;
541 Do not handle rollback if inventory is player's
543 bool ignore_src_rollback = (from_inv.type == InventoryLocation::PLAYER);
546 Collect information of endpoints
549 int take_count = list_from->getItem(from_i).count;
550 if(count != 0 && count < take_count)
552 int src_can_take_count = take_count;
554 // Source is detached
555 if(from_inv.type == InventoryLocation::DETACHED)
557 ItemStack src_item = list_from->getItem(from_i);
558 src_item.count = take_count;
559 src_can_take_count = PLAYER_TO_SA(player)->detached_inventory_AllowTake(
560 from_inv.name, from_list, from_i, src_item, player);
563 // Source is nodemeta
564 if(from_inv.type == InventoryLocation::NODEMETA)
566 ItemStack src_item = list_from->getItem(from_i);
567 src_item.count = take_count;
568 src_can_take_count = PLAYER_TO_SA(player)->nodemeta_inventory_AllowTake(
569 from_inv.p, from_list, from_i, src_item, player);
572 if(src_can_take_count != -1 && src_can_take_count < take_count)
573 take_count = src_can_take_count;
575 int actually_dropped_count = 0;
577 ItemStack src_item = list_from->getItem(from_i);
580 ItemStack item1 = list_from->getItem(from_i);
581 item1.count = take_count;
582 if(PLAYER_TO_SA(player)->item_OnDrop(item1, player,
583 player->getBasePosition() + v3f(0,1,0)))
585 actually_dropped_count = take_count - item1.count;
587 if(actually_dropped_count == 0){
588 infostream<<"Actually dropped no items"<<std::endl;
592 // If source isn't infinite
593 if(src_can_take_count != -1){
594 // Take item from source list
595 ItemStack item2 = list_from->takeItem(from_i, actually_dropped_count);
597 if(item2.count != actually_dropped_count)
598 errorstream<<"Could not take dropped count of items"<<std::endl;
600 mgr->setInventoryModified(from_inv);
604 infostream<<"IDropAction::apply(): dropped "
605 <<" from inv=\""<<from_inv.dump()<<"\""
606 <<" list=\""<<from_list<<"\""
610 src_item.count = actually_dropped_count;
613 Report drop to endpoints
616 // Source is detached
617 if(from_inv.type == InventoryLocation::DETACHED)
619 PLAYER_TO_SA(player)->detached_inventory_OnTake(
620 from_inv.name, from_list, from_i, src_item, player);
623 // Source is nodemeta
624 if(from_inv.type == InventoryLocation::NODEMETA)
626 PLAYER_TO_SA(player)->nodemeta_inventory_OnTake(
627 from_inv.p, from_list, from_i, src_item, player);
631 Record rollback information
633 if(!ignore_src_rollback && gamedef->rollback())
635 IRollbackReportSink *rollback = gamedef->rollback();
637 // If source is not infinite, record item take
638 if(src_can_take_count != -1){
639 RollbackAction action;
642 std::ostringstream os(std::ios::binary);
643 from_inv.serialize(os);
646 action.setModifyInventoryStack(loc, from_list, from_i,
647 false, src_item.getItemString());
648 rollback->reportAction(action);
653 void IDropAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
655 // Optional InventoryAction operation that is run on the client
656 // to make lag less apparent.
658 Inventory *inv_from = mgr->getInventory(from_inv);
662 InventoryLocation current_player;
663 current_player.setCurrentPlayer();
664 Inventory *inv_player = mgr->getInventory(current_player);
665 if(inv_from != inv_player)
668 InventoryList *list_from = inv_from->getList(from_list);
673 list_from->changeItem(from_i, ItemStack());
675 list_from->takeItem(from_i, count);
677 mgr->setInventoryModified(from_inv);
684 ICraftAction::ICraftAction(std::istream &is)
688 std::getline(is, ts, ' ');
691 std::getline(is, ts, ' ');
692 craft_inv.deSerialize(ts);
695 void ICraftAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
697 Inventory *inv_craft = mgr->getInventory(craft_inv);
700 infostream<<"ICraftAction::apply(): FAIL: inventory not found: "
701 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
705 InventoryList *list_craft = inv_craft->getList("craft");
706 InventoryList *list_craftresult = inv_craft->getList("craftresult");
709 If a list doesn't exist or the source item doesn't exist
712 infostream<<"ICraftAction::apply(): FAIL: craft list not found: "
713 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
716 if(!list_craftresult){
717 infostream<<"ICraftAction::apply(): FAIL: craftresult list not found: "
718 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
721 if(list_craftresult->getSize() < 1){
722 infostream<<"ICraftAction::apply(): FAIL: craftresult list too short: "
723 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
728 ItemStack craftresultitem;
729 int count_remaining = count;
730 bool found = getCraftingResult(inv_craft, crafted, false, gamedef);
731 PLAYER_TO_SA(player)->item_CraftPredict(crafted, player, list_craft, craft_inv);
732 found = !crafted.empty();
734 while(found && list_craftresult->itemFits(0, crafted))
736 InventoryList saved_craft_list = *list_craft;
738 // Decrement input and add crafting output
739 getCraftingResult(inv_craft, crafted, true, gamedef);
740 PLAYER_TO_SA(player)->item_OnCraft(crafted, player, &saved_craft_list, craft_inv);
741 list_craftresult->addItem(0, crafted);
742 mgr->setInventoryModified(craft_inv);
744 actionstream<<player->getDescription()
746 <<crafted.getItemString()
750 if(count_remaining == 1)
752 else if(count_remaining > 1)
755 // Get next crafting result
756 found = getCraftingResult(inv_craft, crafted, false, gamedef);
757 PLAYER_TO_SA(player)->item_CraftPredict(crafted, player, list_craft, craft_inv);
758 found = !crafted.empty();
761 infostream<<"ICraftAction::apply(): crafted "
762 <<" craft_inv=\""<<craft_inv.dump()<<"\""
766 void ICraftAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
768 // Optional InventoryAction operation that is run on the client
769 // to make lag less apparent.
774 bool getCraftingResult(Inventory *inv, ItemStack& result,
775 bool decrementInput, IGameDef *gamedef)
777 DSTACK(__FUNCTION_NAME);
781 // Get the InventoryList in which we will operate
782 InventoryList *clist = inv->getList("craft");
786 // Mangle crafting grid to an another format
788 ci.method = CRAFT_METHOD_NORMAL;
789 ci.width = clist->getWidth() ? clist->getWidth() : 3;
790 for(u16 i=0; i<clist->getSize(); i++)
791 ci.items.push_back(clist->getItem(i));
793 // Find out what is crafted and add it to result item slot
795 bool found = gamedef->getCraftDefManager()->getCraftResult(
796 ci, co, decrementInput, gamedef);
798 result.deSerialize(co.item, gamedef->getItemDefManager());
800 if(found && decrementInput)
802 // CraftInput has been changed, apply changes in clist
803 for(u16 i=0; i<clist->getSize(); i++)
805 clist->changeItem(i, ci.items[i]);