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 #include "collision.h"
25 Environment::Environment()
27 m_daynight_ratio = 0.5;
30 Environment::~Environment()
33 for(core::list<Player*>::Iterator i = m_players.begin();
34 i != m_players.end(); i++)
40 void Environment::addPlayer(Player *player)
42 DSTACK(__FUNCTION_NAME);
44 Check that peer_ids are unique.
45 Also check that names are unique.
46 Exception: there can be multiple players with peer_id=0
48 // If peer id is non-zero, it has to be unique.
49 if(player->peer_id != 0)
50 assert(getPlayer(player->peer_id) == NULL);
51 // Name has to be unique.
52 assert(getPlayer(player->getName()) == NULL);
54 m_players.push_back(player);
57 void Environment::removePlayer(u16 peer_id)
59 DSTACK(__FUNCTION_NAME);
61 for(core::list<Player*>::Iterator i = m_players.begin();
62 i != m_players.end(); i++)
65 if(player->peer_id != peer_id)
70 // See if there is an another one
71 // (shouldn't be, but just to be sure)
76 Player * Environment::getPlayer(u16 peer_id)
78 for(core::list<Player*>::Iterator i = m_players.begin();
79 i != m_players.end(); i++)
82 if(player->peer_id == peer_id)
88 Player * Environment::getPlayer(const char *name)
90 for(core::list<Player*>::Iterator i = m_players.begin();
91 i != m_players.end(); i++)
94 if(strcmp(player->getName(), name) == 0)
100 Player * Environment::getRandomConnectedPlayer()
102 core::list<Player*> connected_players = getPlayers(true);
103 u32 chosen_one = myrand() % connected_players.size();
105 for(core::list<Player*>::Iterator
106 i = connected_players.begin();
107 i != connected_players.end(); i++)
119 Player * Environment::getNearestConnectedPlayer(v3f pos)
121 core::list<Player*> connected_players = getPlayers(true);
123 Player *nearest_player = NULL;
124 for(core::list<Player*>::Iterator
125 i = connected_players.begin();
126 i != connected_players.end(); i++)
129 f32 d = player->getPosition().getDistanceFrom(pos);
130 if(d < nearest_d || nearest_player == NULL)
133 nearest_player = player;
136 return nearest_player;
139 core::list<Player*> Environment::getPlayers()
144 core::list<Player*> Environment::getPlayers(bool ignore_disconnected)
146 core::list<Player*> newlist;
147 for(core::list<Player*>::Iterator
148 i = m_players.begin();
149 i != m_players.end(); i++)
153 if(ignore_disconnected)
155 // Ignore disconnected players
156 if(player->peer_id == 0)
160 newlist.push_back(player);
165 void Environment::printPlayers(std::ostream &o)
167 o<<"Players in environment:"<<std::endl;
168 for(core::list<Player*>::Iterator i = m_players.begin();
169 i != m_players.end(); i++)
172 o<<"Player peer_id="<<player->peer_id<<std::endl;
176 void Environment::setDayNightRatio(u32 r)
178 m_daynight_ratio = r;
181 u32 Environment::getDayNightRatio()
183 return m_daynight_ratio;
190 ServerEnvironment::ServerEnvironment(ServerMap *map, Server *server):
193 m_random_spawn_timer(3),
194 m_send_recommended_timer(0)
198 ServerEnvironment::~ServerEnvironment()
204 void ServerEnvironment::serializePlayers(const std::string &savedir)
206 std::string players_path = savedir + "/players";
207 fs::CreateDir(players_path);
209 core::map<Player*, bool> saved_players;
211 std::vector<fs::DirListNode> player_files = fs::GetDirListing(players_path);
212 for(u32 i=0; i<player_files.size(); i++)
214 if(player_files[i].dir)
217 // Full path to this file
218 std::string path = players_path + "/" + player_files[i].name;
220 //dstream<<"Checking player file "<<path<<std::endl;
222 // Load player to see what is its name
223 ServerRemotePlayer testplayer;
225 // Open file and deserialize
226 std::ifstream is(path.c_str(), std::ios_base::binary);
227 if(is.good() == false)
229 dstream<<"Failed to read "<<path<<std::endl;
232 testplayer.deSerialize(is);
235 //dstream<<"Loaded test player with name "<<testplayer.getName()<<std::endl;
237 // Search for the player
238 std::string playername = testplayer.getName();
239 Player *player = getPlayer(playername.c_str());
242 dstream<<"Didn't find matching player, ignoring file "<<path<<std::endl;
246 //dstream<<"Found matching player, overwriting."<<std::endl;
248 // OK, found. Save player there.
250 // Open file and serialize
251 std::ofstream os(path.c_str(), std::ios_base::binary);
252 if(os.good() == false)
254 dstream<<"Failed to overwrite "<<path<<std::endl;
257 player->serialize(os);
258 saved_players.insert(player, true);
262 for(core::list<Player*>::Iterator i = m_players.begin();
263 i != m_players.end(); i++)
266 if(saved_players.find(player) != NULL)
268 /*dstream<<"Player "<<player->getName()
269 <<" was already saved."<<std::endl;*/
272 std::string playername = player->getName();
273 // Don't save unnamed player
276 //dstream<<"Not saving unnamed player."<<std::endl;
282 if(string_allowed(playername, PLAYERNAME_ALLOWED_CHARS) == false)
283 playername = "player";
284 std::string path = players_path + "/" + playername;
286 for(u32 i=0; i<1000; i++)
288 if(fs::PathExists(path) == false)
293 path = players_path + "/" + playername + itos(i);
297 dstream<<"WARNING: Didn't find free file for player"<<std::endl;
302 /*dstream<<"Saving player "<<player->getName()<<" to "
304 // Open file and serialize
305 std::ofstream os(path.c_str(), std::ios_base::binary);
306 if(os.good() == false)
308 dstream<<"WARNING: Failed to overwrite "<<path<<std::endl;
311 player->serialize(os);
312 saved_players.insert(player, true);
316 //dstream<<"Saved "<<saved_players.size()<<" players."<<std::endl;
319 void ServerEnvironment::deSerializePlayers(const std::string &savedir)
321 std::string players_path = savedir + "/players";
323 core::map<Player*, bool> saved_players;
325 std::vector<fs::DirListNode> player_files = fs::GetDirListing(players_path);
326 for(u32 i=0; i<player_files.size(); i++)
328 if(player_files[i].dir)
331 // Full path to this file
332 std::string path = players_path + "/" + player_files[i].name;
334 dstream<<"Checking player file "<<path<<std::endl;
336 // Load player to see what is its name
337 ServerRemotePlayer testplayer;
339 // Open file and deserialize
340 std::ifstream is(path.c_str(), std::ios_base::binary);
341 if(is.good() == false)
343 dstream<<"Failed to read "<<path<<std::endl;
346 testplayer.deSerialize(is);
349 dstream<<"Loaded test player with name "<<testplayer.getName()<<std::endl;
351 // Search for the player
352 std::string playername = testplayer.getName();
353 Player *player = getPlayer(playername.c_str());
354 bool newplayer = false;
357 dstream<<"Is a new player"<<std::endl;
358 player = new ServerRemotePlayer();
364 dstream<<"Reading player "<<testplayer.getName()<<" from "
366 // Open file and deserialize
367 std::ifstream is(path.c_str(), std::ios_base::binary);
368 if(is.good() == false)
370 dstream<<"Failed to read "<<path<<std::endl;
373 player->deSerialize(is);
382 void spawnRandomObjects(MapBlock *block)
384 for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
385 for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
387 bool last_node_walkable = false;
388 for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
391 MapNode n = block->getNodeNoEx(p);
392 if(n.d == CONTENT_IGNORE)
394 if(content_features(n.d).liquid_type != LIQUID_NONE)
396 if(content_features(n.d).walkable)
398 last_node_walkable = true;
401 if(last_node_walkable)
403 // If block contains light information
404 if(content_features(n.d).param_type == CPT_LIGHT)
406 if(n.getLight(LIGHTBANK_DAY) <= 5)
408 if(myrand() % 1000 == 0)
410 v3f pos_f = intToFloat(p+block->getPosRelative(), BS);
412 ServerActiveObject *obj = new Oerkki1SAO(NULL,0,pos_f);
413 std::string data = obj->getStaticData();
414 StaticObject s_obj(obj->getType(),
415 obj->getBasePosition(), data);
417 block->m_static_objects.insert(0, s_obj);
419 block->setChangedFlag();
424 last_node_walkable = false;
430 void ServerEnvironment::step(float dtime)
432 DSTACK(__FUNCTION_NAME);
434 //TimeTaker timer("ServerEnv step");
437 //bool free_move = g_settings.getBool("free_move");
438 bool footprints = g_settings.getBool("footprints");
441 //TimeTaker timer("Server m_map->timerUpdate()");
442 m_map->timerUpdate(dtime);
448 for(core::list<Player*>::Iterator i = m_players.begin();
449 i != m_players.end(); i++)
453 // Ignore disconnected players
454 if(player->peer_id == 0)
457 v3f playerpos = player->getPosition();
460 player->move(dtime, *m_map, 100*BS);
463 Add footsteps to grass
467 // Get node that is at BS/4 under player
468 v3s16 bottompos = floatToInt(playerpos + v3f(0,-BS/4,0), BS);
470 MapNode n = m_map->getNode(bottompos);
471 if(n.d == CONTENT_GRASS)
473 n.d = CONTENT_GRASS_FOOTSTEPS;
474 m_map->setNode(bottompos, n);
477 catch(InvalidPositionException &e)
487 //TimeTaker timer("Step active objects");
489 bool send_recommended = false;
490 m_send_recommended_timer += dtime;
491 if(m_send_recommended_timer > 0.15)
493 m_send_recommended_timer = 0;
494 send_recommended = true;
497 for(core::map<u16, ServerActiveObject*>::Iterator
498 i = m_active_objects.getIterator();
499 i.atEnd()==false; i++)
501 ServerActiveObject* obj = i.getNode()->getValue();
502 // Step object, putting messages directly to the queue
503 obj->step(dtime, m_active_object_messages, send_recommended);
507 if(m_object_management_interval.step(dtime, 0.5))
509 //TimeTaker timer("ServerEnv object management");
512 Remove objects that satisfy (m_removed && m_known_by_count==0)
515 core::list<u16> objects_to_remove;
516 for(core::map<u16, ServerActiveObject*>::Iterator
517 i = m_active_objects.getIterator();
518 i.atEnd()==false; i++)
520 u16 id = i.getNode()->getKey();
521 ServerActiveObject* obj = i.getNode()->getValue();
522 // This shouldn't happen but check it
525 dstream<<"WARNING: NULL object found in ServerEnvironment"
526 <<" while finding removed objects. id="<<id<<std::endl;
527 // Id to be removed from m_active_objects
528 objects_to_remove.push_back(id);
531 // If not m_removed, don't remove.
532 if(obj->m_removed == false)
534 // Delete static data from block
535 if(obj->m_static_exists)
537 MapBlock *block = m_map->getBlockNoCreateNoEx(obj->m_static_block);
540 block->m_static_objects.remove(id);
541 block->setChangedFlag();
544 // If m_known_by_count > 0, don't actually remove.
545 if(obj->m_known_by_count > 0)
549 // Id to be removed from m_active_objects
550 objects_to_remove.push_back(id);
552 // Remove references from m_active_objects
553 for(core::list<u16>::Iterator i = objects_to_remove.begin();
554 i != objects_to_remove.end(); i++)
556 m_active_objects.remove(*i);
561 const s16 to_active_max_blocks = 3;
562 const f32 to_static_max_f = (to_active_max_blocks+2)*MAP_BLOCKSIZE*BS;
565 Convert stored objects from blocks near the players to active.
567 for(core::list<Player*>::Iterator i = m_players.begin();
568 i != m_players.end(); i++)
572 // Ignore disconnected players
573 if(player->peer_id == 0)
576 v3f playerpos = player->getPosition();
578 v3s16 blockpos0 = getNodeBlockPos(floatToInt(playerpos, BS));
579 v3s16 bpmin = blockpos0 - v3s16(1,1,1)*to_active_max_blocks;
580 v3s16 bpmax = blockpos0 + v3s16(1,1,1)*to_active_max_blocks;
581 // Loop through all nearby blocks
582 for(s16 x=bpmin.X; x<=bpmax.X; x++)
583 for(s16 y=bpmin.Y; y<=bpmax.Y; y++)
584 for(s16 z=bpmin.Z; z<=bpmax.Z; z++)
586 v3s16 blockpos(x,y,z);
587 MapBlock *block = m_map->getBlockNoCreateNoEx(blockpos);
590 // Ignore if no stored objects (to not set changed flag)
591 if(block->m_static_objects.m_stored.size() == 0)
593 // This will contain the leftovers of the stored list
594 core::list<StaticObject> new_stored;
595 // Loop through stored static objects
596 for(core::list<StaticObject>::Iterator
597 i = block->m_static_objects.m_stored.begin();
598 i != block->m_static_objects.m_stored.end(); i++)
600 /*dstream<<"INFO: Server: Creating an active object from "
601 <<"static data"<<std::endl;*/
602 StaticObject &s_obj = *i;
603 // Create an active object from the data
604 ServerActiveObject *obj = ServerActiveObject::create
605 (s_obj.type, this, 0, s_obj.pos, s_obj.data);
608 // This is necessary to preserve stuff during bugs
610 new_stored.push_back(s_obj);
613 // This will also add the object to the active static list
614 addActiveObject(obj);
615 //u16 id = addActiveObject(obj);
618 block->m_static_objects.m_stored.clear();
619 // Add leftover stuff to stored list
620 for(core::list<StaticObject>::Iterator
621 i = new_stored.begin();
622 i != new_stored.end(); i++)
624 StaticObject &s_obj = *i;
625 block->m_static_objects.m_stored.push_back(s_obj);
627 block->setChangedFlag();
632 Convert objects that are far away from all the players to static.
635 core::list<u16> objects_to_remove;
636 for(core::map<u16, ServerActiveObject*>::Iterator
637 i = m_active_objects.getIterator();
638 i.atEnd()==false; i++)
640 ServerActiveObject* obj = i.getNode()->getValue();
641 u16 id = i.getNode()->getKey();
642 v3f objectpos = obj->getBasePosition();
644 // This shouldn't happen but check it
647 dstream<<"WARNING: NULL object found in ServerEnvironment"
651 // If known by some client, don't convert to static.
652 if(obj->m_known_by_count > 0)
655 // If close to some player, don't convert to static.
656 bool close_to_player = false;
657 for(core::list<Player*>::Iterator i = m_players.begin();
658 i != m_players.end(); i++)
662 // Ignore disconnected players
663 if(player->peer_id == 0)
666 v3f playerpos = player->getPosition();
667 f32 d = playerpos.getDistanceFrom(objectpos);
668 if(d < to_static_max_f)
670 close_to_player = true;
679 Update the static data and remove the active object.
682 // Delete old static object
683 MapBlock *oldblock = NULL;
684 if(obj->m_static_exists)
686 MapBlock *block = m_map->getBlockNoCreateNoEx
687 (obj->m_static_block);
690 block->m_static_objects.remove(id);
694 // Add new static object
695 std::string staticdata = obj->getStaticData();
696 StaticObject s_obj(obj->getType(), objectpos, staticdata);
697 // Add to the block where the object is located in
698 v3s16 blockpos = getNodeBlockPos(floatToInt(objectpos, BS));
699 MapBlock *block = m_map->getBlockNoCreateNoEx(blockpos);
702 block->m_static_objects.insert(0, s_obj);
703 block->setChangedFlag();
704 obj->m_static_exists = true;
705 obj->m_static_block = block->getPos();
707 // If not possible, add back to previous block
710 oldblock->m_static_objects.insert(0, s_obj);
711 oldblock->setChangedFlag();
712 obj->m_static_exists = true;
713 obj->m_static_block = oldblock->getPos();
716 dstream<<"WARNING: Server: Could not find a block for "
717 <<"storing static object"<<std::endl;
718 obj->m_static_exists = false;
721 /*dstream<<"INFO: Server: Stored static data. Deleting object."
723 // Delete active object
725 // Id to be removed from m_active_objects
726 objects_to_remove.push_back(id);
728 // Remove references from m_active_objects
729 for(core::list<u16>::Iterator i = objects_to_remove.begin();
730 i != objects_to_remove.end(); i++)
732 m_active_objects.remove(*i);
737 if(g_settings.getBool("enable_experimental"))
744 m_random_spawn_timer -= dtime;
745 if(m_random_spawn_timer < 0)
747 //m_random_spawn_timer += myrand_range(2.0, 20.0);
748 //m_random_spawn_timer += 2.0;
749 m_random_spawn_timer += 200.0;
755 /*v2s16 p2d(myrand_range(-5,5), myrand_range(-5,5));
756 s16 y = 1 + getServerMap().findGroundLevel(p2d);
757 v3f pos(p2d.X*BS,y*BS,p2d.Y*BS);*/
759 Player *player = getRandomConnectedPlayer();
762 pos = player->getPosition();
764 myrand_range(-3,3)*BS,
766 myrand_range(-3,3)*BS
770 Create a ServerActiveObject
773 //TestSAO *obj = new TestSAO(this, 0, pos);
774 //ServerActiveObject *obj = new ItemSAO(this, 0, pos, "CraftItem Stick 1");
775 //ServerActiveObject *obj = new RatSAO(this, 0, pos);
776 ServerActiveObject *obj = new Oerkki1SAO(this, 0, pos);
777 addActiveObject(obj);
781 } // enable_experimental
784 ServerActiveObject* ServerEnvironment::getActiveObject(u16 id)
786 core::map<u16, ServerActiveObject*>::Node *n;
787 n = m_active_objects.find(id);
790 return n->getValue();
793 bool isFreeServerActiveObjectId(u16 id,
794 core::map<u16, ServerActiveObject*> &objects)
799 for(core::map<u16, ServerActiveObject*>::Iterator
800 i = objects.getIterator();
801 i.atEnd()==false; i++)
803 if(i.getNode()->getKey() == id)
809 u16 getFreeServerActiveObjectId(
810 core::map<u16, ServerActiveObject*> &objects)
815 if(isFreeServerActiveObjectId(new_id, objects))
825 u16 ServerEnvironment::addActiveObject(ServerActiveObject *object)
828 if(object->getId() == 0)
830 u16 new_id = getFreeServerActiveObjectId(m_active_objects);
833 dstream<<"WARNING: ServerEnvironment::addActiveObject(): "
834 <<"no free ids available"<<std::endl;
838 object->setId(new_id);
840 if(isFreeServerActiveObjectId(object->getId(), m_active_objects) == false)
842 dstream<<"WARNING: ServerEnvironment::addActiveObject(): "
843 <<"id is not free ("<<object->getId()<<")"<<std::endl;
847 /*dstream<<"INGO: ServerEnvironment::addActiveObject(): "
848 <<"added (id="<<object->getId()<<")"<<std::endl;*/
850 m_active_objects.insert(object->getId(), object);
852 // Add static object to active static list of the block
853 v3f objectpos = object->getBasePosition();
854 std::string staticdata = object->getStaticData();
855 StaticObject s_obj(object->getType(), objectpos, staticdata);
856 // Add to the block where the object is located in
857 v3s16 blockpos = getNodeBlockPos(floatToInt(objectpos, BS));
858 MapBlock *block = m_map->getBlockNoCreateNoEx(blockpos);
861 block->m_static_objects.m_active.insert(object->getId(), s_obj);
862 object->m_static_exists = true;
863 object->m_static_block = blockpos;
866 dstream<<"WARNING: Server: Could not find a block for "
867 <<"storing newly added static active object"<<std::endl;
870 return object->getId();
874 Finds out what new objects have been added to
875 inside a radius around a position
877 void ServerEnvironment::getAddedActiveObjects(v3s16 pos, s16 radius,
878 core::map<u16, bool> ¤t_objects,
879 core::map<u16, bool> &added_objects)
881 v3f pos_f = intToFloat(pos, BS);
882 f32 radius_f = radius * BS;
884 Go through the object list,
885 - discard m_removed objects,
886 - discard objects that are too far away,
887 - discard objects that are found in current_objects.
888 - add remaining objects to added_objects
890 for(core::map<u16, ServerActiveObject*>::Iterator
891 i = m_active_objects.getIterator();
892 i.atEnd()==false; i++)
894 u16 id = i.getNode()->getKey();
896 ServerActiveObject *object = i.getNode()->getValue();
899 // Discard if removed
900 if(object->m_removed)
902 // Discard if too far
903 f32 distance_f = object->getBasePosition().getDistanceFrom(pos_f);
904 if(distance_f > radius_f)
906 // Discard if already on current_objects
907 core::map<u16, bool>::Node *n;
908 n = current_objects.find(id);
911 // Add to added_objects
912 added_objects.insert(id, false);
917 Finds out what objects have been removed from
918 inside a radius around a position
920 void ServerEnvironment::getRemovedActiveObjects(v3s16 pos, s16 radius,
921 core::map<u16, bool> ¤t_objects,
922 core::map<u16, bool> &removed_objects)
924 v3f pos_f = intToFloat(pos, BS);
925 f32 radius_f = radius * BS;
927 Go through current_objects; object is removed if:
928 - object is not found in m_active_objects (this is actually an
929 error condition; objects should be set m_removed=true and removed
930 only after all clients have been informed about removal), or
931 - object has m_removed=true, or
932 - object is too far away
934 for(core::map<u16, bool>::Iterator
935 i = current_objects.getIterator();
936 i.atEnd()==false; i++)
938 u16 id = i.getNode()->getKey();
939 ServerActiveObject *object = getActiveObject(id);
942 dstream<<"WARNING: ServerEnvironment::getRemovedActiveObjects():"
943 <<" object in current_objects is NULL"<<std::endl;
945 else if(object->m_removed == false)
947 f32 distance_f = object->getBasePosition().getDistanceFrom(pos_f);
948 /*dstream<<"removed == false"
949 <<"distance_f = "<<distance_f
950 <<", radius_f = "<<radius_f<<std::endl;*/
951 if(distance_f < radius_f)
957 removed_objects.insert(id, false);
961 ActiveObjectMessage ServerEnvironment::getActiveObjectMessage()
963 if(m_active_object_messages.size() == 0)
964 return ActiveObjectMessage(0);
966 return m_active_object_messages.pop_front();
975 ClientEnvironment::ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr):
983 ClientEnvironment::~ClientEnvironment()
985 // delete active objects
986 for(core::map<u16, ClientActiveObject*>::Iterator
987 i = m_active_objects.getIterator();
988 i.atEnd()==false; i++)
990 delete i.getNode()->getValue();
997 void ClientEnvironment::addPlayer(Player *player)
999 DSTACK(__FUNCTION_NAME);
1001 It is a failure if player is local and there already is a local
1004 assert(!(player->isLocal() == true && getLocalPlayer() != NULL));
1006 Environment::addPlayer(player);
1009 LocalPlayer * ClientEnvironment::getLocalPlayer()
1011 for(core::list<Player*>::Iterator i = m_players.begin();
1012 i != m_players.end(); i++)
1014 Player *player = *i;
1015 if(player->isLocal())
1016 return (LocalPlayer*)player;
1021 void ClientEnvironment::step(float dtime)
1023 DSTACK(__FUNCTION_NAME);
1025 // Get some settings
1026 bool free_move = g_settings.getBool("free_move");
1027 bool footprints = g_settings.getBool("footprints");
1030 //TimeTaker timer("Client m_map->timerUpdate()");
1031 m_map->timerUpdate(dtime);
1035 LocalPlayer *lplayer = getLocalPlayer();
1037 // collision info queue
1038 core::list<CollisionInfo> player_collisions;
1041 Get the speed the player is going
1043 f32 player_speed = 0.001; // just some small value
1044 player_speed = lplayer->getSpeed().getLength();
1047 Maximum position increment
1049 //f32 position_max_increment = 0.05*BS;
1050 f32 position_max_increment = 0.1*BS;
1052 // Maximum time increment (for collision detection etc)
1053 // time = distance / speed
1054 f32 dtime_max_increment = position_max_increment / player_speed;
1056 // Maximum time increment is 10ms or lower
1057 if(dtime_max_increment > 0.01)
1058 dtime_max_increment = 0.01;
1060 // Don't allow overly huge dtime
1064 f32 dtime_downcount = dtime;
1067 Stuff that has a maximum time increment
1076 if(dtime_downcount > dtime_max_increment)
1078 dtime_part = dtime_max_increment;
1079 dtime_downcount -= dtime_part;
1083 dtime_part = dtime_downcount;
1085 Setting this to 0 (no -=dtime_part) disables an infinite loop
1086 when dtime_part is so small that dtime_downcount -= dtime_part
1089 dtime_downcount = 0;
1097 v3f lplayerpos = lplayer->getPosition();
1100 if(free_move == false)
1103 v3f speed = lplayer->getSpeed();
1104 if(lplayer->swimming_up == false)
1105 speed.Y -= 9.81 * BS * dtime_part * 2;
1108 if(lplayer->in_water_stable || lplayer->in_water)
1110 f32 max_down = 2.0*BS;
1111 if(speed.Y < -max_down) speed.Y = -max_down;
1114 if(speed.getLength() > max)
1116 speed = speed / speed.getLength() * max;
1120 lplayer->setSpeed(speed);
1125 This also does collision detection.
1127 lplayer->move(dtime_part, *m_map, position_max_increment,
1128 &player_collisions);
1131 while(dtime_downcount > 0.001);
1133 //std::cout<<"Looped "<<loopcount<<" times."<<std::endl;
1135 for(core::list<CollisionInfo>::Iterator
1136 i = player_collisions.begin();
1137 i != player_collisions.end(); i++)
1139 CollisionInfo &info = *i;
1140 if(info.t == COLLISION_FALL)
1142 //f32 tolerance = BS*10; // 2 without damage
1143 f32 tolerance = BS*12; // 3 without damage
1145 if(info.speed > tolerance)
1147 f32 damage_f = (info.speed - tolerance)/BS*factor;
1148 u16 damage = (u16)(damage_f+0.5);
1149 if(lplayer->hp > damage)
1150 lplayer->hp -= damage;
1154 ClientEnvEvent event;
1155 event.type = CEE_PLAYER_DAMAGE;
1156 event.player_damage.amount = damage;
1157 m_client_event_queue.push_back(event);
1163 Stuff that can be done in an arbitarily large dtime
1165 for(core::list<Player*>::Iterator i = m_players.begin();
1166 i != m_players.end(); i++)
1168 Player *player = *i;
1169 v3f playerpos = player->getPosition();
1172 Handle non-local players
1174 if(player->isLocal() == false)
1177 player->move(dtime, *m_map, 100*BS);
1179 // Update lighting on remote players on client
1180 u8 light = LIGHT_MAX;
1183 v3s16 p = floatToInt(playerpos + v3f(0,BS+BS/2,0), BS);
1184 MapNode n = m_map->getNode(p);
1185 light = n.getLightBlend(m_daynight_ratio);
1187 catch(InvalidPositionException &e) {}
1188 player->updateLight(light);
1192 Add footsteps to grass
1196 // Get node that is at BS/4 under player
1197 v3s16 bottompos = floatToInt(playerpos + v3f(0,-BS/4,0), BS);
1199 MapNode n = m_map->getNode(bottompos);
1200 if(n.d == CONTENT_GRASS)
1202 n.d = CONTENT_GRASS_FOOTSTEPS;
1203 m_map->setNode(bottompos, n);
1204 // Update mesh on client
1205 if(m_map->mapType() == MAPTYPE_CLIENT)
1207 v3s16 p_blocks = getNodeBlockPos(bottompos);
1208 MapBlock *b = m_map->getBlockNoCreate(p_blocks);
1209 //b->updateMesh(m_daynight_ratio);
1210 b->setMeshExpired(true);
1214 catch(InvalidPositionException &e)
1221 Step active objects and update lighting of them
1224 for(core::map<u16, ClientActiveObject*>::Iterator
1225 i = m_active_objects.getIterator();
1226 i.atEnd()==false; i++)
1228 ClientActiveObject* obj = i.getNode()->getValue();
1230 obj->step(dtime, this);
1232 //u8 light = LIGHT_MAX;
1236 v3s16 p = obj->getLightPosition();
1237 MapNode n = m_map->getNode(p);
1238 light = n.getLightBlend(m_daynight_ratio);
1240 catch(InvalidPositionException &e) {}
1241 obj->updateLight(light);
1245 void ClientEnvironment::updateMeshes(v3s16 blockpos)
1247 m_map->updateMeshes(blockpos, m_daynight_ratio);
1250 void ClientEnvironment::expireMeshes(bool only_daynight_diffed)
1252 m_map->expireMeshes(only_daynight_diffed);
1255 ClientActiveObject* ClientEnvironment::getActiveObject(u16 id)
1257 core::map<u16, ClientActiveObject*>::Node *n;
1258 n = m_active_objects.find(id);
1261 return n->getValue();
1264 bool isFreeClientActiveObjectId(u16 id,
1265 core::map<u16, ClientActiveObject*> &objects)
1270 for(core::map<u16, ClientActiveObject*>::Iterator
1271 i = objects.getIterator();
1272 i.atEnd()==false; i++)
1274 if(i.getNode()->getKey() == id)
1280 u16 getFreeClientActiveObjectId(
1281 core::map<u16, ClientActiveObject*> &objects)
1286 if(isFreeClientActiveObjectId(new_id, objects))
1296 u16 ClientEnvironment::addActiveObject(ClientActiveObject *object)
1299 if(object->getId() == 0)
1301 u16 new_id = getFreeClientActiveObjectId(m_active_objects);
1304 dstream<<"WARNING: ClientEnvironment::addActiveObject(): "
1305 <<"no free ids available"<<std::endl;
1309 object->setId(new_id);
1311 if(isFreeClientActiveObjectId(object->getId(), m_active_objects) == false)
1313 dstream<<"WARNING: ClientEnvironment::addActiveObject(): "
1314 <<"id is not free ("<<object->getId()<<")"<<std::endl;
1318 dstream<<"INGO: ClientEnvironment::addActiveObject(): "
1319 <<"added (id="<<object->getId()<<")"<<std::endl;
1320 m_active_objects.insert(object->getId(), object);
1321 object->addToScene(m_smgr);
1322 return object->getId();
1325 void ClientEnvironment::addActiveObject(u16 id, u8 type,
1326 const std::string &init_data)
1328 ClientActiveObject* obj = ClientActiveObject::create(type);
1331 dstream<<"WARNING: ClientEnvironment::addActiveObject(): "
1332 <<"id="<<id<<" type="<<type<<": Couldn't create object"
1339 addActiveObject(obj);
1341 obj->initialize(init_data);
1344 void ClientEnvironment::removeActiveObject(u16 id)
1346 dstream<<"ClientEnvironment::removeActiveObject(): "
1347 <<"id="<<id<<std::endl;
1348 ClientActiveObject* obj = getActiveObject(id);
1351 dstream<<"WARNING: ClientEnvironment::removeActiveObject(): "
1352 <<"id="<<id<<" not found"<<std::endl;
1355 obj->removeFromScene();
1357 m_active_objects.remove(id);
1360 void ClientEnvironment::processActiveObjectMessage(u16 id,
1361 const std::string &data)
1363 ClientActiveObject* obj = getActiveObject(id);
1366 dstream<<"WARNING: ClientEnvironment::processActiveObjectMessage():"
1367 <<" got message for id="<<id<<", which doesn't exist."
1371 obj->processMessage(data);
1375 Callbacks for activeobjects
1378 void ClientEnvironment::damageLocalPlayer(u8 damage)
1380 LocalPlayer *lplayer = getLocalPlayer();
1383 if(lplayer->hp > damage)
1384 lplayer->hp -= damage;
1388 ClientEnvEvent event;
1389 event.type = CEE_PLAYER_DAMAGE;
1390 event.player_damage.amount = damage;
1391 m_client_event_queue.push_back(event);
1395 Client likes to call these
1398 void ClientEnvironment::getActiveObjects(v3f origin, f32 max_d,
1399 core::array<DistanceSortedActiveObject> &dest)
1401 for(core::map<u16, ClientActiveObject*>::Iterator
1402 i = m_active_objects.getIterator();
1403 i.atEnd()==false; i++)
1405 ClientActiveObject* obj = i.getNode()->getValue();
1407 f32 d = (obj->getPosition() - origin).getLength();
1412 DistanceSortedActiveObject dso(obj, d);
1414 dest.push_back(dso);
1418 ClientEnvEvent ClientEnvironment::getClientEvent()
1420 if(m_client_event_queue.size() == 0)
1422 ClientEnvEvent event;
1423 event.type = CEE_NONE;
1426 return m_client_event_queue.pop_front();
1429 #endif // #ifndef SERVER