ABM without_neighbors (#14116)

This commit is contained in:
sfence 2024-09-26 17:32:55 +02:00 committed by GitHub
parent c1ea49940b
commit d08d34d803
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 347 additions and 6 deletions

@ -44,6 +44,7 @@ core.features = {
override_item_remove_fields = true, override_item_remove_fields = true,
hotbar_hud_element = true, hotbar_hud_element = true,
bulk_lbms = true, bulk_lbms = true,
abm_without_neighbors = true,
} }
function core.has_feature(arg) function core.has_feature(arg)

@ -5524,6 +5524,8 @@ Utilities
hotbar_hud_element = true, hotbar_hud_element = true,
-- Bulk LBM support (5.10.0) -- Bulk LBM support (5.10.0)
bulk_lbms = true, bulk_lbms = true,
-- ABM supports field without_neighbors (5.10.0)
abm_without_neighbors = true,
} }
``` ```
@ -9106,6 +9108,11 @@ Used by `minetest.register_abm`.
-- If left out or empty, any neighbor will do. -- If left out or empty, any neighbor will do.
-- `group:groupname` can also be used here. -- `group:groupname` can also be used here.
without_neighbors = {"default:lava_source", "default:lava_flowing"},
-- Only apply `action` to nodes that have no one of these neighbors.
-- If left out or empty, it has no effect.
-- `group:groupname` can also be used here.
interval = 10.0, interval = 10.0,
-- Operation interval in seconds -- Operation interval in seconds

@ -0,0 +1,6 @@
# Test ABMs
This mod contains a nodes and related ABM actions.
By placing these nodes, you can test basic ABM behaviours.
There are separate tests for ABM `chance`, `interval`, `min_y`, `max_y`, `neighbor` and `without_neighbor` fields.

@ -0,0 +1,12 @@
local S = minetest.get_translator("testnodes")
-- After ABM node
minetest.register_node("testabms:after_abm", {
description = S("After ABM processed node."),
drawtype = "normal",
tiles = { "testabms_after_node.png" },
groups = { dig_immediate = 3 },
})

@ -0,0 +1,56 @@
-- test ABMs with different chances
local S = minetest.get_translator("testnodes")
-- ABM chance 5 node
minetest.register_node("testabms:chance_5", {
description = S("Node for test ABM chance_5"),
drawtype = "normal",
tiles = { "testabms_wait_node.png" },
groups = { dig_immediate = 3 },
on_construct = function (pos)
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "Waiting for ABM testabms:chance_5")
end,
})
minetest.register_abm({
label = "testabms:chance_5",
nodenames = "testabms:chance_5",
interval = 10,
chance = 5,
action = function (pos)
minetest.swap_node(pos, {name="testabms:after_abm"})
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "ABM testabsm:chance_5 changed this node.")
end
})
-- ABM chance 20 node
minetest.register_node("testabms:chance_20", {
description = S("Node for test ABM chance_20"),
drawtype = "normal",
tiles = { "testabms_wait_node.png" },
groups = { dig_immediate = 3 },
on_construct = function (pos)
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "Waiting for ABM testabms:chance_20")
end,
})
minetest.register_abm({
label = "testabms:chance_20",
nodenames = "testabms:chance_20",
interval = 10,
chance = 20,
action = function (pos)
minetest.swap_node(pos, {name="testabms:after_abm"})
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "ABM testabsm:chance_20 changed this node.")
end
})

@ -0,0 +1,7 @@
local path = minetest.get_modpath(minetest.get_current_modname())
dofile(path.."/after_node.lua")
dofile(path.."/chances.lua")
dofile(path.."/intervals.lua")
dofile(path.."/min_max.lua")
dofile(path.."/neighbors.lua")

