void OnStart()
{
	SetEntityPlayerLookAtCallback("corpse_male_torso_1", "BodySanity", true);	
	//Skybox
	SetSkyBoxActive(true);
	SetSkyBoxTexture("StarrySky.dds");
	SetLocalVarInt("ChillingInEdit", 0);
	SetEntityActive("mansionbase_large_window_broken_1", false);
	SetPlayerLampOil(0);
	//Add the Lantern and 10 Tinderboxes when in Debug mode, always good to have light!
    if(ScriptDebugOn())
    {
        GiveItemFromFile("lantern", "lantern.ent");
		SetPlayerLampOil(100.0f);
        for(int i=0;i<10;i++) GiveItemFromFile("tinderbox_"+i, "tinderbox.ent");
		//AddLocalVarInt("ChillingInEdit", 1);
    }
	// Intro (Editmode not intro)
	
	AddLocalVarInt("ChillingInEdit", 1);
	Intro();

	
	AddEntityCollideCallback("Player", "Change_map", "GoToMansion", true, 1);
	AddEntityCollideCallback("Player", "Falling_death_area", "DeadFunc", false, 1);
	AddEntityCollideCallback("Player", "WindArea", "WindFunc", false, 1);
	AddEntityCollideCallback("Player", "SoundArea", "GruntFunc", true, 1);
	AddEntityCollideCallback("Player", "OwlSound", "OwlFunc", true, 1);
	AddEntityCollideCallback("Player", "OwlSound_1", "OwlFunc1", true, 1);
	AddEntityCollideCallback("Player", "Checkpoint1_area", "SetCheckpoint", true, 1);

	SetEntityPlayerLookAtCallback("barrel01_12", "BarrelFunc", true);
	
	
	//Smash window
	SetEntityPlayerInteractCallback("mansionbase_large_window_breakable_1", "Smash", true);
	
	//Rachel scream
	SetEntityPlayerInteractCallback("Rachel_scream_door", "RachelScream", true);
	
	// Preload sounds and particels
	PreloadSound("ambience_water.snt");
	PreloadSound("break_glass_large.snt");
	PreloadSound("metal_walk.snt");
	PreloadParticleSystem("ps_glass_shards.ps");
	PreloadParticleSystem("blood.ps");
}

//barrel focus
void BarrelFunc(string &in asEntity, int alState) 
{
	SetMessage("Message", "Barrel", 0);
}
///////////////
//The whole intro scene
//////////////

void Intro()
{	
	if(GetLocalVarInt("ChillingInEdit") == 1)
	{
		FadeOut(0.0f);
		
		// disables player movement
		SetPlayerActive(false);
		SetSanityDrainDisabled(true);
		SetInventoryDisabled(true);
		
		// Sets player "sleeping"
		SetPlayerCrouching(true);
		FadePlayerRollTo(55.0f, 30.0f, 30.0f);
		
		BlockDefaultHints();
		AddTimer("Wa1", 2.0f, "Wakeup");
		AddTimer("Wa2", 2.6f, "Wakeup");
		AddTimer("Wa3", 7.0f, "Wakeup");
		AddTimer("Wa4", 9.0f, "Wakeup");
		AddTimer("Wa5", 10.0f, "Wakeup");
		AddTimer("Wa6", 17.0f, "Wakeup");
		AddTimer("Wa7", 25.0f, "Wakeup");
		AddTimer("Wa8", 29.0f, "Wakeup");	
		AddTimer("Wa9", 42.0f, "Wakeup");	
	}
}
void Wakeup(string &in asTimer)
{
	if(asTimer == "Wa1")
	{
		FadeIn(4.1f);
	}
	
	else if(asTimer == "Wa2")
	{
		SetEntityActive("servant_grunt_1", true);
		AddEnemyPatrolNode("servant_grunt_1", "PathNodeArea_1", 0, "");
	}
	
	else if(asTimer == "Wa3")
	{
		FadeOut(2.0f);
	}
	else if(asTimer == "Wa4")
	{
		SetEntityActive("bonfire_1", false);
	}
	
	else if(asTimer == "Wa5")
	{
		SetEntityActive("servant_grunt_1", false);
		FadePlayerRollTo(70.0f, 15.0f, 20.0f);
	}
	
	else if(asTimer == "Wa6")
	{	
		SetEntityActive("bonfire_1", true);
		FadeIn(4.0f);
		AddDebugMessage("1", true);
	}
	
	else if(asTimer == "Wa7")
	{
		FadePlayerRollTo(15.0f, 12.0f, 14.0f);
		MovePlayerHeadPos(1.0f, 0.0f, 0.0f, 2.0f, 1.0f);
		AddEffectVoice("Wake up1.ogg", "", "Flashbacks", "Adam1", false, "", 0, 0);
		AddEffectVoice("Wake up2.ogg", "", "Flashbacks", "Adam2", false, "", 0, 0);
		PlayMusic("06_amb.ogg", true, 0.6f, 3.0f, 0, false);
		AddDebugMessage("2", true);
	}
	
	else if(asTimer == "Wa8")
	{
		SetPlayerCrouching(false);
		FadePlayerRollTo(0.0f, 15.0f, 17.0f);
		SetPlayerActive(true);
		SetSanityDrainDisabled(false);
		SetInventoryDisabled(false);
		MovePlayerHeadPos(0, 0, 0, 2, 1);
	}
	else if(asTimer == "Wa9")
	{
		AddQuest("Find_Rachel", "Quest1");
	}
}

