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"
22 Environment::Environment(Map *map, std::ostream &dout):
26 m_daynight_ratio = 0.2;
29 Environment::~Environment()
32 for(core::list<Player*>::Iterator i = m_players.begin();
33 i != m_players.end(); i++)
38 // The map is removed by the SceneManager
43 void Environment::step(float dtime)
45 DSTACK(__FUNCTION_NAME);
49 //TimeTaker maptimerupdatetimer("m_map->timerUpdate()", g_device);
51 m_map->timerUpdate(dtime);
52 //maptimerupdatetimer.stop();
55 Get the highest speed some player is going
57 //TimeTaker playerspeed("playerspeed", g_device);
59 f32 maximum_player_speed = 0.001; // just some small value
60 for(core::list<Player*>::Iterator i = m_players.begin();
61 i != m_players.end(); i++)
63 f32 speed = (*i)->getSpeed().getLength();
64 if(speed > maximum_player_speed)
65 maximum_player_speed = speed;
69 // Maximum time increment (for collision detection etc)
70 // Allow 0.1 blocks per increment
71 // time = distance / speed
72 f32 dtime_max_increment = 0.1*BS / maximum_player_speed;
73 // Maximum time increment is 10ms or lower
74 if(dtime_max_increment > 0.01)
75 dtime_max_increment = 0.01;
77 //TimeTaker playerupdate("playerupdate", g_device);
80 Stuff that has a maximum time increment
82 // Don't allow overly huge dtime
92 if(dtime > dtime_max_increment)
93 dtime_part = dtime_max_increment;
101 for(core::list<Player*>::Iterator i = m_players.begin();
102 i != m_players.end(); i++)
106 v3f playerpos = player->getPosition();
108 // Apply physics to local player
109 bool haxmode = g_settings.getBool("haxmode");
110 if(player->isLocal() && haxmode == false)
112 // Apply gravity to local player
113 v3f speed = player->getSpeed();
114 speed.Y -= 9.81 * BS * dtime_part * 2;
117 Apply water resistance
121 f32 max_down = 1.0*BS;
122 if(speed.Y < -max_down) speed.Y = -max_down;
125 if(speed.getLength() > max)
127 speed = speed / speed.getLength() * max;
131 player->setSpeed(speed);
136 For local player, this also calculates collision detection.
138 player->move(dtime_part, *m_map);
141 Update lighting on remote players on client
143 u8 light = LIGHT_MAX;
146 v3s16 p = floatToInt(playerpos + v3f(0,BS/4,0));
147 MapNode n = m_map->getNode(p);
148 light = n.getLightBlend(m_daynight_ratio);
150 catch(InvalidPositionException &e) {}
151 player->updateLight(light);
154 Add footsteps to grass
156 // Get node that is at BS/4 under player
157 v3s16 bottompos = floatToInt(playerpos + v3f(0,-BS/4,0));
159 MapNode n = m_map->getNode(bottompos);
160 if(n.d == CONTENT_GRASS)
162 n.d = CONTENT_GRASS_FOOTSTEPS;
163 m_map->setNode(bottompos, n);
165 // Update mesh on client
166 if(m_map->mapType() == MAPTYPE_CLIENT)
168 v3s16 p_blocks = getNodeBlockPos(bottompos);
169 MapBlock *b = m_map->getBlockNoCreate(p_blocks);
170 b->updateMesh(m_daynight_ratio);
175 catch(InvalidPositionException &e)
180 while(dtime > 0.001);
182 //std::cout<<"Looped "<<loopcount<<" times."<<std::endl;
185 Map & Environment::getMap()
190 void Environment::addPlayer(Player *player)
192 DSTACK(__FUNCTION_NAME);
194 Check that only one local player exists and peer_ids are unique.
195 Exception: there can be multiple players with peer_id=0
199 It is a failure if player is local and there already is a local
202 assert(!(player->isLocal() == true && getLocalPlayer() != NULL));
204 if(player->peer_id != 0)
205 assert(getPlayer(player->peer_id) == NULL);
206 m_players.push_back(player);
209 void Environment::removePlayer(u16 peer_id)
211 DSTACK(__FUNCTION_NAME);
213 for(core::list<Player*>::Iterator i = m_players.begin();
214 i != m_players.end(); i++)
217 if(player->peer_id != peer_id)
222 // See if there is an another one
223 // (shouldn't be, but just to be sure)
229 LocalPlayer * Environment::getLocalPlayer()
231 for(core::list<Player*>::Iterator i = m_players.begin();
232 i != m_players.end(); i++)
235 if(player->isLocal())
236 return (LocalPlayer*)player;
242 Player * Environment::getPlayer(u16 peer_id)
244 for(core::list<Player*>::Iterator i = m_players.begin();
245 i != m_players.end(); i++)
248 if(player->peer_id == peer_id)
254 Player * Environment::getPlayer(const char *name)
256 for(core::list<Player*>::Iterator i = m_players.begin();
257 i != m_players.end(); i++)
260 if(strcmp(player->getName(), name) == 0)
266 core::list<Player*> Environment::getPlayers()
271 core::list<Player*> Environment::getPlayers(bool ignore_disconnected)
273 core::list<Player*> newlist;
274 for(core::list<Player*>::Iterator
275 i = m_players.begin();
276 i != m_players.end(); i++)
280 if(ignore_disconnected)
282 // Ignore disconnected players
283 if(player->peer_id == 0)
287 newlist.push_back(player);
292 void Environment::printPlayers(std::ostream &o)
294 o<<"Players in environment:"<<std::endl;
295 for(core::list<Player*>::Iterator i = m_players.begin();
296 i != m_players.end(); i++)
299 o<<"Player peer_id="<<player->peer_id<<std::endl;
304 void Environment::updateMeshes(v3s16 blockpos)
306 m_map->updateMeshes(blockpos, m_daynight_ratio);
309 void Environment::expireMeshes(bool only_daynight_diffed)
311 m_map->expireMeshes(only_daynight_diffed);
315 void Environment::setDayNightRatio(u32 r)
317 m_daynight_ratio = r;
320 u32 Environment::getDayNightRatio()
322 return m_daynight_ratio;