Fixed some problems with crafting and inventory

This commit is contained in:
Perttu Ahola 2011-04-11 21:28:04 +03:00
parent 81ac026e1f
commit 249c0dc68a
6 changed files with 75 additions and 34 deletions

@ -278,6 +278,7 @@ InventoryList::InventoryList(std::string name, u32 size)
m_name = name; m_name = name;
m_size = size; m_size = size;
clearItems(); clearItems();
//m_dirty = false;
} }
InventoryList::~InventoryList() InventoryList::~InventoryList()
@ -303,6 +304,8 @@ void InventoryList::clearItems()
{ {
m_items.push_back(NULL); m_items.push_back(NULL);
} }
//setDirty(true);
} }
void InventoryList::serialize(std::ostream &os) void InventoryList::serialize(std::ostream &os)
@ -396,6 +399,7 @@ InventoryList & InventoryList::operator = (const InventoryList &other)
m_items[i] = item->clone(); m_items[i] = item->clone();
} }
} }
//setDirty(true);
return *this; return *this;
} }
@ -440,6 +444,7 @@ InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
InventoryItem *olditem = m_items[i]; InventoryItem *olditem = m_items[i];
m_items[i] = newitem; m_items[i] = newitem;
//setDirty(true);
return olditem; return olditem;
} }
@ -493,6 +498,8 @@ InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
if(newitem == NULL) if(newitem == NULL)
return NULL; return NULL;
//setDirty(true);
// If it is an empty position, it's an easy job. // If it is an empty position, it's an easy job.
InventoryItem *to_item = m_items[i]; InventoryItem *to_item = m_items[i];
if(to_item == NULL) if(to_item == NULL)
@ -550,6 +557,8 @@ InventoryItem * InventoryList::takeItem(u32 i, u32 count)
if(count == 0) if(count == 0)
return NULL; return NULL;
//setDirty(true);
InventoryItem *item = m_items[i]; InventoryItem *item = m_items[i];
// If it is an empty position, return NULL // If it is an empty position, return NULL
if(item == NULL) if(item == NULL)

@ -456,6 +456,9 @@ public:
u32 getUsedSlots(); u32 getUsedSlots();
u32 getFreeSlots(); u32 getFreeSlots();
/*bool getDirty(){ return m_dirty; }
void setDirty(bool dirty=true){ m_dirty = dirty; }*/
// Get pointer to item // Get pointer to item
InventoryItem * getItem(u32 i); InventoryItem * getItem(u32 i);
// Returns old item (or NULL). Parameter can be NULL. // Returns old item (or NULL). Parameter can be NULL.
@ -490,6 +493,7 @@ private:
core::array<InventoryItem*> m_items; core::array<InventoryItem*> m_items;
u32 m_size; u32 m_size;
std::string m_name; std::string m_name;
//bool m_dirty;
}; };
class Inventory class Inventory

@ -32,6 +32,7 @@ Player::Player():
in_water(false), in_water(false),
in_water_stable(false), in_water_stable(false),
swimming_up(false), swimming_up(false),
craftresult_is_preview(true),
peer_id(PEER_ID_INEXISTENT), peer_id(PEER_ID_INEXISTENT),
m_pitch(0), m_pitch(0),
m_yaw(0), m_yaw(0),
@ -100,6 +101,7 @@ void Player::serialize(std::ostream &os)
args.setFloat("pitch", m_pitch); args.setFloat("pitch", m_pitch);
args.setFloat("yaw", m_yaw); args.setFloat("yaw", m_yaw);
args.setV3F("position", m_position); args.setV3F("position", m_position);
args.setBool("craftresult_is_preview", craftresult_is_preview);
args.writeLines(os); args.writeLines(os);
@ -131,6 +133,11 @@ void Player::deSerialize(std::istream &is)
m_pitch = args.getFloat("pitch"); m_pitch = args.getFloat("pitch");
m_yaw = args.getFloat("yaw"); m_yaw = args.getFloat("yaw");
m_position = args.getV3F("position"); m_position = args.getV3F("position");
try{
craftresult_is_preview = args.getBool("craftresult_is_preview");
}catch(SettingNotFoundException &e){
craftresult_is_preview = true;
}
inventory.deSerialize(is); inventory.deSerialize(is);
} }

