void OnStart()
{	
	AutoSave();
	ClearSavedMaps();
	SetSkyBoxActive(true);
	SetSkyBoxTexture("StarrySky.dds");
  //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");
	}
	
	SetPlayerSanity(90);
	
	//Mix ingredients at the right place!
	AddEntityCollideCallback("Player", "MixArea", "MixFunc", false, 1);
	AddEntityCollideCallback("Player", "MixArea2", "MixFunc2", false, 1);
	
	AddEntityCollideCallback("Player", "ReScArea", "SanityFunc", true, 1);
	// box push
	AddEntityCollideCallback("Player", "BoxPushArea", "BoxPushFunc", true, 1);
	
	//Bridge Expolsion
	for(int x = 0; x <= 11; x += 1)
	{
		AddUseItemCallback("BridgeExpolsion", "Nitroglycerine", "NitroArea_" + x, "PlaceNitroFunc", true);
	}
	AddEntityCollideCallback("Player", "DeathAreaC", "DeathFuncEnd", false, 1);
	AddEntityCollideCallback("Player", "NitroMessageArea", "MessageNitro", true, 1);
	
	//Steam scare
	AddEntityCollideCallback("Player", "SteamArea", "SteamFunc", true, 1);
	
	//Save Rachel
	AddEntityCollideCallback("Player", "Start_water_timer", "WaterFunc", true, 1);
	AddEntityCollideCallback("Player", "SpawnMonstersArea", "MonstersFunc", true, 1);
	AddEntityCollideCallback("Player", "CheckThingsArea", "CheckAllThings", false, 1);
	AddEntityCollideCallback("Player", "MomentoArea", "GiveMomento", true, 1);
	AddEntityCollideCallback("Player", "KillPlayerArea", "KillPlayerFunc", true, 1);
	AddEntityCollideCallback("Player", "KillPlayerArea_Checkpoint", "KillPlayerFunc_Checkpoint", true, 1);
	
	//Thx to Statyk on the forum for this one!!!!!
	SetEntityConnectionStateChangeCallback("lever_1", "lever1func"); 
	SetEntityConnectionStateChangeCallback("lever_2", "lever2func"); 
	SetEntityConnectionStateChangeCallback("lever_3", "lever3func"); 
	SetEntityConnectionStateChangeCallback("lever_4", "lever4func"); 
	SetEntityConnectionStateChangeCallback("lever_5", "lever5func"); 
	SetEntityConnectionStateChangeCallback("lever_6", "lever6func"); 

	SetLocalVarInt("lever1", 0);
	SetLocalVarInt("lever2", 0);  
	SetLocalVarInt("lever3", 0); 
	SetLocalVarInt("lever4", 0);
	SetLocalVarInt("lever5", 0);
	SetLocalVarInt("lever6", 0);
	
	//Monster hiding
	//SetEntityPlayerInteractCallback("note_generic_1", "MonsterFunc1", true);
	
	//Pushbarrel
	AddEntityCollideCallback("Player", "PushBarrelArea", "PushBarrelFunc", true, 1);
	
	//Preload music and particles
	PreloadParticleSystem("ps_fog_sky.ps");
	PreloadParticleSystem("ps_water_rain.ps");
	PreloadParticleSystem("ps_rising_water_erkkanstyle.ps");
	PreloadParticleSystem("ps_area_fog.ps");
	PreloadParticleSystem("ps_area_fog_large.ps");
	PreloadParticleSystem("ps_sinking_water_erkkanstyle.ps");
	//PreloadSound("17_steam_high.snt");
	
	//music maestro
	PlayMusic("01_amb_darkness.ogg", true, 1.0f, 2, 4, true);
	
	//Checkpoints
	AddEntityCollideCallback("Player", "CheckpointC1", "SetCheckpointC1Func", true, 1);
	AddEntityCollideCallback("Player", "CheckpointMelinda", "SetCheckpointC1Func", true, 1);
	
	//Turn off Countdown lights
	for (int x = 0; x <= 61; x += 1)
	{
		SetLightVisible("Number_" + x, false);
	}
	//Ending
	AddEntityCollideCallback("Player", "Ending", "EndingFunc", true, 1);
	
	//monster mohahah
	SetEntityPlayerInteractCallback("note_manual_1", "MonIncomFunc", true);
	
	//mosnter patrolling
	SetEntityActive("patroling_grunt_1", true);
	for (int x = 0; x <= 32; x += 1)
	{
		AddEnemyPatrolNode("patroling_grunt_1", "PatrolNode_" + x, 0, "");
	}
	
	//Clear Path nodes
	for (int x = 0; x <= 6; x += 1)
	{
		AddEntityCollideCallback("patroling_grunt_1", "Clear_" + x, "ClearNodes", true, 1);
	}
}
/*void ClearNodes(string &in asParent, string &in asChild, int alState)
{
	AddDebugMessage("monster nu ska du forsvinna", true);
	ClearEnemyPatrolNodes("patroling_grunt_1");
}*/
void BoxPushFunc(string &in asParent, string &in asChild, int alState)
{
	AddPropForce("Push_box", -3000.0f, 0, 0, "");
	AddDebugMessage("fanfanfan", true);
	GiveSanityDamage(10, true);
}
//Monster!!!!!!!!!!!!!!!
void MonIncomFunc(string &in asEntity) 
{
	SetEntityActive("servant_brute_nitro", true);
	for (int x = 6; x <= 15; x += 1)
	{
		AddEnemyPatrolNode("servant_brute_nitro", "PathNodeArea_" + x, 0, "");
	}
	AddQuest("DJMixing", "Quest7");
}
//Mixture the two things in the freezer
void MixFunc(string &in asParent, string &in asChild, int alState)
{
	AddGlobalVarInt("MixInt", 1);
	SetEntityActive("MixArea", false);
	SetEntityActive("MixArea2", true);
}

