Skip to content
Permalink
Browse files

RemotePlayer/LocalPlayer Player base class proper separation (code cl…

…eanup) (patch 3 of X)

* remove IGameDef from Player class, only LocalPlayer has it now
* move many attributes/functions only used by LocalPlayer from Player to LocalPlayer
* move many attributes/functions only used by RemotePlayer from Player to RemotePlayer
* make some functions const
* hudGetHotbarSelectedImage now returns const ref
* RemotePlayer getHotbarSelectedImage now returns const ref
* various code style fixes
  • Loading branch information
nerzhul committed Oct 8, 2016
1 parent edba6e5 commit 7bbd716426bf989bf071e2322a9b797cc5f78acb
Showing with 255 additions and 292 deletions.
  1. +11 −11 src/environment.cpp
  2. +1 −1 src/environment.h
  3. +20 −4 src/localplayer.cpp
  4. +23 −1 src/localplayer.h
  5. +82 −94 src/player.cpp
  6. +107 −163 src/player.h
  7. +1 −1 src/script/lua_api/l_object.cpp
  8. +6 −16 src/server.cpp
  9. +4 −1 src/server.h
@@ -603,8 +603,9 @@ void ServerEnvironment::kickAllPlayers(AccessDeniedCode reason,
{
for (std::vector<Player*>::iterator it = m_players.begin();
it != m_players.end(); ++it) {
((Server*)m_gamedef)->DenyAccessVerCompliant((*it)->peer_id,
(*it)->protocol_version, reason, str_reason, reconnect);
RemotePlayer *player = dynamic_cast<RemotePlayer *>(*it);
((Server*)m_gamedef)->DenyAccessVerCompliant(player->peer_id,
player->protocol_version, reason, str_reason, reconnect);
}
}

@@ -618,7 +619,7 @@ void ServerEnvironment::saveLoadedPlayers()
++it) {
RemotePlayer *player = static_cast<RemotePlayer*>(*it);
if (player->checkModified()) {
player->save(players_path);
player->save(players_path, m_gamedef);
}
}
}
@@ -628,7 +629,7 @@ void ServerEnvironment::savePlayer(RemotePlayer *player)
std::string players_path = m_path_world + DIR_DELIM "players";
fs::CreateDir(players_path);

player->save(players_path);
player->save(players_path, m_gamedef);
}

RemotePlayer *ServerEnvironment::loadPlayer(const std::string &playername)
@@ -640,7 +641,7 @@ RemotePlayer *ServerEnvironment::loadPlayer(const std::string &playername)

RemotePlayer *player = getPlayer(playername.c_str());
if (!player) {
player = new RemotePlayer(m_gamedef, "");
player = new RemotePlayer("", m_gamedef->idef());
newplayer = true;
}

@@ -2300,15 +2301,14 @@ LocalPlayer *ClientEnvironment::getPlayer(const char* name)
return dynamic_cast<LocalPlayer *>(Environment::getPlayer(name));
}

void ClientEnvironment::addPlayer(Player *player)
void ClientEnvironment::addPlayer(LocalPlayer *player)
{
DSTACK(FUNCTION_NAME);
/*
It is a failure if player is local and there already is a local
player
It is a failure if already is a local player
*/
FATAL_ERROR_IF(player->isLocal() && getLocalPlayer() != NULL,
"Player is local but there is already a local player");
FATAL_ERROR_IF(getLocalPlayer() != NULL,
"Player is local but there is already a local player");

Environment::addPlayer(player);
}
@@ -2563,7 +2563,7 @@ void ClientEnvironment::step(float dtime)
*/
for (std::vector<Player*>::iterator i = m_players.begin();
i != m_players.end(); ++i) {
LocalPlayer *player = dynamic_cast<LocalPlayer *>(*i);
Player *player = *i;
assert(player);

/*
@@ -579,7 +579,7 @@ class ClientEnvironment : public Environment

void step(f32 dtime);

virtual void addPlayer(Player *player);
virtual void addPlayer(LocalPlayer *player);
LocalPlayer * getLocalPlayer();

/*
@@ -26,16 +26,29 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "settings.h"
#include "environment.h"
#include "map.h"
#include "util/numeric.h"
#include "client.h"

/*
LocalPlayer
*/

LocalPlayer::LocalPlayer(IGameDef *gamedef, const char *name):
Player(gamedef, name),
LocalPlayer::LocalPlayer(Client *gamedef, const char *name):
Player(name, gamedef->idef()),
parent(0),
got_teleported(false),
isAttached(false),
touching_ground(false),
in_liquid(false),
in_liquid_stable(false),
liquid_viscosity(0),
is_climbing(false),
swimming_vertical(false),
// Movement overrides are multipliers and must be 1 by default
physics_override_speed(1.0f),
physics_override_jump(1.0f),
physics_override_gravity(1.0f),
physics_override_sneak(true),
physics_override_sneak_glitch(true),
overridePosition(v3f(0,0,0)),
last_position(v3f(0,0,0)),
last_speed(v3f(0,0,0)),
@@ -47,14 +60,17 @@ LocalPlayer::LocalPlayer(IGameDef *gamedef, const char *name):
hotbar_image(""),
hotbar_selected_image(""),
light_color(255,255,255,255),
hurt_tilt_timer(0.0f),
hurt_tilt_strength(0.0f),
m_sneak_node(32767,32767,32767),
m_sneak_node_exists(false),
m_need_to_get_new_sneak_node(true),
m_sneak_node_bb_ymax(0),
m_old_node_below(32767,32767,32767),
m_old_node_below_type("air"),
m_can_jump(false),
m_cao(NULL)
m_cao(NULL),
m_gamedef(gamedef)
{
// Initialize hp to 0, so that no hearts will be shown if server
// doesn't support health points
@@ -21,8 +21,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define LOCALPLAYER_HEADER

#include "player.h"
#include "environment.h"
#include <list>

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

bool isLocal() const
@@ -42,7 +44,23 @@ class LocalPlayer : public Player

ClientActiveObject *parent;

bool got_teleported;
bool isAttached;
bool touching_ground;
// This oscillates so that the player jumps a bit above the surface
bool in_liquid;
// This is more stable and defines the maximum speed of the player
bool in_liquid_stable;
// Gets the viscosity of water to calculate friction
u8 liquid_viscosity;
bool is_climbing;
bool swimming_vertical;

float physics_override_speed;
float physics_override_jump;
float physics_override_gravity;
bool physics_override_sneak;
bool physics_override_sneak_glitch;

v3f overridePosition;

@@ -71,6 +89,9 @@ class LocalPlayer : public Player

video::SColor light_color;

float hurt_tilt_timer;
float hurt_tilt_strength;

GenericCAO* getCAO() const {
return m_cao;
}
@@ -102,6 +123,7 @@ class LocalPlayer : public Player
bool m_can_jump;

GenericCAO* m_cao;
Client *m_gamedef;
};

#endif

0 comments on commit 7bbd716

Please sign in to comment.
You can’t perform that action at this time.