mirror of
https://github.com/minetest/minetest.git
synced 2024-11-23 16:13:46 +01:00
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:
parent
def46c6cdb
commit
2c860a6a42
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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(): "
|
||||
@ -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())
|
||||
@ -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);
|
||||
@ -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);
|
||||
|
||||
@ -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;
|
||||
@ -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 {
|
||||
|
@ -257,6 +257,19 @@ public:
|
||||
*/
|
||||
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.
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -26,6 +26,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
#include <map>
|
||||
#include "debug.h"
|
||||
|
||||
class ServerActiveObject;
|
||||
|
||||
struct StaticObject
|
||||
{
|
||||
u8 type = 0;
|
||||
@ -33,12 +35,7 @@ struct StaticObject
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user