#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "monsters.h"
#include "weapons.h"
#include "nodes.h"
#include "soundent.h"
#include "decals.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"

LINK_ENTITY_TO_CLASS( magicattack, CMagicAttack );

void CMagicAttack::Spawn( void )
{	Precache();
	pev->gravity = 0.000001;//magic is not affected by gravity
	
	pev->globalname = MAKE_STRING("magic_attack");
	pev->rendermode = kRenderTransAdd;
	pev->renderamt = 254;
	pev->effects |= EF_BRIGHTLIGHT;	
}

void CMagicAttack::Precache( void )
{	
}

void CMagicAttack::Remove(void)
{	// silence flysound
	EMIT_SOUND(ENT(pev), CHAN_WEAPON, "common/null.wav", 1.0, ATTN_NORM);	
	UTIL_Remove(this);
}

BOOL CMagicAttack::BlocksAttack(CBasePlayer *pPlayer)
{	if (pPlayer->m_fBlock)
	{	//make sure we deal with positive angles
		int iPlayerYaw = ( (int)pPlayer->pev->v_angle.y + 360)%360;
		int iKiblastYaw = ( (int)pev->angles.y + 540)%360;
	
		//check if the player is facing the attack
		if(iPlayerYaw < iKiblastYaw + 45)
		{	if (iPlayerYaw > iKiblastYaw - 45)
			{	return true;
			}
		}
	}
	return false;
}

void CMagicAttack::SwatAway(CBasePlayer *pPlayer, int fRandomSwat)
{	//push away to the right/left front of the player
	Vector vecDir;
	UTIL_MakeVectors( pPlayer->pev->v_angle );
	if (fRandomSwat)
	{	float flDir = RANDOM_FLOAT(-1,1);
		Vector vecRightDir = flDir * gpGlobals->v_right;
		vecDir = (vecRightDir + gpGlobals->v_forward).Normalize();
		EMIT_SOUND(ENT(pPlayer->pev), CHAN_WEAPON, "weapons/reflect.wav", 1.0, ATTN_NORM);		
		if (flDir<0)
		{	pPlayer->SetAnimation( PLAYER_SWATAWAY_LEFT);
		}
		else
		{	pPlayer->SetAnimation( PLAYER_SWATAWAY_RIGHT);
		}
	}
	else 
	{	vecDir = gpGlobals->v_forward;
		pPlayer->SetAnimation(PLAYER_SWATAWAY_FORWARD);
	}

	UTIL_SetOrigin(pev,pPlayer->GetGunPosition()+gpGlobals->v_forward * (pev->maxs.x+10));

	//change owner
	pev->owner = ENT(pPlayer->pev);	

	SetThink(&CMagicAttack::NormalThink);	
	pev->nextthink = gpGlobals->time + 0.5;

	// Added by Pcjoe
	pPlayer->m_fSwat = true;

	pev->velocity = vecDir * m_flSpeed;
	pev->angles = UTIL_VecToAngles(pev->velocity);	
}

void CMagicAttack::DrawBubbles()
{	//put in bubble trail if underwater
	if (pev->waterlevel == 3)	
	{	UTIL_BubbleTrail( pev->origin - pev->velocity * 0.1, pev->origin, 4 );
	} 
}

void CMagicAttack::AnimateSprite()
{	//animate sprites
	int iMaxFrames = MODEL_FRAMES( pev->modelindex ) - 1;
	if (iMaxFrames>0)
	{	pev->frame ++;
		if ( pev->frame > iMaxFrames )
		{	pev->frame = 0;
		}
	}	
}

void CMagicAttack::NormalThink( void )
{	//remove magic if it has moved out of the world
	if (!IsInWorld())
	{	Remove();
		return;
	}
	DrawBubbles();
	AnimateSprite();
	/*
	//HACK... HACK... fix speed to max speed (2000 for now)
	if (pev->velocity.Length() > 2000)
	{	pev->velocity = pev->velocity.Normalize() * 2000;
	}	
	*/
	pev->nextthink = gpGlobals->time + 0.4;
}
