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"
21 #include "main.h" // g_device for timing debug
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 if(player->isLocal())
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);
193 //Check that only one local player exists and peer_ids are unique
195 assert(player->isLocal() == false || getLocalPlayer() == NULL);
197 assert(getPlayer(player->peer_id) == NULL);
198 m_players.push_back(player);
201 void Environment::removePlayer(u16 peer_id)
203 DSTACK(__FUNCTION_NAME);
205 for(core::list<Player*>::Iterator i = m_players.begin();
206 i != m_players.end(); i++)
209 if(player->peer_id != peer_id)
214 // See if there is an another one
215 // (shouldn't be, but just to be sure)
221 LocalPlayer * Environment::getLocalPlayer()
223 for(core::list<Player*>::Iterator i = m_players.begin();
224 i != m_players.end(); i++)
227 if(player->isLocal())
228 return (LocalPlayer*)player;
234 Player * Environment::getPlayer(u16 peer_id)
236 for(core::list<Player*>::Iterator i = m_players.begin();
237 i != m_players.end(); i++)
240 if(player->peer_id == peer_id)
246 core::list<Player*> Environment::getPlayers()
251 void Environment::printPlayers(std::ostream &o)
253 o<<"Players in environment:"<<std::endl;
254 for(core::list<Player*>::Iterator i = m_players.begin();
255 i != m_players.end(); i++)
258 o<<"Player peer_id="<<player->peer_id<<std::endl;
263 void Environment::updateMeshes(v3s16 blockpos)
265 m_map->updateMeshes(blockpos, m_daynight_ratio);
268 void Environment::expireMeshes(bool only_daynight_diffed)
270 m_map->expireMeshes(only_daynight_diffed);
274 void Environment::setDayNightRatio(u32 r)
276 m_daynight_ratio = r;
279 u32 Environment::getDayNightRatio()
281 return m_daynight_ratio;