Optimize updateFastFaceRow processing by removing some TileSpec copy (#5678)
[oweals/minetest.git] / src / game.cpp
index 5b49d34c0e42a22cd00ae57101113914c10af770..a1cc1ab15e82abe18d02527c84b3270e38c2e49f 100644 (file)
@@ -60,7 +60,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "version.h"
 #include "minimap.h"
 #include "mapblock_mesh.h"
-#include "script/clientscripting.h"
+#include "script/scripting_client.h"
 
 #include "sound.h"
 
@@ -79,14 +79,15 @@ extern Profiler *g_profiler;
        Text input system
 */
 
-struct TextDestNodeMetadata : public TextDest {
+struct TextDestNodeMetadata : public TextDest
+{
        TextDestNodeMetadata(v3s16 p, Client *client)
        {
                m_p = p;
                m_client = client;
        }
        // This is deprecated I guess? -celeron55
-       void gotText(std::wstring text)
+       void gotText(const std::wstring &text)
        {
                std::string ntext = wide_to_utf8(text);
                infostream << "Submitting 'text' field of node at (" << m_p.X << ","
@@ -104,13 +105,14 @@ struct TextDestNodeMetadata : public TextDest {
        Client *m_client;
 };
 
-struct TextDestPlayerInventory : public TextDest {
+struct TextDestPlayerInventory : public TextDest
+{
        TextDestPlayerInventory(Client *client)
        {
                m_client = client;
                m_formname = "";
        }
-       TextDestPlayerInventory(Client *client, std::string formname)
+       TextDestPlayerInventory(Client *client, const std::string &formname)
        {
                m_client = client;
                m_formname = formname;
@@ -125,23 +127,18 @@ struct TextDestPlayerInventory : public TextDest {
 
 struct LocalFormspecHandler : public TextDest
 {
-       LocalFormspecHandler(std::string formname):
-               m_client(0)
+       LocalFormspecHandler(const std::string &formname):
+               m_client(NULL)
        {
                m_formname = formname;
        }
 
-       LocalFormspecHandler(std::string formname, Client *client):
+       LocalFormspecHandler(const std::string &formname, Client *client):
                m_client(client)
        {
                m_formname = formname;
        }
 
-       void gotText(std::wstring message)
-       {
-               errorstream << "LocalFormspecHandler::gotText old style message received" << std::endl;
-       }
-
        void gotText(const StringMap &fields)
        {
                if (m_formname == "MT_PAUSE_MENU") {
@@ -205,7 +202,8 @@ public:
 
                return meta->getString("formspec");
        }
-       std::string resolveText(std::string str)
+
+       virtual std::string resolveText(const std::string &str)
        {
                NodeMetadata *meta = m_map->getNodeMetadata(m_p);
 
@@ -569,27 +567,35 @@ public:
 class GameOnDemandSoundFetcher: public OnDemandSoundFetcher
 {
        std::set<std::string> m_fetched;
+private:
+       void paths_insert(std::set<std::string> &dst_paths,
+               const std::string &base,
+               const std::string &name)
+       {
+               dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".ogg");
+               dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".0.ogg");
+               dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".1.ogg");
+               dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".2.ogg");
+               dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".3.ogg");
+               dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".4.ogg");
+               dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".5.ogg");
+               dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".6.ogg");
+               dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".7.ogg");
+               dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".8.ogg");
+               dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".9.ogg");
+       }
 public:
        void fetchSounds(const std::string &name,
-                       std::set<std::string> &dst_paths,
-                       std::set<std::string> &dst_datas)
+               std::set<std::string> &dst_paths,
+               std::set<std::string> &dst_datas)
        {
                if (m_fetched.count(name))
                        return;
 
                m_fetched.insert(name);
-               std::string base = porting::path_share + DIR_DELIM + "sounds";
-               dst_paths.insert(base + DIR_DELIM + name + ".ogg");
-               dst_paths.insert(base + DIR_DELIM + name + ".0.ogg");
-               dst_paths.insert(base + DIR_DELIM + name + ".1.ogg");
-               dst_paths.insert(base + DIR_DELIM + name + ".2.ogg");
-               dst_paths.insert(base + DIR_DELIM + name + ".3.ogg");
-               dst_paths.insert(base + DIR_DELIM + name + ".4.ogg");
-               dst_paths.insert(base + DIR_DELIM + name + ".5.ogg");
-               dst_paths.insert(base + DIR_DELIM + name + ".6.ogg");
-               dst_paths.insert(base + DIR_DELIM + name + ".7.ogg");
-               dst_paths.insert(base + DIR_DELIM + name + ".8.ogg");
-               dst_paths.insert(base + DIR_DELIM + name + ".9.ogg");
+
+               paths_insert(dst_paths, porting::path_share, name);
+               paths_insert(dst_paths, porting::path_user,  name);
        }
 };
 
