

local nrg,med,cnt,gv,lv,w


-- Эта функция вызывается первой. Онлайновые объекты недоступны! db.actor недоступен!
function on_game_start()
	math.randomseed (device ():time_global ())
end

function first_run() 
	if amk.load_variable("x_first_run",true) then
		amk.g_start_timer("gg_need_sleep",0,0,6)
		--amk.g_start_timer("show_news",0,0,10)
		amk.spawn_item_in_inv("matras")
		amk.save_variable("x_first_run",false)
	end
end 
--'*******************************************************************************
--'   **   **   **   **   **    **   **   **      SLEEP_AMK          **   **   **   **   **    **   **   **   **
--'*******************************************************************************
function reduce_need_sleep(time)
	local tmp=amk.load_variable("gg_need_sleep",0)
	tmp=tmp-time*120
	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)
	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,0,6)
end

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

function check_sleep_item(obj)
	local section = obj:section()
	local stype=nil
	
	if section=="energy_drink" then
		stype="nrg"
	--'elseif (section=="medkit" or section=="medkit_army" or section=="medkit_scientic") then
	--'	stype="med"
	elseif section=="matras" then
		stype="matras"
	--elseif section=="repbox_s1" then
	--	amk.start_timer("sleep_repbox",0.1,amk.pack_array_to_string( {obj:id(), obj:condition(), 1} ))
	--elseif section=="repbox_s2" then
	--	amk.start_timer("sleep_repbox",0.1,amk.pack_array_to_string( {obj:id(), obj:condition(), 2} ))
	elseif section=="treasure_item" then
		stype="tr_item"
	end
 
	if stype~=nil then
		amk.start_timer("sleep_"..stype,0.1,obj:id())
	end
end

function test_for_need_sleep_nrg(oid)
	if alife():object(oid)==nil then
		local n=amk.load_variable("gg_need_sleep_nrg",0)
		if n<3 then
			amk.save_variable("gg_need_sleep_nrg",n+1)
			amk.save_variable("gg_need_sleep",amk.load_variable("gg_need_sleep",0)-30+n*10) 
			test_sleep_pp()
		end  
		amk.g_start_timer("block_sleep_menu",0,1,0)
		amk.save_variable("block_sleep_menu",1)
	end
end
--[[
function test_for_need_sleep_med(oid)
	if alife():object(oid)==nil then
		amk.save_variable("gg_need_sleep",amk.load_variable("gg_need_sleep",0)+5) 
		test_sleep_pp()
	end
end
]]--
function test_for_need_sleep_matras(oid)
	if alife():object(oid)==nil then
		local enemy=false
		for a=0,65534 do
			local obj=level.object_by_id(a)
			if obj then
				if (( IsStalker(obj) and amk.get_npc_relation(obj,db.actor)=="enemy" ) or 
					IsMonster(obj)) and obj:position():distance_to(db.actor:position())<40 
					and obj:see(db.actor) then
					enemy=true
					break
				end
			end
		end
		if enemy==true then
			amk.send_tip(game.translate_string("not_need_sleep_enemy"),nil,nil,5)
		--else
		--	if amk.load_variable("blowout",-1)>-1 and amk.load_variable("blowout",-1)<5 then
		--		amk.send_tip(game.translate_string("blowout_not_sleep"),nil,nil,5)
			else
				if amk.load_variable("block_sleep_menu",0)==0 then
					if amk.load_variable("gg_need_sleep",0)>10 then
						local spwn=ui_cheat.cheat(get_hud())
						level.start_stop_menu(spwn,true)
					else
						amk.send_tip(game.translate_string("not_need_sleep"),nil,nil,5)
					end
				else
					amk.send_tip(game.translate_string("not_need_sleep_nrg"),nil,nil,5)
				end
			
		 end
	    amk.spawn_item_in_inv("matras")
	end
end

function test_for_need_sleep_tr_item(oid)
	if alife():object(oid)==nil then
	    if news_main then
	      news_main.create_treasurebox()
	    end
	end
end

--'*******************************************************************************
--'   **   **   **   **   **    **   **   **      NEWS_AMK          **   **   **   **   **    **   **   **   **
--'*******************************************************************************
function show_news()
	if (news_main and news_main.on_news) then
		news_main.on_news()
	end

	local ln = level.name()
	if (ln == "l03u_agr_underground" or ln == "l04u_labx18" or ln == "l08u_brainlab" or ln == "l10u_bunker") then
		-- Радиомолчание
	else	
		if math.random()>0.2 then
			if math.random()>0.875 then
				if amk_dolg and math.random()>0.5 then
					local dolg_news=amk_dolg.get_strings()
					db.actor:give_game_news(dolg_news, "ui\\ui_iconsTotal", Frect():set(498,141,83,47), 0, 15000)
				elseif amk_freedom then
					local freedom_news=amk_freedom.get_strings()
					db.actor:give_game_news(freedom_news, "ui\\ui_iconsTotal", Frect():set(498,94,83,47), 0, 15000)
				elseif amk_csky then
					local csky_news=amk_csky.get_strings()
					db.actor:give_game_news(csky_news, "ui\\ui_iconsTotal", Frect():set(498,188,83,47), 0, 15000)	
				end
			else
				if math.random()>0.57 then
				--amk.send_tip(when.." "..where.." "..text.." "..mat,name.." "..sname,nil,15,"gen_info")
				else
				--amk.send_tip(name.." "..sname..". "..dead..". "..reason,game.translate_string("stalker_died"),nil,10,"death")
				end
			end 
		else
			if amk_uniq_news_lists and math.random()>0.5 then
				local uniq=amk_uniq_news_lists.get_strings()
				local name,sname = amk_names_lists.get_strings()
				amk.send_tip(uniq,name.." "..sname,nil,15,"uniq")
			elseif amk_modders then
				local mod_news=amk_modders.get_strings()
				db.actor:give_game_news(mod_news, "ui\\ui_iconsTotal", Frect():set(498,47,83,47), 0, 15000)
			end
		end
	end
	amk.g_start_timer("show_news",0,0,math.random(40,80))
end



