daily pastebin goal
54%
SHARE
TWEET

Untitled

a guest Jul 18th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.                     local vel = self.object:get_velocity().y
  60.                     local new_damage = math.floor((damage / 10) * vel)
  61.  
  62.                     obj:punch(self.object, 4.0, {
  63.                         full_punch_interval = 4.0,
  64.                         damage_groups = {fleshy = damage}
  65.                     })
  66.  
  67.                     self.hurt_timer = 0.2
  68.                 end
  69.             end
  70.         end
  71.     end
  72. end
  73.  
  74.  
  75. core.register_entity(":__builtin:falling_node", {
  76.     initial_properties = {
  77.         visual = "wielditem",
  78.         visual_size = {x = 0.667, y = 0.667},
  79.         textures = {},
  80.         physical = true,
  81.         is_visible = false,
  82.         collide_with_objects = false,
  83.         collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
  84.     },
  85.  
  86.     node = {},
  87.     meta = {},
  88.  
  89.     set_node = function(self, node, meta)
  90.  
  91.         self.node = node
  92.         self.meta = meta or {}
  93.         self.hurt_timer = 0
  94.  
  95.         self.object:set_properties({
  96.             is_visible = true,
  97.             textures = {node.name},
  98.         })
  99.     end,
  100.  
  101.     get_staticdata = function(self)
  102.  
  103.         local ds = {node = self.node, meta = self.meta}
  104.  
  105.         return core.serialize(ds)
  106.     end,
  107.  
  108.     on_activate = function(self, staticdata)
  109.  
  110.         self.object:set_armor_groups({immortal = 1})
  111.         self.object:set_acceleration({x = 0, y = -10, z = 0})
  112.  
  113.         local ds = core.deserialize(staticdata)
  114.  
  115.         if ds and ds.node then
  116.  
  117.             self:set_node(ds.node, ds.meta)
  118.  
  119.         elseif ds then
  120.  
  121.             self:set_node(ds)
  122.  
  123.         elseif staticdata ~= "" then
  124.  
  125.             self:set_node({name = staticdata})
  126.         end
  127.     end,
  128.  
  129.     on_step = function(self, dtime)
  130.  
  131.         self.timer = (self.timer or 0) + dtime
  132.  
  133.         if self.timer < delay then
  134.             return
  135.         end
  136.  
  137.         self.timer = 0
  138.  
  139.         -- Set gravity
  140.         local acceleration = self.object:get_acceleration()
  141.  
  142.         if not vector.equals(acceleration, {x = 0, y = -10, z = 0}) then
  143.             self.object:set_acceleration({x = 0, y = -10, z = 0})
  144.         end
  145.  
  146.         local pos = self.object:get_pos()
  147.  
  148.         -- Position of bottom center point
  149.         local below_pos = {x = pos.x, y = pos.y - 0.7, z = pos.z}
  150.  
  151.         -- Check for player/mobs below falling node and hurt them >:D
  152.         fall_hurt_check(self, below_pos, dtime)
  153.  
  154.         -- Avoid bugs caused by an unloaded node below
  155.         local below_node = core.get_node(below_pos)
  156.  
  157.         -- Delete on contact with ignore at world edges
  158.         if below_node.name == "ignore" then
  159.  
  160.             self.object:remove()
  161.  
  162.             return
  163.         end
  164.  
  165.         local below_nodef = core.registered_nodes[below_node.name]
  166.  
  167.         -- Is it a level node we can add to?
  168.         if below_nodef
  169.         and below_nodef.leveled
  170.         and below_node.name == self.node.name then
  171.  
  172.             local addlevel = self.node.level
  173.  
  174.             if not addlevel or addlevel <= 0 then
  175.                 addlevel = below_nodef.leveled
  176.             end
  177.  
  178.             if core.add_node_level(below_pos, addlevel) == 0 then
  179.                 self.object:remove()
  180.                 return
  181.             end
  182.         end
  183.  
  184.         -- Stop node if it falls on walkable surface, or floats on water
  185.         if (below_nodef and below_nodef.walkable == true)
  186.         or (below_nodef
  187.                 and core.get_item_group(self.node.name, "float") ~= 0
  188.                 and below_nodef.liquidtype ~= "none") then
  189.  
  190.             self.object:set_velocity({x = 0, y = 0, z = 0})
  191.         end
  192.  
  193.         -- Square up fallen node on stop
  194.         local vel = self.object:get_velocity()
  195.  
  196.         if vector.equals(vel, {x = 0, y = 0, z = 0}) then
  197.  
  198.             local npos = self.object:get_pos()
  199.  
  200.             -- Get node we've landed inside
  201.             local cnode = minetest.get_node(npos).name
  202.             local cdef = core.registered_nodes[cnode]
  203.  
  204.             -- If 'air' or buildable_to then place node, otherwise drop as item
  205.             if cnode == "air"
  206.             or (cdef and cdef.buildable_to == true)
  207.             or cdef.liquidtype ~= "none" then
  208.  
  209.                 core.add_node(npos, self.node)
  210.  
  211.                 self.object:remove()
  212.  
  213.                 local def = core.registered_nodes[self.node.name]
  214.  
  215.                 if def.sounds and def.sounds.place and def.sounds.place.name then
  216.                     core.sound_play(def.sounds.place, {pos = np})
  217.                 end
  218.  
  219.                 core.check_for_falling(npos)
  220.             else
  221.                 self.object:remove()
  222.                 core.add_item(npos, self.node)
  223.             end
  224.         end
  225.     end
  226. })
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top