Performance fix + SAO factorization
authorRogier <rogier777@gmail.com>
Mon, 9 Jan 2017 19:39:45 +0000 (04:39 +0900)
committerNer'zhul <nerzhul@users.noreply.github.com>
Wed, 11 Jan 2017 14:53:56 +0000 (15:53 +0100)
Original credits goes to @Rogier-5

* Merge common attributes between LuaEntitySAO & PlayerSAO to UnitSAO
* Make some functions const
* Improve some lists performance by returning const ref

Signed-off-by: Loic Blot <loic.blot@unix-experience.fr>
src/activeobject.h
src/clientobject.h
src/content_cao.cpp
src/content_cao.h
src/content_sao.cpp
src/content_sao.h
src/script/lua_api/l_object.cpp
src/serverobject.h

index 48f078d3f28183faf37b8a97e157b19667e07b15..fe6c085141df192cccca5b3f37f8c193a505a1e4 100644 (file)
@@ -64,7 +64,7 @@ public:
                m_id(id)
        {
        }
-       
+
        u16 getId()
        {
                return m_id;
@@ -76,8 +76,8 @@ public:
        }
 
        virtual ActiveObjectType getType() const = 0;
-       virtual bool getCollisionBox(aabb3f *toset) = 0;
-       virtual bool collideWithObjects() = 0;
+       virtual bool getCollisionBox(aabb3f *toset) const = 0;
+       virtual bool collideWithObjects() const = 0;
 protected:
        u16 m_id; // 0 is invalid, "no id"
 };
index f0bde0adc438980fb2d21f296c51a5bbe6be5895..1db5bcf24b7f5c26246cb0fec6e591267adf028c 100644 (file)
@@ -47,8 +47,8 @@ public:
        virtual void updateLightNoCheck(u8 light_at_pos){}
        virtual v3s16 getLightPosition(){return v3s16(0,0,0);}
        virtual aabb3f *getSelectionBox() { return NULL; }
-       virtual bool getCollisionBox(aabb3f *toset){return false;}
-       virtual bool collideWithObjects(){return false;}
+       virtual bool getCollisionBox(aabb3f *toset) const { return false; }
+       virtual bool collideWithObjects() const { return false; }
        virtual v3f getPosition(){return v3f(0,0,0);}
        virtual float getYaw() const {return 0;}
        virtual scene::ISceneNode *getSceneNode(){return NULL;}
index a4c0bf14d8bb71f9a01d8cee3b297846391cbfb0..5ddbd27c93885548798a272525e39a4447b207fa 100644 (file)
@@ -159,7 +159,7 @@ public:
 
        void processMessage(const std::string &data);
 
-       bool getCollisionBox(aabb3f *toset) { return false; }
+       bool getCollisionBox(aabb3f *toset) const { return false; }
 private:
        scene::IMeshSceneNode *m_node;
        v3f m_position;
@@ -316,7 +316,7 @@ public:
        std::string infoText()
                {return m_infotext;}
 
-       bool getCollisionBox(aabb3f *toset) { return false; }
+       bool getCollisionBox(aabb3f *toset) const { return false; }
 private:
        aabb3f m_selection_box;
        scene::IMeshSceneNode *m_node;
@@ -587,7 +587,7 @@ GenericCAO::GenericCAO(Client *client, ClientEnvironment *env):
        }
 }
 
-bool GenericCAO::getCollisionBox(aabb3f *toset)
+bool GenericCAO::getCollisionBox(aabb3f *toset) const
 {
        if (m_prop.physical)
        {
index 96a1600554480f1103cfd70d216577be9cb24993..19fecdde53558e7d4fcd0902866ae62a68d022f2 100644 (file)
@@ -130,7 +130,7 @@ public:
 
        ClientActiveObject *getParent();
 
-       bool getCollisionBox(aabb3f *toset);
+       bool getCollisionBox(aabb3f *toset) const;
 
        bool collideWithObjects();
 
index dd8bdc592588a23754956427c067f82f8445b9e1..c403a36732be087aa19bba39c6adba56a958af7a 100644 (file)
@@ -19,11 +19,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #include "content_sao.h"
 #include "util/serialize.h"
-#include "util/mathconstants.h"
 #include "collision.h"
 #include "environment.h"
-#include "settings.h"
-#include "serialization.h" // For compressZlib
 #include "tool.h" // For ToolCapabilities
 #include "gamedef.h"
 #include "nodedef.h"
@@ -31,7 +28,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "server.h"
 #include "scripting_game.h"
 #include "genericobject.h"
-#include "log.h"
 
 std::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types;
 
@@ -94,13 +90,8 @@ public:
                }
        }
 