@ -0,0 +1,56 @@
-- test ABMs with different interval
local S = minetest.get_translator("testnodes")
-- ABM inteval 1 node
minetest.register_node("testabms:interval_1", {
description = S("Node for test ABM interval_1"),
drawtype = "normal",
tiles = { "testabms_wait_node.png" },
groups = { dig_immediate = 3 },
on_construct = function (pos)
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "Waiting for ABM testabms:interval_1")
end,
})
minetest.register_abm({
label = "testabms:interval_1",
nodenames = "testabms:interval_1",
interval = 1,
chance = 1,
action = function (pos)
minetest.swap_node(pos, {name="testabms:after_abm"})
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "ABM testabsm:interval_1 changed this node.")
end
})
-- ABM interval 60 node
minetest.register_node("testabms:interval_60", {
description = S("Node for test ABM interval_60"),
drawtype = "normal",
tiles = { "testabms_wait_node.png" },
groups = { dig_immediate = 3 },
on_construct = function (pos)
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "Waiting for ABM testabms:interval_60")
end,
})
minetest.register_abm({
label = "testabms:interval_60",
nodenames = "testabms:interval_60",
interval = 60,
chance = 1,
action = function (pos)
minetest.swap_node(pos, {name="testabms:after_abm"})
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "ABM testabsm:interval_60 changed this node.")
end
})

@ -0,0 +1,58 @@
-- test ABMs with min_y and max_y
local S = minetest.get_translator("testnodes")
-- ABM min_y node
minetest.register_node("testabms:min_y", {
description = S("Node for test ABM min_y."),
drawtype = "normal",
tiles = { "testabms_wait_node.png" },
groups = { dig_immediate = 3 },
on_construct = function (pos)
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "Waiting for ABM testabms:min_y at y "..pos.y.." with min_y = 0")
end,
})
minetest.register_abm({
label = "testabms:min_y",
nodenames = "testabms:min_y",
interval = 10,
chance = 1,
min_y = 0,
action = function (pos)
minetest.swap_node(pos, {name="testabms:after_abm"})
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "ABM testabsm:min_y changed this node.")
end
})
-- ABM max_y node
minetest.register_node("testabms:max_y", {
description = S("Node for test ABM max_y."),
drawtype = "normal",
tiles = { "testabms_wait_node.png" },
groups = { dig_immediate = 3 },
on_construct = function (pos)
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "Waiting for ABM testabms:max_y at y "..pos.y.." with max_y = 0")
end,
})
minetest.register_abm({
label = "testabms:max_y",
nodenames = "testabms:max_y",
interval = 10,
chance = 1,
max_y = 0,
action = function (pos)
minetest.swap_node(pos, {name="testabms:after_abm"})
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "ABM testabsm:max_y changed this node.")
end
})

@ -0,0 +1,2 @@
name = testabms
description = Contains some nodes for test ABMs.

@ -0,0 +1,99 @@
-- test ABMs with neighbor and without_neighbor
local S = minetest.get_translator("testnodes")
-- ABM required neighbor
minetest.register_node("testabms:required_neighbor", {
description = S("Node for test ABM required_neighbor.") .. "\n"
.. S("Sensitive neighbor node is testnodes:normal."),
drawtype = "normal",
tiles = { "testabms_wait_node.png" },
groups = { dig_immediate = 3 },
on_construct = function (pos)
local meta = minetest.get_meta(pos)
meta:set_string("infotext",
"Waiting for ABM testabms:required_neighbor "
.. "(normal drawtype testnode sensitive)")
end,
})
minetest.register_abm({
label = "testabms:required_neighbor",
nodenames = "testabms:required_neighbor",
neighbors = {"testnodes:normal"},
interval = 1,
chance = 1,
action = function (pos)
minetest.swap_node(pos, {name="testabms:after_abm"})
local meta = minetest.get_meta(pos)
meta:set_string("infotext",
"ABM testabsm:required_neighbor changed this node.")
end
})
-- ABM missing neighbor node
minetest.register_node("testabms:missing_neighbor", {
description = S("Node for test ABM missing_neighbor.") .. "\n"
.. S("Sensitive neighbor node is testnodes:normal."),
drawtype = "normal",
tiles = { "testabms_wait_node.png" },
groups = { dig_immediate = 3 },
on_construct = function (pos)
local meta = minetest.get_meta(pos)
meta:set_string("infotext",
"Waiting for ABM testabms:missing_neighbor"
.. " (normal drawtype testnode sensitive)")
end,
})
minetest.register_abm({
label = "testabms:missing_neighbor",
nodenames = "testabms:missing_neighbor",
without_neighbors = {"testnodes:normal"},
interval = 1,
chance = 1,
action = function (pos)
minetest.swap_node(pos, {name="testabms:after_abm"})
local meta = minetest.get_meta(pos)
meta:set_string("infotext",
"ABM testabsm:missing_neighbor changed this node.")
end
})
-- ABM required and missing neighbor node
minetest.register_node("testabms:required_missing_neighbor", {
description = S("Node for test ABM required_missing_neighbor.") .. "\n"
.. S("Sensitive neighbor nodes are testnodes:normal and testnodes:glasslike."),
drawtype = "normal",
tiles = { "testabms_wait_node.png" },
groups = { dig_immediate = 3 },
on_construct = function (pos)
local meta = minetest.get_meta(pos)
meta:set_string("infotext",
"Waiting for ABM testabms:required_missing_neighbor"
.. " (wint normal drawtype testnode and no glasslike"
.. " drawtype testnode sensitive)")
end,
})
minetest.register_abm({
label = "testabms:required_missing_neighbor",
nodenames = "testabms:required_missing_neighbor",
neighbors = {"testnodes:normal"},
without_neighbors = {"testnodes:glasslike"},
interval = 1,
chance = 1,
action = function (pos)
minetest.swap_node(pos, {name="testabms:after_abm"})
local meta = minetest.get_meta(pos)
meta:set_string("infotext",
"ABM testabsm:required_missing_neighbor changed this node.")
end
})

