- 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
{
m_name = name;
m_size = size;
+ m_width = 0;
m_itemdef = itemdef;
clearItems();
//m_dirty = false;
m_size = newsize;
}
+void InventoryList::setWidth(u32 newwidth)
+{
+ m_width = newwidth;
+}
+
void InventoryList::setName(const std::string &name)
{
m_name = name;
{
//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];
clearItems();
u32 item_i = 0;
+ m_width = 0;
for(;;)
{
{
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)
{
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);
return m_items.size();
}
+u32 InventoryList::getWidth() const
+{
+ return m_width;
+}
+
u32 InventoryList::getUsedSlots() const
{
u32 num = 0;
~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);
const std::string &getName() const;
u32 getSize() const;
+ u32 getWidth() const;
// Count used slots
u32 getUsedSlots() const;
u32 getFreeSlots() const;
private:
std::vector<ItemStack> m_items;
- u32 m_size;
+ u32 m_size, m_width;
std::string m_name;
IItemDefManager *m_itemdef;
};
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
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]);
}
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);
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)
{
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)
{
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),
{
std::string serialized_inventory =
"List 0 32\n"
+ "Width 3\n"
"Empty\n"
"Empty\n"
"Empty\n"
std::string serialized_inventory_2 =
"List main 32\n"
+ "Width 5\n"
"Empty\n"
"Empty\n"
"Empty\n"
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);