Skip to content
Permalink
Browse files

Player/LocalPlayer/RemotePlayer inheritance cleanup (part 1 on X)

* LocalPlayer take ownership of maxHudId as it's the only caller
* RemotePlayer take ownership of day night ratio as it's the only user
* Pass getPlayerControl as const reference to prevent object copy on each call (perf improvement in ObjectRef::l_get_player_control call)
* getPlayerSAO is now only RemotePlayer call
* get/setHotbarItemCount is now RemotePlayer owned
* Server: Use RemotePlayer instead of Player object on concerned call to properly fix the object type
* PlayerSAO now uses RemotePlayer instead of Player because it's only server side
* ObjectRef::getplayer also returns RemotePlayer as it's linked with PlayerSAO
  • Loading branch information
nerzhul committed Oct 8, 2016
1 parent 0264e38 commit 8bcd10b872bc88c6f474913d6efb8d53c50c5ae1
@@ -751,7 +751,7 @@ bool LuaEntitySAO::collideWithObjects(){

// No prototype, PlayerSAO does not need to be deserialized

PlayerSAO::PlayerSAO(ServerEnvironment *env_, Player *player_, u16 peer_id_,
PlayerSAO::PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, u16 peer_id_,
const std::set<std::string> &privs, bool is_singleplayer):
ServerActiveObject(env_, v3f(0,0,0)),
m_player(player_),
@@ -833,11 +833,10 @@ void PlayerSAO::addedToEnvironment(u32 dtime_s)
void PlayerSAO::removingFromEnvironment()
{
ServerActiveObject::removingFromEnvironment();
if(m_player->getPlayerSAO() == this)
{
if (m_player->getPlayerSAO() == this) {
m_player->setPlayerSAO(NULL);
m_player->peer_id = 0;
m_env->savePlayer((RemotePlayer*)m_player);
m_env->savePlayer(m_player);
m_env->removePlayer(m_player);
}
}
@@ -160,7 +160,7 @@ class LagPool
class PlayerSAO : public ServerActiveObject
{
public:
PlayerSAO(ServerEnvironment *env_, Player *player_, u16 peer_id_,
PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, u16 peer_id_,
const std::set<std::string> &privs, bool is_singleplayer);
~PlayerSAO();
ActiveObjectType getType() const
@@ -231,14 +231,8 @@ class PlayerSAO : public ServerActiveObject

void disconnected();

Player* getPlayer()
{
return m_player;
}
u16 getPeerID() const
{
return m_peer_id;
}
RemotePlayer* getPlayer() { return m_player; }
u16 getPeerID() const { return m_peer_id; }

// Cheat prevention

@@ -291,7 +285,7 @@ class PlayerSAO : public ServerActiveObject
private:
std::string getPropertyPacket();

Player *m_player;
RemotePlayer *m_player;
u16 m_peer_id;
Inventory *m_inventory;
s16 m_damage;
@@ -2851,7 +2851,7 @@ void Game::processItemSelection(u16 *new_playeritem)

s32 wheel = input->getMouseWheel();
u16 max_item = MYMIN(PLAYER_INVENTORY_SIZE - 1,
player->hud_hotbar_itemcount - 1);
player->hud_hotbar_itemcount - 1);

s32 dir = wheel;

@@ -80,6 +80,8 @@ class LocalPlayer : public Player
m_cao = toset;
}

u32 maxHudId() const { return hud.size(); }

private:
void accelerateHorizontal(const v3f &target_speed, const f32 max_increase);
void accelerateVertical(const v3f &target_speed, const f32 max_increase);
@@ -1122,7 +1122,7 @@ void Client::handleCommand_HudSetParam(NetworkPacket* pkt)

*pkt >> param >> value;

Player *player = m_env.getLocalPlayer();
LocalPlayer *player = m_env.getLocalPlayer();
assert(player != NULL);

if (param == HUD_PARAM_HOTBAR_ITEMCOUNT && value.size() == 4) {
@@ -1131,10 +1131,10 @@ void Client::handleCommand_HudSetParam(NetworkPacket* pkt)
player->hud_hotbar_itemcount = hotbar_itemcount;
}
else if (param == HUD_PARAM_HOTBAR_IMAGE) {
((LocalPlayer *) player)->hotbar_image = value;
player->hotbar_image = value;
}
else if (param == HUD_PARAM_HOTBAR_SELECTED_IMAGE) {
((LocalPlayer *) player)->hotbar_selected_image = value;
player->hotbar_selected_image = value;
}
}

