#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "effects.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 "../engine/shake.h"

#define ATTACK_IDLE  0
#define ATTACK_DRAIN 1

/**
* Ki Drain
* @version redo: 8-31-2005
* @author Ian
*/
class CKiDrain:public CBasePlayerWeapon
{	
public:
	/**
	* Init function
	*/
	void Spawn( void );

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

	/*
	* Holsters solarflare
	*/
	void Holster(int skiplocal = 0);

	/*
	* Deploys solarflare
	*/
	BOOL Deploy( void );

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

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

	void PrimaryAttack(void);
private:
	void EmitShootSound();
	float m_fCooldown; // this is when we can use this attack again
	float m_fDrainEnd; // this is when our cuurent attack must end
	float m_fNextDrain;
	int m_iAttackState;
	int m_iTicks;
};

LINK_ENTITY_TO_CLASS( weapon_kidrain, CKiDrain );

void CKiDrain::Spawn( void )
{	Precache();
	m_iId = WEAPON_KIDRAIN;
	m_iDefaultAmmo = 0;
	m_iClip = -1;
	m_fCooldown = m_fDrainEnd = gpGlobals->time;
	m_iAttackState = ATTACK_IDLE;
	FallInit();// get ready to fall down.
}

BOOL  CKiDrain::Deploy()
{	
	// we're going to need our own animation
	return DefaultDeploy("kiblast");
}

void CKiDrain::Precache( void )
{
	PRECACHE_SOUND("weapons/solarflare.wav");
}

int CKiDrain::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 = 3;
	p->iPosition = 0;
	p->iId = m_iId = WEAPON_KIDRAIN;
	p->iWeight = KIDRAIN_WEIGHT;
	p->iFlags = 0;
	return 1;
}

void CKiDrain::EmitShootSound()
{	//m_pPlayer->EmitClassSound("solarflare.wav");	
	EMIT_SOUND(ENT(m_pPlayer->pev), CHAN_WEAPON, "weapons/solarflare.wav", 1.0, ATTN_NORM);
}
	
void CKiDrain::Holster(int skiplocal)
{
	m_iAttackState = ATTACK_IDLE;
}

