#include "../bot/gen_bot_header.h"
#include "talkmonster.h"
#include "buddy_aura.h"
#include "buddy.h"
#include "buddy_weapons.h"
#include "weapons.h"
#include "beamattack.h"
#include "kiattack.h"
#include "guidedattack.h"
//#include "controlledbeam.h"

#define GENERICBALL_TRAILLENGTH		40

extern CBotManager BotManager;
extern cvar_t simplemelee;

// Base buddy weapon - Start
float CBaseBuddyWeapon::GetPowerRatio( void )
{	if (g_pGameRules->m_iAveragePowerLevel)
	{	float flRatio = (float)m_pOwner->GetPowerLevel()/(float)g_pGameRules->m_iAveragePowerLevel;
		return flRatio<MAX_POWER_RATIO?flRatio:MAX_POWER_RATIO;
	}
	else
	{	return 0;
	}
}
// Base buddy weapon - End


// Buddy melee - Start

void CBuddyMelee::ShootSecondary( void )
{
	if (m_fShootDelay > gpGlobals->time)
		return;

	if (!m_bActive)
		Activate();

	// Get nearest enemy player
	CBasePlayer *pPlayer = m_pOwner->m_Melee.GetNearestPlayerToMelee();

	// Get nearest enemy buddy
	CBuddy *pBuddy = m_pOwner->m_Melee.GetNearestBuddyToMelee();

	if (!pBuddy && !pPlayer)
		return;

	// See which is the closest
	if (pBuddy && pPlayer)
	{
		if (UTIL_Distance(pBuddy->pev->origin, m_pOwner->pev->origin) <
			UTIL_Distance(pPlayer->pev->origin, m_pOwner->pev->origin))
			pPlayer = NULL; // So the buddy is used
		else
			pBuddy = NULL; // So the player is used
	}
	
	// Simple Melee
	if(simplemelee.value == 1)
	{	
		if (m_pOwner->UseKi(BLOW_KICK_KI_COST))
		{
			Vector vecDirection = m_pOwner->GetSwoopDirection();
			int iDirection = m_pOwner->m_Swoop.iSwoopDir;
			// Are we going to melee a player?
			if (pPlayer)
			{
				// Super punch
				m_pOwner->m_Melee.SimpleMeleePowerhit( pPlayer, vecDirection, iDirection );
			}
			else
			{
				// Super punch
				m_pOwner->m_Melee.SimpleMeleePowerhit( pBuddy, vecDirection, iDirection );
			}

			m_fShootDelay = UTIL_WeaponTimeBase() + 0.9;
		}
		else
		{	m_fShootDelay = UTIL_WeaponTimeBase() + 0.1;
		}
	}
	// Advanced Melee
	else
	{
		bool fBlocked = false;

		// Attacking a player?
		if (pPlayer)
		{
			// their melee attack
			if(pPlayer->m_Swoop.m_fSwoop && (m_pOwner == pPlayer->m_Melee.GetBuddyEnemyPrepunch(NULL)) &&
				(pPlayer->m_pActiveItem ? (pPlayer->m_pActiveItem->m_iId == WEAPON_MELEE ||
				pPlayer->m_pActiveItem->m_iId == WEAPON_SWORD) : false))
			{	// Call their swoop function to enable their melee hit
				if(pPlayer->m_Melee.GetBuddyEnemyPrepunch( m_pOwner ))
				{	pPlayer->Swoop();
					return;
				}
			}
			else if((pPlayer->m_Melee.m_iConsecutiveHits < CONSECUTIVE_MELEEHITS) &&
					m_pOwner->UseKi(BLOW_KICK_KI_COST))
			{	Vector vecDirection = m_pOwner->GetSwoopDirection();
				int iDirection = m_pOwner->m_Swoop.iSwoopDir;
				// Super punch
				fBlocked = m_pOwner->m_Melee.Powerhit( pPlayer, vecDirection, iDirection );
			}
		}
		else // we are attacking a buddy
		{
			// Other bud swoops?
			if (pBuddy->m_Swoop.bActive && m_pOwner->m_Swoop.bActive)
			{
				m_pOwner->m_Melee.HandleHeadOn(pBuddy);
			}/*
			// their melee attack
			if(pPlayer->m_Swoop.m_fSwoop && (m_pPlayer == pPlayer->m_Melee.GetEnemyPrepunch(NULL)) &&
				(pPlayer->m_pActiveItem ? (pPlayer->m_pActiveItem->m_iId == WEAPON_MELEE ||
				pPlayer->m_pActiveItem->m_iId == WEAPON_SWORD) : false))
			{	// Call their swoop function to enable their melee hit
				if(pPlayer->m_Melee.GetEnemyPrepunch( m_pPlayer ))
				{	pPlayer->Swoop();
					return;
				}
			}*/
			else if((pBuddy->m_Melee.m_iConsecutiveHits < CONSECUTIVE_MELEEHITS) &&
					m_pOwner->UseKi(BLOW_KICK_KI_COST))
			{	Vector vecDirection = m_pOwner->GetSwoopDirection();
				int iDirection = m_pOwner->m_Swoop.iSwoopDir;
				// Super punch
				fBlocked = m_pOwner->m_Melee.Powerhit( pBuddy, vecDirection, iDirection );
			}
		}

		if(fBlocked)
			m_fShootDelay = UTIL_WeaponTimeBase() + 0.9;
		else
			m_fShootDelay = UTIL_WeaponTimeBase() + 0.65;
	}
}

