]> git.lizzy.rs Git - minetest.git/blobdiff - src/environment.cpp
Replace std::list by std::vector into ClientMap::updateDrawList, Map::timerUpdate...
[minetest.git] / src / environment.cpp
index c8af72f6b96c23ff8fa5edaf8bdc876816bebd8a..e805a59a5a453e75e41531fa0b8d27c06f569dec 100644 (file)
@@ -36,6 +36,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #ifndef SERVER
 #include "clientmap.h"
 #include "localplayer.h"
+#include "mapblock_mesh.h"
 #include "event.h"
 #endif
 #include "daynightratio.h"
@@ -54,6 +55,7 @@ Environment::Environment():
        m_enable_day_night_ratio_override(false),
        m_day_night_ratio_override(0.0f)
 {
+       m_cache_enable_shaders = g_settings->getBool("enable_shaders");
 }
 
 Environment::~Environment()
@@ -205,8 +207,7 @@ u32 Environment::getDayNightRatio()
 {
        if(m_enable_day_night_ratio_override)
                return m_day_night_ratio_override;
-       bool smooth = g_settings->getBool("enable_shaders");
-       return time_to_daynight_ratio(m_time_of_day_f*24000, smooth);
+       return time_to_daynight_ratio(m_time_of_day_f*24000, m_cache_enable_shaders);
 }
 
 void Environment::setTimeOfDaySpeed(float speed)
@@ -449,11 +450,11 @@ Player *ServerEnvironment::loadPlayer(const std::string &playername)
        bool newplayer = false;
        bool found = false;
        if (!player) {
-               player = new RemotePlayer(m_gamedef);
+               player = new RemotePlayer(m_gamedef, playername.c_str());
                newplayer = true;
        }
 
-       RemotePlayer testplayer(m_gamedef);
+       RemotePlayer testplayer(m_gamedef, "");
        std::string path = players_path + playername;
        for (u32 i = 0; i < PLAYER_FILE_ALTERNATE_TRIES; i++) {
                // Open file and deserialize
@@ -478,6 +479,7 @@ Player *ServerEnvironment::loadPlayer(const std::string &playername)
        if (newplayer) {
                addPlayer(player);
        }
+       player->setModified(false);
        return player;
 }
 
@@ -508,38 +510,29 @@ void ServerEnvironment::loadMeta()
 
        // Open file and deserialize
        std::ifstream is(path.c_str(), std::ios_base::binary);
