init.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. --[[
  2. Copyright 2018 Noodlemire
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. --]]
  13. clumpfall = {} --global variable
  14. -- Localize for performance.
  15. local math_random = math.random
  16. --the maximum radius of blocks to cause to fall at once.
  17. clumpfall.clump_radius = 1
  18. --Short for modpath, this stores this really long but automatic modpath get
  19. local mp = minetest.get_modpath(minetest.get_current_modname()) .. "/"
  20. --Load other lua components
  21. dofile(mp.."functions.lua")
  22. dofile(mp.."override.lua")
  23. function clumpfall.update_nodedef(name, def)
  24. def.groups = def.groups or {}
  25. local get_group = function(grp)
  26. if def.groups[grp] then
  27. return def.groups[grp]
  28. end
  29. return 0
  30. end
  31. if name ~= "air" and name ~= "ignore" and
  32. get_group("falling_node") == 0 and --Don't need to affect nodes that already fall by themselves
  33. get_group("attached_node") == 0 and --Same thing for nodes in this group, which fall when no longer attached to another node
  34. get_group("liquid") == 0 and --Same thing for nodes in this group, which do technically fall and spread around
  35. get_group("immovable") == 0 and
  36. get_group("leaves") == 0 and
  37. get_group("always_stable") == 0 and
  38. get_group("disable_clump_fall") == 0 and
  39. get_group("unbreakable") == 0 then --Lastly, if a block is invulnerable to begin with, it shouldn't fall down like a typical node
  40. def.groups.clump_fall_node = 1
  41. else
  42. def.groups.clump_fall_node = 0
  43. end
  44. local do_clump_fall = clumpfall.functions.do_clump_fall
  45. ---[[
  46. local old_on_dig = def.on_dig
  47. function def.on_dig(pos, node, digger)
  48. if old_on_dig ~= nil then
  49. old_on_dig(pos, node, digger)
  50. else
  51. -- Execute MT default function.
  52. core.node_dig(pos, node, digger)
  53. end
  54. if pos.y > 1000 then
  55. do_clump_fall(pos)
  56. end
  57. end
  58. --]]
  59. local old_after_place_node = def.after_place_node
  60. function def.after_place_node(pos, placer, itemstack, pt)
  61. local r
  62. if old_after_place_node ~= nil then
  63. r = old_after_place_node(pos, placer, itemstack, pt)
  64. end
  65. if pos.y > 1000 then
  66. minetest.after(math_random(1, 10), function()
  67. do_clump_fall(pos)
  68. end)
  69. end
  70. return r
  71. end
  72. ---[[
  73. local old_on_punch = def.on_punch
  74. function def.on_punch(pos, node, puncher, pt)
  75. local r
  76. if old_on_punch ~= nil then
  77. r = old_on_punch(pos, node, puncher, pt)
  78. else
  79. r = core.node_punch(pos, node, puncher, pt)
  80. end
  81. if pos.y > 1000 then
  82. do_clump_fall(pos)
  83. end
  84. return r
  85. end
  86. --]]
  87. local old_on_place = def.on_place
  88. function def.on_place(itemstack, placer, pt)
  89. --minetest.chat_send_player("MustTest", "Test1")
  90. local a, b, c
  91. if old_on_place ~= nil then
  92. --minetest.chat_send_player("MustTest", "Test2")
  93. a, b, c = old_on_place(itemstack, placer, pt)
  94. else
  95. --minetest.chat_send_player("MustTest", "Test3")
  96. a, b, c = core.item_place(itemstack, placer, pt)
  97. end
  98. --minetest.chat_send_player("MustTest", "Test4")
  99. if b and c and c.y > 1000 then
  100. --minetest.chat_send_player("MustTest", "Test5")
  101. minetest.after(math_random(1, 10), function()
  102. --minetest.chat_send_player("MustTest", "Test6")
  103. do_clump_fall(c)
  104. end)
  105. elseif pt.type == "node" and pt.under and pt.above then
  106. minetest.after(math_random(1, 10), function()
  107. do_clump_fall(pt.under)
  108. do_clump_fall(pt.above)
  109. end)
  110. end
  111. return a, b, c
  112. end
  113. end