Add item pickup age

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

View File

@ -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,38 +52,37 @@ 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
if not inv then
inv = player:get_inventory()
if not inv then
minetest.log("error", "[item_drop] Couldn't " ..
"get inventory")
return
end
end
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 <= 1 then
if inv
and inv:room_for_item("main",
ItemStack(object:get_luaentity().itemstring)
) then
inv:add_item("main", ItemStack( inv:add_item("main", ItemStack(
object:get_luaentity().itemstring)) ent.itemstring))
if object:get_luaentity().itemstring ~= "" then
minetest.sound_play("item_drop_pickup", { minetest.sound_play("item_drop_pickup", {
to_player = player:get_player_name(), to_player = player:get_player_name(),
gain = pickup_gain, gain = pickup_gain,
}) })
end ent.itemstring = ""
object:get_luaentity().itemstring = ""
object:remove() object:remove()
end
else else
if object:get_luaentity().collect then
if inv
and inv:room_for_item("main",
ItemStack(object:get_luaentity().itemstring)
) then
local pos1 = pos local pos1 = pos
pos1.y = pos1.y+0.2 pos1.y = pos1.y+0.2
local vec = {x=pos1.x-pos2.x, y=pos1.y-pos2.y, local vec = {x=pos1.x-pos2.x, y=pos1.y-pos2.y,
@ -76,8 +91,8 @@ if minetest.settings:get_bool("item_drop.enable_item_pickup") ~= false then
vec.y = vec.y*3 vec.y = vec.y*3
vec.z = vec.z*3 vec.z = vec.z*3
object:setvelocity(vec) object:setvelocity(vec)
object:get_luaentity().physical_state = false ent.physical_state = false
object:get_luaentity().object:set_properties({ ent.object:set_properties({
physical = false physical = false
}) })
@ -115,11 +130,10 @@ if minetest.settings:get_bool("item_drop.enable_item_pickup") ~= false then
end end
end end
end end
end
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

View File

@ -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