//
// 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:
//      Controller for manipulating missile particles to home in on targets
//

const float AIM_MAX             = (GAME_SCALE*100.0f);
const float AIM_MIN             = (GAME_SCALE*20.0f);
const float AIM_RANGE           = (AIM_MAX - AIM_MIN);

const float AIM_BLEND_MAX       = 0.9f;
const float AIM_BLEND_MIN       = 0.04f;
const float AIM_BLEND_RANGE     = (AIM_BLEND_MAX - AIM_BLEND_MIN);

/*
==============================================================
TurokMissileController
==============================================================
*/

class TurokMissileController : ScriptObject
{
    kActor@ self;
    
    TurokMissileController(kActor@ actor)
    {
        @self = actor;
    }
    
    /*
    ==============================================================
    OnTick
    ==============================================================
    */
    
    void OnTick(void)
    {
        if(self.GetTarget() is null)
        {
            self.Remove();
            return;
        }
        
        // the type of target must be known. In this case, we should
        // already know that this is a particle
        kParticle@ pParticle = self.GetTarget().CastToParticle();
        
        if(pParticle is null)
        {
            self.Remove();
            return;
        }
        
        // missile particle dead?
        if(pParticle.IsStale() || (pParticle.GetTarget() is null))
        {
            self.Remove();
            return;
        }
        
        kActor@ pTarget = pParticle.GetTarget();
        
        // target must have a valid world component
        if(pTarget.WorldComponent() is null)
        {
            self.Remove();
            return;
        }
        
        // get destination
        kVec3 vTargetPos = pTarget.Origin();
        vTargetPos.z += pTarget.WorldComponent().HeightOffset();
        vTargetPos.z += pTarget.WorldComponent().Height() * 0.5f;

        // if the missile is close enough to the target, then snap it's
        // origin to the target's origin
        float dist = vTargetPos.Distance(pParticle.Origin());
        if(dist <= GAME_SCALE*5.0f)
        {
            pParticle.Origin() = pTarget.Origin();
            return;
        }
        
        // get aim vector
        kVec3 vAim = vTargetPos - pParticle.Origin();
        vAim.Normalize();

        float speed = pParticle.Velocity().Unit();
        float blend = 0.0f;

        // determine how much to interpolate
        if(dist <= AIM_MAX)
        {
            blend = Math::Max(0.0f, dist - AIM_MIN) / AIM_RANGE;
            blend = AIM_BLEND_MAX - (blend * AIM_BLEND_RANGE);
        }
        else
        {
            blend = AIM_BLEND_MIN;
        }

        // interpolate direction and change velocity
        pParticle.Direction().Lerp(vAim, blend);
        pParticle.Velocity() = (pParticle.Direction() * speed);
    }
};

/*
==============================================================
TurokBoreController
==============================================================
*/

class TurokBoreController : ScriptObject
{
    kActor@ self;
    
    TurokBoreController(kActor@ actor)
    {
        @self = actor;
    }
    
    /*
    ==============================================================
    OnTick
    ==============================================================
    */
    
    void OnTick(void)
    {
        if(self.GetTarget() is null)
        {
            self.Remove();
            return;
        }
        
        // the type of target must be known. In this case, we should
        // already know that this is a particle
        kParticle@ pParticle = self.GetTarget().CastToParticle();
        
        if(pParticle is null)
        {
            self.Remove();
            return;
        }
        
        // missile particle dead?
        if(pParticle.IsStale() || (pParticle.GetTarget() is null))
        {
            self.Remove();
            return;
        }
        
        if(pParticle.GetTarget().Health() <= 0)
        {
            self.Remove();
            return;
        }
        
        self.Origin() = pParticle.Origin();
        
        if(pParticle.IsSticking())
        {
            pParticle.GetTarget().PlaySound("sounds/shaders/Cerebral Bore Boring.ksnd");
            self.Remove();
            return;
        }
    }
};

/*
==============================================================
TurokBladeController
==============================================================
*/

const float SAWBLADE_TIME_BEFORE_RETURNING      = 15.0f;
const float SAWBLADE_SNAP_TO_HAND_RADIUS        = (50.0f*GAME_SCALE);
const float SAWBLADE_SNAP_BACK_FAILSAFE_TIME    = 65.0f;

class TurokBladeController : ScriptObject
{
    kActor@ self;
    
    TurokBladeController(kActor@ actor)
    {
        @self = actor;
    }
    
    /*
    ==============================================================
    OnTick
    ==============================================================
    */
    
