diff options
author | Elias Fleckenstein <eliasfleckenstein@web.de> | 2021-05-13 17:21:13 +0200 |
---|---|---|
committer | Elias Fleckenstein <eliasfleckenstein@web.de> | 2021-05-13 17:21:13 +0200 |
commit | 96a37aed31cfb9c131e46eda80bdbe3d2289a546 (patch) | |
tree | b9e8c5628fc81565ec0305f1b36335e5e1605a2b | |
parent | d08242316688ce8ac10dcf94a2cfede21e65be7f (diff) | |
download | dragonfireclient-96a37aed31cfb9c131e46eda80bdbe3d2289a546.tar.xz |
Add minetest.get_send_speed
-rw-r--r-- | doc/client_lua_api.txt | 5 | ||||
-rw-r--r-- | src/client/client.cpp | 16 | ||||
-rw-r--r-- | src/client/localplayer.cpp | 10 | ||||
-rw-r--r-- | src/client/localplayer.h | 2 | ||||
-rw-r--r-- | src/script/cpp_api/s_client.cpp | 21 | ||||
-rw-r--r-- | src/script/cpp_api/s_client.h | 2 |
6 files changed, 48 insertions, 8 deletions
diff --git a/doc/client_lua_api.txt b/doc/client_lua_api.txt index a02a281f5..3c8b1fbb6 100644 --- a/doc/client_lua_api.txt +++ b/doc/client_lua_api.txt @@ -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 diff --git a/src/client/client.cpp b/src/client/client.cpp index e0493e973..e6025ed7b 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -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); } diff --git a/src/client/localplayer.cpp b/src/client/localplayer.cpp index c75404a4b..24a12c35e 100644 --- a/src/client/localplayer.cpp +++ b/src/client/localplayer.cpp @@ -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; diff --git a/src/client/localplayer.h b/src/client/localplayer.h index 842c18015..eaac216d3 100644 --- a/src/client/localplayer.h +++ b/src/client/localplayer.h @@ -143,6 +143,8 @@ public: v3f getLegitSpeed() const { return m_freecam ? m_legit_speed : m_speed; } + v3f getSendSpeed(); + inline void setLegitPosition(const v3f &position) { if (m_freecam) diff --git a/src/script/cpp_api/s_client.cpp b/src/script/cpp_api/s_client.cpp index 5990c4df2..b0e7a073e 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -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 diff --git a/src/script/cpp_api/s_client.h b/src/script/cpp_api/s_client.h index 12d96d81e..2f9ce7aa3 100644 --- a/src/script/cpp_api/s_client.h +++ b/src/script/cpp_api/s_client.h @@ -70,6 +70,8 @@ public: 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); |