aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIlya Zhuravlev <zhuravlevilya@ya.ru>2012-08-20 01:29:56 +0400
committerPerttu Ahola <celeron55@gmail.com>2012-09-01 10:01:41 +0300
commit6a16075912d016926ee0361fb85f9979c119be52 (patch)
tree9f0b589efac5266eb51db71643e174187f83cf3e
parent43ebec2be1949aa5eac127df7cb902d37e4e461b (diff)
downloadminetest-6a16075912d016926ee0361fb85f9979c119be52.tar.gz
minetest-6a16075912d016926ee0361fb85f9979c119be52.tar.bz2
minetest-6a16075912d016926ee0361fb85f9979c119be52.zip
Add InventoryList width property & allow custom crafting grids.
-rw-r--r--doc/lua_api.txt2
-rw-r--r--src/inventory.cpp21
-rw-r--r--src/inventory.h4
-rw-r--r--src/inventorymanager.cpp10
-rw-r--r--src/player.cpp3
-rw-r--r--src/scriptapi.cpp33
-rw-r--r--src/test.cpp4
7 files changed, 69 insertions, 8 deletions
diff --git a/doc/lua_api.txt b/doc/lua_api.txt
index 8a6ea5dea..88c594eb2 100644
--- a/doc/lua_api.txt
+++ b/doc/lua_api.txt
@@ -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
diff --git a/src/inventory.cpp b/src/inventory.cpp
index 0a0a29cd5..fda11e40f 100644
--- a/src/inventory.cpp
+++ b/src/inventory.cpp
@@ -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;
diff --git a/src/inventory.h b/src/inventory.h
index a3c598256..5f90183d2 100644
--- a/src/inventory.h
+++ b/src/inventory.h
@@ -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;
};
diff --git a/src/inventorymanager.cpp b/src/inventorymanager.cpp
index ec4d47def..e2e537838 100644
--- a/src/inventorymanager.cpp
+++ b/src/inventorymanager.cpp
@@ -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]);
}
diff --git a/src/player.cpp b/src/player.cpp
index 2e084b415..36f12c77b 100644
--- a/src/player.cpp
+++ b/src/player.cpp
@@ -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);
diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp
index 95e2b5d01..81e96aec6 100644
--- a/src/scriptapi.cpp
+++ b/src/scriptapi.cpp
@@ -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),
diff --git a/src/test.cpp b/src/test.cpp
index fe5a4f232..f81f2910c 100644
--- a/src/test.cpp
+++ b/src/test.cpp
@@ -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);