item_drop/init.lua

95 lines
2.9 KiB
Lua
Raw Normal View History

2012-09-01 00:52:41 +02:00
minetest.register_globalstep(function(dtime)
2012-09-01 03:49:44 +02:00
for _,player in ipairs(minetest.get_connected_players()) do
2012-09-01 00:52:41 +02:00
local pos = player:getpos()
pos.y = pos.y+0.5
2012-09-01 03:49:44 +02:00
local inv = player:get_inventory()
for _,object in ipairs(minetest.env:get_objects_inside_radius(pos, 1)) do
2012-09-09 17:04:27 +02:00
if not object:is_player() and object:get_luaentity() and object:get_luaentity().name == "__builtin:item" then
if inv and inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then
2012-09-01 03:49:44 +02:00
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(),
})
end
object:get_luaentity().itemstring = ""
object:remove()
2012-09-01 00:52:41 +02:00
end
end
end
2012-09-01 03:49:44 +02:00
for _,object in ipairs(minetest.env:get_objects_inside_radius(pos, 2)) do
2012-09-09 17:04:27 +02:00
if not object:is_player() and object:get_luaentity() and object:get_luaentity().name == "__builtin:item" then
2012-09-01 03:49:44 +02:00
if object:get_luaentity().collect then
if inv and inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then
2012-09-01 03:49:44 +02:00
local pos1 = pos
2012-09-02 15:57:42 +02:00
pos1.y = pos1.y+0.2
2012-09-01 03:49:44 +02:00
local pos2 = object:getpos()
local vec = {x=pos1.x-pos2.x, y=pos1.y-pos2.y, z=pos1.z-pos2.z}
2012-09-02 15:57:42 +02:00
vec.x = vec.x*3
vec.y = vec.y*3
vec.z = vec.z*3
object:setvelocity(vec)
minetest.after(1, function(args)
local lua = object:get_luaentity()
if object == nil or lua == nil or lua.itemstring == nil then
return
end
if inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) 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(),
})
end
object:get_luaentity().itemstring = ""
object:remove()
else
object:setvelocity({x=0,y=0,z=0})
end
end, {player, object})
2012-09-01 00:52:41 +02:00
end
end
end
end
end
2012-09-01 03:49:44 +02:00
end)
function minetest.handle_node_drops(pos, drops, digger)
2012-09-03 18:52:59 +02:00
for _,item in ipairs(drops) do
local count, name
2012-09-02 16:12:39 +02:00
if type(item) == "string" then
2012-09-03 18:52:59 +02:00
count = 1
name = item
2012-09-02 16:12:39 +02:00
else
2012-09-03 18:52:59 +02:00
count = item:get_count()
name = item:get_name()
end
for i=1,count do
local obj = minetest.env:add_item(pos, 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
2012-09-01 00:52:41 +02:00
end
2012-09-03 18:52:59 +02:00
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})
-- FIXME this doesnt work for deactiveted objects
if minetest.setting_get("remove_items") and tonumber(minetest.setting_get("remove_items")) then
minetest.after(tonumber(minetest.setting_get("remove_items")), function(obj)
obj:remove()
end, obj)
end
2012-09-01 00:52:41 +02:00
end
end
end
2012-09-03 18:52:59 +02:00
end