]> git.lizzy.rs Git - minetest.git/blobdiff - src/client.cpp
Fix local map saving when joining a local server from the server tab
[minetest.git] / src / client.cpp
index 72964026813e9c813b5fd3e65dee7bdfc315d0f6..cb6419fade8f2f0d7e5a4451a774a417543e3ab3 100644 (file)
@@ -17,13 +17,19 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-#include "client.h"
 #include <iostream>
 #include <algorithm>
-#include "clientserver.h"
+#include <sstream>
+#include <IFileSystem.h>
 #include "jthread/jmutexautolock.h"
+#include "util/directiontables.h"
+#include "util/pointedthing.h"
+#include "util/serialize.h"
+#include "util/string.h"
+#include "strfnd.h"
+#include "client.h"
+#include "clientserver.h"
 #include "main.h"
-#include <sstream>
 #include "filesys.h"
 #include "porting.h"
 #include "mapsector.h"
@@ -37,20 +43,21 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "nodedef.h"
 #include "itemdef.h"
 #include "shader.h"
-#include <IFileSystem.h>
 #include "base64.h"
 #include "clientmap.h"
 #include "clientmedia.h"
 #include "sound.h"
-#include "util/string.h"
 #include "IMeshCache.h"
 #include "serialization.h"
-#include "util/serialize.h"
 #include "config.h"
-#include "cmake_config_githash.h"
-#include "util/directiontables.h"
-#include "util/pointedthing.h"
 #include "version.h"
+#include "drawscene.h"
+#include "subgame.h"
+#include "server.h"
+#include "database.h"
+#include "database-sqlite3.h"
+
+extern gui::IGUIEnvironment* guienv;
 
 /*
        QueuedMeshUpdate
@@ -72,7 +79,7 @@ QueuedMeshUpdate::~QueuedMeshUpdate()
 /*
        MeshUpdateQueue
 */
-       
+
 MeshUpdateQueue::MeshUpdateQueue()
 {
 }
@@ -123,7 +130,7 @@ void MeshUpdateQueue::addBlock(v3s16 p, MeshMakeData *data, bool ack_block_to_se
                        return;
                }
        }
-       
+
        /*
                Add the block
        */
@@ -166,7 +173,7 @@ void * MeshUpdateThread::Thread()
        log_register_thread("MeshUpdateThread");
 
        DSTACK(__FUNCTION_NAME);
-       
+
        BEGIN_DEBUG_EXCEPTION_HANDLER
 
        porting::setThreadName("MeshUpdateThread");
@@ -221,6 +228,11 @@ Client::Client(
                MtEventManager *event,
                bool ipv6
 ):
+       m_packetcounter_timer(0.0),
+       m_connection_reinit_timer(0.1),
+       m_avg_rtt_timer(0.0),
+       m_playerpos_send_timer(0.0),
+       m_ignore_damage_timer(0.0),
        m_tsrc(tsrc),
        m_shsrc(shsrc),
        m_itemdef(itemdef),
@@ -235,6 +247,7 @@ Client::Client(
                device->getSceneManager(),
                tsrc, this, device
        ),
+       m_particle_manager(&m_env),
        m_con(PROTOCOL_ID, 512, CONNECTION_TIMEOUT, ipv6, this),
        m_device(device),
        m_server_ser_ver(SER_FMT_VER_INVALID),
@@ -242,9 +255,11 @@ Client::Client(
        m_inventory_updated(false),
        m_inventory_from_server(NULL),
        m_inventory_from_server_age(0.0),
+       m_show_highlighted(false),
        m_animation_time(0),
        m_crack_level(-1),
        m_crack_pos(0,0,0),
+       m_highlighted_pos(0,0,0),
        m_map_seed(0),
        m_password(password),
        m_access_denied(false),
@@ -258,29 +273,27 @@ Client::Client(
        m_removed_sounds_check_timer(0),
        m_state(LC_Created)
 {
-       m_packetcounter_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;
-       m_ignore_damage_timer = 0.0;
-
        /*
                Add local player
        */
        {
-               Player *player = new LocalPlayer(this);
-
-               player->updateName(playername);
+               Player *player = new LocalPlayer(this, playername);
 
                m_env.addPlayer(player);
        }
+
+       m_cache_smooth_lighting = g_settings->getBool("smooth_lighting");
 }
 
 void Client::Stop()
 {
        //request all client managed threads to stop
        m_mesh_update_thread.Stop();
+       if (localdb != NULL) {
+               actionstream << "Local map saving ended" << std::endl;
+               localdb->endSave();
+               delete localserver;
+       }
 }
 
 bool Client::isShutdown()
@@ -322,9 +335,14 @@ Client::~Client()
        }
 }
 
