//
// 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:
//      LongHunter Boss Logic
//

#include "scripts/common.txt"

enum BP_longHunterState
{
    BP_LHS_IDLE        = 0,
    BP_LHS_ATTACK,
    BP_LHS_DEAD,
}

enum BP_longHunterGlobalState
{
    BP_LHGS_NEW             = 0,
    BP_LHGS_INCOMBAT,
    BP_LHGS_DEAD
}

//-----------------------------------------------------------------------------
//
// Stage Info
//
//-----------------------------------------------------------------------------

enum BP_longHunterStageInfo
{
    BP_LHSI_HEALTHPERCNT   = 0,
    BP_LHSI_SPEED,
    BP_LHSI_EVADETIME,
    BP_LHSI_EVADESKIPS,
    BP_LHSI_NOEVADETIME,
    BP_LHSI_FLINCHPERCNT,
    BP_LHSI_RUNTIME,
    BP_LHSI_GUNFIRECOUNT
}

// health %, speed, evade time, evade skips, no evade time, flinch %, run time before attack, gun fire total
const array<float> g_BP_longhuntStage_1 = { 75.0f, 1.0f,  1.0f, 4.0f, 4.0f, 20.0f, 2.0f,  2.0f };
const array<float> g_BP_longhuntStage_2 = { 50.0f, 0.95f, 2.0f, 3.0f, 5.0f, 12.0f, 1.75f, 2.0f };
const array<float> g_BP_longhuntStage_3 = { 25.0f, 0.9f,  2.0f, 2.0f, 6.0f, 6.0f,  1.5f,  3.0f };
const array<float> g_BP_longhuntStage_4 = { 0.0f,  0.85f, 3.0f, 1.0f, 7.0f, 4.0f,  1.0f,  3.0f };

const array<const array<float>@> g_BP_longhuntStages =
{
    @g_BP_longhuntStage_1,
    @g_BP_longhuntStage_2,
    @g_BP_longhuntStage_3,
    @g_BP_longhuntStage_4
};

//-----------------------------------------------------------------------------
//
// Anim Weights
//
//-----------------------------------------------------------------------------

const array<int> g_BP_longhuntMeleeAnims = { anim_aiMelee1, anim_aiMelee3, anim_longHunterTaunt };
const array<int> g_BP_longhuntMeleeWeights = { 50, 50, 5 };
const array<float> g_BP_longhuntMeleeSpeed = { 2.5, 2.0f, 3.0f };

const array<int> g_BP_longhuntRangeAnims = { anim_aiMelee4, anim_aiMelee5, anim_aiMelee6 };
const array<int> g_BP_longhuntRangeWeights = { 50, 15, 35 };
const array<float> g_BP_longhuntRangeSpeed = { 4.0f, 1.14f, 4.0f };

const array<int> g_BP_longhuntWalkAnims = { anim_aiMelee2, anim_aiMelee4 };
const array<int> g_BP_longhuntWalkWeights = { 50, 25 };
const array<float> g_BP_longhuntWalkSpeed = { 2.667f, 4.0f };

//-----------------------------------------------------------------------------
//
// Hunter
//
//-----------------------------------------------------------------------------

/*
==============================================================
TurokLongHunter
==============================================================
*/

final class BP_TurokLongHunter : TurokEnemy
{
    int m_state;
    int m_stage;
    int m_rangeAttackLoops;
    int m_standingTauntTime;
    float m_runTime;
    float m_goalDistSq;
    float m_goalDist;
    float m_evadeTime;
    int m_evadeActive;
    int m_evadeCount;
    float m_startHealth;
    float m_animSpeed;
    kVec3 m_vGoalOrigin;
    kAngle m_desiredYaw;
    kAngle m_lookAtYaw;
    kAngle m_randomYaw;
	int m_bossID;
	bool m_bTargetDead;
    const array<float> @m_pStageMode;
    
