init.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. if not minetest.global_exists("redshroom") then redshroom = {} end
  2. redshroom.modpath = minetest.get_modpath("redshroom")
  3. -- Localize for performance.
  4. local math_random = math.random
  5. local SHROOM_SCHEMATICS = {
  6. "redshroom_shroom1.mts",
  7. "redshroom_shroom2.mts",
  8. "redshroom_shroom3.mts",
  9. "redshroom_shroom4.mts",
  10. "redshroom_shroom5.mts",
  11. "redshroom_shroom6.mts",
  12. }
  13. function redshroom.can_grow(pos)
  14. -- Nether shroom does not grow in other dimensions.
  15. local crealm = rc.current_realm_at_pos(pos)
  16. if not (crealm == "overworld" or crealm == "naraxen") then
  17. return
  18. end
  19. -- The seed (fungus spore) hangs below stable soil.
  20. local p2 = vector.offset(pos, 0, 1, 0)
  21. local p3 = vector.offset(pos, 0, 2, 0)
  22. local n2 = minetest.get_node(p2) -- Should be soil.
  23. local n3 = minetest.get_node(p3) -- Should be air.
  24. --minetest.chat_send_all('name1: ' .. n2.name)
  25. --minetest.chat_send_all('name2: ' .. n3.name)
  26. -- Cannot grow if surface node is covered.
  27. if n3.name ~= "air" then
  28. --minetest.chat_send_all('no surface')
  29. return
  30. end
  31. -- Must have nether soil.
  32. if minetest.get_item_group(n2.name, "nether_soil") == 0 then
  33. --minetest.chat_send_all('no soil')
  34. return
  35. end
  36. -- Must not be too bright.
  37. if (minetest.get_node_light(p3, 0.5) or 0) > 10 then
  38. --minetest.chat_send_all('too bright')
  39. return
  40. end
  41. -- Reduced chance to grow if cold/ice nearby.
  42. local cold = minetest.find_nodes_in_area(vector.subtract(pos, 3), vector.add(pos, 3), "group:cold")
  43. if #cold > math.random(0, 196) then
  44. --minetest.chat_send_all('too cold')
  45. return
  46. end
  47. --minetest.chat_send_all('can grow')
  48. return true
  49. end
  50. function redshroom.on_construct(pos)
  51. --minetest.chat_send_all('on_construct')
  52. local timer = minetest.get_node_timer(pos)
  53. timer:start(math.random(60*10, 60*20))
  54. end
  55. function redshroom.on_timer(pos, elapsed)
  56. --minetest.chat_send_all('on_timer')
  57. if redshroom.can_grow(pos) then
  58. redshroom.create_shroom(vector.offset(pos, 0, 2, 0))
  59. minetest.remove_node(pos)
  60. return
  61. end
  62. -- Run timer again with the same timeout.
  63. return true
  64. end
  65. function redshroom.on_bonemeal_use(pos, node, user, itemstack)
  66. local timer = minetest.get_node_timer(pos)
  67. if timer:is_started() then
  68. local timeout = timer:get_timeout()
  69. local elapsed = timer:get_elapsed()
  70. local remain = (timeout - elapsed)
  71. if remain > 0 then
  72. -- Plant growtime is reduced by 2 thirds.
  73. local newtime = (remain / 3)
  74. timer:set(newtime, elapsed)
  75. end
  76. end
  77. itemstack:take_item()
  78. return itemstack
  79. end
  80. redshroom.create_shroom_on_vmanip = function(vm, pos)
  81. local schempath = redshroom.modpath .. "/schematics/"
  82. local path = schempath .. SHROOM_SCHEMATICS[math_random(#SHROOM_SCHEMATICS)]
  83. minetest.place_schematic_on_vmanip(vm, vector.add(pos, {x=-2, y=0, z=-2}), path, "random", nil, false)
  84. end
  85. redshroom.create_shroom = function(pos)
  86. local schempath = redshroom.modpath .. "/schematics/"
  87. local path = schempath .. SHROOM_SCHEMATICS[math_random(#SHROOM_SCHEMATICS)]
  88. minetest.place_schematic(vector.add(pos, {x=-2, y=0, z=-2}), path, "random", nil, false)
  89. end
  90. if not redshroom.registered then
  91. minetest.register_node("redshroom:stemwhite", {
  92. description = "White Shroom Stem",
  93. tiles = {"redshroom_stemtop_white.png", "redshroom_stemtop_white.png", "redshroom_stemside_white.png"},
  94. drawtype = "nodebox",
  95. paramtype2 = "facedir",
  96. paramtype = "light",
  97. groups = utility.dig_groups("shroom", {flammable=2}),
  98. sounds = default.node_sound_wood_defaults(),
  99. on_place = minetest.rotate_node,
  100. node_box = {
  101. type = "fixed",
  102. fixed = basictrees.trunk_nodebox,
  103. },
  104. })
  105. minetest.register_node("redshroom:gills", {
  106. description = "Shroom Gills",
  107. drawtype = "plantlike",
  108. tiles = {"redshroom_gills.png"},
  109. paramtype = "light",
  110. groups = utility.dig_groups("plant", {flammable=2, hanging_node=1}),
  111. drop = "", -- Gills are destroyed when dug.
  112. shears_drop = true, -- obtainable via shears
  113. walkable = false,
  114. buildable_to = true,
  115. sounds = default.node_sound_leaves_defaults(),
  116. selection_box = {
  117. type = "fixed",
  118. fixed = {-0.5, 5/16, -0.5, 0.5, 0.5, 0.5},
  119. },
  120. movement_speed_multiplier = default.SLOW_SPEED_PLANTS,
  121. on_construct = function(...)
  122. return redshroom.on_construct(...)
  123. end,
  124. on_timer = function(...)
  125. return redshroom.on_timer(...)
  126. end,
  127. _on_bonemeal_use = function(...)
  128. return redshroom.on_bonemeal_use(...)
  129. end,
  130. })
  131. minetest.register_craft({
  132. type = "shapeless",
  133. output = "default:stick 16",
  134. recipe = {"redshroom:stemwhite"},
  135. })
  136. minetest.register_craft({
  137. type = "cooking",
  138. output = "default:coal_lump 4",
  139. recipe = "redshroom:stemwhite",
  140. cooktime = 60,
  141. })
  142. minetest.register_node("redshroom:head2", {
  143. description = "Red Shroom Head",
  144. tiles = {"redshroom_headtop.png", "redshroom_headtop.png", "redshroom_headside1.png"},
  145. paramtype2 = "facedir",
  146. groups = utility.dig_groups("shroom", {flammable=2}),
  147. sounds = default.node_sound_wood_defaults(),
  148. on_place = minetest.rotate_node
  149. })
  150. minetest.register_node("redshroom:stem", {
  151. description = "Red Shroom Stem",
  152. tiles = {"redshroom_stemtop.png", "redshroom_stemtop.png", "redshroom_stemside.png"},
  153. drawtype = "nodebox",
  154. paramtype2 = "facedir",
  155. paramtype = "light",
  156. groups = utility.dig_groups("shroom", {flammable=2}),
  157. sounds = default.node_sound_wood_defaults(),
  158. on_place = minetest.rotate_node,
  159. node_box = {
  160. type = "fixed",
  161. fixed = basictrees.trunk_nodebox,
  162. },
  163. })
  164. minetest.register_craft({
  165. type = "shapeless",
  166. output = "default:stick 16",
  167. recipe = {"redshroom:stem"},
  168. })
  169. minetest.register_node("redshroom:head", {
  170. description = "Red Shroom Head",
  171. tiles = {"redshroom_headtop.png", "redshroom_headtop.png", "redshroom_headside1.png^redshroom_headside2.png"},
  172. paramtype2 = "facedir",
  173. groups = utility.dig_groups("shroom", {flammable=2}),
  174. sounds = default.node_sound_wood_defaults(),
  175. on_place = minetest.rotate_node
  176. })
  177. local c = "redshroom:core"
  178. local f = redshroom.modpath .. "/init.lua"
  179. reload.register_file(c, f, false)
  180. redshroom.registered = true
  181. end