-void Client::connect(Address address)
+void Client::connect(Address address,
+               const std::string &address_name,
+               bool is_local_server)
 {
        DSTACK(__FUNCTION_NAME);
+
+       initLocalMapSaving(address, address_name, is_local_server);
+
        m_con.SetTimeoutMs(0);
        m_con.Connect(address);
 }
@@ -332,16 +350,16 @@ void Client::connect(Address address)
 void Client::step(float dtime)
 {
        DSTACK(__FUNCTION_NAME);
-       
+
        // Limit a bit
        if(dtime > 2.0)
                dtime = 2.0;
-       
+
        if(m_ignore_damage_timer > dtime)
                m_ignore_damage_timer -= dtime;
        else
                m_ignore_damage_timer = 0.0;
-       
+
        m_animation_time += dtime;
        if(m_animation_time > 60.0)
                m_animation_time -= 60.0;
@@ -359,8 +377,9 @@ void Client::step(float dtime)
                if(counter <= 0.0)
                {
                        counter = 20.0;
-                       
-                       infostream<<"Client packetcounter (20s):"<<std::endl;
+
+                       infostream << "Client packetcounter (" << m_packetcounter_timer
+                                       << "):"<<std::endl;
                        m_packetcounter.print(infostream);
                        m_packetcounter.clear();
                }
@@ -374,7 +393,7 @@ void Client::step(float dtime)
                        NOTE: This jams the game for a while because deleting sectors
                              clear caches
                */
-               
+
                float &counter = m_delete_unused_sectors_timer;
                counter -= dtime;
                if(counter <= 0.0)
@@ -389,12 +408,12 @@ void Client::step(float dtime)
 
                        float delete_unused_sectors_timeout =
                                g_settings->getFloat("client_delete_unused_sectors_timeout");
-       
+
                        // Delete sector blocks
                        /*u32 num = m_env.getMap().unloadUnusedData
                                        (delete_unused_sectors_timeout,
                                        true, &deleted_blocks);*/
-                       
+
                        // Delete whole sectors
                        m_env.getMap().unloadUnusedData
                                        (delete_unused_sectors_timeout,
@@ -406,14 +425,14 @@ void Client::step(float dtime)
                                                <<" unused sectors"<<std::endl;*/
                                /*infostream<<"Client: Deleted "<<num
                                                <<" unused sectors"<<std::endl;*/
-                               
+
                                /*
                                        Send info to server
                                */
 
                                // Env is locked so con can be locked.
                                //JMutexAutoLock lock(m_con_mutex); //bulk comment-out
-                               
+
                                core::list<v3s16>::Iterator i = deleted_blocks.begin();
                                core::list<v3s16> sendlist;
                                for(;;)
@@ -456,8 +475,13 @@ void Client::step(float dtime)
                }
        }
 #endif
