Skip to content

Commit

Permalink
Implement unloading of static_save=false objects according to existin…
Browse files Browse the repository at this point in the history
…g docs (#10485)
  • Loading branch information
sfan5 committed Oct 19, 2020
1 parent 660115c commit 4f23038
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/server/luaentity_sao.h
Expand Up @@ -42,6 +42,7 @@ class LuaEntitySAO : public UnitSAO
void step(float dtime, bool send_recommended);
std::string getClientInitializationData(u16 protocol_version);
bool isStaticAllowed() const { return m_prop.static_save; }
bool shouldUnload() const { return true; }
void getStaticData(std::string *result) const;
u16 punch(v3f dir, const ToolCapabilities *toolcap = nullptr,
ServerActiveObject *puncher = nullptr,
Expand Down
1 change: 1 addition & 0 deletions src/server/player_sao.h
Expand Up @@ -83,6 +83,7 @@ class PlayerSAO : public UnitSAO
void addedToEnvironment(u32 dtime_s);
void removingFromEnvironment();
bool isStaticAllowed() const { return false; }
bool shouldUnload() const { return false; }
std::string getClientInitializationData(u16 protocol_version);
void getStaticData(std::string *result) const;
void step(float dtime, bool send_recommended);
Expand Down
9 changes: 9 additions & 0 deletions src/server/serveractiveobject.h
Expand Up @@ -125,13 +125,22 @@ class ServerActiveObject : public ActiveObject
assert(isStaticAllowed());
*result = "";
}

/*
Return false in here to never save and instead remove object
on unload. getStaticData() will not be called in that case.
*/
virtual bool isStaticAllowed() const
{return true;}

/*
Return false here to never unload the object.
isStaticAllowed && shouldUnload -> unload when out of active block range
!isStaticAllowed && shouldUnload -> unload when block is unloaded
*/
virtual bool shouldUnload() const
{ return true; }

// Returns tool wear
virtual u16 punch(v3f dir,
const ToolCapabilities *toolcap = nullptr,
Expand Down
9 changes: 6 additions & 3 deletions src/serverenvironment.cpp
Expand Up @@ -1972,8 +1972,8 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete)
// force_delete might be overriden per object
bool force_delete = _force_delete;

// Do not deactivate if static data creation not allowed
if (!force_delete && !obj->isStaticAllowed())
// Do not deactivate if disallowed
if (!force_delete && !obj->shouldUnload())
return false;

// removeRemovedObjects() is responsible for these
Expand Down Expand Up @@ -2002,7 +2002,10 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete)
}

// If block is still active, don't remove
if (!force_delete && m_active_blocks.contains(blockpos_o))
bool still_active = obj->isStaticAllowed() ?
m_active_blocks.contains(blockpos_o) :
getMap().getBlockNoCreateNoEx(blockpos_o) != nullptr;
if (!force_delete && still_active)
return false;

verbosestream << "ServerEnvironment::deactivateFarObjects(): "
Expand Down

0 comments on commit 4f23038

Please sign in to comment.