added player link support sapier-20120115
authorsapier <sapier at gmx dot net>
Sun, 15 Jan 2012 21:41:10 +0000 (22:41 +0100)
committersapier <sapier at gmx dot net>
Sun, 15 Jan 2012 21:41:10 +0000 (22:41 +0100)
src/content_cao_player.cpp
src/environment.cpp
src/player.cpp
src/player.h
src/serverremoteplayer.cpp
src/serverremoteplayer.h

index d5b15c2614e1aa657c37192de3c1930b62b37479..a920cc1399667c588932e6d3f5d617e4598e715a 100644 (file)
@@ -18,12 +18,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 */
 
 #include "content_cao.h"
+#include "clientlinkableobject.h"
 
 /*
        PlayerCAO
 */
 
-class PlayerCAO : public ClientActiveObject
+class PlayerCAO : public ClientActiveObject, public ClientLinkableObject
 {
 private:
        core::aabbox3d<f32> m_selection_box;
@@ -212,8 +213,10 @@ public:
 
        void step(float dtime, ClientEnvironment *env)
                {
-                       pos_translator.translate(dtime);
-                       updateNodePos();
+                       if(!isLinked()) {
+                               pos_translator.translate(dtime);
+                               updateNodePos();
+                       }
 
                        if(m_damage_visual_timer > 0){
                                m_damage_visual_timer -= dtime;
@@ -252,6 +255,11 @@ public:
                                m_damage_visual_timer = 0.5;
                                updateTextures("^[brighten");
                        }
+                       else if (handleLinkUnlinkMessages(cmd,&is,this->m_env))
+                       {
+                               //Link unlink already done in handleLinkUnlinkMessages!
+                       }
+
                }
 
        void updateTextures(const std::string &mod)
@@ -277,6 +285,27 @@ public:
                                }
                        }
                }
+       void setPosition(v3f toset, float dtime){
+               if (isLinked()) {
+                       this->m_position = toset + this->m_linkOffset;
+                       pos_translator.update(this->m_position,false);
+                       updateNodePos();
+
+                       if(m_is_local_player) {
+                               m_local_player->setPosition(this->m_position);
+                       }
+               }
+               else {
+                       errorstream<<"Got linked position update but not linked"<< std::endl;
+               }
+       }
+
+       void updateLinkState(bool value) {
+               if(m_is_local_player) {
+                       m_local_player->Link(value);
+               }
+       }
+
 
        inline v3f getPosition()        {       return m_position;      }
 
index a3d0950f09641cd138d48e910199b3f5d98e38f4..9a99a11108358273beafc3fe3cb3c1e5aee4a4da 100644 (file)
@@ -1890,6 +1890,7 @@ void ClientEnvironment::step(float dtime)
                Get the speed the player is going
        */
        bool is_climbing = lplayer->is_climbing;
+       bool linked      = lplayer->m_linked;
        
        f32 player_speed = lplayer->getSpeed().getLength();
        
@@ -1918,96 +1919,98 @@ void ClientEnvironment::step(float dtime)
        /*
                Stuff that has a maximum time increment
        */
