item_entity.lua 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. })
  63. -- cache for usage in on_step
  64. self._collisionbox = c
  65. end,
  66. get_staticdata = function(self)
  67. return core.serialize({
  68. itemstring = self.itemstring,
  69. age = self.age,
  70. dropped_by = self.dropped_by
  71. })
  72. end,
  73. on_activate = function(self, staticdata, dtime_s)
  74. if string.sub(staticdata, 1, string.len("return")) == "return" then
  75. local data = core.deserialize(staticdata)
  76. if data and type(data) == "table" then
  77. self.itemstring = data.itemstring
  78. self.age = (data.age or 0) + dtime_s
  79. self.dropped_by = data.dropped_by
  80. end
  81. else
  82. self.itemstring = staticdata
  83. end
  84. self.object:set_armor_groups({immortal = 1})
  85. self.object:set_velocity({x = 0, y = 2, z = 0})
  86. self.object:set_acceleration({x = 0, y = -gravity, z = 0})
  87. self._collisionbox = self.initial_properties.collisionbox
  88. self:set_item()
  89. end,
  90. try_merge_with = function(self, own_stack, object, entity)
  91. if self.age == entity.age then
  92. -- Can not merge with itself
  93. return false
  94. end
  95. local stack = ItemStack(entity.itemstring)
  96. local name = stack:get_name()
  97. if own_stack:get_name() ~= name or
  98. own_stack:get_meta() ~= stack:get_meta() or
  99. own_stack:get_wear() ~= stack:get_wear() or
  100. own_stack:get_free_space() == 0 then
  101. -- Can not merge different or full stack
  102. return false
  103. end
  104. local count = own_stack:get_count()
  105. local total_count = stack:get_count() + count
  106. local max_count = stack:get_stack_max()
  107. if total_count > max_count then
  108. return false
  109. end
  110. -- Merge the remote stack into this one
  111. local pos = object:get_pos()
  112. pos.y = pos.y + ((total_count - count) / max_count) * 0.15
  113. self.object:move_to(pos)
  114. self.age = 0 -- Handle as new entity
  115. own_stack:set_count(total_count)
  116. self:set_item(own_stack)
  117. entity.itemstring = ""
  118. object:remove()
  119. return true
  120. end,
  121. enable_physics = function(self)
  122. if not self.physical_state then
  123. self.physical_state = true
  124. self.object:set_properties({physical = true})
  125. self.object:set_velocity({x=0, y=0, z=0})
  126. self.object:set_acceleration({x=0, y=-gravity, z=0})
  127. end
  128. end,
  129. disable_physics = function(self)
  130. if self.physical_state then
  131. self.physical_state = false
  132. self.object:set_properties({physical = false})
  133. self.object:set_velocity({x=0, y=0, z=0})
  134. self.object:set_acceleration({x=0, y=0, z=0})
  135. end
  136. end,
  137. on_step = function(self, dtime, moveresult)
  138. self.age = self.age + dtime
  139. if time_to_live > 0 and self.age > time_to_live then
  140. self.itemstring = ""
  141. self.object:remove()
  142. return
  143. end
  144. local pos = self.object:get_pos()
  145. local node = core.get_node_or_nil({
  146. x = pos.x,
  147. y = pos.y + self._collisionbox[2] - 0.05,
  148. z = pos.z
  149. })
  150. -- Delete in 'ignore' nodes
  151. if node and node.name == "ignore" then
  152. self.itemstring = ""
  153. self.object:remove()
  154. return
  155. end
  156. if self.force_out then
  157. -- This code runs after the entity got a push from the is_stuck code.
  158. -- It makes sure the entity is entirely outside the solid node
  159. local c = self._collisionbox
  160. local s = self.force_out_start
  161. local f = self.force_out
  162. local ok = (f.x > 0 and pos.x + c[1] > s.x + 0.5) or
  163. (f.y > 0 and pos.y + c[2] > s.y + 0.5) or
  164. (f.z > 0 and pos.z + c[3] > s.z + 0.5) or
  165. (f.x < 0 and pos.x + c[4] < s.x - 0.5) or
  166. (f.z < 0 and pos.z + c[6] < s.z - 0.5)
  167. if ok then
  168. -- Item was successfully forced out
  169. self.force_out = nil
  170. self:enable_physics()
  171. return
  172. end
  173. end
  174. if not self.physical_state then
  175. return -- Don't do anything
  176. end
  177. assert(moveresult,
  178. "Collision info missing, this is caused by an out-of-date/buggy mod or game")
  179. if not moveresult.collides then
  180. -- future TODO: items should probably decelerate in air
  181. return
  182. end
  183. -- Push item out when stuck inside solid node
  184. local is_stuck = false
  185. local snode = core.get_node_or_nil(pos)
  186. if snode then
  187. local sdef = core.registered_nodes[snode.name] or {}
  188. is_stuck = (sdef.walkable == nil or sdef.walkable == true)
  189. and (sdef.collision_box == nil or sdef.collision_box.type == "regular")
  190. and (sdef.node_box == nil or sdef.node_box.type == "regular")
  191. end
  192. if is_stuck then
  193. local shootdir
  194. local order = {
  195. {x=1, y=0, z=0}, {x=-1, y=0, z= 0},
  196. {x=0, y=0, z=1}, {x= 0, y=0, z=-1},
  197. }
  198. -- Check which one of the 4 sides is free
  199. for o = 1, #order do
  200. local cnode = core.get_node(vector.add(pos, order[o])).name
  201. local cdef = core.registered_nodes[cnode] or {}
  202. if cnode ~= "ignore" and cdef.walkable == false then
  203. shootdir = order[o]
  204. break
  205. end
  206. end
  207. -- If none of the 4 sides is free, check upwards
  208. if not shootdir then
  209. shootdir = {x=0, y=1, z=0}
  210. local cnode = core.get_node(vector.add(pos, shootdir)).name
  211. if cnode == "ignore" then
  212. shootdir = nil -- Do not push into ignore
  213. end
  214. end
  215. if shootdir then
  216. -- Set new item moving speed accordingly
  217. local newv = vector.multiply(shootdir, 3)
  218. self:disable_physics()
  219. self.object:set_velocity(newv)
  220. self.force_out = newv
  221. self.force_out_start = vector.round(pos)
  222. return
  223. end
  224. end
  225. node = nil -- ground node we're colliding with
  226. if moveresult.touching_ground then
  227. for _, info in ipairs(moveresult.collisions) do
  228. if info.axis == "y" then
  229. node = core.get_node(info.node_pos)
  230. break
  231. end
  232. end
  233. end
  234. -- Slide on slippery nodes
  235. local def = node and core.registered_nodes[node.name]
  236. local keep_movement = false
  237. if def then
  238. local slippery = core.get_item_group(node.name, "slippery")
  239. local vel = self.object:get_velocity()
  240. if slippery ~= 0 and (math.abs(vel.x) > 0.1 or math.abs(vel.z) > 0.1) then
  241. -- Horizontal deceleration
  242. local factor = math.min(4 / (slippery + 4) * dtime, 1)
  243. self.object:set_velocity({
  244. x = vel.x * (1 - factor),
  245. y = 0,
  246. z = vel.z * (1 - factor)
  247. })
  248. keep_movement = true
  249. end
  250. end
  251. if not keep_movement then
  252. self.object:set_velocity({x=0, y=0, z=0})
  253. end
  254. if self.moving_state == keep_movement then
  255. -- Do not update anything until the moving state changes
  256. return
  257. end
  258. self.moving_state = keep_movement
  259. -- Only collect items if not moving
  260. if self.moving_state then
  261. return
  262. end
  263. -- Collect the items around to merge with
  264. local own_stack = ItemStack(self.itemstring)
  265. if own_stack:get_free_space() == 0 then
  266. return
  267. end
  268. local objects = core.get_objects_inside_radius(pos, 1.0)
  269. for k, obj in pairs(objects) do
  270. local entity = obj:get_luaentity()
  271. if entity and entity.name == "__builtin:item" then
  272. if self:try_merge_with(own_stack, obj, entity) then
  273. own_stack = ItemStack(self.itemstring)
  274. if own_stack:get_free_space() == 0 then
  275. return
  276. end
  277. end
  278. end
  279. end
  280. end,
  281. on_punch = function(self, hitter)
  282. local inv = hitter:get_inventory()
  283. if inv and self.itemstring ~= "" then
  284. local left = inv:add_item("main", self.itemstring)
  285. if left and not left:is_empty() then
  286. self:set_item(left)
  287. return
  288. end
  289. end
  290. self.itemstring = ""
  291. self.object:remove()
  292. end,
  293. })