-       bool getCollisionBox(aabb3f *toset) {
-               return false;
-       }
-
-       bool collideWithObjects() {
-               return false;
-       }
+       bool getCollisionBox(aabb3f *toset) const { return false; }
+       bool collideWithObjects() const { return false; }
 
 private:
        float m_timer1;
@@ -110,6 +101,28 @@ private:
 // Prototype (registers item for deserialization)
 TestSAO proto_TestSAO(NULL, v3f(0,0,0));
 
+/*
+       UnitSAO
+ */
+
+UnitSAO::UnitSAO(ServerEnvironment *env, v3f pos):
+       ServerActiveObject(env, pos),
+       m_hp(-1),
+       m_yaw(0),
+       m_properties_sent(true),
+       m_armor_groups_sent(false),
+       m_animation_range(0,0),
+       m_animation_speed(0),
+       m_animation_blend(0),
+       m_animation_loop(true),
+       m_animation_sent(false),
+       m_bone_position_sent(false),
+       m_attachment_parent_id(0),
+       m_attachment_sent(false)
+{
+       // Initialize something to armor groups
+       m_armor_groups["fleshy"] = 100;
+}
 /*
        LuaEntitySAO
 */
@@ -125,29 +138,17 @@ LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos,
        m_registered(false),
        m_velocity(0,0,0),
        m_acceleration(0,0,0),
-       m_properties_sent(true),
        m_last_sent_yaw(0),
        m_last_sent_position(0,0,0),
        m_last_sent_velocity(0,0,0),
        m_last_sent_position_timer(0),
-       m_last_sent_move_precision(0),
-       m_armor_groups_sent(false),
-       m_animation_speed(0),
-       m_animation_blend(0),
-       m_animation_loop(true),
-       m_animation_sent(false),
-       m_bone_position_sent(false),
-       m_attachment_parent_id(0),
-       m_attachment_sent(false)
+       m_last_sent_move_precision(0)
 {
        // Only register type if no environment supplied
        if(env == NULL){
                ServerActiveObject::registerType(getType(), create);
                return;
        }
-
-       // Initialize something to armor groups
-       m_armor_groups["fleshy"] = 100;
 }
 
 LuaEntitySAO::~LuaEntitySAO()
@@ -565,7 +566,7 @@ void LuaEntitySAO::setArmorGroups(const ItemGroupList &armor_groups)
        m_armor_groups_sent = false;
 }
 
-ItemGroupList LuaEntitySAO::getArmorGroups()
+const ItemGroupList &LuaEntitySAO::getArmorGroups()
 {
        return m_armor_groups;
 }
@@ -635,7 +636,7 @@ void LuaEntitySAO::removeAttachmentChild(int child_id)
        m_attachment_child_ids.erase(child_id);
 }
 
-UNORDERED_SET<int> LuaEntitySAO::getAttachmentChildIds()
+const UNORDERED_SET<int> &LuaEntitySAO::getAttachmentChildIds()
 {
        return m_attachment_child_ids;
 }
@@ -732,7 +733,8 @@ void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
        m_messages_out.push(aom);
 }
 
