//::///////////////////////////////////////////////
//:: Wild Mage Spell - Vile Word of Discord
//::///////////////////////////////////////////////
/*
Vile Word of Discord

Caster Level(s): Wizard / Sorcerer 3
Innate Level: 3
School: Enchantment
Descriptor(s): Wild Magic
Component(s): Verbal
Range: Short
Area of Effect / Target: 30’ circle around caster
Duration: 1d4 1 + 1 rounds / 2 levels
Save: Will negates
Spell Resistance: Yes

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

     When casting the Vile Word of Discord the Wild mage 
selects a hostile creature and then speaks the most 
unspeakable, the most savory of the unsavory, and the 
unholiest of unholy insults at them.  All creatures within 
30’ feet of the caster who hear this diatribe (deaf 
creatures are immune) and fail a Will saving throw versus 
mind effects have a powerful and uncontrollable reaction to 
the casters vile words based on their first inclination.  
To determine the reaction a d100 is rolled and the result 
is selected from the following table.

D100
<1-20> - The listener is inspired to heroics. (+2 on all saves and skills).
<21-40) - The listener is deeply offended and goes into a rage. (+2 str, +2 con, +1 will save, -2 AC)
<41-52> - The listener reconsiders his allegiances. (target charmed)
<53-64> - The listener is deeply saddened. (-2 all saves, -2 all skills, -2 ab, -2 damage)
<65-76> - The listener is confused. (as confusion spell)
<77-88> - The listener breaks out into an uncontrollable fit of laughter. (as Tasha’s Hideous Laughter)
<89-100> - The listener is terrified. (as Fear spell)

     Unlike many spells the casters natural ability at 
insulting comes into play here and their charisma modifier 
is added (or subtracted) from the save dc of all listeners.
Additionally, insults have to be completely understood in 
order to have maximum effectiveness and therefore creatures 
of a different base racial type may slightly misinterpret 
the message increasing the chance they will succeed on 
their will saving throw by 4.  Finally, creatures that are 
considered mindless are completely oblivious to the casters 
rancor.


2d2f_sp_vile_word.nss
*/
//::///////////////////////////////////////////////
//:: Created By: 2DruNk2Frag
//:: Created On: 03-29-08
//:: Last Update: 03-29-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"
#include "NW_I0_GENERIC"


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

