void OnStart()
{
PreloadSound("roar.snt");
PreloadParticleSystem("ps_break_mansionbase_wall.ps");


//CHECK ITEMS
AddEntityCollideCallback("Player", "has_items_area", "Checking_Items_1", true, 1);
AddEntityCollideCallback("Player", "start_credits", "start_credits1", true, 1);
AddEntityCollideCallback("Player", "break_beam_area", "break_the_beam", true, 1);
}

void OnEnter()
{
PlayMusic("29_amb_end_daniel.ogg", true, 1.0, 2.0, 0, true);
}

void break_the_beam(string &in asParent, string &in asChild, int alState)
{
SetEntityActive("wood_beam_2", false);
SetEntityActive("wood_beam_2b", true);
PlaySoundAtEntity("", "break_wood.snt", "Player", 0, false);
CreateParticleSystemAtEntity("", "ps_break_mansionbase_wall.ps", "break_beam_area", false);
}


void start_credits1(string &in asParent, string &in asChild, int alState)
{
StartEffectFlash(2.0, 100, 50);
AddTimer("", 3.0, "fading_out");
}

void fading_out(string &in asTimer)
{
FadeOut(2.0);
SetLanternActive(false, false);
SetPlayerActive(false);
TeleportPlayer("ending_player");
AddEffectVoice("ending1", "agrippa_1", "Flashbacks", "Ending1", false, "", 1, 5);
AddEffectVoice("ending2", "agrippa_1", "Flashbacks", "Ending2", false, "", 1, 5);
AddTimer("", 24.0, "fading_out2");
}

void fading_out2(string &in asTimer)
{
StartCredits("drumsofwar.ogg", true, "Ending", "MainCredits", 0);
}


//FIRST TIME CHECK
void Checking_Items_1(string &in asParent, string &in asChild, int alState)
{
		if(GetGlobalVarInt("Saw")==1 && GetGlobalVarInt("Knife")==1)
	{
	AddEffectVoice("01_G", "agrippa_1", "Flashbacks", "Gordon1", false, "", 1, 5);
	AddEffectVoice("02_G", "agrippa_1", "Flashbacks", "Gordon2", false, "", 1, 5);
	AddEffectVoice("03_G", "agrippa_1", "Flashbacks", "Gordon3", false, "", 1, 5);
	AddEffectVoice("04_G", "agrippa_1", "Flashbacks", "Gordon4", false, "", 1, 5);
	AddEffectVoice("05_G", "agrippa_1", "Flashbacks", "Gordon5", false, "", 1, 5);
	AddEffectVoice("06_G", "agrippa_1", "Flashbacks", "Gordon6", false, "", 1, 5);
	AddEffectVoice("07_G", "agrippa_1", "Flashbacks", "Gordon7", false, "", 1, 5);
	AddEntityCollideCallback("Player", "reset_speech", "Checking_Items_2", true, 1);
	SetupLoadScreen("LoadingText", "ending", 0, "screen1.jpg");
	AddQuest("sacquest", "SacQuest");
	
	}
	
		if(GetGlobalVarInt("Saw")==2 && GetGlobalVarInt("Knife")==1)
	{
	AddEffectVoice("01_G", "agrippa_1", "Flashbacks", "Gordon1", false, "", 1, 5);
	AddEffectVoice("02_G", "agrippa_1", "Flashbacks", "Gordon2", false, "", 1, 5);
	AddEffectVoice("03_G", "agrippa_1", "Flashbacks", "Gordon3", false, "", 1, 5);
	AddEffectVoice("04_G", "agrippa_1", "Flashbacks", "Gordon4", false, "", 1, 5);
	AddEffectVoice("05_G", "agrippa_1", "Flashbacks", "Gordon5", false, "", 1, 5);
	AddEffectVoice("06_G", "agrippa_1", "Flashbacks", "Gordon6", false, "", 1, 5);
	AddEffectVoice("07_G", "agrippa_1", "Flashbacks", "Gordon7", false, "", 1, 5);
	AddEffectVoice("has_saw_G", "agrippa_1", "Flashbacks", "Gordon9", false, "", 1, 5);
	AddEntityCollideCallback("Player", "reset_speech", "Checking_Items_2", true, 1);
	SetupLoadScreen("LoadingText", "ending", 0, "screen1.jpg");
	AddQuest("sacquest", "SacQuest");
	}
	
		if(GetGlobalVarInt("Saw")==1 && GetGlobalVarInt("Knife")==2)
	{
	AddEffectVoice("01_G", "agrippa_1", "Flashbacks", "Gordon1", false, "", 1, 5);
	AddEffectVoice("02_G", "agrippa_1", "Flashbacks", "Gordon2", false, "", 1, 5);
	AddEffectVoice("03_G", "agrippa_1", "Flashbacks", "Gordon3", false, "", 1, 5);
	AddEffectVoice("04_G", "agrippa_1", "Flashbacks", "Gordon4", false, "", 1, 5);
	AddEffectVoice("05_G", "agrippa_1", "Flashbacks", "Gordon5", false, "", 1, 5);
	AddEffectVoice("06_G", "agrippa_1", "Flashbacks", "Gordon6", false, "", 1, 5);
	AddEffectVoice("07_G", "agrippa_1", "Flashbacks", "Gordon7", false, "", 1, 5);
	AddEffectVoice("has_knife_G", "agrippa_1", "Flashbacks", "Gordon8", false, "", 1, 5);
	AddEntityCollideCallback("Player", "reset_speech", "Checking_Items_2", true, 1);
	SetupLoadScreen("LoadingText", "ending", 0, "screen1.jpg");
	AddQuest("sacquest", "SacQuest");
	}
	
		if(GetGlobalVarInt("Saw")==2 && GetGlobalVarInt("Knife")==2)
	{
	AddEffectVoice("01_G", "agrippa_1", "Flashbacks", "Gordon1", false, "", 1, 5);
	AddEffectVoice("02_G", "agrippa_1", "Flashbacks", "Gordon2", false, "", 1, 5);
	AddEffectVoice("03_G", "agrippa_1", "Flashbacks", "Gordon3", false, "", 1, 5);
	AddEffectVoice("04_G", "agrippa_1", "Flashbacks", "Gordon4", false, "", 1, 5);
	AddEffectVoice("05_G", "agrippa_1", "Flashbacks", "Gordon5", false, "", 1, 5);
	AddEffectVoice("06_G", "agrippa_1", "Flashbacks", "Gordon6", false, "", 1, 5);
	AddEffectVoice("has_both_G", "agrippa_1", "Flashbacks", "GordonB", false, "", 1, 5);
	AddTimer("wait4finish", 54, "get_ready");
	SetupLoadScreen("LoadingText", "ending", 0, "screen1.jpg");
	AddQuest("sacquest", "SacQuest");
	}
	
}

