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 /*bool getDirty(){ return m_dirty; }
460 void setDirty(bool dirty=true){ m_dirty = dirty; }*/
462 // Get pointer to item
463 InventoryItem * getItem(u32 i);
464 // Returns old item (or NULL). Parameter can be NULL.
465 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
467 void deleteItem(u32 i);
469 // Adds an item to a suitable place. Returns leftover item.
470 // If all went into the list, returns NULL.
471 InventoryItem * addItem(InventoryItem *newitem);
473 // If possible, adds item to given slot.
474 // If cannot be added at all, returns the item back.
475 // If can be added partly, decremented item is returned back.
476 // If can be added fully, NULL is returned.
477 InventoryItem * addItem(u32 i, InventoryItem *newitem);
479 // Checks whether the item could be added to the given slot
480 bool itemFits(u32 i, InventoryItem *newitem);
482 // Takes some items from a slot.
483 // If there are not enough, takes as many as it can.
484 // Returns NULL if couldn't take any.
485 InventoryItem * takeItem(u32 i, u32 count);
487 // Decrements amount of every material item
488 void decrementMaterials(u16 count);
490 void print(std::ostream &o);
493 core::array<InventoryItem*> m_items;
507 Inventory(const Inventory &other);
508 Inventory & operator = (const Inventory &other);
510 void serialize(std::ostream &os);
511 void deSerialize(std::istream &is);
513 InventoryList * addList(const std::string &name, u32 size);
514 InventoryList * getList(const std::string &name);
515 bool deleteList(const std::string &name);
516 // A shorthand for adding items.
517 // Returns NULL if the item was fully added, leftover otherwise.
518 InventoryItem * addItem(const std::string &listname, InventoryItem *newitem)
520 InventoryList *list = getList(listname);
523 return list->addItem(newitem);
528 s32 getListIndex(const std::string &name);
530 core::array<InventoryList*> m_lists;
535 struct InventoryContext
537 Player *current_player;
544 class InventoryAction;
546 class InventoryManager
550 virtual ~InventoryManager(){}
553 Get a pointer to an inventory specified by id.
558 virtual Inventory* getInventory(InventoryContext *c, std::string id)
560 // Used on the server by InventoryAction::apply and other stuff
561 virtual void inventoryModified(InventoryContext *c, std::string id)
563 // Used on the client
564 virtual void inventoryAction(InventoryAction *a)
568 #define IACTION_MOVE 0
570 struct InventoryAction
572 static InventoryAction * deSerialize(std::istream &is);
574 virtual u16 getType() const = 0;
575 virtual void serialize(std::ostream &os) = 0;
576 virtual void apply(InventoryContext *c, InventoryManager *mgr) = 0;
579 struct IMoveAction : public InventoryAction
581 // count=0 means "everything"
583 std::string from_inv;
584 std::string from_list;
596 IMoveAction(std::istream &is)
600 std::getline(is, ts, ' ');
603 std::getline(is, from_inv, ' ');
605 std::getline(is, from_list, ' ');
607 std::getline(is, ts, ' ');
610 std::getline(is, to_inv, ' ');
612 std::getline(is, to_list, ' ');
614 std::getline(is, ts, ' ');
623 void serialize(std::ostream &os)
635 void apply(InventoryContext *c, InventoryManager *mgr);
639 Craft checking system
653 enum ItemSpecType type;
654 // Only other one of these is used
662 ItemSpec(enum ItemSpecType a_type, std::string a_name):
668 ItemSpec(enum ItemSpecType a_type, u16 a_num):
675 bool checkItem(InventoryItem *item);
679 items: a pointer to an array of 9 pointers to items
680 specs: a pointer to an array of 9 ItemSpecs
682 bool checkItemCombination(InventoryItem **items, ItemSpec *specs);