local cos, sin = math.cos, math.sin




local function get_random_unit_vector()
	local yaw = math.random() * 2 * math.pi
	local zaw = math.random() * 2 * math.pi
	local ret = {x = cos(yaw), y = 0, z = sin(yaw)}
	ret = vector.add(ret, {x = cos(zaw), y = sin(zaw), z = 0})
	return vector.normalize(ret)
end

function mob_spawning_director.find_position_air(target_player, dist_addition)
	dist_addition = dist_addition or 20
	local target_pos = target_player:get_pos()
	local attempt_pos = vector.add(target_pos,
		vector.multiply(get_random_unit_vector(), dist_addition + math.random(20)))
	local raycast = Raycast(target_pos, attempt_pos, false, false)
	
	local pt = raycast:next()
	if pt
	then
		if vector.distance(pt.above, attempt_pos) > 10
		then
			return pt.above
		end
		return
	end
	return attempt_pos
end

function mob_spawning_director.find_position_ground(target_player)
	local attempt_pos = mob_spawning_director.find_position_air(target_player, 40)
	if not attempt_pos
	then
		return
	end
	local down_pos = vector.add(attempt_pos, {x = 0, y = -60, z = 0})
	local raycast = Raycast(attempt_pos, down_pos, false, false)
	
	local pt = raycast:next()
	if pt
	then
		local playerpos = target_player:get_pos()
		if vector.distance(pt.above, playerpos) > 10
		then
			return pt.above
		end
	end
end