-
-       if(m_state == LC_Created)
+       // UGLY hack to fix 2 second startup delay caused by non existent
+       // server client startup synchronization in local server or singleplayer mode
+       static bool initial_step = true;
+       if (initial_step) {
+               initial_step = false;
+       }
+       else if(m_state == LC_Created)
        {
                float &counter = m_connection_reinit_timer;
                counter -= dtime;
@@ -466,10 +490,9 @@ void Client::step(float dtime)
                        counter = 2.0;
 
                        //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
-                       
+
                        Player *myplayer = m_env.getLocalPlayer();
                        assert(myplayer != NULL);
-       
                        // Send TOSERVER_INIT
                        // [0] u16 TOSERVER_INIT
                        // [2] u8 SER_FMT_VER_HIGHEST_READ
@@ -489,7 +512,7 @@ void Client::step(float dtime)
 
                        memset((char*)&data[23], 0, PASSWORD_SIZE);
                        snprintf((char*)&data[23], PASSWORD_SIZE, "%s", m_password.c_str());
-                       
+
                        writeU16(&data[51], CLIENT_PROTOCOL_VERSION_MIN);
                        writeU16(&data[53], CLIENT_PROTOCOL_VERSION_MAX);
 
@@ -504,7 +527,7 @@ void Client::step(float dtime)
        /*
                Do stuff if connected
        */
-       
+
        /*
                Run Map's timers and unload unused data
        */
@@ -516,11 +539,11 @@ void Client::step(float dtime)
                m_env.getMap().timerUpdate(map_timer_and_unload_dtime,
                                g_settings->getFloat("client_unload_unused_data_timeout"),
                                &deleted_blocks);
-                               
+
                /*if(deleted_blocks.size() > 0)
                        infostream<<"Client: Unloaded "<<deleted_blocks.size()
                                        <<" unused blocks"<<std::endl;*/
-                       
+
                /*
                        Send info to server
                        NOTE: This loop is intentionally iterated the way it is.
@@ -532,7 +555,7 @@ void Client::step(float dtime)
                {
                        if(sendlist.size() == 255 || i == deleted_blocks.end())
                        {
-                               if(sendlist.size() == 0)
+                               if(sendlist.empty())
                                        break;
                                /*
                                        [0] u16 command
@@ -577,7 +600,7 @@ void Client::step(float dtime)
 
                // Step environment
                m_env.step(dtime);
-               
+
                /*
                        Get events
                */
@@ -593,7 +616,7 @@ void Client::step(float dtime)
                                if(m_ignore_damage_timer <= 0)
                                {
                                        u8 damage = event.player_damage.amount;
-                                       
+
                                        if(event.player_damage.send_to_server)
                                                sendDamage(damage);
 
@@ -743,7 +766,7 @@ void Client::step(float dtime)
                        m_sound->updateSoundPosition(client_id, pos);
                }
        }
-       
+
        /*
                Handle removed remotely initiated sounds
        */
@@ -768,7 +791,7 @@ void Client::step(float dtime)
                        }
                }
                // Sync to server