@@ -1034,6 +1040,7 @@ void KeyCache::populate()
        key[KeyType::INVENTORY]    = getKeySetting("keymap_inventory");
        key[KeyType::CHAT]         = getKeySetting("keymap_chat");
        key[KeyType::CMD]          = getKeySetting("keymap_cmd");
+       key[KeyType::CMD_LOCAL]    = getKeySetting("keymap_cmd_local");
        key[KeyType::CONSOLE]      = getKeySetting("keymap_console");
        key[KeyType::MINIMAP]      = getKeySetting("keymap_minimap");
        key[KeyType::FREEMOVE]     = getKeySetting("keymap_freemove");
@@ -1110,6 +1117,7 @@ struct GameRunData {
        PointedThing pointed_old;
        bool digging;
        bool ldown_for_dig;
+       bool dig_instantly;
        bool left_punch;
        bool update_wielded_item_trigger;
        bool reset_jump_timer;
@@ -1189,7 +1197,7 @@ protected:
                        u16 port,
                        const SubgameSpec &gamespec);
        bool initSound();
-       bool createSingleplayerServer(const std::string map_dir,
+       bool createSingleplayerServer(const std::string &map_dir,
                        const SubgameSpec &gamespec, u16 port, std::string *address);
 
        // Client creation
@@ -1724,9 +1732,10 @@ bool Game::init(
                u16 port,
                const SubgameSpec &gamespec)
 {
+       texture_src = createTextureSource(device);
+
        showOverlayMessage(wgettext("Loading..."), 0, 0);
 
-       texture_src = createTextureSource(device);
        shader_src = createShaderSource(device);
 
        itemdef_manager = createItemDefManager();
@@ -1778,7 +1787,7 @@ bool Game::initSound()
        return true;
 }
 
-bool Game::createSingleplayerServer(const std::string map_dir,
+bool Game::createSingleplayerServer(const std::string &map_dir,
                const SubgameSpec &gamespec, u16 port, std::string *address)
 {
        showOverlayMessage(wgettext("Creating server..."), 0, 5);
@@ -2182,17 +2191,21 @@ bool Game::getServerContent(bool *aborted)
                if (!client->itemdefReceived()) {
                        const wchar_t *text = wgettext("Item definitions...");
                        progress = 25;
-                       draw_load_screen(text, device, guienv, dtime, progress);
+                       draw_load_screen(text, device, guienv, texture_src,
+                               dtime, progress);
                        delete[] text;
                } else if (!client->nodedefReceived()) {
                        const wchar_t *text = wgettext("Node definitions...");
                        progress = 30;
-                       draw_load_screen(text, device, guienv, dtime, progress);
+                       draw_load_screen(text, device, guienv, texture_src,
+                               dtime, progress);
                        delete[] text;
                } else {
                        std::stringstream message;
-                       message.precision(3);
-                       message << gettext("Media...");
+                       std::fixed(message);
+                       message.precision(0);
+                       message << gettext("Media...") << " " << (client->mediaReceiveProgress()*100) << "%";
+                       message.precision(2);
 
                        if ((USE_CURL == 0) ||
                                        (!g_settings->getBool("enable_remote_media_server"))) {
@@ -2209,7 +2222,7 @@ bool Game::getServerContent(bool *aborted)
 
                        progress = 30 + client->mediaReceiveProgress() * 35 + 0.5;
                        draw_load_screen(utf8_to_wide(message.str()), device,
-                                       guienv, dtime, progress);
+                                       guienv, texture_src, dtime, progress);
                }
        }
 
@@ -2449,6 +2462,8 @@ void Game::processKeyInput()
                openConsole(0.2, L"");
        } else if (wasKeyDown(KeyType::CMD)) {
                openConsole(0.2, L"/");
+       } else if (wasKeyDown(KeyType::CMD_LOCAL)) {
+               openConsole(0.2, L".");
        } else if (wasKeyDown(KeyType::CONSOLE)) {
                openConsole(core::clamp(g_settings->getFloat("console_height"), 0.1f, 1.0f));
        } else if (wasKeyDown(KeyType::FREEMOVE)) {
@@ -3040,11 +3055,10 @@ inline void Game::step(f32 *dtime)
 
 void Game::processClientEvents(CameraOrientation *cam)
 {
-       ClientEvent event = client->getClientEvent();
-
        LocalPlayer *player = client->getEnv().getLocalPlayer();
 
-       for ( ; event.type != CE_NONE; event = client->getClientEvent()) {
+       while (client->hasClientEvents()) {
+               ClientEvent event = client->getClientEvent();
 
                switch (event.type) {
                case CE_PLAYER_DAMAGE:
@@ -3397,13 +3411,11 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug)
                        playeritem = mlist->getItem(client->getPlayerItem());
        }
 
-       if (playeritem.getDefinition(itemdef_manager).name.empty()) { // override the hand
-               InventoryList *hlist = local_inventory->getList("hand");
-               if (hlist)
-                       playeritem = hlist->getItem(0);
-       }
        const ItemDefinition &playeritem_def =
                        playeritem.getDefinition(itemdef_manager);
+       InventoryList *hlist = local_inventory->getList("hand");
+       const ItemDefinition &hand_def =
+               hlist ? hlist->getItem(0).getDefinition(itemdef_manager) : itemdef_manager->get("");
 
        v3f player_position  = player->getPosition();
        v3f camera_position  = camera->getPosition();
@@ -3416,7 +3428,7 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug)
        */
 
        f32 d = playeritem_def.range; // max. distance
-       f32 d_hand = itemdef_manager->get("").range;
+       f32 d_hand = hand_def.range;
 
        if (d < 0 && d_hand >= 0)
                d = d_hand;
@@ -3483,6 +3495,10 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug)
                        client->setCrack(-1, v3s16(0, 0, 0));
                        runData.dig_time = 0.0;
                }
+       } else if (runData.dig_instantly && getLeftReleased()) {
+               // Remove e.g. torches faster when clicking instead of holding LMB
+               runData.nodig_delay_timer = 0;
+               runData.dig_instantly = false;
        }
 
        if (!runData.digging && runData.ldown_for_dig && !isLeftPressed()) {
@@ -3504,6 +3520,9 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug)
        } else if (pointed.type == POINTEDTHING_NODE) {
                ToolCapabilities playeritem_toolcap =
                                playeritem.getToolCapabilities(itemdef_manager);
+               if (playeritem.name.empty()) {
+                       playeritem_toolcap = *hand_def.tool_capabilities;
+               }
                handlePointingAtNode(pointed, playeritem_def, playeritem_toolcap, dtime);
        } else if (pointed.type == POINTEDTHING_OBJECT) {
                handlePointingAtObject(pointed, playeritem, player_position, show_debug);
@@ -3710,6 +3729,9 @@ void Game::handlePointingAtNode(const PointedThing &pointed, const ItemDefinitio
                                // Read the sound
                                soundmaker->m_player_rightpunch_sound =
                                                playeritem_def.sound_place;
+
+                               if (client->moddingEnabled())
+                                       client->getScript()->on_placenode(pointed, playeritem_def);
                        } else {
                                soundmaker->m_player_rightpunch_sound =
                                                SimpleSoundSpec();
@@ -3763,9 +3785,16 @@ void Game::handlePointingAtObject(const PointedThing &pointed, const ItemStack &
                        // Report direct punch
                        v3f objpos = runData.selected_object->getPosition();
                        v3f dir = (objpos - player_position).normalize();
+                       ItemStack item = playeritem;
+                       if (playeritem.name.empty()) {
+                               InventoryList *hlist = local_inventory->getList("hand");
+                               if (hlist) {
+                                       item = hlist->getItem(0);
+                               }
+                       }
 
                        bool disable_send = runData.selected_object->directReportPunch(
-                                       dir, &playeritem, runData.time_from_last_punch);
+                                       dir, &item, runData.time_from_last_punch);
                        runData.time_from_last_punch = 0;
 
                        if (!disable_send)
@@ -3785,15 +3814,6 @@ void Game::handleDigging(const PointedThing &pointed, const v3s16 &nodepos,
        ClientMap &map = client->getEnv().getClientMap();
        MapNode n = client->getEnv().getClientMap().getNodeNoEx(nodepos);
 
-       if (!runData.digging) {
-               infostream << "Started digging" << std::endl;
-               if (client->moddingEnabled() && client->getScript()->on_punchnode(nodepos, n))
-                       return;
-               client->interact(0, pointed);
-               runData.digging = true;
-               runData.ldown_for_dig = true;
-       }
-
        // NOTE: Similar piece of code exists on the server side for
        // cheat detection.
        // Get digging parameters
@@ -3802,13 +3822,25 @@ void Game::handleDigging(const PointedThing &pointed, const v3s16 &nodepos,
 
        // If can't dig, try hand
        if (!params.diggable) {
-               const ItemDefinition &hand = itemdef_manager->get("");
+               InventoryList *hlist = local_inventory->getList("hand");
+               const ItemDefinition &hand =
+                       hlist ? hlist->getItem(0).getDefinition(itemdef_manager) : itemdef_manager->get("");
                const ToolCapabilities *tp = hand.tool_capabilities;
 
                if (tp)
                        params = getDigParams(nodedef_manager->get(n).groups, tp);
        }
 
+       if (!runData.digging) {
+               infostream << "Started digging" << std::endl;
+               runData.dig_instantly = params.time == 0;
+               if (client->moddingEnabled() && client->getScript()->on_punchnode(nodepos, n))
+                       return;
+               client->interact(0, pointed);
+               runData.digging = true;
+               runData.ldown_for_dig = true;
+       }
+
        if (!params.diggable) {
                // I guess nobody will wait for this long
                runData.dig_time_complete = 10000000.0;
@@ -3823,12 +3855,12 @@ void Game::handleDigging(const PointedThing &pointed, const v3s16 &nodepos,
                }
        }
 
-       if (runData.dig_time_complete >= 0.001) {
+       if (!runData.dig_instantly) {
                runData.dig_index = (float)crack_animation_length
                                * runData.dig_time
                                / runData.dig_time_complete;
        } else {
-               // This is for torches
+               // This is for e.g. torches
                runData.dig_index = crack_animation_length;
        }
 
@@ -3863,17 +3895,12 @@ void Game::handleDigging(const PointedThing &pointed, const v3s16 &nodepos,
                runData.nodig_delay_timer =
                                runData.dig_time_complete / (float)crack_animation_length;
 
-               // We don't want a corresponding delay to
-               // very time consuming nodes
+               // We don't want a corresponding delay to very time consuming nodes
+               // and nodes without digging time (e.g. torches) get a fixed delay.
                if (runData.nodig_delay_timer > 0.3)
                        runData.nodig_delay_timer = 0.3;
-
-               // We want a slight delay to very little
-               // time consuming nodes
-               const float mindelay = 0.15;
-
-               if (runData.nodig_delay_timer < mindelay)
-                       runData.nodig_delay_timer = mindelay;
+               else if (runData.dig_instantly)
+                       runData.nodig_delay_timer = 0.15;
 
                bool is_valid_position;
                MapNode wasnode = map.getNodeNoEx(nodepos, &is_valid_position);
@@ -4094,7 +4121,7 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
                Drawing begins
        */
 
-       video::SColor skycolor = sky->getSkyColor();
+       const video::SColor &skycolor = sky->getSkyColor();
 
        TimeTaker tt_draw("mainloop: draw");
        driver->beginScene(true, true, skycolor);
@@ -4342,7 +4369,8 @@ inline void Game::limitFps(FpsControl *fps_timings, f32 *dtime)
 void Game::showOverlayMessage(const wchar_t *msg, float dtime,
                int percent, bool draw_clouds)
 {
-       draw_load_screen(msg, device, guienv, dtime, percent, draw_clouds);
+       draw_load_screen(msg, device, guienv, texture_src, dtime, percent,
+               draw_clouds);
        delete[] msg;
 }