void MixFunc2(string &in asParent, string &in asChild, int alState)
{
	AddGlobalVarInt("MixInt", -1);
	SetEntityActive("MixArea2", false);
	SetEntityActive("MixArea", true);
}
void MixTimeDeathFunc(string &in asTimer)
{
	GivePlayerDamage(100, "BloodSplat", true, true);
	PlaySoundAtEntity("", "explosion_rock_large.snt", "Player", 0, false);
}
//scare
void SanityFunc(string &in asParent, string &in asChild, int alState)
{
	PlaySoundAtEntity("", "react_scare.snt", "Player", 0, false);
	GiveSanityDamage(0.4f, true);
}

//Monster ropes
/*void MonsterFunc1(string &in asEntity) 
{
	SetEntityActive("servant_brute_1", true); 
	AddEnemyPatrolNode("servant_brute_1", "PathNodeArea_1", 0, "");
	AddEnemyPatrolNode("servant_brute_1", "PathNodeArea_2", 1, "");
	AddEnemyPatrolNode("servant_brute_1", "PathNodeArea_3", 0, "");
	AddEnemyPatrolNode("servant_brute_1", "PathNodeArea_4", 0, "");
	AddEnemyPatrolNode("servant_brute_1", "PathNodeArea_5", 0, "");
}*/
///////////////////
//Bridge explosion
///////////////////

