1
0
mirror of https://github.com/moparisthebest/minetest synced 2024-12-22 23:58:48 -05:00

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
*/
{
Player *player = new LocalPlayer(this);
player->updateName(playername);
Player *player = new LocalPlayer(this, playername);
m_env.addPlayer(player);
}

View 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

View 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)),

View File

@ -32,7 +32,7 @@ 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

View 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);

View 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);

View 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;