-       if(is.good() == false)
-       {
-               infostream<<"ServerEnvironment::loadMeta(): Failed to open "
-                               <<path<<std::endl;
+       if (!is.good()) {
+               infostream << "ServerEnvironment::loadMeta(): Failed to open "
+                               << path << std::endl;
                throw SerializationError("Couldn't load env meta");
        }
 
        Settings args;
-       
-       for(;;)
-       {
-               if(is.eof())
-                       throw SerializationError
-                                       ("ServerEnvironment::loadMeta(): EnvArgsEnd not found");
-               std::string line;
-               std::getline(is, line);
-               std::string trimmedline = trim(line);
-               if(trimmedline == "EnvArgsEnd")
-                       break;
-               args.parseConfigLine(line);
+
+       if (!args.parseConfigLines(is, "EnvArgsEnd")) {
+               throw SerializationError("ServerEnvironment::loadMeta(): "
+                               "EnvArgsEnd not found!");
        }
-       
-       try{
+
+       try {
                m_game_time = args.getU64("game_time");
-       }catch(SettingNotFoundException &e){
+       } catch (SettingNotFoundException &e) {
                // Getting this is crucial, otherwise timestamps are useless
                throw SerializationError("Couldn't load env meta game_time");
        }
 
-       try{
+       try {
                m_time_of_day = args.getU64("time_of_day");
-       }catch(SettingNotFoundException &e){
+       } catch (SettingNotFoundException &e) {
                // This is not as important
                m_time_of_day = 9000;
        }
@@ -858,11 +851,10 @@ void ServerEnvironment::clearAllObjects()
 {
        infostream<<"ServerEnvironment::clearAllObjects(): "
                        <<"Removing all active objects"<<std::endl;
-       std::list<u16> objects_to_remove;
+       std::vector<u16> objects_to_remove;
        for(std::map<u16, ServerActiveObject*>::iterator
                        i = m_active_objects.begin();
-                       i != m_active_objects.end(); ++i)
-       {
+                       i != m_active_objects.end(); ++i) {
                ServerActiveObject* obj = i->second;
                if(obj->getType() == ACTIVEOBJECT_TYPE_PLAYER)
                        continue;
@@ -895,15 +887,15 @@ void ServerEnvironment::clearAllObjects()
                // Id to be removed from m_active_objects
                objects_to_remove.push_back(id);
        }
+
        // Remove references from m_active_objects
-       for(std::list<u16>::iterator i = objects_to_remove.begin();
-                       i != objects_to_remove.end(); ++i)
-       {
+       for(std::vector<u16>::iterator i = objects_to_remove.begin();
+                       i != objects_to_remove.end(); ++i) {
                m_active_objects.erase(*i);
        }
 
        // Get list of loaded blocks
-       std::list<v3s16> loaded_blocks;
+       std::vector<v3s16> loaded_blocks;
        infostream<<"ServerEnvironment::clearAllObjects(): "
                        <<"Listing all loaded blocks"<<std::endl;
        m_map->listAllLoadedBlocks(loaded_blocks);
@@ -912,7 +904,7 @@ void ServerEnvironment::clearAllObjects()
                        <<loaded_blocks.size()<<std::endl;
 
        // Get list of loadable blocks
-       std::list<v3s16> loadable_blocks;
+       std::vector<v3s16> loadable_blocks;
        infostream<<"ServerEnvironment::clearAllObjects(): "
                        <<"Listing all loadable blocks"<<std::endl;
        m_map->listAllLoadableBlocks(loadable_blocks);
@@ -922,9 +914,8 @@ void ServerEnvironment::clearAllObjects()
                        <<", now clearing"<<std::endl;
 
        // Grab a reference on each loaded block to avoid unloading it
-       for(std::list<v3s16>::iterator i = loaded_blocks.begin();
-                       i != loaded_blocks.end(); ++i)
-       {
+       for(std::vector<v3s16>::iterator i = loaded_blocks.begin();
+                       i != loaded_blocks.end(); ++i) {
                v3s16 p = *i;
                MapBlock *block = m_map->getBlockNoCreateNoEx(p);
                assert(block);
@@ -938,9 +929,8 @@ void ServerEnvironment::clearAllObjects()
        u32 num_blocks_checked = 0;
        u32 num_blocks_cleared = 0;
        u32 num_objs_cleared = 0;
-       for(std::list<v3s16>::iterator i = loadable_blocks.begin();
-                       i != loadable_blocks.end(); ++i)
-       {
+       for(std::vector<v3s16>::iterator i = loadable_blocks.begin();
+                       i != loadable_blocks.end(); ++i) {
                v3s16 p = *i;
                MapBlock *block = m_map->emergeBlock(p, false);
                if(!block){
@@ -976,9 +966,8 @@ void ServerEnvironment::clearAllObjects()
        m_map->unloadUnreferencedBlocks();
 
        // Drop references that were added above
-       for(std::list<v3s16>::iterator i = loaded_blocks.begin();
-                       i != loaded_blocks.end(); ++i)
-       {
+       for(std::vector<v3s16>::iterator i = loaded_blocks.begin();
+                       i != loaded_blocks.end(); ++i) {
                v3s16 p = *i;
                MapBlock *block = m_map->getBlockNoCreateNoEx(p);
                assert(block);
@@ -1233,11 +1222,6 @@ void ServerEnvironment::step(float dtime)
                                i != m_active_objects.end(); ++i)
                {
                        ServerActiveObject* obj = i->second;
-                       // Remove non-peaceful mobs on peaceful mode
-                       if(g_settings->getBool("only_peaceful_mobs")){
-                               if(!obj->isPeaceful())
-                                       obj->m_removed = true;
-                       }
                        // Don't step if is to be removed or stored statically
                        if(obj->m_removed || obj->m_pending_deactivation)
                                continue;
@@ -1358,11 +1342,17 @@ bool ServerEnvironment::addActiveObjectAsStatic(ServerActiveObject *obj)
        inside a radius around a position
 */
 void ServerEnvironment::getAddedActiveObjects(v3s16 pos, s16 radius,
+               s16 player_radius,
                std::set<u16> &current_objects,
                std::set<u16> &added_objects)
 {
        v3f pos_f = intToFloat(pos, BS);
        f32 radius_f = radius * BS;
+       f32 player_radius_f = player_radius * BS;
+
+       if (player_radius_f < 0)
+               player_radius_f = 0;
+
        /*
                Go through the object list,
                - discard m_removed objects,
@@ -1382,12 +1372,15 @@ void ServerEnvironment::getAddedActiveObjects(v3s16 pos, s16 radius,
                // Discard if removed or deactivating
                if(object->m_removed || object->m_pending_deactivation)
                        continue;
-               if(object->unlimitedTransferDistance() == false){
+
+               f32 distance_f = object->getBasePosition().getDistanceFrom(pos_f);
+               if (object->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
                        // Discard if too far
-                       f32 distance_f = object->getBasePosition().getDistanceFrom(pos_f);
-                       if(distance_f > radius_f)
+                       if (distance_f > player_radius_f && player_radius_f != 0)
                                continue;
-               }
+               } else if (distance_f > radius_f)
+                       continue;
+
                // Discard if already on current_objects
                std::set<u16>::iterator n;
                n = current_objects.find(id);
@@ -1403,11 +1396,17 @@ void ServerEnvironment::getAddedActiveObjects(v3s16 pos, s16 radius,
        inside a radius around a position
 */
 void ServerEnvironment::getRemovedActiveObjects(v3s16 pos, s16 radius,
+               s16 player_radius,
                std::set<u16> &current_objects,
                std::set<u16> &removed_objects)
 {
        v3f pos_f = intToFloat(pos, BS);
        f32 radius_f = radius * BS;
+       f32 player_radius_f = player_radius * BS;
+
+       if (player_radius_f < 0)
+               player_radius_f = 0;
+
        /*
                Go through current_objects; object is removed if:
                - object is not found in m_active_objects (this is actually an
@@ -1436,19 +1435,15 @@ void ServerEnvironment::getRemovedActiveObjects(v3s16 pos, s16 radius,
                        continue;
                }
                
-               // If transfer distance is unlimited, don't remove
-               if(object->unlimitedTransferDistance())
-                       continue;
-
                f32 distance_f = object->getBasePosition().getDistanceFrom(pos_f);
-
-               if(distance_f >= radius_f)
-               {
-                       removed_objects.insert(id);
+               if (object->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
+                       if (distance_f <= player_radius_f || player_radius_f == 0)
+                               continue;
+               } else if (distance_f <= radius_f)
                        continue;
-               }
-               
-               // Not removed
+
+               // Object is no longer visible
+               removed_objects.insert(id);
        }
 }
 
@@ -1543,11 +1538,10 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
 */
 void ServerEnvironment::removeRemovedObjects()
 {
-       std::list<u16> objects_to_remove;
+       std::vector<u16> objects_to_remove;
        for(std::map<u16, ServerActiveObject*>::iterator
                        i = m_active_objects.begin();
-                       i != m_active_objects.end(); ++i)
-       {
+                       i != m_active_objects.end(); ++i) {
                u16 id = i->first;
                ServerActiveObject* obj = i->second;
                // This shouldn't happen but check it
@@ -1617,13 +1611,13 @@ void ServerEnvironment::removeRemovedObjects()
                // Delete
                if(obj->environmentDeletes())
                        delete obj;
+
                // Id to be removed from m_active_objects
                objects_to_remove.push_back(id);
        }
        // Remove references from m_active_objects
-       for(std::list<u16>::iterator i = objects_to_remove.begin();
-                       i != objects_to_remove.end(); ++i)
-       {
+       for(std::vector<u16>::iterator i = objects_to_remove.begin();
+                       i != objects_to_remove.end(); ++i) {
                m_active_objects.erase(*i);
        }
 }
@@ -1667,10 +1661,11 @@ static void print_hexdump(std::ostream &o, const std::string &data)
 */
 void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s)
 {
-       if(block==NULL)
+       if(block == NULL)
                return;
+
        // Ignore if no stored objects (to not set changed flag)
-       if(block->m_static_objects.m_stored.size() == 0)
+       if(block->m_static_objects.m_stored.empty())
                return;
        verbosestream<<"ServerEnvironment::activateObjects(): "
                        <<"activating objects of block "<<PP(block->getPos())
@@ -1694,17 +1689,14 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s)
        std::list<StaticObject> new_stored;
        for(std::list<StaticObject>::iterator
                        i = block->m_static_objects.m_stored.begin();
-                       i != block->m_static_objects.m_stored.end(); ++i)
-       {
-               /*infostream<<"Server: Creating an active object from "
-                               <<"static data"<<std::endl;*/
+                       i != block->m_static_objects.m_stored.end(); ++i) {
                StaticObject &s_obj = *i;
+
                // Create an active object from the data
                ServerActiveObject *obj = ServerActiveObject::create
-                               (s_obj.type, this, 0, s_obj.pos, s_obj.data);
+                               ((ActiveObjectType) s_obj.type, this, 0, s_obj.pos, s_obj.data);
                // If couldn't create object, store static data back.
-               if(obj==NULL)
-               {
+               if(obj == NULL) {
                        errorstream<<"ServerEnvironment::activateObjects(): "
                                        <<"failed to create active object from static object "
                                        <<"in block "<<PP(s_obj.pos/BS)
@@ -2332,21 +2324,26 @@ void ClientEnvironment::step(float dtime)
                        player->move(dtime, this, 100*BS);
 
                }
-               
-               // Update lighting on all players on client
-               float light = 1.0;
-               try{
-                       // Get node at head
-                       v3s16 p = player->getLightPosition();
-                       MapNode n = m_map->getNode(p);
-                       light = n.getLightBlendF1((float)getDayNightRatio()/1000, m_gamedef->ndef());
-               }
-               catch(InvalidPositionException &e){
-                       light = blend_light_f1((float)getDayNightRatio()/1000, LIGHT_SUN, 0);
-               }
-               player->light = light;
        }
-       
+
+       // Update lighting on local player (used for wield item)
+       u32 day_night_ratio = getDayNightRatio();
+       {
+               // Get node at head
+
+               // On InvalidPositionException, use this as default
+               // (day: LIGHT_SUN, night: 0)
+               MapNode node_at_lplayer(CONTENT_AIR, 0x0f, 0);
+
+               v3s16 p = lplayer->getLightPosition();
+               node_at_lplayer = m_map->getNodeNoEx(p);
+
+               u16 light = getInteriorLight(node_at_lplayer, 0, m_gamedef->ndef());
+               u8 day = light & 0xff;
+               u8 night = (light >> 8) & 0xff;
+               finalColorBlend(lplayer->light_color, day, night, day_night_ratio);
+       }
+
        /*
                Step active objects and update lighting of them
        */
@@ -2365,15 +2362,16 @@ void ClientEnvironment::step(float dtime)
                {
                        // Update lighting
                        u8 light = 0;
-                       try{
-                               // Get node at head
-                               v3s16 p = obj->getLightPosition();
-                               MapNode n = m_map->getNode(p);
-                               light = n.getLightBlend(getDayNightRatio(), m_gamedef->ndef());
-                       }
-                       catch(InvalidPositionException &e){
-                               light = blend_light(getDayNightRatio(), LIGHT_SUN, 0);
-                       }
+                       bool pos_ok;
+
+                       // Get node at head
+                       v3s16 p = obj->getLightPosition();
+                       MapNode n = m_map->getNodeNoEx(p, &pos_ok);
+                       if (pos_ok)
+                               light = n.getLightBlend(day_night_ratio, m_gamedef->ndef());
+                       else
+                               light = blend_light(day_night_ratio, LIGHT_SUN, 0);
+
                        obj->updateLight(light);
                }
        }
@@ -2464,15 +2462,16 @@ u16 ClientEnvironment::addActiveObject(ClientActiveObject *object)
        object->addToScene(m_smgr, m_texturesource, m_irr);
        { // Update lighting immediately
                u8 light = 0;
-               try{
-                       // Get node at head
-                       v3s16 p = object->getLightPosition();
-                       MapNode n = m_map->getNode(p);
+               bool pos_ok;
+
+               // Get node at head
+               v3s16 p = object->getLightPosition();
+               MapNode n = m_map->getNodeNoEx(p, &pos_ok);
+               if (pos_ok)
                        light = n.getLightBlend(getDayNightRatio(), m_gamedef->ndef());
-               }
-               catch(InvalidPositionException &e){
+               else
                        light = blend_light(getDayNightRatio(), LIGHT_SUN, 0);
-               }
+
                object->updateLight(light);
        }
        return object->getId();
@@ -2482,7 +2481,7 @@ void ClientEnvironment::addActiveObject(u16 id, u8 type,
                const std::string &init_data)
 {
        ClientActiveObject* obj =
-                       ClientActiveObject::create(type, m_gamedef, this);
+                       ClientActiveObject::create((ActiveObjectType) type, m_gamedef, this);
        if(obj == NULL)
        {
                infostream<<"ClientEnvironment::addActiveObject(): "
@@ -2561,6 +2560,8 @@ void ClientEnvironment::damageLocalPlayer(u8 damage, bool handle_hp)
        assert(lplayer);
        
        if(handle_hp){
+               if (lplayer->hp == 0) // Don't damage a dead player
+                       return;
                if(lplayer->hp > damage)
                        lplayer->hp -= damage;
                else