Advertisement
Guest User

Untitled

a guest
Apr 4th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 KB | None | 0 0
  1.  
  2. -- Minetest: builtin/item.lua (override falling entity with new features)
  3.  
  4. local builtin_shared = ...
  5.  
  6. -- override falling nodes to add damage
  7.  
  8. local function add_fall_damage(node, damage)
  9.  
  10. if core.registered_nodes[node] then
  11.  
  12. local group = core.registered_nodes[node].groups
  13.  
  14. group.falling_node_damage = damage
  15.  
  16. core.override_item(node, {groups = group})
  17. else
  18. print (node .. " not found to add fall_damage to")
  19. end
  20. end
  21.  
  22. add_fall_damage("default:sand", 2)
  23. add_fall_damage("default:desert_sand", 2)
  24. add_fall_damage("default:silver_sand", 2)
  25. add_fall_damage("caverealms:coal_dust", 3)
  26. add_fall_damage("tnt:tnt_burning", 4)
  27.  
  28.  
  29. --
  30. -- Falling stuff
  31. --
  32.  
  33. local node_fall_hurt = minetest.setting_getbool("node_fall_hurt") ~= false
  34. local delay = 0.25
  35.  
  36. local function fall_hurt_check(self, pos, dtime)
  37.  
  38. if not node_fall_hurt then return end
  39.  
  40. if self.hurt_timer > 0 then
  41. self.hurt_timer = self.hurt_timer - (dtime * delay)
  42. else
  43. -- Get damage level from falling_node_damage group
  44. local damage = core.registered_nodes[self.node.name] and
  45. core.registered_nodes[self.node.name].groups.falling_node_damage
  46.  
  47. if damage then
  48.  
  49. local all_objects = minetest.get_objects_inside_radius(pos, 0.7)
  50.  
  51. for _,obj in ipairs(all_objects) do
  52.  
  53. local name = obj:get_luaentity() and
  54. obj:get_luaentity().name or ""
  55.  
  56. if name ~= "__builtin:item"
  57. and name ~= "__builtin:falling_node" then
  58.  
  59. obj:punch(obj, 4.0, {
  60. full_punch_interval = 4.0,
  61. damage_groups = {fleshy = damage}})
  62. self.hurt_timer = 0.2
  63. end
  64. end
  65. end
  66. end
  67. end
  68.  
  69.  
  70. core.register_entity(":__builtin:falling_node", {
  71. initial_properties = {
  72. visual = "wielditem",
  73. visual_size = {x = 0.667, y = 0.667},
  74. textures = {},
  75. physical = true,
  76. is_visible = false,
  77. collide_with_objects = false,
  78. collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
  79. },
  80.  
  81. node = {},
  82. meta = {},
  83.  
  84. set_node = function(self, node, meta)
  85.  
  86. self.node = node
  87. self.meta = meta or {}
  88. self.hurt_timer = 0
  89.  
  90. self.object:set_properties({
  91. is_visible = true,
  92. textures = {node.name},
  93. })
  94. end,
  95.  
  96. get_staticdata = function(self)
  97.  
  98. local ds = {node = self.node, meta = self.meta}
  99.  
  100. return core.serialize(ds)
  101. end,
  102.  
  103. on_activate = function(self, staticdata)
  104.  
  105. self.object:set_armor_groups({immortal = 1})
  106.  
  107. local ds = core.deserialize(staticdata)
  108.  
  109. if ds and ds.node then
  110.  
  111. self:set_node(ds.node, ds.meta)
  112.  
  113. elseif ds then
  114.  
  115. self:set_node(ds)
  116.  
  117. elseif staticdata ~= "" then
  118.  
  119. self:set_node({name = staticdata})
  120. end
  121. end,
  122.  
  123. on_step = function(self, dtime)
  124.  
  125. self.timer = (self.timer or 0) + dtime
  126.  
  127. if self.timer < delay then
  128. return
  129. end
  130.  
  131. self.timer = 0
  132.  
  133. -- Set gravity
  134. local acceleration = self.object:getacceleration()
  135.  
  136. if not vector.equals(acceleration, {x = 0, y = -10, z = 0}) then
  137. self.object:setacceleration({x = 0, y = -10, z = 0})
  138. end
  139.  
  140. -- Turn to actual node when colliding with ground, or continue to move
  141. local pos = self.object:get_pos()
  142.  
  143. -- Position of bottom center point
  144. local below_pos = {x = pos.x, y = pos.y - 0.7, z = pos.z}
  145.  
  146. -- Check for player/mobs below falling node and hurt them >:D
  147. fall_hurt_check(self, below_pos, dtime)
  148.  
  149. -- Avoid bugs caused by an unloaded node below
  150. local below_node = core.get_node_or_nil(below_pos)
  151.  
  152. -- Delete on contact with ignore at world edges
  153. if below_node and below_node.name == "ignore" then
  154.  
  155. self.object:remove()
  156.  
  157. return
  158. end
  159.  
  160. local below_nodef = below_node and
  161. core.registered_nodes[below_node.name]
  162.  
  163. -- Is below node walkable, or liquid that node floats on?
  164. if below_node and
  165. (not below_nodef or below_nodef.walkable or
  166. (core.get_item_group(self.node.name, "float") ~= 0 and
  167. below_nodef.liquidtype ~= "none")) then
  168.  
  169. -- Is it a level node we can add to?
  170. if below_nodef and below_nodef.leveled and
  171. below_node.name == self.node.name then
  172.  
  173. local addlevel = self.node.level
  174.  
  175. if not addlevel or addlevel <= 0 then
  176. addlevel = below_nodef.leveled
  177. end
  178.  
  179. if core.add_node_level(below_pos, addlevel) == 0 then
  180.  
  181. self.object:remove()
  182.  
  183. return
  184. end
  185.  
  186. -- Remove node below if buildable_to and not liquid?
  187. elseif below_nodef and below_nodef.buildable_to and
  188. (core.get_item_group(self.node.name, "float") == 0 or
  189. below_nodef.liquidtype == "none") then
  190.  
  191. core.remove_node(below_pos)
  192.  
  193. return
  194. end
  195.  
  196. -- Get falling node position and see what's there
  197. -- local np = {x = below_pos.x, y = below_pos.y + 1, z = below_pos.z}
  198. local np = self.object:get_pos()
  199. local nod = core.get_node(np)
  200. local nodef = core.registered_nodes[nod.name]
  201. local unbreak = nodef and
  202. core.get_item_group(nod.name, "unbreakable") ~= 0
  203.  
  204. -- If not air or liquid or has unbreakable group set,
  205. -- remove node and replace with it's drops
  206. if nod.name ~= "air" and not unbreak and
  207. (not nodef or nodef.liquidtype == "none") then
  208.  
  209. core.remove_node(np)
  210.  
  211. if nodef.buildable_to == false then
  212.  
  213. -- Add dropped items
  214. local drops = core.get_node_drops(nod, "")
  215.  
  216. for _, dropped_item in pairs(drops) do
  217. core.add_item(np, dropped_item)
  218. end
  219. end
  220.  
  221. -- Run script hook
  222. for _, callback in pairs(core.registered_on_dignodes) do
  223. callback(np, nod)
  224. end
  225. end
  226.  
  227. -- Create node and remove entity
  228. local def = core.registered_nodes[self.node.name]
  229.  
  230. if def then
  231.  
  232. -- If the node below has unbreakable group set
  233. -- then move the Y position up 1
  234. if unbreak then
  235. np.y = np.y + 1
  236. end
  237.  
  238. core.add_node(np, self.node)
  239.  
  240. if self.meta then
  241.  
  242. local meta = core.get_meta(np)
  243.  
  244. meta:from_table(self.meta)
  245. end
  246.  
  247. if def.sounds and def.sounds.place and def.sounds.place.name then
  248. core.sound_play(def.sounds.place, {pos = np})
  249. end
  250. end
  251.  
  252. self.object:remove()
  253.  
  254. core.check_for_falling(np)
  255.  
  256. return
  257. end
  258.  
  259. local vel = self.object:getvelocity()
  260.  
  261. if vector.equals(vel, {x = 0, y = 0, z = 0}) then
  262.  
  263. local npos = self.object:get_pos()
  264.  
  265. self.object:setpos(vector.round(npos))
  266. end
  267. end
  268. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement