Skip to content

Commit

Permalink
fix: Python lags affecting game physics
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikel committed Jan 26, 2025
1 parent 42e6772 commit fbb9f3b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
37 changes: 29 additions & 8 deletions src/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#include "craftium.h"
#include "gui/mainmenumanager.h"
#include <chrono>

extern gui::IGUIEnvironment* guienv;

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

frameskip_count++;
/* Clear all virtual key presses except the movement ones (WASD) */
for (int i=0; i<KeyType::INTERNAL_ENUM_COUNT; i++) {
if (i != KeyType::FORWARD && i != KeyType::BACKWARD
&& i != KeyType::LEFT && i != KeyType::RIGHT) {
virtual_key_presses[i] = false;
}
}

// Update and check the frameskip condition
frameskip_count++;
if (frameskip_count != frameskip)
return;

frameskip_count = 0;

/* Clear missing virtual key presses (WASD) */
virtual_key_presses[KeyType::FORWARD] = false;
virtual_key_presses[KeyType::BACKWARD] = false;
virtual_key_presses[KeyType::LEFT] = false;
virtual_key_presses[KeyType::RIGHT] = false;

/* Take the screenshot */
irr::video::IVideoDriver *driver = m_rendering_engine->get_video_driver();
irr::video::IImage* const raw_image = driver->createScreenShot();
Expand Down Expand Up @@ -641,8 +655,11 @@ void Client::connect(const Address &address, const std::string &address_name,

void Client::step(float dtime)
{
syncClientStep();
syncClientStep();

dtime -= m_craftium_lag;

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

ReceiveAll();

/* Clear virtual key presses */
for (int i=0; i<KeyType::INTERNAL_ENUM_COUNT; i++)
virtual_key_presses[i] = false;

/*
Packet counter
*/
Expand Down Expand Up @@ -760,7 +773,15 @@ void Client::step(float dtime)
*/
LocalPlayer *player = m_env.getLocalPlayer();

pyConnStep();
auto begin = std::chrono::steady_clock::now();
pyConnStep();
auto end = std::chrono::steady_clock::now();
std::chrono::duration<float> duration = end - begin;
float seconds = duration.count();
m_craftium_lag = seconds;

// printf("dtime: %f, delta (lag): %lf\n", dtime, seconds);


// Step environment (also handles player controls)
m_env.step(dtime);
Expand Down
2 changes: 2 additions & 0 deletions src/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -617,4 +617,6 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef

// The number of blocks the client will combine for mesh generation.
MeshGrid m_mesh_grid;

float m_craftium_lag = 0.0;
};

0 comments on commit fbb9f3b

Please sign in to comment.