mirror of
				https://github.com/luanti-org/luanti.git
				synced 2025-11-04 01:05:48 +01:00 
			
		
		
		
	Lua on each mapgen thread (#13092)
This commit is contained in:
		
							
								
								
									
										32
									
								
								games/devtest/mods/unittests/inside_mapgen_env.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								games/devtest/mods/unittests/inside_mapgen_env.lua
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
core.log("info", "Hello World")
 | 
			
		||||
 | 
			
		||||
local function do_tests()
 | 
			
		||||
	assert(core == minetest)
 | 
			
		||||
	-- stuff that should not be here
 | 
			
		||||
	assert(not core.get_player_by_name)
 | 
			
		||||
	assert(not core.object_refs)
 | 
			
		||||
	-- stuff that should be here
 | 
			
		||||
	assert(core.register_on_generated)
 | 
			
		||||
	assert(core.get_node)
 | 
			
		||||
	assert(core.spawn_tree)
 | 
			
		||||
	assert(ItemStack)
 | 
			
		||||
	local meta = ItemStack():get_meta()
 | 
			
		||||
	assert(type(meta) == "userdata")
 | 
			
		||||
	assert(type(meta.set_tool_capabilities) == "function")
 | 
			
		||||
	assert(core.registered_items[""])
 | 
			
		||||
	assert(core.save_gen_notify)
 | 
			
		||||
	-- alias handling
 | 
			
		||||
	assert(core.registered_items["unittests:steel_ingot_alias"].name ==
 | 
			
		||||
		"unittests:steel_ingot")
 | 
			
		||||
	-- fallback to item defaults
 | 
			
		||||
	assert(core.registered_items["unittests:description_test"].on_place == true)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
-- there's no (usable) communcation path between mapgen and the regular env
 | 
			
		||||
-- so we just run the test unconditionally
 | 
			
		||||
do_tests()
 | 
			
		||||
 | 
			
		||||
core.register_on_generated(function(vm, pos1, pos2, blockseed)
 | 
			
		||||
	local n = tonumber(core.get_mapgen_setting("chunksize")) * 16 - 1
 | 
			
		||||
	assert(pos2:subtract(pos1) == vector.new(n, n, n))
 | 
			
		||||
end)
 | 
			
		||||
@@ -1,3 +1,6 @@
 | 
			
		||||
core.register_mapgen_script(core.get_modpath(core.get_current_modname()) ..
 | 
			
		||||
	DIR_DELIM .. "inside_mapgen_env.lua")
 | 
			
		||||
 | 
			
		||||
local function test_pseudo_random()
 | 
			
		||||
	-- We have comprehensive unit tests in C++, this is just to make sure the API code isn't messing up
 | 
			
		||||
	local gen1 = PseudoRandom(13)
 | 
			
		||||
@@ -204,3 +207,30 @@ local function test_on_mapblocks_changed(cb, player, pos)
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
unittests.register("test_on_mapblocks_changed", test_on_mapblocks_changed, {map=true, async=true})
 | 
			
		||||
 | 
			
		||||
local function test_gennotify_api()
 | 
			
		||||
	local DECO_ID = 123
 | 
			
		||||
	local UD_ID = "unittests:dummy"
 | 
			
		||||
 | 
			
		||||
	-- the engine doesn't check if the id is actually valid, maybe it should
 | 
			
		||||
	core.set_gen_notify({decoration=true}, {DECO_ID})
 | 
			
		||||
 | 
			
		||||
	core.set_gen_notify({custom=true}, nil, {UD_ID})
 | 
			
		||||
 | 
			
		||||
	local flags, deco, custom = core.get_gen_notify()
 | 
			
		||||
	local function ff(flag)
 | 
			
		||||
		return (" " .. flags .. " "):match("[ ,]" .. flag .. "[ ,]") ~= nil
 | 
			
		||||
	end
 | 
			
		||||
	assert(ff("decoration"), "'decoration' flag missing")
 | 
			
		||||
	assert(ff("custom"), "'custom' flag missing")
 | 
			
		||||
	assert(table.indexof(deco, DECO_ID) > 0)
 | 
			
		||||
	assert(table.indexof(custom, UD_ID) > 0)
 | 
			
		||||
 | 
			
		||||
	core.set_gen_notify({decoration=false, custom=false})
 | 
			
		||||
 | 
			
		||||
	flags, deco, custom = core.get_gen_notify()
 | 
			
		||||
	assert(not ff("decoration") and not ff("custom"))
 | 
			
		||||
	assert(#deco == 0, "deco ids not empty")
 | 
			
		||||
	assert(#custom == 0, "custom ids not empty")
 | 
			
		||||
end
 | 
			
		||||
unittests.register("test_gennotify_api", test_gennotify_api)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user