Simplify player modification checks
authorShadowNinja <shadowninja@minetest.net>
Sun, 3 Aug 2014 20:19:07 +0000 (16:19 -0400)
committerShadowNinja <shadowninja@minetest.net>
Wed, 1 Oct 2014 22:44:36 +0000 (18:44 -0400)
src/client.cpp
src/environment.cpp
src/localplayer.cpp
src/localplayer.h
src/player.cpp
src/player.h
src/server.cpp

index 5a276e3060b747d0be9a46cb1391e5f04e636a1c..4a00283eeaa1b44816a2c1fcaf584c8161e9523a 100644 (file)
@@ -270,9 +270,7 @@ Client::Client(
                Add local player
        */
        {
-               Player *player = new LocalPlayer(this);
-
-               player->updateName(playername);
+               Player *player = new LocalPlayer(this, playername);
 
                m_env.addPlayer(player);
        }
index 66898f012f3573fe032201b8fcf53ba8d08bb29b..4106ecdf5e05cb5a7b2fef7f5a6a79e035882c52 100644 (file)
@@ -449,11 +449,11 @@ Player *ServerEnvironment::loadPlayer(const std::string &playername)
        bool newplayer = false;
        bool found = false;
        if (!player) {
-               player = new RemotePlayer(m_gamedef);
+               player = new RemotePlayer(m_gamedef, playername.c_str());
                newplayer = true;
        }
 
-       RemotePlayer testplayer(m_gamedef);
+       RemotePlayer testplayer(m_gamedef, "");
        std::string path = players_path + playername;
        for (u32 i = 0; i < PLAYER_FILE_ALTERNATE_TRIES; i++) {
                // Open file and deserialize
index 1a238cb4787789c0f45d25cecf5abd015765517d..84b7c11466b2ca2d3287a28907dc767945d1d21f 100644 (file)
@@ -33,8 +33,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
        LocalPlayer
 */
 
-LocalPlayer::LocalPlayer(IGameDef *gamedef):
-       Player(gamedef),
+LocalPlayer::LocalPlayer(IGameDef *gamedef, const char *name):
+       Player(gamedef, name),
        parent(0),
        isAttached(false),
        overridePosition(v3f(0,0,0)),
index bfe476b70360068e64e74c1d26066ce963819048..16830f3ec87343aeb17abfe46b7f793354d65f81 100644 (file)
@@ -32,14 +32,14 @@ enum LocalPlayerAnimations {NO_ANIM, WALK_ANIM, DIG_ANIM, WD_ANIM};  // no local
 class LocalPlayer : public Player
 {
 public:
-       LocalPlayer(IGameDef *gamedef);
+       LocalPlayer(IGameDef *gamedef, const char *name);
        virtual ~LocalPlayer();
 
        bool isLocal() const
        {
                return true;
        }
-       
+
        ClientActiveObject *parent;
 
        bool isAttached;
index 8e5f56199b1978ba803a536d6ecce0317ac621b8..a8f95bb99bb58e5095585efab884bac22c6dad9c 100644 (file)
@@ -28,8 +28,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "content_sao.h"
 #include "filesys.h"
 #include "log.h"
+#include "porting.h"  // strlcpy
 
-Player::Player(IGameDef *gamedef):
+
+Player::Player(IGameDef *gamedef, const char *name):
        touching_ground(false),
        in_liquid(false),
        in_liquid_stable(false),
@@ -52,20 +54,16 @@ Player::Player(IGameDef *gamedef):
        m_speed(0,0,0),
        m_position(0,0,0),
        m_collisionbox(-BS*0.30,0.0,-BS*0.30,BS*0.30,BS*1.75,BS*0.30),
-       m_last_pitch(0),
-       m_last_yaw(0),
-       m_last_pos(0,0,0),
-       m_last_hp(PLAYER_MAX_HP),
-       m_last_inventory(gamedef->idef())
+       m_dirty(false)
 {
-       updateName("<not set>");
+       strlcpy(m_name, name, PLAYERNAME_SIZE);
+
        inventory.clear();
        inventory.addList("main", PLAYER_INVENTORY_SIZE);
        InventoryList *craft = inventory.addList("craft", 9);
        craft->setWidth(3);
        inventory.addList("craftpreview", 1);
        inventory.addList("craftresult", 1);
-       m_last_inventory = inventory;
 
        // Can be redefined via Lua
        inventory_formspec = "size[8,7.5]"
@@ -207,7 +205,7 @@ void Player::deSerialize(std::istream &is, std::string playername)
 
        //args.getS32("version"); // Version field value not used
        std::string name = args.get("name");
-       updateName(name.c_str());
+       strlcpy(m_name, name.c_str(), PLAYERNAME_SIZE);
        setPitch(args.getFloat("pitch"));
        setYaw(args.getFloat("yaw"));
        setPosition(args.getV3F("position"));
@@ -238,8 +236,7 @@ void Player::deSerialize(std::istream &is, std::string playername)
                }
        }
 
-       // Set m_last_*
-       checkModified();
+       m_dirty = false;
 }
 
 u32 Player::addHud(HudElement *toadd)
@@ -290,7 +287,7 @@ void RemotePlayer::save(std::string savedir)
         */
 
        // A player to deserialize files into to check their names
-       RemotePlayer testplayer(m_gamedef);
+       RemotePlayer testplayer(m_gamedef, "");
 
        savedir += DIR_DELIM;
        std::string path = savedir + m_name;
@@ -302,6 +299,7 @@ void RemotePlayer::save(std::string savedir)
                        if (!fs::safeWriteToFile(path, ss.str())) {
                                infostream << "Failed to write " << path << std::endl;
                        }
+                       m_dirty = false;
                        return;
                }
                // Open file and deserialize
