//::///////////////////////////////////////////////
//:: Wild Mage Spell - Nahal's Propensity Modifier
//::///////////////////////////////////////////////
/*
Nahal's Magical Propensity Modifier

Caster Level(s): Wizard / Sorcerer 4
Innate Level: 4
School: Divination
Descriptor(s): Wild Magic
Component(s): Verbal, Somatic
Range: 30 yards
Area of Effect / Target: One Creature
Duration: Special
Save: None
Spell Resistance: Yes

!!! This is a Wild Magic Spell and can only be cast by a Wild Mage !!!

     Nahal’s Magical Propensity Modifier wildly alters the tendency to which a creature is 
susceptible to magic.  The Wild Mage can not completely control this reaction but through 
exertion of will may bias the outcome in one direction or another tipping the scales of fate 
so to speak.
     This target of Nahal’s Magical Propensity Modifier rolls two random numbers that range 
from negative to positive based on the experience level of the Wild Mage who cast the 
spell.  If the target is a hostile creature the higher of these two numbers is added to 
the DC of the next spell they save against increasing their chance for failure.  If the 
creature is friendly the lower of the two numbers is used increasing the chance for success.
For Example, Shazbotian, a level 8 Wild mage ( and a level 2 Wizard ) casts this spell on a 
hostile wizard.  Two random numbers are rolled that range from negative to positive based on 
his level, in this case the range is -10 to +10.  Let’s assume the numbers rolled are -8 and 
5.  The next spell the hostile wizard makes will be made with a penalty of 5 significantly 
increasing his chance for failure.  It is possible that the two numbers rolled were both 
negative in which case Nahal’s Magical Propensity Modifier has backfired and will increase 
the chance for success against for the target against the next spell for which they need to 
save against.  Fortunately for the Wild Mage the deck is stacked in his favor and more times 
than not the outlook for hostile targets will be poor while his friends will have generally 
positive outcomes..
     Hostile targets of Nahal’s Magical Propensity Modifier are not allowed a saving throw 
to avoid its effects but any magic resistance they may have magic resistance still applies.  
The effect imbued upon the target will remain indefinably until they make their next saving 
throw against spells.



2d2f_sp_propensity_modifier.nss
*/
//::///////////////////////////////////////////////
//:: Created By: 2DruNk2Frag
//:: Created On: 04-13-08
//:: Last Update: 04-13-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 nWMLvl = GetLevelByClass(CLASS_WILDMAGE, oCaster);
	if(nWMLvl >= 5)
	{
/* 
  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();
	int nLvl = JXGetCasterLevel();
	
	effect eVis;
	effect eVisHit = EffectVisualEffect(VFX_HIT_SPELL_DIVINATION);
	
	int nModifier;
	int nRand1 = Random(nLvl);
	int nRandMod1 = d2(1);
	int nRand2 = Random(nLvl);
	int nRandMod2 = d2(1);
				
	if(nRandMod1 == 1) // negative
	{
		nRand1 = nRand1 * -1;
	}
	if(nRandMod2 == 1) // negative
	{
		nRand2 = nRand2 * -1;
	}
	
	// no changing the propensity modifier once it is determined
	if(GetHasSpellEffect(SPELL_PROPENSITY_MODIFIER, oTarget) != 1)
	{
		if (spellsIsTarget(oTarget, SPELL_TARGET_SELECTIVEHOSTILE, OBJECT_SELF))
		{
	        //Fire cast spell at event for the specified target
	        SignalEvent(oTarget, EventSpellCastAt(OBJECT_SELF, SPELL_PROPENSITY_MODIFIER_HOSTILE));
	       //Make SR check
	       if (!MyResistSpell(OBJECT_SELF, oTarget))
		   {				
				if(nRand1 >= nRand2) // take the highest. big DC is bad.
				{
					nModifier = nRand1;
				}
				else
				{
					nModifier = nRand2;
				}
				
				if(nModifier<= 0)
				{
					SendMessageToPC(oCaster, GetName(oTarget) + "'s outlook is good.");
					eVis = EffectVisualEffect(VFX_DUR_PROPENSITY_MODIFIER_POS);
					eVis = SetEffectSpellId(eVis,SPELL_PROPENSITY_MODIFIER_HOSTILE);
				}
				else
				{
					SendMessageToPC(oCaster, GetName(oTarget) + "'s outlook is poor.");
					eVis = EffectVisualEffect(VFX_DUR_PROPENSITY_MODIFIER_NEG);
					eVis = SetEffectSpellId(eVis,SPELL_PROPENSITY_MODIFIER_HOSTILE);			
				}
	            			
				JXApplyEffectToObject(DURATION_TYPE_INSTANT,eVisHit,oTarget);
	        	JXApplyEffectToObject(DURATION_TYPE_PERMANENT,eVis,oTarget);
				SetLocalInt(oTarget,"nPropensityModifier", nModifier);
				SetLocalObject(oTarget,"oPropensityCaster", oCaster);
			}
	    }// end hostile check
		else // friendly target
		{
			SignalEvent(oTarget, EventSpellCastAt(OBJECT_SELF, SPELL_PROPENSITY_MODIFIER_FRIENDLY,FALSE));
	       
			if(nRand1 <= nRand2) // take the lower. low dc is good
			{
				nModifier = nRand1;
			}
			else
			{
				nModifier = nRand2;
			}
			
			if(nModifier <= 0)
			{
				SendMessageToPC(oCaster, GetName(oTarget) + "'s outlook is good.");
				eVis = EffectVisualEffect(VFX_DUR_PROPENSITY_MODIFIER_POS);
				eVis = SetEffectSpellId(eVis,SPELL_PROPENSITY_MODIFIER_FRIENDLY);
			}
			else
			{
				SendMessageToPC(oCaster, GetName(oTarget) + "'s outlook is poor.");
				eVis = EffectVisualEffect(VFX_DUR_PROPENSITY_MODIFIER_NEG);
				eVis = SetEffectSpellId(eVis,SPELL_PROPENSITY_MODIFIER_FRIENDLY);			
			}
	            			
			JXApplyEffectToObject(DURATION_TYPE_INSTANT,eVisHit,oTarget);
	        JXApplyEffectToObject(DURATION_TYPE_PERMANENT,eVis,oTarget);
			SetLocalInt(oTarget,"nPropensityModifier", nModifier);
			SetLocalObject(oTarget,"oPropensityCaster", oCaster);
		
		
		}
	} //end effect check
	else
	{
		SendMessageToPC(oCaster, "This target's fate is already determined");	
	}
	
	
	}
	else
	{
	SendMessageToPC(oCaster, "You must be at least a level 5 Wild Mage to use this spell");
	}
	
		JXPostSpellCastCode();
	return;
}