void MessageNitro(string &in asParent, string &in asChild, int alState)
{
	SetMessage("Message", "BlowBridge", 0);
}
void PlaceNitroFunc(string &in asItem, string &in asEntity)
{
	//First Explosion
	if(asEntity == "NitroArea_1")
	{
		SetEntityActive("oil1", true);
		SetEntityActive("GlassShards", true);
		ExplosionFunc1();
		CreateParticleSystemAtEntity("", "ps_hit_glass.ps", "PSArea1_1", false);
		PlaySoundAtEntity("", "break_glass_bottle.snt", "Player", 0, false);
		ExploTimerFunc();
		SetEntityActive("servant_brute_explo", true);
		SetEntityActive("servant_grunt_explo", true);
		ShowEnemyPlayerPosition("servant_grunt_explo");
		ShowEnemyPlayerPosition("servant_brute_explo");
		SetEntityActive("KillPlayerArea", false);
	}
	if(asEntity == "NitroArea_2")
	{
		SetEntityActive("oil2", true);
		ExplosionFunc1();
		CreateParticleSystemAtEntity("", "ps_hit_glass.ps", "PSArea1_2", false);
		PlaySoundAtEntity("", "break_glass_bottle.snt", "Player", 0, false);
		ExploTimerFunc();
		SetEntityActive("servant_brute_explo", true);
		SetEntityActive("servant_grunt_explo", true);
		ShowEnemyPlayerPosition("servant_grunt_explo");
		ShowEnemyPlayerPosition("servant_brute_explo");
		SetEntityActive("KillPlayerArea", false);
	}
	if(asEntity == "NitroArea_3")
	{
		SetEntityActive("oil3", true);
		ExplosionFunc1();
		CreateParticleSystemAtEntity("", "ps_hit_glass.ps", "PSArea1_3", false);
		PlaySoundAtEntity("", "break_glass_bottle.snt", "Player", 0, false);
		ExploTimerFunc();
		SetEntityActive("servant_brute_explo", true);
		SetEntityActive("servant_grunt_explo", true);
		ShowEnemyPlayerPosition("servant_grunt_explo");
		ShowEnemyPlayerPosition("servant_brute_explo");
		SetEntityActive("KillPlayerArea", false);
	}
	
	//Second Explosion
	if(asEntity == "NitroArea_4")
	{
		SetEntityActive("oil4", true);
		ExplosionFunc1();
		CreateParticleSystemAtEntity("", "ps_hit_glass.ps", "PSArea1_4", false);
		PlaySoundAtEntity("", "break_glass_bottle.snt", "Player", 0, false);
		ExploTimerFunc();
		SetEntityActive("servant_brute_explo", true);
		SetEntityActive("servant_grunt_explo", true);
		ShowEnemyPlayerPosition("servant_grunt_explo");
		ShowEnemyPlayerPosition("servant_brute_explo");
		SetEntityActive("KillPlayerArea", false);
	}
	if(asEntity == "NitroArea_5")
	{
		SetEntityActive("oil5", true);
		ExplosionFunc1();
		CreateParticleSystemAtEntity("", "ps_hit_glass.ps", "PSArea1_5", false);
		PlaySoundAtEntity("", "break_glass_bottle.snt", "Player", 0, false);
		ExploTimerFunc();
		SetEntityActive("servant_brute_explo", true);
		SetEntityActive("servant_grunt_explo", true);
		ShowEnemyPlayerPosition("servant_grunt_explo");
		ShowEnemyPlayerPosition("servant_brute_explo");
		SetEntityActive("KillPlayerArea", false);
	}
	if(asEntity == "NitroArea_6")
	{
		SetEntityActive("oil6", true);
		ExplosionFunc1();
		CreateParticleSystemAtEntity("", "ps_hit_glass.ps", "PSArea1_6", false);
		PlaySoundAtEntity("", "break_glass_bottle.snt", "Player", 0, false);
		ExploTimerFunc();
		SetEntityActive("servant_brute_explo", true);
		SetEntityActive("servant_grunt_explo", true);
		ShowEnemyPlayerPosition("servant_grunt_explo");
		ShowEnemyPlayerPosition("servant_brute_explo");
		SetEntityActive("KillPlayerArea", false);
	}
	if(asEntity == "NitroArea_7")
	{
		SetEntityActive("oil7", true);
		ExplosionFunc1();
		CreateParticleSystemAtEntity("", "ps_hit_glass.ps", "PSArea1_7", false);
		PlaySoundAtEntity("", "break_glass_bottle.snt", "Player", 0, false);
		ExploTimerFunc();
		SetEntityActive("servant_brute_explo", true);
		SetEntityActive("servant_grunt_explo", true);
		ShowEnemyPlayerPosition("servant_grunt_explo");
		ShowEnemyPlayerPosition("servant_brute_explo");
		SetEntityActive("KillPlayerArea", false);
	}
	
	//Third Explosion
	if(asEntity == "NitroArea_8")
	{
		SetEntityActive("oil8", true);
		ExplosionFunc1();
		CreateParticleSystemAtEntity("", "ps_hit_glass.ps", "PSArea1_8", false);
		PlaySoundAtEntity("", "break_glass_bottle.snt", "Player", 0, false);
		ExploTimerFunc();
		SetEntityActive("servant_brute_explo", true);
		SetEntityActive("servant_grunt_explo", true);
		ShowEnemyPlayerPosition("servant_grunt_explo");
		ShowEnemyPlayerPosition("servant_brute_explo");
		SetEntityActive("KillPlayerArea", false);
	}
	if(asEntity == "NitroArea_9")
	{
		SetEntityActive("oil9", true);
		ExplosionFunc1();
		CreateParticleSystemAtEntity("", "ps_hit_glass.ps", "PSArea1_9", false);
		PlaySoundAtEntity("", "break_glass_bottle.snt", "Player", 0, false);
		ExploTimerFunc();
		SetEntityActive("servant_brute_explo", true);
		SetEntityActive("servant_grunt_explo", true);
		ShowEnemyPlayerPosition("servant_grunt_explo");
		ShowEnemyPlayerPosition("servant_brute_explo");
		SetEntityActive("KillPlayerArea", false);
	}
	if(asEntity == "NitroArea_10")
	{
		SetEntityActive("oil10", true);
		ExplosionFunc1();
		CreateParticleSystemAtEntity("", "ps_hit_glass.ps", "PSArea1_10", false);
		PlaySoundAtEntity("", "break_glass_bottle.snt", "Player", 0, false);
		ExploTimerFunc();
		SetEntityActive("servant_brute_explo", true);
		SetEntityActive("servant_grunt_explo", true);
		ShowEnemyPlayerPosition("servant_grunt_explo");
		ShowEnemyPlayerPosition("servant_brute_explo");
		SetEntityActive("KillPlayerArea", false);
	}
	if(asEntity == "NitroArea_11")
	{
		SetEntityActive("oil11", true);
		ExplosionFunc1();
		CreateParticleSystemAtEntity("", "ps_hit_glass.ps", "PSArea1_11", false);
		PlaySoundAtEntity("", "break_glass_bottle.snt", "Player", 0, false);
		ExploTimerFunc();
		SetEntityActive("servant_brute_explo", true);
		SetEntityActive("servant_grunt_explo", true);
		ShowEnemyPlayerPosition("servant_grunt_explo");
		ShowEnemyPlayerPosition("servant_brute_explo");
		SetEntityActive("KillPlayerArea", false);
	}
}