/////////////
//Intro scene done
/////////////

void WindFunc(string &in asParent, string &in asChild, int alState)
{
	PlaySoundAtEntity("Eerie", "ambience_wind_eerie.snt", "WindAreaSound", 4, true);
}	
// Block important hints
void BlockDefaultHints()
{
	BlockHint("DarknessDecrease");
	BlockHint("SanityHit");
	BlockHint("SanityLow");
	BlockHint("PickLantern");
	BlockHint("PickTinderbox");
	BlockHint("EnemySeen");
}
	// Unblocks the impotant hints
void UnBlockDefaultHints()
{
	UnBlockHint("DarknessDecrease");
	UnBlockHint("SanityHit");
	UnBlockHint("SanityLow");
	UnBlockHint("PickLantern");
	UnBlockHint("PickTinderbox");
	UnBlockHint("EnemySeen");
}
void BodySanity(string &in asEntity, int alState) 
{
	GiveSanityDamage(10, true);
	PlaySoundAtEntity("", "react_scare.snt", "Player", 0, false);
}
///////////
//Rachel scream
///////////

void RachelScream(string &in asEntity)
{
	PlayMusic("29_amb.ogg", false, 1.0f, 1, 1, false);
	PlaySoundAtEntity("Door", "locked_door.snt", "RachelArea", 0, false);
	AddTimer("", 2, "notice");
	AddTimer("", 7, "addcastleQ");
}
void addcastleQ(string &in asTimer)
{
	AddTimer("Noobs", 200, "helpnoobs");
	AddQuest("ClimbTree", "Quest2");
}
void notice(string &in asTimer)
{
	PlaySoundAtEntity("Rachel", "Rachel_scream_help.snt", "RachelArea", 0, false);
	PlaySoundAtEntity("", "react_scare.snt", "Player", 0, false);
	GiveSanityDamage(20, true);
	FadePlayerAspectMulTo(1.2f, 2.0f);
	AddTimer("Scream1", 0.5f, "Normal");
	AddTimer("Scream2", 3.5f, "Normal");
	AddTimer("Scream3", 6.5f, "Normal");
}
void Normal(string &in asTimer)
{
	if(asTimer == "Scream1")
	{
		FadePlayerAspectMulTo(1.0f, 2.0f);
	}
	if(asTimer == "Scream2")
	{
		PlaySoundAtEntity("", "Rachel_adam.snt", "Player", 0, false);
	}
	if(asTimer == "Scream3")
	{
		PlaySoundAtEntity("", "react_breath_slow.snt", "Player", 0, false);
	}
}
void GruntFunc(string &in asParent, string &in asChild, int alState)
{
	PlaySoundAtEntity("walkingmetal", "metal_walk_loop.snt", "RachelArea", 5, false);
	SetEntityActive("LadderArea_1", true);
	AddTimer("", 7, "Stoppy");
}
void Stoppy(string &in asTimer)
{
	StopSound("walkingmetal", 4);
}
void helpnoobs(string &in asTimer)
{
	SetMessage("Message", "Nobbs", 0);
}
//done

void OwlFunc(string &in asParent, string &in asChild, int alState)
{
	AddDebugMessage("Ugla", true);
	PlaySoundAtEntity("", "11_owl1.snt", "OwlArea", 0.0f, false);
}
void OwlFunc1(string &in asParent, string &in asChild, int alState)
{
	AddDebugMessage("Ugla", true);
	PlaySoundAtEntity("", "11_owl1.snt", "OwlArea_1", 0.5f, false);
}
// Change Map
void GoToMansion(string &in asParent, string &in asChild, int alState)
{
	ChangeMap("SecondFloor.map", "PlayerStartArea_1", "", "");
}

//Checkpoint
void SetCheckpoint(string &in asParent, string &in asChild, int alState)
{
	CheckPoint("", "Checkpoint1", "", "Hints", "Death1");
}

//Don't commit suicide
void DeadFunc(string &in asParent, string &in asChild, int alState)
{
	SetPlayerHealth(0);
}

//Smash window
void Smash(string &in asEntity)
{
	CreateParticleSystemAtEntity("", "ps_glass_shards.ps", "mansionbase_large_window_breakable_1", false);
	PlaySoundAtEntity("", "break_glass_large.snt", "mansionbase_large_window_breakable_1", 0, false);
	SetEntityActive("mansionbase_large_window_breakable_1", false);
	SetEntityActive("mansionbase_large_window_broken_1", true);
	GivePlayerDamage(10, "BloodSplat", true, false);
	CompleteQuest("ClimbTree", "Quest2");
}

void OnEnter()
{
}
void OnLeave()
{
	StopMusic(1, 0);
	SetupLoadScreen("LoadingText", "LoadScreen2", 0, "game_loading_brennenburg.jpg");
}