]> git.lizzy.rs Git - minetest.git/blob - src/environment.cpp
Commented out some debug output about saving players on server.
[minetest.git] / src / environment.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "environment.h"
21 #include "filesys.h"
22
23 Environment::Environment(Map *map, std::ostream &dout):
24                 m_dout(dout)
25 {
26         m_map = map;
27         m_daynight_ratio = 0.2;
28 }
29
30 Environment::~Environment()
31 {
32         // Deallocate players
33         for(core::list<Player*>::Iterator i = m_players.begin();
34                         i != m_players.end(); i++)
35         {
36                 delete (*i);
37         }
38         
39         // The map is removed by the SceneManager
40         m_map->drop();
41         //delete m_map;
42 }
43
44 void Environment::step(float dtime)
45 {
46         DSTACK(__FUNCTION_NAME);
47         /*
48                 Run Map's timers
49         */
50         //TimeTaker maptimerupdatetimer("m_map->timerUpdate()", g_device);
51         // 0ms
52         m_map->timerUpdate(dtime);
53         //maptimerupdatetimer.stop();
54
55         /*
56                 Get the highest speed some player is going
57         */
58         //TimeTaker playerspeed("playerspeed", g_device);
59         // 0ms
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++)
63         {
64                 f32 speed = (*i)->getSpeed().getLength();
65                 if(speed > maximum_player_speed)
66                         maximum_player_speed = speed;
67         }
68         //playerspeed.stop();
69         
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;
77         
78         //TimeTaker playerupdate("playerupdate", g_device);
79         
80         /*
81                 Stuff that has a maximum time increment
82         */
83         // Don't allow overly huge dtime
84         if(dtime > 0.5)
85                 dtime = 0.5;
86
87         u32 loopcount = 0;
88         do
89         {
90                 loopcount++;
91
92                 f32 dtime_part;
93                 if(dtime > dtime_max_increment)
94                         dtime_part = dtime_max_increment;
95                 else
96                         dtime_part = dtime;
97                 dtime -= dtime_part;
98                 
99                 /*
100                         Handle players
101                 */
102                 for(core::list<Player*>::Iterator i = m_players.begin();
103                                 i != m_players.end(); i++)
104                 {
105                         Player *player = *i;
106
107                         v3f playerpos = player->getPosition();
108                         
109                         // Apply physics to local player
110                         bool haxmode = g_settings.getBool("haxmode");
111                         if(player->isLocal() && haxmode == false)
112                         {
113                                 // Apply gravity to local player
114                                 v3f speed = player->getSpeed();
115                                 speed.Y -= 9.81 * BS * dtime_part * 2;
116
117                                 /*
118                                         Apply water resistance
119                                 */
120                                 if(player->in_water)
121                                 {
122                                         f32 max_down = 1.0*BS;
123                                         if(speed.Y < -max_down) speed.Y = -max_down;
124
125                                         f32 max = 2.5*BS;
126                                         if(speed.getLength() > max)
127                                         {
128                                                 speed = speed / speed.getLength() * max;
129                                         }
130                                 }
131
132                                 player->setSpeed(speed);
133                         }
134
135                         /*
136                                 Move the player.
137                                 For local player, this also calculates collision detection.
138                         */
139                         player->move(dtime_part, *m_map);
140                         
141                         /*
142                                 Update lighting on remote players on client
143                         */
144                         u8 light = LIGHT_MAX;
145                         try{
146                                 // Get node at feet
147                                 v3s16 p = floatToInt(playerpos + v3f(0,BS/4,0));
148                                 MapNode n = m_map->getNode(p);
149                                 light = n.getLightBlend(m_daynight_ratio);
150                         }
151                         catch(InvalidPositionException &e) {}
152                         player->updateLight(light);
153
154                         /*
155                                 Add footsteps to grass
156                         */
157                         // Get node that is at BS/4 under player
158                         v3s16 bottompos = floatToInt(playerpos + v3f(0,-BS/4,0));
159                         try{
160                                 MapNode n = m_map->getNode(bottompos);
161                                 if(n.d == CONTENT_GRASS)
162                                 {
163                                         n.d = CONTENT_GRASS_FOOTSTEPS;
164                                         m_map->setNode(bottompos, n);
165 #ifndef SERVER
166                                         // Update mesh on client
167                                         if(m_map->mapType() == MAPTYPE_CLIENT)
168                                         {
169                                                 v3s16 p_blocks = getNodeBlockPos(bottompos);
170                                                 MapBlock *b = m_map->getBlockNoCreate(p_blocks);
171                                                 b->updateMesh(m_daynight_ratio);
172                                         }
173 #endif
174                                 }
175                         }
176                         catch(InvalidPositionException &e)
177                         {
178                         }
179                 }
180         }
181         while(dtime > 0.001);
182         
183         //std::cout<<"Looped "<<loopcount<<" times."<<std::endl;
184 }
185
186 Map & Environment::getMap()
187 {
188         return *m_map;
189 }
190
191 void Environment::addPlayer(Player *player)
192 {
193         DSTACK(__FUNCTION_NAME);
194         /*
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
198         */
199 #ifndef SERVER
200         /*
201                 It is a failure if player is local and there already is a local
202                 player
203         */
204         assert(!(player->isLocal() == true && getLocalPlayer() != NULL));
205 #endif
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);
211         // Add.
212         m_players.push_back(player);
213 }
214
215 void Environment::removePlayer(u16 peer_id)
216 {
217         DSTACK(__FUNCTION_NAME);
218 re_search:
219         for(core::list<Player*>::Iterator i = m_players.begin();
220                         i != m_players.end(); i++)
221         {
222                 Player *player = *i;
223                 if(player->peer_id != peer_id)
224                         continue;
225                 
226                 delete player;
227                 m_players.erase(i);
228                 // See if there is an another one
229                 // (shouldn't be, but just to be sure)
230                 goto re_search;
231         }
232 }
233
234 #ifndef SERVER
235 LocalPlayer * Environment::getLocalPlayer()
236 {
237         for(core::list<Player*>::Iterator i = m_players.begin();
238                         i != m_players.end(); i++)
239         {
240                 Player *player = *i;
241                 if(player->isLocal())
242                         return (LocalPlayer*)player;
243         }
244         return NULL;
245 }
246 #endif
247
248 Player * Environment::getPlayer(u16 peer_id)
249 {
250         for(core::list<Player*>::Iterator i = m_players.begin();
251                         i != m_players.end(); i++)
252         {
253                 Player *player = *i;
254                 if(player->peer_id == peer_id)
255                         return player;
256         }
257         return NULL;
258 }
259
260 Player * Environment::getPlayer(const char *name)
261 {
262         for(core::list<Player*>::Iterator i = m_players.begin();
263                         i != m_players.end(); i++)
264         {
265                 Player *player = *i;
266                 if(strcmp(player->getName(), name) == 0)
267                         return player;
268         }
269         return NULL;
270 }
271
272 core::list<Player*> Environment::getPlayers()
273 {
274         return m_players;
275 }
276
277 core::list<Player*> Environment::getPlayers(bool ignore_disconnected)
278 {
279         core::list<Player*> newlist;
280         for(core::list<Player*>::Iterator
281                         i = m_players.begin();
282                         i != m_players.end(); i++)
283         {
284                 Player *player = *i;
285                 
286                 if(ignore_disconnected)
287                 {
288                         // Ignore disconnected players
289                         if(player->peer_id == 0)
290                                 continue;
291                 }
292
293                 newlist.push_back(player);
294         }
295         return newlist;
296 }
297
298 void Environment::printPlayers(std::ostream &o)
299 {
300         o<<"Players in environment:"<<std::endl;
301         for(core::list<Player*>::Iterator i = m_players.begin();
302                         i != m_players.end(); i++)
303         {
304                 Player *player = *i;
305                 o<<"Player peer_id="<<player->peer_id<<std::endl;
306         }
307 }
308
309 void Environment::serializePlayers(const std::string &savedir)
310 {
311         std::string players_path = savedir + "/players";
312         fs::CreateDir(players_path);
313
314         core::map<Player*, bool> saved_players;
315
316         std::vector<fs::DirListNode> player_files = fs::GetDirListing(players_path);
317         for(u32 i=0; i<player_files.size(); i++)
318         {
319                 if(player_files[i].dir)
320                         continue;
321                 
322                 // Full path to this file
323                 std::string path = players_path + "/" + player_files[i].name;
324
325                 //dstream<<"Checking player file "<<path<<std::endl;
326
327                 // Load player to see what is its name
328                 ServerRemotePlayer testplayer;
329                 {
330                         // Open file and deserialize
331                         std::ifstream is(path.c_str(), std::ios_base::binary);
332                         if(is.good() == false)
333                         {
334                                 dstream<<"Failed to read "<<path<<std::endl;
335                                 continue;
336                         }
337                         testplayer.deSerialize(is);
338                 }
339
340                 //dstream<<"Loaded test player with name "<<testplayer.getName()<<std::endl;
341                 
342                 // Search for the player
343                 std::string playername = testplayer.getName();
344                 Player *player = getPlayer(playername.c_str());
345                 if(player == NULL)
346                 {
347                         dstream<<"Didn't find matching player, ignoring file "<<path<<std::endl;
348                         continue;
349                 }
350
351                 //dstream<<"Found matching player, overwriting."<<std::endl;
352
353                 // OK, found. Save player there.
354                 {
355                         // Open file and serialize
356                         std::ofstream os(path.c_str(), std::ios_base::binary);
357                         if(os.good() == false)
358                         {
359                                 dstream<<"Failed to overwrite "<<path<<std::endl;
360                                 continue;
361                         }
362                         player->serialize(os);
363                         saved_players.insert(player, true);
364                 }
365         }
366
367         for(core::list<Player*>::Iterator i = m_players.begin();
368                         i != m_players.end(); i++)
369         {
370                 Player *player = *i;
371                 if(saved_players.find(player) != NULL)
372                 {
373                         /*dstream<<"Player "<<player->getName()
374                                         <<" was already saved."<<std::endl;*/
375                         continue;
376                 }
377                 std::string playername = player->getName();
378                 // Don't save unnamed player
379                 if(playername == "")
380                 {
381                         //dstream<<"Not saving unnamed player."<<std::endl;
382                         continue;
383                 }
384                 /*
385                         Find a sane filename
386                 */
387                 if(string_allowed(playername, PLAYERNAME_ALLOWED_CHARS) == false)
388                         playername = "player";
389                 std::string path = players_path + "/" + playername;
390                 bool found = false;
391                 for(u32 i=0; i<1000; i++)
392                 {
393                         if(fs::PathExists(path) == false)
394                         {
395                                 found = true;
396                                 break;
397                         }
398                         path = players_path + "/" + playername + itos(i);
399                 }
400                 if(found == false)
401                 {
402                         dstream<<"WARNING: Didn't find free file for player"<<std::endl;
403                         continue;
404                 }
405
406                 {
407                         /*dstream<<"Saving player "<<player->getName()<<" to "
408                                         <<path<<std::endl;*/
409                         // Open file and serialize
410                         std::ofstream os(path.c_str(), std::ios_base::binary);
411                         if(os.good() == false)
412                         {
413                                 dstream<<"WARNING: Failed to overwrite "<<path<<std::endl;
414                                 continue;
415                         }
416                         player->serialize(os);
417                         saved_players.insert(player, true);
418                 }
419         }
420
421         //dstream<<"Saved "<<saved_players.size()<<" players."<<std::endl;
422 }
423
424 void Environment::deSerializePlayers(const std::string &savedir)
425 {
426         std::string players_path = savedir + "/players";
427
428         core::map<Player*, bool> saved_players;
429
430         std::vector<fs::DirListNode> player_files = fs::GetDirListing(players_path);
431         for(u32 i=0; i<player_files.size(); i++)
432         {
433                 if(player_files[i].dir)
434                         continue;
435                 
436                 // Full path to this file
437                 std::string path = players_path + "/" + player_files[i].name;
438
439                 dstream<<"Checking player file "<<path<<std::endl;
440
441                 // Load player to see what is its name
442                 ServerRemotePlayer testplayer;
443                 {
444                         // Open file and deserialize
445                         std::ifstream is(path.c_str(), std::ios_base::binary);
446                         if(is.good() == false)
447                         {
448                                 dstream<<"Failed to read "<<path<<std::endl;
449                                 continue;
450                         }
451                         testplayer.deSerialize(is);
452                 }
453
454                 dstream<<"Loaded test player with name "<<testplayer.getName()<<std::endl;
455                 
456                 // Search for the player
457                 std::string playername = testplayer.getName();
458                 Player *player = getPlayer(playername.c_str());
459                 bool newplayer = false;
460                 if(player == NULL)
461                 {
462                         dstream<<"Is a new player"<<std::endl;
463                         player = new ServerRemotePlayer();
464                         newplayer = true;
465                 }
466
467                 // Load player
468                 {
469                         dstream<<"Reading player "<<testplayer.getName()<<" from "
470                                         <<path<<std::endl;
471                         // Open file and deserialize
472                         std::ifstream is(path.c_str(), std::ios_base::binary);
473                         if(is.good() == false)
474                         {
475                                 dstream<<"Failed to read "<<path<<std::endl;
476                                 continue;
477                         }
478                         player->deSerialize(is);
479                 }
480
481                 if(newplayer)
482                         addPlayer(player);
483         }
484 }
485
486 #ifndef SERVER
487 void Environment::updateMeshes(v3s16 blockpos)
488 {
489         m_map->updateMeshes(blockpos, m_daynight_ratio);
490 }
491
492 void Environment::expireMeshes(bool only_daynight_diffed)
493 {
494         m_map->expireMeshes(only_daynight_diffed);
495 }
496 #endif
497
498 void Environment::setDayNightRatio(u32 r)
499 {
500         m_daynight_ratio = r;
501 }
502
503 u32 Environment::getDayNightRatio()
504 {
505         return m_daynight_ratio;
506 }
507