// BOOM BOOM bang crash 
void ExploTimerFunc()
{
	SetMessage("Message", "ExploRun", 1.5f);
	AddTimer("Ex17", 8.0f, "Explo1Tim");
}
void Explo1Tim(string &in asTimer)
{
	if(asTimer == "Ex17")
	{
		for(int x = 0; x <= 11; x += 1)
		{
		SetEntityActive("oil" + x, false);
		}
	}
	
}
void Arne(string &in asName, int alCount) 
{
}
// BOOM BOOM bang crash 
void ExplosionFunc1()
{
	AddTimer("Bang1", 3.5f, "BANG");
	AddTimer("Bang2", 3.8f, "BANG");
	AddTimer("Bang3", 6.0f, "BANG");
	AddTimer("Bang4", 14.0f, "BANG");
	AddTimer("Bang5", 20.0f, "BANG");
	AddTimer("Bang6", 20.4f, "BANG");
	AddTimer("Bang7", 23.2f, "BANG");
	AddTimer("Bang8", 23.5f, "BANG");
	AddTimer("Bang9", 26.0f, "BANG");
	AddTimer("Bang10", 29.0f, "BANG");
}
void BANG(string &in asTimer)
{
	if(asTimer == "Bang1")
	{
		PlaySoundAtEntity("", "explosion_rock_large_loud.snt", "Player", 0.5f, false);
		StartScreenShake(0.4f, 1.5f, 0, 2);
		AddPlayerBodyForce(90000, 50000, 0, false);
		SetEntityActive("DeathAreaC", false);
		FadePlayerRollTo(60, 30, 25);
		GivePlayerDamage(50, "BloodSplat", true, false);
		GiveSanityDamage(60, false);
		SetEnemyDisabled("servant_grunt_explo", true);
		SetEnemyDisabled("servant_brute_explo", true);
		SetEntityActive("servant_grunt_explo", false);
		SetEntityActive("servant_brute_explo", false);
	}
	if(asTimer == "Bang2")
	{
		SetPlayerActive(false);
		FadeOut(0.7f);
		PlaySoundAtEntity("", "notice.snt", "Player", 0.5f, false);
	}
	if(asTimer == "Bang3")
	{	//All entities disappear and appear
		for(int x = 0; x <= 15; x += 1)
		{
			SetEntityActive("hub" + x, false);
			SetEntityActive("Pillar" + x, false);
		}
		SetEntityActive("torch_floor_9", false);
		SetEntityActive("torch_floor_12", false);
		SetEntityActive("torch_floor_7", false);
		SetEntityActive("torch_floor_6", false);
		SetEntityActive("torch_floor_8", false);
		SetEnemyDisabled("servant_brute_hunting", true);
		SetEnemyDisabled("servant_grunt_hunting", true);
		SetEntityActive("hub2_ex3", true);
		
		TeleportPlayer("AfterExplosion");
		PlaySoundAtEntity("", "react_breath_slow.snt", "Player", 0.5f, false);
	}
	if(asTimer == "Bang4")
	{
		SetPlayerLookSpeedMul(0.08f);
		SetPlayerMoveSpeedMul(0.05f);
		SetPlayerRunSpeedMul(0.1f);
		FadeImageTrailTo(3, 2);
		FadeSepiaColorTo(1.2f, 1);
		FadeIn(4);
		SetPlayerCrouching(true);
		PlaySoundAtEntity("", "react_breath_slow.snt", "Player", 0.5f, false);
	}
	if(asTimer == "Bang5")
	{
		FadeOut(0.2f);
		SetEntityActive("block_box_8", true);
		SetEntityActive("block_box_9", true);
		SetEntityActive("block_box_10", true);
	}
	if(asTimer == "Bang6")
	{
		FadeIn(0.2f);
		PlaySoundAtEntity("", "react_breath_slow.snt", "Player", 0.5f, false);
	}
	if(asTimer == "Bang7")
	{
		FadeOut(0.2f);
	}
	if(asTimer == "Bang8")
	{
		FadeIn(0.1f);
	}
	if(asTimer == "Bang9")
	{
		FadePlayerRollTo(40, 10, 20);
		SetPlayerSanity(50);
		FadeImageTrailTo(1, 1);
		PlaySoundAtEntity("", "react_breath.snt", "Player", 0.5f, false);
		SetPlayerActive(true);
	}
	if(asTimer == "Bang10")
	{
		FadeImageTrailTo(0, 1);
		SetPlayerHealth(35);
		FadePlayerRollTo(0, 30, 20);
		SetPlayerLookSpeedMul(0.8f);
		SetPlayerMoveSpeedMul(0.9f);
		SetPlayerRunSpeedMul(0.1f);
		SetEntityActive("", true);
		PlayMusic("09_amb_safe.ogg", true, 0.6f, 5.0f, 10, false);
		FadeSepiaColorTo(0, 4);
		CompleteQuest("Alive", "Quest8");
	}
}
void KillPlayerFunc(string &in asParent, string &in asChild, int alState)
{
	CheckPoint("Checkpoint3", "KillPlayerStart", "KillAgain", "Hints", "KillPlayerText");
	SetPlayerHealth(50);
	SetEntityActive("servant_brute_killer", true);
	ShowEnemyPlayerPosition("servant_brute_killer");
}
void KillPlayerFunc_Checkpoint(string &in asParent, string &in asChild, int alState)
{
	CheckPoint("Checkpoint3", "KillPlayerStart", "KillAgain", "Hints", "KillPlayerText");
}
void KillAgain(string &in asName, int alCount) 
{
	AddEntityCollideCallback("Player", "NitroMessageArea", "MessageNitro", true, 1);
	AddEntityCollideCallback("Player", "KillPlayerArea", "KillPlayerFunc", true, 1);
}
void DeathFuncEnd(string &in asParent, string &in asChild, int alState)
{
	SetPlayerHealth(0);
	SetPlayerSanity(0);
}

