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"
30 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
32 #define PLAYER_TO_SA(p) p->getEnv()->getScriptIface()
38 std::string InventoryLocation::dump() const
40 std::ostringstream os(std::ios::binary);
45 void InventoryLocation::serialize(std::ostream &os) const
48 case InventoryLocation::UNDEFINED:
51 case InventoryLocation::CURRENT_PLAYER:
54 case InventoryLocation::PLAYER:
57 case InventoryLocation::NODEMETA:
58 os<<"nodemeta:"<<p.X<<","<<p.Y<<","<<p.Z;
60 case InventoryLocation::DETACHED:
61 os<<"detached:"<<name;
68 void InventoryLocation::deSerialize(std::istream &is)
71 std::getline(is, tname, ':');
72 if(tname == "undefined")
74 type = InventoryLocation::UNDEFINED;
76 else if(tname == "current_player")
78 type = InventoryLocation::CURRENT_PLAYER;
80 else if(tname == "player")
82 type = InventoryLocation::PLAYER;
83 std::getline(is, name, '\n');
85 else if(tname == "nodemeta")
87 type = InventoryLocation::NODEMETA;
89 std::getline(is, pos, '\n');
91 p.X = stoi(fn.next(","));
92 p.Y = stoi(fn.next(","));
93 p.Z = stoi(fn.next(","));
95 else if(tname == "detached")
97 type = InventoryLocation::DETACHED;
98 std::getline(is, name, '\n');
102 infostream<<"Unknown InventoryLocation type=\""<<tname<<"\""<<std::endl;
103 throw SerializationError("Unknown InventoryLocation type");
107 void InventoryLocation::deSerialize(std::string s)
109 std::istringstream is(s, std::ios::binary);
117 InventoryAction * InventoryAction::deSerialize(std::istream &is)
120 std::getline(is, type, ' ');
122 InventoryAction *a = NULL;
126 a = new IMoveAction(is);
128 else if(type == "Drop")
130 a = new IDropAction(is);
132 else if(type == "Craft")
134 a = new ICraftAction(is);
144 IMoveAction::IMoveAction(std::istream &is)
148 std::getline(is, ts, ' ');
151 std::getline(is, ts, ' ');
152 from_inv.deSerialize(ts);
154 std::getline(is, from_list, ' ');
156 std::getline(is, ts, ' ');
159 std::getline(is, ts, ' ');
160 to_inv.deSerialize(ts);
162 std::getline(is, to_list, ' ');
164 std::getline(is, ts, ' ');
168 void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
170 Inventory *inv_from = mgr->getInventory(from_inv);
171 Inventory *inv_to = mgr->getInventory(to_inv);
174 infostream<<"IMoveAction::apply(): FAIL: source inventory not found: "
175 <<"from_inv=\""<<from_inv.dump()<<"\""
176 <<", to_inv=\""<<to_inv.dump()<<"\""<<std::endl;
180 infostream<<"IMoveAction::apply(): FAIL: destination inventory not found: "
181 <<"from_inv=\""<<from_inv.dump()<<"\""
182 <<", to_inv=\""<<to_inv.dump()<<"\""<<std::endl;
186 InventoryList *list_from = inv_from->getList(from_list);
187 InventoryList *list_to = inv_to->getList(to_list);
190 If a list doesn't exist or the source item doesn't exist
193 infostream<<"IMoveAction::apply(): FAIL: source list not found: "
194 <<"from_inv=\""<<from_inv.dump()<<"\""
195 <<", from_list=\""<<from_list<<"\""<<std::endl;
199 infostream<<"IMoveAction::apply(): FAIL: destination list not found: "
200 <<"to_inv=\""<<to_inv.dump()<<"\""
201 <<", to_list=\""<<to_list<<"\""<<std::endl;
206 Do not handle rollback if both inventories are that of the same player
208 bool ignore_rollback = (
209 from_inv.type == InventoryLocation::PLAYER &&
210 to_inv.type == InventoryLocation::PLAYER &&
211 from_inv.name == to_inv.name);
214 Collect information of endpoints
217 int try_take_count = count;
218 if(try_take_count == 0)
219 try_take_count = list_from->getItem(from_i).count;
221 int src_can_take_count = 0xffff;
222 int dst_can_put_count = 0xffff;
224 /* Query detached inventories */
226 // Move occurs in the same detached inventory
227 if(from_inv.type == InventoryLocation::DETACHED &&
228 to_inv.type == InventoryLocation::DETACHED &&
229 from_inv.name == to_inv.name)
231 src_can_take_count = PLAYER_TO_SA(player)->detached_inventory_AllowMove(
232 from_inv.name, from_list, from_i,
233 to_list, to_i, try_take_count, player);
234 dst_can_put_count = src_can_take_count;
238 // Destination is detached
239 if(to_inv.type == InventoryLocation::DETACHED)
241 ItemStack src_item = list_from->getItem(from_i);
242 src_item.count = try_take_count;
243 dst_can_put_count = PLAYER_TO_SA(player)->detached_inventory_AllowPut(
244 to_inv.name, to_list, to_i, src_item, player);
246 // Source is detached
247 if(from_inv.type == InventoryLocation::DETACHED)
249 ItemStack src_item = list_from->getItem(from_i);
250 src_item.count = try_take_count;
251 src_can_take_count = PLAYER_TO_SA(player)->detached_inventory_AllowTake(
252 from_inv.name, from_list, from_i, src_item, player);
256 /* Query node metadata inventories */
258 // Both endpoints are nodemeta
259 // Move occurs in the same nodemeta inventory
260 if(from_inv.type == InventoryLocation::NODEMETA &&
261 to_inv.type == InventoryLocation::NODEMETA &&
262 from_inv.p == to_inv.p)
264 src_can_take_count = PLAYER_TO_SA(player)->nodemeta_inventory_AllowMove(
265 from_inv.p, from_list, from_i,
266 to_list, to_i, try_take_count, player);
267 dst_can_put_count = src_can_take_count;
271 // Destination is nodemeta
272 if(to_inv.type == InventoryLocation::NODEMETA)
274 ItemStack src_item = list_from->getItem(from_i);
275 src_item.count = try_take_count;
276 dst_can_put_count = PLAYER_TO_SA(player)->nodemeta_inventory_AllowPut(
277 to_inv.p, to_list, to_i, src_item, player);
279 // Source is nodemeta
280 if(from_inv.type == InventoryLocation::NODEMETA)
282 ItemStack src_item = list_from->getItem(from_i);
283 src_item.count = try_take_count;
284 src_can_take_count = PLAYER_TO_SA(player)->nodemeta_inventory_AllowTake(
285 from_inv.p, from_list, from_i, src_item, player);
289 int old_count = count;
291 /* Modify count according to collected data */
292 count = try_take_count;
293 if(src_can_take_count != -1 && count > src_can_take_count)
294 count = src_can_take_count;
295 if(dst_can_put_count != -1 && count > dst_can_put_count)
296 count = dst_can_put_count;
297 /* Limit according to source item count */
298 if(count > list_from->getItem(from_i).count)
299 count = list_from->getItem(from_i).count;
301 /* If no items will be moved, don't go further */
304 infostream<<"IMoveAction::apply(): move was completely disallowed:"
305 <<" count="<<old_count
306 <<" from inv=\""<<from_inv.dump()<<"\""
307 <<" list=\""<<from_list<<"\""
309 <<" to inv=\""<<to_inv.dump()<<"\""
310 <<" list=\""<<to_list<<"\""
316 ItemStack src_item = list_from->getItem(from_i);
317 src_item.count = count;
318 ItemStack from_stack_was = list_from->getItem(from_i);
319 ItemStack to_stack_was = list_to->getItem(to_i);
324 If something is wrong (source item is empty, destination is the
325 same as source), nothing happens
327 list_from->moveItem(from_i, list_to, to_i, count);
329 // If source is infinite, reset it's stack
330 if(src_can_take_count == -1){
331 // If destination stack is of different type and there are leftover
332 // items, attempt to put the leftover items to a different place in the
333 // destination inventory.
334 // The client-side GUI will try to guess if this happens.
335 if(from_stack_was.name != to_stack_was.name){
336 for(u32 i=0; i<list_to->getSize(); i++){
337 if(list_to->getItem(i).empty()){
338 list_to->changeItem(i, to_stack_was);
343 list_from->deleteItem(from_i);
344 list_from->addItem(from_i, from_stack_was);
346 // If destination is infinite, reset it's stack and take count from source
347 if(dst_can_put_count == -1){
348 list_to->deleteItem(to_i);
349 list_to->addItem(to_i, to_stack_was);
350 list_from->deleteItem(from_i);
351 list_from->addItem(from_i, from_stack_was);
352 list_from->takeItem(from_i, count);
355 infostream<<"IMoveAction::apply(): moved"
357 <<" from inv=\""<<from_inv.dump()<<"\""
358 <<" list=\""<<from_list<<"\""
360 <<" to inv=\""<<to_inv.dump()<<"\""
361 <<" list=\""<<to_list<<"\""
366 Record rollback information
368 if(!ignore_rollback && gamedef->rollback())
370 IRollbackReportSink *rollback = gamedef->rollback();
372 // If source is not infinite, record item take
373 if(src_can_take_count != -1){
374 RollbackAction action;
377 std::ostringstream os(std::ios::binary);
378 from_inv.serialize(os);
381 action.setModifyInventoryStack(loc, from_list, from_i, false,
382 src_item.getItemString());
383 rollback->reportAction(action);
385 // If destination is not infinite, record item put
386 if(dst_can_put_count != -1){
387 RollbackAction action;
390 std::ostringstream os(std::ios::binary);
391 to_inv.serialize(os);
394 action.setModifyInventoryStack(loc, to_list, to_i, true,
395 src_item.getItemString());
396 rollback->reportAction(action);
401 Report move to endpoints
404 /* Detached inventories */
406 // Both endpoints are same detached
407 if(from_inv.type == InventoryLocation::DETACHED &&
408 to_inv.type == InventoryLocation::DETACHED &&
409 from_inv.name == to_inv.name)
411 PLAYER_TO_SA(player)->detached_inventory_OnMove(
412 from_inv.name, from_list, from_i,
413 to_list, to_i, count, player);
417 // Destination is detached
418 if(to_inv.type == InventoryLocation::DETACHED)
420 PLAYER_TO_SA(player)->detached_inventory_OnPut(
421 to_inv.name, to_list, to_i, src_item, player);
423 // Source is detached
424 if(from_inv.type == InventoryLocation::DETACHED)
426 PLAYER_TO_SA(player)->detached_inventory_OnTake(
427 from_inv.name, from_list, from_i, src_item, player);
431 /* Node metadata inventories */
433 // Both endpoints are same nodemeta
434 if(from_inv.type == InventoryLocation::NODEMETA &&
435 to_inv.type == InventoryLocation::NODEMETA &&
436 from_inv.p == to_inv.p)
438 PLAYER_TO_SA(player)->nodemeta_inventory_OnMove(
439 from_inv.p, from_list, from_i,
440 to_list, to_i, count, player);
443 // Destination is nodemeta
444 if(to_inv.type == InventoryLocation::NODEMETA)
446 PLAYER_TO_SA(player)->nodemeta_inventory_OnPut(
447 to_inv.p, to_list, to_i, src_item, player);
449 // Source is nodemeta
450 else if(from_inv.type == InventoryLocation::NODEMETA)
452 PLAYER_TO_SA(player)->nodemeta_inventory_OnTake(
453 from_inv.p, from_list, from_i, src_item, player);
457 mgr->setInventoryModified(from_inv);
458 if(inv_from != inv_to)
459 mgr->setInventoryModified(to_inv);
462 void IMoveAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
464 // Optional InventoryAction operation that is run on the client
465 // to make lag less apparent.
467 Inventory *inv_from = mgr->getInventory(from_inv);
468 Inventory *inv_to = mgr->getInventory(to_inv);
469 if(!inv_from || !inv_to)
472 InventoryLocation current_player;
473 current_player.setCurrentPlayer();
474 Inventory *inv_player = mgr->getInventory(current_player);
475 if(inv_from != inv_player || inv_to != inv_player)
478 InventoryList *list_from = inv_from->getList(from_list);
479 InventoryList *list_to = inv_to->getList(to_list);
480 if(!list_from || !list_to)
483 list_from->moveItem(from_i, list_to, to_i, count);
485 mgr->setInventoryModified(from_inv);
486 if(inv_from != inv_to)
487 mgr->setInventoryModified(to_inv);
494 IDropAction::IDropAction(std::istream &is)
498 std::getline(is, ts, ' ');
501 std::getline(is, ts, ' ');
502 from_inv.deSerialize(ts);
504 std::getline(is, from_list, ' ');
506 std::getline(is, ts, ' ');
510 void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
512 Inventory *inv_from = mgr->getInventory(from_inv);
515 infostream<<"IDropAction::apply(): FAIL: source inventory not found: "
516 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
520 InventoryList *list_from = inv_from->getList(from_list);
523 If a list doesn't exist or the source item doesn't exist
526 infostream<<"IDropAction::apply(): FAIL: source list not found: "
527 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
530 if(list_from->getItem(from_i).empty())
532 infostream<<"IDropAction::apply(): FAIL: source item not found: "
533 <<"from_inv=\""<<from_inv.dump()<<"\""
534 <<", from_list=\""<<from_list<<"\""
535 <<" from_i="<<from_i<<std::endl;
540 Do not handle rollback if inventory is player's
542 bool ignore_src_rollback = (from_inv.type == InventoryLocation::PLAYER);
545 Collect information of endpoints
548 int take_count = list_from->getItem(from_i).count;
549 if(count != 0 && count < take_count)
551 int src_can_take_count = take_count;
553 // Source is detached
554 if(from_inv.type == InventoryLocation::DETACHED)
556 ItemStack src_item = list_from->getItem(from_i);
557 src_item.count = take_count;
558 src_can_take_count = PLAYER_TO_SA(player)->detached_inventory_AllowTake(
559 from_inv.name, from_list, from_i, src_item, player);
562 // Source is nodemeta
563 if(from_inv.type == InventoryLocation::NODEMETA)
565 ItemStack src_item = list_from->getItem(from_i);
566 src_item.count = take_count;
567 src_can_take_count = PLAYER_TO_SA(player)->nodemeta_inventory_AllowTake(
568 from_inv.p, from_list, from_i, src_item, player);
571 if(src_can_take_count != -1 && src_can_take_count < take_count)
572 take_count = src_can_take_count;
574 int actually_dropped_count = 0;
576 ItemStack src_item = list_from->getItem(from_i);
579 ItemStack item1 = list_from->getItem(from_i);
580 item1.count = take_count;
581 if(PLAYER_TO_SA(player)->item_OnDrop(item1, player,
582 player->getBasePosition() + v3f(0,1,0)))
584 actually_dropped_count = take_count - item1.count;
586 if(actually_dropped_count == 0){
587 infostream<<"Actually dropped no items"<<std::endl;
591 // If source isn't infinite
592 if(src_can_take_count != -1){
593 // Take item from source list
594 ItemStack item2 = list_from->takeItem(from_i, actually_dropped_count);
596 if(item2.count != actually_dropped_count)
597 errorstream<<"Could not take dropped count of items"<<std::endl;
599 mgr->setInventoryModified(from_inv);
603 infostream<<"IDropAction::apply(): dropped "
604 <<" from inv=\""<<from_inv.dump()<<"\""
605 <<" list=\""<<from_list<<"\""
609 src_item.count = actually_dropped_count;
612 Report drop to endpoints
615 // Source is detached
616 if(from_inv.type == InventoryLocation::DETACHED)
618 PLAYER_TO_SA(player)->detached_inventory_OnTake(
619 from_inv.name, from_list, from_i, src_item, player);
622 // Source is nodemeta
623 if(from_inv.type == InventoryLocation::NODEMETA)
625 PLAYER_TO_SA(player)->nodemeta_inventory_OnTake(
626 from_inv.p, from_list, from_i, src_item, player);
630 Record rollback information
632 if(!ignore_src_rollback && gamedef->rollback())
634 IRollbackReportSink *rollback = gamedef->rollback();
636 // If source is not infinite, record item take
637 if(src_can_take_count != -1){
638 RollbackAction action;
641 std::ostringstream os(std::ios::binary);
642 from_inv.serialize(os);
645 action.setModifyInventoryStack(loc, from_list, from_i,
646 false, src_item.getItemString());
647 rollback->reportAction(action);
652 void IDropAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
654 // Optional InventoryAction operation that is run on the client
655 // to make lag less apparent.
657 Inventory *inv_from = mgr->getInventory(from_inv);
661 InventoryLocation current_player;
662 current_player.setCurrentPlayer();
663 Inventory *inv_player = mgr->getInventory(current_player);
664 if(inv_from != inv_player)
667 InventoryList *list_from = inv_from->getList(from_list);
672 list_from->changeItem(from_i, ItemStack());
674 list_from->takeItem(from_i, count);
676 mgr->setInventoryModified(from_inv);
683 ICraftAction::ICraftAction(std::istream &is)
687 std::getline(is, ts, ' ');
690 std::getline(is, ts, ' ');
691 craft_inv.deSerialize(ts);
694 void ICraftAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
696 Inventory *inv_craft = mgr->getInventory(craft_inv);
699 infostream<<"ICraftAction::apply(): FAIL: inventory not found: "
700 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
704 InventoryList *list_craft = inv_craft->getList("craft");
705 InventoryList *list_craftresult = inv_craft->getList("craftresult");
708 If a list doesn't exist or the source item doesn't exist
711 infostream<<"ICraftAction::apply(): FAIL: craft list not found: "
712 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
715 if(!list_craftresult){
716 infostream<<"ICraftAction::apply(): FAIL: craftresult list not found: "
717 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
720 if(list_craftresult->getSize() < 1){
721 infostream<<"ICraftAction::apply(): FAIL: craftresult list too short: "
722 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
727 ItemStack craftresultitem;
728 int count_remaining = count;
729 bool found = getCraftingResult(inv_craft, crafted, false, gamedef);
730 PLAYER_TO_SA(player)->item_CraftPredict(crafted, player, list_craft, craft_inv);
731 found = !crafted.empty();
733 while(found && list_craftresult->itemFits(0, crafted))
735 InventoryList saved_craft_list = *list_craft;
737 // Decrement input and add crafting output
738 getCraftingResult(inv_craft, crafted, true, gamedef);
739 PLAYER_TO_SA(player)->item_OnCraft(crafted, player, &saved_craft_list, craft_inv);
740 list_craftresult->addItem(0, crafted);
741 mgr->setInventoryModified(craft_inv);
743 actionstream<<player->getDescription()
745 <<crafted.getItemString()
749 if(count_remaining == 1)
751 else if(count_remaining > 1)
754 // Get next crafting result
755 found = getCraftingResult(inv_craft, crafted, false, gamedef);
756 PLAYER_TO_SA(player)->item_CraftPredict(crafted, player, list_craft, craft_inv);
757 found = !crafted.empty();
760 infostream<<"ICraftAction::apply(): crafted "
761 <<" craft_inv=\""<<craft_inv.dump()<<"\""
765 void ICraftAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
767 // Optional InventoryAction operation that is run on the client
768 // to make lag less apparent.
773 bool getCraftingResult(Inventory *inv, ItemStack& result,
774 bool decrementInput, IGameDef *gamedef)
776 DSTACK(__FUNCTION_NAME);
780 // Get the InventoryList in which we will operate
781 InventoryList *clist = inv->getList("craft");
785 // Mangle crafting grid to an another format
787 ci.method = CRAFT_METHOD_NORMAL;
788 ci.width = clist->getWidth() ? clist->getWidth() : 3;
789 for(u16 i=0; i<clist->getSize(); i++)
790 ci.items.push_back(clist->getItem(i));
792 // Find out what is crafted and add it to result item slot
794 bool found = gamedef->getCraftDefManager()->getCraftResult(
795 ci, co, decrementInput, gamedef);
797 result.deSerialize(co.item, gamedef->getItemDefManager());
799 if(found && decrementInput)
801 // CraftInput has been changed, apply changes in clist
802 for(u16 i=0; i<clist->getSize(); i++)
804 clist->changeItem(i, ci.items[i]);