Add code for zero velocity mode

This commit is contained in:
Hybrid Dog 2017-09-07 21:13:10 +02:00
parent f91c73d2e3
commit fced0590c6

@ -3,6 +3,7 @@ if minetest.settings:get_bool("item_drop.enable_item_pickup") ~= false then
minetest.settings:get("item_drop.pickup_sound_gain")) or 0.4 minetest.settings:get("item_drop.pickup_sound_gain")) or 0.4
local pickup_radius = tonumber( local pickup_radius = tonumber(
minetest.settings:get("item_drop.pickup_radius")) or 0.75 minetest.settings:get("item_drop.pickup_radius")) or 0.75
local inner_radius = 1
local pickup_age = tonumber( local pickup_age = tonumber(
minetest.settings:get("item_drop.pickup_age")) or 0.5 minetest.settings:get("item_drop.pickup_age")) or 0.5
local key_triggered = minetest.settings:get_bool( local key_triggered = minetest.settings:get_bool(
@ -13,45 +14,66 @@ if minetest.settings:get_bool("item_drop.enable_item_pickup") ~= false then
end end
local damage_enabled = minetest.settings:get_bool("enable_damage") local damage_enabled = minetest.settings:get_bool("enable_damage")
-- gets the object's luaentity if it can be collected -- TODO add proper magnet range settings
local function opt_get_ent(object) local zero_velocity_mode = pickup_age == -1
if object:is_player() then and pickup_radius <= inner_radius
return
end
local ent = object:get_luaentity()
if not ent
or ent.name ~= "__builtin:item"
or (ent.dropped_by and ent.age < pickup_age)
or ent.itemstring == "" then
return
end
return ent
end
-- take item or reset velocity after flying a second -- opt_get_ent gets the object's luaentity if it can be collected
local function afterflight(object, inv, player) local opt_get_ent, afterflight
local ent = opt_get_ent(object) if zero_velocity_mode then
if not ent then function opt_get_ent(object)
return if object:is_player()
or not vector.equals(object:getvelocity(), {x=0, y=0, z=0}) then
return
end
local ent = object:get_luaentity()
if not ent
or ent.name ~= "__builtin:item"
or ent.itemstring == "" then
return
end
return ent
end end
if inv else
and inv:room_for_item("main", function opt_get_ent(object)
ItemStack(ent.itemstring) if object:is_player() then
) then return
inv:add_item("main", end
ItemStack(ent.itemstring)) local ent = object:get_luaentity()
minetest.sound_play("item_drop_pickup", { if not ent
to_player = player:get_player_name(), or ent.name ~= "__builtin:item"
gain = pickup_gain, or (ent.dropped_by and ent.age < pickup_age)
}) or ent.itemstring == "" then
ent.itemstring = "" return
object:remove() end
else return ent
object:setvelocity({x=0,y=0,z=0}) end
ent.physical_state = true
ent.object:set_properties({ -- take item or reset velocity after flying a second
physical = true function afterflight(object, inv, player)
}) local ent = opt_get_ent(object)
if not ent then
return
end
if inv
and inv:room_for_item("main",
ItemStack(ent.itemstring)
) then
inv:add_item("main",
ItemStack(ent.itemstring))
minetest.sound_play("item_drop_pickup", {
to_player = player:get_player_name(),
gain = pickup_gain,
})
ent.itemstring = ""
object:remove()
else
object:setvelocity({x=0,y=0,z=0})
ent.physical_state = true
ent.object:set_properties({
physical = true
})
end
end end
end end
@ -98,7 +120,7 @@ if minetest.settings:get_bool("item_drop.enable_item_pickup") ~= false then
if inv:room_for_item("main", ItemStack(ent.itemstring)) then if inv:room_for_item("main", ItemStack(ent.itemstring)) then
local pos2 = object:getpos() local pos2 = object:getpos()
local distance = vector.distance(pos, pos2) local distance = vector.distance(pos, pos2)
if distance <= 1 then if distance <= inner_radius then
inv:add_item("main", ItemStack( inv:add_item("main", ItemStack(
ent.itemstring)) ent.itemstring))
minetest.sound_play("item_drop_pickup", { minetest.sound_play("item_drop_pickup", {