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;
70 // Maximum time increment (for collision detection etc)
71 // Allow 0.1 blocks per increment
72 // time = distance / speed
73 f32 dtime_max_increment = 0.1*BS / maximum_player_speed;
74 // Maximum time increment is 10ms or lower
75 if(dtime_max_increment > 0.01)
76 dtime_max_increment = 0.01;
78 //TimeTaker playerupdate("playerupdate", g_device);
81 Stuff that has a maximum time increment
83 // Don't allow overly huge dtime
93 if(dtime > dtime_max_increment)
94 dtime_part = dtime_max_increment;
102 for(core::list<Player*>::Iterator i = m_players.begin();
103 i != m_players.end(); i++)
107 v3f playerpos = player->getPosition();
109 // Apply physics to local player
110 bool free_move = g_settings.getBool("free_move");
111 if(player->isLocal() && free_move == false)
113 // Apply gravity to local player
114 v3f speed = player->getSpeed();
115 if(player->swimming_up == false)
116 speed.Y -= 9.81 * BS * dtime_part * 2;
119 Apply water resistance
121 if(player->in_water_stable)
123 f32 max_down = 1.5*BS;
124 if(speed.Y < -max_down) speed.Y = -max_down;
127 if(speed.getLength() > max)
129 speed = speed / speed.getLength() * max;
133 player->setSpeed(speed);
138 For local player, this also calculates collision detection.
140 player->move(dtime_part, *m_map);
143 Update lighting on remote players on client
145 u8 light = LIGHT_MAX;
148 v3s16 p = floatToInt(playerpos + v3f(0,BS/4,0));
149 MapNode n = m_map->getNode(p);
150 light = n.getLightBlend(m_daynight_ratio);
152 catch(InvalidPositionException &e) {}
153 player->updateLight(light);
156 Add footsteps to grass
158 if(g_settings.getBool("footprints"))
160 // Get node that is at BS/4 under player
161 v3s16 bottompos = floatToInt(playerpos + v3f(0,-BS/4,0));
163 MapNode n = m_map->getNode(bottompos);
164 if(n.d == CONTENT_GRASS)
166 n.d = CONTENT_GRASS_FOOTSTEPS;
167 m_map->setNode(bottompos, n);
169 // Update mesh on client
170 if(m_map->mapType() == MAPTYPE_CLIENT)
172 v3s16 p_blocks = getNodeBlockPos(bottompos);
173 MapBlock *b = m_map->getBlockNoCreate(p_blocks);
174 b->updateMesh(m_daynight_ratio);
179 catch(InvalidPositionException &e)
185 while(dtime > 0.001);
187 //std::cout<<"Looped "<<loopcount<<" times."<<std::endl;
190 Map & Environment::getMap()
195 void Environment::addPlayer(Player *player)
197 DSTACK(__FUNCTION_NAME);
199 Check that only one local player exists and peer_ids are unique.
200 Also check that names are unique.
201 Exception: there can be multiple players with peer_id=0
205 It is a failure if player is local and there already is a local
208 assert(!(player->isLocal() == true && getLocalPlayer() != NULL));
210 // If peer id is non-zero, it has to be unique.
211 if(player->peer_id != 0)
212 assert(getPlayer(player->peer_id) == NULL);
213 // Name has to be unique.
214 assert(getPlayer(player->getName()) == NULL);
216 m_players.push_back(player);
219 void Environment::removePlayer(u16 peer_id)
221 DSTACK(__FUNCTION_NAME);
223 for(core::list<Player*>::Iterator i = m_players.begin();
224 i != m_players.end(); i++)
227 if(player->peer_id != peer_id)
232 // See if there is an another one
233 // (shouldn't be, but just to be sure)
239 LocalPlayer * Environment::getLocalPlayer()
241 for(core::list<Player*>::Iterator i = m_players.begin();
242 i != m_players.end(); i++)
245 if(player->isLocal())
246 return (LocalPlayer*)player;
252 Player * Environment::getPlayer(u16 peer_id)
254 for(core::list<Player*>::Iterator i = m_players.begin();
255 i != m_players.end(); i++)
258 if(player->peer_id == peer_id)
264 Player * Environment::getPlayer(const char *name)
266 for(core::list<Player*>::Iterator i = m_players.begin();
267 i != m_players.end(); i++)
270 if(strcmp(player->getName(), name) == 0)
276 core::list<Player*> Environment::getPlayers()
281 core::list<Player*> Environment::getPlayers(bool ignore_disconnected)
283 core::list<Player*> newlist;
284 for(core::list<Player*>::Iterator
285 i = m_players.begin();
286 i != m_players.end(); i++)
290 if(ignore_disconnected)
292 // Ignore disconnected players
293 if(player->peer_id == 0)
297 newlist.push_back(player);
302 void Environment::printPlayers(std::ostream &o)
304 o<<"Players in environment:"<<std::endl;
305 for(core::list<Player*>::Iterator i = m_players.begin();
306 i != m_players.end(); i++)
309 o<<"Player peer_id="<<player->peer_id<<std::endl;
313 void Environment::serializePlayers(const std::string &savedir)
315 std::string players_path = savedir + "/players";
316 fs::CreateDir(players_path);
318 core::map<Player*, bool> saved_players;
320 std::vector<fs::DirListNode> player_files = fs::GetDirListing(players_path);
321 for(u32 i=0; i<player_files.size(); i++)
323 if(player_files[i].dir)
326 // Full path to this file
327 std::string path = players_path + "/" + player_files[i].name;
329 //dstream<<"Checking player file "<<path<<std::endl;
331 // Load player to see what is its name
332 ServerRemotePlayer testplayer;
334 // Open file and deserialize
335 std::ifstream is(path.c_str(), std::ios_base::binary);
336 if(is.good() == false)
338 dstream<<"Failed to read "<<path<<std::endl;
341 testplayer.deSerialize(is);
344 //dstream<<"Loaded test player with name "<<testplayer.getName()<<std::endl;
346 // Search for the player
347 std::string playername = testplayer.getName();
348 Player *player = getPlayer(playername.c_str());
351 dstream<<"Didn't find matching player, ignoring file "<<path<<std::endl;
355 //dstream<<"Found matching player, overwriting."<<std::endl;
357 // OK, found. Save player there.
359 // Open file and serialize
360 std::ofstream os(path.c_str(), std::ios_base::binary);
361 if(os.good() == false)
363 dstream<<"Failed to overwrite "<<path<<std::endl;
366 player->serialize(os);
367 saved_players.insert(player, true);
371 for(core::list<Player*>::Iterator i = m_players.begin();
372 i != m_players.end(); i++)
375 if(saved_players.find(player) != NULL)
377 /*dstream<<"Player "<<player->getName()
378 <<" was already saved."<<std::endl;*/
381 std::string playername = player->getName();
382 // Don't save unnamed player
385 //dstream<<"Not saving unnamed player."<<std::endl;
391 if(string_allowed(playername, PLAYERNAME_ALLOWED_CHARS) == false)
392 playername = "player";
393 std::string path = players_path + "/" + playername;
395 for(u32 i=0; i<1000; i++)
397 if(fs::PathExists(path) == false)
402 path = players_path + "/" + playername + itos(i);
406 dstream<<"WARNING: Didn't find free file for player"<<std::endl;
411 /*dstream<<"Saving player "<<player->getName()<<" to "
413 // Open file and serialize
414 std::ofstream os(path.c_str(), std::ios_base::binary);
415 if(os.good() == false)
417 dstream<<"WARNING: Failed to overwrite "<<path<<std::endl;
420 player->serialize(os);
421 saved_players.insert(player, true);
425 //dstream<<"Saved "<<saved_players.size()<<" players."<<std::endl;
428 void Environment::deSerializePlayers(const std::string &savedir)
430 std::string players_path = savedir + "/players";
432 core::map<Player*, bool> saved_players;
434 std::vector<fs::DirListNode> player_files = fs::GetDirListing(players_path);
435 for(u32 i=0; i<player_files.size(); i++)
437 if(player_files[i].dir)
440 // Full path to this file
441 std::string path = players_path + "/" + player_files[i].name;
443 dstream<<"Checking player file "<<path<<std::endl;
445 // Load player to see what is its name
446 ServerRemotePlayer testplayer;
448 // Open file and deserialize
449 std::ifstream is(path.c_str(), std::ios_base::binary);
450 if(is.good() == false)
452 dstream<<"Failed to read "<<path<<std::endl;
455 testplayer.deSerialize(is);
458 dstream<<"Loaded test player with name "<<testplayer.getName()<<std::endl;
460 // Search for the player
461 std::string playername = testplayer.getName();
462 Player *player = getPlayer(playername.c_str());
463 bool newplayer = false;
466 dstream<<"Is a new player"<<std::endl;
467 player = new ServerRemotePlayer();
473 dstream<<"Reading player "<<testplayer.getName()<<" from "
475 // Open file and deserialize
476 std::ifstream is(path.c_str(), std::ios_base::binary);
477 if(is.good() == false)
479 dstream<<"Failed to read "<<path<<std::endl;
482 player->deSerialize(is);
491 void Environment::updateMeshes(v3s16 blockpos)
493 m_map->updateMeshes(blockpos, m_daynight_ratio);
496 void Environment::expireMeshes(bool only_daynight_diffed)
498 m_map->expireMeshes(only_daynight_diffed);
502 void Environment::setDayNightRatio(u32 r)
504 m_daynight_ratio = r;
507 u32 Environment::getDayNightRatio()
509 return m_daynight_ratio;