Skip to content

Commit fbb9f3b

Browse files
author
Mikel
committedJan 26, 2025·
fix: Python lags affecting game physics
1 parent 42e6772 commit fbb9f3b

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed
 

‎src/client/client.cpp

+29-8
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
6969

7070
#include "craftium.h"
7171
#include "gui/mainmenumanager.h"
72+
#include <chrono>
7273

7374
extern gui::IGUIEnvironment* guienv;
7475

@@ -208,13 +209,26 @@ void Client::pyConnStep() {
208209
u32 c; // stores the RGBA pixel color
209210
bool kill;
210211

211-
frameskip_count++;
212+
/* Clear all virtual key presses except the movement ones (WASD) */
213+
for (int i=0; i<KeyType::INTERNAL_ENUM_COUNT; i++) {
214+
if (i != KeyType::FORWARD && i != KeyType::BACKWARD
215+
&& i != KeyType::LEFT && i != KeyType::RIGHT) {
216+
virtual_key_presses[i] = false;
217+
}
218+
}
212219

220+
// Update and check the frameskip condition
221+
frameskip_count++;
213222
if (frameskip_count != frameskip)
214223
return;
215-
216224
frameskip_count = 0;
217225

226+
/* Clear missing virtual key presses (WASD) */
227+
virtual_key_presses[KeyType::FORWARD] = false;
228+
virtual_key_presses[KeyType::BACKWARD] = false;
229+
virtual_key_presses[KeyType::LEFT] = false;
230+
virtual_key_presses[KeyType::RIGHT] = false;
231+
218232
/* Take the screenshot */
219233
irr::video::IVideoDriver *driver = m_rendering_engine->get_video_driver();
220234
irr::video::IImage* const raw_image = driver->createScreenShot();
@@ -641,8 +655,11 @@ void Client::connect(const Address &address, const std::string &address_name,
641655

642656
void Client::step(float dtime)
643657
{
644-
syncClientStep();
658+
syncClientStep();
659+
660+
dtime -= m_craftium_lag;
645661

662+
// printf("client dtime: %f, LIM: %f\n", dtime, DTIME_LIMIT);
646663
// Limit a bit
647664
if (dtime > DTIME_LIMIT)
648665
dtime = DTIME_LIMIT;
@@ -655,10 +672,6 @@ void Client::step(float dtime)
655672

656673
ReceiveAll();
657674

658-
/* Clear virtual key presses */
659-
for (int i=0; i<KeyType::INTERNAL_ENUM_COUNT; i++)
660-
virtual_key_presses[i] = false;
661-
662675
/*
663676
Packet counter
664677
*/
@@ -760,7 +773,15 @@ void Client::step(float dtime)
760773
*/
761774
LocalPlayer *player = m_env.getLocalPlayer();
762775

763-
pyConnStep();
776+
auto begin = std::chrono::steady_clock::now();
777+
pyConnStep();
778+
auto end = std::chrono::steady_clock::now();
779+
std::chrono::duration<float> duration = end - begin;
780+
float seconds = duration.count();
781+
m_craftium_lag = seconds;
782+
783+
// printf("dtime: %f, delta (lag): %lf\n", dtime, seconds);
784+
764785

765786
// Step environment (also handles player controls)
766787
m_env.step(dtime);

‎src/client/client.h

+2
Original file line numberDiff line numberDiff line change
@@ -617,4 +617,6 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
617617

618618
// The number of blocks the client will combine for mesh generation.
619619
MeshGrid m_mesh_grid;
620+
621+
float m_craftium_lag = 0.0;
620622
};

0 commit comments

Comments
 (0)
Please sign in to comment.