Skip to content

Commit b10091b

Browse files
authoredJun 20, 2021
Add min_y and max_y checks for Active Block Modifiers (ABM) (#11333)
This check can be used by ABM to reduce CPU usage.
1 parent 1805775 commit b10091b

File tree

6 files changed

+41
-3
lines changed

6 files changed

+41
-3
lines changed
 

‎builtin/game/features.lua

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ core.features = {
2020
direct_velocity_on_players = true,
2121
use_texture_alpha_string_modes = true,
2222
degrotate_240_steps = true,
23+
abm_min_max_y = true,
2324
}
2425

2526
function core.has_feature(arg)

‎doc/lua_api.txt

+7
Original file line numberDiff line numberDiff line change
@@ -4484,6 +4484,8 @@ Utilities
44844484
-- degrotate param2 rotates in units of 1.5° instead of 2°
44854485
-- thus changing the range of values from 0-179 to 0-240 (5.5.0)
44864486
degrotate_240_steps = true,
4487+
-- ABM supports min_y and max_y fields in definition (5.5.0)
4488+
abm_min_max_y = true,
44874489
}
44884490

44894491
* `minetest.has_feature(arg)`: returns `boolean, missing_features`
@@ -7187,6 +7189,11 @@ Used by `minetest.register_abm`.
71877189
chance = 1,
71887190
-- Chance of triggering `action` per-node per-interval is 1.0 / this
71897191
-- value
7192+
7193+
min_y = -32768,
7194+
max_y = 32767,
7195+
-- min and max height levels where ABM will be processed
7196+
-- can be used to reduce CPU usage
71907197

71917198
catch_up = true,
71927199
-- If true, catch-up behaviour is enabled: The `chance` value is

‎src/script/cpp_api/s_env.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,19 @@ void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
150150

151151
bool simple_catch_up = true;
152152
getboolfield(L, current_abm, "catch_up", simple_catch_up);
153+
154+
s16 min_y = INT16_MIN;
155+
getintfield(L, current_abm, "min_y", min_y);
156+
157+
s16 max_y = INT16_MAX;
158+
getintfield(L, current_abm, "max_y", max_y);
153159

154160
lua_getfield(L, current_abm, "action");
155161
luaL_checktype(L, current_abm + 1, LUA_TFUNCTION);
156162
lua_pop(L, 1);
157163

158164
LuaABM *abm = new LuaABM(L, id, trigger_contents, required_neighbors,
159-
trigger_interval, trigger_chance, simple_catch_up);
165+
trigger_interval, trigger_chance, simple_catch_up, min_y, max_y);
160166

161167
env->addActiveBlockModifier(abm);
162168

‎src/script/lua_api/l_env.h

+14-2
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,21 @@ class LuaABM : public ActiveBlockModifier {
223223
float m_trigger_interval;
224224
u32 m_trigger_chance;
225225
bool m_simple_catch_up;
226+
s16 m_min_y;
227+
s16 m_max_y;
226228
public:
227229
LuaABM(lua_State *L, int id,
228230
const std::vector<std::string> &trigger_contents,
229231
const std::vector<std::string> &required_neighbors,
230-
float trigger_interval, u32 trigger_chance, bool simple_catch_up):
232+
float trigger_interval, u32 trigger_chance, bool simple_catch_up, s16 min_y, s16 max_y):
231233
m_id(id),
232234
m_trigger_contents(trigger_contents),
233235
m_required_neighbors(required_neighbors),
234236
m_trigger_interval(trigger_interval),
235237
m_trigger_chance(trigger_chance),
236-
m_simple_catch_up(simple_catch_up)
238+
m_simple_catch_up(simple_catch_up),
239+
m_min_y(min_y),
240+
m_max_y(max_y)
237241
{
238242
}
239243
virtual const std::vector<std::string> &getTriggerContents() const
@@ -256,6 +260,14 @@ class LuaABM : public ActiveBlockModifier {
256260
{
257261
return m_simple_catch_up;
258262
}
263+
virtual s16 getMinY()
264+
{
265+
return m_min_y;
266+
}
267+
virtual s16 getMaxY()
268+
{
269+
return m_max_y;
270+
}
259271
virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
260272
u32 active_object_count, u32 active_object_count_wider);
261273
};

‎src/serverenvironment.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,8 @@ struct ActiveABM
729729
int chance;
730730
std::vector<content_t> required_neighbors;
731731
bool check_required_neighbors; // false if required_neighbors is known to be empty
732+
s16 min_y;
733+
s16 max_y;
732734
};
733735

734736
class ABMHandler
@@ -773,6 +775,9 @@ class ABMHandler
773775
} else {
774776
aabm.chance = chance;
775777
}
778+
// y limits
779+
aabm.min_y = abm->getMinY();
780+
aabm.max_y = abm->getMaxY();
776781

777782
// Trigger neighbors
778783
const std::vector<std::string> &required_neighbors_s =
@@ -885,6 +890,9 @@ class ABMHandler
885890

886891
v3s16 p = p0 + block->getPosRelative();
887892
for (ActiveABM &aabm : *m_aabms[c]) {
893+
if ((p.Y < aabm.min_y) || (p.Y > aabm.max_y))
894+
continue;
895+
888896
if (myrand() % aabm.chance != 0)
889897
continue;
890898

‎src/serverenvironment.h

+4
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ class ActiveBlockModifier
6767
virtual u32 getTriggerChance() = 0;
6868
// Whether to modify chance to simulate time lost by an unnattended block
6969
virtual bool getSimpleCatchUp() = 0;
70+
// get min Y for apply abm
71+
virtual s16 getMinY() = 0;
72+
// get max Y for apply abm
73+
virtual s16 getMaxY() = 0;
7074
// This is called usually at interval for 1/chance of the nodes
7175
virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
7276
virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,

0 commit comments

Comments
 (0)
Please sign in to comment.