Simplify player modification checks

This commit is contained in:
ShadowNinja 2014-08-03 16:19:07 -04:00
parent b37bff72f1
commit cd0df0d5e7
7 changed files with 30 additions and 48 deletions

View file

@ -270,9 +270,7 @@ Client::Client(
Add local player Add local player
*/ */
{ {
Player *player = new LocalPlayer(this); Player *player = new LocalPlayer(this, playername);
player->updateName(playername);
m_env.addPlayer(player); m_env.addPlayer(player);
} }

View file

@ -449,11 +449,11 @@ Player *ServerEnvironment::loadPlayer(const std::string &playername)
bool newplayer = false; bool newplayer = false;
bool found = false; bool found = false;
if (!player) { if (!player) {
player = new RemotePlayer(m_gamedef); player = new RemotePlayer(m_gamedef, playername.c_str());
newplayer = true; newplayer = true;
} }
RemotePlayer testplayer(m_gamedef); RemotePlayer testplayer(m_gamedef, "");
std::string path = players_path + playername; std::string path = players_path + playername;
for (u32 i = 0; i < PLAYER_FILE_ALTERNATE_TRIES; i++) { for (u32 i = 0; i < PLAYER_FILE_ALTERNATE_TRIES; i++) {
// Open file and deserialize // Open file and deserialize

View file

@ -33,8 +33,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
LocalPlayer LocalPlayer
*/ */
LocalPlayer::LocalPlayer(IGameDef *gamedef): LocalPlayer::LocalPlayer(IGameDef *gamedef, const char *name):
Player(gamedef), Player(gamedef, name),
parent(0), parent(0),
isAttached(false), isAttached(false),
overridePosition(v3f(0,0,0)), overridePosition(v3f(0,0,0)),

View file

@ -32,7 +32,7 @@ enum LocalPlayerAnimations {NO_ANIM, WALK_ANIM, DIG_ANIM, WD_ANIM}; // no local
class LocalPlayer : public Player class LocalPlayer : public Player
{ {
public: public:
LocalPlayer(IGameDef *gamedef); LocalPlayer(IGameDef *gamedef, const char *name);
virtual ~LocalPlayer(); virtual ~LocalPlayer();
bool isLocal() const bool isLocal() const

View file

@ -28,8 +28,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "content_sao.h" #include "content_sao.h"
#include "filesys.h" #include "filesys.h"
#include "log.h" #include "log.h"
#include "porting.h" // strlcpy
Player::Player(IGameDef *gamedef):
Player::Player(IGameDef *gamedef, const char *name):
touching_ground(false), touching_ground(false),
in_liquid(false), in_liquid(false),
in_liquid_stable(false), in_liquid_stable(false),
@ -52,20 +54,16 @@ Player::Player(IGameDef *gamedef):
m_speed(0,0,0), m_speed(0,0,0),
m_position(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_collisionbox(-BS*0.30,0.0,-BS*0.30,BS*0.30,BS*1.75,BS*0.30),
m_last_pitch(0), m_dirty(false)
m_last_yaw(0),
m_last_pos(0,0,0),
m_last_hp(PLAYER_MAX_HP),
m_last_inventory(gamedef->idef())
{ {
updateName("<not set>"); strlcpy(m_name, name, PLAYERNAME_SIZE);
inventory.clear(); inventory.clear();
inventory.addList("main", PLAYER_INVENTORY_SIZE); inventory.addList("main", PLAYER_INVENTORY_SIZE);
InventoryList *craft = inventory.addList("craft", 9); InventoryList *craft = inventory.addList("craft", 9);
craft->setWidth(3); craft->setWidth(3);
inventory.addList("craftpreview", 1); inventory.addList("craftpreview", 1);
inventory.addList("craftresult", 1); inventory.addList("craftresult", 1);
m_last_inventory = inventory;
// Can be redefined via Lua // Can be redefined via Lua
inventory_formspec = "size[8,7.5]" 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 //args.getS32("version"); // Version field value not used
std::string name = args.get("name"); std::string name = args.get("name");
updateName(name.c_str()); strlcpy(m_name, name.c_str(), PLAYERNAME_SIZE);
setPitch(args.getFloat("pitch")); setPitch(args.getFloat("pitch"));
setYaw(args.getFloat("yaw")); setYaw(args.getFloat("yaw"));
setPosition(args.getV3F("position")); setPosition(args.getV3F("position"));
@ -238,8 +236,7 @@ void Player::deSerialize(std::istream &is, std::string playername)
} }
} }
// Set m_last_* m_dirty = false;
checkModified();
} }
u32 Player::addHud(HudElement *toadd) 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 // A player to deserialize files into to check their names
RemotePlayer testplayer(m_gamedef); RemotePlayer testplayer(m_gamedef, "");
savedir += DIR_DELIM; savedir += DIR_DELIM;
std::string path = savedir + m_name; std::string path = savedir + m_name;
@ -302,6 +299,7 @@ void RemotePlayer::save(std::string savedir)
if (!fs::safeWriteToFile(path, ss.str())) { if (!fs::safeWriteToFile(path, ss.str())) {
infostream << "Failed to write " << path << std::endl; infostream << "Failed to write " << path << std::endl;
} }
m_dirty = false;
return; return;
} }
// Open file and deserialize // Open file and deserialize
@ -319,6 +317,7 @@ void RemotePlayer::save(std::string savedir)
if (!fs::safeWriteToFile(path, ss.str())) { if (!fs::safeWriteToFile(path, ss.str())) {
infostream << "Failed to write " << path << std::endl; infostream << "Failed to write " << path << std::endl;
} }
m_dirty = false;
return; return;
} }
path = savedir + m_name + itos(i); path = savedir + m_name + itos(i);

