//::///////////////////////////////////////////////
//:: Wild Mage Spell - Random Spell 0 - IV
//::///////////////////////////////////////////////
/*

Random Spell I - IV

Caster Level(s): Wizard / Sorcerer 0,1,2,3,4
Innate Level: 0,1,2,3,4
School: Evocation
Descriptor(s): Wild Magic
Component(s): Verbal, Somatic
Range: Medium
Area of Effect / Target: Single Creature
Duration: Special
Save: Special
Spell Resistance: Special

     These risky spells draw upon the chaos of magic to form 
magical energy into a spell of more power than the wild mage 
could otherwise be casting. Random Spell 0, when cast,
creates an effect equal to a randomly selected spell of 1st 
level.  All other levels of Random Spell create a random 
spell of one or two levels higher than the level of the 
Random Spell cast.
     The caster chooses one creature within 30 yards as the 
spell's target. The created spell will be centered on that 
creature, unless it is of the type that only affects the 
caster.
      The advantages of Random Spell are the range which is 
30 yards regardless of the duplicated spell's original 
range, and the fact that the wild mage doesn't need to be 
able to cast the spell or even have it in his spellbook. 
On the backside, the caster has no knowledge about which 
spell will be duplicated, not even if the spell's effect is 
harmful or helpful.


2d2f_sp_random_spell.nss
*/
//::///////////////////////////////////////////////
//:: Created By: 2DruNk2Frag
//:: Created On: 03-17-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()
{
	object oCaster = JXGetCaster();
	int nBaseSpellId = GetSpellId();//JXGetSpellId(oCaster);
	int nWMLvl = GetLevelByClass(CLASS_WILDMAGE, oCaster);
	int nLevelCheck = 0;
	if(nWMLvl >= 1){
		if(nBaseSpellId == 3645 || nBaseSpellId == 3646 || nBaseSpellId == 3647){
			nLevelCheck = 1;
		} 
	}
	if(nWMLvl >= 3 && nBaseSpellId == 3648){
		nLevelCheck = 1;
	}
	if(nWMLvl >= 5 && nBaseSpellId == 3649){
		nLevelCheck = 1;
	}
	
	if((nLevelCheck == 1) || (GetObjectType(oCaster) == OBJECT_TYPE_PLACEABLE))
	{
/* 
  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 oTarget = JXGetSpellTargetObject(oCaster);
	int nRand;
	int nCasterLvl;
	int nSpellId;	
	
	if(oCaster == GetFirstPC())
	{
		nCasterLvl = JXGetCasterLevel();
	}
	else
	{
		nCasterLvl = 9;
	}	
	
	if(nBaseSpellId == 3645) // random spell 0
	{
		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(nBaseSpellId == 3646) // random spell I
	{
		nRand = Random(58); // 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
			case	33	: nSpellId =	58	; break; //	Fireball
			case	34	: nSpellId =	59	; break; //	Flame_Arrow
			case	35	: nSpellId =	41	; break; //	Dispel_Magic
			case	36	: nSpellId =	83	; break; //	Hold_Person
			case	37	: nSpellId =	101	; break; //	Lightning_Bolt
			case	38	: nSpellId =	171	; break; //	Stinking_Cloud
			case	39	: nSpellId =	526	; break; //	Scintillating_Sphere
			case	40	: nSpellId =	859	; break; //	Rage
			case	41	: nSpellId =	1036	; break; //	Mass_Curse_Of_Blades
			case	42	: nSpellId =	92	; break; //	Invisibility_Sphere
			case	43	: nSpellId =	78	; break; //	Haste
			case	44	: nSpellId =	166	; break; //	Slow
			case	45	: nSpellId =	523	; break; //	Mestils_Acid_Breath
			case	46	: nSpellId =	855	; break; //	Deep_Slumber
			case	47	: nSpellId =	861	; break; //	Weapon_of_Impact
			case	48	: nSpellId =	1009	; break; //	Power_Word_Weaken
			case	49	: nSpellId =	1010	; break; //	Power_Word_Maladroit
			case	50	: nSpellId =	137	; break; //	Protection_from_Energy
			case	51	: nSpellId =	188	; break; //	Vampiric_Touch
			case	52	: nSpellId =	458	; break; //	Displacement
			case	53	: nSpellId =	539	; break; //	Keen_Edge
			case	54	: nSpellId =	545	; break; //	Greater_Magic_Weapon
			case	55	: nSpellId =	857	; break; //	Heroism
			case	56	: nSpellId =	858	; break; //	Improved_Mage_Armor
			case	57	: nSpellId =	860	; break; //	Spiderskin
			default : break;
		}
	}
	else if(nBaseSpellId == 3647) // random spell II
	{
		nRand = Random(58); // 0 to Random - 1
			
		switch(nRand)
		{
			case	0	: nSpellId = 	41	; break; //	Dispel_Magic
			case	1	: nSpellId = 	58	; break; //	Fireball
			case	2	: nSpellId = 	59	; break; //	Flame_Arrow
			case	3	: nSpellId = 	78	; break; //	Haste
			case	4	: nSpellId = 	83	; break; //	Hold_Person
			case	5	: nSpellId = 	92	; break; //	Invisibility_Sphere
			case	6	: nSpellId = 	101	; break; //	Lightning_Bolt
			case	7	: nSpellId = 	137	; break; //	Protection_from_Energy
			case	8	: nSpellId = 	166	; break; //	Slow
			case	9	: nSpellId = 	171	; break; //	Stinking_Cloud
			case	10	: nSpellId = 	188	; break; //	Vampiric_Touch
			case	11	: nSpellId = 	458	; break; //	Displacement
			case	12	: nSpellId = 	523	; break; //	Mestils_Acid_Breath
			case	13	: nSpellId = 	526	; break; //	Scintillating_Sphere
			case	14	: nSpellId = 	539	; break; //	Keen_Edge
			case	15	: nSpellId = 	545	; break; //	Greater_Magic_Weapon
			case	16	: nSpellId = 	855	; break; //	Deep_Slumber
			case	17	: nSpellId = 	857	; break; //	Heroism
			case	18	: nSpellId = 	858	; break; //	Improved_Mage_Armor
			case	19	: nSpellId = 	859	; break; //	Rage
			case	20	: nSpellId = 	860	; break; //	Spiderskin
			case	21	: nSpellId = 	861	; break; //	Weapon_of_Impact
			case	22	: nSpellId = 	1009	; break; //	Power_Word_Weaken
			case	23	: nSpellId = 	1010	; break; //	Power_Word_Maladroit
			case	24	: nSpellId = 	1036	; break; //	Mass_Curse_Of_Blades
			case	25	: nSpellId = 	2	; break; //	Animate_Dead
			case	26	: nSpellId = 	4	; break; //	Bestow_Curse
			case	27	: nSpellId = 	15	; break; //	Charm_Monster
			case	28	: nSpellId = 	26	; break; //	Confusion
			case	29	: nSpellId = 	27	; break; //	Contagion
			case	30	: nSpellId = 	47	; break; //	Elemental_Shield
			case	31	: nSpellId = 	52	; break; //	Enervation
			case	32	: nSpellId = 	54	; break; //	Fear
			case	33	: nSpellId = 	88	; break; //	Greater_Invisibility
			case	34	: nSpellId = 	98	; break; //	Lesser_Spell_Breach
			case	35	: nSpellId = 	119	; break; //	Lesser_Globe_of_Invulnerability
			case	36	: nSpellId = 	127	; break; //	Phantasmal_Killer
			case	37	: nSpellId = 	146	; break; //	Remove_Curse
			case	38	: nSpellId = 	172	; break; //	Stoneskin
			case	39	: nSpellId = 	191	; break; //	Wall_of_Fire
			case	40	: nSpellId = 	344	; break; //	SHADOW_CON_Summon_Shadow
			case	41	: nSpellId = 	345	; break; //	SHADOW_CON_Darkness
			case	42	: nSpellId = 	346	; break; //	SHADOW_CON_Inivsibility
			case	43	: nSpellId = 	347	; break; //	SHADOW_CON_Mage_Armor
			case	44	: nSpellId = 	348	; break; //	SHADOW_CON_Magic_Missile
			case	45	: nSpellId = 	368	; break; //	Ice_Storm
			case	46	: nSpellId = 	375	; break; //	Evards_Black_Tentacles
			case	47	: nSpellId = 	387	; break; //	Polymorph_SWORD_SPIDER
			case	48	: nSpellId = 	388	; break; //	Polymorph_TROLL
			case	49	: nSpellId = 	389	; break; //	Polymorph_UMBER_HULK
			case	50	: nSpellId = 	390	; break; //	Polymorph_GARGOYLE
			case	51	: nSpellId = 	391	; break; //	Polymorph_MINDFLAYER
			case	52	: nSpellId = 	447	; break; //	Isaacs_Lesser_Missile_Storm
			case	53	: nSpellId = 	862	; break; //	Assay_Resistance
			case	54	: nSpellId = 	863	; break; //	Crushing_Despair
			case	55	: nSpellId = 	967	; break; //	Least_Spell_Mantle
			case	56	: nSpellId = 	1013	; break; //	Greater_Resistance
			case	57	: nSpellId = 	1037	; break; //	Shout
			default : break;
		}
	
	}
	else if(nBaseSpellId == 3648) // random spell III
	{
		nRand = Random(52); // 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
			case	33	: nSpellId = 	23	; break; //	Cloudkill
			case	34	: nSpellId = 	25	; break; //	Cone_of_Cold
			case	35	: nSpellId = 	40	; break; //	Dismissal
			case	36	: nSpellId = 	45	; break; //	Dominate_Person
			case	37	: nSpellId = 	55	; break; //	Feeblemind
			case	38	: nSpellId = 	82	; break; //	Hold_Monster
			case	39	: nSpellId = 	95	; break; //	Lesser_Mind_Blank
			case	40	: nSpellId = 	96	; break; //	Lesser_Planar_Binding
			case	41	: nSpellId = 	99	; break; //	Lesser_Spell_Mantle
			case	42	: nSpellId = 	118	; break; //	Mind_Fog
			case	43	: nSpellId = 	440	; break; //	Firebrand
			case	44	: nSpellId = 	459	; break; //	Bigbys_Interposing_Hand
			case	45	: nSpellId = 	869	; break; //	Greater_Fireburst
			case	46	: nSpellId = 	871	; break; //	Shroud_of_Flame
			case	47	: nSpellId = 	873	; break; //	Vitriolic_Sphere
			case	48	: nSpellId = 	1016	; break; //	Cacophonic_Burst
			case	49	: nSpellId = 	1033	; break; //	Wall_Of_Dispel_Magic
			case	50	: nSpellId = 	1046	; break; //	Power_Word_Disable
			case	51	: nSpellId = 	1133	; break; //	Glass_Doppelganger
			default : break;
		}
	}
	else if(nBaseSpellId == 3649) // random spell VI
	{
		nRand = Random(49); // 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
			case	19	: nSpellId = 	0	; break; //	Acid_Fog
			case	20	: nSpellId = 	14	; break; //	Chain_Lightning
			case	21	: nSpellId = 	18	; break; //	Circle_of_Death
			case	22	: nSpellId = 	30	; break; //	Create_Undead
			case	23	: nSpellId = 	65	; break; //	Globe_of_Invulnerability
			case	24	: nSpellId = 	67	; break; //	Greater_Dispel_Magic
			case	25	: nSpellId = 	72	; break; //	Greater_Spell_Breach
			case	26	: nSpellId = 	74	; break; //	Greater_Stoneskin
			case	27	: nSpellId = 	121	; break; //	Ethereal_Visage
			case	28	: nSpellId = 	128	; break; //	Planar_Binding
			case	29	: nSpellId = 	184	; break; //	Tensers_Transformation
			case	30	: nSpellId = 	186	; break; //	True_Seeing
			case	31	: nSpellId = 	376	; break; //	Legend_Lore
			case	32	: nSpellId = 	448	; break; //	Isaacs_Greater_Missile_Storm
			case	33	: nSpellId = 	460	; break; //	Bigbys_Forceful_Hand
			case	34	: nSpellId = 	485	; break; //	Flesh_to_stone
			case	35	: nSpellId = 	486	; break; //	Stone_to_flesh
			case	36	: nSpellId = 	528	; break; //	Undeath_to_Death
			case	37	: nSpellId = 	875	; break; //	Disintegrate
			case	38	: nSpellId = 	876	; break; //	Greater_Heroism
			case	39	: nSpellId = 	878	; break; //	Stone_Body
			case	40	: nSpellId = 	1014	; break; //	Superior_Resistance
			case	41	: nSpellId = 	1017	; break; //	Mass_Contagion
			case	42	: nSpellId = 	1024	; break; //	Mass_Bear_Endurance
			case	43	: nSpellId = 	1025	; break; //	Mass_Bull_Strength
			case	44	: nSpellId = 	1026	; break; //	Mass_Owl_Wisdom
			case	45	: nSpellId = 	1027	; break; //	Mass_Cat_Grace
			case	46	: nSpellId = 	1028	; break; //	Mass_Eagle_Splendor
			case	47	: nSpellId = 	1029	; break; //	Mass_Fox_Cunning
			case	48	: 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());
	
	}
	else
	{
	SendMessageToPC(oCaster, "You must be a Wild Mage to use this spell");
	}
	
	
	JXPostSpellCastCode();
	return;
}