    BP_TurokLongHunter(kActor @actor)
    {
        m_goalDistSq = 0;
        m_goalDist = 0;
        m_desiredYaw = 0;
        m_lookAtYaw = 0;
        m_randomYaw = 0;
        m_runTime = 0;
        m_evadeTime = 0;
        m_evadeActive = 0;
        m_animSpeed = 1;
        m_rangeAttackLoops = 0;
        m_stage = 0;
        m_startHealth = 1;
        m_evadeCount = 0;
        m_standingTauntTime = 0;
        @m_pStageMode = g_BP_longhuntStages[0];
        m_state = BP_LHS_IDLE;
		m_bTargetDead = false;
        super(actor);
    }
	
    /*
    ==============================================================
    SwooshTrail2
    ==============================================================
    */
    
    void SwooshTrail2(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        self.RenderModel().AddTrailEffect("Trail_LongFootEvade", 3);
        self.RenderModel().AddTrailEffect("Trail_LongFootEvade", 8);
    }
    
    /*
    ==============================================================
    SwooshTrail3
    ==============================================================
    */
    
    void SwooshTrail3(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        self.RenderModel().AddTrailEffect("Trail_LongHand", 12);
        self.RenderModel().AddTrailEffect("Trail_LongHand", 16);
    }
    
    /*
    ==============================================================
    SwooshRightFoot
    ==============================================================
    */
    
    void SwooshRightFoot(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        self.RenderModel().AddTrailEffect("Trail_LongFoot", 8);
    }
    
    /*
    ==============================================================
    SwooshRightHand
    ==============================================================
    */
    
    void SwooshRightHand(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        self.RenderModel().AddTrailEffect("Trail_LongHand", 18);
    }
    
    /*
    ==============================================================
    PickAnimation
    ==============================================================
    */
    
    void PickAnimation(const array<int> &anims, const array<int> &weights, const array<float> &speeds)
    {
        if(anims.length() != weights.length())
        {
            return;
        }
        
        int randomSum = 0;
        uint i;
        int count = 0;
        array<int> rndArray;
        array<int> animArray;
        array<float> animSpeed;
        
        for(i = 0; i < weights.length(); i++)
        {
            if(self.AnimState().CheckAnimID(anims[i]))
            {
                rndArray.insertLast(randomSum);
                animArray.insertLast(anims[i]);
                animSpeed.insertLast(speeds[i]);
                randomSum += weights[i];
                count++;
            }
        }
        
        if(randomSum == 0 || count == 0)
        {
            return;
        }
        
        int r = Math::RandMax(randomSum);
        
        for(i = count-1; i >= 0; i--)
        {
            if(rndArray[i] <= r)
            {
                break;
            }
        }
        
        BlendAnimation(animArray[i], animSpeed[i], 8.0f, false);
    }
    
    /*
    ==============================================================
    BlendAnimation
    ==============================================================
    */
    
    void BlendAnimation(const int anim, const float speed, const float blend, const bool bStoppedOnly = true)
    {
        if(bStoppedOnly && !self.AnimState().Stopped())
        {
            return;
        }
        else if(!bStoppedOnly && self.AnimState().PlayingID() == anim)
        {
            return;
        }
        
        if(self.AnimState().PlayingID() == anim)
        {
            self.AnimState().Set(anim, speed, ANF_ROOTMOTION);
            return;
        }
        
        self.AnimState().Blend(anim, speed*m_animSpeed, blend*m_animSpeed, ANF_ROOTMOTION);
    }
    
    /*
    ==============================================================
    IsRunning
    ==============================================================
    */
    
    bool IsRunning(void)
    {
        switch(self.AnimState().PlayingID())
        {
        case anim_aiRunning:
        case anim_aiTurn_L_Run:
        case anim_aiTurn_R_Run:
        case anim_aiTurn_B_Run:
            return true;
        }
        
        return false;
    }
    
    /*
    ==============================================================
    IsWalking
    ==============================================================
    */
    
    bool IsWalking(void)
    {
        switch(self.AnimState().PlayingID())
        {
        case anim_aiWalking:
        case anim_aiTurn_L_Walk:
        case anim_aiTurn_R_Walk:
        case anim_aiTurn_B_Walk:
            return true;
        }
        
        return false;
    }
    
