init.lua 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. -- Plantlife library mod by Vanessa Ezekowitz
  2. -- last revision, 2013-01-24
  3. --
  4. -- License: WTFPL
  5. --
  6. -- I got the temperature map idea from "hmmmm", values used for it came from
  7. -- Splizard's snow mod.
  8. --
  9. -- Various settings - most of these probably won't need to be changed
  10. plantslib = {}
  11. local DEBUG = false --... except if you want to spam the console with debugging info :-)
  12. plantslib.plantlife_seed_diff = 329 -- needs to be global so other mods can see it
  13. local perlin_octaves = 3
  14. local perlin_persistence = 0.6
  15. local perlin_scale = 100
  16. local temperature_seeddiff = 112
  17. local temperature_octaves = 3
  18. local temperature_persistence = 0.5
  19. local temperature_scale = 150
  20. local humidity_seeddiff = 9130
  21. local humidity_octaves = 3
  22. local humidity_persistence = 0.5
  23. local humidity_scale = 250
  24. --PerlinNoise(seed, octaves, persistence, scale)
  25. plantslib.perlin_temperature = PerlinNoise(temperature_seeddiff, temperature_octaves, temperature_persistence, temperature_scale)
  26. plantslib.perlin_humidity = PerlinNoise(humidity_seeddiff, humidity_octaves, humidity_persistence, humidity_scale)
  27. -- Local functions
  28. math.randomseed(os.time())
  29. function plantslib:is_node_loaded(node_pos)
  30. n = minetest.env:get_node_or_nil(node_pos)
  31. if (not n) or (n.name == "ignore") then
  32. return false
  33. end
  34. return true
  35. end
  36. function plantslib:dbg(msg)
  37. if DEBUG then
  38. print("[Plantlife] "..msg)
  39. minetest.log("verbose", "[Plantlife] "..msg)
  40. end
  41. end
  42. function plantslib:set_defaults(biome)
  43. biome.seed_diff = biome.seed_diff or 0
  44. biome.min_elevation = biome.min_elevation or -31000
  45. biome.max_elevation = biome.max_elevation or 31000
  46. biome.temp_min = biome.temp_min or 1
  47. biome.temp_max = biome.temp_max or -1
  48. biome.humidity_min = biome.humidity_min or 1
  49. biome.humidity_max = biome.humidity_max or -1
  50. biome.plantlife_limit = biome.plantlife_limit or 0.1
  51. biome.near_nodes_vertical = biome.near_nodes_vertical or 1
  52. -- specific to on-generate
  53. biome.neighbors = biome.neighbors or biome.surface
  54. biome.near_nodes_size = biome.near_nodes_size or 0
  55. biome.near_nodes_count = biome.near_nodes_count or 1
  56. biome.rarity = biome.rarity or 50
  57. biome.max_count = biome.max_count or 5
  58. if biome.check_air ~= false then biome.check_air = true end
  59. -- specific to abm spawner
  60. biome.seed_diff = biome.seed_diff or 0
  61. biome.light_min = biome.light_min or 0
  62. biome.light_max = biome.light_max or 15
  63. biome.depth_max = biome.depth_max or 1
  64. biome.facedir = biome.facedir or 0
  65. end
  66. -- Spawn plants using the map generator
  67. function plantslib:register_generate_plant(biomedef, node_or_function_or_model)
  68. plantslib:dbg("Registered mapgen spawner:")
  69. plantslib:dbg(dump(biomedef))
  70. minetest.register_on_generated(plantslib:search_for_surfaces(minp, maxp, biomedef, node_or_function_or_model))
  71. end
  72. function plantslib:search_for_surfaces(minp, maxp, biomedef, node_or_function_or_model)
  73. return function(minp, maxp, blockseed)
  74. local biome = biomedef
  75. plantslib:set_defaults(biome)
  76. plantslib:dbg("Started checking generated mapblock volume...")
  77. local searchnodes = minetest.env:find_nodes_in_area(minp, maxp, biome.surface)
  78. local in_biome_nodes = {}
  79. local num_in_biome_nodes = 0
  80. for i in ipairs(searchnodes) do
  81. local pos = searchnodes[i]
  82. local p_top = { x = pos.x, y = pos.y + 1, z = pos.z }
  83. local perlin1 = minetest.env:get_perlin(biome.seed_diff, perlin_octaves, perlin_persistence, perlin_scale)
  84. local noise1 = perlin1:get2d({x=p_top.x, y=p_top.z})
  85. local noise2 = plantslib.perlin_temperature:get2d({x=p_top.x, y=p_top.z})
  86. local noise3 = plantslib.perlin_humidity:get2d({x=p_top.x+150, y=p_top.z+50})
  87. if (not biome.depth or minetest.env:get_node({ x = pos.x, y = pos.y-biome.depth-1, z = pos.z }).name ~= biome.surface)
  88. and (not biome.check_air or (biome.check_air and minetest.env:get_node(p_top).name == "air"))
  89. and pos.y >= biome.min_elevation
  90. and pos.y <= biome.max_elevation
  91. and noise1 > biome.plantlife_limit
  92. and noise2 <= biome.temp_min
  93. and noise2 >= biome.temp_max
  94. and noise3 <= biome.humidity_min
  95. and noise3 >= biome.humidity_max
  96. and (not biome.ncount or table.getn(minetest.env:find_nodes_in_area({x=pos.x-1, y=pos.y, z=pos.z-1}, {x=pos.x+1, y=pos.y, z=pos.z+1}, biome.neighbors)) > biome.ncount)
  97. and (not biome.near_nodes or table.getn(minetest.env:find_nodes_in_area({x=pos.x-biome.near_nodes_size, y=pos.y-biome.near_nodes_vertical, z=pos.z-biome.near_nodes_size}, {x=pos.x+biome.near_nodes_size, y=pos.y+biome.near_nodes_vertical, z=pos.z+biome.near_nodes_size}, biome.near_nodes)) >= biome.near_nodes_count)
  98. and math.random(1,100) > biome.rarity
  99. and (not biome.below_nodes or string.find(dump(biome.below_nodes), minetest.env:get_node({x=pos.x, y=pos.y-1, z=pos.z}).name) )
  100. then
  101. table.insert(in_biome_nodes, pos)
  102. num_in_biome_nodes = num_in_biome_nodes + 1
  103. end
  104. end
  105. plantslib:dbg("Found "..num_in_biome_nodes.." surface nodes of type(s) "..dump(biome.surface).." in 5x5x5 mapblock volume at {"..dump(minp)..":"..dump(maxp).."} to check.")
  106. if num_in_biome_nodes > 0 then
  107. plantslib:dbg("Calculated maximum of "..math.min(biome.max_count*3, num_in_biome_nodes).." nodes to be checked in that list.")
  108. for i = 1, math.min(biome.max_count, num_in_biome_nodes) do
  109. local tries = 0
  110. local spawned = false
  111. while tries < 2 and not spawned do
  112. local pos = in_biome_nodes[math.random(1, num_in_biome_nodes)]
  113. if biome.spawn_replace_node then
  114. pos.y = pos.y-1
  115. end
  116. local p_top = { x = pos.x, y = pos.y + 1, z = pos.z }
  117. if not(biome.avoid_radius and biome.avoid_nodes) or not minetest.env:find_node_near(p_top, biome.avoid_radius + math.random(-1.5,1.5), biome.avoid_nodes) then
  118. spawned = true
  119. if biome.delete_above then
  120. minetest.env:remove_node(p_top)
  121. minetest.env:remove_node({x=p_top.x, y=p_top.y+1, z=p_top.z})
  122. end
  123. if biome.delete_above_surround then
  124. minetest.env:remove_node({x=p_top.x-1, y=p_top.y, z=p_top.z})
  125. minetest.env:remove_node({x=p_top.x+1, y=p_top.y, z=p_top.z})
  126. minetest.env:remove_node({x=p_top.x, y=p_top.y, z=p_top.z-1})
  127. minetest.env:remove_node({x=p_top.x, y=p_top.y, z=p_top.z+1})
  128. minetest.env:remove_node({x=p_top.x-1, y=p_top.y+1, z=p_top.z})
  129. minetest.env:remove_node({x=p_top.x+1, y=p_top.y+1, z=p_top.z})
  130. minetest.env:remove_node({x=p_top.x, y=p_top.y+1, z=p_top.z-1})
  131. minetest.env:remove_node({x=p_top.x, y=p_top.y+1, z=p_top.z+1})
  132. end
  133. if biome.spawn_replace_node then
  134. minetest.env:remove_node(pos)
  135. end
  136. if type(node_or_function_or_model) == "table" then
  137. plantslib:dbg("Spawn tree at {"..dump(pos).."}")
  138. plantslib:generate_tree(pos, node_or_function_or_model)
  139. elseif type(node_or_function_or_model) == "string" then
  140. if not minetest.registered_nodes[node_or_function_or_model] then
  141. plantslib:dbg("Call function: "..node_or_function_or_model.."("..dump(pos)..")")
  142. assert(loadstring(node_or_function_or_model.."("..dump(pos)..")"))()
  143. else
  144. plantslib:dbg("Add node: "..node_or_function_or_model.." at ("..dump(p_top)..")")
  145. minetest.env:add_node(p_top, { name = node_or_function_or_model })
  146. end
  147. end
  148. else
  149. tries = tries + 1
  150. plantslib:dbg("No room to spawn object at {"..dump(pos).."} -- trying again elsewhere")
  151. end
  152. end
  153. if tries == 2 then
  154. plantslib:dbg("Unable to spawn that object. Giving up on it.")
  155. end
  156. end
  157. end
  158. plantslib:dbg("Finished checking generated area.")
  159. end
  160. end
  161. -- The spawning ABM
  162. function plantslib:spawn_on_surfaces(sd,sp,sr,sc,ss,sa)
  163. local biome = {}
  164. if type(sd) ~= "table" then
  165. biome.spawn_delay = sd -- old api expects ABM interval param here.
  166. biome.spawn_plants = {sp}
  167. biome.avoid_radius = sr
  168. biome.spawn_chance = sc
  169. biome.spawn_surfaces = {ss}
  170. biome.avoid_nodes = sa
  171. else
  172. biome = sd
  173. end
  174. plantslib:set_defaults(biome)
  175. biome.spawn_plants_count = table.getn(biome.spawn_plants)
  176. plantslib:dbg("Registered spawning ABM:")
  177. plantslib:dbg(dump(biome))
  178. plantslib:dbg("Number of trigger nodes in this ABM: "..biome.spawn_plants_count )
  179. minetest.register_abm({
  180. nodenames = biome.spawn_surfaces,
  181. interval = biome.spawn_delay,
  182. chance = biome.spawn_chance,
  183. neighbors = biome.neighbors,
  184. action = function(pos, node, active_object_count, active_object_count_wider)
  185. local p_top = { x = pos.x, y = pos.y + 1, z = pos.z }
  186. local n_top = minetest.env:get_node(p_top)
  187. local perlin1 = minetest.env:get_perlin(biome.seed_diff, perlin_octaves, perlin_persistence, perlin_scale)
  188. local noise1 = perlin1:get2d({x=p_top.x, y=p_top.z})
  189. local noise2 = plantslib.perlin_temperature:get2d({x=p_top.x, y=p_top.z})
  190. local noise3 = plantslib.perlin_humidity:get2d({x=p_top.x+150, y=p_top.z+50})
  191. if noise1 > biome.plantlife_limit
  192. and noise2 <= biome.temp_min
  193. and noise2 >= biome.temp_max
  194. and noise3 <= biome.humidity_min
  195. and noise3 >= biome.humidity_max
  196. and plantslib:is_node_loaded(p_top) then
  197. local n_light = minetest.env:get_node_light(p_top, nil)
  198. if (not(biome.avoid_nodes and biome.avoid_radius) or not minetest.env:find_node_near(p_top, biome.avoid_radius + math.random(-1.5,2), biome.avoid_nodes))
  199. and n_light >= biome.light_min
  200. and n_light <= biome.light_max
  201. and (not(biome.neighbors and biome.ncount) or table.getn(minetest.env:find_nodes_in_area({x=pos.x-1, y=pos.y, z=pos.z-1}, {x=pos.x+1, y=pos.y, z=pos.z+1}, biome.neighbors)) > biome.ncount )
  202. and (not(biome.near_nodes and biome.near_nodes_count and biome.near_nodes_size) or table.getn(minetest.env:find_nodes_in_area({x=pos.x-biome.near_nodes_size, y=pos.y-biome.near_nodes_vertical, z=pos.z-biome.near_nodes_size}, {x=pos.x+biome.near_nodes_size, y=pos.y+biome.near_nodes_vertical, z=pos.z+biome.near_nodes_size}, biome.near_nodes)) >= biome.near_nodes_count)
  203. and (not(biome.air_count and biome.air_size) or table.getn(minetest.env:find_nodes_in_area({x=p_top.x-biome.air_size, y=p_top.y, z=p_top.z-biome.air_size}, {x=p_top.x+biome.air_size, y=p_top.y, z=p_top.z+biome.air_size}, "air")) >= biome.air_count)
  204. and pos.y >= biome.min_elevation
  205. and pos.y <= biome.max_elevation
  206. then
  207. local walldir = plantslib:find_adjacent_wall(p_top, biome.verticals_list)
  208. if biome.alt_wallnode and walldir then
  209. if n_top.name == "air" then
  210. plantslib:dbg("Spawn: "..biome.alt_wallnode.." on top of ("..dump(pos)..") against wall "..walldir)
  211. minetest.env:add_node(p_top, { name = biome.alt_wallnode, param2 = walldir })
  212. end
  213. else
  214. local currentsurface = minetest.env:get_node(pos).name
  215. if currentsurface ~= "default:water_source"
  216. or (currentsurface == "default:water_source" and table.getn(minetest.env:find_nodes_in_area({x=pos.x, y=pos.y-biome.depth_max-1, z=pos.z}, {x=pos.x, y=pos.y, z=pos.z}, {"default:dirt", "default:dirt_with_grass", "default:sand"})) > 0 )
  217. then
  218. local rnd = math.random(1, biome.spawn_plants_count)
  219. local plant_to_spawn = biome.spawn_plants[rnd]
  220. plantslib:dbg("Chose entry number "..rnd.." of "..biome.spawn_plants_count)
  221. local fdir = biome.facedir
  222. if biome.random_facedir then
  223. fdir = math.random(biome.random_facedir[1],biome.random_facedir[2])
  224. plantslib:dbg("Gave it a random facedir: "..fdir)
  225. end
  226. if type(spawn_plants) == "string" then
  227. plantslib:dbg("Call function: "..spawn_plants.."("..dump(pos)..")")
  228. assert(loadstring(spawn_plants.."("..dump(pos)..")"))()
  229. elseif not biome.spawn_on_side and not biome.spawn_on_bottom and not biome.spawn_replace_node then
  230. if n_top.name == "air" then
  231. plantslib:dbg("Spawn: "..plant_to_spawn.." on top of ("..dump(pos).."); facedir="..fdir)
  232. minetest.env:add_node(p_top, { name = plant_to_spawn, param2 = fdir })
  233. end
  234. elseif biome.spawn_replace_node then
  235. plantslib:dbg("Spawn: "..plant_to_spawn.." to replace "..minetest.env:get_node(pos).name.." at ("..dump(pos)..")")
  236. minetest.env:add_node(pos, { name = plant_to_spawn, param2 = fdir })
  237. elseif biome.spawn_on_side then
  238. local onside = plantslib:find_open_side(pos)
  239. if onside then
  240. plantslib:dbg("Spawn: "..plant_to_spawn.." at side of ("..dump(pos).."), facedir "..onside.facedir.."")
  241. minetest.env:add_node(onside.newpos, { name = plant_to_spawn, param2 = onside.facedir })
  242. end
  243. elseif biome.spawn_on_bottom then
  244. if minetest.env:get_node({x=pos.x, y=pos.y-1, z=pos.z}).name == "air" then
  245. plantslib:dbg("Spawn: "..plant_to_spawn.." on bottom of ("..dump(pos)..")")
  246. minetest.env:add_node({x=pos.x, y=pos.y-1, z=pos.z}, { name = plant_to_spawn, param2 = fdir} )
  247. end
  248. end
  249. end
  250. end
  251. end
  252. end
  253. end
  254. })
  255. end
  256. -- The growing ABM
  257. function plantslib:grow_plants(opts)
  258. local options = opts
  259. options.height_limit = options.height_limit or 5
  260. options.ground_nodes = options.ground_nodes or { "default:dirt_with_grass" }
  261. options.grow_nodes = options.grow_nodes or { "default:dirt_with_grass" }
  262. options.seed_diff = options.seed_diff or 0
  263. plantslib:dbg("Registered growing ABM:")
  264. plantslib:dbg(dump(options))
  265. minetest.register_abm({
  266. nodenames = { options.grow_plant },
  267. interval = options.grow_delay,
  268. chance = options.grow_chance,
  269. action = function(pos, node, active_object_count, active_object_count_wider)
  270. local p_top = {x=pos.x, y=pos.y+1, z=pos.z}
  271. local p_bot = {x=pos.x, y=pos.y-1, z=pos.z}
  272. local n_top = minetest.env:get_node(p_top)
  273. local n_bot = minetest.env:get_node(p_bot)
  274. local root_node = minetest.env:get_node({x=pos.x, y=pos.y-options.height_limit, z=pos.z})
  275. local walldir = nil
  276. if options.need_wall and options.verticals_list then
  277. walldir = plantslib:find_adjacent_wall(p_top, options.verticals_list)
  278. end
  279. if n_top.name == "air" and (not options.need_wall or (options.need_wall and walldir))
  280. then
  281. -- corner case for changing short junglegrass
  282. -- to dry shrub in desert
  283. if n_bot.name == options.dry_early_node and options.grow_plant == "junglegrass:short" then
  284. plantslib:dbg("Die: "..options.grow_plant.." becomes default:dry_shrub at ("..dump(pos)..")")
  285. minetest.env:add_node(pos, { name = "default:dry_shrub" })
  286. elseif options.grow_vertically and walldir then
  287. if plantslib:search_downward(pos, options.height_limit, options.ground_nodes) then
  288. plantslib:dbg("Grow "..options.grow_plant.." vertically to "..dump(p_top))
  289. minetest.env:add_node(p_top, { name = options.grow_plant, param2 = walldir})
  290. end
  291. elseif not options.grow_result and not options.grow_function then
  292. plantslib:dbg("Die: "..options.grow_plant.." at ("..dump(pos)..")")
  293. minetest.env:remove_node(pos)
  294. else
  295. plantslib:replace_object(pos, options.grow_result, options.grow_function, options.facedir, options.seed_diff)
  296. end
  297. end
  298. end
  299. })
  300. end
  301. -- Function to decide how to replace a plant - either grow it, replace it with
  302. -- a tree, run a function, or die with an error.
  303. function plantslib:replace_object(pos, replacement, grow_function, walldir, seeddiff)
  304. local growtype = type(grow_function)
  305. plantslib:dbg("replace_object called, growtype="..dump(grow_function))
  306. if growtype == "table" then
  307. plantslib:dbg("Grow: spawn tree at "..dump(pos))
  308. minetest.env:remove_node(pos)
  309. plantslib:grow_tree(pos, grow_function)
  310. return
  311. elseif growtype == "string" then
  312. local perlin1 = minetest.env:get_perlin(seeddiff, perlin_octaves, perlin_persistence, perlin_scale)
  313. local noise1 = perlin1:get2d({x=pos.x, y=pos.z})
  314. local noise2 = plantslib.perlin_temperature:get2d({x=pos.x, y=pos.z})
  315. plantslib:dbg("Grow: call function "..grow_function.."("..dump(pos)..","..noise1..","..noise2..","..dump(walldir)..")")
  316. assert(loadstring(grow_function.."("..dump(pos)..","..noise1..","..noise2..","..dump(walldir)..")"))()
  317. return
  318. elseif growtype == "nil" then
  319. plantslib:dbg("Grow: place "..replacement.." at ("..dump(pos)..") on wall "..dump(walldir))
  320. minetest.env:add_node(pos, { name = replacement, param2 = walldir})
  321. return
  322. elseif growtype ~= "nil" and growtype ~= "string" and growtype ~= "table" then
  323. error("Invalid grow function "..dump(grow_function).." used on object at ("..dump(pos)..")")
  324. end
  325. end
  326. -- function to decide if a node has a wall that's in verticals_list{}
  327. -- returns wall direction of valid node, or nil if invalid.
  328. function plantslib:find_adjacent_wall(pos, verticals)
  329. local verts = dump(verticals)
  330. if string.find(verts, minetest.env:get_node({ x=pos.x-1, y=pos.y, z=pos.z }).name) then return 3 end
  331. if string.find(verts, minetest.env:get_node({ x=pos.x+1, y=pos.y, z=pos.z }).name) then return 2 end
  332. if string.find(verts, minetest.env:get_node({ x=pos.x , y=pos.y, z=pos.z-1 }).name) then return 5 end
  333. if string.find(verts, minetest.env:get_node({ x=pos.x , y=pos.y, z=pos.z+1 }).name) then return 4 end
  334. return nil
  335. end
  336. -- Function to search downward from the given position, looking for the first
  337. -- node that matches the ground table. Returns the new position, or nil if
  338. -- height limit is exceeded before finding it.
  339. function plantslib:search_downward(pos, heightlimit, ground)
  340. for i = 0, heightlimit do
  341. if string.find(dump(ground), minetest.env:get_node({x=pos.x, y=pos.y-i, z = pos.z}).name) then
  342. return {x=pos.x, y=pos.y-i, z = pos.z}
  343. end
  344. end
  345. return false
  346. end
  347. function plantslib:find_open_side(pos)
  348. if minetest.env:get_node({ x=pos.x-1, y=pos.y, z=pos.z }).name == "air" then
  349. return {newpos = { x=pos.x-1, y=pos.y, z=pos.z }, facedir = 2}
  350. end
  351. if minetest.env:get_node({ x=pos.x+1, y=pos.y, z=pos.z }).name == "air" then
  352. return {newpos = { x=pos.x+1, y=pos.y, z=pos.z }, facedir = 3}
  353. end
  354. if minetest.env:get_node({ x=pos.x, y=pos.y, z=pos.z-1 }).name == "air" then
  355. return {newpos = { x=pos.x, y=pos.y, z=pos.z-1 }, facedir = 4}
  356. end
  357. if minetest.env:get_node({ x=pos.x, y=pos.y, z=pos.z+1 }).name == "air" then
  358. return {newpos = { x=pos.x, y=pos.y, z=pos.z+1 }, facedir = 5}
  359. end
  360. return nil
  361. end
  362. -- spawn_tree() on generate is routed through here so that other mods can hook
  363. -- into it.
  364. function plantslib:generate_tree(pos, node_or_function_or_model)
  365. minetest.env:spawn_tree(pos, node_or_function_or_model)
  366. end
  367. -- and this one's for the call used in the growing code
  368. function plantslib:grow_tree(pos, node_or_function_or_model)
  369. minetest.env:spawn_tree(pos, node_or_function_or_model)
  370. end
  371. print("[Plantlife Library] Loaded")