void CKiDrain::PrimaryAttack()	
{	
	// drain!
	if ((m_iAttackState == ATTACK_IDLE) && (gpGlobals->time > m_fCooldown))
	{
		int iKiUse = m_pPlayer->pev->fuser4 / KIDRAIN_PERCENT_COST;

		// have to have at least this much ki to ride this ride
		if (iKiUse < KIDRAIN_MIN_COST)
			return;

		m_pPlayer->UseKi(iKiUse);

		m_iAttackState = ATTACK_DRAIN;

		m_iTicks = KIDRAIN_DRAIN_TICKS;
	}

	if ((m_iAttackState == ATTACK_IDLE) && (gpGlobals->time <= m_fCooldown))
	{
		
		float temp;
		temp = m_fCooldown - gpGlobals->time;
		ALERT( at_console, "Cooldown reaminig: %.0f", temp);
	}

	if ((m_iAttackState == ATTACK_DRAIN) && (m_iTicks >= 0))
	{
		//if (m_fDrainEnd > gpGlobals->time)
		{
			//if (m_fNextDrain >= gpGlobals->time)
			{
				// search around for people to drain ki from
				m_iTicks--;

				CBaseEntity *pPlayer = NULL;
				EmitShootSound();

				while ((pPlayer = UTIL_FindEntityInSphere( pPlayer, m_pPlayer->pev->origin, KIDRAIN_RADIUS )) != NULL)
				{
					if ((pPlayer->IsPlayer()) && (pPlayer != m_pPlayer))
					{
						float fDistance = (pev->origin - pPlayer->pev->origin).Length();
						
						
						//int kidrain = KIDRAIN_DRAIN_AMT - fDistance;
						float kidrain;

						if (fDistance > 150)
							kidrain = 0.1f;
						else if (fDistance > 100)
							kidrain = 0.125;
						else if (fDistance > 50)
							kidrain = 0.18f;
						else
							kidrain = 0.25f;

						float newKi = pPlayer->pev->fuser4 - (pPlayer->pev->fuser4 * kidrain);
						if (newKi < 0) newKi = 0;
						float kiLoss = pPlayer->pev->fuser4 - newKi;

						ALERT( at_console, "Enemy now has %0.f had %.0f from a distance of %.0f", newKi, pPlayer->pev->fuser4, fDistance);
						if (kiLoss > 0)
						{
							pPlayer->pev->fuser4 = newKi;
							if (pPlayer->pev->fuser4 < 0)
								pPlayer->pev->fuser4 = 0;
						}

						// just an idea
						//
						// if you get drained maybe you should also be stunned momentarily?
						CBasePlayer *pEnemy = (CBasePlayer*)pPlayer;
						if (pEnemy)
						{																					
							/*if (pEnemy->m_fTurbo)
							{
								if (RANDOM_LONG(0,10) < KIDRAIN_STOPTURBO_PERCENT)
								{
									pEnemy->StopTurbo();
								}
							}

							if (pEnemy->m_fFlying)
							{
								if (RANDOM_LONG(0,10) < KIDRAIN_STOPFLY_PERCENT)
								{								
									pEnemy->StopFly();
								}
							}*/

							if (RANDOM_LONG(0,10) < KIDRAIN_STOPFLY_PERCENT)
							{
								pEnemy->StopFly();
								pEnemy->StopTurbo();
								pEnemy->StopPowerUp();
							}

						}
					
					}
				}
				m_pPlayer->SetAnimation( PLAYER_ATTACK1 );				
				m_pPlayer->TransformFX(-1);
				
				//m_fNextDrain = gpGlobals->time + KIDRAIN_DRAIN_LENGTH / KIDRAIN_DRAIN_TICKS;
				m_flNextPrimaryAttack = UTIL_WeaponTimeBase() + 0.5;
			}
		}
		//else
		//{
		//	m_iAttackState = ATTACK_IDLE;
		//}
	}
	else if ((m_iAttackState == ATTACK_DRAIN) && (m_iTicks < 0))
	{
		m_flNextPrimaryAttack = UTIL_WeaponTimeBase() + 0.1;
		m_iAttackState = ATTACK_IDLE;
		m_fCooldown = gpGlobals->time + 10;
	}
		
		
/*		
		if(m_pPlayer->pev->fuser4>SOLARFLARE_KI_COST)
		{	
			int iKiUse = SOLARFLARE_MAX_KI_COST;
			if(m_pPlayer->pev->fuser4<SOLARFLARE_MAX_KI_COST)
			{	iKiUse = m_pPlayer->pev->fuser4;
			}
			float flKiRatio = (float)iKiUse/(float)SOLARFLARE_MAX_KI_COST;
			m_pPlayer->UseKi(iKiUse);
		
			//set the right animation
			m_pPlayer->SetAnimation( PLAYER_ATTACK1 );
	
			//emit sound
			EmitShootSound();

			//do flash damage
			UTIL_MakeVectors( m_pPlayer->pev->v_angle + m_pPlayer->pev->punchangle );
			float radius = SOLARFLARE_RADIUS * flKiRatio;
			RadiusDamage( m_pPlayer->pev->origin + gpGlobals->v_forward * (radius/2 + 40), m_pPlayer->pev, m_pPlayer->pev, 0, radius, CLASS_NONE, DMG_FLASH );

			//fade the screen to white for a short time if not already blinded
			if (!m_pPlayer->m_fBlinded)
			{	UTIL_ScreenFade	( m_pPlayer, Vector (255,255,255), 1.0 , 0.5 , 255, FFADE_IN );			
			}			
		}
	}
*/

}