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 haxmode = g_settings.getBool("haxmode");
111 if(player->isLocal() && haxmode == false)
113 // Apply gravity to local player
114 v3f speed = player->getSpeed();
115 speed.Y -= 9.81 * BS * dtime_part * 2;
118 Apply water resistance
122 f32 max_down = 1.0*BS;
123 if(speed.Y < -max_down) speed.Y = -max_down;
126 if(speed.getLength() > max)
128 speed = speed / speed.getLength() * max;
132 player->setSpeed(speed);
137 For local player, this also calculates collision detection.
139 player->move(dtime_part, *m_map);
142 Update lighting on remote players on client
144 u8 light = LIGHT_MAX;
147 v3s16 p = floatToInt(playerpos + v3f(0,BS/4,0));
148 MapNode n = m_map->getNode(p);
149 light = n.getLightBlend(m_daynight_ratio);
151 catch(InvalidPositionException &e) {}
152 player->updateLight(light);
155 Add footsteps to grass
157 // Get node that is at BS/4 under player
158 v3s16 bottompos = floatToInt(playerpos + v3f(0,-BS/4,0));
160 MapNode n = m_map->getNode(bottompos);
161 if(n.d == CONTENT_GRASS)
163 n.d = CONTENT_GRASS_FOOTSTEPS;
164 m_map->setNode(bottompos, n);
166 // Update mesh on client
167 if(m_map->mapType() == MAPTYPE_CLIENT)
169 v3s16 p_blocks = getNodeBlockPos(bottompos);
170 MapBlock *b = m_map->getBlockNoCreate(p_blocks);
171 b->updateMesh(m_daynight_ratio);
176 catch(InvalidPositionException &e)
181 while(dtime > 0.001);
183 //std::cout<<"Looped "<<loopcount<<" times."<<std::endl;
186 Map & Environment::getMap()
191 void Environment::addPlayer(Player *player)
193 DSTACK(__FUNCTION_NAME);
195 Check that only one local player exists and peer_ids are unique.
196 Also check that names are unique.
197 Exception: there can be multiple players with peer_id=0
201 It is a failure if player is local and there already is a local
204 assert(!(player->isLocal() == true && getLocalPlayer() != NULL));
206 // If peer id is non-zero, it has to be unique.
207 if(player->peer_id != 0)
208 assert(getPlayer(player->peer_id) == NULL);
209 // Name has to be unique.
210 assert(getPlayer(player->getName()) == NULL);
212 m_players.push_back(player);
215 void Environment::removePlayer(u16 peer_id)
217 DSTACK(__FUNCTION_NAME);
219 for(core::list<Player*>::Iterator i = m_players.begin();
220 i != m_players.end(); i++)
223 if(player->peer_id != peer_id)
228 // See if there is an another one
229 // (shouldn't be, but just to be sure)
235 LocalPlayer * Environment::getLocalPlayer()
237 for(core::list<Player*>::Iterator i = m_players.begin();
238 i != m_players.end(); i++)
241 if(player->isLocal())
242 return (LocalPlayer*)player;
248 Player * Environment::getPlayer(u16 peer_id)
250 for(core::list<Player*>::Iterator i = m_players.begin();
251 i != m_players.end(); i++)
254 if(player->peer_id == peer_id)
260 Player * Environment::getPlayer(const char *name)
262 for(core::list<Player*>::Iterator i = m_players.begin();
263 i != m_players.end(); i++)
266 if(strcmp(player->getName(), name) == 0)
272 core::list<Player*> Environment::getPlayers()
277 core::list<Player*> Environment::getPlayers(bool ignore_disconnected)
279 core::list<Player*> newlist;
280 for(core::list<Player*>::Iterator
281 i = m_players.begin();
282 i != m_players.end(); i++)
286 if(ignore_disconnected)
288 // Ignore disconnected players
289 if(player->peer_id == 0)
293 newlist.push_back(player);
298 void Environment::printPlayers(std::ostream &o)
300 o<<"Players in environment:"<<std::endl;
301 for(core::list<Player*>::Iterator i = m_players.begin();
302 i != m_players.end(); i++)
305 o<<"Player peer_id="<<player->peer_id<<std::endl;
309 void Environment::serializePlayers(const std::string &savedir)
311 std::string players_path = savedir + "/players";
312 fs::CreateDir(players_path);
314 core::map<Player*, bool> saved_players;
316 std::vector<fs::DirListNode> player_files = fs::GetDirListing(players_path);
317 for(u32 i=0; i<player_files.size(); i++)
319 if(player_files[i].dir)
322 // Full path to this file
323 std::string path = players_path + "/" + player_files[i].name;
325 dstream<<"Checking player file "<<path<<std::endl;
327 // Load player to see what is its name
328 ServerRemotePlayer testplayer;
330 // Open file and deserialize
331 std::ifstream is(path.c_str(), std::ios_base::binary);
332 if(is.good() == false)
334 dstream<<"Failed to read "<<path<<std::endl;
337 testplayer.deSerialize(is);
340 dstream<<"Loaded test player with name "<<testplayer.getName()<<std::endl;
342 // Search for the player
343 std::string playername = testplayer.getName();
344 Player *player = getPlayer(playername.c_str());
347 dstream<<"Didn't find matching player, ignoring file."<<std::endl;
351 dstream<<"Found matching player, overwriting."<<std::endl;
353 // OK, found. Save player there.
355 // Open file and serialize
356 std::ofstream os(path.c_str(), std::ios_base::binary);
357 if(os.good() == false)
359 dstream<<"Failed to overwrite "<<path<<std::endl;
362 player->serialize(os);
363 saved_players.insert(player, true);
367 for(core::list<Player*>::Iterator i = m_players.begin();
368 i != m_players.end(); i++)
371 if(saved_players.find(player) != NULL)
373 dstream<<"Player "<<player->getName()
374 <<" was already saved."<<std::endl;
377 std::string playername = player->getName();
378 // Don't save unnamed player
381 dstream<<"Not saving unnamed player."<<std::endl;
387 if(string_allowed(playername, PLAYERNAME_ALLOWED_CHARS) == false)
388 playername = "player";
389 std::string path = players_path + "/" + playername;
391 for(u32 i=0; i<1000; i++)
393 if(fs::PathExists(path) == false)
398 path = players_path + "/" + playername + itos(i);
402 dstream<<"Didn't find free file for player"<<std::endl;
407 dstream<<"Saving player "<<player->getName()<<" to "
409 // Open file and serialize
410 std::ofstream os(path.c_str(), std::ios_base::binary);
411 if(os.good() == false)
413 dstream<<"Failed to overwrite "<<path<<std::endl;
416 player->serialize(os);
417 saved_players.insert(player, true);
422 void Environment::deSerializePlayers(const std::string &savedir)
424 std::string players_path = savedir + "/players";
426 core::map<Player*, bool> saved_players;
428 std::vector<fs::DirListNode> player_files = fs::GetDirListing(players_path);
429 for(u32 i=0; i<player_files.size(); i++)
431 if(player_files[i].dir)
434 // Full path to this file
435 std::string path = players_path + "/" + player_files[i].name;
437 dstream<<"Checking player file "<<path<<std::endl;
439 // Load player to see what is its name
440 ServerRemotePlayer testplayer;
442 // Open file and deserialize
443 std::ifstream is(path.c_str(), std::ios_base::binary);
444 if(is.good() == false)
446 dstream<<"Failed to read "<<path<<std::endl;
449 testplayer.deSerialize(is);
452 dstream<<"Loaded test player with name "<<testplayer.getName()<<std::endl;
454 // Search for the player
455 std::string playername = testplayer.getName();
456 Player *player = getPlayer(playername.c_str());
457 bool newplayer = false;
460 dstream<<"Is a new player"<<std::endl;
461 player = new ServerRemotePlayer();
467 dstream<<"Reading player "<<testplayer.getName()<<" from "
469 // Open file and deserialize
470 std::ifstream is(path.c_str(), std::ios_base::binary);
471 if(is.good() == false)
473 dstream<<"Failed to read "<<path<<std::endl;
476 player->deSerialize(is);
485 void Environment::updateMeshes(v3s16 blockpos)
487 m_map->updateMeshes(blockpos, m_daynight_ratio);
490 void Environment::expireMeshes(bool only_daynight_diffed)
492 m_map->expireMeshes(only_daynight_diffed);
496 void Environment::setDayNightRatio(u32 r)
498 m_daynight_ratio = r;
501 u32 Environment::getDayNightRatio()
503 return m_daynight_ratio;