//
// 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 Level03.map
//

#include "scripts/common.txt"

kActor @g_pTRex = null;
kActor @g_pTurokBody = null;
kActor @g_pTurokFeathers = null;

float start_x = 0;
float start_y = 0;
float start_z = 0;

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

$script 0
{
    int trexState = 0;
    GameVariables.GetInt("trexState", trexState);
    World.TriggerActorsByTID(instigator, 10);
    @g_pTRex = World.GetActorByTID(400);
    @g_pTurokBody = World.GetActorByTID(1);
    @g_pTurokFeathers = World.GetActorByTID(2);
    
    g_pTurokBody.Flags() |= AF_HIDDEN;
    g_pTurokFeathers.Flags() |= AF_HIDDEN;
    
    start_x = instigator.Origin().x;
    start_y = instigator.Origin().y;
    start_z = instigator.FloorHeight();
    
    Game.CallDelayedMapScript(1, instigator, 0);
    
    delay(1.5f);
    g_pTRex.AnimState().Set(anim_trexRoar, 6.0f, ANF_ROOTMOTION);
    if(trexState == 0) // tag for boss bar now only if in idle state
    {
        PlayLoop.TagActorForBossBar(g_pTRex);
    }
}

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

$script 1
{
    if(g_pTRex.Health() <= 0)
    {
        Camera.StartCinematic();
        Camera.SetFinalView(0);
        PlayLoop.RemoveBossActor();
        
        Camera.fov = 74.0f;
        
        Camera.SetLookAtActor(g_pTRex);
        
        Camera.SetRotationTrack(0,
                                Math::Deg2Rad(0.0f),
                                Math::Deg2Rad(360.0f),
                                10*GAME_SCALE, 40*GAME_SCALE,
                                50*GAME_SCALE, 10*GAME_SCALE,
                                20*GAME_SCALE,  8*GAME_SCALE);
                                       
        Camera.AutoPlayRotationTrack(0, 10.0f, CMLT_COSINE);
        
        Game.CallDelayedMapScript(3, instigator, 10.0f);
        
        delay(0.35f);
        g_pTRex.AnimState().Set(anim_aiDeathStand, 8.0f, ANF_ROOTMOTION);
        
        return;
    }
    
    $restart;
}

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

T-Rex snacking on Turok
==============================================================
*/

$script 2
{
    Camera.StartCinematic();
    Camera.SetFinalView(0);
    PlayLoop.RemoveBossActor();
    
    Camera.fov = 74.0f;
    
    g_pTRex.StopLoopingSounds();
    
    Game.CallDelayedMapScript(4, instigator, 0);
    Game.CallDelayedMapScript(7, instigator, 0);
}

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

$script 3
{
    Player.Actor().SetPosition(kVec3(start_x, start_y, start_z));
    
    Player.Actor().Yaw() = 0;
    Player.Actor().Pitch() = 0;
    Player.Actor().RecoilPitch() = 0;
    
    Camera.ClearLookAtActor();
    
    // warp to finale after victory
    Player.Victory(18998, 25);
}

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

T-Rex snacking on Turok logic
==============================================================
*/

$script 4
{
    if(Camera.CinematicState() >= 2)
    {
        kVec3 vPos;
        kPuppet @actor = Player.Actor();
        
        actor.Flags() &= ~AF_NODRAW;
        
        g_pTRex.AnimState().Set(anim_trexGulp, 4.0f, 0);
        actor.AnimState().Set(anim_turokIsNom, 6.0f, 0);
        g_pTurokBody.AnimState().Set(anim_trapActivate, 4.0f, 0);
        g_pTurokFeathers.AnimState().Set(anim_trapActivate, 4.0f, 0);
        
        // move g_pTRex into position
        vPos.x = 63*GAME_SCALE;
        vPos.y = -29*GAME_SCALE;
        vPos.z = 0;
        
        g_pTRex.SetSector(g_pTRex.GetSectorIndexAtLocation(vPos));
        g_pTRex.Origin() = vPos;
        g_pTRex.Origin().z = g_pTRex.FloorHeight();
        g_pTRex.Yaw() = Math::Deg2Rad(90);
        g_pTRex.SetHeadTrackTarget(null);
        
        // move player into position
        vPos.x = 90*GAME_SCALE;
        vPos.y = -29*GAME_SCALE;
        vPos.z = 0;
        
        actor.SetSector(actor.GetSectorIndexAtLocation(vPos));
        actor.Origin() = vPos;
        actor.Origin().z = actor.FloorHeight();
        actor.Yaw() = Math::Deg2Rad(90);
        actor.Pitch() = 0;
        actor.RecoilPitch() = 0;
        
        // setup body
        g_pTurokBody.Yaw() = g_pTRex.Yaw();
        g_pTurokBody.Scale() = g_pTRex.Scale();
        
        // setup feathers
        g_pTurokFeathers.Yaw() = g_pTRex.Yaw();
        g_pTurokFeathers.Scale() = g_pTRex.Scale();
        
        Camera.SetFinalView(0);
        
        // setup camera position
        Camera.SetEyeView(kVec3(98*GAME_SCALE, -29*GAME_SCALE, actor.Origin().z + GAME_SCALE));
        Camera.SetFocusView(kVec3(80*GAME_SCALE, -29*GAME_SCALE, actor.Origin().z + (10*GAME_SCALE)));
        
        Game.CallDelayedMapScript(5, instigator, 1.0f);
        Game.CallDelayedMapScript(6, instigator, 5.3f);
        return;
    }
    
    $restart;
}

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

$script 5
{
    Player.Actor().Flags() |= AF_NODRAW;
    g_pTurokBody.Flags() &= ~AF_HIDDEN;
    
    Game.CallDelayedMapScript(8, instigator, 0);
}

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

$script 6
{
    Camera.SetEyeView(kVec3(74*GAME_SCALE, -48*GAME_SCALE, g_pTRex.Origin().z + (21*GAME_SCALE)));
    Camera.SetFocusView(kVec3(77*GAME_SCALE, -29*GAME_SCALE, g_pTRex.Origin().z + (21*GAME_SCALE)));
    
    delay(4.0f);
    
    g_pTurokFeathers.Flags() &= ~AF_HIDDEN;
    
    Camera.SetEyeView(kVec3(75*GAME_SCALE, -32*GAME_SCALE, g_pTRex.Origin().z + (21*GAME_SCALE)));
    Camera.SetFocusView(kVec3(75*GAME_SCALE, -29*GAME_SCALE, g_pTRex.Origin().z + (21*GAME_SCALE)));
    
    delay(2.2f);
    
    Game.HaltMapScript(7);
    PlayLoop.HandlePlayerDeath();
}

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

$script 7
{
    if(Camera.UserInterrupted())
    {
        PlayLoop.HandlePlayerDeath();
        return;
    }
    
    $restart;
}

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

$script 8
{
    kVec3 vPos = g_pTRex.Origin();
    
    vPos.x += (21*GAME_SCALE);
    vPos.y += (12*GAME_SCALE);
    g_pTurokBody.SetPosition(vPos);
    
    if(g_pTurokBody.AnimState().TrackTime() >= 0.75f)
    {
        g_pTurokBody.Flags() |= AF_HIDDEN;
    }
    
    vPos = g_pTRex.Origin();
    
    vPos.x += (26*GAME_SCALE);
    vPos.y += ( 6*GAME_SCALE);
    vPos.z += (16*GAME_SCALE);
    g_pTurokFeathers.SetPosition(vPos);
    
    $restart;
}
