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);
62 // Gets amount of items that dropping one SAO will decrement
63 virtual u16 getDropCount(){ return getCount(); }
69 // Shall return true if the item can be add()ed to the other
70 virtual bool addableTo(InventoryItem *other)
79 void setCount(u16 count)
83 // This should return something else for stackable items
84 virtual u16 freeSpace()
90 assert(m_count + count <= QUANTITY_ITEM_MAX_COUNT);
93 void remove(u16 count)
95 assert(m_count >= count);
102 // Whether it can be cooked
103 virtual bool isCookable(){return false;}
105 virtual float getCookTime(){return 3.0;}
107 virtual InventoryItem *createCookResult(){return NULL;}
113 class MaterialItem : public InventoryItem
116 MaterialItem(u8 content, u16 count):
122 Implementation interface
124 virtual const char* getName() const
126 return "MaterialItem";
128 virtual void serialize(std::ostream &os)
130 //os.imbue(std::locale("C"));
133 os<<(unsigned int)m_content;
137 virtual InventoryItem* clone()
139 return new MaterialItem(m_content, m_count);
142 video::ITexture * getImage()
144 return content_features(m_content).inventory_texture;
148 std::string getText()
150 std::ostringstream os;
155 virtual bool addableTo(InventoryItem *other)
157 if(std::string(other->getName()) != "MaterialItem")
159 MaterialItem *m = (MaterialItem*)other;
160 if(m->getMaterial() != m_content)
166 if(m_count > QUANTITY_ITEM_MAX_COUNT)
168 return QUANTITY_ITEM_MAX_COUNT - m_count;
174 InventoryItem *createCookResult();
187 class MapBlockObjectItem : public InventoryItem
190 MapBlockObjectItem(std::string inventorystring):
193 m_inventorystring = inventorystring;
197 Implementation interface
199 virtual const char* getName() const
203 virtual void serialize(std::ostream &os)
207 size_t t = m_inventorystring.find('|');
208 if(t == std::string::npos)
210 m_inventorystring[t] = '?';
214 os<<m_inventorystring;
217 virtual InventoryItem* clone()
219 return new MapBlockObjectItem(m_inventorystring);
223 video::ITexture * getImage();
225 std::string getText();
230 std::string getInventoryString()
232 return m_inventorystring;
235 MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
238 std::string m_inventorystring;
242 An item that is used as a mid-product when crafting.
246 class CraftItem : public InventoryItem
249 CraftItem(std::string subname, u16 count):
255 Implementation interface
257 virtual const char* getName() const
261 virtual void serialize(std::ostream &os)
269 virtual InventoryItem* clone()
271 return new CraftItem(m_subname, m_count);
274 video::ITexture * getImage();
276 std::string getText()
278 std::ostringstream os;
283 ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
286 virtual bool addableTo(InventoryItem *other)
288 if(std::string(other->getName()) != "CraftItem")
290 CraftItem *m = (CraftItem*)other;
291 if(m->m_subname != m_subname)
297 if(m_count > QUANTITY_ITEM_MAX_COUNT)
299 return QUANTITY_ITEM_MAX_COUNT - m_count;
305 InventoryItem *createCookResult();
309 std::string getSubName()
314 std::string m_subname;
317 class ToolItem : public InventoryItem
320 ToolItem(std::string toolname, u16 wear):
323 m_toolname = toolname;
327 Implementation interface
329 virtual const char* getName() const
333 virtual void serialize(std::ostream &os)
341 virtual InventoryItem* clone()
343 return new ToolItem(m_toolname, m_wear);
346 video::ITexture * getImage()
348 if(g_texturesource == NULL)
351 std::string basename;
352 if(m_toolname == "WPick")
353 basename = "tool_woodpick.png";
354 else if(m_toolname == "STPick")
355 basename = "tool_stonepick.png";
356 else if(m_toolname == "SteelPick")
357 basename = "tool_steelpick.png";
358 else if(m_toolname == "MesePick")
359 basename = "tool_mesepick.png";
360 else if(m_toolname == "WShovel")
361 basename = "tool_woodshovel.png";
362 else if(m_toolname == "STShovel")
363 basename = "tool_stoneshovel.png";
364 else if(m_toolname == "SteelShovel")
365 basename = "tool_steelshovel.png";
366 else if(m_toolname == "WAxe")
367 basename = "tool_woodaxe.png";
368 else if(m_toolname == "STAxe")
369 basename = "tool_stoneaxe.png";
370 else if(m_toolname == "SteelAxe")
371 basename = "tool_steelaxe.png";
372 else if(m_toolname == "WSword")
373 basename = "tool_woodsword.png";
374 else if(m_toolname == "STSword")
375 basename = "tool_stonesword.png";
376 else if(m_toolname == "SteelSword")
377 basename = "tool_steelsword.png";
379 basename = "cloud.png";
382 Calculate a progress value with sane amount of
385 u32 maxprogress = 30;
386 u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
388 float value_f = (float)toolprogress / (float)maxprogress;
389 std::ostringstream os;
390 os<<basename<<"^[progressbar"<<value_f;
392 return g_texturesource->getTextureRaw(os.str());
395 spec.addTid(g_irrlicht->getTextureId(basename));
396 spec.addTid(g_irrlicht->getTextureId(os.str()));
397 return g_irrlicht->getTexture(spec);*/
400 std::string getText()
404 /*std::ostringstream os;
408 for(i=0; i<(65535-m_wear)/d; i++)
414 /*std::ostringstream os;
423 std::string getToolName()
431 // Returns true if weared out
432 bool addWear(u16 add)
434 if(m_wear >= 65535 - add)
446 std::string m_toolname;
453 InventoryList(std::string name, u32 size);
456 void serialize(std::ostream &os);
457 void deSerialize(std::istream &is);
459 InventoryList(const InventoryList &other);
460 InventoryList & operator = (const InventoryList &other);
462 std::string getName();
468 /*bool getDirty(){ return m_dirty; }
469 void setDirty(bool dirty=true){ m_dirty = dirty; }*/
471 // Get pointer to item
472 InventoryItem * getItem(u32 i);
473 // Returns old item (or NULL). Parameter can be NULL.
474 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
476 void deleteItem(u32 i);
478 // Adds an item to a suitable place. Returns leftover item.
479 // If all went into the list, returns NULL.
480 InventoryItem * addItem(InventoryItem *newitem);
482 // If possible, adds item to given slot.
483 // If cannot be added at all, returns the item back.
484 // If can be added partly, decremented item is returned back.
485 // If can be added fully, NULL is returned.
486 InventoryItem * addItem(u32 i, InventoryItem *newitem);
488 // Checks whether the item could be added to the given slot
489 bool itemFits(u32 i, InventoryItem *newitem);
491 // Takes some items from a slot.
492 // If there are not enough, takes as many as it can.
493 // Returns NULL if couldn't take any.
494 InventoryItem * takeItem(u32 i, u32 count);
496 // Decrements amount of every material item
497 void decrementMaterials(u16 count);
499 void print(std::ostream &o);
502 core::array<InventoryItem*> m_items;
516 Inventory(const Inventory &other);
517 Inventory & operator = (const Inventory &other);
519 void serialize(std::ostream &os);
520 void deSerialize(std::istream &is);
522 InventoryList * addList(const std::string &name, u32 size);
523 InventoryList * getList(const std::string &name);
524 bool deleteList(const std::string &name);
525 // A shorthand for adding items.
526 // Returns NULL if the item was fully added, leftover otherwise.
527 InventoryItem * addItem(const std::string &listname, InventoryItem *newitem)
529 InventoryList *list = getList(listname);
532 return list->addItem(newitem);
537 s32 getListIndex(const std::string &name);
539 core::array<InventoryList*> m_lists;
544 struct InventoryContext
546 Player *current_player;
553 class InventoryAction;
555 class InventoryManager
559 virtual ~InventoryManager(){}
562 Get a pointer to an inventory specified by id.
567 virtual Inventory* getInventory(InventoryContext *c, std::string id)
569 // Used on the server by InventoryAction::apply and other stuff
570 virtual void inventoryModified(InventoryContext *c, std::string id)
572 // Used on the client
573 virtual void inventoryAction(InventoryAction *a)
577 #define IACTION_MOVE 0
579 struct InventoryAction
581 static InventoryAction * deSerialize(std::istream &is);
583 virtual u16 getType() const = 0;
584 virtual void serialize(std::ostream &os) = 0;
585 virtual void apply(InventoryContext *c, InventoryManager *mgr) = 0;
588 struct IMoveAction : public InventoryAction
590 // count=0 means "everything"
592 std::string from_inv;
593 std::string from_list;
605 IMoveAction(std::istream &is)
609 std::getline(is, ts, ' ');
612 std::getline(is, from_inv, ' ');
614 std::getline(is, from_list, ' ');
616 std::getline(is, ts, ' ');
619 std::getline(is, to_inv, ' ');
621 std::getline(is, to_list, ' ');
623 std::getline(is, ts, ' ');
632 void serialize(std::ostream &os)
644 void apply(InventoryContext *c, InventoryManager *mgr);
648 Craft checking system
662 enum ItemSpecType type;
663 // Only other one of these is used
671 ItemSpec(enum ItemSpecType a_type, std::string a_name):
677 ItemSpec(enum ItemSpecType a_type, u16 a_num):
684 bool checkItem(InventoryItem *item);
688 items: a pointer to an array of 9 pointers to items
689 specs: a pointer to an array of 9 ItemSpecs
691 bool checkItemCombination(InventoryItem **items, ItemSpec *specs);