Skip to content

Commit 6647939

Browse files
Rogier-5nerzhul
authored andcommittedJan 11, 2017
Performance fix + SAO factorization
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>
1 parent ec30d49 commit 6647939

8 files changed

+88
-117
lines changed
 

‎src/activeobject.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class ActiveObject
6464
m_id(id)
6565
{
6666
}
67-
67+
6868
u16 getId()
6969
{
7070
return m_id;
@@ -76,8 +76,8 @@ class ActiveObject
7676
}
7777

7878
virtual ActiveObjectType getType() const = 0;
79-
virtual bool getCollisionBox(aabb3f *toset) = 0;
80-
virtual bool collideWithObjects() = 0;
79+
virtual bool getCollisionBox(aabb3f *toset) const = 0;
80+
virtual bool collideWithObjects() const = 0;
8181
protected:
8282
u16 m_id; // 0 is invalid, "no id"
8383
};

‎src/clientobject.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ class ClientActiveObject : public ActiveObject
4747
virtual void updateLightNoCheck(u8 light_at_pos){}
4848
virtual v3s16 getLightPosition(){return v3s16(0,0,0);}
4949
virtual aabb3f *getSelectionBox() { return NULL; }
50-
virtual bool getCollisionBox(aabb3f *toset){return false;}
51-
virtual bool collideWithObjects(){return false;}
50+
virtual bool getCollisionBox(aabb3f *toset) const { return false; }
51+
virtual bool collideWithObjects() const { return false; }
5252
virtual v3f getPosition(){return v3f(0,0,0);}
5353
virtual float getYaw() const {return 0;}
5454
virtual scene::ISceneNode *getSceneNode(){return NULL;}

‎src/content_cao.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class TestCAO : public ClientActiveObject
159159

160160
void processMessage(const std::string &data);
161161

162-
bool getCollisionBox(aabb3f *toset) { return false; }
162+
bool getCollisionBox(aabb3f *toset) const { return false; }
163163
private:
164164
scene::IMeshSceneNode *m_node;
165165
v3f m_position;
@@ -316,7 +316,7 @@ class ItemCAO : public ClientActiveObject
316316
std::string infoText()
317317
{return m_infotext;}
318318

319-
bool getCollisionBox(aabb3f *toset) { return false; }
319+
bool getCollisionBox(aabb3f *toset) const { return false; }
320320
private:
321321
aabb3f m_selection_box;
322322
scene::IMeshSceneNode *m_node;
@@ -587,7 +587,7 @@ GenericCAO::GenericCAO(Client *client, ClientEnvironment *env):
587587
}
588588
}
589589

590-
bool GenericCAO::getCollisionBox(aabb3f *toset)
590+
bool GenericCAO::getCollisionBox(aabb3f *toset) const
591591
{
592592
if (m_prop.physical)
593593
{

‎src/content_cao.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class GenericCAO : public ClientActiveObject
130130

131131
ClientActiveObject *getParent();
132132

133-
bool getCollisionBox(aabb3f *toset);
133+
bool getCollisionBox(aabb3f *toset) const;
134134

135135
bool collideWithObjects();
136136

‎src/content_sao.cpp

+39-46
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
1919

2020
#include "content_sao.h"
2121
#include "util/serialize.h"
22-
#include "util/mathconstants.h"
2322
#include "collision.h"
2423
#include "environment.h"
25-
#include "settings.h"
26-
#include "serialization.h" // For compressZlib
2724
#include "tool.h" // For ToolCapabilities
2825
#include "gamedef.h"
2926
#include "nodedef.h"
3027
#include "remoteplayer.h"
3128
#include "server.h"
3229
#include "scripting_game.h"
3330
#include "genericobject.h"
34-
#include "log.h"
3531

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

@@ -94,13 +90,8 @@ class TestSAO : public ServerActiveObject
9490
}
9591
}
9692

97-
bool getCollisionBox(aabb3f *toset) {
98-
return false;
99-
}
100-
101-
bool collideWithObjects() {
102-
return false;
103-
}
93+
bool getCollisionBox(aabb3f *toset) const { return false; }
94+
bool collideWithObjects() const { return false; }
10495