// Buddy melee - End


// Buddy kiblast - Start
CBuddyKiblast::CBuddyKiblast(CBuddy *pOwner) : CBaseBuddyWeapon(pOwner), m_bShootLeft(true)
{
	m_sFlareA = PRECACHE_MODEL("sprites/kiblasta.spr");
	m_sFlareB = PRECACHE_MODEL("sprites/kiblastb.spr");
	m_sTrail = PRECACHE_MODEL("sprites/kiblasttrail.spr");

	PRECACHE_SOUND("weapons/kiblast.wav");
	PRECACHE_SOUND("weapons/kiplosion.wav");
}

void CBuddyKiblast::ShootPrimary()
{
	if (m_fShootDelay > gpGlobals->time)
		return;

	// Check we have some ammo
	if (!m_pOwner->UseKi(KIBLAST_PRIMARYKI_COST))
	{	return;
	}

	if (!m_bActive)
		Activate();

	//emit sound
	EMIT_SOUND(ENT(m_pOwner->pev), CHAN_WEAPON, "weapons/kiblast.wav", 1.0, ATTN_NORM);

	//play animation
	m_pOwner->SetAnimation( PLAYER_ATTACK1 );
	
	//shoot it
	//UTIL_MakeVectors( m_pOwner->pev->v_angle );
	UTIL_MakeVectors( m_pOwner->pev->v_angle );
	Vector vecSrc = (m_pOwner->GetGunPosition() + gpGlobals->v_right * 10) + gpGlobals->v_forward * 5;
	Vector vecThrow = gpGlobals->v_forward * KIBLAST_SPEED;
	CKiAttack::ShootKiAttack(m_pOwner->pev, vecSrc, vecThrow, KIBLAST_PRIMARYDAMAGE * GetPowerRatio() * 0.75f,m_sFlareA,m_sFlareB,m_sTrail);
	m_fShootDelay = UTIL_WeaponTimeBase() + 0.5;
}

