init.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. -- Use whenever you would use `minetest.registered_nodes' but don't need stairs.
  2. minetest.reg_ns_nodes = {}
  3. if not minetest.global_exists("falldamage") then falldamage = {} end
  4. falldamage.modpath = minetest.get_modpath("falldamage")
  5. dofile(falldamage.modpath .. "/tilesheet.lua")
  6. dofile(falldamage.modpath .. "/rangecheck.lua")
  7. dofile(falldamage.modpath .. "/liquidinteraction.lua")
  8. local function copy_pointed_thing(pointed_thing)
  9. return {
  10. type = pointed_thing.type,
  11. above = vector.new(pointed_thing.above),
  12. under = vector.new(pointed_thing.under),
  13. ref = pointed_thing.ref,
  14. }
  15. end
  16. local old_register_craftitem = minetest.register_craftitem
  17. function minetest.register_craftitem(name, def2)
  18. local def = table.copy(def2)
  19. if type(def.stack_max) == "nil" then
  20. def.stack_max = 64
  21. end
  22. if type(def.inventory_image) == "string" then
  23. def.inventory_image = image.get(def.inventory_image)
  24. end
  25. if type(def.wield_image) == "string" then
  26. def.wield_image = image.get(def.wield_image)
  27. end
  28. return old_register_craftitem(name, def)
  29. end
  30. local old_register_tool = minetest.register_tool
  31. function minetest.register_tool(name, def)
  32. local ndef = table.copy(def)
  33. if ndef.tool_capabilities then
  34. local rangemod = (ndef.tool_capabilities.range_modifier or 1)
  35. local defrange = 4
  36. -- Swords have less range, should make fighting the mobs a bit more challenging.
  37. if name:find("sword") then
  38. -- Note: not really a good idea. Leave at 4.
  39. -- Too many player expectations would break.
  40. defrange = 4
  41. end
  42. -- Get the damage groups from centralized source.
  43. ndef.tool_capabilities.damage_groups =
  44. sysdmg.get_damage_groups_for(name, ndef.tool_capabilities.damage_groups)
  45. ndef.range = (ndef.range or defrange) * rangemod
  46. end
  47. return old_register_tool(name, ndef)
  48. end
  49. -- Override minetest.register_node so that we can modify the falling damage GLOBALLY.
  50. local old_register_node = minetest.register_node;
  51. local function register_node(name, def2)
  52. local def = table.copy(def2)
  53. -- Make sure groups table exists (even if its empty).
  54. if not def.groups then def.groups = {} end
  55. if not def.groups.fall_damage_add_percent then
  56. def.groups.fall_damage_add_percent = 30
  57. end
  58. -- Any nodes dealing env damage get added to the 'env_damage' group.
  59. if def.damage_per_second ~= 0 then
  60. def.groups.env_damage = 1
  61. end
  62. -- Any airlike drawtype nodes get added to the 'airlike' group.
  63. -- Note: this includes any airlike drawtype nodes from the maptools files.
  64. if def.drawtype == "airlike" then
  65. def.groups.airlike = 1
  66. end
  67. -- Any nodes with "brick" or "block" in the name have dig prediction disabled.
  68. -- This makes them "glitch proof" to normal clients.
  69. if name:find("brick$") or name:find("block$") then
  70. if not def.node_dig_prediction then
  71. def.node_dig_prediction = ""
  72. end
  73. end
  74. if not def.movement_speed_multiplier then
  75. if def.drawtype == "nodebox" or def.drawtype == "mesh" then
  76. if not string.find(name, "^vines:") then
  77. def.movement_speed_multiplier = default.SLOW_SPEED
  78. end
  79. end
  80. end
  81. if type(def.stack_max) == "nil" then
  82. def.stack_max = 64
  83. end
  84. -- Every node that overrides 'on_punch' must have its 'on_punch'
  85. -- handler wrapped in one that calls punchnode callbacks.
  86. if def.on_punch then
  87. local on_punch = def.on_punch
  88. def.on_punch = function(pos, node, puncher, pointed_thing)
  89. -- Run script hook
  90. for _, callback in ipairs(core.registered_on_punchnodes) do
  91. -- Copy pos and node because callback can modify them
  92. local pos_copy = vector.new(pos)
  93. local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
  94. local pointed_thing_copy = pointed_thing and copy_pointed_thing(pointed_thing) or nil
  95. callback(pos_copy, node_copy, puncher, pointed_thing_copy)
  96. end
  97. return on_punch(pos, node, puncher, pointed_thing)
  98. end
  99. end
  100. -- If the node defines 'can_dig' then we must create a wrapper
  101. -- that calls 'minetest.is_protected' if that function returns false.
  102. -- This is because the engine will skip the protection check in core.
  103. if def.can_dig then
  104. local can_dig = def.can_dig
  105. function def.can_dig(pos, digger)
  106. local result = can_dig(pos, digger) -- Call old function.
  107. if not result then
  108. -- Old function returned false, we must check protection (because MT core will not do this).
  109. local pname = ""
  110. if digger and digger:is_player() then
  111. pname = digger:get_player_name()
  112. end
  113. if minetest.test_protection(pos, pname) then
  114. protector.punish_player(pos, pname)
  115. end
  116. end
  117. return result
  118. -- If the old function returned true (i.e., player can dig)
  119. -- the MT core will follow up with a protection check.
  120. end
  121. end
  122. if type(def.tiles) == "table" then
  123. for k, v in pairs(def.tiles) do
  124. if type(v) == "string" then
  125. def.tiles[k] = image.get(v)
  126. end
  127. end
  128. end
  129. if type(def.inventory_image) == "string" then
  130. def.inventory_image = image.get(def.inventory_image)
  131. end
  132. if type(def.wield_image) == "string" then
  133. def.wield_image = image.get(def.wield_image)
  134. end
  135. if def.groups.notify_construct and def.groups.notify_construct > 0 then
  136. if def.on_construct then
  137. local old = def.on_construct
  138. def.on_construct = function(pos)
  139. notify.notify_adjacent(pos)
  140. return old(pos)
  141. end
  142. else
  143. def.on_construct = function(pos)
  144. notify.notify_adjacent(pos)
  145. end
  146. end
  147. end
  148. if def.groups.notify_destruct and def.groups.notify_destruct > 0 then
  149. if def.on_destruct then
  150. local old = def.on_destruct
  151. def.on_destruct = function(pos)
  152. notify.notify_adjacent(pos)
  153. return old(pos)
  154. end
  155. else
  156. def.on_destruct = function(pos)
  157. notify.notify_adjacent(pos)
  158. end
  159. end
  160. end
  161. falldamage.apply_range_checks(def)
  162. falldamage.apply_liquid_interaction_mod(name, def)
  163. if def.sounds then
  164. assert(type(def.sounds) == "table")
  165. end
  166. old_register_node(name, def)
  167. -- Populate table of all non-stair nodes.
  168. if not name:find("^%:?stairs:") then
  169. local first, second = name:match("^%:?([%w_]+)%:([%w_]+)$")
  170. local n = first .. ":" .. second
  171. local def = minetest.registered_nodes[n]
  172. minetest.reg_ns_nodes[n] = def
  173. end
  174. end
  175. minetest.register_node = register_node
  176. -- Make sure our custom node tables contain entries for air and ignore.
  177. minetest.reg_ns_nodes["air"] = minetest.registered_nodes["air"]
  178. minetest.reg_ns_nodes["ignore"] = minetest.registered_nodes["ignore"]