Skip to content
Permalink
Browse files

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

* Server/Client Environments now have an helper to cast Player object in the right type to use it
* Server: use RemotePlayer everywhere and remove previous added casts
* Client: use LocalPlayer where needed
* Environment: remove unused functions (getPlayers(), getRandomConnectedPlayer(), getNearestConnectedPlayer())
  • Loading branch information
nerzhul committed Oct 8, 2016
1 parent 9393e4a commit 656faf7373587bc59b47986a28dbd2fce4c45474
@@ -1418,7 +1418,7 @@ Inventory* Client::getInventory(const InventoryLocation &loc)
break;
case InventoryLocation::PLAYER:
{
Player *player = m_env.getPlayer(loc.name.c_str());
LocalPlayer *player = m_env.getPlayer(loc.name.c_str());
if(!player)
return NULL;
return &player->inventory;
@@ -127,65 +127,6 @@ Player * Environment::getPlayer(const char *name)
return NULL;
}

Player * Environment::getRandomConnectedPlayer()
{
std::vector<Player*> connected_players = getPlayers(true);
u32 chosen_one = myrand() % connected_players.size();
u32 j = 0;
for(std::vector<Player*>::iterator
i = connected_players.begin();
i != connected_players.end(); ++i) {
if(j == chosen_one) {
Player *player = *i;
return player;
}
j++;
}
return NULL;
}

Player * Environment::getNearestConnectedPlayer(v3f pos)
{
std::vector<Player*> connected_players = getPlayers(true);
f32 nearest_d = 0;
Player *nearest_player = NULL;
for(std::vector<Player*>::iterator
i = connected_players.begin();
i != connected_players.end(); ++i) {
Player *player = *i;
f32 d = player->getPosition().getDistanceFrom(pos);
if(d < nearest_d || nearest_player == NULL) {
nearest_d = d;
nearest_player = player;
}
}
return nearest_player;
}

std::vector<Player*> Environment::getPlayers()
{
return m_players;
}

std::vector<Player*> Environment::getPlayers(bool ignore_disconnected)
{
std::vector<Player*> newlist;
for(std::vector<Player*>::iterator
i = m_players.begin();
i != m_players.end(); ++i) {
Player *player = *i;

if(ignore_disconnected) {
// Ignore disconnected players
if(player->peer_id == 0)
continue;
}

newlist.push_back(player);
}
return newlist;
}

u32 Environment::getDayNightRatio()
{
MutexAutoLock lock(this->m_time_lock);
@@ -199,11 +140,6 @@ void Environment::setTimeOfDaySpeed(float speed)
m_time_of_day_speed = speed;
}

float Environment::getTimeOfDaySpeed()
{
return m_time_of_day_speed;
}

void Environment::setDayNightRatioOverride(bool enable, u32 value)
{
MutexAutoLock lock(this->m_time_lock);
@@ -625,6 +561,16 @@ ServerMap & ServerEnvironment::getServerMap()
return *m_map;
}

RemotePlayer *ServerEnvironment::getPlayer(const u16 peer_id)
{
return dynamic_cast<RemotePlayer *>(Environment::getPlayer(peer_id));
}

RemotePlayer *ServerEnvironment::getPlayer(const char* name)
{
return dynamic_cast<RemotePlayer *>(Environment::getPlayer(name));
}

bool ServerEnvironment::line_of_sight(v3f pos1, v3f pos2, float stepsize, v3s16 *p)
{
float distance = pos1.getDistanceFrom(pos2);
@@ -2349,6 +2295,16 @@ ClientMap & ClientEnvironment::getClientMap()
return *m_map;
}

LocalPlayer *ClientEnvironment::getPlayer(const u16 peer_id)
{
return dynamic_cast<LocalPlayer *>(Environment::getPlayer(peer_id));
}

LocalPlayer *ClientEnvironment::getPlayer(const char* name)
{
return dynamic_cast<LocalPlayer *>(Environment::getPlayer(name));
}

