Implement unloading of static_save=false objects according to existing docs (#10485)

This commit is contained in:
sfan5 2020-10-19 20:38:33 +02:00 committed by GitHub
parent 660115c1ab
commit 4f2303849e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 3 deletions

@ -42,6 +42,7 @@ public:
void step(float dtime, bool send_recommended); void step(float dtime, bool send_recommended);
std::string getClientInitializationData(u16 protocol_version); std::string getClientInitializationData(u16 protocol_version);
bool isStaticAllowed() const { return m_prop.static_save; } bool isStaticAllowed() const { return m_prop.static_save; }
bool shouldUnload() const { return true; }
void getStaticData(std::string *result) const; void getStaticData(std::string *result) const;
u16 punch(v3f dir, const ToolCapabilities *toolcap = nullptr, u16 punch(v3f dir, const ToolCapabilities *toolcap = nullptr,
ServerActiveObject *puncher = nullptr, ServerActiveObject *puncher = nullptr,

@ -83,6 +83,7 @@ public:
void addedToEnvironment(u32 dtime_s); void addedToEnvironment(u32 dtime_s);
void removingFromEnvironment(); void removingFromEnvironment();
bool isStaticAllowed() const { return false; } bool isStaticAllowed() const { return false; }
bool shouldUnload() const { return false; }
std::string getClientInitializationData(u16 protocol_version); std::string getClientInitializationData(u16 protocol_version);
void getStaticData(std::string *result) const; void getStaticData(std::string *result) const;
void step(float dtime, bool send_recommended); void step(float dtime, bool send_recommended);

@ -125,6 +125,7 @@ public:
assert(isStaticAllowed()); assert(isStaticAllowed());
*result = ""; *result = "";
} }
/* /*
Return false in here to never save and instead remove object Return false in here to never save and instead remove object
on unload. getStaticData() will not be called in that case. on unload. getStaticData() will not be called in that case.
@ -132,6 +133,14 @@ public:
virtual bool isStaticAllowed() const virtual bool isStaticAllowed() const
{return true;} {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 // Returns tool wear
virtual u16 punch(v3f dir, virtual u16 punch(v3f dir,
const ToolCapabilities *toolcap = nullptr, const ToolCapabilities *toolcap = nullptr,

@ -1972,8 +1972,8 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete)
// force_delete might be overriden per object // force_delete might be overriden per object
bool force_delete = _force_delete; bool force_delete = _force_delete;
// Do not deactivate if static data creation not allowed // Do not deactivate if disallowed
if (!force_delete && !obj->isStaticAllowed()) if (!force_delete && !obj->shouldUnload())
return false; return false;
// removeRemovedObjects() is responsible for these // removeRemovedObjects() is responsible for these
@ -2002,7 +2002,10 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete)
} }
// If block is still active, don't remove // 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; return false;
verbosestream << "ServerEnvironment::deactivateFarObjects(): " verbosestream << "ServerEnvironment::deactivateFarObjects(): "