Skip to content

Commit 2c860a6

Browse files
committed
ServerEnvironment & StaticObject cleanups
* 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
1 parent def46c6 commit 2c860a6

4 files changed

+48
-30
lines changed

src/serverenvironment.cpp

+24-23
Original file line numberDiff line numberDiff line change
@@ -1416,26 +1416,34 @@ ServerActiveObject* ServerEnvironment::getActiveObject(u16 id)
14161416
return (n != m_active_objects.end() ? n->second : NULL);
14171417
}
14181418

1419-
bool isFreeServerActiveObjectId(u16 id, ServerActiveObjectMap &objects)
1419+
/**
1420+
* Verify if id is a free active object id
1421+
* @param id
1422+
* @return true if slot is free
1423+
*/
1424+
bool ServerEnvironment::isFreeServerActiveObjectId(u16 id) const
14201425
{
14211426
if (id == 0)
14221427
return false;
14231428

1424-
return objects.find(id) == objects.end();
1429+
return m_active_objects.find(id) == m_active_objects.end();
14251430
}
14261431

1427-
u16 getFreeServerActiveObjectId(ServerActiveObjectMap &objects)
1432+
/**
1433+
* Retrieve the next free activeobject ID
1434+
* @return free activeobject ID or zero if not free ID found
1435+
*/
1436+
u16 ServerEnvironment::getFreeServerActiveObjectId()
14281437
{
1429-
//try to reuse id's as late as possible
1438+
// try to reuse id's as late as possible
14301439
static u16 last_used_id = 0;
14311440
u16 startid = last_used_id;
1432-
for(;;)
1433-
{
1434-
last_used_id ++;
1435-
if(isFreeServerActiveObjectId(last_used_id, objects))
1441+
for (;;) {
1442+
last_used_id++;
1443+
if (isFreeServerActiveObjectId(last_used_id))
14361444
return last_used_id;
14371445

1438-
if(last_used_id == startid)
1446+
if (last_used_id == startid)
14391447
return 0;
14401448
}
14411449
}
@@ -1623,7 +1631,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
16231631
{
16241632
assert(object); // Pre-condition
16251633
if(object->getId() == 0){
1626-
u16 new_id = getFreeServerActiveObjectId(m_active_objects);
1634+
u16 new_id = getFreeServerActiveObjectId();
16271635
if(new_id == 0)
16281636
{
16291637
errorstream<<"ServerEnvironment::addActiveObjectRaw(): "
@@ -1639,7 +1647,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
16391647
<<"supplied with id "<<object->getId()<<std::endl;
16401648
}
16411649

1642-
if(!isFreeServerActiveObjectId(object->getId(), m_active_objects)) {
1650+
if(!isFreeServerActiveObjectId(object->getId())) {
16431651
errorstream<<"ServerEnvironment::addActiveObjectRaw(): "
16441652
<<"id is not free ("<<object->getId()<<")"<<std::endl;
16451653
if(object->environmentDeletes())
@@ -1677,9 +1685,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
16771685
{
16781686
// Add static object to active static list of the block
16791687
v3f objectpos = object->getBasePosition();
1680-
std::string staticdata;
1681-
object->getStaticData(&staticdata);
1682-
StaticObject s_obj(object->getType(), objectpos, staticdata);
1688+
StaticObject s_obj(object, objectpos);
16831689
// Add to the block where the object is located in
16841690
v3s16 blockpos = getNodeBlockPos(floatToInt(objectpos, BS));
16851691
MapBlock *block = m_map->emergeBlock(blockpos);
@@ -1929,9 +1935,7 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete)
19291935
// Delete from block where object was located
19301936
deleteStaticFromBlock(obj, id, MOD_REASON_STATIC_DATA_REMOVED, false);
19311937

1932-
std::string staticdata_new;
1933-
obj->getStaticData(&staticdata_new);
1934-
StaticObject s_obj(obj->getType(), objectpos, staticdata_new);
1938+
StaticObject s_obj(obj, objectpos);
19351939
// Save to block where object is located
19361940
saveStaticToBlock(blockpos_o, id, obj, s_obj, MOD_REASON_STATIC_DATA_ADDED);
19371941

@@ -1952,12 +1956,9 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete)
19521956
/*
19531957
Update the static data
19541958
*/
1955-
if(obj->isStaticAllowed())
1956-
{
1959+
if (obj->isStaticAllowed()) {
19571960
// Create new static object
1958-
std::string staticdata_new;
1959-
obj->getStaticData(&staticdata_new);
1960-
StaticObject s_obj(obj->getType(), objectpos, staticdata_new);
1961+
StaticObject s_obj(obj, objectpos);
19611962

19621963
bool stays_in_same_block = false;
19631964
bool data_changed = true;
@@ -1977,7 +1978,7 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete)
19771978

19781979
float save_movem = obj->getMinimumSavedMovement();
19791980

1980-
if (static_old.data == staticdata_new &&
1981+
if (static_old.data == s_obj.data &&
19811982
(static_old.pos - objectpos).getLength() < save_movem)
19821983
data_changed = false;
19831984
} else {

src/serverenvironment.h

+13
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,19 @@ class ServerEnvironment : public Environment
257257
*/
258258
u16 addActiveObject(ServerActiveObject *object);
259259

260+
/**
261+
* Verify if id is a free active object id
262+
* @param id
263+
* @return true if slot is free
264+
*/
265+
bool isFreeServerActiveObjectId(u16 id) const;
266+
267+
/**
268+
* Retrieve the next free activeobject ID
269+
* @return free activeobject ID or zero if not free ID found
270+
*/
271+
u16 getFreeServerActiveObjectId();
272+
260273
/*
261274
Add an active object as a static object to the corresponding
262275
MapBlock.

src/staticobject.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
1919

2020
#include "staticobject.h"
2121
#include "util/serialize.h"
22-
#include "log.h"
22+
#include "content_sao.h"
23+
24+
StaticObject::StaticObject(const ServerActiveObject *s_obj, const v3f &pos_):
25+
type(s_obj->getType()),
26+
pos(pos_)
27+
{
28+
s_obj->getStaticData(&data);
29+
}
2330

2431
void StaticObject::serialize(std::ostream &os)
2532
{

src/staticobject.h

+3-6
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,16 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2626
#include <map>
2727
#include "debug.h"
2828

29+
class ServerActiveObject;
30+
2931
struct StaticObject
3032
{
3133
u8 type = 0;
3234
v3f pos;
3335
std::string data;
3436

3537
StaticObject() = default;
36-
StaticObject(u8 type_, const v3f &pos_, const std::string &data_):
37-
type(type_),
38-
pos(pos_),
39-
data(data_)
40-
{
41-
}
38+
StaticObject(const ServerActiveObject *s_obj, const v3f &pos_);
4239

4340
void serialize(std::ostream &os);
4441
void deSerialize(std::istream &is, u8 version);

0 commit comments

Comments
 (0)