Binary file not shown.

After

Width:  |  Height:  |  Size: 179 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 183 B

@ -34,10 +34,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
class LuaABM : public ActiveBlockModifier { class LuaABM : public ActiveBlockModifier {
private: private:
int m_id; const int m_id;
std::vector<std::string> m_trigger_contents; std::vector<std::string> m_trigger_contents;
std::vector<std::string> m_required_neighbors; std::vector<std::string> m_required_neighbors;
std::vector<std::string> m_without_neighbors;
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;
@ -47,11 +48,13 @@ public:
LuaABM(int id, LuaABM(int id,
const std::vector<std::string> &trigger_contents, const std::vector<std::string> &trigger_contents,
const std::vector<std::string> &required_neighbors, const std::vector<std::string> &required_neighbors,
const std::vector<std::string> &without_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): 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_without_neighbors(without_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),
@ -67,6 +70,10 @@ public:
{ {
return m_required_neighbors; return m_required_neighbors;
} }
virtual const std::vector<std::string> &getWithoutNeighbors() const
{
return m_without_neighbors;
}
virtual float getTriggerInterval() virtual float getTriggerInterval()
{ {
return m_trigger_interval; return m_trigger_interval;
@ -230,6 +237,11 @@ void ScriptApiEnv::readABMs()
read_nodenames(L, -1, required_neighbors); read_nodenames(L, -1, required_neighbors);
lua_pop(L, 1); lua_pop(L, 1);
std::vector<std::string> without_neighbors;
lua_getfield(L, current_abm, "without_neighbors");
read_nodenames(L, -1, without_neighbors);
lua_pop(L, 1);
float trigger_interval = 10.0; float trigger_interval = 10.0;
getfloatfield(L, current_abm, "interval", trigger_interval); getfloatfield(L, current_abm, "interval", trigger_interval);
@ -250,7 +262,8 @@ void ScriptApiEnv::readABMs()
lua_pop(L, 1); lua_pop(L, 1);
LuaABM *abm = new LuaABM(id, trigger_contents, required_neighbors, LuaABM *abm = new LuaABM(id, trigger_contents, required_neighbors,
trigger_interval, trigger_chance, simple_catch_up, min_y, max_y); without_neighbors, trigger_interval, trigger_chance,
simple_catch_up, min_y, max_y);
env->addActiveBlockModifier(abm); env->addActiveBlockModifier(abm);

@ -827,6 +827,7 @@ struct ActiveABM
{ {
ActiveBlockModifier *abm; ActiveBlockModifier *abm;
std::vector<content_t> required_neighbors; std::vector<content_t> required_neighbors;
std::vector<content_t> without_neighbors;
int chance; int chance;
s16 min_y, max_y; s16 min_y, max_y;
}; };
@ -885,6 +886,10 @@ public:
ndef->getIds(s, aabm.required_neighbors); ndef->getIds(s, aabm.required_neighbors);
SORT_AND_UNIQUE(aabm.required_neighbors); SORT_AND_UNIQUE(aabm.required_neighbors);
for (const auto &s : abm->getWithoutNeighbors())
ndef->getIds(s, aabm.without_neighbors);
SORT_AND_UNIQUE(aabm.without_neighbors);
// Trigger contents // Trigger contents
std::vector<content_t> ids; std::vector<content_t> ids;
for (const auto &s : abm->getTriggerContents()) for (const auto &s : abm->getTriggerContents())
@ -996,8 +1001,11 @@ public:
continue; continue;
// Check neighbors // Check neighbors
if (!aabm.required_neighbors.empty()) { const bool check_required_neighbors = !aabm.required_neighbors.empty();
const bool check_without_neighbors = !aabm.without_neighbors.empty();
if (check_required_neighbors || check_without_neighbors) {
v3s16 p1; v3s16 p1;
bool have_required = false;
for(p1.X = p0.X-1; p1.X <= p0.X+1; p1.X++) for(p1.X = p0.X-1; p1.X <= p0.X+1; p1.X++)
for(p1.Y = p0.Y-1; p1.Y <= p0.Y+1; p1.Y++) for(p1.Y = p0.Y-1; p1.Y <= p0.Y+1; p1.Y++)
for(p1.Z = p0.Z-1; p1.Z <= p0.Z+1; p1.Z++) for(p1.Z = p0.Z-1; p1.Z <= p0.Z+1; p1.Z++)
@ -1015,12 +1023,25 @@ public:
MapNode n = map->getNode(p1 + block->getPosRelative()); MapNode n = map->getNode(p1 + block->getPosRelative());
c = n.getContent(); c = n.getContent();
} }
if (CONTAINS(aabm.required_neighbors, c)) if (check_required_neighbors && !have_required) {
goto neighbor_found; if (CONTAINS(aabm.required_neighbors, c)) {
if (!check_without_neighbors)
goto neighbor_found;
have_required = true;
}
}
if (check_without_neighbors) {
if (CONTAINS(aabm.without_neighbors, c))
goto neighbor_invalid;
}
} }
if (have_required || !check_required_neighbors)
goto neighbor_found;
// No required neighbor found // No required neighbor found
neighbor_invalid:
continue; continue;
} }
neighbor_found: neighbor_found:
abms_run++; abms_run++;

@ -63,6 +63,9 @@ public:
// Set of required neighbors (trigger doesn't happen if none are found) // Set of required neighbors (trigger doesn't happen if none are found)
// Empty = do not check neighbors // Empty = do not check neighbors
virtual const std::vector<std::string> &getRequiredNeighbors() const = 0; virtual const std::vector<std::string> &getRequiredNeighbors() const = 0;
// Set of without neighbors (trigger doesn't happen if any are found)
// Empty = do not check neighbors
virtual const std::vector<std::string> &getWithoutNeighbors() const = 0;
// Trigger interval in seconds // Trigger interval in seconds
virtual float getTriggerInterval() = 0; virtual float getTriggerInterval() = 0;
// Random chance of (1 / return value), 0 is disallowed // Random chance of (1 / return value), 0 is disallowed

@ -122,7 +122,7 @@ void TestServerModManager::testGetMods()
ServerModManager sm(m_worlddir); ServerModManager sm(m_worlddir);
const auto &mods = sm.getMods(); const auto &mods = sm.getMods();
// `ls ./games/devtest/mods | wc -l` + 1 (test mod) // `ls ./games/devtest/mods | wc -l` + 1 (test mod)
UASSERTEQ(std::size_t, mods.size(), 32 + 1); UASSERTEQ(std::size_t, mods.size(), 33 + 1);
// Ensure we found basenodes mod (part of devtest) // Ensure we found basenodes mod (part of devtest)
// and test_mod (for testing MINETEST_MOD_PATH). // and test_mod (for testing MINETEST_MOD_PATH).