10596
private:
10697
float m_timer1;
@@ -110,6 +101,28 @@ class TestSAO : public ServerActiveObject
110101
// Prototype (registers item for deserialization)
111102
TestSAO proto_TestSAO(NULL, v3f(0,0,0));
112103

104+
/*
105+
UnitSAO
106+
*/
107+
108+
UnitSAO::UnitSAO(ServerEnvironment *env, v3f pos):
109+
ServerActiveObject(env, pos),
110+
m_hp(-1),
111+
m_yaw(0),
112+
m_properties_sent(true),
113+
m_armor_groups_sent(false),
114+
m_animation_range(0,0),
115+
m_animation_speed(0),
116+
m_animation_blend(0),
117+
m_animation_loop(true),
118+
m_animation_sent(false),
119+
m_bone_position_sent(false),
120+
m_attachment_parent_id(0),
121+
m_attachment_sent(false)
122+
{
123+
// Initialize something to armor groups
124+
m_armor_groups["fleshy"] = 100;
125+
}
113126
/*
114127
LuaEntitySAO
115128
*/
@@ -125,29 +138,17 @@ LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos,
125138
m_registered(false),
126139
m_velocity(0,0,0),
127140
m_acceleration(0,0,0),
128-
m_properties_sent(true),
129141
m_last_sent_yaw(0),
130142
m_last_sent_position(0,0,0),
131143
m_last_sent_velocity(0,0,0),
132144
m_last_sent_position_timer(0),
133-
m_last_sent_move_precision(0),
134-
m_armor_groups_sent(false),
135-
m_animation_speed(0),
136-
m_animation_blend(0),
137-
m_animation_loop(true),
138-
m_animation_sent(false),
139-
m_bone_position_sent(false),
140-
m_attachment_parent_id(0),
141-
m_attachment_sent(false)
145+
m_last_sent_move_precision(0)
142146
{
143147
// Only register type if no environment supplied
144148
if(env == NULL){
145149
ServerActiveObject::registerType(getType(), create);
146150
return;
147151
}
148-
149-
// Initialize something to armor groups
150-
m_armor_groups["fleshy"] = 100;
151152
}
152153

153154
LuaEntitySAO::~LuaEntitySAO()
@@ -565,7 +566,7 @@ void LuaEntitySAO::setArmorGroups(const ItemGroupList &armor_groups)
565566
m_armor_groups_sent = false;
566567
}
567568

568-
ItemGroupList LuaEntitySAO::getArmorGroups()
569+
const ItemGroupList &LuaEntitySAO::getArmorGroups()
569570
{
570571
return m_armor_groups;
571572
}
@@ -635,7 +636,7 @@ void LuaEntitySAO::removeAttachmentChild(int child_id)
635636
m_attachment_child_ids.erase(child_id);
636637
}
637638

638-
UNORDERED_SET<int> LuaEntitySAO::getAttachmentChildIds()
639+
const UNORDERED_SET<int> &LuaEntitySAO::getAttachmentChildIds()
639640
{
640641
return m_attachment_child_ids;
641642
}
@@ -732,7 +733,8 @@ void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
732733
m_messages_out.push(aom);
733734
}
734735

