//
// Copyright(C) 2014-2015 Samuel Villarreal
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// DESCRIPTION:
//      Level Script For Level01.map
//

#include "scripts/common.txt"

/*
==============================================================
Script 0
==============================================================
*/

$script 0
{
	Player.Actor().Flags() &= ~AF_NODRAW;
	Player.Actor().AnimState().Set(anim_player_run, 5.5f, ANF_LOOP|ANF_ROOTMOTION);
	Player.Actor().Yaw() += Math::Deg2Rad(0.5f);

	// running down hall, focus on turok
	Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_NO_INITIAL_FADEOUT);
	Camera.SetLookAtActor(Player.Actor().CastToActor());

	Camera.fov = 65.0f;

	Camera.origin = Player.Actor().Origin() + kVec3(8*GAME_SCALE, -40*GAME_SCALE, 6*GAME_SCALE);
	Camera.lookZOffset = 1.85f*GAME_SCALE;

	Game.CallDelayedMapScript(10, instigator, 0);
	// Smoke39 - allow cutscene to be skipped
	Game.CallDelayedMapScript( 11, instigator, 0 );

	delay(1.333f);

	// running down hall, top view down
	Camera.SetFinalView(0);
	Camera.SetEyeView(Player.Actor().Origin() + kVec3(-8*GAME_SCALE, -40*GAME_SCALE, 10*GAME_SCALE));
	Game.CallDelayedMapScript(1, instigator, 0);

	delay(1.333f);

	// running down hall, view from behind
	Camera.ClearLookAtActor();
	Game.HaltMapScript(1);
	Game.CallDelayedMapScript(2, instigator, 0);

	delay(1.77733f);

	// running down hall, toward camera
	Game.HaltMapScript(2);
	Game.CallDelayedMapScript(3, instigator, 0);

	delay(2.667f);

	// running down hall, side view
	Game.HaltMapScript(3);
	Game.CallDelayedMapScript(4, instigator, 0);

	delay(2.222f);

	// running down hall, front view
	Game.HaltMapScript(4);
	Game.CallDelayedMapScript(5, instigator, 0);

	// starting to leap
	delay(1.77733f);
	Player.Actor().AnimState().Blend(anim_player_exit, 6.0f, 8.0f, ANF_ROOTMOTION);

	delay(0.46f);
	Game.HaltMapScript(5);

	// move position over edge of hole
	Camera.SetEyeView(kVec3(-575*GAME_SCALE, 80*GAME_SCALE, 15*GAME_SCALE));
	Camera.SetFocusView(kVec3(-580*GAME_SCALE, 100*GAME_SCALE, 18*GAME_SCALE));

	Game.CallDelayedMapScript(6, instigator, 0);
	Game.CallDelayedMapScript(9, instigator, 0);

	delay(2.0f);
	Game.HaltMapScript(6);

	delay(0.5f);

	// leaping out
	Player.Actor().SetPosition(kVec3(-580*GAME_SCALE, -40*GAME_SCALE, 0));
	kVec3 vPos = Player.Actor().Origin();
	vPos.z = Player.Actor().FloorHeight() + (20*GAME_SCALE);

	Camera.SetEyeView(vPos);

	// fall in water
	Player.Actor().SetPosition(kVec3(-580*GAME_SCALE, 0.0f, -90*GAME_SCALE));
	Player.Actor().AnimState().Set(anim_player_jump, 4.0f, ANF_LOOP);

	Camera.SetLookAtActor(Player.Actor().CastToActor());

	Game.CallDelayedMapScript(7, instigator, 0);

	delay(2.667f);
	Game.HaltMapScript(8);

	Player.Actor().Origin().z = -126*GAME_SCALE;
	Player.Actor().AnimState().Set(anim_player_surface, 4.0f, 0);

	Camera.SetFinalView(0);
	Camera.SetRotationTrack(0,
	                        Math::Deg2Rad(-120.0f),
	                        Math::Deg2Rad(0.0f),
	                        6*GAME_SCALE,
	                        6*GAME_SCALE,
	                        10*GAME_SCALE,
	                        7.5f*GAME_SCALE,
	                        7.5f*GAME_SCALE,
	                        11*GAME_SCALE);

	Camera.AutoPlayRotationTrack(0, 4.0f, CMLT_COSINE);

	delay(10.0f);
	Game.HaltMapScript(10);

	PlayLoop.ChangeMap("levels/level02.map");
}

/*
==============================================================
Script 1
==============================================================
*/

$script 1
{
	kVec3 look = Player.Actor().Origin();
	look.z += 5*GAME_SCALE;

	Camera.SetFocusView(look);
	$restart;
}

