#include "extdll.h" 
#include "decals.h" 
#include "util.h" 
#include "cbase.h" 
#include "monsters.h" 
#include "talkmonster.h"
#include "schedule.h"
#include "defaultai.h"
#include "animation.h"
#include "weapons.h" 
#include "soundent.h" 
#include "shake.h" 
#include "gamerules.h" 
#include "effects.h"
#include "player.h"
#include "../bot/bot_util.h"
#include "buddy_aura.h"
#include "buddy.h"
#include "buddy_classes.h"
#include "classes.h"
#include "magicattack.h"
#include "explodingattack.h"
#include	"../engine/studio.h"
#include	"classes.h"

extern Vector g_veCBuddyAuraColor[];
extern const char *g_pAuraColorString[];

LINK_ENTITY_TO_CLASS( buddy_aura, CBuddyAura );

void CBuddyAura::Spawn()
{	Precache();
	SetTouch( NULL );	
	SET_MODEL(ENT(pev),"models/aura.mdl");
	
	//ingenious tweak by Joe: don't let the aura tough ANYTHING
	pev->solid = SOLID_NOT;
	
	pev->rendermode = kRenderTransAdd;
	pev->renderamt = 25;
	pev->takedamage = DAMAGE_NO;
	
	pev->gaitsequence = 0;
	pev->sequence = 0;
	pev->frame = 0;

	ResetSequenceInfo ( );	
	// Removed by Pcjoe
//	m_fHasTrail = false;
}

void CBuddyAura::Precache()
{	PRECACHE_MODEL("models/aura.mdl");
	PRECACHE_MODEL("sprites/aura.spr");
	PRECACHE_SOUND("weapons/aura.wav");
	// Added by Pcjoe
	PRECACHE_SOUND("weapons/swoop.wav");
	m_sTrailSprite = PRECACHE_MODEL("sprites/auratrail.spr");
}

void CBuddyAura::RemoveThink()
{	entvars_t *pevOwner;
	if (pev->owner)
	{	pevOwner=VARS(pev->owner);
		if (pevOwner->solid == SOLID_NOT)
		{	Destroy();
		}
	}
	else
	{	Destroy();
	}
	
	if (m_pBuddy->m_bFlying)
	{	pev->angles = m_pBuddy->pev->v_angle;
		// Removed by Pcjoe
	/*	if(!m_fHasTrail)
		{	m_fHasTrail = true;
			Vector vecTrailColor = g_veCBuddyAuraColor[pev->skin];
			MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY );
				WRITE_BYTE  (TE_BEAMFOLLOW);			//create a line of decaying beam segments until entity stops moving
				WRITE_SHORT	(entindex()|0x1000);		//entity:attachment to follow)
				WRITE_SHORT	(m_sTrailSprite);			//short (sprite index)
				WRITE_BYTE	(25);						//life in 0.1's) 
				WRITE_BYTE	(25);						//(line width in 0.1's) 
				WRITE_BYTE  (vecTrailColor.x);			//byte,byte,byte (color)
				WRITE_BYTE  (vecTrailColor.y);			
				WRITE_BYTE  (vecTrailColor.z);
				WRITE_BYTE  (254);						//brightness
			MESSAGE_END();
		}*/
	}
	
	// Removed by Pcjoe
/*	if (!m_pBuddy->m_bFlying && m_fHasTrail)
	{	m_fHasTrail = false;
		// remove all trails from entity
		MESSAGE_BEGIN ( MSG_PVS, SVC_TEMPENTITY, pev->origin );
			WRITE_BYTE( TE_KILLBEAM	);	//kill all beams attached to entity
			WRITE_SHORT( m_pBuddy->entindex());	// short (entity)
		MESSAGE_END();	
	}*/

	//pev->nextthink=gpGlobals->time + 0.5;
	pev->nextthink=gpGlobals->time + 0.016f;
}

void CBuddyAura::SetColor()
{
	pev->skin = m_pBuddy->m_pClass->GetAuraSkin();
	pev->rendercolor = Vector(5,5,5);
}

CBuddyAura *CBuddyAura::Create(CBuddy *pevOwner)
{	CBuddyAura *pAura = GetClassPtr( (CBuddyAura *)NULL );
	pAura->Spawn();
	UTIL_SetOrigin( pAura->pev, pevOwner->GetGunPosition());
	pAura->m_pBuddy = pevOwner;
	pAura->pev->owner = pevOwner->edict();
	pAura->SetThink(&CBuddyAura::RemoveThink);
	pAura->pev->nextthink = gpGlobals->time + 0.5;
	
	pAura->pev->aiment = pevOwner->edict();
	pAura->pev->movetype = MOVETYPE_FOLLOW;
	pAura->SetColor();
	pAura->pev->renderfx = kRenderFxGlowShell;


	pAura->m_pBuddy->pev->rendermode = kRenderTransTexture;
	pAura->m_pBuddy->pev->renderamt = 255;	
	
	// Pcjoe : Edited to play the swoop melee sound if your swooping
	if(pAura->m_pBuddy->m_Swoop.bActive)
	{	EMIT_SOUND(ENT(pAura->m_pBuddy->pev), CHAN_ITEM, "weapons/swoop.wav", 1, ATTN_NORM);
	}
	//emit aura sound
	else
	{	EMIT_SOUND(ENT(pAura->m_pBuddy->pev), CHAN_ITEM, "weapons/aura.wav", 1, ATTN_NORM);
	}

	// Pcjoe: "Bend" aura clientside
	UTIL_Model_AuraBend(pAura->entindex());
	return pAura;	
}

void CBuddyAura::Destroy()
{	EMIT_SOUND(ENT(m_pBuddy->pev), CHAN_ITEM, "common/null.wav", 1, ATTN_NORM);
	// Removed by Pcjoe
/*	if (m_fHasTrail)
	{	// remove all trails from entity
		MESSAGE_BEGIN ( MSG_PVS, SVC_TEMPENTITY, pev->origin );
			WRITE_BYTE( TE_KILLBEAM	);				//kill all beams attached to entity
			WRITE_SHORT( m_pBuddy->entindex());	// short (entity)
		MESSAGE_END();
	}*/
	// Pcjoe: Remove the bend for the aura
	UTIL_Model_RemoveAll(entindex());

	UTIL_Remove(this);
	m_pBuddy->pev->rendermode = kRenderNormal;	
}

int	CBuddyAura::TakeDamage( entvars_t* pevInflictor, entvars_t* pevAttacker, float flDamage, int bitsDamageType )
{	return m_pBuddy->TakeDamage( pevInflictor, pevAttacker, flDamage, bitsDamageType);
}