#include "scripts/common.txt"
#include "scripts/BP_common.txt"
#include "scripts/BP_MapCommon.txt"

kActor @trex = null;

$script 0
{
	CheckNotStarted();
	
    @trex = World.GetActorByTID(400);
	
	trex.AnimState().Blend(anim_aiRunning, 2.4f, 4.0f, ANF_LOOP|ANF_ROOTMOTION);
	
	Game.PlaySound("sounds/shaders/trex_scream.ksnd");
	
    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);
	Player.Actor().ModelVariation() = TV_BOW;
    
    // 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(11, instigator, 0);
    Game.CallDelayedMapScript(12, instigator, 0);
    Game.CallDelayedMapScript(10, instigator, 0);
    
	delay(0.333f);
	CheckNotStarted();
    delay(1.0f);
    
    // 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);
    
	Game.PlaySound("sounds/shaders/trex_scream.ksnd");
    Game.HaltMapScript(12);
    Game.CallDelayedMapScript(13, instigator, 0);

    // 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);
	Game.PlaySound("sounds/shaders/trex_scream.ksnd");
    
    // 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);
    
	//trex.AnimState().Blend(anim_trexRoar, 6.0f, 4.0f, ANF_ROOTMOTION);
	//trex.AnimState().Set(anim_aiDeathStand, 8.0f, ANF_ROOTMOTION);
	//Game.PlaySound("sounds/shaders/trex_scream.ksnd");
    //Game.HaltMapScript(13);
	
    // 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(0.5f);
	trex.AnimState().Set(anim_aiDeathStand, 8.0f, ANF_ROOTMOTION);
    Game.HaltMapScript(13);
    delay(1.5f);

    Game.HaltMapScript(6);
    
    delay(0.5f);
	
	//trex.AnimState().Set(anim_aiDeathStand, 8.0f, ANF_ROOTMOTION);
	//trex.Flags() |= AF_NODRAW;
	trex.SetSector(0);
	trex.Yaw() = 3.141593f;
	trex.Origin() = kVec3(-5950, 1220, -295);//153.7795f);
    Game.CallDelayedMapScript(14, instigator, 0);

    
    // 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(8.0f);
    Game.HaltMapScript(10);
    
    PlayLoop.ChangeMap("levels/02.map");
}

$script 11
{
    if(Camera.UserInterrupted())
    {
		Game.HaltMapScript(0);
		PlayLoop.ChangeMap("levels/02.map");
        return;
    }
    
    $restart;
}

$script 12
{
	if (PlayLoop.Ticks() % 4 == 0)
	{

		trex.SpawnFx("fx/blood.kfx", kVec3(0, -450, 320));
		//trex.SpawnFx("fx/gush_blood.kfx", kVec3(0, -450, 320));
		//trex.SpawnFx("fx/spurt_blood.kfx", kVec3(0, -450, 320));
		
	}
    $restart;
}

$script 13
{
	if (PlayLoop.Ticks() % 4 == 0)
	{

		//trex.SpawnFx("fx/blood.kfx", kVec3(0, -450, 320));
		//trex.SpawnFx("fx/gush_blood.kfx", kVec3(0, -450, 320));
		trex.SpawnFx("fx/spurt_blood.kfx", kVec3(0, -450, 320));
		
	}
    $restart;
}

$script 14
{
	if (PlayLoop.Ticks() % 4 == 0)
	{

		//trex.SpawnFx("fx/blood.kfx", kVec3(0, -450, 320));
		//trex.SpawnFx("fx/gush_blood.kfx", kVec3(0, -450, 320));
		trex.SpawnFx("fx/spurt_blood.kfx", kVec3(0, -450, 0));
		
	}
    $restart;
}

/*
==============================================================
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
{
    Camera.Tremor().x = Math::RandRange(Math::Deg2Rad(-0.5f), Math::Deg2Rad(0.5f));
    Camera.Tremor().y = Math::RandRange(Math::Deg2Rad(-0.5f), Math::Deg2Rad(0.5f));
    Camera.Tremor().z = Math::RandRange(Math::Deg2Rad(-0.5f), Math::Deg2Rad(0.5f));
    
    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);
    }
    
    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;
}
