init.lua 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. -- flowers/init.lua
  2. -- Minetest 0.4 mod: default
  3. -- See README.txt for licensing and other information.
  4. -- Namespace for functions
  5. flowers = {}
  6. -- Load support for MT game translation.
  7. local S = minetest.get_translator("flowers")
  8. -- Map Generation
  9. dofile(minetest.get_modpath("flowers") .. "/mapgen.lua")
  10. --
  11. -- Flowers
  12. --
  13. -- Aliases for original flowers mod
  14. minetest.register_alias("flowers:flower_rose", "flowers:rose")
  15. minetest.register_alias("flowers:flower_tulip", "flowers:tulip")
  16. minetest.register_alias("flowers:flower_dandelion_yellow", "flowers:dandelion_yellow")
  17. minetest.register_alias("flowers:flower_geranium", "flowers:geranium")
  18. minetest.register_alias("flowers:flower_viola", "flowers:viola")
  19. minetest.register_alias("flowers:flower_dandelion_white", "flowers:dandelion_white")
  20. -- Flower registration
  21. local function add_simple_flower(name, desc, box, f_groups)
  22. -- Common flowers' groups
  23. f_groups.snappy = 3
  24. f_groups.flower = 1
  25. f_groups.flora = 1
  26. f_groups.attached_node = 1
  27. minetest.register_node("flowers:" .. name, {
  28. description = desc,
  29. drawtype = "plantlike",
  30. waving = 1,
  31. tiles = {"flowers_" .. name .. ".png"},
  32. inventory_image = "flowers_" .. name .. ".png",
  33. wield_image = "flowers_" .. name .. ".png",
  34. sunlight_propagates = true,
  35. paramtype = "light",
  36. walkable = false,
  37. buildable_to = true,
  38. groups = f_groups,
  39. sounds = default.node_sound_leaves_defaults(),
  40. selection_box = {
  41. type = "fixed",
  42. fixed = box
  43. }
  44. })
  45. end
  46. flowers.datas = {
  47. {
  48. "rose",
  49. S("Red Rose"),
  50. {-2 / 16, -0.5, -2 / 16, 2 / 16, 5 / 16, 2 / 16},
  51. {color_red = 1, flammable = 1}
  52. },
  53. {
  54. "tulip",
  55. S("Orange Tulip"),
  56. {-2 / 16, -0.5, -2 / 16, 2 / 16, 3 / 16, 2 / 16},
  57. {color_orange = 1, flammable = 1}
  58. },
  59. {
  60. "dandelion_yellow",
  61. S("Yellow Dandelion"),
  62. {-4 / 16, -0.5, -4 / 16, 4 / 16, -2 / 16, 4 / 16},
  63. {color_yellow = 1, flammable = 1}
  64. },
  65. {
  66. "chrysanthemum_green",
  67. S("Green Chrysanthemum"),
  68. {-4 / 16, -0.5, -4 / 16, 4 / 16, -1 / 16, 4 / 16},
  69. {color_green = 1, flammable = 1}
  70. },
  71. {
  72. "geranium",
  73. S("Blue Geranium"),
  74. {-2 / 16, -0.5, -2 / 16, 2 / 16, 2 / 16, 2 / 16},
  75. {color_blue = 1, flammable = 1}
  76. },
  77. {
  78. "viola",
  79. S("Viola"),
  80. {-5 / 16, -0.5, -5 / 16, 5 / 16, -1 / 16, 5 / 16},
  81. {color_violet = 1, flammable = 1}
  82. },
  83. {
  84. "dandelion_white",
  85. S("White Dandelion"),
  86. {-5 / 16, -0.5, -5 / 16, 5 / 16, -2 / 16, 5 / 16},
  87. {color_white = 1, flammable = 1}
  88. },
  89. {
  90. "tulip_black",
  91. S("Black Tulip"),
  92. {-2 / 16, -0.5, -2 / 16, 2 / 16, 3 / 16, 2 / 16},
  93. {color_black = 1, flammable = 1}
  94. },
  95. }
  96. for _,item in pairs(flowers.datas) do
  97. add_simple_flower(unpack(item))
  98. end
  99. -- Flower spread
  100. -- Public function to enable override by mods
  101. function flowers.flower_spread(pos, node)
  102. pos.y = pos.y - 1
  103. local under = minetest.get_node(pos)
  104. pos.y = pos.y + 1
  105. -- Replace flora with dry shrub in desert sand and silver sand,
  106. -- as this is the only way to generate them.
  107. -- However, preserve grasses in sand dune biomes.
  108. if minetest.get_item_group(under.name, "sand") == 1 and
  109. under.name ~= "default:sand" then
  110. minetest.set_node(pos, {name = "default:dry_shrub"})
  111. return
  112. end
  113. if minetest.get_item_group(under.name, "soil") == 0 then
  114. return
  115. end
  116. local light = minetest.get_node_light(pos)
  117. if not light or light < 13 then
  118. return
  119. end
  120. local pos0 = vector.subtract(pos, 4)
  121. local pos1 = vector.add(pos, 4)
  122. -- Testing shows that a threshold of 3 results in an appropriate maximum
  123. -- density of approximately 7 flora per 9x9 area.
  124. if #minetest.find_nodes_in_area(pos0, pos1, "group:flora") > 3 then
  125. return
  126. end
  127. local soils = minetest.find_nodes_in_area_under_air(
  128. pos0, pos1, "group:soil")
  129. local num_soils = #soils
  130. if num_soils >= 1 then
  131. for si = 1, math.min(3, num_soils) do
  132. local soil = soils[math.random(num_soils)]
  133. local soil_name = minetest.get_node(soil).name
  134. local soil_above = {x = soil.x, y = soil.y + 1, z = soil.z}
  135. light = minetest.get_node_light(soil_above)
  136. if light and light >= 13 and
  137. -- Only spread to same surface node
  138. soil_name == under.name and
  139. -- Desert sand is in the soil group
  140. soil_name ~= "default:desert_sand" then
  141. minetest.set_node(soil_above, {name = node.name})
  142. end
  143. end
  144. end
  145. end
  146. minetest.register_abm({
  147. label = "Flower spread",
  148. nodenames = {"group:flora"},
  149. interval = 13,
  150. chance = 300,
  151. action = function(...)
  152. flowers.flower_spread(...)
  153. end,
  154. })
  155. --
  156. -- Mushrooms
  157. --
  158. minetest.register_node("flowers:mushroom_red", {
  159. description = S("Red Mushroom"),
  160. tiles = {"flowers_mushroom_red.png"},
  161. inventory_image = "flowers_mushroom_red.png",
  162. wield_image = "flowers_mushroom_red.png",
  163. drawtype = "plantlike",
  164. paramtype = "light",
  165. sunlight_propagates = true,
  166. walkable = false,
  167. buildable_to = true,
  168. groups = {mushroom = 1, snappy = 3, attached_node = 1, flammable = 1},
  169. sounds = default.node_sound_leaves_defaults(),
  170. on_use = minetest.item_eat(-5),
  171. selection_box = {
  172. type = "fixed",
  173. fixed = {-4 / 16, -0.5, -4 / 16, 4 / 16, -1 / 16, 4 / 16},
  174. }
  175. })
  176. minetest.register_node("flowers:mushroom_brown", {
  177. description = S("Brown Mushroom"),
  178. tiles = {"flowers_mushroom_brown.png"},
  179. inventory_image = "flowers_mushroom_brown.png",
  180. wield_image = "flowers_mushroom_brown.png",
  181. drawtype = "plantlike",
  182. paramtype = "light",
  183. sunlight_propagates = true,
  184. walkable = false,
  185. buildable_to = true,
  186. groups = {mushroom = 1, food_mushroom = 1, snappy = 3, attached_node = 1, flammable = 1},
  187. sounds = default.node_sound_leaves_defaults(),
  188. on_use = minetest.item_eat(1),
  189. selection_box = {
  190. type = "fixed",
  191. fixed = {-3 / 16, -0.5, -3 / 16, 3 / 16, -2 / 16, 3 / 16},
  192. }
  193. })
  194. -- Mushroom spread and death
  195. function flowers.mushroom_spread(pos, node)
  196. if minetest.get_node_light(pos, 0.5) > 3 then
  197. if minetest.get_node_light(pos, nil) == 15 then
  198. minetest.remove_node(pos)
  199. end
  200. return
  201. end
  202. local positions = minetest.find_nodes_in_area_under_air(
  203. {x = pos.x - 1, y = pos.y - 2, z = pos.z - 1},
  204. {x = pos.x + 1, y = pos.y + 1, z = pos.z + 1},
  205. {"group:soil", "group:tree"})
  206. if #positions == 0 then
  207. return
  208. end
  209. local pos2 = positions[math.random(#positions)]
  210. pos2.y = pos2.y + 1
  211. if minetest.get_node_light(pos2, 0.5) <= 3 then
  212. minetest.set_node(pos2, {name = node.name})
  213. end
  214. end
  215. minetest.register_abm({
  216. label = "Mushroom spread",
  217. nodenames = {"flowers:mushroom_brown", "flowers:mushroom_red"},
  218. interval = 11,
  219. chance = 150,
  220. action = function(...)
  221. flowers.mushroom_spread(...)
  222. end,
  223. })
  224. -- These old mushroom related nodes can be simplified now
  225. minetest.register_alias("flowers:mushroom_spores_brown", "flowers:mushroom_brown")
  226. minetest.register_alias("flowers:mushroom_spores_red", "flowers:mushroom_red")
  227. minetest.register_alias("flowers:mushroom_fertile_brown", "flowers:mushroom_brown")
  228. minetest.register_alias("flowers:mushroom_fertile_red", "flowers:mushroom_red")
  229. minetest.register_alias("mushroom:brown_natural", "flowers:mushroom_brown")
  230. minetest.register_alias("mushroom:red_natural", "flowers:mushroom_red")
  231. --
  232. -- Waterlily
  233. --
  234. local waterlily_def = {
  235. description = S("Waterlily"),
  236. drawtype = "nodebox",
  237. paramtype = "light",
  238. paramtype2 = "facedir",
  239. tiles = {"flowers_waterlily.png", "flowers_waterlily_bottom.png"},
  240. inventory_image = "flowers_waterlily.png",
  241. wield_image = "flowers_waterlily.png",
  242. liquids_pointable = true,
  243. walkable = false,
  244. buildable_to = true,
  245. floodable = true,
  246. groups = {snappy = 3, flower = 1, flammable = 1},
  247. sounds = default.node_sound_leaves_defaults(),
  248. node_placement_prediction = "",
  249. node_box = {
  250. type = "fixed",
  251. fixed = {-0.5, -31 / 64, -0.5, 0.5, -15 / 32, 0.5}
  252. },
  253. selection_box = {
  254. type = "fixed",
  255. fixed = {-7 / 16, -0.5, -7 / 16, 7 / 16, -15 / 32, 7 / 16}
  256. },
  257. on_place = function(itemstack, placer, pointed_thing)
  258. local pos = pointed_thing.above
  259. local node = minetest.get_node(pointed_thing.under)
  260. local def = minetest.registered_nodes[node.name]
  261. if def and def.on_rightclick then
  262. return def.on_rightclick(pointed_thing.under, node, placer, itemstack,
  263. pointed_thing)
  264. end
  265. if def and def.liquidtype == "source" and
  266. minetest.get_item_group(node.name, "water") > 0 then
  267. local player_name = placer and placer:get_player_name() or ""
  268. if not minetest.is_protected(pos, player_name) then
  269. minetest.set_node(pos, {name = "flowers:waterlily" ..
  270. (def.waving == 3 and "_waving" or ""),
  271. param2 = math.random(0, 3)})
  272. if not (creative and creative.is_enabled_for
  273. and creative.is_enabled_for(player_name)) then
  274. itemstack:take_item()
  275. end
  276. else
  277. minetest.chat_send_player(player_name, "Node is protected")
  278. minetest.record_protection_violation(pos, player_name)
  279. end
  280. end
  281. return itemstack
  282. end
  283. }
  284. local waterlily_waving_def = table.copy(waterlily_def)
  285. waterlily_waving_def.waving = 3
  286. waterlily_waving_def.drop = "flowers:waterlily"
  287. waterlily_waving_def.groups.not_in_creative_inventory = 1
  288. minetest.register_node("flowers:waterlily", waterlily_def)
  289. minetest.register_node("flowers:waterlily_waving", waterlily_waving_def)