void ClientEnvironment::addPlayer(Player *player)
{
DSTACK(FUNCTION_NAME);
@@ -74,12 +74,6 @@ class Environment

virtual void addPlayer(Player *player);
void removePlayer(Player *player);
Player * getPlayer(u16 peer_id);
Player * getPlayer(const char *name);
Player * getRandomConnectedPlayer();
Player * getNearestConnectedPlayer(v3f pos);
std::vector<Player*> getPlayers();
std::vector<Player*> getPlayers(bool ignore_disconnected);

u32 getDayNightRatio();

@@ -91,7 +85,6 @@ class Environment
void stepTimeOfDay(float dtime);

void setTimeOfDaySpeed(float speed);
float getTimeOfDaySpeed();

void setDayNightRatioOverride(bool enable, u32 value);

@@ -101,6 +94,9 @@ class Environment
u32 m_added_objects;

protected:
Player * getPlayer(u16 peer_id);
Player * getPlayer(const char *name);

// peer_ids in here should be unique, except that there may be many 0s
std::vector<Player*> m_players;

@@ -440,6 +436,8 @@ class ServerEnvironment : public Environment
void setStaticForActiveObjectsInBlock(v3s16 blockpos,
bool static_exists, v3s16 static_block=v3s16(0,0,0));

RemotePlayer *getPlayer(const u16 peer_id);
RemotePlayer *getPlayer(const char* name);
private:

/*
@@ -640,8 +638,10 @@ class ClientEnvironment : public Environment
{ m_player_names.remove(name); }
void updateCameraOffset(v3s16 camera_offset)
{ m_camera_offset = camera_offset; }
v3s16 getCameraOffset()
{ return m_camera_offset; }
v3s16 getCameraOffset() const { return m_camera_offset; }

LocalPlayer *getPlayer(const u16 peer_id);
LocalPlayer *getPlayer(const char* name);

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

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

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

if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: "
@@ -1081,8 +1079,7 @@ void Server::handleCommand_Damage(NetworkPacket* pkt)

*pkt >> damage;

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

if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: "
@@ -1117,8 +1114,7 @@ void Server::handleCommand_Breath(NetworkPacket* pkt)

*pkt >> breath;

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

if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: "
@@ -1231,8 +1227,7 @@ void Server::handleCommand_PlayerItem(NetworkPacket* pkt)
if (pkt->getSize() < 2)
return;

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

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

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

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

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

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

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

if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: "
@@ -420,7 +420,7 @@ void InvRef::create(lua_State *L, const InventoryLocation &loc)
luaL_getmetatable(L, className);
lua_setmetatable(L, -2);
}
void InvRef::createPlayer(lua_State *L, Player *player)
void InvRef::createPlayer(lua_State *L, RemotePlayer *player)
{
NO_MAP_LOCK_REQUIRED;
InventoryLocation loc;
@@ -25,7 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "inventory.h"
#include "inventorymanager.h"

class Player;
class RemotePlayer;

/*
InvRef
@@ -112,7 +112,7 @@ class InvRef : public ModApiBase {
// Creates an InvRef and leaves it on top of stack
// Not callable from Lua; all references are created on the C side.
static void create(lua_State *L, const InventoryLocation &loc);
static void createPlayer(lua_State *L, Player *player);
static void createPlayer(lua_State *L, RemotePlayer *player);
static void createNodeMeta(lua_State *L, v3s16 p);
static void Register(lua_State *L);
};
@@ -123,11 +123,6 @@ class ModApiInventory : public ModApiBase {

static int l_get_inventory(lua_State *L);

static void inventory_set_list_from_lua(Inventory *inv, const char *name,
lua_State *L, int tableindex, int forcesize);
static void inventory_get_list_to_lua(Inventory *inv, const char *name,
lua_State *L);

public:
static void Initialize(lua_State *L, int top);
};
@@ -508,7 +508,7 @@ int ObjectRef::l_set_local_animation(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;
// Do it
@@ -554,7 +554,7 @@ int ObjectRef::l_set_eye_offset(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;
// Do it
@@ -1256,7 +1256,7 @@ int ObjectRef::l_hud_add(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;

@@ -1319,7 +1319,7 @@ int ObjectRef::l_hud_remove(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;

@@ -1339,7 +1339,7 @@ int ObjectRef::l_hud_change(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;

@@ -1552,7 +1552,7 @@ int ObjectRef::l_hud_set_hotbar_image(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;

@@ -1567,7 +1567,7 @@ int ObjectRef::l_hud_get_hotbar_image(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;

@@ -1581,7 +1581,7 @@ int ObjectRef::l_hud_set_hotbar_selected_image(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;

@@ -1596,7 +1596,7 @@ int ObjectRef::l_hud_get_hotbar_selected_image(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;

@@ -1610,7 +1610,7 @@ int ObjectRef::l_set_sky(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;

@@ -26,7 +26,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
class ServerActiveObject;
class LuaEntitySAO;
class PlayerSAO;
class Player;
class RemotePlayer;

/*

0 comments on commit 656faf7

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