void CBuddyKiblast::ShootSecondary()
{
	if (m_fShootDelay > gpGlobals->time)
		return;

	// Check we have some ammo
	if (!m_pOwner->UseKi(KIBLAST_SECONDARYKI_COST))
	{	return;
	}
	
	if (!m_bActive)
		Activate();

	//emit sound
	EMIT_SOUND(ENT(m_pOwner->pev), CHAN_WEAPON, "weapons/kiblast.wav", 1.0, ATTN_NORM);

	//shoot it
	int dir = RANDOM_LONG(0,10);
	int up = RANDOM_LONG(-10,10);
	
	//choose shoot animation, shoot origin
	UTIL_MakeVectors( m_pOwner->pev->v_angle );
	Vector vecSrc;
	Vector vecThrow;
	if(m_bShootLeft)
	{	vecSrc = (m_pOwner->GetGunPosition() + gpGlobals->v_right * 10) + gpGlobals->v_forward.Normalize() * 10;
		vecThrow = gpGlobals->v_forward * KIBLAST_SPEED + gpGlobals->v_right * -dir + gpGlobals->v_up * up;
		m_pOwner->SetAnimation( PLAYER_ATTACK1 );
	}
	else
	{	vecSrc = (m_pOwner->GetGunPosition() + gpGlobals->v_right * -10) + gpGlobals->v_forward.Normalize() * 10;
		vecThrow = gpGlobals->v_forward * KIBLAST_SPEED + gpGlobals->v_right * dir + gpGlobals->v_up * up;
		m_pOwner->SetAnimation( PLAYER_ATTACK2 );
	}
	m_bShootLeft = !m_bShootLeft;
	
	CKiAttack::ShootCurvedKiAttack( m_pOwner->pev, vecSrc, vecThrow, KIBLAST_SECONDARYDAMAGE * GetPowerRatio() * 0.75f,m_sFlareA,m_sFlareB,m_sTrail);
	m_fShootDelay = UTIL_WeaponTimeBase() + 0.35;
}

BOOL CBuddyKiblast::WeaponThink(CBasePlayer *pBuddyOwner)
{
	if (pBuddyOwner->pev->button & (IN_ATTACK || IN_ATTACK2))
	{
		if (RANDOM_LONG(1,10) > 3)
			ShootPrimary();
		else
			ShootSecondary();
	}	

	return true;
}

// Buddy Kiblast - End

// Buddy Generic Beam - Start
CBuddyGenericBeam::CBuddyGenericBeam(CBuddy *pOwner) : CBaseBuddyWeapon(pOwner)
{
m_sFlareAs[0] = PRECACHE_MODEL("sprites/blueattacka.spr");
	m_sFlareAs[1] = PRECACHE_MODEL("sprites/greenattacka.spr");
	m_sFlareAs[2] = PRECACHE_MODEL("sprites/purpleattacka.spr");
	m_sFlareAs[3] = PRECACHE_MODEL("sprites/redattacka.spr");
	m_sFlareAs[4] = PRECACHE_MODEL("sprites/yellowattacka.spr");

	m_sFlareBs[0] = PRECACHE_MODEL("sprites/blueattackb.spr");
	m_sFlareBs[1] = PRECACHE_MODEL("sprites/greenattackb.spr");
	m_sFlareBs[2] = PRECACHE_MODEL("sprites/purpleattackb.spr");
	m_sFlareBs[3] = PRECACHE_MODEL("sprites/redattackb.spr");
	m_sFlareBs[4] = PRECACHE_MODEL("sprites/yellowattackb.spr");

	m_sStartAs[0] = PRECACHE_MODEL("sprites/blueattackastart.spr");
	m_sStartAs[1] = PRECACHE_MODEL("sprites/greenattackastart.spr");
	m_sStartAs[2] = PRECACHE_MODEL("sprites/purpleattackastart.spr");
	m_sStartAs[3] = PRECACHE_MODEL("sprites/redattackastart.spr");
	m_sStartAs[4] = PRECACHE_MODEL("sprites/yellowattackastart.spr");

	m_sStartBs[0] = PRECACHE_MODEL("sprites/blueattackbstart.spr");
	m_sStartBs[1] = PRECACHE_MODEL("sprites/greenattackbstart.spr");
	m_sStartBs[2] = PRECACHE_MODEL("sprites/purpleattackbstart.spr");
	m_sStartBs[3] = PRECACHE_MODEL("sprites/redattackbstart.spr");
	m_sStartBs[4] = PRECACHE_MODEL("sprites/yellowattackbstart.spr");

	m_sTrails[0] = PRECACHE_MODEL("sprites/bluetrail.spr");
	m_sTrails[1] = PRECACHE_MODEL("sprites/greentrail.spr");
	m_sTrails[2] = PRECACHE_MODEL("sprites/purpletrail.spr");
	m_sTrails[3] = PRECACHE_MODEL("sprites/redtrail.spr");
	m_sTrails[4] = PRECACHE_MODEL("sprites/yellowtrail.spr");

	PRECACHE_MODEL("sprites/bluecharge.spr");
	PRECACHE_MODEL("sprites/greencharge.spr");
	PRECACHE_MODEL("sprites/purplecharge.spr");
	PRECACHE_MODEL("sprites/redcharge.spr");
	PRECACHE_MODEL("sprites/yellowcharge.spr");

	PRECACHE_SOUND("weapons/basicbeam_charge.wav");
	PRECACHE_SOUND("weapons/basicbeam_fire.wav");

	// from controlled weapon
	PRECACHE_SOUND("weapons/beamhead.wav");
	PRECACHE_SOUND("weapons/beamfire.wav");
}

