Notify other players of wielded item change
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Thu, 11 Aug 2011 05:02:57 +0000 (07:02 +0200)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Thu, 11 Aug 2011 13:22:36 +0000 (15:22 +0200)
src/client.cpp
src/clientserver.h
src/server.cpp
src/server.h

index 398b2602dd3f8540ee2c561fda56a03e87e9645c..edce25381148313ab47259a20d92cd6fa947d075 100644 (file)
@@ -1549,6 +1549,47 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                // get damage from falling on ground
                m_ignore_damage_timer = 3.0;
        }
+       else if(command == TOCLIENT_PLAYERITEM)
+       {
+               std::string datastring((char*)&data[2], datasize-2);
+               std::istringstream is(datastring, std::ios_base::binary);
+
+               u16 count = readU16(is);
+
+               for (u16 i = 0; i < count; ++i) {
+                       u16 peer_id = readU16(is);
+                       Player *player = m_env.getPlayer(peer_id);
+
+                       if (player == NULL)
+                       {
+                               dout_client<<DTIME<<"Client: ignoring player item "
+                                       << deSerializeString(is)
+                                       << " for non-existing peer id " << peer_id
+                                       << std::endl;
+                               continue;
+                       } else if (player->isLocal()) {
+                               dout_client<<DTIME<<"Client: ignoring player item "
+                                       << deSerializeString(is)
+                                       << " for local player" << std::endl;
+                               continue;
+                       } else {
+                               InventoryList *inv = player->inventory.getList("main");
+                               std::string itemstring(deSerializeString(is));
+                               if (itemstring.empty()) {
+                                       inv->deleteItem(0);
+                                       dout_client<<DTIME
+                                               <<"Client: empty player item for peer "
+                                               << peer_id << std::endl;
+                               } else {
+                                       std::istringstream iss(itemstring);
+                                       delete inv->changeItem(0, InventoryItem::deSerialize(iss));
+                                       dout_client<<DTIME<<"Client: player item for peer " << peer_id << ": ";
+                                       player->getWieldItem()->serialize(dout_client);
+                                       dout_client<<std::endl;
+                               }
+                       }
+               }
+       }
        else
        {
                dout_client<<DTIME<<"WARNING: Client: Ignoring unknown command "
index 717e657b710deb4982e97c5e4fff921a93cd8b80..9b8b45c8500ef2ac2c6177923e0d24093fc3229b 100644 (file)
@@ -160,6 +160,13 @@ enum ToClientCommand
                u16 reason_length
                wstring reason
        */
+
+       TOCLIENT_PLAYERITEM = 0x36,
+       /*
+               u16 command
+               u16 peer id
+               string serialized item
+       */
 };
 
 enum ToServerCommand
index e3747775a22ae91c0a6295a855da12b8854a6602..176a13e38f7186dfb78c3af3ad511679810bd0d6 100644 (file)
@@ -2157,6 +2157,9 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                UpdateCrafting(peer->id);
                SendInventory(peer->id);
 
+               // Send player items to all players
+               SendPlayerItems();
+
                // Send HP
                {
                        Player *player = m_env.getPlayer(peer_id);
@@ -3388,6 +3391,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
 
                u16 item = readU16(&data[2]);
                player->wieldItem(item);
+               SendWieldedItem(player);
        }
        else
        {
@@ -3673,6 +3677,60 @@ void Server::SendInventory(u16 peer_id)
        m_con.Send(peer_id, 0, data, true);
 }
 
+std::string getWieldedItemString(const Player *player)
+{
+       const InventoryItem *item = player->getWieldItem();
+       if (item == NULL)
+               return std::string("");
+       std::ostringstream os(std::ios_base::binary);
+       item->serialize(os);
+       return os.str();
+}
+
+void Server::SendWieldedItem(const Player* player)
+{
+       DSTACK(__FUNCTION_NAME);
+
+       assert(player);
+
+       std::ostringstream os(std::ios_base::binary);
+
+       writeU16(os, TOCLIENT_PLAYERITEM);
+       writeU16(os, 1);
+       writeU16(os, player->peer_id);
+       os<<serializeString(getWieldedItemString(player));
+
+       // Make data buffer
+       std::string s = os.str();
+       SharedBuffer<u8> data((u8*)s.c_str(), s.size());
+
+       m_con.SendToAll(0, data, true);
+}
+
+void Server::SendPlayerItems()
+{
+       DSTACK(__FUNCTION_NAME);
+
+       std::ostringstream os(std::ios_base::binary);
+       core::list<Player *> players = m_env.getPlayers(true);
+
+       writeU16(os, TOCLIENT_PLAYERITEM);
+       writeU16(os, players.size());
+       core::list<Player *>::Iterator i;
+       for(i = players.begin(); i != players.end(); ++i)
+       {
+               Player *p = *i;
+               writeU16(os, p->peer_id);
+               os<<serializeString(getWieldedItemString(p));
+       }
+
+       // Make data buffer
+       std::string s = os.str();
+       SharedBuffer<u8> data((u8*)s.c_str(), s.size());
+
+       m_con.SendToAll(0, data, true);
+}
+
 void Server::SendChatMessage(u16 peer_id, const std::wstring &message)
 {
        DSTACK(__FUNCTION_NAME);
index 4a2d667a8a91e13f60b312d7d464e9a6c78109ab..b10703e707803ee671c5e8ca9cdf4ac93621890a 100644 (file)
@@ -479,6 +479,10 @@ private:
        void SendObjectData(float dtime);
        void SendPlayerInfos();
        void SendInventory(u16 peer_id);
+       // send wielded item info about player to all
+       void SendWieldedItem(const Player *player);
+       // send wielded item info about all players to all players
+       void SendPlayerItems();
        void SendChatMessage(u16 peer_id, const std::wstring &message);
        void BroadcastChatMessage(const std::wstring &message);
        void SendPlayerHP(Player *player);