Aaron Suen 9c9eb4b85a Guard against dummy objects in 5.3+
Some time in the 5.3 dev stream (docs updated at
217f3a42), object refs started being invalidated
immediately on calling obj:remove(), such that
obj:get_pos() starts to return nil instead of the object's
last known position.

This can cause some crashes in NodeCore, where we
assume that our object is still valid (or usable as if it
were still valid) even though we're looping through
handlers and any one of them may have remove()d the
object before other handlers get a chance to fire.

Instead, just watch for unexpected nil returns from
functions we expect would never return nil (e.g.
get_pos or get_properties) and return if we hit one.
We can assume all other calls will be non-nil after that
one, as long as we stay in the same function flow.
2020-05-18 18:36:06 -04:00

53 lines
1.6 KiB
Lua

-- LUALOCALS < ---------------------------------------------------------
local ItemStack, minetest, nodecore, pairs
= ItemStack, minetest, nodecore, pairs
-- LUALOCALS > ---------------------------------------------------------
local function getcrushdamage(name, alreadyloose)
local def = minetest.registered_items[name]
if def and def.crush_damage then return def.crush_damage end
if alreadyloose then return 0 end
return name and getcrushdamage(name .. "_loose", true) or 0
end
local function maketick(mult, getname, oldtick)
oldtick = oldtick or function() end
return function(self, dtime, ...)
self.crush_damage = self.crush_damage or getcrushdamage(getname(self))
if self.crush_damage <= 0 then
return oldtick(self, dtime, ...)
end
local pos = self.object:get_pos()
if not pos then return end
pos.y = pos.y - 1
local vel = self.object:get_velocity()
local v = vel and -vel.y or 0
if v <= 0 then
return oldtick(self, dtime, ...)
end
local q = v * v * dtime * self.crush_damage * mult
local hit
for _, o in pairs(minetest.get_objects_inside_radius(pos, 1)) do
if o:is_player() then
hit = hit or nodecore.addphealth(o, -q, {
nc_type = "crushing",
entity = self
})
end
end
if hit and vel then
self.object:set_velocity({
x = vel.x / 2,
y = -vel.y / 4,
z = vel.z / 2
})
end
return oldtick(self, dtime, ...)
end
end
nodecore.register_falling_node_step(maketick(1, function(s) return s.node.name end))
nodecore.register_item_entity_step(maketick(0.2, function(s) return ItemStack(s.itemstring):get_name() end))