2010-11-29 19:13:04 +01:00
|
|
|
/*
|
2013-02-24 18:40:43 +01:00
|
|
|
Minetest
|
2013-02-24 19:38:45 +01:00
|
|
|
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2010-11-29 19:13:04 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
2012-06-05 16:56:56 +02:00
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
2010-11-29 19:13:04 +01:00
|
|
|
(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
|
2012-06-05 16:56:56 +02:00
|
|
|
GNU Lesser General Public License for more details.
|
2010-11-29 19:13:04 +01:00
|
|
|
|
2012-06-05 16:56:56 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
2010-11-29 19:13:04 +01:00
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2017-08-17 22:19:39 +02:00
|
|
|
#pragma once
|
2010-11-27 00:02:21 +01:00
|
|
|
|
2012-01-12 06:10:39 +01:00
|
|
|
#include "itemdef.h"
|
2014-06-26 02:28:41 +02:00
|
|
|
#include "irrlichttypes.h"
|
2017-01-31 20:49:01 +01:00
|
|
|
#include "itemstackmetadata.h"
|
2014-06-26 02:28:41 +02:00
|
|
|
#include <istream>
|
|
|
|
#include <ostream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2017-08-18 19:25:07 +02:00
|
|
|
#include <cassert>
|
2010-11-27 00:02:21 +01:00
|
|
|
|
2012-02-28 18:45:23 +01:00
|
|
|
struct ToolCapabilities;
|
2010-12-24 16:08:50 +01:00
|
|
|
|
2012-01-12 06:10:39 +01:00
|
|
|
struct ItemStack
|
2010-11-27 00:02:21 +01:00
|
|
|
{
|
2017-08-20 19:37:29 +02:00
|
|
|
ItemStack() = default;
|
|
|
|
|
2017-01-31 20:49:01 +01:00
|
|
|
ItemStack(const std::string &name_, u16 count_,
|
|
|
|
u16 wear, IItemDefManager *itemdef);
|
|
|
|
|
2017-08-20 19:37:29 +02:00
|
|
|
~ItemStack() = default;
|
2012-01-12 06:10:39 +01:00
|
|
|
|
|
|
|
// Serialization
|
2020-06-09 19:37:25 +02:00
|
|
|
void serialize(std::ostream &os, bool serialize_meta = true) const;
|
2019-05-11 18:48:27 +02:00
|
|
|
// Deserialization. Pass itemdef unless you don't want aliases resolved.
|
2014-06-26 02:28:41 +02:00
|
|
|
void deSerialize(std::istream &is, IItemDefManager *itemdef = NULL);
|
|
|
|
void deSerialize(const std::string &s, IItemDefManager *itemdef = NULL);
|
2012-01-12 06:10:39 +01:00
|
|
|
|
2011-09-01 23:16:55 +02:00
|
|
|
// Returns the string used for inventory
|
2020-06-09 19:37:25 +02:00
|
|
|
std::string getItemString(bool include_meta = true) const;
|
2019-08-24 18:38:02 +02:00
|
|
|
// Returns the tooltip
|
|
|
|
std::string getDescription(IItemDefManager *itemdef) const;
|
2020-10-09 20:11:21 +02:00
|
|
|
std::string getShortDescription(IItemDefManager *itemdef) const;
|
2011-04-10 14:16:27 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Quantity methods
|
|
|
|
*/
|
2010-11-27 00:02:21 +01:00
|
|
|
|
2012-01-12 06:10:39 +01:00
|
|
|
bool empty() const
|
2011-11-29 16:15:18 +01:00
|
|
|
{
|
2012-01-12 06:10:39 +01:00
|
|
|
return count == 0;
|
2011-11-29 16:15:18 +01:00
|
|
|
}
|
2011-11-17 01:28:46 +01:00
|
|
|
|
2012-01-12 06:10:39 +01:00
|
|
|
void clear()
|
2010-12-24 16:08:50 +01:00
|
|
|
{
|
2012-01-12 06:10:39 +01:00
|
|
|
name = "";
|
|
|
|
count = 0;
|
|
|
|
wear = 0;
|
2017-01-31 20:49:01 +01:00
|
|
|
metadata.clear();
|
2010-12-24 16:08:50 +01:00
|
|
|
}
|
2010-11-27 00:02:21 +01:00
|
|
|
|
2012-01-12 06:10:39 +01:00
|
|
|
void add(u16 n)
|
2010-11-27 00:02:21 +01:00
|
|
|
{
|
2012-01-12 06:10:39 +01:00
|
|
|
count += n;
|
2010-11-27 00:02:21 +01:00
|
|
|
}
|
2010-12-24 16:08:50 +01:00
|
|
|
|
2012-01-12 06:10:39 +01:00
|
|
|
void remove(u16 n)
|
2010-11-27 00:02:21 +01:00
|
|
|
{
|
2015-03-06 11:21:51 +01:00
|
|
|
assert(count >= n); // Pre-condition
|
2012-01-12 06:10:39 +01:00
|
|
|
count -= n;
|
|
|
|
if(count == 0)
|
|
|
|
clear(); // reset name, wear and metadata too
|
2011-11-17 01:28:46 +01:00
|
|
|
}
|
2010-11-27 00:02:21 +01:00
|
|
|
|
2012-01-12 06:10:39 +01:00
|
|
|
// Maximum size of a stack
|
|
|
|
u16 getStackMax(IItemDefManager *itemdef) const
|
2011-11-17 01:28:46 +01:00
|
|
|
{
|
2016-09-06 19:13:52 +02:00
|
|
|
return itemdef->get(name).stack_max;
|
2011-11-17 01:28:46 +01:00
|
|
|
}
|
|
|
|
|
2012-01-12 06:10:39 +01:00
|
|
|
// Number of items that can be added to this stack
|
|
|
|
u16 freeSpace(IItemDefManager *itemdef) const
|
2010-12-24 16:08:50 +01:00
|
|
|
{
|
2012-01-12 06:10:39 +01:00
|
|
|
u16 max = getStackMax(itemdef);
|
2016-09-06 19:13:52 +02:00
|
|
|
if (count >= max)
|
2012-01-12 06:10:39 +01:00
|
|
|
return 0;
|
|
|
|
return max - count;
|
2010-12-24 16:08:50 +01:00
|
|
|
}
|
|
|
|
|
2012-01-12 06:10:39 +01:00
|
|
|
// Returns false if item is not known and cannot be used
|
|
|
|
bool isKnown(IItemDefManager *itemdef) const
|
2010-12-24 02:08:05 +01:00
|
|
|
{
|
2012-01-12 06:10:39 +01:00
|
|
|
return itemdef->isKnown(name);
|
2010-12-24 02:08:05 +01:00
|
|
|
}
|
2011-11-13 15:38:14 +01:00
|
|
|
|
2012-01-12 06:10:39 +01: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-24 02:08:05 +01:00
|
|
|
{
|
2012-01-12 06:10:39 +01:00
|
|
|
return itemdef->get(name);
|
2010-12-24 02:08:05 +01:00
|
|
|
}
|
2011-11-17 01:28:46 +01:00
|
|
|
|
2012-01-12 06:10:39 +01:00
|
|
|
// Get tool digging properties, or those of the hand if not a tool
|
2012-02-28 18:45:23 +01:00
|
|
|
const ToolCapabilities& getToolCapabilities(
|
2012-01-12 06:10:39 +01:00
|
|
|
IItemDefManager *itemdef) const
|
2011-11-17 01:28:46 +01:00
|
|
|
{
|
2017-04-19 01:30:27 +02:00
|
|
|
const ToolCapabilities *item_cap =
|
|
|
|
itemdef->get(name).tool_capabilities;
|
|
|
|
|
|
|
|
if (item_cap == NULL)
|
|
|
|
// Fall back to the hand's tool capabilities
|
|
|
|
item_cap = itemdef->get("").tool_capabilities;
|
|
|
|
|
|
|
|
assert(item_cap != NULL);
|
|
|
|
return metadata.getToolCapabilities(*item_cap); // Check for override
|
2011-11-17 01:28:46 +01:00
|
|
|
}
|
|
|
|
|
2012-01-12 06:10:39 +01:00
|
|
|
// Wear out (only tools)
|
|
|
|
// Returns true if the item is (was) a tool
|
|
|
|
bool addWear(s32 amount, IItemDefManager *itemdef)
|
2010-12-24 02:08:05 +01:00
|
|
|
{
|
2012-01-12 06:10:39 +01:00
|
|
|
if(getDefinition(itemdef).type == ITEM_TOOL)
|
2010-12-25 00:54:39 +01:00
|
|
|
{
|
2012-01-12 06:10:39 +01:00
|
|
|
if(amount > 65535 - wear)
|
|
|
|
clear();
|
|
|
|
else if(amount < -wear)
|
|
|
|
wear = 0;
|
|
|
|
else
|
|
|
|
wear += amount;
|
2010-12-25 00:54:39 +01:00
|
|
|
return true;
|
|
|
|
}
|
2017-08-20 19:37:29 +02:00
|
|
|
|
|
|
|
return false;
|
2010-12-25 00:54:39 +01:00
|
|
|
}
|
2012-01-12 06:10:39 +01:00
|
|
|
|
|
|
|
// 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.
|
2017-06-25 11:39:39 +02:00
|
|
|
ItemStack addItem(ItemStack newitem, IItemDefManager *itemdef);
|
2012-01-12 06:10:39 +01:00
|
|
|
|
|
|
|
// Checks whether newitem could be added.
|
|
|
|
// If restitem is non-NULL, it receives the part of newitem that
|
|
|
|
// would be left over after adding.
|
2017-06-25 11:39:39 +02:00
|
|
|
bool itemFits(ItemStack newitem,
|
2012-01-12 06:10:39 +01:00
|
|
|
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;
|
|
|
|
|
2019-05-11 18:48:27 +02:00
|
|
|
bool operator ==(const ItemStack &s) const
|
|
|
|
{
|
|
|
|
return (this->name == s.name &&
|
|
|
|
this->count == s.count &&
|
|
|
|
this->wear == s.wear &&
|
|
|
|
this->metadata == s.metadata);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator !=(const ItemStack &s) const
|
|
|
|
{
|
|
|
|
return !(*this == s);
|
|
|
|
}
|
|
|
|
|
2012-01-12 06:10:39 +01:00
|
|
|
/*
|
|
|
|
Properties
|
|
|
|
*/
|
2017-06-17 19:11:28 +02:00
|
|
|
std::string name = "";
|
|
|
|
u16 count = 0;
|
|
|
|
u16 wear = 0;
|
2017-01-31 20:49:01 +01:00
|
|
|
ItemStackMetadata metadata;
|
2010-12-24 02:08:05 +01:00
|
|
|
};
|
|
|
|
|
2010-12-22 10:29:06 +01:00
|
|
|
class InventoryList
|
2010-11-27 00:02:21 +01:00
|
|
|
{
|
|
|
|
public:
|
2017-04-19 23:02:07 +02:00
|
|
|
InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef);
|
2017-08-18 08:07:59 +02:00
|
|
|
~InventoryList() = default;
|
2010-11-27 00:02:21 +01:00
|
|
|
void clearItems();
|
2011-12-06 14:21:56 +01:00
|
|
|
void setSize(u32 newsize);
|
2012-08-19 23:29:56 +02:00
|
|
|
void setWidth(u32 newWidth);
|
2012-06-02 23:32:49 +02:00
|
|
|
void setName(const std::string &name);
|
2019-08-24 19:07:38 +02:00
|
|
|
void serialize(std::ostream &os, bool incremental) const;
|
2012-01-12 06:10:39 +01:00
|
|
|
void deSerialize(std::istream &is);
|
2010-11-27 00:02:21 +01:00
|
|
|
|
2010-12-22 10:29:06 +01:00
|
|
|
InventoryList(const InventoryList &other);
|
|
|
|
InventoryList & operator = (const InventoryList &other);
|
2014-01-11 19:46:37 +01:00
|
|
|
bool operator == (const InventoryList &other) const;
|
|
|
|
bool operator != (const InventoryList &other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
2010-11-27 00:02:21 +01:00
|
|
|
|
2011-08-10 11:38:49 +02:00
|
|
|
const std::string &getName() const;
|
2012-01-12 06:10:39 +01:00
|
|
|
u32 getSize() const;
|
2012-08-19 23:29:56 +02:00
|
|
|
u32 getWidth() const;
|
2010-12-22 10:29:06 +01:00
|
|
|
// Count used slots
|
2012-01-12 06:10:39 +01:00
|
|
|
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 15:30:23 +01:00
|
|
|
// Delete item
|
2010-11-27 00:02:21 +01:00
|
|
|
void deleteItem(u32 i);
|
2011-04-05 01:56:29 +02:00
|
|
|
|
2012-01-12 06:10:39 +01:00
|
|
|
// Adds an item to a suitable place. Returns leftover item (possibly empty).
|
|
|
|
ItemStack addItem(const ItemStack &newitem);
|
2010-12-24 16:08:50 +01:00
|
|
|
|
|
|
|
// 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.
|
2012-01-12 06:10:39 +01:00
|
|
|
// If can be added fully, empty item is returned.
|
|
|
|
ItemStack addItem(u32 i, const ItemStack &newitem);
|
2010-12-24 16:08:50 +01:00
|
|
|
|
2011-04-05 01:56:29 +02:00
|
|
|
// Checks whether the item could be added to the given slot
|
2012-01-12 06:10:39 +01:00
|
|
|
// 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;
|
2011-08-26 01:27:50 +02:00
|
|
|
|
|
|
|
// Checks whether there is room for a given item
|
2012-01-12 06:10:39 +01:00
|
|
|
bool roomForItem(const ItemStack &item) const;
|
|
|
|
|
2017-06-20 11:19:56 +02:00
|
|
|
// Checks whether the given count of the given item
|
2012-01-12 06:10:39 +01:00
|
|
|
// exists in this inventory list.
|
2017-06-20 11:19:56 +02:00
|
|
|
// If match_meta is false, only the items' names are compared.
|
|
|
|
bool containsItem(const ItemStack &item, bool match_meta) const;
|
2011-08-26 01:27:50 +02:00
|
|
|
|
2012-01-12 06:10:39 +01:00
|
|
|
// 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-05 01:56:29 +02:00
|
|
|
|
2010-12-24 16:08:50 +01:00
|
|
|
// Takes some items from a slot.
|
|
|
|
// If there are not enough, takes as many as it can.
|
2012-01-12 06:10:39 +01:00
|
|
|
// Returns empty item if couldn't take any.
|
|
|
|
ItemStack takeItem(u32 i, u32 takecount);
|
2010-12-22 15:30:23 +01:00
|
|
|
|
2012-01-22 00:49:02 +01:00
|
|
|
// Move an item to a different list (or a different stack in the same list)
|
|
|
|
// count is the maximum number of items to move (0 for everything)
|
2015-06-20 12:55:48 +02:00
|
|
|
// returns number of moved items
|
|
|
|
u32 moveItem(u32 i, InventoryList *dest, u32 dest_i,
|
2015-08-19 02:28:37 +02:00
|
|
|
u32 count = 0, bool swap_if_needed = true, bool *did_swap = NULL);
|
2015-06-20 12:55:48 +02:00
|
|
|
|
|
|
|
// like moveItem, but without a fixed destination index
|
|
|
|
// also with optional rollback recording
|
|
|
|
void moveItemSomewhere(u32 i, InventoryList *dest, u32 count);
|
2012-01-22 00:49:02 +01:00
|
|
|
|
2019-08-24 19:07:38 +02:00
|
|
|
inline bool checkModified() const { return m_dirty; }
|
|
|
|
inline void setModified(bool dirty = true) { m_dirty = dirty; }
|
|
|
|
|
2010-11-27 00:02:21 +01:00
|
|
|
private:
|
2012-01-12 06:10:39 +01:00
|
|
|
std::vector<ItemStack> m_items;
|
2010-12-22 10:29:06 +01:00
|
|
|
std::string m_name;
|
2017-06-17 19:11:28 +02:00
|
|
|
u32 m_size;
|
|
|
|
u32 m_width = 0;
|
2012-01-12 06:10:39 +01:00
|
|
|
IItemDefManager *m_itemdef;
|
2019-08-24 19:07:38 +02:00
|
|
|
bool m_dirty = true;
|
2010-12-22 10:29:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class Inventory
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
~Inventory();
|
|
|
|
|
|
|
|
void clear();
|
|
|
|
|
2012-01-12 06:10:39 +01:00
|
|
|
Inventory(IItemDefManager *itemdef);
|
2010-12-22 10:29:06 +01:00
|
|
|
Inventory(const Inventory &other);
|
|
|
|
Inventory & operator = (const Inventory &other);
|
2014-01-11 19:46:37 +01:00
|
|
|
bool operator == (const Inventory &other) const;
|
|
|
|
bool operator != (const Inventory &other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
2019-08-24 19:07:38 +02:00
|
|
|
// Never ever serialize to disk using "incremental"!
|
|
|
|
void serialize(std::ostream &os, bool incremental = false) const;
|
2012-01-12 06:10:39 +01:00
|
|
|
void deSerialize(std::istream &is);
|
2010-12-22 10:29:06 +01:00
|
|
|
|
|
|
|
InventoryList * addList(const std::string &name, u32 size);
|
|
|
|
InventoryList * getList(const std::string &name);
|
2011-08-10 11:38:49 +02:00
|
|
|
const InventoryList * getList(const std::string &name) const;
|
2012-06-02 10:01:28 +02:00
|
|
|
std::vector<const InventoryList*> getLists();
|
2010-12-22 10:29:06 +01:00
|
|
|
bool deleteList(const std::string &name);
|
2012-01-12 06:10:39 +01:00
|
|
|
// A shorthand for adding items. Returns leftover item (possibly empty).
|
|
|
|
ItemStack addItem(const std::string &listname, const ItemStack &newitem)
|
2010-12-22 10:29:06 +01:00
|
|
|
{
|
|
|
|
InventoryList *list = getList(listname);
|
|
|
|
if(list == NULL)
|
2010-12-24 16:08:50 +01:00
|
|
|
return newitem;
|
2010-12-22 10:29:06 +01:00
|
|
|
return list->addItem(newitem);
|
|
|
|
}
|
2014-09-02 18:53:20 +02:00
|
|
|
|
2019-08-24 19:07:38 +02:00
|
|
|
inline bool checkModified() const
|
2014-09-02 18:53:20 +02:00
|
|
|
{
|
2019-08-24 19:07:38 +02:00
|
|
|
if (m_dirty)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (const auto &list : m_lists)
|
|
|
|
if (list->checkModified())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2014-09-02 18:53:20 +02:00
|
|
|
}
|
|
|
|
|
2019-09-18 18:47:09 +02:00
|
|
|
inline void setModified(bool dirty = true)
|
2014-09-02 18:53:20 +02:00
|
|
|
{
|
2019-08-24 19:07:38 +02:00
|
|
|
m_dirty = dirty;
|
2019-09-18 18:47:09 +02:00
|
|
|
// Set all as handled
|
|
|
|
if (!dirty) {
|
|
|
|
for (const auto &list : m_lists)
|
|
|
|
list->setModified(dirty);
|
|
|
|
}
|
2014-09-02 18:53:20 +02:00
|
|
|
}
|
2010-12-22 10:29:06 +01:00
|
|
|
private:
|
|
|
|
// -1 if not found
|
2011-08-10 11:38:49 +02:00
|
|
|
const s32 getListIndex(const std::string &name) const;
|
2010-12-22 10:29:06 +01:00
|
|
|
|
2012-01-12 06:10:39 +01:00
|
|
|
std::vector<InventoryList*> m_lists;
|
|
|
|
IItemDefManager *m_itemdef;
|
2019-08-24 19:07:38 +02:00
|
|
|
bool m_dirty = true;
|
2010-11-27 00:02:21 +01:00
|
|
|
};
|