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 list_from->deleteItem(from_i);
336 list_from->addItem(from_i, from_stack_was);
338 // If destination is infinite, reset it's stack and take count from source
339 if(dst_can_put_count == -1){
340 list_to->deleteItem(to_i);
341 list_to->addItem(to_i, to_stack_was);
342 list_from->takeItem(from_i, count);
345 infostream<<"IMoveAction::apply(): moved"
347 <<" from inv=\""<<from_inv.dump()<<"\""
348 <<" list=\""<<from_list<<"\""
350 <<" to inv=\""<<to_inv.dump()<<"\""
351 <<" list=\""<<to_list<<"\""
356 Record rollback information
358 if(!ignore_rollback && gamedef->rollback())
360 IRollbackReportSink *rollback = gamedef->rollback();
362 // If source is not infinite, record item take
363 if(src_can_take_count != -1){
364 RollbackAction action;
367 std::ostringstream os(std::ios::binary);
368 from_inv.serialize(os);
371 action.setModifyInventoryStack(loc, from_list, from_i, false,
372 src_item.getItemString());
373 rollback->reportAction(action);
375 // If destination is not infinite, record item put
376 if(dst_can_put_count != -1){
377 RollbackAction action;
380 std::ostringstream os(std::ios::binary);
381 to_inv.serialize(os);
384 action.setModifyInventoryStack(loc, to_list, to_i, true,
385 src_item.getItemString());
386 rollback->reportAction(action);
391 Report move to endpoints
394 /* Detached inventories */
396 // Both endpoints are same detached
397 if(from_inv.type == InventoryLocation::DETACHED &&
398 to_inv.type == InventoryLocation::DETACHED &&
399 from_inv.name == to_inv.name)
401 lua_State *L = player->getEnv()->getLua();
402 scriptapi_detached_inventory_on_move(
403 L, from_inv.name, from_list, from_i,
404 to_list, to_i, count, player);
408 // Destination is detached
409 if(to_inv.type == InventoryLocation::DETACHED)
411 lua_State *L = player->getEnv()->getLua();
412 scriptapi_detached_inventory_on_put(
413 L, to_inv.name, to_list, to_i, src_item, player);
415 // Source is detached
416 if(from_inv.type == InventoryLocation::DETACHED)
418 lua_State *L = player->getEnv()->getLua();
419 scriptapi_detached_inventory_on_take(
420 L, from_inv.name, from_list, from_i, src_item, player);
424 /* Node metadata inventories */
426 // Both endpoints are same nodemeta
427 if(from_inv.type == InventoryLocation::NODEMETA &&
428 to_inv.type == InventoryLocation::NODEMETA &&
429 from_inv.p == to_inv.p)
431 lua_State *L = player->getEnv()->getLua();
432 scriptapi_nodemeta_inventory_on_move(
433 L, from_inv.p, from_list, from_i,
434 to_list, to_i, count, player);
437 // Destination is nodemeta
438 if(to_inv.type == InventoryLocation::NODEMETA)
440 lua_State *L = player->getEnv()->getLua();
441 scriptapi_nodemeta_inventory_on_put(
442 L, to_inv.p, to_list, to_i, src_item, player);
444 // Source is nodemeta
445 else if(from_inv.type == InventoryLocation::NODEMETA)
447 lua_State *L = player->getEnv()->getLua();
448 scriptapi_nodemeta_inventory_on_take(
449 L, from_inv.p, from_list, from_i, src_item, player);
453 mgr->setInventoryModified(from_inv);
454 if(inv_from != inv_to)
455 mgr->setInventoryModified(to_inv);
458 void IMoveAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
460 // Optional InventoryAction operation that is run on the client
461 // to make lag less apparent.
463 Inventory *inv_from = mgr->getInventory(from_inv);
464 Inventory *inv_to = mgr->getInventory(to_inv);
465 if(!inv_from || !inv_to)
468 InventoryLocation current_player;
469 current_player.setCurrentPlayer();
470 Inventory *inv_player = mgr->getInventory(current_player);
471 if(inv_from != inv_player || inv_to != inv_player)
474 InventoryList *list_from = inv_from->getList(from_list);
475 InventoryList *list_to = inv_to->getList(to_list);
476 if(!list_from || !list_to)
479 list_from->moveItem(from_i, list_to, to_i, count);
481 mgr->setInventoryModified(from_inv);
482 if(inv_from != inv_to)
483 mgr->setInventoryModified(to_inv);
490 IDropAction::IDropAction(std::istream &is)
494 std::getline(is, ts, ' ');
497 std::getline(is, ts, ' ');
498 from_inv.deSerialize(ts);
500 std::getline(is, from_list, ' ');
502 std::getline(is, ts, ' ');
506 void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
508 Inventory *inv_from = mgr->getInventory(from_inv);
511 infostream<<"IDropAction::apply(): FAIL: source inventory not found: "
512 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
516 InventoryList *list_from = inv_from->getList(from_list);
519 If a list doesn't exist or the source item doesn't exist
522 infostream<<"IDropAction::apply(): FAIL: source list not found: "
523 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
526 if(list_from->getItem(from_i).empty())
528 infostream<<"IDropAction::apply(): FAIL: source item not found: "
529 <<"from_inv=\""<<from_inv.dump()<<"\""
530 <<", from_list=\""<<from_list<<"\""
531 <<" from_i="<<from_i<<std::endl;
536 Do not handle rollback if inventory is player's
538 bool ignore_src_rollback = (from_inv.type == InventoryLocation::PLAYER);
541 Collect information of endpoints
544 int take_count = list_from->getItem(from_i).count;
545 if(count != 0 && count < take_count)
547 int src_can_take_count = take_count;
549 // Source is detached
550 if(from_inv.type == InventoryLocation::DETACHED)
552 lua_State *L = player->getEnv()->getLua();
553 ItemStack src_item = list_from->getItem(from_i);
554 src_item.count = take_count;
555 src_can_take_count = scriptapi_detached_inventory_allow_take(
556 L, from_inv.name, from_list, from_i, src_item, player);
559 // Source is nodemeta
560 if(from_inv.type == InventoryLocation::NODEMETA)
562 lua_State *L = player->getEnv()->getLua();
563 ItemStack src_item = list_from->getItem(from_i);
564 src_item.count = take_count;
565 src_can_take_count = scriptapi_nodemeta_inventory_allow_take(
566 L, 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(scriptapi_item_on_drop(player->getEnv()->getLua(), 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 lua_State *L = player->getEnv()->getLua();
617 scriptapi_detached_inventory_on_take(
618 L, from_inv.name, from_list, from_i, src_item, player);
621 // Source is nodemeta
622 if(from_inv.type == InventoryLocation::NODEMETA)
624 lua_State *L = player->getEnv()->getLua();
625 scriptapi_nodemeta_inventory_on_take(
626 L, 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 int count_remaining = count;
728 bool found = getCraftingResult(inv_craft, crafted, false, gamedef);
730 while(found && list_craftresult->itemFits(0, crafted))
732 // Decrement input and add crafting output
733 getCraftingResult(inv_craft, crafted, true, gamedef);
734 list_craftresult->addItem(0, crafted);
735 mgr->setInventoryModified(craft_inv);
737 actionstream<<player->getDescription()
739 <<crafted.getItemString()
743 if(count_remaining == 1)
745 else if(count_remaining > 1)
748 // Get next crafting result
749 found = getCraftingResult(inv_craft, crafted, false, gamedef);
752 infostream<<"ICraftAction::apply(): crafted "
753 <<" craft_inv=\""<<craft_inv.dump()<<"\""
757 void ICraftAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
759 // Optional InventoryAction operation that is run on the client
760 // to make lag less apparent.
765 bool getCraftingResult(Inventory *inv, ItemStack& result,
766 bool decrementInput, IGameDef *gamedef)
768 DSTACK(__FUNCTION_NAME);
772 // TODO: Allow different sizes of crafting grids
774 // Get the InventoryList in which we will operate
775 InventoryList *clist = inv->getList("craft");
776 if(!clist || clist->getSize() != 9)
779 // Mangle crafting grid to an another format
781 ci.method = CRAFT_METHOD_NORMAL;
783 for(u16 i=0; i<9; i++)
784 ci.items.push_back(clist->getItem(i));
786 // Find out what is crafted and add it to result item slot
788 bool found = gamedef->getCraftDefManager()->getCraftResult(
789 ci, co, decrementInput, gamedef);
791 result.deSerialize(co.item, gamedef->getItemDefManager());
793 if(found && decrementInput)
795 // CraftInput has been changed, apply changes in clist
796 for(u16 i=0; i<9; i++)
798 clist->changeItem(i, ci.items[i]);