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
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:
51 case InventoryLocation::CURRENT_PLAYER:
56 case InventoryLocation::PLAYER:
61 case InventoryLocation::NODEMETA:
63 os<<"nodemeta:"<<p.X<<","<<p.Y<<","<<p.Z;
71 void InventoryLocation::deSerialize(std::istream &is)
74 std::getline(is, tname, ':');
75 if(tname == "undefined")
77 type = InventoryLocation::UNDEFINED;
79 else if(tname == "current_player")
81 type = InventoryLocation::CURRENT_PLAYER;
83 else if(tname == "player")
85 type = InventoryLocation::PLAYER;
86 std::getline(is, name, '\n');
88 else if(tname == "nodemeta")
90 type = InventoryLocation::NODEMETA;
92 std::getline(is, pos, '\n');
94 p.X = stoi(fn.next(","));
95 p.Y = stoi(fn.next(","));
96 p.Z = stoi(fn.next(","));
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;
203 // Handle node metadata move
204 if(from_inv.type == InventoryLocation::NODEMETA &&
205 to_inv.type == InventoryLocation::NODEMETA &&
206 from_inv.p != to_inv.p)
208 errorstream<<"Directly moving items between two nodes is "
209 <<"disallowed."<<std::endl;
212 else if(from_inv.type == InventoryLocation::NODEMETA &&
213 to_inv.type == InventoryLocation::NODEMETA &&
214 from_inv.p == to_inv.p)
216 lua_State *L = player->getEnv()->getLua();
219 count0 = list_from->getItem(from_i).count;
220 infostream<<player->getDescription()<<" moving "<<count0
221 <<" items inside node at "<<PP(from_inv.p)<<std::endl;
222 scriptapi_node_on_metadata_inventory_move(L, from_inv.p,
223 from_list, from_i, to_list, to_i, count0, player);
225 // Handle node metadata take
226 else if(from_inv.type == InventoryLocation::NODEMETA)
228 lua_State *L = player->getEnv()->getLua();
231 count0 = list_from->getItem(from_i).count;
232 infostream<<player->getDescription()<<" taking "<<count0
233 <<" items from node at "<<PP(from_inv.p)<<std::endl;
234 ItemStack return_stack = scriptapi_node_on_metadata_inventory_take(
235 L, from_inv.p, from_list, from_i, count0, player);
236 if(return_stack.count == 0)
237 infostream<<"Node metadata gave no items"<<std::endl;
238 return_stack = list_to->addItem(to_i, return_stack);
239 list_to->addItem(return_stack); // Force return of everything
241 // Handle node metadata offer
242 else if(to_inv.type == InventoryLocation::NODEMETA)
244 lua_State *L = player->getEnv()->getLua();
247 count0 = list_from->getItem(from_i).count;
248 ItemStack offer_stack = list_from->takeItem(from_i, count0);
249 infostream<<player->getDescription()<<" offering "
250 <<offer_stack.count<<" items to node at "
251 <<PP(to_inv.p)<<std::endl;
252 ItemStack reject_stack = scriptapi_node_on_metadata_inventory_offer(
253 L, to_inv.p, to_list, to_i, offer_stack, player);
254 if(reject_stack.count == offer_stack.count)
255 infostream<<"Node metadata rejected all items"<<std::endl;
256 else if(reject_stack.count != 0)
257 infostream<<"Node metadata rejected some items"<<std::endl;
258 reject_stack = list_from->addItem(from_i, reject_stack);
259 list_from->addItem(reject_stack); // Force return of everything
261 // Handle regular move
265 This performs the actual movement
267 If something is wrong (source item is empty, destination is the
268 same as source), nothing happens
270 list_from->moveItem(from_i, list_to, to_i, count);
272 infostream<<"IMoveAction::apply(): moved "
274 <<" from inv=\""<<from_inv.dump()<<"\""
275 <<" list=\""<<from_list<<"\""
277 <<" to inv=\""<<to_inv.dump()<<"\""
278 <<" list=\""<<to_list<<"\""
283 mgr->setInventoryModified(from_inv);
284 if(inv_from != inv_to)
285 mgr->setInventoryModified(to_inv);
288 void IMoveAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
290 // Optional InventoryAction operation that is run on the client
291 // to make lag less apparent.
293 Inventory *inv_from = mgr->getInventory(from_inv);
294 Inventory *inv_to = mgr->getInventory(to_inv);
295 if(!inv_from || !inv_to)
298 InventoryLocation current_player;
299 current_player.setCurrentPlayer();
300 Inventory *inv_player = mgr->getInventory(current_player);
301 if(inv_from != inv_player || inv_to != inv_player)
304 InventoryList *list_from = inv_from->getList(from_list);
305 InventoryList *list_to = inv_to->getList(to_list);
306 if(!list_from || !list_to)
309 list_from->moveItem(from_i, list_to, to_i, count);
311 mgr->setInventoryModified(from_inv);
312 if(inv_from != inv_to)
313 mgr->setInventoryModified(to_inv);
320 IDropAction::IDropAction(std::istream &is)
324 std::getline(is, ts, ' ');
327 std::getline(is, ts, ' ');
328 from_inv.deSerialize(ts);
330 std::getline(is, from_list, ' ');
332 std::getline(is, ts, ' ');
336 void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
338 Inventory *inv_from = mgr->getInventory(from_inv);
341 infostream<<"IDropAction::apply(): FAIL: source inventory not found: "
342 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
346 InventoryList *list_from = inv_from->getList(from_list);
349 If a list doesn't exist or the source item doesn't exist
352 infostream<<"IDropAction::apply(): FAIL: source list not found: "
353 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
356 if(list_from->getItem(from_i).empty())
358 infostream<<"IDropAction::apply(): FAIL: source item not found: "
359 <<"from_inv=\""<<from_inv.dump()<<"\""
360 <<", from_list=\""<<from_list<<"\""
361 <<" from_i="<<from_i<<std::endl;
367 // Handle node metadata take
368 if(from_inv.type == InventoryLocation::NODEMETA)
370 lua_State *L = player->getEnv()->getLua();
373 count0 = list_from->getItem(from_i).count;
374 infostream<<player->getDescription()<<" dropping "<<count0
375 <<" items from node at "<<PP(from_inv.p)<<std::endl;
376 ItemStack return_stack = scriptapi_node_on_metadata_inventory_take(
377 L, from_inv.p, from_list, from_i, count0, player);
378 if(return_stack.count == 0)
379 infostream<<"Node metadata gave no items"<<std::endl;
380 item1 = return_stack;
384 // Take item from source list
386 item1 = list_from->changeItem(from_i, ItemStack());
388 item1 = list_from->takeItem(from_i, count);
391 // Drop the item and apply the returned ItemStack
392 ItemStack item2 = item1;
393 if(scriptapi_item_on_drop(player->getEnv()->getLua(), item2, player,
394 player->getBasePosition() + v3f(0,1,0)))
396 if(g_settings->getBool("creative_mode") == true
397 && from_inv.type == InventoryLocation::PLAYER)
398 item2 = item1; // creative mode
400 list_from->addItem(from_i, item2);
402 // Unless we have put the same amount back as we took in the first place,
403 // set inventory modified flag
404 if(item2.count != item1.count)
405 mgr->setInventoryModified(from_inv);
408 infostream<<"IDropAction::apply(): dropped "
409 <<" from inv=\""<<from_inv.dump()<<"\""
410 <<" list=\""<<from_list<<"\""
415 void IDropAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
417 // Optional InventoryAction operation that is run on the client
418 // to make lag less apparent.
420 Inventory *inv_from = mgr->getInventory(from_inv);
424 InventoryLocation current_player;
425 current_player.setCurrentPlayer();
426 Inventory *inv_player = mgr->getInventory(current_player);
427 if(inv_from != inv_player)
430 InventoryList *list_from = inv_from->getList(from_list);
435 list_from->changeItem(from_i, ItemStack());
437 list_from->takeItem(from_i, count);
439 mgr->setInventoryModified(from_inv);
446 ICraftAction::ICraftAction(std::istream &is)
450 std::getline(is, ts, ' ');
453 std::getline(is, ts, ' ');
454 craft_inv.deSerialize(ts);
457 void ICraftAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
459 Inventory *inv_craft = mgr->getInventory(craft_inv);
462 infostream<<"ICraftAction::apply(): FAIL: inventory not found: "
463 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
467 InventoryList *list_craft = inv_craft->getList("craft");
468 InventoryList *list_craftresult = inv_craft->getList("craftresult");
471 If a list doesn't exist or the source item doesn't exist
474 infostream<<"ICraftAction::apply(): FAIL: craft list not found: "
475 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
478 if(!list_craftresult){
479 infostream<<"ICraftAction::apply(): FAIL: craftresult list not found: "
480 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
483 if(list_craftresult->getSize() < 1){
484 infostream<<"ICraftAction::apply(): FAIL: craftresult list too short: "
485 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
490 int count_remaining = count;
491 bool found = getCraftingResult(inv_craft, crafted, false, gamedef);
493 while(found && list_craftresult->itemFits(0, crafted))
495 // Decrement input and add crafting output
496 getCraftingResult(inv_craft, crafted, true, gamedef);
497 list_craftresult->addItem(0, crafted);
498 mgr->setInventoryModified(craft_inv);
500 actionstream<<player->getDescription()
502 <<crafted.getItemString()
506 if(count_remaining == 1)
508 else if(count_remaining > 1)
511 // Get next crafting result
512 found = getCraftingResult(inv_craft, crafted, false, gamedef);
515 infostream<<"ICraftAction::apply(): crafted "
516 <<" craft_inv=\""<<craft_inv.dump()<<"\""
520 void ICraftAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
522 // Optional InventoryAction operation that is run on the client
523 // to make lag less apparent.
528 bool getCraftingResult(Inventory *inv, ItemStack& result,
529 bool decrementInput, IGameDef *gamedef)
531 DSTACK(__FUNCTION_NAME);
535 // TODO: Allow different sizes of crafting grids
537 // Get the InventoryList in which we will operate
538 InventoryList *clist = inv->getList("craft");
539 if(!clist || clist->getSize() != 9)
542 // Mangle crafting grid to an another format
544 ci.method = CRAFT_METHOD_NORMAL;
546 for(u16 i=0; i<9; i++)
547 ci.items.push_back(clist->getItem(i));
549 // Find out what is crafted and add it to result item slot
551 bool found = gamedef->getCraftDefManager()->getCraftResult(
552 ci, co, decrementInput, gamedef);
554 result.deSerialize(co.item, gamedef->getItemDefManager());
556 if(found && decrementInput)
558 // CraftInput has been changed, apply changes in clist
559 for(u16 i=0; i<9; i++)
561 clist->changeItem(i, ci.items[i]);