-- -----------------------------
--  DreamMod v0.2 by Ab@dDon ---
-- -----------------------------

function sleep_one_hour()
	sleep_manager.main (3)
end
function sleep_three_hours()
	sleep_manager.main (5)
end
function sleep_nine_hours()
	sleep_manager.main (8)
end
function sleep_unlim(scale)
	local sleep_time = (1 - db.actor.health)*6
      	sleep_manager.main (sleep_time)
end

function main(scale)
	--level.set_snd_volume(0.001)
	amk.mod_call("reduce_need_sleep",scale)
--	get_console():execute("_main_")	
	local sleep_ltx = ini_file ("scripts\\actorsleep.ltx")
	schemes["ar_sleep"] = "ar_sleep"
	ar_sleep.set_scheme(db.actor, sleep_ltx, "ar_sleep", logic)
--	get_console():execute("_main_set_sheme")
	db.actor:stop_talk()
	--db.actor:hide_weapon()
	level.disable_input()
	sleep_manager.hp_corrector(scale)
--	get_console():execute("_main_done")
	sleep_manager.starter (scale)
end

function starter(scale)
	local factor = scale * 2650
	game.start_tutorial("time_scaling")
	level.add_pp_effector("teleport.ppe", 2006, false)
	level.set_time_factor(factor)
--	get_console():execute("scaling_done")
end

function dreamer()
	level.set_time_factor(system_ini():r_float("alife","time_factor"))
	if sleep_manager.is_sleep_active() then
		sleep_manager.hp_corrector(0)
		local dream = dream.sleep_video_name_callback()
--		get_console():execute(dream)
		if dream ~= "" then game.start_tutorial(dream)
		else game.start_tutorial("without_dream") end
	end
end

function stopper()
  if not is_sleep_active() then
    return
  end
--	get_console():execute("stoping")
	level.set_time_factor(system_ini():r_float("alife","time_factor"))
	ar_sleep.disable_scheme()

	if amk_corpses then
		amk_corpses.update_all_corpses()
	end

	
	if not db.actor:alive() then
--
	else 
		level.add_cam_effector("camera_effects\\prison_1.anm", 25, false, "")
		level.add_pp_effector("yantar_underground_psi.ppe", 2007, false)
		level.add_pp_effector("total_recall.ppe", 2008, false)
		--db.actor:restore_weapon()
		local snd_obj = xr_sound_new.get_safe_sound_object([[characters_voice\human_01\stalker\states\breath\breath_2]])
		snd_obj:play_no_feedback(db.actor, sound_object.s2d, 0, vector(), 15.0)
	    level.add_cam_effector("camera_effects\\hit_back_left.anm", 26, false, "")
	end
	level.enable_input()	
	--level.set_snd_volume(1.0)
--	get_console():execute("stoped")
end

function hp_corrector(fr)
	if fr ~= 0 then
		hp = fr/6 + db.actor.health
		if hp > 1 then hp = 1 end
		return
	elseif db.actor.health > hp then 
		db.actor.health = hp - db.actor.health
	end
end

function is_sleep_active()
	if db.storage[db.actor:id()].active_scheme == "ar_sleep" then 
		return true 
	end
	return false
end