void CBuddyGenericBeam::ShootPrimary()
{
	if (gpGlobals->time  < m_flNextPrimaryAttack)
		return;
	
	m_flNextPrimaryAttack = UTIL_WeaponTimeBase() + CHARGETIME;	
	//don't fire a new one while aiming a controlled beam
	if (m_eAttackState == ATTACK_AIM  && m_pMagic)
	{	if(m_pMagic->m_fExploded || m_pMagic->m_fReleased)
		{	
			StopAiming();
			if(m_pMagic->m_fExploded)
			{	m_pMagic->Remove();
			}
			m_pMagic = NULL;
		}
		else if (m_pMagic->m_fBlockStruggle)
		{	/*if (m_pOwner->UseKi(m_iKiCost * PS_KIRATIO))
			{	m_pMagic->SetDamage(m_pMagic->pev->dmg + GetPowerRatio() * m_iDamage);
			}
			m_pOwner->m_fBlockStruggleAttacker = true;
			MESSAGE_BEGIN( MSG_ONE, gmsgPowerStruggle, NULL, m_pPlayer->pev );
				WRITE_CHAR( (int)(m_pMagic->m_flStruggleRatio * 100));	//struggle ratio in % (-100 till 100 %)
			MESSAGE_END();			*/
		}
		else if (m_pMagic->m_fPowerStruggle)
		{	/*if (m_pPlayer->UseKi(m_iKiCost * PS_KIRATIO))
			{	// reduce our ammo
				m_pMagic->SetDamage(m_pMagic->pev->dmg + GetPowerRatio() * m_iDamage);
			}
			m_pPlayer->m_fPowerStruggling = true;
			m_pPlayer->m_pPowerStruggle = m_pMagic->m_pPowerStruggle;
			
			MESSAGE_BEGIN( MSG_ONE, gmsgPowerStruggle, NULL, m_pPlayer->pev );
				WRITE_CHAR( (int)(m_pMagic->m_flStruggleRatio * 100));	//struggle ratio in % (-100 till 100 %)
			MESSAGE_END(); */
		}
		else
		{// Edited by Pcjoe
			/*
			m_pPlayer->m_fPowerStruggling = false;
			m_pPlayer->m_fBlockStruggleAttacker = false;
			UpdateBeamJumpStatus();
		
			//hit by melee, release attack
			if(m_pPlayer->m_fMelee)
			{	m_pMagic->Release();
			}
			*/
			WeaponIdle();
			m_flNextPrimaryAttack = UTIL_WeaponTimeBase();
			return;
		}
	}
	else
	{	Charge();			
		m_flNextSecondaryAttack = m_flNextPrimaryAttack;
	}	
}

