Removed unused camera_position and camera_direction fields from Client. Moved ClientE...
[oweals/minetest.git] / src / client.cpp
index 449b0c2f26ccaedf024a85158120fd100633b10d..df792d116e616049bb8c4fc389c839b2c5583c24 100644 (file)
@@ -5,7 +5,7 @@ Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
-MeshUpdateQueue::(at your option) any later version.
+(at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -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),
@@ -199,7 +197,7 @@ Client::Client(
        m_access_denied(false)
 {
        m_packetcounter_timer = 0.0;
-       m_delete_unused_sectors_timer = 0.0;
+       //m_delete_unused_sectors_timer = 0.0;
        m_connection_reinit_timer = 0.0;
        m_avg_rtt_timer = 0.0;
        m_playerpos_send_timer = 0.0;
@@ -303,7 +301,11 @@ void Client::step(float dtime)
                        m_packetcounter.clear();
                }
        }
+       
+       // Get connection status
+       bool connected = connectedAndInitialized();
 
+#if 0
        {
                /*
                        Delete unused sectors
@@ -392,8 +394,7 @@ void Client::step(float dtime)
                        }
                }
        }
-
-       bool connected = connectedAndInitialized();
+#endif
 
        if(connected == false)
        {
@@ -412,19 +413,23 @@ void Client::step(float dtime)
                        // [0] u16 TOSERVER_INIT
                        // [2] u8 SER_FMT_VER_HIGHEST
                        // [3] u8[20] player_name
-                       // [23] u8[28] password
-                       SharedBuffer<u8> data(2+1+PLAYERNAME_SIZE+PASSWORD_SIZE);
+                       // [23] u8[28] password (new in some version)
+                       // [51] u16 client network protocol version (new in some version)
+                       SharedBuffer<u8> data(2+1+PLAYERNAME_SIZE+PASSWORD_SIZE+2);
                        writeU16(&data[0], TOSERVER_INIT);
                        writeU8(&data[2], SER_FMT_VER_HIGHEST);
 
                        memset((char*)&data[3], 0, PLAYERNAME_SIZE);
                        snprintf((char*)&data[3], PLAYERNAME_SIZE, "%s", myplayer->getName());
 
-                       /*dstream<<"Client: password hash is \""<<m_password<<"\""
+                       /*dstream<<"Client: sending initial password hash: \""<<m_password<<"\""
                                        <<std::endl;*/
 
                        memset((char*)&data[23], 0, PASSWORD_SIZE);
                        snprintf((char*)&data[23], PASSWORD_SIZE, "%s", m_password.c_str());
+                       
+                       // This should be incremented in each version
+                       writeU16(&data[51], 1);
 
                        // Send as unreliable
                        Send(0, data, false);
@@ -438,6 +443,67 @@ void Client::step(float dtime)
                Do stuff if connected
        */
        
+       /*
+               Run Map's timers and unload unused data
+       */
+       const float map_timer_and_unload_dtime = 5.25;
+       if(m_map_timer_and_unload_interval.step(dtime, map_timer_and_unload_dtime))
+       {
+               ScopeProfiler sp(&g_profiler, "Client: map timer and unload");
+               core::list<v3s16> deleted_blocks;
+               m_env.getMap().timerUpdate(map_timer_and_unload_dtime,
+                               g_settings.getFloat("client_unload_unused_data_timeout"),
+                               &deleted_blocks);
+                               
+               /*if(deleted_blocks.size() > 0)
+                       dstream<<"Client: Unloaded "<<deleted_blocks.size()
+                                       <<" unused blocks"<<std::endl;*/
+                       
+               /*
+                       Send info to server
+                       NOTE: This loop is intentionally iterated the way it is.
+               */
+
+               core::list<v3s16>::Iterator i = deleted_blocks.begin();
+               core::list<v3s16> sendlist;
+               for(;;)
+               {
+                       if(sendlist.size() == 255 || i == deleted_blocks.end())
+                       {
+                               if(sendlist.size() == 0)
+                                       break;
+                               /*
+                                       [0] u16 command
+                                       [2] u8 count
+                                       [3] v3s16 pos_0
+                                       [3+6] v3s16 pos_1
+                                       ...
+                               */
+                               u32 replysize = 2+1+6*sendlist.size();
+                               SharedBuffer<u8> reply(replysize);
+                               writeU16(&reply[0], TOSERVER_DELETEDBLOCKS);
+                               reply[2] = sendlist.size();
+                               u32 k = 0;
+                               for(core::list<v3s16>::Iterator
+                                               j = sendlist.begin();
+                                               j != sendlist.end(); j++)
+                               {
+                                       writeV3S16(&reply[2+1+6*k], *j);
+                                       k++;
+                               }
+                               m_con.Send(PEER_ID_SERVER, 1, reply, true);
+
+                               if(i == deleted_blocks.end())
+                                       break;
+
+                               sendlist.clear();
+                       }
+
+                       sendlist.push_back(*i);
+                       i++;
+               }
+       }
+
        /*
                Handle environment
        */
@@ -453,23 +519,23 @@ void Client::step(float dtime)
                //TimeTaker envtimer("env step", m_device);
                // Step environment
                m_env.step(dtime);
-
-               // Step active blocks
+               
+               /*
+                       Handle active blocks
+                       NOTE: These old objects are DEPRECATED. TODO: Remove
+               */
                for(core::map<v3s16, bool>::Iterator
                                i = m_active_blocks.getIterator();
                                i.atEnd() == false; i++)
                {
                        v3s16 p = i.getNode()->getKey();
 
-                       MapBlock *block = NULL;
-                       try
-                       {
-                               block = m_env.getMap().getBlockNoCreate(p);
-                               block->stepObjects(dtime, false, m_env.getDayNightRatio());
-                       }
-                       catch(InvalidPositionException &e)
-                       {
-                       }
+                       MapBlock *block = m_env.getMap().getBlockNoCreateNoEx(p);
+                       if(block == NULL)
+                               continue;
+                       
+                       // Step MapBlockObjects
+                       block->stepObjects(dtime, false, m_env.getDayNightRatio());
                }
 
                /*
@@ -868,7 +934,6 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                */
 
                //m_env.getClientMap().updateMeshes(block->getPos(), getDayNightRatio());
-               
                /*
                        Add it to mesh update queue and set it to be acknowledged after update.
                */
@@ -1183,6 +1248,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
 
                /*
                        Read block objects
+                       NOTE: Deprecated stuff here, TODO: Remove
                */
 
                // Read active block count
@@ -1349,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
                                }
                        */
@@ -1481,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 "
@@ -1796,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
@@ -1852,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)
@@ -1867,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();
 }
 
@@ -1883,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()