]> git.lizzy.rs Git - minetest.git/blobdiff - src/environment.cpp
Install minetest_game without .git and other unnecessary things
[minetest.git] / src / environment.cpp
index cfe82fb8b537a7a82f5719060e40bc3fa72a0292..0c6d829f5b44b557676fec9ed66d1f6f0b2201f6 100644 (file)
@@ -37,12 +37,18 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "nodemetadata.h"
 #include "main.h" // For g_settings, g_profiler
 #include "gamedef.h"
-#include "serverremoteplayer.h"
+#ifndef SERVER
+#include "clientmap.h"
+#endif
+#include "daynightratio.h"
 
 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
 
 Environment::Environment():
-       m_time_of_day(9000)
+       m_time_of_day(9000),
+       m_time_of_day_f(9000./24000),
+       m_time_of_day_speed(0),
+       m_time_counter(0)
 {
 }
 
@@ -192,17 +198,35 @@ void Environment::printPlayers(std::ostream &o)
        }
 }
 
-/*void Environment::setDayNightRatio(u32 r)
-{
-       getDayNightRatio() = r;
-}*/
-
 u32 Environment::getDayNightRatio()
 {
-       //return getDayNightRatio();
        return time_to_daynight_ratio(m_time_of_day);
 }
 
+void Environment::stepTimeOfDay(float dtime)
+{
+       m_time_counter += dtime;
+       f32 speed = m_time_of_day_speed * 24000./(24.*3600);
+       u32 units = (u32)(m_time_counter*speed);
+       m_time_counter -= (f32)units / speed;
+       bool sync_f = false;
+       if(units > 0){
+               // Sync at overflow
+               if(m_time_of_day + units >= 24000)
+                       sync_f = true;
+               m_time_of_day = (m_time_of_day + units) % 24000;
+               if(sync_f)
+                       m_time_of_day_f = (float)m_time_of_day / 24000.0;
+       }
+       if(!sync_f){
+               m_time_of_day_f += m_time_of_day_speed/24/3600*dtime;
+               if(m_time_of_day_f > 1.0)
+                       m_time_of_day_f -= 1.0;
+               if(m_time_of_day_f < 0.0)
+                       m_time_of_day_f += 1.0;
+       }
+}
+
 /*
        ABMWithState
 */
@@ -343,7 +367,7 @@ void ServerEnvironment::serializePlayers(const std::string &savedir)
                //infostream<<"Checking player file "<<path<<std::endl;
 
                // Load player to see what is its name
-               ServerRemotePlayer testplayer(this);
+               RemotePlayer testplayer(m_gamedef);
                {
                        // Open file and deserialize
                        std::ifstream is(path.c_str(), std::ios_base::binary);
@@ -457,7 +481,7 @@ void ServerEnvironment::deSerializePlayers(const std::string &savedir)
                //infostream<<"Checking player file "<<path<<std::endl;
 
                // Load player to see what is its name
-               ServerRemotePlayer testplayer(this);
+               RemotePlayer testplayer(m_gamedef);
                {
                        // Open file and deserialize
                        std::ifstream is(path.c_str(), std::ios_base::binary);
@@ -485,12 +509,10 @@ void ServerEnvironment::deSerializePlayers(const std::string &savedir)
                if(player == NULL)
                {
                        //infostream<<"Is a new player"<<std::endl;
-                       player = new ServerRemotePlayer(this);
+                       player = new RemotePlayer(m_gamedef);
                        newplayer = true;
                }
 
-               ServerRemotePlayer *srp = static_cast<ServerRemotePlayer*>(player);
-
                // Load player
                {
                        verbosestream<<"Reading player "<<testplayer.getName()<<" from "
@@ -502,9 +524,7 @@ void ServerEnvironment::deSerializePlayers(const std::string &savedir)
                                infostream<<"Failed to read "<<path<<std::endl;
                                continue;
                        }
-                       srp->deSerialize(is);
-                       srp->m_last_good_position = srp->getBasePosition();
-                       srp->m_last_good_position_age = 0;
+                       player->deSerialize(is);
                }
 
                if(newplayer)
@@ -890,8 +910,8 @@ void ServerEnvironment::step(float dtime)
        
        //TimeTaker timer("ServerEnv step");
 
-       // Get some settings
-       bool footprints = g_settings->getBool("footprints");
+       /* Step time of day */
+       stepTimeOfDay(dtime);
 
        /*
                Increment game time
@@ -921,26 +941,6 @@ void ServerEnvironment::step(float dtime)
                        
                        // Move
                        player->move(dtime, *m_map, 100*BS);
-                       
-                       /*
-                               Add footsteps to grass
-                       */
-                       if(footprints)
-                       {
-                               // Get node that is at BS/4 under player
-                               v3s16 bottompos = floatToInt(playerpos + v3f(0,-BS/4,0), BS);
-                               try{
-                                       MapNode n = m_map->getNode(bottompos);
-                                       if(n.getContent() == LEGN(m_gamedef->ndef(), "CONTENT_GRASS"))
-                                       {
-                                               n.setContent(LEGN(m_gamedef->ndef(), "CONTENT_GRASS_FOOTSTEPS"));
-                                               m_map->setNode(bottompos, n);
-                                       }
-                               }
-                               catch(InvalidPositionException &e)
-                               {
-                               }
-                       }
                }
        }
 
