init.lua 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. if not minetest.global_exists("bonemeal") then bonemeal = {} end
  2. bonemeal.modpath = minetest.get_modpath("bonemeal")
  3. -- Localize for performance.
  4. local math_random = math.random
  5. function bonemeal.do_dirtspread(pos)
  6. local above = minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z})
  7. if above.name ~= "air" then
  8. return
  9. end
  10. local dirt = minetest.find_node_near(pos, 2, {
  11. "default:dirt_with_grass",
  12. "default:dirt_with_dry_grass",
  13. "moregrass:darkgrass",
  14. })
  15. if dirt then
  16. local node = minetest.get_node(dirt)
  17. minetest.add_node(pos, {name=node.name})
  18. end
  19. end
  20. function bonemeal.particles(pos)
  21. local p = vector.add(pos, {x=0, y=0.5, z=0})
  22. local ndef = minetest.registered_nodes[minetest.get_node(pos).name]
  23. if ndef.drawtype == "plantlike" then
  24. p = vector.add(pos, {x=0, y=-0.4, z=0})
  25. end
  26. minetest.add_particlespawner({
  27. amount = 5,
  28. time = 0.3,
  29. minpos = {x = p.x - 0.5, y = p.y + 0.1, z = p.z - 0.5 },
  30. maxpos = {x = p.x + 0.5, y = p.y + 0.2, z = p.z + 0.5 },
  31. minvel = {x = -0.2, y = 0.1, z = -0.2},
  32. maxvel = {x = 0.2, y = 0.2, z = 0.2},
  33. minacc = {x = -0.15, y = -0.02, z = -0.15},
  34. maxacc = {x = 0.15, y = -0.01, z = 0.15},
  35. minexptime = 0.5,
  36. maxexptime = 0.8,
  37. minsize = 1.5,
  38. maxsize = 2.0,
  39. collisiondetection = true,
  40. texture = "bonemeal_particle.png",
  41. glow = 5,
  42. })
  43. ambiance.sound_play("default_dirt_footstep", pos, 1.0, 16)
  44. end
  45. function bonemeal.on_use(itemstack, user, pt)
  46. if not user or not user:is_player() then
  47. return
  48. end
  49. if pt.type == "node" then
  50. local pos = pt.under
  51. if minetest.is_protected(pos, user:get_player_name()) then
  52. return
  53. end
  54. local node = minetest.get_node(pos)
  55. local def = minetest.registered_items[node.name]
  56. local take = false
  57. if def then
  58. -- Note: prefer to define this callback on your node whenever possible!
  59. -- Hardcoding bonemeal function here is NOT a good idea.
  60. if def._on_bonemeal_use then
  61. local itemcount = itemstack:get_count()
  62. local itemname = itemstack:get_name()
  63. local itemstack = def._on_bonemeal_use(pos, node, user, itemstack)
  64. if itemstack then
  65. local newcount = itemstack:get_count()
  66. if newcount < itemcount and itemstack:get_name() == itemname then
  67. bonemeal.particles(pos)
  68. end
  69. return itemstack
  70. end
  71. elseif def._farming_next_plant and def.on_timer then
  72. local timer = minetest.get_node_timer(pos)
  73. if timer:is_started() then
  74. local timeout = timer:get_timeout()
  75. local elapsed = timer:get_elapsed()
  76. local remain = (timeout - elapsed)
  77. if remain > 0 then
  78. -- Plant growtime is reduced by 2 thirds.
  79. local newtime = (remain / 3)
  80. timer:set(newtime, elapsed)
  81. end
  82. else -- Timer not running?
  83. --minetest.log('not running')
  84. farming.restart_timer(pos)
  85. end
  86. take = true
  87. elseif def.groups and def.groups.flora and def.groups.flora > 0 then
  88. if math_random(1, 3) == 1 then
  89. flowers.flower_spread(pos, node)
  90. end
  91. take = true
  92. elseif node.name == "flowers:mushroom_brown" or
  93. node.name == "flowers:mushroom_red" or
  94. node.name == "cavestuff:mycena" or
  95. node.name == "cavestuff:fungus" then
  96. if math_random(1, 3) == 1 then
  97. flowers.mushroom_spread(pos, node)
  98. end
  99. take = true
  100. elseif node.name == "default:cactus" then
  101. if math_random(1, 3) == 1 then
  102. cactus.grow(pos, node)
  103. end
  104. take = true
  105. elseif node.name == "default:papyrus" then
  106. if math_random(1, 3) == 1 then
  107. papyrus.grow(pos, node)
  108. end
  109. take = true
  110. elseif node.name == "default:tvine" or
  111. node.name == "default:tvine_alt" or
  112. node.name == "default:tvine_top" or
  113. node.name == "default:tvine_top_alt" then
  114. if math_random(1, 3) == 1 then
  115. tvine.grow(pos, node)
  116. end
  117. take = true
  118. elseif def.groups and def.groups.sapling and def.groups.sapling > 0 and def.on_timer then
  119. local timer = minetest.get_node_timer(pos)
  120. if timer:is_started() then
  121. local timeout = timer:get_timeout()
  122. local elapsed = timer:get_elapsed()
  123. local remain = (timeout - elapsed)
  124. if remain > 0 then
  125. local newtime = (remain / 3)*2
  126. timer:set(newtime, elapsed)
  127. end
  128. end
  129. take = true
  130. elseif node.name == "nethervine:vine" then
  131. if math_random(1, 3) == 1 then
  132. nethervine.grow(pos, node)
  133. end
  134. take = true
  135. elseif node.name == "default:dirt" then
  136. if math_random(1, 3) == 1 then
  137. bonemeal.do_dirtspread(pos)
  138. end
  139. take = true
  140. elseif node.name == "default:dirt_with_dry_grass" then
  141. if math_random(1, 2) == 1 then
  142. local above = {x=pos.x, y=pos.y+1, z=pos.z}
  143. local anode = minetest.get_node(above)
  144. if anode.name == "air" then
  145. if not minetest.is_protected(above, user:get_player_name()) then
  146. if math_random(1, 4) > 1 then
  147. minetest.add_node(above, {name="default:dry_grass_" .. math_random(1, 5), param2=2})
  148. else
  149. local name = "default:dry_shrub"
  150. if math_random(1, 2) == 1 then
  151. name = "default:dry_shrub2"
  152. end
  153. minetest.add_node(above, {name=name})
  154. end
  155. end
  156. end
  157. end
  158. take = true
  159. elseif node.name == "default:dirt_with_grass" then
  160. if math_random(1, 2) == 1 then
  161. local above = {x=pos.x, y=pos.y+1, z=pos.z}
  162. local anode = minetest.get_node(above)
  163. if anode.name == "air" then
  164. if not minetest.is_protected(above, user:get_player_name()) then
  165. if math_random(1, 2) == 1 then
  166. minetest.add_node(above, {name="default:grass_" .. math_random(1, 5), param2=2})
  167. else
  168. minetest.add_node(above, {name="default:coarsegrass", param2=2})
  169. end
  170. end
  171. end
  172. end
  173. take = true
  174. elseif node.name == "moregrass:darkgrass" then
  175. if math_random(1, 2) == 1 then
  176. local above = {x=pos.x, y=pos.y+1, z=pos.z}
  177. local anode = minetest.get_node(above)
  178. if anode.name == "air" then
  179. if not minetest.is_protected(above, user:get_player_name()) then
  180. if math_random(1, 2) == 1 then
  181. minetest.add_node(above, {name="default:junglegrass", param2=2})
  182. else
  183. minetest.add_node(above, {name="default:coarsegrass", param2=2})
  184. end
  185. end
  186. end
  187. end
  188. take = true
  189. elseif string.find(node.name, "^nether:grass_%d$") then
  190. if math_random(1, 2) == 1 then
  191. if not minetest.is_protected(pos, user:get_player_name()) then
  192. nethervine.flora_spread(pos, minetest.get_node(pos))
  193. end
  194. end
  195. take = true
  196. end
  197. end
  198. if take then
  199. bonemeal.particles(pos)
  200. itemstack:take_item()
  201. end
  202. return itemstack
  203. end
  204. end
  205. if not bonemeal.run_once then
  206. -- bone item
  207. minetest.register_craftitem("bonemeal:bone", {
  208. description = "Bone",
  209. inventory_image = "bone_bone.png",
  210. })
  211. minetest.register_craft({
  212. output = "bonemeal:bone 16",
  213. recipe = {
  214. {'bones:bones_type2', 'bones:bones_type2'},
  215. {'bones:bones_type2', 'bones:bones_type2'},
  216. },
  217. })
  218. minetest.register_craft({
  219. output = "bones:bones_type2",
  220. recipe = {
  221. {'bonemeal:bone', 'bonemeal:bone'},
  222. {'bonemeal:bone', 'bonemeal:bone'},
  223. },
  224. })
  225. -- bonemeal recipe
  226. minetest.register_craft({
  227. output = 'bonemeal:meal 3',
  228. recipe = {{'bonemeal:bone'}},
  229. })
  230. minetest.register_craft({
  231. type = "anvil",
  232. output = 'bonemeal:meal 5',
  233. recipe = 'bonemeal:bone',
  234. })
  235. minetest.register_craft({
  236. type = "grinding",
  237. output = 'bonemeal:meal 6',
  238. recipe = 'bonemeal:bone',
  239. time = 4,
  240. })
  241. -- bonemeal item
  242. minetest.register_craftitem("bonemeal:meal", {
  243. description = "Bone Meal",
  244. inventory_image = "bone_bonemeal.png",
  245. liquids_pointable = false,
  246. on_use = function(...)
  247. return bonemeal.on_use(...)
  248. end,
  249. })
  250. local c = "bonemeal:core"
  251. local f = bonemeal.modpath .. "/init.lua"
  252. reload.register_file(c, f, false)
  253. bonemeal.run_once = true
  254. end