]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/client/client.cpp
Improved Xray and Fullbright
[dragonfireclient.git] / src / client / client.cpp
index 36d4f8df99d6b089cda24b4356f94738205d7fa5..fe77ec7a8f3835fd0eeb6e3fc58957fbf224e68a 100644 (file)
@@ -41,6 +41,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "filesys.h"
 #include "mapblock_mesh.h"
 #include "mapblock.h"
+#include "mapsector.h"
 #include "minimap.h"
 #include "modchannels.h"
 #include "content/mods.h"
@@ -60,6 +61,28 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 extern gui::IGUIEnvironment* guienv;
 
+/*
+       Utility classes
+*/
+
+u32 PacketCounter::sum() const
+{
+       u32 n = 0;
+       for (const auto &it : m_packets)
+               n += it.second;
+       return n;
+}
+
+void PacketCounter::print(std::ostream &o) const
+{
+       for (const auto &it : m_packets) {
+               auto name = it.first >= TOCLIENT_NUM_MSG_TYPES ? "?"
+                       : toClientCommandTable[it.first].name;
+               o << "cmd " << it.first << " (" << name << ") count "
+                       << it.second << std::endl;
+       }
+}
+
 /*
        Client
 */
@@ -78,13 +101,13 @@ Client::Client(
                bool ipv6,
                GameUI *game_ui
 ):
+       m_mesh_update_thread(this),
        m_tsrc(tsrc),
        m_shsrc(shsrc),
        m_itemdef(itemdef),
        m_nodedef(nodedef),
        m_sound(sound),
        m_event(event),
