//:: Wild Mage Spell - Mass Magical Propensity Modifier Enter Script
//::///////////////////////////////////////////////
/*
Mass Magical Propensity Modifier

Caster Level(s): Wizard / Sorcerer 7
Innate Level: 7
School: Divination
Descriptor(s): Wild Magic
Component(s): Verbal, Somatic
Range: Caster
Area of Effect / Target: 60’ diameter sphere
Duration: 1d4 + 1 round / 4 caster levels
Save: None
Spell Resistance: Yes

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

    Mass Magical Propensity Modifier is in many ways similar to the 4th level single target 
version except for a few key differences.  First, every creature within 30 feet of the 
caster (including the caster) is affected by the spell.  Second, all saves against spells are 
modified for the duration of the spell.  Each round within the area of effect a new modifier 
is calculated for each individual creature.  Third, once the spell expires the effect of the 
spell is removed from the target creature and they will make their saving throws normally.
     This powerful and risky spell may just be what it takes to turn the tide of battle to 
the favor of the Wild Mage.  However, nothing is certain with wild magic and Mass Magical 
Propensity Modifier may become a recipe for the caster’s demise.


2d2f_sp_mass_propensity_enter.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 main()
{
	object oCaster = JXGetAOERealCreator();
	int nLvl = JXGetCasterLevel(oCaster);
	object oTarget = GetEnteringObject();
	float fDur = 6.0;
	int nModifier;
	int nRand1 = Random(nLvl);
	int nRandMod1 = d2(1);
	int nRand2 = Random(nLvl);
	int nRandMod2 = d2(1);
	
	effect eVis ;
	effect eVisHit = EffectVisualEffect(VFX_HIT_SPELL_DIVINATION);	
	
	if(nRandMod1 == 1) // negative
	{
		nRand1 = nRand1 * -1;
	}
	if(nRandMod2 == 1) // negative
	{
		nRand2 = nRand2 * -1;
	}		
	
	if (spellsIsTarget(oTarget, SPELL_TARGET_SELECTIVEHOSTILE, OBJECT_SELF))
	{
		//Fire cast spell at event for the specified target
	    SignalEvent(oTarget, EventSpellCastAt(OBJECT_SELF, SPELL_MASS_PROPENSITY_MODIFIER));
	    //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_MASS_PROPENSITY_MODIFIER);
			}
			else
			{
				SendMessageToPC(oCaster, GetName(oTarget) + "'s outlook is poor.");
				eVis = EffectVisualEffect(VFX_DUR_PROPENSITY_MODIFIER_NEG);
				eVis = SetEffectSpellId(eVis,SPELL_MASS_PROPENSITY_MODIFIER);			
			}
			
			JXApplyEffectToObject(DURATION_TYPE_INSTANT,eVisHit,oTarget);
			JXApplyEffectToObject(DURATION_TYPE_TEMPORARY,eVis,oTarget,fDur);
			SetLocalInt(oTarget,"nPropensityModifier", nModifier);
			SetLocalObject(oTarget,"oPropensityCaster", oCaster);			
		}
	}	
	else		
	{			
		 SignalEvent(oTarget, EventSpellCastAt(OBJECT_SELF, SPELL_MASS_PROPENSITY_MODIFIER, FALSE));
	       	
		 if(nRand1 <= nRand2) // take the lowest. 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_MASS_PROPENSITY_MODIFIER);
			}
			else
			{
				SendMessageToPC(oCaster, GetName(oTarget) + "'s outlook is poor.");
				eVis = EffectVisualEffect(VFX_DUR_PROPENSITY_MODIFIER_NEG);
				eVis = SetEffectSpellId(eVis,SPELL_MASS_PROPENSITY_MODIFIER);			
			}
			
			JXApplyEffectToObject(DURATION_TYPE_INSTANT,eVisHit,oTarget);
			JXApplyEffectToObject(DURATION_TYPE_TEMPORARY,eVis,oTarget,fDur);
			SetLocalInt(oTarget,"nPropensityModifier", nModifier);
			SetLocalObject(oTarget,"oPropensityCaster", oCaster);		
	}	 

}