Fixed some problems with crafting and inventory
authorPerttu Ahola <celeron55@gmail.com>
Mon, 11 Apr 2011 18:28:04 +0000 (21:28 +0300)
committerPerttu Ahola <celeron55@gmail.com>
Mon, 11 Apr 2011 18:28:04 +0000 (21:28 +0300)
src/inventory.cpp
src/inventory.h
src/player.cpp
src/player.h
src/server.cpp
src/utility.h

index b14828ae22ba3250c9aee688cee40c4434a38137..b6560063f39db38e4f62e7e9eeeebad233f69f37 100644 (file)
@@ -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
index 3ba655880667f4a880bc03d423944d211a011e76..cb45371e4a25b68bf6e58081b3d3aa865c5bc3fb 100644 (file)
@@ -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<InventoryItem*> m_items;
        u32 m_size;
        std::string m_name;
+       //bool m_dirty;
 };
 
 class Inventory
index 3bde8a56324f0bf193ab4c8a90ca29cc87f01d4b..8f594eee638ee89d75673b7a2e07494167993c48 100644 (file)
@@ -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);
 }
index 710a9e0ed7c61bffc17385e4e1b869bcce3b45e7..2eaeaae9a6cfdf6e9ce7f03371d2467676e931c7 100644 (file)
@@ -122,6 +122,8 @@ public:
        
        Inventory inventory;
 
+       bool craftresult_is_preview;
+
        u16 peer_id;
 
 protected:
index b4c3d71f4670ab64b773a7f8b882378a34515be0..b08d568cc04798cfb83f4513119d725e93c9a850 100644 (file)
@@ -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
 
        /*
index e3a97740086326d734fdb59b833973bf340352aa..0b59ce6fdae6d8a5905c8c4a09cd914727d36e7b 100644 (file)
@@ -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));