-bool LuaEntitySAO::getCollisionBox(aabb3f *toset) {
+bool LuaEntitySAO::getCollisionBox(aabb3f *toset) const
+{
        if (m_prop.physical)
        {
                //update collision box
@@ -748,7 +750,8 @@ bool LuaEntitySAO::getCollisionBox(aabb3f *toset) {
        return false;
 }
 
-bool LuaEntitySAO::collideWithObjects(){
+bool LuaEntitySAO::collideWithObjects() const
+{
        return m_prop.collideWithObjects;
 }
 
@@ -770,16 +773,7 @@ PlayerSAO::PlayerSAO(ServerEnvironment *env_, u16 peer_id_, bool is_singleplayer
        m_nocheat_dig_time(0),
        m_wield_index(0),
        m_position_not_sent(false),
-       m_armor_groups_sent(false),
-       m_properties_sent(true),
        m_is_singleplayer(is_singleplayer),
-       m_animation_speed(0),
-       m_animation_blend(0),
-       m_animation_loop(true),
-       m_animation_sent(false),
-       m_bone_position_sent(false),
-       m_attachment_parent_id(0),
-       m_attachment_sent(false),
        m_breath(PLAYER_MAX_BREATH),
        m_pitch(0),
        m_fov(0),
@@ -793,7 +787,6 @@ PlayerSAO::PlayerSAO(ServerEnvironment *env_, u16 peer_id_, bool is_singleplayer
        m_physics_override_sent(false)
 {
        assert(m_peer_id != 0); // pre-condition
-       m_armor_groups["fleshy"] = 100;
 
        m_prop.hp_max = PLAYER_MAX_HP;
        m_prop.physical = false;
@@ -828,6 +821,11 @@ void PlayerSAO::initialize(RemotePlayer *player, const std::set<std::string> &pr
        m_inventory = &m_player->inventory;
 }
 
+v3f PlayerSAO::getEyeOffset() const
+{
+       return v3f(0, BS * 1.625f, 0);
+}
+
 std::string PlayerSAO::getDescription()
 {
        return std::string("player ") + m_player->getName();
@@ -1282,7 +1280,7 @@ void PlayerSAO::setArmorGroups(const ItemGroupList &armor_groups)
        m_armor_groups_sent = false;
 }
 
-ItemGroupList PlayerSAO::getArmorGroups()
+const ItemGroupList &PlayerSAO::getArmorGroups()
 {
        return m_armor_groups;
 }
@@ -1354,7 +1352,7 @@ void PlayerSAO::removeAttachmentChild(int child_id)
        m_attachment_child_ids.erase(child_id);
 }
 
-UNORDERED_SET<int> PlayerSAO::getAttachmentChildIds()
+const UNORDERED_SET<int> &PlayerSAO::getAttachmentChildIds()
 {
        return m_attachment_child_ids;
 }
@@ -1512,15 +1510,10 @@ bool PlayerSAO::checkMovementCheat()
        return cheated;
 }
 
-bool PlayerSAO::getCollisionBox(aabb3f *toset)
+bool PlayerSAO::getCollisionBox(aabb3f *toset) const
 {
        *toset = aabb3f(-BS * 0.30, 0.0, -BS * 0.30, BS * 0.30, BS * 1.75, BS * 0.30);
        toset->MinEdge += m_base_position;
        toset->MaxEdge += m_base_position;
        return true;
 }
-
-bool PlayerSAO::collideWithObjects()
-{
-       return true;
-}
index 9c66068b308e3fdadf7683818025d00dce8da13b..d5e9b2cbf1165d823339b416d2a47c6b046da0d3 100644 (file)
@@ -24,14 +24,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "serverobject.h"
 #include "itemgroup.h"
 #include "object_properties.h"
-#include "constants.h"
 
 class UnitSAO: public ServerActiveObject
 {
 public:
-       UnitSAO(ServerEnvironment *env, v3f pos):
-                       ServerActiveObject(env, pos),
-                       m_hp(-1), m_yaw(0) {}
+       UnitSAO(ServerEnvironment *env, v3f pos);
        virtual ~UnitSAO() {}
 
        virtual void setYaw(const float yaw) { m_yaw = yaw; }
@@ -46,6 +43,29 @@ public:
 protected:
        s16 m_hp;
        float m_yaw;
+
+       bool m_properties_sent;
+       struct ObjectProperties m_prop;
+
+       ItemGroupList m_armor_groups;
+       bool m_armor_groups_sent;
+
+       v2f m_animation_range;
+       float m_animation_speed;
+       float m_animation_blend;
+       bool m_animation_loop;
+       bool m_animation_sent;
+
+       // Stores position and rotation for each bone name
+       UNORDERED_MAP<std::string, core::vector2d<v3f> > m_bone_position;
+       bool m_bone_position_sent;
+
+       int m_attachment_parent_id;
+       UNORDERED_SET<int> m_attachment_child_ids;
+       std::string m_attachment_bone;
+       v3f m_attachment_position;
+       v3f m_attachment_rotation;
+       bool m_attachment_sent;
 };
 
 /*
@@ -81,7 +101,7 @@ public:
        void setHP(s16 hp);
        s16 getHP() const;
        void setArmorGroups(const ItemGroupList &armor_groups);
-       ItemGroupList getArmorGroups();
+       const ItemGroupList &getArmorGroups();
        void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
        void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
        void setBonePosition(const std::string &bone, v3f position, v3f rotation);
@@ -90,7 +110,7 @@ public:
        void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation);
        void addAttachmentChild(int child_id);
        void removeAttachmentChild(int child_id);
-       UNORDERED_SET<int> getAttachmentChildIds();
+       const UNORDERED_SET<int> &getAttachmentChildIds();
        ObjectProperties* accessObjectProperties();
        void notifyObjectPropertiesModified();
        /* LuaEntitySAO-specific */
@@ -103,8 +123,8 @@ public:
        void setSprite(v2s16 p, int num_frames, float framelength,
                        bool select_horiz_by_yawpitch);
        std::string getName();
-       bool getCollisionBox(aabb3f *toset);
-       bool collideWithObjects();
+       bool getCollisionBox(aabb3f *toset) const;
+       bool collideWithObjects() const;
 private:
        std::string getPropertyPacket();
        void sendPosition(bool do_interpolate, bool is_movement_end);
@@ -112,36 +132,15 @@ private:
        std::string m_init_name;
        std::string m_init_state;
        bool m_registered;
-       struct ObjectProperties m_prop;
 
        v3f m_velocity;
        v3f m_acceleration;
 
-       ItemGroupList m_armor_groups;
-
-       bool m_properties_sent;
        float m_last_sent_yaw;
        v3f m_last_sent_position;
        v3f m_last_sent_velocity;
        float m_last_sent_position_timer;
        float m_last_sent_move_precision;
-       bool m_armor_groups_sent;
-
-       v2f m_animation_range;
-       float m_animation_speed;
-       float m_animation_blend;
-       bool m_animation_loop;
-       bool m_animation_sent;
-
-       UNORDERED_MAP<std::string, core::vector2d<v3f> > m_bone_position;
-       bool m_bone_position_sent;
-
-       int m_attachment_parent_id;
-       UNORDERED_SET<int> m_attachment_child_ids;
-       std::string m_attachment_bone;
-       v3f m_attachment_position;
-       v3f m_attachment_rotation;
-       bool m_attachment_sent;
 };
 
 /*
@@ -235,7 +234,7 @@ public:
        u16 getBreath() const { return m_breath; }
        void setBreath(const u16 breath, bool send = true);
        void setArmorGroups(const ItemGroupList &armor_groups);
-       ItemGroupList getArmorGroups();
+       const ItemGroupList &getArmorGroups();
        void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
        void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
        void setBonePosition(const std::string &bone, v3f position, v3f rotation);
@@ -244,7 +243,7 @@ public:
        void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation);
        void addAttachmentChild(int child_id);
        void removeAttachmentChild(int child_id);
-       UNORDERED_SET<int> getAttachmentChildIds();
+       const UNORDERED_SET<int> &getAttachmentChildIds();
        ObjectProperties* accessObjectProperties();
        void notifyObjectPropertiesModified();
 
@@ -315,13 +314,13 @@ public:
                m_is_singleplayer = is_singleplayer;
        }
 
-       bool getCollisionBox(aabb3f *toset);
-       bool collideWithObjects();
+       bool getCollisionBox(aabb3f *toset) const;
+       bool collideWithObjects() const { return true; }
 
        void initialize(RemotePlayer *player, const std::set<std::string> &privs);
 
        v3f getEyePosition() const { return m_base_position + getEyeOffset(); }
-       v3f getEyeOffset() const { return v3f(0, BS * 1.625f, 0); }
+       v3f getEyeOffset() const;
 
 private:
        std::string getPropertyPacket();
@@ -346,31 +345,11 @@ private:
 
        int m_wield_index;
        bool m_position_not_sent;
-       ItemGroupList m_armor_groups;
-       bool m_armor_groups_sent;
 
-       bool m_properties_sent;
-       struct ObjectProperties m_prop;
        // Cached privileges for enforcement
        std::set<std::string> m_privs;
        bool m_is_singleplayer;
 
-       v2f m_animation_range;
-       float m_animation_speed;
-       float m_animation_blend;
-       bool m_animation_loop;
-       bool m_animation_sent;
-
-       // Stores position and rotation for each bone name
-       UNORDERED_MAP<std::string, core::vector2d<v3f> > m_bone_position;
-       bool m_bone_position_sent;
-
-       int m_attachment_parent_id;
-       UNORDERED_SET<int> m_attachment_child_ids;
-       std::string m_attachment_bone;
-       v3f m_attachment_position;
-       v3f m_attachment_rotation;
-       bool m_attachment_sent;
        u16 m_breath;
        f32 m_pitch;
        f32 m_fov;
index cfdceb28e7816472746888db34c0d4423cd78ac5..84d14d521f5d937eecbf6b98dd463859e51cfad9 100644 (file)
@@ -137,8 +137,8 @@ int ObjectRef::l_remove(lua_State *L)
        if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
                return 0;
 
-       UNORDERED_SET<int> child_ids = co->getAttachmentChildIds();
-       UNORDERED_SET<int>::iterator it;
+       const UNORDERED_SET<int> &child_ids = co->getAttachmentChildIds();
+       UNORDERED_SET<int>::const_iterator it;
        for (it = child_ids.begin(); it != child_ids.end(); ++it) {
                // Child can be NULL if it was deleted earlier
                if (ServerActiveObject *child = env->getActiveObject(*it))
@@ -396,8 +396,7 @@ int ObjectRef::l_get_armor_groups(lua_State *L)
        if (co == NULL)
                return 0;
        // Do it
-       ItemGroupList groups = co->getArmorGroups();
-       push_groups(L, groups);
+       push_groups(L, co->getArmorGroups());
        return 1;
 }
 
index 9884eb0a1e3aaf08bad5001eddc6aade8325fddf..9e8b5a779f6b798bf48cc8ed4592c512bfc8ddb3 100644 (file)
@@ -146,8 +146,8 @@ public:
 
        virtual void setArmorGroups(const ItemGroupList &armor_groups)
        {}
-       virtual ItemGroupList getArmorGroups()
-       { return ItemGroupList(); }
+       virtual const ItemGroupList &getArmorGroups()
+       { static const ItemGroupList rv; return rv; }
        virtual void setPhysicsOverride(float physics_override_speed, float physics_override_jump, float physics_override_gravity)
        {}
        virtual void setAnimation(v2f frames, float frame_speed, float frame_blend, bool frame_loop)
@@ -166,8 +166,8 @@ public:
        {}
        virtual void removeAttachmentChild(int child_id)
        {}
-       virtual UNORDERED_SET<int> getAttachmentChildIds()
-       { return UNORDERED_SET<int>(); }
+       virtual const UNORDERED_SET<int> &getAttachmentChildIds()
+       { static const UNORDERED_SET<int> rv; return rv; }
        virtual ObjectProperties* accessObjectProperties()
        { return NULL; }
        virtual void notifyObjectPropertiesModified()