@@ -800,7 +800,8 @@ void Server::handleCommand_PlayerPos(NetworkPacket* pkt)
pitch = modulo360f(pitch);
yaw = modulo360f(yaw);

Player *player = m_env->getPlayer(pkt->getPeerId());
RemotePlayer *player =
dynamic_cast<RemotePlayer *>(m_env->getPlayer(pkt->getPeerId()));
if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: "
"No player for peer_id=" << pkt->getPeerId()
@@ -879,7 +880,9 @@ void Server::handleCommand_DeletedBlocks(NetworkPacket* pkt)

void Server::handleCommand_InventoryAction(NetworkPacket* pkt)
{
Player *player = m_env->getPlayer(pkt->getPeerId());
RemotePlayer *player =
dynamic_cast<RemotePlayer *>(m_env->getPlayer(pkt->getPeerId()));

if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: "
"No player for peer_id=" << pkt->getPeerId()
@@ -1078,7 +1081,9 @@ void Server::handleCommand_Damage(NetworkPacket* pkt)

*pkt >> damage;

Player *player = m_env->getPlayer(pkt->getPeerId());
RemotePlayer *player =
dynamic_cast<RemotePlayer *>(m_env->getPlayer(pkt->getPeerId()));

if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: "
"No player for peer_id=" << pkt->getPeerId()
@@ -1112,7 +1117,9 @@ void Server::handleCommand_Breath(NetworkPacket* pkt)

*pkt >> breath;

Player *player = m_env->getPlayer(pkt->getPeerId());
RemotePlayer *player =
dynamic_cast<RemotePlayer *>(m_env->getPlayer(pkt->getPeerId()));

if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: "
"No player for peer_id=" << pkt->getPeerId()
@@ -1224,7 +1231,9 @@ void Server::handleCommand_PlayerItem(NetworkPacket* pkt)
if (pkt->getSize() < 2)
return;

Player *player = m_env->getPlayer(pkt->getPeerId());
RemotePlayer *player =
dynamic_cast<RemotePlayer *>(m_env->getPlayer(pkt->getPeerId()));

if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: "
"No player for peer_id=" << pkt->getPeerId()
@@ -1299,7 +1308,9 @@ void Server::handleCommand_Interact(NetworkPacket* pkt)
verbosestream << "TOSERVER_INTERACT: action=" << (int)action << ", item="
<< item_i << ", pointed=" << pointed.dump() << std::endl;

Player *player = m_env->getPlayer(pkt->getPeerId());
RemotePlayer *player =
dynamic_cast<RemotePlayer *>(m_env->getPlayer(pkt->getPeerId()));

if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: "
"No player for peer_id=" << pkt->getPeerId()
@@ -1719,7 +1730,9 @@ void Server::handleCommand_NodeMetaFields(NetworkPacket* pkt)
fields[fieldname] = pkt->readLongString();
}

Player *player = m_env->getPlayer(pkt->getPeerId());
RemotePlayer *player =
dynamic_cast<RemotePlayer *>(m_env->getPlayer(pkt->getPeerId()));

if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: "
"No player for peer_id=" << pkt->getPeerId()
@@ -1769,7 +1782,9 @@ void Server::handleCommand_InventoryFields(NetworkPacket* pkt)
fields[fieldname] = pkt->readLongString();
}

Player *player = m_env->getPlayer(pkt->getPeerId());
RemotePlayer *player =
dynamic_cast<RemotePlayer *>(m_env->getPlayer(pkt->getPeerId()));

if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: "
"No player for peer_id=" << pkt->getPeerId()
@@ -233,16 +233,6 @@ class Player
return size;
}

void setHotbarItemcount(s32 hotbar_itemcount)
{
hud_hotbar_itemcount = hotbar_itemcount;
}

s32 getHotbarItemcount()
{
return hud_hotbar_itemcount;
}

void setHotbarImage(const std::string &name)
{
hud_hotbar_image = name;
@@ -278,18 +268,6 @@ class Player
*params = m_sky_params;
}

void overrideDayNightRatio(bool do_override, float ratio)
{
m_day_night_ratio_do_override = do_override;
m_day_night_ratio = ratio;
}

void getDayNightRatio(bool *do_override, float *ratio)
{
*do_override = m_day_night_ratio_do_override;
*ratio = m_day_night_ratio;
}

