Backport 'Add min_y and max_y checks for Active Block Modifiers (ABM)' b10091be9b

This check can be used by ABM to reduce CPU usage.
This commit is contained in:
sfence 2021-06-20 17:21:35 +02:00 committed by MoNTE48
parent 9632dcf0be
commit fcaeff1e1e
5 changed files with 43 additions and 6 deletions

View File

@ -4238,6 +4238,12 @@ Definition tables
^ If left out or empty, any neighbor will do ]] ^ If left out or empty, any neighbor will do ]]
interval = 1.0, -- Operation interval in seconds interval = 1.0, -- Operation interval in seconds
chance = 1, -- Chance of trigger per-node per-interval is 1.0 / this chance = 1, -- Chance of trigger per-node per-interval is 1.0 / this
min_y = -32768,
max_y = 32767,
-- min and max height levels where ABM will be processed
-- can be used to reduce CPU usage
catch_up = true, -- If true, catch-up behaviour is enabled --[[ catch_up = true, -- If true, catch-up behaviour is enabled --[[
^ The chance value is temporarily reduced when returning to ^ The chance value is temporarily reduced when returning to
an area to simulate time lost by the area being unattended. an area to simulate time lost by the area being unattended.

View File

@ -151,8 +151,14 @@ void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
bool simple_catch_up = true; bool simple_catch_up = true;
getboolfield(L, current_abm, "catch_up", simple_catch_up); getboolfield(L, current_abm, "catch_up", simple_catch_up);
int min_y = -32767;
getintfield(L, current_abm, "min_y", min_y);
int max_y = 32767;
getintfield(L, current_abm, "max_y", max_y);
LuaABM *abm = new LuaABM(L, id, trigger_contents, required_neighbors, LuaABM *abm = new LuaABM(L, id, trigger_contents, required_neighbors,
trigger_interval, trigger_chance, simple_catch_up); trigger_interval, trigger_chance, simple_catch_up, min_y, max_y);
env->addActiveBlockModifier(abm); env->addActiveBlockModifier(abm);

View File

@ -190,17 +190,21 @@ private:
float m_trigger_interval; float m_trigger_interval;
u32 m_trigger_chance; u32 m_trigger_chance;
bool m_simple_catch_up; bool m_simple_catch_up;
s16 m_min_y;
s16 m_max_y;
public: public:
LuaABM(lua_State *L, int id, LuaABM(lua_State *L, int id,
const std::set<std::string> &trigger_contents, const std::set<std::string> &trigger_contents,
const std::set<std::string> &required_neighbors, const std::set<std::string> &required_neighbors,
float trigger_interval, u32 trigger_chance, bool simple_catch_up): float trigger_interval, u32 trigger_chance, bool simple_catch_up, s16 min_y, s16 max_y):
m_id(id), m_id(id),
m_trigger_contents(trigger_contents), m_trigger_contents(trigger_contents),
m_required_neighbors(required_neighbors), m_required_neighbors(required_neighbors),
m_trigger_interval(trigger_interval), m_trigger_interval(trigger_interval),
m_trigger_chance(trigger_chance), m_trigger_chance(trigger_chance),
m_simple_catch_up(simple_catch_up) m_simple_catch_up(simple_catch_up),
m_min_y(min_y),
m_max_y(max_y)
{ {
} }
virtual const std::set<std::string> &getTriggerContents() const virtual const std::set<std::string> &getTriggerContents() const
@ -223,6 +227,14 @@ public:
{ {
return m_simple_catch_up; return m_simple_catch_up;
} }
virtual s16 getMinY()
{
return m_min_y;
}
virtual s16 getMaxY()
{
return m_max_y;
}
virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n, virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
u32 active_object_count, u32 active_object_count_wider); u32 active_object_count, u32 active_object_count_wider);
}; };

View File

@ -694,6 +694,9 @@ struct ActiveABM
ActiveBlockModifier *abm; ActiveBlockModifier *abm;
int chance; int chance;
std::set<content_t> required_neighbors; std::set<content_t> required_neighbors;
bool check_required_neighbors; // false if required_neighbors is known to be empty
s16 min_y;
s16 max_y;
}; };
class ABMHandler class ABMHandler
@ -739,6 +742,9 @@ public:
} else { } else {
aabm.chance = chance; aabm.chance = chance;
} }
// y limits
aabm.min_y = abm->getMinY();
aabm.max_y = abm->getMaxY();
// Trigger neighbors // Trigger neighbors
const std::set<std::string> &required_neighbors_s = const std::set<std::string> &required_neighbors_s =
@ -826,7 +832,10 @@ public:
v3s16 p = p0 + block->getPosRelative(); v3s16 p = p0 + block->getPosRelative();
for(std::vector<ActiveABM>::iterator for(std::vector<ActiveABM>::iterator
i = m_aabms[c]->begin(); i != m_aabms[c]->end(); ++i) { i = m_aabms[c]->begin(); i != m_aabms[c]->end(); ++i) {
if(myrand() % i->chance != 0) if ((p.Y < i->min_y) || (p.Y > i->max_y))
continue;
if (myrand() % i->chance != 0)
continue; continue;
// Check neighbors // Check neighbors
@ -2169,8 +2178,8 @@ PlayerDatabase *ServerEnvironment::openPlayerDatabase(const std::string &name,
{ {
#ifdef _WIN32 #ifdef _WIN32
if (name == "sqlite3") if (name == "sqlite3")
return new PlayerDatabaseSQLite3(savedir); return new PlayerDatabaseSQLite3(savedir);
else else
#endif #endif
if (name == "dummy") if (name == "dummy")
return new Database_Dummy(); return new Database_Dummy();

View File

@ -62,6 +62,10 @@ public:
virtual u32 getTriggerChance() = 0; virtual u32 getTriggerChance() = 0;
// Whether to modify chance to simulate time lost by an unnattended block // Whether to modify chance to simulate time lost by an unnattended block
virtual bool getSimpleCatchUp() = 0; virtual bool getSimpleCatchUp() = 0;
// get min Y for apply abm
virtual s16 getMinY() = 0;
// get max Y for apply abm
virtual s16 getMaxY() = 0;
// This is called usually at interval for 1/chance of the nodes // This is called usually at interval for 1/chance of the nodes
virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){}; virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n, virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,