/*
==============================================================
Script 2
==============================================================
*/

$script 2
{
	kVec3 look = Player.Actor().Origin();
	look.z += 5*GAME_SCALE;

	Camera.SetFocusView(look);
	Camera.SetEyeView(Player.Actor().Origin() + kVec3(0, -10*GAME_SCALE, 8*GAME_SCALE));
	$restart;
}

/*
==============================================================
Script 3
==============================================================
*/

$script 3
{
	kVec3 look = Player.Actor().Origin();
	look.z += 6*GAME_SCALE;

	Camera.SetFocusView(look);
	Camera.SetEyeView(Player.Actor().Origin() + kVec3(0, -10*GAME_SCALE, 6*GAME_SCALE));
	$restart;
}

/*
==============================================================
Script 4
==============================================================
*/

$script 4
{
	kVec3 look = Player.Actor().Origin();
	look.z += 6*GAME_SCALE;

	Camera.SetFocusView(look);
	Camera.SetEyeView(Player.Actor().Origin() + kVec3(-8*GAME_SCALE, 0, 8*GAME_SCALE));
	$restart;
}

/*
==============================================================
Script 5
==============================================================
*/

$script 5
{
	kVec3 look = Player.Actor().Origin();
	look.z += 6*GAME_SCALE;

	Camera.SetFocusView(look);
	Camera.SetEyeView(Player.Actor().Origin() + kVec3(0, -20*GAME_SCALE, 6*GAME_SCALE));
	$restart;
}

/*
==============================================================
Script 6
==============================================================
*/

$script 6
{
	Player.Actor().Origin() += kVec3(0, -16.0f, 0);
	$restart;
}

/*
==============================================================
Script 7
==============================================================
*/

$script 7
{
	kVec3 look = Player.Actor().Origin();
	look.z += 6*GAME_SCALE;

	Camera.SetFocusView(look);

	Player.Actor().Origin().z -= 8.0f;

	if(Player.Actor().Origin().z < -120*GAME_SCALE)
	{
		Player.Actor().SpawnFx("fx/generic_96.kfx", kVec3(0, 0, 30*GAME_SCALE));
		Player.Actor().AnimState().Blend(anim_aiSwim8, 32.0f, 16.0f, 0);

		Game.CallDelayedMapScript(8, instigator, 0);
		return;
	}

	$restart;
}

/*
==============================================================
Script 8
==============================================================
*/

$script 8
{
	Player.Actor().Origin().z -= 2.0f;

	if(Player.Actor().Origin().z < -136*GAME_SCALE)
	{
		return;
	}

	$restart;
}

/*
==============================================================
Script 9
==============================================================
*/

$script 9
{
	const float shakeScale = Game.ShakeIntensity();
	Camera.Tremor().x = Math::RandRange(Math::Deg2Rad(-0.5f), Math::Deg2Rad(0.5f)) * shakeScale;
	Camera.Tremor().y = Math::RandRange(Math::Deg2Rad(-0.5f), Math::Deg2Rad(0.5f)) * shakeScale;
	Camera.Tremor().z = Math::RandRange(Math::Deg2Rad(-0.5f), Math::Deg2Rad(0.5f)) * shakeScale;

	delay(0.01f);
	$restart;
}

/*
==============================================================
Script 10
==============================================================
*/

$script 10
{
	float x, y, z;

	for(int i = 0; i < 4; ++i)
	{
		x = Math::RandRange(-612*GAME_SCALE, -553*GAME_SCALE);
		y = Math::RandRange(94*GAME_SCALE, 105*GAME_SCALE);
		z = Math::RandRange(-30*GAME_SCALE, 60*GAME_SCALE);

		Game.SpawnFx("fx/generic_235.kfx", kVec3(x, y, z), 0);
	}

	Game.NextSoundVolumeScale(0.25f);
	Game.NextSoundPitchScale(Math::RandRange(0.9f, 1.1f));
	if((Math::Rand() % -2) != 0)
	{
		Player.Actor().PlaySound("sounds/shaders/generic_218.ksnd");
	}
	else
	{
		Player.Actor().PlaySound("sounds/shaders/explosion_2.ksnd");
	}

	delay(0.125f);
	$restart;
}

// Smoke39 - new script for skipping cutscene
$script 11
{
	if ( Camera.UserInterrupted() )
	{
		PlayLoop.ChangeMap( "levels/level02.map" );
		// make sure that if we skip near the end, the normal warp doesn't restart this one
		Game.HaltMapScript( 0 );
	}
	else
		$restart;
}
