void WorldSession::HandleAddDynamicTargetOpcode(WorldPacket & recvPacket)
{

	DEBUG_LOG( "WORLD"," got CMSG_PET_CAST_SPELL." );
	uint64 guid;
	uint8 counter;
	uint32 spellid;
	uint8 flags;
	UnitPointer caster;
	SpellCastTargets targets;
	SpellEntry *sp;
	SpellPointer pSpell;
	list<AI_Spell*>::iterator itr;

	recvPacket >> guid >> counter >> spellid >> flags;
	sp = dbcSpell.LookupEntry(spellid);

	// Summoned Elemental's Freeze
    if (spellid == 33395)
	{
		caster = _player->m_Summon;
		if( caster && TO_PET(caster)->GetAISpellForSpellId(spellid) == NULL )
			return;
	}
	else
	{
		caster = _player->m_CurrentCharm;
		if( caster != NULL )
		{
			for(itr = caster->GetAIInterface()->m_spells.begin(); itr != caster->GetAIInterface()->m_spells.end(); ++itr)
			{
				if( (*itr)->spell->Id == spellid )
					break;
			}

			if( itr == caster->GetAIInterface()->m_spells.end() )
				return;
		}
	}

	if( caster == NULL || guid != caster->GetGUID() )
		return;
	
	targets.read(recvPacket, _player->GetGUID());

	pSpell = SpellPointer(new Spell(caster, sp, false, NULLAURA));
	pSpell->prepare(&targets);
}
    void AIUpdate()
	{
		if( spark_timer < getMSTime() )
		{
			spark_timer = 0;
			_unit->UnRoot();
			_unit->GetAIInterface()->disable_combat = false;
			_unit->m_invisible = false;
			_unit->UpdateVisibility();
			for( uint8 i=0; i<SPARKS_COUNT; i++)
				if( sparks[i] != 0 )
				{
					UnitPointer Spark = _unit->GetMapMgr()->GetUnit( sparks[i] );
					if( Spark )
					{
						Spark->RemoveAllAuras();
						Spark->GetAIInterface()->MoveTo(_unit->GetPositionX(), _unit->GetPositionY(), _unit->GetPositionZ(), _unit->GetOrientation());
						Spark->Root();
						Spark->GetAIInterface()->disable_combat = true;
					}
				}			
		}
		if( spark_timer == 0 && spells.size() > 0 )
		{
			for( uint8 i = 0; i<spells.size(); i++ )
			{
				if( spells[i]->time < getMSTime() )
				{
					if( Rand( spells[i]->chance ) )
					{
						CastScriptSpell( spells[i] );
						spells[i]->time = getMSTime() + spells[i]->timer;
					}
				}
			}
		}
    }
	void OnCombatStart(UnitPointer mTarget) 
	{
		_unit->GetAIInterface()->m_canMove = false;
		_unit->GetAIInterface()->disable_melee = true;
		_unit->SetUInt64Value(UNIT_FIELD_FLAGS, UNIT_FLAG_NOT_SELECTABLE);

		UnitPointer antusul = NULLUNIT;
		antusul = _unit->GetMapMgr()->GetInterface()->GetCreatureNearestCoords(1815.030029f, 686.817017f, 14.519000f, 8127);
		if(antusul)
		{
			if(antusul->isAlive())
			{
				antusul->GetAIInterface()->AttackReaction(mTarget, 0, 0);
				antusul->SendChatMessage(CHAT_MSG_MONSTER_YELL, LANG_UNIVERSAL, "Lunch has arrived, my beutiful childern. Tear them to pieces!");
			}
		}
	}
    void AIUpdate()
    {
		if (!HasSummoned && _unit->GetHealthPct() <= 8)
		{
			UnitPointer Warchief = NULLUNIT;
			Warchief =_unit->GetMapMgr()->GetInterface()->SpawnCreature(CN_REND_BLACKHAND, 157.366516f, -419.779358f, 110.472336f, 3.056772f, true, false, 0, 0);
			if (Warchief != NULL)
			{
				if (_unit->GetAIInterface()->GetNextTarget() != NULL)
				{
					Warchief->GetAIInterface()->AttackReaction(_unit->GetAIInterface()->GetNextTarget(), 1, 0);
				}
			}

			HasSummoned = true;
		}
		
		float val = (float)RandomFloat(100.0f);
        SpellCast(val);
    }
    void AIUpdate()
    {
		if (!HasSummoned && _unit->GetHealthPct() <= 25)
		{
			UnitPointer cGizrul = NULLUNIT;
			cGizrul =_unit->GetMapMgr()->GetInterface()->SpawnCreature(CN_GIZRUL, -195.100006f, -321.970001f, 65.424400f, 0.016500f, true, false, 0, 0);
			if (cGizrul != NULL)
			{
				if (_unit->GetAIInterface()->GetNextTarget() != NULL)
				{
					cGizrul->GetAIInterface()->AttackReaction(_unit->GetAIInterface()->GetNextTarget(), 1, 0);
				}
			}

			HasSummoned = true;
		}

		float val = (float)RandomFloat(100.0f);
        SpellCast(val);
    }
	void OnDamageTaken(UnitPointer mAttacker, float fAmount)
	{
		if( (int)( last_creation_hp - DISPARSE_HP ) >= _unit->GetHealthPct() )
		{
			switch(rand()%2)
			{
			case 0:
				_unit->SendChatMessage( CHAT_MSG_MONSTER_YELL, LANG_UNIVERSAL, "The slightest spark shall be your undoing." );
				break;
			case 1:
				_unit->SendChatMessage( CHAT_MSG_MONSTER_YELL, LANG_UNIVERSAL, "No one is safe!" );
				break;
			}
			spark_timer = getMSTime() + SPARK_PHASE_DURATION;
			last_creation_hp = _unit->GetHealthPct();
			_unit->CastSpell(_unit, 52770, true);//disperse, dummy
			_unit->Root();
			_unit->GetAIInterface()->disable_combat = false;
			_unit->m_invisible = false;
			_unit->UpdateVisibility();
			//disparse
			for( uint8 i=0; i<SPARKS_COUNT; i++)
				if( sparks[i] != 0 )
				{
					UnitPointer Spark = _unit->GetMapMgr()->GetUnit( sparks[i] );
					if( Spark )
					{
						uint32 spellid = heroic ? 59833 : 52667;
						Spark->CastSpell(Spark, spellid, true);
						Spark->UnRoot();
						_unit->GetAIInterface()->disable_combat = false;
						PlayerPointer p_target = GetRandomPlayerTarget();
						if( p_target )
						{
							Spark->GetAIInterface()->MoveTo(p_target->GetPositionX(),p_target->GetPositionY(), p_target->GetPositionZ(), p_target->GetOrientation());
						}
					}
				}			
		}
	}
