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

extern int gmsgCharge;

LINK_ENTITY_TO_CLASS( weapon_charged, CChargedAttack );

BOOL CChargedAttack::Deploy( void )
{	InitChargeSprite();
	return DefaultDeploy(m_pAttackName);
}

void CChargedAttack::Holster(int skiplocal)
{	if (m_pPlayer->m_fCharging)
	{	m_pPlayer->m_flSlowDown=1.0;
		m_pPlayer->m_fCharging=false;
		
		// Modified by Rick
		/*
		//undo drawing charge bar
		MESSAGE_BEGIN( MSG_ONE, gmsgCharge, NULL, m_pPlayer->pev ); 
			WRITE_BYTE(0);	//amount charged in %
		MESSAGE_END();*/

		m_pPlayer->DrawChargeBar(0); // undo drawing charge bar

		DisableChargeSprite();
		SilenceVoice();
		SilenceWeaponSounds();
	}
	m_pPlayer->StopAniTrigger(PLAYER_ATTACK1);
	
	//is this one needed? It won't harm anyways :P
	m_pPlayer->StopAniTrigger(PLAYER_ATTACK2);	
	RemoveChargeSprite();
}

void CChargedAttack::Shoot()	
{	m_pPlayer->m_flSlowDown=1.0;
	m_pPlayer->m_fCharging=false;

	/* Modified by Rick
	//undo drawing charge bar
	MESSAGE_BEGIN( MSG_ONE, gmsgCharge, NULL, m_pPlayer->pev ); 
		WRITE_BYTE(0);	//disable it
	MESSAGE_END();*/

	m_pPlayer->DrawChargeBar(0); // undo drawing charge bar

	//prevent mass shooting
	if (m_iCharge <= m_iMaxCharge/3)
	{	SilenceVoice();
		SilenceWeaponSounds();
		return;
	}
		
	//set the right animation
	m_pPlayer->SetAnimation( PLAYER_ATTACK1 );

	EmitShootSound();
	
	//shoot the attack
	ShootAttack();
	
	//should be called last, in case the charge sprite IS the attack (e.g. telekinesis)
	DisableChargeSprite();
}

void CChargedAttack::WeaponIdle()
{	if(m_pPlayer->m_fCharging)
	{	Shoot();				
	}
}		

void CChargedAttack::PrimaryAttack( void )
{	if (!m_pPlayer->m_fCharging)	//start charging
	{	m_pPlayer->m_fCharging = true;		
		
		m_pPlayer->StopAniTrigger(PLAYER_ATTACK1);
		
		//needed?
		m_pPlayer->StopAniTrigger(PLAYER_ATTACK2);

		m_pPlayer->m_flSlowDown=m_flPlayerSlowDown;
		m_iCharge=0;		
		EmitChargeSound();
	}
	else if (m_iCharge<m_iMaxCharge)
	{	//Check we have some ammo
		if (!m_pPlayer->UseKi(m_iKiCost))
		{	Shoot();			
		}	
		else
		{
			if (m_iCharge==m_iMaxCharge/3)
			{	EnableChargeSprite();
			}
			else if (m_iCharge>m_iMaxCharge/3)
			{	GrowChargeSprite();
			}			
			
			m_iCharge++;

			/* Modified by Rick
			//draw charge bar
			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); // draw charge bar
		}
	}
	/* remove auto-shoot
	else
	{	Shoot();
	}
	*/	
	m_flNextPrimaryAttack = UTIL_WeaponTimeBase() + CHARGETIME;
}

void CChargedAttack::SilenceVoice()
{	EMIT_SOUND(ENT(m_pPlayer->pev), CHAN_WEAPON, "common/null.wav", 1.0, ATTN_NORM);
}

void CChargedAttack::SilenceWeaponSounds()
{	EMIT_SOUND(ENT(m_pPlayer->pev), CHAN_VOICE, "common/null.wav", 1.0, ATTN_NORM);
}