item_entity.lua 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. -- Minetest: builtin/item_entity.lua
  2. function core.spawn_item(pos, item)
  3. -- Take item in any format
  4. local stack = ItemStack(item)
  5. local obj = core.add_entity(pos, "__builtin:item")
  6. -- Don't use obj if it couldn't be added to the map.
  7. if obj then
  8. obj:get_luaentity():set_item(stack:to_string())
  9. end
  10. return obj
  11. end
  12. -- If item_entity_ttl is not set, enity will have default life time
  13. -- Setting it to -1 disables the feature
  14. local time_to_live = tonumber(core.settings:get("item_entity_ttl")) or 900
  15. local gravity = tonumber(core.settings:get("movement_gravity")) or 9.81
  16. core.register_entity(":__builtin:item", {
  17. initial_properties = {
  18. hp_max = 1,
  19. physical = true,
  20. collide_with_objects = false,
  21. collisionbox = {-0.3, -0.3, -0.3, 0.3, 0.3, 0.3},
  22. visual = "wielditem",
  23. visual_size = {x = 0.4, y = 0.4},
  24. textures = {""},
  25. is_visible = false,
  26. },
  27. itemstring = "",
  28. moving_state = true,
  29. physical_state = true,
  30. -- Item expiry
  31. age = 0,
  32. -- Pushing item out of solid nodes
  33. force_out = nil,
  34. force_out_start = nil,
  35. set_item = function(self, item)
  36. local stack = ItemStack(item or self.itemstring)
  37. self.itemstring = stack:to_string()
  38. if self.itemstring == "" then
  39. -- item not yet known
  40. return
  41. end
  42. -- Backwards compatibility: old clients use the texture
  43. -- to get the type of the item
  44. local itemname = stack:is_known() and stack:get_name() or "unknown"
  45. local max_count = stack:get_stack_max()
  46. local count = math.min(stack:get_count(), max_count)
  47. local size = 0.2 + 0.1 * (count / max_count) ^ (1 / 3)
  48. local def = core.registered_items[itemname]
  49. local glow = def and def.light_source and
  50. math.floor(def.light_source / 2 + 0.5)
  51. local size_bias = 1e-3 * math.random() -- small random bias to counter Z-fighting
  52. local c = {-size, -size, -size, size, size, size}
  53. self.object:set_properties({
  54. is_visible = true,
  55. visual = "wielditem",
  56. textures = {itemname},
  57. visual_size = {x = size + size_bias, y = size + size_bias},
  58. collisionbox = c,
  59. automatic_rotate = math.pi * 0.5 * 0.2 / size,
  60. wield_item = self.itemstring,
  61. glow = glow,
  62. infotext = stack:get_description(),
  63. })
  64. -- cache for usage in on_step
  65. self._collisionbox = c
  66. end,
  67. get_staticdata = function(self)
  68. return core.serialize({
  69. itemstring = self.itemstring,
  70. age = self.age,
  71. dropped_by = self.dropped_by
  72. })
  73. end,
  74. on_activate = function(self, staticdata, dtime_s)
  75. if string.sub(staticdata, 1, string.len("return")) == "return" then
  76. local data = core.deserialize(staticdata)
  77. if data and type(data) == "table" then
  78. self.itemstring = data.itemstring
  79. self.age = (data.age or 0) + dtime_s
  80. self.dropped_by = data.dropped_by
  81. end
  82. else
  83. self.itemstring = staticdata
  84. end
  85. self.object:set_armor_groups({immortal = 1})
  86. self.object:set_velocity({x = 0, y = 2, z = 0})
  87. self.object:set_acceleration({x = 0, y = -gravity, z = 0})
  88. self._collisionbox = self.initial_properties.collisionbox
  89. self:set_item()
  90. end,
  91. try_merge_with = function(self, own_stack, object, entity)
  92. if self.age == entity.age then
  93. -- Cannot merge with itself
  94. return false
  95. end
  96. local stack = ItemStack(entity.itemstring)
  97. local name = stack:get_name()
  98. if own_stack:get_name() ~= name or
  99. own_stack:get_meta() ~= stack:get_meta() or
  100. own_stack:get_wear() ~= stack:get_wear() or
  101. own_stack:get_free_space() == 0 then
  102. -- Cannot merge different or full stack
  103. return false
  104. end
  105. local count = own_stack:get_count()
  106. local total_count = stack:get_count() + count
  107. local max_count = stack:get_stack_max()
  108. if total_count > max_count then
  109. return false
  110. end
  111. -- Merge the remote stack into this one
  112. local pos = object:get_pos()
  113. pos.y = pos.y + ((total_count - count) / max_count) * 0.15
  114. self.object:move_to(pos)
  115. self.age = 0 -- Handle as new entity
  116. own_stack:set_count(total_count)
  117. self:set_item(own_stack)
  118. entity.itemstring = ""
  119. object:remove()
  120. return true
  121. end,
  122. enable_physics = function(self)
  123. if not self.physical_state then
  124. self.physical_state = true
  125. self.object:set_properties({physical = true})
  126. self.object:set_velocity({x=0, y=0, z=0})
  127. self.object:set_acceleration({x=0, y=-gravity, z=0})
  128. end
  129. end,
  130. disable_physics = function(self)
  131. if self.physical_state then
  132. self.physical_state = false
  133. self.object:set_properties({physical = false})
  134. self.object:set_velocity({x=0, y=0, z=0})
  135. self.object:set_acceleration({x=0, y=0, z=0})
  136. end
  137. end,
  138. on_step = function(self, dtime, moveresult)
  139. self.age = self.age + dtime
  140. if time_to_live > 0 and self.age > time_to_live then
  141. self.itemstring = ""
  142. self.object:remove()
  143. return
  144. end
  145. local pos = self.object:get_pos()
  146. local node = core.get_node_or_nil({
  147. x = pos.x,
  148. y = pos.y + self._collisionbox[2] - 0.05,
  149. z = pos.z
  150. })
  151. -- Delete in 'ignore' nodes
  152. if node and node.name == "ignore" then
  153. self.itemstring = ""
  154. self.object:remove()
  155. return
  156. end
  157. -- Prevent assert when item_entity is attached
  158. if moveresult == nil and self.object:get_attach() then
  159. return
  160. end
  161. if self.force_out then
  162. -- This code runs after the entity got a push from the is_stuck code.
  163. -- It makes sure the entity is entirely outside the solid node
  164. local c = self._collisionbox
  165. local s = self.force_out_start
  166. local f = self.force_out
  167. local ok = (f.x > 0 and pos.x + c[1] > s.x + 0.5) or
  168. (f.y > 0 and pos.y + c[2] > s.y + 0.5) or
  169. (f.z > 0 and pos.z + c[3] > s.z + 0.5) or
  170. (f.x < 0 and pos.x + c[4] < s.x - 0.5) or
  171. (f.z < 0 and pos.z + c[6] < s.z - 0.5)
  172. if ok then
  173. -- Item was successfully forced out
  174. self.force_out = nil
  175. self:enable_physics()
  176. return
  177. end
  178. end
  179. if not self.physical_state then
  180. return -- Don't do anything
  181. end
  182. assert(moveresult,
  183. "Collision info missing, this is caused by an out-of-date/buggy mod or game")
  184. if not moveresult.collides then
  185. -- future TODO: items should probably decelerate in air
  186. return
  187. end
  188. -- Push item out when stuck inside solid node
  189. local is_stuck = false
  190. local snode = core.get_node_or_nil(pos)
  191. if snode then
  192. local sdef = core.registered_nodes[snode.name] or {}
  193. is_stuck = (sdef.walkable == nil or sdef.walkable == true)
  194. and (sdef.collision_box == nil or sdef.collision_box.type == "regular")
  195. and (sdef.node_box == nil or sdef.node_box.type == "regular")
  196. end
  197. if is_stuck then
  198. local shootdir
  199. local order = {
  200. {x=1, y=0, z=0}, {x=-1, y=0, z= 0},
  201. {x=0, y=0, z=1}, {x= 0, y=0, z=-1},
  202. }
  203. -- Check which one of the 4 sides is free
  204. for o = 1, #order do
  205. local cnode = core.get_node(vector.add(pos, order[o])).name
  206. local cdef = core.registered_nodes[cnode] or {}
  207. if cnode ~= "ignore" and cdef.walkable == false then
  208. shootdir = order[o]
  209. break
  210. end
  211. end
  212. -- If none of the 4 sides is free, check upwards
  213. if not shootdir then
  214. shootdir = {x=0, y=1, z=0}
  215. local cnode = core.get_node(vector.add(pos, shootdir)).name
  216. if cnode == "ignore" then
  217. shootdir = nil -- Do not push into ignore
  218. end
  219. end
  220. if shootdir then
  221. -- Set new item moving speed accordingly
  222. local newv = vector.multiply(shootdir, 3)
  223. self:disable_physics()
  224. self.object:set_velocity(newv)
  225. self.force_out = newv
  226. self.force_out_start = vector.round(pos)
  227. return
  228. end
  229. end
  230. node = nil -- ground node we're colliding with
  231. if moveresult.touching_ground then
  232. for _, info in ipairs(moveresult.collisions) do
  233. if info.axis == "y" then
  234. node = core.get_node(info.node_pos)
  235. break
  236. end
  237. end
  238. end
  239. -- Slide on slippery nodes
  240. local def = node and core.registered_nodes[node.name]
  241. local keep_movement = false
  242. if def then
  243. local slippery = core.get_item_group(node.name, "slippery")
  244. local vel = self.object:get_velocity()
  245. if slippery ~= 0 and (math.abs(vel.x) > 0.1 or math.abs(vel.z) > 0.1) then
  246. -- Horizontal deceleration
  247. local factor = math.min(4 / (slippery + 4) * dtime, 1)
  248. self.object:set_velocity({
  249. x = vel.x * (1 - factor),
  250. y = 0,
  251. z = vel.z * (1 - factor)
  252. })
  253. keep_movement = true
  254. end
  255. end
  256. if not keep_movement then
  257. self.object:set_velocity({x=0, y=0, z=0})
  258. end
  259. if self.moving_state == keep_movement then
  260. -- Do not update anything until the moving state changes
  261. return
  262. end
  263. self.moving_state = keep_movement
  264. -- Only collect items if not moving
  265. if self.moving_state then
  266. return
  267. end
  268. -- Collect the items around to merge with
  269. local own_stack = ItemStack(self.itemstring)
  270. if own_stack:get_free_space() == 0 then
  271. return
  272. end
  273. local objects = core.get_objects_inside_radius(pos, 1.0)
  274. for k, obj in pairs(objects) do
  275. local entity = obj:get_luaentity()
  276. if entity and entity.name == "__builtin:item" then
  277. if self:try_merge_with(own_stack, obj, entity) then
  278. own_stack = ItemStack(self.itemstring)
  279. if own_stack:get_free_space() == 0 then
  280. return
  281. end
  282. end
  283. end
  284. end
  285. end,
  286. on_punch = function(self, hitter, ...)
  287. if self.itemstring == "" then
  288. self.object:remove()
  289. return
  290. end
  291. -- Call on_pickup callback in item definition.
  292. local itemstack = ItemStack(self.itemstring)
  293. local callback = itemstack:get_definition().on_pickup
  294. local ret = callback(itemstack, hitter, {type = "object", ref = self.object}, ...)
  295. if not ret then
  296. -- Don't modify (and don't reset rotation)
  297. return
  298. end
  299. itemstack = ItemStack(ret)
  300. -- Handle the leftover itemstack
  301. if itemstack:is_empty() then
  302. self.itemstring = ""
  303. self.object:remove()
  304. else
  305. self:set_item(itemstack)
  306. end
  307. end,
  308. })