Translated using Weblate (Japanese)
[oweals/minetest.git] / src / environment.cpp
index d437dbd5dfe660d96250997af3a19016cf0aa572..eb599668b82333b644d616c89457c729887bd464 100644 (file)
@@ -17,6 +17,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
+#include <fstream>
 #include "environment.h"
 #include "filesys.h"
 #include "porting.h"
@@ -31,7 +32,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "scripting_game.h"
 #include "nodedef.h"
 #include "nodemetadata.h"
-#include "main.h" // For g_settings, g_profiler
 #include "gamedef.h"
 #ifndef SERVER
 #include "clientmap.h"
@@ -39,11 +39,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "mapblock_mesh.h"
 #include "event.h"
 #endif
+#include "server.h"
 #include "daynightratio.h"
 #include "map.h"
 #include "emerge.h"
 #include "util/serialize.h"
-#include "jthread/jmutexautolock.h"
+#include "threading/mutex_auto_lock.h"
 
 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
 
@@ -203,34 +204,34 @@ u32 Environment::getDayNightRatio()
 
 void Environment::setTimeOfDaySpeed(float speed)
 {
-       JMutexAutoLock(this->m_timeofday_lock);
+       MutexAutoLock(this->m_timeofday_lock);
        m_time_of_day_speed = speed;
 }
 
 float Environment::getTimeOfDaySpeed()
 {
-       JMutexAutoLock(this->m_timeofday_lock);
+       MutexAutoLock(this->m_timeofday_lock);
        float retval = m_time_of_day_speed;
        return retval;
 }
 
 void Environment::setTimeOfDay(u32 time)
 {
-       JMutexAutoLock(this->m_time_lock);
+       MutexAutoLock(this->m_time_lock);
        m_time_of_day = time;
        m_time_of_day_f = (float)time / 24000.0;
 }
 
 u32 Environment::getTimeOfDay()
 {
-       JMutexAutoLock(this->m_time_lock);
+       MutexAutoLock(this->m_time_lock);
        u32 retval = m_time_of_day;
        return retval;
 }
 
 float Environment::getTimeOfDayF()
 {
-       JMutexAutoLock(this->m_time_lock);
+       MutexAutoLock(this->m_time_lock);
        float retval = m_time_of_day_f;
        return retval;
 }
@@ -425,6 +426,18 @@ bool ServerEnvironment::line_of_sight(v3f pos1, v3f pos2, float stepsize, v3s16
        return true;
 }
 
