#include "scripts/common.txt"
#include "scripts/bp_common.txt"
#include "scripts/map/common_level_script.txt"

//Oepning Cinematic MapID Sequence: 5->0->5->4

bool bStartedIntro;
bool bNewGame;
bool reachedEnd = false;
const kVec3 portalEndPos(-2066, 4795, 972);
kActor @cutImage;
//------------------------------------------------------------------------------------------------------------------------
// Start
//------------------------------------------------------------------------------------------------------------------------
$script 0
{
    GameVariables.GetBool("g_newgame", bNewGame);
    GameVariables.GetBool("bStartedHubIntro", bStartedIntro);
    if (bNewGame) {
        // make sure all chronoscepter and key pieces have been cleared when
        // starting a new game
        GameVariables.SetValue("chronoPieceFlags", "0");
        GameVariables.SetValue("hubPanelPieces1", "0");
        GameVariables.SetValue("hubPanelPieces2", "0");
        GameVariables.SetValue("hubPanelPieces3", "0");
        GameVariables.SetValue("hubPanelPieces4", "0");
        GameVariables.SetValue("hubPanelPieces5", "0");
        GameVariables.SetValue("hubPanelPieces6", "0");
        GameVariables.SetValue("hubPanelPieces7", "0");
    }
    
	//if played StartIntro then play intro
	if (bStartedIntro && !bNewGame) {
		if (HasPlayedIntro1()) {
			Game.CallDelayedMapScript(3, instigator, 0.0f); //start game
		} else {
			Game.CallDelayedMapScript(1, instigator, 0.0f); //start intro1
		}
	} else {
		Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_NO_INITIAL_FADEOUT);
		Camera.fov = 47.5f;
		Camera.origin = kVec3(0.0f, 5000.0f, 0.0f);
		Camera.yaw = 0;
		Camera.pitch = 0;
		Camera.roll = 0;
		
		int deathCount = 0;
		GameVariables.GetInt("BP.introdeath", deathCount);
		// Sys.Print("deathCount  " + deathCount);
		if (deathCount > 0) {
			deathCount--;
			GameVariables.SetValue("BP.introdeath", "" + deathCount);
			PlayLoop.HandlePlayerDeath();
			return;
		}
		Camera.StopCinematic();
		
		//debug testing level4 intro
		//bStartedIntro = true;
		//GameVariables.SetValue("bStartedHubIntro", "1");
		//PlayLoop.ChangeMap("levels/level04.map");
		
		//debug testing direct to game
		// bStartedIntro = true;
		// GameVariables.SetValue("bStartedHubIntro", "1");
		// SetPlayedIntro1(true);
		// PlayLoop.ChangeMap("levels/level04.map"); //first level
		// PlayLoop.ChangeMap("levels/level06.map"); //second level
		//PlayLoop.ChangeMap("levels/level08.map"); //third level
		//PlayLoop.ChangeMap("levels/level10.map"); //forth level
		//PlayLoop.ChangeMap("levels/level00.map"); //forth level
		// PlayLoop.ChangeMap("levels/level53.map"); //boss arena
		// PlayLoop.ChangeMap("levels/level52.map"); //shop
		// PlayLoop.ChangeMap("levels/level57.map");
		
		Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_NO_INITIAL_FADEOUT);
		Camera.fov = 47.5f;
		Camera.origin = kVec3(0.0f, 5000.0f, 0.0f);
		Camera.yaw = 0;
		Camera.pitch = 0;
		Camera.roll = 0;
		Game.PrintLine("Turok Dinosaur Hunter", 0, 120); //bottom
		Game.PrintLine("", 1, 120); //middle
		Game.PrintLine("Previously on", 2, 120); //top
		delay(2.0f);
		PlayLoop.ChangeMap("levels/level00.map"); //start StartIntro
		
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Start Intro
//------------------------------------------------------------------------------------------------------------------------
$script 1 {
	Game.StopMusic();
	Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_NO_INITIAL_FADEOUT);
    Camera.fov = 47.5f;
    Camera.origin = kVec3(0.0f, 5000.0f, 0.0f);
	Camera.yaw = 0;
	Camera.pitch = 0;
	Camera.roll = 0;
	
	kVec3 imageStartPos = kVec3(0.0f, 5300.0f, -0.0f);
	@cutImage = ActorFactory.Spawn("BP_CUT_ADON1", imageStartPos.x, imageStartPos.y, imageStartPos.z, 0, 0);
	cutImage.Yaw() = Math::Deg2Rad(180);
	
	Game.CallDelayedMapScript(2, instigator, 0); //update intro
	
	//tex 0 = adon close up
	//tex 1 = adon talking front
	//tex 2 = adon walking away
	//tex 3 = nice head
	//tex 4 = normal head
	//tex 5 = angry head
	//tex 6 = mystery evil
	//tex 7 = black
	//tex 8 = stars in space
	
	Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_NO_INITIAL_FADEOUT);
	cutImage.RenderModel().SetTexture(0, 8);
	cutImage.Scale() = kVec3(2.0f, 2.0f, 2.0f);
	Game.PlaySound("sounds/shaders/ominous.ksnd");
	Game.CallDelayedMapScript(5, instigator, 0.0f); //Move cutImage to the left slowly
	delay(2.0f);
	DoCutScene(3.0f, 8, "", "", "Somewhere across time and space...");
	delay(1.0f);
	Game.HaltMapScript(5);
	cutImage.Origin() = imageStartPos;
	cutImage.Scale() = kVec3(1.0f, 1.0f, 1.0f);
	Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_NO_INITIAL_FADEOUT);

	//cut_intro01 (6sec)- Normal Head: Our decision is made, speaker. Your place is here.
	DoCutScene("sounds/shaders/cut_intro01.ksnd", 6.0f, 4, "Our decision is made speaker", "Your place is here", "");
	
	//cut_intro02 (2sec) - Adon: I can save him
	DoCutScene("sounds/shaders/cut_intro02.ksnd", 2.0f, 0, "I can save him", "", "");
	
	//cut_intro03 (4sec) - Nice Head: and serve what purpose? his death was ordained
	DoCutScene("sounds/shaders/cut_intro03.ksnd", 4.0f, 3, "And serve what purpose?", "His death was ordained", "");

	//cut_intro04 (9sec) - Adon: I have served the coucil since the time of my mothers death. In all that time I have never questioned it's wisdom or it's motives.
	DoCutScene("sounds/shaders/cut_intro04.ksnd", 4.0f, 1, "I have served the coucil since", "the time of my mothers death", "");
	DoCutScene(5.0f, 1, "In all that time I have never", "questioned it's wisdom or it's motives", "");
	
	//cut_intro05 (9sec) - Angry Head: Mind your tongue! You are a speaker of forever light. It is your place to serve this council. Nothing more. We will hear no more of this.
	DoCutScene("sounds/shaders/cut_intro05.ksnd", 5.5f, 5, "Mind your tongue!", "You are a speaker of forever light", "It is your place to serve this council");
	DoCutScene(3.5f, 5, "Nothing more", "We will hear no more of this", "");
	
	//cut_intro06 (3sec) - Adon: Please! I have to try.
	DoCutScene("sounds/shaders/cut_intro06.ksnd", 3.0f, 1, "Please! I have to try", "", "");
	
	//cut_intro07 (11sec) - Normal Head: Be warned speaker. The course you choose now may well determine the future of your family's standing with this council. 
	DoCutScene("sounds/shaders/cut_intro07.ksnd", 6.0f, 4, "Be warned speaker", "The course you choose now may", "well determine the future of your");
	DoCutScene(5.0f, 4, "family's standing with this council", "", "");
	
	//cut_intro08 (10sec) - Adon: I can save him! If the council will not grant me this reprie, then I have no choose but to defy it's wishes.
	DoCutScene("sounds/shaders/cut_intro08.ksnd", 6.0f, 0, "I can save him!", "If the council will not grant", "me this reprie then I have");
	DoCutScene(4.0f, 0, "no choose but to defy it's wishes", "", "");
	
	Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_NO_INITIAL_FADEOUT);
	cutImage.RenderModel().SetTexture(0, 2);
	delay(3.0f);
	Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_NO_INITIAL_FADEOUT);
	
	//cut_intro09 (4sec) - Nice Head: She grows more insulent by the day.
	DoCutScene("sounds/shaders/cut_intro09.ksnd", 4.0f, 7, "She grows more insulent by the day", "", "");
	
	//cut_intro10 (2sec) - Angry Head: She's a danger to us all.
	DoCutScene("sounds/shaders/cut_intro10.ksnd", 2.0f, 7, "She's a danger to us all", "", "");
	delay(1.0f);
	Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_NO_INITIAL_FADEOUT);
	
	//cut_intro11 (6sec) - Evil: The Son's of Stone will fail. Chaos will commence.
	DoCutScene("sounds/shaders/cut_intro11.ksnd", 6.0f, 6, "The Son's of Stone will fail", "Chaos will commence", "");
	Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_NO_INITIAL_FADEOUT);
	cutImage.RenderModel().SetTexture(0, 7);
	delay(1.0f);
	
	Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_NO_INITIAL_FADEOUT);
	//cutImage.RenderModel().SetTexture(0, 8);
	cutImage.Scale() = kVec3(2.0f, 2.0f, 2.0f);
	Game.CallDelayedMapScript(5, instigator, 0.0f); //Move cutImage to the left slowly
	DoCutScene(4.0f, 8, "Time flows in reverse and", "Turok is given a second chance", "");
	delay(1.0f);
	Game.HaltMapScript(5);
	cutImage.Origin() = imageStartPos;
	cutImage.Scale() = kVec3(1.0f, 1.0f, 1.0f);
	cutImage.RenderModel().SetTexture(0, 7);
	
	PlayLoop.ChangeMap("levels/level04.map");
	return;
}
//------------------------------------------------------------------------------------------------------------------------
// Update Intro
//------------------------------------------------------------------------------------------------------------------------
$script 2 {
    if (Camera.UserInterrupted()) {
		PlayLoop.ChangeMap("levels/level04.map");
        return;
    }
    $restart;
}
//------------------------------------------------------------------------------------------------------------------------
// Start Game
//------------------------------------------------------------------------------------------------------------------------
$script 3 {
	World.TriggerActorsByTID(instigator, 890); //Hulk near tek arrows after long climb up
	
	//water before hulk
	World.ChangeAreaFlag(133, AAF_DAMAGE, true);
	World.ChangeAreaArg(133, 4, 5);
	World.ChangeAreaArg(133, 5, 1024);
	
	//water at climbing area before hulk
	World.ChangeAreaFlag(117, AAF_WATER, true);
	World.ChangeAreaWaterHeight(117, 2132.0f);
	
	SpawnMonkey(4, 8642, -657, 2201, -90);
	SpawnMonkey(11, -2666, 8063, 205, 180);
	SpawnMonkey(12, -1684, 11954, -460, 77);
	SpawnMonkey(13, -507, 3137, 972, 83);
	SpawnMonkey(14, -3580, 4285, 972, -90);
	
	//Falling Rocks on platform before checkpoint
	ActorFactory.Spawn("BP_FallingRocks", 9329, -4581, 861, 0, 0);
	ActorFactory.Spawn("BP_FallingRocks", 9000, -4585, 897, 0, 0);
	ActorFactory.Spawn("BP_FallingRocks", 8749, -4752, 936, 0, 0);

	SpawnActor("Emitter_BP_Fire_Tall2", 8545, -4359, 1024);
	SpawnActor("Emitter_BP_Fire_Tall2", 8926, -4163, 1105);
	SpawnActor("Emitter_BP_Fire_Tall2", 8560, -4115, 1024);
	SpawnActor("Emitter_BP_Fire_Tall2", 8707, -3998, 1024);
	SpawnActor("Emitter_BP_Fire_Tall2", 9125, -4069, 1105);

	SpawnActor("BP_SpikeFence", 9251, -4185, 1105, Math::Deg2Rad(-20));
	
	//down cave area whre beetles and key are
	SpawnActor("Emitter_BP_Fire_Tall2", -2599, 10349, 268);
	SpawnActor("Emitter_BP_Fire_Tall2", -2599, 10466, -15);
	
	//in cave area where key is
	ActorFactory.Spawn("BP_FallingRocks", -141, 10043, -388, 0, 0);
	
	kActor @fenceSpike = ActorFactory.Spawn("BP_SpikeFenceLarge", 386, 9743, -204, 0, 0);
	fenceSpike.Yaw() = Math::Deg2Rad(-90);
	
	kActor @fenceSpike2 = ActorFactory.Spawn("BP_SpikeFenceLarge", -2560, 8900, 0, 0, 0);
	fenceSpike2.Yaw() = Math::Deg2Rad(-65);

	kActor @shop = ActorFactory.Spawn("ShopPoint", -1546, 4920, 972, 0, 0); //Spawn Shop Portal
	shop.Yaw() = Math::Deg2Rad(60);
	
	//z beams at save point
	MapSpawnBeamTrapVert(kVec3(5914, -1132, 2828), kVec3(5914, -876, 2828), 1.0f, 1833);
	MapSpawnBeamTrapVert(kVec3(5832, -771, 2864), kVec3(5520, -771, 2864), 0.5f, 1842);

	SpawnActor("BPGruntRange_Poacher", 9252.597656f, -6605.100098f, 665.595398f, 2178, -0.000000f);
	SpawnActor("BPGruntGuardRange-Poacher", 9663.704102f, -4629.542969f, 778.271301f, 2204, -2.356194f);
	SpawnActor("BPGruntGuardGrenade", 8957.481445f, -4124.915527f, 1105.956909f, 2056, -1.570796f);
	SpawnActor("BPGruntRange_Poacher", 9417.575195f, -1862.670410f, 1894.453003f, 1672, 3.141593f);
	SpawnActor("BPGruntRange_Poacher", 9709.690430f, -1840.729736f, 1894.453003f, 1597, 3.141593f);
	SpawnActor("BPGruntMelee_PoacherKnife", 9134.893555f, -2815.545166f, 1894.453003f, 2030, 1.570796f);
	SpawnActor("BPGruntMelee_PoacherKnife", 9990.108398f, -2861.915527f, 1894.453003f, 2031, -1.570797f);
	SpawnActor("BPGruntRange_Poacher", 9122.333008f, -2259.765381f, 1894.453003f, 2030, 2.356194f);
	SpawnActor("BPGruntRange_Poacher", 9984.073242f, -2280.433838f, 1894.453003f, 1644, -2.748894f);
	SpawnActor("BPRaptor_Common", 8391.358398f, -696.006042f, 2201.601563f, 1657, 2.748893f);
	SpawnActor("BPRaptor_Common", 8703.031250f, -1305.926025f, 2201.601563f, 1796, 0.000000f);
	
	//Cave area with beetles
	SpawnActor("BPGruntGuardRange-Poacher", -2885.052490f, 8757.843750f, 205.810928f, 471, 0.785398f);
	SpawnActor("BPGruntGuardGrenade", -1974.185303f, 9189.262695f, 153.603912f, 488, 1.570796f);
	SpawnActor("BPGruntGuardGrenade", -1971.250977f, 9308.398438f, 153.603928f, 488, 1.570796f);
	SpawnActor("BPGruntGuardGrenade", -1961.605225f, 9504.372070f, 153.603928f, 391, 1.570796f);
	SpawnActor("BPPurlin_MeleeCommon", -2507.695313f, 10122.507813f, 460.810974f, 263, 2.356194f);
	SpawnActor("BPBeetle", -1924.764404f, 10862.266602f, -460.810059f, 177, -1.570796f);
	SpawnActor("BPBeetle", -1928.112427f, 10918.468750f, -460.810059f, 177, -1.570796f);
	SpawnActor("BPBeetle", -1931.541504f, 10976.032227f, -460.810059f, 178, -1.570796f);
	SpawnActor("BPBeetle", -1935.240845f, 11039.458984f, -460.810059f, 240, -1.570796f);
	SpawnActor("BPBeetle", -1975.388916f, 11006.901367f, -460.810059f, 240, -1.570796f);
	SpawnActor("BPBeetle", -1971.644531f, 10943.856445f, -460.810059f, 177, -1.570796f);
	SpawnActor("BPBeetle", -1971.023804f, 10887.366211f, -460.810059f, 177, -1.570796f);
	SpawnActor("BPBeetle", -1881.445679f, 11008.110352f, -460.810059f, 176, -1.570796f);
	SpawnActor("BPBeetle", -1870.177124f, 10945.730469f, -460.810059f, 175, -1.570796f);
	SpawnActor("BPBeetle", -1868.128906f, 10888.550781f, -460.810059f, 174, -1.570796f);
	SpawnActor("BP_SpikeFence", -1492.306152f, 11236.097656f, -460.810059f, 169, -1.178098f);
	SpawnActor("BP_SpikeFence", -1190.218872f, 11401.143555f, -460.810059f, 167, -0.785398f);
	SpawnActor("BPGruntGuardGrenade", 16.469559f, 10796.848633f, -409.599121f, 85, -1.178097f);
	SpawnActor("BPGruntGuardRange-Poacher", 549.080200f, 10166.222656f, -204.787918f, 73, -1.178097f);
	SpawnActor("BPGruntGuardGrenade", -320.938354f, 10017.465820f, -51.183525f, 382, 0.000000f);
	
	GameVariables.GetBool("BP.reachedEnd", reachedEnd);
	if (reachedEnd) {
		kActor @portalToEnd = ActorFactory.Spawn("BP_CommonPortalExit", portalEndPos.x, portalEndPos.y, portalEndPos.z, Math::Deg2Rad(0), 0); //Exit to hub
		portalToEnd.Scale() = kVec3(0.35f, 0.35f, 0.35f);
	}
	Game.CallDelayedMapScript(4, instigator, 0.0f); //Update Game
}
//------------------------------------------------------------------------------------------------------------------------
// Update Game
//------------------------------------------------------------------------------------------------------------------------
$script 4 {
	if (reachedEnd and InArea(portalEndPos, 60, 100) and Player.Actor().OnGround()) {
		PlayLoop.ChangeMap("levels/level25.map");
		return;
	}
	
	//when player enters area after hulk with tek arrows start the arena cinematic
	if (InAreaMinMax(kVec3(9466, -1439, 2190), kVec3(9694, -1320, 4000))) {
		CheckStartArenaCinematic(4, 1, kVec3(9560, -1459, 2201), kVec3(9560, -1259, 2201));
	}

	//just before portal hub
	if (InAreaMinMax(kVec3(-2880, 2181, 900), kVec3(-729, 2392, 3000))) {
		CheckStartArenaCinematic(4, 2, kVec3(-1836, 2086, 972), kVec3(-1836, 2286, 972));
	}
	
	UpdateGeneralGame();
	//ActorInWorldTestUpdate();
	$restart;
}
//------------------------------------------------------------------------------------------------------------------------
// Move cutImage to the left slowly
//------------------------------------------------------------------------------------------------------------------------
$script 5 {
	kVec3 pos = cutImage.Origin();
	pos.x -= 0.05f;
	cutImage.Origin() = pos;
	$restart;
}
//------------------------------------------------------------------------------------------------------------------------
void DoCutScene(const kStr &in sound, float time, int tex, const kStr &in topText, const kStr &in middleText, const kStr &in bottomText) {
	Game.PlaySound(sound);
	int iTime = int(60.0f * time - 10);
	Game.PrintLine(bottomText, 0, iTime); //bottom
	Game.PrintLine(middleText, 1, iTime); //middle
	Game.PrintLine(topText, 2, iTime); //top
	cutImage.RenderModel().SetTexture(0, tex);
	delay(time);
}
//------------------------------------------------------------------------------------------------------------------------
void DoCutScene(float time, int tex, const kStr &in topText, const kStr &in middleText, const kStr &in bottomText) {
	int iTime = int(60.0f * time - 10);
	Game.PrintLine(bottomText, 0, iTime); //bottom
	Game.PrintLine(middleText, 1, iTime); //middle
	Game.PrintLine(topText, 2, iTime); //top
	cutImage.RenderModel().SetTexture(0, tex);
	delay(time);
}
//------------------------------------------------------------------------------------------------------------------------