//Barrel scare
void PushBarrelFunc(string &in asParent, string &in asChild, int alState)
{
	AddPropForce("PushBarrel", -1000.0f, 0, 0, "");
	AddTimer("", 2.5f, "splashbarrel");
}
void splashbarrel(string &in asTimer)
{
	PlaySoundAtEntity("", "react_breath_slow.snt", "Player", 0, false);
	PlaySoundAtEntity("", "react_scare", "Player", 0, false);
}

///////////////
//Save Rachel
///////////////

//Give momento
void GiveMomento(string &in asParent, string &in asChild, int alState)
{
	CompleteQuest("Find_Rachel", "Quest1");
	AddQuest("Save_Rachel", "Quest6");
}

//Open Door
void CheckAllThings(string &in asParent, string &in asChild, int alState)
{
	AddDebugMessage("ja det fungerar", true);
	if(HasItem("Nitroglycerine") == true)
	{
		SetSwingDoorLocked("prison_2", false, false);
		SetEntityActive("CheckThingsArea", false);
		AddDebugMessage("det fungerar visst", true);
	}
}
void WaterFunc(string &in asParent, string &in asChild, int alState)
{
	//SetSwingDoorLocked("prison_2", true, true);
	//SetSwingDoorClosed("prison_2", true, true);
	SetEntityActive("Water_rise", true);
	AddTimer("WaterSink", 1, "WaterCheck");	
	AddTimer("THelpSave", 90, "HelpSave");	
	SetLocalVarInt("WaterDown", 0);
	SetLocalVarInt("CountTo", 0);
	CheckPoint("Checkpoint2", "DieRunOut", "SurviveFunc", "Hints", "RunOuts");
	PlayMusic("14_elevator_falls.ogg", true, 1.0f, 3.0f, 5, false);
	//Timer60Func();
	
}
//if Player dies first Timer
void SurviveFunc(string &in asName, int alCount) 
{
	AddEntityCollideCallback("Player", "KillPlayerArea", "KillPlayerFunc", true, 1);
	AddEntityCollideCallback("Player", "NitroMessageArea", "MessageNitro", true, 1);
	if(alCount == 0)
	{
		SetEntityActive("SpawnMonstersArea", true);
		AddEntityCollideCallback("Player", "SpawnMonstersArea", "MonstersFunc", true, 1);
	}
	if(alCount == 1)
	{
		SetEntityActive("SpawnMonstersArea", true);
		AddEntityCollideCallback("Player", "SpawnMonstersArea", "MonstersFunc", true, 1);
	}
	if(alCount == 2)
	{
		SetEntityActive("SpawnMonstersArea", true);
		AddEntityCollideCallback("Player", "SpawnMonstersArea", "MonstersFunc", true, 1);
	}
}
//Levers state to sink the water, thx again to Statyk for help on the forum!!!!!!
void lever1func(string &in asEntity, int alState)
{
	if(alState == 1) 
	{
		SetLocalVarInt("lever1", 1);
		CheckFunction();
		AddDebugMessage("Bjas", true);
	}
	if(alState == 0)
	{
		SetLocalVarInt("lever1", 0);
	}
	if(alState == -1)
	{
		SetLocalVarInt("lever1", 0);
	}
}
void lever2func(string &in asEntity, int alState)
{
	if(alState == 1) 
	{
		SetLocalVarInt("lever2", 0);
	}
	if(alState == 0) 
	{
		SetLocalVarInt("lever2", 0);
	}
	if(alState == -1)
	{
		SetLocalVarInt("lever2", 1);
		CheckFunction(); 
	}
}
void lever3func(string &in asEntity, int alState)
{
	if(alState == 1) 
	{
		SetLocalVarInt("lever3", 0);
	}
	if(alState == 0) 
	{
		SetLocalVarInt("lever3", 0);
	}
	if(alState == -1)
	{
		SetLocalVarInt("lever3", 1);
		CheckFunction(); 
	}
}
void lever4func(string &in asEntity, int alState)
{
	if(alState == 1) 
	{
		SetLocalVarInt("lever4", 0);
	}
	if(alState == 0) 
	{
		SetLocalVarInt("lever4", 0);
	}
	if(alState == -1)
	{
		SetLocalVarInt("lever4", 1);
		CheckFunction(); 
	}
}
void lever5func(string &in asEntity, int alState)
{
	if(alState == 1) 
	{
		SetLocalVarInt("lever5", 1);
		CheckFunction();
		
	}
	if(alState == 0)
	{
		SetLocalVarInt("lever5", 0);
	}
	if(alState == -1)
	{
		SetLocalVarInt("lever5", 0);
	}
}
void lever6func(string &in asEntity, int alState)
{
	if(alState == 1) 
	{
		SetLocalVarInt("lever6", 0);
	}
	if(alState == 0) 
	{
		SetLocalVarInt("lever6", 0);
	}
	if(alState == -1)
	{
		SetLocalVarInt("lever6", 1);
		CheckFunction(); 
	}
}
void CheckFunction()
{
	if(GetLocalVarInt("lever1") == 1 && GetLocalVarInt("lever2") == 1 && GetLocalVarInt("lever3") == 1 && GetLocalVarInt("lever4") == 1 && GetLocalVarInt("lever5") == 1 && GetLocalVarInt("lever6") == 1)
	{
		GiveSanityBoost();
		AddLocalVarInt("WaterDown", 1);
		AddEffectVoice("RachelEndLine.ogg", "", "Flashbacks", "Rachel1", false, "", 0, 0);
		SetEntityActive("SpawnMonstersArea", true);
		SetEntityActive("castle_gate_arched01_open_1", true);
		SetEntityActive("castle_gate_arched01_1", false);
		SetSwingDoorLocked("prison_2", false, false);
		AddEntityCollideCallback("Player", "CloseDoorArea", "CloseDoorFunc", true, 1);
		PlayMusic("10_puzzle02.ogg", true, 0.6f, 0.5f, 6, false);
		SetMoveObjectState("ladder", -1);
		SetMoveObjectState("ladder_1", -1);
		PlaySoundAtEntity("LadderSound", "20_wheel_run.snt", "LadderSoundArea", 1, true);
		AddTimer("", 8.8f, "Fayde");
		RemoveTimer("THelpSave");
		SetEntityInteractionDisabled("lever_1", true);
		SetEntityInteractionDisabled("lever_2", true);
		SetEntityInteractionDisabled("lever_3", true);
		SetEntityInteractionDisabled("lever_4", true);
		SetEntityInteractionDisabled("lever_5", true);
		SetEntityInteractionDisabled("lever_6", true);
		CompleteQuest("Save_Rachel", "Quest6");
		AddTimer("", 15, "LiveLong");
	}
}
void LiveLong(string &in asTimer)
{
	AddQuest("Alive", "Quest8");
}
void Fayde(string &in asTimer)
{
	StopSound("LadderSound", 1);
}
//WaterDown
void WaterCheck(string &in asTimer)
{
	AddLocalVarInt("CountTo", 1);
	if(GetLocalVarInt("CountTo") <= 30) 
	{
		AddDebugMessage("under 30", true);
		if(GetLocalVarInt("WaterDown") == 1) 
			{
				SetEntityActive("Water_sinking1", true);
				AddTimer("", 1.1f, "SinkFunc");
				AddLocalVarInt("CountTo", 10000);
				DestroyPS();
				AddTimer("Sink1", 15, "PlainBot");
			}
		AddTimer("WaterSink", 1, "WaterCheck");
	}

	if(GetLocalVarInt("CountTo") > 30 && GetLocalVarInt("CountTo") <= 60)	
	{
		AddDebugMessage("30 till 60", true);
		AddLocalVarInt("SpawnPS", 1);
		if(GetLocalVarInt("WaterDown") == 1) 
			{
				SetEntityActive("Water_sinking2", true);
				AddLocalVarInt("CountTo", 10000);
				AddTimer("", 1.1f, "SinkFunc");
				DestroyPS();
				AddTimer("Sink1", 30, "PlainBot");
			}
		AddTimer("WaterSink", 1, "WaterCheck");
	}
	if(GetLocalVarInt("CountTo") > 60 && GetLocalVarInt("CountTo") <= 90) 
	{
		AddDebugMessage("60 till 90", true);
		if(GetLocalVarInt("WaterDown") == 1) 
			{
				SetEntityActive("Water_sinking3", true);
				AddLocalVarInt("CountTo", 10000);
				AddTimer("", 1.1f, "SinkFunc");
				DestroyPS();
				AddTimer("Sink1", 60, "PlainBot");
			}
		AddTimer("WaterSink", 1, "WaterCheck");	
	}
	if(GetLocalVarInt("CountTo") > 90 && GetLocalVarInt("CountTo") <= 120) 
	{
		AddDebugMessage("90 till 120", true);
		if(GetLocalVarInt("WaterDown") == 1) 
			{
				SetEntityActive("Water_sinking4", true);
				AddLocalVarInt("CountTo", 10000);
				AddTimer("", 1.1f, "SinkFunc");
				DestroyPS();
				AddTimer("Sink1", 90, "PlainBot");
			}
		AddTimer("WaterSink", 1, "WaterCheck");	
	}
	if(GetLocalVarInt("CountTo") > 120 && GetLocalVarInt("CountTo") <= 180) 
	{
		AddDebugMessage("over 120", true);
		SetEntityActive("Water_plan", true);
		DestroyPS();
		SetSwingDoorLocked("prison_2", false, false);
		SetMessage("Message", "FAIL", 0);
		PlayMusic("dan_grunt.ogg", true, 0.8f, 5, 6, true);
		SetEntityActive("SpawnMonstersArea", true);
		SetEntityActive("castle_gate_arched01_open_1", true);
		SetEntityActive("castle_gate_arched01_1", false);
		AddEntityCollideCallback("Player", "CloseDoorArea", "CloseDoorFunc", true, 1);
		SetEntityInteractionDisabled("lever_1", true);
		SetEntityInteractionDisabled("lever_2", true);
		SetEntityInteractionDisabled("lever_3", true);
		SetEntityInteractionDisabled("lever_4", true);
		SetEntityInteractionDisabled("lever_5", true);
		SetEntityInteractionDisabled("lever_6", true);
		CompleteQuest("Save_Rachel", "Quest6");
		AddQuest("Alive", "Quest8");
		AddTimer("", 0.7f, "Switch");
	}
}
void Switch(string &in asTimer)
{
	SetEntityActive("Water_rise", false);
}
void HelpSave(string &in asTimer)
{
	SetMessage("Message", "HintSave", 0);
}
void SinkFunc(string &in asTimer)
{
	SetEntityActive("Water_rise", false);
}
void DestroyPS()
{
	StopSound("Sound_2", 3.0f);
	for(int x = 60; x <= 175; x += 1)
	{
		DestroyParticleSystem("ParticleSystem_" + x);
	}
}
void PlainBot(string &in asTimer)
{
	SetEntityActive("Water_plan_bot", true);
}