void OnEmote(PlayerPointer pPlayer, uint32 Emote, UnitPointer pUnit)
{
	pUnit = pPlayer->GetMapMgr()->GetUnit(pPlayer->GetSelection());
	if (!pUnit || !pUnit->isAlive() || pUnit->GetAIInterface()->GetNextTarget())
		return;

	// Switch For Emote Name (You do EmoteName to Script Name link).
	switch(Emote)
	{
	case EMOTE_ONESHOT_SALUTE: // <- Its EMOTE name.
		GuardsOnSalute(pPlayer, pUnit); // <- Its Link.
		break;

	case EMOTE_ONESHOT_KISS:
		GaurdsOnKiss(pPlayer, pUnit);
		break;
	
	case EMOTE_ONESHOT_WAVE:
		GuardsOnWave(pPlayer, pUnit);
		break;
	}
}
bool ChatHandler::HandleAddAIAgentCommand(const char* args, WorldSession *m_session)
{
	char* agent = strtok((char*)args, " ");
	if(!agent)
		return false;
	char* procEvent = strtok(NULL, " ");
	if(!procEvent)
		return false;
	char* procChance = strtok(NULL, " ");
	if(!procChance)
		return false;
	char* procCount = strtok(NULL, " ");
	if(!procCount)
		return false;
	char* spellId = strtok(NULL, " ");
	if(!spellId)
		return false;
	char* spellType = strtok(NULL, " ");
	if(!spellType)
		return false;
	char* spelltargetType = strtok(NULL, " ");
	if(!spelltargetType)
		return false;
	char* spellCooldown = strtok(NULL, " ");
	if(!spellCooldown)
		return false;
	char* floatMisc1 = strtok(NULL, " ");
	if(!floatMisc1)
		return false;
	char* Misc2 = strtok(NULL, " ");
	if(!Misc2)
		return false;

	UnitPointer target = m_session->GetPlayer()->GetMapMgr()->GetCreature(GET_LOWGUID_PART(m_session->GetPlayer()->GetSelection()));
	if(!target)
	{
		RedSystemMessage(m_session, "You have to select a Creature!");
		return false;
	}

	AI_Spell * sp = new AI_Spell;
	sp->agent = atoi(agent);
	sp->procChance = atoi(procChance);
	sp->procCount = atoi(procCount);
	sp->spell = dbcSpell.LookupEntry(atoi(spellId));
	sp->spellType = atoi(spellType);
	sp->spelltargetType = atoi(spelltargetType);
	sp->floatMisc1 = (float)atof(floatMisc1);
	sp->Misc2 = (uint32)atof(Misc2);
	sp->cooldown = (uint32)atoi(spellCooldown);
	sp->procCounter=0;
	sp->cooldowntime=0;
	sp->custom_pointer=false;
	sp->minrange = GetMinRange(dbcSpellRange.LookupEntry(dbcSpell.LookupEntry(atoi(spellId))->rangeIndex));
	sp->maxrange = GetMaxRange(dbcSpellRange.LookupEntry(dbcSpell.LookupEntry(atoi(spellId))->rangeIndex));
	if(sp->agent == AGENT_CALLFORHELP)
		target->GetAIInterface()->m_canCallForHelp = true;
	else if(sp->agent == AGENT_FLEE)
		target->GetAIInterface()->m_canFlee = true;
	else if(sp->agent == AGENT_RANGED)
		target->GetAIInterface()->m_canRangedAttack = true;
	else if(sp->agent == AGENT_SPELL)
		target->GetAIInterface()->addSpellToList(sp);

	std::stringstream qry;
	qry << "INSERT INTO ai_agents set entryId = '" << target->GetUInt32Value(OBJECT_FIELD_ENTRY) << "', AI_AGENT = '" << atoi(agent) << "', procChance = '" << atoi(procChance)<< "', procCount = '" << atoi(procCount)<< "', spellId = '" << atoi(spellId)<< "', spellType = '" << atoi(spellType)<< "', spelltargetType = '" << atoi(spelltargetType)<< "', spellCooldown = '" << atoi(spellCooldown)<< "', floatMisc1 = '" << atof(floatMisc1)<< "', Misc2  ='" << atoi(Misc2)<< "'";
	WorldDatabase.Execute( qry.str().c_str( ) );

	return true;
}