-               if(removed_server_ids.size() != 0)
+               if(!removed_server_ids.empty())
                {
                        std::ostringstream os(std::ios_base::binary);
                        writeU16(os, TOSERVER_REMOVED_SOUNDS);
@@ -790,7 +813,7 @@ bool Client::loadMedia(const std::string &data, const std::string &filename)
 {
        // Silly irrlicht's const-incorrectness
        Buffer<char> data_rw(data.c_str(), data.size());
-       
+
        std::string name;
 
        const char *image_ext[] = {
@@ -918,6 +941,41 @@ void Client::received_media()
                        <<std::endl;
 }
 
+void Client::initLocalMapSaving(const Address &address,
+               const std::string &hostname,
+               bool is_local_server)
+{
+       localdb = NULL;
+
+       if (!g_settings->getBool("enable_local_map_saving") || is_local_server)
+               return;
+
+       const std::string world_path = porting::path_user
+               + DIR_DELIM + "worlds"
+               + DIR_DELIM + "server_"
+               + hostname + "_" + to_string(address.getPort());
+
+       SubgameSpec gamespec;
+
+       if (!getWorldExists(world_path)) {
+               gamespec = findSubgame(g_settings->get("default_game"));
+               if (!gamespec.isValid())
+                       gamespec = findSubgame("minimal");
+       } else {
+               gamespec = findWorldSubgame(world_path);
+       }
+
+       if (!gamespec.isValid()) {
+               errorstream << "Couldn't find subgame for local map saving." << std::endl;
+               return;
+       }
+
+       localserver = new Server(world_path, gamespec, false, false);
+       localdb = new Database_SQLite3(&(ServerMap&)localserver->getMap(), world_path);
+       localdb->beginSave();
+       actionstream << "Local map saving started, map will be saved at '" << world_path << "'" << std::endl;
+}
+
 void Client::ReceiveAll()
 {
        DSTACK(__FUNCTION_NAME);
@@ -928,7 +986,7 @@ void Client::ReceiveAll()
                // process
                if(porting::getTimeMs() > start_ms + 100)
                        break;
-               
+
                try{
                        Receive();
                        g_profiler->graphAdd("client_received_packets", 1);
@@ -973,7 +1031,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
 
        //infostream<<"Client: received command="<<command<<std::endl;
        m_packetcounter.add((u16)command);
-       
+
        /*
                If this check is removed, be sure to change the queue
                system to know the ids
@@ -1004,7 +1062,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                                        <<"unsupported ser_fmt_ver"<<std::endl;
                        return;
                }
-               
+
                m_server_ser_ver = deployed;
 
                // Get player position
@@ -1013,12 +1071,12 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                        playerpos_s16 = readV3S16(&data[2+1]);
                v3f playerpos_f = intToFloat(playerpos_s16, BS) - v3f(0, BS/2, 0);
 
-                       
+
                // Set player position
                Player *player = m_env.getLocalPlayer();
                assert(player != NULL);
                player->setPosition(playerpos_f);
-               
+
                if(datasize >= 2+1+6+8)
                {
                        // Get map seed
@@ -1033,7 +1091,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                        infostream<<"Client: received recommended send interval "
                                        <<m_recommended_send_interval<<std::endl;
                }
-               
+
                // Reply to server
                u32 replysize = 2;
                SharedBuffer<u8> reply(replysize);
@@ -1069,7 +1127,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                                " Skipping incoming command="<<command<<std::endl;
                return;
        }
-       
+
        /*
          Handle runtime commands
        */
@@ -1100,13 +1158,13 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
 
                MapNode n;
                n.deSerialize(&data[8], ser_version);
-               
+
                bool remove_metadata = true;
                u32 index = 8 + MapNode::serializedLength(ser_version);
                if ((datasize >= index+1) && data[index]){
                        remove_metadata = false;
                }
-               
+
                addNode(p, n, remove_metadata);
        }
        else if(command == TOCLIENT_BLOCKDATA)
@@ -1114,23 +1172,23 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                // Ignore too small packet
                if(datasize < 8)
                        return;
-                       
+
                v3s16 p;
                p.X = readS16(&data[2]);
                p.Y = readS16(&data[4]);
                p.Z = readS16(&data[6]);
-               
+
                std::string datastring((char*)&data[8], datasize-8);
                std::istringstream istr(datastring, std::ios_base::binary);
-               
+
                MapSector *sector;
                MapBlock *block;
-               
+
                v2s16 p2d(p.X, p.Z);
                sector = m_env.getMap().emergeSector(p2d);
-               
+
                assert(sector->getPos() == p2d);
-               
+
                block = sector->getBlockNoCreateNoEx(p.Y);
                if(block)
                {
@@ -1151,6 +1209,10 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                        sector->insertBlock(block);
                }
 
+               if (localdb != NULL) {
+                       ((ServerMap&) localserver->getMap()).saveBlock(block, localdb);
+               }
+
                /*
                        Add it to mesh update queue and set it to be acknowledged after update.
                */
@@ -1177,7 +1239,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
        {
                if(datasize < 4)
                        return;
-               
+
                u16 time_of_day  = readU16(&data[2]);
                time_of_day      = time_of_day % 24000;
                float time_speed = 0;
@@ -1207,7 +1269,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                                                <<" time_diff="<<time_diff<<std::endl;
                        }
                }
-               
+
                // Update environment
                m_env.setTimeOfDay(time_of_day);
                m_env.setTimeOfDaySpeed(time_speed);
@@ -1228,18 +1290,18 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                u8 buf[6];
                std::string datastring((char*)&data[2], datasize-2);
                std::istringstream is(datastring, std::ios_base::binary);
-               
+
                // Read stuff
                is.read((char*) buf, 2);
                u16 len = readU16(buf);
-               
+
                std::wstring message;
                for(unsigned int i=0; i<len; i++)
                {
                        is.read((char*)buf, 2);
                        message += (wchar_t)readU16(buf);
                }
-               
+
                m_chat_queue.push_back(message);
        }
        else if(command == TOCLIENT_ACTIVE_OBJECT_REMOVE_ADD)
@@ -1408,10 +1470,10 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
        {
                std::string datastring((char*)&data[2], datasize-2);
                std::istringstream is(datastring, std::ios_base::binary);
-               
+
                bool set_camera_point_target = readU8(is);
                v3f camera_point_target = readV3F1000(is);
-               
+
                ClientEvent event;
                event.type                                = CE_DEATHSCREEN;
                event.deathscreen.set_camera_point_target = set_camera_point_target;
@@ -1426,7 +1488,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                std::istringstream is(datastring, std::ios_base::binary);
 
                int num_files = readU16(is);
-               
+
                infostream<<"Client: Received media announcement: packet size: "
                                <<datasize<<std::endl;
 
@@ -1625,7 +1687,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
        {
                std::string datastring((char*)&data[2], datasize-2);
                std::istringstream is(datastring, std::ios_base::binary);
-               
+
                m_privileges.clear();
                infostream<<"Client: Privileges updated: ";
                u16 num_privileges = readU16(is);
@@ -1650,7 +1712,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                std::istringstream is(datastring, std::ios_base::binary);
 
                std::string name = deSerializeString(is);
-               
+
                infostream<<"Client: Detached inventory update: \""<<name<<"\""<<std::endl;
 
                Inventory *inv = NULL;
@@ -1784,9 +1846,13 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                v2f align        = readV2F1000(is);
                v2f offset       = readV2F1000(is);
                v3f world_pos;
+               v2s32 size;
                try{
                        world_pos    = readV3F1000(is);
                }catch(SerializationError &e) {};
+               try{
+                       size = readV2S32(is);
+               } catch(SerializationError &e) {};
 
                ClientEvent event;
                event.type             = CE_HUDADD;
@@ -1802,6 +1868,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                event.hudadd.align     = new v2f(align);
                event.hudadd.offset    = new v2f(offset);
                event.hudadd.world_pos = new v3f(world_pos);
+               event.hudadd.size      = new v2s32(size);
                m_client_event_queue.push_back(event);
        }
        else if(command == TOCLIENT_HUDRM)
@@ -1822,13 +1889,14 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                v2f v2fdata;
                v3f v3fdata;
                u32 intdata = 0;
-               
+               v2s32 v2s32data;
+
                std::string datastring((char *)&data[2], datasize - 2);
                std::istringstream is(datastring, std::ios_base::binary);
 
                u32 id  = readU32(is);
                u8 stat = (HudElementStat)readU8(is);
-               
+
                if (stat == HUD_STAT_POS || stat == HUD_STAT_SCALE ||
                        stat == HUD_STAT_ALIGN || stat == HUD_STAT_OFFSET)
                        v2fdata = readV2F1000(is);
@@ -1836,9 +1904,11 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                        sdata = deSerializeString(is);
                else if (stat == HUD_STAT_WORLD_POS)
                        v3fdata = readV3F1000(is);
+               else if (stat == HUD_STAT_SIZE )
+                       v2s32data = readV2S32(is);
                else
                        intdata = readU32(is);
-               
+
                ClientEvent event;
                event.type              = CE_HUDCHANGE;
                event.hudchange.id      = id;
@@ -1847,6 +1917,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                event.hudchange.v3fdata = new v3f(v3fdata);
                event.hudchange.sdata   = new std::string(sdata);
                event.hudchange.data    = intdata;
+               event.hudchange.v2s32data = new v2s32(v2s32data);
                m_client_event_queue.push_back(event);
        }
        else if(command == TOCLIENT_HUD_SET_FLAGS)
@@ -1856,7 +1927,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
 
                u32 flags = readU32(is);
                u32 mask  = readU32(is);
-               
+
                player->hud_flags &= ~mask;
                player->hud_flags |= flags;
        }
