ServerEnvironment & StaticObject cleanups
authorLoic Blot <loic.blot@unix-experience.fr>
Fri, 9 Mar 2018 07:25:48 +0000 (08:25 +0100)
committerLoïc Blot <nerzhul@users.noreply.github.com>
Fri, 9 Mar 2018 22:27:26 +0000 (23:27 +0100)
* isFreeServerActiveObjectId is now part of ServerEnvironment
* getFreeServerActiveObjectId is now part of ServerEnvironment
* StaticObject constructor now take ServerActiveObject instead of type + string. This permits to remove a big string copy in some code parts

src/serverenvironment.cpp
src/serverenvironment.h
src/staticobject.cpp
src/staticobject.h

index ae0f397eeedfc2cc12f250f2fcc11e360cb911b4..8a26c027288b9529fc878d640571ccd53efadd25 100644 (file)
@@ -1416,26 +1416,34 @@ ServerActiveObject* ServerEnvironment::getActiveObject(u16 id)
        return (n != m_active_objects.end() ? n->second : NULL);
 }
 
-bool isFreeServerActiveObjectId(u16 id, ServerActiveObjectMap &objects)
+/**
+ * Verify if id is a free active object id
+ * @param id
+ * @return true if slot is free
+ */
+bool ServerEnvironment::isFreeServerActiveObjectId(u16 id) const
 {
        if (id == 0)
                return false;
 
-       return objects.find(id) == objects.end();
+       return m_active_objects.find(id) == m_active_objects.end();
 }
 
-u16 getFreeServerActiveObjectId(ServerActiveObjectMap &objects)
+/**
+ * Retrieve the next free activeobject ID
+ * @return free activeobject ID or zero if not free ID found
+ */
+u16 ServerEnvironment::getFreeServerActiveObjectId()
 {
-       //try to reuse id's as late as possible
+       // try to reuse id's as late as possible
        static u16 last_used_id = 0;
        u16 startid = last_used_id;
-       for(;;)
-       {
-               last_used_id ++;
-               if(isFreeServerActiveObjectId(last_used_id, objects))
+       for (;;) {
+               last_used_id++;
+               if (isFreeServerActiveObjectId(last_used_id))
                        return last_used_id;
 
-               if(last_used_id == startid)
+               if (last_used_id == startid)
                        return 0;
        }
 }
@@ -1623,7 +1631,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
 {
        assert(object); // Pre-condition
        if(object->getId() == 0){
-               u16 new_id = getFreeServerActiveObjectId(m_active_objects);
+               u16 new_id = getFreeServerActiveObjectId();
                if(new_id == 0)
                {
                        errorstream<<"ServerEnvironment::addActiveObjectRaw(): "
@@ -1639,7 +1647,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
                        <<"supplied with id "<<object->getId()<<std::endl;
        }
 
-       if(!isFreeServerActiveObjectId(object->getId(), m_active_objects)) {
+       if(!isFreeServerActiveObjectId(object->getId())) {
                errorstream<<"ServerEnvironment::addActiveObjectRaw(): "
                        <<"id is not free ("<<object->getId()<<")"<<std::endl;
                if(object->environmentDeletes())
@@ -1677,9 +1685,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
        {
                // Add static object to active static list of the block
                v3f objectpos = object->getBasePosition();
-               std::string staticdata;
-               object->getStaticData(&staticdata);
-               StaticObject s_obj(object->getType(), objectpos, staticdata);
+               StaticObject s_obj(object, objectpos);
                // Add to the block where the object is located in
                v3s16 blockpos = getNodeBlockPos(floatToInt(objectpos, BS));
                MapBlock *block = m_map->emergeBlock(blockpos);
@@ -1929,9 +1935,7 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete)
                        // Delete from block where object was located
                        deleteStaticFromBlock(obj, id, MOD_REASON_STATIC_DATA_REMOVED, false);
 
-                       std::string staticdata_new;
-                       obj->getStaticData(&staticdata_new);
-                       StaticObject s_obj(obj->getType(), objectpos, staticdata_new);
+                       StaticObject s_obj(obj, objectpos);
                        // Save to block where object is located
                        saveStaticToBlock(blockpos_o, id, obj, s_obj, MOD_REASON_STATIC_DATA_ADDED);
 
@@ -1952,12 +1956,9 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete)
                /*
                        Update the static data
                */
-               if(obj->isStaticAllowed())
-               {
+               if (obj->isStaticAllowed()) {
                        // Create new static object
-                       std::string staticdata_new;
-                       obj->getStaticData(&staticdata_new);
-                       StaticObject s_obj(obj->getType(), objectpos, staticdata_new);
+                       StaticObject s_obj(obj, objectpos);
 
                        bool stays_in_same_block = false;
                        bool data_changed = true;
@@ -1977,7 +1978,7 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete)
 
                                                float save_movem = obj->getMinimumSavedMovement();
 
-                                               if (static_old.data == staticdata_new &&
+                                               if (static_old.data == s_obj.data &&
                                                        (static_old.pos - objectpos).getLength() < save_movem)
                                                        data_changed = false;
                                        } else {
index d5165833096007c5b1634e35da020c7296a6ee38..ee5e8d8577a9ac41cab1edd3fe662292bac5ddc1 100644 (file)
@@ -257,6 +257,19 @@ public:
        */
        u16 addActiveObject(ServerActiveObject *object);
 
+       /**
+        * Verify if id is a free active object id
+        * @param id
+        * @return true if slot is free
+        */
+       bool isFreeServerActiveObjectId(u16 id) const;
+
+       /**
+        * Retrieve the next free activeobject ID
+        * @return free activeobject ID or zero if not free ID found
+        */
+       u16 getFreeServerActiveObjectId();
+
        /*
                Add an active object as a static object to the corresponding
                MapBlock.
index 6ad2e1f940f64bea30c71df828a352049b52db13..b331ac2f2b7557d8c84bd6432509a352ac779db9 100644 (file)
@@ -19,7 +19,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #include "staticobject.h"
 #include "util/serialize.h"
-#include "log.h"
+#include "content_sao.h"
+
+StaticObject::StaticObject(const ServerActiveObject *s_obj, const v3f &pos_):
+       type(s_obj->getType()),
+       pos(pos_)
+{
+       s_obj->getStaticData(&data);
+}
 
 void StaticObject::serialize(std::ostream &os)
 {
index 43f542632298a981ec2c3989f0c69009511f6508..6fb486193c9790fc2727c7283d959e808fe0107b 100644 (file)
@@ -26,6 +26,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <map>
 #include "debug.h"
 
+class ServerActiveObject;
+
 struct StaticObject
 {
        u8 type = 0;
@@ -33,12 +35,7 @@ struct StaticObject
        std::string data;
 
        StaticObject() = default;
-       StaticObject(u8 type_, const v3f &pos_, const std::string &data_):
-               type(type_),
-               pos(pos_),
-               data(data_)
-       {
-       }
+       StaticObject(const ServerActiveObject *s_obj, const v3f &pos_);
 
        void serialize(std::ostream &os);
        void deSerialize(std::istream &is, u8 version);