    /*
    ==============================================================
    CheckForEvade
    ==============================================================
    */
    
    void CheckForEvade(void)
    {
        bool bCanEvade = false;
        
        m_evadeTime -= GAME_DELTA_TIME;
        
        if(m_evadeTime < 0)
        {
            // swap evade states
            m_evadeActive ^= 1;
            
            // reset evade/non-evade time
            if(m_evadeActive == 1)
            {
                m_evadeTime = m_pStageMode[LHSI_EVADETIME];
            }
            else
            {
                m_evadeTime = m_pStageMode[LHSI_NOEVADETIME];
            }
        }
        
        // can we evade in this mode?
        switch(self.AnimState().PlayingID())
        {
        case anim_longHunterTaunt:
        case anim_aiTurn_L_Run:
        case anim_aiTurn_R_Run:
        case anim_aiTurn_L_Stand:
        case anim_aiTurn_L_Walk:
        case anim_aiTurn_R_Stand:
        case anim_aiTurn_R_Walk:
        case anim_aiTurn_B_Stand:
        case anim_aiTurn_B_Walk:
        case anim_aiTurn_B_Run:
        case anim_aiStanding:
        case anim_aiWalking:
        case anim_aiRunning:
            bCanEvade = true;
            break;
        }
        
        if(!bCanEvade)
        {
            return;
        }
        
        if(m_evadeActive == 1 && ++m_evadeCount >= int(m_pStageMode[LHSI_EVADESKIPS]) &&
            InPlayerProjectilePath(Math::Deg2Rad(10.0f)))
        {
            m_evadeCount = 0;
            
            if(m_lookAtYaw >= 0.0f)
            {
                BlendAnimation(anim_aiDodgeRight, 4.0f, 8.0f, false);
            }
            else
            {
                BlendAnimation(anim_aiDodgeLeft, 4.0f, 8.0f, false);
            }
        }
    }
    
    /*
    ==============================================================
    CheckForWalkRunAttack
    ==============================================================
    */
    
    bool CheckForWalkRunAttack(void)
    {
        if(m_state != BP_LHS_ATTACK)
        {
            return false;
        }
        
        bool bWalking = IsWalking();
        bool bRunning = IsRunning();
        int anim = self.AnimState().PlayingID();
        
        if(bWalking)
        {
            m_runTime += (GAME_DELTA_TIME*0.7f)*2;
        }
        else if(bRunning)
        {
            m_runTime += (GAME_DELTA_TIME*0.7f);
        }
        
        if(bWalking)
        {
            kVec3 vDir = (Player.Actor().Origin() - self.Origin()).Normalize();
            kVec3 vMoveDir = Player.Actor().Movement();
            
            if(vMoveDir.Dot(vDir) >= (0.2f*GAME_SCALE))
            {
                bWalking = false;
                bRunning = true;
            }
        }
        
        // been walking/running too long?
        if(m_runTime > m_pStageMode[LHSI_RUNTIME])
        {
            m_runTime = 0;
            
            if(Math::RandMax(100) >= 80)
            {
                self.PlaySound("sounds/shaders/longhunter_taunt_1.ksnd");
            }
            
            if(bWalking)
            {
                PickAnimation(g_BP_longhuntWalkAnims, g_BP_longhuntWalkWeights, g_BP_longhuntWalkSpeed);
            }
            else if(bRunning)
            {
                PickAnimation(g_BP_longhuntRangeAnims, g_BP_longhuntRangeWeights, g_BP_longhuntRangeSpeed);
            }
        }
        
        return (self.AnimState().PlayingID() != anim);
    }
    
    /*
    ==============================================================
    GetTargetPoint
    ==============================================================
    */
    
