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 if(player->swimming_up == false)
116 speed.Y -= 9.81 * BS * dtime_part * 2;
119 Apply water resistance
123 f32 max_down = 1.0*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 // Get node that is at BS/4 under player
159 v3s16 bottompos = floatToInt(playerpos + v3f(0,-BS/4,0));
161 MapNode n = m_map->getNode(bottompos);
162 if(n.d == CONTENT_GRASS)
164 n.d = CONTENT_GRASS_FOOTSTEPS;
165 m_map->setNode(bottompos, n);
167 // Update mesh on client
168 if(m_map->mapType() == MAPTYPE_CLIENT)
170 v3s16 p_blocks = getNodeBlockPos(bottompos);
171 MapBlock *b = m_map->getBlockNoCreate(p_blocks);
172 b->updateMesh(m_daynight_ratio);
177 catch(InvalidPositionException &e)
182 while(dtime > 0.001);
184 //std::cout<<"Looped "<<loopcount<<" times."<<std::endl;
187 Map & Environment::getMap()
192 void Environment::addPlayer(Player *player)
194 DSTACK(__FUNCTION_NAME);
196 Check that only one local player exists and peer_ids are unique.
197 Also check that names are unique.
198 Exception: there can be multiple players with peer_id=0
202 It is a failure if player is local and there already is a local
205 assert(!(player->isLocal() == true && getLocalPlayer() != NULL));
207 // If peer id is non-zero, it has to be unique.
208 if(player->peer_id != 0)
209 assert(getPlayer(player->peer_id) == NULL);
210 // Name has to be unique.
211 assert(getPlayer(player->getName()) == NULL);
213 m_players.push_back(player);
216 void Environment::removePlayer(u16 peer_id)
218 DSTACK(__FUNCTION_NAME);
220 for(core::list<Player*>::Iterator i = m_players.begin();
221 i != m_players.end(); i++)
224 if(player->peer_id != peer_id)
229 // See if there is an another one
230 // (shouldn't be, but just to be sure)
236 LocalPlayer * Environment::getLocalPlayer()
238 for(core::list<Player*>::Iterator i = m_players.begin();
239 i != m_players.end(); i++)
242 if(player->isLocal())
243 return (LocalPlayer*)player;
249 Player * Environment::getPlayer(u16 peer_id)
251 for(core::list<Player*>::Iterator i = m_players.begin();
252 i != m_players.end(); i++)
255 if(player->peer_id == peer_id)
261 Player * Environment::getPlayer(const char *name)
263 for(core::list<Player*>::Iterator i = m_players.begin();
264 i != m_players.end(); i++)
267 if(strcmp(player->getName(), name) == 0)
273 core::list<Player*> Environment::getPlayers()
278 core::list<Player*> Environment::getPlayers(bool ignore_disconnected)
280 core::list<Player*> newlist;
281 for(core::list<Player*>::Iterator
282 i = m_players.begin();
283 i != m_players.end(); i++)
287 if(ignore_disconnected)
289 // Ignore disconnected players
290 if(player->peer_id == 0)
294 newlist.push_back(player);
299 void Environment::printPlayers(std::ostream &o)
301 o<<"Players in environment:"<<std::endl;
302 for(core::list<Player*>::Iterator i = m_players.begin();
303 i != m_players.end(); i++)
306 o<<"Player peer_id="<<player->peer_id<<std::endl;
310 void Environment::serializePlayers(const std::string &savedir)
312 std::string players_path = savedir + "/players";
313 fs::CreateDir(players_path);
315 core::map<Player*, bool> saved_players;
317 std::vector<fs::DirListNode> player_files = fs::GetDirListing(players_path);
318 for(u32 i=0; i<player_files.size(); i++)
320 if(player_files[i].dir)
323 // Full path to this file
324 std::string path = players_path + "/" + player_files[i].name;
326 //dstream<<"Checking player file "<<path<<std::endl;
328 // Load player to see what is its name
329 ServerRemotePlayer testplayer;
331 // Open file and deserialize
332 std::ifstream is(path.c_str(), std::ios_base::binary);
333 if(is.good() == false)
335 dstream<<"Failed to read "<<path<<std::endl;
338 testplayer.deSerialize(is);
341 //dstream<<"Loaded test player with name "<<testplayer.getName()<<std::endl;
343 // Search for the player
344 std::string playername = testplayer.getName();
345 Player *player = getPlayer(playername.c_str());
348 dstream<<"Didn't find matching player, ignoring file "<<path<<std::endl;
352 //dstream<<"Found matching player, overwriting."<<std::endl;
354 // OK, found. Save player there.
356 // Open file and serialize
357 std::ofstream os(path.c_str(), std::ios_base::binary);
358 if(os.good() == false)
360 dstream<<"Failed to overwrite "<<path<<std::endl;
363 player->serialize(os);
364 saved_players.insert(player, true);
368 for(core::list<Player*>::Iterator i = m_players.begin();
369 i != m_players.end(); i++)
372 if(saved_players.find(player) != NULL)
374 /*dstream<<"Player "<<player->getName()
375 <<" was already saved."<<std::endl;*/
378 std::string playername = player->getName();
379 // Don't save unnamed player
382 //dstream<<"Not saving unnamed player."<<std::endl;
388 if(string_allowed(playername, PLAYERNAME_ALLOWED_CHARS) == false)
389 playername = "player";
390 std::string path = players_path + "/" + playername;
392 for(u32 i=0; i<1000; i++)
394 if(fs::PathExists(path) == false)
399 path = players_path + "/" + playername + itos(i);
403 dstream<<"WARNING: Didn't find free file for player"<<std::endl;
408 /*dstream<<"Saving player "<<player->getName()<<" to "
410 // Open file and serialize
411 std::ofstream os(path.c_str(), std::ios_base::binary);
412 if(os.good() == false)
414 dstream<<"WARNING: Failed to overwrite "<<path<<std::endl;
417 player->serialize(os);
418 saved_players.insert(player, true);
422 //dstream<<"Saved "<<saved_players.size()<<" players."<<std::endl;
425 void Environment::deSerializePlayers(const std::string &savedir)
427 std::string players_path = savedir + "/players";
429 core::map<Player*, bool> saved_players;
431 std::vector<fs::DirListNode> player_files = fs::GetDirListing(players_path);
432 for(u32 i=0; i<player_files.size(); i++)
434 if(player_files[i].dir)
437 // Full path to this file
438 std::string path = players_path + "/" + player_files[i].name;
440 dstream<<"Checking player file "<<path<<std::endl;
442 // Load player to see what is its name
443 ServerRemotePlayer testplayer;
445 // Open file and deserialize
446 std::ifstream is(path.c_str(), std::ios_base::binary);
447 if(is.good() == false)
449 dstream<<"Failed to read "<<path<<std::endl;
452 testplayer.deSerialize(is);
455 dstream<<"Loaded test player with name "<<testplayer.getName()<<std::endl;
457 // Search for the player
458 std::string playername = testplayer.getName();
459 Player *player = getPlayer(playername.c_str());
460 bool newplayer = false;
463 dstream<<"Is a new player"<<std::endl;
464 player = new ServerRemotePlayer();
470 dstream<<"Reading player "<<testplayer.getName()<<" from "
472 // Open file and deserialize
473 std::ifstream is(path.c_str(), std::ios_base::binary);
474 if(is.good() == false)
476 dstream<<"Failed to read "<<path<<std::endl;
479 player->deSerialize(is);
488 void Environment::updateMeshes(v3s16 blockpos)
490 m_map->updateMeshes(blockpos, m_daynight_ratio);
493 void Environment::expireMeshes(bool only_daynight_diffed)
495 m_map->expireMeshes(only_daynight_diffed);
499 void Environment::setDayNightRatio(u32 r)
501 m_daynight_ratio = r;
504 u32 Environment::getDayNightRatio()
506 return m_daynight_ratio;