+void ServerEnvironment::kickAllPlayers(AccessDeniedCode reason,
+               const std::string &str_reason, bool reconnect)
+{
+       for (std::vector<Player*>::iterator it = m_players.begin();
+                       it != m_players.end();
+                       ++it) {
+               ((Server*)m_gamedef)->DenyAccessVerCompliant((*it)->peer_id,
+                       (*it)->protocol_version, (AccessDeniedCode)reason,
+                       str_reason, reconnect);
+       }
+}
+
 void ServerEnvironment::saveLoadedPlayers()
 {
        std::string players_path = m_path_world + DIR_DELIM "players";
@@ -600,19 +613,19 @@ public:
                                        = abm->getRequiredNeighbors();
                        for(std::set<std::string>::iterator
                                        i = required_neighbors_s.begin();
-                                       i != required_neighbors_s.end(); i++)
+                                       i != required_neighbors_s.end(); ++i)
                        {
                                ndef->getIds(*i, aabm.required_neighbors);
                        }
                        // Trigger contents
                        std::set<std::string> contents_s = abm->getTriggerContents();
                        for(std::set<std::string>::iterator
-                                       i = contents_s.begin(); i != contents_s.end(); i++)
+                                       i = contents_s.begin(); i != contents_s.end(); ++i)
                        {
                                std::set<content_t> ids;
                                ndef->getIds(*i, ids);
                                for(std::set<content_t>::const_iterator k = ids.begin();
-                                               k != ids.end(); k++)
+                                               k != ids.end(); ++k)
                                {
                                        content_t c = *k;
                                        std::map<content_t, std::vector<ActiveABM> >::iterator j;
@@ -681,7 +694,7 @@ public:
                                continue;
 
                        for(std::vector<ActiveABM>::iterator
-                                       i = j->second.begin(); i != j->second.end(); i++) {
+                                       i = j->second.begin(); i != j->second.end(); ++i) {
                                if(myrand() % i->chance != 0)
                                        continue;
 
@@ -759,7 +772,7 @@ void ServerEnvironment::activateBlock(MapBlock *block, u32 additional_dtime)
                MapNode n;
                for(std::map<v3s16, NodeTimer>::iterator
                                i = elapsed_timers.begin();
-                               i != elapsed_timers.end(); i++){
+                               i != elapsed_timers.end(); ++i){
                        n = block->getNodeNoEx(i->first);
                        v3s16 p = i->first + block->getPosRelative();
                        if(m_script->node_on_timer(p,n,i->second.elapsed))
@@ -840,9 +853,8 @@ bool ServerEnvironment::swapNode(v3s16 p, const MapNode &n)
        return true;
 }
 
-std::set<u16> ServerEnvironment::getObjectsInsideRadius(v3f pos, float radius)
+void ServerEnvironment::getObjectsInsideRadius(std::vector<u16> &objects, v3f pos, float radius)
 {
-       std::set<u16> objects;
        for(std::map<u16, ServerActiveObject*>::iterator
                        i = m_active_objects.begin();
                        i != m_active_objects.end(); ++i)
@@ -852,9 +864,8 @@ std::set<u16> ServerEnvironment::getObjectsInsideRadius(v3f pos, float radius)
                v3f objectpos = obj->getBasePosition();
                if(objectpos.getDistanceFrom(pos) > radius)
                        continue;
-               objects.insert(id);
+               objects.push_back(id);
        }
-       return objects;
 }
 
 void ServerEnvironment::clearAllObjects()
@@ -875,7 +886,7 @@ void ServerEnvironment::clearAllObjects()
                        if(block){
                                block->m_static_objects.remove(id);
                                block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                                               "clearAllObjects");
+                                               MOD_REASON_CLEAR_ALL_OBJECTS);
                                obj->m_static_exists = false;
                        }
                }
@@ -954,7 +965,7 @@ void ServerEnvironment::clearAllObjects()
                        block->m_static_objects.m_stored.clear();
                        block->m_static_objects.m_active.clear();
                        block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                                       "clearAllObjects");
+                               MOD_REASON_CLEAR_ALL_OBJECTS);
                        num_objs_cleared += num_stored + num_active;
                        num_blocks_cleared++;
                }
@@ -1141,7 +1152,7 @@ void ServerEnvironment::step(float dtime)
                        // set block to be saved when it is unloaded
                        if(block->getTimestamp() > block->getDiskTimestamp() + 60)
                                block->raiseModified(MOD_STATE_WRITE_AT_UNLOAD,
-                                               "Timestamp older than 60s (step)");
+                                       MOD_REASON_BLOCK_EXPIRED);
 
                        // Run node timers
                        std::map<v3s16, NodeTimer> elapsed_timers =
