/*
Minetest
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <set>
#include <list>
#include <map>
#include "environment.h"
#include "filesys.h"
#include "porting.h"
#include "collision.h"
#include "content_mapnode.h"
#include "mapblock.h"
#include "serverobject.h"
#include "content_sao.h"
#include "mapgen.h"
#include "settings.h"
#include "log.h"
#include "profiler.h"
#include "scriptapi.h"
#include "nodedef.h"
#include "nodemetadata.h"
#include "main.h" // For g_settings, g_profiler
#include "gamedef.h"
#ifndef SERVER
#include "clientmap.h"
#include "localplayer.h"
#endif
#include "daynightratio.h"
#include "map.h"
#include "util/serialize.h"
#define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
Environment::Environment():
m_time_of_day(9000),
m_time_of_day_f(9000./24000),
m_time_of_day_speed(0),
m_time_counter(0)
{
}
Environment::~Environment()
{
// Deallocate players
for(std::list<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)
assert(getPlayer(player->peer_id) == NULL);
// Name has to be unique.
assert(getPlayer(player->getName()) == NULL);
// Add.
m_players.push_back(player);
}
void Environment::removePlayer(u16 peer_id)
{
DSTACK(__FUNCTION_NAME);
re_search:
for(std::list<Player*>::iterator i = m_players.begin();
i != m_players.end(); ++i)
{
Player *player = *i;
if(player->peer_id != peer_id)
continue;
delete player;
m_players.erase(i);
// See if there is an another one
// (shouldn't be, but just to be sure)
goto re_search;
}
}
Player * Environment::getPlayer(u16 peer_id)
{
for(std::list<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::list<Player*>::iterator i = m_players.begin();
i != m_players.end(); ++i)
{
Player *player = *i;
if(strcmp(player->getName(), name) == 0)
return player;
}
return NULL;
}
Player * Environment::getRandomConnectedPlayer()
{
std::list<Player*> connected_players = getPlayers(true);
u32 chosen_one = myrand() % connected_players.size();
u32 j = 0;
for(std::list<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::list<Player*> connected_players = getPlayers(true);
f32 nearest_d = 0;
Player *nearest_player = NULL;
for(std::list<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::list<Player*> Environment::getPlayers()
{
return m_players;
}
std::list<Player*> Environment::getPlayers(bool ignore_disconnected)
{
std::list<Player*> newlist;
for(std::list<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;
}
void Environment::printPlayers(std::ostream &o)
{
o<<"Players in environment:"<<std::endl;
for(std::list<Player*>::iterator i = m_players.begin();
i != m_players.end(); i++)
{
Player *player = *i;
o<<"Player peer_id="<<player->peer_id<<std::endl;
}
}
u32 Environment::getDayNightRatio()
{
bool smooth = (g_settings->getS32("enable_shaders") != 0);
return time_to_daynight_ratio(m_time_of_day_f*24000, smooth);
}
void Environment::stepTimeOfDay(float dtime)
{
m_time_counter += dtime;
f32 speed = m_time_of_day_speed * 24000./(24.*3600);
u32 units = (u32)(m_time_counter*speed);
m_time_counter -= (f32)units / speed;
bool sync_f = false;
if(units > 0){
// Sync at overflow
if(m_time_of_day + units >= 24000)
sync_f = true;
m_time_of_day = (m_time_of_day + units) % 24000;
if(sync_f)
m_time_of_day_f = (float)m_time_of_day / 24000.0;
}
if(!sync_f){
m_time_of_day_f += m_time_of_day_speed/24/3600*dtime;
if(m_time_of_day_f > 1.0)
m_time_of_day_f -= 1.0;
if(m_time_of_day_f < 0.0)
m_time_of_day_f += 1.0;
}
}
/*
ABMWithState
*/
|