@@ -2015,7 +2086,7 @@ void Client::sendNodemetaFields(v3s16 p, const std::string &formname,
        // Send as reliable
        Send(0, data, true);
 }
-       
+
 void Client::sendInventoryFields(const std::string &formname,
                const std::map<std::string, std::string> &fields)
 {
@@ -2045,13 +2116,13 @@ void Client::sendInventoryAction(InventoryAction *a)
 {
        std::ostringstream os(std::ios_base::binary);
        u8 buf[12];
-       
+
        // Write command
        writeU16(buf, TOSERVER_INVENTORY_ACTION);
        os.write((char*)buf, 2);
 
        a->serialize(os);
-       
+
        // Make data buffer
        std::string s = os.str();
        SharedBuffer<u8> data((u8*)s.c_str(), s.size());
@@ -2063,17 +2134,19 @@ void Client::sendChatMessage(const std::wstring &message)
 {
        std::ostringstream os(std::ios_base::binary);
        u8 buf[12];
-       
+
        // Write command
        writeU16(buf, TOSERVER_CHAT_MESSAGE);
        os.write((char*)buf, 2);
-       
+
        // Write length
        size_t messagesize = message.size();
-       assert(messagesize <= 0xFFFF);
-       writeU16(buf, (u16) (messagesize & 0xFF));
+       if (messagesize > 0xFFFF) {
+               messagesize = 0xFFFF;
+       }
+       writeU16(buf, (u16) messagesize);
        os.write((char*)buf, 2);
-       
+
        // Write string
        for(unsigned int i=0; i<message.size(); i++)
        {
@@ -2081,7 +2154,7 @@ void Client::sendChatMessage(const std::wstring &message)
                writeU16(buf, w);
                os.write((char*)buf, 2);
        }
-       
+
        // Make data buffer
        std::string s = os.str();
        SharedBuffer<u8> data((u8*)s.c_str(), s.size());
@@ -2180,8 +2253,8 @@ void Client::sendReady()
        writeU8(os,VERSION_PATCH_ORIG);
        writeU8(os,0);
 
-       writeU16(os,strlen(CMAKE_VERSION_GITHASH));
-       os.write(CMAKE_VERSION_GITHASH,strlen(CMAKE_VERSION_GITHASH));
+       writeU16(os,strlen(minetest_version_hash));
+       os.write(minetest_version_hash,strlen(minetest_version_hash));
 
        // Make data buffer
        std::string s = os.str();
@@ -2215,13 +2288,13 @@ void Client::sendPlayerPos()
                //JMutexAutoLock lock(m_con_mutex); //bulk comment-out
                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);
-       
+
        v3f pf         = myplayer->getPosition();
        v3f sf         = myplayer->getSpeed();
        s32 pitch      = myplayer->getPitch() * 100;
@@ -2283,21 +2356,18 @@ void Client::removeNode(v3s16 p)
        catch(InvalidPositionException &e)
        {
        }
-       
-       // add urgent task to update the modified node
-       addUpdateMeshTaskForNode(p, false, true);
 
        for(std::map<v3s16, MapBlock * >::iterator
                        i = modified_blocks.begin();
                        i != modified_blocks.end(); ++i)
        {
-               addUpdateMeshTaskWithEdge(i->first);
+               addUpdateMeshTaskWithEdge(i->first, false, true);
        }
 }
 
 void Client::addNode(v3s16 p, MapNode n, bool remove_metadata)
 {
-       TimeTaker timer1("Client::addNode()");
+       //TimeTaker timer1("Client::addNode()");
 
        std::map<v3s16, MapBlock*> modified_blocks;
 
@@ -2308,15 +2378,15 @@ void Client::addNode(v3s16 p, MapNode n, bool remove_metadata)
        }
        catch(InvalidPositionException &e)
        {}
-       
+
        for(std::map<v3s16, MapBlock * >::iterator
                        i = modified_blocks.begin();
                        i != modified_blocks.end(); ++i)
        {
-               addUpdateMeshTaskWithEdge(i->first);
+               addUpdateMeshTaskWithEdge(i->first, false, true);
        }
 }
