3 Copyright (C) 2010-2011 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 "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 lua_State *L = player->getEnv()->getLua();
230 src_can_take_count = scriptapi_detached_inventory_allow_move(
231 L, from_inv.name, from_list, from_i,
232 to_list, to_i, try_take_count, player);
233 dst_can_put_count = src_can_take_count;
237 // Destination is detached
238 if(to_inv.type == InventoryLocation::DETACHED)
240 lua_State *L = player->getEnv()->getLua();
241 ItemStack src_item = list_from->getItem(from_i);
242 src_item.count = try_take_count;
243 dst_can_put_count = scriptapi_detached_inventory_allow_put(
244 L, to_inv.name, to_list, to_i, src_item, player);
246 // Source is detached
247 if(from_inv.type == InventoryLocation::DETACHED)
249 lua_State *L = player->getEnv()->getLua();
250 ItemStack src_item = list_from->getItem(from_i);
251 src_item.count = try_take_count;
252 src_can_take_count = scriptapi_detached_inventory_allow_take(
253 L, 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 lua_State *L = player->getEnv()->getLua();
266 src_can_take_count = scriptapi_nodemeta_inventory_allow_move(
267 L, from_inv.p, from_list, from_i,
268 to_list, to_i, try_take_count, player);
269 dst_can_put_count = src_can_take_count;
273 // Destination is nodemeta
274 if(to_inv.type == InventoryLocation::NODEMETA)
276 lua_State *L = player->getEnv()->getLua();
277 ItemStack src_item = list_from->getItem(from_i);
278 src_item.count = try_take_count;
279 dst_can_put_count = scriptapi_nodemeta_inventory_allow_put(
280 L, to_inv.p, to_list, to_i, src_item, player);
282 // Source is nodemeta
283 if(from_inv.type == InventoryLocation::NODEMETA)
285 lua_State *L = player->getEnv()->getLua();
286 ItemStack src_item = list_from->getItem(from_i);
287 src_item.count = try_take_count;
288 src_can_take_count = scriptapi_nodemeta_inventory_allow_take(
289 L, from_inv.p, from_list, from_i, src_item, player);
293 int old_count = count;
295 /* Modify count according to collected data */
296 count = try_take_count;
297 if(src_can_take_count != -1 && count > src_can_take_count)
298 count = src_can_take_count;
299 if(dst_can_put_count != -1 && count > dst_can_put_count)
300 count = dst_can_put_count;
301 /* Limit according to source item count */
302 if(count > list_from->getItem(from_i).count)
303 count = list_from->getItem(from_i).count;
305 /* If no items will be moved, don't go further */
308 infostream<<"IMoveAction::apply(): move was completely disallowed:"
309 <<" count="<<old_count
310 <<" from inv=\""<<from_inv.dump()<<"\""
311 <<" list=\""<<from_list<<"\""
313 <<" to inv=\""<<to_inv.dump()<<"\""
314 <<" list=\""<<to_list<<"\""
320 ItemStack src_item = list_from->getItem(from_i);
321 src_item.count = count;
322 ItemStack from_stack_was = list_from->getItem(from_i);
323 ItemStack to_stack_was = list_to->getItem(to_i);
328 If something is wrong (source item is empty, destination is the
329 same as source), nothing happens
331 list_from->moveItem(from_i, list_to, to_i, count);
333 // If source is infinite, reset it's stack
334 if(src_can_take_count == -1){
335 // If destination stack is of different type and there are leftover
336 // items, attempt to put the leftover items to a different place in the
337 // destination inventory.
338 // The client-side GUI will try to guess if this happens.
339 if(from_stack_was.name != to_stack_was.name){
340 for(u32 i=0; i<list_to->getSize(); i++){
341 if(list_to->getItem(i).empty()){
342 list_to->changeItem(i, to_stack_was);
347 list_from->deleteItem(from_i);
348 list_from->addItem(from_i, from_stack_was);
350 // If destination is infinite, reset it's stack and take count from source
351 if(dst_can_put_count == -1){
352 list_to->deleteItem(to_i);
353 list_to->addItem(to_i, to_stack_was);
354 list_from->deleteItem(from_i);
355 list_from->addItem(from_i, from_stack_was);
356 list_from->takeItem(from_i, count);
359 infostream<<"IMoveAction::apply(): moved"
361 <<" from inv=\""<<from_inv.dump()<<"\""
362 <<" list=\""<<from_list<<"\""
364 <<" to inv=\""<<to_inv.dump()<<"\""
365 <<" list=\""<<to_list<<"\""
370 Record rollback information
372 if(!ignore_rollback && gamedef->rollback())
374 IRollbackReportSink *rollback = gamedef->rollback();
376 // If source is not infinite, record item take
377 if(src_can_take_count != -1){
378 RollbackAction action;
381 std::ostringstream os(std::ios::binary);
382 from_inv.serialize(os);
385 action.setModifyInventoryStack(loc, from_list, from_i, false,
386 src_item.getItemString());
387 rollback->reportAction(action);
389 // If destination is not infinite, record item put
390 if(dst_can_put_count != -1){
391 RollbackAction action;
394 std::ostringstream os(std::ios::binary);
395 to_inv.serialize(os);
398 action.setModifyInventoryStack(loc, to_list, to_i, true,
399 src_item.getItemString());
400 rollback->reportAction(action);
405 Report move to endpoints
408 /* Detached inventories */
410 // Both endpoints are same detached
411 if(from_inv.type == InventoryLocation::DETACHED &&
412 to_inv.type == InventoryLocation::DETACHED &&
413 from_inv.name == to_inv.name)
415 lua_State *L = player->getEnv()->getLua();
416 scriptapi_detached_inventory_on_move(
417 L, from_inv.name, from_list, from_i,
418 to_list, to_i, count, player);
422 // Destination is detached
423 if(to_inv.type == InventoryLocation::DETACHED)
425 lua_State *L = player->getEnv()->getLua();
426 scriptapi_detached_inventory_on_put(
427 L, to_inv.name, to_list, to_i, src_item, player);
429 // Source is detached
430 if(from_inv.type == InventoryLocation::DETACHED)
432 lua_State *L = player->getEnv()->getLua();
433 scriptapi_detached_inventory_on_take(
434 L, from_inv.name, from_list, from_i, src_item, player);
438 /* Node metadata inventories */
440 // Both endpoints are same nodemeta
441 if(from_inv.type == InventoryLocation::NODEMETA &&
442 to_inv.type == InventoryLocation::NODEMETA &&
443 from_inv.p == to_inv.p)
445 lua_State *L = player->getEnv()->getLua();
446 scriptapi_nodemeta_inventory_on_move(
447 L, from_inv.p, from_list, from_i,
448 to_list, to_i, count, player);
451 // Destination is nodemeta
452 if(to_inv.type == InventoryLocation::NODEMETA)
454 lua_State *L = player->getEnv()->getLua();
455 scriptapi_nodemeta_inventory_on_put(
456 L, to_inv.p, to_list, to_i, src_item, player);
458 // Source is nodemeta
459 else if(from_inv.type == InventoryLocation::NODEMETA)
461 lua_State *L = player->getEnv()->getLua();
462 scriptapi_nodemeta_inventory_on_take(
463 L, from_inv.p, from_list, from_i, src_item, player);
467 mgr->setInventoryModified(from_inv);
468 if(inv_from != inv_to)
469 mgr->setInventoryModified(to_inv);
472 void IMoveAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
474 // Optional InventoryAction operation that is run on the client
475 // to make lag less apparent.
477 Inventory *inv_from = mgr->getInventory(from_inv);
478 Inventory *inv_to = mgr->getInventory(to_inv);
479 if(!inv_from || !inv_to)
482 InventoryLocation current_player;
483 current_player.setCurrentPlayer();
484 Inventory *inv_player = mgr->getInventory(current_player);
485 if(inv_from != inv_player || inv_to != inv_player)
488 InventoryList *list_from = inv_from->getList(from_list);
489 InventoryList *list_to = inv_to->getList(to_list);
490 if(!list_from || !list_to)
493 list_from->moveItem(from_i, list_to, to_i, count);
495 mgr->setInventoryModified(from_inv);
496 if(inv_from != inv_to)
497 mgr->setInventoryModified(to_inv);
504 IDropAction::IDropAction(std::istream &is)
508 std::getline(is, ts, ' ');
511 std::getline(is, ts, ' ');
512 from_inv.deSerialize(ts);
514 std::getline(is, from_list, ' ');
516 std::getline(is, ts, ' ');
520 void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
522 Inventory *inv_from = mgr->getInventory(from_inv);
525 infostream<<"IDropAction::apply(): FAIL: source inventory not found: "
526 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
530 InventoryList *list_from = inv_from->getList(from_list);
533 If a list doesn't exist or the source item doesn't exist
536 infostream<<"IDropAction::apply(): FAIL: source list not found: "
537 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
540 if(list_from->getItem(from_i).empty())
542 infostream<<"IDropAction::apply(): FAIL: source item not found: "
543 <<"from_inv=\""<<from_inv.dump()<<"\""
544 <<", from_list=\""<<from_list<<"\""
545 <<" from_i="<<from_i<<std::endl;
550 Do not handle rollback if inventory is player's
552 bool ignore_src_rollback = (from_inv.type == InventoryLocation::PLAYER);
555 Collect information of endpoints
558 int take_count = list_from->getItem(from_i).count;
559 if(count != 0 && count < take_count)
561 int src_can_take_count = take_count;
563 // Source is detached
564 if(from_inv.type == InventoryLocation::DETACHED)
566 lua_State *L = player->getEnv()->getLua();
567 ItemStack src_item = list_from->getItem(from_i);
568 src_item.count = take_count;
569 src_can_take_count = scriptapi_detached_inventory_allow_take(
570 L, from_inv.name, from_list, from_i, src_item, player);
573 // Source is nodemeta
574 if(from_inv.type == InventoryLocation::NODEMETA)
576 lua_State *L = player->getEnv()->getLua();
577 ItemStack src_item = list_from->getItem(from_i);
578 src_item.count = take_count;
579 src_can_take_count = scriptapi_nodemeta_inventory_allow_take(
580 L, from_inv.p, from_list, from_i, src_item, player);
583 if(src_can_take_count != -1 && src_can_take_count < take_count)
584 take_count = src_can_take_count;
586 int actually_dropped_count = 0;
588 ItemStack src_item = list_from->getItem(from_i);
591 ItemStack item1 = list_from->getItem(from_i);
592 item1.count = take_count;
593 if(scriptapi_item_on_drop(player->getEnv()->getLua(), item1, player,
594 player->getBasePosition() + v3f(0,1,0)))
596 actually_dropped_count = take_count - item1.count;
598 if(actually_dropped_count == 0){
599 infostream<<"Actually dropped no items"<<std::endl;
603 // If source isn't infinite
604 if(src_can_take_count != -1){
605 // Take item from source list
606 ItemStack item2 = list_from->takeItem(from_i, actually_dropped_count);
608 if(item2.count != actually_dropped_count)
609 errorstream<<"Could not take dropped count of items"<<std::endl;
611 mgr->setInventoryModified(from_inv);
615 infostream<<"IDropAction::apply(): dropped "
616 <<" from inv=\""<<from_inv.dump()<<"\""
617 <<" list=\""<<from_list<<"\""
621 src_item.count = actually_dropped_count;
624 Report drop to endpoints
627 // Source is detached
628 if(from_inv.type == InventoryLocation::DETACHED)
630 lua_State *L = player->getEnv()->getLua();
631 scriptapi_detached_inventory_on_take(
632 L, from_inv.name, from_list, from_i, src_item, player);
635 // Source is nodemeta
636 if(from_inv.type == InventoryLocation::NODEMETA)
638 lua_State *L = player->getEnv()->getLua();
639 scriptapi_nodemeta_inventory_on_take(
640 L, from_inv.p, from_list, from_i, src_item, player);
644 Record rollback information
646 if(!ignore_src_rollback && gamedef->rollback())
648 IRollbackReportSink *rollback = gamedef->rollback();
650 // If source is not infinite, record item take
651 if(src_can_take_count != -1){
652 RollbackAction action;
655 std::ostringstream os(std::ios::binary);
656 from_inv.serialize(os);
659 action.setModifyInventoryStack(loc, from_list, from_i,
660 false, src_item.getItemString());
661 rollback->reportAction(action);
666 void IDropAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
668 // Optional InventoryAction operation that is run on the client
669 // to make lag less apparent.
671 Inventory *inv_from = mgr->getInventory(from_inv);
675 InventoryLocation current_player;
676 current_player.setCurrentPlayer();
677 Inventory *inv_player = mgr->getInventory(current_player);
678 if(inv_from != inv_player)
681 InventoryList *list_from = inv_from->getList(from_list);
686 list_from->changeItem(from_i, ItemStack());
688 list_from->takeItem(from_i, count);
690 mgr->setInventoryModified(from_inv);
697 ICraftAction::ICraftAction(std::istream &is)
701 std::getline(is, ts, ' ');
704 std::getline(is, ts, ' ');
705 craft_inv.deSerialize(ts);
708 void ICraftAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
710 Inventory *inv_craft = mgr->getInventory(craft_inv);
713 infostream<<"ICraftAction::apply(): FAIL: inventory not found: "
714 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
718 InventoryList *list_craft = inv_craft->getList("craft");
719 InventoryList *list_craftresult = inv_craft->getList("craftresult");
722 If a list doesn't exist or the source item doesn't exist
725 infostream<<"ICraftAction::apply(): FAIL: craft list not found: "
726 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
729 if(!list_craftresult){
730 infostream<<"ICraftAction::apply(): FAIL: craftresult list not found: "
731 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
734 if(list_craftresult->getSize() < 1){
735 infostream<<"ICraftAction::apply(): FAIL: craftresult list too short: "
736 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
741 int count_remaining = count;
742 bool found = getCraftingResult(inv_craft, crafted, false, gamedef);
744 while(found && list_craftresult->itemFits(0, crafted))
746 // Decrement input and add crafting output
747 getCraftingResult(inv_craft, crafted, true, gamedef);
748 list_craftresult->addItem(0, crafted);
749 mgr->setInventoryModified(craft_inv);
751 actionstream<<player->getDescription()
753 <<crafted.getItemString()
757 if(count_remaining == 1)
759 else if(count_remaining > 1)
762 // Get next crafting result
763 found = getCraftingResult(inv_craft, crafted, false, gamedef);
766 infostream<<"ICraftAction::apply(): crafted "
767 <<" craft_inv=\""<<craft_inv.dump()<<"\""
771 void ICraftAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
773 // Optional InventoryAction operation that is run on the client
774 // to make lag less apparent.
779 bool getCraftingResult(Inventory *inv, ItemStack& result,
780 bool decrementInput, IGameDef *gamedef)
782 DSTACK(__FUNCTION_NAME);
786 // Get the InventoryList in which we will operate
787 InventoryList *clist = inv->getList("craft");
791 // Mangle crafting grid to an another format
793 ci.method = CRAFT_METHOD_NORMAL;
794 ci.width = clist->getWidth() ? clist->getWidth() : 3;
795 for(u16 i=0; i<clist->getSize(); i++)
796 ci.items.push_back(clist->getItem(i));
798 // Find out what is crafted and add it to result item slot
800 bool found = gamedef->getCraftDefManager()->getCraftResult(
801 ci, co, decrementInput, gamedef);
803 result.deSerialize(co.item, gamedef->getItemDefManager());
805 if(found && decrementInput)
807 // CraftInput has been changed, apply changes in clist
808 for(u16 i=0; i<clist->getSize(); i++)
810 clist->changeItem(i, ci.items[i]);