    void OnTick(void)
    {
        if(self.GetTarget() is null)
        {
            self.Remove();
            return;
        }
        
        // the type of target must be known. In this case, we should
        // already know that this is a particle
        kParticle@ pParticle = self.GetTarget().CastToParticle();
        
        if(pParticle is null)
        {
            self.Remove();
            return;
        }
        
        // missile particle dead?
        if(pParticle.IsStale())
        {
            self.Remove();
            return;
        }
        
        self.Origin() = pParticle.Origin();
        self.PlayLoopingSound("sounds/shaders/Razor Wind Fly Loop.ksnd");
        
        // ---------------------------------------------------------------------
        // after a certain time has passed, return back to hand
        if(pParticle.LifeFrames() > SAWBLADE_TIME_BEFORE_RETURNING)
        {
            // get current speed
            float speed = pParticle.Velocity().Unit();

				kActor@ pOwner = pParticle.Owner();

				//bGragbit.Origin() = pParticle.Origin();
            
			    if(pOwner is null)
				{
					pParticle.Remove();
					self.Remove();
					///bGragbit.Remove();
					
				
            

				}
            
            // check if blade has struck something that bleeds
            if((pParticle.Flags() & (FXF_DREW_BLOOD | FXF_DREW_BLOOD_GREEN)) != 0)
            {
				
				
				
                if(pOwner.InstanceOf("kexPuppet"))
                {
						kPuppet@ pPuppet = pOwner.CastToPuppet();

                    kWeapon@ pWeapon = pPuppet.PlayerOwner().WeaponActor();
					kActor@ bGragbit = ActorFactory.Spawn(kActor_AI_Mite,
					pParticle.Origin(),
					0.0f,
					0.0f,
					0.0f);
					ParticleFactory.Spawn(kBrutal_Particle_Multiplayer33, bGragbit, bGragbit.Origin(), kQuat(0, 0, 0), Math::vecZero);
					bGragbit.Origin() = pParticle.Origin();
					bGragbit.Remove();
					if(bGragbit is null)
					{
						///bGragbit.Flags() |= AF_HIDDEN|AF_DEAD;
						///bGragbit.Flags() == ~AF_IMPORTANT|AF_HIDDEN|AF_DEAD;
						///bGragbit.Flags.Type() == AF_IMPORTANT;
						///bGragbit.Flags() = (AF_HIDDEN|AF_DEAD);
						///bGragbit.Flags() |= AF_IMPORTANT;
						////bGragbit.Remove();
					}
                    if(!(pWeapon is null) && pWeapon.Type() == kActor_Wpn_RazorWind)
                    {
                        // change the Razor Wind to the bloody texture set
                        // TODO: don't do if violence options are "no blood"
                        int textureNum = (pParticle.Flags() & FXF_DREW_BLOOD_GREEN) != 0 ? 1 : 0;
                        pWeapon.RenderMeshComponent().AltTexture() = textureNum;
                    }
                }
            }
            
            // ---------------------------------------------------------------------
            // get position to player's hand and distance to it
            kVec3 vHandPos(2.6f*GAME_SCALE, 14.0f*GAME_SCALE, 8.0f*GAME_SCALE);
            kVec3 vHandWorldPos = pOwner.LocalVectorToWorldSpace(vHandPos);
            kVec3 vTargetPos = vHandWorldPos - pParticle.Origin();
            
            float dist = vTargetPos.Unit();
            kVec3 vAim;
            
            // ---------------------------------------------------------------------
            // if close to player's hand
            if(dist <= (SAWBLADE_SNAP_TO_HAND_RADIUS + (3.0f*GAME_SCALE)))
            {
				
                // snap it back?
                if(dist < (SAWBLADE_SNAP_TO_HAND_RADIUS / 3.0f) ||
                    pParticle.LifeFrames() >= SAWBLADE_SNAP_BACK_FAILSAFE_TIME)
                {

					
                    pParticle.Origin() = vHandWorldPos;
                    pParticle.Direction().x = 0;
                    pParticle.Direction().y = 0;
                    pParticle.Direction().z = 1;
                    pParticle.Velocity().Clear();
                    pParticle.Kill();
                    self.Remove();
                    return;
                }

                
                vAim = vTargetPos;
            }
            // ---------------------------------------------------------------------
            // handle outside snap to player's hand
            else
            {
                vHandPos = kVec3(10.0f*GAME_SCALE, SAWBLADE_SNAP_TO_HAND_RADIUS, 0.0f);

                if(!(pOwner.WorldComponent() is null))
                {
                    vHandPos.z += pOwner.WorldComponent().Height() * 0.8f;

                }



                
                vHandWorldPos = pOwner.LocalVectorToWorldSpace(vHandPos);
                vAim = vHandWorldPos - pParticle.Origin();
                dist = vAim.Unit();
            }
            
            // ---------------------------------------------------------------------
            // blend new directions together
            vAim.Normalize();
            float dot = vAim.Dot(pParticle.Direction());
            float angle = Math::ACos(dot);
            float blend = 1.0f;
            
            if(angle >= Math::Deg2Rad(15.0f))
            {
                blend = 0.5f;
            }
            
            // interpolate direction and change velocity
            pParticle.Direction().Lerp(vAim, blend);
            pParticle.Velocity() = (pParticle.Direction() * speed);
        }
    }
};
