#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "effects.h"
#include "weapons.h"
#include "dragonball.h"
#include "shenlong.h"

LINK_ENTITY_TO_CLASS( shenlong, CShenlong );
void CShenlong::Spawn()
{	Precache();
	SET_MODEL(ENT(pev), "models/shenlong.mdl");
	pev->movetype = MOVETYPE_PUSHSTEP;
	pev->solid = SOLID_TRIGGER;
	UTIL_SetSize(pev,Vector(-128,-128,0),Vector(128,128,500));
	pev->gaitsequence = 0;
	pev->sequence = 0;
	pev->frame = 0;
	ResetSequenceInfo ( );
	for (int n=0;n<2;n++)
	{	m_pEyes[n] = CSprite::SpriteCreate( "sprites/shens_eyes.spr", pev->origin, false);
		m_pEyes[n]->SetTransparency( kRenderTransAdd, 255, 255, 255, 0, kRenderFxNone );
		m_pEyes[n]->SetAttachment(edict(),n+1);
		m_pEyes[n]->SetBrightness(0);
		m_pEyes[n]->TurnOff();
	}
}

void CShenlong::Precache()
{	PRECACHE_MODEL("models/shenlong.mdl");
	PRECACHE_MODEL("sprites/shens_eyes.spr");
	PRECACHE_SOUND("dragon/makewish1.wav");
	PRECACHE_SOUND("dragon/grantwish.wav");	
}

void CShenlong::WishGrantThink()
{	if (m_iFadeCount<0)
	{	
		EMIT_SOUND(ENT(m_pDragonBalls[0]->pev),CHAN_WEAPON,"dragon/grantwish.wav",1.0, ATTN_NONE);
		//spread dragonballs
		for (int n=0;n<7;n++)
		{	if (m_pDragonBalls[n])
			{	m_pDragonBalls[n]->Spread();
			}
		}
		UTIL_Remove(m_pEyes[0]);
		UTIL_Remove(m_pEyes[1]);
		pev->gaitsequence = 0;
		pev->sequence = LookupSequence("wish_granted");
		pev->frame = 0;
		ResetSequenceInfo( );
		
		//bleh... fuck the stupid warnings, this function should work
		SUB_StartFadeOut();
		//UTIL_Remove(this);
		return;
	}

	if (m_fEyeFadeIn)
	{	m_iEyeBrightness+=60;
		if (m_iEyeBrightness>255)
		{	m_iEyeBrightness = 255;
			m_fEyeFadeIn = false;
		}
	}
	else
	{	m_iEyeBrightness-=60;
		if (m_iEyeBrightness<0)
		{	m_iEyeBrightness = 0;
			m_iFadeCount--;
			m_fEyeFadeIn = true;			
		}
	}
	m_pEyes[0]->SetBrightness(m_iEyeBrightness);
	m_pEyes[1]->SetBrightness(m_iEyeBrightness);
		
	pev->nextthink = gpGlobals->time + 0.2;
}

void CShenlong::GrantWish()
{	for (int n=0;n<7;n++)
	{	//can't pick them up now
		if (m_pDragonBalls[n])
		{	m_pDragonBalls[n]->m_fSpreading = true;
		}
	}
	SetThink(&CShenlong::WishGrantThink);
	pev->nextthink = gpGlobals->time;
	m_pEyes[0]->TurnOn();
	m_pEyes[1]->TurnOn();
	m_iEyeBrightness = 0;
	m_fEyeFadeIn = true;
	m_iFadeCount = 6;
}

void CShenlong::Touch(CBaseEntity *pOther)
{	//someone got stuck into shenlong or bumps into shenlong
	if (pOther->IsPlayer())
	{	//make sure you can get to the dragonballs
		if (pOther->pev->origin.z > pev->origin.z)
		{	Vector vecDir = (pev->origin-pOther->pev->origin);
			if(vecDir.Length2D()>0)
			{
				//move out backward
				Vector2D vecDir2D = vecDir.Make2D();
				vecDir2D = vecDir2D.Normalize()*250;
				pOther->pev->velocity.x = vecDir2D.x;
				pOther->pev->velocity.x = vecDir2D.y;
			}
			else
			{
				//move out somewhere random
				pOther->pev->velocity.x = 250;
				pOther->pev->velocity.x = 0;
			}
		}
	}
	else if (FGlobalnameIs(pOther->pev,"magic_attack"))
	{	//turn towards the one who shot it
		if (pOther->pev->owner)
		{	entvars_t *pevOwner=VARS(pOther->pev->owner);
			pev->angles = UTIL_VecToAngles(pevOwner->origin - pev->origin);
		}
		
		//lunge
		pev->gaitsequence = 0;
		pev->sequence = LookupSequence("lounge");
		pev->frame = 0;
		ResetSequenceInfo( );	
	}
}

void CShenlong::PissedThink()
{	//EMIT_SOUND(ENT(m_pDragonBalls[0]->pev),CHAN_WEAPON,"dragon/pissed.wav",1.0, ATTN_NONE);
	pev->gaitsequence = 0;
	pev->sequence = LookupSequence("talk");
	pev->frame = 0;
	ResetSequenceInfo( );
	pev->nextthink = gpGlobals->time + 15;
}

CShenlong *CShenlong::CreateShenlong(Vector vecStart, Vector vecDir, CDragonBall *dragonballs[])
{	CShenlong *pShenlong = GetClassPtr( (CShenlong *)NULL );
	pShenlong->Spawn();
	for (int n=0;n<7;n++)
	{	pShenlong->m_pDragonBalls[n] = dragonballs[n];		
	}
	UTIL_SetOrigin(pShenlong->pev,vecStart);
	pShenlong->pev->angles = UTIL_VecToAngles(vecDir);
	
	//just one wish sound for now :(
	int i = RANDOM_LONG(1,1);
	
	//emit wish sound
	char pWishSound[255];
	sprintf(pWishSound,"dragon/makewish%d.wav",i);
	EMIT_SOUND(ENT(pShenlong->m_pDragonBalls[0]->pev),CHAN_WEAPON,pWishSound,1.0, ATTN_NONE);
	
	pShenlong->SetThink(&CShenlong::PissedThink);
	pShenlong->pev->nextthink = gpGlobals->time + 15;

	/* BBOX TEST by harSens
	extern int gmsgBoundingBox;
	MESSAGE_BEGIN( MSG_ALL, gmsgBoundingBox);
		WRITE_LONG(pShenlong->entindex());
	MESSAGE_END();
	*/

	return pShenlong;
}
