#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "monsters.h"
#include "weapons.h"
#include "nodes.h"
#include "effects.h"
#include "aura.h"
#include "classes.h"
#include "magicattack.h"
#include "explodingattack.h"
#include "player.h"
#include "gamerules.h"
#include "magicattack.h"
#include "beamattack.h"
#include "kiattack.h"

/**
* Ki-blast Weapon
* @version 9-7-2001
* @author Herwin 'harSens' van Welbergen
*/
class CKiBlast:public CBasePlayerWeapon
{	
public:
	/**
	* Init function
	*/
	void Spawn( void );

	/**
	* Precaches sprites and sounds
	*/
	void Precache( void );

	/**
	* Get kiblast slot
	* return int: the slot, is 2 for kiblast
	*/
	int iItemSlot( void ) { return 2; }

	/**
	* Get kiblast item info
	* @param ItemInfo *p: The ItemInfo gets stored here
	* @return int: Always returns 1 (why???)
	*/
	int GetItemInfo(ItemInfo *p);

	/**
	* Shoots a kiblast attack
	*/
	void PrimaryAttack( void );

	/**
	* Shoots a secundairy kiblast
	*/
	void SecondaryAttack(void);

	// Added by Pcjoe
	/**
	* Holsters Ki Blast
	*/
	void Holster(int skiplocal);

	/**
	* Called when kiblast is selected as weapon
	*/
	BOOL Deploy( void );
private:
	int m_fShootLeft;
	short m_sFlareA, m_sFlareB, m_sTrail;
};
LINK_ENTITY_TO_CLASS( weapon_kiblast, CKiBlast );

void CKiBlast::Spawn( void )
{	Precache();
	m_iId = WEAPON_KIBLAST;
	m_iDefaultAmmo = 0;
	m_iClip = -1;
	m_fShootLeft = false;

	FallInit();// get ready to fall down.
}

void CKiBlast::Precache( void )
{	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");
}

int CKiBlast::GetItemInfo(ItemInfo *p)
{	p->pszName = STRING(pev->classname);
	p->pszAmmo1 = "ki";
	p->iMaxAmmo1 = -1;	//max ammo is weapon independent
	p->pszAmmo2 = NULL;
	p->iMaxAmmo2 = -1;
	p->iMaxClip = WEAPON_NOCLIP;
	p->iSlot = 1;
	p->iPosition = 0;
	p->iId = m_iId = WEAPON_KIBLAST;
	p->iWeight = KIBLAST_WEIGHT;
	p->iFlags = 0;
	return 1;
}

// Added by Pcjoe
void CKiBlast::Holster( int skiplocal )
{
	m_pPlayer->StopAniTrigger(PLAYER_ATTACK1);
	m_pPlayer->StopAniTrigger(PLAYER_ATTACK2);	
}

BOOL CKiBlast::Deploy( void )
{	return DefaultDeploy( "kiblast" );	
}


void CKiBlast::PrimaryAttack( void )
{	// Check we have some ammo
	if (!m_pPlayer->UseKi(KIBLAST_PRIMARYKI_COST))
	{	return;
	}
	
	//emit sound
	EMIT_SOUND(ENT(m_pPlayer->pev), CHAN_WEAPON, "weapons/kiblast.wav", 1.0, ATTN_NORM);

	//play animation
	m_pPlayer->SetAnimation( PLAYER_ATTACK1 );

	//shoot it
	UTIL_MakeVectors( m_pPlayer->pev->v_angle );
	// Edited by Pcjoe
//	Vector vecSrc = m_pPlayer->GetGunPosition() + gpGlobals->v_right * 10; 
	// Move it frontwards a bit so it wont blow up in your face if your agenst the wall
	Vector vecSrc = (m_pPlayer->GetGunPosition() + gpGlobals->v_right * 10) + gpGlobals->v_forward * 5;
	Vector vecThrow = gpGlobals->v_forward * KIBLAST_SPEED;
	CKiAttack::ShootKiAttack( m_pPlayer->pev, vecSrc, vecThrow, KIBLAST_PRIMARYDAMAGE * GetPowerRatio(),m_sFlareA,m_sFlareB,m_sTrail);		
	// Edited by Pcjoe
//	m_flNextPrimaryAttack = m_flNextSecondaryAttack = UTIL_WeaponTimeBase() + 0.6;
	m_flNextPrimaryAttack = m_flNextSecondaryAttack = UTIL_WeaponTimeBase() + 0.5;
}

void CKiBlast::SecondaryAttack( void )
{	// Check we have some ammo
	if (!m_pPlayer->UseKi(KIBLAST_SECONDARYKI_COST ))
	{	return;
	}
	
	//emit sound
	EMIT_SOUND(ENT(m_pPlayer->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_pPlayer->pev->v_angle );
	Vector vecSrc;
	Vector vecThrow;
	if(m_fShootLeft)
	{	vecSrc = (m_pPlayer->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_pPlayer->SetAnimation( PLAYER_ATTACK1 );
	}
	else
	{	vecSrc = (m_pPlayer->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_pPlayer->SetAnimation( PLAYER_ATTACK2 );
	}
	m_fShootLeft = !m_fShootLeft;
	
	CKiAttack::ShootCurvedKiAttack( m_pPlayer->pev, vecSrc, vecThrow, KIBLAST_SECONDARYDAMAGE * GetPowerRatio(),m_sFlareA,m_sFlareB,m_sTrail);
	// Edited by Pcjoe
//	m_flNextPrimaryAttack = m_flNextSecondaryAttack = UTIL_WeaponTimeBase() + 0.4;
	m_flNextPrimaryAttack = m_flNextSecondaryAttack = UTIL_WeaponTimeBase() + 0.35;
}