//Monsters after water, Survive!!!!
void MonstersFunc(string &in asParent, string &in asChild, int alState)
{
	SetEntityActive("servant_grunt_hunting", true);
	SetEntityActive("servant_brute_hunting", true);
	PlayMusic("dan_grunt.ogg", true, 1.0f, 1, 7, true);
	
	for (int x = 0; x <= 28; x += 1)
	{
		AddEnemyPatrolNode("servant_grunt_hunting", "PathNodeArea_Grunt_" + x, 0, "");
		AddEnemyPatrolNode("servant_brute_hunting", "PathNodeArea_Brute_" + x, 0, "");
	}
}
void CloseDoorFunc(string &in asParent, string &in asChild, int alState)
{
	SetSwingDoorLocked("prison_2", true, true);
	SetSwingDoorClosed("prison_2", true, true);
}
/*void Timer60Func()
{
	AddTimer("CountDown", 1, "TimerFunc");	
	SetLocalVarInt("Count", 0);
	SetLocalVarInt("Delete", -1);
}
void TimerFunc(string &in asTimer)
{
	AddLocalVarInt("Count", 1);
	AddLocalVarInt("Delete", 1);
	
	SetLightVisible("Number_" + GetLocalVarInt("Count"), true);
	SetLightVisible("Number_" + GetLocalVarInt("Delete"), false);
	
	if(GetLocalVarInt("Conut") < 61) 
	{
		AddTimer("CountDown", 1, "TimerFunc");	
	}
}*/
//Checkpoints

