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
34 std::string InventoryLocation::dump() const
36 std::ostringstream os(std::ios::binary);
41 void InventoryLocation::serialize(std::ostream &os) const
44 case InventoryLocation::UNDEFINED:
49 case InventoryLocation::CURRENT_PLAYER:
54 case InventoryLocation::PLAYER:
59 case InventoryLocation::NODEMETA:
61 os<<"nodemeta:"<<p.X<<","<<p.Y<<","<<p.Z;
69 void InventoryLocation::deSerialize(std::istream &is)
72 std::getline(is, tname, ':');
73 if(tname == "undefined")
75 type = InventoryLocation::UNDEFINED;
77 else if(tname == "current_player")
79 type = InventoryLocation::CURRENT_PLAYER;
81 else if(tname == "player")
83 type = InventoryLocation::PLAYER;
84 std::getline(is, name, '\n');
86 else if(tname == "nodemeta")
88 type = InventoryLocation::NODEMETA;
90 std::getline(is, pos, '\n');
92 p.X = stoi(fn.next(","));
93 p.Y = stoi(fn.next(","));
94 p.Z = stoi(fn.next(","));
98 infostream<<"Unknown InventoryLocation type=\""<<tname<<"\""<<std::endl;
99 throw SerializationError("Unknown InventoryLocation type");
103 void InventoryLocation::deSerialize(std::string s)
105 std::istringstream is(s, std::ios::binary);
113 InventoryAction * InventoryAction::deSerialize(std::istream &is)
116 std::getline(is, type, ' ');
118 InventoryAction *a = NULL;
122 a = new IMoveAction(is);
124 else if(type == "Drop")
126 a = new IDropAction(is);
128 else if(type == "Craft")
130 a = new ICraftAction(is);
140 IMoveAction::IMoveAction(std::istream &is)
144 std::getline(is, ts, ' ');
147 std::getline(is, ts, ' ');
148 from_inv.deSerialize(ts);
150 std::getline(is, from_list, ' ');
152 std::getline(is, ts, ' ');
155 std::getline(is, ts, ' ');
156 to_inv.deSerialize(ts);
158 std::getline(is, to_list, ' ');
160 std::getline(is, ts, ' ');
164 void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
166 Inventory *inv_from = mgr->getInventory(from_inv);
167 Inventory *inv_to = mgr->getInventory(to_inv);
170 infostream<<"IMoveAction::apply(): FAIL: source inventory not found: "
171 <<"from_inv=\""<<from_inv.dump()<<"\""
172 <<", to_inv=\""<<to_inv.dump()<<"\""<<std::endl;
176 infostream<<"IMoveAction::apply(): FAIL: destination inventory not found: "
177 <<"from_inv=\""<<from_inv.dump()<<"\""
178 <<", to_inv=\""<<to_inv.dump()<<"\""<<std::endl;
182 InventoryList *list_from = inv_from->getList(from_list);
183 InventoryList *list_to = inv_to->getList(to_list);
186 If a list doesn't exist or the source item doesn't exist
189 infostream<<"IMoveAction::apply(): FAIL: source list not found: "
190 <<"from_inv=\""<<from_inv.dump()<<"\""
191 <<", from_list=\""<<from_list<<"\""<<std::endl;
195 infostream<<"IMoveAction::apply(): FAIL: destination list not found: "
196 <<"to_inv=\""<<to_inv.dump()<<"\""
197 <<", to_list=\""<<to_list<<"\""<<std::endl;
201 This performs the actual movement
203 If something is wrong (source item is empty, destination is the
204 same as source), nothing happens
206 list_from->moveItem(from_i, list_to, to_i, count);
208 mgr->setInventoryModified(from_inv);
209 if(inv_from != inv_to)
210 mgr->setInventoryModified(to_inv);
212 infostream<<"IMoveAction::apply(): moved at "
213 <<" count="<<count<<"\""
214 <<" from inv=\""<<from_inv.dump()<<"\""
215 <<" list=\""<<from_list<<"\""
217 <<" to inv=\""<<to_inv.dump()<<"\""
218 <<" list=\""<<to_list<<"\""
223 void IMoveAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
225 // Optional InventoryAction operation that is run on the client
226 // to make lag less apparent.
228 Inventory *inv_from = mgr->getInventory(from_inv);
229 Inventory *inv_to = mgr->getInventory(to_inv);
230 if(!inv_from || !inv_to)
233 InventoryLocation current_player;
234 current_player.setCurrentPlayer();
235 Inventory *inv_player = mgr->getInventory(current_player);
236 if(inv_from != inv_player || inv_to != inv_player)
239 InventoryList *list_from = inv_from->getList(from_list);
240 InventoryList *list_to = inv_to->getList(to_list);
241 if(!list_from || !list_to)
244 list_from->moveItem(from_i, list_to, to_i, count);
246 mgr->setInventoryModified(from_inv);
247 if(inv_from != inv_to)
248 mgr->setInventoryModified(to_inv);
255 IDropAction::IDropAction(std::istream &is)
259 std::getline(is, ts, ' ');
262 std::getline(is, ts, ' ');
263 from_inv.deSerialize(ts);
265 std::getline(is, from_list, ' ');
267 std::getline(is, ts, ' ');
271 void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
273 Inventory *inv_from = mgr->getInventory(from_inv);
276 infostream<<"IDropAction::apply(): FAIL: source inventory not found: "
277 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
281 InventoryList *list_from = inv_from->getList(from_list);
284 If a list doesn't exist or the source item doesn't exist
287 infostream<<"IDropAction::apply(): FAIL: source list not found: "
288 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
291 if(list_from->getItem(from_i).empty())
293 infostream<<"IDropAction::apply(): FAIL: source item not found: "
294 <<"from_inv=\""<<from_inv.dump()<<"\""
295 <<", from_list=\""<<from_list<<"\""
296 <<" from_i="<<from_i<<std::endl;
300 // Take item from source list
303 item1 = list_from->changeItem(from_i, ItemStack());
305 item1 = list_from->takeItem(from_i, count);
307 // Drop the item and apply the returned ItemStack
308 ItemStack item2 = item1;
309 if(scriptapi_item_on_drop(player->getEnv()->getLua(), item2, player,
310 player->getBasePosition() + v3f(0,1,0)))
312 if(g_settings->getBool("creative_mode") == true
313 && from_inv.type == InventoryLocation::PLAYER)
314 item2 = item1; // creative mode
316 list_from->addItem(from_i, item2);
318 // Unless we have put the same amount back as we took in the first place,
319 // set inventory modified flag
320 if(item2.count != item1.count)
321 mgr->setInventoryModified(from_inv);
324 infostream<<"IDropAction::apply(): dropped "
325 <<" from inv=\""<<from_inv.dump()<<"\""
326 <<" list=\""<<from_list<<"\""
331 void IDropAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
333 // Optional InventoryAction operation that is run on the client
334 // to make lag less apparent.
336 Inventory *inv_from = mgr->getInventory(from_inv);
340 InventoryLocation current_player;
341 current_player.setCurrentPlayer();
342 Inventory *inv_player = mgr->getInventory(current_player);
343 if(inv_from != inv_player)
346 InventoryList *list_from = inv_from->getList(from_list);
351 list_from->changeItem(from_i, ItemStack());
353 list_from->takeItem(from_i, count);
355 mgr->setInventoryModified(from_inv);
362 ICraftAction::ICraftAction(std::istream &is)
366 std::getline(is, ts, ' ');
369 std::getline(is, ts, ' ');
370 craft_inv.deSerialize(ts);
373 void ICraftAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
375 Inventory *inv_craft = mgr->getInventory(craft_inv);
378 infostream<<"ICraftAction::apply(): FAIL: inventory not found: "
379 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
383 InventoryList *list_craft = inv_craft->getList("craft");
384 InventoryList *list_craftresult = inv_craft->getList("craftresult");
387 If a list doesn't exist or the source item doesn't exist
390 infostream<<"ICraftAction::apply(): FAIL: craft list not found: "
391 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
394 if(!list_craftresult){
395 infostream<<"ICraftAction::apply(): FAIL: craftresult list not found: "
396 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
399 if(list_craftresult->getSize() < 1){
400 infostream<<"ICraftAction::apply(): FAIL: craftresult list too short: "
401 <<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
406 int count_remaining = count;
407 bool found = getCraftingResult(inv_craft, crafted, false, gamedef);
409 while(found && list_craftresult->itemFits(0, crafted))
411 // Decrement input and add crafting output
412 getCraftingResult(inv_craft, crafted, true, gamedef);
413 list_craftresult->addItem(0, crafted);
414 mgr->setInventoryModified(craft_inv);
416 actionstream<<player->getDescription()
418 <<crafted.getItemString()
422 if(count_remaining == 1)
424 else if(count_remaining > 1)
427 // Get next crafting result
428 found = getCraftingResult(inv_craft, crafted, false, gamedef);
431 infostream<<"ICraftAction::apply(): crafted "
432 <<" craft_inv=\""<<craft_inv.dump()<<"\""
436 void ICraftAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
438 // Optional InventoryAction operation that is run on the client
439 // to make lag less apparent.
444 bool getCraftingResult(Inventory *inv, ItemStack& result,
445 bool decrementInput, IGameDef *gamedef)
447 DSTACK(__FUNCTION_NAME);
451 // TODO: Allow different sizes of crafting grids
453 // Get the InventoryList in which we will operate
454 InventoryList *clist = inv->getList("craft");
455 if(!clist || clist->getSize() != 9)
458 // Mangle crafting grid to an another format
460 ci.method = CRAFT_METHOD_NORMAL;
462 for(u16 i=0; i<9; i++)
463 ci.items.push_back(clist->getItem(i));
465 // Find out what is crafted and add it to result item slot
467 bool found = gamedef->getCraftDefManager()->getCraftResult(
468 ci, co, decrementInput, gamedef);
470 result.deSerialize(co.item, gamedef->getItemDefManager());
472 if(found && decrementInput)
474 // CraftInput has been changed, apply changes in clist
475 for(u16 i=0; i<9; i++)
477 clist->changeItem(i, ci.items[i]);