Removed unused camera_position and camera_direction fields from Client. Moved ClientE...
[oweals/minetest.git] / src / client.cpp
index 02f78e233293236c193b08ab5ef7d1dfa1206016..df792d116e616049bb8c4fc389c839b2c5583c24 100644 (file)
@@ -189,8 +189,6 @@ Client::Client(
        ),
        m_con(PROTOCOL_ID, 512, CONNECTION_TIMEOUT, this),
        m_device(device),
-       camera_position(0,0,0),
-       camera_direction(0,0,1),
        m_server_ser_ver(SER_FMT_VER_INVALID),
        m_inventory_updated(false),
        m_time_of_day(0),
@@ -1417,7 +1415,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                                for all added objects {
                                        u16 id
                                        u8 type
-                                       u16 initialization data length
+                                       u32 initialization data length
                                        string initialization data
                                }
                        */
@@ -1549,6 +1547,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 "
@@ -1864,6 +1903,28 @@ void Client::sendPlayerPos()
        Send(0, data, false);
 }
 
+void Client::sendPlayerItem(u16 item)
+{
+       Player *myplayer = m_env.getLocalPlayer();
+       if(myplayer == NULL)
+               return;
+
+       u16 our_peer_id = m_con.GetPeerID();
+
+       // Set peer id if not set already
+       if(myplayer->peer_id == PEER_ID_INEXISTENT)
+               myplayer->peer_id = our_peer_id;
+       // Check that an existing peer_id is the same as the connection's
+       assert(myplayer->peer_id == our_peer_id);
+
+       SharedBuffer<u8> data(2+2);
+       writeU16(&data[0], TOSERVER_PLAYERITEM);
+       writeU16(&data[2], item);
+
+       // Send as reliable
+       Send(0, data, true);
+}
+
 void Client::removeNode(v3s16 p)
 {
        //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
@@ -1920,8 +1981,11 @@ void Client::addNode(v3s16 p, MapNode n)
 void Client::updateCamera(v3f pos, v3f dir)
 {
        m_env.getClientMap().updateCamera(pos, dir);
-       camera_position = pos;
-       camera_direction = dir;
+}
+
+void Client::renderPostFx()
+{
+       m_env.getClientMap().renderPostFx();
 }
 
 MapNode Client::getNode(v3s16 p)
@@ -1935,11 +1999,18 @@ NodeMetadata* Client::getNodeMetadata(v3s16 p)
        return m_env.getMap().getNodeMetadata(p);
 }
 
-v3f Client::getPlayerPosition()
+LocalPlayer* Client::getLocalPlayer()
+{
+       return m_env.getLocalPlayer();
+}
+
+v3f Client::getPlayerPosition(v3f *eye_position)
 {
        //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
        LocalPlayer *player = m_env.getLocalPlayer();
        assert(player != NULL);
+       if (eye_position)
+               *eye_position = player->getEyePosition();
        return player->getPosition();
 }
 
@@ -1951,6 +2022,16 @@ void Client::setPlayerControl(PlayerControl &control)
        player->control = control;
 }
 
+void Client::selectPlayerItem(u16 item)
+{
+       LocalPlayer *player = m_env.getLocalPlayer();
+       assert(player != NULL);
+
+       player->wieldItem(item);
+
+       sendPlayerItem(item);
+}
+
 // Returns true if the inventory of the local player has been
 // updated from the server. If it is true, it is set to false.
 bool Client::getLocalInventoryUpdated()