void setLocalAnimations(v2s32 frames[4], float frame_speed)
{
for (int i = 0; i < 4; i++)
@@ -309,11 +287,6 @@ class Player
return false;
}

virtual PlayerSAO *getPlayerSAO()
{
return NULL;
}

virtual void setPlayerSAO(PlayerSAO *sao)
{
FATAL_ERROR("FIXME");
@@ -335,7 +308,7 @@ class Player
void setModified(const bool x)
{
m_dirty = x;
if (x == false)
if (!x)
inventory.setModified(x);
}

@@ -391,10 +364,7 @@ class Player
std::string inventory_formspec;

PlayerControl control;
PlayerControl getPlayerControl()
{
return control;
}
const PlayerControl& getPlayerControl() { return control; }

u32 keyPressed;

@@ -403,9 +373,6 @@ class Player
u32 addHud(HudElement* hud);
HudElement* removeHud(u32 id);
void clearHud();
u32 maxHudId() {
return hud.size();
}

u32 hud_flags;
s32 hud_hotbar_itemcount;
@@ -429,9 +396,6 @@ class Player
std::string m_sky_type;
video::SColor m_sky_bgcolor;
std::vector<std::string> m_sky_params;

bool m_day_night_ratio_do_override;
float m_day_night_ratio;
private:
// Protect some critical areas
// hud for example can be modified by EmergeThread
@@ -463,6 +427,25 @@ class RemotePlayer : public Player

const RemotePlayerChatResult canSendChatMessage();

void setHotbarItemcount(s32 hotbar_itemcount)
{
hud_hotbar_itemcount = hotbar_itemcount;
}

s32 getHotbarItemcount() const { return hud_hotbar_itemcount; }

void overrideDayNightRatio(bool do_override, float ratio)
{
m_day_night_ratio_do_override = do_override;
m_day_night_ratio = ratio;
}

void getDayNightRatio(bool *do_override, float *ratio)
{
*do_override = m_day_night_ratio_do_override;
*ratio = m_day_night_ratio;
}

private:
PlayerSAO *m_sao;

@@ -473,6 +456,9 @@ class RemotePlayer : public Player
u32 m_last_chat_message_sent;
float m_chat_message_allowance;
u16 m_message_rate_overhead;

bool m_day_night_ratio_do_override;
float m_day_night_ratio;
};

#endif
@@ -494,8 +494,8 @@ int ModApiEnvMod::l_get_player_by_name(lua_State *L)

// Do it
const char *name = luaL_checkstring(L, 1);
Player *player = env->getPlayer(name);
if(player == NULL){
RemotePlayer *player = dynamic_cast<RemotePlayer *>(env->getPlayer(name));
if (player == NULL){
lua_pushnil(L);
return 1;
}
@@ -107,7 +107,7 @@ PlayerSAO* ObjectRef::getplayersao(ObjectRef *ref)
return (PlayerSAO*)obj;
}

Player* ObjectRef::getplayer(ObjectRef *ref)
RemotePlayer* ObjectRef::getplayer(ObjectRef *ref)
{
PlayerSAO *playersao = getplayersao(ref);
if (playersao == NULL)
@@ -1212,8 +1212,8 @@ int ObjectRef::l_get_player_control(lua_State *L)
lua_pushlstring(L, "", 0);
return 1;
}
// Do it
PlayerControl control = player->getPlayerControl();

const PlayerControl &control = player->getPlayerControl();
lua_newtable(L);
lua_pushboolean(L, control.up);
lua_setfield(L, -2, "up");
@@ -1467,7 +1467,7 @@ int ObjectRef::l_hud_set_flags(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref);
RemotePlayer *player = getplayer(ref);
if (player == NULL)
return 0;

@@ -1519,7 +1519,7 @@ int ObjectRef::l_hud_set_hotbar_itemcount(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref);
RemotePlayer *player = getplayer(ref);
if (player == NULL)
return 0;

@@ -1537,7 +1537,7 @@ int ObjectRef::l_hud_get_hotbar_itemcount(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref);
RemotePlayer *player = getplayer(ref);
if (player == NULL)
return 0;

@@ -1677,7 +1677,7 @@ int ObjectRef::l_override_day_night_ratio(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref);
RemotePlayer *player = getplayer(ref);
if (player == NULL)
return 0;

@@ -1700,7 +1700,7 @@ int ObjectRef::l_get_day_night_ratio(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref);
RemotePlayer *player = getplayer(ref);
if (player == NULL)
return 0;

0 comments on commit 8bcd10b

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