init.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. -- Minetest: builtin/item_entity.lua
  2. -- override ice to make slippery for 0.4.16
  3. minetest.override_item("default:ice", {
  4. groups = {cracky = 3, puts_out_fire = 1, cools_lava = 1, slippery = 3}})
  5. function core.spawn_item(pos, item)
  6. local stack = ItemStack(item)
  7. local obj = core.add_entity(pos, "__builtin:item")
  8. if obj then
  9. obj:get_luaentity():set_item(stack:to_string())
  10. end
  11. return obj
  12. end
  13. -- If item_entity_ttl is not set, enity will have default life time
  14. -- Setting it to -1 disables the feature
  15. local time_to_live = tonumber(core.settings:get("item_entity_ttl")) or 900
  16. local gravity = tonumber(core.settings:get("movement_gravity")) or 9.81
  17. local destroy_item = core.settings:get_bool("destroy_item") ~= false
  18. -- water flow functions by QwertyMine3, edited by TenPlus1
  19. local function to_unit_vector(dir_vector)
  20. local inv_roots = {
  21. [0] = 1,
  22. [1] = 1,
  23. [2] = 0.70710678118655,
  24. [4] = 0.5,
  25. [5] = 0.44721359549996,
  26. [8] = 0.35355339059327
  27. }
  28. local sum = dir_vector.x * dir_vector.x + dir_vector.z * dir_vector.z
  29. return {
  30. x = dir_vector.x * inv_roots[sum],
  31. y = dir_vector.y,
  32. z = dir_vector.z * inv_roots[sum]
  33. }
  34. end
  35. local function node_ok(pos)
  36. local node = minetest.get_node_or_nil(pos)
  37. if node and minetest.registered_nodes[node.name] then
  38. return node
  39. end
  40. return minetest.registered_nodes["default:dirt"]
  41. end
  42. local function quick_flow_logic(node, pos_testing, direction)
  43. local node_testing = node_ok(pos_testing)
  44. if minetest.registered_nodes[node_testing.name].liquidtype ~= "flowing"
  45. and minetest.registered_nodes[node_testing.name].liquidtype ~= "source" then
  46. return 0
  47. end
  48. local param2_testing = node_testing.param2
  49. if param2_testing < node.param2 then
  50. if (node.param2 - param2_testing) > 6 then
  51. return -direction
  52. else
  53. return direction
  54. end
  55. elseif param2_testing > node.param2 then
  56. if (param2_testing - node.param2) > 6 then
  57. return direction
  58. else
  59. return -direction
  60. end
  61. end
  62. return 0
  63. end
  64. local function quick_flow(pos, node)
  65. if not minetest.registered_nodes[node.name].groups.liquid then
  66. return {x = 0, y = 0, z = 0}
  67. end
  68. local x, z = 0, 0
  69. x = x + quick_flow_logic(node, {x = pos.x - 1, y = pos.y, z = pos.z},-1)
  70. x = x + quick_flow_logic(node, {x = pos.x + 1, y = pos.y, z = pos.z}, 1)
  71. z = z + quick_flow_logic(node, {x = pos.x, y = pos.y, z = pos.z - 1},-1)
  72. z = z + quick_flow_logic(node, {x = pos.x, y = pos.y, z = pos.z + 1}, 1)
  73. return to_unit_vector({x = x, y = 0, z = z})
  74. end
  75. -- END water flow functions
  76. -- particle effects for when item is destroyed
  77. local function add_effects(pos)
  78. minetest.add_particlespawner({
  79. amount = 1,
  80. time = 0.25,
  81. minpos = pos,
  82. maxpos = pos,
  83. minvel = {x = -1, y = 2, z = -1},
  84. maxvel = {x = 1, y = 4, z = 1},
  85. minacc = {x = 0, y = 0, z = 0},
  86. maxacc = {x = 0, y = 0, z = 0},
  87. minexptime = 1,
  88. maxexptime = 3,
  89. minsize = 1,
  90. maxsize = 4,
  91. texture = "tnt_smoke.png",
  92. })
  93. end
  94. core.register_entity(":__builtin:item", {
  95. initial_properties = {
  96. hp_max = 1,
  97. physical = true,
  98. collide_with_objects = false,
  99. collisionbox = {-0.3, -0.3, -0.3, 0.3, 0.3, 0.3},
  100. visual = "wielditem",
  101. visual_size = {x = 0.4, y = 0.4},
  102. textures = {""},
  103. spritediv = {x = 1, y = 1},
  104. initial_sprite_basepos = {x = 0, y = 0},
  105. is_visible = false,
  106. infotext = "",
  107. },
  108. itemstring = "",
  109. moving_state = true,
  110. slippery_state = false,
  111. age = 0,
  112. set_item = function(self, item)
  113. local stack = ItemStack(item or self.itemstring)
  114. self.itemstring = stack:to_string()
  115. if self.itemstring == "" then
  116. return
  117. end
  118. local itemname = stack:is_known() and stack:get_name() or "unknown"
  119. local max_count = stack:get_stack_max()
  120. local count = math.min(stack:get_count(), max_count)
  121. local size = 0.2 + 0.1 * (count / max_count) ^ (1 / 3)
  122. local col_height = size * 0.75
  123. local def = core.registered_nodes[itemname]
  124. local glow = def and def.light_source
  125. self.object:set_properties({
  126. is_visible = true,
  127. visual = "wielditem",
  128. textures = {itemname},
  129. visual_size = {x = size, y = size},
  130. collisionbox = {-size, -col_height, -size, size, col_height, size},
  131. selectionbox = {-size, -size, -size, size, size, size},
  132. automatic_rotate = 0.314 / size,
  133. wield_item = self.itemstring,
  134. glow = glow,
  135. infotext = core.registered_items[itemname].description
  136. })
  137. end,
  138. get_staticdata = function(self)
  139. return core.serialize({
  140. itemstring = self.itemstring,
  141. age = self.age,
  142. dropped_by = self.dropped_by
  143. })
  144. end,
  145. on_activate = function(self, staticdata, dtime_s)
  146. if string.sub(staticdata, 1, string.len("return")) == "return" then
  147. local data = core.deserialize(staticdata)
  148. if data and type(data) == "table" then
  149. self.itemstring = data.itemstring
  150. self.age = (data.age or 0) + dtime_s
  151. self.dropped_by = data.dropped_by
  152. end
  153. else
  154. self.itemstring = staticdata
  155. end
  156. self.object:set_armor_groups({immortal = 1})
  157. self.object:set_velocity({x = 0, y = 2, z = 0})
  158. self.object:set_acceleration({x = 0, y = -gravity, z = 0})
  159. self:set_item()
  160. end,
  161. try_merge_with = function(self, own_stack, object, entity)
  162. if self.age == entity.age then
  163. return false -- Can not merge with itself
  164. end
  165. local stack = ItemStack(entity.itemstring)
  166. local name = stack:get_name()
  167. if own_stack:get_name() ~= name
  168. or own_stack:get_meta() ~= stack:get_meta()
  169. or own_stack:get_wear() ~= stack:get_wear()
  170. or own_stack:get_free_space() == 0 then
  171. return false -- Can not merge different or full stack
  172. end
  173. local count = own_stack:get_count()
  174. local total_count = stack:get_count() + count
  175. local max_count = stack:get_stack_max()
  176. if total_count > max_count then
  177. return false
  178. end
  179. -- Merge the remote stack into this one
  180. local pos = object:get_pos()
  181. pos.y = pos.y + ((total_count - count) / max_count) * 0.15
  182. self.object:move_to(pos)
  183. self.age = 0 -- Handle as new entity
  184. own_stack:set_count(total_count)
  185. self:set_item(own_stack)
  186. entity.itemstring = ""
  187. object:remove()
  188. return true
  189. end,
  190. on_step = function(self, dtime)
  191. local pos = self.object:get_pos()
  192. self.age = self.age + dtime
  193. if time_to_live > 0 and self.age > time_to_live then
  194. self.itemstring = ""
  195. self.object:remove()
  196. add_effects(pos)
  197. return
  198. end
  199. -- get nodes every 1/4 second
  200. self.timer = (self.timer or 0) + dtime
  201. if self.timer > 0.25 or not self.node_inside then
  202. self.node_inside = minetest.get_node_or_nil(pos)
  203. self.def_inside = self.node_inside
  204. and core.registered_nodes[self.node_inside.name]
  205. self.node_under = minetest.get_node_or_nil({
  206. x = pos.x,
  207. y = pos.y + self.object:get_properties().collisionbox[2] - 0.05,
  208. z = pos.z
  209. })
  210. self.def_under = self.node_under
  211. and core.registered_nodes[self.node_under.name]
  212. self.timer = 0
  213. end
  214. local node = self.node_inside
  215. -- Delete in 'ignore' nodes
  216. if node and node.name == "ignore" then
  217. self.itemstring = ""
  218. self.object:remove()
  219. return
  220. end
  221. -- do custom step function
  222. local name = ItemStack(self.itemstring):get_name() or ""
  223. local custom = core.registered_items[name]
  224. and core.registered_items[name].dropped_step
  225. if custom and custom(self, pos, dtime) == false then
  226. return -- skip further checks if false
  227. end
  228. local vel = self.object:get_velocity()
  229. local def = self.def_inside
  230. local is_slippery = false
  231. local is_moving = (def and not def.walkable) or
  232. vel.x ~= 0 or vel.y ~= 0 or vel.z ~= 0
  233. -- destroy item when dropped into lava (if enabled)
  234. if destroy_item and def and def.groups and def.groups.lava then
  235. minetest.sound_play("builtin_item_lava", {
  236. pos = pos,
  237. max_hear_distance = 6,
  238. gain = 0.5
  239. })
  240. self.itemstring = ""
  241. self.object:remove()
  242. add_effects(pos)
  243. return
  244. end
  245. -- water flowing
  246. if def and def.liquidtype == "flowing" then
  247. local vec = quick_flow(pos, node)
  248. local v = self.object:get_velocity()
  249. self.object:set_velocity({x = vec.x, y = v.y, z = vec.z})
  250. return
  251. end
  252. -- item inside block, move to vacant space
  253. if def and (def.walkable == nil or def.walkable == true)
  254. and (def.collision_box == nil or def.collision_box.type == "regular")
  255. and (def.node_box == nil or def.node_box.type == "regular") then
  256. local npos = minetest.find_node_near(pos, 1, "air")
  257. if npos then
  258. self.object:move_to(npos)
  259. end
  260. self.node_inside = nil -- force get_node
  261. return
  262. end
  263. -- Switch locals to node under
  264. node = self.node_under
  265. def = self.def_under
  266. -- Slippery node check
  267. if def and def.walkable then
  268. local slippery = core.get_item_group(node.name, "slippery")
  269. is_slippery = slippery ~= 0
  270. if is_slippery and (math.abs(vel.x) > 0.2 or math.abs(vel.z) > 0.2) then
  271. -- Horizontal deceleration
  272. local slip_factor = 4.0 / (slippery + 4)
  273. self.object:set_acceleration({
  274. x = -vel.x * slip_factor,
  275. y = 0,
  276. z = -vel.z * slip_factor
  277. })
  278. elseif vel.y == 0 then
  279. is_moving = false
  280. end
  281. end
  282. if self.moving_state == is_moving
  283. and self.slippery_state == is_slippery then
  284. return -- No further updates until moving state changes
  285. end
  286. self.moving_state = is_moving
  287. self.slippery_state = is_slippery
  288. if is_moving then
  289. self.object:set_acceleration({x = 0, y = -gravity, z = 0})
  290. else
  291. self.object:set_acceleration({x = 0, y = 0, z = 0})
  292. self.object:set_velocity({x = 0, y = 0, z = 0})
  293. end
  294. --Only collect items if not moving
  295. if is_moving then
  296. return
  297. end
  298. -- Collect the items around to merge with
  299. local own_stack = ItemStack(self.itemstring)
  300. if own_stack:get_free_space() == 0 then
  301. return
  302. end
  303. local objects = core.get_objects_inside_radius(pos, 1.0)
  304. for k, obj in pairs(objects) do
  305. local entity = obj:get_luaentity()
  306. if entity and entity.name == "__builtin:item" then
  307. if self:try_merge_with(own_stack, obj, entity) then
  308. own_stack = ItemStack(self.itemstring)
  309. if own_stack:get_free_space() == 0 then
  310. return
  311. end
  312. end
  313. end
  314. end
  315. end,
  316. on_punch = function(self, hitter)
  317. local inv = hitter:get_inventory()
  318. if inv and self.itemstring ~= "" then
  319. local left = inv:add_item("main", self.itemstring)
  320. if left and not left:is_empty() then
  321. self:set_item(left)
  322. return
  323. end
  324. end
  325. self.itemstring = ""
  326. self.object:remove()
  327. end,
  328. })