-       
+
 void Client::setPlayerControl(PlayerControl &control)
 {
        LocalPlayer *player = m_env.getLocalPlayer();
@@ -2415,7 +2485,7 @@ ClientActiveObject * Client::getSelectedActiveObject(
        std::vector<DistanceSortedActiveObject> objects;
 
        m_env.getActiveObjects(from_pos_f_on_map, max_d, objects);
-       
+
        // Sort them.
        // After this, the closest object is the first in the array.
        std::sort(objects.begin(), objects.end());
@@ -2423,7 +2493,7 @@ ClientActiveObject * Client::getSelectedActiveObject(
        for(unsigned int i=0; i<objects.size(); i++)
        {
                ClientActiveObject *obj = objects[i].obj;
-               
+
                core::aabbox3d<f32> *selection_box = obj->getSelectionBox();
                if(selection_box == NULL)
                        continue;
@@ -2459,6 +2529,15 @@ int Client::getCrackLevel()
        return m_crack_level;
 }
 
+void Client::setHighlighted(v3s16 pos, bool show_highlighted)
+{
+       m_show_highlighted = show_highlighted;
+       v3s16 old_highlighted_pos = m_highlighted_pos;
+       m_highlighted_pos = pos;
+       addUpdateMeshTaskForNode(old_highlighted_pos, false, true);
+       addUpdateMeshTaskForNode(m_highlighted_pos, false, true);
+}
+
 void Client::setCrack(int level, v3s16 pos)
 {
        int old_crack_level = m_crack_level;
@@ -2513,16 +2592,14 @@ void Client::typeChatMessage(const std::wstring &message)
        // Show locally
        if (message[0] == L'/')
        {
-               m_chat_queue.push_back(
-                               (std::wstring)L"issued command: "+message);
+               m_chat_queue.push_back((std::wstring)L"issued command: " + message);
        }
        else
        {
                LocalPlayer *player = m_env.getLocalPlayer();
                assert(player != NULL);
                std::wstring name = narrow_to_wide(player->getName());
-               m_chat_queue.push_back(
-                               (std::wstring)L"<"+name+L"> "+message);
+               m_chat_queue.push_back((std::wstring)L"<" + name + L"> " + message);
        }
 }
 
@@ -2531,22 +2608,23 @@ void Client::addUpdateMeshTask(v3s16 p, bool ack_to_server, bool urgent)
        MapBlock *b = m_env.getMap().getBlockNoCreateNoEx(p);
        if(b == NULL)
                return;
-       
+
        /*
                Create a task to update the mesh of the block
        */
-       
+
        MeshMakeData *data = new MeshMakeData(this);
-       
+
        {
                //TimeTaker timer("data fill");
                // Release: ~0ms
                // Debug: 1-6ms, avg=2ms
                data->fill(b);
                data->setCrack(m_crack_level, m_crack_pos);
-               data->setSmoothLighting(g_settings->getBool("smooth_lighting"));
+               data->setHighlighted(m_highlighted_pos, m_show_highlighted);
+               data->setSmoothLighting(m_cache_smooth_lighting);
        }
-       
+
        // Add task to queue
        m_mesh_update_thread.m_queue_in.addBlock(p, data, ack_to_server, urgent);
 }
@@ -2554,9 +2632,7 @@ void Client::addUpdateMeshTask(v3s16 p, bool ack_to_server, bool urgent)
 void Client::addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server, bool urgent)
 {
        try{
-               v3s16 p = blockpos + v3s16(0,0,0);
-               //MapBlock *b = m_env.getMap().getBlockNoCreate(p);
-               addUpdateMeshTask(p, ack_to_server, urgent);
+               addUpdateMeshTask(blockpos, ack_to_server, urgent);
        }
        catch(InvalidPositionException &e){}
 
@@ -2584,8 +2660,7 @@ void Client::addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server, bool ur
        v3s16 blockpos_relative = blockpos * MAP_BLOCKSIZE;
 
        try{
-               v3s16 p = blockpos + v3s16(0,0,0);
-               addUpdateMeshTask(p, ack_to_server, urgent);
+               addUpdateMeshTask(blockpos, ack_to_server, urgent);
        }
        catch(InvalidPositionException &e){}
 
@@ -2634,52 +2709,60 @@ float Client::mediaReceiveProgress()
                return 1.0; // downloader only exists when not yet done
 }
 
-void draw_load_screen(const std::wstring &text,
-               IrrlichtDevice* device, gui::IGUIFont* font,
-               float dtime=0 ,int percent=0, bool clouds=true);
-
 void Client::afterContentReceived(IrrlichtDevice *device, gui::IGUIFont* font)
 {
        infostream<<"Client::afterContentReceived() started"<<std::endl;
        assert(m_itemdef_received);
        assert(m_nodedef_received);
        assert(mediaReceived());
-       
+
+       wchar_t* text = wgettext("Loading textures...");
+
        // Rebuild inherited images and recreate textures
        infostream<<"- Rebuilding images and textures"<<std::endl;
+       draw_load_screen(text,device, guienv, 0, 70);
        m_tsrc->rebuildImagesAndTextures();
+       delete[] text;
 
        // Rebuild shaders
        infostream<<"- Rebuilding shaders"<<std::endl;
+       text = wgettext("Rebuilding shaders...");
+       draw_load_screen(text, device, guienv, 0, 75);
        m_shsrc->rebuildShaders();
+       delete[] text;
 
        // Update node aliases
        infostream<<"- Updating node aliases"<<std::endl;
+       text = wgettext("Initializing nodes...");
+       draw_load_screen(text, device, guienv, 0, 80);
        m_nodedef->updateAliases(m_itemdef);
+       m_nodedef->setNodeRegistrationStatus(true);
+       m_nodedef->runNodeResolverCallbacks();
+       delete[] text;
 
-       // Update node textures
+       // Update node textures and assign shaders to each tile
        infostream<<"- Updating node textures"<<std::endl;
-       m_nodedef->updateTextures(m_tsrc);
+       m_nodedef->updateTextures(this);
 
        // Preload item textures and meshes if configured to
        if(g_settings->getBool("preload_item_visuals"))
        {
                verbosestream<<"Updating item textures and meshes"<<std::endl;
-               wchar_t* text = wgettext("Item textures...");
-               draw_load_screen(text,device,font,0,0);
+               text = wgettext("Item textures...");
+               draw_load_screen(text, device, guienv, 0, 0);
                std::set<std::string> names = m_itemdef->getAll();
                size_t size = names.size();
                size_t count = 0;
                int percent = 0;
                for(std::set<std::string>::const_iterator
-                               i = names.begin(); i != names.end(); ++i){
+                               i = names.begin(); i != names.end(); ++i)
+               {
                        // Asking for these caches the result
                        m_itemdef->getInventoryTexture(*i, this);
                        m_itemdef->getWieldMesh(*i, this);
                        count++;
-                       percent = count*100/size;
-                       if (count%50 == 0) // only update every 50 item
-                               draw_load_screen(text,device,font,0,percent);
+                       percent = (count * 100 / size * 0.2) + 80;
+                       draw_load_screen(text, device, guienv, 0, percent);
                }
                delete[] text;
        }
@@ -2687,10 +2770,13 @@ void Client::afterContentReceived(IrrlichtDevice *device, gui::IGUIFont* font)
        // Start mesh update thread after setting up content definitions
        infostream<<"- Starting mesh update thread"<<std::endl;
        m_mesh_update_thread.Start();
-       
+
        m_state = LC_Ready;
        sendReady();
+       text = wgettext("Done!");
+       draw_load_screen(text, device, guienv, 0, 100);
        infostream<<"Client::afterContentReceived() done"<<std::endl;
+       delete[] text;
 }
 
 float Client::getRTT(void)
@@ -2698,6 +2784,46 @@ float Client::getRTT(void)
        return m_con.getPeerStat(PEER_ID_SERVER,con::AVG_RTT);
 }
 