@ -122,6 +122,8 @@ public:
Inventory inventory; Inventory inventory;
bool craftresult_is_preview;
u16 peer_id; u16 peer_id;
protected: protected:

@ -2649,48 +2649,54 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
if(ma->to_inv == "current_player" && if(ma->to_inv == "current_player" &&
ma->from_inv == "current_player") ma->from_inv == "current_player")
{ {
// Don't allow moving anything to craftresult
if(ma->to_list == "craftresult")
{
// Do nothing
disable_action = true;
}
// When something is removed from craftresult
if(ma->from_list == "craftresult")
{
disable_action = true;
// Remove stuff from craft
InventoryList *clist = player->inventory.getList("craft");
if(clist)
{
u16 count = ma->count;
if(count == 0)
count = 1;
clist->decrementMaterials(count);
}
// Do action
// Feed action to player inventory
//a->apply(&player->inventory);
a->apply(&c, this);
// Eat it
delete a;
// If something appeared in craftresult, throw it
// in the main list
InventoryList *rlist = player->inventory.getList("craftresult"); InventoryList *rlist = player->inventory.getList("craftresult");
assert(rlist);
InventoryList *clist = player->inventory.getList("craft");
assert(clist);
InventoryList *mlist = player->inventory.getList("main"); InventoryList *mlist = player->inventory.getList("main");
if(rlist && mlist && rlist->getUsedSlots() == 1) assert(mlist);
/*
Craftresult is no longer preview if something
is moved into it
*/
if(ma->to_list == "craftresult"
&& ma->from_list != "craftresult")
{ {
// If it currently is a preview, remove
// its contents
if(player->craftresult_is_preview)
{
rlist->deleteItem(0);
}
player->craftresult_is_preview = false;
}
/*
Crafting takes place if this condition is true.
*/
if(player->craftresult_is_preview &&
ma->from_list == "craftresult")
{
player->craftresult_is_preview = false;
clist->decrementMaterials(1);
}
/*
If the craftresult is placed on itself, move it to
main inventory instead of doing the action
*/
if(ma->to_list == "craftresult"
&& ma->from_list == "craftresult")
{
disable_action = true;
InventoryItem *item1 = rlist->changeItem(0, NULL); InventoryItem *item1 = rlist->changeItem(0, NULL);
mlist->addItem(item1); mlist->addItem(item1);
} }
} }
} }
}
if(disable_action == false) if(disable_action == false)
{ {
// Feed action to player inventory // Feed action to player inventory
//a->apply(&player->inventory);
a->apply(&c, this); a->apply(&c, this);
// Eat the action // Eat the action
delete a; delete a;
@ -3066,6 +3072,7 @@ void Server::SendInventory(u16 peer_id)
DSTACK(__FUNCTION_NAME); DSTACK(__FUNCTION_NAME);
Player* player = m_env.getPlayer(peer_id); Player* player = m_env.getPlayer(peer_id);
assert(player);
/* /*
Calculate crafting stuff Calculate crafting stuff
@ -3074,11 +3081,15 @@ void Server::SendInventory(u16 peer_id)
{ {
InventoryList *clist = player->inventory.getList("craft"); InventoryList *clist = player->inventory.getList("craft");
InventoryList *rlist = player->inventory.getList("craftresult"); InventoryList *rlist = player->inventory.getList("craftresult");
if(rlist)
if(rlist->getUsedSlots() == 0)
player->craftresult_is_preview = true;
if(rlist && player->craftresult_is_preview)
{ {
rlist->clearItems(); rlist->clearItems();
} }
if(clist && rlist) if(clist && rlist && player->craftresult_is_preview)
{ {
InventoryItem *items[9]; InventoryItem *items[9];
for(u16 i=0; i<9; i++) for(u16 i=0; i<9; i++)
@ -3355,8 +3366,8 @@ void Server::SendInventory(u16 peer_id)
found = true; found = true;
} }
} }
} }
} // if creative_mode == false } // if creative_mode == false
/* /*

@ -1331,6 +1331,14 @@ public:
return value; return value;
} }
void setBool(std::string name, bool value)
{
if(value)
set(name, "true");
else
set(name, "false");
}
void setS32(std::string name, s32 value) void setS32(std::string name, s32 value)
{ {
set(name, itos(value)); set(name, itos(value));