RealBadAngel
2012-12-13 b8d77627a4d28c624e63423eef317dd09c68e533
commit | author | age
c362f4 1 -- This part written by PilzAdam (item_drop mod)
MK 2
3 minetest.register_globalstep(function(dtime)
4     for _,player in ipairs(minetest.get_connected_players()) do
5         local pos = player:getpos()
6         pos.y = pos.y+0.5
7         local inv = player:get_inventory()
8         
9         for _,object in ipairs(minetest.env:get_objects_inside_radius(pos, 1)) do
10             if not object:is_player() and object:get_luaentity() and object:get_luaentity().name == "__builtin:item" then
11                 if inv and inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then
12                     inv:add_item("main", ItemStack(object:get_luaentity().itemstring))
13                     if object:get_luaentity().itemstring ~= "" then
14                         minetest.sound_play("item_drop_pickup", {
15                             to_player = player:get_player_name(),
16                         })
17                     end
18                     object:get_luaentity().itemstring = ""
19                     object:remove()
20                 end
21             end
22         end
23         
24         for _,object in ipairs(minetest.env:get_objects_inside_radius(pos, 2)) do
25             if not object:is_player() and object:get_luaentity() and object:get_luaentity().name == "__builtin:item" then
26                 if object:get_luaentity().collect then
27                     if inv and inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then
28                         local pos1 = pos
29                         pos1.y = pos1.y+0.2
30                         local pos2 = object:getpos()
31                         local vec = {x=pos1.x-pos2.x, y=pos1.y-pos2.y, z=pos1.z-pos2.z}
32                         vec.x = vec.x*3
33                         vec.y = vec.y*3
34                         vec.z = vec.z*3
35                         object:setvelocity(vec)
36                         
37                         minetest.after(1, function(args)
38                             local lua = object:get_luaentity()
39                             if object == nil or lua == nil or lua.itemstring == nil then
40                                 return
41                             end
42                             if inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then
43                                 inv:add_item("main", ItemStack(object:get_luaentity().itemstring))
44                                 if object:get_luaentity().itemstring ~= "" then
45                                     minetest.sound_play("item_drop_pickup", {
46                                         to_player = player:get_player_name(),
47                                     })
48                                 end
49                                 object:get_luaentity().itemstring = ""
50                                 object:remove()
51                             else
52                                 object:setvelocity({x=0,y=0,z=0})
53                             end
54                         end, {player, object})
55                         
56                     end
57                 end
58             end
59         end
60     end
61 end)
62
63 function minetest.handle_node_drops(pos, drops, digger)
64     for _,item in ipairs(drops) do
65         local count, name
66         if type(item) == "string" then
67             count = 1
68             name = item
69         else
70             count = item:get_count()
71             name = item:get_name()
72         end
73         for i=1,count do
74             local obj = minetest.env:add_item(pos, name)
75             if obj ~= nil then
76                 obj:get_luaentity().collect = true
77                 local x = math.random(1, 5)
78                 if math.random(1,2) == 1 then
79                     x = -x
80                 end
81                 local z = math.random(1, 5)
82                 if math.random(1,2) == 1 then
83                     z = -z
84                 end
85                 obj:setvelocity({x=1/x, y=obj:getvelocity().y, z=1/z})
86                 
87                 -- FIXME this doesnt work for deactiveted objects
88                 if minetest.setting_get("remove_items") and tonumber(minetest.setting_get("remove_items")) then
89                     minetest.after(tonumber(minetest.setting_get("remove_items")), function(obj)
90                         obj:remove()
91                     end, obj)
92                 end
93             end
94         end
95     end
96 end
97
98 if minetest.setting_get("log_mods") then
99     minetest.log("action", "item_drop loaded")
100 end