mirror of
				https://github.com/mt-mods/biome_lib.git
				synced 2025-10-27 03:15:29 +01:00 
			
		
		
		
	Compare commits
	
		
			45 Commits
		
	
	
		
			6935e73994
			...
			2021-04-08
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | ed0b23677d | ||
|  | 3b35fc67c6 | ||
|  | eabc053c05 | ||
|  | dd650da443 | ||
|  | 0a34e3c7af | ||
|  | 07c2b1d9d4 | ||
|  | 1dc0febd8c | ||
|  | 50f921a85a | ||
|  | 6009f261c2 | ||
|  | 531577afcf | ||
|  | e346fd599f | ||
|  | 212024a9b4 | ||
|  | 1d2593f022 | ||
|  | 3bc8737e2d | ||
|  | 0ea4cb3848 | ||
|  | bef0a0d87e | ||
|  | 89ca62e492 | ||
|  | a325c2ccd8 | ||
|  | 8ecb401309 | ||
|  | 9ed4858518 | ||
|  | 26dbbb5a67 | ||
|  | ec0a0f0c3b | ||
|  | e92361675f | ||
|  | 7f1fec6ae0 | ||
|  | f569bb1fbd | ||
|  | f2a807b814 | ||
|  | 9b7705c380 | ||
|  | c9f6235815 | ||
|  | ddd88613e0 | ||
|  | 27cd07cb36 | ||
|  | ac8738d837 | ||
|  | 0005af6022 | ||
|  | 228296411e | ||
|  | 353ca0cbd4 | ||
|  | d65c72d48b | ||
|  | 10a1089767 | ||
|  | 5a910875af | ||
|  | a96f015ce9 | ||
|  | d72e15f1d0 | ||
|  | 29899f2d36 | ||
|  | 0837ff7fd2 | ||
|  | ff9ce531d6 | ||
|  | fa304f9d18 | ||
|  | b7d69a8487 | ||
|  | f056d6d76e | 
							
								
								
									
										71
									
								
								API.txt
									
									
									
									
									
								
							
							
						
						
									
										71
									
								
								API.txt
									
									
									
									
									
								
							| @@ -72,6 +72,7 @@ biome = { | |||||||
| 	---- most likely want to use at least some of these to limit how and | 	---- most likely want to use at least some of these to limit how and | ||||||
| 	---- where your objects are spawned. | 	---- where your objects are spawned. | ||||||
|  |  | ||||||
|  | 	label = string,		-- set this to identify the ABM for Minetest's profiler | ||||||
| 	avoid_nodes = {table},	-- same meaning as savoid, above | 	avoid_nodes = {table},	-- same meaning as savoid, above | ||||||
| 	avoid_radius = num,	-- same as sradius | 	avoid_radius = num,	-- same as sradius | ||||||
| 	seed_diff = num,	-- The Perlin seed difference value passed to the | 	seed_diff = num,	-- The Perlin seed difference value passed to the | ||||||
| @@ -342,14 +343,18 @@ into something else over time.  This function has no return value, and accepts | |||||||
| a biome definition table as the only parameter.  These are defined like so: | a biome definition table as the only parameter.  These are defined like so: | ||||||
|  |  | ||||||
| options = { | options = { | ||||||
| 	grow_plant = "string", -- Name of the node to be grown into something | 	label = string,		-- set this to identify the ABM for Minetest's | ||||||
| 						-- else.  This value is passed to the ABM as the | 						-- profiler.  If not set, biome_lib will set it to  | ||||||
| 						-- "nodenames" parameter, so it is the plants | 						-- "biome_lib grow_plants(): " appended with the node | ||||||
| 						-- themselves that are the ABM trigger, rather than | 						-- in grow_plant (or the first item if it's a table) | ||||||
|  | 	grow_plant = "string" or {table}, -- Name(s) of the node(s) to be grown | ||||||
|  | 						-- into something else.  This value is passed to the | ||||||
|  | 						-- ABM as the "nodenames" parameter, so the plants | ||||||
|  | 						-- themselves are the ABM trigger, rather than | ||||||
| 						-- the ground they spawned on.  A plant will only grow | 						-- the ground they spawned on.  A plant will only grow | ||||||
| 						-- if the node above it is air.  Can also be a table, | 						-- if the node above it is air.  If you use a table, | ||||||
| 						-- but note that all nodes referenced therein will be | 						-- note that all nodes referenced therein will be | ||||||
| 						-- grown into the same object. | 						-- grown into the same final object. | ||||||
| 	grow_delay = num,	-- Passed as the ABM "interval" parameter, as with | 	grow_delay = num,	-- Passed as the ABM "interval" parameter, as with | ||||||
| 						-- spawning. | 						-- spawning. | ||||||
| 	grow_chance = num,	-- Passed as the ABM "chance" parameter. | 	grow_chance = num,	-- Passed as the ABM "chance" parameter. | ||||||
| @@ -441,12 +446,22 @@ question is already loaded, or false if not. | |||||||
|  |  | ||||||
|  |  | ||||||
| ===== | ===== | ||||||
| dbg(string) | dbg(string, level) | ||||||
|  |  | ||||||
| This is a simple debug output function which takes one string parameter.  It | This is a simple debug output function which takes one string parameter.  It | ||||||
| just checks if DEBUG is true and outputs the phrase "[Plantlife] " followed by | just checks if DEBUG is true and outputs the phrase "[Plantlife] " followed by | ||||||
| the supplied string, via the print() function, if so. | the supplied string, via the print() function, if so. | ||||||
|  |  | ||||||
|  | 'level' is a number that, if supplied, dictates the lowest 'biome_lib_debug' | ||||||
|  | can be set to in minetest.conf for this message to be displayed.  Both the | ||||||
|  | default log level and the default message level are 0, thus always showing the | ||||||
|  | supplied message. | ||||||
|  |  | ||||||
|  | Although it's not set in stone, a good practice is to use a level of 0 (or | ||||||
|  | just omit the value) for anything that generally important enough that it | ||||||
|  | ought always be shown, 1 for errors, 2 for warnings, 3 for info, 4 for verbose | ||||||
|  | spammy stuff. | ||||||
|  |  | ||||||
| ===== | ===== | ||||||
| biome_lib:generate_tree(pos, treemodel) | biome_lib:generate_tree(pos, treemodel) | ||||||
| biome_lib:grow_tree(pos, treemodel) | biome_lib:grow_tree(pos, treemodel) | ||||||
| @@ -583,3 +598,43 @@ And this particular one is mapped slightly differently from the others: | |||||||
|  |  | ||||||
| (Note the +150 and +50 offsets) | (Note the +150 and +50 offsets) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | ================== | ||||||
|  | Default game nodes | ||||||
|  | ================== | ||||||
|  |  | ||||||
|  | Although this project was intended to be used with minetest_game, it can be | ||||||
|  | configured to work with something else instead.  All you need to do is provide | ||||||
|  | the names of the nodes in your game you want biome_lib's internals to use. | ||||||
|  |  | ||||||
|  | Put these settings in your game's minetest.conf (or your client's own config, | ||||||
|  | if desired).  You'll need to set all of them. | ||||||
|  |  | ||||||
|  | biome_lib_default_grow_through_nodes | ||||||
|  | 	Comma-separated list of things that a spawned node is allowed to grow | ||||||
|  | 	through.  Air is always added to whatever you specify. | ||||||
|  | 	Default:  air, default:snow | ||||||
|  |  | ||||||
|  | biome_lib_default_water_nodes | ||||||
|  | 	Comma-separated list of nodes that should be treated as water for the sake | ||||||
|  | 	of looking for neighboring "wet" ground. | ||||||
|  | 	Default: default:water_source, default:water_flowing, | ||||||
|  | 		default:river_water_source, default:river_water_flowing | ||||||
|  |  | ||||||
|  | biome_lib_default_wet_surfaces | ||||||
|  | 	Comma-separated list of nodes that should be considered "wet" if one of | ||||||
|  | 	the aforementioned water nodes is nearby. | ||||||
|  | 	Default: default:dirt, default:dirt_with_grass, default:sand | ||||||
|  |  | ||||||
|  | biome_lib_default_grow_nodes | ||||||
|  | 	Comma-separated list of nodes that something must be sitting on to be | ||||||
|  | 	able to actively change from one thing to another (such as a sapling | ||||||
|  | 	growing into a tree), to be used for ALL growable nodes, if the calling | ||||||
|  | 	mod doesn't provide its own lists. | ||||||
|  | 	Default: "default:dirt_with_grass" | ||||||
|  |  | ||||||
|  | biome_lib_default_ground_nodes | ||||||
|  | 	Comma-separated list of nodes to use as the "root" of something that can | ||||||
|  | 	gradually climb up a wall, to be used for ALL such nodes, if the calling | ||||||
|  | 	mod doesn't provide its own lists. | ||||||
|  | 	Default: "default:dirt_with_grass" | ||||||
|   | |||||||
| @@ -12,7 +12,7 @@ Both mapgen-based spawning and ABM-based spawning is supported. Growing code is | |||||||
|  |  | ||||||
| It is primarily intended for mapgen v6, but it should work fine when used with mapgen v7. | It is primarily intended for mapgen v6, but it should work fine when used with mapgen v7. | ||||||
|  |  | ||||||
| **Dependencies**: default from minetest_game | **Dependencies:** nothing, but if you don't use `minetest_game`, you'll need to supply some settings (see API.txt). | ||||||
|  |  | ||||||
| **Recommends**: [Plantlife Modpack](https://github.com/minetest-mods/plantlife_modpack),  | **Recommends**: [Plantlife Modpack](https://github.com/minetest-mods/plantlife_modpack),  | ||||||
| [More Trees](https://github.com/minetest-mods/moretrees) | [More Trees](https://github.com/minetest-mods/moretrees) | ||||||
|   | |||||||
| @@ -1,3 +0,0 @@ | |||||||
| default |  | ||||||
| intllib? |  | ||||||
|  |  | ||||||
							
								
								
									
										90
									
								
								growth.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										90
									
								
								growth.lua
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,90 @@ | |||||||
|  | local time_scale = ... | ||||||
|  |  | ||||||
|  | -- The growing ABM | ||||||
|  |  | ||||||
|  | function biome_lib.check_surface(name, nodes) | ||||||
|  | 	if not nodes then return true end | ||||||
|  | 	if type(nodes) == "string" then return nodes == name end | ||||||
|  | 	if nodes.set and nodes[name] then | ||||||
|  | 		return true | ||||||
|  | 	else | ||||||
|  | 		for _, n in ipairs(nodes) do | ||||||
|  | 			if name == n then return true end | ||||||
|  | 		end | ||||||
|  | 	end | ||||||
|  | 	return false | ||||||
|  | end | ||||||
|  |  | ||||||
|  | function biome_lib:grow_plants(opts) | ||||||
|  |  | ||||||
|  | 	local options = opts | ||||||
|  |  | ||||||
|  | 	options.height_limit = options.height_limit or 5 | ||||||
|  | 	options.ground_nodes = options.ground_nodes or biome_lib.default_ground_nodes | ||||||
|  | 	options.grow_nodes = options.grow_nodes or biome_lib.default_grow_nodes | ||||||
|  | 	options.seed_diff = options.seed_diff or 0 | ||||||
|  |  | ||||||
|  | 	local n | ||||||
|  |  | ||||||
|  | 	if type(options.grow_plant) == "table" then | ||||||
|  | 		n = "multi: "..options.grow_plant[1]..", ..." | ||||||
|  | 	else | ||||||
|  | 		n = options.grow_plant | ||||||
|  | 	end | ||||||
|  |  | ||||||
|  | 	options.label = options.label or "biome_lib grow_plants(): "..n | ||||||
|  |  | ||||||
|  | 	if options.grow_delay*time_scale >= 1 then | ||||||
|  | 		options.interval = options.grow_delay*time_scale | ||||||
|  | 	else | ||||||
|  | 		options.interval = 1 | ||||||
|  | 	end | ||||||
|  |  | ||||||
|  | 	minetest.register_abm({ | ||||||
|  | 		nodenames = { options.grow_plant }, | ||||||
|  | 		interval = options.interval, | ||||||
|  | 		chance = options.grow_chance, | ||||||
|  | 		label = options.label, | ||||||
|  | 		action = function(pos, node, active_object_count, active_object_count_wider) | ||||||
|  | 			local p_top = {x=pos.x, y=pos.y+1, z=pos.z} | ||||||
|  | 			local p_bot = {x=pos.x, y=pos.y-1, z=pos.z} | ||||||
|  | 			local n_top = minetest.get_node(p_top) | ||||||
|  | 			local n_bot = minetest.get_node(p_bot) | ||||||
|  | 			local root_node = minetest.get_node({x=pos.x, y=pos.y-options.height_limit, z=pos.z}) | ||||||
|  | 			local walldir = nil | ||||||
|  | 			if options.need_wall and options.verticals_list then | ||||||
|  | 				walldir = biome_lib:find_adjacent_wall(p_top, options.verticals_list, options.choose_random_wall) | ||||||
|  | 			end | ||||||
|  | 			if biome_lib.default_grow_through_nodes[n_top.name] | ||||||
|  | 			  and (not options.need_wall or (options.need_wall and walldir)) then | ||||||
|  | 				if options.grow_vertically and walldir then | ||||||
|  | 					if biome_lib:search_downward(pos, options.height_limit, options.ground_nodes) then | ||||||
|  | 						minetest.swap_node(p_top, { name = options.grow_plant, param2 = walldir}) | ||||||
|  | 					end | ||||||
|  |  | ||||||
|  | 				elseif biome_lib.check_surface(n_bot.name, options.grow_nodes) then | ||||||
|  | 					if not options.grow_result and not options.grow_function then | ||||||
|  | 						minetest.swap_node(pos, biome_lib.air) | ||||||
|  |  | ||||||
|  | 					else | ||||||
|  | 						biome_lib:replace_object(pos, options.grow_result, options.grow_function, options.facedir, options.seed_diff) | ||||||
|  | 					end | ||||||
|  | 				end | ||||||
|  | 			end | ||||||
|  | 		end | ||||||
|  | 	}) | ||||||
|  | end | ||||||
|  |  | ||||||
|  |  | ||||||
|  | -- spawn_tree() on generate is routed through here so that other mods can hook | ||||||
|  | -- into it. | ||||||
|  |  | ||||||
|  | function biome_lib:generate_tree(pos, nodes_or_function_or_model) | ||||||
|  | 	minetest.spawn_tree(pos, nodes_or_function_or_model) | ||||||
|  | end | ||||||
|  |  | ||||||
|  | -- and this one's for the call used in the growing code | ||||||
|  |  | ||||||
|  | function biome_lib:grow_tree(pos, nodes_or_function_or_model) | ||||||
|  | 	minetest.spawn_tree(pos, nodes_or_function_or_model) | ||||||
|  | end | ||||||
							
								
								
									
										765
									
								
								init.lua
									
									
									
									
									
								
							
							
						
						
									
										765
									
								
								init.lua
									
									
									
									
									
								
							| @@ -1,36 +1,10 @@ | |||||||
| -- Biome library mod by Vanessa Ezekowitz | -- Biome library mod by VanessaE | ||||||
| -- | -- | ||||||
| -- I got the temperature map idea from "hmmmm", values used for it came from | -- I got the temperature map idea from "hmmmm", values used for it came from | ||||||
| -- Splizard's snow mod. | -- Splizard's snow mod. | ||||||
| -- | -- | ||||||
|  |  | ||||||
| -- Various settings - most of these probably won't need to be changed |  | ||||||
|  |  | ||||||
| biome_lib = {} | biome_lib = {} | ||||||
| biome_lib.air = {name = "air"} |  | ||||||
|  |  | ||||||
| plantslib = setmetatable({}, { __index=function(t,k) print("Use of deprecated function:", k) return biome_lib[k] end }) |  | ||||||
|  |  | ||||||
| biome_lib.blocklist_aircheck = {} |  | ||||||
| biome_lib.blocklist_no_aircheck = {} |  | ||||||
|  |  | ||||||
| biome_lib.surface_nodes_aircheck = {} |  | ||||||
| biome_lib.surface_nodes_no_aircheck = {} |  | ||||||
|  |  | ||||||
| biome_lib.surfaceslist_aircheck = {} |  | ||||||
| biome_lib.surfaceslist_no_aircheck = {} |  | ||||||
|  |  | ||||||
| biome_lib.actioncount_aircheck = {} |  | ||||||
| biome_lib.actioncount_no_aircheck = {} |  | ||||||
|  |  | ||||||
| biome_lib.actionslist_aircheck = {} |  | ||||||
| biome_lib.actionslist_no_aircheck = {} |  | ||||||
|  |  | ||||||
| biome_lib.modpath = minetest.get_modpath("biome_lib") |  | ||||||
|  |  | ||||||
| biome_lib.total_no_aircheck_calls = 0 |  | ||||||
|  |  | ||||||
| biome_lib.queue_run_ratio = tonumber(minetest.settings:get("biome_lib_queue_run_ratio")) or 100 |  | ||||||
|  |  | ||||||
| -- Boilerplate to support localized strings if intllib mod is installed. | -- Boilerplate to support localized strings if intllib mod is installed. | ||||||
| local S | local S | ||||||
| @@ -45,14 +19,73 @@ else | |||||||
| end | end | ||||||
| biome_lib.intllib = S | biome_lib.intllib = S | ||||||
|  |  | ||||||
| local DEBUG = false --... except if you want to spam the console with debugging info :-) | -- Various settings - most of these probably won't need to be changed | ||||||
|  |  | ||||||
| function biome_lib:dbg(msg) | biome_lib.air = {name = "air"} | ||||||
| 	if DEBUG then |  | ||||||
| 		print("[Plantlife] "..msg) | biome_lib.block_log = {} | ||||||
| 		minetest.log("verbose", "[Plantlife] "..msg) | biome_lib.block_recheck_list = {} | ||||||
|  | biome_lib.run_block_recheck_list = false | ||||||
|  |  | ||||||
|  | biome_lib.actionslist_aircheck = {} | ||||||
|  | biome_lib.actionslist_no_aircheck = {} | ||||||
|  |  | ||||||
|  | biome_lib.surfaceslist_aircheck = {} | ||||||
|  | biome_lib.surfaceslist_no_aircheck = {} | ||||||
|  |  | ||||||
|  | biome_lib.modpath = minetest.get_modpath("biome_lib") | ||||||
|  |  | ||||||
|  | local function tableize(s) | ||||||
|  | 	return string.split(string.trim(string.gsub(s, " ", ""))) | ||||||
| end | end | ||||||
|  |  | ||||||
|  | local c1 = minetest.settings:get("biome_lib_default_grow_through_nodes") | ||||||
|  | biome_lib.default_grow_through_nodes = {["air"] = true} | ||||||
|  | if c1 then | ||||||
|  | 	for _, i in ipairs(tableize(c1)) do | ||||||
|  | 		biome_lib.default_grow_through_nodes[i] = true | ||||||
| 	end | 	end | ||||||
|  | else | ||||||
|  | 	biome_lib.default_grow_through_nodes["default:snow"] = true | ||||||
|  | end | ||||||
|  |  | ||||||
|  | local c2 = minetest.settings:get("biome_lib_default_water_nodes") | ||||||
|  | biome_lib.default_water_nodes = {} | ||||||
|  | if c2 then | ||||||
|  | 	for _, i in ipairs(tableize(c2)) do | ||||||
|  | 		biome_lib.default_water_nodes[i] = true | ||||||
|  | 	end | ||||||
|  | else | ||||||
|  | 	biome_lib.default_water_nodes["default:water_source"] = true | ||||||
|  | 	biome_lib.default_water_nodes["default:water_flowing"] = true | ||||||
|  | 	biome_lib.default_water_nodes["default:river_water_source"] = true | ||||||
|  | 	biome_lib.default_water_nodes["default:river_water_flowing"] = true | ||||||
|  | end | ||||||
|  |  | ||||||
|  | local c3 = minetest.settings:get("biome_lib_default_wet_surfaces") | ||||||
|  | local c4 = minetest.settings:get("biome_lib_default_ground_nodes") | ||||||
|  | local c5 = minetest.settings:get("biome_lib_default_grow_nodes") | ||||||
|  |  | ||||||
|  | biome_lib.default_wet_surfaces = c3 and tableize(c3) or {"default:dirt", "default:dirt_with_grass", "default:sand"} | ||||||
|  | biome_lib.default_ground_nodes = c4 and tableize(c4) or {"default:dirt_with_grass"} | ||||||
|  | biome_lib.default_grow_nodes =   c5 and tableize(c5) or {"default:dirt_with_grass"} | ||||||
|  |  | ||||||
|  | biome_lib.debug_log_level = tonumber(minetest.settings:get("biome_lib_debug_log_level")) or 0 | ||||||
|  |  | ||||||
|  | local rr = tonumber(minetest.settings:get("biome_lib_queue_run_ratio")) or -100 | ||||||
|  | biome_lib.queue_run_ratio = 100 - rr | ||||||
|  | biome_lib.entries_per_step = math.max(-rr, 1) | ||||||
|  |  | ||||||
|  | -- the timer that manages the block timeout is in microseconds, but the timer | ||||||
|  | -- that manages the queue wakeup call has to be in seconds, and works best if | ||||||
|  | -- it takes a little longer than the block timeout interval. | ||||||
|  |  | ||||||
|  | local t = tonumber(minetest.settings:get("biome_lib_block_timeout")) or 300 | ||||||
|  |  | ||||||
|  | biome_lib.block_timeout = t * 1000000 | ||||||
|  | biome_lib.block_queue_wakeup_time = t * 1.1 | ||||||
|  |  | ||||||
|  | local time_speed = tonumber(minetest.settings:get("time_speed")) | ||||||
|  |  | ||||||
| biome_lib.plantlife_seed_diff = 329 -- needs to be global so other mods can see it | biome_lib.plantlife_seed_diff = 329 -- needs to be global so other mods can see it | ||||||
|  |  | ||||||
| @@ -71,7 +104,6 @@ local humidity_persistence = 0.5 | |||||||
| local humidity_scale = 250 | local humidity_scale = 250 | ||||||
|  |  | ||||||
| local time_scale = 1 | local time_scale = 1 | ||||||
| local time_speed = tonumber(minetest.settings:get("time_speed")) |  | ||||||
|  |  | ||||||
| if time_speed and time_speed > 0 then | if time_speed and time_speed > 0 then | ||||||
| 	time_scale = 72 / time_speed | 	time_scale = 72 / time_speed | ||||||
| @@ -84,6 +116,30 @@ biome_lib.perlin_humidity = PerlinNoise(humidity_seeddiff, humidity_octaves, hum | |||||||
|  |  | ||||||
| -- Local functions | -- Local functions | ||||||
|  |  | ||||||
|  | function biome_lib.dbg(msg, level) | ||||||
|  | 	local l = tonumber(level) or 0 | ||||||
|  | 	if biome_lib.debug_log_level >= l then | ||||||
|  | 		print("[Biome Lib] "..msg) | ||||||
|  | 		minetest.log("verbose", "[Biome Lib] "..msg) | ||||||
|  | 	end | ||||||
|  | end | ||||||
|  |  | ||||||
|  | local function get_biome_data(pos, perlin_fertile) | ||||||
|  | 	local fertility = perlin_fertile:get_2d({x=pos.x, y=pos.z}) | ||||||
|  |  | ||||||
|  | 	if type(minetest.get_biome_data) == "function" then | ||||||
|  | 		local data = minetest.get_biome_data(pos) | ||||||
|  | 		if data then | ||||||
|  | 			return fertility, data.heat / 100, data.humidity / 100 | ||||||
|  | 		end | ||||||
|  | 	end | ||||||
|  |  | ||||||
|  | 	local temperature = biome_lib.perlin_temperature:get2d({x=pos.x, y=pos.z}) | ||||||
|  | 	local humidity = biome_lib.perlin_humidity:get2d({x=pos.x+150, y=pos.z+50}) | ||||||
|  |  | ||||||
|  | 	return fertility, temperature, humidity | ||||||
|  | end | ||||||
|  |  | ||||||
| function biome_lib:is_node_loaded(node_pos) | function biome_lib:is_node_loaded(node_pos) | ||||||
| 	local n = minetest.get_node_or_nil(node_pos) | 	local n = minetest.get_node_or_nil(node_pos) | ||||||
| 	if (not n) or (n.name == "ignore") then | 	if (not n) or (n.name == "ignore") then | ||||||
| @@ -109,7 +165,7 @@ function biome_lib:set_defaults(biome) | |||||||
| 	biome.near_nodes_size = biome.near_nodes_size or 0 | 	biome.near_nodes_size = biome.near_nodes_size or 0 | ||||||
| 	biome.near_nodes_count = biome.near_nodes_count or 1 | 	biome.near_nodes_count = biome.near_nodes_count or 1 | ||||||
| 	biome.rarity = biome.rarity or 50 | 	biome.rarity = biome.rarity or 50 | ||||||
| 	biome.max_count = biome.max_count or 5 | 	biome.max_count = biome.max_count or 125 | ||||||
| 	if biome.check_air ~= false then biome.check_air = true end | 	if biome.check_air ~= false then biome.check_air = true end | ||||||
|  |  | ||||||
| -- specific to abm spawner | -- specific to abm spawner | ||||||
| @@ -138,17 +194,17 @@ function biome_lib:register_generate_plant(biomedef, nodes_or_function_or_model) | |||||||
| 	if type(nodes_or_function_or_model) == "string" | 	if type(nodes_or_function_or_model) == "string" | ||||||
| 	  and string.find(nodes_or_function_or_model, ":") | 	  and string.find(nodes_or_function_or_model, ":") | ||||||
| 	  and not minetest.registered_nodes[nodes_or_function_or_model] then | 	  and not minetest.registered_nodes[nodes_or_function_or_model] then | ||||||
| 		biome_lib:dbg("Warning: Ignored registration for undefined spawn node: "..dump(nodes_or_function_or_model)) | 		biome_lib.dbg("Warning: Ignored registration for undefined spawn node: "..dump(nodes_or_function_or_model), 2) | ||||||
| 		return | 		return | ||||||
| 	end | 	end | ||||||
|  |  | ||||||
| 	if type(nodes_or_function_or_model) == "string" | 	if type(nodes_or_function_or_model) == "string" | ||||||
| 	  and not string.find(nodes_or_function_or_model, ":") then | 	  and not string.find(nodes_or_function_or_model, ":") then | ||||||
| 		biome_lib:dbg("Warning: Registered function call using deprecated string method: "..dump(nodes_or_function_or_model)) | 		biome_lib.dbg("Warning: Registered function call using deprecated string method: "..dump(nodes_or_function_or_model), 2) | ||||||
| 	end | 	end | ||||||
|  |  | ||||||
| 	if biomedef.check_air == false then  | 	if biomedef.check_air == false then  | ||||||
| 		biome_lib:dbg("Register no-air-check mapgen hook: "..dump(nodes_or_function_or_model)) | 		biome_lib.dbg("Register no-air-check mapgen hook: "..dump(nodes_or_function_or_model), 3) | ||||||
| 		biome_lib.actionslist_no_aircheck[#biome_lib.actionslist_no_aircheck + 1] = { biomedef, nodes_or_function_or_model } | 		biome_lib.actionslist_no_aircheck[#biome_lib.actionslist_no_aircheck + 1] = { biomedef, nodes_or_function_or_model } | ||||||
| 		local s = biomedef.surface | 		local s = biomedef.surface | ||||||
| 		if type(s) == "string" then | 		if type(s) == "string" then | ||||||
| @@ -157,7 +213,7 @@ function biome_lib:register_generate_plant(biomedef, nodes_or_function_or_model) | |||||||
| 					biome_lib.surfaceslist_no_aircheck[#biome_lib.surfaceslist_no_aircheck + 1] = s | 					biome_lib.surfaceslist_no_aircheck[#biome_lib.surfaceslist_no_aircheck + 1] = s | ||||||
| 				end | 				end | ||||||
| 			else | 			else | ||||||
| 				biome_lib:dbg("Warning: Ignored no-air-check registration for undefined surface node: "..dump(s)) | 				biome_lib.dbg("Warning: Ignored no-air-check registration for undefined surface node: "..dump(s), 2) | ||||||
| 			end | 			end | ||||||
| 		else | 		else | ||||||
| 			for i = 1, #biomedef.surface do | 			for i = 1, #biomedef.surface do | ||||||
| @@ -167,12 +223,12 @@ function biome_lib:register_generate_plant(biomedef, nodes_or_function_or_model) | |||||||
| 						biome_lib.surfaceslist_no_aircheck[#biome_lib.surfaceslist_no_aircheck + 1] = s | 						biome_lib.surfaceslist_no_aircheck[#biome_lib.surfaceslist_no_aircheck + 1] = s | ||||||
| 					end | 					end | ||||||
| 				else | 				else | ||||||
| 					biome_lib:dbg("Warning: Ignored no-air-check registration for undefined surface node: "..dump(s)) | 					biome_lib.dbg("Warning: Ignored no-air-check registration for undefined surface node: "..dump(s), 2) | ||||||
| 				end | 				end | ||||||
| 			end | 			end | ||||||
| 		end | 		end | ||||||
| 	else | 	else | ||||||
| 		biome_lib:dbg("Register with-air-checking mapgen hook: "..dump(nodes_or_function_or_model)) | 		biome_lib.dbg("Register with-air-checking mapgen hook: "..dump(nodes_or_function_or_model), 3) | ||||||
| 		biome_lib.actionslist_aircheck[#biome_lib.actionslist_aircheck + 1] = { biomedef, nodes_or_function_or_model } | 		biome_lib.actionslist_aircheck[#biome_lib.actionslist_aircheck + 1] = { biomedef, nodes_or_function_or_model } | ||||||
| 		local s = biomedef.surface | 		local s = biomedef.surface | ||||||
| 		if type(s) == "string" then | 		if type(s) == "string" then | ||||||
| @@ -181,7 +237,7 @@ function biome_lib:register_generate_plant(biomedef, nodes_or_function_or_model) | |||||||
| 					biome_lib.surfaceslist_aircheck[#biome_lib.surfaceslist_aircheck + 1] = s | 					biome_lib.surfaceslist_aircheck[#biome_lib.surfaceslist_aircheck + 1] = s | ||||||
| 				end | 				end | ||||||
| 			else | 			else | ||||||
| 				biome_lib:dbg("Warning: Ignored with-air-checking registration for undefined surface node: "..dump(s)) | 				biome_lib.dbg("Warning: Ignored with-air-checking registration for undefined surface node: "..dump(s), 2) | ||||||
| 			end | 			end | ||||||
| 		else | 		else | ||||||
| 			for i = 1, #biomedef.surface do | 			for i = 1, #biomedef.surface do | ||||||
| @@ -191,28 +247,34 @@ function biome_lib:register_generate_plant(biomedef, nodes_or_function_or_model) | |||||||
| 						biome_lib.surfaceslist_aircheck[#biome_lib.surfaceslist_aircheck + 1] = s | 						biome_lib.surfaceslist_aircheck[#biome_lib.surfaceslist_aircheck + 1] = s | ||||||
| 					end | 					end | ||||||
| 				else | 				else | ||||||
| 					biome_lib:dbg("Warning: Ignored with-air-checking registration for undefined surface node: "..dump(s)) | 					biome_lib.dbg("Warning: Ignored with-air-checking registration for undefined surface node: "..dump(s), 2) | ||||||
| 				end | 				end | ||||||
| 			end | 			end | ||||||
| 		end | 		end | ||||||
| 	end | 	end | ||||||
| end | end | ||||||
|  |  | ||||||
| function biome_lib:populate_surfaces(biome, nodes_or_function_or_model, snodes, checkair) | -- Function to check whether a position matches the given biome definition | ||||||
|  | -- Returns true when the surface can be populated | ||||||
|  |  | ||||||
| 	biome_lib:set_defaults(biome) | local function populate_single_surface(biome, pos, perlin_fertile_area, checkair) | ||||||
|  |  | ||||||
| 	-- filter stage 1 - find nodes from the supplied surfaces that are within the current biome. |  | ||||||
|  |  | ||||||
| 	local in_biome_nodes = {} |  | ||||||
| 	local perlin_fertile_area = minetest.get_perlin(biome.seed_diff, perlin_octaves, perlin_persistence, perlin_scale) |  | ||||||
|  |  | ||||||
| 	for i = 1, #snodes do |  | ||||||
| 		local pos = snodes[i] |  | ||||||
| 	local p_top = { x = pos.x, y = pos.y + 1, z = pos.z } | 	local p_top = { x = pos.x, y = pos.y + 1, z = pos.z } | ||||||
| 		local noise1 = perlin_fertile_area:get2d({x=pos.x, y=pos.z}) |  | ||||||
| 		local noise2 = biome_lib.perlin_temperature:get2d({x=pos.x, y=pos.z}) | 	if math.random(1, 100) <= biome.rarity then | ||||||
| 		local noise3 = biome_lib.perlin_humidity:get2d({x=pos.x+150, y=pos.z+50}) | 		return | ||||||
|  | 	end | ||||||
|  |  | ||||||
|  | 	local fertility, temperature, humidity = get_biome_data(pos, perlin_fertile_area) | ||||||
|  |  | ||||||
|  | 	local pos_biome_ok = pos.y >= biome.min_elevation and pos.y <= biome.max_elevation | ||||||
|  | 		and fertility > biome.plantlife_limit | ||||||
|  | 		and temperature <= biome.temp_min and temperature >= biome.temp_max | ||||||
|  | 		and humidity <= biome.humidity_min and humidity >= biome.humidity_max | ||||||
|  | 	 | ||||||
|  | 	if not pos_biome_ok then | ||||||
|  | 		return -- Y position mismatch, outside of biome | ||||||
|  | 	end | ||||||
|  |  | ||||||
| 	local biome_surfaces_string = dump(biome.surface) | 	local biome_surfaces_string = dump(biome.surface) | ||||||
| 	local surface_ok = false | 	local surface_ok = false | ||||||
|  |  | ||||||
| @@ -231,24 +293,61 @@ function biome_lib:populate_surfaces(biome, nodes_or_function_or_model, snodes, | |||||||
| 				end | 				end | ||||||
| 			end | 			end | ||||||
| 		end | 		end | ||||||
| 		elseif not string.find(biome_surfaces_string, minetest.get_node({ x = pos.x, y = pos.y-biome.depth-1, z = pos.z }).name) then | 	elseif not string.find(biome_surfaces_string, | ||||||
|  | 			minetest.get_node({ x = pos.x, y = pos.y-biome.depth-1, z = pos.z }).name) then | ||||||
| 		surface_ok = true | 		surface_ok = true | ||||||
| 	end | 	end | ||||||
| 	 | 	 | ||||||
| 		if surface_ok | 	if not surface_ok then | ||||||
| 		  and pos.y >= biome.min_elevation | 		return -- Surface does not match the given node group/name | ||||||
| 		  and pos.y <= biome.max_elevation | 	end | ||||||
| 		  and noise1 > biome.plantlife_limit |  | ||||||
| 		  and noise2 <= biome.temp_min | 	if checkair and minetest.get_node(p_top).name ~= "air" then | ||||||
| 		  and noise2 >= biome.temp_max | 		return | ||||||
| 		  and noise3 <= biome.humidity_min | 	end | ||||||
| 		  and noise3 >= biome.humidity_max |  | ||||||
| 		  and (not checkair or minetest.get_node(p_top).name == "air") | 	if biome.below_nodes and | ||||||
| 		  and (not biome.ncount or #(minetest.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) | 			not string.find(dump(biome.below_nodes), | ||||||
| 		  and (not biome.near_nodes or #(minetest.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) | 				minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}).name | ||||||
| 		  and math.random(1,100) > biome.rarity | 			) then | ||||||
| 		  and (not biome.below_nodes or string.find(dump(biome.below_nodes), minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}).name) ) | 		return -- Node below does not match | ||||||
| 		  then | 	end | ||||||
|  |  | ||||||
|  | 	if biome.ncount and | ||||||
|  | 			#minetest.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 then | ||||||
|  | 		return -- Not enough similar biome nodes around | ||||||
|  | 	end | ||||||
|  |  | ||||||
|  | 	if biome.near_nodes and | ||||||
|  | 			#minetest.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 then | ||||||
|  | 		return -- Long distance neighbours do not match | ||||||
|  | 	end | ||||||
|  |  | ||||||
|  | 	-- Position fits into given biome | ||||||
|  | 	return true | ||||||
|  | end | ||||||
|  |  | ||||||
|  | function biome_lib.populate_surfaces(biome, nodes_or_function_or_model, snodes, checkair) | ||||||
|  | 	local items_added = 0 | ||||||
|  |  | ||||||
|  | 	biome_lib:set_defaults(biome) | ||||||
|  |  | ||||||
|  | 	-- filter stage 1 - find nodes from the supplied surfaces that are within the current biome. | ||||||
|  |  | ||||||
|  | 	local in_biome_nodes = {} | ||||||
|  | 	local perlin_fertile_area = minetest.get_perlin(biome.seed_diff, perlin_octaves, perlin_persistence, perlin_scale) | ||||||
|  |  | ||||||
|  | 	for i = 1, #snodes do | ||||||
|  | 		local pos = vector.new(snodes[i]) | ||||||
|  | 		if populate_single_surface(biome, pos, perlin_fertile_area, checkair) then | ||||||
| 			in_biome_nodes[#in_biome_nodes + 1] = pos | 			in_biome_nodes[#in_biome_nodes + 1] = pos | ||||||
| 		end | 		end | ||||||
| 	end | 	end | ||||||
| @@ -257,8 +356,11 @@ function biome_lib:populate_surfaces(biome, nodes_or_function_or_model, snodes, | |||||||
|  |  | ||||||
| 	local num_in_biome_nodes = #in_biome_nodes | 	local num_in_biome_nodes = #in_biome_nodes | ||||||
|  |  | ||||||
| 	if num_in_biome_nodes > 0 then | 	if num_in_biome_nodes == 0 then | ||||||
| 		for i = 1, math.min(biome.max_count, num_in_biome_nodes) do | 		return 0 | ||||||
|  | 	end | ||||||
|  |  | ||||||
|  | 	for i = 1, math.min(math.ceil(biome.max_count/25), num_in_biome_nodes) do | ||||||
| 		local tries = 0 | 		local tries = 0 | ||||||
| 		local spawned = false | 		local spawned = false | ||||||
| 		while tries < 2 and not spawned do | 		while tries < 2 and not spawned do | ||||||
| @@ -268,7 +370,9 @@ function biome_lib:populate_surfaces(biome, nodes_or_function_or_model, snodes, | |||||||
| 			end | 			end | ||||||
| 			local p_top = { x = pos.x, y = pos.y + 1, z = pos.z } | 			local p_top = { x = pos.x, y = pos.y + 1, z = pos.z } | ||||||
|  |  | ||||||
| 				if not (biome.avoid_nodes and biome.avoid_radius and minetest.find_node_near(p_top, biome.avoid_radius + math.random(-1.5,2), biome.avoid_nodes)) then | 			if not (biome.avoid_nodes and biome.avoid_radius | ||||||
|  | 					and minetest.find_node_near(p_top, biome.avoid_radius | ||||||
|  | 					+ math.random(-1.5,2), biome.avoid_nodes)) then | ||||||
| 				if biome.delete_above then | 				if biome.delete_above then | ||||||
| 					minetest.swap_node(p_top, biome_lib.air) | 					minetest.swap_node(p_top, biome_lib.air) | ||||||
| 					minetest.swap_node({x=p_top.x, y=p_top.y+1, z=p_top.z}, biome_lib.air) | 					minetest.swap_node({x=p_top.x, y=p_top.y+1, z=p_top.z}, biome_lib.air) | ||||||
| @@ -295,13 +399,16 @@ function biome_lib:populate_surfaces(biome, nodes_or_function_or_model, snodes, | |||||||
| 				if objtype == "table" then | 				if objtype == "table" then | ||||||
| 					if nodes_or_function_or_model.axiom then | 					if nodes_or_function_or_model.axiom then | ||||||
| 						biome_lib:generate_tree(p_top, nodes_or_function_or_model) | 						biome_lib:generate_tree(p_top, nodes_or_function_or_model) | ||||||
|  | 						biome_lib.dbg("An L-tree was spawned at "..minetest.pos_to_string(p_top), 4) | ||||||
| 						spawned = true | 						spawned = true | ||||||
| 					else | 					else | ||||||
| 						local fdir = nil | 						local fdir = nil | ||||||
| 						if biome.random_facedir then | 						if biome.random_facedir then | ||||||
| 							fdir = math.random(biome.random_facedir[1], biome.random_facedir[2]) | 							fdir = math.random(biome.random_facedir[1], biome.random_facedir[2]) | ||||||
| 						end | 						end | ||||||
| 							minetest.swap_node(p_top, { name = nodes_or_function_or_model[math.random(#nodes_or_function_or_model)], param2 = fdir }) | 						local n=nodes_or_function_or_model[math.random(#nodes_or_function_or_model)] | ||||||
|  | 						minetest.swap_node(p_top, { name = n, param2 = fdir }) | ||||||
|  | 						biome_lib.dbg("Node \""..n.."\" was randomly picked from a list and placed at "..minetest.pos_to_string(p_top), 4) | ||||||
| 						spawned = true | 						spawned = true | ||||||
| 					end | 					end | ||||||
| 				elseif objtype == "string" and | 				elseif objtype == "string" and | ||||||
| @@ -311,166 +418,187 @@ function biome_lib:populate_surfaces(biome, nodes_or_function_or_model, snodes, | |||||||
| 						fdir = math.random(biome.random_facedir[1], biome.random_facedir[2]) | 						fdir = math.random(biome.random_facedir[1], biome.random_facedir[2]) | ||||||
| 					end | 					end | ||||||
| 					minetest.swap_node(p_top, { name = nodes_or_function_or_model, param2 = fdir }) | 					minetest.swap_node(p_top, { name = nodes_or_function_or_model, param2 = fdir }) | ||||||
|  | 					biome_lib.dbg("Node \""..nodes_or_function_or_model.."\" was placed at "..minetest.pos_to_string(p_top), 4) | ||||||
| 					spawned = true | 					spawned = true | ||||||
| 				elseif objtype == "function" then | 				elseif objtype == "function" then | ||||||
| 					nodes_or_function_or_model(pos) | 					nodes_or_function_or_model(pos) | ||||||
|  | 					biome_lib.dbg("A function was run on surface node at "..minetest.pos_to_string(pos), 4) | ||||||
| 					spawned = true | 					spawned = true | ||||||
| 				elseif objtype == "string" and pcall(loadstring(("return %s(...)"): | 				elseif objtype == "string" and pcall(loadstring(("return %s(...)"): | ||||||
| 					format(nodes_or_function_or_model)),pos) then | 					format(nodes_or_function_or_model)),pos) then | ||||||
| 					spawned = true | 					spawned = true | ||||||
|  | 					biome_lib.dbg("An obsolete string-specified function was run on surface node at "..minetest.pos_to_string(p_top), 4) | ||||||
| 				else | 				else | ||||||
| 						biome_lib:dbg("Warning: Ignored invalid definition for object "..dump(nodes_or_function_or_model).." that was pointed at {"..dump(pos).."}") | 					biome_lib.dbg("Warning: Ignored invalid definition for object "..dump(nodes_or_function_or_model).." that was pointed at {"..dump(pos).."}", 2) | ||||||
| 				end | 				end | ||||||
| 			else | 			else | ||||||
| 				tries = tries + 1 | 				tries = tries + 1 | ||||||
| 			end | 			end | ||||||
| 		end | 		end | ||||||
|  | 		if spawned then items_added = items_added + 1 end | ||||||
|  | 	end | ||||||
|  | 	return items_added | ||||||
|  | end | ||||||
|  |  | ||||||
|  | -- Primary log read-out/mapgen spawner | ||||||
|  |  | ||||||
|  | local function confirm_block_surroundings(p) | ||||||
|  | 	local n=minetest.get_node_or_nil(p) | ||||||
|  | 	if not n or n.name == "ignore" then return false end | ||||||
|  |  | ||||||
|  | 	for x = -32,32,64 do -- step of 64 causes it to only check the 8 corner blocks | ||||||
|  | 		for y = -32,32,64 do | ||||||
|  | 			for z = -32,32,64 do | ||||||
|  | 				local n=minetest.get_node_or_nil({x=p.x + x, y=p.y + y, z=p.z + z}) | ||||||
|  | 				if not n or n.name == "ignore" then return false end | ||||||
| 			end | 			end | ||||||
| 		end | 		end | ||||||
| 	end | 	end | ||||||
|  | 	return true | ||||||
|  | end | ||||||
|  |  | ||||||
| -- Primary mapgen spawner, for mods that can work with air checking enabled on | function biome_lib.generate_block(shutting_down) | ||||||
| -- a surface during the initial map read stage. |  | ||||||
|  |  | ||||||
| function biome_lib:generate_block_with_air_checking() | 	if shutting_down then | ||||||
| 	if #biome_lib.blocklist_aircheck > 0 then | 		if #biome_lib.block_recheck_list > 0 then | ||||||
|  | 			for i = 1, #biome_lib.block_recheck_list do | ||||||
| 		local minp =		biome_lib.blocklist_aircheck[1][1] | 				biome_lib.block_log[#biome_lib.block_log + 1] = biome_lib.block_recheck_list[i] | ||||||
| 		local maxp =		biome_lib.blocklist_aircheck[1][2] | 			end | ||||||
|  | 			biome_lib.block_recheck_list = {} | ||||||
| 		-- use the block hash as a unique key into the surface nodes | 		end | ||||||
| 		-- tables, so that we can write the tables thread-safely. | 		biome_lib.run_block_recheck_list = false | ||||||
|  |  | ||||||
| 		local blockhash =	minetest.hash_node_position(minp) |  | ||||||
|  |  | ||||||
| 		if not biome_lib.surface_nodes_aircheck.blockhash then |  | ||||||
|  |  | ||||||
| 			if type(minetest.find_nodes_in_area_under_air) == "function" then -- use newer API call |  | ||||||
| 				biome_lib.surface_nodes_aircheck.blockhash = |  | ||||||
| 					minetest.find_nodes_in_area_under_air(minp, maxp, biome_lib.surfaceslist_aircheck) |  | ||||||
| 	else | 	else | ||||||
| 				local search_area = minetest.find_nodes_in_area(minp, maxp, biome_lib.surfaceslist_aircheck) | 		if biome_lib.run_block_recheck_list | ||||||
|  | 		  and not biome_lib.block_recheck_list[1] then | ||||||
| 				-- search the generated block for air-bounded surfaces the slow way. | 				biome_lib.run_block_recheck_list = false | ||||||
|  |  | ||||||
| 				biome_lib.surface_nodes_aircheck.blockhash = {} |  | ||||||
|  |  | ||||||
| 				for i = 1, #search_area do |  | ||||||
| 				local pos = search_area[i] |  | ||||||
| 					local p_top = { x=pos.x, y=pos.y+1, z=pos.z } |  | ||||||
| 					if minetest.get_node(p_top).name == "air" then |  | ||||||
| 						biome_lib.surface_nodes_aircheck.blockhash[#biome_lib.surface_nodes_aircheck.blockhash + 1] = pos |  | ||||||
| 		end | 		end | ||||||
| 	end | 	end | ||||||
| 			end |  | ||||||
| 			biome_lib.actioncount_aircheck.blockhash = 1 |  | ||||||
|  |  | ||||||
|  | 	local blocklog = biome_lib.run_block_recheck_list | ||||||
|  | 	  and biome_lib.block_recheck_list | ||||||
|  | 	  or biome_lib.block_log | ||||||
|  |  | ||||||
|  | 	if not blocklog[1] then return end | ||||||
|  |  | ||||||
|  | 	local minp =		blocklog[1][1] | ||||||
|  | 	local maxp =		blocklog[1][2] | ||||||
|  | 	local airflag = 	blocklog[1][3] | ||||||
|  | 	local pos_hash = 	minetest.hash_node_position(minp) | ||||||
|  |  | ||||||
|  | 	if not biome_lib.pos_hash then -- we need to read the maplock and get the surfaces list | ||||||
|  | 		local now = minetest.get_us_time() | ||||||
|  | 		biome_lib.pos_hash = {} | ||||||
|  | 		minetest.load_area(minp) | ||||||
|  | 		if not confirm_block_surroundings(minp) | ||||||
|  | 		  and not shutting_down | ||||||
|  | 		  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 | ||||||
|  | 			blocklog[1][4] = now -- reset the timer, give this block more time to "cook" | ||||||
|  | 			if biome_lib.run_block_recheck_list then | ||||||
|  | 				biome_lib.block_log[#biome_lib.block_log + 1] = table.copy(biome_lib.block_recheck_list[1]) | ||||||
|  | 				table.remove(biome_lib.block_recheck_list, 1) | ||||||
| 			else | 			else | ||||||
| 			if biome_lib.actioncount_aircheck.blockhash <= #biome_lib.actionslist_aircheck then | 				biome_lib.block_recheck_list[#biome_lib.block_recheck_list + 1] = table.copy(biome_lib.block_log[1]) | ||||||
| 				-- [1] is biome, [2] is node/function/model | 				table.remove(biome_lib.block_log, 1) | ||||||
| 				biome_lib:populate_surfaces( | 			end | ||||||
| 					biome_lib.actionslist_aircheck[biome_lib.actioncount_aircheck.blockhash][1], | 			biome_lib.pos_hash = nil | ||||||
| 					biome_lib.actionslist_aircheck[biome_lib.actioncount_aircheck.blockhash][2], | 				biome_lib.dbg("Mapblock at "..minetest.pos_to_string(minp).. | ||||||
| 					biome_lib.surface_nodes_aircheck.blockhash, true) | 					" had a neighbor not fully emerged, skipped it for now.", 4) | ||||||
| 				biome_lib.actioncount_aircheck.blockhash = biome_lib.actioncount_aircheck.blockhash + 1 | 			return | ||||||
| 		else | 		else | ||||||
| 				if biome_lib.surface_nodes_aircheck.blockhash then | 			biome_lib.pos_hash.surface_node_list = airflag | ||||||
| 					table.remove(biome_lib.blocklist_aircheck, 1) | 				and minetest.find_nodes_in_area_under_air(minp, maxp, biome_lib.surfaceslist_aircheck) | ||||||
| 					biome_lib.surface_nodes_aircheck.blockhash = nil | 				or minetest.find_nodes_in_area(minp, maxp, biome_lib.surfaceslist_no_aircheck) | ||||||
|  | 			biome_lib.pos_hash.action_index = 1 | ||||||
|  | 			if #biome_lib.pos_hash.surface_node_list > 0 then | ||||||
|  | 				biome_lib.dbg("Mapblock at "..minetest.pos_to_string(minp).. | ||||||
|  | 					" has "..#biome_lib.pos_hash.surface_node_list.. | ||||||
|  | 					" surface nodes to work on (airflag="..dump(airflag)..")", 4) | ||||||
| 			end | 			end | ||||||
| 		end | 		end | ||||||
|  | 	elseif not (airflag and biome_lib.actionslist_aircheck[biome_lib.pos_hash.action_index]) | ||||||
|  | 	  and not (not airflag and biome_lib.actionslist_no_aircheck[biome_lib.pos_hash.action_index]) then | ||||||
|  | 		-- the block is finished, remove it | ||||||
|  | 		if #biome_lib.pos_hash.surface_node_list > 0 then | ||||||
|  | 			biome_lib.dbg("Deleted mapblock "..minetest.pos_to_string(minp).." from the block log", 4) | ||||||
| 		end | 		end | ||||||
| 	end | 		table.remove(blocklog, 1) | ||||||
| end | 		biome_lib.pos_hash = nil | ||||||
|  |  | ||||||
| -- Secondary mapgen spawner, for mods that require disabling of |  | ||||||
| -- checking for air during the initial map read stage. |  | ||||||
|  |  | ||||||
| function biome_lib:generate_block_no_aircheck() |  | ||||||
| 	if #biome_lib.blocklist_no_aircheck > 0 then |  | ||||||
|  |  | ||||||
| 		local minp =		biome_lib.blocklist_no_aircheck[1][1] |  | ||||||
| 		local maxp =		biome_lib.blocklist_no_aircheck[1][2] |  | ||||||
|  |  | ||||||
| 		local blockhash =	minetest.hash_node_position(minp) |  | ||||||
|  |  | ||||||
| 		if not biome_lib.surface_nodes_no_aircheck.blockhash then |  | ||||||
|  |  | ||||||
| 			-- directly read the block to be searched into the chunk cache |  | ||||||
|  |  | ||||||
| 			biome_lib.surface_nodes_no_aircheck.blockhash = |  | ||||||
| 				minetest.find_nodes_in_area(minp, maxp, biome_lib.surfaceslist_no_aircheck) |  | ||||||
| 			biome_lib.actioncount_no_aircheck.blockhash = 1 |  | ||||||
|  |  | ||||||
| 	else | 	else | ||||||
| 			if biome_lib.actioncount_no_aircheck.blockhash <= #biome_lib.actionslist_no_aircheck then | 		-- below, [1] is biome, [2] is the thing to be added | ||||||
| 				biome_lib:populate_surfaces( | 		local added = 0 | ||||||
| 					biome_lib.actionslist_no_aircheck[biome_lib.actioncount_no_aircheck.blockhash][1], | 		if airflag then | ||||||
| 					biome_lib.actionslist_no_aircheck[biome_lib.actioncount_no_aircheck.blockhash][2], | 			if biome_lib.actionslist_aircheck[biome_lib.pos_hash.action_index] then | ||||||
| 					biome_lib.surface_nodes_no_aircheck.blockhash, false) | 				added = biome_lib.populate_surfaces( | ||||||
| 				biome_lib.actioncount_no_aircheck.blockhash = biome_lib.actioncount_no_aircheck.blockhash + 1 | 							biome_lib.actionslist_aircheck[biome_lib.pos_hash.action_index][1], | ||||||
|  | 							biome_lib.actionslist_aircheck[biome_lib.pos_hash.action_index][2], | ||||||
|  | 							biome_lib.pos_hash.surface_node_list, true) | ||||||
|  | 				biome_lib.pos_hash.action_index = biome_lib.pos_hash.action_index + 1 | ||||||
|  | 			end | ||||||
| 		else | 		else | ||||||
| 				if biome_lib.surface_nodes_no_aircheck.blockhash then | 			if biome_lib.actionslist_no_aircheck[biome_lib.pos_hash.action_index] then | ||||||
| 					table.remove(biome_lib.blocklist_no_aircheck, 1) | 				added = biome_lib.populate_surfaces( | ||||||
| 					biome_lib.surface_nodes_no_aircheck.blockhash = nil | 							biome_lib.actionslist_no_aircheck[biome_lib.pos_hash.action_index][1], | ||||||
|  | 							biome_lib.actionslist_no_aircheck[biome_lib.pos_hash.action_index][2], | ||||||
|  | 							biome_lib.pos_hash.surface_node_list, false) | ||||||
|  | 				biome_lib.pos_hash.action_index = biome_lib.pos_hash.action_index + 1 | ||||||
|  | 			end | ||||||
|  | 		end | ||||||
|  | 		if added > 0 then | ||||||
|  | 			biome_lib.dbg("biome_lib.populate_surfaces ran on mapblock at ".. | ||||||
|  | 				minetest.pos_to_string(minp)..".  Entry #".. | ||||||
|  | 				(biome_lib.pos_hash.action_index-1).." added "..added.." items.", 4) | ||||||
| 		end | 		end | ||||||
| 	end | 	end | ||||||
| end | end | ||||||
| 	end |  | ||||||
| end |  | ||||||
|  |  | ||||||
| -- "Record" the chunks being generated by the core mapgen |  | ||||||
|  |  | ||||||
| minetest.register_on_generated(function(minp, maxp, blockseed) |  | ||||||
| 	biome_lib.blocklist_aircheck[#biome_lib.blocklist_aircheck + 1] = { minp, maxp } |  | ||||||
| end) |  | ||||||
|  |  | ||||||
| minetest.register_on_generated(function(minp, maxp, blockseed) |  | ||||||
| 	biome_lib.blocklist_no_aircheck[#biome_lib.blocklist_no_aircheck + 1] = { minp, maxp } |  | ||||||
| end) |  | ||||||
|  |  | ||||||
| -- "Play" them back, populating them with new stuff in the process | -- "Play" them back, populating them with new stuff in the process | ||||||
|  |  | ||||||
| minetest.register_globalstep(function(dtime) | minetest.register_globalstep(function(dtime) | ||||||
| 	if dtime < 0.2    -- don't attempt to populate if lag is already too high | 	if not biome_lib.block_log[1] then return end -- the block log is empty | ||||||
| 	  and math.random(100) <= biome_lib.queue_run_ratio |  | ||||||
| 	  and (#biome_lib.blocklist_aircheck > 0 or #biome_lib.blocklist_no_aircheck > 0) then | 	if math.random(100) > biome_lib.queue_run_ratio then return end | ||||||
| 		biome_lib.globalstep_start_time = minetest.get_us_time() | 	for s = 1, biome_lib.entries_per_step do | ||||||
| 		biome_lib.globalstep_runtime = 0 | 		biome_lib.generate_block() | ||||||
| 		while (#biome_lib.blocklist_aircheck > 0 or #biome_lib.blocklist_no_aircheck > 0) |  | ||||||
| 		  and biome_lib.globalstep_runtime < 200000 do  -- 0.2 seconds, in uS. |  | ||||||
| 			if #biome_lib.blocklist_aircheck > 0 then |  | ||||||
| 				biome_lib:generate_block_with_air_checking() |  | ||||||
| 			end |  | ||||||
| 			if #biome_lib.blocklist_no_aircheck > 0 then |  | ||||||
| 				biome_lib:generate_block_no_aircheck() |  | ||||||
| 			end |  | ||||||
| 			biome_lib.globalstep_runtime = minetest.get_us_time() - biome_lib.globalstep_start_time |  | ||||||
| 		end |  | ||||||
| 	end | 	end | ||||||
| end) | end) | ||||||
|  |  | ||||||
|  | -- Periodically wake-up the queue to give old blocks a chance to time-out | ||||||
|  | -- if the player isn't currently exploring (i.e. they're just playing in one area) | ||||||
|  |  | ||||||
|  | function biome_lib.wake_up_queue() | ||||||
|  | 	if #biome_lib.block_recheck_list > 0 | ||||||
|  | 	  and #biome_lib.block_log == 0 then | ||||||
|  | 		-- we move the second element and not the first because we can't be | ||||||
|  | 		-- sure if the recheck list's first item is the one currently acted upon | ||||||
|  | 		-- (else it'd be the first item in the main block log) | ||||||
|  | 		biome_lib.block_log = table.copy(biome_lib.block_recheck_list) | ||||||
|  | 		biome_lib.block_recheck_list = {} | ||||||
|  | 		biome_lib.queue_idle_flag = false | ||||||
|  | 		biome_lib.dbg("Woke-up the map queue to give old blocks a chance to time-out.", 3) | ||||||
|  | 	end | ||||||
|  | 	minetest.after(biome_lib.block_queue_wakeup_time, biome_lib.wake_up_queue) | ||||||
|  | end | ||||||
|  |  | ||||||
|  | biome_lib.wake_up_queue() | ||||||
|  |  | ||||||
| -- Play out the entire log all at once on shutdown | -- Play out the entire log all at once on shutdown | ||||||
| -- to prevent unpopulated map areas | -- to prevent unpopulated map areas | ||||||
|  |  | ||||||
| minetest.register_on_shutdown(function() | minetest.register_on_shutdown(function() | ||||||
| 	if #biome_lib.blocklist_aircheck > 0 then | 	if #biome_lib.block_log + #biome_lib.block_recheck_list == 0 then | ||||||
| 		print("[biome_lib] Stand by, playing out the rest of the aircheck mapblock log") | 		return | ||||||
| 		print("(there are "..#biome_lib.blocklist_aircheck.." entries)...") |  | ||||||
| 		while true do |  | ||||||
| 			biome_lib:generate_block_with_air_checking(0.1) |  | ||||||
| 			if #biome_lib.blocklist_aircheck == 0 then return end |  | ||||||
| 	end | 	end | ||||||
| 	end |  | ||||||
| end) |  | ||||||
|  |  | ||||||
| minetest.register_on_shutdown(function() | 	biome_lib.dbg("[biome_lib] Stand by, playing out the rest of the mapblock log", 0) | ||||||
| 	if #biome_lib.blocklist_no_aircheck > 0 then | 	biome_lib.dbg("(there are "..(#biome_lib.block_log + #biome_lib.block_recheck_list).." entries)...", 0) | ||||||
| 		print("[biome_lib] Stand by, playing out the rest of the no-aircheck mapblock log") | 	while #biome_lib.block_log > 0 do | ||||||
| 		print("(there are "..#biome_lib.blocklist_no_aircheck.." entries)...") | 		biome_lib.generate_block(true) | ||||||
| 		while true do | 	end | ||||||
| 			biome_lib:generate_block_no_aircheck(0.1) |  | ||||||
| 			if #biome_lib.blocklist_no_aircheck == 0 then return end | 	if #biome_lib.block_recheck_list > 0 then | ||||||
|  | 		biome_lib.block_log = table.copy(biome_lib.block_recheck_list) | ||||||
|  | 		while #biome_lib.block_log > 0 do | ||||||
|  | 			biome_lib.generate_block(true) | ||||||
| 		end | 		end | ||||||
| 	end | 	end | ||||||
| end) | end) | ||||||
| @@ -501,44 +629,94 @@ function biome_lib:spawn_on_surfaces(sd,sp,sr,sc,ss,sa) | |||||||
| 	biome_lib:set_defaults(biome) | 	biome_lib:set_defaults(biome) | ||||||
| 	biome.spawn_plants_count = #(biome.spawn_plants) | 	biome.spawn_plants_count = #(biome.spawn_plants) | ||||||
|  |  | ||||||
|  | 	local n | ||||||
|  | 	if type(biome.spawn_plants) == "table" then | ||||||
|  | 		n = "random: "..biome.spawn_plants[1]..", ..." | ||||||
|  | 	else | ||||||
|  | 		n = biome.spawn_plants | ||||||
|  | 	end | ||||||
|  | 	biome.label = biome.label or "biome_lib spawn_on_surfaces(): "..n | ||||||
|  |  | ||||||
| 	minetest.register_abm({ | 	minetest.register_abm({ | ||||||
| 		nodenames = biome.spawn_surfaces, | 		nodenames = biome.spawn_surfaces, | ||||||
| 		interval = biome.interval, | 		interval = biome.interval, | ||||||
| 		chance = biome.spawn_chance, | 		chance = biome.spawn_chance, | ||||||
| 		neighbors = biome.neighbors, | 		neighbors = biome.neighbors, | ||||||
|  | 		label = biome.label, | ||||||
| 		action = function(pos, node, active_object_count, active_object_count_wider) | 		action = function(pos, node, active_object_count, active_object_count_wider) | ||||||
| 			local p_top = { x = pos.x, y = pos.y + 1, z = pos.z }	 | 			local p_top = { x = pos.x, y = pos.y + 1, z = pos.z }	 | ||||||
| 			local n_top = minetest.get_node(p_top) | 			local n_top = minetest.get_node(p_top) | ||||||
| 			local perlin_fertile_area = minetest.get_perlin(biome.seed_diff, perlin_octaves, perlin_persistence, perlin_scale) | 			local perlin_fertile_area = minetest.get_perlin(biome.seed_diff, perlin_octaves, perlin_persistence, perlin_scale) | ||||||
| 			local noise1 = perlin_fertile_area:get2d({x=p_top.x, y=p_top.z}) |  | ||||||
| 			local noise2 = biome_lib.perlin_temperature:get2d({x=p_top.x, y=p_top.z}) | 			local fertility, temperature, humidity = get_biome_data(pos, perlin_fertile_area) | ||||||
| 			local noise3 = biome_lib.perlin_humidity:get2d({x=p_top.x+150, y=p_top.z+50}) |  | ||||||
| 			if noise1 > biome.plantlife_limit  | 			local pos_biome_ok = pos.y >= biome.min_elevation and pos.y <= biome.max_elevation | ||||||
| 			  and noise2 <= biome.temp_min | 				and fertility > biome.plantlife_limit | ||||||
| 			  and noise2 >= biome.temp_max | 				and temperature <= biome.temp_min and temperature >= biome.temp_max | ||||||
| 			  and noise3 <= biome.humidity_min | 				and humidity <= biome.humidity_min and humidity >= biome.humidity_max | ||||||
| 			  and noise3 >= biome.humidity_max | 				and biome_lib:is_node_loaded(p_top) | ||||||
| 			  and biome_lib:is_node_loaded(p_top) then |  | ||||||
|  | 			if not pos_biome_ok then | ||||||
|  | 				return -- Outside of biome | ||||||
|  | 			end | ||||||
|  |  | ||||||
| 			local n_light = minetest.get_node_light(p_top, nil) | 			local n_light = minetest.get_node_light(p_top, nil) | ||||||
| 				if not (biome.avoid_nodes and biome.avoid_radius and minetest.find_node_near(p_top, biome.avoid_radius + math.random(-1.5,2), biome.avoid_nodes)) | 			if n_light < biome.light_min or n_light > biome.light_max then | ||||||
| 				  and n_light >= biome.light_min | 				return -- Too dark or too bright | ||||||
| 				  and n_light <= biome.light_max | 			end | ||||||
| 				  and (not(biome.neighbors and biome.ncount) or #(minetest.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 ) |  | ||||||
| 				  and (not(biome.near_nodes and biome.near_nodes_count and biome.near_nodes_size) or #(minetest.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) | 			if biome.avoid_nodes and biome.avoid_radius and minetest.find_node_near( | ||||||
| 				  and (not(biome.air_count and biome.air_size) or #(minetest.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) | 					p_top, biome.avoid_radius + math.random(-1.5,2), biome.avoid_nodes) then | ||||||
| 				  and pos.y >= biome.min_elevation | 				return -- Nodes to avoid are nearby | ||||||
| 				  and pos.y <= biome.max_elevation | 			end | ||||||
| 				  then |  | ||||||
|  | 			if biome.neighbors and biome.ncount and | ||||||
|  | 					#minetest.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 then | ||||||
|  | 				return -- Near neighbour nodes are not present | ||||||
|  | 			end | ||||||
|  |  | ||||||
|  | 			local NEAR_DST = biome.near_nodes_size | ||||||
|  | 			if biome.near_nodes and biome.near_nodes_count and biome.near_nodes_size and | ||||||
|  | 					#minetest.find_nodes_in_area( | ||||||
|  | 						{x=pos.x-NEAR_DST, y=pos.y-biome.near_nodes_vertical, z=pos.z-NEAR_DST}, | ||||||
|  | 						{x=pos.x+NEAR_DST, y=pos.y+biome.near_nodes_vertical, z=pos.z+NEAR_DST}, | ||||||
|  | 						biome.near_nodes | ||||||
|  | 					) < biome.near_nodes_count then | ||||||
|  | 				return -- Far neighbour nodes are not present | ||||||
|  | 			end | ||||||
|  |  | ||||||
|  | 			if (biome.air_count and biome.air_size) and | ||||||
|  | 					#minetest.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 then | ||||||
|  | 				return -- Not enough air | ||||||
|  | 			end | ||||||
|  |  | ||||||
| 			local walldir = biome_lib:find_adjacent_wall(p_top, biome.verticals_list, biome.choose_random_wall) | 			local walldir = biome_lib:find_adjacent_wall(p_top, biome.verticals_list, biome.choose_random_wall) | ||||||
| 			if biome.alt_wallnode and walldir then | 			if biome.alt_wallnode and walldir then | ||||||
| 				if n_top.name == "air" then | 				if n_top.name == "air" then | ||||||
| 					minetest.swap_node(p_top, { name = biome.alt_wallnode, param2 = walldir }) | 					minetest.swap_node(p_top, { name = biome.alt_wallnode, param2 = walldir }) | ||||||
| 				end | 				end | ||||||
| 					else | 				return | ||||||
|  | 			end | ||||||
|  |  | ||||||
| 			local currentsurface = minetest.get_node(pos).name | 			local currentsurface = minetest.get_node(pos).name | ||||||
| 						if currentsurface ~= "default:water_source" |  | ||||||
| 						  or (currentsurface == "default:water_source" and #(minetest.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 ) | 			if biome_lib.default_water_nodes[currentsurface] and | ||||||
| 						  then | 					#minetest.find_nodes_in_area( | ||||||
|  | 						{x=pos.x, y=pos.y-biome.depth_max-1, z=pos.z}, | ||||||
|  | 						vector.new(pos), | ||||||
|  | 						biome_lib.default_wet_surfaces | ||||||
|  | 					) == 0 then | ||||||
|  | 				return -- On water but no ground nearby | ||||||
|  | 			end | ||||||
|  |  | ||||||
| 			local rnd = math.random(1, biome.spawn_plants_count) | 			local rnd = math.random(1, biome.spawn_plants_count) | ||||||
| 			local plant_to_spawn = biome.spawn_plants[rnd] | 			local plant_to_spawn = biome.spawn_plants[rnd] | ||||||
| 			local fdir = biome.facedir | 			local fdir = biome.facedir | ||||||
| @@ -565,64 +743,6 @@ function biome_lib:spawn_on_surfaces(sd,sp,sr,sc,ss,sa) | |||||||
| 				end | 				end | ||||||
| 			end | 			end | ||||||
| 		end | 		end | ||||||
| 					end |  | ||||||
| 				end |  | ||||||
| 			end |  | ||||||
| 		end |  | ||||||
| 	}) |  | ||||||
| end |  | ||||||
|  |  | ||||||
| -- The growing ABM |  | ||||||
|  |  | ||||||
| function biome_lib:grow_plants(opts) |  | ||||||
|  |  | ||||||
| 	local options = opts |  | ||||||
|  |  | ||||||
| 	options.height_limit = options.height_limit or 5 |  | ||||||
| 	options.ground_nodes = options.ground_nodes or { "default:dirt_with_grass" } |  | ||||||
| 	options.grow_nodes = options.grow_nodes or { "default:dirt_with_grass" } |  | ||||||
| 	options.seed_diff = options.seed_diff or 0 |  | ||||||
|  |  | ||||||
| 	if options.grow_delay*time_scale >= 1 then |  | ||||||
| 		options.interval = options.grow_delay*time_scale |  | ||||||
| 	else |  | ||||||
| 		options.interval = 1 |  | ||||||
| 	end |  | ||||||
|  |  | ||||||
| 	minetest.register_abm({ |  | ||||||
| 		nodenames = { options.grow_plant }, |  | ||||||
| 		interval = options.interval, |  | ||||||
| 		chance = options.grow_chance, |  | ||||||
| 		action = function(pos, node, active_object_count, active_object_count_wider) |  | ||||||
| 			local p_top = {x=pos.x, y=pos.y+1, z=pos.z} |  | ||||||
| 			local p_bot = {x=pos.x, y=pos.y-1, z=pos.z} |  | ||||||
| 			local n_top = minetest.get_node(p_top) |  | ||||||
| 			local n_bot = minetest.get_node(p_bot) |  | ||||||
| 			local root_node = minetest.get_node({x=pos.x, y=pos.y-options.height_limit, z=pos.z}) |  | ||||||
| 			local walldir = nil |  | ||||||
| 			if options.need_wall and options.verticals_list then |  | ||||||
| 				walldir = biome_lib:find_adjacent_wall(p_top, options.verticals_list, options.choose_random_wall) |  | ||||||
| 			end |  | ||||||
| 			if (n_top.name == "air" or n_top.name == "default:snow") |  | ||||||
| 			  and (not options.need_wall or (options.need_wall and walldir)) then |  | ||||||
| 				-- corner case for changing short junglegrass |  | ||||||
| 				-- to dry shrub in desert |  | ||||||
| 				if n_bot.name == options.dry_early_node and options.grow_plant == "junglegrass:short" then |  | ||||||
| 					minetest.swap_node(pos, { name = "default:dry_shrub" }) |  | ||||||
|  |  | ||||||
| 				elseif options.grow_vertically and walldir then |  | ||||||
| 					if biome_lib:search_downward(pos, options.height_limit, options.ground_nodes) then |  | ||||||
| 						minetest.swap_node(p_top, { name = options.grow_plant, param2 = walldir}) |  | ||||||
| 					end |  | ||||||
|  |  | ||||||
| 				elseif not options.grow_result and not options.grow_function then |  | ||||||
| 					minetest.swap_node(pos, biome_lib.air) |  | ||||||
|  |  | ||||||
| 				else |  | ||||||
| 					biome_lib:replace_object(pos, options.grow_result, options.grow_function, options.facedir, options.seed_diff) |  | ||||||
| 				end |  | ||||||
| 			end |  | ||||||
| 		end |  | ||||||
| 	}) | 	}) | ||||||
| end | end | ||||||
|  |  | ||||||
| @@ -637,15 +757,13 @@ function biome_lib:replace_object(pos, replacement, grow_function, walldir, seed | |||||||
| 		return | 		return | ||||||
| 	elseif growtype == "function" then | 	elseif growtype == "function" then | ||||||
| 		local perlin_fertile_area = minetest.get_perlin(seeddiff, perlin_octaves, perlin_persistence, perlin_scale) | 		local perlin_fertile_area = minetest.get_perlin(seeddiff, perlin_octaves, perlin_persistence, perlin_scale) | ||||||
| 		local noise1 = perlin_fertile_area:get2d({x=pos.x, y=pos.z}) | 		local fertility, temperature, _ = get_biome_data(pos, perlin_fertile_area) | ||||||
| 		local noise2 = biome_lib.perlin_temperature:get2d({x=pos.x, y=pos.z}) | 		grow_function(pos, fertility, temperature, walldir) | ||||||
| 		grow_function(pos,noise1,noise2,walldir) |  | ||||||
| 		return | 		return | ||||||
| 	elseif growtype == "string" then | 	elseif growtype == "string" then | ||||||
| 		local perlin_fertile_area = minetest.get_perlin(seeddiff, perlin_octaves, perlin_persistence, perlin_scale) | 		local perlin_fertile_area = minetest.get_perlin(seeddiff, perlin_octaves, perlin_persistence, perlin_scale) | ||||||
| 		local noise1 = perlin_fertile_area:get2d({x=pos.x, y=pos.z}) | 		local fertility, temperature, _ = get_biome_data(pos, perlin_fertile_area) | ||||||
| 		local noise2 = biome_lib.perlin_temperature:get2d({x=pos.x, y=pos.z}) | 		assert(loadstring(grow_function.."(...)"))(pos, fertility, temperature, walldir) | ||||||
| 		assert(loadstring(grow_function.."(...)"))(pos,noise1,noise2,walldir) |  | ||||||
| 		return | 		return | ||||||
| 	elseif growtype == "nil" then | 	elseif growtype == "nil" then | ||||||
| 		minetest.swap_node(pos, { name = replacement, param2 = walldir}) | 		minetest.swap_node(pos, { name = replacement, param2 = walldir}) | ||||||
| @@ -655,71 +773,8 @@ function biome_lib:replace_object(pos, replacement, grow_function, walldir, seed | |||||||
| 	end | 	end | ||||||
| end | end | ||||||
|  |  | ||||||
| -- function to decide if a node has a wall that's in verticals_list{} | dofile(biome_lib.modpath .. "/search_functions.lua") | ||||||
| -- returns wall direction of valid node, or nil if invalid. | assert(loadfile(biome_lib.modpath .. "/growth.lua"))(time_scale) | ||||||
|  |  | ||||||
| function biome_lib:find_adjacent_wall(pos, verticals, randomflag) |  | ||||||
| 	local verts = dump(verticals) |  | ||||||
| 	if randomflag then |  | ||||||
| 		local walltab = {} |  | ||||||
| 		 |  | ||||||
| 		if string.find(verts, minetest.get_node({ x=pos.x-1, y=pos.y, z=pos.z   }).name) then walltab[#walltab + 1] = 3 end |  | ||||||
| 		if string.find(verts, minetest.get_node({ x=pos.x+1, y=pos.y, z=pos.z   }).name) then walltab[#walltab + 1] = 2 end |  | ||||||
| 		if string.find(verts, minetest.get_node({ x=pos.x  , y=pos.y, z=pos.z-1 }).name) then walltab[#walltab + 1] = 5 end |  | ||||||
| 		if string.find(verts, minetest.get_node({ x=pos.x  , y=pos.y, z=pos.z+1 }).name) then walltab[#walltab + 1] = 4 end |  | ||||||
|  |  | ||||||
| 		if #walltab > 0 then return walltab[math.random(1, #walltab)] end |  | ||||||
|  |  | ||||||
| 	else |  | ||||||
| 		if string.find(verts, minetest.get_node({ x=pos.x-1, y=pos.y, z=pos.z   }).name) then return 3 end |  | ||||||
| 		if string.find(verts, minetest.get_node({ x=pos.x+1, y=pos.y, z=pos.z   }).name) then return 2 end |  | ||||||
| 		if string.find(verts, minetest.get_node({ x=pos.x  , y=pos.y, z=pos.z-1 }).name) then return 5 end |  | ||||||
| 		if string.find(verts, minetest.get_node({ x=pos.x  , y=pos.y, z=pos.z+1 }).name) then return 4 end |  | ||||||
| 	end |  | ||||||
| 	return nil |  | ||||||
| end |  | ||||||
|  |  | ||||||
| -- Function to search downward from the given position, looking for the first |  | ||||||
| -- node that matches the ground table.  Returns the new position, or nil if |  | ||||||
| -- height limit is exceeded before finding it. |  | ||||||
|  |  | ||||||
| function biome_lib:search_downward(pos, heightlimit, ground) |  | ||||||
| 	for i = 0, heightlimit do |  | ||||||
| 		if string.find(dump(ground), minetest.get_node({x=pos.x, y=pos.y-i, z = pos.z}).name) then |  | ||||||
| 			return {x=pos.x, y=pos.y-i, z = pos.z} |  | ||||||
| 		end |  | ||||||
| 	end |  | ||||||
| 	return false |  | ||||||
| end |  | ||||||
|  |  | ||||||
| function biome_lib:find_open_side(pos) |  | ||||||
| 	if minetest.get_node({ x=pos.x-1, y=pos.y, z=pos.z }).name == "air" then |  | ||||||
| 		return {newpos = { x=pos.x-1, y=pos.y, z=pos.z }, facedir = 2} |  | ||||||
| 	end |  | ||||||
| 	if minetest.get_node({ x=pos.x+1, y=pos.y, z=pos.z }).name == "air" then |  | ||||||
| 		return {newpos = { x=pos.x+1, y=pos.y, z=pos.z }, facedir = 3} |  | ||||||
| 	end |  | ||||||
| 	if minetest.get_node({ x=pos.x, y=pos.y, z=pos.z-1 }).name == "air" then |  | ||||||
| 		return {newpos = { x=pos.x, y=pos.y, z=pos.z-1 }, facedir = 4} |  | ||||||
| 	end |  | ||||||
| 	if minetest.get_node({ x=pos.x, y=pos.y, z=pos.z+1 }).name == "air" then |  | ||||||
| 		return {newpos = { x=pos.x, y=pos.y, z=pos.z+1 }, facedir = 5} |  | ||||||
| 	end |  | ||||||
| 	return nil |  | ||||||
| end |  | ||||||
|  |  | ||||||
| -- spawn_tree() on generate is routed through here so that other mods can hook |  | ||||||
| -- into it. |  | ||||||
|  |  | ||||||
| function biome_lib:generate_tree(pos, nodes_or_function_or_model) |  | ||||||
| 	minetest.spawn_tree(pos, nodes_or_function_or_model) |  | ||||||
| end |  | ||||||
|  |  | ||||||
| -- and this one's for the call used in the growing code |  | ||||||
|  |  | ||||||
| function biome_lib:grow_tree(pos, nodes_or_function_or_model) |  | ||||||
| 	minetest.spawn_tree(pos, nodes_or_function_or_model) |  | ||||||
| end |  | ||||||
|  |  | ||||||
| -- Check for infinite stacks | -- Check for infinite stacks | ||||||
|  |  | ||||||
| @@ -738,10 +793,34 @@ function biome_lib:get_nodedef_field(nodename, fieldname) | |||||||
| 	return minetest.registered_nodes[nodename][fieldname] | 	return minetest.registered_nodes[nodename][fieldname] | ||||||
| end | end | ||||||
|  |  | ||||||
| print("[Biome Lib] Loaded") | if biome_lib.debug_log_level >= 3 then | ||||||
|  | 	biome_lib.last_count = 0 | ||||||
|  |  | ||||||
|  | 	function biome_lib.show_pending_block_count() | ||||||
|  | 		if biome_lib.last_count ~= #biome_lib.block_log then | ||||||
|  | 			biome_lib.dbg(string.format("Pending block counts - ready to process: %-8icurrently deferred: %i", | ||||||
|  | 				#biome_lib.block_log, #biome_lib.block_recheck_list), 3) | ||||||
|  | 			biome_lib.last_count = #biome_lib.block_log | ||||||
|  | 			biome_lib.queue_idle_flag = false | ||||||
|  | 		elseif not biome_lib.queue_idle_flag then | ||||||
|  | 			if #biome_lib.block_recheck_list > 0 then | ||||||
|  | 				biome_lib.dbg("Mapblock queue only contains blocks that can't yet be processed.",  3) | ||||||
|  | 				biome_lib.dbg("Idling the queue until new blocks arrive or the next wake-up call occurs.",  3) | ||||||
|  | 			else | ||||||
|  | 				biome_lib.dbg("Mapblock queue has run dry.",  3) | ||||||
|  | 				biome_lib.dbg("Idling the queue until new blocks arrive.",  3) | ||||||
|  | 			end | ||||||
|  | 			biome_lib.queue_idle_flag = true | ||||||
|  | 		end | ||||||
|  | 		minetest.after(1, biome_lib.show_pending_block_count) | ||||||
|  | 	end | ||||||
|  |  | ||||||
|  | 	biome_lib.show_pending_block_count() | ||||||
|  | end | ||||||
|  |  | ||||||
| minetest.after(0, function() | minetest.after(0, function() | ||||||
| 	print("[Biome Lib] Registered a total of "..(#biome_lib.surfaceslist_aircheck)+(#biome_lib.surfaceslist_no_aircheck).." surface types to be evaluated, spread") | 	biome_lib.dbg("Registered a total of "..(#biome_lib.surfaceslist_aircheck)+(#biome_lib.surfaceslist_no_aircheck).." surface types to be evaluated, spread", 0) | ||||||
| 	print("[Biome Lib] across "..#biome_lib.actionslist_aircheck.." actions with air-checking and "..#biome_lib.actionslist_no_aircheck.." actions without.") | 	biome_lib.dbg("across "..#biome_lib.actionslist_aircheck.." actions with air-checking and "..#biome_lib.actionslist_no_aircheck.." actions without.", 0) | ||||||
| end) | end) | ||||||
|  |  | ||||||
|  | biome_lib.dbg("[Biome Lib] Loaded", 0) | ||||||
|   | |||||||
							
								
								
									
										2
									
								
								mod.conf
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								mod.conf
									
									
									
									
									
								
							| @@ -1 +1,3 @@ | |||||||
| name = biome_lib | name = biome_lib | ||||||
|  | min_minetest_version = 5.2.0 | ||||||
|  | optional_depends = default, intllib | ||||||
|   | |||||||
							
								
								
									
										75
									
								
								search_functions.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										75
									
								
								search_functions.lua
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,75 @@ | |||||||
|  |  | ||||||
|  | -- function to decide if a node has a wall that's in verticals_list{} | ||||||
|  | -- returns wall direction of valid node, or nil if invalid. | ||||||
|  |  | ||||||
|  | function biome_lib:find_adjacent_wall(pos, verticals, randomflag) | ||||||
|  | 	local verts = dump(verticals) | ||||||
|  | 	if randomflag then | ||||||
|  | 		local walltab = {} | ||||||
|  | 		 | ||||||
|  | 		if string.find(verts, minetest.get_node({ x=pos.x-1, y=pos.y, z=pos.z   }).name) then walltab[#walltab + 1] = 3 end | ||||||
|  | 		if string.find(verts, minetest.get_node({ x=pos.x+1, y=pos.y, z=pos.z   }).name) then walltab[#walltab + 1] = 2 end | ||||||
|  | 		if string.find(verts, minetest.get_node({ x=pos.x  , y=pos.y, z=pos.z-1 }).name) then walltab[#walltab + 1] = 5 end | ||||||
|  | 		if string.find(verts, minetest.get_node({ x=pos.x  , y=pos.y, z=pos.z+1 }).name) then walltab[#walltab + 1] = 4 end | ||||||
|  |  | ||||||
|  | 		if #walltab > 0 then return walltab[math.random(1, #walltab)] end | ||||||
|  |  | ||||||
|  | 	else | ||||||
|  | 		if string.find(verts, minetest.get_node({ x=pos.x-1, y=pos.y, z=pos.z   }).name) then return 3 end | ||||||
|  | 		if string.find(verts, minetest.get_node({ x=pos.x+1, y=pos.y, z=pos.z   }).name) then return 2 end | ||||||
|  | 		if string.find(verts, minetest.get_node({ x=pos.x  , y=pos.y, z=pos.z-1 }).name) then return 5 end | ||||||
|  | 		if string.find(verts, minetest.get_node({ x=pos.x  , y=pos.y, z=pos.z+1 }).name) then return 4 end | ||||||
|  | 	end | ||||||
|  | 	return nil | ||||||
|  | end | ||||||
|  |  | ||||||
|  | -- Function to search downward from the given position, looking for the first | ||||||
|  | -- node that matches the ground table.  Returns the new position, or nil if | ||||||
|  | -- height limit is exceeded before finding it. | ||||||
|  |  | ||||||
|  | function biome_lib:search_downward(pos, heightlimit, ground) | ||||||
|  | 	for i = 0, heightlimit do | ||||||
|  | 		if string.find(dump(ground), minetest.get_node({x=pos.x, y=pos.y-i, z = pos.z}).name) then | ||||||
|  | 			return {x=pos.x, y=pos.y-i, z = pos.z} | ||||||
|  | 		end | ||||||
|  | 	end | ||||||
|  | 	return false | ||||||
|  | end | ||||||
|  |  | ||||||
|  | function biome_lib:find_open_side(pos) | ||||||
|  | 	if minetest.get_node({ x=pos.x-1, y=pos.y, z=pos.z }).name == "air" then | ||||||
|  | 		return {newpos = { x=pos.x-1, y=pos.y, z=pos.z }, facedir = 2} | ||||||
|  | 	end | ||||||
|  | 	if minetest.get_node({ x=pos.x+1, y=pos.y, z=pos.z }).name == "air" then | ||||||
|  | 		return {newpos = { x=pos.x+1, y=pos.y, z=pos.z }, facedir = 3} | ||||||
|  | 	end | ||||||
|  | 	if minetest.get_node({ x=pos.x, y=pos.y, z=pos.z-1 }).name == "air" then | ||||||
|  | 		return {newpos = { x=pos.x, y=pos.y, z=pos.z-1 }, facedir = 4} | ||||||
|  | 	end | ||||||
|  | 	if minetest.get_node({ x=pos.x, y=pos.y, z=pos.z+1 }).name == "air" then | ||||||
|  | 		return {newpos = { x=pos.x, y=pos.y, z=pos.z+1 }, facedir = 5} | ||||||
|  | 	end | ||||||
|  | 	return nil | ||||||
|  | end | ||||||
|  |  | ||||||
|  | -- "Record" the map chunks being generated by the core mapgen, | ||||||
|  | -- split into individual mapblocks to reduce lag | ||||||
|  |  | ||||||
|  | minetest.register_on_generated(function(minp, maxp, blockseed) | ||||||
|  | 	local timestamp = minetest.get_us_time() | ||||||
|  | 	for x = 0, 4 do | ||||||
|  | 		local minx = minp.x + x*16 | ||||||
|  | 		for y = 0, 4 do | ||||||
|  | 			local miny = minp.y + y*16 | ||||||
|  | 			for z = 0, 4 do | ||||||
|  | 				local minz = minp.z + z*16 | ||||||
|  |  | ||||||
|  | 				local bmin = {x=minx, y=miny, z=minz} | ||||||
|  | 				local bmax = {x=minx + 15, y=miny + 15, z=minz + 15} | ||||||
|  | 				biome_lib.block_log[#biome_lib.block_log + 1] = { bmin, bmax, true, timestamp } | ||||||
|  | 				biome_lib.block_log[#biome_lib.block_log + 1] = { bmin, bmax, false, timestamp } | ||||||
|  | 			end | ||||||
|  | 		end | ||||||
|  | 	end | ||||||
|  | 	biome_lib.run_block_recheck_list = true | ||||||
|  | end) | ||||||
		Reference in New Issue
	
	Block a user