Skip to content

Commit

Permalink
Add min_y and max_y checks for Active Block Modifiers (ABM) (#11333)
Browse files Browse the repository at this point in the history
This check can be used by ABM to reduce CPU usage.
  • Loading branch information
sfence committed Jun 20, 2021
1 parent 1805775 commit b10091b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions builtin/game/features.lua
Expand Up @@ -20,6 +20,7 @@ core.features = {
direct_velocity_on_players = true,
use_texture_alpha_string_modes = true,
degrotate_240_steps = true,
abm_min_max_y = true,
}

function core.has_feature(arg)
Expand Down
7 changes: 7 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -4484,6 +4484,8 @@ Utilities
-- degrotate param2 rotates in units of 1.5° instead of 2°
-- thus changing the range of values from 0-179 to 0-240 (5.5.0)
degrotate_240_steps = true,
-- ABM supports min_y and max_y fields in definition (5.5.0)
abm_min_max_y = true,
}

* `minetest.has_feature(arg)`: returns `boolean, missing_features`
Expand Down Expand Up @@ -7187,6 +7189,11 @@ Used by `minetest.register_abm`.
chance = 1,
-- Chance of triggering `action` per-node per-interval is 1.0 / this
-- value

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: The `chance` value is
Expand Down
8 changes: 7 additions & 1 deletion src/script/cpp_api/s_env.cpp
Expand Up @@ -150,13 +150,19 @@ void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)

bool simple_catch_up = true;
getboolfield(L, current_abm, "catch_up", simple_catch_up);

s16 min_y = INT16_MIN;
getintfield(L, current_abm, "min_y", min_y);

s16 max_y = INT16_MAX;
getintfield(L, current_abm, "max_y", max_y);

lua_getfield(L, current_abm, "action");
luaL_checktype(L, current_abm + 1, LUA_TFUNCTION);
lua_pop(L, 1);

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);

Expand Down
16 changes: 14 additions & 2 deletions src/script/lua_api/l_env.h
Expand Up @@ -223,17 +223,21 @@ class LuaABM : public ActiveBlockModifier {
float m_trigger_interval;
u32 m_trigger_chance;
bool m_simple_catch_up;
s16 m_min_y;
s16 m_max_y;
public:
LuaABM(lua_State *L, int id,
const std::vector<std::string> &trigger_contents,
const std::vector<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_trigger_contents(trigger_contents),
m_required_neighbors(required_neighbors),
m_trigger_interval(trigger_interval),
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::vector<std::string> &getTriggerContents() const
Expand All @@ -256,6 +260,14 @@ class LuaABM : public ActiveBlockModifier {
{
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,
u32 active_object_count, u32 active_object_count_wider);
};
Expand Down
8 changes: 8 additions & 0 deletions src/serverenvironment.cpp
Expand Up @@ -729,6 +729,8 @@ struct ActiveABM
int chance;
std::vector<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
Expand Down Expand Up @@ -773,6 +775,9 @@ class ABMHandler
} else {
aabm.chance = chance;
}
// y limits
aabm.min_y = abm->getMinY();
aabm.max_y = abm->getMaxY();

// Trigger neighbors
const std::vector<std::string> &required_neighbors_s =
Expand Down Expand Up @@ -885,6 +890,9 @@ class ABMHandler

v3s16 p = p0 + block->getPosRelative();
for (ActiveABM &aabm : *m_aabms[c]) {
if ((p.Y < aabm.min_y) || (p.Y > aabm.max_y))
continue;

if (myrand() % aabm.chance != 0)
continue;

Expand Down
4 changes: 4 additions & 0 deletions src/serverenvironment.h
Expand Up @@ -67,6 +67,10 @@ class ActiveBlockModifier
virtual u32 getTriggerChance() = 0;
// Whether to modify chance to simulate time lost by an unnattended block
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
virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
Expand Down

0 comments on commit b10091b

Please sign in to comment.