void get_ready(string &in asTimer)
{
AddUseItemCallback("stabby", "cermony_knife", "use_items_area", "stabbing_g", true);
}

//RESET CHECK ITEMS
void Checking_Items_2(string &in asParent, string &in asChild, int alState)
{
AddEntityCollideCallback("Player", "has_items_area", "Checking_Items_3", true, 1);
}

//SECOND TIME AND THEN ON
void Checking_Items_3(string &in asParent, string &in asChild, int alState)
{
		if(GetGlobalVarInt("Saw")==1 && GetGlobalVarInt("Knife")==1)
	{
	AddEffectVoice("05_G", "agrippa_1", "Flashbacks", "Gordon5", false, "", 1, 5);
	AddEffectVoice("06_G", "agrippa_1", "Flashbacks", "Gordon6", false, "", 1, 5);
	AddEffectVoice("07_G", "agrippa_1", "Flashbacks", "Gordon7", false, "", 1, 5);
	AddEntityCollideCallback("Player", "reset_speech", "Checking_Items_2", true, 1);
	
	}
	
		if(GetGlobalVarInt("Saw")==2 && GetGlobalVarInt("Knife")==1)
	{
	AddEffectVoice("05_G", "agrippa_1", "Flashbacks", "Gordon5", false, "", 1, 5);
	AddEffectVoice("06_G", "agrippa_1", "Flashbacks", "Gordon6", false, "", 1, 5);
	AddEffectVoice("07_G", "agrippa_1", "Flashbacks", "Gordon7", false, "", 1, 5);
	AddEffectVoice("has_saw_G", "agrippa_1", "Flashbacks", "Gordon9", false, "", 1, 5);
	AddEntityCollideCallback("Player", "reset_speech", "Checking_Items_2", true, 1);
	}
	
		if(GetGlobalVarInt("Saw")==1 && GetGlobalVarInt("Knife")==2)
	{
	AddEffectVoice("05_G", "agrippa_1", "Flashbacks", "Gordon5", false, "", 1, 5);
	AddEffectVoice("06_G", "agrippa_1", "Flashbacks", "Gordon6", false, "", 1, 5);
	AddEffectVoice("07_G", "agrippa_1", "Flashbacks", "Gordon7", false, "", 1, 5);
	AddEffectVoice("has_knife_G", "agrippa_1", "Flashbacks", "Gordon8", false, "", 1, 5);
	AddEntityCollideCallback("Player", "reset_speech", "Checking_Items_2", true, 1);
	}
	
		if(GetGlobalVarInt("Saw")==2 && GetGlobalVarInt("Knife")==2)
	{
	AddEffectVoice("05_G", "agrippa_1", "Flashbacks", "Gordon5", false, "", 1, 5);
	AddEffectVoice("06_G", "agrippa_1", "Flashbacks", "Gordon6", false, "", 1, 5);
	AddEffectVoice("has_both_G", "agrippa_1", "Flashbacks", "GordonB", false, "", 1, 5);
	AddTimer("wait4finish", 31, "get_ready");
	}
	
}
	
