Skip to content

Commit

Permalink
Add test nodes and unittest for override entity. Fix bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
sfence committed Aug 11, 2024
1 parent 56b674d commit 02f6071
Show file tree
Hide file tree
Showing 19 changed files with 354 additions and 6 deletions.
11 changes: 7 additions & 4 deletions builtin/game/register.lua
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,18 @@ function core.override_entity(name, redefinition)
if redefinition.name ~= nil then
error("Attempt to redefine entity name of "..name.." to "..dump(redefinition.name), 2)
end
if redefinition.type ~= nil then
error("Attempt to redefine type of "..name.." to "..dump(redefinition.type), 2)
end
local entity = core.registered_entities[name]
if not entity then
error("Attempt to override non-existent entity "..name, 2)
end
for k, v in pairs(redefinition) do
rawset(entity, k, v)
if k ~= "initial_properties" then
rawset(entity, k, v)
else
for k2, v2 in pairs(v) do
rawset(entity.initial_properties, k2, v2)
end
end
end
core.registered_entities[name] = entity
end
Expand Down
6 changes: 6 additions & 0 deletions games/devtest/mods/testabms/README.md
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 12 additions & 0 deletions games/devtest/mods/testabms/after_node.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

local S = minetest.get_translator("testabms")

-- 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 },
})

56 changes: 56 additions & 0 deletions games/devtest/mods/testabms/chances.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
-- test ABMs with different chances

local S = minetest.get_translator("testabms")

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

8 changes: 8 additions & 0 deletions games/devtest/mods/testabms/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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")
dofile(path.."/override.lua")
56 changes: 56 additions & 0 deletions games/devtest/mods/testabms/intervals.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
-- test ABMs with different interval

local S = minetest.get_translator("testabms")

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

58 changes: 58 additions & 0 deletions games/devtest/mods/testabms/min_max.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
-- test ABMs with min_y and max_y

local S = minetest.get_translator("testabms")

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

2 changes: 2 additions & 0 deletions games/devtest/mods/testabms/mod.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = testabms
description = Contains some nodes for test ABMs.
30 changes: 30 additions & 0 deletions games/devtest/mods/testabms/neighbors.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- test ABMs with neighbor and without_neighbor

local S = minetest.get_translator("testabms")

-- ABM required neighboor
minetest.register_node("testabms:required_neighbor", {
description = S("Node for test ABM required_neighbor."),
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
})
40 changes: 40 additions & 0 deletions games/devtest/mods/testabms/override.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
-- test ABMs with override

local S = minetest.get_translator("testabms")

-- ABM override
minetest.register_node("testabms:override", {
description = S("Node for test ABM override"),
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:overrid")
end,
})

minetest.register_abm({
label = "testabms:override",
name = "testabms:override",
nodenames = "testabms:override",
interval = 1000,
chance = 5000,
action = function (pos)
minetest.swap_node(pos, {name="testabms:after_abm"})
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "ABM testabms:override changed this node.")
end
})

minetest.override_abm("testabms:override", {
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", "Override ABM testabms:override changed this node.")
end
})
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions games/devtest/mods/testlbms/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Test LBMs

This mod contains a nodes and related LBM actions.
By placing these nodes, you can test basic LBM behaviours.

45 changes: 45 additions & 0 deletions games/devtest/mods/testlbms/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
local S = minetest.get_translator("testlbms")

-- After LBM node
minetest.register_node("testlbms:after_lbm", {
description = S("After LBM processed node."),
drawtype = "normal",
tiles = { "testlbms_after_node.png" },

groups = { dig_immediate = 3 },
})

-- LBM onload change
minetest.register_node("testlbms:onload_change", {
description = S("Node for test LBM"),
drawtype = "normal",
tiles = { "testlbms_wait_node.png" },

groups = { dig_immediate = 3 },

on_construct = function (pos)
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "Waiting for LBM testlbms:chance_5")
end,
})

minetest.register_lbm({
label = "testlbms:onload_change",
name = "testlbms:onload_change",
nodenames = "testlbms:onload_change",
run_at_every_load = true,
action = function (pos)
minetest.swap_node(pos, {name="testlbms:after_lbm"})
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "LBM testlbms:onload_change changed this node.")
end
})

minetest.override_lbm("testlbms:onload_change", {
action = function (pos)
minetest.swap_node(pos, {name="testlbms:after_lbm"})
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "Override LBM testlbms:onload_change changed this node.")
end,
})

2 changes: 2 additions & 0 deletions games/devtest/mods/testlbms/mod.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = testlbms
description = Contains some nodes for test LBMs.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 26 additions & 1 deletion games/devtest/mods/unittests/entity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ end

core.register_entity("unittests:callbacks", {
initial_properties = {
hp_max = 5,
hp_max = 1,
visual = "upright_sprite",
textures = { "unittests_callback.png" },
static_save = false,
Expand Down Expand Up @@ -52,6 +52,13 @@ core.register_entity("unittests:callbacks", {
end,
})

core.override_entity("unittests:callbacks", {
initial_properties = {
hp_max = 5,
},
_lua_hp_max = 6,
})

--

local function check_log(expect)
Expand Down Expand Up @@ -184,3 +191,21 @@ unittests.register("test_objects_in_area", function(_, pos)
return core.objects_in_area(pos:offset(-1, -1, -1), pos:offset(1, 1, 1))
end)
end, {map=true})

unittests.register("test_entity_override", function(_, pos)
log = {}

local obj = core.add_entity(pos, "unittests:callbacks")
check_log({"on_activate(0)"})

-- check properties
local props = obj:get_properties()
assert(props.hp_max == 5)
assert(props.visual == "upright_sprite")

-- check entity
local lua = obj:get_luaentity()
assert(lua._lua_hp_max == 6)

obj:remove()
end, {map=true})
Loading

0 comments on commit 02f6071

Please sign in to comment.