Skip to content

Commit

Permalink
Performance fix + SAO factorization
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Rogier-5 authored and nerzhul committed Jan 11, 2017
1 parent ec30d49 commit 6647939
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 117 deletions.
6 changes: 3 additions & 3 deletions src/activeobject.h
Expand Up @@ -64,7 +64,7 @@ class ActiveObject
m_id(id)
{
}

u16 getId()
{
return m_id;
Expand All @@ -76,8 +76,8 @@ class ActiveObject
}

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"
};
Expand Down
4 changes: 2 additions & 2 deletions src/clientobject.h
Expand Up @@ -47,8 +47,8 @@ class ClientActiveObject : public ActiveObject
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;}
Expand Down
6 changes: 3 additions & 3 deletions src/content_cao.cpp
Expand Up @@ -159,7 +159,7 @@ class TestCAO : public ClientActiveObject

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;
Expand Down Expand Up @@ -316,7 +316,7 @@ class ItemCAO : public ClientActiveObject
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;
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/content_cao.h
Expand Up @@ -130,7 +130,7 @@ class GenericCAO : public ClientActiveObject

ClientActiveObject *getParent();

bool getCollisionBox(aabb3f *toset);
bool getCollisionBox(aabb3f *toset) const;

bool collideWithObjects();

Expand Down
85 changes: 39 additions & 46 deletions src/content_sao.cpp
Expand Up @@ -19,19 +19,15 @@ 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"
#include "remoteplayer.h"
#include "server.h"
#include "scripting_game.h"
#include "genericobject.h"
#include "log.h"

std::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types;

Expand Down Expand Up @@ -94,13 +90,8 @@ class TestSAO : public ServerActiveObject
}
}

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;
Expand All @@ -110,6 +101,28 @@ class TestSAO : public ServerActiveObject
// 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
*/
Expand All @@ -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()
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -748,7 +750,8 @@ bool LuaEntitySAO::getCollisionBox(aabb3f *toset) {
return false;
}

bool LuaEntitySAO::collideWithObjects(){
bool LuaEntitySAO::collideWithObjects() const
{
return m_prop.collideWithObjects;
}

Expand All @@ -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),
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

0 comments on commit 6647939

Please sign in to comment.