    void GetTargetPoint(void)
    {
        float dist;
        float x, y;
        kVec3 dir;
        kVec3 org;
        
        if(self.AnimState().PlayingID() == anim_aiMelee6)
        {
            m_desiredYaw = 0;
            m_lookAtYaw = 0;
            return;
        }
        
        org = Player.Actor().Origin();
        
        dist = (Math::Sqrt(self.DistanceToPoint(org)) - (15*GAME_SCALE)) * 2.0f;
        if(dist > (80*GAME_SCALE))
        {
            dist = (80*GAME_SCALE);
        }
        else if(dist < 0)
        {
            dist = 0;
        }
        
        dir = (org - self.Origin()).Normalize();
        
        x = dir.y;
        y = dir.x;
        
        m_vGoalOrigin.x = Math::Sin(m_randomYaw) * x * dist + org.x;
        m_vGoalOrigin.y = Math::Cos(m_randomYaw) * y * dist + org.y;
        m_vGoalOrigin.z = self.Origin().z;
        
        m_randomYaw += Math::Deg2Rad(5.0f) * 0.5f;
        
        m_goalDistSq = self.DistanceToPoint(org);
        m_goalDist = Math::Sqrt(m_goalDistSq);
        
        m_desiredYaw = self.GetAvoidanceAngle(m_vGoalOrigin, 1.5f) - self.Yaw();
        m_lookAtYaw = self.GetTurnYaw(Player.Actor().Origin());
    }
    
    /*
    ==============================================================
    StandTurn
    ==============================================================
    */
    
    void StandTurn(void)
    {
        bool bInterrupt = !(self.AnimState().PlayingID() == anim_aiStanding);
        
        if(self.AnimState().PlayingID() != anim_aiTurn_B_Stand)
        {
            if(m_lookAtYaw >= Math::Deg2Rad(-100.0f))
            {
                if(m_lookAtYaw > Math::Deg2Rad(-15.0f))
                {
                    if(m_lookAtYaw > Math::Deg2Rad(15.0f))
                    {
                        BlendAnimation(anim_aiTurn_R_Stand, 4.0f, 8.0f, bInterrupt);
                    }
                }
                else
                {
                    BlendAnimation(anim_aiTurn_L_Stand, 4.0f, 8.0f, bInterrupt);
                }
            }
            else
            {
                BlendAnimation(anim_aiTurn_B_Stand, 4.0f, 8.0f, bInterrupt);
            }
        }
        
        if(Math::Fabs(m_lookAtYaw) <= Math::Deg2Rad(8.0f))
        {
            BlendAnimation(anim_aiStanding, 4.0f, 8.0f, false);
        }
    }
    
    /*
    ==============================================================
    WalkTurn
    ==============================================================
    */
    
    void WalkTurn(void)
    {
        bool bInterrupt = !(self.AnimState().PlayingID() == anim_aiStanding);
        
        if(self.AnimState().PlayingID() != anim_aiTurn_B_Walk)
        {
            TurnAngles(Math::Deg2Rad(6.0f), m_desiredYaw);
            
            if(m_desiredYaw >= Math::Deg2Rad(-150.0f))
            {
                if(m_desiredYaw > Math::Deg2Rad(-20.0f))
                {
                    if(m_desiredYaw > Math::Deg2Rad(20.0f))
                    {
                        BlendAnimation(anim_aiTurn_R_Walk, 4.0f, 8.0f, bInterrupt);
                    }
                }
                else
                {
                    BlendAnimation(anim_aiTurn_L_Walk, 4.0f, 8.0f, bInterrupt);
                }
            }
            else
            {
                BlendAnimation(anim_aiTurn_B_Walk, 4.0f, 8.0f, bInterrupt);
            }
        }
        
        if(Math::Fabs(m_desiredYaw) <= Math::Deg2Rad(30.0f))
        {
            BlendAnimation(anim_aiWalking, 4.0f, 8.0f);
        }
    }
    
    /*
    ==============================================================
    RunTurn
    ==============================================================
    */
    
