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

bool hitButton = false;
kActor @blockingRock;
kActor @knife;
//------------------------------------------------------------------------------------------------------------------------
$script 0 {
	Game.PrintLine("you run out of air", 0, 360);
	Game.PrintLine("Find the dagger before", 1, 360);
	
	World.ChangeAreaFlag(84, AAF_WATER, true);
	World.ChangeAreaWaterHeight(84, 500.0f);
	World.ChangeAreaFlag(141, AAF_WATER, true);
	World.ChangeAreaWaterHeight(141, 500.0f);
	World.ChangeAreaFlag(142, AAF_WATER, true);
	World.ChangeAreaWaterHeight(142, 500.0f);
	
	SpawnActor("Emitter_WaterFall_Steam", -4591, -4443, 320, Math::Deg2Rad(0));
	SpawnActor("Emitter_Mountain_Lava_Smoke", -2747, -3206, 320, Math::Deg2Rad(0));
	
	//start of cave
	kActor @startRock = SpawnActor("BPDetail_CaveRock02", -5517, -3522, 307, Math::Deg2Rad(90));
	startRock.Scale() = kVec3(0.75f, 0.75f, 0.75f);
	
	//bottom right (button)
	SpawnActor("BPDetail-TechPlate", -4593, -4068, 308);
	
	@blockingRock = SpawnActor("BPDetail_CaveRock02", -2320, -3217, 307, Math::Deg2Rad(90));
	blockingRock.Scale() = kVec3(1.0f, 1.0f, 1.0f);
	
	//top left (goal)
	@knife = SpawnActor("BPDetail_Knife", -1886, -2559, 223, Math::Deg2Rad(0));
	knife.Pitch() = Math::Deg2Rad(200);
	knife.Roll() = Math::Deg2Rad(20);
	knife.AnimState().Stop();
	
	//end of cave
	kActor @endRock = SpawnActor("BPDetail_CaveRock02", -929, -3518, 307, Math::Deg2Rad(90));
	endRock.Scale() = kVec3(1.0f, 1.0f, 1.0f);
	
	SpawnActor("BPFish", -1481.941895f, -2815.601807f, 383.849182f, 1195, -2.611292f);
	SpawnActor("BPFish", -1556.906616f, -2992.174316f, 362.328094f, 1195, 2.265089f);
	SpawnActor("BPFish", -1612.773315f, -3210.416016f, 431.863800f, 1193, 2.974460f);
	SpawnActor("BPFish", -1600.462769f, -2603.611328f, 367.849182f, 1207, 1.515504f);
	SpawnActor("BPFish", -1915.614258f, -3241.377930f, 354.024933f, 1192, -2.483743f);
	SpawnActor("BPFish", -2111.704590f, -3186.755859f, 398.253571f, 1192, 0.011310f);
	SpawnActor("BPFish", -2691.988281f, -3277.802734f, 366.875214f, 1152, -0.723823f);
	SpawnActor("BPFish", -2722.581055f, -3684.628174f, 318.598419f, 1180, -1.094531f);
	SpawnActor("BPFish", -2709.567383f, -3165.396484f, 397.100861f, 1180, 1.541265f);
	SpawnActor("BPFish", -2771.010254f, -3610.065918f, 356.891907f, 1180, 2.072823f);
	SpawnActor("BPFish", -2790.147461f, -3749.887451f, 421.830231f, 1179, 3.057398f);
	SpawnActor("BPFish", -2738.996582f, -4119.477051f, 386.508331f, 1180, -1.489115f);
	SpawnActor("BPFish", -2833.573486f, -4468.183105f, 375.120026f, 1161, -1.620434f);
	SpawnActor("BPFish", -3646.364014f, -4353.509766f, 380.657440f, 1307, 1.898150f);
	SpawnActor("BPFish", -3658.070801f, -3708.997314f, 408.668060f, 1227, 1.731646f);
	SpawnActor("BPFish", -3680.859375f, -3232.427490f, 342.811340f, 1274, 3.087557f);
	SpawnActor("BPFish", -3836.239502f, -2942.107422f, 422.089508f, 1273, -3.125885f);
	SpawnActor("BPFish", -4255.248535f, -2910.372070f, 390.035980f, 1259, 0.429770f);
	SpawnActor("BPFish", -4790.972656f, -2934.997314f, 330.472717f, 1258, -0.827496f);
	SpawnActor("BPFish", -4881.694824f, -3254.898438f, 413.086182f, 1241, 2.380071f);
	SpawnActor("BPFish", -4971.647461f, -3494.212891f, 376.192230f, 1241, 0.407779f);
	SpawnActor("BPFish", -4780.895508f, -2872.838379f, 422.121857f, 1311, -2.963151f);
	SpawnActor("BPFish", -3233.420654f, -4454.320801f, 356.778168f, 1307, 0.762150f);
	SpawnActor("BPFish", -4860.931152f, -3536.287109f, 316.641174f, 1244, -2.853195f);
	
	Game.CallDelayedMapScript(1, instigator, 0); //Update Game
}
//------------------------------------------------------------------------------------------------------------------------
// Update Game
//------------------------------------------------------------------------------------------------------------------------
$script 1 {
	if (!hitButton) {
		BlockAreaMinMax(kVec3(-2429, -3291, 300), kVec3(-2253, -3142, 1307), kVec3(-1, 0, 0));
		if (InArea(kVec3(-4593, -4068, 306), 50, 100)) {
			hitButton = true;
			blockingRock.Remove();
			Game.PlaySound("sounds/shaders/explosion_2.ksnd");
			PlayerSetupShake(2.5f, 2.0f);
		}
	} else {
		if (PlayLoop.Ticks() % 2 == 0) {
			Game.SpawnFx("fx/generic_230.kfx", kVec3(-1900, -2599, 340), -1);
		}
		if (InArea(kVec3(-1936, -2599, 300), 50, 100)) {
			Game.PlaySound("sounds/shaders/health_pickup_1.ksnd");
			knife.Remove();
			Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_NO_INITIAL_FADEOUT);
			Camera.SetLookAtActor(Player.Actor().CastToActor());
			Camera.fov = 74.0f;
			Player.Actor().ModelVariation() = TV_KNIFE;
			Player.Actor().AnimState().Set(anim_player_acquire_key, 8.0f, ANF_LOOP);
			Player.Actor().Flags() &= ~AF_NODRAW;
			Camera.SetFinalView(0);
			Camera.SetRotationTrack(0,
									Math::Deg2Rad(0.0f),  // start angle
									Math::Deg2Rad(25.0f),   // dest angle
									10*GAME_SCALE,           // start distance
									6*GAME_SCALE,           // dest distance
									7*GAME_SCALE,           // start height
									12*GAME_SCALE,           // dest height
									4*GAME_SCALE,           // start look at height
									7*GAME_SCALE);          // dest look at height
			Camera.AutoPlayRotationTrack(0, 4.0f, CMLT_COSINE);
			delay(2.0f);
			Game.PlaySound("sounds/shaders/ItemCatch.ksnd");
			Game.PrintLine("The Power is yours", 0, 120);
			delay(2.0f);
			SetUpgradeKnife(true);
			ExitChallengeWarp();
			return;
		}
	}
	BlockAreaMinMax(kVec3(-5582, -3633, 300), kVec3(-5424, -3401, 1307), kVec3(1, 0, 0)); //start cave blocker
	BlockAreaMinMax(kVec3(-1045, -3637, 300), kVec3(-825, -3417, 1307), kVec3(-1, 0, 0)); //end cave blocker
	
	UpdateGeneralGame();
	$restart;
}
//------------------------------------------------------------------------------------------------------------------------
// On Player Death
//------------------------------------------------------------------------------------------------------------------------
$script 2 {
	PlayLoop.HandlePlayerDeath();
	ExitChallengeWarp();
}
//------------------------------------------------------------------------------------------------------------------------
