api.lua 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. biome_lib.block_log = {}
  2. biome_lib.block_recheck_list = {}
  3. biome_lib.run_block_recheck_list = false
  4. biome_lib.actionslist_aircheck = {}
  5. biome_lib.actionslist_no_aircheck = {}
  6. biome_lib.surfaceslist_aircheck = {}
  7. biome_lib.surfaceslist_no_aircheck = {}
  8. biome_lib.registered_decorations = {}
  9. biome_lib.fertile_perlin_octaves = 3
  10. biome_lib.fertile_perlin_persistence = 0.6
  11. biome_lib.fertile_perlin_scale = 100
  12. local temperature_seeddiff = 112
  13. local temperature_octaves = 3
  14. local temperature_persistence = 0.5
  15. local temperature_scale = 150
  16. local humidity_seeddiff = 9130
  17. local humidity_octaves = 3
  18. local humidity_persistence = 0.5
  19. local humidity_scale = 250
  20. local time_speed = tonumber(minetest.settings:get("time_speed"))
  21. biome_lib.time_scale = 1
  22. if time_speed and time_speed > 0 then
  23. biome_lib.time_scale = 72 / time_speed
  24. end
  25. biome_lib.air = {name = "air"}
  26. -- the mapgen rarely creates useful surfaces outside this range, but mods can
  27. -- still specify a wider range if needed.
  28. biome_lib.mapgen_elevation_limit = { ["min"] = -16, ["max"] = 48 }
  29. --PerlinNoise(seed, octaves, persistence, scale)
  30. biome_lib.perlin_temperature = PerlinNoise(temperature_seeddiff, temperature_octaves, temperature_persistence, temperature_scale)
  31. biome_lib.perlin_humidity = PerlinNoise(humidity_seeddiff, humidity_octaves, humidity_persistence, humidity_scale)
  32. -- Local functions
  33. local function tableize(s)
  34. return string.split(string.trim(string.gsub(s, " ", "")))
  35. end
  36. function biome_lib.dbg(msg, level)
  37. local l = tonumber(level) or 0
  38. if biome_lib.debug_log_level >= l then
  39. print(os.date("%Y-%m-%d %H:%M:%S").." [Biome Lib]: "..msg)
  40. minetest.log("verbose", "[Biome Lib]: "..msg)
  41. end
  42. end
  43. local function get_biome_data(pos, perlin_fertile)
  44. local fertility = perlin_fertile:get_2d({x=pos.x, y=pos.z})
  45. if type(minetest.get_biome_data) == "function" then
  46. local data = minetest.get_biome_data(pos)
  47. if data then
  48. return fertility, data.heat / 100, data.humidity / 100
  49. end
  50. end
  51. local temperature = biome_lib.perlin_temperature:get2d({x=pos.x, y=pos.z})
  52. local humidity = biome_lib.perlin_humidity:get2d({x=pos.x+150, y=pos.z+50})
  53. return fertility, temperature, humidity
  54. end
  55. function biome_lib.is_node_loaded(node_pos)
  56. local n = minetest.get_node_or_nil(node_pos)
  57. if (not n) or (n.name == "ignore") then
  58. return false
  59. end
  60. return true
  61. end
  62. function biome_lib.set_defaults(biome)
  63. biome.seed_diff = biome.seed_diff or 0
  64. biome.min_elevation = biome.min_elevation or biome_lib.mapgen_elevation_limit.min
  65. biome.max_elevation = biome.max_elevation or biome_lib.mapgen_elevation_limit.max
  66. biome.temp_min = biome.temp_min or 1
  67. biome.temp_max = biome.temp_max or -1
  68. biome.humidity_min = biome.humidity_min or 1
  69. biome.humidity_max = biome.humidity_max or -1
  70. biome.plantlife_limit = biome.plantlife_limit or 0.1
  71. biome.near_nodes_vertical = biome.near_nodes_vertical or 1
  72. -- specific to on-generate
  73. biome.neighbors = biome.neighbors or biome.surface
  74. biome.near_nodes_size = biome.near_nodes_size or 0
  75. biome.near_nodes_count = biome.near_nodes_count or 1
  76. biome.rarity = biome.rarity or 50
  77. biome.max_count = biome.max_count or 125
  78. if biome.check_air ~= false then biome.check_air = true end
  79. -- specific to abm spawner
  80. biome.seed_diff = biome.seed_diff or 0
  81. biome.light_min = biome.light_min or 0
  82. biome.light_max = biome.light_max or 15
  83. biome.depth_max = biome.depth_max or 1
  84. biome.facedir = biome.facedir or 0
  85. return biome
  86. end
  87. local function search_table(t, s)
  88. for i = 1, #t do
  89. if t[i] == s then return true end
  90. end
  91. return false
  92. end
  93. -- register the list of surfaces to spawn stuff on, filtering out all duplicates.
  94. -- separate the items by air-checking or non-air-checking map eval methods
  95. function biome_lib.register_on_generate(biomedef, nodes_or_function_or_model)
  96. -- if calling code passes an undefined node for a surface or
  97. -- as a node to be spawned, don't register an action for it.
  98. if type(nodes_or_function_or_model) == "string"
  99. and string.find(nodes_or_function_or_model, ":")
  100. and not minetest.registered_nodes[nodes_or_function_or_model] then
  101. biome_lib.dbg("Warning: Ignored registration for undefined spawn node: "..dump(nodes_or_function_or_model), 2)
  102. return
  103. end
  104. if type(nodes_or_function_or_model) == "string"
  105. and not string.find(nodes_or_function_or_model, ":") then
  106. biome_lib.dbg("Warning: Registered function call using deprecated string method: "..dump(nodes_or_function_or_model), 2)
  107. end
  108. biome_lib.mapgen_elevation_limit.min = math.min(biomedef.min_elevation or 0, biome_lib.mapgen_elevation_limit.min)
  109. biome_lib.mapgen_elevation_limit.max = math.max(biomedef.max_elevation or 0, biome_lib.mapgen_elevation_limit.max)
  110. local decor_def = biome_lib.can_use_decorations(biomedef, nodes_or_function_or_model)
  111. if decor_def then
  112. biome_lib.dbg("Using engine decorations instead of biome_lib functions for node(s): "..dump(nodes_or_function_or_model), 3)
  113. biome_lib.registered_decorations[#biome_lib.registered_decorations + 1] = nodes_or_function_or_model
  114. minetest.register_decoration(decor_def)
  115. return
  116. elseif biomedef.check_air == false then
  117. biome_lib.dbg("Register no-air-check mapgen hook: "..dump(nodes_or_function_or_model), 3)
  118. biome_lib.actionslist_no_aircheck[#biome_lib.actionslist_no_aircheck + 1] = { biomedef, nodes_or_function_or_model }
  119. local s = biomedef.surface
  120. if type(s) == "string" then
  121. if s and (string.find(s, "^group:") or minetest.registered_nodes[s]) then
  122. if not search_table(biome_lib.surfaceslist_no_aircheck, s) then
  123. biome_lib.surfaceslist_no_aircheck[#biome_lib.surfaceslist_no_aircheck + 1] = s
  124. end
  125. else
  126. biome_lib.dbg("Warning: Ignored no-air-check registration for undefined surface node: "..dump(s), 2)
  127. end
  128. else
  129. for i = 1, #biomedef.surface do
  130. local s = biomedef.surface[i]
  131. if s and (string.find(s, "^group:") or minetest.registered_nodes[s]) then
  132. if not search_table(biome_lib.surfaceslist_no_aircheck, s) then
  133. biome_lib.surfaceslist_no_aircheck[#biome_lib.surfaceslist_no_aircheck + 1] = s
  134. end
  135. else
  136. biome_lib.dbg("Warning: Ignored no-air-check registration for undefined surface node: "..dump(s), 2)
  137. end
  138. end
  139. end
  140. else
  141. biome_lib.dbg("Register with-air-checking mapgen hook: "..dump(nodes_or_function_or_model), 3)
  142. biome_lib.actionslist_aircheck[#biome_lib.actionslist_aircheck + 1] = { biomedef, nodes_or_function_or_model }
  143. local s = biomedef.surface
  144. if type(s) == "string" then
  145. if s and (string.find(s, "^group:") or minetest.registered_nodes[s]) then
  146. if not search_table(biome_lib.surfaceslist_aircheck, s) then
  147. biome_lib.surfaceslist_aircheck[#biome_lib.surfaceslist_aircheck + 1] = s
  148. end
  149. else
  150. biome_lib.dbg("Warning: Ignored with-air-checking registration for undefined surface node: "..dump(s), 2)
  151. end
  152. else
  153. for i = 1, #biomedef.surface do
  154. local s = biomedef.surface[i]
  155. if s and (string.find(s, "^group:") or minetest.registered_nodes[s]) then
  156. if not search_table(biome_lib.surfaceslist_aircheck, s) then
  157. biome_lib.surfaceslist_aircheck[#biome_lib.surfaceslist_aircheck + 1] = s
  158. end
  159. else
  160. biome_lib.dbg("Warning: Ignored with-air-checking registration for undefined surface node: "..dump(s), 2)
  161. end
  162. end
  163. end
  164. end
  165. end
  166. -- Function to check whether a position matches the given biome definition
  167. -- Returns true when the surface can be populated
  168. local function populate_single_surface(biome, pos, perlin_fertile_area, checkair)
  169. local p_top = { x = pos.x, y = pos.y + 1, z = pos.z }
  170. if math.random(1, 100) <= biome.rarity then
  171. return
  172. end
  173. local fertility, temperature, humidity = get_biome_data(pos, perlin_fertile_area)
  174. local pos_biome_ok = pos.y >= biome.min_elevation and pos.y <= biome.max_elevation
  175. and fertility > biome.plantlife_limit
  176. and temperature <= biome.temp_min and temperature >= biome.temp_max
  177. and humidity <= biome.humidity_min and humidity >= biome.humidity_max
  178. if not pos_biome_ok then
  179. return -- Y position mismatch, outside of biome
  180. end
  181. local biome_surfaces_string = dump(biome.surface)
  182. local surface_ok = false
  183. if not biome.depth then
  184. local dest_node = minetest.get_node(pos)
  185. if string.find(biome_surfaces_string, dest_node.name) then
  186. surface_ok = true
  187. else
  188. if string.find(biome_surfaces_string, "group:") then
  189. for j = 1, #biome.surface do
  190. if string.find(biome.surface[j], "^group:")
  191. and minetest.get_item_group(dest_node.name, biome.surface[j]) then
  192. surface_ok = true
  193. break
  194. end
  195. end
  196. end
  197. end
  198. elseif not string.find(biome_surfaces_string,
  199. minetest.get_node({ x = pos.x, y = pos.y-biome.depth-1, z = pos.z }).name) then
  200. surface_ok = true
  201. end
  202. if not surface_ok then
  203. return -- Surface does not match the given node group/name
  204. end
  205. if checkair and minetest.get_node(p_top).name ~= "air" then
  206. return
  207. end
  208. if biome.below_nodes and
  209. not string.find(dump(biome.below_nodes),
  210. minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}).name
  211. ) then
  212. return -- Node below does not match
  213. end
  214. if biome.ncount and
  215. #minetest.find_nodes_in_area(
  216. {x=pos.x-1, y=pos.y, z=pos.z-1},
  217. {x=pos.x+1, y=pos.y, z=pos.z+1},
  218. biome.neighbors
  219. ) <= biome.ncount then
  220. return -- Not enough similar biome nodes around
  221. end
  222. if biome.near_nodes and
  223. #minetest.find_nodes_in_area(
  224. {x=pos.x-biome.near_nodes_size, y=pos.y-biome.near_nodes_vertical, z=pos.z-biome.near_nodes_size},
  225. {x=pos.x+biome.near_nodes_size, y=pos.y+biome.near_nodes_vertical, z=pos.z+biome.near_nodes_size},
  226. biome.near_nodes
  227. ) < biome.near_nodes_count then
  228. return -- Long distance neighbours do not match
  229. end
  230. -- Position fits into given biome
  231. return true
  232. end
  233. function biome_lib.populate_surfaces(b, nodes_or_function_or_model, snodes, checkair)
  234. local items_added = 0
  235. local biome = biome_lib.set_defaults(b)
  236. -- filter stage 1 - find nodes from the supplied surfaces that are within the current biome.
  237. local in_biome_nodes = {}
  238. local perlin_fertile_area = minetest.get_perlin(biome.seed_diff, biome_lib.fertile_perlin_octaves, biome_lib.fertile_perlin_persistence, biome_lib.fertile_perlin_scale)
  239. for i = 1, #snodes do
  240. local pos = vector.new(snodes[i])
  241. if populate_single_surface(biome, pos, perlin_fertile_area, checkair) then
  242. in_biome_nodes[#in_biome_nodes + 1] = pos
  243. end
  244. end
  245. -- filter stage 2 - find places within that biome area to place the plants.
  246. local num_in_biome_nodes = #in_biome_nodes
  247. if num_in_biome_nodes == 0 then
  248. return 0
  249. end
  250. for i = 1, math.min(math.ceil(biome.max_count/25), num_in_biome_nodes) do
  251. local tries = 0
  252. local spawned = false
  253. while tries < 2 and not spawned do
  254. local pos = in_biome_nodes[math.random(1, num_in_biome_nodes)]
  255. if biome.spawn_replace_node then
  256. pos.y = pos.y-1
  257. end
  258. local p_top = { x = pos.x, y = pos.y + 1, z = pos.z }
  259. if not (biome.avoid_nodes and biome.avoid_radius
  260. and minetest.find_node_near(p_top, biome.avoid_radius
  261. + math.random(-1.5,2), biome.avoid_nodes)) then
  262. if biome.delete_above then
  263. minetest.swap_node(p_top, biome_lib.air)
  264. minetest.swap_node({x=p_top.x, y=p_top.y+1, z=p_top.z}, biome_lib.air)
  265. end
  266. if biome.delete_above_surround then
  267. minetest.swap_node({x=p_top.x-1, y=p_top.y, z=p_top.z}, biome_lib.air)
  268. minetest.swap_node({x=p_top.x+1, y=p_top.y, z=p_top.z}, biome_lib.air)
  269. minetest.swap_node({x=p_top.x, y=p_top.y, z=p_top.z-1}, biome_lib.air)
  270. minetest.swap_node({x=p_top.x, y=p_top.y, z=p_top.z+1}, biome_lib.air)
  271. minetest.swap_node({x=p_top.x-1, y=p_top.y+1, z=p_top.z}, biome_lib.air)
  272. minetest.swap_node({x=p_top.x+1, y=p_top.y+1, z=p_top.z}, biome_lib.air)
  273. minetest.swap_node({x=p_top.x, y=p_top.y+1, z=p_top.z-1}, biome_lib.air)
  274. minetest.swap_node({x=p_top.x, y=p_top.y+1, z=p_top.z+1}, biome_lib.air)
  275. end
  276. if biome.spawn_replace_node then
  277. minetest.swap_node(pos, biome_lib.air)
  278. end
  279. local objtype = type(nodes_or_function_or_model)
  280. if objtype == "table" then
  281. if nodes_or_function_or_model.axiom then
  282. biome_lib.generate_ltree(p_top, nodes_or_function_or_model)
  283. biome_lib.dbg("An L-tree was spawned at "..minetest.pos_to_string(p_top), 4)
  284. spawned = true
  285. else
  286. local fdir = nil
  287. if biome.random_facedir then
  288. fdir = math.random(biome.random_facedir[1], biome.random_facedir[2])
  289. end
  290. local n=nodes_or_function_or_model[math.random(#nodes_or_function_or_model)]
  291. minetest.swap_node(p_top, { name = n, param2 = fdir })
  292. biome_lib.dbg("Node \""..n.."\" was randomly picked from a list and placed at "..minetest.pos_to_string(p_top), 4)
  293. spawned = true
  294. end
  295. elseif objtype == "string" and
  296. minetest.registered_nodes[nodes_or_function_or_model] then
  297. local fdir = nil
  298. if biome.random_facedir then
  299. fdir = math.random(biome.random_facedir[1], biome.random_facedir[2])
  300. end
  301. minetest.swap_node(p_top, { name = nodes_or_function_or_model, param2 = fdir })
  302. biome_lib.dbg("Node \""..nodes_or_function_or_model.."\" was placed at "..minetest.pos_to_string(p_top), 4)
  303. spawned = true
  304. elseif objtype == "function" then
  305. nodes_or_function_or_model(pos)
  306. biome_lib.dbg("A function was run on surface node at "..minetest.pos_to_string(pos), 4)
  307. spawned = true
  308. elseif objtype == "string" and pcall(loadstring(("return %s(...)"):
  309. format(nodes_or_function_or_model)),pos) then
  310. spawned = true
  311. biome_lib.dbg("An obsolete string-specified function was run on surface node at "..minetest.pos_to_string(p_top), 4)
  312. else
  313. biome_lib.dbg("Warning: Ignored invalid definition for object "..dump(nodes_or_function_or_model).." that was pointed at {"..dump(pos).."}", 2)
  314. end
  315. else
  316. tries = tries + 1
  317. end
  318. end
  319. if spawned then items_added = items_added + 1 end
  320. end
  321. return items_added
  322. end
  323. -- Primary log read-out/mapgen spawner
  324. local function confirm_block_surroundings(p)
  325. local n=minetest.get_node_or_nil(p)
  326. if not n or n.name == "ignore" then return false end
  327. for x = -32,32,64 do -- step of 64 causes it to only check the 8 corner blocks
  328. for y = -32,32,64 do
  329. for z = -32,32,64 do
  330. local n=minetest.get_node_or_nil({x=p.x + x, y=p.y + y, z=p.z + z})
  331. if not n or n.name == "ignore" then return false end
  332. end
  333. end
  334. end
  335. return true
  336. end
  337. function biome_lib.generate_block(shutting_down)
  338. if shutting_down then
  339. if #biome_lib.block_recheck_list > 0 then
  340. for i = 1, #biome_lib.block_recheck_list do
  341. biome_lib.block_log[#biome_lib.block_log + 1] = biome_lib.block_recheck_list[i]
  342. end
  343. biome_lib.block_recheck_list = {}
  344. end
  345. biome_lib.run_block_recheck_list = false
  346. else
  347. if biome_lib.run_block_recheck_list
  348. and not biome_lib.block_recheck_list[1] then
  349. biome_lib.run_block_recheck_list = false
  350. end
  351. end
  352. local blocklog = biome_lib.run_block_recheck_list
  353. and biome_lib.block_recheck_list
  354. or biome_lib.block_log
  355. if not blocklog[1] then return end
  356. local minp = blocklog[1][1]
  357. local maxp = blocklog[1][2]
  358. local airflag = blocklog[1][3]
  359. local pos_hash = minetest.hash_node_position(minp)
  360. if not biome_lib.pos_hash then -- we need to read the maplock and get the surfaces list
  361. local now = minetest.get_us_time()
  362. biome_lib.pos_hash = {}
  363. minetest.load_area(minp)
  364. if not confirm_block_surroundings(minp)
  365. and not shutting_down
  366. and (blocklog[1][4] + biome_lib.block_timeout) > now then -- if any neighbors appear not to be loaded and the block hasn't expired yet, defer it
  367. if biome_lib.run_block_recheck_list then
  368. biome_lib.block_log[#biome_lib.block_log + 1] = table.copy(biome_lib.block_recheck_list[1])
  369. table.remove(biome_lib.block_recheck_list, 1)
  370. else
  371. biome_lib.block_recheck_list[#biome_lib.block_recheck_list + 1] = table.copy(biome_lib.block_log[1])
  372. table.remove(biome_lib.block_log, 1)
  373. end
  374. biome_lib.pos_hash = nil
  375. biome_lib.dbg("Mapblock at "..minetest.pos_to_string(minp)..
  376. " had a neighbor not fully emerged, skipped it for now.", 4)
  377. return
  378. else
  379. biome_lib.pos_hash.surface_node_list = airflag
  380. and minetest.find_nodes_in_area_under_air(minp, maxp, biome_lib.surfaceslist_aircheck)
  381. or minetest.find_nodes_in_area(minp, maxp, biome_lib.surfaceslist_no_aircheck)
  382. if #biome_lib.pos_hash.surface_node_list == 0 then
  383. biome_lib.dbg("Mapblock at "..minetest.pos_to_string(minp).." dequeued: no detected surfaces.", 4)
  384. table.remove(blocklog, 1)
  385. biome_lib.pos_hash = nil
  386. return
  387. else
  388. biome_lib.pos_hash.action_index = 1
  389. biome_lib.dbg("Mapblock at "..minetest.pos_to_string(minp)..
  390. " has "..#biome_lib.pos_hash.surface_node_list..
  391. " surface nodes to work on (airflag="..dump(airflag)..")", 4)
  392. end
  393. end
  394. elseif not (airflag and biome_lib.actionslist_aircheck[biome_lib.pos_hash.action_index])
  395. and not (not airflag and biome_lib.actionslist_no_aircheck[biome_lib.pos_hash.action_index]) then
  396. -- the block is finished, remove it
  397. if #biome_lib.pos_hash.surface_node_list > 0 then
  398. biome_lib.dbg("Deleted mapblock "..minetest.pos_to_string(minp).." from the block log", 4)
  399. end
  400. table.remove(blocklog, 1)
  401. biome_lib.pos_hash = nil
  402. else
  403. -- below, [1] is biome, [2] is the thing to be added
  404. local added = 0
  405. if airflag then
  406. if biome_lib.actionslist_aircheck[biome_lib.pos_hash.action_index] then
  407. added = biome_lib.populate_surfaces(
  408. biome_lib.actionslist_aircheck[biome_lib.pos_hash.action_index][1],
  409. biome_lib.actionslist_aircheck[biome_lib.pos_hash.action_index][2],
  410. biome_lib.pos_hash.surface_node_list, true)
  411. biome_lib.pos_hash.action_index = biome_lib.pos_hash.action_index + 1
  412. end
  413. else
  414. if biome_lib.actionslist_no_aircheck[biome_lib.pos_hash.action_index] then
  415. added = biome_lib.populate_surfaces(
  416. biome_lib.actionslist_no_aircheck[biome_lib.pos_hash.action_index][1],
  417. biome_lib.actionslist_no_aircheck[biome_lib.pos_hash.action_index][2],
  418. biome_lib.pos_hash.surface_node_list, false)
  419. biome_lib.pos_hash.action_index = biome_lib.pos_hash.action_index + 1
  420. end
  421. end
  422. if added > 0 then
  423. biome_lib.dbg("biome_lib.populate_surfaces ran on mapblock at "..
  424. minetest.pos_to_string(minp)..". Entry #"..
  425. (biome_lib.pos_hash.action_index-1).." added "..added.." items.", 4)
  426. end
  427. end
  428. end
  429. -- The spawning ABM
  430. function biome_lib.register_active_spawner(sd,sp,sr,sc,ss,sa)
  431. local b = {}
  432. if type(sd) ~= "table" then
  433. b.spawn_delay = sd -- old api expects ABM interval param here.
  434. b.spawn_plants = {sp}
  435. b.avoid_radius = sr
  436. b.spawn_chance = sc
  437. b.spawn_surfaces = {ss}
  438. b.avoid_nodes = sa
  439. else
  440. b = sd
  441. end
  442. if b.spawn_delay*biome_lib.time_scale >= 1 then
  443. b.interval = b.spawn_delay*biome_lib.time_scale
  444. else
  445. b.interval = 1
  446. end
  447. local biome = biome_lib.set_defaults(b)
  448. biome.spawn_plants_count = #(biome.spawn_plants)
  449. local n
  450. if type(biome.spawn_plants) == "table" then
  451. n = "random: "..biome.spawn_plants[1]..", ..."
  452. else
  453. n = biome.spawn_plants
  454. end
  455. biome.label = biome.label or "biome_lib spawn_on_surfaces(): "..n
  456. minetest.register_abm({
  457. nodenames = biome.spawn_surfaces,
  458. interval = biome.interval,
  459. chance = biome.spawn_chance,
  460. neighbors = biome.neighbors,
  461. label = biome.label,
  462. action = function(pos, node, active_object_count, active_object_count_wider)
  463. local p_top = { x = pos.x, y = pos.y + 1, z = pos.z }
  464. local n_top = minetest.get_node(p_top)
  465. local perlin_fertile_area = minetest.get_perlin(biome.seed_diff, biome_lib.fertile_perlin_octaves, biome_lib.fertile_perlin_persistence, biome_lib.fertile_perlin_scale)
  466. local fertility, temperature, humidity = get_biome_data(pos, perlin_fertile_area)
  467. local pos_biome_ok = pos.y >= biome.min_elevation and pos.y <= biome.max_elevation
  468. and fertility > biome.plantlife_limit
  469. and temperature <= biome.temp_min and temperature >= biome.temp_max
  470. and humidity <= biome.humidity_min and humidity >= biome.humidity_max
  471. and biome_lib.is_node_loaded(p_top)
  472. if not pos_biome_ok then
  473. return -- Outside of biome
  474. end
  475. local n_light = minetest.get_node_light(p_top, nil)
  476. if n_light < biome.light_min or n_light > biome.light_max then
  477. return -- Too dark or too bright
  478. end
  479. if biome.avoid_nodes and biome.avoid_radius and minetest.find_node_near(
  480. p_top, biome.avoid_radius + math.random(-1.5,2), biome.avoid_nodes) then
  481. return -- Nodes to avoid are nearby
  482. end
  483. if biome.neighbors and biome.ncount and
  484. #minetest.find_nodes_in_area(
  485. {x=pos.x-1, y=pos.y, z=pos.z-1},
  486. {x=pos.x+1, y=pos.y, z=pos.z+1},
  487. biome.neighbors
  488. ) <= biome.ncount then
  489. return -- Near neighbour nodes are not present
  490. end
  491. local NEAR_DST = biome.near_nodes_size
  492. if biome.near_nodes and biome.near_nodes_count and biome.near_nodes_size and
  493. #minetest.find_nodes_in_area(
  494. {x=pos.x-NEAR_DST, y=pos.y-biome.near_nodes_vertical, z=pos.z-NEAR_DST},
  495. {x=pos.x+NEAR_DST, y=pos.y+biome.near_nodes_vertical, z=pos.z+NEAR_DST},
  496. biome.near_nodes
  497. ) < biome.near_nodes_count then
  498. return -- Far neighbour nodes are not present
  499. end
  500. if (biome.air_count and biome.air_size) and
  501. #minetest.find_nodes_in_area(
  502. {x=p_top.x-biome.air_size, y=p_top.y, z=p_top.z-biome.air_size},
  503. {x=p_top.x+biome.air_size, y=p_top.y, z=p_top.z+biome.air_size},
  504. "air"
  505. ) < biome.air_count then
  506. return -- Not enough air
  507. end
  508. local walldir = biome_lib.find_adjacent_wall(p_top, biome.verticals_list, biome.choose_random_wall)
  509. if biome.alt_wallnode and walldir then
  510. if n_top.name == "air" then
  511. minetest.swap_node(p_top, { name = biome.alt_wallnode, param2 = walldir })
  512. end
  513. return
  514. end
  515. local currentsurface = minetest.get_node(pos).name
  516. if biome_lib.default_water_nodes[currentsurface] and
  517. #minetest.find_nodes_in_area(
  518. {x=pos.x, y=pos.y-biome.depth_max-1, z=pos.z},
  519. vector.new(pos),
  520. biome_lib.default_wet_surfaces
  521. ) == 0 then
  522. return -- On water but no ground nearby
  523. end
  524. local rnd = math.random(1, biome.spawn_plants_count)
  525. local plant_to_spawn = biome.spawn_plants[rnd]
  526. local fdir = biome.facedir
  527. if biome.random_facedir then
  528. fdir = math.random(biome.random_facedir[1],biome.random_facedir[2])
  529. end
  530. if type(biome.spawn_plants) == "string" then
  531. assert(loadstring(biome.spawn_plants.."(...)"))(pos)
  532. elseif not biome.spawn_on_side and not biome.spawn_on_bottom and not biome.spawn_replace_node then
  533. if n_top.name == "air" then
  534. minetest.swap_node(p_top, { name = plant_to_spawn, param2 = fdir })
  535. end
  536. elseif biome.spawn_replace_node then
  537. minetest.swap_node(pos, { name = plant_to_spawn, param2 = fdir })
  538. elseif biome.spawn_on_side then
  539. local onside = biome_lib.find_open_side(pos)
  540. if onside then
  541. minetest.swap_node(onside.newpos, { name = plant_to_spawn, param2 = onside.facedir })
  542. end
  543. elseif biome.spawn_on_bottom then
  544. if minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}).name == "air" then
  545. minetest.swap_node({x=pos.x, y=pos.y-1, z=pos.z}, { name = plant_to_spawn, param2 = fdir} )
  546. end
  547. end
  548. end
  549. })
  550. end
  551. -- Function to decide how to replace a plant - either grow it, replace it with
  552. -- a tree, run a function, or die with an error.
  553. function biome_lib.replace_plant(pos, replacement, grow_function, walldir, seeddiff)
  554. local growtype = type(grow_function)
  555. if growtype == "table" then
  556. minetest.swap_node(pos, biome_lib.air)
  557. biome_lib.grow_ltree(pos, grow_function)
  558. return
  559. elseif growtype == "function" then
  560. local perlin_fertile_area = minetest.get_perlin(seeddiff, biome_lib.fertile_perlin_octaves, biome_lib.fertile_perlin_persistence, biome_lib.fertile_perlin_scale)
  561. local fertility, temperature, _ = get_biome_data(pos, perlin_fertile_area)
  562. grow_function(pos, fertility, temperature, walldir)
  563. return
  564. elseif growtype == "string" then
  565. local perlin_fertile_area = minetest.get_perlin(seeddiff, biome_lib.fertile_perlin_octaves, biome_lib.fertile_perlin_persistence, biome_lib.fertile_perlin_scale)
  566. local fertility, temperature, _ = get_biome_data(pos, perlin_fertile_area)
  567. assert(loadstring(grow_function.."(...)"))(pos, fertility, temperature, walldir)
  568. return
  569. elseif growtype == "nil" then
  570. minetest.swap_node(pos, { name = replacement, param2 = walldir})
  571. return
  572. elseif growtype ~= "nil" and growtype ~= "string" and growtype ~= "table" then
  573. error("Invalid grow function "..dump(grow_function).." used on object at ("..dump(pos)..")")
  574. end
  575. end
  576. -- Check for infinite stacks
  577. if minetest.get_modpath("unified_inventory") or not minetest.settings:get_bool("creative_mode") then
  578. biome_lib.expect_infinite_stacks = false
  579. else
  580. biome_lib.expect_infinite_stacks = true
  581. end
  582. -- read a field from a node's definition
  583. function biome_lib.get_nodedef_field(nodename, fieldname)
  584. if not minetest.registered_nodes[nodename] then
  585. return nil
  586. end
  587. return minetest.registered_nodes[nodename][fieldname]
  588. end