+float Client::getCurRate(void)
+{
+       return ( m_con.getLocalStat(con::CUR_INC_RATE) +
+                       m_con.getLocalStat(con::CUR_DL_RATE));
+}
+
+float Client::getAvgRate(void)
+{
+       return ( m_con.getLocalStat(con::AVG_INC_RATE) +
+                       m_con.getLocalStat(con::AVG_DL_RATE));
+}
+
+void Client::makeScreenshot(IrrlichtDevice *device)
+{
+       irr::video::IVideoDriver *driver = device->getVideoDriver();
+       irr::video::IImage* const raw_image = driver->createScreenShot();
+       if (raw_image) {
+               irr::video::IImage* const image = driver->createImage(video::ECF_R8G8B8,
+                       raw_image->getDimension());
+
+               if (image) {
+                       raw_image->copyTo(image);
+                       irr::c8 filename[256];
+                       snprintf(filename, sizeof(filename), "%s" DIR_DELIM "screenshot_%u.png",
+                                g_settings->get("screenshot_path").c_str(),
+                                device->getTimer()->getRealTime());
+                       std::ostringstream sstr;
+                       if (driver->writeImageToFile(image, filename)) {
+                               sstr << "Saved screenshot to '" << filename << "'";
+                       } else {
+                               sstr << "Failed to save screenshot '" << filename << "'";
+                       }
+                       m_chat_queue.push_back(narrow_to_wide(sstr.str()));
+                       infostream << sstr.str() << std::endl;
+                       image->drop();
+               }
+               raw_image->drop();
+       }
+}
+
 // IGameDef interface
 // Under envlock
 IItemDefManager* Client::getItemDefManager()
@@ -2721,6 +2847,10 @@ IShaderSource* Client::getShaderSource()
 {
        return m_shsrc;
 }
+scene::ISceneManager* Client::getSceneManager()
+{
+       return m_device->getSceneManager();
+}
 u16 Client::allocateUnknownNodeId(const std::string &name)
 {
        errorstream<<"Client::allocateUnknownNodeId(): "
@@ -2737,6 +2867,11 @@ MtEventManager* Client::getEventManager()
        return m_event;
 }
 
+ParticleManager* Client::getParticleManager()
+{
+       return &m_particle_manager;
+}
+
 scene::IAnimatedMesh* Client::getMesh(const std::string &filename)
 {
        std::map<std::string, std::string>::const_iterator i =