mirror of
				https://github.com/luanti-org/luanti.git
				synced 2025-10-30 23:15:32 +01:00 
			
		
		
		
	Reorganize some builtin functions in preparation for async env
This commit is contained in:
		| @@ -8,6 +8,7 @@ local gamepath   = scriptpath .. "game".. DIR_DELIM | ||||
| local builtin_shared = {} | ||||
|  | ||||
| dofile(gamepath .. "constants.lua") | ||||
| dofile(gamepath .. "item_s.lua") | ||||
| assert(loadfile(gamepath .. "item.lua"))(builtin_shared) | ||||
| dofile(gamepath .. "register.lua") | ||||
|  | ||||
| @@ -18,6 +19,7 @@ end | ||||
| dofile(commonpath .. "after.lua") | ||||
| dofile(gamepath .. "item_entity.lua") | ||||
| dofile(gamepath .. "deprecated.lua") | ||||
| dofile(gamepath .. "misc_s.lua") | ||||
| dofile(gamepath .. "misc.lua") | ||||
| dofile(gamepath .. "privileges.lua") | ||||
| dofile(gamepath .. "auth.lua") | ||||
|   | ||||
| @@ -15,15 +15,6 @@ end | ||||
| -- Item definition helpers | ||||
| -- | ||||
|  | ||||
| function core.inventorycube(img1, img2, img3) | ||||
| 	img2 = img2 or img1 | ||||
| 	img3 = img3 or img1 | ||||
| 	return "[inventorycube" | ||||
| 			.. "{" .. img1:gsub("%^", "&") | ||||
| 			.. "{" .. img2:gsub("%^", "&") | ||||
| 			.. "{" .. img3:gsub("%^", "&") | ||||
| end | ||||
|  | ||||
| function core.get_pointed_thing_position(pointed_thing, above) | ||||
| 	if pointed_thing.type == "node" then | ||||
| 		if above then | ||||
| @@ -37,144 +28,6 @@ function core.get_pointed_thing_position(pointed_thing, above) | ||||
| 	end | ||||
| end | ||||
|  | ||||
| function core.dir_to_facedir(dir, is6d) | ||||
| 	--account for y if requested | ||||
| 	if is6d and math.abs(dir.y) > math.abs(dir.x) and math.abs(dir.y) > math.abs(dir.z) then | ||||
|  | ||||
| 		--from above | ||||
| 		if dir.y < 0 then | ||||
| 			if math.abs(dir.x) > math.abs(dir.z) then | ||||
| 				if dir.x < 0 then | ||||
| 					return 19 | ||||
| 				else | ||||
| 					return 13 | ||||
| 				end | ||||
| 			else | ||||
| 				if dir.z < 0 then | ||||
| 					return 10 | ||||
| 				else | ||||
| 					return 4 | ||||
| 				end | ||||
| 			end | ||||
|  | ||||
| 		--from below | ||||
| 		else | ||||
| 			if math.abs(dir.x) > math.abs(dir.z) then | ||||
| 				if dir.x < 0 then | ||||
| 					return 15 | ||||
| 				else | ||||
| 					return 17 | ||||
| 				end | ||||
| 			else | ||||
| 				if dir.z < 0 then | ||||
| 					return 6 | ||||
| 				else | ||||
| 					return 8 | ||||
| 				end | ||||
| 			end | ||||
| 		end | ||||
|  | ||||
| 	--otherwise, place horizontally | ||||
| 	elseif math.abs(dir.x) > math.abs(dir.z) then | ||||
| 		if dir.x < 0 then | ||||
| 			return 3 | ||||
| 		else | ||||
| 			return 1 | ||||
| 		end | ||||
| 	else | ||||
| 		if dir.z < 0 then | ||||
| 			return 2 | ||||
| 		else | ||||
| 			return 0 | ||||
| 		end | ||||
| 	end | ||||
| end | ||||
|  | ||||
| -- Table of possible dirs | ||||
| local facedir_to_dir = { | ||||
| 	vector.new( 0,  0,  1), | ||||
| 	vector.new( 1,  0,  0), | ||||
| 	vector.new( 0,  0, -1), | ||||
| 	vector.new(-1,  0,  0), | ||||
| 	vector.new( 0, -1,  0), | ||||
| 	vector.new( 0,  1,  0), | ||||
| } | ||||
| -- Mapping from facedir value to index in facedir_to_dir. | ||||
| local facedir_to_dir_map = { | ||||
| 	[0]=1, 2, 3, 4, | ||||
| 	5, 2, 6, 4, | ||||
| 	6, 2, 5, 4, | ||||
| 	1, 5, 3, 6, | ||||
| 	1, 6, 3, 5, | ||||
| 	1, 4, 3, 2, | ||||
| } | ||||
| function core.facedir_to_dir(facedir) | ||||
| 	return facedir_to_dir[facedir_to_dir_map[facedir % 32]] | ||||
| end | ||||
|  | ||||
| function core.dir_to_wallmounted(dir) | ||||
| 	if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then | ||||
| 		if dir.y < 0 then | ||||
| 			return 1 | ||||
| 		else | ||||
| 			return 0 | ||||
| 		end | ||||
| 	elseif math.abs(dir.x) > math.abs(dir.z) then | ||||
| 		if dir.x < 0 then | ||||
| 			return 3 | ||||
| 		else | ||||
| 			return 2 | ||||
| 		end | ||||
| 	else | ||||
| 		if dir.z < 0 then | ||||
| 			return 5 | ||||
| 		else | ||||
| 			return 4 | ||||
| 		end | ||||
| 	end | ||||
| end | ||||
|  | ||||
| -- table of dirs in wallmounted order | ||||
| local wallmounted_to_dir = { | ||||
| 	[0] = vector.new( 0,  1,  0), | ||||
| 	vector.new( 0, -1,  0), | ||||
| 	vector.new( 1,  0,  0), | ||||
| 	vector.new(-1,  0,  0), | ||||
| 	vector.new( 0,  0,  1), | ||||
| 	vector.new( 0,  0, -1), | ||||
| } | ||||
| function core.wallmounted_to_dir(wallmounted) | ||||
| 	return wallmounted_to_dir[wallmounted % 8] | ||||
| end | ||||
|  | ||||
| function core.dir_to_yaw(dir) | ||||
| 	return -math.atan2(dir.x, dir.z) | ||||
| end | ||||
|  | ||||
| function core.yaw_to_dir(yaw) | ||||
| 	return vector.new(-math.sin(yaw), 0, math.cos(yaw)) | ||||
| end | ||||
|  | ||||
| function core.is_colored_paramtype(ptype) | ||||
| 	return (ptype == "color") or (ptype == "colorfacedir") or | ||||
| 		(ptype == "colorwallmounted") or (ptype == "colordegrotate") | ||||
| end | ||||
|  | ||||
| function core.strip_param2_color(param2, paramtype2) | ||||
| 	if not core.is_colored_paramtype(paramtype2) then | ||||
| 		return nil | ||||
| 	end | ||||
| 	if paramtype2 == "colorfacedir" then | ||||
| 		param2 = math.floor(param2 / 32) * 32 | ||||
| 	elseif paramtype2 == "colorwallmounted" then | ||||
| 		param2 = math.floor(param2 / 8) * 8 | ||||
| 	elseif paramtype2 == "colordegrotate" then | ||||
| 		param2 = math.floor(param2 / 32) * 32 | ||||
| 	end | ||||
| 	-- paramtype2 == "color" requires no modification. | ||||
| 	return param2 | ||||
| end | ||||
|  | ||||
| local function has_all_groups(tbl, required_groups) | ||||
| 	if type(required_groups) == "string" then | ||||
| 		return (tbl[required_groups] or 0) ~= 0 | ||||
|   | ||||
							
								
								
									
										156
									
								
								builtin/game/item_s.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										156
									
								
								builtin/game/item_s.lua
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,156 @@ | ||||
| -- Minetest: builtin/item_s.lua | ||||
| -- The distinction of what goes here is a bit tricky, basically it's everything | ||||
| -- that does not (directly or indirectly) need access to ServerEnvironment, | ||||
| -- Server or writable access to IGameDef on the engine side. | ||||
| -- (The '_s' stands for standalone.) | ||||
|  | ||||
| -- | ||||
| -- Item definition helpers | ||||
| -- | ||||
|  | ||||
| function core.inventorycube(img1, img2, img3) | ||||
| 	img2 = img2 or img1 | ||||
| 	img3 = img3 or img1 | ||||
| 	return "[inventorycube" | ||||
| 			.. "{" .. img1:gsub("%^", "&") | ||||
| 			.. "{" .. img2:gsub("%^", "&") | ||||
| 			.. "{" .. img3:gsub("%^", "&") | ||||
| end | ||||
|  | ||||
| function core.dir_to_facedir(dir, is6d) | ||||
| 	--account for y if requested | ||||
| 	if is6d and math.abs(dir.y) > math.abs(dir.x) and math.abs(dir.y) > math.abs(dir.z) then | ||||
|  | ||||
| 		--from above | ||||
| 		if dir.y < 0 then | ||||
| 			if math.abs(dir.x) > math.abs(dir.z) then | ||||
| 				if dir.x < 0 then | ||||
| 					return 19 | ||||
| 				else | ||||
| 					return 13 | ||||
| 				end | ||||
| 			else | ||||
| 				if dir.z < 0 then | ||||
| 					return 10 | ||||
| 				else | ||||
| 					return 4 | ||||
| 				end | ||||
| 			end | ||||
|  | ||||
| 		--from below | ||||
| 		else | ||||
| 			if math.abs(dir.x) > math.abs(dir.z) then | ||||
| 				if dir.x < 0 then | ||||
| 					return 15 | ||||
| 				else | ||||
| 					return 17 | ||||
| 				end | ||||
| 			else | ||||
| 				if dir.z < 0 then | ||||
| 					return 6 | ||||
| 				else | ||||
| 					return 8 | ||||
| 				end | ||||
| 			end | ||||
| 		end | ||||
|  | ||||
| 	--otherwise, place horizontally | ||||
| 	elseif math.abs(dir.x) > math.abs(dir.z) then | ||||
| 		if dir.x < 0 then | ||||
| 			return 3 | ||||
| 		else | ||||
| 			return 1 | ||||
| 		end | ||||
| 	else | ||||
| 		if dir.z < 0 then | ||||
| 			return 2 | ||||
| 		else | ||||
| 			return 0 | ||||
| 		end | ||||
| 	end | ||||
| end | ||||
|  | ||||
| -- Table of possible dirs | ||||
| local facedir_to_dir = { | ||||
| 	vector.new( 0,  0,  1), | ||||
| 	vector.new( 1,  0,  0), | ||||
| 	vector.new( 0,  0, -1), | ||||
| 	vector.new(-1,  0,  0), | ||||
| 	vector.new( 0, -1,  0), | ||||
| 	vector.new( 0,  1,  0), | ||||
| } | ||||
| -- Mapping from facedir value to index in facedir_to_dir. | ||||
| local facedir_to_dir_map = { | ||||
| 	[0]=1, 2, 3, 4, | ||||
| 	5, 2, 6, 4, | ||||
| 	6, 2, 5, 4, | ||||
| 	1, 5, 3, 6, | ||||
| 	1, 6, 3, 5, | ||||
| 	1, 4, 3, 2, | ||||
| } | ||||
| function core.facedir_to_dir(facedir) | ||||
| 	return facedir_to_dir[facedir_to_dir_map[facedir % 32]] | ||||
| end | ||||
|  | ||||
| function core.dir_to_wallmounted(dir) | ||||
| 	if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then | ||||
| 		if dir.y < 0 then | ||||
| 			return 1 | ||||
| 		else | ||||
| 			return 0 | ||||
| 		end | ||||
| 	elseif math.abs(dir.x) > math.abs(dir.z) then | ||||
| 		if dir.x < 0 then | ||||
| 			return 3 | ||||
| 		else | ||||
| 			return 2 | ||||
| 		end | ||||
| 	else | ||||
| 		if dir.z < 0 then | ||||
| 			return 5 | ||||
| 		else | ||||
| 			return 4 | ||||
| 		end | ||||
| 	end | ||||
| end | ||||
|  | ||||
| -- table of dirs in wallmounted order | ||||
| local wallmounted_to_dir = { | ||||
| 	[0] = vector.new( 0,  1,  0), | ||||
| 	vector.new( 0, -1,  0), | ||||
| 	vector.new( 1,  0,  0), | ||||
| 	vector.new(-1,  0,  0), | ||||
| 	vector.new( 0,  0,  1), | ||||
| 	vector.new( 0,  0, -1), | ||||
| } | ||||
| function core.wallmounted_to_dir(wallmounted) | ||||
| 	return wallmounted_to_dir[wallmounted % 8] | ||||
| end | ||||
|  | ||||
| function core.dir_to_yaw(dir) | ||||
| 	return -math.atan2(dir.x, dir.z) | ||||
| end | ||||
|  | ||||
| function core.yaw_to_dir(yaw) | ||||
| 	return vector.new(-math.sin(yaw), 0, math.cos(yaw)) | ||||
| end | ||||
|  | ||||
| function core.is_colored_paramtype(ptype) | ||||
| 	return (ptype == "color") or (ptype == "colorfacedir") or | ||||
| 		(ptype == "colorwallmounted") or (ptype == "colordegrotate") | ||||
| end | ||||
|  | ||||
| function core.strip_param2_color(param2, paramtype2) | ||||
| 	if not core.is_colored_paramtype(paramtype2) then | ||||
| 		return nil | ||||
| 	end | ||||
| 	if paramtype2 == "colorfacedir" then | ||||
| 		param2 = math.floor(param2 / 32) * 32 | ||||
| 	elseif paramtype2 == "colorwallmounted" then | ||||
| 		param2 = math.floor(param2 / 8) * 8 | ||||
| 	elseif paramtype2 == "colordegrotate" then | ||||
| 		param2 = math.floor(param2 / 32) * 32 | ||||
| 	end | ||||
| 	-- paramtype2 == "color" requires no modification. | ||||
| 	return param2 | ||||
| end | ||||
| @@ -121,53 +121,6 @@ function core.get_player_radius_area(player_name, radius) | ||||
| end | ||||
|  | ||||
|  | ||||
| function core.hash_node_position(pos) | ||||
| 	return (pos.z + 32768) * 65536 * 65536 | ||||
| 		 + (pos.y + 32768) * 65536 | ||||
| 		 +  pos.x + 32768 | ||||
| end | ||||
|  | ||||
|  | ||||
| function core.get_position_from_hash(hash) | ||||
| 	local x = (hash % 65536) - 32768 | ||||
| 	hash  = math.floor(hash / 65536) | ||||
| 	local y = (hash % 65536) - 32768 | ||||
| 	hash  = math.floor(hash / 65536) | ||||
| 	local z = (hash % 65536) - 32768 | ||||
| 	return vector.new(x, y, z) | ||||
| end | ||||
|  | ||||
|  | ||||
| function core.get_item_group(name, group) | ||||
| 	if not core.registered_items[name] or not | ||||
| 			core.registered_items[name].groups[group] then | ||||
| 		return 0 | ||||
| 	end | ||||
| 	return core.registered_items[name].groups[group] | ||||
| end | ||||
|  | ||||
|  | ||||
| function core.get_node_group(name, group) | ||||
| 	core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead") | ||||
| 	return core.get_item_group(name, group) | ||||
| end | ||||
|  | ||||
|  | ||||
| function core.setting_get_pos(name) | ||||
| 	local value = core.settings:get(name) | ||||
| 	if not value then | ||||
| 		return nil | ||||
| 	end | ||||
| 	return core.string_to_pos(value) | ||||
| end | ||||
|  | ||||
|  | ||||
| -- See l_env.cpp for the other functions | ||||
| function core.get_artificial_light(param1) | ||||
| 	return math.floor(param1 / 16) | ||||
| end | ||||
|  | ||||
|  | ||||
| -- To be overriden by protection mods | ||||
|  | ||||
| function core.is_protected(pos, name) | ||||
| @@ -282,42 +235,3 @@ end | ||||
|  | ||||
| -- Used for callback handling with dynamic_add_media | ||||
| core.dynamic_media_callbacks = {} | ||||
|  | ||||
|  | ||||
| -- PNG encoder safety wrapper | ||||
|  | ||||
| local o_encode_png = core.encode_png | ||||
| function core.encode_png(width, height, data, compression) | ||||
| 	if type(width) ~= "number" then | ||||
| 		error("Incorrect type for 'width', expected number, got " .. type(width)) | ||||
| 	end | ||||
| 	if type(height) ~= "number" then | ||||
| 		error("Incorrect type for 'height', expected number, got " .. type(height)) | ||||
| 	end | ||||
|  | ||||
| 	local expected_byte_count = width * height * 4 | ||||
|  | ||||
| 	if type(data) ~= "table" and type(data) ~= "string" then | ||||
| 		error("Incorrect type for 'data', expected table or string, got " .. type(data)) | ||||
| 	end | ||||
|  | ||||
| 	local data_length = type(data) == "table" and #data * 4 or string.len(data) | ||||
|  | ||||
| 	if data_length ~= expected_byte_count then | ||||
| 		error(string.format( | ||||
| 			"Incorrect length of 'data', width and height imply %d bytes but %d were provided", | ||||
| 			expected_byte_count, | ||||
| 			data_length | ||||
| 		)) | ||||
| 	end | ||||
|  | ||||
| 	if type(data) == "table" then | ||||
| 		local dataBuf = {} | ||||
| 		for i = 1, #data do | ||||
| 			dataBuf[i] = core.colorspec_to_bytes(data[i]) | ||||
| 		end | ||||
| 		data = table.concat(dataBuf) | ||||
| 	end | ||||
|  | ||||
| 	return o_encode_png(width, height, data, compression or 6) | ||||
| end | ||||
|   | ||||
							
								
								
									
										93
									
								
								builtin/game/misc_s.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										93
									
								
								builtin/game/misc_s.lua
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,93 @@ | ||||
| -- Minetest: builtin/misc_s.lua | ||||
| -- The distinction of what goes here is a bit tricky, basically it's everything | ||||
| -- that does not (directly or indirectly) need access to ServerEnvironment, | ||||
| -- Server or writable access to IGameDef on the engine side. | ||||
| -- (The '_s' stands for standalone.) | ||||
|  | ||||
| -- | ||||
| -- Misc. API functions | ||||
| -- | ||||
|  | ||||
| function core.hash_node_position(pos) | ||||
| 	return (pos.z + 32768) * 65536 * 65536 | ||||
| 		 + (pos.y + 32768) * 65536 | ||||
| 		 +  pos.x + 32768 | ||||
| end | ||||
|  | ||||
|  | ||||
| function core.get_position_from_hash(hash) | ||||
| 	local x = (hash % 65536) - 32768 | ||||
| 	hash  = math.floor(hash / 65536) | ||||
| 	local y = (hash % 65536) - 32768 | ||||
| 	hash  = math.floor(hash / 65536) | ||||
| 	local z = (hash % 65536) - 32768 | ||||
| 	return vector.new(x, y, z) | ||||
| end | ||||
|  | ||||
|  | ||||
| function core.get_item_group(name, group) | ||||
| 	if not core.registered_items[name] or not | ||||
| 			core.registered_items[name].groups[group] then | ||||
| 		return 0 | ||||
| 	end | ||||
| 	return core.registered_items[name].groups[group] | ||||
| end | ||||
|  | ||||
|  | ||||
| function core.get_node_group(name, group) | ||||
| 	core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead") | ||||
| 	return core.get_item_group(name, group) | ||||
| end | ||||
|  | ||||
|  | ||||
| function core.setting_get_pos(name) | ||||
| 	local value = core.settings:get(name) | ||||
| 	if not value then | ||||
| 		return nil | ||||
| 	end | ||||
| 	return core.string_to_pos(value) | ||||
| end | ||||
|  | ||||
|  | ||||
| -- See l_env.cpp for the other functions | ||||
| function core.get_artificial_light(param1) | ||||
| 	return math.floor(param1 / 16) | ||||
| end | ||||
|  | ||||
| -- PNG encoder safety wrapper | ||||
|  | ||||
| local o_encode_png = core.encode_png | ||||
| function core.encode_png(width, height, data, compression) | ||||
| 	if type(width) ~= "number" then | ||||
| 		error("Incorrect type for 'width', expected number, got " .. type(width)) | ||||
| 	end | ||||
| 	if type(height) ~= "number" then | ||||
| 		error("Incorrect type for 'height', expected number, got " .. type(height)) | ||||
| 	end | ||||
|  | ||||
| 	local expected_byte_count = width * height * 4 | ||||
|  | ||||
| 	if type(data) ~= "table" and type(data) ~= "string" then | ||||
| 		error("Incorrect type for 'data', expected table or string, got " .. type(data)) | ||||
| 	end | ||||
|  | ||||
| 	local data_length = type(data) == "table" and #data * 4 or string.len(data) | ||||
|  | ||||
| 	if data_length ~= expected_byte_count then | ||||
| 		error(string.format( | ||||
| 			"Incorrect length of 'data', width and height imply %d bytes but %d were provided", | ||||
| 			expected_byte_count, | ||||
| 			data_length | ||||
| 		)) | ||||
| 	end | ||||
|  | ||||
| 	if type(data) == "table" then | ||||
| 		local dataBuf = {} | ||||
| 		for i = 1, #data do | ||||
| 			dataBuf[i] = core.colorspec_to_bytes(data[i]) | ||||
| 		end | ||||
| 		data = table.concat(dataBuf) | ||||
| 	end | ||||
|  | ||||
| 	return o_encode_png(width, height, data, compression or 6) | ||||
| end | ||||
		Reference in New Issue
	
	Block a user