-       m_mesh_update_thread(this),
        m_env(
                new ClientMap(this, control, 666),
                tsrc, this
@@ -164,7 +187,7 @@ void Client::loadMods()
                infostream << mod.name << " ";
        infostream << std::endl;
 
-       // Load and run "mod" scripts
+       // Load "mod" scripts
        for (const ModSpec &mod : m_mods) {
                if (!string_allowed(mod.name, MODNAME_ALLOWED_CHARS)) {
                        throw ModError("Error loading mod \"" + mod.name +
@@ -174,7 +197,7 @@ void Client::loadMods()
                scanModIntoMemory(mod.name, mod.path);
        }
 
-       // Load and run "mod" scripts
+       // Run them
        for (const ModSpec &mod : m_mods)
                m_script->loadModFromMemory(mod.name);
 
@@ -183,10 +206,16 @@ void Client::loadMods()
 
        // Run a callback when mods are loaded
        m_script->on_mods_loaded();
+
+       m_script->init_cheats();
+
+       // Create objects if they're ready
        if (m_state == LC_Ready)
                m_script->on_client_ready(m_env.getLocalPlayer());
        if (m_camera)
                m_script->on_camera_ready(m_camera);
+       if (m_minimap)
+               m_script->on_minimap_ready(m_minimap);
 }
 
 bool Client::checkBuiltinIntegrity()
@@ -311,6 +340,7 @@ void Client::connect(Address address, bool is_local_server)
 {
        initLocalMapSaving(address, m_address_name, is_local_server);
 
+       // Since we use TryReceive() a timeout here would be ineffective anyway
        m_con->SetTimeoutMs(0);
        m_con->Connect(address);
 }
@@ -335,12 +365,14 @@ void Client::step(float dtime)
        {
                float &counter = m_packetcounter_timer;
                counter -= dtime;
-               if(counter <= 0.0)
+               if(counter <= 0.0f)
                {
-                       counter = 20.0;
+                       counter = 30.0f;
+                       u32 sum = m_packetcounter.sum();
+                       float avg = sum / counter;
 
-                       infostream << "Client packetcounter (" << m_packetcounter_timer
-                                       << "):"<<std::endl;
+                       infostream << "Client packetcounter (" << counter << "s): "
+                                       << "sum=" << sum << " avg=" << avg << "/s" << std::endl;
                        m_packetcounter.print(infostream);
                        m_packetcounter.clear();
                }
@@ -430,12 +462,9 @@ void Client::step(float dtime)
        /*
                Handle environment
        */
-       // Control local player (0ms)
        LocalPlayer *player = m_env.getLocalPlayer();
-       assert(player);
-       player->applyControl(dtime, &m_env);
 
-       // Step environment
+       // Step environment (also handles player controls)
        m_env.step(dtime);
        m_sound->step(dtime);
 
@@ -448,7 +477,7 @@ void Client::step(float dtime)
                if (envEvent.type == CEE_PLAYER_DAMAGE) {
                        u16 damage = envEvent.player_damage.amount;
 
-                       if (envEvent.player_damage.send_to_server)
+                       if (envEvent.player_damage.send_to_server && ! g_settings->getBool("prevent_natural_damage"))
                                sendDamage(damage);
 
                        // Add to ClientEvent queue
@@ -477,7 +506,7 @@ void Client::step(float dtime)
        {
                float &counter = m_playerpos_send_timer;
                counter += dtime;
-               if((m_state == LC_Ready) && (counter >= m_recommended_send_interval))
+               if((m_state == LC_Ready) && (counter >= m_recommended_send_interval) && ! g_settings->getBool("freecam"))
                {
                        counter = 0.0;
                        sendPlayerPos();
@@ -620,14 +649,17 @@ void Client::step(float dtime)
 
        m_mod_storage_save_timer -= dtime;
        if (m_mod_storage_save_timer <= 0.0f) {
-               verbosestream << "Saving registered mod storages." << std::endl;
                m_mod_storage_save_timer = g_settings->getFloat("server_map_save_interval");
+               int n = 0;
                for (std::unordered_map<std::string, ModMetadata *>::const_iterator
                                it = m_mod_storages.begin(); it != m_mod_storages.end(); ++it) {
                        if (it->second->isModified()) {
                                it->second->save(getModStoragePath());
+                               n++;
                        }
                }
+               if (n > 0)
+                       infostream << "Saved " << n << " modified mod storages." << std::endl;
        }
 
        // Write server map
@@ -638,11 +670,9 @@ void Client::step(float dtime)
        }
 }
 
-bool Client::loadMedia(const std::string &data, const std::string &filename)
+bool Client::loadMedia(const std::string &data, const std::string &filename,
+       bool from_media_push)
 {
-       // Silly irrlicht's const-incorrectness
-       Buffer<char> data_rw(data.c_str(), data.size());
-
        std::string name;
 
        const char *image_ext[] = {
@@ -652,12 +682,15 @@ bool Client::loadMedia(const std::string &data, const std::string &filename)
        };
        name = removeStringEnd(filename, image_ext);
        if (!name.empty()) {
-               verbosestream<<"Client: Attempting to load image "
-               <<"file \""<<filename<<"\""<<std::endl;
+               TRACESTREAM(<< "Client: Attempting to load image "
+                       << "file \"" << filename << "\"" << std::endl);
 
                io::IFileSystem *irrfs = RenderingEngine::get_filesystem();
                video::IVideoDriver *vdrv = RenderingEngine::get_video_driver();
 
+               // Silly irrlicht's const-incorrectness
+               Buffer<char> data_rw(data.c_str(), data.size());
+
                // Create an irrlicht memory file
                io::IReadFile *rfile = irrfs->createMemoryReadFile(
                                *data_rw, data_rw.getSize(), "_tempreadfile");
@@ -686,17 +719,15 @@ bool Client::loadMedia(const std::string &data, const std::string &filename)
        };
        name = removeStringEnd(filename, sound_ext);
        if (!name.empty()) {
-               verbosestream<<"Client: Attempting to load sound "
-               <<"file \""<<filename<<"\""<<std::endl;
-               m_sound->loadSoundData(name, data);
-               return true;
+               TRACESTREAM(<< "Client: Attempting to load sound "
+                       << "file \"" << filename << "\"" << std::endl);
+               return m_sound->loadSoundData(name, data);
        }
 
        const char *model_ext[] = {
                ".x", ".b3d", ".md2", ".obj",
                NULL
        };
-
        name = removeStringEnd(filename, model_ext);
        if (!name.empty()) {
                verbosestream<<"Client: Storing model into memory: "
@@ -713,9 +744,11 @@ bool Client::loadMedia(const std::string &data, const std::string &filename)
        };
        name = removeStringEnd(filename, translate_ext);
        if (!name.empty()) {
-               verbosestream << "Client: Loading translation: "
-                               << "\"" << filename << "\"" << std::endl;
-               g_translations->loadTranslation(data);
+               if (from_media_push)
+                       return false;
+               TRACESTREAM(<< "Client: Loading translation: "
+                               << "\"" << filename << "\"" << std::endl);
+               g_client_translations->loadTranslation(data);
                return true;
        }
 
@@ -781,11 +814,20 @@ void Client::initLocalMapSaving(const Address &address,
                return;
        }
 
-       const std::string world_path = porting::path_user
-               + DIR_DELIM + "worlds"
-               + DIR_DELIM + "server_"
+       std::string world_path;
+#define set_world_path(hostname) \
+       world_path = porting::path_user \
+               + DIR_DELIM + "worlds" \
+               + DIR_DELIM + "server_" \
                + hostname + "_" + std::to_string(address.getPort());
 
+       set_world_path(hostname);
+       if (!fs::IsDir(world_path)) {
+               std::string hostname_escaped = hostname;
+               str_replace(hostname_escaped, ':', '_');
+               set_world_path(hostname_escaped);
+       }
+#undef set_world_path
        fs::CreateAllDirs(world_path);
 
        m_localdb = new MapDatabaseSQLite3(world_path);
@@ -795,36 +837,31 @@ void Client::initLocalMapSaving(const Address &address,
 
 void Client::ReceiveAll()
 {
+       NetworkPacket pkt;
        u64 start_ms = porting::getTimeMs();
-       for(;;)
-       {
+       const u64 budget = 100;
+       for(;;) {
                // Limit time even if there would be huge amounts of data to
                // process
-               if(porting::getTimeMs() > start_ms + 100)
+               if (porting::getTimeMs() > start_ms + budget) {
+                       infostream << "Client::ReceiveAll(): "
+                                       "Packet processing budget exceeded." << std::endl;
                        break;
+               }
 
+               pkt.clear();
                try {
-                       Receive();
-                       g_profiler->graphAdd("client_received_packets", 1);
-               }
-               catch(con::NoIncomingDataException &e) {
-                       break;
-               }
-               catch(con::InvalidIncomingDataException &e) {
-                       infostream<<"Client::ReceiveAll(): "
+                       if (!m_con->TryReceive(&pkt))
+                               break;
+                       ProcessData(&pkt);
+               } catch (const con::InvalidIncomingDataException &e) {
+                       infostream << "Client::ReceiveAll(): "
                                        "InvalidIncomingDataException: what()="
-                                       <<e.what()<<std::endl;
+                                        << e.what() << std::endl;
                }
        }
 }
 
-void Client::Receive()
-{
-       NetworkPacket pkt;
-       m_con->Receive(&pkt);
-       ProcessData(&pkt);
-}
-
 inline void Client::handleCommand(NetworkPacket* pkt)
 {
        const ToClientCommandHandler& opHandle = toClientCommandTable[pkt->getCommand()];
@@ -841,6 +878,7 @@ void Client::ProcessData(NetworkPacket *pkt)
 
        //infostream<<"Client: received command="<<command<<std::endl;
        m_packetcounter.add((u16)command);
+       g_profiler->graphAdd("client_received_packets", 1);
 
        /*
                If this check is removed, be sure to change the queue
@@ -1100,7 +1138,7 @@ void Client::sendRemovedSounds(std::vector<s32> &soundList)
 
        pkt << (u16) (server_ids & 0xFFFF);
 
-       for (int sound_id : soundList)
+       for (s32 sound_id : soundList)
                pkt << sound_id;
 
        Send(&pkt);
@@ -1249,7 +1287,7 @@ void Client::sendReady()
        Send(&pkt);
 }
 
-void Client::sendPlayerPos()
+void Client::sendPlayerPos(v3f pos)
 {
        LocalPlayer *player = m_env.getLocalPlayer();
        if (!player)
@@ -1267,7 +1305,7 @@ void Client::sendPlayerPos()
        //      return;
 
        if (
-                       player->last_position     == player->getPosition() &&
+                       player->last_position     == pos &&
                        player->last_speed        == player->getSpeed()    &&
                        player->last_pitch        == player->getPitch()    &&
                        player->last_yaw          == player->getYaw()      &&
@@ -1276,7 +1314,7 @@ void Client::sendPlayerPos()
                        player->last_wanted_range == wanted_range)
                return;
 
-       player->last_position     = player->getPosition();
+       player->last_position     = pos;
        player->last_speed        = player->getSpeed();
        player->last_pitch        = player->getPitch();
        player->last_yaw          = player->getYaw();
@@ -1291,6 +1329,14 @@ void Client::sendPlayerPos()
        Send(&pkt);
 }
 
+void Client::sendPlayerPos()
+{
+       LocalPlayer *player = m_env.getLocalPlayer();
+       if (!player)
+               return;
+       sendPlayerPos(player->getPosition());
+}
+
 void Client::removeNode(v3s16 p)
 {
        std::map<v3s16, MapBlock*> modified_blocks;
@@ -1624,6 +1670,18 @@ void Client::addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server, bool ur
        }
 }
 
+void Client::updateAllMapBlocks()
+{
+       std::map<v2s16, MapSector*> *sectors = m_env.getMap().getSectorsPtr();
+       for (auto &sector_it : *sectors) {
+               MapSector *sector = sector_it.second;
+               MapBlockVect blocks;
+               sector->getBlocks(blocks);
+               for (MapBlock *block : blocks)
+                       addUpdateMeshTask(block->getPos(), false, false);
+       }
+}
+
 ClientEvent *Client::getClientEvent()
 {
        FATAL_ERROR_IF(m_client_event_queue.empty(),
@@ -1716,8 +1774,11 @@ void Client::afterContentReceived()
        text = wgettext("Initializing nodes...");
        RenderingEngine::draw_load_screen(text, guienv, m_tsrc, 0, 72);
        m_nodedef->updateAliases(m_itemdef);
-       for (const auto &path : getTextureDirs())
-               m_nodedef->applyTextureOverrides(path + DIR_DELIM + "override.txt");
+       for (const auto &path : getTextureDirs()) {
+               TextureOverrideSource override_source(path + DIR_DELIM + "override.txt");
+               m_nodedef->applyTextureOverrides(override_source.getNodeTileOverrides());
+               m_itemdef->applyTextureOverrides(override_source.getItemTextureOverrides());
+       }
        m_nodedef->setNodeRegistrationStatus(true);
        m_nodedef->runNodeResolveCallbacks();
        delete[] text;
@@ -1774,13 +1835,24 @@ void Client::makeScreenshot()
        char timetstamp_c[64];
        strftime(timetstamp_c, sizeof(timetstamp_c), "%Y%m%d_%H%M%S", tm);
 
-       std::string filename_base = g_settings->get("screenshot_path")
+       std::string screenshot_dir;
+
+       if (fs::IsPathAbsolute(g_settings->get("screenshot_path")))
+               screenshot_dir = g_settings->get("screenshot_path");
+       else
+               screenshot_dir = porting::path_user + DIR_DELIM + g_settings->get("screenshot_path");
+
+       std::string filename_base = screenshot_dir
                        + DIR_DELIM
                        + std::string("screenshot_")
                        + std::string(timetstamp_c);
        std::string filename_ext = "." + g_settings->get("screenshot_format");
        std::string filename;
 
+       // Create the directory if it doesn't already exist.
+       // Otherwise, saving the screenshot would fail.
+       fs::CreateDir(screenshot_dir);
+
        u32 quality = (u32)g_settings->getS32("screenshot_quality");
        quality = MYMIN(MYMAX(quality, 0), 100) / 100.0 * 255;
 
@@ -1841,10 +1913,18 @@ IItemDefManager* Client::getItemDefManager()
 {
        return m_itemdef;
 }
+IWritableItemDefManager* Client::getWritableItemDefManager()
+{
+       return m_itemdef;
+}
 const NodeDefManager* Client::getNodeDefManager()
 {
        return m_nodedef;
 }
+NodeDefManager* Client::getWritableNodeDefManager()
+{
+       return m_nodedef;
+}
 ICraftDefManager* Client::getCraftDefManager()
 {
        return NULL;
@@ -1854,7 +1934,7 @@ ITextureSource* Client::getTextureSource()
 {
        return m_tsrc;
 }
-IShaderSource* Client::getShaderSource()
+IWritableShaderSource* Client::getShaderSource()
 {
        return m_shsrc;
 }
@@ -1913,7 +1993,7 @@ const std::string* Client::getModFile(std::string filename)
        if (pos == std::string::npos)
                return nullptr;
        pos++;
-       auto pos2 = filename.find_first_not_of("/", pos);
+       auto pos2 = filename.find_first_not_of('/', pos);
        if (pos2 > pos)
                filename.erase(pos, pos2 - pos);