3 Copyright (C) 2010 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.
21 (c) 2010 Perttu Ahola <celeron55@gmail.com>
24 #ifndef INVENTORY_HEADER
25 #define INVENTORY_HEADER
30 #include "common_irrlicht.h"
32 #include "mapblockobject.h"
36 #define QUANTITY_ITEM_MAX_COUNT 99
38 class ServerActiveObject;
39 class ServerEnvironment;
44 InventoryItem(u16 count);
45 virtual ~InventoryItem();
47 static InventoryItem* deSerialize(std::istream &is);
49 virtual const char* getName() const = 0;
50 // Shall write the name and the parameters
51 virtual void serialize(std::ostream &os) = 0;
52 // Shall make an exact clone of the item
53 virtual InventoryItem* clone() = 0;
55 // Shall return an image to show in the GUI (or NULL)
56 virtual video::ITexture * getImage() { return NULL; }
58 // Shall return a text to show in the GUI
59 virtual std::string getText() { return ""; }
60 // Creates an object from the item, to be placed in the world.
61 virtual ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
67 // Shall return true if the item can be add()ed to the other
68 virtual bool addableTo(InventoryItem *other)
77 void setCount(u16 count)
81 // This should return something else for stackable items
82 virtual u16 freeSpace()
88 assert(m_count + count <= QUANTITY_ITEM_MAX_COUNT);
91 void remove(u16 count)
93 assert(m_count >= count);
100 // Whether it can be cooked
101 virtual bool isCookable(){return false;}
103 virtual float getCookTime(){return 3.0;}
105 virtual InventoryItem *createCookResult(){return NULL;}
111 class MaterialItem : public InventoryItem
114 MaterialItem(u8 content, u16 count):
120 Implementation interface
122 virtual const char* getName() const
124 return "MaterialItem";
126 virtual void serialize(std::ostream &os)
128 //os.imbue(std::locale("C"));
131 os<<(unsigned int)m_content;
135 virtual InventoryItem* clone()
137 return new MaterialItem(m_content, m_count);
140 video::ITexture * getImage()
142 return content_features(m_content).inventory_texture;
146 std::string getText()
148 std::ostringstream os;
153 virtual bool addableTo(InventoryItem *other)
155 if(std::string(other->getName()) != "MaterialItem")
157 MaterialItem *m = (MaterialItem*)other;
158 if(m->getMaterial() != m_content)
164 if(m_count > QUANTITY_ITEM_MAX_COUNT)
166 return QUANTITY_ITEM_MAX_COUNT - m_count;
172 InventoryItem *createCookResult();
185 class MapBlockObjectItem : public InventoryItem
188 MapBlockObjectItem(std::string inventorystring):
191 m_inventorystring = inventorystring;
195 Implementation interface
197 virtual const char* getName() const
201 virtual void serialize(std::ostream &os)
205 size_t t = m_inventorystring.find('|');
206 if(t == std::string::npos)
208 m_inventorystring[t] = '?';
212 os<<m_inventorystring;
215 virtual InventoryItem* clone()
217 return new MapBlockObjectItem(m_inventorystring);
221 video::ITexture * getImage();
223 std::string getText();
228 std::string getInventoryString()
230 return m_inventorystring;
233 MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
236 std::string m_inventorystring;
240 An item that is used as a mid-product when crafting.
244 class CraftItem : public InventoryItem
247 CraftItem(std::string subname, u16 count):
253 Implementation interface
255 virtual const char* getName() const
259 virtual void serialize(std::ostream &os)
267 virtual InventoryItem* clone()
269 return new CraftItem(m_subname, m_count);
272 video::ITexture * getImage();
274 std::string getText()
276 std::ostringstream os;
281 ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
283 virtual bool addableTo(InventoryItem *other)
285 if(std::string(other->getName()) != "CraftItem")
287 CraftItem *m = (CraftItem*)other;
288 if(m->m_subname != m_subname)
294 if(m_count > QUANTITY_ITEM_MAX_COUNT)
296 return QUANTITY_ITEM_MAX_COUNT - m_count;
302 InventoryItem *createCookResult();
306 std::string getSubName()
311 std::string m_subname;
314 class ToolItem : public InventoryItem
317 ToolItem(std::string toolname, u16 wear):
320 m_toolname = toolname;
324 Implementation interface
326 virtual const char* getName() const
330 virtual void serialize(std::ostream &os)
338 virtual InventoryItem* clone()
340 return new ToolItem(m_toolname, m_wear);
343 video::ITexture * getImage()
345 if(g_texturesource == NULL)
348 std::string basename;
349 if(m_toolname == "WPick")
350 basename = "tool_woodpick.png";
351 else if(m_toolname == "STPick")
352 basename = "tool_stonepick.png";
353 else if(m_toolname == "SteelPick")
354 basename = "tool_steelpick.png";
355 else if(m_toolname == "MesePick")
356 basename = "tool_mesepick.png";
357 else if(m_toolname == "WShovel")
358 basename = "tool_woodshovel.png";
359 else if(m_toolname == "STShovel")
360 basename = "tool_stoneshovel.png";
361 else if(m_toolname == "SteelShovel")
362 basename = "tool_steelshovel.png";
363 else if(m_toolname == "WAxe")
364 basename = "tool_woodaxe.png";
365 else if(m_toolname == "STAxe")
366 basename = "tool_stoneaxe.png";
367 else if(m_toolname == "SteelAxe")
368 basename = "tool_steelaxe.png";
370 basename = "cloud.png";
373 Calculate a progress value with sane amount of
376 u32 maxprogress = 30;
377 u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
379 float value_f = (float)toolprogress / (float)maxprogress;
380 std::ostringstream os;
381 os<<basename<<"^[progressbar"<<value_f;
383 return g_texturesource->getTextureRaw(os.str());
386 spec.addTid(g_irrlicht->getTextureId(basename));
387 spec.addTid(g_irrlicht->getTextureId(os.str()));
388 return g_irrlicht->getTexture(spec);*/
391 std::string getText()
395 /*std::ostringstream os;
399 for(i=0; i<(65535-m_wear)/d; i++)
405 /*std::ostringstream os;
414 std::string getToolName()
422 // Returns true if weared out
423 bool addWear(u16 add)
425 if(m_wear >= 65535 - add)
437 std::string m_toolname;
444 InventoryList(std::string name, u32 size);
447 void serialize(std::ostream &os);
448 void deSerialize(std::istream &is);
450 InventoryList(const InventoryList &other);
451 InventoryList & operator = (const InventoryList &other);
453 std::string getName();
459 // Get pointer to item
460 InventoryItem * getItem(u32 i);
461 // Returns old item (or NULL). Parameter can be NULL.
462 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
464 void deleteItem(u32 i);
466 // Adds an item to a suitable place. Returns leftover item.
467 // If all went into the list, returns NULL.
468 InventoryItem * addItem(InventoryItem *newitem);
470 // If possible, adds item to given slot.
471 // If cannot be added at all, returns the item back.
472 // If can be added partly, decremented item is returned back.
473 // If can be added fully, NULL is returned.
474 InventoryItem * addItem(u32 i, InventoryItem *newitem);
476 // Checks whether the item could be added to the given slot
477 bool itemFits(u32 i, InventoryItem *newitem);
479 // Takes some items from a slot.
480 // If there are not enough, takes as many as it can.
481 // Returns NULL if couldn't take any.
482 InventoryItem * takeItem(u32 i, u32 count);
484 // Decrements amount of every material item
485 void decrementMaterials(u16 count);
487 void print(std::ostream &o);
490 core::array<InventoryItem*> m_items;
503 Inventory(const Inventory &other);
504 Inventory & operator = (const Inventory &other);
506 void serialize(std::ostream &os);
507 void deSerialize(std::istream &is);
509 InventoryList * addList(const std::string &name, u32 size);
510 InventoryList * getList(const std::string &name);
511 bool deleteList(const std::string &name);
512 // A shorthand for adding items.
513 // Returns NULL if the item was fully added, leftover otherwise.
514 InventoryItem * addItem(const std::string &listname, InventoryItem *newitem)
516 InventoryList *list = getList(listname);
519 return list->addItem(newitem);
524 s32 getListIndex(const std::string &name);
526 core::array<InventoryList*> m_lists;
531 struct InventoryContext
533 Player *current_player;
540 class InventoryAction;
542 class InventoryManager
546 virtual ~InventoryManager(){}
549 Get a pointer to an inventory specified by id.
554 virtual Inventory* getInventory(InventoryContext *c, std::string id)
556 // Used on the server by InventoryAction::apply and other stuff
557 virtual void inventoryModified(InventoryContext *c, std::string id)
559 // Used on the client
560 virtual void inventoryAction(InventoryAction *a)
564 #define IACTION_MOVE 0
566 struct InventoryAction
568 static InventoryAction * deSerialize(std::istream &is);
570 virtual u16 getType() const = 0;
571 virtual void serialize(std::ostream &os) = 0;
572 virtual void apply(InventoryContext *c, InventoryManager *mgr) = 0;
575 struct IMoveAction : public InventoryAction
577 // count=0 means "everything"
579 std::string from_inv;
580 std::string from_list;
592 IMoveAction(std::istream &is)
596 std::getline(is, ts, ' ');
599 std::getline(is, from_inv, ' ');
601 std::getline(is, from_list, ' ');
603 std::getline(is, ts, ' ');
606 std::getline(is, to_inv, ' ');
608 std::getline(is, to_list, ' ');
610 std::getline(is, ts, ' ');
619 void serialize(std::ostream &os)
631 void apply(InventoryContext *c, InventoryManager *mgr);
635 Craft checking system
649 enum ItemSpecType type;
650 // Only other one of these is used
658 ItemSpec(enum ItemSpecType a_type, std::string a_name):
664 ItemSpec(enum ItemSpecType a_type, u16 a_num):
671 bool checkItem(InventoryItem *item);
675 items: a pointer to an array of 9 pointers to items
676 specs: a pointer to an array of 9 ItemSpecs
678 bool checkItemCombination(InventoryItem **items, ItemSpec *specs);