From 249c0dc68a107878b7aaca0c8d2dcc3e08862430 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Mon, 11 Apr 2011 21:28:04 +0300 Subject: [PATCH] Fixed some problems with crafting and inventory --- src/inventory.cpp | 9 ++++++ src/inventory.h | 4 +++ src/player.cpp | 7 +++++ src/player.h | 2 ++ src/server.cpp | 79 +++++++++++++++++++++++++++-------------------- src/utility.h | 8 +++++ 6 files changed, 75 insertions(+), 34 deletions(-) diff --git a/src/inventory.cpp b/src/inventory.cpp index b14828ae..b6560063 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -278,6 +278,7 @@ InventoryList::InventoryList(std::string name, u32 size) m_name = name; m_size = size; clearItems(); + //m_dirty = false; } InventoryList::~InventoryList() @@ -303,6 +304,8 @@ void InventoryList::clearItems() { m_items.push_back(NULL); } + + //setDirty(true); } void InventoryList::serialize(std::ostream &os) @@ -396,6 +399,7 @@ InventoryList & InventoryList::operator = (const InventoryList &other) m_items[i] = item->clone(); } } + //setDirty(true); return *this; } @@ -440,6 +444,7 @@ InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem) InventoryItem *olditem = m_items[i]; m_items[i] = newitem; + //setDirty(true); return olditem; } @@ -493,6 +498,8 @@ InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem) if(newitem == NULL) return NULL; + //setDirty(true); + // If it is an empty position, it's an easy job. InventoryItem *to_item = m_items[i]; if(to_item == NULL) @@ -549,6 +556,8 @@ InventoryItem * InventoryList::takeItem(u32 i, u32 count) { if(count == 0) return NULL; + + //setDirty(true); InventoryItem *item = m_items[i]; // If it is an empty position, return NULL diff --git a/src/inventory.h b/src/inventory.h index 3ba65588..cb45371e 100644 --- a/src/inventory.h +++ b/src/inventory.h @@ -455,6 +455,9 @@ public: // Count used slots u32 getUsedSlots(); u32 getFreeSlots(); + + /*bool getDirty(){ return m_dirty; } + void setDirty(bool dirty=true){ m_dirty = dirty; }*/ // Get pointer to item InventoryItem * getItem(u32 i); @@ -490,6 +493,7 @@ private: core::array m_items; u32 m_size; std::string m_name; + //bool m_dirty; }; class Inventory diff --git a/src/player.cpp b/src/player.cpp index 3bde8a56..8f594eee 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -32,6 +32,7 @@ Player::Player(): in_water(false), in_water_stable(false), swimming_up(false), + craftresult_is_preview(true), peer_id(PEER_ID_INEXISTENT), m_pitch(0), m_yaw(0), @@ -100,6 +101,7 @@ void Player::serialize(std::ostream &os) args.setFloat("pitch", m_pitch); args.setFloat("yaw", m_yaw); args.setV3F("position", m_position); + args.setBool("craftresult_is_preview", craftresult_is_preview); args.writeLines(os); @@ -131,6 +133,11 @@ void Player::deSerialize(std::istream &is) m_pitch = args.getFloat("pitch"); m_yaw = args.getFloat("yaw"); m_position = args.getV3F("position"); + try{ + craftresult_is_preview = args.getBool("craftresult_is_preview"); + }catch(SettingNotFoundException &e){ + craftresult_is_preview = true; + } inventory.deSerialize(is); } diff --git a/src/player.h b/src/player.h index 710a9e0e..2eaeaae9 100644 --- a/src/player.h +++ b/src/player.h @@ -122,6 +122,8 @@ public: Inventory inventory; + bool craftresult_is_preview; + u16 peer_id; protected: diff --git a/src/server.cpp b/src/server.cpp index b4c3d71f..b08d568c 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2649,40 +2649,47 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) if(ma->to_inv == "current_player" && ma->from_inv == "current_player") { - // Don't allow moving anything to craftresult - if(ma->to_list == "craftresult") + InventoryList *rlist = player->inventory.getList("craftresult"); + assert(rlist); + InventoryList *clist = player->inventory.getList("craft"); + assert(clist); + InventoryList *mlist = player->inventory.getList("main"); + assert(mlist); + /* + Craftresult is no longer preview if something + is moved into it + */ + if(ma->to_list == "craftresult" + && ma->from_list != "craftresult") { - // Do nothing - disable_action = true; + // 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); } - // When something is removed from craftresult - if(ma->from_list == "craftresult") + /* + 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; - // 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 *mlist = player->inventory.getList("main"); - if(rlist && mlist && rlist->getUsedSlots() == 1) - { - InventoryItem *item1 = rlist->changeItem(0, NULL); - mlist->addItem(item1); - } + + InventoryItem *item1 = rlist->changeItem(0, NULL); + mlist->addItem(item1); } } } @@ -2690,7 +2697,6 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) if(disable_action == false) { // Feed action to player inventory - //a->apply(&player->inventory); a->apply(&c, this); // Eat the action delete a; @@ -3066,6 +3072,7 @@ void Server::SendInventory(u16 peer_id) DSTACK(__FUNCTION_NAME); Player* player = m_env.getPlayer(peer_id); + assert(player); /* Calculate crafting stuff @@ -3074,11 +3081,15 @@ void Server::SendInventory(u16 peer_id) { InventoryList *clist = player->inventory.getList("craft"); 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(); } - if(clist && rlist) + if(clist && rlist && player->craftresult_is_preview) { InventoryItem *items[9]; for(u16 i=0; i<9; i++) @@ -3355,8 +3366,8 @@ void Server::SendInventory(u16 peer_id) found = true; } } - } + } // if creative_mode == false /* diff --git a/src/utility.h b/src/utility.h index e3a97740..0b59ce6f 100644 --- a/src/utility.h +++ b/src/utility.h @@ -1331,6 +1331,14 @@ public: 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) { set(name, itos(value)); -- 2.30.2