3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "environment.h"
23 Environment::Environment(Map *map, std::ostream &dout):
27 m_daynight_ratio = 0.2;
30 Environment::~Environment()
33 for(core::list<Player*>::Iterator i = m_players.begin();
34 i != m_players.end(); i++)
39 // The map is removed by the SceneManager
44 void Environment::step(float dtime)
46 DSTACK(__FUNCTION_NAME);
50 //TimeTaker maptimerupdatetimer("m_map->timerUpdate()", g_device);
52 m_map->timerUpdate(dtime);
53 //maptimerupdatetimer.stop();
56 Get the highest speed some player is going
58 //TimeTaker playerspeed("playerspeed", g_device);
60 f32 maximum_player_speed = 0.001; // just some small value
61 for(core::list<Player*>::Iterator i = m_players.begin();
62 i != m_players.end(); i++)
64 f32 speed = (*i)->getSpeed().getLength();
65 if(speed > maximum_player_speed)
66 maximum_player_speed = speed;
71 Maximum position increment
73 //f32 position_max_increment = 0.05*BS;
74 f32 position_max_increment = 0.1*BS;
76 // Maximum time increment (for collision detection etc)
77 // time = distance / speed
78 f32 dtime_max_increment = position_max_increment / maximum_player_speed;
79 // Maximum time increment is 10ms or lower
80 if(dtime_max_increment > 0.01)
81 dtime_max_increment = 0.01;
83 //TimeTaker playerupdate("playerupdate", g_device);
86 Stuff that has a maximum time increment
88 // Don't allow overly huge dtime
98 if(dtime > dtime_max_increment)
99 dtime_part = dtime_max_increment;
107 for(core::list<Player*>::Iterator i = m_players.begin();
108 i != m_players.end(); i++)
112 v3f playerpos = player->getPosition();
114 // Apply physics to local player
115 bool free_move = g_settings.getBool("free_move");
116 if(player->isLocal() && free_move == false)
118 // Apply gravity to local player
119 v3f speed = player->getSpeed();
120 if(player->swimming_up == false)
121 speed.Y -= 9.81 * BS * dtime_part * 2;
124 Apply water resistance
126 if(player->in_water_stable || player->in_water)
128 f32 max_down = 2.0*BS;
129 if(speed.Y < -max_down) speed.Y = -max_down;
132 if(speed.getLength() > max)
134 speed = speed / speed.getLength() * max;
138 player->setSpeed(speed);
143 For local player, this also calculates collision detection.
145 player->move(dtime_part, *m_map, position_max_increment);
148 Update lighting on remote players on client
150 u8 light = LIGHT_MAX;
153 v3s16 p = floatToInt(playerpos + v3f(0,BS/4,0));
154 MapNode n = m_map->getNode(p);
155 light = n.getLightBlend(m_daynight_ratio);
157 catch(InvalidPositionException &e) {}
158 player->updateLight(light);
161 Add footsteps to grass
163 if(g_settings.getBool("footprints"))
165 // Get node that is at BS/4 under player
166 v3s16 bottompos = floatToInt(playerpos + v3f(0,-BS/4,0));
168 MapNode n = m_map->getNode(bottompos);
169 if(n.d == CONTENT_GRASS)
171 n.d = CONTENT_GRASS_FOOTSTEPS;
172 m_map->setNode(bottompos, n);
174 // Update mesh on client
175 if(m_map->mapType() == MAPTYPE_CLIENT)
177 v3s16 p_blocks = getNodeBlockPos(bottompos);
178 MapBlock *b = m_map->getBlockNoCreate(p_blocks);
179 b->updateMesh(m_daynight_ratio);
184 catch(InvalidPositionException &e)
190 while(dtime > 0.001);
192 //std::cout<<"Looped "<<loopcount<<" times."<<std::endl;
195 Map & Environment::getMap()
200 void Environment::addPlayer(Player *player)
202 DSTACK(__FUNCTION_NAME);
204 Check that only one local player exists and peer_ids are unique.
205 Also check that names are unique.
206 Exception: there can be multiple players with peer_id=0
210 It is a failure if player is local and there already is a local
213 assert(!(player->isLocal() == true && getLocalPlayer() != NULL));
215 // If peer id is non-zero, it has to be unique.
216 if(player->peer_id != 0)
217 assert(getPlayer(player->peer_id) == NULL);
218 // Name has to be unique.
219 assert(getPlayer(player->getName()) == NULL);
221 m_players.push_back(player);
224 void Environment::removePlayer(u16 peer_id)
226 DSTACK(__FUNCTION_NAME);
228 for(core::list<Player*>::Iterator i = m_players.begin();
229 i != m_players.end(); i++)
232 if(player->peer_id != peer_id)
237 // See if there is an another one
238 // (shouldn't be, but just to be sure)
244 LocalPlayer * Environment::getLocalPlayer()
246 for(core::list<Player*>::Iterator i = m_players.begin();
247 i != m_players.end(); i++)
250 if(player->isLocal())
251 return (LocalPlayer*)player;
257 Player * Environment::getPlayer(u16 peer_id)
259 for(core::list<Player*>::Iterator i = m_players.begin();
260 i != m_players.end(); i++)
263 if(player->peer_id == peer_id)
269 Player * Environment::getPlayer(const char *name)
271 for(core::list<Player*>::Iterator i = m_players.begin();
272 i != m_players.end(); i++)
275 if(strcmp(player->getName(), name) == 0)
281 core::list<Player*> Environment::getPlayers()
286 core::list<Player*> Environment::getPlayers(bool ignore_disconnected)
288 core::list<Player*> newlist;
289 for(core::list<Player*>::Iterator
290 i = m_players.begin();
291 i != m_players.end(); i++)
295 if(ignore_disconnected)
297 // Ignore disconnected players
298 if(player->peer_id == 0)
302 newlist.push_back(player);
307 void Environment::printPlayers(std::ostream &o)
309 o<<"Players in environment:"<<std::endl;
310 for(core::list<Player*>::Iterator i = m_players.begin();
311 i != m_players.end(); i++)
314 o<<"Player peer_id="<<player->peer_id<<std::endl;
318 void Environment::serializePlayers(const std::string &savedir)
320 std::string players_path = savedir + "/players";
321 fs::CreateDir(players_path);
323 core::map<Player*, bool> saved_players;
325 std::vector<fs::DirListNode> player_files = fs::GetDirListing(players_path);
326 for(u32 i=0; i<player_files.size(); i++)
328 if(player_files[i].dir)
331 // Full path to this file
332 std::string path = players_path + "/" + player_files[i].name;
334 //dstream<<"Checking player file "<<path<<std::endl;
336 // Load player to see what is its name
337 ServerRemotePlayer testplayer;
339 // Open file and deserialize
340 std::ifstream is(path.c_str(), std::ios_base::binary);
341 if(is.good() == false)
343 dstream<<"Failed to read "<<path<<std::endl;
346 testplayer.deSerialize(is);
349 //dstream<<"Loaded test player with name "<<testplayer.getName()<<std::endl;
351 // Search for the player
352 std::string playername = testplayer.getName();
353 Player *player = getPlayer(playername.c_str());
356 dstream<<"Didn't find matching player, ignoring file "<<path<<std::endl;
360 //dstream<<"Found matching player, overwriting."<<std::endl;
362 // OK, found. Save player there.
364 // Open file and serialize
365 std::ofstream os(path.c_str(), std::ios_base::binary);
366 if(os.good() == false)
368 dstream<<"Failed to overwrite "<<path<<std::endl;
371 player->serialize(os);
372 saved_players.insert(player, true);
376 for(core::list<Player*>::Iterator i = m_players.begin();
377 i != m_players.end(); i++)
380 if(saved_players.find(player) != NULL)
382 /*dstream<<"Player "<<player->getName()
383 <<" was already saved."<<std::endl;*/
386 std::string playername = player->getName();
387 // Don't save unnamed player
390 //dstream<<"Not saving unnamed player."<<std::endl;
396 if(string_allowed(playername, PLAYERNAME_ALLOWED_CHARS) == false)
397 playername = "player";
398 std::string path = players_path + "/" + playername;
400 for(u32 i=0; i<1000; i++)
402 if(fs::PathExists(path) == false)
407 path = players_path + "/" + playername + itos(i);
411 dstream<<"WARNING: Didn't find free file for player"<<std::endl;
416 /*dstream<<"Saving player "<<player->getName()<<" to "
418 // Open file and serialize
419 std::ofstream os(path.c_str(), std::ios_base::binary);
420 if(os.good() == false)
422 dstream<<"WARNING: Failed to overwrite "<<path<<std::endl;
425 player->serialize(os);
426 saved_players.insert(player, true);
430 //dstream<<"Saved "<<saved_players.size()<<" players."<<std::endl;
433 void Environment::deSerializePlayers(const std::string &savedir)
435 std::string players_path = savedir + "/players";
437 core::map<Player*, bool> saved_players;
439 std::vector<fs::DirListNode> player_files = fs::GetDirListing(players_path);
440 for(u32 i=0; i<player_files.size(); i++)
442 if(player_files[i].dir)
445 // Full path to this file
446 std::string path = players_path + "/" + player_files[i].name;
448 dstream<<"Checking player file "<<path<<std::endl;
450 // Load player to see what is its name
451 ServerRemotePlayer testplayer;
453 // Open file and deserialize
454 std::ifstream is(path.c_str(), std::ios_base::binary);
455 if(is.good() == false)
457 dstream<<"Failed to read "<<path<<std::endl;
460 testplayer.deSerialize(is);
463 dstream<<"Loaded test player with name "<<testplayer.getName()<<std::endl;
465 // Search for the player
466 std::string playername = testplayer.getName();
467 Player *player = getPlayer(playername.c_str());
468 bool newplayer = false;
471 dstream<<"Is a new player"<<std::endl;
472 player = new ServerRemotePlayer();
478 dstream<<"Reading player "<<testplayer.getName()<<" from "
480 // Open file and deserialize
481 std::ifstream is(path.c_str(), std::ios_base::binary);
482 if(is.good() == false)
484 dstream<<"Failed to read "<<path<<std::endl;
487 player->deSerialize(is);
496 void Environment::updateMeshes(v3s16 blockpos)
498 m_map->updateMeshes(blockpos, m_daynight_ratio);
501 void Environment::expireMeshes(bool only_daynight_diffed)
503 m_map->expireMeshes(only_daynight_diffed);
507 void Environment::setDayNightRatio(u32 r)
509 m_daynight_ratio = r;
512 u32 Environment::getDayNightRatio()
514 return m_daynight_ratio;