2
0

item_entity.lua 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. function core.spawn_item(pos, item)
  2. -- Take item in any format
  3. local stack = ItemStack(item)
  4. local obj = core.add_entity(pos, "__builtin:item")
  5. -- Don't use obj if it couldn't be added to the map.
  6. if obj then
  7. obj:get_luaentity():set_item(stack:to_string())
  8. end
  9. return obj
  10. end
  11. -- If item_entity_ttl is not set, enity will have default life time
  12. -- Setting it to -1 disables the feature
  13. local time_to_live = tonumber(core.settings:get("item_entity_ttl")) or 900
  14. local gravity = tonumber(core.settings:get("movement_gravity")) or 9.81
  15. core.register_entity(":__builtin:item", {
  16. initial_properties = {
  17. hp_max = 1,
  18. physical = true,
  19. collide_with_objects = false,
  20. collisionbox = {-0.3, -0.3, -0.3, 0.3, 0.3, 0.3},
  21. visual = "wielditem",
  22. visual_size = {x = 0.4, y = 0.4},
  23. textures = {""},
  24. is_visible = false,
  25. },
  26. itemstring = "",
  27. moving_state = true,
  28. physical_state = true,
  29. -- Item expiry
  30. age = 0,
  31. -- Pushing item out of solid nodes
  32. force_out = nil,
  33. force_out_start = nil,
  34. set_item = function(self, item)
  35. local stack = ItemStack(item or self.itemstring)
  36. self.itemstring = stack:to_string()
  37. if self.itemstring == "" then
  38. -- item not yet known
  39. return
  40. end
  41. -- Backwards compatibility: old clients use the texture
  42. -- to get the type of the item
  43. local itemname = stack:is_known() and stack:get_name() or "unknown"
  44. local max_count = stack:get_stack_max()
  45. local count = math.min(stack:get_count(), max_count)
  46. local size = 0.2 + 0.1 * (count / max_count) ^ (1 / 3)
  47. local def = core.registered_items[itemname]
  48. local glow = def and def.light_source and
  49. math.floor(def.light_source / 2 + 0.5)
  50. local size_bias = 1e-3 * math.random() -- small random bias to counter Z-fighting
  51. local c = {-size, -size, -size, size, size, size}
  52. self.object:set_properties({
  53. is_visible = true,
  54. visual = "wielditem",
  55. textures = {itemname},
  56. visual_size = {x = size + size_bias, y = size + size_bias},
  57. collisionbox = c,
  58. automatic_rotate = math.pi * 0.5 * 0.2 / size,
  59. wield_item = self.itemstring,
  60. glow = glow,
  61. infotext = stack:get_description(),
  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. -- Cannot 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. -- Cannot 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. -- Prevent assert when item_entity is attached
  157. if moveresult == nil and self.object:get_attach() then
  158. return
  159. end
  160. if self.force_out then
  161. -- This code runs after the entity got a push from the is_stuck code.
  162. -- It makes sure the entity is entirely outside the solid node
  163. local c = self._collisionbox
  164. local s = self.force_out_start
  165. local f = self.force_out
  166. local ok = (f.x > 0 and pos.x + c[1] > s.x + 0.5) or
  167. (f.y > 0 and pos.y + c[2] > s.y + 0.5) or
  168. (f.z > 0 and pos.z + c[3] > s.z + 0.5) or
  169. (f.x < 0 and pos.x + c[4] < s.x - 0.5) or
  170. (f.z < 0 and pos.z + c[6] < s.z - 0.5)
  171. if ok then
  172. -- Item was successfully forced out
  173. self.force_out = nil
  174. self:enable_physics()
  175. return
  176. end
  177. end
  178. if not self.physical_state then
  179. return -- Don't do anything
  180. end
  181. assert(moveresult,
  182. "Collision info missing, this is caused by an out-of-date/buggy mod or game")
  183. if not moveresult.collides then
  184. -- future TODO: items should probably decelerate in air
  185. return
  186. end
  187. -- Push item out when stuck inside solid node
  188. local is_stuck = false
  189. local snode = core.get_node_or_nil(pos)
  190. if snode then
  191. local sdef = core.registered_nodes[snode.name] or {}
  192. is_stuck = (sdef.walkable == nil or sdef.walkable == true)
  193. and (sdef.collision_box == nil or sdef.collision_box.type == "regular")
  194. and (sdef.node_box == nil or sdef.node_box.type == "regular")
  195. end
  196. if is_stuck then
  197. local shootdir
  198. local order = {
  199. {x=1, y=0, z=0}, {x=-1, y=0, z= 0},
  200. {x=0, y=0, z=1}, {x= 0, y=0, z=-1},
  201. }
  202. -- Check which one of the 4 sides is free
  203. for o = 1, #order do
  204. local cnode = core.get_node(vector.add(pos, order[o])).name
  205. local cdef = core.registered_nodes[cnode] or {}
  206. if cnode ~= "ignore" and cdef.walkable == false then
  207. shootdir = order[o]
  208. break
  209. end
  210. end
  211. -- If none of the 4 sides is free, check upwards
  212. if not shootdir then
  213. shootdir = {x=0, y=1, z=0}
  214. local cnode = core.get_node(vector.add(pos, shootdir)).name
  215. if cnode == "ignore" then
  216. shootdir = nil -- Do not push into ignore
  217. end
  218. end
  219. if shootdir then
  220. -- Set new item moving speed accordingly
  221. local newv = vector.multiply(shootdir, 3)
  222. self:disable_physics()
  223. self.object:set_velocity(newv)
  224. self.force_out = newv
  225. self.force_out_start = vector.round(pos)
  226. return
  227. end
  228. end
  229. node = nil -- ground node we're colliding with
  230. if moveresult.touching_ground then
  231. for _, info in ipairs(moveresult.collisions) do
  232. if info.axis == "y" then
  233. node = core.get_node(info.node_pos)
  234. break
  235. end
  236. end
  237. end
  238. -- Slide on slippery nodes
  239. local def = node and core.registered_nodes[node.name]
  240. local keep_movement = false
  241. if def then
  242. local slippery = core.get_item_group(node.name, "slippery")
  243. local vel = self.object:get_velocity()
  244. if slippery ~= 0 and (math.abs(vel.x) > 0.1 or math.abs(vel.z) > 0.1) then
  245. -- Horizontal deceleration
  246. local factor = math.min(4 / (slippery + 4) * dtime, 1)
  247. self.object:set_velocity({
  248. x = vel.x * (1 - factor),
  249. y = 0,
  250. z = vel.z * (1 - factor)
  251. })
  252. keep_movement = true
  253. end
  254. end
  255. if not keep_movement then
  256. self.object:set_velocity({x=0, y=0, z=0})
  257. end
  258. if self.moving_state == keep_movement then
  259. -- Do not update anything until the moving state changes
  260. return
  261. end
  262. self.moving_state = keep_movement
  263. -- Only collect items if not moving
  264. if self.moving_state then
  265. return
  266. end
  267. -- Collect the items around to merge with
  268. local own_stack = ItemStack(self.itemstring)
  269. if own_stack:get_free_space() == 0 then
  270. return
  271. end
  272. local objects = core.get_objects_inside_radius(pos, 1.0)
  273. for k, obj in pairs(objects) do
  274. local entity = obj:get_luaentity()
  275. if entity and entity.name == "__builtin:item" then
  276. if self:try_merge_with(own_stack, obj, entity) then
  277. own_stack = ItemStack(self.itemstring)
  278. if own_stack:get_free_space() == 0 then
  279. return
  280. end
  281. end
  282. end
  283. end
  284. end,
  285. on_punch = function(self, hitter, ...)
  286. if self.itemstring == "" then
  287. self.object:remove()
  288. return
  289. end
  290. -- Call on_pickup callback in item definition.
  291. local itemstack = ItemStack(self.itemstring)
  292. local callback = itemstack:get_definition().on_pickup
  293. local ret = callback(itemstack, hitter, {type = "object", ref = self.object}, ...)
  294. if not ret then
  295. -- Don't modify (and don't reset rotation)
  296. return
  297. end
  298. itemstack = ItemStack(ret)
  299. -- Handle the leftover itemstack
  300. if itemstack:is_empty() then
  301. self.itemstring = ""
  302. self.object:remove()
  303. else
  304. self:set_item(itemstack)
  305. end
  306. end,
  307. })