735-
bool LuaEntitySAO::getCollisionBox(aabb3f *toset) {
736+
bool LuaEntitySAO::getCollisionBox(aabb3f *toset) const
737+
{
736738
if (m_prop.physical)
737739
{
738740
//update collision box
@@ -748,7 +750,8 @@ bool LuaEntitySAO::getCollisionBox(aabb3f *toset) {
748750
return false;
749751
}
750752

751-
bool LuaEntitySAO::collideWithObjects(){
753+
bool LuaEntitySAO::collideWithObjects() const
754+
{
752755
return m_prop.collideWithObjects;
753756
}
754757

@@ -770,16 +773,7 @@ PlayerSAO::PlayerSAO(ServerEnvironment *env_, u16 peer_id_, bool is_singleplayer
770773
m_nocheat_dig_time(0),
771774
m_wield_index(0),
772775
m_position_not_sent(false),
773-
m_armor_groups_sent(false),
774-
m_properties_sent(true),
775776
m_is_singleplayer(is_singleplayer),
776-
m_animation_speed(0),
777-
m_animation_blend(0),
778-
m_animation_loop(true),
779-
m_animation_sent(false),
780-
m_bone_position_sent(false),
781-
m_attachment_parent_id(0),
782-
m_attachment_sent(false),
783777
m_breath(PLAYER_MAX_BREATH),
784778
m_pitch(0),
785779
m_fov(0),
@@ -793,7 +787,6 @@ PlayerSAO::PlayerSAO(ServerEnvironment *env_, u16 peer_id_, bool is_singleplayer
793787
m_physics_override_sent(false)
794788
{
795789
assert(m_peer_id != 0); // pre-condition
796-
m_armor_groups["fleshy"] = 100;
797790

798791
m_prop.hp_max = PLAYER_MAX_HP;
799792
m_prop.physical = false;
@@ -828,6 +821,11 @@ void PlayerSAO::initialize(RemotePlayer *player, const std::set<std::string> &pr
828821
m_inventory = &m_player->inventory;
829822
}
830823

824+
v3f PlayerSAO::getEyeOffset() const
825+
{
826+
return v3f(0, BS * 1.625f, 0);
827+
}
828+
831829
std::string PlayerSAO::getDescription()
832830
{
833831
return std::string("player ") + m_player->getName();
@@ -1282,7 +1280,7 @@ void PlayerSAO::setArmorGroups(const ItemGroupList &armor_groups)
12821280
m_armor_groups_sent = false;
12831281
}
12841282

1285-
ItemGroupList PlayerSAO::getArmorGroups()
1283+
const ItemGroupList &PlayerSAO::getArmorGroups()
12861284
{
12871285
return m_armor_groups;
12881286
}
@@ -1354,7 +1352,7 @@ void PlayerSAO::removeAttachmentChild(int child_id)
13541352
m_attachment_child_ids.erase(child_id);
13551353
}
13561354

1357-
UNORDERED_SET<int> PlayerSAO::getAttachmentChildIds()
1355+
const UNORDERED_SET<int> &PlayerSAO::getAttachmentChildIds()
13581356
{
13591357
return m_attachment_child_ids;
13601358
}
@@ -1512,15 +1510,10 @@ bool PlayerSAO::checkMovementCheat()
15121510
return cheated;
15131511
}
15141512

1515-
bool PlayerSAO::getCollisionBox(aabb3f *toset)
1513+
bool PlayerSAO::getCollisionBox(aabb3f *toset) const
15161514
{
15171515
*toset = aabb3f(-BS * 0.30, 0.0, -BS * 0.30, BS * 0.30, BS * 1.75, BS * 0.30);
15181516
toset->MinEdge += m_base_position;
15191517
toset->MaxEdge += m_base_position;
15201518
return true;
15211519
}
1522-
1523-
bool PlayerSAO::collideWithObjects()
1524-
{
1525-
return true;
1526-
}

‎src/content_sao.h

+33-54
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2424
#include "serverobject.h"
2525
#include "itemgroup.h"
2626
#include "object_properties.h"
27-
#include "constants.h"
2827

2928
class UnitSAO: public ServerActiveObject
3029
{
3130
public:
32-
UnitSAO(ServerEnvironment *env, v3f pos):
33-
ServerActiveObject(env, pos),
34-
m_hp(-1), m_yaw(0) {}
31+
UnitSAO(ServerEnvironment *env, v3f pos);
3532
virtual ~UnitSAO() {}
3633

3734
virtual void setYaw(const float yaw) { m_yaw = yaw; }
@@ -46,6 +43,29 @@ class UnitSAO: public ServerActiveObject
4643
protected:
4744
s16 m_hp;
4845
float m_yaw;
46+
47+
bool m_properties_sent;
48+
struct ObjectProperties m_prop;
49+
50+
ItemGroupList m_armor_groups;
51+
bool m_armor_groups_sent;
52+
53+
v2f m_animation_range;
54+
float m_animation_speed;
55+
float m_animation_blend;
56+
bool m_animation_loop;
57+
bool m_animation_sent;
58+
59+
// Stores position and rotation for each bone name
60+
UNORDERED_MAP<std::string, core::vector2d<v3f> > m_bone_position;
61+
bool m_bone_position_sent;
62+
63+
int m_attachment_parent_id;
64+
UNORDERED_SET<int> m_attachment_child_ids;
65+
std::string m_attachment_bone;
66+
v3f m_attachment_position;
67+
v3f m_attachment_rotation;
68+
bool m_attachment_sent;
4969
};
5070

5171
/*
@@ -81,7 +101,7 @@ class LuaEntitySAO : public UnitSAO
81101
void setHP(s16 hp);
82102
s16 getHP() const;
83103
void setArmorGroups(const ItemGroupList &armor_groups);
84-
ItemGroupList getArmorGroups();
104+
const ItemGroupList &getArmorGroups();
85105
void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
86106
void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
87107
void setBonePosition(const std::string &bone, v3f position, v3f rotation);
@@ -90,7 +110,7 @@ class LuaEntitySAO : public UnitSAO
90110
void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation);
91111
void addAttachmentChild(int child_id);
92112
void removeAttachmentChild(int child_id);
93-
UNORDERED_SET<int> getAttachmentChildIds();
113+
const UNORDERED_SET<int> &getAttachmentChildIds();
94114
ObjectProperties* accessObjectProperties();
95115
void notifyObjectPropertiesModified();
96116
/* LuaEntitySAO-specific */
@@ -103,45 +123,24 @@ class LuaEntitySAO : public UnitSAO
103123
void setSprite(v2s16 p, int num_frames, float framelength,
104124
bool select_horiz_by_yawpitch);
105125
std::string getName();
106-
bool getCollisionBox(aabb3f *toset);
107-
bool collideWithObjects();
126+
bool getCollisionBox(aabb3f *toset) const;
127+
bool collideWithObjects() const;
108128
private:
109129
std::string getPropertyPacket();
110130
void sendPosition(bool do_interpolate, bool is_movement_end);
111131

112132
std::string m_init_name;
113133
std::string m_init_state;
114134
bool m_registered;
115-
struct ObjectProperties m_prop;
116135

117136
v3f m_velocity;
118137
v3f m_acceleration;
119138

120-
ItemGroupList m_armor_groups;
121-
122-
bool m_properties_sent;
123139
float m_last_sent_yaw;
124140
v3f m_last_sent_position;
125141
v3f m_last_sent_velocity;
126142
float m_last_sent_position_timer;
127143
float m_last_sent_move_precision;
128-
bool m_armor_groups_sent;
129-
130-
v2f m_animation_range;
131-
float m_animation_speed;
132-
float m_animation_blend;
133-
bool m_animation_loop;
134-
bool m_animation_sent;
135-
136-
UNORDERED_MAP<std::string, core::vector2d<v3f> > m_bone_position;
137-
bool m_bone_position_sent;
138-
139-
int m_attachment_parent_id;
140-
UNORDERED_SET<int> m_attachment_child_ids;
141-
std::string m_attachment_bone;
142-
v3f m_attachment_position;
143-
v3f m_attachment_rotation;
144-
bool m_attachment_sent;
145144
};
146145

147146
/*
@@ -235,7 +234,7 @@ class PlayerSAO : public UnitSAO
235234
u16 getBreath() const { return m_breath; }
236235
void setBreath(const u16 breath, bool send = true);
237236
void setArmorGroups(const ItemGroupList &armor_groups);
238-
ItemGroupList getArmorGroups();
237+
const ItemGroupList &getArmorGroups();
239238
void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
240239
void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
241240
void setBonePosition(const std::string &bone, v3f position, v3f rotation);
@@ -244,7 +243,7 @@ class PlayerSAO : public UnitSAO
244243
void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation);
245244
void addAttachmentChild(int child_id);
246245
void removeAttachmentChild(int child_id);
247-
UNORDERED_SET<int> getAttachmentChildIds();
246+
const UNORDERED_SET<int> &getAttachmentChildIds();
248247
ObjectProperties* accessObjectProperties();
249248
void notifyObjectPropertiesModified();
250249

@@ -315,13 +314,13 @@ class PlayerSAO : public UnitSAO
315314
m_is_singleplayer = is_singleplayer;
316315
}
317316

318-
bool getCollisionBox(aabb3f *toset);
319-
bool collideWithObjects();
317+
bool getCollisionBox(aabb3f *toset) const;
318+
bool collideWithObjects() const { return true; }
320319

321320
void initialize(RemotePlayer *player, const std::set<std::string> &privs);
322321

323322
v3f getEyePosition() const { return m_base_position + getEyeOffset(); }
324-
v3f getEyeOffset() const { return v3f(0, BS * 1.625f, 0); }
323+
v3f getEyeOffset() const;
325324

326325
private:
327326
std::string getPropertyPacket();
@@ -346,31 +345,11 @@ class PlayerSAO : public UnitSAO
346345

347346
int m_wield_index;
348347
bool m_position_not_sent;
349-
ItemGroupList m_armor_groups;
350-
bool m_armor_groups_sent;
351348

352-
bool m_properties_sent;
353-
struct ObjectProperties m_prop;
354349
// Cached privileges for enforcement
355350
std::set<std::string> m_privs;
356351
bool m_is_singleplayer;
357352

358-
v2f m_animation_range;
359-
float m_animation_speed;
360-
float m_animation_blend;
361-
bool m_animation_loop;
362-
bool m_animation_sent;
363-
364-
// Stores position and rotation for each bone name
365-
UNORDERED_MAP<std::string, core::vector2d<v3f> > m_bone_position;
366-
bool m_bone_position_sent;
367-
368-
int m_attachment_parent_id;
369-
UNORDERED_SET<int> m_attachment_child_ids;
370-
std::string m_attachment_bone;
371-
v3f m_attachment_position;
372-
v3f m_attachment_rotation;
373-
bool m_attachment_sent;
374353
u16 m_breath;
375354
f32 m_pitch;
376355
f32 m_fov;

‎src/script/lua_api/l_object.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ int ObjectRef::l_remove(lua_State *L)
137137
if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
138138
return 0;
139139

140-
UNORDERED_SET<int> child_ids = co->getAttachmentChildIds();
141-
UNORDERED_SET<int>::iterator it;
140+
const UNORDERED_SET<int> &child_ids = co->getAttachmentChildIds();
141+
UNORDERED_SET<int>::const_iterator it;
142142
for (it = child_ids.begin(); it != child_ids.end(); ++it) {
143143
// Child can be NULL if it was deleted earlier
144144
if (ServerActiveObject *child = env->getActiveObject(*it))
@@ -396,8 +396,7 @@ int ObjectRef::l_get_armor_groups(lua_State *L)
396396
if (co == NULL)
397397
return 0;
398398
// Do it
399-
ItemGroupList groups = co->getArmorGroups();
400-
push_groups(L, groups);
399+
push_groups(L, co->getArmorGroups());
401400
return 1;
402401
}
403402

‎src/serverobject.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ class ServerActiveObject : public ActiveObject
146146

147147
virtual void setArmorGroups(const ItemGroupList &armor_groups)
148148
{}
149-
virtual ItemGroupList getArmorGroups()
150-
{ return ItemGroupList(); }
149+
virtual const ItemGroupList &getArmorGroups()
150+
{ static const ItemGroupList rv; return rv; }
151151
virtual void setPhysicsOverride(float physics_override_speed, float physics_override_jump, float physics_override_gravity)
152152
{}
153153
virtual void setAnimation(v2f frames, float frame_speed, float frame_blend, bool frame_loop)
@@ -166,8 +166,8 @@ class ServerActiveObject : public ActiveObject
166166
{}
167167
virtual void removeAttachmentChild(int child_id)
168168
{}
169-
virtual UNORDERED_SET<int> getAttachmentChildIds()
170-
{ return UNORDERED_SET<int>(); }
169+
virtual const UNORDERED_SET<int> &getAttachmentChildIds()
170+
{ static const UNORDERED_SET<int> rv; return rv; }
171171
virtual ObjectProperties* accessObjectProperties()
172172
{ return NULL; }
173173
virtual void notifyObjectPropertiesModified()

0 commit comments

Comments
 (0)
Please sign in to comment.