void CBuddyGenericBeam::ShootSecondary()
{
	if (gpGlobals->time  < m_flNextSecondaryAttack)
		return;

	// NOTE:: currently we only support beams (not charged balls) so this is hear just to allow us to 
	// release our attack
	if(m_eAttackState != ATTACK_AIM)
	{	/*m_fChargeBall = true;
		Charge();
		m_flNextPrimaryAttack = m_flNextSecondaryAttack = UTIL_WeaponTimeBase() + CHARGETIME;*/
	}
	else
	{		
		if(m_pMagic && m_eAttackState == ATTACK_AIM)
		{
			if(!m_pMagic->m_fPowerStruggle && !m_pMagic->m_fBlockStruggle)
			{	ReleaseAttack();
			}
		}
		m_flNextPrimaryAttack = m_flNextSecondaryAttack = UTIL_WeaponTimeBase() + CHARGETIME;
	}
}

void CBuddyGenericBeam::ReleaseAttack()
{	StopAiming();
	if(m_pMagic)
	{	if(!m_pMagic->m_fExploded && !m_pMagic->m_fReleased)
		{	m_pMagic->Explode();			
		}
		if(m_pMagic->m_fExploded)
		{	m_pMagic->Remove();
		}
		m_pMagic = NULL;
	}	
}

void CBuddyGenericBeam::EmitChargeSound()
{	EMIT_SOUND(ENT(m_pOwner->pev), CHAN_WEAPON, "weapons/basicbeam_charge.wav", 1.0, ATTN_NORM);
}

void CBuddyGenericBeam::Charge()
{	if(m_eAttackState == ATTACK_AIM)
	{	this->StopAiming();
	}

	//first charge
	if (m_eAttackState != ATTACK_CHARGE)
	{	//m_pPlayer->m_flSlowDown = m_flPlayerSlowdown;
		m_eAttackState = ATTACK_CHARGE;
		m_pOwner->m_fCharging = true;
		m_iCharge = 0;
		
		//make start charge sound
		EmitChargeSound();
	}
	
	//charge it
	if (m_iCharge < m_iMaxCharge)
	{	// Check we have some ammo
		if (!m_pOwner->UseKi(m_iKiCost))
		{	Shoot();
			return;
		}		

		if (m_iCharge == m_iMaxCharge/3)
		{	EnableChargeSprite();
		}
		
		if (m_iCharge > m_iMaxCharge/3)
		{	GrowChargeSprite();
		}

		m_iCharge++;

		/* Modifed by Rick
		MESSAGE_BEGIN( MSG_ONE, gmsgCharge, NULL, m_pPlayer->pev ); 
			WRITE_BYTE( m_iCharge*100/m_iMaxCharge);	//amount charged in %
		MESSAGE_END();*/
		
		//m_pPlayer->DrawChargeBar(m_iCharge*100/m_iMaxCharge); // Update charge bar
	}
	/* auto-shoot removed
	else
	{	Shoot();
	}
	*/
}