+       //TODO at this position possibly a better solution is required!
+       if (!linked) {
+               u32 loopcount = 0;
+               do
+               {
+                       loopcount++;
 
-       u32 loopcount = 0;
-       do
-       {
-               loopcount++;
+                       f32 dtime_part;
+                       if(dtime_downcount > dtime_max_increment)
+                       {
+                               dtime_part = dtime_max_increment;
+                               dtime_downcount -= dtime_part;
+                       }
+                       else
+                       {
+                               dtime_part = dtime_downcount;
+                               /*
+                                       Setting this to 0 (no -=dtime_part) disables an infinite loop
+                                       when dtime_part is so small that dtime_downcount -= dtime_part
+                                       does nothing
+                               */
+                               dtime_downcount = 0;
+                       }
 
-               f32 dtime_part;
-               if(dtime_downcount > dtime_max_increment)
-               {
-                       dtime_part = dtime_max_increment;
-                       dtime_downcount -= dtime_part;
-               }
-               else
-               {
-                       dtime_part = dtime_downcount;
                        /*
-                               Setting this to 0 (no -=dtime_part) disables an infinite loop
-                               when dtime_part is so small that dtime_downcount -= dtime_part
-                               does nothing
+                               Handle local player
                        */
-                       dtime_downcount = 0;
-               }
-               
-               /*
-                       Handle local player
-               */
-               
-               {
-                       v3f lplayerpos = lplayer->getPosition();
                        
-                       // Apply physics
-                       if(free_move == false && is_climbing == false)
                        {
-                               // Gravity
-                               v3f speed = lplayer->getSpeed();
-                               if(lplayer->swimming_up == false)
-                                       speed.Y -= 9.81 * BS * dtime_part * 2;
+                               v3f lplayerpos = lplayer->getPosition();
 
-                               // Water resistance
-                               if(lplayer->in_water_stable || lplayer->in_water)
+                               // Apply physics
+                               if(free_move == false && is_climbing == false)
                                {
-                                       f32 max_down = 2.0*BS;
-                                       if(speed.Y < -max_down) speed.Y = -max_down;
+                                       // Gravity
+                                       v3f speed = lplayer->getSpeed();
+                                       if(lplayer->swimming_up == false)
+                                               speed.Y -= 9.81 * BS * dtime_part * 2;
 
-                                       f32 max = 2.5*BS;
-                                       if(speed.getLength() > max)
+                                       // Water resistance
+                                       if(lplayer->in_water_stable || lplayer->in_water)
                                        {
-                                               speed = speed / speed.getLength() * max;
+                                               f32 max_down = 2.0*BS;
+                                               if(speed.Y < -max_down) speed.Y = -max_down;
+
+                                               f32 max = 2.5*BS;
+                                               if(speed.getLength() > max)
+                                               {
+                                                       speed = speed / speed.getLength() * max;
+                                               }
                                        }
+
+                                       lplayer->setSpeed(speed);
                                }
 
-                               lplayer->setSpeed(speed);
+                               /*
+                                       Move the lplayer.
+                                       This also does collision detection.
+                               */
+                               lplayer->move(dtime_part, *m_map, position_max_increment,
+                                               &player_collisions);
                        }
-
-                       /*
-                               Move the lplayer.
-                               This also does collision detection.
-                       */
-                       lplayer->move(dtime_part, *m_map, position_max_increment,
-                                       &player_collisions);
                }
-       }
-       while(dtime_downcount > 0.001);
+               while(dtime_downcount > 0.001);
+
+               //std::cout<<"Looped "<<loopcount<<" times."<<std::endl;
                
-       //std::cout<<"Looped "<<loopcount<<" times."<<std::endl;
-       
-       for(core::list<CollisionInfo>::Iterator
-                       i = player_collisions.begin();
-                       i != player_collisions.end(); i++)
-       {
-               CollisionInfo &info = *i;
-               if(info.t == COLLISION_FALL)
+               for(core::list<CollisionInfo>::Iterator
+                               i = player_collisions.begin();
+                               i != player_collisions.end(); i++)
                {
-                       //f32 tolerance = BS*10; // 2 without damage
-                       f32 tolerance = BS*12; // 3 without damage
-                       f32 factor = 1;
-                       if(info.speed > tolerance)
+                       CollisionInfo &info = *i;
+                       if(info.t == COLLISION_FALL)
                        {
-                               f32 damage_f = (info.speed - tolerance)/BS*factor;
-                               u16 damage = (u16)(damage_f+0.5);
-                               if(lplayer->hp > damage)
-                                       lplayer->hp -= damage;
-                               else
-                                       lplayer->hp = 0;
-
-                               ClientEnvEvent event;
-                               event.type = CEE_PLAYER_DAMAGE;
-                               event.player_damage.amount = damage;
-                               event.player_damage.send_to_server = true;
-                               m_client_event_queue.push_back(event);
+                               //f32 tolerance = BS*10; // 2 without damage
+                               f32 tolerance = BS*12; // 3 without damage
+                               f32 factor = 1;
+                               if(info.speed > tolerance)
+                               {
+                                       f32 damage_f = (info.speed - tolerance)/BS*factor;
+                                       u16 damage = (u16)(damage_f+0.5);
+                                       if(lplayer->hp > damage)
+                                               lplayer->hp -= damage;
+                                       else
+                                               lplayer->hp = 0;
+
+                                       ClientEnvEvent event;
+                                       event.type = CEE_PLAYER_DAMAGE;
+                                       event.player_damage.amount = damage;
+                                       event.player_damage.send_to_server = true;
+                                       m_client_event_queue.push_back(event);
+                               }
                        }
                }
        }
index 963f67c282542265e63ea816a22d3986816225ea..56b7fb4d4e4c3760a3626634b560e12aca806a3e 100644 (file)
@@ -183,7 +183,8 @@ void Player::deSerialize(std::istream &is)
 LocalPlayer::LocalPlayer(IGameDef *gamedef):
        Player(gamedef),
        m_sneak_node(32767,32767,32767),
-       m_sneak_node_exists(false)
+       m_sneak_node_exists(false),
+       m_linked(false)
 {
        // Initialize hp to 0, so that no hearts will be shown if server
        // doesn't support health points
index 1c9dde7e0aab7c369f58164a74f71577de08f048..5091cf6c845734c3d6693347056200bbe047f02f 100644 (file)
@@ -251,6 +251,13 @@ public:
        
        PlayerControl control;
 
+
+       inline void Link(bool value) {
+               m_linked = value;
+       }
+
+       bool m_linked;
+
 private:
        // This is used for determining the sneaking range
        v3s16 m_sneak_node;
index 1681900e009d66f2cc3e8846c19f2d2965e96c84..d34131849846cb9f685d49f50e909a472639c8a6 100644 (file)
@@ -101,13 +101,17 @@ void ServerRemotePlayer::step(float dtime, bool send_recommended)
        if(send_recommended == false)
                return;
        
+       if(isLinked())
+               return;
+
+
        if(m_position_not_sent)
        {
                m_position_not_sent = false;
 
                std::ostringstream os(std::ios::binary);
                // command (0 = update position)
-               writeU8(os, 0);
+               writeU8(os,AO_Message_type::SetPosition );
                // pos
                writeV3F1000(os, getPosition());
                // yaw
@@ -173,7 +177,7 @@ void ServerRemotePlayer::punch(ServerActiveObject *puncher,
        {
                std::ostringstream os(std::ios::binary);
                // command (1 = punched)
-               writeU8(os, 1);
+               writeU8(os, AO_Message_type::Punched);
                // damage
                writeS16(os, hitprop.hp);
                // create message and add to list
@@ -323,4 +327,24 @@ s16 ServerRemotePlayer::getHP()
        return hp;
 }
 
+bool ServerRemotePlayer::sendLinkMsg(ServerActiveObject* parent,v3f offset) {
+       std::ostringstream os(std::ios::binary);
+       writeU8(os, AO_Message_type::Link);
+       // parameters
+       writeU16(os, parent->getId());
+       writeV3F1000(os, offset);
+       // create message and add to list
+       ActiveObjectMessage aom(getId(), true, os.str());
+       m_messages_out.push_back(aom);
+       return true;
+}
+
+bool ServerRemotePlayer::sendUnlinkMsg() {
+       std::ostringstream os(std::ios::binary);
+       writeU8(os, AO_Message_type::UnLink);
+       // create message and add to list
+       ActiveObjectMessage aom(getId(), true, os.str());
+       m_messages_out.push_back(aom);
+       return true;
+}
 
index bdc3bba208f5553edddb1112c6661962b3a899fe..e942c28eaa318877cecdb80ed5806d4a532f0aaa 100644 (file)
@@ -22,13 +22,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #include "player.h"
 #include "serverobject.h"
+#include "serverlinkableobject.h"
 #include "content_object.h" // Object type IDs
 
 /*
        Player on the server
 */
 
-class ServerRemotePlayer : public Player, public ServerActiveObject
+class ServerRemotePlayer : public Player, public ServerActiveObject, public ServerLinkableObject
 {
 public:
        ServerRemotePlayer(ServerEnvironment *env);
@@ -97,6 +98,9 @@ public:
        // Incremented by step(), read and reset by Server
        float m_time_from_last_punch;
 
+       bool sendLinkMsg(ServerActiveObject* parent,v3f offset);
+       bool sendUnlinkMsg();
+
 private:
        bool m_position_not_sent;
 };