]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Add minetest.get_send_speed
authorElias Fleckenstein <eliasfleckenstein@web.de>
Thu, 13 May 2021 15:21:13 +0000 (17:21 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Thu, 13 May 2021 15:21:13 +0000 (17:21 +0200)
doc/client_lua_api.txt
src/client/client.cpp
src/client/localplayer.cpp
src/client/localplayer.h
src/script/cpp_api/s_client.cpp
src/script/cpp_api/s_client.h

index a02a281f51d16dc4b7700db6a351b22c479f0f30..3c8b1fbb6d66664870360bf532f8898b68726e16 100644 (file)
@@ -674,6 +674,11 @@ Minetest namespace reference
 ### Global callback registration functions
 Call these functions only at load time!
 
+* `minetest.get_send_speed(speed)`
+       * This function is called every time the player's speed is sent to server
+       * The `speed` argument is the actual speed of the player
+       * If you define it, you can return a modified `speed`. This speed will be
+               sent to server instead.
 * `minetest.open_enderchest()`
     * This function is called if the client uses the Keybind for it (by default "O")
     * You can override it
index e0493e973808f554678b87ce6c39eff3ce790134..e6025ed7bfd52c8fee2aabcebfa8a2730087ae6b 100644 (file)
@@ -913,9 +913,9 @@ void Client::Send(NetworkPacket* pkt)
 
 // Will fill up 12 + 12 + 4 + 4 + 4 bytes
 void writePlayerPos(LocalPlayer *myplayer, ClientMap *clientMap, NetworkPacket *pkt)
-{      
+{
        v3f pf           = myplayer->getLegitPosition() * 100;
-       v3f sf           = myplayer->getLegitSpeed() * 100;
+       v3f sf           = myplayer->getSendSpeed() * 100;
        s32 pitch        = myplayer->getPitch() * 100;
        s32 yaw          = myplayer->getYaw() * 100;
        u32 keyPressed   = myplayer->keyPressed;
@@ -1286,7 +1286,7 @@ void Client::sendPlayerPos(v3f pos)
 
        if (
                        player->last_position     == pos &&
-                       player->last_speed        == player->getLegitSpeed()    &&
+                       player->last_speed        == player->getSendSpeed()    &&
                        player->last_pitch        == player->getPitch()    &&
                        player->last_yaw          == player->getYaw()      &&
                        player->last_keyPressed   == player->keyPressed    &&
@@ -1295,7 +1295,7 @@ void Client::sendPlayerPos(v3f pos)
                return;
 
        player->last_position     = pos;
-       player->last_speed        = player->getLegitSpeed();
+       player->last_speed        = player->getSendSpeed();
        player->last_pitch        = player->getPitch();
        player->last_yaw          = player->getYaw();
        player->last_keyPressed   = player->keyPressed;
@@ -1645,17 +1645,17 @@ void Client::addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server, bool ur
 void Client::updateAllMapBlocks()
 {
        v3s16 currentBlock = getNodeBlockPos(floatToInt(m_env.getLocalPlayer()->getPosition(), BS));
-       
+
        for (s16 X = currentBlock.X - 2; X <= currentBlock.X + 2; X++)
        for (s16 Y = currentBlock.Y - 2; Y <= currentBlock.Y + 2; Y++)
        for (s16 Z = currentBlock.Z - 2; Z <= currentBlock.Z + 2; Z++)
                addUpdateMeshTask(v3s16(X, Y, Z), false, true);
-       
+
        Map &map = m_env.getMap();
-       
+
        std::vector<v3s16> positions;
        map.listAllLoadedBlocks(positions);
-       
+
        for (v3s16 p : positions) {
                addUpdateMeshTask(p, false, false);
        }
index c75404a4ba8f27577a8579aa1ef93569df227dbe..24a12c35e4802cda4408b8fb3ba8e5cbdd2ff246 100644 (file)
@@ -709,6 +709,16 @@ v3s16 LocalPlayer::getLightPosition() const
        return floatToInt(m_position + v3f(0.0f, BS * 1.5f, 0.0f), BS);
 }
 
+v3f LocalPlayer::getSendSpeed()
+{
+       v3f speed = getLegitSpeed();
+
+       if (m_client->modsLoaded())
+               speed = m_client->getScript()->get_send_speed(speed);
+
+       return speed;
+}
+
 v3f LocalPlayer::getEyeOffset() const
 {
        float eye_height = camera_barely_in_ceiling ? m_eye_height - 0.125f : m_eye_height;
index 842c1801556abf62c82e3de285ba111c6f29eb6a..eaac216d3e7187db7dd4424b949c02cbff89f19d 100644 (file)
@@ -143,6 +143,8 @@ class LocalPlayer : public Player
 
        v3f getLegitSpeed() const { return m_freecam ? m_legit_speed : m_speed; }
 
+       v3f getSendSpeed();
+
        inline void setLegitPosition(const v3f &position)
        {
                if (m_freecam)
index 5990c4df218289a1883fd12f2d1eb4581aa8f85f..b0e7a073ebc4368430039f4b38adeb4ac05bf0c0 100644 (file)
@@ -365,6 +365,27 @@ void ScriptApiClient::open_enderchest()
                lua_pcall(L, 0, 0, error_handler);
 }
 
+v3f ScriptApiClient::get_send_speed(v3f speed)
+{
+       SCRIPTAPI_PRECHECKHEADER
+
+       PUSH_ERROR_HANDLER(L);
+       int error_handler = lua_gettop(L) - 1;
+       lua_insert(L, error_handler);
+
+       lua_getglobal(L, "core");
+       lua_getfield(L, -1, "get_send_speed");
+       if (lua_isfunction(L, -1)) {
+               speed /= BS;
+               push_v3f(L, speed);
+               lua_pcall(L, 1, 1, error_handler);
+               speed = read_v3f(L, -1);
+               speed *= BS;
+       }
+
+       return speed;
+}
+
 void ScriptApiClient::set_node_def(const ContentFeatures &f)
 {
        SCRIPTAPI_PRECHECKHEADER
index 12d96d81ee4edff17516c3df8f3a8eeef7d7cfde..2f9ce7aa355145d978e51c282e60e5cc15f352d2 100644 (file)
@@ -70,6 +70,8 @@ class ScriptApiClient : virtual public ScriptApiBase
        bool on_inventory_open(Inventory *inventory);
        void open_enderchest();
 
+       v3f get_send_speed(v3f speed);
+
        void set_node_def(const ContentFeatures &f);
        void set_item_def(const ItemDefinition &i);