mirror of
https://github.com/minetest/minetest.git
synced 2024-08-18 20:44:16 +02:00
Add InventoryList width property & allow custom crafting grids.
This commit is contained in:
parent
43ebec2be1
commit
6a16075912
@ -1118,6 +1118,8 @@ methods:
|
||||
- is_empty(listname): return true if list is empty
|
||||
- get_size(listname): get size of a list
|
||||
- set_size(listname, size): set size of a list
|
||||
- get_width(listname): get width of a list
|
||||
- set_width(listname, width): set width of list; currently used for crafting
|
||||
- get_stack(listname, i): get a copy of stack index i in list
|
||||
- set_stack(listname, i, stack): copy stack to index i in list
|
||||
- get_list(listname): return full list
|
||||
|
@ -431,6 +431,7 @@ InventoryList::InventoryList(std::string name, u32 size, IItemDefManager *itemde
|
||||
{
|
||||
m_name = name;
|
||||
m_size = size;
|
||||
m_width = 0;
|
||||
m_itemdef = itemdef;
|
||||
clearItems();
|
||||
//m_dirty = false;
|
||||
@ -459,6 +460,11 @@ void InventoryList::setSize(u32 newsize)
|
||||
m_size = newsize;
|
||||
}
|
||||
|
||||
void InventoryList::setWidth(u32 newwidth)
|
||||
{
|
||||
m_width = newwidth;
|
||||
}
|
||||
|
||||
void InventoryList::setName(const std::string &name)
|
||||
{
|
||||
m_name = name;
|
||||
@ -468,6 +474,8 @@ void InventoryList::serialize(std::ostream &os) const
|
||||
{
|
||||
//os.imbue(std::locale("C"));
|
||||
|
||||
os<<"Width "<<m_width<<"\n";
|
||||
|
||||
for(u32 i=0; i<m_items.size(); i++)
|
||||
{
|
||||
const ItemStack &item = m_items[i];
|
||||
@ -492,6 +500,7 @@ void InventoryList::deSerialize(std::istream &is)
|
||||
|
||||
clearItems();
|
||||
u32 item_i = 0;
|
||||
m_width = 0;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
@ -513,6 +522,12 @@ void InventoryList::deSerialize(std::istream &is)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else if(name == "Width")
|
||||
{
|
||||
iss >> m_width;
|
||||
if (iss.fail())
|
||||
throw SerializationError("incorrect width property");
|
||||
}
|
||||
else if(name == "Item")
|
||||
{
|
||||
if(item_i > getSize() - 1)
|
||||
@ -543,6 +558,7 @@ InventoryList & InventoryList::operator = (const InventoryList &other)
|
||||
{
|
||||
m_items = other.m_items;
|
||||
m_size = other.m_size;
|
||||
m_width = other.m_width;
|
||||
m_name = other.m_name;
|
||||
m_itemdef = other.m_itemdef;
|
||||
//setDirty(true);
|
||||
@ -560,6 +576,11 @@ u32 InventoryList::getSize() const
|
||||
return m_items.size();
|
||||
}
|
||||
|
||||
u32 InventoryList::getWidth() const
|
||||
{
|
||||
return m_width;
|
||||
}
|
||||
|
||||
u32 InventoryList::getUsedSlots() const
|
||||
{
|
||||
u32 num = 0;
|
||||
|
@ -176,6 +176,7 @@ public:
|
||||
~InventoryList();
|
||||
void clearItems();
|
||||
void setSize(u32 newsize);
|
||||
void setWidth(u32 newWidth);
|
||||
void setName(const std::string &name);
|
||||
void serialize(std::ostream &os) const;
|
||||
void deSerialize(std::istream &is);
|
||||
@ -185,6 +186,7 @@ public:
|
||||
|
||||
const std::string &getName() const;
|
||||
u32 getSize() const;
|
||||
u32 getWidth() const;
|
||||
// Count used slots
|
||||
u32 getUsedSlots() const;
|
||||
u32 getFreeSlots() const;
|
||||
@ -240,7 +242,7 @@ public:
|
||||
|
||||
private:
|
||||
std::vector<ItemStack> m_items;
|
||||
u32 m_size;
|
||||
u32 m_size, m_width;
|
||||
std::string m_name;
|
||||
IItemDefManager *m_itemdef;
|
||||
};
|
||||
|
@ -769,18 +769,16 @@ bool getCraftingResult(Inventory *inv, ItemStack& result,
|
||||
|
||||
result.clear();
|
||||
|
||||
// TODO: Allow different sizes of crafting grids
|
||||
|
||||
// Get the InventoryList in which we will operate
|
||||
InventoryList *clist = inv->getList("craft");
|
||||
if(!clist || clist->getSize() != 9)
|
||||
if(!clist)
|
||||
return false;
|
||||
|
||||
// Mangle crafting grid to an another format
|
||||
CraftInput ci;
|
||||
ci.method = CRAFT_METHOD_NORMAL;
|
||||
ci.width = 3;
|
||||
for(u16 i=0; i<9; i++)
|
||||
ci.width = clist->getWidth() ? clist->getWidth() : 3;
|
||||
for(u16 i=0; i<clist->getSize(); i++)
|
||||
ci.items.push_back(clist->getItem(i));
|
||||
|
||||
// Find out what is crafted and add it to result item slot
|
||||
@ -793,7 +791,7 @@ bool getCraftingResult(Inventory *inv, ItemStack& result,
|
||||
if(found && decrementInput)
|
||||
{
|
||||
// CraftInput has been changed, apply changes in clist
|
||||
for(u16 i=0; i<9; i++)
|
||||
for(u16 i=0; i<clist->getSize(); i++)
|
||||
{
|
||||
clist->changeItem(i, ci.items[i]);
|
||||
}
|
||||
|
@ -45,7 +45,8 @@ Player::Player(IGameDef *gamedef):
|
||||
updateName("<not set>");
|
||||
inventory.clear();
|
||||
inventory.addList("main", PLAYER_INVENTORY_SIZE);
|
||||
inventory.addList("craft", 9);
|
||||
InventoryList *craft = inventory.addList("craft", 9);
|
||||
craft->setWidth(3);
|
||||
inventory.addList("craftpreview", 1);
|
||||
inventory.addList("craftresult", 1);
|
||||
|
||||
|
@ -1847,6 +1847,20 @@ private:
|
||||
return 1;
|
||||
}
|
||||
|
||||
// get_width(self, listname)
|
||||
static int l_get_width(lua_State *L)
|
||||
{
|
||||
InvRef *ref = checkobject(L, 1);
|
||||
const char *listname = luaL_checkstring(L, 2);
|
||||
InventoryList *list = getlist(L, ref, listname);
|
||||
if(list){
|
||||
lua_pushinteger(L, list->getWidth());
|
||||
} else {
|
||||
lua_pushinteger(L, 0);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
// set_size(self, listname, size)
|
||||
static int l_set_size(lua_State *L)
|
||||
{
|
||||
@ -1869,6 +1883,23 @@ private:
|
||||
return 0;
|
||||
}
|
||||
|
||||
// set_width(self, listname, size)
|
||||
static int l_set_width(lua_State *L)
|
||||
{
|
||||
InvRef *ref = checkobject(L, 1);
|
||||
const char *listname = luaL_checkstring(L, 2);
|
||||
int newwidth = luaL_checknumber(L, 3);
|
||||
Inventory *inv = getinv(L, ref);
|
||||
InventoryList *list = inv->getList(listname);
|
||||
if(list){
|
||||
list->setWidth(newwidth);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
reportInventoryChange(L, ref);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// get_stack(self, listname, i) -> itemstack
|
||||
static int l_get_stack(lua_State *L)
|
||||
{
|
||||
@ -2062,6 +2093,8 @@ const luaL_reg InvRef::methods[] = {
|
||||
method(InvRef, is_empty),
|
||||
method(InvRef, get_size),
|
||||
method(InvRef, set_size),
|
||||
method(InvRef, get_width),
|
||||
method(InvRef, set_width),
|
||||
method(InvRef, get_stack),
|
||||
method(InvRef, set_stack),
|
||||
method(InvRef, get_list),
|
||||
|
@ -698,6 +698,7 @@ struct TestInventory: public TestBase
|
||||
{
|
||||
std::string serialized_inventory =
|
||||
"List 0 32\n"
|
||||
"Width 3\n"
|
||||
"Empty\n"
|
||||
"Empty\n"
|
||||
"Empty\n"
|
||||
@ -735,6 +736,7 @@ struct TestInventory: public TestBase
|
||||
|
||||
std::string serialized_inventory_2 =
|
||||
"List main 32\n"
|
||||
"Width 5\n"
|
||||
"Empty\n"
|
||||
"Empty\n"
|
||||
"Empty\n"
|
||||
@ -778,6 +780,8 @@ struct TestInventory: public TestBase
|
||||
inv.getList("0")->setName("main");
|
||||
UASSERT(!inv.getList("0"));
|
||||
UASSERT(inv.getList("main"));
|
||||
UASSERT(inv.getList("main")->getWidth() == 3);
|
||||
inv.getList("main")->setWidth(5);
|
||||
std::ostringstream inv_os(std::ios::binary);
|
||||
inv.serialize(inv_os);
|
||||
UASSERT(inv_os.str() == serialized_inventory_2);
|
||||
|
Loading…
Reference in New Issue
Block a user