local vzriv = 0
local zadershka = 0
local det_id = 0
local det_id2 = 0
local temp_wait=0
local have_det1=0
local have_det2=0

mina = {}
mina_set = {}
mina_set_t = {}
local i3=0
timer_set = {}
timer_set_t = {}
local t1=0

function messg(news_text, header)
	local player=xr_sound.get_safe_sound_object([[device\pda\pda_news]])
	player:play(db.actor, 0, sound_object.s2d)
	local news_text = "%c[255,160,160,160]"..header.."\\n".."%c[default]"..news_text
	db.actor:give_game_news(news_text, "ui\\ui_VDGicon", Frect():set(0,47,83,47), 0, 5000)
end

function is_deton()
	for i=0, db.actor:object_count()-1 do
		local obj = db.actor:object(i)
		if obj:section() == "detonator" then
			have_det1=1
		end
		if obj:section() == "detonator_2" then
			have_det2=1
		end
	end
end

function li(ob)
	if ob:section()=="detonator" then det_id=ob:id() end
	if ob:section()=="detonator_2" then det_id2=ob:id() end
	if ob:section()=="bomba_mina_n" or ob:section()=="bomba_mina_b" then
		i3=i3+1
		mina_set[i3]=ob:id()
		mina_set_t[i3]=3000
	end
	if ob:section()=="bomba_timer_n_10" or ob:section()=="bomba_timer_b_10" then
		t1=t1+1
		timer_set[t1]=ob
		timer_set_t[t1]=10000
	end
	if ob:section()=="bomba_timer_n_30" or ob:section()=="bomba_timer_b_30" then
		t1=t1+1
		timer_set[t1]=ob
		timer_set_t[t1]=30000
	end
end

function explode_r(ob)
	local h = hit()
	h.power = 1000
	h.impulse = 0
	h.draftsman = ob
	h.direction = vector():set(0,0,0)
	h.type = hit.fire_wound
	ob:hit(h)
end

function update(delta)
	for i,v in ipairs(mina_set) do
		if mina_set_t[i]~=nil then
			if mina_set_t[i]>=0 then
				mina_set_t[i]=mina_set_t[i]-delta
			else
				mina_set[i]=-1
			end
		end
	end
	for i,v in ipairs(timer_set) do
		if timer_set_t[i]~=nil then
			if timer_set_t[i]>=0 then
				timer_set_t[i]=timer_set_t[i]-delta
			end
			if timer_set_t[i]>=-10000 and timer_set_t[i]<0 then
				explode_r(timer_set[i])
				timer_set_t[i]=-10001
			end
		end
	end
	is_deton()
	if det_id>0 then
		if alife():object(det_id)==nil then
			det_id=-1;
			if math.random(100)>75 then
				vzriv=1
				zadershka=math.random(2000)+1000
				alife():create("detonator",db.actor:position(),db.actor:level_vertex_id(),db.actor:game_vertex_id(),db.actor:id())
			else
				messg("Упдефiпед еггог!","Детонатор NK-32beta RUS")
			end
		else
			det_id=-1;
		end
	end
	if det_id2>0 then
		if alife():object(det_id2)==nil then
			det_id2=-1;
			vzriv=1
			zadershka=0
			alife():create("detonator_2",db.actor:position(),db.actor:level_vertex_id(),db.actor:game_vertex_id(),db.actor:id())
		else
			det_id2=-1;
		end
	end
	local i2=0
	local i5=0
	for i = 1,65535 do
		local naido = level.object_by_id(i)
		if naido then
			if naido:section()=="bomba_mina_n" or naido:section()=="bomba_mina_b" then
				i5=0
				for ii,v in ipairs(mina_set) do
					if mina_set_t[ii]~=nil then
						if naido:id()==mina_set[ii] then
							i5=1
						end
					end
				end
				if i5==0 then
					i2=i2+1
					mina[i2]=i
				end
			end
		end
	end
	if i2>0 then
		for id,obj in pairs(db.storage) do
			local o = level.object_by_id(id)
			local so = alife():object(id)
			if so and o then
				local stalker=IsStalker(so)
				local monster=IsMonster(so)
				if stalker or monster then
					for i = 1,i2 do
						local objm=level.object_by_id(mina[i])
						if objm then
							if objm:parent()==nil then
								if o:position():distance_to_xz(objm:position())<=1 then
									explode_r(objm)
								end
							end
						end
					end
				end
			end
		end
	end
	if zadershka>0 then
		if temp_wait==0 then messg("Pleaсe шaиt...","Детонатор NK-32beta RUS") temp_wait=1 end
		zadershka=zadershka-delta
	elseif vzriv==1 then
		temp_wait=0
		local apos=db.actor:position()
		local temp_report=0
		local lovim=50
		if have_det1==1 then lovim=50 end
		if have_det2==1 then lovim=150 end
		for i = 1,65535 do
			local naido = level.object_by_id(i)
			if naido then
				if naido:position():distance_to_xz(apos)<=lovim then
					if (naido:section()=="bomba" or naido:section()=="bomba_big") and naido:parent()==nil then
						explode_r(naido)
						temp_report=temp_report+1
					end
					if naido:section()=="bomba_small" and naido:parent()~=nil then
						explode_r(naido)
						temp_report=temp_report+1
					end
				end
			end
		end
		vzriv=0
		if have_det1==1 then
			messg("Рюкзак(и) взорван(ы)!","Детонатор NK-32beta RUS")
		end
		if have_det2==1 then
			if temp_report==0 then messg("Рюкзаки не найдены!","Детонатор NK-37 RUS") end
			if temp_report==1 then messg("1 рюкзак взорван!","Детонатор NK-37 RUS") end
			if temp_report>1 and temp_report<5 then messg(temp_report.." рюкзака взорваны!","Детонатор NK-37 RUS") end
			if temp_report>4 then messg(temp_report.." рюкзаков взорвано!","Детонатор NK-37 RUS") end
		end
	end
end

function proverka()
--get_console():execute("script_\"babah\"_loaded_sucefully!")
end