void mainSpell()
{
	object oCaster = JXGetCaster();
	int nWMLvl = GetLevelByClass(CLASS_WILDMAGE, oCaster);
	if(nWMLvl >= 3)
	{
/* 
  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();
	location lTarget = GetLocation(oCaster);
	int nLvl = JXGetCasterLevel();
	float fRadius = RADIUS_SIZE_COLOSSAL; // 30 feet
	int nModifier = GetAbilityModifier(ABILITY_CHARISMA, oCaster); // charisma counts
	
	float fDistance = GetDistanceBetween(oCaster,oTarget);
		
	// target must be within 30 feet
	if(fDistance <= 9.114f && GetIsObjectValid(oTarget) == TRUE)
	{	
		AssignCommand(oCaster, ClearAllActions());
		AssignCommand(oCaster, PlaySound("sco_lgrinevil01"));
		AssignCommand(oCaster, PlayVoiceChat(VOICE_CHAT_CUSS, oCaster));
				
		object oTarget = GetFirstObjectInShape(SHAPE_SPHERE,fRadius,lTarget,TRUE,OBJECT_TYPE_CREATURE);	
			
		while(GetIsObjectValid(oTarget) && oTarget != oCaster)
		{
	        //Fire cast spell at event for the specified target
	       SignalEvent(oTarget, EventSpellCastAt(OBJECT_SELF, SPELL_VILE_WORD));
	       
		   // spell has no effect if target is deaf
		   if(GetHasEffect(EFFECT_TYPE_DEAF,oTarget) != TRUE)
		   {
			   // spell doesn't work if creature is really really stupid
			   if (spellsIsMindless(oTarget) == FALSE)
		       {
				   //Make SR check
			       if (!MyResistSpell(OBJECT_SELF, oTarget))
				   {
				   		// the insults are not interpreted as well by diffrent racial types
						if (GetRacialType(oTarget) != GetRacialType(oCaster))
			    		{
			        		nModifier = nModifier + 4;
			    		}
					
						// make save
			            if (!/*Will Save*/ MySavingThrow(SAVING_THROW_WILL, oTarget, JXGetSpellSaveDC() - 4,SAVING_THROW_TYPE_MIND_SPELLS))
			            {	
							//no stacking
							if(GetHasSpellEffect(SPELL_VILE_WORD,oTarget) == TRUE)
							{
								RemoveSpellEffects(SPELL_VILE_WORD,oCaster,oTarget);
							} 					
							
							effect eLink;
									
			            	int nRand = Random(100);
											
							if(nRand < 20 )// heroism
							{ 
								SendMessageToPC(oCaster,GetName(oTarget) + " is inspired to heroics.");
								
								effect eAttack = EffectAttackIncrease(2);
			        			effect eSave = EffectSavingThrowIncrease(SAVING_THROW_ALL, 2, SAVING_THROW_TYPE_ALL);
			        			effect eSkill = EffectSkillIncrease(SKILL_ALL_SKILLS, 2);
			        			effect eDur = EffectVisualEffect( VFX_DUR_SPELL_HEROISM );
			
			        			eLink = EffectLinkEffects(eAttack, eSave);
			        			eLink = EffectLinkEffects(eLink, eSkill);
			        			eLink = EffectLinkEffects(eLink, eDur);
							}
							else if(nRand > 20 && nRand < 40) // rage
							{
								SendMessageToPC(oCaster,GetName(oTarget) + " is deeply offended and goes into a rage.");
							
								effect eStr = EffectAbilityIncrease(ABILITY_CONSTITUTION, 2);
							    effect eCon = EffectAbilityIncrease(ABILITY_STRENGTH, 2);
							    effect eSave = EffectSavingThrowIncrease(SAVING_THROW_WILL, 1);
							    effect eAC = EffectACDecrease(2, AC_DODGE_BONUS);
							    effect eDur = EffectVisualEffect( VFX_DUR_SPELL_RAGE );
							    eLink = EffectLinkEffects(eCon, eStr);
								eLink = EffectLinkEffects(eLink, eSave);
								eLink = EffectLinkEffects(eLink, eAC);
								eLink = EffectLinkEffects(eLink, eDur);
								//Make effect extraordinary
								eLink = ExtraordinaryEffect(eLink);
							}
							else if(nRand > 40 && nRand < 52) // charm
							{
								SendMessageToPC(oCaster,GetName(oTarget) + " The listener reconsiders his allegiances.");
								
								effect eVis = EffectVisualEffect(VFX_DUR_SPELL_CHARM_MONSTER);
								effect eCharm = EffectCharmed();
								eLink = EffectLinkEffects(eVis,eCharm);							
							}							
							else if(nRand > 52 && nRand < 64) // dispair
							{
								SendMessageToPC(oCaster,GetName(oTarget) + " is deeply saddened.");
								
								effect eSave = EffectSavingThrowDecrease(SAVING_THROW_ALL, 2, SAVING_THROW_TYPE_ALL);
								effect eSkill = EffectSkillDecrease(SKILL_ALL_SKILLS, 2);
		                        effect eDamagePenalty = EffectDamageDecrease(2);
		                        effect eAttackPenalty = EffectAttackDecrease(2);
		                        eLink = EffectLinkEffects(eDamagePenalty, eAttackPenalty);
								eLink = EffectLinkEffects(eLink,eSave);
								eLink = EffectLinkEffects(eLink,eSkill);
								
								effect eHit = EffectVisualEffect( VFX_DUR_SPELL_CRUSHING_DESP );	
		                        JXApplyEffectToObject(DURATION_TYPE_INSTANT, eHit, oTarget);
		                    }						
							else if(nRand > 64 && nRand < 76) // confusion
							{
								SendMessageToPC(oCaster,GetName(oTarget) + " is confused.");
								
								effect eConfuse = EffectConfused();
		    					effect eMind = EffectVisualEffect( VFX_DUR_SPELL_CONFUSION );
		    					effect eLink = EffectLinkEffects(eMind, eConfuse);
		    			 	}
							
							else if(nRand > 76 && nRand < 88) // hideous
							{
								SendMessageToPC(oCaster,GetName(oTarget) + " breaks out into an uncontrollable fit of laughter.");
								
								effect eLaugh = EffectKnockdown();
								effect eDur = EffectVisualEffect( VFX_DUR_SPELL_TASHA_LAUGHTER );
								eLink = EffectLinkEffects(eLaugh,eDur);
		                    	
								AssignCommand(oTarget, ClearAllActions());
			                    AssignCommand(oTarget, PlayVoiceChat(VOICE_CHAT_LAUGH));
			                    AssignCommand(oTarget, ActionPlayAnimation(ANIMATION_LOOPING_TALK_LAUGHING));
			                }
							else if(nRand > 88) // fear	
							{
								SendMessageToPC(oCaster,GetName(oTarget) + " is terrified.");
								
								effect eVis = EffectVisualEffect( VFX_DUR_SPELL_FEAR );	
	    						effect eFear = EffectFrightened();
								eLink = EffectLinkEffects( eFear, eVis );
							}
								
							int nDur = d4(1) + (nLvl / 2);
							float fDur = RoundsToSeconds(nDur);
		
							//Check for metamagic extend
			    			fDur = ApplyMetamagicDurationMods(fDur);
			    			int nDurType = ApplyMetamagicDurationTypeMods(DURATION_TYPE_TEMPORARY);
							
							eLink = SetEffectSpellId(eLink,SPELL_VILE_WORD);	
												
							DelayCommand(0.3,JXApplyEffectToObject(nDurType, eLink, oTarget, fDur));
			   				
						}// end will save fail
			        } // end SR fail
				}// end mindless check
	    	}// deaf check
		oTarget = GetNextObjectInShape(SHAPE_SPHERE,fRadius,lTarget,TRUE,OBJECT_TYPE_CREATURE);	
		}// end while loop
	}// end target and distance check
	}
	else
	{
	SendMessageToPC(oCaster, "You must be at least a level 3 Wild Mage to use this spell");
	}
	
	
	JXPostSpellCastCode();
	return;
}