2013-09-10 21:18:57 -04:00

253 lines
7.2 KiB
Lua

minetest.register_globalstep(function(dtime)
for _,player in ipairs(minetest.get_connected_players()) do
if player:get_hp() > 0 or not minetest.setting_getbool("enable_damage") then
local pos = player:getpos()
pos.y = pos.y+0.7
local inv = player:get_inventory()
for _,object in ipairs(minetest.env:get_objects_inside_radius(pos, 0.7)) do
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
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 = 0.4,
})
end
object:get_luaentity().itemstring = ""
object:remove()
end
end
end
for _,object in ipairs(minetest.env:get_objects_inside_radius(pos, 1.5)) do
if not object:is_player() and object:get_luaentity() and object:get_luaentity().name == "__builtin:item" then
if object:get_luaentity().collect then
if inv and inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then
local pos1 = pos
pos1.y = pos1.y+0.2
local pos2 = object:getpos()
local vec = {x=pos1.x-pos2.x, y=pos1.y-pos2.y, z=pos1.z-pos2.z}
vec.x = vec.x*2
vec.y = vec.y*2
vec.z = vec.z*2
object:setvelocity(vec)
object:get_luaentity().physical_state = false
object:get_luaentity().flying = false
object:get_luaentity().object:set_properties({
physical = false,
collisionbox = {0,0,0, 0,0,0},
})
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(),
gain = 0.4,
})
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,
collisionbox = {-0.17,-0.17,-0.17, 0.17,0.17,0.17},
})
end
end, {player, object})
end
end
end
end
end
end
end)
function minetest.handle_node_drops(pos, drops, digger)
local inv
if minetest.setting_getbool("creative_mode") and digger and digger:is_player() then
inv = digger:get_inventory()
end
for _,item in ipairs(drops) do
local count, name
if type(item) == "string" then
name, count = item:match("^([a-zA-Z0-9_:]*) ([0-9]*)$")
if not name then
name = item
end
if not count then
count = 1
end
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 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
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
minetest.register_entity(":__builtin:item", {
initial_properties = {
hp_max = 1,
physical = true,
collisionbox = {-0.17,-0.17,-0.17, 0.17,0.17,0.17},
visual = "sprite",
visual_size = {x=0.5, y=0.5},
textures = {""},
spritediv = {x=1, y=1},
initial_sprite_basepos = {x=0, y=0},
collide_with_objects = false,
is_visible = false,
},
itemstring = '',
physical_state = true,
flying = false,
time = 0,
set_item = function(self, itemstring)
self.itemstring = itemstring
local stack = ItemStack(itemstring)
local itemtable = stack:to_table()
local itemname = nil
if itemtable then
itemname = stack:to_table().name
end
local item_texture = nil
local item_type = ""
if minetest.registered_items[itemname] then
item_texture = minetest.registered_items[itemname].inventory_image
item_type = minetest.registered_items[itemname].type
end
prop = {
is_visible = true,
visual = "wielditem",
textures = {itemname},
visual_size = {x=0.20, y=0.20},
automatic_rotate = math.pi * 0.25,
}
self.object:set_properties(prop)
end,
get_staticdata = function(self)
--return self.itemstring
return minetest.serialize({
itemstring = self.itemstring,
always_collect = self.always_collect,
time = self.time,
collect = self.collect,
})
end,
on_activate = function(self, staticdata, dtime_s)
if string.sub(staticdata, 1, string.len("return")) == "return" then
local data = minetest.deserialize(staticdata)
if data and type(data) == "table" then
self.itemstring = data.itemstring
self.always_collect = data.always_collect
self.collect = data.collect
if data.time then
self.time = data.time + dtime_s
end
end
else
self.itemstring = staticdata
end
self.object:set_armor_groups({immortal=1})
self.object:setvelocity({x=0, y=2, z=0})
self.object:setacceleration({x=0, y=-10, z=0})
self:set_item(self.itemstring)
end,
on_step = function(self, dtime)
if self.flying then
return
end
self.time = self.time + dtime
if self.time > 300 then
self.object:remove()
return
end
local p = self.object:getpos()
if minetest.get_item_group(minetest.env:get_node(p).name, "lava") ~= 0 then
self.object:remove()
end
p.y = p.y - 0.3
local nn = minetest.env:get_node(p).name
-- If node is not registered or node is walkably solid and resting on nodebox
local v = self.object:getvelocity()
if not minetest.registered_nodes[nn] or minetest.registered_nodes[nn].walkable and v.y == 0 then
if self.physical_state then
self.object:setvelocity({x=0,y=0,z=0})
self.object:setacceleration({x=0, y=0, z=0})
self.physical_state = false
self.object:set_properties({
physical = false,
collisionbox = {0,0,0, 0,0,0},
})
end
else
if not self.physical_state then
self.object:setvelocity({x=0,y=0,z=0})
self.object:setacceleration({x=0, y=-10, z=0})
self.physical_state = true
self.object:set_properties({
physical = true,
collisionbox = {-0.17,-0.17,-0.17, 0.17,0.17,0.17},
})
end
end
end,
})
minetest.register_on_dieplayer(function(player)
if minetest.setting_getbool("creative_mode") then
return
end
local inv = player:get_inventory()
local pos = player:getpos()
for _,list in ipairs({"main", "craft"}) do
for i,stack in ipairs(inv:get_list(list)) do
local x = math.random(0, 9)/3 - 1.5
local z = math.random(0, 9)/3 - 1.5
pos.x = pos.x + x
pos.z = pos.z + z
local obj = minetest.env:add_item(pos, stack)
if obj then
obj:get_luaentity().collect = true
end
stack:clear()
inv:set_stack(list, i, stack)
pos.x = pos.x - x
pos.z = pos.z - z
end
end
end)