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->takeItem(from_i, count);
357 infostream<<"IMoveAction::apply(): moved"
359 <<" from inv=\""<<from_inv.dump()<<"\""
360 <<" list=\""<<from_list<<"\""
362 <<" to inv=\""<<to_inv.dump()<<"\""
363 <<" list=\""<<to_list<<"\""
368 Record rollback information
370 if(!ignore_rollback && gamedef->rollback())
372 IRollbackReportSink *rollback = gamedef->rollback();
374 // If source is not infinite, record item take
375 if(src_can_take_count != -1){
376 RollbackAction action;
379 std::ostringstream os(std::ios::binary);
380 from_inv.serialize(os);
383 action.setModifyInventoryStack(loc, from_list, from_i, false,
384 src_item.getItemString());
385 rollback->reportAction(action);
387 // If destination is not infinite, record item put
388 if(dst_can_put_count != -1){
389 RollbackAction action;
392 std::ostringstream os(std::ios::binary);
393 to_inv.serialize(os);
396 action.setModifyInventoryStack(loc, to_list, to_i, true,
397 src_item.getItemString());
398 rollback->reportAction(action);
403 Report move to endpoints
406 /* Detached inventories */
408 // Both endpoints are same detached
409 if(from_inv.type == InventoryLocation::DETACHED &&
410 to_inv.type == InventoryLocation::DETACHED &&
411 from_inv.name == to_inv.name)
413 lua_State *L = player->getEnv()->getLua();
414 scriptapi_detached_inventory_on_move(
415 L, from_inv.name, from_list, from_i,
416 to_list, to_i, count, player);
420 // Destination is detached
421 if(to_inv.type == InventoryLocation::DETACHED)
423 lua_State *L = player->getEnv()->getLua();
424 scriptapi_detached_inventory_on_put(
425 L, to_inv.name, to_list, to_i, src_item, player);
427 // Source is detached
428 if(from_inv.type == InventoryLocation::DETACHED)
430 lua_State *L = player->getEnv()->getLua();
431 scriptapi_detached_inventory_on_take(
432 L, from_inv.name, from_list, from_i, src_item, player);
436 /* Node metadata inventories */
438 // Both endpoints are same nodemeta
439 if(from_inv.type == InventoryLocation::NODEMETA &&
440 to_inv.type == InventoryLocation::NODEMETA &&
441 from_inv.p == to_inv.p)
443 lua_State *L = player->getEnv()->getLua();
444 scriptapi_nodemeta_inventory_on_move(
445 L, from_inv.p, from_list, from_i,
446 to_list, to_i, count, player);
449 // Destination is nodemeta
450 if(to_inv.type == InventoryLocation::NODEMETA)
452 lua_State *L = player->getEnv()->getLua();
453 scriptapi_nodemeta_inventory_on_put(
454 L, to_inv.p, to_list, to_i, src_item, player);
456 // Source is nodemeta
457 else if(from_inv.type == InventoryLocation::NODEMETA)
459 lua_State *L = player->getEnv()->getLua();
460 scriptapi_nodemeta_inventory_on_take(
461 L, from_inv.p, from_list, from_i, src_item, player);
465 mgr->setInventoryModified(from_inv);
466 if(inv_from != inv_to)
467 mgr->setInventoryModified(to_inv);
470 void IMoveAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
472 // Optional InventoryAction operation that is run on the client
473 // to make lag less apparent.
475 Inventory *inv_from = mgr->getInventory(from_inv);
476 Inventory *inv_to = mgr->getInventory(to_inv);
477 if(!inv_from || !inv_to)
480 InventoryLocation current_player;
481 current_player.setCurrentPlayer();
482 Inventory *inv_player = mgr->getInventory(current_player);
483 if(inv_from != inv_player || inv_to != inv_player)
486 InventoryList *list_from = inv_from->getList(from_list);
487 InventoryList *list_to = inv_to->getList(to_list);
488 if(!list_from || !list_to)
491 list_from->moveItem(from_i, list_to, to_i, count);
493 mgr->setInventoryModified(from_inv);
494 if(inv_from != inv_to)
495 mgr->setInventoryModified(to_inv);
502 IDropAction::IDropAction(std::istream &is)
506 std::getline(is, ts, ' ');
509 std::getline(is, ts, ' ');
510 from_inv.deSerialize(ts);
512 std::getline(is, from_list, ' ');
514 std::getline(is, ts, ' ');
518 void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
520 Inventory *inv_from = mgr->getInventory(from_inv);
523 infostream<<"IDropAction::apply(): FAIL: source inventory not found: "
524 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
528 InventoryList *list_from = inv_from->getList(from_list);
531 If a list doesn't exist or the source item doesn't exist
534 infostream<<"IDropAction::apply(): FAIL: source list not found: "
535 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
538 if(list_from->getItem(from_i).empty())
540 infostream<<"IDropAction::apply(): FAIL: source item not found: "
541 <<"from_inv=\""<<from_inv.dump()<<"\""
542 <<", from_list=\""<<from_list<<"\""
543 <<" from_i="<<from_i<<std::endl;
548 Do not handle rollback if inventory is player's
550 bool ignore_src_rollback = (from_inv.type == InventoryLocation::PLAYER);
553 Collect information of endpoints
556 int take_count = list_from->getItem(from_i).count;
557 if(count != 0 && count < take_count)
559 int src_can_take_count = take_count;
561 // Source is detached
562 if(from_inv.type == InventoryLocation::DETACHED)
564 lua_State *L = player->getEnv()->getLua();
565 ItemStack src_item = list_from->getItem(from_i);
566 src_item.count = take_count;
567 src_can_take_count = scriptapi_detached_inventory_allow_take(
568 L, from_inv.name, from_list, from_i, src_item, player);
571 // Source is nodemeta
572 if(from_inv.type == InventoryLocation::NODEMETA)
574 lua_State *L = player->getEnv()->getLua();
575 ItemStack src_item = list_from->getItem(from_i);
576 src_item.count = take_count;
577 src_can_take_count = scriptapi_nodemeta_inventory_allow_take(
578 L, from_inv.p, from_list, from_i, src_item, player);
581 if(src_can_take_count != -1 && src_can_take_count < take_count)
582 take_count = src_can_take_count;
584 int actually_dropped_count = 0;
586 ItemStack src_item = list_from->getItem(from_i);
589 ItemStack item1 = list_from->getItem(from_i);
590 item1.count = take_count;
591 if(scriptapi_item_on_drop(player->getEnv()->getLua(), item1, player,
592 player->getBasePosition() + v3f(0,1,0)))
594 actually_dropped_count = take_count - item1.count;
596 if(actually_dropped_count == 0){
597 infostream<<"Actually dropped no items"<<std::endl;
601 // If source isn't infinite
602 if(src_can_take_count != -1){
603 // Take item from source list
604 ItemStack item2 = list_from->takeItem(from_i, actually_dropped_count);
606 if(item2.count != actually_dropped_count)
607 errorstream<<"Could not take dropped count of items"<<std::endl;
609 mgr->setInventoryModified(from_inv);
613 infostream<<"IDropAction::apply(): dropped "
614 <<" from inv=\""<<from_inv.dump()<<"\""
615 <<" list=\""<<from_list<<"\""
619 src_item.count = actually_dropped_count;
622 Report drop to endpoints
625 // Source is detached
626 if(from_inv.type == InventoryLocation::DETACHED)
628 lua_State *L = player->getEnv()->getLua();
629 scriptapi_detached_inventory_on_take(
630 L, from_inv.name, from_list, from_i, src_item, player);
633 // Source is nodemeta
634 if(from_inv.type == InventoryLocation::NODEMETA)
636 lua_State *L = player->getEnv()->getLua();
637 scriptapi_nodemeta_inventory_on_take(
638 L, from_inv.p, from_list, from_i, src_item, player);
642 Record rollback information
644 if(!ignore_src_rollback && gamedef->rollback())
646 IRollbackReportSink *rollback = gamedef->rollback();
648 // If source is not infinite, record item take
649 if(src_can_take_count != -1){
650 RollbackAction action;
653 std::ostringstream os(std::ios::binary);
654 from_inv.serialize(os);
657 action.setModifyInventoryStack(loc, from_list, from_i,
658 false, src_item.getItemString());
659 rollback->reportAction(action);
664 void IDropAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
666 // Optional InventoryAction operation that is run on the client
667 // to make lag less apparent.
669 Inventory *inv_from = mgr->getInventory(from_inv);
673 InventoryLocation current_player;
674 current_player.setCurrentPlayer();
675 Inventory *inv_player = mgr->getInventory(current_player);
676 if(inv_from != inv_player)
679 InventoryList *list_from = inv_from->getList(from_list);
684 list_from->changeItem(from_i, ItemStack());
686 list_from->takeItem(from_i, count);
688 mgr->setInventoryModified(from_inv);
695 ICraftAction::ICraftAction(std::istream &is)
699 std::getline(is, ts, ' ');
702 std::getline(is, ts, ' ');
703 craft_inv.deSerialize(ts);
706 void ICraftAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
708 Inventory *inv_craft = mgr->getInventory(craft_inv);
711 infostream<<"ICraftAction::apply(): FAIL: inventory not found: "
712 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
716 InventoryList *list_craft = inv_craft->getList("craft");
717 InventoryList *list_craftresult = inv_craft->getList("craftresult");
720 If a list doesn't exist or the source item doesn't exist
723 infostream<<"ICraftAction::apply(): FAIL: craft list not found: "
724 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
727 if(!list_craftresult){
728 infostream<<"ICraftAction::apply(): FAIL: craftresult list not found: "
729 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
732 if(list_craftresult->getSize() < 1){
733 infostream<<"ICraftAction::apply(): FAIL: craftresult list too short: "
734 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
739 int count_remaining = count;
740 bool found = getCraftingResult(inv_craft, crafted, false, gamedef);
742 while(found && list_craftresult->itemFits(0, crafted))
744 // Decrement input and add crafting output
745 getCraftingResult(inv_craft, crafted, true, gamedef);
746 list_craftresult->addItem(0, crafted);
747 mgr->setInventoryModified(craft_inv);
749 actionstream<<player->getDescription()
751 <<crafted.getItemString()
755 if(count_remaining == 1)
757 else if(count_remaining > 1)
760 // Get next crafting result
761 found = getCraftingResult(inv_craft, crafted, false, gamedef);
764 infostream<<"ICraftAction::apply(): crafted "
765 <<" craft_inv=\""<<craft_inv.dump()<<"\""
769 void ICraftAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
771 // Optional InventoryAction operation that is run on the client
772 // to make lag less apparent.
777 bool getCraftingResult(Inventory *inv, ItemStack& result,
778 bool decrementInput, IGameDef *gamedef)
780 DSTACK(__FUNCTION_NAME);
784 // Get the InventoryList in which we will operate
785 InventoryList *clist = inv->getList("craft");
789 // Mangle crafting grid to an another format
791 ci.method = CRAFT_METHOD_NORMAL;
792 ci.width = clist->getWidth() ? clist->getWidth() : 3;
793 for(u16 i=0; i<clist->getSize(); i++)
794 ci.items.push_back(clist->getItem(i));
796 // Find out what is crafted and add it to result item slot
798 bool found = gamedef->getCraftDefManager()->getCraftResult(
799 ci, co, decrementInput, gamedef);
801 result.deSerialize(co.item, gamedef->getItemDefManager());
803 if(found && decrementInput)
805 // CraftInput has been changed, apply changes in clist
806 for(u16 i=0; i<clist->getSize(); i++)
808 clist->changeItem(i, ci.items[i]);