    void RunTurn(void)
    {
        bool bInterrupt = !(self.AnimState().PlayingID() == anim_aiStanding);
        
        if(self.AnimState().PlayingID() != anim_aiTurn_B_Run)
        {
            TurnAngles(Math::Deg2Rad(12.0f), m_desiredYaw);
            
            if(m_desiredYaw >= Math::Deg2Rad(-150.0f))
            {
                if(m_desiredYaw > Math::Deg2Rad(-25.0f))
                {
                    if(m_desiredYaw > Math::Deg2Rad(25.0f))
                    {
                        BlendAnimation(anim_aiTurn_R_Run, 5.0f, 8.0f, bInterrupt);
                    }
                }
                else
                {
                    BlendAnimation(anim_aiTurn_L_Run, 5.0f, 8.0f, bInterrupt);
                }
            }
            else
            {
                BlendAnimation(anim_aiTurn_B_Run, 5.0f, 8.0f, bInterrupt);
            }
        }
        
        if(Math::Fabs(m_desiredYaw) <= Math::Deg2Rad(35.0f))
        {
            BlendAnimation(anim_aiRunning, 5.0f, 8.0f);
        }
    }
    
    /*
    ==============================================================
    IsPlayerLocked
    ==============================================================
    */
    
    bool IsPlayerLocked(void)
    {
        return (Player.Actor().Health() > 0 && Player.Locked());
    }
    
    /*
    ==============================================================
    DoLockedTaunt
    ==============================================================
    */
    
    void DoLockedTaunt(void)
    {
        if(!Player.Locked())
        {
            return;
        }
        --m_standingTauntTime;
        if(m_standingTauntTime <= 0)
        {
            BlendAnimation(anim_longHunterTaunt, 4.0f, 8.0f, false);
            self.PlaySound("sounds/shaders/longhunter_taunt_1.ksnd");
            m_standingTauntTime = 250 + Math::RandMax(111);
        }
        else
        {
            BlendAnimation(anim_aiStanding, 4.0f, 8.0f, false);
        }
    }
    
    /*
    ==============================================================
    DoAttacks
    ==============================================================
    */
    
    void DoAttacks(void)
    {
        switch(self.AnimState().PlayingID())
        {
        case anim_aiTurn_L_Run:
        case anim_aiTurn_R_Run:
        case anim_aiTurn_B_Run:
            return;
        }
        
        // don't attack during cinematics unless the player is dead
        if(IsPlayerLocked())
        {
            DoLockedTaunt();
            return;
        }
        
        if(m_goalDist <= (12*GAME_SCALE) && Math::Fabs(m_desiredYaw) <= Math::Deg2Rad(15.0f))
        {
            PickAnimation(g_BP_longhuntMeleeAnims, g_BP_longhuntMeleeWeights, g_BP_longhuntMeleeSpeed);
        }
        else if(CheckForWalkRunAttack())
        {
            m_rangeAttackLoops = int(m_pStageMode[LHSI_GUNFIRECOUNT]);
        }
    }
    
    /*
    ==============================================================
    Chase
    ==============================================================
    */
    
    void Chase(void)
    {
        float dist = (6*GAME_SCALE);
        
        // don't chase toward player during cinematics unless he is dead
        if(IsPlayerLocked())
        {
            DoLockedTaunt();
            return;
        }
        
        if(m_goalDist > (20*GAME_SCALE))
        {
            RunTurn();
        }
        else if(m_goalDist > dist)
        {
            WalkTurn();
        }
        
        if(m_goalDist <= dist)
        {
            StandTurn();
        }
        
        if(m_state == BP_LHS_ATTACK)
        {
            DoAttacks();
        }
        
        if(self.AnimState().Stopped())
        {
            self.AnimState().Blend(anim_aiStanding, 4.0f, 8.0f, ANF_ROOTMOTION);
        }
    }
    
    /*
    ==============================================================
    LoopAttack
    ==============================================================
    */
    