View file

@ -95,7 +95,7 @@ class Player
{ {
public: public:
Player(IGameDef *gamedef); Player(IGameDef *gamedef, const char *name);
virtual ~Player() = 0; virtual ~Player() = 0;
virtual void move(f32 dtime, Environment *env, f32 pos_max_d) virtual void move(f32 dtime, Environment *env, f32 pos_max_d)
@ -142,16 +142,19 @@ class Player
virtual void setPosition(const v3f &position) virtual void setPosition(const v3f &position)
{ {
m_dirty = true;
m_position = position; m_position = position;
} }
void setPitch(f32 pitch) void setPitch(f32 pitch)
{ {
m_dirty = true;
m_pitch = pitch; m_pitch = pitch;
} }
virtual void setYaw(f32 yaw) virtual void setYaw(f32 yaw)
{ {
m_dirty = true;
m_yaw = yaw; m_yaw = yaw;
} }
@ -172,6 +175,7 @@ class Player
virtual void setBreath(u16 breath) virtual void setBreath(u16 breath)
{ {
m_dirty = true;
m_breath = breath; m_breath = breath;
} }
@ -185,11 +189,6 @@ class Player
return (m_yaw + 90.) * core::DEGTORAD; return (m_yaw + 90.) * core::DEGTORAD;
} }
void updateName(const char *name)
{
snprintf(m_name, PLAYERNAME_SIZE, "%s", name);
}
const char * getName() const const char * getName() const
{ {
return m_name; return m_name;
@ -225,19 +224,7 @@ class Player
bool checkModified() bool checkModified()
{ {
if(m_last_hp != hp || m_last_pitch != m_pitch || return m_dirty;
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;
}
} }
bool touching_ground; bool touching_ground;
@ -316,11 +303,7 @@ class Player
v3f m_position; v3f m_position;
core::aabbox3d<f32> m_collisionbox; core::aabbox3d<f32> m_collisionbox;
f32 m_last_pitch; bool m_dirty;
f32 m_last_yaw;
v3f m_last_pos;
u16 m_last_hp;
Inventory m_last_inventory;
std::vector<HudElement *> hud; std::vector<HudElement *> hud;
}; };
@ -332,7 +315,10 @@ class Player
class RemotePlayer : public Player class RemotePlayer : public Player
{ {
public: public:
RemotePlayer(IGameDef *gamedef): Player(gamedef), m_sao(0) {} RemotePlayer(IGameDef *gamedef, const char *name):
Player(gamedef, name),
m_sao(NULL)
{}
virtual ~RemotePlayer() {} virtual ~RemotePlayer() {}
void save(std::string savedir); void save(std::string savedir);

View file

@ -5038,8 +5038,7 @@ PlayerSAO* Server::emergePlayer(const char *name, u16 peer_id)
// Create player if it doesn't exist // Create player if it doesn't exist
if (!player) { if (!player) {
newplayer = true; newplayer = true;
player = new RemotePlayer(this); player = new RemotePlayer(this, name);
player->updateName(name);
/* Set player position */ /* Set player position */
infostream<<"Server: Finding spawn place for player \"" infostream<<"Server: Finding spawn place for player \""
<<name<<"\""<<std::endl; <<name<<"\""<<std::endl;