//::///////////////////////////////////////////////
//:: Wild Mage Spell - Random Spell 1 to 6 utility 
//::///////////////////////////////////////////////
/*

This spell casts a random spell from level 1 to 6.  Used as a
utility script in cases there a wild surge occurs with out an 
original caster.

The level of the randomly selected spell is determined according
to the following probabilities.
Level 1 - 10%
Level 2 - 20%
Level 3 - 25%
Level 4 - 20%
Level 5 - 15%
Level 6 - 10%


2d2f_sp_random_spell_utility.nss
*/
//::///////////////////////////////////////////////
//:: Created By: 2DruNk2Frag
//:: Created On: 03-24-08
//:: Last Update: 03-24-08
//::///////////////////////////////////////////////

#include "2d2f_includes"
#include "jx_inc_magic"
#include "x2_inc_spellhook" 
#include "jx_inc_magic_info"
#include "nwn2_inc_spells"
#include "nw_i0_spells"



void mainSpell();void main()
{
	AssignCommand(JXGetCaster(), mainSpell());
}

void mainSpell()
{
	
/* 
  Spellcast Hook Code 
  Added 2003-06-23 by GeorgZ
  If you want to make changes to all spells,
  check x2_inc_spellhook.nss to find out more
  
*/

    if (!X2PreSpellCastCode())
    {
	// If code within the PreSpellCastHook (i.e. UMD) reports FALSE, do not run this spell
        { JXPostSpellCastCode(); return; }
    }
	//:://////////////////////////////////////////
	
	
	object oCaster = JXGetCaster();
	//int nBaseSpellId = GetSpellId();//JXGetSpellId(oCaster);
	//int nWMLvl = GetLevelByClass(CLASS_WILDMAGE, oCaster);
	object oTarget = JXGetSpellTargetObject(oCaster);
	int nRandomLvl = Random(100) + 1;
	int nRand;
	int nCasterLvl = JXGetCasterLevel();;
	int nSpellId;	
	
	if(nCasterLvl == 0)
	{
		nCasterLvl = 9;
	}
	
	if(nRandomLvl <= 10) // level 1 10%
	{
		nRand = Random(19); // 0 to Random - 1
			
		switch(nRand)
		{
			case	0	: nSpellId = 	10	; break; //	Burning_Hands
			case	1	: nSpellId = 	16	; break; //	Charm_Person
			case	2	: nSpellId = 	24	; break; //	Color_Spray
			case	3	: nSpellId = 	50	; break; //	Endure_Elements
			case	4	: nSpellId = 	66	; break; //	Grease
			case	5	: nSpellId = 	102	; break; //	Mage_Armor
			case	6	: nSpellId = 	107	; break; //	Magic_Missile
			case	7	: nSpellId = 	138	; break; //	Protection_from_Evil
			case	8	: nSpellId = 	139	; break; //	Protection_from_Good
			case	9	: nSpellId = 	143	; break; //	Ray_of_Enfeeblement
			case	10	: nSpellId = 	155	; break; //	Cause_Fear
			case	11	: nSpellId = 	165	; break; //	Sleep
			case	12	: nSpellId = 	415	; break; //	True_Strike
			case	13	: nSpellId = 	417	; break; //	Shield
			case	14	: nSpellId = 	456	; break; //	Expeditious_Retreat
			case	15	: nSpellId = 	544	; break; //	Magic_Weapon
			case	16	: nSpellId = 	846	; break; //	Enlarge_Person
			case	17	: nSpellId = 	847	; break; //	Shocking_Grasp
			case	18	: nSpellId = 	848	; break; //	Low_Light_Vision
			default : break;
		}
	}
	else if(nRandomLvl >= 11 && nRandomLvl <= 30) // level 2 20%
	{
		nRand = Random(33); // 0 to Random - 1
			
		switch(nRand)
		{
			case	0	: nSpellId =	36	; break; //	Darkness
			case	1	: nSpellId =	115	; break; //	Melfs_Acid_Arrow
			case	2	: nSpellId =	1057	; break; //	Scorching_Ray_Single
			case	3	: nSpellId =	8	; break; //	Blindness_and_Deafness
			case	4	: nSpellId =	75	; break; //	Gust_of_Wind
			case	5	: nSpellId =	94	; break; //	Lesser_Dispel
			case	6	: nSpellId =	192	; break; //	Web
			case	7	: nSpellId =	854	; break; //	Scare
			case	8	: nSpellId =	1035	; break; //	Curse_of_Blades
			case	9	: nSpellId =	120	; break; //	Ghostly_Visage
			case	10	: nSpellId =	157	; break; //	See_Invisibility
			case	11	: nSpellId =	519	; break; //	Death_Armor
			case	12	: nSpellId =	850	; break; //	False_Life
			case	13	: nSpellId =	851	; break; //	Fireburst
			case	14	: nSpellId =	852	; break; //	Mirror_Image
			case	15	: nSpellId =	1001	; break; //	Body_of_the_Sun
			case	16	: nSpellId =	436	; break; //	BalagarnsIronHorn
			case	17	: nSpellId =	457	; break; //	Tashas_Hideous_Laughter
			case	18	: nSpellId =	520	; break; //	Gedlees_Electric_Loop
			case	19	: nSpellId =	569	; break; //	Cloud_of_Bewilderment
			case	20	: nSpellId =	9	; break; //	Bulls_Strength
			case	21	: nSpellId =	13	; break; //	Cats_Grace
			case	22	: nSpellId =	49	; break; //	Bears_Endurance
			case	23	: nSpellId =	64	; break; //	Ghoul_Touch
			case	24	: nSpellId =	90	; break; //	Invisibility
			case	25	: nSpellId =	150	; break; //	Resist_Energy
			case	26	: nSpellId =	354	; break; //	Eagle_Splendor
			case	27	: nSpellId =	355	; break; //	Owls_Wisdom
			case	28	: nSpellId =	356	; break; //	Foxs_Cunning
			case	29	: nSpellId =	518	; break; //	Combust
			case	30	: nSpellId =	849	; break; //	Blindsight
			case	31	: nSpellId =	853	; break; //	Protection_From_Arrows
			case	32	: nSpellId =	1051	; break; //	Touch_Of_Idiocy
			default : break;
		}
	}
	else if(nRandomLvl >= 31 && nRandomLvl <= 55) // level 3 25%
	{
		nRand = Random(25); // 0 to Random - 1
			
		switch(nRand)
		{			
			case	0	: nSpellId =	58	; break; //	Fireball
			case	1	: nSpellId =	59	; break; //	Flame_Arrow
			case	2	: nSpellId =	41	; break; //	Dispel_Magic
			case	3	: nSpellId =	83	; break; //	Hold_Person
			case	4	: nSpellId =	101	; break; //	Lightning_Bolt
			case	5	: nSpellId =	171	; break; //	Stinking_Cloud
			case	6	: nSpellId =	526	; break; //	Scintillating_Sphere
			case	7	: nSpellId =	859	; break; //	Rage
			case	8	: nSpellId =	1036	; break; //	Mass_Curse_Of_Blades
			case	9	: nSpellId =	92	; break; //	Invisibility_Sphere
			case	10	: nSpellId =	78	; break; //	Haste
			case	11	: nSpellId =	166	; break; //	Slow
			case	12	: nSpellId =	523	; break; //	Mestils_Acid_Breath
			case	13	: nSpellId =	855	; break; //	Deep_Slumber
			case	14	: nSpellId =	861	; break; //	Weapon_of_Impact
			case	15	: nSpellId =	1009	; break; //	Power_Word_Weaken
			case	16	: nSpellId =	1010	; break; //	Power_Word_Maladroit
			case	17	: nSpellId =	137	; break; //	Protection_from_Energy
			case	18	: nSpellId =	188	; break; //	Vampiric_Touch
			case	19	: nSpellId =	458	; break; //	Displacement
			case	20	: nSpellId =	539	; break; //	Keen_Edge
			case	21	: nSpellId =	545	; break; //	Greater_Magic_Weapon
			case	22	: nSpellId =	857	; break; //	Heroism
			case	23	: nSpellId =	858	; break; //	Improved_Mage_Armor
			case	24	: nSpellId =	860	; break; //	Spiderskin
			default : break;
		}
	}
	else if(nRandomLvl >= 56 && nRandomLvl <= 75) // level 4 20%
	{
		nRand = Random(33); // 0 to Random - 1
			
		switch(nRand)
		{
			case	0	: nSpellId = 	2	; break; //	Animate_Dead
			case	1	: nSpellId = 	4	; break; //	Bestow_Curse
			case	2	: nSpellId = 	15	; break; //	Charm_Monster
			case	3	: nSpellId = 	26	; break; //	Confusion
			case	4	: nSpellId = 	27	; break; //	Contagion
			case	5	: nSpellId = 	47	; break; //	Elemental_Shield
			case	6	: nSpellId = 	52	; break; //	Enervation
			case	7	: nSpellId = 	54	; break; //	Fear
			case	8	: nSpellId = 	88	; break; //	Greater_Invisibility
			case	9	: nSpellId = 	98	; break; //	Lesser_Spell_Breach
			case	10	: nSpellId = 	119	; break; //	Lesser_Globe_of_Invulnerability
			case	11	: nSpellId = 	127	; break; //	Phantasmal_Killer
			case	12	: nSpellId = 	146	; break; //	Remove_Curse
			case	13	: nSpellId = 	172	; break; //	Stoneskin
			case	14	: nSpellId = 	191	; break; //	Wall_of_Fire
			case	15	: nSpellId = 	344	; break; //	SHADOW_CON_Summon_Shadow
			case	16	: nSpellId = 	345	; break; //	SHADOW_CON_Darkness
			case	17	: nSpellId = 	346	; break; //	SHADOW_CON_Inivsibility
			case	18	: nSpellId = 	347	; break; //	SHADOW_CON_Mage_Armor
			case	19	: nSpellId = 	348	; break; //	SHADOW_CON_Magic_Missile
			case	20	: nSpellId = 	368	; break; //	Ice_Storm
			case	21	: nSpellId = 	375	; break; //	Evards_Black_Tentacles
			case	22	: nSpellId = 	387	; break; //	Polymorph_SWORD_SPIDER
			case	23	: nSpellId = 	388	; break; //	Polymorph_TROLL
			case	24	: nSpellId = 	389	; break; //	Polymorph_UMBER_HULK
			case	25	: nSpellId = 	390	; break; //	Polymorph_GARGOYLE
			case	26	: nSpellId = 	391	; break; //	Polymorph_MINDFLAYER
			case	27	: nSpellId = 	447	; break; //	Isaacs_Lesser_Missile_Storm
			case	28	: nSpellId = 	862	; break; //	Assay_Resistance
			case	29	: nSpellId = 	863	; break; //	Crushing_Despair
			case	30	: nSpellId = 	967	; break; //	Least_Spell_Mantle
			case	31	: nSpellId = 	1013	; break; //	Greater_Resistance
			case	32	: nSpellId = 	1037	; break; //	Shout
			default : break;
		}
	
	}
	else if(nRandomLvl >= 76 && nRandomLvl <= 90) // level 5 15%
	{
		nRand = Random(19); // 0 to Random - 1
			
		switch(nRand)
		{
			case	0	: nSpellId = 	23	; break; //	Cloudkill
			case	1	: nSpellId = 	25	; break; //	Cone_of_Cold
			case	2	: nSpellId = 	40	; break; //	Dismissal
			case	3	: nSpellId = 	45	; break; //	Dominate_Person
			case	4	: nSpellId = 	55	; break; //	Feeblemind
			case	5	: nSpellId = 	82	; break; //	Hold_Monster
			case	6	: nSpellId = 	95	; break; //	Lesser_Mind_Blank
			case	7	: nSpellId = 	96	; break; //	Lesser_Planar_Binding
			case	8	: nSpellId = 	99	; break; //	Lesser_Spell_Mantle
			case	9	: nSpellId = 	118	; break; //	Mind_Fog
			case	10	: nSpellId = 	440	; break; //	Firebrand
			case	11	: nSpellId = 	459	; break; //	Bigbys_Interposing_Hand
			case	12	: nSpellId = 	869	; break; //	Greater_Fireburst
			case	13	: nSpellId = 	871	; break; //	Shroud_of_Flame
			case	14	: nSpellId = 	873	; break; //	Vitriolic_Sphere
			case	15	: nSpellId = 	1016	; break; //	Cacophonic_Burst
			case	16	: nSpellId = 	1033	; break; //	Wall_Of_Dispel_Magic
			case	17	: nSpellId = 	1046	; break; //	Power_Word_Disable
			case	18	: nSpellId = 	1133	; break; //	Glass_Doppelganger
			default : break;
		}
	}
	else if(nRandomLvl >= 91 && nRandomLvl <= 100) // level 6 10%
	{
		nRand = Random(30); // 0 to Random - 1
			
		switch(nRand)
		{
			case	0	: nSpellId = 	0	; break; //	Acid_Fog
			case	1	: nSpellId = 	14	; break; //	Chain_Lightning
			case	2	: nSpellId = 	18	; break; //	Circle_of_Death
			case	3	: nSpellId = 	30	; break; //	Create_Undead
			case	4	: nSpellId = 	65	; break; //	Globe_of_Invulnerability
			case	5	: nSpellId = 	67	; break; //	Greater_Dispel_Magic
			case	6	: nSpellId = 	72	; break; //	Greater_Spell_Breach
			case	7	: nSpellId = 	74	; break; //	Greater_Stoneskin
			case	8	: nSpellId = 	121	; break; //	Ethereal_Visage
			case	9	: nSpellId = 	128	; break; //	Planar_Binding
			case	10	: nSpellId = 	184	; break; //	Tensers_Transformation
			case	11	: nSpellId = 	186	; break; //	True_Seeing
			case	12	: nSpellId = 	376	; break; //	Legend_Lore
			case	13	: nSpellId = 	448	; break; //	Isaacs_Greater_Missile_Storm
			case	14	: nSpellId = 	460	; break; //	Bigbys_Forceful_Hand
			case	15	: nSpellId = 	485	; break; //	Flesh_to_stone
			case	16	: nSpellId = 	486	; break; //	Stone_to_flesh
			case	17	: nSpellId = 	528	; break; //	Undeath_to_Death
			case	18	: nSpellId = 	875	; break; //	Disintegrate
			case	19	: nSpellId = 	876	; break; //	Greater_Heroism
			case	20	: nSpellId = 	878	; break; //	Stone_Body
			case	21	: nSpellId = 	1014	; break; //	Superior_Resistance
			case	22	: nSpellId = 	1017	; break; //	Mass_Contagion
			case	23	: nSpellId = 	1024	; break; //	Mass_Bear_Endurance
			case	24	: nSpellId = 	1025	; break; //	Mass_Bull_Strength
			case	25	: nSpellId = 	1026	; break; //	Mass_Owl_Wisdom
			case	26	: nSpellId = 	1027	; break; //	Mass_Cat_Grace
			case	27	: nSpellId = 	1028	; break; //	Mass_Eagle_Splendor
			case	28	: nSpellId = 	1029	; break; //	Mass_Fox_Cunning
			case	29	: nSpellId = 	1058	; break; //	Extract_Water_Elemental
			default : break;
		}
	
	}
	else
	{
		nSpellId = SPELL_LIGHT;	
	}

	object oFirstPC = GetFirstPC();
	SendMessageToPC(oFirstPC,"From the weave a spell takes shape : " + JXGetSpellName(nSpellId));
		
	float fRange = JXGetSpellRange(nSpellId);
	float fDistance = GetDistanceBetween(oCaster, oTarget);
	location lLoc;
	if (fDistance > fRange)
	{
		vector vCaster = GetPosition(oCaster);
		vector vTarget = GetPosition(oTarget);
		vector vNewSource = Vector(vCaster.x + (vTarget.x - vCaster.x) / 2,
								   vCaster.y + (vTarget.y - vCaster.y) / 2);
		lLoc = Location(GetArea(oCaster), vNewSource, 0.0f);
	}
	else
	{
		lLoc = GetLocation(oCaster);
	}
		
	SignalEvent(oTarget,EventSpellCastAt(oCaster,nSpellId,TRUE));
	
	JXCastSpellFromLocationAtObject(lLoc,
									oTarget,
									nSpellId,
									METAMAGIC_NONE,
									nCasterLvl,
									JXGetSpellSaveDC());
	JXPostSpellCastCode();
	return;
}