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
|
|
|
|
|
2017-08-24 23:06:36 +02:00
|
|
|
local time_to_live = tonumber(core.settings:get("item_entity_ttl")) or 900
|
|
|
|
local gravity = tonumber(core.settings:get("movement_gravity")) or 9.81
|
|
|
|
|
2014-05-17 11:25:25 +02:00
|
|
|
|
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 = {""},
|
|
|
|
is_visible = false,
|
|
|
|
},
|
2014-05-17 11:25:25 +02:00
|
|
|
|
2017-08-24 23:06:36 +02:00
|
|
|
itemstring = "",
|
2017-09-18 20:18:55 +02:00
|
|
|
moving_state = true,
|
2019-09-04 22:21:40 +02:00
|
|
|
physical_state = true,
|
|
|
|
-- Item expiry
|
2014-05-17 11:25:25 +02:00
|
|
|
age = 0,
|
2019-09-04 22:21:40 +02:00
|
|
|
-- Pushing item out of solid nodes
|
|
|
|
force_out = nil,
|
|
|
|
force_out_start = nil,
|
2012-04-01 15:06:01 +02:00
|
|
|
|
2017-09-11 08:20:06 +02:00
|
|
|
set_item = function(self, item)
|
|
|
|
local stack = ItemStack(item or self.itemstring)
|
2017-08-24 23:06:36 +02:00
|
|
|
self.itemstring = stack:to_string()
|
2017-09-11 08:20:06 +02:00
|
|
|
if self.itemstring == "" then
|
|
|
|
-- item not yet known
|
|
|
|
return
|
|
|
|
end
|
2017-08-24 23:06:36 +02:00
|
|
|
|
2017-03-10 18:25:58 +01:00
|
|
|
-- Backwards compatibility: old clients use the texture
|
|
|
|
-- to get the type of the item
|
2017-09-11 08:20:06 +02:00
|
|
|
local itemname = stack:is_known() and stack:get_name() or "unknown"
|
2017-08-24 23:06:36 +02:00
|
|
|
|
|
|
|
local max_count = stack:get_stack_max()
|
|
|
|
local count = math.min(stack:get_count(), max_count)
|
2017-09-11 08:20:06 +02:00
|
|
|
local size = 0.2 + 0.1 * (count / max_count) ^ (1 / 3)
|
2020-08-30 01:02:21 +02:00
|
|
|
local def = core.registered_items[itemname]
|
2020-08-30 15:34:28 +02:00
|
|
|
local glow = def and def.light_source and
|
|
|
|
math.floor(def.light_source / 2 + 0.5)
|
2017-08-24 23:06:36 +02:00
|
|
|
|
2022-04-04 15:19:04 +02:00
|
|
|
local size_bias = 1e-3 * math.random() -- small random bias to counter Z-fighting
|
2022-05-05 20:49:57 +02:00
|
|
|
local c = {-size, -size, -size, size, size, size}
|
2017-08-24 23:06:36 +02:00
|
|
|
self.object:set_properties({
|
2012-04-01 15:06:01 +02:00
|
|
|
is_visible = true,
|
2014-05-17 11:25:25 +02:00
|
|
|
visual = "wielditem",
|
|
|
|
textures = {itemname},
|
2022-04-04 15:19:04 +02:00
|
|
|
visual_size = {x = size + size_bias, y = size + size_bias},
|
2022-05-05 20:49:57 +02:00
|
|
|
collisionbox = c,
|
2017-09-11 08:20:06 +02:00
|
|
|
automatic_rotate = math.pi * 0.5 * 0.2 / size,
|
2017-08-24 23:06:36 +02:00
|
|
|
wield_item = self.itemstring,
|
2019-12-19 19:41:44 +01:00
|
|
|
glow = glow,
|
2023-08-24 07:00:18 +02:00
|
|
|
infotext = stack:get_description(),
|
2017-08-24 23:06:36 +02:00
|
|
|
})
|
2017-09-11 08:20:06 +02:00
|
|
|
|
2022-05-05 20:49:57 +02:00
|
|
|
-- cache for usage in on_step
|
|
|
|
self._collisionbox = c
|
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,
|
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
|
2017-08-24 23:06:36 +02:00
|
|
|
self.age = (data.age or 0) + dtime_s
|
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})
|
2017-08-11 21:16:09 +02:00
|
|
|
self.object:set_velocity({x = 0, y = 2, z = 0})
|
2017-08-24 23:06:36 +02:00
|
|
|
self.object:set_acceleration({x = 0, y = -gravity, z = 0})
|
2022-05-05 20:49:57 +02:00
|
|
|
self._collisionbox = self.initial_properties.collisionbox
|
2017-08-24 23:06:36 +02:00
|
|
|
self:set_item()
|
2012-04-01 15:06:01 +02:00
|
|
|
end,
|
|
|
|
|
2017-08-24 23:06:36 +02:00
|
|
|
try_merge_with = function(self, own_stack, object, entity)
|
|
|
|
if self.age == entity.age then
|
2022-11-09 17:57:19 +01:00
|
|
|
-- Cannot merge with itself
|
2017-08-24 23:06:36 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local stack = ItemStack(entity.itemstring)
|
|
|
|
local name = stack:get_name()
|
|
|
|
if own_stack:get_name() ~= name or
|
|
|
|
own_stack:get_meta() ~= stack:get_meta() or
|
|
|
|
own_stack:get_wear() ~= stack:get_wear() or
|
|
|
|
own_stack:get_free_space() == 0 then
|
2022-11-09 17:57:19 +01:00
|
|
|
-- Cannot merge different or full stack
|
2017-08-24 23:06:36 +02:00
|
|
|
return false
|
2015-05-15 00:03:19 +02:00
|
|
|
end
|
2017-08-24 23:06:36 +02:00
|
|
|
|
|
|
|
local count = own_stack:get_count()
|
|
|
|
local total_count = stack:get_count() + count
|
|
|
|
local max_count = stack:get_stack_max()
|
|
|
|
|
|
|
|
if total_count > max_count then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
-- Merge the remote stack into this one
|
|
|
|
|
|
|
|
local pos = object:get_pos()
|
|
|
|
pos.y = pos.y + ((total_count - count) / max_count) * 0.15
|
|
|
|
self.object:move_to(pos)
|
|
|
|
|
|
|
|
self.age = 0 -- Handle as new entity
|
|
|
|
own_stack:set_count(total_count)
|
|
|
|
self:set_item(own_stack)
|
|
|
|
|
|
|
|
entity.itemstring = ""
|
|
|
|
object:remove()
|
|
|
|
return true
|
2015-05-15 00:03:19 +02:00
|
|
|
end,
|
|
|
|
|
2019-09-04 22:21:40 +02:00
|
|
|
enable_physics = function(self)
|
|
|
|
if not self.physical_state then
|
|
|
|
self.physical_state = true
|
|
|
|
self.object:set_properties({physical = true})
|
|
|
|
self.object:set_velocity({x=0, y=0, z=0})
|
|
|
|
self.object:set_acceleration({x=0, y=-gravity, z=0})
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
disable_physics = function(self)
|
|
|
|
if self.physical_state then
|
|
|
|
self.physical_state = false
|
|
|
|
self.object:set_properties({physical = false})
|
|
|
|
self.object:set_velocity({x=0, y=0, z=0})
|
|
|
|
self.object:set_acceleration({x=0, y=0, z=0})
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
2020-04-14 16:55:16 +02:00
|
|
|
on_step = function(self, dtime, moveresult)
|
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
|
2017-08-24 23:06:36 +02:00
|
|
|
self.itemstring = ""
|
2014-05-17 11:25:25 +02:00
|
|
|
self.object:remove()
|
|
|
|
return
|
|
|
|
end
|
2017-08-24 23:06:36 +02:00
|
|
|
|
|
|
|
local pos = self.object:get_pos()
|
|
|
|
local node = core.get_node_or_nil({
|
|
|
|
x = pos.x,
|
2022-05-05 20:49:57 +02:00
|
|
|
y = pos.y + self._collisionbox[2] - 0.05,
|
2017-08-24 23:06:36 +02:00
|
|
|
z = pos.z
|
|
|
|
})
|
2018-02-10 05:55:40 +01:00
|
|
|
-- Delete in 'ignore' nodes
|
|
|
|
if node and node.name == "ignore" then
|
|
|
|
self.itemstring = ""
|
|
|
|
self.object:remove()
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-06-05 12:00:11 +02:00
|
|
|
-- Prevent assert when item_entity is attached
|
|
|
|
if moveresult == nil and self.object:get_attach() then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-04-14 16:55:16 +02:00
|
|
|
if self.force_out then
|
|
|
|
-- This code runs after the entity got a push from the is_stuck code.
|
|
|
|
-- It makes sure the entity is entirely outside the solid node
|
2022-05-05 20:49:57 +02:00
|
|
|
local c = self._collisionbox
|
2020-04-14 16:55:16 +02:00
|
|
|
local s = self.force_out_start
|
|
|
|
local f = self.force_out
|
|
|
|
local ok = (f.x > 0 and pos.x + c[1] > s.x + 0.5) or
|
|
|
|
(f.y > 0 and pos.y + c[2] > s.y + 0.5) or
|
|
|
|
(f.z > 0 and pos.z + c[3] > s.z + 0.5) or
|
|
|
|
(f.x < 0 and pos.x + c[4] < s.x - 0.5) or
|
|
|
|
(f.z < 0 and pos.z + c[6] < s.z - 0.5)
|
|
|
|
if ok then
|
|
|
|
-- Item was successfully forced out
|
|
|
|
self.force_out = nil
|
|
|
|
self:enable_physics()
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if not self.physical_state then
|
|
|
|
return -- Don't do anything
|
|
|
|
end
|
|
|
|
|
2020-05-14 17:54:17 +02:00
|
|
|
assert(moveresult,
|
|
|
|
"Collision info missing, this is caused by an out-of-date/buggy mod or game")
|
|
|
|
|
2020-04-14 16:55:16 +02:00
|
|
|
if not moveresult.collides then
|
|
|
|
-- future TODO: items should probably decelerate in air
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Push item out when stuck inside solid node
|
2019-09-04 22:21:40 +02:00
|
|
|
local is_stuck = false
|
|
|
|
local snode = core.get_node_or_nil(pos)
|
|
|
|
if snode then
|
|
|
|
local sdef = core.registered_nodes[snode.name] or {}
|
|
|
|
is_stuck = (sdef.walkable == nil or sdef.walkable == true)
|
|
|
|
and (sdef.collision_box == nil or sdef.collision_box.type == "regular")
|
|
|
|
and (sdef.node_box == nil or sdef.node_box.type == "regular")
|
|
|
|
end
|
|
|
|
|
|
|
|
if is_stuck then
|
|
|
|
local shootdir
|
|
|
|
local order = {
|
|
|
|
{x=1, y=0, z=0}, {x=-1, y=0, z= 0},
|
|
|
|
{x=0, y=0, z=1}, {x= 0, y=0, z=-1},
|
|
|
|
}
|
|
|
|
|
|
|
|
-- Check which one of the 4 sides is free
|
|
|
|
for o = 1, #order do
|
|
|
|
local cnode = core.get_node(vector.add(pos, order[o])).name
|
|
|
|
local cdef = core.registered_nodes[cnode] or {}
|
|
|
|
if cnode ~= "ignore" and cdef.walkable == false then
|
|
|
|
shootdir = order[o]
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- If none of the 4 sides is free, check upwards
|
|
|
|
if not shootdir then
|
|
|
|
shootdir = {x=0, y=1, z=0}
|
|
|
|
local cnode = core.get_node(vector.add(pos, shootdir)).name
|
|
|
|
if cnode == "ignore" then
|
|
|
|
shootdir = nil -- Do not push into ignore
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if shootdir then
|
|
|
|
-- Set new item moving speed accordingly
|
|
|
|
local newv = vector.multiply(shootdir, 3)
|
|
|
|
self:disable_physics()
|
|
|
|
self.object:set_velocity(newv)
|
|
|
|
|
|
|
|
self.force_out = newv
|
|
|
|
self.force_out_start = vector.round(pos)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-14 16:55:16 +02:00
|
|
|
node = nil -- ground node we're colliding with
|
|
|
|
if moveresult.touching_ground then
|
|
|
|
for _, info in ipairs(moveresult.collisions) do
|
|
|
|
if info.axis == "y" then
|
|
|
|
node = core.get_node(info.node_pos)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2019-09-04 22:21:40 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Slide on slippery nodes
|
2017-08-24 23:06:36 +02:00
|
|
|
local def = node and core.registered_nodes[node.name]
|
2020-04-14 16:55:16 +02:00
|
|
|
local keep_movement = false
|
2017-08-24 23:06:36 +02:00
|
|
|
|
2020-04-14 16:55:16 +02:00
|
|
|
if def then
|
2017-08-24 23:06:36 +02:00
|
|
|
local slippery = core.get_item_group(node.name, "slippery")
|
2020-04-14 16:55:16 +02:00
|
|
|
local vel = self.object:get_velocity()
|
|
|
|
if slippery ~= 0 and (math.abs(vel.x) > 0.1 or math.abs(vel.z) > 0.1) then
|
2017-08-24 23:06:36 +02:00
|
|
|
-- Horizontal deceleration
|
2020-04-14 16:55:16 +02:00
|
|
|
local factor = math.min(4 / (slippery + 4) * dtime, 1)
|
|
|
|
self.object:set_velocity({
|
|
|
|
x = vel.x * (1 - factor),
|
2017-08-24 23:06:36 +02:00
|
|
|
y = 0,
|
2020-04-14 16:55:16 +02:00
|
|
|
z = vel.z * (1 - factor)
|
2017-08-24 23:06:36 +02:00
|
|
|
})
|
2020-04-14 16:55:16 +02:00
|
|
|
keep_movement = true
|
2017-08-24 23:06:36 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-14 16:55:16 +02:00
|
|
|
if not keep_movement then
|
|
|
|
self.object:set_velocity({x=0, y=0, z=0})
|
2017-08-24 23:06:36 +02:00
|
|
|
end
|
|
|
|
|
2020-04-14 16:55:16 +02:00
|
|
|
if self.moving_state == keep_movement then
|
|
|
|
-- Do not update anything until the moving state changes
|
|
|
|
return
|
2017-08-24 23:06:36 +02:00
|
|
|
end
|
2020-04-14 16:55:16 +02:00
|
|
|
self.moving_state = keep_movement
|
2017-08-24 23:06:36 +02:00
|
|
|
|
2020-04-14 16:55:16 +02:00
|
|
|
-- Only collect items if not moving
|
|
|
|
if self.moving_state then
|
2017-09-18 20:18:55 +02:00
|
|
|
return
|
|
|
|
end
|
2017-08-24 23:06:36 +02:00
|
|
|
-- Collect the items around to merge with
|
|
|
|
local own_stack = ItemStack(self.itemstring)
|
|
|
|
if own_stack:get_free_space() == 0 then
|
2015-05-14 12:35:24 +02:00
|
|
|
return
|
|
|
|
end
|
2017-08-24 23:06:36 +02:00
|
|
|
local objects = core.get_objects_inside_radius(pos, 1.0)
|
|
|
|
for k, obj in pairs(objects) do
|
|
|
|
local entity = obj:get_luaentity()
|
|
|
|
if entity and entity.name == "__builtin:item" then
|
|
|
|
if self:try_merge_with(own_stack, obj, entity) then
|
|
|
|
own_stack = ItemStack(self.itemstring)
|
|
|
|
if own_stack:get_free_space() == 0 then
|
|
|
|
return
|
2014-05-17 11:25:25 +02:00
|
|
|
end
|
|
|
|
end
|
2012-04-01 15:06:01 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
2022-10-01 21:21:06 +02:00
|
|
|
on_punch = function(self, hitter, ...)
|
|
|
|
if self.itemstring == "" then
|
|
|
|
self.object:remove()
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Call on_pickup callback in item definition.
|
|
|
|
local itemstack = ItemStack(self.itemstring)
|
|
|
|
local callback = itemstack:get_definition().on_pickup
|
|
|
|
|
|
|
|
local ret = callback(itemstack, hitter, {type = "object", ref = self.object}, ...)
|
|
|
|
if not ret then
|
|
|
|
-- Don't modify (and don't reset rotation)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
itemstack = ItemStack(ret)
|
|
|
|
|
|
|
|
-- Handle the leftover itemstack
|
|
|
|
if itemstack:is_empty() then
|
|
|
|
self.itemstring = ""
|
|
|
|
self.object:remove()
|
|
|
|
else
|
|
|
|
self:set_item(itemstack)
|
2012-04-01 15:06:01 +02:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|