--============================================================================--
--== Скрипт детекторов артефактов
--== Автор: Singapur22
--== Дата последнего изменения: 23 августа 2010 г.
---------------------------------------------------
--== Переделал под Вариант "Омега": vovandolg
--============================================================================--

local det_ind  = "det_artefact_indy"
local det_pro  = "det_artefact_pro"
local det_spr  = "det_artefact_super"
--================================== Модифицированные версии =====
local det_ind_m1  = "det_artefact_indy_m1"
local det_pro_m1  = "det_artefact_pro_m1"
local det_spr_m1  = "det_artefact_super_m1"
local det_spr_m2  = "det_artefact_super_m2"
--=============================================================================
local del_spots = false

local snd_obj = xr_sound.get_safe_sound_object("ambient\\da_beep_indy") --пищалка indy
local snd_obj_2 = xr_sound.get_safe_sound_object("ambient\\da_beep_pro") --пищалка pro
local snd_obj_3 = xr_sound.get_safe_sound_object("ambient\\da_beep_super") --пищалка super

--функция определения наличия детектора на поясе
function update()
    local detector = (db.belt[det_spr] and det_spr) or
					 (db.belt[det_spr_m1] and det_spr_m1) or
					 (db.belt[det_spr_m2] and det_spr_m2) or
	                 (db.belt[det_pro] and det_pro) or
					 (db.belt[det_pro_m1] and det_pro_m1) or
					 (db.belt[det_ind] and det_ind) or
	                 (db.belt[det_ind_m1] and det_ind_m1)
		
	if not detector then
	    if del_spots then
	        this.iteration_del_spot()
		    del_spots = false
		    return
	    else
	        return
	    end
	else
	    this.iterate_artefacts(detector)
		del_spots = true
	end
end

--итерация таблицы артефактов
function iterate_artefacts(name_func)
    for id, storage in pairs(db.artefacts) do
	    local sobj = alife():object( id )
		if sobj then
		    local obj = level.object_by_id( id )
			if obj then
			    if not obj:parent() then
				    local dist = db.actor:position():distance_to_sqr(obj:position())
					if not storage.tim_beep then
					    db.artefacts[id].tim_beep = 0
					end
	                this[name_func](id, storage, sobj, obj, dist)
				else
				    this.del_spot(id, storage)
				end
			end
		else
		    db.artefacts[id] = nil
		end
	end
end

--============ функции детекторов ====================--
--===== вызываются для каждого артефакта =============--
--============== в отдельности =======================--
--200=5
--400=10
--800=20
--1600=40
--2000=50
--детектор INDY
function det_artefact_indy(id, storage, sobj, obj, dist)
    if dist <= 200 then -- 5 метров
    	if dist * 6 < time_global() - storage.tim_beep  then
			if not snd_obj:playing() then
			    db.artefacts[id].tim_beep = time_global()
			    snd_obj:play_no_feedback(db.actor, sound_object.s2d, 0, vector(), 1.0)
			 end
		end
	end
	this.del_spot(id, storage)
end

--детектор PRO(prototip INDY)
function det_artefact_pro(id, storage, sobj, obj, dist)
    if dist <= 600 then -- 15 метров
	if dist * 6 < time_global() - storage.tim_beep  then
		if not snd_obj_2:playing() then
				db.artefacts[id].tim_beep = time_global()
				snd_obj_2:play_no_feedback(db.actor, sound_object.s2d, 0, vector(), 1.0)
			 end
		end
	end
	this.del_spot(id, storage)
end

--детектор SUPER(мега-prototip INDY)
function det_artefact_super(id, storage, sobj, obj, dist)
    if dist <= 1000 then -- 25 метров
	if dist * 6 < time_global() - storage.tim_beep  then
		if not snd_obj_3:playing() then
				db.artefacts[id].tim_beep = time_global()
				snd_obj_3:play_no_feedback(db.actor, sound_object.s2d, 0, vector(), 1.0)
			 end
		end
	end
	this.del_spot(id, storage)
end

