Fix item magnet

There is no field named physical_state, so the item didn't lose the physical property, but it happily hopped towards me after I dropped it.
Disabling physical looked quite wrong, because the item flew through mountains. Instead, it gets zero acceleration now.
To avoid resetting the acceleration in the on_step function, it is overridden to disable acceleration and velocity changes during executing the original on_step.
Small code fix: Add underscores in set_velocity and set_acceleration
This commit is contained in:
HybridDog 2018-10-03 17:07:25 +02:00
parent 1d2e42eaab
commit 02683056e5

View File

@ -142,13 +142,38 @@ minetest.settings:get_bool("enable_item_pickup") ~= false then
and inv:room_for_item("main", item) then and inv:room_for_item("main", item) then
collect_item(ent, object:get_pos(), player) collect_item(ent, object:get_pos(), player)
else else
object:setvelocity({x=0,y=0,z=0}) -- the acceleration will be reset by the object's on_step
ent.physical_state = true object:set_velocity({x=0,y=0,z=0})
ent.object:set_properties({ ent.is_magnet_item = false
physical = true
})
end end
end end
-- disable velocity and acceleration changes of items flying to players
minetest.after(0, function()
local ObjectRef
local blocked_methods = {"set_acceleration", "set_velocity",
"setacceleration", "setvelocity"}
local itemdef = minetest.registered_entities["__builtin:item"]
local old_on_step = itemdef.on_step
local function do_nothing() end
function itemdef.on_step(self, dtime)
if not self.is_magnet_item then
return old_on_step(self, dtime)
end
ObjectRef = ObjectRef or getmetatable(self.object)
local old_funcs = {}
for i = 1, #blocked_methods do
local method = blocked_methods[i]
old_funcs[method] = ObjectRef[method]
ObjectRef[method] = do_nothing
end
old_on_step(self, dtime)
for i = 1, #blocked_methods do
local method = blocked_methods[i]
ObjectRef[method] = old_funcs[method]
end
end
end)
end end
-- set keytype to the key name if possible -- set keytype to the key name if possible
@ -221,15 +246,12 @@ minetest.settings:get_bool("enable_item_pickup") ~= false then
collect_item(ent, pos, player) collect_item(ent, pos, player)
return true return true
end end
local vel = vector.multiply( local vel = vector.multiply(vector.subtract(pos, pos2), 3)
vector.subtract(pos, pos2), 3)
vel.y = vel.y + 0.6 vel.y = vel.y + 0.6
object:setvelocity(vel) object:set_velocity(vel)
if ent.physical_state then if not ent.is_magnet_item then
ent.physical_state = false ent.object:set_acceleration({x=0, y=0, z=0})
ent.object:set_properties({ ent.is_magnet_item = true
physical = false
})
minetest.after(magnet_time, afterflight, minetest.after(magnet_time, afterflight,
object, inv, player) object, inv, player)
@ -294,7 +316,7 @@ and not minetest.settings:get_bool("creative_mode") then
z = z+1 z = z+1
end end
vel.z = 1 / z vel.z = 1 / z
obj:setvelocity(vel) obj:set_velocity(vel)
end end
end end
end end