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 "cpp_api/scriptapi.h"
24 #include "serverobject.h"
25 #include "main.h" // for g_settings
28 #include "rollback_interface.h"
30 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
36 std::string InventoryLocation::dump() const
38 std::ostringstream os(std::ios::binary);
43 void InventoryLocation::serialize(std::ostream &os) const
46 case InventoryLocation::UNDEFINED:
49 case InventoryLocation::CURRENT_PLAYER:
52 case InventoryLocation::PLAYER:
55 case InventoryLocation::NODEMETA:
56 os<<"nodemeta:"<<p.X<<","<<p.Y<<","<<p.Z;
58 case InventoryLocation::DETACHED:
59 os<<"detached:"<<name;
66 void InventoryLocation::deSerialize(std::istream &is)
69 std::getline(is, tname, ':');
70 if(tname == "undefined")
72 type = InventoryLocation::UNDEFINED;
74 else if(tname == "current_player")
76 type = InventoryLocation::CURRENT_PLAYER;
78 else if(tname == "player")
80 type = InventoryLocation::PLAYER;
81 std::getline(is, name, '\n');
83 else if(tname == "nodemeta")
85 type = InventoryLocation::NODEMETA;
87 std::getline(is, pos, '\n');
89 p.X = stoi(fn.next(","));
90 p.Y = stoi(fn.next(","));
91 p.Z = stoi(fn.next(","));
93 else if(tname == "detached")
95 type = InventoryLocation::DETACHED;
96 std::getline(is, name, '\n');
100 infostream<<"Unknown InventoryLocation type=\""<<tname<<"\""<<std::endl;
101 throw SerializationError("Unknown InventoryLocation type");
105 void InventoryLocation::deSerialize(std::string s)
107 std::istringstream is(s, std::ios::binary);
115 InventoryAction * InventoryAction::deSerialize(std::istream &is)
118 std::getline(is, type, ' ');
120 InventoryAction *a = NULL;
124 a = new IMoveAction(is);
126 else if(type == "Drop")
128 a = new IDropAction(is);
130 else if(type == "Craft")
132 a = new ICraftAction(is);
142 IMoveAction::IMoveAction(std::istream &is)
146 std::getline(is, ts, ' ');
149 std::getline(is, ts, ' ');
150 from_inv.deSerialize(ts);
152 std::getline(is, from_list, ' ');
154 std::getline(is, ts, ' ');
157 std::getline(is, ts, ' ');
158 to_inv.deSerialize(ts);
160 std::getline(is, to_list, ' ');
162 std::getline(is, ts, ' ');
166 void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
168 Inventory *inv_from = mgr->getInventory(from_inv);
169 Inventory *inv_to = mgr->getInventory(to_inv);
172 infostream<<"IMoveAction::apply(): FAIL: source inventory not found: "
173 <<"from_inv=\""<<from_inv.dump()<<"\""
174 <<", to_inv=\""<<to_inv.dump()<<"\""<<std::endl;
178 infostream<<"IMoveAction::apply(): FAIL: destination inventory not found: "
179 <<"from_inv=\""<<from_inv.dump()<<"\""
180 <<", to_inv=\""<<to_inv.dump()<<"\""<<std::endl;
184 InventoryList *list_from = inv_from->getList(from_list);
185 InventoryList *list_to = inv_to->getList(to_list);
188 If a list doesn't exist or the source item doesn't exist
191 infostream<<"IMoveAction::apply(): FAIL: source list not found: "
192 <<"from_inv=\""<<from_inv.dump()<<"\""
193 <<", from_list=\""<<from_list<<"\""<<std::endl;
197 infostream<<"IMoveAction::apply(): FAIL: destination list not found: "
198 <<"to_inv=\""<<to_inv.dump()<<"\""
199 <<", to_list=\""<<to_list<<"\""<<std::endl;
204 Do not handle rollback if both inventories are that of the same player
206 bool ignore_rollback = (
207 from_inv.type == InventoryLocation::PLAYER &&
208 to_inv.type == InventoryLocation::PLAYER &&
209 from_inv.name == to_inv.name);
212 Collect information of endpoints
215 int try_take_count = count;
216 if(try_take_count == 0)
217 try_take_count = list_from->getItem(from_i).count;
219 int src_can_take_count = 0xffff;
220 int dst_can_put_count = 0xffff;
222 /* Query detached inventories */
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)
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;
236 // Destination is detached
237 if(to_inv.type == InventoryLocation::DETACHED)
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);
244 // Source is detached
245 if(from_inv.type == InventoryLocation::DETACHED)
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);
254 /* Query node metadata inventories */
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)
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;
269 // Destination is nodemeta
270 if(to_inv.type == InventoryLocation::NODEMETA)
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);
277 // Source is nodemeta
278 if(from_inv.type == InventoryLocation::NODEMETA)
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);
287 int old_count = count;
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;
299 /* If no items will be moved, don't go further */
302 infostream<<"IMoveAction::apply(): move was completely disallowed:"
303 <<" count="<<old_count
304 <<" from inv=\""<<from_inv.dump()<<"\""
305 <<" list=\""<<from_list<<"\""
307 <<" to inv=\""<<to_inv.dump()<<"\""
308 <<" list=\""<<to_list<<"\""
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);
322 If something is wrong (source item is empty, destination is the
323 same as source), nothing happens
325 list_from->moveItem(from_i, list_to, to_i, count);
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);
341 list_from->deleteItem(from_i);
342 list_from->addItem(from_i, from_stack_was);
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);
353 infostream<<"IMoveAction::apply(): moved"
355 <<" from inv=\""<<from_inv.dump()<<"\""
356 <<" list=\""<<from_list<<"\""
358 <<" to inv=\""<<to_inv.dump()<<"\""
359 <<" list=\""<<to_list<<"\""
364 Record rollback information
366 if(!ignore_rollback && gamedef->rollback())
368 IRollbackReportSink *rollback = gamedef->rollback();
370 // If source is not infinite, record item take
371 if(src_can_take_count != -1){
372 RollbackAction action;
375 std::ostringstream os(std::ios::binary);
376 from_inv.serialize(os);
379 action.setModifyInventoryStack(loc, from_list, from_i, false,
380 src_item.getItemString());
381 rollback->reportAction(action);
383 // If destination is not infinite, record item put
384 if(dst_can_put_count != -1){
385 RollbackAction action;
388 std::ostringstream os(std::ios::binary);
389 to_inv.serialize(os);
392 action.setModifyInventoryStack(loc, to_list, to_i, true,
393 src_item.getItemString());
394 rollback->reportAction(action);
399 Report move to endpoints
402 /* Detached inventories */
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)
409 PLAYER_TO_SA(player)->detached_inventory_OnMove(
410 from_inv.name, from_list, from_i,
411 to_list, to_i, count, player);
415 // Destination is detached
416 if(to_inv.type == InventoryLocation::DETACHED)
418 PLAYER_TO_SA(player)->detached_inventory_OnPut(
419 to_inv.name, to_list, to_i, src_item, player);
421 // Source is detached
422 if(from_inv.type == InventoryLocation::DETACHED)
424 PLAYER_TO_SA(player)->detached_inventory_OnTake(
425 from_inv.name, from_list, from_i, src_item, player);
429 /* Node metadata inventories */
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)
436 PLAYER_TO_SA(player)->nodemeta_inventory_OnMove(
437 from_inv.p, from_list, from_i,
438 to_list, to_i, count, player);
441 // Destination is nodemeta
442 if(to_inv.type == InventoryLocation::NODEMETA)
444 PLAYER_TO_SA(player)->nodemeta_inventory_OnPut(
445 to_inv.p, to_list, to_i, src_item, player);
447 // Source is nodemeta
448 else if(from_inv.type == InventoryLocation::NODEMETA)
450 PLAYER_TO_SA(player)->nodemeta_inventory_OnTake(
451 from_inv.p, from_list, from_i, src_item, player);
455 mgr->setInventoryModified(from_inv);
456 if(inv_from != inv_to)
457 mgr->setInventoryModified(to_inv);
460 void IMoveAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
462 // Optional InventoryAction operation that is run on the client
463 // to make lag less apparent.
465 Inventory *inv_from = mgr->getInventory(from_inv);
466 Inventory *inv_to = mgr->getInventory(to_inv);
467 if(!inv_from || !inv_to)
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)
476 InventoryList *list_from = inv_from->getList(from_list);
477 InventoryList *list_to = inv_to->getList(to_list);
478 if(!list_from || !list_to)
481 list_from->moveItem(from_i, list_to, to_i, count);
483 mgr->setInventoryModified(from_inv);
484 if(inv_from != inv_to)
485 mgr->setInventoryModified(to_inv);
492 IDropAction::IDropAction(std::istream &is)
496 std::getline(is, ts, ' ');
499 std::getline(is, ts, ' ');
500 from_inv.deSerialize(ts);
502 std::getline(is, from_list, ' ');
504 std::getline(is, ts, ' ');
508 void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
510 Inventory *inv_from = mgr->getInventory(from_inv);
513 infostream<<"IDropAction::apply(): FAIL: source inventory not found: "
514 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
518 InventoryList *list_from = inv_from->getList(from_list);
521 If a list doesn't exist or the source item doesn't exist
524 infostream<<"IDropAction::apply(): FAIL: source list not found: "
525 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
528 if(list_from->getItem(from_i).empty())
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;
538 Do not handle rollback if inventory is player's
540 bool ignore_src_rollback = (from_inv.type == InventoryLocation::PLAYER);
543 Collect information of endpoints
546 int take_count = list_from->getItem(from_i).count;
547 if(count != 0 && count < take_count)
549 int src_can_take_count = take_count;
551 // Source is detached
552 if(from_inv.type == InventoryLocation::DETACHED)
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);
560 // Source is nodemeta
561 if(from_inv.type == InventoryLocation::NODEMETA)
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);
569 if(src_can_take_count != -1 && src_can_take_count < take_count)
570 take_count = src_can_take_count;
572 int actually_dropped_count = 0;
574 ItemStack src_item = list_from->getItem(from_i);
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)))
582 actually_dropped_count = take_count - item1.count;
584 if(actually_dropped_count == 0){
585 infostream<<"Actually dropped no items"<<std::endl;
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);
594 if(item2.count != actually_dropped_count)
595 errorstream<<"Could not take dropped count of items"<<std::endl;
597 mgr->setInventoryModified(from_inv);
601 infostream<<"IDropAction::apply(): dropped "
602 <<" from inv=\""<<from_inv.dump()<<"\""
603 <<" list=\""<<from_list<<"\""
607 src_item.count = actually_dropped_count;
610 Report drop to endpoints
613 // Source is detached
614 if(from_inv.type == InventoryLocation::DETACHED)
616 PLAYER_TO_SA(player)->detached_inventory_OnTake(
617 from_inv.name, from_list, from_i, src_item, player);
620 // Source is nodemeta
621 if(from_inv.type == InventoryLocation::NODEMETA)
623 PLAYER_TO_SA(player)->nodemeta_inventory_OnTake(
624 from_inv.p, from_list, from_i, src_item, player);
628 Record rollback information
630 if(!ignore_src_rollback && gamedef->rollback())
632 IRollbackReportSink *rollback = gamedef->rollback();
634 // If source is not infinite, record item take
635 if(src_can_take_count != -1){
636 RollbackAction action;
639 std::ostringstream os(std::ios::binary);
640 from_inv.serialize(os);
643 action.setModifyInventoryStack(loc, from_list, from_i,
644 false, src_item.getItemString());
645 rollback->reportAction(action);
650 void IDropAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
652 // Optional InventoryAction operation that is run on the client
653 // to make lag less apparent.
655 Inventory *inv_from = mgr->getInventory(from_inv);
659 InventoryLocation current_player;
660 current_player.setCurrentPlayer();
661 Inventory *inv_player = mgr->getInventory(current_player);
662 if(inv_from != inv_player)
665 InventoryList *list_from = inv_from->getList(from_list);
670 list_from->changeItem(from_i, ItemStack());
672 list_from->takeItem(from_i, count);
674 mgr->setInventoryModified(from_inv);
681 ICraftAction::ICraftAction(std::istream &is)
685 std::getline(is, ts, ' ');
688 std::getline(is, ts, ' ');
689 craft_inv.deSerialize(ts);
692 void ICraftAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
694 Inventory *inv_craft = mgr->getInventory(craft_inv);
697 infostream<<"ICraftAction::apply(): FAIL: inventory not found: "
698 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
702 InventoryList *list_craft = inv_craft->getList("craft");
703 InventoryList *list_craftresult = inv_craft->getList("craftresult");
706 If a list doesn't exist or the source item doesn't exist
709 infostream<<"ICraftAction::apply(): FAIL: craft list not found: "
710 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
713 if(!list_craftresult){
714 infostream<<"ICraftAction::apply(): FAIL: craftresult list not found: "
715 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
718 if(list_craftresult->getSize() < 1){
719 infostream<<"ICraftAction::apply(): FAIL: craftresult list too short: "
720 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
725 int count_remaining = count;
726 bool found = getCraftingResult(inv_craft, crafted, false, gamedef);
728 while(found && list_craftresult->itemFits(0, crafted))
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);
735 actionstream<<player->getDescription()
737 <<crafted.getItemString()
741 if(count_remaining == 1)
743 else if(count_remaining > 1)
746 // Get next crafting result
747 found = getCraftingResult(inv_craft, crafted, false, gamedef);
750 infostream<<"ICraftAction::apply(): crafted "
751 <<" craft_inv=\""<<craft_inv.dump()<<"\""
755 void ICraftAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
757 // Optional InventoryAction operation that is run on the client
758 // to make lag less apparent.
763 bool getCraftingResult(Inventory *inv, ItemStack& result,
764 bool decrementInput, IGameDef *gamedef)
766 DSTACK(__FUNCTION_NAME);
770 // Get the InventoryList in which we will operate
771 InventoryList *clist = inv->getList("craft");
775 // Mangle crafting grid to an another format
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));
782 // Find out what is crafted and add it to result item slot
784 bool found = gamedef->getCraftDefManager()->getCraftResult(
785 ci, co, decrementInput, gamedef);
787 result.deSerialize(co.item, gamedef->getItemDefManager());
789 if(found && decrementInput)
791 // CraftInput has been changed, apply changes in clist
792 for(u16 i=0; i<clist->getSize(); i++)
794 clist->changeItem(i, ci.items[i]);