2012-04-01 15:06:01 +02:00
|
|
|
-- Minetest: builtin/item_entity.lua
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
function core.spawn_item(pos, item)
|
2012-04-01 15:06:01 +02:00
|
|
|
-- Take item in any format
|
|
|
|
local stack = ItemStack(item)
|
2014-04-28 03:02:48 +02:00
|
|
|
local obj = core.add_entity(pos, "__builtin:item")
|
2012-04-01 15:06:01 +02:00
|
|
|
obj:get_luaentity():set_item(stack:to_string())
|
|
|
|
return obj
|
|
|
|
end
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
core.register_entity(":__builtin:item", {
|
2012-04-01 15:06:01 +02:00
|
|
|
initial_properties = {
|
|
|
|
hp_max = 1,
|
|
|
|
physical = true,
|
2013-06-14 14:04:46 +02:00
|
|
|
collide_with_objects = false,
|
2012-04-04 12:16:09 +02:00
|
|
|
collisionbox = {-0.17,-0.17,-0.17, 0.17,0.17,0.17},
|
2012-04-01 15:06:01 +02:00
|
|
|
visual = "sprite",
|
|
|
|
visual_size = {x=0.5, y=0.5},
|
|
|
|
textures = {""},
|
|
|
|
spritediv = {x=1, y=1},
|
|
|
|
initial_sprite_basepos = {x=0, y=0},
|
|
|
|
is_visible = false,
|
|
|
|
},
|
|
|
|
|
|
|
|
itemstring = '',
|
|
|
|
physical_state = true,
|
|
|
|
|
|
|
|
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
|
2012-04-04 12:16:09 +02:00
|
|
|
local item_type = ""
|
2014-04-28 03:02:48 +02:00
|
|
|
if core.registered_items[itemname] then
|
|
|
|
item_texture = core.registered_items[itemname].inventory_image
|
|
|
|
item_type = core.registered_items[itemname].type
|
2012-04-01 15:06:01 +02:00
|
|
|
end
|
2012-04-04 12:16:09 +02:00
|
|
|
prop = {
|
2012-04-01 15:06:01 +02:00
|
|
|
is_visible = true,
|
2012-04-04 12:16:09 +02:00
|
|
|
visual = "sprite",
|
2012-11-27 16:32:58 +01:00
|
|
|
textures = {"unknown_item.png"}
|
2012-04-04 12:16:09 +02:00
|
|
|
}
|
2012-11-27 16:32:58 +01:00
|
|
|
if item_texture and item_texture ~= "" then
|
|
|
|
prop.visual = "sprite"
|
|
|
|
prop.textures = {item_texture}
|
|
|
|
prop.visual_size = {x=0.50, y=0.50}
|
|
|
|
else
|
2012-04-04 12:16:09 +02:00
|
|
|
prop.visual = "wielditem"
|
|
|
|
prop.textures = {itemname}
|
|
|
|
prop.visual_size = {x=0.20, y=0.20}
|
|
|
|
prop.automatic_rotate = math.pi * 0.25
|
|
|
|
end
|
|
|
|
self.object:set_properties(prop)
|
2012-04-01 15:06:01 +02:00
|
|
|
end,
|
|
|
|
|
|
|
|
get_staticdata = function(self)
|
2012-09-09 20:29:44 +02:00
|
|
|
--return self.itemstring
|
2014-04-28 03:02:48 +02:00
|
|
|
return core.serialize({
|
2012-09-09 20:29:44 +02:00
|
|
|
itemstring = self.itemstring,
|
|
|
|
always_collect = self.always_collect,
|
|
|
|
})
|
2012-04-01 15:06:01 +02:00
|
|
|
end,
|
|
|
|
|
|
|
|
on_activate = function(self, staticdata)
|
2012-09-09 20:29:44 +02:00
|
|
|
if string.sub(staticdata, 1, string.len("return")) == "return" then
|
2014-04-28 03:02:48 +02:00
|
|
|
local data = core.deserialize(staticdata)
|
2012-09-09 20:29:44 +02:00
|
|
|
if data and type(data) == "table" then
|
|
|
|
self.itemstring = data.itemstring
|
|
|
|
self.always_collect = data.always_collect
|
|
|
|
end
|
|
|
|
else
|
|
|
|
self.itemstring = staticdata
|
|
|
|
end
|
2012-04-01 15:06:01 +02:00
|
|
|
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)
|
|
|
|
local p = self.object:getpos()
|
|
|
|
p.y = p.y - 0.3
|
2014-04-28 03:02:48 +02:00
|
|
|
local nn = core.get_node(p).name
|
2012-12-15 21:32:17 +01:00
|
|
|
-- If node is not registered or node is walkably solid and resting on nodebox
|
|
|
|
local v = self.object:getvelocity()
|
2014-04-28 03:02:48 +02:00
|
|
|
if not core.registered_nodes[nn] or core.registered_nodes[nn].walkable and v.y == 0 then
|
2012-04-01 15:06:01 +02:00
|
|
|
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
|
|
|
|
})
|
|
|
|
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
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
on_punch = function(self, hitter)
|
|
|
|
if self.itemstring ~= '' then
|
2013-01-12 20:18:43 +01:00
|
|
|
local left = hitter:get_inventory():add_item("main", self.itemstring)
|
|
|
|
if not left:is_empty() then
|
2013-03-03 16:34:06 +01:00
|
|
|
self.itemstring = left:to_string()
|
2013-01-12 20:18:43 +01:00
|
|
|
return
|
|
|
|
end
|
2012-04-01 15:06:01 +02:00
|
|
|
end
|
2014-03-18 20:02:18 +01:00
|
|
|
self.itemstring = ''
|
2012-04-01 15:06:01 +02:00
|
|
|
self.object:remove()
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|