Don't send player position from client to server if the player hasn't moved
authorMirceaKitsune <sonichedgehog_hyperblast00@yahoo.com>
Sun, 2 Dec 2012 12:59:08 +0000 (14:59 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Sun, 2 Dec 2012 18:22:02 +0000 (20:22 +0200)
src/client.cpp
src/localplayer.cpp
src/localplayer.h
src/player.h

index 865cf71ee449f8975e2c6f21b6c8323cc8919743..3463e926269683605fcf24f42fdc754cde274d45 100644 (file)
@@ -1976,10 +1976,24 @@ void Client::sendPlayerPos()
 {
        //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
        
-       Player *myplayer = m_env.getLocalPlayer();
+       LocalPlayer *myplayer = m_env.getLocalPlayer();
        if(myplayer == NULL)
                return;
-       
+
+       // Save bandwidth by only updating position when something changed
+       if(myplayer->last_position == myplayer->getPosition() &&
+                       myplayer->last_speed == myplayer->getSpeed() &&
+                       myplayer->last_pitch == myplayer->getPitch() &&
+                       myplayer->last_yaw == myplayer->getYaw() &&
+                       myplayer->last_keyPressed == myplayer->keyPressed)
+               return;
+
+       myplayer->last_position = myplayer->getPosition();
+       myplayer->last_speed = myplayer->getSpeed();
+       myplayer->last_pitch = myplayer->getPitch();
+       myplayer->last_yaw = myplayer->getYaw();
+       myplayer->last_keyPressed = myplayer->keyPressed;
+
        u16 our_peer_id;
        {
                //JMutexAutoLock lock(m_con_mutex); //bulk comment-out
index 17c4cdeb9904d98fa4d3cfa19c5657976107ced6..14d1ac60ecb605fbe59e3f24e2e9acfa3c9c6527 100644 (file)
@@ -36,6 +36,11 @@ LocalPlayer::LocalPlayer(IGameDef *gamedef):
        Player(gamedef),
        isAttached(false),
        overridePosition(v3f(0,0,0)),
+       last_position(v3f(0,0,0)),
+       last_speed(v3f(0,0,0)),
+       last_pitch(0),
+       last_yaw(0),
+       last_keyPressed(0),
        m_sneak_node(32767,32767,32767),
        m_sneak_node_exists(false),
        m_old_node_below(32767,32767,32767),
index 9d1829db86a12d7f70de44104cfd47103385d93d..c9d010d7c653ff3343e3a3b9993d3eb72e20d544 100644 (file)
@@ -44,6 +44,14 @@ public:
        void applyControl(float dtime);
 
        v3s16 getStandingNodePos();
+
+       // Used to check if anything changed and prevent sending packets if not
+       v3f last_position;
+       v3f last_speed;
+       float last_pitch;
+       float last_yaw;
+       unsigned int last_keyPressed;
+
 private:
        // This is used for determining the sneaking range
        v3s16 m_sneak_node;
index 6c7c1e4ea2db8f7918a2ebbc68d91872eb949816..5a489e64f813cd2b3febf5d688f92ef0b0e23944 100644 (file)
@@ -220,7 +220,7 @@ public:
        }
        
        u32 keyPressed;
-       
+
 protected:
        IGameDef *m_gamedef;