#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"
#include "customentity.h"

#define CANDY_TRAIL_WIDTH 10

/**
* Candy attack
* @version 19-2-2002
* @author Herwin 'harSens' van Welbergen
*/
class CCandyAttack:public CBasePlayerWeapon
{	
public:
	/**
	* Init function
	*/
	void Spawn( void );

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

	/**
	 * Deploys the candy attack
	 * @return BOOL: true if succesfully deployes
	 */
	BOOL Deploy();

	/**
	 * Holsters attack
	 * @param skiplocal:?
	 */
	void Holster(int skiplocal = 0);


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

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

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

	/**
	* Stops shooting the attack
	*/
	void SecondaryAttack( void );

	/**
	* Weapon idle function
	*/
	void WeaponIdle();
private:
	CBeam *m_pCandyBeam;
	CSprite *m_pCandyStart;
	// Added by Pcjoe
	bool m_blShooting;
};

LINK_ENTITY_TO_CLASS( weapon_candy, CCandyAttack );

void CCandyAttack::Spawn( void )
{	Precache();
	m_iId = WEAPON_CANDY;
	m_iDefaultAmmo = 0;
	m_iClip = -1;

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

void CCandyAttack::Precache( void )
{	PRECACHE_MODEL("sprites/candystart.spr");
	PRECACHE_MODEL("sprites/candytrail.spr");
	PRECACHE_SOUND("weapons/candyattack.wav");
}

BOOL CCandyAttack::Deploy()
{
	m_pCandyBeam = CBeam::BeamCreate("sprites/candytrail.spr",CANDY_TRAIL_WIDTH);
	m_pCandyBeam->pev->rendermode = kRenderTransAdd;
	m_pCandyBeam->SetBrightness(255);
	m_pCandyBeam->SetEndEntity(m_pPlayer->entindex());
	m_pCandyBeam->SetEndAttachment(2);
	m_pCandyBeam->SetStartPos(m_pPlayer->pev->origin);
	m_pCandyBeam->SetType( BEAM_ENTPOINT );
	m_pCandyBeam->RelinkBeam();
	m_pCandyBeam->SetScrollRate(10);
	m_pCandyBeam->pev->effects |= EF_NODRAW;		
	
	m_pCandyStart = CSprite::SpriteCreate( "sprites/candystart.spr", m_pPlayer->pev->origin, true);
	m_pCandyStart->SetAttachment(m_pPlayer->edict(),2);
	m_pCandyStart->SetTransparency( kRenderTransAdd, 255, 255, 255, 254, kRenderFxGlowShell );
	m_pCandyStart->pev->framerate = 10;
	m_pCandyStart->SetScale(0.2);
	m_pCandyStart->TurnOff();
	// Added by Pcjoe
	m_blShooting = false;

	return DefaultDeploy("candyattack");
}

void CCandyAttack::Holster(int skiplocal)
{	UTIL_Remove(m_pCandyBeam);
	UTIL_Remove(m_pCandyStart);
	// added by Pcjoe
	if(m_blShooting)
	{
		// Emit null sound
		EMIT_SOUND(ENT(m_pPlayer->pev), CHAN_WEAPON, "common/null.wav", 1.0, ATTN_NORM);
		m_blShooting = false;
	}
}

int CCandyAttack::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 = 4;
	p->iPosition = 5;
	p->iId = m_iId = WEAPON_CANDY;
	p->iWeight = CANDY_WEIGHT;
	p->iFlags = 0;
	return 1;
}

void CCandyAttack::WeaponIdle(void)
{
	m_pCandyStart->TurnOff();
	m_pCandyBeam->pev->effects |= EF_NODRAW;
	if(m_blShooting)
	{
		m_blShooting = false;
		// Added by Pcjoe: Emit sound
		EMIT_SOUND(ENT(m_pPlayer->pev), CHAN_WEAPON, "common/null.wav", 1.0, ATTN_NORM);
	}
}

void CCandyAttack::PrimaryAttack( void )
{	// Check we have some ammo
	if (!m_pPlayer->UseKi(CANDY_KI_COST))
	{	m_pCandyStart->TurnOff();
		m_pCandyBeam->pev->effects |= EF_NODRAW;
		return;
	}

	// Added by Pcjoe
	if(!m_blShooting)
	{
		m_blShooting = true;
		// Added by Pcjoe: Emit sound
		EMIT_SOUND(ENT(m_pPlayer->pev), CHAN_WEAPON, "weapons/candyattack.wav", 1.0, ATTN_NORM);
	}

	//set the right animation
	m_pPlayer->SetAnimation( PLAYER_ATTACK1 );	

	//calculate crosshair pos
	UTIL_MakeVectors(m_pPlayer->pev->v_angle);
	Vector vecAiming = gpGlobals->v_forward;
	Vector vecSrc = m_pPlayer->GetGunPosition();
	TraceResult tr;
	UTIL_TraceLine (vecSrc,vecSrc + vecAiming * CANDYATTACK_RADIUS, dont_ignore_monsters, ENT(pev), &tr );
	
	//do damage if hit a player THROW_WALLSTICK
	CBaseEntity *pEntity = CBaseEntity::Instance(tr.pHit);
	// Edited by Pcjoe, reduce damage by 25% if the player is being "tossed"
	float flReduction = 1;
	if(pEntity && pEntity->IsPlayer())
	{	CBasePlayer *pPlayer = (CBasePlayer*)pEntity;
		if(pPlayer->m_Melee.InThrow())
		{	if(pPlayer->m_Melee.SpecialLaserReduction())
			{	flReduction = .5;
			}
			else
			{	flReduction = .25;
			}
		}
	}
	pEntity->TakeDamage(m_pPlayer->pev,m_pPlayer->pev,CANDY_DAMAGE * GetPowerRatio() * flReduction,DMG_CANDY);
	// end edit by Pcjoe
	m_pCandyStart->TurnOn();
	m_pCandyBeam->pev->effects &= ~EF_NODRAW;
	m_pCandyBeam->SetStartPos(tr.vecEndPos);	

	m_flNextPrimaryAttack = UTIL_WeaponTimeBase() + 0.2;
}

void CCandyAttack::SecondaryAttack()
{	//ignore pressing of secondary attack.
	WeaponIdle();
}