void SetCheckpointC1Func(string &in asParent, string &in asChild, int alState)
{
	if(asChild == "CheckpointMelinda")
	{
		CheckPoint("Checkpoint1", "CheckpointStartC1", "RespawnMonster", "", "");
	}
}
void RespawnMonster(string &in asName, int alCount) 
{
	SetEntityActive("patroling_grunt_1", true);
	for (int x = 0; x <= 32; x += 1)
	{
		AddEnemyPatrolNode("patroling_grunt_1", "PatrolNode_" + x, 0, "");
	}
		//Clear Path nodes
	for (int x = 0; x <= 6; x += 1)
	{
		AddEntityCollideCallback("patroling_grunt_1", "Clear_" + x, "ClearNodes", true, 1);
	}
	
}

////////////////
//Steam Scare
////////////////

void SteamFunc(string &in asParent, string &in asChild, int alState)
{
	for (int x = 0; x <= 5; x += 1)
		{
			CreateParticleSystemAtEntity("", "ps_steam_deadly.ps", "SpawnPS_" + x, false);
		}
	PlaySoundAtEntity("steam", "17_steam_high.snt", "SteamArea", 0, true);
	AddPlayerBodyForce(-20000, 0, -15000, false);
	AddTimer("", 2.8f, "BAJS");
}
void BAJS(string &in asTimer)
{
	StopSound("steam", 1);
}

//Ending
void EndingFunc(string &in asParent, string &in asChild, int alState)
{
	StartEffectFlash(28, 0.5f, 1);
	FadeOut(25);
	StartPlayerLookAt("LookAtEnd", 0.5f, 1, "");
	AddTimer("End1", 1, "Credits");
	AddTimer("End2", 30, "Credits");
}
void Credits(string &in asTimer)
{
	if(asTimer == "End1")
	{
		SetPlayerActive(false);
		AddEffectVoice("AdamEndingNo2.ogg", "", "Flashbacks", "Adam4", false, "", 0, 0);
		AddEffectVoice("AdamEndingNo3.ogg", "", "Flashbacks", "Adam5", false, "", 0, 0);
		AddEffectVoice("AdamEndingNo4.ogg", "", "Flashbacks", "Adam6", false, "", 0, 0);
	}
	if(asTimer == "End2")
	{
		StartCredits("", true, "Ending", "MainCredits", 3);
	}
}
void OnEnter()
{
}
void OnLeave()
{
}