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
This commit is contained in:
Loic Blot 2018-03-09 08:25:48 +01:00 committed by Loïc Blot
parent def46c6cdb
commit 2c860a6a42
4 changed files with 48 additions and 30 deletions

@ -1416,23 +1416,31 @@ ServerActiveObject* ServerEnvironment::getActiveObject(u16 id)
return (n != m_active_objects.end() ? n->second : NULL); 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) if (id == 0)
return false; 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; static u16 last_used_id = 0;
u16 startid = last_used_id; u16 startid = last_used_id;
for(;;) for (;;) {
{
last_used_id++; last_used_id++;
if(isFreeServerActiveObjectId(last_used_id, objects)) if (isFreeServerActiveObjectId(last_used_id))
return last_used_id; return last_used_id;
if (last_used_id == startid) if (last_used_id == startid)
@ -1623,7 +1631,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
{ {
assert(object); // Pre-condition assert(object); // Pre-condition
if(object->getId() == 0){ if(object->getId() == 0){
u16 new_id = getFreeServerActiveObjectId(m_active_objects); u16 new_id = getFreeServerActiveObjectId();
if(new_id == 0) if(new_id == 0)
{ {
errorstream<<"ServerEnvironment::addActiveObjectRaw(): " errorstream<<"ServerEnvironment::addActiveObjectRaw(): "
@ -1639,7 +1647,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
<<"supplied with id "<<object->getId()<<std::endl; <<"supplied with id "<<object->getId()<<std::endl;
} }
if(!isFreeServerActiveObjectId(object->getId(), m_active_objects)) { if(!isFreeServerActiveObjectId(object->getId())) {
errorstream<<"ServerEnvironment::addActiveObjectRaw(): " errorstream<<"ServerEnvironment::addActiveObjectRaw(): "
<<"id is not free ("<<object->getId()<<")"<<std::endl; <<"id is not free ("<<object->getId()<<")"<<std::endl;
if(object->environmentDeletes()) if(object->environmentDeletes())
@ -1677,9 +1685,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
{ {
// Add static object to active static list of the block // Add static object to active static list of the block
v3f objectpos = object->getBasePosition(); v3f objectpos = object->getBasePosition();
std::string staticdata; StaticObject s_obj(object, objectpos);
object->getStaticData(&staticdata);
StaticObject s_obj(object->getType(), objectpos, staticdata);
// Add to the block where the object is located in // Add to the block where the object is located in
v3s16 blockpos = getNodeBlockPos(floatToInt(objectpos, BS)); v3s16 blockpos = getNodeBlockPos(floatToInt(objectpos, BS));
MapBlock *block = m_map->emergeBlock(blockpos); MapBlock *block = m_map->emergeBlock(blockpos);
@ -1929,9 +1935,7 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete)
// Delete from block where object was located // Delete from block where object was located
deleteStaticFromBlock(obj, id, MOD_REASON_STATIC_DATA_REMOVED, false); deleteStaticFromBlock(obj, id, MOD_REASON_STATIC_DATA_REMOVED, false);
std::string staticdata_new; StaticObject s_obj(obj, objectpos);
obj->getStaticData(&staticdata_new);
StaticObject s_obj(obj->getType(), objectpos, staticdata_new);
// Save to block where object is located // Save to block where object is located
saveStaticToBlock(blockpos_o, id, obj, s_obj, MOD_REASON_STATIC_DATA_ADDED); saveStaticToBlock(blockpos_o, id, obj, s_obj, MOD_REASON_STATIC_DATA_ADDED);
@ -1952,12 +1956,9 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete)
/* /*
Update the static data Update the static data
*/ */
if(obj->isStaticAllowed()) if (obj->isStaticAllowed()) {
{
// Create new static object // Create new static object
std::string staticdata_new; StaticObject s_obj(obj, objectpos);
obj->getStaticData(&staticdata_new);
StaticObject s_obj(obj->getType(), objectpos, staticdata_new);
bool stays_in_same_block = false; bool stays_in_same_block = false;
bool data_changed = true; bool data_changed = true;
@ -1977,7 +1978,7 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete)
float save_movem = obj->getMinimumSavedMovement(); 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) (static_old.pos - objectpos).getLength() < save_movem)
data_changed = false; data_changed = false;
} else { } else {

@ -257,6 +257,19 @@ public:
*/ */
u16 addActiveObject(ServerActiveObject *object); 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 Add an active object as a static object to the corresponding
MapBlock. MapBlock.

@ -19,7 +19,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "staticobject.h" #include "staticobject.h"
#include "util/serialize.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) void StaticObject::serialize(std::ostream &os)
{ {

@ -26,6 +26,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <map> #include <map>
#include "debug.h" #include "debug.h"
class ServerActiveObject;
struct StaticObject struct StaticObject
{ {
u8 type = 0; u8 type = 0;
@ -33,12 +35,7 @@ struct StaticObject
std::string data; std::string data;
StaticObject() = default; StaticObject() = default;
StaticObject(u8 type_, const v3f &pos_, const std::string &data_): StaticObject(const ServerActiveObject *s_obj, const v3f &pos_);
type(type_),
pos(pos_),
data(data_)
{
}
void serialize(std::ostream &os); void serialize(std::ostream &os);
void deSerialize(std::istream &is, u8 version); void deSerialize(std::istream &is, u8 version);