minetest/src/inventory.h

280 lines
7.2 KiB
C
Raw Normal View History

/*
Minetest-c55
Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
2010-11-26 15:02:21 -08:00
#ifndef INVENTORY_HEADER
#define INVENTORY_HEADER
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
2010-11-26 15:02:21 -08:00
#include "common_irrlicht.h"
#include "debug.h"
#include "itemdef.h"
2010-11-26 15:02:21 -08:00
struct ToolDiggingProperties;
struct ItemStack
2010-11-26 15:02:21 -08:00
{
ItemStack(): name(""), count(0), wear(0), metadata("") {}
ItemStack(std::string name_, u16 count_,
u16 wear, std::string metadata_,
IItemDefManager *itemdef);
~ItemStack() {}
// Serialization
void serialize(std::ostream &os) const;
void deSerialize(std::istream &is, IItemDefManager *itemdef);
void deSerialize(const std::string &s, IItemDefManager *itemdef);
// Returns the string used for inventory
std::string getItemString() const;
/*
Quantity methods
*/
2010-11-26 15:02:21 -08:00
bool empty() const
2011-11-29 07:15:18 -08:00
{
return count == 0;
2011-11-29 07:15:18 -08:00
}
2011-11-16 16:28:46 -08:00
void clear()
{
name = "";
count = 0;
wear = 0;
metadata = "";
}
2010-11-26 15:02:21 -08:00
void add(u16 n)
2010-11-26 15:02:21 -08:00
{
count += n;
2010-11-26 15:02:21 -08:00
}
void remove(u16 n)
2010-11-26 15:02:21 -08:00
{
assert(count >= n);
count -= n;
if(count == 0)
clear(); // reset name, wear and metadata too
2011-11-16 16:28:46 -08:00
}
2010-11-26 15:02:21 -08:00
// Maximum size of a stack
u16 getStackMax(IItemDefManager *itemdef) const
2011-11-16 16:28:46 -08:00
{
s16 max = itemdef->get(name).stack_max;
return (max >= 0) ? max : 0;
2011-11-16 16:28:46 -08:00
}
// Number of items that can be added to this stack
u16 freeSpace(IItemDefManager *itemdef) const
{
u16 max = getStackMax(itemdef);
if(count > max)
return 0;
return max - count;
}
// Returns false if item is not known and cannot be used
bool isKnown(IItemDefManager *itemdef) const
2010-12-23 17:08:05 -08:00
{
return itemdef->isKnown(name);
2010-12-23 17:08:05 -08:00
}
2011-11-13 06:38:14 -08:00
// Returns a pointer to the item definition struct,
// or a fallback one (name="unknown") if the item is unknown.
const ItemDefinition& getDefinition(
IItemDefManager *itemdef) const
2010-12-23 17:08:05 -08:00
{
return itemdef->get(name);
2010-12-23 17:08:05 -08:00
}
2011-11-16 16:28:46 -08:00
// Get tool digging properties, or those of the hand if not a tool
const ToolDiggingProperties& getToolDiggingProperties(
IItemDefManager *itemdef) const
2011-11-16 16:28:46 -08:00
{
ToolDiggingProperties *prop;
prop = itemdef->get(name).tool_digging_properties;
if(prop == NULL)
prop = itemdef->get("").tool_digging_properties;
assert(prop != NULL);
return *prop;
2011-11-16 16:28:46 -08:00
}
// Wear out (only tools)
// Returns true if the item is (was) a tool
bool addWear(s32 amount, IItemDefManager *itemdef)
2010-12-23 17:08:05 -08:00
{
if(getDefinition(itemdef).type == ITEM_TOOL)
2010-12-24 15:54:39 -08:00
{
if(amount > 65535 - wear)
clear();
else if(amount < -wear)
wear = 0;
else
wear += amount;
2010-12-24 15:54:39 -08:00
return true;
}
else
{
return false;
}
}
// If possible, adds newitem to this item.
// If cannot be added at all, returns the item back.
// If can be added partly, decremented item is returned back.
// If can be added fully, empty item is returned.
ItemStack addItem(const ItemStack &newitem,
IItemDefManager *itemdef);
// Checks whether newitem could be added.
// If restitem is non-NULL, it receives the part of newitem that
// would be left over after adding.
bool itemFits(const ItemStack &newitem,
ItemStack *restitem, // may be NULL
IItemDefManager *itemdef) const;
// Takes some items.
// If there are not enough, takes as many as it can.
// Returns empty item if couldn't take any.
ItemStack takeItem(u32 takecount);
// Similar to takeItem, but keeps this ItemStack intact.
ItemStack peekItem(u32 peekcount) const;
/*
Properties
*/
std::string name;
u16 count;
u16 wear;
std::string metadata;
2010-12-23 17:08:05 -08:00
};
2010-12-22 01:29:06 -08:00
class InventoryList
2010-11-26 15:02:21 -08:00
{
public:
InventoryList(std::string name, u32 size, IItemDefManager *itemdef);
2010-12-22 01:29:06 -08:00
~InventoryList();
2010-11-26 15:02:21 -08:00
void clearItems();
void setSize(u32 newsize);
2011-08-10 14:22:44 -07:00
void serialize(std::ostream &os) const;
void deSerialize(std::istream &is);
2010-11-26 15:02:21 -08:00
2010-12-22 01:29:06 -08:00
InventoryList(const InventoryList &other);
InventoryList & operator = (const InventoryList &other);
2010-11-26 15:02:21 -08:00
2011-08-10 02:38:49 -07:00
const std::string &getName() const;
u32 getSize() const;
2010-12-22 01:29:06 -08:00
// Count used slots
u32 getUsedSlots() const;
u32 getFreeSlots() const;
// Get reference to item
const ItemStack& getItem(u32 i) const;
ItemStack& getItem(u32 i);
// Returns old item. Parameter can be an empty item.
ItemStack changeItem(u32 i, const ItemStack &newitem);
2010-12-22 06:30:23 -08:00
// Delete item
2010-11-26 15:02:21 -08:00
void deleteItem(u32 i);
2011-04-04 16:56:29 -07:00
// Adds an item to a suitable place. Returns leftover item (possibly empty).
ItemStack addItem(const ItemStack &newitem);
// If possible, adds item to given slot.
// If cannot be added at all, returns the item back.
// If can be added partly, decremented item is returned back.
// If can be added fully, empty item is returned.
ItemStack addItem(u32 i, const ItemStack &newitem);
2011-04-04 16:56:29 -07:00
// Checks whether the item could be added to the given slot
// If restitem is non-NULL, it receives the part of newitem that
// would be left over after adding.
bool itemFits(const u32 i, const ItemStack &newitem,
ItemStack *restitem = NULL) const;
// Checks whether there is room for a given item
bool roomForItem(const ItemStack &item) const;
// Checks whether the given count of the given item name
// exists in this inventory list.
bool containsItem(const ItemStack &item) const;
// Removes the given count of the given item name from
// this inventory list. Walks the list in reverse order.
// If not as many items exist as requested, removes as
// many as possible.
// Returns the items that were actually removed.
ItemStack removeItem(const ItemStack &item);
2011-04-04 16:56:29 -07:00
// Takes some items from a slot.
// If there are not enough, takes as many as it can.
// Returns empty item if couldn't take any.
ItemStack takeItem(u32 i, u32 takecount);
2010-12-22 06:30:23 -08:00
// Similar to takeItem, but keeps the slot intact.
ItemStack peekItem(u32 i, u32 peekcount) const;
2010-11-26 15:02:21 -08:00
private:
std::vector<ItemStack> m_items;
2010-11-26 15:02:21 -08:00
u32 m_size;
2010-12-22 01:29:06 -08:00
std::string m_name;
IItemDefManager *m_itemdef;
2010-12-22 01:29:06 -08:00
};
class Inventory
{
public:
~Inventory();
void clear();
Inventory(IItemDefManager *itemdef);
2010-12-22 01:29:06 -08:00
Inventory(const Inventory &other);
Inventory & operator = (const Inventory &other);
2011-08-10 14:22:44 -07:00
void serialize(std::ostream &os) const;
void deSerialize(std::istream &is);
2010-12-22 01:29:06 -08:00
InventoryList * addList(const std::string &name, u32 size);
InventoryList * getList(const std::string &name);
2011-08-10 02:38:49 -07:00
const InventoryList * getList(const std::string &name) const;
2010-12-22 01:29:06 -08:00
bool deleteList(const std::string &name);
// A shorthand for adding items. Returns leftover item (possibly empty).
ItemStack addItem(const std::string &listname, const ItemStack &newitem)
2010-12-22 01:29:06 -08:00
{
InventoryList *list = getList(listname);
if(list == NULL)
return newitem;
2010-12-22 01:29:06 -08:00
return list->addItem(newitem);
}
private:
// -1 if not found
2011-08-10 02:38:49 -07:00
const s32 getListIndex(const std::string &name) const;
2010-12-22 01:29:06 -08:00
std::vector<InventoryList*> m_lists;
IItemDefManager *m_itemdef;
2010-11-26 15:02:21 -08:00
};
#endif