@@ -1843,6 +1843,16 @@ ClientEnvironment::~ClientEnvironment()
        m_map->drop();
 }
 
+Map & ClientEnvironment::getMap()
+{
+       return *m_map;
+}
+
+ClientMap & ClientEnvironment::getClientMap()
+{
+       return *m_map;
+}
+
 void ClientEnvironment::addPlayer(Player *player)
 {
        DSTACK(__FUNCTION_NAME);
@@ -1871,9 +1881,11 @@ void ClientEnvironment::step(float dtime)
 {
        DSTACK(__FUNCTION_NAME);
 
+       /* Step time of day */
+       stepTimeOfDay(dtime);
+
        // Get some settings
        bool free_move = g_settings->getBool("free_move");
-       bool footprints = g_settings->getBool("footprints");
 
        // Get local player
        LocalPlayer *lplayer = getLocalPlayer();
@@ -1987,13 +1999,15 @@ void ClientEnvironment::step(float dtime)
                if(info.t == COLLISION_FALL)
                {
                        //f32 tolerance = BS*10; // 2 without damage
-                       f32 tolerance = BS*12; // 3 without damage
+                       //f32 tolerance = BS*12; // 3 without damage
+                       f32 tolerance = BS*14; // 5 without damage
                        f32 factor = 1;
                        if(info.speed > tolerance)
                        {
                                f32 damage_f = (info.speed - tolerance)/BS*factor;
                                u16 damage = (u16)(damage_f+0.5);
-                               damageLocalPlayer(damage, true);
+                               if(damage != 0)
+                                       damageLocalPlayer(damage, true);
                        }
                }
        }
@@ -2057,35 +2071,7 @@ void ClientEnvironment::step(float dtime)
                catch(InvalidPositionException &e){
                        light = blend_light(getDayNightRatio(), LIGHT_SUN, 0);
                }
-               player->updateLight(light);
-
-               /*
-                       Add footsteps to grass
-               */
-               if(footprints)
-               {
-                       // Get node that is at BS/4 under player
-                       v3s16 bottompos = floatToInt(playerpos + v3f(0,-BS/4,0), BS);
-                       try{
-                               MapNode n = m_map->getNode(bottompos);
-                               if(n.getContent() == LEGN(m_gamedef->ndef(), "CONTENT_GRASS"))
-                               {
-                                       n.setContent(LEGN(m_gamedef->ndef(), "CONTENT_GRASS_FOOTSTEPS"));
-                                       m_map->setNode(bottompos, n);
-                                       // Update mesh on client
-                                       if(m_map->mapType() == MAPTYPE_CLIENT)
-                                       {
-                                               v3s16 p_blocks = getNodeBlockPos(bottompos);
-                                               MapBlock *b = m_map->getBlockNoCreate(p_blocks);
-                                               //b->updateMesh(getDayNightRatio());
-                                               b->setMeshExpired(true);
-                                       }
-                               }
-                       }
-                       catch(InvalidPositionException &e)
-                       {
-                       }
-               }
+               player->light = light;
        }
        
        /*
@@ -2133,16 +2119,6 @@ void ClientEnvironment::step(float dtime)
                }
        }
 }
-
-void ClientEnvironment::updateMeshes(v3s16 blockpos)
-{
-       m_map->updateMeshes(blockpos, getDayNightRatio());
-}
-
-void ClientEnvironment::expireMeshes(bool only_daynight_diffed)
-{
-       m_map->expireMeshes(only_daynight_diffed);
-}
        
 void ClientEnvironment::addSimpleObject(ClientSimpleObject *simple)
 {
@@ -2247,8 +2223,19 @@ void ClientEnvironment::addActiveObject(u16 id, u8 type,
        
        obj->setId(id);
 
-       obj->initialize(init_data);
-       
+       try
+       {
+               obj->initialize(init_data);
+       }
+       catch(SerializationError &e)
+       {
+               errorstream<<"ClientEnvironment::addActiveObject():"
+                               <<" id="<<id<<" type="<<type
+                               <<": SerializationError in initialize(),"
+                               <<" init_data="<<serializeJsonString(init_data)
+                               <<std::endl;
+       }
+
        addActiveObject(obj);
 }
 
@@ -2279,7 +2266,18 @@ void ClientEnvironment::processActiveObjectMessage(u16 id,
                                <<std::endl;
                return;
        }
-       obj->processMessage(data);
+       try
+       {
+               obj->processMessage(data);
+       }
+       catch(SerializationError &e)
+       {
+               errorstream<<"ClientEnvironment::processActiveObjectMessage():"
+                               <<" id="<<id<<" type="<<obj->getType()
+                               <<" SerializationError in processMessage(),"
+                               <<" message="<<serializeJsonString(data)
+                               <<std::endl;
+       }
 }
 
 /*