void CBuddyGenericBeam::StopAiming()
{	// buddies dont have bars!
	//disable powerstruggle bar
	//MESSAGE_BEGIN( MSG_ONE, gmsgPowerStruggle, NULL, m_pPlayer->pev ); 
	//	WRITE_CHAR( 127 );
	//MESSAGE_END();
 
	//m_pPlayer->m_fPowerStruggling = false;
	//m_pPlayer->m_pPowerStruggle = NULL;
	//m_pPlayer->m_fBlockStruggleAttacker = false;

	// Edited by Pcjoe, only return slowdown if the player is not ascending
	//if(!m_pOwner->m_fAscending&& !m_pPlayer->m_fMelee)
	//{	m_pOwner->m_flSlowDown = 1.0;
	//}
	//else
	{	// Pcjoe: HACK HACK - Reset sequence after we release a beam
		m_pOwner->pev->sequence = 0;
		m_pOwner->m_eAnimationTrigger = PLAYER_NULL;
		m_pOwner->ResetSequenceInfo();
	}
	m_pOwner->m_fControl = false;

	//if(m_fBeamJumping)
	//{	StopBeamJump();
	//	m_pOwner->StopFly();
	//}
	//if (m_fBeamJump)
	//{	DisableBeamJump();
	//}
	
	m_iAttack = ATTACK_NONE;
	m_eAttackState = ATTACK_IDLE;

	//disable control sound
	SilenceBodySounds();

	// Added by Pcjoe, disable model fixing
	//if(!m_pOwner->m_fWallJump)
	//{	UTIL_Model_RemoveAll(m_pPlayer->entindex());
	//}
}

void CBuddyGenericBeam::CreateBeam(Vector vecSrc, Vector vecThrow, float flSize)
{
	m_pMagic = CGuidedAttack::ShootGuidedMagic( m_pOwner->pev, vecSrc, vecThrow, m_iCharge * m_iDamage * GetPowerRatio(),m_pAttackName ,flSize, m_iExplosionColor,m_sTrail, m_sStartA, m_sStartB, m_sFlareA, m_sFlareB, m_iRadius, m_flRateOfTurn,m_iAttachment,2);	
}

void CBuddyGenericBeam::Shoot()
{	/* Modifed by Rick
	//disable charge bar
	MESSAGE_BEGIN( MSG_ONE, gmsgCharge, NULL, m_pPlayer->pev ); 
		WRITE_BYTE( 0 );	//amount charged in %
	MESSAGE_END();*/

	//m_pPlayer->DrawChargeBar(0); // disable charge bar
	//silence charge soundsa
	SilenceWeaponSounds();
		
	m_pOwner->m_fCharging = false;

	//charge to low?
	if (m_iCharge<m_iMaxCharge/3)
	{	m_eAttackState=ATTACK_IDLE;
		m_iAttack = ATTACK_NONE;
		m_pOwner->m_flSlowDown = 1.0;

		SilenceVoice();
		return;
	}

	m_eAttackState = ATTACK_AIM;	
	if(!FBitSet(m_pOwner->pev->flags,FL_ONGROUND))
	{	m_pOwner->StartFly();
	}
	m_pOwner->m_fControl = true;


	//turn the charging sprite off
	DisableChargeSprite();
		
	//make shoot/control sounds
	EmitShootSound();
	EmitControlSound();

	UTIL_MakeVectors( m_pOwner->pev->v_angle );
	Vector vecSrc = m_pOwner->GetGunPosition();

	//do a traceline, to place beamjump beams correctly
	TraceResult tr;
	UTIL_TraceLine(vecSrc, vecSrc + gpGlobals->v_forward * 10,ignore_monsters,ENT(m_pOwner->pev),&tr);
	vecSrc = vecSrc + gpGlobals->v_forward * 10 * tr.flFraction *0.9;
	Vector vecThrow = gpGlobals->v_forward * m_iSpeed;
	m_pOwner->m_flSlowDown = 0.0;
	
	//if we are aimed at an enemy this attack will be locked onto him
	Vector vecStart = m_pOwner->GetGunPosition();
	TraceResult trEnemy;
	UTIL_TraceLine(vecStart, vecStart + gpGlobals->v_forward * m_pOwner->m_flDistLook, dont_ignore_monsters, ENT(m_pOwner->pev), &trEnemy);

	// if the attack was released while the owner was targetting an enemy then we will lock onto that enemy anbd destroy him!
	if (m_pOwner->m_pEnemy)
	{
		m_pTarget = m_pOwner->m_pEnemy;
	}

	// stop moving
	m_pOwner->pev->velocity = Vector(0,0,0);
	
	//shoot the magic
	float flSize = (float)m_iCharge/(float)m_iMaxCharge;	
	CreateBeam(vecSrc,vecThrow,flSize);	
	
	// Added by Pcjoe, lock model angles
	UTIL_Model_FixAngles(m_pOwner->entindex());
}

