SHARE
TWEET

Untitled

a guest Jul 19th, 2018 59 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.             return
  162.         end
  163.  
  164.         local below_nodef = core.registered_nodes[below_node.name]
  165.  
  166.         -- Is it a level node we can add to?
  167.         if below_nodef
  168.         and below_nodef.leveled
  169.         and below_node.name == self.node.name then
  170.  
  171.             local addlevel = self.node.level
  172.  
  173.             if not addlevel or addlevel <= 0 then
  174.                 addlevel = below_nodef.leveled
  175.             end
  176.  
  177.             if core.add_node_level(below_pos, addlevel) == 0 then
  178.                 self.object:remove()
  179.                 return
  180.             end
  181.         end
  182.  
  183.         -- Stop node if it falls on walkable surface, or floats on water
  184.         if (below_nodef and below_nodef.walkable == true)
  185.         or (below_nodef
  186.                 and core.get_item_group(self.node.name, "float") ~= 0
  187.                 and below_nodef.liquidtype ~= "none") then
  188.  
  189.             self.object:set_velocity({x = 0, y = 0, z = 0})
  190.         end
  191.  
  192.         -- Square up fallen node on stop
  193.         local vel = self.object:get_velocity()
  194.  
  195.         if vector.equals(vel, {x = 0, y = 0, z = 0}) then
  196.  
  197.             local npos = self.object:get_pos()
  198.  
  199.             -- Get node we've landed inside
  200.             local cnode = minetest.get_node(npos).name
  201.             local cdef = core.registered_nodes[cnode]
  202.  
  203.             -- If 'air' or buildable_to then place node, otherwise drop as item
  204.             if cnode == "air"
  205.             or (cdef and cdef.buildable_to == true)
  206.             or (cdef and cdef.liquidtype ~= "none")
  207.             or core.get_item_group(cnode, "attached_node") ~= 0 then
  208.  
  209.                 -- if attached node then drop drops (grass, flowers, crops)
  210.                 if core.get_item_group(cnode, "attached_node") ~= 0 then
  211.  
  212.                     -- Add drops from attached node
  213.                     local drops = core.get_node_drops(cnode, "")
  214.  
  215.                     for _, dropped_item in pairs(drops) do
  216.                         core.add_item(npos, dropped_item)
  217.                     end
  218.  
  219.                     -- Run script hook
  220.                     for _, callback in pairs(core.registered_on_dignodes) do
  221.                         callback(npos, cnode)
  222.                     end
  223.                 end
  224.  
  225.                 core.add_node(npos, self.node)
  226.  
  227.                 if self.meta then
  228.                     local meta = core.get_meta(npos)
  229.                     meta:from_table(self.meta)
  230.                 end
  231.  
  232.                 local def = core.registered_nodes[self.node.name]
  233.  
  234.                 if def.sounds and def.sounds.place and def.sounds.place.name then
  235.                     core.sound_play(def.sounds.place, {pos = np})
  236.                 end
  237.  
  238.                 self.object:remove()
  239.  
  240.                 core.check_for_falling(npos)
  241.             else
  242.                 -- Add drops from falling node
  243.                 local drops = core.get_node_drops(self.node, "")
  244.  
  245.                 for _, dropped_item in pairs(drops) do
  246.                     core.add_item(npos, dropped_item)
  247.                 end
  248.  
  249.                 self.object:remove()
  250.             end
  251.         end
  252.     end
  253. })
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