    void LoopAttack(const int anim, const float speed)
    {
        if(self.AnimState().Stopped())
        {
            m_rangeAttackLoops--;
        }
        else
        {
            TurnAngles(Math::Deg2Rad(12.0f), m_lookAtYaw);
            return;
        }
        
        if(m_rangeAttackLoops > 0)
        {
            self.AnimState().Blend(anim, speed, 4.0f, ANF_ROOTMOTION);
            TurnAngles(Math::Deg2Rad(20.0f), m_lookAtYaw);
        }
        else
        {
            DoAttacks();
        }
        
        if(self.AnimState().Stopped())
        {
            Chase();
        }
    }
    
    /*
    ==============================================================
    UpdateStage
    ==============================================================
    */
    
    void UpdateStage(void)
    {
        float healthPercnt = float(self.Health()*100) / m_startHealth;
        
        if(healthPercnt < m_pStageMode[LHSI_HEALTHPERCNT])
        {
            m_stage++;
            if(m_stage >= int(g_BP_longhuntStages.length()))
            {
                m_stage = int(g_BP_longhuntStages.length()-1);
            }
            
            @m_pStageMode = g_BP_longhuntStages[m_stage];
            m_animSpeed = m_pStageMode[LHSI_SPEED];
        }
    }
    
    /*
    ==============================================================
    SetGlobalState
    ==============================================================
    */
    
    void SetGlobalState(const int stateVal)
    {
        GameVariables.SetValue("longHunterState" + m_bossID, "" + stateVal);
        SetGlobalProperties();
    }
    
    /*
    ==============================================================
    SetGlobalProperties
    ==============================================================
    */
    
    void SetGlobalProperties(void)
    {
        GameVariables.SetValue("longHunterHealth" + m_bossID, "" + self.Health());
        GameVariables.SetValue("longHunterSector" + m_bossID, "" + self.SectorIndex());
        GameVariables.SetValue("longHunterOrigin" + m_bossID, self.Origin().ToString());
        GameVariables.SetValue("longHunterYaw" + m_bossID, "" + self.Yaw());
    }
    
    /*
    ==============================================================
    GetGlobalState
    ==============================================================
    */
    
    void GetGlobalState(int &out stateVal, int &out stateHealth, kVec3 &out vStateOrigin,
                        int &out stateSector, float &out stateYaw)
    {
        GameVariables.GetInt("longHunterState" + m_bossID, stateVal);
        GameVariables.GetInt("longHunterHealth" + m_bossID, stateHealth);
        GameVariables.GetInt("longHunterSector" + m_bossID, stateSector);
        GameVariables.GetVector("longHunterOrigin" + m_bossID, vStateOrigin);
        GameVariables.GetFloat("longHunterYaw" + m_bossID, stateYaw);
    }
    
    /*
    ==============================================================
    OnTick
    ==============================================================
    */
    
    void OnTick(void)
    {
		if (Player.Actor().Health() <= 0 || Player.Locked())
        {
            if (!m_bTargetDead)
            {
                SetGlobalState(m_state);
                m_bTargetDead = true;
            }
        }
		
        switch(m_state)
        {
        case BP_LHS_ATTACK:
            GetTargetPoint();
            CheckForEvade();
            break;
            
        case BP_LHS_IDLE:
            if(self.AnimState().PlayingID() == anim_longHunterTaunt)
            {
                if(self.AnimState().TrackTime() >= 0.8f)
                {
			        self.SetHeadTrackTarget(Player.Actor().CastToActor());
                }
            }
            return;
            
        case BP_LHS_DEAD:
            return;
        }
        
        UpdateStage();
        
        switch(self.AnimState().PlayingID())
        {
        case anim_aiTurn_L_Run:
        case anim_aiTurn_R_Run:
            if(Math::Fabs(m_desiredYaw) <= Math::Deg2Rad(15.0f))
            {
                BlendAnimation(anim_aiRunning, 4.0f, 8.0f, false);
                break;
            }
            // fall through
        case anim_aiTurn_L_Stand:
        case anim_aiTurn_L_Walk:
        case anim_aiTurn_R_Stand:
        case anim_aiTurn_R_Walk:
        case anim_aiTurn_B_Stand:
        case anim_aiTurn_B_Walk:
        case anim_aiTurn_B_Run:
        case anim_aiStanding:
        case anim_aiWalking:
        case anim_aiRunning:
            Chase();
            break;
            
        case anim_aiDodgeRight:
        case anim_aiDodgeLeft:
        case anim_aiMelee1:
        case anim_aiMelee2:
        case anim_aiMelee3:
        case anim_longHunterTaunt:
            if(self.AnimState().Stopped())
            {
                DoAttacks();
                
                if(self.AnimState().Stopped())
                {
                    Chase();
                }
            }
            else
            {
                float max = m_goalDist / (4*GAME_SCALE);
                Math::Clamp(max, 2.0f, 50.0f);
                
                TurnAngles(Math::Deg2Rad(max), m_lookAtYaw);
            }
            break;
            
        case anim_aiMelee4:
            LoopAttack(anim_aiMelee4, 4.0f);
            break;
            
        case anim_aiMelee5:
            LoopAttack(anim_aiMelee5, 1.14f);
            break;
            
        case anim_aiMelee6:
            LoopAttack(anim_aiMelee6, 4.0f);
            break;
        }
    }
    
