--[[
В составе этого скрипта работы\моды следующих авторов: 
Мод Alter Zone by XiaNi, взят код для подмены детекторов,
Биорадар by xStream, BAC9-FLCL, tolusha, Z-Vertoz, Rusya, Nekt, взят код, текстуры для вывода миникарты и отметок на ней,
Текстура Биорадара by Ирбиs ( Аналогичная текстура имеется у Wolkolak )

Адаптация к моду LWR, худы детекторов, некоторые идеи by Ирбиs, Real Wolf
Взятые материалы послужили лишь основой для создания детектора аномалий.
]]

local counter = 1
local max_count = 25 --количество меток максимум
max_dist = 50 --макс расстояние до цели
start_dist = 50 --начальное расстояние до цели
screen_max_dist = 100 --макс расстояние в пикселях радара
screen_start_dist = 100 --начальное расстояние в пикселях радара
local center_x = 876 --отсюда отсчитываем, центр радара
local center_y = 760 --отсюда отсчитываем, центр радара
local tex_arr = {}
local col_arr = {}
local anoms={}
local show = false

function simple()
		local active_item = db.actor:active_item()
		if active_item then
			if (active_item:section()=="bolt_d1" and active_item:condition() > 0.1) then
				if db.actor:object("detector_simple_active") == nil and db.actor:object("detector_advances_active") == nil 
				and db.actor:object("detector_elite_active") == nil  then
					amk.spawn_item_in_inv("detector_simple_active")
					show = true
				end
			else
				if show and active_item:condition() < 0.1 then
					news_manager.send_tip(db.actor, "Аккумулятор детектора разряжен.", nil, nil, 5000)
					show = false
				end
				local det = db.actor:object("detector_simple_active")
				if det ~= nil then
					alife():release(alife():object(det:id()), true)
				end
			end
		else
			local det = db.actor:object("detector_simple_active")
			if det ~= nil then
				alife():release(alife():object(det:id()), true)
			end
		end
end

function advances()
		local active_item = db.actor:active_item()
		if active_item then
			if (active_item:section()=="bolt_d2" and active_item:condition() > 0.1) then
				if db.actor:object("detector_simple_active") == nil and db.actor:object("detector_advances_active") == nil 
				and db.actor:object("detector_elite_active") == nil  then
					amk.spawn_item_in_inv("detector_advances_active")
					show = true
				end
			else
				if show and active_item:condition() < 0.1 then
					news_manager.send_tip(db.actor, "Аккумулятор детектора разряжен.", nil, nil, 5000)
					show = false
				end
				local det = db.actor:object("detector_advances_active")
				if det ~= nil then
					alife():release(alife():object(det:id()), true)
				end
			end
		else
			local det = db.actor:object("detector_advances_active")
			if det ~= nil then
				alife():release(alife():object(det:id()), true)
			end
		end
end

function elite()
		local active_item = db.actor:active_item()
		if active_item then
			if (active_item:section() == "bolt_d3" and active_item:condition() > 0.1) then
				if db.actor:object("detector_simple_active") == nil and db.actor:object("detector_advances_active") == nil 
				and db.actor:object("detector_elite_active") == nil  then
					amk.spawn_item_in_inv("detector_elite_active")
					show = true
				end
			else
				if show and active_item:condition() < 0.1 then
					news_manager.send_tip(db.actor, "Аккумулятор детектора разряжен.", nil, nil, 5000)
					show = false
				end
				local det = db.actor:object("detector_elite_active")
				if det ~= nil then
					alife():release(alife():object(det:id()), true)
				end
			end
		else
			local det = db.actor:object("detector_elite_active")
			if det ~= nil then
				alife():release(alife():object(det:id()), true)
			end
		end
end

function anom()
	for i = 0, 65534 do
		local sobj = alife():object(i)
			if sobj then
				if IsAnomaly(sobj) then
					anoms[sobj.id] = sobj.id
				end
			end
	end
end

function draw_dot(dx,dy,color,tex)
	local hud = get_hud()
	
	local custom_static = hud:GetCustomStatic("target_dot_"..counter)
	if custom_static == nil then
		hud:AddCustomStatic("target_dot_"..counter, true)
		custom_static = hud:GetCustomStatic("target_dot_"..counter)
	end
	
	custom_static:wnd():SetWndPos(center_x+dx,center_y+dy)
	
	tex = tex or [[ui\ui_white_dot]]
	if not tex_arr[counter] or tex_arr[counter]~=tex then
		custom_static:wnd():InitTexture(tex)
		tex_arr[counter]=tex
	end
	color = color or GetARGB(255,255,0,0)
	if not col_arr[counter] or col_arr[counter]~=color then
		custom_static:wnd():SetColor(color)
		col_arr[counter]=color
	end
	
end

function transform_coords(tpos)
	local sdir = device().cam_dir
	local dir = tpos:sub(db.actor:position())
	local dx = dir.x / max_dist * screen_max_dist
	local dy = -dir.z / max_dist * screen_max_dist
	local vect = vector():set(dx,0,dy)
	vect = vector_rotate_y(vect,math.atan2(-sdir.x,sdir.z)/math.pi*180)
	return vect.x,vect.z
end

function get_color()
	return GetARGB(255,0,255,0)
end

function update_map()
        if not db.actor:object("detector_elite_active") then
           local map = get_hud()
           if map:GetCustomStatic("det_map")~=nil then
              local custom_static=map:GetCustomStatic("det_map")
              custom_static:wnd():SetWndPos(-1000,-1000)
           end
           for a=1,25 do
              if map:GetCustomStatic("target_dot_"..a)~=nil then
                 local custom_static=map:GetCustomStatic("target_dot_"..a)
                 custom_static:wnd():SetWndPos(-100,-100)
              end
           end
           return
        end
        local hud = get_hud()
	if hud:GetCustomStatic("det_map") == nil then
		hud:AddCustomStatic("det_map", true)
	end
	local det_static=hud:GetCustomStatic("det_map")
	det_static:wnd():SetWndPos(752,636)
	counter = 1
	for i, an_id in pairs(anoms) do
		local o = level.object_by_id(an_id)
		local so = alife():object(an_id)
                if so and o then
			if o:position():distance_to_xz(db.actor:position())<=max_dist then
				local x,y=transform_coords(o:position())
                                local tex = nil
                                draw_dot(x,y,get_color(),tex)
				counter = counter+1
				if counter>max_count then break end
			end
		end
	end
	
	if counter<=max_count then
		for a=counter,max_count do
			local custom_static = hud:GetCustomStatic("target_dot_"..a)
			if custom_static ~= nil then
				custom_static:wnd():SetWndPos(-100,-100)
			end
		end
	end
end