Skip to content

Commit

Permalink
ServerEnvironment & StaticObject cleanups
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nerzhul committed Mar 9, 2018
1 parent def46c6 commit 2c860a6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 30 deletions.
47 changes: 24 additions & 23 deletions src/serverenvironment.cpp
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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(): "
Expand All @@ -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())
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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;
Expand All @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions src/serverenvironment.h
Expand Up @@ -257,6 +257,19 @@ class ServerEnvironment : public Environment
*/
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.
Expand Down
9 changes: 8 additions & 1 deletion src/staticobject.cpp
Expand Up @@ -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)
{
Expand Down
9 changes: 3 additions & 6 deletions src/staticobject.h
Expand Up @@ -26,19 +26,16 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <map>
#include "debug.h"

class ServerActiveObject;

struct StaticObject
{
u8 type = 0;
v3f pos;
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);
Expand Down

0 comments on commit 2c860a6

Please sign in to comment.