--================ Модификации :) =================================--
--детектор INDY_M1
function det_artefact_indy_m1(id, storage, sobj, obj, dist)
    if dist <= 400 then -- 10 метров
    	if dist * 6 < time_global() - storage.tim_beep  then
			if not snd_obj:playing() then
			    db.artefacts[id].tim_beep = time_global()
			    snd_obj:play_no_feedback(db.actor, sound_object.s2d, 0, vector(), 1.0)
			 end
		end
	end
	this.del_spot(id, storage)
end

--детектор PRO_M1
function det_artefact_pro_m1(id, storage, sobj, obj, dist)
    if dist <= 800 then -- 20 метров
	if dist * 6 < time_global() - storage.tim_beep  then
		if not snd_obj_2:playing() then
				db.artefacts[id].tim_beep = time_global()
				snd_obj_2:play_no_feedback(db.actor, sound_object.s2d, 0, vector(), 1.0)
			 end
		end
	end
	this.del_spot(id, storage)
end

--детектор SUPER_M1
function det_artefact_super_m1(id, storage, sobj, obj, dist)
    if dist <= 1200 then -- 30 метров
	if dist * 6 < time_global() - storage.tim_beep  then
		if not snd_obj_3:playing() then
				db.artefacts[id].tim_beep = time_global()
				snd_obj_3:play_no_feedback(db.actor, sound_object.s2d, 0, vector(), 1.0)
			 end
		end
	end
	this.del_spot(id, storage)
end

--детектор SUPER_M2
function det_artefact_super_m2(id, storage, sobj, obj, dist)
    if dist <= 1400 then -- 35 метров
	if dist * 6 < time_global() - storage.tim_beep  then
		if not snd_obj_3:playing() then
				db.artefacts[id].tim_beep = time_global()
				snd_obj_3:play_no_feedback(db.actor, sound_object.s2d, 0, vector(), 1.0)
			 end
		end
	end
	this.del_spot(id, storage)
end
--================================================================--
--================ Родные данные==================================--
--детектор PRO
--[[function det_artefact_pro(id, storage, sobj, obj, dist)
    if dist <= 1600 then -- 40 метров
	    this.add_spot(id, storage, false, obj)
	else
	    this.del_spot(id, storage)
	end
	if dist <= 400 and storage.tim_beep < time_global() and not snd_obj:playing() then
		db.artefacts[id].tim_beep = time_global() + 800
		snd_obj:play_no_feedback(db.actor, sound_object.s2d, 0, vector(), 1.0)
	end
end]]

--детектор SUPER
--[[function det_artefact_super(id, storage, sobj, obj, dist)
    if dist <= 10000 then -- 100 метров
	    this.add_spot(id, storage, true, obj)
	else
	    this.del_spot(id, storage)
	end
	if dist <= 400 and storage.tim_beep < time_global() and not snd_obj:playing() then
		db.artefacts[id].tim_beep = time_global() + 800
		snd_obj:play_no_feedback(db.actor, sound_object.s2d, 0, vector(), 1.0)
	end
end]]

--============================================================--
--=========== функции управления спотами =====================--
--============================================================--

--функция установки/переустановки спота
function add_spot(id, storage, super, obj)
    if not storage.spot then --установка спота
		local name = "артефакт"
		local spot_id = "artefact_location"
		local section = storage.podmena or obj:section()
		if super then
		    local ini = system_ini()
			if ini and section and ini:section_exist(section) then
				if ini:line_exist(section, "inv_name") then
					name = ini:r_string(section, "inv_name")
				end
			end
			db.artefacts[id].super = true
			spot_id = "spot_"..section
		end
		level.map_add_object_spot(id, spot_id, name)
		db.artefacts[id].spot = spot_id
	elseif super ~= storage.super then --переустановка спота
		this.del_spot(id, storage)
		db.artefacts[id].super = super
	end
end

--функция удаления всех спотов
function iteration_del_spot()
    for id, storage in pairs(db.artefacts) do
	    local sobj = alife():object(id)
		if sobj then
		    this.del_spot(id, storage)
		end
	end
end

--функция удаления спота с указанного арта
function del_spot(id, storage)
    if storage.spot then
		level.map_remove_object_spot(id, storage.spot)
		db.artefacts[id].spot = nil
	end
end
