//////////////////////////////////////////////
/*
Spell Elemental's on-hit IP

shaz_sp_spell_elemental_onhit.nss
*/
//::///////////////////////////////////////////////
//:: Created By: Shazbotian
//:: Created On: 06-03-08
//:: Last Update: 06-03-08
//::///////////////////////////////////////////////

//#include "2d2f_includes"
//#include "2d2f_misc_functions"
#include "jx_inc_magic"
//#include "x2_inc_spellhook" 
#include "jx_inc_magic_info"
//#include "nwn2_inc_spells"
//#include "nw_i0_spells"
#include "2d2f_inc_magic_wild"

//:: prototypes

//int getWeaponDamageType(object oItem);

//int getWeaponDamage(object oItem);

//int getLauncher(int nItem);

void main()
{
	object oPC = GetFirstPC();
	object oTarget = GetSpellTargetObject();
  	object oItem = GetSpellCastItem();
	object oCaster = GetItemPossessor(oItem);
	int iSpellID = GetLocalInt(oCaster, "ShazElementalSpellID");
			
	//SendMessageToPC(oPC,"Spell Elemental onhit fired, spell id: " + IntToString(iSpellID) + " caster: " + GetName(oCaster) + " target: " + GetName(oTarget));
	
	// Shaz: this method had problems: 1: the creature intended to cast the spell had to have its action queue cleared, and 2: for some reason AOE's cast by the spell elem wouldn't hit it (unlike if a player cast them), and i wanted it to
//	if(JXGetSpellRangeType(iSpellID) == JX_SPELLRANGE_PERSONAL) {
//		// if a personal spell, they must cast it on themselves, and the elemental will make them do that!
//		AssignCommand(oTarget, ClearAllActions());
//		JXCastSpellFromObjectAtObject(oTarget, oTarget, iSpellID, METAMAGIC_NONE, GetLocalInt(oCaster, "ShazElementalCastLevel"), GetLocalInt(oCaster, "ShazElementalSpellDC"));
//	} else if(JXGetSpellRangeType(iSpellID) == JX_SPELLRANGE_INVALID) {
//		// no valid spellrange - busted spell, don't do anything
//	} else {
//		AssignCommand(oCaster, ClearAllActions());
//		JXCastSpellFromObjectAtObject(oCaster, oTarget, iSpellID, METAMAGIC_NONE, GetLocalInt(oCaster, "ShazElementalCastLevel"), GetLocalInt(oCaster, "ShazElementalSpellDC"));
//	}
	if(ShazIsSummoningSpell(iSpellID)) {
		// these appear to need to be cast from the target at the target, we'll try that here
		AssignCommand(oTarget, ClearAllActions());
		JXCastSpellFromObjectAtObject(oTarget, oTarget, iSpellID, METAMAGIC_NONE, GetLocalInt(oCaster, "ShazElementalCastLevel"), GetLocalInt(oCaster, "ShazElementalSpellDC"), FALSE);
		
		//SendMessageToPC(oPC, "Random spell surge casts SELF spell on: " + GetName(oRandomTarget) + " tag: " + GetTag(oRandomTarget));
	} else {
		JXCastSpellFromLocationAtObject(GetLocation(oCaster), oTarget, iSpellID, METAMAGIC_NONE, GetLocalInt(oCaster, "ShazElementalCastLevel"), GetLocalInt(oCaster, "ShazElementalSpellDC"), FALSE, oCaster);
	}
	
	// tag that we hit this guy (our custom AI for him makes him go after someone else sometimes)
	SetLocalInt(oTarget, "SpellElemHit", 1);

}// main