API for getting objects inside one node.

This commit is contained in:
Aaron Suen 2020-02-09 10:11:13 -05:00
parent db5ff04b1c
commit 266de04987
4 changed files with 34 additions and 27 deletions

View File

@ -403,3 +403,14 @@ end
function nodecore.near_unloaded(pos, radius)
return minetest.find_node_near(pos, radius or 1, {"ignore"}, true)
end
function nodecore.get_objects_at_pos(pos)
pos = vector.round(pos)
local t = {}
for _, obj in pairs(minetest.get_objects_inside_radius(pos, 0.866025403784)) do
if vector.equals(vector.round(obj:get_pos()), pos) then
t[#t + 1] = obj
end
end
return t
end

View File

@ -104,12 +104,10 @@ function nodecore.entity_settle_check(on_settle, isnode)
if not on_settle(self, pos, collides) then return end
pos.y = pos.y + 1
for _, obj in pairs(minetest.get_objects_inside_radius(pos, 2)) do
if vector.equals(vector.round(obj:get_pos()), pos) then
obj = obj.get_luaentity and obj:get_luaentity()
if obj and obj.settle_check then
obj:settle_check()
end
for _, obj in pairs(nodecore.get_objects_at_pos(pos)) do
obj = obj.get_luaentity and obj:get_luaentity()
if obj and obj.settle_check then
obj:settle_check()
end
end

View File

@ -16,26 +16,24 @@ function nodecore.item_ent_merge(pos)
cache[hash] = (t or nodecore.gametime) + 0.75 + 0.5 * math_random()
local db = {}
for _, obj in pairs(minetest.get_objects_inside_radius(pos, 1)) do
if vector.equals(vector.round(obj:get_pos()), pos) then
local lua = obj.get_luaentity and obj:get_luaentity()
if lua and lua.name == "__builtin:item" then
local stack = ItemStack(lua.itemstring or "")
if not stack:is_empty() then
local qty = stack:get_count()
stack:set_count(1)
local key = stack:to_string()
local entry = db[key]
if entry then
entry.qty = entry.qty + qty
entry.objs[#entry.objs + 1] = {obj = obj, lua = lua}
else
db[key] = {
stack = stack,
qty = qty,
objs = {{obj = obj, lua = lua}}
}
end
for _, obj in pairs(nodecore.get_objects_at_pos(pos, 1)) do
local lua = obj.get_luaentity and obj:get_luaentity()
if lua and lua.name == "__builtin:item" then
local stack = ItemStack(lua.itemstring or "")
if not stack:is_empty() then
local qty = stack:get_count()
stack:set_count(1)
local key = stack:to_string()
local entry = db[key]
if entry then
entry.qty = entry.qty + qty
entry.objs[#entry.objs + 1] = {obj = obj, lua = lua}
else
db[key] = {
stack = stack,
qty = qty,
objs = {{obj = obj, lua = lua}}
}
end
end
end

View File

@ -51,7 +51,7 @@ function nodecore.visinv_update_ents(pos, node)
local max = def.groups and def.groups.visinv and 1 or 0
local found = {}
for _, v in pairs(minetest.get_objects_inside_radius(pos, 0.5)) do
for _, v in pairs(nodecore.get_objects_at_pos(pos)) do
if v and v.get_luaentity and v:get_luaentity()
and v:get_luaentity().is_stack then
found[#found + 1] = v