@@ -1150,7 +1161,7 @@ void ServerEnvironment::step(float dtime)
                                MapNode n;
                                for(std::map<v3s16, NodeTimer>::iterator
                                                i = elapsed_timers.begin();
-                                               i != elapsed_timers.end(); i++){
+                                               i != elapsed_timers.end(); ++i){
                                        n = block->getNodeNoEx(i->first);
                                        p = i->first + block->getPosRelative();
                                        if(m_script->node_on_timer(p,n,i->second.elapsed))
@@ -1240,7 +1251,7 @@ void ServerEnvironment::step(float dtime)
                        // Read messages from object
                        while(!obj->m_messages_out.empty())
                        {
-                               m_active_object_messages.push_back(
+                               m_active_object_messages.push(
                                                obj->m_messages_out.front());
                                obj->m_messages_out.pop();
                        }
@@ -1303,51 +1314,6 @@ u16 ServerEnvironment::addActiveObject(ServerActiveObject *object)
        return id;
 }
 
-#if 0
-bool ServerEnvironment::addActiveObjectAsStatic(ServerActiveObject *obj)
-{
-       assert(obj);
-
-       v3f objectpos = obj->getBasePosition();
-
-       // The block in which the object resides in
-       v3s16 blockpos_o = getNodeBlockPos(floatToInt(objectpos, BS));
-
-       /*
-               Update the static data
-       */
-
-       // Create new static object
-       std::string staticdata = obj->getStaticData();
-       StaticObject s_obj(obj->getType(), objectpos, staticdata);
-       // Add to the block where the object is located in
-       v3s16 blockpos = getNodeBlockPos(floatToInt(objectpos, BS));
-       // Get or generate the block
-       MapBlock *block = m_map->emergeBlock(blockpos);
-
-       bool succeeded = false;
-
-       if(block)
-       {
-               block->m_static_objects.insert(0, s_obj);
-               block->raiseModified(MOD_STATE_WRITE_AT_UNLOAD,
-                               "addActiveObjectAsStatic");
-               succeeded = true;
-       }
-       else{
-               infostream<<"ServerEnvironment::addActiveObjectAsStatic: "
-                               <<"Could not find or generate "
-                               <<"a block for storing static object"<<std::endl;
-               succeeded = false;
-       }
-
-       if(obj->environmentDeletes())
-               delete obj;
-
-       return succeeded;
-}
-#endif
-
 /*
        Finds out what new objects have been added to
        inside a radius around a position
@@ -1458,13 +1424,40 @@ void ServerEnvironment::getRemovedActiveObjects(v3s16 pos, s16 radius,
        }
 }
 
+void ServerEnvironment::setStaticForActiveObjectsInBlock(
+       v3s16 blockpos, bool static_exists, v3s16 static_block)
+{
+       MapBlock *block = m_map->getBlockNoCreateNoEx(blockpos);
+       if (!block)
+               return;
+
+       for (std::map<u16, StaticObject>::iterator
+                       so_it = block->m_static_objects.m_active.begin();
+                       so_it != block->m_static_objects.m_active.end(); ++so_it) {
+               // Get the ServerActiveObject counterpart to this StaticObject
+               std::map<u16, ServerActiveObject *>::iterator ao_it;
+               ao_it = m_active_objects.find(so_it->first);
+               if (ao_it == m_active_objects.end()) {
+                       // If this ever happens, there must be some kind of nasty bug.
+                       errorstream << "ServerEnvironment::setStaticForObjectsInBlock(): "
+                               "Object from MapBlock::m_static_objects::m_active not found "
+                               "in m_active_objects";
+                       continue;
+               }
+
+               ServerActiveObject *sao = ao_it->second;
+               sao->m_static_exists = static_exists;
+               sao->m_static_block  = static_block;
+       }
+}
+
 ActiveObjectMessage ServerEnvironment::getActiveObjectMessage()
 {
        if(m_active_object_messages.empty())
                return ActiveObjectMessage(0);
 
        ActiveObjectMessage message = m_active_object_messages.front();
-       m_active_object_messages.pop_front();
+       m_active_object_messages.pop();
        return message;
 }
 
@@ -1532,7 +1525,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
 
                        if(set_changed)
                                block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                                               "addActiveObjectRaw");
+                                       MOD_REASON_ADD_ACTIVE_OBJECT_RAW);
                } else {
                        v3s16 p = floatToInt(objectpos, BS);
                        errorstream<<"ServerEnvironment::addActiveObjectRaw(): "
@@ -1581,7 +1574,7 @@ void ServerEnvironment::removeRemovedObjects()
                        if (block) {
                                block->m_static_objects.remove(id);
                                block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                                               "removeRemovedObjects/remove");
+                                       MOD_REASON_REMOVE_OBJECTS_REMOVE);
                                obj->m_static_exists = false;
                        } else {
                                infostream<<"Failed to emerge block from which an object to "
@@ -1606,7 +1599,7 @@ void ServerEnvironment::removeRemovedObjects()
                                        block->m_static_objects.m_stored.push_back(i->second);
                                        block->m_static_objects.m_active.erase(id);
                                        block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                                                       "removeRemovedObjects/deactivate");
+                                               MOD_REASON_REMOVE_OBJECTS_DEACTIVATE);
                                }
                        } else {
                                infostream<<"Failed to emerge block from which an object to "
@@ -1692,8 +1685,7 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s)
                // Clear stored list
                block->m_static_objects.m_stored.clear();
                block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                               "stored list cleared in activateObjects due to "
-                               "large amount of objects");
+                       MOD_REASON_TOO_MANY_OBJECTS);
                return;
        }
 
@@ -1814,7 +1806,7 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
                        block->m_static_objects.insert(id, s_obj);
                        obj->m_static_block = blockpos_o;
                        block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                                       "deactivateFarObjects: Static data moved in");
+                               MOD_REASON_STATIC_DATA_ADDED);
 
                        // Delete from block where object was located
                        block = m_map->emergeBlock(old_static_block, false);
@@ -1827,7 +1819,7 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
                        }
                        block->m_static_objects.remove(id);
                        block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                                       "deactivateFarObjects: Static data moved out");
+                               MOD_REASON_STATIC_DATA_REMOVED);
                        continue;
                }
 
@@ -1855,27 +1847,29 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
                        bool stays_in_same_block = false;
                        bool data_changed = true;
 
-                       if(obj->m_static_exists){
-                               if(obj->m_static_block == blockpos_o)
+                       if (obj->m_static_exists) {
+                               if (obj->m_static_block == blockpos_o)
                                        stays_in_same_block = true;
 
                                MapBlock *block = m_map->emergeBlock(obj->m_static_block, false);
 
-                               std::map<u16, StaticObject>::iterator n =
+                               if (block) {
+                                       std::map<u16, StaticObject>::iterator n =
                                                block->m_static_objects.m_active.find(id);
-                               if(n != block->m_static_objects.m_active.end()){
-                                       StaticObject static_old = n->second;
+                                       if (n != block->m_static_objects.m_active.end()) {
+                                               StaticObject static_old = n->second;
 
-                                       float save_movem = obj->getMinimumSavedMovement();
+                                               float save_movem = obj->getMinimumSavedMovement();
 
-                                       if(static_old.data == staticdata_new &&
-                                                       (static_old.pos - objectpos).getLength() < save_movem)
-                                               data_changed = false;
-                               } else {
-                                       errorstream<<"ServerEnvironment::deactivateFarObjects(): "
+                                               if (static_old.data == staticdata_new &&
+                                                               (static_old.pos - objectpos).getLength() < save_movem)
+                                                       data_changed = false;
+                                       } else {
+                                               errorstream<<"ServerEnvironment::deactivateFarObjects(): "
                                                        <<"id="<<id<<" m_static_exists=true but "
                                                        <<"static data doesn't actually exist in "
                                                        <<PP(obj->m_static_block)<<std::endl;
+                                       }
                                }
                        }
 
@@ -1892,8 +1886,7 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
                                        // Only mark block as modified if data changed considerably
                                        if(shall_be_written)
                                                block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                                                               "deactivateFarObjects: Static data "
-                                                               "changed considerably");
+                                                       MOD_REASON_STATIC_DATA_CHANGED);
                                }
                        }
 
@@ -1939,8 +1932,7 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
                                        // Only mark block as modified if data changed considerably
                                        if(shall_be_written)
                                                block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                                                               "deactivateFarObjects: Static data "
-                                                               "changed considerably");
+                                                       MOD_REASON_STATIC_DATA_CHANGED);
 
                                        obj->m_static_exists = true;
                                        obj->m_static_block = block->getPos();
@@ -1995,7 +1987,6 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
        }
 }
 
-
 #ifndef SERVER
 
 #include "clientsimpleobject.h"
@@ -2014,7 +2005,7 @@ ClientEnvironment::ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr,
        m_irr(irr)
 {
        char zero = 0;
-       memset(m_attachements, zero, sizeof(m_attachements));
+       memset(attachement_parent_ids, zero, sizeof(attachement_parent_ids));
 }
 
 ClientEnvironment::~ClientEnvironment()
@@ -2174,15 +2165,6 @@ void ClientEnvironment::step(float dtime)
 
                                        v3f d = d_wanted.normalize() * dl;
                                        speed += d;
-
-#if 0 // old code
-                                       if(speed.X > lplayer->movement_liquid_fluidity + lplayer->movement_liquid_fluidity_smooth)      speed.X -= lplayer->movement_liquid_fluidity_smooth;
-                                       if(speed.X < -lplayer->movement_liquid_fluidity - lplayer->movement_liquid_fluidity_smooth)     speed.X += lplayer->movement_liquid_fluidity_smooth;
-                                       if(speed.Y > lplayer->movement_liquid_fluidity + lplayer->movement_liquid_fluidity_smooth)      speed.Y -= lplayer->movement_liquid_fluidity_smooth;
-                                       if(speed.Y < -lplayer->movement_liquid_fluidity - lplayer->movement_liquid_fluidity_smooth)     speed.Y += lplayer->movement_liquid_fluidity_smooth;
-                                       if(speed.Z > lplayer->movement_liquid_fluidity + lplayer->movement_liquid_fluidity_smooth)      speed.Z -= lplayer->movement_liquid_fluidity_smooth;
-                                       if(speed.Z < -lplayer->movement_liquid_fluidity - lplayer->movement_liquid_fluidity_smooth)     speed.Z += lplayer->movement_liquid_fluidity_smooth;
-#endif
                                }
 
                                lplayer->setSpeed(speed);
@@ -2405,6 +2387,15 @@ void ClientEnvironment::addSimpleObject(ClientSimpleObject *simple)
        m_simple_objects.push_back(simple);
 }
 
+GenericCAO* ClientEnvironment::getGenericCAO(u16 id)
+{
+       ClientActiveObject *obj = getActiveObject(id);
+       if (obj && obj->getType() == ACTIVEOBJECT_TYPE_GENERIC)
+               return (GenericCAO*) obj;
+       else
+               return NULL;
+}
+
 ClientActiveObject* ClientEnvironment::getActiveObject(u16 id)
 {
        std::map<u16, ClientActiveObject*>::iterator n;
@@ -2531,28 +2522,23 @@ void ClientEnvironment::removeActiveObject(u16 id)
        m_active_objects.erase(id);
 }
 
-void ClientEnvironment::processActiveObjectMessage(u16 id,
-               const std::string &data)
+void ClientEnvironment::processActiveObjectMessage(u16 id, const std::string &data)
 {
-       ClientActiveObject* obj = getActiveObject(id);
-       if(obj == NULL)
-       {
-               infostream<<"ClientEnvironment::processActiveObjectMessage():"
-                               <<" got message for id="<<id<<", which doesn't exist."
-                               <<std::endl;
+       ClientActiveObject *obj = getActiveObject(id);
+       if (obj == NULL) {
+               infostream << "ClientEnvironment::processActiveObjectMessage():"
+                       << " got message for id=" << id << ", which doesn't exist."
+                       << std::endl;
                return;
        }
-       try
-       {
+
+       try {
                obj->processMessage(data);
-       }
-       catch(SerializationError &e)
-       {
+       } catch (SerializationError &e) {
                errorstream<<"ClientEnvironment::processActiveObjectMessage():"
-                               <<" id="<<id<<" type="<<obj->getType()
-                               <<" SerializationError in processMessage(),"
-                               <<" message="<<serializeJsonString(data)
-                               <<std::endl;
+                       << " id=" << id << " type=" << obj->getType()
+                       << " SerializationError in processMessage(): " << e.what()
+                       << std::endl;
        }
 }
 
@@ -2576,7 +2562,7 @@ void ClientEnvironment::damageLocalPlayer(u8 damage, bool handle_hp)
        event.type = CEE_PLAYER_DAMAGE;
        event.player_damage.amount = damage;
        event.player_damage.send_to_server = handle_hp;
-       m_client_event_queue.push_back(event);
+       m_client_event_queue.push(event);
 }
 
 void ClientEnvironment::updateLocalPlayerBreath(u16 breath)
@@ -2584,7 +2570,7 @@ void ClientEnvironment::updateLocalPlayerBreath(u16 breath)
        ClientEnvEvent event;
        event.type = CEE_PLAYER_BREATH;
        event.player_breath.amount = breath;
-       m_client_event_queue.push_back(event);
+       m_client_event_queue.push(event);
 }
 
 /*
@@ -2618,7 +2604,7 @@ ClientEnvEvent ClientEnvironment::getClientEvent()
                event.type = CEE_NONE;
        else {
                event = m_client_event_queue.front();
-               m_client_event_queue.pop_front();
+               m_client_event_queue.pop();
        }
        return event;
 }