local get_relevant_players = limbo_respawning.get_living_players

local target_methods = {}
defense_mob_api.library.target_methods = target_methods


--returns player with lowest distance
function target_methods.nearest(self)
	local selfpos = self.object:get_pos()
	local nearest_player = nil
	local nearest_dist = math.huge
	local nearest_pos
	for _, obj in ipairs(get_relevant_players())
	do
		local playerpos = obj:get_pos()
		playerpos.y = playerpos.y + 1
		local d = vector.distance(playerpos, selfpos)
		if d < nearest_dist then
			nearest_player = obj
			nearest_dist = d
			nearest_pos = playerpos
		end
	end
	return {ref = nearest_player, distance = nearest_dist, pos = nearest_pos}
end


--returns player with lowest hp * distance
function target_methods.weak(self)
	local selfpos = self.object:get_pos()
	local best = nil
	local best_value = math.huge
	local best_dist = math.huge
	local best_pos
	for _, player in ipairs(get_relevant_players())
	do
		local playerpos = player:get_pos()
		playerpos.y = playerpos.y + 1
		local dist = vector.distance(playerpos, selfpos)
		local hp = player:get_hp()
		
		if hp * dist < best_value
		then
			best = player
			best_dist = dist
			best_pos = playerpos
		end
	end
	return {ref = best, distance = best_dist, pos = best_pos}
end