@@ -319,6 +317,7 @@ void RemotePlayer::save(std::string savedir)
                        if (!fs::safeWriteToFile(path, ss.str())) {
                                infostream << "Failed to write " << path << std::endl;
                        }
+                       m_dirty = false;
                        return;
                }
                path = savedir + m_name + itos(i);
index 93197d374a5ecff3573e2c3d2d115024fcdae503..174f147289a91f20420fddef557a55f5d9c05a7c 100644 (file)
@@ -95,7 +95,7 @@ class Player
 {
 public:
 
-       Player(IGameDef *gamedef);
+       Player(IGameDef *gamedef, const char *name);
        virtual ~Player() = 0;
 
        virtual void move(f32 dtime, Environment *env, f32 pos_max_d)
@@ -142,16 +142,19 @@ public:
 
        virtual void setPosition(const v3f &position)
        {
+               m_dirty = true;
                m_position = position;
        }
 
        void setPitch(f32 pitch)
        {
+               m_dirty = true;
                m_pitch = pitch;
        }
 
        virtual void setYaw(f32 yaw)
        {
+               m_dirty = true;
                m_yaw = yaw;
        }
 
@@ -172,6 +175,7 @@ public:
 
        virtual void setBreath(u16 breath)
        {
+               m_dirty = true;
                m_breath = breath;
        }
 
@@ -185,11 +189,6 @@ public:
                return (m_yaw + 90.) * core::DEGTORAD;
        }
 
-       void updateName(const char *name)
-       {
-               snprintf(m_name, PLAYERNAME_SIZE, "%s", name);
-       }
-
        const char * getName() const
        {
                return m_name;
@@ -225,19 +224,7 @@ public:
 
        bool checkModified()
        {
-               if(m_last_hp != hp || m_last_pitch != m_pitch ||
-                               m_last_pos != m_position || m_last_yaw != m_yaw ||
-                               !(inventory == m_last_inventory))
-               {
-                       m_last_hp = hp;
-                       m_last_pitch = m_pitch;
-                       m_last_pos = m_position;
-                       m_last_yaw = m_yaw;
-                       m_last_inventory = inventory;
-                       return true;
-               } else {
-                       return false;
-               }
+               return m_dirty;
        }
 
        bool touching_ground;
@@ -316,11 +303,7 @@ protected:
        v3f m_position;
        core::aabbox3d<f32> m_collisionbox;
 
-       f32 m_last_pitch;
-       f32 m_last_yaw;
-       v3f m_last_pos;
-       u16 m_last_hp;
-       Inventory m_last_inventory;
+       bool m_dirty;
 
        std::vector<HudElement *> hud;
 };
@@ -332,7 +315,10 @@ protected:
 class RemotePlayer : public Player
 {
 public:
-       RemotePlayer(IGameDef *gamedef): Player(gamedef), m_sao(0) {}
+       RemotePlayer(IGameDef *gamedef, const char *name):
+               Player(gamedef, name),
+               m_sao(NULL)
+       {}
        virtual ~RemotePlayer() {}
 
        void save(std::string savedir);
index 262970c09a195bdd96615121d2b0e8e40683097d..4f4763ea47f056bdc53bf93980788e824a23f34e 100644 (file)
@@ -5038,8 +5038,7 @@ PlayerSAO* Server::emergePlayer(const char *name, u16 peer_id)
        // Create player if it doesn't exist
        if (!player) {
                newplayer = true;
-               player = new RemotePlayer(this);
-               player->updateName(name);
+               player = new RemotePlayer(this, name);
                /* Set player position */
                infostream<<"Server: Finding spawn place for player \""
                                <<name<<"\""<<std::endl;