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")
|
2015-09-29 17:26:07 +02:00
|
|
|
-- Don't use obj if it couldn't be added to the map.
|
|
|
|
if obj then
|
|
|
|
obj:get_luaentity():set_item(stack:to_string())
|
|
|
|
end
|
2012-04-01 15:06:01 +02:00
|
|
|
return obj
|
|
|
|
end
|
|
|
|
|
2015-09-29 17:26:07 +02:00
|
|
|
-- If item_entity_ttl is not set, enity will have default life time
|
2014-05-17 11:25:25 +02:00
|
|
|
-- Setting it to -1 disables the feature
|
|
|
|
|
2014-12-12 20:49:19 +01:00
|
|
|
local time_to_live = tonumber(core.settings:get("item_entity_ttl"))
|
2014-05-17 11:25:25 +02:00
|
|
|
if not time_to_live then
|
|
|
|
time_to_live = 900
|
|
|
|
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,
|
2014-11-30 10:26:51 +01:00
|
|
|
collisionbox = {-0.3, -0.3, -0.3, 0.3, 0.3, 0.3},
|
2014-05-17 11:25:25 +02:00
|
|
|
visual = "wielditem",
|
2014-11-30 10:26:51 +01:00
|
|
|
visual_size = {x = 0.4, y = 0.4},
|
2012-04-01 15:06:01 +02:00
|
|
|
textures = {""},
|
2014-05-17 11:25:25 +02:00
|
|
|
spritediv = {x = 1, y = 1},
|
|
|
|
initial_sprite_basepos = {x = 0, y = 0},
|
2012-04-01 15:06:01 +02:00
|
|
|
is_visible = false,
|
|
|
|
},
|
2014-05-17 11:25:25 +02:00
|
|
|
|
2012-04-01 15:06:01 +02:00
|
|
|
itemstring = '',
|
|
|
|
physical_state = true,
|
2014-05-17 11:25:25 +02:00
|
|
|
age = 0,
|
2012-04-01 15:06:01 +02:00
|
|
|
|
|
|
|
set_item = function(self, itemstring)
|
|
|
|
self.itemstring = itemstring
|
|
|
|
local stack = ItemStack(itemstring)
|
2014-05-17 11:25:25 +02:00
|
|
|
local count = stack:get_count()
|
|
|
|
local max_count = stack:get_stack_max()
|
|
|
|
if count > max_count then
|
|
|
|
count = max_count
|
|
|
|
self.itemstring = stack:get_name().." "..max_count
|
|
|
|
end
|
2014-11-30 10:26:51 +01:00
|
|
|
local s = 0.2 + 0.1 * (count / max_count)
|
|
|
|
local c = s
|
2012-04-01 15:06:01 +02:00
|
|
|
local itemtable = stack:to_table()
|
|
|
|
local itemname = nil
|
|
|
|
if itemtable then
|
|
|
|
itemname = stack:to_table().name
|
|
|
|
end
|
2017-03-10 18:25:58 +01:00
|
|
|
-- Backwards compatibility: old clients use the texture
|
|
|
|
-- to get the type of the item
|
2012-04-01 15:06:01 +02:00
|
|
|
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
|
2014-06-05 18:40:34 +02:00
|
|
|
local prop = {
|
2012-04-01 15:06:01 +02:00
|
|
|
is_visible = true,
|
2014-05-17 11:25:25 +02:00
|
|
|
visual = "wielditem",
|
|
|
|
textures = {itemname},
|
|
|
|
visual_size = {x = s, y = s},
|
|
|
|
collisionbox = {-c, -c, -c, c, c, c},
|
2014-11-30 10:26:51 +01:00
|
|
|
automatic_rotate = math.pi * 0.5,
|
2017-03-10 18:25:58 +01:00
|
|
|
wield_item = itemstring,
|
2012-04-04 12:16:09 +02:00
|
|
|
}
|
|
|
|
self.object:set_properties(prop)
|
2012-04-01 15:06:01 +02:00
|
|
|
end,
|
|
|
|
|
|
|
|
get_staticdata = function(self)
|
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,
|
2015-11-05 19:56:19 +01:00
|
|
|
age = self.age,
|
|
|
|
dropped_by = self.dropped_by
|
2012-09-09 20:29:44 +02:00
|
|
|
})
|
2012-04-01 15:06:01 +02:00
|
|
|
end,
|
|
|
|
|
2014-05-17 11:25:25 +02:00
|
|
|
on_activate = function(self, staticdata, dtime_s)
|
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
|
2015-09-29 17:26:07 +02:00
|
|
|
if data.age then
|
2014-05-17 11:25:25 +02:00
|
|
|
self.age = data.age + dtime_s
|
|
|
|
else
|
|
|
|
self.age = dtime_s
|
|
|
|
end
|
2015-11-05 19:56:19 +01:00
|
|
|
self.dropped_by = data.dropped_by
|
2012-09-09 20:29:44 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
self.itemstring = staticdata
|
|
|
|
end
|
2014-05-17 11:25:25 +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})
|
2012-04-01 15:06:01 +02:00
|
|
|
self:set_item(self.itemstring)
|
|
|
|
end,
|
|
|
|
|
2017-03-10 18:25:58 +01:00
|
|
|
-- moves items from this stack to an other stack
|
2015-05-15 00:03:19 +02:00
|
|
|
try_merge_with = function(self, own_stack, object, obj)
|
2017-03-10 18:25:58 +01:00
|
|
|
-- other item's stack
|
2015-05-15 00:03:19 +02:00
|
|
|
local stack = ItemStack(obj.itemstring)
|
2017-03-10 18:25:58 +01:00
|
|
|
-- only merge if items are the same
|
|
|
|
if own_stack:get_name() == stack:get_name() and
|
|
|
|
own_stack:get_meta() == stack:get_meta() and
|
|
|
|
own_stack:get_wear() == stack:get_wear() and
|
|
|
|
stack:get_free_space() > 0 then
|
2015-05-15 00:03:19 +02:00
|
|
|
local overflow = false
|
|
|
|
local count = stack:get_count() + own_stack:get_count()
|
|
|
|
local max_count = stack:get_stack_max()
|
|
|
|
if count > max_count then
|
|
|
|
overflow = true
|
2017-03-10 18:25:58 +01:00
|
|
|
stack:set_count(max_count)
|
2015-05-15 00:03:19 +02:00
|
|
|
count = count - max_count
|
2017-03-10 18:25:58 +01:00
|
|
|
own_stack:set_count(count)
|
2015-05-15 00:03:19 +02:00
|
|
|
else
|
|
|
|
self.itemstring = ''
|
2017-03-10 18:25:58 +01:00
|
|
|
stack:set_count(count)
|
2015-05-15 00:03:19 +02:00
|
|
|
end
|
|
|
|
local pos = object:getpos()
|
|
|
|
pos.y = pos.y + (count - stack:get_count()) / max_count * 0.15
|
|
|
|
object:moveto(pos, false)
|
|
|
|
local s, c
|
|
|
|
if not overflow then
|
2017-03-10 18:25:58 +01:00
|
|
|
obj.itemstring = stack:to_string()
|
2015-05-15 00:03:19 +02:00
|
|
|
s = 0.2 + 0.1 * (count / max_count)
|
|
|
|
c = s
|
|
|
|
object:set_properties({
|
|
|
|
visual_size = {x = s, y = s},
|
2017-03-10 18:25:58 +01:00
|
|
|
collisionbox = {-c, -c, -c, c, c, c},
|
|
|
|
wield_item = obj.itemstring
|
2015-05-15 00:03:19 +02:00
|
|
|
})
|
|
|
|
self.object:remove()
|
|
|
|
-- merging succeeded
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
s = 0.4
|
|
|
|
c = 0.3
|
2017-03-10 18:25:58 +01:00
|
|
|
obj.itemstring = stack:to_string()
|
2015-05-15 00:03:19 +02:00
|
|
|
object:set_properties({
|
|
|
|
visual_size = {x = s, y = s},
|
2017-03-10 18:25:58 +01:00
|
|
|
collisionbox = {-c, -c, -c, c, c, c},
|
|
|
|
wield_item = obj.itemstring
|
2015-05-15 00:03:19 +02:00
|
|
|
})
|
|
|
|
s = 0.2 + 0.1 * (count / max_count)
|
|
|
|
c = s
|
2017-03-10 18:25:58 +01:00
|
|
|
self.itemstring = own_stack:to_string()
|
2015-05-15 00:03:19 +02:00
|
|
|
self.object:set_properties({
|
|
|
|
visual_size = {x = s, y = s},
|
2017-03-10 18:25:58 +01:00
|
|
|
collisionbox = {-c, -c, -c, c, c, c},
|
|
|
|
wield_item = self.itemstring
|
2015-05-15 00:03:19 +02:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- merging didn't succeed
|
|
|
|
return false
|
|
|
|
end,
|
|
|
|
|
2012-04-01 15:06:01 +02:00
|
|
|
on_step = function(self, dtime)
|
2014-05-17 11:25:25 +02:00
|
|
|
self.age = self.age + dtime
|
|
|
|
if time_to_live > 0 and self.age > time_to_live then
|
|
|
|
self.itemstring = ''
|
|
|
|
self.object:remove()
|
|
|
|
return
|
|
|
|
end
|
2012-04-01 15:06:01 +02:00
|
|
|
local p = self.object:getpos()
|
2014-11-30 10:26:51 +01:00
|
|
|
p.y = p.y - 0.5
|
2015-05-14 12:35:24 +02:00
|
|
|
local node = core.get_node_or_nil(p)
|
|
|
|
local in_unloaded = (node == nil)
|
|
|
|
if in_unloaded then
|
|
|
|
-- Don't infinetly fall into unloaded map
|
|
|
|
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})
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local nn = node.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
|
2014-05-17 11:25:25 +02:00
|
|
|
local own_stack = ItemStack(self.object:get_luaentity().itemstring)
|
2015-05-15 00:03:19 +02:00
|
|
|
-- Merge with close entities of the same item
|
|
|
|
for _, object in ipairs(core.get_objects_inside_radius(p, 0.8)) do
|
2014-05-17 11:25:25 +02:00
|
|
|
local obj = object:get_luaentity()
|
2015-05-15 00:03:19 +02:00
|
|
|
if obj and obj.name == "__builtin:item"
|
|
|
|
and obj.physical_state == false then
|
|
|
|
if self:try_merge_with(own_stack, object, obj) then
|
|
|
|
return
|
2014-05-17 11:25:25 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.object:setvelocity({x = 0, y = 0, z = 0})
|
|
|
|
self.object:setacceleration({x = 0, y = 0, z = 0})
|
2012-04-01 15:06:01 +02:00
|
|
|
self.physical_state = false
|
2014-05-17 11:25:25 +02:00
|
|
|
self.object:set_properties({physical = false})
|
2012-04-01 15:06:01 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
if not self.physical_state then
|
2014-05-17 11:25:25 +02:00
|
|
|
self.object:setvelocity({x = 0, y = 0, z = 0})
|
|
|
|
self.object:setacceleration({x = 0, y = -10, z = 0})
|
2012-04-01 15:06:01 +02:00
|
|
|
self.physical_state = true
|
2014-05-17 11:25:25 +02:00
|
|
|
self.object:set_properties({physical = true})
|
2012-04-01 15:06:01 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
on_punch = function(self, hitter)
|
2015-10-25 18:15:31 +01:00
|
|
|
local inv = hitter:get_inventory()
|
|
|
|
if inv and self.itemstring ~= '' then
|
|
|
|
local left = inv:add_item("main", self.itemstring)
|
|
|
|
if left and 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,
|
|
|
|
})
|