Add item pickup age

This commit is contained in:
Hybrid Dog 2017-09-07 20:39:49 +02:00
parent fb2c076b92
commit 9f54694931
2 changed files with 84 additions and 67 deletions

148
init.lua

@ -3,6 +3,8 @@ 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 pickup_age = tonumber(
minetest.settings:get("item_drop.pickup_age")) or 0.5
local key_triggered = minetest.settings:get_bool( local key_triggered = minetest.settings:get_bool(
"item_drop.enable_pickup_key") ~= false "item_drop.enable_pickup_key") ~= false
local keytype local keytype
@ -11,6 +13,20 @@ 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
local function opt_get_ent(object)
if object:is_player() then
return
end
local ent = object:get_luaentity()
if not ent
or ent.name ~= "__builtin:item"
or (ent.dropped_by and ent.age < pickup_age) then
return
end
return ent
end
local function pickupfunc() local function pickupfunc()
for _,player in ipairs(minetest.get_connected_players()) do for _,player in ipairs(minetest.get_connected_players()) do
local keys_pressed = not key_triggered local keys_pressed = not key_triggered
@ -36,80 +52,78 @@ if minetest.settings:get_bool("item_drop.enable_item_pickup") ~= false then
local pos = player:getpos() local pos = player:getpos()
pos.y = pos.y+0.5 pos.y = pos.y+0.5
local inv = player:get_inventory() local inv
local objectlist = minetest.get_objects_inside_radius(pos, local objectlist = minetest.get_objects_inside_radius(pos,
pickup_radius) pickup_radius)
for _,object in ipairs(objectlist) do for i = 1,#objectlist do
if not object:is_player() local object = objectlist[i]
and object:get_luaentity() local ent = opt_get_ent(object)
and object:get_luaentity().name == "__builtin:item" then if ent then
local pos2 = object:getpos() if not inv then
local distance = vector.distance(pos, pos2) inv = player:get_inventory()
if distance <= 1 then if not inv then
if inv minetest.log("error", "[item_drop] Couldn't " ..
and inv:room_for_item("main", "get inventory")
ItemStack(object:get_luaentity().itemstring) return
) then
inv:add_item("main", ItemStack(
object:get_luaentity().itemstring))
if object:get_luaentity().itemstring ~= "" then
minetest.sound_play("item_drop_pickup", {
to_player = player:get_player_name(),
gain = pickup_gain,
})
end
object:get_luaentity().itemstring = ""
object:remove()
end end
else end
if object:get_luaentity().collect then if inv:room_for_item("main",
if inv ItemStack(ent.itemstring)
and inv:room_for_item("main", ) then
ItemStack(object:get_luaentity().itemstring) local pos2 = object:getpos()
) then local distance = vector.distance(pos, pos2)
local pos1 = pos if distance <= 1 then
pos1.y = pos1.y+0.2 inv:add_item("main", ItemStack(
local vec = {x=pos1.x-pos2.x, y=pos1.y-pos2.y, ent.itemstring))
z=pos1.z-pos2.z} minetest.sound_play("item_drop_pickup", {
vec.x = vec.x*3 to_player = player:get_player_name(),
vec.y = vec.y*3 gain = pickup_gain,
vec.z = vec.z*3 })
object:setvelocity(vec) ent.itemstring = ""
object:get_luaentity().physical_state = false object:remove()
object:get_luaentity().object:set_properties({ else
physical = false local pos1 = pos
}) pos1.y = pos1.y+0.2
local vec = {x=pos1.x-pos2.x, y=pos1.y-pos2.y,
z=pos1.z-pos2.z}
vec.x = vec.x*3
vec.y = vec.y*3
vec.z = vec.z*3
object:setvelocity(vec)
ent.physical_state = false
ent.object:set_properties({
physical = false
})
minetest.after(1, function() minetest.after(1, function()
local lua = object:get_luaentity() local lua = object:get_luaentity()
if not lua or not lua.itemstring then if not lua or not lua.itemstring then
return return
end end
if inv:room_for_item("main", if inv:room_for_item("main",
ItemStack(object:get_luaentity(
).itemstring)
) then
inv:add_item("main",
ItemStack(object:get_luaentity( ItemStack(object:get_luaentity(
).itemstring) ).itemstring))
) then if object:get_luaentity().itemstring ~= "" then
inv:add_item("main", minetest.sound_play("item_drop_pickup", {
ItemStack(object:get_luaentity( to_player = player:get_player_name(),
).itemstring)) gain = pickup_gain,
if object:get_luaentity().itemstring ~= "" then
minetest.sound_play("item_drop_pickup", {
to_player = player:get_player_name(),
gain = pickup_gain,
})
end
object:get_luaentity().itemstring = ""
object:remove()
else
object:setvelocity({x=0,y=0,z=0})
object:get_luaentity().physical_state = true
object:get_luaentity().object:set_properties({
physical = true
}) })
end end
end, {player, object}) object:get_luaentity().itemstring = ""
end object:remove()
else
object:setvelocity({x=0,y=0,z=0})
object:get_luaentity().physical_state = true
object:get_luaentity().object:set_properties({
physical = true
})
end
end, {player, object})
end end
end end
end end
@ -119,7 +133,7 @@ if minetest.settings:get_bool("item_drop.enable_item_pickup") ~= false then
local function pickup_step() local function pickup_step()
pickupfunc() pickupfunc()
minetest.after(0.2, pickup_step) minetest.after(0.01, pickup_step)
end end
minetest.after(3.0, pickup_step) minetest.after(3.0, pickup_step)
end end

@ -15,3 +15,6 @@ item_drop.pickup_sound_gain (Pickup sound gain) float 0.4
#Player pickup radius #Player pickup radius
item_drop.pickup_radius (Pickup radius) float 0.75 item_drop.pickup_radius (Pickup radius) float 0.75
#Time delay in seconds after autopicking an item if it's dropped by a player
item_drop.pickup_age (Pickup age) float 0.5