void CBuddyGenericBeam::StopBeamJump()
{	// Edited by Pcjoe, leave movetype alone if we're in melee
	m_fBeamJumping = false;	
}

void CBuddyGenericBeam::WeaponIdle( void )
{	
	if (gpGlobals->time < m_flTimeWeaponIdle)
		return;

	if (m_eAttackState == ATTACK_CHARGE)
	{	// Added by Pcjoe: Walljump check
		//if(!CheckWallJump())
		{	Shoot();
		}
	}

	if(m_eAttackState == ATTACK_AIM)
	{	
		if(m_pMagic)
		{	//magic exploded, back to idle mode
			if(m_pMagic->m_fExploded||m_pMagic->m_fReleased)
			{	
				StopAiming();
				if(m_pMagic->m_fExploded)
				{	m_pMagic->Remove();
				}
				m_pMagic = NULL;				
			}
			//show powerstrugglebar
			else if (m_pMagic->m_fPowerStruggle)
			{	m_pOwner->m_fPowerStruggling = true;
				m_pOwner->m_pPowerStruggle = m_pMagic->m_pPowerStruggle;
				
				if (UTIL_WeaponTimeBase() > m_flTimeWeaponIdle)
				{
					/*MESSAGE_BEGIN( MSG_ONE, gmsgPowerStruggle, NULL, m_pPlayer->pev ); 
						WRITE_CHAR( (int)(m_pMagic->m_flStruggleRatio * 100));	//struggle ratio in % (-100 till 100 %)
					MESSAGE_END();*/
				}
			}
			//show block struggle bar
			else if (m_pMagic->m_fBlockStruggle)
			{	m_pOwner->m_fBlockStruggleAttacker = true;

				if (UTIL_WeaponTimeBase() > m_flTimeWeaponIdle)
				{
					/*MESSAGE_BEGIN( MSG_ONE, gmsgPowerStruggle, NULL, m_pPlayer->pev );
						WRITE_CHAR( (int)(m_pMagic->m_flStruggleRatio * 100));	//struggle ratio in % (-100 till 100 %)
					MESSAGE_END();*/
				}
			}
			else
			{	m_pOwner->m_fPowerStruggling = false;
				m_pOwner->m_pPowerStruggle = NULL;
				m_pOwner->m_fBlockStruggleAttacker = false;
//				UpdateBeamJumpStatus();
		
				// we are going to override the aim here
				if (m_pOwner->m_pEnemy)
				{
					CBaseEntity *pTemp = m_pOwner->m_pEnemy;
					CBasePlayer *pEnemy = (CBasePlayer *)pTemp;
					
					// this will be lock on aim unless the enemy flies around a corner .. is this good?
					if (pEnemy)
						m_pOwner->AimToVecFromPevOrigin(pEnemy->pev->origin, m_pMagic->pev->origin);
				}

				//hit by melee, release attack
				if(m_pOwner->m_fMelee)
				{	m_pMagic->Release();
				}
			}
		}
		else 
		{	StopAiming();			
		}		
	}

	if(m_flTimeWeaponIdle < UTIL_WeaponTimeBase())
	{	m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + CHARGETIME;
	}
}

