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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
15 You should have received a copy of the GNU 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 lua_State *L = player->getEnv()->getLua();
211 count0 = list_from->getItem(from_i).count;
212 infostream<<player->getDescription()<<" moving "<<count0
213 <<" items inside node at "<<PP(from_inv.p)<<std::endl;
214 scriptapi_node_on_metadata_inventory_move(L, from_inv.p,
215 from_list, from_i, to_list, to_i, count0, player);
217 // Handle node metadata take
218 else if(from_inv.type == InventoryLocation::NODEMETA)
220 lua_State *L = player->getEnv()->getLua();
223 count0 = list_from->getItem(from_i).count;
224 infostream<<player->getDescription()<<" taking "<<count0
225 <<" items from node at "<<PP(from_inv.p)<<std::endl;
226 ItemStack return_stack = scriptapi_node_on_metadata_inventory_take(
227 L, from_inv.p, from_list, from_i, count0, player);
228 if(return_stack.count == 0)
229 infostream<<"Node metadata gave no items"<<std::endl;
230 return_stack = list_to->addItem(to_i, return_stack);
231 list_to->addItem(return_stack); // Force return of everything
233 // Handle node metadata offer
234 else if(to_inv.type == InventoryLocation::NODEMETA)
236 lua_State *L = player->getEnv()->getLua();
239 count0 = list_from->getItem(from_i).count;
240 ItemStack offer_stack = list_from->takeItem(from_i, count0);
241 infostream<<player->getDescription()<<" offering "
242 <<offer_stack.count<<" items to node at "
243 <<PP(to_inv.p)<<std::endl;
244 ItemStack reject_stack = scriptapi_node_on_metadata_inventory_offer(
245 L, to_inv.p, to_list, to_i, offer_stack, player);
246 if(reject_stack.count == offer_stack.count)
247 infostream<<"Node metadata rejected all items"<<std::endl;
248 else if(reject_stack.count != 0)
249 infostream<<"Node metadata rejected some items"<<std::endl;
250 reject_stack = list_from->addItem(from_i, reject_stack);
251 list_from->addItem(reject_stack); // Force return of everything
253 // Handle regular move
257 This performs the actual movement
259 If something is wrong (source item is empty, destination is the
260 same as source), nothing happens
262 list_from->moveItem(from_i, list_to, to_i, count);
264 infostream<<"IMoveAction::apply(): moved "
266 <<" from inv=\""<<from_inv.dump()<<"\""
267 <<" list=\""<<from_list<<"\""
269 <<" to inv=\""<<to_inv.dump()<<"\""
270 <<" list=\""<<to_list<<"\""
275 mgr->setInventoryModified(from_inv);
276 if(inv_from != inv_to)
277 mgr->setInventoryModified(to_inv);
280 void IMoveAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
282 // Optional InventoryAction operation that is run on the client
283 // to make lag less apparent.
285 Inventory *inv_from = mgr->getInventory(from_inv);
286 Inventory *inv_to = mgr->getInventory(to_inv);
287 if(!inv_from || !inv_to)
290 InventoryLocation current_player;
291 current_player.setCurrentPlayer();
292 Inventory *inv_player = mgr->getInventory(current_player);
293 if(inv_from != inv_player || inv_to != inv_player)
296 InventoryList *list_from = inv_from->getList(from_list);
297 InventoryList *list_to = inv_to->getList(to_list);
298 if(!list_from || !list_to)
301 list_from->moveItem(from_i, list_to, to_i, count);
303 mgr->setInventoryModified(from_inv);
304 if(inv_from != inv_to)
305 mgr->setInventoryModified(to_inv);
312 IDropAction::IDropAction(std::istream &is)
316 std::getline(is, ts, ' ');
319 std::getline(is, ts, ' ');
320 from_inv.deSerialize(ts);
322 std::getline(is, from_list, ' ');
324 std::getline(is, ts, ' ');
328 void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
330 Inventory *inv_from = mgr->getInventory(from_inv);
333 infostream<<"IDropAction::apply(): FAIL: source inventory not found: "
334 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
338 InventoryList *list_from = inv_from->getList(from_list);
341 If a list doesn't exist or the source item doesn't exist
344 infostream<<"IDropAction::apply(): FAIL: source list not found: "
345 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
348 if(list_from->getItem(from_i).empty())
350 infostream<<"IDropAction::apply(): FAIL: source item not found: "
351 <<"from_inv=\""<<from_inv.dump()<<"\""
352 <<", from_list=\""<<from_list<<"\""
353 <<" from_i="<<from_i<<std::endl;
357 // Take item from source list
360 item1 = list_from->changeItem(from_i, ItemStack());
362 item1 = list_from->takeItem(from_i, count);
364 // Drop the item and apply the returned ItemStack
365 ItemStack item2 = item1;
366 if(scriptapi_item_on_drop(player->getEnv()->getLua(), item2, player,
367 player->getBasePosition() + v3f(0,1,0)))
369 if(g_settings->getBool("creative_mode") == true
370 && from_inv.type == InventoryLocation::PLAYER)
371 item2 = item1; // creative mode
373 list_from->addItem(from_i, item2);
375 // Unless we have put the same amount back as we took in the first place,
376 // set inventory modified flag
377 if(item2.count != item1.count)
378 mgr->setInventoryModified(from_inv);
381 infostream<<"IDropAction::apply(): dropped "
382 <<" from inv=\""<<from_inv.dump()<<"\""
383 <<" list=\""<<from_list<<"\""
388 void IDropAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
390 // Optional InventoryAction operation that is run on the client
391 // to make lag less apparent.
393 Inventory *inv_from = mgr->getInventory(from_inv);
397 InventoryLocation current_player;
398 current_player.setCurrentPlayer();
399 Inventory *inv_player = mgr->getInventory(current_player);
400 if(inv_from != inv_player)
403 InventoryList *list_from = inv_from->getList(from_list);
408 list_from->changeItem(from_i, ItemStack());
410 list_from->takeItem(from_i, count);
412 mgr->setInventoryModified(from_inv);
419 ICraftAction::ICraftAction(std::istream &is)
423 std::getline(is, ts, ' ');
426 std::getline(is, ts, ' ');
427 craft_inv.deSerialize(ts);
430 void ICraftAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
432 Inventory *inv_craft = mgr->getInventory(craft_inv);
435 infostream<<"ICraftAction::apply(): FAIL: inventory not found: "
436 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
440 InventoryList *list_craft = inv_craft->getList("craft");
441 InventoryList *list_craftresult = inv_craft->getList("craftresult");
444 If a list doesn't exist or the source item doesn't exist
447 infostream<<"ICraftAction::apply(): FAIL: craft list not found: "
448 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
451 if(!list_craftresult){
452 infostream<<"ICraftAction::apply(): FAIL: craftresult list not found: "
453 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
456 if(list_craftresult->getSize() < 1){
457 infostream<<"ICraftAction::apply(): FAIL: craftresult list too short: "
458 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
463 int count_remaining = count;
464 bool found = getCraftingResult(inv_craft, crafted, false, gamedef);
466 while(found && list_craftresult->itemFits(0, crafted))
468 // Decrement input and add crafting output
469 getCraftingResult(inv_craft, crafted, true, gamedef);
470 list_craftresult->addItem(0, crafted);
471 mgr->setInventoryModified(craft_inv);
473 actionstream<<player->getDescription()
475 <<crafted.getItemString()
479 if(count_remaining == 1)
481 else if(count_remaining > 1)
484 // Get next crafting result
485 found = getCraftingResult(inv_craft, crafted, false, gamedef);
488 infostream<<"ICraftAction::apply(): crafted "
489 <<" craft_inv=\""<<craft_inv.dump()<<"\""
493 void ICraftAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
495 // Optional InventoryAction operation that is run on the client
496 // to make lag less apparent.
501 bool getCraftingResult(Inventory *inv, ItemStack& result,
502 bool decrementInput, IGameDef *gamedef)
504 DSTACK(__FUNCTION_NAME);
508 // TODO: Allow different sizes of crafting grids
510 // Get the InventoryList in which we will operate
511 InventoryList *clist = inv->getList("craft");
512 if(!clist || clist->getSize() != 9)
515 // Mangle crafting grid to an another format
517 ci.method = CRAFT_METHOD_NORMAL;
519 for(u16 i=0; i<9; i++)
520 ci.items.push_back(clist->getItem(i));
522 // Find out what is crafted and add it to result item slot
524 bool found = gamedef->getCraftDefManager()->getCraftResult(
525 ci, co, decrementInput, gamedef);
527 result.deSerialize(co.item, gamedef->getItemDefManager());
529 if(found && decrementInput)
531 // CraftInput has been changed, apply changes in clist
532 for(u16 i=0; i<9; i++)
534 clist->changeItem(i, ci.items[i]);