item_entity.lua 8.8 KB

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