item_drop/init.lua

187 lines
4.7 KiB
Lua
Raw Normal View History

2017-09-06 15:56:04 +02:00
if minetest.settings:get_bool("item_drop.enable_item_pickup") ~= false then
local pickup_gain = tonumber(
minetest.settings:get("item_drop.pickup_sound_gain")) or 0.4
local pickup_radius = tonumber(
minetest.settings:get("item_drop.pickup_radius")) or 0.75
2017-09-07 20:39:49 +02:00
local pickup_age = tonumber(
minetest.settings:get("item_drop.pickup_age")) or 0.5
2017-09-06 15:56:04 +02:00
local key_triggered = minetest.settings:get_bool(
"item_drop.enable_pickup_key") ~= false
2017-09-06 15:51:34 +02:00
local keytype
if key_triggered then
2017-09-06 15:56:04 +02:00
keytype = minetest.settings:get("item_drop.pickup_keytype") or "Use"
2017-09-06 15:51:34 +02:00
end
local damage_enabled = minetest.settings:get_bool("enable_damage")
2017-09-07 20:39:49 +02:00
-- 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"
2017-09-07 20:57:02 +02:00
or (ent.dropped_by and ent.age < pickup_age)
or ent.itemstring == "" then
2017-09-07 20:39:49 +02:00
return
end
return ent
end
2017-09-07 20:57:02 +02:00
-- take item or reset velocity after flying a second
local 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
2017-09-07 21:00:55 +02:00
local function pickupfunc(player)
local keys_pressed = not key_triggered
2017-09-06 15:51:34 +02:00
2017-09-07 21:00:55 +02:00
local control = player:get_player_control()
2017-09-06 15:51:34 +02:00
2017-09-07 21:00:55 +02:00
if keytype == "Use" then
keys_pressed = control.aux1
elseif keytype == "Sneak" then
keys_pressed = control.sneak
elseif keytype == "LeftAndRight" then -- LeftAndRight combination
keys_pressed = control.left and control.right
elseif keytype == "RMB" then
keys_pressed = control.RMB
elseif keytype == "SneakAndRMB" then -- SneakAndRMB combination
keys_pressed = control.sneak and control.RMB
end
2017-09-06 15:51:34 +02:00
2017-09-07 21:00:55 +02:00
if not keys_pressed
or (damage_enabled and player:get_hp() <= 0) then
return
end
2017-09-06 15:51:34 +02:00
2017-09-07 21:00:55 +02:00
local pos = player:getpos()
pos.y = pos.y+0.5
local inv
2017-09-06 15:51:34 +02:00
2017-09-07 21:00:55 +02:00
local objectlist = minetest.get_objects_inside_radius(pos,
pickup_radius)
for i = 1,#objectlist do
local object = objectlist[i]
local ent = opt_get_ent(object)
if ent then
if not inv then
inv = player:get_inventory()
2017-09-07 20:39:49 +02:00
if not inv then
2017-09-07 21:00:55 +02:00
minetest.log("error", "[item_drop] Couldn't " ..
"get inventory")
return
2017-09-07 20:39:49 +02:00
end
2017-09-07 21:00:55 +02:00
end
if inv:room_for_item("main", ItemStack(ent.itemstring)) then
local pos2 = object:getpos()
local distance = vector.distance(pos, pos2)
if distance <= 1 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
local vel = vector.multiply(
vector.subtract(pos, pos2), 3)
vel.y = vel.y + 0.6
object:setvelocity(vel)
ent.physical_state = false
ent.object:set_properties({
physical = false
})
2017-09-06 15:51:34 +02:00
2017-09-07 21:00:55 +02:00
minetest.after(1.0, afterflight,
object, inv, player)
2017-09-06 15:51:34 +02:00
end
end
end
end
end
local function pickup_step()
2017-09-07 21:00:55 +02:00
local players = minetest.get_connected_players()
for i = 1,#players do
pickupfunc(players[i])
end
2017-09-07 20:39:49 +02:00
minetest.after(0.01, pickup_step)
2017-09-06 15:51:34 +02:00
end
minetest.after(3.0, pickup_step)
end
2012-09-01 03:49:44 +02:00
2017-09-06 15:56:04 +02:00
if minetest.settings:get_bool("item_drop.enable_item_drop") ~= false then
2017-09-06 15:51:34 +02:00
function minetest.handle_node_drops(pos, drops, digger)
local inv
local diggerPos = pos
if minetest.settings:get_bool("creative_mode")
and digger
and digger:is_player() then
inv = digger:get_inventory()
diggerPos = digger:getpos()
end
for _,item in ipairs(drops) do
local count, name
if type(item) == "string" then
count = 1
name = item
else
count = item:get_count()
name = item:get_name()
end
if not inv or not inv:contains_item("main", ItemStack(name)) then
for i=1,count do
local adjustedPos = {x=diggerPos.x, y=diggerPos.y,
z=diggerPos.z}
local obj = minetest.add_item(adjustedPos, name)
if obj ~= nil then
obj:get_luaentity().collect = true
local x = math.random(1, 5)
if math.random(1,2) == 1 then x = -x end
local z = math.random(1, 5)
if math.random(1,2) == 1 then z = -z end
obj:setvelocity({x=1/x, y=obj:getvelocity().y, z=1/z})
end
end
end
end
end
2012-09-03 18:52:59 +02:00
end
2012-10-07 19:44:16 +02:00
2017-09-06 15:51:34 +02:00
if minetest.settings:get("log_mods") then
minetest.log("action", "[Mod] item_drop loaded")
end