void stabbing_g(string &in asItem, string &in asEntity)
{
CompleteQuest("sacquest", "SacQuest");
SetPlayerActive(false);
SetLanternActive(false, false);
StartPlayerLookAt("look_chest", 20, 20, "");
SetEntityActive("ceremony_knife_1", true);
AddTimer("", 1.0, "Looking_Up");
AddTimer("", 10.0, "Saw_Head");
AddTimer("", 10.0, "Saw_sound1");
AddTimer("", 13.0, "Saw_sound2");
}

void Looking_Up(string &in asTimer)
{
StopPlayerLookAt();
AddTimer("", 0.5, "Looking_chest");
StartPlayerLookAt("look_up", 20, 20, "");
SetEntityActive("ceremony_knife_1", false);
SetEntityActive("ceremony_knife_2", true);
}

void Looking_chest(string &in asTimer)
{
PlaySoundAtEntity("", "slime_attack_normal_hit.snt", "Player", 0, false);
StopPlayerLookAt();
StartPlayerLookAt("look_chest", 20, 20, "");
CreateParticleSystemAtEntityExt("bloody", "ps_impact_water_high.ps", "blood_area", false, 1, 0, 0, 1, false, 0, 0, 0, 0);
PlaySoundAtEntity("screamy", "15_the_big_scream1.snt", "Player", 0.5, false);
FadeOut(1.0);

}

void Saw_Head(string &in asTimer)
{
PlaySoundAtEntity("screaming1", "23_saw_voice3.snt", "Player", 0.5, false);
PlaySoundAtEntity("sawwry", "23_saw1.snt", "Player", 0.5, false);
AddTimer("", 12.0, "give_heart");
SetEntityActive("ceremony_knife_2", false);
RemoveItem("cermony_knife");
RemoveItem("BoneSaw");
}

void Saw_sound1(string &in asTimer)
{
PlaySoundAtEntity("sawwry", "23_saw2.snt", "Player", 0, false);
SetEntityActive("agrippa_headless_1", true);
SetEntityActive("agrippa_head_1", true);
SetEntityActive("blood_spatter01_1", true);
SetEntityActive("blood_spatter03_1", true);
SetEntityActive("blood_spatter04_1", true);
SetEntityActive("BoneSaw", true);
SetEntityActive("agrippa_1", false);
}

void Saw_sound2(string &in asTimer)
{
PlaySoundAtEntity("sawwry", "23_saw3.snt", "Player", 0, false);
}

void give_heart(string &in asTimer)
{
AddQuest("heartquest", "HeartQuest");
SetMessage("Messages", "GiveHeart", 2.0);
PlaySoundAtEntity("impact", "impact_organic_high.snt", "Player", 0, false);
FadeIn(1.0);
SetPlayerActive(true);
StopPlayerLookAt();
StopSound("screaming1", 0.5);
StopSound("sawwry", 0.5);
GiveItem("Heart", "Heart", "Heart", "orbpiece_crimson.tga", 1);
AddUseItemCallback("ending_this_game", "Heart", "orb_pedestal_1", "opening_door", true);
}



void opening_door(string &in asItem, string &in asEntity)
{
CompleteQuest("heartquest", "HeartQuest");
SetEntityActive("heart", true);
RemoveItem("Heart");
SetEntityActive("castle_gate_arched01_1", false);
SetEntityActive("castle_gate_arched01_open_1", true);
SetEntityPlayerLookAtCallback("look_area", "change_sky", true);
PlaySoundAtEntity("", "door_level_wood_open.snt", "Player", 0, false);
PlaySoundAtEntity("", "roar.snt", "Player", 0.5, false);
AddTimer("", 2.0, "burning_heart");
PlayMusic("29_event_end.ogg", false, 0.5f, 0, 0, false);
PlaySoundAtEntity("rumble", "general_rock_rumble.snt", "Player", 1.0, false);
}

void burning_heart(string &in asTimer)
{
StartScreenShake(0.1, 10, 10, 10);
SetEntityActive("burner1", true);
SetEntityActive("burner2", true);
AddTimer("", 2.0, "look_door");
}

void look_door(string &in asTimer)
{
StartPlayerLookAt("torch_static01_1", 3, 3, "");
AddTimer("", 1.0, "look_door2");
SetEntityActive("heart", false);
SetEntityActive("servant_grunt_1", true);
SetEntityActive("servant_brute_1", true);
SetEntityActive("enemy_suitor_1", true);
ShowEnemyPlayerPosition("servant_grunt_1");
ShowEnemyPlayerPosition("servant_brute_1");
}

void look_door2(string &in asTimer)
{
StopPlayerLookAt();
StartPlayerLookAt("reset_speech", 7, 7, "");
SetLampLit("torch_static01_2", true, false);
SetLampLit("torch_static01_1", true, false);
AddTimer("", 1.0, "look_door3");
}

void look_door3(string &in asTimer)
{
StopPlayerLookAt();
StopSound("rumble", 4.0);
}

void change_sky(string &in entity, int alState)
{
PlayMusic("29_event_end.ogg", false, 0.5f, 0, 0, false);
SetSkyBoxColor(1, 1, 1, 1);
ShowEnemyPlayerPosition("enemy_suitor_1");
}