More code cleanup (UNORDERED + RemotePlayer/LocalPlayer)

* ClientEnvironment now uses UNORDERED MAP for active objects
* Use RemotePlayer and LocalPlayer everywhere it's possible
* Minor code style fixes
* Drop Client::getBreath() unused function
pr
Loic Blot 2016-10-08 16:31:22 +02:00 committed by Ner'zhul
parent 067766eec2
commit fd5a130b86
10 changed files with 124 additions and 152 deletions

View File

@ -383,7 +383,7 @@ void Client::step(float dtime)
if(counter <= 0.0) { if(counter <= 0.0) {
counter = 2.0; counter = 2.0;
Player *myplayer = m_env.getLocalPlayer(); LocalPlayer *myplayer = m_env.getLocalPlayer();
FATAL_ERROR_IF(myplayer == NULL, "Local player not found in environment."); FATAL_ERROR_IF(myplayer == NULL, "Local player not found in environment.");
u16 proto_version_min = g_settings->getFlag("send_pre_v25_init") ? u16 proto_version_min = g_settings->getFlag("send_pre_v25_init") ?
@ -613,7 +613,7 @@ void Client::step(float dtime)
{ {
// Do this every <interval> seconds after TOCLIENT_INVENTORY // Do this every <interval> seconds after TOCLIENT_INVENTORY
// Reset the locally changed inventory to the authoritative inventory // Reset the locally changed inventory to the authoritative inventory
Player *player = m_env.getLocalPlayer(); LocalPlayer *player = m_env.getLocalPlayer();
player->inventory = *m_inventory_from_server; player->inventory = *m_inventory_from_server;
m_inventory_updated = true; m_inventory_updated = true;
} }
@ -1191,7 +1191,7 @@ void Client::sendChatMessage(const std::wstring &message)
void Client::sendChangePassword(const std::string &oldpassword, void Client::sendChangePassword(const std::string &oldpassword,
const std::string &newpassword) const std::string &newpassword)
{ {
Player *player = m_env.getLocalPlayer(); LocalPlayer *player = m_env.getLocalPlayer();
if (player == NULL) if (player == NULL)
return; return;
@ -1317,7 +1317,7 @@ void Client::sendPlayerPos()
void Client::sendPlayerItem(u16 item) void Client::sendPlayerItem(u16 item)
{ {
Player *myplayer = m_env.getLocalPlayer(); LocalPlayer *myplayer = m_env.getLocalPlayer();
if(myplayer == NULL) if(myplayer == NULL)
return; return;
@ -1398,7 +1398,7 @@ bool Client::getLocalInventoryUpdated()
// Copies the inventory of the local player to parameter // Copies the inventory of the local player to parameter
void Client::getLocalInventory(Inventory &dst) void Client::getLocalInventory(Inventory &dst)
{ {
Player *player = m_env.getLocalPlayer(); LocalPlayer *player = m_env.getLocalPlayer();
assert(player != NULL); assert(player != NULL);
dst = player->inventory; dst = player->inventory;
} }
@ -1411,7 +1411,7 @@ Inventory* Client::getInventory(const InventoryLocation &loc)
break; break;
case InventoryLocation::CURRENT_PLAYER: case InventoryLocation::CURRENT_PLAYER:
{ {
Player *player = m_env.getLocalPlayer(); LocalPlayer *player = m_env.getLocalPlayer();
assert(player != NULL); assert(player != NULL);
return &player->inventory; return &player->inventory;
} }
@ -1537,18 +1537,11 @@ void Client::setCrack(int level, v3s16 pos)
u16 Client::getHP() u16 Client::getHP()
{ {
Player *player = m_env.getLocalPlayer(); LocalPlayer *player = m_env.getLocalPlayer();
assert(player != NULL); assert(player != NULL);
return player->hp; return player->hp;
} }
u16 Client::getBreath()
{
Player *player = m_env.getLocalPlayer();
assert(player != NULL);
return player->getBreath();
}
bool Client::getChatMessage(std::wstring &message) bool Client::getChatMessage(std::wstring &message)
{ {
if(m_chat_queue.size() == 0) if(m_chat_queue.size() == 0)

View File

@ -460,7 +460,6 @@ public:
void setCrack(int level, v3s16 pos); void setCrack(int level, v3s16 pos);
u16 getHP(); u16 getHP();
u16 getBreath();
bool checkPrivilege(const std::string &priv) const bool checkPrivilege(const std::string &priv) const
{ return (m_privileges.count(priv) != 0); } { return (m_privileges.count(priv) != 0); }

View File

@ -77,9 +77,9 @@ void RemoteClient::GetNextBlocks (
if(m_nothing_to_send_pause_timer >= 0) if(m_nothing_to_send_pause_timer >= 0)
return; return;
Player *player = env->getPlayer(peer_id); RemotePlayer *player = env->getPlayer(peer_id);
// This can happen sometimes; clients and players are not in perfect sync. // This can happen sometimes; clients and players are not in perfect sync.
if(player == NULL) if (player == NULL)
return; return;
// Won't send anything if already sending // Won't send anything if already sending
@ -645,8 +645,7 @@ void ClientInterface::step(float dtime)
void ClientInterface::UpdatePlayerList() void ClientInterface::UpdatePlayerList()
{ {
if (m_env != NULL) if (m_env != NULL) {
{
std::vector<u16> clients = getClientIDs(); std::vector<u16> clients = getClientIDs();
m_clients_names.clear(); m_clients_names.clear();
@ -654,10 +653,8 @@ void ClientInterface::UpdatePlayerList()
if(!clients.empty()) if(!clients.empty())
infostream<<"Players:"<<std::endl; infostream<<"Players:"<<std::endl;
for(std::vector<u16>::iterator for (std::vector<u16>::iterator i = clients.begin(); i != clients.end(); ++i) {
i = clients.begin(); RemotePlayer *player = m_env->getPlayer(*i);
i != clients.end(); ++i) {
Player *player = m_env->getPlayer(*i);
if (player == NULL) if (player == NULL)
continue; continue;

View File

@ -655,12 +655,11 @@ void GenericCAO::initialize(const std::string &data)
if(m_is_player) if(m_is_player)
{ {
Player *player = m_env->getPlayer(m_name.c_str()); LocalPlayer *player = m_env->getPlayer(m_name.c_str());
if(player && player->isLocal()) if (player && player->isLocal()) {
{
m_is_local_player = true; m_is_local_player = true;
m_is_visible = false; m_is_visible = false;
LocalPlayer* localplayer = dynamic_cast<LocalPlayer*>(player); LocalPlayer* localplayer = player;
assert( localplayer != NULL ); assert( localplayer != NULL );
localplayer->setCAO(this); localplayer->setCAO(this);

View File

@ -105,20 +105,20 @@ void Environment::removePlayer(Player* player)
} }
} }
Player * Environment::getPlayer(u16 peer_id) Player *Environment::getPlayer(u16 peer_id)
{ {
for(std::vector<Player*>::iterator i = m_players.begin(); for (std::vector<Player*>::iterator i = m_players.begin();
i != m_players.end(); ++i) { i != m_players.end(); ++i) {
Player *player = *i; Player *player = *i;
if(player->peer_id == peer_id) if (player->peer_id == peer_id)
return player; return player;
} }
return NULL; return NULL;
} }
Player * Environment::getPlayer(const char *name) Player *Environment::getPlayer(const char *name)
{ {
for(std::vector<Player*>::iterator i = m_players.begin(); for (std::vector<Player*>::iterator i = m_players.begin();
i != m_players.end(); ++i) { i != m_players.end(); ++i) {
Player *player = *i; Player *player = *i;
if(strcmp(player->getName(), name) == 0) if(strcmp(player->getName(), name) == 0)
@ -602,11 +602,9 @@ void ServerEnvironment::kickAllPlayers(AccessDeniedCode reason,
const std::string &str_reason, bool reconnect) const std::string &str_reason, bool reconnect)
{ {
for (std::vector<Player*>::iterator it = m_players.begin(); for (std::vector<Player*>::iterator it = m_players.begin();
it != m_players.end(); it != m_players.end(); ++it) {
++it) {
((Server*)m_gamedef)->DenyAccessVerCompliant((*it)->peer_id, ((Server*)m_gamedef)->DenyAccessVerCompliant((*it)->peer_id,
(*it)->protocol_version, (AccessDeniedCode)reason, (*it)->protocol_version, reason, str_reason, reconnect);
str_reason, reconnect);
} }
} }
@ -633,14 +631,14 @@ void ServerEnvironment::savePlayer(RemotePlayer *player)
player->save(players_path); player->save(players_path);
} }
Player *ServerEnvironment::loadPlayer(const std::string &playername) RemotePlayer *ServerEnvironment::loadPlayer(const std::string &playername)
{ {
bool newplayer = false; bool newplayer = false;
bool found = false; bool found = false;
std::string players_path = m_path_world + DIR_DELIM "players" DIR_DELIM; std::string players_path = m_path_world + DIR_DELIM "players" DIR_DELIM;
std::string path = players_path + playername; std::string path = players_path + playername;
RemotePlayer *player = static_cast<RemotePlayer *>(getPlayer(playername.c_str())); RemotePlayer *player = getPlayer(playername.c_str());
if (!player) { if (!player) {
player = new RemotePlayer(m_gamedef, ""); player = new RemotePlayer(m_gamedef, "");
newplayer = true; newplayer = true;
@ -1254,10 +1252,10 @@ void ServerEnvironment::step(float dtime)
*/ */
{ {
ScopeProfiler sp(g_profiler, "SEnv: handle players avg", SPT_AVG); ScopeProfiler sp(g_profiler, "SEnv: handle players avg", SPT_AVG);
for(std::vector<Player*>::iterator i = m_players.begin(); for (std::vector<Player*>::iterator i = m_players.begin();
i != m_players.end(); ++i) i != m_players.end(); ++i) {
{ RemotePlayer *player = dynamic_cast<RemotePlayer *>(*i);
Player *player = *i; assert(player);
// Ignore disconnected players // Ignore disconnected players
if(player->peer_id == 0) if(player->peer_id == 0)
@ -1277,12 +1275,12 @@ void ServerEnvironment::step(float dtime)
Get player block positions Get player block positions
*/ */
std::vector<v3s16> players_blockpos; std::vector<v3s16> players_blockpos;
for(std::vector<Player*>::iterator for (std::vector<Player*>::iterator i = m_players.begin();
i = m_players.begin();
i != m_players.end(); ++i) { i != m_players.end(); ++i) {
Player *player = *i; RemotePlayer *player = dynamic_cast<RemotePlayer *>(*i);
assert(player);
// Ignore disconnected players // Ignore disconnected players
if(player->peer_id == 0) if (player->peer_id == 0)
continue; continue;
v3s16 blockpos = getNodeBlockPos( v3s16 blockpos = getNodeBlockPos(
@ -1381,8 +1379,7 @@ void ServerEnvironment::step(float dtime)
block->m_node_timers.step((float)dtime); block->m_node_timers.step((float)dtime);
if (!elapsed_timers.empty()) { if (!elapsed_timers.empty()) {
MapNode n; MapNode n;
for (std::vector<NodeTimer>::iterator for (std::vector<NodeTimer>::iterator i = elapsed_timers.begin();
i = elapsed_timers.begin();
i != elapsed_timers.end(); ++i) { i != elapsed_timers.end(); ++i) {
n = block->getNodeNoEx(i->position); n = block->getNodeNoEx(i->position);
p = i->position + block->getPosRelative(); p = i->position + block->getPosRelative();
@ -1571,7 +1568,7 @@ u16 ServerEnvironment::addActiveObject(ServerActiveObject *object)
Finds out what new objects have been added to Finds out what new objects have been added to
inside a radius around a position inside a radius around a position
*/ */
void ServerEnvironment::getAddedActiveObjects(Player *player, s16 radius, void ServerEnvironment::getAddedActiveObjects(RemotePlayer *player, s16 radius,
s16 player_radius, s16 player_radius,
std::set<u16> &current_objects, std::set<u16> &current_objects,
std::queue<u16> &added_objects) std::queue<u16> &added_objects)
@ -1624,7 +1621,7 @@ void ServerEnvironment::getAddedActiveObjects(Player *player, s16 radius,
Finds out what objects have been removed from Finds out what objects have been removed from
inside a radius around a position inside a radius around a position
*/ */
void ServerEnvironment::getRemovedActiveObjects(Player *player, s16 radius, void ServerEnvironment::getRemovedActiveObjects(RemotePlayer *player, s16 radius,
s16 player_radius, s16 player_radius,
std::set<u16> &current_objects, std::set<u16> &current_objects,
std::queue<u16> &removed_objects) std::queue<u16> &removed_objects)
@ -2269,10 +2266,8 @@ ClientEnvironment::ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr,
ClientEnvironment::~ClientEnvironment() ClientEnvironment::~ClientEnvironment()
{ {
// delete active objects // delete active objects
for(std::map<u16, ClientActiveObject*>::iterator for (UNORDERED_MAP<u16, ClientActiveObject*>::iterator i = m_active_objects.begin();
i = m_active_objects.begin(); i != m_active_objects.end(); ++i) {
i != m_active_objects.end(); ++i)
{
delete i->second; delete i->second;
} }
@ -2312,18 +2307,18 @@ void ClientEnvironment::addPlayer(Player *player)
It is a failure if player is local and there already is a local It is a failure if player is local and there already is a local
player player
*/ */
FATAL_ERROR_IF(player->isLocal() == true && getLocalPlayer() != NULL, FATAL_ERROR_IF(player->isLocal() && getLocalPlayer() != NULL,
"Player is local but there is already a local player"); "Player is local but there is already a local player");
Environment::addPlayer(player); Environment::addPlayer(player);
} }
LocalPlayer * ClientEnvironment::getLocalPlayer() LocalPlayer *ClientEnvironment::getLocalPlayer()
{ {
for(std::vector<Player*>::iterator i = m_players.begin(); for(std::vector<Player*>::iterator i = m_players.begin();
i != m_players.end(); ++i) { i != m_players.end(); ++i) {
Player *player = *i; Player *player = *i;
if(player->isLocal()) if (player->isLocal())
return (LocalPlayer*)player; return (LocalPlayer*)player;
} }
return NULL; return NULL;
@ -2407,11 +2402,11 @@ void ClientEnvironment::step(float dtime)
{ {
// Apply physics // Apply physics
if(free_move == false && is_climbing == false) if(!free_move && !is_climbing)
{ {
// Gravity // Gravity
v3f speed = lplayer->getSpeed(); v3f speed = lplayer->getSpeed();
if(lplayer->in_liquid == false) if(!lplayer->in_liquid)
speed.Y -= lplayer->movement_gravity * lplayer->physics_override_gravity * dtime_part * 2; speed.Y -= lplayer->movement_gravity * lplayer->physics_override_gravity * dtime_part * 2;
// Liquid floating / sinking // Liquid floating / sinking
@ -2566,14 +2561,15 @@ void ClientEnvironment::step(float dtime)
/* /*
Stuff that can be done in an arbitarily large dtime Stuff that can be done in an arbitarily large dtime
*/ */
for(std::vector<Player*>::iterator i = m_players.begin(); for (std::vector<Player*>::iterator i = m_players.begin();
i != m_players.end(); ++i) { i != m_players.end(); ++i) {
Player *player = *i; LocalPlayer *player = dynamic_cast<LocalPlayer *>(*i);
assert(player);
/* /*
Handle non-local players Handle non-local players
*/ */
if(player->isLocal() == false) { if (!player->isLocal()) {
// Move // Move
player->move(dtime, this, 100*BS); player->move(dtime, this, 100*BS);
@ -2604,10 +2600,8 @@ void ClientEnvironment::step(float dtime)
g_profiler->avg("CEnv: num of objects", m_active_objects.size()); g_profiler->avg("CEnv: num of objects", m_active_objects.size());
bool update_lighting = m_active_object_light_update_interval.step(dtime, 0.21); bool update_lighting = m_active_object_light_update_interval.step(dtime, 0.21);
for(std::map<u16, ClientActiveObject*>::iterator for (UNORDERED_MAP<u16, ClientActiveObject*>::iterator i = m_active_objects.begin();
i = m_active_objects.begin(); i != m_active_objects.end(); ++i) {
i != m_active_objects.end(); ++i)
{
ClientActiveObject* obj = i->second; ClientActiveObject* obj = i->second;
// Step object // Step object
obj->step(dtime, this); obj->step(dtime, this);
@ -2666,15 +2660,14 @@ GenericCAO* ClientEnvironment::getGenericCAO(u16 id)
ClientActiveObject* ClientEnvironment::getActiveObject(u16 id) ClientActiveObject* ClientEnvironment::getActiveObject(u16 id)
{ {
std::map<u16, ClientActiveObject*>::iterator n; UNORDERED_MAP<u16, ClientActiveObject*>::iterator n = m_active_objects.find(id);
n = m_active_objects.find(id); if (n == m_active_objects.end())
if(n == m_active_objects.end())
return NULL; return NULL;
return n->second; return n->second;
} }
bool isFreeClientActiveObjectId(u16 id, bool isFreeClientActiveObjectId(const u16 id,
std::map<u16, ClientActiveObject*> &objects) UNORDERED_MAP<u16, ClientActiveObject*> &objects)
{ {
if(id == 0) if(id == 0)
return false; return false;
@ -2682,19 +2675,17 @@ bool isFreeClientActiveObjectId(u16 id,
return objects.find(id) == objects.end(); return objects.find(id) == objects.end();
} }
u16 getFreeClientActiveObjectId( u16 getFreeClientActiveObjectId(UNORDERED_MAP<u16, ClientActiveObject*> &objects)
std::map<u16, ClientActiveObject*> &objects)
{ {
//try to reuse id's as late as possible //try to reuse id's as late as possible
static u16 last_used_id = 0; static u16 last_used_id = 0;
u16 startid = last_used_id; u16 startid = last_used_id;
for(;;) for(;;) {
{
last_used_id ++; last_used_id ++;
if(isFreeClientActiveObjectId(last_used_id, objects)) if (isFreeClientActiveObjectId(last_used_id, objects))
return last_used_id; return last_used_id;
if(last_used_id == startid) if (last_used_id == startid)
return 0; return 0;
} }
} }
@ -2714,8 +2705,7 @@ u16 ClientEnvironment::addActiveObject(ClientActiveObject *object)
} }
object->setId(new_id); object->setId(new_id);
} }
if(isFreeClientActiveObjectId(object->getId(), m_active_objects) == false) if(!isFreeClientActiveObjectId(object->getId(), m_active_objects)) {
{
infostream<<"ClientEnvironment::addActiveObject(): " infostream<<"ClientEnvironment::addActiveObject(): "
<<"id is not free ("<<object->getId()<<")"<<std::endl; <<"id is not free ("<<object->getId()<<")"<<std::endl;
delete object; delete object;
@ -2779,8 +2769,7 @@ void ClientEnvironment::removeActiveObject(u16 id)
verbosestream<<"ClientEnvironment::removeActiveObject(): " verbosestream<<"ClientEnvironment::removeActiveObject(): "
<<"id="<<id<<std::endl; <<"id="<<id<<std::endl;
ClientActiveObject* obj = getActiveObject(id); ClientActiveObject* obj = getActiveObject(id);
if(obj == NULL) if (obj == NULL) {
{
infostream<<"ClientEnvironment::removeActiveObject(): " infostream<<"ClientEnvironment::removeActiveObject(): "
<<"id="<<id<<" not found"<<std::endl; <<"id="<<id<<" not found"<<std::endl;
return; return;
@ -2848,10 +2837,8 @@ void ClientEnvironment::updateLocalPlayerBreath(u16 breath)
void ClientEnvironment::getActiveObjects(v3f origin, f32 max_d, void ClientEnvironment::getActiveObjects(v3f origin, f32 max_d,
std::vector<DistanceSortedActiveObject> &dest) std::vector<DistanceSortedActiveObject> &dest)
{ {
for(std::map<u16, ClientActiveObject*>::iterator for (UNORDERED_MAP<u16, ClientActiveObject*>::iterator i = m_active_objects.begin();
i = m_active_objects.begin(); i != m_active_objects.end(); ++i) {
i != m_active_objects.end(); ++i)
{
ClientActiveObject* obj = i->second; ClientActiveObject* obj = i->second;
f32 d = (obj->getPosition() - origin).getLength(); f32 d = (obj->getPosition() - origin).getLength();

View File

@ -94,8 +94,8 @@ public:
u32 m_added_objects; u32 m_added_objects;
protected: protected:
Player * getPlayer(u16 peer_id); Player *getPlayer(u16 peer_id);
Player * getPlayer(const char *name); Player *getPlayer(const char *name);
// peer_ids in here should be unique, except that there may be many 0s // peer_ids in here should be unique, except that there may be many 0s
std::vector<Player*> m_players; std::vector<Player*> m_players;
@ -324,7 +324,7 @@ public:
// Save players // Save players
void saveLoadedPlayers(); void saveLoadedPlayers();
void savePlayer(RemotePlayer *player); void savePlayer(RemotePlayer *player);
Player *loadPlayer(const std::string &playername); RemotePlayer *loadPlayer(const std::string &playername);
/* /*
Save and load time of day and game timer Save and load time of day and game timer
@ -368,7 +368,7 @@ public:
Find out what new objects have been added to Find out what new objects have been added to
inside a radius around a position inside a radius around a position
*/ */
void getAddedActiveObjects(Player *player, s16 radius, void getAddedActiveObjects(RemotePlayer *player, s16 radius,
s16 player_radius, s16 player_radius,
std::set<u16> &current_objects, std::set<u16> &current_objects,
std::queue<u16> &added_objects); std::queue<u16> &added_objects);
@ -377,7 +377,7 @@ public:
Find out what new objects have been removed from Find out what new objects have been removed from
inside a radius around a position inside a radius around a position
*/ */
void getRemovedActiveObjects(Player* player, s16 radius, void getRemovedActiveObjects(RemotePlayer* player, s16 radius,
s16 player_radius, s16 player_radius,
std::set<u16> &current_objects, std::set<u16> &current_objects,
std::queue<u16> &removed_objects); std::queue<u16> &removed_objects);
@ -649,7 +649,7 @@ private:
ITextureSource *m_texturesource; ITextureSource *m_texturesource;
IGameDef *m_gamedef; IGameDef *m_gamedef;
IrrlichtDevice *m_irr; IrrlichtDevice *m_irr;
std::map<u16, ClientActiveObject*> m_active_objects; UNORDERED_MAP<u16, ClientActiveObject*> m_active_objects;
std::vector<ClientSimpleObject*> m_simple_objects; std::vector<ClientSimpleObject*> m_simple_objects;
std::queue<ClientEnvEvent> m_client_event_queue; std::queue<ClientEnvEvent> m_client_event_queue;
IntervalLimiter m_active_object_light_update_interval; IntervalLimiter m_active_object_light_update_interval;

View File

@ -110,7 +110,7 @@ void Client::handleCommand_AuthAccept(NetworkPacket* pkt)
playerpos -= v3f(0, BS / 2, 0); playerpos -= v3f(0, BS / 2, 0);
// Set player position // Set player position
Player *player = m_env.getLocalPlayer(); LocalPlayer *player = m_env.getLocalPlayer();
assert(player != NULL); assert(player != NULL);
player->setPosition(playerpos); player->setPosition(playerpos);
@ -176,7 +176,7 @@ void Client::handleCommand_InitLegacy(NetworkPacket* pkt)
// Set player position // Set player position
Player *player = m_env.getLocalPlayer(); LocalPlayer *player = m_env.getLocalPlayer();
assert(player != NULL); assert(player != NULL);
player->setPosition(playerpos_f); player->setPosition(playerpos_f);
@ -333,7 +333,7 @@ void Client::handleCommand_Inventory(NetworkPacket* pkt)
std::string datastring(pkt->getString(0), pkt->getSize()); std::string datastring(pkt->getString(0), pkt->getSize());
std::istringstream is(datastring, std::ios_base::binary); std::istringstream is(datastring, std::ios_base::binary);
Player *player = m_env.getLocalPlayer(); LocalPlayer *player = m_env.getLocalPlayer();
assert(player != NULL); assert(player != NULL);
player->inventory.deSerialize(is); player->inventory.deSerialize(is);
@ -486,7 +486,7 @@ void Client::handleCommand_ActiveObjectMessages(NetworkPacket* pkt)
void Client::handleCommand_Movement(NetworkPacket* pkt) void Client::handleCommand_Movement(NetworkPacket* pkt)
{ {
Player *player = m_env.getLocalPlayer(); LocalPlayer *player = m_env.getLocalPlayer();
assert(player != NULL); assert(player != NULL);
float mad, maa, maf, msw, mscr, msf, mscl, msj, lf, lfs, ls, g; float mad, maa, maf, msw, mscr, msf, mscl, msj, lf, lfs, ls, g;
@ -511,7 +511,7 @@ void Client::handleCommand_Movement(NetworkPacket* pkt)
void Client::handleCommand_HP(NetworkPacket* pkt) void Client::handleCommand_HP(NetworkPacket* pkt)
{ {
Player *player = m_env.getLocalPlayer(); LocalPlayer *player = m_env.getLocalPlayer();
assert(player != NULL); assert(player != NULL);
u8 oldhp = player->hp; u8 oldhp = player->hp;
@ -532,7 +532,7 @@ void Client::handleCommand_HP(NetworkPacket* pkt)
void Client::handleCommand_Breath(NetworkPacket* pkt) void Client::handleCommand_Breath(NetworkPacket* pkt)
{ {
Player *player = m_env.getLocalPlayer(); LocalPlayer *player = m_env.getLocalPlayer();
assert(player != NULL); assert(player != NULL);
u16 breath; u16 breath;
@ -544,7 +544,7 @@ void Client::handleCommand_Breath(NetworkPacket* pkt)
void Client::handleCommand_MovePlayer(NetworkPacket* pkt) void Client::handleCommand_MovePlayer(NetworkPacket* pkt)
{ {
Player *player = m_env.getLocalPlayer(); LocalPlayer *player = m_env.getLocalPlayer();
assert(player != NULL); assert(player != NULL);
v3f pos; v3f pos;
@ -840,7 +840,7 @@ void Client::handleCommand_Privileges(NetworkPacket* pkt)
void Client::handleCommand_InventoryFormSpec(NetworkPacket* pkt) void Client::handleCommand_InventoryFormSpec(NetworkPacket* pkt)
{ {
Player *player = m_env.getLocalPlayer(); LocalPlayer *player = m_env.getLocalPlayer();
assert(player != NULL); assert(player != NULL);
// Store formspec in LocalPlayer // Store formspec in LocalPlayer
@ -1098,7 +1098,7 @@ void Client::handleCommand_HudSetFlags(NetworkPacket* pkt)
*pkt >> flags >> mask; *pkt >> flags >> mask;
Player *player = m_env.getLocalPlayer(); LocalPlayer *player = m_env.getLocalPlayer();
assert(player != NULL); assert(player != NULL);
bool was_minimap_visible = player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE; bool was_minimap_visible = player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE;

View File

@ -1052,7 +1052,7 @@ void Server::handleCommand_ChatMessage(NetworkPacket* pkt)
message += (wchar_t)tmp_wchar; message += (wchar_t)tmp_wchar;
} }
Player *player = m_env->getPlayer(pkt->getPeerId()); RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
if (player == NULL) { if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: " errorstream << "Server::ProcessData(): Canceling: "
"No player for peer_id=" << pkt->getPeerId() "No player for peer_id=" << pkt->getPeerId()
@ -1179,7 +1179,7 @@ void Server::handleCommand_Password(NetworkPacket* pkt)
newpwd += c; newpwd += c;
} }
Player *player = m_env->getPlayer(pkt->getPeerId()); RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
if (player == NULL) { if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: " errorstream << "Server::ProcessData(): Canceling: "
"No player for peer_id=" << pkt->getPeerId() "No player for peer_id=" << pkt->getPeerId()
@ -1255,7 +1255,7 @@ void Server::handleCommand_PlayerItem(NetworkPacket* pkt)
void Server::handleCommand_Respawn(NetworkPacket* pkt) void Server::handleCommand_Respawn(NetworkPacket* pkt)
{ {
Player *player = m_env->getPlayer(pkt->getPeerId()); RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
if (player == NULL) { if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: " errorstream << "Server::ProcessData(): Canceling: "
"No player for peer_id=" << pkt->getPeerId() "No player for peer_id=" << pkt->getPeerId()

View File

@ -533,7 +533,7 @@ int ObjectRef::l_get_local_animation(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED NO_MAP_LOCK_REQUIRED
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); RemotePlayer *player = getplayer(ref);
if (player == NULL) if (player == NULL)
return 0; return 0;
@ -584,7 +584,7 @@ int ObjectRef::l_get_eye_offset(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); RemotePlayer *player = getplayer(ref);
if (player == NULL) if (player == NULL)
return 0; return 0;
// Do it // Do it
@ -762,7 +762,7 @@ int ObjectRef::l_is_player(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); RemotePlayer *player = getplayer(ref);
lua_pushboolean(L, (player != NULL)); lua_pushboolean(L, (player != NULL));
return 1; return 1;
} }
@ -973,7 +973,7 @@ int ObjectRef::l_is_player_connected(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); RemotePlayer *player = getplayer(ref);
lua_pushboolean(L, (player != NULL && player->peer_id != 0)); lua_pushboolean(L, (player != NULL && player->peer_id != 0));
return 1; return 1;
} }
@ -983,7 +983,7 @@ int ObjectRef::l_get_player_name(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); RemotePlayer *player = getplayer(ref);
if (player == NULL) { if (player == NULL) {
lua_pushlstring(L, "", 0); lua_pushlstring(L, "", 0);
return 1; return 1;
@ -998,7 +998,7 @@ int ObjectRef::l_get_player_velocity(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); RemotePlayer *player = getplayer(ref);
if (player == NULL) { if (player == NULL) {
lua_pushnil(L); lua_pushnil(L);
return 1; return 1;
@ -1013,7 +1013,7 @@ int ObjectRef::l_get_look_dir(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); RemotePlayer *player = getplayer(ref);
if (player == NULL) return 0; if (player == NULL) return 0;
// Do it // Do it
float pitch = player->getRadPitchDep(); float pitch = player->getRadPitchDep();
@ -1033,7 +1033,7 @@ int ObjectRef::l_get_look_pitch(lua_State *L)
"Deprecated call to get_look_pitch, use get_look_vertical instead"); "Deprecated call to get_look_pitch, use get_look_vertical instead");
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); RemotePlayer *player = getplayer(ref);
if (player == NULL) return 0; if (player == NULL) return 0;
// Do it // Do it
lua_pushnumber(L, player->getRadPitchDep()); lua_pushnumber(L, player->getRadPitchDep());
@ -1050,7 +1050,7 @@ int ObjectRef::l_get_look_yaw(lua_State *L)
"Deprecated call to get_look_yaw, use get_look_horizontal instead"); "Deprecated call to get_look_yaw, use get_look_horizontal instead");
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); RemotePlayer *player = getplayer(ref);
if (player == NULL) return 0; if (player == NULL) return 0;
// Do it // Do it
lua_pushnumber(L, player->getRadYawDep()); lua_pushnumber(L, player->getRadYawDep());
@ -1062,7 +1062,7 @@ int ObjectRef::l_get_look_vertical(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); RemotePlayer *player = getplayer(ref);
if (player == NULL) return 0; if (player == NULL) return 0;
// Do it // Do it
lua_pushnumber(L, player->getRadPitch()); lua_pushnumber(L, player->getRadPitch());
@ -1074,7 +1074,7 @@ int ObjectRef::l_get_look_horizontal(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); RemotePlayer *player = getplayer(ref);
if (player == NULL) return 0; if (player == NULL) return 0;
// Do it // Do it
lua_pushnumber(L, player->getRadYaw()); lua_pushnumber(L, player->getRadYaw());
@ -1179,7 +1179,7 @@ int ObjectRef::l_set_inventory_formspec(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); RemotePlayer *player = getplayer(ref);
if (player == NULL) return 0; if (player == NULL) return 0;
std::string formspec = luaL_checkstring(L, 2); std::string formspec = luaL_checkstring(L, 2);
@ -1194,7 +1194,7 @@ int ObjectRef::l_get_inventory_formspec(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); RemotePlayer *player = getplayer(ref);
if (player == NULL) return 0; if (player == NULL) return 0;
std::string formspec = player->inventory_formspec; std::string formspec = player->inventory_formspec;
@ -1207,7 +1207,7 @@ int ObjectRef::l_get_player_control(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); RemotePlayer *player = getplayer(ref);
if (player == NULL) { if (player == NULL) {
lua_pushlstring(L, "", 0); lua_pushlstring(L, "", 0);
return 1; return 1;
@ -1241,7 +1241,7 @@ int ObjectRef::l_get_player_control_bits(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); RemotePlayer *player = getplayer(ref);
if (player == NULL) { if (player == NULL) {
lua_pushlstring(L, "", 0); lua_pushlstring(L, "", 0);
return 1; return 1;
@ -1416,7 +1416,7 @@ int ObjectRef::l_hud_get(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); RemotePlayer *player = getplayer(ref);
if (player == NULL) if (player == NULL)
return 0; return 0;
@ -1493,7 +1493,7 @@ int ObjectRef::l_hud_get_flags(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); RemotePlayer *player = getplayer(ref);
if (player == NULL) if (player == NULL)
return 0; return 0;
@ -1649,7 +1649,7 @@ int ObjectRef::l_get_sky(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); RemotePlayer *player = getplayer(ref);
if (player == NULL) if (player == NULL)
return 0; return 0;
video::SColor bgcolor(255, 255, 255, 255); video::SColor bgcolor(255, 255, 255, 255);

View File

@ -692,7 +692,7 @@ void Server::AsyncRunStep(bool initial_step)
if (client->getState() < CS_DefinitionsSent) if (client->getState() < CS_DefinitionsSent)
continue; continue;
Player *player = m_env->getPlayer(client->peer_id); RemotePlayer *player = m_env->getPlayer(client->peer_id);
if (player == NULL) { if (player == NULL) {
// This can happen if the client timeouts somehow // This can happen if the client timeouts somehow
/*warningstream<<FUNCTION_NAME<<": Client " /*warningstream<<FUNCTION_NAME<<": Client "
@ -1857,7 +1857,7 @@ void Server::SendPlayerBreath(u16 peer_id)
void Server::SendMovePlayer(u16 peer_id) void Server::SendMovePlayer(u16 peer_id)
{ {
DSTACK(FUNCTION_NAME); DSTACK(FUNCTION_NAME);
Player *player = m_env->getPlayer(peer_id); RemotePlayer *player = m_env->getPlayer(peer_id);
assert(player); assert(player);
NetworkPacket pkt(TOCLIENT_MOVE_PLAYER, sizeof(v3f) + sizeof(f32) * 2, peer_id); NetworkPacket pkt(TOCLIENT_MOVE_PLAYER, sizeof(v3f) + sizeof(f32) * 2, peer_id);
@ -1896,7 +1896,7 @@ void Server::SendEyeOffset(u16 peer_id, v3f first, v3f third)
} }
void Server::SendPlayerPrivileges(u16 peer_id) void Server::SendPlayerPrivileges(u16 peer_id)
{ {
Player *player = m_env->getPlayer(peer_id); RemotePlayer *player = m_env->getPlayer(peer_id);
assert(player); assert(player);
if(player->peer_id == PEER_ID_INEXISTENT) if(player->peer_id == PEER_ID_INEXISTENT)
return; return;
@ -1917,7 +1917,7 @@ void Server::SendPlayerPrivileges(u16 peer_id)
void Server::SendPlayerInventoryFormspec(u16 peer_id) void Server::SendPlayerInventoryFormspec(u16 peer_id)
{ {
Player *player = m_env->getPlayer(peer_id); RemotePlayer *player = m_env->getPlayer(peer_id);
assert(player); assert(player);
if(player->peer_id == PEER_ID_INEXISTENT) if(player->peer_id == PEER_ID_INEXISTENT)
return; return;
@ -1962,7 +1962,7 @@ s32 Server::playSound(const SimpleSoundSpec &spec,
std::vector<u16> dst_clients; std::vector<u16> dst_clients;
if(params.to_player != "") if(params.to_player != "")
{ {
Player *player = m_env->getPlayer(params.to_player.c_str()); RemotePlayer *player = m_env->getPlayer(params.to_player.c_str());
if(!player){ if(!player){
infostream<<"Server::playSound: Player \""<<params.to_player infostream<<"Server::playSound: Player \""<<params.to_player
<<"\" not found"<<std::endl; <<"\" not found"<<std::endl;
@ -1978,13 +1978,12 @@ s32 Server::playSound(const SimpleSoundSpec &spec,
else { else {
std::vector<u16> clients = m_clients.getClientIDs(); std::vector<u16> clients = m_clients.getClientIDs();
for(std::vector<u16>::iterator for (std::vector<u16>::iterator i = clients.begin(); i != clients.end(); ++i) {
i = clients.begin(); i != clients.end(); ++i) { RemotePlayer *player = m_env->getPlayer(*i);
Player *player = m_env->getPlayer(*i); if (!player)
if(!player)
continue; continue;
if(pos_exists) { if (pos_exists) {
if(player->getPosition().getDistanceFrom(pos) > if(player->getPosition().getDistanceFrom(pos) >
params.max_hear_distance) params.max_hear_distance)
continue; continue;
@ -2048,7 +2047,7 @@ void Server::sendRemoveNode(v3s16 p, u16 ignore_id,
i != clients.end(); ++i) { i != clients.end(); ++i) {
if (far_players) { if (far_players) {
// Get player // Get player
if(Player *player = m_env->getPlayer(*i)) { if (RemotePlayer *player = m_env->getPlayer(*i)) {
// If player is far away, only set modified blocks not sent // If player is far away, only set modified blocks not sent
v3f player_pos = player->getPosition(); v3f player_pos = player->getPosition();
if(player_pos.getDistanceFrom(p_f) > maxd) { if(player_pos.getDistanceFrom(p_f) > maxd) {
@ -2076,7 +2075,7 @@ void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id,
if(far_players) { if(far_players) {
// Get player // Get player
if(Player *player = m_env->getPlayer(*i)) { if (RemotePlayer *player = m_env->getPlayer(*i)) {
// If player is far away, only set modified blocks not sent // If player is far away, only set modified blocks not sent
v3f player_pos = player->getPosition(); v3f player_pos = player->getPosition();
if(player_pos.getDistanceFrom(p_f) > maxd) { if(player_pos.getDistanceFrom(p_f) > maxd) {
@ -2661,8 +2660,8 @@ void Server::DeleteClient(u16 peer_id, ClientDeletionReason reason)
for(std::vector<u16>::iterator i = clients.begin(); for(std::vector<u16>::iterator i = clients.begin();
i != clients.end(); ++i) { i != clients.end(); ++i) {
// Get player // Get player
Player *player = m_env->getPlayer(*i); RemotePlayer *player = m_env->getPlayer(*i);
if(!player) if (!player)
continue; continue;
// Get name of player // Get name of player
@ -2842,8 +2841,8 @@ RemoteClient* Server::getClientNoEx(u16 peer_id, ClientState state_min)
std::string Server::getPlayerName(u16 peer_id) std::string Server::getPlayerName(u16 peer_id)
{ {
Player *player = m_env->getPlayer(peer_id); RemotePlayer *player = m_env->getPlayer(peer_id);
if(player == NULL) if (player == NULL)
return "[id="+itos(peer_id)+"]"; return "[id="+itos(peer_id)+"]";
return player->getName(); return player->getName();
} }
@ -2870,13 +2869,12 @@ std::wstring Server::getStatusString()
bool first = true; bool first = true;
os<<L", clients={"; os<<L", clients={";
std::vector<u16> clients = m_clients.getClientIDs(); std::vector<u16> clients = m_clients.getClientIDs();
for(std::vector<u16>::iterator i = clients.begin(); for (std::vector<u16>::iterator i = clients.begin(); i != clients.end(); ++i) {
i != clients.end(); ++i) {
// Get player // Get player
Player *player = m_env->getPlayer(*i); RemotePlayer *player = m_env->getPlayer(*i);
// Get name of player // Get name of player
std::wstring name = L"unknown"; std::wstring name = L"unknown";
if(player != NULL) if (player != NULL)
name = narrow_to_wide(player->getName()); name = narrow_to_wide(player->getName());
// Add name to information string // Add name to information string
if(!first) if(!first)
@ -2912,12 +2910,11 @@ void Server::reportPrivsModified(const std::string &name)
std::vector<u16> clients = m_clients.getClientIDs(); std::vector<u16> clients = m_clients.getClientIDs();
for(std::vector<u16>::iterator i = clients.begin(); for(std::vector<u16>::iterator i = clients.begin();
i != clients.end(); ++i) { i != clients.end(); ++i) {
Player *player = m_env->getPlayer(*i); RemotePlayer *player = m_env->getPlayer(*i);
reportPrivsModified(player->getName()); reportPrivsModified(player->getName());
} }
} else { } else {
RemotePlayer *player = RemotePlayer *player = m_env->getPlayer(name.c_str());
dynamic_cast<RemotePlayer *>(m_env->getPlayer(name.c_str()));
if (!player) if (!player)
return; return;
SendPlayerPrivileges(player->peer_id); SendPlayerPrivileges(player->peer_id);
@ -2932,8 +2929,8 @@ void Server::reportPrivsModified(const std::string &name)
void Server::reportInventoryFormspecModified(const std::string &name) void Server::reportInventoryFormspecModified(const std::string &name)
{ {
Player *player = m_env->getPlayer(name.c_str()); RemotePlayer *player = m_env->getPlayer(name.c_str());
if(!player) if (!player)
return; return;
SendPlayerInventoryFormspec(player->peer_id); SendPlayerInventoryFormspec(player->peer_id);
} }
@ -2963,7 +2960,7 @@ void Server::notifyPlayer(const char *name, const std::wstring &msg)
m_admin_chat->outgoing_queue.push_back(new ChatEventChat("", msg)); m_admin_chat->outgoing_queue.push_back(new ChatEventChat("", msg));
} }
Player *player = m_env->getPlayer(name); RemotePlayer *player = m_env->getPlayer(name);
if (!player) { if (!player) {
return; return;
} }
@ -2981,7 +2978,7 @@ bool Server::showFormspec(const char *playername, const std::string &formspec,
if (!m_env) if (!m_env)
return false; return false;
Player *player = m_env->getPlayer(playername); RemotePlayer *player = m_env->getPlayer(playername);
if (!player) if (!player)
return false; return false;
@ -3152,7 +3149,7 @@ void Server::spawnParticle(const std::string &playername, v3f pos,
u16 peer_id = PEER_ID_INEXISTENT; u16 peer_id = PEER_ID_INEXISTENT;
if (playername != "") { if (playername != "") {
Player* player = m_env->getPlayer(playername.c_str()); RemotePlayer* player = m_env->getPlayer(playername.c_str());
if (!player) if (!player)
return; return;
peer_id = player->peer_id; peer_id = player->peer_id;
@ -3176,7 +3173,7 @@ u32 Server::addParticleSpawner(u16 amount, float spawntime,
u16 peer_id = PEER_ID_INEXISTENT; u16 peer_id = PEER_ID_INEXISTENT;
if (playername != "") { if (playername != "") {
Player* player = m_env->getPlayer(playername.c_str()); RemotePlayer* player = m_env->getPlayer(playername.c_str());
if (!player) if (!player)
return -1; return -1;
peer_id = player->peer_id; peer_id = player->peer_id;
@ -3199,7 +3196,7 @@ void Server::deleteParticleSpawner(const std::string &playername, u32 id)
u16 peer_id = PEER_ID_INEXISTENT; u16 peer_id = PEER_ID_INEXISTENT;
if (playername != "") { if (playername != "") {
Player* player = m_env->getPlayer(playername.c_str()); RemotePlayer* player = m_env->getPlayer(playername.c_str());
if (!player) if (!player)
return; return;
peer_id = player->peer_id; peer_id = player->peer_id;