BOOL CBuddyGenericBeam::Deploy( void )
{	InitChargeSprite();
	m_fChargeBall = false;

	//int iSpriteColor = m_pPlayer->m_pClass->GetAuraSkin();
	int iSpriteColor = AURA_PURPLE_SKIN;

	m_sTrail = m_sTrails[iSpriteColor];
	m_sFlareA = m_sFlareAs[iSpriteColor];
	m_sFlareB = m_sFlareBs[iSpriteColor];
	m_sStartA = m_sStartAs[iSpriteColor];
	m_sStartB = m_sStartBs[iSpriteColor];
	
	//set explosion color
	switch (iSpriteColor)
	{
	case AURA_WHITE_SKIN: m_iExplosionColor = BLUE_EXPLOSION;break;
	case AURA_GREEN_SKIN: m_iExplosionColor = GREEN_EXPLOSION;break;
	case AURA_PURPLE_SKIN:m_iExplosionColor = PURPLE_EXPLOSION;break;
	//case AURA_RED_SKIN:m_iExplosionColor = RED_EXPLOSION;break;
	case AURA_YELLOW_SKIN:m_iExplosionColor = YELLOW_EXPLOSION;break;
	default:break;
	};
	
	sprintf(m_pAttackName,"%sattack", "purple");

	m_eAttackState = ATTACK_IDLE;
	m_iAttack = ATTACK_NONE;
	//return DefaultDeploy( "genericbeam" );

	m_flNextPrimaryAttack = m_flNextSecondaryAttack = m_flTimeWeaponIdle = 0;
	return true;
}

void CBuddyGenericBeam::InitChargeSprite( void )
{	char pAuraColor[255];
	strcpy(pAuraColor,"purple");
	char pChargeSprite[255];
	sprintf(pChargeSprite,"sprites/%scharge.spr",pAuraColor);
	m_pChargeSprite = CSprite::SpriteCreate( pChargeSprite, m_pOwner->pev->origin, true);
	m_pChargeSprite->SetAttachment(m_pOwner->edict(),1);
	m_pChargeSprite->SetTransparency( kRenderTransAdd, 255, 255, 255, 254, kRenderFxGlowShell );
	m_pChargeSprite->SetScale(0.1);
	m_pChargeSprite->pev->framerate = 10;
	m_pChargeSprite->TurnOff();	
}

void CBuddyGenericBeam::Activate()
{
	m_eAttackState = ATTACK_IDLE; //not firing
	m_iAttack = ATTACK_NONE;
	//m_iCharge = 0;
	m_pMagic = NULL;
	//m_iId = WEAPON_GENERICBEAM;
	//m_iDefaultAmmo = 0;
	//m_iClip = -1;

	m_iMaxCharge = GENERICBEAM_MAXCHARGETIME;
	m_iSpeed = GENERICBEAM_SPEED;
	m_iKiCost = GENERICBEAM_KI_COST / 3; // it cost the main player a litte less to use their buddy
	m_iDamage = GENERICBEAM_DAMAGE * 0.75; // buddy does less damage
	m_iRadius = GENERICBEAM_RADIUS;
	m_flPlayerSlowdown = GENERICBEAM_PLAYERSLOWDOWN;
	strcpy(m_pAttackName,"genericbeam");
	m_flRateOfTurn = GENERICBEAM_ROT;
	m_iAttachment =	1;

}

// if this is called then our owner is definently attack in some sort of way
BOOL CBuddyGenericBeam::WeaponThink(CBasePlayer *pBuddyOwner)
{
	// if we were shooting a beam with our owner and his beam has exploded lets explode our beam
	if ((m_pOwner->m_fControl) && (!pBuddyOwner->m_fControl))
	{
		ShootSecondary();
		return true;
	}

	if (pBuddyOwner->pev->button & (IN_ATTACK))
	{
		ShootPrimary();
	}
	else if (pBuddyOwner->pev->button & (IN_ATTACK2))
	{
		// only if we are aimed at an enemy do we shoot him
		CBaseBuddyWeapon *pAttack;
		pAttack = m_pOwner->GetWeapon("kiblast");

		
		if (pAttack)
			pAttack->WeaponThink(pBuddyOwner);
	}
	else
		WeaponIdle();

	return true;
}
// Buddy Generic Beam - End