continued.
[oweals/minetest.git] / src / environment.cpp
index 412bb33e8da21be44444b2a6e76fae375e82db68..7c236c3556a54324eca746e5980a43c8078df006 100644 (file)
@@ -1,3 +1,22 @@
+/*
+Minetest-c55
+Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
 #include "environment.h"
 #include "main.h" // g_device for timing debug
 
@@ -5,6 +24,7 @@ Environment::Environment(Map *map, std::ostream &dout):
                m_dout(dout)
 {
        m_map = map;
+       m_daynight_ratio = 0.2;
 }
 
 Environment::~Environment()
@@ -16,7 +36,9 @@ Environment::~Environment()
                delete (*i);
        }
        
-       delete m_map;
+       // The map is removed by the SceneManager
+       m_map->drop();
+       //delete m_map;
 }
 
 void Environment::step(float dtime)
@@ -81,12 +103,31 @@ void Environment::step(float dtime)
                                i != m_players.end(); i++)
                {
                        Player *player = *i;
+
+                       v3f playerpos = player->getPosition();
                        
-                       // Apply gravity to local player
+                       // Apply physics to local player
                        if(player->isLocal())
                        {
+                               // Apply gravity to local player
                                v3f speed = player->getSpeed();
                                speed.Y -= 9.81 * BS * dtime_part * 2;
+
+                               /*
+                                       Apply water resistance
+                               */
+                               if(player->in_water)
+                               {
+                                       f32 max_down = 1.0*BS;
+                                       if(speed.Y < -max_down) speed.Y = -max_down;
+
+                                       f32 max = 2.0*BS;
+                                       if(speed.getLength() > max)
+                                       {
+                                               speed = speed / speed.getLength() * max;
+                                       }
+                               }
+
                                player->setSpeed(speed);
                        }
 
@@ -96,34 +137,44 @@ void Environment::step(float dtime)
                        */
                        player->move(dtime_part, *m_map);
                        
+                       /*
+                               Update lighting on remote players on client
+                       */
+                       u8 light = LIGHT_MAX;
+                       try{
+                               // Get node at feet
+                               v3s16 p = floatToInt(playerpos + v3f(0,BS/4,0));
+                               MapNode n = m_map->getNode(p);
+                               light = n.getLightBlend(m_daynight_ratio);
+                       }
+                       catch(InvalidPositionException &e) {}
+                       player->updateLight(light);
+
                        /*
                                Add footsteps to grass
                        */
-                       //TimeTaker footsteptimer("footstep", g_device);
-                       // 0ms
-                       v3f playerpos = player->getPosition();
                        // Get node that is at BS/4 under player
                        v3s16 bottompos = floatToInt(playerpos + v3f(0,-BS/4,0));
                        try{
                                MapNode n = m_map->getNode(bottompos);
-                               if(n.d == MATERIAL_GRASS)
+                               if(n.d == CONTENT_GRASS)
                                {
-                                       n.d = MATERIAL_GRASS_FOOTSTEPS;
+                                       n.d = CONTENT_GRASS_FOOTSTEPS;
                                        m_map->setNode(bottompos, n);
-
+#ifndef SERVER
                                        // Update mesh on client
                                        if(m_map->mapType() == MAPTYPE_CLIENT)
                                        {
                                                v3s16 p_blocks = getNodeBlockPos(bottompos);
                                                MapBlock *b = m_map->getBlockNoCreate(p_blocks);
-                                               b->updateMesh();
+                                               b->updateMesh(m_daynight_ratio);
                                        }
+#endif
                                }
                        }
                        catch(InvalidPositionException &e)
                        {
                        }
-                       //footsteptimer.stop();
                }
        }
        while(dtime > 0.001);
@@ -140,7 +191,9 @@ void Environment::addPlayer(Player *player)
 {
        DSTACK(__FUNCTION_NAME);
        //Check that only one local player exists and peer_ids are unique
+#ifndef SERVER
        assert(player->isLocal() == false || getLocalPlayer() == NULL);
+#endif
        assert(getPlayer(player->peer_id) == NULL);
        m_players.push_back(player);
 }
@@ -164,6 +217,7 @@ re_search:
        }
 }
 
+#ifndef SERVER
 LocalPlayer * Environment::getLocalPlayer()
 {
        for(core::list<Player*>::Iterator i = m_players.begin();
@@ -175,6 +229,7 @@ LocalPlayer * Environment::getLocalPlayer()
        }
        return NULL;
 }
+#endif
 
 Player * Environment::getPlayer(u16 peer_id)
 {
@@ -204,3 +259,25 @@ void Environment::printPlayers(std::ostream &o)
        }
 }
 
+#ifndef SERVER
+void Environment::updateMeshes(v3s16 blockpos)
+{
+       m_map->updateMeshes(blockpos, m_daynight_ratio);
+}
+
+void Environment::expireMeshes(bool only_daynight_diffed)
+{
+       m_map->expireMeshes(only_daynight_diffed);
+}
+#endif
+
+void Environment::setDayNightRatio(u32 r)
+{
+       m_daynight_ratio = r;
+}
+
+u32 Environment::getDayNightRatio()
+{
+       return m_daynight_ratio;
+}
+