Environment::~Environment()
{
- // Deallocate players
- for(std::vector<Player*>::iterator i = m_players.begin();
- i != m_players.end(); ++i) {
- delete (*i);
- }
-}
-
-void Environment::addPlayer(Player *player)
-{
- DSTACK(FUNCTION_NAME);
- /*
- Check that peer_ids are unique.
- Also check that names are unique.
- Exception: there can be multiple players with peer_id=0
- */
- // If peer id is non-zero, it has to be unique.
- if(player->peer_id != 0)
- FATAL_ERROR_IF(getPlayer(player->peer_id) != NULL, "Peer id not unique");
- // Name has to be unique.
- FATAL_ERROR_IF(getPlayer(player->getName()) != NULL, "Player name not unique");
- // Add.
- m_players.push_back(player);
-}
-
-void Environment::removePlayer(Player* player)
-{
- for (std::vector<Player*>::iterator it = m_players.begin();
- it != m_players.end(); ++it) {
- if ((*it) == player) {
- delete *it;
- m_players.erase(it);
- return;
- }
- }
-}
-
-Player *Environment::getPlayer(u16 peer_id)
-{
- for (std::vector<Player*>::iterator i = m_players.begin();
- i != m_players.end(); ++i) {
- Player *player = *i;
- if (player->peer_id == peer_id)
- return player;
- }
- return NULL;
-}
-
-Player *Environment::getPlayer(const char *name)
-{
- for (std::vector<Player*>::iterator i = m_players.begin();
- i != m_players.end(); ++i) {
- Player *player = *i;
- if(strcmp(player->getName(), name) == 0)
- return player;
- }
- return NULL;
}
u32 Environment::getDayNightRatio()
m_map->drop();
// Delete ActiveBlockModifiers
- for(std::vector<ABMWithState>::iterator
+ for (std::vector<ABMWithState>::iterator
i = m_abms.begin(); i != m_abms.end(); ++i){
delete i->abm;
}
+
+ // Deallocate players
+ for (std::vector<RemotePlayer *>::iterator i = m_players.begin();
+ i != m_players.end(); ++i) {
+ delete (*i);
+ }
}
Map & ServerEnvironment::getMap()
RemotePlayer *ServerEnvironment::getPlayer(const u16 peer_id)
{
- return dynamic_cast<RemotePlayer *>(Environment::getPlayer(peer_id));
+ for (std::vector<RemotePlayer *>::iterator i = m_players.begin();
+ i != m_players.end(); ++i) {
+ RemotePlayer *player = *i;
+ if (player->peer_id == peer_id)
+ return player;
+ }
+ return NULL;
}
RemotePlayer *ServerEnvironment::getPlayer(const char* name)
{
- return dynamic_cast<RemotePlayer *>(Environment::getPlayer(name));
+ for (std::vector<RemotePlayer *>::iterator i = m_players.begin();
+ i != m_players.end(); ++i) {
+ RemotePlayer *player = *i;
+ if (strcmp(player->getName(), name) == 0)
+ return player;
+ }
+ return NULL;
+}
+
+void ServerEnvironment::addPlayer(RemotePlayer *player)
+{
+ DSTACK(FUNCTION_NAME);
+ /*
+ Check that peer_ids are unique.
+ Also check that names are unique.
+ Exception: there can be multiple players with peer_id=0
+ */
+ // If peer id is non-zero, it has to be unique.
+ if (player->peer_id != 0)
+ FATAL_ERROR_IF(getPlayer(player->peer_id) != NULL, "Peer id not unique");
+ // Name has to be unique.
+ FATAL_ERROR_IF(getPlayer(player->getName()) != NULL, "Player name not unique");
+ // Add.
+ m_players.push_back(player);
+}
+
+void ServerEnvironment::removePlayer(RemotePlayer *player)
+{
+ for (std::vector<RemotePlayer *>::iterator it = m_players.begin();
+ it != m_players.end(); ++it) {
+ if ((*it) == player) {
+ delete *it;
+ m_players.erase(it);
+ return;
+ }
+ }
}
bool ServerEnvironment::line_of_sight(v3f pos1, v3f pos2, float stepsize, v3s16 *p)
void ServerEnvironment::kickAllPlayers(AccessDeniedCode reason,
const std::string &str_reason, bool reconnect)
{
- for (std::vector<Player*>::iterator it = m_players.begin();
+ for (std::vector<RemotePlayer *>::iterator it = m_players.begin();
it != m_players.end(); ++it) {
RemotePlayer *player = dynamic_cast<RemotePlayer *>(*it);
((Server*)m_gamedef)->DenyAccessVerCompliant(player->peer_id,
std::string players_path = m_path_world + DIR_DELIM "players";
fs::CreateDir(players_path);
- for (std::vector<Player*>::iterator it = m_players.begin();
+ for (std::vector<RemotePlayer *>::iterator it = m_players.begin();
it != m_players.end();
++it) {
RemotePlayer *player = static_cast<RemotePlayer*>(*it);
*/
{
ScopeProfiler sp(g_profiler, "SEnv: handle players avg", SPT_AVG);
- for (std::vector<Player*>::iterator i = m_players.begin();
+ for (std::vector<RemotePlayer *>::iterator i = m_players.begin();
i != m_players.end(); ++i) {
RemotePlayer *player = dynamic_cast<RemotePlayer *>(*i);
assert(player);
Get player block positions
*/
std::vector<v3s16> players_blockpos;
- for (std::vector<Player*>::iterator i = m_players.begin();
+ for (std::vector<RemotePlayer *>::iterator i = m_players.begin();
i != m_players.end(); ++i) {
RemotePlayer *player = dynamic_cast<RemotePlayer *>(*i);
assert(player);
ITextureSource *texturesource, IGameDef *gamedef,
IrrlichtDevice *irr):
m_map(map),
+ m_local_player(NULL),
m_smgr(smgr),
m_texturesource(texturesource),
m_gamedef(gamedef),
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(LocalPlayer *player)
+void ClientEnvironment::setLocalPlayer(LocalPlayer *player)
{
DSTACK(FUNCTION_NAME);
/*
It is a failure if already is a local player
*/
- FATAL_ERROR_IF(getLocalPlayer() != NULL,
- "Player is local but there is already a local player");
-
- Environment::addPlayer(player);
-}
+ FATAL_ERROR_IF(m_local_player != NULL,
+ "Local player already allocated");
-LocalPlayer *ClientEnvironment::getLocalPlayer()
-{
- for(std::vector<Player*>::iterator i = m_players.begin();
- i != m_players.end(); ++i) {
- Player *player = *i;
- if (player->isLocal())
- return (LocalPlayer*)player;
- }
- return NULL;
+ m_local_player = player;
}
void ClientEnvironment::step(float dtime)
}
}
- /*
- Stuff that can be done in an arbitarily large dtime
- */
- for (std::vector<Player*>::iterator i = m_players.begin();
- i != m_players.end(); ++i) {
- Player *player = *i;
- assert(player);
-
- /*
- Handle non-local players
- */
- if (!player->isLocal()) {
- // Move
- player->move(dtime, this, 100*BS);
-
- }
- }
-
// Update lighting on local player (used for wield item)
u32 day_night_ratio = getDayNightRatio();
{
virtual Map & getMap() = 0;
- virtual void addPlayer(Player *player);
- void removePlayer(Player *player);
-
u32 getDayNightRatio();
// 0-23999
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;
-
GenericAtomic<float> m_time_of_day_speed;
/*
void saveLoadedPlayers();
void savePlayer(RemotePlayer *player);
RemotePlayer *loadPlayer(const std::string &playername);
+ void addPlayer(RemotePlayer *player);
+ void removePlayer(RemotePlayer *player);
/*
Save and load time of day and game timer
// Can raise to high values like 15s with eg. map generation mods.
float m_max_lag_estimate;
+ // peer_ids in here should be unique, except that there may be many 0s
+ std::vector<RemotePlayer*> m_players;
+
// Particles
IntervalLimiter m_particle_management_interval;
UNORDERED_MAP<u32, float> m_particle_spawners;
void step(f32 dtime);
- virtual void addPlayer(LocalPlayer *player);
- LocalPlayer * getLocalPlayer();
+ virtual void setLocalPlayer(LocalPlayer *player);
+ LocalPlayer *getLocalPlayer() { return m_local_player; }
/*
ClientSimpleObjects
u16 attachement_parent_ids[USHRT_MAX + 1];
- std::list<std::string> getPlayerNames()
- { return m_player_names; }
- void addPlayerName(std::string name)
- { m_player_names.push_back(name); }
- void removePlayerName(std::string name)
- { m_player_names.remove(name); }
+ const std::list<std::string> &getPlayerNames() { return m_player_names; }
+ void addPlayerName(const std::string &name) { m_player_names.push_back(name); }
+ void removePlayerName(const std::string &name) { m_player_names.remove(name); }
void updateCameraOffset(v3s16 camera_offset)
{ m_camera_offset = camera_offset; }
v3s16 getCameraOffset() const { return m_camera_offset; }
-
- LocalPlayer *getPlayer(const u16 peer_id);
- LocalPlayer *getPlayer(const char* name);
-
private:
ClientMap *m_map;
+ LocalPlayer *m_local_player;
scene::ISceneManager *m_smgr;
ITextureSource *m_texturesource;
IGameDef *m_gamedef;