--/amk_rel_1/
local nrg,med,cnt

--первый запуск мода 
function first_run()
	amk.g_start_timer("gg_need_sleep",0,0,6)
	amk.g_start_timer("show_news",0,0,20)
	ogsm_anomaly.blow_flags()
	ogsm_anomaly.add_anom(level.name())

end


--новости
function show_news()
	
	local when,where,text,mat = amk_news_lists.get_strings()
	local name,sname = amk_names_lists.get_strings()
	local reason,dead = amk_death_lists.get_strings()
	local uniq=amk_uniq_news_lists.get_strings()
		
	if math.random(0,1)>0.8 then
		name=""
	end

	if math.random(0,1)>0.1 then
		mat=""
	end

	if math.random(0,1)>0.4 then
		if math.random(0,1)>0.4 then
			amk.send_tip(when.." "..where.." "..text.." "..mat,name.." "..sname,nil,15)
		else
			amk.send_tip(name.." "..sname..". "..dead..". "..reason,"Погиб сталкер:",nil,10,"death")
		end	
	else
		amk.send_tip(uniq,name.." "..sname,nil,15)
	end
	amk.g_start_timer("show_news",0,0,math.random(40,80))
end

--сон
function reduce_need_sleep(time)  --в часах
	local tmp=amk.load_variable("gg_need_sleep",0)
	tmp=tmp-time-5
	if tmp<0 then tmp=0 end
	amk.save_variable("gg_need_sleep_nrg",0)
	amk.save_variable("block_sleep_menu",0)
	amk.save_variable("gg_need_sleep",tmp)
	amk.save_variable("gg_need_sleep_vodka",0)	
	test_sleep_pp()
end

function test_for_need_sleep()	
	if sleep_manager.is_sleep_active() == false then 
		amk.save_variable("gg_need_sleep",amk.load_variable("gg_need_sleep",0)+1) 
		test_sleep_pp()
	end	
	amk.g_start_timer("gg_need_sleep",0,1,0)	
end

function test_sleep_pp()
	local tmp=amk.load_variable("gg_need_sleep",0)
	if tmp>48 then
		sleep_manager.main(10+amk.load_variable("gg_need_sleep_nrg",0))
	end
	if tmp>36 then
		level.add_pp_effector("yantar_underground_psi.ppe", 999, true)
		level.set_pp_effector_factor(999, 5.0)	
	end
	if tmp<=36 then
		level.remove_pp_effector(999)
	end
end

function test_for_need_sleep_nrg()
	local n=amk.load_variable("gg_need_sleep_nrg",0)
	if n<4 then
		amk.save_variable("gg_need_sleep_nrg",n+1)
		n=amk.load_variable("gg_need_sleep_nrg",0)
		amk.save_variable("gg_need_sleep",amk.load_variable("gg_need_sleep",0)-4+n)
		test_sleep_pp()
	end
		
	amk.g_start_timer("block_sleep_menu",0,1,0)
	amk.save_variable("block_sleep_menu",1)
end

function test_for_need_sleep_vodka()
	amk.save_variable("gg_need_sleep_vodka",amk.load_variable("gg_need_sleep_vodka",0)+1)
	local n=amk.load_variable("gg_need_sleep_vodka",0)
	amk.save_variable("gg_need_sleep",amk.load_variable("gg_need_sleep",0)+n) 
	test_sleep_pp()
end

function test_for_need_sleep_matras()
	if db.Flag2 == 1 then 
		amk.send_tip("Вы не можете спать во время выброса.", "Сообщение", nil, 5) 
	elseif amk.load_variable("block_sleep_menu",0)==0 then
		local htime = level.get_time_hours()
		local time = htime - 24 + amk.load_variable("gg_need_sleep",0) + 5
		if time < 0 then 
			time = 3
		end	
		if time > 10 then 
			time = 10	
		end
		start_sleep(time)
	else
		amk.send_tip("Кайдан не может спать из-за действия энергетика.","Сообщение",nil,5)
	end
	amk.spawn_item_in_inv("matras")
end

function start_sleep(t)
	local time = math.floor( t / db.actor.health )
	sleep_manager.main(time)
end

function check_radar_off()
	if (level.name() == "l10_radar") then
		if not has_alife_info("bar_deactivate_radar_done") then
			local pos = db.actor:position()
			if pos.z>65 and pos.x>350 and pos.x<410 then
				if amk.load_variable("radar_fix",0)==0 then
					level.add_pp_effector("fire_hit.ppe", 1523, true)
					level.set_pp_effector_factor(1523, 0.5)
					amk.save_variable("radar_fix",1)
					amk.start_timer("radar_fix",5)
				end
			end
		end
	end
end

function radar_fix()
	level.remove_pp_effector(1523)
	local pos = db.actor:position()
	if pos.z>65 and pos.x>350 and pos.x<410 then
		db.actor:kill(db.actor)
	end
	amk.del_variable("radar_fix")
end