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

function main(scale)
	amk_mod.reduce_need_sleep(scale)
	local sleep_ltx = ini_file ("scripts\\actorsleep.ltx")
	schemes["ar_sleep"] = "ar_sleep"
	ar_sleep.set_scheme(db.actor, sleep_ltx, "ar_sleep", logic)
	db.actor:stop_talk()
	db.actor:hide_weapon()
	level.hide_indicators()
	level.disable_input()
	sleep_manager.hp_corrector(scale)
	sleep_manager.starter(scale)
end

function starter(scale)
	lwc.PlaySound([[inv_sounds\sleep_bag_down]])
	local factor = scale * 2650
	game.start_tutorial("time_scaling")
	level.add_pp_effector("teleport.ppe", 2006, false)
	level.set_time_factor(factor)
	
	sleep_manager.hp_corrector(scale)
	lwc_surge.bSleep = true
end

function dreamer()
	level.set_time_factor(lwc.timefactor())
	if sleep_manager.is_sleep_active() then
		sleep_manager.hp_corrector(0)
		local dream = dream.sleep_video_name_callback ()
		if dream ~= "" then game.start_tutorial(dream)
		else game.start_tutorial("without_dream") end
	end
end

function stopper()
	level.set_time_factor(lwc.timefactor())
	ar_sleep.disable_scheme()
	if db.actor:alive() then
		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()
		lwc.PlaySound([[inv_sounds\sleep_bag_up]])
	    	level.add_cam_effector("camera_effects\\hit_back_left.anm", 26, false, "")
	end
	level.enable_input()
	level.show_indicators()
	lwc_surge.bSleep = false
end

function hp_corrector(iHour)
	db.actor.health = 0.04 * iHour
end

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