    /*
    ==============================================================
    OnBeginLevel
    ==============================================================
    */
    
    void OnPostBeginLevel(void)
    {
		m_bossID = self.SpawnParams(7);
		
        int state, sector, health;
        float yaw;
        kVec3 vOrigin;
        
		GameVariables.GetInt("longHunterInitHealth" + m_bossID, m_startHealth);
        
        self.SetTarget(Player.Actor().CastToActor());
        
        self.AnimState().Set(anim_aiStanding, 4.0f, ANF_ROOTMOTION);
        self.ImpactType() = IT_FLESH_HUMAN;
        
        GetGlobalState(state, health, vOrigin, sector, yaw);
        
        if (state == BP_LHGS_NEW)
        {
			m_startHealth = float(self.Health());
			GameVariables.SetValue("longHunterInitHealth" + m_bossID, "" + self.Health());
			m_state = BP_LHS_ATTACK;
            self.SetHeadTrackTarget(Player.Actor().CastToActor());
			SetGlobalState(m_state);
            return;
        }
        
        switch(state)
        {
        case BP_LHGS_INCOMBAT:
            m_state = BP_LHS_ATTACK;
            self.SetHeadTrackTarget(Player.Actor().CastToActor());
            break;
        case BP_LHGS_DEAD:
            self.AnimState().Set(anim_aiDeathStand, 8.0f, ANF_ROOTMOTION);
            self.AnimState().SetLastFrame();
            self.ModelVariation() = 1;
			self.Flags() &= ~AF_SOLID;
			self.Flags() |= AF_DEAD;
            break;
        }
        
        self.Health() = health;
        self.Origin() = vOrigin;
        self.SetSector(sector);
        self.Yaw() = yaw;
    }
    
    /*
    ==============================================================
    OnEndLevel
    ==============================================================
    */
    
    void OnEndLevel(void)
    {
        SetGlobalProperties();
    }
    
    /*
    ==============================================================
    OnDamage
    ==============================================================
    */
    
    void OnDamage(kActor @instigator, kDictMem @damageDef, const int damage)
    {
        if(self.Health() <= 0)
        {
            return;
        }
        
        if(Math::RandMax(99) > 80)
        {
            self.PlaySound("sounds/shaders/longhunter_taunt_3.ksnd");
        }
    }
    
    /*
    ==============================================================
    OnDeath
    ==============================================================
    */
    
    void OnDeath(kActor @killer, kDictMem @damageDef)
    {
        self.SetTarget(null);
        self.SetHeadTrackTarget(null);
		
		self.AnimState().Set(anim_aiDeathStand, 4.0f, ANF_ROOTMOTION);
        self.ModelVariation() = 1;
		self.Flags() &= ~AF_SOLID;
		self.Flags() |= AF_DEAD;

        m_state = BP_LHS_DEAD;
        SetGlobalState(BP_LHGS_DEAD);
    }
}
